From d54b7acf43ae8a48fdff28323f9307fac794c6ab Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 17 Nov 2026 21:10:09 +0800 Subject: [PATCH] gateway: Claude Code's dashed advisor id goes upstream as the provider names it Refeccd (Discord, v0.1.4366): Claude Code with Opus 5.5 as the model and Fable 5.2 as the advisor, both on one provider; the advisor's turns came from Opus and Routing showed no Fable request. Claude Code 2.1.316 (real binary, sandbox HOME, a fake upstream) never sends a request of its own for the advisor: it is the server tool {"type":"advisor_20260301","model":"relay/claude-fable-5-2"} inside the main request, with settings.json's advisorModel as written. magpie writes a dotted Claude id dashed (ClaudeSpelled), and the gateway took the main model back to the served id (validateModel) but not the advisor's. On a provider listing claude-opus-5.5 and claude-fable-5.2 the request went upstream as model claude-opus-5.5 with advisor claude-fable-5-2, a name the relay doesn't know. advisorID now takes the advisor's model to the served id as a request's own model is (ClaudeUndashed, then unflat and CanonicalRef), and drops [2m] beside [1m]. Every reader of it goes through it: advisorModel (passthrough name), advisorKeyModel (key limits), advisorAsked (advisedHere, advisedInBridge) and readAnthropic's Request.Advisor (magpie-run advice). Run (on mica): - go test -count=1 -timeout 45m ./internal/gateway/: ok at 38babcb13 (this change on efcf8d9c0), and before at d67ccbd3c and 8e1534f5f - rebased since onto a922e3864, which adds only internal/library, internal/agent/dsh.go and internal/provider/agy_move.go: at this head, go test -run Advis ./internal/gateway/ ok, and GOOS=linux and GOOS=windows go vet ./internal/gateway/ ./internal/provider/ ok - gofmt -l internal/gateway: clean - GOOS=linux and GOOS=windows go vet ./internal/gateway/: ok Broken by hand: advisorID returning the trimmed id as before makes TestAdvisorModelAsTheUpstreamNamesIt's new "a dotted id as Claude Code names it" case red: advisor sent as ["claude-fable-5-2"], want "claude-fable-5.2", the reporter's shape. Not tried: the reporter's own relay. magpie still doesn't read usage.iterations' advisor_message entries, so a passthrough advisor run at the vendor shows no Fable row in Routing; that is by design of the server tool, not changed here (#6977). --- docs/subsystems/gateway-routing.md | 2 +- internal/gateway/advisor.go | 24 ++++++++++++++++++++++-- internal/gateway/advisor_serve.go | 2 +- internal/gateway/advisor_test.go | 5 +++++ internal/gateway/anthropic.go | 2 +- 5 files changed, 30 insertions(+), 5 deletions(-) diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index 1e3f66f22..5e7e6c5c1 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -431,7 +431,7 @@ the key's leaves the call as asked, with a log line (`compactRefused`). - 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. The provider's Claude models stand in only when the chat itself is on one of them: a chat on Leee's DeepSeek had Claude Code's sonnet calls sent to Leee's `claude-sonnet-5-5`, which its key may not use (403, CyberCaius on Discord), so Claude Code's own calls in a chat on another vendor's model go to the chat's model, and a tier the user picked takes Claude Code's own id of it even where a provider lists a model by that name (`TestClaudeDesktopNonClaudeChatKeepsTierCalls`). A lead's turn and an id the user picked for a tier (a catalog id) go as asked. `TestClaudeDesktopBackgroundStaysOnChatProvider`. - While a model is picked for one of Claude Desktop's tiers, magpie's Desktop profile also lists Desktop's models in `inferenceModels` (`DesktopInferenceModels` in [`desktop_list.go`](../../internal/gateway/desktop_list.go)), so a pick that isn't a Claude model runs in the Code tab with the prompt of its tier's newest Claude model (`behavesAs`: `claude-opus-5-5`, `claude-sonnet-5-5`, `claude-haiku-5-5`, Desktop's catalog v3739; `claude-fable-5-2`, Claude Code 2.1.321's newest Fable, which Desktop's catalog v4371 doesn't list yet), not Claude Code's generic one (moer2831 on Discord). Desktop 4.29 and later hand Claude Code `behavesAs` only from that list (z3t in its app.asar, 4.4212: `modelPicker.options` with each entry's `behavesAs`); the gateway's `behaves_as` row field feeds Desktop's own effort profile only. A non-empty list replaces `/v1/models` in Desktop's menus (pzn: a discovered row gives only its name and description, by id), so the list is every row `/v1/models` gives Desktop, by its id and in its order, each with what Desktop would have read off the row: `supports1m` for a window of 1M and up, `prefer1m` (and the row's `display_name` as `labelOverride`, where Desktop names the row by display_name) for a model listed by its `[1m]` id, and a pick's `anthropicFamilyTier` with `isFamilyDefault`. Desktop keys the list by name and reads one tier off an entry (FPn in its app.asar, 4.23118: the entry tagged with a tier names its `ANTHROPIC_DEFAULT__MODEL`), so a model picked for two tiers is tagged with the first (`desktopTierOf`). Each later tier (`desktopSharedTiers`) gets the model's id form under a number of its own (`desktopTierAlias`: magpie-.anthropic., mythos-magpie- or anthropic/magpie-), which `aliased` reads back to the model, so the gateway serves it as the pick (`desktopTierAsked`). That id is a row of Desktop's `/v1/models` only (`DesktopTierRows`, a copy of the model's row tagged `anthropic_family_tier` and `is_family_default`), never a list entry: the list is Desktop's model menu, and an entry there showed as a model of its own, "Gemini 4 Hydrogen · haiku" beside "Gemini 4 Hydrogen" (yanghonghang on X, v0.1.3997). Desktop's discovery pins a tier the list leaves out from a tagged row (`shortnameIdentityOverrides` in its app.asar, 4.23118, under the list's own pins), and with a list it reads `/v1/models` only when the profile says `modelDiscoveryEnabled`, which magpie's profile carries while a tier is shared (`DesktopDiscovers`). The later tier has no `behavesAs`, which Desktop takes only from the list, so a model that isn't Claude runs there on Claude Code's generic prompt. Before, the later tier had no model: copilot/claude-sonnet-5.5 picked for sonnet and haiku gave Claude Code an empty `ANTHROPIC_DEFAULT_HAIKU_MODEL`, and its small tasks, WebFetch's page summaries among them, ran on the session's model. A Claude model picked runs as itself where Claude Code knows it by the id it is listed by; a routing group led by one is listed by its mythos-magpie- and runs as the group's model, as one listed by anthropic/magpie- does (`desktopRunsAs`; `TestDesktopTierSharedPick`, `TestDesktopGroupPickRunsAsItsModel`). Nothing picked (or a pick of a model no longer listed): no list, and Desktop finds the models as before, unless two rows are named after one Claude model of Desktop's catalog, when the list is written for its `labelOverride`s alone (`desktopNamesClash`, onkashiko on X). Picks that can't be read are unknown, and what is written stays. Older builds take the entries' names and drop the rest (2.7032's entry is a zod object, which strips unknown keys). Desktop reads its profile only as it starts, so the tiers' menu says to restart it, and the Agents row's Restart shows (`AppBehind` reads the profile). `TestDesktopInferenceModels`. - 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), Hermes Agent's earlier default persona, and oh-my-pi's RFC 2119 line; since the evening of 11-14 also "You are Hermes Agent" (Hermes' current persona), "You are OpenCode", Codex CLI's "You are a coding agent running in the Codex CLI" (its prompt for models it has none of its own for) and pi's "a coding assistant running inside pi". Codex's GPT-6 prompt, Claude Code's own line, Gemini CLI, Qwen Code, Cline, Kilo, goose, Amp, Droid, Kimi, Aider and Cursor pass as written (real account, 11-14). An Antigravity account's system instruction goes with each such line reworded with the same meaning (`antigravityCloak`, `agSaidAs`, in `codeAssistBody`): the billing line left out, "built on the Claude Agent SDK by Anthropic", Codex's "built on GPT-5", Hermes' "built by Nous Research", oh-my-pi's "RFC 2119 key words", "You're" for Hermes', OpenCode's and Codex CLI's "You are", and pi's "running within pi". The refusal's hint names the agent (`antigravityHint`: Codex, Codex CLI, Hermes, OpenCode, pi, RFC), word for word as the antigravity and agy-cli plugins and the Routing page's `HINTS` have it. Until 11-14 they went broken up by zero-width spaces; since then Google's check reads past invisible characters and turned every broken-up line away 5 of 5 on a real account, so a line is also matched with invisible characters inside it and a copy an older magpie or middleware broke up is reworded too. 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 run by magpie when magpie serves it (below) and otherwise 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 bridge runs it as magpie does on a translation (below, and [claude-subscription-bridge.md](claude-subscription-bridge.md)). `TestAdvisorModelAsTheUpstreamNamesIt`, `TestAdvisorLeftOutOnTranslation`. +- 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)). The model Claude Code names is first taken to the id magpie serves, as a request's own model is (`advisorID`): a dotted Claude id is given to Claude Code with dashes, and the advisor came back so, so `relay/claude-fable-5-2` on a provider listing `claude-fable-5.2` went upstream as `claude-fable-5-2`, a name the relay doesn't know (Refeccd on Discord); one it can't run there (another provider's, a group, not Claude's) is run by magpie when magpie serves it (below) and otherwise 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 bridge runs it as magpie does on a translation (below, and [claude-subscription-bridge.md](claude-subscription-bridge.md)). `TestAdvisorModelAsTheUpstreamNamesIt`, `TestAdvisorLeftOutOnTranslation`. - magpie runs Claude Code's advisor itself where the upstream can't and the advisor's model is one magpie serves (`provider.Resolve` takes it; `advisedHere` in [`advisor_serve.go`](../../internal/gateway/advisor_serve.go), Awad_Yoo on X). Claude Code sends the advisor for any model only with `CLAUDE_CODE_ENABLE_EXPERIMENTAL_ADVISOR_TOOL` set (2.1.316), which the Claude Code advisor field writes (agent-wiring.md). Such a request is translated, a relay on Messages too (`ownAPI`): the server tool becomes `Request.Advisor`, and the model is offered a function tool `advisor` with no parameters (`advisorToolDef`, a neutral description naming neither magpie nor a vendor), Claude Code's instructions and `tool_addition` kept as written. A call of it is answered inside the reply's rounds, beside web search's (`searchRounds`, `adviceRound`): the advisor model is asked through the gateway (`advise`, User-Agent `magpie-advisor/1`, the executor's system prompt and conversation, what the model said before the call, its tools offered with `tool_choice` none, no reasoning, and a short ask appended to the last user message, `adviceFor`; Claude Code's advisor instructions are cut from its system prompt and messages, `advisorCut`, since with them, or `advisorGone` in their place, the advisor answered as the executor that it had no advisor, #6804), and the reply goes on with the advice as the call's result. Claude Code is told as Anthropic's server tells it: a `server_tool_use` (`srvtoolu_…`, empty input) and an `advisor_tool_result` with `{type: advisor_result, text}`, or `{type: advisor_tool_result_error, error_code}` (`unavailable`, `too_many_requests`, `overloaded`, `prompt_too_long`, `execution_time_exceeded`, or `max_uses_exceeded` past three calls a reply). Claude Code echoes both blocks in the next turn's assistant message; `readAdvised` reads them back as the model's call and its result, splitting the message around them (`adviceApart`). Not handled: a round that calls the advisor beside an agent tool keeps the agent's tool call only, as with web search. `TestAdvisorServedForAVendorModel`, `TestAdvisorHistoryGoesBackAsACall`, `TestAdvisorFailureIsAnErrorCode`, `TestAdvisedHere`. - 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. - The cache marks `buildAnthropic` adds for a Responses client (Codex) to a Claude model last an hour (`addedMark`, `Request.hourMarks`; 7eleven on Discord): Codex's turns are often more than five minutes apart, and Codex's own vendor keeps a thread cached an hour (`chatGPTCold`). A 1h write costs 2x the input price where Anthropic's default 5m costs 1.25x and a read 0.1x or less, so one turn read after a 5 to 60 minute pause, which a 5m mark would have written again, pays for the dearer writes of dozens of turns in quick succession. Every mark it adds has the same ttl, since Anthropic refuses a 1h mark after a 5m one. The client's `prompt_cache_retention: "in_memory"` keeps them at 5m. A Chat client's marks, a model that isn't Claude's, and marks a client placed itself (an Anthropic client's, relayed as sent) are unchanged. An upstream that answers 400 naming `cache_control` and `ttl` (a relay that knows no ttl) is asked again at 5m and remembered in `unfit` (`hourMarksRefused`) until magpie restarts. A Claude subscription is served by Claude Code, which asks 1h unless the user picked 5m ([Claude subscription bridge](claude-subscription-bridge.md)). `TestResponsesToClaudeMarksLastAnHour`, `TestHourMarksRefused`. diff --git a/internal/gateway/advisor.go b/internal/gateway/advisor.go index d3c04565b..73198d44d 100644 --- a/internal/gateway/advisor.go +++ b/internal/gateway/advisor.go @@ -89,11 +89,31 @@ func advisorTools(body []byte, p provider.Provider, upstream string, wires map[s return out } +// advisorID is the advisor model Claude Code names, taken to the id magpie +// serves as a request's own model is (validateModel): Claude Code is given +// a dotted Claude id with dashes (provider.ClaudeSpelled) and writes +// settings.json's advisorModel so, and it sends that id as written, its +// [1m] or [2m] mark dropped. Taken as it was, relay/claude-fable-5-2 was no +// model of a provider that lists claude-fable-5.2, so it went upstream as +// claude-fable-5-2 beside the request's claude-opus-5.5, a name the relay +// doesn't know (Refeccd on Discord). +func advisorID(model string) string { + model = strings.TrimSpace(model) + model = strings.TrimSuffix(strings.TrimSuffix(model, "[1m]"), "[2m]") + if model == "" { + return "" + } + if real, ok := provider.ClaudeUndashed(model); ok { + return real + } + return provider.CanonicalRef(unflat(model)) +} + // advisorKeyModel is the model id a key's models are checked against for // an advisor named model: magpie's model id when it names one, else the // bare id as it was sent. func advisorKeyModel(model string) string { - model = strings.TrimSuffix(strings.TrimSpace(model), "[1m]") + model = advisorID(model) if _, m, ok := provider.Resolve(model); ok && !provider.IsGroupRef(model) { return m } @@ -104,7 +124,7 @@ func advisorKeyModel(model string) string { // whether p can run it: one of p's models, by magpie's id, or a Claude // model's own id that names no provider. func advisorModel(p provider.Provider, model string, wires map[string]string) (string, bool) { - model = strings.TrimSuffix(strings.TrimSpace(model), "[1m]") + model = advisorID(model) if model == "" { return "", false } diff --git a/internal/gateway/advisor_serve.go b/internal/gateway/advisor_serve.go index 026c08599..0d97f4fef 100644 --- a/internal/gateway/advisor_serve.go +++ b/internal/gateway/advisor_serve.go @@ -73,7 +73,7 @@ func advisorAsked(from provider.Protocol, body []byte) string { typ := t.Get("type").String() if strings.HasPrefix(typ, "advisor_") { if adv == "" { - adv = t.Get("model").String() + adv = advisorID(t.Get("model").String()) } continue } diff --git a/internal/gateway/advisor_test.go b/internal/gateway/advisor_test.go index 8aff21dcb..f232c1eea 100644 --- a/internal/gateway/advisor_test.go +++ b/internal/gateway/advisor_test.go @@ -39,6 +39,7 @@ func TestAdvisorModelAsTheUpstreamNamesIt(t *testing.T) { {ID: "relay", Name: "Relay", Key: "k", Anthropic: url, Models: []string{"claude-opus-5-5", "claude-sonnet-5-5"}}, {ID: "other", Name: "Other", Key: "k", Anthropic: url, Models: []string{"claude-opus-5-5"}}, {ID: "ds", Name: "DeepSeek", Key: "k", Anthropic: url, Models: []string{"deepseek-v4.1-flash"}}, + {ID: "dot", Name: "Dotted", Key: "k", Anthropic: url, Models: []string{"claude-opus-5.5", "claude-fable-5.2"}}, } { if err := provider.Save(p); err != nil { t.Fatal(err) @@ -63,6 +64,10 @@ func TestAdvisorModelAsTheUpstreamNamesIt(t *testing.T) { {"another provider", "relay/claude-sonnet-5-5", "other/claude-opus-5-5", "", true}, {"a vendor's model", "ds/deepseek-v4.1-flash", "relay/claude-opus-5-5", "", true}, {"a model magpie doesn't serve", "ds/deepseek-v4.1-flash", "nobody/claude-opus-5-5", "", false}, + {"2M mark", "relay/claude-sonnet-5-5", "relay/claude-opus-5-5[2m]", "claude-opus-5-5", false}, + // Claude Code is given a dotted id with dashes, and names the + // advisor so (Refeccd on Discord) + {"a dotted id as Claude Code names it", "dot/claude-opus-5-5", "dot/claude-fable-5-2", "claude-fable-5.2", false}, } { claudeCodeSends(t, advisorTurn(c.model, c.advisor)) b := u.last(t) diff --git a/internal/gateway/anthropic.go b/internal/gateway/anthropic.go index 4e0a48a97..49c5c3f7e 100644 --- a/internal/gateway/anthropic.go +++ b/internal/gateway/anthropic.go @@ -299,7 +299,7 @@ func readAdvised(body []byte, clientMarks, facts, advised bool) (*Request, error if advised { // run by magpie: offered as a tool of the model's // own when the reply is asked (advisor_serve.go) - r.Advisor = cmp.Or(r.Advisor, t.Model) + r.Advisor = cmp.Or(r.Advisor, advisorID(t.Model)) continue } // Claude Code's advisor: its instructions, which would