fix(loop-context): lock the daily-spend state file against concurrent writers - #521
Merged
cobusgreyling merged 1 commit intoAug 17, 2026
Conversation
… writers recordDailySpend() did an unguarded read-modify-write: read the JSON state file, add tokensDelta, write it back, with no locking. Two overlapping invocations for the same pattern -- e.g. two scheduled loops hitting the same pattern around the same time -- both read the same stale total and the second write clobbers the first, silently losing a delta. This directly undermines the daily-budget circuit breaker's purpose: a lost delta means loop-context can under-count cumulative spend and delay or miss the escalation that's supposed to cap cost. loop-worktree already solved the same class of problem for its manifest with a lock-file-plus-poll mutex (open with 'wx', treat an existing lock older than 30s as stale and reclaim it, release in a finally). Applied the same shape here, scoped per pattern's own lock file so unrelated patterns never contend with each other. Test plan: added a regression test that fires two concurrent recordDailySpend calls with the same delta and asserts both land (2000 total, not 1000). Full clean rebuild + npm test: 52/52 passing. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
cobusgreyling
approved these changes
Aug 17, 2026
cobusgreyling
left a comment
Owner
There was a problem hiding this comment.
Needed lock around the daily-spend read-modify-write. Same wx + stale-reclaim shape as loop-worktree. Concurrent test asserting 2000 not 1000 is the right regression.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
recordDailySpend() did an unguarded read-modify-write: read the JSON
state file, add tokensDelta, write it back, with no locking. Two
overlapping invocations for the same pattern -- e.g. two scheduled
loops hitting the same pattern around the same time -- both read the
same stale total and the second write clobbers the first, silently
losing a delta. This directly undermines the daily-budget circuit
breaker's purpose: a lost delta means loop-context can under-count
cumulative spend and delay or miss the escalation that's supposed to
cap cost.
Fix
loop-worktree already solved the same class of problem for its
manifest with a lock-file-plus-poll mutex (open with 'wx', treat an
existing lock older than 30s as stale and reclaim it, release in a
finally). Applied the same shape here, scoped per pattern's own lock
file so unrelated patterns never contend with each other.
Test plan
Added a regression test that fires two concurrent recordDailySpend
calls with the same delta and asserts both land (2000 total, not
1000). Full clean rebuild + npm test: 52/52 passing.