From e460dae2f059c4d5cddb1ebd35eb0b3e32f6d267 Mon Sep 17 00:00:00 2001 From: MYCF711 <1451895195@qq.com> Date: Wed, 28 Oct 2026 10:26:47 +0800 Subject: [PATCH] workbuddy 0.1.41: a package with no cycle total reads as a wallet balance (#450) A WorkBuddy package with no CycleTotalCapacity is a prepaid wallet: it is sent as a balance window ("Prepaid credits", magpie#3950), beside the share of packages that have a total. A literal "0" total stays on the old path, so a lapsed package adds nothing; an account with no packages keeps its unknown allowance (no TotalCredits fallback). Reviewed at 5bdfa9048d66095d35af5e7955d0945db02dc3b5 over four rounds. Run on mote at that head: HOME=$(mktemp -d) bun test packages/workbuddy, 185 pass / 0 fail; bun scripts/check.mjs workbuddy ok. npm had 0.1.40, so 0.1.41 is free. Co-authored-by: MYCF711 --- packages/workbuddy/index.mjs | 23 ++++++++++++++++++++--- packages/workbuddy/package.json | 2 +- packages/workbuddy/usage.test.mjs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 797083e..9c39ae1 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -1738,14 +1738,31 @@ const compact = (n) => (Number.isInteger(n) ? String(n) : n.toFixed(2).replace(/ // as used or left beside the share (magpie#659). The free plan is said as a // plan: "Free" alone beside the account read as the model being free, and // routing as preferring it for that (#337). +// +// A package with no CycleTotalCapacity has no denominator: it is sent as +// magpie's balance-shaped window (#3950), {balance, unit} with no used, +// limit or aside, and routing ranks it by what is left. A literal "0" total +// is still a total, not a wallet: it stays on the old path, so a lapsed or +// placeholder package adds nothing. A literal "0" remainder is a reading +// of nothing left, not an absent field; only a package with no +// CycleRemainCapacity at all leaves the window out. TotalCredits is not a +// fallback: an account with no packages at all keeps its unknown allowance, +// as before, so its free models are not skipped. function usageOf(sum, plan) { const out = { plan: plan || (sum?.IsPaidUser ? "Pro" : "Free plan"), windows: [] } - let total = 0, used = 0 + let total = 0, used = 0, left = 0, bags = 0 for (const p of sum?.Packages ?? []) { - total += capacity(p?.CycleTotalCapacity) - used += capacity(p?.CycleUsedCapacity) + const hasTotal = p?.CycleTotalCapacity != null && p.CycleTotalCapacity !== "" + if (hasTotal) { + total += capacity(p.CycleTotalCapacity) + used += capacity(p?.CycleUsedCapacity) + } else if (p?.CycleRemainCapacity != null && p.CycleRemainCapacity !== "") { + left += capacity(p.CycleRemainCapacity) + bags++ + } } if (total > 0) out.windows.push({ name: "Credits", used: (100 * used) / total, display: `${compact(used)} / ${compact(total)}`, amount: used, limit: total, unit: "credits" }) + if (bags > 0 && (total === 0 || left > 0)) out.windows.push({ name: "Prepaid credits", balance: left, display: `${compact(left)} credits`, unit: "credits" }) return out } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index 990e4eb..9a73b2b 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.1.40", + "version": "0.1.41", "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)", "type": "module", "main": "./index.mjs", diff --git a/packages/workbuddy/usage.test.mjs b/packages/workbuddy/usage.test.mjs index 1bb5195..234be8a 100644 --- a/packages/workbuddy/usage.test.mjs +++ b/packages/workbuddy/usage.test.mjs @@ -78,6 +78,36 @@ test("counts and plans as Go says them", () => { expect(() => _internal.usageOf({ Packages: [{ CycleTotalCapacity: "lots" }] })).toThrow('strconv.ParseFloat: parsing "lots": invalid syntax') }) +test("a prepaid credit package with no total is a balance reading (#3950)", () => { + expect(_internal.usageOf({ IsPaidUser: false, Packages: [{ CycleTotalCapacity: "", CycleUsedCapacity: "", CycleRemainCapacity: "750" }] }, "")) + .toEqual({ plan: "Free plan", windows: [{ name: "Prepaid credits", balance: 750, display: "750 credits", unit: "credits" }] }) + // a literal "0" is a reading of nothing left, not an absent field + expect(_internal.usageOf({ Packages: [{ CycleTotalCapacity: "", CycleRemainCapacity: "0" }] }, "")) + .toEqual({ plan: "Free plan", windows: [{ name: "Prepaid credits", balance: 0, display: "0 credits", unit: "credits" }] }) + // no remainder field at all: unknown, not a balance + expect(_internal.usageOf({ Packages: [{ CycleTotalCapacity: "", CycleRemainCapacity: "" }] }, "")) + .toEqual({ plan: "Free plan", windows: [] }) + // a "0" total is still a total (a lapsed or placeholder package): old path, + // no balance reading, so the account stays unknown as on main + expect(_internal.usageOf({ Packages: [{ CycleTotalCapacity: "0", CycleRemainCapacity: "0" }] }, "")) + .toEqual({ plan: "Free plan", windows: [] }) + // one package with a total and one without: the latter keeps its own reading + expect(_internal.usageOf({ Packages: [ + { CycleTotalCapacity: "1000", CycleUsedCapacity: "100" }, + { CycleTotalCapacity: "", CycleRemainCapacity: "750" }, + ] }, "")) + .toEqual({ plan: "Free plan", windows: [ + { name: "Credits", used: 10, display: "100 / 1000", amount: 100, limit: 1000, unit: "credits" }, + { name: "Prepaid credits", balance: 750, display: "750 credits", unit: "credits" }, + ] }) + // TotalCredits alone is not a reading: an account with no packages stays + // unknown, so its free models are not skipped + expect(_internal.usageOf({ Packages: [], TotalCredits: "120" }, "")) + .toEqual({ plan: "Free plan", windows: [] }) + expect(_internal.usageOf({ Packages: [], TotalCredits: "0" }, "")) + .toEqual({ plan: "Free plan", windows: [] }) +}) + test("WorkBuddy's refusals are the card's error", async () => { const auth = { type: "oauth", access: "a", expires: later(), uid: "u" } // a bare 401 (no invalid_token) with nothing to renew with: said, not marked