@devries-cameron20 / t3code-1 / commits / c3f5e363e5
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): avoid replaying stale Codex usage limits (#26331)

Yash Singh committed Oct 28, 2026 at 02:52 UTC c3f5e363e59371d292f4a7e27008d2c5d1260725
3 files changed +38 -3
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+2 -2
@@ -4304,7 +4304,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
4304
4305 yield* client.handleServerNotification("account/rateLimits/updated", (payload) =>
4306 Effect.gen(function* () {
4307 - yield* Ref.update(rateLimitSnapshot, (previous) =>
4307 + const previous = yield* Ref.getAndUpdate(rateLimitSnapshot, (previous) =>
4308 mergeCodexRateLimits(previous, payload.rateLimits),
4309 );
4310 const resetAt = codexUsageLimitResetAt(yield* Ref.get(rateLimitSnapshot));
@@ -4324,7 +4324,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
4324 turnItem: updated,
4325 });
4326 }
4327 - const update = codexRateLimitsToUpdate(payload.rateLimits);
4327 + const update = codexRateLimitsToUpdate(payload.rateLimits, previous);
4328 if (update && adapterOptions.onUsageLimits) {
4329 const checkedAt = DateTime.formatIso(yield* DateTime.now);
4330 yield* adapterOptions.onUsageLimits({ ...update, checkedAt });
apps/server/src/provider/codexUsageLimits.test.ts
+23
@@ -128,6 +128,29 @@ describe("codexRateLimitsToLimits", () => {
128 });
129
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 +
154 it("carries only the windows the notification names", () => {
155 expect(
156 codexRateLimitsToUpdate({
apps/server/src/provider/codexUsageLimits.ts
+13 -1
@@ -135,8 +135,20 @@ export function codexRateLimitsToLimits(input: {
135
136 export function codexRateLimitsToUpdate(
137 snapshot: CodexRateLimitSnapshot,
138 + previous?: CodexRateLimitSnapshot,
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 + });
152 return windows.length > 0 ? { windows } : undefined;
153 }
154