stock-bot/docs/development/backlog/tasks/F07.md

60 lines
4.9 KiB
Markdown

# F07 — Expose bounded Rust step sessions through existing NAPI workers
Parent epic: E2. Priority/severity: P0 / Critical. Relative complexity: M. Recommended specialization: Rust/TypeScript integration.
Scheduling source of truth: [manifest entry F07](../manifest.json). No task is authorized until the plan is approved. Dependencies and decision references below describe the design; the manifest controls current readiness.
## Evidence and root cause
- **FIN-05 — Verified:** Rust and TypeScript separately implement fill/accounting behavior; the exposed native TypeScript callback method explicitly rejects callbacks. [apps/stock/engine/src/api/backtest.rs:63](../../../../apps/stock/engine/src/api/backtest.rs#L63); [apps/stock/orchestrator/src/execution/SimulationExecution.ts:114](../../../../apps/stock/orchestrator/src/execution/SimulationExecution.ts#L114).
- **PER-01 — Verified:** EventQueue insertion scans prior events; native run is synchronous/one-shot and returns full JSON. Actual end-to-end bottleneck distribution still needs controlled measurements. [apps/stock/engine/src/backtest/mod.rs:138](../../../../apps/stock/engine/src/backtest/mod.rs#L138); [apps/stock/engine/src/api/backtest.rs:194](../../../../apps/stock/engine/src/api/backtest.rs#L194).
- **KEEP-02 — Verified:** Preserve frozen/hash-verified historical snapshots and fenced durable runner attempts; extend them instead of creating a second job engine. [apps/stock/orchestrator/src/data/historical-dataset.ts:98](../../../../apps/stock/orchestrator/src/data/historical-dataset.ts#L98); [apps/stock/orchestrator/src/backtest/BacktestRunner.ts:190](../../../../apps/stock/orchestrator/src/backtest/BacktestRunner.ts#L190).
## Intended behavior and implementation scope
Expose a small pull session (load/create/advance/submit/finalize/dispose) in the existing worker, backed by the same financial kernel. advance returns a tagged union: progress, decision batch, or complete. For a decision batch, all due strategies receive immutable views from the same ledger revision; collect all decisions before ordered admission. Use one native test strategy/intent trace for conformance, not a port of every Rust built-in. Per-session dataset load once; cross-trial residency is P03.
## Exclusions
No unrelated refactor, new service, broker enablement, or silent reinterpretation of historical results.
## Likely files and ownership
- apps/stock/engine/src/api/backtest.rs
- apps/stock/orchestrator/src/backtest/native-worker.ts
- apps/stock/orchestrator/src/backtest/native-worker-protocol.ts
- apps/stock/orchestrator/src/backtest/NativeBacktestWorker.ts
Write locks: native-public-api, worker-protocol. One agent owns this change. Lock names do not override actual overlapping files.
## Dependencies and blockers
Implementation requires [F06](F06.md), [C02](C02.md) integrated.
Architecture decisions: ADR-004. Agents may not independently amend their policies.
Parallel eligibility: only with disjoint actual files and reserved write locks; read FIRST-WAVE.md for the initial exact ownership boundaries.
## Acceptance criteria
- TS caller handles every step variant; it never assumes each advance returns a decision. Progress/cancel boundaries remain bounded.
- All due decisions reference one barrier revision. Reversing callback evaluation order cannot change output when contexts/states are unchanged; final admission uses declared owner priority.
- No partial barrier is committed if a decision throws. Rust returns per-intent accepted/rejected outcomes; business rejection is not a rollback of the observed decision event.
- Native conformance intent trace and TS fixture use identical financial transitions. Native built-in production strategy parity is not required.
- Worker crash/cancel/lease loss cannot publish successful stale results; snapshot/ID/version mismatches reject.
## Mandatory tests
- step_tagged_union, immutable_barrier_revision, reverse_callback_order, decision_exception_aborts_barrier, rejected_intent_is_committed_outcome.
- Existing native-worker/runner cancellation and lease tests; extend P01 with step and context-byte timings.
Apply relevant [quality gates](../../QUALITY-GATES.md). Report exact commands/results and unavailable checks; named new cases are requirements, not claims of existing passing tests.
## Regression risks and rollback
Financial or compatibility regression; preserve existing fixtures and version changed semantics.
Keep migrations additive and previous results readable. Do not add a switch that restores a correctness defect. Versioned cutover/migration is required only when persisted contracts or active engines change; never silently fall back to old economics.
## Handoff
Report scope changes (if any), fixture expectations, API/schema changes, measurements, remaining limitations and commit/PR references once authorized. Request independent review; only the integration/planning owner may mark the manifest done.