Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

fix(tui): the scroll gutter must not cost the painter its scroll fast path

Rach Pradhanf3678da1de62
diff --git a/TUI/app.zig b/TUI/app.zig
index a5590f8e..921a1e51 100644
--- a/TUI/app.zig
+++ b/TUI/app.zig
@@ -136,6 +136,11 @@ pub const Model = struct {
/// viewport this many milliseconds later (scrollbar.zig); zero means the
/// session has never scrolled, so no gutter has ever been earned.
scroll_seen_ms: u64 = 0,
+ /// Did the frame just composed carry the scroll gutter? Two consecutive
+ /// frames that both did are shaped the same way, which is what lets the
+ /// painter scroll the band and patch the gutter cell (scrollpaint.zig)
+ /// instead of refusing the fast path outright.
+ gutter_on: bool = false,
/// A one-shot byte string the run loop owes the TERMINAL, not the screen:
/// the OSC 52 clipboard write a copy just produced. Deliberately not part
/// of the frame — a frame is re-painted (the self-heal rewrites it every
diff --git a/TUI/render.zig b/TUI/render.zig
index c1db0a93..d631fd3f 100644
--- a/TUI/render.zig
+++ b/TUI/render.zig
@@ -190,7 +190,6 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,
if (image_card[image_card.len - 1] != '\n') try out.append('\n');
}
try out.appendSlice(bottom_block);
- self.paint_hint = scrollHint(self, prev_band);
// Selection is a post-pass over the finished frame: the row builders stay
// unaware of it, and the band lands on screen rows exactly as the mouse
// reported them (#529).
@@ -198,7 +197,13 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,
// The scroll gutter goes LAST, after the capture: the thumb is chrome the
// selection must never copy, and it hides for the duration of a drag
// anyway (scrollbar.zig owns both halves of that rule).
- return gpa.dupe(u8, try scrollbar.paint(self, a, banded, width));
+ const had_gutter = self.gutter_on;
+ const painted = try scrollbar.paint(self, a, banded, width);
+ // The hint describes the FINISHED frames, so it is taken after both
+ // post-passes: the gutter reshapes every band row, and whether the painter
+ // may scroll them depends on the two frames having the same shape.
+ self.paint_hint = scrollHint(self, prev_band, had_gutter and self.gutter_on);
+ return gpa.dupe(u8, painted);
}
/// `count` mid rows starting at `from`, taken from the layout cache when the
@@ -214,7 +219,7 @@ fn midSlice(a: std.mem.Allocator, cache: ?*layout_cache.Cache, lines: [][]const
/// The delta the scrollable band moved by between the last frame and this one,
/// when that is the ONLY thing that happened to it — the painter's scroll fast
/// path. Null is always safe: it just means the ordinary row diff.
-fn scrollHint(self: *const Model, prev: app.Band) ?scrollpaint.Hint {
+fn scrollHint(self: *const Model, prev: app.Band, gutter: bool) ?scrollpaint.Hint {
const cur = self.band;
if (!cur.live or !prev.live) return null;
// Geometry moved, so screen row N is not the same slot it was: a resize, an
@@ -228,7 +233,7 @@ fn scrollHint(self: *const Model, prev: app.Band) ?scrollpaint.Hint {
if (self.sel.active or self.sel.pressed) return null;
const d = @as(isize, @intCast(cur.off)) - @as(isize, @intCast(prev.off));
if (@abs(d) >= cur.len) return null;
- return .{ .top = cur.top, .len = cur.len, .delta = d };
+ return .{ .top = cur.top, .len = cur.len, .delta = d, .gutter = gutter };
}
fn countLines(s: []const u8) usize {
diff --git a/TUI/scrollbar.zig b/TUI/scrollbar.zig
index 17db6a83..cf169bbe 100644
--- a/TUI/scrollbar.zig
+++ b/TUI/scrollbar.zig
@@ -88,9 +88,24 @@ pub fn current(self: *const Model) ?Thumb {
/// stay unaware of the gutter, and the selection capture never sees the glyph.
/// Returns `frame` untouched whenever the thumb is hidden, which is what makes
/// hiding self-healing rather than a second code path.
+///
+/// While it IS showing, EVERY band row gets the gutter shape — content cut to
+/// `width - 1`, padded to it, then the cell — and rows off the thumb carry a
+/// space there. That uniformity is not cosmetic: it is what lets the painter's
+/// scroll fast path keep working. A hardware scroll slides the whole row, but
+/// the gutter's position comes from the scroll OFFSET and does not travel with
+/// the lines under it; with every band row the same shape, scrollpaint.zig can
+/// compare the content halves (which do slide) and patch the one cell that
+/// does not. Reshape only the thumb rows and every notch would look like a
+/// band whose rows changed for their own reasons, and the fast path would be
+/// refused on exactly the frames it exists for.
pub fn paint(self: *Model, a: std.mem.Allocator, frame: []const u8, width: usize) ![]const u8 {
- if (width == 0) return frame;
- const t = current(self) orelse return frame;
+ if (width < 2) return frame;
+ const t = current(self) orelse {
+ self.gutter_on = false;
+ return frame;
+ };
+ self.gutter_on = true;
const first = self.band.top + t.top;
const last = first + t.len; // exclusive
var out = std.array_list.Managed(u8).init(a);
@@ -99,20 +114,20 @@ pub fn paint(self: *Model, a: std.mem.Allocator, frame: []const u8, width: usize
const style = self.theme().muted;
while (it.next()) |ln| : (row += 1) {
if (row > 0) try out.append('\n');
- if (row < first or row >= last) {
+ if (row < self.band.top or row >= self.band.top + self.band.len) {
try out.appendSlice(ln);
continue;
}
- try overlayLast(&out, ln, width, style);
+ try gutterRow(&out, ln, width, style, row >= first and row < last);
}
return out.items;
}
-/// One row with its last column replaced by the thumb. The body is cut to
-/// `width - 1` columns and padded out to them, so the glyph always lands on the
-/// last cell whatever the row measured — and the row ends on a reset, so the
-/// gutter's colour cannot leak into the next row a diff paint touches.
-fn overlayLast(out: *std.array_list.Managed(u8), ln: []const u8, width: usize, style: []const u8) !void {
+/// One band row in the gutter shape: `content ++ pad ++ reset ++ style ++ cell`.
+/// The reset is the SPLIT MARKER the painter looks for (scrollpaint.zig takes
+/// everything from the last one as the cell), and it also stops the gutter's
+/// colour leaking into the next row a diff paint touches.
+fn gutterRow(out: *std.array_list.Managed(u8), ln: []const u8, width: usize, style: []const u8, thumb_here: bool) !void {
const keep = width - 1;
const body = theme_mod.takeCols(ln, keep);
try out.appendSlice(body);
@@ -120,8 +135,7 @@ fn overlayLast(out: *std.array_list.Managed(u8), ln: []const u8, width: usize, s
while (pad > 0) : (pad -= 1) try out.append(' ');
try out.appendSlice(theme_mod.reset);
try out.appendSlice(style);
- try out.appendSlice(glyphs.scroll_thumb);
- try out.appendSlice(theme_mod.reset);
+ try out.appendSlice(if (thumb_here) glyphs.scroll_thumb else " ");
}
// ------------------------------------------------------------------ tests
@@ -265,7 +279,7 @@ test "the overlay lands on the last column of exactly the thumb's rows" {
if (!marked) continue;
// Exactly `width` columns, with the glyph on the last one.
try testing.expectEqual(width, theme_mod.visibleLen(ln));
- try testing.expect(std.mem.endsWith(u8, ln, glyphs.scroll_thumb ++ theme_mod.reset));
+ try testing.expect(std.mem.endsWith(u8, ln, glyphs.scroll_thumb));
}
try testing.expectEqual(@as(usize, 16), row);
}
@@ -307,6 +321,6 @@ test "a row already at full width keeps its width when the thumb lands on it" {
for ([_][]const u8{ "0123456789", "ab", "12345678\u{1F409}" }) |body| {
const out = try paint(&m, ar, body, 10);
try testing.expectEqual(@as(usize, 10), theme_mod.visibleLen(out));
- try testing.expect(std.mem.endsWith(u8, out, glyphs.scroll_thumb ++ theme_mod.reset));
+ try testing.expect(std.mem.endsWith(u8, out, glyphs.scroll_thumb));
}
}
diff --git a/TUI/scrollpaint.zig b/TUI/scrollpaint.zig
index 9dc74092..a6bd563f 100644
--- a/TUI/scrollpaint.zig
+++ b/TUI/scrollpaint.zig
@@ -27,6 +27,7 @@ const std = @import("std");
const Io = std.Io;
const paint = @import("paint.zig");
+const theme_mod = @import("theme.zig");
/// What render.zig believes the last two frames did to the scrollable band.
/// `delta` counts CONTENT lines: positive means the viewport moved further
@@ -37,6 +38,16 @@ pub const Hint = struct {
top: usize,
len: usize,
delta: isize,
+ /// Both frames carry the scroll gutter (scrollbar.zig), so every band row
+ /// on both is shaped `content ++ reset ++ style ++ cell`. The content half
+ /// slides with the scroll; the gutter cell does NOT, because its position
+ /// comes from the scroll offset rather than from the lines under it. So
+ /// the verification compares content halves and the paint patches the one
+ /// cell per row, on the two or three rows the thumb actually moved across.
+ /// False means ordinary whole-row comparison — including the frame the
+ /// gutter appears on and the frame it leaves, where the shapes differ and
+ /// the caller correctly falls back to a full diff.
+ gutter: bool = false,
};
/// Terminals taller than this fall back to the diff path rather than grow the
@@ -83,6 +94,22 @@ fn viable(rows: usize, h: Hint) bool {
return @abs(h.delta) < h.len;
}
+/// A gutter row splits at its LAST reset: everything before is the content the
+/// scroll carries, everything from it (`reset ++ style ++ cell`) is the one
+/// column that does not. scrollbar.zig appends that suffix after every band
+/// row, so the last reset is always the split — and the suffix is
+/// self-contained, which is what lets the patch below write it straight onto
+/// the last cell.
+fn content(row: []const u8) []const u8 {
+ const at = std.mem.lastIndexOf(u8, row, theme_mod.reset) orelse return row;
+ return row[0..at];
+}
+
+fn cell(row: []const u8) []const u8 {
+ const at = std.mem.lastIndexOf(u8, row, theme_mod.reset) orelse return "";
+ return row[at..];
+}
+
/// Decide, writing nothing. Returns the exposed-row plan when a hardware
/// scroll is provably equivalent to repainting the band, and null otherwise —
/// a null here is never a correctness risk, only a missed optimisation.
@@ -95,7 +122,11 @@ fn plan(rows: usize, h: Hint, cur: *const Lines, old: *const Lines) ?Plan {
while (j < kept) : (j += 1) {
const dst = if (up) j else j + mag;
const src = if (up) j + mag else j;
- if (!std.mem.eql(u8, cur.at(h.top + dst), old.at(h.top + src))) return null;
+ const want = cur.at(h.top + dst);
+ const have = old.at(h.top + src);
+ if (h.gutter) {
+ if (!std.mem.eql(u8, content(want), content(have))) return null;
+ } else if (!std.mem.eql(u8, want, have)) return null;
}
return .{
.first_exposed = h.top + (if (up) kept else 0),
@@ -141,6 +172,25 @@ pub fn tryPaint(
try w.print("\x1b[{d};1H", .{r + 1});
try paint.paintRow(w, cur.at(r), cols, bg);
}
+ // The gutter rode the scroll along with the content, but its position
+ // comes from the scroll OFFSET, so on the rows the thumb moved across the
+ // terminal is now showing the wrong cell. Patch that one column, and only
+ // where it actually differs — a notch moves the thumb by about a row, so
+ // this is two cells, not a band. (Exposed rows were just written whole.)
+ if (h.gutter) {
+ const kept = h.len - p.count;
+ var j: usize = 0;
+ while (j < kept) : (j += 1) {
+ const dst = if (p.up) j else j + p.count;
+ const src = if (p.up) j + p.count else j;
+ const want = cell(cur.at(h.top + dst));
+ if (std.mem.eql(u8, want, cell(old.at(h.top + src)))) continue;
+ try w.print("\x1b[{d};{d}H", .{ h.top + dst + 1, cols });
+ try w.writeAll(paint.row_prologue);
+ try w.writeAll(bg);
+ try w.writeAll(want);
+ }
+ }
// The band moved; nothing else did. Chrome that changed for its own
// reasons — the sticky header now pinning a different prompt, a scroll
// indicator, the composer — is still an ordinary row diff.
@@ -310,6 +360,85 @@ test "a selection band dropped by the same wheel notch blocks the scroll" {
try std.testing.expect((try emit(ar, after, 24, 80, banded.items, h)) == null);
}
+/// The same frame with every band row in the gutter shape scrollbar.zig gives
+/// it: content, then `reset ++ style ++ cell`, with the thumb on `thumb_len`
+/// rows starting `thumb_top` into the band.
+fn withGutter(a: std.mem.Allocator, frame: []const u8, band_top: usize, band_len: usize, thumb_top: usize, thumb_len: usize, cols: usize) ![]const u8 {
+ var out = std.array_list.Managed(u8).init(a);
+ var r: usize = 0;
+ var it = std.mem.splitScalar(u8, frame, '\n');
+ while (it.next()) |ln| : (r += 1) {
+ if (r > 0) try out.append('\n');
+ if (r < band_top or r >= band_top + band_len) {
+ try out.appendSlice(ln);
+ continue;
+ }
+ const body = theme_mod.takeCols(ln, cols - 1);
+ try out.appendSlice(body);
+ var pad = cols - 1 - theme_mod.visibleLen(body);
+ while (pad > 0) : (pad -= 1) try out.append(' ');
+ try out.appendSlice(theme_mod.reset);
+ try out.appendSlice("\x1b[38;2;90;90;90m");
+ const on = r >= band_top + thumb_top and r < band_top + thumb_top + thumb_len;
+ try out.appendSlice(if (on) "\u{275A}" else " ");
+ }
+ return out.items;
+}
+
+test "the scroll gutter rides the fast path and is patched a cell at a time" {
+ const a = std.testing.allocator;
+ var arena = std.heap.ArenaAllocator.init(a);
+ defer arena.deinit();
+ const ar = arena.allocator();
+ const rows: usize = 24;
+ const cols: usize = 80;
+ const top: usize = 3;
+ const len: usize = 18;
+ // A notch: the content slid one line AND the thumb moved one row down.
+ const before = try withGutter(ar, try frameAt(ar, 100, rows, top, len, "same"), top, len, 4, 6, cols);
+ const after = try withGutter(ar, try frameAt(ar, 101, rows, top, len, "same"), top, len, 5, 6, cols);
+ const h: Hint = .{ .top = top, .len = len, .delta = 1, .gutter = true };
+ // Without the gutter flag the frames look like a band that changed for its
+ // own reasons, and the fast path is refused — that is the regression this
+ // pins, and it is why the flag exists.
+ try std.testing.expect((try emit(ar, after, rows, cols, before, .{ .top = top, .len = len, .delta = 1 })) == null);
+ const out = (try emit(ar, after, rows, cols, before, h)).?;
+ try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, out, "\x1b[1S"));
+ // A handful of cells, not a band. The thumb is a RANGE, and the content
+ // slid up by one while the thumb moved down by one, so it is displaced by
+ // two rows relative to what the terminal just carried: the two rows at
+ // each end. Each is addressed at the LAST column, never at column 1.
+ try std.testing.expectEqual(@as(usize, 4), std.mem.count(u8, out, ";80H"));
+ // One whole row: the one the scroll exposed at the bottom of the band.
+ try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, out, ";1H"));
+ // Cheap: a gutter notch must stay in the same class as a plain one.
+ var aw = Io.Writer.Allocating.init(ar);
+ try paint.paint(&aw.writer, after, rows, cols, before, test_bg, true, null);
+ try std.testing.expect(out.len * 4 < aw.written().len);
+}
+
+test "a gutter frame whose content did NOT slide is still refused" {
+ const a = std.testing.allocator;
+ var arena = std.heap.ArenaAllocator.init(a);
+ defer arena.deinit();
+ const ar = arena.allocator();
+ const top: usize = 3;
+ const len: usize = 18;
+ const before = try withGutter(ar, try frameAt(ar, 100, 24, top, len, "same"), top, len, 4, 6, 80);
+ // Same offset the hint claims, but a row inside the band changed: the
+ // content comparison has to catch it even though the gutter halves agree.
+ const raw = try frameAt(ar, 101, 24, top, len, "same");
+ var folded = std.array_list.Managed(u8).init(ar);
+ var it = std.mem.splitScalar(u8, raw, '\n');
+ var r: usize = 0;
+ while (it.next()) |ln| : (r += 1) {
+ if (r > 0) try folded.append('\n');
+ try folded.appendSlice(if (r == 9) "the tool run just expanded" else ln);
+ }
+ const after = try withGutter(ar, folded.items, top, len, 5, 6, 80);
+ try std.testing.expect((try emit(ar, after, 24, 80, before, .{ .top = top, .len = len, .delta = 1, .gutter = true })) == null);
+}
+
/// A terminal that also knows DECSTBM / SU / SD — the two sequences the diff
/// painter's own screen model never had to understand.
const Screen = struct {