Skip to content

Commit 7b6f791

Browse files
joh-kleinclaude
andcommitted
fix(shared): rank clean reads over failed reads with windows when merging accounts
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 54a3af3 commit 7b6f791

2 files changed

Lines changed: 68 additions & 40 deletions

File tree

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

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -189,41 +189,38 @@ describe("pools", () => {
189189
auth: { status: "authenticated", email: "[email protected]" },
190190
usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 40 }] },
191191
});
192-
const input = new Map([
193-
[EnvironmentId.make("env-a"), { ...laptop, serverConfig: { providers: [native] } }],
194-
[
195-
EnvironmentId.make("env-b"),
196-
{
197-
entry: { target: { label: "Desktop" } },
198-
serverConfig: {
199-
providers: [
200-
{
201-
...native,
202-
usageLimits: {
203-
checkedAt: "2026-09-03T11:30:00.000Z",
204-
windows: [{ ...window, usedPercent: 55 }],
192+
const newer: NonNullable<ServerProvider["usageLimits"]> = {
193+
checkedAt: "2026-09-03T11:30:00.000Z",
194+
windows: [{ ...window, usedPercent: 55 }],
195+
};
196+
const inputWith = (newerLimits: NonNullable<ServerProvider["usageLimits"]>) =>
197+
new Map([
198+
[EnvironmentId.make("env-a"), { ...laptop, serverConfig: { providers: [native] } }],
199+
[
200+
EnvironmentId.make("env-b"),
201+
{
202+
entry: { target: { label: "Desktop" } },
203+
serverConfig: {
204+
providers: [{ ...native, usageLimits: newerLimits }],
205+
usageLimitSources: [
206+
{
207+
...source,
208+
accounts: [
209+
{
210+
211+
driver: claude,
212+
213+
plan: "Claude Subscription",
214+
usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 10 }] },
215+
},
216+
],
205217
},
206-
},
207-
],
208-
usageLimitSources: [
209-
{
210-
...source,
211-
accounts: [
212-
{
213-
214-
driver: claude,
215-
216-
plan: "Claude Subscription",
217-
usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 10 }] },
218-
},
219-
],
220-
},
221-
],
218+
],
219+
},
222220
},
223-
},
224-
],
225-
]);
226-
const accounts = collectLimitAccounts(input);
221+
],
222+
]);
223+
const accounts = collectLimitAccounts(inputWith(newer));
227224
expect(accounts).toHaveLength(1);
228225
expect(accounts[0]).toMatchObject({
229226
key: "env-a:claude",
@@ -235,6 +232,11 @@ describe("pools", () => {
235232
],
236233
});
237234
expect(accounts[0]?.limits.windows[0]?.usedPercent).toBe(55);
235+
const [clean] = collectLimitAccounts(
236+
inputWith({ ...newer, unavailable: { reason: "probeFailed" } }),
237+
);
238+
expect(clean?.limits).toMatchObject({ windows: [{ usedPercent: 40 }] });
239+
expect(clean?.limits.unavailable).toBeUndefined();
238240
});
239241

240242
it("uses the freshest reported plan even when the newest quota has no plan", () => {
@@ -1349,6 +1351,22 @@ describe("/usage-limits", () => {
13491351
const failed = provider({ usageLimits: { ...limits, unavailable: { reason: "probeFailed" } } });
13501352
expect(limitsNotice(failed.usageLimits!)).toBeNull();
13511353
expect(limitsNotice({ ...failed.usageLimits!, windows: [] })).toBe("Could not read limits.");
1354+
const newerEmpty = provider({
1355+
usageLimits: {
1356+
checkedAt: "2026-09-03T11:59:00.000Z",
1357+
windows: [],
1358+
unavailable: { reason: "probeFailed" },
1359+
},
1360+
});
1361+
for (const duplicates of [
1362+
[failed, newerEmpty],
1363+
[newerEmpty, failed],
1364+
]) {
1365+
expect(
1366+
collectProviderUsageLimits(selected.instanceId, duplicates, [], now)?.accounts[0]?.limits
1367+
.windows,
1368+
).toEqual(limits.windows);
1369+
}
13521370
expect(
13531371
collectProviderUsageLimits(selected.instanceId, [failed], sources, now)?.accounts.map(
13541372
(account) => account.id,

‎packages/shared/src/usageLimits.ts‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,16 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl
298298
!previous.environments.some((seen) => seen.environmentId === candidate.environmentId),
299299
),
300300
];
301-
const winner = fresher ? next : previous;
301+
const nextRank = readRank(next.limits);
302+
const previousRank = readRank(previous.limits);
303+
const winner =
304+
nextRank === previousRank
305+
? fresher
306+
? next
307+
: previous
308+
: nextRank > previousRank
309+
? next
310+
: previous;
302311
const creditSource = creditSources.get(key);
303312
// A hub e-mail names the account better than the driver's default instance name.
304313
const hubEmail = [previous, next].some(
@@ -624,8 +633,10 @@ export function limitsNotice(limits: ServerProviderUsageLimits): string | null {
624633
return limits.windows.length === 0 ? "No limits reported." : null;
625634
}
626635

627-
function isCleanRead(limits: ServerProviderUsageLimits): boolean {
628-
return !limits.unavailable && limitsNotice(limits) === null;
636+
/** Clean read (2) beats a failed probe that still holds streamed windows (1) beats no windows (0). */
637+
function readRank(limits: ServerProviderUsageLimits): number {
638+
if (limitsNotice(limits) !== null) return 0;
639+
return limits.unavailable ? 1 : 2;
629640
}
630641

631642
export function remainingPercent(window: ServerProviderUsageWindow): number {
@@ -776,12 +787,11 @@ export function collectProviderUsageLimits(
776787
const snapshot = { provider, limits: provider.usageLimits };
777788
const key = resolveKey(nativeReport(provider)) ?? `instance:${provider.instanceId}`;
778789
const previous = nativeByAccount.get(key);
779-
const previousUsable = previous ? isCleanRead(previous.usage.limits) : false;
780-
const usable = isCleanRead(snapshot.limits);
790+
const rank = readRank(snapshot.limits);
781791
const providerWins =
782792
!previous ||
783-
(usable && !previousUsable) ||
784-
(usable === previousUsable &&
793+
rank > readRank(previous.usage.limits) ||
794+
(rank === readRank(previous.usage.limits) &&
785795
Date.parse(snapshot.limits.checkedAt) > Date.parse(previous.usage.limits.checkedAt));
786796
const previousCredit = previous?.credits;
787797
const credits =

0 commit comments

Comments
 (0)