The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 12:15 UTC
fix(server): proxied Codex usage limits keep their reset time (#32188)
Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Md Ehsanul Haque committed
Nov 15, 2026 at 10:34 UTC
4afa6f0983d2bda57dde80091cda0dc1bc73775e
4 files changed
+144
-4
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+19
@@ -7024,6 +7024,15 @@ describe("CodexAdapterV2 post-settle continuation", () => {
7024
expectedClass: "usage_limit",
7025
},
7026
{ name: "retry", code: "usageLimitExceeded", notification: true, expectedClass: "usage_limit" },
7027
+ // A proxy such as CLIProxyAPI strips the snapshot, so only the error text carries the reset.
7028
+ {
7029
+ name: "message-reset",
7030
+ code: "usageLimitExceeded",
7031
+ message:
7032
+ "You’ve hit your usage limit. Visit https://chatgpt.com/codex/settings/usage to purchase more credits or try again at Nov 11th, 2033 3:37 PM.",
7033
+ notification: false,
7034
+ expectedClass: "usage_limit",
7035
+ },
7036
{
7037
name: "exhausted-429",
7038
code: "responseTooManyFailedAttempts",
@@ -7484,6 +7493,16 @@ describe("CodexAdapterV2 post-settle continuation", () => {
7493
if (terminal?.status !== "failed") return;
7494
assert.equal(terminal.failure.class, scenario.expectedClass);
7495
assert.equal(terminal.threadDisposition, "reusable");
7496
+ if (scenario.name === "message-reset")
7497
+ assert.equal(
7498
+ terminal.failure.resetAt,
7499
+ DateTime.formatIso(
7500
+ DateTime.makeZonedUnsafe(
7501
+ { year: 2033, month: 11, day: 11, hour: 15, minute: 38 },
7502
+ { timeZone: DateTime.zoneMakeLocal(), adjustForTimeZone: true },
7503
+ ),
7504
+ ),
7505
+ );
7506
if (scenario.name === "deferred-reset" || scenario.name === "first-request-reset")
7507
assert.equal(terminal.failure.resetAt, resetAt);
7508
if (scenario.name === "newer-reset-during-read")
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+16
-4
@@ -25,6 +25,8 @@ import {
25
import {
26
codexRateLimitsToUpdate,
27
mergeCodexRateLimits,
28
+ codexMessageResetAt,
29
+ codexTimeZone,
30
codexUsageLimitResetAt,
31
type CodexRateLimitSnapshot,
32
} from "../../provider/codexUsageLimits.ts";
@@ -1246,11 +1248,15 @@ function withCodexManagedFailure(
1248
function failureResetAt(
1249
failure: OrchestrationV2ProviderFailure,
1250
snapshot: CodexRateLimitSnapshot | undefined,
1251
+ /** Read the failure text against when Codex reported it; omit to use the snapshot only. */
1252
+ text?: { readonly failedAt: DateTime.Utc; readonly zone: DateTime.TimeZone },
1253
) {
1254
// Sharing caps have no reset timestamp in the ordinary Codex allowance snapshot.
1251
- return classifyCodexManagedError(failure.code)?.usageLimit
1252
- ? null
1253
- : codexUsageLimitResetAt(snapshot);
1255
+ if (classifyCodexManagedError(failure.code)?.usageLimit) return null;
1256
+ return (
1257
+ codexUsageLimitResetAt(snapshot) ??
1258
+ (text === undefined ? null : codexMessageResetAt(failure.message, text.failedAt, text.zone))
1259
+ );
1260
}
1261
1262
interface ActiveCodexTurnContext {
@@ -1924,6 +1930,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
1930
settings: resolvedRuntime?.config ?? adapterOptions.settings,
1931
environment: resolvedRuntime?.environment ?? adapterOptions.environment,
1932
});
1933
+ const codexZone = codexTimeZone(resolvedRuntime?.environment ?? adapterOptions.environment);
1934
const additionalContextByThread = yield* Ref.make(
1935
new Map<string, NonNullable<CodexTurnStartParams["additionalContext"]>>(),
1936
);
@@ -6304,6 +6311,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6311
function* (input: {
6312
readonly context: ActiveCodexTurnContext;
6313
readonly status: OrchestrationV2ProviderTurn["status"];
6314
+ readonly completedAt: DateTime.Utc;
6315
readonly failureMessage?: string;
6316
readonly failureErrorInfo?: CodexErrorInfo;
6317
readonly providerRetry?: ActiveCodexProviderRetry;
@@ -6375,7 +6383,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6383
failure.class === "usage_limit"
6384
? {
6385
...failure,
6378
- resetAt: failureResetAt(failure, yield* Ref.get(rateLimitSnapshot)),
6386
+ resetAt: failureResetAt(failure, yield* Ref.get(rateLimitSnapshot), {
6387
+ failedAt: input.completedAt,
6388
+ zone: codexZone,
6389
+ }),
6390
}
6391
: failure,
6392
...(input.providerRetry === undefined
@@ -6483,6 +6494,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6494
readonly context: ActiveCodexTurnContext;
6495
readonly nativeTurnId: string;
6496
readonly status: OrchestrationV2ProviderTurn["status"];
6497
+ readonly completedAt: DateTime.Utc;
6498
readonly failureMessage?: string;
6499
readonly failureErrorInfo?: CodexErrorInfo;
6500
readonly providerRetry?: ActiveCodexProviderRetry;
apps/server/src/provider/codexUsageLimits.test.ts
+61
@@ -1,12 +1,15 @@
1
+import * as DateTime from "effect/DateTime";
2
import { ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts";
3
import { exhaustedUsageWindow } from "@t3tools/shared/usageLimits";
4
import * as CodexErrors from "effect-codex-app-server/errors";
5
import { describe, expect, it } from "vite-plus/test";
6
7
import {
8
+ codexMessageResetAt,
9
codexRateLimitsFailureMessage,
10
codexRateLimitsToLimits,
11
codexRateLimitsToUpdate,
12
+ codexTimeZone,
13
mergeCodexRateLimits,
14
} from "./codexUsageLimits.ts";
15
@@ -313,3 +316,61 @@ describe("codexUsageLimitResetAt", () => {
316
).toBeNull();
317
});
318
});
319
+
320
+describe("codexMessageResetAt", () => {
321
+ const dhaka = DateTime.zoneMakeNamedUnsafe("Asia/Dhaka");
322
+ const at = (iso: string) => DateTime.makeUnsafe(iso);
323
+ const failedAt = at("2026-11-07T03:00:00Z"); // 09:00 in Dhaka
324
+
325
+ // Codex prints local time without seconds, so the parsed reset is the minute after.
326
+ it("reads Codex's same-day and dated reset text in Codex's zone", () => {
327
+ expect(
328
+ codexMessageResetAt("You’ve hit your usage limit. Try again at 3:37 PM.", failedAt, dhaka),
329
+ ).toBe("2026-11-07T09:38:00.000Z");
330
+ expect(
331
+ codexMessageResetAt("Upgrade, or try again at Dec 1st, 2026 12:05 AM.", failedAt, dhaka),
332
+ ).toBe("2026-11-30T18:06:00.000Z");
333
+ expect(codexMessageResetAt("Try again at 12:30 PM.", failedAt, dhaka)).toBe(
334
+ "2026-11-07T06:31:00.000Z",
335
+ );
336
+ });
337
+
338
+ it("dates today's form by when Codex failed, across local midnight", () => {
339
+ expect(codexMessageResetAt("Try again at 11:59 PM.", at("2026-11-07T17:59:59Z"), dhaka)).toBe(
340
+ "2026-11-07T18:00:00.000Z",
341
+ );
342
+ });
343
+
344
+ it("takes the later instant of a repeated DST hour", () => {
345
+ // 1:30 AM occurs twice in New York on Nov 1st, 2026: 05:30Z (EDT) and 06:30Z (EST).
346
+ const newYork = DateTime.zoneMakeNamedUnsafe("America/New_York");
347
+ for (const iso of ["2026-11-01T05:15:00Z", "2026-11-01T06:15:00Z"]) {
348
+ expect(codexMessageResetAt("Try again at 1:30 AM.", at(iso), newYork)).toBe(
349
+ "2026-11-01T06:31:00.000Z",
350
+ );
351
+ }
352
+ });
353
+
354
+ it("leaves the reset unknown without reset text or once it has passed", () => {
355
+ expect(
356
+ codexMessageResetAt("You’ve hit your usage limit. Try again later.", failedAt, dhaka),
357
+ ).toBeNull();
358
+ expect(
359
+ codexMessageResetAt(
360
+ "exceeded retry limit, last status: 429 Too Many Requests",
361
+ failedAt,
362
+ dhaka,
363
+ ),
364
+ ).toBeNull();
365
+ expect(codexMessageResetAt("Try again at 8:15 AM.", failedAt, dhaka)).toBeNull();
366
+ });
367
+});
368
+
369
+describe("codexTimeZone", () => {
370
+ it("uses Codex's TZ and falls back to the server's zone", () => {
371
+ const local = DateTime.zoneToString(DateTime.zoneMakeLocal());
372
+ expect(DateTime.zoneToString(codexTimeZone({ TZ: ":Asia/Dhaka" }))).toBe("Asia/Dhaka");
373
+ expect(DateTime.zoneToString(codexTimeZone({ TZ: "not/a-zone" }))).toBe(local);
374
+ expect(DateTime.zoneToString(codexTimeZone({}))).toBe(local);
375
+ });
376
+});
apps/server/src/provider/codexUsageLimits.ts
+48
@@ -201,3 +201,51 @@ export function codexUsageLimitResetAt(
201
null,
202
);
203
}
204
+
205
+const CODEX_RESET_TEXT =
206
+ /[Tt]ry again at (?:([A-Z][a-z]{2}) (\d{1,2})(?:st|nd|rd|th), (\d{4}) )?(\d{1,2}):(\d{2}) ([AP]M)\./;
207
+const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
208
+
209
+/** The zone Codex prints reset times in: its own `TZ`, else the server's. */
210
+export function codexTimeZone(
211
+ environment: Readonly<Record<string, string | undefined>>,
212
+): DateTime.TimeZone {
213
+ return Option.getOrElse(
214
+ DateTime.zoneMakeNamed(environment.TZ?.replace(/^:/, "") ?? ""),
215
+ DateTime.zoneMakeLocal,
216
+ );
217
+}
218
+
219
+/**
220
+ * Codex writes a usage limit's reset into its error text in its own local time:
221
+ * "Try again at 3:37 PM." for today, else "try again at Nov 11th, 2026 3:37 PM.".
222
+ * It is the only reset left when a proxy such as CLIProxyAPI strips the rate-limit snapshot.
223
+ * `failedAt` is when Codex reported the failure, which fixes the date of today's form.
224
+ */
225
+export function codexMessageResetAt(
226
+ message: string,
227
+ failedAt: DateTime.DateTime,
228
+ zone: DateTime.TimeZone,
229
+): string | null {
230
+ const match = CODEX_RESET_TEXT.exec(message);
231
+ if (!match) return null;
232
+ const [, month, day, year, hour, minute, meridiem] = match;
233
+ const monthIndex = month === undefined ? undefined : MONTHS.indexOf(month);
234
+ if (monthIndex === -1) return null;
235
+ const today = DateTime.toParts(DateTime.setZone(failedAt, zone));
236
+ const reset = DateTime.makeZoned(
237
+ {
238
+ year: monthIndex === undefined ? today.year : Number(year),
239
+ month: monthIndex === undefined ? today.month : monthIndex + 1,
240
+ day: monthIndex === undefined ? today.day : Number(day),
241
+ hour: (Number(hour) % 12) + (meridiem === "PM" ? 12 : 0),
242
+ minute: Number(minute),
243
+ },
244
+ // The repeated hour at a DST fall-back takes its later instant so a resume never fires early.
245
+ { timeZone: zone, adjustForTimeZone: true, disambiguation: "later" },
246
+ );
247
+ if (Option.isNone(reset)) return null;
248
+ // Codex drops the seconds, so the next minute is the first one past the reset.
249
+ const resetAt = DateTime.add(reset.value, { minutes: 1 });
250
+ return DateTime.isGreaterThan(resetAt, failedAt) ? DateTime.formatIso(resetAt) : null;
251
+}