Class: Legion::Extensions::Agentic::Defense::Dissonance::Helpers::DissonanceModel

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/defense/dissonance/helpers/dissonance_model.rb

Constant Summary

Constants included from Constants

Constants::CONTRADICTION_TYPES, Constants::DECAY_RATE, Constants::DISSONANCE_THRESHOLD, Constants::IMPORTANCE_WEIGHTS, Constants::MAX_BELIEFS, Constants::MAX_DISSONANCE_EVENTS, Constants::RATIONALIZATION_FACTOR, Constants::RESOLUTION_RELIEF, Constants::RESOLUTION_STRATEGIES, Constants::STRESS_CEILING, Constants::STRESS_FLOOR

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDissonanceModel

Returns a new instance of DissonanceModel.



14
15
16
17
18
# File 'lib/legion/extensions/agentic/defense/dissonance/helpers/dissonance_model.rb', line 14

def initialize
  @beliefs = {}
  @events  = {}
  @stress  = STRESS_FLOOR
end

Instance Attribute Details

#beliefsObject (readonly)

Returns the value of attribute beliefs.



12
13
14
# File 'lib/legion/extensions/agentic/defense/dissonance/helpers/dissonance_model.rb', line 12

def beliefs
  @beliefs
end

#eventsObject (readonly)

Returns the value of attribute events.



12
13
14
# File 'lib/legion/extensions/agentic/defense/dissonance/helpers/dissonance_model.rb', line 12

def events
  @events
end

#stressObject (readonly)

Returns the value of attribute stress.



12
13
14
# File 'lib/legion/extensions/agentic/defense/dissonance/helpers/dissonance_model.rb', line 12

def stress
  @stress
end

Instance Method Details

#add_belief(domain:, content:, confidence: 0.7, importance: :moderate) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/legion/extensions/agentic/defense/dissonance/helpers/dissonance_model.rb', line 20

def add_belief(domain:, content:, confidence: 0.7, importance: :moderate)
  prune_beliefs if @beliefs.size >= MAX_BELIEFS

  belief = Belief.new(domain: domain, content: content,
                      confidence: confidence, importance: importance)
  @beliefs[belief.id] = belief

  new_events = detect_contradictions_for(belief)
  new_events.each { |ev| @events[ev.id] = ev }

  { belief: belief, new_dissonance_events: new_events }
end

#decayObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/legion/extensions/agentic/defense/dissonance/helpers/dissonance_model.rb', line 79

def decay
  unresolved = unresolved_events
  if unresolved.any?
    increment = DECAY_RATE * unresolved.size
    @stress   = (@stress + increment).clamp(STRESS_FLOOR, STRESS_CEILING)
  else
    @stress = (@stress - DECAY_RATE).clamp(STRESS_FLOOR, STRESS_CEILING)
  end
  @stress
end

#detect_contradictionsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/legion/extensions/agentic/defense/dissonance/helpers/dissonance_model.rb', line 33

def detect_contradictions
  new_events = []
  belief_list = @beliefs.values

  belief_list.each_with_index do |bel_a, idx|
    belief_list[(idx + 1)..].each do |bel_b|
      next unless bel_a.contradicts?(bel_b)
      next if contradiction_tracked?(bel_a.id, bel_b.id)

      ev = build_event(bel_a, bel_b)
      @events[ev.id] = ev
      new_events << ev
    end
  end

  new_events
end

#domain_stress(domain) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/legion/extensions/agentic/defense/dissonance/helpers/dissonance_model.rb', line 67

def domain_stress(domain)
  unresolved = unresolved_events.select { |ev| ev.domain == domain }
  return STRESS_FLOOR if unresolved.empty?

  raw = unresolved.sum(&:magnitude) / MAX_DISSONANCE_EVENTS.to_f
  raw.clamp(STRESS_FLOOR, STRESS_CEILING)
end

#resolve(event_id, strategy:) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/legion/extensions/agentic/defense/dissonance/helpers/dissonance_model.rb', line 51

def resolve(event_id, strategy:)
  event = @events[event_id]
  return nil unless event
  return nil if event.resolved
  return nil unless RESOLUTION_STRATEGIES.include?(strategy)

  event.resolve!(strategy)
  relief = compute_relief(strategy)
  @stress = (@stress - relief).clamp(STRESS_FLOOR, STRESS_CEILING)
  event
end

#stress_levelObject



63
64
65
# File 'lib/legion/extensions/agentic/defense/dissonance/helpers/dissonance_model.rb', line 63

def stress_level
  @stress
end

#to_hObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/legion/extensions/agentic/defense/dissonance/helpers/dissonance_model.rb', line 90

def to_h
  {
    beliefs:          @beliefs.values.map(&:to_h),
    events:           @events.values.map(&:to_h),
    stress:           @stress,
    unresolved_count: unresolved_events.size,
    total_beliefs:    @beliefs.size,
    total_events:     @events.size
  }
end

#unresolved_eventsObject



75
76
77
# File 'lib/legion/extensions/agentic/defense/dissonance/helpers/dissonance_model.rb', line 75

def unresolved_events
  @events.values.reject(&:resolved)
end