Module: Legion::Extensions::Agentic::Affect::Defusion::Runners::CognitiveDefusion

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

Instance Method Summary collapse

Instance Method Details

#apply_all_techniques(thought_id:, engine: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/legion/extensions/agentic/affect/defusion/runners/cognitive_defusion.rb', line 33

def apply_all_techniques(thought_id:, engine: nil, **)
  eng    = engine || defusion_engine
  result = eng.apply_all_techniques(thought_id: thought_id)
  return { success: false, **result } if result[:error]

  log.debug("[cognitive_defusion] all techniques applied thought_id=#{thought_id} " \
            "final_fusion=#{result[:final_fusion]&.round(4)}")
  { success: true, **result }
end

#apply_defusion(thought_id:, technique:, engine: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/legion/extensions/agentic/affect/defusion/runners/cognitive_defusion.rb', line 23

def apply_defusion(thought_id:, technique:, engine: nil, **)
  eng    = engine || defusion_engine
  result = eng.apply_defusion(thought_id: thought_id, technique: technique)
  return { success: false, **result } if result[:error]

  log.debug("[cognitive_defusion] defusion applied technique=#{technique} " \
            "reduction=#{result[:reduction]&.round(4)} fusion=#{result[:after]&.round(4)}")
  result
end

#defused_thoughts(engine: nil) ⇒ Object



71
72
73
74
75
76
# File 'lib/legion/extensions/agentic/affect/defusion/runners/cognitive_defusion.rb', line 71

def defused_thoughts(engine: nil, **)
  eng      = engine || defusion_engine
  thoughts = eng.defused_thoughts.map(&:to_h)
  log.debug("[cognitive_defusion] defused thoughts count=#{thoughts.size}")
  { success: true, count: thoughts.size, thoughts: thoughts }
end

#defusion_report(engine: nil) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/legion/extensions/agentic/affect/defusion/runners/cognitive_defusion.rb', line 102

def defusion_report(engine: nil, **)
  eng    = engine || defusion_engine
  report = eng.defusion_report
  log.debug("[cognitive_defusion] report: total=#{report[:total_thoughts]} " \
            "enmeshed=#{report[:enmeshed_count]} avg_fusion=#{report[:average_fusion].round(4)}")
  { success: true, **report }
end

#defusion_state(engine: nil) ⇒ Object



110
111
112
113
114
# File 'lib/legion/extensions/agentic/affect/defusion/runners/cognitive_defusion.rb', line 110

def defusion_state(engine: nil, **)
  eng = engine || defusion_engine
  log.debug('[cognitive_defusion] state queried')
  { success: true, **eng.to_h }
end

#enmeshed_thoughts(engine: nil) ⇒ Object



64
65
66
67
68
69
# File 'lib/legion/extensions/agentic/affect/defusion/runners/cognitive_defusion.rb', line 64

def enmeshed_thoughts(engine: nil, **)
  eng      = engine || defusion_engine
  thoughts = eng.enmeshed_thoughts.map(&:to_h)
  log.debug("[cognitive_defusion] enmeshed thoughts count=#{thoughts.size}")
  { success: true, count: thoughts.size, thoughts: thoughts }
end

#fuse_thought(thought_id:, engine: nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/legion/extensions/agentic/affect/defusion/runners/cognitive_defusion.rb', line 53

def fuse_thought(thought_id:, engine: nil, **)
  eng = engine || defusion_engine
  thought = eng.thoughts[thought_id]
  return { success: false, error: :thought_not_found } unless thought

  result = thought.fuse!
  log.debug("[cognitive_defusion] thought fused before=#{result[:before].round(4)} " \
            "after=#{result[:after].round(4)}")
  { success: true, thought_id: thought_id, before: result[:before], after: result[:after], enmeshed: thought.enmeshed? }
end

#most_fused(limit: 5, engine: nil) ⇒ Object



85
86
87
88
89
90
# File 'lib/legion/extensions/agentic/affect/defusion/runners/cognitive_defusion.rb', line 85

def most_fused(limit: 5, engine: nil, **)
  eng      = engine || defusion_engine
  thoughts = eng.most_fused(limit: limit).map(&:to_h)
  log.debug("[cognitive_defusion] most fused count=#{thoughts.size}")
  { success: true, count: thoughts.size, thoughts: thoughts }
end

#recommend_technique(thought_id:, engine: nil) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/legion/extensions/agentic/affect/defusion/runners/cognitive_defusion.rb', line 92

def recommend_technique(thought_id:, engine: nil, **)
  eng    = engine || defusion_engine
  result = eng.recommend_technique(thought_id: thought_id)
  return { success: false, **result } if result[:error]

  log.debug("[cognitive_defusion] technique recommended=#{result[:technique]} " \
            "for type=#{result[:thought_type]}")
  { success: true, **result }
end

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



13
14
15
16
17
18
19
20
21
# File 'lib/legion/extensions/agentic/affect/defusion/runners/cognitive_defusion.rb', line 13

def register_thought(content:, thought_type:, belief_strength: 0.5, engine: nil, **)
  eng    = engine || defusion_engine
  result = eng.register_thought(content: content, thought_type: thought_type, belief_strength: belief_strength)
  return { success: false, **result } if result[:error]

  log.debug("[cognitive_defusion] registered thought type=#{thought_type} " \
            "belief=#{belief_strength.round(2)} id=#{result[:thought_id]}")
  { success: true, **result }
end

#ruminating_thoughts(engine: nil) ⇒ Object



78
79
80
81
82
83
# File 'lib/legion/extensions/agentic/affect/defusion/runners/cognitive_defusion.rb', line 78

def ruminating_thoughts(engine: nil, **)
  eng      = engine || defusion_engine
  thoughts = eng.ruminating_thoughts.map(&:to_h)
  log.debug("[cognitive_defusion] ruminating thoughts count=#{thoughts.size}")
  { success: true, count: thoughts.size, thoughts: thoughts }
end

#visit_thought(thought_id:, engine: nil) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/legion/extensions/agentic/affect/defusion/runners/cognitive_defusion.rb', line 43

def visit_thought(thought_id:, engine: nil, **)
  eng    = engine || defusion_engine
  result = eng.visit_thought(thought_id: thought_id)
  return { success: false, **result } if result[:error]

  log.debug("[cognitive_defusion] thought visited count=#{result[:visit_count]} " \
            "ruminating=#{result[:ruminating]}")
  { success: true, **result }
end