Skip to content

Commit 7bce616

Browse files
committed
fix(usage): retain quota windows across credit-only snapshots
1 parent e0f3e51 commit 7bce616

2 files changed

Lines changed: 47 additions & 8 deletions

File tree

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,20 @@ describe("credit balances", () => {
186186
).toBe("20");
187187
expect(collectLimitAccounts(makePresentations(null))[0]?.limits.credits).toBeNull();
188188
});
189-
it.each(["native", "source"])(
190-
"honors a newer %s clear without windows and retains another environment's quota",
191-
(kind) => {
189+
it.each([
190+
["native", null],
191+
["source", null],
192+
["native", { hasCredits: true, unlimited: false, balance: "20" }],
193+
["source", { hasCredits: true, unlimited: false, balance: "20" }],
194+
] as const)(
195+
"honors a newer %s credit-only snapshot and retains the freshest quota windows",
196+
(kind, credits) => {
192197
const oldLimits = {
193198
checkedAt: "2026-09-03T10:00:00.000Z",
194199
windows: [window],
195200
credits: { hasCredits: true, unlimited: false, balance: "42" },
196201
};
197-
const cleared = { checkedAt: "2026-09-03T11:00:00.000Z", windows: [], credits: null };
202+
const cleared = { checkedAt: "2026-09-03T11:00:00.000Z", windows: [], credits };
198203
const auth = { status: "authenticated", email: "[email protected]" } as const;
199204
const newer = {
200205
entry: { target: { label: "Laptop" } },
@@ -228,21 +233,44 @@ describe("credit balances", () => {
228233
usageLimitSources: [],
229234
},
230235
};
236+
const middle = {
237+
entry: { target: { label: "Tablet" } },
238+
serverConfig: {
239+
providers: [
240+
provider({
241+
auth,
242+
usageLimits: {
243+
checkedAt: "2026-09-03T10:30:00.000Z",
244+
windows: [{ ...window, usedPercent: 60 }],
245+
},
246+
}),
247+
],
248+
usageLimitSources: [],
249+
},
250+
};
231251
const entries = [
232252
[EnvironmentId.make("desktop"), older],
233253
[EnvironmentId.make("laptop"), newer],
254+
[EnvironmentId.make("tablet"), middle],
234255
] as const;
235256
for (const order of [entries, entries.toReversed()]) {
236257
const accounts = collectLimitAccounts(new Map(order));
237258
expect(accounts).toHaveLength(1);
238-
expect(accounts[0]?.limits.credits).toBeNull();
239-
expect(accounts[0]?.limits.windows).toEqual([window]);
259+
expect(accounts[0]?.limits.credits).toEqual(credits);
260+
expect(accounts[0]?.limits.windows).toEqual([{ ...window, usedPercent: 60 }]);
261+
}
262+
const lone = collectLimitAccounts(new Map([[EnvironmentId.make("laptop"), newer]]));
263+
expect(lone).toHaveLength(credits === null ? 0 : 1);
264+
if (credits !== null) {
265+
expect(lone[0]?.limits.credits).toEqual(credits);
266+
expect(lone[0]?.limits.windows).toEqual([]);
240267
}
241-
expect(collectLimitAccounts(new Map([[EnvironmentId.make("laptop"), newer]]))).toEqual([]);
242268
older.serverConfig.providers = [
243269
provider({ auth, usageLimits: { ...oldLimits, windows: [] } }),
244270
];
245-
expect(collectLimitAccounts(new Map(entries))).toEqual([]);
271+
const withoutWindows = collectLimitAccounts(new Map(entries.slice(0, 2)));
272+
expect(withoutWindows).toHaveLength(credits === null ? 0 : 1);
273+
expect(withoutWindows[0]?.limits.windows ?? []).toEqual([]);
246274
},
247275
);
248276
});

‎packages/shared/src/usageLimits.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl
204204
const accounts = new Map<string, LimitAccount>();
205205
const creditSources = new Map<string, LimitAccount>();
206206
const balanceSources = new Map<string, LimitAccount>();
207+
const windowSources = new Map<string, LimitAccount>();
207208
const hubRedeems = new Map<string, LimitAccount>();
208209
const merge = (key: string, next: LimitAccount) => {
209210
const previousBalance = balanceSources.get(key);
@@ -218,6 +219,15 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl
218219
// A clear without windows still invalidates older balances from another environment.
219220
if (limitsNotice(next.limits) !== null) return;
220221

222+
const previousWindow = windowSources.get(key);
223+
if (
224+
next.limits.windows.length > 0 &&
225+
(!previousWindow ||
226+
Date.parse(next.limits.checkedAt) > Date.parse(previousWindow.limits.checkedAt))
227+
) {
228+
windowSources.set(key, next);
229+
}
230+
221231
// Redeeming through a hub also clears the routing cooldown that hub holds
222232
// for the account. Redeeming natively against the same subscription resets
223233
// it upstream but leaves the hub refusing to route to the account until
@@ -270,6 +280,7 @@ export function collectLimitAccounts(presentations: LimitPresentations): readonl
270280
(creditSource ? creditSource.redeem : (winner.redeem ?? previous.redeem ?? next.redeem)),
271281
limits: {
272282
...winner.limits,
283+
windows: windowSources.get(key)?.limits.windows ?? winner.limits.windows,
273284
...(creditSource?.limits.resetCredits
274285
? { resetCredits: creditSource.limits.resetCredits }
275286
: { resetCredits: undefined }),

0 commit comments

Comments
 (0)