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,