Skip to content

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

Python

merge: polish panels + provider-aware model picker

Rach Pradhan7f61ef94f8fb
diff --cc TUI/chrome.zig
index 16620085,96a63a92..f88257ae
--- a/TUI/chrome.zig
+++ b/TUI/chrome.zig
@@@ -270,13 -352,91 +270,39 @@@ test "status paints coral [stop] while
try std.testing.expect(std.mem.indexOf(u8, text, "Esc:cancel") != null);
}
- pub const splitModels = @import("models.zig").splitModels;
-
+/// The overlay bodies live in overlaypane.zig — they are the panel's business,
+/// not the composer's, and keeping both here put this file over its ceiling.
+/// Re-exported so the frame composer still has one door to the chrome.
+pub const overlay = @import("overlaypane.zig").overlay;
+
+ test "model overlay lists engine.g_model_entries with their providers" {
+ 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(std.testing.allocator);
+ defer m.deinit();
+ m.openOverlay(.model);
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ const text = try overlay(&m, arena.allocator(), 80);
+ try std.testing.expect(std.mem.indexOf(u8, text, "grok-4") != null);
+ try std.testing.expect(std.mem.indexOf(u8, text, "gpt-5.5") != null);
- try std.testing.expect(std.mem.indexOf(u8, text, "Model ›") != null);
++ // The name and the tally ride in the panel's TOP EDGE now, not in a body row.
++ try std.testing.expect(std.mem.indexOf(u8, text, "Model \u{203A}") != null);
+ // The blind spot this fixes: the provider was nowhere on the surface.
- try std.testing.expect(std.mem.indexOf(u8, text, "xai · plan") != null);
- try std.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(std.testing.allocator);
- defer m.deinit();
- m.openOverlay(.debug);
- const text = try overlay(&m, std.testing.allocator, 80);
- defer std.testing.allocator.free(text);
- try std.testing.expect(std.mem.indexOf(u8, text, "offline") == null);
- try std.testing.expect(std.mem.indexOf(u8, text, "usage") != null);
- try std.testing.expect(std.mem.indexOf(u8, text, "overlay debug") != null);
- try std.testing.expect(std.mem.indexOf(u8, text, "prompt-origin") != null);
-}
-
-fn settingsOverlay(self: *const Model, a: std.mem.Allocator) ![]const u8 {
- const th = self.theme();
- var out = std.array_list.Managed(u8).init(a);
- try out.appendSlice(try theme_mod.paint(a, th.accent, "Settings"));
- try out.appendSlice("\n\n");
- 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');
- }
- try out.append('\n');
- try out.appendSlice(try theme_mod.paint(a, th.muted, "Enter change · type /model to search · Esc"));
- try out.append('\n');
- return out.items;
-}
-
-fn debugOverlay(self: *const Model, a: std.mem.Allocator) ![]const u8 {
- const th = self.theme();
- var buf: [2048]u8 = undefined;
- const body: []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);
- return std.fmt.allocPrint(a, "{s}{s}Observability{s}\n{s}{s}{s}\n{s}Esc close{s}\n", .{
- theme_mod.bold, th.text, theme_mod.reset, th.text, body, lay, th.muted, theme_mod.reset,
- });
++ try std.testing.expect(std.mem.indexOf(u8, text, "xai \u{B7} plan") != null);
++ try std.testing.expect(std.mem.indexOf(u8, text, "openai \u{B7} api") != null);
+ }
+
test "the composer footer holds its columns as the context meter ticks" {
// The footer label is CENTRED, so a share that widened from 9% to 10%
// shifted every character of it one column mid-turn. Same class of defect
diff --cc TUI/models.zig
index 23c2b865,63134af9..1c0a7309
--- a/TUI/models.zig
+++ b/TUI/models.zig
@@@ -9,9 -8,9 +9,11 @@@ const theme_mod = @import("theme.zig")
const Model = app.Model;
pub const max_models = 256;
--const visible_rows = 14;
++/// Rows the picker draws at once. Public so the click map (overlays.rowSpan)
++/// reads the same window the renderer draws rather than a second copy of it.
++pub const visible_rows = 14;
--/// Case-insensitive substring, else fzf-style subsequence ("gpt56" → gpt-5.6).
++/// Case-insensitive substring, else fzf-style subsequence ("gpt56" -> gpt-5.6).
pub fn modelMatch(name: []const u8, query: []const u8) bool {
if (query.len == 0) return true;
if (containsIgnoreCase(name, query)) return true;
@@@ -47,51 -51,81 +54,92 @@@ pub fn filterModels(list: []const engin
return n;
}
- /// Unfiltered split (empty query). Kept so callers that do not search still work.
- pub fn splitModels(list: []const u8, out: [][]const u8) usize {
- return filterModels(list, "", out);
+ /// The row that is live right now: name AND provider, because the same name
+ /// under two providers is two different seats and only one of them is current.
+ fn isCurrent(e: engine.ModelEntry) bool {
+ if (!std.mem.eql(u8, e.name, engine.g_model_name)) return false;
+ if (engine.g_model_provider.len == 0 or e.provider.len == 0) return true;
+ return std.mem.eql(u8, e.provider, engine.g_model_provider);
+ }
+
-/// Display columns, not bytes — model ids are ASCII today, but a padded byte
++/// Display columns, not bytes - model ids are ASCII today, but a padded byte
+ /// count would silently skew the provider column the day one is not.
+ fn cellWidth(text: []const u8) usize {
+ var n: usize = 0;
+ for (text) |c| {
+ if (c & 0xC0 != 0x80) n += 1;
+ }
+ return n;
+ }
+
+ /// Column width for the model names in the visible window: wide enough that
+ /// the provider column lines up, never wider than the pane can hold.
+ fn nameColumn(rows: []const engine.ModelEntry, term_width: usize) usize {
+ const cap = if (term_width > 46) @min(term_width - 34, 40) else 12;
+ var w: usize = 12;
+ for (rows) |e| w = @max(w, cellWidth(tailId(e.name)));
+ return @min(w, cap);
}
-pub fn render(self: *const Model, a: std.mem.Allocator) ![]const u8 {
- const th = self.theme();
+/// What the panel's top edge says: the live filter, and how much of the
+/// catalogue survives it. The picker owns these strings; the frame only places
+/// them (overlaypane.zig).
+pub fn head(self: *const Model, a: std.mem.Allocator) !panel.Head {
- var names: [max_models][]const u8 = undefined;
- const total = splitModels(engine.g_models, &names);
- const n = filterModels(engine.g_models, self.overlay_filter, &names);
+ var rows: [max_models]engine.ModelEntry = undefined;
+ const all = engine.g_model_entries;
+ const total = @min(all.len, max_models);
+ const n = filterModels(all, self.overlay_filter, &rows);
- var out = std.array_list.Managed(u8).init(a);
+ return .{
- .title = try std.fmt.allocPrint(a, "Model › {s}\u{258B}", .{self.overlay_filter}),
++ .title = try std.fmt.allocPrint(a, "Model \u{203A} {s}\u{258B}", .{self.overlay_filter}),
+ .note = try std.fmt.allocPrint(a, "{d}/{d}", .{ n, total }),
+ };
+}
- pub const hint = "type to search · ↑↓ move · Enter pick · Esc";
- const q = self.overlay_filter;
- const title = try std.fmt.allocPrint(a, "Model › {s}▋", .{q});
- 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");
++pub const hint = "type to search (name or provider) \u{B7} \u{2191}\u{2193} move \u{B7} click or Enter picks \u{B7} Esc \u{B7} \u{2014} no key";
+
+/// The ROWS only. Title, tally and keys ride in the panel's edges.
++///
++/// One catalog row is exactly one LINE: the panel clips each to its inner
++/// width rather than wrapping, so the click map (overlays.rowSpan) can stay
++/// arithmetic. A wrapped row would slide every row below it off its own line.
+pub fn render(self: *const Model, a: std.mem.Allocator) ![]const u8 {
+ const th = self.theme();
- var names: [max_models][]const u8 = undefined;
- const n = filterModels(engine.g_models, self.overlay_filter, &names);
++ var rows: [max_models]engine.ModelEntry = undefined;
++ const n = filterModels(engine.g_model_entries, self.overlay_filter, &rows);
+ var out = std.array_list.Managed(u8).init(a);
if (n == 0) {
- try out.appendSlice(try theme_mod.paint(a, th.muted, " no matches — type to filter\n"));
- try out.appendSlice(try theme_mod.paint(a, th.muted, "no matches — type to filter, Esc to close\n"));
++ try out.appendSlice(try theme_mod.paint(a, th.muted, " no matches \u{2014} type to filter"));
++ try out.append('\n');
return out.items;
}
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);
var i = off;
while (i < n and i < off + vis) : (i += 1) {
- const mark: []const u8 = if (i == sel) "› " else " ";
- const cur: []const u8 = if (std.mem.eql(u8, names[i], engine.g_model_name)) " (current)" else "";
- const shown = tailId(names[i]);
- const line = try std.fmt.allocPrint(a, "{s}{s}{s}", .{ mark, shown, cur });
- try out.appendSlice(if (i == sel) try theme_mod.paint(a, th.accent, line) else try theme_mod.paint(a, th.muted, line));
+ const e = rows[i];
- try out.appendSlice(if (i == sel) "› " else " ");
++ try out.appendSlice(if (i == sel) "\u{203A} " else " ");
+ const shown = tailId(e.name);
- // A seat with no credential keeps its row — the catalog is the map —
++ // A seat with no credential keeps its row - the catalog is the map -
+ // but reads as unavailable rather than as one more thing to pick.
+ const dim: []const u8 = if (e.has_key) "" else theme_mod.dim;
+ const name_sgr = try std.fmt.allocPrint(a, "{s}{s}", .{ dim, if (i == sel) th.accent else th.text });
+ try out.appendSlice(try theme_mod.paint(a, name_sgr, shown));
+ try out.appendNTimes(' ', col -| cellWidth(shown));
- // provider · cost · key — the three facts a bare name hid.
- const meta = try std.fmt.allocPrint(a, " {s} · {s} {s}{s}", .{
++ // provider - cost - key: the three facts a bare name hid.
++ const meta = try std.fmt.allocPrint(a, " {s} \u{B7} {s} {s}{s}", .{
+ e.provider,
+ e.cost.badge(),
- if (e.has_key) "✓" else "—",
++ if (e.has_key) "\u{2713}" else "\u{2014}",
+ if (isCurrent(e)) " (current)" else "",
+ });
+ try out.appendSlice(try theme_mod.paint(a, try std.fmt.allocPrint(a, "{s}{s}", .{ dim, th.muted }), meta));
try out.append('\n');
}
- try out.append('\n');
- try out.appendSlice(try theme_mod.paint(a, th.muted, "type to search (name or provider) · ↑↓ move · Enter pick · Esc · — no key"));
- try out.append('\n');
+ if (n > vis) try out.appendSlice(try panel.windowRow(a, th, off, vis, n));
return out.items;
}
@@@ -110,13 -144,32 +158,32 @@@ test "modelMatch: substring and subsequ
try std.testing.expect(!modelMatch("gpt-5.6", "claude"));
}
- test "filterModels ranks by query, not the first 32 only" {
- const list = "aaa, bbb, ccc, accounts/fireworks/models/deepseek-v4-pro, gpt-5.6";
- var buf: [8][]const u8 = undefined;
- const n = filterModels(list, "deepseek", &buf);
+ const sample = [_]engine.ModelEntry{
+ .{ .name = "aaa", .provider = "openai", .has_key = true, .cost = .api },
+ .{ .name = "bbb", .provider = "codegraff", .has_key = true, .cost = .credits },
+ .{ .name = "ccc", .provider = "codex", .cost = .plan },
+ .{ .name = "accounts/fireworks/models/deepseek-v4-pro", .provider = "fireworks", .has_key = true, .cost = .api },
+ .{ .name = "gpt-5.6", .provider = "codex", .has_key = true, .cost = .plan },
+ };
+
+ test "filterModels keeps whole catalog rows, not names" {
+ var buf: [8]engine.ModelEntry = undefined;
+ const n = filterModels(&sample, "deepseek", &buf);
try std.testing.expectEqual(@as(usize, 1), n);
- try std.testing.expectEqualStrings("accounts/fireworks/models/deepseek-v4-pro", buf[0]);
- try std.testing.expectEqual(@as(usize, 5), filterModels(list, "", &buf));
+ try std.testing.expectEqualStrings("accounts/fireworks/models/deepseek-v4-pro", buf[0].name);
+ try std.testing.expectEqualStrings("fireworks", buf[0].provider);
+ try std.testing.expectEqual(@as(usize, 5), filterModels(&sample, "", &buf));
+ }
+
+ test "the query searches the provider column too" {
- // "show me what my plan serves" — impossible while rows were name-only.
++ // "show me what my plan serves" - impossible while rows were name-only.
+ var buf: [8]engine.ModelEntry = undefined;
+ const n = filterModels(&sample, "codex", &buf);
+ try std.testing.expectEqual(@as(usize, 2), n);
+ try std.testing.expectEqualStrings("ccc", buf[0].name);
+ try std.testing.expectEqualStrings("gpt-5.6", buf[1].name);
+ try std.testing.expectEqual(@as(usize, 1), filterModels(&sample, "codegraff", &buf));
+ try std.testing.expectEqual(@as(usize, 0), filterModels(&sample, "anthropic", &buf));
}
test "model overlay typed query is a filtered list, not the dump" {
@@@ -135,8 -188,93 +202,95 @@@
defer arena.deinit();
const text = try render(&m, arena.allocator());
try std.testing.expect(std.mem.indexOf(u8, text, "deepseek-v4-pro") != null);
- try std.testing.expect(std.mem.indexOf(u8, text, "gpt-5.5") == null);
+ try std.testing.expect(std.mem.indexOf(u8, text, "gpt-5.6") == null);
- try std.testing.expect(std.mem.indexOf(u8, text, "1/5") != null);
- try std.testing.expect(std.mem.indexOf(u8, text, "Model › deepseek") != null);
++ // The tally and the live filter moved into the panel's top edge.
+ const hd = try head(&m, arena.allocator());
- try std.testing.expectEqualStrings("1/3", hd.note);
- try std.testing.expect(std.mem.indexOf(u8, hd.title, "Model › deepseek") != null);
++ try std.testing.expectEqualStrings("1/5", hd.note);
++ try std.testing.expect(std.mem.indexOf(u8, hd.title, "Model \u{203A} deepseek") != null);
+ }
+
+ test "picker rows carry the provider, a cost badge and a key marker" {
+ // The reported bug: two seats for the same model rendered identically, so
+ // "codex (free) vs codegraff (paid) vs openai (paid)" was invisible.
+ const dupes = [_]engine.ModelEntry{
+ .{ .name = "gpt-5.6", .provider = "codex", .has_key = true, .cost = .plan },
+ .{ .name = "gpt-5.6", .provider = "openai", .has_key = true, .cost = .api },
+ .{ .name = "gpt-5.6", .provider = "codegraff", .cost = .credits },
+ };
+ engine.g_model_entries = &dupes;
+ engine.g_model_name = "gpt-5.6";
+ engine.g_model_provider = "openai";
+ defer {
+ engine.g_model_entries = &.{};
+ engine.g_model_name = "";
+ engine.g_model_provider = "";
+ }
+ var m: Model = undefined;
+ m.setup(std.testing.allocator);
+ defer m.deinit();
+ m.openOverlay(.model);
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ const text = try render(&m, arena.allocator());
- // provider · cost · key, per row — the keyless seat is marked, not hidden.
- for ([_][]const u8{ "codex · plan ✓", "openai · api ✓", "codegraff · credits —" }) |want|
++ // provider, cost and key per row - the keyless seat is marked, not hidden.
++ for ([_][]const u8{ "codex \u{B7} plan \u{2713}", "openai \u{B7} api \u{2713}", "codegraff \u{B7} credits \u{2014}" }) |want|
+ try std.testing.expect(std.mem.indexOf(u8, text, want) != null);
- // Exactly one row is current, and it is the one whose PROVIDER matches —
++ // Exactly one row is current, and it is the one whose PROVIDER matches -
+ // by name alone all three would have claimed it.
+ try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, text, "(current)"));
+ const cur = std.mem.indexOf(u8, text, "(current)").?;
- const openai = std.mem.indexOf(u8, text, "openai · api").?;
++ const openai = std.mem.indexOf(u8, text, "openai \u{B7} api").?;
+ try std.testing.expect(openai < cur and cur - openai < 24);
+ // ...and that row is the only dimmed one.
+ try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, text, theme_mod.dim));
+ }
+
+ test "the provider column lines up under ragged model names" {
+ const ragged = [_]engine.ModelEntry{
+ .{ .name = "k3", .provider = "kimi", .has_key = true, .cost = .plan },
+ .{ .name = "claude-opus-4-8", .provider = "anthropic", .has_key = true, .cost = .api },
+ };
+ engine.g_model_entries = &ragged;
+ defer engine.g_model_entries = &.{};
+ var m: Model = undefined;
+ m.setup(std.testing.allocator);
+ defer m.deinit();
+ m.openOverlay(.model);
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ const text = try render(&m, arena.allocator());
+ var col: ?usize = null;
+ var seen: usize = 0;
+ var it = std.mem.splitScalar(u8, text, '\n');
+ var buf: [512]u8 = undefined;
+ while (it.next()) |line| {
+ const plain = strip(line, &buf);
+ // Only the catalog rows: they alone open with the selection gutter.
- if (!std.mem.startsWith(u8, plain, " ") and !std.mem.startsWith(u8, plain, "› ")) continue;
- const sep = std.mem.indexOf(u8, plain, " · ") orelse continue;
++ if (!std.mem.startsWith(u8, plain, " ") and !std.mem.startsWith(u8, plain, "\u{203A} ")) continue;
++ const sep = std.mem.indexOf(u8, plain, " \u{B7} ") orelse continue;
+ var start = sep;
+ while (start > 0 and plain[start - 1] != ' ') start -= 1;
+ const at = cellWidth(plain[0..start]);
+ if (col) |c| try std.testing.expectEqual(c, at) else col = at;
+ seen += 1;
+ }
+ try std.testing.expectEqual(@as(usize, 2), seen);
+ }
+
+ /// `line` with its SGR sequences removed, written into `buf`. The padding is
+ /// what is under test and an escape sequence is zero cells wide.
+ fn strip(line: []const u8, buf: []u8) []const u8 {
+ var n: usize = 0;
+ var i: usize = 0;
+ while (i < line.len and n < buf.len) {
+ if (line[i] == 0x1b) {
+ while (i < line.len and line[i] != 'm') i += 1;
+ i += 1;
+ continue;
+ }
+ buf[n] = line[i];
+ n += 1;
+ i += 1;
+ }
+ return buf[0..n];
}
diff --cc TUI/overlaypane.zig
index 9386bcec,00000000..0864dba2
mode 100644,000000..100644
--- a/TUI/overlaypane.zig
+++ b/TUI/overlaypane.zig
@@@ -1,407 -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",
+ .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",
+ .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",
+ .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",
+ .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 engine.g_models" {
- engine.g_models = "grok-4, gpt-5.5";
++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_models = "";
++ 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_models = "grok-4, gpt-5.5";
- defer engine.g_models = "";
++ 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(u8).init(arena0.allocator());
++ var buf = std.array_list.Managed(engine.ModelEntry).init(arena0.allocator());
+ for (0..75) |i| {
- if (i > 0) try buf.appendSlice(", ");
- try buf.appendSlice(try std.fmt.allocPrint(arena0.allocator(), "model-{d}", .{i}));
++ try buf.append(.{
++ .name = try std.fmt.allocPrint(arena0.allocator(), "model-{d}", .{i}),
++ .provider = "openai",
++ .has_key = true,
++ .cost = .api,
++ });
+ }
- engine.g_models = buf.items;
- defer engine.g_models = "";
++ 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 scripts/eval-tier1.sh
index 9552ae5e,b82bfa0c..3e4d98a4
--- a/scripts/eval-tier1.sh
+++ b/scripts/eval-tier1.sh
@@@ -326,15 -326,14 +326,23 @@@ if wanted tuiguard; the
if python3 scripts/test-tui-scrollbar-osc52.py zig-out/bin/graff; then :; else
record_fail tuiguard
fi
+ # Every overlay is a bordered panel with its name in the top rule, docked to
+ # the composer, opened at its FIRST row; and the composer no longer claims a
+ # clipboard image on every idle frame. Border cells and the gap between the
+ # panel and the input box are geometry a unit test cannot see. Each of the
+ # four checks is verified: unframing the panel, undocking it, opening a
+ # sheet at its tail, or restoring the clipboard row each fails its own.
+ if python3 scripts/test-tui-overlay-panels.py zig-out/bin/graff; then :; else
+ record_fail tuiguard
+ fi
+ # #560: the picker has to say WHO serves each model. Reads the open overlay
+ # off the virtual screen and checks that every row carries a provider and a
+ # cost badge, that gpt-5.5 draws one distinguishable row per provider with
+ # the right badge on each (codex plan / openai api / codegraff credits),
+ # and that typing a provider name filters to that provider's seats.
+ if python3 scripts/test-tui-model-picker.py zig-out/bin/graff; then :; else
+ record_fail tuiguard
+ fi
fi
fi