swarm_memory 2.1.1 → 2.1.3

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 (79) hide show
  1. checksums.yaml +4 -4
  2. data/lib/claude_swarm/cli.rb +9 -11
  3. data/lib/claude_swarm/commands/ps.rb +1 -2
  4. data/lib/claude_swarm/configuration.rb +30 -7
  5. data/lib/claude_swarm/mcp_generator.rb +4 -10
  6. data/lib/claude_swarm/orchestrator.rb +43 -44
  7. data/lib/claude_swarm/system_utils.rb +4 -4
  8. data/lib/claude_swarm/version.rb +1 -1
  9. data/lib/claude_swarm.rb +5 -9
  10. data/lib/swarm_cli/commands/mcp_serve.rb +2 -2
  11. data/lib/swarm_cli/commands/mcp_tools.rb +3 -3
  12. data/lib/swarm_cli/config_loader.rb +14 -13
  13. data/lib/swarm_cli/version.rb +1 -1
  14. data/lib/swarm_cli.rb +2 -0
  15. data/lib/swarm_memory/adapters/base.rb +4 -4
  16. data/lib/swarm_memory/adapters/filesystem_adapter.rb +0 -12
  17. data/lib/swarm_memory/core/storage.rb +66 -6
  18. data/lib/swarm_memory/core/storage_read_tracker.rb +51 -14
  19. data/lib/swarm_memory/integration/cli_registration.rb +3 -2
  20. data/lib/swarm_memory/integration/sdk_plugin.rb +24 -4
  21. data/lib/swarm_memory/optimization/defragmenter.rb +4 -0
  22. data/lib/swarm_memory/tools/memory_edit.rb +3 -2
  23. data/lib/swarm_memory/tools/memory_glob.rb +24 -1
  24. data/lib/swarm_memory/tools/memory_multi_edit.rb +2 -2
  25. data/lib/swarm_memory/tools/memory_read.rb +3 -3
  26. data/lib/swarm_memory/tools/memory_write.rb +2 -2
  27. data/lib/swarm_memory/version.rb +1 -1
  28. data/lib/swarm_memory.rb +7 -0
  29. data/lib/swarm_sdk/agent/builder.rb +33 -0
  30. data/lib/swarm_sdk/agent/chat/context_tracker.rb +33 -0
  31. data/lib/swarm_sdk/agent/chat/hook_integration.rb +41 -0
  32. data/lib/swarm_sdk/agent/chat/system_reminder_injector.rb +11 -27
  33. data/lib/swarm_sdk/agent/chat.rb +199 -52
  34. data/lib/swarm_sdk/agent/context.rb +6 -2
  35. data/lib/swarm_sdk/agent/context_manager.rb +6 -0
  36. data/lib/swarm_sdk/agent/definition.rb +32 -23
  37. data/lib/swarm_sdk/agent/llm_instrumentation_middleware.rb +180 -0
  38. data/lib/swarm_sdk/configuration.rb +420 -103
  39. data/lib/swarm_sdk/events_to_messages.rb +181 -0
  40. data/lib/swarm_sdk/log_collector.rb +31 -5
  41. data/lib/swarm_sdk/log_stream.rb +37 -8
  42. data/lib/swarm_sdk/model_aliases.json +4 -1
  43. data/lib/swarm_sdk/node/agent_config.rb +39 -9
  44. data/lib/swarm_sdk/node/builder.rb +158 -42
  45. data/lib/swarm_sdk/node_context.rb +75 -0
  46. data/lib/swarm_sdk/node_orchestrator.rb +492 -18
  47. data/lib/swarm_sdk/plugin.rb +73 -1
  48. data/lib/swarm_sdk/proc_helpers.rb +53 -0
  49. data/lib/swarm_sdk/prompts/base_system_prompt.md.erb +0 -126
  50. data/lib/swarm_sdk/providers/openai_with_responses.rb +22 -15
  51. data/lib/swarm_sdk/restore_result.rb +65 -0
  52. data/lib/swarm_sdk/result.rb +32 -6
  53. data/lib/swarm_sdk/snapshot.rb +156 -0
  54. data/lib/swarm_sdk/snapshot_from_events.rb +386 -0
  55. data/lib/swarm_sdk/state_restorer.rb +491 -0
  56. data/lib/swarm_sdk/state_snapshot.rb +369 -0
  57. data/lib/swarm_sdk/swarm/agent_initializer.rb +360 -55
  58. data/lib/swarm_sdk/swarm/all_agents_builder.rb +28 -1
  59. data/lib/swarm_sdk/swarm/builder.rb +208 -11
  60. data/lib/swarm_sdk/swarm/swarm_registry_builder.rb +67 -0
  61. data/lib/swarm_sdk/swarm/tool_configurator.rb +46 -11
  62. data/lib/swarm_sdk/swarm.rb +367 -90
  63. data/lib/swarm_sdk/swarm_loader.rb +145 -0
  64. data/lib/swarm_sdk/swarm_registry.rb +136 -0
  65. data/lib/swarm_sdk/tools/delegate.rb +94 -9
  66. data/lib/swarm_sdk/tools/read.rb +17 -5
  67. data/lib/swarm_sdk/tools/scratchpad/scratchpad_list.rb +23 -2
  68. data/lib/swarm_sdk/tools/scratchpad/scratchpad_read.rb +23 -2
  69. data/lib/swarm_sdk/tools/scratchpad/scratchpad_write.rb +21 -4
  70. data/lib/swarm_sdk/tools/stores/read_tracker.rb +47 -12
  71. data/lib/swarm_sdk/tools/stores/scratchpad_storage.rb +45 -0
  72. data/lib/swarm_sdk/tools/stores/storage.rb +4 -4
  73. data/lib/swarm_sdk/tools/think.rb +4 -1
  74. data/lib/swarm_sdk/tools/todo_write.rb +20 -8
  75. data/lib/swarm_sdk/utils.rb +18 -0
  76. data/lib/swarm_sdk/validation_result.rb +33 -0
  77. data/lib/swarm_sdk/version.rb +1 -1
  78. data/lib/swarm_sdk.rb +365 -28
  79. metadata +17 -5
@@ -7,7 +7,32 @@ module SwarmSDK
7
7
  # Plugins are self-registering - they call SwarmSDK::PluginRegistry.register
8
8
  # when the gem is loaded.
9
9
  #
10
- # @example Implementing a plugin
10
+ # ## Adding Custom Attributes to Agents
11
+ #
12
+ # Plugins can add custom attributes to Agent::Definition that are preserved
13
+ # when agents are cloned (e.g., in NodeOrchestrator). To do this:
14
+ #
15
+ # 1. Add attr_reader to Agent::Definition for your attribute
16
+ # 2. Parse the attribute in Agent::Definition#initialize
17
+ # 3. Implement serialize_config to preserve it during serialization
18
+ #
19
+ # @example Plugin with custom agent attributes
20
+ # # 1. Extend Agent::Definition (in your plugin gem)
21
+ # module SwarmSDK
22
+ # module Agent
23
+ # class Definition
24
+ # attr_reader :my_custom_config
25
+ #
26
+ # alias_method :original_initialize, :initialize
27
+ # def initialize(name, config = {})
28
+ # @my_custom_config = config[:my_custom_config]
29
+ # original_initialize(name, config)
30
+ # end
31
+ # end
32
+ # end
33
+ # end
34
+ #
35
+ # # 2. Implement plugin with serialize_config
11
36
  # class MyPlugin < SwarmSDK::Plugin
12
37
  # def name
13
38
  # :my_plugin
@@ -20,9 +45,34 @@ module SwarmSDK
20
45
  # def create_tool(tool_name, context)
21
46
  # # Create and return tool instance
22
47
  # end
48
+ #
49
+ # # Preserve custom config when agents are cloned
50
+ # def serialize_config(agent_definition:)
51
+ # return {} unless agent_definition.my_custom_config
52
+ #
53
+ # { my_custom_config: agent_definition.my_custom_config }
54
+ # end
23
55
  # end
24
56
  #
25
57
  # SwarmSDK::PluginRegistry.register(MyPlugin.new)
58
+ #
59
+ # Now agents can use your custom config:
60
+ #
61
+ # agent :researcher do
62
+ # my_custom_config { option: "value" }
63
+ # end
64
+ #
65
+ # And it will be preserved when NodeOrchestrator clones the agent!
66
+ #
67
+ # @example Real-world: SwarmMemory plugin
68
+ # # SwarmMemory adds 'memory' attribute to agents
69
+ # class SDKPlugin < SwarmSDK::Plugin
70
+ # def serialize_config(agent_definition:)
71
+ # return {} unless agent_definition.memory
72
+ # { memory: agent_definition.memory }
73
+ # end
74
+ # end
75
+ #
26
76
  class Plugin
27
77
  # Plugin name (must be unique)
28
78
  #
@@ -143,5 +193,27 @@ module SwarmSDK
143
193
  def on_user_message(agent_name:, prompt:, is_first_message:)
144
194
  []
145
195
  end
196
+
197
+ # Contribute to agent serialization (optional)
198
+ #
199
+ # Called when Agent::Definition.to_h is invoked (e.g., for cloning agents
200
+ # in NodeOrchestrator). Plugins can return config keys that should be
201
+ # included in the serialized hash to preserve their state.
202
+ #
203
+ # This allows plugins to maintain their configuration when agents are
204
+ # cloned or serialized, without SwarmSDK needing to know about plugin-specific fields.
205
+ #
206
+ # @param agent_definition [Agent::Definition] Agent definition
207
+ # @return [Hash] Config keys to include in to_h (e.g., { memory: config })
208
+ #
209
+ # @example Memory plugin serialization
210
+ # def serialize_config(agent_definition:)
211
+ # return {} unless agent_definition.memory
212
+ #
213
+ # { memory: agent_definition.memory }
214
+ # end
215
+ def serialize_config(agent_definition:)
216
+ {}
217
+ end
146
218
  end
147
219
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SwarmSDK
4
+ # Helper methods for working with Procs and Lambdas
5
+ #
6
+ # Provides functionality to convert regular Proc objects into Lambdas to enable
7
+ # safe use of return statements in DSL blocks (like input/output transformers).
8
+ module ProcHelpers
9
+ class << self
10
+ # Convert a Proc to a Lambda
11
+ #
12
+ # The fundamental difference between a Proc and a Lambda is in how they handle
13
+ # return statements. In a Proc, return exits the enclosing method (or program),
14
+ # while in a Lambda, return only exits the lambda itself.
15
+ #
16
+ # This method converts a Proc to a Lambda by:
17
+ # 1. Converting the proc to an unbound method via define_method
18
+ # 2. Wrapping it in a lambda that binds and calls the method
19
+ # 3. In the method, return exits the method (not the original scope)
20
+ #
21
+ # This allows users to write natural control flow with return statements:
22
+ #
23
+ # @example
24
+ # my_proc = proc { |x| return x * 2 if x > 0; 0 }
25
+ # my_lambda = ProcHelpers.to_lambda(my_proc)
26
+ # my_lambda.call(5) # => 10 (return works safely!)
27
+ #
28
+ # @param proc [Proc] The proc to convert
29
+ # @return [Proc] A lambda with the same behavior but safe return semantics
30
+ def to_lambda(proc)
31
+ return proc if proc.lambda?
32
+
33
+ # Save local reference to proc so we can use it in module_exec/lambda scopes
34
+ source_proc = proc
35
+
36
+ # Convert proc to unbound method
37
+ # define_method with a block converts the block to a method, where return
38
+ # exits the method (not the original scope)
39
+ unbound_method = Module.new.module_exec do
40
+ instance_method(define_method(:_proc_call, &source_proc))
41
+ end
42
+
43
+ # Return lambda which binds our unbound method to correct receiver and calls it
44
+ lambda do |*args, **kwargs, &block|
45
+ # Bind method to the original proc's receiver (the context where it was defined)
46
+ # This preserves access to instance variables, local variables via closure, etc.
47
+ receiver = source_proc.binding.eval("self")
48
+ unbound_method.bind(receiver).call(*args, **kwargs, &block)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -69,139 +69,15 @@ When making changes to files, first understand the file's conventions. Mimic exi
69
69
  - When you edit something, first look at the surrounding context (especially imports/requires) to understand the choice of frameworks and libraries. Then consider how to make the given change in a way that is most consistent with existing patterns.
70
70
  - Always follow security best practices. Never introduce code that exposes or logs secrets and keys. Never commit secrets or keys to repositories.
71
71
 
72
- # Task Management
73
-
74
- You have access to the TodoWrite tool to help you manage and plan tasks. Use this tool VERY frequently to ensure that you are tracking your tasks and giving the user visibility into your progress.
75
- This tool is also EXTREMELY helpful for planning tasks, and for breaking down larger complex tasks into smaller steps. If you do not use this tool when planning, you may forget to do important tasks - and that is unacceptable.
76
-
77
- **CRITICAL WORKFLOW**: When starting a multi-step task:
78
- 1. **FIRST**: Analyze what needs to be done (search, read files, understand scope)
79
- 2. **SECOND**: Create a COMPLETE todo list with ALL known tasks before starting work
80
- 3. **THIRD**: Begin executing tasks, marking them in_progress → completed as you work
81
- 4. **ONLY add new todos** if you discover unexpected work during implementation
82
-
83
- **CRITICAL RULES FOR TODO COMPLETION**:
84
- - Mark EACH task as completed IMMEDIATELY after finishing it (do not batch updates)
85
- - You MUST complete ALL pending todos before giving your final answer to the user
86
- - If a task becomes irrelevant, remove it from the list or mark it completed with a note
87
- - NEVER leave in_progress or pending tasks when you finish responding to the user
88
- - Before giving your final response, verify all todos are marked completed
89
-
90
- Examples:
91
-
92
- <example>
93
- user: Run the build and fix any type errors
94
- assistant: I'll run the build first to identify all type errors, then create a complete todo list.
95
-
96
- [Runs build and finds 3 type errors in 3 different files]
97
-
98
- Now I'll create a complete todo list with all the work:
99
-
100
- [Uses TodoWrite to create full list:]
101
- 1. Fix type error in auth.ts:45 (in_progress)
102
- 2. Fix type error in user.ts:23 (pending)
103
- 3. Fix type error in api.ts:67 (pending)
104
- 4. Run build again to verify all fixes (pending)
105
-
106
- Starting with the first error in auth.ts...
107
- [Fixes auth.ts error]
108
-
109
- [Updates TodoWrite - marks task 1 completed, task 2 in_progress]
110
-
111
- Now fixing user.ts...
112
- [Fixes user.ts error]
113
-
114
- [Updates TodoWrite - marks task 2 completed, task 3 in_progress]
115
-
116
- Now fixing api.ts...
117
- [Fixes api.ts error]
118
-
119
- [Updates TodoWrite - marks task 3 completed, task 4 in_progress]
120
-
121
- Running build to verify...
122
- [Runs build - all errors fixed!]
123
-
124
- [Updates TodoWrite - marks task 4 completed]
125
-
126
- All type errors have been fixed and the build passes successfully.
127
- </example>
128
- In this example, the assistant: (1) analyzed scope first, (2) created COMPLETE todo list, (3) marked EACH task completed immediately after finishing it, (4) completed ALL tasks before giving final response.
129
-
130
- <example>
131
- user: Help me write a new feature that allows users to track their usage metrics and export them to various formats
132
-
133
- A: I'll help you implement a usage metrics tracking and export feature. Let me first research the codebase to understand the scope, then create a complete todo list.
134
-
135
- [Searches for existing metrics/telemetry code, examines architecture, identifies integration points]
136
-
137
- Based on my analysis, here's the complete implementation plan:
138
-
139
- [Uses TodoWrite to create full list:]
140
- 1. Create MetricsCollector class in src/metrics/ (in_progress)
141
- 2. Add database schema for metrics storage (pending)
142
- 3. Implement CSV export format (pending)
143
- 4. Implement JSON export format (pending)
144
- 5. Create MetricsService API endpoints (pending)
145
- 6. Add frontend UI for export (pending)
146
- 7. Write unit tests for collectors and exporters (pending)
147
- 8. Update documentation (pending)
148
-
149
- Now starting with the MetricsCollector class...
150
- [Implements MetricsCollector]
151
-
152
- [Updates TodoWrite - marks task 1 completed, task 2 in_progress]
153
-
154
- Adding database schema...
155
- [Creates schema]
156
-
157
- [Updates TodoWrite - marks task 2 completed, task 3 in_progress]
158
-
159
- Implementing CSV export...
160
- [Implements CSV exporter]
161
-
162
- [Updates TodoWrite - marks task 3 completed, task 4 in_progress]
163
-
164
- Implementing JSON export...
165
- [Implements JSON exporter]
166
-
167
- [Updates TodoWrite - marks task 4 completed, task 5 in_progress]
168
-
169
- Creating MetricsService API endpoints...
170
- [Implements API endpoints]
171
-
172
- [Updates TodoWrite - marks task 5 completed, task 6 in_progress]
173
-
174
- Adding frontend UI for export...
175
- [Implements UI components]
176
-
177
- [Updates TodoWrite - marks task 6 completed, task 7 in_progress]
178
-
179
- Writing unit tests...
180
- [Writes and runs tests - all pass]
181
-
182
- [Updates TodoWrite - marks task 7 completed, task 8 in_progress]
183
-
184
- Updating documentation...
185
- [Updates docs with usage examples]
186
-
187
- [Updates TodoWrite - marks task 8 completed]
188
-
189
- The metrics tracking and export feature is now complete. Users can collect metrics and export them to CSV or JSON formats through both the API and the frontend UI.
190
- </example>
191
-
192
72
  # Doing tasks
193
73
 
194
74
  The user will primarily request you perform tasks. This includes solving problems, adding new functionality, refactoring, explaining content, and more. For these tasks the following steps are recommended:
195
75
 
196
- - Use the TodoWrite tool to plan the task if required
197
76
  - Use the available search tools to understand the context and the user's query. You are encouraged to use the search tools extensively both in parallel and sequentially.
198
77
  - Implement the solution using all tools available to you
199
- - Mark each todo completed IMMEDIATELY after finishing it
200
78
  - Verify the solution if possible with tests. NEVER assume specific test framework or test script. Check the project documentation or search to determine the testing approach.
201
79
  - When you have completed a task, if there are linting or validation commands available to you, run them to ensure your work is correct. NEVER assume what these commands are - check the project documentation first.
202
80
  NEVER commit changes unless the user explicitly asks you to. It is VERY IMPORTANT to only commit when explicitly asked, otherwise the user will feel that you are being too proactive.
203
- - Before giving your final response: Ensure ALL todos are marked completed. NEVER leave pending or in_progress tasks.
204
- - IMPORTANT: Always use the TodoWrite tool to plan and track tasks throughout the conversation.
205
81
 
206
82
  # Tool usage policy
207
83
 
@@ -211,8 +87,6 @@ NEVER commit changes unless the user explicitly asks you to. It is VERY IMPORTAN
211
87
  - If the user specifies that they want you to run tools "in parallel", you MUST send a single message with multiple tool use content blocks. For example, if you need to delegate a task to multiple agents in parallel, send a single message with multiple DelegateTask tool calls.
212
88
  - Use specialized tools instead of bash commands when possible, as this provides a better user experience. For file operations, use dedicated tools: Read for reading files instead of cat/head/tail, Edit/MultiEdit for editing instead of sed/awk, and Write for creating files instead of cat with heredoc or echo redirection. Reserve bash tools exclusively for actual system commands and terminal operations that require shell execution. NEVER use bash echo or other command-line tools to communicate thoughts, explanations, or instructions to the user. Output all communication directly in your response text instead.
213
89
 
214
- IMPORTANT: Always use the TodoWrite tool to plan and track tasks throughout the conversation.
215
-
216
90
  You MUST answer concisely with fewer than 4 lines of text (not including tool use or code generation), unless user asks for detail.
217
91
 
218
92
 
@@ -145,7 +145,7 @@ module SwarmSDK
145
145
  rescue NoMethodError => e
146
146
  # Catch fetch/dig errors on nil and provide better context
147
147
  if e.message.include?("undefined method") && (e.message.include?("fetch") || e.message.include?("dig"))
148
- log_parse_error(e.class.name, e.message, response.body)
148
+ log_parse_error(e.class.name, e.message, response.body, e.backtrace)
149
149
  nil
150
150
  else
151
151
  raise
@@ -377,30 +377,36 @@ module SwarmSDK
377
377
  # This differs from chat/completions which nests under 'function':
378
378
  # { type: "function", function: { name: "tool_name", ... } }
379
379
  #
380
+ # RubyLLM 1.9.0+: Uses tool.params_schema for unified schema generation.
381
+ # This supports both old param helper and new params DSL, and includes
382
+ # proper JSON Schema formatting (strict, additionalProperties, etc.)
383
+ #
380
384
  # @param tool [RubyLLM::Tool] Tool to convert
381
385
  # @return [Hash] Tool definition in Responses API format
382
386
  def responses_tool_for(tool)
387
+ # Use tool.params_schema which returns a complete JSON Schema hash
388
+ # This works with both param helper and params DSL
389
+ parameters_schema = tool.params_schema || empty_parameters_schema
390
+
383
391
  {
384
392
  type: "function",
385
393
  name: tool.name,
386
394
  description: tool.description,
387
- parameters: {
388
- type: "object",
389
- properties: tool.parameters.transform_values { |param| param_schema(param) },
390
- required: tool.parameters.select { |_, p| p.required }.keys,
391
- },
395
+ parameters: parameters_schema,
392
396
  }
393
397
  end
394
398
 
395
- # Build parameter schema for a tool parameter
399
+ # Empty parameter schema for tools with no parameters
396
400
  #
397
- # @param param [RubyLLM::Tool::Parameter] Parameter to convert
398
- # @return [Hash] Parameter schema
399
- def param_schema(param)
401
+ # @return [Hash] Empty JSON Schema matching OpenAI's format
402
+ def empty_parameters_schema
400
403
  {
401
- type: param.type,
402
- description: param.description,
403
- }.compact
404
+ "type" => "object",
405
+ "properties" => {},
406
+ "required" => [],
407
+ "additionalProperties" => false,
408
+ "strict" => true,
409
+ }
404
410
  end
405
411
 
406
412
  # Parse Responses API response
@@ -562,7 +568,7 @@ module SwarmSDK
562
568
  # @param error_class [String] Error class name
563
569
  # @param error_message [String] Error message
564
570
  # @param response_body [Object] Response body that failed to parse
565
- def log_parse_error(error_class, error_message, response_body)
571
+ def log_parse_error(error_class, error_message, response_body, error_backtrace = nil)
566
572
  if @agent_name
567
573
  # Emit structured JSON log through LogStream
568
574
  LogStream.emit(
@@ -570,11 +576,12 @@ module SwarmSDK
570
576
  agent: @agent_name,
571
577
  error_class: error_class,
572
578
  error_message: error_message,
579
+ error_backtrace: error_backtrace,
573
580
  response_body: response_body.inspect,
574
581
  )
575
582
  else
576
583
  # Fallback to RubyLLM logger if agent name not set
577
- RubyLLM.logger.error("SwarmSDK: #{error_class}: #{error_message}\nResponse: #{response_body.inspect}")
584
+ RubyLLM.logger.error("SwarmSDK: #{error_class}: #{error_message}\nResponse: #{response_body.inspect}\nError backtrace: #{error_backtrace.join("\n")}")
578
585
  end
579
586
  end
580
587
  end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SwarmSDK
4
+ # Result object returned from snapshot restore operations
5
+ #
6
+ # Provides information about the restore process, including any warnings
7
+ # about agents or delegations that couldn't be restored due to configuration
8
+ # mismatches.
9
+ #
10
+ # @example Successful restore
11
+ # result = swarm.restore(snapshot_data)
12
+ # if result.success?
13
+ # puts "All agents restored successfully"
14
+ # end
15
+ #
16
+ # @example Partial restore with warnings
17
+ # result = swarm.restore(snapshot_data)
18
+ # if result.partial_restore?
19
+ # puts result.summary
20
+ # result.warnings.each do |warning|
21
+ # puts " - #{warning[:message]}"
22
+ # end
23
+ # end
24
+ class RestoreResult
25
+ attr_reader :warnings, :skipped_agents, :skipped_delegations
26
+
27
+ # Initialize restore result
28
+ #
29
+ # @param warnings [Array<Hash>] Warning messages with details
30
+ # @param skipped_agents [Array<Symbol>] Names of agents that couldn't be restored
31
+ # @param skipped_delegations [Array<String>] Names of delegation instances that couldn't be restored
32
+ def initialize(warnings:, skipped_agents:, skipped_delegations:)
33
+ @warnings = warnings
34
+ @skipped_agents = skipped_agents
35
+ @skipped_delegations = skipped_delegations
36
+ end
37
+
38
+ # Check if restore was completely successful
39
+ #
40
+ # @return [Boolean] true if all agents restored without warnings
41
+ def success?
42
+ warnings.empty?
43
+ end
44
+
45
+ # Check if restore was partial (some agents skipped)
46
+ #
47
+ # @return [Boolean] true if some agents were skipped
48
+ def partial_restore?
49
+ !warnings.empty?
50
+ end
51
+
52
+ # Get human-readable summary of restore result
53
+ #
54
+ # @return [String] Summary message
55
+ def summary
56
+ if success?
57
+ "Snapshot restored successfully. All agents restored."
58
+ else
59
+ "Snapshot restored with warnings. " \
60
+ "#{skipped_agents.size} agents skipped, " \
61
+ "#{skipped_delegations.size} delegation instances skipped."
62
+ end
63
+ end
64
+ end
65
+ end
@@ -2,17 +2,17 @@
2
2
 
3
3
  module SwarmSDK
4
4
  class Result
5
- attr_reader :content, :agent, :cost, :tokens, :duration, :logs, :error, :metadata
5
+ attr_reader :content, :agent, :duration, :logs, :error, :metadata
6
6
 
7
- def initialize(content: nil, agent:, cost: 0.0, tokens: {}, duration: 0.0, logs: [], error: nil, metadata: {})
7
+ def initialize(content: nil, agent:, cost: nil, tokens: nil, duration: 0.0, logs: [], error: nil, metadata: {})
8
8
  @content = content
9
9
  @agent = agent
10
- @cost = cost
11
- @tokens = tokens
12
10
  @duration = duration
13
11
  @logs = logs
14
12
  @error = error
15
13
  @metadata = metadata
14
+ # Legacy parameters kept for backward compatibility but not stored
15
+ # Use total_cost and tokens methods instead which calculate from logs
16
16
  end
17
17
 
18
18
  def success?
@@ -23,12 +23,38 @@ module SwarmSDK
23
23
  !success?
24
24
  end
25
25
 
26
+ # Calculate total cost from logs
27
+ #
28
+ # Delegates to total_cost for consistency. This attribute is calculated
29
+ # dynamically rather than stored.
30
+ #
31
+ # @return [Float] Total cost in dollars
32
+ def cost
33
+ total_cost
34
+ end
35
+
36
+ # Get token breakdown from logs
37
+ #
38
+ # Returns input and output tokens from the last log entry with usage data.
39
+ # This attribute is calculated dynamically rather than stored.
40
+ #
41
+ # @return [Hash] Token breakdown with :input and :output keys, or empty hash if no usage data
42
+ def tokens
43
+ last_entry = @logs.reverse.find { |entry| entry.dig(:usage, :cumulative_input_tokens) }
44
+ return {} unless last_entry
45
+
46
+ {
47
+ input: last_entry.dig(:usage, :cumulative_input_tokens) || 0,
48
+ output: last_entry.dig(:usage, :cumulative_output_tokens) || 0,
49
+ }
50
+ end
51
+
26
52
  def to_h
27
53
  {
28
54
  content: @content,
29
55
  agent: @agent,
30
- cost: @cost,
31
- tokens: @tokens,
56
+ cost: cost,
57
+ tokens: tokens,
32
58
  duration: @duration,
33
59
  success: success?,
34
60
  error: @error&.message,
@@ -0,0 +1,156 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SwarmSDK
4
+ # Snapshot of swarm conversation state
5
+ #
6
+ # Encapsulates snapshot data with methods for serialization and deserialization.
7
+ # Provides a clean API for saving/loading snapshots in various formats.
8
+ #
9
+ # @example Create and save snapshot
10
+ # snapshot = swarm.snapshot
11
+ # snapshot.write_to_file("session.json")
12
+ #
13
+ # @example Load and restore snapshot
14
+ # snapshot = SwarmSDK::Snapshot.from_file("session.json")
15
+ # result = swarm.restore(snapshot)
16
+ #
17
+ # @example Convert to/from hash
18
+ # hash = snapshot.to_hash
19
+ # snapshot = SwarmSDK::Snapshot.from_hash(hash)
20
+ class Snapshot
21
+ attr_reader :data
22
+
23
+ # Initialize snapshot with data
24
+ #
25
+ # @param data [Hash] Snapshot data hash
26
+ def initialize(data)
27
+ @data = data
28
+ end
29
+
30
+ # Convert snapshot to hash
31
+ #
32
+ # @return [Hash] Snapshot data as hash
33
+ def to_hash
34
+ @data
35
+ end
36
+
37
+ # Convert snapshot to JSON string
38
+ #
39
+ # @param pretty [Boolean] Whether to pretty-print JSON (default: true)
40
+ # @return [String] JSON string
41
+ def to_json(pretty: true)
42
+ if pretty
43
+ JSON.pretty_generate(@data)
44
+ else
45
+ JSON.generate(@data)
46
+ end
47
+ end
48
+
49
+ # Write snapshot to file as JSON
50
+ #
51
+ # Uses atomic write pattern (write to temp file, then rename) to prevent
52
+ # corruption if process crashes during write.
53
+ #
54
+ # @param path [String] File path to write to
55
+ # @param pretty [Boolean] Whether to pretty-print JSON (default: true)
56
+ # @return [void]
57
+ def write_to_file(path, pretty: true)
58
+ # Atomic write: write to temp file, then rename
59
+ # This ensures the snapshot file is never corrupted even if process crashes
60
+ temp_path = "#{path}.tmp.#{Process.pid}.#{Time.now.to_i}.#{SecureRandom.hex(4)}"
61
+
62
+ File.write(temp_path, to_json(pretty: pretty))
63
+ File.rename(temp_path, path)
64
+ rescue
65
+ # Clean up temp file if it exists
66
+ File.delete(temp_path) if File.exist?(temp_path)
67
+ raise
68
+ end
69
+
70
+ class << self
71
+ # Create snapshot from hash
72
+ #
73
+ # @param hash [Hash] Snapshot data hash
74
+ # @return [Snapshot] New snapshot instance
75
+ def from_hash(hash)
76
+ new(hash)
77
+ end
78
+
79
+ # Create snapshot from JSON string
80
+ #
81
+ # @param json_string [String] JSON string
82
+ # @return [Snapshot] New snapshot instance
83
+ def from_json(json_string)
84
+ hash = JSON.parse(json_string, symbolize_names: true)
85
+ new(hash)
86
+ end
87
+
88
+ # Create snapshot from JSON file
89
+ #
90
+ # @param path [String] File path to read from
91
+ # @return [Snapshot] New snapshot instance
92
+ def from_file(path)
93
+ json_string = File.read(path)
94
+ from_json(json_string)
95
+ end
96
+ end
97
+
98
+ # Get snapshot version
99
+ #
100
+ # @return [String] Snapshot version
101
+ def version
102
+ @data[:version] || @data["version"]
103
+ end
104
+
105
+ # Get snapshot type (swarm or node_orchestrator)
106
+ #
107
+ # @return [String] Snapshot type
108
+ def type
109
+ @data[:type] || @data["type"]
110
+ end
111
+
112
+ # Get timestamp when snapshot was created
113
+ #
114
+ # @return [String] ISO8601 timestamp
115
+ def snapshot_at
116
+ @data[:snapshot_at] || @data["snapshot_at"]
117
+ end
118
+
119
+ # Get SwarmSDK version that created this snapshot
120
+ #
121
+ # @return [String] SwarmSDK version
122
+ def swarm_sdk_version
123
+ @data[:swarm_sdk_version] || @data["swarm_sdk_version"]
124
+ end
125
+
126
+ # Get agent names from snapshot
127
+ #
128
+ # @return [Array<String>] Agent names
129
+ def agent_names
130
+ agents = @data[:agents] || @data["agents"]
131
+ agents ? agents.keys.map(&:to_s) : []
132
+ end
133
+
134
+ # Get delegation instance names from snapshot
135
+ #
136
+ # @return [Array<String>] Delegation instance names
137
+ def delegation_instance_names
138
+ delegations = @data[:delegation_instances] || @data["delegation_instances"]
139
+ delegations ? delegations.keys.map(&:to_s) : []
140
+ end
141
+
142
+ # Check if snapshot is for a swarm (vs node_orchestrator)
143
+ #
144
+ # @return [Boolean] true if swarm snapshot
145
+ def swarm?
146
+ type == "swarm"
147
+ end
148
+
149
+ # Check if snapshot is for a node orchestrator
150
+ #
151
+ # @return [Boolean] true if node orchestrator snapshot
152
+ def node_orchestrator?
153
+ type == "node_orchestrator"
154
+ end
155
+ end
156
+ end