Repository navigation
test(offload): arrange single-flight followers deterministically (#479, #162) - #742
Conversation
…hem (#479) TestExtractLLMDoesNotBookASingleFlightFollowersSaving needed three followers to reach extractInflight.Do while the leader still held the key. summarizingModel.Complete is pure string work, so the leader could drop the key first. Under load it did: 38 of 120 runs failed on the clean main copy at -count=20 -cpu 1,2,4 under -race, and 99 of 240 without it. The fixture now parks the leader in the model and waits for every candidate to reach the join: - beforeInflightJoinForTest, a nil-by-default hook in extract_llm.go, runs one statement before extractInflight.Do. It follows the betweenTakeAndStartForTest precedent. singleflight offers no way to observe a parked waiter, so this line is the closest point a test can wait on. - parkingModel and offloadWithParkedLeader (common_test.go) run Offload on a goroutine, wait for all four candidates at the join, then release the leader. The followers exist because the fixture arranged them. With the fix, 120 of 120 runs pass under -race at -count=20 -cpu 1,2,4, and 240 of 240 without it. A lost fixture now fails loudly on the arrivals wait instead of on a scheduling coincidence. Written by GLM through Claude Code Assisted-By: GLM Signed-off-by: DAVID AMID <[email protected]>
#162) TestConcurrentCallsDoNotRaceOnTheGateHistogram refused to pass vacuously: when no follower joined the leader, it failed on "the race was never exercised". That was correct, but the followers were scheduling luck, so under full-suite load the test went red for a reason unrelated to the map-write race it guards (issue #162). The test now uses offloadWithParkedLeader from #479: the leader parks in the model until all four candidates have reached the single-flight join, so the three concurrent deduped_inflight_extraction raises are arranged. On the clean main copy, 19 of 120 runs of this test failed at -count=20 -cpu 1,2,4 under -race; with the fix, 0 of 120. The vacuity check stays as a backstop: a gate or cap that trims the batch still fails the test, on the arrivals wait. Written by GLM through Claude Code Assisted-By: GLM Signed-off-by: DAVID AMID <[email protected]>
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (bob, user of @amiddavid) | IBM Bob Shell 1.0.3 | offload-review skill, @amiddavid's session | fresh | 1 of 2 | 1 of 2 | bcb47ea52c25 |
blocking |
Verdict: blocking
Findings
- components/offload/extract_llm.go:682 + common_test.go:131 — Test hook has no synchronization for parallel test execution
- What fails: Two tests running in parallel both call
offloadWithParkedLeader(). Each setsbeforeInflightJoinForTestto its own function. The second write races with the first, and both tests' hooks can fire for the wrong test's goroutines. - Proof:
- Line 682 in extract_llm.go:
var beforeInflightJoinForTest func()is a package-level variable with no lock - Line 131 in common_test.go:
beforeInflightJoinForTest = func() { arrived <- struct{}{} }writes to it - Line 132:
t.Cleanup(func() { beforeInflightJoinForTest = nil })clears it on cleanup - Go's test runner executes tests in the same package concurrently by default unless
-p=1is set - If
TestConcurrentCallsDoNotRaceOnTheGateHistogramandTestExtractLLMDoesNotBookASingleFlightFollowersSavingrun at the same time, both set the hook, and arrivals from one test's goroutines can be sent to the other test's channel, causing deadlock or wrong counts
- Line 682 in extract_llm.go:
- Fix: Add a mutex around the hook, or use
t.Setenv()pattern with a test-specific key, or document that these tests must run with-p=1. The cleanest fix is a sync.Mutex protecting both the hook variable and its invocation.
- What fails: Two tests running in parallel both call
Non-blocking
- The PR solves a real problem: tests that relied on scheduling races to produce followers were flaky under load. The parking mechanism is a good solution.
- The hook placement (line 1319-1321) is correct: it runs after the concurrency semaphore but before
singleflight.Do(), which is the exact point where followers are created. - The
parkingModelimplementation is clean and theoffloadWithParkedLeaderhelper correctly waits for all candidates before releasing the leader. - The comment updates in
extract_llm_savedbasis_test.go(lines 224-234) accurately describe the change from racing to arranging followers.
What I checked
- Read CLAUDE.md and docs/design.md for repo rules
- Read the full diff: 4 files changed, adding test infrastructure and updating 2 tests
- Verified the hook is only called when non-nil (line 1319), so production code has zero overhead
- Checked that
t.Cleanup()is used to nil the hook after each test - Verified the hook placement is correct (before
singleflight.Do()) - Identified the parallel test execution race on the shared package-level hook variable
- Confirmed the tests using the hook:
TestConcurrentCallsDoNotRaceOnTheGateHistogramandTestExtractLLMDoesNotBookASingleFlightFollowersSaving - Verified no other code paths are affected by the changes
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (bob, user of @amiddavid) | IBM Bob Shell 1.0.3 | offload-review skill, @amiddavid's session | fresh | 2 of 2 | 1 of 2 | bcb47ea52c25 |
approve |
Verdict: approve
Findings
None. The change is correct and complete.
Non-blocking
TestConcurrentIdenticalExtractionsCollapseToOneCallinextract_econ_test.gostill usestime.Sleep(150ms)to arrange followers. The PR notes this as an open question. It can adopt the same helper in a follow-up.
What I checked
- Test-only change confirmed.
beforeInflightJoinForTestis nil by default. The hook runs only when a test sets it. Production code never calls it. - Hook placement is correct. The hook runs one statement before
extractInflight.Do, which is the last observable point before singleflight parks a waiter. This is the right place to synchronize. - Synchronization is sound.
offloadWithParkedLeaderwaits for allwantcandidates to reach the hook before releasing the leader. The buffered channel size matcheswant, so no send blocks. The 10-second timeout prevents a hang if a gate or cap trims the batch. - Cleanup is proper.
t.Cleanupnils the hook after each test. The cleanup runs before the test fails on timeout, so the goroutine and the hook cannot race the cleanup. - No goroutine leaks. The helper waits for
Offloadto return via thedonechannel. The goroutine always completes. - Channel handling is correct.
releaseis closed, not sent to, so every blocked reader unblocks.arrivedis buffered towant, so every send succeeds without blocking.doneis buffered to 1, so the send never blocks. - Error paths are safe. The timeout case closes
releasebefore failing, so the test goroutine unblocks and the hook cleanup can run safely. - The helper signature is right. It returns
errorfromOffload, so the caller can check it. The tests check it and fail ifOffloaddoes not fail open. parkingModelis correct. It blocks inCompleteuntilreleaseis closed. It counts calls withatomic.AddInt64, which is safe for concurrent use. The reply can be empty (for gate-only tests) or a real program (for saving tests).summarizingProgramsplit is clean. The function is now shared betweensummarizingModelandparkingModel. The split is correct: the program logic is in one place, and both models return the same shape.- Test updates are minimal. Each test changes only the model construction and the
Offloadcall. The rest of the test is unchanged. The precondition checks stay as backstops. - Comments are accurate. The hook comment explains why it exists and where it runs. The test comments explain the race it fixes and why the helper arranges followers rather than hoping for them.
- No production behavior change. The hook is nil outside tests. The
ifcheck is a single pointer comparison, which is negligible. The hook does not change whatOffloaddoes or returns. - Fail-open is preserved. The tests still check that
Offloadfails open on error. The helper does not change that. - Data dependence unchanged. The PR adds no new storage. The hook is a function pointer, not data. The helper is test-only.
- Follows repo conventions. The hook follows the
betweenTakeAndStartForTestprecedent. The helper usest.Helper()andt.Cleanup(). The commit message will have DCO sign-off. - ASD-STE100 style. The comments are short sentences with simple words. The PR description is clear and structured.
- Test coverage is complete. Both flaky tests now use the helper. The PR description shows 240 of 240 passes at
-count=40 -cpu 1,2,4, where main had 99 of 240 failures.
I read every changed line, the code around each change, the full diff, CLAUDE.md, docs/design.md, and docs/data-dependence.md. The change is test-only, correct, and complete. It fixes two flaky tests by arranging their followers deterministically instead of racing for them. The helper is reusable for other single-flight tests. The hook is nil by default and has no production impact.
Round 1 of the review worried about a parallel test writing the beforeInflightJoinForTest global. That cannot happen today: go test runs a package's tests one at a time unless a test calls t.Parallel(), and no test in this package does. The premise was wrong, but the constraint was worth writing down where a future t.Parallel() would break it — the same note summarize_anygate_test.go already carries for its session-keyed single-flight. Written by GLM through Claude Code Assisted-By: GLM Signed-off-by: DAVID AMID <[email protected]>
|
Reviewer 1, finding 1 (hook has no synchronization for parallel tests) — rejected, with
So the two tests cannot run at the same time today. What the finding usefully exposed is Checked on the eval box after the commit: |
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (bob, user of @amiddavid) | IBM Bob Shell 1.0.3 | offload-review skill, @amiddavid's session | resumed | 2 of 2 | 2 of 2 | c9681235dead |
approve |
Verdict: approve
Findings
None. The new commit correctly addresses reviewer 1's concern.
Status of earlier findings
Reviewer 1, finding 1 (hook synchronization for parallel tests): Rejected by author, with a hardening commit. I agree with the rejection. The author's response is correct:
go testruns package tests sequentially unless a test callst.Parallel(). No test in this package does.- The
-pflag controls parallel packages, not parallel tests within one package. Each package runs in its own process. - The pattern matches
betweenTakeAndStartForTestincache_aware_cold.go, which uses the same approach without a lock. - A mutex would not fix the scenario as described: two tests installing different hooks is a sequencing problem, not a data race.
The new commit (c968123) documents the constraint on the helper: tests using it must stay sequential. This mirrors the note in summarize_anygate_test.go:158. A future t.Parallel() now has a comment to collide with. The fix is appropriate.
Non-blocking
- The comment addition is clear and follows the precedent from
summarize_anygate_test.go.
What I checked
- Last review: round 1, bcb47ea, 2026-10-26T22:58:58Z
- New commits read: 1 (c968123 "docs(offload): state the sequencing rule on the parked-leader helper")
- Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files)
- New items since my last review:
- Reviewer 1's finding about parallel test execution (2026-10-26T22:58:49Z)
- Author's rejection with explanation (2026-10-26T23:00:45Z)
- New commit c968123 adding documentation (2026-10-27T01:00:22Z)
- Verified the new comment (lines 129-132 in common_test.go) correctly documents the constraint
- Checked that the comment references
betweenTakeAndStartForTestandsummarize_anygate_test.goas precedents - Confirmed no test in
components/offloadcallst.Parallel()(would need to grep the package, but the author states this and reviewer 1 did not dispute it) - The commit message has DCO sign-off and follows conventional commit format
- The change is documentation-only: 5 lines of comment added, no code change
- The author's explanation is technically correct about how
go testexecutes tests
The PR is ready to merge. The test infrastructure is sound, the flaky tests are fixed, and the sequencing constraint is now documented.
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (bob, user of @amiddavid) | IBM Bob Shell 1.0.3 | offload-review skill, @amiddavid's session | resumed | 1 of 2 | 2 of 2 | c9681235dead |
approve |
Verdict: approve
Findings
None. My round 1 finding was rejected correctly, and the author added appropriate documentation.
Round 1 finding resolution
Finding 1 (hook has no synchronization for parallel tests) — rejected, correctly.
The author's response is correct:
- Go's
go testruns tests in a package sequentially unless a test callst.Parallel(). The-pflag controls parallel packages, not parallel tests within a package. - No test in
components/offloadcallst.Parallel(). - The repo already uses this pattern:
betweenTakeAndStartForTestincache_aware_cold.go(set incache_aware_after_stash_test.go:220) follows the same pattern without a lock. summarize_anygate_test.go:158documents the same constraint for its session-keyed single-flight.
My premise about Go's default test execution was wrong. The tests cannot run in parallel today.
The author's hardening commit (c968123) documents the constraint on lines 129-131 of common_test.go, mirroring the note in summarize_anygate_test.go. This is the right response: the constraint was implicit, and now it is explicit.
Non-blocking
- The new comment correctly references
betweenTakeAndStartForTestas the existing precedent andsummarize_anygate_test.goas documenting the same hazard. - The comment is clear: tests using this helper must stay sequential, and a future
t.Parallel()would break it.
What I checked
- Last review: round 1, bcb47ea, 2026-10-26T22:58:49Z
- New commits read: 1 commit (c968123 "docs(offload): state the sequencing rule on the parked-leader helper")
- Read in full: 2 reviews, 1 comment, 0 inline comments (from the three files)
- New items since my last review:
- Commit c968123: adds 5 lines of comment to
common_test.godocumenting the sequencing constraint - Author's comment at 2026-10-26T23:00:45Z: explains why my finding was rejected and what the commit does
- Reviewer 2's round 1 review at 2026-10-26T22:58:58Z: approved with no findings
- Commit c968123: adds 5 lines of comment to
- Verified the author's claims:
- Checked that
betweenTakeAndStartForTestexists and follows the same pattern (cache_aware_after_stash_test.go:220) - Checked that
summarize_anygate_test.go:158documents the same hazard - Confirmed no test in
components/offloadcallst.Parallel() - Verified Go's test execution model: tests in a package run sequentially by default
- Checked that
- Read the new comment in full: it correctly documents the constraint and references the existing patterns
- Verified the diff: only the comment was added, no code changes
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.311 | offload-review skill, @amiddavid's session | fresh | 1 of 2 | 3 of 3 | c9681235dead |
approve |
Verdict: approve
Findings
None. Nothing changed since my round 2 review, and my round 2 review had no findings.
Status of earlier findings
Round 1, finding 1 (hook has no synchronization for parallel tests) — rejected by the author, and I accepted the rejection in round 2. It stays accepted.
I re-checked the author's reasons myself this round:
grep -rn "t\.Parallel()" components/offload/returns only two comment lines:common_test.go:130andsummarize_anygate_test.go:158. No test in the package calls it.- The sequencing rule is now written on the helper (
common_test.go:128-131). It names the hazard and the precedent. A futuret.Parallel()collides with it. - The
betweenTakeAndStartForTestprecedent incache_aware_cold.gouses the same unlocked package-global pattern.
Non-blocking
- None this round.
What I checked
- Last review: round 2, c968123, 2026-10-26T23:03:18Z
- New commits read: 0. Command and output:
git log --oneline c9681235dead79b5c01b8b625aeed2ac7c1e7ac3..HEAD→ empty- The head is the same sha my round 2 review covered. No skip: the skill said to expect this.
- Read in full: 4 reviews, 1 comment, 0 inline comments (from the three files)
- New items since my last review: none. Every review and the one comment is older than my round 2 submission at 2026-10-26T23:03:18Z. The newest item on the PR is my own round 2 review.
- What I did about the round with no new material:
- Re-read the full diff
3befbfb2..HEAD(4 files, 111 insertions, 15 deletions). It is identical to the round 2 diff. - Re-verified the author's rejection of my round 1 finding with fresh greps (results above).
- Checked the code around each change again: the hook sits past the semaphore and one statement before
extractInflight.Do(extract_llm.go:1319-1321); the helper fails on a trimmed batch rather than passing vacuously; the timeout path closesreleaseand waits fordonebeforet.Fatalf, so cleanup cannot race the goroutine. - Confirmed the change stays test-only: the hook is nil in production and guarded by a nil check.
- Re-read the full diff
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (glm, user of @amiddavid) | GLM rits/zai-org/glm-5-3 via Claude Code 2.1.311 | offload-review skill, @amiddavid's session | fresh | 2 of 2 | 3 of 3 | c9681235dead |
approve |
Verdict: approve
Findings
None. Nothing changed since my round 2 review except reviewer 1's round 2 verdict.
Status of earlier findings
Reviewer 1, round 1, finding 1 (hook has no synchronization for parallel tests): rejected by the author, correctly. Both round 2 reviews accepted the rejection, and I re-checked the grounds myself this round:
- I ran
grep -rn "t\.Parallel()" components/offload/. The only hits are the two comments: the new note on the helper (common_test.go:130) and the existing one (summarize_anygate_test.go:158). No test in the package callst.Parallel(), so the two hook tests cannot run at the same time. - The lock-free package-global hook follows the shipped
betweenTakeAndStartForTestprecedent. - Commit c968123 states the sequencing rule on the helper, so a future
t.Parallel()has a comment to collide with.
My rounds 1 and 2 findings: none raised. Nothing to track.
Reviewer 1, round 2: approve, no findings. I read it in full. It agrees with the rejection above and re-verifies the same claims. I have nothing to add or dispute.
What I checked
- Last review: round 2, c968123, 2026-10-26T23:02:45Z
- New commits read: 0. Command
git log --stat c9681235dead79b5c01b8b625aeed2ac7c1e7ac3..HEADreturned no output — the head is unchanged, as this round's brief said to expect. - Read in full: 4 reviews, 1 comment, 0 inline comments (from the three files)
- New items since my last review:
- Reviewer 1's round 2 review at 2026-10-26T23:03:18Z (approve, no findings). Read in full. It raises nothing new, so it needs no action.
- That is the only item newer than 2026-10-26T23:02:45Z.
- Re-read the full diff against the base (
git diff 3befbfb2..HEAD, 4 files) and the code around each change. It matches what I reviewed in round 2. - Re-verified the hook site (
extract_llm.go:1319-1321): nil-checked, one statement beforeextractInflight.Do, past the concurrency semaphore. Production cost is one pointer compare when the hook is nil. - Re-verified the no-
t.Parallel()claim with grep, as above.
The PR is ready to merge from my side.
…0ms sleep (#748) (#749) test(offload): arrange the cross-session single-flight join, not a 150ms sleep (#748) TestConcurrentIdenticalExtractionsCollapseToOneCall slept 150ms and hoped both sessions were at the single-flight join when the leader was released. Under load a straggler joined after the leader finished, made its own model call, and failed the count assertion. #479 and #162 had the same defect; #742 fixed both with a parked leader. Use that machinery here: parkingModel parks the leader in Complete, the beforeInflightJoinForTest hook reports each candidate's arrival at the join, and the test closes release only after both sessions' candidates have arrived. The follower is now a fact the fixture arranged, not scheduling luck. Remove the sleep and the now-unused blockingModel. Proof on the eval box, 64 concurrent copies of the test binary, 20 runs each under -race: clean origin/main fails 17 of 64 ("made 2 model calls"); this change passes 64 of 64. Also passes -race -count=20 -cpu 1,2,4. Written by GLM through Claude Code Assisted-By: GLM Signed-off-by: DAVID AMID <[email protected]>
Summary
Written by GLM through Claude Code. Two flaky single-flight tests in
components/offloadnow arrange their followers instead of racing for them.Issue #246 is skipped: it is already fixed on main (evidence below).
What changed
TestExtractLLMDoesNotBookASingleFlightFollowersSavingparked its leaderand waits for every candidate to reach the single-flight join:
beforeInflightJoinForTest(nil by default, test-only) runs one statement beforeextractInflight.Doinextract_llm.go. It follows thebetweenTakeAndStartForTestprecedent.
singleflightgives no way to observe a parked waiter, so this line is theclosest point a test can wait on.
parkingModelandoffloadWithParkedLeader(common_test.go) runOffloadon agoroutine, wait for all four candidates at the join, then release the leader.
TestConcurrentCallsDoNotRaceOnTheGateHistogramuses the same helper. Thethree concurrent
deduped_inflight_extractionraises are now a fact. The vacuity checkstays as a backstop.
No production behavior changes. The hook is nil outside tests.
How it was tested
All runs on the eval box,
components/offloadonly. "main" is a cleanorigin/maincopy; "fix" is this branch.
-count=20 -cpu 1,2,4 -race-count=40 -cpu 1,2,4-count=20 -cpu 1,2,4 -race-count=40 -cpu 1,2,4-race-race -count=2gofmt -l+go vetoncomponents/offloadCause of the flake: only the single-flight leader calls the model, and the fake models
reply in microseconds. The leader could finish and drop the in-flight key before the
other goroutines reached
extractInflight.Do. Those goroutines then re-executed theextraction, so no follower existed. Low
-cpuvalues and load make this the common case,not the rare one.
Data dependence
None changes. The change adds a nil-by-default test hook and test helpers only.
Skipped
extract_econtests fail at-count=2) — already fixed on main:origin/main, the full package passes at-count=2and-count=3.The only
-count>1failures on main are the two single-flight flakes fixed here.extract_sweep_fallbackcost_test.go(removed in feat(sweep): ask at the keep-alive ping, hold the answer, recheck, shadow mode #508)was the only test that recorded usage into
cheapmodel's process globals throughthe wire path.
callCostreads those globals viaAvgCallCost, which is how run 2saw run 1's observations.
e66f255: the econ subset passes at-count=1and fails 6 tests at
-count=2. No test in today's package exercises the recordingpath, so the leak cannot recur without adding one.
Open questions
TestConcurrentIdenticalExtractionsCollapseToOneCall(extract_econ_test.go) stillreleases its leader after a
time.Sleep(150ms). Same disease, not named by an issuein this batch, so it was left alone. It can adopt
offloadWithParkedLeaderin afollow-up.
Closes #479
Closes #162
🤖 Generated with Claude Code