claude: magpie's model names never stay in a Claude Code switched off it (roswellcsy on X)

roswellcsy on X (v0.1.2846). Claude Code was switched off magpie and on
its own sign-in, with several Claude accounts and one made first. Every
session said "There's an issue with the selected model
(claude/claude-opus-5-5)", while settings.json's model was the bare
claude-opus-5-5.

Cause: a name of magpie's left in Claude Code's env.
- The env's ANTHROPIC_MODEL outranks settings.json's model (Claude Code
  2.1.316: Wg(), `i.ANTHROPIC_MODEL||settings.model`). The tier variables
  stand in for opus/sonnet/haiku. On Anthropic's endpoint a
  provider/model name is a 404, shown as that message.
- How it got there and kept coming back:
  - Connecting a Claude Code that is not routed stashes its env models as
    "the user's own" (claude.env.<VAR>), magpie's names included.
  - Disconnect (unroute) then writes them back, again unchecked.
  - Once one is there, every connect and switch-off keeps it.
  - The bare model is the user's own, from connect.was or untouched.
- Nothing took the name out afterwards:
  - unroute returned at once when not routed;
  - Sync only did desktopOnly;
  - Disconnect is a no-op once Wired() is false (a bare model with no
    gateway URL).
- Sibling: wiring at an older gateway address (magpie's key) was left in
  place by Disconnect, while set("") already took it out.

Fix (internal/agent/claude.go):
- claudeMagpieModel(v): v is a catalog ref, read through ClaudeRead and
  with any :effort or [1m] taken off.
- It is never stashed as the user's own, and never given back from a
  stash (one an older magpie wrote included).
- dropStray: with no ANTHROPIC_BASE_URL (Anthropic's own endpoint), every
  claudeModelEnv key and `model` holding one of magpie's names is taken
  out. ANTHROPIC_MODEL, SMALL_FAST, SUBAGENT, the four tiers and the
  auto mode model are covered.
  - It runs from Sync when not routed, so SyncCatalog repairs users
    already in this state at the next start.
  - It also runs from unroute when not routed (Disconnect, the default
    model, a non-magpie model).
  - An endpoint of the user's own may name its models so, and those
    stay.
- unroute, when not routed: wiring at an older gateway address
  (keyOurs) goes whole, as set("") takes it.
- docs/subsystems/agent-wiring.md says so.

Test: TestClaudeOffLeavesNoMagpieModel, in a sandbox HOME. Each subtest
reads settings.json as Claude Code starts on it (ANTHROPIC_MODEL over
model, the tiers, small fast, subagent, auto mode).
- start: the stray env, then SyncCatalog;
- connect + Disconnect twice;
- a stash an older magpie wrote;
- the user's own models come back;
- an older gateway address;
- another endpoint's names are left alone.

Red on the old code: start, connect+Disconnect and older-address fail
(red-old.log). "connected and switched off again" reproduces the report
exactly: model "claude-opus-5-5" with env ANTHROPIC_MODEL
"relay/claude-opus-5-5".

Broken by hand, each through mote-run:
- dropStray out of Sync: start red.
- Both stash guards out: connect+Disconnect red.
- The restore guard alone out: older-stash red.
- keyOurs cleanup off: older-address red.
- With the stash guard alone out, the restore guard still holds, so it
  stays green.

Run on mote (mote-run, at d8501848a plus this change; the doc edit came
after the run):
- go test ./internal/agent/ ./internal/provider/ -count=1 -timeout 45m:
  ok agent (200s), ok provider (136s).
- The new test: 7 of 7 PASS (the parent and 6 subtests).
- GOOS=windows go vet ./...: ok.
- GOOS=linux go vet -tags nogui ./...: ok. Without the tag it stops in
  Wails' cgo-only Linux files when cross-compiling, which this change
  doesn't touch. GOOS=linux go vet ./internal/agent/ ./internal/provider/
  is ok too.
- gofmt -l: clean.

Not tried:
- A real Claude Code run against Anthropic. The 404 path was read from
  the 2.1.316 bundle, not hit.
- The user's literal provider id claude/: the test uses a relay
  provider. isMagpie resolves both the same way.
- WSL's Claude Code goes through the same claudeIn and wasn't run.
- A Claude Desktop Code tab with a provider/model in settings.json's
  model and no base URL would also lose it at Sync. Desktop writes
  mythos-magpie-<n> ids there, not provider/model ones.
This commit is contained in:
yetone
2026-11-03 19:18:50 +08:00
parent 1d0fc2bb08
commit 3f1f1882ac
3 changed files with 255 additions and 5 deletions
+1 -1
View File
@@ -106,7 +106,7 @@ something else shows up as drift.
- A field that follows another while empty (`Follows`: Claude Code's tiers and subagents on its main model) is read by `Drift` as on that field's model. The main model moved, in Claude Code's own `/model`, onto the model magpie had set a tier to is not drift (#1050). The tier keeps that model as its own through the move and the next one (`tier_own` in the stash), whatever it would follow on the new main model: fable, which follows no model that is no Fable, and haiku, which follows a Claude model on its Haiku, read as following and lost their model on the next move (#2257).
- A Claude Code tier or its subagents given a model of their own keep it when the main model moves onto that model and on again (`ownKey` in `claudeIn`), whether the main model is picked in magpie or in Claude Code's `/model`. Same as model, picked for them, has them follow again.
- While this machine's Claude Desktop is on magpie, Claude Code's shared settings get `CLAUDE_CODE_DISABLE_EXPLORE_INHERIT_CAP="1"` (`writeExplore` in `claudeIn`, [`claude.go`](../../internal/agent/claude.go)). Claude Code 2.1.295's Explore inherit cap can otherwise replace Desktop's `mythos-magpie` compatibility alias with Opus. This disables that cap, not the model choices of all subagents. It applies with Desktop alone or beside a connected Claude Code, including model picks and Sync; WSL twins do not get it. A user's existing value, including an empty string, stays. The stash (`claude.explore_inherit_cap`) marks only magpie's value: taking Claude Code off magpie leaves it while Desktop is on, and taking Desktop off removes it only if the user has not changed it. Since the settings file is shared, other Claude Code sessions reading it see the flag while Desktop is connected too.
- Claude Code's tier and subagent models the user had set for their own endpoint (`claudeOwnEnv`) are stashed with that endpoint and token when magpie is wired in, and come back with them when magpie steps out, by Disconnect or by Claude Code's own model (#1050). So does their main model in the env (`ANTHROPIC_MODEL`, a Bedrock model id beside `CLAUDE_CODE_USE_BEDROCK` for one), which magpie takes out while wired in because it outranks a pick in `/model`.
- Claude Code's tier and subagent models the user had set for their own endpoint (`claudeOwnEnv`) are stashed with that endpoint and token when magpie is wired in, and come back with them when magpie steps out, by Disconnect or by Claude Code's own model (#1050). So does their main model in the env (`ANTHROPIC_MODEL`, a Bedrock model id beside `CLAUDE_CODE_USE_BEDROCK` for one), which magpie takes out while wired in because it outranks a pick in `/model`. A model name of magpie's is never one of them (`claudeMagpieModel`): it is neither stashed as the user's nor given back from a stash an older magpie wrote, and a Claude Code on Anthropic's own endpoint (no `ANTHROPIC_BASE_URL`) has any left in its model or its env's models taken out by `Sync` at each start and when magpie steps out (`dropStray`), since Anthropic answers one with a 404 (roswellcsy on X: "There's an issue with the selected model (claude/claude-opus-5-5)" from `ANTHROPIC_MODEL`, given back on every Disconnect). Magpie's wiring at a gateway address since changed (its key, `keyOurs`) goes whole when magpie steps out, by Disconnect too. An endpoint of the user's own may name its models so, and they stay (`TestClaudeOffLeavesNoMagpieModel`).
- Claude Code's auto mode field (`auto_mode`, `claudeAutoModeEnv` in [`claude.go`](../../internal/agent/claude.go)) is `CLAUDE_CODE_AUTO_MODE_MODEL`, the model Claude Code 2.1.302's client-side auto mode classifier judges each action with (Linka on Discord). It changes nothing about server-side auto mode, and a classifier model Anthropic configures for the account comes before it. Unset, Claude Code picks its own (the sonnet tier's model, else Sonnet), so it follows no field and isn't `Follows`. It is set only while Claude Code is routed through magpie, only to one of magpie's models, dashed as the other model keys are, and never to Sonnet 5.5, Opus 5.5 or Opus 5 (`claudeNoClassifier`), which Claude Code 2.1.303 refuses there and replaces with its own pick. A value the user had before magpie routed Claude Code is stashed and taken out with their tiers (`claudeOwnEnv`), and comes back when magpie steps out, unless they set one again since; magpie's own value (`claude.auto_mode_model` in the stash, or any of magpie's models) goes then, and one the user set while wired in is left alone. Claude Code reads it as it goes, so it is Quiet and needs no restart.
- Claude Code's provider switches (`agentenv.ClaudeProviders`: `CLAUDE_CODE_USE_BEDROCK`, `_VERTEX`, `_FOUNDRY`, `_ANTHROPIC_AWS`, `_ANTHROPIC_GOOGLE_CLOUD`, `_MANTLE`, Claude Code 2.1.300) send it to that cloud's own endpoint and pass `ANTHROPIC_BASE_URL` over, so with one on it never reaches magpie (jw1dev on X: Bedrock refused magpie's model, and magpie showed nothing). Claude Code reads 1, true, yes and on as on (`claudeSwitchOn`). One that is on in the settings magpie writes is taken out on every route that writes the wiring (`dropClouds` in `writeTiers` and `Sync`), its raw value stashed (`claude.cloud.<var>`, under the distro's id for a WSL twin), and put back as written when magpie steps out (`putClouds` in `unroute`), unless the user set it again since. The cloud's own variables (`AWS_*`, `ANTHROPIC_VERTEX_PROJECT_ID`, …) and a switch that is off stay. Where magpie doesn't write it, Check says so, naming the variable, the cloud and where (`claudeCloud`): the managed settings, which win over the user's; `~/.claude.json`'s env, under the user's settings; and, for this machine's Claude Code, the login shell's environment (`proc.LoginEnv`, read but never set in magpie's), under every settings file. A project's `.claude/settings.json` isn't read: magpie doesn't know which folders Claude Code runs in. T3 Code's Check says the same of the user's `~/.claude` settings, which are put over its magpie instance's environment, and a switch put on that instance goes at its next write; that Check first says when the instance's `ANTHROPIC_BASE_URL` no longer reaches the gateway (`t3EnvOf`), as every agent's Check does, so a port move rewrites it too. The drift's detail is shown in the row's line and its pill through `tNotice` (`claude-cloud-drift.test.cjs`). Claude Desktop's Code tab runs with `CLAUDE_CODE_PROVIDER_MANAGED_BY_HOST`, under which Claude Code ignores a switch in settings files, so Desktop needs neither.
- Claude Code's sign-in (`login`) is magpie's key (`""`, the default) or claude.ai (`claudeai`). claude.ai writes `ANTHROPIC_AUTH_TOKEN` empty, which Claude Code reads as no key: it keeps its claude.ai login and sends that to magpie, which never passes it on (`claudeSignIn` in the gateway). Every later write keeps the token empty (`wiredKey`), and a caller key of the gateway's own put there by hand is taken for magpie's, by Check and a model pick alike, kept as the user left it (`keyHolds`, #2545); a key the gateway would refuse is reported by Check and wired over, and so is the sharing key magpie itself made (`access.Default`), once sharing is off — magpie's own, however well the gateway still takes it. It's offered only where the gateway takes any key (`gwKey() == gateway.Token`: loopback, a WSL distro mirrored), not from a distro under NAT while magpie is shared on the network, where only the sharing key gets in. The choice is stashed, so it survives Disconnect and a port move. An empty token counts as magpie's only beside the address magpie last wrote (`claude.wired_at` in the stash, which Rewire carries to the new port, `keyOurs`): an endpoint of the user's own with an empty token stays theirs when the model goes back to Claude Code's own.
+62 -4
View File
@@ -543,6 +543,16 @@ var claudeModelEnv = []string{
"ANTHROPIC_DEFAULT_HAIKU_MODEL", "ANTHROPIC_DEFAULT_FABLE_MODEL",
}
// claudeMagpieModel says a model in Claude Code's env or its model, as
// written there, is one of magpie's: a provider/model name only magpie's
// gateway serves, at an effort or marked 1M or not. On Anthropic's own
// endpoint it is a 404, "There's an issue with the selected model"
// (roswellcsy on X).
func claudeMagpieModel(v string) bool {
m, _ := tierAt(provider.ClaudeRead(v))
return isMagpie(strings.TrimSuffix(m, "[1m]"))
}
// claudeSameModel says two of a tier's models as written are one model,
// whatever effort each is fixed at and however its 1M is marked: what the
// env says about the one is about the other.
@@ -1368,6 +1378,31 @@ func claudeIn(at place) *Agent {
}
return edit.SetJSON(path, edit.KV{Path: "env." + claudeContextEnv, Value: v})
}
// dropStray takes magpie's model names out of a Claude Code on
// Anthropic's own endpoint, where each is a 404 ("There's an issue
// with the selected model (claude/claude-opus-5-5)", roswellcsy on X):
// left in its env, ANTHROPIC_MODEL over settings.json's model, by an
// older magpie that gave one back from the stash as the user's own on
// every Disconnect. The user's own models, and any at an endpoint of
// theirs, which may name its models so, are left as they are.
dropStray := func() error {
if env("ANTHROPIC_BASE_URL") != "" {
return nil
}
var keys []string
for _, k := range claudeModelEnv {
if claudeMagpieModel(rawEnv(k)) {
keys = append(keys, "env."+k)
}
}
if claudeMagpieModel(model()) {
keys = append(keys, "model")
}
if len(keys) == 0 {
return nil
}
return edit.DelJSON(path, keys...)
}
// desktopOnly is what is written while Claude Code itself isn't on
// magpie: Claude Desktop's Code tab still runs Claude Code on this
// settings.json while Desktop is, so the window and capabilities of
@@ -1520,7 +1555,18 @@ func claudeIn(at place) *Agent {
return "", err
}
if !routed() {
return "", nil
// magpie's, left at a gateway address since changed: its
// endpoint, key and models all go, as set("") takes them
if keyOurs() {
keys := make([]string, len(claudeEnv))
for i, k := range claudeEnv {
keys[i] = "env." + k
}
if err := edit.DelJSON(path, keys...); err != nil {
return "", err
}
}
return "", dropStray()
}
keys := make([]string, len(claudeEnv))
for i, k := range claudeEnv {
@@ -1541,7 +1587,9 @@ func claudeIn(at place) *Agent {
var back []edit.KV
for _, k := range claudeOwnEnv {
// one the user set again since is theirs as it is
if v := unstash(at.key("claude.env." + k)); v != "" && rawEnv(k) == "" {
// one of magpie's kept by an older magpie as the user's is no
// model of their endpoint's (roswellcsy on X)
if v := unstash(at.key("claude.env." + k)); v != "" && rawEnv(k) == "" && !(contains(claudeModelEnv, k) && claudeMagpieModel(v)) {
back = append(back, edit.KV{Path: "env." + k, Value: v})
}
}
@@ -1598,8 +1646,15 @@ func claudeIn(at place) *Agent {
at.key("claude.auth_token"): env("ANTHROPIC_AUTH_TOKEN"),
}
for _, k := range claudeOwnEnv {
// as the user wrote it, never respelled
kept[at.key("claude.env."+k)] = rawEnv(k)
// as the user wrote it, never respelled; one of
// magpie's own left there is not theirs to get back
// (roswellcsy on X: it came back as their
// ANTHROPIC_MODEL with every Disconnect)
v := rawEnv(k)
if contains(claudeModelEnv, k) && claudeMagpieModel(v) {
v = ""
}
kept[at.key("claude.env."+k)] = v
}
// the auto mode classifier's model of their own
// endpoint waits in the stash, out of the env, as
@@ -2228,6 +2283,9 @@ func claudeIn(at place) *Agent {
Sync: func() error {
ask()
if !routed() {
if err := dropStray(); err != nil {
return err
}
return desktopOnly()
}
// a switch turned on since, or left by an older magpie
+192
View File
@@ -0,0 +1,192 @@
package agent
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
// claudeOffHome is a sandbox HOME with a provider serving Claude models,
// as the user's Claude subscription serves claude/claude-opus-5-5, and
// Claude Code's settings.json holding before.
func claudeOffHome(t *testing.T, before string) (*Agent, string) {
t.Helper()
home := syncHome(t)
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Chat: "https://relay.example/v1", Key: "k",
Models: []string{"claude-opus-5-5", "claude-haiku-4-5"}}); err != nil {
t.Fatal(err)
}
path := filepath.Join(home, ".claude", "settings.json")
writeFile(t, path, before)
return claude(home), path
}
// claudeStartsOn is every model a fresh Claude Code started on settings.json
// at path asks its endpoint for, as Claude Code 2.1.316 reads them: its
// main model (ANTHROPIC_MODEL over settings.json's model, pG/Wg in its
// bundle), each alias's tier, the small fast and subagent models and the
// auto mode classifier's. With no ANTHROPIC_BASE_URL that endpoint is
// Anthropic's, which answers a provider/model name with a 404 Claude Code
// shows as "There's an issue with the selected model (…)".
func claudeStartsOn(t *testing.T, path string) (base string, models map[string]string) {
t.Helper()
var s struct {
Model string `json:"model"`
Env map[string]string `json:"env"`
}
b, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(b, &s); err != nil {
t.Fatalf("%v\n%s", err, b)
}
models = map[string]string{"main": s.Env["ANTHROPIC_MODEL"]}
if models["main"] == "" {
models["main"] = s.Model
}
for _, k := range claudeModelEnv {
if k != "ANTHROPIC_MODEL" {
models[k] = s.Env[k]
}
}
return s.Env["ANTHROPIC_BASE_URL"], models
}
// noMagpieOnAnthropic fails when Claude Code, on Anthropic's own endpoint,
// would ask for one of magpie's provider/model names.
func noMagpieOnAnthropic(t *testing.T, path, when string) {
t.Helper()
base, models := claudeStartsOn(t, path)
if base != "" {
t.Fatalf("%s: ANTHROPIC_BASE_URL is %q, want Anthropic's own\n%s", when, base, readFile(path))
}
for k, v := range models {
if strings.Contains(v, "/") {
t.Fatalf("%s: Claude Code on Anthropic's endpoint starts with %s = %q\n%s", when, k, v, readFile(path))
}
}
}
// roswellcsy on X (v0.1.2846): Claude Code switched off magpie, on its own
// sign-in, and every session said "There's an issue with the selected
// model (claude/claude-opus-5-5)" while settings.json's model was the
// bare claude-opus-5-5. A model name of magpie's left in Claude Code's env
// outranks or stands in for that model, and Anthropic has no such model.
// None is ever left there, given back from the stash, or kept as the
// user's own; one already there goes when magpie next looks.
func TestClaudeOffLeavesNoMagpieModel(t *testing.T) {
// what the user's settings.json held: the bare model, and magpie's
// names in the env with no endpoint of magpie's beside them
const stray = `{"model": "claude-opus-5-5", "env": {
"ANTHROPIC_MODEL": "relay/claude-opus-5-5",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "relay/claude-opus-5-5[1m]",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "relay/claude-haiku-4-5:low",
"ANTHROPIC_SMALL_FAST_MODEL": "relay/claude-haiku-4-5",
"CLAUDE_CODE_SUBAGENT_MODEL": "relay/claude-opus-5-5",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-4-6",
"MY_OWN": "x"}}`
t.Run("magpie's start takes them out", func(t *testing.T) {
a, path := claudeOffHome(t, stray)
SyncCatalog()
noMagpieOnAnthropic(t, path, "after a start")
get := func(k string) string { v, _ := edit.GetJSON(path, k); return v }
// the user's own stay as they are
if get("model") != "claude-opus-5-5" || get("env.ANTHROPIC_DEFAULT_SONNET_MODEL") != "claude-sonnet-4-6" || get("env.MY_OWN") != "x" {
t.Fatalf("the user's own values went:\n%s", readFile(path))
}
if a.Wired() {
t.Fatalf("taking magpie's names out connected Claude Code:\n%s", readFile(path))
}
})
t.Run("connected and switched off again", func(t *testing.T) {
a, path := claudeOffHome(t, stray)
if err := a.Connect(); err != nil {
t.Fatal(err)
}
if !a.Wired() {
t.Fatalf("not connected:\n%s", readFile(path))
}
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
noMagpieOnAnthropic(t, path, "after Disconnect")
// and again: nothing of magpie's was kept to come back
if err := a.Connect(); err != nil {
t.Fatal(err)
}
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
noMagpieOnAnthropic(t, path, "after a second Disconnect")
})
t.Run("kept by an older magpie", func(t *testing.T) {
// a stash an older magpie wrote, holding magpie's names as the
// user's own: never given back
a, path := claudeOffHome(t, `{"model": "claude-opus-5-5"}`)
if err := a.Connect(); err != nil {
t.Fatal(err)
}
at := here(os.Getenv("HOME"))
stash(map[string]string{
at.key("claude.env.ANTHROPIC_MODEL"): "relay/claude-opus-5-5",
at.key("claude.env.ANTHROPIC_DEFAULT_OPUS_MODEL"): "relay/claude-opus-5-5[1m]",
at.key("claude.env.CLAUDE_CODE_SUBAGENT_MODEL"): "relay/claude-opus-5-5:high",
})
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
noMagpieOnAnthropic(t, path, "after Disconnect")
})
t.Run("the user's own model comes back", func(t *testing.T) {
a, path := claudeOffHome(t, `{"model": "claude-opus-5-5", "env": {"ANTHROPIC_MODEL": "claude-opus-5-5", "ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude-haiku-4-5"}}`)
if err := a.Field("model").Set("relay/claude-opus-5-5"); err != nil {
t.Fatal(err)
}
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
get := func(k string) string { v, _ := edit.GetJSON(path, k); return v }
if get("env.ANTHROPIC_MODEL") != "claude-opus-5-5" || get("env.ANTHROPIC_DEFAULT_HAIKU_MODEL") != "claude-haiku-4-5" {
t.Fatalf("the user's own models not back:\n%s", readFile(path))
}
})
t.Run("an older gateway address", func(t *testing.T) {
// magpie's wiring at an address it no longer serves (a port
// changed): its key, so magpie's, and all of it goes
a, path := claudeOffHome(t, `{"model": "relay/claude-opus-5-5", "env": {
"ANTHROPIC_BASE_URL": "http://127.0.0.1:1",
"ANTHROPIC_AUTH_TOKEN": "`+gateway.Token+`",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "relay/claude-opus-5-5",
"ANTHROPIC_SMALL_FAST_MODEL": "relay/claude-haiku-4-5"}}`)
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
noMagpieOnAnthropic(t, path, "after Disconnect")
if v, has := edit.GetJSON(path, "env.ANTHROPIC_AUTH_TOKEN"); has {
t.Fatalf("magpie's key left: %q\n%s", v, readFile(path))
}
})
t.Run("another endpoint's names are its own", func(t *testing.T) {
// a relay of the user's own may name its models so: magpie
// leaves them
const own = `{"env": {"ANTHROPIC_BASE_URL": "https://mine.example", "ANTHROPIC_AUTH_TOKEN": "sk-mine", "ANTHROPIC_MODEL": "relay/claude-opus-5-5"}}`
_, path := claudeOffHome(t, own)
SyncCatalog()
if v, _ := edit.GetJSON(path, "env.ANTHROPIC_MODEL"); v != "relay/claude-opus-5-5" {
t.Fatalf("a model of the user's own endpoint went: %q\n%s", v, readFile(path))
}
})
}