Reconnect WebRTC Realtime sideband transports (#39257)

## What changed

- Reconnect frameless bidirectional WebRTC sideband sockets after unexpected transport loss with capped exponential backoff.
- Preserve pending text or handoff output and bounded transcript state across reconnects.
- Treat `404 Not Found` and `410 Gone` handshake responses as terminal session completion, and preserve HTTP status information from failed WebSocket handshakes.
- Bound Realtime delegation input and transcript fields while retaining the most relevant portion of each.

## Testing

- Add unit coverage for reconnect backoff, terminal status handling, transport error classification, transcript continuity and truncation, and pending outbound messages.
- Add an end-to-end WebRTC conversation test covering an unclean disconnect, reconnect, transcript reconciliation, outbound progress during sustained inbound traffic, and terminal session completion.

GitOrigin-RevId: 544fd7e542c3434d0bb42cfbcc7143604419a037
This commit is contained in:
Peter Bakkum authored and copyberry committed 2026-08-18 18:41:40 +00:00
1 parent fa9a05f2d2
commit ecb8013dfa
11 files changed
+1046 -215

No files matched your search

@@ -0,0 +1,162 @@
use super::RealtimeInputTask;
use super::RealtimeInputTaskExit;
use super::RealtimeWebrtcSidebandInputTask;
use super::flush_realtime_transcript_tail;
use super::report_realtime_transport_loss;
use super::run_realtime_input_task;
use codex_api::ApiError;
use codex_api::RealtimeEvent;
use codex_api::RealtimeEventParser;
use codex_api::RealtimeTranscriptState;
use codex_api::map_api_error;
use codex_login::default_client::default_headers;
use http::StatusCode;
use std::sync::atomic::Ordering;
use std::time::Duration;
use tokio::task::JoinHandle;
use tokio::time::Instant;
use tokio::time::sleep;
use tracing::info;
use tracing::warn;
const RECONNECT_BASE_DELAY: Duration = Duration::from_millis(200);
const RECONNECT_MAX_DELAY: Duration = Duration::from_secs(5);
const STABLE_CONNECTION_DURATION: Duration = Duration::from_secs(30);
pub(super) fn spawn_webrtc_sideband_input_task(
input: RealtimeWebrtcSidebandInputTask,
) -> JoinHandle<()> {
let RealtimeWebrtcSidebandInputTask {
client,
session_config,
call_id,
sideband_headers,
input_channels,
events_tx,
handoff_state,
session_kind,
event_parser,
realtime_active,
transcript_tail_flush,
stop_token,
} = input;
tokio::spawn(async move {
let mut reconnecting = false;
let mut rapid_disconnects = 0_u32;
let mut pending_outbound = None;
let transcript_state = RealtimeTranscriptState::default();
while realtime_active.load(Ordering::Relaxed) {
let connection = match tokio::select! {
biased;
_ = stop_token.cancelled() => break,
connection = client.connect_webrtc_sideband(
session_config.clone(),
&call_id,
sideband_headers.clone(),
default_headers(),
transcript_state.clone(),
) => connection,
} {
Ok(connection) => connection,
Err(err) => {
if !realtime_active.load(Ordering::Relaxed) || stop_token.is_cancelled() {
break;
}
if reconnecting && webrtc_sideband_session_ended(&err) {
info!(
call_id,
"realtime sideband session ended while reconnecting"
);
break;
}
let mapped_error = map_api_error(err);
warn!(
reconnecting,
"failed to connect realtime sideband: {mapped_error}"
);
let _ = events_tx
.send(RealtimeEvent::Error(mapped_error.to_string()))
.await;
break;
}
};
if !realtime_active.load(Ordering::Relaxed) || stop_token.is_cancelled() {
break;
}
let connected_at = Instant::now();
let exit = run_realtime_input_task(
RealtimeInputTask {
writer: connection.writer(),
events: connection.events(),
text_rx: input_channels.text_rx.clone(),
handoff_output_rx: input_channels.handoff_output_rx.clone(),
audio_rx: input_channels.audio_rx.clone(),
events_tx: events_tx.clone(),
handoff_state: handoff_state.clone(),
session_kind,
event_parser,
stop_token: stop_token.clone(),
},
pending_outbound.take(),
)
.await;
let RealtimeInputTaskExit::TransportLost {
err,
pending_outbound: failed_outbound,
} = exit
else {
break;
};
pending_outbound = failed_outbound;
if !realtime_active.load(Ordering::Relaxed) || stop_token.is_cancelled() {
break;
}
if event_parser != RealtimeEventParser::FramelessBidi {
report_realtime_transport_loss(&events_tx, err).await;
break;
}
if connected_at.elapsed() >= STABLE_CONNECTION_DURATION {
rapid_disconnects = 0;
}
rapid_disconnects = rapid_disconnects.saturating_add(1);
let delay = reconnect_delay(rapid_disconnects);
warn!(
call_id,
delay_ms = delay.as_millis(),
"live sideband transport lost; reconnecting: {err}"
);
reconnecting = true;
tokio::select! {
biased;
_ = stop_token.cancelled() => break,
_ = sleep(delay) => {}
}
}
let transcript_tail = transcript_state.take_transcript_tail().await;
flush_realtime_transcript_tail(&transcript_tail_flush, &transcript_tail).await;
})
}
fn webrtc_sideband_session_ended(err: &ApiError) -> bool {
matches!(
err,
ApiError::Api { status, .. }
if matches!(*status, StatusCode::NOT_FOUND | StatusCode::GONE)
)
}
fn reconnect_delay(rapid_disconnects: u32) -> Duration {
let multiplier = 2_u32.saturating_pow(rapid_disconnects.saturating_sub(1));
RECONNECT_BASE_DELAY
.saturating_mul(multiplier)
.min(RECONNECT_MAX_DELAY)
}
#[cfg(test)]
#[path = "sideband_tests.rs"]
mod tests;
@@ -0,0 +1,36 @@
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn reconnect_delay_backs_off_and_caps() {
assert_eq!(
reconnect_delay(/*rapid_disconnects*/ 1),
Duration::from_millis(200)
);
assert_eq!(
reconnect_delay(/*rapid_disconnects*/ 2),
Duration::from_millis(400)
);
assert_eq!(
reconnect_delay(/*rapid_disconnects*/ 3),
Duration::from_millis(800)
);
assert_eq!(
reconnect_delay(/*rapid_disconnects*/ 10),
RECONNECT_MAX_DELAY
);
}
#[test]
fn recognizes_terminal_sideband_statuses() {
for status in [StatusCode::NOT_FOUND, StatusCode::GONE] {
assert!(webrtc_sideband_session_ended(&ApiError::Api {
status,
message: "session ended".to_string(),
}));
}
assert!(!webrtc_sideband_session_ended(&ApiError::Api {
status: StatusCode::INTERNAL_SERVER_ERROR,
message: "retry".to_string(),
}));
}