Module: Legion::Extensions::Agentic::Inference::Counterfactual::Runners::Counterfactual

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

Constant Summary

Constants included from Helpers::Constants

Helpers::Constants::COUNTERFACTUAL_TYPES, Helpers::Constants::DEFAULT_REGRET, Helpers::Constants::DOWNWARD_WEIGHT, Helpers::Constants::EMOTIONAL_RESPONSES, Helpers::Constants::MAX_ALTERNATIVES, Helpers::Constants::MAX_HISTORY, Helpers::Constants::MAX_SCENARIOS, Helpers::Constants::MUTATION_TYPES, Helpers::Constants::PLAUSIBILITY_THRESHOLD, Helpers::Constants::REGRET_CEILING, Helpers::Constants::REGRET_DECAY, Helpers::Constants::REGRET_FLOOR, Helpers::Constants::RELEVANCE_THRESHOLD, Helpers::Constants::UPWARD_WEIGHT

Instance Method Summary collapse

Instance Method Details

#compute_regret(scenario_id:) ⇒ Object



59
60
61
62
63
# File 'lib/legion/extensions/agentic/inference/counterfactual/runners/counterfactual.rb', line 59

def compute_regret(scenario_id:, **)
  regret = engine.compute_regret(scenario_id: scenario_id)
  log.debug "[counterfactual] regret: id=#{scenario_id[0..7]} value=#{regret.round(4)}"
  { success: true, scenario_id: scenario_id, regret: regret }
end

#counterfactual_statsObject



89
90
91
92
93
# File 'lib/legion/extensions/agentic/inference/counterfactual/runners/counterfactual.rb', line 89

def counterfactual_stats(**)
  stats = engine.to_h
  log.debug "[counterfactual] stats: total=#{stats[:total]} unresolved=#{stats[:unresolved]}"
  { success: true, stats: stats }
end

#domain_regret(domain:) ⇒ Object



71
72
73
74
75
# File 'lib/legion/extensions/agentic/inference/counterfactual/runners/counterfactual.rb', line 71

def domain_regret(domain:, **)
  regret = engine.domain_regret(domain: domain)
  log.debug "[counterfactual] domain_regret: domain=#{domain} value=#{regret.round(4)}"
  { success: true, domain: domain, regret: regret }
end

#generate_alternatives(actual_outcome:, domain:) ⇒ Object



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

def generate_alternatives(actual_outcome:, domain:, **)
  alternatives = engine.generate_alternatives(actual_outcome: actual_outcome, domain: domain)
  log.debug "[counterfactual] generated #{alternatives.size} alternatives for domain=#{domain}"
  { success: true, alternatives: alternatives.map(&:to_h), count: alternatives.size }
end

#imagine_counterfactual(actual_outcome:, counterfactual_outcome:, antecedent:, scenario_type:, mutation_type:, domain:, plausibility:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/legion/extensions/agentic/inference/counterfactual/runners/counterfactual.rb', line 14

def imagine_counterfactual(actual_outcome:, counterfactual_outcome:, antecedent:,
                           scenario_type:, mutation_type:, domain:, plausibility:, **)
  unless COUNTERFACTUAL_TYPES.include?(scenario_type)
    return { success: false, error: :invalid_scenario_type,
             valid_types: COUNTERFACTUAL_TYPES }
  end

  unless MUTATION_TYPES.include?(mutation_type)
    return { success: false, error: :invalid_mutation_type,
             valid_types: MUTATION_TYPES }
  end

  scenario = engine.generate(
    actual_outcome:         actual_outcome,
    counterfactual_outcome: counterfactual_outcome,
    antecedent:             antecedent,
    scenario_type:          scenario_type,
    mutation_type:          mutation_type,
    domain:                 domain,
    plausibility:           plausibility
  )

  log.debug "[counterfactual] imagined: type=#{scenario_type} " \
            "domain=#{domain} id=#{scenario.id[0..7]}"

  { success: true, scenario: scenario.to_h }
end

#lessons_learnedObject



77
78
79
80
81
# File 'lib/legion/extensions/agentic/inference/counterfactual/runners/counterfactual.rb', line 77

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

#net_regret_levelObject



65
66
67
68
69
# File 'lib/legion/extensions/agentic/inference/counterfactual/runners/counterfactual.rb', line 65

def net_regret_level(**)
  net = engine.net_regret
  log.debug "[counterfactual] net_regret=#{net.round(4)}"
  { success: true, net_regret: net }
end

#resolve_counterfactual(scenario_id:, lesson:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/legion/extensions/agentic/inference/counterfactual/runners/counterfactual.rb', line 48

def resolve_counterfactual(scenario_id:, lesson:, **)
  scenario = engine.resolve(scenario_id: scenario_id, lesson: lesson)
  if scenario
    log.info "[counterfactual] resolved: id=#{scenario_id[0..7]} lesson=#{lesson[0..40]}"
    { success: true, scenario: scenario.to_h }
  else
    log.debug "[counterfactual] resolve failed: id=#{scenario_id[0..7]} not found"
    { success: false, reason: :not_found }
  end
end

#update_counterfactualObject



83
84
85
86
87
# File 'lib/legion/extensions/agentic/inference/counterfactual/runners/counterfactual.rb', line 83

def update_counterfactual(**)
  engine.regret_decay
  log.debug '[counterfactual] regret decay applied'
  { success: true, action: :regret_decay }
end