mirror of
https://github.com/openai/codex.git
synced 2026-12-04 00:51:02 +00:00
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:
@@ -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(());
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -9,6 +9,7 @@ use codex_core::ResponseEvent;
|
||||
use codex_core::TurnInputRequest;
|
||||
use codex_core::X_CODEX_ROUTING_HINT_HEADER;
|
||||
use codex_core::X_RESPONSESAPI_INCLUDE_TIMING_METRICS_HEADER;
|
||||
use codex_core::test_support::EmptyUserInstructionsProvider;
|
||||
use codex_core::test_support::with_parent_turn;
|
||||
use codex_extension_api::ExtensionRegistryBuilder;
|
||||
use codex_features::Feature;
|
||||
@@ -62,6 +63,7 @@ use core_test_support::responses::start_websocket_server;
|
||||
use core_test_support::responses::start_websocket_server_with_headers;
|
||||
use core_test_support::responses_metadata as test_responses_metadata;
|
||||
use core_test_support::skip_if_no_network;
|
||||
use core_test_support::test_codex::RecordingUserInstructionsProvider;
|
||||
use core_test_support::test_codex::test_codex;
|
||||
use core_test_support::tracing::install_test_tracing;
|
||||
use core_test_support::wait_for_event;
|
||||
@@ -635,18 +637,24 @@ async fn responses_websocket_resume_prewarm_reuses_and_repairs_connection() -> a
|
||||
.await;
|
||||
let mut extensions = ExtensionRegistryBuilder::new();
|
||||
extensions.thread_lifecycle_contributor(Arc::new(ThreadIdle));
|
||||
let instructions = Arc::new(RecordingUserInstructionsProvider::new(Arc::new(
|
||||
EmptyUserInstructionsProvider,
|
||||
)));
|
||||
let test = test_codex()
|
||||
.with_extensions(Arc::new(extensions.build()))
|
||||
.with_user_instructions_provider(instructions.clone())
|
||||
.build_with_websocket_server(&server)
|
||||
.await?;
|
||||
test.submit_text_turn("hello").await?;
|
||||
ThreadIdle::wait(&test.codex).await;
|
||||
|
||||
// A healthy warm resume preserves the existing response baseline.
|
||||
// A healthy warm resume skips preparation; only the user turn reloads instructions.
|
||||
let instruction_loads = instructions.load_count();
|
||||
test.codex.prewarm().await;
|
||||
test.codex.prewarm().await;
|
||||
test.submit_text_turn("continue").await?;
|
||||
|
||||
assert_eq!(instructions.load_count(), instruction_loads + 1);
|
||||
assert_eq!(server.handshakes().len(), 1);
|
||||
assert_eq!(
|
||||
server.single_handshake().header(USER_AGENT_HEADER),
|
||||
@@ -663,6 +671,7 @@ async fn responses_websocket_resume_prewarm_reuses_and_repairs_connection() -> a
|
||||
// Turn idle does not synchronize with the reader observing the server's close.
|
||||
// Retry resume until it sees the close; pending attempts must still share one socket.
|
||||
ThreadIdle::wait(&test.codex).await;
|
||||
let instruction_loads = instructions.load_count();
|
||||
let warmup = tokio::time::timeout(Duration::from_secs(10), async {
|
||||
loop {
|
||||
test.codex.prewarm().await;
|
||||
@@ -674,6 +683,7 @@ async fn responses_websocket_resume_prewarm_reuses_and_repairs_connection() -> a
|
||||
}
|
||||
})
|
||||
.await?;
|
||||
assert!(instructions.load_count() > instruction_loads);
|
||||
assert_eq!(warmup.body_json()["generate"], false);
|
||||
assert!(warmup.body_json().get("previous_response_id").is_none());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user