diff --git a/apps/mobile/src/features/usage/UsageLimitsPooled.tsx b/apps/mobile/src/features/usage/UsageLimitsPooled.tsx index 218a7f21feb1..fdc096b3394a 100644 --- a/apps/mobile/src/features/usage/UsageLimitsPooled.tsx +++ b/apps/mobile/src/features/usage/UsageLimitsPooled.tsx @@ -436,13 +436,18 @@ export function UsageLimitsSection({ {pools.map((pool) => { const windows = displayLimitWindows(pool); return ( - + {providerClients.get(pool.driver)?.label ?? pool.driver} + {pool.label ? ( + + · {pool.label} + + ) : null} {windows.map((window) => { const details = @@ -533,7 +538,7 @@ export function UsageLimitAccountScreen({ route }: AccountScreenProps) { const accounts = collectLimitAccounts(selected); const account = accounts.find((candidate) => candidate.key === accountKey); const pool = collectLimitPools(accounts, now) - .find((candidate) => candidate.driver === account?.driver) + .find((candidate) => candidate.accounts.some((member) => member.key === accountKey)) ?.windows.find((candidate) => candidate.id === windowId && candidate.kind === windowKind); const window = pool?.members.find((member) => member.account.key === accountKey)?.window; const evenPace = window ? paceRemainingPercent(window, now) : null; diff --git a/apps/mobile/src/widgets/subscriptionUsageSnapshot.ts b/apps/mobile/src/widgets/subscriptionUsageSnapshot.ts index 1e0d67689891..b0efb8c04aed 100644 --- a/apps/mobile/src/widgets/subscriptionUsageSnapshot.ts +++ b/apps/mobile/src/widgets/subscriptionUsageSnapshot.ts @@ -125,7 +125,8 @@ function subscriptionUsageProps( configuredDrivers: ReadonlySet, maxWindowsPerProvider: number, ): SubscriptionUsageSnapshot { - const pools = collectLimitPools(accounts, now); + // One row per provider: the widget has no room for a row per CLI Proxy source. + const pools = collectLimitPools(accounts, now, { splitSources: false }); const checked = accounts .filter((account) => account.driver === "codex" || account.driver === "claudeAgent") .map((account) => Date.parse(account.limits.checkedAt)); diff --git a/apps/web/src/components/usage/UsageLimitsPooled.tsx b/apps/web/src/components/usage/UsageLimitsPooled.tsx index 8ec1c7a02afc..5e8830ea3c9b 100644 --- a/apps/web/src/components/usage/UsageLimitsPooled.tsx +++ b/apps/web/src/components/usage/UsageLimitsPooled.tsx @@ -587,19 +587,22 @@ function PoolWindowCard({ function PoolSection({ pool, now }: { readonly pool: LimitPool; readonly now: number }) { const color = barColor(pool.driver); - const label = providerClients.get(pool.driver)?.label ?? String(pool.driver); + const driverLabel = providerClients.get(pool.driver)?.label ?? String(pool.driver); const windows = displayLimitWindows(pool); return (

- {label} + {driverLabel} + {pool.label ? ( + · {pool.label} + ) : null}

{windows.map((window) => { const details = @@ -640,7 +643,7 @@ export function UsageLimitsPooled({ ) : null} {pools.map((pool) => ( - + ))} diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index b66001ca1924..ccc6b4aa6300 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1018,6 +1018,144 @@ describe("pools", () => { ).toContainEqual(["pro@example.com", 95]); }); + it("keeps each CLI Proxy source in its own pool and names it", () => { + const hubAccount = (id: string, usedPercent: number) => ({ + id, + driver: claude, + email: `${id}@example.com`, + usageLimits: { checkedAt, windows: [{ ...weekly, usedPercent }] }, + }); + const input = new Map([ + [ + EnvironmentId.make("env-a"), + { + ...laptop, + serverConfig: { + providers: [], + usageLimitSources: [ + { + ...source, + id: UsageLimitSourceId.make("main"), + label: "Main hub", + accounts: [ + hubAccount("a", 100), + { ...hubAccount("x", 50), driver: ProviderDriverKind.make("codex") }, + hubAccount("b", 100), + ], + }, + { + ...source, + id: UsageLimitSourceId.make("work"), + label: "Work hub", + // Two organisations of one login: separate credentials with separate quotas. + accounts: [hubAccount("c", 20), { ...hubAccount("d", 94), email: "c@example.com" }], + }, + ], + }, + }, + ], + ]); + const accounts = collectLimitAccounts(input); + const pools = collectLimitPools(accounts, now); + // A driver with one source keeps its unlabelled pool; one driver's pools stay adjacent. + expect(pools.map((pool) => [pool.driver, pool.label, pool.accounts.length])).toEqual([ + ["claudeAgent", "Main hub", 2], + ["claudeAgent", "Work hub", 2], + ["codex", null, 1], + ]); + expect(new Set(pools.map((pool) => pool.key)).size).toBe(3); + expect(pools.map((pool) => pool.windows[0]?.remainingPercent)).toEqual([0, 43, 50]); + + // A hub account that this environment also reports natively stays in its hub's pool. + const withNative = new Map([ + [ + EnvironmentId.make("env-a"), + { + ...input.get(EnvironmentId.make("env-a"))!, + serverConfig: { + ...input.get(EnvironmentId.make("env-a"))!.serverConfig, + providers: [ + provider({ + driver: claude, + instanceId: ProviderInstanceId.make("claude"), + auth: { status: "authenticated", email: "a@example.com" }, + usageLimits: { checkedAt, windows: [{ ...weekly, usedPercent: 100 }] }, + }), + ], + }, + }, + ], + ]); + expect( + collectLimitPools(collectLimitAccounts(withNative), now).map((pool) => [ + pool.driver, + pool.label, + pool.accounts.length, + ]), + ).toEqual([ + ["claudeAgent", "Main hub", 2], + ["claudeAgent", "Work hub", 2], + ["codex", null, 1], + ]); + + const combined = collectLimitPools(accounts, now, { splitSources: false }); + expect(combined.map((pool) => [pool.driver, pool.label, pool.accounts.length])).toEqual([ + ["claudeAgent", null, 4], + ["codex", null, 1], + ]); + + const sources = input.get(EnvironmentId.make("env-a"))!.serverConfig.usageLimitSources; + for (const separateEnvironments of [false, true]) { + const sharedLabelSources = sources.map((entry) => ({ ...entry, label: "proxy.example.com" })); + const presentations = new Map([ + [ + EnvironmentId.make("env-a"), + { + ...laptop, + serverConfig: { + providers: [], + usageLimitSources: separateEnvironments + ? sharedLabelSources.slice(0, 1) + : sharedLabelSources, + }, + }, + ], + ]); + if (separateEnvironments) { + presentations.set(EnvironmentId.make("env-b"), { + ...laptop, + serverConfig: { + providers: [], + usageLimitSources: [{ ...sharedLabelSources[1]!, id: sources[0]!.id }], + }, + }); + } + const sameLabelPools = collectLimitPools(collectLimitAccounts(presentations), now); + expect(sameLabelPools.map((pool) => [pool.driver, pool.accounts.length])).toEqual([ + ["claudeAgent", 2], + ["claudeAgent", 2], + ["codex", 1], + ]); + expect(new Set(sameLabelPools.map((pool) => pool.key)).size).toBe(3); + expect(sameLabelPools.map((pool) => pool.windows[0]?.remainingPercent)).toEqual([0, 43, 50]); + } + + // One remote hub reached from two environments is one quota, so it stays one unlabelled pool. + const shared = { ...sources[1]!, endpoint: "http://100.64.0.1:8318" }; + const sameHub = new Map( + ["env-a", "env-b"].map((id) => [ + EnvironmentId.make(id), + { ...laptop, serverConfig: { providers: [], usageLimitSources: [shared] } }, + ]), + ); + expect( + collectLimitPools(collectLimitAccounts(sameHub), now).map((pool) => [ + pool.label, + pool.accounts.length, + ]), + ).toEqual([[null, 2]]); + }); + it("pools windows by id across accounts and orders resets by when they land", () => { const input = new Map([ [ @@ -1116,6 +1254,7 @@ describe("pools", () => { plan: undefined, accentColor: undefined, environments: [], + hub: null, sourceLabel: null, redeem: null, limits: { @@ -1167,6 +1306,7 @@ describe("pooled account columns", () => { plan: undefined, accentColor: undefined, environments: [], + hub: { key: "hub", label: "Hub" }, sourceLabel: "Hub", redeem: null, limits: { checkedAt: "2026-09-03T11:00:00.000Z", windows }, diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 6fe6c70a89a4..f64cac7822da 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -280,6 +280,8 @@ export interface LimitAccount { readonly environmentId: EnvironmentId; readonly label: string; }>; + /** The CLI Proxy hub that reports this account, kept through native merges for pooling. */ + readonly hub: { readonly key: string; readonly label: string } | null; readonly sourceLabel: string | null; readonly redeem: { readonly environmentId: EnvironmentId; @@ -349,6 +351,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl plan: planSources.get(key)?.plan ?? previous.plan ?? next.plan, accentColor: previous.accentColor ?? next.accentColor, environments, + hub: previous.hub ?? next.hub, sourceLabel: environments.length > 0 ? null : (previous.sourceLabel ?? next.sourceLabel), redeem: hubRedeems.get(key)?.redeem ?? @@ -381,6 +384,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl plan: provider.auth.label, accentColor: provider.accentColor, environments: [{ environmentId, label }], + hub: null, sourceLabel: null, redeem: { environmentId, input: { instanceId: provider.instanceId } }, limits: provider.usageLimits, @@ -393,9 +397,14 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl const sourceLabel = labelEnvironment ? `${presentation.entry.target.label} · ${source.label}` : source.label; + // One source lists each credential once, so two of its accounts that resolve to the same + // login (one e-mail, two organisations) are separate quotas and must not merge. + const claimed = new Set(); for (const account of source.accounts) { if (limitsNotice(account.usageLimits) !== null) continue; - const resolved = resolveKey(account); + const found = resolveKey(account); + const resolved = found && !claimed.has(found) ? found : null; + if (found) claimed.add(found); // A resolved account already has an identity; its public key must stay distinct from others'. const hub = resolved ? `${environmentId}:${source.id}` : hubIdentity(environmentId, source); const key = `${hub}:${account.id}${account.workspaceId ? `:${account.workspaceId}` : ""}`; @@ -407,6 +416,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl plan: account.plan, accentColor: undefined, environments: [], + hub: { key: hubIdentity(environmentId, source), label: sourceLabel }, sourceLabel, redeem: account.usageLimits.resetCredits?.nextCreditId ? { @@ -474,7 +484,10 @@ export interface LimitPoolWindow { } export interface LimitPool { + readonly key: string; readonly driver: ServerProvider["driver"]; + /** The CLI Proxy source, when one driver's accounts come from more than one place. */ + readonly label: string | null; readonly accounts: readonly LimitAccount[]; readonly windows: readonly LimitPoolWindow[]; } @@ -529,14 +542,36 @@ const WINDOW_KIND_ORDER: Record = { export function collectLimitPools( accounts: readonly LimitAccount[], now: number, + options: { readonly splitSources?: boolean } = {}, ): readonly LimitPool[] { - const byDriver = new Map(); + const sourcesByDriver = new Map>(); + if (options.splitSources ?? true) { + for (const account of accounts) { + if (!account.hub) continue; + const sources = sourcesByDriver.get(account.driver) ?? new Set(); + sourcesByDriver.set(account.driver, sources.add(account.hub.key)); + } + } + const split = (account: LimitAccount) => (sourcesByDriver.get(account.driver)?.size ?? 0) > 1; + const byPool = new Map(); for (const account of accounts) { - const list = byDriver.get(account.driver); + const key = + split(account) && account.hub ? `${account.driver}:${account.hub.key}` : account.driver; + const list = byPool.get(key); if (list) list.push(account); - else byDriver.set(account.driver, [account]); + else byPool.set(key, [account]); } - return [...byDriver].map(([driver, members]) => { + // Pools of one driver stay together, in the order drivers first appear (the sort is stable). + const driverOrder = new Map(); + for (const account of accounts) { + if (!driverOrder.has(account.driver)) driverOrder.set(account.driver, driverOrder.size); + } + const grouped = [...byPool].sort( + ([, [left]], [, [right]]) => driverOrder.get(left!.driver)! - driverOrder.get(right!.driver)!, + ); + return grouped.map(([key, members]) => { + const driver = members[0]!.driver; + const label = split(members[0]!) ? (members[0]!.hub?.label ?? null) : null; const orderWindow = members .flatMap((account) => account.limits.windows) .sort((left, right) => WINDOW_KIND_ORDER[left.kind] - WINDOW_KIND_ORDER[right.kind])[0]; @@ -555,7 +590,7 @@ export function collectLimitPools( accountSortName(left).localeCompare(accountSortName(right)) || left.key.localeCompare(right.key), ); - return { driver, accounts: sorted, windows: poolWindows(sorted, now) }; + return { key, driver, label, accounts: sorted, windows: poolWindows(sorted, now) }; }); }