@agent-relay/daemon 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/dist/agent-manager.d.ts +134 -0
  2. package/dist/agent-manager.d.ts.map +1 -0
  3. package/dist/agent-manager.js +578 -0
  4. package/dist/agent-manager.js.map +1 -0
  5. package/dist/agent-registry.d.ts +99 -0
  6. package/dist/agent-registry.d.ts.map +1 -0
  7. package/dist/agent-registry.js +213 -0
  8. package/dist/agent-registry.js.map +1 -0
  9. package/dist/agent-signing.d.ts +158 -0
  10. package/dist/agent-signing.d.ts.map +1 -0
  11. package/dist/agent-signing.js +523 -0
  12. package/dist/agent-signing.js.map +1 -0
  13. package/dist/api.d.ts +106 -0
  14. package/dist/api.d.ts.map +1 -0
  15. package/dist/api.js +876 -0
  16. package/dist/api.js.map +1 -0
  17. package/dist/auth.d.ts +94 -0
  18. package/dist/auth.d.ts.map +1 -0
  19. package/dist/auth.js +197 -0
  20. package/dist/auth.js.map +1 -0
  21. package/dist/channel-membership-store.d.ts +55 -0
  22. package/dist/channel-membership-store.d.ts.map +1 -0
  23. package/dist/channel-membership-store.js +176 -0
  24. package/dist/channel-membership-store.js.map +1 -0
  25. package/dist/cli-auth.d.ts +89 -0
  26. package/dist/cli-auth.d.ts.map +1 -0
  27. package/dist/cli-auth.js +792 -0
  28. package/dist/cli-auth.js.map +1 -0
  29. package/dist/cloud-sync.d.ts +150 -0
  30. package/dist/cloud-sync.d.ts.map +1 -0
  31. package/dist/cloud-sync.js +446 -0
  32. package/dist/cloud-sync.js.map +1 -0
  33. package/dist/connection.d.ts +130 -0
  34. package/dist/connection.d.ts.map +1 -0
  35. package/dist/connection.js +438 -0
  36. package/dist/connection.js.map +1 -0
  37. package/dist/consensus-integration.d.ts +167 -0
  38. package/dist/consensus-integration.d.ts.map +1 -0
  39. package/dist/consensus-integration.js +371 -0
  40. package/dist/consensus-integration.js.map +1 -0
  41. package/dist/consensus.d.ts +271 -0
  42. package/dist/consensus.d.ts.map +1 -0
  43. package/dist/consensus.js +632 -0
  44. package/dist/consensus.js.map +1 -0
  45. package/dist/delivery-tracker.d.ts +34 -0
  46. package/dist/delivery-tracker.d.ts.map +1 -0
  47. package/dist/delivery-tracker.js +104 -0
  48. package/dist/delivery-tracker.js.map +1 -0
  49. package/dist/enhanced-features.d.ts +118 -0
  50. package/dist/enhanced-features.d.ts.map +1 -0
  51. package/dist/enhanced-features.js +176 -0
  52. package/dist/enhanced-features.js.map +1 -0
  53. package/dist/index.d.ts +31 -0
  54. package/dist/index.d.ts.map +1 -0
  55. package/dist/index.js +37 -0
  56. package/dist/index.js.map +1 -0
  57. package/dist/migrations/index.d.ts +73 -0
  58. package/dist/migrations/index.d.ts.map +1 -0
  59. package/dist/migrations/index.js +241 -0
  60. package/dist/migrations/index.js.map +1 -0
  61. package/dist/orchestrator.d.ts +217 -0
  62. package/dist/orchestrator.d.ts.map +1 -0
  63. package/dist/orchestrator.js +1143 -0
  64. package/dist/orchestrator.js.map +1 -0
  65. package/dist/rate-limiter.d.ts +68 -0
  66. package/dist/rate-limiter.d.ts.map +1 -0
  67. package/dist/rate-limiter.js +130 -0
  68. package/dist/rate-limiter.js.map +1 -0
  69. package/dist/registry.d.ts +9 -0
  70. package/dist/registry.d.ts.map +1 -0
  71. package/dist/registry.js +9 -0
  72. package/dist/registry.js.map +1 -0
  73. package/dist/relay-ledger.d.ts +261 -0
  74. package/dist/relay-ledger.d.ts.map +1 -0
  75. package/dist/relay-ledger.js +532 -0
  76. package/dist/relay-ledger.js.map +1 -0
  77. package/dist/relay-watchdog.d.ts +125 -0
  78. package/dist/relay-watchdog.d.ts.map +1 -0
  79. package/dist/relay-watchdog.js +611 -0
  80. package/dist/relay-watchdog.js.map +1 -0
  81. package/dist/repo-manager.d.ts +116 -0
  82. package/dist/repo-manager.d.ts.map +1 -0
  83. package/dist/repo-manager.js +384 -0
  84. package/dist/repo-manager.js.map +1 -0
  85. package/dist/router.d.ts +370 -0
  86. package/dist/router.d.ts.map +1 -0
  87. package/dist/router.js +1437 -0
  88. package/dist/router.js.map +1 -0
  89. package/dist/server.d.ts +174 -0
  90. package/dist/server.d.ts.map +1 -0
  91. package/dist/server.js +1001 -0
  92. package/dist/server.js.map +1 -0
  93. package/dist/spawn-manager.d.ts +78 -0
  94. package/dist/spawn-manager.d.ts.map +1 -0
  95. package/dist/spawn-manager.js +165 -0
  96. package/dist/spawn-manager.js.map +1 -0
  97. package/dist/sync-queue.d.ts +116 -0
  98. package/dist/sync-queue.d.ts.map +1 -0
  99. package/dist/sync-queue.js +361 -0
  100. package/dist/sync-queue.js.map +1 -0
  101. package/dist/types.d.ts +133 -0
  102. package/dist/types.d.ts.map +1 -0
  103. package/dist/types.js +6 -0
  104. package/dist/types.js.map +1 -0
  105. package/dist/workspace-manager.d.ts +80 -0
  106. package/dist/workspace-manager.d.ts.map +1 -0
  107. package/dist/workspace-manager.js +314 -0
  108. package/dist/workspace-manager.js.map +1 -0
  109. package/package.json +52 -0
package/dist/router.js ADDED
@@ -0,0 +1,1437 @@
1
+ /**
2
+ * Message router for the agent relay daemon.
3
+ * Handles routing messages between agents, topic subscriptions, and broadcast.
4
+ */
5
+ import { generateId } from '@agent-relay/wrapper';
6
+ import { PROTOCOL_VERSION, } from '@agent-relay/protocol/types';
7
+ import { routerLog } from '@agent-relay/utils/logger';
8
+ import { RateLimiter, NoOpRateLimiter } from './rate-limiter.js';
9
+ import * as crypto from 'node:crypto';
10
+ import { DeliveryTracker, } from './delivery-tracker.js';
11
+ export class Router {
12
+ storage;
13
+ channelMembershipStore;
14
+ connections = new Map(); // connectionId -> Connection
15
+ agents = new Map(); // agentName -> Connection
16
+ subscriptions = new Map(); // topic -> Set<agentName>
17
+ processingAgents = new Map(); // agentName -> processing state
18
+ registry;
19
+ crossMachineHandler;
20
+ deliveryTracker;
21
+ /** Shadow relationships: primaryAgent -> list of shadow configs */
22
+ shadowsByPrimary = new Map();
23
+ /** Reverse lookup: shadowAgent -> primaryAgent (for cleanup) */
24
+ primaryByShadow = new Map();
25
+ /** Channel membership: channel -> Set of member names */
26
+ channels = new Map();
27
+ /** User entities (human users, not agents) */
28
+ users = new Map();
29
+ /** Reverse lookup: member name -> Set of channels they're in */
30
+ memberChannels = new Map();
31
+ /**
32
+ * Agents that are currently being spawned but haven't completed HELLO yet.
33
+ * Maps agent name to timestamp when spawn started.
34
+ * Messages sent to these agents will be queued for delivery after HELLO completes.
35
+ */
36
+ spawningAgents = new Map();
37
+ /** Default timeout for processing indicator (30 seconds) */
38
+ static PROCESSING_TIMEOUT_MS = 30_000;
39
+ /** Timeout for spawning agent entries (60 seconds) */
40
+ static SPAWNING_TIMEOUT_MS = 60_000;
41
+ /** Callback when processing state changes (for real-time dashboard updates) */
42
+ onProcessingStateChange;
43
+ /** Rate limiter for per-agent throttling */
44
+ rateLimiter;
45
+ constructor(options = {}) {
46
+ this.storage = options.storage;
47
+ this.channelMembershipStore = options.channelMembershipStore;
48
+ this.registry = options.registry;
49
+ this.onProcessingStateChange = options.onProcessingStateChange;
50
+ this.crossMachineHandler = options.crossMachineHandler;
51
+ this.deliveryTracker = new DeliveryTracker({
52
+ storage: this.storage,
53
+ delivery: options.delivery,
54
+ getConnection: (id) => this.connections.get(id),
55
+ });
56
+ // Initialize rate limiter (null = disabled)
57
+ this.rateLimiter = options.rateLimit === null
58
+ ? new NoOpRateLimiter()
59
+ : new RateLimiter(options.rateLimit);
60
+ }
61
+ /**
62
+ * Restore channel memberships from persisted storage.
63
+ */
64
+ async restoreChannelMemberships() {
65
+ if (!this.storage && !this.channelMembershipStore)
66
+ return;
67
+ try {
68
+ if (this.channelMembershipStore) {
69
+ const memberships = await this.channelMembershipStore.loadMemberships();
70
+ for (const membership of memberships) {
71
+ this.handleMembershipUpdate({
72
+ channel: membership.channel,
73
+ member: membership.member,
74
+ action: 'join',
75
+ });
76
+ }
77
+ }
78
+ if (this.storage) {
79
+ const messages = await this.storage.getMessages({ order: 'asc' });
80
+ for (const msg of messages) {
81
+ const channel = msg.to;
82
+ const data = msg.data;
83
+ const membership = data?._channelMembership;
84
+ if (!channel || !membership?.member) {
85
+ continue;
86
+ }
87
+ const action = membership.action ?? 'join';
88
+ this.handleMembershipUpdate({
89
+ channel,
90
+ member: membership.member,
91
+ action,
92
+ });
93
+ }
94
+ }
95
+ }
96
+ catch (err) {
97
+ routerLog.error('Failed to restore channel memberships', { error: String(err) });
98
+ }
99
+ }
100
+ /**
101
+ * Set or update the cross-machine handler.
102
+ */
103
+ setCrossMachineHandler(handler) {
104
+ this.crossMachineHandler = handler;
105
+ }
106
+ /**
107
+ * Mark an agent as spawning (before HELLO completes).
108
+ * Messages sent to this agent will be queued for delivery after registration.
109
+ */
110
+ markSpawning(agentName) {
111
+ this.spawningAgents.set(agentName, Date.now());
112
+ routerLog.info(`Agent marked as spawning: ${agentName}`, {
113
+ currentSpawning: Array.from(this.spawningAgents.keys()),
114
+ });
115
+ // Clean up stale spawning entries
116
+ this.cleanupStaleSpawning();
117
+ }
118
+ /**
119
+ * Clear the spawning flag for an agent.
120
+ * Called when agent completes registration or spawn fails.
121
+ */
122
+ clearSpawning(agentName) {
123
+ if (this.spawningAgents.delete(agentName)) {
124
+ routerLog.debug(`Agent spawning flag cleared: ${agentName}`);
125
+ }
126
+ }
127
+ /**
128
+ * Check if an agent is currently spawning.
129
+ */
130
+ isSpawning(agentName) {
131
+ const timestamp = this.spawningAgents.get(agentName);
132
+ if (!timestamp)
133
+ return false;
134
+ // Check if spawn has timed out
135
+ if (Date.now() - timestamp > Router.SPAWNING_TIMEOUT_MS) {
136
+ this.spawningAgents.delete(agentName);
137
+ return false;
138
+ }
139
+ return true;
140
+ }
141
+ /**
142
+ * Clean up spawning entries older than SPAWNING_TIMEOUT_MS.
143
+ */
144
+ cleanupStaleSpawning() {
145
+ const now = Date.now();
146
+ for (const [name, timestamp] of this.spawningAgents) {
147
+ if (now - timestamp > Router.SPAWNING_TIMEOUT_MS) {
148
+ this.spawningAgents.delete(name);
149
+ routerLog.debug(`Cleaned up stale spawning entry: ${name}`);
150
+ }
151
+ }
152
+ }
153
+ /**
154
+ * Register a connection after successful handshake.
155
+ */
156
+ register(connection) {
157
+ this.connections.set(connection.id, connection);
158
+ if (connection.agentName) {
159
+ const isUser = connection.entityType === 'user';
160
+ if (isUser) {
161
+ // Handle existing user connection with same name (disconnect old)
162
+ const existingUser = this.users.get(connection.agentName);
163
+ if (existingUser && existingUser.id !== connection.id) {
164
+ existingUser.close();
165
+ this.connections.delete(existingUser.id);
166
+ }
167
+ this.users.set(connection.agentName, connection);
168
+ routerLog.info(`User registered: ${connection.agentName}`);
169
+ }
170
+ else {
171
+ // Handle existing agent connection with same name (disconnect old)
172
+ const existing = this.agents.get(connection.agentName);
173
+ if (existing && existing.id !== connection.id) {
174
+ routerLog.warn('Duplicate agent connection detected, closing old connection', {
175
+ agent: connection.agentName,
176
+ oldConnectionId: existing.id,
177
+ newConnectionId: connection.id,
178
+ });
179
+ existing.close();
180
+ this.connections.delete(existing.id);
181
+ }
182
+ this.agents.set(connection.agentName, connection);
183
+ // Clear spawning flag now that agent has completed registration
184
+ this.clearSpawning(connection.agentName);
185
+ this.registry?.registerOrUpdate({
186
+ name: connection.agentName,
187
+ cli: connection.cli,
188
+ program: connection.program,
189
+ model: connection.model,
190
+ task: connection.task,
191
+ workingDirectory: connection.workingDirectory,
192
+ });
193
+ }
194
+ }
195
+ }
196
+ /**
197
+ * Unregister a connection.
198
+ */
199
+ unregister(connection) {
200
+ this.connections.delete(connection.id);
201
+ if (connection.agentName) {
202
+ const isUser = connection.entityType === 'user';
203
+ let wasCurrentConnection = false;
204
+ if (isUser) {
205
+ const currentUser = this.users.get(connection.agentName);
206
+ if (currentUser?.id === connection.id) {
207
+ this.users.delete(connection.agentName);
208
+ wasCurrentConnection = true;
209
+ }
210
+ }
211
+ else {
212
+ const current = this.agents.get(connection.agentName);
213
+ if (current?.id === connection.id) {
214
+ this.agents.delete(connection.agentName);
215
+ wasCurrentConnection = true;
216
+ }
217
+ }
218
+ // Only clean up channel/subscription state if this was the current connection.
219
+ // If a new connection replaced this one, we don't want to remove channel memberships
220
+ // that the new connection should inherit.
221
+ if (wasCurrentConnection) {
222
+ // Remove from all subscriptions
223
+ for (const subscribers of this.subscriptions.values()) {
224
+ subscribers.delete(connection.agentName);
225
+ }
226
+ // Remove from all channels and notify remaining members
227
+ this.removeFromAllChannels(connection.agentName);
228
+ // Clean up shadow relationships
229
+ this.unbindShadow(connection.agentName);
230
+ // Clear processing state
231
+ this.clearProcessing(connection.agentName);
232
+ }
233
+ }
234
+ this.clearPendingForConnection(connection.id);
235
+ }
236
+ /**
237
+ * Remove a member from all channels they're in.
238
+ */
239
+ removeFromAllChannels(memberName) {
240
+ const memberChannelSet = this.memberChannels.get(memberName);
241
+ if (!memberChannelSet)
242
+ return;
243
+ for (const channelName of memberChannelSet) {
244
+ const members = this.channels.get(channelName);
245
+ if (members) {
246
+ members.delete(memberName);
247
+ // Clean up empty channels
248
+ if (members.size === 0) {
249
+ this.channels.delete(channelName);
250
+ }
251
+ }
252
+ }
253
+ this.memberChannels.delete(memberName);
254
+ }
255
+ /**
256
+ * Subscribe an agent to a topic.
257
+ */
258
+ subscribe(agentName, topic) {
259
+ let subscribers = this.subscriptions.get(topic);
260
+ if (!subscribers) {
261
+ subscribers = new Set();
262
+ this.subscriptions.set(topic, subscribers);
263
+ }
264
+ subscribers.add(agentName);
265
+ }
266
+ /**
267
+ * Unsubscribe an agent from a topic.
268
+ */
269
+ unsubscribe(agentName, topic) {
270
+ const subscribers = this.subscriptions.get(topic);
271
+ if (subscribers) {
272
+ subscribers.delete(agentName);
273
+ if (subscribers.size === 0) {
274
+ this.subscriptions.delete(topic);
275
+ }
276
+ }
277
+ }
278
+ /**
279
+ * Bind a shadow agent to a primary agent.
280
+ * The shadow will receive copies of messages to/from the primary.
281
+ */
282
+ bindShadow(shadowAgent, primaryAgent, options = {}) {
283
+ // Clean up any existing shadow binding for this shadow
284
+ this.unbindShadow(shadowAgent);
285
+ const relationship = {
286
+ shadowAgent,
287
+ primaryAgent,
288
+ speakOn: options.speakOn ?? ['EXPLICIT_ASK'],
289
+ receiveIncoming: options.receiveIncoming ?? true,
290
+ receiveOutgoing: options.receiveOutgoing ?? true,
291
+ };
292
+ // Add to primary's shadow list
293
+ let shadows = this.shadowsByPrimary.get(primaryAgent);
294
+ if (!shadows) {
295
+ shadows = [];
296
+ this.shadowsByPrimary.set(primaryAgent, shadows);
297
+ }
298
+ shadows.push(relationship);
299
+ // Set reverse lookup
300
+ this.primaryByShadow.set(shadowAgent, primaryAgent);
301
+ routerLog.info(`Shadow bound: ${shadowAgent} -> ${primaryAgent}`, { speakOn: relationship.speakOn });
302
+ }
303
+ /**
304
+ * Unbind a shadow agent from its primary.
305
+ */
306
+ unbindShadow(shadowAgent) {
307
+ const primaryAgent = this.primaryByShadow.get(shadowAgent);
308
+ if (!primaryAgent)
309
+ return;
310
+ // Remove from primary's shadow list
311
+ const shadows = this.shadowsByPrimary.get(primaryAgent);
312
+ if (shadows) {
313
+ const updatedShadows = shadows.filter(s => s.shadowAgent !== shadowAgent);
314
+ if (updatedShadows.length === 0) {
315
+ this.shadowsByPrimary.delete(primaryAgent);
316
+ }
317
+ else {
318
+ this.shadowsByPrimary.set(primaryAgent, updatedShadows);
319
+ }
320
+ }
321
+ // Remove reverse lookup
322
+ this.primaryByShadow.delete(shadowAgent);
323
+ routerLog.info(`Shadow unbound: ${shadowAgent} from ${primaryAgent}`);
324
+ }
325
+ /**
326
+ * Get all shadows for a primary agent.
327
+ */
328
+ getShadowsForPrimary(primaryAgent) {
329
+ return this.shadowsByPrimary.get(primaryAgent) ?? [];
330
+ }
331
+ /**
332
+ * Get the primary agent for a shadow, if any.
333
+ */
334
+ getPrimaryForShadow(shadowAgent) {
335
+ return this.primaryByShadow.get(shadowAgent);
336
+ }
337
+ /**
338
+ * Emit a trigger event for an agent's shadows.
339
+ * Shadows configured to speakOn this trigger will receive a notification.
340
+ * @param primaryAgent The agent whose shadows should be notified
341
+ * @param trigger The trigger event that occurred
342
+ * @param context Optional context data about the trigger
343
+ */
344
+ emitShadowTrigger(primaryAgent, trigger, context) {
345
+ const shadows = this.shadowsByPrimary.get(primaryAgent);
346
+ if (!shadows || shadows.length === 0)
347
+ return;
348
+ for (const shadow of shadows) {
349
+ // Check if this shadow is configured to speak on this trigger
350
+ if (!shadow.speakOn.includes(trigger) && !shadow.speakOn.includes('ALL_MESSAGES')) {
351
+ continue;
352
+ }
353
+ const target = this.agents.get(shadow.shadowAgent);
354
+ if (!target)
355
+ continue;
356
+ // Create a trigger notification envelope
357
+ const triggerEnvelope = {
358
+ v: PROTOCOL_VERSION,
359
+ type: 'SEND',
360
+ id: generateId(),
361
+ ts: Date.now(),
362
+ from: primaryAgent,
363
+ to: shadow.shadowAgent,
364
+ payload: {
365
+ kind: 'action',
366
+ body: `SHADOW_TRIGGER:${trigger}`,
367
+ data: {
368
+ _shadowTrigger: trigger,
369
+ _shadowOf: primaryAgent,
370
+ _triggerContext: context,
371
+ },
372
+ },
373
+ };
374
+ const deliver = this.createDeliverEnvelope(primaryAgent, shadow.shadowAgent, triggerEnvelope, target);
375
+ const sent = target.send(deliver);
376
+ if (sent) {
377
+ this.trackDelivery(target, deliver);
378
+ routerLog.debug(`Shadow trigger ${trigger} sent to ${shadow.shadowAgent}`, { primary: primaryAgent });
379
+ // Set processing state for triggered shadows - they're expected to respond
380
+ this.setProcessing(shadow.shadowAgent, deliver.id);
381
+ }
382
+ }
383
+ }
384
+ /**
385
+ * Check if a shadow should speak based on a specific trigger.
386
+ */
387
+ shouldShadowSpeak(shadowAgent, trigger) {
388
+ const primaryAgent = this.primaryByShadow.get(shadowAgent);
389
+ if (!primaryAgent)
390
+ return true; // Not a shadow, can always speak
391
+ const shadows = this.shadowsByPrimary.get(primaryAgent);
392
+ if (!shadows)
393
+ return true;
394
+ const relationship = shadows.find(s => s.shadowAgent === shadowAgent);
395
+ if (!relationship)
396
+ return true;
397
+ return relationship.speakOn.includes(trigger) || relationship.speakOn.includes('ALL_MESSAGES');
398
+ }
399
+ /**
400
+ * Route a SEND message to its destination(s).
401
+ */
402
+ route(from, envelope) {
403
+ // Check if this is a cross-machine message (injected from cloud)
404
+ const isCrossMachine = envelope.payload?.data?._crossMachine === true;
405
+ // Use envelope.from for cross-machine messages where the connection is the recipient,
406
+ // otherwise use the connection's agent name (normal local messages)
407
+ const senderName = isCrossMachine ? envelope.from : (envelope.from || from.agentName);
408
+ if (!senderName) {
409
+ routerLog.warn('Dropping message - sender has no name');
410
+ return;
411
+ }
412
+ // Skip rate limiting, processing state, and send recording for cross-machine messages
413
+ // These only apply to local agents sending messages
414
+ if (!isCrossMachine) {
415
+ // Check rate limit
416
+ if (!this.rateLimiter.tryAcquire(senderName)) {
417
+ routerLog.warn(`Rate limited: ${senderName}`);
418
+ return;
419
+ }
420
+ // Agent is responding - clear their processing state
421
+ this.clearProcessing(senderName);
422
+ this.registry?.recordSend(senderName);
423
+ }
424
+ const to = envelope.to;
425
+ const topic = envelope.topic;
426
+ routerLog.debug(`Route ${senderName} -> ${to}`, { preview: envelope.payload.body?.substring(0, 50) });
427
+ if (to === '*') {
428
+ // Broadcast to all (except sender)
429
+ this.broadcast(senderName, envelope, topic);
430
+ }
431
+ else if (to === '@users') {
432
+ // Broadcast to all human users only (not agents)
433
+ this.broadcastToUsers(senderName, envelope);
434
+ }
435
+ else if (to) {
436
+ // Direct message
437
+ this.sendDirect(senderName, to, envelope);
438
+ }
439
+ // Route copies to shadows of the sender (outgoing messages)
440
+ this.routeToShadows(senderName, envelope, 'outgoing');
441
+ // Route copies to shadows of the recipient (incoming messages)
442
+ if (to && to !== '*') {
443
+ this.routeToShadows(to, envelope, 'incoming', senderName);
444
+ }
445
+ }
446
+ /**
447
+ * Route a copy of a message to shadows of an agent.
448
+ * @param primaryAgent The primary agent whose shadows should receive the message
449
+ * @param envelope The original message envelope
450
+ * @param direction Whether this is an 'incoming' or 'outgoing' message for the primary
451
+ * @param actualFrom Override the 'from' field (for incoming messages, use original sender)
452
+ */
453
+ routeToShadows(primaryAgent, envelope, direction, actualFrom) {
454
+ const shadows = this.shadowsByPrimary.get(primaryAgent);
455
+ if (!shadows || shadows.length === 0)
456
+ return;
457
+ for (const shadow of shadows) {
458
+ // Check if shadow wants this direction
459
+ if (direction === 'incoming' && shadow.receiveIncoming === false)
460
+ continue;
461
+ if (direction === 'outgoing' && shadow.receiveOutgoing === false)
462
+ continue;
463
+ // Don't send to self
464
+ if (shadow.shadowAgent === (actualFrom ?? primaryAgent))
465
+ continue;
466
+ const target = this.agents.get(shadow.shadowAgent);
467
+ if (!target)
468
+ continue;
469
+ // Create a shadow copy envelope with metadata indicating it's a shadow copy
470
+ const shadowEnvelope = {
471
+ ...envelope,
472
+ payload: {
473
+ ...envelope.payload,
474
+ data: {
475
+ ...envelope.payload.data,
476
+ _shadowCopy: true,
477
+ _shadowOf: primaryAgent,
478
+ _shadowDirection: direction,
479
+ },
480
+ },
481
+ };
482
+ const deliver = this.createDeliverEnvelope(actualFrom ?? primaryAgent, shadow.shadowAgent, shadowEnvelope, target);
483
+ const sent = target.send(deliver);
484
+ if (sent) {
485
+ this.trackDelivery(target, deliver);
486
+ routerLog.debug(`Shadow copy to ${shadow.shadowAgent}`, { direction, primary: primaryAgent });
487
+ // Note: Don't set processing state for shadow copies - shadow stays passive
488
+ }
489
+ }
490
+ }
491
+ /**
492
+ * Send a direct message to a specific agent.
493
+ *
494
+ * If the target agent is offline but known (has connected before),
495
+ * the message is persisted for delivery when the agent reconnects.
496
+ * This prevents silent message drops during brief disconnections or spawn timing issues.
497
+ */
498
+ sendDirect(from, to, envelope) {
499
+ const target = this.agents.get(to) ?? this.users.get(to);
500
+ const isUserTarget = target?.entityType === 'user';
501
+ // If agent not found locally, check if it's on a remote machine
502
+ if (!target) {
503
+ const remoteAgent = this.crossMachineHandler?.isRemoteAgent(to);
504
+ if (remoteAgent) {
505
+ routerLog.info(`Routing to remote agent: ${to}`, { daemonName: remoteAgent.daemonName });
506
+ return this.sendToRemoteAgent(from, to, envelope, remoteAgent);
507
+ }
508
+ // Also check if it's a remote user (human connected via cloud dashboard)
509
+ const remoteUser = this.crossMachineHandler?.isRemoteUser?.(to);
510
+ if (remoteUser) {
511
+ routerLog.info(`Routing to remote user: ${to}`, { daemonName: remoteUser.daemonName });
512
+ return this.sendToRemoteAgent(from, to, envelope, remoteUser);
513
+ }
514
+ // Check if this is a known agent (has connected before) - queue for later delivery
515
+ // This prevents message drops during brief disconnections or spawn timing issues
516
+ if (this.registry?.has(to)) {
517
+ routerLog.info(`Target "${to}" offline but known, queueing message for delivery on reconnect`);
518
+ this.persistMessageForOfflineAgent(from, to, envelope);
519
+ return true; // Message accepted (queued), not dropped
520
+ }
521
+ // Check if agent is currently spawning (pre-HELLO) - queue for delivery after registration
522
+ // This handles the race condition between spawn completion and HELLO handshake
523
+ const spawning = this.isSpawning(to);
524
+ routerLog.debug(`Spawning check for "${to}": ${spawning}`, {
525
+ spawningAgents: Array.from(this.spawningAgents.keys()),
526
+ hasStorage: !!this.storage,
527
+ });
528
+ if (spawning) {
529
+ routerLog.info(`Target "${to}" is spawning, queueing message for delivery after registration`);
530
+ this.persistMessageForOfflineAgent(from, to, envelope);
531
+ return true; // Message accepted (queued), not dropped
532
+ }
533
+ routerLog.warn(`Target "${to}" not found and unknown`, { availableAgents: Array.from(this.agents.keys()), spawningAgents: Array.from(this.spawningAgents.keys()) });
534
+ return false;
535
+ }
536
+ const deliver = this.createDeliverEnvelope(from, to, envelope, target);
537
+ const sent = target.send(deliver);
538
+ routerLog.debug(`Delivered ${from} -> ${to}`, { success: sent, preview: envelope.payload.body?.substring(0, 40) });
539
+ this.persistDeliverEnvelope(deliver);
540
+ if (sent) {
541
+ this.trackDelivery(target, deliver);
542
+ this.registry?.recordReceive(to);
543
+ // Only mark AI agents as processing; humans don't need processing indicators
544
+ if (!isUserTarget) {
545
+ this.setProcessing(to, deliver.id);
546
+ }
547
+ }
548
+ return sent;
549
+ }
550
+ /**
551
+ * Send a message to an agent on a remote machine via cloud.
552
+ */
553
+ sendToRemoteAgent(from, to, envelope, remoteAgent) {
554
+ if (!this.crossMachineHandler) {
555
+ routerLog.warn('Cross-machine handler not available');
556
+ return false;
557
+ }
558
+ // Send asynchronously via cloud
559
+ this.crossMachineHandler.sendCrossMachineMessage(remoteAgent.daemonId, to, from, envelope.payload.body, {
560
+ topic: envelope.topic,
561
+ thread: envelope.payload.thread,
562
+ kind: envelope.payload.kind,
563
+ data: envelope.payload.data,
564
+ originalId: envelope.id,
565
+ }).then((sent) => {
566
+ if (sent) {
567
+ routerLog.info(`Cross-machine message sent to ${to}`, { daemonName: remoteAgent.daemonName });
568
+ // Persist as cross-machine message
569
+ this.storage?.saveMessage({
570
+ id: envelope.id || `cross-${Date.now()}`,
571
+ ts: Date.now(),
572
+ from,
573
+ to,
574
+ topic: envelope.topic,
575
+ kind: envelope.payload.kind,
576
+ body: envelope.payload.body,
577
+ data: {
578
+ ...envelope.payload.data,
579
+ _crossMachine: true,
580
+ _targetDaemon: remoteAgent.daemonId,
581
+ _targetDaemonName: remoteAgent.daemonName,
582
+ },
583
+ thread: envelope.payload.thread,
584
+ status: 'unread',
585
+ is_urgent: false,
586
+ is_broadcast: false,
587
+ }).catch(err => routerLog.error('Failed to persist cross-machine message', { error: String(err) }));
588
+ }
589
+ else {
590
+ routerLog.error(`Failed to send cross-machine message to ${to}`);
591
+ }
592
+ }).catch(err => {
593
+ routerLog.error('Cross-machine send error', { error: String(err) });
594
+ });
595
+ // Return true immediately - message is queued
596
+ return true;
597
+ }
598
+ /**
599
+ * Broadcast to all agents (optionally filtered by topic subscription).
600
+ */
601
+ broadcast(from, envelope, topic) {
602
+ // Build recipients list from both agents and users
603
+ const recipients = topic
604
+ ? this.subscriptions.get(topic) ?? new Set()
605
+ : new Set([...this.agents.keys(), ...this.users.keys()]);
606
+ for (const recipientName of recipients) {
607
+ if (recipientName === from)
608
+ continue; // Don't send to self
609
+ // Check both agents and users maps (consistent with sendDirect)
610
+ const target = this.agents.get(recipientName) ?? this.users.get(recipientName);
611
+ if (target) {
612
+ const isUserTarget = target.entityType === 'user';
613
+ const deliver = this.createDeliverEnvelope(from, recipientName, envelope, target);
614
+ const sent = target.send(deliver);
615
+ this.persistDeliverEnvelope(deliver, true); // Mark as broadcast
616
+ if (sent) {
617
+ this.trackDelivery(target, deliver);
618
+ this.registry?.recordReceive(recipientName);
619
+ // Only mark AI agents as processing; humans don't need processing indicators
620
+ if (!isUserTarget) {
621
+ this.setProcessing(recipientName, deliver.id);
622
+ }
623
+ }
624
+ }
625
+ }
626
+ }
627
+ /**
628
+ * Broadcast a message to all human users (not agents).
629
+ * Used for system notifications that only humans should see.
630
+ */
631
+ broadcastToUsers(from, envelope) {
632
+ for (const [userName, target] of this.users) {
633
+ if (userName === from)
634
+ continue; // Don't send to self
635
+ const deliver = this.createDeliverEnvelope(from, userName, envelope, target);
636
+ const sent = target.send(deliver);
637
+ this.persistDeliverEnvelope(deliver, true); // Mark as broadcast
638
+ if (sent) {
639
+ this.trackDelivery(target, deliver);
640
+ this.registry?.recordReceive(userName);
641
+ routerLog.debug(`Broadcast to user ${userName}`);
642
+ }
643
+ }
644
+ }
645
+ /**
646
+ * Create a DELIVER envelope from a SEND.
647
+ */
648
+ createDeliverEnvelope(from, to, original, target) {
649
+ // Preserve the original 'to' field for broadcasts so agents know to reply to '*'
650
+ const originalTo = original.to;
651
+ return {
652
+ v: PROTOCOL_VERSION,
653
+ type: 'DELIVER',
654
+ id: generateId(),
655
+ ts: Date.now(),
656
+ from,
657
+ to,
658
+ topic: original.topic,
659
+ payload: original.payload,
660
+ payload_meta: original.payload_meta,
661
+ delivery: {
662
+ seq: target.getNextSeq(original.topic ?? 'default', from),
663
+ session_id: target.sessionId,
664
+ originalTo: originalTo !== to ? originalTo : undefined, // Only include if different
665
+ },
666
+ };
667
+ }
668
+ /**
669
+ * Persist a delivered message if storage is configured.
670
+ */
671
+ persistDeliverEnvelope(envelope, isBroadcast = false) {
672
+ if (!this.storage)
673
+ return;
674
+ this.storage.saveMessage({
675
+ id: envelope.id,
676
+ ts: envelope.ts,
677
+ from: envelope.from ?? 'unknown',
678
+ to: envelope.to ?? 'unknown',
679
+ topic: envelope.topic,
680
+ kind: envelope.payload.kind,
681
+ body: envelope.payload.body,
682
+ data: envelope.payload.data,
683
+ payloadMeta: envelope.payload_meta,
684
+ thread: envelope.payload.thread,
685
+ deliverySeq: envelope.delivery.seq,
686
+ deliverySessionId: envelope.delivery.session_id,
687
+ sessionId: envelope.delivery.session_id,
688
+ status: 'unread',
689
+ is_urgent: false,
690
+ is_broadcast: isBroadcast || envelope.to === '*',
691
+ }).catch((err) => {
692
+ routerLog.error('Failed to persist message', { error: String(err) });
693
+ });
694
+ }
695
+ /**
696
+ * Persist a message for an offline agent.
697
+ * Called when a message is sent to a known agent that is not currently connected.
698
+ * The message is marked with _offlineQueued and will be delivered when the agent reconnects.
699
+ */
700
+ persistMessageForOfflineAgent(from, to, envelope) {
701
+ if (!this.storage) {
702
+ routerLog.warn('Cannot queue offline message: no storage configured');
703
+ return;
704
+ }
705
+ routerLog.info(`Persisting offline message for "${to}"`, {
706
+ from,
707
+ messageId: envelope.id,
708
+ bodyPreview: envelope.payload.body?.substring(0, 50),
709
+ });
710
+ this.storage.saveMessage({
711
+ id: envelope.id || generateId(),
712
+ ts: Date.now(),
713
+ from,
714
+ to,
715
+ topic: envelope.topic,
716
+ kind: envelope.payload.kind,
717
+ body: envelope.payload.body,
718
+ data: {
719
+ ...envelope.payload.data,
720
+ _offlineQueued: true, // Mark as queued for offline delivery
721
+ _queuedAt: Date.now(),
722
+ },
723
+ payloadMeta: envelope.payload_meta,
724
+ thread: envelope.payload.thread,
725
+ status: 'unread', // Unread = pending delivery
726
+ is_urgent: false,
727
+ is_broadcast: false,
728
+ }).catch((err) => {
729
+ routerLog.error('Failed to persist offline message', { error: String(err), to });
730
+ });
731
+ }
732
+ /**
733
+ * Deliver pending messages to an agent that just connected.
734
+ * Queries for unread messages addressed to this agent that were queued while offline.
735
+ * This handles messages that were sent while the agent was offline.
736
+ */
737
+ async deliverPendingMessages(connection) {
738
+ const agentName = connection.agentName;
739
+ if (!agentName)
740
+ return;
741
+ if (!this.storage?.getMessages)
742
+ return;
743
+ try {
744
+ // Query for unread messages addressed to this agent
745
+ const pendingMessages = await this.storage.getMessages({
746
+ to: agentName,
747
+ unreadOnly: true,
748
+ order: 'asc', // Deliver oldest first
749
+ });
750
+ // Filter to only include offline-queued messages (not already-delivered unacked messages)
751
+ const offlineMessages = pendingMessages.filter(msg => msg.data?._offlineQueued === true).sort((a, b) => a.ts - b.ts);
752
+ if (offlineMessages.length === 0)
753
+ return;
754
+ routerLog.info(`Delivering ${offlineMessages.length} pending messages to ${agentName}`);
755
+ for (const msg of offlineMessages) {
756
+ // Create deliver envelope
757
+ const deliverEnvelope = {
758
+ v: PROTOCOL_VERSION,
759
+ type: 'DELIVER',
760
+ id: generateId(),
761
+ ts: Date.now(),
762
+ from: msg.from,
763
+ to: agentName,
764
+ topic: msg.topic,
765
+ payload: {
766
+ body: msg.body,
767
+ kind: msg.kind,
768
+ data: msg.data,
769
+ thread: msg.thread,
770
+ },
771
+ payload_meta: msg.payloadMeta,
772
+ delivery: {
773
+ seq: connection.getNextSeq(msg.topic ?? 'default', msg.from),
774
+ session_id: connection.sessionId,
775
+ },
776
+ };
777
+ const sent = connection.send(deliverEnvelope);
778
+ if (sent) {
779
+ this.trackDelivery(connection, deliverEnvelope);
780
+ this.registry?.recordReceive(agentName);
781
+ this.setProcessing(agentName, deliverEnvelope.id);
782
+ // Mark original message as delivered (update status)
783
+ if (this.storage.updateMessageStatus) {
784
+ await this.storage.updateMessageStatus(msg.id, 'read');
785
+ }
786
+ routerLog.info(`Delivered pending message to ${agentName}`, {
787
+ from: msg.from,
788
+ preview: msg.body.substring(0, 40),
789
+ });
790
+ }
791
+ else {
792
+ routerLog.warn(`Failed to deliver pending message to ${agentName}`);
793
+ }
794
+ }
795
+ }
796
+ catch (err) {
797
+ routerLog.error('Failed to deliver pending messages', { error: String(err), agentName });
798
+ }
799
+ }
800
+ /**
801
+ * Get list of connected agent names.
802
+ */
803
+ getAgents() {
804
+ return Array.from(this.agents.keys());
805
+ }
806
+ /**
807
+ * Get connection by agent name.
808
+ */
809
+ getConnection(agentName) {
810
+ return this.agents.get(agentName);
811
+ }
812
+ /**
813
+ * Get number of active connections.
814
+ */
815
+ get connectionCount() {
816
+ return this.connections.size;
817
+ }
818
+ get pendingDeliveryCount() {
819
+ return this.deliveryTracker.pendingCount;
820
+ }
821
+ /**
822
+ * Get rate limiter statistics.
823
+ */
824
+ getRateLimiterStats() {
825
+ return this.rateLimiter.getStats();
826
+ }
827
+ /**
828
+ * Reset rate limit for a specific agent (admin operation).
829
+ */
830
+ resetRateLimit(agentName) {
831
+ this.rateLimiter.reset(agentName);
832
+ }
833
+ /**
834
+ * Get list of agents currently processing (thinking).
835
+ * Returns an object with agent names as keys and processing info as values.
836
+ */
837
+ getProcessingAgents() {
838
+ const result = {};
839
+ for (const [name, state] of this.processingAgents.entries()) {
840
+ result[name] = { startedAt: state.startedAt, messageId: state.messageId };
841
+ }
842
+ return result;
843
+ }
844
+ /**
845
+ * Check if a specific agent is processing.
846
+ */
847
+ isAgentProcessing(agentName) {
848
+ return this.processingAgents.has(agentName);
849
+ }
850
+ /**
851
+ * Mark an agent as processing (called when they receive a message).
852
+ */
853
+ setProcessing(agentName, messageId) {
854
+ // Clear any existing processing state
855
+ this.clearProcessing(agentName);
856
+ const timer = setTimeout(() => {
857
+ this.clearProcessing(agentName);
858
+ routerLog.warn(`Processing timeout for ${agentName}`);
859
+ }, Router.PROCESSING_TIMEOUT_MS);
860
+ this.processingAgents.set(agentName, {
861
+ startedAt: Date.now(),
862
+ messageId,
863
+ timer,
864
+ });
865
+ routerLog.debug(`${agentName} started processing`, { messageId });
866
+ this.onProcessingStateChange?.();
867
+ }
868
+ /**
869
+ * Clear processing state for an agent (called when they send a message).
870
+ */
871
+ clearProcessing(agentName) {
872
+ const state = this.processingAgents.get(agentName);
873
+ if (state) {
874
+ if (state.timer) {
875
+ clearTimeout(state.timer);
876
+ }
877
+ this.processingAgents.delete(agentName);
878
+ routerLog.debug(`${agentName} finished processing`);
879
+ this.onProcessingStateChange?.();
880
+ }
881
+ }
882
+ /**
883
+ * Handle ACK for previously delivered messages.
884
+ */
885
+ handleAck(connection, envelope) {
886
+ const ackId = envelope.payload.ack_id;
887
+ this.deliveryTracker.handleAck(connection.id, ackId);
888
+ }
889
+ /**
890
+ * Clear pending deliveries for a connection (e.g., on disconnect).
891
+ */
892
+ clearPendingForConnection(connectionId) {
893
+ this.deliveryTracker.clearPendingForConnection(connectionId);
894
+ }
895
+ /**
896
+ * Track a delivery and schedule retries until ACKed or TTL/attempts exhausted.
897
+ */
898
+ trackDelivery(target, deliver) {
899
+ this.deliveryTracker.track(target, deliver);
900
+ }
901
+ /**
902
+ * Broadcast a system message to all connected agents.
903
+ * Used for system notifications like agent death announcements.
904
+ */
905
+ broadcastSystemMessage(message, data) {
906
+ const envelope = {
907
+ v: PROTOCOL_VERSION,
908
+ type: 'SEND',
909
+ id: generateId(),
910
+ ts: Date.now(),
911
+ from: '_system',
912
+ to: '*',
913
+ payload: {
914
+ kind: 'message',
915
+ body: message,
916
+ data: {
917
+ ...data,
918
+ _isSystemMessage: true,
919
+ },
920
+ },
921
+ };
922
+ // Broadcast to all agents
923
+ for (const [agentName, connection] of this.agents.entries()) {
924
+ const deliver = this.createDeliverEnvelope('_system', agentName, envelope, connection);
925
+ const sent = connection.send(deliver);
926
+ if (sent) {
927
+ routerLog.debug(`System broadcast sent to ${agentName}`);
928
+ }
929
+ }
930
+ }
931
+ /**
932
+ * Replay any pending (unacked) messages for a resumed session.
933
+ */
934
+ async replayPending(connection) {
935
+ if (!this.storage?.getPendingMessagesForSession || !connection.agentName) {
936
+ return;
937
+ }
938
+ const pending = await this.storage.getPendingMessagesForSession(connection.agentName, connection.sessionId);
939
+ if (!pending.length)
940
+ return;
941
+ routerLog.info(`Replaying ${pending.length} messages to ${connection.agentName}`);
942
+ for (const msg of pending) {
943
+ const deliver = {
944
+ v: PROTOCOL_VERSION,
945
+ type: 'DELIVER',
946
+ id: msg.id,
947
+ ts: msg.ts,
948
+ from: msg.from,
949
+ to: msg.to,
950
+ topic: msg.topic,
951
+ payload: {
952
+ kind: msg.kind,
953
+ body: msg.body,
954
+ data: msg.data,
955
+ thread: msg.thread,
956
+ },
957
+ payload_meta: msg.payloadMeta,
958
+ delivery: {
959
+ seq: msg.deliverySeq ?? connection.getNextSeq(msg.topic ?? 'default', msg.from),
960
+ session_id: msg.deliverySessionId ?? connection.sessionId,
961
+ },
962
+ };
963
+ const sent = connection.send(deliver);
964
+ if (sent) {
965
+ this.trackDelivery(connection, deliver);
966
+ }
967
+ }
968
+ }
969
+ // ==================== Channel Methods ====================
970
+ /**
971
+ * Handle a CHANNEL_JOIN message.
972
+ * Adds the member to the channel and notifies existing members.
973
+ * If payload.member is set, adds that member (admin mode).
974
+ * Otherwise, adds the connection's agent name.
975
+ */
976
+ handleChannelJoin(connection, envelope) {
977
+ // Use payload.member if provided (admin mode), otherwise use connection's name
978
+ const memberName = envelope.payload.member ?? connection.agentName;
979
+ if (!memberName) {
980
+ routerLog.warn('CHANNEL_JOIN from connection without name and no member specified');
981
+ return;
982
+ }
983
+ const channel = envelope.payload.channel;
984
+ const isAdminJoin = Boolean(envelope.payload.member);
985
+ // Get or create channel
986
+ let members = this.channels.get(channel);
987
+ if (!members) {
988
+ members = new Set();
989
+ this.channels.set(channel, members);
990
+ }
991
+ // Check if already a member
992
+ if (members.has(memberName)) {
993
+ routerLog.debug(`${memberName} already in ${channel}`);
994
+ return;
995
+ }
996
+ // Only notify existing members for non-admin joins (agents joining themselves)
997
+ // Admin joins are silent to avoid spamming notifications when syncing
998
+ if (!isAdminJoin) {
999
+ const existingMembers = members ? Array.from(members) : [];
1000
+ for (const existingMember of existingMembers) {
1001
+ const memberConn = this.getConnectionByName(existingMember);
1002
+ if (memberConn) {
1003
+ const joinNotification = {
1004
+ v: PROTOCOL_VERSION,
1005
+ type: 'CHANNEL_JOIN',
1006
+ id: generateId(),
1007
+ ts: Date.now(),
1008
+ from: memberName,
1009
+ payload: envelope.payload,
1010
+ };
1011
+ memberConn.send(joinNotification);
1012
+ }
1013
+ }
1014
+ }
1015
+ const added = this.addChannelMember(channel, memberName, { persist: true });
1016
+ if (!added) {
1017
+ routerLog.debug(`${memberName} already in ${channel}`);
1018
+ return;
1019
+ }
1020
+ routerLog.info(`${memberName} joined ${channel} (${this.channels.get(channel)?.size ?? 0} members)${isAdminJoin ? ' [admin]' : ''}`);
1021
+ }
1022
+ /**
1023
+ * Handle a CHANNEL_LEAVE message.
1024
+ * Removes the member from the channel and notifies remaining members.
1025
+ * If payload.member is provided, removes that member instead (admin mode).
1026
+ */
1027
+ handleChannelLeave(connection, envelope) {
1028
+ // Use payload.member if provided (admin mode), otherwise use connection's name
1029
+ const memberName = envelope.payload.member ?? connection.agentName;
1030
+ if (!memberName) {
1031
+ routerLog.warn('CHANNEL_LEAVE from connection without name and no member specified');
1032
+ return;
1033
+ }
1034
+ const channel = envelope.payload.channel;
1035
+ const isAdminRemove = Boolean(envelope.payload.member);
1036
+ const members = this.channels.get(channel);
1037
+ if (!members || !members.has(memberName)) {
1038
+ routerLog.debug(`${memberName} not in ${channel}, ignoring leave`);
1039
+ return;
1040
+ }
1041
+ const removed = this.removeChannelMember(channel, memberName, { persist: true });
1042
+ if (!removed) {
1043
+ routerLog.debug(`${memberName} not in ${channel}, ignoring leave`);
1044
+ return;
1045
+ }
1046
+ // Only notify remaining members for non-admin removes
1047
+ // Admin removes are silent to avoid spamming notifications
1048
+ if (!isAdminRemove) {
1049
+ const remainingMembers = this.channels.get(channel);
1050
+ if (remainingMembers) {
1051
+ for (const remainingMember of remainingMembers) {
1052
+ const memberConn = this.getConnectionByName(remainingMember);
1053
+ if (memberConn) {
1054
+ const leaveNotification = {
1055
+ v: PROTOCOL_VERSION,
1056
+ type: 'CHANNEL_LEAVE',
1057
+ id: generateId(),
1058
+ ts: Date.now(),
1059
+ from: memberName,
1060
+ payload: envelope.payload,
1061
+ };
1062
+ memberConn.send(leaveNotification);
1063
+ }
1064
+ }
1065
+ }
1066
+ }
1067
+ routerLog.info(`${memberName} left ${channel}${isAdminRemove ? ' [admin]' : ''}`);
1068
+ }
1069
+ /**
1070
+ * Route a channel message to all members except the sender.
1071
+ */
1072
+ routeChannelMessage(connection, envelope) {
1073
+ const senderName = connection.agentName;
1074
+ if (!senderName) {
1075
+ routerLog.warn('CHANNEL_MESSAGE from connection without name');
1076
+ return;
1077
+ }
1078
+ const channel = envelope.payload.channel;
1079
+ const members = this.channels.get(channel);
1080
+ routerLog.info(`routeChannelMessage: channel=${channel} sender=${senderName} members=${members ? Array.from(members).join(',') : 'NONE'}`);
1081
+ if (!members) {
1082
+ routerLog.warn(`Message to non-existent channel ${channel} (available channels: ${Array.from(this.channels.keys()).join(', ')})`);
1083
+ return;
1084
+ }
1085
+ // Case-insensitive membership check
1086
+ const senderMemberName = this.findMemberInSet(members, senderName);
1087
+ if (!senderMemberName) {
1088
+ routerLog.warn(`${senderName} not a member of ${channel} (members: ${Array.from(members).join(', ')})`);
1089
+ return;
1090
+ }
1091
+ // Route to all members except the sender (no echo)
1092
+ const allMembers = Array.from(members);
1093
+ routerLog.info(`Routing channel message from ${senderName} to ${channel}`, {
1094
+ totalMembers: allMembers.length,
1095
+ members: allMembers,
1096
+ });
1097
+ let deliveredCount = 0;
1098
+ const undeliveredMembers = [];
1099
+ const connectedAgents = Array.from(this.agents.keys());
1100
+ const connectedUsers = Array.from(this.users.keys());
1101
+ routerLog.info(`Connected entities: agents=[${connectedAgents.join(',')}] users=[${connectedUsers.join(',')}]`);
1102
+ for (const memberName of members) {
1103
+ // Case-insensitive comparison to skip sender
1104
+ if (this.namesMatch(memberName, senderName)) {
1105
+ continue;
1106
+ }
1107
+ const memberConn = this.getConnectionByName(memberName);
1108
+ if (memberConn) {
1109
+ const deliverEnvelope = {
1110
+ v: PROTOCOL_VERSION,
1111
+ type: 'CHANNEL_MESSAGE',
1112
+ id: generateId(),
1113
+ ts: Date.now(),
1114
+ from: senderName,
1115
+ payload: envelope.payload,
1116
+ };
1117
+ const sent = memberConn.send(deliverEnvelope);
1118
+ if (sent) {
1119
+ deliveredCount++;
1120
+ routerLog.info(`Delivered to ${memberName} (${memberConn.entityType || 'agent'})`);
1121
+ }
1122
+ else {
1123
+ routerLog.warn(`Failed to send to ${memberName}`);
1124
+ undeliveredMembers.push(memberName);
1125
+ }
1126
+ }
1127
+ else {
1128
+ routerLog.warn(`Member ${memberName} is registered in channel but NOT connected to daemon - message not delivered`);
1129
+ undeliveredMembers.push(memberName);
1130
+ }
1131
+ }
1132
+ // Persist channel message
1133
+ this.persistChannelMessage(envelope, senderName);
1134
+ const recipientCount = allMembers.length - 1; // Exclude sender
1135
+ routerLog.info(`${senderName} -> ${channel}: delivered to ${deliveredCount}/${recipientCount} members`);
1136
+ // Log warning if some members didn't receive the message
1137
+ if (undeliveredMembers.length > 0) {
1138
+ routerLog.warn(`Channel message undelivered to: [${undeliveredMembers.join(', ')}] - these agents may need to reconnect to the relay daemon`);
1139
+ }
1140
+ }
1141
+ /**
1142
+ * Persist a channel message to storage.
1143
+ */
1144
+ persistChannelMessage(envelope, from) {
1145
+ if (!this.storage)
1146
+ return;
1147
+ const payloadData = {
1148
+ ...envelope.payload.data,
1149
+ _isChannelMessage: true,
1150
+ _channel: envelope.payload.channel,
1151
+ _mentions: envelope.payload.mentions,
1152
+ };
1153
+ this.storage.saveMessage({
1154
+ id: envelope.id,
1155
+ ts: envelope.ts,
1156
+ from,
1157
+ to: envelope.payload.channel, // Channel name as "to"
1158
+ topic: undefined,
1159
+ kind: 'message',
1160
+ body: envelope.payload.body,
1161
+ data: payloadData,
1162
+ thread: envelope.payload.thread,
1163
+ status: 'unread',
1164
+ is_urgent: false,
1165
+ is_broadcast: true, // Channel messages are effectively broadcasts
1166
+ }).catch((err) => {
1167
+ routerLog.error('Failed to persist channel message', { error: String(err) });
1168
+ });
1169
+ }
1170
+ persistChannelMembership(channel, member, action, opts) {
1171
+ if (this.storage) {
1172
+ this.storage.saveMessage({
1173
+ id: crypto.randomUUID(),
1174
+ ts: Date.now(),
1175
+ from: '__system__',
1176
+ to: channel,
1177
+ topic: undefined,
1178
+ kind: 'state', // membership events stored as state
1179
+ body: `${action}:${member}`,
1180
+ data: {
1181
+ _channelMembership: {
1182
+ member,
1183
+ action,
1184
+ invitedBy: opts?.invitedBy,
1185
+ },
1186
+ },
1187
+ status: 'read',
1188
+ is_urgent: false,
1189
+ is_broadcast: true,
1190
+ }).catch((err) => {
1191
+ routerLog.error('Failed to persist channel membership', { error: String(err) });
1192
+ });
1193
+ }
1194
+ if (this.channelMembershipStore) {
1195
+ const persistPromise = action === 'leave'
1196
+ ? this.channelMembershipStore.removeMember(channel, member)
1197
+ : this.channelMembershipStore.addMember(channel, member);
1198
+ persistPromise.catch((err) => {
1199
+ routerLog.error('Failed to sync channel membership to cloud store', {
1200
+ channel,
1201
+ member,
1202
+ action,
1203
+ error: err instanceof Error ? err.message : String(err),
1204
+ });
1205
+ });
1206
+ }
1207
+ }
1208
+ /**
1209
+ * Get all members of a channel.
1210
+ */
1211
+ getChannelMembers(channel) {
1212
+ const members = this.channels.get(channel);
1213
+ return members ? Array.from(members) : [];
1214
+ }
1215
+ /**
1216
+ * Get all channels.
1217
+ */
1218
+ getChannels() {
1219
+ return Array.from(this.channels.keys());
1220
+ }
1221
+ /**
1222
+ * Get all channels a member is in.
1223
+ */
1224
+ getChannelsForMember(memberName) {
1225
+ const channels = this.memberChannels.get(memberName);
1226
+ return channels ? Array.from(channels) : [];
1227
+ }
1228
+ /**
1229
+ * Check if a name belongs to a user (not an agent).
1230
+ */
1231
+ isUser(name) {
1232
+ return this.users.has(name);
1233
+ }
1234
+ /**
1235
+ * Check if a name belongs to an agent (not a user).
1236
+ */
1237
+ isAgent(name) {
1238
+ return this.agents.has(name);
1239
+ }
1240
+ /**
1241
+ * Get list of connected user names (human users only).
1242
+ */
1243
+ getUsers() {
1244
+ return Array.from(this.users.keys());
1245
+ }
1246
+ /**
1247
+ * Get a connection by name (checks both agents and users).
1248
+ * Uses case-insensitive lookup to handle mismatched casing.
1249
+ */
1250
+ getConnectionByName(name) {
1251
+ // Try exact match first
1252
+ const exact = this.agents.get(name) ?? this.users.get(name);
1253
+ if (exact)
1254
+ return exact;
1255
+ // Fall back to case-insensitive search
1256
+ const lowerName = name.toLowerCase();
1257
+ for (const [key, conn] of this.agents) {
1258
+ if (key.toLowerCase() === lowerName)
1259
+ return conn;
1260
+ }
1261
+ for (const [key, conn] of this.users) {
1262
+ if (key.toLowerCase() === lowerName)
1263
+ return conn;
1264
+ }
1265
+ return undefined;
1266
+ }
1267
+ /**
1268
+ * Check if a member is in a Set (case-insensitive).
1269
+ * Returns the actual stored name if found, undefined otherwise.
1270
+ */
1271
+ findMemberInSet(members, name) {
1272
+ // Try exact match first
1273
+ if (members.has(name))
1274
+ return name;
1275
+ // Fall back to case-insensitive search
1276
+ const lowerName = name.toLowerCase();
1277
+ for (const member of members) {
1278
+ if (member.toLowerCase() === lowerName)
1279
+ return member;
1280
+ }
1281
+ return undefined;
1282
+ }
1283
+ /**
1284
+ * Check if two names match (case-insensitive).
1285
+ */
1286
+ namesMatch(a, b) {
1287
+ return a.toLowerCase() === b.toLowerCase();
1288
+ }
1289
+ /**
1290
+ * Auto-join a member to a channel without notifications.
1291
+ * Used for default channel membership (e.g., #general).
1292
+ * @param memberName - The agent or user name to add
1293
+ * @param channel - The channel to join (e.g., '#general')
1294
+ */
1295
+ autoJoinChannel(memberName, channel, options) {
1296
+ // Get or create channel
1297
+ let members = this.channels.get(channel);
1298
+ if (!members) {
1299
+ members = new Set();
1300
+ this.channels.set(channel, members);
1301
+ }
1302
+ // Check if already a member
1303
+ const added = this.addChannelMember(channel, memberName, { persist: options?.persist });
1304
+ if (added) {
1305
+ routerLog.debug(`Auto-joined ${memberName} to ${channel}`);
1306
+ }
1307
+ }
1308
+ addChannelMember(channel, memberName, options) {
1309
+ let members = this.channels.get(channel);
1310
+ if (!members) {
1311
+ members = new Set();
1312
+ this.channels.set(channel, members);
1313
+ }
1314
+ // Case-insensitive check for existing membership
1315
+ const existingMember = this.findMemberInSet(members, memberName);
1316
+ if (existingMember) {
1317
+ return false;
1318
+ }
1319
+ members.add(memberName);
1320
+ const memberChannelSet = this.memberChannels.get(memberName) ?? new Set();
1321
+ memberChannelSet.add(channel);
1322
+ this.memberChannels.set(memberName, memberChannelSet);
1323
+ if (options?.persist ?? true) {
1324
+ this.persistChannelMembership(channel, memberName, 'join');
1325
+ }
1326
+ return true;
1327
+ }
1328
+ removeChannelMember(channel, memberName, options) {
1329
+ const members = this.channels.get(channel);
1330
+ if (!members) {
1331
+ return false;
1332
+ }
1333
+ // Case-insensitive lookup to find actual stored name
1334
+ const actualMemberName = this.findMemberInSet(members, memberName);
1335
+ if (!actualMemberName) {
1336
+ return false;
1337
+ }
1338
+ members.delete(actualMemberName);
1339
+ if (members.size === 0) {
1340
+ this.channels.delete(channel);
1341
+ }
1342
+ // Also try case-insensitive for memberChannels cleanup
1343
+ const memberChannelSet = this.memberChannels.get(actualMemberName) ?? this.memberChannels.get(memberName);
1344
+ if (memberChannelSet) {
1345
+ memberChannelSet.delete(channel);
1346
+ if (memberChannelSet.size === 0) {
1347
+ this.memberChannels.delete(actualMemberName);
1348
+ this.memberChannels.delete(memberName); // Clean up both potential keys
1349
+ }
1350
+ }
1351
+ if (options?.persist ?? true) {
1352
+ this.persistChannelMembership(channel, actualMemberName, 'leave');
1353
+ }
1354
+ return true;
1355
+ }
1356
+ handleMembershipUpdate(update) {
1357
+ if (!update.channel || !update.member) {
1358
+ return;
1359
+ }
1360
+ if (update.action === 'leave') {
1361
+ this.removeChannelMember(update.channel, update.member, { persist: false });
1362
+ }
1363
+ else {
1364
+ this.addChannelMember(update.channel, update.member, { persist: false });
1365
+ }
1366
+ }
1367
+ /**
1368
+ * Auto-rejoin an agent to their persisted channels on reconnect.
1369
+ * This handles daemon restarts where in-memory channel state is lost.
1370
+ * Queries both cloud DB (if available) and SQLite storage for memberships.
1371
+ * Uses silent/admin mode to avoid spamming join notifications.
1372
+ */
1373
+ async autoRejoinChannelsForAgent(agentName) {
1374
+ const channelsToJoin = new Set();
1375
+ // Query cloud DB if available
1376
+ if (this.channelMembershipStore?.loadMembershipsForAgent) {
1377
+ try {
1378
+ const cloudMemberships = await this.channelMembershipStore.loadMembershipsForAgent(agentName);
1379
+ for (const membership of cloudMemberships) {
1380
+ channelsToJoin.add(membership.channel);
1381
+ }
1382
+ if (cloudMemberships.length > 0) {
1383
+ routerLog.debug(`Found ${cloudMemberships.length} channel memberships for ${agentName} in cloud DB`);
1384
+ }
1385
+ }
1386
+ catch (err) {
1387
+ routerLog.error('Failed to query cloud DB for channel memberships', {
1388
+ agentName,
1389
+ error: String(err),
1390
+ });
1391
+ }
1392
+ }
1393
+ // Query SQLite storage if available
1394
+ if (this.storage?.getChannelMembershipsForAgent) {
1395
+ try {
1396
+ const sqliteMemberships = await this.storage.getChannelMembershipsForAgent(agentName);
1397
+ for (const channel of sqliteMemberships) {
1398
+ channelsToJoin.add(channel);
1399
+ }
1400
+ if (sqliteMemberships.length > 0) {
1401
+ routerLog.debug(`Found ${sqliteMemberships.length} channel memberships for ${agentName} in SQLite`);
1402
+ }
1403
+ }
1404
+ catch (err) {
1405
+ routerLog.error('Failed to query SQLite for channel memberships', {
1406
+ agentName,
1407
+ error: String(err),
1408
+ });
1409
+ }
1410
+ }
1411
+ if (channelsToJoin.size === 0) {
1412
+ routerLog.debug(`No persisted channel memberships found for ${agentName}`);
1413
+ return;
1414
+ }
1415
+ // Rejoin channels silently (don't notify other members)
1416
+ let rejoinedCount = 0;
1417
+ for (const channel of channelsToJoin) {
1418
+ // Skip if already in channel (handles deduplication)
1419
+ const members = this.channels.get(channel);
1420
+ if (members && this.findMemberInSet(members, agentName)) {
1421
+ routerLog.debug(`${agentName} already in ${channel}, skipping auto-rejoin`);
1422
+ continue;
1423
+ }
1424
+ // Add to channel without persisting (already persisted) or notifying
1425
+ const added = this.addChannelMember(channel, agentName, { persist: false });
1426
+ if (added) {
1427
+ rejoinedCount++;
1428
+ }
1429
+ }
1430
+ if (rejoinedCount > 0) {
1431
+ routerLog.info(`Auto-rejoined ${agentName} to ${rejoinedCount} channels`, {
1432
+ channels: Array.from(channelsToJoin),
1433
+ });
1434
+ }
1435
+ }
1436
+ }
1437
+ //# sourceMappingURL=router.js.map