provider, catalog: a model listed with a 512-token window is told to agents at models.dev's window, or none, never 512 (H20 on Discord)

H20 and others saw free models (hy3, Cline's free ones) at a 512-token
context window, and Claude Code and Codex then treated them as unusable:
they compact at once. Every agent and the gateway read a model's window
from its catalog entry (Entry.Context, from WindowOf and ListedWindow),
which took any positive window the vendor's or plugin's list gave as said.

Where the 512 itself comes from isn't seen here: models.dev's cache
(2026-10-12), Cline's live /ai/cline/models and recommended feed, the
WorkBuddy and Cline plugins' static lists and magpie's own code give no
window under 4095 for any of these models. It is in a vendor's live list
read at run time (WorkBuddy's /v3/config maxInputTokens or a plugin's
list); so the floor is where every listed window is read:

- catalog.LeastWindow (2048) and catalog.Window: a window under it is not
  known. The smallest real chat windows are about 4096
  (gpt-3.5-turbo-instruct's 4095, mythomax's 4096) and stand.
- mdModel.window: a models.dev limit under it doesn't vote, so the other
  limit, or nothing, answers.
- ListedWindow: a vendor's window under it falls back to models.dev's
  for the model, else 0 (the agent's default).
- pluginCatalog: a plugin's input or context under it is none, so a
  512 prompt cap under a 192K window leaves 192K.
- planModels (Cline): a free model's 512 takes models.dev's too.
- A window the user sets (Contexts) is theirs, however small.

Tests, with the literal 512: TestTinyListedWindowIsUnknown (a Cline
plugin listing cline-free/mimo-v2.6-flash and cline-free/hy3 at 512 is
served at models.dev's 1048576 and 192000; one nothing knows at 0; a real
4096 stands; ListedWindow on a built-in's hy3 at 512 gives 192000; a
user-set 1024 stands) and four new cases in
TestWindowClampsInputAboveContext. Both fail on the code before (512, want
1048576 / 192000 / 0) and pass with it.

Not verified: the reporter's own list, so which vendor sent 512 is
inferred, not seen; not tried in a real Claude Code or Codex.

Run for both commits, on origin/main 94aa15397, sandbox HOME,
private GOCACHE (the shared one was being emptied under the build), load
average 70-86: gofmt -l clean; go vet ./... (darwin) and go vet -tags nogui
./... with GOOS=linux and GOOS=windows pass; go build -tags nogui ./...
passes. go test -tags nogui: catalog, agent, plugin, library pass;
provider -run 'Window|Listed|Plugin|Cline|Free|Catalog|Models' passes, and
the whole of provider passed before the rebase (794s) but for
TestCopilotSecretToolUsername, which fails only inside the command sandbox
(its secret-tool script can't run there) and passes outside it; gateway
-run 'TestVertex|Credit|Rest|Quota|Rate|Free|Plugin' passes. The whole
gateway and provider packages hit their 25m and 9m timeouts on this
machine's load, with stacks in tests still running (vertex, signin_proxy),
not stuck; those two packages weren't run whole at this head.
Rebased again onto 24350a372 (the doc's Verification list took both
lines): go vet -tags nogui ./internal/... and go test -tags nogui -run
'Credit|Rest|Quota|Rate|Free|Group|Window|Listed|AutoMode|Codex' on
gateway, catalog and provider pass.
This commit is contained in:
yetone
2026-10-12 22:33:07 +08:00
parent 974aaf7e22
commit b0a904bd9a
5 changed files with 140 additions and 13 deletions
+27 -5
View File
@@ -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
+6
View File
@@ -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
+95
View File
@@ -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)
}
}
+5 -3
View File
@@ -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)
}
+7 -5
View File
@@ -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