graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
channel: folder-scoped hearing — the device room is addressed-only
Rach Pradhande98472e182d
diff --git a/src/peer_channel.zig b/src/peer_channel.zigindex be49af4a..9f6f4286 100644--- a/src/peer_channel.zig+++ b/src/peer_channel.zig@@ -31,7 +31,7 @@ const ExecResult = tools_mod.ExecResult;const Owner = worktree_lease.Owner;pub const tool_name = "peer_message";-pub const tool_desc = "Post a message to the shared graff channel: live graff sessions hear it at their next step boundary, stamped with your session name and current goal. Use it to coordinate the way you would steer a subagent, but sideways: announce what you are restructuring, ask the others to hold off a directory, or split the work. Bare posts go to this folder's room; `session` names an intended recipient (name substring, possibly in another folder) or \"all\" to reach every live session on this device — everyone hears the post either way, the name only marks who it's for. Delivery is queued and one-way — the tool result only confirms the post, never a reply.";+pub const tool_desc = "Post a message to the shared graff channel: live graff sessions hear it at their next step boundary, stamped with your session name and current goal. Use it to coordinate the way you would steer a subagent, but sideways: announce what you are restructuring, ask the others to hold off a directory, or split the work. Hearing is folder-scoped: bare posts go to this folder's room and every session in it hears them; `session` names one intended recipient (name substring, possibly in another folder) — a cross-folder post is delivered to the addressed session only, never broadcast. Device-wide broadcast is the user's /tell all, not this tool. Delivery is queued and one-way — the tool result only confirms the post, never a reply.";pub const tool_schema =\\{"type": "object", "properties": {"session": {"type": "string", "description": "peer session name substring (omit for this folder's room), \"all\" for every live session on this device, or a session name in another folder"}, "text": {"type": "string", "description": "one or two sentences of coordination intent"}}, "required": ["text"]};@@ -87,18 +87,15 @@ pub fn handleMessage(self: *Agent, call: ToolCall) !ExecResult {.text = "no live co-resident graff sessions in this worktree — the presence registry says you are alone here (a peer shows up via the startup warning when one starts)",.is_error = true,};- // Routing: bare posts stay in the worktree room; "all" broadcasts to every- // live session on the device; a named target in another folder rides the- // device room with the address as metadata — everyone still hears it.- if (std.mem.eql(u8, want, "all")) {- const everyone = presence.liveAllPeers(self.io, self.arena);- if (everyone.len == 0) return .{ .text = "no live graff sessions on this device at all", .is_error = true };- if (!presence.postToDevice(self.io, self.arena, text, "")) return .{ .text = "delivery failed — the presence registry is unavailable", .is_error = true };- return .{- .text = try std.fmt.allocPrint(self.arena, "posted to the device-wide room — {d} live session(s) across all folders hear it at their next step boundary", .{everyone.len}),- .is_error = false,- };- }+ // Routing: bare posts stay in the worktree room; a named target in+ // another folder rides the device room, delivered to the addressed+ // session only. Model broadcasts are rejected: unaddressed "all" posts+ // flooded every folder on the device with another room's coordination —+ // device-wide broadcast is the user's channel (/tell all).+ if (std.mem.eql(u8, want, "all")) return .{+ .text = "\"all\" is retired for sessions — hearing is folder-scoped now. Post bare for this folder's room, or name one session (possibly in another folder) to reach it directly.",+ .is_error = true,+ };// `session` is validated so a mistyped target errors usefully, but every// peer hears the post either way — the channel is a room, not a DM.const to: []const u8 = if (want.len == 0) "" else switch (resolvePeer(presence.liveAllPeers(self.io, self.arena), want)) {@@ -114,7 +111,7 @@ pub fn handleMessage(self: *Agent, call: ToolCall) !ExecResult {};const cross_folder = to.len > 0 and !isLocal(presence.liveAllPeers(self.io, self.arena), to, presence.ownIdentity());const posted = if (cross_folder)- presence.postToDevice(self.io, self.arena, text, to)+ presence.postToDevice(self.io, self.arena, text, to, false)elsepresence.postTo(self.io, self.arena, text, to);if (!posted) return .{@@ -122,7 +119,7 @@ pub fn handleMessage(self: *Agent, call: ToolCall) !ExecResult {.is_error = true,};const addressed = if (to.len > 0) try std.fmt.allocPrint(self.arena, " (for \"{s}\")", .{to}) else "";- const room: []const u8 = if (cross_folder) "device-wide room" else "worktree channel";+ const room: []const u8 = if (cross_folder) "device room, delivered to the addressed session only" else "worktree channel";return .{.text = try std.fmt.allocPrint(self.arena, "posted to the {s}{s} — live peer(s) hear it at their next step boundary. Delivery is one-way: any answer arrives the same way.", .{ room, addressed }),.is_error = false,@@ -171,6 +168,15 @@ fn peerNoteIfChanged(root: *Agent) ?[]const u8 {pub const backlog_tail_max = 10;var g_backlog_tail_cut = false;+/// Device-room hearing is addressed-only: the line names this session, or the+/// USER posted it (/tell sets from_user). A model's cross-folder post reaches+/// its target and no one else; unaddressed device lines never cross folders.+pub fn deviceHears(m: presence.Message, own: []const u8) bool {+ if (m.from_user) return true;+ if (m.to.len == 0 or own.len == 0) return false;+ return std.mem.indexOf(u8, m.to, own) != null or std.mem.indexOf(u8, own, m.to) != null;+}+pub fn deliverInbound(root: *Agent) void {if (root.sub) return;const note = peerNoteIfChanged(root);@@ -190,6 +196,14 @@ pub fn deliverInbound(root: *Agent) void {if (note == null and local_msgs.len == 0 and device_msgs.len == 0 and omitted == 0) return;const sink = engine_sink.forAgent(root);const own = presence.ownSession();+ // Device room is folder-scoped: hear what names this session or what the+ // user posted; everything else collapses into one marker below.+ var heard: std.ArrayList(presence.Message) = .empty;+ var skipped: usize = 0;+ for (device_msgs) |m| {+ if (deviceHears(m, own)) heard.append(root.arena, m) catch break else skipped += 1;+ }+ device_msgs = heard.items;var buf: std.ArrayList(u8) = .empty;if (note) |n| {buf.appendSlice(root.arena, n) catch {};@@ -204,6 +218,14 @@ pub fn deliverInbound(root: *Agent) void {sink.emit(root.io, .{ .session_notice = .{ .text = marker, .tone = .plain } });}}+ if (skipped > 0) {+ const marker = std.fmt.allocPrint(root.arena, "[#469 device room: {d} message(s) not addressed to this session skipped — folder-scoped hearing]", .{skipped}) catch "";+ if (marker.len > 0) {+ buf.appendSlice(root.arena, marker) catch {};+ buf.append(root.arena, '\n') catch {};+ sink.emit(root.io, .{ .session_notice = .{ .text = marker, .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 "";@@ -255,7 +277,7 @@ pub fn tellCommand(root: *Agent, arena: Allocator, line: []const u8, out: *Io.Wr};const device_wide = broadcast or !isLocal(everyone, to, presence.ownIdentity());const posted = if (device_wide)- presence.postToDevice(root.io, arena, text, to)+ presence.postToDevice(root.io, arena, text, to, true) // the user's line crosses folders; a session's does notelsepresence.postTo(root.io, arena, text, to);if (posted) {@@ -456,3 +478,16 @@ test "summarizeTranscript: last prompt, last words, last tool, from complete lintry std.testing.expectEqualStrings("edit_file", sum.last_tool);try std.testing.expectEqual(5, sum.messages); // the torn tail is skipped}++test "deviceHears: addressed lines and the user's /tell cross folders; broadcasts do not" {+ const own = "session-111-us";+ // A cross-folder post naming this session arrives (substring, either way).+ try std.testing.expect(deviceHears(.{ .to = "session-111" }, own));+ try std.testing.expect(deviceHears(.{ .to = own }, "session-111"));+ // The user's /tell crosses folders even unaddressed (their broadcast).+ try std.testing.expect(deviceHears(.{ .from_user = true }, own));+ // Another folder's addressed work and bare/broadcast lines stay out.+ try std.testing.expect(!deviceHears(.{ .to = "session-999-them" }, own));+ try std.testing.expect(!deviceHears(.{}, own));+ try std.testing.expect(!deviceHears(.{ .to = "session-111" }, ""));+}diff --git a/src/presence.zig b/src/presence.zigindex 0839aad8..1a8508df 100644--- a/src/presence.zig+++ b/src/presence.zig@@ -461,9 +461,9 @@ pub fn liveAllPeers(io: Io, arena: Allocator) []const Owner {return live.items;}-/// Post to the device-wide room. Every announced session hears it, whatever-/// folder it sits in; `to` marks the intended recipient as metadata.-pub fn postToDevice(io: Io, arena: Allocator, text: []const u8, to: []const u8) bool {+/// Post to the device-wide room. Delivery there is addressed-only: heard when+/// `to` names the session or the USER posted it (/tell sets from_user).+pub fn postToDevice(io: Io, arena: Allocator, text: []const u8, to: []const u8, from_user: bool) bool {const dir_path = g_dir orelse return false;if (g_self.pid == 0) return false;var dir = Io.Dir.cwd().openDir(io, dir_path, .{}) catch return false;@@ -476,6 +476,7 @@ pub fn postToDevice(io: Io, arena: Allocator, text: []const u8, to: []const u8).to = to,.ts_ms = unixMs(io),.text = text,+ .from_user = from_user,});}diff --git a/src/presence_chan.zig b/src/presence_chan.zigindex 5c0d5915..7d6cd0f6 100644--- a/src/presence_chan.zig+++ b/src/presence_chan.zig@@ -23,7 +23,9 @@ pub const chan_name_max = 96;/// One channel line. `from_*` identify the sender's presence record at send/// time; the harness writes them, never the model. `to` is a session-name-/// substring naming who the message is FOR — everyone still hears it.+/// substring naming who the message is FOR — the worktree room delivers every+/// line, but the device room is folder-scoped: only addressed lines and the+/// user's own posts (`from_user`, set by /tell) cross folders.pub const Message = struct {from_pid: i32 = 0,from_start: u64 = 0,@@ -32,6 +34,7 @@ pub const Message = struct {to: []const u8 = "",ts_ms: i64 = 0,text: []const u8 = "",+ from_user: bool = false,};/// The channel file for a worktree identity: content-hash, so it is stable