From 7e454bba9be35f8d81c9bbd39bcdba6c00ea4a98 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 30 Sep 2026 17:44:41 +0800 Subject: [PATCH] =?UTF-8?q?agents:=20Claude=20Code=20on=20a=20magpie=20mod?= =?UTF-8?q?el=20is=20told=20the=20model's=20context=20window=20(#311:=20it?= =?UTF-8?q?s=20auto-compact=20settings=20stay=20as=20they=20were=20for=20a?= =?UTF-8?q?nother=20model)=20=E2=80=94=20Claude=20Code=20takes=20any=20mod?= =?UTF-8?q?el=20it=20doesn't=20know=20by=20name=20(all=20but=20Claude's=20?= =?UTF-8?q?own)=20for=20200K,=20and=20CLAUDE=5FCODE=5FAUTO=5FCOMPACT=5FWIN?= =?UTF-8?q?DOW=20is=20only=20ever=20the=20smaller=20of=20its=20value=20and?= =?UTF-8?q?=20that,=20so=20a=20128K=20model=20compacted=20too=20late=20and?= =?UTF-8?q?=20a=20400K=20one=20far=20too=20early;=20CLAUDE=5FAUTOCOMPACT?= =?UTF-8?q?=5FPCT=5FOVERRIDE=20is=20a=20share=20of=20the=20window=20and=20?= =?UTF-8?q?holds=20for=20any=20model=20(read=20in=20Claude=20Code=202.1.28?= =?UTF-8?q?5).=20magpie=20now=20writes=20CLAUDE=5FCODE=5FMAX=5FCONTEXT=5FT?= =?UTF-8?q?OKENS,=20the=20window=20Claude=20Code=20takes=20those=20names?= =?UTF-8?q?=20for,=20from=20the=20model's=20known=20context=20(a=20group's?= =?UTF-8?q?=20smallest),=20with=20the=20model:=20another=20model's=20repla?= =?UTF-8?q?ces=20it,=20one=20magpie=20doesn't=20know=20the=20window=20of,?= =?UTF-8?q?=20a=20switch=20back=20to=20a=20Claude=20model=20or=20a=20reset?= =?UTF-8?q?=20removes=20it;=20with=20a=20[1m]=20main=20model,=20the=20smal?= =?UTF-8?q?lest=20of=20the=20tiers=20not=20marked.=20A=20value=20the=20use?= =?UTF-8?q?r=20set,=20or=20changed=20magpie's=20to,=20is=20left=20as=20it?= =?UTF-8?q?=20is=20(magpie=20keeps=20the=20one=20it=20wrote).=20TestClaude?= =?UTF-8?q?ContextWindow=20fails=20without=20the=20change.=20go=20vet,=20g?= =?UTF-8?q?o=20build=20-tags=20nogui=20(darwin,=20GOOS=3Dlinux),=20GOOS=3D?= =?UTF-8?q?windows=20go=20build,=20go=20test=20-tags=20nogui=20./...=20pas?= =?UTF-8?q?s.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/agent/claude.go | 65 +++++++++++++++ internal/agent/claude_context_test.go | 116 ++++++++++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 internal/agent/claude_context_test.go diff --git a/internal/agent/claude.go b/internal/agent/claude.go index c1d322d81..bc2608ac1 100644 --- a/internal/agent/claude.go +++ b/internal/agent/claude.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "runtime" + "strconv" "strings" "time" @@ -40,6 +41,14 @@ var claudeEfforts = []string{"low", "medium", "high", "xhigh", "max"} // among them. const claudeEffortEnv = "CLAUDE_CODE_EFFORT_LEVEL" +// claudeContextEnv is the context window Claude Code takes a model it +// doesn't know for (any but Claude's own names); without it, 200K. Its +// auto-compact window (CLAUDE_CODE_AUTO_COMPACT_WINDOW, the autoCompactWindow +// setting) is only ever the smaller of its own value and this one, so this +// is what tells it where a 128K or a 400K model runs out. A name marked [1m] +// is 1M whatever it says. +const claudeContextEnv = "CLAUDE_CODE_MAX_CONTEXT_TOKENS" + func tierEnv(tier string) string { return "ANTHROPIC_DEFAULT_" + strings.ToUpper(tier) + "_MODEL" } func claude(home string) *Agent { return claudeIn(here(home)) } @@ -62,6 +71,20 @@ func claudeIn(at place) *Agent { } return model() } + // the context window magpie last wrote, so a value the user wrote is + // never taken for magpie's: that one is left as it is + windowKey := at.key("claude.context_tokens") + windowOurs := func() bool { + cur := env(claudeContextEnv) + return cur != "" && cur == stashLoad()[windowKey] + } + dropWindow := func() error { + defer forget(windowKey) + if windowOurs() { + return edit.DelJSON(path, "env."+claudeContextEnv) + } + return nil + } var writeTiers func(main string, tiers map[string]string) error set := func(v string) error { if v == "" { @@ -71,6 +94,9 @@ func claudeIn(at place) *Agent { keys = append(keys, "env."+k) } forget(at.key("claude.model"), at.key("claude.base_url"), at.key("claude.auth_token")) + if err := dropWindow(); err != nil { + return err + } return edit.DelJSON(path, keys...) } if isMagpie(v) { @@ -92,6 +118,9 @@ func claudeIn(at place) *Agent { } return writeTiers(v, tiers) } + if err := dropWindow(); err != nil { + return err + } if routed() { keys := make([]string, len(claudeEnv)) for i, k := range claudeEnv { @@ -140,6 +169,18 @@ func claudeIn(at place) *Agent { } else { kvs = append(kvs, edit.KV{Path: "env.CLAUDE_CODE_SUBAGENT_MODEL", Value: main}) } + // the new model's window replaces the old one's, when magpie knows + // it; one the user set is theirs + if env(claudeContextEnv) == "" || windowOurs() { + if w := claudeWindow(main, tiers); w > 0 { + kvs = append(kvs, edit.KV{Path: "env." + claudeContextEnv, Value: strconv.Itoa(w)}) + stash(map[string]string{windowKey: strconv.Itoa(w)}) + } else if err := dropWindow(); err != nil { + return err + } + } else { + forget(windowKey) + } return edit.SetJSON(path, kvs...) } @@ -279,6 +320,30 @@ func claudeViaMagpie() []Option { return opts } +// claudeWindow is the context window to tell Claude Code for the models it +// runs on, 0 when magpie doesn't know it. One value serves every model not +// marked [1m]: the main model's, or when that one is marked, the smallest of +// the tiers' that aren't, so none of them outgrows its own. +func claudeWindow(main string, tiers map[string]string) int { + const mark = "[1m]" + window := map[string]int{} + for _, m := range magpieModels("claude") { + window[m.ID] = m.Context + } + if !strings.HasSuffix(main, mark) { + return window[main] + } + w := 0 + for _, t := range claudeTiers { + if v := tiers[t]; !strings.HasSuffix(v, mark) { + if c := window[v]; c > 0 && (w == 0 || c < w) { + w = c + } + } + } + return w +} + // claudeManaged is where an administrator's Claude Code settings live; a var // so tests can point it elsewhere. var claudeManaged = func() string { diff --git a/internal/agent/claude_context_test.go b/internal/agent/claude_context_test.go new file mode 100644 index 000000000..ed90ec19e --- /dev/null +++ b/internal/agent/claude_context_test.go @@ -0,0 +1,116 @@ +package agent + +import ( + "path/filepath" + "testing" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/edit" + "github.com/yetone/magpie/internal/provider" +) + +// TestClaudeContextWindow: Claude Code takes a model it doesn't know for a +// 200K one, so magpie tells it the window of the model it switches it to +// (CLAUDE_CODE_MAX_CONTEXT_TOKENS), changes it with the model, takes it away +// for a model it doesn't know the window of and for Claude's own, and never +// touches a value the user set (#311). +func TestClaudeContextWindow(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + if err := provider.Save(provider.Provider{ID: "v", Name: "V", Chat: "https://example.test/v1", Key: "k", + Models: []string{"small", "mid", "big", "plain"}}); err != nil { + t.Fatal(err) + } + if err := catalog.SaveLive("v", "https://example.test/v1", []catalog.Model{ + {ID: "small", Context: 128000}, {ID: "mid", Context: 400000}, {ID: "big", Context: 1000000}, {ID: "plain"}, + }); err != nil { + t.Fatal(err) + } + path := filepath.Join(home, ".claude", "settings.json") + writeFile(t, path, `{"theme":"dark"}`) + a := claude(home) + window := func() string { v, _ := edit.GetJSON(path, "env."+claudeContextEnv); return v } + set := func(key, v string) { + t.Helper() + if err := a.Field(key).Set(v); err != nil { + t.Fatal(err) + } + } + + set("model", "v/small") + if window() != "128000" { + t.Fatalf("small: %q", window()) + } + set("model", "v/mid") + if window() != "400000" { + t.Fatalf("switched to mid: %q", window()) + } + set("model", "v/plain") + if window() != "" { + t.Fatalf("a model of no known window keeps the last one's: %q", window()) + } + // a 1M model is marked [1m], which Claude Code takes for 1M whatever + // the window says; a tier that isn't is told its own + set("model", "v/big[1m]") + if window() != "" { + t.Fatalf("[1m]: %q", window()) + } + set("haiku", "v/small") + if window() != "128000" { + t.Fatalf("haiku on small beside a [1m] model: %q", window()) + } + set("model", "v/mid") + if window() != "400000" { + t.Fatalf("the main model's window wins: %q", window()) + } + // back to Claude's own model, and to Claude Code as installed + set("model", "claude-opus-4-8") + if window() != "" { + t.Fatalf("Claude's own model kept magpie's window: %q", window()) + } + set("model", "v/mid") + set("model", "") + if window() != "" { + t.Fatalf("reset kept magpie's window: %q", window()) + } + if v, _ := edit.GetJSON(path, "theme"); v != "dark" { + t.Fatal("theme lost") + } + + // the user's own value is theirs, set before magpie or over magpie's + if err := edit.SetJSON(path, edit.KV{Path: "env." + claudeContextEnv, Value: "64000"}); err != nil { + t.Fatal(err) + } + for _, v := range []string{"v/small", "v/plain", "v/mid", "claude-opus-4-8", ""} { + set("model", v) + if window() != "64000" { + t.Fatalf("the user's value after %q: %q", v, window()) + } + } + if err := edit.DelJSON(path, "env."+claudeContextEnv); err != nil { + t.Fatal(err) + } + set("model", "v/small") + if err := edit.SetJSON(path, edit.KV{Path: "env." + claudeContextEnv, Value: "100000"}); err != nil { + t.Fatal(err) + } + set("model", "v/mid") + set("model", "") + if window() != "100000" { + t.Fatalf("a value the user changed magpie's to: %q", window()) + } + // and one the same as magpie would write, but not written by it + set("model", "v/small") + if window() != "100000" { + t.Fatalf("user value replaced: %q", window()) + } + if err := edit.SetJSON(path, edit.KV{Path: "env." + claudeContextEnv, Value: "400000"}); err != nil { + t.Fatal(err) + } + set("model", "") + if window() != "400000" { + t.Fatalf("the user's 400000 was taken for magpie's: %q", window()) + } +}