diff --git a/internal/catalog/catalog.go b/internal/catalog/catalog.go index f92b14ffa..7234aa15b 100644 --- a/internal/catalog/catalog.go +++ b/internal/catalog/catalog.go @@ -361,14 +361,36 @@ func (p mdProvider) levels(m mdModel) []string { // models.dev gives one (gpt-5's 272K of its 400K), else the whole context. // A row whose input limit sits above its window (Cloudflare AI Gateway's // gpt-5 at 272K over 128K) holds the window: the cap never exceeds it. +// +// Either limit under LeastWindow says nothing (Window), so the other one +// answers, or none does. func (m mdModel) window() int { - if m.Limit.Input > 0 { - if m.Limit.Context > 0 && m.Limit.Context < m.Limit.Input { - return m.Limit.Context + in, ctx := Window(m.Limit.Input), Window(m.Limit.Context) + if in > 0 { + if ctx > 0 && ctx < in { + return ctx } - return m.Limit.Input + return in } - return m.Limit.Context + return ctx +} + +// LeastWindow is the smallest context window a list is taken at its word +// for. Less can't hold an agent's system prompt, let alone a turn, and an +// agent told it compacts at once or won't send at all (H20 on Discord: hy3 +// and Cline's free models listed at 512, which Claude Code and Codex took +// as the model's window). The smallest real chat models' windows are +// about 4096 (gpt-3.5-turbo-instruct's 4095, mythomax's 4096): those stand. +const LeastWindow = 2048 + +// Window is n as a model's context window: n, or 0 — not known, for +// models.dev, a family's window or the agent's default to answer — when n +// is under LeastWindow. +func Window(n int) int { + if n < LeastWindow { + return 0 + } + return n } var modelsDevURL = "https://models.dev/api.json" // a var for tests diff --git a/internal/catalog/window_clamp_test.go b/internal/catalog/window_clamp_test.go index 031a70986..2e7150117 100644 --- a/internal/catalog/window_clamp_test.go +++ b/internal/catalog/window_clamp_test.go @@ -15,6 +15,12 @@ func TestWindowClampsInputAboveContext(t *testing.T) { {"input above context", 128_000, 272_000, 128_000}, {"input only", 0, 200_000, 200_000}, {"context only", 1_000_000, 0, 1_000_000}, + // a limit of 512 is none (H20 on Discord): the other answers, or + // nothing does; gpt-3.5-turbo-instruct's real 4095 stands + {"context 512", 512, 0, 0}, + {"input 512 under context", 192_000, 512, 192_000}, + {"context 512 over input", 512, 192_000, 192_000}, + {"small but real", 4095, 0, 4095}, } { m := mdModel{} m.Limit.Context, m.Limit.Input = tc.context, tc.in diff --git a/internal/provider/least_window_test.go b/internal/provider/least_window_test.go new file mode 100644 index 000000000..3aa11508d --- /dev/null +++ b/internal/provider/least_window_test.go @@ -0,0 +1,95 @@ +package provider + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" + + "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/plugin" + "github.com/yetone/magpie/internal/settings" +) + +// A model listed with a 512-token window (H20 on Discord: hy3 and Cline's +// free models) isn't told to agents at 512: Claude Code and Codex took it as +// the window and compacted at once. models.dev's window for the model +// answers, else none (the agent's default). A small window that is real +// (4096) stands, and so does one the user sets. +func TestTinyListedWindowIsUnknown(t *testing.T) { + claudeHome(t) + t.Setenv("MAGPIE_BUN", "") + os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755) + // models.dev's rows as it has them (2026-10-12): hy3's input under its + // context, mimo's context + os.WriteFile(catalog.CachePath(), []byte(`{ + "tencent-tokenhub":{"id":"tencent-tokenhub","models":{"hy3":{"id":"hy3","limit":{"context":256000,"input":192000,"output":128000}}}}, + "xiaomi":{"id":"xiaomi","models":{"mimo-v2.6-flash":{"id":"mimo-v2.6-flash","limit":{"context":1048576,"output":131072}}}}}`), 0o644) + catalog.Reset() + t.Cleanup(catalog.Reset) + spec := "@magpie-community/opencode-cline-auth" + write := func(name string, v any) { + t.Helper() + b, _ := json.Marshal(v) + os.MkdirAll(settings.Dir(), 0o700) + if err := os.WriteFile(filepath.Join(settings.Dir(), name), b, 0o600); err != nil { + t.Fatal(err) + } + } + write("plugins.json", map[string]any{"plugins": []map[string]any{{"spec": spec}}}) + write("plugin-auth.json", map[string]map[string]any{"cline": {"type": "api", "key": "sk-cline-1234"}}) + plugin.UseCached([]plugin.Provider{{ID: "cline", Spec: spec, Name: "Cline", + Methods: []plugin.Method{{Type: "api", Label: "API key"}}, + Accounts: []plugin.Account{{Key: "cline", Type: "api"}}, + Models: []plugin.Model{ + // the literal 512 as listed + {ID: "cline-free/mimo-v2.6-flash", Name: "MiMo V2.6 Flash (free)", Free: true, Context: 512}, + {ID: "cline-free/hy3", Name: "Hy3 (free)", Free: true, Input: 512, Context: 512}, + // a prompt cap of 512 under a real window: the window answers + {ID: "cline-free/hy3-x", Name: "Hy3-X (free)", Free: true, Input: 512, Context: 192_000}, + // nothing knows it: no window, not 512 + {ID: "cline-free/unheard-of", Name: "Unheard Of", Free: true, Context: 512}, + // a real small window stands + {ID: "cline-pass/mythomax-l2-13b", Name: "MythoMax", Context: 4096}, + }}}) + t.Cleanup(func() { plugin.UseCached(nil) }) + + for id, want := range map[string]int{ + "cline/cline-free/mimo-v2.6-flash": 1_048_576, + "cline/cline-free/hy3": 192_000, + "cline/cline-free/hy3-x": 192_000, + "cline/cline-free/unheard-of": 0, + "cline/cline-pass/mythomax-l2-13b": 4096, + } { + if got := pluginWindow(t, id); got != want { + t.Errorf("%s: %d, want %d", id, got, want) + } + } + + // a built-in's list (WorkBuddy's /v3/config maxInputTokens) the same + for _, tc := range []struct { + m catalog.Model + want int + }{ + {catalog.Model{ID: "hy3", Context: 512}, 192_000}, + {catalog.Model{ID: "unheard-of", Context: 512}, 0}, + {catalog.Model{ID: "unheard-of", Context: 4096}, 4096}, + {catalog.Model{ID: "hy3", Context: 200_000}, 200_000}, + } { + if got := ListedWindow(tc.m); got != tc.want { + t.Errorf("ListedWindow(%s at %d): %d, want %d", tc.m.ID, tc.m.Context, got, tc.want) + } + } + + // the user's own window is theirs, however small + p, err := Find("cline") + if err != nil { + t.Fatal(err) + } + if err := SetContext(*p, "cline-free/unheard-of", 1024); err != nil { + t.Fatal(err) + } + if got := pluginWindow(t, "cline/cline-free/unheard-of"); got != 1024 { + t.Errorf("set by the user: %d, want 1024", got) + } +} diff --git a/internal/provider/models.go b/internal/provider/models.go index 3e03e56dc..36d7ec764 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -610,7 +610,7 @@ func (p Provider) planModels(ms []catalog.Model) []catalog.Model { if m.Free { id = m.ID[strings.LastIndex(m.ID, "/")+1:] } - if m.Context == 0 { + if catalog.Window(m.Context) == 0 { out[i].Context = catalog.ContextOf(id) } if m.Output == 0 { @@ -1726,9 +1726,11 @@ func IDs() []string { // A vendor models.dev doesn't list (a custom provider, a proxy, a plugin // whose list says none, as Cline's) serves models it knows from others, under // the vendor's prefix too: cline-free/mimo-v2.6-flash is mimo-v2.6-flash. +// A window the vendor lists under catalog.LeastWindow (512, H20 on Discord) +// is no window: models.dev's answers, else none, and the agent's default. func ListedWindow(m catalog.Model) int { - if m.Context > 0 { - return m.Context + if n := catalog.Window(m.Context); n > 0 { + return n } return catalog.ContextOf(m.ID) } diff --git a/internal/provider/plugins.go b/internal/provider/plugins.go index 5d8e53ff8..b8a8ad767 100644 --- a/internal/provider/plugins.go +++ b/internal/provider/plugins.go @@ -181,20 +181,22 @@ func pluginCatalog(pp plugin.Provider) []catalog.Model { c := catalog.Model{ ID: m.ID, Name: m.Name, Provider: pp.ID, Released: m.Released, APIs: []string{string(pluginProtocol(pp.ID, m))}, Images: m.Image, - Context: m.Input, Output: m.Output, Free: m.Free, + Context: catalog.Window(m.Input), Output: m.Output, Free: m.Free, Rate: m.Rate, RateWas: m.RateWas, // a decision model the plugin marked counts only where it // serves a decision API (pp.Decide) Decides: pp.Decide && m.Decides, } - if c.Context == 0 { - c.Context = m.Context - } else if m.Context > 0 && m.Context < c.Context { + // a limit under catalog.LeastWindow (512, H20 on Discord) is none: + // the other one, or models.dev, answers (ListedWindow) + if window := catalog.Window(m.Context); c.Context == 0 { + c.Context = window + } else if window > 0 && window < c.Context { // the prompt cap a plugin names wins, but never above the // window it also names: a row with input over context // (opencode/hy3-free's 192K over 190K) was served past its // window (#1286) - c.Context = m.Context + c.Context = window } // Cursor's own ids no catalog knows: one not named a 1M model // holds what the catalog knows its base to, as the built-in's did