@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/server.js ADDED
@@ -0,0 +1,1001 @@
1
+ /**
2
+ * Agent Relay Daemon Server
3
+ * Main entry point for the relay daemon.
4
+ */
5
+ import net from 'node:net';
6
+ import fs from 'node:fs';
7
+ import path from 'node:path';
8
+ import os from 'node:os';
9
+ import { Connection, DEFAULT_CONFIG } from './connection.js';
10
+ import { Router } from './router.js';
11
+ import { PROTOCOL_VERSION, } from '@agent-relay/protocol/types';
12
+ import { SpawnManager } from './spawn-manager.js';
13
+ import { createStorageAdapter } from '@agent-relay/storage/adapter';
14
+ import { SqliteStorageAdapter } from '@agent-relay/storage/sqlite-adapter';
15
+ import { getProjectPaths } from '@agent-relay/config';
16
+ import { AgentRegistry } from './agent-registry.js';
17
+ import { daemonLog as log } from '@agent-relay/utils/logger';
18
+ import { getCloudSync } from './cloud-sync.js';
19
+ import { generateId } from '@agent-relay/wrapper';
20
+ import { createConsensusIntegration, } from './consensus-integration.js';
21
+ export const DEFAULT_SOCKET_PATH = '/tmp/agent-relay.sock';
22
+ export const DEFAULT_DAEMON_CONFIG = {
23
+ ...DEFAULT_CONFIG,
24
+ socketPath: DEFAULT_SOCKET_PATH,
25
+ pidFilePath: `${DEFAULT_SOCKET_PATH}.pid`,
26
+ };
27
+ export class Daemon {
28
+ server;
29
+ router;
30
+ config;
31
+ running = false;
32
+ connections = new Set();
33
+ pendingAcks = new Map();
34
+ storage;
35
+ storageInitialized = false;
36
+ registry;
37
+ processingStateInterval;
38
+ cloudSync;
39
+ remoteAgents = [];
40
+ remoteUsers = [];
41
+ consensus;
42
+ cloudSyncDebounceTimer;
43
+ spawnManager;
44
+ /** Callback for log output from agents (used by dashboard for streaming) */
45
+ onLogOutput;
46
+ /** Interval for writing processing state file (500ms for responsive UI) */
47
+ static PROCESSING_STATE_INTERVAL_MS = 500;
48
+ static DEFAULT_SYNC_TIMEOUT_MS = 30000;
49
+ constructor(config = {}) {
50
+ this.config = { ...DEFAULT_DAEMON_CONFIG, ...config };
51
+ if (config.socketPath && !config.pidFilePath) {
52
+ this.config.pidFilePath = `${config.socketPath}.pid`;
53
+ }
54
+ // Default teamDir to same directory as socket
55
+ if (!this.config.teamDir) {
56
+ this.config.teamDir = path.dirname(this.config.socketPath);
57
+ }
58
+ if (this.config.teamDir) {
59
+ this.registry = new AgentRegistry(this.config.teamDir);
60
+ }
61
+ // Initialize SpawnManager if enabled
62
+ if (this.config.spawnManager) {
63
+ const spawnConfig = typeof this.config.spawnManager === 'object'
64
+ ? this.config.spawnManager
65
+ : {};
66
+ // Derive projectRoot from teamDir (teamDir is typically {projectRoot}/.agent-relay/)
67
+ const projectRoot = spawnConfig.projectRoot || path.dirname(this.config.teamDir || this.config.socketPath);
68
+ this.spawnManager = new SpawnManager({
69
+ projectRoot,
70
+ socketPath: this.config.socketPath,
71
+ ...spawnConfig,
72
+ });
73
+ }
74
+ // Storage is initialized lazily in start() to support async createStorageAdapter
75
+ this.server = net.createServer(this.handleConnection.bind(this));
76
+ }
77
+ /**
78
+ * Write current agents to agents.json for dashboard consumption.
79
+ */
80
+ writeAgentsFile() {
81
+ if (!this.registry)
82
+ return;
83
+ // The registry persists on every update; this is a no-op helper for symmetry.
84
+ const agents = this.registry.getAgents();
85
+ try {
86
+ const targetPath = path.join(this.config.teamDir ?? path.dirname(this.config.socketPath), 'agents.json');
87
+ const data = JSON.stringify({ agents }, null, 2);
88
+ // Write atomically: write to temp file first, then rename
89
+ // This prevents race conditions where readers see partial/empty data
90
+ const tempPath = `${targetPath}.tmp`;
91
+ fs.writeFileSync(tempPath, data, 'utf-8');
92
+ fs.renameSync(tempPath, targetPath);
93
+ }
94
+ catch (err) {
95
+ log.error('Failed to write agents.json', { error: String(err) });
96
+ }
97
+ }
98
+ /**
99
+ * Write processing state to processing-state.json for dashboard consumption.
100
+ * This file contains agents currently processing/thinking after receiving a message.
101
+ */
102
+ writeProcessingStateFile() {
103
+ try {
104
+ const processingAgents = this.router.getProcessingAgents();
105
+ const targetPath = path.join(this.config.teamDir ?? path.dirname(this.config.socketPath), 'processing-state.json');
106
+ const data = JSON.stringify({ processingAgents, updatedAt: Date.now() }, null, 2);
107
+ const tempPath = `${targetPath}.tmp`;
108
+ fs.writeFileSync(tempPath, data, 'utf-8');
109
+ fs.renameSync(tempPath, targetPath);
110
+ }
111
+ catch (err) {
112
+ log.error('Failed to write processing-state.json', { error: String(err) });
113
+ }
114
+ }
115
+ /**
116
+ * Write currently connected agents to connected-agents.json for CLI consumption.
117
+ * This file contains agents with active socket connections (vs agents.json which is historical).
118
+ */
119
+ writeConnectedAgentsFile() {
120
+ try {
121
+ const connectedAgents = this.router.getAgents();
122
+ const connectedUsers = this.router.getUsers();
123
+ const targetPath = path.join(this.config.teamDir ?? path.dirname(this.config.socketPath), 'connected-agents.json');
124
+ const data = JSON.stringify({
125
+ agents: connectedAgents,
126
+ users: connectedUsers,
127
+ updatedAt: Date.now(),
128
+ }, null, 2);
129
+ const tempPath = `${targetPath}.tmp`;
130
+ fs.writeFileSync(tempPath, data, 'utf-8');
131
+ fs.renameSync(tempPath, targetPath);
132
+ }
133
+ catch (err) {
134
+ log.error('Failed to write connected-agents.json', { error: String(err) });
135
+ }
136
+ }
137
+ /**
138
+ * Mark an agent as spawning (before HELLO completes).
139
+ * Messages sent to this agent will be queued for delivery after registration.
140
+ * Call this before starting the agent's PTY process.
141
+ */
142
+ markSpawning(agentName) {
143
+ this.router.markSpawning(agentName);
144
+ }
145
+ /**
146
+ * Clear the spawning flag for an agent.
147
+ * Called when spawn fails or is cancelled (successful registration clears automatically).
148
+ */
149
+ clearSpawning(agentName) {
150
+ this.router.clearSpawning(agentName);
151
+ }
152
+ /**
153
+ * Initialize storage adapter (called during start).
154
+ */
155
+ async initStorage() {
156
+ if (this.storageInitialized)
157
+ return;
158
+ if (this.config.storageAdapter) {
159
+ // Use explicitly provided adapter
160
+ this.storage = this.config.storageAdapter;
161
+ }
162
+ else {
163
+ // Create adapter based on config/env
164
+ const storagePath = this.config.storagePath ??
165
+ path.join(path.dirname(this.config.socketPath), 'agent-relay.sqlite');
166
+ this.storage = await createStorageAdapter(storagePath, this.config.storageConfig);
167
+ }
168
+ let channelMembershipStore;
169
+ const workspaceId = process.env.RELAY_WORKSPACE_ID
170
+ || process.env.AGENT_RELAY_WORKSPACE_ID
171
+ || process.env.WORKSPACE_ID;
172
+ const databaseUrl = process.env.CLOUD_DATABASE_URL
173
+ || process.env.DATABASE_URL
174
+ || process.env.AGENT_RELAY_STORAGE_URL;
175
+ const isPostgresUrl = databaseUrl?.startsWith('postgres://') || databaseUrl?.startsWith('postgresql://');
176
+ if (workspaceId && isPostgresUrl && databaseUrl) {
177
+ try {
178
+ const { CloudChannelMembershipStore } = await import('./channel-membership-store.js');
179
+ channelMembershipStore = new CloudChannelMembershipStore({ workspaceId, databaseUrl });
180
+ log.info('Channel membership store enabled (cloud DB)', { workspaceId });
181
+ }
182
+ catch (err) {
183
+ log.error('Failed to initialize channel membership store', { error: String(err) });
184
+ }
185
+ }
186
+ else {
187
+ log.debug('Channel membership store disabled (missing workspaceId or Postgres database URL)');
188
+ }
189
+ this.router = new Router({
190
+ storage: this.storage,
191
+ registry: this.registry,
192
+ onProcessingStateChange: () => this.writeProcessingStateFile(),
193
+ crossMachineHandler: {
194
+ sendCrossMachineMessage: this.sendCrossMachineMessage.bind(this),
195
+ isRemoteAgent: this.isRemoteAgent.bind(this),
196
+ isRemoteUser: this.isRemoteUser.bind(this),
197
+ },
198
+ channelMembershipStore,
199
+ });
200
+ // Initialize consensus (enabled by default, can be disabled with consensus: false)
201
+ if (this.config.consensus !== false) {
202
+ const consensusConfig = typeof this.config.consensus === 'object'
203
+ ? this.config.consensus
204
+ : {};
205
+ this.consensus = createConsensusIntegration(this.router, consensusConfig);
206
+ log.info('Consensus mechanism enabled');
207
+ }
208
+ this.storageInitialized = true;
209
+ }
210
+ /**
211
+ * Start the daemon.
212
+ */
213
+ async start() {
214
+ if (this.running)
215
+ return;
216
+ // Initialize storage
217
+ await this.initStorage();
218
+ // Restore channel memberships from persisted storage (cloud DB or SQLite)
219
+ await this.router.restoreChannelMemberships();
220
+ // Initialize cloud sync if configured
221
+ await this.initCloudSync();
222
+ // Clean up stale socket (only if it's actually a socket)
223
+ if (fs.existsSync(this.config.socketPath)) {
224
+ const stat = fs.lstatSync(this.config.socketPath);
225
+ if (!stat.isSocket()) {
226
+ throw new Error(`Refusing to unlink non-socket at ${this.config.socketPath}`);
227
+ }
228
+ fs.unlinkSync(this.config.socketPath);
229
+ }
230
+ // Ensure directory exists
231
+ const socketDir = path.dirname(this.config.socketPath);
232
+ if (!fs.existsSync(socketDir)) {
233
+ fs.mkdirSync(socketDir, { recursive: true });
234
+ }
235
+ // Set up inbox symlink for workspace namespacing
236
+ // Daemon delivers to legacy path (/tmp/relay-inbox), symlink points to workspace path
237
+ // This allows agents to use simple instructions while maintaining workspace isolation
238
+ const workspaceId = process.env.RELAY_WORKSPACE_ID
239
+ || process.env.AGENT_RELAY_WORKSPACE_ID
240
+ || process.env.WORKSPACE_ID;
241
+ const legacyInboxPath = '/tmp/relay-inbox';
242
+ let inboxPath = legacyInboxPath;
243
+ if (workspaceId) {
244
+ // Workspace-namespaced inbox directory
245
+ inboxPath = `/tmp/relay/${workspaceId}/inbox`;
246
+ try {
247
+ // Ensure workspace inbox directory exists
248
+ const inboxDir = path.dirname(inboxPath);
249
+ if (!fs.existsSync(inboxDir)) {
250
+ fs.mkdirSync(inboxDir, { recursive: true });
251
+ }
252
+ if (!fs.existsSync(inboxPath)) {
253
+ fs.mkdirSync(inboxPath, { recursive: true });
254
+ }
255
+ // Ensure legacy inbox parent directory exists
256
+ const legacyInboxParent = path.dirname(legacyInboxPath);
257
+ if (!fs.existsSync(legacyInboxParent)) {
258
+ fs.mkdirSync(legacyInboxParent, { recursive: true });
259
+ }
260
+ // Create symlink from legacy path to workspace path
261
+ // If legacy path exists as a regular directory, remove it first
262
+ if (fs.existsSync(legacyInboxPath)) {
263
+ try {
264
+ const stats = fs.lstatSync(legacyInboxPath);
265
+ if (stats.isSymbolicLink()) {
266
+ // Already a symlink - remove and recreate to ensure correct target
267
+ fs.unlinkSync(legacyInboxPath);
268
+ }
269
+ else if (stats.isDirectory()) {
270
+ // Regular directory - remove it (may have stale files from previous run)
271
+ fs.rmSync(legacyInboxPath, { recursive: true, force: true });
272
+ }
273
+ }
274
+ catch {
275
+ // Ignore errors during cleanup
276
+ }
277
+ }
278
+ // Create the symlink: legacy path -> workspace path
279
+ fs.symlinkSync(inboxPath, legacyInboxPath);
280
+ log.info('Created inbox symlink', { from: legacyInboxPath, to: inboxPath });
281
+ }
282
+ catch (err) {
283
+ log.error('Failed to set up inbox symlink', { error: err.message });
284
+ // Fall back to creating legacy directory directly
285
+ try {
286
+ if (!fs.existsSync(legacyInboxPath)) {
287
+ fs.mkdirSync(legacyInboxPath, { recursive: true });
288
+ }
289
+ }
290
+ catch {
291
+ // Ignore
292
+ }
293
+ }
294
+ }
295
+ else {
296
+ // No workspace ID - just ensure legacy inbox directory exists
297
+ try {
298
+ if (!fs.existsSync(legacyInboxPath)) {
299
+ fs.mkdirSync(legacyInboxPath, { recursive: true });
300
+ }
301
+ }
302
+ catch (err) {
303
+ log.error('Failed to create inbox directory', { error: err.message });
304
+ }
305
+ }
306
+ return new Promise((resolve, reject) => {
307
+ this.server.on('error', reject);
308
+ this.server.listen(this.config.socketPath, () => {
309
+ this.running = true;
310
+ // Set restrictive permissions
311
+ fs.chmodSync(this.config.socketPath, 0o600);
312
+ fs.writeFileSync(this.config.pidFilePath, `${process.pid}\n`, 'utf-8');
313
+ // Start periodic processing state updates for dashboard
314
+ this.processingStateInterval = setInterval(() => {
315
+ this.writeProcessingStateFile();
316
+ }, Daemon.PROCESSING_STATE_INTERVAL_MS);
317
+ log.info('Listening', { socketPath: this.config.socketPath });
318
+ resolve();
319
+ });
320
+ });
321
+ }
322
+ /**
323
+ * Initialize cloud sync service for cross-machine agent communication.
324
+ */
325
+ async initCloudSync() {
326
+ // Check for cloud config file OR environment variables
327
+ const dataDir = process.env.AGENT_RELAY_DATA_DIR ||
328
+ path.join(os.homedir(), '.local', 'share', 'agent-relay');
329
+ const configPath = path.join(dataDir, 'cloud-config.json');
330
+ const hasConfigFile = fs.existsSync(configPath);
331
+ const hasEnvApiKey = !!process.env.AGENT_RELAY_API_KEY;
332
+ // Allow cloud sync if config file exists OR API key is set via env var
333
+ // This enables cloud-hosted workspaces (Fly.io) to sync messages without a config file
334
+ if (!hasConfigFile && !hasEnvApiKey) {
335
+ log.info('Cloud sync disabled (not linked to cloud)');
336
+ return;
337
+ }
338
+ try {
339
+ let apiKey;
340
+ let cloudUrl;
341
+ if (hasConfigFile) {
342
+ // Use config file (local daemons linked via CLI)
343
+ const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
344
+ apiKey = config.apiKey;
345
+ cloudUrl = config.cloudUrl;
346
+ }
347
+ else {
348
+ // Use env vars (cloud-hosted workspaces like Fly.io)
349
+ apiKey = process.env.AGENT_RELAY_API_KEY;
350
+ // CLOUD_API_URL is set by Fly.io provisioner, AGENT_RELAY_CLOUD_URL is the standard
351
+ cloudUrl = process.env.AGENT_RELAY_CLOUD_URL || process.env.CLOUD_API_URL;
352
+ log.info('Using environment variables for cloud sync', { hasApiKey: !!apiKey, hasCloudUrl: !!cloudUrl });
353
+ }
354
+ // Get project root for workspace detection via git remote
355
+ const projectPaths = getProjectPaths();
356
+ this.cloudSync = getCloudSync({
357
+ apiKey,
358
+ cloudUrl: cloudUrl || this.config.cloudUrl,
359
+ enabled: this.config.cloudSync !== false,
360
+ projectDirectory: projectPaths.projectRoot,
361
+ });
362
+ // Listen for remote agent updates
363
+ this.cloudSync.on('remote-agents-updated', (agents) => {
364
+ this.remoteAgents = agents;
365
+ log.info('Remote agents updated', { count: agents.length });
366
+ this.writeRemoteAgentsFile();
367
+ });
368
+ // Listen for remote user updates (humans connected via cloud dashboard)
369
+ this.cloudSync.on('remote-users-updated', (users) => {
370
+ this.remoteUsers = users;
371
+ log.info('Remote users updated', { count: users.length });
372
+ this.writeRemoteUsersFile();
373
+ });
374
+ // Listen for cross-machine messages
375
+ this.cloudSync.on('cross-machine-message', (msg) => {
376
+ this.handleCrossMachineMessage(msg);
377
+ });
378
+ // Listen for cloud commands (e.g., credential refresh)
379
+ this.cloudSync.on('command', (cmd) => {
380
+ log.info('Cloud command received', { type: cmd.type });
381
+ // Handle commands like credential updates, config changes, etc.
382
+ });
383
+ await this.cloudSync.start();
384
+ // Set storage adapter for message sync to cloud
385
+ if (this.storage) {
386
+ this.cloudSync.setStorage(this.storage);
387
+ }
388
+ log.info('Cloud sync enabled');
389
+ }
390
+ catch (err) {
391
+ log.error('Failed to initialize cloud sync', { error: String(err) });
392
+ }
393
+ }
394
+ /**
395
+ * Write remote agents to file for dashboard consumption.
396
+ */
397
+ writeRemoteAgentsFile() {
398
+ try {
399
+ const targetPath = path.join(this.config.teamDir ?? path.dirname(this.config.socketPath), 'remote-agents.json');
400
+ const data = JSON.stringify({
401
+ agents: this.remoteAgents,
402
+ updatedAt: Date.now(),
403
+ }, null, 2);
404
+ const tempPath = `${targetPath}.tmp`;
405
+ fs.writeFileSync(tempPath, data, 'utf-8');
406
+ fs.renameSync(tempPath, targetPath);
407
+ }
408
+ catch (err) {
409
+ log.error('Failed to write remote-agents.json', { error: String(err) });
410
+ }
411
+ }
412
+ /**
413
+ * Write remote users to file for dashboard consumption.
414
+ * Remote users are humans connected via the cloud dashboard.
415
+ */
416
+ writeRemoteUsersFile() {
417
+ try {
418
+ const targetPath = path.join(this.config.teamDir ?? path.dirname(this.config.socketPath), 'remote-users.json');
419
+ const data = JSON.stringify({
420
+ users: this.remoteUsers,
421
+ updatedAt: Date.now(),
422
+ }, null, 2);
423
+ const tempPath = `${targetPath}.tmp`;
424
+ fs.writeFileSync(tempPath, data, 'utf-8');
425
+ fs.renameSync(tempPath, targetPath);
426
+ }
427
+ catch (err) {
428
+ log.error('Failed to write remote-users.json', { error: String(err) });
429
+ }
430
+ }
431
+ /**
432
+ * Handle incoming message from another machine via cloud.
433
+ */
434
+ handleCrossMachineMessage(msg) {
435
+ log.info('Cross-machine message received', {
436
+ from: `${msg.from.daemonName}:${msg.from.agent}`,
437
+ to: msg.to,
438
+ });
439
+ // Find local agent
440
+ const targetConnection = Array.from(this.connections).find(c => c.agentName === msg.to);
441
+ if (!targetConnection) {
442
+ log.warn('Target agent not found locally', { agent: msg.to });
443
+ return;
444
+ }
445
+ // Inject message to local agent
446
+ const envelope = {
447
+ v: 1,
448
+ type: 'SEND',
449
+ id: generateId(),
450
+ ts: Date.now(),
451
+ from: `${msg.from.daemonName}:${msg.from.agent}`,
452
+ to: msg.to,
453
+ payload: {
454
+ kind: 'message',
455
+ body: msg.content,
456
+ data: {
457
+ _crossMachine: true,
458
+ _fromDaemon: msg.from.daemonId,
459
+ _fromDaemonName: msg.from.daemonName,
460
+ ...msg.metadata,
461
+ },
462
+ },
463
+ };
464
+ this.router.route(targetConnection, envelope);
465
+ }
466
+ /**
467
+ * Send message to agent on another machine via cloud.
468
+ */
469
+ async sendCrossMachineMessage(targetDaemonId, targetAgent, fromAgent, content, metadata) {
470
+ if (!this.cloudSync?.isConnected()) {
471
+ log.warn('Cannot send cross-machine message: not connected to cloud');
472
+ return false;
473
+ }
474
+ try {
475
+ await this.cloudSync.sendCrossMachineMessage(targetDaemonId, targetAgent, fromAgent, content, metadata);
476
+ return true;
477
+ }
478
+ catch (err) {
479
+ log.error('Failed to send cross-machine message', { error: String(err) });
480
+ return false;
481
+ }
482
+ }
483
+ /**
484
+ * Get list of remote agents (from other machines).
485
+ */
486
+ getRemoteAgents() {
487
+ return this.remoteAgents;
488
+ }
489
+ /**
490
+ * Check if an agent is on a remote machine.
491
+ */
492
+ isRemoteAgent(agentName) {
493
+ return this.remoteAgents.find(a => a.name === agentName);
494
+ }
495
+ /**
496
+ * Check if a user is on a remote machine (connected via cloud dashboard).
497
+ */
498
+ isRemoteUser(userName) {
499
+ return this.remoteUsers.find(u => u.name === userName);
500
+ }
501
+ /**
502
+ * Notify cloud sync about local agent changes.
503
+ * Debounced to prevent flooding the cloud API with rapid connect/disconnect events.
504
+ */
505
+ notifyCloudSync() {
506
+ if (!this.cloudSync?.isConnected())
507
+ return;
508
+ // Debounce: clear any pending sync and schedule a new one
509
+ if (this.cloudSyncDebounceTimer) {
510
+ clearTimeout(this.cloudSyncDebounceTimer);
511
+ }
512
+ this.cloudSyncDebounceTimer = setTimeout(() => {
513
+ this.cloudSyncDebounceTimer = undefined;
514
+ this.doCloudSync();
515
+ }, 1000); // 1 second debounce
516
+ }
517
+ /**
518
+ * Actually perform the cloud sync (called after debounce).
519
+ */
520
+ doCloudSync() {
521
+ if (!this.cloudSync?.isConnected())
522
+ return;
523
+ // Get AI agents (exclude internal ones like Dashboard)
524
+ const aiAgents = Array.from(this.connections)
525
+ .filter(c => {
526
+ if (!c.agentName)
527
+ return false;
528
+ if (c.entityType === 'user')
529
+ return false;
530
+ if (this.isInternalAgent(c.agentName))
531
+ return false;
532
+ return true;
533
+ })
534
+ .map(c => ({
535
+ name: c.agentName,
536
+ status: 'online',
537
+ isHuman: false,
538
+ }));
539
+ // Get human users (entityType === 'user', exclude Dashboard)
540
+ const humanUsers = Array.from(this.connections)
541
+ .filter(c => {
542
+ if (!c.agentName)
543
+ return false;
544
+ if (c.entityType !== 'user')
545
+ return false;
546
+ if (this.isInternalAgent(c.agentName))
547
+ return false;
548
+ return true;
549
+ })
550
+ .map(c => ({
551
+ name: c.agentName,
552
+ status: 'online',
553
+ isHuman: true,
554
+ avatarUrl: c.avatarUrl,
555
+ }));
556
+ this.cloudSync.updateAgents([...aiAgents, ...humanUsers]);
557
+ }
558
+ /**
559
+ * Check if an agent is internal (should be hidden from cloud sync and listings).
560
+ */
561
+ isInternalAgent(name) {
562
+ if (name.startsWith('__'))
563
+ return true;
564
+ // Dashboard, _DashboardUI, and cli are internal system agents
565
+ return name === 'Dashboard' || name === '_DashboardUI' || name === 'cli';
566
+ }
567
+ /**
568
+ * Stop the daemon.
569
+ */
570
+ async stop() {
571
+ if (!this.running)
572
+ return;
573
+ // Stop cloud sync
574
+ if (this.cloudSync) {
575
+ this.cloudSync.stop();
576
+ this.cloudSync = undefined;
577
+ }
578
+ // Clear cloud sync debounce timer
579
+ if (this.cloudSyncDebounceTimer) {
580
+ clearTimeout(this.cloudSyncDebounceTimer);
581
+ this.cloudSyncDebounceTimer = undefined;
582
+ }
583
+ // Stop processing state updates
584
+ if (this.processingStateInterval) {
585
+ clearInterval(this.processingStateInterval);
586
+ this.processingStateInterval = undefined;
587
+ }
588
+ // Close all active connections
589
+ for (const connection of this.connections) {
590
+ connection.close();
591
+ }
592
+ this.connections.clear();
593
+ return new Promise((resolve) => {
594
+ this.server.close(() => {
595
+ this.running = false;
596
+ // Clean up socket file
597
+ if (fs.existsSync(this.config.socketPath)) {
598
+ fs.unlinkSync(this.config.socketPath);
599
+ }
600
+ // Clean up pid file
601
+ if (fs.existsSync(this.config.pidFilePath)) {
602
+ fs.unlinkSync(this.config.pidFilePath);
603
+ }
604
+ if (this.storage?.close) {
605
+ this.storage.close().catch((err) => {
606
+ log.error('Failed to close storage', { error: String(err) });
607
+ });
608
+ }
609
+ log.info('Stopped');
610
+ resolve();
611
+ });
612
+ });
613
+ }
614
+ /**
615
+ * Handle new connection.
616
+ */
617
+ handleConnection(socket) {
618
+ log.debug('New connection');
619
+ const resumeHandler = this.storage?.getSessionByResumeToken
620
+ ? async ({ agent, resumeToken }) => {
621
+ const session = await this.storage.getSessionByResumeToken(resumeToken);
622
+ if (!session || session.agentName !== agent)
623
+ return null;
624
+ let seedSequences;
625
+ if (this.storage?.getMaxSeqByStream) {
626
+ const streams = await this.storage.getMaxSeqByStream(agent, session.id);
627
+ seedSequences = streams.map(s => ({
628
+ topic: s.topic ?? 'default',
629
+ peer: s.peer,
630
+ seq: s.maxSeq,
631
+ }));
632
+ }
633
+ return {
634
+ sessionId: session.id,
635
+ resumeToken: session.resumeToken ?? resumeToken,
636
+ seedSequences,
637
+ };
638
+ }
639
+ : undefined;
640
+ // Provide processing state callback for heartbeat exemption
641
+ const isProcessing = (agentName) => this.router.isAgentProcessing(agentName);
642
+ const connection = new Connection(socket, { ...this.config, resumeHandler, isProcessing });
643
+ this.connections.add(connection);
644
+ connection.onMessage = (envelope) => {
645
+ this.handleMessage(connection, envelope);
646
+ };
647
+ connection.onAck = (envelope) => {
648
+ this.handleAck(connection, envelope);
649
+ };
650
+ // Update lastSeen on successful heartbeat to keep agent status fresh
651
+ connection.onPong = () => {
652
+ if (connection.agentName) {
653
+ this.registry?.touch(connection.agentName);
654
+ }
655
+ };
656
+ // Register agent when connection becomes active (after successful handshake)
657
+ connection.onActive = () => {
658
+ if (connection.agentName) {
659
+ this.router.register(connection);
660
+ log.info('Agent registered', { agent: connection.agentName });
661
+ // Registry handles persistence internally via save()
662
+ this.registry?.registerOrUpdate({
663
+ name: connection.agentName,
664
+ cli: connection.cli,
665
+ program: connection.program,
666
+ model: connection.model,
667
+ task: connection.task,
668
+ workingDirectory: connection.workingDirectory,
669
+ });
670
+ // Auto-join all agents to #general channel
671
+ this.router.autoJoinChannel(connection.agentName, '#general');
672
+ // Record session start
673
+ if (this.storage instanceof SqliteStorageAdapter) {
674
+ const projectPaths = getProjectPaths();
675
+ const storage = this.storage;
676
+ const persistSession = async () => {
677
+ let startedAt = Date.now();
678
+ if (connection.isResumed && storage.getSessionByResumeToken) {
679
+ const existing = await storage.getSessionByResumeToken(connection.resumeToken);
680
+ if (existing?.startedAt) {
681
+ startedAt = existing.startedAt;
682
+ }
683
+ }
684
+ await storage.startSession({
685
+ id: connection.sessionId,
686
+ agentName: connection.agentName,
687
+ cli: connection.cli,
688
+ projectId: projectPaths.projectId,
689
+ projectRoot: projectPaths.projectRoot,
690
+ startedAt,
691
+ resumeToken: connection.resumeToken,
692
+ });
693
+ };
694
+ persistSession().catch(err => log.error('Failed to record session start', { error: String(err) }));
695
+ }
696
+ }
697
+ // Replay pending deliveries for resumed sessions (unacked messages from previous session)
698
+ if (connection.isResumed) {
699
+ this.router.replayPending(connection).catch(err => {
700
+ log.error('Failed to replay pending messages', { error: String(err) });
701
+ });
702
+ }
703
+ // Deliver any messages that were sent while this agent was offline
704
+ // This handles messages sent during spawn timing gaps or brief disconnections
705
+ this.router.deliverPendingMessages(connection).catch(err => {
706
+ log.error('Failed to deliver pending messages', { error: String(err) });
707
+ });
708
+ // Auto-rejoin channels that the agent was a member of before daemon restart
709
+ // This restores channel memberships from persisted storage (cloud DB or SQLite)
710
+ if (connection.agentName) {
711
+ this.router.autoRejoinChannelsForAgent(connection.agentName).catch(err => {
712
+ log.error('Failed to auto-rejoin channels', { error: String(err) });
713
+ });
714
+ }
715
+ // Notify cloud sync about agent changes
716
+ this.notifyCloudSync();
717
+ // Update connected agents file for CLI
718
+ this.writeConnectedAgentsFile();
719
+ };
720
+ connection.onClose = () => {
721
+ log.debug('Connection closed', { agent: connection.agentName ?? connection.id });
722
+ this.connections.delete(connection);
723
+ this.clearPendingAcksForConnection(connection.id);
724
+ this.router.unregister(connection);
725
+ // Registry handles persistence internally via touch() -> save()
726
+ if (connection.agentName) {
727
+ this.registry?.touch(connection.agentName);
728
+ }
729
+ // Record session end (disconnect - agent may still mark it closed explicitly)
730
+ if (this.storage instanceof SqliteStorageAdapter) {
731
+ this.storage.endSession(connection.sessionId, { closedBy: 'disconnect' })
732
+ .catch(err => log.error('Failed to record session end', { error: String(err) }));
733
+ }
734
+ // Notify cloud sync about agent changes
735
+ this.notifyCloudSync();
736
+ // Update connected agents file for CLI
737
+ this.writeConnectedAgentsFile();
738
+ };
739
+ connection.onError = (error) => {
740
+ log.error('Connection error', { error: error.message });
741
+ this.connections.delete(connection);
742
+ this.clearPendingAcksForConnection(connection.id);
743
+ this.router.unregister(connection);
744
+ // Registry handles persistence internally via touch() -> save()
745
+ if (connection.agentName) {
746
+ this.registry?.touch(connection.agentName);
747
+ }
748
+ // Record session end on error
749
+ if (this.storage instanceof SqliteStorageAdapter) {
750
+ this.storage.endSession(connection.sessionId, { closedBy: 'error' })
751
+ .catch(err => log.error('Failed to record session end', { error: String(err) }));
752
+ }
753
+ // Update connected agents file for CLI
754
+ this.writeConnectedAgentsFile();
755
+ };
756
+ }
757
+ /**
758
+ * Handle incoming message from a connection.
759
+ */
760
+ handleMessage(connection, envelope) {
761
+ switch (envelope.type) {
762
+ case 'SEND': {
763
+ const sendEnvelope = envelope;
764
+ const membershipUpdate = sendEnvelope.payload.data?._channelMembershipUpdate;
765
+ if (membershipUpdate && sendEnvelope.to === '_router') {
766
+ this.router.handleMembershipUpdate({
767
+ channel: membershipUpdate.channel ?? '',
768
+ member: membershipUpdate.member ?? '',
769
+ action: membershipUpdate.action ?? 'join',
770
+ });
771
+ return;
772
+ }
773
+ // Check for consensus commands (messages to _consensus)
774
+ if (this.consensus?.enabled && sendEnvelope.to === '_consensus') {
775
+ const from = connection.agentName ?? 'unknown';
776
+ const result = this.consensus.processIncomingMessage(from, sendEnvelope.payload.body);
777
+ if (result.isConsensusCommand) {
778
+ log.info(`Consensus ${result.type} from ${from}`, {
779
+ success: result.result?.success,
780
+ proposalId: result.result?.proposal?.id,
781
+ });
782
+ // Don't route consensus commands to the router
783
+ return;
784
+ }
785
+ }
786
+ const syncMeta = sendEnvelope.payload_meta?.sync;
787
+ if (syncMeta?.blocking) {
788
+ if (!syncMeta.correlationId) {
789
+ this.sendErrorEnvelope(connection, 'Missing sync correlationId for blocking SEND');
790
+ return;
791
+ }
792
+ const registered = this.registerPendingAck(connection, syncMeta.correlationId, syncMeta.timeoutMs);
793
+ if (!registered) {
794
+ return;
795
+ }
796
+ }
797
+ this.router.route(connection, sendEnvelope);
798
+ break;
799
+ }
800
+ case 'SUBSCRIBE':
801
+ if (connection.agentName && envelope.topic) {
802
+ this.router.subscribe(connection.agentName, envelope.topic);
803
+ }
804
+ break;
805
+ case 'UNSUBSCRIBE':
806
+ if (connection.agentName && envelope.topic) {
807
+ this.router.unsubscribe(connection.agentName, envelope.topic);
808
+ }
809
+ break;
810
+ case 'SHADOW_BIND':
811
+ if (connection.agentName) {
812
+ const payload = envelope.payload;
813
+ this.router.bindShadow(connection.agentName, payload.primaryAgent, {
814
+ speakOn: payload.speakOn,
815
+ receiveIncoming: payload.receiveIncoming,
816
+ receiveOutgoing: payload.receiveOutgoing,
817
+ });
818
+ }
819
+ break;
820
+ case 'SHADOW_UNBIND':
821
+ if (connection.agentName) {
822
+ const payload = envelope.payload;
823
+ // Verify the shadow is actually bound to the specified primary
824
+ const currentPrimary = this.router.getPrimaryForShadow(connection.agentName);
825
+ if (currentPrimary === payload.primaryAgent) {
826
+ this.router.unbindShadow(connection.agentName);
827
+ }
828
+ }
829
+ break;
830
+ case 'LOG':
831
+ // Handle log output from daemon-connected agents
832
+ if (connection.agentName) {
833
+ const payload = envelope.payload;
834
+ const timestamp = payload.timestamp ?? envelope.ts;
835
+ // Forward to dashboard via callback
836
+ if (this.onLogOutput) {
837
+ this.onLogOutput(connection.agentName, payload.data, timestamp);
838
+ }
839
+ }
840
+ break;
841
+ // Channel messaging handlers
842
+ case 'CHANNEL_JOIN': {
843
+ const channelEnvelope = envelope;
844
+ log.info(`Channel join: ${connection.agentName} -> ${channelEnvelope.payload.channel}`);
845
+ this.router.handleChannelJoin(connection, channelEnvelope);
846
+ break;
847
+ }
848
+ case 'CHANNEL_LEAVE': {
849
+ const channelEnvelope = envelope;
850
+ log.info(`Channel leave: ${connection.agentName} <- ${channelEnvelope.payload.channel}`);
851
+ this.router.handleChannelLeave(connection, channelEnvelope);
852
+ break;
853
+ }
854
+ case 'CHANNEL_MESSAGE': {
855
+ const channelEnvelope = envelope;
856
+ log.info(`CHANNEL_MESSAGE received: from=${connection.agentName} channel=${channelEnvelope.payload.channel}`);
857
+ this.router.routeChannelMessage(connection, channelEnvelope);
858
+ break;
859
+ }
860
+ // Spawn/release handlers (protocol-based agent spawning)
861
+ case 'SPAWN': {
862
+ if (!this.spawnManager) {
863
+ this.sendErrorEnvelope(connection, 'SpawnManager not enabled. Configure spawnManager: true in daemon config.');
864
+ break;
865
+ }
866
+ const spawnEnvelope = envelope;
867
+ log.info(`SPAWN request: from=${connection.agentName} agent=${spawnEnvelope.payload.name} cli=${spawnEnvelope.payload.cli}`);
868
+ this.spawnManager.handleSpawn(connection, spawnEnvelope);
869
+ break;
870
+ }
871
+ case 'RELEASE': {
872
+ if (!this.spawnManager) {
873
+ this.sendErrorEnvelope(connection, 'SpawnManager not enabled. Configure spawnManager: true in daemon config.');
874
+ break;
875
+ }
876
+ const releaseEnvelope = envelope;
877
+ log.info(`RELEASE request: from=${connection.agentName} agent=${releaseEnvelope.payload.name}`);
878
+ this.spawnManager.handleRelease(connection, releaseEnvelope);
879
+ break;
880
+ }
881
+ }
882
+ }
883
+ handleAck(connection, envelope) {
884
+ this.router.handleAck(connection, envelope);
885
+ const correlationId = envelope.payload.correlationId;
886
+ if (!correlationId)
887
+ return;
888
+ const pending = this.pendingAcks.get(correlationId);
889
+ if (!pending)
890
+ return;
891
+ clearTimeout(pending.timeoutHandle);
892
+ this.pendingAcks.delete(correlationId);
893
+ const forwardAck = {
894
+ v: envelope.v,
895
+ type: 'ACK',
896
+ id: generateId(),
897
+ ts: Date.now(),
898
+ from: connection.agentName,
899
+ to: pending.connection.agentName,
900
+ payload: envelope.payload,
901
+ };
902
+ pending.connection.send(forwardAck);
903
+ }
904
+ registerPendingAck(connection, correlationId, timeoutMs) {
905
+ if (this.pendingAcks.has(correlationId)) {
906
+ this.sendErrorEnvelope(connection, `Duplicate correlationId: ${correlationId}`);
907
+ return false;
908
+ }
909
+ const timeout = timeoutMs ?? Daemon.DEFAULT_SYNC_TIMEOUT_MS;
910
+ const timeoutHandle = setTimeout(() => {
911
+ this.pendingAcks.delete(correlationId);
912
+ this.sendErrorEnvelope(connection, `ACK timeout after ${timeout}ms`);
913
+ }, timeout);
914
+ this.pendingAcks.set(correlationId, {
915
+ correlationId,
916
+ connectionId: connection.id,
917
+ connection,
918
+ timeoutHandle,
919
+ });
920
+ return true;
921
+ }
922
+ clearPendingAcksForConnection(connectionId) {
923
+ for (const [correlationId, pending] of this.pendingAcks.entries()) {
924
+ if (pending.connectionId !== connectionId)
925
+ continue;
926
+ clearTimeout(pending.timeoutHandle);
927
+ this.pendingAcks.delete(correlationId);
928
+ }
929
+ }
930
+ sendErrorEnvelope(connection, message) {
931
+ const errorEnvelope = {
932
+ v: PROTOCOL_VERSION,
933
+ type: 'ERROR',
934
+ id: generateId(),
935
+ ts: Date.now(),
936
+ payload: {
937
+ code: 'INTERNAL',
938
+ message,
939
+ fatal: false,
940
+ },
941
+ };
942
+ connection.send(errorEnvelope);
943
+ }
944
+ /**
945
+ * Get list of connected agents.
946
+ */
947
+ getAgents() {
948
+ return this.router.getAgents();
949
+ }
950
+ /**
951
+ * Broadcast a system message to all connected agents.
952
+ * Used for system notifications like agent death announcements.
953
+ */
954
+ broadcastSystemMessage(message, data) {
955
+ this.router.broadcastSystemMessage(message, data);
956
+ }
957
+ /**
958
+ * Get connection count.
959
+ */
960
+ get connectionCount() {
961
+ return this.router.connectionCount;
962
+ }
963
+ /**
964
+ * Check if daemon is running.
965
+ */
966
+ get isRunning() {
967
+ return this.running;
968
+ }
969
+ /**
970
+ * Check if consensus is enabled.
971
+ */
972
+ get consensusEnabled() {
973
+ return this.consensus?.enabled ?? false;
974
+ }
975
+ /**
976
+ * Get the consensus integration (for API access).
977
+ */
978
+ getConsensus() {
979
+ return this.consensus;
980
+ }
981
+ }
982
+ // Run as standalone if executed directly
983
+ const isMainModule = import.meta.url === `file://${process.argv[1]}`;
984
+ if (isMainModule) {
985
+ const daemon = new Daemon();
986
+ process.on('SIGINT', async () => {
987
+ log.info('Shutting down (SIGINT)');
988
+ await daemon.stop();
989
+ process.exit(0);
990
+ });
991
+ process.on('SIGTERM', async () => {
992
+ log.info('Shutting down (SIGTERM)');
993
+ await daemon.stop();
994
+ process.exit(0);
995
+ });
996
+ daemon.start().catch((err) => {
997
+ log.error('Failed to start', { error: String(err) });
998
+ process.exit(1);
999
+ });
1000
+ }
1001
+ //# sourceMappingURL=server.js.map