@agentmeshhq/agent 0.2.0 → 0.3.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 (147) hide show
  1. package/README.md +39 -0
  2. package/dist/__tests__/context-template.test.d.ts +4 -0
  3. package/dist/__tests__/context-template.test.js +233 -0
  4. package/dist/__tests__/context-template.test.js.map +1 -0
  5. package/dist/__tests__/loader.test.js +140 -28
  6. package/dist/__tests__/loader.test.js.map +1 -1
  7. package/dist/__tests__/no-respawn.test.d.ts +1 -0
  8. package/dist/__tests__/no-respawn.test.js +254 -0
  9. package/dist/__tests__/no-respawn.test.js.map +1 -0
  10. package/dist/__tests__/onboard.test.d.ts +5 -0
  11. package/dist/__tests__/onboard.test.js +341 -0
  12. package/dist/__tests__/onboard.test.js.map +1 -0
  13. package/dist/__tests__/orphan-process.test.d.ts +11 -0
  14. package/dist/__tests__/orphan-process.test.js +286 -0
  15. package/dist/__tests__/orphan-process.test.js.map +1 -0
  16. package/dist/__tests__/runner.test.js +16 -0
  17. package/dist/__tests__/runner.test.js.map +1 -1
  18. package/dist/__tests__/shared-resource-guards.test.d.ts +7 -0
  19. package/dist/__tests__/shared-resource-guards.test.js +260 -0
  20. package/dist/__tests__/shared-resource-guards.test.js.map +1 -0
  21. package/dist/__tests__/watchdog.test.js +138 -12
  22. package/dist/__tests__/watchdog.test.js.map +1 -1
  23. package/dist/cli/status.js +11 -0
  24. package/dist/cli/status.js.map +1 -1
  25. package/dist/cli/stop.js +7 -2
  26. package/dist/cli/stop.js.map +1 -1
  27. package/dist/config/loader.d.ts +0 -4
  28. package/dist/config/loader.js +102 -42
  29. package/dist/config/loader.js.map +1 -1
  30. package/dist/config/schema.d.ts +6 -4
  31. package/dist/core/daemon/assignment-message.d.ts +12 -0
  32. package/dist/core/daemon/assignment-message.js +36 -0
  33. package/dist/core/daemon/assignment-message.js.map +1 -0
  34. package/dist/core/daemon/bootstrap.d.ts +35 -0
  35. package/dist/core/daemon/bootstrap.js +52 -0
  36. package/dist/core/daemon/bootstrap.js.map +1 -0
  37. package/dist/core/daemon/context-template.d.ts +11 -0
  38. package/dist/core/daemon/context-template.js +144 -0
  39. package/dist/core/daemon/context-template.js.map +1 -0
  40. package/dist/core/daemon/crash-log.d.ts +14 -0
  41. package/dist/core/daemon/crash-log.js +23 -0
  42. package/dist/core/daemon/crash-log.js.map +1 -0
  43. package/dist/core/daemon/git-auth.d.ts +18 -0
  44. package/dist/core/daemon/git-auth.js +88 -0
  45. package/dist/core/daemon/git-auth.js.map +1 -0
  46. package/dist/core/daemon/health-policy.d.ts +17 -0
  47. package/dist/core/daemon/health-policy.js +24 -0
  48. package/dist/core/daemon/health-policy.js.map +1 -0
  49. package/dist/core/daemon/sandbox-config.d.ts +9 -0
  50. package/dist/core/daemon/sandbox-config.js +17 -0
  51. package/dist/core/daemon/sandbox-config.js.map +1 -0
  52. package/dist/core/daemon/state.d.ts +33 -0
  53. package/dist/core/daemon/state.js +78 -0
  54. package/dist/core/daemon/state.js.map +1 -0
  55. package/dist/core/daemon/tmux-session.d.ts +17 -0
  56. package/dist/core/daemon/tmux-session.js +34 -0
  57. package/dist/core/daemon/tmux-session.js.map +1 -0
  58. package/dist/core/daemon/workspace.d.ts +23 -0
  59. package/dist/core/daemon/workspace.js +90 -0
  60. package/dist/core/daemon/workspace.js.map +1 -0
  61. package/dist/core/daemon.d.ts +9 -12
  62. package/dist/core/daemon.js +293 -393
  63. package/dist/core/daemon.js.map +1 -1
  64. package/dist/core/injector.d.ts +5 -1
  65. package/dist/core/injector.js +83 -0
  66. package/dist/core/injector.js.map +1 -1
  67. package/dist/core/registry.d.ts +62 -0
  68. package/dist/core/registry.js +18 -0
  69. package/dist/core/registry.js.map +1 -1
  70. package/dist/core/runner/build.d.ts +9 -0
  71. package/dist/core/runner/build.js +53 -0
  72. package/dist/core/runner/build.js.map +1 -0
  73. package/dist/core/runner/detect.d.ts +5 -0
  74. package/dist/core/runner/detect.js +14 -0
  75. package/dist/core/runner/detect.js.map +1 -0
  76. package/dist/core/runner/index.d.ts +5 -0
  77. package/dist/core/runner/index.js +5 -0
  78. package/dist/core/runner/index.js.map +1 -0
  79. package/dist/core/runner/model.d.ts +5 -0
  80. package/dist/core/runner/model.js +7 -0
  81. package/dist/core/runner/model.js.map +1 -0
  82. package/dist/core/runner/opencode-models.d.ts +15 -0
  83. package/dist/core/runner/opencode-models.js +70 -0
  84. package/dist/core/runner/opencode-models.js.map +1 -0
  85. package/dist/core/runner/types.d.ts +19 -0
  86. package/dist/core/runner/types.js +8 -0
  87. package/dist/core/runner/types.js.map +1 -0
  88. package/dist/core/runner.d.ts +5 -47
  89. package/dist/core/runner.js +5 -167
  90. package/dist/core/runner.js.map +1 -1
  91. package/dist/core/tmux-runtime.d.ts +13 -0
  92. package/dist/core/tmux-runtime.js +72 -0
  93. package/dist/core/tmux-runtime.js.map +1 -0
  94. package/dist/core/tmux.d.ts +7 -1
  95. package/dist/core/tmux.js +75 -45
  96. package/dist/core/tmux.js.map +1 -1
  97. package/dist/core/watchdog.d.ts +18 -1
  98. package/dist/core/watchdog.js +78 -29
  99. package/dist/core/watchdog.js.map +1 -1
  100. package/package.json +24 -4
  101. package/src/__tests__/context.test.ts +0 -464
  102. package/src/__tests__/injector.test.ts +0 -29
  103. package/src/__tests__/jwt.test.ts +0 -112
  104. package/src/__tests__/loader.test.ts +0 -239
  105. package/src/__tests__/runner.test.ts +0 -104
  106. package/src/__tests__/sandbox.test.ts +0 -435
  107. package/src/__tests__/watchdog.test.ts +0 -368
  108. package/src/cli/attach.ts +0 -22
  109. package/src/cli/build.ts +0 -145
  110. package/src/cli/config.ts +0 -148
  111. package/src/cli/context.ts +0 -231
  112. package/src/cli/deploy.ts +0 -155
  113. package/src/cli/index.ts +0 -376
  114. package/src/cli/init.ts +0 -75
  115. package/src/cli/list.ts +0 -70
  116. package/src/cli/local.ts +0 -183
  117. package/src/cli/logs.ts +0 -64
  118. package/src/cli/migrate.ts +0 -212
  119. package/src/cli/nudge.ts +0 -81
  120. package/src/cli/restart.ts +0 -59
  121. package/src/cli/slack.ts +0 -70
  122. package/src/cli/start.ts +0 -118
  123. package/src/cli/status.ts +0 -91
  124. package/src/cli/stop.ts +0 -48
  125. package/src/cli/test.ts +0 -143
  126. package/src/cli/token.ts +0 -188
  127. package/src/cli/whoami.ts +0 -142
  128. package/src/config/loader.ts +0 -121
  129. package/src/config/schema.ts +0 -68
  130. package/src/context/handoff.ts +0 -122
  131. package/src/context/index.ts +0 -8
  132. package/src/context/schema.ts +0 -111
  133. package/src/context/storage.ts +0 -197
  134. package/src/core/daemon.ts +0 -1317
  135. package/src/core/heartbeat.ts +0 -129
  136. package/src/core/injector.ts +0 -292
  137. package/src/core/registry.ts +0 -159
  138. package/src/core/runner.ts +0 -225
  139. package/src/core/sandbox.ts +0 -547
  140. package/src/core/session-id.ts +0 -111
  141. package/src/core/tmux.ts +0 -405
  142. package/src/core/watchdog.ts +0 -238
  143. package/src/core/websocket.ts +0 -94
  144. package/src/index.ts +0 -10
  145. package/src/utils/jwt.ts +0 -87
  146. package/tsconfig.json +0 -8
  147. package/vitest.config.ts +0 -12
@@ -1,1317 +0,0 @@
1
- import { type ChildProcess, execSync, spawn } from "node:child_process";
2
- import fs from "node:fs";
3
- import os from "node:os";
4
- import path from "node:path";
5
- import {
6
- addAgentToState,
7
- getAgentState,
8
- loadConfig,
9
- resetAgentRestartCount,
10
- updateAgentInState,
11
- } from "../config/loader.js";
12
- import type { AgentConfig, AgentStatus, Config } from "../config/schema.js";
13
- import { loadContext, loadOrCreateContext, saveContext } from "../context/index.js";
14
- import { Heartbeat } from "./heartbeat.js";
15
- import { handleWebSocketEvent, injectRestoredContext, injectStartupMessage } from "./injector.js";
16
- import { checkInbox, fetchAssignments, registerAgent, type ServerContext } from "./registry.js";
17
- import { buildRunnerConfig, getRunnerDisplayName, type RunnerConfig } from "./runner.js";
18
- import { DockerSandbox } from "./sandbox.js";
19
- import { getLatestSessionId, snapshotSessionId, waitForNewSessionId } from "./session-id.js";
20
- import {
21
- captureSessionContext,
22
- captureSessionOutput,
23
- createSession,
24
- destroySession,
25
- getSessionName,
26
- isSessionHealthy,
27
- sessionExists,
28
- updateSessionEnvironment,
29
- } from "./tmux.js";
30
- import {
31
- checkAgentProgress,
32
- cleanupOrphanContainers,
33
- isProcessRunning,
34
- sendNudge,
35
- } from "./watchdog.js";
36
- import { AgentWebSocket } from "./websocket.js";
37
-
38
- // Maximum number of auto-restart attempts
39
- const MAX_RESTART_ATTEMPTS = 3;
40
- // Time after which restart count resets (30 minutes of stable operation)
41
- const RESTART_COUNT_RESET_MS = 30 * 60 * 1000;
42
- // Time to wait after nudging before restarting (2 minutes)
43
- const NUDGE_WAIT_MS = 2 * 60 * 1000;
44
-
45
- // Path to the sandbox OpenCode config (permissive permissions)
46
- const SANDBOX_OPENCODE_CONFIG_PATH = path.join(os.homedir(), ".agentmesh", "opencode-sandbox.json");
47
-
48
- // Sandbox OpenCode config content - allow everything since container is sandboxed
49
- const SANDBOX_OPENCODE_CONFIG = {
50
- $schema: "https://opencode.ai/config.json",
51
- permission: "allow",
52
- };
53
-
54
- export interface DaemonOptions {
55
- name: string;
56
- command?: string;
57
- workdir?: string;
58
- model?: string;
59
- daemonize?: boolean;
60
- /** Whether to restore context from previous session (default: false) */
61
- restoreContext?: boolean;
62
- /** Only send nudges/restart for worker agents (default: false) */
63
- worker?: boolean;
64
- /** Auto-clone repository for project assignments */
65
- autoSetup?: boolean;
66
- /** Run opencode serve instead of tmux TUI (for Integration Service) */
67
- serve?: boolean;
68
- /** Port for opencode serve (default: 3001) */
69
- servePort?: number;
70
- /** Run agent in Docker sandbox container */
71
- sandbox?: boolean;
72
- /** Docker image for sandbox (default: agentmesh/agent-sandbox:latest) */
73
- sandboxImage?: string;
74
- /** CPU limit for sandbox (default: 1) */
75
- sandboxCpu?: string;
76
- /** Memory limit for sandbox (default: 2g) */
77
- sandboxMemory?: string;
78
- }
79
-
80
- export class AgentDaemon {
81
- private agentName: string;
82
- private config: Config;
83
- private agentConfig: AgentConfig;
84
- private runnerConfig: RunnerConfig;
85
- private ws: AgentWebSocket | null = null;
86
- private heartbeat: Heartbeat | null = null;
87
- private token: string | null = null;
88
- private agentId: string | null = null;
89
- private isRunning = false;
90
- private assignedProject: string | undefined;
91
- private shouldRestoreContext: boolean;
92
- private isWorkerAgent: boolean;
93
- private autoSetup: boolean;
94
- private serveMode: boolean;
95
- private servePort: number;
96
- private serveProcess: ChildProcess | null = null;
97
- private sandboxMode: boolean;
98
- private sandboxImage: string;
99
- private sandboxCpu: string;
100
- private sandboxMemory: string;
101
- private sandbox: DockerSandbox | null = null;
102
- private healthCheckInterval: ReturnType<typeof setInterval> | null = null;
103
- private serverContext: ServerContext | undefined;
104
- // Session resume tracking
105
- private _preStartSessionId: string | null | undefined;
106
- private _attemptedResumeSessionId: string | undefined;
107
- // Auto-restart tracking
108
- private restartCount = 0;
109
- private lastStableTime: Date | null = null;
110
- private stuckSince: Date | null = null;
111
- private nudgeSentAt: Date | null = null;
112
-
113
- constructor(options: DaemonOptions) {
114
- const config = loadConfig();
115
- if (!config) {
116
- throw new Error("No config found. Run 'agentmesh init' first.");
117
- }
118
-
119
- // Ensure config has required fields with defaults
120
- if (!config.agents) config.agents = [];
121
- if (!config.defaults) config.defaults = { command: "opencode", model: "claude-sonnet-4-5-20250929" };
122
-
123
- this.config = config;
124
- this.agentName = options.name;
125
- this.shouldRestoreContext = options.restoreContext === true;
126
- this.isWorkerAgent = options.worker === true;
127
- this.autoSetup = options.autoSetup === true;
128
-
129
- // Find or create agent config
130
- let agentConfig = config.agents.find((a) => a.name === options.name);
131
-
132
- if (!agentConfig) {
133
- agentConfig = {
134
- name: options.name,
135
- command: options.command || config.defaults.command,
136
- workdir: options.workdir,
137
- model: options.model || config.defaults.model,
138
- };
139
- }
140
-
141
- // Override with provided options
142
- if (options.command) agentConfig.command = options.command;
143
- if (options.workdir) agentConfig.workdir = options.workdir;
144
- if (options.model) agentConfig.model = options.model;
145
-
146
- this.agentConfig = agentConfig;
147
- this.serveMode = options.serve === true;
148
- this.servePort = options.servePort || 3001;
149
- this.sandboxMode = options.sandbox === true;
150
- this.sandboxImage = options.sandboxImage || "agentmesh/agent-sandbox:latest";
151
- this.sandboxCpu = options.sandboxCpu || "1";
152
- this.sandboxMemory = options.sandboxMemory || "2g";
153
-
154
- // Build runner configuration with model resolution
155
- this.runnerConfig = buildRunnerConfig({
156
- cliModel: options.model,
157
- agentModel: agentConfig.model,
158
- defaultModel: config.defaults.model,
159
- command: agentConfig.command,
160
- });
161
-
162
- const runnerName = getRunnerDisplayName(this.runnerConfig.type);
163
- console.log(`Runner: ${runnerName}`);
164
- console.log(`Effective model: ${this.runnerConfig.model}`);
165
- }
166
-
167
- async start(): Promise<void> {
168
- if (this.isRunning) {
169
- console.error("Daemon already running");
170
- return;
171
- }
172
-
173
- console.log(`Starting agent: ${this.agentName}`);
174
-
175
- // Check for duplicate process
176
- const existingState = getAgentState(this.agentName);
177
- if (existingState && existingState.pid > 0) {
178
- if (isProcessRunning(existingState.pid)) {
179
- throw new Error(
180
- `Agent "${this.agentName}" is already running (PID: ${existingState.pid}). ` +
181
- `Use 'agentmesh stop ${this.agentName}' first.`,
182
- );
183
- }
184
- // Process not running, clean up stale state
185
- console.log(`Cleaning up stale state for PID ${existingState.pid}`);
186
- }
187
-
188
- // Clean up orphan containers in sandbox mode
189
- if (this.sandboxMode) {
190
- const cleaned = cleanupOrphanContainers(this.agentName);
191
- if (cleaned > 0) {
192
- console.log(`Cleaned up ${cleaned} orphan container(s)`);
193
- }
194
- }
195
-
196
- // Reset restart count on manual start
197
- this.restartCount = 0;
198
- this.lastStableTime = new Date();
199
-
200
- // Register with hub first (needed for assignment check)
201
- console.log("Registering with AgentMesh hub...");
202
- console.log(`Existing state: ${existingState ? `agentId=${existingState.agentId}` : "none"}`);
203
-
204
- const registration = await registerAgent({
205
- url: this.config.hubUrl,
206
- apiKey: this.config.apiKey,
207
- workspace: this.config.workspace,
208
- agentId: existingState?.agentId || this.agentConfig.agentId,
209
- agentName: this.agentName,
210
- model: this.agentConfig.model || this.config.defaults.model,
211
- restoreContext: this.shouldRestoreContext,
212
- });
213
-
214
- this.agentId = registration.agentId;
215
- this.token = registration.token;
216
-
217
- if (registration.status === "re-registered") {
218
- console.log(`Re-registered as: ${this.agentId}`);
219
- if (registration.context && Object.keys(registration.context).length > 0) {
220
- this.serverContext = registration.context;
221
- console.log(`Server context restored: ${Object.keys(registration.context).join(", ")}`);
222
- }
223
- } else {
224
- console.log(`Registered as: ${this.agentId}`);
225
- }
226
-
227
- // Check assignments and auto-setup workdir if needed (before creating tmux session)
228
- await this.checkAssignments();
229
-
230
- // Choose runtime mode: sandbox > serve > tmux
231
- if (this.sandboxMode) {
232
- await this.startSandboxMode();
233
- } else if (this.serveMode) {
234
- await this.startServeMode();
235
- } else {
236
- // Check if session already exists
237
- const sessionName = getSessionName(this.agentName);
238
- const sessionAlreadyExists = sessionExists(sessionName);
239
-
240
- // Create tmux session if it doesn't exist
241
- if (!sessionAlreadyExists) {
242
- // Load saved context to check for OpenCode session ID (for native resume)
243
- let savedSessionId: string | undefined;
244
- if (this.shouldRestoreContext && this.agentId) {
245
- const savedContext = loadContext(this.agentId);
246
- savedSessionId = savedContext?.custom?.opencodeSessionId as string | undefined;
247
- if (savedSessionId) {
248
- console.log(`[CONTEXT] Found saved OpenCode session ID: ${savedSessionId}`);
249
- }
250
- }
251
-
252
- // Snapshot the latest session ID in logs BEFORE starting OpenCode.
253
- // This lets us detect whether OpenCode actually resumed vs created a new session.
254
- const preStartSessionId = snapshotSessionId(this.agentName);
255
-
256
- console.log(`Creating tmux session: ${sessionName}`);
257
-
258
- // Include runner env vars (e.g., OPENCODE_MODEL) at session creation
259
- const created = createSession(
260
- this.agentName,
261
- this.agentConfig.command,
262
- this.agentConfig.workdir,
263
- this.runnerConfig.env, // Apply model env at process start
264
- savedSessionId, // Resume OpenCode session if available
265
- );
266
-
267
- if (!created) {
268
- throw new Error("Failed to create tmux session");
269
- }
270
-
271
- // Store pre-start snapshot for fallback detection later
272
- this._preStartSessionId = preStartSessionId;
273
- this._attemptedResumeSessionId = savedSessionId;
274
- } else {
275
- console.log(`Reconnecting to existing session: ${sessionName}`);
276
- // Update environment for existing session
277
- updateSessionEnvironment(this.agentName, this.runnerConfig.env);
278
- }
279
-
280
- // Inject environment variables into tmux session
281
- console.log("Injecting environment variables...");
282
- updateSessionEnvironment(this.agentName, {
283
- AGENT_TOKEN: this.token,
284
- AGENTMESH_AGENT_ID: this.agentId,
285
- });
286
- }
287
-
288
- // Save state including runtime model info
289
- const sessionName = this.serveMode ? `serve:${this.servePort}` : getSessionName(this.agentName);
290
- addAgentToState({
291
- name: this.agentName,
292
- agentId: this.agentId,
293
- pid: process.pid,
294
- tmuxSession: sessionName,
295
- startedAt: new Date().toISOString(),
296
- token: this.token,
297
- workdir: this.agentConfig.workdir,
298
- assignedProject: this.assignedProject,
299
- runtimeModel: this.runnerConfig.model,
300
- runnerType: this.runnerConfig.type,
301
- sandboxContainer: this.sandbox?.getContainerName(),
302
- });
303
-
304
- // Start heartbeat with auto-refresh
305
- console.log("Starting heartbeat...");
306
- this.heartbeat = new Heartbeat({
307
- url: this.config.hubUrl,
308
- token: this.token,
309
- intervalMs: 30000,
310
- agentName: this.agentName,
311
- agentId: this.agentId,
312
- apiKey: this.config.apiKey,
313
- workspace: this.config.workspace,
314
- onError: (error) => {
315
- console.error("Heartbeat error:", error.message);
316
- },
317
- onContextSave: () => {
318
- // Periodically save context (every 5 heartbeats = ~2.5 minutes)
319
- this.saveAgentContext();
320
- },
321
- contextSaveFrequency: 5,
322
- onTokenRefresh: (newToken) => {
323
- this.token = newToken;
324
- // Update state file
325
- updateAgentInState(this.agentName, { token: newToken });
326
- // Update tmux environment
327
- updateSessionEnvironment(this.agentName, {
328
- AGENT_TOKEN: newToken,
329
- AGENTMESH_AGENT_ID: this.agentId!,
330
- });
331
- // Reconnect WebSocket with new token
332
- if (this.ws) {
333
- this.ws.disconnect();
334
- const wsUrl = this.config.hubUrl
335
- .replace("https://", "wss://")
336
- .replace("http://", "ws://");
337
- this.ws = new AgentWebSocket({
338
- url: `${wsUrl}/ws/v1`,
339
- token: newToken,
340
- onMessage: (event) => {
341
- console.log(`[WS] Received event: ${event.type}`);
342
- handleWebSocketEvent(this.agentName, event);
343
- },
344
- onConnect: () => {
345
- console.log("WebSocket reconnected with new token");
346
- },
347
- onDisconnect: () => {
348
- console.log("WebSocket disconnected");
349
- },
350
- onError: (error) => {
351
- console.error("WebSocket error:", error.message);
352
- },
353
- });
354
- this.ws.connect();
355
- }
356
- },
357
- });
358
- this.heartbeat.start();
359
-
360
- // Connect WebSocket
361
- console.log("Connecting WebSocket...");
362
- const wsUrl = this.config.hubUrl.replace("https://", "wss://").replace("http://", "ws://");
363
-
364
- this.ws = new AgentWebSocket({
365
- url: `${wsUrl}/ws/v1`,
366
- token: this.token,
367
- onMessage: (event) => {
368
- console.log(`[WS] Received event: ${event.type}`);
369
- handleWebSocketEvent(this.agentName, event, {
370
- hubUrl: this.config.hubUrl,
371
- token: this.token ?? undefined,
372
- });
373
- },
374
- onConnect: () => {
375
- console.log("WebSocket connected");
376
- },
377
- onDisconnect: () => {
378
- console.log("WebSocket disconnected");
379
- },
380
- onError: (error) => {
381
- console.error("WebSocket error:", error.message);
382
- },
383
- });
384
- this.ws.connect();
385
-
386
- // Check inbox and auto-nudge with full handoff details
387
- console.log("Checking inbox...");
388
- try {
389
- const inboxItems = await checkInbox(this.config.hubUrl, this.config.workspace, this.token);
390
- injectStartupMessage(this.agentName, inboxItems.length, inboxItems);
391
- } catch (error) {
392
- console.error("Failed to check inbox:", error);
393
- injectStartupMessage(this.agentName, 0);
394
- }
395
-
396
- // Restore context from previous session
397
- if (this.shouldRestoreContext && this.agentId) {
398
- console.log("Checking for previous context...");
399
- const savedContext = loadContext(this.agentId);
400
- if (savedContext) {
401
- if (this._attemptedResumeSessionId && !this.serveMode && !this.sandboxMode) {
402
- // Native session resume was attempted — verify it worked.
403
- // Wait for OpenCode to write a NEW session entry to logs.
404
- // If resume succeeded, it reuses the session (no new entry).
405
- // If resume failed, OpenCode creates a new session (new entry appears).
406
- const newSessionId = await waitForNewSessionId(
407
- this.agentName,
408
- this._preStartSessionId ?? null,
409
- 15000,
410
- );
411
-
412
- if (!newSessionId) {
413
- // No new session appeared in logs. Could mean:
414
- // a) Resume succeeded (OpenCode reused session, no new "created" log)
415
- // b) OpenCode is sitting at splash (session not found, no new session created)
416
- const health = isSessionHealthy(this.agentName);
417
- const currentSessionId = getLatestSessionId(this.agentName);
418
-
419
- if (!health.healthy) {
420
- // OpenCode died — clear stale session ID to prevent restart loop
421
- console.log(`[CONTEXT] Fallback: OpenCode not healthy, injecting text summary.`);
422
- savedContext.custom = { ...savedContext.custom, opencodeSessionId: undefined };
423
- saveContext(savedContext);
424
- injectRestoredContext(this.agentName, savedContext);
425
- } else if (currentSessionId === this._attemptedResumeSessionId) {
426
- // The session ID we tried to resume is still the latest — resume worked
427
- console.log(`[CONTEXT] Resumed OpenCode session ${this._attemptedResumeSessionId}`);
428
- } else {
429
- // Pane is alive but no matching session ID in logs — OpenCode is at splash
430
- console.log(
431
- `[CONTEXT] Fallback: session not found in OpenCode. Injecting text summary.`,
432
- );
433
- savedContext.custom = { ...savedContext.custom, opencodeSessionId: undefined };
434
- saveContext(savedContext);
435
- injectRestoredContext(this.agentName, savedContext);
436
- }
437
- } else if (newSessionId === this._attemptedResumeSessionId) {
438
- // OpenCode logged the same session ID — resume succeeded
439
- console.log(`[CONTEXT] Resumed OpenCode session ${this._attemptedResumeSessionId}`);
440
- } else {
441
- // OpenCode created a different session — resume failed, fallback to text.
442
- // Update saved context with new session ID to prevent restart loop.
443
- console.log(
444
- `[CONTEXT] Fallback: resume failed (expected ${this._attemptedResumeSessionId}, got ${newSessionId}). Injecting text summary.`,
445
- );
446
- savedContext.custom = { ...savedContext.custom, opencodeSessionId: newSessionId };
447
- saveContext(savedContext);
448
- injectRestoredContext(this.agentName, savedContext);
449
- }
450
- } else {
451
- // No session ID saved or non-tmux mode — use text injection
452
- console.log(`Restoring context from ${savedContext.savedAt}`);
453
- await new Promise((resolve) => setTimeout(resolve, 1000));
454
- injectRestoredContext(this.agentName, savedContext);
455
- }
456
- }
457
- }
458
-
459
- this.isRunning = true;
460
-
461
- // Start session health monitoring (every 60 seconds)
462
- this.startHealthMonitor();
463
-
464
- console.log(`
465
- Agent "${this.agentName}" is running.
466
-
467
- Attach to session:
468
- agentmesh attach ${this.agentName}
469
-
470
- Stop agent:
471
- agentmesh stop ${this.agentName}
472
-
473
- Nudge agent:
474
- agentmesh nudge ${this.agentName} "Your message"
475
- `);
476
-
477
- // Handle shutdown
478
- process.on("SIGINT", () => this.stop());
479
- process.on("SIGTERM", () => this.stop());
480
- }
481
-
482
- /**
483
- * Starts periodic health monitoring for the tmux session
484
- * Includes auto-restart logic and progress watchdog
485
- */
486
- private startHealthMonitor(): void {
487
- // Skip health monitoring for serve mode (no tmux session)
488
- if (this.serveMode) return;
489
-
490
- const logDir = path.join(os.homedir(), ".agentmesh", "logs");
491
- if (!fs.existsSync(logDir)) {
492
- fs.mkdirSync(logDir, { recursive: true });
493
- }
494
-
495
- this.healthCheckInterval = setInterval(async () => {
496
- if (!this.isRunning) return;
497
-
498
- // Reset restart count after stable operation
499
- if (this.lastStableTime && this.restartCount > 0) {
500
- const stableTime = Date.now() - this.lastStableTime.getTime();
501
- if (stableTime > RESTART_COUNT_RESET_MS) {
502
- console.log(`[HEALTH] Agent stable for 30+ minutes, resetting restart count`);
503
- this.restartCount = 0;
504
- resetAgentRestartCount(this.agentName);
505
- }
506
- }
507
-
508
- // For sandbox mode, pass container name so health check looks inside container
509
- const containerName = this.sandboxMode ? this.sandbox?.getContainerName() : undefined;
510
- const health = isSessionHealthy(this.agentName, containerName);
511
-
512
- if (!health.healthy) {
513
- // Session died - attempt restart
514
- await this.handleSessionDeath(health.reason || "unknown", logDir);
515
- return;
516
- }
517
-
518
- // Session is alive - check progress watchdog
519
- const progress = checkAgentProgress(this.agentName, containerName);
520
-
521
- if (progress.status === "permission_blocked" || progress.status === "stuck") {
522
- await this.handleStuckAgent(progress);
523
- } else if (progress.status === "active") {
524
- // Agent is working - reset stuck tracking
525
- if (this.stuckSince) {
526
- console.log(`[HEALTH] Agent resumed activity`);
527
- this.stuckSince = null;
528
- this.nudgeSentAt = null;
529
- updateAgentInState(this.agentName, { stuckSince: undefined, status: "running" });
530
- }
531
- this.lastStableTime = new Date();
532
- }
533
- }, 60000); // Check every 60 seconds
534
- }
535
-
536
- /**
537
- * Handles session death - logs crash and attempts auto-restart
538
- */
539
- private async handleSessionDeath(reason: string, logDir: string): Promise<void> {
540
- const timestamp = new Date().toISOString();
541
- const logFile = path.join(logDir, `crash-${this.agentName}.log`);
542
-
543
- // Capture last session output before it's gone
544
- let lastOutput = "";
545
- try {
546
- lastOutput = captureSessionOutput(this.agentName, 200) || "Unable to capture output";
547
- } catch {
548
- lastOutput = "Failed to capture session output";
549
- }
550
-
551
- const crashLog = `
552
- ================================================================================
553
- AGENT CRASH DETECTED
554
- ================================================================================
555
- Timestamp: ${timestamp}
556
- Agent: ${this.agentName}
557
- Agent ID: ${this.agentId}
558
- Reason: ${reason}
559
- Restart Count: ${this.restartCount}/${MAX_RESTART_ATTEMPTS}
560
- Sandbox: ${this.sandboxMode ? this.sandbox?.getContainerName() : "none"}
561
- Workdir: ${this.agentConfig.workdir}
562
- Model: ${this.runnerConfig.model}
563
-
564
- --- Last Session Output ---
565
- ${lastOutput}
566
- ================================================================================
567
-
568
- `;
569
-
570
- fs.appendFileSync(logFile, crashLog);
571
-
572
- // Save context (including session ID) before restart attempt
573
- if (this.agentId) {
574
- this.saveAgentContext();
575
- }
576
-
577
- // Check if we can restart
578
- if (this.restartCount < MAX_RESTART_ATTEMPTS) {
579
- this.restartCount++;
580
- console.error(
581
- `[CRASH] Session died: ${reason}. Attempting restart (${this.restartCount}/${MAX_RESTART_ATTEMPTS})...`,
582
- );
583
-
584
- updateAgentInState(this.agentName, {
585
- restartCount: this.restartCount,
586
- lastRestartAt: timestamp,
587
- status: "running",
588
- });
589
-
590
- try {
591
- await this.restartSession();
592
- console.log(`[RESTART] Agent restarted successfully`);
593
- this.lastStableTime = new Date();
594
- } catch (error) {
595
- console.error(`[RESTART] Failed to restart: ${(error as Error).message}`);
596
- }
597
- } else {
598
- // Exceeded restart limit - mark as failed
599
- console.error(
600
- `[FAILED] Agent exceeded restart limit (${MAX_RESTART_ATTEMPTS}). Manual intervention required.`,
601
- );
602
-
603
- // Terminal bell to alert user
604
- process.stdout.write("\x07");
605
-
606
- updateAgentInState(this.agentName, {
607
- status: "failed",
608
- restartCount: this.restartCount,
609
- });
610
-
611
- // Stop monitoring
612
- this.isRunning = false;
613
- if (this.healthCheckInterval) {
614
- clearInterval(this.healthCheckInterval);
615
- this.healthCheckInterval = null;
616
- }
617
- }
618
- }
619
-
620
- /**
621
- * Handles stuck agent - sends nudge first, then restarts if still stuck
622
- */
623
- private async handleStuckAgent(progress: {
624
- status: string;
625
- blockedOn?: string;
626
- details?: string;
627
- }): Promise<void> {
628
- const now = new Date();
629
-
630
- if (!this.stuckSince) {
631
- // First detection of stuck state
632
- this.stuckSince = now;
633
- console.log(
634
- `[HEALTH] Agent appears stuck: ${progress.details || progress.blockedOn || "no activity"}`,
635
- );
636
-
637
- updateAgentInState(this.agentName, {
638
- stuckSince: now.toISOString(),
639
- status: "stuck",
640
- });
641
- }
642
-
643
- // Only nudge worker agents - others restart immediately
644
- if (this.isWorkerAgent) {
645
- // If we haven't sent a nudge yet, send one
646
- if (!this.nudgeSentAt) {
647
- console.log(`[HEALTH] Sending nudge to worker agent...`);
648
-
649
- const nudgeMessage =
650
- progress.status === "permission_blocked"
651
- ? "Please continue with your task. If you see a permission prompt, try an alternative approach that doesn't require that permission."
652
- : "Please continue with your current task.";
653
-
654
- const sent = sendNudge(this.agentName, nudgeMessage);
655
- if (sent) {
656
- this.nudgeSentAt = now;
657
- console.log(`[HEALTH] Nudge sent successfully`);
658
- } else {
659
- console.log(`[HEALTH] Failed to send nudge`);
660
- }
661
- return;
662
- }
663
-
664
- // Check if enough time has passed since nudge
665
- const timeSinceNudge = now.getTime() - this.nudgeSentAt.getTime();
666
- if (timeSinceNudge < NUDGE_WAIT_MS) {
667
- // Still waiting for agent to respond to nudge
668
- return;
669
- }
670
- }
671
-
672
- // Agent still stuck - trigger restart (or restart immediately if not a worker)
673
- console.log(
674
- `[HEALTH] Agent still stuck${this.isWorkerAgent ? " after nudge" : ""}, triggering restart...`,
675
- );
676
- this.stuckSince = null;
677
- this.nudgeSentAt = null;
678
-
679
- await this.handleSessionDeath(
680
- "stuck_after_nudge",
681
- path.join(os.homedir(), ".agentmesh", "logs"),
682
- );
683
- }
684
-
685
- /**
686
- * Restarts the agent session (sandbox or non-sandbox)
687
- */
688
- private async restartSession(): Promise<void> {
689
- // Destroy existing session
690
- destroySession(this.agentName);
691
-
692
- if (this.sandboxMode && this.sandbox) {
693
- // Restart sandbox container
694
- const newContainerId = await this.sandbox.restart();
695
- console.log(`[RESTART] New container: ${newContainerId.substring(0, 12)}`);
696
-
697
- // Recreate tmux session for sandbox
698
- const containerName = this.sandbox.getContainerName();
699
- const sessionName = getSessionName(this.agentName);
700
-
701
- // Build environment args for docker exec
702
- const envArgs: string[] = [];
703
- const allEnv = {
704
- ...this.runnerConfig.env,
705
- AGENT_TOKEN: this.token!,
706
- AGENTMESH_AGENT_ID: this.agentId!,
707
- };
708
- for (const [key, value] of Object.entries(allEnv)) {
709
- if (value !== undefined && value !== "") {
710
- envArgs.push(`-e "${key}=${value}"`);
711
- }
712
- }
713
- const envString = envArgs.join(" ");
714
- const modelArg = this.runnerConfig.env?.OPENCODE_MODEL
715
- ? ` --model ${this.runnerConfig.env.OPENCODE_MODEL}`
716
- : "";
717
- const dockerExecCommand = `docker exec -it ${envString} ${containerName} opencode${modelArg}`;
718
-
719
- const created = createSession(this.agentName, dockerExecCommand, undefined, undefined);
720
- if (!created) {
721
- throw new Error("Failed to create tmux session for restarted sandbox");
722
- }
723
-
724
- // Update state with new container name
725
- updateAgentInState(this.agentName, {
726
- sandboxContainer: containerName,
727
- });
728
- } else {
729
- // Non-sandbox restart — load saved session ID for native resume
730
- let savedSessionId: string | undefined;
731
- let savedContext = null;
732
- if (this.agentId) {
733
- savedContext = loadContext(this.agentId);
734
- savedSessionId = savedContext?.custom?.opencodeSessionId as string | undefined;
735
- if (savedSessionId) {
736
- console.log(`[RESTART] Attempting to resume OpenCode session: ${savedSessionId}`);
737
- }
738
- }
739
-
740
- const preRestartSessionId = snapshotSessionId(this.agentName);
741
-
742
- const created = createSession(
743
- this.agentName,
744
- this.agentConfig.command,
745
- this.agentConfig.workdir,
746
- this.runnerConfig.env,
747
- savedSessionId,
748
- );
749
-
750
- if (!created) {
751
- throw new Error("Failed to create tmux session");
752
- }
753
-
754
- // Re-inject environment
755
- updateSessionEnvironment(this.agentName, {
756
- AGENT_TOKEN: this.token!,
757
- AGENTMESH_AGENT_ID: this.agentId!,
758
- ...this.runnerConfig.env,
759
- });
760
-
761
- // Verify native resume and fallback if needed
762
- if (savedSessionId && savedContext) {
763
- const newSessionId = await waitForNewSessionId(this.agentName, preRestartSessionId, 15000);
764
-
765
- if (!newSessionId) {
766
- const health = isSessionHealthy(this.agentName);
767
- const currentSessionId = getLatestSessionId(this.agentName);
768
-
769
- if (!health.healthy) {
770
- console.log(`[RESTART] Fallback: OpenCode not healthy, injecting text summary.`);
771
- savedContext.custom = { ...savedContext.custom, opencodeSessionId: undefined };
772
- saveContext(savedContext);
773
- injectRestoredContext(this.agentName, savedContext);
774
- } else if (currentSessionId === savedSessionId) {
775
- console.log(`[RESTART] Resumed OpenCode session ${savedSessionId}`);
776
- } else {
777
- console.log(`[RESTART] Fallback: session not found. Injecting text summary.`);
778
- savedContext.custom = { ...savedContext.custom, opencodeSessionId: undefined };
779
- saveContext(savedContext);
780
- injectRestoredContext(this.agentName, savedContext);
781
- }
782
- } else if (newSessionId === savedSessionId) {
783
- console.log(`[RESTART] Resumed OpenCode session ${savedSessionId}`);
784
- } else {
785
- console.log(
786
- `[RESTART] Fallback: resume failed (got ${newSessionId}). Injecting text summary.`,
787
- );
788
- savedContext.custom = { ...savedContext.custom, opencodeSessionId: newSessionId };
789
- saveContext(savedContext);
790
- injectRestoredContext(this.agentName, savedContext);
791
- }
792
- }
793
- }
794
-
795
- // Wait for session to be ready
796
- await new Promise((resolve) => setTimeout(resolve, 2000));
797
- }
798
-
799
- async stop(): Promise<void> {
800
- console.log(`\nStopping agent: ${this.agentName}`);
801
-
802
- this.isRunning = false;
803
-
804
- // Stop health monitor
805
- if (this.healthCheckInterval) {
806
- clearInterval(this.healthCheckInterval);
807
- this.healthCheckInterval = null;
808
- }
809
-
810
- // Save context before stopping
811
- if (this.agentId) {
812
- console.log("Saving agent context...");
813
- this.saveAgentContext();
814
- }
815
-
816
- // Stop heartbeat
817
- if (this.heartbeat) {
818
- this.heartbeat.stop();
819
- this.heartbeat = null;
820
- }
821
-
822
- // Disconnect WebSocket
823
- if (this.ws) {
824
- this.ws.disconnect();
825
- this.ws = null;
826
- }
827
-
828
- // Stop sandbox, serve process, or destroy tmux session
829
- if (this.sandboxMode && this.sandbox) {
830
- console.log("Stopping sandbox...");
831
- // In sandbox mode, we have both a tmux session (on host) and a Docker container
832
- // Destroy tmux session first (this stops docker exec)
833
- destroySession(this.agentName);
834
- // Then destroy the container
835
- await this.sandbox.destroy();
836
- this.sandbox = null;
837
- } else if (this.serveMode && this.serveProcess) {
838
- console.log("Stopping opencode serve...");
839
- this.serveProcess.kill("SIGTERM");
840
- this.serveProcess = null;
841
- } else {
842
- destroySession(this.agentName);
843
- }
844
-
845
- // Update state to mark as stopped but preserve agentId for next restart
846
- updateAgentInState(this.agentName, {
847
- pid: 0,
848
- tmuxSession: "",
849
- startedAt: "",
850
- token: undefined,
851
- });
852
-
853
- console.log("Agent stopped.");
854
- process.exit(0);
855
- }
856
-
857
- /**
858
- * Starts opencode serve mode (for Integration Service)
859
- * Replaces tmux with a direct HTTP server
860
- */
861
- private async startServeMode(): Promise<void> {
862
- console.log(`Starting opencode serve mode on port ${this.servePort}...`);
863
-
864
- const workdir = this.agentConfig.workdir || process.cwd();
865
-
866
- // Isolate OpenCode's SQLite database per agent to prevent WAL corruption.
867
- // See docs/RCA-OPENCODE-SQLITE-CORRUPTION.md for details.
868
- const agentDataDir = path.join(os.homedir(), ".agentmesh", "opencode-data", this.agentName);
869
- const agentOpencodeDir = path.join(agentDataDir, "opencode");
870
- if (!fs.existsSync(agentOpencodeDir)) {
871
- fs.mkdirSync(agentOpencodeDir, { recursive: true });
872
- }
873
-
874
- // Copy auth.json from default OpenCode data dir so agents inherit API keys.
875
- // Strips xAI provider to prevent OpenCode from defaulting to non-Anthropic models.
876
- const agentAuthPath = path.join(agentOpencodeDir, "auth.json");
877
- const sourceAuthPath = path.join(os.homedir(), ".local", "share", "opencode", "auth.json");
878
- if (!fs.existsSync(agentAuthPath) && fs.existsSync(sourceAuthPath)) {
879
- try {
880
- const auth = JSON.parse(fs.readFileSync(sourceAuthPath, "utf-8"));
881
- delete auth.xai;
882
- fs.writeFileSync(agentAuthPath, JSON.stringify(auth, null, 2));
883
- } catch {
884
- // Non-fatal — agent will just need manual auth
885
- }
886
- }
887
-
888
- // Build environment for opencode serve
889
- const env: Record<string, string> = {
890
- ...process.env,
891
- ...this.runnerConfig.env,
892
- AGENT_TOKEN: this.token!,
893
- AGENTMESH_AGENT_ID: this.agentId!,
894
- XDG_DATA_HOME: agentDataDir,
895
- } as Record<string, string>;
896
-
897
- // Spawn opencode serve as a child process
898
- this.serveProcess = spawn(
899
- "opencode",
900
- ["serve", "--port", String(this.servePort), "--hostname", "0.0.0.0"],
901
- {
902
- cwd: workdir,
903
- env,
904
- stdio: ["ignore", "inherit", "inherit"],
905
- },
906
- );
907
-
908
- // Handle process exit
909
- this.serveProcess.on("exit", (code, signal) => {
910
- console.error(`opencode serve exited with code ${code}, signal ${signal}`);
911
- if (this.isRunning) {
912
- console.log("Restarting opencode serve in 5 seconds...");
913
- setTimeout(() => {
914
- if (this.isRunning) {
915
- this.startServeMode().catch(console.error);
916
- }
917
- }, 5000);
918
- }
919
- });
920
-
921
- this.serveProcess.on("error", (error) => {
922
- console.error("Failed to start opencode serve:", error);
923
- });
924
-
925
- // Wait a moment for the server to start
926
- await new Promise((resolve) => setTimeout(resolve, 2000));
927
-
928
- console.log(`opencode serve started on http://0.0.0.0:${this.servePort}`);
929
-
930
- // Store saved session ID for integration service reuse
931
- if (this.shouldRestoreContext && this.agentId) {
932
- const savedContext = loadContext(this.agentId);
933
- const savedSessionId = savedContext?.custom?.opencodeSessionId as string | undefined;
934
- if (savedSessionId) {
935
- console.log(`[SERVE] Saved OpenCode session available for reuse: ${savedSessionId}`);
936
- updateAgentInState(this.agentName, { opencodeSessionId: savedSessionId });
937
- }
938
- }
939
- }
940
-
941
- /**
942
- * Starts agent in Docker sandbox mode
943
- * Provides filesystem isolation with only workspace mounted
944
- *
945
- * Strategy: Start Docker container with tail -f /dev/null, then create
946
- * a tmux session on the HOST that runs `docker exec -it <container> opencode`.
947
- * This way tmux provides the TTY that docker exec needs.
948
- */
949
- private async startSandboxMode(): Promise<void> {
950
- console.log("Starting in Docker sandbox mode...");
951
-
952
- // Check Docker availability
953
- if (!DockerSandbox.checkDockerAvailable()) {
954
- throw new Error(
955
- "Docker is not available. Install Docker or use --sandbox host to run on host.",
956
- );
957
- }
958
-
959
- const workdir = this.agentConfig.workdir || process.cwd();
960
-
961
- // Check for existing sandbox container
962
- const existingContainer = DockerSandbox.findExisting(this.agentName);
963
- if (existingContainer) {
964
- console.log(`Found existing sandbox container: ${existingContainer}`);
965
- console.log(`Stop it with: agentmesh stop ${this.agentName}`);
966
- throw new Error("Sandbox container already exists");
967
- }
968
-
969
- // Build additional mounts for credentials and config
970
- // The entrypoint script copies these from /tmp/ to the correct locations
971
- const additionalMounts: string[] = [];
972
-
973
- // Mount git credentials
974
- const gitCredentialsPath = path.join(os.homedir(), ".git-credentials");
975
- if (fs.existsSync(gitCredentialsPath)) {
976
- additionalMounts.push(`${gitCredentialsPath}:/tmp/.git-credentials-host:ro`);
977
- }
978
-
979
- // Mount OpenCode auth.json for API provider tokens (Anthropic, OpenAI, etc.)
980
- const opencodeAuthPath = path.join(os.homedir(), ".local", "share", "opencode", "auth.json");
981
- if (fs.existsSync(opencodeAuthPath)) {
982
- additionalMounts.push(`${opencodeAuthPath}:/tmp/.opencode-auth-host:ro`);
983
- }
984
-
985
- // Mount AgentMesh config for hub URL, API key, workspace
986
- const agentmeshConfigPath = path.join(os.homedir(), ".agentmesh", "config.json");
987
- if (fs.existsSync(agentmeshConfigPath)) {
988
- additionalMounts.push(`${agentmeshConfigPath}:/tmp/.agentmesh-config-host:ro`);
989
- }
990
-
991
- // Create and mount permissive OpenCode config for sandbox
992
- // This allows all permissions since the container is already sandboxed
993
- this.ensureSandboxOpencodeConfig();
994
- additionalMounts.push(`${SANDBOX_OPENCODE_CONFIG_PATH}:/workspace/opencode.json:ro`);
995
-
996
- // Pass GitHub token as environment variable for git operations
997
- const gitCredentials = fs.existsSync(gitCredentialsPath)
998
- ? fs.readFileSync(gitCredentialsPath, "utf-8").trim()
999
- : "";
1000
- const gitHubToken = gitCredentials.match(/github_pat_[^\s@]+/)?.[0] || "";
1001
-
1002
- // Build the command to run inside the container
1003
- // The agentmesh CLI inside the container will create tmux + opencode
1004
- const model =
1005
- this.runnerConfig.env?.OPENCODE_MODEL || this.runnerConfig.model || "claude-sonnet-4";
1006
- const containerCommand = [
1007
- "agentmesh",
1008
- "start",
1009
- "--name",
1010
- this.agentName,
1011
- "--model",
1012
- model,
1013
- "--foreground",
1014
- ];
1015
-
1016
- // Create sandbox configuration
1017
- // Isolate OpenCode's SQLite database per agent to prevent WAL corruption.
1018
- const hostDataDir = path.join(os.homedir(), ".agentmesh", "opencode-data", this.agentName);
1019
- if (!fs.existsSync(hostDataDir)) {
1020
- fs.mkdirSync(hostDataDir, { recursive: true });
1021
- }
1022
-
1023
- this.sandbox = new DockerSandbox({
1024
- agentName: this.agentName,
1025
- image: this.sandboxImage,
1026
- workspacePath: workdir,
1027
- cpuLimit: this.sandboxCpu,
1028
- memoryLimit: this.sandboxMemory,
1029
- env: {
1030
- ...this.runnerConfig.env,
1031
- AGENT_TOKEN: this.token!,
1032
- AGENTMESH_AGENT_ID: this.agentId!,
1033
- AGENT_NAME: this.agentName,
1034
- // XDG_DATA_HOME set by entrypoint based on AGENT_NAME
1035
- // Git credentials for pushing to GitHub
1036
- ...(gitHubToken && { GH_TOKEN: gitHubToken, GITHUB_TOKEN: gitHubToken }),
1037
- },
1038
- serveMode: this.serveMode,
1039
- servePort: this.servePort,
1040
- networkMode: "bridge",
1041
- additionalMounts: [
1042
- ...additionalMounts,
1043
- `${hostDataDir}:/home/node/.agentmesh/opencode-data/${this.agentName}:rw`,
1044
- ],
1045
- command: this.serveMode ? undefined : containerCommand,
1046
- });
1047
-
1048
- // Validate mount policy (will throw if denied)
1049
- this.sandbox.validateMountPolicy();
1050
-
1051
- // Pull image if needed
1052
- await this.sandbox.pullImage();
1053
-
1054
- // Start container with agentmesh running inside
1055
- // The entrypoint script sets up credentials before agentmesh starts
1056
- await this.sandbox.start();
1057
-
1058
- const containerName = this.sandbox.getContainerName();
1059
-
1060
- console.log(`
1061
- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1062
- SANDBOX MODE ACTIVE
1063
- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1064
-
1065
- Container: ${containerName}
1066
- Image: ${this.sandboxImage}
1067
- Workspace: ${workdir} -> /workspace
1068
- CPU: ${this.sandboxCpu} core(s)
1069
- Memory: ${this.sandboxMemory}
1070
- Model: ${model}
1071
-
1072
- The agent daemon is running INSIDE the Docker container.
1073
- tmux session and OpenCode are managed inside the container.
1074
-
1075
- Attach: agentmesh attach ${this.agentName}
1076
- Nudge: agentmesh nudge ${this.agentName} "message"
1077
- Stop: agentmesh stop ${this.agentName}
1078
- Logs: docker logs ${containerName}
1079
-
1080
- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1081
- `);
1082
-
1083
- // No host tmux session needed - the container runs agentmesh which creates its own tmux
1084
- // Heartbeats are sent by the daemon running inside the container
1085
- }
1086
-
1087
- /**
1088
- * Saves the current agent context to disk
1089
- */
1090
- private saveAgentContext(): void {
1091
- if (!this.agentId) return;
1092
-
1093
- try {
1094
- // Load existing context or create new
1095
- const context = loadOrCreateContext(this.agentId, this.agentName);
1096
-
1097
- // Capture current session state
1098
- const sessionContext = captureSessionContext(this.agentName);
1099
- if (sessionContext) {
1100
- context.workingState = {
1101
- ...context.workingState,
1102
- workdir: sessionContext.workdir,
1103
- gitBranch: sessionContext.gitBranch,
1104
- gitStatus: sessionContext.gitStatus,
1105
- };
1106
- }
1107
-
1108
- // Capture OpenCode session ID for native resume on restart
1109
- const sessionId = getLatestSessionId(this.agentName);
1110
- if (sessionId) {
1111
- context.custom = { ...context.custom, opencodeSessionId: sessionId };
1112
- console.log(`[CONTEXT] Captured OpenCode session ID: ${sessionId}`);
1113
- }
1114
-
1115
- // Save updated context
1116
- saveContext(context);
1117
- console.log(`Context saved for agent ${this.agentName}`);
1118
- } catch (error) {
1119
- console.error("Failed to save agent context:", error);
1120
- }
1121
- }
1122
-
1123
- /**
1124
- * Fetches assignments from HQ and validates workdir setup
1125
- * Uses project.workdir from HQ as source of truth, falls back to helpful instructions
1126
- */
1127
- private async checkAssignments(): Promise<void> {
1128
- if (!this.token) return;
1129
-
1130
- try {
1131
- console.log("Fetching project assignments from HQ...");
1132
- const assignments = await fetchAssignments(this.config.hubUrl, this.token);
1133
-
1134
- if (assignments.length === 0) {
1135
- console.log("No project assignments found.");
1136
- return;
1137
- }
1138
-
1139
- console.log(`Found ${assignments.length} assignment(s):`);
1140
- for (const assignment of assignments) {
1141
- const repoInfo = assignment.repo ? ` -> ${assignment.repo.full_name}` : "";
1142
- const workdirInfo = assignment.project.workdir ? ` [${assignment.project.workdir}]` : "";
1143
- console.log(` - ${assignment.project.name} (${assignment.role})${repoInfo}${workdirInfo}`);
1144
- }
1145
-
1146
- this.assignedProject = assignments[0]?.project.name;
1147
-
1148
- // If no CLI workdir specified, try to use project.workdir from HQ
1149
- if (!this.agentConfig.workdir) {
1150
- const assignmentWithWorkdir = assignments.find((a) => a.project.workdir);
1151
- if (assignmentWithWorkdir?.project.workdir) {
1152
- console.log(
1153
- `Using workdir from project settings: ${assignmentWithWorkdir.project.workdir}`,
1154
- );
1155
- this.agentConfig.workdir = assignmentWithWorkdir.project.workdir;
1156
- return;
1157
- }
1158
-
1159
- // No project.workdir set, check if we have a repo assignment
1160
- const repoAssignment = assignments.find((a) => a.repo !== null);
1161
- if (repoAssignment) {
1162
- const repo = repoAssignment.repo!;
1163
- const expandedPath = path.join(
1164
- os.homedir(),
1165
- ".agentmesh",
1166
- "workspaces",
1167
- this.config.workspace,
1168
- repoAssignment.project.code.toLowerCase(),
1169
- this.agentName,
1170
- );
1171
- const suggestedPath = `~/.agentmesh/workspaces/${this.config.workspace}/${repoAssignment.project.code.toLowerCase()}/${this.agentName}`;
1172
-
1173
- // If --auto-setup is enabled, automatically clone the repo
1174
- if (this.autoSetup) {
1175
- this.agentConfig.workdir = this.setupWorkspace(
1176
- expandedPath,
1177
- repo.url,
1178
- repo.default_branch,
1179
- repoAssignment.project.name,
1180
- );
1181
- return;
1182
- }
1183
-
1184
- console.error(`
1185
- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1186
- ⚠️ WORKDIR REQUIRED
1187
-
1188
- You have a project assignment with a repository, but no workdir is configured.
1189
-
1190
- Project: ${repoAssignment.project.name}
1191
- Repo: ${repo.full_name}
1192
- Branch: ${repo.default_branch}
1193
-
1194
- Option 1: Set workdir in project settings (recommended)
1195
- - Go to AgentMesh HQ → Projects → ${repoAssignment.project.name} → Settings
1196
- - Set the workdir field to the local path
1197
-
1198
- Option 2: Set up workspace manually and pass --workdir:
1199
-
1200
- mkdir -p ${suggestedPath}
1201
- git clone ${repo.url} ${suggestedPath}
1202
- cd ${suggestedPath} && git checkout ${repo.default_branch}
1203
-
1204
- Then start the agent with:
1205
-
1206
- agentmesh start -n ${this.agentName} --workdir ${suggestedPath}
1207
-
1208
- Option 3: Use --auto-setup to automatically clone the repository:
1209
-
1210
- agentmesh start -n ${this.agentName} --auto-setup
1211
-
1212
- ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
1213
- `);
1214
- // No session to clean up - we haven't created it yet
1215
- process.exit(1);
1216
- }
1217
- }
1218
- } catch (error) {
1219
- // Non-fatal: log and continue (HQ might not have assignments feature yet)
1220
- console.log("Could not fetch assignments:", (error as Error).message);
1221
- }
1222
- }
1223
-
1224
- /**
1225
- * Sets up workspace by cloning repository or using existing clone
1226
- * Returns the absolute path to the workspace
1227
- */
1228
- private setupWorkspace(
1229
- workspacePath: string,
1230
- repoUrl: string,
1231
- defaultBranch: string,
1232
- projectName: string,
1233
- ): string {
1234
- console.log(
1235
- `\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━`,
1236
- );
1237
- console.log(`🔧 AUTO-SETUP: Setting up workspace for ${projectName}`);
1238
- console.log(
1239
- `━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n`,
1240
- );
1241
-
1242
- // Check if directory already exists and is a git repo
1243
- const gitDir = path.join(workspacePath, ".git");
1244
- if (fs.existsSync(gitDir)) {
1245
- console.log(`✓ Workspace already exists: ${workspacePath}`);
1246
- console.log(` Updating from remote...`);
1247
-
1248
- try {
1249
- // Fetch and checkout the branch
1250
- execSync(`git fetch origin`, { cwd: workspacePath, stdio: "inherit" });
1251
- execSync(`git checkout ${defaultBranch}`, { cwd: workspacePath, stdio: "inherit" });
1252
- execSync(`git pull origin ${defaultBranch}`, { cwd: workspacePath, stdio: "inherit" });
1253
- console.log(`✓ Workspace updated successfully\n`);
1254
- } catch (error) {
1255
- console.warn(`⚠ Could not update workspace: ${(error as Error).message}`);
1256
- console.log(` Continuing with existing state...\n`);
1257
- }
1258
-
1259
- return workspacePath;
1260
- }
1261
-
1262
- // Create parent directories
1263
- const parentDir = path.dirname(workspacePath);
1264
- if (!fs.existsSync(parentDir)) {
1265
- console.log(`Creating directory: ${parentDir}`);
1266
- fs.mkdirSync(parentDir, { recursive: true });
1267
- }
1268
-
1269
- // Clone the repository
1270
- console.log(`Cloning repository...`);
1271
- console.log(` URL: ${repoUrl}`);
1272
- console.log(` Path: ${workspacePath}`);
1273
- console.log(` Branch: ${defaultBranch}\n`);
1274
-
1275
- try {
1276
- execSync(`git clone --branch ${defaultBranch} "${repoUrl}" "${workspacePath}"`, {
1277
- stdio: "inherit",
1278
- });
1279
- console.log(`\n✓ Repository cloned successfully`);
1280
- } catch (error) {
1281
- console.error(`\n✗ Failed to clone repository: ${(error as Error).message}`);
1282
- console.error(`\nMake sure you have access to the repository and SSH keys are configured.`);
1283
- // No session to clean up - we haven't created it yet
1284
- process.exit(1);
1285
- }
1286
-
1287
- console.log(`✓ Workspace ready: ${workspacePath}\n`);
1288
- return workspacePath;
1289
- }
1290
-
1291
- /**
1292
- * Ensures the sandbox OpenCode config exists
1293
- * Creates ~/.agentmesh/opencode-sandbox.json with permissive permissions and model
1294
- */
1295
- private ensureSandboxOpencodeConfig(): void {
1296
- const configDir = path.dirname(SANDBOX_OPENCODE_CONFIG_PATH);
1297
-
1298
- if (!fs.existsSync(configDir)) {
1299
- fs.mkdirSync(configDir, { recursive: true });
1300
- }
1301
-
1302
- // Build config with model if available
1303
- const config: Record<string, unknown> = {
1304
- ...SANDBOX_OPENCODE_CONFIG,
1305
- };
1306
-
1307
- // Include model from runner config
1308
- const model = this.runnerConfig.env?.OPENCODE_MODEL;
1309
- if (model) {
1310
- config.model = model;
1311
- }
1312
-
1313
- // Always write to ensure model is up to date
1314
- fs.writeFileSync(SANDBOX_OPENCODE_CONFIG_PATH, JSON.stringify(config, null, 2));
1315
- console.log(`Updated sandbox OpenCode config: ${SANDBOX_OPENCODE_CONFIG_PATH}`);
1316
- }
1317
- }