diff --git a/packages/workbuddy/README.md b/packages/workbuddy/README.md index b3bc1c4..19c2636 100644 --- a/packages/workbuddy/README.md +++ b/packages/workbuddy/README.md @@ -447,14 +447,30 @@ What is measured, and what is easy to read wrongly: rather than +08:00 guessed. A window that has lapsed reports nothing rather than 0%: nothing is -exhausted, and the next request opens a fresh one. The China build -(`copilot.tencent.com`) carries the same window: two Free-plan accounts +exhausted, and the next request opens a fresh one. + +### The China build's windows (measured, shown as estimates) + +The China build (`copilot.tencent.com`) carries the same kind of window, +measured one model at a time. `deepseek-v4.1-flash`: 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 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. +about **2e8** — and its refusals name the reset in Chinese +(`将在 … UTC+8 重置`), which reads as an edge the same way. + +Unlike the AI build's, the China model is **charged in credits** (`x0.11`) +— the token window and the credits meter are two limits on one model — so +the card is not a "free model" card. It is named after its model and says +its cap is an estimate: **`deepseek-v4.1-flash tokens (est.)`**, with +`~200M` in its text (`151.5M / ~200M`). The estimate is this machine's ledger +against a cap measured on two accounts; the vendor reports neither, and the +first refusal of a window pins the card full at the reset the vendor names. + +The measured windows live in one table (`CN_ESTIMATED_WINDOWS`), one entry +per model. Only measured models have a card — a guessed cap would read wrong +every day. Add an entry when another model's window is measured; remove one +when the vendor stops applying it. Because the card is set aside (shown, not +routed on), a stale cap costs a wrong percentage, never a wrongly rested +model: the refusal is what stops a model, on both builds. ## 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 bf5a7c6..c1b7ddc 100644 --- a/packages/workbuddy/freewindow.test.mjs +++ b/packages/workbuddy/freewindow.test.mjs @@ -127,13 +127,15 @@ const cnRefusal = (reset, account = "me@example.com") => 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) + const w = _internal.freeWindowOf(rows, "me@example.com", at("2026-11-02T12:00:00+08:00"), _internal.CN_ESTIMATED_WINDOWS[0]) expect(w.amount).toBe(189_500_200) expect(w.limit).toBe(2e8) expect(w.used).toBeCloseTo(94.75, 3) - expect(w.display).toBe("189.5M / 200M") + expect(w.display).toBe("189.5M / ~200M") expect(w.resetsAt).toBe(iso("2026-11-03T02:32:00+08:00")) - expect(w.unit).toBe("tokens") + // the card says the cap is an estimate, and names its own model + expect(w.name).toBe("deepseek-v4.1-flash tokens (est.)") + expect(w.unit).toBe("") expect(w.models).toEqual(["deepseek-v4.1-flash", "deepseek-v4.1-flash-sg"]) }) @@ -149,7 +151,7 @@ test("the two builds' rows stay apart over one account", () => { const ai = _internal.freeWindowOf(rows, "me@example.com", at("2026-11-02T12:00:00+08:00")) expect(ai.amount).toBe(40_000_000) 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) + const cn = _internal.freeWindowOf(rows, "me@example.com", at("2026-11-02T12:00:00+08:00"), _internal.CN_ESTIMATED_WINDOWS[0]) expect(cn.amount).toBe(98_000_000) expect(cn.limit).toBe(2e8) }) @@ -171,13 +173,13 @@ test("a China account refused under the cap reads full", () => { cnRow("2026-11-02T02:32:00+08:00", { in: 80_000_000 }), 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) + const w = _internal.freeWindowOf(rows, "me@example.com", at("2026-11-02T12:00:00+08:00"), _internal.CN_ESTIMATED_WINDOWS[0]) 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("200.0M / 200M") + expect(w.display).toBe("200.0M / ~200M") }) // readLedgerTail stops the chain on both languages' edges: a Chinese refusal @@ -192,11 +194,11 @@ test("the China read stops on a Chinese refusal's edge", () => { const fresh = cnRow("2026-11-02T02:32:00+08:00", { in: 300_000 }) const root = writeLedger([JSON.stringify(spent), JSON.stringify(denied), JSON.stringify(fresh)]) try { - const rows = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"), _internal.FREE_WINDOW_CN) + const rows = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"), _internal.CN_ESTIMATED_WINDOWS[0]) // 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 200M, resetting 24h later - const w = _internal.freeWindowOf(rows, "me@example.com", now, _internal.FREE_WINDOW_CN) + const w = _internal.freeWindowOf(rows, "me@example.com", now, _internal.CN_ESTIMATED_WINDOWS[0]) expect(w.amount).toBe(300_000) expect(w.resetsAt).toBe(iso("2026-11-03T02:32:00+08:00")) expect(rows.every((r) => r.provider === "workbuddy")).toBe(true) @@ -215,7 +217,7 @@ test("the parse cache keys the build", () => { const lines = [cnRow("2026-11-02T02:00:00+08:00", { in: 90_000_000 }), row("2026-11-02T02:10:00+08:00", { in: 40_000_000 })].map((r) => JSON.stringify(r)) const root = writeLedger(lines) try { - const cn = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"), _internal.FREE_WINDOW_CN) + const cn = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome"), _internal.CN_ESTIMATED_WINDOWS[0]) expect(cn.map((r) => r.provider)).toEqual(["workbuddy"]) const ai = _internal.freeWindowRows("me@example.com", now, root, {}, join(root, "nohome")) expect(ai.map((r) => r.provider)).toEqual(["workbuddy-ai"]) @@ -737,7 +739,7 @@ test("a busy account's ledger is read in time linear in its rows", () => { Date.parse = (s) => (calls++, parse(s)) try { const path = join(root, "usage.jsonl") - const got = _internal.readLedgerTail(path, statSync(path), now, undefined, "me@example.com", _internal.FREE_WINDOW_CN) + const got = _internal.readLedgerTail(path, statSync(path), now, undefined, "me@example.com", _internal.CN_ESTIMATED_WINDOWS[0]) expect(got.rows.length).toBe(6000) expect(calls).toBeLessThan(lines.length * 4) } finally { @@ -759,11 +761,11 @@ test("a window the vendor hasn't refused is set aside, on both builds (plugins#6 [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, + _internal.CN_ESTIMATED_WINDOWS[0], ) expect(cn.amount).toBe(187_701_367) expect(cn.limit).toBe(2e8) - expect(cn.display).toBe("187.7M / 200M") + 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 @@ -775,7 +777,7 @@ test("a window the vendor hasn't refused is set aside, on both builds (plugins#6 [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, + _internal.CN_ESTIMATED_WINDOWS[0], ) expect(cnDenied.refused).toBe(true) expect(cnDenied.aside).toBeUndefined() diff --git a/packages/workbuddy/index.mjs b/packages/workbuddy/index.mjs index 4d8c1f7..329560d 100644 --- a/packages/workbuddy/index.mjs +++ b/packages/workbuddy/index.mjs @@ -2212,7 +2212,7 @@ const compact = (n) => (Number.isInteger(n) ? String(n) : n.toFixed(2).replace(/ // The window WorkBuddy rate limits deepseek-v4.1-flash by, which its credit // meter does not show: WorkBuddy AI measured at about 1e8 over ten days, the -// China build at about 2e8 on 2026-11-02 (see FREE_WINDOW_CN). +// China build at about 2e8 on 2026-11-02 (see CN_ESTIMATED_WINDOWS). // // A free account's usage() reports only the credits window, so a plan with // 160 credits untouched reads 0% while chats to deepseek-v4.1-flash are @@ -2293,18 +2293,46 @@ const compact = (n) => (Number.isInteger(n) ? String(n) : n.toFixed(2).replace(/ // ~/.config/magpie otherwise. No endpoint reports the window; a ledger that // cannot be read yields no window rather than a made-up one. -// A window: the build's ledger rows it counts (provider), the cap it reports -// against, and how deep its display rounds. Two builds, two windows, two -// caps: the same phone number can be signed in to both, and their ledgers +// A window: the build's ledger rows it counts (provider and model), the cap +// it reports against, and how its card reads. Two builds, two sets of +// windows: 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 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" } +// The China caps are measured on this machine from the vendor's own +// refusals, never the vendor's word, and each is an estimate: 2e8 for +// deepseek-v4.1-flash (two accounts refused at 200.39M and 200.49M on +// 2026-11-02), with the card saying so (`~` and "(est.)"). The card is +// shown only — set aside, plugins#641 — so a cap that goes stale costs a +// wrong percentage, never a wrongly rested model; the first refusal of a +// window pins the card full at the reset the vendor names. +// +// One entry per model, and only models that have been measured: a guessed +// cap would read wrong every day. Add an entry when another model's window +// is measured; remove one when the vendor stops applying it. +const CN_ESTIMATED_WINDOWS = [ + { + provider: "workbuddy", + model: "deepseek-v4.1-flash", + aliases: ["deepseek-v4.1-flash-sg"], + cap: 2e8, + name: "deepseek-v4.1-flash tokens (est.)", + short: "~200M", + // no unit: the name already says tokens, and magpie's narrow account + // cards cut a unit short ("tok…") beside the nine-digit numbers + unit: "", + }, +] +// WorkBuddy AI's window keeps the name it shipped with: there the model is +// served at x0.00, so "Free model tokens" is what it is. +const FREE_WINDOW_AI = { + provider: "workbuddy-ai", + model: "deepseek-v4.1-flash", + aliases: ["deepseek-v4.1-flash-sg"], + cap: 1e8, + name: "Free model tokens", + short: "100M", + unit: "tokens", +} 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 // that opens a window needs the window the span makes of the rows above it, @@ -2317,10 +2345,9 @@ const FREE_WINDOW_LEAD = 86400 * 1000 // earlier row began at most 24h before it, and so ended 24h after it at the // latest. This is one of the two edges the ledger itself spells out. const FREE_WINDOW_GAP = FREE_WINDOW_SPAN -// the model the window is counted for; -sg shares it -const FREE_WINDOW_MODEL = "deepseek-v4.1-flash" -// both ids, so magpie's exact match finds the -sg one too -const FREE_WINDOW_MODELS = ["deepseek-v4.1-flash", "deepseek-v4.1-flash-sg"] +// the model a window is counted for is its spec's `model`; `aliases` are the +// ids that share the window (-sg is the same model under its paid id), and +// both are named in the card's models so magpie's exact match finds them // windowOffset is the UTC offset a refusal spells out, in minutes, from // "UTC+8", "UTC+08:00", "UTC-5" or "GMT+8". null when it spells out none, @@ -2367,7 +2394,7 @@ function freeWindowOf(rows, account, now = Date.now(), win = FREE_WINDOW_AI) { r?.provider === win.provider && r.providerAccount === account && typeof r.model === "string" && - r.model.startsWith(FREE_WINDOW_MODEL) && + r.model.startsWith(win.model) && r.t, ) if (!mine.length) return null @@ -2440,15 +2467,17 @@ function freeWindowOf(rows, account, now = Date.now(), win = FREE_WINDOW_AI) { if (now >= end) return null // lapsed: nothing is exhausted, a new window opens on the next request const card = (amount, resetsAt, refused) => ({ - name: "Free model tokens", + name: win.name, used: Math.min(100, (100 * amount) / win.cap), amount, limit: win.cap, - unit: "tokens", + unit: win.unit, display: `${tokensShort(amount)} / ${win.short}`, resetsAt: new Date(resetsAt).toISOString(), span: FREE_WINDOW_SPAN / 1000, - models: FREE_WINDOW_MODELS.slice(), + // the window's own model and the ids that share it, so magpie's exact + // match finds every id it limits + models: [win.model, ...win.aliases], // 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 }), @@ -2550,10 +2579,11 @@ function freeWindowRows(account, now = Date.now(), cwd = process.cwd(), env = pr // way this ledger is written; one rewritten in place (a rotation or a // compaction) moves mtime even when its size lands the same. const file = path + "|" + st.mtimeMs + ":" + st.size - // the key carries the account and the build: one account's rows are its - // own, and the two builds' chains over one ledger must not answer for - // each other (the same phone number can be signed in to both) - const key = file + "|" + win.provider + "|" + account + // the key carries the account, the build and the model: one account's + // rows are its own, the two builds' chains over one ledger must not + // answer for each other (the same phone number can be signed in to both), + // and two windows of one build count different models + const key = file + "|" + win.provider + "|" + win.model + "|" + account if (ledgerParsed?.key === key) return ledgerParsed.rows let got try { @@ -2617,15 +2647,15 @@ function rowAccount(line) { } // ofAccount is whether a row belongs to the chain read for account: this -// account's own row of the build's free model, either id. A refusal of -// another account's, of the other build, or of a paid model says nothing -// about where this chain opens. +// account's own row of the window's model, either id. A refusal of another +// account's, of the other build, or of another model says nothing about +// where this chain opens. function ofAccount(row, account, win = FREE_WINDOW_AI) { return ( row?.provider === win.provider && row.providerAccount === account && typeof row.model === "string" && - row.model.startsWith(FREE_WINDOW_MODEL) + row.model.startsWith(win.model) ) } @@ -3883,17 +3913,21 @@ function makePlugin(site) { } catch {} } await spareFree(site, a, out.windows) - // 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 at about 2e8 (2026-11-02). Set - // aside until the vendor refuses (see the window definitions above) + // each measured model's own window, which the credit meter above + // does not show: what actually refuses a chat long before the + // credits are spent. WorkBuddy AI has one (deepseek, 1e8, ten days + // of refusals); the China build's are measured one model at a time + // (deepseek at about 2e8, 2026-11-02), each its own card. Shown + // only, set aside: the vendor's refusal is what stops a model + // (see the window definitions above) { const account = a.accountId || a.user || a.uid || "" - const win = site.ai ? FREE_WINDOW_AI : FREE_WINDOW_CN - const rows = freeWindowRows(account, Date.now(), process.cwd(), process.env, homedir(), win) - const w = rows && freeWindowOf(rows, account, Date.now(), win) - if (w) out.windows.push(w) + const wins = site.ai ? [FREE_WINDOW_AI] : CN_ESTIMATED_WINDOWS + for (const win of wins) { + const rows = freeWindowRows(account, Date.now(), process.cwd(), process.env, homedir(), win) + const w = rows && freeWindowOf(rows, account, Date.now(), win) + if (w) out.windows.push(w) + } } return { ...out, signIn: said } } catch (e) { @@ -4037,4 +4071,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy) export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"]) // for tests -export const _internal = { planCodes, listedExtra, idFor, OFF_TAKEN, replyAfterToolsOn, withDefaultEffort, SMALL_BUDGET, defaultEffortOf, gzipped, FREE_ONLY, PRICE_TTL, keyAccount, remoteConfig, cycleOf, typeUnions, systemAsUser, systemKey, DEFAULT_SYSTEM, EDGE_READS, fitShape, replyAfterTools, AFTER_TOOLS_REFUSED, enterpriseUsageOf, accountName, notDesktop, textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, asReasoning, normalizeToolChoice, fitTools, fitted, renamed, unsaid, FILTER_SAYS, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, REFUSED_DOC, commaPaused, commaPausedOn, opened, freeCredits, creditsOf, priceOf, fresh, SITES, renewing, renewed, SESSION, attend, scopeOf, turnKey, conversationKey, signatureOf, isMessages, withoutUnsigned, withoutSignedThinking, prepared, answeredBefore, nativeOf, modelOf, configModels, freeWindowOf, tokensShort, freeWindowRows, freeWindowLedger, readLedgerTail, pinned, pinning, rowTime, rowAccount, ofAccount, windowOffset, windowReset, FREE_WINDOW_MODELS, FREE_WINDOW_CN, FREE_WINDOW_AI, FREE_WINDOW_SPAN, FREE_WINDOW_GAP, FREE_WINDOW_LEAD, checkinOf, API_MESSAGES: NPM_MESSAGES, API_CHAT: NPM } +export const _internal = { planCodes, listedExtra, idFor, OFF_TAKEN, replyAfterToolsOn, withDefaultEffort, SMALL_BUDGET, defaultEffortOf, gzipped, FREE_ONLY, PRICE_TTL, keyAccount, remoteConfig, cycleOf, typeUnions, systemAsUser, systemKey, DEFAULT_SYSTEM, EDGE_READS, fitShape, replyAfterTools, AFTER_TOOLS_REFUSED, enterpriseUsageOf, accountName, notDesktop, textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, asReasoning, normalizeToolChoice, fitTools, fitted, renamed, unsaid, FILTER_SAYS, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, REFUSED_DOC, commaPaused, commaPausedOn, opened, freeCredits, creditsOf, priceOf, fresh, SITES, renewing, renewed, SESSION, attend, scopeOf, turnKey, conversationKey, signatureOf, isMessages, withoutUnsigned, withoutSignedThinking, prepared, answeredBefore, nativeOf, modelOf, configModels, freeWindowOf, tokensShort, freeWindowRows, freeWindowLedger, readLedgerTail, pinned, pinning, rowTime, rowAccount, ofAccount, windowOffset, windowReset, CN_ESTIMATED_WINDOWS, FREE_WINDOW_AI, FREE_WINDOW_SPAN, FREE_WINDOW_GAP, FREE_WINDOW_LEAD, checkinOf, API_MESSAGES: NPM_MESSAGES, API_CHAT: NPM } diff --git a/packages/workbuddy/package.json b/packages/workbuddy/package.json index ce48349..5ef4dd8 100644 --- a/packages/workbuddy/package.json +++ b/packages/workbuddy/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-workbuddy-auth", - "version": "0.2.18", + "version": "0.2.19", "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)", "type": "module", "main": "./opencode.mjs", diff --git a/packages/workbuddy/usage.test.mjs b/packages/workbuddy/usage.test.mjs index caaa262..0f6d2ba 100644 --- a/packages/workbuddy/usage.test.mjs +++ b/packages/workbuddy/usage.test.mjs @@ -736,9 +736,9 @@ test("a WorkBuddy AI card carries the free model's window from the ledger", asyn 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 2e8 - expect(cn.windows[1]).toMatchObject({ amount: 90_000_000, limit: 2e8, unit: "tokens", aside: true }) + // the China card is the model's own, and says its cap is an estimate + expect(cn.windows.map((w) => w.name)).toEqual(["Credits", "deepseek-v4.1-flash tokens (est.)"]) + expect(cn.windows[1]).toMatchObject({ amount: 90_000_000, limit: 2e8, unit: "", aside: true }) } finally { rmSync(ledger, { force: true }) }