Skip to content

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

Python

Claim gate: an unresolved explicit PR target no longer blames unrelated claims

justrach860d618d064e
diff --git a/src/artifact_claim.zig b/src/artifact_claim.zig
index 90a00e0c..b08eca48 100644
--- a/src/artifact_claim.zig
+++ b/src/artifact_claim.zig
@@ -13,7 +13,7 @@ const tools_mod = @import("tools.zig");
const ExecResult = tools_mod.ExecResult;
const presence = @import("presence.zig");
const proc_identity = @import("proc_identity.zig");
-
+const unresolved_mod = @import("artifact_claim_unresolved.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");
@@ -189,15 +189,19 @@ pub fn gateCommandIn(arena: Allocator, io: Io, cmd: []const u8, key: []const u8,
if (persistJson(scratch.allocator(), ledger)) |json| transaction.write(json) catch {} else |_| {}
}
}
- // A failed `gh pr view` is unknown identity, not "PR N in no repo".
- // explicit() would keep the number and skip a foreign publication claim.
- var target: @import("artifact_claim_target.zig").Target = if (kind == .pull_request and !builtin.is_test)
- resolved orelse .{ .kind = .pull_request, .key = "" }
+ // A failed `gh pr view` is unknown identity, not "PR N in no repo" (#1344).
+ const unresolved = kind == .pull_request and resolved == null and (!builtin.is_test or unresolved_mod.g_force_for_test);
+ var target: @import("artifact_claim_target.zig").Target = if (unresolved) unresolved_mod.target(cmd) else if (kind == .pull_request and !builtin.is_test)
+ resolved.?
else
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| {
if (!claimRelevant(c.kind, kind)) continue;
+ if (unresolved) {
+ if (sameOwner(c.owner, me) or !ownerLive(io, c.owner) or unresolved_mod.provablyUnrelated(c, target)) continue;
+ return unresolved_mod.refuseText(arena, c, target);
+ }
const branch_claim = c.kind == .publication or c.kind == .branch;
if (c.kind == .branch and claim_ledger.differentRepository(c.repo, target.head_repo)) continue;
if (claim_ledger.differentRepository(c.repo, target.repo)) {
diff --git a/src/artifact_claim_unresolved.zig b/src/artifact_claim_unresolved.zig
new file mode 100644
index 00000000..2e4da637
--- /dev/null
+++ b/src/artifact_claim_unresolved.zig
@@ -0,0 +1,78 @@
+//! A PR command whose target could not be looked up (#1340, #1344).
+//!
+//! The claim gate resolves `gh pr <verb> N` to the PR's repository and head
+//! branch before comparing it with foreign claims. When that lookup fails,
+//! the target is unknown and a foreign claim still blocks conservatively,
+//! except a numbered PR claim for a different number, which is provably
+//! another artifact. The refusal then says what could not be resolved and
+//! asks nobody for a handoff: the other session's claim was never shown to be
+//! about this PR.
+
+const std = @import("std");
+const ledger = @import("artifact_claim_ledger.zig");
+const Target = @import("artifact_claim_target.zig").Target;
+
+/// Tests take the unresolved path; production reaches it when the lookup fails.
+pub var g_force_for_test = false;
+
+/// The literal PR number from the command, or an unknown target.
+pub fn target(cmd: []const u8) Target {
+ const t = @import("artifact_claim_target.zig").explicit(cmd, .pull_request) orelse return .{ .kind = .pull_request, .key = "" };
+ return if (t.kind == .pull_request) t else .{ .kind = .pull_request, .key = "" };
+}
+
+/// A different numbered PR is another artifact whatever its branch.
+pub fn provablyUnrelated(c: ledger.Claim, t: Target) bool {
+ return c.kind == .pull_request and t.key.len > 0 and c.key.len > 0 and !std.mem.eql(u8, c.key, t.key);
+}
+
+pub fn refuseText(a: std.mem.Allocator, c: ledger.Claim, t: Target) []const u8 {
+ return std.fmt.allocPrint(a, "artifact claim check: could not resolve which pull request this command targets{s}{s}{s} (the GitHub lookup failed), so another session's live {s} claim {s} blocks it conservatively. NOT performed; no handoff was requested because that claim was not shown to cover this PR. Retry the command, or run it from the PR's own worktree.", .{
+ if (t.key.len > 0) " (#" else "",
+ t.key,
+ if (t.key.len > 0) ")" else "",
+ @tagName(c.kind),
+ if (c.key.len > 0) c.key else "(worktree)",
+ }) catch "artifact claim check: the pull request target could not be resolved; NOT performed";
+}
+
+test "an unresolved explicit PR skips a different numbered claim only" {
+ const t = target("gh pr edit 42 --body-file notes.md");
+ try std.testing.expectEqualStrings("42", t.key);
+ const other: ledger.Claim = .{ .kind = .pull_request, .key = "7", .owner = .{ .session = "peer", .pid = 1 } };
+ const same: ledger.Claim = .{ .kind = .pull_request, .key = "42", .owner = .{ .session = "peer", .pid = 1 } };
+ const branch: ledger.Claim = .{ .kind = .branch, .key = "feature", .owner = .{ .session = "peer", .pid = 1 } };
+ try std.testing.expect(provablyUnrelated(other, t));
+ try std.testing.expect(!provablyUnrelated(same, t));
+ try std.testing.expect(!provablyUnrelated(branch, t));
+ try std.testing.expect(!provablyUnrelated(other, target("gh pr edit --body-file notes.md")));
+ const text = refuseText(std.testing.allocator, branch, t);
+ defer std.testing.allocator.free(text);
+ try std.testing.expect(std.mem.indexOf(u8, text, "(#42)") != null);
+ try std.testing.expect(std.mem.indexOf(u8, text, "no handoff was requested") != null);
+}
+
+test "#1344 the gate: another numbered PR claim does not block, a branch claim explains without a handoff" {
+ const claims = @import("artifact_claim.zig");
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const ar = arena_state.allocator();
+ const io = std.testing.io;
+ claims.resetForTest();
+ defer claims.resetForTest();
+ g_force_for_test = true;
+ defer g_force_for_test = false;
+ claims.setTestOwner(.{ .session = "s-owner" });
+ _ = try claims.handleTool(ar, io, "claim", "pull_request", "7", "");
+ claims.setTestOwner(.{ .session = "s-other" });
+ claims.setTestOwnerLive(true);
+ try std.testing.expect(claims.gateCommand(ar, io, "gh pr edit 42 --body-file notes.md", "") == null);
+ const same = claims.gateCommand(ar, io, "gh pr edit 7 --body-file notes.md", "") orelse return error.TestUnexpectedResult;
+ try std.testing.expect(std.mem.indexOf(u8, same, "could not resolve") != null);
+ claims.setTestOwner(.{ .session = "s-owner" });
+ _ = try claims.handleTool(ar, io, "claim", "branch", "feature", "");
+ claims.setTestOwner(.{ .session = "s-other" });
+ const branch = claims.gateCommand(ar, io, "gh pr edit 42 --body-file notes.md", "") orelse return error.TestUnexpectedResult;
+ try std.testing.expect(std.mem.indexOf(u8, branch, "(#42)") != null);
+ try std.testing.expect(std.mem.indexOf(u8, branch, "no handoff was requested") != null);
+}
diff --git a/src/main.zig b/src/main.zig
index 448a6f04..7899c206 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -53,7 +53,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("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("artifact_claim_stale.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_unresolved.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;
diff --git a/src/pr_command.zig b/src/pr_command.zig
index 8a5a3fbc..06930726 100644
--- a/src/pr_command.zig
+++ b/src/pr_command.zig
@@ -60,7 +60,7 @@ pub const Command = struct {
if (takesValue(arg[0..eq])) continue;
}
var known = false;
- for ([_][]const u8{ "--draft", "--draft=true", "--undo", "--help", "--web", "--force", "--delete-branch", "--squash", "--merge", "--rebase", "--auto", "--disable-auto", "--admin" }) |option| if (std.mem.eql(u8, arg, option)) {
+ for ([_][]const u8{ "--draft", "--draft=true", "--undo", "--help", "--web", "--force", "--delete-branch", "--squash", "--merge", "--rebase", "--auto", "--disable-auto", "--admin", "--remove-milestone", "--edit-last", "--delete-last", "--create-if-none", "--yes" }) |option| if (std.mem.eql(u8, arg, option)) {
known = true;
break;
};
@@ -76,6 +76,10 @@ pub const Command = struct {
fn takesValue(arg: []const u8) bool {
for ([_][]const u8{ "--body", "-b", "--body-file", "-F", "--title", "-t", "--head", "-H", "--base", "-B", "--repo", "-R", "--reviewer", "-r", "--assignee", "-a", "--label", "-l", "--milestone", "-m", "--project", "-p", "--template", "-T" }) |flag| if (std.mem.eql(u8, arg, flag)) return true;
+ // #1344: edit/close/comment/merge options; an unknown one used to make an
+ // explicit `gh pr edit N` unresolvable, so the claim gate blocked on any
+ // foreign claim.
+ for ([_][]const u8{ "--add-label", "--remove-label", "--add-reviewer", "--remove-reviewer", "--add-assignee", "--remove-assignee", "--add-project", "--remove-project", "--comment", "-c", "--subject", "-s", "--match-head-commit", "--author-email", "-A" }) |flag| if (std.mem.eql(u8, arg, flag)) return true;
return false;
}
@@ -170,3 +174,15 @@ test "PR selectors skip known option values and refuse ambiguous options" {
const unknown = try parse(a, "gh pr edit --unknown 12");
try std.testing.expectError(error.UnsupportedOption, unknown.selectorChecked());
}
+
+test "#1344 edit and close options keep an explicit PR selector" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+ for ([_][]const u8{
+ "gh pr edit 42 --repo owner/repo --add-label bug --remove-reviewer someone --body-file notes.md",
+ "gh pr close 42 --comment done --delete-branch",
+ "gh pr comment 42 --edit-last --body x",
+ "gh pr merge 42 --squash --subject s --match-head-commit abc",
+ }) |cmd| try std.testing.expectEqualStrings("42", (try (try parse(a, cmd)).selectorChecked()).?);
+}