Class: Legion::Extensions::Agentic::Self::Metacognition::Helpers::SnapshotStore

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/self/metacognition/helpers/snapshot_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSnapshotStore

Returns a new instance of SnapshotStore.



12
13
14
# File 'lib/legion/extensions/agentic/self/metacognition/helpers/snapshot_store.rb', line 12

def initialize
  @snapshots = []
end

Instance Attribute Details

#snapshotsObject (readonly)

Returns the value of attribute snapshots.



10
11
12
# File 'lib/legion/extensions/agentic/self/metacognition/helpers/snapshot_store.rb', line 10

def snapshots
  @snapshots
end

Instance Method Details

#architecture_changesObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/legion/extensions/agentic/self/metacognition/helpers/snapshot_store.rb', line 37

def architecture_changes
  return [] if @snapshots.size < 2

  changes = []
  @snapshots.each_cons(2) do |prev, curr|
    prev_loaded = prev.dig(:architecture, :loaded) || []
    curr_loaded = curr.dig(:architecture, :loaded) || []

    added = curr_loaded - prev_loaded
    removed = prev_loaded - curr_loaded

    next if added.empty? && removed.empty?

    changes << {
      at:      curr[:assembled_at],
      added:   added,
      removed: removed
    }
  end
  changes
end

#clearObject



73
74
75
# File 'lib/legion/extensions/agentic/self/metacognition/helpers/snapshot_store.rb', line 73

def clear
  @snapshots.clear
end

#health_trend(limit: 20) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/legion/extensions/agentic/self/metacognition/helpers/snapshot_store.rb', line 59

def health_trend(limit: 20)
  recent = @snapshots.last(limit)
  recent.filter_map do |s|
    health = s.dig(:cognitive, :health)
    next unless health

    { at: s[:assembled_at], health: health }
  end
end

#history(limit: 10) ⇒ Object



33
34
35
# File 'lib/legion/extensions/agentic/self/metacognition/helpers/snapshot_store.rb', line 33

def history(limit: 10)
  @snapshots.last(limit)
end

#latestObject



22
23
24
# File 'lib/legion/extensions/agentic/self/metacognition/helpers/snapshot_store.rb', line 22

def latest
  @snapshots.last
end

#sizeObject



69
70
71
# File 'lib/legion/extensions/agentic/self/metacognition/helpers/snapshot_store.rb', line 69

def size
  @snapshots.size
end

#stale?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
# File 'lib/legion/extensions/agentic/self/metacognition/helpers/snapshot_store.rb', line 26

def stale?
  return true if @snapshots.empty?

  age = Time.now.utc - (latest[:assembled_at] || Time.at(0))
  age > Constants::SNAPSHOT_TTL
end

#store(model) ⇒ Object



16
17
18
19
20
# File 'lib/legion/extensions/agentic/self/metacognition/helpers/snapshot_store.rb', line 16

def store(model)
  @snapshots << model
  @snapshots = @snapshots.last(Constants::MAX_SNAPSHOTS) if @snapshots.size > Constants::MAX_SNAPSHOTS
  model
end