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

4.7 KiB

CL07 — Share JSON transport handling across experiment and validation clients

Parent epic: E10. Priority/severity: P2 / Medium. Complexity: M. Specialization: Frontend HTTP contracts and retry semantics.

Scheduling metadata: manifest. Read the cleanup execution contract. Implementation and validated commits were authorized after planning; scheduling and completion evidence live in the manifest.

User story and outcome

As a frontend maintainer, I want one small transport helper for repeated experiment request handling, so error and cancellation behavior stays consistent.

Expected benefit: Removes duplicated request/JSON/status handling without combining independent domain workflows.

Verified evidence

Implementation scope

  • Extract a small browser JSON request helper and apply it first to ordinary/cost experiment, durable chronological-validation and walk-forward clients.
  • Keep endpoint schemas, request identity comparisons and operation-specific error messages beside each domain client. Preserve cancellation as cancellation and existing ambiguous-acceptance classifications.
  • Keep browser cookie transport separate from ResearchClient bearer credentials; reuse shared response parsers already present.
  • Avoid hidden retries, generalized request managers, new state libraries or a policy-heavy client framework.

Likely existing files/modules:

  • apps/stock/web-app/src/lib/api.ts
  • apps/stock/web-app/src/features/experiments/services/experimentApi.ts
  • apps/stock/web-app/src/features/experiments/services/chronologicalValidationDurableApi.ts
  • apps/stock/web-app/src/features/experiments/services/walkForwardApi.ts

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

  • apps/stock/web-app/src/lib/requestJson.ts
  • test/integration/research-http-transport.test.ts

Exclusions

  • No backtestApiV2.ts migration in this story; CL06 owns its dataset parser cleanup.
  • No auth weakening, new automatic retries, endpoint/receipt changes, or mandatory conversion of every HTTP client.

Dependencies and ownership

Implementation prerequisites: None.

Write locks: web-experiment-transport.

Parallel execution: Ready independently. It can run with CL05/CL06 and frontend component stories only when actual client/test files are disjoint.

Acceptance criteria

  1. The selected clients share fetch/JSON/status plumbing while retaining their public method and error contracts.
  2. Aborted reads remain distinguishable from failed writes; no retry occurs unless explicitly requested by the caller.
  3. Lost responses, malformed accepted responses and server failures preserve the current uncertain-submission behavior and receipt inputs.
  4. Unexpected success statuses, invalid envelopes and wrong response identities still reject.
  5. Browser requests include cookies; research-agent tokens are neither introduced into the browser nor routed through this helper.

Mandatory verification

research-http-transport.test.ts is a proposed new test file. Substitute only HTTP responses; exercise the production helper and existing domain clients.

bun test test/integration/research-http-transport.test.ts
bun test test/integration/experiments-ui.test.tsx test/integration/chronological-validation-durable-ui.test.tsx test/integration/walk-forward-ui.test.tsx
bun test test/integration/experiment-retries-ui.test.tsx test/integration/cost-sensitivity-ui.test.tsx
bun run typecheck

Required regression cases: abort, network loss, invalid JSON, wrong 2xx status, 4xx/5xx domain errors, identity mismatch, cookie forwarding and absence of implicit POST retries.

Risks and definition of done

A generic helper can erase distinctions between uncertain acceptance and definitive rejection. Keep those policies explicit in the domain client and preserve existing race/retry tests.

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.