Skip to content

test(shared-budget): deflake the concurrent-spend assertion#6

Merged
hamr0 merged 1 commit into
mainfrom
test/deflake-shared-budget
May 24, 2026
Merged

test(shared-budget): deflake the concurrent-spend assertion#6
hamr0 merged 1 commit into
mainfrom
test/deflake-shared-budget

Conversation

@hamr0
Copy link
Copy Markdown
Owner

@hamr0 hamr0 commented May 23, 2026

Deflakes shared-budget.test.js (the intermittent ubuntu/Node-20 failure seen on PR #5).

Problem: the test asserted exact $0.60 across two concurrent workers, but cross-process budget is documented as NOT penny-exact (gotcha #2: "don't rely on hard cents-precision enforcement"). Under heavy lock contention one $0.03 update was lost ($0.57) → CI flake. Passes 65/65 locally.

A /code-review pass correctly flagged my first attempt (a $0.54 lower-bound band) as too loose: record() is file-relative read-modify-write, so a broken lock loses a few updates and clusters just under $0.60 — it does not collapse to $0.30. No single aggregate floor separates "rare 1-tick loss on correct code" from "broken lock".

Fix: assert only the scheduling-independent invariants —

  • record() can never over-count (it adds its delta to a lock-fresh read) — the hard invariant; over-counting would be a real double-apply bug;
  • both workers' spend accumulates cross-process (total > one worker's run) — a coarse collapse smoke check, immune to documented soft-budget slop.

Renamed off "is exact". Test-only — budget.js untouched, no version bump, no publish.

🤖 Generated with Claude Code

The "total is exact" test asserted exact $0.60 across two concurrent
workers, but cross-process budget is documented as NOT penny-exact
(gotcha #2), and exact-equality flaked CI under lock contention (one lost
$0.03 tick; passes 65/65 locally). A /code-review pass correctly noted the
relaxed band I first tried ($0.54 floor) was too loose to catch a broken
lock — record() is file-relative read-modify-write, so a broken lock loses
a few updates and clusters just under $0.60, not down at $0.30. No single
aggregate floor cleanly separates "rare 1-tick loss on correct code" from
"broken lock".

So assert only the scheduling-independent invariants:
- record() can NEVER over-count (adds its delta to a lock-fresh read) —
  the hard invariant; over-counting would be a real double-apply bug.
- both workers' spend accumulates cross-process (total > one worker's run)
  — a coarse collapse smoke check, immune to documented soft-budget slop.

Renamed the test off "is exact". Test-only; budget.js untouched.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@hamr0 hamr0 merged commit e3766ae into main May 24, 2026
6 checks passed
@hamr0 hamr0 deleted the test/deflake-shared-budget branch May 24, 2026 07:51
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.

1 participant