From 52b516da6d375da50fb155c8a253c05d96cd454d Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 12 Nov 2026 11:53:29 +0800 Subject: [PATCH] agents: a model of no known window is told the default under the longest context too (ul3140773077392 on X, #6259) settings.ContextCap held every window magpie knew, but fourteen agents write a default of their own (128000 or 200000) for a model whose window magpie doesn't know, and that default went out past the cap: Crush, ZCode, VS Code and its forks (Copilot, Zoo Code, Air), Copilot for JetBrains, Zed, jcode, Pencil, AtomCode, Kimi, OpenClaw, Snow, WorkBuddy and CodeBuddy. unknownWindow holds each default to the cap. AtomCode's output clamp used the constant and now uses the window it is told. BaoCode (a Claude Code front end) keeps its own context per model and reads no window from magpie; Claude Code gets the cap through CLAUDE_CODE_MAX_CONTEXT_TOKENS, which it does follow. Not changed. Guard: TestEveryAgentFollowsContextCap connects every agent that can be here to a provider with a 987654 model and a model of no window, under a cap of 99999, and fails on any agent file holding either window or a default. Red on the old code in 14 agents' files (checked on mote), green after. Run on mote: go test ./internal/agent ./internal/provider ok; go vet darwin, GOOS=windows, GOOS=linux -tags nogui ok; gofmt clean. Not tried: a live BaoCode, the full suite (started after the tag), GUI. --- internal/agent/agents.go | 2 +- internal/agent/atomcode.go | 6 +-- internal/agent/context_cap_agents_test.go | 64 +++++++++++++++++++++++ internal/agent/copilotjetbrains.go | 2 +- internal/agent/jcode.go | 2 +- internal/agent/kimi.go | 2 +- internal/agent/openclaw.go | 2 +- internal/agent/pencil.go | 2 +- internal/agent/route.go | 6 +++ internal/agent/snow.go | 2 +- internal/agent/vscode.go | 2 +- internal/agent/workbuddy.go | 2 +- internal/agent/zcode.go | 2 +- internal/agent/zed.go | 2 +- internal/agent/zoocode.go | 2 +- 15 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 internal/agent/context_cap_agents_test.go diff --git a/internal/agent/agents.go b/internal/agent/agents.go index 23ad223eb..379f3e6cb 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -648,7 +648,7 @@ func magpieProviderJSONAt(shape, catalog, gw string) any { for _, m := range models { window := m.Context if window == 0 { - window = 200000 + window = unknownWindow(200000) } // without it Crush caps every reply at 16384 tokens, a model // that can write more of them never asked for it; an output diff --git a/internal/agent/atomcode.go b/internal/agent/atomcode.go index 77c4f5ef4..3bc53d966 100644 --- a/internal/agent/atomcode.go +++ b/internal/agent/atomcode.go @@ -310,7 +310,7 @@ func atomcodeTablesAt(path, v1, key string) []edit.Table { for _, m := range sharedModels("atomcode") { ctx := m.Context if ctx <= 0 { - ctx = atomcodeContext + ctx = unknownWindow(atomcodeContext) } kvs := []edit.KV{ {Path: "account", Value: magpieID}, @@ -320,8 +320,8 @@ func atomcodeTablesAt(path, v1, key string) []edit.Table { // an output above the window is cut to it — the window AtomCode is // told when magpie doesn't know the model's is atomcodeContext if n := maxTokens(m); n > 0 { - if m.Context <= 0 && n > atomcodeContext { - n = atomcodeContext + if m.Context <= 0 && n > ctx { + n = ctx } kvs = append(kvs, edit.KV{Path: "max_tokens", Value: n}) } diff --git a/internal/agent/context_cap_agents_test.go b/internal/agent/context_cap_agents_test.go new file mode 100644 index 000000000..5d073ea60 --- /dev/null +++ b/internal/agent/context_cap_agents_test.go @@ -0,0 +1,64 @@ +package agent + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/provider" + "github.com/yetone/magpie/internal/settings" +) + +// The longest context (settings.ContextCap) is told to every agent that is +// written a context window (ul3140773077392 on X, #6259: dsh followed it, +// another harness kept 200K). Each agent is connected to a provider whose +// model has a 987654-token window under a cap of 99999; no file the agent +// reads may hold the longer one. +func TestEveryAgentFollowsContextCap(t *testing.T) { + const long, capAt = "987654", 99999 + home, _ := codexHome(t, "", "") + managed := claudeManaged + claudeManaged = func() string { return filepath.Join(home, "managed-settings.json") } + t.Cleanup(func() { claudeManaged = managed }) + startAlma(t) + startShelley(t) + os.MkdirAll(filepath.Join(home, ".hanako", "agents", "hana"), 0o755) + os.WriteFile(filepath.Join(home, ".hanako", "agents", "hana", "config.yaml"), []byte("agent:\n name: Hana\n"), 0o644) + if err := provider.Save(provider.Provider{ID: "v", Name: "V", Chat: "https://example.test/v1", Key: "k", + Models: []string{"big", "plain-model-of-no-window"}}); err != nil { + t.Fatal(err) + } + if err := catalog.SaveLive("v", "https://example.test/v1", []catalog.Model{{ID: "big", Context: 987654}, {ID: "plain-model-of-no-window"}}); err != nil { + t.Fatal(err) + } + if err := settings.Save(settings.Settings{ContextCap: 99999}); err != nil { + t.Fatal(err) + } + catalog.Touched() + for _, a := range All() { + if a.Launch != nil || a.Native != nil || a.WSL != "" || len(a.Fields) == 0 { + continue + } + t.Run(a.ID, func(t *testing.T) { + if _, err := a.ConnectHow(); err != nil { + t.Skip("can't be connected here:", err) + } + if !a.Wired() { + t.Skip("not wired by Connect here") + } + SyncCatalog() + for p, b := range agentFiles(t, home) { + for _, d := range []string{"128000", "128_000", "200000", "250000", "262144", "256000", "131072"} { + if strings.Contains(b, d) { + t.Errorf("%s holds the default window %s past the cap %d", p, d, capAt) + } + } + if strings.Contains(b, long) { + t.Errorf("%s holds the window %s past the cap %d", p, long, capAt) + } + } + }) + } +} diff --git a/internal/agent/copilotjetbrains.go b/internal/agent/copilotjetbrains.go index 67c64fe99..712597942 100644 --- a/internal/agent/copilotjetbrains.go +++ b/internal/agent/copilotjetbrains.go @@ -225,7 +225,7 @@ func copilotJBModelsJSON(hidden map[string]bool) map[string]any { for _, m := range sharedModels(copilotJBID) { context := m.Context if context == 0 { - context = 128000 // the server's own default for a custom endpoint + context = unknownWindow(128000) // the server's own default for a custom endpoint } // the output is reserved out of the window the prompt fills output := maxTokens(m) diff --git a/internal/agent/jcode.go b/internal/agent/jcode.go index 8fcc7efbd..9ed1f4b48 100644 --- a/internal/agent/jcode.go +++ b/internal/agent/jcode.go @@ -107,7 +107,7 @@ func jcodeWant(was []jcodeModel) []jcodeModel { for _, m := range sharedModels("jcode") { ctx := m.Context if ctx <= 0 { - ctx = jcodeContext + ctx = unknownWindow(jcodeContext) } e := jcodeModel{ID: m.ID, ContextWindow: ctx, Input: []string{"text"}} if m.Images { diff --git a/internal/agent/kimi.go b/internal/agent/kimi.go index 99a0781e4..55d35d838 100644 --- a/internal/agent/kimi.go +++ b/internal/agent/kimi.go @@ -70,7 +70,7 @@ func kimiModelTables(path string, legacy bool, wrote map[string]map[string]strin name := "models." + strconv.Quote(magpieID+"/"+m.ID) ctx := m.Context if ctx <= 0 { - ctx = kimiContext + ctx = unknownWindow(kimiContext) } var was map[string]string if slices.Contains(names, name) { diff --git a/internal/agent/openclaw.go b/internal/agent/openclaw.go index 8f2428c80..cb9910f09 100644 --- a/internal/agent/openclaw.go +++ b/internal/agent/openclaw.go @@ -186,7 +186,7 @@ func openClawWant(raw gjson.Result) []any { for _, m := range sharedModels("openclaw") { ctx := m.Context if ctx <= 0 { - ctx = openClawContext + ctx = unknownWindow(openClawContext) } e := openClawEntry{ID: m.ID, Name: m.ID, Input: []string{"text"}, ContextWindow: ctx} if m.Images { diff --git a/internal/agent/pencil.go b/internal/agent/pencil.go index fcb1183b4..a2c1e954f 100644 --- a/internal/agent/pencil.go +++ b/internal/agent/pencil.go @@ -105,7 +105,7 @@ func pencilProviderJSON(gw string) map[string]any { e["input"] = []string{"text"} } if _, ok := e["contextWindow"]; !ok { - e["contextWindow"] = 128000 + e["contextWindow"] = unknownWindow(128000) } if _, ok := e["maxTokens"]; !ok { e["maxTokens"] = min(16384, e["contextWindow"].(int)) diff --git a/internal/agent/route.go b/internal/agent/route.go index 4e5949153..3e420bd73 100644 --- a/internal/agent/route.go +++ b/internal/agent/route.go @@ -498,3 +498,9 @@ func group(name string, opts []Option) []Option { } func gatewayV1() string { return gateway.URL() + "/v1" } + +// unknownWindow is the window an agent is told of a model whose own magpie +// doesn't know: the agent's default n, held to the longest context the user +// set for every model (settings.ContextCap) like every window that is known +// (ul3140773077392 on X, #6259). +func unknownWindow(n int) int { return settings.Shared().Capped(n) } diff --git a/internal/agent/snow.go b/internal/agent/snow.go index 53bdfa917..624618f95 100644 --- a/internal/agent/snow.go +++ b/internal/agent/snow.go @@ -491,7 +491,7 @@ func snowProfileFor(from []byte, ours bool, v1, key, ref string, wrote map[strin if !ours { snowOwnVision(sc) } - ctx, out := snowContext, snowOutput + ctx, out := unknownWindow(snowContext), snowOutput sees := false for _, m := range sharedModels("snow") { if m.ID == ref { diff --git a/internal/agent/vscode.go b/internal/agent/vscode.go index 9b14542e6..07ae09780 100644 --- a/internal/agent/vscode.go +++ b/internal/agent/vscode.go @@ -624,7 +624,7 @@ func vscodeGroupJSON(k vscodeKind, vendor string) map[string]any { for _, m := range sharedModels(k.id) { context := m.Context if context == 0 { - context = 128000 // what VS Code takes a model of unknown window for + context = unknownWindow(128000) // what VS Code takes a model of unknown window for } // VS Code keeps the output budget out of the window it fills with // the prompt, so a model whose output limit is near its window diff --git a/internal/agent/workbuddy.go b/internal/agent/workbuddy.go index 54664013d..d127b8f8e 100644 --- a/internal/agent/workbuddy.go +++ b/internal/agent/workbuddy.go @@ -359,7 +359,7 @@ const buddySmallWindow = 200000 // agent's key. func buddyModel(agent, id, name string, context, output int, images bool, efforts []string) map[string]any { if context == 0 { - context = 200000 + context = unknownWindow(200000) } e := map[string]any{ "id": id, "name": name, "vendor": magpieID, diff --git a/internal/agent/zcode.go b/internal/agent/zcode.go index f5672491a..482d7fd95 100644 --- a/internal/agent/zcode.go +++ b/internal/agent/zcode.go @@ -135,7 +135,7 @@ func zcodeProviderJSON(path string, keep bool) any { for _, m := range sharedModels("zcode") { window := m.Context if window == 0 { - window = 200000 + window = unknownWindow(200000) } in := []string{"text"} if m.Images { diff --git a/internal/agent/zed.go b/internal/agent/zed.go index bd7baae23..57b859bf6 100644 --- a/internal/agent/zed.go +++ b/internal/agent/zed.go @@ -213,7 +213,7 @@ func zedProviderJSON(path, url string) map[string]any { for _, m := range sharedModels("zed") { context := m.Context if context == 0 { - context = 128000 // Zed requires a context window for every custom model. + context = unknownWindow(128000) // Zed requires a context window for every custom model. } // Zed's max_tokens is the window a prompt and its reply share: it // keeps max_output_tokens of it for the reply and lets the prompt diff --git a/internal/agent/zoocode.go b/internal/agent/zoocode.go index 71ae9c1db..efa675aff 100644 --- a/internal/agent/zoocode.go +++ b/internal/agent/zoocode.go @@ -254,7 +254,7 @@ func zooCodeModelInfo(m catalog.Model) map[string]any { info := map[string]any{"supportsPromptCache": false, "supportsImages": m.Images} // Zoo Code's own default for a model it isn't told about // (openAiModelInfoSaneDefaults) - info["contextWindow"] = 128_000 + info["contextWindow"] = unknownWindow(128_000) if m.Context > 0 { info["contextWindow"] = m.Context }