lex-cognitive-defusion 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +15 -0
- data/LICENSE +21 -0
- data/README.md +43 -0
- data/lex-cognitive-defusion.gemspec +30 -0
- data/lib/legion/extensions/cognitive_defusion/client.rb +24 -0
- data/lib/legion/extensions/cognitive_defusion/helpers/constants.rb +60 -0
- data/lib/legion/extensions/cognitive_defusion/helpers/defusion_engine.rb +163 -0
- data/lib/legion/extensions/cognitive_defusion/helpers/thought.rb +88 -0
- data/lib/legion/extensions/cognitive_defusion/runners/cognitive_defusion.rb +123 -0
- data/lib/legion/extensions/cognitive_defusion/version.rb +9 -0
- data/lib/legion/extensions/cognitive_defusion.rb +16 -0
- data/spec/legion/extensions/cognitive_defusion/client_spec.rb +80 -0
- data/spec/legion/extensions/cognitive_defusion/helpers/constants_spec.rb +84 -0
- data/spec/legion/extensions/cognitive_defusion/helpers/defusion_engine_spec.rb +250 -0
- data/spec/legion/extensions/cognitive_defusion/helpers/thought_spec.rb +178 -0
- data/spec/legion/extensions/cognitive_defusion/runners/cognitive_defusion_spec.rb +185 -0
- data/spec/spec_helper.rb +24 -0
- metadata +78 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 5299401a100267f302a905877b344a026e0b0efeabcc342191cdf136b683cad3
|
|
4
|
+
data.tar.gz: 5835e3f7c4ddaa10727973f0db95bfc157912df2587d38cb40616e4a7abe3068
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b49626a6477d240889916c43e694cd16fa41842797e699a195bd7879f07378758005a6e1622d9716d2c259ffd7942d68343563062b9086e9beb13f5e2fd7318e
|
|
7
|
+
data.tar.gz: a60b6b8f3458a9981e836af5015caec61c8a749a830d2d27846eaaa5f84ae5a9387f3b718e707d3e095c7d7ef0fd198c27f9e87841e748bbea8ea0d97167bb2f
|
data/Gemfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
source 'https://rubygems.org'
|
|
4
|
+
gemspec
|
|
5
|
+
|
|
6
|
+
group :test do
|
|
7
|
+
gem 'rake'
|
|
8
|
+
gem 'rspec', '~> 3.13'
|
|
9
|
+
gem 'rspec_junit_formatter'
|
|
10
|
+
gem 'rubocop', '~> 1.75', require: false
|
|
11
|
+
gem 'rubocop-rspec', require: false
|
|
12
|
+
gem 'simplecov'
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
gem 'legion-gaia', path: '../../legion-gaia'
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matthew Iverson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# lex-cognitive-defusion
|
|
2
|
+
|
|
3
|
+
Cognitive defusion (ACT) for LegionIO. Enables the agent to step back from thoughts and observe them as mental events rather than literal truths.
|
|
4
|
+
|
|
5
|
+
## What It Does
|
|
6
|
+
|
|
7
|
+
Fusion is when a thought takes on the weight of absolute truth — the agent acts as if the belief, assumption, or judgment is reality rather than a cognitive event. Defusion creates distance between the agent and its thoughts, reducing the behavioral influence of entrenched beliefs. Six techniques are available, each with different potency: acceptance (strongest), distancing, contextualization, reframing, labeling, and metaphor.
|
|
8
|
+
|
|
9
|
+
Thoughts that are visited repeatedly without defusion accumulate as rumination; the extension detects this state and recommends type-appropriate techniques.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
client = Legion::Extensions::CognitiveDefusion::Client.new
|
|
15
|
+
|
|
16
|
+
thought = client.register_thought(
|
|
17
|
+
content: 'I must always provide a complete answer or I have failed',
|
|
18
|
+
thought_type: :rule,
|
|
19
|
+
belief_strength: 0.8
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
# Recommended technique for :rule type is :contextualization
|
|
23
|
+
rec = client.recommend_technique(thought_id: thought[:thought_id])
|
|
24
|
+
client.apply_defusion(thought_id: thought[:thought_id], technique: rec[:technique])
|
|
25
|
+
|
|
26
|
+
# Or apply all techniques in sequence
|
|
27
|
+
client.apply_all_techniques(thought_id: thought[:thought_id])
|
|
28
|
+
|
|
29
|
+
client.defusion_report
|
|
30
|
+
# => { total_thoughts: 1, enmeshed_count: 0, average_fusion: 0.31 }
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Development
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
bundle install
|
|
37
|
+
bundle exec rspec
|
|
38
|
+
bundle exec rubocop
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## License
|
|
42
|
+
|
|
43
|
+
MIT
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'lib/legion/extensions/cognitive_defusion/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = 'lex-cognitive-defusion'
|
|
7
|
+
spec.version = Legion::Extensions::CognitiveDefusion::VERSION
|
|
8
|
+
spec.authors = ['Esity']
|
|
9
|
+
spec.email = ['matthewdiverson@gmail.com']
|
|
10
|
+
|
|
11
|
+
spec.summary = 'LEX Cognitive Defusion'
|
|
12
|
+
spec.description = 'Cognitive defusion (ACT) for brain-modeled agentic AI — ' \
|
|
13
|
+
'step back from thoughts and observe them as mental events rather than literal truths'
|
|
14
|
+
spec.homepage = 'https://github.com/LegionIO/lex-cognitive-defusion'
|
|
15
|
+
spec.license = 'MIT'
|
|
16
|
+
spec.required_ruby_version = '>= 3.4'
|
|
17
|
+
|
|
18
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
19
|
+
spec.metadata['source_code_uri'] = 'https://github.com/LegionIO/lex-cognitive-defusion'
|
|
20
|
+
spec.metadata['documentation_uri'] = 'https://github.com/LegionIO/lex-cognitive-defusion'
|
|
21
|
+
spec.metadata['changelog_uri'] = 'https://github.com/LegionIO/lex-cognitive-defusion'
|
|
22
|
+
spec.metadata['bug_tracker_uri'] = 'https://github.com/LegionIO/lex-cognitive-defusion/issues'
|
|
23
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
|
24
|
+
|
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
26
|
+
Dir.glob('{lib,spec}/**/*') + %w[lex-cognitive-defusion.gemspec Gemfile LICENSE README.md]
|
|
27
|
+
end
|
|
28
|
+
spec.require_paths = ['lib']
|
|
29
|
+
spec.add_development_dependency 'legion-gaia'
|
|
30
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/cognitive_defusion/helpers/constants'
|
|
4
|
+
require 'legion/extensions/cognitive_defusion/helpers/thought'
|
|
5
|
+
require 'legion/extensions/cognitive_defusion/helpers/defusion_engine'
|
|
6
|
+
require 'legion/extensions/cognitive_defusion/runners/cognitive_defusion'
|
|
7
|
+
|
|
8
|
+
module Legion
|
|
9
|
+
module Extensions
|
|
10
|
+
module CognitiveDefusion
|
|
11
|
+
class Client
|
|
12
|
+
include Runners::CognitiveDefusion
|
|
13
|
+
|
|
14
|
+
def initialize(engine: nil)
|
|
15
|
+
@defusion_engine = engine || Helpers::DefusionEngine.new
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
attr_reader :defusion_engine
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module CognitiveDefusion
|
|
6
|
+
module Helpers
|
|
7
|
+
module Constants
|
|
8
|
+
MAX_THOUGHTS = 300
|
|
9
|
+
DEFAULT_FUSION = 0.7
|
|
10
|
+
FUSION_DELTA_FUSE = 0.05
|
|
11
|
+
FUSION_DELTA_VISIT = 0.02
|
|
12
|
+
|
|
13
|
+
DEFUSION_TECHNIQUES = %i[labeling distancing contextualization acceptance reframing metaphor].freeze
|
|
14
|
+
|
|
15
|
+
TECHNIQUE_POTENCY = {
|
|
16
|
+
labeling: 0.08,
|
|
17
|
+
distancing: 0.12,
|
|
18
|
+
contextualization: 0.10,
|
|
19
|
+
acceptance: 0.15,
|
|
20
|
+
reframing: 0.10,
|
|
21
|
+
metaphor: 0.06
|
|
22
|
+
}.freeze
|
|
23
|
+
|
|
24
|
+
FUSION_LABELS = {
|
|
25
|
+
(0.8..) => :enmeshed,
|
|
26
|
+
(0.6...0.8) => :fused,
|
|
27
|
+
(0.4...0.6) => :partially_fused,
|
|
28
|
+
(0.2...0.4) => :defused,
|
|
29
|
+
(..0.2) => :fully_defused
|
|
30
|
+
}.freeze
|
|
31
|
+
|
|
32
|
+
BELIEF_LABELS = {
|
|
33
|
+
(0.8..) => :entrenched,
|
|
34
|
+
(0.6...0.8) => :strong,
|
|
35
|
+
(0.4...0.6) => :moderate,
|
|
36
|
+
(0.2...0.4) => :weak,
|
|
37
|
+
(..0.2) => :negligible
|
|
38
|
+
}.freeze
|
|
39
|
+
|
|
40
|
+
FUSION_THRESHOLD = 0.7
|
|
41
|
+
DEFUSED_THRESHOLD = 0.3
|
|
42
|
+
RUMINATION_COUNT = 3
|
|
43
|
+
|
|
44
|
+
THOUGHT_TYPES = %i[belief assumption evaluation prediction judgment self_concept rule].freeze
|
|
45
|
+
|
|
46
|
+
# Technique recommendations by thought type
|
|
47
|
+
RECOMMENDED_TECHNIQUES = {
|
|
48
|
+
belief: :acceptance,
|
|
49
|
+
assumption: :contextualization,
|
|
50
|
+
evaluation: :distancing,
|
|
51
|
+
prediction: :reframing,
|
|
52
|
+
judgment: :labeling,
|
|
53
|
+
self_concept: :distancing,
|
|
54
|
+
rule: :contextualization
|
|
55
|
+
}.freeze
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module CognitiveDefusion
|
|
6
|
+
module Helpers
|
|
7
|
+
class DefusionEngine
|
|
8
|
+
include Constants
|
|
9
|
+
|
|
10
|
+
attr_reader :thoughts, :defusion_history
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
@thoughts = {}
|
|
14
|
+
@defusion_history = []
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def register_thought(content:, thought_type:, belief_strength: 0.5)
|
|
18
|
+
return { error: :invalid_thought_type, valid: THOUGHT_TYPES } unless THOUGHT_TYPES.include?(thought_type)
|
|
19
|
+
|
|
20
|
+
if @thoughts.size >= MAX_THOUGHTS
|
|
21
|
+
oldest = @thoughts.values.min_by(&:created_at)
|
|
22
|
+
@thoughts.delete(oldest.id) if oldest
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
thought = Thought.new(content: content, thought_type: thought_type, belief_strength: belief_strength)
|
|
26
|
+
@thoughts[thought.id] = thought
|
|
27
|
+
{ thought_id: thought.id, thought: thought.to_h }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def apply_defusion(thought_id:, technique:)
|
|
31
|
+
thought = @thoughts[thought_id]
|
|
32
|
+
return { error: :thought_not_found } unless thought
|
|
33
|
+
return { error: :invalid_technique, valid: DEFUSION_TECHNIQUES } unless DEFUSION_TECHNIQUES.include?(technique)
|
|
34
|
+
|
|
35
|
+
before_fusion = thought.fusion_level
|
|
36
|
+
result = thought.defuse!(technique: technique)
|
|
37
|
+
|
|
38
|
+
@defusion_history << {
|
|
39
|
+
thought_id: thought_id,
|
|
40
|
+
technique: technique,
|
|
41
|
+
before_fusion: before_fusion,
|
|
42
|
+
after_fusion: thought.fusion_level,
|
|
43
|
+
at: Time.now.utc
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
{
|
|
47
|
+
success: true,
|
|
48
|
+
thought_id: thought_id,
|
|
49
|
+
technique: technique,
|
|
50
|
+
before: result[:before],
|
|
51
|
+
after: result[:after],
|
|
52
|
+
reduction: result[:reduction],
|
|
53
|
+
fusion_label: thought.fusion_label,
|
|
54
|
+
defused: thought.defused?
|
|
55
|
+
}
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def apply_all_techniques(thought_id:)
|
|
59
|
+
thought = @thoughts[thought_id]
|
|
60
|
+
return { error: :thought_not_found } unless thought
|
|
61
|
+
|
|
62
|
+
results = DEFUSION_TECHNIQUES.map do |technique|
|
|
63
|
+
apply_defusion(thought_id: thought_id, technique: technique)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
{
|
|
67
|
+
thought_id: thought_id,
|
|
68
|
+
techniques: results,
|
|
69
|
+
final_fusion: thought.fusion_level,
|
|
70
|
+
fusion_label: thought.fusion_label,
|
|
71
|
+
defused: thought.defused?,
|
|
72
|
+
total_applied: results.size
|
|
73
|
+
}
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def visit_thought(thought_id:)
|
|
77
|
+
thought = @thoughts[thought_id]
|
|
78
|
+
return { error: :thought_not_found } unless thought
|
|
79
|
+
|
|
80
|
+
result = thought.visit!
|
|
81
|
+
{
|
|
82
|
+
thought_id: thought_id,
|
|
83
|
+
visit_count: result[:visit_count],
|
|
84
|
+
fusion: thought.fusion_level,
|
|
85
|
+
ruminating: thought.ruminating?
|
|
86
|
+
}
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def enmeshed_thoughts
|
|
90
|
+
@thoughts.values.select(&:enmeshed?)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def defused_thoughts
|
|
94
|
+
@thoughts.values.select(&:defused?)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def ruminating_thoughts
|
|
98
|
+
@thoughts.values.select(&:ruminating?)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def most_fused(limit: 5)
|
|
102
|
+
@thoughts.values
|
|
103
|
+
.sort_by { |t| -t.fusion_level }
|
|
104
|
+
.first(limit)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def recommend_technique(thought_id:)
|
|
108
|
+
thought = @thoughts[thought_id]
|
|
109
|
+
return { error: :thought_not_found } unless thought
|
|
110
|
+
|
|
111
|
+
technique = RECOMMENDED_TECHNIQUES.fetch(thought.thought_type, :acceptance)
|
|
112
|
+
potency = TECHNIQUE_POTENCY[technique]
|
|
113
|
+
|
|
114
|
+
{
|
|
115
|
+
thought_id: thought_id,
|
|
116
|
+
thought_type: thought.thought_type,
|
|
117
|
+
technique: technique,
|
|
118
|
+
potency: potency,
|
|
119
|
+
current_fusion: thought.fusion_level,
|
|
120
|
+
projected_fusion: (thought.fusion_level - potency).clamp(0.0, 1.0).round(10)
|
|
121
|
+
}
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def average_fusion
|
|
125
|
+
return 0.0 if @thoughts.empty?
|
|
126
|
+
|
|
127
|
+
total = @thoughts.values.sum(&:fusion_level)
|
|
128
|
+
(total / @thoughts.size).round(10)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def defusion_effectiveness
|
|
132
|
+
return 0.0 if @defusion_history.empty?
|
|
133
|
+
|
|
134
|
+
total_reduction = @defusion_history.sum { |h| h[:before_fusion] - h[:after_fusion] }
|
|
135
|
+
(total_reduction / @defusion_history.size).round(10)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def defusion_report
|
|
139
|
+
all = @thoughts.values
|
|
140
|
+
{
|
|
141
|
+
total_thoughts: all.size,
|
|
142
|
+
enmeshed_count: all.count(&:enmeshed?),
|
|
143
|
+
defused_count: all.count(&:defused?),
|
|
144
|
+
ruminating_count: all.count(&:ruminating?),
|
|
145
|
+
average_fusion: average_fusion,
|
|
146
|
+
defusion_attempts: @defusion_history.size,
|
|
147
|
+
defusion_effectiveness: defusion_effectiveness,
|
|
148
|
+
most_fused: most_fused(limit: 3).map(&:to_h)
|
|
149
|
+
}
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def to_h
|
|
153
|
+
{
|
|
154
|
+
thoughts: @thoughts.transform_values(&:to_h),
|
|
155
|
+
defusion_history: @defusion_history,
|
|
156
|
+
report: defusion_report
|
|
157
|
+
}
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module CognitiveDefusion
|
|
8
|
+
module Helpers
|
|
9
|
+
class Thought
|
|
10
|
+
include Constants
|
|
11
|
+
|
|
12
|
+
attr_reader :id, :content, :thought_type, :belief_strength,
|
|
13
|
+
:fusion_level, :defusion_count, :visit_count, :created_at
|
|
14
|
+
|
|
15
|
+
def initialize(content:, thought_type:, belief_strength:)
|
|
16
|
+
@id = SecureRandom.uuid
|
|
17
|
+
@content = content
|
|
18
|
+
@thought_type = thought_type
|
|
19
|
+
@belief_strength = belief_strength.clamp(0.0, 1.0)
|
|
20
|
+
@fusion_level = DEFAULT_FUSION
|
|
21
|
+
@defusion_count = 0
|
|
22
|
+
@visit_count = 0
|
|
23
|
+
@created_at = Time.now.utc
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def defuse!(technique:)
|
|
27
|
+
potency = TECHNIQUE_POTENCY.fetch(technique, 0.0)
|
|
28
|
+
before = @fusion_level
|
|
29
|
+
@fusion_level = (@fusion_level - potency).clamp(0.0, 1.0).round(10)
|
|
30
|
+
@defusion_count += 1
|
|
31
|
+
{ before: before, after: @fusion_level, technique: technique, reduction: (before - @fusion_level).round(10) }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def fuse!
|
|
35
|
+
before = @fusion_level
|
|
36
|
+
@fusion_level = (@fusion_level + FUSION_DELTA_FUSE).clamp(0.0, 1.0).round(10)
|
|
37
|
+
{ before: before, after: @fusion_level }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def visit!
|
|
41
|
+
@visit_count += 1
|
|
42
|
+
before = @fusion_level
|
|
43
|
+
@fusion_level = (@fusion_level + FUSION_DELTA_VISIT).clamp(0.0, 1.0).round(10)
|
|
44
|
+
{ visit_count: @visit_count, fusion_before: before, fusion_after: @fusion_level }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def enmeshed?
|
|
48
|
+
@fusion_level >= FUSION_THRESHOLD
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def defused?
|
|
52
|
+
@fusion_level <= DEFUSED_THRESHOLD
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def ruminating?
|
|
56
|
+
@visit_count >= RUMINATION_COUNT
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def fusion_label
|
|
60
|
+
FUSION_LABELS.find { |range, _| range.cover?(@fusion_level) }&.last || :unknown
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def belief_label
|
|
64
|
+
BELIEF_LABELS.find { |range, _| range.cover?(@belief_strength) }&.last || :unknown
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def to_h
|
|
68
|
+
{
|
|
69
|
+
id: @id,
|
|
70
|
+
content: @content,
|
|
71
|
+
thought_type: @thought_type,
|
|
72
|
+
belief_strength: @belief_strength,
|
|
73
|
+
fusion_level: @fusion_level,
|
|
74
|
+
defusion_count: @defusion_count,
|
|
75
|
+
visit_count: @visit_count,
|
|
76
|
+
fusion_label: fusion_label,
|
|
77
|
+
belief_label: belief_label,
|
|
78
|
+
enmeshed: enmeshed?,
|
|
79
|
+
defused: defused?,
|
|
80
|
+
ruminating: ruminating?,
|
|
81
|
+
created_at: @created_at
|
|
82
|
+
}
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module CognitiveDefusion
|
|
6
|
+
module Runners
|
|
7
|
+
module CognitiveDefusion
|
|
8
|
+
include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
|
|
9
|
+
Legion::Extensions::Helpers.const_defined?(:Lex)
|
|
10
|
+
|
|
11
|
+
def register_thought(content:, thought_type:, belief_strength: 0.5, engine: nil, **)
|
|
12
|
+
eng = engine || defusion_engine
|
|
13
|
+
result = eng.register_thought(content: content, thought_type: thought_type, belief_strength: belief_strength)
|
|
14
|
+
return { success: false, **result } if result[:error]
|
|
15
|
+
|
|
16
|
+
Legion::Logging.debug "[cognitive_defusion] registered thought type=#{thought_type} " \
|
|
17
|
+
"belief=#{belief_strength.round(2)} id=#{result[:thought_id]}"
|
|
18
|
+
{ success: true, **result }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def apply_defusion(thought_id:, technique:, engine: nil, **)
|
|
22
|
+
eng = engine || defusion_engine
|
|
23
|
+
result = eng.apply_defusion(thought_id: thought_id, technique: technique)
|
|
24
|
+
return { success: false, **result } if result[:error]
|
|
25
|
+
|
|
26
|
+
Legion::Logging.debug "[cognitive_defusion] defusion applied technique=#{technique} " \
|
|
27
|
+
"reduction=#{result[:reduction]&.round(4)} fusion=#{result[:after]&.round(4)}"
|
|
28
|
+
result
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def apply_all_techniques(thought_id:, engine: nil, **)
|
|
32
|
+
eng = engine || defusion_engine
|
|
33
|
+
result = eng.apply_all_techniques(thought_id: thought_id)
|
|
34
|
+
return { success: false, **result } if result[:error]
|
|
35
|
+
|
|
36
|
+
Legion::Logging.debug "[cognitive_defusion] all techniques applied thought_id=#{thought_id} " \
|
|
37
|
+
"final_fusion=#{result[:final_fusion]&.round(4)}"
|
|
38
|
+
{ success: true, **result }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def visit_thought(thought_id:, engine: nil, **)
|
|
42
|
+
eng = engine || defusion_engine
|
|
43
|
+
result = eng.visit_thought(thought_id: thought_id)
|
|
44
|
+
return { success: false, **result } if result[:error]
|
|
45
|
+
|
|
46
|
+
Legion::Logging.debug "[cognitive_defusion] thought visited count=#{result[:visit_count]} " \
|
|
47
|
+
"ruminating=#{result[:ruminating]}"
|
|
48
|
+
{ success: true, **result }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def fuse_thought(thought_id:, engine: nil, **)
|
|
52
|
+
eng = engine || defusion_engine
|
|
53
|
+
thought = eng.thoughts[thought_id]
|
|
54
|
+
return { success: false, error: :thought_not_found } unless thought
|
|
55
|
+
|
|
56
|
+
result = thought.fuse!
|
|
57
|
+
Legion::Logging.debug "[cognitive_defusion] thought fused before=#{result[:before].round(4)} " \
|
|
58
|
+
"after=#{result[:after].round(4)}"
|
|
59
|
+
{ success: true, thought_id: thought_id, before: result[:before], after: result[:after], enmeshed: thought.enmeshed? }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def enmeshed_thoughts(engine: nil, **)
|
|
63
|
+
eng = engine || defusion_engine
|
|
64
|
+
thoughts = eng.enmeshed_thoughts.map(&:to_h)
|
|
65
|
+
Legion::Logging.debug "[cognitive_defusion] enmeshed thoughts count=#{thoughts.size}"
|
|
66
|
+
{ success: true, count: thoughts.size, thoughts: thoughts }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def defused_thoughts(engine: nil, **)
|
|
70
|
+
eng = engine || defusion_engine
|
|
71
|
+
thoughts = eng.defused_thoughts.map(&:to_h)
|
|
72
|
+
Legion::Logging.debug "[cognitive_defusion] defused thoughts count=#{thoughts.size}"
|
|
73
|
+
{ success: true, count: thoughts.size, thoughts: thoughts }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def ruminating_thoughts(engine: nil, **)
|
|
77
|
+
eng = engine || defusion_engine
|
|
78
|
+
thoughts = eng.ruminating_thoughts.map(&:to_h)
|
|
79
|
+
Legion::Logging.debug "[cognitive_defusion] ruminating thoughts count=#{thoughts.size}"
|
|
80
|
+
{ success: true, count: thoughts.size, thoughts: thoughts }
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def most_fused(limit: 5, engine: nil, **)
|
|
84
|
+
eng = engine || defusion_engine
|
|
85
|
+
thoughts = eng.most_fused(limit: limit).map(&:to_h)
|
|
86
|
+
Legion::Logging.debug "[cognitive_defusion] most fused count=#{thoughts.size}"
|
|
87
|
+
{ success: true, count: thoughts.size, thoughts: thoughts }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def recommend_technique(thought_id:, engine: nil, **)
|
|
91
|
+
eng = engine || defusion_engine
|
|
92
|
+
result = eng.recommend_technique(thought_id: thought_id)
|
|
93
|
+
return { success: false, **result } if result[:error]
|
|
94
|
+
|
|
95
|
+
Legion::Logging.debug "[cognitive_defusion] technique recommended=#{result[:technique]} " \
|
|
96
|
+
"for type=#{result[:thought_type]}"
|
|
97
|
+
{ success: true, **result }
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def defusion_report(engine: nil, **)
|
|
101
|
+
eng = engine || defusion_engine
|
|
102
|
+
report = eng.defusion_report
|
|
103
|
+
Legion::Logging.debug "[cognitive_defusion] report: total=#{report[:total_thoughts]} " \
|
|
104
|
+
"enmeshed=#{report[:enmeshed_count]} avg_fusion=#{report[:average_fusion].round(4)}"
|
|
105
|
+
{ success: true, **report }
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def defusion_state(engine: nil, **)
|
|
109
|
+
eng = engine || defusion_engine
|
|
110
|
+
Legion::Logging.debug '[cognitive_defusion] state queried'
|
|
111
|
+
{ success: true, **eng.to_h }
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
private
|
|
115
|
+
|
|
116
|
+
def defusion_engine
|
|
117
|
+
@defusion_engine ||= Helpers::DefusionEngine.new
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/cognitive_defusion/version'
|
|
4
|
+
require 'legion/extensions/cognitive_defusion/helpers/constants'
|
|
5
|
+
require 'legion/extensions/cognitive_defusion/helpers/thought'
|
|
6
|
+
require 'legion/extensions/cognitive_defusion/helpers/defusion_engine'
|
|
7
|
+
require 'legion/extensions/cognitive_defusion/runners/cognitive_defusion'
|
|
8
|
+
require 'legion/extensions/cognitive_defusion/client'
|
|
9
|
+
|
|
10
|
+
module Legion
|
|
11
|
+
module Extensions
|
|
12
|
+
module CognitiveDefusion
|
|
13
|
+
extend Legion::Extensions::Core if Legion::Extensions.const_defined? :Core
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|