The best way to control Claude Code, Codex and more from any computer or phone
fix(usage): deduplicate native subscription accounts (#10700)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Stephen Jason Wang committed
Oct 20, 2026 at 04:29 UTC
780b62dd26c4b897ec43b095911ff6659c48da3c
2 files changed
+133
-18
packages/shared/src/usageLimits.test.ts
+88
-1
@@ -1258,7 +1258,7 @@ describe("/usage-limits", () => {
1258
expect(report?.accounts[0]?.limits.resetCredits?.availableCount).toBe(3);
1259
});
1260
1261
- it("keeps accounts and custom instances separate, filtering by driver", () => {
1261
+ it("keeps unidentified custom instances separate, filtering by driver", () => {
1262
const report = collectProviderUsageLimits(
1263
selected.instanceId,
1264
[
@@ -1299,6 +1299,93 @@ describe("/usage-limits", () => {
1299
expect(report?.notices).toEqual([]);
1300
});
1301
1302
+ it("keeps the freshest usable native duplicate and the freshest native credits in either order", () => {
1303
+ const failed = provider({
1304
+ auth: selected.auth,
1305
+ usageLimits: {
1306
+ ...limits,
1307
+ checkedAt: "2026-09-03T11:40:00.000Z",
1308
+ unavailable: { reason: "probeFailed" },
1309
+ },
1310
+ });
1311
+ const credited = provider({
1312
+ instanceId: ProviderInstanceId.make("codex-credit"),
1313
+ auth: selected.auth,
1314
+ usageLimits: {
1315
+ checkedAt: "2026-09-03T11:20:00.000Z",
1316
+ windows: [{ ...window, usedPercent: 50 }],
1317
+ resetCredits: { availableCount: 2 },
1318
+ },
1319
+ });
1320
+ const staleCredit = provider({
1321
+ ...credited,
1322
+ instanceId: ProviderInstanceId.make("codex-stale-credit"),
1323
+ usageLimits: { ...limits, resetCredits: { availableCount: 3 } },
1324
+ });
1325
+ const freshest = provider({
1326
+ instanceId: ProviderInstanceId.make("codex-fresh"),
1327
+ auth: selected.auth,
1328
+ usageLimits: {
1329
+ checkedAt: "2026-09-03T11:30:00.000Z",
1330
+ windows: [{ ...window, usedPercent: 55 }],
1331
+ },
1332
+ });
1333
+ const duplicates = [failed, staleCredit, credited, freshest];
1334
+ for (const providers of [duplicates, duplicates.toReversed()]) {
1335
+ const report = collectProviderUsageLimits(failed.instanceId, providers, sources, now);
1336
+ expect(report?.accounts.map((account) => account.id)).toEqual(["codex-fresh", "hub:oss"]);
1337
+ expect(report?.accounts[0]).toMatchObject({
1338
+ limits: {
1339
+ checkedAt: "2026-09-03T11:30:00.000Z",
1340
+ windows: [{ usedPercent: 55 }],
1341
+ resetCredits: { availableCount: 2 },
1342
+ },
1343
+ resetCreditInput: { instanceId: "codex-credit" },
1344
+ });
1345
+ expect(report?.accounts[0]?.limits.unavailable).toBeUndefined();
1346
+ }
1347
+ });
1348
+
1349
+ it("merges native workspace duplicates only while the workspace evidence is unambiguous", () => {
1350
+ const known = provider({
1351
+ ...selected,
1352
+ instanceId: ProviderInstanceId.make("workspace-a"),
1353
+ auth: { ...selected.auth, workspaceId: "ws-a" },
1354
+ });
1355
+ const fresh = provider({
1356
+ ...known,
1357
+ instanceId: ProviderInstanceId.make("workspace-a-fresh"),
1358
+ usageLimits: { ...limits, checkedAt: "2026-09-03T11:30:00.000Z" },
1359
+ });
1360
+ const natives = [known, fresh, selected];
1361
+ expect(
1362
+ collectProviderUsageLimits(selected.instanceId, natives, [], now)?.accounts.map(
1363
+ (account) => account.id,
1364
+ ),
1365
+ ).toEqual(["workspace-a-fresh"]);
1366
+
1367
+ const unreadable = provider({
1368
+ instanceId: ProviderInstanceId.make("workspace-b"),
1369
+ auth: { ...selected.auth, workspaceId: "ws-b" },
1370
+ });
1371
+ expect(
1372
+ collectProviderUsageLimits(
1373
+ selected.instanceId,
1374
+ [...natives, unreadable],
1375
+ [],
1376
+ now,
1377
+ )?.accounts.map((account) => account.id),
1378
+ ).toEqual(["workspace-a-fresh", "codex"]);
1379
+ expect(
1380
+ collectProviderUsageLimits(
1381
+ selected.instanceId,
1382
+ [...natives, { ...unreadable, usageLimits: limits }],
1383
+ [],
1384
+ now,
1385
+ )?.accounts.map((account) => account.id),
1386
+ ).toEqual(["workspace-a-fresh", "codex", "workspace-b"]);
1387
+ });
1388
+
1389
it("supports a source-only provider and keeps duplicates when the native probe failed", () => {
1390
expect(
1391
collectProviderUsageLimits(selected.instanceId, [provider({})], sources, now)?.accounts.map(
packages/shared/src/usageLimits.ts
+45
-17
@@ -662,25 +662,52 @@ export function collectProviderUsageLimits(
662
): UsageLimitsReport | null {
663
const selected = providers.find((provider) => provider.instanceId === instanceId);
664
if (!selected || !hasProviderUsageLimits(selected.driver, providers, sources)) return null;
665
- const native = providersWithLimits(providers).filter(
666
- (provider) => provider.driver === selected.driver,
667
- );
665
const resolveKey = accountKeyResolver([
666
...providers.filter((provider) => provider.enabled).map(nativeReport),
667
...sources.flatMap((source) => source.accounts),
668
]);
672
- const nativeAccounts = new Set(
673
- native.flatMap((provider) => {
669
+ type NativeSnapshot = { provider: ServerProvider; limits: ServerProviderUsageLimits };
670
+ const nativeByAccount = new Map<
671
+ string,
672
+ { usage: NativeSnapshot; credits: NativeSnapshot | null }
673
+ >();
674
+ for (const provider of providersWithLimits(providers)) {
675
+ if (provider.driver !== selected.driver || !provider.usageLimits) continue;
676
+ const snapshot = { provider, limits: provider.usageLimits };
677
+ const key = resolveKey(nativeReport(provider)) ?? `instance:${provider.instanceId}`;
678
+ const previous = nativeByAccount.get(key);
679
+ const previousUsable = previous ? limitsNotice(previous.usage.limits) === null : false;
680
+ const usable = limitsNotice(snapshot.limits) === null;
681
+ const providerWins =
682
+ !previous ||
683
+ (usable && !previousUsable) ||
684
+ (usable === previousUsable &&
685
+ Date.parse(snapshot.limits.checkedAt) > Date.parse(previous.usage.limits.checkedAt));
686
+ const previousCredit = previous?.credits;
687
+ const credits =
688
+ snapshot.limits.resetCredits &&
689
+ (!previousCredit ||
690
+ Date.parse(snapshot.limits.checkedAt) > Date.parse(previousCredit.limits.checkedAt))
691
+ ? snapshot
692
+ : (previousCredit ?? null);
693
+ nativeByAccount.set(key, {
694
+ usage: providerWins ? snapshot : previous.usage,
695
+ credits,
696
+ });
697
+ }
698
+ const native = [...nativeByAccount.values()];
699
+ const nativeAccountKeys = new Set(
700
+ native.flatMap(({ usage: { provider, limits } }) => {
701
const key = resolveKey(nativeReport(provider));
675
- return key && provider.usageLimits?.windows.length && !provider.usageLimits.unavailable
676
- ? [key]
677
- : [];
702
+ return key && limits.windows.length && !limits.unavailable ? [key] : [];
703
}),
704
);
705
const accounts: Array<UsageLimitsReport["accounts"][number]> = [];
706
const notices: string[] = [];
682
- for (const provider of native) {
683
- if (!provider.usageLimits) continue;
707
+ for (const {
708
+ usage: { provider, limits },
709
+ credits,
710
+ } of native) {
711
const key = resolveKey(nativeReport(provider));
712
const hubCredits = sources
713
.flatMap((source) => source.accounts.map((account) => ({ source, account })))
@@ -698,9 +725,12 @@ export function collectProviderUsageLimits(
725
const hubCreditId = hubCredits?.account.usageLimits.resetCredits?.nextCreditId;
726
const showHubCredits =
727
hubCredits &&
701
- (!provider.usageLimits.resetCredits ||
728
+ (!credits ||
729
Date.parse(hubCredits.account.usageLimits.checkedAt) >
703
- Date.parse(provider.usageLimits.checkedAt));
730
+ Date.parse(credits.limits.checkedAt));
731
+ const shownCredits = showHubCredits
732
+ ? hubCredits.account.usageLimits.resetCredits
733
+ : credits?.limits.resetCredits;
734
accounts.push({
735
id: provider.instanceId,
736
driver: provider.driver,
@@ -714,20 +744,18 @@ export function collectProviderUsageLimits(
744
accountId: hubCredits.account.id,
745
creditId: hubCreditId,
746
}
717
- : { instanceId: provider.instanceId },
747
+ : { instanceId: credits?.provider.instanceId ?? provider.instanceId },
748
...(provider.displayName ? { displayName: provider.displayName } : {}),
749
...(provider.accentColor ? { accentColor: provider.accentColor } : {}),
750
...(provider.auth.email ? { email: provider.auth.email } : {}),
721
- limits: showHubCredits
722
- ? { ...provider.usageLimits, resetCredits: hubCredits.account.usageLimits.resetCredits }
723
- : provider.usageLimits,
751
+ limits: shownCredits ? { ...limits, resetCredits: shownCredits } : limits,
752
});
753
}
754
for (const source of sources) {
755
const matching = source.accounts.filter((account) => account.driver === selected.driver);
756
for (const account of matching) {
757
const key = resolveKey(account);
730
- if (key && nativeAccounts.has(key)) continue;
758
+ if (key && nativeAccountKeys.has(key)) continue;
759
accounts.push({
760
id: `${source.id}:${account.id}`,
761
driver: account.driver,