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

+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())
);
}