provider, agent: a context window the user typed on a routing group or a provider is the one Codex and Claude Code run to, not magpie's default 272K (#1809)
A DeepSeek V4.1 Flash routing group set to 500K reached the catalog as Context 500000, but magpie's default working window (settings.Working, 272K, #876) cut it: Codex's magpie-models.json said context_window 272000, max_context_window 500000, and Codex runs to 95% of context_window, the ~258K in the report. Claude Code showed 500K (CLAUDE_CODE_MAX_CONTEXT_TOKENS) while magpie also wrote CLAUDE_CODE_AUTO_COMPACT_WINDOW=272000, so it compacted at 272K too and only displayed the larger number. From Codex's source (openai/codex @0c7362d, codex-cli 0.162.1): config.toml's model_context_window sets context_window to min(value, max_context_window); usable = context_window x effective_context_window_percent (95 by default); the auto-compact limit is min(model_auto_compact_token_limit, 90% of context_window). magpie writes neither config key and still doesn't, so a user's own model_context_window keeps working as before. magpie's default compaction now gives way to a window the user typed, as it already gives way to Claude Code's own autoCompactWindow (602d4ec6): - provider: Entry.WindowSet marks a window typed on the provider for the model or all its models (ContextOf) or on the group (a size). CompactFor answers the threshold Codex is told: a per-model or provider Compact at first (CompactSet), else that whole typed window when no size is typed in Settings, else 0 (the default). Used by the gateway's Codex list (codexListed) and WindowTyped for Claude Code. - agent: magpie-models.json (and the WSL twin) take CompactFor, so the group says context_window 500000 with no max; Claude Code gets no AUTO_COMPACT_WINDOW on a typed window unless Compact at is typed in Settings. - A model nobody typed a window for keeps 272K, and a Compact at size typed in Settings still comes before a typed window. - gui: unchanged.f36e744c3shortened Settings > Long conversations to one line upstream; it no longer spells out precedence. Run: - internal/agent TestTypedWindowReachesCodexAndClaudeCode: group 500K, provider per-model 400K, an untyped 1M model, then Compact at 300K; checks magpie-models.json, the gateway's Codex list and Claude Code's env. On the old code it fails 6 ways (272000/500000 and 272000/400000 on disk, list Compact 0, Claude compact 272000), checked by reverting the non-test files. - Sandbox HOME with the built binary and real codex 0.162.1 (`codex debug models`): old binary group/ds-flash 272000 / max 500000; new 500000, no max. A per-model `context` override of 1m: old 272000 / 1000000, new 1000000. `magpie model compact 300k`: 300000 with the typed window as max. Claude Code in the sandbox: old wrote AUTO_COMPACT_WINDOW=272000 + MAX_CONTEXT_TOKENS=500000, new only MAX_CONTEXT_TOKENS=500000. - gofmt -l, go vet (darwin, GOOS=linux -tags nogui, GOOS=windows), the three builds, go test -tags nogui ./...: 45 packages ok. At load average ~115 agent, gateway, library and provider hit the 10m timeout with no failure. Rerun with -timeout 40m, agent, library and provider pass. gateway failed three timing tests that pass 3 of 3 alone: TestOTelWaterfallIncludesUnbilledFallback (#1794, already fixed on origin/main by6ae6b3402), TestCodexHeardWithinItsIdleTimeout (#1800, open) and TestIntentClassifierFails (filed as #1829). Not tried: a real `codex exec` turn against a sandbox gateway (the catalog Codex reads was checked with `codex debug models` instead), and Windows itself; the path is the same Go code on every OS.
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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/<provider>/<model> (#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
|
||||
|
||||
Reference in New Issue
Block a user