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 the model chosen in its own menu (cybercaius on Discord)
yetone9f36d768d393
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.mdindex 2260ff7c4..a0956c617 100644--- a/docs/subsystems/gateway-routing.md+++ b/docs/subsystems/gateway-routing.md@@ -280,7 +280,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 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`.+- 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 the model the user chose in one of Desktop's own model menus that magpie still serves (`desktopChosen`: `__model_selector_state` in Claude-3p's `local-agent-mode-sessions/<account>/<org>/cowork_account_settings.json`, the one the last session ran on first, then Code, Cowork, Chat; cybercaius's DeepSeek in the Code tab, v0.1.2033), else 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`, `TestClaudeDesktopProbeToChosenModel`.- Claude Desktop's `/v1/models` tags a model with `anthropic_family_tier` only when the user picked it for that tier (`DesktopTiers`, the Agents page's opus, sonnet, haiku and fable). Before, a Claude model magpie served was tagged with its own tier unpicked, so Desktop's Code tab told Claude Code to run its haiku calls (its small, fast model) on whichever provider served a `claude-haiku-…`: a chat on AgentRouter's gpt-6.1-sol sent them to Leee's `claude-haiku-5-5` (cybercaius on Discord). Untagged, Claude Code asks for its own id of the tier. A background call (no tools) or a subagent's turn asking 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: `servedBeside` gives that provider's model of that name, else its newest model of the tier, beside the chat's model first, else the call goes to the chat's model. A lead's turn and an id the user picked for a tier (a catalog id) go as asked. `TestClaudeDesktopBackgroundStaysOnChatProvider`.- 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`.diff --git a/internal/gateway/desktop.go b/internal/gateway/desktop.goindex 2edc7a3aa..2114881bc 100644--- a/internal/gateway/desktop.go+++ b/internal/gateway/desktop.go@@ -9,10 +9,14 @@ import ("os""path/filepath""regexp"+ "slices""strconv""strings""sync"+ "time"+ "github.com/yetone/magpie/internal/appdir"+ "github.com/yetone/magpie/internal/desktopdir""github.com/yetone/magpie/internal/lastgood""github.com/yetone/magpie/internal/provider""github.com/yetone/magpie/internal/settings"@@ -489,9 +493,16 @@ func desktopTurn(asked string, body []byte, byClaudeCode, bySubagent bool) strin}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+ // Test connection: on the model the user chose in Desktop's own+ // menu (desktopChosen), else its default model; never the model+ // of whichever session last ran (desktopPicked) on its own,+ // which may be days old and on another provider+ if m := desktopChosen(); m != "" {+ if m == asked {+ return asked+ }+ return m+ }if m := desktopDefault(asked); m != "" {return m}@@ -673,6 +684,93 @@ func desktopDefault(asked string) string {return shown[0].ID}+// desktopSelectorRoots are the Claude-3p folders Desktop keeps its model+// menus' choices in (desktopChosen): every set desktopdir finds. A test+// sets its own.+var desktopSelectorRoots = func() []string {+ home, _ := os.UserHomeDir()+ var out []string+ for _, d := range desktopdir.Sets(desktopdir.OS, home, appdir.Getenv) {+ if d.ThreeP != "" {+ out = append(out, d.ThreeP)+ }+ }+ return out+}++// desktopSurfaces are Desktop's model menus (bC in its app.asar 2.7032),+// in the order desktopChosen takes them: its Code tab first, the one+// magpie's users run, then Cowork, which can be turned off, then Chat.+var desktopSurfaces = []string{"code", "cowork", "chat"}++// desktopChosen is the model the user chose in one of Claude Desktop's+// model menus that magpie still serves, by magpie's id for it, "" for+// none. Desktop keeps each menu's choice in its userData (Claude-3p in+// its 3p mode), in+// local-agent-mode-sessions/<account>/<org>/cowork_account_settings.json:+//+// {"__model_selector_state":{"code":{"model":<id>},"cowork":{…},"chat":{…}}}+//+// (wLt and zLt in its app.asar 2.7032, written as a menu's choice+// changes; the newest such file is read). Desktop's check names a model+// by its name (desktopProbe), so a chat on DeepSeek in the Code tab had+// its check go to the first model listed, another provider's+// (cybercaius on Discord, v0.1.2033). Of the menus' choices, the one the+// last session ran on (desktopPicked) comes first, then desktopSurfaces'+// order. Read only for the check, as Desktop starts and from Test+// connection; a file not read or not parsed is no choice (desktopDefault).+func desktopChosen() string {+ var path string+ var newest time.Time+ for _, root := range desktopSelectorRoots() {+ found, _ := filepath.Glob(filepath.Join(root, "local-agent-mode-sessions", "*", "*", "cowork_account_settings.json"))+ for _, f := range found {+ if fi, err := os.Stat(f); err == nil && fi.Mode().IsRegular() && (path == "" || fi.ModTime().After(newest)) {+ path, newest = f, fi.ModTime()+ }+ }+ }+ if path == "" {+ return ""+ }+ b, err := os.ReadFile(path)+ if err != nil {+ return ""+ }+ var v struct {+ State map[string]struct {+ Model string `json:"model"`+ } `json:"__model_selector_state"`+ }+ if json.Unmarshal(b, &v) != nil {+ return ""+ }+ var chosen []string+ for _, s := range desktopSurfaces {+ // Desktop's id for it (claudeLooking), as magpie serves it+ m := unprefixed(strings.TrimSpace(v.State[s].Model))+ if m != "" && !unserved(m) && !slices.Contains(chosen, m) {+ chosen = append(chosen, m)+ }+ }+ if len(chosen) == 0 {+ return ""+ }+ desktopPicked.Lock()+ if p := desktopPickedPath(); desktopPicked.from != p {+ b, _ := os.ReadFile(p)+ desktopPicked.model, desktopPicked.from = strings.TrimSpace(string(b)), p+ }+ picked := desktopPicked.model+ desktopPicked.Unlock()+ for _, m := range chosen {+ if picked != "" && strings.TrimSuffix(m, "[1m]") == strings.TrimSuffix(unprefixed(picked), "[1m]") {+ return m+ }+ }+ return chosen[0]+}+// 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):@@ -681,7 +779,8 @@ func desktopDefault(asked string) string {//// 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+// It is asked of the model chosen in Desktop's menus (desktopChosen),+// else of Desktop's default model (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 anddiff --git a/internal/gateway/desktop_probe_test.go b/internal/gateway/desktop_probe_test.goindex 6efc7376b..37fa9e750 100644--- a/internal/gateway/desktop_probe_test.go+++ b/internal/gateway/desktop_probe_test.go@@ -5,6 +5,8 @@ import ("io""net/http""net/http/httptest"+ "os"+ "path/filepath""strings""testing"@@ -377,3 +379,116 @@ func TestWithThinkingEnabled(t *testing.T) {}}}++// Claude Desktop's check goes to the model the user chose in its own+// model menu, kept in Claude-3p's cowork_account_settings.json+// (__model_selector_state), not to the first model listed: cybercaius+// on Discord (v0.1.2033) had DeepSeek in the Code tab and Cowork off,+// and the check as Desktop started went to another provider's model.+// The menu the last session ran on comes first; a choice magpie no+// longer serves, or no file, leaves it to the first listed.+func TestClaudeDesktopProbeToChosenModel(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"}`)}+ ds := &fake{t: t, reply: deepseekReply}+ kimi := &fake{t: t, reply: deepseekReply}+ up := setup(t, provider.Anthropic, claude)+ du := httptest.NewServer(ds)+ t.Cleanup(du.Close)+ ku := httptest.NewServer(kimi)+ t.Cleanup(ku.Close)+ for _, p := range []provider.Provider{+ {ID: "leee", Name: "leee", Key: "k", Anthropic: up.URL, Models: []string{"claude-haiku-5-5"}},+ {ID: "deepseek", Name: "DeepSeek", Key: "k", Chat: du.URL + "/v1", Models: []string{"deepseek-v4-flash"}},+ {ID: "kimi", Name: "Kimi", Key: "k", Chat: ku.URL + "/v1", Models: []string{"kimi-k3"}},+ } {+ if err := provider.Save(p); err != nil {+ t.Fatal(err)+ }+ }+ // leee's haiku is listed first: Desktop's default+ if err := provider.SetModelOrder("claude-desktop", []string{"leee/claude-haiku-5-5", "deepseek/deepseek-v4-flash", "kimi/kimi-k3"}); err != nil {+ t.Fatal(err)+ }+ idOf := func(id string) string {+ t.Helper()+ for _, e := range provider.Catalog() {+ if e.ID == id {+ return DesktopID(e)+ }+ }+ t.Fatalf("%s not in the catalog", id)+ return ""+ }+ haiku, dsID, kimiID := idOf("leee/claude-haiku-5-5"), idOf("deepseek/deepseek-v4-flash"), idOf("kimi/kimi-k3")+ root := t.TempDir()+ old := desktopSelectorRoots+ desktopSelectorRoots = func() []string { return []string{root} }+ t.Cleanup(func() { desktopSelectorRoots = old })+ dir := filepath.Join(root, "local-agent-mode-sessions", "c774b2e6", "00000000")+ if err := os.MkdirAll(dir, 0o755); err != nil {+ t.Fatal(err)+ }+ choose := func(state string) {+ t.Helper()+ if err := os.WriteFile(filepath.Join(dir, "cowork_account_settings.json"), []byte(`{"__created_at":1,"__model_selector_state":`+state+`}`), 0o600); err != nil {+ t.Fatal(err)+ }+ }+ pick := func(m string) {+ desktopPicked.Lock()+ desktopPicked.model, desktopPicked.from = m, desktopPickedPath()+ desktopPicked.Unlock()+ }+ pick("")+ srv := New()+ probe := `{"model":"` + haiku + `","max_tokens":1,"messages":[{"role":"user","content":"."}]}`+ check := func(want *fake, wantModel, why string) {+ t.Helper()+ claude.got, ds.got, kimi.got = nil, nil, nil+ req := httptest.NewRequest("POST", "/v1/messages", strings.NewReader(probe))+ 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()+ srv.Handler().ServeHTTP(rec, req)+ if rec.Code != 200 {+ t.Fatalf("%s: probe %d %s", why, rec.Code, rec.Body.String())+ }+ for _, f := range []*fake{claude, ds, kimi} {+ if f != want && f.got != nil {+ t.Fatalf("%s: the check went elsewhere (%s)", why, f.got)+ }+ }+ if want == claude {+ if claude.got == nil {+ t.Fatalf("%s: the check didn't reach leee", why)+ }+ return+ }+ if modelOf(want.got) != wantModel {+ t.Fatalf("%s: the check reached %q, want %q", why, modelOf(want.got), wantModel)+ }+ }++ // no menu chosen yet: the first listed, as before+ check(claude, "", "no choice")+ // the reporter's: DeepSeek in the Code tab, Cowork left on its default+ choose(`{"code":{"model":"` + dsID + `"}}`)+ check(ds, "deepseek-v4-flash", "Code tab on DeepSeek")+ // by the id with Claude Code's "[1m]" too+ choose(`{"code":{"model":"` + dsID + `[1m]"},"chat":{"model":"` + haiku + `"}}`)+ check(ds, "deepseek-v4-flash", "Code tab on DeepSeek [1m]")+ // two menus: the Code tab's, unless the last session ran on the other+ choose(`{"cowork":{"model":"` + kimiID + `"},"code":{"model":"` + dsID + `"}}`)+ check(ds, "deepseek-v4-flash", "Code before Cowork")+ pick(kimiID)+ check(kimi, "kimi-k3", "the last session's menu first")+ pick("")+ // a choice magpie no longer serves leaves it to the first listed+ choose(`{"code":{"model":"anthropic/magpie-0000000001"}}`)+ check(claude, "", "unserved choice")+ // a half-written file is no choice+ choose(`{"code":{"model":"` + dsID)+ check(claude, "", "unparsed file")+}