workbuddy 0.1.23: credits used up leave the free models counted out, so magpie doesn't put the account last for deepseek-v4.1-flash (#138, hheei)

WorkBuddy serves a model at x0.00 (deepseek-v4.1-flash) with the account's
credits used up: hheei's account at 560 / 560 was still served it. The
usage read gave one Credits window over every model, so at 100% magpie's
routing ranked the account spent for its free models too: put behind every
other account, and moved off by conversation affinity.

The Credits window (personal and enterprise) now carries notModels: the
ids the account's live list (/v3/config) prices at x0.00. A model free only
some hours (a night promotion) still counts, as the list never says it is
free. A list that can't be read leaves the window as before. magpie reads
notModels as it does for Devin's and Cursor's windows (Limit.applies), so
For(free model) no longer sees the full window, and a 14018 on a paid model
rests only that model (Pooled), as PaidBesideFree already did.

Run: HOME=<temp> bun test in packages/workbuddy: 92 pass. Broke it by hand
(no notModels set): the two new usage tests go red. bun scripts/check.mjs
workbuddy: ok. Not tried against a real account at 100% credits; the
behaviour that free models are still served there is the reporter's.
This commit is contained in:
yetone
2026-10-15 13:11:46 +08:00
parent b015ee515e
commit ec8ae09cc8
3 changed files with 65 additions and 2 deletions
+20 -1
View File
@@ -1209,6 +1209,24 @@ function enterpriseUsageOf(u) {
return w
}
// spareFree has the credits windows not count the models that cost none
// (notModels): a model at x0.00 draws no credits, and with the credits
// used up WorkBuddy still serves it (deepseek-v4.1-flash at 560 / 560,
// magpie-community/plugins#138, hheei). Counted, the window at 100% put
// the account last, and behind every other, for its free models too. A
// model free only some hours (a night promotion) is not free here, as in
// the list (priceOf). A list that can't be read leaves the windows as they
// are: every model counted, as before.
async function spareFree(site, a, windows) {
if (!windows.length) return windows
let free = []
try {
free = (await liveModels(site, a)).filter((m) => m.free).map((m) => m.id)
} catch {}
if (free.length) for (const w of windows) w.notModels = free
return windows
}
// The packages' codes, as WorkBuddy's own client lists them when it asks
// for each package's credits (its COMMODITY_CODES): the API refuses a
// detail list without them (10001 "PackageCodes required").
@@ -1577,7 +1595,7 @@ function makePlugin(site) {
try {
ent = enterpriseUsageOf(await meter(site, a, "/billing/meter/get-enterprise-user-usage"))
} catch {}
if (ent) return { plan: auth.plan || "Enterprise", windows: [ent], signIn: "kept" }
if (ent) return { plan: auth.plan || "Enterprise", windows: await spareFree(site, a, [ent]), signIn: "kept" }
}
// when the credits left run out, for routing to spend them first;
// a list that can't be read leaves it unknown, the card stands
@@ -1587,6 +1605,7 @@ function makePlugin(site) {
Object.assign(credits, runsOutOf(sum, await packagesOf(site, a)) ?? {})
} catch {}
}
await spareFree(site, a, out.windows)
return { ...out, signIn: "kept" }
} catch (e) {
return { error: e?.message ?? String(e), signIn: "kept" }
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-workbuddy-auth",
"version": "0.1.22",
"version": "0.1.23",
"description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)",
"type": "module",
"main": "./index.mjs",
+44
View File
@@ -378,3 +378,47 @@ test("enterpriseUsageOf: no limit, no window; times it can't read are left out",
expect(_internal.enterpriseUsageOf({ credit: "12.5", limitNum: "100", cycleResetTime: "soon" }))
.toEqual({ name: "Credits", used: 12.5, display: "12.5 / 100", amount: 12.5, limit: 100, unit: "credits" })
})
// A model at x0.00 draws no credits, and with the credits used up
// WorkBuddy still serves it: hheei's account at 560 / 560 was served
// deepseek-v4.1-flash (magpie-community/plugins#138). The credits window
// leaves those models out (notModels), so magpie doesn't put the account
// last for them. One free only at night (a daily promotion) still counts.
const FREE_CFG = () => ok({
agents: [{ name: "cli", models: ["deepseek-v4.1-flash", "deepseek-v4.1-flash-sg", "gpt-5.5", "hy4"] }],
models: [
{ id: "deepseek-v4.1-flash", name: "Deepseek-V4.1-Flash", credits: "x0.00" },
{ id: "deepseek-v4.1-flash-sg", name: "Deepseek-V4.1-Flash", credits: "x0.03" },
{ id: "gpt-5.5", credits: "x1.00" },
{ id: "hy4", credits: "x0.50" },
],
modelPromotions: [{ kind: "discount", modelIds: ["hy4"], discount: { discountedCredits: "0.00x" }, schedule: { daily: [{ start: "23:00", end: "08:00" }] } }],
})
const SPENT = { IsPaidUser: false, Packages: [{ CycleTotalCapacity: "560", CycleUsedCapacity: "560" }] }
test("credits used up don't count the free models (plugins#138, hheei)", async () => {
for (const plugin of [WorkBuddyAuthPlugin, WorkBuddyAIAuthPlugin]) {
serve({ "/billing/meter/get-user-resource-summary": () => ok(SPENT), "/v3/config": FREE_CFG })
const auth = { type: "oauth", access: "a", refresh: "r", expires: later(), uid: "u", domain: "" }
expect(await usageOf(plugin, auth)).toEqual({ signIn: "kept", plan: "Free", windows: [{
name: "Credits", used: 100, display: "560 / 560", amount: 560, limit: 560, unit: "credits", notModels: ["deepseek-v4.1-flash"] }] })
}
})
test("an enterprise account's credits don't count the free models either", async () => {
serve({
"/billing/meter/get-user-resource-summary": () => new Response(ENT_SUMMARY),
"/billing/meter/get-enterprise-user-usage": () => new Response(ENT_USAGE),
"/v3/config": FREE_CFG,
})
const auth = { type: "oauth", access: "ent-access", refresh: "r", expires: later(), uid: "u9", domain: "www.workbuddy.cn", enterpriseId: "ent-1" }
expect((await usageOf(WorkBuddyAuthPlugin, auth)).windows[0].notModels).toEqual(["deepseek-v4.1-flash"])
})
test("a model list that can't be read leaves the credits counting every model", async () => {
serve({ "/billing/meter/get-user-resource-summary": () => ok(SPENT), "/v3/config": () => new Response("busy", { status: 503 }) })
const auth = { type: "oauth", access: "a", refresh: "r", expires: later(), uid: "u", domain: "" }
const w = (await usageOf(WorkBuddyAuthPlugin, auth)).windows[0]
expect(w.used).toBe(100)
expect(w.notModels).toBeUndefined()
})