From 12cba74ab3fc2d1d1f70e6c0044440c74ad377c5 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 5 Oct 2026 11:05:49 +0800 Subject: [PATCH] =?UTF-8?q?agent:=20Zed's=20max=5Ftokens=20is=20the=20wind?= =?UTF-8?q?ow=20a=20prompt=20and=20its=20reply=20share,=20so=20a=20prompt?= =?UTF-8?q?=20keeps=20the=20room=20magpie's=20catalog=20gives=20it=20(#850?= =?UTF-8?q?)=20=E2=80=94=20Zed's=20openai=5Fcompatible=20max=5Ftokens=20is?= =?UTF-8?q?=20the=20whole=20window:=20it=20keeps=20max=5Foutput=5Ftokens?= =?UTF-8?q?=20of=20it=20for=20the=20reply=20and=20lets=20the=20prompt=20fi?= =?UTF-8?q?ll=20only=20the=20rest=20before=20it=20compacts=20(its=20thread?= =?UTF-8?q?'s=20input=5Ftoken=5Fcapacity=20=3D=20max=5Ftokens=20-=20max=5F?= =?UTF-8?q?output=5Ftokens).=20magpie=20wrote=20its=20catalog's=20Context?= =?UTF-8?q?=20there,=20which=20is=20what=20a=20prompt=20may=20hold,=20so?= =?UTF-8?q?=20Zed=20took=20the=20reply=20out=20of=20it:=20glm-4.6=20(20480?= =?UTF-8?q?0=20/=20131072)=20left=20a=20prompt=2073728,=20deepseek-chat=20?= =?UTF-8?q?(128000=20/=20128000)=20none=20at=20all,=20gpt-5=20(input=20272?= =?UTF-8?q?000=20/=20128000)=20144000=20of=20its=20400000=20window.=20max?= =?UTF-8?q?=5Ftokens=20is=20now=20Context=20+=20max=5Foutput=5Ftokens:=203?= =?UTF-8?q?35872,=20256000,=20400000=20(OpenAI's=20own),=20and=20a=20model?= =?UTF-8?q?=20with=20no=20output=20limit=20still=20gets=20its=20Context=20?= =?UTF-8?q?alone.=20In=20a=20sandbox=20HOME=20with=20the=20real=20models.d?= =?UTF-8?q?ev=20catalog,=20setting=20Zed=20to=20magpie=20wrote=20glm-4.6?= =?UTF-8?q?=20335872/131072,=20gpt-5=20400000/128000,=20deepseek-chat=2025?= =?UTF-8?q?6000/128000,=20claude-sonnet-4-5=20264000/64000,=20kimi-k2=2022?= =?UTF-8?q?9376/98304.=20TestZedWindowHoldsPromptAndReply=20(gpt-5=2040000?= =?UTF-8?q?0=20with=20a=20272000=20prompt;=20a=20model=20without=20an=20ou?= =?UTF-8?q?tput=20limit)=20and=20TestZedRestoresProviderAndModelLimits=20f?= =?UTF-8?q?ail=20on=20the=20old=20code.=20go=20vet,=20builds=20(darwin,=20?= =?UTF-8?q?GOOS=3Dlinux=20nogui,=20GOOS=3Dwindows)=20and=20go=20test=20-ta?= =?UTF-8?q?gs=20nogui=20-count=3D1=20./...=20under=20a=20temp=20HOME=20pas?= =?UTF-8?q?s=20(TestProvidersAnswerWhileListsComeIn,=20TestUserPath=20and?= =?UTF-8?q?=20TestClaudeSignedOut=20timed=20out=20under=20load=20and=20pas?= =?UTF-8?q?s=20on=20their=20own).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/agent/zed.go | 10 ++++++- internal/agent/zed_test.go | 53 +++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/internal/agent/zed.go b/internal/agent/zed.go index af4f926d4..9d0b85f6f 100644 --- a/internal/agent/zed.go +++ b/internal/agent/zed.go @@ -125,6 +125,12 @@ func zedProviderJSON() map[string]any { if context == 0 { context = 128000 // Zed requires a context window for every custom model. } + // Zed's max_tokens is the window a prompt and its reply share: it + // keeps max_output_tokens of it for the reply and lets the prompt + // fill the rest before it compacts. Context is what a prompt may + // hold, so the window is it and the reply together (#850); written + // as Context alone, a model whose reply may be as long as its + // prompt (glm-4.6) left Zed no room for a prompt at all. entry := map[string]any{ "name": m.ID, "display_name": m.Name, "max_tokens": context, "capabilities": map[string]any{ @@ -134,7 +140,9 @@ func zedProviderJSON() map[string]any { }, } if output := maxTokens(m); output > 0 { - entry["max_output_tokens"] = min(output, context) + output = min(output, context) + entry["max_output_tokens"] = output + entry["max_tokens"] = context + output } models = append(models, entry) } diff --git a/internal/agent/zed_test.go b/internal/agent/zed_test.go index ac102509a..a06d4834c 100644 --- a/internal/agent/zed_test.go +++ b/internal/agent/zed_test.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "runtime" + "strconv" "strings" "testing" @@ -139,6 +140,54 @@ func TestZedFreshAndOwn(t *testing.T) { } } +// Zed's max_tokens is the window a prompt and its reply share, and it lets +// a prompt fill max_tokens - max_output_tokens before it compacts (its +// thread's input_token_capacity). That room is the prompt magpie's catalog +// says the model takes (#850): GPT-5's input 272000 with its 128000 reply, +// a 400000 window as OpenAI gives it, and a model with no output limit +// gets its context alone. +func TestZedWindowHoldsPromptAndReply(t *testing.T) { + home := syncHome(t) + credential := zedCredential + t.Cleanup(func() { zedCredential = credential; catalog.Reset() }) + zedCredential = func(string) error { return nil } + writeFile(t, catalog.CachePath(), `{"openai":{"models":{ + "gpt-5":{"id":"gpt-5","name":"GPT-5","limit":{"context":400000,"input":272000,"output":128000}}, + "plain":{"id":"plain","name":"Plain","limit":{"context":64000}}}}}`) + catalog.Reset() + if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-5", "plain"}}); err != nil { + t.Fatal(err) + } + a := zedAt(filepath.Join(home, "zed")) + if err := a.Field("model").Set("magpie/relay/gpt-5"); err != nil { + t.Fatal(err) + } + var p struct { + Models []struct { + Name string `json:"name"` + Max int `json:"max_tokens"` + Output *int `json:"max_output_tokens"` + } `json:"available_models"` + } + raw, _ := edit.GetJSON(a.Path, zedProvider) + if err := json.Unmarshal([]byte(raw), &p); err != nil { + t.Fatal(err) + } + got := map[string]string{} + for _, m := range p.Models { + s := strconv.Itoa(m.Max) + if m.Output != nil { + s += " " + strconv.Itoa(*m.Output) + " prompt " + strconv.Itoa(m.Max-*m.Output) + } + got[m.Name] = s + } + for name, want := range map[string]string{"relay/gpt-5": "400000 128000 prompt 272000", "relay/plain": "64000"} { + if got[name] != want { + t.Errorf("%s: max_tokens, max_output_tokens = %q, want %q (all %v)", name, got[name], want, got) + } + } +} + func TestZedRestoresProviderAndModelLimits(t *testing.T) { home := syncHome(t) writeFile(t, catalog.CachePath(), `{"zai":{"models":{"glm-4.6":{"id":"glm-4.6","name":"GLM","modalities":{"input":["text","image"]},"limit":{"context":204800,"output":300000}}}}}`) @@ -152,7 +201,9 @@ func TestZedRestoresProviderAndModelLimits(t *testing.T) { if err := a.Field("model").Set("magpie/relay/glm-4.6"); err != nil { t.Fatal(err) } - for key, want := range map[string]string{"max_tokens": "204800", "max_output_tokens": "204800", "capabilities.images": "true"} { + // Zed keeps max_output_tokens of max_tokens for the reply: the prompt's + // 204800 stays (#850), not 204800 - 204800 = 0 + for key, want := range map[string]string{"max_tokens": "409600", "max_output_tokens": "204800", "capabilities.images": "true"} { if got, _ := edit.GetJSON(a.Path, zedProvider+".available_models.0."+key); got != want { t.Errorf("%s = %q, want %q", key, got, want) }