Module: Legion::Extensions::Agentic::Inference::Abductive::Runners::AbductiveReasoning

Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning.rb

Instance Method Summary collapse

Instance Method Details

#abductive_reasoning_statsObject



112
113
114
115
116
# File 'lib/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning.rb', line 112

def abductive_reasoning_stats(**)
  stats = engine.to_h
  log.debug "[abductive_reasoning] stats: #{stats.inspect}"
  { success: true }.merge(stats)
end

#add_hypothesis_evidence(hypothesis_id:, supporting:) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning.rb', line 58

def add_hypothesis_evidence(hypothesis_id:, supporting:, **)
  result = engine.add_evidence(hypothesis_id: hypothesis_id, supporting: supporting)
  if result[:found] == false
    log.debug "[abductive_reasoning] add_evidence: not found id=#{hypothesis_id[0..7]}"
    return { success: false, error: :not_found }
  end

  log.debug "[abductive_reasoning] evidence added: id=#{hypothesis_id[0..7]} " \
            "supporting=#{supporting} state=#{result[:state]}"
  { success: true }.merge(result)
end

#best_explanation(observation_id:) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning.rb', line 70

def best_explanation(observation_id:, **)
  hyp = engine.best_explanation(observation_id: observation_id)
  if hyp
    log.debug "[abductive_reasoning] best_explanation: obs=#{observation_id[0..7]} " \
              "hyp=#{hyp.id[0..7]} score=#{hyp.overall_score.round(3)}"
    { success: true, found: true, hypothesis: hyp.to_h }
  else
    log.debug "[abductive_reasoning] best_explanation: obs=#{observation_id[0..7]} none found"
    { success: true, found: false }
  end
end

#competing_hypotheses(observation_id:) ⇒ Object



82
83
84
85
86
# File 'lib/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning.rb', line 82

def competing_hypotheses(observation_id:, **)
  ranked = engine.competing_hypotheses(observation_id: observation_id)
  log.debug "[abductive_reasoning] competing_hypotheses: obs=#{observation_id[0..7]} count=#{ranked.size}"
  { success: true, hypotheses: ranked.map(&:to_h), count: ranked.size }
end

#evaluate_hypothesis(hypothesis_id:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning.rb', line 46

def evaluate_hypothesis(hypothesis_id:, **)
  result = engine.evaluate_hypothesis(hypothesis_id: hypothesis_id)
  if result[:found] == false
    log.debug "[abductive_reasoning] evaluate: not found id=#{hypothesis_id[0..7]}"
    return { success: false, error: :not_found }
  end

  log.debug "[abductive_reasoning] evaluate: id=#{hypothesis_id[0..7]} " \
            "score=#{result[:score].round(3)} rank=#{result[:rank]} label=#{result[:quality_label]}"
  { success: true }.merge(result)
end

#generate_hypothesis(content:, observation_ids:, domain:, simplicity:, explanatory_power:, prior_probability: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning.rb', line 30

def generate_hypothesis(content:, observation_ids:, domain:, simplicity:,
                        explanatory_power:, prior_probability: nil, **)
  prior = prior_probability || Helpers::Constants::DEFAULT_PLAUSIBILITY
  hyp = engine.generate_hypothesis(
    content:           content,
    observation_ids:   observation_ids,
    domain:            domain,
    simplicity:        simplicity,
    explanatory_power: explanatory_power,
    prior_probability: prior
  )
  log.debug "[abductive_reasoning] hypothesis generated: id=#{hyp.id[0..7]} " \
            "domain=#{domain} score=#{hyp.overall_score.round(3)}"
  { success: true, hypothesis: hyp.to_h }
end

#record_observation(content:, domain:, surprise_level: :notable, context: {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning.rb', line 13

def record_observation(content:, domain:, surprise_level: :notable, context: {}, **)
  unless Helpers::Constants::SURPRISE_LEVELS.include?(surprise_level)
    return { success: false, error: :invalid_surprise_level,
             valid_levels: Helpers::Constants::SURPRISE_LEVELS }
  end

  obs = engine.record_observation(
    content:        content,
    domain:         domain,
    surprise_level: surprise_level,
    context:        context
  )
  log.debug "[abductive_reasoning] observation recorded: id=#{obs.id[0..7]} " \
            "domain=#{domain} surprise=#{surprise_level}"
  { success: true, observation: obs.to_h }
end

#refute_hypothesis(hypothesis_id:) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning.rb', line 88

def refute_hypothesis(hypothesis_id:, **)
  result = engine.refute_hypothesis(hypothesis_id: hypothesis_id)
  if result[:found] == false
    log.debug "[abductive_reasoning] refute: not found id=#{hypothesis_id[0..7]}"
    return { success: false, error: :not_found }
  end

  log.debug "[abductive_reasoning] hypothesis refuted: id=#{hypothesis_id[0..7]}"
  { success: true }.merge(result)
end

#unexplained_observationsObject



99
100
101
102
103
# File 'lib/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning.rb', line 99

def unexplained_observations(**)
  observations = engine.unexplained_observations
  log.debug "[abductive_reasoning] unexplained_observations: count=#{observations.size}"
  { success: true, observations: observations.map(&:to_h), count: observations.size }
end

#update_abductive_reasoningObject



105
106
107
108
109
110
# File 'lib/legion/extensions/agentic/inference/abductive/runners/abductive_reasoning.rb', line 105

def update_abductive_reasoning(**)
  decayed = engine.decay_stale
  pruned  = engine.prune_refuted
  log.debug "[abductive_reasoning] update cycle: decayed=#{decayed} pruned=#{pruned}"
  { success: true, decayed: decayed, pruned: pruned }
end