From 7aad6a50ea981fcfef0c7182c193cd6e6d7fb61d Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 23 Oct 2026 01:45:50 +0800 Subject: [PATCH] provider: a window set for all of a custom provider's models wins over another vendor's guess MisakaNatsuxun (Discord, 10-23) set a custom OpenAI-compatible provider's Advanced > Context window to 1M. Routing still showed 200K for deepseek-v4.1-flash-free, and dsh was told contextWindow 200000. Cause: WindowOf capped the provider-wide "*" window at ListedWindow(m) (#2112, 273ab9c23). ListedWindow falls back to catalog.ContextOf, a guess by model id from any vendor on models.dev. For a relay with no catalog of its own, opencode's deepseek-v4.1-flash-free (200000) or Anthropic's claude-sonnet-4-5 (200000) capped the user's 1M. WindowOf feeds Served, so /v1/models, the Routing picker, the editor's model chips and every agent config (dsh's contextWindow too) got 200K. Fix: "*" is capped only by the provider's own word: catalog.Window (m.Context, set from its live list, its own models.dev catalog or plan models) and m.MaxContext. #2112's OpenCode Go case still caps, since that catalog sets m.Context. A per-model entry already won and still does. Siblings: Max output (replyLimit) already took the user's value over m.Output and the catalog, on old and new (checked "*"=128000). Anthropic- and Responses-type custom providers read the same WindowOf and are fixed with it. Left alone: with no window set, OutputWithin still caps a user's Max output at the guessed window. Tests, each red on the old code (ListedWindow put back by hand): - provider TestProviderWindowSetOverAnotherVendorsGuess: "map[*:1000000]: claude-sonnet-4-5 told 200000, want 1000000" - agent TestDshToldTheWindowSetForACustomProvider (opencode's deepseek-v4.1-flash-free fixture, dshRouteConfig and dshProviderLines): told contextWindow 200000 Sandbox HOME, fake OpenAI /models on 127.0.0.1:3481, relay "*"=1M: - old binary: /v1/models relay/deepseek-v4.1-flash-free 200000, relay/claude-sonnet-4-5 200000; connecting dsh wrote contextWindow 200000; no 1M on the Sonnet chip in the editor or the Routing picker. - new binary (this tree, -tags dev): 1000000 for both, dsh config written fresh says contextWindow 1000000 / maxTokens 32000 (ca4a1cde0's cap at the model's output); Routing picker "DeepSeek V4.1 Flash Free 1M"; relay-a (anthropic), relay-r (responses) 1M; relay-lo "*"=128K stays 128000. Not tried: a real relay account, and dsh making a real request. Run at 62cf0c76d (this tree on origin/main 032d88c13): gofmt -l clean; go vet darwin, linux -tags nogui, windows clean; go test -tags nogui -timeout 45m ./... : 69 ok, 19 no test files, no FAIL (agent 336s, gateway 474s, gui 123s, provider 187s). GUI files in chromium and webkit: context-picks 10/10, provider-advanced-fold 25/25, provider-output-picks 2/2, compact-at 4/4, gui-ja 3/3, gui-zh-tw 3/3, gui-de 3/3, i18n-keys 3/3. --- internal/agent/dsh_test.go | 51 +++++++++++++++++++++++ internal/provider/context_test.go | 67 +++++++++++++++++++++++++++++++ internal/provider/models.go | 22 +++++----- 3 files changed, 131 insertions(+), 9 deletions(-) diff --git a/internal/agent/dsh_test.go b/internal/agent/dsh_test.go index ee63e397b..2ad2d8355 100644 --- a/internal/agent/dsh_test.go +++ b/internal/agent/dsh_test.go @@ -3,6 +3,7 @@ package agent import ( "os" "path/filepath" + "strconv" "strings" "testing" @@ -322,3 +323,53 @@ func TestDshSetEffortWritesALegacyEntryWithTheCatalog(t *testing.T) { } } } + +// MisakaNatsuxun on Discord (10-23): a custom OpenAI-compatible provider +// serving deepseek-v4.1-flash-free, set to 1M for all of its models in the +// editor's Advanced → Context window, still had dsh told 200K, the window +// models.dev gives that id under opencode alone. The relay's list says no +// window, so the user's 1M is what dsh is told, and maxTokens follows from +// it (dshMaxTokens). The models.dev bytes are opencode's own entry. +func TestDshToldTheWindowSetForACustomProvider(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("DSH_HOME", "") + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache")) + if err := os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(catalog.CachePath(), []byte(`{"opencode":{"models":{ + "deepseek-v4.1-flash-free":{"id":"deepseek-v4.1-flash-free","name":"DeepSeek V4.1 Flash Free","reasoning":true,"tool_call":true, + "modalities":{"input":["text"],"output":["text"]},"limit":{"context":200000,"output":32000}}}}}`), 0o644); err != nil { + t.Fatal(err) + } + catalog.Reset() + t.Cleanup(catalog.Reset) + for _, c := range []struct { + cx map[string]int + window, limit int + }{ + {nil, 200000, 32000}, + {map[string]int{"*": 1000000}, 1000000, 32000}, + {map[string]int{"deepseek-v4.1-flash-free": 1000000}, 1000000, 32000}, + {map[string]int{"*": 128000}, 128000, 32000}, + } { + if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Chat: "https://relay.test/v1", Key: "k", + Models: []string{"deepseek-v4.1-flash-free"}, Contexts: c.cx}); err != nil { + t.Fatal(err) + } + r := dshRouteConfig(magpieModels("dsh"), "", gateway.URL()) + if len(r.Models) != 1 || r.Models[0].ID != "relay/deepseek-v4.1-flash-free" { + t.Fatalf("%v: models %+v", c.cx, r.Models) + } + if m := r.Models[0]; m.ContextWindow != c.window || m.MaxTokens != c.limit { + t.Errorf("%v: dsh told contextWindow %d, maxTokens %d; want %d, %d", c.cx, m.ContextWindow, m.MaxTokens, c.window, c.limit) + } + lines := strings.Join(dshProviderLines("high", magpieModels("dsh"), gateway.URL()), "\n") + "\n" + if !strings.Contains(lines, "contextWindow: "+strconv.Itoa(c.window)+"\n") { + t.Errorf("%v: llm-deepseek entry, want contextWindow: %d:\n%s", c.cx, c.window, lines) + } + } +} diff --git a/internal/provider/context_test.go b/internal/provider/context_test.go index 2a7423ddf..ec1633278 100644 --- a/internal/provider/context_test.go +++ b/internal/provider/context_test.go @@ -186,3 +186,70 @@ func TestProviderWindowNoLargerThanTheModels(t *testing.T) { t.Errorf("codex past its max: %d", got) } } + +// MisakaNatsuxun on Discord (10-23): a custom OpenAI-compatible provider set +// to 1M for all of its models (Advanced → Context window) still told its +// claude-sonnet-4-5 200K, the window models.dev gives that id under other +// vendors. A relay's list says nothing of its window, and a guess from +// another vendor's entry is magpie's default, not the provider's own word, +// so the user's 1M is told. The models.dev bytes are anthropic's own entry +// for the model, as it has it today. A window set lower than the guess +// still holds, and a vendor's own list still caps "*" (#2112, above). +func TestProviderWindowSetOverAnotherVendorsGuess(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(`{"anthropic":{"models":{ + "claude-sonnet-4-5":{"id":"claude-sonnet-4-5","name":"Claude Sonnet 4.5 (latest)","attachment":true,"reasoning":true,"tool_call":true, + "modalities":{"input":["text","image","pdf"],"output":["text"]},"limit":{"context":200000,"output":64000}}}}}`), 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 + } + relay := func(cx map[string]int) { + t.Helper() + if err := Save(Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "https://relay.test/v1", + Models: []string{"claude-sonnet-4-5", "my-own-model-x"}, Contexts: cx}); err != nil { + t.Fatal(err) + } + } + relay(nil) + if got := of("relay/claude-sonnet-4-5"); got != 200000 { + t.Fatalf("nothing set: told %d, want models.dev's 200000", got) + } + for _, c := range []struct { + cx map[string]int + sonnet, x int + }{ + {map[string]int{"*": 1000000}, 1000000, 1000000}, + {map[string]int{"*": 128000}, 128000, 128000}, + {map[string]int{"*": 128000, "claude-sonnet-4-5": 1000000}, 1000000, 128000}, + {map[string]int{"claude-sonnet-4-5": 1000000}, 1000000, 0}, + } { + relay(c.cx) + if got := of("relay/claude-sonnet-4-5"); got != c.sonnet { + t.Errorf("%v: claude-sonnet-4-5 told %d, want %d", c.cx, got, c.sonnet) + } + if got := of("relay/my-own-model-x"); got != c.x { + t.Errorf("%v: my-own-model-x told %d, want %d", c.cx, got, c.x) + } + } + // a window the provider's own list gives is its word, and "*" stays + // within it, as for OpenCode Go's catalog (#2112) + p := Provider{ID: "relay", Contexts: map[string]int{"*": 1000000}} + if got := p.WindowOf(catalog.Model{ID: "claude-sonnet-4-5", Context: 200000}); got != 200000 { + t.Errorf("listed by the vendor: %d", got) + } +} diff --git a/internal/provider/models.go b/internal/provider/models.go index ff394a81c..42b77504d 100644 --- a/internal/provider/models.go +++ b/internal/provider/models.go @@ -2681,24 +2681,28 @@ 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 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. +// ("*") is no larger than a model's own, where the provider says it (the +// window its list or its models.dev catalog gives, 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). The window models.dev +// gives the id under other vendors (catalog.ContextOf) is not the +// provider's word but magpie's guess, and never caps what the user set: a +// custom OpenAI-compatible relay set to 1M told its claude-sonnet-4-5 +// 200K (MisakaNatsuxun on Discord, 10-23). 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.Contexts[m.ID]; n > 0 { return n } - listed := ListedWindow(m) if n := p.Contexts["*"]; n > 0 { - if most := max(listed, m.MaxContext); most > 0 && most < n { + if most := max(catalog.Window(m.Context), m.MaxContext); most > 0 && most < n { return most } return n } - return listed + return ListedWindow(m) } // ContextOf is the context the user set for the provider's model: its own,