GeezerVison is showing github.com — fix(server): avoid replaying stale Codex usage limits (#26331) · pingdotgg/t3code@c3f5e36
File treeExpand file treeCollapse file tree Open diff view settings orchestration-v2/Adapters
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4304,7 +4304,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( | 4304 | 4304 |
| 4305 | 4305 | yield* client.handleServerNotification("account/rateLimits/updated", (payload) => | 4306 | 4306 | Effect.gen(function* () { | 4307 | | - yield* Ref.update(rateLimitSnapshot, (previous) => | | 4307 | + const previous = yield* Ref.getAndUpdate(rateLimitSnapshot, (previous) => | 4308 | 4308 | mergeCodexRateLimits(previous, payload.rateLimits), | 4309 | 4309 | ); | 4310 | 4310 | const resetAt = codexUsageLimitResetAt(yield* Ref.get(rateLimitSnapshot)); | @@ -4324,7 +4324,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( | 4324 | 4324 | turnItem: updated, | 4325 | 4325 | }); | 4326 | 4326 | } | 4327 | | - const update = codexRateLimitsToUpdate(payload.rateLimits); | | 4327 | + const update = codexRateLimitsToUpdate(payload.rateLimits, previous); | 4328 | 4328 | if (update && adapterOptions.onUsageLimits) { | 4329 | 4329 | const checkedAt = DateTime.formatIso(yield* DateTime.now); | 4330 | 4330 | yield* adapterOptions.onUsageLimits({ ...update, checkedAt }); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/codexUsageLimits.test.ts +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -128,6 +128,29 @@ describe("codexRateLimitsToLimits", () => { | 128 | 128 | }); | 129 | 129 |
| 130 | 130 | describe("codexRateLimitsToUpdate", () => { | | 131 | + it("does not republish cached windows over a newer account probe", () => { | | 132 | + const cached = { | | 133 | + primary: { usedPercent: 0, resetsAt: 1_784_000_000, windowDurationMins: 300 }, | | 134 | + secondary: { usedPercent: 20, resetsAt: 1_784_500_000, windowDurationMins: 10080 }, | | 135 | + }; | | 136 | + expect(codexRateLimitsToUpdate(cached)?.windows).toHaveLength(2); | | 137 | + expect(codexRateLimitsToUpdate(structuredClone(cached), cached)).toBeUndefined(); | | 138 | + | | 139 | + const changed = { ...cached, secondary: { ...cached.secondary, usedPercent: 21 } }; | | 140 | + expect(codexRateLimitsToUpdate(changed, cached)?.windows.map((window) => window.id)).toEqual([ | | 141 | + "secondary", | | 142 | + ]); | | 143 | + expect(codexRateLimitsToUpdate({ primary: cached.primary }, cached)).toBeUndefined(); | | 144 | + expect( | | 145 | + codexRateLimitsToUpdate({ primary: { ...cached.primary, resetsAt: 1_784_001_000 } }, cached) | | 146 | + ?.windows, | | 147 | + ).toHaveLength(1); | | 148 | + expect( | | 149 | + codexRateLimitsToUpdate({ primary: { ...cached.primary, usedPercent: 76 } }, cached) | | 150 | + ?.windows[0]?.usedPercent, | | 151 | + ).toBe(76); | | 152 | + }); | | 153 | + | 131 | 154 | it("carries only the windows the notification names", () => { | 132 | 155 | expect( | 133 | 156 | codexRateLimitsToUpdate({ | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/codexUsageLimits.ts +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -135,8 +135,20 @@ export function codexRateLimitsToLimits(input: { | 135 | 135 |
| 136 | 136 | export function codexRateLimitsToUpdate( | 137 | 137 | snapshot: CodexRateLimitSnapshot, | | 138 | + previous?: CodexRateLimitSnapshot, | 138 | 139 | ): ProviderUsageLimitsUpdate | undefined { | 139 | | - const windows = codexRateLimitsToWindows(snapshot); | | 140 | + const previousWindows = previous ? codexRateLimitsToWindows(previous) : []; | | 141 | + // Token ticks repeat session-cached limits that may predate an account probe or reset. | | 142 | + const windows = codexRateLimitsToWindows(snapshot).filter((window) => { | | 143 | + const existing = previousWindows.find((candidate) => candidate.id === window.id); | | 144 | + return ( | | 145 | + !existing || | | 146 | + existing.kind !== window.kind || | | 147 | + existing.usedPercent !== window.usedPercent || | | 148 | + existing.resetsAt !== window.resetsAt || | | 149 | + existing.windowDurationMins !== window.windowDurationMins | | 150 | + ); | | 151 | + }); | 140 | 152 | return windows.length > 0 ? { windows } : undefined; | 141 | 153 | } | 142 | 154 |
| |
|