Skip to content

test(offload): arrange single-flight followers deterministically (#479, #162) - #742

Merged
amiddavid merged 3 commits into
mainfrom
fix/glm-flaky-offload
Oct 27, 2026
Merged

amiddavid merged 3 commits into
mainfrom
fix/glm-flaky-offload

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Summary

Written by GLM through Claude Code. Two flaky single-flight tests in
components/offload now arrange their followers instead of racing for them.
Issue #246 is skipped: it is already fixed on main (evidence below).

What changed

No production behavior changes. The hook is nil outside tests.

How it was tested

All runs on the eval box, components/offload only. "main" is a clean origin/main
copy; "fix" is this branch.

Run Result
main, both tests, -count=20 -cpu 1,2,4 -race 38 of 120 FAIL (no follower ran)
main, both tests, -count=40 -cpu 1,2,4 99 of 240 FAIL
fix, both tests, -count=20 -cpu 1,2,4 -race 120 of 120 pass
fix, both tests, -count=40 -cpu 1,2,4 240 of 240 pass
fix, full package, -race pass (169s)
fix, full package, -race -count=2 pass (258s)
fix, gofmt -l + go vet on components/offload clean

Cause 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 the
extraction, so no follower existed. Low -cpu values 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

Open questions

  • TestConcurrentIdenticalExtractionsCollapseToOneCall (extract_econ_test.go) still
    releases its leader after a time.Sleep(150ms). Same disease, not named by an issue
    in this batch, so it was left alone. It can adopt offloadWithParkedLeader in a
    follow-up.

Closes #479
Closes #162

🤖 Generated with Claude Code

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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

  1. 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 sets beforeInflightJoinForTest to 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=1 is set
      • If TestConcurrentCallsDoNotRaceOnTheGateHistogram and TestExtractLLMDoesNotBookASingleFlightFollowersSaving run 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
    • 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.

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 parkingModel implementation is clean and the offloadWithParkedLeader helper 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: TestConcurrentCallsDoNotRaceOnTheGateHistogram and TestExtractLLMDoesNotBookASingleFlightFollowersSaving
  • Verified no other code paths are affected by the changes

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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

  • TestConcurrentIdenticalExtractionsCollapseToOneCall in extract_econ_test.go still uses time.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. beforeInflightJoinForTest is 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. offloadWithParkedLeader waits for all want candidates to reach the hook before releasing the leader. The buffered channel size matches want, so no send blocks. The 10-second timeout prevents a hang if a gate or cap trims the batch.
  • Cleanup is proper. t.Cleanup nils 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 Offload to return via the done channel. The goroutine always completes.
  • Channel handling is correct. release is closed, not sent to, so every blocked reader unblocks. arrived is buffered to want, so every send succeeds without blocking. done is buffered to 1, so the send never blocks.
  • Error paths are safe. The timeout case closes release before failing, so the test goroutine unblocks and the hook cleanup can run safely.
  • The helper signature is right. It returns error from Offload, so the caller can check it. The tests check it and fail if Offload does not fail open.
  • parkingModel is correct. It blocks in Complete until release is closed. It counts calls with atomic.AddInt64, which is safe for concurrent use. The reply can be empty (for gate-only tests) or a real program (for saving tests).
  • summarizingProgram split is clean. The function is now shared between summarizingModel and parkingModel. 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 Offload call. 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 if check is a single pointer comparison, which is negligible. The hook does not change what Offload does or returns.
  • Fail-open is preserved. The tests still check that Offload fails 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 betweenTakeAndStartForTest precedent. The helper uses t.Helper() and t.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]>
@amiddavid

Copy link
Copy Markdown
Collaborator Author

Reviewer 1, finding 1 (hook has no synchronization for parallel tests) — rejected, with
a hardening commit.
The premise does not hold:

  • go test runs the tests of one package one at a time unless a test calls
    t.Parallel(). The -p flag bounds parallel packages, and each package runs in
    its own process, so a package-level variable cannot be shared across them either.
  • No test in components/offload calls t.Parallel(). The only occurrence of the string
    is a comment (summarize_anygate_test.go:158) that warns about the same hazard for its
    own package-global single-flight registry.
  • The repo already ships this exact pattern without a lock:
    betweenTakeAndStartForTest in cache_aware_cold.go, set by a test and nil'd in
    t.Cleanup (cache_aware_after_stash_test.go:220).
  • A mutex would not fix the scenario as described anyway: the hazard in the finding is two
    tests installing different hooks, which is a sequencing constraint, not a data race.

So the two tests cannot run at the same time today. What the finding usefully exposed is
that the constraint was implicit. Commit c968123 writes it on the helper: the tests that
use it must stay sequential, mirroring the note summarize_anygate_test.go already
carries. A future t.Parallel() now has a comment to collide with.

Checked on the eval box after the commit: gofmt -l and go vet clean, and both tests
pass -count=10 -cpu 1,4.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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 test runs package tests sequentially unless a test calls t.Parallel(). No test in this package does.
  • The -p flag controls parallel packages, not parallel tests within one package. Each package runs in its own process.
  • The pattern matches betweenTakeAndStartForTest in cache_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 betweenTakeAndStartForTest and summarize_anygate_test.go as precedents
  • Confirmed no test in components/offload calls t.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 test executes 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 amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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 test runs tests in a package sequentially unless a test calls t.Parallel(). The -p flag controls parallel packages, not parallel tests within a package.
  • No test in components/offload calls t.Parallel().
  • The repo already uses this pattern: betweenTakeAndStartForTest in cache_aware_cold.go (set in cache_aware_after_stash_test.go:220) follows the same pattern without a lock.
  • summarize_anygate_test.go:158 documents 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 betweenTakeAndStartForTest as the existing precedent and summarize_anygate_test.go as 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.go documenting 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
  • Verified the author's claims:
    • Checked that betweenTakeAndStartForTest exists and follows the same pattern (cache_aware_after_stash_test.go:220)
    • Checked that summarize_anygate_test.go:158 documents the same hazard
    • Confirmed no test in components/offload calls t.Parallel()
    • Verified Go's test execution model: tests in a package run sequentially by default
  • 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 amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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:130 and summarize_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 future t.Parallel() collides with it.
  • The betweenTakeAndStartForTest precedent in cache_aware_cold.go uses 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 closes release and waits for done before t.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.

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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 calls t.Parallel(), so the two hook tests cannot run at the same time.
  • The lock-free package-global hook follows the shipped betweenTakeAndStartForTest precedent.
  • 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..HEAD returned 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 before extractInflight.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.

@amiddavid
amiddavid merged commit ab7868a into main Oct 27, 2026
4 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Oct 27, 2026
amiddavid added a commit that referenced this pull request Oct 27, 2026
…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]>
@amiddavid
amiddavid deleted the fix/glm-flaky-offload branch October 27, 2026 17:52
@amiddavid amiddavid mentioned this pull request Oct 28, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

1 participant