Module: Legion::Extensions::Agentic::Affect::Regulation::Runners::EmotionalRegulation

Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/agentic/affect/regulation/runners/emotional_regulation.rb

Instance Method Summary collapse

Instance Method Details

#emotional_regulation_statsObject

Return aggregate statistics about regulation performance.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/legion/extensions/agentic/affect/regulation/runners/emotional_regulation.rb', line 84

def emotional_regulation_stats(**)
  history = regulation_model.regulation_history
  total   = history.size

  if total.zero?
    return { success: true, total_events: 0, success_rate: 0.0,
             average_cost: 0.0, strategy_breakdown: {},
             overall_ability: regulation_model.overall_regulation_ability,
             regulation_label: regulation_model.regulation_label }
  end

  successes  = history.count { |e| e[:success] }
  total_cost = history.sum { |e| e[:cost] }

  strategy_breakdown = Helpers::Constants::STRATEGIES.to_h do |strategy|
    events = history.select { |e| e[:strategy] == strategy }
    [strategy, { count: events.size, successes: events.count { |e| e[:success] } }]
  end

  log.debug("[emotional_regulation] stats: total=#{total} success_rate=#{(successes.to_f / total).round(2)}")

  {
    success:            true,
    total_events:       total,
    success_rate:       successes.to_f / total,
    average_cost:       total_cost / total,
    strategy_breakdown: strategy_breakdown,
    overall_ability:    regulation_model.overall_regulation_ability,
    regulation_label:   regulation_model.regulation_label
  }
end

#recommend_strategy(emotion_magnitude:, emotion_valence: :neutral, context: :general) ⇒ Object

Return a strategy recommendation without applying it.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/legion/extensions/agentic/affect/regulation/runners/emotional_regulation.rb', line 35

def recommend_strategy(emotion_magnitude:, emotion_valence: :neutral, context: :general, **)
  result = regulation_model.recommend_strategy(
    emotion_magnitude: emotion_magnitude,
    emotion_valence:   emotion_valence,
    context:           context
  )

  log.debug("[emotional_regulation] recommend: magnitude=#{emotion_magnitude.round(2)} " \
            "context=#{context} recommended=#{result[:recommended]}")

  { success: true }.merge(result)
end

#regulate_emotion(emotion_magnitude:, emotion_valence: :neutral, strategy: nil) ⇒ Object

Apply emotion regulation. Auto-selects strategy when none is provided.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/legion/extensions/agentic/affect/regulation/runners/emotional_regulation.rb', line 14

def regulate_emotion(emotion_magnitude:, emotion_valence: :neutral, strategy: nil, **)
  chosen = strategy || regulation_model.recommend_strategy(
    emotion_magnitude: emotion_magnitude,
    emotion_valence:   emotion_valence
  )[:recommended]

  result = regulation_model.regulate(
    emotion_magnitude: emotion_magnitude,
    emotion_valence:   emotion_valence,
    strategy:          chosen
  )

  log.debug("[emotional_regulation] regulate: strategy=#{chosen} " \
            "magnitude=#{emotion_magnitude.round(2)} -> " \
            "#{result[:regulated_magnitude].round(2)} " \
            "cost=#{result[:cost].round(3)} success=#{result[:success]}")

  { success: true }.merge(result)
end

#regulation_history(count: 20) ⇒ Object

Return recent regulation events.



77
78
79
80
81
# File 'lib/legion/extensions/agentic/affect/regulation/runners/emotional_regulation.rb', line 77

def regulation_history(count: 20, **)
  events = regulation_model.regulation_history.last(count)
  log.debug("[emotional_regulation] history: requested=#{count} returned=#{events.size}")
  { success: true, events: events, count: events.size }
end

#regulation_profileObject

Return the full skill profile across all strategies.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/legion/extensions/agentic/affect/regulation/runners/emotional_regulation.rb', line 60

def regulation_profile(**)
  profile = Helpers::Constants::STRATEGIES.to_h do |strategy|
    [strategy, regulation_model.skill_for(strategy)]
  end

  log.debug("[emotional_regulation] profile query: overall=#{regulation_model.overall_regulation_ability.round(3)}")

  {
    success:      true,
    skills:       profile,
    overall:      regulation_model.overall_regulation_ability,
    label:        regulation_model.regulation_label,
    suppressions: regulation_model.consecutive_suppressions
  }
end

#update_emotional_regulationObject

Per-tick skill decay — call from scheduler or tick actor.



49
50
51
52
53
54
55
56
57
# File 'lib/legion/extensions/agentic/affect/regulation/runners/emotional_regulation.rb', line 49

def update_emotional_regulation(**)
  regulation_model.decay
  ability = regulation_model.overall_regulation_ability
  label   = regulation_model.regulation_label

  log.debug("[emotional_regulation] decay tick: ability=#{ability.round(3)} label=#{label}")

  { success: true, overall_ability: ability, regulation_label: label }
end