Files
magpie/internal/gui/groups_classifier_test.go
T
yetone c46829e0b4 routing: a group's card says its classifier's failure only when that group asked it
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.
2026-10-29 23:36:58 +08:00

82 lines
2.6 KiB
Go

package gui
import (
"encoding/json"
"os"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// #1929 (versun): a group whose classifier couldn't say the last time it
// was asked says why on the Routing page's card, rather than only in each
// request's details while its rules with an intent quietly never match. A
// group whose classifier answered, or that has none, says nothing.
func TestGroupsSayAClassifierThatFailed(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
for _, id := range []string{"a", "b"} {
if err := provider.Save(provider.Provider{ID: id, Name: id, Key: "k" + id, Chat: "http://127.0.0.1:1/v1", Models: []string{"m"}}); err != nil {
t.Fatal(err)
}
}
// MLX-Serve's own words, its decision model asked as a chat model
const why = `b/m: This is a typed-decision model; it cannot serve chat/text requests. Use POST /v1/decisions instead.`
at := time.Now().Add(-time.Minute).Truncate(time.Second)
was := classifierFailure
t.Cleanup(func() { classifierFailure = was })
classifierFailure = func(group, model string) (string, time.Time, bool) {
if group == "intent" && model == "b/m" {
return why, at, true
}
return "", time.Time{}, false
}
for _, g := range []provider.Group{
{ID: "intent", Name: "Intent", Members: []string{"a/m", "b/m"}, Classifier: "b/m", Rules: []provider.Rule{{Use: "b/m", Intent: "planning"}}},
{ID: "fine", Name: "Fine", Members: []string{"a/m", "b/m"}, Classifier: "a/m", Rules: []provider.Rule{{Use: "b/m", Intent: "planning"}}},
{ID: "plain", Name: "Plain", Members: []string{"a/m", "b/m"}},
} {
if err := provider.SaveGroup(g); err != nil {
t.Fatal(err)
}
}
b, _ := json.Marshal(groupsState())
var st struct {
Groups []struct {
ID string `json:"id"`
ClassifierFailed *struct {
Error string `json:"error"`
At time.Time `json:"at"`
} `json:"classifierFailed"`
} `json:"groups"`
}
if err := json.Unmarshal(b, &st); err != nil {
t.Fatal(err)
}
seen := 0
for _, g := range st.Groups {
switch g.ID {
case "intent":
seen++
if g.ClassifierFailed == nil || g.ClassifierFailed.Error != why || !g.ClassifierFailed.At.Equal(at) {
t.Fatalf("the failing classifier's group: %+v", g.ClassifierFailed)
}
case "fine", "plain":
seen++
if g.ClassifierFailed != nil {
t.Fatalf("%s: %+v", g.ID, g.ClassifierFailed)
}
}
}
if seen != 3 {
t.Fatalf("groups: %s", b)
}
if strings.Count(string(b), `"classifierFailed"`) != 1 {
t.Fatalf("classifierFailed sent for a group it isn't for: %s", b)
}
}