Files
magpie/internal/agent/opencode_limit_input_test.go
yetone 3e66d25d84 agent: OpenCode's limit keeps models.dev's context and input apart
Nepsama1337 on X: magpie 0.1.4414 wrote gpt-6.2-sol into OpenCode's
config as limit {context: 922000, output: 128000}. models.dev gives
{context: 1050000, input: 922000, output: 128000}. magpie's
catalog.Model.Context is the prompt window, so it prefers models.dev's
input, and the opencode shape wrote that number as context and
dropped input.

What OpenCode reads, checked in anomalyco/opencode on dev:
packages/schema/src/config/provider.ts and
packages/core/src/v1/config/provider.ts take limit {context, input,
output}. session/compaction.ts uses min(input, context) when both are
there. OpenCode 1's models.ts (8df09abb1) has input too, and its
compaction uses limit.input || context - output.

catalog keeps the whole window that models.dev pairs with each input
cap (WholeOf, voted the same way as the windows and looked up through
the same renames). The opencode shape now writes {context: whole,
input: prompt window, output} when the catalog gives a wider whole
window, and {context, output} as before otherwise. A user's own
context for a model (Contexts) has no pair, so it stays a plain
context. This shape serves OpenCode 1 and 2, MiMo Code, DevEco, Air
and OpenChamber. OpenCode 2's converter copies limit whole, so input
reaches it too.

Agents with one window field (Crush's context_window, droid, Cline and
the rest) get the prompt window, which is right for them. ZCode and
MiniMax Code also write {context, output}, but their schemas can't be
checked: #7060.

Run:
- TestOpenCodeLimitKeepsContextAndInput is red on the old agents.go
  (relay/gpt-6.2-sol: limit {"context":922000,"output":128000}) and
  green with the fix. It covers the paired model, a model with no
  input cap, a user's own context, and openCodeNative.
- go test -run 'TestOpenCode' ./internal/agent on mote: ok.
- Real OpenCode 1.19.5 on mote, sandbox HOME: `opencode models magpie
  --verbose` lists gpt-6.2-sol with limit {context 1050000, input
  922000, output 128000}.
- Full suite, go test -timeout 45m ./... on Linux (magpie-desktop, at
  48fffb60c plus this change; mote and mica were over their cores):
  every package ok except internal/usage's
  TestRequestStateKeptAcrossACatalogFetchedAgain, red on the old code
  too (#6572). The three GUI-linked packages need -tags gtk3 there:
  . and internal/cli ok; internal/gui's TestClipboardReadBack
  segfaults under WSLg (#7074), the rest ok.
- go vet ./... on Linux (-tags gtk3) and on Windows: ok. gofmt -l:
  clean.

After rebasing onto 1318864d1: internal/agent and internal/catalog ok on Linux.

Not tried: a real OpenCode 2 run (its schema was read from source),
ZCode and MiniMax Code (#7060), and the macOS full suite.
2026-11-18 16:51:14 +08:00

69 lines
2.7 KiB
Go

package agent
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// Nepsama1337 on X: OpenCode was told gpt-6.2-sol's input limit (922000)
// as its context, and no input. models.dev gives the model
// {context:1050000, input:922000, output:128000}, and OpenCode's limit
// takes all three, so all three are written, in OpenCode 1's config and in
// OpenCode 2's. A model models.dev gives one window keeps its context
// alone, and so does a window the user typed, which is no input limit of
// models.dev's.
func TestOpenCodeLimitKeepsContextAndInput(t *testing.T) {
syncHome(t)
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
os.WriteFile(catalog.CachePath(), []byte(`{
"openai":{"id":"openai","name":"OpenAI","npm":"@ai-sdk/openai","models":{
"gpt-6.2-sol":{"id":"gpt-6.2-sol","name":"GPT-6.2 Sol","reasoning":true,"tool_call":true,"release_date":"2026-11-10","modalities":{"input":["text","image"],"output":["text"]},"limit":{"context":1050000,"input":922000,"output":128000},"cost":{"input":2,"output":10}}}},
"zai":{"id":"zai","name":"Z.AI","models":{
"glm-5.3-flash":{"id":"glm-5.3-flash","name":"GLM-5.3-Flash","tool_call":true,"release_date":"2026-08-26","modalities":{"input":["text"],"output":["text"]},"limit":{"context":1000000,"output":131072}}}}}`), 0o644)
catalog.Reset()
t.Cleanup(catalog.Reset)
for _, p := range []provider.Provider{
{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-6.2-sol", "glm-5.3-flash"}},
{ID: "mine", Name: "Mine", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-6.2-sol"}, Contexts: map[string]int{"gpt-6.2-sol": 400000}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
want := map[string]string{
"relay/gpt-6.2-sol": `{"context":1050000,"input":922000,"output":128000}`,
"relay/glm-5.3-flash": `{"context":1000000,"output":131072}`,
"mine/gpt-6.2-sol": `{"context":400000,"output":128000}`,
}
limits := func(t *testing.T, raw []byte, at string) {
t.Helper()
var got map[string]json.RawMessage
var top map[string]any
json.Unmarshal(raw, &top)
ms, _ := json.Marshal(top[at])
if err := json.Unmarshal(ms, &got); err != nil {
t.Fatalf("%v: %s", err, raw)
}
for id, w := range want {
var m struct {
Limit json.RawMessage `json:"limit"`
}
json.Unmarshal(got[id], &m)
if string(m.Limit) != w {
t.Errorf("%s: limit %s, want %s", id, m.Limit, w)
}
}
}
block := magpieProviderJSON("opencode")
b, _ := json.Marshal(block)
limits(t, b, "models")
// OpenCode 2's words for the same block keep the limit as it is
b2, _ := json.Marshal(openCodeNative(block))
limits(t, b2, "models")
}