zcode 0.1.28: a key's route is read again with each card read, after a Start Plan refusal, and a minute after one list without its plan (#369, #379)

Also publishes #371 (a team seat lists the person's gift plans and their
-Trial models, 935ddb5), which landed without a version bump.

#369: a BigModel GLM Coding Plan account got the Start Plan's 405 for about
seven minutes on plain models, then recovered with nothing changed. A plain
model goes to the Start Plan only when plansOf says start, i.e. the key's
/api/biz/subscription/list read with no VALID plan, and that answer was
kept for ten minutes. Now:
- a key whose list reads no plan where the last read found one keeps that
  for a minute, not ten (read the same way again, it is known: ten);
- a Start Plan refusal (405 / 3012 / block page) on a key account that
  wasn't a -Trial pick drops the route, so the next request reads the
  plans again.
Not known: what the list answered during the reporter's window; this is
the one path in the plugin that sends a plan-holding key's plain model to
zcode.z.ai, and it matches the window's length.

#379: the card's numbers are read fresh on every usage call already; what
lagged was the route (which plan, which gift) for up to ten minutes. Each
usage read now reads the route again, so routing follows what the card
shows.

Run: packages/zcode, Bun 1.4.2, sandbox HOME and TMPDIR: 110 pass, 0 fail.
The three new dual.test.mjs cases are red on 0.1.27+935ddb5's index.mjs
(3 fail). bun scripts/check.mjs zcode ok. Loaded through magpie's plugin
host (origin/main 4dfd88206, scripts/try.sh, sandbox HOME): zcode listed
with 4 models.
Not tried: against a real account; no account was in the sandbox.
This commit is contained in:
yetone
2026-10-25 14:25:56 +08:00
parent 3a59be3caf
commit b07478285c
4 changed files with 72 additions and 4 deletions
+5 -1
View File
@@ -83,7 +83,11 @@ the ZCode app's sign-in (`refresh` then holds `"source": "zcode"`).
## How requests are routed
Before each request, the plugin checks which plan the account is on, and
asks again every 10 minutes:
asks again every 10 minutes, and with every read of the usage card, so a
plan bought, a gift claimed or a plan lapsed reaches the requests when the
card shows it. A key whose plan list once reads no plan after reading one
is asked again after a minute, and a Start Plan refusal on such a key reads
the plans again on the next request:
- If the key's account has a GLM Coding Plan
(`/api/biz/subscription/list`), its requests go to that plan. A model
+49
View File
@@ -393,3 +393,52 @@ test("a gift-only account sends every request to the gift, and reads no balance
expect(sent.at(-1).origin).toBe("https://zcode.z.ai")
expect(sent.filter((c) => c.path === "/api/v1/zcode-plan/billing/balance").length).toBe(0)
})
// #369: a GLM Coding Plan account whose plan list read no plan once was sent
// to the Start Plan for ten minutes, and every request answered 405
test("a key's plan list that once reads no plan after reading one is asked again in a minute", async () => {
answers["/api/biz/subscription/list"] = ok([{ productName: "GLM Coding Lite", status: "VALID" }])
answers["/api/v1/zcode-plan/billing/balance"] = ok(balance)
answers["/api/anthropic/v1/messages"] = ok({ type: "message", content: [] })
answers["/api/v1/zcode-plan/anthropic/v1/messages"] = ok({ type: "message", content: [] })
await sendTo(MSG, body("GLM-5.3"))
const id = KEY + "\0" + jwt
expect(_internal.routes.get(id)).toMatchObject({ start: false, ttl: 600_000 })
_internal.routes.get(id).at = 0 // the ten minutes are up
answers["/api/biz/subscription/list"] = ok([])
await sendTo(MSG, body("GLM-5.3"))
expect(_internal.routes.get(id)).toMatchObject({ start: true, ttl: 60_000 })
// read the same way again, it is known: ten minutes
_internal.routes.get(id).at = 0
await sendTo(MSG, body("GLM-5.3"))
expect(_internal.routes.get(id)).toMatchObject({ start: true, ttl: 600_000 })
})
test("a Start Plan refusal on a key account reads its plans again on the next request", async () => {
answers["/api/biz/subscription/list"] = ok([{ productName: "GLM Coding Lite", status: "VALID" }])
answers["/api/v1/zcode-plan/billing/balance"] = ok(balance)
answers["/api/anthropic/v1/messages"] = ok({ type: "message", content: [] })
answers["/api/v1/zcode-plan/anthropic/v1/messages"] = new Response(JSON.stringify({ error: { message: "Method Not Allowed" } }), { status: 405 })
const id = KEY + "\0" + jwt
_internal.routes.set(id, { start: true, gift: null, at: Date.now(), ttl: 600_000 })
let res = await sendTo(MSG, body("GLM-5.3"))
expect(res.status).toBe(405)
expect(sent.at(-1).origin).toBe("https://zcode.z.ai")
res = await sendTo(MSG, body("GLM-5.3"))
expect(res.status).toBe(200)
expect(sent.at(-1).origin).toBe("https://api.z.ai")
expect(sent.at(-1).headers["x-api-key"]).toBe(KEY)
})
// #379: a plan bought, a gift claimed or a plan lapsed reaches the requests
// when the card shows it
test("a card read reads the route again", async () => {
answers["/api/biz/subscription/list"] = ok([{ productName: "GLM Coding Pro", status: "VALID" }])
answers["/api/v1/zcode-plan/billing/balance"] = ok(balance)
answers["/api/monitor/usage/quota/limit"] = ok({ limits: [{ type: "CREDIT_LIMIT", unit: 6, number: 1, percentage: 10 }] })
const id = KEY + "\0" + jwt
_internal.routes.set(id, { start: true, gift: null, at: Date.now(), ttl: 600_000 })
const u = await (await hooks()).usage(async () => coding, { id: "zcode" })
expect(u.windows[0].used).toBe(10)
expect(_internal.routes.get(id)).toMatchObject({ start: false, gift: { models: ["glm-5.3-flash"] } })
})
+17 -2
View File
@@ -224,7 +224,12 @@ async function plansOf(s) {
gift = await startPlan(s.jwt, s.device).catch(() => ((failed = !jwtExpired(s.jwt)), null))
sure = !failed
}
if (start) sure = true
// a key whose plan list read no plan, where the last read found one,
// is asked again in a minute rather than sent to the Start Plan for
// ten: one list without the plan (a renewal's boundary, a hiccup)
// turned a GLM Coding Plan account's every request into the Start
// Plan's 405 until the route was read again (#369)
if (start) sure = !(s.key && r && !r.start)
} catch {
// the plan list could not be read: a key account takes the dual route
// (start stays false, the gift is kept, a minute's ttl), so a gift
@@ -1916,7 +1921,13 @@ export async function ZCodeAuthPlugin({ client }) {
// it: the account blocked, or the network
if (start && res.status >= 400) {
const text = await res.text()
if (startRefused(res.status, text)) return kept(startBlocked(res, text))
if (startRefused(res.status, text)) {
// a key account sent to the Start Plan (its plan list read
// none) reads its plans again on the next request, so a
// coding plan that is there after all is used (#369)
if (s.key && !forcedGift) routes.delete(s.key + "\0" + s.jwt)
return kept(startBlocked(res, text))
}
res = new Response(text, { status: res.status, statusText: res.statusText, headers: res.headers })
}
// a key with no plan behind it: what Z.ai said, and why a
@@ -1972,6 +1983,10 @@ export async function ZCodeAuthPlugin({ client }) {
return out
}
try {
// a card read is a fresh read: the route is read again with it,
// so a plan bought, a gift claimed or a plan lapsed reaches the
// requests when the card shows it, not ten minutes later (#379)
routes.delete(s.key + "\0" + s.jwt)
const { start, gift } = await plansOf(s)
let card
if (isTeam(s)) {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-zcode-auth",
"version": "0.1.27",
"version": "0.1.28",
"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",
"type": "module",
"main": "./index.mjs",