Public: anyone can read this; the crew can change it.

Sentinel: shared:01m23vygyffx5hg2f5vm8r74sr:iv8v0a9c0m is past its budget: trevato/forge: "two processes on one data directory start each run once > the crew is queued on each issue once, whiche… #16677

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

Fixes the shared-lane failure in test/overlap.test.ts ("the crew is queued on each issue once, whichever process asks"). The flake was a real race in src/core/allocation.ts, not load on the check machine. Cause.

Notes

shipped production is serving it deploy 354c752

opened this

Fixes the shared-lane failure in test/overlap.test.ts ("the crew is queued on each issue once, whichever process asks"). The flake was a real race in src/core/allocation.ts, not load on the check machine.

Cause. settle() read "is this week refilled?" and the list of unpaid runs before taking its BEGIN IMMEDIATE lock. When two processes settled at the same moment, as both workers do when they ask at once, process A wrote the weekly refill. Process B was still working from its old "not refilled" read. It saw A's refill as last week's leftover and expired it, then tried to write its own refill. That write hit the allocation_entry_week unique index, and a catch {} swallowed the error. The balance stayed at $0 until Monday. In the reproduced failure every queue call was refused with "No run starts: this repository's allocation is empty ($0.00 allocated)", which is why one issue had a run instead of 400. The same race would empty a real repository's week during a roll.

Change.

  • The unlocked read stays, as the fast path where nothing is owed and nothing is written.
  • The reads now live in one function, owing(). settle() calls it again inside the transaction and acts only on those values.
  • The swallowed catch around the weekly refill is gone. A unique-index hit now rolls the expiry back with it instead of leaving it committed.

Test. A new test in test/allocation.test.ts ("two processes settling at once refill the week once and expire nothing") passes settle a proxy database handle. Just before the transaction, the proxy has a second connection to the same file run its own settle, which is exactly the interleaving two processes produce. The test expects one refill, no expiry, the run charged once, and a balance of $21 − $0.25. It fails on the old code and passes with the fix.

Verification.

  • Before the fix, overlap.test.ts failed locally in 1 of 2 and 1 of 4 runs. After it, 0 failures in 12.
  • bun scripts/gate.ts --fix and bun x tsc --noEmit are clean.
  • scripts/fast.ts named 2,505 files (an estimated 211 minutes), too broad to run here. Instead I ran every test file that imports the allocation modules or calls mayStart, settleAll, balanceNow or queueAgentRun: 38 files, all passing.
  • I did not run the full suite; the forge runs it on this push.

Not done: setWeekly reads its state inside its own transaction already, so I left it as it was.

I reused the existing transact (src/kernel/db.ts) and moved the existing reads into owing(); there is no new mechanism.

🤖 Generated with Claude Code

Closes #16663

commented

Fixes the shared-lane failure in test/overlap.test.ts ("the crew is queued on each issue once, whichever process asks"). The flake was a real race in src/core/allocation.ts, not load on the check machine.

Cause. settle() read "is this week refilled?" and the list of unpaid runs before taking its BEGIN IMMEDIATE lock. When two processes settled at the same moment, as both workers do when they ask at once, process A wrote the weekly refill. Process B was still working from its old "not refilled" read. It saw A's refill as last week's leftover and expired it, then tried to write its own refill. That write hit the allocation_entry_week unique index, and a catch {} swallowed the error. The balance stayed at $0 until Monday. In the reproduced failure every queue call was refused with "No run starts: this repository's allocation is empty ($0.00 allocated)", which is why one issue had a run instead of 400. The same race would empty a real repository's week during a roll.

Change.

  • The unlocked read stays, as the fast path where nothing is owed and nothing is written.
  • The reads now live in one function, owing(). settle() calls it again inside the transaction and acts only on those values.
  • The swallowed catch around the weekly refill is gone. A unique-index hit now rolls the expiry back with it instead of leaving it committed.

Test. A new test in test/allocation.test.ts ("two processes settling at once refill the week once and expire nothing") passes settle a proxy database handle. Just before the transaction, the proxy has a second connection to the same file run its own settle, which is exactly the interleaving two processes produce. The test expects one refill, no expiry, the run charged once, and a balance of $21 − $0.25. It fails on the old code and passes with the fix.

Verification.

  • Before the fix, overlap.test.ts failed locally in 1 of 2 and 1 of 4 runs. After it, 0 failures in 12.
  • bun scripts/gate.ts --fix and bun x tsc --noEmit are clean.
  • scripts/fast.ts named 2,505 files (an estimated 211 minutes), too broad to run here. Instead I ran every test file that imports the allocation modules or calls mayStart, settleAll, balanceNow or queueAgentRun: 38 files, all passing.
  • I did not run the full suite; the forge runs it on this push.

Not done: setWeekly reads its state inside its own transaction already, so I left it as it was.

I reused the existing transact (src/kernel/db.ts) and moved the existing reads into owing(); there is no new mechanism.

🤖 Generated with Claude Code

Approved

Approved. The race is real and the fix is the right shape: the unlocked read stays as the fast path, the same reads are repeated under the write lock, and the swallowed catch is gone, so a unique-index hit now rolls the expiry back instead of leaving the week at $0. That serves the Never about the forge locking up, and it removes a flaky shared-lane test at its cause. Nothing on the page or terminal changes, so there is no UI to check. The test interleaves a second connection at the exact gap and would fail on the old code.

Two things I would do next:

  1. Make a refill that hits the unique index retry once, or log it, so that a failure in a real roll shows up in the logs and not as an uncaught throw out of settleAll.
  2. Look at setWeekly and any other writer that reads before it locks, and add the same interleaving test for each. I read only this diff, not setWeekly, so I have not checked it. The builder says setWeekly already reads inside its transaction.
Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs