@@ -1144,10 +1144,6 @@ export async function handleResponses(
11441144 }
11451145 if ( parsed . _compactionRequest === true ) parsed . _cursorIsolateConversation = true ;
11461146
1147- if ( isThreadSpawnRequest ( req . headers ) ) {
1148- await maybePrimeSubagentQuota ( config ) ;
1149- }
1150-
11511147 let route : RouteResult ;
11521148 try {
11531149 route = routeModel ( config , parsed . modelId ) ;
@@ -1158,6 +1154,17 @@ export async function handleResponses(
11581154 return formatErrorResponse ( 404 , "invalid_request_error" , err instanceof Error ? err . message : String ( err ) ) ;
11591155 }
11601156
1157+ const hasUnexpandedPreviousResponse = ! ! parsed . previousResponseId
1158+ && parsed . _previousResponseInputExpanded !== true ;
1159+ // A canonical replay miss must not poll quota upstream before the final fail-closed decision.
1160+ // Cached fallback state can still select a provider with native continuation support below.
1161+ if (
1162+ isThreadSpawnRequest ( req . headers )
1163+ && ! ( hasUnexpandedPreviousResponse && isCanonicalOpenAiForwardProvider ( route . provider ) )
1164+ ) {
1165+ await maybePrimeSubagentQuota ( config ) ;
1166+ }
1167+
11611168 let authCtx : CodexAuthContext = { kind : "main" , accountId : null } ;
11621169 let selectedForwardHeaders = req . headers ;
11631170 let subagentFallbackAccountId = config . activeCodexAccountId ?? null ;
@@ -1206,6 +1213,20 @@ export async function handleResponses(
12061213 return unreadableEncryptedAgentTaskResponse ( ) ;
12071214 }
12081215
1216+ // The canonical ChatGPT backend rejects previous_response_id, so a local replay miss leaves no
1217+ // safe way to recover the omitted history. Fail before auth, adapter construction, or upstream
1218+ // I/O instead of stripping the id and silently forwarding a context-free delta (#702).
1219+ if (
1220+ hasUnexpandedPreviousResponse
1221+ && isCanonicalOpenAiForwardProvider ( route . provider )
1222+ ) {
1223+ return formatErrorResponse (
1224+ 400 ,
1225+ "invalid_request_error" ,
1226+ "OpenAI forward continuation state is unavailable or expired; start a new session instead of reusing this previous_response_id." ,
1227+ ) ;
1228+ }
1229+
12091230 await applyFinalRouteRequestNormalization ( { parsed, route, config, req, logCtx } ) ;
12101231
12111232 {
0 commit comments