fix(runtime): list a run under its new status as soon as its record shows it

Merged
KI
18d ago
+92-12

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, getRun returns interrupted while listRuns({ status: "interrupted" }) returns [] until putRun settles.

Root cause

  • putRun saved runs:<id> first, then moved the run's index: entries.
  • Between the two, a reader could see the record with its new status while the listing still lacked it.
  • Existing tests wait for putRun to settle before listing, so they never looked inside that window.

Change

  • putRun saves 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:done is 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:
    • deliver needs status === "waiting", and a run saved as waiting looks for kept events again, so an event sent in between is not lost.
    • wakeDue and execute check the status.
  • spec/durability.md states the guarantee.

Safety and compatibility

  • No API or stored-format change. One RunStore serves 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 it
  • pnpm build && pnpm exec vitest run: 364 passed
  • pnpm 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/agent to 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:done entry is still saved after the record, so retention never deletes a run before it finished.
  • Adds test/store-index.test.ts reproducing 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.