graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(tui): sweep the late debris tails the carry window missed
Rach Pradhan0efdf9bcc695
diff --git a/TUI/key_loop_tests.zig b/TUI/key_loop_tests.zigindex 1d8a8b33..ada86f12 100644--- a/TUI/key_loop_tests.zig+++ b/TUI/key_loop_tests.zig@@ -334,3 +334,62 @@ test "a stale debris arm never eats the token the user types later" {try std.testing.expectEqual(@as(usize, 0), slow.mice);try std.testing.expectEqual(@as(usize, 0), slow.pending);}++test "late debris tails are eaten while armed and typed when they are not" {+ // The head is dropped, the 400ms carry window expires, and the tail lands+ // late: `take` rejected every final outside `M/m/u/~`, so `2A`, `~`, `[A`+ // and a split paste START marker all typed themselves into the composer.+ const tails = [_][]const u8{ "2A", "~", "[A", "[3~", "1;2D", "[H", "[Z", "2F" };+ for (tails) |tail| {+ var loop = Loop.init(std.testing.allocator);+ defer loop.deinit();+ key.armOrphan(true);+ defer key.armOrphan(false);+ try loop.read(tail);+ try std.testing.expectEqualStrings("", loop.typed.items);+ try std.testing.expectEqual(@as(usize, 0), loop.pending);+ }++ // A paste START marker split off its ESC: the marker goes, the body is+ // still text, and the arm is spent on that one fragment.+ var split = Loop.init(std.testing.allocator);+ defer split.deinit();+ key.armOrphan(true);+ try split.read("[200~PASTED");+ try std.testing.expectEqualStrings("PASTED", split.typed.items);+ key.armOrphan(false);++ // Unarmed, every one of those is somebody typing and must arrive intact —+ // this is the half the widened final set must not regress.+ for (tails) |tail| {+ var typed = Loop.init(std.testing.allocator);+ defer typed.deinit();+ try typed.read(tail);+ try std.testing.expectEqualStrings(tail, typed.typed.items);+ try std.testing.expectEqual(@as(usize, 0), typed.mice);+ }+ // ...including the shapes the widening comes closest to: a bare `[` really+ // is the start of typed text when no head was lost.+ var text = Loop.init(std.testing.allocator);+ defer text.deinit();+ try text.read("[12] and 3H at 2:30");+ try std.testing.expectEqualStrings("[12] and 3H at 2:30", text.typed.items);+}++test "an armed sweeper still never swallows a word the user typed" {+ // The widened final set includes `h` and `l` (CSI mode replies), which are+ // also the first letters of ordinary words. They may only ever count+ // BEHIND an orphaned `[` or a parameter run, never bare.+ const words = [_][]const u8{ "hello world", "list the files", "done", "Fix it", "Zoom" };+ for (words) |w| {+ var loop = Loop.init(std.testing.allocator);+ defer loop.deinit();+ try loop.read("\x1b[<35;80");+ loop.stallDrop();+ loop.expireCarry(); // the head is spent; the ARM is what is on trial+ try loop.read(w);+ try std.testing.expectEqualStrings(w, loop.typed.items);+ try std.testing.expectEqual(@as(usize, 0), loop.pending);+ }+ key.armOrphan(false);+}diff --git a/TUI/key_orphan.zig b/TUI/key_orphan.zigindex 64960e4a..5d0d6131 100644--- a/TUI/key_orphan.zig+++ b/TUI/key_orphan.zig@@ -110,6 +110,22 @@ fn isInputFinal(c: u8) bool {};}+/// Finals that count only while `armed`. Cursor/mode replies (`[2A`, `[H`,+/// `[?1003l`) reach the sweeper whenever the carry window expired before the+/// tail landed, and typing `2A` into the composer is exactly the debris this+/// file exists to eat. Unarmed they stay ordinary text — `2A`, `3H` and `1F`+/// are all things people write, and the suite pins them.+fn isArmedFinal(c: u8) bool {+ return switch (c) {+ 'A'...'D', 'F', 'H', 'Z', 'h', 'l' => true,+ else => false,+ };+}++fn isFinal(c: u8, armed_now: bool) bool {+ return c == 'M' or c == 'm' or c == 'u' or c == '~' or (armed_now and isArmedFinal(c));+}+pub const Orphan = union(enum) {/// Not debris — hand the bytes to the normal parser (typed text).none,@@ -127,10 +143,25 @@ pub const Orphan = union(enum) {/// 0x40..0x7e range ate `0x1f` (final `x`), `[12]` (final `]`) and `1e5`/// (final `e`). A run with neither `<` nor a separator (`3u`) is debris only/// while `armed` says the loop really did drop a truncated sequence.+///+/// `armed` also widens the shape, because it means the loop REALLY lost a head+/// and the carry window expired before the tail landed: an orphaned `[`+/// introducer counts, so do cursor/mode finals (`2A`, `[H`, `[200~`), and a+/// bare `~` whose parameters went with the head. Unarmed none of that moves —+/// every one of those strings is something a human types.pub fn take(bytes: []const u8, i: *usize) Orphan {const start = i.*;if (start >= bytes.len) return .none;var j = start;+ // The head that was dropped was a bare `\x1b`, so its `[` now leads the+ // read (`[A`, `[200~PASTED`). Only ever debris while armed: unarmed, `[12]`+ // is somebody typing.+ var lost_csi = false;+ if (armed and bytes[j] == '[') {+ lost_csi = true;+ j += 1;+ if (j >= bytes.len) return .partial;+ }const lt = bytes[j] == '<';if (lt) j += 1;var sep = false;@@ -143,8 +174,18 @@ pub fn take(bytes: []const u8, i: *usize) Orphan {headless = true;j += 1;}- if (j >= bytes.len) return if (headless) .partial else .none;- if (bytes[j] < '0' or bytes[j] > '9') return .none;+ if (j >= bytes.len) return if (headless or lost_csi) .partial else .none;+ if (bytes[j] < '0' or bytes[j] > '9') {+ // No parameters left at all: they went with the head. `[A` (we saw the+ // orphaned `[`) and a lone `~` are debris while armed. A bare letter+ // with no `[` in front of it is NOT — that is the user typing, and+ // eating it would swallow the `h` of `hello`.+ if (armed and ((lost_csi and isFinal(bytes[j], true)) or bytes[j] == '~')) {+ i.* = j + 1;+ return .{ .took = .ignore };+ }+ return .none;+ }var k = j;while (k < bytes.len) : (k += 1) {const c = bytes[k];@@ -153,7 +194,7 @@ pub fn take(bytes: []const u8, i: *usize) Orphan {sep = true;continue;}- if (c == 'M' or c == 'm' or c == 'u' or c == '~') {+ if (isFinal(c, armed)) {if (!lt and !sep and !armed) return .none;i.* = k + 1;// A fragment that opened on a separator lost its leading fields:@@ -179,7 +220,7 @@ pub fn take(bytes: []const u8, i: *usize) Orphan {// Ran off the end mid-fragment. Hold it when it already reads as a CSI// parameter list, or when the loop armed us; a bare unarmed digit run is// somebody typing, and holding it would strand the keystrokes.- return if (lt or sep or armed) .partial else .none;+ return if (lt or sep or armed or lost_csi) .partial else .none;}test "a join only happens when the tail really completes the head" {