Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gateway: Claude Desktop's connection check goes to its default model
yetoneee6f8866f331
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.mdindex 73b39e6ea..494f3bdf9 100644--- a/docs/subsystems/gateway-routing.md+++ b/docs/subsystems/gateway-routing.md@@ -271,7 +271,7 @@ recorded as a prediction failure (`noPrediction`).- Locally, Claude Code's classifier is a request of its own on the session's model: no tools, a system prompt asking for `<block>yes</block>` or `<block>no</block>`, the transcript in `<transcript>`, a small `max_tokens`, and no stream; Claude Code 2.1.310 waits 60 seconds an attempt for it. To a model that isn't Claude it goes with `classifierRoom` (2048) added to a limit below it and asked to reason least (`fitAutoModeClassifier`): none, or the lowest of the model's known levels. A model whose levels magpie doesn't know (a relay's own name, gpt6.1-sol) is asked for none too, as magpie's own ask (`Request.OwnEffort`): left to the vendor, a reasoning model reasoned at its default past the 60 seconds and every classifier request ended 499 (CyberCaius on Discord). Its 400 to that effort (`ownEffortRefused`) is asked again at low when it named the level, and without the effort when it named the field ("Unrecognized request argument supplied: reasoning_effort"), remembered in `unfit` until magpie restarts (`TestAutoModeClassifierReasonsLeastOnAnUnknownModel`, `TestAutoModeClassifierEffortRefusedGoesWithout`).- Claude Code's billing line (`x-anthropic-billing-header: cc_version=…; cc_entrypoint=…; cch=…;`, its own first system block, `cch` changing every turn) goes on only to a Claude model asked on Anthropic's Messages API (`keepsBillingLine` in [`billing_line.go`](../../internal/gateway/billing_line.go): Anthropic's, a relay's, Bedrock's), as Claude Code sent it. Anywhere else it is left out, the rest of the system as sent (`withoutBillingLine`): a vendor's model relayed on Messages (`passthrough`), every translation to Chat, Responses or Gemini (`forwardTranslated`, Claude models too). Kept, a vendor read it as the prompt's first words and its prefix cache missed from byte 0 every turn (#1600, justrun1983: 4% read from DeepSeek's cache behind Command Code's plan). The Claude subscription's run sends its own (`withoutBillingHeader`), another magpie decides for the provider it picks, and Code Assist (Antigravity, Gemini CLI) is sent it as before, which `antigravityTurnsAway` reads (#666). Once Antigravity has turned such a request away, for `agForeseeing` (6 hours) after, a candidate loop with someone left after an Antigravity account skips it for a request that carries the line, without asking it (`antigravityForeseen`, #2156: each Claude Desktop turn waited about a second on the same refusal before the next member). The skip goes in `Fallback` with the hint, nothing rests, and the agent is told the refusal if nobody after it answers but with a quota. The last one left is still asked, and an old refusal foresees nothing, so a block Google lifts is noticed (`TestAntigravityTurnedAwayIsForeseen`). Codex's own identity line for its GPT-5 models ("You are Codex, an agent based on GPT-5", "…a coding agent based on GPT-5", any case) is turned away the same way (`antigravityTurnsCodexAway`): Codex sends its model's prompt as the thread's first developer message, which goes in as the system instruction, and a thread begun on a GPT model keeps it after `/model`, `resume -m` or a fork as an agent's thread, so such a thread on an Antigravity model 429'd until Codex's retry limit while dsh on the same account answered (Jodiezzzzzz on X, 10-27). It gets its own hint (`antigravityCodexHint`: start a new chat on the model, which is sent magpie's own Codex prompt "You are Codex, based on GPT-5.", which Antigravity answers), chosen by `antigravityHint`. Codex sends its `web_search` tool every turn, so an Antigravity model's Codex turn goes through magpie's own search (`searchReply`); its rounds are wrapped in `antigravityRound`, which tells the 429 (or the reply's error event) the same way and marks the attempt, where before the search path returned the bare 429 that Codex retried to its limit.- A request translated to a Chat or Responses upstream that carries no `prompt_cache_key` of the client's (every Anthropic client's) gets one for its conversation when the provider is OpenAI's API with a key or one the user added by its address (`ownCacheKey`, `cacheKeyOf` in [`gateway.go`](../../internal/gateway/gateway.go)): a hash, in a UUID's shape, of the session the client names, else of its first user message, so every turn of it has the same key and nothing in it names the client or magpie. A relay can turn a request away without one ("prompt_cache_key is required", CyberCaius's AnyRouter). A vendor's preset, a signed-in account, a plugin, Bedrock and Vertex get none, nor does a provider whose extraBody names `prompt_cache_key` (the user's value is sent). An upstream whose 400 names the field is asked again without it and not sent it again; another 400 is not asked again for it, so an unrelated refusal is tried as often as before.-- Claude Desktop's check that a gateway model answers (`{"max_tokens":1,"messages":[{"role":"user","content":"."}]}`, sent as it starts and by its settings' Test on the first haiku it is listed, else sonnet, else opus) is routed as a title is (`desktopTurn` in [`desktop.go`](../../internal/gateway/desktop.go)): to the session's model, never to another provider's Claude model whose quota it would spend (CyberCaius on Discord, after v0.1.1819 sent it to the haiku it names). A relay's floor on max_tokens is met by asking again (`tokenFloor`), and a relay that needs `prompt_cache_key` gets one (above), so the check is answered there.+- Claude Desktop's check that a gateway model answers (`{"max_tokens":1,"messages":[{"role":"user","content":"."}]}`, sent as it starts and by its settings' Test connection on the shortest id it is listed with haiku in it, else sonnet, else opus; `desktopProbe` in [`desktop.go`](../../internal/gateway/desktop.go)) goes to Desktop's default model, the first one listed to it (`desktopDefault`; Desktop's own settings call the first listed model the default), never to the haiku it names, which is another provider's quota, nor to the model the last session ran on (`desktopPicked`), which can be days old: v0.1.1821 sent it there, a relay's GPT model CyberCaius had left for his routing group, and the relay turned it away (Discord, v0.1.1838). It is asked for 16 tokens upstream (`desktopProbeTokens`): OpenAI's models refuse fewer, and a relay in front of one (AnyRouter's "bad response status code 400") passes on no floor for `tokenFloor` to ask again at. A relay that needs `prompt_cache_key` gets one (above). `TestClaudeDesktopProbeToDefaultModel`.- Antigravity answers a system instruction holding one of a few lines with a 429 "Resource has been exhausted" whatever quota is left, on every model (`agTurnedAway` in [`codeassist.go`](../../internal/gateway/codeassist.go)): Claude Code's billing line, the Claude Agent SDK's identity line, Codex's identity lines for its GPT-5 models (which a Codex thread begun on a GPT model keeps after a switch), and Hermes Agent's earlier default persona. An Antigravity account's system instruction goes with a zero-width space after each such line's first letter (`antigravityCloak`, in `codeAssistBody`), which Google lets through and the model reads the same; every other provider, Gemini CLI's sign-in on the same envelope included, is sent the prompt as written. `magpie provider set antigravity prompt=as-written` (`Provider.PromptAsWritten`) turns it off. The refusal is still told against the system prompt as the agent wrote it (`antigravityTurnedAway`): a 400 with the hint for that prompt, the account not rested, and a group skipping Antigravity for that prompt for 6 hours after a refusal (`antigravityForeseen`).- Claude Code's advisor (`/advisor`, `--advisor`) is Anthropic's server tool `advisor_20260301`, its `model` named as Claude Code names its own: on magpie, by magpie's id (the tier an alias like `opus` is on), which no vendor knows, so the request was turned away whole (JackChou on Discord). Relayed on Messages (`passthrough`), the advisor's model goes as the upstream's own name for it when it is the same provider's Claude model, or a Claude id naming no provider (`advisorTools` in [`advisor.go`](../../internal/gateway/advisor.go)); one it can't run there (another provider's, a group, not Claude's) is left out, as is every advisor when the request's own model isn't a Claude model (a vendor's Messages API knows no such tool), and the turn is answered without one. A translation drops it with every other server tool. Claude Code (2.1.300) also puts the advisor in the conversation: a `{role: "system"}` message whose text ends in its instructions ("# Advisor Tool … call advisor BEFORE substantive work") and a `tool_addition` naming it. Where the advisor is left out, that `tool_addition` goes too (and a message it leaves empty), and the instructions become Claude Code's own "The advisor tool is no longer available; disregard the earlier advisor instructions." (`withoutAdvisor` on Messages, `advisorGoneIn` in `parseAnthropic` for a translation): left there, the model called `advisor()` as one of the agent's tools and Claude Code answered "No such tool available: advisor" (JackChou on Discord). The Claude subscription's run declares only magpie's tools, so it never has one. `TestAdvisorModelAsTheUpstreamNamesIt`, `TestAdvisorLeftOutOnTranslation`.- Kiro is told where to cache the prompt (plugins#110: a Kiro subscription read 0 from its cache). Its API takes Bedrock's `cachePoint: {"type":"default"}` on a `userInputMessage`, an `assistantResponseMessage` or as a tool entry. The built-in (`buildKiro` in [`kiro.go`](../../internal/gateway/kiro.go)) marks the first message when there is a system prompt, which it folds into that message, and the current message, as `buildAnthropic` marks Anthropic's. A moved Kiro's plugin (opencode-kiro-auth 0.1.10, the mover's `min`) turns each `cache_control` in the Anthropic body it is handed into one, at most 4, the last kept.diff --git a/internal/gateway/desktop.go b/internal/gateway/desktop.goindex 463863b83..6b0c3f6a1 100644--- a/internal/gateway/desktop.go+++ b/internal/gateway/desktop.go@@ -483,6 +483,16 @@ func desktopTurn(asked string, body []byte, byClaudeCode, bySubagent bool) strinif m := desktopTierTurn(asked); m != "" {return m}+ if desktopProbe(body) {+ // Desktop's check that the gateway answers, as it starts and from+ // Test connection: on its default model, never the model of+ // whichever session last ran (desktopPicked), which may be days+ // old and on another provider+ if m := desktopDefault(asked); m != "" {+ return m+ }+ return asked+ }tools := hasTools(body)desktopPicked.Lock()defer desktopPicked.Unlock()@@ -589,6 +599,43 @@ func desktopDefault(asked string) string {return shown[0].ID}+// desktopProbe: the request is Claude Desktop's check that a gateway+// answers, which it runs as it starts and from its gateway settings' Test+// connection (lWt in its app.asar, 2.7032):+//+// {"model":<id>,"max_tokens":1,"messages":[{"role":"user","content":"."}]}+//+// on the shortest id of /v1/models with haiku in it, else sonnet, else+// opus (YT): a model Desktop picks by its name, not one the user chose.+// It is asked of Desktop's default model instead (desktopDefault, the+// first listed: "the default model (the first listed model)", as its+// settings say). Sent as a title is, to the model the last session ran+// on, it went to a relay's GPT model the user had left days before and+// was turned away there, shown as the haiku rejected (CyberCaius on+// Discord, v0.1.1838).+func desktopProbe(body []byte) bool {+ var v struct {+ MaxTokens int `json:"max_tokens"`+ System json.RawMessage `json:"system"`+ Tools json.RawMessage `json:"tools"`+ Messages []json.RawMessage `json:"messages"`+ }+ if json.Unmarshal(body, &v) != nil || v.MaxTokens != 1 || len(v.System) > 0 || len(v.Tools) > 0 || len(v.Messages) != 1 {+ return false+ }+ var m struct {+ Role string `json:"role"`+ Content string `json:"content"`+ }+ return json.Unmarshal(v.Messages[0], &m) == nil && m.Role == "user" && m.Content == "."+}++// desktopProbeTokens is the reply Desktop's check is asked for upstream:+// OpenAI's models refuse fewer than 16 output tokens, and a relay in front+// of one (AnyRouter: "bad response status code 400") passes on no floor+// to ask again at (tokenFloor). Desktop reads only whether it is answered.+const desktopProbeTokens = 16+// desktopTitlePrompt is how the system prompt Claude Desktop asks a// session's title and branch name with starts (dcr in its app.asar), sent// by its own client or, when that fails, as Claude Code's --system-prompt.diff --git a/internal/gateway/desktop_probe_test.go b/internal/gateway/desktop_probe_test.goindex 3440b3a94..36c2624b6 100644--- a/internal/gateway/desktop_probe_test.go+++ b/internal/gateway/desktop_probe_test.go@@ -19,38 +19,48 @@ func relayNeedsCacheKey(body []byte) (int, string) {return 0, ""}-// Claude Desktop checks a gateway model as it starts with-// {"max_tokens":1,"messages":[{"role":"user","content":"."}]} on the first-// haiku it is listed. It goes, as a title does, to the session's model:-// the one the user picked, never another provider's Claude model, whose-// quota it would spend (CyberCaius on Discord, after v0.1.1819 sent it to-// leee's haiku). On a relay's GPT model it once failed for the cache key-// the relay needs (shown as the haiku rejected); with the key, and the-// relay's floor on max_tokens met by asking again, it is answered.-func TestClaudeDesktopProbeToSessionModel(t *testing.T) {+// Claude Desktop checks the gateway as it starts, and from Test+// connection, with {"max_tokens":1,"messages":[{"role":"user",+// "content":"."}]} on the shortest haiku it is listed:+// magpie-0958175816.anthropic.claude-haiku-5-5[1m], leee's haiku, for+// CyberCaius on Discord (v0.1.1838). It goes to Desktop's default model,+// the first listed (his routing group 天翼云), not to leee nor to the+// relay's GPT model a session ran on last, and is answered there. A+// title still goes to that session's model.+func TestClaudeDesktopProbeToDefaultModel(t *testing.T) {claude := &fake{t: t, reply: sse(`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m","type":"message","role":"assistant","model":"claude-haiku-5-5","content":[],"usage":{"input_tokens":1,"output_tokens":0}}}`,`event: message_stop`+"\n"+`data: {"type":"message_stop"}`)}+ // as AnyRouter answers a GPT model asked for fewer than 16 tokens: the+ // upstream's floor isn't passed onrelay := &fake{t: t, reply: deepseekReply, refuse: func(body []byte) (int, string) {if c, m := relayNeedsCacheKey(body); c != 0 {return c, m}- // as OpenAI's models answer a reply of fewer than 16 tokensif n := max(gjson.GetBytes(body, "max_tokens").Int(), gjson.GetBytes(body, "max_completion_tokens").Int()); n > 0 && n < 16 {- return 400, `{"error":{"message":"Invalid 'max_tokens': integer below minimum value. Expected a value >= 16, but got 1 instead.","type":"invalid_request_error"}}`+ return 400, `{"error":{"message":"AnyRouter: bad response status code 400 (request id: x)","type":"new_api_error"}}`}return 0, ""}}+ ctyun := &fake{t: t, reply: deepseekReply}up := setup(t, provider.Anthropic, claude)- _ = upru := httptest.NewServer(relay)t.Cleanup(ru.Close)- if err := provider.Save(provider.Provider{ID: "leee", Name: "leee", Key: "k", Anthropic: up.URL,- Models: []string{"claude-haiku-5-5"}}); err != nil {+ cu := httptest.NewServer(ctyun)+ t.Cleanup(cu.Close)+ for _, p := range []provider.Provider{+ {ID: "leee", Name: "leee", Key: "k", Anthropic: up.URL, Models: []string{"claude-haiku-5-5"}},+ {ID: "anyrouter", Name: "AnyRouter", Key: "k", Chat: ru.URL + "/v1", Models: []string{"gpt-6.1-sol"}},+ {ID: "ctyun", Name: "天翼云", Key: "k", Chat: cu.URL + "/v1", Models: []string{"glm-5"}},+ } {+ if err := provider.Save(p); err != nil {+ t.Fatal(err)+ }+ }+ if err := provider.SaveGroup(provider.Group{ID: "tianyi", Name: "天翼云", Members: []string{"ctyun/glm-5"}, Routing: provider.Ordered}); err != nil {t.Fatal(err)}- if err := provider.Save(provider.Provider{ID: "anyrouter", Name: "AnyRouter", Key: "k", Chat: ru.URL + "/v1",- Models: []string{"gpt-6.1-sol"}}); err != nil {+ if err := provider.SetModelOrder("claude-desktop", []string{"group/tianyi"}); err != nil {t.Fatal(err)}desktopPicked.Lock()@@ -66,7 +76,7 @@ func TestClaudeDesktopProbeToSessionModel(t *testing.T) {srv.Handler().ServeHTTP(rec, req)return rec.Code, rec.Body.String()}- // the session's turn, on the relay's model+ // a session's turn, on the relay's model, before the restartif code, body := send(`{"model":"anyrouter/gpt-6.1-sol","max_tokens":32000,"stream":true,"tools":[{"name":"Bash","input_schema":{"type":"object"}}],"messages":[{"role":"user","content":"hi"}]}`); code != 200 {t.Fatalf("turn: %d %s", code, body)}@@ -75,22 +85,19 @@ func TestClaudeDesktopProbeToSessionModel(t *testing.T) {t.Fatalf("turn's cache key %q", first)}- // Desktop's check of its haiku, by the id it was listed- haiku := "magpie-" + aliasNumber("leee/claude-haiku-5-5") + ".anthropic.claude-haiku-5-5[1m]"+ // Desktop's check, by the id it was listed leee's haiku by+ const probe = `{"model":"magpie-0958175816.anthropic.claude-haiku-5-5[1m]","max_tokens":1,"messages":[{"role":"user","content":"."}]}`relay.calls, relay.got, claude.got = 0, nil, nil- if code, body := send(`{"model":"` + haiku + `","max_tokens":1,"messages":[{"role":"user","content":"."}]}`); code != 200 {+ if code, body := send(probe); code != 200 {t.Fatalf("probe: %d %s", code, body)}- if claude.got != nil || modelOf(relay.got) != "gpt-6.1-sol" {- t.Fatalf("probe went to leee (%s), not the session's model (relay got %s)", claude.got, relay.got)- }- if relay.calls != 2 {- t.Fatalf("relay asked %d times, want 2 (the floor met once)", relay.calls)+ if claude.got != nil || relay.calls != 0 || modelOf(ctyun.got) != "glm-5" {+ t.Fatalf("probe went to leee (%s) or the relay (%d calls), not Desktop's default 天翼云 (got %s)", claude.got, relay.calls, ctyun.got)}- // so does a title+ // a title still goes to the session's modelrelay.got = nil- if code, body := send(`{"model":"` + haiku + `","max_tokens":200,"stream":true,"system":"You write short session titles.","messages":[{"role":"user","content":"hi"}]}`); code != 200 {+ if code, body := send(`{"model":"magpie-0958175816.anthropic.claude-haiku-5-5[1m]","max_tokens":200,"stream":true,"system":"You write short session titles.","messages":[{"role":"user","content":"hi"}]}`); code != 200 {t.Fatalf("title: %d %s", code, body)}if modelOf(relay.got) != "gpt-6.1-sol" {@@ -103,6 +110,35 @@ func TestClaudeDesktopProbeToSessionModel(t *testing.T) {if k := gjson.GetBytes(relay.got, "prompt_cache_key").String(); k != first {t.Fatalf("second turn's key %q, first %q", k, first)}++ // with the relay's GPT model first listed, the check is answered there+ // at once: asked for a reply OpenAI's models give, as the relay says+ // no floor to ask again at+ if err := provider.SetModelOrder("claude-desktop", []string{"anyrouter/gpt-6.1-sol"}); err != nil {+ t.Fatal(err)+ }+ relay.calls, relay.got, claude.got = 0, nil, nil+ if code, body := send(probe); code != 200 {+ t.Fatalf("probe on the relay: %d %s", code, body)+ }+ if claude.got != nil || relay.calls != 1 || modelOf(relay.got) != "gpt-6.1-sol" {+ t.Fatalf("probe on the relay: %d calls, leee got %s", relay.calls, claude.got)+ }+}++// desktopProbe knows Desktop's check alone, not a title or a turn.+func TestDesktopProbe(t *testing.T) {+ for body, want := range map[string]bool{+ `{"model":"magpie-0958175816.anthropic.claude-haiku-5-5[1m]","max_tokens":1,"messages":[{"role":"user","content":"."}]}`: true,+ `{"model":"x","max_tokens":200,"messages":[{"role":"user","content":"."}]}`: false,+ `{"model":"x","max_tokens":1,"system":"You write short session titles.","messages":[{"role":"user","content":"."}]}`: false,+ `{"model":"x","max_tokens":1,"messages":[{"role":"user","content":"hi"}]}`: false,+ `{"model":"x","max_tokens":1,"tools":[{"name":"a"}],"messages":[{"role":"user","content":"."}]}`: false,+ } {+ if desktopProbe([]byte(body)) != want {+ t.Errorf("desktopProbe(%s) = %v", body, !want)+ }+ }}// magpie's own cache key goes only to OpenAI's API and a provider the userdiff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.goindex 5ea2f9dc7..258e144b9 100644--- a/internal/gateway/gateway.go+++ b/internal/gateway/gateway.go@@ -1807,7 +1807,17 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Proasked = call.Modelif agent == "claude-desktop" {sub, _ := subagentOf(r.Header)+ probe := desktopProbe(body)asked = desktopTurn(asked, body, fromClaudeCode(r.Header), sub != "")+ if probe {+ // its check, asked for room every model answers in+ if b, ok := withTokenFloor(body, desktopProbeTokens); ok {+ body = b+ }+ if b, ok := withTokenFloor(plain, desktopProbeTokens); ok {+ plain = b+ }+ }} else if agent == ScienceID {asked = scienceTurn(asked, body)}