gateway tests: TestReasoningClassifierAnswersInTheWaitUnset red on Linux (WSL) under load: 250ms of room between a 500ms answer and a 750ms wait
internal/gateway's TestReasoningClassifierAnswersInTheWaitUnset went red in a full go test -tags nogui -count=1 -timeout 45m ./... at origin/main 35a03fee0, on Linux (WSL magpie-desktop on xiaomi-gpu, 11-19 06:13 to 06:39). Other sessions' GUI and package runs were on the box at the same time.
--- FAIL: TestReasoningClassifierAnswersInTheWaitUnset (2.03s)
classify_test.go:358: a classifier answering in 10s, unset: {..."content":"from ka"...} &{N:0 ... Classified:0x116af87a5a00 ...} &{By:c/cls Intents:[refactoring] ... Ms:1399 Error:c/cls gave no answer in 750ms ...}
The cause is in the test's timing. classifyUnit is cut to 50ms, so the fake classifier answers in 500ms (10 units) and the unset wait is 750ms (15 units). That leaves 250ms of room, and the call took 1399ms. "A timing test leaves room on both sides" (docs/code-standards.md): on a loaded machine, 250ms isn't enough. A fix would let the fake classifier answer only after the test says so, or after the first try is seen, instead of after a fixed 500ms, and would check the "past the whole wait" half against a wait that is clearly past.
Not taken by me. Found while checking a Library change (bdefdc319, 8634c4ad0), which doesn't touch the gateway. Same run: internal/usage's TestRequestStateKeptAcrossACatalogFetchedAgain is #6572, and doneHold's "close of closed channel" panic is #6505/#6690.
A test-only candidate for #7141, based on a18ed4995f8eb0c9b1e60546502ebb38ce5f72a8. Thank you @yetone for the failure and timing analysis.
The two timers do not start together: the gateway starts its deadline before the fake receives the HTTP request, then the fake sleeps another 500 ms. This patch separates the guarantees instead of widening that narrow race:
- Assert the unset, unscaled timeout is exactly 15 seconds, then check a successful answer's routing without an artificial sleep.
- Test cancellation/accounting separately. The fake cannot answer until its request is canceled (or cleanup releases its gate), so it cannot win a race against the gateway timeout.
- Keep the fallback, unique canceled usage record,
magpie stopped waiting/decision model, non-user-cancellation, andFailed()assertions.
Scope: this no longer claims to exercise an actual 10-second delayed success. It checks the exact default policy and successful routing separately. The original usage-log polling bound remains, so this is not a claim that every possible load-related failure is eliminated. No production code or dependencies change.
Verified on Linux, isolated HOME, fake upstream only:
- Final
go test -p 1 -tags nogui ./internal/gateway -run '^(TestReasoningClassifier.*|TestClassifierTimeoutSetting|TestIntentRoutesATurn)$' -count=1: PASS (5.346s). go test -p 1 -race -tags nogui ./internal/gateway -run '^TestReasoningClassifier' -count=20: PASS (19.634s).- Behavior-mutation overlay: returning an 8-second default and replacing the decision timeout cause with
context.Canceledmakes both tests FAIL:unset classifier wait = 8s, want 15s; and499 "the agent canceled the request". Overlay removed for the final passing run. go vet -p 1 -tags nogui ./internal/gateway, gofmt, andgit diff --check: clean.- Independent read-only review checked the gate synchronization and cleanup order.
Not run: the original WSL concurrent full-suite scenario, full repository/package tests, Windows/macOS tests. I have not independently reproduced the original loaded-host failure; the red evidence above checks that the replacement tests still catch policy/accounting regressions, not a claim to have reproduced your WSL run.
Suggested attribution if applied:
Co-authored-by: yetone <[email protected]>
AI-assisted implementation and review.
Complete patch (git apply)
diff --git a/internal/gateway/classify_test.go b/internal/gateway/classify_test.go
index 6840046..6c2d708 100644
--- a/internal/gateway/classify_test.go
+++ b/internal/gateway/classify_test.go
@@ -25,6 +25,7 @@ type clsUp struct {
answer string
fail int
wait time.Duration
+ hold <-chan struct{}
asked []string
agents []string
}
@@ -33,8 +34,15 @@ func (u *clsUp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
u.mu.Lock()
u.asked = append(u.asked, string(b))
- answer, fail, wait := u.answer, u.fail, u.wait
+ answer, fail, wait, hold := u.answer, u.fail, u.wait, u.hold
u.mu.Unlock()
+ if hold != nil {
+ select {
+ case <-hold:
+ case <-r.Context().Done():
+ return
+ }
+ }
if wait > 0 {
select {
case <-time.After(wait):
@@ -342,27 +350,36 @@ func TestClassifierTimeoutSetting(t *testing.T) {
// Asked with a real routing prompt they answered in 4 to 10 seconds, past
// the 8 a classifier was given unset, and every such call was logged 499
// "the agent canceled the request", though no agent had. Unset, a
-// classifier that answers in 10 seconds now routes the turn, and one past
-// the wait is logged as magpie stopping, a failure.
+// classifier has 15 seconds to answer. Check that policy separately from
+// routing an answer: racing two scaled sleeps left only 250ms of slack
+// under load (#7141). Cancellation and its accounting are checked below.
func TestReasoningClassifierAnswersInTheWaitUnset(t *testing.T) {
- was := classifyUnit
- classifyUnit = 50 * time.Millisecond // 10 seconds are 500ms
- t.Cleanup(func() { classifyUnit = was })
s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "refactoring"})
+ if settings.Shared().ClassifyTimeout != 0 || classifyUnit != time.Second {
+ t.Fatal("the classifier test must start with the unset, unscaled wait")
+ }
+ if got := classifyTimeout(); got != 15*time.Second {
+ t.Fatalf("unset classifier wait = %s, want 15s", got)
+ }
c.set("1", 0)
- c.mu.Lock()
- c.wait = 10 * classifyUnit
- c.mu.Unlock()
out, r := postOK(t, s, "rs1", chat("rename the parser package and move its tests", nil, 0, ""))
if cl := r.Rule.Classified; !strings.Contains(out, "from kb") || r.Rule.N != 1 || cl.Intent != "refactoring" || cl.Error != "" {
- t.Fatalf("a classifier answering in 10s, unset: %s %+v %+v", out, r.Rule, cl)
+ t.Fatalf("a classifier answering within the unset wait: %s %+v %+v", out, r.Rule, cl)
}
- // past the whole wait it is no answer, and the call magpie stopped is
- // its failure, not an agent canceling
+}
+
+// The fake cannot answer before cancellation, irrespective of scheduler load.
+func TestReasoningClassifierPastTheWaitIsItsFailure(t *testing.T) {
+ was := classifyUnit
+ classifyUnit = 50 * time.Millisecond
+ t.Cleanup(func() { classifyUnit = was })
+ s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "refactoring"})
+ hold := make(chan struct{})
+ t.Cleanup(func() { close(hold) })
c.mu.Lock()
- c.wait = time.Duration(settings.ClassifyTimeoutMost) * classifyUnit
+ c.hold = hold
c.mu.Unlock()
- out, r = postOK(t, s, "rs2", chat("rename everything under internal", nil, 0, ""))
+ out, r := postOK(t, s, "rs2", chat("rename everything under internal", nil, 0, ""))
if cl := r.Rule.Classified; !strings.Contains(out, "from ka") || !strings.Contains(cl.Error, "no answer") {
t.Fatalf("a classifier past its wait: %s %+v %+v", out, r.Rule, cl)
}
Taking this with yhdhaidong's patch, applied as written (seen again on WSL in a full run today). -race -count=20 ok on mica under load; red with an 8s default. Commit follows.
Fixed in 73f0f395b (v0.1.5448) with @yhdhaidong's patch from this thread, as written, with credit: the unset wait is checked as policy (15s), and a classifier past the wait is checked against a held answer, not a 10-second sleep. -race -count=20 on mica ok; the whole internal/gateway package with it ok (1446s, mica).