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()) }