forked from parruda/swarm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllms.txt
More file actions
383 lines (380 loc) · 12.7 KB
/
llms.txt
File metadata and controls
383 lines (380 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# claude-swarm
claude-swarm is a Ruby gem that orchestrates multiple Claude Code instances as a collaborative AI development team. It enables running AI agents with specialized roles, tools, and directory contexts, communicating via MCP (Model Context Protocol).
## Purpose
claude-swarm allows you to:
- Create teams of specialized AI agents working together
- Restrict tool access per agent for safety and focus
- Organize agents in tree-like hierarchies for delegation
- Maintain separate working directories per agent
- Enable inter-agent communication via MCP protocol
- Track sessions and restore previous swarm states
## Installation
```bash
# Install via RubyGems
gem install claude_swarm
# Or add to Gemfile
gem 'claude_swarm'
bundle install
```
### Prerequisites
- Ruby 3.2.0 or higher
- Claude CLI installed and configured (`claude` command available)
- Any MCP servers you plan to use (optional)
## Quick Start
1. Initialize a configuration:
```bash
claude-swarm init
```
2. Or create `claude-swarm.yml`:
```yaml
version: 1
swarm:
name: "My Dev Team"
main: lead
instances:
lead:
description: "Team lead coordinating development"
directory: .
model: opus
connections: [frontend, backend]
allowed_tools: [Read, Edit, Bash]
prompt: "You coordinate the team"
frontend:
description: "Frontend developer"
directory: ./frontend
model: sonnet
allowed_tools: [Read, Edit, Write, "Bash(npm:*)"]
```
3. Start the swarm:
```bash
claude-swarm
claude-swarm --vibe # Allow all tools (dangerous!)
```
## Key Concepts
### Swarm
A collection of Claude instances (agents) working together. One instance is designated as "main" - the entry point that coordinates others.
### Instance
An individual Claude Code agent with:
- **description** (required): Role and responsibilities
- **directory**: Working directory context
- **model**: Claude model (opus/sonnet)
- **connections**: Other instances it can delegate to
- **allowed_tools**: Tools this instance can use
- **disallowed_tools**: Explicitly denied tools (override allowed)
- **prompt**: Custom system prompt
- **vibe**: Skip permission checks for this instance
- **mcps**: Additional MCP servers
### MCP (Model Context Protocol)
Protocol enabling Claude to use external tools. claude-swarm uses MCP for:
- Inter-instance communication (task delegation)
- Permission management
- Additional tool integration
## Configuration Format
```yaml
version: 1
swarm:
name: "Swarm Name"
main: instance_key # Entry point instance
instances:
instance_name:
description: "Agent role description" # REQUIRED
directory: ~/path/to/dir # Working directory
model: opus # opus/sonnet
connections: [other1, other2] # Connected instances
prompt: "Custom system prompt" # Additional instructions
vibe: false # Skip permissions (default: false)
allowed_tools: # Tools this instance can use
- Read
- Edit
- Write
- Bash
- WebSearch
- WebFetch
disallowed_tools: # Explicitly deny patterns
- "Write(*.log)"
- "Bash(rm:*)"
mcps: # Additional MCP servers
- name: database
type: stdio
command: /path/to/mcp
args: ["--flag"]
env:
API_KEY: "value"
- name: api
type: sse
url: https://example.com/mcp
```
## Tool Patterns
Tools can be restricted using patterns:
- `Bash(npm:*)` - Only npm commands in Bash
- `Write(*.md)` - Only write markdown files
- `mcp__frontend__*` - All frontend MCP tools
- `Read` - Unrestricted Read tool
Disallowed tools always override allowed tools.
## Inter-Instance Communication
Instances communicate via the `task` tool exposed by connected instances:
### Task Tool
When instance A connects to instance B, A can use:
```
mcp__B__task
```
**Parameters:**
- `prompt` (required): The task or question for the agent
- `new_session` (optional): Start fresh session (default: false)
- `system_prompt` (optional): Override system prompt for this request
**Example Usage:**
```
Main instance: "Please analyze the frontend performance"
→ Uses mcp__frontend__task with prompt "Analyze performance bottlenecks"
→ Frontend instance executes with its tools and returns results
```
### Session Info Tool
```
mcp__B__session_info
```
Returns current Claude session ID and working directory.
### Reset Session Tool
```
mcp__B__reset_session
```
Resets the Claude session for a fresh start.
## Commands
### Start Swarm
```bash
# Default configuration
claude-swarm
# Custom configuration
claude-swarm --config team.yml
claude-swarm -c my-swarm.yml
# Skip all permissions (dangerous!)
claude-swarm --vibe
# Non-interactive mode with prompt
claude-swarm -p "Build a login feature"
claude-swarm --prompt "Fix the bug" --stream-logs
# Resume session
claude-swarm --session-id 20241225_120000
claude-swarm --session-id ~/.claude-swarm/sessions/project/20241225_120000
```
### List Sessions
```bash
# List recent sessions (default: 10)
claude-swarm list-sessions
# List more sessions
claude-swarm list-sessions --limit 20
# List for specific project
claude-swarm list-sessions --project myapp
```
### Other Commands
```bash
# Initialize starter config
claude-swarm init
# Show version
claude-swarm version
# Start permission MCP server (for testing)
claude-swarm tools-mcp --allowed-tools 'Read,Edit,mcp__*'
# Internal MCP serve command (used by swarm)
claude-swarm mcp-serve -n NAME -d DIR -m MODEL [options]
```
## Session Management
### Session Structure
Sessions are stored in `~/.claude-swarm/sessions/{project}/{timestamp}/`:
```
20241225_143022/
├── config.yml # Swarm configuration used
├── state/ # Instance states
│ ├── lead_abc123.json # Lead instance state
│ └── frontend_def456.json # Frontend instance state
├── lead.mcp.json # MCP configurations
├── frontend.mcp.json
├── session.log # Human-readable logs
├── session.log.json # JSONL format events
└── permissions.log # Permission decisions
```
### Session Files
- **config.yml**: Copy of swarm configuration
- **state/*.json**: Claude session IDs for each instance
- **session.log**: Request/response tracking
- **session.log.json**: All events in JSONL format
- **permissions.log**: Tool permission checks
### Restoring Sessions (Experimental)
```bash
claude-swarm --session-id 20241225_143022
```
**Limitations:**
- Main instance conversation context not fully restored
- Only session IDs preserved, not full state
- Connected instances restore more reliably
## Debugging
### Enable Debug Output
```bash
claude-swarm --debug
```
### Check Logs
```bash
# Session logs
cat ~/.claude-swarm/sessions/PROJECT/TIMESTAMP/session.log
cat ~/.claude-swarm/sessions/PROJECT/TIMESTAMP/permissions.log
# JSONL events
cat ~/.claude-swarm/sessions/PROJECT/TIMESTAMP/session.log.json
```
### Common Issues
**"Configuration file not found"**
- Ensure `claude-swarm.yml` exists
- Or use `--config path/to/config.yml`
**"Main instance not found"**
- Check `main:` references valid instance key
**"Circular dependency detected"**
- Remove circular connections between instances
**"Tool not allowed"**
- Check `allowed_tools` includes the tool
- Check `disallowed_tools` doesn't block it
- Use `vibe: true` to skip all checks (dangerous)
**"MCP server failed to start"**
- Check the command/URL is correct
- Verify MCP server is installed
- Check logs for error details
## Common Patterns
### Full-Stack Team
```yaml
instances:
architect:
description: "System architect"
connections: [frontend_lead, backend_lead, devops]
frontend_lead:
description: "Frontend team lead"
connections: [ui_dev, ux_designer]
backend_lead:
description: "Backend team lead"
connections: [api_dev, db_admin]
```
### Investigation Team
```yaml
instances:
investigator:
description: "Lead investigator"
connections: [data_analyst, code_expert, test_runner]
data_analyst:
description: "Analyzes metrics and logs"
allowed_tools: ["Bash(grep:*, awk:*)", Read]
code_expert:
description: "Reviews code changes"
allowed_tools: [Read, Grep, Glob]
```
### Mixed Permissions
```yaml
instances:
trusted_lead:
description: "Trusted lead with full access"
vibe: true # Skip all permission checks
restricted_worker:
description: "Limited access worker"
allowed_tools: [Read] # Read-only access
normal_worker:
description: "Standard developer"
allowed_tools: [Read, Edit, Write]
```
## Complete Example
### Performance Investigation Swarm
```yaml
version: 1
swarm:
name: "Performance Investigation"
main: coordinator
instances:
coordinator:
description: "Coordinates performance investigation"
directory: ~/projects/webapp
model: opus
connections: [metrics_analyst, code_reviewer, fix_implementer]
allowed_tools: [Read]
prompt: |
You coordinate a performance investigation team.
1. Use metrics_analyst to identify when/where issues occur
2. Use code_reviewer to find root causes
3. Use fix_implementer to create solutions
metrics_analyst:
description: "Analyzes performance metrics"
directory: ~/projects/webapp/logs
model: sonnet
allowed_tools: [Read, "Bash(grep:*, awk:*, sort:*)"]
prompt: |
You analyze logs and metrics for performance issues.
Focus on response times, error rates, and patterns.
code_reviewer:
description: "Reviews code for performance issues"
directory: ~/projects/webapp/src
model: opus
allowed_tools: [Read, Grep, Glob]
prompt: |
You review code for performance bottlenecks.
Look for N+1 queries, missing indexes, inefficient algorithms.
fix_implementer:
description: "Implements performance fixes"
directory: ~/projects/webapp
model: opus
allowed_tools: [Read, Edit, Write, Bash]
prompt: |
You implement optimizations and fixes.
Always add tests and measure improvements.
```
### Usage Flow
1. **User**: "The checkout page is slow"
2. **Coordinator** → **metrics_analyst**: "Find checkout performance metrics"
3. **Metrics analyst** returns: "Latency spike at 2pm, 3x increase"
4. **Coordinator** → **code_reviewer**: "What changed around 2pm?"
5. **Code reviewer** returns: "HTTP client gem updated, removed connection pooling"
6. **Coordinator** → **fix_implementer**: "Add connection pooling configuration"
7. **Fix implementer**: Creates fix and tests
## How It Works
1. **Configuration Parsing**: Validates YAML and expands paths
2. **MCP Generation**: Creates MCP configs in `.claude-swarm/`
3. **Permission Setup**: Adds permission MCP server (unless vibe mode)
4. **Instance Launch**: Starts main instance with connections
5. **Task Delegation**: Main instance uses mcp__instance__task tools
6. **Session Tracking**: All activity logged to session directory
## Development
```bash
# Setup
bin/setup
# Run tests
rake test
# Lint code
rake rubocop -A
# Console
bin/console
# Install locally
bundle exec rake install
# Release to RubyGems
bundle exec rake release
```
## Architecture
- **CLI** (`cli.rb`): Thor-based commands
- **Configuration** (`configuration.rb`): YAML parser/validator
- **McpGenerator** (`mcp_generator.rb`): Creates MCP JSON configs
- **Orchestrator** (`orchestrator.rb`): Launches main instance
- **ClaudeCodeExecutor** (`claude_code_executor.rb`): Executes Claude
- **ClaudeMcpServer** (`claude_mcp_server.rb`): Inter-instance MCP
- **PermissionMcpServer** (`permission_mcp_server.rb`): Tool permissions
## Environment Variables
- `CLAUDE_SWARM_HOME`: Override session storage location (default: ~/.claude-swarm)
- `ANTHROPIC_MODEL`: Default Claude model if not specified
## Security Considerations
- Tool restrictions enforced through configuration
- Vibe mode bypasses ALL restrictions - use carefully
- Session files may contain sensitive data
- Each instance runs with its directory context
- MCP servers inherit instance tool configurations
## Limitations
- Tree hierarchy only (no circular dependencies)
- Session restoration is experimental
- Main instance context not fully preserved
- All paths expand relative to execution directory
- MCP servers must support required protocol
## Best Practices
1. **Clear Descriptions**: Help agents understand their roles
2. **Focused Tools**: Give each instance only needed tools
3. **Directory Context**: Place instances in relevant directories
4. **Prompt Engineering**: Use prompts to guide behavior
5. **Test Configurations**: Start simple, add complexity gradually
6. **Monitor Logs**: Check session logs for issues
7. **Use Vibe Sparingly**: Only for trusted operations