feat: persist peer session state across disconnects ("welcome back" on reconnect)

Save groups, profile, visibility, summary, display name, and cumulative
stats to a new mesh.peer_state table on disconnect. On reconnect (same
meshId + memberId), restore them automatically — hello groups take
precedence over stored groups if provided. Broadcast peer_returned
system event with last-seen time and summary to other peers.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Alejandro Gutiérrez
2026-04-08 00:20:20 +01:00
parent e09671cdcb
commit fc8a7edc23
6 changed files with 315 additions and 17 deletions

View File

@@ -233,6 +233,22 @@ export class BrokerClient {
this.reconnectAttempt = 0;
this.flushOutbound();
this.startStatsReporting();
// Restore cumulative stats from a previous session if available.
if (msg.restored) {
const groups = msg.restoredGroups
? (msg.restoredGroups as Array<{ name: string; role?: string }>).map((g) => g.role ? `@${g.name}:${g.role}` : `@${g.name}`).join(", ")
: "none";
process.stderr.write(
`[claudemesh] session restored — last seen ${msg.lastSeenAt ?? "unknown"}, groups: ${groups}\n`,
);
if (msg.restoredStats) {
const rs = msg.restoredStats as { messagesIn: number; messagesOut: number; toolCalls: number; errors: number };
this._statsCounters.messagesIn = rs.messagesIn ?? 0;
this._statsCounters.messagesOut = rs.messagesOut ?? 0;
this._statsCounters.toolCalls = rs.toolCalls ?? 0;
this._statsCounters.errors = rs.errors ?? 0;
}
}
resolve();
return;
}