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

4.4 KiB

CL03 — Roll back failed shared-service startup and clear initialization timers

Parent epic: E10. Priority/severity: P1 / High. Complexity: M. Specialization: TypeScript resource lifecycle and concurrency.

Scheduling metadata: manifest. Read the cleanup execution contract. This story defines planned work; implementation has not been requested.

User story and outcome

As a developer restarting services, I want a failed or timed-out initialization to release resources it acquired, so a retry starts from a clean state.

Expected benefit: Fixes a reproduced resource-ownership gap in shared DI startup without changing trading recovery.

Verified evidence

Implementation scope

  • Track the resources and initialization work owned by a build attempt. Clean up acquired resources when initialization or service resolution fails.
  • Clear initialization timeout handles when work settles. Keep startup failure bounded and retain cleanup ownership of any late-settling initializer.
  • Reuse the existing lifecycle manager and client cleanup methods. Ensure cleanup errors do not hide the original startup failure and repeated cleanup is safe.
  • Add failure-oriented tests with controlled deferred promises and fake/controlled timers; do not depend on live stores.

Likely existing files/modules:

  • libs/core/di/src/utils/lifecycle.ts
  • libs/core/di/src/container/builder.ts
  • libs/core/di/test/lifecycle.test.ts

Proposed new files/modules (names may be adjusted within this scope):

  • test/integration/service-startup-rollback.test.ts

Exclusions

  • No changes to ModeManager, strategy checkpoints, paper recovery, research leases or financial execution.
  • No new lifecycle framework or blanket migration of all application shutdown handlers.

Dependencies and ownership

Implementation prerequisites: None.

Write locks: di-container, di-service-lifecycle.

Parallel execution: Ready independently and disjoint from CL01/CL04. Serialize with CL02 on di-container; use separate test files from CL01.

Acceptance criteria

  1. When one service connects and another initialization fails, the connected service is cleaned up and build rejects with the original failure.
  2. Resolution failures and timeouts also release resources acquired by that attempt; late successful initialization cannot leave an orphaned connection after failure.
  3. Successful initialization leaves no pending startup timeout handles; failure paths clear settled-work timers.
  4. Cleanup failure does not prevent remaining cleanup or conceal the initiating failure. Repeated shutdown is safe.
  5. Existing successful startup and shutdown contracts remain covered; no tests require waiting for the production 30-second timeout.

Mandatory verification

service-startup-rollback.test.ts is a proposed new test file. Use simulated services and controlled clocks; no databases or native builds are needed.

bun test libs/core/di/test/lifecycle.test.ts
bun test test/integration/service-startup-rollback.test.ts
bun run typecheck

Required regression cases: partial success followed by failure, dependency-resolution exception, timeout, late completion, cleanup exception, repeated cleanup and successful timer disposal.

Risks and definition of done

A timeout does not cancel an arbitrary promise. The implementation must retain responsibility for pending work and clean up late resources, rather than assuming Promise.race cancelled initialization.

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.