Skip to content

Commit 49dacaa

Browse files
fix(server): classify Codex 429 retry exhaustion through one typed helper
Fold the three usage-limit checks into codexUsageLimitError, read the exhaustion status from the typed codexErrorInfo union instead of Record casts, and pass the completion error info itself into root terminalization so the code and class derive from one source. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 4bea96f commit 49dacaa

1 file changed

Lines changed: 48 additions & 57 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,26 +1110,30 @@ function codexErrorInfoCode(value: unknown): string | null {
11101110
return Object.keys(value)[0] ?? null;
11111111
}
11121112

1113-
function codexErrorInfoHttpStatus(value: unknown, code: string | null): number | null {
1114-
if (code === null || typeof value !== "object" || value === null) {
1115-
return null;
1116-
}
1117-
const detail = (value as Record<string, unknown>)[code];
1118-
if (typeof detail !== "object" || detail === null) {
1119-
return null;
1120-
}
1121-
const status = (detail as Record<string, unknown>).httpStatusCode;
1122-
return typeof status === "number" ? status : null;
1123-
}
1113+
type CodexErrorInfo = CodexSchema.ServerNotification__CodexErrorInfo;
11241114

11251115
/**
1126-
* Retry exhaustion is reported as `responseTooManyFailedAttempts`, and only the
1127-
* embedded last-attempt status says why. Exhaustion that ended on HTTP 429 is
1128-
* a usage or rate limit, so it keeps the Limited state and resume controls;
1129-
* exhaustion on other statuses stays a provider or transport error.
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.
11301119
*/
1131-
function codexExhaustedOnRateLimit(code: string | null, info: unknown): boolean {
1132-
return code === "responseTooManyFailedAttempts" && codexErrorInfoHttpStatus(info, code) === 429;
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+
);
11331137
}
11341138

11351139
interface ActiveCodexTurnContext {
@@ -4538,12 +4542,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
45384542
failure: makeProviderFailure({
45394543
message: payload.error.additionalDetails?.trim() || payload.error.message,
45404544
code: notificationCode,
4541-
class:
4542-
notificationCode === "usageLimitExceeded" ||
4543-
notificationCode === "rateLimitExceeded" ||
4544-
codexExhaustedOnRateLimit(notificationCode, payload.error.codexErrorInfo)
4545-
? "usage_limit"
4546-
: "provider_error",
4545+
class: codexUsageLimitError(notificationCode, payload.error.codexErrorInfo)
4546+
? "usage_limit"
4547+
: "provider_error",
45474548
}),
45484549
};
45494550
return;
@@ -4564,14 +4565,11 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
45644565
? payload.error.message
45654566
: additionalDetails,
45664567
code,
4567-
class:
4568-
code === "usageLimitExceeded" ||
4569-
code === "rateLimitExceeded" ||
4570-
codexExhaustedOnRateLimit(code, payload.error.codexErrorInfo)
4571-
? "usage_limit"
4572-
: code?.startsWith("http") === true || code?.startsWith("responseStream") === true
4573-
? "transport_error"
4574-
: "provider_error",
4568+
class: codexUsageLimitError(code, payload.error.codexErrorInfo)
4569+
? "usage_limit"
4570+
: code?.startsWith("http") === true || code?.startsWith("responseStream") === true
4571+
? "transport_error"
4572+
: "provider_error",
45754573
retryable: true,
45764574
});
45774575
const itemOrdinal =
@@ -5569,37 +5567,34 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
55695567
readonly context: ActiveCodexTurnContext;
55705568
readonly status: OrchestrationV2ProviderTurn["status"];
55715569
readonly failureMessage?: string;
5572-
readonly failureCode?: string | null;
5573-
readonly failureInfo?: unknown;
5570+
readonly failureErrorInfo?: CodexErrorInfo;
55745571
readonly providerRetry?: ActiveCodexProviderRetry;
55755572
}): Effect.fn.Return<CodexRootTerminalEvent> {
55765573
const terminalStatus = providerTurnStatusToTerminal(input.status);
55775574
if (terminalStatus === "failed") {
55785575
const previousFailure = input.context.latestProviderFailure ?? input.providerRetry;
5579-
const completionFailureClass =
5580-
input.failureCode === "usageLimitExceeded" ||
5581-
input.failureCode === "rateLimitExceeded" ||
5582-
codexExhaustedOnRateLimit(input.failureCode ?? null, input.failureInfo)
5583-
? "usage_limit"
5584-
: "provider_error";
5585-
const completionHasExhaustionStatus =
5586-
input.failureCode === "responseTooManyFailedAttempts" &&
5587-
codexErrorInfoHttpStatus(input.failureInfo, input.failureCode) !== null;
5588-
// Keep richer notification evidence unless an explicit completion status
5589-
// contradicts its classification. Missing status is not new evidence.
5576+
const failureCode =
5577+
input.failureErrorInfo === undefined
5578+
? undefined
5579+
: codexErrorInfoCode(input.failureErrorInfo);
5580+
const failureClass = codexUsageLimitError(failureCode ?? null, input.failureErrorInfo)
5581+
? "usage_limit"
5582+
: "provider_error";
5583+
// The notification carries the richer message, so it wins when the
5584+
// completion repeats it. An explicit exhaustion status on the
5585+
// completion still decides the class; a missing one is not evidence.
5586+
const exhaustionStatus = codexExhaustionHttpStatus(input.failureErrorInfo);
55905587
const failure =
55915588
previousFailure !== undefined &&
55925589
(input.failureMessage === undefined ||
55935590
input.failureMessage === previousFailure.nativeMessage) &&
5594-
(input.failureCode === undefined ||
5595-
input.failureCode === previousFailure.failure.code) &&
5596-
(!completionHasExhaustionStatus ||
5597-
previousFailure.failure.class === completionFailureClass)
5591+
(failureCode === undefined || failureCode === previousFailure.failure.code) &&
5592+
(exhaustionStatus === null || previousFailure.failure.class === failureClass)
55985593
? previousFailure.failure
55995594
: makeProviderFailure({
56005595
message: input.failureMessage,
5601-
code: input.failureCode,
5602-
class: completionFailureClass,
5596+
code: failureCode,
5597+
class: failureClass,
56035598
});
56045599
return {
56055600
type: "turn.terminal",
@@ -5691,7 +5686,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
56915686
readonly nativeTurnId: string;
56925687
readonly status: OrchestrationV2ProviderTurn["status"];
56935688
readonly failureMessage?: string;
5694-
readonly failureCode?: string | null;
5689+
readonly failureErrorInfo?: CodexErrorInfo;
56955690
readonly providerRetry?: ActiveCodexProviderRetry;
56965691
readonly goalHoldTurn?: OrchestrationV2ProviderTurn;
56975692
}) {
@@ -5801,8 +5796,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
58015796
readonly status: OrchestrationV2ProviderTurn["status"];
58025797
readonly completedAt: DateTime.Utc;
58035798
readonly failureMessage?: string;
5804-
readonly failureCode?: string | null;
5805-
readonly failureInfo?: unknown;
5799+
readonly failureErrorInfo?: CodexErrorInfo;
58065800
}) =>
58075801
turnTerminalizationPermit.withPermits(1)(
58085802
Effect.gen(function* () {
@@ -6075,10 +6069,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
60756069
failureMessage: payload.turn.error.message,
60766070
...(payload.turn.error.codexErrorInfo == null
60776071
? {}
6078-
: {
6079-
failureCode: codexErrorInfoCode(payload.turn.error.codexErrorInfo),
6080-
failureInfo: payload.turn.error.codexErrorInfo,
6081-
}),
6072+
: { failureErrorInfo: payload.turn.error.codexErrorInfo }),
60826073
}),
60836074
});
60846075
}),

0 commit comments

Comments
 (0)