Skip to content

Commit 5b72385

Browse files
fix(usage): show empty sessions when weekly quota is exhausted (#20349)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 8211893 commit 5b72385

5 files changed

Lines changed: 185 additions & 21 deletions

File tree

‎apps/mobile/src/features/usage/UsageLimitsPooled.tsx‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ function accountName(account: LimitAccount) {
4343
function AccountSegment({
4444
remaining,
4545
color,
46-
pending,
46+
refillPercent,
4747
}: {
4848
readonly remaining: number;
4949
readonly color: string;
50-
readonly pending: boolean;
50+
readonly refillPercent: number;
5151
}) {
5252
const patternId = useId().replace(/:/g, "");
5353
const [size, setSize] = useState({ width: 0, height: 0 });
@@ -70,10 +70,10 @@ function AccountSegment({
7070
<Path d="M-1 1L1 -1M0 6L6 0M5 7L7 5" stroke={color} strokeWidth={1} opacity={0.22} />
7171
</Pattern>
7272
</Defs>
73-
{pending ? (
73+
{refillPercent > 0 ? (
7474
<Rect
75-
x={`${remaining}%`}
76-
width={`${100 - remaining}%`}
75+
x={`${100 - Math.min(100 - remaining, refillPercent)}%`}
76+
width={`${Math.min(100 - remaining, refillPercent)}%`}
7777
height="100%"
7878
fill={`url(#${patternId})`}
7979
/>
@@ -142,8 +142,10 @@ function PoolWindowCard({
142142
</Text>
143143
) : null}
144144
<View className="flex-row gap-1">
145-
{pool.columns.map(({ account, window }, index) => {
146-
if (!window) return <View key={account.key} className="h-7 min-w-0 flex-1" />;
145+
{pool.columns.map((member, index) => {
146+
if (!member.window)
147+
return <View key={member.account.key} className="h-7 min-w-0 flex-1" />;
148+
const { account, window } = member;
147149
return (
148150
<Pressable
149151
key={account.key}
@@ -156,7 +158,7 @@ function PoolWindowCard({
156158
<AccountSegment
157159
remaining={remainingPercent(window)}
158160
color={color}
159-
pending={Boolean(window.resetsAt)}
161+
refillPercent={window.resetsAt ? member.refillPercent : 0}
160162
/>
161163
<View pointerEvents="none" className="absolute inset-0 items-center justify-center">
162164
<Text className="text-xs font-t3-medium tabular-nums text-foreground">

‎apps/web/src/components/usage/UsageLimitsPooled.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ function PoolSegment({
209209
account,
210210
window,
211211
reset,
212+
refillPercent,
212213
color,
213214
now,
214215
index,
@@ -217,6 +218,7 @@ function PoolSegment({
217218
readonly account: LimitAccount;
218219
readonly window: LimitPoolMember["window"];
219220
readonly reset: LimitPoolWindow["resets"][number] | undefined;
221+
readonly refillPercent: number;
220222
readonly color: string;
221223
readonly now: number;
222224
readonly index: number;
@@ -244,12 +246,12 @@ function PoolSegment({
244246
className="absolute inset-y-0 left-0 rounded-md opacity-35"
245247
style={{ width: `${remaining}%`, backgroundColor: color }}
246248
/>
247-
{remaining < 100 && reset ? (
249+
{remaining < 100 && reset && refillPercent > 0 ? (
248250
<div
249251
aria-hidden
250252
className="absolute inset-y-0 right-0 opacity-20"
251253
style={{
252-
width: `${100 - remaining}%`,
254+
width: `${Math.min(100 - remaining, refillPercent)}%`,
253255
backgroundImage: `repeating-linear-gradient(135deg, ${color} 0 1px, transparent 1px 5px)`,
254256
}}
255257
/>
@@ -440,6 +442,7 @@ function PoolBar({
440442
account={member.account}
441443
window={member.window}
442444
reset={restores.get(member.account.key)}
445+
refillPercent={member.refillPercent}
443446
color={color}
444447
now={now}
445448
index={position + 1}

‎docs/user/usage.md‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ the dialog.
8181
**Usage → Limits** pools every subscription account it can see per provider, so with several Codex
8282
or Claude accounts across your environments and hubs you read one number per window rather than a
8383
list. Each window card shows how much of the pool is left and a bar with one segment per account,
84-
kept in the same column across windows. Accounts are ordered by their 5-hour reset, soonest
85-
first, or by the first available window when no account reports a 5-hour limit. A gap means the
84+
kept in the same column across windows. Accounts are ordered by their next 5-hour refill, soonest
85+
first. For an account with no weekly quota left, this is when its session becomes usable again.
86+
The first available window is used when no account reports a 5-hour limit. A gap means the
8687
account does not report that window. When the provider reports reset times, the card also says
8788
when the next reset lands and how much it hands back. The hatched
8889
part of a segment is what that reset restores. Tap a segment or account row for the account's plan,
@@ -95,6 +96,12 @@ the bar show each account's quota, countdown, and credits. Tap a row to open its
9596
The same account signed in on more than one environment, or reported by a hub as well, counts once.
9697
Filter with the environment dropdown to see what a single machine has.
9798

99+
When a Claude or Codex account has no overall weekly quota left, its pooled session contribution
100+
shows 0% left. The next refill depends on both reset times: an exhausted session must also reset,
101+
and a partly spent session may only regain its unused portion when the week resets. Missing or
102+
stale reset times have no refill estimate; refresh Limits for a new reading. Claude's
103+
model-specific weekly allowances keep their reported values.
104+
98105
Opening Limits checks the selected connected environments automatically. Each client waits at
99106
least five minutes between automatic checks of an environment, including after a failed check.
100107
If a window still looks stale, refresh Limits to re-check every provider and hub.

‎packages/shared/src/usageLimits.test.ts‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,63 @@ describe("pooled account columns", () => {
862862
row.columns.map((member) => (member.window ? member.account.key : null)),
863863
);
864864

865+
it.each([
866+
["claudeAgent", "13:00", 40, "14:00", 100],
867+
["codex", "14:00", 100, "14:00", 100],
868+
["claudeAgent", "15:00", 100, "15:00", 100],
869+
["codex", "15:00", 40, "14:00", 60],
870+
] as const)(
871+
"%s session resetting at %s with %i percent spent becomes usable at %s with %i percent restored",
872+
(driver, sessionReset, usedPercent, usableAt, restoresPercent) => {
873+
const at = (time: string) => `2026-09-03T${time}:00.000Z`;
874+
const a = {
875+
...account("a", [
876+
{
877+
...window,
878+
id: driver === "codex" ? "primary" : window.id,
879+
usedPercent,
880+
resetsAt: at(sessionReset),
881+
},
882+
{
883+
...weekly,
884+
id: driver === "codex" ? "secondary" : weekly.id,
885+
usedPercent: 100,
886+
resetsAt: at("14:00"),
887+
},
888+
]),
889+
driver: ProviderDriverKind.make(driver),
890+
};
891+
const [session] = collectLimitPools([a], now)[0]!.windows;
892+
expect.soft(session?.remainingPercent).toBe(0);
893+
expect(session?.resets[0]).toMatchObject({
894+
at: Date.parse(at(usableAt)),
895+
restoresPercent,
896+
});
897+
},
898+
);
899+
900+
it("excludes weekly-blocked sessions from the pool without changing snapshots or model allowances", () => {
901+
const blocked = account("blocked", [
902+
window,
903+
{ ...weekly, usedPercent: 100 },
904+
{ ...weekly, id: "seven_day_fable", usedPercent: 20 },
905+
]);
906+
const accounts = [
907+
blocked,
908+
account("available", [
909+
{ ...window, usedPercent: 20 },
910+
{ ...weekly, usedPercent: 99 },
911+
{ ...weekly, id: "seven_day_fable", usedPercent: 40 },
912+
]),
913+
{ ...blocked, key: "cursor", driver: ProviderDriverKind.make("cursor") },
914+
];
915+
const snapshot = structuredClone(accounts);
916+
const [claude, cursor] = collectLimitPools(accounts, now);
917+
expect.soft(claude?.windows.map((row) => row.remainingPercent)).toEqual([40, 1, 70]);
918+
expect.soft(cursor?.windows.map((row) => row.remainingPercent)).toEqual([60, 0, 80]);
919+
expect(accounts).toEqual(snapshot);
920+
});
921+
865922
it("keeps session columns across rows with opposite reset and usage orders", () => {
866923
const accounts = [
867924
account("a", [
@@ -884,6 +941,52 @@ describe("pooled account columns", () => {
884941
expect(keys(collectLimitPools(accounts.toReversed(), now)[0]!)).toEqual(keys(pool!));
885942
});
886943

944+
it.each([undefined, "invalid", "2026-09-03T11:30:00.000Z"])(
945+
"keeps blocked sessions empty without predicting a refill from a missing or stale clock (%s)",
946+
(resetsAt) => {
947+
const [pool] = collectLimitPools(
948+
[
949+
account("session-clock", [
950+
{ ...window, resetsAt },
951+
{ ...weekly, usedPercent: 100 },
952+
]),
953+
account("weekly-clock", [window, { ...weekly, usedPercent: 100, resetsAt }]),
954+
],
955+
now,
956+
);
957+
expect(pool!.windows[0]).toMatchObject({ remainingPercent: 0, pace: null, resets: [] });
958+
expect(pool!.windows[0]!.columns.map((column) => column.window?.resetsAt)).toEqual([
959+
undefined,
960+
undefined,
961+
]);
962+
},
963+
);
964+
965+
it("orders by usable session refill and clears suppression after a fresh weekly reading", () => {
966+
const blocked = account("blocked", [
967+
{ ...window, resetsAt: "2026-09-03T13:00:00.000Z" },
968+
{ ...weekly, usedPercent: 100, resetsAt: "2026-09-03T15:00:00.000Z" },
969+
]);
970+
const available = account("available", [window]);
971+
const [before] = collectLimitPools([blocked, available], now);
972+
expect(before!.accounts.map((account) => account.key)).toEqual(["available", "blocked"]);
973+
expect(before!.windows[0]!.remainingPercent).toBe(30);
974+
const refreshed = {
975+
...blocked,
976+
limits: {
977+
checkedAt: "2026-09-03T12:00:00.000Z",
978+
windows: [blocked.limits.windows[0]!, { ...weekly, usedPercent: 10 }],
979+
},
980+
};
981+
const [after] = collectLimitPools([refreshed, available], now);
982+
expect(after!.accounts.map((account) => account.key)).toEqual(["blocked", "available"]);
983+
expect(after!.windows[0]!.remainingPercent).toBe(60);
984+
expect(after!.windows[0]!.resets[0]).toMatchObject({
985+
at: Date.parse("2026-09-03T13:00:00.000Z"),
986+
restoresPercent: 20,
987+
});
988+
});
989+
887990
it("preserves gaps without counting missing windows toward pooled quota", () => {
888991
const [pool] = collectLimitPools(
889992
[

‎packages/shared/src/usageLimits.ts‎

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,17 @@ export function collectLimitNotices(presentations: LimitPresentations): readonly
313313
export interface LimitPoolMember {
314314
readonly account: LimitAccount;
315315
readonly window: ServerProviderUsageWindow;
316+
readonly refillPercent: number;
316317
}
317318

318319
export interface LimitPoolWindow {
319320
readonly id: string;
320321
readonly kind: ServerProviderUsageWindow["kind"];
321322
readonly label: string;
322323
readonly members: readonly LimitPoolMember[];
323-
readonly columns: ReadonlyArray<{
324-
readonly account: LimitAccount;
325-
readonly window: ServerProviderUsageWindow | null;
326-
}>;
324+
readonly columns: ReadonlyArray<
325+
LimitPoolMember | { readonly account: LimitAccount; readonly window: null }
326+
>;
327327
readonly remainingPercent: number;
328328
readonly usedPercent: number;
329329
readonly pace: LimitPace | null;
@@ -385,7 +385,10 @@ export function collectLimitPools(
385385
const window = account.limits.windows.find(
386386
(window) => orderWindow && poolKey(driver, window) === poolKey(driver, orderWindow),
387387
);
388-
return (window ? resetMillis(window) : null) ?? Number.POSITIVE_INFINITY;
388+
return (
389+
(window ? resetMillis(poolMember(account, window, now).window) : null) ??
390+
Number.POSITIVE_INFINITY
391+
);
389392
};
390393
const sorted = [...members].sort(
391394
(left, right) =>
@@ -410,17 +413,63 @@ function poolKey(driver: ServerProvider["driver"], window: ServerProviderUsageWi
410413
return driver === "codex" ? window.kind : `${window.kind}:${window.id}`;
411414
}
412415

416+
function poolMember(account: LimitAccount, reported: ServerProviderUsageWindow, now: number) {
417+
const spentWeekly =
418+
reported.kind === "session"
419+
? account.limits.windows.find(
420+
(window) =>
421+
window.usedPercent >= 100 &&
422+
(account.driver === "claudeAgent"
423+
? window.id === "seven_day"
424+
: account.driver === "codex" && window.kind === "weekly"),
425+
)
426+
: undefined;
427+
if (!spentWeekly) return { account, window: reported, refillPercent: reported.usedPercent };
428+
429+
const weeklyReset = resetMillis(spentWeekly);
430+
const sessionReset = resetMillis(reported);
431+
const cutoff = Math.max(now, Date.parse(account.limits.checkedAt));
432+
let resetsAt: string | undefined;
433+
let refillPercent = 0;
434+
// A stale or missing clock cannot tell us when blocked quota becomes usable.
435+
if (
436+
weeklyReset !== null &&
437+
sessionReset !== null &&
438+
weeklyReset > cutoff &&
439+
sessionReset > cutoff
440+
) {
441+
if (sessionReset <= weeklyReset) {
442+
resetsAt = spentWeekly.resetsAt;
443+
refillPercent = 100;
444+
} else if (reported.usedPercent >= 100) {
445+
resetsAt = reported.resetsAt;
446+
refillPercent = 100;
447+
} else {
448+
resetsAt = spentWeekly.resetsAt;
449+
refillPercent = 100 - Math.max(0, Math.min(100, reported.usedPercent));
450+
}
451+
}
452+
return {
453+
account,
454+
// Suppression is not session spending, so it has no session pace clock.
455+
window: { ...reported, usedPercent: 100, resetsAt, windowDurationMins: undefined },
456+
refillPercent,
457+
};
458+
}
459+
413460
function poolWindows(accounts: readonly LimitAccount[], now: number): readonly LimitPoolWindow[] {
414461
const byKey = new Map<string, LimitPoolMember[]>();
415462
for (const account of accounts) {
416-
for (const window of account.limits.windows) {
463+
for (const reported of account.limits.windows) {
464+
const member = poolMember(account, reported, now);
465+
const { window } = member;
417466
const pooled = poolKey(account.driver, window);
418467
const key = byKey.get(pooled)?.some((member) => member.account === account)
419468
? `${window.kind}:${window.id}`
420469
: pooled;
421470
const list = byKey.get(key);
422-
if (list) list.push({ account, window });
423-
else byKey.set(key, [{ account, window }]);
471+
if (list) list.push(member);
472+
else byKey.set(key, [member]);
424473
}
425474
}
426475
const pools = [...byKey].map(([key, members]): LimitPoolWindow => {
@@ -447,7 +496,7 @@ function poolWindows(accounts: readonly LimitAccount[], now: number): readonly L
447496
{
448497
member,
449498
at,
450-
restoresPercent: Math.round(member.window.usedPercent / members.length),
499+
restoresPercent: Math.round(member.refillPercent / members.length),
451500
},
452501
];
453502
})

0 commit comments

Comments
 (0)