GeezerVison is showing github.com — fix(usage): held accounts count down to the reset that frees them · pingdotgg/t3code@91ac573
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/shared/src/usageLimits.test.ts +18-3Lines changed: 18 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1156,7 +1156,22 @@ describe("pooled binding limits", () => { | 1156 | 1156 | }); | 1157 | 1157 | }); | 1158 | 1158 |
| 1159 | | - it("restores nothing while another binding weekly is still spent", () => { | | 1159 | + it("counts down to its own reset when a spent session outlasts its spent weekly", () => { | | 1160 | + const [pool] = collectLimitPools( | | 1161 | + [ | | 1162 | + account("A", [ | | 1163 | + { ...window, usedPercent: 100, resetsAt: resetIn(240) }, | | 1164 | + { ...weekly, usedPercent: 100, resetsAt: resetIn(60) }, | | 1165 | + ]), | | 1166 | + ], | | 1167 | + now, | | 1168 | + ); | | 1169 | + const session = pool!.windows[0]!; | | 1170 | + expect(session.members[0]).toMatchObject({ effectiveRemainingPercent: 0, holdingWindow: null }); | | 1171 | + expect(session.resets).toMatchObject([{ at: Date.parse(resetIn(240)), restoresPercent: 100 }]); | | 1172 | + }); | | 1173 | + | | 1174 | + it("counts down to the last of several spent binding weeklies", () => { | 1160 | 1175 | const [pool] = collectLimitPools( | 1161 | 1176 | [ | 1162 | 1177 | account( | @@ -1173,9 +1188,9 @@ describe("pooled binding limits", () => { | 1173 | 1188 | ); | 1174 | 1189 | expect(pool!.windows[0]).toMatchObject({ | 1175 | 1190 | remainingPercent: 0, | 1176 | | - resets: [{ at: Date.parse(resetIn(120)), restoresPercent: 0 }], | | 1191 | + members: [{ holdingWindow: { id: "other" } }], | | 1192 | + resets: [{ at: Date.parse(resetIn(180)), restoresPercent: 100 }], | 1177 | 1193 | }); | 1178 | | - expect(pool!.windows[0]!.resets.find((reset) => reset.restoresPercent > 0)).toBeUndefined(); | 1179 | 1194 | }); | 1180 | 1195 | }); | 1181 | 1196 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/src/usageLimits.ts +24-5Lines changed: 24 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -364,7 +364,7 @@ export interface LimitPoolMember { | 364 | 364 | readonly window: ServerProviderUsageWindow; | 365 | 365 | /** Raw remaining quota, or zero while a broader allowance is used up. */ | 366 | 366 | readonly effectiveRemainingPercent: number; | 367 | | - /** A broader allowance that is used up, or null when this window is unheld. */ | | 367 | + /** The used-up broader allowance whose reset frees this window, or null when none holds it. */ | 368 | 368 | readonly holdingWindow: ServerProviderUsageWindow | null; | 369 | 369 | } | 370 | 370 |
| @@ -514,11 +514,30 @@ function quotaRemaining(window: ServerProviderUsageWindow): number { | 514 | 514 | return 100 - Math.max(0, Math.min(100, window.usedPercent)); | 515 | 515 | } | 516 | 516 |
| | 517 | +/** | | 518 | + * The account spends again only once every used-up window has reset, so the | | 519 | + * last of those resets is its countdown. A broader window holds it when it | | 520 | + * is that last reset; a session that is spent past it holds itself. | | 521 | + */ | 517 | 522 | function poolMember(account: LimitAccount, window: ServerProviderUsageWindow): LimitPoolMember { | 518 | | - const holdingWindow = | 519 | | - bindingWindows(account, window).find((broader) => broader.usedPercent >= 100) ?? null; | 520 | | - const effectiveRemainingPercent = holdingWindow ? 0 : quotaRemaining(window); | 521 | | - return { account, window, effectiveRemainingPercent, holdingWindow }; | | 523 | + const lastReset = (w: ServerProviderUsageWindow) => resetMillis(w) ?? Number.POSITIVE_INFINITY; | | 524 | + const spent = bindingWindows(account, window).filter((broader) => broader.usedPercent >= 100); | | 525 | + if (spent.length === 0) { | | 526 | + return { | | 527 | + account, | | 528 | + window, | | 529 | + effectiveRemainingPercent: quotaRemaining(window), | | 530 | + holdingWindow: null, | | 531 | + }; | | 532 | + } | | 533 | + const latest = spent.reduce((a, b) => (lastReset(b) > lastReset(a) ? b : a)); | | 534 | + const selfLater = window.usedPercent >= 100 && lastReset(window) > lastReset(latest); | | 535 | + return { | | 536 | + account, | | 537 | + window, | | 538 | + effectiveRemainingPercent: 0, | | 539 | + holdingWindow: selfLater ? null : latest, | | 540 | + }; | 522 | 541 | } | 523 | 542 |
| 524 | 543 | function poolWindows(accounts: readonly LimitAccount[], now: number): readonly LimitPoolWindow[] { | |
|