perf(research): add compact replay projections and representative profiling
This commit is contained in:
parent
19e9dad68b
commit
e3bd722e31
21 changed files with 2830 additions and 130 deletions
|
|
@ -1,7 +1,12 @@
|
|||
//! Trusted host-only NAPI adapter. The TS loader authenticates D01 data with
|
||||
//! validateDatasetV2; this envelope binds identity but is not a second normalizer
|
||||
//! or an untrusted-code sandbox. No mutable financial/clock handles are exported.
|
||||
use crate::financial::{contract::*, matching::OrderStatus, replay::*};
|
||||
use crate::financial::{
|
||||
contract::*,
|
||||
matching::OrderStatus,
|
||||
replay::*,
|
||||
result::{project, FinancialProjection},
|
||||
};
|
||||
use napi::{Error, Result};
|
||||
#[cfg(not(test))]
|
||||
use napi_derive::napi;
|
||||
|
|
@ -37,6 +42,24 @@ struct Submit {
|
|||
decisions: Vec<StrategyDecision>,
|
||||
}
|
||||
|
||||
/// Borrow retained provenance and flattened financial facts instead of building
|
||||
/// a serde_json::Value tree for the entire nested journal.
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct ProjectionResult<'a> {
|
||||
format: &'static str,
|
||||
projection_version: Version,
|
||||
financial_version: Version,
|
||||
identity: &'a Identity,
|
||||
status: &'static str,
|
||||
manifest: &'a Value,
|
||||
policy: &'a ReplayConfig,
|
||||
view: Value,
|
||||
limitations: &'a [String],
|
||||
model_limitations: &'static [&'static str],
|
||||
projection: FinancialProjection<'a>,
|
||||
}
|
||||
|
||||
fn error(error: impl std::fmt::Display) -> Error {
|
||||
Error::from_reason(error.to_string())
|
||||
}
|
||||
|
|
@ -158,6 +181,29 @@ impl ReplayHost {
|
|||
pub fn dispose(&mut self) {
|
||||
self.replay = None;
|
||||
}
|
||||
|
||||
pub fn project(&self, input: String) -> Result<String> {
|
||||
let identity: Identity = parse(&input)?;
|
||||
self.check(&identity)?;
|
||||
let replay = self.replay.as_ref().unwrap();
|
||||
if replay.status() != &ReplayStatus::Complete {
|
||||
return Err(error("only a complete replay can project successfully"));
|
||||
}
|
||||
serde_json::to_string(&ProjectionResult {
|
||||
format: "projection",
|
||||
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)?,
|
||||
})
|
||||
.map_err(error)
|
||||
}
|
||||
}
|
||||
|
||||
// NAPI's impl macro requires literal method annotations. Keep this tiny
|
||||
|
|
@ -186,6 +232,10 @@ impl FinancialReplaySession {
|
|||
self.0.finalize(input)
|
||||
}
|
||||
#[napi]
|
||||
pub fn project(&self, input: String) -> Result<String> {
|
||||
self.0.project(input)
|
||||
}
|
||||
#[napi]
|
||||
pub fn dispose(&mut self) {
|
||||
self.0.dispose();
|
||||
}
|
||||
|
|
@ -274,7 +324,25 @@ mod tests {
|
|||
);
|
||||
assert_eq!(final_result["view"]["portfolio"]["cash"], "9975.76000000");
|
||||
assert_eq!(final_result["view"]["equity"], "10005.76000000");
|
||||
let before = native.replay.as_ref().unwrap().checkpoint();
|
||||
let compact = native.project(identity.to_string()).unwrap();
|
||||
assert_eq!(native.project(identity.to_string()).unwrap(), compact);
|
||||
assert_eq!(native.replay.as_ref().unwrap().checkpoint(), before);
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&native.finalize(identity.to_string()).unwrap()).unwrap(),
|
||||
final_result
|
||||
);
|
||||
let projected: Value = serde_json::from_str(&compact).unwrap();
|
||||
assert_eq!(projected["view"], final_result["view"]);
|
||||
assert!(projected.get("receipts").is_none());
|
||||
let load: Load = parse(fixture).unwrap();
|
||||
let restored = Replay::restore(load.prepared, load.config, &before).unwrap();
|
||||
assert_eq!(
|
||||
projected["projection"],
|
||||
serde_json::to_value(project(restored.financial().ledger()).unwrap()).unwrap()
|
||||
);
|
||||
native.dispose();
|
||||
assert!(native.finalize(identity.to_string()).is_err());
|
||||
assert!(native.project(identity.to_string()).is_err());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue