szn

Bug 443 — Raw history index arithmetic in render.zig and mode_copy.zig accesses out of bounds or displays stale lines when history ring wraps

443. Scrollback buffer shows stale output or fails to scroll after history ring wraps

Severity: HIGH
Status: Fixed

Symptoms

When running commands that output text (e.g. ls, followed by switching windows, then running a program with long output like -h), scrolling up in the terminal scrollback buffer either:

  1. Showed older text (like the previous ls output) rather than the most recently scrolled-off text (the top part of -h), cutting off right at the top row of the screen.
  2. Failed to scroll up at all.

Root Cause Analysis

  1. Raw index arithmetic without modulo in render.zig: In src/server/render.zig:renderAll() and renderScreen(), cells from the scrollback history were accessed with:
    &pane_grid.history.items[pane_grid.history_start + @as(usize, @intCast(combined_idx))].cells
    

    When the history ring buffer filled up and wrapped around (history_start > 0):

    • Logical lines $0 \dots \text{hist_len}-1$ (where $0$ is the oldest and $\text{hist_len}-1$ is the newest line) are stored at physical indices $(\text{history_start} + i) \pmod{\text{items.len}}$.
    • For recent history lines (near the top of the visible screen, where combined_idx is close to hist_len), history_start + combined_idx exceeded pane_grid.history.items.len, resulting in out-of-bounds slice access.
    • For older history lines where history_start + combined_idx < items.len, it read stale entries from the unrotated beginning of the ring buffer instead of the actual logical lines, displaying previous commands (like ls) instead of recent output.
  2. Direct un-modulo indexing in mode_copy.zig: src/mode_copy.zig in getCellAtOffset and isLineWrapped also performed direct indexing:
    const line = &grid.history.items[grid.history_start + combined_idx];
    

    instead of calling grid.getHistoryLine(combined_idx). Additionally, visible lines used grid.lines.items[visible_y] instead of grid.getLine(visible_y), ignoring grid.start_index.

  3. Inconsistent modulo in grid.zig: clearHistory, setHistoryLimit, scrollUp, and scrollDown in src/grid.zig used % self.history_limit rather than % self.history.items.len. When history.items.len < history_limit and history_start > 0, ring index calculations produced indices larger than history.items.len.

  4. Missing history-limit inheritance in Session.newWindow: src/session.zig:newWindow() did not propagate the session’s history-limit option to newly spawned window panes, unlike Session.init.

Solution

  1. In src/server/render.zig, updated renderAll and renderScreen to use pane_grid.getHistoryLine(@as(usize, @intCast(combined_idx))) and grid.getLine(@intCast(visible_y)) consistently.
  2. In src/mode_copy.zig, updated getCellAtOffset and isLineWrapped to use grid.getHistoryLine(combined_idx) and grid.getLine(@intCast(visible_y)).
  3. In src/grid.zig, standardized all circular deque indexing to % self.history.items.len and protected scrollUp/scrollDown and reflowCursorInternal error rollback against ring state corruption.
  4. In src/session.zig, added history-limit propagation to Session.newWindow.