Fall back to HTTP immediately for oversized WebSocket requests (#60840)

## Why

A WebSocket close with code `1009` before any text arrives indicates that the request exceeds the peer's message size limit. Retrying the request over another WebSocket does not address that limit.

## What changed

- Classify these early size rejections as requiring HTTP fallback and switch transports without exhausting WebSocket retries, including during prewarming.
- Preserve ordinary stream retry behavior for size closes after any text frame and for other close codes.

## Testing

Add integration coverage for early size rejections during generation and prewarming, size closes after metadata, rate limits, or response creation, and ordinary closes.

GitOrigin-RevId: 4550d289bb414fec1faae2a195bcc635d394117b
This commit is contained in:
Charlie Marsh
2026-11-11 18:58:21 +00:00
committed by copyberry
parent 9e6d1c5278
commit bc3ccba6e1
13 changed files with 216 additions and 12 deletions
+4
View File
@@ -2571,6 +2571,10 @@ impl ModelClientSession {
while let Some(event) = stream.next().await {
match event {
Ok(ResponseEvent::Completed { .. }) => break,
Err(err) if err.requires_http_fallback() => {
self.try_switch_fallback_transport(session_telemetry, model_info);
break;
}
Err(err) => return Err(err),
_ => {}
}
+7 -3
View File
@@ -118,12 +118,13 @@ pub(crate) async fn handle_response_stream_error(
return Ok(());
}
if retry_state.retries >= max_retries
let requires_http_fallback = err.requires_http_fallback();
let switched = (requires_http_fallback || retry_state.retries >= max_retries)
&& client_session.try_switch_fallback_transport(
&turn_context.session_telemetry,
turn_context.model_info(),
)
{
);
if switched {
// Changing transport must not bypass the server's retry deadline.
if let Some(retry_after) = retry_after {
tokio::time::sleep_until(retry_after.deadline()).await;
@@ -135,6 +136,9 @@ pub(crate) async fn handle_response_stream_error(
}),
)
.await;
}
// Another request can already have switched this session to HTTP.
if switched || requires_http_fallback {
retry_state.retries = 0;
return Ok(());
}
+4 -1
View File
@@ -3158,7 +3158,10 @@ async fn try_run_sampling_request(
.await;
should_emit_token_count |= incomplete.token_usage.is_some();
}
if *draining_for_mailbox && matches!(err.details(), CodexErrorDetails::Stream(_)) {
if *draining_for_mailbox
&& (matches!(err.details(), CodexErrorDetails::Stream(_))
|| err.requires_http_fallback())
{
// Mail already requires a follow-up, even if the producer exited before
// receiving the interrupt. Do not retry without that pending input.
break Ok(SamplingRequestResult {