Skip to content

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

Python

Collect released clipboard drafts after worker completion

justrach3b8c70fea7e8
diff --git a/TUI/app.zig b/TUI/app.zig
index c26c3658..b9507883 100644
--- a/TUI/app.zig
+++ b/TUI/app.zig
@@ -292,6 +292,7 @@ pub const Model = struct {
const owned = try sanitize.sanitized(self.alloc, text);
errdefer self.alloc.free(owned);
try self.history.append(.{ .kind = kind, .text = owned, .folded = kind == .tool, .at_ms = self.now_ms });
+ if (kind == .user) @import("owned_images.zig").retain(self, owned);
if (kind == .user or kind == .assistant) {
self.screen = .agent;
self.follow = true;
diff --git a/TUI/bgop.zig b/TUI/bgop.zig
index 0dae2aa0..fb07d637 100644
--- a/TUI/bgop.zig
+++ b/TUI/bgop.zig
@@ -126,6 +126,7 @@ fn release(self: *Model, op: *Op) void {
if (op.text) |t| self.alloc.free(t);
self.alloc.destroy(op);
self.bg = null;
+ @import("owned_images.zig").collect(self);
}
fn applyCompact(self: *Model, op: *Op) void {
diff --git a/TUI/issue_883_tests.zig b/TUI/issue_883_tests.zig
index 9dfaad5f..84fb3c2b 100644
--- a/TUI/issue_883_tests.zig
+++ b/TUI/issue_883_tests.zig
@@ -259,3 +259,70 @@ fn changedExport(edited: bool, symlink: bool) !void {
const n = try preserved.readPositionalAll(io, &bytes, 0);
try std.testing.expectEqualStrings(if (symlink) "pixels" else "user replacement pixels", bytes[0..n]);
}
+
+test "sent clipboard exports survive clearing visible history and prompt recall" {
+ const io = std.Io.Threaded.global_single_threaded.io();
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(io, .{ .sub_path = "paste.png", .data = "pixels" });
+ const file_path = try tmp.dir.realPathFileAlloc(io, "paste.png", std.testing.allocator);
+ defer std.testing.allocator.free(file_path);
+ {
+ var term: Term = undefined;
+ term.init(std.testing.allocator, 100, 24);
+ defer term.deinit();
+ @import("owned_images.zig").attach(&term.model, file_path, true);
+ _ = term.typeText("describe this");
+ _ = term.enter();
+ term.model.clearHistory();
+ @import("prompt_history.zig").deinit(&term.model);
+ term.model.prompt_hist_images = std.array_list.Managed([]const []const u8).init(term.alloc);
+ @import("owned_images.zig").collect(&term.model);
+ const retained = try tmp.dir.openFile(io, "paste.png", .{});
+ retained.close(io);
+ }
+ const replay = try tmp.dir.openFile(io, "paste.png", .{});
+ replay.close(io);
+}
+
+test "completed model worker releases a removed unsent clipboard draft" {
+ try workerReleasesDraft(false);
+}
+
+test "completed background worker releases a removed unsent clipboard draft" {
+ try workerReleasesDraft(true);
+}
+
+fn workerReleasesDraft(background: bool) !void {
+ const io = std.Io.Threaded.global_single_threaded.io();
+ const gpa = std.testing.allocator;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(io, .{ .sub_path = "paste.png", .data = "pixels" });
+ const file_path = try tmp.dir.realPathFileAlloc(io, "paste.png", gpa);
+ defer gpa.free(file_path);
+ var term: Term = undefined;
+ term.init(gpa, 100, 24);
+ defer term.deinit();
+ @import("owned_images.zig").attach(&term.model, file_path, true);
+ if (background) {
+ const op = try gpa.create(engine.BgOp);
+ op.* = .{ .kind = .files, .gpa = gpa, .threaded = false };
+ term.model.bg = op;
+ } else {
+ const job = try gpa.create(engine.Job);
+ job.* = .{ .gpa = gpa, .threaded = false, .history = try gpa.alloc(engine.Turn, 0), .params = .{}, .stream = .{} };
+ term.model.pending = job;
+ }
+ @import("image.zig").clearAll(&term.model);
+ const pending = try tmp.dir.openFile(io, "paste.png", .{});
+ pending.close(io);
+ if (background) {
+ term.model.bg.?.done.store(true, .release);
+ @import("bgop.zig").finish(&term.model);
+ } else {
+ term.model.pending.?.done.store(true, .release);
+ @import("turn.zig").finishJob(&term.model);
+ }
+ try std.testing.expectError(error.FileNotFound, tmp.dir.openFile(io, "paste.png", .{}));
+}
diff --git a/TUI/owned_images.zig b/TUI/owned_images.zig
index d07a3904..15ff0ff6 100644
--- a/TUI/owned_images.zig
+++ b/TUI/owned_images.zig
@@ -2,7 +2,7 @@
const std = @import("std");
const Model = @import("app.zig").Model;
-pub const Owned = struct { path: []const u8, identity: std.Io.File.Stat };
+pub const Owned = struct { path: []const u8, identity: std.Io.File.Stat, retained: bool = false };
fn identity(path: []const u8) ?std.Io.File.Stat {
const stat = std.Io.Dir.cwd().statFile(std.Io.Threaded.global_single_threaded.io(), path, .{ .follow_symlinks = false }) catch return null;
@@ -50,6 +50,13 @@ fn referenced(self: *const Model, path: []const u8) bool {
return false;
}
+/// A submitted turn may be persisted independently of visible TUI history.
+pub fn retain(self: *Model, text: []const u8) void {
+ for (self.owned_images.items) |*owned| {
+ if (std.mem.indexOf(u8, text, owned.path) != null) owned.retained = true;
+ }
+}
+
pub fn collect(self: *Model) void {
// Pending consumers may already hold a snapshot outside the model arrays.
if (self.pending != null or self.bg != null) return;
@@ -57,7 +64,7 @@ pub fn collect(self: *Model) void {
while (i < self.owned_images.items.len) {
const owned = self.owned_images.items[i];
const path = owned.path;
- if (referenced(self, path)) {
+ if (owned.retained or referenced(self, path)) {
i += 1;
continue;
}
@@ -78,7 +85,7 @@ pub fn deinit(self: *Model, settled: bool) void {
const path = owned.path;
// Saved turns retain path markers. UI teardown is not proof that
// those replay consumers (or an abandoned worker) have released them.
- if (settled and !replayReferenced(self, path)) remove(owned);
+ if (settled and !owned.retained and !replayReferenced(self, path)) remove(owned);
self.alloc.free(path);
}
self.owned_images.deinit();
diff --git a/TUI/turn.zig b/TUI/turn.zig
index 05a6ed9b..9d6bab53 100644
--- a/TUI/turn.zig
+++ b/TUI/turn.zig
@@ -107,6 +107,7 @@ pub fn finishJob(self: *Model) void {
job.events.deinit();
self.alloc.destroy(job);
self.pending = null;
+ @import("owned_images.zig").collect(self);
self.cancel_requested = false;
self.scroll = 0;
self.follow = true;
diff --git a/docs/adr/0117-clipboard-files-follow-consumer-ownership.md b/docs/adr/0117-clipboard-files-follow-consumer-ownership.md
index fb3519a2..d9556f7b 100644
--- a/docs/adr/0117-clipboard-files-follow-consumer-ownership.md
+++ b/docs/adr/0117-clipboard-files-follow-consumer-ownership.md
@@ -15,7 +15,9 @@ The TUI transfers clipboard ownership explicitly across its callback boundary.
Only owned files without live consumers are released. The TUI captures file
identity without following symlinks and preserves replaced or edited exports
before attachment removal or teardown. Pending workers protect
-staging inputs, and sent path markers preserve files for saved replay. Preview
+staging inputs; worker completion retries deferred draft cleanup. Accepting a
+user history entry retains its owned image paths independently of later visible
+history or recall clearing, because saved replay can still need those files. Preview
conversion uses an exclusively claimed output and removes it on every outcome.
The native desktop records upload ownership and file identity. Discarding an