diff --git a/packages/workbuddy/README.md b/packages/workbuddy/README.md index fc72b84..b3d6278 100644 --- a/packages/workbuddy/README.md +++ b/packages/workbuddy/README.md @@ -399,6 +399,14 @@ What is measured, and what is easy to read wrongly: 429 whose named reset is still ahead makes the window full — `used: 100`, `resetsAt` the reset the vendor named — whatever the rows add up to. The sum carries the window only until the first refusal. +- **The sum is shown, not routed on.** Until the vendor refuses, the card is + set aside (`aside: true`): magpie draws it beside the account and never + counts it as the account's allowance, so an estimate near its cap does not + stop the model. The refusal's card does (`refused: true`), and magpie's + gateway rests this model alone on this account until the reset the + refusal names. Before 0.2.14 the sum stopped the model too, and a paid + China account read 99% of a cap it had never been refused at, with 2,457 + credits left (plugins#641). - **The card names both ids**, `deepseek-v4.1-flash` and `deepseek-v4.1-flash-sg`: magpie matches a window's models by exact id (only an `@size` suffix folds into the bare id), so one alone would leave @@ -435,10 +443,10 @@ exhausted, and the next request opens a fresh one. The China build (`copilot.tencent.com`) carries the same window: two Free-plan accounts refused on 2026-11-02 at 200,389,043 and 200,485,923 tokens, so its cap is measured at about 2e8 — and its refusals name the reset in Chinese -(`将在 … UTC+8 重置`), which reads as an edge the same way. Its card stops -at a **soft cap of 1.9e8**, 5% short of the measured one, so routing rests -the model before the vendor refuses it; the headroom absorbs the account's -spending on other machines, which this ledger cannot see. +(`将在 … UTC+8 重置`), which reads as an edge the same way. Its card reads +against 2e8. In 0.2.12 and 0.2.13 it read against a soft cap of 1.9e8, so +that routing rested the model before the vendor refused it; with the sum set +aside that headroom only stopped the model early, so it is gone. ## When free models work but a credit model does not diff --git a/packages/workbuddy/freewindow.test.mjs b/packages/workbuddy/freewindow.test.mjs index d50e978..bf5a7c6 100644 --- a/packages/workbuddy/freewindow.test.mjs +++ b/packages/workbuddy/freewindow.test.mjs @@ -112,9 +112,9 @@ test("other accounts, other models and the other build are left out", () => { // The China build (copilot.tencent.com) has the same window under its own // words: measured 2026-11-02, two Free-plan accounts refused at 200,389,043 -// and 200,485,923 tokens counted the same way. Its card stops at a soft cap -// of 1.9e8 — 5% short of the measured one — so routing rests the model -// before the vendor refuses it. A refusal in force still reads full. +// and 200,485,923 tokens counted the same way. Its card reads against 2e8 +// (a soft 1.9e8 in 0.2.12 and 0.2.13, plugins#641). A refusal in force +// still reads full. const cnRow = (t, opts = {}) => row(t, { ...opts, provider: "workbuddy" }) // the refusal the China build sends, in its own words const cnRefusal = (reset, account = "me@example.com") => @@ -125,13 +125,13 @@ const cnRefusal = (reset, account = "me@example.com") => err: `WorkBuddy: 您的使用量已超出频率限制,将在 ${reset} UTC+8 重置,您也可以切换其他模型继续使用。`, }) -test("the China build counts its own rows against the 190M soft cap", () => { +test("the China build counts its own rows against 200M", () => { const rows = [cnRow("2026-11-02T02:32:00+08:00", { in: 1_000_000, cache: 188_000_000, out: 500_000 }), cnRow("2026-11-02T03:00:00+08:00", { in: 200 })] const w = _internal.freeWindowOf(rows, "me@example.com", at("2026-11-02T12:00:00+08:00"), _internal.FREE_WINDOW_CN) expect(w.amount).toBe(189_500_200) - expect(w.limit).toBe(1.9e8) - expect(w.used).toBeCloseTo(99.737, 3) - expect(w.display).toBe("189.5M / 190M") + expect(w.limit).toBe(2e8) + expect(w.used).toBeCloseTo(94.75, 3) + expect(w.display).toBe("189.5M / 200M") expect(w.resetsAt).toBe(iso("2026-11-03T02:32:00+08:00")) expect(w.unit).toBe("tokens") expect(w.models).toEqual(["deepseek-v4.1-flash", "deepseek-v4.1-flash-sg"]) @@ -151,7 +151,7 @@ test("the two builds' rows stay apart over one account", () => { expect(ai.limit).toBe(1e8) const cn = _internal.freeWindowOf(rows, "me@example.com", at("2026-11-02T12:00:00+08:00"), _internal.FREE_WINDOW_CN) expect(cn.amount).toBe(98_000_000) - expect(cn.limit).toBe(1.9e8) + expect(cn.limit).toBe(2e8) }) // the China build names its reset in Chinese, the time in the same shape and @@ -163,7 +163,7 @@ test("windowReset reads the China refusal", () => { expect(Number.isNaN(_internal.windowReset("将在明天重置"))).toBe(true) }) -// same as the AI probe the review asked for: a refusal at 100M of a 190M +// same as the AI probe the review asked for: a refusal at 100M of a 200M // window (the account spent elsewhere) reads full, refused, on the Chinese // words alone test("a China account refused under the cap reads full", () => { @@ -172,11 +172,12 @@ test("a China account refused under the cap reads full", () => { cnRefusal("2026-11-02 19:17:02"), ] const w = _internal.freeWindowOf(rows, "me@example.com", at("2026-11-02T12:00:00+08:00"), _internal.FREE_WINDOW_CN) - expect(w.amount).toBe(1.9e8) // the rows sum to 80M + expect(w.amount).toBe(2e8) // the rows sum to 80M expect(w.used).toBe(100) expect(w.refused).toBe(true) + expect(w.aside).toBeUndefined() // the vendor's word stops the model expect(w.resetsAt).toBe(iso("2026-11-02T19:17:02+08:00")) - expect(w.display).toBe("190.0M / 190M") + expect(w.display).toBe("200.0M / 200M") }) // readLedgerTail stops the chain on both languages' edges: a Chinese refusal @@ -194,7 +195,7 @@ test("the China read stops on a Chinese refusal's edge", () => { const rows = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"), _internal.FREE_WINDOW_CN) // the row that reveals the edge is kept — the read goes back to it and // stops — while the window the refusal closed reads nothing: the chain - // opens at the next answered row, 300k of 190M, resetting 24h later + // opens at the next answered row, 300k of 200M, resetting 24h later const w = _internal.freeWindowOf(rows, "me@example.com", now, _internal.FREE_WINDOW_CN) expect(w.amount).toBe(300_000) expect(w.resetsAt).toBe(iso("2026-11-03T02:32:00+08:00")) @@ -744,3 +745,41 @@ test("a busy account's ledger is read in time linear in its rows", () => { rmSync(root, { recursive: true, force: true }) } }) + +// plugins#641 (XiaoFeiyu9): a paid China account, 937 answered calls and no +// 429 on deepseek-v4.1-flash, read as 99% of the 190M soft cap while its +// credits had 2,457 left. The ledger's sum is an estimate: it is shown, set +// aside, and never stops the model; only the vendor's refusal does, and +// magpie's gateway rests the model on it until the reset it names. Both +// builds, as both read the sum the same way. +test("a window the vendor hasn't refused is set aside, on both builds (plugins#641)", () => { + const now = at("2026-11-02T14:00:00+08:00") + // the reporter's sum, 187,701,367, in a window opened at 11:08:53 + const cn = _internal.freeWindowOf( + [cnRow("2026-11-02T11:08:53+08:00", { in: 7_701_367, cache: 179_000_000, out: 1_000_000 }), cnRow("2026-11-02T12:00:00+08:00", { in: 0, cache: 0, out: 0, status: 499 })], + "me@example.com", + now, + _internal.FREE_WINDOW_CN, + ) + expect(cn.amount).toBe(187_701_367) + expect(cn.limit).toBe(2e8) + expect(cn.display).toBe("187.7M / 200M") + expect(cn.aside).toBe(true) + expect(cn.refused).toBeUndefined() + // a sum over the cap is still only the sum + const over = _internal.freeWindowOf([row("2026-11-02T11:00:00+08:00", { in: 120_000_000 })], "me@example.com", now) + expect(over.used).toBe(100) + expect(over.aside).toBe(true) + // the same rows with the vendor's refusal stop the model, on both builds + const cnDenied = _internal.freeWindowOf( + [cnRow("2026-11-02T11:08:53+08:00", { in: 200_000_000 }), cnRefusal("2026-11-03 11:08:53")], + "me@example.com", + now, + _internal.FREE_WINDOW_CN, + ) + expect(cnDenied.refused).toBe(true) + expect(cnDenied.aside).toBeUndefined() + const aiDenied = _internal.freeWindowOf([row("2026-11-02T11:00:00+08:00", { in: 100_000_000 }), refusal("2026-11-03 11:00:00")], "me@example.com", now) + expect(aiDenied.refused).toBe(true) + expect(aiDenied.aside).toBeUndefined() +}) diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 7716b42..f7b8505 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -2276,6 +2276,17 @@ const compact = (n) => (Number.isInteger(n) ? String(n) : n.toFixed(2).replace(/ // full whatever the sum says. That is the one exact signal there is, // and routing must not pass over it either. // +// So the sum is an estimate, and it is shown, not routed on: its card is set +// aside (aside: true), which magpie draws beside the account and never counts +// as its allowance. Only a refusal's card stops the model. The estimate used +// to stop it as well, and it read a paid China account at 99% of a cap it had +// never been refused at, with 2,457 credits left (XiaoFeiyu9, plugins#641): +// the cap is two accounts' measurement, the ledger is one machine's, and +// neither is the vendor's word for this account. Nothing is lost by waiting +// for that word: magpie's gateway reads the refusal's reset ("将在 … 重置", +// "reset at …") and its "switch to the other models", and rests this model +// alone, on this account alone, until the time the vendor named. +// // The ledger is magpie's own, in magpie's config folder (appdir.Config()): // the folder the plugin host runs in, which is the portable data folder // when magpie runs portable, $XDG_CONFIG_HOME/magpie when that is set, and @@ -2287,12 +2298,12 @@ const compact = (n) => (Number.isInteger(n) ? String(n) : n.toFixed(2).replace(/ // caps: the same phone number can be signed in to both, and their ledgers // stay apart — one build's spending must not fill the other's window. // -// The China cap is a soft one. Measured at about 2e8 (two accounts refusing -// at 200.39M and 200.49M), the card stops the model at 1.9e8: 5% of headroom -// for the account's spending on other machines (which this ledger cannot -// see), so routing rests the model before the vendor's refusal does. A -// refusal in force still reads the window full whatever the sum says. -const FREE_WINDOW_CN = { provider: "workbuddy", cap: 1.9e8, short: "190M" } +// The China cap is the measured one, 2e8 (two accounts refused at 200.39M +// and 200.49M). It was a soft 1.9e8 in 0.2.12 and 0.2.13, so that routing +// rested the model before the vendor refused it; since the sum is set aside +// (freeWindowOf's card), the cap only scales what the card says, and the +// headroom bought nothing but a model stopped 10M tokens early (plugins#641). +const FREE_WINDOW_CN = { provider: "workbuddy", cap: 2e8, short: "200M" } const FREE_WINDOW_AI = { provider: "workbuddy-ai", cap: 1e8, short: "100M" } const FREE_WINDOW_SPAN = 24 * 3600 * 1000 // The lead: how far back of an edge the read goes on. A chain read from a row @@ -2438,7 +2449,9 @@ function freeWindowOf(rows, account, now = Date.now(), win = FREE_WINDOW_AI) { resetsAt: new Date(resetsAt).toISOString(), span: FREE_WINDOW_SPAN / 1000, models: FREE_WINDOW_MODELS.slice(), - ...(refused ? { refused: true } : {}), + // the ledger's sum is an estimate, shown and set aside; the vendor's + // refusal is the one that stops the model (see the window above) + ...(refused ? { refused: true } : { aside: true }), }) // A refusal still in force is exact: the account is spending this window @@ -3788,9 +3801,8 @@ function makePlugin(site) { // the free model's own window, which the credit meter above does // not show: what actually refuses a chat long before 160 credits // are spent. Both builds: WorkBuddy AI at 1e8 (ten days of - // refusals), the China build's refusals measured at about 2e8 on - // 2026-11-02, its card stopping at the 1.9e8 soft cap (see the - // window definitions above) + // refusals), the China build's at about 2e8 (2026-11-02). Set + // aside until the vendor refuses (see the window definitions above) { const account = a.accountId || a.user || a.uid || "" const win = site.ai ? FREE_WINDOW_AI : FREE_WINDOW_CN diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index a08af14..7682b92 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.2.13", + "version": "0.2.14", "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 f05d3ed..32df388 100644 --- a/packages/workbuddy/usage.test.mjs +++ b/packages/workbuddy/usage.test.mjs @@ -612,7 +612,7 @@ test("a model list that can't be read leaves the credits counting every model", // #299: the free model's window, read from magpie's usage ledger, reaches // the WorkBuddy AI card beside Credits. Both builds carry it now: the China // build refused at about 2e8 on 2026-11-02, and its card counts its own -// ledger rows against the 1.9e8 soft cap — one phone number signed in to +// ledger rows against 2e8 — one phone number signed in to // both must see its spending twice apart, not summed (see freeWindowOf) test("a WorkBuddy AI card carries the free model's window from the ledger", async () => { const dir = join(home, ".config", "magpie") @@ -632,13 +632,14 @@ test("a WorkBuddy AI card carries the free model's window from the ledger", asyn const auth = { type: "oauth", access: "ai-access", expires: later(), uid: "ai3", accountId: "me@example.com", domain: "www.codebuddy.ai" } const out = await usageOf(WorkBuddyAIAuthPlugin, auth) expect(out.windows.map((w) => w.name)).toEqual(["Credits", "Free model tokens"]) - expect(out.windows[1]).toMatchObject({ amount: 30_000_000, limit: 1e8, unit: "tokens" }) + // the ledger's sum is set aside: shown, never the allowance (plugins#641) + expect(out.windows[1]).toMatchObject({ amount: 30_000_000, limit: 1e8, unit: "tokens", aside: true }) serve({ "/billing/meter/get-user-resource-summary": () => ok({ IsPaidUser: false, Packages: [{ CycleTotalCapacity: "500", CycleUsedCapacity: "0" }] }) }) const cn = await usageOf(WorkBuddyAuthPlugin, { ...auth, domain: "" }) expect(cn.windows.map((w) => w.name)).toEqual(["Credits", "Free model tokens"]) - // the China card counts the workbuddy rows alone, against the soft cap - expect(cn.windows[1]).toMatchObject({ amount: 90_000_000, limit: 1.9e8, unit: "tokens" }) + // the China card counts the workbuddy rows alone, against 2e8 + expect(cn.windows[1]).toMatchObject({ amount: 90_000_000, limit: 2e8, unit: "tokens", aside: true }) } finally { rmSync(ledger, { force: true }) }