agent: with Claude Desktop on magpie, Claude Code runs each model at its own window, not the least of them all (#2025, 6.1sol)

Claude Desktop's Code tab runs Claude Code on ~/.claude/settings.json
with every model Desktop lists. Claude Code takes one window
(CLAUDE_CODE_MAX_CONTEXT_TOKENS) for every model it doesn't know, and
magpie told it the least of Desktop's (#1458), so one small model held
a 500K or 1M one to its 131K.

Now, while Desktop is on magpie, magpie tells Claude Code the largest
window of Desktop's models and of Claude Code's own main and tier
models, and holds each model below it to its own with
modelSettings.<id>.autoCompactWindow (claudePlan, claude_budget.go).
CLAUDE_CODE_AUTO_COMPACT_WINDOW would take precedence over those, so in
this mode magpie's working window goes in the top-level
autoCompactWindow instead, and an entry is written only where a
model's window or threshold differs from what it would run at
without one. Claude models (and groups of them) are left out as
before. magpie's entries and top-level value are stashed and go when
Desktop leaves magpie. The user's own entries, top-level value and
variables are never written over: an entry of theirs past its model's
window (or "auto") holds the largest to that window, and with a
CLAUDE_CODE_AUTO_COMPACT_WINDOW of theirs the least window is told as
before. Desktop off is unchanged.

Verified with real Claude Code 2.1.296 in a sandbox
CLAUDE_CONFIG_DIR, on a settings.json written through magpie's editor:
/context shows 256k for mythos-magpie-2's entry, 131k for
magpie/v/gpt-6.1-sol (dotted key, with and without [1m]), and the
top-level 272k for a model without an entry, under a 1M
MAX_CONTEXT_TOKENS.

Tests: TestClaudePlan and TestBudgetPath (new), and
TestClaudeDesktopContextWindow / TestClaudeDesktopAloneContextWindow
rewritten (largest window, per-model entries, Full window, the user's
entry and top-level kept, Desktop off removing only magpie's). They fail
on the old claude.go (256000 told), and with the skip of the user's
entries removed from claudePlan. Run: go test ./internal/agent (full),
gofmt -l, go vet for darwin, GOOS=linux -tags nogui and GOOS=windows.
This commit is contained in:
yetone
2026-10-14 08:17:33 +08:00
parent 8a4cfe0e4e
commit 1555cddfa0
5 changed files with 534 additions and 46 deletions
+1
View File
@@ -55,6 +55,7 @@ something else shows up as drift.
- Claude Code's tier and subagent models the user had set for their own endpoint (`claudeOwnEnv`) are stashed with that endpoint and token when magpie is wired in, and come back with them when magpie steps out, by Disconnect or by Claude Code's own model (#1050).
- Claude Code's sign-in (`login`) is magpie's key (`""`, the default) or claude.ai (`claudeai`). claude.ai writes `ANTHROPIC_AUTH_TOKEN` empty, which Claude Code reads as no key: it keeps its claude.ai login and sends that to magpie, which never passes it on (`claudeSignIn` in the gateway). Every later write keeps the token empty (`wiredKey`), and a key put there by hand is reported by Check, not taken for magpie's. It's offered only where the gateway takes any key (`gwKey() == gateway.Token`: loopback, a WSL distro mirrored), not from a distro under NAT while magpie is shared on the network, where only the sharing key gets in. The choice is stashed, so it survives Disconnect and a port move. An empty token counts as magpie's only beside the address magpie last wrote (`claude.wired_at` in the stash, which Rewire carries to the new port, `keyOurs`): an endpoint of the user's own with an empty token stays theirs when the model goes back to Claude Code's own.
- Claude Code's auto mode review (`CLAUDE_CODE_AUTO_MODE_SERVER`, `claudeReviewEnv` in [`claude.go`](../../internal/agent/claude.go)) is written `"0"` while Claude Code is routed through magpie and none of the models it can reach there is one Anthropic reviews (`claudeReviewed`, by `gateway.AutoModeReviewed`): those it is shown (and Claude Desktop's while Desktop is on magpie), a shown group's members, those providers' fallbacks, and the models it is set to. Off, it classifies locally from the start, as it does anyway behind a gateway that doesn't answer, without the "isn't eligible because your requests go through 127.0.0.1:3425" notice (1EFLOPS on X). Any reviewed model leaves it unset, since `/model` can take the session there; a model hidden from Claude Code doesn't count. It is rewritten on every model set and `Sync`, so adding a Claude provider turns the review back on. magpie's value is kept apart in the stash (`claude.auto_mode_server`), a user's own is left alone, and it goes when Claude Code is taken off magpie (`desktopOnly`, `unroute`). T3 Code's magpie instance, which runs Claude Code on the gateway through its own environment, carries the same `"0"` there by the models it lists (`t3InstanceJSON` in [`t3code.go`](../../internal/agent/t3code.go)); a value in the user's `~/.claude` settings comes after it.
- Claude Code takes one window (`CLAUDE_CODE_MAX_CONTEXT_TOKENS`, `claudeContextEnv` in [`claude.go`](../../internal/agent/claude.go)) for every model it doesn't know. Claude Code alone on magpie is told its main model's (`claudeWindow`). While Claude Desktop is on magpie too, its Code tab runs Claude Code on the same `~/.claude/settings.json` with every model Desktop lists, so magpie tells it the largest window of those and of Claude Code's own models, and holds each model below it to its own with `modelSettings.<id>.autoCompactWindow` (`claudePlan` in [`claude_budget.go`](../../internal/agent/claude_budget.go), #2025: the least held a 1M model to 131K). Claude Code 2.1.296 matches that key by the id it was handed, lowercased and without `[1m]`; it comes before the top-level `autoCompactWindow`, which serves a model without one. `CLAUDE_CODE_AUTO_COMPACT_WINDOW` would take precedence over both, so in this mode magpie's working window goes in the top-level `autoCompactWindow` instead, and an entry is written only where a model's window or its own threshold differs from what it would run at without one. magpie's entries and top-level value are stashed (`claude.model_budgets`, `claude.compact_default`) and go when Desktop leaves magpie; the user's own entries, top-level value and variables stay, a user's entry past its model's window (or `auto`) holds the largest to that window, and with a `CLAUDE_CODE_AUTO_COMPACT_WINDOW` of the user's the least window is told as before.
- MiniMax Code takes a model ref with one slash in the whole (a SubAgent's `model` must match `^[^/\s]+\/[^/\s]+$`, #1387), and magpie's refs there are `custom_provider:magpie/<model>`. So a model under `custom_provider.magpie` is written with `~` for each slash of its catalog id (`bb-codex~gpt-6.1-sol`, `provider.FlatID`), only when that spelling stands for one served id (`provider.Flattened`); the gateway takes it back. A config an older magpie wrote moves over on the next sync: the user's keys on a model go to its new key, `defaultModel`, `defaultLightModel` and `memory.captureModel`/`consolidationModel` follow (`mcodeRefs`), and the slashed key stays, switched off (`enabled: false`), so a session begun on it still resumes. magpie never edits the user's own `agents/<name>/agent.md`. No other agent was found with this limit ([`minimax.go`](../../internal/agent/minimax.go)).
- Claude Code's `/model` list (`modelPicker` in its settings.json) is magpie's while it is marked in the stash (`claude.model_picker`, `claude@wsl:<distro>.model_picker` for a WSL distro) or while every row's description ends "via magpie", as every row magpie writes does (`magpiesPicker` in [`claude.go`](../../internal/agent/claude.go)). magpie keeps its own list up to date on every model set and `Sync`, and removes it when Claude Code is taken off magpie; a list the user wrote is left alone. The fingerprint covers a list whose mark an older magpie's stash lost, which otherwise stayed without every model added since (#2046).
- Claude Code 2.1.296 reads a Claude model whose version has a dot (`relay/claude-opus-4.6-us`) as Claude Opus 4: a retired warning, budget thinking, 32000 output tokens, no effort. With dashes (`relay/claude-opus-4-6-us`) it knows the model (misteer_ti. on Discord). So every model magpie writes into its settings (`model`, the tier envs, `ANTHROPIC_SMALL_FAST_MODEL`, `CLAUDE_CODE_SUBAGENT_MODEL`, `modelPicker`), T3 Code's Claude slugs and Claude Desktop's Code-tab ids (`claudeLooking`) spell a dotted Claude version with dashes (`provider.ClaudeSpelled`), only when that spelling stands for one served id. A Claude model listed to Desktop with reasoning levels is given as `magpie-<n>.anthropic.<model>`, its dotted version dashed as well (`claudeModel`: Copilot's `claude-opus-5.5` is `…anthropic.claude-opus-5-5`, which Claude Code knows; it was `mythos-magpie-<n>`, an unknown model, and Haiku 4.5 got an effort Copilot refuses, #1819); a request for it is served by `<n>` alone (`aliased`), so that spelling needn't be unique. Mythos counts as Claude here too: Claude Code matches `claude-mythos-5-1` by substring, so `claude-mythos-5.1` would read as Mythos 5. Every read of those keys takes it back (`provider.ClaudeRead`: `claudeIn`'s `env`, `claudeMain`, `claudeStandInAt`), so magpie's fields, applied state and Drift keep the dotted id. The user's own values are stashed and given back as written. Values an older magpie wrote dotted are rewritten on the next Sync. Other vendors' ids are never respelled ([`claudedash.go`](../../internal/provider/claudedash.go)).
+210 -4
View File
@@ -619,8 +619,99 @@ func claudeIn(at place) *Agent {
cur := env(claudeContextEnv)
return cur != "" && cur == stashLoad()[windowKey]
}
// the modelSettings windows magpie last wrote while Claude Desktop is
// on magpie (claudePlan, #2025), as JSON {id: window}, and the
// top-level autoCompactWindow it wrote beside them: each is magpie's
// only while it still says what magpie wrote, so the user's own, an
// /autocompact among them, is never taken for magpie's
budgetsKey := at.key("claude.model_budgets")
defaultKey := at.key("claude.compact_default")
budgetsOurs := func() map[string]int {
var m map[string]int
_ = json.Unmarshal([]byte(stashLoad()[budgetsKey]), &m)
return m
}
defaultOurs := func() bool {
cur, ok := edit.GetJSON(path, "autoCompactWindow")
return ok && cur != "" && cur == stashLoad()[defaultKey]
}
// writeBudgets puts in entries and the default (0 for none), and takes
// out what magpie wrote before that they no longer have
writeBudgets := func(def int, entries map[string]int) error {
raw, _ := edit.GetJSON(path, "modelSettings")
cur := budgetsRead(raw)
var del []string
for id, v := range budgetsOurs() {
if _, keep := entries[id]; keep || cur[id] != strconv.Itoa(v) {
continue
}
del = append(del, budgetPath(id))
}
if len(del) > 0 {
if err := edit.DelJSON(path, del...); err != nil {
return err
}
// an id's object magpie emptied goes too
raw, _ = edit.GetJSON(path, "modelSettings")
var per map[string]map[string]json.RawMessage
if json.Unmarshal([]byte(raw), &per) == nil {
var empty []string
for _, p := range del {
id := strings.TrimSuffix(strings.TrimPrefix(p, "modelSettings."), ".autoCompactWindow")
if o, ok := per[strings.ReplaceAll(id, `\.`, ".")]; ok && len(o) == 0 {
empty = append(empty, "modelSettings."+id)
}
}
if len(empty) > 0 && len(empty) == len(per) {
empty = []string{"modelSettings"}
}
if len(empty) > 0 {
if err := edit.DelJSON(path, empty...); err != nil {
return err
}
}
}
}
var kvs []edit.KV
for id, v := range entries {
if cur[id] != strconv.Itoa(v) {
kvs = append(kvs, edit.KV{Path: budgetPath(id), Value: v})
}
}
if len(entries) > 0 {
b, _ := json.Marshal(entries)
stash(map[string]string{budgetsKey: string(b)})
} else {
forget(budgetsKey)
}
top, has := edit.GetJSON(path, "autoCompactWindow")
switch {
case def > 0 && (!has || defaultOurs()):
stash(map[string]string{defaultKey: strconv.Itoa(def)})
if top != strconv.Itoa(def) {
kvs = append(kvs, edit.KV{Path: "autoCompactWindow", Value: def})
}
case def > 0:
forget(defaultKey)
default:
if defaultOurs() {
if err := edit.DelJSON(path, "autoCompactWindow"); err != nil {
return err
}
}
forget(defaultKey)
}
if len(kvs) == 0 {
return nil
}
return edit.SetJSON(path, kvs...)
}
dropBudgets := func() error { return writeBudgets(0, nil) }
dropWindow := func() error {
defer forget(windowKey)
if err := dropBudgets(); err != nil {
return err
}
if windowOurs() {
return edit.DelJSON(path, "env."+claudeContextEnv)
}
@@ -668,6 +759,10 @@ func claudeIn(at place) *Agent {
}
return nil
}
// budgeted is set by writeWindow, which runs before writeCompact,
// while each model has its own window under modelSettings (claudePlan):
// the variable would take precedence over them, so it goes
budgeted := false
// writeCompact has Claude Code compact at the working window
// (settings.WorkingWindow) on magpie, unless settings.FullContext or the
// main model is one of Anthropic's Claude models: Anthropic runs a [1m]
@@ -679,6 +774,9 @@ func claudeIn(at place) *Agent {
forget(compactKey)
return nil
}
if budgeted {
return dropCompact()
}
// a threshold the user set on the main model or its provider comes
// first (#876), a Claude model's included; then the one for every
// model (settings.Compact), none under Full window
@@ -834,14 +932,122 @@ func claudeIn(at place) *Agent {
}
return edit.SetJSON(path, edit.KV{Path: "env." + claudeReviewEnv, Value: "0"})
}
// desktopPlan is what writeWindow writes while Claude Desktop is on
// magpie (claudePlan, #2025): the models its Code tab runs Claude Code
// on, and while Claude Code itself is on magpie (main set) its main
// model and tiers', each with its own window under the
// largest. A model Claude Code knows as Claude's has its own window.
desktopPlan := func(main string, tiers map[string]string) (top, def int, entries map[string]int) {
s := settings.Load()
// the user's own top-level autoCompactWindow
cur, userTop := edit.GetJSON(path, "autoCompactWindow")
userTop = userTop && !defaultOurs()
// the window each model of magpie's compacts at (claudeBudget's
// compact), as writeCompact gives the main model: the user's
// threshold for it first (#876); a Claude model, or a group of them,
// runs to its own (#1741), as does one whose window the user typed
// (#1809); the user's autoCompactWindow, unless they gave magpie a
// threshold for every model; then that one, the working window
// unless Full window. It is the model's own entry, so Desktop's
// models have it with Claude Code itself off magpie too, where
// Claude's own models compact as they did
work := func(ref string) int {
if n := provider.CompactSet(ref); n > 0 {
return n
}
if claudeModel(ref) || provider.ClaudeGroup(ref) {
return -1
}
if s.CompactAt == 0 && userTop {
return 0
}
if s.CompactAt == 0 && provider.WindowTyped(ref) {
return -1
}
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 main != "" {
window := map[string]int{}
for _, m := range magpieModels("claude") {
window[m.ID] = m.Context
}
// a tier fixed at an effort is handed Claude Code with it, and
// matched by it
refs := []string{main}
for _, t := range claudeTiers {
refs = append(refs, tiers[t])
}
for _, 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)})
}
// the one for every model else, as the variable gave it: none
// on a Claude main model, which Anthropic runs to its own
if m, _ := tierAt(main); !claudeModel(strings.TrimSuffix(m, "[1m]")) && !provider.ClaudeGroup(strings.TrimSuffix(m, "[1m]")) {
def = s.Compact()
}
}
// the user's own top-level window is the one a model without an
// entry runs at ("auto": the one Claude Code is told), in place of
// magpie's
base := def
if userTop {
def = 0
base, _ = strconv.Atoi(cur)
}
raw, _ := edit.GetJSON(path, "modelSettings")
ours := budgetsOurs()
theirs := map[string]string{}
for k, v := range budgetsRead(raw) {
if n, ok := ours[k]; ok && v == strconv.Itoa(n) {
continue
}
theirs[budgetKey(k)] = v
}
top, entries = claudePlan(models, base, theirs)
return top, def, entries
}
// writeWindow tells Claude Code the window of the models it runs on
// (claudeWindow), and while Claude Desktop runs on magpie, of those its
// Code tab runs it on too (claudeDesktopWindow): the least of them, as
// one value serves them all. One the user set is theirs.
// (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.
writeWindow := func(main string, tiers map[string]string) error {
budgeted = false
if env(claudeContextEnv) != "" && !windowOurs() {
forget(windowKey)
return nil
return dropBudgets()
}
if desktopOn() && (env(claudeCompactEnv) == "" || compactOurs()) {
top, def, entries := desktopPlan(main, tiers)
if top <= 0 {
return dropWindow()
}
if err := writeBudgets(def, entries); err != nil {
return err
}
budgeted = true
v := strconv.Itoa(top)
stash(map[string]string{windowKey: v})
if v == env(claudeContextEnv) {
return nil
}
return edit.SetJSON(path, edit.KV{Path: "env." + claudeContextEnv, Value: v})
}
if err := dropBudgets(); err != nil {
return err
}
w := claudeWindow(main, tiers)
if desktopOn() {
+139
View File
@@ -0,0 +1,139 @@
package agent
import (
"encoding/json"
"strconv"
"strings"
)
// While Claude Desktop is on magpie, its Code tab runs Claude Code on
// ~/.claude/settings.json with every model Desktop lists, and Claude Code
// takes one window (claudeContextEnv) for all of those it doesn't know.
// The least of them held every one to it: one 131K image model beside a
// 500K GPT ran the GPT at 131K too (#2025). So magpie tells Claude Code
// the largest window instead, and holds each model below it to its own
// with modelSettings.<id>.autoCompactWindow, which Claude Code (2.1.296)
// looks up by the id it was handed (lowercased, "[1m]" dropped) and takes
// before the top-level autoCompactWindow. CLAUDE_CODE_AUTO_COMPACT_WINDOW
// would take precedence over both, so magpie's working window
// (settings.WorkingWindow) goes in the top-level autoCompactWindow
// instead while it does this.
// claudeBudget is a model Claude Code may run on through magpie while
// Claude Desktop is on it.
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]")
// the auto-compact window magpie gives it: 0 for the top-level one
// (base), below 0 for none, its whole window
compact int
}
// claudeLong is the window Claude Code takes a "[1m]" model for.
const claudeLong = 1000000
// budgetKey is a modelSettings key as Claude Code matches it.
func budgetKey(id string) string { return strings.ToLower(strings.TrimSuffix(id, "[1m]")) }
// budgetKeyOK says id can go under modelSettings as a key path magpie's
// JSON editor writes: a dot is escaped, gjson's other path characters
// aren't, so a model whose id has one gets no entry.
func budgetKeyOK(id string) bool {
return id != "" && !strings.ContainsAny(id, `*?|#@\"`)
}
// budgetPath is the key path of id's auto-compact window.
func budgetPath(id string) string {
return "modelSettings." + strings.ReplaceAll(strings.TrimSuffix(id, "[1m]"), ".", `\.`) + ".autoCompactWindow"
}
// claudePlan is the window to tell Claude Code (top) and the
// auto-compact window to give each model whose own differs from what it
// would run at without one (entries, by id without "[1m]"). base is the
// top-level autoCompactWindow in the file, magpie's or the user's, 0 for
// none; theirs are the user's own modelSettings windows by budgetKey,
// which magpie never writes over. A model the user gave a window past its
// own ("auto" too, which runs it to the one Claude Code is told) holds
// 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 {
top = m.window
}
}
for _, m := range models {
v, ok := theirs[budgetKey(m.id)]
if !ok || m.long || m.window <= 0 {
continue
}
if n, err := strconv.Atoi(v); err != nil || n > m.window {
top = min(top, m.window)
}
}
if top <= 0 {
return 0, nil
}
entries = map[string]int{}
seen := map[string]string{}
for _, m := range models {
k := budgetKey(m.id)
if _, own := theirs[k]; own || !budgetKeyOK(m.id) {
continue
}
assumed := top
if m.long {
assumed = claudeLong
}
want := assumed
if m.window > 0 && !m.long {
want = min(m.window, top)
}
switch {
case m.compact > 0:
want = min(want, m.compact)
case m.compact == 0 && base > 0:
want = min(want, base)
}
had := assumed
if base > 0 {
had = min(base, assumed)
}
if want == had {
continue
}
id := strings.TrimSuffix(m.id, "[1m]")
if was, dup := seen[k]; dup {
// two ids Claude Code matches as one: the smaller window
if want < entries[was] {
entries[was] = want
}
continue
}
seen[k] = id
entries[id] = want
}
return top, entries
}
// budgetsRead is the modelSettings entries' autoCompactWindow in raw, by
// key as written: a number's digits, a string's text.
func budgetsRead(raw string) map[string]string {
var per map[string]map[string]json.RawMessage
if json.Unmarshal([]byte(raw), &per) != nil {
return nil
}
out := map[string]string{}
for k, v := range per {
w, ok := v["autoCompactWindow"]
if !ok {
continue
}
var s string
if json.Unmarshal(w, &s) != nil {
s = string(w)
}
out[k] = s
}
return out
}
+72
View File
@@ -0,0 +1,72 @@
package agent
import (
"reflect"
"testing"
)
// TestClaudePlan (#2025): the largest window of the models is the one
// Claude Code is told, and each model whose own differs from the one it
// would run at without an entry gets one; a "[1m]" model counts as 1M, the
// user's own entries are theirs, and one past its model's window holds the
// largest to that window.
func TestClaudePlan(t *testing.T) {
models := []claudeBudget{
{id: "mythos-magpie-1", window: 500000, compact: 272000},
{id: "mythos-magpie-2", window: 131000, compact: 272000},
{id: "anthropic/magpie-3[1m]", window: 1000000, long: true, compact: 272000},
{id: "anthropic/magpie-4", compact: -1},
{id: "magpie/v/gpt-6.1-sol", window: 400000, compact: -1},
}
top, entries := claudePlan(models, 272000, nil)
want := map[string]int{"mythos-magpie-2": 131000, "anthropic/magpie-4": 500000, "magpie/v/gpt-6.1-sol": 400000}
if top != 500000 || !reflect.DeepEqual(entries, want) {
t.Fatalf("magpie's default: %d %v", top, entries)
}
// no top-level window: every model runs at the largest, a [1m] one at 1M
top, entries = claudePlan(models, 0, nil)
want = map[string]int{"mythos-magpie-1": 272000, "mythos-magpie-2": 131000, "anthropic/magpie-3": 272000, "magpie/v/gpt-6.1-sol": 400000}
if top != 500000 || !reflect.DeepEqual(entries, want) {
t.Fatalf("no default: %d %v", top, entries)
}
// the user's own entries are theirs; "auto" on the 131K one holds the
// largest to its window
top, entries = claudePlan(models, 0, map[string]string{"mythos-magpie-2": "auto", "magpie/v/gpt-6.1-sol": "100000"})
want = map[string]int{"anthropic/magpie-3": 272000}
if top != 131000 || !reflect.DeepEqual(entries, want) {
t.Fatalf("the user's own: %d %v", top, entries)
}
// one within the model's window is kept as it is, and doesn't hold
// the largest
top, entries = claudePlan(models, 0, map[string]string{"mythos-magpie-2": "50000"})
want = map[string]int{"mythos-magpie-1": 272000, "anthropic/magpie-3": 272000, "magpie/v/gpt-6.1-sol": 400000}
if top != 500000 || !reflect.DeepEqual(entries, want) {
t.Fatalf("the user's 50000: %d %v", top, entries)
}
// the user's top-level window is the one a model without a threshold of
// its own is held to; two ids Claude Code matches as one get the
// smaller window
top, entries = claudePlan([]claudeBudget{{id: "a", window: 1000000}, {id: "b", window: 200000}, {id: "B", window: 128000}}, 300000, nil)
if top != 1000000 || !reflect.DeepEqual(entries, map[string]int{"b": 128000}) {
t.Fatalf("the user's top-level: %d %v", top, entries)
}
// only 1M ones, or no window known: nothing to tell
if top, entries = claudePlan([]claudeBudget{{id: "x[1m]", window: 1000000, long: true}, {id: "y"}}, 0, nil); top != 0 || entries != nil {
t.Fatalf("nothing known: %d %v", top, entries)
}
// an id magpie's editor can't put in a key path gets no entry
if _, entries = claudePlan([]claudeBudget{{id: "a", window: 400000}, {id: "b*", window: 100000}}, 0, nil); len(entries) != 0 {
t.Fatalf("b*: %v", entries)
}
}
// TestBudgetPath: a dot in a model's id is part of its key, as Claude Code
// reads it, and the "[1m]" mark is not.
func TestBudgetPath(t *testing.T) {
if p := budgetPath("magpie/v/gpt-6.1-sol[1m]"); p != `modelSettings.magpie/v/gpt-6\.1-sol.autoCompactWindow` {
t.Fatal(p)
}
if got := budgetsRead(`{"a.b":{"autoCompactWindow":5},"c":{"autoCompactWindow":"auto"},"d":{"effortLevel":"high"}}`); !reflect.DeepEqual(got, map[string]string{"a.b": "5", "c": "auto"}) {
t.Fatal(got)
}
}
+112 -42
View File
@@ -43,16 +43,18 @@ func desktopWindowSandbox(t *testing.T) (home, path string) {
return home, path
}
// TestClaudeDesktopContextWindow (#1458): Claude Desktop's Code tab runs
// Claude Code on ~/.claude/settings.json with the id Desktop lists a model
// by, and hands it nothing of the model's window; Claude Code 2.1.293 takes
// a mythos-magpie id it doesn't know for 200K unless
// TestClaudeDesktopContextWindow (#1458, #2025): Claude Desktop's Code tab
// runs Claude Code on ~/.claude/settings.json with the id Desktop lists a
// model by, and hands it nothing of the model's window; Claude Code 2.1.293
// takes a mythos-magpie id it doesn't know for 200K unless
// CLAUDE_CODE_MAX_CONTEXT_TOKENS says otherwise, so GPT 272K and Grok 256K
// showed "/ 200K". While Desktop is on magpie, magpie tells Claude Code the
// least window of Desktop's models (it takes one value for all of them),
// whatever model Claude Code itself is on, and never more than one of them
// takes; a model Claude Code knows, and one listed by its 1M id, are left
// out; the user's own value is theirs.
// showed "/ 200K". That variable is one value for all of them, and the
// least held a 1M model to 256K (#2025, 6.1sol), so while Desktop is on
// magpie it is the largest window of Desktop's and Claude Code's models,
// and each model below it runs at its own through
// modelSettings.<id>.autoCompactWindow. A model Claude Code knows, and one
// listed by its 1M id, are left out of the largest; the user's own value
// is theirs.
func TestClaudeDesktopContextWindow(t *testing.T) {
home, path := desktopWindowSandbox(t)
a := claude(home)
@@ -69,15 +71,20 @@ func TestClaudeDesktopContextWindow(t *testing.T) {
t.Fatal(err)
}
}
// the ids Desktop's Code tab hands Claude Code are ones it doesn't know
ids := map[string]string{}
for _, e := range provider.Catalog() {
if e.ID == "v/gpt" || e.ID == "v/grok" {
if id := gateway.DesktopID(e); !strings.HasPrefix(id, "mythos-magpie-") || claudeName(id) != "" {
t.Fatalf("%s is listed as %s", e.ID, id)
}
ids[e.ID] = gateway.DesktopID(e)
}
// the ids Desktop's Code tab hands Claude Code are ones it doesn't know
for _, id := range []string{"v/gpt", "v/grok"} {
if !strings.HasPrefix(ids[id], "mythos-magpie-") || claudeName(ids[id]) != "" {
t.Fatalf("%s is listed as %s", id, ids[id])
}
}
budget := func(ref string) string {
v, _ := edit.GetJSON(path, budgetPath(ids[ref]))
return v
}
// Claude Code on magpie on a model of no known window: nothing to
// tell it until Desktop is on magpie
@@ -86,40 +93,103 @@ func TestClaudeDesktopContextWindow(t *testing.T) {
t.Fatalf("plain, Desktop off: %q", window())
}
desktop("magpie")
if window() != "256000" {
t.Fatalf("Desktop on magpie, Claude Code on plain: %q, want Grok's 256000", window())
// glm's 1M is the largest; GPT is held to the working window (272K)
// as the top-level autoCompactWindow holds every model, Grok to its
// own 256K
if window() != "1000000" {
t.Fatalf("Desktop on magpie, Claude Code on plain: %q, want glm's 1000000", window())
}
// Claude Code's own model of 272K is told no more than Desktop's 256K
if v, _ := edit.GetJSON(path, "autoCompactWindow"); v != "272000" {
t.Fatalf("top-level autoCompactWindow %q, want the working window", v)
}
if v, _ := edit.GetJSON(path, "env."+claudeCompactEnv); v != "" {
t.Fatalf("%s %q would take precedence over every model's own", claudeCompactEnv, v)
}
if budget("v/grok") != "256000" || budget("v/gpt") != "" || budget("v/glm") != "" {
t.Fatalf("grok %q gpt %q glm %q: %s", budget("v/grok"), budget("v/gpt"), budget("v/glm"), readFile(path))
}
// Full window: each its own whole
s := settings.Load()
s.FullContext = true
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if v, _ := edit.GetJSON(path, "autoCompactWindow"); v != "" || window() != "1000000" || budget("v/grok") != "256000" || budget("v/gpt") != "272000" || budget("v/glm") != "" {
t.Fatalf("Full window: top %q window %q grok %q gpt %q glm %q", v, window(), budget("v/grok"), budget("v/gpt"), budget("v/glm"))
}
s.FullContext = false
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
// Claude Code's own model of 272K gets its own too
set("model", "v/gpt")
if window() != "256000" {
t.Fatalf("main gpt beside Desktop's grok: %q", window())
}
// a [1m] main model, which before told nothing at all
set("model", "v/glm[1m]")
if window() != "256000" {
t.Fatalf("[1m] main model: %q", window())
if window() != "1000000" || budget("v/grok") != "256000" {
t.Fatalf("main gpt beside Desktop's: %q %q", window(), budget("v/grok"))
}
// a model added later reaches it with the rest of the catalog
if err := provider.Save(provider.Provider{ID: "w", Name: "W", Chat: "http://127.0.0.1:1/v1", Key: "k", Models: []string{"small"}}); err != nil {
if err := provider.Save(provider.Provider{ID: "w", Name: "W", Chat: "http://127.0.0.1:1/v1", Key: "k", Models: []string{"small.v2"}}); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("w", "http://127.0.0.1:1/v1", []catalog.Model{{ID: "small", Context: 128000}}); err != nil {
if err := catalog.SaveLive("w", "http://127.0.0.1:1/v1", []catalog.Model{{ID: "small.v2", Context: 128000}}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil || window() != "128000" {
t.Fatalf("after w/small was added: %q %v", window(), err)
for _, e := range provider.Catalog() {
ids[e.ID] = gateway.DesktopID(e)
}
if err := a.Sync(); err != nil || window() != "1000000" || budget("w/small.v2") != "128000" {
t.Fatalf("after w/small.v2 was added: %q %q %v: %s", window(), budget("w/small.v2"), err, readFile(path))
}
if err := provider.Delete("w"); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil || window() != "256000" {
t.Fatalf("after w/small went: %q %v", window(), err)
if err := a.Sync(); err != nil || budget("w/small.v2") != "" || !strings.Contains(readFile(path), ids["v/grok"]) {
t.Fatalf("after w/small.v2 went: %v %s", err, readFile(path))
}
// the user's own entry for a model, and a window of their own past the
// model's, which holds the largest to it
if err := edit.SetJSON(path, edit.KV{Path: budgetPath(ids["v/grok"]), Value: "auto"}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil || window() != "256000" || budget("v/grok") != "auto" {
t.Fatalf("the user's grok entry: window %q grok %q %v", window(), budget("v/grok"), err)
}
if err := edit.SetJSON(path, edit.KV{Path: budgetPath(ids["v/grok"]), Value: 100000}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil || window() != "1000000" || budget("v/grok") != "100000" {
t.Fatalf("the user's grok 100000: window %q grok %q %v", window(), budget("v/grok"), err)
}
if err := edit.DelJSON(path, "modelSettings"); err != nil {
t.Fatal(err)
}
// the user's own top-level autoCompactWindow is theirs, and the one a
// model without an entry runs at
if err := edit.SetJSON(path, edit.KV{Path: "autoCompactWindow", Value: 500000}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if v, _ := edit.GetJSON(path, "autoCompactWindow"); v != "500000" || window() != "1000000" || budget("v/grok") != "256000" || budget("v/gpt") != "272000" || budget("v/glm") != "" {
t.Fatalf("the user's top-level: %q window %q grok %q gpt %q glm %q", v, window(), budget("v/grok"), budget("v/gpt"), budget("v/glm"))
}
// Desktop off: Claude Code's own model's window again, magpie's
// entries gone, the user's kept
if err := edit.SetJSON(path, edit.KV{Path: "modelSettings.mine.autoCompactWindow", Value: 90000}); err != nil {
t.Fatal(err)
}
// Desktop off: Claude Code's own model's window again, or none
desktop("")
set("model", "v/gpt")
if window() != "272000" {
t.Fatalf("Desktop off, main gpt: %q", window())
if window() != "272000" || budget("v/grok") != "" || budget("v/gpt") != "" {
t.Fatalf("Desktop off, main gpt: %q: %s", window(), readFile(path))
}
if v, _ := edit.GetJSON(path, "autoCompactWindow"); v != "500000" {
t.Fatalf("the user's top-level went: %s", readFile(path))
}
if v, _ := edit.GetJSON(path, "modelSettings.mine.autoCompactWindow"); v != "90000" {
t.Fatalf("the user's entry went: %s", readFile(path))
}
set("model", "claude-opus-4-8")
if window() != "" {
@@ -132,8 +202,8 @@ func TestClaudeDesktopContextWindow(t *testing.T) {
}
desktop("magpie")
set("model", "v/gpt")
if err := a.Sync(); err != nil || window() != "64000" {
t.Fatalf("the user's value was overwritten: %q %v", window(), err)
if err := a.Sync(); err != nil || window() != "64000" || budget("v/grok") != "" {
t.Fatalf("the user's value was overwritten: %q %v %s", window(), err, readFile(path))
}
set("model", "")
if window() != "64000" {
@@ -197,8 +267,8 @@ func TestClaudeDesktopAloneContextWindow(t *testing.T) {
}
desktop("magpie")
if window() != "256000" {
t.Fatalf("Desktop alone on magpie: window %q, want Grok's 256000", window())
if window() != "1000000" {
t.Fatalf("Desktop alone on magpie: window %q, want glm's 1000000", window())
}
if caps := get("env." + claudeCapsEnv); !strings.Contains(caps, "mythos-magpie-") {
t.Fatalf("Desktop alone on magpie: capabilities %q name none of Desktop's ids", caps)
@@ -207,7 +277,7 @@ func TestClaudeDesktopAloneContextWindow(t *testing.T) {
t.Fatalf("the user's own Claude Code setup changed: %s", readFile(path))
}
// the catalog's round keeps it
if err := a.Sync(); err != nil || window() != "256000" {
if err := a.Sync(); err != nil || window() != "1000000" {
t.Fatalf("after Sync: %q %v", window(), err)
}
// Claude Code wired in and out again while Desktop stays on
@@ -220,12 +290,12 @@ func TestClaudeDesktopAloneContextWindow(t *testing.T) {
if get("env.ANTHROPIC_BASE_URL") != "" {
t.Fatalf("Claude Code still on magpie: %s", readFile(path))
}
if window() != "256000" {
if window() != "1000000" {
t.Fatalf("Claude Code taken off magpie, Desktop still on: %q", window())
}
// Desktop off: magpie's values go
desktop("")
if window() != "" || get("env."+claudeCapsEnv) != "" {
if window() != "" || get("env."+claudeCapsEnv) != "" || get("modelSettings") != "" {
t.Fatalf("Desktop off: %s", readFile(path))
}
if get("theme") != "dark" || get("autoCompactWindow") != "300000" {