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.
This commit is contained in:
yetone
2026-11-18 16:51:14 +08:00
parent 1318864d1b
commit 3e66d25d84
3 changed files with 148 additions and 1 deletions
+21 -1
View File
@@ -642,6 +642,26 @@ func magpieProviderJSONFor(shape, catalog string) any {
}
// magpieProviderJSONAt is magpieProviderJSONFor for an agent that reaches
// openCodeLimit is a model's limit as OpenCode is told it. OpenCode's
// limit has three sizes (packages/schema/src/config/provider.ts, and
// OpenCode 1's models.dev schema): context, the window prompt and
// reply share; input, the most a prompt may hold, which it compacts by
// (core's session/compaction.ts inputWindow, the least of the two;
// OpenCode 1's isOverflow takes it over context less output); and
// output. m.Context is the prompt's, models.dev's input limit where it
// gives one, so a model with both is told both as models.dev gives them:
// gpt-6.2-sol's {context:1050000, input:922000, output:128000}, where
// OpenCode was told the input limit as its context and no input
// (Nepsama1337 on X). A window with no larger context beside it in
// models.dev (one the user typed, one a vendor's list gives) is the
// context alone, as before.
func openCodeLimit(m catalog.Model) map[string]any {
if whole := catalog.WholeOf(m.ID, m.Context); whole > m.Context {
return map[string]any{"context": whole, "input": m.Context, "output": maxTokens(m)}
}
return map[string]any{"context": m.Context, "output": maxTokens(m)}
}
// openCodeKinds are the input kinds OpenCode's config takes for a model
// (its modalities enum, models.dev's words).
var openCodeKinds = []string{"text", "image", "audio", "video", "pdf"}
@@ -690,7 +710,7 @@ func magpieProviderJSONAt(shape, catalog, gw string) any {
// group's context (magpie group set … context=) never reaches
// it; an output of 0 is OpenCode's own default
if m.Context > 0 {
e["limit"] = map[string]any{"context": m.Context, "output": maxTokens(m)}
e["limit"] = openCodeLimit(m)
}
if openCodeReasons(m) {
e["reasoning"] = true
@@ -0,0 +1,68 @@
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")
}
+59
View File
@@ -602,6 +602,10 @@ var (
windows map[string]int
// outputs are the most tokens their replies may hold, likewise
outputs map[string]int
// wholes are the context windows, prompt and reply together, that
// models.dev gives beside an input limit below them, by bare id and
// that input limit: gpt-6.2-sol's 1050000 beside its 922000 (WholeOf)
wholes map[string]map[int]int
// efforts are the models' reasoning levels, by bare id, as most of the
// providers that give any for them give them
efforts map[string][]string
@@ -685,6 +689,7 @@ func loadGen() (map[string]mdProvider, int) {
votes, reasons := map[string]int{}, map[string]int{}
named := map[string]map[string]int{}
sizes, outs := map[string]map[int]int{}, map[string]map[int]int{}
whole := map[string]map[int]map[int]int{}
levels := map[string]map[string]int{}
kinds := map[string]map[string]int{}
voted := map[string]bool{}
@@ -749,6 +754,15 @@ func loadGen() (map[string]mdProvider, int) {
}
sizes[who][w]++
}
if in, ctx := Window(x.Limit.Input), Window(x.Limit.Context); in > 0 && ctx > in {
if whole[who] == nil {
whole[who] = map[int]map[int]int{}
}
if whole[who][in] == nil {
whole[who][in] = map[int]int{}
}
whole[who][in][ctx]++
}
if o := x.Limit.Output; o > 0 {
if outs[who] == nil {
outs[who] = map[int]int{}
@@ -811,6 +825,13 @@ func loadGen() (map[string]mdProvider, int) {
for id, by := range sizes {
windows[id] = mostGiven(by)
}
wholes = map[string]map[int]int{}
for id, ins := range whole {
wholes[id] = map[int]int{}
for in, by := range ins {
wholes[id][in] = mostGiven(by)
}
}
outputs = map[string]int{}
for id, by := range outs {
outputs[id] = mostGiven(by)
@@ -868,6 +889,7 @@ func Reset() {
gen++
resets.Add(1)
mdev, images, inputs, windows, outputs, efforts, thinks, names, listed, canonical, labs, normal = nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil
wholes = nil
loadMu.Unlock()
if Forget != nil {
Forget()
@@ -1593,6 +1615,43 @@ func kindOrder(ks []string) []string {
// vendor's own list gives is read before this, by the caller, and stays.
func ContextOf(id string) int { return contextAs(id, true) }
// WholeOf is the context window, prompt and reply together, that
// models.dev gives model id beside an input limit of window: 1050000 for
// gpt-6.2-sol told 922000, the input limit Context holds (window). 0 when
// none of its entries pairs that input limit with a larger context: a
// window the user typed, or one a vendor's own list gives, is a window of
// its own, and an agent that takes both (OpenCode's limit.context and
// limit.input) is told it alone, as before. The id is looked up as
// ContextOf looks it up: a relay's spelling or a snapshot of it.
func WholeOf(id string, window int) int {
if window <= 0 {
return 0
}
load()
return wholeIn(bareID(id), window, true)
}
func wholeIn(b string, window int, spellings bool) int {
if w, ok := found(b, wholes); ok {
return w[window]
}
if !spellings {
return 0
}
if r, _, ok := renamed(b); ok {
if w := wholeIn(r, window, false); w > 0 {
return w
}
}
if n, ok := normalFor(b); ok {
return wholeIn(n, window, false)
}
if t, ok := untagged(b); ok {
return wholeIn(t, window, true)
}
return 0
}
// contextAs is ContextOf, with another spelling (normalFor), the model
// the user said it is the same as (UserSame) and a tag in brackets taken
// off (untagged) only when spellings is true.