gateway: Claude Desktop's sonnet classifier on a Remote magpie's Claude session goes to that magpie's sonnet, not the session's opus (#2187, SuInk)

A Claude Desktop session on another magpie's Claude model
(office/claude/claude-opus-5-5) sent Claude Code's own background
requests for claude-sonnet-5 (auto mode's safety and notification
classifiers) to the session's opus. The remote lists the model as
office/claude/claude-sonnet-5 with the model claude/claude-sonnet-5, so no
entry had "claude-sonnet-5" as its id or model, unserved() said magpie
didn't serve it, and desktopTurn fell back to the session's pick. The
reporter's 9 such calls wrote 480K tokens of 1-hour cache on opus.

desktopTurn now asks servedBeside first for such a request: the entry of
the session's own provider whose model is "<prefix>/<asked>", preferring
the one beside the session's model (same subscription on that magpie).
Only the picked model's provider is looked at, so a same-named model of
another provider doesn't take it, and Desktop's title (and a title Claude
Code asks for on Desktop's behalf) still follows the session. A Claude
model the session's provider doesn't serve still goes to the session's.
unserved() itself is unchanged, so terminal Claude Code's stand-in and
DesktopTiers keep their meaning; Resolve couldn't serve a bare id of a
nested model anyway, which is why the answer is a catalog id here.

Test: TestClaudeDesktopClassifierOnRemoteMagpie (desktop_remote_classifier_test.go):
the gateway serves as the other magpie's model list over httptest, the
local "work" provider is then removed so only the remote serves Claude; the
session turn goes by the alias Desktop was shown (DesktopID), then two
classifier bodies (Claude Code 2.1.295/2.1.296 bytes from #1539), a
Desktop title, and claude-haiku-4-5 the remote doesn't list.

Broken by hand:
- servedBeside skipped (if false &&): FAIL "classifier: the other magpie
  was asked for "work/claude-opus-5-5", want work/claude-sonnet-5".
- applied to titles too (title rule dropped): FAIL "title: sent
  "work/claude-sonnet-5"".
Restored, it passes.

Run at d692b2548: gofmt -l (clean); go vet ./..., GOOS=linux go vet -tags
nogui ./..., GOOS=windows go vet ./... (clean); go test -tags nogui
-timeout 40m ./... under a sandbox HOME with pinned caches: 50 packages ok; internal/gui's
TestProviderListReadsSettingsOnce failed (23s against its 2s bar, load
~260), which is #2185, filed and being fixed apart; internal/agent and
internal/gateway hit the 40m package timeout at load ~260 with a test
that had just started (no hang in the stacks), and rerun alone with
-timeout 100m both pass (1304s, 1355s). go test -run
'TestClaudeTier|Desktop|Remote' ./internal/gateway passes.
Not tried: the reporter's real Claude Desktop against a real remote
magpie with a Claude subscription (no second machine with one here).
This commit is contained in:
yetone
2026-10-14 22:36:15 +08:00
parent 6087c82b02
commit 429698f969
2 changed files with 184 additions and 1 deletions
+53 -1
View File
@@ -437,7 +437,14 @@ func desktopTurn(asked string, body []byte, byClaudeCode, bySubagent bool) strin
if byClaudeCode {
aside = desktopTitle(body)
}
if unserved(asked) || !tools && aside && desktopAccepts(asked) {
title := !tools && aside && desktopAccepts(asked)
gone := unserved(asked)
if gone && !title {
if m := servedBeside(asked, picked); m != "" {
return m
}
}
if gone || title {
if picked != "" {
return picked
}
@@ -455,6 +462,51 @@ func desktopTurn(asked string, body []byte, byClaudeCode, bySubagent bool) strin
return asked
}
// servedBeside is the catalog id the session's provider serves a Claude
// model by that Claude Code asked for by Claude's own id, which no entry
// has as its id or model: another magpie (a Remote magpie provider) lists
// it under its own provider's prefix, office/claude/claude-sonnet-5 with
// the model claude/claude-sonnet-5. Its auto mode's classifier asks for
// its sonnet by name while the session is on office/claude/claude-opus-5-5,
// and went to that opus, writing an hour's cache for each tiny reply
// (#2187, SuInk). Only the provider picked is looked at, so another
// provider's model of the same name doesn't take it; of that provider's,
// the one beside the picked model (the same subscription there) comes
// first. "" when the session's provider serves no such model.
func servedBeside(asked, picked string) string {
bare := strings.TrimSuffix(asked, "[1m]")
if picked == "" || strings.Contains(bare, "/") || claudeTier(bare) == "" {
return ""
}
cat := provider.Catalog()
var pid, dir string
for _, e := range cat {
if e.ID == strings.TrimSuffix(picked, "[1m]") && e.Group == "" {
pid = e.Provider.ID
if i := strings.LastIndex(e.Model, "/"); i >= 0 {
dir = e.Model[:i+1]
}
break
}
}
if pid == "" {
return ""
}
found := ""
for _, e := range cat {
if e.Group != "" || e.Provider.ID != pid || !strings.HasSuffix(e.Model, "/"+bare) {
continue
}
if dir != "" && e.Model == dir+bare {
return e.ID + strings.TrimPrefix(asked, bare)
}
if found == "" {
found = e.ID + strings.TrimPrefix(asked, bare)
}
}
return found
}
// desktopDefault is the model a request Desktop sends before any turn has
// named one goes to: a new session's first message is titled before it is
// sent (ARNO), so nothing is picked yet when the title is asked for. It is
@@ -0,0 +1,131 @@
package gateway
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A Claude Desktop session on another magpie's Claude model keeps Claude
// Code's own background requests on the model they name, as served by that
// magpie: its auto mode's classifier asks for claude-sonnet-5 by Claude's
// id, which the Remote magpie lists as office/claude/claude-sonnet-5, and
// went to the session's office/claude/claude-opus-5-5 instead (#2187,
// SuInk: 9 sonnet calls answered by opus, 480K tokens of 1h cache written).
// Desktop's title still follows the session, and a model the remote
// doesn't serve still goes to the session's.
func TestClaudeDesktopClassifierOnRemoteMagpie(t *testing.T) {
reply := sse(
`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m","type":"message","role":"assistant","model":"m1","content":[],"usage":{"input_tokens":1,"output_tokens":0}}}`,
`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)
f := &fake{reply: reply}
up := setup(t, provider.Anthropic, f)
// the other computer's Claude model, under a provider "work": its
// subscription is "claude" there, which a test can't sign in to
if err := provider.Save(provider.Provider{ID: "work", Name: "Work", Key: "k", Anthropic: up.URL,
Models: []string{"claude-opus-5-5", "claude-sonnet-5", "claude-haiku-5-5"}}); err != nil {
t.Fatal(err)
}
h := New().Handler()
var mu sync.Mutex
var hops []string // the models the other magpie was asked for
remote := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
h.ServeHTTP(w, r) // its model list, as that magpie gives it
return
}
b, _ := io.ReadAll(r.Body)
mu.Lock()
hops = append(hops, gjsonModel(b))
mu.Unlock()
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, reply)
}))
t.Cleanup(remote.Close)
id, err := provider.Add(provider.Provider{ID: "office", Name: "Office", Preset: provider.RemoteMagpiePreset, Chat: strings.TrimPrefix(remote.URL, "http://")})
if err != nil {
t.Fatal(err)
}
office, err := provider.Find(id)
if err != nil {
t.Fatal(err)
}
if _, err := office.Fetch(context.Background()); err != nil {
t.Fatal(err)
}
// here, only the other magpie serves Claude
if err := provider.Delete("work"); err != nil {
t.Fatal(err)
}
if e, ok := entryOf("office/work/claude-sonnet-5"); !ok || e.Model != "work/claude-sonnet-5" {
t.Fatalf("office's sonnet: %+v %v", e, ok)
}
if !unserved("claude-sonnet-5") {
t.Fatal("claude-sonnet-5 is served here by its bare id; the test needs it not to be")
}
desktopPicked.Lock()
desktopPicked.model, desktopPicked.from = "", ""
desktopPicked.Unlock()
const cli = "claude-cli/2.1.295 (external, claude-desktop)"
const electron = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Claude/1.6608.0 Chrome/142.0.7444.235 Electron/39.2.7 Safari/537.36"
send := func(ua, body string) string {
t.Helper()
mu.Lock()
hops = nil
mu.Unlock()
req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body))
req.Header.Set("x-api-key", Token+"-claude-desktop")
req.Header.Set("User-Agent", ua)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
mu.Lock()
defer mu.Unlock()
if len(hops) != 1 {
t.Fatalf("the other magpie was asked %v", hops)
}
return hops[0]
}
// the session is on the other magpie's opus, by the id Desktop was
// shown it by
opus, _ := entryOf("office/work/claude-opus-5-5")
if got := send(cli, `{"model":"`+DesktopID(opus)+`","max_tokens":64000,"stream":true,"tools":[{"name":"Bash","input_schema":{"type":"object"}}],"messages":[{"role":"user","content":"hi"}]}`); got != "work/claude-opus-5-5" {
t.Fatalf("turn: sent %q", got)
}
// Claude Code's safety and notification classifiers name their sonnet
for _, body := range []string{
`{"model":"claude-sonnet-5","max_tokens":64,"stream":true,"system":[{"type":"text","text":"x-anthropic-billing-header: cc_version=2.1.295; cc_entrypoint=claude-desktop;"},{"type":"text","text":"You are a security monitor for autonomous AI coding agents."}],"messages":[{"role":"user","content":"<transcript>…</transcript>"}]}`,
`{"model":"claude-sonnet-5","max_tokens":1024,"stream":true,"system":[{"type":"text","text":"A user kicked off a Claude Code agent to do a coding task and walked away."}],"messages":[{"role":"user","content":"…"}]}`,
} {
if got := send(cli, body); got != "work/claude-sonnet-5" {
t.Fatalf("classifier: the other magpie was asked for %q, want work/claude-sonnet-5", got)
}
}
// Desktop's title follows the session
title := `{"model":"claude-sonnet-5","max_tokens":200,"stream":true,"system":"You write short session titles. Reply with only the tagged fields the prompt asks for.","messages":[{"role":"user","content":"hi"}]}`
if got := send(electron, title); got != "work/claude-opus-5-5" {
t.Fatalf("title: sent %q", got)
}
// a Claude model the other magpie doesn't serve goes to the session's
if got := send(cli, `{"model":"claude-haiku-4-5","max_tokens":64,"stream":true,"messages":[{"role":"user","content":"hi"}]}`); got != "work/claude-opus-5-5" {
t.Fatalf("unserved: sent %q", got)
}
}
func entryOf(id string) (provider.Entry, bool) {
for _, e := range provider.Catalog() {
if e.ID == id {
return e, true
}
}
return provider.Entry{}, false
}