zcode 0.1.10: a gift-only account's spent bucket doesn't hold up a model another of its plans still has (ganlerk on Discord) — an account with both ZCode Trust Build (GLM-5.3-Flash 14.5% used) and the Start Plan (GLM-5.3-Flash all used) showed the Start Plan's spent bucket as counting GLM-5.3-Flash, so magpie read the model, and the account, as 100% used and sent Flash requests elsewhere. The server picks the bucket (ZCode sends no plan with a request), so a gift-only account's usage now counts each model on its live bucket, the least used, and sets spent siblings aside, as an account with a coding plan already did. bun scripts/check.mjs zcode and the zcode tests pass; the new gift test fails on 0.1.9.

This commit is contained in:
yetone
2026-10-05 19:50:37 +08:00
parent 42783f0033
commit 6b13dc05e4
5 changed files with 70 additions and 13 deletions
+6
View File
@@ -112,3 +112,9 @@ A gift plan's models appear again as `<model>-Trial` entries, so a
request can spend the gift's quota on purpose. A trial entry is listed
while any live bucket serves its model, spent buckets included (a spent
entry answers 429 rather than vanishing mid-session).
Two plans can give one model (ZCode Trust Build and the Start Plan both
give GLM-5.3-Flash). The server picks the bucket, so the usage card
counts the model on its live bucket, the least used, and sets a spent
sibling aside. A spent sibling never makes the model, or the account,
look used up while another bucket still has quota.
+8
View File
@@ -65,6 +65,14 @@ Magpie plugin contract. No new UI page or test framework is introduced.
- When the plugin maps gift model quotas.
- Then the exhausted sibling does not make that gift entry appear exhausted.
### Allow a live sibling bucket on a gift-only account
- Given an account with no coding plan and two live gift plans (ZCode Trust Build and ZCode Start Plan) whose buckets both serve GLM-5.3-Flash.
- And the Start Plan's bucket is spent while Trust Build's has quota.
- When the plugin reports the account's usage.
- Then GLM-5.3-Flash and its trial entry count on Trust Build's bucket alone.
- And the spent Start Plan bucket stays on the card, set aside, so it doesn't mark the model or the account as used up.
### Keep plain models on the coding plan
- Given a caller uses an existing model ID without the Trial suffix.
+25
View File
@@ -186,3 +186,28 @@ test("a grant ending before the normal route TTL is removed on the next model re
clock.mockRestore()
}
})
test("a gift-only account's spent Start Plan bucket doesn't hold up a model its Trust Build still has", async () => {
// ganlerk's account (Discord): Trust Build's GLM-5.3-Flash 14.5% used,
// the Start Plan's GLM-5.3-Flash all used, its GLM-5.3 untouched
balance.plans = [
{ plan_id: "trust", user_plan_id: "t1", name: "ZCode Trust Build", status: "active", ends_at: now + 86400 },
{ plan_id: "start", user_plan_id: "s1", name: "ZCode Start Plan", status: "active", ends_at: now + 7 * 86400 },
]
balance.balances = [
{ plan_id: "start", user_plan_id: "s1", show_name: "GLM-5.3", capabilities: ["model:GLM-5.3"], total_units: 3_000_000, used_units: 0, remaining_units: 3_000_000 },
{ plan_id: "start", user_plan_id: "s1", show_name: "GLM-5.3-Flash", capabilities: ["model:GLM-5.3-Flash"], total_units: 5_000_000, used_units: 5_000_000, remaining_units: 0 },
{ plan_id: "trust", user_plan_id: "t1", show_name: "GLM-5.3-Flash", capabilities: ["model:GLM-5.3-Flash"], total_units: 100_000_000, used_units: 14_501_196, remaining_units: 85_498_804, expires_at: now + 3600 },
]
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 counting = (m) => u.windows.filter((w) => !w.aside && w.models?.some((x) => x.toLowerCase() === m.toLowerCase()))
for (const m of ["GLM-5.3-Flash", "GLM-5.3-Flash-Trial"]) {
expect(counting(m)).toHaveLength(1)
expect(counting(m)[0].used).toBeCloseTo(14.5, 1)
}
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).toHaveLength(3)
})
+30 -12
View File
@@ -538,14 +538,38 @@ const NO_START = "this account has no GLM Coding Plan, and ZCode's Start Plan ha
async function startUsage(s) {
const g = await giftOf(s)
if (!g) return { error: NO_START }
for (const w of g.windows) {
if (w.models?.length) w.models = [...w.models, ...w.models.map(giftID)]
delete w._plan
delete w._spent
}
const best = bestBuckets(g.windows)
g.windows = g.windows.map(({ _plan, _spent, ...w }, i) => {
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
// without holding the model up (ganlerk, Discord)
const models = w.models.filter((m) => best.get(m.toLowerCase()) === i)
if (!models.length) {
delete w.models
return { ...w, aside: true }
}
return { ...w, models: [...models, ...models.map(giftID)] }
})
return g
}
// 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
// (ZCode sends no plan with a request), so one spent bucket doesn't stop
// a model another still has.
function bestBuckets(windows) {
const best = new Map()
windows.forEach((w, i) => {
for (const m of w.models ?? []) {
const key = m.toLowerCase(), old = best.get(key), prev = windows[old]
if (old === undefined || (prev._spent && !w._spent) || (prev._spent === w._spent && w.used < prev.used)) best.set(key, i)
}
})
return best
}
// dualUsage shows both pools but only the pool a request can use counts
// for its model. Live gift models don't spend the Coding windows; spent
// or blocked gift buckets stay visible without holding up the Coding Plan.
@@ -571,13 +595,7 @@ async function dualUsage(s) {
const notModels = gift.allModels.map((m) => giftID(m).toLowerCase())
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 = new Map()
g.windows.forEach((w, i) => {
for (const m of w.models ?? []) {
const key = m.toLowerCase(), old = best.get(key), prev = g.windows[old]
if (old === undefined || (prev._spent && !w._spent) || (prev._spent === w._spent && w.used < prev.used)) best.set(key, i)
}
})
const best = bestBuckets(g.windows)
out.windows.push(...g.windows.map(({ _plan: p, _spent, ...w }, i) => {
const selected = gift.allModels.filter((m) => best.get(m.toLowerCase()) === i)
const models = selected.map(giftID)
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-zcode-auth",
"version": "0.1.9",
"version": "0.1.10",
"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",