# CL11 — Separate backtest template loading, run monitoring and submission state Parent epic: E10. Priority/severity: P2 / Medium. Complexity: M. Specialization: React asynchronous state and durable-run UX. Scheduling metadata: [manifest](../manifest.json). Read the [cleanup execution contract](../../CLEANUP.md#execution-contract). Implementation and validated commits were authorized after planning; scheduling and completion evidence live in the manifest. ## User story and outcome As a researcher, I want saved-template editing, run selection and job submission to remain independent, so navigation and retries behave predictably. Expected benefit: Reduces the responsibilities of the 571-line backtest hook without discarding its existing correctness protections. ## Verified evidence - **Verified — CL-F07:** The hook owns template CRUD, eligibility, selected-run request chains, polling, submission receipts and cancellation. [apps/stock/web-app/src/features/backtest/hooks/useBacktestV2.ts:34](../../../../apps/stock/web-app/src/features/backtest/hooks/useBacktestV2.ts#L34). - **Verified — CL-F07:** A 645-line page composes template configuration, controls, run history and result presentation. [apps/stock/web-app/src/features/backtest/BacktestDetailPageV2.tsx:1](../../../../apps/stock/web-app/src/features/backtest/BacktestDetailPageV2.tsx#L1). ## Implementation scope - Extract focused template state, selected-run monitoring and submission/cancellation helpers behind the current useBacktestV2 facade. - Keep one owner for the selected status-then-results request chain. Generation checks and AbortControllers must still reject stale responses. - Keep template archival, browser monitoring stop and server job cancellation distinct. Preserve exact in-memory receipt custody through ambiguous submissions. - Reduce page wiring only where this extraction makes it redundant; reuse current result/configuration components. Likely existing files/modules: - `apps/stock/web-app/src/features/backtest/hooks/useBacktestV2.ts` - `apps/stock/web-app/src/features/backtest/BacktestDetailPageV2.tsx` - `test/integration/backtest-run-controls.test.tsx` - `test/integration/canonical-workbench.test.tsx` Proposed new files/modules (names may be adjusted within this scope): - `apps/stock/web-app/src/features/backtest/hooks/useRunMonitoring.ts` - `apps/stock/web-app/src/features/backtest/hooks/useRunSubmission.ts` ## Exclusions - No browser-persistent receipt journal, new backend controls, pause/resume support or financial result reconstruction. - No dataset/HTTP parser edits owned by CL06 and no general hook framework. ## Dependencies and ownership Implementation prerequisites: None. Write locks: `web-backtest-monitor`. Parallel execution: Ready independently. Can run with CL08 because UI/client contracts remain stable. Serialize with other backtest page/hook owners; keep CL06 API-client edits separate. ## Acceptance criteria 1. useBacktestV2 retains its public return contract while delegating distinct lifetimes to focused helpers. 2. Only the selected run/backtest identity can update status, errors or results; changing the parent route fences earlier requests. 3. Polling, WebSocket refresh and cancellation acknowledgement share one serialized refresh chain without overlapping result requests. 4. Uncertain submissions reuse the original key/body even after template edits or archival; creating a new logical request remains explicit. 5. Leaving a page stops browser observation without cancelling server work. Completed/cancelled/failed/missing results stay distinct and reopen correctly. ## Mandatory verification Use existing workspace builds. Tests requiring PostgreSQL/Redis must use explicitly disposable safety fixtures. ```sh bun test test/integration/backtest-run-controls.test.tsx test/integration/canonical-workbench.test.tsx bun test test/integration/run-reproduction-ui.test.tsx bun run typecheck ``` Required regression cases: selected-run and parent navigation races, reload, late status/results, WebSocket invalidation during a poll, cancellation failure, archive while pending and exact receipt retry. ## Risks and definition of done Moving refs between hooks can change their lifetime or create stale closures. Preserve identity ownership and existing interaction tests; do not replace this with several independent polling effects. 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.