opencode-swarm-plugin 0.5.0 → 0.6.1

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/README.md CHANGED
@@ -131,6 +131,8 @@ Client-side, per-agent rate limits prevent abuse and ensure fair resource usage
131
131
  | Tool | Description |
132
132
  | ------------------------------ | ------------------------------------------------------------------------ |
133
133
  | `swarm_init` | Check tool availability, report degraded features |
134
+ | `swarm_select_strategy` | Analyze task and recommend decomposition strategy |
135
+ | `swarm_plan_prompt` | Generate strategy-specific planning prompt with CASS integration |
134
136
  | `swarm_decompose` | Generate decomposition prompt, optionally queries CASS for similar tasks |
135
137
  | `swarm_validate_decomposition` | Validate decomposition response, detect instruction conflicts |
136
138
  | `swarm_status` | Get swarm status by epic ID |
@@ -152,6 +154,80 @@ Client-side, per-agent rate limits prevent abuse and ensure fair resource usage
152
154
  | `structured_parse_decomposition` | Parse and validate task decomposition |
153
155
  | `structured_parse_bead_tree` | Parse and validate bead tree for epic creation |
154
156
 
157
+ ## Decomposition Strategies
158
+
159
+ The plugin supports three decomposition strategies, auto-selected based on task keywords:
160
+
161
+ ### File-Based Strategy
162
+
163
+ Best for: Refactoring, migrations, pattern changes across codebase
164
+
165
+ **Keywords**: refactor, migrate, rename, update all, convert, upgrade
166
+
167
+ **Guidelines**:
168
+
169
+ - Group files by directory or type
170
+ - Handle shared types/utilities first
171
+ - Minimize cross-directory dependencies
172
+
173
+ ### Feature-Based Strategy
174
+
175
+ Best for: New features, adding functionality
176
+
177
+ **Keywords**: add, implement, build, create, feature, new
178
+
179
+ **Guidelines**:
180
+
181
+ - Each subtask is a complete vertical slice
182
+ - Start with data layer, then logic, then UI
183
+ - Keep related components together
184
+
185
+ ### Risk-Based Strategy
186
+
187
+ Best for: Bug fixes, security issues, critical changes
188
+
189
+ **Keywords**: fix, bug, security, critical, urgent, hotfix
190
+
191
+ **Guidelines**:
192
+
193
+ - Write tests FIRST
194
+ - Isolate risky changes
195
+ - Audit similar code for same issue
196
+
197
+ ### Strategy Selection
198
+
199
+ Use `swarm_select_strategy` to see the recommended strategy:
200
+
201
+ ```typescript
202
+ swarm_select_strategy({ task: "Add user authentication" });
203
+ // Returns: { strategy: "feature-based", confidence: 0.85, reasoning: "..." }
204
+ ```
205
+
206
+ Or let `swarm_plan_prompt` auto-select:
207
+
208
+ ```typescript
209
+ swarm_plan_prompt({ task: "Refactor all components to use hooks" });
210
+ // Auto-selects file-based strategy
211
+ ```
212
+
213
+ ## Example Planner Agent
214
+
215
+ The plugin includes an example planner agent at `examples/agents/swarm-planner.md`.
216
+
217
+ Copy to your OpenCode agents directory:
218
+
219
+ ```bash
220
+ cp examples/agents/swarm-planner.md ~/.config/opencode/agents/
221
+ ```
222
+
223
+ Then invoke with:
224
+
225
+ ```
226
+ @swarm-planner "Add user authentication with OAuth"
227
+ ```
228
+
229
+ The planner uses `swarm_select_strategy` and `swarm_plan_prompt` internally to create optimal decompositions.
230
+
155
231
  ### Schemas (for structured outputs)
156
232
 
157
233
  The plugin exports Zod schemas for validated agent responses:
package/dist/index.js CHANGED
@@ -23386,6 +23386,11 @@ var CriterionWeightSchema = exports_external.object({
23386
23386
  last_validated: exports_external.string().optional(),
23387
23387
  half_life_days: exports_external.number().positive().default(90)
23388
23388
  });
23389
+ var DecompositionStrategySchema = exports_external.enum([
23390
+ "file-based",
23391
+ "feature-based",
23392
+ "risk-based"
23393
+ ]);
23389
23394
  var OutcomeSignalsSchema = exports_external.object({
23390
23395
  bead_id: exports_external.string(),
23391
23396
  duration_ms: exports_external.number().int().min(0),
@@ -23393,7 +23398,8 @@ var OutcomeSignalsSchema = exports_external.object({
23393
23398
  retry_count: exports_external.number().int().min(0),
23394
23399
  success: exports_external.boolean(),
23395
23400
  files_touched: exports_external.array(exports_external.string()).default([]),
23396
- timestamp: exports_external.string()
23401
+ timestamp: exports_external.string(),
23402
+ strategy: DecompositionStrategySchema.optional()
23397
23403
  });
23398
23404
  var ScoredOutcomeSchema = exports_external.object({
23399
23405
  signals: OutcomeSignalsSchema,
@@ -23553,6 +23559,167 @@ function detectInstructionConflicts(subtasks) {
23553
23559
  }
23554
23560
  return conflicts;
23555
23561
  }
23562
+ var STRATEGIES = {
23563
+ "file-based": {
23564
+ name: "file-based",
23565
+ description: "Group by file type or directory. Best for refactoring, migrations, and pattern changes across codebase.",
23566
+ keywords: [
23567
+ "refactor",
23568
+ "migrate",
23569
+ "update all",
23570
+ "rename",
23571
+ "replace",
23572
+ "convert",
23573
+ "upgrade",
23574
+ "deprecate",
23575
+ "remove",
23576
+ "cleanup",
23577
+ "lint",
23578
+ "format"
23579
+ ],
23580
+ guidelines: [
23581
+ "Group files by directory or type (e.g., all components, all tests)",
23582
+ "Minimize cross-directory dependencies within a subtask",
23583
+ "Handle shared types/utilities first if they change",
23584
+ "Each subtask should be a complete transformation of its file set",
23585
+ "Consider import/export relationships when grouping"
23586
+ ],
23587
+ antiPatterns: [
23588
+ "Don't split tightly coupled files across subtasks",
23589
+ "Don't group files that have no relationship",
23590
+ "Don't forget to update imports when moving/renaming"
23591
+ ],
23592
+ examples: [
23593
+ "Migrate all components to new API \u2192 split by component directory",
23594
+ "Rename userId to accountId \u2192 split by module (types first, then consumers)",
23595
+ "Update all tests to use new matcher \u2192 split by test directory"
23596
+ ]
23597
+ },
23598
+ "feature-based": {
23599
+ name: "feature-based",
23600
+ description: "Vertical slices with UI + API + data. Best for new features and adding functionality.",
23601
+ keywords: [
23602
+ "add",
23603
+ "implement",
23604
+ "build",
23605
+ "create",
23606
+ "feature",
23607
+ "new",
23608
+ "integrate",
23609
+ "connect",
23610
+ "enable",
23611
+ "support"
23612
+ ],
23613
+ guidelines: [
23614
+ "Each subtask is a complete vertical slice (UI + logic + data)",
23615
+ "Start with data layer/types, then logic, then UI",
23616
+ "Keep related components together (form + validation + submission)",
23617
+ "Separate concerns that can be developed independently",
23618
+ "Consider user-facing features as natural boundaries"
23619
+ ],
23620
+ antiPatterns: [
23621
+ "Don't split a single feature across multiple subtasks",
23622
+ "Don't create subtasks that can't be tested independently",
23623
+ "Don't forget integration points between features"
23624
+ ],
23625
+ examples: [
23626
+ "Add user auth \u2192 [OAuth setup, Session management, Protected routes]",
23627
+ "Build dashboard \u2192 [Data fetching, Chart components, Layout/navigation]",
23628
+ "Add search \u2192 [Search API, Search UI, Results display]"
23629
+ ]
23630
+ },
23631
+ "risk-based": {
23632
+ name: "risk-based",
23633
+ description: "Isolate high-risk changes, add tests first. Best for bug fixes, security issues, and critical changes.",
23634
+ keywords: [
23635
+ "fix",
23636
+ "bug",
23637
+ "security",
23638
+ "vulnerability",
23639
+ "critical",
23640
+ "urgent",
23641
+ "hotfix",
23642
+ "patch",
23643
+ "audit",
23644
+ "review",
23645
+ "investigate"
23646
+ ],
23647
+ guidelines: [
23648
+ "Write tests FIRST to capture expected behavior",
23649
+ "Isolate the risky change to minimize blast radius",
23650
+ "Add monitoring/logging around the change",
23651
+ "Create rollback plan as part of the task",
23652
+ "Audit similar code for the same issue"
23653
+ ],
23654
+ antiPatterns: [
23655
+ "Don't make multiple risky changes in one subtask",
23656
+ "Don't skip tests for 'simple' fixes",
23657
+ "Don't forget to check for similar issues elsewhere"
23658
+ ],
23659
+ examples: [
23660
+ "Fix auth bypass \u2192 [Add regression test, Fix vulnerability, Audit similar endpoints]",
23661
+ "Fix race condition \u2192 [Add test reproducing issue, Implement fix, Add concurrency tests]",
23662
+ "Security audit \u2192 [Scan for vulnerabilities, Fix critical issues, Document remaining risks]"
23663
+ ]
23664
+ }
23665
+ };
23666
+ function selectStrategy(task) {
23667
+ const taskLower = task.toLowerCase();
23668
+ const scores = {
23669
+ "file-based": 0,
23670
+ "feature-based": 0,
23671
+ "risk-based": 0
23672
+ };
23673
+ for (const [strategyName, definition] of Object.entries(STRATEGIES)) {
23674
+ const name = strategyName;
23675
+ for (const keyword of definition.keywords) {
23676
+ if (taskLower.includes(keyword)) {
23677
+ scores[name] += 1;
23678
+ }
23679
+ }
23680
+ }
23681
+ const entries = Object.entries(scores);
23682
+ entries.sort((a, b) => b[1] - a[1]);
23683
+ const [winner, winnerScore] = entries[0];
23684
+ const [runnerUp, runnerUpScore] = entries[1] || [null, 0];
23685
+ const totalScore = entries.reduce((sum, [, score]) => sum + score, 0);
23686
+ const confidence = totalScore > 0 ? Math.min(0.95, 0.5 + (winnerScore - runnerUpScore) / totalScore) : 0.5;
23687
+ let reasoning;
23688
+ if (winnerScore === 0) {
23689
+ reasoning = `No strong keyword signals. Defaulting to feature-based as it's most versatile.`;
23690
+ } else {
23691
+ const matchedKeywords = STRATEGIES[winner].keywords.filter((k) => taskLower.includes(k));
23692
+ reasoning = `Matched keywords: ${matchedKeywords.join(", ")}. ${STRATEGIES[winner].description}`;
23693
+ }
23694
+ const finalStrategy = winnerScore === 0 ? "feature-based" : winner;
23695
+ return {
23696
+ strategy: finalStrategy,
23697
+ confidence,
23698
+ reasoning,
23699
+ alternatives: entries.filter(([s]) => s !== finalStrategy).map(([strategy, score]) => ({ strategy, score }))
23700
+ };
23701
+ }
23702
+ function formatStrategyGuidelines(strategy) {
23703
+ const def = STRATEGIES[strategy];
23704
+ const guidelines = def.guidelines.map((g) => `- ${g}`).join(`
23705
+ `);
23706
+ const antiPatterns = def.antiPatterns.map((a) => `- ${a}`).join(`
23707
+ `);
23708
+ const examples = def.examples.map((e) => `- ${e}`).join(`
23709
+ `);
23710
+ return `## Strategy: ${strategy}
23711
+
23712
+ ${def.description}
23713
+
23714
+ ### Guidelines
23715
+ ${guidelines}
23716
+
23717
+ ### Anti-Patterns (Avoid These)
23718
+ ${antiPatterns}
23719
+
23720
+ ### Examples
23721
+ ${examples}`;
23722
+ }
23556
23723
  var DECOMPOSITION_PROMPT = `You are decomposing a task into parallelizable subtasks for a swarm of agents.
23557
23724
 
23558
23725
  ## Task
@@ -23926,6 +24093,155 @@ function formatCassHistoryForPrompt(history) {
23926
24093
  return lines.join(`
23927
24094
  `);
23928
24095
  }
24096
+ var swarm_select_strategy = tool({
24097
+ description: "Analyze task and recommend decomposition strategy (file-based, feature-based, or risk-based)",
24098
+ args: {
24099
+ task: tool.schema.string().min(1).describe("Task description to analyze"),
24100
+ codebase_context: tool.schema.string().optional().describe("Optional codebase context (file structure, tech stack, etc.)")
24101
+ },
24102
+ async execute(args) {
24103
+ const result = selectStrategy(args.task);
24104
+ let enhancedReasoning = result.reasoning;
24105
+ if (args.codebase_context) {
24106
+ enhancedReasoning += `
24107
+
24108
+ Codebase context considered: ${args.codebase_context.slice(0, 200)}...`;
24109
+ }
24110
+ return JSON.stringify({
24111
+ strategy: result.strategy,
24112
+ confidence: Math.round(result.confidence * 100) / 100,
24113
+ reasoning: enhancedReasoning,
24114
+ description: STRATEGIES[result.strategy].description,
24115
+ guidelines: STRATEGIES[result.strategy].guidelines,
24116
+ anti_patterns: STRATEGIES[result.strategy].antiPatterns,
24117
+ alternatives: result.alternatives.map((alt) => ({
24118
+ strategy: alt.strategy,
24119
+ description: STRATEGIES[alt.strategy].description,
24120
+ score: alt.score
24121
+ }))
24122
+ }, null, 2);
24123
+ }
24124
+ });
24125
+ var STRATEGY_DECOMPOSITION_PROMPT = `You are decomposing a task into parallelizable subtasks for a swarm of agents.
24126
+
24127
+ ## Task
24128
+ {task}
24129
+
24130
+ {strategy_guidelines}
24131
+
24132
+ {context_section}
24133
+
24134
+ {cass_history}
24135
+
24136
+ ## MANDATORY: Beads Issue Tracking
24137
+
24138
+ **Every subtask MUST become a bead.** This is non-negotiable.
24139
+
24140
+ After decomposition, the coordinator will:
24141
+ 1. Create an epic bead for the overall task
24142
+ 2. Create child beads for each subtask
24143
+ 3. Track progress through bead status updates
24144
+ 4. Close beads with summaries when complete
24145
+
24146
+ Agents MUST update their bead status as they work. No silent progress.
24147
+
24148
+ ## Requirements
24149
+
24150
+ 1. **Break into 2-{max_subtasks} independent subtasks** that can run in parallel
24151
+ 2. **Assign files** - each subtask must specify which files it will modify
24152
+ 3. **No file overlap** - files cannot appear in multiple subtasks (they get exclusive locks)
24153
+ 4. **Order by dependency** - if subtask B needs subtask A's output, A must come first in the array
24154
+ 5. **Estimate complexity** - 1 (trivial) to 5 (complex)
24155
+ 6. **Plan aggressively** - break down more than you think necessary, smaller is better
24156
+
24157
+ ## Response Format
24158
+
24159
+ Respond with a JSON object matching this schema:
24160
+
24161
+ \`\`\`typescript
24162
+ {
24163
+ epic: {
24164
+ title: string, // Epic title for the beads tracker
24165
+ description?: string // Brief description of the overall goal
24166
+ },
24167
+ subtasks: [
24168
+ {
24169
+ title: string, // What this subtask accomplishes
24170
+ description?: string, // Detailed instructions for the agent
24171
+ files: string[], // Files this subtask will modify (globs allowed)
24172
+ dependencies: number[], // Indices of subtasks this depends on (0-indexed)
24173
+ estimated_complexity: 1-5 // Effort estimate
24174
+ },
24175
+ // ... more subtasks
24176
+ ]
24177
+ }
24178
+ \`\`\`
24179
+
24180
+ Now decompose the task:`;
24181
+ var swarm_plan_prompt = tool({
24182
+ description: "Generate strategy-specific decomposition prompt. Auto-selects strategy or uses provided one. Queries CASS for similar tasks.",
24183
+ args: {
24184
+ task: tool.schema.string().min(1).describe("Task description to decompose"),
24185
+ strategy: tool.schema.enum(["file-based", "feature-based", "risk-based", "auto"]).optional().describe("Decomposition strategy (default: auto-detect)"),
24186
+ max_subtasks: tool.schema.number().int().min(2).max(10).default(5).describe("Maximum number of subtasks (default: 5)"),
24187
+ context: tool.schema.string().optional().describe("Additional context (codebase info, constraints, etc.)"),
24188
+ query_cass: tool.schema.boolean().optional().describe("Query CASS for similar past tasks (default: true)"),
24189
+ cass_limit: tool.schema.number().int().min(1).max(10).optional().describe("Max CASS results to include (default: 3)")
24190
+ },
24191
+ async execute(args) {
24192
+ let selectedStrategy;
24193
+ let strategyReasoning;
24194
+ if (args.strategy && args.strategy !== "auto") {
24195
+ selectedStrategy = args.strategy;
24196
+ strategyReasoning = `User-specified strategy: ${selectedStrategy}`;
24197
+ } else {
24198
+ const selection = selectStrategy(args.task);
24199
+ selectedStrategy = selection.strategy;
24200
+ strategyReasoning = selection.reasoning;
24201
+ }
24202
+ let cassContext = "";
24203
+ let cassResult = null;
24204
+ if (args.query_cass !== false) {
24205
+ cassResult = await queryCassHistory(args.task, args.cass_limit ?? 3);
24206
+ if (cassResult && cassResult.results.length > 0) {
24207
+ cassContext = formatCassHistoryForPrompt(cassResult);
24208
+ }
24209
+ }
24210
+ const strategyGuidelines = formatStrategyGuidelines(selectedStrategy);
24211
+ const contextSection = args.context ? `## Additional Context
24212
+ ${args.context}` : `## Additional Context
24213
+ (none provided)`;
24214
+ const prompt = STRATEGY_DECOMPOSITION_PROMPT.replace("{task}", args.task).replace("{strategy_guidelines}", strategyGuidelines).replace("{context_section}", contextSection).replace("{cass_history}", cassContext || "").replace("{max_subtasks}", (args.max_subtasks ?? 5).toString());
24215
+ return JSON.stringify({
24216
+ prompt,
24217
+ strategy: {
24218
+ selected: selectedStrategy,
24219
+ reasoning: strategyReasoning,
24220
+ guidelines: STRATEGIES[selectedStrategy].guidelines,
24221
+ anti_patterns: STRATEGIES[selectedStrategy].antiPatterns
24222
+ },
24223
+ expected_schema: "BeadTree",
24224
+ schema_hint: {
24225
+ epic: { title: "string", description: "string?" },
24226
+ subtasks: [
24227
+ {
24228
+ title: "string",
24229
+ description: "string?",
24230
+ files: "string[]",
24231
+ dependencies: "number[]",
24232
+ estimated_complexity: "1-5"
24233
+ }
24234
+ ]
24235
+ },
24236
+ validation_note: "Parse agent response as JSON and validate with swarm_validate_decomposition",
24237
+ cass_history: cassResult ? {
24238
+ queried: true,
24239
+ results_found: cassResult.results.length,
24240
+ included_in_context: cassResult.results.length > 0
24241
+ } : { queried: false, reason: "disabled or unavailable" }
24242
+ }, null, 2);
24243
+ }
24244
+ });
23929
24245
  var swarm_decompose = tool({
23930
24246
  description: "Generate decomposition prompt for breaking task into parallelizable subtasks. Optionally queries CASS for similar past tasks.",
23931
24247
  args: {
@@ -24285,7 +24601,8 @@ var swarm_record_outcome = tool({
24285
24601
  retry_count: tool.schema.number().int().min(0).default(0).describe("Number of retry attempts"),
24286
24602
  success: tool.schema.boolean().describe("Whether the subtask succeeded"),
24287
24603
  files_touched: tool.schema.array(tool.schema.string()).optional().describe("Files that were modified"),
24288
- criteria: tool.schema.array(tool.schema.string()).optional().describe("Criteria to generate feedback for (default: all default criteria)")
24604
+ criteria: tool.schema.array(tool.schema.string()).optional().describe("Criteria to generate feedback for (default: all default criteria)"),
24605
+ strategy: tool.schema.enum(["file-based", "feature-based", "risk-based"]).optional().describe("Decomposition strategy used for this task")
24289
24606
  },
24290
24607
  async execute(args) {
24291
24608
  const signals = {
@@ -24295,7 +24612,8 @@ var swarm_record_outcome = tool({
24295
24612
  retry_count: args.retry_count ?? 0,
24296
24613
  success: args.success,
24297
24614
  files_touched: args.files_touched ?? [],
24298
- timestamp: new Date().toISOString()
24615
+ timestamp: new Date().toISOString(),
24616
+ strategy: args.strategy
24299
24617
  };
24300
24618
  const validated = OutcomeSignalsSchema.parse(signals);
24301
24619
  const scored = scoreImplicitFeedback(validated, DEFAULT_LEARNING_CONFIG);
@@ -24305,7 +24623,13 @@ var swarm_record_outcome = tool({
24305
24623
  "patterns",
24306
24624
  "readable"
24307
24625
  ];
24308
- const feedbackEvents = criteriaToScore.map((criterion) => outcomeToFeedback(scored, criterion));
24626
+ const feedbackEvents = criteriaToScore.map((criterion) => {
24627
+ const event = outcomeToFeedback(scored, criterion);
24628
+ if (args.strategy) {
24629
+ event.context = `${event.context || ""} [strategy: ${args.strategy}]`.trim();
24630
+ }
24631
+ return event;
24632
+ });
24309
24633
  return JSON.stringify({
24310
24634
  success: true,
24311
24635
  outcome: {
@@ -24322,7 +24646,8 @@ var swarm_record_outcome = tool({
24322
24646
  duration_seconds: Math.round(args.duration_ms / 1000),
24323
24647
  error_count: args.error_count ?? 0,
24324
24648
  retry_count: args.retry_count ?? 0,
24325
- success: args.success
24649
+ success: args.success,
24650
+ strategy: args.strategy
24326
24651
  },
24327
24652
  note: "Feedback events should be stored for criterion weight calculation. Use learning.ts functions to apply weights."
24328
24653
  }, null, 2);
@@ -24542,6 +24867,8 @@ var swarm_init = tool({
24542
24867
  });
24543
24868
  var swarmTools = {
24544
24869
  swarm_init,
24870
+ swarm_select_strategy,
24871
+ swarm_plan_prompt,
24545
24872
  swarm_decompose,
24546
24873
  swarm_validate_decomposition,
24547
24874
  swarm_status,
@@ -25027,6 +25354,7 @@ export {
25027
25354
  swarmTools,
25028
25355
  structuredTools,
25029
25356
  setStorage,
25357
+ selectStrategy,
25030
25358
  resetToolCache,
25031
25359
  resetStorage,
25032
25360
  requireTool,
@@ -25040,6 +25368,7 @@ export {
25040
25368
  formatToolAvailability,
25041
25369
  formatSubtaskPromptV2,
25042
25370
  formatSubtaskPrompt,
25371
+ formatStrategyGuidelines,
25043
25372
  formatEvaluationPrompt,
25044
25373
  extractJsonFromText,
25045
25374
  src_default as default,
@@ -25072,6 +25401,7 @@ export {
25072
25401
  SpawnedAgentSchema,
25073
25402
  SemanticMemoryStorage,
25074
25403
  SUBTASK_PROMPT_V2,
25404
+ STRATEGIES,
25075
25405
  InMemoryStorage,
25076
25406
  FileReservationConflictError,
25077
25407
  EvaluationSchema,