agent: Claude Code's /model takes models added in magpie again when the picker magpie wrote has lost its mark (#2046, cjyrainbow)

cjyrainbow's WSL Claude Code (Ubuntu) kept an old /model list: a model
added in magpie was in the gateway's /v1/models but not in modelPicker.
settings.json held the picker magpie wrote (every row "… · via magpie"),
but stash.json had no claude@wsl:Ubuntu.model_picker mark for it, so
writePicker took it for the user's own and never wrote it again. An
older magpie's stash lost keys (concurrent writes / a stash that didn't
parse, fixed in 270b5257), which is the likely way the mark went.

claudeIn's pickerOurs now also counts a picker whose every row has a
model and a description ending "via magpie" (magpiesPicker) as magpie's.
That holds for native and every WSL distro, since all go through
claudeIn with their own place key. Sync rewrites it and marks it again;
Disconnect removes it, as it does a marked one. A picker with any row of
the user's own (another description) stays theirs.

docs/subsystems/agent-wiring.md says when the picker is magpie's.

Verification:
- New TestClaudePickerMagpieWroteIsKeptUpToDateWithoutItsMark, native
  and wsl subtests: the reporter's state (picker written by magpie, mark
  forgotten, a model added, Sync) and a user-written picker after it.
- Broke it by hand: with the old pickerOurs both subtests fail "a model
  added in magpie isn't in /model: [cpa/gpt-6-luna cpa/claude-sonnet-4-6]";
  with the fingerprint ignoring descriptions both fail "the user's own
  picker was written over".
- go test -tags nogui ./internal/agent/ (count=1): ok, 1412s, on
  origin/main 591ed58b5.
- gofmt -l clean; go vet ./internal/agent/ on darwin, GOOS=linux -tags
  nogui and GOOS=windows.
- Not tried against a real WSL distro or the reporter's settings.json.
This commit is contained in:
yetone
2026-10-14 05:29:56 +08:00
parent b6cf5e3eab
commit 59a8aec5df
3 changed files with 128 additions and 2 deletions
+1
View File
@@ -56,6 +56,7 @@ something else shows up as drift.
- 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 key put there by hand is reported by Check, not taken for magpie's. 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.
- Claude Code's auto mode review (`CLAUDE_CODE_AUTO_MODE_SERVER`, `claudeReviewEnv` in [`claude.go`](../../internal/agent/claude.go)) is written `"0"` while Claude Code is routed through magpie and none of the models it can reach there is one Anthropic reviews (`claudeReviewed`, by `gateway.AutoModeReviewed`): those it is shown (and Claude Desktop's while Desktop is on magpie), a shown group's members, those providers' fallbacks, and the models it is set to. Off, it classifies locally from the start, as it does anyway behind a gateway that doesn't answer, without the "isn't eligible because your requests go through 127.0.0.1:3425" notice (1EFLOPS on X). Any reviewed model leaves it unset, since `/model` can take the session there; a model hidden from Claude Code doesn't count. It is rewritten on every model set and `Sync`, so adding a Claude provider turns the review back on. magpie's value is kept apart in the stash (`claude.auto_mode_server`), a user's own is left alone, and it goes when Claude Code is taken off magpie (`desktopOnly`, `unroute`). T3 Code's magpie instance, which runs Claude Code on the gateway through its own environment, carries the same `"0"` there by the models it lists (`t3InstanceJSON` in [`t3code.go`](../../internal/agent/t3code.go)); a value in the user's `~/.claude` settings comes after it.
- MiniMax Code takes a model ref with one slash in the whole (a SubAgent's `model` must match `^[^/\s]+\/[^/\s]+$`, #1387), and magpie's refs there are `custom_provider:magpie/<model>`. So a model under `custom_provider.magpie` is written with `~` for each slash of its catalog id (`bb-codex~gpt-6.1-sol`, `provider.FlatID`), only when that spelling stands for one served id (`provider.Flattened`); the gateway takes it back. A config an older magpie wrote moves over on the next sync: the user's keys on a model go to its new key, `defaultModel`, `defaultLightModel` and `memory.captureModel`/`consolidationModel` follow (`mcodeRefs`), and the slashed key stays, switched off (`enabled: false`), so a session begun on it still resumes. magpie never edits the user's own `agents/<name>/agent.md`. No other agent was found with this limit ([`minimax.go`](../../internal/agent/minimax.go)).
- Claude Code's `/model` list (`modelPicker` in its settings.json) is magpie's while it is marked in the stash (`claude.model_picker`, `claude@wsl:<distro>.model_picker` for a WSL distro) or while every row's description ends "via magpie", as every row magpie writes does (`magpiesPicker` in [`claude.go`](../../internal/agent/claude.go)). magpie keeps its own list up to date on every model set and `Sync`, and removes it when Claude Code is taken off magpie; a list the user wrote is left alone. The fingerprint covers a list whose mark an older magpie's stash lost, which otherwise stayed without every model added since (#2046).
- Claude Code 2.1.296 reads a Claude model whose version has a dot (`relay/claude-opus-4.6-us`) as Claude Opus 4: a retired warning, budget thinking, 32000 output tokens, no effort. With dashes (`relay/claude-opus-4-6-us`) it knows the model (misteer_ti. on Discord). So every model magpie writes into its settings (`model`, the tier envs, `ANTHROPIC_SMALL_FAST_MODEL`, `CLAUDE_CODE_SUBAGENT_MODEL`, `modelPicker`), T3 Code's Claude slugs and Claude Desktop's Code-tab ids (`claudeLooking`) spell a dotted Claude version with dashes (`provider.ClaudeSpelled`), only when that spelling stands for one served id. A Claude model listed to Desktop with reasoning levels is given as `magpie-<n>.anthropic.<model>`, its dotted version dashed as well (`claudeModel`: Copilot's `claude-opus-5.5` is `…anthropic.claude-opus-5-5`, which Claude Code knows; it was `mythos-magpie-<n>`, an unknown model, and Haiku 4.5 got an effort Copilot refuses, #1819); a request for it is served by `<n>` alone (`aliased`), so that spelling needn't be unique. Mythos counts as Claude here too: Claude Code matches `claude-mythos-5-1` by substring, so `claude-mythos-5.1` would read as Mythos 5. Every read of those keys takes it back (`provider.ClaudeRead`: `claudeIn`'s `env`, `claudeMain`, `claudeStandInAt`), so magpie's fields, applied state and Drift keep the dotted id. The user's own values are stashed and given back as written. Values an older magpie wrote dotted are rewritten on the next Sync. Other vendors' ids are never respelled ([`claudedash.go`](../../internal/provider/claudedash.go)).
- Codex's "subagents" field is `[agents] default_subagent_model`: only the model a subagent starts on when its lead names none in `spawn_agent` (codex-rs `core/src/agent/child_config.rs`). Unset, it isn't "same as model": the lead may still name another, so the GUI calls it the lead's pick, else the model (`leadPicks` in app.js), and its title points at the "subagent model" field.
- Codex's "subagent model" field writes nothing to Codex's config: its lead names each subagent's model in `spawn_agent`, which `[agents]` can't overrule. It is magpie's setting (`settings.CodexSubagentModel`, [`codex_subagent.go`](../../internal/provider/codex_subagent.go)), and the gateway moves the subagent's request onto it (see [gateway routing](gateway-routing.md)). Its options are every model and routing group Codex's own lists show (`CatalogFor("codex")`, so a model taken out of them or not picked is left out here too), ChatGPT accounts' first (#1633: a lead on a chat provider wants its subagents there too). A task a ChatGPT-backed lead sealed opens only on a ChatGPT account, so the gateway keeps such a subagent on its own model when the one set is another provider's. Its setter takes any `provider/model` or `group/<id>`, also one magpie doesn't serve now, so a profile or backup put back on another computer still applies (`ApplyFields` stops at the first failed Set), and refuses one of Codex's own bare names.
+31 -2
View File
@@ -712,9 +712,13 @@ func claudeIn(at place) *Agent {
// groups first as viaMagpie lists them, so the user picks among them
// in Claude Code itself. Groups were left out, so one magpie had just
// made the main model and every tier wasn't in /model (#1575). One the
// user wrote is theirs.
// user wrote is theirs. One whose every row says "via magpie" is
// magpie's though its mark is gone (an older magpie's stash lost it),
// so it is kept up to date rather than left as the user's: a WSL
// Claude Code's /model kept a list without the models added since
// (#2046, cjyrainbow).
pickerKey := at.key("claude.model_picker")
pickerOurs := func() bool { return stashLoad()[pickerKey] != "" }
pickerOurs := func() bool { return stashLoad()[pickerKey] != "" || magpiesPicker(path) }
dropPicker := func() error {
defer forget(pickerKey)
if _, has := edit.GetJSON(path, "modelPicker"); has && pickerOurs() {
@@ -2078,3 +2082,28 @@ var (
noticeClaudeEffort = newNotice("an open Claude Code session keeps the effort it started with — restart it to use this.")
noticeClaudeUltracode = newNotice("an open Claude Code session keeps the ultracode it started with — restart it to use this.")
)
// magpiesPicker says whether settings.json's modelPicker is one magpie
// wrote: it has rows, and each one's description is magpie's, which ends
// "via magpie" (writePicker in claudeIn).
func magpiesPicker(path string) bool {
raw, has := edit.GetJSON(path, "modelPicker")
if !has {
return false
}
var p struct {
Options []struct {
Model string `json:"model"`
Description string `json:"description"`
} `json:"options"`
}
if json.Unmarshal([]byte(raw), &p) != nil || len(p.Options) == 0 {
return false
}
for _, o := range p.Options {
if o.Model == "" || !strings.HasSuffix(o.Description, "via magpie") {
return false
}
}
return true
}
@@ -0,0 +1,96 @@
package agent
import (
"encoding/json"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
// cjyrainbow (#2046): a WSL Claude Code's settings.json held a modelPicker
// magpie had written, but stash.json had no claude@wsl:Ubuntu.model_picker
// mark for it, so writePicker took it for the user's and never touched it
// again: a model added in magpie since was in the gateway's /v1/models and
// not in /model. A picker whose every row is magpie's ("… · via magpie") is
// kept up to date though its mark is gone; one the user wrote stays as it is.
func TestClaudePickerMagpieWroteIsKeptUpToDateWithoutItsMark(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("APPDATA", filepath.Join(home, "AppData", "Roaming"))
t.Setenv("LOCALAPPDATA", filepath.Join(home, "AppData", "Local"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
t.Setenv("CLAUDE_CONFIG_DIR", "")
root := t.TempDir()
d := distro{Name: "Ubuntu", Home: "/home/me", Root: root, Running: true, Mirrored: true}
for _, c := range []struct {
name string
at place
mark string
}{
{"native", here(home), "claude.model_picker"},
{"wsl", d.place("claude@wsl:Ubuntu"), "claude@wsl:Ubuntu.model_picker"},
} {
t.Run(c.name, func(t *testing.T) {
if err := provider.Save(provider.Provider{ID: "cpa", Name: "CPA", Chat: "http://127.0.0.1:1/v1", Key: "k", Models: []string{"gpt-6-luna", "claude-sonnet-4-6"}}); err != nil {
t.Fatal(err)
}
a := claudeIn(c.at)
path := filepath.Join(c.at.claudeDir(), "settings.json")
writeFile(t, path, `{"theme":"dark"}`)
if err := a.Apply("model", "cpa/gpt-6-luna"); err != nil {
t.Fatal(err)
}
listed := func() []string {
t.Helper()
var p struct{ Options []map[string]string }
raw, _ := edit.GetJSON(path, "modelPicker")
if err := json.Unmarshal([]byte(raw), &p); err != nil {
t.Fatalf("modelPicker: %v\n%s", err, readFile(path))
}
var out []string
for _, o := range p.Options {
out = append(out, strings.TrimSuffix(o["model"], "[1m]"))
}
return out
}
if !slices.Contains(listed(), "cpa/gpt-6-luna") {
t.Fatalf("picker after connecting: %v", listed())
}
// the reporter's state: the picker magpie wrote, its mark gone
forget(c.mark)
if err := provider.Save(provider.Provider{ID: "cpa", Name: "CPA", Chat: "http://127.0.0.1:1/v1", Key: "k", Models: []string{"gpt-6-luna", "claude-sonnet-4-6", "gpt-6.1-sol"}}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if !slices.Contains(listed(), "cpa/gpt-6.1-sol") {
t.Fatalf("a model added in magpie isn't in /model: %v\n%s", listed(), readFile(path))
}
if stashLoad()[c.mark] == "" {
t.Fatalf("the picker magpie keeps up to date has no %s mark", c.mark)
}
// a picker the user wrote stays theirs
forget(c.mark)
mine := map[string]any{"options": []any{map[string]any{"model": "cpa/gpt-6-luna", "label": "Luna", "description": "mine"}}}
if err := edit.SetJSON(path, edit.KV{Path: "modelPicker", Value: mine}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if got := listed(); !slices.Equal(got, []string{"cpa/gpt-6-luna"}) {
t.Fatalf("the user's own picker was written over: %v", got)
}
})
}
}