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

59 lines
4.4 KiB
Markdown

# S03 — Adapt durable paper transactions to Rust financial reducers
Parent epic: E4. Priority/severity: P0 / Critical. Relative complexity: L. Recommended specialization: Durable execution/Rust integration.
Scheduling source of truth: [manifest entry S03](../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
- **KEEP-01 — Verified:** Preserve transaction-scoped account locking, idempotent fills/event fingerprints, durable decisions and recovery checkpoints; these are real behavior, not interfaces alone. [apps/stock/orchestrator/src/execution/PostgresExecutionRepository.ts:46](../../../../apps/stock/orchestrator/src/execution/PostgresExecutionRepository.ts#L46); [apps/stock/orchestrator/src/execution/StrategyRecovery.ts:151](../../../../apps/stock/orchestrator/src/execution/StrategyRecovery.ts#L151).
- **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).
## Intended behavior and implementation scope
Preserve the existing PostgreSQL row lock, checkpoint+intent commit and per-effect journal. Rehydrate a private Rust staged state, call the same in-place financial kernel used by research, persist returned snapshot/effects atomically, publish after commit. Do not serialize/rebuild resident research state per event merely to satisfy paper staging. Metrics/UI are not transaction prerequisites. Before paper cutover, retain supported current order/TIF and recovery semantics or explicitly refuse incompatible outstanding state.
## Exclusions
No unrelated refactor, new service or broker activation.
## Likely files and ownership
- apps/stock/orchestrator/src/execution/PostgresExecutionRepository.ts
- apps/stock/orchestrator/src/execution/SimulationExecution.ts
- apps/stock/orchestrator/src/execution/StrategyRecovery.ts
- apps/stock/orchestrator/src/services/ExecutionService.ts
- apps/stock/engine/src/api/
Write locks: ts-execution-model, paper-transaction, financial-contract. One agent owns this change. Lock names do not override actual overlapping files.
## Dependencies and blockers
Implementation requires [S01](S01.md), [C06](C06.md) integrated.
Architecture decisions: ADR-005. 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
- Rollback discards staged native changes; crash before/after commit/publish has no duplicate fee/order or lost checkpoint.
- Restore includes owner cost residuals, open-order policies/reservations, event cursor, strategy/RNG state and IDs.
- Existing PostgreSQL failure cases and legacy supported DAY/GTC/IOC/FOK behavior pass the canonical path; incompatible old snapshot versions fail with a migration instruction.
- Research and paper reducers match economic postings for the same event/intent trace; different live data/timing is not claimed to produce identical trades.
## Mandatory tests
- Port all existing PostgreSQL order-execution and strategy-recovery failure cases.
- fault_before_commit, fault_after_commit_before_publish, multi_owner_lock, same_id_conflict, snapshot_restore_parity.
Apply the relevant [quality gates](../../QUALITY-GATES.md); submit exact commands, outcomes and skipped checks. Named cases above are required regression scenarios, not claims that those tests already exist.
## Regression risks and rollback
Compatibility or lifecycle regression; preserve frozen inputs, old results and existing failure tests.
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.