Module: Legion::Extensions::Agentic::Inference::CausalReasoning::Runners::CausalReasoning

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

Instance Method Summary collapse

Instance Method Details

#add_causal_edge(cause:, effect:, edge_type:, domain: :general, strength: Helpers::Constants::DEFAULT_STRENGTH) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/runners/causal_reasoning.rb', line 29

def add_causal_edge(cause:, effect:, edge_type:, domain: :general,
                    strength: Helpers::Constants::DEFAULT_STRENGTH, **)
  edge = graph.add_edge(cause: cause, effect: effect,
                        edge_type: edge_type, domain: domain, strength: strength)
  if edge
    str = edge.strength.round(2)
    log.debug "[causal] add_edge: #{cause}->#{effect} type=#{edge_type} str=#{str}"
    { success: true, edge: edge.to_h }
  else
    log.warn "[causal] add_edge failed: cause=#{cause} effect=#{effect} type=#{edge_type}"
    { success: false, reason: :limit_or_invalid_type, cause: cause, effect: effect }
  end
end

#add_causal_evidence(edge_id:) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/runners/causal_reasoning.rb', line 74

def add_causal_evidence(edge_id:, **)
  edge = graph.add_evidence(edge_id: edge_id)
  if edge
    cnt = edge.evidence_count
    str = edge.strength.round(2)
    log.debug "[causal] add_evidence: edge=#{edge_id} count=#{cnt} strength=#{str}"
    { success: true, edge_id: edge_id, evidence_count: edge.evidence_count, strength: edge.strength }
  else
    log.warn "[causal] add_evidence failed: edge=#{edge_id} not found"
    { success: false, reason: :edge_not_found, edge_id: edge_id }
  end
end

#add_causal_variable(name:, domain: :general) ⇒ Object



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

def add_causal_variable(name:, domain: :general, **)
  if graph.variable_exists?(name)
    log.warn "[causal] add_variable duplicate: name=#{name}"
    return { success: false, reason: :limit_or_duplicate, name: name }
  end

  variable = graph.add_variable(name: name, domain: domain)
  if variable
    log.debug "[causal] add_variable: name=#{name} domain=#{domain}"
    { success: true, variable: variable }
  else
    log.warn "[causal] add_variable failed (limit): name=#{name}"
    { success: false, reason: :limit_or_duplicate, name: name }
  end
end

#causal_intervention(variable:, value:) ⇒ Object



61
62
63
64
65
66
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/runners/causal_reasoning.rb', line 61

def causal_intervention(variable:, value:, **)
  result = graph.intervene(variable: variable, value: value)
  count = result[:downstream_effects].size
  log.info "[causal] intervention: do(#{variable}=#{value}) downstream=#{count}"
  { success: true }.merge(result)
end

#causal_reasoning_statsObject



94
95
96
97
98
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/runners/causal_reasoning.rb', line 94

def causal_reasoning_stats(**)
  stats = graph.to_h
  log.debug "[causal] stats: #{stats.inspect}"
  { success: true }.merge(stats)
end

#find_causes(variable:) ⇒ Object



43
44
45
46
47
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/runners/causal_reasoning.rb', line 43

def find_causes(variable:, **)
  edges = graph.causes_of(variable: variable)
  log.debug "[causal] find_causes: variable=#{variable} count=#{edges.size}"
  { success: true, variable: variable, causes: edges.map(&:to_h), count: edges.size }
end

#find_confounders(var_a:, var_b:) ⇒ Object



68
69
70
71
72
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/runners/causal_reasoning.rb', line 68

def find_confounders(var_a:, var_b:, **)
  common = graph.confounders(var_a: var_a, var_b: var_b)
  log.debug "[causal] confounders: #{var_a} <-> #{var_b} count=#{common.size}"
  { success: true, var_a: var_a, var_b: var_b, confounders: common, count: common.size }
end

#find_effects(variable:) ⇒ Object



49
50
51
52
53
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/runners/causal_reasoning.rb', line 49

def find_effects(variable:, **)
  edges = graph.effects_of(variable: variable)
  log.debug "[causal] find_effects: variable=#{variable} count=#{edges.size}"
  { success: true, variable: variable, effects: edges.map(&:to_h), count: edges.size }
end

#trace_causal_chain(from:, to:, max_depth: 5) ⇒ Object



55
56
57
58
59
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/runners/causal_reasoning.rb', line 55

def trace_causal_chain(from:, to:, max_depth: 5, **)
  paths = graph.causal_chain(from: from, to: to, max_depth: max_depth)
  log.debug "[causal] trace_chain: from=#{from} to=#{to} paths=#{paths.size}"
  { success: true, from: from, to: to, paths: paths, path_count: paths.size }
end

#update_causal_reasoningObject



87
88
89
90
91
92
# File 'lib/legion/extensions/agentic/inference/causal_reasoning/runners/causal_reasoning.rb', line 87

def update_causal_reasoning(**)
  decayed = graph.decay_all
  pruned  = graph.prune_weak
  log.debug "[causal] update: decayed=#{decayed} pruned=#{pruned}"
  { success: true, decayed: decayed, pruned: pruned }
end