diff --git a/internal/agent/claude.go b/internal/agent/claude.go index 6f1c09716..768c68905 100644 --- a/internal/agent/claude.go +++ b/internal/agent/claude.go @@ -679,8 +679,12 @@ func claudeIn(at place) *Agent { n = s.Compact() // magpie's default gives way to an autoCompactWindow the user // set in Claude Code (/autocompact, its settings), which the env - // would take precedence over; one typed in magpie doesn't (#876) - if s.CompactAt == 0 && claudeOwnCompact(path, main) { + // would take precedence over; one typed in magpie doesn't (#876). + // So does it to a window the user typed for the main model, on + // its group or its provider: Claude Code then runs to the window + // it is told (claudeContextEnv), as Codex is (provider.CompactFor, + // #1809) + if s.CompactAt == 0 && (claudeOwnCompact(path, main) || provider.WindowTyped(main)) { n = 0 } } diff --git a/internal/agent/route.go b/internal/agent/route.go index c13855376..092b90f7b 100644 --- a/internal/agent/route.go +++ b/internal/agent/route.go @@ -118,9 +118,10 @@ func magpieModels(agent string) []catalog.Model { for i, e := range shown { m := catalog.Model{ID: e.ID, Name: labels[i], Provider: firstOf(e.Provider.Catalogs()), Efforts: e.Efforts, Images: e.Images || seen, ImageInput: e.ImageInput, Context: e.Context, Output: e.Output, AgentsV2: e.AgentsV2, Reasoning: e.Reasoning} // where Codex and Claude Code compact it (#876): a threshold the - // user set on the model or on its provider, else the one for every - // model, which codexcat answers from the settings it is given - m.Compact = provider.CompactSetIn(st, e.ID, find) + // user set on the model or on its provider, else a window they + // typed whole (#1809), else the one for every model, which codexcat + // answers from the settings it is given + m.Compact = provider.CompactFor(st, e, find) // what a call costs the user (Pi's cost, #781) if pr, ok := provider.EntryPriceIn(st, find, e); ok { m.Price = &pr diff --git a/internal/agent/window_typed_test.go b/internal/agent/window_typed_test.go new file mode 100644 index 000000000..8a1570b09 --- /dev/null +++ b/internal/agent/window_typed_test.go @@ -0,0 +1,114 @@ +package agent + +import ( + "encoding/json" + "path/filepath" + "testing" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/edit" + "github.com/yetone/magpie/internal/provider" +) + +// A window the user typed is the one Codex and Claude Code run to (#1809, +// a DeepSeek V4.1 Flash group set to 500K): a routing group's own size, or +// one set on the provider for a model, isn't cut to magpie's default +// working window (272K, which Codex ran to 95% of: ~258K). Codex is told +// it as its context_window, on disk and in the gateway's list, with no +// max_context_window; Claude Code is handed no CLAUDE_CODE_AUTO_COMPACT_ +// WINDOW, so it runs to the CLAUDE_CODE_MAX_CONTEXT_TOKENS it is told. A +// model whose window nobody typed keeps the default, and a size typed in +// Settings (Compact at) still comes before a typed window. +func TestTypedWindowReachesCodexAndClaudeCode(t *testing.T) { + home := syncHome(t) + catalog.Changed = SyncCatalog + t.Cleanup(func() { catalog.Changed = nil }) + must := func(err error) { + t.Helper() + if err != nil { + t.Fatal(err) + } + } + must(provider.Save(provider.Provider{ID: "ds", Name: "DS", Key: "k", Chat: "http://127.0.0.1:1/v1", + Models: []string{"flash", "pro"}, Contexts: map[string]int{"pro": 400000}})) + must(catalog.SaveLive("ds", "http://127.0.0.1:1/v1", []catalog.Model{{ID: "flash", Context: 1000000}, {ID: "pro", Context: 1000000}})) + must(provider.SaveGroup(provider.Group{ID: "flash", Name: "Flash", Members: []string{"ds/flash"}, Context: 500000})) + + must(codex(home).Fields[0].Set("group/flash")) + type window struct{ context, max int } + onDisk := func() map[string]window { + t.Helper() + var got struct { + Models []struct { + Slug string `json:"slug"` + Context int `json:"context_window"` + Max int `json:"max_context_window"` + } `json:"models"` + } + raw := readFile(filepath.Join(home, ".codex", "magpie-models.json")) + if err := json.Unmarshal([]byte(raw), &got); err != nil { + t.Fatalf("%v: %s", err, raw) + } + out := map[string]window{} + for _, m := range got.Models { + out[m.Slug] = window{m.Context, m.Max} + } + return out + } + listed := func() map[string]int { + out := map[string]int{} + for _, m := range provider.CodexListed() { + out[m.ID] = m.Compact + } + return out + } + expect := func(what string, want map[string]window, compacts map[string]int) { + t.Helper() + got := onDisk() + for slug, w := range want { + if got[slug] != w { + t.Errorf("%s: on disk %s is told %+v; want %+v", what, slug, got[slug], w) + } + } + l := listed() + for id, n := range compacts { + if l[id] != n { + t.Errorf("%s: the gateway's list compacts %s at %d; want %d", what, id, l[id], n) + } + } + } + expect("typed windows", map[string]window{ + "group/flash": {500000, 0}, // the group's own size + "ds/pro": {400000, 0}, // the provider's for the model + "ds/flash": {272000, 1000000}, // nobody typed it: the default + }, map[string]int{"group/flash": 500000, "ds/pro": 400000, "ds/flash": 0}) + + path := filepath.Join(home, ".claude", "settings.json") + writeFile(t, path, `{}`) + a := claude(home) + env := func(k string) string { v, _ := edit.GetJSON(path, "env."+k); return v } + must(a.Field("model").Set("group/flash")) + if c, w := env(claudeCompactEnv), env(claudeContextEnv); c != "" || w != "500000" { + t.Errorf("Claude Code on the group: compact %q, window %q; want none and 500000", c, w) + } + must(a.Field("model").Set("ds/pro")) + if c, w := env(claudeCompactEnv), env(claudeContextEnv); c != "" || w != "400000" { + t.Errorf("Claude Code on ds/pro: compact %q, window %q; want none and 400000", c, w) + } + must(a.Field("model").Set("ds/flash")) + if c := env(claudeCompactEnv); c != "272000" { + t.Errorf("Claude Code on ds/flash: compact %q; want the default 272000", c) + } + + // a size the user typed in Settings comes first + must(provider.SetCompactAt(300000)) + SyncCatalog() + expect("Compact at 300K", map[string]window{ + "group/flash": {300000, 500000}, + "ds/pro": {300000, 400000}, + }, map[string]int{"group/flash": 0, "ds/pro": 0}) + must(a.Field("model").Set("group/flash")) + if c := env(claudeCompactEnv); c != "300000" { + t.Errorf("Claude Code on the group with Compact at 300K: %q", c) + } +} diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index 38965aab1..2f7a6bd1e 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -56,9 +56,10 @@ type Model struct { // Output is the most tokens a reply may hold, when known. Output int `json:",omitempty"` // Compact is where the user has Codex compact a conversation on this - // model (#876), its own or its provider's (settings.ModelCompacts): the - // window it is told when Context is longer. 0 is none set, for the - // one for every model (settings.Compact). + // model (#876), its own or its provider's (settings.ModelCompacts), or + // Context itself when that is a window the user typed (#1809, + // provider.CompactFor): the window it is told when Context is longer. + // 0 is none set, for the one for every model (settings.Compact). Compact int `json:",omitempty"` // Plain, on a model of another magpie's list, is its name there alone // (display_name), when Name is its label there, with that magpie's diff --git a/internal/provider/codex_models.go b/internal/provider/codex_models.go index 8b90e1937..ff4add287 100644 --- a/internal/provider/codex_models.go +++ b/internal/provider/codex_models.go @@ -581,7 +581,7 @@ func codexListed(shown []Entry, members func(id string) []Member, own bool) []ca continue } m := catalog.Model{ID: e.ID, Name: labels[i], Efforts: e.Efforts, Images: e.Images || seen, Context: e.Context, AgentsV2: e.AgentsV2} - m.Compact = compactSet(s, e.ID, find) + m.Compact = CompactFor(s, e, find) CodexTiers(&m, e, members) ms = append(ms, m) } diff --git a/internal/provider/compact.go b/internal/provider/compact.go index c5d77b9e7..bf120cbbd 100644 --- a/internal/provider/compact.go +++ b/internal/provider/compact.go @@ -30,19 +30,57 @@ func CompactSet(id string) int { return compactSet(settings.Load(), id, GroupFinder()) } -// CompactSetIn is CompactSet for a caller that already holds the settings -// and the group finder: an agent's list is built for hundreds of models at -// magpie's start, and reading both again for each is what EffectivePriceIn -// saves the same loop. -func CompactSetIn(s settings.Settings, id string, find func(string) (Group, []Member, bool)) int { - return compactSet(s, id, find) +// CompactFor is where Codex compacts a conversation on e (catalog.Model's +// Compact): the threshold the user set on it or its provider (CompactSet), +// else, when its window is one the user typed (Entry.WindowSet) and no +// size of theirs is typed in Settings, that whole window (#1809: a group +// set to 500K was told Codex as magpie's default 272K, which Codex runs +// to 95% of). magpie's default gives way to a window the user typed as +// it does to one they set in Claude Code (#876); a threshold they typed +// still comes first. 0 leaves it to settings.Compact. It takes the +// settings and the group finder from its caller: an agent's list is built +// for hundreds of models at magpie's start, and reading both again for +// each is what EffectivePriceIn saves the same loop. +func CompactFor(s settings.Settings, e Entry, find func(string) (Group, []Member, bool)) int { + if n := compactSet(s, e.ID, find); n > 0 { + return n + } + if e.WindowSet && s.CompactAt == 0 { + return e.Context + } + return 0 +} + +// WindowTyped says the window agents are told for a model, by its +// catalog id (as CompactSet takes it), is one the user typed: the +// group's own size, or one set on the provider for the model or all of +// its models. Claude Code then isn't handed magpie's default compaction +// window for it (CompactFor's rule). +func WindowTyped(id string) bool { + id = catalogID(id) + if strings.HasPrefix(id, GroupPrefix) { + g, _, ok := GroupFinder()(id) + return ok && g.Context > 0 + } + pid, model, ok := strings.Cut(id, "/") + if !ok { + return false + } + p, err := Find(pid) + return err == nil && p.ContextOf(model) > 0 +} + +// catalogID is an agent's model id as the catalog has it: magpie/ and +// Claude Code's [1m] mark taken off. +func catalogID(id string) string { + return strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(id), "magpie/"), "[1m]") } func compactSet(s settings.Settings, id string, find func(string) (Group, []Member, bool)) int { if len(s.ModelCompacts) == 0 { return 0 } - id = strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(id), "magpie/"), "[1m]") + id = catalogID(id) if strings.HasPrefix(id, GroupPrefix) { _, ms, _ := find(id) least := 0 diff --git a/internal/provider/group.go b/internal/provider/group.go index 718c1ff4c..b028c52de 100644 --- a/internal/provider/group.go +++ b/internal/provider/group.go @@ -833,7 +833,7 @@ func groupEntries(entries []Entry) []Entry { } ruledEntry(&e, g.Live(), ms, entries) if g.Context > 0 { - e.Context = g.Context + e.Context, e.WindowSet = g.Context, true } else if g.Context == ContextSmallest && smallest > 0 { e.Context = smallest } diff --git a/internal/provider/models.go b/internal/provider/models.go index 36d7ec764..e2e5176c0 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -1408,6 +1408,12 @@ type Entry struct { // Context is the tokens a prompt may hold, when known (a group's: the // least of its members') Context int `json:"context,omitempty"` + // WindowSet is set when Context is a window the user typed: on the + // provider for the model or for all of its models (ContextOf), or on + // the group (Group.Context, a size). Codex and Claude Code are then + // told it whole rather than magpie's default working window + // (CompactFor, #1809). + WindowSet bool `json:"-"` // Output is the most tokens a reply may hold, when known (a group's: // the least of its members') Output int `json:"output,omitempty"` @@ -1541,7 +1547,7 @@ func entryFor(p Provider, m catalog.Model, s settings.Settings) Entry { // an agent's list showed the whole magpie// (#955) name := cmp.Or(m.Name, m.ID) e := Entry{ID: p.ID + "/" + m.ID, Model: m.ID, Family: p.Family, Name: name, Efforts: effortsOf(m), Provider: p, - Images: images, ImageInput: imageInput, Context: ctx, Output: output, Free: m.Free, Rate: m.Rate, RateWas: m.RateWas, Tiers: m.Tiers, Released: m.Released, + Images: images, ImageInput: imageInput, Context: ctx, WindowSet: p.ContextOf(m.ID) > 0, Output: output, Free: m.Free, Rate: m.Rate, RateWas: m.RateWas, Tiers: m.Tiers, Released: m.Released, Inputs: m.InputModalities, Outputs: m.OutputModalities} if n, ok := modelNameIn(s.ModelNames, p.ID, m.ID); ok { e.Name, e.Default = n, name