graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix: TUI /resume lists saved sessions and accepts a click (#719)
Rach Pradhan294ff1090894
diff --git a/TUI/app.zig b/TUI/app.zigindex dbbda914..1eff96e6 100644--- a/TUI/app.zig+++ b/TUI/app.zig@@ -9,7 +9,7 @@ const theme_mod = @import("theme.zig");pub const Screen = enum { welcome, agent };pub const Focus = enum { prompt, scrollback };-pub const Overlay = enum { none, palette, help, theme, model, effort, settings, rewind, slash, debug, image, file, jump };+pub const Overlay = enum { none, palette, help, theme, model, effort, settings, rewind, slash, debug, image, file, jump, sessions };/// One vocabulary with the engine (#551): the Model does not keep a private/// copy of the permission policy, it holds the engine's own enum and hands it/// straight to the turn backend.@@ -178,6 +178,7 @@ pub const Model = struct {hist_idx: ?usize = null,/// Newline-joined paths for the @-file picker, loaded once per session.files_cache: ?[]const u8 = null,+ sessions_cache: ?[]const u8 = null,toast: []const u8 = "",toast_until_ms: u64 = 0,@@ -256,6 +257,7 @@ pub const Model = struct {}if (self.overlay_filter.len > 0) self.alloc.free(self.overlay_filter);if (self.files_cache) |f| self.alloc.free(f);+ if (self.sessions_cache) |s| self.alloc.free(s);if (self.sel_text.len > 0) self.alloc.free(self.sel_text);if (self.osc_pending.len > 0) self.alloc.free(self.osc_pending);self.input.deinit();diff --git a/TUI/click_tests.zig b/TUI/click_tests.zigindex 6db5c599..da6389eb 100644--- a/TUI/click_tests.zig+++ b/TUI/click_tests.zig@@ -473,6 +473,15 @@ test "the click map names the rows the settings, theme and jump panels drew" {try checkSpan(&m, 80, &.{ "first turn", "second turn" });}+test "the click map names the rows the resume picker drew" {+ var m: Model = undefined;+ m.setup(alloc);+ defer m.deinit();+ m.sessions_cache = try alloc.dupe(u8, "alpha\tAlpha title\t2h ago\nbeta\tbeta\tjust now");+ m.openOverlay(.sessions);+ try checkSpan(&m, 80, &.{ "Alpha title", "beta" });+}+test "the click map names the rows the command palette drew" {var m: Model = undefined;m.setup(alloc);diff --git a/TUI/dispatch.zig b/TUI/dispatch.zigindex c8cfd2c6..b9c55b63 100644--- a/TUI/dispatch.zig+++ b/TUI/dispatch.zig@@ -111,7 +111,7 @@ pub fn runCommand(self: *Model, line: []const u8) Effect {self.push(.system, "started a new conversation") catch {};self.screen = .welcome;} else if (std.mem.eql(u8, canon, "/resume")) {- resume_mod.run(self, arg);+ if (arg.len == 0) resume_mod.open(self) else resume_mod.run(self, arg);} else if (std.mem.eql(u8, canon, "/home")) {self.screen = .welcome;self.focus = .prompt;diff --git a/TUI/engine.zig b/TUI/engine.zigindex 5bbe84eb..4ebc8771 100644--- a/TUI/engine.zig+++ b/TUI/engine.zig@@ -177,6 +177,8 @@ pub const ResumeOut = struct {note: []const u8 = "",};pub const ResumeFn = *const fn (turn_ctx: ?*anyopaque, gpa: std.mem.Allocator, spec: []const u8, out: *ResumeOut) bool;+/// Newline-joined saved-session rows (`base\tlabel\tdesc`) for `/resume` (caller frees), or null.+pub const SessionsFn = *const fn (turn_ctx: ?*anyopaque, gpa: std.mem.Allocator) ?[]const u8;pub const Job = struct {thread: std.Thread = undefined,@@ -285,6 +287,7 @@ pub const RunOpts = struct {compact_fn: ?CompactFn = null,history_fn: ?HistoryFn = null,resume_fn: ?ResumeFn = null,+ sessions_fn: ?SessionsFn = null,state_fn: ?StateFn = null,emergency_fn: ?*const fn (turn_ctx: ?*anyopaque) void = null,idle_wake_fn: ?IdleWakeFn = null,@@ -303,6 +306,7 @@ pub var g_copy_fn: ?CopyFn = null;pub var g_compact_fn: ?CompactFn = null;pub var g_history_fn: ?HistoryFn = null;pub var g_resume_fn: ?ResumeFn = null;+pub var g_sessions_fn: ?SessionsFn = null;pub var g_state_fn: ?StateFn = null;/// Tell the engine the transcript was cut. Silent when nothing is wireddiff --git a/TUI/overlaypane.zig b/TUI/overlaypane.zigindex 77e8e393..08ab854b 100644--- a/TUI/overlaypane.zig+++ b/TUI/overlaypane.zig@@ -93,6 +93,11 @@ fn spec(self: *const Model, a: std.mem.Allocator) !panel.Spec {.footer = "↑↓ move · click or Enter jumps · Esc",.body = try jumpBody(self, a),},+ .sessions => blk: {+ const resume_mod = @import("resume.zig");+ const head = try resume_mod.head(self, a);+ break :blk .{ .title = head.title, .note = head.note, .footer = resume_mod.hint, .body = try resume_mod.render(self, a) };+ },else => .{ .body = "" },};}@@ -254,6 +259,7 @@ const help_body =\\ /always-approve skip permission prompts\\ /import-claude copy Claude and Cursor MCP + skills\\ /jump jump to a previous turn+ \\ /resume list saved sessions\\ /copy copy the last reply\\ /btw queue an aside mid-turn\\ /tell message a live graff (/tell all broadcasts)@@ -272,8 +278,8 @@ test "help overlay names the advertised pager commands" {defer arena.deinit();const text = try overlay(&m, arena.allocator(), 80);for ([_][]const u8{- "/quit", "/help", "/new", "/home", "/model", "/settings", "/usage", "/debug", "/cache", "/plan", "/always-approve",- "/tell", "/peek", "Shift+Tab", "PgUp", "PgDn",+ "/quit", "/help", "/new", "/home", "/model", "/settings", "/usage", "/debug", "/cache", "/plan", "/always-approve",+ "/tell", "/peek", "/resume", "Shift+Tab", "PgUp", "PgDn","←","→","Tab", "Enter", "Esc",@@ -356,6 +362,7 @@ test "every overlay is a bordered panel with its name in the frame" {.{ .o = .file, .name = "File" },.{ .o = .settings, .name = "Settings" },.{ .o = .jump, .name = "Jump to turn" },+ .{ .o = .sessions, .name = "Resume session" },};for (cases) |c| {for ([_]usize{ 40, 60, 80, 120 }) |w| {diff --git a/TUI/overlays.zig b/TUI/overlays.zigindex c406409d..df7d18f3 100644--- a/TUI/overlays.zig+++ b/TUI/overlays.zig@@ -1,5 +1,5 @@//! Overlay key routing + activation (palette, theme, model, effort, settings,-//! rewind, @-file picker, /jump). Split from keys.zig for the line budget.+//! rewind, @-file picker, /jump, /resume). Split from keys.zig for the line budget.const std = @import("std");@@ -57,7 +57,7 @@ pub fn key(self: *Model, k: Key) Effect {return .stay;}if (k == .enter) return activate(self);- if (self.overlay == .model or self.overlay == .effort or self.overlay == .file) {+ if (self.overlay == .model or self.overlay == .effort or self.overlay == .file or self.overlay == .sessions) {switch (k) {.char => |c| self.typeOverlayFilter(c),.backspace => self.backspaceOverlayFilter(),@@ -169,6 +169,13 @@ pub fn rowSpan(self: *const Model) ?Span {if (n == 0) return null;return windowed(frame_rows, n, self.overlay_sel % n, files_mod.visible_rows);},+ .sessions => {+ const resume_mod = @import("resume.zig");+ var rows: [resume_mod.max_sessions]resume_mod.Row = undefined;+ const n = resume_mod.filterList(self.sessions_cache orelse "", self.overlay_filter, &rows);+ if (n == 0) return null;+ return windowed(frame_rows, n, self.overlay_sel % n, resume_mod.visible_rows);+ },else => return null,}}@@ -295,6 +302,12 @@ pub fn activate(self: *Model) Effect {seen += 1;}},+ .sessions => {+ const resume_mod = @import("resume.zig");+ const base = resume_mod.pick(self);+ self.closeOverlay();+ if (base) |b| resume_mod.run(self, b);+ },else => self.closeOverlay(),}return .stay;diff --git a/TUI/resume.zig b/TUI/resume.zigindex 0192a67d..e5519750 100644--- a/TUI/resume.zig+++ b/TUI/resume.zig@@ -1,12 +1,109 @@-//! Fullscreen `/resume SOURCE [--branch DEST]` projection.+//! Fullscreen `/resume` — session list overlay, plus `/resume SOURCE [--branch DEST]`.const std = @import("std");const app = @import("app.zig");const engine = @import("engine.zig");+const glyphs = @import("glyphs.zig");+const models = @import("models.zig");+const panel = @import("panel.zig");+const theme_mod = @import("theme.zig");+const Model = app.Model;++pub const max_sessions = 512;+pub const visible_rows = 10;+pub const hint = "type to search · ↑↓ move · click or Enter resumes · Esc";++pub const Row = struct { base: []const u8, label: []const u8, desc: []const u8 };++fn parseRow(raw: []const u8) Row {+ const t1 = std.mem.indexOfScalar(u8, raw, '\t') orelse return .{ .base = raw, .label = raw, .desc = "" };+ const base = raw[0..t1];+ const rest = raw[t1 + 1 ..];+ const t2 = std.mem.indexOfScalar(u8, rest, '\t') orelse return .{ .base = base, .label = rest, .desc = "" };+ const label = rest[0..t2];+ return .{ .base = base, .label = if (label.len > 0) label else base, .desc = rest[t2 + 1 ..] };+}++pub fn filterList(list: []const u8, query: []const u8, out: []Row) usize {+ var n: usize = 0;+ var it = std.mem.splitScalar(u8, list, '\n');+ while (it.next()) |raw| {+ const line = std.mem.trim(u8, raw, " \t\r");+ if (line.len == 0) continue;+ const row = parseRow(line);+ if (!models.modelMatch(row.base, query) and !models.modelMatch(row.label, query) and !models.modelMatch(row.desc, query)) continue;+ if (n >= out.len) break;+ out[n] = row;+ n += 1;+ }+ return n;+}++pub fn pick(self: *const Model) ?[]const u8 {+ var rows: [max_sessions]Row = undefined;+ const n = filterList(self.sessions_cache orelse "", self.overlay_filter, &rows);+ if (n == 0) return null;+ return rows[self.overlay_sel % n].base;+}++/// Bare `/resume`: load the saved-session list and open the picker.+pub fn open(self: *Model) void {+ if (self.sessions_cache) |old| {+ self.alloc.free(old);+ self.sessions_cache = null;+ }+ if (engine.g_sessions_fn) |f| self.sessions_cache = f(engine.g_turn_ctx, self.alloc);+ self.openOverlay(.sessions);+}++pub fn head(self: *const Model, a: std.mem.Allocator) !panel.Head {+ var rows: [max_sessions]Row = undefined;+ const list = self.sessions_cache orelse "";+ const total = filterList(list, "", &rows);+ const n = filterList(list, self.overlay_filter, &rows);+ return .{+ .title = try std.fmt.allocPrint(a, "Resume session › {s}\u{258B}", .{self.overlay_filter}),+ .note = try std.fmt.allocPrint(a, "{d}/{d}", .{ n, total }),+ };+}++pub fn render(self: *const Model, a: std.mem.Allocator) ![]const u8 {+ const th = self.theme();+ var rows: [max_sessions]Row = undefined;+ const list = self.sessions_cache orelse "";+ const n = filterList(list, self.overlay_filter, &rows);+ var out = std.array_list.Managed(u8).init(a);+ if (n == 0) {+ const why: []const u8 = if (self.sessions_cache == null)+ " resume isn't available (offline)"+ else if (self.overlay_filter.len > 0)+ " no matches — type to filter"+ else+ " no saved sessions in cwd — /save creates one";+ try out.appendSlice(try theme_mod.paint(a, th.muted, why));+ 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;+ var i = off;+ while (i < n and i < off + vis) : (i += 1) {+ const mark = glyphs.frame(&glyphs.row_mark, @intFromBool(i != sel));+ const line = if (rows[i].desc.len > 0)+ try std.fmt.allocPrint(a, "{s}{s} {s}", .{ mark, rows[i].label, rows[i].desc })+ else+ try std.fmt.allocPrint(a, "{s}{s}", .{ mark, rows[i].label });+ 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 > vis) try out.appendSlice(try panel.windowRow(a, th, off, vis, n));+ return out.items;+}pub fn run(self: *app.Model, spec: []const u8) void {if (spec.len == 0) {- self.push(.system, "usage: /resume SOURCE [--branch DEST]") catch {};+ open(self);return;}const callback = engine.g_resume_fn orelse {@@ -40,7 +137,21 @@ pub fn run(self: *app.Model, spec: []const u8) void {}}+fn fakeSessions(_: ?*anyopaque, gpa: std.mem.Allocator) ?[]const u8 {+ return gpa.dupe(u8, "alpha\tAlpha title\t2h ago\nbeta\tbeta\tjust now") catch null;+}+fn fakeResume(_: ?*anyopaque, gpa: std.mem.Allocator, spec: []const u8, out: *engine.ResumeOut) bool {+ if (std.mem.eql(u8, spec, "alpha")) {+ const turns = gpa.alloc(engine.Turn, 1) catch return false;+ turns[0] = .{ .role = .user, .text = gpa.dupe(u8, "from alpha") catch return false };+ out.* = .{+ .turns = turns,+ .session_name = gpa.dupe(u8, "alpha") catch return false,+ .note = gpa.dupe(u8, "resumed alpha") catch return false,+ };+ return true;+ }if (!std.mem.eql(u8, spec, "base --branch child")) return false;const turns = gpa.alloc(engine.Turn, 2) catch return false;turns[0] = .{ .role = .user, .text = gpa.dupe(u8, "baseline prompt") catch return false };@@ -68,3 +179,56 @@ test "fullscreen resume replaces transcript and selects the branch" {try std.testing.expectEqualStrings("baseline answer", model.history.items[1].text);try std.testing.expectEqualStrings("branched base → child", model.history.items[2].text);}++test "bare /resume opens a list overlay and Enter resumes the highlighted session" {+ engine.g_sessions_fn = fakeSessions;+ defer engine.g_sessions_fn = null;+ engine.g_resume_fn = fakeResume;+ defer engine.g_resume_fn = null;+ var m: app.Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ try m.push(.user, "stale");+ open(&m);+ try std.testing.expectEqual(app.Overlay.sessions, m.overlay);+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const text = try render(&m, arena.allocator());+ try std.testing.expect(std.mem.indexOf(u8, text, "Alpha title") != null);+ try std.testing.expect(std.mem.indexOf(u8, text, "beta") != null);+ try std.testing.expect(std.mem.indexOf(u8, text, "2h ago") != null);+ _ = @import("overlays.zig").key(&m, .enter);+ try std.testing.expectEqual(app.Overlay.none, m.overlay);+ try std.testing.expectEqualStrings("alpha", m.session_name.?);+ try std.testing.expectEqualStrings("from alpha", m.history.items[0].text);+}++test "clicking a /resume row selects then confirms" {+ engine.g_sessions_fn = fakeSessions;+ defer engine.g_sessions_fn = null;+ engine.g_resume_fn = fakeResume;+ defer engine.g_resume_fn = null;+ var term: @import("sim.zig").Term = undefined;+ term.init(std.testing.allocator, 80, 24);+ defer term.deinit();+ _ = @import("dispatch.zig").applyLine(&term.model, "/resume");+ try std.testing.expectEqual(app.Overlay.sessions, term.model.overlay);+ const vis = try term.screen();+ defer std.testing.allocator.free(vis);+ try std.testing.expect(std.mem.indexOf(u8, vis, "Alpha title") != null);+ try std.testing.expect(try term.clickText("Alpha title"));+ try std.testing.expectEqual(app.Overlay.sessions, term.model.overlay);+ try std.testing.expect(try term.clickText("Alpha title"));+ try std.testing.expectEqual(app.Overlay.none, term.model.overlay);+ try std.testing.expectEqualStrings("alpha", term.model.session_name.?);+}++test "filterList matches title, base, and subsequence" {+ var buf: [8]Row = undefined;+ const list = "alpha\tAlpha title\t2h ago\nbeta\tbeta\tjust now";+ try std.testing.expectEqual(@as(usize, 2), filterList(list, "", &buf));+ try std.testing.expectEqual(@as(usize, 1), filterList(list, "alpha", &buf));+ try std.testing.expectEqualStrings("alpha", buf[0].base);+ try std.testing.expectEqual(@as(usize, 1), filterList(list, "title", &buf));+ try std.testing.expectEqualStrings("alpha", buf[0].base);+}diff --git a/TUI/run.zig b/TUI/run.zigindex c8a1ff3c..d260a119 100644--- a/TUI/run.zig+++ b/TUI/run.zig@@ -57,6 +57,7 @@ pub fn run(engine.g_compact_fn = opts.compact_fn;engine.g_history_fn = opts.history_fn;engine.g_resume_fn = opts.resume_fn;+ engine.g_sessions_fn = opts.sessions_fn;engine.g_state_fn = opts.state_fn;engine.g_idle_wake_fn = opts.idle_wake_fn;engine.g_peer_fn = opts.peer_fn;diff --git a/src/tui_launch.zig b/src/tui_launch.zigindex d9a500c1..369661a9 100644--- a/src/tui_launch.zig+++ b/src/tui_launch.zig@@ -144,6 +144,7 @@ pub fn run(.compact_fn = compactCb,.history_fn = historyCb,.resume_fn = tui_session.resumeCb,+ .sessions_fn = tui_session.sessionsCb,.state_fn = stateCb,.emergency_fn = emergencyCb,.idle_wake_fn = idleWakeCb,diff --git a/src/tui_session.zig b/src/tui_session.zigindex 080971ce..aa9bc1e2 100644--- a/src/tui_session.zig+++ b/src/tui_session.zig@@ -94,6 +94,37 @@ pub fn resumeCb(ctx_ptr: ?*anyopaque, gpa: Allocator, raw: []const u8, out: *tuireturn true;}+pub fn sessionsCb(ctx_ptr: ?*anyopaque, gpa: Allocator) ?[]const u8 {+ const ctx: *repl_glue.ReplCtx = @ptrCast(@alignCast(ctx_ptr orelse return null));+ const root = ctx.root orelse return null;+ var arena_inst = std.heap.ArenaAllocator.init(gpa);+ defer arena_inst.deinit();+ const arena = arena_inst.allocator();+ const entries = session.listSavedSessions(root, arena);+ if (entries.items.len == 0) return gpa.dupe(u8, "") catch null;+ var out: std.ArrayList(u8) = .empty;+ for (entries.items, 0..) |e, i| {+ if (i > 0) out.append(gpa, '\n') catch {+ out.deinit(gpa);+ return null;+ };+ const age = session.sessionAge(arena, root.io, e.updated_ms);+ const title = e.title orelse e.base;+ const desc = if (e.title == null)+ age+ else if (age.len > 0)+ std.fmt.allocPrint(arena, "{s} · {s}", .{ age, e.base }) catch e.base+ else+ e.base;+ const line = std.fmt.allocPrint(arena, "{s}\t{s}\t{s}", .{ e.base, title, desc }) catch continue;+ out.appendSlice(gpa, line) catch {+ out.deinit(gpa);+ return null;+ };+ }+ return out.toOwnedSlice(gpa) catch null;+}+test "visible turns keep human text and omit provider tool envelopes" {const arena = std.testing.allocator;var messages = std.json.Array.init(arena);