Class: Legion::Extensions::Agentic::Memory::Palimpsest::Helpers::PalimpsestEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb

Constant Summary

Constants included from Constants

Constants::CONFIDENCE_LABELS, Constants::DEFAULT_CONFIDENCE, Constants::DRIFT_LABELS, Constants::EROSION_RATE, Constants::GHOST_DECAY, Constants::GHOST_LABELS, Constants::GHOST_THRESHOLD, Constants::LAYER_DOMAINS, Constants::MAX_LAYERS_PER_TOPIC, Constants::MAX_PALIMPSESTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Constants

label_for

Constructor Details

#initializePalimpsestEngine

Returns a new instance of PalimpsestEngine.



14
15
16
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb', line 14

def initialize
  @palimpsests = {}
end

Instance Attribute Details

#palimpsestsObject (readonly)

Returns the value of attribute palimpsests.



12
13
14
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb', line 12

def palimpsests
  @palimpsests
end

Instance Method Details

#all_ghost_layersObject



55
56
57
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb', line 55

def all_ghost_layers
  @palimpsests.flat_map { |_topic, p| p.ghost_layers.map(&:to_h) }
end

#belief_drift(topic:) ⇒ Object



67
68
69
70
71
72
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb', line 67

def belief_drift(topic:)
  p = @palimpsests[topic]
  return nil unless p

  { drift: p.belief_drift.round(4), label: p.drift_label }
end

#create(topic:, domain: :unknown) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb', line 18

def create(topic:, domain: :unknown)
  return nil if @palimpsests.size >= MAX_PALIMPSESTS
  return nil if @palimpsests.key?(topic)

  p = Palimpsest.new(topic: topic, domain: domain)
  @palimpsests[topic] = p
  p
end

#decay_all!(ghost_rate: GHOST_DECAY) ⇒ Object



86
87
88
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb', line 86

def decay_all!(ghost_rate: GHOST_DECAY)
  @palimpsests.each_value { |p| p.decay_ghosts!(rate: ghost_rate) }
end

#domain_archaeology(domain:) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb', line 59

def domain_archaeology(domain:)
  @palimpsests.each_with_object([]) do |(_topic, p), results|
    next unless p.domain == domain

    p.all_layers.each { |layer| results << layer.to_h.merge(topic: p.topic) }
  end
end

#erode(topic:, rate: EROSION_RATE) ⇒ Object



41
42
43
44
45
46
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb', line 41

def erode(topic:, rate: EROSION_RATE)
  p = @palimpsests[topic]
  return nil unless p

  p.erode_current!(rate: rate)
end

#ghost_layers_for(topic:) ⇒ Object



48
49
50
51
52
53
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb', line 48

def ghost_layers_for(topic:)
  p = @palimpsests[topic]
  return [] unless p

  p.ghost_layers.map(&:to_h)
end

#most_rewritten(limit: 10) ⇒ Object



81
82
83
84
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb', line 81

def most_rewritten(limit: 10)
  sorted = @palimpsests.values.sort_by { |p| -p.overwrite_count }
  sorted.first(limit).map(&:to_h)
end

#overwrite(topic:, content:, confidence: DEFAULT_CONFIDENCE, author: :system) ⇒ Object



27
28
29
30
31
32
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb', line 27

def overwrite(topic:, content:, confidence: DEFAULT_CONFIDENCE, author: :system)
  p = find_or_create(topic)
  return nil unless p

  p.overwrite!(content, confidence: confidence, author: author)
end

#overwrite_frequency(topic:) ⇒ Object



74
75
76
77
78
79
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb', line 74

def overwrite_frequency(topic:)
  p = @palimpsests[topic]
  return nil unless p

  p.overwrite_count
end

#palimpsest_reportObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb', line 90

def palimpsest_report
  total_ghosts = @palimpsests.values.sum { |p| p.ghost_layers.size }
  avg_drift    = if @palimpsests.empty?
                   0.0
                 else
                   total = @palimpsests.values.sum(&:belief_drift)
                   (total / @palimpsests.size).round(4)
                 end
  {
    palimpsest_count: @palimpsests.size,
    total_ghosts:     total_ghosts,
    average_drift:    avg_drift,
    most_rewritten:   most_rewritten(limit: 5)
  }
end

#peek_through(topic:, depth: 1) ⇒ Object



34
35
36
37
38
39
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest_engine.rb', line 34

def peek_through(topic:, depth: 1)
  p = @palimpsests[topic]
  return [] unless p

  p.peek_through(depth: depth).map(&:to_h)
end