graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
presence: the executing session is never its own peer (#1424)
justrach500da76ef1e9
diff --git a/src/presence.zig b/src/presence.zigindex 6026a45e..889d4516 100644--- a/src/presence.zig+++ b/src/presence.zig@@ -94,12 +94,20 @@ pub fn ackKey(rec: Owner) u64 {return std.hash.Wyhash.hash(0, text);}+/// #1424: another live process recorded under this very session (a reloaded+/// or relaunched session whose earlier process has not exited) is the session+/// itself, not a peer to coordinate with. Pid alone missed it.+pub fn ownSessionRecord(rec: Owner, my_session: []const u8) bool {+ return my_session.len > 0 and std.mem.eql(u8, rec.session_id, my_session);+}+/// The first live foreign co-owner of `my_identity` that has not been/// acknowledged yet, if any. Pure: the probe results and the ack set are the/// caller's, so tests need no processes and no filesystem.-pub fn unackedPeer(peers: Peers, my_identity: []const u8, my_pid: i32, acked: []const u64) ?Owner {+pub fn unackedPeer(peers: Peers, my_identity: []const u8, my_pid: i32, my_session: []const u8, acked: []const u64) ?Owner {for (peers.records, 0..) |rec, i| {if (i >= peers.probes.len) break;+ if (ownSessionRecord(rec, my_session)) continue;switch (worktree_lease.ownerVerdict(rec, my_identity, my_pid, peers.probes[i])) {.live_foreign, .live_unverified => {const key = ackKey(rec);@@ -320,7 +328,7 @@ pub fn gateCheckIdentity(io: Io, arena: Allocator, identity: []const u8) ?[]consvar dir = Io.Dir.cwd().openDir(io, dir_path, .{ .iterate = true }) catch return null;defer dir.close(io);const peers = listPeers(io, arena, dir);- const peer = unackedPeer(peers, identity, proc_identity.selfPid(), g_acked[0..g_acked_len]) orelse return null;+ const peer = unackedPeer(peers, identity, proc_identity.selfPid(), g_session, g_acked[0..g_acked_len]) orelse return null;if (g_acked_len < g_acked.len) {g_acked[g_acked_len] = ackKey(peer);g_acked_len += 1;@@ -351,6 +359,7 @@ pub fn liveTreePeers(io: Io, arena: Allocator) []const Owner {var live: std.ArrayList(Owner) = .empty;for (peers.records, 0..) |rec, i| {if (i >= peers.probes.len) break;+ if (ownSessionRecord(rec, g_session)) continue;switch (worktree_lease.ownerVerdict(rec, g_identity, g_self.pid, peers.probes[i])) {.live_foreign, .live_unverified => live.append(arena, rec) catch break,else => {},@@ -497,7 +506,7 @@ pub fn liveAllPeers(io: Io, arena: Allocator) []const Owner {var live: std.ArrayList(Owner) = .empty;for (peers.records, 0..) |rec, i| {if (i >= peers.probes.len) break;- if (rec.pid == g_self.pid) continue;+ if (rec.pid == g_self.pid or ownSessionRecord(rec, g_session)) continue;switch (peers.probes[i]) {.gone => {},else => live.append(arena, rec) catch break,diff --git a/src/presence_tests.zig b/src/presence_tests.zigindex 3fe43135..81a30280 100644--- a/src/presence_tests.zig+++ b/src/presence_tests.zig@@ -58,15 +58,15 @@ test "unackedPeer: returns the live foreign co-owner once, then yields to the acconst records = [_]Owner{ other_tree, foreign };const probes = [_]proc_identity.Probe{ .{ .id = 98 }, .{ .id = 99 } };const peers: Peers = .{ .records = &records, .probes = &probes };- const found = unackedPeer(peers, my_identity, 1, &.{}) orelse return error.ExpectedPeer;+ const found = unackedPeer(peers, my_identity, 1, "s-a", &.{}) orelse return error.ExpectedPeer;try std.testing.expectEqualStrings("s-b", found.session_id);const key = ackKey(found);- try std.testing.expect(unackedPeer(peers, my_identity, 1, &.{key}) == null);+ try std.testing.expect(unackedPeer(peers, my_identity, 1, "s-a", &.{key}) == null);// A new session reusing that pid is a NEW peer, not an acked one.const reused: Owner = .{ .pid = 4242, .start_id = 100, .session_id = "s-d", .identity = "/repo/.git" };const records2 = [_]Owner{reused};const probes2 = [_]proc_identity.Probe{.{ .id = 100 }};- try std.testing.expect(unackedPeer(.{ .records = &records2, .probes = &probes2 }, my_identity, 1, &.{key}) != null);+ try std.testing.expect(unackedPeer(.{ .records = &records2, .probes = &probes2 }, my_identity, 1, "s-a", &.{key}) != null);}test "lean one-shots skip the shared-tree checkpoint" {@@ -75,3 +75,20 @@ test "lean one-shots skip the shared-tree checkpoint" {no_local_tools.lean = true;try std.testing.expect(gateCheck(std.testing.io, std.testing.allocator) == null);}++test "the executing session's own records are never a peer, only other sessions are (#1424)" {+ const my_identity = "/repo/.git";+ // A second live process of this same session (a reloaded session whose+ // earlier process is still exiting) and a genuinely separate peer.+ const own_other_pid: Owner = .{ .pid = 5151, .start_id = 7, .session_id = "s-me", .identity = "/repo/.git" };+ const separate: Owner = .{ .pid = 5252, .start_id = 8, .session_id = "s-them", .identity = "/repo/.git" };+ const records = [_]Owner{ own_other_pid, separate };+ const probes = [_]proc_identity.Probe{ .{ .id = 7 }, .{ .id = 8 } };+ const peers: Peers = .{ .records = &records, .probes = &probes };+ const found = unackedPeer(peers, my_identity, 1, "s-me", &.{}) orelse return error.ExpectedPeer;+ try std.testing.expectEqualStrings("s-them", found.session_id);+ // Once the real peer is acknowledged, nothing else asks for confirmation.+ try std.testing.expect(unackedPeer(peers, my_identity, 1, "s-me", &.{ackKey(separate)}) == null);+ try std.testing.expect(presence.ownSessionRecord(own_other_pid, "s-me"));+ try std.testing.expect(!presence.ownSessionRecord(own_other_pid, ""));+}