Class: Legion::Extensions::Agentic::Attention::Salience::Helpers::SalienceMap

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

Constant Summary collapse

MAX_HISTORY =
50
TREND_WINDOW =
5
TREND_DELTA =
0.02

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSalienceMap

Returns a new instance of SalienceMap.



16
17
18
19
20
21
# File 'lib/legion/extensions/agentic/attention/salience/helpers/salience_map.rb', line 16

def initialize
  @current_map   = {}
  @baseline      = 0.0
  @history       = []
  @novel_sources = {}
end

Instance Attribute Details

#baselineObject (readonly)

Returns the value of attribute baseline.



10
11
12
# File 'lib/legion/extensions/agentic/attention/salience/helpers/salience_map.rb', line 10

def baseline
  @baseline
end

#current_mapObject (readonly)

Returns the value of attribute current_map.



10
11
12
# File 'lib/legion/extensions/agentic/attention/salience/helpers/salience_map.rb', line 10

def current_map
  @current_map
end

#historyObject (readonly)

Returns the value of attribute history.



10
11
12
# File 'lib/legion/extensions/agentic/attention/salience/helpers/salience_map.rb', line 10

def history
  @history
end

Instance Method Details

#above_baseline?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/legion/extensions/agentic/attention/salience/helpers/salience_map.rb', line 57

def above_baseline?
  overall_salience > @baseline
end

#conflict_signalsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/legion/extensions/agentic/attention/salience/helpers/salience_map.rb', line 61

def conflict_signals
  values = @current_map.values
  return [] if values.size < 2

  pairs = []
  keys  = @current_map.keys
  keys.each_with_index do |key_a, i|
    keys[(i + 1)..].each do |key_b|
      diff = (@current_map[key_a] - @current_map[key_b]).abs
      pairs << [key_a, key_b] if diff > 0.3
    end
  end
  pairs
end

#dominant_sourceObject



51
52
53
54
55
# File 'lib/legion/extensions/agentic/attention/salience/helpers/salience_map.rb', line 51

def dominant_source
  return nil if @current_map.empty?

  @current_map.max_by { |_k, v| v }&.first
end

#overall_salienceObject



40
41
42
# File 'lib/legion/extensions/agentic/attention/salience/helpers/salience_map.rb', line 40

def overall_salience
  @current_map.values.sum.round(6)
end

#salience_trendObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/legion/extensions/agentic/attention/salience/helpers/salience_map.rb', line 76

def salience_trend
  return :stable if @history.size < 2

  window   = @history.last([TREND_WINDOW, @history.size].min).map { |h| h[:overall] }
  delta    = window.last - window.first
  if delta > TREND_DELTA
    :rising
  elsif delta < -TREND_DELTA
    :falling
  else
    :stable
  end
end

#to_hObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/legion/extensions/agentic/attention/salience/helpers/salience_map.rb', line 90

def to_h
  {
    current_map:    @current_map,
    overall:        overall_salience,
    baseline:       @baseline,
    urgency:        urgency_level,
    dominant:       dominant_source,
    above_baseline: above_baseline?,
    conflicts:      conflict_signals,
    trend:          salience_trend,
    history_size:   @history.size
  }
end

#update(integrated_signals) ⇒ Object



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

def update(integrated_signals)
  @current_map = integrated_signals.transform_values { |v| v[:weighted] }
  apply_novelty_boost(integrated_signals)
  overall = overall_salience
  @baseline = (Constants::INTEGRATION_ALPHA * overall) + ((1.0 - Constants::INTEGRATION_ALPHA) * @baseline)
  snapshot = {
    timestamp:    Time.now.utc.to_f,
    overall:      overall,
    urgency:      urgency_level,
    dominant:     dominant_source,
    map_snapshot: @current_map.dup
  }
  @history << snapshot
  @history.shift while @history.size > MAX_HISTORY
  snapshot
end

#urgency_levelObject



44
45
46
47
48
49
# File 'lib/legion/extensions/agentic/attention/salience/helpers/salience_map.rb', line 44

def urgency_level
  score = overall_salience
  Constants::URGENCY_LEVELS.find do |level|
    score >= Constants::URGENCY_THRESHOLDS[level]
  end || :background
end