Class: Legion::Extensions::Agentic::Affect::Defusion::Helpers::DefusionEngine

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb

Constant Summary

Constants included from Constants

Constants::BELIEF_LABELS, Constants::DEFAULT_FUSION, Constants::DEFUSED_THRESHOLD, Constants::DEFUSION_TECHNIQUES, Constants::FUSION_DELTA_FUSE, Constants::FUSION_DELTA_VISIT, Constants::FUSION_LABELS, Constants::FUSION_THRESHOLD, Constants::MAX_THOUGHTS, Constants::RECOMMENDED_TECHNIQUES, Constants::RUMINATION_COUNT, Constants::TECHNIQUE_POTENCY, Constants::THOUGHT_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefusionEngine

Returns a new instance of DefusionEngine.



14
15
16
17
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 14

def initialize
  @thoughts         = {}
  @defusion_history = []
end

Instance Attribute Details

#defusion_historyObject (readonly)

Returns the value of attribute defusion_history.



12
13
14
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 12

def defusion_history
  @defusion_history
end

#thoughtsObject (readonly)

Returns the value of attribute thoughts.



12
13
14
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 12

def thoughts
  @thoughts
end

Instance Method Details

#apply_all_techniques(thought_id:) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 60

def apply_all_techniques(thought_id:)
  thought = @thoughts[thought_id]
  return { error: :thought_not_found } unless thought

  results = DEFUSION_TECHNIQUES.map do |technique|
    apply_defusion(thought_id: thought_id, technique: technique)
  end

  {
    thought_id:    thought_id,
    techniques:    results,
    final_fusion:  thought.fusion_level,
    fusion_label:  thought.fusion_label,
    defused:       thought.defused?,
    total_applied: results.size
  }
end

#apply_defusion(thought_id:, technique:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 32

def apply_defusion(thought_id:, technique:)
  thought = @thoughts[thought_id]
  return { error: :thought_not_found } unless thought
  return { error: :invalid_technique, valid: DEFUSION_TECHNIQUES } unless DEFUSION_TECHNIQUES.include?(technique)

  before_fusion = thought.fusion_level
  result = thought.defuse!(technique: technique)

  @defusion_history << {
    thought_id:    thought_id,
    technique:     technique,
    before_fusion: before_fusion,
    after_fusion:  thought.fusion_level,
    at:            Time.now.utc
  }

  {
    success:      true,
    thought_id:   thought_id,
    technique:    technique,
    before:       result[:before],
    after:        result[:after],
    reduction:    result[:reduction],
    fusion_label: thought.fusion_label,
    defused:      thought.defused?
  }
end

#average_fusionObject



126
127
128
129
130
131
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 126

def average_fusion
  return 0.0 if @thoughts.empty?

  total = @thoughts.values.sum(&:fusion_level)
  (total / @thoughts.size).round(10)
end

#defused_thoughtsObject



95
96
97
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 95

def defused_thoughts
  @thoughts.values.select(&:defused?)
end

#defusion_effectivenessObject



133
134
135
136
137
138
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 133

def defusion_effectiveness
  return 0.0 if @defusion_history.empty?

  total_reduction = @defusion_history.sum { |h| h[:before_fusion] - h[:after_fusion] }
  (total_reduction / @defusion_history.size).round(10)
end

#defusion_reportObject



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 140

def defusion_report
  all = @thoughts.values
  {
    total_thoughts:         all.size,
    enmeshed_count:         all.count(&:enmeshed?),
    defused_count:          all.count(&:defused?),
    ruminating_count:       all.count(&:ruminating?),
    average_fusion:         average_fusion,
    defusion_attempts:      @defusion_history.size,
    defusion_effectiveness: defusion_effectiveness,
    most_fused:             most_fused(limit: 3).map(&:to_h)
  }
end

#enmeshed_thoughtsObject



91
92
93
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 91

def enmeshed_thoughts
  @thoughts.values.select(&:enmeshed?)
end

#most_fused(limit: 5) ⇒ Object



103
104
105
106
107
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 103

def most_fused(limit: 5)
  @thoughts.values
           .sort_by { |t| -t.fusion_level }
           .first(limit)
end

#recommend_technique(thought_id:) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 109

def recommend_technique(thought_id:)
  thought = @thoughts[thought_id]
  return { error: :thought_not_found } unless thought

  technique = RECOMMENDED_TECHNIQUES.fetch(thought.thought_type, :acceptance)
  potency   = TECHNIQUE_POTENCY[technique]

  {
    thought_id:       thought_id,
    thought_type:     thought.thought_type,
    technique:        technique,
    potency:          potency,
    current_fusion:   thought.fusion_level,
    projected_fusion: (thought.fusion_level - potency).clamp(0.0, 1.0).round(10)
  }
end

#register_thought(content:, thought_type:, belief_strength: 0.5) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 19

def register_thought(content:, thought_type:, belief_strength: 0.5)
  return { error: :invalid_thought_type, valid: THOUGHT_TYPES } unless THOUGHT_TYPES.include?(thought_type)

  if @thoughts.size >= MAX_THOUGHTS
    oldest = @thoughts.values.min_by(&:created_at)
    @thoughts.delete(oldest.id) if oldest
  end

  thought = Thought.new(content: content, thought_type: thought_type, belief_strength: belief_strength)
  @thoughts[thought.id] = thought
  { thought_id: thought.id, thought: thought.to_h }
end

#ruminating_thoughtsObject



99
100
101
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 99

def ruminating_thoughts
  @thoughts.values.select(&:ruminating?)
end

#to_hObject



154
155
156
157
158
159
160
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 154

def to_h
  {
    thoughts:         @thoughts.transform_values(&:to_h),
    defusion_history: @defusion_history,
    report:           defusion_report
  }
end

#visit_thought(thought_id:) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/legion/extensions/agentic/affect/defusion/helpers/defusion_engine.rb', line 78

def visit_thought(thought_id:)
  thought = @thoughts[thought_id]
  return { error: :thought_not_found } unless thought

  result = thought.visit!
  {
    thought_id:  thought_id,
    visit_count: result[:visit_count],
    fusion:      thought.fusion_level,
    ruminating:  thought.ruminating?
  }
end