opencode-swarm-plugin 0.17.1 → 0.19.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.
package/src/swarm.ts CHANGED
@@ -1,3651 +1,35 @@
1
1
  /**
2
2
  * Swarm Module - High-level swarm coordination
3
3
  *
4
- * Orchestrates beads, Agent Mail, and structured validation for parallel task execution.
5
- * The actual agent spawning happens via OpenCode's Task tool - this module provides
6
- * the primitives and prompts that /swarm command uses.
4
+ * This module re-exports from focused submodules for backward compatibility.
5
+ * For new code, prefer importing from specific modules:
6
+ * - swarm-strategies.ts - Strategy selection
7
+ * - swarm-decompose.ts - Task decomposition
8
+ * - swarm-prompts.ts - Prompt templates
9
+ * - swarm-orchestrate.ts - Status and completion
7
10
  *
8
- * Key responsibilities:
9
- * - Task decomposition into bead trees with file assignments
10
- * - Swarm status tracking via beads + Agent Mail
11
- * - Progress reporting and completion handling
12
- * - Prompt templates for decomposition, subtasks, and evaluation
11
+ * @module swarm
13
12
  */
14
- import { tool } from "@opencode-ai/plugin";
15
- import { z } from "zod";
16
- import {
17
- BeadTreeSchema,
18
- SwarmStatusSchema,
19
- AgentProgressSchema,
20
- EvaluationSchema,
21
- BeadSchema,
22
- type SwarmStatus,
23
- type AgentProgress,
24
- type Evaluation,
25
- type SpawnedAgent,
26
- type Bead,
27
- } from "./schemas";
28
- import {
29
- sendSwarmMessage,
30
- getSwarmInbox,
31
- readSwarmMessage,
32
- releaseSwarmFiles,
33
- } from "./streams/swarm-mail";
34
- import {
35
- OutcomeSignalsSchema,
36
- DecompositionStrategySchema,
37
- scoreImplicitFeedback,
38
- outcomeToFeedback,
39
- ErrorAccumulator,
40
- ErrorEntrySchema,
41
- type OutcomeSignals,
42
- type ScoredOutcome,
43
- type FeedbackEvent,
44
- type ErrorEntry,
45
- type ErrorType,
46
- type DecompositionStrategy as LearningDecompositionStrategy,
47
- DEFAULT_LEARNING_CONFIG,
48
- } from "./learning";
49
- import {
50
- isToolAvailable,
51
- warnMissingTool,
52
- checkAllTools,
53
- formatToolAvailability,
54
- type ToolName,
55
- } from "./tool-availability";
56
- import {
57
- getSkillsContextForSwarm,
58
- findRelevantSkills,
59
- listSkills,
60
- } from "./skills";
61
13
 
62
- // ============================================================================
63
- // Conflict Detection
64
- // ============================================================================
14
+ // Re-export everything for backward compatibility
15
+ export * from "./swarm-strategies";
16
+ export * from "./swarm-decompose";
17
+ export * from "./swarm-prompts";
18
+ export * from "./swarm-orchestrate";
65
19
 
66
- /**
67
- * Marker words that indicate positive directives
68
- */
69
- const POSITIVE_MARKERS = [
70
- "always",
71
- "must",
72
- "required",
73
- "ensure",
74
- "use",
75
- "prefer",
76
- ];
77
-
78
- /**
79
- * Marker words that indicate negative directives
80
- */
81
- const NEGATIVE_MARKERS = [
82
- "never",
83
- "dont",
84
- "don't",
85
- "avoid",
86
- "forbid",
87
- "no ",
88
- "not ",
89
- ];
90
-
91
- /**
92
- * A detected conflict between subtask instructions
93
- */
94
- export interface InstructionConflict {
95
- subtask_a: number;
96
- subtask_b: number;
97
- directive_a: string;
98
- directive_b: string;
99
- conflict_type: "positive_negative" | "contradictory";
100
- description: string;
101
- }
102
-
103
- /**
104
- * Extract directives from text based on marker words
105
- */
106
- function extractDirectives(text: string): {
107
- positive: string[];
108
- negative: string[];
109
- } {
110
- const sentences = text.split(/[.!?\n]+/).map((s) => s.trim().toLowerCase());
111
- const positive: string[] = [];
112
- const negative: string[] = [];
113
-
114
- for (const sentence of sentences) {
115
- if (!sentence) continue;
116
-
117
- const hasPositive = POSITIVE_MARKERS.some((m) => sentence.includes(m));
118
- const hasNegative = NEGATIVE_MARKERS.some((m) => sentence.includes(m));
119
-
120
- if (hasPositive && !hasNegative) {
121
- positive.push(sentence);
122
- } else if (hasNegative) {
123
- negative.push(sentence);
124
- }
125
- }
126
-
127
- return { positive, negative };
128
- }
129
-
130
- /**
131
- * Check if two directives conflict
132
- *
133
- * Simple heuristic: look for common subjects with opposite polarity
134
- */
135
- function directivesConflict(positive: string, negative: string): boolean {
136
- // Extract key nouns/concepts (simple word overlap check)
137
- const positiveWords = new Set(
138
- positive.split(/\s+/).filter((w) => w.length > 3),
139
- );
140
- const negativeWords = negative.split(/\s+/).filter((w) => w.length > 3);
141
-
142
- // If they share significant words, they might conflict
143
- const overlap = negativeWords.filter((w) => positiveWords.has(w));
144
- return overlap.length >= 2;
145
- }
146
-
147
- /**
148
- * Detect conflicts between subtask instructions
149
- *
150
- * Looks for cases where one subtask says "always use X" and another says "avoid X".
151
- *
152
- * @param subtasks - Array of subtask descriptions
153
- * @returns Array of detected conflicts
154
- *
155
- * @see https://github.com/Dicklesworthstone/cass_memory_system/blob/main/src/curate.ts#L36-L89
156
- */
157
- export function detectInstructionConflicts(
158
- subtasks: Array<{ title: string; description?: string }>,
159
- ): InstructionConflict[] {
160
- const conflicts: InstructionConflict[] = [];
161
-
162
- // Extract directives from each subtask
163
- const subtaskDirectives = subtasks.map((s, i) => ({
164
- index: i,
165
- title: s.title,
166
- ...extractDirectives(`${s.title} ${s.description || ""}`),
167
- }));
168
-
169
- // Compare each pair of subtasks
170
- for (let i = 0; i < subtaskDirectives.length; i++) {
171
- for (let j = i + 1; j < subtaskDirectives.length; j++) {
172
- const a = subtaskDirectives[i];
173
- const b = subtaskDirectives[j];
174
-
175
- // Check if A's positive conflicts with B's negative
176
- for (const posA of a.positive) {
177
- for (const negB of b.negative) {
178
- if (directivesConflict(posA, negB)) {
179
- conflicts.push({
180
- subtask_a: i,
181
- subtask_b: j,
182
- directive_a: posA,
183
- directive_b: negB,
184
- conflict_type: "positive_negative",
185
- description: `Subtask ${i} says "${posA}" but subtask ${j} says "${negB}"`,
186
- });
187
- }
188
- }
189
- }
190
-
191
- // Check if B's positive conflicts with A's negative
192
- for (const posB of b.positive) {
193
- for (const negA of a.negative) {
194
- if (directivesConflict(posB, negA)) {
195
- conflicts.push({
196
- subtask_a: j,
197
- subtask_b: i,
198
- directive_a: posB,
199
- directive_b: negA,
200
- conflict_type: "positive_negative",
201
- description: `Subtask ${j} says "${posB}" but subtask ${i} says "${negA}"`,
202
- });
203
- }
204
- }
205
- }
206
- }
207
- }
208
-
209
- return conflicts;
210
- }
211
-
212
- // ============================================================================
213
- // Strategy Definitions
214
- // ============================================================================
215
-
216
- /**
217
- * Decomposition strategy types
218
- */
219
- export type DecompositionStrategy =
220
- | "file-based"
221
- | "feature-based"
222
- | "risk-based"
223
- | "research-based"
224
- | "auto";
225
-
226
- /**
227
- * Strategy definition with keywords, guidelines, and anti-patterns
228
- */
229
- export interface StrategyDefinition {
230
- name: DecompositionStrategy;
231
- description: string;
232
- keywords: string[];
233
- guidelines: string[];
234
- antiPatterns: string[];
235
- examples: string[];
236
- }
237
-
238
- /**
239
- * Strategy definitions for task decomposition
240
- */
241
- export const STRATEGIES: Record<
242
- Exclude<DecompositionStrategy, "auto">,
243
- StrategyDefinition
244
- > = {
245
- "file-based": {
246
- name: "file-based",
247
- description:
248
- "Group by file type or directory. Best for refactoring, migrations, and pattern changes across codebase.",
249
- keywords: [
250
- "refactor",
251
- "migrate",
252
- "update all",
253
- "rename",
254
- "replace",
255
- "convert",
256
- "upgrade",
257
- "deprecate",
258
- "remove",
259
- "cleanup",
260
- "lint",
261
- "format",
262
- ],
263
- guidelines: [
264
- "Group files by directory or type (e.g., all components, all tests)",
265
- "Minimize cross-directory dependencies within a subtask",
266
- "Handle shared types/utilities first if they change",
267
- "Each subtask should be a complete transformation of its file set",
268
- "Consider import/export relationships when grouping",
269
- ],
270
- antiPatterns: [
271
- "Don't split tightly coupled files across subtasks",
272
- "Don't group files that have no relationship",
273
- "Don't forget to update imports when moving/renaming",
274
- ],
275
- examples: [
276
- "Migrate all components to new API → split by component directory",
277
- "Rename userId to accountId → split by module (types first, then consumers)",
278
- "Update all tests to use new matcher → split by test directory",
279
- ],
280
- },
281
- "feature-based": {
282
- name: "feature-based",
283
- description:
284
- "Vertical slices with UI + API + data. Best for new features and adding functionality.",
285
- keywords: [
286
- "add",
287
- "implement",
288
- "build",
289
- "create",
290
- "feature",
291
- "new",
292
- "integrate",
293
- "connect",
294
- "enable",
295
- "support",
296
- ],
297
- guidelines: [
298
- "Each subtask is a complete vertical slice (UI + logic + data)",
299
- "Start with data layer/types, then logic, then UI",
300
- "Keep related components together (form + validation + submission)",
301
- "Separate concerns that can be developed independently",
302
- "Consider user-facing features as natural boundaries",
303
- ],
304
- antiPatterns: [
305
- "Don't split a single feature across multiple subtasks",
306
- "Don't create subtasks that can't be tested independently",
307
- "Don't forget integration points between features",
308
- ],
309
- examples: [
310
- "Add user auth → [OAuth setup, Session management, Protected routes]",
311
- "Build dashboard → [Data fetching, Chart components, Layout/navigation]",
312
- "Add search → [Search API, Search UI, Results display]",
313
- ],
314
- },
315
- "risk-based": {
316
- name: "risk-based",
317
- description:
318
- "Isolate high-risk changes, add tests first. Best for bug fixes, security issues, and critical changes.",
319
- keywords: [
320
- "fix",
321
- "bug",
322
- "security",
323
- "vulnerability",
324
- "critical",
325
- "urgent",
326
- "hotfix",
327
- "patch",
328
- "audit",
329
- "review",
330
- ],
331
- guidelines: [
332
- "Write tests FIRST to capture expected behavior",
333
- "Isolate the risky change to minimize blast radius",
334
- "Add monitoring/logging around the change",
335
- "Create rollback plan as part of the task",
336
- "Audit similar code for the same issue",
337
- ],
338
- antiPatterns: [
339
- "Don't make multiple risky changes in one subtask",
340
- "Don't skip tests for 'simple' fixes",
341
- "Don't forget to check for similar issues elsewhere",
342
- ],
343
- examples: [
344
- "Fix auth bypass → [Add regression test, Fix vulnerability, Audit similar endpoints]",
345
- "Fix race condition → [Add test reproducing issue, Implement fix, Add concurrency tests]",
346
- "Security audit → [Scan for vulnerabilities, Fix critical issues, Document remaining risks]",
347
- ],
348
- },
349
- "research-based": {
350
- name: "research-based",
351
- description:
352
- "Parallel search across multiple sources, then synthesize. Best for investigation, learning, and discovery tasks.",
353
- keywords: [
354
- "research",
355
- "investigate",
356
- "explore",
357
- "find out",
358
- "discover",
359
- "understand",
360
- "learn about",
361
- "analyze",
362
- "what is",
363
- "what are",
364
- "how does",
365
- "how do",
366
- "why does",
367
- "why do",
368
- "compare",
369
- "evaluate",
370
- "study",
371
- "look up",
372
- "look into",
373
- "search for",
374
- "dig into",
375
- "figure out",
376
- "debug options",
377
- "debug levers",
378
- "configuration options",
379
- "environment variables",
380
- "available options",
381
- "documentation",
382
- ],
383
- guidelines: [
384
- "Split by information source (PDFs, repos, history, web)",
385
- "Each agent searches with different query angles",
386
- "Include a synthesis subtask that depends on all search subtasks",
387
- "Use pdf-brain for documentation/books if available",
388
- "Use repo-crawl for GitHub repos if URL provided",
389
- "Use cass for past agent session history",
390
- "Assign NO files to research subtasks (read-only)",
391
- ],
392
- antiPatterns: [
393
- "Don't have one agent search everything sequentially",
394
- "Don't skip synthesis - raw search results need consolidation",
395
- "Don't forget to check tool availability before assigning sources",
396
- ],
397
- examples: [
398
- "Research auth patterns → [Search PDFs, Search repos, Search history, Synthesize]",
399
- "Investigate error → [Search cass for similar errors, Search repo for error handling, Synthesize]",
400
- "Learn about library → [Search docs, Search examples, Search issues, Synthesize findings]",
401
- ],
402
- },
403
- };
404
-
405
- /**
406
- * Analyze task description and select best decomposition strategy
407
- *
408
- * @param task - Task description
409
- * @returns Selected strategy with reasoning
410
- */
411
- export function selectStrategy(task: string): {
412
- strategy: Exclude<DecompositionStrategy, "auto">;
413
- confidence: number;
414
- reasoning: string;
415
- alternatives: Array<{
416
- strategy: Exclude<DecompositionStrategy, "auto">;
417
- score: number;
418
- }>;
419
- } {
420
- const taskLower = task.toLowerCase();
421
-
422
- // Score each strategy based on keyword matches
423
- const scores: Record<Exclude<DecompositionStrategy, "auto">, number> = {
424
- "file-based": 0,
425
- "feature-based": 0,
426
- "risk-based": 0,
427
- "research-based": 0,
428
- };
429
-
430
- for (const [strategyName, definition] of Object.entries(STRATEGIES)) {
431
- const name = strategyName as Exclude<DecompositionStrategy, "auto">;
432
- for (const keyword of definition.keywords) {
433
- // Use word boundary matching to avoid "debug" matching "bug"
434
- // For multi-word keywords, just check includes (they're specific enough)
435
- if (keyword.includes(" ")) {
436
- if (taskLower.includes(keyword)) {
437
- scores[name] += 1;
438
- }
439
- } else {
440
- // Single word: use word boundary regex
441
- const regex = new RegExp(`\\b${keyword}\\b`, "i");
442
- if (regex.test(taskLower)) {
443
- scores[name] += 1;
444
- }
445
- }
446
- }
447
- }
448
-
449
- // Find the winner
450
- const entries = Object.entries(scores) as Array<
451
- [Exclude<DecompositionStrategy, "auto">, number]
452
- >;
453
- entries.sort((a, b) => b[1] - a[1]);
454
-
455
- const [winner, winnerScore] = entries[0];
456
- const [runnerUp, runnerUpScore] = entries[1] || [null, 0];
457
-
458
- // Calculate confidence based on margin
459
- const totalScore = entries.reduce((sum, [, score]) => sum + score, 0);
460
- const confidence =
461
- totalScore > 0
462
- ? Math.min(0.95, 0.5 + (winnerScore - runnerUpScore) / totalScore)
463
- : 0.5; // Default to 50% if no keywords matched
464
-
465
- // Build reasoning
466
- let reasoning: string;
467
- if (winnerScore === 0) {
468
- reasoning = `No strong keyword signals. Defaulting to feature-based as it's most versatile.`;
469
- } else {
470
- const matchedKeywords = STRATEGIES[winner].keywords.filter((k) =>
471
- taskLower.includes(k),
472
- );
473
- reasoning = `Matched keywords: ${matchedKeywords.join(", ")}. ${STRATEGIES[winner].description}`;
474
- }
475
-
476
- // If no keywords matched, default to feature-based
477
- const finalStrategy = winnerScore === 0 ? "feature-based" : winner;
478
-
479
- return {
480
- strategy: finalStrategy,
481
- confidence,
482
- reasoning,
483
- alternatives: entries
484
- .filter(([s]) => s !== finalStrategy)
485
- .map(([strategy, score]) => ({ strategy, score })),
486
- };
487
- }
488
-
489
- /**
490
- * Format strategy-specific guidelines for the decomposition prompt
491
- */
492
- export function formatStrategyGuidelines(
493
- strategy: Exclude<DecompositionStrategy, "auto">,
494
- ): string {
495
- const def = STRATEGIES[strategy];
496
-
497
- const guidelines = def.guidelines.map((g) => `- ${g}`).join("\n");
498
- const antiPatterns = def.antiPatterns.map((a) => `- ${a}`).join("\n");
499
- const examples = def.examples.map((e) => `- ${e}`).join("\n");
500
-
501
- return `## Strategy: ${strategy}
502
-
503
- ${def.description}
504
-
505
- ### Guidelines
506
- ${guidelines}
507
-
508
- ### Anti-Patterns (Avoid These)
509
- ${antiPatterns}
510
-
511
- ### Examples
512
- ${examples}`;
513
- }
514
-
515
- // ============================================================================
516
- // Prompt Templates
517
- // ============================================================================
518
-
519
- /**
520
- * Prompt for decomposing a task into parallelizable subtasks.
521
- *
522
- * Used by swarm:decompose to instruct the agent on how to break down work.
523
- * The agent responds with a BeadTree that gets validated.
524
- */
525
- export const DECOMPOSITION_PROMPT = `You are decomposing a task into parallelizable subtasks for a swarm of agents.
526
-
527
- ## Task
528
- {task}
529
-
530
- {context_section}
531
-
532
- ## MANDATORY: Beads Issue Tracking
533
-
534
- **Every subtask MUST become a bead.** This is non-negotiable.
535
-
536
- After decomposition, the coordinator will:
537
- 1. Create an epic bead for the overall task
538
- 2. Create child beads for each subtask
539
- 3. Track progress through bead status updates
540
- 4. Close beads with summaries when complete
541
-
542
- Agents MUST update their bead status as they work. No silent progress.
543
-
544
- ## Requirements
545
-
546
- 1. **Break into 2-{max_subtasks} independent subtasks** that can run in parallel
547
- 2. **Assign files** - each subtask must specify which files it will modify
548
- 3. **No file overlap** - files cannot appear in multiple subtasks (they get exclusive locks)
549
- 4. **Order by dependency** - if subtask B needs subtask A's output, A must come first in the array
550
- 5. **Estimate complexity** - 1 (trivial) to 5 (complex)
551
- 6. **Plan aggressively** - break down more than you think necessary, smaller is better
552
-
553
- ## Response Format
554
-
555
- Respond with a JSON object matching this schema:
556
-
557
- \`\`\`typescript
558
- {
559
- epic: {
560
- title: string, // Epic title for the beads tracker
561
- description?: string // Brief description of the overall goal
562
- },
563
- subtasks: [
564
- {
565
- title: string, // What this subtask accomplishes
566
- description?: string, // Detailed instructions for the agent
567
- files: string[], // Files this subtask will modify (globs allowed)
568
- dependencies: number[], // Indices of subtasks this depends on (0-indexed)
569
- estimated_complexity: 1-5 // Effort estimate
570
- },
571
- // ... more subtasks
572
- ]
573
- }
574
- \`\`\`
575
-
576
- ## Guidelines
577
-
578
- - **Plan aggressively** - when in doubt, split further. 3 small tasks > 1 medium task
579
- - **Prefer smaller, focused subtasks** over large complex ones
580
- - **Include test files** in the same subtask as the code they test
581
- - **Consider shared types** - if multiple files share types, handle that first
582
- - **Think about imports** - changes to exported APIs affect downstream files
583
- - **Explicit > implicit** - spell out what each subtask should do, don't assume
584
-
585
- ## File Assignment Examples
586
-
587
- - Schema change: \`["src/schemas/user.ts", "src/schemas/index.ts"]\`
588
- - Component + test: \`["src/components/Button.tsx", "src/components/Button.test.tsx"]\`
589
- - API route: \`["src/app/api/users/route.ts"]\`
590
-
591
- Now decompose the task:`;
592
-
593
- /**
594
- * Prompt template for spawned subtask agents.
595
- *
596
- * Each agent receives this prompt with their specific subtask details filled in.
597
- * The prompt establishes context, constraints, and expectations.
598
- */
599
- export const SUBTASK_PROMPT = `You are a swarm agent working on a subtask of a larger epic.
600
-
601
- ## Your Identity
602
- - **Agent Name**: {agent_name}
603
- - **Bead ID**: {bead_id}
604
- - **Epic ID**: {epic_id}
605
-
606
- ## Your Subtask
607
- **Title**: {subtask_title}
608
-
609
- {subtask_description}
610
-
611
- ## File Scope
612
- You have exclusive reservations for these files:
613
- {file_list}
614
-
615
- **CRITICAL**: Only modify files in your reservation. If you need to modify other files,
616
- send a message to the coordinator requesting the change.
617
-
618
- ## Shared Context
619
- {shared_context}
620
-
621
- ## MANDATORY: Beads Tracking
622
-
623
- You MUST keep your bead updated as you work:
624
-
625
- 1. **Your bead is already in_progress** - don't change this unless blocked
626
- 2. **If blocked**: \`bd update {bead_id} --status blocked\` and message coordinator
627
- 3. **When done**: Use \`swarm_complete\` - it closes your bead automatically
628
- 4. **Discovered issues**: Create new beads with \`bd create "issue" -t bug\`
629
-
630
- **Never work silently.** Your bead status is how the swarm tracks progress.
631
-
632
- ## MANDATORY: Swarm Mail Communication
633
-
634
- You MUST communicate with other agents:
635
-
636
- 1. **Report progress** every significant milestone (not just at the end)
637
- 2. **Ask questions** if requirements are unclear - don't guess
638
- 3. **Announce blockers** immediately - don't spin trying to fix alone
639
- 4. **Coordinate on shared concerns** - if you see something affecting other agents, say so
640
-
641
- Use Swarm Mail for all communication:
642
- \`\`\`
643
- swarmmail_send(
644
- to: ["coordinator" or specific agent],
645
- subject: "Brief subject",
646
- body: "Message content",
647
- thread_id: "{epic_id}"
648
- )
649
- \`\`\`
650
-
651
- ## Coordination Protocol
652
-
653
- 1. **Start**: Your bead is already marked in_progress
654
- 2. **Progress**: Use swarm_progress to report status updates
655
- 3. **Blocked**: Report immediately via Swarm Mail - don't spin
656
- 4. **Complete**: Use swarm_complete when done - it handles:
657
- - Closing your bead with a summary
658
- - Releasing file reservations
659
- - Notifying the coordinator
660
-
661
- ## Self-Evaluation
662
-
663
- Before calling swarm_complete, evaluate your work:
664
- - Type safety: Does it compile without errors?
665
- - No obvious bugs: Did you handle edge cases?
666
- - Follows patterns: Does it match existing code style?
667
- - Readable: Would another developer understand it?
668
-
669
- If evaluation fails, fix the issues before completing.
670
-
671
- ## Planning Your Work
672
-
673
- Before writing code:
674
- 1. **Read the files** you're assigned to understand current state
675
- 2. **Plan your approach** - what changes, in what order?
676
- 3. **Identify risks** - what could go wrong? What dependencies?
677
- 4. **Communicate your plan** via Swarm Mail if non-trivial
678
-
679
- Begin work on your subtask now.`;
680
-
681
- /**
682
- * Streamlined subtask prompt (V2) - uses Swarm Mail and beads
683
- *
684
- * This is a cleaner version of SUBTASK_PROMPT that's easier to parse.
685
- * Agents MUST use Swarm Mail for communication and beads for tracking.
686
- *
687
- * Supports {error_context} placeholder for retry prompts.
688
- */
689
- export const SUBTASK_PROMPT_V2 = `You are a swarm agent working on: **{subtask_title}**
690
-
691
- ## [IDENTITY]
692
- Agent: (assigned at spawn)
693
- Bead: {bead_id}
694
- Epic: {epic_id}
695
-
696
- ## [TASK]
697
- {subtask_description}
698
-
699
- ## [FILES]
700
- Reserved (exclusive):
701
- {file_list}
702
-
703
- Only modify these files. Need others? Message the coordinator.
704
-
705
- ## [CONTEXT]
706
- {shared_context}
707
-
708
- {compressed_context}
709
-
710
- {error_context}
711
-
712
- ## [MANDATORY: SWARM MAIL]
713
-
714
- **YOU MUST USE SWARM MAIL FOR ALL COORDINATION.** This is non-negotiable.
715
-
716
- ### Initialize FIRST (before any work)
717
- \`\`\`
718
- swarmmail_init(project_path="$PWD", task_description="{subtask_title}")
719
- \`\`\`
720
-
721
- ### Reserve Files (if not already reserved by coordinator)
722
- \`\`\`
723
- swarmmail_reserve(paths=[...files...], reason="{bead_id}: {subtask_title}")
724
- \`\`\`
725
-
726
- ### Check Inbox Regularly
727
- \`\`\`
728
- swarmmail_inbox() # Check for coordinator messages
729
- swarmmail_read_message(message_id=N) # Read specific message
730
- \`\`\`
731
-
732
- ### Report Progress (REQUIRED - don't work silently)
733
- \`\`\`
734
- swarmmail_send(
735
- to=["coordinator"],
736
- subject="Progress: {bead_id}",
737
- body="<what you did, blockers, questions>",
738
- thread_id="{epic_id}"
739
- )
740
- \`\`\`
741
-
742
- ### When Blocked
743
- \`\`\`
744
- swarmmail_send(
745
- to=["coordinator"],
746
- subject="BLOCKED: {bead_id}",
747
- body="<blocker description, what you need>",
748
- importance="high",
749
- thread_id="{epic_id}"
750
- )
751
- beads_update(id="{bead_id}", status="blocked")
752
- \`\`\`
753
-
754
- ### Release Files When Done
755
- \`\`\`
756
- swarmmail_release() # Or let swarm_complete handle it
757
- \`\`\`
758
-
759
- ## [OTHER TOOLS]
760
- ### Beads
761
- - beads_update(id, status) - Mark blocked if stuck
762
- - beads_create(title, type) - Log new bugs found
763
-
764
- ### Skills (if available)
765
- - skills_list() - Discover available skills
766
- - skills_use(name) - Activate skill for specialized guidance
767
-
768
- ### Completion (REQUIRED)
769
- - swarm_complete(project_key, agent_name, bead_id, summary, files_touched)
770
-
771
- ## [LEARNING]
772
- As you work, note reusable patterns, best practices, or domain insights:
773
- - If you discover something that would help future agents, consider creating a skill
774
- - Use skills_create to codify patterns for the project
775
- - Good skills have clear "when to use" descriptions with actionable instructions
776
- - Skills make swarms smarter over time
777
-
778
- ## [WORKFLOW]
779
- 1. **swarmmail_init** - Initialize session FIRST
780
- 2. Read assigned files
781
- 3. Implement changes
782
- 4. **swarmmail_send** - Report progress to coordinator
783
- 5. Verify (typecheck)
784
- 6. **swarm_complete** - Mark done, release reservations
785
-
786
- **CRITICAL: Never work silently. Send progress updates via swarmmail_send every significant milestone.**
787
-
788
- Begin now.`;
789
-
790
- /**
791
- * Format the V2 subtask prompt for a specific agent
792
- */
793
- export function formatSubtaskPromptV2(params: {
794
- bead_id: string;
795
- epic_id: string;
796
- subtask_title: string;
797
- subtask_description: string;
798
- files: string[];
799
- shared_context?: string;
800
- compressed_context?: string;
801
- error_context?: string;
802
- }): string {
803
- const fileList =
804
- params.files.length > 0
805
- ? params.files.map((f) => `- \`${f}\``).join("\n")
806
- : "(no specific files - use judgment)";
807
-
808
- const compressedSection = params.compressed_context
809
- ? params.compressed_context
810
- : "";
811
-
812
- const errorSection = params.error_context ? params.error_context : "";
813
-
814
- return SUBTASK_PROMPT_V2.replace(/{bead_id}/g, params.bead_id)
815
- .replace(/{epic_id}/g, params.epic_id)
816
- .replace("{subtask_title}", params.subtask_title)
817
- .replace(
818
- "{subtask_description}",
819
- params.subtask_description || "(see title)",
820
- )
821
- .replace("{file_list}", fileList)
822
- .replace("{shared_context}", params.shared_context || "(none)")
823
- .replace("{compressed_context}", compressedSection)
824
- .replace("{error_context}", errorSection);
825
- }
826
-
827
- /**
828
- * Prompt for self-evaluation before completing a subtask.
829
- *
830
- * Agents use this to assess their work quality before marking complete.
831
- */
832
- export const EVALUATION_PROMPT = `Evaluate the work completed for this subtask.
833
-
834
- ## Subtask
835
- **Bead ID**: {bead_id}
836
- **Title**: {subtask_title}
837
-
838
- ## Files Modified
839
- {files_touched}
840
-
841
- ## Evaluation Criteria
842
-
843
- For each criterion, assess passed/failed and provide brief feedback:
844
-
845
- 1. **type_safe**: Code compiles without TypeScript errors
846
- 2. **no_bugs**: No obvious bugs, edge cases handled
847
- 3. **patterns**: Follows existing codebase patterns and conventions
848
- 4. **readable**: Code is clear and maintainable
849
-
850
- ## Response Format
851
-
852
- \`\`\`json
853
- {
854
- "passed": boolean, // Overall pass/fail
855
- "criteria": {
856
- "type_safe": { "passed": boolean, "feedback": string },
857
- "no_bugs": { "passed": boolean, "feedback": string },
858
- "patterns": { "passed": boolean, "feedback": string },
859
- "readable": { "passed": boolean, "feedback": string }
860
- },
861
- "overall_feedback": string,
862
- "retry_suggestion": string | null // If failed, what to fix
863
- }
864
- \`\`\`
865
-
866
- If any criterion fails, the overall evaluation fails and retry_suggestion
867
- should describe what needs to be fixed.`;
868
-
869
- // ============================================================================
870
- // Errors
871
- // ============================================================================
872
-
873
- export class SwarmError extends Error {
874
- constructor(
875
- message: string,
876
- public readonly operation: string,
877
- public readonly details?: unknown,
878
- ) {
879
- super(message);
880
- this.name = "SwarmError";
881
- }
882
- }
883
-
884
- export class DecompositionError extends SwarmError {
885
- constructor(
886
- message: string,
887
- public readonly zodError?: z.ZodError,
888
- ) {
889
- super(message, "decompose", zodError?.issues);
890
- }
891
- }
892
-
893
- // ============================================================================
894
- // Helper Functions
895
- // ============================================================================
896
-
897
- /**
898
- * Format the decomposition prompt with actual values
899
- */
900
- function formatDecompositionPrompt(
901
- task: string,
902
- maxSubtasks: number,
903
- context?: string,
904
- ): string {
905
- const contextSection = context
906
- ? `## Additional Context\n${context}`
907
- : "## Additional Context\n(none provided)";
908
-
909
- return DECOMPOSITION_PROMPT.replace("{task}", task)
910
- .replace("{max_subtasks}", maxSubtasks.toString())
911
- .replace("{context_section}", contextSection);
912
- }
913
-
914
- /**
915
- * Format the subtask prompt for a specific agent
916
- */
917
- export function formatSubtaskPrompt(params: {
918
- agent_name: string;
919
- bead_id: string;
920
- epic_id: string;
921
- subtask_title: string;
922
- subtask_description: string;
923
- files: string[];
924
- shared_context?: string;
925
- }): string {
926
- const fileList = params.files.map((f) => `- \`${f}\``).join("\n");
927
-
928
- return SUBTASK_PROMPT.replace("{agent_name}", params.agent_name)
929
- .replace("{bead_id}", params.bead_id)
930
- .replace(/{epic_id}/g, params.epic_id)
931
- .replace("{subtask_title}", params.subtask_title)
932
- .replace("{subtask_description}", params.subtask_description || "(none)")
933
- .replace("{file_list}", fileList || "(no files assigned)")
934
- .replace("{shared_context}", params.shared_context || "(none)");
935
- }
936
-
937
- /**
938
- * Format the evaluation prompt
939
- */
940
- export function formatEvaluationPrompt(params: {
941
- bead_id: string;
942
- subtask_title: string;
943
- files_touched: string[];
944
- }): string {
945
- const filesList = params.files_touched.map((f) => `- \`${f}\``).join("\n");
946
-
947
- return EVALUATION_PROMPT.replace("{bead_id}", params.bead_id)
948
- .replace("{subtask_title}", params.subtask_title)
949
- .replace("{files_touched}", filesList || "(no files recorded)");
950
- }
951
-
952
- /**
953
- * Query beads for subtasks of an epic
954
- */
955
- async function queryEpicSubtasks(epicId: string): Promise<Bead[]> {
956
- // Check if beads is available
957
- const beadsAvailable = await isToolAvailable("beads");
958
- if (!beadsAvailable) {
959
- warnMissingTool("beads");
960
- return []; // Return empty - swarm can still function without status tracking
961
- }
962
-
963
- const result = await Bun.$`bd list --parent ${epicId} --json`
964
- .quiet()
965
- .nothrow();
966
-
967
- if (result.exitCode !== 0) {
968
- // Don't throw - just return empty and log error prominently
969
- console.error(
970
- `[swarm] ERROR: Failed to query subtasks for epic ${epicId}:`,
971
- result.stderr.toString(),
972
- );
973
- return [];
974
- }
975
-
976
- try {
977
- const parsed = JSON.parse(result.stdout.toString());
978
- return z.array(BeadSchema).parse(parsed);
979
- } catch (error) {
980
- if (error instanceof z.ZodError) {
981
- console.error(
982
- `[swarm] ERROR: Invalid bead data for epic ${epicId}:`,
983
- error.message,
984
- );
985
- return [];
986
- }
987
- console.error(
988
- `[swarm] ERROR: Failed to parse beads for epic ${epicId}:`,
989
- error,
990
- );
991
- throw error;
992
- }
993
- }
994
-
995
- /**
996
- * Query Agent Mail for swarm thread messages
997
- */
998
- async function querySwarmMessages(
999
- projectKey: string,
1000
- threadId: string,
1001
- ): Promise<number> {
1002
- // Check if agent-mail is available
1003
- const agentMailAvailable = await isToolAvailable("agent-mail");
1004
- if (!agentMailAvailable) {
1005
- // Don't warn here - it's checked elsewhere
1006
- return 0;
1007
- }
1008
-
1009
- try {
1010
- // Use embedded swarm-mail inbox to count messages in thread
1011
- const inbox = await getSwarmInbox({
1012
- projectPath: projectKey,
1013
- agentName: "coordinator", // Dummy agent name for thread query
1014
- limit: 5,
1015
- includeBodies: false,
1016
- });
1017
-
1018
- // Count messages that match the thread ID
1019
- const threadMessages = inbox.messages.filter(
1020
- (m) => m.thread_id === threadId,
1021
- );
1022
- return threadMessages.length;
1023
- } catch (error) {
1024
- // Thread might not exist yet, or query failed
1025
- console.warn(
1026
- `[swarm] Failed to query swarm messages for thread ${threadId}:`,
1027
- error,
1028
- );
1029
- return 0;
1030
- }
1031
- }
1032
-
1033
- /**
1034
- * Format a progress message for Agent Mail
1035
- */
1036
- function formatProgressMessage(progress: AgentProgress): string {
1037
- const lines = [
1038
- `**Status**: ${progress.status}`,
1039
- progress.progress_percent !== undefined
1040
- ? `**Progress**: ${progress.progress_percent}%`
1041
- : null,
1042
- progress.message ? `**Message**: ${progress.message}` : null,
1043
- progress.files_touched && progress.files_touched.length > 0
1044
- ? `**Files touched**:\n${progress.files_touched.map((f) => `- \`${f}\``).join("\n")}`
1045
- : null,
1046
- progress.blockers && progress.blockers.length > 0
1047
- ? `**Blockers**:\n${progress.blockers.map((b) => `- ${b}`).join("\n")}`
1048
- : null,
1049
- ];
1050
-
1051
- return lines.filter(Boolean).join("\n\n");
1052
- }
1053
-
1054
- // ============================================================================
1055
- // CASS History Integration
1056
- // ============================================================================
1057
-
1058
- /**
1059
- * CASS search result from similar past tasks
1060
- */
1061
- interface CassSearchResult {
1062
- query: string;
1063
- results: Array<{
1064
- source_path: string;
1065
- line: number;
1066
- agent: string;
1067
- preview: string;
1068
- score: number;
1069
- }>;
1070
- }
1071
-
1072
- /**
1073
- * CASS query result with status
1074
- */
1075
- type CassQueryResult =
1076
- | { status: "unavailable" }
1077
- | { status: "failed"; error?: string }
1078
- | { status: "empty"; query: string }
1079
- | { status: "success"; data: CassSearchResult };
1080
-
1081
- /**
1082
- * Query CASS for similar past tasks
1083
- *
1084
- * @param task - Task description to search for
1085
- * @param limit - Maximum results to return
1086
- * @returns Structured result with status indicator
1087
- */
1088
- async function queryCassHistory(
1089
- task: string,
1090
- limit: number = 3,
1091
- ): Promise<CassQueryResult> {
1092
- // Check if CASS is available first
1093
- const cassAvailable = await isToolAvailable("cass");
1094
- if (!cassAvailable) {
1095
- warnMissingTool("cass");
1096
- return { status: "unavailable" };
1097
- }
1098
-
1099
- try {
1100
- const result = await Bun.$`cass search ${task} --limit ${limit} --json`
1101
- .quiet()
1102
- .nothrow();
1103
-
1104
- if (result.exitCode !== 0) {
1105
- const error = result.stderr.toString();
1106
- console.warn(
1107
- `[swarm] CASS search failed (exit ${result.exitCode}):`,
1108
- error,
1109
- );
1110
- return { status: "failed", error };
1111
- }
1112
-
1113
- const output = result.stdout.toString();
1114
- if (!output.trim()) {
1115
- return { status: "empty", query: task };
1116
- }
1117
-
1118
- try {
1119
- const parsed = JSON.parse(output);
1120
- const searchResult: CassSearchResult = {
1121
- query: task,
1122
- results: Array.isArray(parsed) ? parsed : parsed.results || [],
1123
- };
1124
-
1125
- if (searchResult.results.length === 0) {
1126
- return { status: "empty", query: task };
1127
- }
1128
-
1129
- return { status: "success", data: searchResult };
1130
- } catch (error) {
1131
- console.warn(`[swarm] Failed to parse CASS output:`, error);
1132
- return { status: "failed", error: String(error) };
1133
- }
1134
- } catch (error) {
1135
- console.error(`[swarm] CASS query error:`, error);
1136
- return { status: "failed", error: String(error) };
1137
- }
1138
- }
1139
-
1140
- /**
1141
- * Format CASS history for inclusion in decomposition prompt
1142
- */
1143
- function formatCassHistoryForPrompt(history: CassSearchResult): string {
1144
- if (history.results.length === 0) {
1145
- return "";
1146
- }
1147
-
1148
- const lines = [
1149
- "## Similar Past Tasks",
1150
- "",
1151
- "These similar tasks were found in agent history:",
1152
- "",
1153
- ...history.results.slice(0, 3).map((r, i) => {
1154
- const preview = r.preview.slice(0, 200).replace(/\n/g, " ");
1155
- return `${i + 1}. [${r.agent}] ${preview}...`;
1156
- }),
1157
- "",
1158
- "Consider patterns that worked in these past tasks.",
1159
- "",
1160
- ];
1161
-
1162
- return lines.join("\n");
1163
- }
1164
-
1165
- // ============================================================================
1166
- // Tool Definitions
1167
- // ============================================================================
1168
-
1169
- /**
1170
- * Select the best decomposition strategy for a task
1171
- *
1172
- * Analyzes task description and recommends a strategy with reasoning.
1173
- * Use this before swarm_plan_prompt to understand the recommended approach.
1174
- */
1175
- export const swarm_select_strategy = tool({
1176
- description:
1177
- "Analyze task and recommend decomposition strategy (file-based, feature-based, or risk-based)",
1178
- args: {
1179
- task: tool.schema.string().min(1).describe("Task description to analyze"),
1180
- codebase_context: tool.schema
1181
- .string()
1182
- .optional()
1183
- .describe("Optional codebase context (file structure, tech stack, etc.)"),
1184
- },
1185
- async execute(args) {
1186
- const result = selectStrategy(args.task);
1187
-
1188
- // Enhance reasoning with codebase context if provided
1189
- let enhancedReasoning = result.reasoning;
1190
- if (args.codebase_context) {
1191
- enhancedReasoning += `\n\nCodebase context considered: ${args.codebase_context.slice(0, 200)}...`;
1192
- }
1193
-
1194
- return JSON.stringify(
1195
- {
1196
- strategy: result.strategy,
1197
- confidence: Math.round(result.confidence * 100) / 100,
1198
- reasoning: enhancedReasoning,
1199
- description: STRATEGIES[result.strategy].description,
1200
- guidelines: STRATEGIES[result.strategy].guidelines,
1201
- anti_patterns: STRATEGIES[result.strategy].antiPatterns,
1202
- alternatives: result.alternatives.map((alt) => ({
1203
- strategy: alt.strategy,
1204
- description: STRATEGIES[alt.strategy].description,
1205
- score: alt.score,
1206
- })),
1207
- },
1208
- null,
1209
- 2,
1210
- );
1211
- },
1212
- });
1213
-
1214
- /**
1215
- * Strategy-specific decomposition prompt template
1216
- */
1217
- const STRATEGY_DECOMPOSITION_PROMPT = `You are decomposing a task into parallelizable subtasks for a swarm of agents.
1218
-
1219
- ## Task
1220
- {task}
1221
-
1222
- {strategy_guidelines}
1223
-
1224
- {context_section}
1225
-
1226
- {cass_history}
1227
-
1228
- {skills_context}
1229
-
1230
- ## MANDATORY: Beads Issue Tracking
1231
-
1232
- **Every subtask MUST become a bead.** This is non-negotiable.
1233
-
1234
- After decomposition, the coordinator will:
1235
- 1. Create an epic bead for the overall task
1236
- 2. Create child beads for each subtask
1237
- 3. Track progress through bead status updates
1238
- 4. Close beads with summaries when complete
1239
-
1240
- Agents MUST update their bead status as they work. No silent progress.
1241
-
1242
- ## Requirements
1243
-
1244
- 1. **Break into 2-{max_subtasks} independent subtasks** that can run in parallel
1245
- 2. **Assign files** - each subtask must specify which files it will modify
1246
- 3. **No file overlap** - files cannot appear in multiple subtasks (they get exclusive locks)
1247
- 4. **Order by dependency** - if subtask B needs subtask A's output, A must come first in the array
1248
- 5. **Estimate complexity** - 1 (trivial) to 5 (complex)
1249
- 6. **Plan aggressively** - break down more than you think necessary, smaller is better
1250
-
1251
- ## Response Format
1252
-
1253
- Respond with a JSON object matching this schema:
1254
-
1255
- \`\`\`typescript
1256
- {
1257
- epic: {
1258
- title: string, // Epic title for the beads tracker
1259
- description?: string // Brief description of the overall goal
1260
- },
1261
- subtasks: [
1262
- {
1263
- title: string, // What this subtask accomplishes
1264
- description?: string, // Detailed instructions for the agent
1265
- files: string[], // Files this subtask will modify (globs allowed)
1266
- dependencies: number[], // Indices of subtasks this depends on (0-indexed)
1267
- estimated_complexity: 1-5 // Effort estimate
1268
- },
1269
- // ... more subtasks
1270
- ]
1271
- }
1272
- \`\`\`
1273
-
1274
- Now decompose the task:`;
1275
-
1276
- /**
1277
- * Generate a strategy-specific planning prompt
1278
- *
1279
- * Higher-level than swarm_decompose - includes strategy selection and guidelines.
1280
- * Use this when you want the full planning experience with strategy-specific advice.
1281
- */
1282
- export const swarm_plan_prompt = tool({
1283
- description:
1284
- "Generate strategy-specific decomposition prompt. Auto-selects strategy or uses provided one. Queries CASS for similar tasks.",
1285
- args: {
1286
- task: tool.schema.string().min(1).describe("Task description to decompose"),
1287
- strategy: tool.schema
1288
- .enum(["file-based", "feature-based", "risk-based", "auto"])
1289
- .optional()
1290
- .describe("Decomposition strategy (default: auto-detect)"),
1291
- max_subtasks: tool.schema
1292
- .number()
1293
- .int()
1294
- .min(2)
1295
-
1296
- .default(5)
1297
- .describe("Maximum number of subtasks (default: 5)"),
1298
- context: tool.schema
1299
- .string()
1300
- .optional()
1301
- .describe("Additional context (codebase info, constraints, etc.)"),
1302
- query_cass: tool.schema
1303
- .boolean()
1304
- .optional()
1305
- .describe("Query CASS for similar past tasks (default: true)"),
1306
- cass_limit: tool.schema
1307
- .number()
1308
- .int()
1309
- .min(1)
1310
-
1311
- .optional()
1312
- .describe("Max CASS results to include (default: 3)"),
1313
- include_skills: tool.schema
1314
- .boolean()
1315
- .optional()
1316
- .describe("Include available skills in context (default: true)"),
1317
- },
1318
- async execute(args) {
1319
- // Select strategy
1320
- let selectedStrategy: Exclude<DecompositionStrategy, "auto">;
1321
- let strategyReasoning: string;
1322
-
1323
- if (args.strategy && args.strategy !== "auto") {
1324
- selectedStrategy = args.strategy;
1325
- strategyReasoning = `User-specified strategy: ${selectedStrategy}`;
1326
- } else {
1327
- const selection = selectStrategy(args.task);
1328
- selectedStrategy = selection.strategy;
1329
- strategyReasoning = selection.reasoning;
1330
- }
1331
-
1332
- // Query CASS for similar past tasks
1333
- let cassContext = "";
1334
- let cassResultInfo: {
1335
- queried: boolean;
1336
- results_found?: number;
1337
- included_in_context?: boolean;
1338
- reason?: string;
1339
- };
1340
-
1341
- if (args.query_cass !== false) {
1342
- const cassResult = await queryCassHistory(
1343
- args.task,
1344
- args.cass_limit ?? 3,
1345
- );
1346
- if (cassResult.status === "success") {
1347
- cassContext = formatCassHistoryForPrompt(cassResult.data);
1348
- cassResultInfo = {
1349
- queried: true,
1350
- results_found: cassResult.data.results.length,
1351
- included_in_context: true,
1352
- };
1353
- } else {
1354
- cassResultInfo = {
1355
- queried: true,
1356
- results_found: 0,
1357
- included_in_context: false,
1358
- reason: cassResult.status,
1359
- };
1360
- }
1361
- } else {
1362
- cassResultInfo = { queried: false, reason: "disabled" };
1363
- }
1364
-
1365
- // Fetch skills context
1366
- let skillsContext = "";
1367
- let skillsInfo: { included: boolean; count?: number; relevant?: string[] } =
1368
- {
1369
- included: false,
1370
- };
1371
-
1372
- if (args.include_skills !== false) {
1373
- const allSkills = await listSkills();
1374
- if (allSkills.length > 0) {
1375
- skillsContext = await getSkillsContextForSwarm();
1376
- const relevantSkills = await findRelevantSkills(args.task);
1377
- skillsInfo = {
1378
- included: true,
1379
- count: allSkills.length,
1380
- relevant: relevantSkills,
1381
- };
1382
-
1383
- // Add suggestion for relevant skills
1384
- if (relevantSkills.length > 0) {
1385
- skillsContext += `\n\n**Suggested skills for this task**: ${relevantSkills.join(", ")}`;
1386
- }
1387
- }
1388
- }
1389
-
1390
- // Format strategy guidelines
1391
- const strategyGuidelines = formatStrategyGuidelines(selectedStrategy);
1392
-
1393
- // Combine user context
1394
- const contextSection = args.context
1395
- ? `## Additional Context\n${args.context}`
1396
- : "## Additional Context\n(none provided)";
1397
-
1398
- // Build the prompt
1399
- const prompt = STRATEGY_DECOMPOSITION_PROMPT.replace("{task}", args.task)
1400
- .replace("{strategy_guidelines}", strategyGuidelines)
1401
- .replace("{context_section}", contextSection)
1402
- .replace("{cass_history}", cassContext || "")
1403
- .replace("{skills_context}", skillsContext || "")
1404
- .replace("{max_subtasks}", (args.max_subtasks ?? 5).toString());
1405
-
1406
- return JSON.stringify(
1407
- {
1408
- prompt,
1409
- strategy: {
1410
- selected: selectedStrategy,
1411
- reasoning: strategyReasoning,
1412
- guidelines: STRATEGIES[selectedStrategy].guidelines,
1413
- anti_patterns: STRATEGIES[selectedStrategy].antiPatterns,
1414
- },
1415
- expected_schema: "BeadTree",
1416
- schema_hint: {
1417
- epic: { title: "string", description: "string?" },
1418
- subtasks: [
1419
- {
1420
- title: "string",
1421
- description: "string?",
1422
- files: "string[]",
1423
- dependencies: "number[]",
1424
- estimated_complexity: "1-5",
1425
- },
1426
- ],
1427
- },
1428
- validation_note:
1429
- "Parse agent response as JSON and validate with swarm_validate_decomposition",
1430
- cass_history: cassResultInfo,
1431
- skills: skillsInfo,
1432
- },
1433
- null,
1434
- 2,
1435
- );
1436
- },
1437
- });
1438
-
1439
- /**
1440
- * Decompose a task into a bead tree
1441
- *
1442
- * This is a PROMPT tool - it returns a prompt for the agent to respond to.
1443
- * The agent's response (JSON) should be validated with BeadTreeSchema.
1444
- *
1445
- * Optionally queries CASS for similar past tasks to inform decomposition.
1446
- */
1447
- export const swarm_decompose = tool({
1448
- description:
1449
- "Generate decomposition prompt for breaking task into parallelizable subtasks. Optionally queries CASS for similar past tasks.",
1450
- args: {
1451
- task: tool.schema.string().min(1).describe("Task description to decompose"),
1452
- max_subtasks: tool.schema
1453
- .number()
1454
- .int()
1455
- .min(2)
1456
-
1457
- .default(5)
1458
- .describe("Maximum number of subtasks (default: 5)"),
1459
- context: tool.schema
1460
- .string()
1461
- .optional()
1462
- .describe("Additional context (codebase info, constraints, etc.)"),
1463
- query_cass: tool.schema
1464
- .boolean()
1465
- .optional()
1466
- .describe("Query CASS for similar past tasks (default: true)"),
1467
- cass_limit: tool.schema
1468
- .number()
1469
- .int()
1470
- .min(1)
1471
-
1472
- .optional()
1473
- .describe("Max CASS results to include (default: 3)"),
1474
- },
1475
- async execute(args) {
1476
- // Query CASS for similar past tasks
1477
- let cassContext = "";
1478
- let cassResultInfo: {
1479
- queried: boolean;
1480
- results_found?: number;
1481
- included_in_context?: boolean;
1482
- reason?: string;
1483
- };
1484
-
1485
- if (args.query_cass !== false) {
1486
- const cassResult = await queryCassHistory(
1487
- args.task,
1488
- args.cass_limit ?? 3,
1489
- );
1490
- if (cassResult.status === "success") {
1491
- cassContext = formatCassHistoryForPrompt(cassResult.data);
1492
- cassResultInfo = {
1493
- queried: true,
1494
- results_found: cassResult.data.results.length,
1495
- included_in_context: true,
1496
- };
1497
- } else {
1498
- cassResultInfo = {
1499
- queried: true,
1500
- results_found: 0,
1501
- included_in_context: false,
1502
- reason: cassResult.status,
1503
- };
1504
- }
1505
- } else {
1506
- cassResultInfo = { queried: false, reason: "disabled" };
1507
- }
1508
-
1509
- // Combine user context with CASS history
1510
- const fullContext = [args.context, cassContext]
1511
- .filter(Boolean)
1512
- .join("\n\n");
1513
-
1514
- const prompt = formatDecompositionPrompt(
1515
- args.task,
1516
- args.max_subtasks ?? 5,
1517
- fullContext || undefined,
1518
- );
1519
-
1520
- // Return the prompt and schema info for the caller
1521
- return JSON.stringify(
1522
- {
1523
- prompt,
1524
- expected_schema: "BeadTree",
1525
- schema_hint: {
1526
- epic: { title: "string", description: "string?" },
1527
- subtasks: [
1528
- {
1529
- title: "string",
1530
- description: "string?",
1531
- files: "string[]",
1532
- dependencies: "number[]",
1533
- estimated_complexity: "1-5",
1534
- },
1535
- ],
1536
- },
1537
- validation_note:
1538
- "Parse agent response as JSON and validate with BeadTreeSchema from schemas/bead.ts",
1539
- cass_history: cassResultInfo,
1540
- },
1541
- null,
1542
- 2,
1543
- );
1544
- },
1545
- });
1546
-
1547
- /**
1548
- * Validate a decomposition response from an agent
1549
- *
1550
- * Use this after the agent responds to swarm:decompose to validate the structure.
1551
- */
1552
- export const swarm_validate_decomposition = tool({
1553
- description: "Validate a decomposition response against BeadTreeSchema",
1554
- args: {
1555
- response: tool.schema
1556
- .string()
1557
- .describe("JSON response from agent (BeadTree format)"),
1558
- },
1559
- async execute(args) {
1560
- try {
1561
- const parsed = JSON.parse(args.response);
1562
- const validated = BeadTreeSchema.parse(parsed);
1563
-
1564
- // Additional validation: check for file conflicts
1565
- const allFiles = new Set<string>();
1566
- const conflicts: string[] = [];
1567
-
1568
- for (const subtask of validated.subtasks) {
1569
- for (const file of subtask.files) {
1570
- if (allFiles.has(file)) {
1571
- conflicts.push(file);
1572
- }
1573
- allFiles.add(file);
1574
- }
1575
- }
1576
-
1577
- if (conflicts.length > 0) {
1578
- return JSON.stringify(
1579
- {
1580
- valid: false,
1581
- error: `File conflicts detected: ${conflicts.join(", ")}`,
1582
- hint: "Each file can only be assigned to one subtask",
1583
- },
1584
- null,
1585
- 2,
1586
- );
1587
- }
1588
-
1589
- // Check dependency indices are valid
1590
- for (let i = 0; i < validated.subtasks.length; i++) {
1591
- const deps = validated.subtasks[i].dependencies;
1592
- for (const dep of deps) {
1593
- // Check bounds first
1594
- if (dep < 0 || dep >= validated.subtasks.length) {
1595
- return JSON.stringify(
1596
- {
1597
- valid: false,
1598
- error: `Invalid dependency: subtask ${i} depends on ${dep}, but only ${validated.subtasks.length} subtasks exist (indices 0-${validated.subtasks.length - 1})`,
1599
- hint: "Dependency index is out of bounds",
1600
- },
1601
- null,
1602
- 2,
1603
- );
1604
- }
1605
- // Check forward references
1606
- if (dep >= i) {
1607
- return JSON.stringify(
1608
- {
1609
- valid: false,
1610
- error: `Invalid dependency: subtask ${i} depends on ${dep}, but dependencies must be earlier in the array`,
1611
- hint: "Reorder subtasks so dependencies come before dependents",
1612
- },
1613
- null,
1614
- 2,
1615
- );
1616
- }
1617
- }
1618
- }
1619
-
1620
- // Check for instruction conflicts between subtasks
1621
- const instructionConflicts = detectInstructionConflicts(
1622
- validated.subtasks,
1623
- );
1624
-
1625
- return JSON.stringify(
1626
- {
1627
- valid: true,
1628
- bead_tree: validated,
1629
- stats: {
1630
- subtask_count: validated.subtasks.length,
1631
- total_files: allFiles.size,
1632
- total_complexity: validated.subtasks.reduce(
1633
- (sum, s) => sum + s.estimated_complexity,
1634
- 0,
1635
- ),
1636
- },
1637
- // Include conflicts as warnings (not blocking)
1638
- warnings:
1639
- instructionConflicts.length > 0
1640
- ? {
1641
- instruction_conflicts: instructionConflicts,
1642
- hint: "Review these potential conflicts between subtask instructions",
1643
- }
1644
- : undefined,
1645
- },
1646
- null,
1647
- 2,
1648
- );
1649
- } catch (error) {
1650
- if (error instanceof z.ZodError) {
1651
- return JSON.stringify(
1652
- {
1653
- valid: false,
1654
- error: "Schema validation failed",
1655
- details: error.issues,
1656
- },
1657
- null,
1658
- 2,
1659
- );
1660
- }
1661
- if (error instanceof SyntaxError) {
1662
- return JSON.stringify(
1663
- {
1664
- valid: false,
1665
- error: "Invalid JSON",
1666
- details: error.message,
1667
- },
1668
- null,
1669
- 2,
1670
- );
1671
- }
1672
- throw error;
1673
- }
1674
- },
1675
- });
1676
-
1677
- /**
1678
- * Get status of a swarm by epic ID
1679
- *
1680
- * Requires project_key to query Agent Mail for message counts.
1681
- */
1682
- export const swarm_status = tool({
1683
- description: "Get status of a swarm by epic ID",
1684
- args: {
1685
- epic_id: tool.schema.string().describe("Epic bead ID (e.g., bd-abc123)"),
1686
- project_key: tool.schema
1687
- .string()
1688
- .describe("Project path (for Agent Mail queries)"),
1689
- },
1690
- async execute(args) {
1691
- // Query subtasks from beads
1692
- const subtasks = await queryEpicSubtasks(args.epic_id);
1693
-
1694
- // Count statuses
1695
- const statusCounts = {
1696
- running: 0,
1697
- completed: 0,
1698
- failed: 0,
1699
- blocked: 0,
1700
- };
1701
-
1702
- const agents: SpawnedAgent[] = [];
1703
-
1704
- for (const bead of subtasks) {
1705
- // Map bead status to agent status
1706
- let agentStatus: SpawnedAgent["status"] = "pending";
1707
- switch (bead.status) {
1708
- case "in_progress":
1709
- agentStatus = "running";
1710
- statusCounts.running++;
1711
- break;
1712
- case "closed":
1713
- agentStatus = "completed";
1714
- statusCounts.completed++;
1715
- break;
1716
- case "blocked":
1717
- agentStatus = "pending"; // Blocked treated as pending for swarm
1718
- statusCounts.blocked++;
1719
- break;
1720
- default:
1721
- // open = pending
1722
- break;
1723
- }
1724
-
1725
- agents.push({
1726
- bead_id: bead.id,
1727
- agent_name: "", // We don't track this in beads
1728
- status: agentStatus,
1729
- files: [], // Would need to parse from description
1730
- });
1731
- }
1732
-
1733
- // Query Agent Mail for message activity
1734
- const messageCount = await querySwarmMessages(
1735
- args.project_key,
1736
- args.epic_id,
1737
- );
1738
-
1739
- const status: SwarmStatus = {
1740
- epic_id: args.epic_id,
1741
- total_agents: subtasks.length,
1742
- running: statusCounts.running,
1743
- completed: statusCounts.completed,
1744
- failed: statusCounts.failed,
1745
- blocked: statusCounts.blocked,
1746
- agents,
1747
- last_update: new Date().toISOString(),
1748
- };
1749
-
1750
- // Validate and return
1751
- const validated = SwarmStatusSchema.parse(status);
1752
-
1753
- return JSON.stringify(
1754
- {
1755
- ...validated,
1756
- message_count: messageCount,
1757
- progress_percent:
1758
- subtasks.length > 0
1759
- ? Math.round((statusCounts.completed / subtasks.length) * 100)
1760
- : 0,
1761
- },
1762
- null,
1763
- 2,
1764
- );
1765
- },
1766
- });
1767
-
1768
- /**
1769
- * Report progress on a subtask
1770
- *
1771
- * Takes explicit agent identity since tools don't have persistent state.
1772
- */
1773
- export const swarm_progress = tool({
1774
- description: "Report progress on a subtask to coordinator",
1775
- args: {
1776
- project_key: tool.schema.string().describe("Project path"),
1777
- agent_name: tool.schema.string().describe("Your Agent Mail name"),
1778
- bead_id: tool.schema.string().describe("Subtask bead ID"),
1779
- status: tool.schema
1780
- .enum(["in_progress", "blocked", "completed", "failed"])
1781
- .describe("Current status"),
1782
- message: tool.schema
1783
- .string()
1784
- .optional()
1785
- .describe("Progress message or blockers"),
1786
- progress_percent: tool.schema
1787
- .number()
1788
- .min(0)
1789
- .max(100)
1790
- .optional()
1791
- .describe("Completion percentage"),
1792
- files_touched: tool.schema
1793
- .array(tool.schema.string())
1794
- .optional()
1795
- .describe("Files modified so far"),
1796
- },
1797
- async execute(args) {
1798
- // Build progress report
1799
- const progress: AgentProgress = {
1800
- bead_id: args.bead_id,
1801
- agent_name: args.agent_name,
1802
- status: args.status,
1803
- progress_percent: args.progress_percent,
1804
- message: args.message,
1805
- files_touched: args.files_touched,
1806
- timestamp: new Date().toISOString(),
1807
- };
1808
-
1809
- // Validate
1810
- const validated = AgentProgressSchema.parse(progress);
1811
-
1812
- // Update bead status if needed
1813
- if (args.status === "blocked" || args.status === "in_progress") {
1814
- const beadStatus = args.status === "blocked" ? "blocked" : "in_progress";
1815
- await Bun.$`bd update ${args.bead_id} --status ${beadStatus} --json`
1816
- .quiet()
1817
- .nothrow();
1818
- }
1819
-
1820
- // Extract epic ID from bead ID (e.g., bd-abc123.1 -> bd-abc123)
1821
- const epicId = args.bead_id.includes(".")
1822
- ? args.bead_id.split(".")[0]
1823
- : args.bead_id;
1824
-
1825
- // Send progress message to thread using embedded swarm-mail
1826
- await sendSwarmMessage({
1827
- projectPath: args.project_key,
1828
- fromAgent: args.agent_name,
1829
- toAgents: [], // Coordinator will pick it up from thread
1830
- subject: `Progress: ${args.bead_id} - ${args.status}`,
1831
- body: formatProgressMessage(validated),
1832
- threadId: epicId,
1833
- importance: args.status === "blocked" ? "high" : "normal",
1834
- });
1835
-
1836
- return `Progress reported: ${args.status}${args.progress_percent !== undefined ? ` (${args.progress_percent}%)` : ""}`;
1837
- },
1838
- });
1839
-
1840
- /**
1841
- * UBS scan result schema
1842
- */
1843
- interface UbsScanResult {
1844
- exitCode: number;
1845
- bugs: Array<{
1846
- file: string;
1847
- line: number;
1848
- severity: string;
1849
- message: string;
1850
- category: string;
1851
- }>;
1852
- summary: {
1853
- total: number;
1854
- critical: number;
1855
- high: number;
1856
- medium: number;
1857
- low: number;
1858
- };
1859
- }
1860
-
1861
- // ============================================================================
1862
- // Verification Gate
1863
- // ============================================================================
1864
-
1865
- /**
1866
- * Verification Gate result - tracks each verification step
1867
- *
1868
- * Based on the Gate Function from superpowers:
1869
- * 1. IDENTIFY: What command proves this claim?
1870
- * 2. RUN: Execute the FULL command (fresh, complete)
1871
- * 3. READ: Full output, check exit code, count failures
1872
- * 4. VERIFY: Does output confirm the claim?
1873
- * 5. ONLY THEN: Make the claim
1874
- */
1875
- interface VerificationStep {
1876
- name: string;
1877
- command: string;
1878
- passed: boolean;
1879
- exitCode: number;
1880
- output?: string;
1881
- error?: string;
1882
- skipped?: boolean;
1883
- skipReason?: string;
1884
- }
1885
-
1886
- interface VerificationGateResult {
1887
- passed: boolean;
1888
- steps: VerificationStep[];
1889
- summary: string;
1890
- blockers: string[];
1891
- }
1892
-
1893
- /**
1894
- * Run typecheck verification
1895
- *
1896
- * Attempts to run TypeScript type checking on the project.
1897
- * Falls back gracefully if tsc is not available.
1898
- */
1899
- async function runTypecheckVerification(): Promise<VerificationStep> {
1900
- const step: VerificationStep = {
1901
- name: "typecheck",
1902
- command: "tsc --noEmit",
1903
- passed: false,
1904
- exitCode: -1,
1905
- };
1906
-
1907
- try {
1908
- // Check if tsconfig.json exists in current directory
1909
- const tsconfigExists = await Bun.file("tsconfig.json").exists();
1910
- if (!tsconfigExists) {
1911
- step.skipped = true;
1912
- step.skipReason = "No tsconfig.json found";
1913
- step.passed = true; // Don't block if no TypeScript
1914
- return step;
1915
- }
1916
-
1917
- const result = await Bun.$`tsc --noEmit`.quiet().nothrow();
1918
- step.exitCode = result.exitCode;
1919
- step.passed = result.exitCode === 0;
1920
-
1921
- if (!step.passed) {
1922
- step.error = result.stderr.toString().slice(0, 1000); // Truncate for context
1923
- step.output = result.stdout.toString().slice(0, 1000);
1924
- }
1925
- } catch (error) {
1926
- step.skipped = true;
1927
- step.skipReason = `tsc not available: ${error instanceof Error ? error.message : String(error)}`;
1928
- step.passed = true; // Don't block if tsc unavailable
1929
- }
1930
-
1931
- return step;
1932
- }
1933
-
1934
- /**
1935
- * Run test verification for specific files
1936
- *
1937
- * Attempts to find and run tests related to the touched files.
1938
- * Uses common test patterns (*.test.ts, *.spec.ts, __tests__/).
1939
- */
1940
- async function runTestVerification(
1941
- filesTouched: string[],
1942
- ): Promise<VerificationStep> {
1943
- const step: VerificationStep = {
1944
- name: "tests",
1945
- command: "bun test <related-files>",
1946
- passed: false,
1947
- exitCode: -1,
1948
- };
1949
-
1950
- if (filesTouched.length === 0) {
1951
- step.skipped = true;
1952
- step.skipReason = "No files touched";
1953
- step.passed = true;
1954
- return step;
1955
- }
1956
-
1957
- // Find test files related to touched files
1958
- const testPatterns: string[] = [];
1959
- for (const file of filesTouched) {
1960
- // Skip if already a test file
1961
- if (file.includes(".test.") || file.includes(".spec.")) {
1962
- testPatterns.push(file);
1963
- continue;
1964
- }
1965
-
1966
- // Look for corresponding test file
1967
- const baseName = file.replace(/\.(ts|tsx|js|jsx)$/, "");
1968
- testPatterns.push(`${baseName}.test.ts`);
1969
- testPatterns.push(`${baseName}.test.tsx`);
1970
- testPatterns.push(`${baseName}.spec.ts`);
1971
- }
1972
-
1973
- // Check if any test files exist
1974
- const existingTests: string[] = [];
1975
- for (const pattern of testPatterns) {
1976
- try {
1977
- const exists = await Bun.file(pattern).exists();
1978
- if (exists) {
1979
- existingTests.push(pattern);
1980
- }
1981
- } catch {
1982
- // File doesn't exist, skip
1983
- }
1984
- }
1985
-
1986
- if (existingTests.length === 0) {
1987
- step.skipped = true;
1988
- step.skipReason = "No related test files found";
1989
- step.passed = true;
1990
- return step;
1991
- }
1992
-
1993
- try {
1994
- step.command = `bun test ${existingTests.join(" ")}`;
1995
- const result = await Bun.$`bun test ${existingTests}`.quiet().nothrow();
1996
- step.exitCode = result.exitCode;
1997
- step.passed = result.exitCode === 0;
1998
-
1999
- if (!step.passed) {
2000
- step.error = result.stderr.toString().slice(0, 1000);
2001
- step.output = result.stdout.toString().slice(0, 1000);
2002
- }
2003
- } catch (error) {
2004
- step.skipped = true;
2005
- step.skipReason = `Test runner failed: ${error instanceof Error ? error.message : String(error)}`;
2006
- step.passed = true; // Don't block if test runner unavailable
2007
- }
2008
-
2009
- return step;
2010
- }
2011
-
2012
- /**
2013
- * Run the full Verification Gate
2014
- *
2015
- * Implements the Gate Function (IDENTIFY → RUN → READ → VERIFY → CLAIM):
2016
- * 1. UBS scan (already exists)
2017
- * 2. Typecheck
2018
- * 3. Tests for touched files
2019
- *
2020
- * All steps must pass (or be skipped with valid reason) to proceed.
2021
- */
2022
- async function runVerificationGate(
2023
- filesTouched: string[],
2024
- skipUbs: boolean = false,
2025
- ): Promise<VerificationGateResult> {
2026
- const steps: VerificationStep[] = [];
2027
- const blockers: string[] = [];
2028
-
2029
- // Step 1: UBS scan
2030
- if (!skipUbs && filesTouched.length > 0) {
2031
- const ubsResult = await runUbsScan(filesTouched);
2032
- if (ubsResult) {
2033
- const ubsStep: VerificationStep = {
2034
- name: "ubs_scan",
2035
- command: `ubs scan ${filesTouched.join(" ")}`,
2036
- passed: ubsResult.summary.critical === 0,
2037
- exitCode: ubsResult.exitCode,
2038
- };
2039
-
2040
- if (!ubsStep.passed) {
2041
- ubsStep.error = `Found ${ubsResult.summary.critical} critical bugs`;
2042
- blockers.push(
2043
- `UBS found ${ubsResult.summary.critical} critical bug(s). Try: Run 'ubs scan ${filesTouched.join(" ")}' to see details, fix critical bugs in reported files, or use skip_ubs_scan=true to bypass (not recommended).`,
2044
- );
2045
- }
2046
-
2047
- steps.push(ubsStep);
2048
- } else {
2049
- steps.push({
2050
- name: "ubs_scan",
2051
- command: "ubs scan",
2052
- passed: true,
2053
- exitCode: 0,
2054
- skipped: true,
2055
- skipReason: "UBS not available",
2056
- });
2057
- }
2058
- }
2059
-
2060
- // Step 2: Typecheck
2061
- const typecheckStep = await runTypecheckVerification();
2062
- steps.push(typecheckStep);
2063
- if (!typecheckStep.passed && !typecheckStep.skipped) {
2064
- blockers.push(
2065
- `Typecheck failed: ${typecheckStep.error?.slice(0, 100) || "type errors found"}. Try: Run 'tsc --noEmit' to see full errors, check tsconfig.json configuration, or fix reported type errors in modified files.`,
2066
- );
2067
- }
2068
-
2069
- // Step 3: Tests
2070
- const testStep = await runTestVerification(filesTouched);
2071
- steps.push(testStep);
2072
- if (!testStep.passed && !testStep.skipped) {
2073
- blockers.push(
2074
- `Tests failed: ${testStep.error?.slice(0, 100) || "test failures"}. Try: Run 'bun test ${testStep.command.split(" ").slice(2).join(" ")}' to see full output, check test assertions, or fix failing tests in modified files.`,
2075
- );
2076
- }
2077
-
2078
- // Build summary
2079
- const passedCount = steps.filter((s) => s.passed).length;
2080
- const skippedCount = steps.filter((s) => s.skipped).length;
2081
- const failedCount = steps.filter((s) => !s.passed && !s.skipped).length;
2082
-
2083
- const summary =
2084
- failedCount === 0
2085
- ? `Verification passed: ${passedCount} checks passed, ${skippedCount} skipped`
2086
- : `Verification FAILED: ${failedCount} checks failed, ${passedCount} passed, ${skippedCount} skipped`;
2087
-
2088
- return {
2089
- passed: failedCount === 0,
2090
- steps,
2091
- summary,
2092
- blockers,
2093
- };
2094
- }
2095
-
2096
- /**
2097
- * Run UBS scan on files before completion
2098
- *
2099
- * @param files - Files to scan
2100
- * @returns Scan result or null if UBS not available
2101
- */
2102
- async function runUbsScan(files: string[]): Promise<UbsScanResult | null> {
2103
- if (files.length === 0) {
2104
- return null;
2105
- }
2106
-
2107
- // Check if UBS is available first
2108
- const ubsAvailable = await isToolAvailable("ubs");
2109
- if (!ubsAvailable) {
2110
- warnMissingTool("ubs");
2111
- return null;
2112
- }
2113
-
2114
- try {
2115
- // Run UBS scan with JSON output
2116
- const result = await Bun.$`ubs scan ${files.join(" ")} --json`
2117
- .quiet()
2118
- .nothrow();
2119
-
2120
- const output = result.stdout.toString();
2121
- if (!output.trim()) {
2122
- return {
2123
- exitCode: result.exitCode,
2124
- bugs: [],
2125
- summary: { total: 0, critical: 0, high: 0, medium: 0, low: 0 },
2126
- };
2127
- }
2128
-
2129
- try {
2130
- const parsed = JSON.parse(output);
2131
-
2132
- // Basic validation of structure
2133
- if (typeof parsed !== "object" || parsed === null) {
2134
- throw new Error("UBS output is not an object");
2135
- }
2136
- if (!Array.isArray(parsed.bugs)) {
2137
- console.warn("[swarm] UBS output missing bugs array, using empty");
2138
- }
2139
- if (typeof parsed.summary !== "object" || parsed.summary === null) {
2140
- console.warn("[swarm] UBS output missing summary object, using empty");
2141
- }
2142
-
2143
- return {
2144
- exitCode: result.exitCode,
2145
- bugs: Array.isArray(parsed.bugs) ? parsed.bugs : [],
2146
- summary: parsed.summary || {
2147
- total: 0,
2148
- critical: 0,
2149
- high: 0,
2150
- medium: 0,
2151
- low: 0,
2152
- },
2153
- };
2154
- } catch (error) {
2155
- // UBS output wasn't JSON - this is an error condition
2156
- console.error(
2157
- `[swarm] CRITICAL: UBS scan failed to parse JSON output because output is malformed:`,
2158
- error,
2159
- );
2160
- console.error(
2161
- `[swarm] Raw output: ${output}. Try: Run 'ubs doctor' to check installation, verify UBS version with 'ubs --version' (need v1.0.0+), or check if UBS supports --json flag.`,
2162
- );
2163
- return {
2164
- exitCode: result.exitCode,
2165
- bugs: [],
2166
- summary: { total: 0, critical: 0, high: 0, medium: 0, low: 0 },
2167
- };
2168
- }
2169
- } catch {
2170
- return null;
2171
- }
2172
- }
2173
-
2174
- /**
2175
- * Broadcast context updates to all agents in the epic
2176
- *
2177
- * Enables mid-task coordination by sharing discoveries, warnings, or blockers
2178
- * with all agents working on the same epic. Agents can broadcast without
2179
- * waiting for task completion.
2180
- *
2181
- * Based on "Patterns for Building AI Agents" p.31: "Ensure subagents can share context along the way"
2182
- */
2183
- export const swarm_broadcast = tool({
2184
- description:
2185
- "Broadcast context update to all agents working on the same epic",
2186
- args: {
2187
- project_path: tool.schema
2188
- .string()
2189
- .describe("Absolute path to project root"),
2190
- agent_name: tool.schema
2191
- .string()
2192
- .describe("Name of the agent broadcasting the message"),
2193
- epic_id: tool.schema.string().describe("Epic ID (e.g., bd-abc123)"),
2194
- message: tool.schema
2195
- .string()
2196
- .describe("Context update to share (what changed, what was learned)"),
2197
- importance: tool.schema
2198
- .enum(["info", "warning", "blocker"])
2199
- .default("info")
2200
- .describe("Priority level (default: info)"),
2201
- files_affected: tool.schema
2202
- .array(tool.schema.string())
2203
- .optional()
2204
- .describe("Files this context relates to"),
2205
- },
2206
- async execute(args, ctx) {
2207
- // Extract bead_id from context if available (for traceability)
2208
- const beadId = (ctx as { beadId?: string }).beadId || "unknown";
2209
-
2210
- // Format the broadcast message
2211
- const body = [
2212
- `## Context Update`,
2213
- "",
2214
- `**From**: ${args.agent_name} (${beadId})`,
2215
- `**Priority**: ${args.importance.toUpperCase()}`,
2216
- "",
2217
- args.message,
2218
- "",
2219
- args.files_affected && args.files_affected.length > 0
2220
- ? `**Files affected**:\n${args.files_affected.map((f) => `- \`${f}\``).join("\n")}`
2221
- : "",
2222
- ]
2223
- .filter(Boolean)
2224
- .join("\n");
2225
-
2226
- // Map importance to Agent Mail importance
2227
- const mailImportance =
2228
- args.importance === "blocker"
2229
- ? "urgent"
2230
- : args.importance === "warning"
2231
- ? "high"
2232
- : "normal";
2233
-
2234
- // Send as broadcast to thread using embedded swarm-mail
2235
- await sendSwarmMessage({
2236
- projectPath: args.project_path,
2237
- fromAgent: args.agent_name,
2238
- toAgents: [], // Broadcast to thread
2239
- subject: `[${args.importance.toUpperCase()}] Context update from ${args.agent_name}`,
2240
- body,
2241
- threadId: args.epic_id,
2242
- importance: mailImportance,
2243
- ackRequired: args.importance === "blocker",
2244
- });
2245
-
2246
- return JSON.stringify(
2247
- {
2248
- broadcast: true,
2249
- epic_id: args.epic_id,
2250
- from: args.agent_name,
2251
- bead_id: beadId,
2252
- importance: args.importance,
2253
- recipients: "all agents in epic",
2254
- ack_required: args.importance === "blocker",
2255
- },
2256
- null,
2257
- 2,
2258
- );
2259
- },
2260
- });
2261
-
2262
- /**
2263
- * Mark a subtask as complete
2264
- *
2265
- * Implements the Verification Gate (from superpowers):
2266
- * 1. IDENTIFY: What commands prove this claim?
2267
- * 2. RUN: Execute verification (UBS, typecheck, tests)
2268
- * 3. READ: Check exit codes and output
2269
- * 4. VERIFY: All checks must pass
2270
- * 5. ONLY THEN: Close the bead
2271
- *
2272
- * Closes bead, releases reservations, notifies coordinator.
2273
- */
2274
- export const swarm_complete = tool({
2275
- description:
2276
- "Mark subtask complete with Verification Gate. Runs UBS scan, typecheck, and tests before allowing completion.",
2277
- args: {
2278
- project_key: tool.schema.string().describe("Project path"),
2279
- agent_name: tool.schema.string().describe("Your Agent Mail name"),
2280
- bead_id: tool.schema.string().describe("Subtask bead ID"),
2281
- summary: tool.schema.string().describe("Brief summary of work done"),
2282
- evaluation: tool.schema
2283
- .string()
2284
- .optional()
2285
- .describe("Self-evaluation JSON (Evaluation schema)"),
2286
- files_touched: tool.schema
2287
- .array(tool.schema.string())
2288
- .optional()
2289
- .describe("Files modified - will be verified (UBS, typecheck, tests)"),
2290
- skip_ubs_scan: tool.schema
2291
- .boolean()
2292
- .optional()
2293
- .describe("Skip UBS bug scan (default: false)"),
2294
- skip_verification: tool.schema
2295
- .boolean()
2296
- .optional()
2297
- .describe(
2298
- "Skip ALL verification (UBS, typecheck, tests). Use sparingly! (default: false)",
2299
- ),
2300
- },
2301
- async execute(args) {
2302
- // Run Verification Gate unless explicitly skipped
2303
- let verificationResult: VerificationGateResult | null = null;
2304
-
2305
- if (!args.skip_verification && args.files_touched?.length) {
2306
- verificationResult = await runVerificationGate(
2307
- args.files_touched,
2308
- args.skip_ubs_scan ?? false,
2309
- );
2310
-
2311
- // Block completion if verification failed
2312
- if (!verificationResult.passed) {
2313
- return JSON.stringify(
2314
- {
2315
- success: false,
2316
- error: "Verification Gate FAILED - fix issues before completing",
2317
- verification: {
2318
- passed: false,
2319
- summary: verificationResult.summary,
2320
- blockers: verificationResult.blockers,
2321
- steps: verificationResult.steps.map((s) => ({
2322
- name: s.name,
2323
- passed: s.passed,
2324
- skipped: s.skipped,
2325
- skipReason: s.skipReason,
2326
- error: s.error?.slice(0, 200),
2327
- })),
2328
- },
2329
- hint:
2330
- verificationResult.blockers.length > 0
2331
- ? `Fix these issues: ${verificationResult.blockers.map((b, i) => `${i + 1}. ${b}`).join(", ")}. Use skip_verification=true only as last resort.`
2332
- : "Fix the failing checks and try again. Use skip_verification=true only as last resort.",
2333
- gate_function:
2334
- "IDENTIFY → RUN → READ → VERIFY → CLAIM (you are at VERIFY, claim blocked)",
2335
- },
2336
- null,
2337
- 2,
2338
- );
2339
- }
2340
- }
2341
-
2342
- // Legacy UBS-only path for backward compatibility (when no files_touched)
2343
- let ubsResult: UbsScanResult | null = null;
2344
- if (
2345
- !args.skip_verification &&
2346
- !verificationResult &&
2347
- args.files_touched?.length &&
2348
- !args.skip_ubs_scan
2349
- ) {
2350
- ubsResult = await runUbsScan(args.files_touched);
2351
-
2352
- // Block completion if critical bugs found
2353
- if (ubsResult && ubsResult.summary.critical > 0) {
2354
- return JSON.stringify(
2355
- {
2356
- success: false,
2357
- error: `UBS found ${ubsResult.summary.critical} critical bug(s) that must be fixed before completing`,
2358
- ubs_scan: {
2359
- critical_count: ubsResult.summary.critical,
2360
- bugs: ubsResult.bugs.filter((b) => b.severity === "critical"),
2361
- },
2362
- hint: `Fix these critical bugs: ${ubsResult.bugs
2363
- .filter((b) => b.severity === "critical")
2364
- .map((b) => `${b.file}:${b.line} - ${b.message}`)
2365
- .slice(0, 3)
2366
- .join(
2367
- "; ",
2368
- )}. Try: Run 'ubs scan ${args.files_touched?.join(" ") || "."} --json' for full report, fix reported issues, or use skip_ubs_scan=true to bypass (not recommended).`,
2369
- },
2370
- null,
2371
- 2,
2372
- );
2373
- }
2374
- }
2375
-
2376
- // Parse and validate evaluation if provided
2377
- let parsedEvaluation: Evaluation | undefined;
2378
- if (args.evaluation) {
2379
- try {
2380
- parsedEvaluation = EvaluationSchema.parse(JSON.parse(args.evaluation));
2381
- } catch (error) {
2382
- return JSON.stringify(
2383
- {
2384
- success: false,
2385
- error: "Invalid evaluation format",
2386
- details: error instanceof z.ZodError ? error.issues : String(error),
2387
- },
2388
- null,
2389
- 2,
2390
- );
2391
- }
2392
-
2393
- // If evaluation failed, don't complete
2394
- if (!parsedEvaluation.passed) {
2395
- return JSON.stringify(
2396
- {
2397
- success: false,
2398
- error: "Self-evaluation failed",
2399
- retry_suggestion: parsedEvaluation.retry_suggestion,
2400
- feedback: parsedEvaluation.overall_feedback,
2401
- },
2402
- null,
2403
- 2,
2404
- );
2405
- }
2406
- }
2407
-
2408
- // Close the bead
2409
- const closeResult =
2410
- await Bun.$`bd close ${args.bead_id} --reason ${args.summary} --json`
2411
- .quiet()
2412
- .nothrow();
2413
-
2414
- if (closeResult.exitCode !== 0) {
2415
- throw new SwarmError(
2416
- `Failed to close bead because bd close command failed: ${closeResult.stderr.toString()}. Try: Verify bead exists and is not already closed with 'bd show ${args.bead_id}', check if bead ID is correct with 'beads_query()', or use beads_close tool directly.`,
2417
- "complete",
2418
- );
2419
- }
2420
-
2421
- // Release file reservations for this agent using embedded swarm-mail
2422
- try {
2423
- await releaseSwarmFiles({
2424
- projectPath: args.project_key,
2425
- agentName: args.agent_name,
2426
- // Release all reservations for this agent
2427
- });
2428
- } catch (error) {
2429
- // Release might fail (e.g., no reservations existed)
2430
- // This is non-fatal - log and continue
2431
- console.warn(
2432
- `[swarm] Failed to release file reservations for ${args.agent_name}:`,
2433
- error,
2434
- );
2435
- }
2436
-
2437
- // Extract epic ID
2438
- const epicId = args.bead_id.includes(".")
2439
- ? args.bead_id.split(".")[0]
2440
- : args.bead_id;
2441
-
2442
- // Send completion message using embedded swarm-mail
2443
- const completionBody = [
2444
- `## Subtask Complete: ${args.bead_id}`,
2445
- "",
2446
- `**Summary**: ${args.summary}`,
2447
- "",
2448
- parsedEvaluation
2449
- ? `**Self-Evaluation**: ${parsedEvaluation.passed ? "PASSED" : "FAILED"}`
2450
- : "",
2451
- parsedEvaluation?.overall_feedback
2452
- ? `**Feedback**: ${parsedEvaluation.overall_feedback}`
2453
- : "",
2454
- ]
2455
- .filter(Boolean)
2456
- .join("\n");
2457
-
2458
- await sendSwarmMessage({
2459
- projectPath: args.project_key,
2460
- fromAgent: args.agent_name,
2461
- toAgents: [], // Thread broadcast
2462
- subject: `Complete: ${args.bead_id}`,
2463
- body: completionBody,
2464
- threadId: epicId,
2465
- importance: "normal",
2466
- });
2467
-
2468
- return JSON.stringify(
2469
- {
2470
- success: true,
2471
- bead_id: args.bead_id,
2472
- closed: true,
2473
- reservations_released: true,
2474
- message_sent: true,
2475
- verification_gate: verificationResult
2476
- ? {
2477
- passed: true,
2478
- summary: verificationResult.summary,
2479
- steps: verificationResult.steps.map((s) => ({
2480
- name: s.name,
2481
- passed: s.passed,
2482
- skipped: s.skipped,
2483
- skipReason: s.skipReason,
2484
- })),
2485
- }
2486
- : args.skip_verification
2487
- ? { skipped: true, reason: "skip_verification=true" }
2488
- : { skipped: true, reason: "no files_touched provided" },
2489
- ubs_scan: ubsResult
2490
- ? {
2491
- ran: true,
2492
- bugs_found: ubsResult.summary.total,
2493
- summary: ubsResult.summary,
2494
- warnings: ubsResult.bugs.filter((b) => b.severity !== "critical"),
2495
- }
2496
- : verificationResult
2497
- ? { ran: true, included_in_verification_gate: true }
2498
- : {
2499
- ran: false,
2500
- reason: args.skip_ubs_scan
2501
- ? "skipped"
2502
- : "no files or ubs unavailable",
2503
- },
2504
- learning_prompt: `## Reflection
2505
-
2506
- Did you learn anything reusable during this subtask? Consider:
2507
-
2508
- 1. **Patterns**: Any code patterns or approaches that worked well?
2509
- 2. **Gotchas**: Edge cases or pitfalls to warn future agents about?
2510
- 3. **Best Practices**: Domain-specific guidelines worth documenting?
2511
- 4. **Tool Usage**: Effective ways to use tools for this type of task?
2512
-
2513
- If you discovered something valuable, use \`swarm_learn\` or \`skills_create\` to preserve it as a skill for future swarms.
2514
-
2515
- Files touched: ${args.files_touched?.join(", ") || "none recorded"}`,
2516
- },
2517
- null,
2518
- 2,
2519
- );
2520
- },
2521
- });
2522
-
2523
- /**
2524
- * Classify failure based on error message heuristics
2525
- *
2526
- * Simple pattern matching to categorize why a task failed.
2527
- * Used when failure_mode is not explicitly provided.
2528
- *
2529
- * @param error - Error object or message
2530
- * @returns FailureMode classification
2531
- */
2532
- function classifyFailure(error: Error | string): string {
2533
- const msg = (typeof error === "string" ? error : error.message).toLowerCase();
2534
-
2535
- if (msg.includes("timeout")) return "timeout";
2536
- if (msg.includes("conflict") || msg.includes("reservation"))
2537
- return "conflict";
2538
- if (msg.includes("validation") || msg.includes("schema")) return "validation";
2539
- if (msg.includes("context") || msg.includes("token"))
2540
- return "context_overflow";
2541
- if (msg.includes("blocked") || msg.includes("dependency"))
2542
- return "dependency_blocked";
2543
- if (msg.includes("cancel")) return "user_cancelled";
2544
-
2545
- // Check for tool failure patterns
2546
- if (
2547
- msg.includes("tool") ||
2548
- msg.includes("command") ||
2549
- msg.includes("failed to execute")
2550
- ) {
2551
- return "tool_failure";
2552
- }
2553
-
2554
- return "unknown";
2555
- }
2556
-
2557
- /**
2558
- * Record outcome signals from a completed subtask
2559
- *
2560
- * Tracks implicit feedback (duration, errors, retries) to score
2561
- * decomposition quality over time. This data feeds into criterion
2562
- * weight calculations.
2563
- *
2564
- * Strategy tracking enables learning about which decomposition strategies
2565
- * work best for different task types.
2566
- *
2567
- * @see src/learning.ts for scoring logic
2568
- */
2569
- export const swarm_record_outcome = tool({
2570
- description:
2571
- "Record subtask outcome for implicit feedback scoring. Tracks duration, errors, retries to learn decomposition quality.",
2572
- args: {
2573
- bead_id: tool.schema.string().describe("Subtask bead ID"),
2574
- duration_ms: tool.schema
2575
- .number()
2576
- .int()
2577
- .min(0)
2578
- .describe("Duration in milliseconds"),
2579
- error_count: tool.schema
2580
- .number()
2581
- .int()
2582
- .min(0)
2583
- .default(0)
2584
- .describe("Number of errors encountered"),
2585
- retry_count: tool.schema
2586
- .number()
2587
- .int()
2588
- .min(0)
2589
- .default(0)
2590
- .describe("Number of retry attempts"),
2591
- success: tool.schema.boolean().describe("Whether the subtask succeeded"),
2592
- files_touched: tool.schema
2593
- .array(tool.schema.string())
2594
- .optional()
2595
- .describe("Files that were modified"),
2596
- criteria: tool.schema
2597
- .array(tool.schema.string())
2598
- .optional()
2599
- .describe(
2600
- "Criteria to generate feedback for (default: all default criteria)",
2601
- ),
2602
- strategy: tool.schema
2603
- .enum(["file-based", "feature-based", "risk-based", "research-based"])
2604
- .optional()
2605
- .describe("Decomposition strategy used for this task"),
2606
- failure_mode: tool.schema
2607
- .enum([
2608
- "timeout",
2609
- "conflict",
2610
- "validation",
2611
- "tool_failure",
2612
- "context_overflow",
2613
- "dependency_blocked",
2614
- "user_cancelled",
2615
- "unknown",
2616
- ])
2617
- .optional()
2618
- .describe(
2619
- "Failure classification (only when success=false). Auto-classified if not provided.",
2620
- ),
2621
- failure_details: tool.schema
2622
- .string()
2623
- .optional()
2624
- .describe("Detailed failure context (error message, stack trace, etc.)"),
2625
- },
2626
- async execute(args) {
2627
- // Build outcome signals
2628
- const signals: OutcomeSignals = {
2629
- bead_id: args.bead_id,
2630
- duration_ms: args.duration_ms,
2631
- error_count: args.error_count ?? 0,
2632
- retry_count: args.retry_count ?? 0,
2633
- success: args.success,
2634
- files_touched: args.files_touched ?? [],
2635
- timestamp: new Date().toISOString(),
2636
- strategy: args.strategy as LearningDecompositionStrategy | undefined,
2637
- failure_mode: args.failure_mode,
2638
- failure_details: args.failure_details,
2639
- };
2640
-
2641
- // If task failed but no failure_mode provided, try to classify from failure_details
2642
- if (!args.success && !args.failure_mode && args.failure_details) {
2643
- signals.failure_mode = classifyFailure(args.failure_details) as any;
2644
- }
2645
-
2646
- // Validate signals
2647
- const validated = OutcomeSignalsSchema.parse(signals);
2648
-
2649
- // Score the outcome
2650
- const scored: ScoredOutcome = scoreImplicitFeedback(
2651
- validated,
2652
- DEFAULT_LEARNING_CONFIG,
2653
- );
2654
-
2655
- // Get error patterns from accumulator
2656
- const errorStats = await globalErrorAccumulator.getErrorStats(args.bead_id);
2657
-
2658
- // Generate feedback events for each criterion
2659
- const criteriaToScore = args.criteria ?? [
2660
- "type_safe",
2661
- "no_bugs",
2662
- "patterns",
2663
- "readable",
2664
- ];
2665
- const feedbackEvents: FeedbackEvent[] = criteriaToScore.map((criterion) => {
2666
- const event = outcomeToFeedback(scored, criterion);
2667
- // Include strategy in feedback context for future analysis
2668
- if (args.strategy) {
2669
- event.context =
2670
- `${event.context || ""} [strategy: ${args.strategy}]`.trim();
2671
- }
2672
- // Include error patterns in feedback context
2673
- if (errorStats.total > 0) {
2674
- const errorSummary = Object.entries(errorStats.by_type)
2675
- .map(([type, count]) => `${type}:${count}`)
2676
- .join(", ");
2677
- event.context =
2678
- `${event.context || ""} [errors: ${errorSummary}]`.trim();
2679
- }
2680
- return event;
2681
- });
2682
-
2683
- return JSON.stringify(
2684
- {
2685
- success: true,
2686
- outcome: {
2687
- signals: validated,
2688
- scored: {
2689
- type: scored.type,
2690
- decayed_value: scored.decayed_value,
2691
- reasoning: scored.reasoning,
2692
- },
2693
- },
2694
- feedback_events: feedbackEvents,
2695
- error_patterns: errorStats,
2696
- summary: {
2697
- feedback_type: scored.type,
2698
- duration_seconds: Math.round(args.duration_ms / 1000),
2699
- error_count: args.error_count ?? 0,
2700
- retry_count: args.retry_count ?? 0,
2701
- success: args.success,
2702
- strategy: args.strategy,
2703
- failure_mode: validated.failure_mode,
2704
- failure_details: validated.failure_details,
2705
- accumulated_errors: errorStats.total,
2706
- unresolved_errors: errorStats.unresolved,
2707
- },
2708
- note: "Feedback events should be stored for criterion weight calculation. Use learning.ts functions to apply weights.",
2709
- },
2710
- null,
2711
- 2,
2712
- );
2713
- },
2714
- });
2715
-
2716
- /**
2717
- * Generate subtask prompt for a spawned agent
2718
- */
2719
- export const swarm_subtask_prompt = tool({
2720
- description: "Generate the prompt for a spawned subtask agent",
2721
- args: {
2722
- agent_name: tool.schema.string().describe("Agent Mail name for the agent"),
2723
- bead_id: tool.schema.string().describe("Subtask bead ID"),
2724
- epic_id: tool.schema.string().describe("Epic bead ID"),
2725
- subtask_title: tool.schema.string().describe("Subtask title"),
2726
- subtask_description: tool.schema
2727
- .string()
2728
- .optional()
2729
- .describe("Detailed subtask instructions"),
2730
- files: tool.schema
2731
- .array(tool.schema.string())
2732
- .describe("Files assigned to this subtask"),
2733
- shared_context: tool.schema
2734
- .string()
2735
- .optional()
2736
- .describe("Context shared across all agents"),
2737
- },
2738
- async execute(args) {
2739
- const prompt = formatSubtaskPrompt({
2740
- agent_name: args.agent_name,
2741
- bead_id: args.bead_id,
2742
- epic_id: args.epic_id,
2743
- subtask_title: args.subtask_title,
2744
- subtask_description: args.subtask_description || "",
2745
- files: args.files,
2746
- shared_context: args.shared_context,
2747
- });
2748
-
2749
- return prompt;
2750
- },
2751
- });
2752
-
2753
- /**
2754
- * Prepare a subtask for spawning with Task tool (V2 prompt)
2755
- *
2756
- * Generates a streamlined prompt that tells agents to USE Agent Mail and beads.
2757
- * Returns JSON that can be directly used with Task tool.
2758
- */
2759
- export const swarm_spawn_subtask = tool({
2760
- description:
2761
- "Prepare a subtask for spawning. Returns prompt with Agent Mail/beads instructions.",
2762
- args: {
2763
- bead_id: tool.schema.string().describe("Subtask bead ID"),
2764
- epic_id: tool.schema.string().describe("Parent epic bead ID"),
2765
- subtask_title: tool.schema.string().describe("Subtask title"),
2766
- subtask_description: tool.schema
2767
- .string()
2768
- .optional()
2769
- .describe("Detailed subtask instructions"),
2770
- files: tool.schema
2771
- .array(tool.schema.string())
2772
- .describe("Files assigned to this subtask"),
2773
- shared_context: tool.schema
2774
- .string()
2775
- .optional()
2776
- .describe("Context shared across all agents"),
2777
- },
2778
- async execute(args) {
2779
- const prompt = formatSubtaskPromptV2({
2780
- bead_id: args.bead_id,
2781
- epic_id: args.epic_id,
2782
- subtask_title: args.subtask_title,
2783
- subtask_description: args.subtask_description || "",
2784
- files: args.files,
2785
- shared_context: args.shared_context,
2786
- });
2787
-
2788
- return JSON.stringify(
2789
- {
2790
- prompt,
2791
- bead_id: args.bead_id,
2792
- epic_id: args.epic_id,
2793
- files: args.files,
2794
- },
2795
- null,
2796
- 2,
2797
- );
2798
- },
2799
- });
2800
-
2801
- /**
2802
- * Schema for task agent result
2803
- */
2804
- const TaskResultSchema = z.object({
2805
- success: z.boolean(),
2806
- summary: z.string(),
2807
- files_modified: z.array(z.string()).optional().default([]),
2808
- files_created: z.array(z.string()).optional().default([]),
2809
- issues_found: z.array(z.string()).optional().default([]),
2810
- tests_passed: z.boolean().optional(),
2811
- notes: z.string().optional(),
2812
- blocker: z.string().optional(),
2813
- suggestions: z.array(z.string()).optional(),
2814
- });
2815
-
2816
- type TaskResult = z.infer<typeof TaskResultSchema>;
2817
-
2818
- /**
2819
- * Handle subtask completion from a Task agent
2820
- *
2821
- * This tool is for coordinators to process the result after a Task subagent
2822
- * returns. It parses the JSON result, closes the bead on success, and
2823
- * creates new beads for any issues discovered.
2824
- *
2825
- * @example
2826
- * // Task agent returns JSON:
2827
- * // { "success": true, "summary": "Added auth", "files_modified": ["src/auth.ts"], "issues_found": ["Missing tests"] }
2828
- * //
2829
- * // Coordinator calls:
2830
- * swarm_complete_subtask(bead_id="bd-123.1", task_result=<agent_response>)
2831
- */
2832
- export const swarm_complete_subtask = tool({
2833
- description:
2834
- "Handle subtask completion after Task agent returns. Parses result JSON, closes bead on success, creates new beads for issues found.",
2835
- args: {
2836
- bead_id: z.string().describe("Subtask bead ID to close"),
2837
- task_result: z
2838
- .string()
2839
- .describe("JSON result from the Task agent (TaskResult schema)"),
2840
- files_touched: z
2841
- .array(z.string())
2842
- .optional()
2843
- .describe(
2844
- "Override files touched (uses task_result.files_modified if not provided)",
2845
- ),
2846
- },
2847
- async execute(args) {
2848
- // Parse the task result JSON
2849
- let result: TaskResult;
2850
- try {
2851
- const parsed = JSON.parse(args.task_result);
2852
- result = TaskResultSchema.parse(parsed);
2853
- } catch (error) {
2854
- // Handle parse errors gracefully
2855
- const errorMessage =
2856
- error instanceof SyntaxError
2857
- ? `Invalid JSON: ${error.message}`
2858
- : error instanceof z.ZodError
2859
- ? `Schema validation failed: ${error.issues.map((i) => i.message).join(", ")}`
2860
- : String(error);
2861
-
2862
- return JSON.stringify(
2863
- {
2864
- success: false,
2865
- error: "Failed to parse task result",
2866
- details: errorMessage,
2867
- hint: "Task agent should return JSON matching TaskResult schema: { success, summary, files_modified?, issues_found?, ... }",
2868
- },
2869
- null,
2870
- 2,
2871
- );
2872
- }
2873
-
2874
- const filesTouched = args.files_touched ?? [
2875
- ...result.files_modified,
2876
- ...result.files_created,
2877
- ];
2878
- const issuesCreated: Array<{ title: string; id?: string }> = [];
2879
-
2880
- // If task failed, don't close the bead - return info for coordinator to handle
2881
- if (!result.success) {
2882
- return JSON.stringify(
2883
- {
2884
- success: false,
2885
- bead_id: args.bead_id,
2886
- task_failed: true,
2887
- summary: result.summary,
2888
- blocker: result.blocker,
2889
- suggestions: result.suggestions,
2890
- files_touched: filesTouched,
2891
- action_needed:
2892
- "Task failed - review blocker and decide whether to retry or close as failed",
2893
- },
2894
- null,
2895
- 2,
2896
- );
2897
- }
2898
-
2899
- // Task succeeded - close the bead
2900
- const closeReason = result.summary.slice(0, 200); // Truncate for safety
2901
- await Bun.$`bd close ${args.bead_id} -r "${closeReason}"`.quiet().nothrow();
2902
-
2903
- // Create new beads for each issue found
2904
- if (result.issues_found.length > 0) {
2905
- for (const issue of result.issues_found) {
2906
- const issueTitle = issue.slice(0, 100); // Truncate long titles
2907
- const createResult = await Bun.$`bd create "${issueTitle}" -t bug`
2908
- .quiet()
2909
- .nothrow();
2910
-
2911
- if (createResult.exitCode === 0) {
2912
- // Try to parse the bead ID from output
2913
- const output = createResult.stdout.toString();
2914
- const idMatch = output.match(/bd-[a-z0-9]+/);
2915
- issuesCreated.push({
2916
- title: issueTitle,
2917
- id: idMatch?.[0],
2918
- });
2919
- } else {
2920
- issuesCreated.push({
2921
- title: issueTitle,
2922
- id: undefined, // Failed to create
2923
- });
2924
- }
2925
- }
2926
- }
2927
-
2928
- return JSON.stringify(
2929
- {
2930
- success: true,
2931
- bead_id: args.bead_id,
2932
- bead_closed: true,
2933
- summary: result.summary,
2934
- files_touched: filesTouched,
2935
- tests_passed: result.tests_passed,
2936
- notes: result.notes,
2937
- issues_created: issuesCreated.length > 0 ? issuesCreated : undefined,
2938
- issues_count: issuesCreated.length,
2939
- },
2940
- null,
2941
- 2,
2942
- );
2943
- },
2944
- });
2945
-
2946
- /**
2947
- * Generate self-evaluation prompt
2948
- */
2949
- export const swarm_evaluation_prompt = tool({
2950
- description: "Generate self-evaluation prompt for a completed subtask",
2951
- args: {
2952
- bead_id: tool.schema.string().describe("Subtask bead ID"),
2953
- subtask_title: tool.schema.string().describe("Subtask title"),
2954
- files_touched: tool.schema
2955
- .array(tool.schema.string())
2956
- .describe("Files that were modified"),
2957
- },
2958
- async execute(args) {
2959
- const prompt = formatEvaluationPrompt({
2960
- bead_id: args.bead_id,
2961
- subtask_title: args.subtask_title,
2962
- files_touched: args.files_touched,
2963
- });
2964
-
2965
- return JSON.stringify(
2966
- {
2967
- prompt,
2968
- expected_schema: "Evaluation",
2969
- schema_hint: {
2970
- passed: "boolean",
2971
- criteria: {
2972
- type_safe: { passed: "boolean", feedback: "string" },
2973
- no_bugs: { passed: "boolean", feedback: "string" },
2974
- patterns: { passed: "boolean", feedback: "string" },
2975
- readable: { passed: "boolean", feedback: "string" },
2976
- },
2977
- overall_feedback: "string",
2978
- retry_suggestion: "string | null",
2979
- },
2980
- },
2981
- null,
2982
- 2,
2983
- );
2984
- },
2985
- });
2986
-
2987
- // ============================================================================
2988
- // Swarm Learning
2989
- // ============================================================================
2990
-
2991
- /**
2992
- * Learn from completed work and optionally create a skill
2993
- *
2994
- * This tool helps agents reflect on patterns, best practices, or domain
2995
- * knowledge discovered during task execution and codify them into reusable
2996
- * skills for future swarms.
2997
- *
2998
- * Implements the "learning swarm" pattern where swarms get smarter over time.
2999
- */
3000
- export const swarm_learn = tool({
3001
- description: `Analyze completed work and optionally create a skill from learned patterns.
3002
-
3003
- Use after completing a subtask when you've discovered:
3004
- - Reusable code patterns or approaches
3005
- - Domain-specific best practices
3006
- - Gotchas or edge cases to warn about
3007
- - Effective tool usage patterns
3008
-
3009
- This tool helps you formalize learnings into a skill that future agents can discover and use.`,
3010
- args: {
3011
- summary: tool.schema
3012
- .string()
3013
- .describe("Brief summary of what was learned (1-2 sentences)"),
3014
- pattern_type: tool.schema
3015
- .enum([
3016
- "code-pattern",
3017
- "best-practice",
3018
- "gotcha",
3019
- "tool-usage",
3020
- "domain-knowledge",
3021
- "workflow",
3022
- ])
3023
- .describe("Category of the learning"),
3024
- details: tool.schema
3025
- .string()
3026
- .describe("Detailed explanation of the pattern or practice"),
3027
- example: tool.schema
3028
- .string()
3029
- .optional()
3030
- .describe("Code example or concrete illustration"),
3031
- when_to_use: tool.schema
3032
- .string()
3033
- .describe("When should an agent apply this knowledge?"),
3034
- files_context: tool.schema
3035
- .array(tool.schema.string())
3036
- .optional()
3037
- .describe("Files that exemplify this pattern"),
3038
- create_skill: tool.schema
3039
- .boolean()
3040
- .optional()
3041
- .describe(
3042
- "Create a skill from this learning (default: false, just document)",
3043
- ),
3044
- skill_name: tool.schema
3045
- .string()
3046
- .regex(/^[a-z0-9-]+$/)
3047
- .max(64)
3048
- .optional()
3049
- .describe("Skill name if creating (required if create_skill=true)"),
3050
- skill_tags: tool.schema
3051
- .array(tool.schema.string())
3052
- .optional()
3053
- .describe("Tags for the skill if creating"),
3054
- },
3055
- async execute(args) {
3056
- // Format the learning as structured documentation
3057
- const learning = {
3058
- summary: args.summary,
3059
- type: args.pattern_type,
3060
- details: args.details,
3061
- example: args.example,
3062
- when_to_use: args.when_to_use,
3063
- files_context: args.files_context,
3064
- recorded_at: new Date().toISOString(),
3065
- };
3066
-
3067
- // If creating a skill, generate and create it
3068
- if (args.create_skill) {
3069
- if (!args.skill_name) {
3070
- return JSON.stringify(
3071
- {
3072
- success: false,
3073
- error: "skill_name is required when create_skill=true",
3074
- learning: learning,
3075
- },
3076
- null,
3077
- 2,
3078
- );
3079
- }
3080
-
3081
- // Build skill body from learning
3082
- const skillBody = `# ${args.summary}
3083
-
3084
- ## When to Use
3085
- ${args.when_to_use}
3086
-
3087
- ## ${args.pattern_type.replace(/-/g, " ").replace(/\b\w/g, (c) => c.toUpperCase())}
3088
-
3089
- ${args.details}
3090
-
3091
- ${args.example ? `## Example\n\n\`\`\`\n${args.example}\n\`\`\`\n` : ""}
3092
- ${args.files_context && args.files_context.length > 0 ? `## Reference Files\n\n${args.files_context.map((f) => `- \`${f}\``).join("\n")}\n` : ""}
3093
-
3094
- ---
3095
- *Learned from swarm execution on ${new Date().toISOString().split("T")[0]}*`;
3096
-
3097
- // Import skills_create functionality
3098
- const { getSkill, invalidateSkillsCache } = await import("./skills");
3099
- const { mkdir, writeFile } = await import("fs/promises");
3100
- const { join } = await import("path");
3101
-
3102
- // Check if skill exists
3103
- const existing = await getSkill(args.skill_name);
3104
- if (existing) {
3105
- return JSON.stringify(
3106
- {
3107
- success: false,
3108
- error: `Skill '${args.skill_name}' already exists`,
3109
- existing_path: existing.path,
3110
- learning: learning,
3111
- suggestion:
3112
- "Use skills_update to add to existing skill, or choose a different name",
3113
- },
3114
- null,
3115
- 2,
3116
- );
3117
- }
3118
-
3119
- // Create skill directory and file
3120
- const skillDir = join(
3121
- process.cwd(),
3122
- ".opencode",
3123
- "skills",
3124
- args.skill_name,
3125
- );
3126
- const skillPath = join(skillDir, "SKILL.md");
3127
-
3128
- const frontmatter = [
3129
- "---",
3130
- `name: ${args.skill_name}`,
3131
- `description: ${args.when_to_use.slice(0, 200)}${args.when_to_use.length > 200 ? "..." : ""}`,
3132
- "tags:",
3133
- ` - ${args.pattern_type}`,
3134
- ` - learned`,
3135
- ...(args.skill_tags || []).map((t) => ` - ${t}`),
3136
- "---",
3137
- ].join("\n");
3138
-
3139
- try {
3140
- await mkdir(skillDir, { recursive: true });
3141
- await writeFile(skillPath, `${frontmatter}\n\n${skillBody}`, "utf-8");
3142
- invalidateSkillsCache();
3143
-
3144
- return JSON.stringify(
3145
- {
3146
- success: true,
3147
- skill_created: true,
3148
- skill: {
3149
- name: args.skill_name,
3150
- path: skillPath,
3151
- type: args.pattern_type,
3152
- },
3153
- learning: learning,
3154
- message: `Created skill '${args.skill_name}' from learned pattern. Future agents can discover it with skills_list.`,
3155
- },
3156
- null,
3157
- 2,
3158
- );
3159
- } catch (error) {
3160
- return JSON.stringify(
3161
- {
3162
- success: false,
3163
- error: `Failed to create skill: ${error instanceof Error ? error.message : String(error)}`,
3164
- learning: learning,
3165
- },
3166
- null,
3167
- 2,
3168
- );
3169
- }
3170
- }
3171
-
3172
- // Just document the learning without creating a skill
3173
- return JSON.stringify(
3174
- {
3175
- success: true,
3176
- skill_created: false,
3177
- learning: learning,
3178
- message:
3179
- "Learning documented. Use create_skill=true to persist as a skill for future agents.",
3180
- suggested_skill_name:
3181
- args.skill_name ||
3182
- args.summary
3183
- .toLowerCase()
3184
- .replace(/[^a-z0-9\s-]/g, "")
3185
- .replace(/\s+/g, "-")
3186
- .slice(0, 64),
3187
- },
3188
- null,
3189
- 2,
3190
- );
3191
- },
3192
- });
3193
-
3194
- // ============================================================================
3195
- // Error Accumulator
3196
- // ============================================================================
3197
-
3198
- /**
3199
- * Global error accumulator for tracking errors across subtasks
3200
- *
3201
- * This is a session-level singleton that accumulates errors during
3202
- * swarm execution for feeding into retry prompts.
3203
- */
3204
- const globalErrorAccumulator = new ErrorAccumulator();
3205
-
3206
- /**
3207
- * Record an error during subtask execution
3208
- *
3209
- * Implements pattern from "Patterns for Building AI Agents" p.40:
3210
- * "Good agents examine and correct errors when something goes wrong"
3211
- *
3212
- * Errors are accumulated and can be fed into retry prompts to help
3213
- * agents learn from past failures.
3214
- */
3215
- export const swarm_accumulate_error = tool({
3216
- description:
3217
- "Record an error during subtask execution. Errors feed into retry prompts.",
3218
- args: {
3219
- bead_id: tool.schema.string().describe("Bead ID where error occurred"),
3220
- error_type: tool.schema
3221
- .enum(["validation", "timeout", "conflict", "tool_failure", "unknown"])
3222
- .describe("Category of error"),
3223
- message: tool.schema.string().describe("Human-readable error message"),
3224
- stack_trace: tool.schema
3225
- .string()
3226
- .optional()
3227
- .describe("Stack trace for debugging"),
3228
- tool_name: tool.schema.string().optional().describe("Tool that failed"),
3229
- context: tool.schema
3230
- .string()
3231
- .optional()
3232
- .describe("What was happening when error occurred"),
3233
- },
3234
- async execute(args) {
3235
- const entry = await globalErrorAccumulator.recordError(
3236
- args.bead_id,
3237
- args.error_type as ErrorType,
3238
- args.message,
3239
- {
3240
- stack_trace: args.stack_trace,
3241
- tool_name: args.tool_name,
3242
- context: args.context,
3243
- },
3244
- );
3245
-
3246
- return JSON.stringify(
3247
- {
3248
- success: true,
3249
- error_id: entry.id,
3250
- bead_id: entry.bead_id,
3251
- error_type: entry.error_type,
3252
- message: entry.message,
3253
- timestamp: entry.timestamp,
3254
- note: "Error recorded for retry context. Use swarm_get_error_context to retrieve accumulated errors.",
3255
- },
3256
- null,
3257
- 2,
3258
- );
3259
- },
3260
- });
3261
-
3262
- /**
3263
- * Get accumulated errors for a bead to feed into retry prompts
3264
- *
3265
- * Returns formatted error context that can be injected into retry prompts
3266
- * to help agents learn from past failures.
3267
- */
3268
- export const swarm_get_error_context = tool({
3269
- description:
3270
- "Get accumulated errors for a bead. Returns formatted context for retry prompts.",
3271
- args: {
3272
- bead_id: tool.schema.string().describe("Bead ID to get errors for"),
3273
- include_resolved: tool.schema
3274
- .boolean()
3275
- .optional()
3276
- .describe("Include resolved errors (default: false)"),
3277
- },
3278
- async execute(args) {
3279
- const errorContext = await globalErrorAccumulator.getErrorContext(
3280
- args.bead_id,
3281
- args.include_resolved ?? false,
3282
- );
3283
-
3284
- const stats = await globalErrorAccumulator.getErrorStats(args.bead_id);
3285
-
3286
- return JSON.stringify(
3287
- {
3288
- bead_id: args.bead_id,
3289
- error_context: errorContext,
3290
- stats: {
3291
- total_errors: stats.total,
3292
- unresolved: stats.unresolved,
3293
- by_type: stats.by_type,
3294
- },
3295
- has_errors: errorContext.length > 0,
3296
- usage:
3297
- "Inject error_context into retry prompt using {error_context} placeholder",
3298
- },
3299
- null,
3300
- 2,
3301
- );
3302
- },
3303
- });
3304
-
3305
- /**
3306
- * Mark an error as resolved
3307
- *
3308
- * Call this after an agent successfully addresses an error to update
3309
- * the accumulator state.
3310
- */
3311
- export const swarm_resolve_error = tool({
3312
- description:
3313
- "Mark an error as resolved after fixing it. Updates error accumulator state.",
3314
- args: {
3315
- error_id: tool.schema.string().describe("Error ID to mark as resolved"),
3316
- },
3317
- async execute(args) {
3318
- await globalErrorAccumulator.resolveError(args.error_id);
3319
-
3320
- return JSON.stringify(
3321
- {
3322
- success: true,
3323
- error_id: args.error_id,
3324
- resolved: true,
3325
- },
3326
- null,
3327
- 2,
3328
- );
3329
- },
3330
- });
20
+ // Import tools from each module
21
+ import { strategyTools } from "./swarm-strategies";
22
+ import { decomposeTools } from "./swarm-decompose";
23
+ import { promptTools } from "./swarm-prompts";
24
+ import { orchestrateTools } from "./swarm-orchestrate";
3331
25
 
3332
26
  /**
3333
- * Initialize swarm and check tool availability
3334
- *
3335
- * Call this at the start of a swarm session to see what tools are available,
3336
- * what skills exist in the project, and what features will be degraded.
3337
- *
3338
- * Skills are automatically discovered from:
3339
- * - .opencode/skills/
3340
- * - .claude/skills/
3341
- * - skills/
27
+ * Combined swarm tools for plugin registration.
28
+ * Includes all tools from strategy, decompose, prompt, and orchestrate modules.
3342
29
  */
3343
- export const swarm_init = tool({
3344
- description:
3345
- "Initialize swarm session: discovers available skills, checks tool availability. ALWAYS call at swarm start.",
3346
- args: {
3347
- project_path: tool.schema
3348
- .string()
3349
- .optional()
3350
- .describe("Project path (for Agent Mail init)"),
3351
- },
3352
- async execute(args) {
3353
- // Check all tools
3354
- const availability = await checkAllTools();
3355
-
3356
- // Build status report
3357
- const report = formatToolAvailability(availability);
3358
-
3359
- // Check critical tools
3360
- const beadsAvailable = availability.get("beads")?.status.available ?? false;
3361
- const agentMailAvailable =
3362
- availability.get("agent-mail")?.status.available ?? false;
3363
-
3364
- // Build warnings
3365
- const warnings: string[] = [];
3366
- const degradedFeatures: string[] = [];
3367
-
3368
- if (!beadsAvailable) {
3369
- warnings.push(
3370
- "⚠️ beads (bd) not available - issue tracking disabled, swarm coordination will be limited",
3371
- );
3372
- degradedFeatures.push("issue tracking", "progress persistence");
3373
- }
3374
-
3375
- if (!agentMailAvailable) {
3376
- warnings.push(
3377
- "⚠️ agent-mail not available - multi-agent communication disabled",
3378
- );
3379
- degradedFeatures.push("agent communication", "file reservations");
3380
- }
3381
-
3382
- if (!availability.get("cass")?.status.available) {
3383
- degradedFeatures.push("historical context from past sessions");
3384
- }
3385
-
3386
- if (!availability.get("ubs")?.status.available) {
3387
- degradedFeatures.push("pre-completion bug scanning");
3388
- }
3389
-
3390
- if (!availability.get("semantic-memory")?.status.available) {
3391
- degradedFeatures.push("persistent learning (using in-memory fallback)");
3392
- }
3393
-
3394
- // Discover available skills
3395
- const availableSkills = await listSkills();
3396
- const skillsInfo = {
3397
- count: availableSkills.length,
3398
- available: availableSkills.length > 0,
3399
- skills: availableSkills.map((s) => ({
3400
- name: s.name,
3401
- description: s.description,
3402
- hasScripts: s.hasScripts,
3403
- })),
3404
- };
3405
-
3406
- // Add skills guidance if available
3407
- let skillsGuidance: string | undefined;
3408
- if (availableSkills.length > 0) {
3409
- skillsGuidance = `Found ${availableSkills.length} skill(s). Use skills_list to see details, skills_use to activate.`;
3410
- } else {
3411
- skillsGuidance =
3412
- "No skills found. Add skills to .opencode/skills/ or .claude/skills/ for specialized guidance.";
3413
- }
3414
-
3415
- return JSON.stringify(
3416
- {
3417
- ready: true,
3418
- tool_availability: Object.fromEntries(
3419
- Array.from(availability.entries()).map(([k, v]) => [
3420
- k,
3421
- {
3422
- available: v.status.available,
3423
- fallback: v.status.available ? null : v.fallbackBehavior,
3424
- },
3425
- ]),
3426
- ),
3427
- skills: skillsInfo,
3428
- warnings: warnings.length > 0 ? warnings : undefined,
3429
- degraded_features:
3430
- degradedFeatures.length > 0 ? degradedFeatures : undefined,
3431
- recommendations: {
3432
- skills: skillsGuidance,
3433
- beads: beadsAvailable
3434
- ? "✓ Use beads for all task tracking"
3435
- : "Install beads: npm i -g @joelhooks/beads",
3436
- agent_mail: agentMailAvailable
3437
- ? "✓ Use Agent Mail for coordination"
3438
- : "Start Agent Mail: agent-mail serve",
3439
- },
3440
- report,
3441
- },
3442
- null,
3443
- 2,
3444
- );
3445
- },
3446
- });
3447
-
3448
- // ============================================================================
3449
- // Export all tools
3450
- // ============================================================================
3451
-
3452
30
  export const swarmTools = {
3453
- swarm_init: swarm_init,
3454
- swarm_select_strategy: swarm_select_strategy,
3455
- swarm_plan_prompt: swarm_plan_prompt,
3456
- swarm_decompose: swarm_decompose,
3457
- swarm_validate_decomposition: swarm_validate_decomposition,
3458
- swarm_status: swarm_status,
3459
- swarm_progress: swarm_progress,
3460
- swarm_broadcast: swarm_broadcast,
3461
- swarm_complete: swarm_complete,
3462
- swarm_learn: swarm_learn,
3463
- swarm_record_outcome: swarm_record_outcome,
3464
- swarm_subtask_prompt: swarm_subtask_prompt,
3465
- swarm_spawn_subtask: swarm_spawn_subtask,
3466
- swarm_complete_subtask: swarm_complete_subtask,
3467
- swarm_evaluation_prompt: swarm_evaluation_prompt,
3468
- swarm_accumulate_error: swarm_accumulate_error,
3469
- swarm_get_error_context: swarm_get_error_context,
3470
- swarm_resolve_error: swarm_resolve_error,
31
+ ...strategyTools,
32
+ ...decomposeTools,
33
+ ...promptTools,
34
+ ...orchestrateTools,
3471
35
  };
3472
-
3473
- // ============================================================================
3474
- // 3-Strike Detection
3475
- // ============================================================================
3476
-
3477
- /**
3478
- * Global strike storage for tracking consecutive fix failures
3479
- */
3480
- import {
3481
- InMemoryStrikeStorage,
3482
- addStrike,
3483
- getStrikes,
3484
- isStrikedOut,
3485
- getArchitecturePrompt,
3486
- clearStrikes,
3487
- type StrikeStorage,
3488
- } from "./learning";
3489
-
3490
- const globalStrikeStorage: StrikeStorage = new InMemoryStrikeStorage();
3491
-
3492
- /**
3493
- * Check if a bead has struck out (3 consecutive failures)
3494
- *
3495
- * The 3-Strike Rule:
3496
- * IF 3+ fixes have failed:
3497
- * STOP → Question the architecture
3498
- * DON'T attempt Fix #4
3499
- * Discuss with human partner
3500
- *
3501
- * This is NOT a failed hypothesis.
3502
- * This is a WRONG ARCHITECTURE.
3503
- *
3504
- * Use this tool to:
3505
- * - Check strike count before attempting a fix
3506
- * - Get architecture review prompt if struck out
3507
- * - Record a strike when a fix fails
3508
- * - Clear strikes when a fix succeeds
3509
- */
3510
- export const swarm_check_strikes = tool({
3511
- description:
3512
- "Check 3-strike status for a bead. Records failures, detects architectural problems, generates architecture review prompts.",
3513
- args: {
3514
- bead_id: tool.schema.string().describe("Bead ID to check"),
3515
- action: tool.schema
3516
- .enum(["check", "add_strike", "clear", "get_prompt"])
3517
- .describe(
3518
- "Action: check count, add strike, clear strikes, or get prompt",
3519
- ),
3520
- attempt: tool.schema
3521
- .string()
3522
- .optional()
3523
- .describe("Description of fix attempt (required for add_strike)"),
3524
- reason: tool.schema
3525
- .string()
3526
- .optional()
3527
- .describe("Why the fix failed (required for add_strike)"),
3528
- },
3529
- async execute(args) {
3530
- switch (args.action) {
3531
- case "check": {
3532
- const count = await getStrikes(args.bead_id, globalStrikeStorage);
3533
- const strikedOut = await isStrikedOut(
3534
- args.bead_id,
3535
- globalStrikeStorage,
3536
- );
3537
-
3538
- return JSON.stringify(
3539
- {
3540
- bead_id: args.bead_id,
3541
- strike_count: count,
3542
- is_striked_out: strikedOut,
3543
- message: strikedOut
3544
- ? "⚠️ STRUCK OUT: 3 strikes reached. Use get_prompt action for architecture review."
3545
- : count === 0
3546
- ? "No strikes. Clear to proceed."
3547
- : `${count} strike${count > 1 ? "s" : ""}. ${3 - count} remaining before architecture review required.`,
3548
- next_action: strikedOut
3549
- ? "Call with action=get_prompt to get architecture review questions"
3550
- : "Continue with fix attempt",
3551
- },
3552
- null,
3553
- 2,
3554
- );
3555
- }
3556
-
3557
- case "add_strike": {
3558
- if (!args.attempt || !args.reason) {
3559
- return JSON.stringify(
3560
- {
3561
- error: "add_strike requires 'attempt' and 'reason' parameters",
3562
- },
3563
- null,
3564
- 2,
3565
- );
3566
- }
3567
-
3568
- const record = await addStrike(
3569
- args.bead_id,
3570
- args.attempt,
3571
- args.reason,
3572
- globalStrikeStorage,
3573
- );
3574
-
3575
- const strikedOut = record.strike_count >= 3;
3576
-
3577
- return JSON.stringify(
3578
- {
3579
- bead_id: args.bead_id,
3580
- strike_count: record.strike_count,
3581
- is_striked_out: strikedOut,
3582
- failures: record.failures,
3583
- message: strikedOut
3584
- ? "⚠️ STRUCK OUT: 3 strikes reached. STOP and question the architecture."
3585
- : `Strike ${record.strike_count} recorded. ${3 - record.strike_count} remaining.`,
3586
- warning: strikedOut
3587
- ? "DO NOT attempt Fix #4. Call with action=get_prompt for architecture review."
3588
- : undefined,
3589
- },
3590
- null,
3591
- 2,
3592
- );
3593
- }
3594
-
3595
- case "clear": {
3596
- await clearStrikes(args.bead_id, globalStrikeStorage);
3597
-
3598
- return JSON.stringify(
3599
- {
3600
- bead_id: args.bead_id,
3601
- strike_count: 0,
3602
- is_striked_out: false,
3603
- message: "Strikes cleared. Fresh start.",
3604
- },
3605
- null,
3606
- 2,
3607
- );
3608
- }
3609
-
3610
- case "get_prompt": {
3611
- const prompt = await getArchitecturePrompt(
3612
- args.bead_id,
3613
- globalStrikeStorage,
3614
- );
3615
-
3616
- if (!prompt) {
3617
- return JSON.stringify(
3618
- {
3619
- bead_id: args.bead_id,
3620
- has_prompt: false,
3621
- message: "No architecture prompt (not struck out yet)",
3622
- },
3623
- null,
3624
- 2,
3625
- );
3626
- }
3627
-
3628
- return JSON.stringify(
3629
- {
3630
- bead_id: args.bead_id,
3631
- has_prompt: true,
3632
- architecture_review_prompt: prompt,
3633
- message:
3634
- "Architecture review required. Present this prompt to the human partner.",
3635
- },
3636
- null,
3637
- 2,
3638
- );
3639
- }
3640
-
3641
- default:
3642
- return JSON.stringify(
3643
- {
3644
- error: `Unknown action: ${args.action}`,
3645
- },
3646
- null,
3647
- 2,
3648
- );
3649
- }
3650
- },
3651
- });