cybercaius picked AgentRouter's gpt-6.1-sol in Claude Desktop, and the Routing page showed Claude Code's background calls in the same chat (3-9k tokens) going to leee/claude-haiku-5-5: "为什么Claude Desktop中模型 选择的是AgentRouter · gpt-6.1-sol,但在对话过程中还是会去请求leee供应商的 模型呢?" Cause: magpie's own default, not a pick of theirs. desktopModels tagged every Claude model magpie serves with its own tier in the /v1/models it gives Desktop (anthropic_family_tier,f55747ce8) whenever that tier had no pick. Desktop's Code tab hands a tagged model to Claude Code as ANTHROPIC_DEFAULT_HAIKU_MODEL, its small, fast model, so every haiku call (summaries, bash prefix checks and the like) asked for Leee's haiku by its catalog id and was served there, whatever the chat was on. Now: - Only a model the user picked for a tier (claude-desktop.tiers.json, the Agents page's opus/sonnet/haiku/fable) is tagged. Untagged, Claude Code asks for its own id of the tier (claude-haiku-4-5-…). - desktopTurn: a background call (no tools) or a subagent's turn for a Claude model by Claude's own id that is no catalog id (claudeOwnID) stays on the chat's provider (desktopPicked) even when another provider serves a model of that name; before, only an unserved one did. A lead's turn and a tier pick (a catalog id) go as asked. - servedBeside: the chat's provider's model of that name (also one listed by the bare name), else its newest model of the same tier, beside the chat's model first, else "" and the call goes to the chat's model. A chat on GPT gets its haiku calls on that GPT; a chat on a provider with haikus keeps them on a haiku there. - The tier picker's "Not set" note says so in en/zh/zh-TW/ja/de ("the chat's provider's model of the tier, else the chat's model"). - gateway-routing.md describes the rule. A value the user set is untouched: a tier pick stays tagged and served as picked (asserted in the new test). Tests: TestClaudeDesktopBackgroundStaysOnChatProvider (new): AgentRouter gpt-6.1-sol + Leee claude-haiku-5-5 + a third provider with two haikus; asserts nothing tagged unpicked, Claude Code's haiku calls (bare claude-haiku-5-5 served elsewhere, an unserved dated haiku, a subagent) go to agentrouter/gpt-6.1-sol, a chat on the third provider gets its haikus, and a picked haiku is tagged and kept. Red on5143bf593(old code, test file only): "auto/claude-haiku-5-5 tagged "haiku" with nothing picked". TestClaudeDesktopTiers expects no tag unpicked; TestClaudeDesktopClassifierOnRemoteMagpie's unserved claude-haiku-4-5 now goes to the remote's claude-haiku-5-5 (same provider, same tier), and an unserved fable still to the session's opus. Broken by hand (mote, new test): - elsewhere forced false: FAIL "chat on AgentRouter, claude-haiku-5-5 (subagent false) sent to "claude-haiku-5-5"" (and the subagent and Claudeish cases). - servedBeside's tier stand-in skipped: FAIL "chat on Claudeish, claude-haiku-4-5-20251001 sent to "claudeish/claude-opus-5-5"". Restored, it passes. The first run of this change also turned the old remote test's expectation red ("unserved: sent "work/claude-haiku-5-5""), the tier stand-in at work; changed on purpose, see above. Run (mote, this change on5143bf593): go test ./internal/gateway ./internal/agent/... -count=1 -timeout 45m: gateway ok (889s), agent ok (469s). Rebased onto origin/main after the run; the commits since touch no file here. GUI (wsl-run, magpie-desktop): desktop-tiers.test.cjs chromium+webkit, en+zh, 4/4 ok; i18n-keys.test.cjs 8/8 ok. gofmt -l clean. Not tried: a real Claude Desktop. Desktop reads /v1/models at start-up, so the change reaches it after Desktop is restarted.
137 lines
5.8 KiB
Go
137 lines
5.8 KiB
Go
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 its model of
|
|
// the same tier (cybercaius: a haiku call stays a haiku call on the
|
|
// session's provider), and one of a tier it has none of 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-haiku-5-5" {
|
|
t.Fatalf("unserved haiku: sent %q", got)
|
|
}
|
|
if got := send(cli, `{"model":"claude-fable-5-1","max_tokens":64,"stream":true,"messages":[{"role":"user","content":"hi"}]}`); got != "work/claude-opus-5-5" {
|
|
t.Fatalf("unserved fable: 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
|
|
}
|