continuation

This commit is contained in:
Boki 2026-10-20 16:57:59 -04:00
parent 32e43c9243
commit 879e9b4228
42 changed files with 2607 additions and 1306 deletions

View file

@ -80,3 +80,59 @@ is contacted. Native SMA and MACD regressions are covered by the repository chec
Coverage reports contain measured LCOV data only. Missing coverage or an empty package
selection fails the coverage command instead of producing an estimated score.
## Backtest execution workflow
The orchestrator's `BacktestRunner` is the application boundary for both HTTP and decorated
jobs. It validates the request, claims a run, executes the configured native or TypeScript
backend, normalizes the result, and commits the result and terminal status in one transaction.
Completion events are published only after that commit. Progress cannot revive a terminal
run, duplicate claims are rejected, and unsupported controls do not change persisted state.
`@stock-bot/types` exports the shared `BacktestRunResult` used by the runner, API, and UI.
Return, win-rate, drawdown, and chart-analytics percentages use fractions (`0.10` means 10%).
Trade records identify their granularity with `kind: 'fill' | 'closed'`; `totalTrades` counts
completed round trips. Undefined ratios remain `null`. Engine-specific supplemental metrics
retain their documented backend meaning.
The web API owns backtest definitions and pending-run creation; the orchestrator owns
execution state and result writes. V1 is a compatibility facade over V2, not a second SQL
schema or execution path. Strategy parameters and exact run configuration are preserved.
All historical readers delegate to the same storage policy; simulated fills do not enter
the trading ledger. Shared adapters are closed by DI, not by a backtest mode transition.
Deployment requires the additive migration
`database/postgres/init/09-backtest-run-contract.sql`, after the existing runs migration.
The web API's migration allowlist includes it. It adds configuration/provenance columns and
a unique result-per-run index without deleting history. Existing duplicate result rows must
be reconciled before applying that index. Historical rows without snapshots remain readable
and are explicitly marked `legacy`; their configuration is reconstructed from the definition.
New executions require PostgreSQL; there is no implicit in-memory production repository.
Native execution uses a disposable **worker process**, not the HTTP thread. A small
`BacktestExecutionEngine` port separates the runner from both implementations. The parent
owns storage reads and persistence; only serializable configuration, bars, progress, and
results cross IPC. One native worker runs at a time per orchestrator instance; additional
requests receive a conflict rather than spawning unbounded workers. The existing
`maxConcurrent` setting does not enable parallel runs through this serialized runner.
`services.orchestrator.backtesting.executionTimeoutMs` defaults to 300000 (five minutes)
and covers historical reads, worker startup, and native execution, not result persistence.
Cancellation and deadlines kill and reap the worker even during a synchronous Rust call.
A forced cancellation commits `cancelled` without a result row or fabricated partial metrics;
the original execution request returns HTTP 409 with `status: 'cancelled'`, and `/stop`
acknowledges only after that status is persisted. SSE publishes a terminal `cancelled` event.
Native pause/resume and speed controls remain explicitly unsupported. Native progress is
stage-level, not an invented percentage inside Rust's synchronous loop. Shutdown handles
SIGINT and SIGTERM, stops the worker, and settles its run before service cleanup.
The orchestrator build emits **both** `dist/index.js` and `dist/native-worker.js`; deploy
them together with the external `@stock-bot/engine` package/native binary. The worker does
not require source TypeScript in production. Process isolation is intentional: terminating
a thread cannot guarantee interruption or contain a native process crash.
The integration suite covers both real engines, REST/job parity, V1/V2 dispatch and result
reads, missing data, duplicate/concurrent requests, failed persistence/rollback, controls,
responsive requests/streams, worker crashes, deadlines, cancellation, and cleanup. SQL tests
use an in-process PostgreSQL emulator and the real transaction manager, not a live database.
Durable queued dispatch and recovery after an orchestrator crash remain follow-up work.