@devries-cameron20 / t3code-1 / commits / 58b48274ff
Fork 0

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

fix(server): classify Codex 429 retry exhaustion as a usage limit (#17501)

Co-authored-by: Hylouis233 <Hylouis233@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Julius Marminge <51714798+juliusmarminge@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>

Hong Liu committed Oct 20, 2026 at 10:49 UTC 58b48274ff7db9361ab904d90c76b968e9a9228f
2 files changed +180 -27
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+127 -2
@@ -6852,12 +6852,27 @@ describe("CodexAdapterV2 post-settle continuation", () => {
6852 expectedClass: "usage_limit",
6853 },
6854 { name: "retry", code: "usageLimitExceeded", notification: true, expectedClass: "usage_limit" },
6855 + {
6856 + name: "exhausted-429",
6857 + code: "responseTooManyFailedAttempts",
6858 + errorInfo: { responseTooManyFailedAttempts: { httpStatusCode: 429 } },
6859 + notification: false,
6860 + expectedClass: "usage_limit",
6861 + },
6862 + {
6863 + name: "exhausted-500",
6864 + code: "responseTooManyFailedAttempts",
6865 + errorInfo: { responseTooManyFailedAttempts: { httpStatusCode: 500 } },
6866 + notification: false,
6867 + expectedClass: "provider_error",
6868 + },
6869 ] as const)("classifies Codex terminal failures from $name evidence", (scenario) =>
6870 Effect.scoped(
6871 Effect.gen(function* () {
6872 const nativeThreadId = `native-limit-${scenario.name}`;
6873 const nativeTurnId = `turn-limit-${scenario.name}`;
6874 const message = "Provider stopped this request.";
6875 + const errorInfo = "errorInfo" in scenario ? scenario.errorInfo : scenario.code;
6876 const resetAt = "2033-05-19T07:20:00.000Z";
6877 const snapshot = {
6878 type: "emit_inbound" as const,
@@ -6928,7 +6943,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
6943 willRetry: scenario.name === "retry",
6944 error: {
6945 message,
6931 - codexErrorInfo: scenario.code,
6946 + codexErrorInfo: errorInfo,
6947 additionalDetails:
6948 scenario.name === "matching-details"
6949 ? "Detailed provider allowance explanation."
@@ -6952,7 +6967,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
6967 message: scenario.name === "replacement" ? "A different failure." : message,
6968 ...(scenario.notification && scenario.name !== "matching-details"
6969 ? {}
6955 - : { codexErrorInfo: scenario.code }),
6970 + : { codexErrorInfo: errorInfo }),
6971 },
6972 },
6973 },
@@ -7041,6 +7056,116 @@ describe("CodexAdapterV2 post-settle continuation", () => {
7056 ),
7057 );
7058
7059 + it.effect.each([
7060 + {
7061 + notificationStatus: 500,
7062 + completionStatus: 429,
7063 + willRetry: false,
7064 + expectedClass: "usage_limit",
7065 + },
7066 + {
7067 + notificationStatus: 429,
7068 + completionStatus: 500,
7069 + willRetry: true,
7070 + expectedClass: "provider_error",
7071 + },
7072 + {
7073 + notificationStatus: 429,
7074 + completionStatus: null,
7075 + willRetry: false,
7076 + expectedClass: "usage_limit",
7077 + },
7078 + {
7079 + notificationStatus: 429,
7080 + completionStatus: null,
7081 + willRetry: true,
7082 + expectedClass: "usage_limit",
7083 + },
7084 + ] as const)(
7085 + "honors completion HTTP status from $notificationStatus to $completionStatus (retry: $willRetry)",
7086 + ({ notificationStatus, completionStatus, expectedClass, willRetry }) =>
7087 + Effect.scoped(
7088 + Effect.gen(function* () {
7089 + const name = `${notificationStatus}-${completionStatus}-${willRetry}`;
7090 + const nativeThreadId = `native-exhaustion-status-${name}`;
7091 + const nativeTurnId = `turn-exhaustion-status-${name}`;
7092 + const message = "Provider stopped this request.";
7093 + const additionalDetails = "Detailed notification evidence.";
7094 + const transcript = makeCodexReplayTranscript({
7095 + scenario: `codex-exhaustion-status-${name}`,
7096 + entries: [
7097 + ...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "Continue." }),
7098 + {
7099 + type: "emit_inbound",
7100 + label: "error",
7101 + frame: {
7102 + method: "error",
7103 + params: {
7104 + threadId: nativeThreadId,
7105 + turnId: nativeTurnId,
7106 + willRetry,
7107 + error: {
7108 + message,
7109 + codexErrorInfo: {
7110 + responseTooManyFailedAttempts: { httpStatusCode: notificationStatus },
7111 + },
7112 + additionalDetails,
7113 + },
7114 + },
7115 + },
7116 + },
7117 + {
7118 + type: "emit_inbound",
7119 + label: "turn/completed",
7120 + frame: {
7121 + method: "turn/completed",
7122 + params: {
7123 + threadId: nativeThreadId,
7124 + turn: {
7125 + ...makeCodexReplayTurn({ id: nativeTurnId, status: "failed" }),
7126 + error: {
7127 + message,
7128 + codexErrorInfo: {
7129 + responseTooManyFailedAttempts: { httpStatusCode: completionStatus },
7130 + },
7131 + },
7132 + },
7133 + },
7134 + },
7135 + },
7136 + ],
7137 + });
7138 + const harness = yield* makeCodexReplayHarness(transcript);
7139 + yield* harness.runtime.startTurn(
7140 + makeCodexTestTurnInput({
7141 + threadId: harness.threadId,
7142 + providerThread: harness.providerThread,
7143 + now: yield* DateTime.now,
7144 + text: "Continue.",
7145 + attemptId: RunAttemptId.make(`attempt-exhaustion-status-${name}`),
7146 + }),
7147 + );
7148 + yield* harness.firstTerminal;
7149 + const terminal = harness.terminalEvents()[0];
7150 + assert.equal(terminal?.status, "failed");
7151 + if (terminal?.status !== "failed") return;
7152 + assert.equal(terminal.failure.class, expectedClass);
7153 + assert.equal(terminal.failure.code, "responseTooManyFailedAttempts");
7154 + assert.equal(terminal.threadDisposition, "reusable");
7155 + const notificationClass = notificationStatus === 429 ? "usage_limit" : "provider_error";
7156 + assert.equal(
7157 + terminal.failure.message,
7158 + expectedClass === notificationClass ? additionalDetails : message,
7159 + );
7160 + if (willRetry) assert.equal(terminal.retry?.attempt, 1);
7161 + }).pipe(
7162 + Effect.provide(
7163 + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
7164 + ),
7165 + ),
7166 + ),
7167 + );
7168 +
7169 const FAILED_SCENARIO = "codex-failed-mid-command";
7170 const FAILED_NATIVE_THREAD = "native-codex-failed-thread";
7171 const FAILED_NATIVE_TURN = "native-codex-failed-turn";
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+53 -25
@@ -1110,6 +1110,32 @@ function codexErrorInfoCode(value: unknown): string | null {
1110 return Object.keys(value)[0] ?? null;
1111 }
1112
1113 +type CodexErrorInfo = CodexSchema.ServerNotification__CodexErrorInfo;
1114 +
1115 +/**
1116 + * Retry exhaustion only says why it failed through the last attempt's HTTP
1117 + * status. Exhaustion on 429 is a usage or rate limit, so it keeps the Limited
1118 + * state and resume controls; other statuses stay provider or transport errors.
1119 + */
1120 +function codexExhaustionHttpStatus(errorInfo: CodexErrorInfo | null | undefined): number | null {
1121 + return typeof errorInfo === "object" &&
1122 + errorInfo !== null &&
1123 + "responseTooManyFailedAttempts" in errorInfo
1124 + ? (errorInfo.responseTooManyFailedAttempts.httpStatusCode ?? null)
1125 + : null;
1126 +}
1127 +
1128 +function codexUsageLimitError(
1129 + code: string | null,
1130 + errorInfo: CodexErrorInfo | null | undefined,
1131 +): boolean {
1132 + return (
1133 + code === "usageLimitExceeded" ||
1134 + code === "rateLimitExceeded" ||
1135 + codexExhaustionHttpStatus(errorInfo) === 429
1136 + );
1137 +}
1138 +
1139 interface ActiveCodexTurnContext {
1140 latestProviderFailure?: {
1141 readonly nativeMessage: string;
@@ -4532,11 +4558,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
4558 failure: makeProviderFailure({
4559 message: payload.error.additionalDetails?.trim() || payload.error.message,
4560 code: notificationCode,
4535 - class:
4536 - notificationCode === "usageLimitExceeded" ||
4537 - notificationCode === "rateLimitExceeded"
4538 - ? "usage_limit"
4539 - : "provider_error",
4561 + class: codexUsageLimitError(notificationCode, payload.error.codexErrorInfo)
4562 + ? "usage_limit"
4563 + : "provider_error",
4564 }),
4565 };
4566 return;
@@ -4557,12 +4581,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
4581 ? payload.error.message
4582 : additionalDetails,
4583 code,
4560 - class:
4561 - code === "usageLimitExceeded" || code === "rateLimitExceeded"
4562 - ? "usage_limit"
4563 - : code?.startsWith("http") === true || code?.startsWith("responseStream") === true
4564 - ? "transport_error"
4565 - : "provider_error",
4584 + class: codexUsageLimitError(code, payload.error.codexErrorInfo)
4585 + ? "usage_limit"
4586 + : code?.startsWith("http") === true || code?.startsWith("responseStream") === true
4587 + ? "transport_error"
4588 + : "provider_error",
4589 retryable: true,
4590 });
4591 const itemOrdinal =
@@ -5560,27 +5583,34 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
5583 readonly context: ActiveCodexTurnContext;
5584 readonly status: OrchestrationV2ProviderTurn["status"];
5585 readonly failureMessage?: string;
5563 - readonly failureCode?: string | null;
5586 + readonly failureErrorInfo?: CodexErrorInfo;
5587 readonly providerRetry?: ActiveCodexProviderRetry;
5588 }): Effect.fn.Return<CodexRootTerminalEvent> {
5589 const terminalStatus = providerTurnStatusToTerminal(input.status);
5590 if (terminalStatus === "failed") {
5591 const previousFailure = input.context.latestProviderFailure ?? input.providerRetry;
5592 + const failureCode =
5593 + input.failureErrorInfo === undefined
5594 + ? undefined
5595 + : codexErrorInfoCode(input.failureErrorInfo);
5596 + const failureClass = codexUsageLimitError(failureCode ?? null, input.failureErrorInfo)
5597 + ? "usage_limit"
5598 + : "provider_error";
5599 + // The notification carries the richer message, so it wins when the
5600 + // completion repeats it. An explicit exhaustion status on the
5601 + // completion still decides the class; a missing one is not evidence.
5602 + const exhaustionStatus = codexExhaustionHttpStatus(input.failureErrorInfo);
5603 const failure =
5604 previousFailure !== undefined &&
5605 (input.failureMessage === undefined ||
5606 input.failureMessage === previousFailure.nativeMessage) &&
5573 - (input.failureCode === undefined ||
5574 - input.failureCode === previousFailure.failure.code)
5607 + (failureCode === undefined || failureCode === previousFailure.failure.code) &&
5608 + (exhaustionStatus === null || previousFailure.failure.class === failureClass)
5609 ? previousFailure.failure
5610 : makeProviderFailure({
5611 message: input.failureMessage,
5578 - code: input.failureCode,
5579 - class:
5580 - input.failureCode === "usageLimitExceeded" ||
5581 - input.failureCode === "rateLimitExceeded"
5582 - ? "usage_limit"
5583 - : "provider_error",
5612 + code: failureCode,
5613 + class: failureClass,
5614 });
5615 return {
5616 type: "turn.terminal",
@@ -5672,7 +5702,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
5702 readonly nativeTurnId: string;
5703 readonly status: OrchestrationV2ProviderTurn["status"];
5704 readonly failureMessage?: string;
5675 - readonly failureCode?: string | null;
5705 + readonly failureErrorInfo?: CodexErrorInfo;
5706 readonly providerRetry?: ActiveCodexProviderRetry;
5707 readonly goalHoldTurn?: OrchestrationV2ProviderTurn;
5708 }) {
@@ -5782,7 +5812,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
5812 readonly status: OrchestrationV2ProviderTurn["status"];
5813 readonly completedAt: DateTime.Utc;
5814 readonly failureMessage?: string;
5785 - readonly failureCode?: string | null;
5815 + readonly failureErrorInfo?: CodexErrorInfo;
5816 }) =>
5817 turnTerminalizationPermit.withPermits(1)(
5818 Effect.gen(function* () {
@@ -6055,9 +6085,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6085 failureMessage: payload.turn.error.message,
6086 ...(payload.turn.error.codexErrorInfo == null
6087 ? {}
6058 - : {
6059 - failureCode: codexErrorInfoCode(payload.turn.error.codexErrorInfo),
6060 - }),
6088 + : { failureErrorInfo: payload.turn.error.codexErrorInfo }),
6089 }),
6090 });
6091 }),