@@ -49,6 +49,7 @@ import {
4949 headersForCodexAuthContext ,
5050 isCodexAuthContextUsable ,
5151 resolveCodexAuthContext ,
52+ stripCodexRuntimeProviderFields ,
5253 type CodexAuthContext ,
5354} from "../../codex/auth-context" ;
5455import {
@@ -150,6 +151,45 @@ export function usesCodexForwardPoolAuth(
150151 && provider . authMode === "forward" && provider . adapter === "openai-responses" ;
151152}
152153
154+ function normalizeCodexUnsupportedModelDetail ( value : string ) : string {
155+ return value . trim ( ) . replace ( / \s + / gu, " " ) . toLocaleLowerCase ( "en-US" ) ;
156+ }
157+
158+ function isAllowListedCodexAccountModel400 (
159+ status : number ,
160+ bodyText : string ,
161+ modelId : string ,
162+ ) : boolean {
163+ if ( status !== 400 ) return false ;
164+ try {
165+ const payload = JSON . parse ( bodyText ) as unknown ;
166+ if ( ! payload || typeof payload !== "object" || Array . isArray ( payload ) ) return false ;
167+ const detail = ( payload as { detail ?: unknown } ) . detail ;
168+ if ( typeof detail !== "string" ) return false ;
169+ const expected = `The '${ modelId } ' model is not supported when using Codex with a ChatGPT account.` ;
170+ return normalizeCodexUnsupportedModelDetail ( detail )
171+ === normalizeCodexUnsupportedModelDetail ( expected ) ;
172+ } catch {
173+ return false ;
174+ }
175+ }
176+
177+ async function shouldRetryCodexPoolAccountModel400 (
178+ response : Response ,
179+ modelId : string ,
180+ signal ?: AbortSignal ,
181+ ) : Promise < boolean > {
182+ if ( response . status !== 400 ) return false ;
183+ try {
184+ const body = await readBoundedResponseBody ( response . clone ( ) , { signal } ) ;
185+ return body . displaySafe
186+ && ! body . truncated
187+ && isAllowListedCodexAccountModel400 ( response . status , body . text , modelId ) ;
188+ } catch {
189+ return false ;
190+ }
191+ }
192+
153193
154194
155195export function codexForwardTerminalOutcomeRecorder (
@@ -920,7 +960,7 @@ export async function handleResponses(
920960 + `(model ${ parsed . modelId } ); forwarding without it — earlier turns may be missing from this request` ,
921961 ) ;
922962 }
923- const request = await adapter . buildRequest ( parsed , { headers : selectedForwardHeaders } ) ;
963+ let request = await adapter . buildRequest ( parsed , { headers : selectedForwardHeaders } ) ;
924964 const passthroughEstimate = typeof request . usageLog ?. inputTokens === "number"
925965 ? request . usageLog . inputTokens
926966 : undefined ;
@@ -934,6 +974,20 @@ export async function handleResponses(
934974 linkAbortSignal ( upstream , options . abortSignal ) ;
935975 const connectMs = config . connectTimeoutMs ?? 200_000 ;
936976 let upstreamResponse : Response ;
977+ const transportFailureResponse = ( err : unknown ) : Response => {
978+ upstream . abort ( ) ;
979+ if ( options . abortSignal ?. aborted ) return clientCancelledResponse ( ) ;
980+ const outcome = err instanceof Error && err . name === "TimeoutError" ? "timeout" : "connect_error" ;
981+ if ( usesCodexForwardPoolAuth ( authCtx , route . provider ) ) {
982+ recordCodexUpstreamOutcome ( config , authCtx . accountId , outcome , {
983+ threadId : req . headers . get ( "x-codex-parent-thread-id" ) ,
984+ } ) ;
985+ }
986+ const msg = outcome === "timeout"
987+ ? `Provider connect timeout after ${ connectMs } ms`
988+ : `Provider unreachable: ${ err instanceof Error ? err . message : String ( err ) } ` ;
989+ return formatErrorResponse ( 502 , "upstream_error" , msg ) ;
990+ } ;
937991 try {
938992 // Transient-5xx pre-stream retry (devlog/_plan/260716_claudecode_hardening/010):
939993 // the ChatGPT backend emits transient 502/520s that an immediate retry absorbs.
@@ -950,18 +1004,80 @@ export async function handleResponses(
9501004 { abortSignal : upstream . signal , label : safeHostLabel ( request . url ) } ,
9511005 ) ;
9521006 } catch ( err ) {
953- upstream . abort ( ) ;
954- if ( options . abortSignal ?. aborted ) return clientCancelledResponse ( ) ;
955- const outcome = err instanceof Error && err . name === "TimeoutError" ? "timeout" : "connect_error" ;
956- if ( usesCodexForwardPoolAuth ( authCtx , route . provider ) ) {
957- recordCodexUpstreamOutcome ( config , authCtx . accountId , outcome , {
1007+ return transportFailureResponse ( err ) ;
1008+ }
1009+
1010+ if (
1011+ usesCodexForwardPoolAuth ( authCtx , route . provider )
1012+ && await shouldRetryCodexPoolAccountModel400 (
1013+ upstreamResponse ,
1014+ route . modelId ,
1015+ options . abortSignal ,
1016+ )
1017+ ) {
1018+ const firstAuthCtx = authCtx ;
1019+ let retryAuthCtx : CodexAuthContext | undefined ;
1020+ try {
1021+ retryAuthCtx = await resolveCodexAuthContext (
1022+ req . headers ,
1023+ config ,
1024+ "pool" ,
1025+ { excludeAccountId : firstAuthCtx . accountId } ,
1026+ ) ;
1027+ } catch ( error ) {
1028+ if (
1029+ ! ( error instanceof CodexPoolAuthenticationError )
1030+ && ! ( error instanceof CodexAuthContextError )
1031+ && ! ( error instanceof CodexAccountCooldownError )
1032+ ) throw error ;
1033+ }
1034+
1035+ if ( retryAuthCtx ?. kind === "pool" || retryAuthCtx ?. kind === "main-pool" ) {
1036+ recordCodexUpstreamOutcome ( config , firstAuthCtx . accountId , 400 , {
9581037 threadId : req . headers . get ( "x-codex-parent-thread-id" ) ,
9591038 } ) ;
1039+
1040+ const retryHeaders = headersForCodexAuthContext ( req . headers , retryAuthCtx ) ;
1041+ const retryProvider = applyCodexAuthContextToProvider (
1042+ stripCodexRuntimeProviderFields ( route . provider ) ,
1043+ retryAuthCtx ,
1044+ "pool" ,
1045+ ) ;
1046+ const retryAdapter = resolveAdapter (
1047+ resolveWireProtocolOverride ( route . providerName , route . modelId , retryProvider ) ,
1048+ config . cacheRetention ,
1049+ ) ;
1050+ request = await retryAdapter . buildRequest ( parsed , { headers : retryHeaders } ) ;
1051+
1052+ await upstreamResponse . body ?. cancel ( ) . catch ( ( ) => undefined ) ;
1053+ authCtx = retryAuthCtx ;
1054+ options . onCodexAuthContextResolved ?.( retryAuthCtx ) ;
1055+ selectedForwardHeaders = retryHeaders ;
1056+ route . provider = retryProvider ;
1057+ logCtx . provider = formatCodexProviderForLog (
1058+ route . providerName ,
1059+ retryAuthCtx . accountId ,
1060+ config ,
1061+ ) ;
1062+
1063+ noteAttemptSend ( logCtx . activeAttempt , passthroughEstimate ) ;
1064+ try {
1065+ upstreamResponse = await fetchWithHeaderTimeout (
1066+ request . url ,
1067+ {
1068+ method : request . method ,
1069+ headers : request . headers ,
1070+ body : request . body ,
1071+ } ,
1072+ upstream . signal ,
1073+ connectMs ,
1074+ parsed . stream ,
1075+ providerFetch ( route . provider ) ,
1076+ ) ;
1077+ } catch ( err ) {
1078+ return transportFailureResponse ( err ) ;
1079+ }
9601080 }
961- const msg = outcome === "timeout"
962- ? `Provider connect timeout after ${ connectMs } ms`
963- : `Provider unreachable: ${ err instanceof Error ? err . message : String ( err ) } ` ;
964- return formatErrorResponse ( 502 , "upstream_error" , msg ) ;
9651081 }
9661082 const headers = sanitizePassthroughHeaders ( upstreamResponse . headers ) ;
9671083 const resolvedModel = headers . get ( "openai-model" ) ?. trim ( ) ;
0 commit comments