Skip to content

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

Python

fix(tui): stop mouse CSI leaking into the thinking line

Rach Pradhan28d993fc190f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2c4fd572..f5e5271a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,12 @@ The release workflow uses a tag's section here as its release notes (a
hand-written `docs/releases/<tag>.md` wins if present), so keeping this file
current is part of cutting a release.
+## v0.0.255 (2026-08-14)
+
+- The fullscreen pager no longer prints raw SGR mouse reports
+ (`39;33;23M`) into the thinking line. Hover-motion tracking is off;
+ split/orphan mouse sequences are consumed instead of typed.
+
## v0.0.254 (2026-08-14)
- `graff tui` (and TTY `graff repl`) is a Grok-style fullscreen pager: wrapped
diff --git a/TUI/key.zig b/TUI/key.zig
index 6f87a5cd..9b8fa190 100644
--- a/TUI/key.zig
+++ b/TUI/key.zig
@@ -49,6 +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;
const b = bytes[i.*];
i.* += 1;
if (b == 0x1b) return escapeSeq(bytes, i);
@@ -65,7 +66,13 @@ pub fn next(bytes: []const u8, i: *usize) ?Key {
}
fn escapeSeq(bytes: []const u8, i: *usize) ?Key {
- if (i.* >= bytes.len) return .escape;
+ // Lone ESC at the end of a read is almost always a split CSI (mouse
+ // flood, arrow). Do not treat it as the Escape key or the rest of the
+ // sequence is inserted as letters on the next read.
+ if (i.* >= bytes.len) {
+ i.* -= 1;
+ return null;
+ }
const c0 = bytes[i.*];
if (c0 == 0x7f or c0 == 0x08) {
i.* += 1;
@@ -85,6 +92,26 @@ fn escapeSeq(bytes: []const u8, i: *usize) ?Key {
}
if (c0 != '[') return .escape;
i.* += 1;
+ // X10 mouse (1000h without 1006): CSI M + 3 raw bytes.
+ if (i.* < bytes.len and bytes[i.*] == 'M' and (i.* + 1 >= bytes.len or bytes[i.* + 1] != ';')) {
+ if (i.* + 3 >= bytes.len) {
+ i.* -= 2;
+ return null;
+ }
+ i.* += 1;
+ const btn = bytes[i.*];
+ i.* += 1;
+ const x = bytes[i.*];
+ i.* += 1;
+ const y = bytes[i.*];
+ i.* += 1;
+ return .{ .mouse = .{
+ .btn = if (btn >= 32) btn - 32 else btn,
+ .x = if (x >= 32) @as(u16, x) - 32 else x,
+ .y = if (y >= 32) @as(u16, y) - 32 else y,
+ .down = true,
+ } };
+ }
const start = i.*;
while (i.* < bytes.len) : (i.* += 1) {
const c = bytes[i.*];
@@ -214,6 +241,40 @@ 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 {
+ 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");
+ i.* = k + 1;
+ return .{ .mouse = .{
+ .btn = @intCast(@min(btn, 255)),
+ .x = @intCast(@min(x, 999)),
+ .y = @intCast(@min(y, 999)),
+ .down = c == 'M',
+ } };
+ }
+ return null;
+ }
+ return null;
+}
+
fn leadingInt(s: []const u8) u32 {
var n: u32 = 0;
for (s) |c| {
@@ -280,3 +341,35 @@ test "next: letters, enter, ctrl-c, arrows, kitty ctrl-p" {
try std.testing.expectEqual(Key.delete_to_start, next("\x7f", &i).?);
held = 0;
}
+
+test "split ESC does not become Escape" {
+ var i: usize = 0;
+ try std.testing.expect(next("\x1b", &i) == null);
+ try std.testing.expectEqual(@as(usize, 0), i);
+}
+
+test "orphan SGR mouse is never inserted as letters" {
+ var i: usize = 0;
+ const k = next("39;33;23M", &i).?;
+ try std.testing.expect(k == .mouse);
+ try std.testing.expectEqual(@as(u8, 39), k.mouse.btn);
+ try std.testing.expectEqual(@as(u16, 33), k.mouse.x);
+ try std.testing.expectEqual(@as(usize, 9), i);
+ i = 0;
+ const k2 = next("<64;4;8Mhi", &i).?;
+ try std.testing.expect(k2 == .mouse);
+ try std.testing.expectEqual(@as(u8, 64), k2.mouse.btn);
+ try std.testing.expectEqual(Key{ .char = 'h' }, next("<64;4;8Mhi", &i).?);
+}
+
+test "SGR mouse flood does not leak digits" {
+ const seq = "\x1b[<39;33;23M\x1b[<39;26;20M\x1b[<39;25;19M";
+ var i: usize = 0;
+ var n: usize = 0;
+ while (next(seq, &i)) |k| {
+ try std.testing.expect(k == .mouse);
+ n += 1;
+ }
+ try std.testing.expectEqual(@as(usize, 3), n);
+ try std.testing.expectEqual(seq.len, i);
+}
diff --git a/TUI/run.zig b/TUI/run.zig
index 1d2f2307..7a685570 100644
--- a/TUI/run.zig
+++ b/TUI/run.zig
@@ -55,14 +55,14 @@ pub fn run(
var out_buf: [64 * 1024]u8 = undefined;
var stdout = Io.File.stdout().writer(io, &out_buf);
const w = &stdout.interface;
- // 1000/1003/1006: click + hover + wheel as buttons 64/65 (not arrows).
+ // 1000/1006: click + wheel as 64/65. Not 1003h — motion floods leak as text.
// 2004: bracketed paste. 7l: no autowrap into the prompt.
// >11u: kitty disambiguate + event types + all-keys (Cmd+Delete / Super latch).
// >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[?1003h\x1b[?1006h\x1b[?7l\x1b[>11u\x1b[>4;2m") catch {};
+ w.writeAll("\x1b[?1049h\x1b[?25l\x1b[?2004h\x1b[?1000h\x1b[?1006h\x1b[?7l\x1b[>11u\x1b[>4;2m") catch {};
w.flush() catch {};
defer {
- w.writeAll("\x1b[>4;0m\x1b[<u\x1b[?7h\x1b[?1006l\x1b[?1003l\x1b[?1000l\x1b[?2004l\x1b[?25h\x1b[?1049l") catch {};
+ w.writeAll("\x1b[>4;0m\x1b[<u\x1b[?7h\x1b[?1006l\x1b[?1000l\x1b[?2004l\x1b[?25h\x1b[?1049l") catch {};
w.flush() catch {};
}
@@ -131,14 +131,14 @@ pub fn run(
}
fn parkToShell(io: Io, w: *Io.Writer, raw: *tty.RawState) void {
- w.writeAll("\x1b[>4;0m\x1b[<u\x1b[?7h\x1b[?1006l\x1b[?1003l\x1b[?1000l\x1b[?2004l\x1b[?25h\x1b[?1049l") catch {};
+ w.writeAll("\x1b[>4;0m\x1b[<u\x1b[?7h\x1b[?1006l\x1b[?1000l\x1b[?2004l\x1b[?25h\x1b[?1049l") catch {};
w.flush() catch {};
tty.restore(raw.*);
if (builtin.os.tag != .windows) {
std.posix.raise(std.posix.SIG.TSTP) catch {};
}
raw.* = tty.enterRaw() orelse raw.*;
- w.writeAll("\x1b[?1049h\x1b[?25l\x1b[?2004h\x1b[?1000h\x1b[?1003h\x1b[?1006h\x1b[?7l\x1b[>11u\x1b[>4;2m") catch {};
+ w.writeAll("\x1b[?1049h\x1b[?25l\x1b[?2004h\x1b[?1000h\x1b[?1006h\x1b[?7l\x1b[>11u\x1b[>4;2m") catch {};
w.flush() catch {};
_ = io;
}
@@ -215,7 +215,7 @@ test "run loop enables click tracking and bracketed paste" {
try std.testing.expect(std.mem.indexOf(u8, src, &mouse_on) != null);
try std.testing.expect(std.mem.indexOf(u8, src, &sgr_on) != null);
try std.testing.expect(std.mem.indexOf(u8, src, &paste_on) != null);
- try std.testing.expect(std.mem.indexOf(u8, src, &hover_on) != null);
+ try std.testing.expect(std.mem.indexOf(u8, src, &hover_on) == null);
try std.testing.expect(std.mem.indexOf(u8, src, &[_]u8{ '?', '1', '0', '0', '7', 'h' }) == null);
try std.testing.expect(std.mem.indexOf(u8, src, &kitty_on) != null);
try std.testing.expect(std.mem.indexOf(u8, src, &wrap_off) != null);
diff --git a/docs/releases/v0.0.255.md b/docs/releases/v0.0.255.md
new file mode 100644
index 00000000..83a1ef49
--- /dev/null
+++ b/docs/releases/v0.0.255.md
@@ -0,0 +1,11 @@
+# v0.0.255 (2026-08-14)
+
+Hotfix: the Grok-style pager was painting leftover mouse CSI
+(`4M39;33;23M…`) into the thinking line whenever the pointer moved.
+
+- Disable any-event mouse (`1003h`). Clicks and wheel still work.
+- A split ESC at the end of a stdin read is no longer treated as Escape.
+- Orphan `b;x;yM` / `<b;x;yM` reports are consumed as mouse events, never
+ inserted as letters.
+
+Bare `graff` stays the line REPL. `graff tui` is the pager.
diff --git a/src/cli.zig b/src/cli.zig
index f1871726..7b60b12e 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.255
+ \\ • TUI no longer dumps mouse CSI (39;33;23M) into the thinking line
+ \\
\\0.0.254
\\ • graff tui is a Grok-style fullscreen pager (composer wrap, tool fold, effort picker, markdown tables); bare graff stays the line REPL
\\ • /import-claude copies Claude/Cursor MCP servers and skills into ~/.codegraff on first start or on demand