feat(research): add canonical session reporting and analytics

This commit is contained in:
Boki 2026-10-23 12:05:14 -04:00
parent 1e164450c3
commit 3838295eec
28 changed files with 2402 additions and 47 deletions

View file

@ -5,6 +5,7 @@ use crate::financial::{
contract::*,
matching::OrderStatus,
replay::*,
reporting::ReportingFacts,
result::{project, FinancialProjection},
};
use napi::{Error, Result};
@ -60,6 +61,15 @@ struct ProjectionResult<'a> {
projection: FinancialProjection<'a>,
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ReportResult<'a> {
#[serde(flatten)]
financial: ProjectionResult<'a>,
reporting_version: Version,
reporting: &'a ReportingFacts,
}
fn error(error: impl std::fmt::Display) -> Error {
Error::from_reason(error.to_string())
}
@ -204,6 +214,31 @@ impl ReplayHost {
})
.map_err(error)
}
pub fn report(&self, input: String) -> Result<String> {
let identity: Identity = parse(&input)?;
self.check(&identity)?;
let replay = self.replay.as_ref().unwrap();
let reporting = replay.reporting().map_err(error)?;
serde_json::to_string(&ReportResult {
financial: ProjectionResult {
format: "report",
projection_version: Version,
financial_version: Version,
identity: &self.identity,
status: "complete",
manifest: &self.manifest,
policy: &self.config,
view: view(replay)?,
limitations: replay.limitations(),
model_limitations: replay.model_limitations(),
projection: project(replay.financial().ledger()).map_err(error)?,
},
reporting_version: Version,
reporting,
})
.map_err(error)
}
}
// NAPI's impl macro requires literal method annotations. Keep this tiny
@ -236,6 +271,10 @@ impl FinancialReplaySession {
self.0.project(input)
}
#[napi]
pub fn report(&self, input: String) -> Result<String> {
self.0.report(input)
}
#[napi]
pub fn dispose(&mut self) {
self.0.dispose();
}
@ -333,6 +372,13 @@ mod tests {
final_result
);
let projected: Value = serde_json::from_str(&compact).unwrap();
let report = native.report(identity.to_string()).unwrap();
assert_eq!(native.report(identity.to_string()).unwrap(), report);
assert_eq!(native.project(identity.to_string()).unwrap(), compact);
assert_eq!(native.replay.as_ref().unwrap().checkpoint(), before);
let reported: Value = serde_json::from_str(&report).unwrap();
assert_eq!(reported["projection"], projected["projection"]);
assert_eq!(reported["reporting"]["occupiedMs"], "120000");
assert_eq!(projected["view"], final_result["view"]);
assert!(projected.get("receipts").is_none());
let load: Load = parse(fixture).unwrap();
@ -341,8 +387,13 @@ mod tests {
projected["projection"],
serde_json::to_value(project(restored.financial().ledger()).unwrap()).unwrap()
);
assert_eq!(
reported["reporting"],
serde_json::to_value(restored.reporting().unwrap()).unwrap()
);
native.dispose();
assert!(native.finalize(identity.to_string()).is_err());
assert!(native.project(identity.to_string()).is_err());
assert!(native.report(identity.to_string()).is_err());
}
}