Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/shared/src/usageLimits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,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: "same@example.com", 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"),
Expand Down
11 changes: 10 additions & 1 deletion packages/shared/src/usageLimits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl
const 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);
Expand All @@ -190,6 +191,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);
Expand All @@ -208,7 +217,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl
accounts.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),
Expand Down
Loading