graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
feat(tui): inset the pager like grok-build
Cursor Agent60848b65cf79
diff --git a/TUI/chrome.zig b/TUI/chrome.zigindex c2186f93..c2441dfd 100644--- a/TUI/chrome.zig+++ b/TUI/chrome.zig@@ -233,7 +233,7 @@ pub fn slashWindow(self: *const Model) ?SlashGeom {const sel = @min(self.slash_sel, n - 1);// A panel too narrow to frame is returned as bare rows (panel.min_width),// and then the first item IS the first line.- const framed = self.last_term_width >= panel.min_width;+ const framed = @import("inset.zig").wrapWidth(self) >= panel.min_width;const edges: usize = if (framed) 2 else 0;return .{.first = if (sel >= show) sel + 1 - show else 0,diff --git a/TUI/hover.zig b/TUI/hover.zigindex 8d36b9c6..5274d805 100644--- a/TUI/hover.zig+++ b/TUI/hover.zig@@ -112,7 +112,7 @@ pub fn hitTest(m: *Model, y: usize) Hit {return if (m.sticky_rows > 0 and y == m.mid_origin) .{ .target = .sticky } else .{};}const vis = y - m.mid_origin + m.mid_skip;- const i = layout_cache.indexAtVisual(m, vis, m.last_term_width) orelse return .{};+ const i = layout_cache.indexAtVisual(m, vis, @import("inset.zig").wrapWidth(m)) orelse return .{};if (m.history.items[i].kind != .tool) return .{};const run = m.toolRun(i);return .{ .target = .tool, .idx = i, .collapsed = m.history.items[run.start].folded };@@ -184,7 +184,7 @@ pub fn dragged(m: *Model) void {/// top row of the viewport. No-op when there is nothing pinned or nothing to/// scroll — the same numbers render.zig used to lay the frame out.pub fn jumpToSticky(m: *Model) void {- const c = layout_cache.ensure(m, m.last_term_width);+ const c = layout_cache.ensure(m, @import("inset.zig").wrapWidth(m));if (layout_cache.stickyUserAbove(c, m.mid_skip) == null) return;var i = layout_cache.indexAt(c, m.mid_skip) orelse m.history.items.len;const line = while (i > 0) {diff --git a/TUI/image.zig b/TUI/image.zigindex 8b0c776d..71bad50b 100644--- a/TUI/image.zig+++ b/TUI/image.zig@@ -209,7 +209,7 @@ pub fn mouse(self: *Model, ev: key_mod.Mouse) bool {}return false;};- const hist = if (y >= self.prompt_origin or y < self.mid_origin + self.sticky_rows) null else @import("layout_cache.zig").indexAtVisual(self, y -| self.mid_origin + self.mid_skip, self.last_term_width);+ const hist = if (y >= self.prompt_origin or y < self.mid_origin + self.sticky_rows) null else @import("layout_cache.zig").indexAtVisual(self, y -| self.mid_origin + self.mid_skip, @import("inset.zig").wrapWidth(self));const path = pathForChip(self, n, hist);show(self, path, n, click);return true;@@ -217,7 +217,7 @@ pub fn mouse(self: *Model, ev: key_mod.Mouse) bool {fn chipAt(self: *Model, x: usize, y: usize) ?u32 {if (self.images.items.len > 0 and y == self.prompt_origin + 1) {- const box_pad: usize = 4;+ const box_pad: usize = 4 + @import("inset.zig").of(self).left;if (x < box_pad) return null;var buf: [128]u8 = undefined;var n: usize = 0;@@ -232,10 +232,10 @@ fn chipAt(self: *Model, x: usize, y: usize) ?u32 {if (y < self.mid_origin) return null;if (y < self.mid_origin + self.sticky_rows) return null; // sticky chrome is inertconst vis = y - self.mid_origin + self.mid_skip;- const idx = @import("layout_cache.zig").indexAtVisual(self, vis, self.last_term_width) orelse return null;+ const idx = @import("layout_cache.zig").indexAtVisual(self, vis, @import("inset.zig").wrapWidth(self)) orelse return null;if (self.history.items[idx].kind != .user) return null;if (pathInUserText(self.history.items[idx].text, 1) == null) return null;- return parseChipOnUser(self, idx, x);+ return parseChipOnUser(self, idx, @import("inset.zig").screenToInner(self, x));}fn parseChipOnUser(self: *const Model, idx: usize, x: usize) ?u32 {diff --git a/TUI/inset.zig b/TUI/inset.zignew file mode 100644index 00000000..b341b690--- /dev/null+++ b/TUI/inset.zig@@ -0,0 +1,213 @@+//! grok-build's default pager inset (xai-org/grok-build `LayoutConfig`).+//!+//! Measured from `crates/codegen/xai-grok-pager-render/src/appearance/config.rs`+//! and the pager.toml docs (`[scrollback.layout]`):+//!+//! outer_vpad = 1 // top/bottom of the scrollback viewport+//! outer_hpad_left = 2 // left gutter (minimum 1)+//! outer_hpad_right = 2 // right gutter (minimum 1)+//! block_pad_left = 2 // inside a block, after the accent rail+//! block_pad_right = 2+//!+//! Compact mode (their `eff_*`): horizontal pad drops to `MIN_HPAD` (1) and+//! vertical pad drops to 0. `/compact-mode` is the same switch here.+//!+//! Presentation only — colours stay in `theme.zig` / `ansi.zig`. No OSC 50.++const std = @import("std");++const app = @import("app.zig");+const Model = app.Model;++/// grok-build `LayoutConfig::outer_hpad_left`.+pub const outer_hpad_left: usize = 2;+/// grok-build `LayoutConfig::outer_hpad_right`.+pub const outer_hpad_right: usize = 2;+/// grok-build `LayoutConfig::outer_vpad`.+pub const outer_vpad: usize = 1;+/// grok-build `LayoutConfig::MIN_HPAD` — compact mode, and the clamp floor.+pub const min_hpad: usize = 1;++pub const Pads = struct {+ left: usize,+ right: usize,+ vpad: usize,+ /// Columns the chrome and transcript wrap to (`term - left - right`).+ inner: usize,+};++pub fn hpadLeft(compact: bool) usize {+ return if (compact) min_hpad else outer_hpad_left;+}++pub fn hpadRight(compact: bool) usize {+ return if (compact) min_hpad else outer_hpad_right;+}++pub fn vpad(compact: bool) usize {+ return if (compact) 0 else outer_vpad;+}++/// Effective pads for a terminal `width`. Narrow screens drop to `min_hpad`+/// rather than crush the composer below the panel minimum.+pub fn forTerm(width: usize, compact: bool) Pads {+ var left = hpadLeft(compact);+ var right = hpadRight(compact);+ const floor: usize = 24;+ if (width < left + right + floor) {+ left = @min(left, min_hpad);+ right = @min(right, min_hpad);+ }+ if (width < left + right + 16) {+ left = 0;+ right = 0;+ }+ const inner = width -| (left + right);+ return .{+ .left = left,+ .right = right,+ .vpad = vpad(compact),+ .inner = if (inner == 0) width else inner,+ };+}++pub fn of(m: *const Model) Pads {+ return forTerm(if (m.last_term_width == 0) 80 else m.last_term_width, m.compact_mode);+}++/// Wrap / layout width — what `layout_cache.ensure` and `indexAtVisual` share.+pub fn wrapWidth(m: *const Model) usize {+ return of(m).inner;+}++/// Screen column → content column (drops the left gutter).+pub fn screenToInner(m: *const Model, x: usize) usize {+ return x -| of(m).left;+}++/// Prefix every row of `text` with `left` spaces. Trailing newline shape is+/// preserved so `render.zig`'s `countLines` stays honest.+pub fn appendPadded(out: *std.array_list.Managed(u8), text: []const u8, left: usize) !void {+ if (text.len == 0) return;+ var it = std.mem.splitScalar(u8, text, '\n');+ var first = true;+ while (it.next()) |ln| {+ if (!first) try out.append('\n');+ first = false;+ if (ln.len == 0 and it.rest().len == 0) break;+ try out.appendNTimes(' ', left);+ try out.appendSlice(ln);+ }+ if (text[text.len - 1] == '\n') try out.append('\n');+}++test "grok-build default inset is 2 / 2 / 1" {+ const p = forTerm(80, false);+ try std.testing.expectEqual(@as(usize, 2), p.left);+ try std.testing.expectEqual(@as(usize, 2), p.right);+ try std.testing.expectEqual(@as(usize, 1), p.vpad);+ try std.testing.expectEqual(@as(usize, 76), p.inner);+ try std.testing.expectEqual(outer_hpad_left, p.left);+ try std.testing.expectEqual(outer_vpad, p.vpad);+}++test "compact mode uses MIN_HPAD and drops outer_vpad" {+ const p = forTerm(80, true);+ try std.testing.expectEqual(min_hpad, p.left);+ try std.testing.expectEqual(min_hpad, p.right);+ try std.testing.expectEqual(@as(usize, 0), p.vpad);+ try std.testing.expectEqual(@as(usize, 78), p.inner);+}++test "appendPadded prefixes every row and keeps a trailing newline" {+ var out = std.array_list.Managed(u8).init(std.testing.allocator);+ defer out.deinit();+ try appendPadded(&out, "ab\ncd\n", 2);+ try std.testing.expectEqualStrings(" ab\n cd\n", out.items);+}++fn firstGlyphCol(line: []const u8) usize {+ var n: usize = 0;+ for (line) |c| {+ if (c != ' ') return n;+ n += 1;+ }+ return n;+}++fn rowWith(vis: []const u8, needle: []const u8) ?[]const u8 {+ var it = std.mem.splitScalar(u8, vis, '\n');+ while (it.next()) |ln| {+ if (std.mem.indexOf(u8, ln, needle) != null) return ln;+ }+ return null;+}++test "Term screen insets transcript, composer, and footer like grok-build" {+ const sim = @import("sim.zig");+ var term: sim.Term = undefined;+ term.init(std.testing.allocator, 80, 24);+ defer term.deinit();+ try term.model.push(.assistant, "INSETPROBE unique breathing room");+ const vis = try term.screen();+ defer std.testing.allocator.free(vis);+ const probe = rowWith(vis, "INSETPROBE") orelse return error.NoTranscript;+ // Outer gutter plus the unselected row mark: never flush to column 0.+ try std.testing.expect(firstGlyphCol(probe) >= outer_hpad_left);+ try std.testing.expect(probe.len >= 2 and probe[0] == ' ' and probe[1] == ' ');++ const box = rowWith(vis, "╭") orelse return error.NoComposer;+ try std.testing.expectEqual(outer_hpad_left, firstGlyphCol(box));+ try std.testing.expect(box[0] == ' ');++ const hints = rowWith(vis, "Enter:send") orelse return error.NoFooter;+ try std.testing.expect(firstGlyphCol(hints) >= outer_hpad_left);++ var rows = std.mem.splitScalar(u8, vis, '\n');+ var last: []const u8 = "";+ var n: usize = 0;+ while (rows.next()) |ln| {+ if (ln.len > 0 or rows.rest().len > 0) {+ last = ln;+ n += 1;+ }+ }+ // The last painted row is the outer_vpad blank under the hints — not the+ // composer wall, and not a flush hint line.+ try std.testing.expect(std.mem.indexOf(u8, last, "╭") == null);+ try std.testing.expect(std.mem.indexOf(u8, last, "╰") == null);+ try std.testing.expect(firstGlyphCol(last) == last.len);++ const lay = try term.layout();+ defer std.testing.allocator.free(lay);+ try std.testing.expect(std.mem.indexOf(u8, lay, "prompt-origin") != null);+ try std.testing.expect(term.model.prompt_origin + 1 < 24);+}++test "Term annotated rows carry the left gutter; compact drops vpad" {+ const sim = @import("sim.zig");+ var term: sim.Term = undefined;+ term.init(std.testing.allocator, 80, 24);+ defer term.deinit();+ try term.model.push(.user, "gutter-check prompt");+ const ann = try term.annotated();+ defer std.testing.allocator.free(ann);+ const line = rowWith(ann, "gutter-check") orelse return error.NoAnnotated;+ // " 12| › #1 gutter-check" — the dump prefix is 4 cols, then the gutter.+ const bar = std.mem.indexOfScalar(u8, line, '|') orelse return error.NoBar;+ try std.testing.expectEqual(@as(u8, ' '), line[bar + 1]);+ try std.testing.expectEqual(@as(u8, ' '), line[bar + 2]);++ term.model.compact_mode = true;+ const vis = try term.screen();+ defer std.testing.allocator.free(vis);+ const box = rowWith(vis, "╭") orelse return error.NoComposer;+ try std.testing.expectEqual(min_hpad, firstGlyphCol(box));+ var it = std.mem.splitScalar(u8, vis, '\n');+ var last: []const u8 = "";+ while (it.next()) |ln| {+ if (ln.len > 0 or it.rest().len > 0) last = ln;+ }+ // Compact drops the blank last row; the hint line is the footer.+ try std.testing.expect(std.mem.indexOf(u8, last, "Enter:send") != null);+}diff --git a/TUI/keys.zig b/TUI/keys.zigindex 25863aec..25640974 100644--- a/TUI/keys.zig+++ b/TUI/keys.zig@@ -313,7 +313,7 @@ fn mouseKey(self: *Model, ev: key_mod.Mouse) Effect {self.focus = .scrollback;// A click on blank padding maps to no entry — it must not select or// toggle anything (#519).- const i = layout_cache.indexAtVisual(self, vis, self.last_term_width) orelse return .stay;+ const i = layout_cache.indexAtVisual(self, vis, @import("inset.zig").wrapWidth(self)) orelse return .stay;self.selected = i;if (self.history.items[i].kind == .tool and !dbl) {// A double click is ONE net toggle of the whole group: the first pressdiff --git a/TUI/models.zig b/TUI/models.zigindex 692c63ea..fcddd7f3 100644--- a/TUI/models.zig+++ b/TUI/models.zig@@ -173,7 +173,7 @@ pub fn render(self: *const Model, a: std.mem.Allocator) ![]const u8 {const sel = self.overlay_sel % n;const vis = @min(visible_rows, n);const off = if (sel >= vis) sel - vis + 1 else 0;- const col = nameColumn(rows[off..@min(n, off + vis)], self.last_term_width);+ const col = nameColumn(rows[off..@min(n, off + vis)], @import("inset.zig").wrapWidth(self));var i = off;while (i < n and i < off + vis) : (i += 1) {const e = rows[i];diff --git a/TUI/nav.zig b/TUI/nav.zigindex d28300e2..c4854d73 100644--- a/TUI/nav.zig+++ b/TUI/nav.zig@@ -65,7 +65,7 @@ pub fn jumpTo(self: *Model, idx: usize) void {self.focus = .scrollback;self.follow = false;const sb = @import("layout_cache.zig");- const width = if (self.last_term_width == 0) 80 else self.last_term_width;+ const width = @import("inset.zig").wrapWidth(self);const total = sb.totalVisualLines(self, width);const at = sb.visualOfIndex(self, idx, width) orelse return;// prompt_origin - mid_origin includes the image-card rows render subtractsdiff --git a/TUI/overlays.zig b/TUI/overlays.zigindex a06dc180..73abab0b 100644--- a/TUI/overlays.zig+++ b/TUI/overlays.zig@@ -135,7 +135,7 @@ pub fn rowSpan(self: *const Model) ?Span {// Too narrow to frame is too narrow to CLIP: panel.zig hands the body back// untouched, the rows wrap, and one item stops being one line. Nothing is// clickable at that width rather than the wrong thing being.- if (self.last_term_width < panel.min_width) return null;+ if (@import("inset.zig").wrapWidth(self) < panel.min_width) return null;switch (self.overlay) {.palette => {var idx: [catalog.items.len]usize = undefined;diff --git a/TUI/render.zig b/TUI/render.zigindex a32a6e53..9c9c5adf 100644--- a/TUI/render.zig+++ b/TUI/render.zig@@ -10,6 +10,7 @@ const chrome = @import("chrome.zig");const engine = @import("engine.zig");const glyphs = @import("glyphs.zig");const hover = @import("hover.zig");+const inset = @import("inset.zig");const layout_cache = @import("layout_cache.zig");const scrollbar = @import("scrollbar.zig");const scrollback = @import("scrollback.zig");@@ -28,7 +29,7 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,// rows are not history entries.const rewrap: ?anchor.Anchor = if (width != self.last_term_width and(self.overlay == .none or self.overlay == .image))- anchor.capture(self, self.last_term_width)+ anchor.capture(self, inset.forTerm(self.last_term_width, self.compact_mode).inner)elsenull;self.last_term_width = width;@@ -45,6 +46,13 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,defer arena_state.deinit();const a = arena_state.allocator();+ // grok-build's outer pads (inset.zig): wrap the chrome and transcript to+ // `inner`, then prefix `left` spaces so nothing kisses the terminal edge.+ const pads = inset.forTerm(width, self.compact_mode);+ const inner = pads.inner;+ const left = pads.left;+ const gap = pads.vpad;+// Nothing composed here may be wider than the screen. A row that is// becomes a lie the painter has to clean up after: with autowrap off the// terminal chops it, and on any terminal that does not honour ?7l it wraps@@ -52,9 +60,9 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,// map the composer's click math, the sticky header and the selection band// all ride on. The chrome builders that take `width` clamp themselves; the// overlay bodies and the image card historically did not.- const top = theme_mod.takeCols(try chrome.topBar(self, a, width), width);+ const top = theme_mod.takeCols(try chrome.topBar(self, a, inner), inner);const image_card = if (self.overlay == .image)- try theme_mod.wrapToWidth(a, try chrome.overlay(self, a, width), width)+ try theme_mod.wrapToWidth(a, try chrome.overlay(self, a, inner), inner)else"";// The image overlay is a card ABOVE the composer, so `mid` stays the@@ -66,17 +74,17 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,// so the hash-diff suppressed every paint).const welcome_pane = self.screen == .welcome and self.history.items.len == 0 and self.pending == null;const mid = if (overlay_body)- try theme_mod.wrapToWidth(a, try chrome.overlay(self, a, width), width)+ try theme_mod.wrapToWidth(a, try chrome.overlay(self, a, inner), inner)else if (welcome_pane)- try welcome.render(self, a, width)+ try welcome.render(self, a, inner)else"";// The transcript is never composed whole: the layout cache holds its// wrapped lines and the frame takes a SLICE of them (layout_cache.zig).- const cache: ?*layout_cache.Cache = if (overlay_body or welcome_pane) null else layout_cache.ensure(self, width);- const slash = try chrome.slashMenu(self, a, width);- const prompt = try chrome.promptBox(self, a, width);- const status = try chrome.statusBar(self, a, width);+ const cache: ?*layout_cache.Cache = if (overlay_body or welcome_pane) null else layout_cache.ensure(self, inner);+ const slash = try chrome.slashMenu(self, a, inner);+ const prompt = try chrome.promptBox(self, a, inner);+ const status = try chrome.statusBar(self, a, inner);var bottom = std.array_list.Managed(u8).init(a);if (slash.len > 0) {@@ -86,6 +94,9 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,try bottom.appendSlice(prompt);if (prompt.len == 0 or prompt[prompt.len - 1] != '\n') try bottom.append('\n');try bottom.appendSlice(status);+ // grok-build outer_vpad: one blank row under the hints so the footer is+ // not glued to the last cell of the screen. Compact drops it.+ if (gap > 0) try bottom.append('\n');const top_lines = countLines(top);// countLines counts a trailing '\n' as an extra line but the append below@@ -99,7 +110,7 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,// painter clipped it at the last row, so the composer and the status bar// were simply never drawn, while prompt_origin went on claiming a row for// a composer that was not on screen.- const room = if (height > top_lines + card_lines + 1) height - top_lines - card_lines - 1 else 1;+ const room = if (height > top_lines + card_lines + gap + gap + 1) height - top_lines - card_lines - gap - gap - 1 else 1;const bottom_block = lastLines(bottom.items, room);const bottom_lines = countLines(bottom_block);// The completion menu rides at the TOP of the bottom block, and a short@@ -111,9 +122,11 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,else(countLines(slash) - @intFromBool(slash[slash.len - 1] == '\n')) -| (countLines(bottom.items) - bottom_lines);self.preview_rows = card_lines;- self.mid_origin = top_lines;+ self.mid_origin = top_lines + gap;self.prompt_origin = if (height > bottom_lines) height - bottom_lines else 0;- const used = bottom_lines + top_lines + card_lines;+ // One blank row above the mid (top gutter) and one between the transcript+ // and the composer — grok-build's outer_vpad on both ends of the viewport.+ const used = bottom_lines + top_lines + card_lines + gap + gap;const view_h: usize = if (height > used) height - used else 1;var mid_lines = std.array_list.Managed([]const u8).init(a);@@ -124,8 +137,10 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,}var out = std.array_list.Managed(u8).init(a);- try out.appendSlice(top);+ try inset.appendPadded(&out, top, left);if (top.len > 0 and top[top.len - 1] != '\n') try out.append('\n');+ var lead_gap = gap;+ while (lead_gap > 0) : (lead_gap -= 1) try out.append('\n');const n = if (cache) |c| c.total else mid_lines.items.len;// Mid lines this frame, transcript or overlay body alike. A press below@@ -144,6 +159,7 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,var lead = above;while (lead > 0) : (lead -= 1) try out.append('\n');for (try midSlice(a, cache, mid_lines.items, 0, n)) |ln| {+ try out.appendNTimes(' ', left);try out.appendSlice(ln);try out.append('\n');}@@ -192,7 +208,8 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,var one = utext;if (std.mem.indexOfScalar(u8, utext, '\n')) |nl| one = utext[0..nl];try head.appendSlice(one);- const cols = if (width > 2) width - 2 else 1;+ const cols = if (inner > 2) inner - 2 else 1;+ try out.appendNTimes(' ', left);try out.appendSlice(theme_mod.takeCols(head.items, cols));try out.appendSlice(theme_mod.reset);try out.append('\n');@@ -202,6 +219,7 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,}}for (try midSlice(a, cache, mid_lines.items, start + chrome_rows, view_h - chrome_rows)) |ln| {+ try out.appendNTimes(' ', left);try out.appendSlice(ln);try out.append('\n');}@@ -217,11 +235,13 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,.total = n,};}+ var mid_gap = gap;+ while (mid_gap > 0) : (mid_gap -= 1) try out.append('\n');if (image_card.len > 0) {- try out.appendSlice(image_card);+ try inset.appendPadded(&out, image_card, left);if (image_card[image_card.len - 1] != '\n') try out.append('\n');}- try out.appendSlice(bottom_block);+ try inset.appendPadded(&out, bottom_block, left);// Hover, selection and the scroll gutter are all post-passes over the// finished frame: the row builders stay unaware of all three, and each// lands on screen rows exactly as the mouse reported them (#529). Hoverdiff --git a/TUI/root.zig b/TUI/root.zigindex 6e36582a..503c73f6 100644--- a/TUI/root.zig+++ b/TUI/root.zig@@ -97,6 +97,7 @@ test {_ = @import("syntax.zig");_ = @import("diff.zig");_ = @import("dump.zig");+ _ = @import("inset.zig");_ = @import("effort.zig");_ = @import("sim.zig");_ = @import("render.zig");