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

5.2 KiB

F05 — Unify matching and order lifecycle on the ledger

Parent epic: E2. Priority/severity: P0 / Critical. Relative complexity: L. Recommended specialization: Rust execution semantics.

Scheduling source of truth: manifest entry F05. 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

Intended behavior and implementation scope

Implement pure matcher/order-transition functions over explicit execution events; replay scheduling belongs to F06. Market/limit orders use declared spread/slippage and available liquidity, with no future-bar fields. Retain DAY/GTC/IOC/FOK semantics needed by the existing paper path; unsupported stop/OCO types reject. Target mode and explicit-order mode are mutually exclusive for one strategy instance in v1. Repeated absolute targets account for owned holdings and outstanding commitments; replacements cancel only owned orders.

Exclusions

No unrelated refactor, new service, broker enablement, or silent reinterpretation of historical results.

Likely files and ownership

  • apps/stock/engine/src/financial/matching.rs (new)
  • apps/stock/engine/src/backtest/engine.rs

Write locks: rust-matching, financial-contract. One agent owns this change. Lock names do not override actual overlapping files.

Dependencies and blockers

Implementation requires F04 integrated.

Architecture decisions: ADR-003. Agents may not independently amend their policies.

Feature activation/release gates: F06. These do not block implementing or integrating this component against its own tests.

Parallel eligibility: only with disjoint actual files and reserved write locks; read FIRST-WAVE.md for the initial exact ownership boundaries.

Acceptance criteria

  • Given an already-eligible execution event, market/limit price protection, volume sharing, partial fill, rejection/cancellation and expiry are deterministic.
  • IOC cancels remainder; FOK either fills fully or does not fill/charge; DAY accepts only in-session; target-generated GTC survives a session boundary.
  • Same owner/instrument repeated target after partial fill does not duplicate quantity; mixed target/explicit mode rejects; another owner's order cannot be cancelled.
  • Pure eligibility tests reject prior/same observed event keys. Full next-open ordering is an F06 release gate, not a hidden F05 implementation requirement.

Mandatory tests

  • matcher_gap_limit, shared_volume_capacity, partial_cancel, IOC_remainder, FOK_atomicity, DAY_expiry, target_GTC_replace.
  • unchanged_target_no_duplicate, mixed_intent_mode_rejected, wrong_owner_cancel, ineligible_event_key_rejected.

Apply relevant quality gates. 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.