Skip to content

fix(loop-context): lock the daily-spend state file against concurrent writers - #521

Merged
cobusgreyling merged 1 commit into
cobusgreyling:mainfrom
KhaiTrang1995:fix/loop-context-daily-spend-race
Aug 17, 2026
Merged

fix(loop-context): lock the daily-spend state file against concurrent writers#521
cobusgreyling merged 1 commit into
cobusgreyling:mainfrom
KhaiTrang1995:fix/loop-context-daily-spend-race

Conversation

@KhaiTrang1995

Copy link
Copy Markdown
Contributor

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.

… 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 cobusgreyling left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@cobusgreyling
cobusgreyling merged commit c39ca41 into cobusgreyling:main Aug 17, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants