Class: Legion::Extensions::Agentic::Affect::Reappraisal::Helpers::ReappraisalEngine
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Affect::Reappraisal::Helpers::ReappraisalEngine
show all
- Includes:
- Constants
- Defined in:
- lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb
Constant Summary
collapse
- MECHANICAL_REAPPRAISALS =
{
reinterpretation: {
negative: 'This situation may have aspects that are not immediately apparent. Consider alternative explanations.',
highly_negative: 'Strong negative reactions often signal something important. Examine what underlying need is unmet.',
neutral: 'A balanced perspective reveals both challenges and opportunities in this situation.'
},
distancing: {
negative: 'Viewed from a broader timeline, this event occupies a small portion of overall experience.',
highly_negative: 'In the larger context of ongoing goals and relationships, this moment will pass.',
neutral: 'Stepping back reveals this is one event among many.'
},
benefit_finding: {
negative: 'Difficult experiences often contain lessons that become apparent with reflection.',
highly_negative: 'Even significant setbacks can reveal strengths and areas for growth.',
neutral: 'There may be unexpected value in examining this experience closely.'
},
acceptance: {
negative: 'Acknowledging this experience as it is, without resistance, creates space for response.',
highly_negative: 'Some experiences cannot be changed, only accepted and integrated.',
neutral: 'This experience is acknowledged and integrated without judgment.'
},
normalizing: {
negative: 'Many others have faced similar situations. This reaction is a common and understandable response.',
highly_negative: 'Intense responses to difficult events are a natural part of experience, not a sign of weakness.',
neutral: 'This situation falls within the normal range of experience.'
},
perspective_taking: {
negative: 'Considering this from another vantage point reveals aspects that were not initially visible.',
highly_negative: 'Seeing through a different lens can transform how a significant event is understood.',
neutral: 'Multiple perspectives offer a fuller picture of what is happening.'
},
temporal_distancing: {
negative: 'Looking back from the future, this moment is likely to appear smaller and more manageable.',
highly_negative: 'Even the most difficult moments recede with time. This too will become part of a larger story.',
neutral: 'In the fullness of time, the significance of this event will become clearer.'
}
}.freeze
Constants included
from Constants
Constants::HIGH_INTENSITY_THRESHOLD, Constants::INTENSITY_LABELS, Constants::MAX_EVENTS, Constants::MAX_REAPPRAISALS, Constants::NEGATIVE_VALENCE_THRESHOLD, Constants::REAPPRAISAL_DIFFICULTY_MULTIPLIER, Constants::REGULATION_LABELS, Constants::STRATEGIES, Constants::STRATEGY_EFFECTIVENESS, Constants::VALENCE_LABELS
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Constants
clamp, clamp_intensity, label_for, valid_strategy?
Constructor Details
Returns a new instance of ReappraisalEngine.
52
53
54
55
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 52
def initialize
@events = {}
@reappraisal_log = []
end
|
Instance Attribute Details
#events ⇒ Object
Returns the value of attribute events.
50
51
52
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 50
def events
@events
end
|
#reappraisal_log ⇒ Object
Returns the value of attribute reappraisal_log.
50
51
52
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 50
def reappraisal_log
@reappraisal_log
end
|
Class Method Details
.mechanical_appraisal(strategy, valence) ⇒ Object
182
183
184
185
186
187
188
189
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 182
def self.mechanical_appraisal(strategy, valence)
bracket = valence_bracket(valence)
strategy = strategy.to_sym
brackets = MECHANICAL_REAPPRAISALS[strategy]
return "Reappraised via #{strategy}" unless brackets
brackets[bracket] || brackets.values.first || "Reappraised via #{strategy}"
end
|
.valence_bracket(valence) ⇒ Object
172
173
174
175
176
177
178
179
180
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 172
def self.valence_bracket(valence)
if valence < -0.6
:highly_negative
elsif valence < 0.0
:negative
else
:neutral
end
end
|
Instance Method Details
#auto_reappraise(event_id:) ⇒ Object
101
102
103
104
105
106
107
108
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 101
def auto_reappraise(event_id:)
event = @events[event_id]
return { success: false, reason: :event_not_found } unless event
strategy = select_strategy(event)
new_appraisal = self.class.mechanical_appraisal(strategy, event.current_valence)
reappraise(event_id: event_id, strategy: strategy, new_appraisal: new_appraisal)
end
|
#average_regulation ⇒ Object
132
133
134
135
136
137
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 132
def average_regulation
return 0.0 if @events.empty?
total = @events.values.sum(&:regulation_amount)
(total / @events.size).round(10)
end
|
#intense_events ⇒ Object
114
115
116
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 114
def intense_events
@events.values.select(&:intense?)
end
|
#most_regulated(limit: 5) ⇒ Object
118
119
120
121
122
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 118
def most_regulated(limit: 5)
@events.values
.sort_by { |e| -e.regulation_amount }
.first(limit)
end
|
#negative_events ⇒ Object
110
111
112
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 110
def negative_events
@events.values.select(&:negative?)
end
|
#overall_regulation_ability ⇒ Object
139
140
141
142
143
144
145
146
147
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 139
def overall_regulation_ability
return 0.0 if @events.empty?
regulated_count = @events.values.count { |e| e.regulation_amount > 0.0 }
mean_reg = average_regulation
coverage = regulated_count.to_f / @events.size
((mean_reg + coverage) / 2.0).round(10)
end
|
#reappraisal_report ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 149
def reappraisal_report
{
total_events: @events.size,
total_reappraisals: @reappraisal_log.size,
negative_events: negative_events.size,
intense_events: intense_events.size,
average_regulation: average_regulation,
overall_regulation_ability: overall_regulation_ability,
strategy_effectiveness: strategy_effectiveness,
most_regulated: most_regulated(limit: 3).map(&:to_h)
}
end
|
#reappraise(event_id:, strategy:, new_appraisal:) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 70
def reappraise(event_id:, strategy:, new_appraisal:)
event = @events[event_id]
return { success: false, reason: :event_not_found } unless event
return { success: false, reason: :invalid_strategy } unless Constants.valid_strategy?(strategy)
old_valence = event.current_valence
old_intensity = event.current_intensity
change = event.reappraise!(strategy: strategy, new_appraisal: new_appraisal)
log_entry = {
event_id: event_id,
strategy: strategy,
valence_change: (event.current_valence - old_valence).round(10),
intensity_change: (old_intensity - event.current_intensity).round(10),
applied_at: Time.now.utc
}
@reappraisal_log.shift while @reappraisal_log.size >= Constants::MAX_REAPPRAISALS
@reappraisal_log << log_entry
{
success: true,
event_id: event_id,
strategy: strategy,
change: change.round(10),
current_valence: event.current_valence.round(10),
current_intensity: event.current_intensity.round(10)
}
end
|
#register_event(content:, valence:, intensity:, appraisal:) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 57
def register_event(content:, valence:, intensity:, appraisal:)
event = EmotionalEvent.new(
content: content,
valence: valence,
intensity: intensity,
appraisal: appraisal
)
@events.shift while @events.size >= Constants::MAX_EVENTS
@events[event.id] = event
event
end
|
#strategy_effectiveness ⇒ Object
124
125
126
127
128
129
130
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 124
def strategy_effectiveness
grouped = @reappraisal_log.group_by { |entry| entry[:strategy] }
grouped.transform_values do |entries|
changes = entries.map { |e| e[:valence_change] }
changes.sum.to_f / changes.size
end
end
|
#to_h ⇒ Object
162
163
164
165
166
167
168
169
170
|
# File 'lib/legion/extensions/agentic/affect/reappraisal/helpers/reappraisal_engine.rb', line 162
def to_h
{
events: @events.transform_values(&:to_h),
reappraisal_log: @reappraisal_log,
average_regulation: average_regulation,
overall_regulation_ability: overall_regulation_ability,
strategy_effectiveness: strategy_effectiveness
}
end
|