Skip to content

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

Python

fix(tui): a resting mouse no longer starves the paste recovery

Rach Pradhan302a02a2a42e
diff --git a/TUI/run.zig b/TUI/run.zig
index 55a0ce78..a63a0304 100644
--- a/TUI/run.zig
+++ b/TUI/run.zig
@@ -88,7 +88,9 @@ pub fn run(
var pending_len: usize = 0;
var esc_stall: u8 = 0;
var zero_reads: u8 = 0;
- var last_input_ms: u64 = 0;
+ // Clock for the bracketed-paste latch only: refreshed by bytes that could
+ // plausibly BE paste content, never by mouse-motion noise (see below).
+ var last_paste_ms: u64 = 0;
var stash_ms: u64 = 0;
var last_hash: u64 = 0;
var saw_gfx: bool = false;
@@ -153,7 +155,7 @@ pub fn run(
// nothing follows, the lone ESC was a real Escape keypress (#94).
const wait: i32 = if (pending_len > 0) 25 else if (m.pending != null or m.bg != null) 50 else 200;
if (!tty.poll(wait)) {
- if (key_mod.inPaste() and m.now_ms -| last_input_ms >= stall.paste_idle_ms) {
+ if (key_mod.inPaste() and m.now_ms -| last_paste_ms >= stall.paste_idle_ms) {
// A `CSI 200~` whose `CSI 201~` never arrives latches the
// composer into literal mode for the rest of the session:
// Enter only inserts a newline, and Escape, Tab and every
@@ -161,7 +163,7 @@ pub fn run(
// has been quiet far longer than any paste keeps streaming
// (#532/#536/#548).
closePaste(&m);
- last_input_ms = m.now_ms;
+ last_paste_ms = m.now_ms;
if (pending_len > 0) {
// Whatever was stuck mid-sequence belongs to the paste
// window this sweep just declared broken — debris, by
@@ -231,7 +233,12 @@ pub fn run(
continue;
}
zero_reads = 0;
- last_input_ms = m.now_ms;
+ // ?1003h is on for image-chip hover, so a pointer merely RESTING over
+ // the terminal emits a motion report roughly twice a second. Counting
+ // those as paste activity postponed the idle sweep above forever: a
+ // wedged paste never released while the mouse sat still anywhere over
+ // the window. Only bytes that could be paste content run the clock.
+ if (!stall.onlyMouseReports(inbuf[pending_len .. pending_len + got])) last_paste_ms = m.now_ms;
// Spends any head the stall path carried: it is glued back on only
// when these bytes really complete it (key_orphan.zig), and only while
// the join can still plausibly be link jitter rather than a human
@@ -442,6 +449,8 @@ test "run loop enables click+hover tracking and bracketed paste" {
const sweep_block = src[sweep_at..stall_at];
try std.testing.expect(std.mem.indexOf(u8, sweep_block, "pending_len = 0;") != null);
try std.testing.expect(std.mem.indexOf(u8, sweep_block, "armOrphan(true)") != null);
+ // A resting mouse must not keep the paste latch alive.
+ try std.testing.expect(std.mem.indexOf(u8, src, "if (!stall.onlyMouseReports(") != null);
}
// The stall-verdict / carry-window battery lives beside the policy it pins,
diff --git a/TUI/run_stall.zig b/TUI/run_stall.zig
index 371eeea9..9324cd0d 100644
--- a/TUI/run_stall.zig
+++ b/TUI/run_stall.zig
@@ -61,6 +61,28 @@ pub fn carryExpired(now_ms: u64, stash_ms: u64) bool {
return now_ms -| stash_ms > carry_window_ms;
}
+/// Is this read nothing but complete SGR mouse reports?
+///
+/// ?1003h is on by default for image-chip hover, and a pointer merely RESTING
+/// over the window makes the terminal emit a motion report roughly twice a
+/// second. Those bytes are not paste content and must not count as paste
+/// activity: while they did, a mouse sitting still over the terminal postponed
+/// the #548 idle recovery indefinitely and an unterminated paste NEVER
+/// released. Deliberately strict — only whole `CSI < params M|m` reports, so a
+/// read that carries any real pasted byte keeps the latch alive.
+pub fn onlyMouseReports(bytes: []const u8) bool {
+ if (bytes.len == 0) return false;
+ var i: usize = 0;
+ while (i < bytes.len) {
+ if (i + 3 > bytes.len or bytes[i] != 0x1b or bytes[i + 1] != '[' or bytes[i + 2] != '<') return false;
+ var j = i + 3;
+ while (j < bytes.len and ((bytes[j] >= '0' and bytes[j] <= '9') or bytes[j] == ';')) : (j += 1) {}
+ if (j >= bytes.len or (bytes[j] != 'M' and bytes[j] != 'm')) return false;
+ i = j + 1;
+ }
+ return true;
+}
+
test "a truncated CSI never becomes Escape or typed debris; a lone ESC still does (#94)" {
const idle: StallCtx = .{};
try std.testing.expectEqual(StallVerdict.wait, stallVerdict("\x1b", 1, idle));
@@ -114,3 +136,18 @@ test "a paste marker is never abandoned on the #94 timescale (#532)" {
try std.testing.expect(isPasteMarkerPrefix("\x1b[201~"));
try std.testing.expect(!isPasteMarkerPrefix("\x1b[202"));
}
+
+test "a resting mouse is not paste activity (#548 starvation)" {
+ // Hover reports under ?1003h arrive while the pointer does not move at all.
+ try std.testing.expect(onlyMouseReports("\x1b[<35;80;24M"));
+ try std.testing.expect(onlyMouseReports("\x1b[<35;80;24M\x1b[<35;80;24M"));
+ try std.testing.expect(onlyMouseReports("\x1b[<0;4;9m"));
+ // Anything that could be paste content keeps the latch's clock running.
+ try std.testing.expect(!onlyMouseReports(""));
+ try std.testing.expect(!onlyMouseReports("hello"));
+ try std.testing.expect(!onlyMouseReports("\x1b[<35;80;24Mhello"));
+ try std.testing.expect(!onlyMouseReports("hello\x1b[<35;80;24M"));
+ try std.testing.expect(!onlyMouseReports("\x1b[<35;80;24")); // split: not complete
+ try std.testing.expect(!onlyMouseReports("\x1b[201~"));
+ try std.testing.expect(!onlyMouseReports("\x1b[A"));
+}