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
data/lib/swarm_sdk.rb CHANGED
@@ -15,25 +15,20 @@ require "yaml"
15
15
  require "async"
16
16
  require "async/semaphore"
17
17
  require "ruby_llm"
18
- require "ruby_llm/mcp"
19
18
 
20
19
  require_relative "swarm_sdk/version"
21
20
 
22
21
  require "zeitwerk"
23
22
  loader = Zeitwerk::Loader.new
23
+ loader.tag = File.basename(__FILE__, ".rb")
24
24
  loader.push_dir("#{__dir__}/swarm_sdk", namespace: SwarmSDK)
25
+ loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
25
26
  loader.inflector.inflect(
26
27
  "cli" => "CLI",
28
+ "openai_with_responses" => "OpenAIWithResponses",
27
29
  )
28
30
  loader.setup
29
31
 
30
- # Load plugin system explicitly (core infrastructure)
31
- require_relative "swarm_sdk/plugin"
32
- require_relative "swarm_sdk/plugin_registry"
33
-
34
- # Load custom providers explicitly (Zeitwerk doesn't eager load by default)
35
- require_relative "swarm_sdk/providers/openai_with_responses"
36
-
37
32
  module SwarmSDK
38
33
  class Error < StandardError; end
39
34
  class ConfigurationError < Error; end
@@ -52,6 +47,170 @@ module SwarmSDK
52
47
  Swarm::Builder.build(&block)
53
48
  end
54
49
 
50
+ # Validate YAML configuration without creating a swarm
51
+ #
52
+ # Performs comprehensive validation of YAML configuration including:
53
+ # - YAML syntax
54
+ # - Required fields (version, swarm name, lead, agents)
55
+ # - Agent configurations (description, directory existence)
56
+ # - Circular dependencies
57
+ # - File references (agent_file paths)
58
+ # - Hook configurations
59
+ #
60
+ # @param yaml_content [String] YAML configuration content
61
+ # @param base_dir [String, Pathname] Base directory for resolving agent file paths (default: Dir.pwd)
62
+ # @return [Array<Hash>] Array of error hashes (empty if valid)
63
+ #
64
+ # @example Validate YAML string
65
+ # errors = SwarmSDK.validate(yaml_content)
66
+ # if errors.empty?
67
+ # puts "Configuration is valid!"
68
+ # else
69
+ # errors.each do |error|
70
+ # puts "#{error[:field]}: #{error[:message]}"
71
+ # end
72
+ # end
73
+ #
74
+ # @example Error hash structure
75
+ # {
76
+ # type: :missing_field, # Error type
77
+ # field: "swarm.agents.backend.description", # JSON-style path to field
78
+ # message: "Agent 'backend' missing required 'description' field",
79
+ # agent: "backend" # Optional, present if error is agent-specific
80
+ # }
81
+ def validate(yaml_content, base_dir: Dir.pwd)
82
+ errors = []
83
+
84
+ begin
85
+ config = Configuration.new(yaml_content, base_dir: base_dir)
86
+ config.load_and_validate
87
+ rescue ConfigurationError, CircularDependencyError => e
88
+ errors << parse_configuration_error(e)
89
+ rescue StandardError => e
90
+ errors << {
91
+ type: :unknown_error,
92
+ field: nil,
93
+ message: e.message,
94
+ }
95
+ end
96
+
97
+ errors
98
+ end
99
+
100
+ # Validate YAML configuration file
101
+ #
102
+ # Convenience method that reads the file and validates the content.
103
+ #
104
+ # @param path [String, Pathname] Path to YAML configuration file
105
+ # @return [Array<Hash>] Array of error hashes (empty if valid)
106
+ #
107
+ # @example
108
+ # errors = SwarmSDK.validate_file("config.yml")
109
+ # if errors.empty?
110
+ # puts "Valid configuration!"
111
+ # swarm = SwarmSDK.load_file("config.yml")
112
+ # else
113
+ # errors.each { |e| puts "Error: #{e[:message]}" }
114
+ # end
115
+ def validate_file(path)
116
+ path = Pathname.new(path).expand_path
117
+
118
+ unless path.exist?
119
+ return [{
120
+ type: :file_not_found,
121
+ field: nil,
122
+ message: "Configuration file not found: #{path}",
123
+ }]
124
+ end
125
+
126
+ yaml_content = File.read(path)
127
+ base_dir = path.dirname
128
+
129
+ validate(yaml_content, base_dir: base_dir)
130
+ rescue StandardError => e
131
+ [{
132
+ type: :file_read_error,
133
+ field: nil,
134
+ message: "Error reading file: #{e.message}",
135
+ }]
136
+ end
137
+
138
+ # Load swarm from YAML string
139
+ #
140
+ # This is the primary programmatic API for loading YAML configurations.
141
+ # For file-based loading, use SwarmSDK.load_file for convenience.
142
+ #
143
+ # @param yaml_content [String] YAML configuration content
144
+ # @param base_dir [String, Pathname] Base directory for resolving agent file paths (default: Dir.pwd)
145
+ # @return [Swarm, NodeOrchestrator] Configured swarm or orchestrator instance
146
+ # @raise [ConfigurationError] If YAML is invalid or configuration is incorrect
147
+ #
148
+ # @example Load from YAML string
149
+ # yaml = <<~YAML
150
+ # version: 2
151
+ # swarm:
152
+ # name: "Dev Team"
153
+ # lead: backend
154
+ # agents:
155
+ # backend:
156
+ # description: "Backend developer"
157
+ # model: "gpt-4"
158
+ # agent_file: "agents/backend.md" # Resolved relative to base_dir
159
+ # YAML
160
+ #
161
+ # swarm = SwarmSDK.load(yaml, base_dir: "/path/to/project")
162
+ # result = swarm.execute("Build authentication")
163
+ #
164
+ # @example Load with default base_dir (Dir.pwd)
165
+ # yaml = File.read("config.yml")
166
+ # swarm = SwarmSDK.load(yaml) # base_dir defaults to Dir.pwd
167
+ def load(yaml_content, base_dir: Dir.pwd)
168
+ config = Configuration.new(yaml_content, base_dir: base_dir)
169
+ config.load_and_validate
170
+ swarm = config.to_swarm
171
+
172
+ # Apply hooks if any are configured (YAML-only feature)
173
+ if hooks_configured?(config)
174
+ Hooks::Adapter.apply_hooks(swarm, config)
175
+ end
176
+
177
+ # Store config reference for agent hooks (applied during initialize_agents)
178
+ swarm.config_for_hooks = config
179
+
180
+ swarm
181
+ end
182
+
183
+ # Load swarm from YAML file (convenience method)
184
+ #
185
+ # Reads the YAML file and uses the file's directory as the base directory
186
+ # for resolving agent file paths. This is the recommended method for
187
+ # loading swarms from configuration files.
188
+ #
189
+ # @param path [String, Pathname] Path to YAML configuration file
190
+ # @return [Swarm, NodeOrchestrator] Configured swarm or orchestrator instance
191
+ # @raise [ConfigurationError] If file not found or configuration invalid
192
+ #
193
+ # @example
194
+ # swarm = SwarmSDK.load_file("config.yml")
195
+ # result = swarm.execute("Build authentication")
196
+ #
197
+ # @example With absolute path
198
+ # swarm = SwarmSDK.load_file("/absolute/path/config.yml")
199
+ def load_file(path)
200
+ config = Configuration.load_file(path)
201
+ swarm = config.to_swarm
202
+
203
+ # Apply hooks if any are configured (YAML-only feature)
204
+ if hooks_configured?(config)
205
+ Hooks::Adapter.apply_hooks(swarm, config)
206
+ end
207
+
208
+ # Store config reference for agent hooks (applied during initialize_agents)
209
+ swarm.config_for_hooks = config
210
+
211
+ swarm
212
+ end
213
+
55
214
  # Configure SwarmSDK global settings
56
215
  def configure
57
216
  self.settings ||= Settings.new
@@ -66,6 +225,171 @@ module SwarmSDK
66
225
  # Alias for backward compatibility
67
226
  alias_method :configuration, :settings
68
227
  alias_method :reset_configuration!, :reset_settings!
228
+
229
+ private
230
+
231
+ # Check if hooks are configured in the configuration
232
+ #
233
+ # @param config [Configuration] Configuration instance
234
+ # @return [Boolean] true if any hooks are configured
235
+ def hooks_configured?(config)
236
+ config.swarm_hooks.any? ||
237
+ config.all_agents_hooks.any? ||
238
+ config.agents.any? { |_, agent_def| agent_def.hooks&.any? }
239
+ end
240
+
241
+ # Parse configuration error and extract structured information
242
+ #
243
+ # Attempts to extract field path and agent name from error messages.
244
+ # Returns a structured error hash with type, field, message, and optional agent.
245
+ #
246
+ # @param error [StandardError] The caught error
247
+ # @return [Hash] Structured error hash
248
+ def parse_configuration_error(error)
249
+ message = error.message
250
+ error_hash = { message: message }
251
+
252
+ # Detect error type and extract field information
253
+ case message
254
+ # YAML syntax errors
255
+ when /Invalid YAML syntax/i
256
+ error_hash.merge!(
257
+ type: :syntax_error,
258
+ field: nil,
259
+ )
260
+
261
+ # Missing version field
262
+ when /Missing 'version' field/i
263
+ error_hash.merge!(
264
+ type: :missing_field,
265
+ field: "version",
266
+ )
267
+
268
+ # Invalid version
269
+ when /SwarmSDK requires version: (\d+)/i
270
+ error_hash.merge!(
271
+ type: :invalid_value,
272
+ field: "version",
273
+ )
274
+
275
+ # Missing swarm fields
276
+ when /Missing '(\w+)' field in swarm configuration/i
277
+ field_name = Regexp.last_match(1)
278
+ error_hash.merge!(
279
+ type: :missing_field,
280
+ field: "swarm.#{field_name}",
281
+ )
282
+
283
+ # Agent missing required field
284
+ when /Agent '([^']+)' missing required '([^']+)' field/i
285
+ agent_name = Regexp.last_match(1)
286
+ field_name = Regexp.last_match(2)
287
+ error_hash.merge!(
288
+ type: :missing_field,
289
+ field: "swarm.agents.#{agent_name}.#{field_name}",
290
+ agent: agent_name,
291
+ )
292
+
293
+ # Directory does not exist
294
+ when /Directory '([^']+)' for agent '([^']+)' does not exist/i
295
+ agent_name = Regexp.last_match(2)
296
+ error_hash.merge!(
297
+ type: :directory_not_found,
298
+ field: "swarm.agents.#{agent_name}.directory",
299
+ agent: agent_name,
300
+ )
301
+
302
+ # Error loading agent from file (must come before "Agent file not found")
303
+ when /Error loading agent '([^']+)' from file/i
304
+ agent_name = Regexp.last_match(1)
305
+ error_hash.merge!(
306
+ type: :file_load_error,
307
+ field: "swarm.agents.#{agent_name}.agent_file",
308
+ agent: agent_name,
309
+ )
310
+
311
+ # Agent file not found
312
+ when /Agent file not found: (.+)/i
313
+ # Try to extract agent name from the error context if available
314
+ error_hash.merge!(
315
+ type: :file_not_found,
316
+ field: nil, # We don't know which agent without more context
317
+ )
318
+
319
+ # Lead agent not found
320
+ when /Lead agent '([^']+)' not found in agents/i
321
+ error_hash.merge!(
322
+ type: :invalid_reference,
323
+ field: "swarm.lead",
324
+ )
325
+
326
+ # Unknown agent in connections
327
+ when /Agent '([^']+)' has connection to unknown agent '([^']+)'/i
328
+ agent_name = Regexp.last_match(1)
329
+ error_hash.merge!(
330
+ type: :invalid_reference,
331
+ field: "swarm.agents.#{agent_name}.delegates_to",
332
+ agent: agent_name,
333
+ )
334
+
335
+ # Circular dependency
336
+ when /Circular dependency detected/i
337
+ error_hash.merge!(
338
+ type: :circular_dependency,
339
+ field: nil,
340
+ )
341
+
342
+ # Configuration file not found
343
+ when /Configuration file not found/i
344
+ error_hash.merge!(
345
+ type: :file_not_found,
346
+ field: nil,
347
+ )
348
+
349
+ # Invalid hook event
350
+ when /Invalid hook event '([^']+)' for agent '([^']+)'/i
351
+ agent_name = Regexp.last_match(2)
352
+ error_hash.merge!(
353
+ type: :invalid_value,
354
+ field: "swarm.agents.#{agent_name}.hooks",
355
+ agent: agent_name,
356
+ )
357
+
358
+ # api_version validation error
359
+ when /Agent '([^']+)' has api_version set, but provider is/i
360
+ agent_name = Regexp.last_match(1)
361
+ error_hash.merge!(
362
+ type: :invalid_value,
363
+ field: "swarm.agents.#{agent_name}.api_version",
364
+ agent: agent_name,
365
+ )
366
+
367
+ # api_version invalid value
368
+ when /Agent '([^']+)' has invalid api_version/i
369
+ agent_name = Regexp.last_match(1)
370
+ error_hash.merge!(
371
+ type: :invalid_value,
372
+ field: "swarm.agents.#{agent_name}.api_version",
373
+ agent: agent_name,
374
+ )
375
+
376
+ # No agents defined
377
+ when /No agents defined/i
378
+ error_hash.merge!(
379
+ type: :missing_field,
380
+ field: "swarm.agents",
381
+ )
382
+
383
+ # Default: unknown error
384
+ else
385
+ error_hash.merge!(
386
+ type: :validation_error,
387
+ field: nil,
388
+ )
389
+ end
390
+
391
+ error_hash.compact
392
+ end
69
393
  end
70
394
 
71
395
  # Settings class for SwarmSDK global settings (not to be confused with Configuration for YAML loading)
@@ -136,22 +460,3 @@ RubyLLM.configure do |config|
136
460
  config.gpustack_api_base ||= ENV["GPUSTACK_API_BASE"]
137
461
  config.gpustack_api_key ||= ENV["GPUSTACK_API_KEY"]
138
462
  end
139
-
140
- # monkey patches
141
- # ruby_llm/mcp
142
- # - add `id` when sending "notifications/initialized" message: https://github.com/patvice/ruby_llm-mcp/issues/65
143
- # - remove `to_sym` on MCP parameter type: https://github.com/patvice/ruby_llm-mcp/issues/62#issuecomment-3421488406
144
- require "ruby_llm/mcp/notifications/initialize"
145
- require "ruby_llm/mcp/parameter"
146
-
147
- module RubyLLM
148
- module MCP
149
- module Notifications
150
- class Initialize
151
- def call
152
- @coordinator.request(notification_body, add_id: true, wait_for_response: false)
153
- end
154
- end
155
- end
156
- end
157
- end
data/swarm_sdk.gemspec CHANGED
@@ -36,6 +36,6 @@ Gem::Specification.new do |spec|
36
36
 
37
37
  spec.add_dependency("async", "~> 2.0")
38
38
  spec.add_dependency("ruby_llm", "~> 1.8")
39
- spec.add_dependency("ruby_llm-mcp", "~> 0.6.2")
39
+ spec.add_dependency("ruby_llm-mcp", "~> 0.6.3")
40
40
  spec.add_dependency("zeitwerk", "~> 2.6")
41
41
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: claude_swarm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paulo Arruda
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-10-28 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: thor
@@ -159,6 +159,7 @@ files:
159
159
  - docs/v2/CHANGELOG.swarm_memory.md
160
160
  - docs/v2/CHANGELOG.swarm_sdk.md
161
161
  - docs/v2/README.md
162
+ - docs/v2/guides/MEMORY_DEFRAG_GUIDE.md
162
163
  - docs/v2/guides/claude-code-agents.md
163
164
  - docs/v2/guides/complete-tutorial.md
164
165
  - docs/v2/guides/getting-started.md
@@ -167,8 +168,12 @@ files:
167
168
  - docs/v2/guides/quick-start-cli.md
168
169
  - docs/v2/guides/rails-integration.md
169
170
  - docs/v2/guides/swarm-memory.md
171
+ - docs/v2/reference/architecture-flow.md
170
172
  - docs/v2/reference/cli.md
173
+ - docs/v2/reference/event_payload_structures.md
174
+ - docs/v2/reference/execution-flow.md
171
175
  - docs/v2/reference/ruby-dsl.md
176
+ - docs/v2/reference/swarm_memory_technical_details.md
172
177
  - docs/v2/reference/yaml.md
173
178
  - examples/claude-swarm.yml
174
179
  - examples/learning-assistant/assistant.md
@@ -370,6 +375,7 @@ files:
370
375
  - lib/swarm_sdk/log_collector.rb
371
376
  - lib/swarm_sdk/log_stream.rb
372
377
  - lib/swarm_sdk/markdown_parser.rb
378
+ - lib/swarm_sdk/mcp.rb
373
379
  - lib/swarm_sdk/model_aliases.json
374
380
  - lib/swarm_sdk/models.json
375
381
  - lib/swarm_sdk/models.rb
@@ -456,7 +462,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
456
462
  - !ruby/object:Gem::Version
457
463
  version: '0'
458
464
  requirements: []
459
- rubygems_version: 3.6.2
465
+ rubygems_version: 3.6.9
460
466
  specification_version: 4
461
467
  summary: Orchestrate multiple Claude Code instances as a collaborative AI development
462
468
  team