opencode-swarm-plugin 0.12.31 → 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 (47) 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/bun.lock +23 -0
  5. package/dist/index.js +4020 -438
  6. package/dist/pglite.data +0 -0
  7. package/dist/pglite.wasm +0 -0
  8. package/dist/plugin.js +4008 -514
  9. package/examples/commands/swarm.md +51 -7
  10. package/examples/skills/beads-workflow/SKILL.md +75 -28
  11. package/examples/skills/swarm-coordination/SKILL.md +92 -1
  12. package/global-skills/testing-patterns/SKILL.md +430 -0
  13. package/global-skills/testing-patterns/references/dependency-breaking-catalog.md +586 -0
  14. package/package.json +11 -5
  15. package/src/index.ts +44 -5
  16. package/src/streams/agent-mail.test.ts +777 -0
  17. package/src/streams/agent-mail.ts +535 -0
  18. package/src/streams/debug.test.ts +500 -0
  19. package/src/streams/debug.ts +629 -0
  20. package/src/streams/effect/ask.integration.test.ts +314 -0
  21. package/src/streams/effect/ask.ts +202 -0
  22. package/src/streams/effect/cursor.integration.test.ts +418 -0
  23. package/src/streams/effect/cursor.ts +288 -0
  24. package/src/streams/effect/deferred.test.ts +357 -0
  25. package/src/streams/effect/deferred.ts +445 -0
  26. package/src/streams/effect/index.ts +17 -0
  27. package/src/streams/effect/layers.ts +73 -0
  28. package/src/streams/effect/lock.test.ts +385 -0
  29. package/src/streams/effect/lock.ts +399 -0
  30. package/src/streams/effect/mailbox.test.ts +260 -0
  31. package/src/streams/effect/mailbox.ts +318 -0
  32. package/src/streams/events.test.ts +628 -0
  33. package/src/streams/events.ts +214 -0
  34. package/src/streams/index.test.ts +229 -0
  35. package/src/streams/index.ts +492 -0
  36. package/src/streams/migrations.test.ts +355 -0
  37. package/src/streams/migrations.ts +269 -0
  38. package/src/streams/projections.test.ts +611 -0
  39. package/src/streams/projections.ts +302 -0
  40. package/src/streams/store.integration.test.ts +548 -0
  41. package/src/streams/store.ts +546 -0
  42. package/src/streams/swarm-mail.ts +552 -0
  43. package/src/swarm-mail.integration.test.ts +970 -0
  44. package/src/swarm-mail.ts +739 -0
  45. package/src/swarm.ts +84 -59
  46. package/src/tool-availability.ts +35 -2
  47. package/global-skills/mcp-tool-authoring/SKILL.md +0 -695
@@ -0,0 +1,739 @@
1
+ /**
2
+ * Swarm Mail Plugin Tools - Embedded event-sourced implementation
3
+ *
4
+ * Replaces the MCP-based agent-mail with embedded PGLite storage.
5
+ * Same tool API surface, but no external server dependency.
6
+ *
7
+ * Key features:
8
+ * - Event sourcing for full audit trail
9
+ * - Offset-based resumability (Durable Streams inspired)
10
+ * - Materialized views for fast queries
11
+ * - File reservation with conflict detection
12
+ *
13
+ * CRITICAL CONSTRAINTS (same as agent-mail):
14
+ * - swarmmail_inbox ALWAYS limits to 5 messages max
15
+ * - swarmmail_inbox ALWAYS excludes bodies by default
16
+ * - Use summarize_thread instead of fetching all messages
17
+ * - Auto-release reservations when tasks complete
18
+ */
19
+ import { tool } from "@opencode-ai/plugin";
20
+ import {
21
+ initSwarmAgent,
22
+ sendSwarmMessage,
23
+ getSwarmInbox,
24
+ readSwarmMessage,
25
+ reserveSwarmFiles,
26
+ releaseSwarmFiles,
27
+ acknowledgeSwarmMessage,
28
+ checkSwarmHealth,
29
+ } from "./streams/swarm-mail";
30
+ import { getActiveReservations } from "./streams/projections";
31
+ import {
32
+ existsSync,
33
+ mkdirSync,
34
+ readFileSync,
35
+ writeFileSync,
36
+ unlinkSync,
37
+ } from "node:fs";
38
+ import { join } from "node:path";
39
+ import { tmpdir } from "node:os";
40
+
41
+ // ============================================================================
42
+ // Types
43
+ // ============================================================================
44
+
45
+ /** Tool execution context from OpenCode plugin */
46
+ interface ToolContext {
47
+ sessionID: string;
48
+ }
49
+
50
+ /** Swarm Mail session state */
51
+ export interface SwarmMailState {
52
+ projectKey: string;
53
+ agentName: string;
54
+ reservations: number[];
55
+ startedAt: string;
56
+ }
57
+
58
+ /** Init tool arguments */
59
+ interface InitArgs {
60
+ project_path?: string;
61
+ agent_name?: string;
62
+ task_description?: string;
63
+ }
64
+
65
+ /** Send tool arguments */
66
+ interface SendArgs {
67
+ to: string[];
68
+ subject: string;
69
+ body: string;
70
+ thread_id?: string;
71
+ importance?: "low" | "normal" | "high" | "urgent";
72
+ ack_required?: boolean;
73
+ }
74
+
75
+ /** Inbox tool arguments */
76
+ interface InboxArgs {
77
+ limit?: number;
78
+ urgent_only?: boolean;
79
+ }
80
+
81
+ /** Read message tool arguments */
82
+ interface ReadMessageArgs {
83
+ message_id: number;
84
+ }
85
+
86
+ /** Reserve tool arguments */
87
+ interface ReserveArgs {
88
+ paths: string[];
89
+ reason?: string;
90
+ exclusive?: boolean;
91
+ ttl_seconds?: number;
92
+ }
93
+
94
+ /** Release tool arguments */
95
+ interface ReleaseArgs {
96
+ paths?: string[];
97
+ reservation_ids?: number[];
98
+ }
99
+
100
+ /** Ack tool arguments */
101
+ interface AckArgs {
102
+ message_id: number;
103
+ }
104
+
105
+ // ============================================================================
106
+ // Configuration
107
+ // ============================================================================
108
+
109
+ const MAX_INBOX_LIMIT = 5; // HARD CAP - context preservation
110
+
111
+ /**
112
+ * Default project directory for Swarm Mail operations
113
+ *
114
+ * This is set by the plugin init to the actual working directory (from OpenCode).
115
+ * Without this, tools might use the plugin's directory instead of the project's.
116
+ */
117
+ let swarmMailProjectDirectory: string | null = null;
118
+
119
+ /**
120
+ * Set the default project directory for Swarm Mail operations
121
+ *
122
+ * Called during plugin initialization with the actual project directory.
123
+ */
124
+ export function setSwarmMailProjectDirectory(directory: string): void {
125
+ swarmMailProjectDirectory = directory;
126
+ }
127
+
128
+ /**
129
+ * Get the default project directory
130
+ * Returns undefined if not set - let getDatabasePath use global fallback
131
+ */
132
+ export function getSwarmMailProjectDirectory(): string | undefined {
133
+ return swarmMailProjectDirectory ?? undefined;
134
+ }
135
+
136
+ // ============================================================================
137
+ // Session State Management
138
+ // ============================================================================
139
+
140
+ const SESSION_STATE_DIR =
141
+ process.env.SWARM_STATE_DIR || join(tmpdir(), "swarm-sessions");
142
+
143
+ function getSessionStatePath(sessionID: string): string {
144
+ const safeID = sessionID.replace(/[^a-zA-Z0-9_-]/g, "_");
145
+ return join(SESSION_STATE_DIR, `${safeID}.json`);
146
+ }
147
+
148
+ function loadSessionState(sessionID: string): SwarmMailState | null {
149
+ const path = getSessionStatePath(sessionID);
150
+ try {
151
+ if (existsSync(path)) {
152
+ const data = readFileSync(path, "utf-8");
153
+ return JSON.parse(data) as SwarmMailState;
154
+ }
155
+ } catch (error) {
156
+ console.warn(`[swarm-mail] Could not load session state: ${error}`);
157
+ }
158
+ return null;
159
+ }
160
+
161
+ function saveSessionState(sessionID: string, state: SwarmMailState): boolean {
162
+ try {
163
+ if (!existsSync(SESSION_STATE_DIR)) {
164
+ mkdirSync(SESSION_STATE_DIR, { recursive: true });
165
+ }
166
+ const path = getSessionStatePath(sessionID);
167
+ writeFileSync(path, JSON.stringify(state, null, 2));
168
+ return true;
169
+ } catch (error) {
170
+ console.warn(`[swarm-mail] Could not save session state: ${error}`);
171
+ return false;
172
+ }
173
+ }
174
+
175
+ export function clearSessionState(sessionID: string): void {
176
+ const path = getSessionStatePath(sessionID);
177
+ try {
178
+ if (existsSync(path)) {
179
+ unlinkSync(path);
180
+ }
181
+ } catch {
182
+ // Ignore errors
183
+ }
184
+ }
185
+
186
+ // ============================================================================
187
+ // Plugin Tools
188
+ // ============================================================================
189
+
190
+ /**
191
+ * Initialize Swarm Mail session
192
+ */
193
+ export const swarmmail_init = tool({
194
+ description:
195
+ "Initialize Swarm Mail session. Creates agent identity and registers with the embedded event store.",
196
+ args: {
197
+ project_path: tool.schema
198
+ .string()
199
+ .optional()
200
+ .describe("Project path (defaults to current working directory)"),
201
+ agent_name: tool.schema
202
+ .string()
203
+ .optional()
204
+ .describe("Custom agent name (auto-generated if not provided)"),
205
+ task_description: tool.schema
206
+ .string()
207
+ .optional()
208
+ .describe("Description of the task this agent is working on"),
209
+ },
210
+ async execute(args: InitArgs, ctx: ToolContext): Promise<string> {
211
+ // For init, we need a project path - use provided, stored, or cwd
212
+ const projectPath =
213
+ args.project_path || getSwarmMailProjectDirectory() || process.cwd();
214
+ const sessionID = ctx.sessionID || "default";
215
+
216
+ // Check if already initialized
217
+ const existingState = loadSessionState(sessionID);
218
+ if (existingState) {
219
+ return JSON.stringify(
220
+ {
221
+ agent_name: existingState.agentName,
222
+ project_key: existingState.projectKey,
223
+ message: `Session already initialized as ${existingState.agentName}`,
224
+ already_initialized: true,
225
+ },
226
+ null,
227
+ 2,
228
+ );
229
+ }
230
+
231
+ try {
232
+ const result = await initSwarmAgent({
233
+ projectPath,
234
+ agentName: args.agent_name,
235
+ taskDescription: args.task_description,
236
+ });
237
+
238
+ // Save session state
239
+ const state: SwarmMailState = {
240
+ projectKey: result.projectKey,
241
+ agentName: result.agentName,
242
+ reservations: [],
243
+ startedAt: new Date().toISOString(),
244
+ };
245
+ saveSessionState(sessionID, state);
246
+
247
+ return JSON.stringify(
248
+ {
249
+ agent_name: result.agentName,
250
+ project_key: result.projectKey,
251
+ message: `Initialized as ${result.agentName}`,
252
+ },
253
+ null,
254
+ 2,
255
+ );
256
+ } catch (error) {
257
+ return JSON.stringify(
258
+ {
259
+ error: `Failed to initialize: ${error instanceof Error ? error.message : String(error)}`,
260
+ },
261
+ null,
262
+ 2,
263
+ );
264
+ }
265
+ },
266
+ });
267
+
268
+ /**
269
+ * Send message to other agents
270
+ */
271
+ export const swarmmail_send = tool({
272
+ description: "Send message to other swarm agents",
273
+ args: {
274
+ to: tool.schema
275
+ .array(tool.schema.string())
276
+ .describe("List of recipient agent names"),
277
+ subject: tool.schema.string().describe("Message subject"),
278
+ body: tool.schema.string().describe("Message body"),
279
+ thread_id: tool.schema
280
+ .string()
281
+ .optional()
282
+ .describe("Thread ID for conversation tracking"),
283
+ importance: tool.schema
284
+ .enum(["low", "normal", "high", "urgent"])
285
+ .optional()
286
+ .describe("Message importance level"),
287
+ ack_required: tool.schema
288
+ .boolean()
289
+ .optional()
290
+ .describe("Whether acknowledgement is required"),
291
+ },
292
+ async execute(args: SendArgs, ctx: ToolContext): Promise<string> {
293
+ const sessionID = ctx.sessionID || "default";
294
+ const state = loadSessionState(sessionID);
295
+
296
+ if (!state) {
297
+ return JSON.stringify(
298
+ { error: "Session not initialized. Call swarmmail_init first." },
299
+ null,
300
+ 2,
301
+ );
302
+ }
303
+
304
+ try {
305
+ const result = await sendSwarmMessage({
306
+ projectPath: state.projectKey,
307
+ fromAgent: state.agentName,
308
+ toAgents: args.to,
309
+ subject: args.subject,
310
+ body: args.body,
311
+ threadId: args.thread_id,
312
+ importance: args.importance,
313
+ ackRequired: args.ack_required,
314
+ });
315
+
316
+ return JSON.stringify(
317
+ {
318
+ success: result.success,
319
+ message_id: result.messageId,
320
+ thread_id: result.threadId,
321
+ recipient_count: result.recipientCount,
322
+ },
323
+ null,
324
+ 2,
325
+ );
326
+ } catch (error) {
327
+ return JSON.stringify(
328
+ {
329
+ error: `Failed to send message: ${error instanceof Error ? error.message : String(error)}`,
330
+ },
331
+ null,
332
+ 2,
333
+ );
334
+ }
335
+ },
336
+ });
337
+
338
+ /**
339
+ * Fetch inbox (CONTEXT-SAFE: bodies excluded, limit 5)
340
+ */
341
+ export const swarmmail_inbox = tool({
342
+ description:
343
+ "Fetch inbox (CONTEXT-SAFE: bodies excluded by default, max 5 messages). Use swarmmail_read_message for full body.",
344
+ args: {
345
+ limit: tool.schema
346
+ .number()
347
+ .max(MAX_INBOX_LIMIT)
348
+ .optional()
349
+ .describe(`Max messages to fetch (hard cap: ${MAX_INBOX_LIMIT})`),
350
+ urgent_only: tool.schema
351
+ .boolean()
352
+ .optional()
353
+ .describe("Only fetch urgent messages"),
354
+ },
355
+ async execute(args: InboxArgs, ctx: ToolContext): Promise<string> {
356
+ const sessionID = ctx.sessionID || "default";
357
+ const state = loadSessionState(sessionID);
358
+
359
+ if (!state) {
360
+ return JSON.stringify(
361
+ { error: "Session not initialized. Call swarmmail_init first." },
362
+ null,
363
+ 2,
364
+ );
365
+ }
366
+
367
+ try {
368
+ const result = await getSwarmInbox({
369
+ projectPath: state.projectKey,
370
+ agentName: state.agentName,
371
+ limit: Math.min(args.limit || MAX_INBOX_LIMIT, MAX_INBOX_LIMIT),
372
+ urgentOnly: args.urgent_only,
373
+ includeBodies: false, // ALWAYS false for context preservation
374
+ });
375
+
376
+ return JSON.stringify(
377
+ {
378
+ messages: result.messages.map((m) => ({
379
+ id: m.id,
380
+ from: m.from_agent,
381
+ subject: m.subject,
382
+ thread_id: m.thread_id,
383
+ importance: m.importance,
384
+ timestamp: m.created_at,
385
+ })),
386
+ total: result.total,
387
+ note: "Use swarmmail_read_message to fetch full body",
388
+ },
389
+ null,
390
+ 2,
391
+ );
392
+ } catch (error) {
393
+ return JSON.stringify(
394
+ {
395
+ error: `Failed to fetch inbox: ${error instanceof Error ? error.message : String(error)}`,
396
+ },
397
+ null,
398
+ 2,
399
+ );
400
+ }
401
+ },
402
+ });
403
+
404
+ /**
405
+ * Fetch ONE message body by ID
406
+ */
407
+ export const swarmmail_read_message = tool({
408
+ description:
409
+ "Fetch ONE message body by ID. Use for reading full message content.",
410
+ args: {
411
+ message_id: tool.schema.number().describe("Message ID to read"),
412
+ },
413
+ async execute(args: ReadMessageArgs, ctx: ToolContext): Promise<string> {
414
+ const sessionID = ctx.sessionID || "default";
415
+ const state = loadSessionState(sessionID);
416
+
417
+ if (!state) {
418
+ return JSON.stringify(
419
+ { error: "Session not initialized. Call swarmmail_init first." },
420
+ null,
421
+ 2,
422
+ );
423
+ }
424
+
425
+ try {
426
+ const message = await readSwarmMessage({
427
+ projectPath: state.projectKey,
428
+ messageId: args.message_id,
429
+ agentName: state.agentName,
430
+ markAsRead: true,
431
+ });
432
+
433
+ if (!message) {
434
+ return JSON.stringify(
435
+ { error: `Message ${args.message_id} not found` },
436
+ null,
437
+ 2,
438
+ );
439
+ }
440
+
441
+ return JSON.stringify(
442
+ {
443
+ id: message.id,
444
+ from: message.from_agent,
445
+ subject: message.subject,
446
+ body: message.body,
447
+ thread_id: message.thread_id,
448
+ importance: message.importance,
449
+ timestamp: message.created_at,
450
+ },
451
+ null,
452
+ 2,
453
+ );
454
+ } catch (error) {
455
+ return JSON.stringify(
456
+ {
457
+ error: `Failed to read message: ${error instanceof Error ? error.message : String(error)}`,
458
+ },
459
+ null,
460
+ 2,
461
+ );
462
+ }
463
+ },
464
+ });
465
+
466
+ /**
467
+ * Reserve file paths for exclusive editing
468
+ */
469
+ export const swarmmail_reserve = tool({
470
+ description:
471
+ "Reserve file paths for exclusive editing. Prevents conflicts with other agents.",
472
+ args: {
473
+ paths: tool.schema
474
+ .array(tool.schema.string())
475
+ .describe("File paths or glob patterns to reserve"),
476
+ reason: tool.schema
477
+ .string()
478
+ .optional()
479
+ .describe("Reason for reservation (e.g., bead ID)"),
480
+ exclusive: tool.schema
481
+ .boolean()
482
+ .optional()
483
+ .describe("Whether reservation is exclusive (default: true)"),
484
+ ttl_seconds: tool.schema
485
+ .number()
486
+ .optional()
487
+ .describe("Time-to-live in seconds (default: 3600)"),
488
+ },
489
+ async execute(args: ReserveArgs, ctx: ToolContext): Promise<string> {
490
+ const sessionID = ctx.sessionID || "default";
491
+ const state = loadSessionState(sessionID);
492
+
493
+ if (!state) {
494
+ return JSON.stringify(
495
+ { error: "Session not initialized. Call swarmmail_init first." },
496
+ null,
497
+ 2,
498
+ );
499
+ }
500
+
501
+ try {
502
+ const result = await reserveSwarmFiles({
503
+ projectPath: state.projectKey,
504
+ agentName: state.agentName,
505
+ paths: args.paths,
506
+ reason: args.reason,
507
+ exclusive: args.exclusive ?? true,
508
+ ttlSeconds: args.ttl_seconds,
509
+ });
510
+
511
+ // Track reservations in session state
512
+ if (result.granted.length > 0) {
513
+ state.reservations.push(...result.granted.map((r) => r.id));
514
+ saveSessionState(sessionID, state);
515
+ }
516
+
517
+ if (result.conflicts.length > 0) {
518
+ return JSON.stringify(
519
+ {
520
+ granted: result.granted,
521
+ conflicts: result.conflicts,
522
+ warning: `${result.conflicts.length} path(s) already reserved by other agents`,
523
+ },
524
+ null,
525
+ 2,
526
+ );
527
+ }
528
+
529
+ return JSON.stringify(
530
+ {
531
+ granted: result.granted,
532
+ message: `Reserved ${result.granted.length} path(s)`,
533
+ },
534
+ null,
535
+ 2,
536
+ );
537
+ } catch (error) {
538
+ return JSON.stringify(
539
+ {
540
+ error: `Failed to reserve files: ${error instanceof Error ? error.message : String(error)}`,
541
+ },
542
+ null,
543
+ 2,
544
+ );
545
+ }
546
+ },
547
+ });
548
+
549
+ /**
550
+ * Release file reservations
551
+ */
552
+ export const swarmmail_release = tool({
553
+ description: "Release file reservations. Call when done editing files.",
554
+ args: {
555
+ paths: tool.schema
556
+ .array(tool.schema.string())
557
+ .optional()
558
+ .describe("Specific paths to release (releases all if omitted)"),
559
+ reservation_ids: tool.schema
560
+ .array(tool.schema.number())
561
+ .optional()
562
+ .describe("Specific reservation IDs to release"),
563
+ },
564
+ async execute(args: ReleaseArgs, ctx: ToolContext): Promise<string> {
565
+ const sessionID = ctx.sessionID || "default";
566
+ const state = loadSessionState(sessionID);
567
+
568
+ if (!state) {
569
+ return JSON.stringify(
570
+ { error: "Session not initialized. Call swarmmail_init first." },
571
+ null,
572
+ 2,
573
+ );
574
+ }
575
+
576
+ try {
577
+ // Get current reservations to find which IDs correspond to paths
578
+ const currentReservations = await getActiveReservations(
579
+ state.projectKey,
580
+ state.projectKey,
581
+ state.agentName,
582
+ );
583
+
584
+ const result = await releaseSwarmFiles({
585
+ projectPath: state.projectKey,
586
+ agentName: state.agentName,
587
+ paths: args.paths,
588
+ reservationIds: args.reservation_ids,
589
+ });
590
+
591
+ // Clear tracked reservations
592
+ if (!args.paths && !args.reservation_ids) {
593
+ state.reservations = [];
594
+ } else if (args.reservation_ids) {
595
+ state.reservations = state.reservations.filter(
596
+ (id) => !args.reservation_ids!.includes(id),
597
+ );
598
+ } else if (args.paths) {
599
+ // When releasing by paths, find the reservation IDs that match those paths
600
+ const releasedIds = currentReservations
601
+ .filter((r: { path_pattern: string }) =>
602
+ args.paths!.includes(r.path_pattern),
603
+ )
604
+ .map((r: { id: number }) => r.id);
605
+ state.reservations = state.reservations.filter(
606
+ (id: number) => !releasedIds.includes(id),
607
+ );
608
+ }
609
+ saveSessionState(sessionID, state);
610
+
611
+ return JSON.stringify(
612
+ {
613
+ released: result.released,
614
+ released_at: result.releasedAt,
615
+ },
616
+ null,
617
+ 2,
618
+ );
619
+ } catch (error) {
620
+ return JSON.stringify(
621
+ {
622
+ error: `Failed to release files: ${error instanceof Error ? error.message : String(error)}`,
623
+ },
624
+ null,
625
+ 2,
626
+ );
627
+ }
628
+ },
629
+ });
630
+
631
+ /**
632
+ * Acknowledge a message
633
+ */
634
+ export const swarmmail_ack = tool({
635
+ description:
636
+ "Acknowledge a message (for messages that require acknowledgement)",
637
+ args: {
638
+ message_id: tool.schema.number().describe("Message ID to acknowledge"),
639
+ },
640
+ async execute(args: AckArgs, ctx: ToolContext): Promise<string> {
641
+ const sessionID = ctx.sessionID || "default";
642
+ const state = loadSessionState(sessionID);
643
+
644
+ if (!state) {
645
+ return JSON.stringify(
646
+ { error: "Session not initialized. Call swarmmail_init first." },
647
+ null,
648
+ 2,
649
+ );
650
+ }
651
+
652
+ try {
653
+ const result = await acknowledgeSwarmMessage({
654
+ projectPath: state.projectKey,
655
+ messageId: args.message_id,
656
+ agentName: state.agentName,
657
+ });
658
+
659
+ return JSON.stringify(
660
+ {
661
+ acknowledged: result.acknowledged,
662
+ acknowledged_at: result.acknowledgedAt,
663
+ },
664
+ null,
665
+ 2,
666
+ );
667
+ } catch (error) {
668
+ return JSON.stringify(
669
+ {
670
+ error: `Failed to acknowledge message: ${error instanceof Error ? error.message : String(error)}`,
671
+ },
672
+ null,
673
+ 2,
674
+ );
675
+ }
676
+ },
677
+ });
678
+
679
+ /**
680
+ * Check if Swarm Mail is healthy
681
+ */
682
+ export const swarmmail_health = tool({
683
+ description: "Check if Swarm Mail embedded store is healthy",
684
+ args: {},
685
+ async execute(
686
+ _args: Record<string, never>,
687
+ ctx: ToolContext,
688
+ ): Promise<string> {
689
+ const sessionID = ctx.sessionID || "default";
690
+ const state = loadSessionState(sessionID);
691
+ // For health check, undefined is OK - database layer uses global fallback
692
+ const projectPath = state?.projectKey || getSwarmMailProjectDirectory();
693
+
694
+ try {
695
+ const result = await checkSwarmHealth(projectPath);
696
+
697
+ return JSON.stringify(
698
+ {
699
+ healthy: result.healthy,
700
+ database: result.database,
701
+ stats: result.stats,
702
+ session: state
703
+ ? {
704
+ agent_name: state.agentName,
705
+ project_key: state.projectKey,
706
+ reservations: state.reservations.length,
707
+ }
708
+ : null,
709
+ },
710
+ null,
711
+ 2,
712
+ );
713
+ } catch (error) {
714
+ return JSON.stringify(
715
+ {
716
+ healthy: false,
717
+ error: `Health check failed: ${error instanceof Error ? error.message : String(error)}`,
718
+ },
719
+ null,
720
+ 2,
721
+ );
722
+ }
723
+ },
724
+ });
725
+
726
+ // ============================================================================
727
+ // Exports
728
+ // ============================================================================
729
+
730
+ export const swarmMailTools = {
731
+ swarmmail_init: swarmmail_init,
732
+ swarmmail_send: swarmmail_send,
733
+ swarmmail_inbox: swarmmail_inbox,
734
+ swarmmail_read_message: swarmmail_read_message,
735
+ swarmmail_reserve: swarmmail_reserve,
736
+ swarmmail_release: swarmmail_release,
737
+ swarmmail_ack: swarmmail_ack,
738
+ swarmmail_health: swarmmail_health,
739
+ };