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

118 files
changed, in 5 commits
4,152
lines added, 489 removed
1,788
of those are tests (794) and generated files (994, mostly the OpenAPI snapshot)
2,364
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. A run that loses its container is back in the queue within about two minutes, a trader can cancel a run, and asking twice for the same run gives one run.

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, then succeeded, failed or cancelled; every write over a pending run names the execution it expects; heartbeat, cancel, one pending run per request, and the sweep of runs nothing reports on; a run summary without its result.141035
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.11112
apps/robodev-beThe runs/execute job with its heartbeat, the runs/requeue-stale job every minute, the one computeRun function, the two ends of a run thread, the cancel route, the wait-by-polling for the assistant, three columns on the runs table. About 980 of these lines are the OpenAPI snapshot.221716
apps/serverThe module a run thread loads, which builds that thread's own Postgres pool and storage client and calls a function of the backend build; pools opened at a given size; tests against a real Postgres, two of them on Parquet read through DuckDB.13514
apps/feQueued, running, failed and cancelled runs in Results, with a Cancel button; live progress in the Run bar and the chat step; polling while a run is pending. About 260 of these lines are API tests.37613
Docs and rulesADR-0014, the layout and assistant docs, the vocabulary, the server README, the Robodev API rule.986

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, cancelled; Cancel
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, replay, cancelstore the run as queued or return the same pending one, enqueue
Chat turnqueues the run, reads its row until it settles
Audit chainsame entries as before
↓ Postgres job queue
Job thread
runs/executeclaims the run, beats every 5 s, saves progress, enforces 10 minutes, writes the outcome
runs/requeue-staleevery minute: re-queues a run nothing reports on
↓ 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; its status, replay and summary are repeated in columns of their ownA run can no longer vanish, the run list reads no result and filters replays in SQL, and rows already stored are not rewritten at deployThree columns and an index to keep in step with the stored record; an old row is filled in the first time its trader's runs are listed
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
Every write over a pending run names the status and attempt it expectsA late or duplicate execution cannot write over a settled or newer runAn execution finds out it lost its run only when its write does not land
A heartbeat on the run and a sweep every minute, not one transaction for the run and its jobThe queue's enqueue takes no transaction of the caller's and hosted Robodev's queue is elsewhere; the sweep also catches a dead containerA run whose request died before enqueueing waits up to 2 minutes; a run that lost its container about 2; where runs compute in the job's thread a lost running run is noticed only after 16 minutes
Cancelled is a status of its own, not a failed run with a reasonNothing went wrong, and lists and counts of failed runs stay honestOne more status every reader handles; where runs compute in the job's thread a cancelled run computes to its end before its outcome is dropped
A second identical request returns the pending runDouble clicks and an assistant asking again do not queue a second year of workA trader cannot run the same thing twice at once on purpose
The run thread imports a function from the backend build instead of invoking a jobNothing can enqueue or claim itThe server names a compiled module path of the backend
The server opens its pools itself, with drizzle-orm, pg and zod at the SDK's versionsThe SDK's createDb fixes the pool at 5 and offers no sizeThree dependencies to keep at the SDK's versions, or the two would stop sharing one copy of Drizzle

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 (7 cases in the file)
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, guarded writes, heartbeat and sweep, cancel, one pending run per requestpackages/record/src/runLifecycle.test.ts, 18 cases on the in-memory recordPass
DuckDB works inside a run threadapps/server/tests/runThreadData.test.ts: an imported futures run and two recorded options runs on real-format Parquet fixtures, each against the run computed in the test's threadPass, byte for byte
Cancel stops a computing threadrunThread.test.ts through the real heartbeat, and the booted server: cancelled at day 1 of 259, still cancelled 8 seconds later, the job endedPass
Cancel and the pending-run rule through the APIapps/fe/tests/api/backtester.e2e.test.ts on a throwaway RobodevPass
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; a second identical request got the same run; the log names the thread
Nothing in the browser flows regressedPlaywright on the first two commits and on a clean develop checkout27 passed, 10 failed on both: the same 10. Not run again after the review changes
The thread's memory is returnedProcess memory around a year of the synthetic options chainAbout 85 to 170 MB before, 120 to 260 MB peak, back to where it started

Not evidenced: The sweep against a container that really died: the record tests and the store cover its logic, no test kills a process. 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 full Playwright suite after the review changes; the Cancel button and the cancelled state were looked at in a browser at 375 and 1440 px with the API answers held. 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. Memory of an options year on real data, measured on a worker task.