zcode 0.1.23: with two gift plans each bucket is named by its plan, and the live ones come first

01huadalang (Discord #feedback, 10-24): an account with the 1024 plan and
the new-user plan showed GLM-5.3 at 0 once the new-user plan's GLM-5.3 was
used up, and never showed the 1024 plan's GLM-5.3.

Cause: the plugin already kept every live plan's buckets (giftOf) and
counted a model on the live one (bestBuckets), setting the spent one aside.
But every window was named by its bucket's show_name alone, so both read
"GLM-5.3", and the windows went out in the balance's order: the new-user
plan's spent GLM-5.3 and its Flash first, the 1024 plan's after. magpie's
account row shows a card's first two windows and puts the rest in its
tooltip, so the row showed the spent GLM-5.3 at 0 and the 1024 plan's
allowance was out of sight, and where both showed they couldn't be told
apart.

Fix (startUsage and dualUsage alike): when the gift's buckets come from
more than one plan, each window is named "<plan> · <bucket>" ("ZCode 1024
Dev Day · GLM-5.3"); one plan's buckets keep their own names, as before.
The windows a request spends go before those set aside, each group in the
balance's order. A used-up plan still shows, at 100% used, set aside.

The 1024 plan's plan_id, name, show_names, capabilities and grant_units in
the new fixture are real bytes: the owner's account read
/billing/preview (read-only GET, credentials read in memory, nothing
written) and is offered "ZCode 1024 Dev Day" (zcode-v3-start-plan-1024,
GLM-5.3-Flash 300M, GLM-5.3 200M). That account has no active plan, so
its /billing/balance is empty; the balance rows are written in the shape
the existing fixtures use, and the new-user plan's name is the Start
Plan's from ganlerk's balance. The reporter's real balance wasn't seen.

Run at this head (parent efa41e3, origin/main fetched just before):
- packages/zcode: HOME=$(mktemp -d) bun test (Bun) 92 pass, 0 fail. The
  tests import bun:test, so they don't run under Node; under Node 24 the
  plugin loaded (node --check) and a scratch script ran auth.usage over the
  reporter's two-plan shape, gift-only and with a coding plan, and gave
  the labelled, ordered windows.
- Whole repo, as CI runs it: HOME=$(mktemp -d) bun test 2375 pass,
  0 fail; bun scripts/check.mjs all pass.
- Broke it by hand: index.mjs at efa41e3 turns the two new gift tests and
  ganlerk's test red (3 fail); keeping the labels but dropping the
  ordering turns both new tests red.

Changed expectations: ganlerk's test and dual.test's "every live plan"
test have two plans, so their windows now carry the plan's name.

Not tried: no account with two active gift plans was available, so no
real card with both was seen in magpie. Not changed: in dualUsage a
spent bucket set aside keeps its plain model names (startUsage drops
them); aside windows don't count, so left as it was.
This commit is contained in:
yetone
2026-10-24 05:04:58 +08:00
parent efa41e36e3
commit 1f8d38b8cd
4 changed files with 84 additions and 11 deletions
+3 -2
View File
@@ -373,9 +373,10 @@ test("every live plan spends first by its buckets' models, and the card shows th
const before = sent.length
expect((await sendTo(MSG, body("GLM-4"))).status).toBe(200)
expect(sent.slice(before).at(-1).origin).toBe("https://api.z.ai")
// the card shows both plans' live buckets, spent ones included, none of the ended plan's
// the card shows both plans' live buckets, spent ones included, none of the
// ended plan's, each named after its plan as there are two
const u = await (await hooks()).usage(async () => coding, { id: "zcode" })
expect(u.windows.map((w) => w.name)).toEqual(["5 hours", "Trust Build", "GLM-5.2-Trial", "GLM-5.2 late"])
expect(u.windows.map((w) => w.name)).toEqual(["5 hours", "Start Plan · Trust Build", "GLM Festival Grant · GLM-5.2-Trial", "GLM Festival Grant · GLM-5.2 late"])
expect(u.windows.some((w) => w.name === "Old")).toBe(false)
expect(u.windows[0].notModels).toEqual(["glm-5.3-flash-trial", "glm-5.2-trial"])
expect(u.windows.at(-1).aside).toBe(true) // a spent sibling cannot cap a live bucket
+50 -1
View File
@@ -208,7 +208,7 @@ test("a gift-only account's spent Start Plan bucket doesn't hold up a model its
}
expect(counting("GLM-5.3")[0].used).toBe(0)
// the spent bucket is still on the card, as ZCode shows it
expect(u.windows.find((w) => w.used === 100)).toMatchObject({ name: "GLM-5.3-Flash", aside: true })
expect(u.windows.find((w) => w.used === 100)).toMatchObject({ name: "ZCode Start Plan · GLM-5.3-Flash", aside: true })
expect(u.windows).toHaveLength(3)
})
@@ -246,3 +246,52 @@ test("a coding plan account's gift stays on its trial entries alone", async () =
const models = await (await hooks()).provider.models({ models: {} }, { auth })
expect(Object.keys(models).filter((id) => !(id in coding))).toEqual(["GLM-5.3-Trial"])
})
// 01huadalang's account (Discord, 10-24): the new-user Start Plan and the
// 1024 Dev Day plan both give GLM-5.3, and the new-user one is spent. The
// 1024 plan's id, name, show_names, capabilities and grant_units are the
// real /billing/preview bytes of an account it was offered to; its balance
// rows are written in the shape the balance gives (no 1024 balance was
// read), with the Start Plan's name as ganlerk's balance has it.
const twoPlans = () => {
balance.plans = [
{ plan_id: "zcode-v3-start-plan", user_plan_id: "s1", name: "ZCode Start Plan", status: "active", ends_at: now + 7 * 86400 },
{ plan_id: "zcode-v3-start-plan-1024", user_plan_id: "d1", name: "ZCode 1024 Dev Day", status: "active", ends_at: now + 30 * 86400 },
]
balance.balances = [
{ plan_id: "zcode-v3-start-plan", user_plan_id: "s1", show_name: "GLM-5.3", capabilities: ["model:glm-5.3"], total_units: 3_000_000, used_units: 3_000_000, remaining_units: 0 },
{ plan_id: "zcode-v3-start-plan", user_plan_id: "s1", show_name: "GLM-5.3-Flash", capabilities: ["model:glm-5.3-flash"], total_units: 5_000_000, used_units: 0, remaining_units: 5_000_000 },
{ plan_id: "zcode-v3-start-plan-1024", user_plan_id: "d1", entitlement_id: "ent-zcode-v3-start-plan-1024-1", show_name: "GLM-5.3-Flash", capabilities: ["model:glm-5.3-flash"], total_units: 300_000_000, used_units: 30_000_000, remaining_units: 270_000_000 },
{ plan_id: "zcode-v3-start-plan-1024", user_plan_id: "d1", entitlement_id: "ent-zcode-v3-start-plan-1024-2", show_name: "GLM-5.3", capabilities: ["model:glm-5.3"], total_units: 200_000_000, used_units: 50_000_000, remaining_units: 150_000_000 },
]
}
test("two gift plans' GLM-5.3 each show, named by plan, the live one first (01huadalang)", async () => {
twoPlans()
const only = { type: "oauth", access: jwt, refresh: JSON.stringify({ ...state, key: undefined, plan: undefined }), expires: 0 }
const u = await (await hooks()).auth.usage(async () => only, { id: "zcode" })
const names = u.windows.map((w) => w.name)
expect(new Set(names).size).toBe(names.length)
// the 1024 plan's GLM-5.3 shows its own amount left, and counts the model
expect(u.windows.find((w) => w.name === "ZCode 1024 Dev Day · GLM-5.3")).toMatchObject({ used: 25, amount: 50_000_000, limit: 200_000_000, models: ["glm-5.3", "GLM-5.3-Trial"] })
expect(u.windows.find((w) => w.name === "ZCode 1024 Dev Day · GLM-5.3").aside).toBeUndefined()
// the spent new-user plan's GLM-5.3 still shows, used up, set aside
expect(u.windows.find((w) => w.name === "ZCode Start Plan · GLM-5.3")).toMatchObject({ used: 100, amount: 3_000_000, aside: true })
// magpie's account row shows a card's first two windows: what is spent now
expect(u.windows.slice(0, 2).every((w) => !w.aside)).toBe(true)
expect(u.windows.slice(0, 2).some((w) => w.name === "ZCode 1024 Dev Day · GLM-5.3")).toBe(true)
})
test("a coding plan account's two gift plans' GLM-5.3 each show, named by plan", async () => {
twoPlans()
const u = await (await hooks()).auth.usage(async () => auth, { id: "zcode" })
const gift = u.windows.slice(1)
expect(gift.map((w) => w.name)).toEqual([
"ZCode Start Plan · GLM-5.3-Flash-Trial",
"ZCode 1024 Dev Day · GLM-5.3-Trial",
"ZCode Start Plan · GLM-5.3",
"ZCode 1024 Dev Day · GLM-5.3-Flash",
])
expect(gift[1]).toMatchObject({ used: 25, models: ["GLM-5.3-Trial"] })
expect(gift[2]).toMatchObject({ used: 100, aside: true })
})
+30 -7
View File
@@ -578,13 +578,16 @@ async function claimHint(s) {
// startUsage is a gift-only account's card: its buckets, or the words
// for having none to show. The card's header already names the plan, so
// the windows keep their own names.
// the windows of one plan keep their own names; with several plans each
// bucket is named after its plan (see byPlan).
const NO_START = "this account has no GLM Coding Plan, and ZCode's Start Plan has ended or was never started"
async function startUsage(s) {
const g = await giftOf(s)
if (!g) return { error: NO_START }
const best = bestBuckets(g.windows)
g.windows = g.windows.map(({ _plan, _spent, ...w }, i) => {
const label = byPlan(g.windows)
g.windows = countedFirst(g.windows.map(({ _plan, _spent, ...w }, i) => {
w.name = label(w.name, _plan)
if (!w.models?.length) return w
// a model two plans give (Trust Build's and the Start Plan's Flash)
// counts on the bucket ZCode spends now; the other stays on the card
@@ -595,10 +598,28 @@ async function startUsage(s) {
return { ...w, aside: true }
}
return { ...w, models: [...models, ...models.map(giftID)] }
})
}))
return g
}
// byPlan names a bucket after its plan ("ZCode 1024 Dev Day · GLM-5.3")
// when the gift's buckets come from more than one plan: two plans each
// give a GLM-5.3, and named alike a spent one and a live one can't be
// told apart (01huadalang, Discord: the new-user plan's spent GLM-5.3
// showed, the 1024 plan's own GLM-5.3 didn't). One plan's buckets keep
// their own names, as the card's header names the plan.
function byPlan(windows) {
const several = new Set(windows.map((w) => w._plan || "")).size > 1
return (name, plan) => (several && plan ? plan + " · " + name : name)
}
// countedFirst puts the buckets a request spends before those set aside
// (a spent bucket another plan's live one stands in for), each group in
// the balance's order: magpie's account row shows a card's first two
// windows and leaves the rest to its tooltip, so a spent plan listed first
// hid the live plan's allowance for the same model.
const countedFirst = (ws) => [...ws.filter((w) => !w.aside), ...ws.filter((w) => w.aside)]
// bestBuckets is, for each model the gift's buckets serve, the index of
// the one a request spends: a live one before a spent one, the least
// used of those. The server picks among an account's buckets itself
@@ -641,19 +662,21 @@ async function dualUsage(s) {
if (notModels.length) out.windows = out.windows.map((w) => w.aside ? w : { ...w, notModels })
// Several buckets may serve one model: one spent sibling cannot block it.
const best = bestBuckets(g.windows)
out.windows.push(...g.windows.map(({ _plan: p, _spent, ...w }, i) => {
const label = byPlan(g.windows)
out.windows.push(...countedFirst(g.windows.map(({ _plan: p, _spent, ...w }, i) => {
const selected = gift.allModels.filter((m) => best.get(m.toLowerCase()) === i)
const models = selected.map(giftID)
const scoped = models.length ? { ...w, models } : { ...w, aside: true }
// the card names a bucket's model as the picker does (GLM-5.3-Flash-Trial);
// no plan prefix — the card's section header already says the account
// no plan prefix while one plan gives them all — the card's section
// header already says the account; with several, byPlan names each
let name = w.name
for (const m of selected) {
const at = name.toLowerCase().indexOf(m.toLowerCase())
if (at >= 0) name = name.slice(0, at) + giftID(m) + name.slice(at + m.length)
}
return { ...scoped, name }
}))
return { ...scoped, name: label(name, p) }
})))
}
} catch {
if (cached) {
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-zcode-auth",
"version": "0.1.22",
"version": "0.1.23",
"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",