38 lines
6.2 KiB
Markdown
38 lines
6.2 KiB
Markdown
# P08 — Reduce measured TypeScript strategy decision overhead
|
|
|
|
Parent epic: E7. Priority/severity: P1 / High. Complexity: M. Specialist: TypeScript performance and deterministic runtime engineering.
|
|
|
|
Story: As a researcher, I can run realistic window-based TypeScript strategies without repeatedly paying avoidable state-validation or copying costs.
|
|
|
|
Scheduling source of truth: [manifest P08](../manifest.json). **Deferred at the owner's request, 2026-10-23:** stop performance work for now and continue the remaining backlog. Initial inspection produced no application changes, profiling or benchmark runs. Resume only when requested.
|
|
|
|
## Evidence and root cause
|
|
|
|
**PER-02 — Verified aggregate cost; individual causes need profiling.** S02's release comparison replays exactly the same financial decisions and IDs. For 10,000 total bars, recorded decisions versus SMA callbacks took 1,091→1,467 ms (5/15, one owner) and 1,018→2,351 ms (15/252, one owner). With ten assets/ten owners, 15/252 took 594→1,681 ms. All 48 timed sessions preserve complete financial hashes; repeat/reversed-owner runs preserve diagnostics. These are warmed synthetic workloads, not durable-job throughput or pure callback/NAPI measurements.
|
|
|
|
Potential contributors are the [host staging boundary](../../../../apps/stock/orchestrator/src/backtest/strategy-runtime.ts#L205), strict JSON copying/freezing, [per-callback SMA state parsing](../../../../apps/stock/orchestrator/src/strategies/canonical-sma.ts#L128), bounded history and [reviewed trailing calculations](../../../../libs/calculations/src/indicators/window-statistics.ts#L50). The aggregate difference does not attribute cost among them. Larger windows also change decisions and fills; compare each strategy with its own exact recorded control, not the two periods as identical economics.
|
|
|
|
Raw evidence/harness/source identity: `manifest.json` → `fifteenth_wave_validation.performance`; [roadmap](../../ROADMAP.md#fifteenth-wave-execution--2026-10-23). Retained local harness: `/tmp/stock-bot-wave15.zi3GmP/sma-benchmark.ts`.
|
|
|
|
## Scope and exclusions
|
|
|
|
1. Extend the existing research benchmark entrypoint with the S02 paired strategy/recorded workload; incorporate the useful temporary harness logic into existing tooling, not another benchmark framework. Preserve cold import/preparation versus warmed-session boundaries, source/addon hashes, raw samples, counts and exact comparisons.
|
|
2. Profile actual calculation, schema parsing, state copy/freezing, host admission feedback and allocations. Measure instrumentation separately; a residual is not a named subsystem cost.
|
|
3. Implement the smallest evidenced local optimization in `backtest/strategy-runtime.ts`, `strategies/canonical-sma.ts` or their private helpers. Avoid changing public contracts. Publish a proposed exact file scope before editing shared runtime/strategy files.
|
|
4. Preserve strict JSON state validation, detached immutable contexts, per-owner isolation, staged commit-after-native-ack, failure diagnostics, scoped RNG, whole-attempt retry, reviewed crossover arithmetic, financial decisions and result provenance. Do not special-case away validation for trusted built-ins or change strategy semantics to make the benchmark faster.
|
|
|
|
No Rust financial changes, DSL, worker pool, resident dataset/feature framework, shared mutable checkpoints, public/default cutover, weakened validation, arbitrary input limits or general cleanup. Incremental indicator arithmetic or a new state contract needs a separately reviewed proposal; neither is assumed necessary.
|
|
|
|
## Dependencies, ownership and parallelism
|
|
|
|
Code prerequisites: S02 accepted/integrated, P07 complete. No additional policy approval for profiling/local semantics-preserving optimization. One owner holds strategy-contract, strategy-builtins and benchmark-harness locks for the minimal patch; disjoint review may run in parallel. Do not overlap S03/S05 or another worker using those actual files. Root alone updates task state. No native rebuild during measurements or artifact review.
|
|
|
|
## Acceptance and mandatory tests
|
|
|
|
- `sma_benchmark_modes` validates selection/period/topology arguments, labels ten-assets/two-owners as eight untraded assets, and reports actual decisions/fills rather than inferring equal work from bar count.
|
|
- `sma_recorded_decision_parity` compares identical captured decision IDs and complete financial outputs for 5/15 and 15/252, with 1/1, 10/2 and 10/10 asset/owner shapes. Fresh retries and reversed program order preserve diagnostic/RNG/state hashes. Retain raw repeated measurements and the exact baseline/candidate artifacts.
|
|
- Profile identifies the removed cost. Counterbalance baseline/candidate runs on the same host/release addon and configurations; show repeatable end-to-end improvement larger than observed run-to-run variation on both 15/252 owner-dense workloads, with the smaller workloads checked for regression. Do not claim an optimization from changed economics, instrumentation overhead or host drift. If no safe local improvement is demonstrated, hand off the blocker/proposal rather than mark implementation complete.
|
|
- Preserve all `canonical-sma`, `strategy-runtime`, `native-replay`, worker and runtime tooling regression tests. Add a focused regression for each changed state/validation path, including retained mutable references, malformed JSON, callback failure after prior owners staged, native rejection and malformed/ambiguous admission feedback as relevant. Accessors must not execute; prototype/cycle/symbol/sparse/non-finite/negative-zero rejection and future-information isolation remain intact. No unstable wall-time assertions in unit tests.
|
|
- Strict TypeScript for new test/tooling sources, scoped lint/format, full `bun run check`, final release verification and G2 recovery if worker/durable boundaries change. Independent reviewer signs off both invariants and measurement methodology.
|
|
|
|
Risks: accidental mutable aliasing, skipped validation, changed floating-point crossover arithmetic, committing partial owner state, misleading benchmark attribution. Revert a failed optimization without weakening economic policy or deleting reviewed helpers. Definition of done: focused reviewed patch, passing gates and repeatable exact-economics measurements; root acceptance and explicit commit authorization remain separate.
|