diff --git a/internal/agent/claude.go b/internal/agent/claude.go index 2d74224eb..e8634ee3e 100644 --- a/internal/agent/claude.go +++ b/internal/agent/claude.go @@ -1098,10 +1098,12 @@ func claudeIn(at place) *Agent { return cmp.Or(s.Compact(), -1) } var models []claudeBudget - shown, _ := provider.CatalogFor("claude-desktop") - for _, e := range shown { - if id := gateway.DesktopID(e); claudeName(id) == "" { - models = append(models, claudeBudget{id: id, window: e.Context, long: gateway.DesktopListed1M(e), compact: work(e.ID)}) + if desktopOn() { + shown, _ := provider.CatalogFor("claude-desktop") + for _, e := range shown { + if id := gateway.DesktopID(e); claudeName(id) == "" { + models = append(models, claudeBudget{id: id, window: e.Context, long: gateway.DesktopListed1M(e), compact: work(e.ID)}) + } } } if main != "" { @@ -1115,13 +1117,13 @@ func claudeIn(at place) *Agent { for _, t := range claudeTiers { refs = append(refs, tiers[t]) } - for _, v := range refs { + for i, v := range refs { m, _ := tierAt(v) bare := strings.TrimSuffix(m, "[1m]") if v == "" || claudeModel(bare) || provider.ClaudeGroup(bare) { continue } - models = append(models, claudeBudget{id: spell(v), window: window[bare], long: strings.HasSuffix(v, "[1m]"), compact: work(bare)}) + models = append(models, claudeBudget{id: spell(v), window: window[bare], long: strings.HasSuffix(v, "[1m]"), main: i == 0, compact: work(bare)}) } // the one for every model else, as the variable gave it: none // on a Claude main model, which Anthropic runs to its own @@ -1152,16 +1154,22 @@ func claudeIn(at place) *Agent { // writeWindow tells Claude Code the window of the models it runs on // (claudeWindow). While Claude Desktop runs on magpie too, it is the // largest of theirs and each model below it gets its own - // (desktopPlan); with a CLAUDE_CODE_AUTO_COMPACT_WINDOW of the user's, - // which takes precedence over those, the least of them, as one value - // serves them all. One the user set is theirs. + // (desktopPlan), and so while the main model is a [1m] one beside a + // tier that isn't: the main's own window, with each tier below it held + // to the tier's (多喝热水 on Discord: the least tier's 246K beside a + // [1m] DeepSeek read as the main's window under Full window, and was + // the one it fell back to whenever Claude Code stopped taking it for + // 1M). With a + // CLAUDE_CODE_AUTO_COMPACT_WINDOW of the user's, which takes + // precedence over those, the least of them, as one value serves them + // all. One the user set is theirs. writeWindow := func(main string, tiers map[string]string) error { budgeted = false if env(claudeContextEnv) != "" && !windowOurs() { forget(windowKey) return dropBudgets() } - if desktopOn() && (env(claudeCompactEnv) == "" || compactOurs()) { + if (desktopOn() || claudeLongMain(main) && claudeWindow(main, tiers) > 0) && (env(claudeCompactEnv) == "" || compactOurs()) { top, def, entries := desktopPlan(main, tiers) if top <= 0 { return dropWindow() @@ -2329,6 +2337,14 @@ func claudeOwnCompact(path, model string) bool { return false } +// claudeLongMain says main is a model of magpie's Claude Code takes for 1M +// by its "[1m]", not one of Claude's own, which Anthropic runs to its own. +func claudeLongMain(main string) bool { + m, _ := tierAt(main) + bare := strings.TrimSuffix(m, "[1m]") + return bare != m && !claudeModel(bare) && !provider.ClaudeGroup(bare) +} + // 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 diff --git a/internal/agent/claude_budget.go b/internal/agent/claude_budget.go index d9bd14b77..07f78823e 100644 --- a/internal/agent/claude_budget.go +++ b/internal/agent/claude_budget.go @@ -1,6 +1,7 @@ package agent import ( + "cmp" "encoding/json" "strconv" "strings" @@ -25,6 +26,10 @@ type claudeBudget struct { id string // as Claude Code is handed it: its key under modelSettings window int // its window, 0 when unknown long bool // Claude Code takes it for 1M by its id ("[1m]") + // Claude Code's main model: a long one's window is the one told too, + // so the main runs to its own should Claude Code stop taking it for + // 1M, and nothing written reads as holding it to a smaller tier's + main bool // the auto-compact window magpie gives it: 0 for the top-level one // (base), below 0 for none, its whole window compact int @@ -58,8 +63,11 @@ func budgetPath(id string) string { // top to its own window, as magpie can't put theirs right. func claudePlan(models []claudeBudget, base int, theirs map[string]string) (top int, entries map[string]int) { for _, m := range models { - if !m.long && m.window > top { + switch { + case !m.long && m.window > top: top = m.window + case m.long && m.main: + top = max(top, cmp.Or(m.window, claudeLong)) } } for _, m := range models { diff --git a/internal/agent/claude_context_test.go b/internal/agent/claude_context_test.go index fede63d5f..4ecc985ac 100644 --- a/internal/agent/claude_context_test.go +++ b/internal/agent/claude_context_test.go @@ -7,6 +7,7 @@ import ( "github.com/yetone/magpie/internal/catalog" "github.com/yetone/magpie/internal/edit" "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/settings" ) // TestClaudeContextWindow: Claude Code takes a model it doesn't know for a @@ -53,14 +54,39 @@ func TestClaudeContextWindow(t *testing.T) { 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 + // the window says; beside a tier that isn't, the window told is the + // main's own, and the tier compacts by its own (多喝热水 on Discord: + // the tier's read as the main's), under Full window and without it set("model", "v/big[1m]") if window() != "" { t.Fatalf("[1m]: %q", window()) } + budget := func(id string) string { v, _ := edit.GetJSON(path, budgetPath(id)); return v } + compact := func() (env, top string) { + env, _ = edit.GetJSON(path, "env."+claudeCompactEnv) + top, _ = edit.GetJSON(path, "autoCompactWindow") + return env, top + } set("haiku", "v/small") - if window() != "128000" { - t.Fatalf("haiku on small beside a [1m] model: %q", window()) + if e, top := compact(); window() != "1000000" || budget("v/small") != "128000" || budget("v/big[1m]") != "" || e != "" || top != "272000" { + t.Fatalf("haiku on small beside a [1m] model: window %q, small %q, big %q, compact env %q, top-level %q", + window(), budget("v/small"), budget("v/big[1m]"), e, top) + } + if err := settings.Save(settings.Settings{FullContext: true}); err != nil { + t.Fatal(err) + } + if err := a.Sync(); err != nil { + t.Fatal(err) + } + if e, top := compact(); window() != "1000000" || budget("v/small") != "128000" || e != "" || top != "" { + t.Fatalf("full window: window %q, small %q, compact env %q, top-level %q", window(), budget("v/small"), e, top) + } + if err := settings.Save(settings.Settings{}); err != nil { + t.Fatal(err) + } + set("haiku", "") + if e, top := compact(); window() != "" || budget("v/small") != "" || e != "272000" || top != "" { + t.Fatalf("the tier gone: window %q, small %q, compact env %q, top-level %q", window(), budget("v/small"), e, top) } set("model", "v/mid") if window() != "400000" {