graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
peer channel: proactive peer awareness — the model auto-decides to coordinate (#469)
Rach Pradhanf7855ac4e567
diff --git a/evals/harness_behavior.jsonl b/evals/harness_behavior.jsonlindex cd7dfc9e..a707a02b 100644--- a/evals/harness_behavior.jsonl+++ b/evals/harness_behavior.jsonl@@ -47,3 +47,4 @@{"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"}}]}{"id": "peer-message-all-broadcasts-device-wide", "why": "#469: session 'all' must reach every live session on the device, not only same-folder peers. The regression is a broadcast silently scoped to one worktree.", "live_peer": true, "args": [], "prompt": "tell everyone the freeze starts at five", "script": [{"tool": "peer_message", "arguments": {"session": "all", "text": "freeze starts at five"}}, {"text": "broadcast sent"}], "assert": [{"event": {"type": "tool_call", "name": "peer_message"}}, {"request_contains": {"index": 1, "text": "device-wide room"}}]}+{"id": "peer-awareness-precedes-first-reply", "why": "#469: the model can only auto-decide to coordinate with sessions it knows about - with a live peer, the FIRST request must already carry the presence note (who else is live, with goals), before any collision forces the issue.", "live_peer": true, "args": [], "prompt": "just checking in", "script": [{"text": "acknowledged"}], "assert": [{"request_contains": {"index": 0, "text": "other live graff session"}}, {"request_contains": {"index": 0, "text": "peer_message"}}]}diff --git a/src/peer_channel.zig b/src/peer_channel.zigindex 4506b926..6a1a3d85 100644--- a/src/peer_channel.zig+++ b/src/peer_channel.zig@@ -128,18 +128,57 @@ pub fn handleMessage(self: *Agent, call: ToolCall) !ExecResult {};}-/// The receiving half, called once at the top of a root agent's turn: every-/// queued peer message becomes one durable user-role note in history plus a-/// user-visible event. Never fails a turn — delivery trouble is silence, not-/// an error, because a peer's inbox must never break our session.+var g_peer_fp: u64 = 0; // fingerprint of the last-injected awareness note+var g_peer_note_rewrites: u64 = 0; // history_rewrites when it was injected++/// The proactive half of #469: the model can only auto-decide to coordinate+/// with sessions it KNOWS about — this renders the live peer set (who, where,+/// what goal) as a context note. Returned only when the set changed since the+/// last injection, or a history rewrite may have compacted the note away, so+/// a steady peer set costs zero tokens. Null while the device has no peers at+/// all and none were ever announced — solo sessions never hear about this.+fn peerNoteIfChanged(root: *Agent) ?[]const u8 {+ const peers = presence.liveAllPeers(root.io, root.arena);+ if (peers.len == 0 and g_peer_fp == 0) return null;+ var buf: std.ArrayList(u8) = .empty;+ if (peers.len == 0) {+ buf.appendSlice(root.arena, "[#469 presence] the other live graff sessions are gone — the shared tree is yours alone now.") catch return null;+ } else {+ const mine = presence.ownIdentity();+ buf.appendSlice(root.arena, std.fmt.allocPrint(root.arena, "[#469 presence] {d} other live graff session(s):", .{peers.len}) catch return null) catch return null;+ for (peers) |p| {+ const where: []const u8 = if (std.mem.eql(u8, p.identity, mine)) "this folder" else p.identity;+ buf.appendSlice(root.arena, std.fmt.allocPrint(root.arena, " {s} (goal: {s}, {s});", .{ p.session_id, if (p.goal.len > 0) p.goal else "?", where }) catch break) catch break;+ }+ buf.appendSlice(root.arena, " Coordinate with the peer_message tool before restructuring shared files — bare posts stay in this folder, \"all\" or a name in another folder reaches every session on this device.") catch {};+ }+ const text = buf.items;+ const fp = std.hash.Wyhash.hash(0, text);+ if (fp == g_peer_fp and root.history_rewrites == g_peer_note_rewrites) return null;+ g_peer_fp = fp;+ g_peer_note_rewrites = root.history_rewrites;+ return text;+}++/// The receiving half, called at every step boundary of a root agent's turn:+/// queued peer messages plus the peer-awareness note become one durable+/// user-role note in history plus user-visible events. Never fails a turn —+/// delivery trouble is silence, not an error, because a peer must never break+/// our session.pub fn deliverInbound(root: *Agent) void {if (root.sub) return;+ const note = peerNoteIfChanged(root);const local_msgs = presence.drainChannel(root.io, root.arena);const device_msgs = presence.drainDevice(root.io, root.arena);- if (local_msgs.len == 0 and device_msgs.len == 0) return;+ if (note == null and local_msgs.len == 0 and device_msgs.len == 0) return;const sink = engine_sink.forAgent(root);const own = presence.ownSession();var buf: std.ArrayList(u8) = .empty;+ if (note) |n| {+ buf.appendSlice(root.arena, n) catch {};+ buf.append(root.arena, '\n') catch {};+ sink.emit(root.io, .{ .session_notice = .{ .text = n, .tone = .plain } });+ }for ([2][]const presence.Message{ local_msgs, device_msgs }, [2][]const u8{ "", " · device" }) |msgs, scope| {for (msgs) |m| {const goal = if (m.from_goal.len > 0) std.fmt.allocPrint(root.arena, " (goal: {s})", .{m.from_goal}) catch "" else "";