From fe8c05e0c847ebb8c089ab930d0f9007d48dc23e Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 11 Nov 2026 15:03:29 +0800 Subject: [PATCH] agent: Claude's model variables hold Anthropic's own ids on Claude Code's own subscription, so Desktop's Code tab stops failing WebFetch (iryan_sun on X) ANTHROPIC_DEFAULT__MODEL, ANTHROPIC_SMALL_FAST_MODEL, CLAUDE_CODE_SUBAGENT_MODEL and the auto mode model stayed "claude/claude-haiku-4-5[1m]" after the main model became an alias. A tier variable can't be an alias of itself, so on this computer's own file a Claude model of Claude Code's own subscription is now written as Anthropic's bare id (provider.ClaudeBare): through magpie that id goes to the same subscription (OwnRoute), and Anthropic knows it. Haiku has no 1M window, so its [1m] mark is taken off. Read back, a bare Claude id is the subscription's ref (ClaudeUnbare), so every field, the tiers' follow logic and the gateway's stand-in read it as before. Old files: a settings.json written with the prefixed ids is rewritten by the next Sync (the check that already brought dashed spellings up to date), for every user. Models of other vendors or a relay keep their ref: Anthropic has no id for them. A distro's file and the claude-magpie launch file keep refs. Run (wsl-run, Ubuntu, at the pushed head after rebasing on origin/main): internal/agent -run "Claude|Tier|Folder" ok; internal/provider ok; gofmt -l clean; go vet -tags nogui for linux, darwin and windows clean. Before the rebase, the whole internal/agent package had two reds: TestFolderVarsAreListed (BUN_INSTALL, fixed on main since) and TestGrokInstalled (WSL2 binfmt, already #3548). Neither touches this change. Real claude 2.1.316 with a fake upstream showed Desktop's PROVIDER_MANAGED_BY_HOST sends the tier env as written, so prefixed ids go to Anthropic. Broken by hand: with ClaudeBare turned into a no-op, TestClaudeModelVariablesAreAnthropicsOwnIDs fails on the bare ids and on the Sync rewrite. Not tried: the real Claude Desktop Code tab, a WSL distro user, real accounts through magpie end to end. A non-Claude model or a relay-served Claude model still has no Anthropic id, so its tier variable keeps the ref and Desktop's Code tab still can't use it. --- internal/agent/claude.go | 33 +++++-- internal/agent/claude_bare_models_test.go | 107 ++++++++++++++++++++++ internal/provider/claudedash.go | 50 ++++++++++ 3 files changed, 181 insertions(+), 9 deletions(-) create mode 100644 internal/agent/claude_bare_models_test.go diff --git a/internal/agent/claude.go b/internal/agent/claude.go index fda1010a3..0f9898a78 100644 --- a/internal/agent/claude.go +++ b/internal/agent/claude.go @@ -660,13 +660,21 @@ func claudeIn(at place) *Agent { env := func(k string) string { v := rawEnv(k) if contains(claudeModelEnv, k) { - v = provider.ClaudeRead(v) + v = claudeReadOwn(v) } return v } // spell is a model of magpie's as Claude Code is given it: a dotted // Claude version dashed, which it would read as Claude Opus 4 - spell := provider.ClaudeSpelled + // and on this computer's own file a Claude model of Claude Code's own + // subscription is Anthropic's bare id (ClaudeBare), which Desktop's Code + // tab can send to Anthropic as it is + spell := func(ref string) string { + if at.id == "" { + ref = provider.ClaudeBare(ref) + } + return provider.ClaudeSpelled(ref) + } model := jsonGet(path, "model") // routed: on magpie's gateway, or on a proxy to it where magpie has // answered (#1985, claudeProxied), which a pick keeps (gwAt) @@ -2398,8 +2406,8 @@ func claudeIn(at place) *Agent { raw = append(raw, rawEnv(k)) } if slices.ContainsFunc(raw, func(v string) bool { - m, _ := tierAt(provider.ClaudeRead(v)) - return v != "" && isMagpie(m) && spell(provider.ClaudeRead(v)) != v + m, _ := tierAt(claudeReadOwn(v)) + return v != "" && isMagpie(m) && spell(claudeReadOwn(v)) != v }) { return writeTiers(curTiers()) } @@ -2408,7 +2416,7 @@ func claudeIn(at place) *Agent { if m, ts := curTiers(); m != "" { word, cur := mainWord(m, ts), jsonGet(path, "model")() if cur != word && word != spell(m) { - if r, _ := tierAt(provider.ClaudeRead(cur)); isMagpie(strings.TrimSuffix(r, "[1m]")) { + if r, _ := tierAt(claudeReadOwn(cur)); isMagpie(strings.TrimSuffix(r, "[1m]")) { return writeTiers(m, ts) } } @@ -2985,7 +2993,7 @@ func ClassifierStandIn(agent, model string) string { // mode model set for the classifier, is left as it is. func claudeClassifierIn(at place, model string) string { path := filepath.Join(at.claudeDir(), "settings.json") - env := func(k string) string { v, _ := edit.GetJSON(path, "env."+k); return provider.ClaudeRead(v) } + env := func(k string) string { v, _ := edit.GetJSON(path, "env."+k); return claudeReadOwn(v) } if u := env("ANTHROPIC_BASE_URL"); u != at.gw() && !claudeProxied(at, path, u) { return "" } @@ -2998,7 +3006,7 @@ func claudeClassifierIn(at place, model string) string { } bare := func(v string) string { m, _ := tierAt(v); return strings.TrimSuffix(m, "[1m]") } tier, effort := tierAt(env(tierEnv("sonnet"))) - if tier == "" || bare(provider.ClaudeRead(model)) != bare(tier) { + if tier == "" || bare(claudeReadOwn(model)) != bare(tier) { return "" } was := bare(cmp.Or(st[at.key("claude.main")], env("ANTHROPIC_MODEL"))) @@ -3017,7 +3025,7 @@ func claudeStandIn(path, model string) string { return claudeStandInAt(here(""), // claudeStandInAt is claudeStandIn for the Claude Code at a place, which // reaches the gateway at its own address. func claudeStandInAt(at place, path, model string) string { - env := func(k string) string { v, _ := edit.GetJSON(path, "env."+k); return provider.ClaudeRead(v) } + env := func(k string) string { v, _ := edit.GetJSON(path, "env."+k); return claudeReadOwn(v) } if u := env("ANTHROPIC_BASE_URL"); u != at.gw() && !claudeProxied(at, path, u) { return "" } @@ -3111,10 +3119,17 @@ func claudeMain(path string) string { if m == "" { m = env(tierEnv("sonnet")) } - m, _ = tierAt(provider.ClaudeRead(m)) + m, _ = tierAt(claudeReadOwn(m)) return m } +// claudeReadOwn is a model as settings.json says it, read as magpie serves +// it: a dashed spelling taken back (ClaudeRead), a Claude model by its bare +// id taken to Claude Code's own subscription (ClaudeBare). Only where +// Claude Code goes through magpie: on Anthropic's own endpoint a bare id is +// Anthropic's, not magpie's (claudeMagpieModel). +func claudeReadOwn(v string) string { return provider.ClaudeRead(provider.ClaudeUnbare(v)) } + // what an open Claude Code session keeps (notice.go) var ( noticeClaudeEffort = newNotice("an open Claude Code session keeps the effort it started with — restart it to use this.") diff --git a/internal/agent/claude_bare_models_test.go b/internal/agent/claude_bare_models_test.go new file mode 100644 index 000000000..1d05aa060 --- /dev/null +++ b/internal/agent/claude_bare_models_test.go @@ -0,0 +1,107 @@ +package agent + +import ( + "os" + "path/filepath" + "strconv" + "strings" + "testing" + "time" + + "github.com/yetone/magpie/internal/edit" + "github.com/yetone/magpie/internal/provider" +) + +// Claude Desktop's Code tab on the user's own subscription sends the model +// variables of settings.json to Anthropic as they stand (iryan_sun on X: +// ANTHROPIC_SMALL_FAST_MODEL and the haiku tier were still +// "claude/claude-haiku-4-5[1m]", and WebFetch failed with "There's an issue +// with the selected model"). A Claude model of Claude Code's own +// subscription goes into every model variable as Anthropic's bare id; a +// model of another vendor, or a relay's, has no such id and stays. +func TestClaudeModelVariablesAreAnthropicsOwnIDs(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + t.Setenv("CLAUDE_CONFIG_DIR", "") + write := func(p, s string) { + t.Helper() + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(p, []byte(s), 0o600); err != nil { + t.Fatal(err) + } + } + exp := time.Now().Add(time.Hour).UnixMilli() + write(filepath.Join(home, ".claude", ".credentials.json"), `{"claudeAiOauth":{"accessToken":"sk-ant-oat01-x","refreshToken":"sk-ant-ort01-x","expiresAt":`+strconv.FormatInt(exp, 10)+`,"subscriptionType":"max","scopes":["user:inference","user:profile"]}}`) + write(filepath.Join(home, ".claude.json"), `{"oauthAccount":{"emailAddress":"own@example.com"},"additionalModelOptionsCache":[{"value":"claude-sonnet-4-6","label":"S"},{"value":"claude-haiku-4-5","label":"H"},{"value":"claude-opus-4-7","label":"O"}]}`) + if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash"}}); err != nil { + t.Fatal(err) + } + provider.ForgetAccounts() + if p, err := provider.Find("claude"); err != nil || p.Account == nil { + t.Fatalf("no Claude Code account in the sandbox: %v", err) + } + path := filepath.Join(home, ".claude", "settings.json") + write(path, `{}`) + a := claude(home) + main := "claude/claude-sonnet-4-6" + if err := a.Field("model").Set(main); err != nil { + t.Fatal(err) + } + if err := a.Field("haiku").Set("claude/claude-haiku-4-5[1m]"); err != nil { + t.Fatal(err) + } + if err := a.Field("opus").Set("deepseek/pro"); err != nil { + t.Fatal(err) + } + get := func(k string) string { v, _ := edit.GetJSON(path, "env."+k); return v } + for k, want := range map[string]string{ + "ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-4-6", + "ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude-haiku-4-5", + "ANTHROPIC_SMALL_FAST_MODEL": "claude-haiku-4-5", + "ANTHROPIC_DEFAULT_OPUS_MODEL": "deepseek/pro", // no id Anthropic knows + } { + if got := strings.TrimSuffix(get(k), "[1m]"); got != want { + t.Errorf("%s is %q, want %q", k, got, want) + } + } + if got := get("ANTHROPIC_DEFAULT_HAIKU_MODEL"); strings.HasSuffix(got, "[1m]") { + t.Errorf("haiku has no 1M window to mark: %q", got) + } + // magpie reads them back as the subscription's models + if got := a.Field("haiku").Get(); got != "claude/claude-haiku-4-5" { + t.Errorf("haiku shown as %q", got) + } + if got := claudeMain(path); got != main { + t.Errorf("main read as %q", got) + } + // a settings.json an older magpie wrote is rewritten by the next sync + for k, v := range map[string]string{ + "ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude/claude-haiku-4-5[1m]", + "ANTHROPIC_SMALL_FAST_MODEL": "claude/claude-haiku-4-5[1m]", + "CLAUDE_CODE_SUBAGENT_MODEL": "claude/claude-sonnet-4-6", + } { + if err := edit.SetJSON(path, edit.KV{Path: "env." + k, Value: v}); err != nil { + t.Fatal(err) + } + } + if err := a.Sync(); err != nil { + t.Fatal(err) + } + for _, k := range []string{"ANTHROPIC_DEFAULT_HAIKU_MODEL", "ANTHROPIC_SMALL_FAST_MODEL", "CLAUDE_CODE_SUBAGENT_MODEL"} { + if v := get(k); strings.Contains(v, "/") || strings.HasSuffix(v, "[1m]") { + t.Errorf("after Sync %s is still %q", k, v) + } + } + // the user's own bare model on a tier magpie doesn't hold stays theirs + if err := a.Sync(); err != nil { + t.Fatal(err) + } + if got := get("ANTHROPIC_SMALL_FAST_MODEL"); got != "claude-haiku-4-5" { + t.Errorf("a second Sync moved it to %q", got) + } +} diff --git a/internal/provider/claudedash.go b/internal/provider/claudedash.go index 53a66d654..daec28114 100644 --- a/internal/provider/claudedash.go +++ b/internal/provider/claudedash.go @@ -129,3 +129,53 @@ func offEntries() []Entry { } return out } + +// claudeOwnModel is a Claude model's id as Anthropic names it. +var claudeOwnModel = lazyre.New(`^claude-(?:opus|sonnet|haiku|fable|mythos)-\d+(?:-\d+)?(?:-\d{8})?$`) + +// ClaudeBare is ref (a served "provider/model", with a [1m] mark or not) +// as Anthropic names it, without the provider, when ref is a Claude model +// on Claude Code's own subscription in magpie and the bare id goes to that +// same model (OwnRoute). Claude Desktop's Code tab on the user's own +// subscription reads the model variables of settings.json and sends them to +// Anthropic, which turns "anth/claude-haiku-5-5[1m]" away ("There's an +// issue with the selected model", iryan_sun on X); through magpie the bare +// id arrives at the same subscription. Haiku has no 1M window to mark, so +// its mark is taken off. Anything else is as it is: another vendor's model +// or a relay's has no id Anthropic knows, and one with an effort fixed +// on it names that effort only through its ref. +func ClaudeBare(ref string) string { + id, rest := claudeSplit(ref) + if strings.Contains(rest, ":") { + return ref + } + i := strings.Index(id, "/") + if i <= 0 { + return ref + } + model := id[i+1:] + if !claudeOwnModel.MatchString(model) { + return ref + } + if to, _, ok := OwnRoute("claude", model); !ok || to != id { + return ref + } + if strings.HasPrefix(model, "claude-haiku-") { + return model + } + return model + rest +} + +// ClaudeUnbare is ClaudeBare taken back: a Claude model of Anthropic's by +// its bare id, which magpie sends to Claude Code's own subscription, is +// that subscription's ref. Any other value is as it is. +func ClaudeUnbare(v string) string { + id, rest := claudeSplit(v) + if strings.Contains(rest, ":") || !claudeOwnModel.MatchString(id) { + return v + } + if to, _, ok := OwnRoute("claude", id); ok { + return to + rest + } + return v +}