graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
merge: single-click actions onto the panelled overlays
Rach Pradhan009aff4f75d6
diff --cc TUI/chrome.zigindex f88257ae,2e0f0740..128f526c--- a/TUI/chrome.zig+++ b/TUI/chrome.zig@@@ -147,72 -142,49 +147,109 @@@ fn fitWidth(width: usize) usizereturn if (width == 0) 80 else width;}-pub fn overlay(self: *const Model, a: std.mem.Allocator, width: usize) ![]const u8 {- return switch (self.overlay) {- .none => "",- .palette => try listOverlay(self, a, width),- .theme => try themeOverlay(self, a, width),- .help => try helpOverlay(self, a),- .rewind => try std.fmt.allocPrint(a, "{s}{s}Rewind last turn?{s}\n{s}Enter rewind · Esc cancel{s}\n", .{ theme_mod.bold, self.theme().accent, theme_mod.reset, self.theme().muted, theme_mod.reset }),- .debug => try debugOverlay(self, a),- .model => try @import("models.zig").render(self, a, width),- .effort => try @import("effort.zig").render(self, a, width),- .settings => try settingsOverlay(self, a, width),- .image => try @import("image.zig").render(self, a, width),- .file => try @import("files.zig").render(self, a, width),- .jump => try jumpOverlay(self, a, width),- .slash => "",- };+const Hint = struct {+ text: []const u8,+ /// Defaults to the muted token at paint time — a hint is never the accent.+ sgr: []const u8 = "",+ /// Reserved before anything else is measured, and painted last: the stop+ /// control is the one segment a narrow terminal may not drop.+ pin: bool = false,+ /// False leaves the segment out entirely (an affordance that is not wired).+ want: bool = true,+};++const hint_sep = " · ";++/// The footer hint, assembled from the segments that FIT.+///+/// grok-build's footer is one line that never wraps and never runs off the+/// edge. graff's was a fixed string handed to takeCols, so an 44-column+/// terminal read "Enter:send · Shift+Enter:newl" — a key name cut in half.+/// Segments are whole or absent, dropped from the right, and a pinned one keeps+/// its room reserved before the walk begins.+fn hintLine(self: *const Model, a: std.mem.Allocator, width: usize, segs: []const Hint) ![]const u8 {+ const th = self.theme();+ const room = fitWidth(width);+ var reserved: usize = 0;+ for (segs) |s| {+ if (s.pin and s.want) reserved += theme_mod.visibleLen(s.text) + theme_mod.visibleLen(hint_sep);+ }+ var out = std.array_list.Managed(u8).init(a);+ try out.append(' ');+ var used: usize = 1;+ var any = false;+ for (segs) |s| {+ if (s.pin or !s.want) continue;+ const cost = theme_mod.visibleLen(s.text) + (if (any) theme_mod.visibleLen(hint_sep) else 0);+ if (used + cost + reserved > room) break;+ if (any) try out.appendSlice(hint_sep);+ try out.appendSlice(try theme_mod.paint(a, if (s.sgr.len > 0) s.sgr else th.muted, s.text));+ used += cost;+ any = true;+ }+ for (segs) |s| {+ if (!s.pin or !s.want) continue;+ if (any) try out.appendSlice(hint_sep);+ try out.appendSlice(try theme_mod.paint(a, if (s.sgr.len > 0) s.sgr else th.muted, s.text));+ any = true;+ }+ // The separators are painted outside any span, so the row would otherwise+ // wear whatever pen the previous paint left on it.+ return theme_mod.paint(a, th.muted, out.items);}- pub fn slashMenu(self: *const Model, a: std.mem.Allocator, width: usize) ![]const u8 {-/// The completion menu's window over the filtered command list: the rows it-/// draws and the item the first of them shows. Clamped selection + a window-/// that follows it, so the highlighted row is always visible and always the-/// row Enter fires (#522) — and so a click can map a screen row straight back-/// to a command (click.zig) instead of re-deriving the same three numbers.-pub fn slashWindow(self: *const Model) ?struct { first: usize, show: usize, n: usize } {++/// The completion menu's geometry, published so the renderer below and the++/// click map (click.zig) share ONE set of numbers instead of each deriving++/// its own. `line0` and `lines` describe the COMPOSED block, frame included:++/// the menu is a panel now, so its first row is a border and an item no++/// longer sits at the block's top line.++pub const SlashGeom = struct {++ /// First item shown - the window follows the highlight.++ first: usize,++ /// Item rows drawn.++ show: usize,++ /// Items in the filtered catalogue.++ n: usize,++ /// Block line the first item row sits on: 1 inside a frame, else 0.++ line0: usize,++ /// Lines the whole block composes to, edges and window row included.++ lines: usize,++};++++pub fn slashWindow(self: *const Model) ?SlashGeom {const v = self.input.getValue();- if (self.focus != .prompt or v.len == 0 or v[0] != '/') return "";+ if (self.focus != .prompt or v.len == 0 or v[0] != '/') return null;var idx: [catalog.items.len]usize = undefined;const n = catalog.filter(v, &idx);- if (n == 0) return "";+ if (n == 0) return null;+ const show = @min(n, @as(usize, 8));+ const sel = @min(self.slash_sel, n - 1);- return .{ .first = if (sel >= show) sel + 1 - show else 0, .show = show, .n = n };++ // 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 edges: usize = if (framed) 2 else 0;++ return .{++ .first = if (sel >= show) sel + 1 - show else 0,++ .show = show,++ .n = n,++ .line0 = if (framed) 1 else 0,++ .lines = show + @intFromBool(n > show) + edges,++ };+ }++ pub fn slashMenu(self: *const Model, a: std.mem.Allocator, width: usize) ![]const u8 {+ const w = slashWindow(self) orelse return "";+ var idx: [catalog.items.len]usize = undefined;- _ = catalog.filter(self.input.getValue(), &idx);++ const n = catalog.filter(self.input.getValue(), &idx);const th = self.theme();var out = std.array_list.Managed(u8).init(a);- const sel = @min(self.slash_sel, w.n - 1);- var i: usize = w.first;- while (i < w.first + w.show) : (i += 1) {+ // Clamped selection + a window that follows it, so the highlighted row is+ // always visible and always the row Enter fires (#522).- const show = @min(n, @as(usize, 8));++ const show = w.show;+ const sel = @min(self.slash_sel, n - 1);- const first = if (sel >= show) sel + 1 - show else 0;++ const first = w.first;+ var i: usize = first;+ while (i < first + show) : (i += 1) {const it = catalog.items[idx[i]];const mark = glyphs.frame(&glyphs.row_mark, @intFromBool(i != sel));// One menu entry is one ROW: a click maps a screen row straight back todiff --cc TUI/click.zigindex 00000000,5cfa197b..0c15e3b4mode 000000,100644..100644--- a/TUI/click.zig+++ b/TUI/click.zig@@@ -1,0 -1,199 +1,204 @@@+ //! Single click as an ACTION.+ //!+ //! Everything AROUND the press was already wired: hover.zig tints the row+ //! under the pointer, the wheel scrolls, a drag paints a band and copies it,+ //! and scrollbar.zig draws a thumb on the right edge. The press itself still+ //! only ever did three things - focus the composer, toggle a fold, or dismiss+ //! whatever overlay was up - so every list on screen looked clickable and was+ //! not. This file is the missing half.+ //!+ //! THREE TARGETS, ONE RULE. A press answers the thing it LANDED on:+ //!+ //! * the scroll gutter (the last column of the band) seeks the viewport, and+ //! keeps seeking while the button is held,+ //! * a row of an open list picks it,+ //! * a row of the slash completion menu picks that command,+ //!+ //! and a press that lands on none of them falls through to exactly what it did+ //! before, byte for byte.+ //!+ //! SELECT, THEN CONFIRM. A click on a list row moves the highlight to it; a+ //! click on the row that ALREADY carries the highlight fires it, like Enter.+ //! That is one rule, not two, and it is also why a double click confirms+ //! without a second copy of the double-click window: the first press makes the+ //! row current and the second one lands on a current row. hover.zig's press+ //! counter stays out of it - a list has no "one net toggle" problem to solve.+ //!+ //! CLICK IS NOT DRAG. A gutter press never reaches selection.zig, so it can+ //! never anchor a band (a scrollbar that fought the copy gesture would be+ //! worse than no scrollbar). Everywhere else the boundary is the one+ //! selection.zig already owns: a press only ARMS the transcript's fold toggle+ //! and records an anchor, and the instant motion proves the gesture was a+ //! drag, `selection.extend` puts the fold back. So a motionless press+release+ //! is a click, a press that moved is a selection, and neither ever does both.+ //!+ //! PRESENTATION ONLY. Which row a list highlights, where the viewport sits and+ //! which command the menu offers are all screen state. What a picked row DOES+ //! is not re-implemented here: a list row goes through `overlays.activate` and+ //! a menu row through `dispatch.runCommand`, which are the two calls Enter+ //! already makes.++ const std = @import("std");++ const app = @import("app.zig");+ const catalog = @import("catalog.zig");+ const chrome = @import("chrome.zig");+ const dispatch = @import("dispatch.zig");+ const key_mod = @import("key.zig");+ const overlays = @import("overlays.zig");+ const scrollbar = @import("scrollbar.zig");+ const Model = app.Model;+ const Effect = app.Effect;++ /// Gesture state. One flag: whether the gutter currently owns the pointer.+ pub const State = struct {+ /// A press landed on the scroll gutter and the button has not come up.+ /// Every report until it does is a seek, and none of them is a selection.+ gutter: bool = false,+ };++ // -------------------------------------------------------------- the gutter++ /// The gutter gesture, run BEFORE selection.zig sees the report. Returns true+ /// when the gutter owns this event.+ ///+ /// A press on the track jumps toward it; the motion reports that follow keep+ /// seeking, which is what makes the thumb draggable. The release ends it. A+ /// motion report with NO button held (35) can only mean the release went+ /// missing - a terminal that lost focus mid-drag - so it ends the gesture+ /// rather than leaving the pointer captured forever.+ pub fn gutterGesture(self: *Model, ev: key_mod.Mouse) bool {+ if (self.click.gutter) {+ if (ev.btn == 35) {+ self.click.gutter = false;+ return false;+ }+ if (!ev.down) {+ self.click.gutter = false;+ return true;+ }+ if (ev.btn == 32 or ev.btn == 0) {+ seek(self, ev.y);+ return true;+ }+ return false;+ }+ if (!ev.down or ev.btn != 0) return false;+ if (!onGutter(self, ev)) return false;+ self.click.gutter = true;+ seek(self, ev.y);+ return true;+ }++ /// Is this press on the gutter? Only while a thumb is actually SHOWING: the+ /// gutter fades out of a tail-parked viewport (scrollbar.zig), and a column+ /// the user cannot see must not swallow a click on the text under it.+ fn onGutter(self: *const Model, ev: key_mod.Mouse) bool {+ if (self.last_term_width < 2) return false;+ const x: usize = if (ev.x > 0) ev.x - 1 else 0;+ if (x != self.last_term_width - 1) return false;+ const y: usize = if (ev.y > 0) ev.y - 1 else 0;+ if (y < self.band.top or y >= self.band.top + self.band.len) return false;+ return scrollbar.current(self) != null;+ }++ /// Put the viewport where the pointer is pointing.+ ///+ /// The thumb is CENTRED on the row under the pointer, the way a browser's is:+ /// grabbing it anywhere and dragging then moves it by the number of rows the+ /// pointer moved, instead of snapping its top edge under the cursor.+ ///+ /// The arithmetic runs in the scrollbar's own coordinates - `band.off` lines+ /// above the viewport, out of `band.total` - and comes back through the one+ /// door every other scroll uses (`keys.scrollBy`), so the fade clock, the+ /// follow latch and the hover drop are wound exactly once, here as anywhere.+ fn seek(self: *Model, row1: u16) void {+ const b = self.band;+ if (!b.live or b.len == 0 or b.total <= b.len) return;+ const t = scrollbar.current(self) orelse return;+ const y: usize = if (row1 > 0) row1 - 1 else 0;+ const in_track = std.math.clamp(y, b.top, b.top + b.len - 1) - b.top;+ const span = b.len - t.len; // rows the thumb's TOP can take+ const max_off = b.total - b.len;+ const want_top = @min(in_track -| t.len / 2, span);+ const off = if (span == 0) max_off else (want_top * max_off + span / 2) / span;+ // `scroll` counts from the BOTTOM, `off` from the top; the sticky header+ // occludes rows the scroll never reaches, so the achievable maximum falls+ // short of `max_off` by exactly those rows (render.zig).+ const target = @min(max_off - off, b.total -| (b.len + self.sticky_rows));+ if (target == self.scroll) return;+ const d: i64 = @as(i64, @intCast(target)) - @as(i64, @intCast(self.scroll));+ @import("keys.zig").scrollBy(self, std.math.lossyCast(i32, d));+ }++ // ---------------------------------------------------------------- the lists++ /// A press that reached the click layer: the wheel, the drag and the image+ /// card have all already declined it, and it is a left-button DOWN report.+ /// Returns the effect when a list answered it, else null - and null is what+ /// keeps every pre-existing meaning of a press intact.+ pub fn press(self: *Model, ev: key_mod.Mouse) ?Effect {+ const y: usize = if (ev.y > 0) ev.y - 1 else 0;+ if (self.overlay == .none) return slashPress(self, y);+ if (self.overlay == .image) return null; // a card, not a panel+ return overlayPress(self, y);+ }++ /// The list item on screen row `y` of an open overlay, or null. Shared with+ /// hover.zig so the tint and the click cannot disagree about what a row is.+ pub fn overlayRowAt(self: *const Model, y: usize) ?usize {+ const span = overlays.rowSpan(self) orelse return null;+ if (y < self.mid_origin or y >= self.prompt_origin) return null;+ const line = (y - self.mid_origin) + self.mid_skip;+ if (line < span.line0 or line >= span.line0 + span.rows) return null;+ return span.first + (line - span.line0);+ }++ /// A press with a list overlay up. Inside the panel it picks a row or does+ /// nothing; OUTSIDE it - the backdrop under a short panel, the top bar, the+ /// composer - it declines, and the press closes the overlay exactly as it+ /// always has (keys.zig), which is the click-outside-to-dismiss half.+ fn overlayPress(self: *Model, y: usize) ?Effect {+ // An overlay with no list (help, the observability HUD, the rewind+ // prompt) has no rows to pick and keeps its click-anywhere dismissal.+ _ = overlays.rowSpan(self) orelse return null;+ if (y < self.mid_origin or y >= self.prompt_origin) return null;+ const line = (y - self.mid_origin) + self.mid_skip;+ if (line >= self.mid_total) return null; // padding below a short panel+ const item = overlayRowAt(self, y) orelse return .stay; // title, footer: inert+ return overlays.clickRow(self, item);+ }++ /// A press on the slash completion menu, which sits at the TOP of the bottom+ /// block - the rows `prompt_origin` names before the composer box starts. Same+ /// select-then-confirm rule as an overlay list.+ fn slashPress(self: *Model, y: usize) ?Effect {+ if (self.slash_rows == 0) return null;+ if (y < self.prompt_origin or y >= self.prompt_origin + self.slash_rows) return null;+ const w = chrome.slashWindow(self) orelse return null;- // A short terminal trims the bottom block from the TOP (render.zig), so- // the first row still on screen is not the first row the menu wanted.- const item = w.first + (w.show -| self.slash_rows) + (y - self.prompt_origin);++ // The menu is a PANEL: its first line is a border, its last is another,++ // and a windowed list spends one more on the "N below" row. Work in block++ // lines, not screen rows. A short terminal trims the bottom block from the++ // TOP (render.zig), so the first line still on screen is not line 0.++ const trimmed = w.lines -| self.slash_rows;++ const line = (y - self.prompt_origin) + trimmed;++ if (line < w.line0 or line >= w.line0 + w.show) return null; // an edge++ const item = w.first + (line - w.line0);+ if (item >= w.n) return null;+ if (@min(self.slash_sel, w.n - 1) != item) {+ self.slash_sel = item;+ return .stay;+ }+ var idx: [catalog.items.len]usize = undefined;+ const n = catalog.filter(self.input.getValue(), &idx);+ if (n == 0 or item >= n) return .stay;+ const pick = catalog.items[idx[item]];+ self.input.setValue("") catch {};+ return dispatch.runCommand(self, pick.name);+ }++ test {+ // The tests live next door so this file stays under the ceiling. Without+ // this reference they compile for nobody and silently never run.+ _ = @import("click_tests.zig");+ }diff --cc TUI/click_tests.zigindex 00000000,57d9bd7d..6db5c599mode 000000,100644..100644--- a/TUI/click_tests.zig+++ b/TUI/click_tests.zig@@@ -1,0 -1,478 +1,520 @@@+ //! Single-click semantics (click.zig), pinned.+ //!+ //! Four boundaries live here, and every one of them is a place the gesture+ //! could quietly do the wrong thing:+ //!+ //! * a fold header answers a click with EXACTLY one net toggle, however fast+ //! the second click arrives,+ //! * a press that MOVED is a selection and never a fold toggle; a press that+ //! did not is a click and never touches the clipboard,+ //! * the scroll gutter owns its column before the selection can anchor in+ //! it, and gives the pointer back on the button-up,+ //! * a list row picks, the row that already carries the highlight confirms,+ //! and the backdrop around the panel dismisses it.+ //!+ //! The overlay cases drive a REAL composed frame and find their row by reading+ //! it back, so the click map (overlays.rowSpan) is checked against the bytes+ //! the renderer actually produced rather than against a second opinion.++ const std = @import("std");++ const app = @import("app.zig");+ const catalog = @import("catalog.zig");+ const chrome = @import("chrome.zig");+ const engine = @import("engine.zig");+ const hover = @import("hover.zig");+ const keys = @import("keys.zig");+ const overlays = @import("overlays.zig");+ const render_mod = @import("render.zig");+ const scrollbar = @import("scrollbar.zig");+ const theme_mod = @import("theme.zig");+ const Model = app.Model;++ const alloc = std.testing.allocator;+ const expect = std.testing.expect;+ const expectEqual = std.testing.expectEqual;++ fn press(m: *Model, x: u16, y: u16) app.Effect {+ return keys.handle(m, .{ .mouse = .{ .btn = 0, .x = x, .y = y, .down = true } });+ }++ fn release(m: *Model, x: u16, y: u16) void {+ _ = keys.handle(m, .{ .mouse = .{ .btn = 0, .x = x, .y = y, .down = false } });+ }++ fn motion(m: *Model, x: u16, y: u16) void {+ _ = keys.handle(m, .{ .mouse = .{ .btn = 32, .x = x, .y = y, .down = true } });+ }++ /// A click: press and release on the SAME cell with nothing in between.+ fn clickAt(m: *Model, x: u16, y: u16) app.Effect {+ const e = press(m, x, y);+ release(m, x, y);+ return e;+ }++ fn frame(m: *Model, w: usize, h: usize) ![]const u8 {+ return render_mod.render(m, alloc, w, h, m.now_ms);+ }+++/// The text of screen row `y1` (1-based, as a mouse report numbers it).++fn rowText(f: []const u8, y1: u16) ?[]const u8 {++ var r: usize = 0;++ var it = std.mem.splitScalar(u8, f, '\n');++ while (it.next()) |ln| : (r += 1) {++ if (r == @as(usize, y1) - 1) return ln;++ }++ return null;++}++++/// The `/command` printed on a menu row, SGR and row mark stripped off.++fn commandOn(line: []const u8) ?[]const u8 {++ const at = std.mem.indexOfScalar(u8, line, '/') orelse return null;++ var end = at;++ while (end < line.len and line[end] != ' ' and line[end] != 0x1b) end += 1;++ return line[at..end];++}+++ /// 0-based screen row of the first frame line carrying `needle`.+ fn rowOf(f: []const u8, needle: []const u8) ?usize {+ var r: usize = 0;+ var it = std.mem.splitScalar(u8, f, '\n');+ while (it.next()) |ln| : (r += 1) {+ if (std.mem.indexOf(u8, ln, needle) != null) return r;+ }+ return null;+ }++ /// A transcript with one collapsed tool group, laid out by hand so the row a+ /// press lands on is arithmetic rather than a search through the frame.+ fn foldModel(m: *Model) !void {+ m.setup(alloc);+ try m.push(.user, "hi");+ try m.pushTool(.{ .name = "bash" });+ try m.pushTool(.{ .name = "bash", .detail = "ok", .done = true });+ m.last_term_width = 80;+ m.last_term_height = 24;+ m.mid_origin = 1;+ m.mid_skip = 0;+ m.prompt_origin = 20;+ }++ // ------------------------------------------------------------------- folds++ test "a motionless click on a fold header is exactly one net toggle" {+ var m: Model = undefined;+ try foldModel(&m);+ defer m.deinit();+ try expect(m.history.items[1].folded);+ _ = clickAt(&m, 4, 3);+ try expect(!m.history.items[1].folded);+ // A click is not a selection: no band, no capture, no toast claiming one.+ try expect(!m.sel.active);+ try expect(!m.sel.pressed);+ try expectEqual(@as(usize, 0), m.sel_text.len);+ try std.testing.expectEqualStrings("", m.toast);+ // ...and the next click, outside the double-click window, puts it back.+ m.now_ms += hover.double_ms + 1;+ _ = clickAt(&m, 4, 3);+ try expect(m.history.items[1].folded);+ }++ test "two clicks inside the double-click window are ONE net toggle" {+ // The pinned semantics: the first press toggles, the second REPLACES that+ // action rather than repeating it, so the pair opens the group and leaves+ // it open. Toggling twice would look like the gesture did nothing at all.+ var m: Model = undefined;+ try foldModel(&m);+ defer m.deinit();+ try expect(m.history.items[1].folded);+ _ = clickAt(&m, 4, 3);+ _ = clickAt(&m, 4, 3); // same cell, same millisecond+ try expect(!m.history.items[1].folded);+ // A third press, still inside the window, opens a fresh single click: the+ // completed double disarmed, so the counter cannot fire twice in a row.+ _ = clickAt(&m, 4, 3);+ try expect(m.history.items[1].folded);+ }++ test "a press that MOVES is a selection, and leaves the fold as it was" {+ var m: Model = undefined;+ try foldModel(&m);+ defer m.deinit();+ try expect(m.history.items[1].folded);+ _ = press(&m, 4, 3);+ motion(&m, 30, 5);+ // The optimistic expand is reverted the instant motion proves the drag.+ try expect(m.history.items[1].folded);+ try expect(m.sel.active);+ // A gesture that moved can never be half of a double click either.+ try expect(!m.hover.armed);+ release(&m, 30, 5);+ try expect(m.history.items[1].folded);+ }++ // --------------------------------------------------------------- the gutter++ /// A transcript long enough to scroll, composed for real so the band, the+ /// thumb and the viewport all agree with each other.+ fn scrolledModel(m: *Model) !void {+ m.setup(alloc);+ var i: usize = 0;+ while (i < 200) : (i += 1) {+ var buf: [32]u8 = undefined;+ try m.push(.assistant, try std.fmt.bufPrint(&buf, "line {d:0>3}", .{i}));+ }+ const first = try frame(m, 80, 24);+ alloc.free(first);+ keys.scrollBy(m, 40);+ const second = try frame(m, 80, 24);+ alloc.free(second);+ }++ test "a press on the gutter seeks the viewport and never anchors a band" {+ var m: Model = undefined;+ try scrolledModel(&m);+ defer m.deinit();+ try expect(m.band.live);+ try expect(scrollbar.current(&m) != null);+ const track_top: u16 = @intCast(m.band.top + 1);+ const track_bottom: u16 = @intCast(m.band.top + m.band.len);+ // The bottom of the track is the tail of the transcript.+ _ = press(&m, 80, track_bottom);+ try expect(m.click.gutter);+ try expectEqual(@as(usize, 0), m.scroll);+ try expect(m.follow);+ // A scrollbar gesture is never a drag-copy, and never a fold toggle.+ try expect(!m.sel.pressed);+ try expect(!m.sel.active);+ try expectEqual(app.Focus.prompt, m.focus);+ release(&m, 80, track_bottom);+ try expect(!m.click.gutter);+ // ...and the top of the track is the top of the transcript.+ _ = press(&m, 80, track_top);+ const f = try frame(&m, 80, 24);+ defer alloc.free(f);+ try expectEqual(@as(usize, 0), m.band.off -| m.sticky_rows);+ try expect(rowOf(f, "line 000") != null);+ release(&m, 80, track_top);+ }++ test "a drag down the gutter walks the viewport, one report at a time" {+ var m: Model = undefined;+ try scrolledModel(&m);+ defer m.deinit();+ const track_top: u16 = @intCast(m.band.top + 1);+ _ = press(&m, 80, track_top);+ try expect(m.click.gutter);+ const at_top = m.scroll;+ var y = track_top;+ var last = at_top;+ while (y < @as(u16, @intCast(m.band.top + m.band.len))) : (y += 1) {+ motion(&m, 80, y + 1);+ // Monotone: dragging DOWN the track only ever moves toward the tail.+ try expect(m.scroll <= last);+ last = m.scroll;+ // Still the gutter's pointer — the motion never became a selection.+ try expect(!m.sel.active);+ }+ try expect(last < at_top);+ release(&m, 80, @intCast(m.band.top + m.band.len));+ try expect(!m.click.gutter);+ }++ test "a faded gutter is not a target: the press means what it always meant" {+ var m: Model = undefined;+ try scrolledModel(&m);+ defer m.deinit();+ keys.scrollBy(&m, -1000); // back to the tail+ const f = try frame(&m, 80, 24);+ alloc.free(f);+ m.now_ms += scrollbar.fade_ms + 1;+ try expect(scrollbar.current(&m) == null);+ const row: u16 = @intCast(m.band.top + 2);+ _ = press(&m, 80, row);+ try expect(!m.click.gutter);+ try expectEqual(@as(usize, 0), m.scroll);+ // The ordinary transcript press ran instead.+ try expectEqual(app.Focus.scrollback, m.focus);+ release(&m, 80, row);+ }++ test "a key hands the pointer back when the gutter's release goes missing" {+ var m: Model = undefined;+ try scrolledModel(&m);+ defer m.deinit();+ _ = press(&m, 80, @intCast(m.band.top + 1));+ try expect(m.click.gutter);+ _ = keys.handle(&m, .{ .char = 'x' });+ try expect(!m.click.gutter);+ }++ // -------------------------------------------------------------- list rows++ fn modelOverlay(m: *Model) !void {+ m.setup(alloc);+ m.openOverlay(.model);+ }+++/// The catalog the click tests pick through. Entries now, not a joined string:++/// the picker reads `engine.g_model_entries` (the provider column lives there).++const click_catalog = [_]engine.ModelEntry{++ .{ .name = "alpha-one", .provider = "openai", .has_key = true, .cost = .api },++ .{ .name = "beta-two", .provider = "openai", .has_key = true, .cost = .api },++ .{ .name = "gamma-three", .provider = "openai", .has_key = true, .cost = .api },++};+++ test "a click on a model row highlights it, and a second click picks it" {- engine.g_models = "alpha-one, beta-two, gamma-three";++ engine.g_model_entries = &click_catalog;+ engine.g_model_name = "alpha-one";+ defer {- engine.g_models = "";++ engine.g_model_entries = &.{};+ engine.g_model_name = "";+ }+ var m: Model = undefined;+ try modelOverlay(&m);+ defer m.deinit();+ const f = try frame(&m, 80, 24);+ defer alloc.free(f);+ const row = rowOf(f, "gamma-three") orelse return error.NoModelRow;+ _ = press(&m, 3, @intCast(row + 1));+ try expectEqual(app.Overlay.model, m.overlay); // selecting is not picking+ try expectEqual(@as(usize, 2), m.overlay_sel);+ _ = press(&m, 3, @intCast(row + 1));+ try expectEqual(app.Overlay.none, m.overlay);+ try std.testing.expectEqualStrings("gamma-three", engine.g_model_name);+ }++ test "the panel keeps a press, and the backdrop around it dismisses" {- engine.g_models = "alpha-one, beta-two, gamma-three";- defer engine.g_models = "";++ engine.g_model_entries = &click_catalog;++ defer engine.g_model_entries = &.{};+ var m: Model = undefined;+ try modelOverlay(&m);+ defer m.deinit();+ const f = try frame(&m, 80, 24);+ defer alloc.free(f);+ // The title is inside the panel: inert, and the overlay stays up.+ const title = rowOf(f, "Model ›") orelse return error.NoTitle;+ _ = press(&m, 3, @intCast(title + 1));+ try expectEqual(app.Overlay.model, m.overlay);+ try expectEqual(@as(usize, 0), m.overlay_sel);- // Below the body's last line is the backdrop, and a press there closes it- // exactly like Esc.- const below: u16 = @intCast(m.mid_origin + m.mid_total + 1);- try expect(below <= m.prompt_origin);- _ = press(&m, 3, below);++ // The panel DOCKS to the composer (render.zig), so the backdrop is the++ // empty band ABOVE it - and a press up there closes it exactly like Esc.++ try expect(m.mid_origin > 0);++ const above: u16 = @intCast(m.mid_origin - 1);++ _ = press(&m, 3, above);+ try expectEqual(app.Overlay.none, m.overlay);+ }++ test "an overlay with no rows to pick still closes on any press" {+ var m: Model = undefined;+ m.setup(alloc);+ defer m.deinit();+ m.openOverlay(.help);+ const f = try frame(&m, 80, 24);+ alloc.free(f);+ try expect(overlays.rowSpan(&m) == null);+ _ = press(&m, 10, @intCast(m.mid_origin + 3));+ try expectEqual(app.Overlay.none, m.overlay);+ }++ test "a settings row opens the picker it names" {+ var m: Model = undefined;+ m.setup(alloc);+ defer m.deinit();+ m.openOverlay(.settings);+ const f = try frame(&m, 80, 24);+ defer alloc.free(f);+ const row = rowOf(f, "Theme") orelse return error.NoSettingsRow;+ _ = press(&m, 3, @intCast(row + 1)); // highlight+ try expectEqual(@as(usize, 3), m.overlay_sel);+ _ = press(&m, 3, @intCast(row + 1)); // confirm+ try expectEqual(app.Overlay.theme, m.overlay);+ }++ test "hover announces a list row, so the tint and the click agree" {- engine.g_models = "alpha-one, beta-two, gamma-three";- defer engine.g_models = "";++ engine.g_model_entries = &click_catalog;++ defer engine.g_model_entries = &.{};+ var m: Model = undefined;+ try modelOverlay(&m);+ defer m.deinit();+ const f = try frame(&m, 80, 24);+ defer alloc.free(f);+ const row = rowOf(f, "beta-two") orelse return error.NoModelRow;+ const hit = hover.hitTest(&m, row);+ try expectEqual(hover.Target.row, hit.target);+ try expectEqual(@as(?usize, 1), hit.idx);+ // The title is not a target — an affordance on a row a press ignores+ // would be worse than none.+ const title = rowOf(f, "Model ›") orelse return error.NoTitle;+ try expectEqual(hover.Target.none, hover.hitTest(&m, title).target);+ }++ // ------------------------------------------------------------ slash menu++ test "a click on a completion row highlights it, and on the current one runs it" {+ var m: Model = undefined;+ m.setup(alloc);+ defer m.deinit();+ m.focus = .prompt;+ try m.input.setValue("/");+ const f = try frame(&m, 80, 24);+ defer alloc.free(f);+ try expect(m.slash_rows > 2);+ try expectEqual(@as(usize, 0), m.slash_sel);- const w = chrome.slashWindow(&m) orelse return error.NoMenu;- const trimmed = w.show -| m.slash_rows;- _ = press(&m, 3, @intCast(m.prompt_origin + 3));- try expectEqual(w.first + trimmed + 2, m.slash_sel);++ // Round-trip against the BYTES: read the command printed on the row that++ // is about to be clicked, then check the highlight landed on that same++ // command. The menu is a framed panel, so a click map that forgot the++ // border would select a neighbour and still look arithmetically tidy.++ const target_row: u16 = @intCast(m.prompt_origin + 3);++ const printed = rowText(f, target_row) orelse return error.NoMenuRow;++ const name = commandOn(printed) orelse return error.NoCommandOnRow;++ _ = press(&m, 3, target_row);++ var idx: [catalog.items.len]usize = undefined;++ const n = catalog.filter(m.input.getValue(), &idx);++ try expect(m.slash_sel < n);++ try std.testing.expectEqualStrings(name, catalog.items[idx[m.slash_sel]].name);+ // ...and the draft is untouched: highlighting is not running.+ try std.testing.expectEqualStrings("/", m.input.getValue());+ }++ test "a click on the highlighted completion row runs that command" {+ var m: Model = undefined;+ m.setup(alloc);+ defer m.deinit();+ m.focus = .prompt;+ try m.input.setValue("/plan");+ const f = try frame(&m, 80, 24);+ defer alloc.free(f);+ const w = chrome.slashWindow(&m) orelse return error.NoMenu;+ try expectEqual(@as(usize, 1), w.n);- try expectEqual(@as(usize, 1), m.slash_rows);++ // One item, and the two panel edges around it.++ try expectEqual(@as(usize, 1), w.show);++ try expectEqual(w.lines, m.slash_rows);+ try expectEqual(app.AgentMode.normal, m.mode);- _ = press(&m, 3, @intCast(m.prompt_origin + 1));++ // +2, not +1: the row under the panel's TOP EDGE is the item.++ _ = press(&m, 3, @intCast(m.prompt_origin + 2));+ try expectEqual(app.AgentMode.plan, m.mode);+ try std.testing.expectEqualStrings("", m.input.getValue());+ }++ // ------------------------------------------------------- the map vs the bytes++ /// Every body line fits the width, and the span's rows carry `want` in order.+ /// This is the drift guard: `rowSpan` states each renderer's header shape as+ /// arithmetic, and the day a renderer grows a line the arithmetic is a lie —+ /// clicks would land one row off, silently, on the wrong item.+ fn checkSpan(m: *Model, width: usize, want: []const []const u8) !void {+ var arena = std.heap.ArenaAllocator.init(alloc);+ defer arena.deinit();+ const body = try chrome.overlay(m, arena.allocator(), width);+ var lines = std.array_list.Managed([]const u8).init(arena.allocator());+ var it = std.mem.splitScalar(u8, body, '\n');+ while (it.next()) |ln| try lines.append(ln);+ // A list row is CUT, never wrapped: one item is one line, or the map that+ // turns a screen row into an item index cannot be arithmetic at all.+ for (lines.items) |ln| try expect(theme_mod.visibleLen(ln) <= width);+ const span = overlays.rowSpan(m) orelse return error.NoSpan;+ try expectEqual(want.len, span.rows);+ for (want, 0..) |text, k| {+ const ln = lines.items[span.line0 + k];+ if (std.mem.indexOf(u8, ln, text) == null) {+ std.debug.print("row {d} (body line {d}) = {s}\n", .{ k, span.line0 + k, ln });+ return error.RowDrifted;+ }+ }+ }++ test "the click map names the rows the model picker drew" {- engine.g_models = "alpha-one, beta-two, gamma-three";- defer engine.g_models = "";++ engine.g_model_entries = &click_catalog;++ defer engine.g_model_entries = &.{};+ var m: Model = undefined;+ m.setup(alloc);+ defer m.deinit();+ m.openOverlay(.model);+ try checkSpan(&m, 80, &.{ "alpha-one", "beta-two", "gamma-three" });+ }++ test "the click map names the rows the effort picker drew" {+ var m: Model = undefined;+ m.setup(alloc);+ defer m.deinit();+ m.openOverlay(.effort);+ try checkSpan(&m, 80, &.{ "low", "medium", "high", "xhigh", "max", "ultra" });+ }++ test "the click map names the rows the file picker drew, however long the path" {+ var m: Model = undefined;+ m.setup(alloc);+ defer m.deinit();+ m.files_cache = try alloc.dupe(u8, "src/a.zig\n" +++ "a/very/deeply/nested/directory/tree/that/runs/past/the/screen/edge/file.zig\n" +++ "docs/b.md");+ m.openOverlay(.file);+ try checkSpan(&m, 80, &.{ "src/a.zig", "a/very/deeply", "docs/b.md" });+ // ...and at a width where the long path can only be a cut row.+ try checkSpan(&m, 40, &.{ "src/a.zig", "a/very/deeply", "docs/b.md" });+ }++ test "the click map names the rows the settings, theme and jump panels drew" {+ var m: Model = undefined;+ m.setup(alloc);+ defer m.deinit();+ m.openOverlay(.settings);+ try checkSpan(&m, 80, &.{ "Model", "Effort", "Mode", "Theme" });+ m.openOverlay(.theme);+ var want: [theme_mod.all.len][]const u8 = undefined;+ for (theme_mod.all, 0..) |id, i| want[i] = id.label();+ try checkSpan(&m, 80, &want);+ try m.push(.user, "first turn");+ try m.push(.assistant, "ok");+ try m.push(.user, "second turn");+ m.openOverlay(.jump);+ try checkSpan(&m, 80, &.{ "first turn", "second turn" });+ }++ test "the click map names the rows the command palette drew" {+ var m: Model = undefined;+ m.setup(alloc);+ defer m.deinit();+ m.openOverlay(.palette);+ try m.input.setValue("/th");+ var idx: [catalog.items.len]usize = undefined;+ const n = catalog.filter("/th", &idx);+ try expect(n > 0 and n <= 12);+ var want = std.array_list.Managed([]const u8).init(alloc);+ defer want.deinit();+ for (idx[0..n]) |i| try want.append(catalog.items[i].name);+ try checkSpan(&m, 80, want.items);+ }++ test "a click on a SCROLLED overlay body still names the row under it" {+ // The picker's window follows the highlight and the body itself scrolls+ // once it outgrows the viewport, so both offsets have to be in the map.- var list = std.array_list.Managed(u8).init(alloc);- defer list.deinit();++ var arena = std.heap.ArenaAllocator.init(alloc);++ defer arena.deinit();++ var list = std.array_list.Managed(engine.ModelEntry).init(arena.allocator());+ for (0..40) |i| {- if (i > 0) try list.appendSlice(", ");- var buf: [16]u8 = undefined;- try list.appendSlice(try std.fmt.bufPrint(&buf, "mdl-{d:0>2}", .{i}));++ try list.append(.{++ .name = try std.fmt.allocPrint(arena.allocator(), "mdl-{d:0>2}", .{i}),++ .provider = "openai",++ .has_key = true,++ .cost = .api,++ });+ }- engine.g_models = list.items;- defer engine.g_models = "";++ engine.g_model_entries = list.items;++ defer engine.g_model_entries = &.{};+ var m: Model = undefined;+ m.setup(alloc);+ defer m.deinit();+ m.openOverlay(.model);+ m.overlay_sel = 30; // window scrolled well down the list- const f = try frame(&m, 80, 20);++ m.overlay_scroll = 3; // ...and the panel is taller than this viewport++ const f = try frame(&m, 80, 12);+ defer alloc.free(f);+ try expect(m.mid_skip > 0); // the body scrolled too+ const row = rowOf(f, "mdl-25") orelse return error.NoModelRow;+ _ = press(&m, 3, @intCast(row + 1));+ try expectEqual(@as(usize, 25), m.overlay_sel);+ try expectEqual(app.Overlay.model, m.overlay);+ }diff --cc TUI/effort.zigindex 72a9d714,4229f9aa..69c24f4e--- a/TUI/effort.zig+++ b/TUI/effort.zig@@@ -39,19 -38,8 +39,19 @@@ pub fn pick(self: *const Model) ?enginereturn buf[self.overlay_sel % n];}-pub fn render(self: *const Model, a: std.mem.Allocator, width: usize) ![]const u8 {- const w = @import("chrome.zig").rowCols(width);+/// The panel's top-edge slots (overlaypane.zig places them).+pub fn head(self: *const Model, a: std.mem.Allocator) !panel.Head {+ var names: [all.len]engine.Effort = undefined;+ return .{+ .title = try std.fmt.allocPrint(a, "Effort › {s}\u{258B}", .{self.overlay_filter}),+ .note = try std.fmt.allocPrint(a, "{d}/{d}", .{ filter(self.overlay_filter, &names), all.len }),+ };+}+- pub const hint = "type to filter · ↑↓ move · Enter pick · Esc";++pub const hint = "type to filter · ↑↓ move · click or Enter picks · Esc";++/// The ROWS only — the frame carries the rest.+pub fn render(self: *const Model, a: std.mem.Allocator) ![]const u8 {const th = self.theme();var names: [all.len]engine.Effort = undefined;const n = filter(self.overlay_filter, &names);diff --cc TUI/files.zigindex 4ee26a0b,1278431c..dee795f1--- a/TUI/files.zig+++ b/TUI/files.zig@@@ -9,7 -8,9 +9,9 @@@ const theme_mod = @import("theme.zig")const Model = app.Model;pub const max_files = 512;- const visible_rows = 10;-/// Rows the picker draws at once — the click map reads this same window-/// (overlays.rowSpan) instead of keeping a second copy of it.++/// Rows the picker draws at once - the click map (overlays.rowSpan) reads this++/// same window instead of keeping a second copy of it.+ pub const visible_rows = 10;/// Newline-joined `list` filtered by substring-or-subsequence `query`.pub fn filterList(list: []const u8, query: []const u8, out: [][]const u8) usize {@@@ -32,29 -34,20 +34,29 @@@ pub fn head(self: *const Model, a: std.const list = self.files_cache orelse "";const total = filterList(list, "", &names);const n = filterList(list, self.overlay_filter, &names);+ return .{+ .title = try std.fmt.allocPrint(a, "File › {s}\u{258B}", .{self.overlay_filter}),+ .note = try std.fmt.allocPrint(a, "{d}/{d}", .{ n, total }),+ };+}+- pub const hint = "type to search · ↑↓ move · Enter insert · Esc";++pub const hint = "type to search · ↑↓ move · click or Enter inserts · Esc";++/// The ROWS only — the frame carries the rest.+pub fn render(self: *const Model, a: std.mem.Allocator) ![]const u8 {+ const th = self.theme();+ var names: [max_files][]const u8 = undefined;+ const list = self.files_cache orelse "";+ const n = filterList(list, self.overlay_filter, &names);var out = std.array_list.Managed(u8).init(a);- const title = theme_mod.takeCols(try std.fmt.allocPrint(a, "File › {s}▋", .{self.overlay_filter}), w);- try out.appendSlice(try theme_mod.paint(a, th.accent, title));- try out.append('\n');- try out.appendSlice(try theme_mod.paint(a, th.muted, try std.fmt.allocPrint(a, "{d}/{d}", .{ n, total })));- try out.appendSlice("\n\n");if (n == 0) {- const hint = if (@import("bgop.zig").loadingFiles(self))- "loading files…"+ const why = if (@import("bgop.zig").loadingFiles(self))+ " loading files…"else if (list.len == 0)- "no file list (offline?) — Esc"+ " no file list (offline?)"else- "no matches — type to filter, Esc";- try out.appendSlice(try theme_mod.paint(a, th.muted, theme_mod.takeCols(hint, w)));+ " no matches — type to filter";+ try out.appendSlice(try theme_mod.paint(a, th.muted, why));try out.append('\n');return out.items;}diff --cc TUI/overlaypane.zigindex 0864dba2,00000000..cda328b7mode 100644,000000..100644--- a/TUI/overlaypane.zig+++ b/TUI/overlaypane.zig@@@ -1,422 -1,0 +1,422 @@@+//! Overlay bodies, and the panel each one is framed in.+//!+//! Split out of chrome.zig: the composer and the overlays had grown into one+//! file over its ceiling, and they answer to different rules. chrome.zig owns+//! the rows that are ALWAYS on screen; this file owns the ones that replace the+//! transcript while they are open.+//!+//! Every overlay is the same shape — a name, a tally, some rows, the keys —+//! and panel.zig turns that shape into grok-build's bordered box. Nothing here+//! paints an edge itself, so a panel cannot drift surface by surface.++const std = @import("std");++const app = @import("app.zig");+const catalog = @import("catalog.zig");+const engine = @import("engine.zig");+const glyphs = @import("glyphs.zig");+const panel = @import("panel.zig");+const theme_mod = @import("theme.zig");+const Model = app.Model;++/// Scroll hint for the bodies that are PROSE rather than a list. It rides in+/// the TOP edge: those are the bodies long enough to overflow the viewport, and+/// an overflowing panel has its bottom edge off the bottom of the screen.+const scroll_note = "↑↓ scroll · Esc";++pub fn overlay(self: *const Model, a: std.mem.Allocator, width: usize) ![]const u8 {+ // The image card is a preview pinned above the composer, not a panel that+ // replaces the transcript; it keeps its own shape (image.zig).+ if (self.overlay == .none or self.overlay == .slash) return "";+ if (self.overlay == .image) return @import("image.zig").render(self, a, width);+ return panel.wrap(a, self.theme(), width, try spec(self, a));+}++fn spec(self: *const Model, a: std.mem.Allocator) !panel.Spec {+ const th = self.theme();+ return switch (self.overlay) {+ .palette => blk: {+ var idx: [catalog.items.len]usize = undefined;+ break :blk .{+ .title = "Commands",+ .note = try tally(a, catalog.filter(self.input.getValue(), &idx), catalog.items.len),- .footer = "↑↓ move · Enter run · Esc",++ .footer = "↑↓ move · click or Enter runs · Esc",+ .body = try listBody(self, a),+ };+ },+ // No tally: a list that cannot be filtered has nothing to count down.+ .theme => .{+ .title = "Theme",- .footer = "↑↓ move · Enter apply · Esc",++ .footer = "↑↓ move · click or Enter applies · Esc",+ .body = try themeBody(self, a),+ },+ .help => .{+ .title = "Shortcuts",+ .note = scroll_note,+ .footer = "Esc close",+ .body = try theme_mod.paint(a, th.text, help_body),+ },+ .rewind => .{+ .title = "Rewind",+ .footer = "Enter rewind · Esc cancel",+ .body = try theme_mod.paint(a, th.text, " Undo the last turn?"),+ },+ .debug => .{+ .title = "Observability",+ .note = scroll_note,+ .footer = "Esc close",+ .body = try debugBody(self, a),+ },+ .model => blk: {+ const models = @import("models.zig");+ const head = try models.head(self, a);+ break :blk .{ .title = head.title, .note = head.note, .footer = models.hint, .body = try models.render(self, a) };+ },+ .effort => blk: {+ const effort = @import("effort.zig");+ const head = try effort.head(self, a);+ break :blk .{ .title = head.title, .note = head.note, .footer = effort.hint, .body = try effort.render(self, a) };+ },+ .file => blk: {+ const files = @import("files.zig");+ const head = try files.head(self, a);+ break :blk .{ .title = head.title, .note = head.note, .footer = files.hint, .body = try files.render(self, a) };+ },+ .settings => .{+ .title = "Settings",- .footer = "↑↓ move · Enter change · Esc",++ .footer = "↑↓ move · click or Enter changes · Esc",+ .body = try settingsBody(self, a),+ },+ .jump => .{+ .title = "Jump to turn",+ .note = if (self.userTurnCount() == 0) "" else try std.fmt.allocPrint(a, "{d} turns", .{self.userTurnCount()}),- .footer = "↑↓ move · Enter jump · Esc",++ .footer = "↑↓ move · click or Enter jumps · Esc",+ .body = try jumpBody(self, a),+ },+ else => .{ .body = "" },+ };+}++fn tally(a: std.mem.Allocator, n: usize, total: usize) ![]const u8 {+ return std.fmt.allocPrint(a, "{d}/{d}", .{ n, total });+}++fn listBody(self: *const Model, a: std.mem.Allocator) ![]const u8 {+ const th = self.theme();+ var idx: [catalog.items.len]usize = undefined;+ const n = catalog.filter(self.input.getValue(), &idx);+ var out = std.array_list.Managed(u8).init(a);+ const show = @min(n, @as(usize, 12));+ if (show == 0) {+ try out.appendSlice(try theme_mod.paint(a, th.muted, " no matches"));+ try out.append('\n');+ return out.items;+ }+ const sel = self.overlay_sel % show;+ var i: usize = 0;+ while (i < show) : (i += 1) {+ const it = catalog.items[idx[i]];+ const mark = glyphs.frame(&glyphs.row_mark, @intFromBool(i != sel));+ // One menu entry is one ROW: a long description is CUT by the panel+ // wall, never wrapped — wrapping would slide every entry below it off+ // its own row.+ const line = try std.fmt.allocPrint(a, "{s}{s} {s}", .{ mark, it.name, it.desc });+ try out.appendSlice(if (i == sel) try theme_mod.paint(a, th.accent, line) else try theme_mod.paint(a, th.muted, line));+ try out.append('\n');+ }+ if (n > show) try out.appendSlice(try panel.windowRow(a, th, 0, show, n));+ return out.items;+}++fn themeBody(self: *const Model, a: std.mem.Allocator) ![]const u8 {+ const th = self.theme();+ var out = std.array_list.Managed(u8).init(a);+ const sel = self.overlay_sel % theme_mod.all.len;+ for (theme_mod.all, 0..) |id, i| {+ const mark = glyphs.frame(&glyphs.row_mark, @intFromBool(i != sel));+ const cur: []const u8 = if (id == self.theme_id) " (current)" else "";+ const line = try std.fmt.allocPrint(a, "{s}{s}{s}", .{ mark, id.label(), cur });+ try out.appendSlice(if (i == sel) try theme_mod.paint(a, th.accent, line) else try theme_mod.paint(a, th.muted, line));+ try out.append('\n');+ }+ return out.items;+}++fn jumpBody(self: *const Model, a: std.mem.Allocator) ![]const u8 {+ const th = self.theme();+ var out = std.array_list.Managed(u8).init(a);+ const total = self.userTurnCount();+ if (total == 0) {+ try out.appendSlice(try theme_mod.paint(a, th.muted, " no turns yet"));+ try out.append('\n');+ return out.items;+ }+ const sel = self.overlay_sel % total;+ var no: usize = 0;+ for (self.history.items) |e| {+ if (e.kind != .user) continue;+ const nl = std.mem.indexOfScalar(u8, e.text, '\n') orelse e.text.len;+ const clip = e.text[0..@min(nl, 60)];+ const mark = glyphs.frame(&glyphs.row_mark, @intFromBool(no != sel));+ const line = try std.fmt.allocPrint(a, "{s}#{d} {s}", .{ mark, no + 1, clip });+ try out.appendSlice(if (no == sel) try theme_mod.paint(a, th.accent, line) else try theme_mod.paint(a, th.muted, line));+ try out.append('\n');+ no += 1;+ }+ return out.items;+}++fn settingsBody(self: *const Model, a: std.mem.Allocator) ![]const u8 {+ const th = self.theme();+ var out = std.array_list.Managed(u8).init(a);+ const rows = [_][2][]const u8{+ .{ "Model", if (engine.g_model_name.len > 0) engine.g_model_name else "—" },+ .{ "Effort", @tagName(self.effort) },+ .{ "Mode", self.modeLabel() },+ .{ "Theme", self.theme_id.label() },+ };+ const sel = self.overlay_sel % rows.len;+ for (rows, 0..) |r, i| {+ const mark = glyphs.frame(&glyphs.row_mark, @intFromBool(i != sel));+ const line = try std.fmt.allocPrint(a, "{s}{s:<10} {s}", .{ mark, r[0], r[1] });+ try out.appendSlice(if (i == sel) try theme_mod.paint(a, th.accent, line) else try theme_mod.paint(a, th.muted, line));+ try out.append('\n');+ }+ return out.items;+}++fn debugBody(self: *const Model, a: std.mem.Allocator) ![]const u8 {+ const th = self.theme();+ var buf: [2048]u8 = undefined;+ const hud: []const u8 = if (engine.g_hud_fn) |f| buf[0..f(.debug, &buf)] else "observability (offline — no session sink)\n";+ var lbuf: [256]u8 = undefined;+ const lay = @import("dump.zig").layoutBuf(&lbuf, self);+ const joined = try std.fmt.allocPrint(a, "{s}{s}", .{ hud, lay });+ // The HUD is written for a bare pager and starts every section flush left.+ // Inside a panel that puts the section headings hard against the wall while+ // their own rows sit two columns in, so the block is inset to the two the+ // picker rows already use and the whole overlay set shares one left margin.+ var out = std.array_list.Managed(u8).init(a);+ try out.appendSlice(th.text);+ var it = std.mem.splitScalar(u8, joined, '\n');+ while (it.next()) |ln| {+ if (ln.len == 0 and it.rest().len == 0) break;+ try out.appendSlice(" ");+ try out.appendSlice(ln);+ try out.append('\n');+ }+ return out.items;+}++/// The shortcut sheet. No leading heading of its own: the panel's top edge+/// already says "Shortcuts", and a body that repeated it spent a row saying+/// what the frame had said one line earlier.+const help_body =+ \\ Tab prompt / scrollback+ \\ Enter send+ \\ Esc cancel · 2× clear · 2× rewind+ \\ Ctrl+C cancel, then quit+ \\ Ctrl+X / F1 this overlay+ \\ Ctrl+P command palette+ \\ Shift+Tab Normal → Plan → Always-approve+ \\ click / ← → collapse / expand tools+ \\ hover [Image] preview · y copy · Enter open+ \\ Shift+← → prev / next user turn+ \\ Ctrl+J / K scroll one line+ \\ Ctrl+N N new session+ \\ Ctrl+Q quit+ \\ drag select transcript · copy on release+ \\ Shift+drag the terminal's own selection (any region)+ \\ Ctrl+Z undo composer+ \\ Cmd+Delete kill to start of line+ \\ Option+Delete kill previous word+ \\ wheel scroll transcript+ \\ PgUp / PgDn page transcript+ \\ cmd+v paste text+ \\ Ctrl+V attach clipboard image+ \\ Ctrl+R prompt history+ \\ / slash menu+ \\ @ fuzzy file mention+ \\ !cmd run a shell command+ \\+ \\Commands+ \\ /quit /exit /q leave the pager+ \\ /help this overlay+ \\ /new /clear fresh session+ \\ /home welcome screen+ \\ /model switch model (type to search)+ \\ /effort reasoning depth (type to filter)+ \\ /settings model · effort · mode · theme+ \\ /usage /cost live token/cost line+ \\ /debug observability HUD+ \\ /plan toggle plan mode+ \\ /always-approve skip permission prompts+ \\ /import-claude copy Claude and Cursor MCP + skills+ \\ /jump jump to a previous turn+ \\ /copy copy the last reply+ \\ /btw queue an aside mid-turn+ \\ /vim-mode vim keys in the scrollback+;++const testing = std.testing;++test "help overlay names the advertised pager commands" {+ var m: Model = undefined;+ m.setup(testing.allocator);+ defer m.deinit();+ m.openOverlay(.help);+ var arena = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena.deinit();+ const text = try overlay(&m, arena.allocator(), 80);+ for ([_][]const u8{+ "/quit", "/help", "/new", "/home", "/model", "/settings", "/usage", "/debug", "/plan", "/always-approve",+ "Shift+Tab", "PgUp", "PgDn",+ "←",+ "→",+ "Tab", "Enter", "Esc",+ }) |name| {+ try testing.expect(std.mem.indexOf(u8, text, name) != null);+ }+ // The name is in the FRAME, and the body no longer repeats it.+ try testing.expectEqual(@as(usize, 1), std.mem.count(u8, text, "Shortcuts"));+}++test "model overlay lists the catalog with its provider column" {+ engine.g_model_entries = &.{+ .{ .name = "grok-4", .provider = "xai", .has_key = true, .cost = .plan },+ .{ .name = "gpt-5.5", .provider = "openai", .has_key = true, .cost = .api },+ };+ engine.g_model_name = "grok-4";+ engine.g_model_provider = "xai";+ defer {+ engine.g_model_entries = &.{};+ engine.g_model_name = "";+ engine.g_model_provider = "";+ }+ var m: Model = undefined;+ m.setup(testing.allocator);+ defer m.deinit();+ m.openOverlay(.model);+ var arena = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena.deinit();+ const text = try overlay(&m, arena.allocator(), 80);+ try testing.expect(std.mem.indexOf(u8, text, "grok-4") != null);+ try testing.expect(std.mem.indexOf(u8, text, "gpt-5.5") != null);+ try testing.expect(std.mem.indexOf(u8, text, "Model ›") != null);+ // The provider rides on the row, inside the frame.+ try testing.expect(std.mem.indexOf(u8, text, "xai · plan") != null);+ try testing.expect(std.mem.indexOf(u8, text, "openai · api") != null);+}++test "debug overlay is not the offline fallback when a hud is wired" {+ engine.g_hud_fn = struct {+ fn f(kind: engine.HudKind, buf: []u8) usize {+ _ = kind;+ const s = "observability graff.schema v1\n usage 1 api call(s)";+ const n = @min(s.len, buf.len);+ @memcpy(buf[0..n], s[0..n]);+ return n;+ }+ }.f;+ defer engine.g_hud_fn = null;+ var m: Model = undefined;+ m.setup(testing.allocator);+ defer m.deinit();+ m.openOverlay(.debug);+ var arena = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena.deinit();+ const text = try overlay(&m, arena.allocator(), 80);+ try testing.expect(std.mem.indexOf(u8, text, "offline") == null);+ try testing.expect(std.mem.indexOf(u8, text, "usage") != null);+ try testing.expect(std.mem.indexOf(u8, text, "overlay debug") != null);+ try testing.expect(std.mem.indexOf(u8, text, "prompt-origin") != null);+}++test "every overlay is a bordered panel with its name in the frame" {+ // The audit this pass exists for: before it, an open picker was bare text+ // floating on the pager with no edge anywhere, and each surface spelled its+ // own title row. One panel builder means one look, and the check is that+ // NONE of them opts out.+ engine.g_model_entries = &.{+ .{ .name = "grok-4", .provider = "xai", .has_key = true, .cost = .plan },+ .{ .name = "gpt-5.5", .provider = "openai", .has_key = true, .cost = .api },+ };+ defer engine.g_model_entries = &.{};+ const cases = [_]struct { o: app.Overlay, name: []const u8 }{+ .{ .o = .palette, .name = "Commands" },+ .{ .o = .theme, .name = "Theme" },+ .{ .o = .help, .name = "Shortcuts" },+ .{ .o = .rewind, .name = "Rewind" },+ .{ .o = .debug, .name = "Observability" },+ .{ .o = .model, .name = "Model" },+ .{ .o = .effort, .name = "Effort" },+ .{ .o = .file, .name = "File" },+ .{ .o = .settings, .name = "Settings" },+ .{ .o = .jump, .name = "Jump to turn" },+ };+ for (cases) |c| {+ for ([_]usize{ 40, 60, 80, 120 }) |w| {+ var m: Model = undefined;+ m.setup(testing.allocator);+ defer m.deinit();+ try m.push(.user, "a turn so the jump list has a row");+ m.openOverlay(c.o);+ var arena = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena.deinit();+ const text = try overlay(&m, arena.allocator(), w);+ var it = std.mem.splitScalar(u8, text, '\n');+ const top = it.next() orelse return error.EmptyPanel;+ if (std.mem.indexOf(u8, top, "╭") == null) {+ std.debug.print("{s} @ {d} has no top edge: {s}\n", .{ @tagName(c.o), w, top });+ return error.NoPanelEdge;+ }+ try testing.expect(std.mem.indexOf(u8, top, c.name) != null);+ // ...and the last row is the closing edge, with the keys on it.+ var last: []const u8 = top;+ while (it.next()) |ln| {+ if (ln.len > 0) last = ln;+ }+ try testing.expect(std.mem.indexOf(u8, last, "╰") != null);+ try testing.expect(std.mem.indexOf(u8, last, "Esc") != null);+ // Every row is exactly the frame width — the painter's contract.+ var rows = std.mem.splitScalar(u8, text, '\n');+ while (rows.next()) |ln| {+ if (ln.len == 0 and rows.rest().len == 0) break;+ try testing.expectEqual(w, theme_mod.visibleLen(ln));+ }+ }+ }+}++test "a windowed list says how much of itself is off-screen" {+ // 75 models in a 14-row window used to show 14 rows and no hint at all+ // that the other 61 existed.+ var arena0 = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena0.deinit();+ var buf = std.array_list.Managed(engine.ModelEntry).init(arena0.allocator());+ for (0..75) |i| {+ try buf.append(.{+ .name = try std.fmt.allocPrint(arena0.allocator(), "model-{d}", .{i}),+ .provider = "openai",+ .has_key = true,+ .cost = .api,+ });+ }+ engine.g_model_entries = buf.items;+ defer engine.g_model_entries = &.{};+ var m: Model = undefined;+ m.setup(testing.allocator);+ defer m.deinit();+ m.openOverlay(.model);+ var arena = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena.deinit();+ const text = try overlay(&m, arena.allocator(), 80);+ try testing.expect(std.mem.indexOf(u8, text, "61 below") != null);+ try testing.expect(std.mem.indexOf(u8, text, "0/75") == null);+ try testing.expect(std.mem.indexOf(u8, text, "75/75") != null);+ // Walked to the end of the list, the marker turns around instead of+ // vanishing — the row is reserved either way, so nothing below it jumps.+ m.overlay_sel = 74;+ const tail = try overlay(&m, arena.allocator(), 80);+ try testing.expect(std.mem.indexOf(u8, tail, "61 above") != null);+}diff --cc TUI/overlays.zigindex f5b9c147,431cad3e..a06dc180--- a/TUI/overlays.zig+++ b/TUI/overlays.zig@@@ -8,6 -8,6 +8,7 @@@ const catalog = @import("catalog.zig")const dispatch = @import("dispatch.zig");const engine = @import("engine.zig");const files_mod = @import("files.zig");++const panel = @import("panel.zig");const theme_mod = @import("theme.zig");const Key = @import("key.zig").Key;const Model = app.Model;@@@ -100,6 -69,89 +101,98 @@@ pub fn wheel(self: *Model, up: bool) boreturn true;}+ // ------------------------------------------------------------- row geometry++ /// Where an open list overlay's ROWS sit inside its own composed body, in body+ /// lines. render.zig lays that body out exactly like the transcript - screen+ /// row `mid_origin + k` shows body line `mid_skip + k` - so this plus those two+ /// numbers is the whole row map a click needs.+ ///-/// The header shapes are the renderers' own: a bare title (palette, theme), a-/// title and a blank (settings, jump), or a title, a count and a blank (the-/// searchable model / effort / file pickers). Each list row is CUT to the-/// screen width by its renderer, never wrapped, so one item is always one line-/// and this map can be arithmetic rather than a second pass over the bytes.++/// Every overlay is a PANEL now (panel.zig): its name and its tally ride in the++/// top edge and its keys in the bottom one, so the body a list composes is its++/// rows and nothing else, and the frame contributes exactly one line above them.++/// That is why `line0` is the same for all of them - the per-overlay header++/// shapes this used to track no longer exist. The panel also CLIPS every row to++/// its inner width instead of wrapping, which is what keeps one item on one++/// line and lets this map be arithmetic rather than a second pass over bytes.+ pub const Span = struct {+ /// Body line of the first row drawn.+ line0: usize,+ /// Item index that row shows - a windowed list starts partway down.+ first: usize,+ /// Rows drawn.+ rows: usize,+ /// Items in the FILTERED list, which is what `overlay_sel` indexes.+ total: usize,+ };+++/// The panel's top edge, and the only reason `line0` is not zero.++const frame_rows: usize = 1;+++ pub fn rowSpan(self: *const Model) ?Span {+ const models = @import("models.zig");+ const effort_mod = @import("effort.zig");++ // 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;+ switch (self.overlay) {+ .palette => {+ var idx: [catalog.items.len]usize = undefined;+ const n = catalog.filter(self.input.getValue(), &idx);+ const show = @min(n, @as(usize, 12));+ if (show == 0) return null;- return .{ .line0 = 1, .first = 0, .rows = show, .total = show };++ return .{ .line0 = frame_rows, .first = 0, .rows = show, .total = show };+ },- .theme => return .{ .line0 = 1, .first = 0, .rows = theme_mod.all.len, .total = theme_mod.all.len },- .settings => return .{ .line0 = 2, .first = 0, .rows = 4, .total = 4 },++ .theme => return .{ .line0 = frame_rows, .first = 0, .rows = theme_mod.all.len, .total = theme_mod.all.len },++ .settings => return .{ .line0 = frame_rows, .first = 0, .rows = 4, .total = 4 },+ .jump => {+ const total = self.userTurnCount();+ if (total == 0) return null;- return .{ .line0 = 2, .first = 0, .rows = total, .total = total };++ return .{ .line0 = frame_rows, .first = 0, .rows = total, .total = total };+ },+ .effort => {+ var buf: [effort_mod.all.len]engine.Effort = undefined;+ const n = effort_mod.filter(self.overlay_filter, &buf);+ if (n == 0) return null;- return .{ .line0 = 3, .first = 0, .rows = n, .total = n };++ return .{ .line0 = frame_rows, .first = 0, .rows = n, .total = n };+ },+ .model => {- var names: [models.max_models][]const u8 = undefined;- const n = models.filterModels(engine.g_models, self.overlay_filter, &names);++ var rows: [models.max_models]engine.ModelEntry = undefined;++ const n = models.filterModels(engine.g_model_entries, self.overlay_filter, &rows);+ if (n == 0) return null;- return windowed(3, n, self.overlay_sel % n, models.visible_rows);++ return windowed(frame_rows, n, self.overlay_sel % n, models.visible_rows);+ },+ .file => {+ var names: [files_mod.max_files][]const u8 = undefined;+ const n = files_mod.filterList(self.files_cache orelse "", self.overlay_filter, &names);+ if (n == 0) return null;- return windowed(3, n, self.overlay_sel % n, files_mod.visible_rows);++ return windowed(frame_rows, n, self.overlay_sel % n, files_mod.visible_rows);+ },+ else => return null,+ }+ }++ /// The scrolling window the searchable pickers draw: `vis` rows that follow the+ /// highlight, so it is always on screen and always the row Enter fires.+ fn windowed(line0: usize, n: usize, sel: usize, vis_max: usize) Span {+ const vis = @min(vis_max, n);+ const off = if (sel >= vis) sel - vis + 1 else 0;+ return .{ .line0 = line0, .first = off, .rows = @min(vis, n - off), .total = n };+ }++ /// A click landed on item `item`. The first click on a row moves the highlight+ /// there; a click on the row that already carries it CONFIRMS, exactly as+ /// Enter does. Those two halves are also what make a double click a confirm,+ /// without a second copy of hover.zig's double-click window.+ pub fn clickRow(self: *Model, item: usize) Effect {+ const span = rowSpan(self) orelse return .stay;+ if (item >= span.total) return .stay;+ if (self.overlay_sel % span.total == item) return activate(self);+ self.overlay_sel = item;+ return .stay;+ }+/// Open the @-picker immediately and load the session file list in the/// background. Loading it inline blocked the render+input thread for up to the/// runCapped 10s cap on the first @ of a session (#533).diff --cc TUI/render.zigindex 6d6ce3ce,9a2eb67a..a32a6e53--- a/TUI/render.zig+++ b/TUI/render.zig@@@ -354,246 -345,242 +365,8 @@@ test "countLines"try std.testing.expectEqual(@as(usize, 3), countLines("a\nb\nc"));}--test "welcome frame has chrome, prompt, and no offline stub" {-- engine.g_model_name = "grok-4";-- engine.g_cwd = "/tmp/proj";-- defer {-- engine.g_model_name = "";-- engine.g_cwd = ".";-- }-- var m: Model = undefined;-- m.setup(std.testing.allocator);-- defer m.deinit();-- const frame = try render(&m, std.testing.allocator, 80, 24, 0);-- defer std.testing.allocator.free(frame);-- try std.testing.expect(std.mem.indexOf(u8, frame, "grok-4") != null);-- try std.testing.expect(std.mem.indexOf(u8, frame, "╭") != null);-- try std.testing.expect(std.mem.indexOf(u8, frame, "›") != null);-- try std.testing.expect(std.mem.indexOf(u8, frame, "offline") == null);-- try std.testing.expect(std.mem.indexOf(u8, frame, "Fullscreen") == null);-- try std.testing.expect(std.mem.indexOf(u8, frame, "click") == null);--}----test "click on a painted fold header expands the tools" {-- var m: Model = undefined;-- m.setup(std.testing.allocator);-- defer m.deinit();-- try m.push(.assistant, "Hey. I am in /Users/blackfloofie/codegraff on wip/shared-checkout-2026-08-14 and holding a long worktree path so this wraps");-- try m.pushTool(.{ .name = "bash" });-- try m.pushTool(.{ .name = "bash", .detail = "ok", .done = true });-- const frame = try render(&m, std.testing.allocator, 80, 24, 0);-- defer std.testing.allocator.free(frame);-- var row: usize = 0;-- var hit: ?usize = null;-- var it = std.mem.splitScalar(u8, frame, '\n');-- while (it.next()) |ln| : (row += 1) {-- if (std.mem.indexOf(u8, ln, "Ran bash") != null) {-- hit = row;-- break;-- }-- }-- try std.testing.expect(hit != null);-- try std.testing.expect(m.history.items[1].folded);-- _ = @import("keys.zig").handle(&m, .{ .mouse = .{ .btn = 0, .x = 4, .y = @intCast(hit.? + 1), .down = true } });-- try std.testing.expect(!m.history.items[1].folded);--}----test "image overlay keeps the conversation and paints the Grok card" {-- var m: Model = undefined;-- m.setup(std.testing.allocator);-- defer m.deinit();-- try m.push(.user, "@[/tmp/shot.png] look");-- try m.push(.assistant, "got it");-- m.preview_path = "/tmp/shot.png";-- m.preview_n = 1;-- m.openOverlay(.image);-- const frame = try render(&m, std.testing.allocator, 80, 24, 0);-- defer std.testing.allocator.free(frame);-- try std.testing.expect(std.mem.indexOf(u8, frame, "Image #1") != null);-- try std.testing.expect(std.mem.indexOf(u8, frame, "got it") != null);-- try std.testing.expect(std.mem.indexOf(u8, frame, "y copy path") != null);--}----test "visible dump of a fixture frame shows chips, the fold header, composer, no CSI" {-- var m: Model = undefined;-- m.setup(std.testing.allocator);-- defer m.deinit();-- try m.push(.user, "@[/tmp/shot.png] look");-- try m.pushTool(.{ .name = "bash" });-- try m.pushTool(.{ .name = "bash", .detail = "ok", .done = true });-- const frame = try render(&m, std.testing.allocator, 80, 24, 0);-- defer std.testing.allocator.free(frame);-- const dump_mod = @import("dump.zig");-- const vis = try dump_mod.visible(std.testing.allocator, frame);-- defer std.testing.allocator.free(vis);-- try std.testing.expect(std.mem.indexOf(u8, vis, "[Image #1]") != null);-- try std.testing.expect(std.mem.indexOf(u8, vis, "Ran bash") != null);-- try std.testing.expect(std.mem.indexOf(u8, vis, "╭") != null);-- try std.testing.expect(std.mem.indexOf(u8, vis, "›") != null);-- try std.testing.expect(std.mem.indexOf(u8, vis, "\x1b") == null);-- const lay = try dump_mod.layout(std.testing.allocator, &m);-- defer std.testing.allocator.free(lay);-- try std.testing.expect(std.mem.indexOf(u8, lay, "overlay none") != null);-- try std.testing.expect(std.mem.indexOf(u8, lay, "prompt-origin") != null);-- try std.testing.expect(std.mem.indexOf(u8, lay, "mid-origin") != null);-- try std.testing.expect(std.mem.indexOf(u8, lay, "images") != null);---- m.openOverlay(.debug);- // The overlay body is built from many small pieces now that it is framed- // (panel.zig), so it wants the arena every other chrome builder gets.- var oarena = std.heap.ArenaAllocator.init(std.testing.allocator);- defer oarena.deinit();- const dbg = try chrome.overlay(&m, oarena.allocator(), 80);- const dbg = try chrome.overlay(&m, std.testing.allocator, 80);- defer std.testing.allocator.free(dbg);-- const lay_dbg = try dump_mod.layout(std.testing.allocator, &m);-- defer std.testing.allocator.free(lay_dbg);-- try std.testing.expect(std.mem.indexOf(u8, lay_dbg, "overlay debug") != null);-- try std.testing.expect(std.mem.indexOf(u8, dbg, "Observability") != null);-- if (std.c.getenv("GRAFF_TUI_DUMP")) |dir_z| {-- const dir = std.mem.span(dir_z);-- const io = std.Io.Threaded.global_single_threaded.io();-- var fb: [512]u8 = undefined;-- var lb: [512]u8 = undefined;-- const fp = std.fmt.bufPrint(&fb, "{s}/tui-frame.txt", .{dir}) catch return error.PathTooLong;-- const lp = std.fmt.bufPrint(&lb, "{s}/tui-layout.txt", .{dir}) catch return error.PathTooLong;-- std.Io.Dir.cwd().writeFile(io, .{ .sub_path = fp, .data = vis }) catch {};-- const both = try std.fmt.allocPrint(std.testing.allocator, "--- overlay none ---\n{s}\n--- overlay debug ---\n{s}\n--- debug overlay body ---\n{s}", .{ lay, lay_dbg, dbg });-- defer std.testing.allocator.free(both);-- std.Io.Dir.cwd().writeFile(io, .{ .sub_path = lp, .data = both }) catch {};-- }--}----test "debug overlay keeps the observability HUD and adds layout" {-- var m: Model = undefined;-- m.setup(std.testing.allocator);-- defer m.deinit();-- try m.push(.user, "@[/tmp/shot.png] look");-- try m.pushTool(.{ .name = "bash" });-- const frame = try render(&m, std.testing.allocator, 80, 24, 0);-- defer std.testing.allocator.free(frame);-- m.openOverlay(.debug);- var oarena = std.heap.ArenaAllocator.init(std.testing.allocator);- defer oarena.deinit();- const text = try chrome.overlay(&m, oarena.allocator(), 80);- const text = try chrome.overlay(&m, std.testing.allocator, 80);- defer std.testing.allocator.free(text);-- try std.testing.expect(std.mem.indexOf(u8, text, "Observability") != null);-- try std.testing.expect(std.mem.indexOf(u8, text, "offline") != null);-- try std.testing.expect(std.mem.indexOf(u8, text, "overlay debug") != null);-- try std.testing.expect(std.mem.indexOf(u8, text, "focus") != null);-- try std.testing.expect(std.mem.indexOf(u8, text, "prompt-origin") != null);-- try std.testing.expect(std.mem.indexOf(u8, text, "mid-origin") != null);-- try std.testing.expect(std.mem.indexOf(u8, text, "images") != null);-- try std.testing.expect(std.mem.indexOf(u8, text, "pending") != null);--}----test "a wheel notch reports a scroll hint; anything else about the frame refuses one" {-- const keys = @import("keys.zig");-- const a = std.testing.allocator;-- var m: Model = undefined;-- m.setup(a);-- defer m.deinit();-- try m.push(.user, "how do I frobnicate the widget?");-- var i: usize = 0;-- while (i < 60) : (i += 1)-- try m.pushFmt(.assistant, "explanation line {d}, long enough to fill the transcript", .{i});---- // A first frame has nothing to compare against, and an idle one has not moved.-- for (0..2) |_| {-- a.free(try render(&m, a, 80, 24, 0));-- try std.testing.expect(m.paint_hint == null);-- }---- // One wheel notch. The band slides by exactly the three content lines-- // keys.zig moved the viewport, and keeps its geometry — which is the whole-- // claim paint.zig acts on. `top` clears the sticky header: those rows are-- // chrome occluding the viewport, and a hardware scroll must not move them.-- _ = keys.handle(&m, .{ .mouse = .{ .btn = 64, .x = 1, .y = 5, .down = true } });-- a.free(try render(&m, a, 80, 24, 0));-- const h = m.paint_hint orelse return error.NoScrollHint;-- try std.testing.expectEqual(@as(isize, -3), h.delta);-- try std.testing.expectEqual(m.mid_origin + m.sticky_rows, h.top);-- try std.testing.expectEqual(@as(usize, 2), m.sticky_rows);-- try std.testing.expect(h.len >= 2 and h.top + h.len <= 24);---- // A resize on the same frame as a scroll: every row below rewraps, so the-- // band is not the same band and nothing about it may be claimed to slide.-- keys.scrollBy(&m, 3);-- a.free(try render(&m, a, 79, 24, 0));-- try std.testing.expect(m.paint_hint == null);---- // A live drag-selection band is anchored to SCREEN rows, so its inverse-- // video does not travel with the content a hardware scroll would move.-- a.free(try render(&m, a, 79, 24, 0));-- m.sel.active = true;-- keys.scrollBy(&m, 3);-- a.free(try render(&m, a, 79, 24, 0));-- try std.testing.expect(m.paint_hint == null);-- m.sel.active = false;---- // Scrolling all the way to the top clamps: the viewport stops moving, and-- // a band that did not move is not a scroll.-- m.scroll = 1_000_000;-- a.free(try render(&m, a, 79, 24, 0));-- a.free(try render(&m, a, 79, 24, 0));-- try std.testing.expect(m.paint_hint == null);---- // An overlay replaces the whole viewport — no band at all, so no hint even-- // though the frame changed completely.-- m.openOverlay(.help);-- a.free(try render(&m, a, 79, 24, 0));-- try std.testing.expect(m.paint_hint == null);--}----test "the sticky header never pins a prompt over an overlay" {-- var m: Model = undefined;-- m.setup(std.testing.allocator);-- defer m.deinit();-- try m.push(.user, "SECRETPROMPT about widgets");-- var i: usize = 0;-- while (i < 40) : (i += 1) try m.push(.assistant, "filler assistant prose to make the transcript long");-- // It engages on the transcript...-- const transcript = try render(&m, std.testing.allocator, 80, 24, 0);-- std.testing.allocator.free(transcript);-- try std.testing.expectEqual(@as(usize, 2), m.sticky_rows);-- // ...and stands down once `mid` is an overlay body long enough to scroll:-- // ungated, the pin ate the overlay's own first two rows.-- m.openOverlay(.help);-- const overlay_frame = try render(&m, std.testing.allocator, 80, 24, 0);-- defer std.testing.allocator.free(overlay_frame);-- try std.testing.expectEqual(@as(usize, 0), m.sticky_rows);-- try std.testing.expect(std.mem.indexOf(u8, overlay_frame, "SECRETPROMPT") == null);-- // The image overlay is a card, not a body: the transcript keeps its pin.-- m.closeOverlay();-- m.preview_path = "/tmp/shot.png";-- m.preview_n = 1;-- m.openOverlay(.image);-- const card_frame = try render(&m, std.testing.allocator, 80, 24, 0);-- defer std.testing.allocator.free(card_frame);-- try std.testing.expectEqual(@as(usize, 2), m.sticky_rows);--}----test "sticky header pins the last scrolled-past user prompt with a blank separator" {-- var m: Model = undefined;-- m.setup(std.testing.allocator);-- defer m.deinit();-- try m.push(.user, "how do I frobnicate the widget?");-- var i: usize = 0;-- while (i < 40) : (i += 1) try m.push(.assistant, "a long explanation line that fills the scrollback with content");-- // follow-mode bottom: prompt is far above, so it pins.-- const frame = try render(&m, std.testing.allocator, 80, 24, 0);-- defer std.testing.allocator.free(frame);-- var it = std.mem.splitScalar(u8, frame, '\n');-- var row: usize = 0;-- var pin_row: ?usize = null;-- while (it.next()) |ln| : (row += 1) {-- if (std.mem.indexOf(u8, ln, "\u{276F} ") != null and std.mem.indexOf(u8, ln, "frobnicate") != null) {-- pin_row = row;-- break;-- }-- }-- try std.testing.expect(pin_row != null);-- // scrolled fully back to the top, the prompt is inline: no pin.-- m.follow = false;-- m.scroll = 100000;-- const top_frame = try render(&m, std.testing.allocator, 80, 24, 0);-- defer std.testing.allocator.free(top_frame);-- try std.testing.expect(std.mem.indexOf(u8, top_frame, "\u{276F} ") == null);++test {++ // The frame-level tests live next door so this file stays under the++ // ceiling. Without this reference they compile for nobody and never run.++ _ = @import("render_tests.zig");}diff --cc TUI/render_tests.zigindex 00000000,00000000..5e04c826new file mode 100644--- /dev/null+++ b/TUI/render_tests.zig@@@ -1,0 -1,0 +1,262 @@@++//! Frame-level tests for render.zig.++//!++//! Split out of render.zig to keep it under the line ceiling. These drive the++//! PUBLIC `render` entry point and read the composed frame back, so they live++//! just as well next door; the two tests that reach for render.zig's private++//! line helpers stayed behind with them.++++const std = @import("std");++++const app = @import("app.zig");++const chrome = @import("chrome.zig");++const dump_mod = @import("dump.zig");++const engine = @import("engine.zig");++const scrollback = @import("scrollback.zig");++const selection = @import("selection.zig");++const welcome = @import("welcome.zig");++const Model = app.Model;++++const render = @import("render.zig").render;++++test "welcome frame has chrome, prompt, and no offline stub" {++ engine.g_model_name = "grok-4";++ engine.g_cwd = "/tmp/proj";++ defer {++ engine.g_model_name = "";++ engine.g_cwd = ".";++ }++ var m: Model = undefined;++ m.setup(std.testing.allocator);++ defer m.deinit();++ const frame = try render(&m, std.testing.allocator, 80, 24, 0);++ defer std.testing.allocator.free(frame);++ try std.testing.expect(std.mem.indexOf(u8, frame, "grok-4") != null);++ try std.testing.expect(std.mem.indexOf(u8, frame, "╭") != null);++ try std.testing.expect(std.mem.indexOf(u8, frame, "›") != null);++ try std.testing.expect(std.mem.indexOf(u8, frame, "offline") == null);++ try std.testing.expect(std.mem.indexOf(u8, frame, "Fullscreen") == null);++ try std.testing.expect(std.mem.indexOf(u8, frame, "click") == null);++}++++test "click on a painted fold header expands the tools" {++ var m: Model = undefined;++ m.setup(std.testing.allocator);++ defer m.deinit();++ try m.push(.assistant, "Hey. I am in /Users/blackfloofie/codegraff on wip/shared-checkout-2026-08-14 and holding a long worktree path so this wraps");++ try m.pushTool(.{ .name = "bash" });++ try m.pushTool(.{ .name = "bash", .detail = "ok", .done = true });++ const frame = try render(&m, std.testing.allocator, 80, 24, 0);++ defer std.testing.allocator.free(frame);++ var row: usize = 0;++ var hit: ?usize = null;++ var it = std.mem.splitScalar(u8, frame, '\n');++ while (it.next()) |ln| : (row += 1) {++ if (std.mem.indexOf(u8, ln, "Ran bash") != null) {++ hit = row;++ break;++ }++ }++ try std.testing.expect(hit != null);++ try std.testing.expect(m.history.items[1].folded);++ _ = @import("keys.zig").handle(&m, .{ .mouse = .{ .btn = 0, .x = 4, .y = @intCast(hit.? + 1), .down = true } });++ try std.testing.expect(!m.history.items[1].folded);++}++++test "image overlay keeps the conversation and paints the Grok card" {++ var m: Model = undefined;++ m.setup(std.testing.allocator);++ defer m.deinit();++ try m.push(.user, "@[/tmp/shot.png] look");++ try m.push(.assistant, "got it");++ m.preview_path = "/tmp/shot.png";++ m.preview_n = 1;++ m.openOverlay(.image);++ const frame = try render(&m, std.testing.allocator, 80, 24, 0);++ defer std.testing.allocator.free(frame);++ try std.testing.expect(std.mem.indexOf(u8, frame, "Image #1") != null);++ try std.testing.expect(std.mem.indexOf(u8, frame, "got it") != null);++ try std.testing.expect(std.mem.indexOf(u8, frame, "y copy path") != null);++}++++test "visible dump of a fixture frame shows chips, the fold header, composer, no CSI" {++ var m: Model = undefined;++ m.setup(std.testing.allocator);++ defer m.deinit();++ try m.push(.user, "@[/tmp/shot.png] look");++ try m.pushTool(.{ .name = "bash" });++ try m.pushTool(.{ .name = "bash", .detail = "ok", .done = true });++ const frame = try render(&m, std.testing.allocator, 80, 24, 0);++ defer std.testing.allocator.free(frame);++ const vis = try dump_mod.visible(std.testing.allocator, frame);++ defer std.testing.allocator.free(vis);++ try std.testing.expect(std.mem.indexOf(u8, vis, "[Image #1]") != null);++ try std.testing.expect(std.mem.indexOf(u8, vis, "Ran bash") != null);++ try std.testing.expect(std.mem.indexOf(u8, vis, "╭") != null);++ try std.testing.expect(std.mem.indexOf(u8, vis, "›") != null);++ try std.testing.expect(std.mem.indexOf(u8, vis, "\x1b") == null);++ const lay = try dump_mod.layout(std.testing.allocator, &m);++ defer std.testing.allocator.free(lay);++ try std.testing.expect(std.mem.indexOf(u8, lay, "overlay none") != null);++ try std.testing.expect(std.mem.indexOf(u8, lay, "prompt-origin") != null);++ try std.testing.expect(std.mem.indexOf(u8, lay, "mid-origin") != null);++ try std.testing.expect(std.mem.indexOf(u8, lay, "images") != null);++++ m.openOverlay(.debug);++ // The overlay body is built from many small pieces now that it is framed++ // (panel.zig), so it wants the arena every other chrome builder gets.++ var oarena = std.heap.ArenaAllocator.init(std.testing.allocator);++ defer oarena.deinit();++ const dbg = try chrome.overlay(&m, oarena.allocator(), 80);++ const lay_dbg = try dump_mod.layout(std.testing.allocator, &m);++ defer std.testing.allocator.free(lay_dbg);++ try std.testing.expect(std.mem.indexOf(u8, lay_dbg, "overlay debug") != null);++ try std.testing.expect(std.mem.indexOf(u8, dbg, "Observability") != null);++ if (std.c.getenv("GRAFF_TUI_DUMP")) |dir_z| {++ const dir = std.mem.span(dir_z);++ const io = std.Io.Threaded.global_single_threaded.io();++ var fb: [512]u8 = undefined;++ var lb: [512]u8 = undefined;++ const fp = std.fmt.bufPrint(&fb, "{s}/tui-frame.txt", .{dir}) catch return error.PathTooLong;++ const lp = std.fmt.bufPrint(&lb, "{s}/tui-layout.txt", .{dir}) catch return error.PathTooLong;++ std.Io.Dir.cwd().writeFile(io, .{ .sub_path = fp, .data = vis }) catch {};++ const both = try std.fmt.allocPrint(std.testing.allocator, "--- overlay none ---\n{s}\n--- overlay debug ---\n{s}\n--- debug overlay body ---\n{s}", .{ lay, lay_dbg, dbg });++ defer std.testing.allocator.free(both);++ std.Io.Dir.cwd().writeFile(io, .{ .sub_path = lp, .data = both }) catch {};++ }++}++++test "debug overlay keeps the observability HUD and adds layout" {++ var m: Model = undefined;++ m.setup(std.testing.allocator);++ defer m.deinit();++ try m.push(.user, "@[/tmp/shot.png] look");++ try m.pushTool(.{ .name = "bash" });++ const frame = try render(&m, std.testing.allocator, 80, 24, 0);++ defer std.testing.allocator.free(frame);++ m.openOverlay(.debug);++ var oarena = std.heap.ArenaAllocator.init(std.testing.allocator);++ defer oarena.deinit();++ const text = try chrome.overlay(&m, oarena.allocator(), 80);++ try std.testing.expect(std.mem.indexOf(u8, text, "Observability") != null);++ try std.testing.expect(std.mem.indexOf(u8, text, "offline") != null);++ try std.testing.expect(std.mem.indexOf(u8, text, "overlay debug") != null);++ try std.testing.expect(std.mem.indexOf(u8, text, "focus") != null);++ try std.testing.expect(std.mem.indexOf(u8, text, "prompt-origin") != null);++ try std.testing.expect(std.mem.indexOf(u8, text, "mid-origin") != null);++ try std.testing.expect(std.mem.indexOf(u8, text, "images") != null);++ try std.testing.expect(std.mem.indexOf(u8, text, "pending") != null);++}++++test "a wheel notch reports a scroll hint; anything else about the frame refuses one" {++ const keys = @import("keys.zig");++ const a = std.testing.allocator;++ var m: Model = undefined;++ m.setup(a);++ defer m.deinit();++ try m.push(.user, "how do I frobnicate the widget?");++ var i: usize = 0;++ while (i < 60) : (i += 1)++ try m.pushFmt(.assistant, "explanation line {d}, long enough to fill the transcript", .{i});++++ // A first frame has nothing to compare against, and an idle one has not moved.++ for (0..2) |_| {++ a.free(try render(&m, a, 80, 24, 0));++ try std.testing.expect(m.paint_hint == null);++ }++++ // One wheel notch. The band slides by exactly the three content lines++ // keys.zig moved the viewport, and keeps its geometry — which is the whole++ // claim paint.zig acts on. `top` clears the sticky header: those rows are++ // chrome occluding the viewport, and a hardware scroll must not move them.++ _ = keys.handle(&m, .{ .mouse = .{ .btn = 64, .x = 1, .y = 5, .down = true } });++ a.free(try render(&m, a, 80, 24, 0));++ const h = m.paint_hint orelse return error.NoScrollHint;++ try std.testing.expectEqual(@as(isize, -3), h.delta);++ try std.testing.expectEqual(m.mid_origin + m.sticky_rows, h.top);++ try std.testing.expectEqual(@as(usize, 2), m.sticky_rows);++ try std.testing.expect(h.len >= 2 and h.top + h.len <= 24);++++ // A resize on the same frame as a scroll: every row below rewraps, so the++ // band is not the same band and nothing about it may be claimed to slide.++ keys.scrollBy(&m, 3);++ a.free(try render(&m, a, 79, 24, 0));++ try std.testing.expect(m.paint_hint == null);++++ // A live drag-selection band is anchored to SCREEN rows, so its inverse++ // video does not travel with the content a hardware scroll would move.++ a.free(try render(&m, a, 79, 24, 0));++ m.sel.active = true;++ keys.scrollBy(&m, 3);++ a.free(try render(&m, a, 79, 24, 0));++ try std.testing.expect(m.paint_hint == null);++ m.sel.active = false;++++ // Scrolling all the way to the top clamps: the viewport stops moving, and++ // a band that did not move is not a scroll.++ m.scroll = 1_000_000;++ a.free(try render(&m, a, 79, 24, 0));++ a.free(try render(&m, a, 79, 24, 0));++ try std.testing.expect(m.paint_hint == null);++++ // An overlay replaces the whole viewport — no band at all, so no hint even++ // though the frame changed completely.++ m.openOverlay(.help);++ a.free(try render(&m, a, 79, 24, 0));++ try std.testing.expect(m.paint_hint == null);++}++++test "the sticky header never pins a prompt over an overlay" {++ var m: Model = undefined;++ m.setup(std.testing.allocator);++ defer m.deinit();++ try m.push(.user, "SECRETPROMPT about widgets");++ var i: usize = 0;++ while (i < 40) : (i += 1) try m.push(.assistant, "filler assistant prose to make the transcript long");++ // It engages on the transcript...++ const transcript = try render(&m, std.testing.allocator, 80, 24, 0);++ std.testing.allocator.free(transcript);++ try std.testing.expectEqual(@as(usize, 2), m.sticky_rows);++ // ...and stands down once `mid` is an overlay body long enough to scroll:++ // ungated, the pin ate the overlay's own first two rows.++ m.openOverlay(.help);++ const overlay_frame = try render(&m, std.testing.allocator, 80, 24, 0);++ defer std.testing.allocator.free(overlay_frame);++ try std.testing.expectEqual(@as(usize, 0), m.sticky_rows);++ try std.testing.expect(std.mem.indexOf(u8, overlay_frame, "SECRETPROMPT") == null);++ // The image overlay is a card, not a body: the transcript keeps its pin.++ m.closeOverlay();++ m.preview_path = "/tmp/shot.png";++ m.preview_n = 1;++ m.openOverlay(.image);++ const card_frame = try render(&m, std.testing.allocator, 80, 24, 0);++ defer std.testing.allocator.free(card_frame);++ try std.testing.expectEqual(@as(usize, 2), m.sticky_rows);++}++++test "sticky header pins the last scrolled-past user prompt with a blank separator" {++ var m: Model = undefined;++ m.setup(std.testing.allocator);++ defer m.deinit();++ try m.push(.user, "how do I frobnicate the widget?");++ var i: usize = 0;++ while (i < 40) : (i += 1) try m.push(.assistant, "a long explanation line that fills the scrollback with content");++ // follow-mode bottom: prompt is far above, so it pins.++ const frame = try render(&m, std.testing.allocator, 80, 24, 0);++ defer std.testing.allocator.free(frame);++ var it = std.mem.splitScalar(u8, frame, '\n');++ var row: usize = 0;++ var pin_row: ?usize = null;++ while (it.next()) |ln| : (row += 1) {++ if (std.mem.indexOf(u8, ln, "\u{276F} ") != null and std.mem.indexOf(u8, ln, "frobnicate") != null) {++ pin_row = row;++ break;++ }++ }++ try std.testing.expect(pin_row != null);++ // scrolled fully back to the top, the prompt is inline: no pin.++ m.follow = false;++ m.scroll = 100000;++ const top_frame = try render(&m, std.testing.allocator, 80, 24, 0);++ defer std.testing.allocator.free(top_frame);++ try std.testing.expect(std.mem.indexOf(u8, top_frame, "\u{276F} ") == null);++}