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

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest.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

#initialize(topic:, domain: :unknown) ⇒ Palimpsest

Returns a new instance of Palimpsest.



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

def initialize(topic:, domain: :unknown)
  @id                = ::SecureRandom.uuid
  @topic             = topic
  @domain            = domain
  @current_layer     = nil
  @historical_layers = []
  @created_at        = ::Time.now.utc
  @overwrite_count   = 0
  @version_counter   = 0
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#current_layerObject (readonly)

Returns the value of attribute current_layer.



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

def current_layer
  @current_layer
end

#domainObject (readonly)

Returns the value of attribute domain.



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

def domain
  @domain
end

#historical_layersObject (readonly)

Returns the value of attribute historical_layers.



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

def historical_layers
  @historical_layers
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#overwrite_countObject (readonly)

Returns the value of attribute overwrite_count.



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

def overwrite_count
  @overwrite_count
end

#topicObject (readonly)

Returns the value of attribute topic.



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

def topic
  @topic
end

Instance Method Details

#all_layersObject



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

def all_layers
  layers = @historical_layers.dup
  layers << @current_layer if @current_layer
  layers
end

#belief_driftObject



82
83
84
85
86
87
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest.rb', line 82

def belief_drift
  return 0.0 unless @current_layer && @historical_layers.any?

  origin = @historical_layers.first
  (@current_layer.confidence - origin.confidence).abs.round(10)
end

#decay_ghosts!(rate: GHOST_DECAY) ⇒ Object



93
94
95
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest.rb', line 93

def decay_ghosts!(rate: GHOST_DECAY)
  @historical_layers.each { |layer| layer.erode!(rate: rate) if layer.superseded? }
end

#drift_labelObject



89
90
91
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest.rb', line 89

def drift_label
  Constants.label_for(DRIFT_LABELS, belief_drift)
end

#erode_current!(rate: EROSION_RATE) ⇒ Object



57
58
59
60
61
62
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest.rb', line 57

def erode_current!(rate: EROSION_RATE)
  return nil unless @current_layer

  @current_layer.erode!(rate: rate)
  @current_layer.confidence
end

#ghost_layersObject



64
65
66
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest.rb', line 64

def ghost_layers
  @historical_layers.select(&:ghost?)
end

#overwrite!(new_content, confidence: DEFAULT_CONFIDENCE, author: :system) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest.rb', line 29

def overwrite!(new_content, confidence: DEFAULT_CONFIDENCE, author: :system)
  return false if at_layer_limit?

  @version_counter += 1
  new_layer = BeliefLayer.new(
    content:    new_content,
    confidence: confidence,
    domain:     @domain,
    version:    @version_counter,
    author:     author
  )

  if @current_layer
    @current_layer.supersede!(new_layer.id)
    @historical_layers << @current_layer
  end

  @current_layer = new_layer
  @overwrite_count += 1
  new_layer
end

#peek_through(depth: 1) ⇒ Object



51
52
53
54
55
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest.rb', line 51

def peek_through(depth: 1)
  return [] if @historical_layers.empty?

  @historical_layers.last([depth, @historical_layers.size].min).reverse
end

#restoration_strengthObject



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

def restoration_strength
  ghosts = ghost_layers
  return 0.0 if ghosts.empty?

  total = ghosts.sum(&:confidence)
  (total / ghosts.size).round(10)
end

#to_hObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/legion/extensions/agentic/memory/palimpsest/helpers/palimpsest.rb', line 97

def to_h
  {
    id:                   @id,
    topic:                @topic,
    domain:               @domain,
    layer_count:          all_layers.size,
    overwrite_count:      @overwrite_count,
    ghost_count:          ghost_layers.size,
    restoration_strength: restoration_strength.round(4),
    belief_drift:         belief_drift.round(4),
    drift_label:          drift_label,
    current_layer:        @current_layer&.to_h,
    created_at:           @created_at.iso8601
  }
end