Skip to content

Commit 780b62d

Browse files
fix(usage): deduplicate native subscription accounts (pingdotgg#10700)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 284ce8f commit 780b62d

2 files changed

Lines changed: 133 additions & 18 deletions

File tree

‎packages/shared/src/usageLimits.test.ts‎

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,7 @@ describe("/usage-limits", () => {
12581258
expect(report?.accounts[0]?.limits.resetCredits?.availableCount).toBe(3);
12591259
});
12601260

1261-
it("keeps accounts and custom instances separate, filtering by driver", () => {
1261+
it("keeps unidentified custom instances separate, filtering by driver", () => {
12621262
const report = collectProviderUsageLimits(
12631263
selected.instanceId,
12641264
[
@@ -1299,6 +1299,93 @@ describe("/usage-limits", () => {
12991299
expect(report?.notices).toEqual([]);
13001300
});
13011301

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+
13021389
it("supports a source-only provider and keeps duplicates when the native probe failed", () => {
13031390
expect(
13041391
collectProviderUsageLimits(selected.instanceId, [provider({})], sources, now)?.accounts.map(

‎packages/shared/src/usageLimits.ts‎

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -662,25 +662,52 @@ export function collectProviderUsageLimits(
662662
): UsageLimitsReport | null {
663663
const selected = providers.find((provider) => provider.instanceId === instanceId);
664664
if (!selected || !hasProviderUsageLimits(selected.driver, providers, sources)) return null;
665-
const native = providersWithLimits(providers).filter(
666-
(provider) => provider.driver === selected.driver,
667-
);
668665
const resolveKey = accountKeyResolver([
669666
...providers.filter((provider) => provider.enabled).map(nativeReport),
670667
...sources.flatMap((source) => source.accounts),
671668
]);
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 } }) => {
674701
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] : [];
678703
}),
679704
);
680705
const accounts: Array<UsageLimitsReport["accounts"][number]> = [];
681706
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) {
684711
const key = resolveKey(nativeReport(provider));
685712
const hubCredits = sources
686713
.flatMap((source) => source.accounts.map((account) => ({ source, account })))
@@ -698,9 +725,12 @@ export function collectProviderUsageLimits(
698725
const hubCreditId = hubCredits?.account.usageLimits.resetCredits?.nextCreditId;
699726
const showHubCredits =
700727
hubCredits &&
701-
(!provider.usageLimits.resetCredits ||
728+
(!credits ||
702729
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;
704734
accounts.push({
705735
id: provider.instanceId,
706736
driver: provider.driver,
@@ -714,20 +744,18 @@ export function collectProviderUsageLimits(
714744
accountId: hubCredits.account.id,
715745
creditId: hubCreditId,
716746
}
717-
: { instanceId: provider.instanceId },
747+
: { instanceId: credits?.provider.instanceId ?? provider.instanceId },
718748
...(provider.displayName ? { displayName: provider.displayName } : {}),
719749
...(provider.accentColor ? { accentColor: provider.accentColor } : {}),
720750
...(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,
724752
});
725753
}
726754
for (const source of sources) {
727755
const matching = source.accounts.filter((account) => account.driver === selected.driver);
728756
for (const account of matching) {
729757
const key = resolveKey(account);
730-
if (key && nativeAccounts.has(key)) continue;
758+
if (key && nativeAccountKeys.has(key)) continue;
731759
accounts.push({
732760
id: `${source.id}:${account.id}`,
733761
driver: account.driver,

0 commit comments

Comments
 (0)