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

55 lines
3.6 KiB
Markdown

# C04 — Make partial runtime initialization recoverable
Parent epic: E1. Priority/severity: P1 / High. Relative complexity: S. Recommended specialization: Trading infrastructure implementation.
Scheduling source of truth: [manifest entry C04](../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
- **RUN-02 — Verified:** Partial initialization can allocate services before isInitialized becomes true; shutdown exits early on that failure path. [apps/stock/orchestrator/src/core/ModeManager.ts:55](../../../../apps/stock/orchestrator/src/core/ModeManager.ts#L55); [apps/stock/orchestrator/src/core/ModeManager.ts:157](../../../../apps/stock/orchestrator/src/core/ModeManager.ts#L157).
## Intended behavior and implementation scope
Keep the successful lifecycle ordering. Register cleanup before each stage that may partially acquire resources; on initialization failure stop admission/feed, attempt all applicable cleanups, preserve the original initialization error and clear failed engine/config readiness. Retrying must be safe. Do not redesign runtime composition or add an imaginary native dispose method.
## Exclusions
No unrelated refactor, new service, broker enablement, or silent reinterpretation of historical results.
## Likely files and ownership
- apps/stock/orchestrator/src/core/ModeManager.ts
- test/integration/mode-lifecycle.test.ts
Write locks: runtime-composition. 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
- Inject failure in execution initialize, storage initialize, strategy restore and market initialize. Clean up the failing stage as well as previously acquired stages; untouched stages are not invented resources.
- Cleanup failure neither masks the original error nor prevents remaining cleanup; feed stops before strategy/account teardown.
- No successful modeChanged event is published on failure; engine/config access cannot expose failed readiness; a retry succeeds.
- Existing shutdown ordering and live-disabled validation remain unchanged.
## Mandatory tests
- bun test test/integration/mode-lifecycle.test.ts with initialization_failure_each_stage, partial_acquisition_cleanup, cleanup_preserves_original_error, failed_init_retry and no_success_event.
- bun run typecheck; bun run test:contracts.
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.