swarm_memory 2.1.2 → 2.1.4
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.
- checksums.yaml +4 -4
- data/lib/claude_swarm/claude_mcp_server.rb +1 -0
- data/lib/claude_swarm/cli.rb +5 -18
- data/lib/claude_swarm/configuration.rb +30 -19
- data/lib/claude_swarm/mcp_generator.rb +5 -10
- data/lib/claude_swarm/openai/chat_completion.rb +4 -12
- data/lib/claude_swarm/openai/executor.rb +3 -1
- data/lib/claude_swarm/openai/responses.rb +13 -32
- data/lib/claude_swarm/version.rb +1 -1
- data/lib/swarm_cli/commands/mcp_serve.rb +2 -2
- data/lib/swarm_cli/commands/run.rb +2 -2
- data/lib/swarm_cli/config_loader.rb +14 -14
- data/lib/swarm_cli/formatters/human_formatter.rb +70 -0
- data/lib/swarm_cli/interactive_repl.rb +11 -5
- data/lib/swarm_cli/ui/icons.rb +0 -23
- data/lib/swarm_cli/version.rb +1 -1
- data/lib/swarm_memory/adapters/base.rb +4 -4
- data/lib/swarm_memory/adapters/filesystem_adapter.rb +11 -34
- data/lib/swarm_memory/core/storage_read_tracker.rb +51 -14
- data/lib/swarm_memory/integration/cli_registration.rb +3 -2
- data/lib/swarm_memory/integration/sdk_plugin.rb +98 -12
- data/lib/swarm_memory/tools/memory_edit.rb +2 -2
- data/lib/swarm_memory/tools/memory_multi_edit.rb +2 -2
- data/lib/swarm_memory/tools/memory_read.rb +3 -3
- data/lib/swarm_memory/version.rb +1 -1
- data/lib/swarm_memory.rb +6 -1
- data/lib/swarm_sdk/agent/builder.rb +91 -0
- data/lib/swarm_sdk/agent/chat.rb +540 -925
- data/lib/swarm_sdk/agent/{chat → chat_helpers}/context_tracker.rb +33 -79
- data/lib/swarm_sdk/agent/chat_helpers/event_emitter.rb +204 -0
- data/lib/swarm_sdk/agent/{chat → chat_helpers}/hook_integration.rb +147 -39
- data/lib/swarm_sdk/agent/chat_helpers/instrumentation.rb +78 -0
- data/lib/swarm_sdk/agent/chat_helpers/llm_configuration.rb +233 -0
- data/lib/swarm_sdk/agent/{chat → chat_helpers}/logging_helpers.rb +1 -1
- data/lib/swarm_sdk/agent/chat_helpers/serialization.rb +83 -0
- data/lib/swarm_sdk/agent/{chat → chat_helpers}/system_reminder_injector.rb +22 -38
- data/lib/swarm_sdk/agent/chat_helpers/system_reminders.rb +79 -0
- data/lib/swarm_sdk/agent/chat_helpers/token_tracking.rb +98 -0
- data/lib/swarm_sdk/agent/context.rb +8 -4
- data/lib/swarm_sdk/agent/context_manager.rb +6 -0
- data/lib/swarm_sdk/agent/definition.rb +79 -174
- data/lib/swarm_sdk/agent/llm_instrumentation_middleware.rb +182 -0
- data/lib/swarm_sdk/agent/system_prompt_builder.rb +161 -0
- data/lib/swarm_sdk/builders/base_builder.rb +409 -0
- data/lib/swarm_sdk/concerns/cleanupable.rb +39 -0
- data/lib/swarm_sdk/concerns/snapshotable.rb +67 -0
- data/lib/swarm_sdk/concerns/validatable.rb +55 -0
- data/lib/swarm_sdk/configuration/parser.rb +353 -0
- data/lib/swarm_sdk/configuration/translator.rb +255 -0
- data/lib/swarm_sdk/configuration.rb +100 -261
- data/lib/swarm_sdk/context_compactor/token_counter.rb +3 -3
- data/lib/swarm_sdk/context_compactor.rb +6 -11
- data/lib/swarm_sdk/context_management/builder.rb +128 -0
- data/lib/swarm_sdk/context_management/context.rb +328 -0
- data/lib/swarm_sdk/defaults.rb +196 -0
- data/lib/swarm_sdk/events_to_messages.rb +199 -0
- data/lib/swarm_sdk/hooks/shell_executor.rb +2 -1
- data/lib/swarm_sdk/log_collector.rb +192 -16
- data/lib/swarm_sdk/log_stream.rb +66 -8
- data/lib/swarm_sdk/model_aliases.json +4 -1
- data/lib/swarm_sdk/node_context.rb +1 -1
- data/lib/swarm_sdk/observer/builder.rb +81 -0
- data/lib/swarm_sdk/observer/config.rb +45 -0
- data/lib/swarm_sdk/observer/manager.rb +236 -0
- data/lib/swarm_sdk/patterns/agent_observer.rb +160 -0
- data/lib/swarm_sdk/plugin.rb +93 -3
- data/lib/swarm_sdk/proc_helpers.rb +53 -0
- data/lib/swarm_sdk/prompts/base_system_prompt.md.erb +0 -126
- data/lib/swarm_sdk/restore_result.rb +65 -0
- data/lib/swarm_sdk/snapshot.rb +156 -0
- data/lib/swarm_sdk/snapshot_from_events.rb +397 -0
- data/lib/swarm_sdk/state_restorer.rb +476 -0
- data/lib/swarm_sdk/state_snapshot.rb +334 -0
- data/lib/swarm_sdk/swarm/agent_initializer.rb +428 -79
- data/lib/swarm_sdk/swarm/all_agents_builder.rb +28 -1
- data/lib/swarm_sdk/swarm/builder.rb +69 -407
- data/lib/swarm_sdk/swarm/executor.rb +213 -0
- data/lib/swarm_sdk/swarm/hook_triggers.rb +150 -0
- data/lib/swarm_sdk/swarm/logging_callbacks.rb +340 -0
- data/lib/swarm_sdk/swarm/mcp_configurator.rb +7 -4
- data/lib/swarm_sdk/swarm/swarm_registry_builder.rb +67 -0
- data/lib/swarm_sdk/swarm/tool_configurator.rb +88 -149
- data/lib/swarm_sdk/swarm.rb +366 -631
- data/lib/swarm_sdk/swarm_loader.rb +145 -0
- data/lib/swarm_sdk/swarm_registry.rb +136 -0
- data/lib/swarm_sdk/tools/bash.rb +11 -3
- data/lib/swarm_sdk/tools/delegate.rb +127 -24
- data/lib/swarm_sdk/tools/edit.rb +8 -13
- data/lib/swarm_sdk/tools/glob.rb +9 -1
- data/lib/swarm_sdk/tools/grep.rb +7 -0
- data/lib/swarm_sdk/tools/multi_edit.rb +15 -11
- data/lib/swarm_sdk/tools/path_resolver.rb +51 -2
- data/lib/swarm_sdk/tools/read.rb +28 -18
- data/lib/swarm_sdk/tools/registry.rb +122 -10
- data/lib/swarm_sdk/tools/scratchpad/scratchpad_list.rb +23 -2
- data/lib/swarm_sdk/tools/scratchpad/scratchpad_read.rb +23 -2
- data/lib/swarm_sdk/tools/scratchpad/scratchpad_write.rb +21 -4
- data/lib/swarm_sdk/tools/stores/read_tracker.rb +47 -12
- data/lib/swarm_sdk/tools/stores/scratchpad_storage.rb +53 -5
- data/lib/swarm_sdk/tools/stores/storage.rb +0 -6
- data/lib/swarm_sdk/tools/think.rb +4 -1
- data/lib/swarm_sdk/tools/todo_write.rb +27 -8
- data/lib/swarm_sdk/tools/web_fetch.rb +3 -2
- data/lib/swarm_sdk/tools/write.rb +8 -13
- data/lib/swarm_sdk/utils.rb +18 -0
- data/lib/swarm_sdk/validation_result.rb +33 -0
- data/lib/swarm_sdk/version.rb +1 -1
- data/lib/swarm_sdk/{node → workflow}/agent_config.rb +34 -9
- data/lib/swarm_sdk/workflow/builder.rb +143 -0
- data/lib/swarm_sdk/workflow/executor.rb +497 -0
- data/lib/swarm_sdk/{node/builder.rb → workflow/node_builder.rb} +42 -21
- data/lib/swarm_sdk/{node → workflow}/transformer_executor.rb +3 -2
- data/lib/swarm_sdk/workflow.rb +554 -0
- data/lib/swarm_sdk.rb +393 -22
- metadata +51 -16
- data/lib/swarm_memory/chat_extension.rb +0 -34
- data/lib/swarm_sdk/node_orchestrator.rb +0 -591
- data/lib/swarm_sdk/providers/openai_with_responses.rb +0 -582
|
@@ -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
|
|
|
@@ -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
|
|
@@ -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 workflow)
|
|
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 workflow)
|
|
143
|
+
#
|
|
144
|
+
# @return [Boolean] true if swarm snapshot
|
|
145
|
+
def swarm?
|
|
146
|
+
type == "swarm"
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Check if snapshot is for a workflow
|
|
150
|
+
#
|
|
151
|
+
# @return [Boolean] true if workflow snapshot
|
|
152
|
+
def workflow?
|
|
153
|
+
type == "workflow"
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|