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

4.1 KiB

C03 — Close the existing TypeScript fee-funding hole

Parent epic: E1. Priority/severity: P0 / Critical. Relative complexity: M. Recommended specialization: Trading infrastructure implementation.

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

Narrow interim fix: retain the current legacy gross-exposure financing model and its reducing-order exemption. Add projected unpaid commissions to the existing non-reducing capital test, using proposed remaining quantity plus pending remaining quantities and each order's frozen commission policy. At fill, exclude the current order from other reservations and use its stored policy. Paid fees already reduce current equity and must not be counted again. No new cash-reservation table, schema, long-only rule or independent financing subsystem.

Exclusions

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

Likely files and ownership

  • apps/stock/orchestrator/src/execution/model.ts
  • apps/stock/orchestrator/src/execution/SimulationExecution.ts
  • test/integration/fee-funding.test.ts (new)

Write locks: ts-execution-model. Review concrete file overlap; first-wave writable paths are further restricted by FIRST-WAVE.md.

Dependencies and blockers

No code prerequisite. Global execution approval and write-lock acquisition still required.

No additional economic-policy approval is needed for this narrowly scoped task.

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

Acceptance criteria

  • Under existing non-reducing policy:10000 cash, buy100@100,1% fee rejects without financial mutation; buy99@100 succeeds leaving1.
  • Two zero-liquidity pending buys of50@100 with1% fees cannot both pass: reserved commitments would total10100. Cancellation releases the computed commitment.
  • A99-share order filled10 then89 shares pays99 total commission and leaves1; duplicate events charge nothing twice.
  • Existing short/reversal and risk-reducing behavior is unchanged. This fix does not claim the legacy financing model is cash-only or that insolvent short covers cannot create debt.

Mandatory tests

  • Dedicated test/integration/fee-funding.test.ts: fee_budget_10000, fee_budget_99, pending_fee_commitments, partial_fee_not_double_counted, cancelled_commitment_released, frozen_order_fee_policy.
  • bun test test/integration/fee-funding.test.ts test/integration/order-execution.test.ts test/integration/strategy-recovery.test.ts; bun run typecheck; bun run test:contracts.
  • Integration owner runs the existing disposable PostgreSQL order-execution suite before merge; no development trading DB.

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.