From fc25d8f1203a152fe44d7a980edcab1a55f08fc9 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Wed, 18 Nov 2026 02:53:34 +0000 Subject: [PATCH] Fix stale terminal dimensions in immediate TUI redraws (#62175) ## Why An immediate redraw can happen before the TUI receives a resize event. For example, submitting a turn just after shrinking the terminal can render the frame using the previous dimensions. ## What changed Resolve the screen size and run resize handling before immediate paints for turn submission, session lifecycle actions, startup modals, Cloud switching, and shutdown. Consume the reserved size when drawing the frame and preserve the scheduled recheck for settled terminal dimensions. ## Testing Add regression coverage for fullscreen turn submission and archive progress after a terminal shrink, plus checks that a draw consumes its reserved size and an early paint preserves the scheduled size recheck. GitOrigin-RevId: 494c746cb02712350cc600ea4aea64460c608639 --- codex-rs/tui/src/app.rs | 2 +- .../tui/src/app/agents_overview_actions.rs | 4 +- codex-rs/tui/src/app/cloud_switch_wait.rs | 16 +++++--- codex-rs/tui/src/app/event_dispatch.rs | 11 +++-- .../tui/src/app/owned_transcript_tests.rs | 28 +++++++++++++ codex-rs/tui/src/app/startup.rs | 4 +- .../app/tests/session_lifecycle_requests.rs | 17 ++++++++ codex-rs/tui/src/app/thread_routing.rs | 4 +- codex-rs/tui/src/tui.rs | 2 + codex-rs/tui/src/tui/screen_size.rs | 5 ++- codex-rs/tui/src/tui/screen_size_tests.rs | 40 +++++++++++++++++++ 11 files changed, 118 insertions(+), 15 deletions(-) diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index 1a1848ad91..a83022f433 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -1501,7 +1501,7 @@ impl App { pub(super) fn show_shutdown_feedback(&mut self, tui: &mut tui::Tui) -> Result<()> { self.disable_ambient_pet_before_shutdown(tui)?; self.chat_widget.show_shutdown_in_progress(); - let screen_size = tui.terminal.last_known_screen_size; + let screen_size = tui.prepare_draw_size()?; self.handle_draw_pre_render(tui, screen_size)?; self.chat_widget.pre_draw_tick(); self.render_chat_widget_frame(tui, screen_size)?; diff --git a/codex-rs/tui/src/app/agents_overview_actions.rs b/codex-rs/tui/src/app/agents_overview_actions.rs index d5414302f4..af62e02947 100644 --- a/codex-rs/tui/src/app/agents_overview_actions.rs +++ b/codex-rs/tui/src/app/agents_overview_actions.rs @@ -57,7 +57,9 @@ async fn run_lifecycle_modal( operation: impl Future, events: impl Stream + Unpin, ) -> color_eyre::Result { - app.render_chat_widget_frame(tui, tui.terminal.last_known_screen_size)?; + let screen_size = tui.prepare_draw_size()?; + app.handle_draw_pre_render(tui, screen_size)?; + app.render_chat_widget_frame(tui, screen_size)?; let mut events = events.fuse(); tokio::pin!(operation); let result = loop { diff --git a/codex-rs/tui/src/app/cloud_switch_wait.rs b/codex-rs/tui/src/app/cloud_switch_wait.rs index 5802d6d618..c18920b026 100644 --- a/codex-rs/tui/src/app/cloud_switch_wait.rs +++ b/codex-rs/tui/src/app/cloud_switch_wait.rs @@ -14,7 +14,6 @@ use crossterm::event::KeyCode; use crossterm::event::KeyEvent; use crossterm::event::KeyEventKind; use crossterm::event::KeyModifiers; -use ratatui::layout::Size; use std::future::Future; use std::ops::ControlFlow; use tokio_stream::Stream; @@ -120,7 +119,7 @@ fn handle_cloud_switch_event( return ControlFlow::Continue(()); } app.chat_widget.pre_draw_tick(); - draw_cloud_switch(app, tui, screen_size); + draw_cloud_switch(app, tui); } tui::TuiEvent::FocusLost => { app.cancel_pending_key_chord(); @@ -147,8 +146,13 @@ fn clear_cloud_switch_hint(app: &mut App) { app.chat_widget.set_footer_hint_override(/*items*/ None); } -fn draw_cloud_switch(app: &mut App, tui: &mut tui::Tui, screen_size: Size) { - if let Err(error) = app.render_chat_widget_frame(tui, screen_size) { +fn draw_cloud_switch(app: &mut App, tui: &mut tui::Tui) { + let result = (|| { + let screen_size = tui.prepare_draw_size()?; + app.handle_draw_pre_render(tui, screen_size)?; + app.render_chat_widget_frame(tui, screen_size) + })(); + if let Err(error) = result { tracing::warn!(%error, "failed to render during cloud switch"); } } @@ -161,7 +165,7 @@ pub(super) async fn finish_failed_cloud_switch( ) -> ControlFlow<()> { let progress = CloudSwitchProgress::Failed; show_cloud_switch_hint(app, progress); - draw_cloud_switch(app, tui, tui.terminal.last_known_screen_size); + draw_cloud_switch(app, tui); drain_cloud_switch_events(app, tui, &mut events, progress).await } @@ -220,7 +224,7 @@ pub(crate) async fn wait_for_cloud_switch( let progress = CloudSwitchProgress::Connecting(clipboard_handoff); app.cancel_pending_key_chord(); show_cloud_switch_hint(app, progress); - draw_cloud_switch(app, tui, tui.terminal.last_known_screen_size); + draw_cloud_switch(app, tui); if app.pending_right_click_paste.is_some() { // A completion frame can arrive between Cloud preparation phases. tui.frame_requester().schedule_frame(); diff --git a/codex-rs/tui/src/app/event_dispatch.rs b/codex-rs/tui/src/app/event_dispatch.rs index f93a7d070a..8f4c098009 100644 --- a/codex-rs/tui/src/app/event_dispatch.rs +++ b/codex-rs/tui/src/app/event_dispatch.rs @@ -769,7 +769,7 @@ impl App { } self.chat_widget.fork_in_progress = true; // This handler awaits the fork outside the draw loop. Paint before waiting. - let screen_size = tui.terminal.last_known_screen_size; + let screen_size = tui.prepare_draw_size()?; self.handle_draw_pre_render(tui, screen_size)?; self.chat_widget.pre_draw_tick(); self.render_chat_widget_frame(tui, screen_size)?; @@ -1485,7 +1485,7 @@ impl App { | AppCommand::RealtimeConversationSpeech { .. } ); if is_user_turn { - let screen_size = tui.terminal.last_known_screen_size; + let screen_size = tui.prepare_draw_size()?; self.handle_draw_pre_render(tui, screen_size)?; if self.transcript_reflow.has_pending_reflow() { self.transcript_reflow.schedule_immediate(); @@ -4547,8 +4547,11 @@ impl App { self.chat_widget .set_footer_hint_override(Some(vec![("Archiving…".into(), String::new())])); // Cleanup waits outside the event loop. Paint before sending its first request. - let screen_size = tui.terminal.last_known_screen_size; - if let Err(error) = self.render_chat_widget_frame(tui, screen_size) { + if let Err(error) = (|| -> Result<_> { + let screen_size = tui.prepare_draw_size()?; + self.handle_draw_pre_render(tui, screen_size)?; + self.render_chat_widget_frame(tui, screen_size) + })() { tracing::warn!(%error, "failed to render archive progress"); } } diff --git a/codex-rs/tui/src/app/owned_transcript_tests.rs b/codex-rs/tui/src/app/owned_transcript_tests.rs index 5aeeea699a..6b76d69a08 100644 --- a/codex-rs/tui/src/app/owned_transcript_tests.rs +++ b/codex-rs/tui/src/app/owned_transcript_tests.rs @@ -2027,6 +2027,34 @@ async fn fullscreen_composer_mouse_copy_and_input_ownership() -> Result<()> { Ok(()) } +#[tokio::test] +async fn user_turn_redraw_handles_terminal_shrink_without_resize_event() -> Result<()> { + let (mut app, _events, mut ops) = make_test_app_with_channels().await; + attach_thread(&mut app, ThreadId::new()); + app.local_settings.tui.animations = false; + let mut server = Box::pin(crate::start_embedded_app_server_for_picker(&app.config)).await?; + let mut tui = crate::tui::test_support::make_test_tui()?; + tui.set_owned_screen(/*owned*/ true)?; + let backend_size = tui.terminal.size()?; + tui.terminal + .resize(Size::new(backend_size.width, backend_size.height + 13))?; + + app.chat_widget + .apply_external_edit("resize regression".into()); + app.chat_widget.handle_key_event(KeyCode::Enter.into()); + let turn = std::iter::from_fn(|| ops.try_recv().ok()) + .find(|op| matches!(op, AppCommand::UserTurn { .. })) + .expect("Enter submits a user turn"); + Box::pin(app.handle_event(&mut tui, &mut server, AppEvent::CodexOp(turn))).await?; + + assert_eq!(tui.terminal.last_known_screen_size, backend_size); + let buffer = crate::custom_terminal::test_support::last_rendered_buffer(&tui.terminal); + assert_eq!(buffer.area, Rect::from(backend_size)); + server.shutdown().await?; + tui.set_owned_screen(/*owned*/ false)?; + Ok(()) +} + #[tokio::test] async fn inline_confirmation_preserves_compact_viewport_without_replaying_history() -> Result<()> { let mut app = crate::app::test_support::make_test_app().await; diff --git a/codex-rs/tui/src/app/startup.rs b/codex-rs/tui/src/app/startup.rs index 9e51c9be30..44f74d575e 100644 --- a/codex-rs/tui/src/app/startup.rs +++ b/codex-rs/tui/src/app/startup.rs @@ -190,7 +190,9 @@ impl App { return Ok(()); } self.chat_widget.pre_draw_tick(); - self.render_chat_widget_frame(tui, tui.terminal.last_known_screen_size)?; + let screen_size = tui.prepare_draw_size()?; + self.handle_draw_pre_render(tui, screen_size)?; + self.render_chat_widget_frame(tui, screen_size)?; if self.chat_widget.has_active_modal() && self.startup_protected_input_boundary { self.discard_startup_modal_input(tui)?; } diff --git a/codex-rs/tui/src/app/tests/session_lifecycle_requests.rs b/codex-rs/tui/src/app/tests/session_lifecycle_requests.rs index a424cf1114..c0ffb0290c 100644 --- a/codex-rs/tui/src/app/tests/session_lifecycle_requests.rs +++ b/codex-rs/tui/src/app/tests/session_lifecycle_requests.rs @@ -2617,6 +2617,15 @@ async fn archive_opens_command_center_before_the_server_replies() -> Result<()> #[tokio::test] async fn archive_shows_progress_and_keeps_the_session_when_side_chat_cleanup_fails() -> Result<()> { + archive_progress_scenario(/*owned_screen*/ false).await +} + +#[tokio::test] +async fn fullscreen_archive_progress_handles_terminal_shrink() -> Result<()> { + archive_progress_scenario(/*owned_screen*/ true).await +} + +async fn archive_progress_scenario(owned_screen: bool) -> Result<()> { use codex_app_server_client::AppServerClient; use codex_app_server_client::CloudClient; use codex_app_server_client::CloudOperation; @@ -2641,6 +2650,11 @@ async fn archive_shows_progress_and_keeps_the_session_when_side_chat_cleanup_fai app.side_threads .insert(side_id, SideThreadState::new(thread_id)); let mut tui = crate::tui::test_support::make_test_tui()?; + if owned_screen { + tui.set_owned_screen(/*owned*/ true)?; + tui.terminal + .resize(Size::new(/*width*/ 80, /*height*/ 37))?; + } let mut archive = Box::pin(app.handle_event(&mut tui, &mut server, AppEvent::ArchiveCurrentThread)); @@ -2692,6 +2706,9 @@ async fn archive_shows_progress_and_keeps_the_session_when_side_chat_cleanup_fai "Unsent draft" ); assert!(!render_bottom_popup(&app.chat_widget, /*width*/ 80).contains("Archiving")); + if owned_screen { + tui.set_owned_screen(/*owned*/ false)?; + } Ok(()) } diff --git a/codex-rs/tui/src/app/thread_routing.rs b/codex-rs/tui/src/app/thread_routing.rs index 3891ae0356..f1a0b5fc6a 100644 --- a/codex-rs/tui/src/app/thread_routing.rs +++ b/codex-rs/tui/src/app/thread_routing.rs @@ -2300,7 +2300,9 @@ impl App { && self.startup_protected_input_boundary { self.chat_widget.pre_draw_tick(); - self.render_chat_widget_frame(tui, tui.terminal.last_known_screen_size)?; + let screen_size = tui.prepare_draw_size()?; + self.handle_draw_pre_render(tui, screen_size)?; + self.render_chat_widget_frame(tui, screen_size)?; self.discard_startup_modal_input(tui)?; } if self.backtrack_render_pending { diff --git a/codex-rs/tui/src/tui.rs b/codex-rs/tui/src/tui.rs index e32f831d55..6bd03f234f 100644 --- a/codex-rs/tui/src/tui.rs +++ b/codex-rs/tui/src/tui.rs @@ -1612,6 +1612,8 @@ impl Tui { hud: Option, draw_fn: impl FnOnce(&mut custom_terminal::Frame), ) -> Result<()> { + // This draw uses the caller's resolved geometry directly, so its reservation is consumed. + self.screen_size.pending_draw_size = None; // If we are resuming from ^Z, we need to prepare the resume action now so we can apply it // in the synchronized update. #[cfg(unix)] diff --git a/codex-rs/tui/src/tui/screen_size.rs b/codex-rs/tui/src/tui/screen_size.rs index fe315d4c28..668763115c 100644 --- a/codex-rs/tui/src/tui/screen_size.rs +++ b/codex-rs/tui/src/tui/screen_size.rs @@ -13,7 +13,7 @@ use crate::transcript_reflow::TRANSCRIPT_REFLOW_DEBOUNCE; #[derive(Default)] pub(super) struct ScreenSizePolicy { pending_recheck_at: Option, - pending_draw_size: Option, + pub(super) pending_draw_size: Option, deferred_size: Option, } @@ -76,7 +76,10 @@ impl Tui { /// Reserve the next draw's geometry so live layouts use the same width as their frame. pub(crate) fn prepare_draw_size(&mut self) -> io::Result { + // An early paint must not cancel the later sample of settled terminal dimensions. + let pending_recheck_at = self.screen_size.pending_recheck_at; let size = self.take_event_screen_size()?; + self.screen_size.pending_recheck_at = pending_recheck_at; self.screen_size.pending_draw_size = Some(size); Ok(size) } diff --git a/codex-rs/tui/src/tui/screen_size_tests.rs b/codex-rs/tui/src/tui/screen_size_tests.rs index 248f69b0d1..b61442e455 100644 --- a/codex-rs/tui/src/tui/screen_size_tests.rs +++ b/codex-rs/tui/src/tui/screen_size_tests.rs @@ -52,6 +52,46 @@ async fn standalone_resize_draw_rechecks_settled_screen_size_once() { assert!(tui.screen_size.pending_recheck_at.is_none()); } +#[tokio::test] +async fn resize_reflow_draw_consumes_reserved_screen_size() { + let mut tui = crate::tui::test_support::make_test_tui().expect("test tui"); + let backend_size = tui.terminal.size().expect("backend size"); + let resize_size = Size::new(backend_size.width, backend_size.height + 13); + let size = tui + .screen_size_for_event(&TuiEvent::Resize(resize_size)) + .expect("resolve resize"); + + tui.draw_with_resize_reflow(/*height*/ 3, size, /*hud*/ None, |_| {}) + .expect("draw inline frame"); + + assert_eq!( + tui.prepare_draw_size().expect("next draw size"), + backend_size + ); +} + +#[tokio::test] +async fn early_paint_preserves_settled_size_recheck() { + let mut tui = crate::tui::test_support::make_test_tui().expect("test tui"); + let backend_size = tui.terminal.size().expect("backend size"); + tui.schedule_screen_size_recheck(Duration::from_secs(1)); + let deadline = tui.screen_size.pending_recheck_at; + let size = tui.prepare_draw_size().expect("early paint size"); + tui.draw_with_resize_reflow(/*height*/ 3, size, /*hud*/ None, |_| {}) + .expect("early inline paint"); + assert_eq!(tui.screen_size.pending_recheck_at, deadline); + tui.screen_size.pending_recheck_at = Some(std::time::Instant::now()); + tui.terminal + .resize(Size::new(backend_size.width + 16, backend_size.height)) + .expect("intermediate cached size"); + + assert_eq!( + tui.screen_size_for_event(&TuiEvent::Draw) + .expect("settled size"), + backend_size + ); +} + #[tokio::test] async fn entering_alternate_screen_updates_cached_screen_size() { let mut tui = crate::tui::test_support::make_test_tui().expect("test tui");