diff --git a/packages/zcode/dual.test.mjs b/packages/zcode/dual.test.mjs index 87bf00d..5accf65 100644 --- a/packages/zcode/dual.test.mjs +++ b/packages/zcode/dual.test.mjs @@ -404,6 +404,55 @@ test("a rested -Trial entry's local 429 ends with the words magpie reads", async expect(said).toContain("not asked again for a minute") }) +// 11-08: a busy hold armed while a -Trial request was in flight answers it +// with the hold's own 429 (askBusy's in-flight stop), and that answer has +// no 3008 in it. spentUp counts every 429 that busy() does not claim as a +// spent bucket, so the hold's answer armed the blocked map — the model then +// answered "over its quota" for a minute with half its free quota left. +// The hold's answer must pass through the -Trial path untouched and arm +// nothing. +test("a busy hold's answer through a -Trial entry is passed on and arms no quota rest", async () => { + planAndBalance() + const waits = _internal.BUSY_WAITS.splice(0) + _internal.BUSY_WAITS.push(1) + try { + let caps = 0 + answers["/api/v1/zcode-plan/anthropic/v1/messages"] = () => { + caps++ + return new Response(JSON.stringify({ code: "3008", msg: "user concurrency limit exceeded" }), { status: 429, headers: { "content-type": "application/json" } }) + } + const opts = await freePlugin({ freeFirst: true }).then((h) => h.loader(async () => coding, { id: "zcode" })) + const ask = (model) => opts.fetch(MSG, { method: "POST", headers: { "content-type": "application/json" }, body: body(model) }) + // five asks in flight: the first ask of each caps once (one signal per + // request), the re-ask of each caps again — the fifth distinct cap hit + // arms the hold, and every ask that wakes after it comes home the + // hold's 429 + const all = await Promise.all([1, 2, 3, 4, 5].map(() => ask("GLM-5.3-Flash-Trial"))) + expect(all.map((r) => r.status)).toEqual([429, 429, 429, 429, 429]) + expect(_internal.busyHold.until).toBeGreaterThan(Date.now()) + for (const r of all) { + const said = JSON.parse(await r.text()) + const text = said.error?.message ?? said.msg ?? "" + // every answer is the hold's own, or the vendor's cap as it came — + // never a quota exhaustion the gift never said + expect(text.includes("turned away 5 requests") || said.code === "3008").toBe(true) + expect(text).not.toContain("over its quota") + expect(text).not.toContain("exhausted or temporarily unavailable") + } + // the hold's answer is no spent answer: the gift stays usable + expect(_internal.blocked.get(KEY + "\0" + jwt + "\0glm-5.3-flash")).toBeUndefined() + // and the next -Trial ask really goes to the gift once the hold is off + _internal.busyHold.until = 0 + answers["/api/v1/zcode-plan/anthropic/v1/messages"] = ok({ type: "message", content: [] }) + sent.length = 0 + expect((await ask("GLM-5.3-Flash-Trial")).status).toBe(200) + expect(sent.filter((c) => c.origin === "https://zcode.z.ai" && c.path.endsWith("/messages"))).toHaveLength(1) + } finally { + _internal.BUSY_WAITS.length = 0 + _internal.BUSY_WAITS.push(...waits) + } +}) + test("magpie's free-first switch is declared on, and off keeps the plain model on the plan", async () => { planAndBalance() answers["/api/v1/zcode-plan/anthropic/v1/messages"] = ok({ type: "message", content: [] }) diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index eb6a726..aa95673 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -2158,6 +2158,15 @@ function busyCap() { } } +// BUSY_HOLD_MARK marks an answer the hold made up: a request the gate or the +// hold stopped locally never reached ZCode, and it says nothing about the +// gift's quota. The gift paths (a -Trial pick, free first) read the mark +// before they judge the answer spent, so a hold's 429 passes through as the +// rate limit it is and arms no minute's rest (11-08: the hold's answer +// through a -Trial entry read as "over its quota" with half the free quota +// left). +const BUSY_HOLD_MARK = "x-magpie-busy-hold" + function busyHeldAnswer() { const wait = Math.max(1, Math.ceil((busyHold.until - busyHold.now()) / 1000)) const error = { @@ -2165,7 +2174,8 @@ function busyHeldAnswer() { message: `ZCode turned away ${BUSY_N} requests in the last minute as too many requests at once, each again when asked a second later. So that ZCode's firewall does not block this network, magpie sends no free Start Plan requests for the next ${wait}s. Wait, run fewer agents or subagents on ZCode at once, or use a GLM Coding Plan model`, } return new Response(JSON.stringify({ type: "error", error }), { - status: 429, statusText: STATUS_TEXT[429], headers: { "content-type": "application/json", "retry-after": String(wait) }, + status: 429, statusText: STATUS_TEXT[429], + headers: { "content-type": "application/json", "retry-after": String(wait), [BUSY_HOLD_MARK]: "1" }, }) } @@ -3067,7 +3077,11 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) { const json = (res.headers.get("content-type") ?? "").includes("application/json") if (res.status >= 400 || json) { const text = await res.text() - const spent = spentUp(text, res.status) + // the hold's own answer (BUSY_HOLD_MARK) is a rate limit the + // gift never said: it passes through as it came and arms + // no rest + const held = res.headers.get(BUSY_HOLD_MARK) === "1" + const spent = !held && spentUp(text, res.status) if (!spent && res.status < 400) return done(kept(res, text)) if (spent) rest(blocked, s.key + "\0" + s.jwt + "\0" + modelOf(orig).toLowerCase()) return spent && res.status < 400 ? kept(new Response(text, { status: 429, headers: res.headers })) : done(kept(res, text)) @@ -3087,7 +3101,9 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) { if (res.status >= 400 || json) { const text = await res.text() const cap = busy(res.status, text) - const spent = !cap && spentUp(text, res.status) + // the hold's own answer is no spent answer either (see the + // -Trial path above) + const spent = !cap && res.headers.get(BUSY_HOLD_MARK) !== "1" && spentUp(text, res.status) if (spent) { restModel(true) return done(kept(await spentSaid(await replayOnPlan()))) diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 0d1a1f7..2445b79 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.56", + "version": "0.1.57", "description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan, and claims its gift plans daily", "type": "module", "main": "./opencode.mjs",