jjt on Discord: the Routing card of "claude难易" (group/claude-ny), which
no turn of theirs was sent to, said "分类器3分钟前失败:claude/claude-haiku-5-5:
Claude Code: You've hit your weekly limit", read as magpie asking its
classifier at every turn. The card read the failure by classifier model
(ClassifierFailure), so every group sharing a classifier showed the
failure of whichever group had last asked it.
What runs the classifier was not changed: ruleFor asks it only for a
request whose model is the group (group/<id>, its name, or a stand-in an
agent's config names it by: Claude Code's tiers, Codex's bare name), or
for a group in it its rules or order put first (nestedRules). A request
for another model or group asks nothing. Out of quota, the classifier's
account rests until the reset (restAfter), so later classifier calls get
magpie's held refusal back, at most once each classifyRest (30s), and
do not reach the vendor.
Now ruleFor notes the outcome per group (classifiedFor) and the card
reads GroupClassifierFailure(group, classifier): the classifier's own
words and time (resting or not) from that group's last ask, cleared when
it answers that group, and nothing once the group's classifier changed.
Run (mote, macOS 27, Go 1.26.8), at base 4586307de:
- gofmt -l internal: clean; go vet darwin, GOOS=linux -tags nogui,
GOOS=windows for ./internal/gateway ./internal/gui: ok
- go test -count=1 -timeout 45m ./internal/gateway ./internal/gui:
two reds, neither in this change. TestUnroutedHeldReportSaved passed
5 of 5 alone after; filed #4300. TestMainWindowCorners/macOS_26_SDK
is #3949.
- -run 'TestClassifierFailure|TestGroupsSayAClassifier': ok
- node --test internal/gui/tests/group-classifier-failed.test.cjs: 20 of
20 (Chromium and WebKit, both widths, every language)
- Broken by hand: GroupClassifierFailure read by model as before, and
TestClassifierFailureIsTheGroupsOwn went red ("idle, which nothing
asked: c/cls: C: classifier down").
Not tried: a real Claude Code account at its weekly limit.
222 lines
6.3 KiB
Go
222 lines
6.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"testing/synctest"
|
|
"time"
|
|
)
|
|
|
|
type classifyResult struct {
|
|
v verdict
|
|
cached bool
|
|
err error
|
|
}
|
|
|
|
func resetClassified(t *testing.T) {
|
|
t.Helper()
|
|
reset := func() {
|
|
classified.Lock()
|
|
defer classified.Unlock()
|
|
clear(classified.m)
|
|
clear(classified.failed)
|
|
clear(classified.last)
|
|
clear(classified.group)
|
|
clear(classified.inflight)
|
|
}
|
|
reset()
|
|
t.Cleanup(reset)
|
|
}
|
|
|
|
func TestConcurrentClassificationSharesAnswer(t *testing.T) {
|
|
resetClassified(t)
|
|
synctest.Test(t, func(t *testing.T) {
|
|
release := make(chan struct{})
|
|
results := make(chan classifyResult, 17)
|
|
var calls atomic.Int32
|
|
want := verdict{Intent: "refactoring", Effort: "high", Score: 2, Sure: 0.9}
|
|
ask := func(string, []string, before, bool, string) (verdict, error) {
|
|
calls.Add(1)
|
|
<-release
|
|
return want, nil
|
|
}
|
|
run := func() {
|
|
v, cached, err := classify(ask, t.Name(), []string{"refactoring"}, before{}, true, "rename this package")
|
|
results <- classifyResult{v, cached, err}
|
|
}
|
|
go run()
|
|
synctest.Wait()
|
|
for range 16 {
|
|
go run()
|
|
}
|
|
synctest.Wait()
|
|
if n := calls.Load(); n != 1 {
|
|
t.Errorf("concurrent calls = %d, want 1", n)
|
|
}
|
|
close(release)
|
|
synctest.Wait()
|
|
shared := 0
|
|
for range 17 {
|
|
got := <-results
|
|
if got.v != want || got.err != nil {
|
|
t.Fatalf("result = %+v, want %+v", got, want)
|
|
}
|
|
if got.cached {
|
|
shared++
|
|
}
|
|
}
|
|
if shared != 16 {
|
|
t.Fatalf("shared answers = %d, want 16", shared)
|
|
}
|
|
v, cached, err := classify(ask, t.Name(), []string{"refactoring"}, before{}, true, "rename this package")
|
|
if v != want || !cached || err != nil || calls.Load() != 1 {
|
|
t.Fatalf("completed cache = %+v, %v, %v; calls = %d", v, cached, err, calls.Load())
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestConcurrentClassificationDistinctKeys(t *testing.T) {
|
|
resetClassified(t)
|
|
synctest.Test(t, func(t *testing.T) {
|
|
release := make(chan struct{})
|
|
results := make(chan classifyResult, 7)
|
|
var calls atomic.Int32
|
|
want := verdict{Intent: "refactoring"}
|
|
ask := func(string, []string, before, bool, string) (verdict, error) {
|
|
calls.Add(1)
|
|
<-release
|
|
return want, nil
|
|
}
|
|
for _, input := range []struct {
|
|
model string
|
|
intents []string
|
|
prev before
|
|
effort bool
|
|
text string
|
|
}{
|
|
{t.Name(), []string{"refactoring"}, before{}, false, "rename"},
|
|
{t.Name() + "/other", []string{"refactoring"}, before{}, false, "rename"},
|
|
{t.Name(), []string{"testing"}, before{}, false, "rename"},
|
|
{t.Name(), []string{"refactoring"}, before{Intent: "refactoring"}, false, "rename"},
|
|
{t.Name(), []string{"refactoring"}, before{Effort: "high"}, false, "rename"},
|
|
{t.Name(), []string{"refactoring"}, before{}, true, "rename"},
|
|
{t.Name(), []string{"refactoring"}, before{}, false, "test"},
|
|
} {
|
|
go func() {
|
|
v, cached, err := classify(ask, input.model, input.intents, input.prev, input.effort, input.text)
|
|
results <- classifyResult{v, cached, err}
|
|
}()
|
|
}
|
|
synctest.Wait()
|
|
if n := calls.Load(); n != 7 {
|
|
t.Errorf("distinct calls = %d, want 7", n)
|
|
}
|
|
close(release)
|
|
synctest.Wait()
|
|
for range 7 {
|
|
if got := <-results; got.v != want || got.cached || got.err != nil {
|
|
t.Fatalf("distinct result = %+v", got)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestConcurrentClassificationSharesFailure(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
err error
|
|
rests bool
|
|
}{
|
|
{"upstream", errors.New("classifier down"), true},
|
|
{"answer", answerError{errors.New("not a number")}, false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
resetClassified(t)
|
|
synctest.Test(t, func(t *testing.T) {
|
|
release := make(chan struct{})
|
|
results := make(chan classifyResult, 2)
|
|
var calls atomic.Int32
|
|
want := verdict{Intent: "refactoring"}
|
|
ask := func(string, []string, before, bool, string) (verdict, error) {
|
|
n := calls.Add(1)
|
|
<-release
|
|
if n == 1 {
|
|
return want, tc.err
|
|
}
|
|
return want, nil
|
|
}
|
|
run := func() {
|
|
v, cached, err := classify(ask, t.Name(), []string{"refactoring"}, before{}, false, "rename")
|
|
results <- classifyResult{v, cached, err}
|
|
}
|
|
go run()
|
|
synctest.Wait()
|
|
go run()
|
|
synctest.Wait()
|
|
if n := calls.Load(); n != 1 {
|
|
t.Errorf("concurrent calls = %d, want 1", n)
|
|
}
|
|
close(release)
|
|
synctest.Wait()
|
|
for range 2 {
|
|
if got := <-results; got.v != (verdict{}) || got.cached || !errors.Is(got.err, tc.err) {
|
|
t.Fatalf("shared failure = %+v, want %v", got, tc.err)
|
|
}
|
|
}
|
|
if tc.rests {
|
|
_, cached, err := classify(ask, t.Name(), []string{"refactoring"}, before{}, false, "rename")
|
|
if cached || err == nil || !strings.Contains(err.Error(), "not asked again") || calls.Load() != 1 {
|
|
t.Fatalf("cooldown = %v, %v; calls = %d", cached, err, calls.Load())
|
|
}
|
|
time.Sleep(classifyRest + time.Second)
|
|
}
|
|
v, cached, err := classify(ask, t.Name(), []string{"refactoring"}, before{}, false, "rename")
|
|
if v != want || cached || err != nil || calls.Load() != 2 {
|
|
t.Fatalf("retry = %+v, %v, %v; calls = %d", v, cached, err, calls.Load())
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConcurrentClassificationPanicReleasesWaiters(t *testing.T) {
|
|
resetClassified(t)
|
|
synctest.Test(t, func(t *testing.T) {
|
|
release := make(chan struct{})
|
|
panicked := make(chan any, 1)
|
|
result := make(chan classifyResult, 1)
|
|
var calls atomic.Int32
|
|
ask := func(string, []string, before, bool, string) (verdict, error) {
|
|
calls.Add(1)
|
|
<-release
|
|
panic("classifier panic")
|
|
}
|
|
go func() {
|
|
defer func() { panicked <- recover() }()
|
|
classify(ask, t.Name(), nil, before{}, false, "rename")
|
|
}()
|
|
synctest.Wait()
|
|
go func() {
|
|
v, cached, err := classify(ask, t.Name(), nil, before{}, false, "rename")
|
|
result <- classifyResult{v, cached, err}
|
|
}()
|
|
synctest.Wait()
|
|
close(release)
|
|
synctest.Wait()
|
|
if got := <-panicked; got != "classifier panic" {
|
|
t.Fatalf("panic = %v", got)
|
|
}
|
|
if got := <-result; got.cached || got.err == nil || calls.Load() != 1 {
|
|
t.Fatalf("panic waiter = %+v; calls = %d", got, calls.Load())
|
|
}
|
|
want := verdict{Intent: "refactoring"}
|
|
retry := func(string, []string, before, bool, string) (verdict, error) { return want, nil }
|
|
v, cached, err := classify(retry, t.Name(), nil, before{}, false, "rename")
|
|
if v != want || cached || err != nil {
|
|
t.Fatalf("retry after panic = %+v, %v, %v", v, cached, err)
|
|
}
|
|
})
|
|
}
|