graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Hotfix: one claim ledger per Git common dir
justrach958ea9616bef
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ac1db4f6..e08b2e7b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,6 +10,10 @@ The release workflow uses a tag's section here as its release notes (ahand-written `docs/releases/<tag>.md` wins if present), so keeping this filecurrent is part of cutting a release.+## v0.0.302.1++- `#1092`: claim ledger lives under the Git common directory, so a worktree switch still sees and can release the same claim. Per-worktree `.graff` files are imported, not bypassed. CLI-only hotfix.+## v0.0.302- Line REPL boxes GFM pipe tables (including `|---|` separators and pipe-less `a | b` rows) when the stream ends.diff --git a/src/artifact_claim.zig b/src/artifact_claim.zigindex 38010fb5..b9f18a1e 100644--- a/src/artifact_claim.zig+++ b/src/artifact_claim.zig@@ -15,6 +15,7 @@ const presence = @import("presence.zig");const proc_identity = @import("proc_identity.zig");const claim_ledger = @import("artifact_claim_ledger.zig");+const claim_path = @import("artifact_claim_path.zig");pub const Kind = claim_ledger.Kind;pub const Owner = claim_ledger.Owner;pub const Claim = claim_ledger.Claim;@@ -67,10 +68,11 @@ var g_ledger: Ledger = .{};var g_test_owner: Owner = .{};var g_test_live: bool = true;var g_persist_path: ?[]const u8 = null;+var g_test_resolve_cwd: bool = false;var g_test_peers: ?[]const @import("worktree_lease.zig").Owner = null;var g_store: ?std.heap.ArenaAllocator = null;-pub const persist_rel = ".graff/artifact-claims.json";+pub const persist_rel = claim_path.persist_rel;fn storeAlloc() Allocator {if (g_store == null) g_store = std.heap.ArenaAllocator.init(std.heap.page_allocator);@@ -82,6 +84,7 @@ pub fn resetForTest() void {g_test_owner = .{};g_test_live = true;g_persist_path = null;+ g_test_resolve_cwd = false;g_test_peers = null;if (g_store) |*st| {st.deinit();@@ -93,6 +96,10 @@ pub fn setPersistPath(path: []const u8) void {g_persist_path = path;}+pub fn setTestResolveCwd(on: bool) void {+ g_test_resolve_cwd = on;+}+pub fn setTestOwner(owner: Owner) void {g_test_owner = owner;}@@ -143,7 +150,7 @@ pub fn gateCommandIn(arena: Allocator, io: Io, cmd: []const u8, key: []const u8,if (!isClaimedMutation(cmd)) return null;var scratch = std.heap.ArenaAllocator.init(std.heap.page_allocator);defer scratch.deinit();- var local = readSnapshot(io, scratch.allocator()) catch return "artifact claim ledger unreadable or busy: action NOT performed";+ var local = readSnapshot(io, scratch.allocator(), cwd) catch return "artifact claim ledger unreadable or busy: action NOT performed";const ledger = &local;if (ledger.len == 0) return null;const kind = mutationKind(cmd);@@ -160,11 +167,11 @@ pub fn gateCommandIn(arena: Allocator, io: Io, cmd: []const u8, key: []const u8,const resolved = if (builtin.is_test or kind != .pull_request) null else @import("artifact_claim_target.zig").resolve(arena, io, cmd, kind, cwd);// The remote read does not hold the ledger lock. A handoff/release during// that read must be observed before deciding whether this action may run.- const tx = begin(io, scratch.allocator()) catch return "artifact claim ledger unavailable or busy: action NOT performed";+ const tx = begin(io, scratch.allocator(), cwd) catch return "artifact claim ledger unavailable or busy: action NOT performed";defer if (tx) |transaction| transaction.end();local = .{};if (tx) |transaction| {- loadTransaction(transaction, ledger) catch return "artifact claim ledger unreadable: action NOT performed";+ loadTransaction(io, transaction, ledger, cwd) catch return "artifact claim ledger unreadable: action NOT performed";} else local = g_ledger;var target: @import("artifact_claim_target.zig").Target = resolved orelse @import("artifact_claim_target.zig").explicit(cmd, kind) orelse .{ .kind = if (kind == .issue) Kind.issue else Kind.branch, .key = key };if (target.kind == .branch and target.key.len == 0) target.key = key;@@ -203,12 +210,12 @@ pub fn handleToolIn(arena: Allocator, io: Io, action: []const u8, kind_s: []consdefer scratch.deinit();const repo = if (builtin.is_test and requested_repo == null) null else @import("artifact_repository.zig").resolve(arena, io, cwd, requested_repo);if (requested_repo != null and repo == null) return .{ .text = "claim repository could not be resolved; no claim changed", .is_error = true };- const tx = begin(io, scratch.allocator()) catch return .{ .text = "claim ledger unavailable or busy", .is_error = true };+ const tx = begin(io, scratch.allocator(), cwd) catch return .{ .text = "claim ledger unavailable or busy", .is_error = true };defer if (tx) |transaction| transaction.end();var local: Ledger = .{};const ledger = if (tx != null) &local else &g_ledger;const storage = if (tx != null) scratch.allocator() else storeAlloc();- if (tx) |transaction| loadTransaction(transaction, ledger) catch return .{ .text = "claim ledger unreadable; no claim changed", .is_error = true };+ if (tx) |transaction| loadTransaction(io, transaction, ledger, cwd) catch return .{ .text = "claim ledger unreadable; no claim changed", .is_error = true };const kind = kindFrom(kind_s) orelse return .{.text = "kind must be branch, issue, commit, pull_request, or publication",.is_error = true,@@ -273,32 +280,53 @@ pub fn handleToolIn(arena: Allocator, io: Io, action: []const u8, kind_s: []consreturn .{ .text = "action must be claim, release, handoff, or status", .is_error = true };}-fn persistPath() ?[]const u8 {+fn persistPath(io: Io, arena: Allocator, cwd: []const u8) ?[]const u8 {if (g_persist_path) |p| return p;+ if (builtin.is_test and !g_test_resolve_cwd) return null;+ if (claim_path.canonicalFile(arena, io, cwd)) |p| return p;if (builtin.is_test) return null;return persist_rel;}-fn begin(io: Io, arena: Allocator) !?@import("repo_transaction.zig").Transaction {- const path = persistPath() orelse return null;+fn begin(io: Io, arena: Allocator, cwd: []const u8) !?@import("repo_transaction.zig").Transaction {+ const path = persistPath(io, arena, cwd) orelse return null;return try @import("repo_transaction.zig").Transaction.begin(io, arena, path);}-fn readSnapshot(io: Io, arena: Allocator) !Ledger {- const tx = try begin(io, arena);+fn readSnapshot(io: Io, arena: Allocator, cwd: []const u8) !Ledger {+ const tx = try begin(io, arena, cwd);defer if (tx) |transaction| transaction.end();var ledger: Ledger = .{};- if (tx) |transaction| try loadTransaction(transaction, &ledger) else return g_ledger;+ if (tx) |transaction| try loadTransaction(io, transaction, &ledger, cwd) else return g_ledger;return ledger;}-fn loadTransaction(tx: @import("repo_transaction.zig").Transaction, ledger: *Ledger) !void {- const text = try tx.read() orelse return;- try loadJson(tx.arena, ledger, text);+fn mergeLegacyFile(io: Io, arena: Allocator, ledger: *Ledger, path: []const u8) void {+ const text = Io.Dir.cwd().readFileAlloc(io, path, arena, .limited(64 * 1024)) catch return;+ var extra: Ledger = .{};+ loadJson(arena, &extra, text) catch return;+ for (extra.slice()) |c| {+ if (claim_ledger.findIn(ledger.slice(), c.kind, c.key, c.repo) != null) continue;+ if (ledger.len >= max_claims) return;+ ledger.items[ledger.len] = c;+ ledger.len += 1;+ }+}++fn loadTransaction(io: Io, tx: @import("repo_transaction.zig").Transaction, ledger: *Ledger, cwd: []const u8) !void {+ if (try tx.read()) |text| try loadJson(tx.arena, ledger, text);+ if (builtin.is_test and !g_test_resolve_cwd) return;+ if (cwd.len == 0 or std.mem.eql(u8, cwd, ".")) return;+ const legacy = claim_path.legacyFile(tx.arena, cwd) orelse return;+ if (!claim_path.samePath(legacy, tx.path)) mergeLegacyFile(io, tx.arena, ledger, legacy);+ for (claim_path.siblingLegacyFiles(tx.arena, io, cwd, tx.path)) |other| {+ if (claim_path.samePath(other, legacy)) continue;+ mergeLegacyFile(io, tx.arena, ledger, other);+ }}fn reload(io: Io) !void {- const path = persistPath() orelse return;+ const path = persistPath(io, storeAlloc(), ".") orelse return;// Every transaction reads again under the lock. A previously cached owner// must never survive a peer's acknowledged handoff.g_ledger.len = 0;@@ -314,6 +342,10 @@ fn reload(io: Io) !void {pub const persistJson = @import("artifact_claim_store.zig").persistJson;pub const loadJson = @import("artifact_claim_store.zig").loadJson;+test {+ _ = @import("artifact_claim_path.zig");+}+test "acquire / handoff / release are atomic and session-scoped" {var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);defer arena_state.deinit();diff --git a/src/artifact_claim_path.zig b/src/artifact_claim_path.zignew file mode 100644index 00000000..3413e980--- /dev/null+++ b/src/artifact_claim_path.zig@@ -0,0 +1,180 @@+//! One claim ledger per Git repository, not per worktree cwd (#1092).+const std = @import("std");+const Io = std.Io;+const Allocator = std.mem.Allocator;+const lease = @import("worktree_lease.zig");+const process_runner = @import("process_runner.zig");+const runCapped = process_runner.runCapped;+const ranOk = process_runner.ranOk;++pub const persist_rel = ".graff/artifact-claims.json";++/// Shared file under the Git common dir. Empty when `cwd` is not a repo.+pub fn canonicalFile(arena: Allocator, io: Io, cwd: []const u8) ?[]const u8 {+ if (cwd.len == 0) return null;+ var common = lease.gitCommonDirAt(std.heap.page_allocator, io, arena, cwd);+ if (common.len == 0) {+ const r = runCapped(std.heap.page_allocator, io, &.{ "git", "-C", cwd, "rev-parse", "--git-common-dir" }, 8192, 8192, 15_000) catch return null;+ defer {+ std.heap.page_allocator.free(r.stdout);+ std.heap.page_allocator.free(r.stderr);+ }+ if (!ranOk(r)) return null;+ const rel = std.mem.trim(u8, r.stdout, " \t\r\n");+ if (rel.len == 0) return null;+ common = if (std.fs.path.isAbsolute(rel)) arena.dupe(u8, rel) catch return null else std.fs.path.resolve(arena, &.{ cwd, rel }) catch return null;+ }+ return std.fs.path.join(arena, &.{ common, "artifact-claims.json" }) catch null;+}++pub fn legacyFile(arena: Allocator, cwd: []const u8) ?[]const u8 {+ if (cwd.len == 0) return null;+ return std.fs.path.join(arena, &.{ cwd, persist_rel }) catch null;+}++pub fn samePath(a: []const u8, b: []const u8) bool {+ return std.mem.eql(u8, a, b);+}++/// Other worktree `.graff` ledgers of this repo (porcelain `worktree` lines).+pub fn siblingLegacyFiles(arena: Allocator, io: Io, cwd: []const u8, skip: []const u8) []const []const u8 {+ const r = runCapped(std.heap.page_allocator, io, &.{ "git", "-C", cwd, "worktree", "list", "--porcelain" }, 64 * 1024, 4096, 15_000) catch return &.{};+ defer {+ std.heap.page_allocator.free(r.stdout);+ std.heap.page_allocator.free(r.stderr);+ }+ if (!ranOk(r)) return &.{};+ var list: std.ArrayList([]const u8) = .empty;+ var it = std.mem.splitScalar(u8, r.stdout, '\n');+ while (it.next()) |line| {+ const prefix = "worktree ";+ if (!std.mem.startsWith(u8, line, prefix)) continue;+ const dir = std.mem.trim(u8, line[prefix.len..], " \t\r");+ if (dir.len == 0) continue;+ const file = legacyFile(arena, dir) orelse continue;+ if (samePath(file, skip)) continue;+ list.append(arena, file) catch continue;+ if (list.items.len >= 32) break;+ }+ return list.items;+}++test "canonicalFile is identical across linked worktrees of one repo" {+ const a = std.testing.allocator;+ const io = std.testing.io;+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const tmp_root = try tmp.dir.realPathFileAlloc(io, ".", a);+ defer a.free(tmp_root);+ const root = try std.fmt.allocPrint(a, "{s}/repo-a", .{tmp_root});+ defer a.free(root);+ try Io.Dir.cwd().createDirPath(io, root);+ try git(io, a, &.{ "init", "-q", "-b", "main", root });+ try git(io, a, &.{ "-C", root, "config", "user.email", "t@t" });+ try git(io, a, &.{ "-C", root, "config", "user.name", "t" });+ const tracked = try std.fs.path.join(a, &.{ root, "f" });+ defer a.free(tracked);+ {+ const f = try Io.Dir.cwd().createFile(io, tracked, .{});+ defer f.close(io);+ try f.writePositionalAll(io, "x\n", 0);+ }+ try git(io, a, &.{ "-C", root, "add", "f" });+ try git(io, a, &.{ "-C", root, "commit", "-m", "i" });+ const other = try std.fmt.allocPrint(a, "{s}/repo-b", .{tmp_root});+ defer a.free(other);+ try git(io, a, &.{ "-C", root, "worktree", "add", "--detach", other });+ var arena = std.heap.ArenaAllocator.init(a);+ defer arena.deinit();+ const ar = arena.allocator();+ const pa = canonicalFile(ar, io, root) orelse return error.TestUnexpectedResult;+ const pb = canonicalFile(ar, io, other) orelse return error.TestUnexpectedResult;+ try std.testing.expectEqualStrings(pa, pb);+ try std.testing.expect(std.mem.endsWith(u8, pa, "artifact-claims.json"));+ const other_repo = try std.fmt.allocPrint(a, "{s}/other", .{tmp_root});+ defer a.free(other_repo);+ try Io.Dir.cwd().createDirPath(io, other_repo);+ try git(io, a, &.{ "init", "-q", "-b", "main", other_repo });+ const pc = canonicalFile(ar, io, other_repo) orelse return error.TestUnexpectedResult;+ try std.testing.expect(!std.mem.eql(u8, pa, pc));+}++test "claim follows a worktree switch and does not shadow another repo" {+ const claims = @import("artifact_claim.zig");+ const a = std.testing.allocator;+ const io = std.testing.io;+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const tmp_root = try tmp.dir.realPathFileAlloc(io, ".", a);+ defer a.free(tmp_root);+ const root = try std.fmt.allocPrint(a, "{s}/wt-main", .{tmp_root});+ defer a.free(root);+ try Io.Dir.cwd().createDirPath(io, root);+ try git(io, a, &.{ "init", "-q", "-b", "main", root });+ try git(io, a, &.{ "-C", root, "config", "user.email", "t@t" });+ try git(io, a, &.{ "-C", root, "config", "user.name", "t" });+ const tracked = try std.fs.path.join(a, &.{ root, "f" });+ defer a.free(tracked);+ {+ const f = try Io.Dir.cwd().createFile(io, tracked, .{});+ defer f.close(io);+ try f.writePositionalAll(io, "x\n", 0);+ }+ try git(io, a, &.{ "-C", root, "add", "f" });+ try git(io, a, &.{ "-C", root, "commit", "-m", "i" });+ const linked = try std.fmt.allocPrint(a, "{s}/wt-link", .{tmp_root});+ defer a.free(linked);+ try git(io, a, &.{ "-C", root, "worktree", "add", "--detach", linked });+ const other = try std.fmt.allocPrint(a, "{s}/wt-other", .{tmp_root});+ defer a.free(other);+ try Io.Dir.cwd().createDirPath(io, other);+ try git(io, a, &.{ "init", "-q", "-b", "main", other });+ try git(io, a, &.{ "-C", other, "config", "user.email", "t@t" });+ try git(io, a, &.{ "-C", other, "config", "user.name", "t" });++ var arena = std.heap.ArenaAllocator.init(a);+ defer arena.deinit();+ const ar = arena.allocator();+ claims.resetForTest();+ defer claims.resetForTest();+ claims.setTestResolveCwd(true);+ claims.setTestOwner(.{ .session = "s-a", .pid = 11, .start_id = 1 });+ claims.setTestOwnerLive(true);+ const claimed = try claims.handleToolIn(ar, io, "claim", "issue", "1092", "", root, null);+ try std.testing.expect(!claimed.is_error);+ const mine = try claims.handleToolIn(ar, io, "status", "issue", "1092", "", linked, null);+ try std.testing.expect(std.mem.indexOf(u8, mine.text, "mine") != null);+ claims.setTestOwner(.{ .session = "s-b", .pid = 12, .start_id = 2 });+ const foreign = try claims.handleToolIn(ar, io, "status", "issue", "1092", "", linked, null);+ try std.testing.expect(std.mem.indexOf(u8, foreign.text, "foreign") != null);+ const blocked = claims.gateCommandIn(ar, io, "gh issue comment 1092 --body x", "1092", linked);+ try std.testing.expect(blocked != null);+ claims.setTestOwner(.{ .session = "s-a", .pid = 11, .start_id = 1 });+ const released = try claims.handleToolIn(ar, io, "release", "issue", "1092", "", linked, null);+ try std.testing.expect(!released.is_error);+ const free_a = try claims.handleToolIn(ar, io, "status", "issue", "1092", "", root, null);+ try std.testing.expectEqualStrings("no claim", free_a.text);+ const free_b = try claims.handleToolIn(ar, io, "status", "issue", "1092", "", linked, null);+ try std.testing.expectEqualStrings("no claim", free_b.text);+ try std.testing.expect(claims.gateCommandIn(ar, io, "gh issue comment 1092 --body x", "1092", root) == null);+ const other_claim = try claims.handleToolIn(ar, io, "claim", "issue", "1092", "", other, null);+ try std.testing.expect(!other_claim.is_error);+ const still_free = try claims.handleToolIn(ar, io, "status", "issue", "1092", "", root, null);+ try std.testing.expectEqualStrings("no claim", still_free.text);+}++fn git(io: Io, a: Allocator, argv: []const []const u8) !void {+ var args: std.ArrayList([]const u8) = .empty;+ defer args.deinit(a);+ try args.append(a, "git");+ try args.appendSlice(a, argv);+ const r = try runCapped(a, io, args.items, 4096, 4096, 15_000);+ defer {+ a.free(r.stdout);+ a.free(r.stderr);+ }+ if (!ranOk(r)) {+ std.debug.print("git {s} failed: {s}\n", .{ argv[0], r.stderr });+ return error.TestUnexpectedResult;+ }+}diff --git a/src/worktree_lease.zig b/src/worktree_lease.zigindex 1fa5c9a7..6e6e6c9f 100644--- a/src/worktree_lease.zig+++ b/src/worktree_lease.zig@@ -179,6 +179,13 @@ pub fn gitCommonDir(gpa: Allocator, io: Io, arena: Allocator) []const u8 {return revParse(gpa, io, arena, "--git-common-dir");}+/// Same as `gitCommonDir`, but `git -C dir` so a worktree switch still hits+/// the shared `.git` of the repository.+pub fn gitCommonDirAt(gpa: Allocator, io: Io, arena: Allocator, dir: []const u8) []const u8 {+ if (dir.len == 0) return gitCommonDir(gpa, io, arena);+ return revParseAt(gpa, io, arena, dir, "--git-common-dir");+}+fn revParseAt(gpa: Allocator, io: Io, arena: Allocator, dir: []const u8, flag: []const u8) []const u8 {const r = runCapped(gpa, io, &.{ "git", "-C", dir, "rev-parse", "--path-format=absolute", flag }, 8192, 8192, 15_000) catch return "";defer {