graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
feat(tui): in-app drag selection — inverse band, copy on release (#529)
Rach Pradhan14db10364bda
diff --git a/TUI/app.zig b/TUI/app.zigindex 362fd506..4f785bec 100644--- a/TUI/app.zig+++ b/TUI/app.zig@@ -91,6 +91,11 @@ pub const Model = struct {preview_n: u32 = 0,preview_pin: bool = false,preview_rows: usize = 0,+ /// Drag-selection band over the composed frame (#529) — presentation only.+ sel: @import("selection.zig").Sel = .{},+ /// Text the band currently covers, captured by the same pass that paints+ /// it, so what the clipboard gets is exactly what the user saw highlighted.+ sel_text: []const u8 = "",cancel_requested: bool = false,quit_requested: bool = false,chat: bool = false,@@ -133,6 +138,7 @@ pub const Model = struct {if (self.session_name) |s| self.alloc.free(s);if (self.overlay_filter.len > 0) self.alloc.free(self.overlay_filter);if (self.files_cache) |f| self.alloc.free(f);+ if (self.sel_text.len > 0) self.alloc.free(self.sel_text);self.input.deinit();}diff --git a/TUI/chrome.zig b/TUI/chrome.zigindex e2440fed..f7c81f4c 100644--- a/TUI/chrome.zig+++ b/TUI/chrome.zig@@ -249,7 +249,8 @@ fn helpOverlay(self: *const Model, a: std.mem.Allocator) ![]const u8 {\\ Ctrl+J / K scroll one line\\ Ctrl+N N new session\\ Ctrl+Q quit- \\ Shift+drag copy (native selection)+ \\ 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 worddiff --git a/TUI/image.zig b/TUI/image.zigindex 2cff4a78..bfdcf71a 100644--- a/TUI/image.zig+++ b/TUI/image.zig@@ -302,23 +302,7 @@ fn openPath(path: []const u8) void {}fn copyPath(path: []const u8) void {- if (path.len == 0) return;- const io = ioHandle();- var child = std.process.spawn(io, .{- .argv = &.{"pbcopy"},- .stdin = .pipe,- .stdout = .ignore,- .stderr = .ignore,- }) catch return;- if (child.stdin) |*s| {- var wbuf: [4096]u8 = undefined;- var w = s.writerStreaming(io, &wbuf);- w.interface.writeAll(path) catch {};- w.interface.flush() catch {};- s.close(io);- child.stdin = null;- }- _ = child.wait(io) catch {};+ _ = @import("selection.zig").copyText(path);}pub fn attachDropped(self: *Model, raw: []const u8) bool {diff --git a/TUI/keys.zig b/TUI/keys.zigindex 8461c491..ce698e54 100644--- a/TUI/keys.zig+++ b/TUI/keys.zig@@ -6,6 +6,7 @@ const app = @import("app.zig");const catalog = @import("catalog.zig");const dispatch = @import("dispatch.zig");const scrollback = @import("scrollback.zig");+const selection = @import("selection.zig");const engine = @import("engine.zig");const key_mod = @import("key.zig");const theme_mod = @import("theme.zig");@@ -16,6 +17,9 @@ const Effect = app.Effect;pub fn handle(self: *Model, k: Key) Effect {if (k == .ignore) return .stay;+ // Anything that is not part of the drag gesture drops the selection band+ // (#529). The OSC-11 polarity reply is the terminal talking, not the user.+ if (k != .mouse and k != .bg_report) selection.clear(self);if (@import("nav.zig").handle(self, k)) |e| return e;// Job-control wins overlays so Ctrl+C always does something.if (isCtrl(k, 'c')) return ctrlC(self);@@ -212,14 +216,16 @@ fn scrollbackKey(self: *Model, k: Key) Effect {}fn mouseKey(self: *Model, ev: key_mod.Mouse) Effect {- if (ev.btn == 64) {- scrollBy(self, 3);- return .stay;- }- if (ev.btn == 65) {- scrollBy(self, -3);+ if (ev.btn == 64 or ev.btn == 65) {+ // The band is anchored to screen rows, so scrolling it would slide it+ // onto other content — drop it and scroll.+ selection.clear(self);+ scrollBy(self, if (ev.btn == 64) 3 else -3);return .stay;}+ // A drag or its release belongs to the selection; a press only ANCHORS one+ // and falls through, so a plain click keeps its meaning (#529).+ if (selection.mouse(self, ev)) return .stay;if (@import("image.zig").mouse(self, ev)) return .stay;if (!ev.down or ev.btn != 0) return .stay;if (self.overlay != .none) {@@ -241,7 +247,12 @@ fn mouseKey(self: *Model, ev: key_mod.Mouse) Effect {// toggle anything (#519).const i = scrollback.indexAtVisual(self, vis, self.last_term_width) orelse return .stay;self.selected = i;- if (self.history.items[i].kind == .tool) self.toggleToolGroup(i);+ if (self.history.items[i].kind == .tool) {+ self.toggleToolGroup(i);+ // Remembered so the fold can be put back if this press turns out to be+ // the start of a drag — selecting text must not restructure the view.+ self.sel.undo_idx = i;+ }return .stay;}@@ -491,23 +502,9 @@ test "Cmd+Delete and Ctrl+U kill the draft, not the viewport" {try std.testing.expect(m.follow);}-test "click on blank padding selects and toggles nothing (#519)" {- var m: Model = undefined;- m.setup(std.testing.allocator);- defer m.deinit();- try m.push(.user, "hi");- try m.push(.tool, "⚙ bash");- try m.push(.tool, "✓ bash");- m.last_term_height = 24;- m.last_term_width = 80;- m.mid_origin = 0;- m.mid_skip = 0;- m.prompt_origin = 20;- try std.testing.expect(m.history.items[1].folded);- _ = handle(&m, .{ .mouse = .{ .btn = 0, .x = 2, .y = 15, .down = true } });- try std.testing.expect(m.history.items[1].folded);- try std.testing.expectEqual(app.Focus.scrollback, m.focus);-}+// The click-behaviour tests live beside the drag tests in selection.zig: a+// click and a drag are two readings of the same press, and they only stay+// separable if they are pinned together.test "slash selection clamps to the filtered list and stays visible (#522)" {var m: Model = undefined;@@ -526,22 +523,6 @@ test "slash selection clamps to the filtered list and stays visible (#522)" {try std.testing.expect(std.mem.indexOf(u8, menu, "› ") != null);}-test "click on a folded tool row expands it" {- var m: Model = undefined;- m.setup(std.testing.allocator);- defer m.deinit();- try m.push(.user, "hi");- try m.push(.tool, "⚙ bash");- try m.push(.tool, "✓ bash");- m.last_term_height = 24;- m.last_term_width = 80;- m.mid_origin = 1;- m.mid_skip = 0;- try std.testing.expect(m.history.items[1].folded);- _ = handle(&m, .{ .mouse = .{ .btn = 0, .x = 4, .y = 3, .down = true } });- try std.testing.expect(!m.history.items[1].folded);-}-test "empty Up scrolls transcript and keeps prompt focus" {var m: Model = undefined;m.setup(std.testing.allocator);diff --git a/TUI/render.zig b/TUI/render.zigindex 6540d0fe..83de3097 100644--- a/TUI/render.zig+++ b/TUI/render.zig@@ -8,6 +8,7 @@ const app = @import("app.zig");const chrome = @import("chrome.zig");const engine = @import("engine.zig");const scrollback = @import("scrollback.zig");+const selection = @import("selection.zig");const theme_mod = @import("theme.zig");const welcome = @import("welcome.zig");const Model = app.Model;@@ -129,7 +130,10 @@ pub fn render(self: *Model, gpa: std.mem.Allocator, width: usize, height: usize,if (image_card[image_card.len - 1] != '\n') try out.append('\n');}try out.appendSlice(bottom.items);- return gpa.dupe(u8, out.items);+ // Selection is a post-pass over the finished frame: the row builders stay+ // unaware of it, and the band lands on screen rows exactly as the mouse+ // reported them (#529).+ return gpa.dupe(u8, try selection.paint(self, a, out.items, width));}fn countLines(s: []const u8) usize {diff --git a/TUI/root.zig b/TUI/root.zigindex 826bc6b9..7692c77f 100644--- a/TUI/root.zig+++ b/TUI/root.zig@@ -52,6 +52,7 @@ test {_ = @import("bgop.zig");_ = @import("welcome.zig");_ = @import("scrollback.zig");+ _ = @import("selection.zig");_ = @import("chrome.zig");_ = @import("models.zig");_ = @import("markdown.zig");diff --git a/TUI/selection.zig b/TUI/selection.zignew file mode 100644index 00000000..68f56485--- /dev/null+++ b/TUI/selection.zig@@ -0,0 +1,548 @@+//! In-app drag selection (#529). Mouse tracking (?1000/?1003/?1006) swallows+//! the terminal's own selection gesture, so the TUI owns it: drag paints an+//! inverse-video band over the composed frame, release copies the covered text+//! to the system clipboard, and any other input drops the band.+//!+//! Granularity is grok's: whole rows, with column bounds on the FIRST and LAST+//! row only. Character precision inside a styled, wrapped row would have to+//! agree with every row builder in scrollback.zig/markdown.zig; row bands agree+//! with the rendered frame by construction — the band and the copied text come+//! out of the same pass over the same bytes.+//!+//! Presentation state only. This renders the engine's transcript; it does not+//! fork engine behaviour, and nothing here reaches a provider.++const std = @import("std");+const builtin = @import("builtin");++const app = @import("app.zig");+const engine = @import("engine.zig");+const key_mod = @import("key.zig");+const theme_mod = @import("theme.zig");+const Model = app.Model;++/// Anchor/head live in SCREEN coordinates: the band belongs to the frame it was+/// drawn over, and it is cleared by the next non-drag input, so it can never+/// drift onto content that scrolled underneath it.+pub const Sel = struct {+ /// A drag has happened — the band paints and `Model.sel_text` is live.+ active: bool = false,+ /// Button is down inside the transcript; motion may still turn it into a drag.+ pressed: bool = false,+ a_row: usize = 0,+ a_col: usize = 0,+ h_row: usize = 0,+ h_col: usize = 0,+ /// Tool group the press expanded/collapsed. Reverted the instant motion+ /// proves the gesture was a drag, so selecting text never folds a group.+ undo_idx: ?usize = null,+ /// Release seen; the copy runs in the next paint. A terminal delivers a+ /// whole flick of the mouse in one read, and run.zig drains every event in+ /// that read before rendering — copying at release time would then ship the+ /// text of a frame the last motion never reached.+ copy_pending: bool = false,+ /// setToast BORROWS its slice — the copy toast has to outlive this call, so+ /// it is formatted into model-owned storage.+ toast_buf: [40]u8 = undefined,+};++pub const Range = struct { r0: usize, c0: usize, r1: usize, c1: usize };++/// True when the event belonged to the drag gesture and nothing else should see+/// it. A press is only RECORDED: a click keeps its existing meaning (tool-group+/// expand, composer focus) until motion proves otherwise.+pub fn mouse(self: *Model, ev: key_mod.Mouse) bool {+ // SGR and X10 agree on 32 = motion with the left button held.+ if (ev.btn == 32 and ev.down) {+ extend(self, ev);+ return true;+ }+ // Release: SGR reports the button with a final 'm', X10 reports button 3.+ if (!ev.down and (ev.btn == 0 or ev.btn == 3)) return finish(self);+ if (ev.down and ev.btn == 0) begin(self, ev);+ return false;+}++fn begin(self: *Model, ev: key_mod.Mouse) void {+ clear(self);+ // A press with an overlay up means "close it" (keys.zig) — the frame the+ // band would be measured against is gone by the next motion.+ if (self.overlay != .none) return;+ const p = point(self, ev, false) orelse return;+ self.sel.pressed = true;+ self.sel.a_row = p.row;+ self.sel.a_col = p.col;+ self.sel.h_row = p.row;+ self.sel.h_col = p.col;+}++fn extend(self: *Model, ev: key_mod.Mouse) void {+ if (!self.sel.pressed) return;+ const p = point(self, ev, true) orelse return;+ if (self.sel.undo_idx) |i| {+ self.toggleToolGroup(i);+ self.sel.undo_idx = null;+ }+ self.sel.h_row = p.row;+ self.sel.h_col = p.col;+ self.sel.active = true;+}++/// Mouse-up arms the copy. The band itself KEEPS painting: the user sees+/// exactly what landed on the clipboard until the next keystroke or click.+fn finish(self: *Model) bool {+ if (!self.sel.pressed) return true; // a release we never anchored+ self.sel.pressed = false;+ self.sel.undo_idx = null;+ if (self.sel.active) self.sel.copy_pending = true;+ return true;+}++/// Runs inside the paint that captured the text, so the clipboard and the band+/// can never disagree about which frame was selected.+fn commit(self: *Model) void {+ self.sel.copy_pending = false;+ const text = self.sel_text;+ if (text.len == 0) {+ clear(self);+ return;+ }+ const lines = std.mem.count(u8, text, "\n") + 1;+ if (copyText(text)) {+ const s = std.fmt.bufPrint(&self.sel.toast_buf, "{d} line{s} copied", .{+ lines,+ if (lines == 1) "" else "s",+ }) catch "copied";+ self.setToast(s);+ } else self.setToast("copy failed");+}++pub fn clear(self: *Model) void {+ self.sel.active = false;+ self.sel.pressed = false;+ self.sel.undo_idx = null;+ self.sel.copy_pending = false;+ if (self.sel_text.len > 0) {+ self.alloc.free(self.sel_text);+ self.sel_text = "";+ }+}++const Point = struct { row: usize, col: usize };++/// Screen cell under the pointer, restricted to the transcript viewport. A+/// press outside it anchors nothing (`clamp = false`); a drag that runs off the+/// edge keeps selecting the edge row (`clamp = true`).+fn point(self: *const Model, ev: key_mod.Mouse, clamp: bool) ?Point {+ if (self.prompt_origin <= self.mid_origin) return null;+ const last_row = self.prompt_origin - 1;+ const last_col = if (self.last_term_width > 0) self.last_term_width - 1 else 0;+ var y: usize = if (ev.y > 0) ev.y - 1 else 0;+ var x: usize = if (ev.x > 0) ev.x - 1 else 0;+ if (y < self.mid_origin or y > last_row) {+ if (!clamp) return null;+ y = std.math.clamp(y, self.mid_origin, last_row);+ }+ if (x > last_col) x = last_col;+ return .{ .row = y, .col = x };+}++/// Normalized band: [r0,c0] inclusive to [r1,c1) exclusive, so the cell under+/// the pointer is part of the selection.+pub fn ordered(self: *const Model) ?Range {+ if (!self.sel.active) return null;+ const s = self.sel;+ const fwd = s.a_row < s.h_row or (s.a_row == s.h_row and s.a_col <= s.h_col);+ return .{+ .r0 = if (fwd) s.a_row else s.h_row,+ .c0 = if (fwd) s.a_col else s.h_col,+ .r1 = if (fwd) s.h_row else s.a_row,+ .c1 = (if (fwd) s.h_col else s.a_col) + 1,+ };+}++/// Post-pass over the COMPOSED frame: the row builders in scrollback.zig stay+/// untouched, and the captured text is the same bytes the band covers.+pub fn paint(self: *Model, a: std.mem.Allocator, frame: []const u8, width: usize) ![]const u8 {+ const r = ordered(self) orelse return frame;+ var out = std.array_list.Managed(u8).init(a);+ var text = std.array_list.Managed(u8).init(a);+ var row: usize = 0;+ var it = std.mem.splitScalar(u8, frame, '\n');+ while (it.next()) |ln| : (row += 1) {+ if (row > 0) try out.append('\n');+ const c0 = if (row == r.r0) r.c0 else 0;+ const c1 = @min(if (row == r.r1) r.c1 else width, width);+ if (row < r.r0 or row > r.r1 or c1 <= c0) {+ try out.appendSlice(ln);+ continue;+ }+ if (row > r.r0) try text.append('\n');+ try band(&out, &text, ln, c0, c1);+ }+ setText(self, text.items);+ if (self.sel.copy_pending) commit(self);+ return out.items;+}++fn setText(self: *Model, s: []const u8) void {+ const owned = self.alloc.dupe(u8, s) catch return;+ if (self.sel_text.len > 0) self.alloc.free(self.sel_text);+ self.sel_text = owned;+}++/// One row: verbatim up to `c0`, inverse video across [c0,c1), verbatim after.+/// SGR inside the band is dropped so the highlight is uniform (a stray `0m`+/// would otherwise cancel it mid-band) and re-emitted at the far edge, or the+/// rest of the row would paint in whatever style the band swallowed.+fn band(out: *std.array_list.Managed(u8), text: *std.array_list.Managed(u8), ln: []const u8, c0: usize, c1: usize) !void {+ var sgr: theme_mod.Sgr = .{};+ var i: usize = 0;+ var col: usize = 0;+ while (i < ln.len and col < c0) {+ if (ln[i] == 0x1b) {+ const e = theme_mod.skipEsc(ln, i);+ sgr.note(ln[i..e]);+ try out.appendSlice(ln[i..e]);+ i = e;+ continue;+ }+ const c = cpAt(ln, i);+ if (col + c.w > c0) break; // a wide glyph never straddles the edge+ try out.appendSlice(ln[i .. i + c.n]);+ col += c.w;+ i += c.n;+ }+ try pad(out, c0 -| col);+ col = @max(col, c0);+ try out.appendSlice("\x1b[7m");+ const mark = text.items.len;+ while (i < ln.len and col < c1) {+ if (ln[i] == 0x1b) {+ const e = theme_mod.skipEsc(ln, i);+ sgr.note(ln[i..e]);+ i = e;+ continue;+ }+ const c = cpAt(ln, i);+ if (col + c.w > c1) break;+ try out.appendSlice(ln[i .. i + c.n]);+ try text.appendSlice(ln[i .. i + c.n]);+ col += c.w;+ i += c.n;+ }+ // The band covers blank cells too, exactly like a terminal's own selection.+ // Those pad columns are not text: trim them back out of the capture.+ try pad(out, c1 -| col);+ var end = text.items.len;+ while (end > mark and text.items[end - 1] == ' ') end -= 1;+ text.shrinkRetainingCapacity(end);+ try out.appendSlice("\x1b[27m");+ var buf: [theme_mod.Sgr.max_render]u8 = undefined;+ try out.appendSlice(sgr.render(&buf));+ if (i < ln.len) try out.appendSlice(ln[i..]);+}++fn pad(out: *std.array_list.Managed(u8), n: usize) !void {+ var left = n;+ while (left > 0) : (left -= 1) try out.append(' ');+}++const Cp = struct { w: usize, n: usize };++/// Columns and bytes of the codepoint at `i` — theme_mod.charWidth plus the+/// VS16 promotion, so the band measures the same cells visibleLen does.+fn cpAt(s: []const u8, i: usize) Cp {+ const len = std.unicode.utf8ByteSequenceLength(s[i]) catch return .{ .w = 1, .n = 1 };+ const n = @min(@as(usize, len), s.len - i);+ const cp = std.unicode.utf8Decode(s[i .. i + n]) catch return .{ .w = 1, .n = n };+ const w: usize = theme_mod.charWidth(cp);+ if (w == 1 and i + n + 3 <= s.len and std.mem.eql(u8, s[i + n .. i + n + 3], "\u{FE0F}")) return .{ .w = 2, .n = n };+ return .{ .w = w, .n = n };+}++/// The session's clipboard callback when there is one (/copy's path), else the+/// platform tool directly — the TUI also runs without a live session.+pub fn copyText(text: []const u8) bool {+ if (text.len == 0) return false;+ if (engine.g_copy_fn) |f| return f(engine.g_turn_ctx, text);+ const argv: []const []const u8 = switch (builtin.os.tag) {+ .macos => &.{"pbcopy"},+ .linux => &.{ "xclip", "-selection", "clipboard" },+ else => return false,+ };+ const io = std.Io.Threaded.global_single_threaded.io();+ var child = std.process.spawn(io, .{+ .argv = argv,+ .stdin = .pipe,+ .stdout = .ignore,+ .stderr = .ignore,+ }) catch return false;+ if (child.stdin) |*s| {+ var wbuf: [4096]u8 = undefined;+ var w = s.writerStreaming(io, &wbuf);+ w.interface.writeAll(text) catch {};+ w.interface.flush() catch {};+ s.close(io);+ child.stdin = null;+ }+ const term = child.wait(io) catch return false;+ return term == .exited and term.exited == 0;+}++// ---------------------------------------------------------------- tests++const keys = @import("keys.zig");+const render_mod = @import("render.zig");++var copied: [4096]u8 = undefined;+var copied_len: usize = 0;++fn fakeCopy(_: ?*anyopaque, text: []const u8) bool {+ copied_len = @min(text.len, copied.len);+ @memcpy(copied[0..copied_len], text[0..copied_len]);+ return true;+}++fn press(m: *Model, x: u16, y: u16) void {+ _ = keys.handle(m, .{ .mouse = .{ .btn = 0, .x = x, .y = y, .down = true } });+}++fn drag(m: *Model, x: u16, y: u16) void {+ _ = keys.handle(m, .{ .mouse = .{ .btn = 32, .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 frameOf(m: *Model) ![]const u8 {+ return render_mod.render(m, std.testing.allocator, 80, 24, 0);+}++test "a drag paints an inverse band and copies the covered rows (#529)" {+ engine.g_copy_fn = fakeCopy;+ defer engine.g_copy_fn = null;+ copied_len = 0;+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ try m.push(.assistant, "SELECTME alpha line");+ try m.push(.assistant, "SELECTME beta line");+ const first = try frameOf(&m);+ std.testing.allocator.free(first);+ // Find the painted row of the first entry, then drag across both.+ const top = m.mid_origin + m.sticky_rows;+ press(&m, 1, @intCast(top + 1));+ drag(&m, 40, @intCast(top + 2));+ const dragging = try frameOf(&m);+ defer std.testing.allocator.free(dragging);+ try std.testing.expect(std.mem.indexOf(u8, dragging, "\x1b[7m") != null);+ try std.testing.expect(std.mem.indexOf(u8, dragging, "\x1b[27m") != null);+ try std.testing.expect(std.mem.indexOf(u8, m.sel_text, "SELECTME alpha") != null);+ release(&m, 40, @intCast(top + 2));+ // The band survives the release so the user sees what was copied...+ const after = try frameOf(&m);+ defer std.testing.allocator.free(after);+ try std.testing.expect(m.sel.active);+ try std.testing.expect(std.mem.indexOf(u8, after, "\x1b[7m") != null);+ try std.testing.expect(std.mem.indexOf(u8, copied[0..copied_len], "SELECTME alpha") != null);+ try std.testing.expect(std.mem.indexOf(u8, m.toast, "copied") != null);+ // ...and the next ordinary keystroke drops it.+ _ = keys.handle(&m, .{ .char = 'x' });+ try std.testing.expect(!m.sel.active);+ const clean = try frameOf(&m);+ defer std.testing.allocator.free(clean);+ try std.testing.expect(std.mem.indexOf(u8, clean, "\x1b[7m") == null);+}++test "click on a folded tool row still expands it, and paints no band" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ try m.push(.user, "hi");+ try m.push(.tool, "⚙ bash");+ try m.push(.tool, "✓ bash");+ m.last_term_height = 24;+ m.last_term_width = 80;+ m.mid_origin = 1;+ m.mid_skip = 0;+ m.prompt_origin = 20;+ try std.testing.expect(m.history.items[1].folded);+ press(&m, 4, 3);+ release(&m, 4, 3);+ try std.testing.expect(!m.history.items[1].folded);+ try std.testing.expect(!m.sel.active);+ try std.testing.expect(m.sel_text.len == 0);+}++test "click on blank padding selects and toggles nothing (#519)" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ try m.push(.user, "hi");+ try m.push(.tool, "⚙ bash");+ try m.push(.tool, "✓ bash");+ m.last_term_height = 24;+ m.last_term_width = 80;+ m.mid_origin = 0;+ m.mid_skip = 0;+ m.prompt_origin = 20;+ try std.testing.expect(m.history.items[1].folded);+ press(&m, 2, 15);+ try std.testing.expect(m.history.items[1].folded);+ try std.testing.expectEqual(app.Focus.scrollback, m.focus);+}++test "a drag that starts on a tool group leaves the group as it was" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ try m.push(.user, "hi");+ try m.push(.tool, "⚙ bash");+ try m.push(.tool, "✓ bash");+ m.last_term_height = 24;+ m.last_term_width = 80;+ m.mid_origin = 1;+ m.prompt_origin = 20;+ try std.testing.expect(m.history.items[1].folded);+ press(&m, 4, 3);+ drag(&m, 30, 5);+ try std.testing.expect(m.history.items[1].folded); // the expand was undone+ try std.testing.expect(m.sel.active);+}++test "a press outside the transcript never anchors a band" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ m.last_term_width = 80;+ m.mid_origin = 2;+ m.prompt_origin = 20;+ press(&m, 5, 21); // composer row+ drag(&m, 40, 10);+ try std.testing.expect(!m.sel.active);+ try std.testing.expectEqual(app.Focus.prompt, m.focus);+}++test "the wheel scrolls and drops the band instead of extending it" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ m.last_term_width = 80;+ m.last_term_height = 24;+ m.mid_origin = 1;+ m.prompt_origin = 20;+ press(&m, 2, 3);+ drag(&m, 40, 6);+ try std.testing.expect(m.sel.active);+ _ = keys.handle(&m, .{ .mouse = .{ .btn = 64, .x = 40, .y = 6, .down = true } });+ try std.testing.expect(!m.sel.active);+ try std.testing.expect(!m.follow);+}++test "column bounds hold on the first and last row; middle rows are whole" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ m.last_term_width = 10;+ m.mid_origin = 0;+ m.prompt_origin = 5;+ m.sel = .{ .active = true, .a_row = 0, .a_col = 3, .h_row = 2, .h_col = 4 };+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const out = try paint(&m, arena.allocator(), "abcdefghij\nklmnopqrst\nuvwxyz\nTAIL", 10);+ // First row starts at column 3, last row ends after column 4.+ try std.testing.expect(std.mem.indexOf(u8, out, "abc\x1b[7mdefghij") != null);+ try std.testing.expect(std.mem.indexOf(u8, out, "\x1b[7muvwxy\x1b[27m") != null);+ try std.testing.expect(std.mem.indexOf(u8, out, "TAIL") != null);+ try std.testing.expectEqualStrings("defghij\nklmnopqrst\nuvwxy", m.sel_text);+ // A row shorter than the band is padded, and the pad is not text.+ m.sel = .{ .active = true, .a_row = 0, .a_col = 0, .h_row = 0, .h_col = 9 };+ const short = try paint(&m, arena.allocator(), "hi", 10);+ try std.testing.expect(std.mem.indexOf(u8, short, "\x1b[7mhi \x1b[27m") != null);+ try std.testing.expectEqualStrings("hi", m.sel_text);+}++test "the band drops SGR inside it and restores the style after it" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ m.last_term_width = 12;+ m.mid_origin = 0;+ m.prompt_origin = 4;+ m.sel = .{ .active = true, .a_row = 0, .a_col = 2, .h_row = 0, .h_col = 5 };+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const out = try paint(&m, arena.allocator(), "ab\x1b[31mcdef\x1b[0mgh", 12);+ // No reset survives inside the band...+ const b0 = std.mem.indexOf(u8, out, "\x1b[7m").?;+ const b1 = std.mem.indexOf(u8, out, "\x1b[27m").?;+ try std.testing.expect(std.mem.indexOf(u8, out[b0..b1], "\x1b[") == 0);+ try std.testing.expect(std.mem.indexOf(u8, out[b0 + 4 .. b1], "\x1b[") == null);+ try std.testing.expectEqualStrings("cdef", m.sel_text);+}++test "a wide glyph never straddles a band edge" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ m.last_term_width = 10;+ m.mid_origin = 0;+ m.prompt_origin = 4;+ // "a" + 世 (2 cols) + "b": a band ending mid-glyph must leave it out.+ m.sel = .{ .active = true, .a_row = 0, .a_col = 0, .h_row = 0, .h_col = 1 };+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const out = try paint(&m, arena.allocator(), "a\u{4e16}b", 10);+ try std.testing.expectEqualStrings("a", m.sel_text);+ // The excluded glyph leaves a pad column so the band still measures 2, and+ // the glyph itself paints outside it, whole.+ try std.testing.expect(std.mem.indexOf(u8, out, "\x1b[7ma \x1b[27m") != null);+ try std.testing.expect(std.mem.endsWith(u8, out, "\u{4e16}b"));+}++test "an empty drag copies nothing and leaves no band" {+ engine.g_copy_fn = fakeCopy;+ defer engine.g_copy_fn = null;+ copied_len = 0;+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ m.last_term_width = 80;+ m.mid_origin = 1;+ m.prompt_origin = 20;+ press(&m, 4, 5);+ drag(&m, 4, 5);+ release(&m, 4, 5);+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ _ = try paint(&m, arena.allocator(), "\n\n\n\n\n\n", 80);+ try std.testing.expectEqual(@as(usize, 0), copied_len);+ try std.testing.expect(!m.sel.active);+}++test "a drag whose motion and release arrive in ONE read still copies" {+ // Terminals batch mouse reports, and run.zig drains every event in a read+ // before it renders — so the copy has to be taken from the paint that+ // follows, not from whatever text the previous frame happened to hold.+ engine.g_copy_fn = fakeCopy;+ defer engine.g_copy_fn = null;+ copied_len = 0;+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ try m.push(.assistant, "BATCHED alpha");+ try m.push(.assistant, "BATCHED beta");+ const first = try frameOf(&m);+ std.testing.allocator.free(first);+ const top = m.mid_origin + m.sticky_rows;+ press(&m, 1, @intCast(top + 1));+ drag(&m, 60, @intCast(top + 2));+ release(&m, 60, @intCast(top + 2));+ try std.testing.expectEqual(@as(usize, 0), copied_len); // nothing yet...+ const frame = try frameOf(&m);+ defer std.testing.allocator.free(frame);+ try std.testing.expect(std.mem.indexOf(u8, copied[0..copied_len], "BATCHED alpha") != null);+ try std.testing.expectEqualStrings(m.sel_text, copied[0..copied_len]);+}diff --git a/scripts/eval-tier1.sh b/scripts/eval-tier1.shindex 5869598d..190c71a2 100644--- a/scripts/eval-tier1.sh+++ b/scripts/eval-tier1.sh@@ -229,6 +229,12 @@ if wanted tuiguard; thenif python3 scripts/tui-pty-guard.py zig-out/bin/graff; then :; elserecord_fail tuiguardfi+ # #529: the drag gesture the mouse-tracking modes above swallow. Paints an+ # inverse band and lands on the real clipboard (restored afterwards);+ # skips itself off macOS, where there is no pbcopy/pbpaste.+ if python3 scripts/test-tui-selection.py zig-out/bin/graff; then :; else+ record_fail tuiguard+ fififidiff --git a/scripts/eval/tier1-manifest.json b/scripts/eval/tier1-manifest.jsonindex c59d2dea..ab1ad17d 100644--- a/scripts/eval/tier1-manifest.json+++ b/scripts/eval/tier1-manifest.json@@ -15,7 +15,7 @@"src/repl.zig","TUI/root.zig"],- "test_count_baseline": 1318,+ "test_count_baseline": 1322,"test_count_slack": 25,"required_invariants": [{diff --git a/scripts/test-tui-selection.py b/scripts/test-tui-selection.pynew file mode 100644index 00000000..d59d321d--- /dev/null+++ b/scripts/test-tui-selection.py@@ -0,0 +1,230 @@+#!/usr/bin/env python3+"""Live proof for #529: drag-select in the fullscreen TUI, against the real binary.++Under a pty, with a real session (so the clipboard callback is the session's+own), the probe:++ 1. boots `graff tui --yolo` in a scratch cwd with an EMPTY MCP config,+ 2. fills the transcript with a shell line (`!printf ...`) so no model call is+ needed and the text on screen is known,+ 3. sends an SGR mouse drag over the transcript — press, motions, release,+ 4. asserts the frame that comes back carries inverse-video (SGR 7) spans,+ 5. asserts the system clipboard now holds the dragged text (pbpaste), and+ that a following keystroke clears the band again.++Usage: python3 scripts/test-tui-selection.py [path/to/graff] (default zig-out/bin/graff)+Exit 0 = pass. Skips (exit 0, notice) with no pty or no clipboard tool.+"""+import os+import re+import subprocess+import sys+import tempfile+import time++# Absolutized BEFORE the fork: the child chdirs into its scratch workspace.+BIN = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else "zig-out/bin/graff")+BOOT_MAX = 15.0+MARK = "SELECTME_ZULU"+SENTINEL = "CLIPBOARD_SENTINEL_BEFORE_DRAG"+++def drain(fd, seconds):+ # select-gated: a quiet pty blocks on read, and a blocking read here would+ # hang the probe on exactly the wedged states it exists to catch.+ import select+ out = b""+ end = time.time() + seconds+ while time.time() < end:+ r, _, _ = select.select([fd], [], [], min(0.1, max(end - time.time(), 0.01)))+ if not r:+ continue+ try:+ chunk = os.read(fd, 65536)+ except OSError:+ break+ if not chunk:+ break+ out += chunk+ return out+++def boot(fd):+ out = b""+ end = time.time() + BOOT_MAX+ while time.time() < end:+ out += drain(fd, 0.3)+ if b"\x1b[?1049h" in out:+ return out + drain(fd, 1.5)+ return out+++def resize(fd, rows, cols):+ import fcntl+ import struct+ import termios+ fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack("HHHH", rows, cols, 0, 0))+++def spawn(cwd, env_extra, rows=30, cols=100):+ import pty+ pid, fd = pty.fork()+ if pid == 0:+ os.chdir(cwd)+ os.environ.update(env_extra)+ os.execv(BIN, [BIN, "tui", "--yolo"])+ # A pty forked without a size reports 0x0; the TUI would clamp to its 40x12+ # floor and the drag rows below would land in the composer.+ resize(fd, rows, cols)+ return pid, fd+++def fresh_ws():+ ws = tempfile.mkdtemp(prefix="tui-sel-")+ empty = os.path.join(ws, "empty-mcp.json")+ with open(empty, "w") as f:+ f.write('{"mcpServers": {}}')+ return ws, {"GRAFF_MCP_CONFIG": empty, "GRAFF_LEARN_AUTO": "off"}+++def reap(pid, fd):+ import signal+ try:+ os.write(fd, b"\x11") # Ctrl+Q+ drain(fd, 3.0)+ except OSError:+ pass+ try:+ os.kill(pid, signal.SIGKILL)+ except OSError:+ pass+ try:+ os.waitpid(pid, 0)+ except OSError:+ pass+++def clipboard_set(text):+ subprocess.run(["pbcopy"], input=text.encode(), check=True)+++def clipboard_get():+ return subprocess.run(["pbpaste"], capture_output=True, check=True).stdout.decode()+++def sgr(btn, col, row, press=True):+ return f"\x1b[<{btn};{col};{row}{'M' if press else 'm'}".encode()+++def full_frame(fd, rows, cols):+ """Screen rows, in order, from a FORCED full repaint.++ Between repaints the TUI only rewrites the rows that changed, addressing+ each with CSI row;1H — stripping those out of a diff stream gives row+ indices that are plausible and wrong. A resize invalidates run.zig's diff+ baseline, so what follows is one whole frame, top row first.+ """+ resize(fd, rows - 1, cols)+ drain(fd, 0.4)+ resize(fd, rows, cols)+ stream = drain(fd, 1.2)+ frame = stream.rsplit(b"\x1b[2J\x1b[H", 1)+ if len(frame) < 2:+ return None+ text = re.sub(rb"\x1b\][^\x07\x1b]*(\x07|\x1b\\)", b"", frame[1])+ text = re.sub(rb"\x1b\[[0-9;?<>]*[a-zA-Z]", b"", text)+ return [ln.decode(errors="replace") for ln in text.split(b"\r\n")]+++ROWS, COLS = 30, 100+++def run():+ ws, env = fresh_ws()+ pid, fd = spawn(ws, env, ROWS, COLS)+ try:+ boot(fd)+ # Transcript content without a model call: the `!` line runs in-session.+ os.write(fd, f"!printf '{MARK}_ONE\\n{MARK}_TWO\\n'\r".encode())+ out = drain(fd, 4.0)+ if MARK.encode() not in out:+ return f"the transcript never showed {MARK} (bash row missing)"+ rows = full_frame(fd, ROWS, COLS)+ if rows is None:+ return "no full repaint to read the screen from"+ hit = next((i for i, ln in enumerate(rows) if MARK + "_ONE" in ln), None)+ if hit is None:+ return f"{MARK}_ONE is not on any screen row of the full frame"+ # Baseline: nothing is inverse-video before the drag.+ idle = drain(fd, 0.4)+ if b"\x1b[7m" in idle:+ return "the idle frame already carries an inverse-video band"+ clipboard_set(SENTINEL)+ # Press on the marked row, drag two rows down, release. SGR: button 0+ # press, 32 = motion with the button held, final 'm' = release.+ top = hit + 1 # screen rows are 1-based in the SGR report+ os.write(fd, sgr(0, 3, top))+ drain(fd, 0.3)+ os.write(fd, sgr(32, 40, top + 1))+ drain(fd, 0.3)+ os.write(fd, sgr(32, 60, top + 2))+ painted = drain(fd, 0.6)+ if b"\x1b[7m" not in painted:+ return "the drag painted no inverse-video band (no SGR 7 in the frame)"+ if b"\x1b[27m" not in painted:+ return "the band never emitted its SGR 27 terminator"+ os.write(fd, sgr(0, 60, top + 2, press=False))+ drain(fd, 1.0)+ got = clipboard_get()+ if got == SENTINEL:+ return "the release did not reach the clipboard (still the sentinel)"+ if f"{MARK}_ONE" not in got:+ return f"the clipboard got {got!r}, which does not contain {MARK}_ONE"+ if len(got.split("\n")) != 3:+ return f"a 3-row drag copied {len(got.split(chr(10)))} row(s): {got!r}"+ # A following keystroke drops the band: the repaint that follows must+ # rewrite those rows WITHOUT inverse video.+ os.write(fd, b"z")+ after = drain(fd, 1.0)+ if b"\x1b[7m" in after:+ return "a keystroke did not clear the band"+ return None+ finally:+ reap(pid, fd)+++def main():+ if not os.path.exists(BIN):+ print(f"tui-selection: {BIN} not built — skipping")+ return 0+ try:+ import pty # noqa: F401+ except ImportError:+ print("tui-selection: no pty support here — skipping")+ return 0+ if sys.platform != "darwin":+ print("tui-selection: clipboard probe is macOS-only (pbcopy/pbpaste) — skipping")+ return 0+ saved = None+ try:+ saved = clipboard_get()+ except Exception:+ print("tui-selection: no clipboard access — skipping")+ return 0+ try:+ err = run()+ finally:+ if saved is not None:+ try:+ clipboard_set(saved)+ except Exception:+ pass+ if err:+ print(f" ✗ drag-select: {err}")+ return 1+ print(" ✓ drag-select: inverse band painted, clipboard received the rows")+ return 0+++if __name__ == "__main__":+ sys.exit(main())