hayden4608 (Discord) and handong66 (#4791) have four Codex accounts. Three show 5 hours 100% left with 7 days 0% left, and one shows 5 hours 0% left with 7 days 22% left. The All accounts strip read "5 hours 75% left", but none of those three can spend its 5 hours until its week starts again. The right figure is 0%. mergeQuotas (quotaall.js) is the one place that sums windows across accounts. It feeds the Usage page's strip, the tray panel's strip and the Routing groups' line. The native menu-bar tray, the TUI, the CLI and /v1/magpie/quotas show one account at a time and are unchanged. The GUI's JSON doesn't carry Aside, Model or the matches, so the call is made in Go. provider.WithHeldWindows marks a window heldBy another window of the same account when that other window: - stops the account for every model, by routing's rule: not aside, no model, no matches, used >= 100, reset not passed, and not Codex credits that go on; and - starts again after this one (by reset times, else by span). So 7 days used up holds 5 hours, but 5 hours used up doesn't hold a week with 22% left. /api/usage/quotas and its refresh apply it. mergeQuotas counts a held share window as 100 used and a held counted one as its limit, and the strip's tooltip says how many were counted that way (en/zh/zh-TW/ja/de). The account cards still show the vendor's own figure. Run on mote (macOS) at this head's parent tree: - go vet for darwin, linux (nogui) and windows: ok; gofmt -l: none. - go test -count=1 ./internal/provider ./internal/gui, before the quota_span leftOut line and an i18n comment fix. The provider package's only red was TestQuotaSpanCarriesEveryWindowField, which wanted heldBy named, and it is now named. internal/gui's only red was TestMainWindowCorners/macOS_26_SDK, which is #3949. - GUI, chromium and webkit, every language, 440px and 360px: quota-*, usage-* (all-accounts, merge, accounts, fold, refresh and more), panel-usage/quota/switch-accounts/balance-order, routing-quota-left, routing-group, tray-usage, i18n-keys, gui-de/ja/zh-tw, old-webkit: 704 of 707. The reds were i18n-keys (a quoted "All accounts" in window_held.go's comment, fixed here) and usage-fold's "17 subscriptions and 29 accounts" in both engines, which is #4790 and is red at origin/main too (see below). At the pushed head, rebased oneb3b9376e, on mote: gofmt none, vet ok. internal/provider ok. internal/gui is red only on TestMainWindowCorners (#3949). GUI usage-merge, usage-all-accounts, i18n-keys and usage-fold, both engines: 44 of 46. The 2 reds are usage-fold's 29 accounts, which fail the same at origin/maineb3b9376eon mote (noted on #4790). Broken by hand, each red, then restored (cmp): - Go, no HeldBy set: both TestHeldWindows tests red (a, b, c not held). - Go, outlasts always true: red on d and on "a week that starts again first". - JS, held forced false: usage-merge plus all 5 languages of usage-all-accounts red in chromium, showing the reporter's "5 hours · 75% left". Not tried: real accounts. The issue names no provider; Codex is assumed from the cards' 5 hours / 7 days. Not done: marking a card as held by its week, the reporter's optional second ask. Not written: a handler-level test that /api/usage/quotas carries heldBy. Not run on Linux or Windows.
145 lines
7.3 KiB
JavaScript
145 lines
7.3 KiB
JavaScript
// A provider's accounts together (CryptoSarom on X: how much of a model is
|
|
// left matters more than which account holds it). mergeQuotas, the shipped
|
|
// function, puts together only what reads alike: counted windows of one
|
|
// name and unit add their amounts and limits, wallets their balances, and a
|
|
// window said only as a share is the mean of accounts on one plan. A share
|
|
// is never added to a count, two units never meet, two plans' shares
|
|
// never meet, and an account whose reading failed is unknown, not 0 or
|
|
// 100. A window an earlier reading stands in for, past its reset, is left
|
|
// out and counted. Needs no browser.
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const vm = require("node:vm");
|
|
const { test } = require("node:test");
|
|
|
|
const source = fs.readFileSync(process.env.MAGPIE_APP_JS || path.resolve(__dirname, "../assets/app.js"), "utf8").replace(/\r\n/g, "\n");
|
|
function section(start, end) {
|
|
const a = source.indexOf(start), b = source.indexOf(end, a);
|
|
assert(a >= 0 && b > a, `missing ${start}`);
|
|
return source.slice(a, b);
|
|
}
|
|
// quotaall.js, which app.js loads once a provider has two accounts
|
|
const merging = fs.readFileSync(path.resolve(__dirname, "../assets/quotaall.js"), "utf8").replace(/\r\n/g, "\n");
|
|
const ctx = vm.createContext({});
|
|
ctx.window = ctx;
|
|
vm.runInContext(`
|
|
${section("function to100(", "\n")}
|
|
${merging}
|
|
`, ctx);
|
|
const merge = (subs, now) => JSON.parse(JSON.stringify(ctx.mergeQuotas(JSON.parse(JSON.stringify(subs)), now)));
|
|
const H = 3600e3, now = Date.parse("2026-11-01T12:00:00Z");
|
|
const iso = (ms) => new Date(ms).toISOString();
|
|
const acct = (user, windows, extra) => ({ provider: "workbuddy-ai", name: "WorkBuddy AI", user, plan: "Pro", windows, ...extra });
|
|
const by = (r, name) => r.windows.filter((w) => w.name === name);
|
|
|
|
test("counted windows of one unit add up; the share is of the sum", () => {
|
|
const r = merge([
|
|
acct("a", [{ name: "Credits", amount: 100, limit: 500, unit: "credits", used: 20 }]),
|
|
acct("b", [{ name: "Credits", amount: 250, limit: 500, unit: "credits", used: 50 }]),
|
|
acct("c", [{ name: "Credits", amount: 5, limit: 500, unit: "credits", used: 1 }]),
|
|
], now);
|
|
assert.equal(r.accounts, 3);
|
|
assert.equal(r.unknown, 0);
|
|
const [w] = by(r, "Credits");
|
|
assert.equal(w.amount, 355);
|
|
assert.equal(w.limit, 1500);
|
|
assert.ok(Math.abs(w.used - (100 * 355) / 1500) < 1e-9);
|
|
assert.equal(w.accounts, 3);
|
|
assert.equal(w.how, "c");
|
|
});
|
|
|
|
test("a share is never added to a count, nor one unit to another", () => {
|
|
const r = merge([
|
|
acct("a", [{ name: "Monthly", amount: 100, limit: 500, unit: "credits" }, { name: "Tokens", amount: 1, limit: 10, unit: "M tokens" }]),
|
|
acct("b", [{ name: "Monthly", used: 40 }, { name: "Tokens", amount: 3, limit: 10, unit: "requests" }]),
|
|
], now);
|
|
// each kind has one account only: nothing goes together
|
|
assert.deepEqual(r.windows, []);
|
|
});
|
|
|
|
test("an account whose reading failed is unknown, left out of the figure", () => {
|
|
const r = merge([
|
|
acct("a", [{ name: "5 hours", used: 20, resetsAt: iso(now + H) }]),
|
|
acct("b", [{ name: "5 hours", used: 60, resetsAt: iso(now + 2 * H) }]),
|
|
acct("c", [], { error: "401 Unauthorized" }),
|
|
], now);
|
|
assert.equal(r.accounts, 2);
|
|
assert.equal(r.unknown, 1);
|
|
const [w] = by(r, "5 hours");
|
|
assert.equal(w.used, 40, "the mean of the two read, the failed one neither 0 nor 100");
|
|
assert.equal(w.accounts, 2);
|
|
assert.equal(w.firstReset, iso(now + H));
|
|
assert.equal(w.resetsAt, undefined, "no single reset is claimed for the whole");
|
|
});
|
|
|
|
test("shares are averaged within a plan only", () => {
|
|
const r = merge([
|
|
acct("a", [{ name: "5 hours", used: 10 }], { plan: "Plus" }),
|
|
acct("b", [{ name: "5 hours", used: 30 }], { plan: "Plus" }),
|
|
acct("c", [{ name: "5 hours", used: 90 }], { plan: "Pro" }),
|
|
], now);
|
|
const ws = by(r, "5 hours");
|
|
assert.equal(ws.length, 1, "the Pro account alone is not merged into the Plus mean");
|
|
assert.equal(ws[0].used, 20);
|
|
assert.equal(ws[0].plan, "Plus", "named with its plan when the accounts have several");
|
|
const one = merge([acct("a", [{ name: "5 hours", used: 10 }], { plan: "Plus" }), acct("b", [{ name: "5 hours", used: 30 }], { plan: "Pro" })], now);
|
|
assert.deepEqual(one.windows, []);
|
|
});
|
|
|
|
test("a stale window past its reset is left out and counted", () => {
|
|
const r = merge([
|
|
acct("a", [{ name: "Daily", used: 10, resetsAt: iso(now + H) }]),
|
|
acct("b", [{ name: "Daily", used: 50, resetsAt: iso(now + H) }]),
|
|
acct("c", [{ name: "Daily", used: 99, resetsAt: iso(now - H) }], { asOf: iso(now - 3 * H) }),
|
|
], now);
|
|
const [w] = by(r, "Daily");
|
|
assert.equal(w.used, 30);
|
|
assert.equal(w.unknown, 1);
|
|
});
|
|
|
|
test("balances add up; unlimited only when every account is; text is left out", () => {
|
|
const r = merge([
|
|
acct("a", [{ name: "Wallet", balance: 12.5, unit: "USD" }, { name: "Chat", unlimited: true }, { name: "Overages", text: true, used: 100 }]),
|
|
acct("b", [{ name: "Wallet", balance: 7.5, unit: "USD" }, { name: "Chat", unlimited: true }, { name: "Overages", text: true, used: 100 }]),
|
|
acct("c", [{ name: "Wallet", balance: 1, unit: "credits" }, { name: "Chat", used: 30 }]),
|
|
], now);
|
|
assert.deepEqual(by(r, "Wallet").map((w) => [w.balance, w.unit]), [[20, "USD"]]);
|
|
assert.deepEqual(by(r, "Chat").filter((w) => w.unlimited), [], "one account has a limit");
|
|
assert.deepEqual(by(r, "Overages"), []);
|
|
});
|
|
|
|
test("windows keep their family and pool, apart by them", () => {
|
|
const r = merge([
|
|
acct("a", [{ name: "5 hours", pool: "Gemini", used: 10 }, { name: "5 hours", pool: "Claude & GPT", used: 70 }]),
|
|
acct("b", [{ name: "5 hours", pool: "Gemini", used: 30 }, { name: "5 hours", pool: "Claude & GPT", used: 90 }]),
|
|
], now);
|
|
assert.deepEqual(r.windows.map((w) => [w.pool, w.used]), [["Gemini", 20], ["Claude & GPT", 80]]);
|
|
});
|
|
|
|
// #4791 (hayden4608 on Discord, handong66): three accounts with 100% of 5
|
|
// hours left and 0% of 7 days, one with 0% of 5 hours and 22% of 7 days.
|
|
// The three can't spend their 5 hours before their week starts again: the
|
|
// server marks those windows heldBy "7 days", and the strip read 75% of 5
|
|
// hours left in all. Held, they count as used up: 0% left; the week's
|
|
// figure is the mean as before, 5.5% left.
|
|
test("a window held by a longer one used up counts as nothing left", () => {
|
|
const codex = (user, five, week, fiveHeld) => ({ provider: "codex", name: "Codex", user, plan: "plus", windows: [
|
|
{ name: "5 hours", used: five, resetsAt: iso(now + 5 * H), ...(fiveHeld ? { heldBy: "7 days" } : {}) },
|
|
{ name: "7 days", used: week, resetsAt: iso(now + 72 * H) },
|
|
] });
|
|
const r = merge([codex("a", 0, 100, true), codex("b", 0, 100, true), codex("c", 0, 100, true), codex("d", 100, 78, false)], now);
|
|
const [five] = by(r, "5 hours"), [week] = by(r, "7 days");
|
|
assert.equal(five.used, 100, "no 5 hours to spend in all, not 75% left");
|
|
assert.equal(five.held, 3);
|
|
assert.equal(five.accounts, 4);
|
|
assert.equal(week.used, 94.5, "5.5% of the week left, as before");
|
|
assert.equal(week.held, 0);
|
|
// a counted window held so adds its whole as used
|
|
const c = merge([
|
|
acct("a", [{ name: "Credits", amount: 0, limit: 500, unit: "credits", heldBy: "Monthly" }]),
|
|
acct("b", [{ name: "Credits", amount: 100, limit: 500, unit: "credits" }]),
|
|
], now);
|
|
assert.deepEqual(by(c, "Credits").map((w) => [w.amount, w.limit, w.held]), [[600, 1000, 1]]);
|
|
});
|