magpie icon indicating copy to clipboard operation
magpie copied to clipboard

TestIntentClassifierFails: the slow step's 1.5s bound on the whole turn fails under load (1.58s, answer right)

Open yetone opened this issue 1 month ago • 1 comments

internal/gateway TestIntentClassifierFails failed under load on macOS. It was a full go test -tags nogui -p 2 ./internal/agent ./internal/gateway ./internal/library ./internal/provider, load average about 115, on the #1809 worktree based on 94aa15397. The #1809 change doesn't touch the classifier.

--- FAIL: TestIntentClassifierFails (5.87s)
    classify_test.go:263: slow: {"id":"x","choices":[{"index":0,"message":{"role":"assistant","content":"from ka"},...}]} &{N:0 ... Classified:0x708b4188bb80 ...} 1.578789208s
  • The turn behaved right: the answer came "from ka".
  • It took 1.58s. The slow step bounds the whole turn at time.Since(t0) > 1500*time.Millisecond, with classifyTimeout set to 200ms and a stub that waits 2s.
  • That bound covers the whole POST, not just the classifier wait, so on a busy machine it has about 1.3s of room for everything else.
  • Run alone, the test passes 3 of 3 (-count=3, load about 22).
  • The %+v prints Classified as a pointer, so the log can't show whether the Error said "no answer". Printing *r.Rule.Classified would.

Suggested fix, following "a timing test leaves room on both sides":

  • Check that the stub's 2s wait wasn't waited out: elapsed well under 2s, or the stub records that it was cancelled.
  • Don't bound the whole turn tightly.

yetone avatar Oct 12 '26 15:10 yetone

Took this one (state was OPEN with no claim; git log --grep=1829 shows no fix on main as of b1d02fa6). Since the repo doesn't take pull requests, the fix is on a branch of my fork for cherry-picking — one commit (aeaf6703) covers this, #1813, #1812 and #1791:

git fetch https://github.com/Niko-Wu/magpie fix/timing && git cherry-pick aeaf6703

(based on current b1d02fa6, single commit, test files only)

Root cause — the test judged the turn by a wall-clock bound, not by the state it is about:

  • internal/gateway/classify_test.go:279-282 (old) bounded the whole slow-classifier POST at time.Since(t0) > 1500*time.Millisecond, with classifyTimeout = 200ms and a stub that waits 2s. That bound covered the whole turn, so on a busy machine it had about 1.3s of room for everything else — and it spent 1.58s with the answer right (your report).

The fix (the bound's point was that the turn gives up on the slow classifier rather than waiting its 2s stub out — that is state, so it is now checked as state):

  • clsUp.ServeHTTP (classify_test.go:37-45) counts the asks whose caller cancelled (r.Context().Done() fired while it was waiting) into a new cancelled counter, read through gaveUp().
  • The f4 step (classify_test.go:284-302) keeps the "no answer" check and now waits for the cancellation count to rise (within, the package's state poll) and checks it is exactly 1. A turn that really waited the stub out leaves the count at 0 and still goes red — the timeout firing is what cancels the ask, so no cancel means the turn didn't give up.
  • The 1.5s wall-clock bound is gone, and the failure print expands *r.Rule.Classified instead of the pointer, as you noted.

Red → green

  • On origin/main 707fbb24, injecting 1.6s into ruleUp.ServeHTTP (the turn's answer path, standing in for the loaded machine) reproduces your red exactly: slow: at classify_test.go:282, turn past 1.5s. With the fix, the same injected load passes (and passed -count=3 even with a 3.4s injection into every turn).
  • Broken by hand: if the turn didn't give up (no cancellation), the count stays 0 and the test fails on within.

Verification

  • The four changed tests, go test -tags nogui -count=3: internal/gateway 21 passed (classifier + TestMaxRPM set).
  • Full packages go test -tags nogui: internal/gateway 4192 passed on the final tree.
  • gofmt -l clean; go vet -tags nogui clean for GOOS=linux and GOOS=windows.
  • Not run: -race; CI's machines.

Niko-Wu avatar Oct 15 '26 03:10 Niko-Wu