Pull request #75, open against develop

Backtests run as jobs, computed on a run thread

Where the boundaries moved, what was decided and what it costs, what is proven and what is not, and where to look by hand. github.com/povio/lrbo3/pull/75

108 files
changed, in 2 commits
2,635
lines added, 453 removed
930
of those are tests (409) and generated files (521, mostly the OpenAPI snapshot)
1,705
lines of code and docs to read

Before, a backtest was computed inside the request or the chat turn that asked for it. A long one held the process that also answers the health check, the sockets and the job queue: the client saw a 502 and the run left nothing behind. After, a run is on the record the moment it is requested, a job computes it, and on the self-hosted server that computation, from reading the data to the result, happens on a thread of its own.

Not in it: batch runs (a base strategy plus N changes), which are designed only, and making runs faster, which is the next pull request.

What changed, by layer

LayerWhat is newFilesAdded
packages/engineBoth engines tell an optional listener each trading day they finish. Results are unchanged.861
packages/recordThe run lifecycle: queued, running, succeeded or failed; attempts counted on the run; one compute for futures and options that returns the completed run or the refusal; replays as runs of their own.12632
packages/agentrun_backtest takes a launcher that can wait for a queued run; a run_progress signal to the browser, outside the audit chain; the export leaves the status out.11109
apps/robodev-beThe runs/execute job, the one computeRun function, the two ends of a run thread, the wait-by-polling for the assistant, run status in the API. 518 of these lines are the OpenAPI snapshot.20995
apps/serverThe module a run thread loads, which builds that thread's own Postgres pool and storage client; two settings; a test against a real Postgres.9254
apps/feQueued, running and failed runs in Results; live progress in the Run bar and the chat step; polling while a run is pending. 230 of these lines are API tests.36498
Docs and rulesADR-0014, the layout and assistant docs, the vocabulary, the server README, the Robodev API rule.977

How it fits, now and later

new in this PR changed in this PR untouched not built
Web app
Run bar and chatshow progress by trading day
Run status in Resultsqueued, running, failed
Batch of runsN changes under one batch
↓ request answers at once; the browser polls the run, the chat hears progress
API process
POST /api/runs, replaystore the run as queued, enqueue
Chat turnqueues the run, reads its row until it settles
Audit chainsame entries as before
↓ Postgres job queue
Job thread
runs/executemarks running, saves progress, enforces 10 minutes, writes the outcome
Job heartbeatretry an orphaned run in seconds
↓ starts a thread on the self-hosted server; calls the function directly under robodev dev
computeRun(db, storage, onProgress): read, verify, decode, engine, result
Run thread
Own Postgres pool and storagebuilt by the server for this thread
Futures and options enginesreport each trading day
Faster data readersthe next pull request

One function computes every run. The runtime decides only which thread it runs on, never which code runs.

Decisions made in this PR

DecisionWhyWhat it costs
The run row is written at the request, and its status lives inside the stored recordA run can no longer vanish, and rows already stored need no migration: one without a status reads as succeededRuns cannot be queried by status in SQL without reading the JSON
One function computes a run of either kind, and the runtime only decides which thread it runs onNo second mechanism for futures, and the code tested under robodev dev is the code production runsUnder robodev dev and hosted Robodev a run still holds the job's thread while it computes
The run thread's entry lives in apps/server, which builds the thread's clientsThe SDK gives the backend no storage constructor; only the server owns that configurationbun dev, the API tests' backend and Playwright never exercise the thread; one server test and a boot do
A thread per run, not a poolA run past its limit is stopped outright and its memory goes with the threadEach run pays the thread's start: loading the runtime and the backend build
The assistant waits by reading the run row every 500 msWorks whichever container claimed the job, holds no thread, and leaves the tool's contract and the audit chain as they wereUp to half a second of added latency per run, and one small query per poll
A refusal fails the run at once; any other error is retried, and the third execution fails itAn orphaned or flaky run recovers by itself, and no run is left pending by a dead jobA thread that crashes deterministically is started three times before the run reads as failed
No job heartbeatThat is a change to the job runner, outside this ticketA run whose container went away is executed again only 14 minutes after it started

ADR-0014 in the pull request holds these with the alternatives that were turned down.

The evidence

ClaimChecked againstResult
A run computed on the thread is the run computed in the caller's thread, byte for byte, for futures and for optionsapps/server/tests/runThread.test.ts on a real Postgres: both as returned and as read back from the run rowPass, both kinds
A refusal, a crash and the time limit each end with a failed run and its reasonThe same test: dates outside the dataset, a thread whose configuration is broken, a 300 ms limitPass; the crash is retried twice first
The lifecycle holds: queued before anything is computed, attempts counted, a settled run never rewrittenpackages/record/src/runLifecycle.test.ts, 8 cases on the in-memory recordPass
The whole repo still checksbun check on top of current develop31 of 31 tasks
The real server computes on the threadapps/server booted on a throwaway Postgres and driven through the APIQueued, running, succeeded with progress on the row; a refused run failed; the log names the thread
Nothing in the browser flows regressedPlaywright on this branch and on a clean develop checkout27 passed, 10 failed on both: the same 10
The thread's memory is returnedProcess memory around a year of the synthetic options chainAbout 80 MB before, 150 to 260 MB peak, back to about 80 to 125 MB after

Not evidenced: recorded options through the thread (the dataset such a run prepares and saves is covered on the job's thread only; the thread test uses synthetic options). Memory for a year of real options data. The Run bar's and the chat step's live progress in a browser. Hosted Robodev. The 10 Playwright failures are failures on develop too, so those flows have no browser check on either side.

Changes outside the feature

Data and rollout

Worth reviewing by hand

Ordered by how much a mistake would cost. Tick them off as you go; the ticks stay in this browser.

What comes after

  1. Run speed in the data readers (LATB-101), which now pays off on the run thread.
  2. Batch runs: one saved version and one job per change, under a shared batch id.
  3. A heartbeat in the job runner, so an orphaned run is retried in seconds.
  4. A recorded-options run through the thread, and its memory on real data.