@agentmeshhq/agent 0.1.12 → 0.1.14

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 (117) hide show
  1. package/dist/__tests__/loader.test.js +44 -1
  2. package/dist/__tests__/loader.test.js.map +1 -1
  3. package/dist/__tests__/runner.test.js.map +1 -1
  4. package/dist/__tests__/sandbox.test.d.ts +1 -0
  5. package/dist/__tests__/sandbox.test.js +362 -0
  6. package/dist/__tests__/sandbox.test.js.map +1 -0
  7. package/dist/__tests__/watchdog.test.d.ts +1 -0
  8. package/dist/__tests__/watchdog.test.js +290 -0
  9. package/dist/__tests__/watchdog.test.js.map +1 -0
  10. package/dist/cli/attach.js +20 -1
  11. package/dist/cli/attach.js.map +1 -1
  12. package/dist/cli/build.js +8 -2
  13. package/dist/cli/build.js.map +1 -1
  14. package/dist/cli/context.js.map +1 -1
  15. package/dist/cli/deploy.js +1 -1
  16. package/dist/cli/deploy.js.map +1 -1
  17. package/dist/cli/inbox.d.ts +5 -0
  18. package/dist/cli/inbox.js +123 -0
  19. package/dist/cli/inbox.js.map +1 -0
  20. package/dist/cli/index.js +5 -1
  21. package/dist/cli/index.js.map +1 -1
  22. package/dist/cli/init.js +1 -1
  23. package/dist/cli/init.js.map +1 -1
  24. package/dist/cli/issue.d.ts +42 -0
  25. package/dist/cli/issue.js +297 -0
  26. package/dist/cli/issue.js.map +1 -0
  27. package/dist/cli/list.js +3 -3
  28. package/dist/cli/list.js.map +1 -1
  29. package/dist/cli/local.js +5 -3
  30. package/dist/cli/local.js.map +1 -1
  31. package/dist/cli/migrate.js +1 -1
  32. package/dist/cli/migrate.js.map +1 -1
  33. package/dist/cli/nudge.js +16 -3
  34. package/dist/cli/nudge.js.map +1 -1
  35. package/dist/cli/ready.d.ts +5 -0
  36. package/dist/cli/ready.js +131 -0
  37. package/dist/cli/ready.js.map +1 -0
  38. package/dist/cli/restart.js.map +1 -1
  39. package/dist/cli/slack.js +1 -1
  40. package/dist/cli/slack.js.map +1 -1
  41. package/dist/cli/start.d.ts +8 -0
  42. package/dist/cli/start.js +9 -0
  43. package/dist/cli/start.js.map +1 -1
  44. package/dist/cli/stop.js +13 -5
  45. package/dist/cli/stop.js.map +1 -1
  46. package/dist/cli/sync.d.ts +8 -0
  47. package/dist/cli/sync.js +154 -0
  48. package/dist/cli/sync.js.map +1 -0
  49. package/dist/cli/test.js +1 -1
  50. package/dist/cli/test.js.map +1 -1
  51. package/dist/cli/token.js +2 -2
  52. package/dist/cli/token.js.map +1 -1
  53. package/dist/config/loader.d.ts +5 -1
  54. package/dist/config/loader.js +27 -2
  55. package/dist/config/loader.js.map +1 -1
  56. package/dist/config/schema.d.ts +13 -0
  57. package/dist/core/daemon.d.ts +50 -0
  58. package/dist/core/daemon.js +445 -11
  59. package/dist/core/daemon.js.map +1 -1
  60. package/dist/core/injector.d.ts +2 -2
  61. package/dist/core/injector.js +23 -4
  62. package/dist/core/injector.js.map +1 -1
  63. package/dist/core/issue-cache.d.ts +44 -0
  64. package/dist/core/issue-cache.js +75 -0
  65. package/dist/core/issue-cache.js.map +1 -0
  66. package/dist/core/registry.d.ts +5 -0
  67. package/dist/core/registry.js +8 -1
  68. package/dist/core/registry.js.map +1 -1
  69. package/dist/core/runner.d.ts +1 -1
  70. package/dist/core/runner.js +23 -1
  71. package/dist/core/runner.js.map +1 -1
  72. package/dist/core/sandbox.d.ts +138 -0
  73. package/dist/core/sandbox.js +409 -0
  74. package/dist/core/sandbox.js.map +1 -0
  75. package/dist/core/tmux.d.ts +8 -0
  76. package/dist/core/tmux.js +28 -1
  77. package/dist/core/tmux.js.map +1 -1
  78. package/dist/core/watchdog.d.ts +41 -0
  79. package/dist/core/watchdog.js +198 -0
  80. package/dist/core/watchdog.js.map +1 -0
  81. package/dist/core/websocket.js +1 -1
  82. package/dist/core/websocket.js.map +1 -1
  83. package/dist/index.d.ts +5 -5
  84. package/dist/index.js +5 -5
  85. package/dist/index.js.map +1 -1
  86. package/package.json +1 -1
  87. package/src/__tests__/loader.test.ts +52 -4
  88. package/src/__tests__/runner.test.ts +1 -2
  89. package/src/__tests__/sandbox.test.ts +435 -0
  90. package/src/__tests__/watchdog.test.ts +368 -0
  91. package/src/cli/attach.ts +22 -1
  92. package/src/cli/build.ts +12 -4
  93. package/src/cli/context.ts +0 -1
  94. package/src/cli/deploy.ts +7 -5
  95. package/src/cli/index.ts +8 -1
  96. package/src/cli/init.ts +7 -19
  97. package/src/cli/list.ts +6 -10
  98. package/src/cli/local.ts +21 -12
  99. package/src/cli/migrate.ts +6 -4
  100. package/src/cli/nudge.ts +29 -14
  101. package/src/cli/restart.ts +1 -1
  102. package/src/cli/slack.ts +16 -15
  103. package/src/cli/start.ts +14 -0
  104. package/src/cli/stop.ts +14 -5
  105. package/src/cli/test.ts +5 -3
  106. package/src/cli/token.ts +4 -4
  107. package/src/config/loader.ts +29 -2
  108. package/src/config/schema.ts +14 -0
  109. package/src/core/daemon.ts +540 -17
  110. package/src/core/injector.ts +27 -4
  111. package/src/core/registry.ts +14 -1
  112. package/src/core/runner.ts +26 -1
  113. package/src/core/sandbox.ts +550 -0
  114. package/src/core/tmux.ts +35 -2
  115. package/src/core/watchdog.ts +238 -0
  116. package/src/core/websocket.ts +2 -2
  117. package/src/index.ts +6 -5
@@ -0,0 +1,435 @@
1
+ import os from "node:os";
2
+ import path from "node:path";
3
+ import { beforeEach, describe, expect, it, vi } from "vitest";
4
+ import { DockerSandbox, type SandboxConfig, type SandboxMountPolicy } from "../core/sandbox.js";
5
+
6
+ // Mock child_process
7
+ vi.mock("node:child_process", () => ({
8
+ execSync: vi.fn(),
9
+ spawn: vi.fn(() => ({
10
+ on: vi.fn(),
11
+ unref: vi.fn(),
12
+ pid: 12345,
13
+ })),
14
+ spawnSync: vi.fn(() => ({
15
+ status: 0,
16
+ stdout: "mock-container-id\n",
17
+ stderr: "",
18
+ })),
19
+ }));
20
+
21
+ // Mock fs
22
+ vi.mock("node:fs", () => ({
23
+ default: {
24
+ existsSync: vi.fn(() => true),
25
+ },
26
+ }));
27
+
28
+ describe("DockerSandbox", () => {
29
+ const defaultConfig: SandboxConfig = {
30
+ agentName: "test-agent",
31
+ image: "agentmesh/agent-sandbox:latest",
32
+ workspacePath: "/tmp/workspace",
33
+ };
34
+
35
+ beforeEach(() => {
36
+ vi.clearAllMocks();
37
+ });
38
+
39
+ describe("constructor", () => {
40
+ it("should create sandbox with default values", () => {
41
+ const sandbox = new DockerSandbox(defaultConfig);
42
+
43
+ expect(sandbox.getContainerName()).toMatch(/^agentmesh-sandbox-test-agent-[a-f0-9]{8}$/);
44
+ });
45
+
46
+ it("should merge config with defaults", () => {
47
+ const config: SandboxConfig = {
48
+ ...defaultConfig,
49
+ cpuLimit: "0.5",
50
+ memoryLimit: "1g",
51
+ };
52
+
53
+ const sandbox = new DockerSandbox(config);
54
+ expect(sandbox).toBeDefined();
55
+ });
56
+ });
57
+
58
+ describe("validateMountPolicy", () => {
59
+ it("should deny sensitive paths by default", () => {
60
+ const sandbox = new DockerSandbox({
61
+ ...defaultConfig,
62
+ workspacePath: path.join(os.homedir(), ".ssh"),
63
+ });
64
+
65
+ expect(() => sandbox.validateMountPolicy()).toThrow(/Mount denied.*\.ssh/);
66
+ });
67
+
68
+ it("should deny ~/.gnupg", () => {
69
+ const sandbox = new DockerSandbox({
70
+ ...defaultConfig,
71
+ workspacePath: path.join(os.homedir(), ".gnupg"),
72
+ });
73
+
74
+ expect(() => sandbox.validateMountPolicy()).toThrow(/Mount denied.*\.gnupg/);
75
+ });
76
+
77
+ it("should deny ~/.aws", () => {
78
+ const sandbox = new DockerSandbox({
79
+ ...defaultConfig,
80
+ workspacePath: path.join(os.homedir(), ".aws"),
81
+ });
82
+
83
+ expect(() => sandbox.validateMountPolicy()).toThrow(/Mount denied.*\.aws/);
84
+ });
85
+
86
+ it("should deny ~/.config/gcloud", () => {
87
+ const sandbox = new DockerSandbox({
88
+ ...defaultConfig,
89
+ workspacePath: path.join(os.homedir(), ".config/gcloud"),
90
+ });
91
+
92
+ expect(() => sandbox.validateMountPolicy()).toThrow(/Mount denied.*gcloud/);
93
+ });
94
+
95
+ it("should deny ~/.kube", () => {
96
+ const sandbox = new DockerSandbox({
97
+ ...defaultConfig,
98
+ workspacePath: path.join(os.homedir(), ".kube"),
99
+ });
100
+
101
+ expect(() => sandbox.validateMountPolicy()).toThrow(/Mount denied.*\.kube/);
102
+ });
103
+
104
+ it("should deny ~/.docker", () => {
105
+ const sandbox = new DockerSandbox({
106
+ ...defaultConfig,
107
+ workspacePath: path.join(os.homedir(), ".docker"),
108
+ });
109
+
110
+ expect(() => sandbox.validateMountPolicy()).toThrow(/Mount denied.*\.docker/);
111
+ });
112
+
113
+ it("should deny /var/run/docker.sock", () => {
114
+ const sandbox = new DockerSandbox({
115
+ ...defaultConfig,
116
+ workspacePath: "/var/run/docker.sock",
117
+ });
118
+
119
+ expect(() => sandbox.validateMountPolicy()).toThrow(/Mount denied.*docker\.sock/);
120
+ });
121
+
122
+ it("should allow regular workspace paths", () => {
123
+ const sandbox = new DockerSandbox({
124
+ ...defaultConfig,
125
+ workspacePath: "/tmp/workspace",
126
+ });
127
+
128
+ expect(() => sandbox.validateMountPolicy()).not.toThrow();
129
+ });
130
+
131
+ it("should enforce custom allowed paths when specified", () => {
132
+ const sandbox = new DockerSandbox({
133
+ ...defaultConfig,
134
+ workspacePath: "/home/user/random",
135
+ });
136
+
137
+ const policy: SandboxMountPolicy = {
138
+ allowedPaths: ["/home/user/allowed-only"],
139
+ deniedPaths: [],
140
+ };
141
+
142
+ expect(() => sandbox.validateMountPolicy(policy)).toThrow(/not in allowed paths/);
143
+ });
144
+
145
+ it("should allow path within custom allowed paths", () => {
146
+ const sandbox = new DockerSandbox({
147
+ ...defaultConfig,
148
+ workspacePath: "/home/user/projects/myrepo",
149
+ });
150
+
151
+ const policy: SandboxMountPolicy = {
152
+ allowedPaths: ["/home/user/projects"],
153
+ deniedPaths: [],
154
+ };
155
+
156
+ expect(() => sandbox.validateMountPolicy(policy)).not.toThrow();
157
+ });
158
+ });
159
+
160
+ describe("checkDockerAvailable", () => {
161
+ it("should return true when docker is available", async () => {
162
+ const { spawnSync } = await import("node:child_process");
163
+ vi.mocked(spawnSync).mockReturnValueOnce({
164
+ status: 0,
165
+ stdout: "Docker version 24.0.0",
166
+ stderr: "",
167
+ pid: 1,
168
+ output: [],
169
+ signal: null,
170
+ });
171
+
172
+ expect(DockerSandbox.checkDockerAvailable()).toBe(true);
173
+ });
174
+
175
+ it("should return false when docker is not available", async () => {
176
+ const { spawnSync } = await import("node:child_process");
177
+ vi.mocked(spawnSync).mockReturnValueOnce({
178
+ status: 1,
179
+ stdout: "",
180
+ stderr: "docker: command not found",
181
+ pid: 1,
182
+ output: [],
183
+ signal: null,
184
+ });
185
+
186
+ expect(DockerSandbox.checkDockerAvailable()).toBe(false);
187
+ });
188
+ });
189
+
190
+ describe("listAll", () => {
191
+ it("should parse docker ps output correctly", async () => {
192
+ const { spawnSync } = await import("node:child_process");
193
+ vi.mocked(spawnSync).mockReturnValueOnce({
194
+ status: 0,
195
+ stdout:
196
+ "abc123|agentmesh-sandbox-agent1-12345678|Up 5 minutes|agentmesh/agent-sandbox:latest\ndef456|agentmesh-sandbox-agent2-87654321|Exited (0)|agentmesh/agent-sandbox:latest",
197
+ stderr: "",
198
+ pid: 1,
199
+ output: [],
200
+ signal: null,
201
+ });
202
+
203
+ const containers = DockerSandbox.listAll();
204
+
205
+ expect(containers).toHaveLength(2);
206
+ expect(containers[0]).toEqual({
207
+ id: "abc123",
208
+ name: "agentmesh-sandbox-agent1-12345678",
209
+ status: "Up 5 minutes",
210
+ image: "agentmesh/agent-sandbox:latest",
211
+ });
212
+ });
213
+
214
+ it("should return empty array when no containers found", async () => {
215
+ const { spawnSync } = await import("node:child_process");
216
+ vi.mocked(spawnSync).mockReturnValueOnce({
217
+ status: 0,
218
+ stdout: "",
219
+ stderr: "",
220
+ pid: 1,
221
+ output: [],
222
+ signal: null,
223
+ });
224
+
225
+ const containers = DockerSandbox.listAll();
226
+ expect(containers).toEqual([]);
227
+ });
228
+ });
229
+
230
+ describe("findExisting", () => {
231
+ it("should find existing container for agent", async () => {
232
+ const { spawnSync } = await import("node:child_process");
233
+ vi.mocked(spawnSync).mockReturnValueOnce({
234
+ status: 0,
235
+ stdout: "abc123def456\n",
236
+ stderr: "",
237
+ pid: 1,
238
+ output: [],
239
+ signal: null,
240
+ });
241
+
242
+ const containerId = DockerSandbox.findExisting("test-agent");
243
+ expect(containerId).toBe("abc123def456");
244
+ });
245
+
246
+ it("should return null when no container exists", async () => {
247
+ const { spawnSync } = await import("node:child_process");
248
+ vi.mocked(spawnSync).mockReturnValueOnce({
249
+ status: 0,
250
+ stdout: "\n",
251
+ stderr: "",
252
+ pid: 1,
253
+ output: [],
254
+ signal: null,
255
+ });
256
+
257
+ const containerId = DockerSandbox.findExisting("nonexistent");
258
+ expect(containerId).toBeNull();
259
+ });
260
+ });
261
+
262
+ describe("container lifecycle", () => {
263
+ it("should build correct docker run arguments", async () => {
264
+ const { spawnSync } = await import("node:child_process");
265
+ const mockSpawnSync = vi.mocked(spawnSync);
266
+
267
+ // Mock image inspect (image exists)
268
+ mockSpawnSync.mockReturnValueOnce({
269
+ status: 0,
270
+ stdout: "{}",
271
+ stderr: "",
272
+ pid: 1,
273
+ output: [],
274
+ signal: null,
275
+ });
276
+
277
+ // Mock docker run
278
+ mockSpawnSync.mockReturnValueOnce({
279
+ status: 0,
280
+ stdout: "container-id-12345\n",
281
+ stderr: "",
282
+ pid: 1,
283
+ output: [],
284
+ signal: null,
285
+ });
286
+
287
+ const sandbox = new DockerSandbox({
288
+ agentName: "test",
289
+ image: "agentmesh/agent-sandbox:latest",
290
+ workspacePath: "/tmp/workspace",
291
+ cpuLimit: "2",
292
+ memoryLimit: "4g",
293
+ env: {
294
+ AGENT_TOKEN: "test-token",
295
+ },
296
+ });
297
+
298
+ await sandbox.pullImage();
299
+ await sandbox.start();
300
+
301
+ // Find the docker run call
302
+ const runCall = mockSpawnSync.mock.calls.find(
303
+ (call) => call[0] === "docker" && call[1]?.[0] === "run",
304
+ );
305
+
306
+ expect(runCall).toBeDefined();
307
+ const args = runCall![1] as string[];
308
+
309
+ // Check key arguments
310
+ expect(args).toContain("-d");
311
+ expect(args).toContain("--cpus");
312
+ expect(args).toContain("2");
313
+ expect(args).toContain("--memory");
314
+ expect(args).toContain("4g");
315
+ expect(args).toContain("--user");
316
+ expect(args).toContain("1000:1000");
317
+ expect(args).toContain("-e");
318
+ expect(args).toContain("AGENT_TOKEN=test-token");
319
+ });
320
+
321
+ it("should get container status", async () => {
322
+ const { spawnSync } = await import("node:child_process");
323
+ vi.mocked(spawnSync).mockReturnValueOnce({
324
+ status: 0,
325
+ stdout: "true|running|healthy",
326
+ stderr: "",
327
+ pid: 1,
328
+ output: [],
329
+ signal: null,
330
+ });
331
+
332
+ const sandbox = new DockerSandbox(defaultConfig);
333
+ // @ts-expect-error - accessing private property for testing
334
+ sandbox.containerId = "test-container";
335
+
336
+ const status = sandbox.getStatus();
337
+
338
+ expect(status).toEqual({
339
+ running: true,
340
+ status: "running",
341
+ health: "healthy",
342
+ });
343
+ });
344
+
345
+ it("should return not found status when container not started", async () => {
346
+ const { spawnSync } = await import("node:child_process");
347
+ vi.mocked(spawnSync).mockReturnValueOnce({
348
+ status: 1, // docker inspect fails for non-existent container
349
+ stdout: "",
350
+ stderr: "Error: No such container",
351
+ pid: 1,
352
+ output: [],
353
+ signal: null,
354
+ });
355
+
356
+ const sandbox = new DockerSandbox(defaultConfig);
357
+ const status = sandbox.getStatus();
358
+
359
+ expect(status).toEqual({
360
+ running: false,
361
+ status: "not found",
362
+ });
363
+ });
364
+ });
365
+
366
+ describe("resource limits", () => {
367
+ it("should apply default CPU limit of 1", () => {
368
+ const sandbox = new DockerSandbox(defaultConfig);
369
+ // The default is applied in constructor
370
+ expect(sandbox).toBeDefined();
371
+ });
372
+
373
+ it("should apply default memory limit of 2g", () => {
374
+ const sandbox = new DockerSandbox(defaultConfig);
375
+ expect(sandbox).toBeDefined();
376
+ });
377
+
378
+ it("should allow custom resource limits", () => {
379
+ const sandbox = new DockerSandbox({
380
+ ...defaultConfig,
381
+ cpuLimit: "0.5",
382
+ memoryLimit: "512m",
383
+ });
384
+ expect(sandbox).toBeDefined();
385
+ });
386
+ });
387
+
388
+ describe("serve mode", () => {
389
+ it("should configure serve mode with port", async () => {
390
+ const { spawnSync } = await import("node:child_process");
391
+ const mockSpawnSync = vi.mocked(spawnSync);
392
+
393
+ // Mock image inspect
394
+ mockSpawnSync.mockReturnValueOnce({
395
+ status: 0,
396
+ stdout: "{}",
397
+ stderr: "",
398
+ pid: 1,
399
+ output: [],
400
+ signal: null,
401
+ });
402
+
403
+ // Mock docker run
404
+ mockSpawnSync.mockReturnValueOnce({
405
+ status: 0,
406
+ stdout: "container-id\n",
407
+ stderr: "",
408
+ pid: 1,
409
+ output: [],
410
+ signal: null,
411
+ });
412
+
413
+ const sandbox = new DockerSandbox({
414
+ ...defaultConfig,
415
+ serveMode: true,
416
+ servePort: 3001,
417
+ });
418
+
419
+ await sandbox.pullImage();
420
+ await sandbox.start();
421
+
422
+ const runCall = mockSpawnSync.mock.calls.find(
423
+ (call) => call[0] === "docker" && call[1]?.[0] === "run",
424
+ );
425
+
426
+ expect(runCall).toBeDefined();
427
+ const args = runCall![1] as string[];
428
+
429
+ expect(args).toContain("-p");
430
+ expect(args).toContain("3001:3001");
431
+ expect(args).toContain("opencode");
432
+ expect(args).toContain("serve");
433
+ });
434
+ });
435
+ });