agents: Claude Code on a magpie model is told the model's context window (#311: its auto-compact settings stay as they were for another model) — Claude Code takes any model it doesn't know by name (all but Claude's own) for 200K, and CLAUDE_CODE_AUTO_COMPACT_WINDOW is only ever the smaller of its value and that, so a 128K model compacted too late and a 400K one far too early; CLAUDE_AUTOCOMPACT_PCT_OVERRIDE is a share of the window and holds for any model (read in Claude Code 2.1.285). magpie now writes CLAUDE_CODE_MAX_CONTEXT_TOKENS, the window Claude Code takes those names for, from the model's known context (a group's smallest), with the model: another model's replaces it, one magpie doesn't know the window of, a switch back to a Claude model or a reset removes it; with a [1m] main model, the smallest of the tiers not marked. A value the user set, or changed magpie's to, is left as it is (magpie keeps the one it wrote). TestClaudeContextWindow fails without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass.

This commit is contained in:
yetone
2026-09-30 17:44:44 +08:00
parent 91f10f929f
commit 7e454bba9b
2 changed files with 181 additions and 0 deletions
+65
View File
@@ -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 {
+116
View File
@@ -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())
}
}