Improve paginated TUI history loading (#36949)

## Why

Paginated sessions can have visible messages beyond the first item page, so
bounded hydration could leave resumed history or transcript previews empty and
the transcript viewer incomplete.

## What changed

- Scan paginated history until the configured rendered-row budget is filled,
  while retaining the configured scrollback beyond the visible viewport.
- Fully hydrate transcripts opened from the resume picker and build previews
  from bounded item pages, including when non-visible items precede messages.
- Stop pagination on repeated cursors and let `Esc` cancel an in-flight
  transcript load.
- Replay restored token usage for cold metadata-only resumes when its stored
  turn can be identified.

## Testing

- Add coverage for paginated and legacy transcript loading, bounded resume
  hydration, repeated cursors, transcript cancellation, and resize reflow.

GitOrigin-RevId: 2ce69f7d5eae88e2b09a27c333b00b1830ff17bf
This commit is contained in:
Felipe Coury authored and copyberry committed 2026-08-04 19:05:45 +00:00
1 parent 8bfa49e350
commit 3b8d22ec2c
13 files changed
+721 -125

No files matched your search

+24 -1
View File
@@ -19,6 +19,7 @@ use std::sync::Arc;
use std::time::Instant;
use color_eyre::eyre::Result;
use ratatui::layout::Size;
use ratatui::text::Line;
use super::App;
@@ -192,7 +193,8 @@ impl App {
return;
}
let max_rows = self.resize_reflow_max_rows();
let max_rows =
crate::resize_reflow_cap::resize_reflow_max_rows(self.config.terminal_resize_reflow);
if let Some(buffer) = &mut self.initial_history_replay_buffer {
if let Some(max_rows) = max_rows {
Self::buffer_initial_history_replay_display_lines(buffer, display, max_rows);
@@ -239,6 +241,19 @@ impl App {
crate::resize_reflow_cap::resize_reflow_max_rows(self.config.terminal_resize_reflow)
}
pub(super) fn update_visible_history_rows(&mut self, screen_size: Size) {
let width = screen_size.width.max(/*other*/ 1);
let viewport_height = self
.with_chat_widget_frame(width, |desired_height, _| desired_height)
.min(screen_size.height);
self.transcript_reflow.set_visible_history_rows(
screen_size
.height
.saturating_sub(viewport_height)
.max(/*other*/ 1),
);
}
fn clear_terminal_for_resize_replay(&mut self, tui: &mut tui::Tui) -> Result<()> {
if tui.is_alt_screen_active() {
tui.terminal.clear_visible_screen()?;
@@ -316,6 +331,10 @@ impl App {
last_known_screen_size: ratatui::layout::Size,
frame_requester: &tui::FrameRequester,
) -> bool {
if size != last_known_screen_size || self.transcript_reflow.visible_history_rows().is_none()
{
self.update_visible_history_rows(size);
}
let width = self.transcript_reflow.note_width(size.width);
let reflow_needed = self.transcript_reflow.reflow_needed_for_width(size.width);
let height_changed = size.height != last_known_screen_size.height;
@@ -570,3 +589,7 @@ impl App {
< self.transcript_cells.len()
}
}
#[cfg(test)]
#[path = "resize_reflow_tests.rs"]
mod tests;
+144
View File
@@ -0,0 +1,144 @@
use super::*;
use crate::app::test_support::make_test_app;
use crate::history_cell::PlainHistoryCell;
use crate::legacy_core::config::TerminalResizeReflowMaxRows;
use pretty_assertions::assert_eq;
fn plain_history_cells(count: usize) -> Vec<Arc<dyn HistoryCell>> {
(0..count)
.map(|index| {
Arc::new(PlainHistoryCell::new(vec![Line::from(format!(
"cell {index}"
))])) as Arc<dyn HistoryCell>
})
.collect()
}
fn rendered_line_text(line: &HyperlinkLine) -> String {
line.line
.spans
.iter()
.map(|span| span.content.as_ref())
.collect()
}
#[tokio::test]
async fn resize_reflow_preserves_configured_scrollback_beyond_the_visible_viewport() {
let mut app = make_test_app().await;
app.config.terminal_resize_reflow.max_rows = TerminalResizeReflowMaxRows::Limit(32);
app.transcript_cells = plain_history_cells(/*count*/ 64);
let screen_size = Size::new(/*width*/ 80, /*height*/ 24);
let chat_height = app.with_chat_widget_frame(screen_size.width, |height, _| height);
let visible_history_rows = screen_size
.height
.saturating_sub(chat_height)
.max(/*other*/ 1);
app.update_visible_history_rows(screen_size);
let rendered = app.render_transcript_lines_for_reflow(screen_size.width);
assert_eq!(app.resize_reflow_max_rows(), Some(32));
assert_eq!(rendered.lines.len(), 32);
assert!(rendered.lines.len() > usize::from(visible_history_rows));
assert_eq!(
rendered.lines.last().map(rendered_line_text),
Some("cell 63".to_string())
);
}
#[tokio::test]
async fn initial_resume_replay_retains_scrollback_beyond_the_visible_viewport() -> Result<()> {
let mut app = make_test_app().await;
app.config.terminal_resize_reflow.max_rows = TerminalResizeReflowMaxRows::Limit(32);
let screen_size = Size::new(/*width*/ 80, /*height*/ 24);
app.update_visible_history_rows(screen_size);
let visible_history_rows = app
.transcript_reflow
.visible_history_rows()
.expect("visible history row budget");
app.begin_initial_history_replay_buffer();
let mut tui = crate::tui::test_support::make_test_tui()?;
for cell in plain_history_cells(/*count*/ 24) {
app.insert_history_cell_lines_with_initial_replay_buffer(
&mut tui,
cell.as_ref(),
screen_size.width,
);
}
let retained_lines = &app
.initial_history_replay_buffer
.as_ref()
.expect("initial replay buffer should remain active")
.retained_lines;
assert_eq!(retained_lines.len(), 32);
assert!(retained_lines.len() > usize::from(visible_history_rows));
insta::assert_snapshot!(
retained_lines
.iter()
.rev()
.take(/*n*/ 3)
.map(rendered_line_text)
.collect::<Vec<_>>()
.join("\n"),
@r"
cell 23
cell 22
"
);
Ok(())
}
#[tokio::test]
async fn resize_reflow_preserves_configured_scrollback_when_the_terminal_height_changes() {
let mut app = make_test_app().await;
app.config.terminal_resize_reflow.max_rows = TerminalResizeReflowMaxRows::Limit(48);
app.transcript_cells = plain_history_cells(/*count*/ 64);
app.update_visible_history_rows(Size::new(/*width*/ 80, /*height*/ 24));
let smaller = app.render_transcript_lines_for_reflow(/*width*/ 80);
app.update_visible_history_rows(Size::new(/*width*/ 80, /*height*/ 48));
let larger = app.render_transcript_lines_for_reflow(/*width*/ 80);
assert_eq!(smaller.lines.len(), 48);
assert_eq!(larger.lines.len(), 48);
assert_eq!(
smaller
.lines
.iter()
.map(rendered_line_text)
.collect::<Vec<_>>(),
larger
.lines
.iter()
.map(rendered_line_text)
.collect::<Vec<_>>()
);
assert_eq!(
larger.lines.last().map(rendered_line_text),
Some("cell 63".to_string())
);
}
#[tokio::test]
async fn resize_reflow_preserves_explicitly_unlimited_history() {
let mut app = make_test_app().await;
app.config.terminal_resize_reflow.max_rows = TerminalResizeReflowMaxRows::Disabled;
app.transcript_cells = plain_history_cells(/*count*/ 20);
app.update_visible_history_rows(Size::new(/*width*/ 80, /*height*/ 24));
let rendered = app.render_transcript_lines_for_reflow(/*width*/ 80);
assert_eq!(app.resize_reflow_max_rows(), None);
assert_eq!(rendered.lines.len(), 39);
assert_eq!(
rendered.lines.first().map(rendered_line_text),
Some("cell 0".to_string())
);
assert_eq!(
rendered.lines.last().map(rendered_line_text),
Some("cell 19".to_string())
);
}
@@ -186,6 +186,7 @@ async fn older_pagination_reconciles_review_prompts_across_page_boundaries() ->
let codex_home = tempdir()?;
app.config.codex_home = codex_home.path().to_path_buf().abs();
app.config.sqlite = SqliteConfig::new_for_testing(codex_home.path().abs());
app.config.terminal_resize_reflow.max_rows = TerminalResizeReflowMaxRows::Limit(100);
let thread_id = create_fake_paginated_rollout(
codex_home.path(),
"2026-01-02T00-00-00",