graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Claims: show what a full ledger holds and what can be released
justrach4097be4e24ee
diff --git a/src/artifact_claim.zig b/src/artifact_claim.zigindex d8e41257..cc02a348 100644--- a/src/artifact_claim.zig+++ b/src/artifact_claim.zig@@ -122,7 +122,7 @@ pub fn selfOwner(io: Io) Owner {};}-fn ownerLive(io: Io, owner: Owner) bool {+pub fn ownerLive(io: Io, owner: Owner) bool {if (g_test_owner.session.len > 0 or g_test_owner.pid != 0) {if (sameOwner(owner, g_test_owner)) return true;return g_test_live;@@ -254,7 +254,7 @@ pub fn handleToolIn(arena: Allocator, io: Io, action: []const u8, kind_s: []consif (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) {error.ClaimHeld => return .{ .text = refuseText(arena, kind, key, existing.?.owner), .is_error = true },- else => |e| return .{ .text = claim_ledger.acquireFailText(arena, e), .is_error = true },+ else => |e| return .{ .text = try @import("artifact_claim_inventory.zig").acquireFail(arena, io, e, ledger.slice(), me), .is_error = true }, // #1614: at capacity, list what can be released};if (tx) |transaction| transaction.write(try persistJson(scratch.allocator(), ledger)) catch return .{ .text = "claim was NOT persisted; retry before publishing", .is_error = true };announceLive(io, scratch.allocator(), ledger);@@ -302,7 +302,7 @@ pub fn handleToolIn(arena: Allocator, io: Io, action: []const u8, kind_s: []consconst 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 };+ return .{ .text = if (key.len == 0) try @import("artifact_claim_inventory.zig").status(arena, io, ledger.slice(), me) else "no claim", .is_error = false }; // #1614: keyless status lists the ledger}return .{ .text = "action must be claim, release, handoff, or status", .is_error = true };}diff --git a/src/artifact_claim_inventory.zig b/src/artifact_claim_inventory.zignew file mode 100644index 00000000..88e9e585--- /dev/null+++ b/src/artifact_claim_inventory.zig@@ -0,0 +1,105 @@+//! #1614: what the claim ledger holds. A full ledger refuses new claims and+//! tells the caller to release a finished one, and `status` without a key+//! asks about the whole ledger, so both list every claim with whether the+//! caller may release it: its own claims, and claims whose owner has exited.+//! A claim a live session holds is listed but never evicted.++const std = @import("std");+const Allocator = std.mem.Allocator;+const Io = std.Io;+const claim_ledger = @import("artifact_claim_ledger.zig");+const Claim = claim_ledger.Claim;+const Owner = claim_ledger.Owner;++/// `status` without a key: every claim, or "no claim" for an empty ledger.+pub fn status(arena: Allocator, io: Io, claims: []const Claim, me: Owner) ![]const u8 {+ if (claims.len == 0) return "no claim";+ const header = try std.fmt.allocPrint(arena, "{d}/{d} claims held:", .{ claims.len, claim_ledger.max_claims });+ return list(arena, header, claims, me, try liveness(arena, io, claims));+}++/// A refused acquire. At capacity the refusal also lists what can be released.+pub fn acquireFail(arena: Allocator, io: Io, err: anyerror, claims: []const Claim, me: Owner) ![]const u8 {+ const text = claim_ledger.acquireFailText(arena, err);+ if (err != error.ClaimFull) return text;+ return list(arena, text, claims, me, try liveness(arena, io, claims));+}++fn liveness(arena: Allocator, io: Io, claims: []const Claim) ![]const bool {+ const live = try arena.alloc(bool, claims.len);+ for (claims, live) |c, *owner_live| owner_live.* = @import("artifact_claim.zig").ownerLive(io, c.owner);+ return live;+}++/// `header`, then one line per claim. `live[i]` is whether `claims[i]`'s+/// recorded owner is still running.+pub fn list(arena: Allocator, header: []const u8, claims: []const Claim, me: Owner, live: []const bool) ![]const u8 {+ var out: Io.Writer.Allocating = .init(arena);+ const w = &out.writer;+ try w.writeAll(header);+ for (claims, live) |c, owner_live| {+ try w.print("\n- {s} {s}", .{ @tagName(c.kind), if (c.key.len > 0) c.key else "(unnamed)" });+ if (c.repo) |repo| try w.print(" in {s}", .{repo});+ if (claim_ledger.sameOwner(c.owner, me)) {+ try w.writeAll(": yours");+ } else if (owner_live) {+ try w.print(": held by live session \"{s}\"", .{c.owner.session});+ continue;+ } else {+ try w.print(": owner session \"{s}\" has exited", .{c.owner.session});+ }+ try w.print("; release with action=release kind={s} key={s}", .{ @tagName(c.kind), c.key });+ if (c.repo) |repo| try w.print(" repo={s}", .{repo});+ }+ return out.written();+}++test "the inventory marks which claims the caller may release" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const me: Owner = .{ .session = "me", .pid = 1, .start_id = 1 };+ const claims = [_]Claim{+ .{ .kind = .issue, .key = "12", .owner = me, .repo = "github.com/org/repo" },+ .{ .kind = .branch, .key = "feature", .owner = .{ .session = "gone", .pid = 2, .start_id = 2 } },+ .{ .kind = .pull_request, .key = "45", .owner = .{ .session = "busy", .pid = 3, .start_id = 3 } },+ };+ const text = try list(arena_state.allocator(), "3/32 claims held:", &claims, me, &.{ true, false, true });+ try std.testing.expectEqualStrings(+ \\3/32 claims held:+ \\- issue 12 in github.com/org/repo: yours; release with action=release kind=issue key=12 repo=github.com/org/repo+ \\- branch feature: owner session "gone" has exited; release with action=release kind=branch key=feature+ \\- pull_request 45: held by live session "busy"+ , text);+}++test "a full ledger and a keyless status list the claims (#1614)" {+ const claims = @import("artifact_claim.zig");+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const ar = arena.allocator();+ const io = std.testing.io;+ claims.resetForTest();+ defer claims.resetForTest();+ claims.setTestOwner(.{ .session = "s-a", .pid = 11, .start_id = 1 });+ try std.testing.expectEqualStrings("no claim", (try claims.handleTool(ar, io, "status", "issue", "", "")).text);+ var i: usize = 0;+ while (i < claim_ledger.max_claims) : (i += 1) {+ const r = try claims.handleTool(ar, io, "claim", "issue", try std.fmt.allocPrint(ar, "{d}", .{i + 1}), "");+ try std.testing.expect(!r.is_error);+ }+ const full = try claims.handleTool(ar, io, "claim", "issue", "999", "");+ try std.testing.expect(full.is_error);+ try std.testing.expect(std.mem.indexOf(u8, full.text, "ledger is full") != null);+ try std.testing.expect(std.mem.indexOf(u8, full.text, "\n- issue 32: yours; release with action=release kind=issue key=32") != null);++ // Another session sees whose claims they are, and which owners exited.+ claims.setTestOwner(.{ .session = "s-b", .pid = 12, .start_id = 2 });+ const held = try claims.handleTool(ar, io, "status", "issue", "", "");+ try std.testing.expect(std.mem.startsWith(u8, held.text, "32/32 claims held:"));+ try std.testing.expect(std.mem.indexOf(u8, held.text, "- issue 1: held by live session \"s-a\"") != null);+ claims.setTestOwnerLive(false);+ const stale = try claims.handleTool(ar, io, "status", "issue", "", "");+ try std.testing.expect(std.mem.indexOf(u8, stale.text, "- issue 1: owner session \"s-a\" has exited; release") != null);+ // A keyed status still answers only for that key.+ try std.testing.expectEqualStrings("no claim", (try claims.handleTool(ar, io, "status", "issue", "999", "")).text);+}diff --git a/src/main.zig b/src/main.zigindex a2b160f7..d5dc93cc 100644--- a/src/main.zig+++ b/src/main.zig@@ -52,7 +52,7 @@ test {_ = .{ @import("http2_buffered.zig"), @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("term_sender.zig") };_ = @import("repo_transaction.zig");- _ = .{ @import("pr_command.zig"), @import("pr_evidence.zig"), @import("artifact_repository.zig"), @import("artifact_claim_ledger.zig"), @import("artifact_claim_store.zig"), @import("artifact_claim_stale.zig"), @import("artifact_claim_unresolved.zig") };+ _ = .{ @import("pr_command.zig"), @import("pr_evidence.zig"), @import("artifact_repository.zig"), @import("artifact_claim_ledger.zig"), @import("artifact_claim_store.zig"), @import("artifact_claim_stale.zig"), @import("artifact_claim_unresolved.zig"), @import("artifact_claim_inventory.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;