provider: a window set for all of a provider's models is no larger than a model's own (#2112)
OpenCode Go set to 512K for all its models (Contexts["*"]) told Claude Code 512K for cat-jam-free too, whose window is 262144 (models.dev), so Claude Code ran past the model's real window before compacting and the vendor refused the request. WindowOf now caps the "*" value at the model's own window when one is known: the listed window (vendor list or models.dev), or the larger max_context_window a model may be raised to (Codex's 872K), so a Codex raised to 600K stays raised. A model with no known window takes the "*" value as before, and a window set for the model itself is the user's word and is told as set. The gateway's windowOf (fallback.go) reads the served entry's window, which is WindowOf's, after a per-model value, so the gateway's own fit uses the same number the agent is told. Run: - TestProviderWindowNoLargerThanTheModels: the models.dev bytes for opencode-go cat-jam-free (262144) and a 1M model with "*" at 512000: 262144, 512000, 512000 for an unknown model, a per-model 400000 kept; Codex 272K/872K with "*" 600K stays 600K, capped at a 400K max. FAILs on the old WindowOf (cat-jam-free told 512000). - gofmt -l; go vet darwin, GOOS=linux -tags nogui, GOOS=windows on internal/provider and internal/gateway; go test -tags nogui ./internal/provider/ ./internal/gateway/. Not tried: a real OpenCode Go account with Claude Code.
This commit is contained in:
@@ -1175,9 +1175,11 @@ func tooLongUnsaid(p provider.Provider, model string, tokens, status int, body [
|
||||
}
|
||||
|
||||
// windowOf is the tokens a provider's model takes, where known: the
|
||||
// user's (Provider.ContextOf), else its catalog entry's.
|
||||
// user's for the model, else its catalog entry's (Provider.WindowOf, where
|
||||
// the one set for all the provider's models is no larger than the model's
|
||||
// own, #2112), else the one set for all of them.
|
||||
func windowOf(p provider.Provider, model string) int {
|
||||
if n := p.ContextOf(model); n > 0 {
|
||||
if n := p.Contexts[model]; n > 0 {
|
||||
return n
|
||||
}
|
||||
for _, e := range provider.Served() {
|
||||
@@ -1185,7 +1187,7 @@ func windowOf(p provider.Provider, model string) int {
|
||||
return e.Context
|
||||
}
|
||||
}
|
||||
return 0
|
||||
return p.ContextOf(model)
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/catalog"
|
||||
)
|
||||
|
||||
func TestContextSetByUser(t *testing.T) {
|
||||
@@ -128,3 +131,58 @@ func TestContextSetOnAccount(t *testing.T) {
|
||||
t.Errorf("set: %d, was %d", got, before)
|
||||
}
|
||||
}
|
||||
|
||||
// #2112: OpenCode Go set to 512K for all of its models, to spend less.
|
||||
// cat-jam-free, which models.dev says takes 262144, is told 262144, not
|
||||
// 512K; DeepSeek V4.1 Flash (1M) is told the 512K set, and so is a model
|
||||
// whose window nothing says. A window set for the model itself is the
|
||||
// user's word on it, larger than its own or not.
|
||||
func TestProviderWindowNoLargerThanTheModels(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
t.Setenv("USERPROFILE", home)
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
||||
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
||||
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
|
||||
if err := os.WriteFile(catalog.CachePath(), []byte(`{"opencode-go":{"models":{
|
||||
"cat-jam-free":{"id":"cat-jam-free","name":"Cat Jam Free","limit":{"context":262144,"output":131072}},
|
||||
"deepseek-v4.1-flash":{"id":"deepseek-v4.1-flash","name":"DeepSeek V4.1 Flash","limit":{"context":1048576,"output":393216}}}}}`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
catalog.Reset()
|
||||
t.Cleanup(catalog.Reset)
|
||||
of := func(id string) int {
|
||||
for _, e := range Served() {
|
||||
if e.ID == id {
|
||||
return e.Context
|
||||
}
|
||||
}
|
||||
t.Fatalf("no %s", id)
|
||||
return 0
|
||||
}
|
||||
if err := Save(Provider{ID: "opencode-go", Name: "OpenCode Go", Key: "k", Chat: "https://opencode.test/v1", Catalog: "opencode-go",
|
||||
Models: []string{"cat-jam-free", "deepseek-v4.1-flash", "unheard-of"}, Contexts: map[string]int{"*": 512000}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for id, want := range map[string]int{"opencode-go/cat-jam-free": 262144, "opencode-go/deepseek-v4.1-flash": 512000, "opencode-go/unheard-of": 512000} {
|
||||
if got := of(id); got != want {
|
||||
t.Errorf("%s: told %d, want %d", id, got, want)
|
||||
}
|
||||
}
|
||||
if err := Save(Provider{ID: "opencode-go", Name: "OpenCode Go", Key: "k", Chat: "https://opencode.test/v1", Catalog: "opencode-go",
|
||||
Models: []string{"cat-jam-free"}, Contexts: map[string]int{"*": 512000, "cat-jam-free": 400000}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := of("opencode-go/cat-jam-free"); got != 400000 {
|
||||
t.Errorf("set for the model: told %d, want 400000", got)
|
||||
}
|
||||
// a window asked for above the listed one, Codex's max_context_window,
|
||||
// is the model's own too: Codex raised to 600K stays raised (#120)
|
||||
p := Provider{ID: "codex", Contexts: map[string]int{"*": 600000}}
|
||||
if got := p.WindowOf(catalog.Model{ID: "gpt-6-sol", Context: 272000, MaxContext: 872000}); got != 600000 {
|
||||
t.Errorf("codex raised: %d", got)
|
||||
}
|
||||
if got := p.WindowOf(catalog.Model{ID: "gpt-6-sol", Context: 272000, MaxContext: 400000}); got != 400000 {
|
||||
t.Errorf("codex past its max: %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2326,14 +2326,26 @@ func ListedWindow(m catalog.Model) int {
|
||||
}
|
||||
|
||||
// WindowOf is the window one of the provider's models takes, as agents are
|
||||
// told it and requests are routed on it: the one the user set (ContextOf)
|
||||
// over ListedWindow. The catalog's entries and the Providers page's model
|
||||
// lists both read it, so the page shows the window agents are told.
|
||||
// told it and requests are routed on it: the one the user set for the
|
||||
// model over ListedWindow. The one set for all of the provider's models
|
||||
// ("*") is no larger than a model's own, where that is known (its listed
|
||||
// window, or the most it may be asked for, MaxContext): set to 512K to
|
||||
// spend less, it told opencode-go's cat-jam-free, which takes 262K, 512K
|
||||
// (#2112). One set for the model itself is the user's word on it, larger
|
||||
// or not. The catalog's entries and the Providers page's model lists both
|
||||
// read it, so the page shows the window agents are told.
|
||||
func (p Provider) WindowOf(m catalog.Model) int {
|
||||
if n := p.ContextOf(m.ID); n > 0 {
|
||||
if n := p.Contexts[m.ID]; n > 0 {
|
||||
return n
|
||||
}
|
||||
return ListedWindow(m)
|
||||
listed := ListedWindow(m)
|
||||
if n := p.Contexts["*"]; n > 0 {
|
||||
if most := max(listed, m.MaxContext); most > 0 && most < n {
|
||||
return most
|
||||
}
|
||||
return n
|
||||
}
|
||||
return listed
|
||||
}
|
||||
|
||||
// ContextOf is the context the user set for the provider's model: its own,
|
||||
|
||||
Reference in New Issue
Block a user