From b8f6787e95a5006d91bc712f59d72af32ef9b205 Mon Sep 17 00:00:00 2001 From: Dylan Hurd Date: Tue, 17 Nov 2026 20:49:31 +0000 Subject: [PATCH] Stabilize TUI startup tests and strengthen daemon recovery coverage (#62116) ## Why Model fetches can delay the startup screens that PTY tests wait for. Daemon compatibility recovery also probes the daemon version on a separate connection before offering a recovery choice. ## What changed - Configure the daemon compatibility and external editor tests with a local catalog of bundled models to avoid model-fetch delays. - Return a complete `InitializeResponse` from the mock daemon and verify the version probe's `initialize`/`initialized` handshake before selecting recovery. - Keep the daemon listener open and assert that the TUI does not reconnect after choosing to run without the daemon, for both explicit feature enable and disable scenarios. GitOrigin-RevId: 243f5075fca42d1192f0087e2cc21a6b0796226b --- .../tui/tests/suite/daemon_compatibility.rs | 65 +++++++++++++++++-- .../tui/tests/suite/external_editor_tests.rs | 4 +- codex-rs/tui/tests/suite/focus_palette.rs | 18 +++++ 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/codex-rs/tui/tests/suite/daemon_compatibility.rs b/codex-rs/tui/tests/suite/daemon_compatibility.rs index 86e8b77910..fc0ff63f30 100644 --- a/codex-rs/tui/tests/suite/daemon_compatibility.rs +++ b/codex-rs/tui/tests/suite/daemon_compatibility.rs @@ -1,26 +1,46 @@ //! Exercises daemon compatibility recovery through real TUI startup. use super::focus_palette::PtyCodex; -use super::focus_palette::write_test_config; +use super::focus_palette::write_test_config_with_bundled_models; use anyhow::Context; use anyhow::Result; +use anyhow::bail; use anyhow::ensure; +use codex_app_server_protocol::InitializeResponse; use codex_app_server_protocol::JSONRPCMessage; use futures::SinkExt; use futures::StreamExt; use serde_json::json; use tokio::net::UnixListener; +use tokio::net::UnixStream; +use tokio_tungstenite::WebSocketStream; use tokio_tungstenite::tungstenite::Message; +async fn read_version_probe_message( + probe: &mut WebSocketStream, +) -> Result { + let frame = probe + .next() + .await + .context("daemon version probe disconnected")??; + Ok(serde_json::from_str(frame.to_text()?)?) +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn incompatible_daemon_can_run_embedded_for_explicit_features() -> Result<()> { for scenario in ["explicit enable", "explicit disable"] { let cwd = codex_utils_cargo_bin::repo_root()?; let home = tempfile::tempdir_in("/tmp")?; - write_test_config(home.path(), &cwd)?; + write_test_config_with_bundled_models(home.path(), &cwd)?; let socket = codex_app_server_client::app_server_control_socket_path(home.path())?; std::fs::create_dir_all(socket.parent().unwrap())?; let listener = UnixListener::bind(socket.as_path())?; + let initialize = InitializeResponse { + user_agent: "daemon-test/0.0.0".into(), + codex_home: home.path().try_into()?, + platform_family: std::env::consts::FAMILY.into(), + platform_os: std::env::consts::OS.into(), + }; let server = tokio::spawn(async move { let mut socket = loop { let (stream, _) = listener.accept().await?; @@ -34,7 +54,7 @@ async fn incompatible_daemon_can_run_embedded_for_explicit_features() -> Result< }; let response = match request.method.as_str() { "initialize" => { - json!({"id": request.id, "result": {"userAgent": "daemon-test/0.0.0"}}) + json!({"id": request.id, "result": initialize}) } "config/read" => { json!({"id": request.id, "result": {"config": {}, "layers": []}}) @@ -53,7 +73,30 @@ async fn incompatible_daemon_can_run_embedded_for_explicit_features() -> Result< .send(Message::Text(response.to_string().into())) .await?; } - Ok::<_, anyhow::Error>(()) + // Recovery checks the daemon version on a new connection before offering a choice. + let (stream, _) = listener.accept().await?; + let mut probe = tokio_tungstenite::accept_async(stream).await?; + let JSONRPCMessage::Request(request) = read_version_probe_message(&mut probe).await? + else { + bail!("expected initialize request from the daemon version probe"); + }; + ensure!( + request.method == "initialize" + && request.params.as_ref().is_some_and( + |params| params["clientInfo"]["name"] == "codex_app_server_daemon" + ), + "expected the daemon version probe after the compatibility check", + ); + let response = json!({"id": request.id, "result": initialize}); + probe + .send(Message::Text(response.to_string().into())) + .await?; + let initialized = read_version_probe_message(&mut probe).await?; + ensure!( + matches!(initialized, JSONRPCMessage::Notification(n) if n.method == "initialized"), + "daemon version probe did not acknowledge initialize", + ); + Ok::<_, anyhow::Error>(listener.into_std()?) }); let args = if scenario == "explicit disable" { vec!["-c", "features.api_key_model_discovery=false"] @@ -62,6 +105,9 @@ async fn incompatible_daemon_can_run_embedded_for_explicit_features() -> Result< }; let mut terminal = PtyCodex::start(&cwd, home, &args)?; terminal.wait_for_screen("Background server has incompatible feature settings")?; + let listener = tokio::time::timeout(std::time::Duration::from_secs(/*secs*/ 5), server) + .await + .context("recovery prompt appeared without a completed daemon version probe")???; terminal.write_input(b"1")?; terminal.wait_for_startup()?; terminal.wait_for_screen("warning")?; @@ -87,11 +133,18 @@ async fn incompatible_daemon_can_run_embedded_for_explicit_features() -> Result< terminal.write_input(b"\x14")?; terminal.write_input(b"/status")?; terminal.wait_for_screen("show current session configuration")?; - terminal.read_output(std::time::Duration::from_millis(/*millis*/ 200))?; terminal.write_input(b"\r")?; terminal.wait_for_screen("Model:")?; ensure!(!terminal.screen_contains("unix://")); - tokio::time::timeout(std::time::Duration::from_secs(/*secs*/ 5), server).await???; + // No one has accepted since the probe. Even a retry that immediately disconnected + // and fell back to embedded would still be queued on this nonblocking listener. + match listener.accept() { + Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => {} + Err(err) => return Err(err.into()), + Ok(_) => { + bail!("TUI reconnected to the daemon after choosing to run without it ({scenario})") + } + } } Ok(()) } diff --git a/codex-rs/tui/tests/suite/external_editor_tests.rs b/codex-rs/tui/tests/suite/external_editor_tests.rs index 70e356f52c..c4ac0b6ea0 100644 --- a/codex-rs/tui/tests/suite/external_editor_tests.rs +++ b/codex-rs/tui/tests/suite/external_editor_tests.rs @@ -1,7 +1,7 @@ //! Exercise editor handoff through a terminal, including editors that switch screens. use super::PtyCodex; -use super::write_test_config; +use super::write_test_config_with_bundled_models; use anyhow::Result; use anyhow::ensure; use std::os::fd::AsRawFd; @@ -19,7 +19,7 @@ fn external_editors_keep_the_screen_and_return_the_draft() -> Result<()> { ("", false), ] { let home = tempfile::tempdir()?; - write_test_config(home.path(), &repo_root)?; + write_test_config_with_bundled_models(home.path(), &repo_root)?; let editor = home.path().join("editor.sh"); codex_utils_cargo_bin::write_executable( &editor, diff --git a/codex-rs/tui/tests/suite/focus_palette.rs b/codex-rs/tui/tests/suite/focus_palette.rs index 62bd74fd72..3d3bd15fe5 100644 --- a/codex-rs/tui/tests/suite/focus_palette.rs +++ b/codex-rs/tui/tests/suite/focus_palette.rs @@ -568,6 +568,24 @@ fn contains_bytes(buffer: &[u8], needle: &[u8]) -> bool { buffer.windows(needle.len()).any(|window| window == needle) } +pub(super) fn write_test_config_with_bundled_models( + codex_home: &Path, + repo_root: &Path, +) -> Result<()> { + write_test_config(codex_home, repo_root)?; + // Model fetches must not delay the startup screens this PTY is waiting for. + let catalog = codex_home.join("models.json"); + std::fs::write( + &catalog, + serde_json::to_vec(&codex_models_manager::bundled_models_response()?)?, + )?; + let catalog = serde_json::to_string(&catalog)?; + let path = codex_home.join("config.toml"); + let config = std::fs::read_to_string(&path)?; + std::fs::write(path, format!("model_catalog_json = {catalog}\n{config}"))?; + Ok(()) +} + pub(super) fn write_test_config(codex_home: &Path, repo_root: &Path) -> Result<()> { let repo_root = repo_root.display(); let config = format!(