TypeScript
fix(server): classify Codex 429 retry exhaustion as a usage limit (#17501)
Hong Liu58b48274ff7d
diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.tsindex 17d43f5c14a..c1622f04869 100644--- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts+++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts@@ -6852,12 +6852,27 @@ describe("CodexAdapterV2 post-settle continuation", () => {expectedClass: "usage_limit",},{ name: "retry", code: "usageLimitExceeded", notification: true, expectedClass: "usage_limit" },+ {+ name: "exhausted-429",+ code: "responseTooManyFailedAttempts",+ errorInfo: { responseTooManyFailedAttempts: { httpStatusCode: 429 } },+ notification: false,+ expectedClass: "usage_limit",+ },+ {+ name: "exhausted-500",+ code: "responseTooManyFailedAttempts",+ errorInfo: { responseTooManyFailedAttempts: { httpStatusCode: 500 } },+ notification: false,+ expectedClass: "provider_error",+ },] as const)("classifies Codex terminal failures from $name evidence", (scenario) =>Effect.scoped(Effect.gen(function* () {const nativeThreadId = `native-limit-${scenario.name}`;const nativeTurnId = `turn-limit-${scenario.name}`;const message = "Provider stopped this request.";+ const errorInfo = "errorInfo" in scenario ? scenario.errorInfo : scenario.code;const resetAt = "2033-05-19T07:20:00.000Z";const snapshot = {type: "emit_inbound" as const,@@ -6928,7 +6943,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {willRetry: scenario.name === "retry",error: {message,- codexErrorInfo: scenario.code,+ codexErrorInfo: errorInfo,additionalDetails:scenario.name === "matching-details"? "Detailed provider allowance explanation."@@ -6952,7 +6967,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {message: scenario.name === "replacement" ? "A different failure." : message,...(scenario.notification && scenario.name !== "matching-details"? {}- : { codexErrorInfo: scenario.code }),+ : { codexErrorInfo: errorInfo }),},},},@@ -7041,6 +7056,116 @@ describe("CodexAdapterV2 post-settle continuation", () => {),);+ it.effect.each([+ {+ notificationStatus: 500,+ completionStatus: 429,+ willRetry: false,+ expectedClass: "usage_limit",+ },+ {+ notificationStatus: 429,+ completionStatus: 500,+ willRetry: true,+ expectedClass: "provider_error",+ },+ {+ notificationStatus: 429,+ completionStatus: null,+ willRetry: false,+ expectedClass: "usage_limit",+ },+ {+ notificationStatus: 429,+ completionStatus: null,+ willRetry: true,+ expectedClass: "usage_limit",+ },+ ] as const)(+ "honors completion HTTP status from $notificationStatus to $completionStatus (retry: $willRetry)",+ ({ notificationStatus, completionStatus, expectedClass, willRetry }) =>+ Effect.scoped(+ Effect.gen(function* () {+ const name = `${notificationStatus}-${completionStatus}-${willRetry}`;+ const nativeThreadId = `native-exhaustion-status-${name}`;+ const nativeTurnId = `turn-exhaustion-status-${name}`;+ const message = "Provider stopped this request.";+ const additionalDetails = "Detailed notification evidence.";+ const transcript = makeCodexReplayTranscript({+ scenario: `codex-exhaustion-status-${name}`,+ entries: [+ ...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "Continue." }),+ {+ type: "emit_inbound",+ label: "error",+ frame: {+ method: "error",+ params: {+ threadId: nativeThreadId,+ turnId: nativeTurnId,+ willRetry,+ error: {+ message,+ codexErrorInfo: {+ responseTooManyFailedAttempts: { httpStatusCode: notificationStatus },+ },+ additionalDetails,+ },+ },+ },+ },+ {+ type: "emit_inbound",+ label: "turn/completed",+ frame: {+ method: "turn/completed",+ params: {+ threadId: nativeThreadId,+ turn: {+ ...makeCodexReplayTurn({ id: nativeTurnId, status: "failed" }),+ error: {+ message,+ codexErrorInfo: {+ responseTooManyFailedAttempts: { httpStatusCode: completionStatus },+ },+ },+ },+ },+ },+ },+ ],+ });+ const harness = yield* makeCodexReplayHarness(transcript);+ yield* harness.runtime.startTurn(+ makeCodexTestTurnInput({+ threadId: harness.threadId,+ providerThread: harness.providerThread,+ now: yield* DateTime.now,+ text: "Continue.",+ attemptId: RunAttemptId.make(`attempt-exhaustion-status-${name}`),+ }),+ );+ yield* harness.firstTerminal;+ const terminal = harness.terminalEvents()[0];+ assert.equal(terminal?.status, "failed");+ if (terminal?.status !== "failed") return;+ assert.equal(terminal.failure.class, expectedClass);+ assert.equal(terminal.failure.code, "responseTooManyFailedAttempts");+ assert.equal(terminal.threadDisposition, "reusable");+ const notificationClass = notificationStatus === 429 ? "usage_limit" : "provider_error";+ assert.equal(+ terminal.failure.message,+ expectedClass === notificationClass ? additionalDetails : message,+ );+ if (willRetry) assert.equal(terminal.retry?.attempt, 1);+ }).pipe(+ Effect.provide(+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),+ ),+ ),+ ),+ );+const FAILED_SCENARIO = "codex-failed-mid-command";const FAILED_NATIVE_THREAD = "native-codex-failed-thread";const FAILED_NATIVE_TURN = "native-codex-failed-turn";diff --git a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.tsindex 4750aad5326..a971e77fced 100644--- a/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts+++ b/apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts@@ -1110,6 +1110,32 @@ function codexErrorInfoCode(value: unknown): string | null {return Object.keys(value)[0] ?? null;}+type CodexErrorInfo = CodexSchema.ServerNotification__CodexErrorInfo;++/**+ * Retry exhaustion only says why it failed through the last attempt's HTTP+ * status. Exhaustion on 429 is a usage or rate limit, so it keeps the Limited+ * state and resume controls; other statuses stay provider or transport errors.+ */+function codexExhaustionHttpStatus(errorInfo: CodexErrorInfo | null | undefined): number | null {+ return typeof errorInfo === "object" &&+ errorInfo !== null &&+ "responseTooManyFailedAttempts" in errorInfo+ ? (errorInfo.responseTooManyFailedAttempts.httpStatusCode ?? null)+ : null;+}++function codexUsageLimitError(+ code: string | null,+ errorInfo: CodexErrorInfo | null | undefined,+): boolean {+ return (+ code === "usageLimitExceeded" ||+ code === "rateLimitExceeded" ||+ codexExhaustionHttpStatus(errorInfo) === 429+ );+}+interface ActiveCodexTurnContext {latestProviderFailure?: {readonly nativeMessage: string;@@ -4532,11 +4558,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (failure: makeProviderFailure({message: payload.error.additionalDetails?.trim() || payload.error.message,code: notificationCode,- class:- notificationCode === "usageLimitExceeded" ||- notificationCode === "rateLimitExceeded"- ? "usage_limit"- : "provider_error",+ class: codexUsageLimitError(notificationCode, payload.error.codexErrorInfo)+ ? "usage_limit"+ : "provider_error",}),};return;@@ -4557,12 +4581,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (? payload.error.message: additionalDetails,code,- class:- code === "usageLimitExceeded" || code === "rateLimitExceeded"- ? "usage_limit"- : code?.startsWith("http") === true || code?.startsWith("responseStream") === true- ? "transport_error"- : "provider_error",+ class: codexUsageLimitError(code, payload.error.codexErrorInfo)+ ? "usage_limit"+ : code?.startsWith("http") === true || code?.startsWith("responseStream") === true+ ? "transport_error"+ : "provider_error",retryable: true,});const itemOrdinal =@@ -5560,27 +5583,34 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (readonly context: ActiveCodexTurnContext;readonly status: OrchestrationV2ProviderTurn["status"];readonly failureMessage?: string;- readonly failureCode?: string | null;+ readonly failureErrorInfo?: CodexErrorInfo;readonly providerRetry?: ActiveCodexProviderRetry;}): Effect.fn.Return<CodexRootTerminalEvent> {const terminalStatus = providerTurnStatusToTerminal(input.status);if (terminalStatus === "failed") {const previousFailure = input.context.latestProviderFailure ?? input.providerRetry;+ const failureCode =+ input.failureErrorInfo === undefined+ ? undefined+ : codexErrorInfoCode(input.failureErrorInfo);+ const failureClass = codexUsageLimitError(failureCode ?? null, input.failureErrorInfo)+ ? "usage_limit"+ : "provider_error";+ // The notification carries the richer message, so it wins when the+ // completion repeats it. An explicit exhaustion status on the+ // completion still decides the class; a missing one is not evidence.+ const exhaustionStatus = codexExhaustionHttpStatus(input.failureErrorInfo);const failure =previousFailure !== undefined &&(input.failureMessage === undefined ||input.failureMessage === previousFailure.nativeMessage) &&- (input.failureCode === undefined ||- input.failureCode === previousFailure.failure.code)+ (failureCode === undefined || failureCode === previousFailure.failure.code) &&+ (exhaustionStatus === null || previousFailure.failure.class === failureClass)? previousFailure.failure: makeProviderFailure({message: input.failureMessage,- code: input.failureCode,- class:- input.failureCode === "usageLimitExceeded" ||- input.failureCode === "rateLimitExceeded"- ? "usage_limit"- : "provider_error",+ code: failureCode,+ class: failureClass,});return {type: "turn.terminal",@@ -5672,7 +5702,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (readonly nativeTurnId: string;readonly status: OrchestrationV2ProviderTurn["status"];readonly failureMessage?: string;- readonly failureCode?: string | null;+ readonly failureErrorInfo?: CodexErrorInfo;readonly providerRetry?: ActiveCodexProviderRetry;readonly goalHoldTurn?: OrchestrationV2ProviderTurn;}) {@@ -5782,7 +5812,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (readonly status: OrchestrationV2ProviderTurn["status"];readonly completedAt: DateTime.Utc;readonly failureMessage?: string;- readonly failureCode?: string | null;+ readonly failureErrorInfo?: CodexErrorInfo;}) =>turnTerminalizationPermit.withPermits(1)(Effect.gen(function* () {@@ -6055,9 +6085,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (failureMessage: payload.turn.error.message,...(payload.turn.error.codexErrorInfo == null? {}- : {- failureCode: codexErrorInfoCode(payload.turn.error.codexErrorInfo),- }),+ : { failureErrorInfo: payload.turn.error.codexErrorInfo }),}),});}),