From 956aa3f6372ffd73a0df639beff356b3b664b858 Mon Sep 17 00:00:00 2001 From: Felipe Coury Date: Fri, 4 Sep 2026 00:15:35 +0000 Subject: [PATCH] Restore the inline TUI after full-screen overlays (#42641) ## Why Leaving an alternate-screen overlay in inline mode could retain stale overlay cells and leave conversation history scrolled out of view. ## What changed - Invalidate the restored inline viewport after leaving the alternate screen so the next draw repaints it against the main screen. - Reflow transcript history once when a full-height inline viewport returns to the shorter chat layout, including transitions not tracked by the agents overview. ## Testing Added regression coverage for repainting the restored composer and rebuilding history exactly once after a full-screen overlay closes. GitOrigin-RevId: c6d63299fd95731603d8b1d7f04afcd4b92c1681 --- codex-rs/tui/src/app.rs | 12 +++-- codex-rs/tui/src/app/tests.rs | 43 +++++++++++++++++ codex-rs/tui/src/custom_terminal.rs | 73 +++++++++++++++++++++++++++++ codex-rs/tui/src/tui.rs | 2 + 4 files changed, 127 insertions(+), 3 deletions(-) diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index 76944a3a4a..493d281f9b 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -986,11 +986,17 @@ impl App { .chat_widget .selected_index_for_present_view(AGENTS_OVERVIEW_VIEW_ID) .is_some(); - if std::mem::replace( + let dashboard_was_visible = std::mem::replace( &mut self.agents_overview.rendered_full_screen, dashboard_visible, - ) && !dashboard_visible - { + ); + // Full-height inline overlays scroll history off screen without a terminal resize. + // Rebuild it once when returning to a content-height chat viewport. + let restoring_inline_viewport = !tui.is_alt_screen_active() + && tui.terminal.viewport_area.height == screen_size.height + && self.with_chat_widget_frame(screen_size.width, |height, _| height) + < screen_size.height; + if !dashboard_visible && (dashboard_was_visible || restoring_inline_viewport) { self.schedule_immediate_resize_reflow(tui); self.maybe_run_resize_reflow(tui, screen_size)?; } diff --git a/codex-rs/tui/src/app/tests.rs b/codex-rs/tui/src/app/tests.rs index bd29376141..3737f1cf27 100644 --- a/codex-rs/tui/src/app/tests.rs +++ b/codex-rs/tui/src/app/tests.rs @@ -5947,6 +5947,49 @@ async fn resize_reflow_wraps_transcript_early_when_pet_is_enabled() { ); } +#[tokio::test] +async fn closing_fullscreen_inline_overlay_restores_history_once() -> Result<()> { + let (mut app, _rx, _op_rx) = make_test_app_with_channels().await; + app.transcript_cells = vec![plain_line_cell("Previous conversation")]; + let mut tui = crate::tui::test_support::make_test_tui()?; + tui.set_alt_screen_enabled(/*enabled*/ false); + let screen_size = tui.terminal.last_known_screen_size; + tui.insert_history_lines(vec![Line::from("Previous conversation")]); + app.render_chat_widget_frame(&mut tui, screen_size)?; + + // Content-height popups keep the existing history without rebuilding scrollback. + let popup_height = screen_size.height - 1; + tui.draw(popup_height, |_| {})?; + app.render_chat_widget_frame(&mut tui, screen_size)?; + assert!(app.last_rendered_history_tail.is_none()); + + tui.enter_alt_screen()?; + tui.draw(screen_size.height, |_| {})?; + tui.leave_alt_screen()?; + app.render_chat_widget_frame(&mut tui, screen_size)?; + + assert!(tui.terminal.viewport_area.height < screen_size.height); + assert!(tui.terminal.viewport_area.y > 0); + let tail = app + .last_rendered_history_tail + .as_ref() + .expect("fullscreen exit rebuilt history"); + assert_snapshot!( + tail.lines + .iter() + .map(rendered_line_text) + .collect::>() + .join("\n"), + @"Previous conversation" + ); + + let deferred = vec![Line::from("Pending history").into()]; + app.deferred_history_lines = deferred.clone(); + app.render_chat_widget_frame(&mut tui, screen_size)?; + assert_eq!(app.deferred_history_lines, deferred); + Ok(()) +} + #[tokio::test] async fn copy_picker_opening_preserves_terminal_scrollback_without_reflow() { let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await; diff --git a/codex-rs/tui/src/custom_terminal.rs b/codex-rs/tui/src/custom_terminal.rs index 39eb5f1b08..64e163f18b 100644 --- a/codex-rs/tui/src/custom_terminal.rs +++ b/codex-rs/tui/src/custom_terminal.rs @@ -1009,6 +1009,79 @@ mod tests { "); } + #[tokio::test] + async fn leaving_alternate_screen_repaints_restored_inline_viewport() { + let mut tui = crate::tui::test_support::make_test_tui().expect("test tui"); + let size = tui.terminal.last_known_screen_size; + let inline = Rect::new(/*x*/ 0, /*y*/ 3, size.width, /*height*/ 6); + tui.terminal.set_viewport_area(inline); + tui.enter_alt_screen().expect("enter alternate screen"); + tui.terminal + .draw(|frame| { + Paragraph::new(vec![ + Line::from(" Resume a previous session"), + Line::default(), + Line::from(" Type to search".dim()), + ]) + .render(frame.area(), frame.buffer_mut()); + }) + .expect("draw picker"); + tui.leave_alt_screen().expect("leave alternate screen"); + + let mut terminal = Terminal::with_options(VT100Backend::new(size.width, size.height)) + .expect("physical terminal"); + write!(terminal.backend_mut(), "Previous conversation").expect("write history"); + terminal.set_viewport_area(inline); + terminal + .draw(|frame| { + Paragraph::new(vec![ + Line::default(), + Line::default(), + Line::from(vec!["›".bold(), " ".into(), "/resume".dim()]), + ]) + .render(frame.area(), frame.buffer_mut()); + }) + .expect("draw submitted command"); + + // Apply the real Tui screen-return baseline to the unchanged physical main screen. + // Matching spaces and letters in the picker must not leave /resume cells behind. + *terminal.previous_buffer_mut() = tui.terminal.previous_buffer().clone(); + for _ in 0..2 { + terminal + .draw(|frame| { + Paragraph::new(vec![ + Line::default(), + Line::default(), + Line::from(vec![ + "›".bold(), + " ".into(), + "Ask Codex to do anything".dim(), + ]), + ]) + .render(frame.area(), frame.buffer_mut()); + }) + .expect("restore composer"); + } + + let visible = terminal + .backend() + .vt100() + .screen() + .contents() + .lines() + .map(str::trim_end) + .collect::>() + .join("\n"); + assert_snapshot!(visible.trim_end(), @r" + Previous conversation + + + + + › Ask Codex to do anything + "); + } + #[test] fn ordinary_redraws_with_known_size_do_not_query_backend_size() { let mut terminal = diff --git a/codex-rs/tui/src/tui.rs b/codex-rs/tui/src/tui.rs index 814e01d844..96b3fbcbb9 100644 --- a/codex-rs/tui/src/tui.rs +++ b/codex-rs/tui/src/tui.rs @@ -861,6 +861,8 @@ impl Tui { if let Some(saved) = self.alt_saved_viewport.take() { self.terminal.set_viewport_area(saved); } + // The restored main screen does not contain the alternate screen's diff baseline. + self.terminal.invalidate_viewport(); self.alt_screen_active.store(false, Ordering::Relaxed); Ok(()) }