From 84cd686afae907ca4a6665ad9f93ae5306218eb5 Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 17 Oct 2026 16:09:46 +0800 Subject: [PATCH] agent: a Claude Code tier picked as the main model stays its own, not "Same as main model" (#2654) A tier's Set marked a pick as the user's own (claude.tier_own.) only when it equalled the main model and the tier would follow some other model. For opus and sonnet, and haiku on a non-Claude main model, the main model is what they follow, so the explicit pick was forgotten: it read back as "Same as main model" and the next main-model move took it along. Now any non-empty pick is the user's own; only the empty value ("Same as main model") follows. The subagents had the same hole: their Set always forgot the mark, so CLAUDE_CODE_SUBAGENT_MODEL given the main model was dropped on the next write. They now keep a pick the same way. A haiku tier given the Haiku it follows on a Claude main model is kept too (it went to the new main model before). No other agent has this follow-main logic (Follows: "model" is Claude's tiers and subagents only; Aside's roles store their picks directly). The GUI shows "follows the main model" only for an empty value, so it is right once the value reads back. TestClaudeTierPickedAsTheMainModelStays (reporter's case, as described in the issue) covers every tier and the subagents, on a non-Claude and a Claude main model, with the main model moved in magpie and in Claude Code's /model, then "Same as main model" following again. With claude.go reverted to origin/main it fails in 16 subtests (opus, sonnet, haiku and subagent on both mains and routes, plus haiku given its Haiku); fable and haiku=Opus already passed. TestClaudeTierEffort set haiku to the main model by name and expected it to read as following, which is this bug; it now picks "Same as main model". Run at this head (go1.26.8, load average 120-200): - gofmt -l: clean; go vet ./... (darwin), GOOS=linux and GOOS=windows go vet -tags nogui ./...: clean. - go test -tags nogui -timeout 45m ./...: 64 packages ok; internal/agent failed TestClaudeTierEffort (fixed above) and then, with internal/gateway, hit the 45m timeout under the machine's load (agent stuck in an fsync, gateway in TestPluginRestrictedAccountSkipped). - go test -tags nogui -timeout 120m ./internal/agent ./internal/gateway with the test fixed: ok (3472s, 2262s). - Rebased onto origin/main cb05dc2d1; the commits since the run above touch no internal/agent file. At the rebased head: go vet -tags nogui ./internal/... clean; go test -tags nogui -run TestClaude ./internal/agent ok (1456s). No GUI change, so the GUI suite was not run. Co-authored-by: ypli0629 <22133189+ypli0629@users.noreply.github.com> --- internal/agent/claude.go | 25 +++--- internal/agent/claude_tier_pick_test.go | 98 ++++++++++++++++++++++++ internal/agent/claude_tiereffort_test.go | 7 +- 3 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 internal/agent/claude_tier_pick_test.go diff --git a/internal/agent/claude.go b/internal/agent/claude.go index 3538b39fd..bf75b9b5c 100644 --- a/internal/agent/claude.go +++ b/internal/agent/claude.go @@ -1715,16 +1715,16 @@ func claudeIn(at place) *Agent { main, tiers := curTiers() // its effort, if it has one, goes with it to the new model _, e := tierAt(tiers[tier]) - // the main model picked where the tier would follow another - // is the user's own; any other model is by itself - if v != "" && strings.TrimSuffix(v, "[1m]") == strings.TrimSuffix(main, "[1m]") && follow(tier, main) != main { - stash(map[string]string{ownKey(tier): "1"}) - } else { - forget(ownKey(tier)) - } + // a model picked is the user's own, the main model or the + // one the tier would follow too, so it stays when the main + // model moves (#2654: a sonnet tier given the main model + // read as "Same as main model" and went with its next + // move); only "Same as main model" follows if v != "" { + stash(map[string]string{ownKey(tier): "1"}) tiers[tier] = tierWith(v, e) } else { + forget(ownKey(tier)) tiers[tier] = followAt(tier, main, e) } return writeTiers(main, tiers) @@ -1766,8 +1766,15 @@ func claudeIn(at place) *Agent { return fmt.Errorf("subagents: %q is not a model magpie serves", v) } _, e := subagentAt() - // a pick of its own is the user's anew - forget(ownKey("subagent")) + // a model picked is the user's own, the main model too + // (#2654: given the main model, the subagents' was left out + // and they followed the main model's next move); only "Same + // as main model" follows + if v != "" { + stash(map[string]string{ownKey("subagent"): "1"}) + } else { + forget(ownKey("subagent")) + } return setSubagent(v, e) }, Options: func(map[string]string) []Option { diff --git a/internal/agent/claude_tier_pick_test.go b/internal/agent/claude_tier_pick_test.go new file mode 100644 index 000000000..2ee91b45b --- /dev/null +++ b/internal/agent/claude_tier_pick_test.go @@ -0,0 +1,98 @@ +package agent + +import ( + "path/filepath" + "strings" + "testing" + + "github.com/yetone/magpie/internal/edit" + "github.com/yetone/magpie/internal/provider" +) + +// A tier, or the subagents, given a model picked from the list keeps it +// when the main model moves, though the pick is the main model itself or +// the model the tier would follow on it, picked in magpie with the main +// model moved in magpie or in Claude Code's /model (#2654, ypli0629: a +// sonnet tier given K2.8 Preview, the main model, read as "Same as main +// model", and the move to GLM-6 took it along). "Same as main model" +// picked afterwards has it follow again. +func TestClaudeTierPickedAsTheMainModelStays(t *testing.T) { + type pick struct{ main, key, model string } + var picks []pick + for _, main := range []string{"oa/k2.8-preview", "anth/claude-opus-5-5"} { + for _, k := range append(append([]string{}, claudeTiers...), "subagent") { + picks = append(picks, pick{main, k, main}) + } + } + // haiku on a Claude main model follows its Haiku: that Haiku picked + picks = append(picks, pick{"anth/claude-opus-5-5", "haiku", "anth/claude-haiku-4-5"}) + for _, p := range picks { + for _, via := range []string{"magpie", "/model"} { + t.Run(p.main+"/"+p.key+"="+p.model+"/"+via, func(t *testing.T) { + home, _ := codexHome(t, "", "") + a := claude(home) + if err := provider.Save(provider.Provider{ID: "anth", Name: "Anthropic", Chat: "https://api.anthropic.com/v1", Key: "k", + Models: []string{"claude-opus-5-5", "claude-haiku-4-5"}}); err != nil { + t.Fatal(err) + } + if err := provider.Save(provider.Provider{ID: "oa", Name: "OA", Chat: "https://oa.example/v1", Key: "k", + Models: []string{"k2.8-preview", "glm-6"}}); err != nil { + t.Fatal(err) + } + path := filepath.Join(home, ".claude", "settings.json") + bare := func(s string) string { return strings.TrimSuffix(s, "[1m]") } + envKey := "CLAUDE_CODE_SUBAGENT_MODEL" + if p.key != "subagent" { + envKey = tierEnv(p.key) + } + written := func() string { m, _ := edit.GetJSON(path, "env."+envKey); return bare(m) } + main := func(m string) { + t.Helper() + if via == "magpie" { + if err := a.Apply("model", m); err != nil { + t.Fatal(err) + } + } else { + if err := edit.SetJSON(path, edit.KV{Path: "model", Value: m}); err != nil { + t.Fatal(err) + } + if err := a.Follow(); err != nil { + t.Fatal(err) + } + } + } + if err := a.Apply("model", p.main); err != nil { + t.Fatal(err) + } + if err := a.Apply(p.key, p.model); err != nil { + t.Fatal(err) + } + if v := a.Values(); bare(v[p.key]) != p.model { + t.Fatalf("%s picked as %s reads %q, as if it followed the main model\n%s", p.key, p.model, v[p.key], readFile(path)) + } + if written() != p.model { + t.Fatalf("%s picked as %s is written %q\n%s", p.key, p.model, written(), readFile(path)) + } + main("oa/glm-6") + if written() != p.model { + t.Fatalf("main model moved: %s is %q, not its own %s\n%s", p.key, written(), p.model, readFile(path)) + } + if v := a.Values(); bare(v[p.key]) != p.model { + t.Fatalf("main model moved: %s reads %q, not its own %s", p.key, v[p.key], p.model) + } + + // "Same as main model": it follows again + if err := a.Apply(p.key, ""); err != nil { + t.Fatal(err) + } + main("oa/k2.8-preview") + if v := a.Values(); v[p.key] != "" { + t.Fatalf("same as main model: %s reads %q\n%s", p.key, v[p.key], readFile(path)) + } + if w := written(); w == p.model && p.model != "oa/k2.8-preview" || w == "oa/glm-6" { + t.Fatalf("same as main model: %s is still %q\n%s", p.key, w, readFile(path)) + } + }) + } + } +} diff --git a/internal/agent/claude_tiereffort_test.go b/internal/agent/claude_tiereffort_test.go index 1bcbf1d2d..c27010da3 100644 --- a/internal/agent/claude_tiereffort_test.go +++ b/internal/agent/claude_tiereffort_test.go @@ -82,9 +82,10 @@ func TestClaudeTierEffort(t *testing.T) { if env(claudeContextEnv) != "200000" { t.Fatalf("window: %s", env(claudeContextEnv)) } - // the effort stays with the tier through a new model, and back to the - // main model's - set("haiku", "v/glm") + // the effort stays with the tier through a new model, and back to + // following the main model ("Same as main model"; the main model + // picked by name is the tier's own, #2654) + set("haiku", "") if env("ANTHROPIC_DEFAULT_HAIKU_MODEL") != "v/glm:low" || a.Field("haiku").Get() != "" { t.Fatalf("haiku on the main model: %v", a.Values()) }