graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix: ignore inert publication text in bash claims
yxlyx019e824d96f9
diff --git a/src/artifact_claim.zig b/src/artifact_claim.zigindex 0df79cbb..a3bcc77c 100644--- a/src/artifact_claim.zig+++ b/src/artifact_claim.zig@@ -159,35 +159,15 @@ pub fn handoff(ledger: *Ledger, arena: Allocator, kind: Kind, key: []const u8, f/// Git / GitHub writes that must not proceed under a foreign live claim.pub fn isClaimedMutation(cmd: []const u8) bool {- if (@import("presence_mutate.zig").isSharedTreeGit(cmd)) return true;- var it = std.mem.tokenizeAny(u8, cmd, " \t\r\n;&|\"'`()");- var saw_git = false;- var saw_gh = false;- var saw_pr = false;- while (it.next()) |tok| {- if (std.mem.eql(u8, std.fs.path.basename(tok), "git")) {- saw_git = true;- continue;- }- if (saw_git and (std.mem.eql(u8, tok, "push") or std.mem.eql(u8, tok, "commit") or std.mem.eql(u8, tok, "add"))) return true;- if (std.mem.eql(u8, std.fs.path.basename(tok), "gh")) {- saw_gh = true;- continue;- }- if (saw_gh and std.mem.eql(u8, tok, "pr")) {- saw_pr = true;- continue;- }- if (saw_pr and (std.mem.eql(u8, tok, "create") or std.mem.eql(u8, tok, "edit") or std.mem.eql(u8, tok, "ready"))) return true;- }- return false;+ return @import("artifact_claim_command.zig").classify(cmd) != null;}pub fn mutationKind(cmd: []const u8) Kind {- 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) {+ .commit => .commit,+ .pull_request => .pull_request,+ .publication => .publication,+ };}pub fn refuseText(arena: Allocator, kind: Kind, key: []const u8, owner: Owner) []const u8 {diff --git a/src/artifact_claim_command.zig b/src/artifact_claim_command.zignew file mode 100644index 00000000..cd5755c7--- /dev/null+++ b/src/artifact_claim_command.zig@@ -0,0 +1,225 @@+//! 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 };++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 };++pub fn action(input: []const u8) ?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;+ } else if (isSharedTreeGit(word)) {+ return .commit;+ } 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 {+ 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;+ state = .ignore;+ },+ .shell => {+ if (hasCommandFlag(word)) state = .shell_script;+ },+ .shell_script => {+ if (action(tokenText(raw))) |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,+ };+}++pub fn isPrCreate(input: []const u8) bool {+ return action(input) == .pr_create;+}++pub fn isPrReady(input: []const u8) bool {+ return action(input) == .pr_ready;+}++pub fn isPrEdit(input: []const u8) bool {+ return action(input) == .pr_edit;+}++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").?);+}diff --git a/src/pr_publish.zig b/src/pr_publish.zigindex 45da8990..d0e722fd 100644--- a/src/pr_publish.zig+++ b/src/pr_publish.zig@@ -66,17 +66,18 @@ fn seqAfter(cmd: []const u8, first: []const u8, second: []const u8, third: []con}pub fn isPrCreate(cmd: []const u8) bool {- return seqAfter(cmd, "gh", "pr", "create");+ return @import("artifact_claim_command.zig").isPrCreate(cmd);}pub fn isPrReady(cmd: []const u8) bool {- if (seqAfter(cmd, "gh", "pr", "ready")) {+ const command = @import("artifact_claim_command.zig");+ if (command.isPrReady(cmd)) {var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);defer arena.deinit();const parsed = @import("pr_command.zig").parse(arena.allocator(), cmd) catch return true;return !parsed.has("--undo");}- if (!seqAfter(cmd, "gh", "pr", "edit")) return false;+ if (!command.isPrEdit(cmd)) return false;return std.mem.indexOf(u8, cmd, "--draft=false") != null or std.mem.indexOf(u8, cmd, "--draft false") != null;}diff --git a/src/publish_gate.zig b/src/publish_gate.zigindex 2221587a..247d7968 100644--- a/src/publish_gate.zig+++ b/src/publish_gate.zig@@ -107,6 +107,23 @@ test "#840 conflicting gh pr create never reaches execution after an acknowledgetry 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 "#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();