diff --git a/codex-rs/app-server/tests/suite/v2/code_mode_host.rs b/codex-rs/app-server/tests/suite/v2/code_mode_host.rs
index 534c34878f..82d2a7fa50 100644
--- a/codex-rs/app-server/tests/suite/v2/code_mode_host.rs
+++ b/codex-rs/app-server/tests/suite/v2/code_mode_host.rs
@@ -34,6 +34,35 @@ async fn app_server_shares_flag_selected_grpc_code_mode_host_across_threads() ->
assert_shared_remote_code_mode_host("grpc://127.0.0.1:0").await
}
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn app_server_prewarms_flag_selected_code_mode_host_before_first_turn() -> Result<()> {
+ let model_server = responses::start_mock_server().await;
+ let codex_home = TempDir::new()?;
+ MockResponsesConfig::new(&model_server.uri())
+ .enable_feature(Feature::CodeModePrewarm)
+ .write(codex_home.path())?;
+
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
+ let host_url = format!("ws://{}", listener.local_addr()?);
+ let mut app_server = TestAppServer::builder()
+ .with_codex_home(codex_home.path())
+ .with_args(&["--code-mode-host", &host_url])
+ .build_initialized_with_timeout(DEFAULT_READ_TIMEOUT)
+ .await?;
+ app_server
+ .start_thread(ThreadStartParams::default())
+ .await?;
+
+ let (_stalled_connection, _) = timeout(DEFAULT_READ_TIMEOUT, listener.accept())
+ .await
+ .context("code-mode host was not contacted before the first turn")??;
+ let status = timeout(Duration::from_secs(5), app_server.shutdown_gracefully())
+ .await
+ .context("stalled code-mode prewarm blocked thread shutdown")??;
+ assert!(status.success(), "app-server did not exit successfully");
+ Ok(())
+}
+
async fn assert_shared_remote_code_mode_host(listen_url: &str) -> Result<()> {
let host_program = codex_utils_cargo_bin::cargo_bin("codex-code-mode-host")?;
let mut code_mode_host = Command::new(host_program)
@@ -91,6 +120,7 @@ async fn assert_shared_remote_code_mode_host(listen_url: &str) -> Result<()> {
let codex_home = TempDir::new()?;
MockResponsesConfig::new(&model_server.uri())
.enable_feature(Feature::CodeModeOnly)
+ .enable_feature(Feature::CodeModePrewarm)
.write(codex_home.path())?;
let original_config = std::fs::read_to_string(codex_home.path().join("config.toml"))?;
let mut app_server = TestAppServer::builder()
diff --git a/codex-rs/code-mode/src/remote_session.rs b/codex-rs/code-mode/src/remote_session.rs
index 502642129f..5433b88f38 100644
--- a/codex-rs/code-mode/src/remote_session.rs
+++ b/codex-rs/code-mode/src/remote_session.rs
@@ -184,6 +184,8 @@ struct OwnedCodeModeHost {
endpoint: HostEndpoint,
connection: StdMutex