more stuff

This commit is contained in:
Boki 2026-10-20 20:11:40 -04:00
parent c3cb36c8d6
commit 7dec0be3b4
26 changed files with 1394 additions and 275 deletions

View file

@ -101,9 +101,10 @@ schema or execution path. Strategy parameters and exact run configuration are pr
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
Deployment requires the additive migrations
`database/postgres/init/09-backtest-run-contract.sql` and `10-durable-backtest-dispatch.sql`,
after the existing runs migration. The web API's migration allowlist includes both.
The first 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.
@ -135,7 +136,47 @@ The integration suite covers both real engines, REST/job parity, V1/V2 dispatch
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.
### Durable dispatch and recovery
`POST /api/v2/backtests/:id/runs` returns **202** with a pending run ID and a `Location`
header. In one PostgreSQL transaction it stores the run, its immutable execution request,
and a `backtest_dispatch` intent. No Redis/HTTP call is needed to accept the run. Editing
a definition later does not change an accepted run. The V1 facade uses the same workflow.
The orchestrator's direct synchronous REST execution routes remain compatibility endpoints.
The web API runs a producer-only dispatcher (zero workers) every five seconds. It reserves
due intents with `FOR UPDATE SKIP LOCKED` and publishes identity-only notifications to the
existing `{orchestrator_orchestrator}` queue. Stable job IDs deduplicate outstanding jobs;
pending intents are republished every 30 seconds, including after a lost Redis job or a
publisher restart. Redis outages leave accepted runs pending; they do not falsely fail them.
The queue clients honor explicit Redis endpoints and reconnect after transient outages.
Each orchestrator consumes one job at a time. It atomically claims a pending run with a new
attempt UUID and a 30-second PostgreSQL-clock lease, renewed every five seconds. Progress,
controls, failure, cancellation, and terminal result writes are fenced by that attempt and
an unexpired lease. A lost heartbeat stops the local execution and leaves recovery to the
dispatcher. Expired owners are retried from their saved request, up to three execution
attempts; exhaustion becomes `failed`. Terminal redelivery is acknowledged without rerunning.
Computation is at-least-once, not exactly-once; only an owned result can commit, atomically
with terminal status, and the unique result-per-run constraint remains the final guard.
Recovery also depends on BullMQ detecting stalled jobs and the API dispatcher being available.
Cancellation is durable: queued runs become `cancelled` before a claim; active runs record
`cancel_requested` for their owner to observe, stop, and acknowledge. The V2 cancel endpoint
returns **202**, not a claim that execution already stopped. A cancelled abandoned lease
becomes `cancelled`, without a result. The dashboard keeps polling until persisted terminal
state; WebSocket updates are read from committed SQL state, not an in-memory dispatch request.
Native pause/resume/speed remain unsupported; those compatibility controls still require
the HTTP request to reach the owning instance.
Before applying migration 10, **stop all older orchestrators**, start the migrated web API,
then start lease-aware orchestrators. Legacy pending runs receive a one-time snapshot of
their current definition; legacy active runs with no owner token are marked failed rather
than pretending they are recoverable. Existing results are preserved. Infrastructure should
supervise the orchestrator process tree: SIGKILL of a parent cannot guarantee reaping a child
blocked in native code, though a stale child cannot persist a result through the fenced parent.
No migration or restart is performed against your existing local databases by the tests.
## HTTP access and secrets
@ -200,5 +241,9 @@ incremental/batch parity; no synthetic D values are produced before warm-up comp
`bun run test:postgres` additionally requires `PIPELINE_TEST_DATABASE_URL` pointing to a
**disposable database whose name starts with `stock_bot_safety`**. It creates/drops isolated
test schemas and verifies real rollback, foreign-key constraints, idempotency, and competing
transaction locking. CI provisions that database and runs this additional gate. It is
separate from the infrastructure-free `check`; never point tests at production.
transaction locking. Set `BACKTEST_TEST_REDIS_URL` to a dedicated Redis fixture using database
15 as well; queue names are unique per test. The durability tests cover HTTP acceptance,
outbox rollback, immutable requests, migration replay, hard-killed owners, stale-write fencing,
cancellation races, retry exhaustion, disconnected publishing, and lost-job redelivery with
the real native engine. CI provisions PostgreSQL and Redis and runs this additional gate.
It is separate from the infrastructure-free `check`; never point tests at production.