codex icon indicating copy to clipboard operation
codex copied to clipboard

Fullscreen transcript: high-rate mouse-wheel input causes delayed scrolling and stopping

Open JohannesGESchmidt opened this issue 2 months ago • 1 comments

Two changes reduce fullscreen wheel-input backlog: skip the per-wheel pre-render, then drain ready wheel events before drawing, with a fairness bound. On Windows, a local optimized build reduced 401-event burst stopping latency from 734–791 ms to 110–134 ms, with correct final positions.

1. Skip the pre-render for wheel events

codex-rs/tui/src/app/owned_transcript.rs:284 — the TuiEvent::Mouse branch currently renders before every event except Moved.

Extend that exclusion to ScrollUp and ScrollDown. Keep processing each wheel event; let the existing scheduled draw render the accumulated viewport position. Other mouse events retain their pre-render.

if !matches!(
    mouse.kind,
    MouseEventKind::Moved | MouseEventKind::ScrollUp | MouseEventKind::ScrollDown
) {
    let size = tui.prepare_draw_size()?;
    self.render_owned_transcript(tui, size)?;
}

2. Drain ready wheel input before drawing

codex-rs/tui/src/tui/event_stream.rs:312 — change TuiEventStream::poll_next from unconditional round-robin preference:

  • Add wheel_events_since_draw: usize, initialized to 0.
  • After a wheel event, prefer ready input. At 64 consecutive wheel events, prefer a ready draw. If the preferred source is pending, immediately poll the other source.
  • Increment the counter for each wheel event; reset it on other events. After Draw, set poll_draw_first = false so input is preferred next.
  • Preserve every event and its order. No debounce timer, dropped ticks, or merging of opposite directions. The bound prevents sustained wheel input from starving ready frames.

The exact locally tested implementation diff is below; the snippets and links target 0.157.1 / 36650394c5b38c2990ccf2a3457165ca3e9d9726.

Exact implementation diff (two files)
diff --git a/codex-rs/tui/src/app/owned_transcript.rs b/codex-rs/tui/src/app/owned_transcript.rs
index 18ba695600..8089ac010d 100644
--- a/codex-rs/tui/src/app/owned_transcript.rs
+++ b/codex-rs/tui/src/app/owned_transcript.rs
@@ -284,7 +284,15 @@ impl App {
         }
         if let TuiEvent::Mouse(mouse) = event {
             let composer_ready = self.chat_widget.prepare_composer_mouse(*mouse);
-            if mouse.kind != crossterm::event::MouseEventKind::Moved {
+            // Wheel bursts only update the viewport position. Drawing before every
+            // tick lets high-rate wheels queue expensive, already obsolete frames.
+            // The scheduled draw below presents the accumulated scroll position.
+            if !matches!(
+                mouse.kind,
+                crossterm::event::MouseEventKind::Moved
+                    | crossterm::event::MouseEventKind::ScrollUp
+                    | crossterm::event::MouseEventKind::ScrollDown
+            ) {
                 let size = tui.prepare_draw_size()?;
                 self.render_owned_transcript(tui, size)?;
             }
diff --git a/codex-rs/tui/src/tui/event_stream.rs b/codex-rs/tui/src/tui/event_stream.rs
index 35afa4bfbb..6c994748ff 100644
--- a/codex-rs/tui/src/tui/event_stream.rs
+++ b/codex-rs/tui/src/tui/event_stream.rs
@@ -166,6 +166,7 @@ pub struct TuiEventStream<S: EventSource + Default + Unpin = CrosstermEventSourc
     resume_stream: WatchStream<()>,
     terminal_focused: Arc<AtomicBool>,
     poll_draw_first: bool,
+    wheel_events_since_draw: usize,
     #[cfg(unix)]
     suspend_context: crate::tui::job_control::SuspendContext,
     #[cfg(unix)]
@@ -187,6 +188,7 @@ impl<S: EventSource + Default + Unpin> TuiEventStream<S> {
             resume_stream,
             terminal_focused,
             poll_draw_first: false,
+            wheel_events_since_draw: 0,
             #[cfg(unix)]
             suspend_context,
             #[cfg(unix)]
@@ -312,27 +314,44 @@ impl<S: EventSource + Default + Unpin> Stream for TuiEventStream<S> {
     type Item = TuiEvent;
 
     fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
-        // approximate fairness + no starvation via round-robin.
-        let draw_first = self.poll_draw_first;
+        // Drain ready wheel ticks before drawing intermediate scroll positions,
+        // with a bound that keeps continuous input from starving display frames.
+        const MAX_WHEEL_EVENTS_PER_FRAME: usize = 64;
+        let draw_first = if self.wheel_events_since_draw >= MAX_WHEEL_EVENTS_PER_FRAME {
+            true
+        } else if self.wheel_events_since_draw > 0 {
+            false
+        } else {
+            self.poll_draw_first
+        };
         self.poll_draw_first = !self.poll_draw_first;
 
-        if draw_first {
-            if let Poll::Ready(event) = self.poll_draw_event(cx) {
-                return Poll::Ready(event);
-            }
-            if let Poll::Ready(event) = self.poll_crossterm_event(cx) {
-                return Poll::Ready(event);
+        let result = if draw_first {
+            match self.poll_draw_event(cx) {
+                Poll::Pending => self.poll_crossterm_event(cx),
+                ready => ready,
             }
         } else {
-            if let Poll::Ready(event) = self.poll_crossterm_event(cx) {
-                return Poll::Ready(event);
+            match self.poll_crossterm_event(cx) {
+                Poll::Pending => self.poll_draw_event(cx),
+                ready => ready,
             }
-            if let Poll::Ready(event) = self.poll_draw_event(cx) {
-                return Poll::Ready(event);
+        };
+        if let Poll::Ready(Some(event)) = &result {
+            if matches!(event, TuiEvent::Mouse(mouse) if matches!(
+                mouse.kind,
+                crossterm::event::MouseEventKind::ScrollUp
+                    | crossterm::event::MouseEventKind::ScrollDown
+            )) {
+                self.wheel_events_since_draw = self.wheel_events_since_draw.saturating_add(1);
+            } else {
+                self.wheel_events_since_draw = 0;
+                if matches!(event, TuiEvent::Draw) {
+                    self.poll_draw_first = false;
+                }
             }
         }
-
-        Poll::Pending
+        result
     }
 }

Reproduce / evidence

Windows x64 build 26200; Codex CLI 0.157.1; tlbx 10.17.0 (browser/xterm.js terminal). Set [tui] fullscreen_transcript = true, resume a long conversation, spin a Logitech free-spin wheel over the transcript, then stop or reverse. The stock build visibly starts late and continues scrolling after input stops. Expected: prompt movement and stopping while preserving scroll distance.

Stock npm executable versus local cargo build --release -p codex-cli, same release tag and terminal setup; no terminal changes. Offline fixture: 40 responses × 300 numbered lines. Synthetic wheel input travels through xterm mouse reporting. Timing is from the last emitted mouse input to the final visible transcript-buffer change:

401-event burst, three runs Stock Patched
Reverse every 10 events 734 / 753 / 791 ms 116 / 112 / 114 ms
Same direction 784 / 745 / 756 ms 110 / 134 / 126 ms

All final-position checks passed. Paced-input tests also passed. A manual Logitech free-spin test confirmed a substantial improvement after restarting into the patched build.

Validation limits: 13 focused tests passed, including ready-input ordering, yielding a frame after 64 ticks, immediate short-tail drawing, and an accumulated-viewport snapshot. An earlier broad TUI run had 78 failures and 5 timeouts; no unchanged baseline was established, so the full suite is not claimed green. Linux/macOS are untested. The timing comparison uses stock release versus a local optimized build, not two identically built binaries.

JohannesGESchmidt avatar Sep 26 '26 11:09 JohannesGESchmidt

(response from codex)

Thanks for the detailed reproduction and patch. Codex CLI 0.171.0 shipped a fix for the per-tick redraw you identified. When the transcript layout is current, Codex applies each wheel tick in order and draws the accumulated movement on the next scheduled frame.

I replayed six 401-tick bursts per version on macOS with a 12,000-line transcript. The official 0.157.1 build emitted 411–412 frames per burst. The official 0.172.0 build emitted just 2, without the proposed 64-tick rule. Both versions reached the same final rows, including when the wheel reversed every 10 ticks.

I’m closing this as fixed, though I couldn’t verify the visible delay in Windows/tlbx. If you still see lag after upgrading to 0.171.0 or later and restarting Codex, please reply with your CLI version and whether scrolling starts late, continues after you stop, or lags when you reverse.

etraut-openai avatar Nov 25 '26 23:11 etraut-openai