graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(tui): swallow leftover Kitty CSI-u and record stdin trajectories
Rach Pradhanea0880c7fccd
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f5e5271a..c97d86df 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,6 +10,13 @@ The release workflow uses a tag's section here as its release notes (ahand-written `docs/releases/<tag>.md` wins if present), so keeping this filecurrent is part of cutting a release.+## v0.0.256 (2026-08-15)++- Leftover Kitty keyboard CSI (`3u7444;9u`) is ignored instead of typed or+ turned into Escape/Ctrl+C (which cancelled the turn).+- The pager appends raw stdin hex to `.graff/tui-traj.jsonl` and the last+ visible frame on exit so the next leak can be replayed.+## v0.0.255 (2026-08-14)- The fullscreen pager no longer prints raw SGR mouse reportsdiff --git a/TUI/key.zig b/TUI/key.zigindex 9b8fa190..f2c8e1ca 100644--- a/TUI/key.zig+++ b/TUI/key.zig@@ -49,7 +49,7 @@ pub const Mouse = struct {pub fn next(bytes: []const u8, i: *usize) ?Key {if (i.* >= bytes.len) return null;- if (takeOrphanMouse(bytes, i)) |k| return k;+ if (takeOrphanCsi(bytes, i)) |k| return k;const b = bytes[i.*];i.* += 1;if (b == 0x1b) return escapeSeq(bytes, i);@@ -241,34 +241,35 @@ fn sgrMouse(params: []const u8, down: bool) Key {} };}-/// `<b;x;yM` or `b;x;yM` with no ESC — leftover after a split CSI.-fn takeOrphanMouse(bytes: []const u8, i: *usize) ?Key {+/// CSI debris after a split ESC (`7444;9u`, `39;33;23M`, `3u`). Never a+/// letter and never Escape/Ctrl-C — those cancel or kill the turn.+fn takeOrphanCsi(bytes: []const u8, i: *usize) ?Key {const start = i.*;if (start >= bytes.len) return null;var j = start;if (bytes[j] == '<') j += 1;if (j >= bytes.len or bytes[j] < '0' or bytes[j] > '9') return null;- var semis: u8 = 0;var k = j;while (k < bytes.len) : (k += 1) {const c = bytes[k];if (c >= '0' and c <= '9') continue;- if (c == ';') {- semis += 1;- continue;- }- if ((c == 'M' or c == 'm') and semis >= 2) {- var it = std.mem.splitScalar(u8, bytes[j..k], ';');- const btn = leadingInt(it.next() orelse "0");- const x = leadingInt(it.next() orelse "1");- const y = leadingInt(it.next() orelse "1");+ if (c == ';' or c == ':') continue;+ if (c >= 0x40 and c <= 0x7e) {i.* = k + 1;- return .{ .mouse = .{- .btn = @intCast(@min(btn, 255)),- .x = @intCast(@min(x, 999)),- .y = @intCast(@min(y, 999)),- .down = c == 'M',- } };+ if (c == 'M' or c == 'm') {+ const body = bytes[j..k];+ var it = std.mem.splitScalar(u8, body, ';');+ const btn = leadingInt(it.next() orelse "0");+ const x = leadingInt(it.next() orelse "1");+ const y = leadingInt(it.next() orelse "1");+ return .{ .mouse = .{+ .btn = @intCast(@min(btn, 255)),+ .x = @intCast(@min(x, 999)),+ .y = @intCast(@min(y, 999)),+ .down = c == 'M',+ } };+ }+ return .ignore;}return null;}@@ -373,3 +374,17 @@ test "SGR mouse flood does not leak digits" {try std.testing.expectEqual(@as(usize, 3), n);try std.testing.expectEqual(seq.len, i);}++test "orphan kitty CSI-u is ignore, never letters or Escape" {+ var i: usize = 0;+ const seq = "3u7444;9u7441;10u";+ var n: usize = 0;+ while (next(seq, &i)) |k| {+ try std.testing.expect(k == .ignore);+ n += 1;+ }+ try std.testing.expectEqual(@as(usize, 3), n);+ try std.testing.expectEqual(seq.len, i);+ i = 0;+ try std.testing.expectEqual(Key{ .char = 'h' }, next("hi", &i).?);+}diff --git a/TUI/root.zig b/TUI/root.zigindex 156d8f1d..5021d61a 100644--- a/TUI/root.zig+++ b/TUI/root.zig@@ -50,6 +50,7 @@ test {_ = @import("render.zig");_ = @import("tty.zig");_ = run_mod;+ _ = @import("traj.zig");}pub fn main(init: std.process.Init) !void {diff --git a/TUI/run.zig b/TUI/run.zigindex 7a685570..ecf66e83 100644--- a/TUI/run.zig+++ b/TUI/run.zig@@ -12,6 +12,7 @@ const render_mod = @import("render.zig");const theme_mod = @import("theme.zig");const turn = @import("turn.zig");const tty = @import("tty.zig");+const traj = @import("traj.zig");const Model = app.Model;pub const RunOpts = struct {@@ -61,6 +62,7 @@ pub fn run(// >4;2m: xterm modifyOtherKeys so Super+Backspace also arrives as CSI 27;9;127~.w.writeAll("\x1b[?1049h\x1b[?25l\x1b[?2004h\x1b[?1000h\x1b[?1006h\x1b[?7l\x1b[>11u\x1b[>4;2m") catch {};w.flush() catch {};+ traj.open(io);defer {w.writeAll("\x1b[>4;0m\x1b[<u\x1b[?7h\x1b[?1006l\x1b[?1000l\x1b[?2004l\x1b[?25h\x1b[?1049l") catch {};w.flush() catch {};@@ -107,6 +109,7 @@ pub fn run(if (!tty.poll(wait)) continue;const got = tty.readStdin(inbuf[pending_len..]);const n = pending_len + got;+ if (got > 0) traj.note(io, m.now_ms, inbuf[pending_len..n]);if (n == 0) {pending_len = 0;continue;@@ -128,6 +131,11 @@ pub fn run(pending_len = rest;} else pending_len = 0;}+ if (prev.len != 0) {+ const vis = @import("dump.zig").visible(gpa, prev) catch prev;+ defer if (vis.ptr != prev.ptr) gpa.free(vis);+ traj.snap(io, m.now_ms, vis);+ }}fn parkToShell(io: Io, w: *Io.Writer, raw: *tty.RawState) void {diff --git a/TUI/traj.zig b/TUI/traj.zignew file mode 100644index 00000000..1cdbdb3d--- /dev/null+++ b/TUI/traj.zig@@ -0,0 +1,112 @@+//! Append-only TUI input log so a leak or cancelled turn can be replayed.+//! `.graff/tui-traj.jsonl` — raw stdin hex, no prompts, no model text.++const std = @import("std");+const Io = std.Io;++pub const path = ".graff/tui-traj.jsonl";+const cap: u64 = 512 * 1024;+const chunk_max: usize = 256;++var ready: bool = false;+var test_path: ?[]const u8 = null;++fn dest() []const u8 {+ return test_path orelse path;+}++pub fn open(io: Io) void {+ Io.Dir.cwd().createDirPath(io, ".graff") catch {};+ ready = true;+}++pub fn note(io: Io, now_ms: u64, bytes: []const u8) void {+ if (!ready or bytes.len == 0) return;+ const slice = if (bytes.len > chunk_max) bytes[0..chunk_max] else bytes;+ var hex: [chunk_max * 2]u8 = undefined;+ const hexed = toHex(slice, &hex);+ var line_buf: [64 + chunk_max * 2]u8 = undefined;+ const line = std.fmt.bufPrint(&line_buf, "{{\"t\":{d},\"n\":{d},\"hex\":\"{s}\"}}\n", .{+ now_ms, bytes.len, hexed,+ }) catch return;+ append(io, line);+}++pub fn snap(io: Io, now_ms: u64, visible: []const u8) void {+ if (!ready) return;+ const slice = if (visible.len > chunk_max) visible[visible.len - chunk_max ..] else visible;+ var esc: [chunk_max * 2]u8 = undefined;+ const n = jsonEscape(slice, &esc);+ var line_buf: [80 + chunk_max * 2]u8 = undefined;+ const line = std.fmt.bufPrint(&line_buf, "{{\"t\":{d},\"vis\":\"{s}\"}}\n", .{ now_ms, esc[0..n] }) catch return;+ append(io, line);+}++fn append(io: Io, line: []const u8) void {+ rotateIfHuge(io);+ const f = Io.Dir.cwd().createFile(io, dest(), .{ .truncate = false }) catch return;+ defer f.close(io);+ const st = f.stat(io) catch return;+ f.writePositionalAll(io, line, st.size) catch {};+}++fn rotateIfHuge(io: Io) void {+ const st = Io.Dir.cwd().statFile(io, dest(), .{}) catch return;+ if (st.size < cap) return;+ Io.Dir.cwd().deleteFile(io, dest()) catch {};+}++fn toHex(src: []const u8, out: []u8) []const u8 {+ const hex = "0123456789abcdef";+ var i: usize = 0;+ while (i < src.len) : (i += 1) {+ out[i * 2] = hex[src[i] >> 4];+ out[i * 2 + 1] = hex[src[i] & 0xf];+ }+ return out[0 .. src.len * 2];+}++fn jsonEscape(src: []const u8, out: []u8) usize {+ var o: usize = 0;+ for (src) |c| {+ if (o + 2 >= out.len) break;+ if (c == '"' or c == '\\') {+ out[o] = '\\';+ o += 1;+ out[o] = c;+ o += 1;+ } else if (c == '\n') {+ out[o] = '\\';+ o += 1;+ out[o] = 'n';+ o += 1;+ } else if (c >= 32 and c < 127) {+ out[o] = c;+ o += 1;+ } else {+ if (o + 1 >= out.len) break;+ out[o] = '?';+ o += 1;+ }+ }+ return o;+}++test "note writes hex stdin and snap writes visible tail" {+ const io = std.Io.Threaded.global_single_threaded.io();+ const tmp = "TUI/testdata-tui-traj.jsonl";+ test_path = tmp;+ defer {+ test_path = null;+ Io.Dir.cwd().deleteFile(io, tmp) catch {};+ }+ Io.Dir.cwd().deleteFile(io, tmp) catch {};+ ready = true;+ note(io, 42, "\x1b[57444;9u");+ snap(io, 43, "thinking.\n3u7444;9u");+ const text = Io.Dir.cwd().readFileAlloc(io, tmp, std.testing.allocator, .limited(4096)) catch return error.TestUnexpectedResult;+ defer std.testing.allocator.free(text);+ try std.testing.expect(std.mem.indexOf(u8, text, "1b5b35373434343b3975") != null);+ try std.testing.expect(std.mem.indexOf(u8, text, "3u7444;9u") != null);+ try std.testing.expect(std.mem.indexOf(u8, text, "\"t\":42") != null);+}diff --git a/docs/releases/v0.0.256.md b/docs/releases/v0.0.256.mdnew file mode 100644index 00000000..525b205b--- /dev/null+++ b/docs/releases/v0.0.256.md@@ -0,0 +1,12 @@+# v0.0.256 (2026-08-15)++Hotfix: Super/modifier Kitty CSI-u leftovers (`3u7444;9u7441;10u`) were+typed into the transcript and sometimes decoded as Escape/Ctrl+C, which+killed the turn.++- Any orphan CSI (`…u`, `…M`, arrows) after a split ESC is consumed as+ ignore/mouse, never as letters.+- TUI stdin is appended to `.graff/tui-traj.jsonl` (hex, rotated at 512KiB)+ and the last visible frame is written on exit.++Bare `graff` stays the line REPL. `graff tui` is the pager.diff --git a/src/cli.zig b/src/cli.zigindex 7b60b12e..c157d713 100644--- a/src/cli.zig+++ b/src/cli.zig@@ -21,6 +21,9 @@ const harness_version = root.harness_version;pub const changelog_text =\\What's new\\──────────+ \\0.0.256+ \\ • TUI swallows leftover Kitty CSI-u (3u7444;9u) and logs stdin to .graff/tui-traj.jsonl+ \\\\0.0.255\\ • TUI no longer dumps mouse CSI (39;33;23M) into the thinking line\\