Skip to content

Commit 5c8104c

Browse files
committed
fix(usage): show Codex workspace quotas separately
1 parent 9a3070b commit 5c8104c

13 files changed

Lines changed: 563 additions & 29 deletions

‎apps/server/src/provider/CodexProvider.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const RATE_LIMITS_PROBE_TIMEOUT_MS = 3_000;
5454
type CodexRateLimitsProbe =
5555
| {
5656
readonly snapshot: CodexRateLimitSnapshot;
57+
readonly accountId?: string | null | undefined;
5758
readonly rateLimitsByLimitId?:
5859
| Readonly<Record<string, CodexRateLimitSnapshot>>
5960
| null
@@ -451,6 +452,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
451452
: client.request("account/rateLimits/read", null).pipe(
452453
Effect.map((response): CodexRateLimitsProbe => ({
453454
snapshot: response.rateLimits,
455+
accountId: response.accountId,
454456
rateLimitsByLimitId: response.rateLimitsByLimitId,
455457
resetCredits: response.rateLimitResetCredits,
456458
})),
@@ -678,6 +680,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
678680
})
679681
: codexRateLimitsToLimits({
680682
snapshot: snapshot.rateLimits.snapshot,
683+
accountId: snapshot.rateLimits.accountId,
681684
rateLimitsByLimitId: snapshot.rateLimits.rateLimitsByLimitId,
682685
resetCredits: snapshot.rateLimits.resetCredits,
683686
checkedAt,

‎apps/server/src/provider/ProviderRegistry.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@ it.layer(
403403
const status = yield* checkCodexProviderStatus(defaultCodexSettings, () =>
404404
Effect.succeed(
405405
makeCodexProbeSnapshot({
406+
rateLimits: {
407+
snapshot: { primary: { usedPercent: 9 } },
408+
accountId: "business-account",
409+
resetCredits: undefined,
410+
},
406411
skills: [
407412
{
408413
name: "github:gh-fix-ci",
@@ -422,6 +427,7 @@ it.layer(
422427
assert.strictEqual(status.auth.type, "chatgpt");
423428
assert.strictEqual(status.auth.label, "ChatGPT Pro 20x Subscription");
424429
assert.strictEqual(status.auth.email, "[email protected]");
430+
assert.strictEqual(status.usageLimits?.accountId, "business-account");
425431
assert.deepStrictEqual(status.models, [
426432
{
427433
slug: "gpt-live-codex",

‎apps/server/src/provider/codexUsageLimits.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ import {
1313
const checkedAt = "2026-07-18T10:00:00.000Z";
1414

1515
describe("codexRateLimitsToLimits", () => {
16+
it("carries the quota account identity into the client snapshot", () => {
17+
expect(
18+
codexRateLimitsToLimits({
19+
checkedAt,
20+
accountId: "business-account",
21+
snapshot: { primary: { usedPercent: 9 } },
22+
}),
23+
).toMatchObject({ accountId: "business-account" });
24+
});
25+
1626
it("maps primary and secondary onto the session and weekly windows", () => {
1727
expect(
1828
codexRateLimitsToLimits({

‎apps/server/src/provider/codexUsageLimits.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export function codexResetCreditsToContract(
115115

116116
export function codexRateLimitsToLimits(input: {
117117
readonly snapshot: CodexRateLimitSnapshot;
118+
readonly accountId?: string | null | undefined;
118119
readonly rateLimitsByLimitId?:
119120
| Readonly<Record<string, CodexRateLimitSnapshot>>
120121
| null
@@ -123,13 +124,15 @@ export function codexRateLimitsToLimits(input: {
123124
readonly checkedAt: string;
124125
}): ServerProviderUsageLimits {
125126
const resetCredits = codexResetCreditsToContract(input.resetCredits);
127+
const accountId = input.accountId?.trim();
126128
// Select the main bucket explicitly; the legacy snapshot can name another limit.
127129
const windows = codexRateLimitsToWindows(input.rateLimitsByLimitId?.codex ?? input.snapshot);
128130
return {
129131
...makeUsageLimits({
130132
checkedAt: input.checkedAt,
131133
windows,
132134
}),
135+
...(accountId ? { accountId } : {}),
133136
...(resetCredits ? { resetCredits } : {}),
134137
};
135138
}

‎apps/server/src/provider/providerUsageLimits.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ const weekly = {
2121
const published = { checkedAt, windows: [session, weekly] };
2222

2323
describe("applyUsageLimitsUpdate", () => {
24+
it("retains account identity when a streamed update changes the quota", () => {
25+
expect(
26+
applyUsageLimitsUpdate({
27+
previous: { ...published, accountId: "business-account" },
28+
checkedAt,
29+
update: { windows: [{ ...session, usedPercent: 55 }] },
30+
}),
31+
).toMatchObject({ accountId: "business-account" });
32+
});
33+
2434
it("returns the published object itself when no window moved", () => {
2535
// Codex repeats the same numbers beside every token-usage tick; the
2636
// ingestion path relies on identity to skip the publish.

‎apps/server/src/provider/providerUsageLimits.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export function applyUsageLimitsUpdate(input: {
9393
}
9494
return {
9595
...makeUsageLimits({ checkedAt: input.checkedAt, windows: merged.values() }),
96+
...(previous?.accountId !== undefined ? { accountId: previous.accountId } : {}),
9697
...(previous?.resetCredits !== undefined ? { resetCredits: previous.resetCredits } : {}),
9798
};
9899
}

‎apps/server/src/usage/cliproxyApi.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ describe("CLIProxyAPI built-in management API", () => {
123123
const test = fixture();
124124
const api = yield* test.api;
125125
const result = yield* api.readAccounts(config);
126+
expect(result.map((account) => account.usageLimits.accountId)).toEqual([
127+
"account-a",
128+
"account-b",
129+
]);
126130
expect(result.map((account) => account.usageLimits.resetCredits)).toEqual([
127131
{ availableCount: 2, nextCreditId: "first", nextExpiresAt: "2099-01-01T00:00:00.000Z" },
128132
{ availableCount: 2, nextCreditId: "first", nextExpiresAt: "2099-01-01T00:00:00.000Z" },

‎apps/server/src/usage/cliproxyApi.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
268268
usageLimits: {
269269
...codexRateLimitsToLimits({
270270
checkedAt,
271+
accountId: account.id_token?.chatgpt_account_id,
271272
snapshot: {
272273
planType: usage.plan_type ?? null,
273274
primary: toWindow(usage.rate_limit?.primary_window),

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

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,38 @@ function AccountName({
121121
);
122122
}
123123

124+
/** A visible label key; initials chips can collide even when the emails differ. */
125+
function accountNameKey(account: LimitAccount): string {
126+
return (
127+
account.displayName ??
128+
(account.email
129+
? accountInitials(account.email)
130+
: (getDriverOption(account.driver)?.label ?? String(account.driver)))
131+
);
132+
}
133+
134+
/** Preserve distinct labels and allocate collision-free suffixes in stable key order. */
135+
function disambiguateAccountNames(accounts: readonly LimitAccount[]): readonly LimitAccount[] {
136+
const names = accounts.map(accountNameKey);
137+
const used = new Set(names);
138+
const labels = new Map<string, string>();
139+
accounts
140+
.toSorted((left, right) => left.key.localeCompare(right.key))
141+
.forEach((account, index) => {
142+
const name = accountNameKey(account);
143+
if (names.indexOf(name) === names.lastIndexOf(name)) return;
144+
let suffix = index + 1;
145+
let label = `${account.displayName ?? "Account"} ${suffix}`;
146+
while (used.has(label)) label = `${account.displayName ?? "Account"} ${++suffix}`;
147+
used.add(label);
148+
labels.set(account.key, label);
149+
});
150+
return accounts.map((account) => {
151+
const displayName = labels.get(account.key);
152+
return displayName ? { ...account, displayName } : account;
153+
});
154+
}
155+
124156
function Row({ label, children }: { readonly label: string; readonly children: ReactNode }) {
125157
return (
126158
<div className="grid grid-cols-[4.5rem_minmax(0,1fr)] gap-x-3">
@@ -194,7 +226,9 @@ function SegmentPopover({
194226
</Row>
195227
) : null}
196228
{reset && reset.restoresPercent > 0 ? (
197-
<Row label="Restores">+{reset.restoresPercent}% of pool</Row>
229+
<Row label="Restores">
230+
+{reset.restoresPercent}% {account.driver === "codex" ? "of quota" : "of pool"}
231+
</Row>
198232
) : null}
199233
</div>
200234
{credits && redeem ? (
@@ -274,12 +308,14 @@ function PoolSegment({
274308
}}
275309
/>
276310
) : null}
277-
<span
278-
aria-hidden
279-
className="absolute inset-0 flex items-center justify-center text-3xs leading-none font-semibold text-foreground/80 tabular-nums @2xl/pool:hidden"
280-
>
281-
{index}
282-
</span>
311+
{account.driver !== "codex" ? (
312+
<span
313+
aria-hidden
314+
className="absolute inset-0 flex items-center justify-center text-3xs leading-none font-semibold text-foreground/80 tabular-nums @2xl/pool:hidden"
315+
>
316+
{index}
317+
</span>
318+
) : null}
283319
<div className="relative hidden h-full min-w-0 items-center gap-1.5 px-2 text-xs @2xl/pool:flex">
284320
{showAccountName ? (
285321
<AccountName
@@ -360,15 +396,17 @@ function LegendRow({
360396
render={<Button variant="ghost" size="compact" />}
361397
className="min-w-0 @2xl/pool:hidden"
362398
>
363-
<span className="relative inline-flex size-4 shrink-0 items-center justify-center rounded-sm text-3xs leading-none font-semibold text-foreground/80 tabular-nums">
364-
<span
365-
aria-hidden
366-
className="absolute inset-0 rounded-sm opacity-35"
367-
style={{ backgroundColor: color }}
368-
/>
369-
<span className="sr-only">Segment </span>
370-
<span className="relative">{index}</span>
371-
</span>
399+
{account.driver !== "codex" ? (
400+
<span className="relative inline-flex size-4 shrink-0 items-center justify-center rounded-sm text-3xs leading-none font-semibold text-foreground/80 tabular-nums">
401+
<span
402+
aria-hidden
403+
className="absolute inset-0 rounded-sm opacity-35"
404+
style={{ backgroundColor: color }}
405+
/>
406+
<span className="sr-only">Segment </span>
407+
<span className="relative">{index}</span>
408+
</span>
409+
) : null}
372410
<AccountName account={account} className="min-w-0 truncate font-medium text-foreground" />
373411
<span className="shrink-0 font-semibold text-foreground tabular-nums">{remaining}%</span>
374412
<span className="ms-auto flex shrink-0 items-center gap-1.5 text-2xs text-muted-foreground tabular-nums">
@@ -499,15 +537,17 @@ function PoolWindowCard({
499537
readonly pool: LimitPoolWindow;
500538
readonly color: string;
501539
readonly now: number;
502-
readonly label?: string | undefined;
540+
readonly label?: ReactNode;
503541
readonly description?: string | undefined;
504542
}) {
505543
// The soonest reset that hands anything back; an untouched account resets to no effect.
506544
const nextRefill = pool.resets.find((reset) => reset.restoresPercent > 0);
507545
return (
508546
<div className="grid items-center gap-x-6 gap-y-3 rounded-lg border border-border/60 p-4 md:grid-cols-[11rem_minmax(0,1fr)]">
509547
<div className="flex flex-col gap-1">
510-
<span className="text-sm font-medium text-foreground">{label ?? pool.label}</span>
548+
<span className="flex min-w-0 items-center gap-1.5 text-sm font-medium text-foreground">
549+
{label ?? pool.label}
550+
</span>
511551
<span className="flex items-baseline gap-2">
512552
<span className="text-3xl font-semibold text-foreground tabular-nums">
513553
{pool.remainingPercent}%
@@ -532,7 +572,13 @@ function PoolWindowCard({
532572
function PoolSection({ pool, now }: { readonly pool: LimitPool; readonly now: number }) {
533573
const color = barColor(pool.driver);
534574
const label = getDriverOption(pool.driver)?.label ?? String(pool.driver);
535-
const windows = displayLimitWindows(pool);
575+
// Codex workspaces can share an email but have independent subscription quotas.
576+
const separateAccounts = pool.driver === "codex" && pool.accounts.length > 1;
577+
const windows = separateAccounts
578+
? disambiguateAccountNames(pool.accounts).flatMap((account) =>
579+
displayLimitWindows(collectLimitPools([account], now)[0]!),
580+
)
581+
: displayLimitWindows(pool);
536582
return (
537583
<section className="flex flex-col gap-3">
538584
<h2 className="flex items-center gap-2 text-sm font-medium text-foreground">
@@ -547,13 +593,23 @@ function PoolSection({ pool, now }: { readonly pool: LimitPool; readonly now: nu
547593
</h2>
548594
{windows.map((window) => {
549595
const details = pool.driver === "cursor" ? cursorUsageWindowDetails(window.id) : undefined;
596+
const account = separateAccounts ? window.members[0]!.account : undefined;
550597
return (
551598
<PoolWindowCard
552-
key={`${window.kind}:${window.id}`}
599+
key={`${account?.key ?? ""}:${window.kind}:${window.id}`}
553600
pool={window}
554601
color={color}
555602
now={now}
556-
label={details?.label}
603+
label={
604+
account ? (
605+
<>
606+
<AccountName account={account} className="min-w-0 truncate" />
607+
<span className="shrink-0 text-muted-foreground">· {window.label}</span>
608+
</>
609+
) : (
610+
details?.label
611+
)
612+
}
557613
description={details?.description}
558614
/>
559615
);

0 commit comments

Comments
 (0)