From 18df04453d784f62925a976c38b7e4666a69ed83 Mon Sep 17 00:00:00 2001 From: WhiteWarrior625 <42657673+WhiteWarrior625@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:38:26 -0500 Subject: [PATCH 1/2] fix(web): keep each CLI Proxy source's limits in its own pool MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Limits page pools every account of a driver together, so two CLI Proxy sources that both serve Claude (for example a personal hub and a work hub) were shown as one "Claude" pool whose windows averaged two separate quotas. When one driver's accounts come from two or more hubs, each hub now gets its own pool, identified by hubIdentity so one remote hub seen from several environments stays one pool, and its heading names the source ("Claude · Work hub") on web and mobile. Pools of one driver stay adjacent. Within one source, two credentials for the same e-mail (two organisations of one login) stay separate accounts. Everything else pools as before, and the subscription widget keeps one combined row per provider. Co-Authored-By: Claude Opus 5.5 --- .../src/features/usage/UsageLimitsPooled.tsx | 9 +- .../src/widgets/subscriptionUsageSnapshot.ts | 3 +- .../components/usage/UsageLimitsPooled.tsx | 11 +- packages/shared/src/usageLimits.test.ts | 108 ++++++++++++++++++ packages/shared/src/usageLimits.ts | 48 +++++++- 5 files changed, 166 insertions(+), 13 deletions(-) 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..c91f8f984996 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1018,6 +1018,112 @@ 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]); + + 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 +1222,7 @@ describe("pools", () => { plan: undefined, accentColor: undefined, environments: [], + sourceKey: null, sourceLabel: null, redeem: null, limits: { @@ -1167,6 +1274,7 @@ describe("pooled account columns", () => { plan: undefined, accentColor: undefined, environments: [], + sourceKey: "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..bb34a71fe725 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -280,6 +280,7 @@ export interface LimitAccount { readonly environmentId: EnvironmentId; readonly label: string; }>; + readonly sourceKey: string | null; readonly sourceLabel: string | null; readonly redeem: { readonly environmentId: EnvironmentId; @@ -349,6 +350,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl plan: planSources.get(key)?.plan ?? previous.plan ?? next.plan, accentColor: previous.accentColor ?? next.accentColor, environments, + sourceKey: environments.length > 0 ? null : (previous.sourceKey ?? next.sourceKey), sourceLabel: environments.length > 0 ? null : (previous.sourceLabel ?? next.sourceLabel), redeem: hubRedeems.get(key)?.redeem ?? @@ -381,6 +383,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl plan: provider.auth.label, accentColor: provider.accentColor, environments: [{ environmentId, label }], + sourceKey: null, sourceLabel: null, redeem: { environmentId, input: { instanceId: provider.instanceId } }, limits: provider.usageLimits, @@ -393,9 +396,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 +415,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl plan: account.plan, accentColor: undefined, environments: [], + sourceKey: hubIdentity(environmentId, source), sourceLabel, redeem: account.usageLimits.resetCredits?.nextCreditId ? { @@ -474,7 +483,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 +541,38 @@ 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.sourceKey) continue; + const sources = sourcesByDriver.get(account.driver) ?? new Set(); + sourcesByDriver.set(account.driver, sources.add(account.sourceKey)); + } + } + 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.sourceKey + ? `${account.driver}:${account.sourceKey}` + : 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]!.sourceLabel ?? 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 +591,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) }; }); } From 02e35cb51097603cd03e3f8f2f02579c3f3b7e65 Mon Sep 17 00:00:00 2001 From: WhiteWarrior625 <42657673+WhiteWarrior625@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:53:56 -0500 Subject: [PATCH 2/2] fix(shared): a native report no longer pulls a hub account out of its pool Merging a hub account with a native report of the same login cleared its source identity, so with two hubs it left its hub's pool for an unlabelled one. LimitAccount now carries `hub: { key, label }`, kept through the merge and used only for pooling; `sourceLabel` keeps its display meaning. Co-Authored-By: Claude Opus 5.5 --- packages/shared/src/usageLimits.test.ts | 36 +++++++++++++++++++++++-- packages/shared/src/usageLimits.ts | 19 +++++++------ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index c91f8f984996..ccc6b4aa6300 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1066,6 +1066,38 @@ describe("pools", () => { 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], @@ -1222,7 +1254,7 @@ describe("pools", () => { plan: undefined, accentColor: undefined, environments: [], - sourceKey: null, + hub: null, sourceLabel: null, redeem: null, limits: { @@ -1274,7 +1306,7 @@ describe("pooled account columns", () => { plan: undefined, accentColor: undefined, environments: [], - sourceKey: "hub", + 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 bb34a71fe725..f64cac7822da 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -280,7 +280,8 @@ export interface LimitAccount { readonly environmentId: EnvironmentId; readonly label: string; }>; - readonly sourceKey: string | null; + /** 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; @@ -350,7 +351,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl plan: planSources.get(key)?.plan ?? previous.plan ?? next.plan, accentColor: previous.accentColor ?? next.accentColor, environments, - sourceKey: environments.length > 0 ? null : (previous.sourceKey ?? next.sourceKey), + hub: previous.hub ?? next.hub, sourceLabel: environments.length > 0 ? null : (previous.sourceLabel ?? next.sourceLabel), redeem: hubRedeems.get(key)?.redeem ?? @@ -383,7 +384,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl plan: provider.auth.label, accentColor: provider.accentColor, environments: [{ environmentId, label }], - sourceKey: null, + hub: null, sourceLabel: null, redeem: { environmentId, input: { instanceId: provider.instanceId } }, limits: provider.usageLimits, @@ -415,7 +416,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl plan: account.plan, accentColor: undefined, environments: [], - sourceKey: hubIdentity(environmentId, source), + hub: { key: hubIdentity(environmentId, source), label: sourceLabel }, sourceLabel, redeem: account.usageLimits.resetCredits?.nextCreditId ? { @@ -546,18 +547,16 @@ export function collectLimitPools( const sourcesByDriver = new Map>(); if (options.splitSources ?? true) { for (const account of accounts) { - if (!account.sourceKey) continue; + if (!account.hub) continue; const sources = sourcesByDriver.get(account.driver) ?? new Set(); - sourcesByDriver.set(account.driver, sources.add(account.sourceKey)); + 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 key = - split(account) && account.sourceKey - ? `${account.driver}:${account.sourceKey}` - : account.driver; + split(account) && account.hub ? `${account.driver}:${account.hub.key}` : account.driver; const list = byPool.get(key); if (list) list.push(account); else byPool.set(key, [account]); @@ -572,7 +571,7 @@ export function collectLimitPools( ); return grouped.map(([key, members]) => { const driver = members[0]!.driver; - const label = split(members[0]!) ? (members[0]!.sourceLabel ?? null) : null; + 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];