Skip to content

Commit 94dafd4

Browse files
fix(usage): preserve workspace identity when deduplicating native accounts
1 parent da848f4 commit 94dafd4

2 files changed

Lines changed: 88 additions & 42 deletions

File tree

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

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,10 +1290,14 @@ describe("/usage-limits", () => {
12901290
expect(report?.notices).toEqual([]);
12911291
});
12921292

1293-
it("keeps the freshest usable duplicate and its latest native credits", () => {
1293+
it("keeps the freshest usable native duplicate and the freshest native credits in either order", () => {
12941294
const failed = provider({
12951295
auth: selected.auth,
1296-
usageLimits: { ...limits, unavailable: { reason: "probeFailed" } },
1296+
usageLimits: {
1297+
...limits,
1298+
checkedAt: "2026-09-03T11:40:00.000Z",
1299+
unavailable: { reason: "probeFailed" },
1300+
},
12971301
});
12981302
const credited = provider({
12991303
instanceId: ProviderInstanceId.make("codex-credit"),
@@ -1304,6 +1308,11 @@ describe("/usage-limits", () => {
13041308
resetCredits: { availableCount: 2 },
13051309
},
13061310
});
1311+
const staleCredit = provider({
1312+
...credited,
1313+
instanceId: ProviderInstanceId.make("codex-stale-credit"),
1314+
usageLimits: { ...limits, resetCredits: { availableCount: 3 } },
1315+
});
13071316
const freshest = provider({
13081317
instanceId: ProviderInstanceId.make("codex-fresh"),
13091318
auth: selected.auth,
@@ -1312,21 +1321,60 @@ describe("/usage-limits", () => {
13121321
windows: [{ ...window, usedPercent: 55 }],
13131322
},
13141323
});
1315-
const report = collectProviderUsageLimits(
1316-
failed.instanceId,
1317-
[failed, credited, freshest],
1318-
sources,
1319-
now,
1320-
);
1321-
expect(report?.accounts.map((account) => account.id)).toEqual(["codex-fresh", "hub:oss"]);
1322-
expect(report?.accounts[0]).toMatchObject({
1323-
limits: {
1324-
checkedAt: "2026-09-03T11:30:00.000Z",
1325-
windows: [{ usedPercent: 55 }],
1326-
resetCredits: { availableCount: 2 },
1327-
},
1328-
resetCreditInput: { instanceId: "codex-credit" },
1324+
const duplicates = [failed, staleCredit, credited, freshest];
1325+
for (const providers of [duplicates, duplicates.toReversed()]) {
1326+
const report = collectProviderUsageLimits(failed.instanceId, providers, sources, now);
1327+
expect(report?.accounts.map((account) => account.id)).toEqual(["codex-fresh", "hub:oss"]);
1328+
expect(report?.accounts[0]).toMatchObject({
1329+
limits: {
1330+
checkedAt: "2026-09-03T11:30:00.000Z",
1331+
windows: [{ usedPercent: 55 }],
1332+
resetCredits: { availableCount: 2 },
1333+
},
1334+
resetCreditInput: { instanceId: "codex-credit" },
1335+
});
1336+
expect(report?.accounts[0]?.limits.unavailable).toBeUndefined();
1337+
}
1338+
});
1339+
1340+
it("merges native workspace duplicates only while the workspace evidence is unambiguous", () => {
1341+
const known = provider({
1342+
...selected,
1343+
instanceId: ProviderInstanceId.make("workspace-a"),
1344+
auth: { ...selected.auth, workspaceId: "ws-a" },
13291345
});
1346+
const fresh = provider({
1347+
...known,
1348+
instanceId: ProviderInstanceId.make("workspace-a-fresh"),
1349+
usageLimits: { ...limits, checkedAt: "2026-09-03T11:30:00.000Z" },
1350+
});
1351+
const natives = [known, fresh, selected];
1352+
expect(
1353+
collectProviderUsageLimits(selected.instanceId, natives, [], now)?.accounts.map(
1354+
(account) => account.id,
1355+
),
1356+
).toEqual(["workspace-a-fresh"]);
1357+
1358+
const unreadable = provider({
1359+
instanceId: ProviderInstanceId.make("workspace-b"),
1360+
auth: { ...selected.auth, workspaceId: "ws-b" },
1361+
});
1362+
expect(
1363+
collectProviderUsageLimits(
1364+
selected.instanceId,
1365+
[...natives, unreadable],
1366+
[],
1367+
now,
1368+
)?.accounts.map((account) => account.id),
1369+
).toEqual(["workspace-a-fresh", "codex"]);
1370+
expect(
1371+
collectProviderUsageLimits(
1372+
selected.instanceId,
1373+
[...natives, { ...unreadable, usageLimits: limits }],
1374+
[],
1375+
now,
1376+
)?.accounts.map((account) => account.id),
1377+
).toEqual(["workspace-a-fresh", "codex", "workspace-b"]);
13301378
});
13311379

13321380
it("supports a source-only provider and keeps duplicates when the native probe failed", () => {

‎packages/shared/src/usageLimits.ts‎

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -657,48 +657,48 @@ export function collectProviderUsageLimits(
657657
...providers.filter((provider) => provider.enabled).map(nativeReport),
658658
...sources.flatMap((source) => source.accounts),
659659
]);
660+
type NativeSnapshot = { provider: ServerProvider; limits: ServerProviderUsageLimits };
660661
const nativeByAccount = new Map<
661662
string,
662-
{ provider: ServerProvider; creditProvider: ServerProvider | null }
663+
{ usage: NativeSnapshot; credits: NativeSnapshot | null }
663664
>();
664665
for (const provider of providersWithLimits(providers)) {
665666
if (provider.driver !== selected.driver || !provider.usageLimits) continue;
667+
const snapshot = { provider, limits: provider.usageLimits };
666668
const key = resolveKey(nativeReport(provider)) ?? `instance:${provider.instanceId}`;
667669
const previous = nativeByAccount.get(key);
668-
const previousUsable = previous ? limitsNotice(previous.provider.usageLimits!) === null : false;
669-
const usable = limitsNotice(provider.usageLimits) === null;
670+
const previousUsable = previous ? limitsNotice(previous.usage.limits) === null : false;
671+
const usable = limitsNotice(snapshot.limits) === null;
670672
const providerWins =
671673
!previous ||
672674
(usable && !previousUsable) ||
673675
(usable === previousUsable &&
674-
Date.parse(provider.usageLimits.checkedAt) >
675-
Date.parse(previous.provider.usageLimits!.checkedAt));
676-
const previousCredit = previous?.creditProvider;
677-
const creditProvider =
678-
provider.usageLimits.resetCredits &&
676+
Date.parse(snapshot.limits.checkedAt) > Date.parse(previous.usage.limits.checkedAt));
677+
const previousCredit = previous?.credits;
678+
const credits =
679+
snapshot.limits.resetCredits &&
679680
(!previousCredit ||
680-
Date.parse(provider.usageLimits.checkedAt) >
681-
Date.parse(previousCredit.usageLimits!.checkedAt))
682-
? provider
681+
Date.parse(snapshot.limits.checkedAt) > Date.parse(previousCredit.limits.checkedAt))
682+
? snapshot
683683
: (previousCredit ?? null);
684684
nativeByAccount.set(key, {
685-
provider: providerWins ? provider : previous.provider,
686-
creditProvider,
685+
usage: providerWins ? snapshot : previous.usage,
686+
credits,
687687
});
688688
}
689689
const native = [...nativeByAccount.values()];
690690
const nativeAccountKeys = new Set(
691-
native.flatMap(({ provider }) => {
691+
native.flatMap(({ usage: { provider, limits } }) => {
692692
const key = resolveKey(nativeReport(provider));
693-
return key && provider.usageLimits?.windows.length && !provider.usageLimits.unavailable
694-
? [key]
695-
: [];
693+
return key && limits.windows.length && !limits.unavailable ? [key] : [];
696694
}),
697695
);
698696
const accounts: Array<UsageLimitsReport["accounts"][number]> = [];
699697
const notices: string[] = [];
700-
for (const { provider, creditProvider } of native) {
701-
if (!provider.usageLimits) continue;
698+
for (const {
699+
usage: { provider, limits },
700+
credits,
701+
} of native) {
702702
const key = resolveKey(nativeReport(provider));
703703
const hubCredits = sources
704704
.flatMap((source) => source.accounts.map((account) => ({ source, account })))
@@ -716,12 +716,12 @@ export function collectProviderUsageLimits(
716716
const hubCreditId = hubCredits?.account.usageLimits.resetCredits?.nextCreditId;
717717
const showHubCredits =
718718
hubCredits &&
719-
(!creditProvider ||
719+
(!credits ||
720720
Date.parse(hubCredits.account.usageLimits.checkedAt) >
721-
Date.parse(creditProvider.usageLimits!.checkedAt));
721+
Date.parse(credits.limits.checkedAt));
722722
const shownCredits = showHubCredits
723723
? hubCredits.account.usageLimits.resetCredits
724-
: creditProvider?.usageLimits?.resetCredits;
724+
: credits?.limits.resetCredits;
725725
accounts.push({
726726
id: provider.instanceId,
727727
driver: provider.driver,
@@ -735,13 +735,11 @@ export function collectProviderUsageLimits(
735735
accountId: hubCredits.account.id,
736736
creditId: hubCreditId,
737737
}
738-
: { instanceId: creditProvider?.instanceId ?? provider.instanceId },
738+
: { instanceId: credits?.provider.instanceId ?? provider.instanceId },
739739
...(provider.displayName ? { displayName: provider.displayName } : {}),
740740
...(provider.accentColor ? { accentColor: provider.accentColor } : {}),
741741
...(provider.auth.email ? { email: provider.auth.email } : {}),
742-
limits: shownCredits
743-
? { ...provider.usageLimits, resetCredits: shownCredits }
744-
: provider.usageLimits,
742+
limits: shownCredits ? { ...limits, resetCredits: shownCredits } : limits,
745743
});
746744
}
747745
for (const source of sources) {

0 commit comments

Comments
 (0)