feat: broadcast system notifications on peer join/leave

When a peer connects or disconnects, the broker now broadcasts a
system push (subtype: "system") to all other peers in the same mesh.
The CLI formats these as [system] channel notifications so AI sessions
can react to topology changes without polling.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Alejandro Gutiérrez
2026-04-07 23:28:49 +01:00
parent 5cb4cc4fe7
commit 453705a4e1
4 changed files with 148 additions and 17 deletions

View File

@@ -990,6 +990,39 @@ Your message mode is "${messageMode}".
client.onPush(async (msg) => {
if (messageMode === "off") return;
// System events (peer join/leave) — always push, regardless of mode.
if (msg.subtype === "system" && msg.event) {
const eventName = msg.event;
const data = msg.eventData ?? {};
let content: string;
if (eventName === "peer_joined") {
content = `[system] Peer "${data.name ?? "unknown"}" joined the mesh`;
} else if (eventName === "peer_left") {
content = `[system] Peer "${data.name ?? "unknown"}" left the mesh`;
} else {
content = `[system] ${eventName}: ${JSON.stringify(data)}`;
}
try {
await server.notification({
method: "notifications/claude/channel",
params: {
content,
meta: {
kind: "system",
event: eventName,
mesh_slug: client.meshSlug,
mesh_id: client.meshId,
...(Object.keys(data).length > 0 ? { eventData: data } : {}),
},
},
});
process.stderr.write(`[claudemesh] system: ${content}\n`);
} catch (pushErr) {
process.stderr.write(`[claudemesh] system push FAILED: ${pushErr}\n`);
}
return;
}
const fromPubkey = msg.senderPubkey || "";
const fromName = fromPubkey
? await resolvePeerName(client, fromPubkey)