From a8391197518690b6c4db48e2d2074cef927f905b Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 4 Oct 2026 11:58:20 +0800 Subject: [PATCH] =?UTF-8?q?qoder=200.2.6:=20the=20window=20a=20request=20i?= =?UTF-8?q?s=20sent=20holds=20the=20reply's=20max=5Ftokens=20too=20(yetone?= =?UTF-8?q?/magpie#700=20follow-up,=20kyzhouxu:=20a=20group's=20DeepSeek?= =?UTF-8?q?=20V4.1=20Flash=20refused=20near=20207K=20until=20Qoder's=20Dee?= =?UTF-8?q?pSeek-Flash=20was=20taken=20out=20of=20it)=20=E2=80=94=20since?= =?UTF-8?q?=200.2.5=20windowFor=20sends=20the=20model's=20default=20window?= =?UTF-8?q?=20(200K=20on=20the=20models=20context=5Fconfig=20lists=20200K/?= =?UTF-8?q?400K/1M=20for)=20while=20the=20request's=20estimate,=20a=20toke?= =?UTF-8?q?n=20per=203=20bytes=20of=20its=20messages=20and=20tools,=20fits?= =?UTF-8?q?=20it,=20and=20asked=20for=20nothing=20of=20the=20reply:=20a=20?= =?UTF-8?q?180K=20conversation=20with=20the=2032000=20max=5Ftokens=20Qoder?= =?UTF-8?q?=20is=20sent=20went=20out=20with=20context=5Flength=20200000,?= =?UTF-8?q?=20which=20a=20prompt=20plus=20reply=20past=20200K=20overflows,?= =?UTF-8?q?=20while=20magpie=20and=20the=20agents=20behind=20it=20are=20to?= =?UTF-8?q?ld=20the=20model=20takes=201M.=20The=20estimate=20now=20adds=20?= =?UTF-8?q?the=20reply's=20max=5Ftokens=20(what=20qoderBody=20sends),=20so?= =?UTF-8?q?=20that=20request=20is=20sent=20400K;=20a=20short=20one=20still?= =?UTF-8?q?=20gets=20the=20default=20window.=20Qoder's=20own=20metadata=20?= =?UTF-8?q?does=20say=201M=20for=20dfmodel:=20its=20live=20listing's=20max?= =?UTF-8?q?=5Finput=5Ftokens=20was=201000000=20(the=20context=200.2.4=20ca?= =?UTF-8?q?ched=20for=20it=20in=20a=20sandbox=20magpie=20on=202026-10-03,?= =?UTF-8?q?=20where=20MiniMax-M3=20read=20180000)=20and=200.2.5=20reads=20?= =?UTF-8?q?1M=20as=20its=20largest=20window,=20and=20docs.qoder.com/cli/mo?= =?UTF-8?q?del=20lists=20DeepSeek-V4-Flash=20with=20200K=20/=20400K=20/=20?= =?UTF-8?q?1M,=20so=20the=20reported=20context=20stays=201M.=20When=20kyzh?= =?UTF-8?q?ouxu's=20refusal=20happened=20isn't=20known:=20#700=20itself=20?= =?UTF-8?q?(ynky007)=20was=20opened=20before=200.2.5,=20when=20every=20req?= =?UTF-8?q?uest=20was=20sent=20context=5Flength=201000000,=20and=20their?= =?UTF-8?q?=20comment=20came=20after=20it=20(2026-10-04=2003:34=20UTC),=20?= =?UTF-8?q?so=20this=20is=20the=20way=200.2.5=20can=20refuse=20a=20request?= =?UTF-8?q?=20under=20the=20window=20magpie=20reports,=20not=20shown=20to?= =?UTF-8?q?=20be=20what=20they=20hit.=20context.test.mjs=20gains=20the=20c?= =?UTF-8?q?ase=20(180K=20prompt:=20200K=20with=20max=5Ftokens=201000,=2040?= =?UTF-8?q?0K=20with=2032000=20and=20with=20none=20asked);=20it=20fails=20?= =?UTF-8?q?on=200.2.5=20(200000)=20and=20passes;=20bun=20test=20(49=20pass?= =?UTF-8?q?)=20and=20bun=20scripts/check.mjs=20qoder=20under=20a=20temp=20?= =?UTF-8?q?HOME=20pass.=20Not=20tried=20with=20a=20real=20Qoder=20account.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/qoder/context.test.mjs | 12 ++++++++++++ packages/qoder/index.mjs | 11 +++++++---- packages/qoder/package.json | 2 +- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/packages/qoder/context.test.mjs b/packages/qoder/context.test.mjs index 1d762fa..3f4a8e4 100644 --- a/packages/qoder/context.test.mjs +++ b/packages/qoder/context.test.mjs @@ -49,3 +49,15 @@ test("a request is sent the default window while it fits, a larger one when it d expect(sent(700000 * 3)).toBe(1000000) expect(sent(1200000 * 3)).toBe(1000000) }) + +// a window holds the reply too: a prompt that fits 200K alone but not with +// the max_tokens asked for the reply is sent the next window (yetone/magpie#700) +test("the reply's max_tokens is counted in the window a request needs", () => { + const m = { ..._internal.modelInfo(flash), config: flash } + const ctx = (max_tokens) => + _internal.qoderBody({ max_tokens, messages: [{ role: "user", content: "a".repeat(180000 * 3) }] }, m).parameters.context_length + expect(ctx(1000)).toBe(200000) + expect(ctx(32000)).toBe(400000) + // none asked: the 32000 Qoder is sent counts + expect(_internal.qoderBody({ messages: [{ role: "user", content: "a".repeat(180000 * 3) }] }, m).parameters.context_length).toBe(400000) +}) diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index a14988a..b6a5833 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -576,11 +576,14 @@ function qoderMessages(msgs) { // (Qoder: changing the Context "may change the rate"), so it is asked for // only when the conversation needs it. The request's size is counted // generously, a token for every 3 bytes of its UTF-8 JSON, so a window is -// raised a little early rather than late. -function windowFor(chat, m) { +// raised a little early rather than late, and the reply's max_tokens is +// counted in it: a window holds the prompt and the reply, so a +// conversation of 180K asking 32K for the reply is sent 400K, not the 200K +// it would overflow while magpie says the model takes 1M (yetone/magpie#700). +function windowFor(chat, m, reply = 0) { const base = m.defaultWindow || (m.config?.max_input_tokens > 0 ? m.config.max_input_tokens : 0) || m.context if (!m.windows?.length) return m.context - const need = Math.ceil(Buffer.byteLength(JSON.stringify({ m: chat.messages ?? [], t: chat.tools ?? [] })) / 3) + const need = Math.ceil(Buffer.byteLength(JSON.stringify({ m: chat.messages ?? [], t: chat.tools ?? [] })) / 3) + Math.max(0, reply) if (base > 0 && need <= base) return base return m.windows.find((w) => w >= need) ?? m.windows[m.windows.length - 1] } @@ -596,7 +599,7 @@ function qoderBody(chat, m) { const [thinking, effort] = effortFor(chat.reasoning_effort, m) const params = { enable_thinking: thinking, max_tokens: chat.max_completion_tokens || chat.max_tokens || 32000 } if (effort) params.reasoning_effort = effort - const window = windowFor(chat, m) + const window = windowFor(chat, m, params.max_tokens) if (window > 0) params.context_length = window const body = { parameters: params, diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 47e29e7..31d6da8 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.5", + "version": "0.2.6", "description": "Qoder subscriptions in OpenCode and magpie: Qoder (qoder.com) and Qoder CN (qoder.cn)", "type": "module", "main": "./index.mjs",