graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
peer checkpoint: guard the files themselves, not only git (#469)
Rach Pradhan03b98998c4f6
diff --git a/evals/harness_behavior.jsonl b/evals/harness_behavior.jsonlindex 952f1e88..98906717 100644--- a/evals/harness_behavior.jsonl+++ b/evals/harness_behavior.jsonl@@ -45,3 +45,4 @@{"id": "session-recap-model-silent-without-cheap-rung", "why": "#419: the cheap-model recap is silently disabled when the provider has no ladder rung (lmstudio in the mock rig) - no event, no error, the heuristic recap still lands.", "prompt": "say hi", "script": [{"text": "hi"}], "controls_after_turn": [{"type": "set_mode", "mode": "normal"}], "assert": [{"event": {"type": "session_recap", "source": "heuristic"}}, {"no_event": {"type": "session_recap", "source": "model"}}]}{"id": "peer-message-queued-for-live-peer", "why": "#469 phase 2: a session must be able to message a live co-resident peer (the channel half of the crosstalk fix). The regression is peer_message erroring while a peer is genuinely live, or claiming delivery it did not make.", "live_peer": true, "args": [], "prompt": "tell the other session to hold off gui/src until you land the move", "script": [{"tool": "peer_message", "arguments": {"text": "hold off gui/src until I land the move"}}, {"text": "told them"}], "assert": [{"event": {"type": "tool_call", "name": "peer_message"}}, {"request_contains": {"index": 1, "text": "posted to the worktree channel"}}]}{"id": "peer-message-without-peer-errors-clean", "why": "#469 phase 2: with no live peer, peer_message must come back as a clean error that says so - never a fake delivery, never a crash. The regression is a send reported into the void.", "args": [], "prompt": "tell the other session to hold off", "script": [{"tool": "peer_message", "arguments": {"text": "hold off gui/src"}}, {"text": "nobody to tell"}], "assert": [{"event": {"type": "tool_call", "name": "peer_message"}}, {"request_contains": {"index": 1, "text": "[error] no live co-resident"}}]}+{"id": "peer-checkpoint-blocks-file-writes", "why": "#469: the once-per-peer checkpoint must guard the files themselves, not only git - the incident's other vector was one session moving/editing files the other had open. A live peer makes the first write_file come back un-executed, and the identical retry lands.", "live_peer": true, "args": [], "prompt": "create notes.txt with hello in it", "script": [{"tool": "write_file", "arguments": {"path": "notes.txt", "content": "hello"}}, {"tool": "write_file", "arguments": {"path": "notes.txt", "content": "hello"}}, {"text": "created, after the checkpoint told me about the peer"}], "assert": [{"event": {"type": "tool_call", "name": "write_file"}}, {"request_contains": {"index": 1, "text": "shared-tree checkpoint (#469)"}}, {"request_contains": {"index": 1, "text": "another graff session already owns this worktree"}}]}diff --git a/src/agent_tool_gate.zig b/src/agent_tool_gate.zigindex 043bd2af..86995d98 100644--- a/src/agent_tool_gate.zig+++ b/src/agent_tool_gate.zig@@ -218,7 +218,7 @@ pub fn gateTool(self: *Agent, call: ToolCall) !?ExecResult {// touches the shared index/tree — fires under --yolo too, where no// approvals prompt would ever surface the collision. The model sees// the peer's identity + goal and re-issues the command to proceed.- if (presence.isSharedTreeGit(cmd)) {+ if (presence.isSharedTreeGit(cmd) or presence.isSharedTreeShell(cmd)) {if (presence.gateCheck(self.io, self.arena)) |checkpoint| return .{.text = checkpoint,.is_error = true,@@ -233,6 +233,12 @@ pub fn gateTool(self: *Agent, call: ToolCall) !?ExecResult {elsestd.fmt.bufPrint(&line_buf, "run: {s}", .{cmd}) catch cmd;} else if (std.mem.eql(u8, call.name, "write_file") or std.mem.eql(u8, call.name, "edit_file")) {+ // #469: the same once-per-peer checkpoint guards the files themselves —+ // the incident's other vector was editing a file a live peer had open.+ if (presence.gateCheck(self.io, self.arena)) |checkpoint| return .{+ .text = checkpoint,+ .is_error = true,+ };if (approvals.allowedExact(self.io, call.name)) return null;key = call.name;const path = if (call.input == .object)diff --git a/src/presence.zig b/src/presence.zigindex 3b4c7367..0ce4b929 100644--- a/src/presence.zig+++ b/src/presence.zig@@ -79,6 +79,34 @@ pub fn isSharedTreeGit(cmd: []const u8) bool {return false;}+/// The shell half of the #469 incident vector: `mv` (any form — a rename can+/// disappear a file a peer just wrote) and recursive `rm`. Plain `rm` of one+/// file stays ungated: the checkpoint exists for tree-level disruption, and+/// it fires once per peer regardless, so the odd false positive costs one+/// line, never a workflow.+pub fn isSharedTreeShell(cmd: []const u8) bool {+ var it = std.mem.tokenizeAny(u8, cmd, " \t\r\n;&|\"'`()");+ while (it.next()) |tok| {+ if (std.mem.eql(u8, tok, "mv")) {+ var operands: usize = 0;+ while (it.next()) |arg| {+ if (arg[0] == '-') continue;+ operands += 1;+ }+ if (operands >= 2) return true;+ continue;+ }+ if (std.mem.eql(u8, tok, "rm")) {+ while (it.next()) |arg| {+ if (arg[0] != '-') break;+ if (std.mem.indexOfAny(u8, arg, "rR") != null) return true;+ }+ continue;+ }+ }+ return false;+}+/// The on-disk shape. Older/newer graffs tolerate each other via/// ignore_unknown_fields both ways (a superset write parses down fine).const RecordJson = struct {@@ -310,7 +338,7 @@ pub fn gateCheck(io: Io, arena: Allocator) ?[]const u8 {g_acked_len += 1;}const warning = worktree_lease.duplicateOwnerWarning(arena, peer, unixMs(io) - peer.last_seen_ms);- return std.fmt.allocPrint(arena, "{s}shared-tree checkpoint (#469): the command was NOT run. Re-issue the identical command to proceed — this fires once per live peer — coordinate first via the peer_message tool (session \"{s}\"), or keep your edits disjoint from theirs.", .{ warning, peer.session_id }) catch warning;+ return std.fmt.allocPrint(arena, "{s}shared-tree checkpoint (#469): the action was NOT performed. Re-issue the identical call to proceed — this fires once per live peer, across git mutations, file writes, and shell moves alike — coordinate first via the peer_message tool (session \"{s}\"), or keep your edits disjoint from theirs.", .{ warning, peer.session_id }) catch warning;}// The channel's wire format (Message, chanName, postMessage, readNewMessages)@@ -405,6 +433,20 @@ test "isSharedTreeGit: flags index/tree-mutating git, ignores read-only git" {try std.testing.expect(!isSharedTreeGit("ls src/"));}+test "isSharedTreeShell: flags mv and recursive rm, leaves everyday commands alone" {+ try std.testing.expect(isSharedTreeShell("mv old/ new/"));+ try std.testing.expect(isSharedTreeShell("mv a.ts b.ts"));+ try std.testing.expect(isSharedTreeShell("mv src/a src/b dest/"));+ try std.testing.expect(isSharedTreeShell("rm -rf node_modules"));+ try std.testing.expect(isSharedTreeShell("rm -r build"));+ try std.testing.expect(isSharedTreeShell("cd x && mv a b"));+ try std.testing.expect(!isSharedTreeShell("rm -f .lock"));+ try std.testing.expect(!isSharedTreeShell("rm one-file.txt"));+ try std.testing.expect(!isSharedTreeShell("mv")); // no operands: a usage error, not a tree event+ try std.testing.expect(!isSharedTreeShell("ls -la"));+ try std.testing.expect(!isSharedTreeShell("echo moved"));+}+test "presence record round-trips pid, identity, and goal" {var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);defer arena_state.deinit();