A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
TUI: complete a PageUp that hits the top while the older page loads (#3724)
Sebastian Müllerbbe4f4cfdaa8
diff --git a/crates/pa-tui/src/session_ui/keys.rs b/crates/pa-tui/src/session_ui/keys.rsindex b5cbf307bf..13095deee1 100644--- a/crates/pa-tui/src/session_ui/keys.rs+++ b/crates/pa-tui/src/session_ui/keys.rs@@ -109,7 +109,7 @@ impl SessionUi {}if let Some(delta) = crate::mouse::wheel_scroll_delta(&event) {if !overlay_focused {- view.scroll_by(delta);+ self.window.scroll(delta, view);self.dirty = true;}return;@@ -270,7 +270,9 @@ impl SessionUi {return;}if view.selection_auto_scroll_direction(armed.row) != Some(armed.direction)- || !view.scroll_selection(armed.direction, armed.col)+ || !self+ .window+ .scroll_selection(armed.direction, armed.col, view){self.selection_auto_scroll = None;return;@@ -408,14 +410,14 @@ impl SessionUi {// The viewport consumes the key before the editor, so a selection must// collapse here or it survives the scroll as a stale replace range.view.editor.clear_selection();- view.scroll_by(-(view.page_size() as isize));+ self.window.scroll(-(view.page_size() as isize), view);self.track_scroll("page_up", view.is_following());self.dirty = true;return Ok(());}if page_down {view.editor.clear_selection();- view.scroll_by(view.page_size() as isize);+ self.window.scroll(view.page_size() as isize, view);self.track_scroll("page_down", view.is_following());self.dirty = true;return Ok(());diff --git a/crates/pa-tui/src/session_ui/window.rs b/crates/pa-tui/src/session_ui/window.rsindex e37d754f6b..bae53f1833 100644--- a/crates/pa-tui/src/session_ui/window.rs+++ b/crates/pa-tui/src/session_ui/window.rs@@ -93,6 +93,8 @@ pub(crate) struct TranscriptWindow {head_requested: bool,/// A page failed: no more pages until the next reset.failed: bool,+ /// Scroll-up rows owed to the older page: it applies them when it lands.+ pending_scroll_up: usize,epoch: u64,/// Dropping the handle aborts the in-flight page; the epoch fences a note/// that was already queued.@@ -137,6 +139,7 @@ impl TranscriptWindow {/// The top key: the session's first page replaces the resident run.pub(crate) fn request_head(&mut self) {self.head_requested = !self.start_resident;+ self.pending_scroll_up = 0;}fn resident_entries(&self) -> usize {@@ -226,6 +229,9 @@ impl TranscriptWindow {if self.chunks.len() > RESIDENT_CHUNKS {grown += self.evict_newest(view);}+ if self.pending_scroll_up > 0 {+ view.scroll_by(-(std::mem::take(&mut self.pending_scroll_up) as isize));+ }grown}PageKind::Newer => {@@ -273,6 +279,28 @@ impl TranscriptWindow {}}+ /// Scroll the viewport; a scroll up at the top of the resident rows, while older history is+ /// still to load, is owed to the older page.+ pub(crate) fn scroll(&mut self, delta: isize, view: &mut AgentView) {+ if delta < 0 && view.viewport_at_top() && !self.start_resident && !self.failed {+ self.pending_scroll_up += delta.unsigned_abs();+ } else {+ self.pending_scroll_up = 0;+ view.scroll_by(delta);+ }+ }++ /// A selection auto-scroll drops the owed rows.+ pub(crate) fn scroll_selection(+ &mut self,+ direction: isize,+ col: usize,+ view: &mut AgentView,+ ) -> bool {+ self.pending_scroll_up = 0;+ view.scroll_selection(direction, col)+ }+/// Evict the chunk next to the tail, leaving the gap row in its place.fn evict_newest(&mut self, view: &mut AgentView) -> isize {let Some(newest) = self.chunks.pop_back() else {@@ -359,6 +387,7 @@ impl TranscriptWindow {/// Drop a disjoint resident run, leaving the contiguous tail. Returns how many/// entries the rows above the tail shrank by.fn collapse_to_tail(&mut self, view: &mut AgentView) -> usize {+ self.pending_scroll_up = 0;if !self.gap {return 0;}@@ -944,4 +973,63 @@ mod tests {assert_eq!(grown, full.len() as isize - 1, "the rows above the tail");assert_eq!(window.tail_start(), full.len() - 1);}++ #[test]+ fn a_page_up_owed_to_the_older_page_moves_the_view_one_page() {+ let entries = |prefix: &str| -> Vec<ChatEntry> {+ (0..30)+ .map(|index| ChatEntry::User {+ text: format!("{prefix} {index}"),+ })+ .collect()+ };+ let land = |paint_before_apply: bool| {+ let mut window = TranscriptWindow::default();+ window.reset(+ Some("leaf".to_string()),+ Some("t0".to_string()),+ BackfillSeam::Preserve,+ );+ let mut view = AgentView::new(crate::theme::Theme::builtin(+ "prime",+ crate::theme::ColorMode::TrueColor,+ ));+ view.splash_suppressed = true;+ for entry in entries("tail") {+ view.push_entry(entry);+ }+ view.render_frame(40, 16);+ while !view.viewport_at_top() {+ window.scroll(-(view.page_size() as isize), &mut view);+ view.render_frame(40, 16);+ }+ window.scroll(-(view.page_size() as isize), &mut view);+ if paint_before_apply {+ view.render_frame(40, 16);+ }+ let page = LoadedPage {+ entries: entries("older"),+ seam: BackfillSeam::Preserve,+ first_id: Some("o0".to_string()),+ last_id: Some("o29".to_string()),+ more: true,+ };+ let request = TranscriptPage::End {+ before: Some("t0".to_string()),+ limit: CHUNK_MESSAGES,+ };+ window.apply(PageKind::Older, request, page, &mut view);+ crate::app::render_frame_text(&mut view, 40, 16).join("\n")+ };+ let painted = land(true);+ assert!(+ painted.contains("older 29"),+ "the press at the top scrolls one page into the older rows:\n{painted}"+ );+ assert_eq!(+ land(false),+ painted,+ "a page that lands before the next paint completes the press once"+ );+ }}diff --git a/crates/pa-tui/src/view/lazy.rs b/crates/pa-tui/src/view/lazy.rsindex 89a5545fb9..cd1296a1cf 100644--- a/crates/pa-tui/src/view/lazy.rs+++ b/crates/pa-tui/src/view/lazy.rs@@ -228,6 +228,13 @@ impl AgentView {.then(|| (section - 1, row))}+ /// Whether the last frame's top sat at or above the first entry's first row: scrolling up+ /// reveals no resident entry.+ pub(crate) fn viewport_at_top(&self) -> bool {+ self.sparse_window+ .is_some_and(|window| matches!(window.cursor, Some((0, _) | (1, 0))))+ }+/// Pause the viewport with row `row` of entry `index` at its top.pub(crate) fn scroll_to_entry(&mut self, index: usize, row: usize) {if self.layout_width == 0 {