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

76 lines
4.6 KiB
Markdown

# CL08 — Split backtest template, run and reproduction responsibilities behind the existing service
Parent epic: E10. Priority/severity: P2 / Medium. Complexity: M. Specialization: TypeScript application services and PostgreSQL transactions.
Scheduling metadata: [manifest](../manifest.json). Read the [cleanup execution contract](../../CLEANUP.md#execution-contract). This story defines planned work; implementation has not been requested.
## User story and outcome
As a backend developer, I want coherent backtest application modules, so changing reproduction or run admission does not require navigating a thousand-line service.
Expected benefit: Reduces the number of unrelated behaviors sharing one class while preserving existing API and transaction contracts.
## Verified evidence
- **Verified — CL-F07:** BacktestServiceV2 combines template CRUD, run admission, controls, results, reproduction and row mapping in 1,071 lines at the audit baseline. [apps/stock/web-api/src/services/backtest-v2.service.ts:167](../../../../apps/stock/web-api/src/services/backtest-v2.service.ts#L167).
## Implementation scope
- Keep BacktestServiceV2 as a compatible facade while extracting focused template operations, run submission/control operations and reproduction/result readers within the same application.
- Move logic with its existing transaction boundaries, identity checks and error types. Pass the existing query/transaction client through helpers; do not open nested pool transactions.
- Use the shared CL05 report/domain helpers. Keep schema mappings explicit and avoid a new generic repository/service layer for each table.
- Preserve idempotent receipt lookup before current eligibility checks and keep run/outbox insertion atomic.
Likely existing files/modules:
- `apps/stock/web-api/src/services/backtest-v2.service.ts`
- `apps/stock/web-api/src/services/run-reproduction.ts`
- `apps/stock/web-api/src/services/reproduction-comparison.ts`
- `apps/stock/web-api/src/routes/backtests-v2.routes.ts`
Proposed new files/modules (names may be adjusted within this scope):
- `apps/stock/web-api/src/services/backtests/templates.ts`
- `apps/stock/web-api/src/services/backtests/runs.ts`
- `apps/stock/web-api/src/services/backtests/reproduction.ts`
- `apps/stock/web-api/src/services/backtests/results.ts`
## Exclusions
- No schema migration, endpoint redesign, new simulator, financial calculations or receipt-key changes.
- No cleanup of legacy execution branches in this extraction; CL12 follows it.
## Dependencies and ownership
Implementation prerequisites: [CL05](CL05.md).
Write locks: `web-backtest-service`, `web-run-reproduction`.
Parallel execution: After CL05. Must not overlap CL12 or another backtest service owner. Can run with disjoint frontend work.
## Acceptance criteria
1. Existing service callers and routes retain method signatures, HTTP behavior and persisted result shapes.
2. Templates, admission/controls and reproduction/results have focused implementation modules; the facade does not duplicate extracted logic.
3. Same-key retries after edits/archive or eligibility changes recover the original admitted request; conflicting key reuse still fails.
4. Run and dispatch writes remain atomic; single-connection transactions do not acquire another pool client.
5. Reproduction pins retained original inputs/builds, and result reading still verifies provenance and reports missing canonical data explicitly.
## Mandatory verification
PostgreSQL tests require disposable stock_bot_safety* and Redis DB15 fixtures under the existing harness; never run them on development research or paper state. Extend existing admission/reproduction transaction tests.
```sh
bun test test/integration/run-reproduction-api.test.ts test/integration/reproduction-comparison.test.ts test/integration/canonical-durable-runner.test.ts
bun test test/integration/canonical-workbench.test.tsx test/integration/backtest-run-controls.test.tsx
bun run test:postgres
bun run typecheck
```
Required regression cases: receipt replay/conflicts, concurrent submission, archive races, cancellation, one-connection transaction behavior, result corruption and unavailable original builds.
## Risks and definition of done
Splitting code can accidentally move a check outside a lock or create nested transactions. Review operation order and test actual concurrent SQL behavior, not just mocked method calls.
Done means the acceptance criteria and relevant checks pass, an independent review accepts the focused diff, and the integrator records exact test results and a commit/reference in the manifest. Preserve the compatibility and financial invariants in the cleanup execution contract.