Skip to content

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

Python

Merge release branch and preserve publication classification coverage

justrachd4cda7214da2
diff --cc src/artifact_claim.zig
index a3bcc77c,33684b6b..61f5c673
--- a/src/artifact_claim.zig
+++ b/src/artifact_claim.zig
@@@ -163,11 -184,11 +163,12 @@@ pub fn isClaimedMutation(cmd: []const u
}
pub fn mutationKind(cmd: []const u8) Kind {
- if (std.mem.indexOf(u8, cmd, "gh issue ") != null) return .issue;
- if (std.mem.indexOf(u8, cmd, "gh pr") != null) return .pull_request;
- if (std.mem.indexOf(u8, cmd, "git push") != null) return .publication;
- if (std.mem.indexOf(u8, cmd, "git commit") != null or std.mem.indexOf(u8, cmd, "git add") != null) return .commit;
- return .publication;
+ return switch (@import("artifact_claim_command.zig").classify(cmd) orelse return .publication) {
++ .issue => .issue,
+ .commit => .commit,
+ .pull_request => .pull_request,
+ .publication => .publication,
+ };
}
pub fn refuseText(arena: Allocator, kind: Kind, key: []const u8, owner: Owner) []const u8 {
diff --cc src/artifact_claim_command.zig
index cd5755c7,00000000..96b5dcc3
mode 100644,000000..100644
--- a/src/artifact_claim_command.zig
+++ b/src/artifact_claim_command.zig
@@@ -1,225 -1,0 +1,257 @@@
+//! Classify claim-protected shell commands without treating inert argument or
+//! heredoc text as commands. This is deliberately a command-shape parser, not
+//! a general shell evaluator: unknown executables make their remaining words
+//! arguments until the next real shell separator.
+
+const std = @import("std");
+
- pub const Kind = enum { commit, pull_request, publication };
- pub const Action = enum { commit, push, pr_create, pr_edit, pr_ready };
++pub const Kind = enum { commit, pull_request, publication, issue };
++pub const Action = enum { commit, push, pr_create, pr_edit, pr_ready, pr_mutation, issue_mutation };
+
+const Token = union(enum) {
+ word: []const u8,
+ separator,
+};
+
+const Lexer = struct {
+ input: []const u8,
+ index: usize = 0,
+ heredoc: ?struct { delimiter: []const u8, strip_tabs: bool } = null,
+
+ fn next(self: *Lexer) ?Token {
+ while (self.index < self.input.len) {
+ const c = self.input[self.index];
+ if (c == ' ' or c == '\t' or c == '\r') {
+ self.index += 1;
+ continue;
+ }
+ if (c == '\n') {
+ self.index += 1;
+ if (self.heredoc != null) self.skipHeredocBody();
+ return .separator;
+ }
+ if (c == ';' or c == '&' or c == '|' or c == '(' or c == ')') {
+ self.index += 1;
+ if (self.index < self.input.len and self.input[self.index] == c and (c == '&' or c == '|')) self.index += 1;
+ return .separator;
+ }
+ if (c == '<' and self.index + 1 < self.input.len and self.input[self.index + 1] == '<') {
+ self.consumeHeredocRedirect();
+ continue;
+ }
+ return .{ .word = self.consumeWord() };
+ }
+ return null;
+ }
+
+ fn consumeWord(self: *Lexer) []const u8 {
+ const start = self.index;
+ var quote: u8 = 0;
+ while (self.index < self.input.len) : (self.index += 1) {
+ const c = self.input[self.index];
+ if (quote == 0 and (std.ascii.isWhitespace(c) or c == ';' or c == '&' or c == '|' or c == '(' or c == ')')) break;
+ if (c == '\\' and quote != '\'') {
+ if (self.index + 1 < self.input.len) self.index += 1;
+ continue;
+ }
+ if (quote == 0 and (c == '\'' or c == '"')) {
+ quote = c;
+ } else if (c == quote) {
+ quote = 0;
+ }
+ }
+ return self.input[start..self.index];
+ }
+
+ fn consumeHeredocRedirect(self: *Lexer) void {
+ self.index += 2;
+ var strip_tabs = false;
+ if (self.index < self.input.len and self.input[self.index] == '-') {
+ strip_tabs = true;
+ self.index += 1;
+ }
+ while (self.index < self.input.len and (self.input[self.index] == ' ' or self.input[self.index] == '\t')) self.index += 1;
+ if (self.index >= self.input.len) return;
+ const delimiter = self.consumeWord();
+ if (delimiter.len > 0) self.heredoc = .{ .delimiter = delimiter, .strip_tabs = strip_tabs };
+ }
+
+ fn skipHeredocBody(self: *Lexer) void {
+ const spec = self.heredoc orelse return;
+ self.heredoc = null;
+ const delimiter = tokenText(spec.delimiter);
+ while (self.index <= self.input.len) {
+ const line_start = self.index;
+ const line_end = std.mem.indexOfScalarPos(u8, self.input, line_start, '\n') orelse self.input.len;
+ var line = std.mem.trimEnd(u8, self.input[line_start..line_end], "\r");
+ if (spec.strip_tabs) line = std.mem.trimStart(u8, line, "\t");
+ self.index = if (line_end < self.input.len) line_end + 1 else self.input.len;
+ if (std.mem.eql(u8, line, delimiter) or line_end == self.input.len) return;
+ }
+ }
+};
+
- const State = enum { command, ignore, git, git_value, gh, gh_value, gh_pr, shell, shell_script };
++const State = enum { command, ignore, git, git_value, gh, gh_value, gh_pr, gh_issue, shell, shell_script };
+
+pub fn action(input: []const u8) ?Action {
++ return scan(input, null);
++}
++
++fn scan(input: []const u8, wanted: ?Action) ?Action {
+ var lexer = Lexer{ .input = input };
+ var state: State = .command;
+ while (lexer.next()) |token| switch (token) {
+ .separator => state = .command,
+ .word => |raw| {
+ const word = tokenText(raw);
+ if (word.len == 0) continue;
+ switch (state) {
+ .command => {
+ if (isAssignment(word)) continue;
+ const exe = std.fs.path.basename(word);
+ if (std.mem.eql(u8, exe, "git")) state = .git else if (std.mem.eql(u8, exe, "gh")) state = .gh else if (isShell(exe)) state = .shell else if (isWrapper(exe)) {} else state = .ignore;
+ },
+ .ignore => {},
+ .git_value => state = .git,
+ .git => {
+ if (std.mem.eql(u8, word, "-C") or std.mem.eql(u8, word, "-c")) {
+ state = .git_value;
+ } else if (word[0] == '-') {
+ continue;
+ } else if (std.mem.eql(u8, word, "push")) {
- return .push;
++ if (wanted == null or wanted == .push) return .push;
++ state = .ignore;
+ } else if (isSharedTreeGit(word)) {
- return .commit;
++ if (wanted == null or wanted == .commit) return .commit;
++ state = .ignore;
+ } else {
+ state = .ignore;
+ }
+ },
+ .gh_value => state = .gh,
+ .gh => {
+ if (std.mem.eql(u8, word, "-R") or std.mem.eql(u8, word, "--repo")) {
+ state = .gh_value;
+ } else if (word[0] == '-') {
+ continue;
+ } else if (std.mem.eql(u8, word, "pr")) {
+ state = .gh_pr;
++ } else if (std.mem.eql(u8, word, "issue")) {
++ state = .gh_issue;
+ } else {
+ state = .ignore;
+ }
+ },
+ .gh_pr => {
+ if (word[0] == '-') continue;
- if (std.mem.eql(u8, word, "create")) return .pr_create;
- if (std.mem.eql(u8, word, "edit")) return .pr_edit;
- if (std.mem.eql(u8, word, "ready")) return .pr_ready;
++ if (std.mem.eql(u8, word, "create") and (wanted == null or wanted == .pr_create)) return .pr_create;
++ if (std.mem.eql(u8, word, "edit") and (wanted == null or wanted == .pr_edit)) return .pr_edit;
++ if (std.mem.eql(u8, word, "ready") and (wanted == null or wanted == .pr_ready)) return .pr_ready;
++ if (isDiscussionMutation(word) and (wanted == null or wanted == .pr_mutation)) return .pr_mutation;
++ state = .ignore;
++ },
++ .gh_issue => {
++ if (word[0] == '-') continue;
++ if ((isDiscussionMutation(word) or std.mem.eql(u8, word, "create") or std.mem.eql(u8, word, "edit")) and (wanted == null or wanted == .issue_mutation)) return .issue_mutation;
+ state = .ignore;
+ },
+ .shell => {
+ if (hasCommandFlag(word)) state = .shell_script;
+ },
+ .shell_script => {
- if (action(tokenText(raw))) |nested| return nested;
++ if (scan(tokenText(raw), wanted)) |nested| return nested;
+ state = .ignore;
+ },
+ }
+ },
+ };
+ return null;
+}
+
+pub fn classify(input: []const u8) ?Kind {
+ return switch (action(input) orelse return null) {
+ .commit => .commit,
+ .push => .publication,
- .pr_create, .pr_edit, .pr_ready => .pull_request,
++ .pr_create, .pr_edit, .pr_ready, .pr_mutation => .pull_request,
++ .issue_mutation => .issue,
+ };
+}
+
+pub fn isPrCreate(input: []const u8) bool {
- return action(input) == .pr_create;
++ return scan(input, .pr_create) != null;
+}
+
+pub fn isPrReady(input: []const u8) bool {
- return action(input) == .pr_ready;
++ return scan(input, .pr_ready) != null;
+}
+
+pub fn isPrEdit(input: []const u8) bool {
- return action(input) == .pr_edit;
++ return scan(input, .pr_edit) != null;
+}
+
+fn tokenText(raw: []const u8) []const u8 {
+ if (raw.len >= 2 and ((raw[0] == '\'' and raw[raw.len - 1] == '\'') or (raw[0] == '"' and raw[raw.len - 1] == '"'))) return raw[1 .. raw.len - 1];
+ return raw;
+}
+
+fn isAssignment(word: []const u8) bool {
+ const eq = std.mem.indexOfScalar(u8, word, '=') orelse return false;
+ if (eq == 0) return false;
+ for (word[0..eq]) |c| if (!(std.ascii.isAlphanumeric(c) or c == '_')) return false;
+ return true;
+}
+
+fn isWrapper(exe: []const u8) bool {
+ for ([_][]const u8{ "command", "env", "exec", "nohup", "sudo", "time" }) |name| if (std.mem.eql(u8, exe, name)) return true;
+ return false;
+}
+
+fn isShell(exe: []const u8) bool {
+ for ([_][]const u8{ "bash", "dash", "ksh", "sh", "zsh" }) |name| if (std.mem.eql(u8, exe, name)) return true;
+ return false;
+}
+
+fn hasCommandFlag(word: []const u8) bool {
+ if (word.len < 2 or word[0] != '-') return false;
+ return std.mem.indexOfScalar(u8, word[1..], 'c') != null;
+}
+
+fn isSharedTreeGit(subcommand: []const u8) bool {
+ for ([_][]const u8{
+ "add", "rm", "mv", "commit", "reset", "restore", "checkout", "switch", "stash", "pull",
+ "rebase", "merge", "cherry-pick", "revert", "am", "apply", "clean", "update-index",
+ }) |name| if (std.mem.eql(u8, subcommand, name)) return true;
+ return false;
+}
+
+test "classify protects actual writes across command boundaries and shell wrappers" {
+ try std.testing.expectEqual(Kind.commit, classify("git -C repo add -A").?);
+ try std.testing.expectEqual(Kind.publication, classify("cd repo && git push origin HEAD").?);
+ try std.testing.expectEqual(Kind.pull_request, classify("env gh -R owner/repo pr create --title x").?);
+ try std.testing.expectEqual(Kind.pull_request, classify("bash -lc 'gh pr ready 42'").?);
+ try std.testing.expect(classify("git status && gh pr checks --watch") == null);
+}
+
+test "#879 quoted arguments and read-only heredocs are not executable publications" {
+ try std.testing.expect(classify("python3 -c 'print(\"gh pr create --title x\")'") == null);
+ try std.testing.expect(classify("grep -n 'git commit -m nope' notes.txt") == null);
+ try std.testing.expect(classify("python3 - <<'PY'\nneedle = 'gh pr create --title x'\nprint(needle)\nPY") == null);
+ try std.testing.expectEqual(Kind.pull_request, classify("python3 - <<'PY'\nprint('gh pr create')\nPY\ngh pr edit 42 --body fixed").?);
+}
++
++fn isDiscussionMutation(word: []const u8) bool {
++ for ([_][]const u8{ "close", "reopen", "comment" }) |verb| if (std.mem.eql(u8, word, verb)) return true;
++ return false;
++}
++
++test "publication predicates inspect later command positions" {
++ try std.testing.expect(isPrCreate("git add file && gh pr create"));
++ try std.testing.expect(isPrReady("git commit -m message && gh pr ready 12"));
++ try std.testing.expect(isPrEdit("git push && gh pr edit 12"));
++}
++
++test "release issue and discussion claim coverage is preserved" {
++ try std.testing.expectEqual(Kind.issue, classify("gh issue comment 12 --body note").?);
++ try std.testing.expectEqual(Kind.pull_request, classify("gh pr close 12").?);
++ try std.testing.expect(classify("echo 'gh issue close 12'") == null);
++}
diff --cc src/main.zig
index a95fede8,d9bab7ba..9c9c2508
--- a/src/main.zig
+++ b/src/main.zig
@@@ -563,8 -562,8 +562,9 @@@ const agentJobsReap = subagent.agentJob
const workflow = @import("workflow_test.zig"); // the engine's tests live here (workflow.zig hit the 600-line cap)
const exec = @import("exec.zig");
test { // ── Unit tests (`zig build test`): pull in tests from imported modules (mcp.zig)
- _ = .{ @import("mcp_apps.zig"), @import("html_view.zig") };
+ _ = .{ @import("mcp_apps.zig"), @import("html_view.zig"), @import("providers_confirmation_tests.zig"), @import("repl_model_confirmation_tests.zig"), @import("repl_model_pick_tests.zig"), @import("engine_sink_citation_tests.zig"), @import("cite_markup_stream_tests.zig"), @import("oneshot_citation_tests.zig"), @import("tui_acp.zig") };
_ = @import("main_test.zig");
++ _ = @import("artifact_claim_command.zig");
_ = @import("session_catalog.zig");
_ = @import("session_prompt.zig");
_ = @import("prompt_astra.zig");
diff --cc src/publish_gate.zig
index 247d7968,07f0075b..c0d4891b
--- a/src/publish_gate.zig
+++ b/src/publish_gate.zig
@@@ -107,23 -110,74 +110,91 @@@ test "#840 conflicting gh pr create nev
try std.testing.expect(std.mem.indexOf(u8, denied.text, "NOT performed") != null);
}
+test "#879 read-only heredoc with publication text bypasses the claim gate" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const ar = arena_state.allocator();
+ artifact_claim.resetForTest();
+ defer artifact_claim.resetForTest();
+ artifact_claim.setTestOwner(.{ .session = "s-owner", .pid = 1, .start_id = 1 });
+ _ = try artifact_claim.handleTool(ar, std.testing.io, "claim", "publication", "feat/x", "");
+ artifact_claim.setTestOwner(.{ .session = "s-reader", .pid = 2, .start_id = 2 });
+ artifact_claim.setTestOwnerLive(true);
+ var agent: Agent = undefined;
+ agent.arena = ar;
+ agent.io = std.testing.io;
+ const probe = "python3 - <<'PY'\nneedle = 'gh pr create --title x'\nprint(needle)\nPY";
+ try std.testing.expect(try bash(&agent, probe) == null);
+}
+
+ test "#840 warm dispatch observes late claims, lock contention and corrupt storage" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const ar = arena_state.allocator();
+ const io = std.testing.io;
+ const path = "zig-cache/claim-dispatch-840.json";
+ const lock_path = path ++ ".lock";
+ std.Io.Dir.cwd().deleteFile(io, path) catch {};
+ defer std.Io.Dir.cwd().deleteFile(io, path) catch {};
+ defer std.Io.Dir.cwd().deleteFile(io, lock_path) catch {};
+ artifact_claim.resetForTest();
+ defer artifact_claim.resetForTest();
+ artifact_claim.setPersistPath(path);
+ artifact_claim.setTestOwner(.{ .session = "reader" });
+ _ = try artifact_claim.handleTool(ar, io, "status", "publication", "feat/a", "");
+ var agent: Agent = undefined;
+ agent.arena = ar;
+ agent.io = io;
+ // A separate writer's persisted state, without resetting the warm reader.
+ const foreign = "[{\"kind\":\"branch\",\"key\":\"feat/a\",\"session\":\"writer\",\"pid\":1,\"start_id\":0}]";
+ try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = foreign });
+ try std.testing.expect((try bash(&agent, "gh pr create --draft --head feat/a")).?.is_error);
+ try std.testing.expect(try bash(&agent, "gh pr list") == null);
+ try std.testing.expect((try bash(&agent, "gh pr create --draft --head feat/a")).?.is_error);
+ try std.testing.expect(try bash(&agent, "gh pr create --draft --head feat/b") == null);
+ for ([_][]const u8{
+ "gh issue list && gh pr create --draft --head feat/a",
+ "gh issue list; gh pr create --draft --head feat/a",
+ "gh issue list\ngh pr create --draft --head feat/a",
+ }) |cmd| try std.testing.expect((try bash(&agent, cmd)).?.is_error);
+ const held = try std.Io.Dir.cwd().createFile(io, lock_path, .{ .truncate = false, .lock = .exclusive, .lock_nonblocking = true });
+ try std.testing.expect((try bash(&agent, "gh pr create --draft --head feat/a")).?.is_error);
+ const blocked = try artifact_claim.handleTool(ar, io, "claim", "issue", "841", "");
+ try std.testing.expect(blocked.is_error);
+ held.close(io);
+ const unchanged = try std.Io.Dir.cwd().readFileAlloc(io, path, ar, .limited(4096));
+ try std.testing.expectEqualStrings(foreign, unchanged);
+ var transferred: artifact_claim.Ledger = .{};
+ try artifact_claim.loadJson(ar, &transferred, foreign);
+ _ = try artifact_claim.handoff(&transferred, ar, .branch, "feat/a", .{ .session = "writer", .pid = 1 }, .{ .session = "reader" }, 0, true);
+ try @import("credential_store.zig").replaceFile(io, std.Io.Dir.cwd(), path, try artifact_claim.persistJson(ar, &transferred), .default_file);
+ try std.testing.expect(try bash(&agent, "gh pr create --draft --head feat/a") == null);
+ artifact_claim.setTestOwner(.{ .session = "writer", .pid = 1 });
+ try std.testing.expect((try bash(&agent, "gh pr create --draft --head feat/a")).?.is_error);
+ _ = try artifact_claim.handleTool(ar, io, "claim", "issue", "840", "");
+ artifact_claim.setTestOwner(.{ .session = "reader" });
+ try std.testing.expect((try bash(&agent, "gh issue edit 840 --title x")).?.is_error);
+ try std.testing.expect(try bash(&agent, "gh issue edit 841 --title x") == null);
+ try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = "not json" });
+ try std.testing.expect((try bash(&agent, "gh pr create --draft")).?.is_error);
+ try std.testing.expect((try artifact_claim.handleTool(ar, io, "claim", "issue", "841", "")).is_error);
+ }
+
+ test "#840 publication branch keys are not compared with PR numbers" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ artifact_claim.resetForTest();
+ defer artifact_claim.resetForTest();
+ artifact_claim.setTestOwner(.{ .session = "owner" });
+ _ = try artifact_claim.handleTool(arena.allocator(), std.testing.io, "claim", "publication", "feat/a", "");
+ artifact_claim.setTestOwner(.{ .session = "reader" });
+ var agent: Agent = undefined;
+ agent.arena = arena.allocator();
+ agent.io = std.testing.io;
+ try std.testing.expect((try bash(&agent, "gh pr edit 123 --title x")).?.is_error);
+ try std.testing.expect(try bash(&agent, "gh pr create --draft --head feat/b") == null);
+ }
+
test "#847 non-draft create with failed head is stopped before bash" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();