agent: a [1m] main beside a smaller tier is told its own 1M, the tier held to its own (多喝热水 on Discord)
多喝热水 had Full window on, Claude Code's main on a [1m] DeepSeek 4.1 and a smaller tier beside it. magpie told Claude Code the least tier's window (CLAUDE_CODE_MAX_CONTEXT_TOKENS=246000), so it read 246K as the main model's window and asked why 1M never showed. It is also the window Claude Code falls back to when it stops taking the main for 1M. Now a [1m] main of magpie's beside a tier that isn't takes the layout Desktop already has (desktopPlan): the window told is the main's own (1M), each smaller tier gets modelSettings.<id>.autoCompactWindow at its own window, and the working window goes in the top-level autoCompactWindow in place of the env (none under Full window). A [1m] main alone, a Claude main and the user's own env and settings are as before. Run: - TestClaudeContextWindow's new case (tier beside a [1m] main, with and without Full window, then the tier gone) fails on the old code (window "128000", no tier entry, env compact 272000) and passes now. - Real Claude Code 2.1.302 in a sandbox CLAUDE_CONFIG_DIR on the live gateway, main workbuddy/deepseek-v4.1-flash[1m], haiku workbuddy/hy3: /context shows the main at 272k and hy3 at 192k without Full window, and the main at 1m and hy3 at 192k with it. With the old layout's env of 246000, hy3 showed 246k, past its own 192k. A real request on the main replied "ok". - gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./... clean; go build ./... ok. - go test -timeout 45m -count=1 ./... at95ef45fda: every package ok. After rebasing ontoa595f1559(no internal/agent changes there), go test ./internal/agent/ again: ok.
This commit is contained in:
+26
-10
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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" {
|
||||
|
||||
Reference in New Issue
Block a user