Skip to content

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

Python

fix: address review findings — truthful restore counter, single Rewound route, honest /rewind message

Rach Pradhand9c8df321207
diff --git a/src/commands_model.zig b/src/commands_model.zig
index 5fc21c74..8b3f3f66 100644
--- a/src/commands_model.zig
+++ b/src/commands_model.zig
@@ -24,7 +24,7 @@ const isLocalUrl = keys_cli.isLocalUrl;
const openAiModelsUrl = keys_cli.openAiModelsUrl;
const fetchOpenAIModels = keys_cli.fetchOpenAIModels;
const harness_version = main_mod.harness_version;
-const snapshots_mod = @import("snapshots.zig"); // /rewind's file history (Rewound)
+const Rewound = @import("tools.zig").Rewound; // what one /rewind restored/skipped
const pricing = @import("pricing.zig");
const kimi_catalog = @import("kimi_catalog.zig");
@@ -286,15 +286,15 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,
root.goal_note_fp = 0; // the goal note may have been in the dropped turns (#318)
// Restore files written/edited during the rewound turns, and re-point the
// turn counter so the next prompt re-takes turn n.
- var rw: snapshots_mod.Rewound = .{};
+ var rw: Rewound = .{};
if (root.snapshots) |snaps| {
rw = snaps.restore(@intCast(n));
snaps.turn = @intCast(n - 1);
}
try out.print("⏪ rewound to before prompt {d} — dropped {d} message(s)", .{ n, dropped });
if (rw.restored > 0) try out.print(", restored {d} file(s)", .{rw.restored});
- // Left alone rather than deleted: no snapshot was ever captured for these.
- if (rw.skipped > 0) try out.print(", left {d} file(s) as-is (unreadable when written, nothing to restore)", .{rw.skipped});
+ // Left alone rather than deleted. State the fact, never a cause: `.unreadable` is every non-FileNotFound read failure, so the reason is not knowable here.
+ if (rw.skipped > 0) try out.print(", left {d} file(s) as-is (no snapshot was captured)", .{rw.skipped});
if (rw.restored == 0 and rw.skipped == 0) try out.print("{s} (no tracked file changes){s}", .{ style.dim, style.reset });
try out.writeAll("\n");
try out.flush();
diff --git a/src/snapshots.zig b/src/snapshots.zig
index 5f69ff01..1b5071a1 100644
--- a/src/snapshots.zig
+++ b/src/snapshots.zig
@@ -85,7 +85,13 @@ pub const Snapshots = struct {
done.append(self.gpa, snap.path) catch {};
switch (snap.before) {
.content => |b| Io.Dir.cwd().writeFile(self.io, .{ .sub_path = snap.path, .data = b }) catch continue,
- .absent => Io.Dir.cwd().deleteFile(self.io, snap.path) catch {},
+ // A delete that FAILED left the file exactly as the rewound
+ // turns wrote it, so it must not be counted as restored.
+ // FileNotFound is the one exception: something else already
+ // removed it, so the tree is in the state the rewind wanted.
+ .absent => Io.Dir.cwd().deleteFile(self.io, snap.path) catch |err| {
+ if (err != error.FileNotFound) continue;
+ },
// Nothing was captured: there is nothing to put back, and
// deleting would destroy content /rewind never owned.
.unreadable => {
diff --git a/src/snapshots_tests.zig b/src/snapshots_tests.zig
index 28955448..810434b3 100644
--- a/src/snapshots_tests.zig
+++ b/src/snapshots_tests.zig
@@ -103,3 +103,62 @@ test "/rewind: a file too big to snapshot survives — it is never mistaken for
defer gpa.free(after);
try std.testing.expectEqualStrings("clobbered\n", after);
}
+
+test "/rewind: a delete that FAILS is not counted as a file restored" {
+ if (builtin.os.tag == .windows) return error.SkipZigTest;
+ const gpa = std.testing.allocator;
+ const io = std.testing.io;
+ var arena_state = std.heap.ArenaAllocator.init(gpa);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.createDir(io, "sub", .default_dir);
+ const dir = try std.fmt.allocPrint(a, ".zig-cache/tmp/{s}", .{&tmp.sub_path});
+
+ var snaps: snapshots.Snapshots = .{ .gpa = gpa, .io = io };
+ defer snaps.deinit();
+ snaps.turn = 1;
+ // Brand new file → snapshot `.absent` → the rewind's job is to delete it.
+ try writeFile(&snaps, a, try std.fmt.allocPrint(a, "{s}/sub/new.txt", .{dir}), "created this turn\n");
+
+ // The tree moves under us between the write and the rewind: `sub` is a
+ // regular FILE now, so deleting `sub/new.txt` fails with NotDir.
+ try tmp.dir.deleteTree(io, "sub");
+ try tmp.dir.writeFile(io, .{ .sub_path = "sub", .data = "not a directory\n" });
+
+ const rw = snaps.restore(1);
+ // Nothing was put back and nothing was deleted, so the count must be 0:
+ // reporting "restored 1 file(s)" here tells the user a lie they would act on.
+ try std.testing.expectEqual(@as(usize, 0), rw.restored);
+ // `skipped` is only for snapshots we never captured — this one we had.
+ try std.testing.expectEqual(@as(usize, 0), rw.skipped);
+ const kept = try tmp.dir.readFileAlloc(io, "sub", gpa, .limited(4096));
+ defer gpa.free(kept);
+ try std.testing.expectEqualStrings("not a directory\n", kept);
+}
+
+test "/rewind: a file already gone when the rewind runs still counts as restored" {
+ if (builtin.os.tag == .windows) return error.SkipZigTest;
+ const gpa = std.testing.allocator;
+ const io = std.testing.io;
+ var arena_state = std.heap.ArenaAllocator.init(gpa);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var snaps: snapshots.Snapshots = .{ .gpa = gpa, .io = io };
+ defer snaps.deinit();
+ snaps.turn = 1;
+ const path = try std.fmt.allocPrint(a, ".zig-cache/tmp/{s}/gone.txt", .{&tmp.sub_path});
+ try writeFile(&snaps, a, path, "created this turn\n");
+
+ // A bash `rm` (untracked by /rewind) got there first. The delete fails with
+ // FileNotFound, but the tree IS in the state the rewind wanted.
+ try tmp.dir.deleteFile(io, "gone.txt");
+ const rw = snaps.restore(1);
+ try std.testing.expectEqual(@as(usize, 1), rw.restored);
+ try std.testing.expectEqual(@as(usize, 0), rw.skipped);
+}
diff --git a/src/tools.zig b/src/tools.zig
index 10d519b6..26c80308 100644
--- a/src/tools.zig
+++ b/src/tools.zig
@@ -83,11 +83,12 @@ pub const ToolOutput = struct {
// `/rewind`'s pre-write file history lives in snapshots.zig (this file is at
// the 600-line cap); re-exported so `tools.Snapshots` keeps resolving for
-// main.zig/agent.zig/exec.zig.
+// main.zig/agent.zig/exec.zig — and so `Rewound`, which `restore()` now
+// returns, reaches its callers by that one route rather than a second import.
const snapshots_mod = @import("snapshots.zig");
-pub const Before = snapshots_mod.Before;
pub const beforeFromRead = snapshots_mod.beforeFromRead;
pub const Snapshots = snapshots_mod.Snapshots;
+pub const Rewound = snapshots_mod.Rewound;
/// Everything a tool executor may touch from a pool thread. All fields are
/// thread-safe (gpa, io, shared http client, mutex-guarded registry and