opencode-swarm-plugin 0.20.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/.beads/issues.jsonl +213 -0
  2. package/INTEGRATION_EXAMPLE.md +66 -0
  3. package/README.md +352 -522
  4. package/dist/index.js +2046 -984
  5. package/dist/plugin.js +2051 -1017
  6. package/docs/analysis/subagent-coordination-patterns.md +2 -0
  7. package/docs/semantic-memory-cli-syntax.md +123 -0
  8. package/docs/swarm-mail-architecture.md +1147 -0
  9. package/evals/README.md +116 -0
  10. package/evals/evalite.config.ts +15 -0
  11. package/evals/example.eval.ts +32 -0
  12. package/evals/fixtures/decomposition-cases.ts +105 -0
  13. package/evals/lib/data-loader.test.ts +288 -0
  14. package/evals/lib/data-loader.ts +111 -0
  15. package/evals/lib/llm.ts +115 -0
  16. package/evals/scorers/index.ts +200 -0
  17. package/evals/scorers/outcome-scorers.test.ts +27 -0
  18. package/evals/scorers/outcome-scorers.ts +349 -0
  19. package/evals/swarm-decomposition.eval.ts +112 -0
  20. package/package.json +8 -1
  21. package/scripts/cleanup-test-memories.ts +346 -0
  22. package/src/beads.ts +49 -0
  23. package/src/eval-capture.ts +487 -0
  24. package/src/index.ts +45 -3
  25. package/src/learning.integration.test.ts +19 -4
  26. package/src/output-guardrails.test.ts +438 -0
  27. package/src/output-guardrails.ts +381 -0
  28. package/src/schemas/index.ts +18 -0
  29. package/src/schemas/swarm-context.ts +115 -0
  30. package/src/storage.ts +117 -5
  31. package/src/streams/events.test.ts +296 -0
  32. package/src/streams/events.ts +93 -0
  33. package/src/streams/migrations.test.ts +24 -20
  34. package/src/streams/migrations.ts +51 -0
  35. package/src/streams/projections.ts +187 -0
  36. package/src/streams/store.ts +275 -0
  37. package/src/swarm-orchestrate.ts +771 -189
  38. package/src/swarm-prompts.ts +84 -12
  39. package/src/swarm.integration.test.ts +124 -0
  40. package/vitest.integration.config.ts +6 -0
  41. package/vitest.integration.setup.ts +48 -0
package/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  # opencode-swarm-plugin
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/opencode-swarm-plugin.svg)](https://www.npmjs.com/package/opencode-swarm-plugin)
4
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
4
 
6
5
  ```
7
6
  ███████╗██╗ ██╗ █████╗ ██████╗ ███╗ ███╗
@@ -13,636 +12,467 @@
13
12
 
14
13
  \ ` - ' /
15
14
  - .(o o). -
16
- ( >.< ) Multi-agent coordination for OpenCode
17
- /| |\ Break complex tasks into parallel subtasks,
18
- (_| |_) spawn agents, coordinate via messaging.
19
- bzzzz... The plugin learns from outcomes.
20
- ```
21
-
22
- ## Install
23
-
24
- ```bash
25
- npm install -g opencode-swarm-plugin@latest
26
- swarm setup
27
- ```
28
-
29
- The setup wizard handles everything:
30
-
31
- ```
32
- ┌ opencode-swarm-plugin v0.16.0
33
-
34
- ◇ Checking dependencies...
35
-
36
- ◆ OpenCode
37
- ◆ Beads
38
- ▲ CASS (optional)
39
- ▲ UBS (optional)
40
- ▲ semantic-memory (optional)
41
-
42
- ◇ Setting up OpenCode integration...
43
-
44
- ◆ Plugin: ~/.config/opencode/plugin/swarm.ts
45
- ◆ Command: ~/.config/opencode/command/swarm.md
46
- ◆ Agent: ~/.config/opencode/agent/swarm-planner.md
47
-
48
- Setup complete!
49
- ```
50
-
51
- Then in your project:
52
-
53
- ```bash
54
- cd your-project
55
- swarm init
56
- ```
57
-
58
- ## Migrating from MCP Agent Mail
59
-
60
- If you were using the MCP-based Agent Mail (pre-v0.15), here's how to migrate:
61
-
62
- ### What Changed
15
+ ( >.< ) Break big tasks into small ones.
16
+ /| |\ Spawn agents to work in parallel.
17
+ (_| |_) Learn from what works.
18
+ bzzzz...
19
+ ```
20
+
21
+ ## The Problem
22
+
23
+ You're working with an AI coding agent. You ask it to "add OAuth authentication." It starts writing code. Five minutes later, you realize it's going down the wrong path. Or it's touching files it shouldn't. Or it's making changes that conflict with what you just did in another session.
24
+
25
+ **The fundamental issue:** AI agents are single-threaded, context-limited, and have no memory of what worked before.
26
+
27
+ ## The Solution
28
+
29
+ What if the agent could:
30
+
31
+ - **Break the task into pieces** that can be worked on simultaneously
32
+ - **Spawn parallel workers** that don't step on each other
33
+ - **Remember what worked** and avoid patterns that failed
34
+ - **Survive context compaction** without losing progress
35
+
36
+ That's what Swarm does.
37
+
38
+ ## How It Works
39
+
40
+ ```
41
+ "Add OAuth"
42
+
43
+
44
+ ┌────────────────────────┐
45
+ │ COORDINATOR │
46
+
47
+ 1. Query CASS: │
48
+ │ "How did we solve │
49
+ │ this before?" │
50
+ │ │
51
+ │ 2. Pick strategy: │
52
+ │ file-based? │
53
+ │ feature-based? │
54
+ │ risk-based? │
55
+ │ │
56
+ │ 3. Break into pieces │
57
+ └────────────────────────┘
58
+
59
+ ┌─────────────────────┼─────────────────────┐
60
+ ▼ ▼ ▼
61
+ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
62
+ │ Worker A │ │ Worker B │ │ Worker C │
63
+ │ │ │ │ │ │
64
+ │ auth/oauth │ │ auth/session│ │ auth/tests │
65
+ │ 🔒 files │ │ 🔒 files │ │ 🔒 files │
66
+ │ │ │ │ │ │
67
+ │ "I need │──────►│ "Got it, │ │ "Running │
68
+ │ session │ │ here's the │ │ tests..." │
69
+ │ types" │ │ interface" │ │ │
70
+ └─────────────┘ └─────────────┘ └─────────────┘
71
+ │ │ │
72
+ │ │ │
73
+ └─────────────────────┼─────────────────────┘
74
+
75
+
76
+ ┌────────────────────────┐
77
+ │ LEARNING SYSTEM │
78
+ │ │
79
+ │ "File-based split │
80
+ │ worked well for │
81
+ │ auth - 3 workers, │
82
+ │ 15 min, 0 conflicts" │
83
+ │ │
84
+ │ Next time: use this │
85
+ │ pattern again │
86
+ └────────────────────────┘
87
+ ```
88
+
89
+ ### The Flow
90
+
91
+ 1. **You give it a task**: `/swarm "Add OAuth authentication"`
92
+
93
+ 2. **It queries history**: "Have we done something like this before?" (via CASS - cross-agent session search)
94
+
95
+ 3. **It picks a strategy**:
96
+ - **File-based**: "Split by directory structure" (good for refactoring)
97
+ - **Feature-based**: "Split by vertical slices" (good for new features)
98
+ - **Risk-based**: "Tests first, then implementation" (good for bug fixes)
99
+ - **Research-based**: "Explore before committing" (good for unknowns)
100
+
101
+ 4. **It breaks the work into beads** (git-backed issues):
63
102
 
64
- - **Before:** Agent Mail required a separate MCP server running Go-based agent-mail
65
- - **After:** Agent Mail is now embedded using PGLite (no external dependencies)
66
-
67
- ### Migration Steps
68
-
69
- 1. **Update the plugin:**
70
-
71
- ```bash
72
- npm install -g opencode-swarm-plugin@latest
103
+ ```
104
+ Epic: Add OAuth
105
+ ├─ Bead 1: OAuth provider integration (src/auth/oauth.ts)
106
+ ├─ Bead 2: Session management (src/auth/session.ts)
107
+ └─ Bead 3: Integration tests (tests/auth/)
73
108
  ```
74
109
 
75
- 2. **Remove MCP configuration** (if present):
76
- - Delete any `agent-mail` MCP server configuration from your OpenCode config
77
- - The embedded version starts automatically
78
-
79
- 3. **Data Migration:**
80
- - Previous MCP data is NOT automatically migrated
81
- - For most users, starting fresh is recommended (swarm state is ephemeral)
82
- - If you need historical data, export from MCP before upgrading
83
-
84
- ### Breaking Changes
110
+ 5. **It spawns parallel workers**:
111
+ - Each worker reserves its files (no conflicts)
112
+ - Workers coordinate via Swarm Mail (actor-model messaging)
113
+ - Progress is checkpointed at 25%, 50%, 75%
85
114
 
86
- - `agentmail_*` tools now use embedded PGLite instead of MCP
87
- - No external server required
88
- - Slightly different error messages (more actionable)
115
+ 6. **It learns from the outcome**:
116
+ - Fast + success = good signal
117
+ - Slow + errors = bad signal
118
+ - Patterns that fail >60% of the time get auto-inverted
89
119
 
90
- ### Rollback
120
+ ## What Makes It Different
91
121
 
92
- If you need to rollback:
122
+ ### It Survives Context Death
93
123
 
94
- ```bash
95
- npm install -g opencode-swarm-plugin@0.14.x
96
- ```
97
-
98
- And restore your MCP configuration.
99
-
100
- ## CLI
124
+ OpenCode compacts context when it gets too long. Swarms used to die when this happened. Not anymore.
101
125
 
102
126
  ```
103
- swarm setup Interactive installer - checks and installs all dependencies
104
- swarm doctor Health check - shows status of all dependencies
105
- swarm init Initialize beads in current project
106
- swarm config Show paths to generated config files
107
- swarm version Show version and banner
108
- swarm help Show help
127
+ Session 1 Context Session 2
128
+ │ Compacts │
129
+ ▼ 💥 ▼
130
+ ┌─────────────────┐ ┌─────────────────┐
131
+ swarm running │ │ swarm_recover()
132
+ ├─ 25% ✓ saved │ │ │ │
133
+ │ ├─ 50% ✓ saved │ ─────────────────────────────────►│ ▼ │
134
+ │ └─ 75% ✓ saved │ checkpoints survive │ resume at 75% │
135
+ └─────────────────┘ └─────────────────┘
109
136
  ```
110
137
 
111
- ## Usage
138
+ **Checkpoints capture:**
112
139
 
113
- In OpenCode:
140
+ - Which subtasks are done/in-progress/pending
141
+ - File reservations (who owns what)
142
+ - Shared context for workers
143
+ - Progress percentage
114
144
 
115
- ```
116
- /swarm "Add user authentication with OAuth"
117
- ```
145
+ **Recovery restores:**
118
146
 
119
- Or invoke the planner directly:
147
+ - Swarm state from last checkpoint
148
+ - File locks (prevents conflicts)
149
+ - Worker context (what they were doing)
120
150
 
121
- ```
122
- @swarm-planner "Refactor all components to use hooks"
123
- ```
151
+ All stored in PGLite (embedded Postgres) - no external servers, survives across sessions.
124
152
 
125
- ## Customization
153
+ ### It Learns From Outcomes
126
154
 
127
- Run `swarm config` to see your config file paths:
155
+ Every swarm completion records:
128
156
 
129
- ```
130
- 🔌 Plugin loader
131
- ~/.config/opencode/plugin/swarm.ts
157
+ - Duration (how long did it take?)
158
+ - Errors (how many retries?)
159
+ - Files touched (did scope match prediction?)
160
+ - Success (did tests pass? were changes accepted?)
132
161
 
133
- 📜 /swarm command prompt
134
- ~/.config/opencode/command/swarm.md
162
+ This feeds back into the decomposition strategy:
135
163
 
136
- 🤖 @swarm-planner agent
137
- ~/.config/opencode/agent/swarm-planner.md
138
164
  ```
165
+ ┌─────────────────────────────────┐
166
+ │ LEARNING LOOP │
167
+ └─────────────────────────────────┘
168
+
169
+ ┌───────────────────────────┼───────────────────────────┐
170
+ ▼ ▼ ▼
171
+ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐
172
+ │ OUTCOMES │ │ PATTERNS │ │ ANTI-PATTERNS │
173
+ │ │ │ │ │ │
174
+ │ fast+success │ │ candidate │ │ >60% failure │
175
+ │ = good signal │──────────►│ ↓ │──────────►│ = auto-invert │
176
+ │ │ │ established │ │ │
177
+ │ slow+errors │ │ ↓ │ │ "split by X" │
178
+ │ = bad signal │ │ proven │ │ becomes │
179
+ │ │ │ │ │ "DON'T split │
180
+ └───────────────┘ └───────────────┘ │ by X" │
181
+ └───────────────┘
139
182
 
140
- ### /swarm Command
141
-
142
- The `/swarm` command is defined in `~/.config/opencode/command/swarm.md`:
143
-
144
- ```markdown
145
- ---
146
- description: Decompose task into parallel subtasks and coordinate agents
147
- ---
183
+ Confidence decays over 90 days
184
+ unless patterns are revalidated
185
+ ```
148
186
 
149
- You are a swarm coordinator. Decompose the task into beads and spawn parallel agents.
187
+ **Pattern maturity lifecycle:**
150
188
 
151
- ## Task
189
+ - `candidate` → new pattern, low confidence
190
+ - `established` → validated 3+ times
191
+ - `proven` → 10+ successes (gets 1.5x weight in future decompositions)
192
+ - `deprecated` → >60% failure rate (auto-inverted to anti-pattern)
152
193
 
153
- $ARGUMENTS
194
+ **Confidence decay:** Patterns fade over 90 days unless revalidated. Prevents stale knowledge from dominating.
154
195
 
155
- ## Workflow
196
+ ### Swarm Mail: Actor-Model Coordination
156
197
 
157
- 1. **Initialize**: `swarmmail_init` with project_path and task_description
158
- 2. **Decompose**: Use `swarm_select_strategy` then `swarm_plan_prompt`
159
- 3. **Create beads**: `beads_create_epic` with subtasks and file assignments
160
- 4. **Reserve files**: `swarmmail_reserve` for each subtask's files
161
- 5. **Spawn agents**: Use Task tool with `swarm_spawn_subtask` prompts
162
- 6. **Monitor**: Check `swarmmail_inbox` for progress
163
- 7. **Complete**: `swarm_complete` when done, then `beads_sync` to push
198
+ Workers don't just run in parallel - they coordinate via **Swarm Mail**, an event-sourced actor model built on local-first primitives.
164
199
 
165
- ## Strategy Selection
200
+ **What makes Swarm Mail different from traditional agent messaging:**
166
201
 
167
- | Strategy | Best For | Keywords |
168
- | ------------- | ----------------------- | ------------------------------------- |
169
- | file-based | Refactoring, migrations | refactor, migrate, rename, update all |
170
- | feature-based | New features | add, implement, build, create |
171
- | risk-based | Bug fixes, security | fix, bug, security, critical, urgent |
202
+ - **Actor model over durable streams** - DurableMailbox, DurableLock, DurableDeferred (inspired by Electric SQL patterns)
203
+ - **Local-first with PGlite** - embedded Postgres, no external servers, survives across sessions
204
+ - **Event-sourced coordination** - append-only log, materialized views, full audit trail
205
+ - **Context-safe by design** - hard caps on inbox (max 5 messages), thread summarization, body-on-demand
172
206
 
173
- Begin decomposition now.
174
207
  ```
175
-
176
- > **Note**: The `$ARGUMENTS` placeholder captures everything you type after `/swarm`. This is how your task description gets passed to the agent.
177
-
178
- ### Agents
179
-
180
- The setup wizard creates two agents with your chosen models:
181
-
182
- **@swarm-planner** (`~/.config/opencode/agent/swarm-planner.md`) - Coordinator that decomposes tasks:
183
-
184
- ```yaml
185
- ---
186
- name: swarm-planner
187
- description: Strategic task decomposition for swarm coordination
188
- model: anthropic/claude-sonnet-4-5 # Your chosen coordinator model
189
- ---
208
+ ┌──────────────────────────────────────────────────────────────┐
209
+ │ SWARM MAIL │
210
+ │ │
211
+ │ Worker A: "I need the SessionUser type" │
212
+ │ ↓ │
213
+ │ Worker B: "Here's the interface:" │
214
+ │ interface SessionUser { │
215
+ │ id: string │
216
+ │ email: string │
217
+ │ roles: string[] │
218
+ │ } │
219
+ │ ↓ │
220
+ │ Worker A: "Got it, implementing OAuth flow now" │
221
+ │ │
222
+ └──────────────────────────────────────────────────────────────┘
190
223
  ```
191
224
 
192
- **@swarm-worker** (`~/.config/opencode/agent/swarm-worker.md`) - Fast executor for subtasks:
225
+ **File reservations** prevent conflicts:
193
226
 
194
- ```yaml
195
- ---
196
- name: swarm-worker
197
- description: Executes subtasks in a swarm - fast, focused, cost-effective
198
- model: anthropic/claude-haiku-4-5 # Your chosen worker model
199
- ---
200
- ```
201
-
202
- ### Decomposition Rules
203
-
204
- - **2-7 subtasks** - Too few = not parallel, too many = coordination overhead
205
- - **No file overlap** - Each file appears in exactly one subtask
206
- - **Include tests** - Put test files with the code they test
207
- - **Order by dependency** - If B needs A's output, A comes first (lower index)
208
-
209
- Edit these files to customize behavior. Run `swarm setup` to regenerate defaults.
227
+ - Worker A reserves `src/auth/oauth.ts` (exclusive via DurableLock)
228
+ - Worker B tries to reserve it → blocked
229
+ - Worker B waits or works on something else
210
230
 
211
- ## Skills
231
+ **Inbox limits** prevent context bloat:
212
232
 
213
- Skills are reusable knowledge packages that agents can load on-demand. They contain domain expertise, workflows, and patterns that help agents perform specialized tasks.
214
-
215
- ### Using Skills
216
-
217
- ```bash
218
- # List available skills
219
- swarm tool skills_list
233
+ - Max 5 messages per fetch (headers only)
234
+ - Read individual message bodies on demand
235
+ - Thread summarization for long conversations
220
236
 
221
- # Read a skill's content
222
- swarm tool skills_read --json '{"name": "debugging"}'
237
+ All coordination state survives context compaction and session restarts.
223
238
 
224
- # Use a skill (get formatted for context injection)
225
- swarm tool skills_use --json '{"name": "code-review", "context": "reviewing a PR"}'
226
- ```
239
+ #### Architecture: 3-Tier Stack
227
240
 
228
- In OpenCode, agents can use skills directly:
241
+ Swarm Mail is built on **Durable Streams primitives** (inspired by Kyle Matthews' [Electric SQL patterns](https://x.com/kylemathews/status/1999896667030700098)):
229
242
 
230
243
  ```
231
- skills_list() # See what's available
232
- skills_use(name="debugging") # Load debugging patterns
233
- skills_use(name="swarm-coordination") # Load swarm workflow
244
+ ┌─────────────────────────────────────────────────────────────┐
245
+ │ SWARM MAIL STACK │
246
+ ├─────────────────────────────────────────────────────────────┤
247
+ │ │
248
+ │ TIER 3: COORDINATION │
249
+ │ ┌───────────────────────────────────────────────────────┐ │
250
+ │ │ ask<Req, Res>() - Request/Response (RPC-style) │ │
251
+ │ └───────────────────────────────────────────────────────┘ │
252
+ │ │ │
253
+ │ TIER 2: PATTERNS ▼ │
254
+ │ ┌─────────────────┐ ┌─────────────────┐ │
255
+ │ │ DurableMailbox │ │ DurableLock │ │
256
+ │ │ Actor Inbox │ │ File Mutex │ │
257
+ │ └─────────────────┘ └─────────────────┘ │
258
+ │ │ │ │
259
+ │ TIER 1: PRIMITIVES ▼ │
260
+ │ ┌─────────────────┐ ┌─────────────────┐ │
261
+ │ │ DurableCursor │ │ DurableDeferred │ │
262
+ │ │ Checkpointed │ │ Distributed │ │
263
+ │ │ Reader │ │ Promise │ │
264
+ │ └─────────────────┘ └─────────────────┘ │
265
+ │ │ │
266
+ │ STORAGE ▼ │
267
+ │ ┌───────────────────────────────────────────────────────┐ │
268
+ │ │ PGLite (Embedded Postgres) + Migrations │ │
269
+ │ └───────────────────────────────────────────────────────┘ │
270
+ │ │
271
+ └─────────────────────────────────────────────────────────────┘
234
272
  ```
235
273
 
236
- ### Bundled Skills
237
-
238
- | Skill | Tags | Description |
239
- | -------------------- | -------------------- | ------------------------------------------------------------------------------------ |
240
- | `cli-builder` | cli, typescript, bun | Building TypeScript CLIs with Bun - argument parsing, subcommands, output formatting |
241
- | `learning-systems` | learning, feedback | Implicit feedback scoring, confidence decay, anti-pattern detection |
242
- | `mcp-tool-authoring` | mcp, tools | Building MCP tools - schema definition, context passing, error handling |
243
- | `skill-creator` | meta, skills | Guide for creating effective skills |
244
- | `swarm-coordination` | swarm, multi-agent | Complete swarm playbook - strategies, coordinator patterns, failure recovery |
245
- | `system-design` | design, architecture | Building reusable systems - deep modules, complexity management, design red flags |
246
-
247
- ### Skill Locations
248
-
249
- Skills are loaded from three locations (in order):
274
+ **Tier 1 - Primitives:**
250
275
 
251
- 1. **Project skills**: `.opencode/skills/`, `.claude/skills/`, or `skills/`
252
- 2. **Global skills**: `~/.config/opencode/skills/`
253
- 3. **Bundled skills**: Included with the plugin
276
+ - **DurableCursor** - Positioned event stream consumption with checkpointing (exactly-once)
277
+ - **DurableDeferred** - URL-addressable distributed promises for async coordination
278
+ - **DurableLock** - CAS-based mutual exclusion for file reservations (TTL + retry/backoff)
254
279
 
255
- ### Creating Skills
280
+ **Tier 2 - Patterns:**
256
281
 
257
- ```bash
258
- # Initialize project skills directory
259
- swarm tool skills_init
260
-
261
- # Create a new skill
262
- swarm tool skills_create --json '{"name": "my-skill", "description": "What it does", "tags": ["tag1", "tag2"]}'
263
- ```
282
+ - **DurableMailbox** - Actor inbox with typed envelopes (sender, replyTo, payload)
283
+ - File reservation protocol built on DurableLock
264
284
 
265
- Or use the `skill-creator` skill for guidance:
285
+ **Tier 3 - Coordination:**
266
286
 
267
- ```
268
- skills_use(name="skill-creator")
269
- ```
287
+ - **ask()** pattern - Synchronous-style RPC over async streams (creates DurableDeferred, appends to mailbox, returns promise)
270
288
 
271
- Each skill is a directory containing:
289
+ #### Message Flow Example
272
290
 
273
291
  ```
274
- my-skill/
275
- SKILL.md # Main content (required)
276
- references/ # Optional supporting files
277
- patterns.md
278
- examples.md
292
+ Agent A Event Stream Agent B
293
+ │ │ │
294
+ ask("get SessionUser") │ │
295
+ ├───────────────────────────>│ │
296
+ │ (creates deferred) │ │
297
+ │ │ consume event │
298
+ │ ├────────────────────────>│
299
+ │ │ │
300
+ │ │ reply to deferred │
301
+ │ │<────────────────────────┤
302
+ │ await deferred.value │ │
303
+ │<───────────────────────────┤ │
304
+ │ │ │
305
+ │ SessionUser interface │ │
306
+ │ │ │
279
307
  ```
280
308
 
281
- ### SKILL.md Format
309
+ **Why this matters:**
282
310
 
283
- ```markdown
284
- ---
285
- name: my-skill
286
- description: Brief description for discovery
287
- tags:
288
- - tag1
289
- - tag2
290
- ---
311
+ - No external servers (Redis, Kafka, NATS) - just PGlite
312
+ - Full audit trail - every message is an event
313
+ - Resumable - cursors checkpoint position, survive crashes
314
+ - Type-safe - Effect-TS with full inference
291
315
 
292
- # My Skill
316
+ > **Architecture deep-dive:** See [Swarm Mail Architecture](docs/swarm-mail-architecture.md) for complete implementation details, database schemas, and Effect-TS patterns.
293
317
 
294
- ## When to Use
318
+ ### It Has Skills
295
319
 
296
- - Trigger condition 1
297
- - Trigger condition 2
320
+ Skills are knowledge packages agents can load. Teach once, use everywhere.
298
321
 
299
- ## Patterns
322
+ ```typescript
323
+ skills_use((name = "testing-patterns")); // Load Feathers seams + Beck's 4 rules
324
+ skills_use((name = "swarm-coordination")); // Load swarm workflow patterns
325
+ ```
300
326
 
301
- ### Pattern Name
327
+ **Bundled skills:**
302
328
 
303
- Description and examples...
329
+ - `testing-patterns` - 25 dependency-breaking techniques, characterization tests
330
+ - `swarm-coordination` - Multi-agent decomposition, file reservations
331
+ - `cli-builder` - Argument parsing, help text, subcommands
332
+ - `system-design` - Architecture decisions, module boundaries
333
+ - `learning-systems` - Confidence decay, pattern maturity
304
334
 
305
- ## Anti-Patterns
335
+ **Create your own:**
306
336
 
307
- What NOT to do...
337
+ ```bash
338
+ swarm init # Creates .opencode/skills/ in project
308
339
  ```
309
340
 
310
- ## Dependencies
311
-
312
- | Dependency | Purpose | Required |
313
- | ------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------ | -------- |
314
- | [OpenCode](https://opencode.ai) | Plugin host | Yes |
315
- | [Beads](https://github.com/steveyegge/beads) | Git-backed issue tracking | Yes |
316
- | [CASS (Coding Agent Session Search)](https://github.com/Dicklesworthstone/coding_agent_session_search) | Historical context from past sessions | No |
317
- | [UBS (Ultimate Bug Scanner)](https://github.com/Dicklesworthstone/ultimate_bug_scanner) | Pre-completion bug scanning using AI-powered static analysis | No |
318
- | [semantic-memory](https://github.com/joelhooks/semantic-memory) | Learning persistence | No |
319
- | [Redis](https://redis.io) | Rate limiting (SQLite fallback available) | No |
341
+ Skills can include:
320
342
 
321
- All dependencies are checked and can be installed via `swarm setup`.
343
+ - Step-by-step workflows
344
+ - Code examples
345
+ - Reference documentation
346
+ - Executable scripts
322
347
 
323
- ### Installing Optional Dependencies
324
-
325
- **UBS (Ultimate Bug Scanner)** - Scans code for bugs before task completion:
348
+ ## Install
326
349
 
327
350
  ```bash
328
- curl -fsSL "https://raw.githubusercontent.com/Dicklesworthstone/ultimate_bug_scanner/master/install.sh" | bash
351
+ npm install -g opencode-swarm-plugin@latest
352
+ swarm setup
329
353
  ```
330
354
 
331
- **CASS (Coding Agent Session Search)** - Indexes and searches AI coding agent history:
355
+ ## Usage
332
356
 
333
357
  ```bash
334
- curl -fsSL https://raw.githubusercontent.com/Dicklesworthstone/coding_agent_session_search/main/install.sh | bash -s -- --easy-mode
358
+ /swarm "Add user authentication with OAuth"
335
359
  ```
336
360
 
337
- > **Note:** Swarm Mail is now embedded (PGLite in-process) and works out of the box with no external dependencies. No Go or external servers required.
338
-
339
- ## Tools Reference
340
-
341
- ### Swarm
342
-
343
- | Tool | Description |
344
- | ------------------------------ | ------------------------------------------------------------------------- |
345
- | `swarm_init` | Initialize swarm session |
346
- | `swarm_select_strategy` | Analyze task, recommend decomposition strategy (file/feature/risk-based) |
347
- | `swarm_plan_prompt` | Generate strategy-specific planning prompt with CASS history |
348
- | `swarm_decompose` | Generate decomposition prompt |
349
- | `swarm_validate_decomposition` | Validate response, detect file conflicts |
350
- | `swarm_spawn_subtask` | Generate worker agent prompt with Swarm Mail/beads instructions |
351
- | `swarm_status` | Get swarm progress by epic ID |
352
- | `swarm_progress` | Report subtask progress to coordinator |
353
- | `swarm_complete` | Complete subtask - runs UBS (Ultimate Bug Scanner), releases reservations |
354
- | `swarm_record_outcome` | Record outcome for learning (duration, errors, retries) |
355
-
356
- ### Beads
357
-
358
- | Tool | Description |
359
- | ------------------- | ---------------------------------------------- |
360
- | `beads_create` | Create bead with type-safe validation |
361
- | `beads_create_epic` | Create epic + subtasks atomically |
362
- | `beads_query` | Query beads with filters (status, type, ready) |
363
- | `beads_update` | Update status/description/priority |
364
- | `beads_close` | Close bead with reason |
365
- | `beads_start` | Mark bead as in-progress |
366
- | `beads_ready` | Get next unblocked bead |
367
- | `beads_sync` | Sync to git and push |
368
- | `beads_link_thread` | Link bead to Swarm Mail thread |
369
-
370
- ### Swarm Mail (Embedded - Primary)
371
-
372
- | Tool | Description |
373
- | ------------------------ | --------------------------------------------- |
374
- | `swarmmail_init` | Initialize session, register agent |
375
- | `swarmmail_send` | Send message to agents |
376
- | `swarmmail_inbox` | Fetch inbox (max 5, no bodies - context safe) |
377
- | `swarmmail_read_message` | Fetch single message body by ID |
378
- | `swarmmail_reserve` | Reserve file paths for exclusive editing |
379
- | `swarmmail_release` | Release file reservations |
380
- | `swarmmail_ack` | Acknowledge message |
381
- | `swarmmail_health` | Check embedded database health |
382
-
383
- ### Agent Mail (Deprecated - MCP-based)
384
-
385
- > **Note:** The MCP-based `agentmail_*` tools in `src/agent-mail.ts` are **deprecated** as of v0.14.0. They remain for backward compatibility but will be removed in v1.0.0.
386
- >
387
- > **Use `swarmmail_*` tools instead** - embedded PGLite implementation with no external server required. See [Migrating from MCP Agent Mail](#migrating-from-mcp-agent-mail) for migration guide.
388
-
389
- ## Event-Sourced Architecture (Embedded)
390
-
391
- > **🙏 Built on the shoulders of giants**
392
- >
393
- > The Swarm Mail system is deeply inspired by and builds upon [**MCP Agent Mail**](https://github.com/Dicklesworthstone/mcp_agent_mail) by [@Dicklesworthstone](https://github.com/Dicklesworthstone). The original MCP Agent Mail pioneered multi-agent coordination patterns including file reservations, thread-based messaging, and agent registration - concepts that form the foundation of this embedded implementation.
394
- >
395
- > If you need a production-ready, battle-tested solution with a full Go server, **use MCP Agent Mail directly**. This embedded version is an experimental alternative that trades the external server for in-process PGLite, optimized for single-machine development workflows.
396
- >
397
- > **Key contributions from MCP Agent Mail:**
398
- >
399
- > - File reservation protocol with conflict detection
400
- > - Agent registration and heartbeat patterns
401
- > - Thread-based message organization
402
- > - Importance levels and acknowledgment tracking
403
- >
404
- > Thank you to the MCP Agent Mail team for open-sourcing such a well-designed system.
405
-
406
- > **🎯 Quality Patterns from Superpowers**
407
- >
408
- > Several verification and debugging patterns in this plugin are inspired by [**Superpowers**](https://github.com/obra/superpowers) by [@obra](https://github.com/obra) (Jesse Vincent). Superpowers is a complete software development workflow for coding agents built on composable "skills".
409
- >
410
- > **Key patterns adopted:**
411
- >
412
- > - **Verification Gate** - The Gate Function (IDENTIFY → RUN → READ → VERIFY → CLAIM) ensures no completion claims without fresh verification evidence
413
- > - **3-Strike Architecture Rule** - After 3 failed fixes, question the architecture, not the bug
414
- > - **CSO (Claude Search Optimization)** - Skill descriptions that answer "Should I read this right now?"
415
- > - **Defense-in-Depth** - Validate at every layer data passes through
416
- >
417
- > Thank you Jesse for open-sourcing such a thoughtfully designed system.
418
-
419
- The plugin includes an embedded event-sourced Swarm Mail implementation as an alternative to the external MCP server. This provides the same multi-agent coordination capabilities without requiring a separate server process.
420
-
421
- ### Architecture Comparison
422
-
423
- **MCP-based (deprecated, external):**
424
-
425
- ```
426
- plugin tools → HTTP → MCP Server (Go process) → SQLite
427
- ```
361
+ The coordinator will:
428
362
 
429
- **Event-sourced (embedded, current):**
363
+ 1. Query CASS for similar past tasks
364
+ 2. Select decomposition strategy
365
+ 3. Break into subtasks (beads)
366
+ 4. Spawn parallel workers
367
+ 5. Track progress with checkpoints
368
+ 6. Record outcome for learning
430
369
 
431
- ```
432
- plugin tools → streams/agent-mail.ts → streams/store.ts → PGLite (in-process)
433
-
434
- streams/projections.ts
435
-
436
- Materialized views (agents, messages, reservations)
437
-
438
- Fast reads
439
- ```
370
+ ## Architecture
440
371
 
441
- ### Architecture Flow
372
+ Everything runs in-process. No external servers.
442
373
 
443
374
  ```
444
375
  ┌─────────────────────────────────────────────────────────────────┐
445
- Plugin Tools Layer
446
- │ (swarmmail_init, swarmmail_send, swarmmail_reserve, etc.) │
447
- └──────────────────────────────┬──────────────────────────────────┘
448
-
449
-
376
+ YOUR TASK
377
+ └─────────────────────────────────────────────────────────────────┘
378
+
379
+
450
380
  ┌─────────────────────────────────────────────────────────────────┐
451
- streams/agent-mail.ts
452
- (High-level API wrapper)
453
- └──────────────────────────────┬──────────────────────────────────┘
454
-
455
-
381
+ DECOMPOSITION strategy selection, subtask creation
382
+ (queries CASS, semantic memory)
383
+ └─────────────────────────────────────────────────────────────────┘
384
+
385
+
456
386
  ┌─────────────────────────────────────────────────────────────────┐
457
- streams/events.ts
458
- 11 event types: agent_registered, message_sent,
459
- │ file_reserved, message_read, message_acked, etc. │
460
- └──────────────────────────────┬──────────────────────────────────┘
461
-
462
-
387
+ BEADS git-backed issues for each subtask
388
+ (atomic epic + subtasks creation)
389
+ └─────────────────────────────────────────────────────────────────┘
390
+
391
+
463
392
  ┌─────────────────────────────────────────────────────────────────┐
464
- streams/store.ts
465
- Append-only event log (PGLite storage)
466
- │ appendEvent() • readEvents() • replayEvents() │
467
- └──────────────────────────────┬──────────────────────────────────┘
468
-
469
-
393
+ SWARM MAIL actor-model coordination (local-first)
394
+ (DurableMailbox, DurableLock, PGlite)
395
+ └─────────────────────────────────────────────────────────────────┘
396
+
397
+
470
398
  ┌─────────────────────────────────────────────────────────────────┐
471
- streams/projections.ts
472
- Build materialized views from events
473
- │ getAgents() • getInbox() • getActiveReservations() │
474
- checkConflicts() • threadStats() │
475
- └─────────────────────────────┬────────────────────────────────────┘
476
-
477
-
399
+ PGLITE embedded postgres, event-sourced state
400
+ (append-only log, materialized views)
401
+ └─────────────────────────────────────────────────────────────────┘
402
+
403
+
478
404
  ┌─────────────────────────────────────────────────────────────────┐
479
- Materialized Tables (Derived State)
480
- agents messages • reservations • message_reads
481
- │ (Rebuilt by replaying event log) │
405
+ LEARNING outcomes feed back into decomposition
406
+ (confidence decay, pattern maturity)
482
407
  └─────────────────────────────────────────────────────────────────┘
483
408
  ```
484
409
 
485
- ### Module Descriptions
486
-
487
- | Module | Responsibility |
488
- | -------------------------- | -------------------------------------------------------------------------------------------------- |
489
- | **streams/events.ts** | Zod schemas for 11 event types (agent_registered, message_sent, file_reserved, task_progress, etc) |
490
- | **streams/store.ts** | Append-only event log with PGLite backend (appendEvent, readEvents, replayEvents) |
491
- | **streams/projections.ts** | Materialize views from events (getAgents, getInbox, checkConflicts, threadStats) |
492
- | **streams/agent-mail.ts** | High-level API matching MCP interface (initAgent, sendAgentMessage, reserveAgentFiles) |
493
- | **streams/debug.ts** | Debugging utilities (debugEvents, debugAgent, debugMessage, inspectState) |
494
-
495
- ### Key Benefits
496
-
497
- - **No external dependencies** - Runs in-process with PGLite (Postgres compiled to WASM)
498
- - **Full audit trail** - Every state change is an immutable event
499
- - **Crash recovery** - Rebuild state by replaying events from log
500
- - **Time-travel debugging** - Replay events up to any point in time
501
- - **Testability** - 127 tests passing across streams module
502
- - **Durable Streams protocol** - Inspired by Electric SQL's event sourcing patterns
503
-
504
- ### Event Types
505
-
506
- The system emits 11 event types tracked in `streams/events.ts`:
507
-
508
- | Event | Triggered By |
509
- | ------------------ | ------------------------------------- |
510
- | `agent_registered` | Agent initialization |
511
- | `message_sent` | Sending inter-agent message |
512
- | `file_reserved` | Reserving files for exclusive editing |
513
- | `file_released` | Releasing file reservations |
514
- | `message_read` | Reading a message |
515
- | `message_acked` | Acknowledging a message |
516
- | `task_started` | Starting work on a bead |
517
- | `task_progress` | Reporting progress update |
518
- | `task_completed` | Completing a bead |
519
- | `task_blocked` | Marking a task as blocked |
520
- | `agent_active` | Agent heartbeat/keep-alive |
521
-
522
- ### Materialized Views
523
-
524
- Projections build these derived tables from the event log:
410
+ ### Event Sourcing
525
411
 
526
- | View | Contains |
527
- | --------------- | ------------------------------------------------------ |
528
- | `agents` | Registered agents with metadata and last activity |
529
- | `messages` | All inter-agent messages with thread/importance |
530
- | `reservations` | Active file reservations with TTL and exclusivity flag |
531
- | `message_reads` | Read receipts for message tracking |
412
+ All state is stored as an append-only event log:
532
413
 
533
- State is always derived - delete the tables and replay events to rebuild.
534
-
535
- ### Structured Output
536
-
537
- | Tool | Description |
538
- | -------------------------------- | ----------------------------------------------------- |
539
- | `structured_extract_json` | Extract JSON from markdown/text (multiple strategies) |
540
- | `structured_validate` | Validate response against schema |
541
- | `structured_parse_evaluation` | Parse self-evaluation response |
542
- | `structured_parse_decomposition` | Parse task decomposition response |
543
- | `structured_parse_bead_tree` | Parse bead tree for epic creation |
544
-
545
- ## Decomposition Strategies
546
-
547
- ### File-Based
548
-
549
- Best for: refactoring, migrations, pattern changes
550
-
551
- - Group files by directory or type
552
- - Handle shared types/utilities first
553
- - Minimize cross-directory dependencies
554
-
555
- **Keywords**: refactor, migrate, rename, update all, replace
556
-
557
- ### Feature-Based
558
-
559
- Best for: new features, adding functionality
560
-
561
- - Each subtask is a complete vertical slice
562
- - Start with data layer, then logic, then UI
563
- - Keep related components together
564
-
565
- **Keywords**: add, implement, build, create, feature
566
-
567
- ### Risk-Based
568
-
569
- Best for: bug fixes, security issues
570
-
571
- - Write tests FIRST
572
- - Isolate risky changes
573
- - Audit similar code for same issue
574
-
575
- **Keywords**: fix, bug, security, critical, urgent
576
-
577
- ## Learning
414
+ ```
415
+ Event Log (PGLite)
416
+ ├─ agent_registered → Agent joins swarm
417
+ ├─ message_sent → Agent-to-agent communication
418
+ ├─ file_reserved → Exclusive file lock acquired
419
+ ├─ file_released → Lock released
420
+ ├─ swarm_checkpointed → Progress snapshot saved
421
+ ├─ decomposition_generated Task broken into subtasks
422
+ └─ subtask_outcome → Worker completion result
578
423
 
579
- The plugin learns from outcomes:
424
+ Materialized Views (derived from events)
425
+ ├─ agents → Active agents per project
426
+ ├─ messages → Agent inbox/outbox
427
+ ├─ file_reservations → Current file locks
428
+ └─ eval_records → Outcome data for learning
429
+ ```
580
430
 
581
- | Mechanism | How It Works |
582
- | ----------------- | ----------------------------------------------------------- |
583
- | Confidence decay | Criteria weights fade unless revalidated (90-day half-life) |
584
- | Implicit feedback | Fast + success = helpful signal, slow + errors = harmful |
585
- | Pattern maturity | candidate → established → proven (or deprecated) |
586
- | Anti-patterns | Patterns with >60% failure rate auto-invert |
431
+ **Why event sourcing?**
587
432
 
588
- ## Context Preservation
433
+ - **Audit trail** - full history of what happened
434
+ - **Replay** - reconstruct state from events
435
+ - **Debugging** - see exactly what went wrong
436
+ - **Learning** - analyze outcomes over time
589
437
 
590
- Hard limits to prevent context exhaustion:
438
+ See the [Swarm Mail Architecture](docs/swarm-mail-architecture.md) section above for details on the durable primitives (DurableCursor, DurableDeferred, DurableLock, DurableMailbox) and how they enable exactly-once processing, request/response patterns, and actor coordination.
591
439
 
592
- | Constraint | Default | Reason |
593
- | ------------------- | ---------- | ------------------------------ |
594
- | Inbox limit | 5 messages | Prevents token burn |
595
- | Bodies excluded | Always | Fetch individually when needed |
596
- | Summarize preferred | Yes | Key points, not raw dump |
440
+ ## Dependencies
597
441
 
598
- ## Rate Limiting
442
+ | Required | Optional |
443
+ | -------------------------------------------- | --------------------------------------------------------------------------------------------- |
444
+ | [OpenCode](https://opencode.ai) | [CASS](https://github.com/Dicklesworthstone/coding_agent_session_search) - historical context |
445
+ | [Beads](https://github.com/steveyegge/beads) | [UBS](https://github.com/Dicklesworthstone/ultimate_bug_scanner) - bug scanning |
446
+ | | [semantic-memory](https://github.com/joelhooks/semantic-memory) - learning persistence |
599
447
 
600
- Client-side limits (Redis primary, SQLite fallback):
448
+ Run `swarm doctor` to check status.
601
449
 
602
- | Endpoint | Per Minute | Per Hour |
603
- | -------- | ---------- | -------- |
604
- | send | 20 | 200 |
605
- | reserve | 10 | 100 |
606
- | inbox | 60 | 600 |
450
+ ## CLI
607
451
 
608
- Configure via `OPENCODE_RATE_LIMIT_{ENDPOINT}_PER_MIN` env vars.
452
+ ```bash
453
+ swarm setup # Install and configure
454
+ swarm doctor # Check dependencies
455
+ swarm init # Initialize beads in project
456
+ swarm config # Show config file paths
457
+ ```
609
458
 
610
459
  ## Development
611
460
 
612
461
  ```bash
613
462
  bun install
614
- bun run typecheck
615
- bun test
463
+ bun test # Unit tests (230 tests)
464
+ bun run test:integration # Integration tests
616
465
  bun run build
617
466
  ```
618
467
 
619
- ## Troubleshooting
620
-
621
- ### 1. First Step: Run Doctor
622
-
623
- ```bash
624
- swarm doctor
625
- ```
626
-
627
- This checks all dependencies and shows their installation status.
628
-
629
- ### 2. Common Issues
630
-
631
- | Issue | Cause | Solution |
632
- | --------------------------- | ---------------------------------- | ------------------------------------------------- |
633
- | `beads: command not found` | Beads CLI not installed | `npm install -g @joelhooks/beads` |
634
- | `bd: command not found` | Same as above | `npm install -g @joelhooks/beads` |
635
- | Verification Gate fails | TypeScript errors or test failures | Fix errors shown, or use `skip_verification=true` |
636
- | File reservation conflict | Another agent has the file | Wait for release, or check `swarmmail_inbox` |
637
- | `Mandate not found` | ID doesn't exist | Use `mandate_list` to see available mandates |
638
- | Swarm Mail connection error | Database not initialized | Run `swarm setup` again |
639
- | `Agent not registered` | Session not initialized | Call `swarmmail_init` first |
468
+ ## Credits
640
469
 
641
- ### 3. Getting Help
470
+ **Inspiration & Core Ideas:**
642
471
 
643
- - Run `swarm doctor` for dependency status
644
- - Check `swarmmail_health` for database status
645
- - File issues at: https://github.com/joelhooks/opencode-swarm-plugin/issues
472
+ - [MCP Agent Mail](https://github.com/Dicklesworthstone/mcp_agent_mail) - **THE INSPIRATION** for multi-agent coordination. Swarm Mail is our implementation built on actor-model primitives (DurableMailbox, DurableLock) with local-first PGlite and event sourcing.
473
+ - [Superpowers](https://github.com/obra/superpowers) - verification patterns, Socratic planning, skill architecture
474
+ - [Electric SQL](https://electric-sql.com) - durable streams and event sourcing patterns that power Swarm Mail
475
+ - [Evalite](https://evalite.dev) - outcome-based evaluation framework for learning systems
646
476
 
647
477
  ## License
648
478