Files
magpie/internal/gateway/model_output_window_test.go
T
yetone 7f62dbf3ed gateway, agents, gui: a model's reply limit is published within the window beside it, on /v1/models, Cursor's and Muse's lists, the Gateway page and magpie model output (#1438, yhong91)
#1438 (yhong91): GET /v1/models listed grok/grok-4.7 with context_window
256000 (the Grok backend's own) and max_output_tokens 500000 (models.dev's
x-ai entry, which catalog.Decorate fills in when the backend gives no
output). A client that reads both sizes its replies past what the window
holds. The agents' files already kept output within the window (maxTokens,
#338/#347), but the gateway's lists, the GUI and the CLI did not.

What changed:
- provider.OutputWithin(window, output) and Entry.PublishedOutput(): the
  output is kept within the window; an unknown window or output (0) is left
  as it is.
- Every place that publishes the pair uses it: modelObject (/v1/models),
  cursorLocalModel (Cursor Private Inference), Muse Code's list, the
  Gateway page's models and groups (providerInfo, providersState), the
  agents' maxTokens, and `magpie model output` (which also says
  "500k, kept within its 200k window" when it lowers one).
- Entry.Output stays the model's own limit. The request-path cap
  (outputLimit/withMaxOutput) and the user's Max output setting are
  unchanged, so no request is lowered further than before. This is why the
  clamp is not in replyLimit/entryFor as the issue suggests, and why the
  commenter's patch (modelObject only) was not taken as is: it was right
  but missed Cursor, Muse, the GUI and the CLI.
- docs/subsystems/gateway-routing.md: the rule and its routes.

Tests (each fails without the change):
- gateway TestModelListsKeepOutputWithinTheWindow: models.dev 500000 over a
  backend 256000 window, plus output within the window, unknown window and
  unknown output, on /v1/models, Cursor's capabilities and Muse's limit; a
  group request with max_tokens 300000 still reaches upstream as 300000.
- provider TestOutputWithin, gui TestProviderModelOutputWithinTheWindow,
  TestModelOutputCmdWithinTheWindow.
Breaking it by hand: OutputWithin returning output unclamped turns all of
them red, plus agent TestMaxTokensWithinContextWindow; gateway sites back
on e.Output turn the gateway test red on all three lists; moving the clamp
into Entry.Output turns the fixture and the group-request guard red.

Real data: an offline dump of the owner's catalog (copies of
~/.config/magpie and its caches, 203 entries) has 3 models with output
over their window, grok/grok-4.5, 4.6 and 4.7 (256000 vs 500000), now
published as 256000; 0 are not Grok. The live gateway on 3425 was not
running, so /v1/models itself was not read, and no real Grok request was
made.

Run: gofmt -l; go vet -tags nogui ./... on darwin, linux and windows;
go test -tags nogui ./... -count=1: all green except
TestLoopEndsWorkBuddysReply/plugin/anthropic ("let go only after 3207
lines", bound 2000) at load average ~35 with other suites running; it
passes 5 of 5 alone and does not touch the output limit or request path.
2026-10-10 01:30:29 +08:00

126 lines
4.7 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// #1438 (yhong91): /v1/models published grok/grok-4.7 with a 256000
// window, the Grok backend's own, beside max_output_tokens 500000, which
// models.dev gives x-ai's grok-4.7. Every model list the gateway serves now
// keeps the reply limit within the window beside it, as the agents' files
// have since #338; an unknown window or output keeps its shape, and a
// request is still lowered only to the model's own limit.
func TestModelListsKeepOutputWithinTheWindow(t *testing.T) {
f := &fake{ctype: "application/json", reply: `{"id":"x","object":"chat.completion","choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}]}`}
up := setup(t, provider.Chat, f)
// models.dev's entry, as the reporter's cache has it
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
if err := os.WriteFile(catalog.CachePath(), []byte(`{"xai":{"id":"xai","models":{"grok-4.7":{"id":"grok-4.7","limit":{"context":500000,"output":500000}}}}}`), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
if err := provider.Save(provider.Provider{ID: "gx", Name: "GX", Key: "k", Chat: up.URL + "/v1",
Models: []string{"grok-4.7", "own-within", "own-nowindow", "own-nooutput"}}); err != nil {
t.Fatal(err)
}
// the backend's own list: grok-4.7's window, as cli-chat-proxy.grok.com
// gives it, with no output of its own
if err := catalog.SaveLive("gx", up.URL+"/v1", []catalog.Model{
{ID: "grok-4.7", Context: 256000},
{ID: "own-within", Context: 128000, Output: 64000},
{ID: "own-nowindow", Output: 500000},
{ID: "own-nooutput", Context: 256000},
}); err != nil {
t.Fatal(err)
}
// the fixture is the report's: models.dev's output over the backend's
// window in the catalog itself
e, ok := provider.ServedEntryOf("gx/grok-4.7")
if !ok || e.Context != 256000 || e.Output != 500000 {
t.Fatalf("fixture: window %d, output %d; want models.dev's 500000 over the backend's 256000", e.Context, e.Output)
}
list := func(path, key, ua string) map[string]map[string]any {
t.Helper()
req := httptest.NewRequest("GET", path, nil)
if key != "" {
req.Header.Set("Authorization", "Bearer "+key)
}
if ua != "" {
req.Header.Set("User-Agent", ua)
}
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, req)
var out struct {
Data []map[string]any `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
t.Fatalf("%s: %s", path, rec.Body)
}
got := map[string]map[string]any{}
for _, d := range out.Data {
got[d["id"].(string)] = d
}
return got
}
num := func(v any) any {
if n, ok := v.(float64); ok {
return int(n)
}
return v
}
// /v1/models, as every client reads it
got := list("/v1/models", "", "")
for id, want := range map[string][2]any{
"gx/grok-4.7": {256000, 256000},
"gx/own-within": {128000, 64000},
"gx/own-nowindow": {nil, 500000},
"gx/own-nooutput": {256000, nil},
} {
m := got[id]
if m == nil {
t.Fatalf("%s not listed: %v", id, got)
}
if w, o := num(m["context_window"]), num(m["max_output_tokens"]); w != want[0] || o != want[1] {
t.Errorf("/v1/models %s: context_window %v, max_output_tokens %v; want %v, %v", id, w, o, want[0], want[1])
}
}
// Cursor Private Inference's capabilities
c, _ := list("/v1/models", TokenFor("cursor-local"), "")["gx/grok-4.7"]["capabilities"].(map[string]any)
if num(c["context_length"]) != 256000 || num(c["max_output_tokens"]) != 256000 {
t.Errorf("Cursor's capabilities: %v", c)
}
// Muse Code's limit
muse := list("/muse-code/models", "", "muse-build/1.4.2 (non-interactive; macos-aarch64; build 0)")
if meta, _ := muse["gx/grok-4.7"]["metadata"].(map[string]any)["muse-code"].(map[string]any); meta == nil {
t.Errorf("Muse's list: %v", muse)
} else if l, _ := meta["limit"].(map[string]any); num(l["context"]) != 256000 || num(l["output"]) != 256000 {
t.Errorf("Muse's limit: %v", meta["limit"])
}
// a group's member is asked for no more than the model's own limit, as
// before (withMaxOutput): what is published is the pair, not a new cap
// on what is asked
if err := provider.SaveGroup(provider.Group{ID: "gg", Name: "GG", Members: []string{"gx/grok-4.7"}}); err != nil {
t.Fatal(err)
}
if code, body := post(t, "/v1/chat/completions", `{"model":"`+provider.GroupPrefix+`gg","max_tokens":300000,"messages":[{"role":"user","content":"hi"}]}`); code != 200 {
t.Fatalf("%d %s", code, body)
}
var asked map[string]any
if err := json.Unmarshal(f.got, &asked); err != nil || num(asked["max_tokens"]) != 300000 {
t.Errorf("asked upstream: %s", f.got)
}
}