Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

routing: a group's card says its classifier's failure only when that group asked it

yetonec46829e0b444
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index f165c900e..8eb73ad0f 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -161,6 +161,7 @@ recorded as a prediction failure (`noPrediction`).
- A rule's members go first (`ruleOver`, `applyRule`): the members it names, in order, then those the matching rules after it name (`ruleOrder`), each not resting, then the group's others to fail over to. A member that is a group in the group is one member here: its models keep the order the group's own routing planned them, where ranking each by the group's listed order pinned a rotate group's first member on every request the rule put it first (#2407, `TestRuleNamingARotateGroupKeepsItsTurns`). That holds within the turn too, at the group's rule and at each nested group's (`nestedRules`): the one the turn stays on (affinity) stays first while it is of the rule's member that goes first, and the member's others follow it ahead of the group's others, so a failure fails over within the member the rule chose before going outside it. One the turn was kept on outside that member, which took over when the member failed, isn't held there: once one of the member is out of its rest, the turn's next request goes back to it (`Affinity.Why` `rule`). Before #1946 (XcodeFish) a held rule's order wasn't applied to a kept turn at all: a coding-quality member failing mid-turn failed over to coding-fast, the parent's first, and the turn stayed there. `TestHeldRuleFailsOverWithinItsMember`. Two groups in the group may share a model (the same provider, model and effort): it is asked once a request, where the walk met it first, and `provider.Member.Also` keeps its later places, each with its own path, `At` (its place in the walk) and Fast. A rule naming the later group has the model as that group does (`Member.In`, `membersNamed`), in that group's own order, and the nested rules below it, Fast and the first token's wait go by that group's way down (`homed`, `homeCands`). Before #2361 (davisjiahao) such a rule found none of the shared models, read unready, and the group ahead of it answered. A pause rule naming the later group still leaves a shared model to the group ahead of it. `rule_shared_test.go`.
- A classifier's timeout is global (`Settings.ClassifyTimeout`, JSON `classifyTimeout`, seconds, in [`settings.go`](../../internal/settings/settings.go)). Missing or 0 keeps the 8-second default; saving accepts 1–60 seconds or 0 and rejects negative values or values above 60. `Settings.ClassifyWait` bounds a hand-edited value above 60 to 60 seconds and uses the default for a value below 1. `classifyTimeout()` reads it for each call: both `askChat` in [`classify.go`](../../internal/gateway/classify.go) and `askJev` in [`decide.go`](../../internal/gateway/decide.go) give the classifier that time, including its provider's failover. Settings → Models → Replies → *Intent classifier timeout* (`renderReplies` in [`app.js`](../../internal/gui/assets/app.js)) offers 8, 15, 30 and 60 seconds and shows a custom valid value as its own choice. A change applies to the next classifier call, with no restart; the existing fallback, 30-second cooldown (`classifyRest`) and 10-minute answer cache (`classifyKeep`) still apply.
- A classifier that can't say which kind a turn is (an error, no answer within `classifyTimeout()`, an answer that isn't a number, or resting `classifyRest` after a failure) leaves a turn whose conversation had an intent, still one of this turn's, with that intent (`Classified.Fallback` is `previous`, its `Error` kept), so the turn goes where the turn before went rather than to the group's first member; with no intent before, no rule with an intent matches, as before. The Routing page says the turn kept it (`classWhy`). The turn's decision itself (`turnRules`) is kept in memory only: after a restart a turn under way waits for the next one and the stick holds it meanwhile. `TestIntentClassifierFailsKeepsThePreviousIntent`, `routing-intent-fallback.test.cjs`.
+ - A group's classifier is asked only for that group's own turns: a request whose model is the group (`group/<id>`, its bare name, or a stand-in an agent's config names it by: Claude Code's tiers, Codex's bare name), or a group in it that its rules or order put first (`nestedRules`). A request for another model or group asks it nothing. The Routing page's card says why the classifier couldn't say the last time *that group* asked it (`GroupClassifierFailure`, kept by `classifiedFor` in `ruleFor`): the classifier's own words and time, resting or not, until it answers that group, and nothing once the group's classifier changed. Keyed by the classifier model alone, a group nothing reached showed another group's failure on a classifier they share (jjt on Discord). A classifier on a subscription out of its quota is not asked of the vendor again until the reset: out of quota, its account rests until the quota comes back (`restAfter`: the time the refusal names, else when the account's window renews), and the classifier's call meanwhile gets that refusal back from magpie, at most once each `classifyRest`. `TestClassifierFailureIsTheGroupsOwn`.
- The classifier's requests are magpie's own (#1839). Asked in words, they go out with `RouterAgent`'s User-Agent (`magpie-router/1`), so their records' agent is magpie and `callKind` marks them `routing`; a decision model's (Jev, System One) record is written with `Kind: "routing"` too. They go to the same key or account as any other request and count in its totals, while `usage.Group.Own` (`Record.ByMagpie`: agent `magpie` or one of its `magpie-` User-Agents, with search's and image description's) says how many of an account's, key's or model's calls, tokens and cost were magpie's own. A classifier set to a subscription's model (`modelRef.Sub`, or a group whose first member is one) asks that subscription as every turn begins, twice when it picks both intent and effort in words; the Routing page says so beside it (`classifierSub` in `plugins/routing/client.js`). `TestIntentRoutesATurn`, `TestJevPicksTheIntent`, `TestSummaryCountsMagpiesOwnCallsApart`, `TestByMagpie`, `TestServedRefSaysASubscription`, `own-requests.test.cjs`.
- A rule may name several members (`Rule.More` after `Rule.Use`, `more` in the group's JSON, `use=a,b` typed; monkeyin_92 on X: one rule, several models, not a rule each). They go first in that order, ahead of the later matching rules' members (`ThenUses` leaves out every one the rule names) and the group's others: when the first fails or rests the rule's next takes the request, and the hit says so (`RuleHit.More`; `Instead` is that next one with `Unready` false, where an `Instead` from a later rule comes with `Unready` true). `Uses`, `SetUses`, `MapUses`, `Without` and `Names` in [`grouprule.go`](../../internal/provider/grouprule.go) are how every reader goes through them: `cleanRules` (each must be the group's, each named once, a pause names one), the turn's hold, the tool rounds and compaction (`ruleFor`, the rule's first that takes the summary), `nestedRules` and `ruleLed` (the one of them a nested group's turn stays on), `ruleEffort` (the rule's `useEffort` asked of each), `ruledEntry` (what a group takes is what every one of them takes), a model's rename and removal (a rule goes with its last), `ShareGroup` and its import (one this magpie hasn't is left out of the rule), the CLI's `groups rules` and the TUI. The editor draws a chip per member with a + for the next and an × on each while there is more than one (`rUses`/`setUses` in [`plugins/routing/client.js`](../../internal/gui/assets/plugins/routing/client.js)). A rule of one member writes no `more`, so a group saved before it reads and saves as it was. A magpie from before it reads `use` alone: its requests go to the first, and the others are plain members in the group's order; if it saves the group, `more` is dropped. `TestRuleSeveralModelsInOrder`, `TestCleanRulesSeveralModels`, `routing-rule-models.test.cjs`.
- The editor's rules are checked top first, so their order is theirs to set: a rule's number is its handle (`ruleHandle`, `.rt-rhandle`), dragged (`dragRows`, as the members are) or moved a place with Alt+↑/↓, the rules renumbered at once; the grip beside it shows on hover only, and Move up and down stay under its ⋯ (`moveRule`). Saved as the group's `rules` in their new order. `routing-rule-drag.test.cjs`.
diff --git a/internal/gateway/classify.go b/internal/gateway/classify.go
index 95c2e9763..190782585 100644
--- a/internal/gateway/classify.go
+++ b/internal/gateway/classify.go
@@ -81,15 +81,25 @@ var classified = struct {
// last: classifier model → why it couldn't say when last asked, an
// answer that wasn't one among them; gone once it answers
// (ClassifierFailure, #1929)
- last map[string]classifyFailure
+ last map[string]classifyFailure
+ // group: routing group id → why its classifier couldn't say when that
+ // group last asked it; gone once it answers that group
+ // (GroupClassifierFailure)
+ group map[string]groupFailure
inflight map[string]*classifyCall
}{
m: map[string]classifiedAs{},
failed: map[string]classifyFailure{},
last: map[string]classifyFailure{},
+ group: map[string]groupFailure{},
inflight: map[string]*classifyCall{},
}
+type groupFailure struct {
+ classifyFailure
+ model string
+}
+
type classifyCall struct {
done chan struct{}
v verdict
@@ -174,6 +184,42 @@ func classify(ask classifier, model string, intents []string, prev before, effor
return v, false, nil
}
+// classifiedFor notes what group's classifier, model, gave the group's
+// turn: err, or an answer. A failure is the one the classifier last had
+// (classified.last), resting from it or not, so the group's card says the
+// vendor's words and when they were said, not the rest's.
+func classifiedFor(group, model string, err error) {
+ classified.Lock()
+ defer classified.Unlock()
+ if err == nil {
+ delete(classified.group, group)
+ return
+ }
+ f, ok := classified.last[model]
+ if !ok {
+ f = classifyFailure{err: err.Error(), at: time.Now()}
+ }
+ classified.group[group] = groupFailure{classifyFailure: f, model: model}
+}
+
+// GroupClassifierFailure is why group's classifier, model, couldn't say
+// which intent or effort a turn of that group's was, the last time the
+// group asked it, and when; ok is false once it answers the group, before
+// the group asks it, and after the group's classifier changed. The Routing
+// page says it on that group's card alone: keyed by the classifier, a
+// group no request reached showed the failures of another group that
+// shares its classifier, as though its own turns were being judged (jjt on
+// Discord: "claude难易", not in use, said Claude Haiku's weekly limit).
+func GroupClassifierFailure(group, model string) (why string, at time.Time, ok bool) {
+ classified.Lock()
+ defer classified.Unlock()
+ f, ok := classified.group[group]
+ if !ok || f.model != model {
+ return "", time.Time{}, false
+ }
+ return f.err, f.at, true
+}
+
// ClassifierFailure is why model, a routing group's classifier, couldn't
// say which intent or effort a turn is the last time it was asked, and
// when; ok is false once it answers, and before it is asked. The Routing
diff --git a/internal/gateway/classify_concurrency_test.go b/internal/gateway/classify_concurrency_test.go
index 45822e2c6..97b688f9e 100644
--- a/internal/gateway/classify_concurrency_test.go
+++ b/internal/gateway/classify_concurrency_test.go
@@ -23,6 +23,7 @@ func resetClassified(t *testing.T) {
clear(classified.m)
clear(classified.failed)
clear(classified.last)
+ clear(classified.group)
clear(classified.inflight)
}
reset()
diff --git a/internal/gateway/classify_failure_test.go b/internal/gateway/classify_failure_test.go
index e741c6573..de31c745a 100644
--- a/internal/gateway/classify_failure_test.go
+++ b/internal/gateway/classify_failure_test.go
@@ -48,3 +48,54 @@ func TestClassifierFailureIsKept(t *testing.T) {
t.Fatalf("answered: %q %+v", why, r.Rule)
}
}
+
+// A group says its classifier's failure only when that group's own turn
+// asked it (jjt on Discord): "claude难易", which no request reached, said
+// "the classifier failed 3 minutes ago: claude/claude-haiku-5-5: You've hit
+// your weekly limit", read as its classifier judging every turn, while the
+// failure was another group's that has the same classifier. A request for
+// another model asks no group's classifier at all.
+func TestClassifierFailureIsTheGroupsOwn(t *testing.T) {
+ resetClassified(t)
+ s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "refactoring"})
+ if err := provider.SaveGroup(provider.Group{ID: "idle", Name: "Idle", Members: []string{"a/small", "b/big"}, Routing: provider.Ordered,
+ Classifier: "c/cls", Rules: []provider.Rule{{Use: "b/big", Intent: "refactoring"}}}); err != nil {
+ t.Fatal(err)
+ }
+ c.set("", 400)
+ // a model of the idle group's, asked for itself: no classifier
+ postOK(t, s, "own1", `{"model":"a/small","messages":[{"role":"user","content":"rename this package"}]}`)
+ if n := c.n(); n != 0 {
+ t.Fatalf("a request for a/small asked a classifier %d times", n)
+ }
+ _, r := postOK(t, s, "own2", chat("rename this package", nil, 0, ""))
+ if r.Rule == nil || r.Rule.Classified == nil || r.Rule.Classified.Error == "" {
+ t.Fatalf("group/r's turn: %+v", r.Rule)
+ }
+ if why, _, ok := GroupClassifierFailure("r", "c/cls"); !ok || !strings.Contains(why, "classifier down") {
+ t.Fatalf("group/r, whose turn it failed: %q %v", why, ok)
+ }
+ if why, at, ok := GroupClassifierFailure("idle", "c/cls"); ok {
+ t.Fatalf("idle, which nothing asked: %q at %v", why, at)
+ }
+ // group/r's classifier changed since: what the old one said is not the new one's
+ if _, _, ok := GroupClassifierFailure("r", "c/other"); ok {
+ t.Fatal("a failure said for a classifier the group no longer has")
+ }
+ // resting, it is still why group/r's next turn went unjudged, in the
+ // vendor's words and at the time it failed, not the rest's
+ _, at0, _ := GroupClassifierFailure("r", "c/cls")
+ postOK(t, s, "own3", chat("rename that package", nil, 0, ""))
+ if why, at, ok := GroupClassifierFailure("r", "c/cls"); !ok || !strings.Contains(why, "classifier down") || !at.Equal(at0) {
+ t.Fatalf("resting: %q %v %v", why, at, ok)
+ }
+ // it answers group/r: nothing to say
+ classified.Lock()
+ delete(classified.failed, "c/cls")
+ classified.Unlock()
+ c.set("1", 0)
+ postOK(t, s, "own4", chat("rename everything", nil, 0, ""))
+ if why, _, ok := GroupClassifierFailure("r", "c/cls"); ok {
+ t.Fatalf("answered: %q", why)
+ }
+}
diff --git a/internal/gateway/rules.go b/internal/gateway/rules.go
index f91f05845..efe58f524 100644
--- a/internal/gateway/rules.go
+++ b/internal/gateway/rules.go
@@ -312,6 +312,7 @@ func ruleFor(key string, g provider.Group, ms []provider.Member, req *Request, a
default:
t0 := time.Now()
v, cached, err := classify(ask, g.Classifier, intents, prev, effort, text)
+ classifiedFor(g.ID, g.Classifier, err)
c.Intent, c.Cached, c.Ms = v.Intent, cached, int(time.Since(t0).Milliseconds())
if effort {
c.Effort, c.Score = v.Effort, v.Score
diff --git a/internal/gui/groups.go b/internal/gui/groups.go
index 46f6dde69..0d162f732 100644
--- a/internal/gui/groups.go
+++ b/internal/gui/groups.go
@@ -66,9 +66,9 @@ type groupJSON struct {
// Ref: the id agents pick it by (provider.Group.Ref): auto/<model> for
// a group magpie found, group/<id> for any other
Ref string `json:"ref"`
- // ClassifierFailed: its classifier couldn't say the last time it was
- // asked, and why (gateway.ClassifierFailure), which its card says
- // (#1929): otherwise only each request's details did
+ // ClassifierFailed: its classifier couldn't say the last time this
+ // group asked it, and why (gateway.GroupClassifierFailure), which its
+ // card says (#1929): otherwise only each request's details did
ClassifierFailed *classifierFailedJSON `json:"classifierFailed,omitempty"`
}
@@ -77,8 +77,9 @@ type classifierFailedJSON struct {
At time.Time `json:"at"`
}
-// classifierFailure is gateway.ClassifierFailure, which a test stands in for
-var classifierFailure = gateway.ClassifierFailure
+// classifierFailure is gateway.GroupClassifierFailure, which a test stands
+// in for
+var classifierFailure = gateway.GroupClassifierFailure
type memberJSON struct {
ID string `json:"id"`
@@ -276,7 +277,7 @@ func groupsState() groupsJSON {
gj.Decides = g.Decides()
gj.Ref = g.Ref()
if g.Classifier != "" {
- if why, at, ok := classifierFailure(g.Classifier); ok {
+ if why, at, ok := classifierFailure(g.ID, g.Classifier); ok {
gj.ClassifierFailed = &classifierFailedJSON{Error: why, At: at}
}
}
diff --git a/internal/gui/groups_classifier_test.go b/internal/gui/groups_classifier_test.go
index b067e78b8..3324526d3 100644
--- a/internal/gui/groups_classifier_test.go
+++ b/internal/gui/groups_classifier_test.go
@@ -29,8 +29,8 @@ func TestGroupsSayAClassifierThatFailed(t *testing.T) {
at := time.Now().Add(-time.Minute).Truncate(time.Second)
was := classifierFailure
t.Cleanup(func() { classifierFailure = was })
- classifierFailure = func(model string) (string, time.Time, bool) {
- if model == "b/m" {
+ classifierFailure = func(group, model string) (string, time.Time, bool) {
+ if group == "intent" && model == "b/m" {
return why, at, true
}
return "", time.Time{}, false