graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Preserve replaced clipboard exports during TUI cleanup
justrach848e4fa719c0
diff --git a/TUI/app.zig b/TUI/app.zigindex da7f9e97..c26c3658 100644--- a/TUI/app.zig+++ b/TUI/app.zig@@ -90,7 +90,7 @@ pub const Model = struct {draft_images: ?[]const []const u8 = null,steer_queue: std.array_list.Managed([]const u8),images: std.array_list.Managed([]const u8),- owned_images: std.array_list.Managed([]const u8),+ owned_images: std.array_list.Managed(@import("owned_images.zig").Owned),pending: ?*engine.Job = null,/// A background engine op (/compact, !cmd, @-file list) — same thread +/// done-flag contract as `pending`, so neither can freeze the loop (#533).@@ -222,7 +222,7 @@ pub const Model = struct {.prompt_hist_images = std.array_list.Managed([]const []const u8).init(alloc),.steer_queue = std.array_list.Managed([]const u8).init(alloc),.images = std.array_list.Managed([]const u8).init(alloc),- .owned_images = std.array_list.Managed([]const u8).init(alloc),+ .owned_images = std.array_list.Managed(@import("owned_images.zig").Owned).init(alloc),.chat = engine.g_turn_fn != null,};self.input.setPlaceholder("");diff --git a/TUI/issue_883_tests.zig b/TUI/issue_883_tests.zigindex 393c8c9d..9dfaad5f 100644--- a/TUI/issue_883_tests.zig+++ b/TUI/issue_883_tests.zig@@ -220,3 +220,42 @@ test "closing an unsent clipboard draft releases only its owned export" {}try std.testing.expectError(error.FileNotFound, tmp.dir.openFile(io, "paste.png", .{}));}++test "clipboard removal preserves a replacement file at the exported path" {+ try changedExport(false, false);+}++test "clipboard teardown preserves edited exports" {+ try changedExport(true, false);+}++test "clipboard removal preserves a symlink substituted for the export" {+ try changedExport(false, true);+}++fn changedExport(edited: bool, symlink: bool) !void {+ 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);+ if (!edited) try tmp.dir.rename("paste.png", tmp.dir, "old.png", io);+ if (symlink) {+ try tmp.dir.symLink(io, "old.png", "paste.png", .{});+ } else {+ try tmp.dir.writeFile(io, .{ .sub_path = "paste.png", .data = "user replacement pixels" });+ }+ if (!edited) _ = term.feed("\x7f");+ }+ const preserved = try tmp.dir.openFile(io, "paste.png", .{});+ defer preserved.close(io);+ var bytes: [64]u8 = undefined;+ const n = try preserved.readPositionalAll(io, &bytes, 0);+ try std.testing.expectEqualStrings(if (symlink) "pixels" else "user replacement pixels", bytes[0..n]);+}diff --git a/TUI/owned_images.zig b/TUI/owned_images.zigindex adc03885..d07a3904 100644--- a/TUI/owned_images.zig+++ b/TUI/owned_images.zig@@ -2,15 +2,23 @@const std = @import("std");const Model = @import("app.zig").Model;+pub const Owned = struct { path: []const u8, identity: std.Io.File.Stat };++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;+ return if (stat.kind == .file) stat else null;+}+pub fn attach(self: *Model, path: []const u8, owned: bool) void {if (!owned) return self.attachImage(path);+ const original = identity(path) orelse return self.attachImage(path);const copy = self.alloc.dupe(u8, path) catch {- remove(path);+ remove(.{ .path = path, .identity = original });return;};- self.owned_images.append(copy) catch {+ self.owned_images.append(.{ .path = copy, .identity = original }) catch {self.alloc.free(copy);- remove(path);+ remove(.{ .path = path, .identity = original });return;};const before = self.images.items.len;@@ -18,8 +26,14 @@ pub fn attach(self: *Model, path: []const u8, owned: bool) void {if (self.images.items.len == before) collect(self);}-fn remove(path: []const u8) void {- std.Io.Dir.cwd().deleteFile(std.Io.Threaded.global_single_threaded.io(), path) catch {};+fn remove(owned: Owned) void {+ const current = identity(owned.path) orelse return;+ const original = owned.identity;+ // A pathname is not deletion authority after another file replaces it.+ // Also preserve edits made in place; reading a preview only changes atime.+ if (current.inode != original.inode or current.size != original.size or+ !std.meta.eql(current.mtime, original.mtime) or !std.meta.eql(current.ctime, original.ctime)) return;+ std.Io.Dir.cwd().deleteFile(std.Io.Threaded.global_single_threaded.io(), owned.path) catch {};}fn contains(paths: []const []const u8, path: []const u8) bool {@@ -41,13 +55,14 @@ pub fn collect(self: *Model) void {if (self.pending != null or self.bg != null) return;var i: usize = 0;while (i < self.owned_images.items.len) {- const path = self.owned_images.items[i];+ const owned = self.owned_images.items[i];+ const path = owned.path;if (referenced(self, path)) {i += 1;continue;}- remove(path);- self.alloc.free(self.owned_images.orderedRemove(i));+ remove(owned);+ self.alloc.free(self.owned_images.orderedRemove(i).path);}}@@ -59,10 +74,11 @@ fn replayReferenced(self: *const Model, path: []const u8) bool {}pub fn deinit(self: *Model, settled: bool) void {- for (self.owned_images.items) |path| {+ for (self.owned_images.items) |owned| {+ 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(path);+ if (settled and !replayReferenced(self, path)) remove(owned);self.alloc.free(path);}self.owned_images.deinit();diff --git a/docs/adr/0117-clipboard-files-follow-consumer-ownership.md b/docs/adr/0117-clipboard-files-follow-consumer-ownership.mdindex 24b06dbd..fb3519a2 100644--- a/docs/adr/0117-clipboard-files-follow-consumer-ownership.md+++ b/docs/adr/0117-clipboard-files-follow-consumer-ownership.md@@ -12,7 +12,9 @@ An age-only attachment sweep can remove a live draft when another file uploads.## DecisionThe TUI transfers clipboard ownership explicitly across its callback boundary.-Only owned files without live consumers are released. Pending workers protect+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 protectstaging inputs, and sent path markers preserve files for saved replay. Previewconversion uses an exclusively claimed output and removes it on every outcome.