Skip to content

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

Python

Expire orphaned publication claims before they block writes

Cursor Agentfbfecdbb5082
diff --git a/src/artifact_claim.zig b/src/artifact_claim.zig
index b9f18a1e..b33fafc1 100644
--- a/src/artifact_claim.zig
+++ b/src/artifact_claim.zig
@@ -16,6 +16,7 @@ const proc_identity = @import("proc_identity.zig");
const claim_ledger = @import("artifact_claim_ledger.zig");
const claim_path = @import("artifact_claim_path.zig");
+const claim_stale = @import("artifact_claim_stale.zig");
pub const Kind = claim_ledger.Kind;
pub const Owner = claim_ledger.Owner;
pub const Claim = claim_ledger.Claim;
@@ -173,6 +174,21 @@ pub fn gateCommandIn(arena: Allocator, io: Io, cmd: []const u8, key: []const u8,
if (tx) |transaction| {
loadTransaction(io, transaction, ledger, cwd) catch return "artifact claim ledger unreadable: action NOT performed";
} else local = g_ledger;
+ const before_len = ledger.len;
+ var stale_i: usize = 0;
+ while (stale_i < ledger.len) {
+ if (ledger.items[stale_i].kind == .publication and !ownerLive(io, ledger.items[stale_i].owner)) {
+ ledger.items[stale_i] = ledger.items[ledger.len - 1];
+ ledger.len -= 1;
+ continue;
+ }
+ stale_i += 1;
+ }
+ if (ledger.len < before_len) {
+ if (tx) |transaction| {
+ if (persistJson(scratch.allocator(), ledger)) |json| transaction.write(json) catch {} else |_| {}
+ }
+ }
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;
for (ledger.slice()) |c| {
@@ -223,7 +239,8 @@ pub fn handleToolIn(arena: Allocator, io: Io, action: []const u8, kind_s: []cons
const me = selfOwner(io);
const now: i64 = 0;
if (claim_ledger.ambiguous(ledger.slice(), kind, key, repo)) return .{ .text = "claim repository is ambiguous; specify repo explicitly", .is_error = true };
- const existing = claim_ledger.findIn(ledger.slice(), kind, key, repo);
+ const existing = claim_ledger.findIn(ledger.slice(), kind, key, repo) orelse
+ if (kind == .publication) claim_stale.publicationRecord(ledger.slice(), key, repo) else null;
const live = if (existing) |c| ownerLive(io, c.owner) else false;
if (std.mem.eql(u8, action, "claim") or std.mem.eql(u8, action, "acquire")) {
const msg = claim_ledger.acquireIn(ledger, storage, kind, key, me, now, live, repo) catch |err| switch (err) {
@@ -237,7 +254,8 @@ pub fn handleToolIn(arena: Allocator, io: Io, action: []const u8, kind_s: []cons
if (std.mem.eql(u8, action, "release")) {
const existing_owner = if (existing) |c| c.owner else null;
const recoverable = if (existing_owner) |o| o.pid == 0 and (std.mem.eql(u8, o.session, me.session) or o.session.len == 0) else false;
- const msg = claim_ledger.releaseIn(ledger, kind, key, me, live and !recoverable, repo) catch return .{
+ const held = existing orelse return .{ .text = "no claim to release", .is_error = false };
+ const msg = claim_ledger.releaseIn(ledger, held.kind, held.key, me, live and !recoverable, held.repo orelse repo) catch return .{
.text = "cannot release a live foreign claim",
.is_error = true,
};
@@ -272,8 +290,8 @@ pub fn handleToolIn(arena: Allocator, io: Io, action: []const u8, kind_s: []cons
}
if (std.mem.eql(u8, action, "status")) {
if (existing) |c| {
- const v = claim_ledger.verdictIn(ledger.slice(), kind, key, me, live, repo);
- return .{ .text = try std.fmt.allocPrint(arena, "claim {s} {s}: {s} owner=\"{s}\"", .{ @tagName(kind), key, @tagName(v), c.owner.session }), .is_error = false };
+ const v = claim_ledger.verdictIn(ledger.slice(), c.kind, c.key, me, live, c.repo orelse repo);
+ return .{ .text = try std.fmt.allocPrint(arena, "claim {s} {s}: {s} owner=\"{s}\"", .{ @tagName(c.kind), c.key, @tagName(v), c.owner.session }), .is_error = false };
}
return .{ .text = "no claim", .is_error = false };
}
@@ -344,6 +362,7 @@ pub const loadJson = @import("artifact_claim_store.zig").loadJson;
test {
_ = @import("artifact_claim_path.zig");
+ _ = @import("artifact_claim_stale.zig");
}
test "acquire / handoff / release are atomic and session-scoped" {
diff --git a/src/artifact_claim_stale.zig b/src/artifact_claim_stale.zig
new file mode 100644
index 00000000..7aad72d6
--- /dev/null
+++ b/src/artifact_claim_stale.zig
@@ -0,0 +1,84 @@
+//! Publication claims share one repository-scoped record for status, release,
+//! and the write gate. An orphaned (dead-owner) publication must not block
+//! unrelated Git writes.
+
+const std = @import("std");
+const ledger = @import("artifact_claim_ledger.zig");
+const Claim = ledger.Claim;
+const Kind = ledger.Kind;
+const Owner = ledger.Owner;
+const Ledger = ledger.Ledger;
+
+pub fn publicationRecord(claims: []const Claim, key: []const u8, repo: ?[]const u8) ?Claim {
+ if (ledger.findIn(claims, .publication, key, repo)) |found| return found;
+ if (key.len != 0) return null;
+ var found: ?Claim = null;
+ for (claims) |c| {
+ if (c.kind != .publication) continue;
+ if (ledger.differentRepository(c.repo, repo)) continue;
+ if (found != null) return null;
+ found = c;
+ }
+ return found;
+}
+
+pub fn expireOrphans(store: *Ledger, live: bool) usize {
+ var removed: usize = 0;
+ var i: usize = 0;
+ while (i < store.len) {
+ if (store.items[i].kind == .publication and !live) {
+ store.items[i] = store.items[store.len - 1];
+ store.len -= 1;
+ removed += 1;
+ continue;
+ }
+ i += 1;
+ }
+ return removed;
+}
+
+pub fn expireDead(store: *Ledger, owner_live: *const fn (Owner) bool) usize {
+ var removed: usize = 0;
+ var i: usize = 0;
+ while (i < store.len) {
+ if (store.items[i].kind == .publication and !owner_live(store.items[i].owner)) {
+ store.items[i] = store.items[store.len - 1];
+ store.len -= 1;
+ removed += 1;
+ continue;
+ }
+ i += 1;
+ }
+ return removed;
+}
+
+test "status and release share the repo-scoped publication record" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ var store: Ledger = .{};
+ const owner = Owner{ .session = "s-gone", .pid = 9, .start_id = 1 };
+ _ = try ledger.acquireIn(&store, arena.allocator(), .publication, "feat/x", owner, 1, false, "github.com/org/repo");
+ try std.testing.expect(ledger.findIn(store.slice(), .publication, "", "github.com/org/repo") == null);
+ const found = publicationRecord(store.slice(), "", "github.com/org/repo").?;
+ try std.testing.expectEqualStrings("feat/x", found.key);
+ try std.testing.expect(publicationRecord(store.slice(), "feat/other", "github.com/org/repo") == null);
+}
+
+test "orphaned publication claims expire and do not block unrelated writes" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ var store: Ledger = .{};
+ const dead = Owner{ .session = "s-dead", .pid = 9, .start_id = 1 };
+ const live = Owner{ .session = "s-live", .pid = 8, .start_id = 2 };
+ _ = try ledger.acquire(&store, arena.allocator(), .publication, "feat/x", dead, 1, false);
+ _ = try ledger.acquire(&store, arena.allocator(), .issue, "12", live, 1, false);
+ const none = struct {
+ fn gone(_: Owner) bool {
+ return false;
+ }
+ };
+ try std.testing.expectEqual(@as(usize, 1), expireDead(&store, none.gone));
+ try std.testing.expectEqual(@as(usize, 1), store.len);
+ try std.testing.expectEqual(Kind.issue, store.items[0].kind);
+ try std.testing.expect(publicationRecord(store.slice(), "", null) == null);
+}
diff --git a/src/main.zig b/src/main.zig
index d0995be5..f980a290 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -52,7 +52,7 @@ test {
_ = .{ @import("clipboard_native.zig"), @import("clipboard_edge_tests.zig"), @import("clipboard_failure_tests.zig"), @import("process_runner_clipboard_tests.zig") };
_ = @import("server_orphan.zig");
_ = @import("repo_transaction.zig");
- _ = .{ @import("pr_command.zig"), @import("pr_evidence.zig"), @import("pr_verify.zig"), @import("pr_acceptance.zig"), @import("artifact_repository.zig"), @import("artifact_claim_ledger.zig"), @import("artifact_claim_store.zig") };
+ _ = .{ @import("pr_command.zig"), @import("pr_evidence.zig"), @import("pr_verify.zig"), @import("pr_acceptance.zig"), @import("artifact_repository.zig"), @import("artifact_claim_ledger.zig"), @import("artifact_claim_store.zig"), @import("artifact_claim_stale.zig") };
_ = @import("argstream_citation_tests.zig"); // unit_tests' root is main.zig only, so reference every split-out module or its tests silently never run
_ = @import("codex_node_repl.zig");
_ = pricing;