szn

Bug 347 — reflowCursorInternal trims trailing empty screen lines during copy-mode entry, shifting visible grid down and corrupting prompt

347. Grid reflow trims trailing empty screen lines when scrollback history exists

File: src/grid.zig:754-764
Severity: HIGH
Status: ✅ FIXED

Problem

Running interactive applications (such as Antigravity CLI) inside szn, entering copy-mode, scrolling up, and then exiting copy-mode resulted in rendering glitches near the bottom of the screen where the input prompt was shifted down or layout corrupted.

Root Cause

  1. Line Shift: When entering copy-mode (enterCopyMode), pane.forceReflow() invokes reflowCursorInternal at the current window width (new_width == self.width). reflowCursorInternal calculated process_limit by trimming trailing empty rows from the bottom of self.lines (the visible screen). When scrollback history was present (hist_len > 0), dropping trailing empty screen rows reduced the total line count to $H + V - E$. During layout reconstruction, self.lines stole $E$ lines from self.history to fill the top of the visible screen, shifting all visible screen content down by $E$ rows.
  2. Cursor Reset to $L-1$: In rewrap (src/grid.zig), when cursor_offset (co) was at or beyond the last character cell of a line (co >= cells.len), or on an empty prompt line, the cursor boundary condition (co == i and i == cells.len) failed for co > cells.len or co > 0 on empty lines. As a result, rewrap never set out_cursor_pos, causing new_cursor_logical_line to default to 0 (line $L-1$). When exiting copy-mode, the cursor was moved to $L-1$ instead of $L$, causing double-line ghosting.

Fix

  1. In src/grid.zig, updated reflowCursorInternal so that when reflowing at the same width with history present (new_width == self.width and hist_len > 0), process_limit is set to total.
  2. In src/grid.zig, updated rewrap so that empty lines (cells.len == 0) and trailing cursor offsets (co >= cells.len) correctly record out_cursor_pos clamped to new_width - 1, keeping the cursor on line $L$ at column cursor_x.