Class: SwarmSDK::StateRestorer
- Inherits:
-
Object
- Object
- SwarmSDK::StateRestorer
- Defined in:
- lib/swarm_sdk/state_restorer.rb
Overview
Restores swarm/workflow conversation state from snapshots
Unified implementation that works for both Swarm and Workflow. Validates compatibility between snapshot and current configuration, restores conversation history, context state, scratchpad contents, and read tracking information.
Handles configuration mismatches gracefully by skipping agents that don't exist in the current swarm/workflow and returning warnings in RestoreResult.
System Prompt Handling
By default, system prompts are taken from the current YAML configuration, not from the snapshot. This makes configuration the source of truth and allows you to update system prompts without creating new sessions.
Set preserve_system_prompts: true to use historical prompts from the snapshot
(useful for debugging, auditing, or exact reproducibility).
Instance Method Summary collapse
-
#initialize(orchestration, snapshot, preserve_system_prompts: false) ⇒ StateRestorer
constructor
Initialize state restorer.
-
#restore ⇒ RestoreResult
Restore state from snapshot.
Constructor Details
#initialize(orchestration, snapshot, preserve_system_prompts: false) ⇒ StateRestorer
Initialize state restorer
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/swarm_sdk/state_restorer.rb', line 38 def initialize(orchestration, snapshot, preserve_system_prompts: false) @orchestration = orchestration @preserve_system_prompts = preserve_system_prompts # Handle different input types @snapshot_data = case snapshot when Snapshot snapshot.to_hash when String JSON.parse(snapshot, symbolize_names: true) when Hash snapshot else raise ArgumentError, "snapshot must be a Snapshot object, Hash, or JSON string" end validate_version! validate_type_match! end |
Instance Method Details
#restore ⇒ RestoreResult
Restore state from snapshot
Three-phase process:
- Validate compatibility (which agents can be restored)
- Restore state (only for matched agents)
- Return result with warnings about skipped agents
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/swarm_sdk/state_restorer.rb', line 66 def restore # Phase 1: Validate compatibility validation = validate_compatibility # Phase 2: Restore state (only for matched agents) restore_agent_conversations(validation.restorable_agents) restore_delegation_conversations(validation.restorable_delegations) restore_scratchpad restore_read_tracking restore_plugin_states # Phase 3: Return result with warnings SwarmSDK::RestoreResult.new( warnings: validation.warnings, skipped_agents: validation.skipped_agents, skipped_delegations: validation.skipped_delegations, ) end |