Skip to content

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

Python

fix(tui): multiline paste stays one unsent draft (#643)

Cursor Agent2359eda0d2e2
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d6a65f77..b6109f2a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -31,6 +31,10 @@ current is part of cutting a release.
stdio APIs (`graff acp` and `@codegraff/sdk` `Harness`), with a Node
JSON-RPC recipe and a thin `@codegraff/sdk/acp` helper (`spawnAcp` /
`acp`). Not WASM / libgraff. No `v0.0.279` tag until asked.
+- TUI composer: a multiline paste stays one unsent draft. Embedded
+ `\n` / `\r\n` from a bracketed paste (or a wrap-less burst) cannot
+ steer or queue one prompt per line; Enter after the paste submits
+ exactly one (#643).
- Existing Tauri `gui/` is unchanged.
## v0.0.278 (2026-08-26)
diff --git a/TUI/key.zig b/TUI/key.zig
index 1dd5af4b..f6fe8e69 100644
--- a/TUI/key.zig
+++ b/TUI/key.zig
@@ -2,6 +2,7 @@
const std = @import("std");
const orphan = @import("key_orphan.zig");
+const paste = @import("key_paste.zig");
/// Super/alt currently held, from kitty modifier-key events (Ghostty Cmd+Delete
/// often arrives as a bare DEL after Super-down).
@@ -130,7 +131,14 @@ pub fn next(bytes: []const u8, i: *usize) ?Key {
const b = bytes[i.*];
i.* += 1;
if (b == 0x1b) return escapeSeq(bytes, i);
- if (b == 0x0d or b == 0x0a) return .enter;
+ 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 (paste.pasteNewline(bytes, i, b)) |nl| return .{ .char = nl };
+ }
+ return .enter;
+ }
if (b == 0x09) return .tab;
if (b == 0x7f or b == 0x08) {
if (held & 8 != 0) return .delete_to_start;
@@ -474,7 +482,10 @@ fn mapCode(code: u32, mods: u32) Key {
const shift = mods & 1 != 0;
const alt = mods & 2 != 0;
const super = mods & 8 != 0 or held & 8 != 0;
- if (code == 13 or code == 10) return if (shift or alt) .shift_enter else .enter;
+ if (code == 13 or code == 10) {
+ if (in_paste) return .{ .char = '\n' };
+ return if (shift or alt) .shift_enter else .enter;
+ }
if (code == 9) return if (shift) .shift_tab else .tab;
if (code == 27) return .escape;
if (code == 127 or code == 8) {
@@ -556,6 +567,7 @@ pub fn leadingInt(s: []const u8) u32 {
test "#545: 10+-digit CSI params saturate instead of aborting the TUI" {
try std.testing.expectEqual(std.math.maxInt(u32), leadingInt("99999999999999999999"));
try std.testing.expectEqual(std.math.maxInt(u32), leadingInt("9999999999")); // 10 digits — the shortest crasher
+ _ = @import("key_paste.zig");
try std.testing.expectEqual(@as(u32, 4294967295), leadingInt("4294967295")); // exact maxInt still parses
try std.testing.expectEqual(@as(u32, 200), leadingInt("200~tail")); // normal params unchanged
// End-to-end: the fleet's four crashing payloads decode without a panic.
diff --git a/TUI/key_paste.zig b/TUI/key_paste.zig
new file mode 100644
index 00000000..b89e6ab1
--- /dev/null
+++ b/TUI/key_paste.zig
@@ -0,0 +1,57 @@
+//! Multiline paste burst: a single tty read that carries more than one
+//! prompt line. Bracketed paste already latches `in_paste`; this catches
+//! the wrap-less dump (clipboard `\r\n` without `CSI 200~/201~`) so each
+//! newline cannot fire Enter/send (#643).
+
+const std = @import("std");
+
+/// True when `bytes` is a multi-line paste, not a typed line plus Enter.
+/// A lone trailing CR/LF (submit) and a double-Enter mash (force-interrupt)
+/// stay false.
+pub fn looksLikeBurst(bytes: []const u8) bool {
+ var breaks: usize = 0;
+ var text: usize = 0;
+ var text_after_break = false;
+ var seen_break = false;
+ var i: usize = 0;
+ while (i < bytes.len) : (i += 1) {
+ if (bytes[i] == 0x0d) {
+ breaks += 1;
+ seen_break = true;
+ if (i + 1 < bytes.len and bytes[i + 1] == 0x0a) i += 1;
+ } else if (bytes[i] == 0x0a) {
+ breaks += 1;
+ seen_break = true;
+ } else if (bytes[i] >= 32) {
+ text += 1;
+ if (seen_break) text_after_break = true;
+ }
+ }
+ return text_after_break or (breaks >= 2 and text > 0);
+}
+
+/// 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 {
+ if (b != 0x0d and b != 0x0a) return null;
+ if (b == 0x0d and i.* < bytes.len and bytes[i.*] == 0x0a) i.* += 1;
+ return '\n';
+}
+
+test "a typed line plus Enter is not a paste burst" {
+ try std.testing.expect(!looksLikeBurst("hello\r"));
+ try std.testing.expect(!looksLikeBurst("hello\n"));
+ try std.testing.expect(!looksLikeBurst("hello\r\n"));
+ try std.testing.expect(!looksLikeBurst("\r"));
+ try std.testing.expect(!looksLikeBurst("\r\r")); // double-enter force
+}
+
+test "text after a newline is a paste burst (#643)" {
+ try std.testing.expect(looksLikeBurst("line1\nline2"));
+ try std.testing.expect(looksLikeBurst("line1\r\nline2\r\nline3"));
+ try std.testing.expect(looksLikeBurst("line1\nline2\n"));
+}
+
+test "blank-line-only dumps are not a burst" {
+ try std.testing.expect(!looksLikeBurst("\n\n"));
+}
diff --git a/TUI/key_tests.zig b/TUI/key_tests.zig
index 53f31ecc..9cbbb334 100644
--- a/TUI/key_tests.zig
+++ b/TUI/key_tests.zig
@@ -339,3 +339,27 @@ test "the event type is read from the modifier field, not the text field (#549)"
try std.testing.expectEqual(Key.ignore, next("\x1b[1;1:3A", &i).?);
key.held = 0;
}
+
+test "next: embedded paste newlines are chars, not Enter (#643)" {
+ key.resetInputState();
+ defer key.resetInputState();
+ var i: usize = 0;
+ const p = "\x1b[200~a\r\nb\x1b[201~";
+ try std.testing.expectEqual(Key.paste_start, next(p, &i).?);
+ try std.testing.expectEqual(Key{ .char = 'a' }, next(p, &i).?);
+ try std.testing.expectEqual(Key{ .char = '\n' }, next(p, &i).?);
+ try std.testing.expectEqual(Key{ .char = 'b' }, next(p, &i).?);
+ try std.testing.expectEqual(Key.paste_end, next(p, &i).?);
+}
+
+test "next: wrap-less multiline burst is not Enter (#643)" {
+ key.resetInputState();
+ defer key.resetInputState();
+ var i: usize = 0;
+ const p = "a\r\nb";
+ try std.testing.expectEqual(Key{ .char = 'a' }, next(p, &i).?);
+ try std.testing.expectEqual(Key{ .char = '\n' }, next(p, &i).?);
+ try std.testing.expectEqual(Key{ .char = 'b' }, next(p, &i).?);
+ i = 0;
+ try std.testing.expectEqual(Key.enter, next("\r", &i).?);
+}
diff --git a/TUI/keys.zig b/TUI/keys.zig
index 651fac39..25863aec 100644
--- a/TUI/keys.zig
+++ b/TUI/keys.zig
@@ -60,7 +60,7 @@ pub fn handle(self: *Model, k: Key) Effect {
}
if (self.pasting) {
switch (k) {
- .enter => self.input.handle(.{ .char = '\n' }),
+ .enter, .shift_enter => self.input.handle(.{ .char = '\n' }),
.char => |c| self.input.handle(.{ .char = c }),
.codepoint => self.input.handle(k),
// In-band escape hatch. A paste whose `CSI 201~` never arrived
diff --git a/TUI/keys_tests.zig b/TUI/keys_tests.zig
index 3f25bc80..e17909c5 100644
--- a/TUI/keys_tests.zig
+++ b/TUI/keys_tests.zig
@@ -373,3 +373,94 @@ test "Ctrl+U clears chips with the draft (#634)" {
try std.testing.expectEqualStrings("", m.input.getValue());
try std.testing.expectEqual(@as(usize, 0), m.images.items.len);
}
+
+fn pendingTerm() !@import("sim.zig").Term {
+ const Term = @import("sim.zig").Term;
+ var term: Term = undefined;
+ term.init(std.testing.allocator, 80, 24);
+ const job = try std.testing.allocator.create(engine.Job);
+ job.* = .{
+ .gpa = std.testing.allocator,
+ .history = &.{},
+ .params = .{},
+ .stream = .{},
+ .threaded = false,
+ };
+ try term.model.push(.pending, "");
+ term.model.pending = job;
+ return term;
+}
+
+fn dropPending(term: *@import("sim.zig").Term) void {
+ if (term.model.pending) |job| {
+ term.model.pending = null;
+ std.testing.allocator.destroy(job);
+ }
+}
+
+test "idle bracketed multiline paste stays one unsent draft (#643)" {
+ var term: @import("sim.zig").Term = undefined;
+ term.init(std.testing.allocator, 80, 24);
+ defer term.deinit();
+ _ = term.feed("\x1b[200~line1\nline2\nline3\x1b[201~");
+ try std.testing.expectEqualStrings("line1\nline2\nline3", term.model.input.getValue());
+ try std.testing.expectEqual(@as(usize, 0), term.model.steer_queue.items.len);
+ var users: usize = 0;
+ for (term.model.history.items) |e| {
+ if (e.kind == .user) users += 1;
+ }
+ try std.testing.expectEqual(@as(usize, 0), users);
+ _ = term.enter();
+ try std.testing.expectEqualStrings("", term.model.input.getValue());
+ users = 0;
+ for (term.model.history.items) |e| {
+ if (e.kind == .user) users += 1;
+ }
+ try std.testing.expectEqual(@as(usize, 1), users);
+ try std.testing.expectEqualStrings("line1\nline2\nline3", term.model.history.items[0].text);
+}
+
+test "streaming bracketed paste does not steer, Enter queues one (#643)" {
+ var term = try pendingTerm();
+ defer {
+ dropPending(&term);
+ term.deinit();
+ }
+ _ = term.feed("\x1b[200~first\nsecond\nthird\x1b[201~");
+ try std.testing.expectEqualStrings("first\nsecond\nthird", term.model.input.getValue());
+ try std.testing.expectEqual(@as(usize, 0), term.model.steer_queue.items.len);
+ try std.testing.expect(!term.model.cancel_requested);
+ _ = term.enter();
+ try std.testing.expectEqual(@as(usize, 1), term.model.steer_queue.items.len);
+ try std.testing.expectEqualStrings("first\nsecond\nthird", term.model.steer_queue.items[0]);
+ try std.testing.expectEqualStrings("", term.model.input.getValue());
+}
+
+test "bracketed CRLF paste cannot invoke Enter/send (#643)" {
+ var term = try pendingTerm();
+ defer {
+ dropPending(&term);
+ term.deinit();
+ }
+ _ = term.feed("\x1b[200~one\r\ntwo\r\nthree\x1b[201~");
+ try std.testing.expectEqualStrings("one\ntwo\nthree", term.model.input.getValue());
+ try std.testing.expectEqual(@as(usize, 0), term.model.steer_queue.items.len);
+ try std.testing.expect(!term.model.cancel_requested);
+ var forced: usize = 0;
+ for (term.model.history.items) |e| {
+ if (std.mem.eql(u8, e.text, "↳ force › interrupting…")) forced += 1;
+ }
+ try std.testing.expectEqual(@as(usize, 0), forced);
+}
+
+test "wrap-less CRLF dump while streaming stays one draft (#643)" {
+ var term = try pendingTerm();
+ defer {
+ dropPending(&term);
+ term.deinit();
+ }
+ _ = term.feed("alpha\r\nbeta\r\ngamma");
+ try std.testing.expectEqualStrings("alpha\nbeta\ngamma", term.model.input.getValue());
+ try std.testing.expectEqual(@as(usize, 0), term.model.steer_queue.items.len);
+ try std.testing.expect(!term.model.cancel_requested);
+}
diff --git a/TUI/root.zig b/TUI/root.zig
index bc3a71ac..68bdc96a 100644
--- a/TUI/root.zig
+++ b/TUI/root.zig
@@ -57,6 +57,7 @@ test {
_ = @import("prompt_history.zig");
_ = @import("meters.zig");
_ = @import("key.zig");
+ _ = @import("key_paste.zig");
_ = @import("key_orphan.zig");
_ = @import("key_tests.zig");
_ = @import("spec_terminal_modes_conformance.zig");
diff --git a/docs/releases/v0.0.279.md b/docs/releases/v0.0.279.md
index 6d317a72..3cefa6b7 100644
--- a/docs/releases/v0.0.279.md
+++ b/docs/releases/v0.0.279.md
@@ -89,9 +89,18 @@ pool, counts the suite and the named invariants by scanning the
unfiltered test artifact (no filtered recompiles, no second suite
execution on a zig cache hit), and accepts `--only tui,tuiguard`.
+## Multiline paste stays one draft (#643)
+
+Pasting several newline-separated lines into the TUI composer — including
+while a turn is streaming — inserts them as one unsent draft. Embedded
+`\n` / `\r\n` from a bracketed paste (or a wrap-less burst in the same
+read) cannot invoke Enter/send, so they no longer steer the live turn or
+queue one prompt per line. An explicit Enter after the paste submits or
+queues exactly one user prompt.
+
## Tests
Unit suite grows by the ACP translator cases (`kindFor`, thought/text
chunks, minted tool ids, EventSink) plus the #638 picker/retire,
-#640 deadline, and #581 pin-budget cases. Floor stays **1683** from
-the 278 ratchet (slack 25).
+#640 deadline, #581 pin-budget, and #643 paste-boundary cases. Floor
+stays **1683** from the 278 ratchet (slack 25).