Class: Legion::Extensions::Agentic::Attention::Prism::Helpers::PrismEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/attention/prism/helpers/prism_engine.rb

Instance Method Summary collapse

Constructor Details

#initializePrismEngine

Returns a new instance of PrismEngine.



10
11
12
13
# File 'lib/legion/extensions/agentic/attention/prism/helpers/prism_engine.rb', line 10

def initialize
  @beams      = {}
  @components = {}
end

Instance Method Details

#attenuate_all!(rate: SpectralComponent::ATTENUATION_RATE_DEFAULT) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/legion/extensions/agentic/attention/prism/helpers/prism_engine.rb', line 49

def attenuate_all!(rate: SpectralComponent::ATTENUATION_RATE_DEFAULT)
  count = 0
  @beams.each_value do |beam|
    beam.components.each do |c|
      c.attenuate!(rate: rate)
      count += 1
    end
  end
  { success: true, attenuated: count, rate: rate }
end

#beam_countObject



103
104
105
# File 'lib/legion/extensions/agentic/attention/prism/helpers/prism_engine.rb', line 103

def beam_count
  @beams.size
end

#clear!Object



107
108
109
110
111
# File 'lib/legion/extensions/agentic/attention/prism/helpers/prism_engine.rb', line 107

def clear!
  @beams.clear
  @components.clear
  self
end

#create_beam(domain:, content:, beam_id: nil) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
# File 'lib/legion/extensions/agentic/attention/prism/helpers/prism_engine.rb', line 15

def create_beam(domain:, content:, beam_id: nil)
  raise ArgumentError, "MAX_BEAMS (#{Constants::MAX_BEAMS}) reached" if @beams.size >= Constants::MAX_BEAMS

  id   = beam_id || SecureRandom.uuid
  beam = Beam.new(beam_id: id, domain: domain, content: content)
  @beams[id] = beam
  { success: true, beam_id: id, domain: domain }
end

#decompose(beam_id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/legion/extensions/agentic/attention/prism/helpers/prism_engine.rb', line 24

def decompose(beam_id)
  beam = @beams.fetch(beam_id) { return { success: false, error: "Beam #{beam_id} not found" } }

  beam.decompose!
  beam.components.each { |c| @components["#{beam_id}:#{c.band}"] = c }

  Legion::Logging.debug("[cognitive_prism] decompose: beam=#{beam_id[0..7]} components=#{beam.components.size} purity=#{beam.purity.round(4)}") # rubocop:disable Legion/HelperMigration/DirectLogging
  {
    success:         true,
    beam_id:         beam_id,
    component_count: beam.components.size,
    dominant_band:   beam.dominant_band,
    purity:          beam.purity
  }
end

#dominant_bandsObject



60
61
62
63
64
65
66
67
# File 'lib/legion/extensions/agentic/attention/prism/helpers/prism_engine.rb', line 60

def dominant_bands
  result = Hash.new(0)
  @beams.each_value do |beam|
    band = beam.dominant_band
    result[band] += 1 if band
  end
  result.sort_by { |_, v| -v }.to_h
end

#get_beam(beam_id) ⇒ Object



99
100
101
# File 'lib/legion/extensions/agentic/attention/prism/helpers/prism_engine.rb', line 99

def get_beam(beam_id)
  @beams[beam_id]
end

#most_intense(limit: 5) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/legion/extensions/agentic/attention/prism/helpers/prism_engine.rb', line 69

def most_intense(limit: 5)
  all_components = @beams.values.flat_map(&:components)
  all_components
    .sort_by { |c| -c.intensity }
    .first(limit)
    .map(&:to_h)
end

#recompose(component_ids) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/legion/extensions/agentic/attention/prism/helpers/prism_engine.rb', line 40

def recompose(component_ids)
  selected = component_ids.filter_map { |cid| resolve_component(cid) }
  active   = selected.reject(&:faded?)
  return { success: true, synthesis: '', active_count: 0, total_count: 0 } if active.empty?

  synthesis = build_synthesis(active)
  { success: true, synthesis: synthesis, active_count: active.size, total_count: selected.size }
end

#spectral_reportObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/legion/extensions/agentic/attention/prism/helpers/prism_engine.rb', line 77

def spectral_report
  total_beams      = @beams.size
  total_components = @beams.values.sum { |b| b.components.size }
  decomposed_beams = @beams.count { |_, b| b.components.any? }

  avg_purity = if decomposed_beams.positive?
                 purity_sum = @beams.values.select { |b| b.components.any? }.sum(&:purity)
                 (purity_sum / decomposed_beams).round(10)
               else
                 0.0
               end

  {
    total_beams:      total_beams,
    decomposed_beams: decomposed_beams,
    total_components: total_components,
    dominant_bands:   dominant_bands,
    avg_purity:       avg_purity,
    most_intense:     most_intense(limit: 3)
  }
end