feat(engine): add canonical risk and order matching

This commit is contained in:
Boki 2026-10-22 22:15:34 -04:00
parent b65acccd91
commit ccf333cc0a
13 changed files with 5129 additions and 36 deletions

View file

@ -1,24 +1,22 @@
{
"schema_version": 1,
"plan_revision": "2026-10-22.18",
"plan_revision": "2026-10-22.21",
"repository_base": "b7aeca4",
"plan_status": "seventh_wave_validated_uncommitted",
"plan_status": "ninth_wave_validated_uncommitted",
"execution_authorized": true,
"execution_scope": {
"authorized_by": "User: commit and continue (2026-10-22); commit accepted F01/D01 and continue with the explicitly proposed F02/D02 pair",
"authorized_by": "User: do it (2026-10-22), authorizing the explicitly proposed F05 continuation on accepted integrated F04; no commit requested",
"tasks": [
"F02",
"D02"
"F05"
],
"permissions": [
"commit_validated_sixth_wave",
"isolated_implementation",
"local_tests",
"local_integration_review",
"planning_progress_updates"
],
"excluded": [
"seventh_wave_commits_without_further_approval",
"commits_without_further_approval",
"push",
"pull_requests",
"live_or_paper_operations",
@ -368,7 +366,7 @@
"epic": "E2",
"priority": "P0",
"severity": "Critical",
"status": "review",
"status": "done",
"dependencies": [
"F01"
],
@ -392,7 +390,7 @@
"assignment": "/root/wave7_f02",
"branch": "wave7/f02-owned-ledger",
"pull_request": null,
"completed_commit": null,
"completed_commit": "b65acccd91c15a7fd82536c0c6b3a65e01055113",
"blocker": null,
"release_gates": [],
"worktree": "/tmp/stock-bot-wave7.UIkAQD/f02",
@ -403,8 +401,8 @@
"Cargo.lock",
"test/fixtures/economics/"
],
"review_notes": "Independently accepted, exact frozen files integrated, combined repository checks passed. Seventh-wave changes remain uncommitted pending owner acceptance/commit; no downstream task started.",
"integration_state": "validated_uncommitted",
"review_notes": "Independently accepted, frozen source integrated and combined gates passed. Owner authorized commit and continuation; completed_commit contains F02/D02.",
"integration_state": "validated_committed",
"reviewed_by": "/root/wave7_review",
"verification_reference": "ROADMAP.md#seventh-wave-handoff--2026-10-22"
},
@ -439,7 +437,7 @@
"epic": "E2",
"priority": "P0",
"severity": "Critical",
"status": "blocked",
"status": "review",
"dependencies": [
"F02"
],
@ -459,12 +457,25 @@
"risk-policy"
],
"parallel_policy": "Allowed only with satisfied dependencies/approvals and disjoint write locks AND actual files; see agents/integration.md.",
"assignment": null,
"branch": null,
"assignment": "/root/wave8_f04",
"branch": "wave8/f04-financial-risk",
"pull_request": null,
"completed_commit": null,
"blocker": null,
"release_gates": []
"release_gates": [],
"worktree": "/tmp/stock-bot-wave8.pKzr7p/f04",
"integration_base": "b65acccd91c15a7fd82536c0c6b3a65e01055113",
"concrete_write_scope": [
"apps/stock/engine/src/financial/"
],
"integration_state": "validated_uncommitted",
"reviewed_by": [
"/root/wave8_risk_review",
"/root/wave8_quant_review"
],
"review_notes": "Both independent reviewers and combined gates passed; owner accepted continuation to F05. Integrated F04 remains uncommitted, with frozen source hashes recorded in eighth_wave_validation.",
"verification_reference": "ROADMAP.md#eighth-wave-handoff--2026-10-22",
"accepted_by": "Owner: do it, authorizing proposed F05 continuation on validated integrated F04 (2026-10-22)"
},
{
"id": "F05",
@ -472,7 +483,7 @@
"epic": "E2",
"priority": "P0",
"severity": "Critical",
"status": "blocked",
"status": "review",
"dependencies": [
"F04"
],
@ -493,14 +504,32 @@
"financial-contract"
],
"parallel_policy": "Allowed only with satisfied dependencies/approvals and disjoint write locks AND actual files; see agents/integration.md.",
"assignment": null,
"branch": null,
"assignment": "/root/wave9_f05",
"branch": "wave9/f05-matching",
"pull_request": null,
"completed_commit": null,
"blocker": null,
"release_gates": [
"F06"
]
],
"worktree": "/tmp/stock-bot-wave9.P8EKsc/f05",
"integration_base": "b65acccd91c15a7fd82536c0c6b3a65e01055113",
"integration_overlay": {
"task": "F04",
"source_hashes_reference": "eighth_wave_validation.source_hashes",
"accepted_integration": true,
"note": "Exact validated source copied with apply_patch into isolated worktree; no implicit commit required or created."
},
"concrete_write_scope": [
"apps/stock/engine/src/financial/"
],
"integration_state": "validated_uncommitted",
"review_notes": "Both independent reviewers accepted the final frozen source; root integrated matching hashes and passed all repository checks plus the release matching measurement. No active runtime cutover; F06 remains the replay release gate.",
"reviewed_by": [
"/root/wave9_execution_review",
"/root/wave9_quant_review"
],
"verification_reference": "ROADMAP.md#ninth-wave-handoff--2026-10-22"
},
{
"id": "F06",
@ -652,7 +681,7 @@
"epic": "E3",
"priority": "P1",
"severity": "High",
"status": "review",
"status": "done",
"dependencies": [
"D01"
],
@ -670,7 +699,7 @@
"assignment": "/root/wave7_d02",
"branch": "wave7/d02-intraday-storage",
"pull_request": null,
"completed_commit": null,
"completed_commit": "b65acccd91c15a7fd82536c0c6b3a65e01055113",
"blocker": null,
"release_gates": [],
"worktree": "/tmp/stock-bot-wave7.UIkAQD/d02",
@ -681,8 +710,8 @@
"test/integration/intraday-storage.test.ts",
"test/fixtures/intraday/"
],
"review_notes": "Independently accepted, exact frozen files integrated, combined repository checks passed. Seventh-wave changes remain uncommitted pending owner acceptance/commit; no downstream task started.",
"integration_state": "validated_uncommitted",
"review_notes": "Independently accepted, frozen source integrated and combined gates passed. Owner authorized commit and continuation; completed_commit contains F02/D02.",
"integration_state": "validated_committed",
"reviewed_by": "/root/wave7_review",
"verification_reference": "ROADMAP.md#seventh-wave-handoff--2026-10-22"
},
@ -3015,6 +3044,191 @@
"Initial isolated D02 builds needed existing logger/config dependencies; native sandbox attempt timed out. Escalated tests and independent rerun passed unchanged. Existing build warnings remain."
],
"cleanup": "Only D02 stale test980112/worker980619 were terminated after exact PID/cwd checks; original session exited143 and process absence verified. No development services, databases or data touched. Worktrees/logs retained.",
"handoff": "F01/D01 committedb4af840 at owner request. F02/D02 independently accepted and validated_uncommitted, awaiting owner acceptance/commit. F04 is next prerequisite-dependent task; other ADR approvals unchanged. No push/PR/trading operation or extra task."
"handoff": "Owner subsequently authorized F02/D02 commit b65accc and F04 continuation. Seventh-wave validation remains recorded here; other ADR approvals unchanged. No push, PR or trading operation."
},
"eighth_wave": [
"F04"
],
"eighth_wave_base": "b65acccd91c15a7fd82536c0c6b3a65e01055113",
"eighth_wave_notes": [
"One isolated financial implementation owner; two read-only reviewers. Root owns planning and integration.",
"Additive RiskAccount privately owns Ledger; no active runtime cutover, F05 matching/target expansion, F06 replay, F07 NAPI or SQL/paper changes.",
"Task execution-policy clarifications pin units, fee rounding, cash/inventory commitments, event-time mark freshness, risk halts and operator-reset API authority."
],
"eighth_wave_validation": {
"base": "b65acccd91c15a7fd82536c0c6b3a65e01055113",
"integration_state": "validated_uncommitted",
"repository_check": {
"command": "CARGO_BUILD_JOBS=2 bun run check",
"log": "/tmp/stock-bot-wave8.pKzr7p/integration-check.log",
"exit_code": 0,
"passed": {
"tooling": 60,
"utils": 43,
"calculations": 35,
"integration": 342,
"rust": 77
},
"rust_manual_benchmarks_ignored": 2,
"other_gates": "Build, typecheck, contracts, lint and secret audit passed."
},
"independent_review": {
"risk": "/root/wave8_risk_review: frozen five-file hashes verified; 18 risk tests and 40 financial tests passed, overlapping totals. Log /tmp/stock-bot-wave8.pKzr7p/f04-independent-financial.log.",
"quant": "/root/wave8_quant_review: frozen hashes verified;18 risk tests passed. Reviewed once-rounded fees, owner-book exposure, fill-time commitments and atomic overflow."
},
"scoped_checks": [
"rustfmt --edition 2021 --check --config skip_children=true on five changed financial files passed.",
"git diff --check and manifest task-file/DAG/authorized dependency/ADR readiness checks passed.",
"Whole-repository rustfmt baseline differs outside owned scope; no unrelated rewrite. Planning manifest Prettier check also differs on HEAD baseline; retained existing JSON formatting."
],
"source_hashes": {
"apps/stock/engine/src/financial/ledger.rs": "f2092937bd0ef60d3267751629dae573900680edb777330a09e5ec8e7fc2d854",
"apps/stock/engine/src/financial/mod.rs": "63d0cd994ccfc7dec0a421f9d900ec46877fc2d3e1faa69dffbe3e58f0d8bddc",
"apps/stock/engine/src/financial/scalar.rs": "6150e0de5be8432ef13f5d10c2d9a9b50c0387bf8c86810d917eb347bf33d7b2",
"apps/stock/engine/src/financial/risk.rs": "9d6189177b158afc87f32ee9b9c8ffd2f24bab269de5f1fedd8c8ab515d237ce",
"apps/stock/engine/src/financial/risk_tests.rs": "bcaff321cbe84ef6253ce3552f312b0a2057553ad41ea69247a02db92822d098"
},
"native_artifact": {
"path": "apps/stock/engine/dist/engine.node",
"sha256": "3987253b57e515acbb58c1e0e83a9d63309fb36aea267de1995ccc18637e256d",
"note": "Independently built from integrated checkout; active simulator remains legacy."
},
"measurement": {
"command": "CARGO_BUILD_JOBS=2 cargo test -p engine --lib --release --offline risk_transition_measurement -- --ignored --nocapture",
"author_log": "/tmp/stock-bot-wave8.pKzr7p/f04-risk-release.log",
"integrated_log": "/tmp/stock-bot-wave8.pKzr7p/integration-risk-release.log",
"result": "1 passed, 0 failed;78 filtered. Author and integrated builds both passed; the earlier ledger-only manual measurement was not repeated this wave.",
"artifact": "target/release/deps/engine-abe56cbc3e15ae30",
"artifact_sha256": "fd30aea3b087bea9961f5e9d8dd96bf1317f5551ba652560d5538dc5c0880338",
"host": "AMD Ryzen9 9950X3D; Linux6.8 x86_64; Rust/Cargo1.88.0; build jobs2. Same artifact hash in author/integrated worktrees.",
"workload": "Prebuilt 1k/10k commands: mark10, admit2shares, fill1share, cancel remainder. Fee1%,capital1m. One active owner/asset or two active owners/four assets;3repeats,no explicit warmup. At most1live reservation. Apply includes receipt/outcome collection; checkpoint copying and restore separately timed; serialization/input generation/build excluded.",
"equality": "Full checkpoint plus returned outcomes byte-equal across three repeats; restored checkpoint byte-equal. No cross-platform determinism claim.",
"raw_integrated_rows": [
"risk multi=false commands=1000 repeat=0 apply_us=4764 checkpoint_us=639 restore_us=5273 checkpoint_bytes=990585 open_orders=0 owners=1 assets=1",
"risk multi=false commands=1000 repeat=1 apply_us=3964 checkpoint_us=303 restore_us=4891 checkpoint_bytes=990585 open_orders=0 owners=1 assets=1",
"risk multi=false commands=1000 repeat=2 apply_us=4026 checkpoint_us=292 restore_us=5049 checkpoint_bytes=990585 open_orders=0 owners=1 assets=1",
"risk multi=false commands=10000 repeat=0 apply_us=45071 checkpoint_us=7485 restore_us=58692 checkpoint_bytes=10019596 open_orders=0 owners=1 assets=1",
"risk multi=false commands=10000 repeat=1 apply_us=42218 checkpoint_us=4706 restore_us=56548 checkpoint_bytes=10019596 open_orders=0 owners=1 assets=1",
"risk multi=false commands=10000 repeat=2 apply_us=41986 checkpoint_us=4951 restore_us=57307 checkpoint_bytes=10019596 open_orders=0 owners=1 assets=1",
"risk multi=true commands=1000 repeat=0 apply_us=5195 checkpoint_us=343 restore_us=6067 checkpoint_bytes=996239 open_orders=0 owners=2 assets=4",
"risk multi=true commands=1000 repeat=1 apply_us=5171 checkpoint_us=291 restore_us=6012 checkpoint_bytes=996239 open_orders=0 owners=2 assets=4",
"risk multi=true commands=1000 repeat=2 apply_us=5189 checkpoint_us=295 restore_us=6004 checkpoint_bytes=996239 open_orders=0 owners=2 assets=4",
"risk multi=true commands=10000 repeat=0 apply_us=55959 checkpoint_us=5995 restore_us=74884 checkpoint_bytes=10070263 open_orders=0 owners=2 assets=4",
"risk multi=true commands=10000 repeat=1 apply_us=56209 checkpoint_us=6110 restore_us=74580 checkpoint_bytes=10070263 open_orders=0 owners=2 assets=4",
"risk multi=true commands=10000 repeat=2 apply_us=54665 checkpoint_us=4800 restore_us=69609 checkpoint_bytes=10070263 open_orders=0 owners=2 assets=4"
],
"limits": "Narrow component workload, not end-to-end backtesting, batch scaling or speedup evidence. Risk checks scan retained book keys and active commitments with ordered-map factors. Journal memory and restore work grow with history; no compaction."
},
"not_run": [
"Disposable PostgreSQL/Redis, browser and Python suites: persistence/API/UI/Python boundaries unchanged."
],
"limitations": [
"No active-engine cutover, F05 matcher/limit/TIF/capacity eligibility or target expansion/mode coordinator, F06 replay, F07 NAPI, or paper/SQL integration.",
"Checkpoint covers risk+ledger, not replay cursors/strategy/RNG. Reconstruction checks consistency, not authenticity.",
"Host instance operator capability is API authority separation, not authentication or an arbitrary-code sandbox.",
"RiskOutcome.replayed gates publication of all nested original financial effects. Arithmetic overflow is an atomic command error; host must fail unsupported attempts."
],
"handoff": "F02/D02 committed b65accc at owner request. F04 independently accepted and validated_uncommitted. Owner subsequently authorized F05 on this accepted source without requesting a commit; exact overlay is recorded under F05. F06 remains the replay release gate. No commit, push, PR or trading operation.",
"cleanup": "No services/data removed; isolated worktree and logs retained."
},
"ninth_wave": [
"F05"
],
"ninth_wave_base": "b65acccd91c15a7fd82536c0c6b3a65e01055113",
"ninth_wave_overlay_reference": "eighth_wave_validation.source_hashes",
"ninth_wave_notes": [
"The owner authorized F05 on accepted integrated F04 without requesting commits. Its five exact financial source files were applied and hash-verified in the isolated worktree.",
"One financial implementation owner; independent execution and quant reviewers; root owns planning/integration.",
"Additive matching/order lifecycle privately owns RiskAccount. No legacy simulator, replay scheduler, native API, paper/SQL or brokerage change.",
"F05 task clarifies price stages, shared liquidity, target omission/replacement, coordinator-vs-ledger revisions and terminal failed-attempt prefix receipts."
],
"ninth_wave_validation": {
"base": "b65acccd91c15a7fd82536c0c6b3a65e01055113",
"integration_state": "validated_uncommitted",
"prerequisite_overlay": "Accepted F04 source in eighth_wave_validation.source_hashes; no prerequisite commit required or created.",
"repository_check": {
"command": "CARGO_BUILD_JOBS=2 bun run check",
"log": "/tmp/stock-bot-wave9.P8EKsc/integration-check.log",
"exit_code": 0,
"passed": {
"tooling": 60,
"utils": 43,
"calculations": 35,
"integration": 342,
"rust": 106
},
"rust_manual_benchmarks_ignored": 3,
"other_gates": "Build, typecheck, contracts, lint and secret audit passed."
},
"independent_review": {
"execution": "/root/wave9_execution_review: final frozen five-file hashes accepted; 29 matching regressions passed, one manual benchmark ignored.",
"quant": "/root/wave9_quant_review: final frozen hashes accepted; 29 matching regressions passed. Reviewed fee/funding authority, limit pricing, physical quote eligibility, capacity dedup, target commitments and terminal partial failures."
},
"scoped_checks": [
"rustfmt --edition 2021 --check --config skip_children=true on five changed financial files passed.",
"git diff --check and manifest task-file/DAG/authorized dependency/ADR readiness checks passed.",
"Cumulative seven-file source hashes below include unchanged F04 ledger.rs/risk_tests.rs; final five F05 source files match the isolated reviewed worktree exactly."
],
"changed_source_files": [
"apps/stock/engine/src/financial/mod.rs",
"apps/stock/engine/src/financial/scalar.rs",
"apps/stock/engine/src/financial/risk.rs",
"apps/stock/engine/src/financial/matching.rs",
"apps/stock/engine/src/financial/matching_tests.rs"
],
"source_hashes": {
"apps/stock/engine/src/financial/ledger.rs": "f2092937bd0ef60d3267751629dae573900680edb777330a09e5ec8e7fc2d854",
"apps/stock/engine/src/financial/mod.rs": "45ec9613eb0db8194cc9b21979043f7edff5ad3dc0116a03209e157ae784013d",
"apps/stock/engine/src/financial/scalar.rs": "280cefdc8da4f04e1f3ad9fec999e7b7974c06504191ccace1c8eaec3768e189",
"apps/stock/engine/src/financial/risk.rs": "ad57837ef631df4b54f24dc752914964e5f6bf891aa74946e054d960584b3c64",
"apps/stock/engine/src/financial/risk_tests.rs": "bcaff321cbe84ef6253ce3552f312b0a2057553ad41ea69247a02db92822d098",
"apps/stock/engine/src/financial/matching.rs": "98cda1d4ee802c87dbf948e1bc7b40e8a5b6aff6d44b4f3e0571c27ae40cf7b6",
"apps/stock/engine/src/financial/matching_tests.rs": "b718de809e2ea76b4a31284aae946eb257d466c916eae0d00c3f174fe7dae43b"
},
"native_artifact": {
"path": "apps/stock/engine/dist/engine.node",
"sha256": "d81b0324c111ff05e54584f311a3d6f3bec907ce0e52e288ca3b97590dfe8062",
"note": "Built from integrated checkout; active simulator remains legacy."
},
"measurement": {
"command": "CARGO_BUILD_JOBS=2 cargo test -p engine --lib --release --offline matching_transition_measurement -- --ignored --nocapture",
"author_log": "/tmp/stock-bot-wave9.P8EKsc/f05-matching-release-final.log",
"integrated_log": "/tmp/stock-bot-wave9.P8EKsc/integration-matching-release.log",
"result": "1 passed, 0 failed;108 filtered. Author and integrated builds both passed; earlier ledger/risk manual measurements not repeated this wave.",
"artifact": "target/release/deps/engine-abe56cbc3e15ae30",
"artifact_sha256": "ed9ac89737bcd57e671c59ef818f7411c7b29c9158bc5eb9dc5e4fc2e777a2bc",
"host": "AMD Ryzen9 9950X3D; Linux6.8 x86_64; Rust/Cargo1.88.0; build jobs2. Same artifact hash in author/integrated worktrees.",
"workload": "1k/10k execution events/fills, each preceded by one Submit =2k/20k coordinator commands plus4setup. Alternate buy/sell quantity1 at10; fee0.001,capital1m. One active owner/asset or two owners/two assets;3repeats,no explicit warmup.",
"timing": "Apply includes fixture construction, helper assertions and outcome collection, unlike the earlier prebuilt F04 workload. Checkpoint copying is separate; restore includes checkpoint.clone() input copying. Serialization/build excluded.",
"equality": "Expected fills/fees/cash/flat holdings asserted. Full checkpoint and returned outcome bytes equal across repeats; restored checkpoint equal. No cross-platform determinism claim.",
"raw_integrated_rows": [
"matching multi=false events=1000 fills=1000 repeat=0 apply_us=24361 checkpoint_us=5945 restore_us=42809 checkpoint_effect_bytes=12635053",
"matching multi=false events=1000 fills=1000 repeat=1 apply_us=20561 checkpoint_us=4586 restore_us=37263 checkpoint_effect_bytes=12635053",
"matching multi=false events=1000 fills=1000 repeat=2 apply_us=19960 checkpoint_us=4580 restore_us=34314 checkpoint_effect_bytes=12635053",
"matching multi=false events=10000 fills=10000 repeat=0 apply_us=235326 checkpoint_us=77381 restore_us=437521 checkpoint_effect_bytes=128041656",
"matching multi=false events=10000 fills=10000 repeat=1 apply_us=213718 checkpoint_us=55617 restore_us=455499 checkpoint_effect_bytes=128041656",
"matching multi=false events=10000 fills=10000 repeat=2 apply_us=213643 checkpoint_us=55453 restore_us=437177 checkpoint_effect_bytes=128041656",
"matching multi=true events=1000 fills=1000 repeat=0 apply_us=22787 checkpoint_us=4393 restore_us=36271 checkpoint_effect_bytes=12635409",
"matching multi=true events=1000 fills=1000 repeat=1 apply_us=22417 checkpoint_us=3633 restore_us=36277 checkpoint_effect_bytes=12635409",
"matching multi=true events=1000 fills=1000 repeat=2 apply_us=22572 checkpoint_us=3776 restore_us=35502 checkpoint_effect_bytes=12635409",
"matching multi=true events=10000 fills=10000 repeat=0 apply_us=238015 checkpoint_us=50975 restore_us=434694 checkpoint_effect_bytes=128042015",
"matching multi=true events=10000 fills=10000 repeat=1 apply_us=234303 checkpoint_us=51207 restore_us=434431 checkpoint_effect_bytes=128042015",
"matching multi=true events=10000 fills=10000 repeat=2 apply_us=235764 checkpoint_us=51505 restore_us=437413 checkpoint_effect_bytes=128042015"
],
"limits": "Narrow component-plus-fixture workload, not end-to-end backtesting, batch scaling or speedup evidence. Serialized checkpoint plus external outcomes ~12.6MB/128MB for1k/10k; not RSS. Retained journals grow with history; no compaction."
},
"not_run": [
"Disposable PostgreSQL/Redis, browser and Python suites: persistence/API/UI/Python boundaries unchanged."
],
"limitations": [
"No active-engine cutover, F06 replay scheduler, F07 NAPI sessions or paper/SQL integration. F06 remains matcher replay release gate.",
"Checkpoint covers matching+risk+ledger, not strategy/replay/RNG state; validates retained-history consistency, not authenticity or sandbox security.",
"Coordinator admission_revision differs from nested FinancialSnapshot ledger revision. F06/F07 must expose the correct barrier token.",
"Unexpected numeric errors can follow committed subtransitions: terminal Failed outcome retains the committed prefix. Host must fail/discard the attempt; no successful performance metrics may be published.",
"MatchingOutcome.replayed gates all nested original effects. Physical execution dedup uses open instrument+opens_at or quote instrument+observed_at+source_sequence; source sequence need not be chronological.",
"Owner target-array order is explicit priority, not an atomic portfolio rebalance. Existing build warnings remain."
],
"handoff": "F04/F05 independently reviewed and validated, uncommitted. Next after owner acceptance: F06 availability-aware deterministic replay. No commit, push, PR, other task implementation or trading operation.",
"cleanup": "No services/data removed; isolated worktree and logs retained."
}
}