diff --git a/codex-rs/tui/tests/suite/worktree_stack.rs b/codex-rs/tui/tests/suite/worktree_stack.rs index 52a1c4eaf3..ec4f91e69c 100644 --- a/codex-rs/tui/tests/suite/worktree_stack.rs +++ b/codex-rs/tui/tests/suite/worktree_stack.rs @@ -105,7 +105,7 @@ async fn picker_side_worktree_fork_and_cd_run_on_the_production_stack() -> Resul "-c", "features.shell_snapshot=false", "-c", - "tui.status_line=['run-state']", + "tui.status_line=['thread-id','run-state']", "Reply with the marker requested by this test.", ], )?; @@ -171,8 +171,53 @@ async fn picker_side_worktree_fork_and_cd_run_on_the_production_stack() -> Resul ))?; terminal.ensure_running()?; - submit(&mut terminal, &format!("/cd {}", root.display()))?; - terminal.wait_for_screen(&format!("Working directory changed to: {}", root.display()))?; + // The directory acknowledgement can precede the end of startup for the fork. + let owner = worktrees.owner(&forked[0].root)?.expect("bound fork"); + let ready = format!("{owner} ยท Ready"); + let command = format!("/cd {}", root.display()); + let changed = format!("Working directory changed to: {}", root.display()); + let deadline = Instant::now() + Duration::from_secs(/*secs*/ 30); + terminal.wait_for_screen(&ready)?; + paste(&mut terminal, &command)?; + let mut rejections_at_submit = None; + loop { + let screen = terminal.screen_contents(); + // A PTY read can end partway through a path. Decide only after + // the TUI has finished its synchronized update. + let begin = b"\x1b[?2026h"; + let end = b"\x1b[?2026l"; + let complete = terminal + .output + .windows(begin.len()) + .rev() + .find(|bytes| *bytes == begin || *bytes == end) + .is_some_and(|bytes| bytes == end); + let has_line = |text: &str| screen.lines().any(|line| line.trim_end().ends_with(text)); + if complete && rejections_at_submit.is_some() && has_line(&changed) { + break; + } + ensure!( + Instant::now() < deadline, + "missing {changed:?}; screen:\n{screen}" + ); + + // MCP startup can turn the composer busy between Ready and Enter. A + // rejection keeps the draft and adds a history entry. Only press Enter + // again if the count grows, so a redraw cannot replay an old rejection. + let rejections = screen + .matches("'/cd' is disabled while a task is in progress.") + .count(); + if complete + && screen.contains(&ready) + && has_line(&command) + && rejections_at_submit.is_none_or(|previous| rejections > previous) + { + terminal.write_input(b"\r")?; + rejections_at_submit = Some(rejections); + } + terminal.read_output(Duration::from_millis(/*millis*/ 50))?; + terminal.ensure_running()?; + } terminal.ensure_running()?; submit(&mut terminal, "/worktree")?; @@ -200,10 +245,14 @@ async fn picker_side_worktree_fork_and_cd_run_on_the_production_stack() -> Resul Ok(()) } -fn submit(terminal: &mut PtyCodex, input: &str) -> Result<()> { +fn paste(terminal: &mut PtyCodex, input: &str) -> Result<()> { // Send one paste event so slashes in arguments cannot accept a completion // while earlier characters are still buffered by paste-burst detection. - terminal.write_input(format!("\x1b[200~{input}\x1b[201~").as_bytes())?; + terminal.write_input(format!("\x1b[200~{input}\x1b[201~").as_bytes()) +} + +fn submit(terminal: &mut PtyCodex, input: &str) -> Result<()> { + paste(terminal, input)?; terminal.wait_for_screen(input)?; // A worktree switch can acknowledge the new directory while the composer is still busy. terminal.wait_for_screen("Ready")?;