# P04 — Schedule deterministic batches across a bounded worker pool Parent epic: E7. Priority/severity: P2 / Medium. Relative complexity: M. Recommended specialization: Performance/infrastructure engineering. Scheduling source of truth: [manifest entry P04](../manifest.json). No task is authorized until the plan is approved. Dependencies and decision references below describe the design; the manifest controls current readiness. ## Evidence and root cause - **PER-01 — Verified:** EventQueue insertion scans prior events; native run is synchronous/one-shot and returns full JSON. Actual end-to-end bottleneck distribution still needs controlled measurements. [apps/stock/engine/src/backtest/mod.rs:138](../../../../apps/stock/engine/src/backtest/mod.rs#L138); [apps/stock/engine/src/api/backtest.rs:194](../../../../apps/stock/engine/src/api/backtest.rs#L194). - **KEEP-02 — Verified:** Preserve frozen/hash-verified historical snapshots and fenced durable runner attempts; extend them instead of creating a second job engine. [apps/stock/orchestrator/src/data/historical-dataset.ts:98](../../../../apps/stock/orchestrator/src/data/historical-dataset.ts#L98); [apps/stock/orchestrator/src/backtest/BacktestRunner.ts:190](../../../../apps/stock/orchestrator/src/backtest/BacktestRunner.ts#L190). ## Intended behavior and implementation scope Use existing isolated one-shot workers and durable claims to run independent trials concurrently at a configurable1/2/4 process limit. Resident dataset reuse is optional P03, not a dependency. Each trial gets deterministic seed/identity and separate mutable state; admission/backpressure honors cancellation/leases. No parallel event processing inside one portfolio. ## Exclusions No speculative distributed infrastructure or weakened correctness gates. ## Likely files and ownership - apps/stock/orchestrator/src/backtest/NativeBacktestWorker.ts - apps/stock/orchestrator/src/backtest/BacktestRunner.ts - apps/stock/web-api/src/services/BacktestDispatcher.ts Write locks: worker-pool, web-backtest-dispatch. One agent owns this change. Lock names do not override actual overlapping files. ## Dependencies and blockers Implementation requires [F07](F07.md), [R06](R06.md), [C06](C06.md), [P01](P01.md) integrated. Architecture decisions: ADR-008. Agents may not independently amend their policies. Parallel eligibility: only with disjoint actual files and reserved write locks; read FIRST-WAVE.md for the initial exact ownership boundaries. ## Acceptance criteria - Concurrency1/2/4 produces identical per-trial economic hashes and completion-independent ranking. - A worker crash retries only affected durable attempts; batch cancellation drains or kills workers with no stale result commit. - Pool concurrency is configurable; no assumption that all available CPUs improve throughput. ## Mandatory tests - parallel_hash_parity, worker_crash_retry, cancellation_under_load, lease_fencing_pool. - P01 trials/s, efficiency T1/(p*Tp), tail latency and RSS at1/2/4 workers. Apply the relevant [quality gates](../../QUALITY-GATES.md); submit exact commands, outcomes and skipped checks. Named cases above are required regression scenarios, not claims that those tests already exist. ## Regression risks and rollback Performance measurements can hide semantic changes; require economic hash equivalence. Keep migrations additive and previous results readable. Do not add a switch that restores a correctness defect. Versioned cutover/migration is required only when persisted contracts or active engines change; never silently fall back to old economics. ## Handoff Report scope changes (if any), fixture expectations, API/schema changes, measurements, remaining limitations and commit/PR references once authorized. Request independent review; only the integration/planning owner may mark the manifest done.