Skip to content

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

Python

fix(windows): do not panic on unimplemented path chmod (#606)

Rach Pradhanc9f66215adc0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dd9d90e1..8ca32762 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,10 @@ current is part of cutting a release.
## v0.0.269 (2026-08-20)
+- Windows: `edit_file`/`write_file` no longer panic the session on Zig 0.17's
+ unimplemented path chmod (`dirSetFilePermissions`). Mode restore uses handle
+ chmod; directory chmod is skipped on Windows the same way the rest of the
+ tree already did (#606).
- codedb pairing (ADR 0019): advertise five one-shots — `context <task>`,
`around <name>` (def+callers), `callpath A B`, `list_dir <path>`,
`status`. Hop verbs stay callable, not on the catalog menu.
diff --git a/src/credential_store.zig b/src/credential_store.zig
index 2a6b2c65..7a73e44b 100644
--- a/src/credential_store.zig
+++ b/src/credential_store.zig
@@ -89,7 +89,8 @@ pub fn writeOAuth(io: Io, arena: Allocator, home: []const u8, provider_dir: []co
// O_PATH on Linux, where fchmod panics EBADF instead of erroring.
const dir = Io.Dir.cwd().openDir(io, path, .{ .iterate = true }) catch continue;
defer dir.close(io);
- dir.setPermissions(io, private_dir) catch {};
+ // Dir.setPermissions panics on Windows in Zig 0.17 (`dirSetPermissionsWindows`).
+ if (builtin.os.tag != .windows) dir.setPermissions(io, private_dir) catch {};
}
var obj: std.json.ObjectMap = .empty;
try obj.put(arena, "access_token", .{ .string = access });
diff --git a/src/edit_verify.zig b/src/edit_verify.zig
index f7845844..5cd2e9f9 100644
--- a/src/edit_verify.zig
+++ b/src/edit_verify.zig
@@ -323,15 +323,16 @@ pub fn fsErrorText(gpa: Allocator, op: FileOp, path: []const u8, err: anyerror)
}) catch null;
}
-/// Re-apply a file's saved permission bits after a rewrite. edit_file's atomic
-/// zigpatch splice — and write_file overwriting an existing file — land the new
-/// content on a fresh inode with the default 0644, silently dropping a 0755
-/// executable bit; restore it (#179). `prev` is null for a brand-new file (keep
-/// the default) or when the pre-write stat failed. Best-effort: a chmod failure
-/// never fails the write itself.
+/// Re-apply a file's saved permission bits after a rewrite (#179). Atomic
+/// splice lands a fresh 0644 inode; restore the pre-edit mode. `prev` is null
+/// for a new file or a failed pre-write stat. Best-effort: chmod never fails
+/// the write. Path chmod panics on Windows in Zig 0.17 (#606); handle chmod
+/// (`File.setPermissions`) is implemented on every OS we ship.
pub fn preserveMode(io: Io, path: []const u8, prev: ?Io.Dir.Stat) void {
const st = prev orelse return;
- Io.Dir.cwd().setFilePermissions(io, path, st.permissions, .{}) catch {};
+ const file = Io.Dir.cwd().openFile(io, path, .{ .mode = .read_write }) catch return;
+ defer file.close(io);
+ file.setPermissions(io, st.permissions) catch {};
}
// --- tests ----------------------------------------------------------------
diff --git a/src/exec.zig b/src/exec.zig
index 37bb43d0..e4ba58ea 100644
--- a/src/exec.zig
+++ b/src/exec.zig
@@ -1,6 +1,7 @@
//! Tool dispatch: `execTool` + `execToolInner`. Split out of main.zig.
const std = @import("std");
+const builtin = @import("builtin");
const Io = std.Io;
const Value = std.json.Value;
const Allocator = std.mem.Allocator;
@@ -499,6 +500,27 @@ fn execToolInner(ctx: ToolCtx, call: ToolCall) !ToolOutput {
return .{ .text = try std.fmt.allocPrint(gpa, "unknown tool: {s}", .{call.name}), .is_error = true };
}
+test "preserveMode restores bits via handle chmod so Windows cannot panic (#179/#606)" {
+ const io = std.testing.io;
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ try tmp.dir.writeFile(io, .{ .sub_path = "x.sh", .data = "a" });
+ if (builtin.os.tag != .windows) {
+ try tmp.dir.setFilePermissions(io, "x.sh", Io.File.Permissions.fromMode(0o755), .{});
+ }
+ const st = try tmp.dir.statFile(io, "x.sh", .{});
+ try tmp.dir.writeFile(io, .{ .sub_path = "x.sh", .data = "b" });
+ var real_buf: [std.fs.max_path_bytes]u8 = undefined;
+ const n = try tmp.dir.realPath(io, &real_buf);
+ const path = try std.fmt.allocPrint(std.testing.allocator, "{s}/x.sh", .{real_buf[0..n]});
+ defer std.testing.allocator.free(path);
+ preserveMode(io, path, st);
+ if (builtin.os.tag != .windows) {
+ const after = try tmp.dir.statFile(io, "x.sh", .{});
+ try std.testing.expectEqual(@as(std.posix.mode_t, 0o755), after.permissions.toMode() & 0o777);
+ }
+}
+
test "internal learning respects the parent privacy ceiling" {
var argv: [10][]const u8 = undefined;
var len = learningArgv(&argv, "graff", false);
diff --git a/src/kimi_catalog.zig b/src/kimi_catalog.zig
index f2aba807..6c2b23e4 100644
--- a/src/kimi_catalog.zig
+++ b/src/kimi_catalog.zig
@@ -30,7 +30,8 @@ fn secureDir(io: Io, path: []const u8) void {
// panic, not a catchable error. A readable handle chmods fine everywhere.
const dir = Io.Dir.cwd().openDir(io, path, .{ .iterate = true }) catch return;
defer dir.close(io);
- dir.setPermissions(io, private_dir_permissions) catch {};
+ // Dir.setPermissions panics on Windows in Zig 0.17 (`dirSetPermissionsWindows`).
+ if (builtin.os.tag != .windows) dir.setPermissions(io, private_dir_permissions) catch {};
}
fn validDeviceId(value: []const u8) bool {