TypeScript
fix(usage): use the freshest reported pooled plan (#25214)
maria9f52b7d419fe
diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.tsindex 5da7bc2cc02..23447ae7930 100644--- a/packages/shared/src/usageLimits.test.ts+++ b/packages/shared/src/usageLimits.test.ts@@ -236,6 +236,46 @@ describe("pools", () => {expect(accounts[0]?.limits.windows[0]?.usedPercent).toBe(55);});+ it("uses the freshest reported plan even when the newest quota has no plan", () => {+ const snapshot = (label: string | undefined, checkedAt: string, usedPercent: number) =>+ provider({+ driver: claude,+ auth: { status: "authenticated", email: "[email protected]", label },+ usageLimits: { checkedAt, windows: [{ ...window, usedPercent }] },+ });+ const stale = snapshot("Claude Team Subscription", checkedAt, 40);+ const fresh = snapshot("Claude Max Subscription", "2026-09-03T11:30:00.000Z", 7);+ const newest = snapshot(undefined, "2026-09-03T12:00:00.000Z", 5);+ for (const [reports, plan, usedPercent] of [+ [[stale, fresh], "Claude Max Subscription", 7],+ [[stale, newest], "Claude Team Subscription", 5],+ [[stale, fresh, newest], "Claude Max Subscription", 5],+ ] as const) {+ for (const order of reports.length === 2+ ? [reports, reports.toReversed()]+ : [+ [stale, fresh, newest],+ [stale, newest, fresh],+ [fresh, stale, newest],+ [fresh, newest, stale],+ [newest, stale, fresh],+ [newest, fresh, stale],+ ]) {+ const accounts = collectLimitAccounts(+ new Map(+ order.map((entry, index) => [+ EnvironmentId.make(`env-${index}`),+ { ...laptop, serverConfig: { providers: [entry] } },+ ]),+ ),+ );+ expect(accounts).toHaveLength(1);+ expect(accounts[0]?.plan).toBe(plan);+ expect(accounts[0]?.limits.windows[0]?.usedPercent).toBe(usedPercent);+ }+ }+ });+it("merges OpenCode Go limits from machines with the same API key", () => {const go = provider({driver: ProviderDriverKind.make("opencode"),diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.tsindex 3dca207a2ef..bf7dfea4e04 100644--- a/packages/shared/src/usageLimits.ts+++ b/packages/shared/src/usageLimits.ts@@ -255,6 +255,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonlconst accounts = new Map<string, LimitAccount>();const creditSources = new Map<string, LimitAccount>();const hubRedeems = new Map<string, LimitAccount>();+ const planSources = new Map<string, LimitAccount>();const merge = (key: string, next: LimitAccount) => {// Prefer hub redemption to clear its routing cooldown too; displayed balance still follows the freshest read.const previousHub = hubRedeems.get(key);@@ -273,6 +274,14 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl) {creditSources.set(key, next);}+ const previousPlan = planSources.get(key);+ if (+ next.plan &&+ (!previousPlan ||+ Date.parse(next.limits.checkedAt) > Date.parse(previousPlan.limits.checkedAt))+ ) {+ planSources.set(key, next);+ }const previous = accounts.get(key);if (!previous) {accounts.set(key, next);@@ -291,7 +300,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonlaccounts.set(key, {...previous,displayName: previous.displayName ?? next.displayName,- plan: previous.plan ?? next.plan,+ plan: planSources.get(key)?.plan ?? previous.plan ?? next.plan,accentColor: previous.accentColor ?? next.accentColor,environments,sourceLabel: environments.length > 0 ? null : (previous.sourceLabel ?? next.sourceLabel),