diff --git a/codex-rs/tui/src/tui_startup_tests.rs b/codex-rs/tui/src/tui_startup_tests.rs index 3518709ac4..741b830cb2 100644 --- a/codex-rs/tui/src/tui_startup_tests.rs +++ b/codex-rs/tui/src/tui_startup_tests.rs @@ -16,6 +16,8 @@ use tokio_stream::StreamExt; use super::TuiEvent; const STARTUP_INPUT_CHILD_TEST: &str = "tui::startup_tests::startup_typeahead_pty_child"; +const PASTE_BOUNDARY: &[u8] = b"\x1b[201~\x1b[12;3R"; +const UNREAD_ACTION: &[u8] = b"1\r"; #[test] fn startup_preserves_typeahead_and_discards_buffered_action_key_after_first_draw() { @@ -63,9 +65,7 @@ fn startup_preserves_typeahead_and_discards_buffered_action_key_after_first_draw let fresh_ready = signals.path().join("fresh-ready"); let fresh_queued = signals.path().join("fresh-queued"); let boundary_ready = signals.path().join("boundary-ready"); - let boundary_queued = signals.path().join("boundary-queued"); let boundary_consumed = signals.path().join("boundary-consumed"); - let boundary_action_queued = signals.path().join("boundary-action-queued"); let control_ready = signals.path().join("control-ready"); let control_queued = signals.path().join("control-queued"); let mut child = Command::new(std::env::current_exe().expect("current test binary")) @@ -141,16 +141,12 @@ fn startup_preserves_typeahead_and_discards_buffered_action_key_after_first_draw wait_for_file(&boundary_ready)?; master - .write_all(b"\x1b[201~\x1b[12;3R") + .write_all(PASTE_BOUNDARY) .map_err(|err| format!("failed to queue completed paste boundary: {err}"))?; - std::fs::write(&boundary_queued, []) - .map_err(|err| format!("failed to signal queued paste boundary: {err}"))?; wait_for_file(&boundary_consumed)?; master - .write_all(b"1\r") + .write_all(UNREAD_ACTION) .map_err(|err| format!("failed to queue unread action after paste boundary: {err}"))?; - std::fs::write(&boundary_action_queued, []) - .map_err(|err| format!("failed to signal queued unread action: {err}"))?; wait_for_file(&control_ready)?; master @@ -411,7 +407,7 @@ async fn startup_typeahead_pty_child() { crossterm::event::InputDiscardStatus::BracketedPasteInProgress ); std::fs::write(signals.join("boundary-ready"), []).expect("signal paste-boundary readiness"); - wait_for_file(&signals.join("boundary-queued")).expect("wait for the paste boundary"); + wait_for_unread_input(PASTE_BOUNDARY); assert!( !crossterm::event::poll(Duration::ZERO).expect("poll a completed paste with unread input") ); @@ -420,8 +416,7 @@ async fn startup_typeahead_pty_child() { crossterm::event::InputDiscardStatus::Complete ); std::fs::write(signals.join("boundary-consumed"), []).expect("signal completed paste boundary"); - wait_for_file(&signals.join("boundary-action-queued")).expect("wait for the unread action"); - assert!(super::terminal_input_is_readable().expect("check unread input after an elapsed poll")); + wait_for_unread_input(UNREAD_ACTION); tui.discard_pending_input_before_interactive_screen() .expect("discard actionable unread input after the paste boundary"); assert!( @@ -458,6 +453,30 @@ async fn startup_typeahead_pty_child() { .expect("restore terminal after startup draft handoff"); } +// A completed master write can reach the slave in fragments. Wait for the +// entire sequence without consuming bytes ahead of Crossterm's zero-time poll. +fn wait_for_unread_input(input: &[u8]) { + let started_at = Instant::now(); + loop { + let mut pending: libc::c_int = 0; + // Safety: stdin is the child PTY and FIONREAD writes a single int. + assert_eq!( + unsafe { libc::ioctl(libc::STDIN_FILENO, libc::FIONREAD, &mut pending) }, + 0, + "query unread PTY input: {}", + std::io::Error::last_os_error() + ); + if usize::try_from(pending).is_ok_and(|count| count >= input.len()) { + return; + } + assert!( + started_at.elapsed() < Duration::from_secs(/*secs*/ 5), + "only {pending} bytes of {input:?} reached the PTY" + ); + std::thread::sleep(Duration::from_millis(/*millis*/ 10)); + } +} + fn wait_for_file(path: &Path) -> Result<(), String> { let started_at = Instant::now(); while !path.exists() {