Skip to content

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

Python

fix(tui): the idle paste sweep no longer fires a phantom Escape

Rach Pradhan86f9223a4c23
diff --git a/TUI/run.zig b/TUI/run.zig
index d0965213..55a0ce78 100644
--- a/TUI/run.zig
+++ b/TUI/run.zig
@@ -162,6 +162,22 @@ pub fn run(
// (#532/#536/#548).
closePaste(&m);
last_input_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
+ // definition. Handing it back to the stall path let it be
+ // re-classified as a KEY: a lone pending ESC (the head of
+ // the `CSI 201~` that never came) became the Escape key the
+ // instant the sweep cleared `in_paste`, cancelling a live
+ // turn and wiping the composer with no user keypress at
+ // all. Carry it for a late tail, arm the sweeper for a
+ // headless one, and never let it become a keystroke.
+ key_mod.stashOrphanHead(inbuf[0..pending_len]);
+ stash_ms = m.now_ms;
+ key_mod.armOrphan(true);
+ pending_len = 0;
+ esc_stall = 0;
+ }
}
if (pending_len > 0) {
esc_stall +|= 1;
@@ -417,6 +433,15 @@ test "run loop enables click+hover tracking and bracketed paste" {
// #517: a buffer-filling parser wedge must be cleared before the read,
// or the zero-length read reads as a hangup and kills the TUI.
try std.testing.expect(std.mem.indexOf(u8, src, "pending_len == inbuf.len") != null);
+ // The idle paste sweep must DISCARD whatever was stuck mid-sequence before
+ // the stall path below can see it. Leaving it there let a lone pending ESC
+ // become the Escape KEY the instant `in_paste` cleared, cancelling a live
+ // turn and wiping the composer with no keypress at all.
+ const sweep_at = std.mem.indexOf(u8, src, "closePaste(&m);").?;
+ const stall_at = std.mem.indexOfPos(u8, src, sweep_at, "esc_stall +|= 1").?;
+ 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);
}
// The stall-verdict / carry-window battery lives beside the policy it pins,
diff --git a/TUI/sim.zig b/TUI/sim.zig
index b961dd57..ec3597dc 100644
--- a/TUI/sim.zig
+++ b/TUI/sim.zig
@@ -95,6 +95,21 @@ pub const Term = struct {
}
}
+ /// run.zig's `paste_idle_ms` sweep: the terminal has gone quiet far longer
+ /// than any paste keeps streaming, so the latch is force-closed. Anything
+ /// still stuck mid-sequence belongs to the paste window this just declared
+ /// broken — it is carried as DEBRIS and never handed back to the stall
+ /// path, which would re-classify it as a key.
+ pub fn idlePasteSweep(self: *Term) void {
+ if (!key_mod.inPaste()) return;
+ key_mod.endPaste();
+ _ = keys.handle(&self.model, .paste_end);
+ if (self.pending == 0) return;
+ key_mod.stashOrphanHead(self.inbuf[0..self.pending]);
+ self.pending = 0;
+ key_mod.armOrphan(true);
+ }
+
pub fn press(self: *Term, k: Key) Effect {
const last = keys.handle(&self.model, k);
self.last_effect = last;
@@ -347,3 +362,48 @@ test "giving up on a split paste terminator closes the paste, tail and all (#532
_ = term.feed("~");
try std.testing.expectEqualStrings("hello", term.model.input.getValue());
}
+
+test "the idle paste sweep never fires a phantom Escape at a live turn" {
+ const engine = @import("engine.zig");
+ var term: Term = undefined;
+ term.init(std.testing.allocator, 80, 24);
+ defer term.deinit();
+ defer key_mod.armOrphan(false);
+ try term.model.push(.user, "!sleep 30");
+ const job = try std.testing.allocator.create(engine.Job);
+ defer std.testing.allocator.destroy(job);
+ job.* = .{
+ .gpa = std.testing.allocator,
+ .history = &.{},
+ .params = .{},
+ .stream = .{},
+ .threaded = false,
+ };
+ try term.model.push(.pending, "");
+ term.model.pending = job;
+ defer term.model.pending = null;
+
+ // A paste opens, its terminator is cut in flight, and the ESC that opened
+ // `CSI 201~` is left sitting in the pending buffer.
+ _ = term.feed("\x1b[200~draft text");
+ _ = term.feed("\x1b");
+ try std.testing.expect(key_mod.inPaste());
+ try std.testing.expectEqual(@as(usize, 1), term.pending);
+
+ // Two seconds of quiet: the sweep closes the paste. The pending ESC must
+ // go with it. Handing it back to the stall path re-classified it as the
+ // Escape KEY the instant `in_paste` cleared, cancelling the live turn at
+ // t=2.03s with no user keypress at all — and the same path wiped a
+ // composer that had a draft in it.
+ term.idlePasteSweep();
+ try std.testing.expect(!key_mod.inPaste());
+ try std.testing.expect(!term.model.pasting);
+ try std.testing.expect(!term.model.cancel_requested);
+ try std.testing.expectEqual(@as(usize, 0), term.pending);
+ try std.testing.expectEqualStrings("draft text", term.model.input.getValue());
+
+ // ...and the late terminator still rejoins its carried head rather than
+ // typing `[201~` on the end of the draft.
+ _ = term.feed("[201~");
+ try std.testing.expectEqualStrings("draft text", term.model.input.getValue());
+}