lex-agentic-inference 0.1.1 → 0.1.3
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 +4 -4
- data/CHANGELOG.md +13 -1
- data/LICENSE +201 -21
- data/README.md +2 -2
- data/lex-agentic-inference.gemspec +8 -0
- data/lib/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning.rb +19 -19
- data/lib/legion/extensions/agentic/inference/affordance/runners/affordance.rb +6 -6
- data/lib/legion/extensions/agentic/inference/analogical/runners/analogical_reasoning.rb +17 -14
- data/lib/legion/extensions/agentic/inference/argument_mapping/runners/argument_mapping.rb +10 -10
- data/lib/legion/extensions/agentic/inference/bayesian/runners/bayesian_belief.rb +14 -14
- data/lib/legion/extensions/agentic/inference/causal_attribution/runners/causal_attribution.rb +13 -13
- data/lib/legion/extensions/agentic/inference/causal_reasoning/runners/causal_reasoning.rb +14 -14
- data/lib/legion/extensions/agentic/inference/coherence/runners/cognitive_coherence.rb +16 -16
- data/lib/legion/extensions/agentic/inference/counterfactual/runners/counterfactual.rb +11 -11
- data/lib/legion/extensions/agentic/inference/debugging/runners/cognitive_debugging.rb +29 -29
- data/lib/legion/extensions/agentic/inference/enactive_cognition/runners/enactive_cognition.rb +17 -17
- data/lib/legion/extensions/agentic/inference/expectation_violation/runners/expectation_violation.rb +12 -12
- data/lib/legion/extensions/agentic/inference/gravity/runners/gravity.rb +16 -16
- data/lib/legion/extensions/agentic/inference/horizon/runners/cognitive_horizon.rb +10 -10
- data/lib/legion/extensions/agentic/inference/hypothesis_testing/runners/hypothesis_testing.rb +11 -11
- data/lib/legion/extensions/agentic/inference/magnet/helpers/magnet_engine.rb +2 -0
- data/lib/legion/extensions/agentic/inference/magnet/runners/cognitive_magnet.rb +13 -13
- data/lib/legion/extensions/agentic/inference/momentum/runners/cognitive_momentum.rb +16 -16
- data/lib/legion/extensions/agentic/inference/perceptual_inference/helpers/perceptual_field.rb +2 -0
- data/lib/legion/extensions/agentic/inference/perceptual_inference/runners/perceptual_inference.rb +12 -12
- data/lib/legion/extensions/agentic/inference/prediction/runners/prediction.rb +11 -11
- data/lib/legion/extensions/agentic/inference/predictive_coding/runners/predictive_coding.rb +13 -13
- data/lib/legion/extensions/agentic/inference/predictive_processing/runners/predictive_processing.rb +11 -11
- data/lib/legion/extensions/agentic/inference/reality_testing/helpers/reality_engine.rb +5 -3
- data/lib/legion/extensions/agentic/inference/reality_testing/runners/reality_testing.rb +4 -4
- data/lib/legion/extensions/agentic/inference/schema/runners/schema.rb +8 -8
- data/lib/legion/extensions/agentic/inference/uncertainty_tolerance/runners/uncertainty_tolerance.rb +14 -14
- data/lib/legion/extensions/agentic/inference/version.rb +1 -1
- data/spec/spec_helper.rb +21 -23
- metadata +99 -1
|
@@ -17,11 +17,11 @@ module Legion
|
|
|
17
17
|
)
|
|
18
18
|
belief = belief_network.add_belief(content: content, domain: domain, prior: pri)
|
|
19
19
|
unless belief
|
|
20
|
-
|
|
20
|
+
log.warn "[bayesian_belief] add failed: network at capacity (#{Helpers::Constants::MAX_HYPOTHESES})"
|
|
21
21
|
return { success: false, reason: :capacity_exceeded, max: Helpers::Constants::MAX_HYPOTHESES }
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
log.debug "[bayesian_belief] add: id=#{belief.id[0..7]} domain=#{domain} prior=#{pri.round(3)}"
|
|
25
25
|
{ success: true, belief_id: belief.id, domain: domain, prior: belief.prior, posterior: belief.posterior }
|
|
26
26
|
end
|
|
27
27
|
|
|
@@ -29,12 +29,12 @@ module Legion
|
|
|
29
29
|
clamped = likelihood.clamp(Helpers::Constants::LIKELIHOOD_FLOOR, Helpers::Constants::LIKELIHOOD_CEILING)
|
|
30
30
|
belief = belief_network.update_belief(belief_id: belief_id, evidence_id: evidence_id, likelihood: clamped)
|
|
31
31
|
unless belief
|
|
32
|
-
|
|
32
|
+
log.debug "[bayesian_belief] update failed: belief_id=#{belief_id} not found"
|
|
33
33
|
return { success: false, reason: :not_found, belief_id: belief_id }
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
log.debug "[bayesian_belief] update: id=#{belief_id[0..7]} evidence=#{evidence_id} " \
|
|
37
|
+
"likelihood=#{clamped.round(3)} posterior=#{belief.posterior.round(3)}"
|
|
38
38
|
{
|
|
39
39
|
success: true,
|
|
40
40
|
belief_id: belief_id,
|
|
@@ -48,49 +48,49 @@ module Legion
|
|
|
48
48
|
|
|
49
49
|
def batch_bayesian_update(evidence_id:, likelihoods:, **)
|
|
50
50
|
if likelihoods.nil? || likelihoods.empty?
|
|
51
|
-
|
|
51
|
+
log.debug '[bayesian_belief] batch_update: empty likelihoods, skipping'
|
|
52
52
|
return { success: true, updated: 0, posteriors: {} }
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
posteriors = belief_network.batch_update(evidence_id: evidence_id, likelihoods: likelihoods)
|
|
56
|
-
|
|
56
|
+
log.debug "[bayesian_belief] batch_update: evidence=#{evidence_id} updated=#{posteriors.size}"
|
|
57
57
|
{ success: true, evidence_id: evidence_id, updated: posteriors.size, posteriors: posteriors }
|
|
58
58
|
end
|
|
59
59
|
|
|
60
60
|
def most_probable_beliefs(domain: nil, limit: 5, **)
|
|
61
61
|
beliefs = belief_network.most_probable(domain: domain, limit: limit)
|
|
62
|
-
|
|
62
|
+
log.debug "[bayesian_belief] most_probable: domain=#{domain.inspect} count=#{beliefs.size}"
|
|
63
63
|
{ success: true, beliefs: beliefs.map(&:to_h), count: beliefs.size }
|
|
64
64
|
end
|
|
65
65
|
|
|
66
66
|
def least_probable_beliefs(domain: nil, limit: 5, **)
|
|
67
67
|
beliefs = belief_network.least_probable(domain: domain, limit: limit)
|
|
68
|
-
|
|
68
|
+
log.debug "[bayesian_belief] least_probable: domain=#{domain.inspect} count=#{beliefs.size}"
|
|
69
69
|
{ success: true, beliefs: beliefs.map(&:to_h), count: beliefs.size }
|
|
70
70
|
end
|
|
71
71
|
|
|
72
72
|
def posterior_distribution(domain: nil, **)
|
|
73
73
|
dist = belief_network.posterior_distribution(domain: domain)
|
|
74
|
-
|
|
74
|
+
log.debug "[bayesian_belief] posterior_distribution: domain=#{domain.inspect} size=#{dist.size}"
|
|
75
75
|
{ success: true, distribution: dist, size: dist.size }
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
def information_gain(belief_id:, evidence_id:, likelihood:, **)
|
|
79
79
|
clamped = likelihood.clamp(Helpers::Constants::LIKELIHOOD_FLOOR, Helpers::Constants::LIKELIHOOD_CEILING)
|
|
80
80
|
gain = belief_network.information_gain(belief_id: belief_id, evidence_id: evidence_id, likelihood: clamped)
|
|
81
|
-
|
|
81
|
+
log.debug "[bayesian_belief] information_gain: id=#{belief_id[0..7]} likelihood=#{clamped.round(3)} gain=#{gain.round(4)}"
|
|
82
82
|
{ success: true, belief_id: belief_id, evidence_id: evidence_id, likelihood: clamped, information_gain: gain }
|
|
83
83
|
end
|
|
84
84
|
|
|
85
85
|
def belief_entropy(domain: nil, **)
|
|
86
86
|
ent = belief_network.entropy(domain: domain)
|
|
87
|
-
|
|
87
|
+
log.debug "[bayesian_belief] entropy: domain=#{domain.inspect} entropy=#{ent.round(4)}"
|
|
88
88
|
{ success: true, domain: domain, entropy: ent }
|
|
89
89
|
end
|
|
90
90
|
|
|
91
91
|
def update_bayesian_beliefs(**)
|
|
92
92
|
decayed = belief_network.decay_all
|
|
93
|
-
|
|
93
|
+
log.debug "[bayesian_belief] decay cycle: beliefs_updated=#{decayed}"
|
|
94
94
|
{ success: true, decayed: decayed }
|
|
95
95
|
end
|
|
96
96
|
|
|
@@ -100,7 +100,7 @@ module Legion
|
|
|
100
100
|
most = belief_network.most_probable(limit: 1).first
|
|
101
101
|
least = belief_network.least_probable(limit: 1).first
|
|
102
102
|
|
|
103
|
-
|
|
103
|
+
log.debug "[bayesian_belief] stats: total=#{total} entropy=#{ent.round(4)}"
|
|
104
104
|
{
|
|
105
105
|
success: true,
|
|
106
106
|
total_beliefs: total,
|
data/lib/legion/extensions/agentic/inference/causal_attribution/runners/causal_attribution.rb
CHANGED
|
@@ -23,8 +23,8 @@ module Legion
|
|
|
23
23
|
controllability: controllability.to_sym,
|
|
24
24
|
confidence: conf
|
|
25
25
|
)
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
log.info "[causal_attribution] create id=#{attr.id} event=#{event} " \
|
|
27
|
+
"outcome=#{outcome} locus=#{locus} emotion=#{attr.emotional_response}"
|
|
28
28
|
{ success: true, attribution: attr.to_h }
|
|
29
29
|
end
|
|
30
30
|
|
|
@@ -36,12 +36,12 @@ module Legion
|
|
|
36
36
|
controllability: controllability&.to_sym
|
|
37
37
|
)
|
|
38
38
|
if result.is_a?(Hash) && result[:found] == false
|
|
39
|
-
|
|
39
|
+
log.warn "[causal_attribution] reattribute not_found id=#{attribution_id}"
|
|
40
40
|
return { success: false, attribution_id: attribution_id, found: false }
|
|
41
41
|
end
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
log.debug "[causal_attribution] reattribute id=#{attribution_id} " \
|
|
44
|
+
"locus=#{result.locus} emotion=#{result.emotional_response}"
|
|
45
45
|
{ success: true, attribution: result.to_h }
|
|
46
46
|
end
|
|
47
47
|
|
|
@@ -51,49 +51,49 @@ module Legion
|
|
|
51
51
|
stability: stability&.to_sym,
|
|
52
52
|
controllability: controllability&.to_sym
|
|
53
53
|
)
|
|
54
|
-
|
|
54
|
+
log.debug "[causal_attribution] by_pattern count=#{results.size}"
|
|
55
55
|
{ success: true, attributions: results.map(&:to_h), count: results.size }
|
|
56
56
|
end
|
|
57
57
|
|
|
58
58
|
def domain_attributions(domain:, **)
|
|
59
59
|
results = engine.by_domain(domain: domain.to_sym)
|
|
60
|
-
|
|
60
|
+
log.debug "[causal_attribution] by_domain domain=#{domain} count=#{results.size}"
|
|
61
61
|
{ success: true, attributions: results.map(&:to_h), count: results.size }
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
def outcome_attributions(outcome:, **)
|
|
65
65
|
results = engine.by_outcome(outcome: outcome.to_sym)
|
|
66
|
-
|
|
66
|
+
log.debug "[causal_attribution] by_outcome outcome=#{outcome} count=#{results.size}"
|
|
67
67
|
{ success: true, attributions: results.map(&:to_h), count: results.size }
|
|
68
68
|
end
|
|
69
69
|
|
|
70
70
|
def attribution_bias_assessment(**)
|
|
71
71
|
bias = engine.attribution_bias
|
|
72
|
-
|
|
72
|
+
log.debug "[causal_attribution] bias_assessment self_serving=#{bias[:self_serving_bias_detected]}"
|
|
73
73
|
{ success: true, bias: bias }
|
|
74
74
|
end
|
|
75
75
|
|
|
76
76
|
def emotional_attribution_profile(**)
|
|
77
77
|
profile = engine.emotional_profile
|
|
78
|
-
|
|
78
|
+
log.debug "[causal_attribution] emotional_profile dominant=#{profile[:dominant]} total=#{profile[:total]}"
|
|
79
79
|
{ success: true, profile: profile }
|
|
80
80
|
end
|
|
81
81
|
|
|
82
82
|
def most_common_attribution(**)
|
|
83
83
|
result = engine.most_common_pattern
|
|
84
|
-
|
|
84
|
+
log.debug "[causal_attribution] most_common pattern=#{result[:pattern].inspect} count=#{result[:count]}"
|
|
85
85
|
{ success: true, pattern: result[:pattern], count: result[:count] }
|
|
86
86
|
end
|
|
87
87
|
|
|
88
88
|
def update_causal_attribution(**)
|
|
89
89
|
decayed = engine.decay_all
|
|
90
|
-
|
|
90
|
+
log.debug "[causal_attribution] decay cycle entries=#{decayed}"
|
|
91
91
|
{ success: true, decayed: decayed }
|
|
92
92
|
end
|
|
93
93
|
|
|
94
94
|
def causal_attribution_stats(**)
|
|
95
95
|
stats = engine.to_h
|
|
96
|
-
|
|
96
|
+
log.debug "[causal_attribution] stats total=#{stats[:total_attributions]}"
|
|
97
97
|
{ success: true, stats: stats }
|
|
98
98
|
end
|
|
99
99
|
|
|
@@ -12,16 +12,16 @@ module Legion
|
|
|
12
12
|
|
|
13
13
|
def add_causal_variable(name:, domain: :general, **)
|
|
14
14
|
if graph.variable_exists?(name)
|
|
15
|
-
|
|
15
|
+
log.warn "[causal] add_variable duplicate: name=#{name}"
|
|
16
16
|
return { success: false, reason: :limit_or_duplicate, name: name }
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
variable = graph.add_variable(name: name, domain: domain)
|
|
20
20
|
if variable
|
|
21
|
-
|
|
21
|
+
log.debug "[causal] add_variable: name=#{name} domain=#{domain}"
|
|
22
22
|
{ success: true, variable: variable }
|
|
23
23
|
else
|
|
24
|
-
|
|
24
|
+
log.warn "[causal] add_variable failed (limit): name=#{name}"
|
|
25
25
|
{ success: false, reason: :limit_or_duplicate, name: name }
|
|
26
26
|
end
|
|
27
27
|
end
|
|
@@ -32,42 +32,42 @@ module Legion
|
|
|
32
32
|
edge_type: edge_type, domain: domain, strength: strength)
|
|
33
33
|
if edge
|
|
34
34
|
str = edge.strength.round(2)
|
|
35
|
-
|
|
35
|
+
log.debug "[causal] add_edge: #{cause}->#{effect} type=#{edge_type} str=#{str}"
|
|
36
36
|
{ success: true, edge: edge.to_h }
|
|
37
37
|
else
|
|
38
|
-
|
|
38
|
+
log.warn "[causal] add_edge failed: cause=#{cause} effect=#{effect} type=#{edge_type}"
|
|
39
39
|
{ success: false, reason: :limit_or_invalid_type, cause: cause, effect: effect }
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
def find_causes(variable:, **)
|
|
44
44
|
edges = graph.causes_of(variable: variable)
|
|
45
|
-
|
|
45
|
+
log.debug "[causal] find_causes: variable=#{variable} count=#{edges.size}"
|
|
46
46
|
{ success: true, variable: variable, causes: edges.map(&:to_h), count: edges.size }
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
def find_effects(variable:, **)
|
|
50
50
|
edges = graph.effects_of(variable: variable)
|
|
51
|
-
|
|
51
|
+
log.debug "[causal] find_effects: variable=#{variable} count=#{edges.size}"
|
|
52
52
|
{ success: true, variable: variable, effects: edges.map(&:to_h), count: edges.size }
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
def trace_causal_chain(from:, to:, max_depth: 5, **)
|
|
56
56
|
paths = graph.causal_chain(from: from, to: to, max_depth: max_depth)
|
|
57
|
-
|
|
57
|
+
log.debug "[causal] trace_chain: from=#{from} to=#{to} paths=#{paths.size}"
|
|
58
58
|
{ success: true, from: from, to: to, paths: paths, path_count: paths.size }
|
|
59
59
|
end
|
|
60
60
|
|
|
61
61
|
def causal_intervention(variable:, value:, **)
|
|
62
62
|
result = graph.intervene(variable: variable, value: value)
|
|
63
63
|
count = result[:downstream_effects].size
|
|
64
|
-
|
|
64
|
+
log.info "[causal] intervention: do(#{variable}=#{value}) downstream=#{count}"
|
|
65
65
|
{ success: true }.merge(result)
|
|
66
66
|
end
|
|
67
67
|
|
|
68
68
|
def find_confounders(var_a:, var_b:, **)
|
|
69
69
|
common = graph.confounders(var_a: var_a, var_b: var_b)
|
|
70
|
-
|
|
70
|
+
log.debug "[causal] confounders: #{var_a} <-> #{var_b} count=#{common.size}"
|
|
71
71
|
{ success: true, var_a: var_a, var_b: var_b, confounders: common, count: common.size }
|
|
72
72
|
end
|
|
73
73
|
|
|
@@ -76,10 +76,10 @@ module Legion
|
|
|
76
76
|
if edge
|
|
77
77
|
cnt = edge.evidence_count
|
|
78
78
|
str = edge.strength.round(2)
|
|
79
|
-
|
|
79
|
+
log.debug "[causal] add_evidence: edge=#{edge_id} count=#{cnt} strength=#{str}"
|
|
80
80
|
{ success: true, edge_id: edge_id, evidence_count: edge.evidence_count, strength: edge.strength }
|
|
81
81
|
else
|
|
82
|
-
|
|
82
|
+
log.warn "[causal] add_evidence failed: edge=#{edge_id} not found"
|
|
83
83
|
{ success: false, reason: :edge_not_found, edge_id: edge_id }
|
|
84
84
|
end
|
|
85
85
|
end
|
|
@@ -87,13 +87,13 @@ module Legion
|
|
|
87
87
|
def update_causal_reasoning(**)
|
|
88
88
|
decayed = graph.decay_all
|
|
89
89
|
pruned = graph.prune_weak
|
|
90
|
-
|
|
90
|
+
log.debug "[causal] update: decayed=#{decayed} pruned=#{pruned}"
|
|
91
91
|
{ success: true, decayed: decayed, pruned: pruned }
|
|
92
92
|
end
|
|
93
93
|
|
|
94
94
|
def causal_reasoning_stats(**)
|
|
95
95
|
stats = graph.to_h
|
|
96
|
-
|
|
96
|
+
log.debug "[causal] stats: #{stats.inspect}"
|
|
97
97
|
{ success: true }.merge(stats)
|
|
98
98
|
end
|
|
99
99
|
|
|
@@ -16,10 +16,10 @@ module Legion
|
|
|
16
16
|
|
|
17
17
|
prop_id = engine.add_proposition(content: content, domain: domain, acceptance: acceptance)
|
|
18
18
|
if prop_id
|
|
19
|
-
|
|
19
|
+
log.debug "[cognitive_coherence] add_proposition domain=#{domain} id=#{prop_id[0..7]}"
|
|
20
20
|
{ success: true, proposition_id: prop_id, domain: domain, acceptance: acceptance }
|
|
21
21
|
else
|
|
22
|
-
|
|
22
|
+
log.warn '[cognitive_coherence] add_proposition failed: max propositions reached'
|
|
23
23
|
{ success: false, reason: :max_propositions_reached }
|
|
24
24
|
end
|
|
25
25
|
end
|
|
@@ -37,8 +37,8 @@ module Legion
|
|
|
37
37
|
positive: positive
|
|
38
38
|
)
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
log.debug "[cognitive_coherence] add_constraint type=#{constraint_type} " \
|
|
41
|
+
"positive=#{positive} success=#{result[:success]}"
|
|
42
42
|
result
|
|
43
43
|
end
|
|
44
44
|
|
|
@@ -47,34 +47,34 @@ module Legion
|
|
|
47
47
|
prop = engine.propositions[proposition_id]
|
|
48
48
|
|
|
49
49
|
unless prop
|
|
50
|
-
|
|
50
|
+
log.debug "[cognitive_coherence] compute_coherence: #{proposition_id[0..7]} not found"
|
|
51
51
|
return { success: false, reason: :not_found }
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
log.debug '[cognitive_coherence] compute_coherence ' \
|
|
55
|
+
"id=#{proposition_id[0..7]} score=#{score.round(3)}"
|
|
56
56
|
{ success: true, proposition_id: proposition_id, coherence_score: score, state: prop.state }
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
def maximize_coherence(**)
|
|
60
60
|
result = engine.maximize_coherence
|
|
61
61
|
overall = result[:overall_coherence]&.round(3)
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
log.info '[cognitive_coherence] maximize_coherence ' \
|
|
63
|
+
"overall=#{overall} props=#{result[:proposition_count]}"
|
|
64
64
|
result
|
|
65
65
|
end
|
|
66
66
|
|
|
67
67
|
def find_contradictions(**)
|
|
68
68
|
pairs = engine.find_contradictions
|
|
69
|
-
|
|
69
|
+
log.debug "[cognitive_coherence] find_contradictions count=#{pairs.size}"
|
|
70
70
|
{ success: true, contradictions: pairs, count: pairs.size }
|
|
71
71
|
end
|
|
72
72
|
|
|
73
73
|
def coherence_partition(**)
|
|
74
74
|
result = engine.partition
|
|
75
75
|
totals = result.transform_values(&:size)
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
log.debug "[cognitive_coherence] partition accepted=#{totals[:accepted]} " \
|
|
77
|
+
"rejected=#{totals[:rejected]} undecided=#{totals[:undecided]}"
|
|
78
78
|
{ success: true, partition: result, counts: totals }
|
|
79
79
|
end
|
|
80
80
|
|
|
@@ -83,8 +83,8 @@ module Legion
|
|
|
83
83
|
decay_result = engine.decay_all
|
|
84
84
|
|
|
85
85
|
overall = coherence_result[:overall_coherence]&.round(3)
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
log.info "[cognitive_coherence] update overall=#{overall} " \
|
|
87
|
+
"decayed=#{decay_result[:decayed_count]}"
|
|
88
88
|
{
|
|
89
89
|
success: true,
|
|
90
90
|
overall_coherence: coherence_result[:overall_coherence],
|
|
@@ -96,8 +96,8 @@ module Legion
|
|
|
96
96
|
def cognitive_coherence_stats(**)
|
|
97
97
|
part = engine.partition
|
|
98
98
|
counts = part.transform_values(&:size)
|
|
99
|
-
|
|
100
|
-
|
|
99
|
+
log.debug "[cognitive_coherence] stats propositions=#{engine.propositions.size} " \
|
|
100
|
+
"coherence=#{engine.overall_coherence.round(3)}"
|
|
101
101
|
{
|
|
102
102
|
success: true,
|
|
103
103
|
proposition_count: engine.propositions.size,
|
|
@@ -33,62 +33,62 @@ module Legion
|
|
|
33
33
|
plausibility: plausibility
|
|
34
34
|
)
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
log.debug "[counterfactual] imagined: type=#{scenario_type} " \
|
|
37
|
+
"domain=#{domain} id=#{scenario.id[0..7]}"
|
|
38
38
|
|
|
39
39
|
{ success: true, scenario: scenario.to_h }
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
def generate_alternatives(actual_outcome:, domain:, **)
|
|
43
43
|
alternatives = engine.generate_alternatives(actual_outcome: actual_outcome, domain: domain)
|
|
44
|
-
|
|
44
|
+
log.debug "[counterfactual] generated #{alternatives.size} alternatives for domain=#{domain}"
|
|
45
45
|
{ success: true, alternatives: alternatives.map(&:to_h), count: alternatives.size }
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
def resolve_counterfactual(scenario_id:, lesson:, **)
|
|
49
49
|
scenario = engine.resolve(scenario_id: scenario_id, lesson: lesson)
|
|
50
50
|
if scenario
|
|
51
|
-
|
|
51
|
+
log.info "[counterfactual] resolved: id=#{scenario_id[0..7]} lesson=#{lesson[0..40]}"
|
|
52
52
|
{ success: true, scenario: scenario.to_h }
|
|
53
53
|
else
|
|
54
|
-
|
|
54
|
+
log.debug "[counterfactual] resolve failed: id=#{scenario_id[0..7]} not found"
|
|
55
55
|
{ success: false, reason: :not_found }
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
def compute_regret(scenario_id:, **)
|
|
60
60
|
regret = engine.compute_regret(scenario_id: scenario_id)
|
|
61
|
-
|
|
61
|
+
log.debug "[counterfactual] regret: id=#{scenario_id[0..7]} value=#{regret.round(4)}"
|
|
62
62
|
{ success: true, scenario_id: scenario_id, regret: regret }
|
|
63
63
|
end
|
|
64
64
|
|
|
65
65
|
def net_regret_level(**)
|
|
66
66
|
net = engine.net_regret
|
|
67
|
-
|
|
67
|
+
log.debug "[counterfactual] net_regret=#{net.round(4)}"
|
|
68
68
|
{ success: true, net_regret: net }
|
|
69
69
|
end
|
|
70
70
|
|
|
71
71
|
def domain_regret(domain:, **)
|
|
72
72
|
regret = engine.domain_regret(domain: domain)
|
|
73
|
-
|
|
73
|
+
log.debug "[counterfactual] domain_regret: domain=#{domain} value=#{regret.round(4)}"
|
|
74
74
|
{ success: true, domain: domain, regret: regret }
|
|
75
75
|
end
|
|
76
76
|
|
|
77
77
|
def lessons_learned(**)
|
|
78
78
|
lessons = engine.lessons_learned
|
|
79
|
-
|
|
79
|
+
log.debug "[counterfactual] lessons_learned: count=#{lessons.size}"
|
|
80
80
|
{ success: true, lessons: lessons.map(&:to_h), count: lessons.size }
|
|
81
81
|
end
|
|
82
82
|
|
|
83
83
|
def update_counterfactual(**)
|
|
84
84
|
engine.regret_decay
|
|
85
|
-
|
|
85
|
+
log.debug '[counterfactual] regret decay applied'
|
|
86
86
|
{ success: true, action: :regret_decay }
|
|
87
87
|
end
|
|
88
88
|
|
|
89
89
|
def counterfactual_stats(**)
|
|
90
90
|
stats = engine.to_h
|
|
91
|
-
|
|
91
|
+
log.debug "[counterfactual] stats: total=#{stats[:total]} unresolved=#{stats[:unresolved]}"
|
|
92
92
|
{ success: true, stats: stats }
|
|
93
93
|
end
|
|
94
94
|
|
|
@@ -12,7 +12,7 @@ module Legion
|
|
|
12
12
|
|
|
13
13
|
def detect_error(error_type:, description:, severity:, source_phase:, confidence_at_detection: 0.5, **)
|
|
14
14
|
unless Helpers::Constants::ERROR_TYPES.include?(error_type)
|
|
15
|
-
|
|
15
|
+
log.debug "[cognitive_debugging] detect_error: invalid error_type=#{error_type}"
|
|
16
16
|
return { success: false, error: :invalid_error_type, valid: Helpers::Constants::ERROR_TYPES }
|
|
17
17
|
end
|
|
18
18
|
|
|
@@ -25,11 +25,11 @@ module Legion
|
|
|
25
25
|
)
|
|
26
26
|
|
|
27
27
|
if err
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
log.info "[cognitive_debugging] error detected: id=#{err.id[0..7]} type=#{error_type} " \
|
|
29
|
+
"phase=#{source_phase} severity=#{err.severity_label}"
|
|
30
30
|
{ success: true, error_id: err.id, error_type: error_type, severity_label: err.severity_label }
|
|
31
31
|
else
|
|
32
|
-
|
|
32
|
+
log.warn '[cognitive_debugging] detect_error: engine returned nil (cap reached?)'
|
|
33
33
|
{ success: false, error: :cap_reached }
|
|
34
34
|
end
|
|
35
35
|
end
|
|
@@ -43,29 +43,29 @@ module Legion
|
|
|
43
43
|
)
|
|
44
44
|
|
|
45
45
|
if trace
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
log.info "[cognitive_debugging] error traced: error_id=#{error_id[0..7]} " \
|
|
47
|
+
"trace_id=#{trace.id[0..7]} depth=#{trace.depth} root_cause=#{root_cause}"
|
|
48
48
|
{ success: true, trace_id: trace.id, depth: trace.depth, root_cause: root_cause }
|
|
49
49
|
else
|
|
50
|
-
|
|
50
|
+
log.debug "[cognitive_debugging] trace_error: error_id=#{error_id[0..7]} not found or cap"
|
|
51
51
|
{ success: false, error: :not_found_or_cap }
|
|
52
52
|
end
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
def propose_correction(error_id:, strategy:, description:, **)
|
|
56
56
|
unless Helpers::Constants::CORRECTION_STRATEGIES.include?(strategy)
|
|
57
|
-
|
|
57
|
+
log.debug "[cognitive_debugging] propose_correction: invalid strategy=#{strategy}"
|
|
58
58
|
return { success: false, error: :invalid_strategy, valid: Helpers::Constants::CORRECTION_STRATEGIES }
|
|
59
59
|
end
|
|
60
60
|
|
|
61
61
|
correction = engine.propose_correction(error_id: error_id, strategy: strategy, description: description)
|
|
62
62
|
|
|
63
63
|
if correction
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
log.info "[cognitive_debugging] correction proposed: error_id=#{error_id[0..7]} " \
|
|
65
|
+
"correction_id=#{correction.id[0..7]} strategy=#{strategy}"
|
|
66
66
|
{ success: true, correction_id: correction.id, strategy: strategy }
|
|
67
67
|
else
|
|
68
|
-
|
|
68
|
+
log.debug "[cognitive_debugging] propose_correction: error_id=#{error_id[0..7]} not found"
|
|
69
69
|
{ success: false, error: :not_found }
|
|
70
70
|
end
|
|
71
71
|
end
|
|
@@ -74,10 +74,10 @@ module Legion
|
|
|
74
74
|
correction = engine.apply_correction(correction_id: correction_id)
|
|
75
75
|
|
|
76
76
|
if correction
|
|
77
|
-
|
|
77
|
+
log.info "[cognitive_debugging] correction applied: id=#{correction_id[0..7]}"
|
|
78
78
|
{ success: true, correction_id: correction_id, applied: true }
|
|
79
79
|
else
|
|
80
|
-
|
|
80
|
+
log.debug "[cognitive_debugging] apply_correction: id=#{correction_id[0..7]} not found"
|
|
81
81
|
{ success: false, error: :not_found }
|
|
82
82
|
end
|
|
83
83
|
end
|
|
@@ -86,12 +86,12 @@ module Legion
|
|
|
86
86
|
correction = engine.measure_correction(correction_id: correction_id, effectiveness: effectiveness)
|
|
87
87
|
|
|
88
88
|
if correction
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
log.info "[cognitive_debugging] correction measured: id=#{correction_id[0..7]} " \
|
|
90
|
+
"effectiveness=#{correction.effectiveness} effective=#{correction.effective?}"
|
|
91
91
|
{ success: true, correction_id: correction_id, effectiveness: correction.effectiveness,
|
|
92
92
|
effective: correction.effective? }
|
|
93
93
|
else
|
|
94
|
-
|
|
94
|
+
log.debug "[cognitive_debugging] measure_correction: id=#{correction_id[0..7]} not found"
|
|
95
95
|
{ success: false, error: :not_found }
|
|
96
96
|
end
|
|
97
97
|
end
|
|
@@ -100,67 +100,67 @@ module Legion
|
|
|
100
100
|
err = engine.resolve_error(error_id: error_id)
|
|
101
101
|
|
|
102
102
|
if err
|
|
103
|
-
|
|
103
|
+
log.info "[cognitive_debugging] error resolved: id=#{error_id[0..7]}"
|
|
104
104
|
{ success: true, error_id: error_id, resolved: true }
|
|
105
105
|
else
|
|
106
|
-
|
|
106
|
+
log.debug "[cognitive_debugging] resolve_error: id=#{error_id[0..7]} not found"
|
|
107
107
|
{ success: false, error: :not_found }
|
|
108
108
|
end
|
|
109
109
|
end
|
|
110
110
|
|
|
111
111
|
def active_errors(**)
|
|
112
112
|
errors = engine.active_errors
|
|
113
|
-
|
|
113
|
+
log.debug "[cognitive_debugging] active_errors: count=#{errors.size}"
|
|
114
114
|
{ success: true, errors: errors.map(&:to_h), count: errors.size }
|
|
115
115
|
end
|
|
116
116
|
|
|
117
117
|
def resolved_errors(**)
|
|
118
118
|
errors = engine.resolved_errors
|
|
119
|
-
|
|
119
|
+
log.debug "[cognitive_debugging] resolved_errors: count=#{errors.size}"
|
|
120
120
|
{ success: true, errors: errors.map(&:to_h), count: errors.size }
|
|
121
121
|
end
|
|
122
122
|
|
|
123
123
|
def errors_by_type(**)
|
|
124
124
|
tally = engine.errors_by_type
|
|
125
|
-
|
|
125
|
+
log.debug "[cognitive_debugging] errors_by_type: types=#{tally.keys.join(',')}"
|
|
126
126
|
{ success: true, tally: tally }
|
|
127
127
|
end
|
|
128
128
|
|
|
129
129
|
def errors_by_phase(**)
|
|
130
130
|
tally = engine.errors_by_phase
|
|
131
|
-
|
|
131
|
+
log.debug "[cognitive_debugging] errors_by_phase: phases=#{tally.keys.join(',')}"
|
|
132
132
|
{ success: true, tally: tally }
|
|
133
133
|
end
|
|
134
134
|
|
|
135
135
|
def most_common_error_type(**)
|
|
136
136
|
type = engine.most_common_error_type
|
|
137
|
-
|
|
137
|
+
log.debug "[cognitive_debugging] most_common_error_type: type=#{type}"
|
|
138
138
|
{ success: true, error_type: type }
|
|
139
139
|
end
|
|
140
140
|
|
|
141
141
|
def most_effective_strategy(**)
|
|
142
142
|
strategy = engine.most_effective_strategy
|
|
143
|
-
|
|
143
|
+
log.debug "[cognitive_debugging] most_effective_strategy: strategy=#{strategy}"
|
|
144
144
|
{ success: true, strategy: strategy }
|
|
145
145
|
end
|
|
146
146
|
|
|
147
147
|
def correction_success_rate(**)
|
|
148
148
|
rate = engine.correction_success_rate
|
|
149
|
-
|
|
149
|
+
log.debug "[cognitive_debugging] correction_success_rate: rate=#{rate}"
|
|
150
150
|
{ success: true, rate: rate }
|
|
151
151
|
end
|
|
152
152
|
|
|
153
153
|
def debugging_report(**)
|
|
154
154
|
report = engine.debugging_report
|
|
155
|
-
|
|
156
|
-
|
|
155
|
+
log.info "[cognitive_debugging] debugging_report: total_errors=#{report[:total_errors]} " \
|
|
156
|
+
"active=#{report[:active_errors]}"
|
|
157
157
|
{ success: true, report: report }
|
|
158
158
|
end
|
|
159
159
|
|
|
160
160
|
def snapshot(**)
|
|
161
161
|
data = engine.to_h
|
|
162
|
-
|
|
163
|
-
|
|
162
|
+
log.debug "[cognitive_debugging] snapshot: errors=#{data[:errors].size} " \
|
|
163
|
+
"traces=#{data[:traces].size} corrections=#{data[:corrections].size}"
|
|
164
164
|
{ success: true, snapshot: data }
|
|
165
165
|
end
|
|
166
166
|
|