fix(runtime): list a run under its new status as soon as its record shows it
Merged
KI
18d ago+92+92+01234567890123456789-12-12-01234567890123456789
Problem
A client that reads a run as interrupted and then lists GET /runs?status=interrupted can get [].
- Farm.js's agent pipeline test hit this on CI:
expected [] to include 'run_…'(farming-labs/farm.js run 44391935901, Ubuntu Node 22.13; the same failure hit a Windows Node 24 leg on another PR). - It reproduces with the store alone, on 0.6.0: once the index is built,
getRunreturnsinterruptedwhilelistRuns({ status: "interrupted" })returns[]untilputRunsettles.
Root cause
putRunsavedruns:<id>first, then moved the run'sindex:entries.- Between the two, a reader could see the record with its new status while the listing still lacked it.
- Existing tests wait for
putRunto settle before listing, so they never looked inside that window.
Change
putRunsaves the index entries before the record.- A conditional save that loses its version check takes its entries back. It removes each one the stored record has no use for and puts the stored record's copy back in the rest.
index:doneis still saved after the record. Retention deletes by it without reading the record, so a run is never deleted before it finished.- Every other reader of an entry already reads the record again before acting:
deliverneedsstatus === "waiting", and a run saved as waiting looks for kept events again, so an event sent in between is not lost.wakeDueandexecutecheck the status.
spec/durability.mdstates the guarantee.
Safety and compatibility
- No API or stored-format change. One
RunStoreserves every store, the Durable Objects index included, so all of them get this. - A run can briefly appear under its old status and its new one at once. Before, it could be missing from both.
Verification
pnpm exec vitest run test/store-index.test.ts: fails without the change (the listing is[]at every status) and passes with itpnpm build && pnpm exec vitest run: 364 passedpnpm typecheck: clean- The store-only reproduction against the built runtime now lists the run right after the record changes
Documentation and release
spec/durability.md- Needs a patch release for Farm.js's
@farm.js/agentto pick it up
Summary by cubic
Fixes a race in putRun where the run record was saved before its index entries, so a client that read a run as interrupted and then listed GET /runs?status=interrupted could get [] until the index write landed.
- Index entries are now saved before the record, so a run is listed under its new status as soon as its record shows it.
- A conditional write that loses its version check removes the entries it wrote ahead, or puts the stored record's copy back.
- The
index:doneentry is still saved after the record, so retention never deletes a run before it finished. - Adds
test/store-index.test.tsreproducing the race; it fails without the change at every status and passes with it.
No API or stored-format change; one RunStore serves all stores, so every backend gets this.