Skip to content

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

Python

Ctrl-V screenshot paste is not killed by a leftover Esc

justrach6b16d4404d9d
diff --git a/src/clipboard_native.zig b/src/clipboard_native.zig
index 3bf70e6a..e0196b6a 100644
--- a/src/clipboard_native.zig
+++ b/src/clipboard_native.zig
@@ -8,7 +8,20 @@ pub const script = @embedFile("clipboard_export.js");
pub fn grab(io: std.Io, gpa: std.mem.Allocator, board: []const u8) clip.GrabAttempt {
if (@import("builtin").os.tag != .macos) return .empty;
const path = clip.tempPath(io, gpa, "png") orelse return .{ .failed = .extract };
- return grabWithRunner(io, gpa, board, path, runner.runCapped);
+ return grabWithRunner(io, gpa, board, path, runHelper);
+}
+
+/// osascript must outlive a leftover Esc. `runCapped` shares `Agent.esc_cancel`
+/// with bash; a prior cancel classified every screenshot paste as unavailable.
+fn runHelper(
+ gpa: std.mem.Allocator,
+ io: std.Io,
+ argv: []const []const u8,
+ stdout_cap: usize,
+ stderr_cap: usize,
+ deadline_ms: u64,
+) !runner.CappedRun {
+ return runner.runCappedWithOptions(gpa, io, argv, stdout_cap, stderr_cap, deadline_ms, .{ .ignore_cancel = true });
}
/// Same extraction path with an injected process boundary for failure tests.
diff --git a/src/process_runner.zig b/src/process_runner.zig
index 43480bf2..bd7fe592 100644
--- a/src/process_runner.zig
+++ b/src/process_runner.zig
@@ -108,6 +108,9 @@ pub const CappedRunOptions = struct {
/// Schedule overrides (tests run the real thresholds in milliseconds).
pulse_first_ms: u64 = tool_pulse.first_silence_ms,
pulse_interval_ms: u64 = tool_pulse.silence_interval_ms,
+ /// Clipboard export is not a user turn. Esc would kill osascript and
+ /// Ctrl-V would report `clipboard helper unavailable` (#1146).
+ ignore_cancel: bool = false,
};
/// #253: RLIMIT_NOFILE as this process actually observes it. main() calls
@@ -185,7 +188,7 @@ fn terminateTree(child: *std.process.Child, io: Io, job: *WindowsJobHandle, grou
// A canceled POSIX wait closes the pipes and clears id without reaping.
// Retain the id so the caller can still kill/reap after a deadline or Esc.
-fn waitForExit(child: *std.process.Child, io: Io, started: Io.Timestamp, deadline_ms: u64, cancelled: *bool, timed_out: *bool) !std.process.Child.Term {
+fn waitForExit(child: *std.process.Child, io: Io, started: Io.Timestamp, deadline_ms: u64, cancelled: *bool, timed_out: *bool, ignore_cancel: bool) !std.process.Child.Term {
const id = child.id;
const Event = union(enum) { term: std.process.Child.WaitError!std.process.Child.Term, tick: Io.Cancelable!void };
var buffer: [2]Event = undefined;
@@ -214,7 +217,7 @@ fn waitForExit(child: *std.process.Child, io: Io, started: Io.Timestamp, deadlin
},
.tick => |result| try result,
}
- cancelled.* = Agent.esc_cancel.load(.acquire);
+ cancelled.* = !ignore_cancel and Agent.esc_cancel.load(.acquire);
timed_out.* = deadline_ms > 0 and started.untilNow(io, .awake).toMilliseconds() >= deadline_ms;
if (cancelled.* or timed_out.*) {
while (select.cancel()) |event| switch (event) {
@@ -308,7 +311,7 @@ pub fn runCappedWithOptions(gpa: Allocator, io: Io, argv: []const []const u8, st
if (options.pulse) |pulse_fn| pulse_fn(options.stream_ctx, @intCast(@max(0, started.untilNow(io, .awake).toMilliseconds())));
}
if (eof) break :loop;
- if (Agent.esc_cancel.load(.acquire)) {
+ if (!options.ignore_cancel and Agent.esc_cancel.load(.acquire)) {
esc_killed = true;
terminateTree(&child, io, &windows_job, group_id, options.kill_process_tree);
cleanup_needed = false;
@@ -324,7 +327,7 @@ pub fn runCappedWithOptions(gpa: Allocator, io: Io, argv: []const []const u8, st
if (!esc_killed and !timed_out) try multi_reader.checkAnyError();
const term: std.process.Child.Term = if (esc_killed or timed_out) .{ .signal = .TERM } else blk: {
- const term = try waitForExit(&child, io, started, deadline_ms, &esc_killed, &timed_out);
+ const term = try waitForExit(&child, io, started, deadline_ms, &esc_killed, &timed_out, options.ignore_cancel);
if (esc_killed or timed_out) {
terminateTree(&child, io, &windows_job, group_id, options.kill_process_tree);
cleanup_needed = false;
diff --git a/src/process_runner_clipboard_tests.zig b/src/process_runner_clipboard_tests.zig
index ff77c792..eb621e9f 100644
--- a/src/process_runner_clipboard_tests.zig
+++ b/src/process_runner_clipboard_tests.zig
@@ -30,6 +30,18 @@ fn cancelSoon(io: std.Io) void {
@import("agent.zig").Agent.esc_cancel.store(true, .release);
}
+test "clipboard helper ignores Esc so screenshot paste can write /tmp" {
+ if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
+ defer @import("agent.zig").Agent.esc_cancel.store(false, .release);
+ @import("agent.zig").Agent.esc_cancel.store(true, .release);
+ const result = try runner.runCappedWithOptions(std.testing.allocator, std.testing.io, &.{ "/bin/sh", "-c", "printf ok" }, 8, 8, 2000, .{ .ignore_cancel = true });
+ defer std.testing.allocator.free(result.stdout);
+ defer std.testing.allocator.free(result.stderr);
+ try std.testing.expect(runner.ranOk(result));
+ try std.testing.expectEqualStrings("ok", result.stdout);
+ try std.testing.expect(!result.cancelled and !result.timed_out);
+}
+
test "clipboard runner Esc survives pipe EOF without deadline" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
const io = std.testing.io;