graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix: preserve pasted newlines and clean up job completion state
Rach Pradhanfbf6aa4026b6
diff --git a/TUI/issue_737_tests.zig b/TUI/issue_737_tests.zigindex c80e1180..6a062b9a 100644--- a/TUI/issue_737_tests.zig+++ b/TUI/issue_737_tests.zig@@ -39,7 +39,7 @@ test "#737 long bracketed single/LF/CRLF paste across reads and batches stays unfor (0..33) |p| {if (p != 0) {try payload.appendSlice(a, ending);- if (ending.len != 0) try expected.append(a, '\n');+ try expected.appendSlice(a, ending);}try payload.appendSlice(a, ¶graph);try expected.appendSlice(a, ¶graph);diff --git a/TUI/key.zig b/TUI/key.zigindex 7b1f11a0..fbe995ad 100644--- a/TUI/key.zig+++ b/TUI/key.zig@@ -144,7 +144,8 @@ pub fn next(bytes: []const u8, i: *usize) ?Key {if (b == 0x0d or b == 0x0a) {// Newlines inside a paste are text, not the Enter/send key: bracketed,// a multiline dump in this read, or a run spanning reads (#643/#737).- if (in_paste or (!paste_ended_in_read and (paste.burstActive() or paste.looksLikeBurst(bytes)))) {+ if (in_paste) return .{ .char = b }; // preserve bracketed CRLF across reads (#737)+ if (!paste_ended_in_read and (paste.burstActive() or paste.looksLikeBurst(bytes))) {if (paste.pasteNewline(bytes, i, b)) |nl| return .{ .char = nl };}return .enter;diff --git a/TUI/key_tests.zig b/TUI/key_tests.zigindex 6b1261cb..0070496a 100644--- a/TUI/key_tests.zig+++ b/TUI/key_tests.zig@@ -362,6 +362,7 @@ test "next: embedded paste newlines are chars, not Enter (#643)" {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 = '\r' }, 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).?);diff --git a/TUI/keys_tests.zig b/TUI/keys_tests.zigindex e17909c5..f99a0465 100644--- a/TUI/keys_tests.zig+++ b/TUI/keys_tests.zig@@ -443,7 +443,7 @@ test "bracketed CRLF paste cannot invoke Enter/send (#643)" {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.expectEqualStrings("one\r\ntwo\r\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;@@ -453,6 +453,62 @@ test "bracketed CRLF paste cannot invoke Enter/send (#643)" {try std.testing.expectEqual(@as(usize, 0), forced);}+test "#737 streaming large single paragraph crosses real read and batch boundaries" {+ try streamingPaste737("a long single paragraph ", 2500);+}++test "#737 streaming multiline paste preserves LF CRLF and trailing newline across reads" {+ try streamingPaste737("first paragraph\n\nsecond paragraph\r\n", 1800);+}++fn streamingPaste737(unit: []const u8, repeats: usize) !void {+ var term = try pendingTerm();+ defer {+ dropPending(&term);+ term.deinit();+ }+ const a = std.testing.allocator;+ const body = try a.alloc(u8, unit.len * repeats);+ defer a.free(body);+ for (0..repeats) |j| @memcpy(body[j * unit.len ..][0..unit.len], unit);+ // Both markers arrive split, with enough body to overflow the production+ // 16KiB input read and 64-event batch many times. No timing/model calls.+ for ([_][]const u8{ "\x1b", "[2", "00~" }) |part| _ = term.feed(part);+ var at: usize = 0;+ while (at < body.len) {+ const end = @min(at + 16381, body.len);+ _ = term.feed(body[at..end]);+ try std.testing.expectEqualStrings(body[0..end], 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);+ for (term.model.history.items) |entry| try std.testing.expect(entry.kind != .user);+ at = end;+ }+ for ([_][]const u8{ "\x1b[", "20", "1~" }) |part| _ = term.feed(part);+ try std.testing.expectEqualStrings(body, term.model.input.getValue());+ try std.testing.expectEqual(@as(usize, 0), term.model.steer_queue.items.len);+ _ = term.enter();+ try std.testing.expectEqual(@as(usize, 1), term.model.steer_queue.items.len);+ // steerEnter trims surrounding whitespace, so don't confuse its existing+ // submit policy with the lossless unsent draft assertion above.+ try std.testing.expectEqualStrings(std.mem.trim(u8, body, " \t\r\n"), term.model.steer_queue.items[0]);+ try std.testing.expectEqualStrings("", term.model.input.getValue());+}++test "#737 delayed paste_end dispatch cannot reset a subsequent decoded paste" {+ var term = try pendingTerm();+ defer {+ dropPending(&term);+ term.deinit();+ }+ _ = term.feed("\x1b[200~first\x1b[201~\x1b[200~second");+ _ = term.feed("\r");+ _ = term.feed("\nthird\x1b[201~");+ try std.testing.expectEqualStrings("firstsecond\r\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);+}+test "wrap-less CRLF dump while streaming stays one draft (#643)" {var term = try pendingTerm();defer {diff --git a/src/jobs.zig b/src/jobs.zigindex ab69ca00..dd939f32 100644--- a/src/jobs.zig+++ b/src/jobs.zig@@ -205,12 +205,11 @@ fn jobPump(job: *Job, gpa: Allocator, io: Io) void {job.killed = killed and !detached;job.done = true;const id = job.id;- const cmd = job.cmd;const quiet = job.quiet;const idle = job.stopped_idle;// Publish done + queue atomically against jobOutput/jobKill consuming it.// A bounded dismissed-id cache cannot close a queue-after-unlock race.- if (!quiet and !detached) job_notify.queue(io, id, code, killed, cmd, idle);+ if (!quiet and !detached) job_notify.queue(io, id, code, killed, job.cmd, idle);g_jobs.mutex.unlock(io);if (detached) return;if (builtin.is_test) if (completion_test_hook) |hook| hook(io, id);@@ -385,6 +384,7 @@ pub fn jobOutput(gpa: Allocator, io: Io, id: u32, wait_ms: u64) !ToolOutput {job.last_active_ms = nowMs(io); // a read or a blocking wait is activity (#199)const fresh = job.buf.items[job.cursor..];if (job.done or interrupted or waited >= deadline) {+ errdefer g_jobs.mutex.unlock(io);var aw: Io.Writer.Allocating = .init(gpa);errdefer aw.deinit();const w = &aw.writer;@@ -409,10 +409,11 @@ pub fn jobOutput(gpa: Allocator, io: Io, id: u32, wait_ms: u64) !ToolOutput {} else {try w.writeAll("\n(no new output)");}+ const text = try aw.toOwnedSlice();job.cursor = job.buf.items.len;if (job.done) job_notify.dismiss(io, id); // the exit was just read here; no wake for it (ADR 0061)g_jobs.mutex.unlock(io);- return .{ .text = try aw.toOwnedSlice() };+ return .{ .text = text };}g_jobs.mutex.unlock(io);if (Agent.esc_cancel.load(.acquire)) {@@ -518,6 +519,7 @@ pub fn waitForeground(gpa: Allocator, io: Io, id: u32, wait_ms: u64) !FgWait {g_jobs.mutex.unlock(io);return error.OutOfMemory;};+ job_notify.dismiss(io, id);const result: FgWait = .{ .done = .{ .exit_code = job.exit_code, .killed = job.killed, .output = pair[0], .dropped = pair[1] } };g_jobs.mutex.unlock(io);return result;@@ -559,7 +561,6 @@ pub fn waitForeground(gpa: Allocator, io: Io, id: u32, wait_ms: u64) !FgWait {}}-/// Session end: kill every job, await pumps, free. From main's defer.pub fn jobsReap(gpa: Allocator, io: Io) void {g_jobs.mutex.lockUncancelable(io);const jobs = g_jobs.list.toOwnedSlice(gpa) catch {diff --git a/src/jobs_tests.zig b/src/jobs_tests.zigindex 25bfe6e2..08b69133 100644--- a/src/jobs_tests.zig+++ b/src/jobs_tests.zig@@ -13,6 +13,32 @@ test {_ = @import("jobs_completion_tests.zig");}+test "#728 consumed successful blocking output cannot inject a late completion" {+ if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;+ const notify = @import("job_notify.zig");+ const Agent = @import("agent.zig").Agent;+ const io = std.testing.io;+ const gpa = std.testing.allocator;+ jobs.g_jobs = .{};+ Agent.esc_cancel.store(false, .release);+ var wake: [512]u8 = undefined;+ while (notify.takeWake(io, &wake) != null) {}+ defer jobs.jobsReap(gpa, io);+ // More than the notice capacity: correctness must not depend on a+ // bounded cache remembering consumed ids while pump publication lags.+ for (0..24) |_| {+ const id = (try jobs.spawnJob(gpa, io, "printf success; exit 0")).id;+ const output = try jobs.jobOutput(gpa, io, id, 1);+ defer gpa.free(output.text);+ try std.testing.expect(!output.is_error);+ try std.testing.expect(std.mem.indexOf(u8, output.text, "exited with code 0") != null);+ try std.testing.expect(std.mem.indexOf(u8, output.text, "success") != null);+ jobs.reapFinished(gpa, io, id); // await pump, including late publication+ try std.testing.expect(notify.takeWake(io, &wake) == null);+ try std.testing.expect(!Agent.esc_cancel.load(.acquire));+ }+}+test "foreground tool subprocesses own their process group (#266, #198)" {const inherited = jobs.toolRunOptions(null);try std.testing.expect(inherited.kill_process_tree);diff --git a/src/readline.zig b/src/readline.zigindex c8a053e3..d07f201d 100644--- a/src/readline.zig+++ b/src/readline.zig@@ -439,8 +439,7 @@ pub fn readLine(break;}}- var pasted = blob.items;- if (pasted.len > 0 and pasted[pasted.len - 1] == '\n') pasted = pasted[0 .. pasted.len - 1];+ const pasted = blob.items; // preserve the complete draft, including trailing LF/CRLF (#737)const lines = std.mem.count(u8, pasted, "\n") + 1;const dropped = cleanDroppedPath(gpa, root.home, pasted);defer if (dropped) |dp| gpa.free(dp);