Skip startup prewarm preparation when the WebSocket is ready (#47745)

## Why

Repeated prewarm calls still prepared instructions, tools, and a prompt even when an existing WebSocket connection was already warm.

## What changed

Check for a previous request and a connection that is not known to be closed before startup prewarm preparation. Reuse this readiness check in `prewarm_websocket`, and record `ready` or `needs_prewarm` in the `codex.startup_prewarm.websocket_check` counter before preparation can fail.

## Testing

Extend the WebSocket resume test to verify that repeated prewarm calls skip instruction loading on a healthy connection, while a closed connection triggers preparation again.

GitOrigin-RevId: 6a1b361b36b4e7f16fb283e48a8ce4d4fb996ceb
This commit is contained in:
vkg-oai
2026-09-24 04:29:15 +00:00
committed by copyberry
parent 1d87af5faa
commit a98a07759a
3 changed files with 42 additions and 8 deletions
+13 -6
View File
@@ -2112,6 +2112,18 @@ impl ModelClientSession {
websocket_telemetry
}
/// Whether a previous request prepared a connection that is not known to be closed.
/// The next request still validates provider, auth, and headers before reuse.
pub(crate) async fn is_websocket_prewarmed(&self) -> bool {
if self.websocket_session.last_request.is_some()
&& let Some(connection) = self.websocket_session.connection.as_ref()
{
!connection.is_closed().await
} else {
false
}
}
#[allow(clippy::too_many_arguments)]
pub async fn prewarm_websocket(
&mut self,
@@ -2126,12 +2138,7 @@ impl ModelClientSession {
if !self.client.responses_websocket_enabled() {
return Ok(());
}
// A previous request only makes prewarm unnecessary while its socket is
// still alive. Reconnecting below resets the old continuation normally.
if self.websocket_session.last_request.is_some()
&& let Some(connection) = self.websocket_session.connection.as_ref()
&& !connection.is_closed().await
{
if self.is_websocket_prewarmed().await {
return Ok(());
}
+18 -1
View File
@@ -287,6 +287,24 @@ impl Session {
async fn schedule_startup_prewarm_inner(session: Arc<Session>) -> CodexResult<ModelClientSession> {
let prewarm_started_at = Instant::now();
let mut client_session = session.services.model_client.new_session();
let websocket_ready = client_session.is_websocket_prewarmed().await;
// Count the decision before preparation can fail; fresh clients also need prewarm.
session.services.session_telemetry.counter(
"codex.startup_prewarm.websocket_check",
/*inc*/ 1,
&[(
"outcome",
if websocket_ready {
"ready"
} else {
"needs_prewarm"
},
)],
);
if websocket_ready {
return Ok(client_session);
}
let base_instructions = session.get_prompt_base_instructions().await.text;
let startup_turn_context = session
.new_startup_prewarm_turn_with_sub_id(INITIAL_SUBMIT_ID.to_owned())
@@ -329,7 +347,6 @@ async fn schedule_startup_prewarm_inner(session: Arc<Session>) -> CodexResult<Mo
window_number,
context_window_id,
);
let mut client_session = session.services.model_client.new_session();
// Start the handshake with the expected route while capturing tools for generate=false.
let (step_context, ()) = tokio::try_join!(
async {