Skip to content

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

Python

fix(tui): one paste stays one prompt when it spans several reads (#737)

justrache4beb1c3fd2d
diff --git a/TUI/key.zig b/TUI/key.zig
index cf864504..4f07f1d7 100644
--- a/TUI/key.zig
+++ b/TUI/key.zig
@@ -18,6 +18,15 @@ pub fn inPaste() bool {
return in_paste;
}
+/// Set by the read loop for reads that belong to a paste spanning several
+/// tty reads. `looksLikeBurst` only sees the read in front of it, so without
+/// this a chunk ending at a line boundary submits mid-paste (#737).
+var burst_read: bool = false;
+
+pub fn setBurstRead(on: bool) void {
+ burst_read = on;
+}
+
/// Close a bracketed paste and clear modifiers whose events were ignored
/// inside it. The read loop also calls this when `CSI 201~` never arrived —
/// without it the latch is permanent and every Enter, Escape and slash command
@@ -57,6 +66,8 @@ pub fn abandonSequence(bytes: []const u8, recovery: SequenceRecovery) void {
pub fn resetInputState() void {
abandonSequence("", .none);
in_paste = false;
+ burst_read = false;
+ paste.resetBurst();
orphan.reset();
}
@@ -147,7 +158,7 @@ pub fn next(bytes: []const u8, i: *usize) ?Key {
if (b == 0x0d or b == 0x0a) {
// Embedded newlines from a bracketed paste — or a wrap-less multiline
// dump in this same read — must not become the Enter/send key (#643).
- if (in_paste or paste.looksLikeBurst(bytes)) {
+ if (in_paste or burst_read or paste.looksLikeBurst(bytes)) {
if (paste.pasteNewline(bytes, i, b)) |nl| return .{ .char = nl };
}
return .enter;
diff --git a/TUI/key_paste.zig b/TUI/key_paste.zig
index b89e6ab1..7e37cb06 100644
--- a/TUI/key_paste.zig
+++ b/TUI/key_paste.zig
@@ -30,6 +30,40 @@ pub fn looksLikeBurst(bytes: []const u8) bool {
return text_after_break or (breaks >= 2 and text > 0);
}
+/// A paste larger than one tty read arrives as several reads back to back.
+/// `looksLikeBurst` only ever sees one of them, so a chunk that happens to
+/// end at a line boundary looks like a typed line plus Enter and submits
+/// mid-paste — one pasted message became several prompts (#737, a return of
+/// #643 by another route). A burst therefore latches for a short window, and
+/// every read that still looks like one pushes the window out.
+var burst_until_ms: u64 = 0;
+
+/// Reads this close together are the same paste. Terminals deliver the
+/// chunks with no gap; a person reaching for Enter takes far longer.
+pub const burst_window_ms: u64 = 150;
+
+pub fn armBurst(now_ms: u64) void {
+ burst_until_ms = now_ms + burst_window_ms;
+}
+
+pub fn inBurst(now_ms: u64) bool {
+ return now_ms < burst_until_ms;
+}
+
+pub fn resetBurst() void {
+ burst_until_ms = 0;
+}
+
+/// True when this read is part of a paste: it looks like one on its own, or
+/// it arrived inside the window opened by the read before it. Arms the
+/// window either way so the next chunk is covered too.
+pub fn burstRead(bytes: []const u8, now_ms: u64) bool {
+ const carried = inBurst(now_ms);
+ if (!looksLikeBurst(bytes) and !carried) return false;
+ armBurst(now_ms);
+ return true;
+}
+
/// CR/LF inside a bracketed paste or a wrap-less burst is a literal newline,
/// never the Enter/send key. Collapses `\r\n` to one `\n`.
pub fn pasteNewline(bytes: []const u8, i: *usize, b: u8) ?u8 {
@@ -55,3 +89,28 @@ test "text after a newline is a paste burst (#643)" {
test "blank-line-only dumps are not a burst" {
try std.testing.expect(!looksLikeBurst("\n\n"));
}
+
+test "a paste split across reads stays one prompt (#737)" {
+ resetBurst();
+ // The terminal hands over a long paste in chunks. The first is plainly a
+ // burst; the second ends at a line boundary and, judged alone, reads as a
+ // typed line plus Enter — which is what submitted mid-paste.
+ try std.testing.expect(!looksLikeBurst("tail of the paragraph\n"));
+ try std.testing.expect(burstRead("first paragraph\nsecond paragraph\n", 1_000));
+ try std.testing.expect(burstRead("tail of the paragraph\n", 1_010));
+ try std.testing.expect(burstRead("and the last line\n", 1_100));
+}
+
+test "a typed Enter after the paste settles still sends" {
+ resetBurst();
+ try std.testing.expect(burstRead("pasted line one\npasted line two\n", 1_000));
+ // Long enough after the last chunk to be a person, not the terminal.
+ try std.testing.expect(!burstRead("now my own line\n", 1_000 + burst_window_ms + 1));
+}
+
+test "an ordinary typed line never opens the window" {
+ resetBurst();
+ try std.testing.expect(!burstRead("hello\r", 5_000));
+ try std.testing.expect(!inBurst(5_000));
+ try std.testing.expect(!burstRead("\r", 5_010));
+}
diff --git a/TUI/run.zig b/TUI/run.zig
index d189bb48..9d36897e 100644
--- a/TUI/run.zig
+++ b/TUI/run.zig
@@ -9,6 +9,7 @@ const bgop = @import("bgop.zig");
const engine = @import("engine.zig");
const key_mod = @import("key.zig");
const keys = @import("keys.zig");
+const paste_mod = @import("key_paste.zig");
const pacing = @import("pacing.zig");
const paint_mod = @import("paint.zig");
const render_mod = @import("render.zig");
@@ -372,6 +373,10 @@ pub fn run(
if (stall.escapeCarryExpired(m.now_ms, stash_ms)) key_mod.expireOrphanHead();
if (stall.armExpired(m.now_ms, arm_ms)) key_mod.armOrphan(false);
const n = key_mod.joinOrphanHead(&inbuf, filled);
+ // A paste bigger than one read arrives as several back to back. Judge
+ // the whole run, not each chunk: otherwise a chunk that ends at a line
+ // boundary reads as Enter and sends half the paste (#737).
+ key_mod.setBurstRead(paste_mod.burstRead(inbuf[0..n], m.now_ms));
// Everything this tick drained is applied as ONE batch, with runs of
// consecutive wheel reports folded into a single accumulated delta
// (pacing.zig). Order is preserved and a keystroke BREAKS the run at