diff --git a/internal/gateway/desktop_probe_test.go b/internal/gateway/desktop_probe_test.go index 36c2624b6..e31c885c4 100644 --- a/internal/gateway/desktop_probe_test.go +++ b/internal/gateway/desktop_probe_test.go @@ -185,3 +185,49 @@ func TestOwnCacheKey(t *testing.T) { } } } + +// Claude Desktop's check through a routing group with intent rules is +// answered by the group's order, not after its classifier: the check is +// no user's turn, and Desktop gives it 10 seconds as it starts. Asked of +// the classifier, the first check after a start waited up to the +// Classifier timeout (8s) on a ".", and Desktop cancelled it as a 499; +// the second passed on the verdict kept (CyberCaius on Discord, +// v0.1.1847). A user's turn from Desktop still asks it. +func TestClaudeDesktopProbeAsksNoClassifier(t *testing.T) { + resetClassified(t) + s, a, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "writing or fixing tests"}) + c.set("1", 0) + if err := provider.SetModelOrder("claude-desktop", []string{"group/r"}); err != nil { + t.Fatal(err) + } + desktopPicked.Lock() + desktopPicked.model, desktopPicked.from = "", "" + desktopPicked.Unlock() + send := func(body string) (int, string) { + t.Helper() + req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)) + req.Header.Set("x-api-key", Token+"-claude-desktop") + req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh) Claude/2.7032.0 Chrome/140.0 Electron/38.0") + rec := httptest.NewRecorder() + s.Handler().ServeHTTP(rec, req) + return rec.Code, rec.Body.String() + } + const probe = `{"model":"magpie-claude-haiku-5-5","max_tokens":1,"messages":[{"role":"user","content":"."}]}` + if code, body := send(probe); code != 200 { + t.Fatalf("probe: %d %s", code, body) + } + if n := c.n(); n != 0 { + t.Fatalf("the check asked the classifier %d times: %s", n, c.last()) + } + r := lastRoute(s) + if a.last == "" || r.Rule == nil || r.Rule.Classified == nil || !strings.Contains(r.Rule.Classified.Error, "Claude Desktop's connection check") { + t.Fatalf("not answered by the group's first member, or the trace doesn't say why: %q %+v", a.last, r.Rule) + } + // a user's turn through the same group is still classified + if code, body := send(`{"model":"group/r","max_tokens":32000,"stream":true,"messages":[{"role":"user","content":"add a unit test for the parser"}]}`); code != 200 { + t.Fatalf("turn: %d %s", code, body) + } + if c.n() != 1 { + t.Fatalf("a user's turn asked the classifier %d times", c.n()) + } +} diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 70ce3d1a4..229cbf02e 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -1806,9 +1806,11 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // the group's, as "group/" is, rather than one provider's that // serves it: the Routing view shows the group it went to asked = call.Model + desktopCheck := false // Claude Desktop's check that the gateway answers if agent == "claude-desktop" { sub, _ := subagentOf(r.Header) probe := desktopProbe(body) + desktopCheck = probe asked = desktopTurn(asked, body, fromClaudeCode(r.Header), sub != "") if probe { // its check, asked for room every model answers in @@ -2058,8 +2060,17 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro // a classifier's own call to a group (a group's classifier may be one) // asks no classifier in turn: one that is, however far round, the group // asking would ask itself for ever + // + // Nor does Claude Desktop's check (desktopProbe): it is no user's turn, + // and it has 10 seconds to be answered as Desktop starts (8 from Test + // connection). Asked of a group with intent rules or Effort auto, its + // "." waited on the classifier first, up to the Classifier timeout's 8 + // seconds, so the first check after a start was cancelled as a 499 + // and the second, its verdict kept or the classifier resting, passed + // (CyberCaius on Discord, v0.1.1847). It goes by the group's order, + // as a model's check does. ask := s.askClassifier - if r.Header.Get("User-Agent") == RouterAgent { + if r.Header.Get("User-Agent") == RouterAgent || desktopCheck { ask = nil } else { // a wait magpie knows of, before a route is begun (unrouted.go) diff --git a/internal/gateway/rules.go b/internal/gateway/rules.go index df2465704..2352958a6 100644 --- a/internal/gateway/rules.go +++ b/internal/gateway/rules.go @@ -306,7 +306,7 @@ func ruleFor(key string, g provider.Group, ms []provider.Member, req *Request, a case g.Classifier == "": c.Error = "the group has no classifier" case ask == nil: - c.Error = "nothing to ask the classifier with: this is a classifier's own call" + c.Error = "nothing to ask the classifier with: this is a classifier's own call, or Claude Desktop's connection check" case text == "": c.Error = "the message has no words to classify" default: