Module: Legion::Extensions::Agentic::Memory::Trace::Helpers::Snapshot
- Includes:
- Logging::Helper
- Defined in:
- lib/legion/extensions/agentic/memory/trace/helpers/snapshot.rb
Class Method Summary collapse
- .list_snapshots(agent_id:) ⇒ Object
- .prune_snapshots(agent_id:, max_count: 10) ⇒ Object
- .restore_snapshot(agent_id:) ⇒ Object
- .save_snapshot(agent_id:) ⇒ Object
Class Method Details
.list_snapshots(agent_id:) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/legion/extensions/agentic/memory/trace/helpers/snapshot.rb', line 52 def list_snapshots(agent_id:) dir = snapshot_dir(agent_id) return { success: true, snapshots: [] } unless Dir.exist?(dir) files = Dir.glob(File.join(dir, '*.snapshot')).map do |f| { filename: File.basename(f), size: File.size(f), mtime: File.mtime(f) } end { success: true, snapshots: files } end |
.prune_snapshots(agent_id:, max_count: 10) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/legion/extensions/agentic/memory/trace/helpers/snapshot.rb', line 62 def prune_snapshots(agent_id:, max_count: 10) dir = snapshot_dir(agent_id) return { success: true, pruned: 0 } unless Dir.exist?(dir) files = Dir.glob(File.join(dir, '*.snapshot')) excess = files.size - max_count return { success: true, pruned: 0 } if excess <= 0 files.first(excess).each { |f| File.delete(f) } { success: true, pruned: excess } end |
.restore_snapshot(agent_id:) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/legion/extensions/agentic/memory/trace/helpers/snapshot.rb', line 32 def restore_snapshot(agent_id:) path = latest_snapshot_path(agent_id) return { success: false, reason: :no_snapshot } unless path raw = File.binread(path) return { success: false, reason: :too_small } if raw.bytesize < 65 signature = raw[-64..] packed = raw[0..-65] return { success: false, reason: :invalid_signature } unless verify_data(packed, signature, agent_id) state = MessagePack.unpack(packed, symbolize_keys: true) distribute_state(state) { success: true, agent_id: agent_id, timestamp: state[:timestamp] } rescue StandardError => e log.error(e.) { success: false, reason: :error, message: e. } end |
.save_snapshot(agent_id:) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/legion/extensions/agentic/memory/trace/helpers/snapshot.rb', line 17 def save_snapshot(agent_id:) state = gather_state(agent_id) packed = MessagePack.pack(state) signed = sign_data(packed, agent_id) dir = snapshot_dir(agent_id) FileUtils.mkdir_p(dir) filename = "#{Time.now.utc.strftime('%Y%m%d%H%M%S%L')}_#{SecureRandom.hex(4)}.snapshot" path = File.join(dir, filename) File.binwrite(path, signed) prune_snapshots(agent_id: agent_id) { success: true, path: path, size: signed.bytesize } end |