Class: Legion::Extensions::Agentic::Memory::Hologram::Helpers::Hologram

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

Constant Summary

Constants included from Constants

Constants::FIDELITY_LABELS, Constants::FRAGMENT_LABELS, Constants::INTERFERENCE_DECAY, Constants::INTERFERENCE_LABELS, Constants::MAX_HOLOGRAMS, Constants::RECONSTRUCTION_THRESHOLD, Constants::RESOLUTION_LABELS, Constants::RESOLUTION_LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Constants

label_for

Constructor Details

#initialize(domain:, content:) ⇒ Hologram

Returns a new instance of Hologram.



17
18
19
20
21
22
23
# File 'lib/legion/extensions/agentic/memory/hologram/helpers/hologram.rb', line 17

def initialize(domain:, content:)
  @id         = SecureRandom.uuid
  @domain     = domain.to_sym
  @content    = content
  @fragments  = []
  @created_at = Time.now.utc
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#created_atObject (readonly)

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#domainObject (readonly)

Returns the value of attribute domain.



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

def domain
  @domain
end

#fragmentsObject (readonly)

Returns the value of attribute fragments.



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

def fragments
  @fragments
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Instance Method Details

#add_fragment(fragment) ⇒ Object



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

def add_fragment(fragment)
  @fragments << fragment
  fragment
end

#fragment!(count) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/legion/extensions/agentic/memory/hologram/helpers/hologram.rb', line 36

def fragment!(count)
  count = count.clamp(1, 20)
  new_fragments = (1..count).map do |i|
    completeness = ((1.0 / count) + (rand * 0.2)).clamp(0.0, 1.0)
    fidelity     = (1.0 - ((i - 1).to_f / (count * 2))).clamp(0.0, 1.0)
    HolographicFragment.new(
      content:            @content,
      parent_hologram_id: @id,
      completeness:       completeness,
      fidelity:           fidelity
    )
  end
  @fragments.concat(new_fragments)
  new_fragments
end

#interference_with(other_hologram) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/legion/extensions/agentic/memory/hologram/helpers/hologram.rb', line 79

def interference_with(other_hologram)
  return 0.0 if other_hologram.nil? || other_hologram.id == @id

  self_words  = tokenize(@content)
  other_words = tokenize(other_hologram.content)

  return 0.0 if self_words.empty? || other_words.empty?

  shared = (self_words & other_words).size
  total  = (self_words | other_words).size
  return 0.0 if total.zero?

  (shared.to_f / total).round(10)
end

#reconstruct(fragments) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/legion/extensions/agentic/memory/hologram/helpers/hologram.rb', line 52

def reconstruct(fragments)
  sufficient = Array(fragments).select(&:sufficient?)
  return { success: false, reason: :insufficient_fragments, resolution: 0.0 } if sufficient.empty?

  avg_completeness = sufficient.sum(&:completeness).round(10) / sufficient.size
  avg_fidelity     = sufficient.sum(&:fidelity).round(10) / sufficient.size
  reconstructed_resolution = ((avg_completeness + avg_fidelity) / 2.0).round(10)

  {
    success:         true,
    resolution:      reconstructed_resolution,
    label:           resolution_label(reconstructed_resolution),
    fragment_count:  sufficient.size,
    total_fragments: fragments.size
  }
end

#resolutionObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/legion/extensions/agentic/memory/hologram/helpers/hologram.rb', line 25

def resolution
  return 0.0 if @fragments.empty?

  sufficient = @fragments.select(&:sufficient?)
  return 0.0 if sufficient.empty?

  avg_completeness = sufficient.sum(&:completeness).round(10) / sufficient.size
  avg_fidelity     = sufficient.sum(&:fidelity).round(10) / sufficient.size
  ((avg_completeness + avg_fidelity) / 2.0).round(10)
end

#resolution_label(res = nil) ⇒ Object



74
75
76
77
# File 'lib/legion/extensions/agentic/memory/hologram/helpers/hologram.rb', line 74

def resolution_label(res = nil)
  value = res || resolution
  Constants.label_for(Constants::RESOLUTION_LABELS, value)
end

#to_hObject



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/legion/extensions/agentic/memory/hologram/helpers/hologram.rb', line 94

def to_h
  {
    id:               @id,
    domain:           @domain,
    content:          @content,
    resolution:       resolution,
    resolution_label: resolution_label,
    fragment_count:   @fragments.size,
    fragments:        @fragments.map(&:to_h),
    created_at:       @created_at.iso8601
  }
end