opencode-swarm-plugin 0.12.30 → 0.13.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 (48) hide show
  1. package/.beads/issues.jsonl +204 -10
  2. package/.opencode/skills/tdd/SKILL.md +182 -0
  3. package/README.md +165 -17
  4. package/bin/swarm.ts +120 -31
  5. package/bun.lock +23 -0
  6. package/dist/index.js +4020 -438
  7. package/dist/pglite.data +0 -0
  8. package/dist/pglite.wasm +0 -0
  9. package/dist/plugin.js +4008 -514
  10. package/examples/commands/swarm.md +114 -19
  11. package/examples/skills/beads-workflow/SKILL.md +75 -28
  12. package/examples/skills/swarm-coordination/SKILL.md +92 -1
  13. package/global-skills/testing-patterns/SKILL.md +430 -0
  14. package/global-skills/testing-patterns/references/dependency-breaking-catalog.md +586 -0
  15. package/package.json +11 -5
  16. package/src/index.ts +44 -5
  17. package/src/streams/agent-mail.test.ts +777 -0
  18. package/src/streams/agent-mail.ts +535 -0
  19. package/src/streams/debug.test.ts +500 -0
  20. package/src/streams/debug.ts +629 -0
  21. package/src/streams/effect/ask.integration.test.ts +314 -0
  22. package/src/streams/effect/ask.ts +202 -0
  23. package/src/streams/effect/cursor.integration.test.ts +418 -0
  24. package/src/streams/effect/cursor.ts +288 -0
  25. package/src/streams/effect/deferred.test.ts +357 -0
  26. package/src/streams/effect/deferred.ts +445 -0
  27. package/src/streams/effect/index.ts +17 -0
  28. package/src/streams/effect/layers.ts +73 -0
  29. package/src/streams/effect/lock.test.ts +385 -0
  30. package/src/streams/effect/lock.ts +399 -0
  31. package/src/streams/effect/mailbox.test.ts +260 -0
  32. package/src/streams/effect/mailbox.ts +318 -0
  33. package/src/streams/events.test.ts +628 -0
  34. package/src/streams/events.ts +214 -0
  35. package/src/streams/index.test.ts +229 -0
  36. package/src/streams/index.ts +492 -0
  37. package/src/streams/migrations.test.ts +355 -0
  38. package/src/streams/migrations.ts +269 -0
  39. package/src/streams/projections.test.ts +611 -0
  40. package/src/streams/projections.ts +302 -0
  41. package/src/streams/store.integration.test.ts +548 -0
  42. package/src/streams/store.ts +546 -0
  43. package/src/streams/swarm-mail.ts +552 -0
  44. package/src/swarm-mail.integration.test.ts +970 -0
  45. package/src/swarm-mail.ts +739 -0
  46. package/src/swarm.ts +84 -59
  47. package/src/tool-availability.ts +35 -2
  48. package/global-skills/mcp-tool-authoring/SKILL.md +0 -695
@@ -0,0 +1,970 @@
1
+ /**
2
+ * Integration tests for swarm-mail.ts (embedded implementation)
3
+ *
4
+ * These tests run against the embedded PGLite database.
5
+ * No external server required - everything runs in-process.
6
+ *
7
+ * Run with: pnpm test:integration
8
+ */
9
+
10
+ import { randomUUID } from "node:crypto";
11
+ import { describe, it, expect, beforeEach, afterEach } from "vitest";
12
+ import { resetDatabase, closeDatabase, getDatabase } from "./streams/index";
13
+ import {
14
+ swarmmail_init,
15
+ swarmmail_send,
16
+ swarmmail_inbox,
17
+ swarmmail_read_message,
18
+ swarmmail_reserve,
19
+ swarmmail_release,
20
+ swarmmail_ack,
21
+ swarmmail_health,
22
+ clearSessionState,
23
+ } from "./swarm-mail";
24
+
25
+ // ============================================================================
26
+ // Test Configuration
27
+ // ============================================================================
28
+
29
+ /** Generate unique test database path per test run */
30
+ function testDbPath(prefix = "swarm-mail"): string {
31
+ return `/tmp/${prefix}-${randomUUID()}`;
32
+ }
33
+
34
+ /** Track paths created during test for cleanup */
35
+ let testPaths: string[] = [];
36
+
37
+ function trackPath(path: string): string {
38
+ testPaths.push(path);
39
+ return path;
40
+ }
41
+
42
+ let TEST_DB_PATH: string;
43
+
44
+ /**
45
+ * Generate a unique test context to avoid state collisions between tests
46
+ */
47
+ function createTestContext() {
48
+ const id = `test-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
49
+ return {
50
+ sessionID: id,
51
+ };
52
+ }
53
+
54
+ /**
55
+ * Mock tool context
56
+ */
57
+ interface MockToolContext {
58
+ sessionID: string;
59
+ }
60
+
61
+ /**
62
+ * Helper to execute tool and parse JSON response
63
+ */
64
+ async function executeTool<T>(
65
+ tool: { execute: (args: unknown, ctx: unknown) => Promise<string> },
66
+ args: unknown,
67
+ ctx: MockToolContext,
68
+ ): Promise<T> {
69
+ const result = await tool.execute(args, ctx);
70
+ return JSON.parse(result) as T;
71
+ }
72
+
73
+ // ============================================================================
74
+ // Test Lifecycle Hooks
75
+ // ============================================================================
76
+
77
+ beforeEach(async () => {
78
+ testPaths = [];
79
+ TEST_DB_PATH = trackPath(testDbPath());
80
+ await resetDatabase(TEST_DB_PATH);
81
+ });
82
+
83
+ afterEach(async () => {
84
+ // Clean up all test databases
85
+ for (const path of testPaths) {
86
+ try {
87
+ // Wipe all data before closing
88
+ const db = await getDatabase(path);
89
+ await db.exec(`
90
+ DELETE FROM message_recipients;
91
+ DELETE FROM messages;
92
+ DELETE FROM reservations;
93
+ DELETE FROM agents;
94
+ DELETE FROM events;
95
+ DELETE FROM locks;
96
+ DELETE FROM cursors;
97
+ DELETE FROM deferred;
98
+ `);
99
+ } catch {
100
+ // Ignore errors during cleanup
101
+ }
102
+ await closeDatabase(path);
103
+ }
104
+ testPaths = [];
105
+ });
106
+
107
+ // ============================================================================
108
+ // Health Check Tests
109
+ // ============================================================================
110
+
111
+ describe("swarm-mail integration (embedded)", () => {
112
+ describe("swarmmail_health", () => {
113
+ it("returns healthy when database is initialized", async () => {
114
+ const ctx = createTestContext();
115
+
116
+ const result = await executeTool<{
117
+ healthy: boolean;
118
+ database: string;
119
+ stats: { events: number; agents: number; messages: number };
120
+ }>(swarmmail_health, {}, ctx);
121
+
122
+ expect(result.healthy).toBe(true);
123
+ expect(result.database).toBeTruthy();
124
+ expect(result.stats).toBeDefined();
125
+ });
126
+
127
+ it("includes session info when initialized", async () => {
128
+ const ctx = createTestContext();
129
+
130
+ // Initialize session
131
+ await executeTool(
132
+ swarmmail_init,
133
+ { project_path: TEST_DB_PATH, agent_name: "HealthAgent" },
134
+ ctx,
135
+ );
136
+
137
+ const result = await executeTool<{
138
+ healthy: boolean;
139
+ session: {
140
+ agent_name: string;
141
+ project_key: string;
142
+ reservations: number;
143
+ };
144
+ }>(swarmmail_health, {}, ctx);
145
+
146
+ expect(result.healthy).toBe(true);
147
+ expect(result.session).toBeDefined();
148
+ expect(result.session.agent_name).toBe("HealthAgent");
149
+ expect(result.session.reservations).toBe(0);
150
+
151
+ clearSessionState(ctx.sessionID);
152
+ });
153
+ });
154
+
155
+ // ============================================================================
156
+ // Initialization Tests
157
+ // ============================================================================
158
+
159
+ describe("swarmmail_init", () => {
160
+ it("creates agent and returns name and project_key", async () => {
161
+ const ctx = createTestContext();
162
+
163
+ const result = await executeTool<{
164
+ agent_name: string;
165
+ project_key: string;
166
+ message: string;
167
+ }>(swarmmail_init, { project_path: TEST_DB_PATH }, ctx);
168
+
169
+ expect(result.agent_name).toBeTruthy();
170
+ expect(result.project_key).toBe(TEST_DB_PATH);
171
+ expect(result.message).toContain(result.agent_name);
172
+
173
+ clearSessionState(ctx.sessionID);
174
+ });
175
+
176
+ it("generates unique agent name when not provided", async () => {
177
+ const ctx1 = createTestContext();
178
+ const ctx2 = createTestContext();
179
+
180
+ const result1 = await executeTool<{ agent_name: string }>(
181
+ swarmmail_init,
182
+ { project_path: TEST_DB_PATH },
183
+ ctx1,
184
+ );
185
+
186
+ const result2 = await executeTool<{ agent_name: string }>(
187
+ swarmmail_init,
188
+ { project_path: TEST_DB_PATH },
189
+ ctx2,
190
+ );
191
+
192
+ // Both should have adjective+noun style names
193
+ expect(result1.agent_name).toMatch(/^[A-Z][a-z]+[A-Z][a-z]+$/);
194
+ expect(result2.agent_name).toMatch(/^[A-Z][a-z]+[A-Z][a-z]+$/);
195
+ expect(result1.agent_name).not.toBe(result2.agent_name);
196
+
197
+ clearSessionState(ctx1.sessionID);
198
+ clearSessionState(ctx2.sessionID);
199
+ });
200
+
201
+ it("uses provided agent name when specified", async () => {
202
+ const ctx = createTestContext();
203
+ const customName = "BlueLake";
204
+
205
+ const result = await executeTool<{ agent_name: string }>(
206
+ swarmmail_init,
207
+ { project_path: TEST_DB_PATH, agent_name: customName },
208
+ ctx,
209
+ );
210
+
211
+ expect(result.agent_name).toBe(customName);
212
+
213
+ clearSessionState(ctx.sessionID);
214
+ });
215
+
216
+ it("returns existing session if already initialized", async () => {
217
+ const ctx = createTestContext();
218
+
219
+ const result1 = await executeTool<{
220
+ agent_name: string;
221
+ already_initialized?: boolean;
222
+ }>(swarmmail_init, { project_path: TEST_DB_PATH }, ctx);
223
+
224
+ const result2 = await executeTool<{
225
+ agent_name: string;
226
+ already_initialized?: boolean;
227
+ }>(swarmmail_init, { project_path: TEST_DB_PATH }, ctx);
228
+
229
+ expect(result1.agent_name).toBe(result2.agent_name);
230
+ expect(result2.already_initialized).toBe(true);
231
+
232
+ clearSessionState(ctx.sessionID);
233
+ });
234
+ });
235
+
236
+ // ============================================================================
237
+ // Messaging Tests
238
+ // ============================================================================
239
+
240
+ describe("swarmmail_send", () => {
241
+ it("sends message to another agent", async () => {
242
+ const senderCtx = createTestContext();
243
+ const recipientCtx = createTestContext();
244
+
245
+ // Initialize both agents
246
+ await executeTool<{ agent_name: string }>(
247
+ swarmmail_init,
248
+ { project_path: TEST_DB_PATH, agent_name: "Sender" },
249
+ senderCtx,
250
+ );
251
+
252
+ const recipient = await executeTool<{ agent_name: string }>(
253
+ swarmmail_init,
254
+ { project_path: TEST_DB_PATH, agent_name: "Recipient" },
255
+ recipientCtx,
256
+ );
257
+
258
+ // Send message
259
+ const result = await executeTool<{
260
+ success: boolean;
261
+ message_id: number;
262
+ thread_id?: string;
263
+ recipient_count: number;
264
+ }>(
265
+ swarmmail_send,
266
+ {
267
+ to: [recipient.agent_name],
268
+ subject: "Test message",
269
+ body: "This is a test message body",
270
+ thread_id: "bd-test-123",
271
+ importance: "normal",
272
+ },
273
+ senderCtx,
274
+ );
275
+
276
+ expect(result.success).toBe(true);
277
+ expect(result.message_id).toBeGreaterThan(0);
278
+ expect(result.thread_id).toBe("bd-test-123");
279
+ expect(result.recipient_count).toBe(1);
280
+
281
+ clearSessionState(senderCtx.sessionID);
282
+ clearSessionState(recipientCtx.sessionID);
283
+ });
284
+
285
+ it("sends urgent message with ack_required", async () => {
286
+ const senderCtx = createTestContext();
287
+ const recipientCtx = createTestContext();
288
+
289
+ await executeTool(
290
+ swarmmail_init,
291
+ { project_path: TEST_DB_PATH, agent_name: "UrgentSender" },
292
+ senderCtx,
293
+ );
294
+
295
+ const recipient = await executeTool<{ agent_name: string }>(
296
+ swarmmail_init,
297
+ { project_path: TEST_DB_PATH, agent_name: "UrgentRecipient" },
298
+ recipientCtx,
299
+ );
300
+
301
+ const result = await executeTool<{
302
+ success: boolean;
303
+ message_id: number;
304
+ }>(
305
+ swarmmail_send,
306
+ {
307
+ to: [recipient.agent_name],
308
+ subject: "Urgent: Action required",
309
+ body: "Please acknowledge this message",
310
+ importance: "urgent",
311
+ ack_required: true,
312
+ },
313
+ senderCtx,
314
+ );
315
+
316
+ expect(result.success).toBe(true);
317
+ expect(result.message_id).toBeGreaterThan(0);
318
+
319
+ clearSessionState(senderCtx.sessionID);
320
+ clearSessionState(recipientCtx.sessionID);
321
+ });
322
+
323
+ it("returns error when not initialized", async () => {
324
+ const ctx = createTestContext();
325
+
326
+ const result = await executeTool<{ error?: string }>(
327
+ swarmmail_send,
328
+ {
329
+ to: ["SomeAgent"],
330
+ subject: "Test",
331
+ body: "Body",
332
+ },
333
+ ctx,
334
+ );
335
+
336
+ expect(result.error).toContain("not initialized");
337
+
338
+ clearSessionState(ctx.sessionID);
339
+ });
340
+ });
341
+
342
+ // ============================================================================
343
+ // Inbox Tests
344
+ // ============================================================================
345
+
346
+ describe("swarmmail_inbox", () => {
347
+ it("fetches messages without bodies by default (context-safe)", async () => {
348
+ const senderCtx = createTestContext();
349
+ const recipientCtx = createTestContext();
350
+
351
+ await executeTool<{ agent_name: string }>(
352
+ swarmmail_init,
353
+ { project_path: TEST_DB_PATH, agent_name: "InboxSender" },
354
+ senderCtx,
355
+ );
356
+
357
+ const recipient = await executeTool<{ agent_name: string }>(
358
+ swarmmail_init,
359
+ { project_path: TEST_DB_PATH, agent_name: "InboxRecipient" },
360
+ recipientCtx,
361
+ );
362
+
363
+ // Send a message
364
+ await executeTool(
365
+ swarmmail_send,
366
+ {
367
+ to: [recipient.agent_name],
368
+ subject: "Inbox test message",
369
+ body: "This body should NOT be included by default",
370
+ },
371
+ senderCtx,
372
+ );
373
+
374
+ // Fetch inbox
375
+ const result = await executeTool<{
376
+ messages: Array<{
377
+ id: number;
378
+ from: string;
379
+ subject: string;
380
+ body?: string;
381
+ }>;
382
+ total: number;
383
+ note: string;
384
+ }>(swarmmail_inbox, {}, recipientCtx);
385
+
386
+ expect(result.messages.length).toBeGreaterThan(0);
387
+ const testMsg = result.messages.find(
388
+ (m) => m.subject === "Inbox test message",
389
+ );
390
+ expect(testMsg).toBeDefined();
391
+ expect(testMsg?.from).toBe("InboxSender");
392
+ // Body should NOT be included
393
+ expect(testMsg?.body).toBeUndefined();
394
+ expect(result.note).toContain("swarmmail_read_message");
395
+
396
+ clearSessionState(senderCtx.sessionID);
397
+ clearSessionState(recipientCtx.sessionID);
398
+ });
399
+
400
+ it("enforces MAX_INBOX_LIMIT (5) constraint", async () => {
401
+ const senderCtx = createTestContext();
402
+ const recipientCtx = createTestContext();
403
+
404
+ await executeTool<{ agent_name: string }>(
405
+ swarmmail_init,
406
+ { project_path: TEST_DB_PATH, agent_name: "LimitSender" },
407
+ senderCtx,
408
+ );
409
+
410
+ const recipient = await executeTool<{ agent_name: string }>(
411
+ swarmmail_init,
412
+ { project_path: TEST_DB_PATH, agent_name: "LimitRecipient" },
413
+ recipientCtx,
414
+ );
415
+
416
+ // Send 8 messages (more than limit)
417
+ for (let i = 0; i < 8; i++) {
418
+ await executeTool(
419
+ swarmmail_send,
420
+ {
421
+ to: [recipient.agent_name],
422
+ subject: `Limit test message ${i}`,
423
+ body: `Message body ${i}`,
424
+ },
425
+ senderCtx,
426
+ );
427
+ }
428
+
429
+ // Request 10 messages (should be capped at 5)
430
+ const result = await executeTool<{
431
+ messages: Array<{ id: number }>;
432
+ }>(swarmmail_inbox, { limit: 10 }, recipientCtx);
433
+
434
+ // Should be capped at 5
435
+ expect(result.messages.length).toBeLessThanOrEqual(5);
436
+
437
+ clearSessionState(senderCtx.sessionID);
438
+ clearSessionState(recipientCtx.sessionID);
439
+ });
440
+
441
+ it("filters urgent messages when urgent_only is true", async () => {
442
+ const senderCtx = createTestContext();
443
+ const recipientCtx = createTestContext();
444
+
445
+ await executeTool<{ agent_name: string }>(
446
+ swarmmail_init,
447
+ { project_path: TEST_DB_PATH, agent_name: "UrgentFilterSender" },
448
+ senderCtx,
449
+ );
450
+
451
+ const recipient = await executeTool<{ agent_name: string }>(
452
+ swarmmail_init,
453
+ { project_path: TEST_DB_PATH, agent_name: "UrgentFilterRecipient" },
454
+ recipientCtx,
455
+ );
456
+
457
+ // Send normal and urgent messages
458
+ await executeTool(
459
+ swarmmail_send,
460
+ {
461
+ to: [recipient.agent_name],
462
+ subject: "Normal message",
463
+ body: "Not urgent",
464
+ importance: "normal",
465
+ },
466
+ senderCtx,
467
+ );
468
+
469
+ await executeTool(
470
+ swarmmail_send,
471
+ {
472
+ to: [recipient.agent_name],
473
+ subject: "Urgent message",
474
+ body: "Very urgent!",
475
+ importance: "urgent",
476
+ },
477
+ senderCtx,
478
+ );
479
+
480
+ // Fetch only urgent messages
481
+ const result = await executeTool<{
482
+ messages: Array<{ subject: string; importance: string }>;
483
+ }>(swarmmail_inbox, { urgent_only: true }, recipientCtx);
484
+
485
+ // All returned messages should be urgent
486
+ for (const msg of result.messages) {
487
+ expect(msg.importance).toBe("urgent");
488
+ }
489
+ expect(result.messages.some((m) => m.subject === "Urgent message")).toBe(
490
+ true,
491
+ );
492
+
493
+ clearSessionState(senderCtx.sessionID);
494
+ clearSessionState(recipientCtx.sessionID);
495
+ });
496
+ });
497
+
498
+ // ============================================================================
499
+ // Read Message Tests
500
+ // ============================================================================
501
+
502
+ describe("swarmmail_read_message", () => {
503
+ it("returns full message body when reading by ID", async () => {
504
+ const senderCtx = createTestContext();
505
+ const recipientCtx = createTestContext();
506
+
507
+ await executeTool<{ agent_name: string }>(
508
+ swarmmail_init,
509
+ { project_path: TEST_DB_PATH, agent_name: "ReadSender" },
510
+ senderCtx,
511
+ );
512
+
513
+ const recipient = await executeTool<{ agent_name: string }>(
514
+ swarmmail_init,
515
+ { project_path: TEST_DB_PATH, agent_name: "ReadRecipient" },
516
+ recipientCtx,
517
+ );
518
+
519
+ // Send a message
520
+ const sent = await executeTool<{ message_id: number }>(
521
+ swarmmail_send,
522
+ {
523
+ to: [recipient.agent_name],
524
+ subject: "Read test message",
525
+ body: "This message body should be returned",
526
+ },
527
+ senderCtx,
528
+ );
529
+
530
+ // Read the message
531
+ const result = await executeTool<{
532
+ id: number;
533
+ from: string;
534
+ subject: string;
535
+ body: string;
536
+ }>(swarmmail_read_message, { message_id: sent.message_id }, recipientCtx);
537
+
538
+ expect(result.id).toBe(sent.message_id);
539
+ expect(result.from).toBe("ReadSender");
540
+ expect(result.subject).toBe("Read test message");
541
+ expect(result.body).toBe("This message body should be returned");
542
+
543
+ clearSessionState(senderCtx.sessionID);
544
+ clearSessionState(recipientCtx.sessionID);
545
+ });
546
+
547
+ it("returns error when message not found", async () => {
548
+ const ctx = createTestContext();
549
+
550
+ await executeTool(
551
+ swarmmail_init,
552
+ { project_path: TEST_DB_PATH, agent_name: "NotFoundAgent" },
553
+ ctx,
554
+ );
555
+
556
+ const result = await executeTool<{ error?: string }>(
557
+ swarmmail_read_message,
558
+ { message_id: 99999 },
559
+ ctx,
560
+ );
561
+
562
+ expect(result.error).toContain("not found");
563
+
564
+ clearSessionState(ctx.sessionID);
565
+ });
566
+ });
567
+
568
+ // ============================================================================
569
+ // File Reservation Tests
570
+ // ============================================================================
571
+
572
+ describe("swarmmail_reserve", () => {
573
+ it("grants file reservations", async () => {
574
+ const ctx = createTestContext();
575
+
576
+ await executeTool(
577
+ swarmmail_init,
578
+ { project_path: TEST_DB_PATH, agent_name: "ReserveAgent" },
579
+ ctx,
580
+ );
581
+
582
+ const result = await executeTool<{
583
+ granted: Array<{
584
+ id: number;
585
+ path_pattern: string;
586
+ exclusive: boolean;
587
+ }>;
588
+ conflicts?: Array<{ path: string; holders: string[] }>;
589
+ }>(
590
+ swarmmail_reserve,
591
+ {
592
+ paths: ["src/auth/**", "src/config.ts"],
593
+ reason: "bd-test-123: Working on auth",
594
+ exclusive: true,
595
+ ttl_seconds: 3600,
596
+ },
597
+ ctx,
598
+ );
599
+
600
+ expect(result.granted.length).toBe(2);
601
+ expect(result.conflicts).toBeUndefined();
602
+ expect(result.granted[0].exclusive).toBe(true);
603
+
604
+ clearSessionState(ctx.sessionID);
605
+ });
606
+
607
+ it("detects conflicts with exclusive reservations", async () => {
608
+ const agent1Ctx = createTestContext();
609
+ const agent2Ctx = createTestContext();
610
+
611
+ await executeTool(
612
+ swarmmail_init,
613
+ { project_path: TEST_DB_PATH, agent_name: "ConflictAgent1" },
614
+ agent1Ctx,
615
+ );
616
+
617
+ await executeTool(
618
+ swarmmail_init,
619
+ { project_path: TEST_DB_PATH, agent_name: "ConflictAgent2" },
620
+ agent2Ctx,
621
+ );
622
+
623
+ const conflictPath = "src/conflict.ts";
624
+
625
+ // Agent 1 reserves the file
626
+ const result1 = await executeTool<{
627
+ granted: Array<{ id: number }>;
628
+ }>(
629
+ swarmmail_reserve,
630
+ {
631
+ paths: [conflictPath],
632
+ exclusive: true,
633
+ },
634
+ agent1Ctx,
635
+ );
636
+
637
+ expect(result1.granted.length).toBe(1);
638
+
639
+ // Agent 2 tries to reserve the same file
640
+ const result2 = await executeTool<{
641
+ granted: Array<{ id: number }>;
642
+ conflicts?: Array<{ path: string; holders: string[] }>;
643
+ warning?: string;
644
+ }>(
645
+ swarmmail_reserve,
646
+ {
647
+ paths: [conflictPath],
648
+ exclusive: true,
649
+ },
650
+ agent2Ctx,
651
+ );
652
+
653
+ // Should still grant but report conflicts
654
+ expect(result2.granted.length).toBeGreaterThan(0);
655
+ expect(result2.conflicts).toBeDefined();
656
+ expect(result2.conflicts?.length).toBeGreaterThan(0);
657
+ expect(result2.warning).toContain("already reserved");
658
+
659
+ clearSessionState(agent1Ctx.sessionID);
660
+ clearSessionState(agent2Ctx.sessionID);
661
+ });
662
+
663
+ it("returns error when not initialized", async () => {
664
+ const ctx = createTestContext();
665
+
666
+ const result = await executeTool<{ error?: string }>(
667
+ swarmmail_reserve,
668
+ {
669
+ paths: ["src/test.ts"],
670
+ },
671
+ ctx,
672
+ );
673
+
674
+ expect(result.error).toContain("not initialized");
675
+
676
+ clearSessionState(ctx.sessionID);
677
+ });
678
+ });
679
+
680
+ // ============================================================================
681
+ // Release Reservation Tests
682
+ // ============================================================================
683
+
684
+ describe("swarmmail_release", () => {
685
+ it("releases all reservations for an agent", async () => {
686
+ const ctx = createTestContext();
687
+
688
+ await executeTool(
689
+ swarmmail_init,
690
+ { project_path: TEST_DB_PATH, agent_name: "ReleaseAgent" },
691
+ ctx,
692
+ );
693
+
694
+ // Create reservations
695
+ await executeTool(
696
+ swarmmail_reserve,
697
+ {
698
+ paths: ["src/release-test-1.ts", "src/release-test-2.ts"],
699
+ exclusive: true,
700
+ },
701
+ ctx,
702
+ );
703
+
704
+ // Release all
705
+ const result = await executeTool<{
706
+ released: number;
707
+ released_at: string;
708
+ }>(swarmmail_release, {}, ctx);
709
+
710
+ expect(result.released).toBe(2);
711
+ expect(result.released_at).toBeTruthy();
712
+
713
+ clearSessionState(ctx.sessionID);
714
+ });
715
+
716
+ it("releases specific paths only", async () => {
717
+ const ctx = createTestContext();
718
+
719
+ await executeTool(
720
+ swarmmail_init,
721
+ { project_path: TEST_DB_PATH, agent_name: "SpecificReleaseAgent" },
722
+ ctx,
723
+ );
724
+
725
+ const path1 = "src/specific-release-1.ts";
726
+ const path2 = "src/specific-release-2.ts";
727
+
728
+ // Create reservations
729
+ await executeTool(
730
+ swarmmail_reserve,
731
+ {
732
+ paths: [path1, path2],
733
+ exclusive: true,
734
+ },
735
+ ctx,
736
+ );
737
+
738
+ // Release only one path
739
+ const result = await executeTool<{ released: number }>(
740
+ swarmmail_release,
741
+ { paths: [path1] },
742
+ ctx,
743
+ );
744
+
745
+ expect(result.released).toBe(1);
746
+
747
+ clearSessionState(ctx.sessionID);
748
+ });
749
+
750
+ it("releases by reservation IDs", async () => {
751
+ const ctx = createTestContext();
752
+
753
+ await executeTool(
754
+ swarmmail_init,
755
+ { project_path: TEST_DB_PATH, agent_name: "IdReleaseAgent" },
756
+ ctx,
757
+ );
758
+
759
+ // Create reservations
760
+ const reserve = await executeTool<{
761
+ granted: Array<{ id: number }>;
762
+ }>(
763
+ swarmmail_reserve,
764
+ {
765
+ paths: ["src/id-release-1.ts", "src/id-release-2.ts"],
766
+ exclusive: true,
767
+ },
768
+ ctx,
769
+ );
770
+
771
+ const firstId = reserve.granted[0].id;
772
+
773
+ // Release by ID
774
+ const result = await executeTool<{ released: number }>(
775
+ swarmmail_release,
776
+ { reservation_ids: [firstId] },
777
+ ctx,
778
+ );
779
+
780
+ expect(result.released).toBe(1);
781
+
782
+ clearSessionState(ctx.sessionID);
783
+ });
784
+ });
785
+
786
+ // ============================================================================
787
+ // Acknowledge Message Tests
788
+ // ============================================================================
789
+
790
+ describe("swarmmail_ack", () => {
791
+ it("acknowledges a message requiring acknowledgement", async () => {
792
+ const senderCtx = createTestContext();
793
+ const recipientCtx = createTestContext();
794
+
795
+ const sender = await executeTool<{ agent_name: string }>(
796
+ swarmmail_init,
797
+ { project_path: TEST_DB_PATH, agent_name: "AckSender" },
798
+ senderCtx,
799
+ );
800
+
801
+ const recipient = await executeTool<{ agent_name: string }>(
802
+ swarmmail_init,
803
+ { project_path: TEST_DB_PATH, agent_name: "AckRecipient" },
804
+ recipientCtx,
805
+ );
806
+
807
+ // Send message requiring ack
808
+ const sent = await executeTool<{ message_id: number }>(
809
+ swarmmail_send,
810
+ {
811
+ to: [recipient.agent_name],
812
+ subject: "Please acknowledge",
813
+ body: "This requires acknowledgement",
814
+ ack_required: true,
815
+ },
816
+ senderCtx,
817
+ );
818
+
819
+ // Acknowledge
820
+ const result = await executeTool<{
821
+ acknowledged: boolean;
822
+ acknowledged_at: string;
823
+ }>(swarmmail_ack, { message_id: sent.message_id }, recipientCtx);
824
+
825
+ expect(result.acknowledged).toBe(true);
826
+ expect(result.acknowledged_at).toBeTruthy();
827
+
828
+ clearSessionState(senderCtx.sessionID);
829
+ clearSessionState(recipientCtx.sessionID);
830
+ });
831
+ });
832
+
833
+ // ============================================================================
834
+ // Multi-Agent Coordination Tests
835
+ // ============================================================================
836
+
837
+ describe("multi-agent coordination", () => {
838
+ it("enables communication between multiple agents", async () => {
839
+ const coordCtx = createTestContext();
840
+ const worker1Ctx = createTestContext();
841
+ const worker2Ctx = createTestContext();
842
+
843
+ await executeTool<{ agent_name: string }>(
844
+ swarmmail_init,
845
+ { project_path: TEST_DB_PATH, agent_name: "Coordinator" },
846
+ coordCtx,
847
+ );
848
+
849
+ await executeTool<{ agent_name: string }>(
850
+ swarmmail_init,
851
+ { project_path: TEST_DB_PATH, agent_name: "Worker1" },
852
+ worker1Ctx,
853
+ );
854
+
855
+ await executeTool<{ agent_name: string }>(
856
+ swarmmail_init,
857
+ { project_path: TEST_DB_PATH, agent_name: "Worker2" },
858
+ worker2Ctx,
859
+ );
860
+
861
+ // Coordinator broadcasts to workers
862
+ await executeTool(
863
+ swarmmail_send,
864
+ {
865
+ to: ["Worker1", "Worker2"],
866
+ subject: "Task assignment",
867
+ body: "Please complete your subtasks",
868
+ thread_id: "bd-epic-123",
869
+ importance: "high",
870
+ },
871
+ coordCtx,
872
+ );
873
+
874
+ // Verify both workers received the message
875
+ const worker1Inbox = await executeTool<{
876
+ messages: Array<{ subject: string }>;
877
+ }>(swarmmail_inbox, {}, worker1Ctx);
878
+
879
+ const worker2Inbox = await executeTool<{
880
+ messages: Array<{ subject: string }>;
881
+ }>(swarmmail_inbox, {}, worker2Ctx);
882
+
883
+ expect(
884
+ worker1Inbox.messages.some((m) => m.subject === "Task assignment"),
885
+ ).toBe(true);
886
+ expect(
887
+ worker2Inbox.messages.some((m) => m.subject === "Task assignment"),
888
+ ).toBe(true);
889
+
890
+ clearSessionState(coordCtx.sessionID);
891
+ clearSessionState(worker1Ctx.sessionID);
892
+ clearSessionState(worker2Ctx.sessionID);
893
+ });
894
+
895
+ it("prevents file conflicts in swarm scenarios", async () => {
896
+ const worker1Ctx = createTestContext();
897
+ const worker2Ctx = createTestContext();
898
+
899
+ await executeTool<{ agent_name: string }>(
900
+ swarmmail_init,
901
+ { project_path: TEST_DB_PATH, agent_name: "SwarmWorker1" },
902
+ worker1Ctx,
903
+ );
904
+
905
+ await executeTool<{ agent_name: string }>(
906
+ swarmmail_init,
907
+ { project_path: TEST_DB_PATH, agent_name: "SwarmWorker2" },
908
+ worker2Ctx,
909
+ );
910
+
911
+ const path1 = "src/swarm/file1.ts";
912
+ const path2 = "src/swarm/file2.ts";
913
+
914
+ // Worker 1 reserves file 1
915
+ const res1 = await executeTool<{
916
+ granted: Array<{ id: number }>;
917
+ conflicts?: unknown[];
918
+ }>(
919
+ swarmmail_reserve,
920
+ {
921
+ paths: [path1],
922
+ exclusive: true,
923
+ reason: "bd-subtask-1",
924
+ },
925
+ worker1Ctx,
926
+ );
927
+
928
+ // Worker 2 reserves file 2
929
+ const res2 = await executeTool<{
930
+ granted: Array<{ id: number }>;
931
+ conflicts?: unknown[];
932
+ }>(
933
+ swarmmail_reserve,
934
+ {
935
+ paths: [path2],
936
+ exclusive: true,
937
+ reason: "bd-subtask-2",
938
+ },
939
+ worker2Ctx,
940
+ );
941
+
942
+ // Both should succeed (no conflicts)
943
+ expect(res1.granted.length).toBe(1);
944
+ expect(res1.conflicts).toBeUndefined();
945
+ expect(res2.granted.length).toBe(1);
946
+ expect(res2.conflicts).toBeUndefined();
947
+
948
+ // Worker 1 tries to reserve file 2 (should conflict)
949
+ const conflict = await executeTool<{
950
+ granted?: Array<{ id: number }>;
951
+ conflicts?: Array<{ path: string; holders: string[] }>;
952
+ warning?: string;
953
+ }>(
954
+ swarmmail_reserve,
955
+ {
956
+ paths: [path2],
957
+ exclusive: true,
958
+ },
959
+ worker1Ctx,
960
+ );
961
+
962
+ expect(conflict.conflicts).toBeDefined();
963
+ expect(conflict.conflicts?.length).toBeGreaterThan(0);
964
+ expect(conflict.warning).toContain("already reserved");
965
+
966
+ clearSessionState(worker1Ctx.sessionID);
967
+ clearSessionState(worker2Ctx.sessionID);
968
+ });
969
+ });
970
+ });