Class: Legion::Extensions::Agentic::Attention::Schema::Helpers::AttentionSchemaModel

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb

Constant Summary

Constants included from Constants

Constants::ATTENTION_STATE_LABELS, Constants::AWARENESS_BOOST, Constants::AWARENESS_DECAY, Constants::AWARENESS_FLOOR, Constants::AWARENESS_LABELS, Constants::DEFAULT_AWARENESS, Constants::DRIFT_THRESHOLD, Constants::HYPERFOCUS_THRESHOLD, Constants::MAX_HISTORY, Constants::MAX_SCHEMA_ITEMS, Constants::MAX_SOCIAL_MODELS, Constants::META_ATTENTION_ALPHA, Constants::SCHEMA_UPDATE_ALPHA

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAttentionSchemaModel

Returns a new instance of AttentionSchemaModel.



14
15
16
17
18
19
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 14

def initialize
  @schema_items    = {}    # target => SchemaItem
  @social_models   = {}    # agent_id => { target:, awareness:, updated_at: }
  @meta_accuracy   = 0.5   # EMA confidence in schema self-accuracy
  @attention_history = []  # ring buffer of { target:, event:, at: }
end

Instance Attribute Details

#attention_historyObject (readonly)

Returns the value of attribute attention_history.



12
13
14
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 12

def attention_history
  @attention_history
end

#meta_accuracyObject (readonly)

Returns the value of attribute meta_accuracy.



12
13
14
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 12

def meta_accuracy
  @meta_accuracy
end

#schema_itemsObject (readonly)

Returns the value of attribute schema_items.



12
13
14
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 12

def schema_items
  @schema_items
end

#social_modelsObject (readonly)

Returns the value of attribute social_models.



12
13
14
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 12

def social_models
  @social_models
end

Instance Method Details

#am_i_aware_of(target:) ⇒ Object

“Am I aware of X?” — the central Graziano query



54
55
56
57
58
59
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 54

def am_i_aware_of(target:)
  item = @schema_items[target.to_s]
  return { aware: false, awareness_level: 0.0, label: :unconscious } unless item

  { aware: item.awareness_level > AWARENESS_FLOOR, awareness_level: item.awareness_level.round(4), label: item.label }
end

#attention_stateObject

Overall qualitative attention state



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 89

def attention_state
  return :distracted if @schema_items.empty?

  top = top_awareness
  avg = average_awareness

  if top >= HYPERFOCUS_THRESHOLD
    :hyperfocused
  elsif avg < DRIFT_THRESHOLD && @schema_items.size > 3
    :distracted
  elsif avg < DRIFT_THRESHOLD
    :drifting
  elsif top >= 0.6
    :focused
  else
    :normal
  end
end

#decay_allObject

Apply per-tick decay to all schema items and prune faded ones



159
160
161
162
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 159

def decay_all
  @schema_items.each_value(&:decay)
  @schema_items.reject! { |_, item| item.faded? }
end

#defocus(target:) ⇒ Object

Remove an item from the schema entirely



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

def defocus(target:)
  target = target.to_s
  removed = @schema_items.delete(target)
  record_history(target, :defocused) if removed
  !removed.nil?
end

#focus_on(target:, domain:, reason:, source:) ⇒ Object

Add or boost an item in the attention schema



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

def focus_on(target:, domain:, reason:, source:)
  target = target.to_s
  if @schema_items.key?(target)
    @schema_items[target].boost
    record_history(target, :refocused)
  else
    prune_to_capacity if @schema_items.size >= MAX_SCHEMA_ITEMS
    @schema_items[target] = SchemaItem.new(
      target:          target,
      domain:          domain,
      reason:          reason,
      source:          source,
      awareness_level: DEFAULT_AWARENESS + AWARENESS_BOOST
    )
    record_history(target, :focused)
  end
  @schema_items[target]
end

#meta_checkObject

Assess whether meta-attention signals are active



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 129

def meta_check
  state = attention_state
  signals = []

  signals << :drifting   if %i[drifting distracted].include?(state)
  signals << :hyperfocus if state == :hyperfocused
  signals << :normal     if signals.empty?

  top = top_awareness
  avg = average_awareness

  {
    state:         state,
    signals:       signals,
    top_awareness: top.round(4),
    avg_awareness: avg.round(4),
    schema_size:   @schema_items.size,
    meta_accuracy: @meta_accuracy.round(4)
  }
end

#model_other_attention(agent_id:, target:, awareness:) ⇒ Object

Record what another agent appears to be attending to



111
112
113
114
115
116
117
118
119
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 111

def model_other_attention(agent_id:, target:, awareness:)
  agent_id = agent_id.to_s
  prune_social_models if @social_models.size >= MAX_SOCIAL_MODELS && !@social_models.key?(agent_id)
  @social_models[agent_id] = {
    target:     target.to_s,
    awareness:  awareness.clamp(0.0, 1.0),
    updated_at: Time.now.utc
  }
end

#query_other_attention(agent_id:) ⇒ Object

Query what another agent is modeled as attending to



122
123
124
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 122

def query_other_attention(agent_id:)
  @social_models[agent_id.to_s]
end

#report_awarenessObject

Natural-language-style summary of current attention state



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 64

def report_awareness
  state     = attention_state
  state_str = ATTENTION_STATE_LABELS[state] || 'in an unknown state'
  top_items = top_schema_items(3)

  return { state: state, state_label: state_str, report: 'No active attention targets.', items: [] } if top_items.empty?

  primary  = top_items.first
  summary  = "I am #{state_str}, primarily attending to '#{primary[:target]}' " \
             "(#{primary[:label]}, #{primary[:awareness_level]}). " \
             "Reason: #{primary[:reason]}."

  if top_items.size > 1
    secondary = top_items[1..]
                .map { |i| "'#{i[:target]}' (#{i[:label]})" }
                .join(', ')
    summary += " Also attending to: #{secondary}."
  end

  { state: state, state_label: state_str, report: summary, items: top_items }
end

#schema_sizeObject

— Accessors —



166
167
168
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 166

def schema_size
  @schema_items.size
end

#to_hObject



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 170

def to_h
  {
    state:         attention_state,
    state_label:   ATTENTION_STATE_LABELS[attention_state],
    schema_size:   schema_size,
    meta_accuracy: @meta_accuracy.round(4),
    top_awareness: top_awareness.round(4),
    avg_awareness: average_awareness.round(4),
    social_models: @social_models.size,
    items:         @schema_items.values.map(&:to_h)
  }
end

#update_meta_accuracy(was_correct:) ⇒ Object

Update the EMA tracking how well the schema predicted actual attention



151
152
153
154
# File 'lib/legion/extensions/agentic/attention/schema/helpers/attention_schema_model.rb', line 151

def update_meta_accuracy(was_correct:)
  correction = was_correct ? 1.0 : 0.0
  @meta_accuracy += (META_ATTENTION_ALPHA * (correction - @meta_accuracy))
end