claude_swarm 1.0.4 → 1.0.6

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 (64) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +24 -0
  3. data/Rakefile +4 -4
  4. data/docs/v2/CHANGELOG.swarm_cli.md +19 -0
  5. data/docs/v2/CHANGELOG.swarm_memory.md +19 -0
  6. data/docs/v2/CHANGELOG.swarm_sdk.md +92 -0
  7. data/docs/v2/README.md +56 -22
  8. data/docs/v2/guides/MEMORY_DEFRAG_GUIDE.md +811 -0
  9. data/docs/v2/guides/complete-tutorial.md +115 -3
  10. data/docs/v2/guides/getting-started.md +6 -6
  11. data/docs/v2/guides/rails-integration.md +6 -6
  12. data/docs/v2/reference/architecture-flow.md +407 -0
  13. data/docs/v2/reference/event_payload_structures.md +471 -0
  14. data/docs/v2/reference/execution-flow.md +600 -0
  15. data/docs/v2/reference/ruby-dsl.md +138 -5
  16. data/docs/v2/reference/swarm_memory_technical_details.md +2090 -0
  17. data/examples/v2/swarm_with_hooks.yml +1 -1
  18. data/lib/claude_swarm/cli.rb +9 -11
  19. data/lib/claude_swarm/commands/ps.rb +1 -2
  20. data/lib/claude_swarm/configuration.rb +2 -3
  21. data/lib/claude_swarm/mcp_generator.rb +4 -10
  22. data/lib/claude_swarm/orchestrator.rb +43 -44
  23. data/lib/claude_swarm/system_utils.rb +4 -4
  24. data/lib/claude_swarm/version.rb +1 -1
  25. data/lib/claude_swarm.rb +4 -9
  26. data/lib/swarm_cli/commands/mcp_serve.rb +2 -2
  27. data/lib/swarm_cli/commands/mcp_tools.rb +3 -3
  28. data/lib/swarm_cli/config_loader.rb +14 -13
  29. data/lib/swarm_cli/version.rb +1 -1
  30. data/lib/swarm_cli.rb +2 -0
  31. data/lib/swarm_memory/adapters/base.rb +4 -4
  32. data/lib/swarm_memory/adapters/filesystem_adapter.rb +0 -12
  33. data/lib/swarm_memory/core/storage.rb +66 -6
  34. data/lib/swarm_memory/integration/sdk_plugin.rb +14 -0
  35. data/lib/swarm_memory/optimization/defragmenter.rb +4 -0
  36. data/lib/swarm_memory/tools/memory_edit.rb +1 -0
  37. data/lib/swarm_memory/tools/memory_glob.rb +24 -1
  38. data/lib/swarm_memory/tools/memory_write.rb +2 -2
  39. data/lib/swarm_memory/version.rb +1 -1
  40. data/lib/swarm_memory.rb +2 -0
  41. data/lib/swarm_sdk/agent/chat.rb +1 -1
  42. data/lib/swarm_sdk/agent/definition.rb +18 -21
  43. data/lib/swarm_sdk/configuration.rb +34 -10
  44. data/lib/swarm_sdk/mcp.rb +16 -0
  45. data/lib/swarm_sdk/node/agent_config.rb +7 -2
  46. data/lib/swarm_sdk/node/builder.rb +130 -35
  47. data/lib/swarm_sdk/node_context.rb +75 -0
  48. data/lib/swarm_sdk/node_orchestrator.rb +219 -12
  49. data/lib/swarm_sdk/plugin.rb +73 -1
  50. data/lib/swarm_sdk/prompts/base_system_prompt.md.erb +0 -126
  51. data/lib/swarm_sdk/result.rb +32 -6
  52. data/lib/swarm_sdk/swarm/builder.rb +1 -0
  53. data/lib/swarm_sdk/swarm.rb +32 -50
  54. data/lib/swarm_sdk/tools/delegate.rb +2 -2
  55. data/lib/swarm_sdk/tools/scratchpad/scratchpad_list.rb +23 -2
  56. data/lib/swarm_sdk/tools/scratchpad/scratchpad_read.rb +23 -2
  57. data/lib/swarm_sdk/tools/scratchpad/scratchpad_write.rb +21 -4
  58. data/lib/swarm_sdk/tools/stores/storage.rb +4 -4
  59. data/lib/swarm_sdk/tools/think.rb +4 -1
  60. data/lib/swarm_sdk/tools/todo_write.rb +20 -8
  61. data/lib/swarm_sdk/version.rb +1 -1
  62. data/lib/swarm_sdk.rb +332 -27
  63. data/swarm_sdk.gemspec +1 -1
  64. metadata +9 -3
@@ -0,0 +1,471 @@
1
+ # SwarmSDK Event Payload Structures
2
+
3
+ This document describes the exact structure of all SwarmSDK event payloads emitted via `LogStream.emit()`.
4
+
5
+ All events automatically include a `timestamp` field (ISO8601 format) added by `LogStream.emit()`.
6
+
7
+ ## Event Types
8
+
9
+ ### 1. swarm_start
10
+
11
+ Emitted when `Swarm.execute()` is called, before any agent execution begins.
12
+
13
+ **Location**: `lib/swarm_sdk/swarm.rb:670-677`
14
+
15
+ ```ruby
16
+ {
17
+ type: "swarm_start",
18
+ timestamp: "2025-01-15T10:30:45Z", # Auto-added by LogStream
19
+ agent: :lead_agent_name, # Lead agent name (for consistency)
20
+ swarm_name: "Development Team", # Swarm name
21
+ lead_agent: :lead_agent_name, # Lead agent name
22
+ prompt: "Build authentication system" # User's task prompt
23
+ }
24
+ ```
25
+
26
+ **Field Locations**:
27
+ - Root level: `type`, `timestamp`, `agent`, `swarm_name`, `lead_agent`, `prompt`
28
+ - No nested metadata
29
+
30
+ ---
31
+
32
+ ### 2. swarm_stop
33
+
34
+ Emitted when swarm execution completes (success or error).
35
+
36
+ **Location**: `lib/swarm_sdk/swarm.rb:685-696`
37
+
38
+ ```ruby
39
+ {
40
+ type: "swarm_stop",
41
+ timestamp: "2025-01-15T10:35:22Z",
42
+ swarm_name: "Development Team",
43
+ lead_agent: :lead_agent_name,
44
+ last_agent: "backend", # Agent that produced final response
45
+ content: "Authentication system complete", # Final response content (can be nil)
46
+ success: true, # Boolean: true if no error
47
+ duration: 277.5, # Seconds (Float)
48
+ total_cost: 0.00234, # USD (Float)
49
+ total_tokens: 12450, # Integer
50
+ agents_involved: [:lead, :backend, :frontend] # Array of agent names
51
+ }
52
+ ```
53
+
54
+ **Field Locations**:
55
+ - Root level: All fields are at root level
56
+ - No nested metadata for this event
57
+
58
+ ---
59
+
60
+ ### 3. agent_start
61
+
62
+ Emitted once per agent when agents are initialized (lazy initialization).
63
+
64
+ **Location**: `lib/swarm_sdk/swarm.rb:567-578`
65
+
66
+ ```ruby
67
+ {
68
+ type: "agent_start",
69
+ timestamp: "2025-01-15T10:30:46Z",
70
+ agent: :backend, # Agent name
71
+ swarm_name: "Development Team",
72
+ model: "gpt-5", # Model ID
73
+ provider: "openai", # Provider name
74
+ directory: "./backend", # Working directory
75
+ system_prompt: "You are a backend dev...", # Full system prompt
76
+ tools: [:Read, :Edit, :Bash, :DelegateTaskToFrontend], # Array of tool names
77
+ delegates_to: [:frontend], # Array of delegate agent names
78
+ plugin_storages: { # Plugin storage info (optional)
79
+ memory: {
80
+ enabled: true,
81
+ config: { directory: ".swarm/backend-memory" }
82
+ }
83
+ }
84
+ }
85
+ ```
86
+
87
+ **Field Locations**:
88
+ - Root level: All fields except plugin configs
89
+ - Nested in `plugin_storages`: Per-plugin configuration
90
+
91
+ ---
92
+
93
+ ### 4. agent_stop
94
+
95
+ Emitted when an agent completes with a final response (no more tool calls).
96
+
97
+ **Location**: `lib/swarm_sdk/swarm.rb:746-756`
98
+
99
+ ```ruby
100
+ {
101
+ type: "agent_stop",
102
+ timestamp: "2025-01-15T10:31:20Z",
103
+ agent: "backend",
104
+ model: "gpt-5",
105
+ content: "I've implemented the auth system", # Final response text
106
+ tool_calls: nil, # Always nil for agent_stop
107
+ finish_reason: "stop", # "stop", "finish_agent", "finish_swarm"
108
+ usage: { # Usage statistics
109
+ input_tokens: 2450,
110
+ output_tokens: 856,
111
+ total_tokens: 3306,
112
+ input_cost: 0.001225,
113
+ output_cost: 0.000428,
114
+ total_cost: 0.001653,
115
+ cumulative_input_tokens: 8920, # Total for entire conversation
116
+ cumulative_output_tokens: 3530,
117
+ cumulative_total_tokens: 12450,
118
+ context_limit: 200000,
119
+ tokens_used_percentage: "6.23%",
120
+ tokens_remaining: 187550
121
+ },
122
+ tool_executions: nil, # Array of tool execution results (if any)
123
+ metadata: { # Additional context (minimal)
124
+ # Fields promoted to root level are excluded here
125
+ }
126
+ }
127
+ ```
128
+
129
+ **Field Locations**:
130
+ - Root level: `type`, `timestamp`, `agent`, `model`, `content`, `tool_calls`, `finish_reason`, `tool_executions`, `metadata`
131
+ - Nested in `usage`: All token counts, costs, and context tracking
132
+ - Nested in `metadata`: Minimal (most fields promoted to root)
133
+
134
+ **Note**: The `metadata` field in `agent_stop` has most fields extracted to root level to avoid duplication (see `swarm.rb:743`).
135
+
136
+ ---
137
+
138
+ ### 5. agent_step
139
+
140
+ Emitted when an agent makes an intermediate response with tool calls (agent hasn't finished yet).
141
+
142
+ **Location**: `lib/swarm_sdk/swarm.rb:725-735`
143
+
144
+ ```ruby
145
+ {
146
+ type: "agent_step",
147
+ timestamp: "2025-01-15T10:31:15Z",
148
+ agent: "backend",
149
+ model: "gpt-5",
150
+ content: "I'll read the config files", # Agent's reasoning/message
151
+ tool_calls: [ # Array of tool calls
152
+ {
153
+ id: "call_abc123",
154
+ name: "Read",
155
+ arguments: { file_path: "config/auth.rb" }
156
+ }
157
+ ],
158
+ finish_reason: "tool_calls", # Always "tool_calls" for agent_step
159
+ usage: { # Same structure as agent_stop
160
+ input_tokens: 1850,
161
+ output_tokens: 156,
162
+ total_tokens: 2006,
163
+ input_cost: 0.000925,
164
+ output_cost: 0.000078,
165
+ total_cost: 0.001003,
166
+ cumulative_input_tokens: 6470,
167
+ cumulative_output_tokens: 2674,
168
+ cumulative_total_tokens: 9144,
169
+ context_limit: 200000,
170
+ tokens_used_percentage: "4.57%",
171
+ tokens_remaining: 190856
172
+ },
173
+ tool_executions: [], # Usually empty for steps (filled on completion)
174
+ metadata: {
175
+ # Fields promoted to root level are excluded here
176
+ }
177
+ }
178
+ ```
179
+
180
+ **Field Locations**:
181
+ - Root level: `type`, `timestamp`, `agent`, `model`, `content`, `tool_calls`, `finish_reason`, `usage`, `tool_executions`, `metadata`
182
+ - Nested in `tool_calls`: Array of `{ id, name, arguments }` objects
183
+ - Nested in `usage`: All token and cost information
184
+ - Nested in `metadata`: Minimal (most fields promoted to root)
185
+
186
+ **Note**: Similar to `agent_stop`, but `finish_reason` is always `"tool_calls"` and `tool_calls` array is populated.
187
+
188
+ ---
189
+
190
+ ### 6. user_prompt
191
+
192
+ Emitted when a user message is about to be sent to the LLM.
193
+
194
+ **Location**: `lib/swarm_sdk/swarm.rb:705-714`
195
+
196
+ ```ruby
197
+ {
198
+ type: "user_prompt",
199
+ timestamp: "2025-01-15T10:31:10Z",
200
+ agent: "backend",
201
+ model: "gpt-5",
202
+ provider: "openai",
203
+ message_count: 5, # Number of messages in conversation so far
204
+ tools: [:Read, :Edit, :Bash], # Available tools (excluding delegation tools)
205
+ delegates_to: ["frontend"], # Agents this agent can delegate to
206
+ metadata: { # Full context available here
207
+ prompt: "Build authentication",
208
+ message_count: 5,
209
+ model: "gpt-5",
210
+ provider: "openai",
211
+ tools: [:Read, :Edit, :Bash],
212
+ delegates_to: ["frontend"],
213
+ timestamp: "2025-01-15T10:31:10Z"
214
+ }
215
+ }
216
+ ```
217
+
218
+ **Field Locations**:
219
+ - Root level: `type`, `timestamp`, `agent`, `model`, `provider`, `message_count`, `tools`, `delegates_to`, `metadata`
220
+ - Nested in `metadata`: Complete copy of all context fields (including prompt)
221
+
222
+ **Important**: The `metadata` hash contains the original context from the hook, including the `prompt` field which is NOT promoted to root level for this event type.
223
+
224
+ ---
225
+
226
+ ### 7. tool_call
227
+
228
+ Emitted before a tool is executed (pre_tool_use hook).
229
+
230
+ **Location**: `lib/swarm_sdk/swarm.rb:766-773`
231
+
232
+ ```ruby
233
+ {
234
+ type: "tool_call",
235
+ timestamp: "2025-01-15T10:31:16Z",
236
+ agent: "backend",
237
+ tool_call_id: "call_abc123", # Unique call ID
238
+ tool: "Read", # Tool name
239
+ arguments: { # Tool parameters
240
+ file_path: "config/auth.rb"
241
+ },
242
+ metadata: {} # Additional context (usually empty)
243
+ }
244
+ ```
245
+
246
+ **Field Locations**:
247
+ - Root level: `type`, `timestamp`, `agent`, `tool_call_id`, `tool`, `arguments`, `metadata`
248
+ - Nested in `arguments`: Tool-specific parameters
249
+ - Nested in `metadata`: Additional context (typically minimal)
250
+
251
+ ---
252
+
253
+ ### 8. tool_result
254
+
255
+ Emitted after a tool completes execution (post_tool_use hook).
256
+
257
+ **Location**: `lib/swarm_sdk/swarm.rb:783-790`
258
+
259
+ ```ruby
260
+ {
261
+ type: "tool_result",
262
+ timestamp: "2025-01-15T10:31:17Z",
263
+ agent: "backend",
264
+ tool_call_id: "call_abc123", # Matches the tool_call ID
265
+ tool: "Read", # Tool name
266
+ result: "# Authentication config\n...", # Tool output (can be String, Hash, Array)
267
+ metadata: {} # Additional context (usually empty)
268
+ }
269
+ ```
270
+
271
+ **Field Locations**:
272
+ - Root level: `type`, `timestamp`, `agent`, `tool_call_id`, `tool`, `result`, `metadata`
273
+ - `result` can be:
274
+ - String: Direct text output
275
+ - Hash/Array: Structured data
276
+ - See `LoggingHelpers.serialize_result()` for how different types are handled
277
+
278
+ ---
279
+
280
+ ## Additional Event Types
281
+
282
+ ### 9. model_lookup_warning
283
+
284
+ Emitted when a model isn't found in the registry during initialization.
285
+
286
+ **Location**: `lib/swarm_sdk/swarm.rb:445-452`
287
+
288
+ ```ruby
289
+ {
290
+ type: "model_lookup_warning",
291
+ timestamp: "2025-01-15T10:30:46Z",
292
+ agent: :backend,
293
+ model: "gpt-5-turbo", # Invalid model name
294
+ error_message: "Model 'gpt-5-turbo' not...", # Error description
295
+ suggestions: [ # Similar models found
296
+ {
297
+ id: "gpt-5",
298
+ name: "GPT-5",
299
+ context_window: 200000
300
+ }
301
+ ]
302
+ }
303
+ ```
304
+
305
+ ---
306
+
307
+ ### 10. context_limit_warning
308
+
309
+ Emitted when context usage crosses threshold percentages (60%, 80%, 90%, 95%).
310
+
311
+ **Location**: `lib/swarm_sdk/swarm.rb:798-808`
312
+
313
+ ```ruby
314
+ {
315
+ type: "context_limit_warning",
316
+ timestamp: "2025-01-15T10:32:45Z",
317
+ agent: "backend",
318
+ model: "gpt-5",
319
+ threshold: "80%", # Threshold crossed
320
+ current_usage: "82.5%", # Current usage
321
+ tokens_used: 165000,
322
+ tokens_remaining: 35000,
323
+ context_limit: 200000,
324
+ metadata: {} # Agent context metadata
325
+ }
326
+ ```
327
+
328
+ ---
329
+
330
+ ### 11. agent_delegation
331
+
332
+ Emitted when an agent delegates work to another agent.
333
+
334
+ **Location**: `lib/swarm_sdk/agent/chat/context_tracker.rb:186-193`
335
+
336
+ ```ruby
337
+ {
338
+ type: "agent_delegation",
339
+ timestamp: "2025-01-15T10:31:30Z",
340
+ agent: "backend",
341
+ tool_call_id: "call_xyz789",
342
+ delegate_to: "frontend", # Target agent name
343
+ arguments: { # Delegation parameters
344
+ prompt: "Build the login UI"
345
+ },
346
+ metadata: {} # Agent context metadata
347
+ }
348
+ ```
349
+
350
+ ---
351
+
352
+ ### 12. delegation_result
353
+
354
+ Emitted when a delegated task completes and returns to the delegating agent.
355
+
356
+ **Location**: `lib/swarm_sdk/agent/chat/context_tracker.rb:163-170`
357
+
358
+ ```ruby
359
+ {
360
+ type: "delegation_result",
361
+ timestamp: "2025-01-15T10:32:15Z",
362
+ agent: "backend", # Agent that delegated
363
+ delegate_from: "frontend", # Agent that was delegated to
364
+ tool_call_id: "call_xyz789", # Matches delegation tool_call_id
365
+ result: "Login UI implemented", # Result from delegate
366
+ metadata: {} # Agent context metadata
367
+ }
368
+ ```
369
+
370
+ ---
371
+
372
+ ### 13. context_compression
373
+
374
+ Emitted when automatic context compression is triggered at 60% threshold.
375
+
376
+ **Location**: `lib/swarm_sdk/agent/chat/context_tracker.rb:301-310`
377
+
378
+ ```ruby
379
+ {
380
+ type: "context_compression",
381
+ timestamp: "2025-01-15T10:33:00Z",
382
+ agent: "backend",
383
+ total_messages: 45, # Total messages after compression
384
+ messages_compressed: 12, # Number of messages compressed
385
+ tokens_before: 125000, # Token count before compression
386
+ current_usage: "62.5%", # Usage after compression
387
+ compression_strategy: "progressive_tool_result_compression",
388
+ keep_recent: 10 # Number of recent messages kept uncompressed
389
+ }
390
+ ```
391
+
392
+ ---
393
+
394
+ ### 14. llm_retry_attempt
395
+
396
+ Emitted when an LLM API call fails and is being retried.
397
+
398
+ **Location**: `lib/swarm_sdk/agent/chat.rb:813-823`
399
+
400
+ ```ruby
401
+ {
402
+ type: "llm_retry_attempt",
403
+ timestamp: "2025-01-15T10:31:18Z",
404
+ agent: "backend",
405
+ model: "gpt-5",
406
+ attempt: 1, # Current attempt number
407
+ max_retries: 10, # Maximum retries
408
+ error_class: "Faraday::ConnectionFailed", # Error class name
409
+ error_message: "Connection refused", # Error message
410
+ retry_delay: 10 # Seconds before next retry
411
+ }
412
+ ```
413
+
414
+ ---
415
+
416
+ ### 15. llm_retry_exhausted
417
+
418
+ Emitted when all LLM API retry attempts are exhausted.
419
+
420
+ **Location**: `lib/swarm_sdk/agent/chat.rb:802-809`
421
+
422
+ ```ruby
423
+ {
424
+ type: "llm_retry_exhausted",
425
+ timestamp: "2025-01-15T10:33:30Z",
426
+ agent: "backend",
427
+ model: "gpt-5",
428
+ attempts: 10, # Total attempts made
429
+ error_class: "Faraday::ConnectionFailed",
430
+ error_message: "Connection refused"
431
+ }
432
+ ```
433
+
434
+ ---
435
+
436
+ ## Summary: Field Location Guide
437
+
438
+ ### Always at Root Level
439
+ - `type` - Event type identifier
440
+ - `timestamp` - ISO8601 timestamp (auto-added)
441
+ - `agent` - Agent name
442
+
443
+ ### Event-Specific Root Fields
444
+
445
+ | Event | Root Fields | Nested Fields |
446
+ |-------|-------------|---------------|
447
+ | `swarm_start` | swarm_name, lead_agent, prompt | None |
448
+ | `swarm_stop` | swarm_name, lead_agent, last_agent, content, success, duration, total_cost, total_tokens, agents_involved | None |
449
+ | `agent_start` | swarm_name, model, provider, directory, system_prompt, tools, delegates_to | plugin_storages.* |
450
+ | `agent_stop` | model, content, tool_calls, finish_reason, tool_executions | usage.*, metadata.* |
451
+ | `agent_step` | model, content, tool_calls, finish_reason, tool_executions | usage.*, tool_calls[].*, metadata.* |
452
+ | `user_prompt` | model, provider, message_count, tools, delegates_to | metadata.* (includes prompt) |
453
+ | `tool_call` | tool_call_id, tool | arguments.*, metadata.* |
454
+ | `tool_result` | tool_call_id, tool, result | metadata.* |
455
+
456
+ ### Important Notes
457
+
458
+ 1. **Usage Information**: Always nested in `usage` hash within `agent_step` and `agent_stop` events
459
+ 2. **Tool Calls**: Nested as array of objects in `tool_calls` field within `agent_step` events
460
+ 3. **Prompt Location**: For `user_prompt` events, the prompt is in `metadata.prompt`, NOT at root level
461
+ 4. **Metadata Deduplication**: `agent_step` and `agent_stop` events have minimal metadata because most fields are promoted to root level (see `swarm.rb:723` and `swarm.rb:744`)
462
+
463
+ ---
464
+
465
+ ## Code References
466
+
467
+ - **LogStream**: `lib/swarm_sdk/log_stream.rb` - Core emission mechanism
468
+ - **Event Emissions**: `lib/swarm_sdk/swarm.rb:664-809` - Default logging callbacks
469
+ - **Agent Events**: `lib/swarm_sdk/agent/chat/context_tracker.rb` - Agent-level event tracking
470
+ - **Hook Integration**: `lib/swarm_sdk/agent/chat/hook_integration.rb` - User prompt event preparation
471
+ - **Logging Helpers**: `lib/swarm_sdk/agent/chat/logging_helpers.rb` - Tool call/result formatting