Skip to content

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

Python

fix(tui): bound the orphan-debris arm on its own clock

Rach Pradhan774193a68022
diff --git a/TUI/key_loop_tests.zig b/TUI/key_loop_tests.zig
index 7a1a2431..1d8a8b33 100644
--- a/TUI/key_loop_tests.zig
+++ b/TUI/key_loop_tests.zig
@@ -67,6 +67,20 @@ const Loop = struct {
self.paste_ends += 1;
}
}
+
+ /// run.zig's carry window elapsing (`stall.carryExpired`): the head is
+ /// spent before the read, so it can no longer glue itself onto anything.
+ fn expireCarry(_: *Loop) void {
+ key.stashOrphanHead("");
+ }
+
+ /// run.zig's arm window elapsing (`stall.armExpired`): the loop disarms the
+ /// sweeper before parsing, because a claim that a head was "just" dropped
+ /// goes stale. Strictly the longer of the two windows.
+ fn expireArm(self: *Loop) void {
+ self.expireCarry();
+ key.armOrphan(false);
+ }
};
test "SGR motion flood chopped at every byte offset never types a character" {
@@ -292,3 +306,31 @@ test "a real multi-read paste is untouched by any of the recovery paths" {
try std.testing.expectEqual(@as(usize, 0), loop.mice);
try std.testing.expect(!key.inPaste());
}
+
+test "a stale debris arm never eats the token the user types later" {
+ // The arm had no clock at all, so it stayed live until something happened
+ // to consume it: `3u apples` reached the composer as ` apples` twelve
+ // seconds after the head was dropped, from a keyboard (Alt+[ on a
+ // meta-sending terminal is enough to lose a head).
+ var loop = Loop.init(std.testing.allocator);
+ defer loop.deinit();
+ try loop.read("\x1b[<35;80");
+ loop.stallDrop();
+ loop.expireArm(); // ~1s later: the claim is stale
+ try loop.read("3u apples");
+ try std.testing.expectEqualStrings("3u apples", loop.typed.items);
+ try std.testing.expectEqual(@as(usize, 0), loop.mice);
+ try std.testing.expectEqual(@as(usize, 0), loop.pending);
+
+ // Human-speed typing lost even more, through the `.partial` hold: `2m
+ // later` arrived as `m later` with the `2m` swallowed as a mouse report.
+ var slow = Loop.init(std.testing.allocator);
+ defer slow.deinit();
+ try slow.read("\x1b[<35;80");
+ slow.stallDrop();
+ slow.expireArm();
+ for ("2m later") |c| try slow.read(&[_]u8{c});
+ try std.testing.expectEqualStrings("2m later", slow.typed.items);
+ try std.testing.expectEqual(@as(usize, 0), slow.mice);
+ try std.testing.expectEqual(@as(usize, 0), slow.pending);
+}
diff --git a/TUI/run.zig b/TUI/run.zig
index a63a0304..9c738ab6 100644
--- a/TUI/run.zig
+++ b/TUI/run.zig
@@ -92,6 +92,7 @@ pub fn run(
// plausibly BE paste content, never by mouse-motion noise (see below).
var last_paste_ms: u64 = 0;
var stash_ms: u64 = 0;
+ var arm_ms: u64 = 0;
var last_hash: u64 = 0;
var saw_gfx: bool = false;
var prev: []u8 = &.{};
@@ -177,6 +178,7 @@ pub fn run(
key_mod.stashOrphanHead(inbuf[0..pending_len]);
stash_ms = m.now_ms;
key_mod.armOrphan(true);
+ arm_ms = m.now_ms;
pending_len = 0;
esc_stall = 0;
}
@@ -207,6 +209,7 @@ pub fn run(
key_mod.stashOrphanHead(inbuf[0..pending_len]);
stash_ms = m.now_ms;
key_mod.armOrphan(true);
+ arm_ms = m.now_ms;
pending_len = 0;
esc_stall = 0;
closePaste(&m);
@@ -242,8 +245,11 @@ pub fn run(
// 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
- // resuming typing.
+ // resuming typing. The debris ARM is bounded on the same principle —
+ // left latched it ate the first token of whatever was typed next, at
+ // any later time.
if (stall.carryExpired(m.now_ms, stash_ms)) key_mod.stashOrphanHead("");
+ if (stall.armExpired(m.now_ms, arm_ms)) key_mod.armOrphan(false);
const n = key_mod.joinOrphanHead(&inbuf, pending_len + got);
var i: usize = 0;
while (key_mod.next(inbuf[0..n], &i)) |k| {
@@ -449,8 +455,10 @@ 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.
+ // Both unbounded holds are bounded: a resting mouse must not keep the paste
+ // latch alive, and the debris arm must go stale on its own clock.
try std.testing.expect(std.mem.indexOf(u8, src, "if (!stall.onlyMouseReports(") != null);
+ try std.testing.expect(std.mem.indexOf(u8, src, "if (stall.armExpired(") != 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 9324cd0d..656e4364 100644
--- a/TUI/run_stall.zig
+++ b/TUI/run_stall.zig
@@ -32,6 +32,13 @@ pub const paste_idle_ms: u64 = 2000;
/// that the next bytes are a human typing, and `\x1b[` + `Hello` would eat the
/// H (`CSI H` is a legal Home).
const carry_window_ms: u64 = 400;
+/// How long the orphan-debris sweeper stays armed. The arm says "the loop
+/// really did drop a head just now", and that claim goes stale: latched with no
+/// clock at all it ate the first token of whatever the user typed NEXT, at any
+/// later time (`3u apples` -> ` apples` twelve seconds after the drop). Slightly
+/// longer than the carry window, because a `.partial` fragment legitimately
+/// spans a read boundary or two before it completes.
+const arm_window_ms: u64 = 1000;
/// What to do with input bytes stuck mid-sequence after quiet polls (~25ms
/// each). Exactly one pending ESC byte is the Escape key after the #94 grace.
@@ -61,6 +68,10 @@ pub fn carryExpired(now_ms: u64, stash_ms: u64) bool {
return now_ms -| stash_ms > carry_window_ms;
}
+pub fn armExpired(now_ms: u64, arm_ms: u64) bool {
+ return now_ms -| arm_ms > arm_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
@@ -121,6 +132,18 @@ test "the carried head expires before it can reach a human keystroke (#530)" {
try std.testing.expect(carryExpired(100_000, 0));
}
+test "the debris arm expires too, so it can never eat a later keystroke" {
+ // Unbounded, the arm swallowed the first token of whatever was typed next
+ // at ANY later time — `3u apples` reached the composer as ` apples` twelve
+ // seconds after the head was dropped.
+ try std.testing.expect(!armExpired(1000, 1000));
+ try std.testing.expect(!armExpired(2000, 1000));
+ try std.testing.expect(armExpired(2001, 1000));
+ try std.testing.expect(armExpired(13_000, 1000));
+ // Outlives the carry window: a `.partial` fragment may span a read or two.
+ try std.testing.expect(carryExpired(1600, 1000) and !armExpired(1600, 1000));
+}
+
test "a paste marker is never abandoned on the #94 timescale (#532)" {
// Both give-up routes strand `in_paste` forever, so every prefix of either
// marker gets ~2s — including the lone ESC, which must NOT become Escape.