TypeScript
fix(usage): deduplicate native subscription accounts (#10700)
Stephen Jason Wang780b62dd26c4
diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.tsindex 7cc2962f664..5c32792cde4 100644--- a/packages/shared/src/usageLimits.test.ts+++ b/packages/shared/src/usageLimits.test.ts@@ -1258,7 +1258,7 @@ describe("/usage-limits", () => {expect(report?.accounts[0]?.limits.resetCredits?.availableCount).toBe(3);});- it("keeps accounts and custom instances separate, filtering by driver", () => {+ it("keeps unidentified custom instances separate, filtering by driver", () => {const report = collectProviderUsageLimits(selected.instanceId,[@@ -1299,6 +1299,93 @@ describe("/usage-limits", () => {expect(report?.notices).toEqual([]);});+ it("keeps the freshest usable native duplicate and the freshest native credits in either order", () => {+ const failed = provider({+ auth: selected.auth,+ usageLimits: {+ ...limits,+ checkedAt: "2026-09-03T11:40:00.000Z",+ unavailable: { reason: "probeFailed" },+ },+ });+ const credited = provider({+ instanceId: ProviderInstanceId.make("codex-credit"),+ auth: selected.auth,+ usageLimits: {+ checkedAt: "2026-09-03T11:20:00.000Z",+ windows: [{ ...window, usedPercent: 50 }],+ resetCredits: { availableCount: 2 },+ },+ });+ const staleCredit = provider({+ ...credited,+ instanceId: ProviderInstanceId.make("codex-stale-credit"),+ usageLimits: { ...limits, resetCredits: { availableCount: 3 } },+ });+ const freshest = provider({+ instanceId: ProviderInstanceId.make("codex-fresh"),+ auth: selected.auth,+ usageLimits: {+ checkedAt: "2026-09-03T11:30:00.000Z",+ windows: [{ ...window, usedPercent: 55 }],+ },+ });+ const duplicates = [failed, staleCredit, credited, freshest];+ for (const providers of [duplicates, duplicates.toReversed()]) {+ const report = collectProviderUsageLimits(failed.instanceId, providers, sources, now);+ expect(report?.accounts.map((account) => account.id)).toEqual(["codex-fresh", "hub:oss"]);+ expect(report?.accounts[0]).toMatchObject({+ limits: {+ checkedAt: "2026-09-03T11:30:00.000Z",+ windows: [{ usedPercent: 55 }],+ resetCredits: { availableCount: 2 },+ },+ resetCreditInput: { instanceId: "codex-credit" },+ });+ expect(report?.accounts[0]?.limits.unavailable).toBeUndefined();+ }+ });++ it("merges native workspace duplicates only while the workspace evidence is unambiguous", () => {+ const known = provider({+ ...selected,+ instanceId: ProviderInstanceId.make("workspace-a"),+ auth: { ...selected.auth, workspaceId: "ws-a" },+ });+ const fresh = provider({+ ...known,+ instanceId: ProviderInstanceId.make("workspace-a-fresh"),+ usageLimits: { ...limits, checkedAt: "2026-09-03T11:30:00.000Z" },+ });+ const natives = [known, fresh, selected];+ expect(+ collectProviderUsageLimits(selected.instanceId, natives, [], now)?.accounts.map(+ (account) => account.id,+ ),+ ).toEqual(["workspace-a-fresh"]);++ const unreadable = provider({+ instanceId: ProviderInstanceId.make("workspace-b"),+ auth: { ...selected.auth, workspaceId: "ws-b" },+ });+ expect(+ collectProviderUsageLimits(+ selected.instanceId,+ [...natives, unreadable],+ [],+ now,+ )?.accounts.map((account) => account.id),+ ).toEqual(["workspace-a-fresh", "codex"]);+ expect(+ collectProviderUsageLimits(+ selected.instanceId,+ [...natives, { ...unreadable, usageLimits: limits }],+ [],+ now,+ )?.accounts.map((account) => account.id),+ ).toEqual(["workspace-a-fresh", "codex", "workspace-b"]);+ });+it("supports a source-only provider and keeps duplicates when the native probe failed", () => {expect(collectProviderUsageLimits(selected.instanceId, [provider({})], sources, now)?.accounts.map(diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.tsindex 2ced4094548..9073fb5dfd5 100644--- a/packages/shared/src/usageLimits.ts+++ b/packages/shared/src/usageLimits.ts@@ -662,25 +662,52 @@ export function collectProviderUsageLimits(): UsageLimitsReport | null {const selected = providers.find((provider) => provider.instanceId === instanceId);if (!selected || !hasProviderUsageLimits(selected.driver, providers, sources)) return null;- const native = providersWithLimits(providers).filter(- (provider) => provider.driver === selected.driver,- );const resolveKey = accountKeyResolver([...providers.filter((provider) => provider.enabled).map(nativeReport),...sources.flatMap((source) => source.accounts),]);- const nativeAccounts = new Set(- native.flatMap((provider) => {+ type NativeSnapshot = { provider: ServerProvider; limits: ServerProviderUsageLimits };+ const nativeByAccount = new Map<+ string,+ { usage: NativeSnapshot; credits: NativeSnapshot | null }+ >();+ for (const provider of providersWithLimits(providers)) {+ if (provider.driver !== selected.driver || !provider.usageLimits) continue;+ const snapshot = { provider, limits: provider.usageLimits };+ const key = resolveKey(nativeReport(provider)) ?? `instance:${provider.instanceId}`;+ const previous = nativeByAccount.get(key);+ const previousUsable = previous ? limitsNotice(previous.usage.limits) === null : false;+ const usable = limitsNotice(snapshot.limits) === null;+ const providerWins =+ !previous ||+ (usable && !previousUsable) ||+ (usable === previousUsable &&+ Date.parse(snapshot.limits.checkedAt) > Date.parse(previous.usage.limits.checkedAt));+ const previousCredit = previous?.credits;+ const credits =+ snapshot.limits.resetCredits &&+ (!previousCredit ||+ Date.parse(snapshot.limits.checkedAt) > Date.parse(previousCredit.limits.checkedAt))+ ? snapshot+ : (previousCredit ?? null);+ nativeByAccount.set(key, {+ usage: providerWins ? snapshot : previous.usage,+ credits,+ });+ }+ const native = [...nativeByAccount.values()];+ const nativeAccountKeys = new Set(+ native.flatMap(({ usage: { provider, limits } }) => {const key = resolveKey(nativeReport(provider));- return key && provider.usageLimits?.windows.length && !provider.usageLimits.unavailable- ? [key]- : [];+ return key && limits.windows.length && !limits.unavailable ? [key] : [];}),);const accounts: Array<UsageLimitsReport["accounts"][number]> = [];const notices: string[] = [];- for (const provider of native) {- if (!provider.usageLimits) continue;+ for (const {+ usage: { provider, limits },+ credits,+ } of native) {const key = resolveKey(nativeReport(provider));const hubCredits = sources.flatMap((source) => source.accounts.map((account) => ({ source, account })))@@ -698,9 +725,12 @@ export function collectProviderUsageLimits(const hubCreditId = hubCredits?.account.usageLimits.resetCredits?.nextCreditId;const showHubCredits =hubCredits &&- (!provider.usageLimits.resetCredits ||+ (!credits ||Date.parse(hubCredits.account.usageLimits.checkedAt) >- Date.parse(provider.usageLimits.checkedAt));+ Date.parse(credits.limits.checkedAt));+ const shownCredits = showHubCredits+ ? hubCredits.account.usageLimits.resetCredits+ : credits?.limits.resetCredits;accounts.push({id: provider.instanceId,driver: provider.driver,@@ -714,20 +744,18 @@ export function collectProviderUsageLimits(accountId: hubCredits.account.id,creditId: hubCreditId,}- : { instanceId: provider.instanceId },+ : { instanceId: credits?.provider.instanceId ?? provider.instanceId },...(provider.displayName ? { displayName: provider.displayName } : {}),...(provider.accentColor ? { accentColor: provider.accentColor } : {}),...(provider.auth.email ? { email: provider.auth.email } : {}),- limits: showHubCredits- ? { ...provider.usageLimits, resetCredits: hubCredits.account.usageLimits.resetCredits }- : provider.usageLimits,+ limits: shownCredits ? { ...limits, resetCredits: shownCredits } : limits,});}for (const source of sources) {const matching = source.accounts.filter((account) => account.driver === selected.driver);for (const account of matching) {const key = resolveKey(account);- if (key && nativeAccounts.has(key)) continue;+ if (key && nativeAccountKeys.has(key)) continue;accounts.push({id: `${source.id}:${account.id}`,driver: account.driver,