zcode 0.1.53: a spent bucket's 200 says 429 on the plain path too (#817)

A ZCode answer that carries a spent code (1005 "exceed quota limit",
1113) inside an HTTP 200 now reaches magpie as a 429 on every path. Before,
only a -Trial entry (forcedGift) and free first's gift did this. On a
gift-only Start Plan account, a request for the plain model (GLM-5.3) went
through `return done(kept(res))` as a 200. magpie took it for a reply that
did not stream (502), failOther, and its window branch rested the whole
account until the bucket reset. GLM-5.3-Flash, which has its own bucket,
went unused for ~22h. As a 429 it is failQuota, and magpie rests the model
whose window is full.

spentSaid reads only a non-stream JSON answer under 400. Any other answer,
a stream included, goes on as it came. A reply whose text happens to say
1005 stays a 200. The same applies to the plan answer free first replays
on (replayOnPlan, three places), and to the coding plan's own 200 with
1113.

Run at 081c67ac on mote, HOME a temp folder: bun test packages/zcode
212 pass, 0 fail; the whole repo bun test 6558 pass, 20 skip, 0 fail;
scripts/check.mjs exit 0. Broken by hand, each red: the plain path back
to done(kept(res)) (gift.test's two new tests, 200 for 429); the
replays without spentSaid (dual.test's new free-first test).

Not tried: a live spent bucket. The 200 body is the reporter's words, and
dual.test's fixture already had it.

Reported-by: zane668
This commit is contained in:
yetone
2026-11-07 09:11:36 +08:00
parent 006e96ef03
commit 367edb503c
4 changed files with 68 additions and 8 deletions
+13
View File
@@ -230,6 +230,19 @@ test("a gift 200 that says the quota is spent is answered from the plan too", as
expect(sent.filter((c) => c.origin === "https://api.z.ai" && c.path.endsWith("/messages"))).toHaveLength(1)
})
// the plan free first falls back on, itself spent inside a 200, says 429
// as a plain request's plan does (plugins#817)
test("free first's plan answer that says it is spent inside a 200 says 429", async () => {
planAndBalance()
answers["/api/v1/zcode-plan/anthropic/v1/messages"] = new Response(JSON.stringify({ code: 1005, msg: "exceed quota limit" }), { headers: { "content-type": "application/json" } })
answers["/api/anthropic/v1/messages"] = new Response(JSON.stringify({ code: 1113, msg: "Insufficient balance or no resource package. Please recharge." }), { headers: { "content-type": "application/json" } })
const opts = await freePlugin({ freeFirst: true }).then((h) => h.loader(async () => coding, { id: "zcode" }))
const res = await opts.fetch(MSG, { method: "POST", headers: { "content-type": "application/json" }, body: body("GLM-5.3-Flash") })
expect(res.status).toBe(429)
expect((await res.json()).code).toBe(1113)
expect(sent.filter((c) => c.origin === "https://api.z.ai" && c.path.endsWith("/messages"))).toHaveLength(1)
})
test("after the minute's rest the plan keeps serving, inside the route's ten", async () => {
planAndBalance()
answers["/api/v1/zcode-plan/anthropic/v1/messages"] = new Response(JSON.stringify({ error: { message: "Insufficient balance or no resource package", code: 1113 } }), { status: 429, headers: { "content-type": "application/json" } })
+34 -2
View File
@@ -14,13 +14,14 @@ const MSG = "https://api.z.ai/api/anthropic/v1/messages"
const ok = (data) => new Response(JSON.stringify({ code: 0, data }), { headers: { "content-type": "application/json" } })
const real = globalThis.fetch
const sent = []
let balance, paid, giftReply
let balance, paid, giftReply, planReply
beforeEach(() => {
_internal.routes.clear()
_internal.blocked.clear()
sent.length = 0
paid = 100
planReply = null
giftReply = new Response(JSON.stringify({ type: "message", content: [] }), { headers: { "content-type": "application/json" } })
balance = {
server_time: now,
@@ -35,7 +36,7 @@ beforeEach(() => {
if (url.pathname.endsWith("/messages")) {
const text = init.body ?? (input instanceof Request ? await input.text() : undefined)
sent.push({ origin: url.origin, path: url.pathname, body: JSON.parse(text), headers: new Headers(init.headers ?? (input instanceof Request ? input.headers : {})) })
return url.origin === "https://zcode.z.ai" ? giftReply.clone() : ok({ type: "message", content: [] })
return url.origin === "https://zcode.z.ai" ? giftReply.clone() : planReply ? planReply.clone() : ok({ type: "message", content: [] })
}
throw new Error("unmocked request blocked: " + url.pathname)
}
@@ -115,6 +116,37 @@ test("a gift quota error inside HTTP 200 becomes 429 without paid fallback", asy
expect(sent[0].origin).toBe("https://zcode.z.ai")
})
// plugins#817 (zane668): a gift-only account's own model, not a -Trial
// entry, out of its bucket for the day is answered 200 "exceed quota
// limit" as the trial entry's is. It says 429 too, its words kept, so
// magpie rests that model and not the account, whose other buckets
// (GLM-5.3-Flash's) still serve.
test("a gift-only account's plain model spent inside HTTP 200 becomes 429", async () => {
const only = { type: "oauth", access: jwt, refresh: JSON.stringify({ ...state, key: undefined, plan: undefined }), expires: 0 }
const said = { code: 1005, msg: "exceed quota limit", logid: "x" }
giftReply = new Response(JSON.stringify(said), { headers: { "content-type": "application/json" } })
const res = await send(original, only)
expect(res.status).toBe(429)
expect(await res.json()).toEqual(said)
expect(sent).toHaveLength(1)
expect(sent[0]).toMatchObject({ origin: "https://zcode.z.ai", body: { model: original } })
// an answer that is a reply stays the reply it was
giftReply = new Response(JSON.stringify({ type: "message", content: [{ type: "text", text: "1005" }] }), { headers: { "content-type": "application/json" } })
const fine = await send(original, only)
expect(fine.status).toBe(200)
expect((await fine.json()).content[0].text).toBe("1005")
})
// the coding plan's own spent answer inside a 200 is said the same way
test("the coding plan's spent answer inside HTTP 200 becomes 429", async () => {
planReply = new Response(JSON.stringify({ code: 1113, msg: "Insufficient balance or no resource package. Please recharge." }), { headers: { "content-type": "application/json" } })
const res = await send("glm-5.3")
expect(res.status).toBe(429)
expect((await res.json()).code).toBe(1113)
expect(sent).toHaveLength(1)
expect(sent[0].origin).toBe("https://api.z.ai")
})
test("an exhausted gift alias is refused locally without a paid request", async () => {
paid = 0
Object.assign(balance.balances[0], { used_units: 100, remaining_units: 0 })
+20 -5
View File
@@ -315,6 +315,19 @@ function spentUp(text, status) {
return c1 === "1113" || c1 === "1005" || c2 === "1113" || c2 === "1005"
}
// spentSaid is an answer that carries spentUp's code inside a 200, as a
// spent bucket's can, said as the 429 it is (plugins#817: a Start Plan
// account's GLM-5.3 out for the day answered 200 "exceed quota limit",
// which magpie took for a reply that did not stream and rested the whole
// account, GLM-5.3-Flash's own bucket with it). Any other answer goes on
// as it came; a stream's body is never read here.
async function spentSaid(res) {
if (res.status >= 400 || !(res.headers.get("content-type") ?? "").includes("application/json")) return res
const text = await res.text()
const status = spentUp(text, res.status) ? 429 : res.status
return new Response(text, { status, statusText: status === 429 ? "Too Many Requests" : res.statusText, headers: res.headers })
}
// ---- keys ------------------------------------------------------------------------
function teamHeaders(base, org, project) {
@@ -2708,7 +2721,7 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
if (EDGE_PAGE.test(text)) edge.until = Date.now() + EDGE_HOLD
if (freeGift) {
restModel(false)
return done(kept(await replayOnPlan()))
return done(kept(await spentSaid(await replayOnPlan())))
}
if (s.key && !forcedGift) routes.delete(s.key + "\0" + s.jwt)
return kept(startBlocked(res, text))
@@ -2756,18 +2769,20 @@ export async function ZCodeAuthPlugin({ client, magpie } = {}, options = {}) {
const spent = !cap && spentUp(text, res.status)
if (spent) {
restModel(true)
return done(kept(await replayOnPlan()))
return done(kept(await spentSaid(await replayOnPlan())))
}
if (res.status >= 400) {
if (!cap && (res.status === 401 || res.status === 403)) routes.delete(s.key + "\0" + s.jwt)
return done(kept(await replayOnPlan()))
return done(kept(await spentSaid(await replayOnPlan())))
}
return done(kept(res, text))
}
}
// the plan's answer goes on as it came, the account kept: the
// built-in never marked a ZCode account lapsed nor cleared one
return done(kept(res))
// built-in never marked a ZCode account lapsed nor cleared one.
// A spent bucket's 200 says 429 (spentSaid), as a trial
// entry's does, so magpie rests the model, not the account
return done(kept(await spentSaid(res)))
},
}
},
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-zcode-auth",
"version": "0.1.52",
"version": "0.1.53",
"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",