Module: Legion::Extensions::Agentic::Inference::PredictiveCoding::Runners::PredictiveCoding

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

Instance Method Summary collapse

Instance Method Details

#active_inference_candidatesObject



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

def active_inference_candidates(**)
  candidates = generative_model.active_inference_candidates
  log.debug "[predictive_coding] active_inference_candidates count=#{candidates.size}"
  { success: true, candidates: candidates, count: candidates.size }
end

#free_energy_statusObject



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

def free_energy_status(**)
  fe    = generative_model.free_energy
  level = generative_model.free_energy_level
  log.debug "[predictive_coding] free_energy_status fe=#{fe.round(3)} level=#{level}"
  {
    success:     true,
    free_energy: fe,
    level:       level,
    model_stats: generative_model.to_h
  }
end

#generate_prediction(domain:, context: {}) ⇒ Object



15
16
17
18
19
20
# File 'lib/legion/extensions/agentic/inference/predictive_coding/runners/predictive_coding.rb', line 15

def generate_prediction(domain:, context: {}, **)
  prediction = generative_model.predict(domain: domain, context: context)
  log.debug "[predictive_coding] generate_prediction domain=#{domain} " \
            "predicted=#{prediction[:predicted]} confidence=#{prediction[:confidence].round(3)}"
  { success: true, domain: domain, predicted: prediction[:predicted], confidence: prediction[:confidence] }
end

#precision_for(domain:) ⇒ Object



37
38
39
40
41
# File 'lib/legion/extensions/agentic/inference/predictive_coding/runners/predictive_coding.rb', line 37

def precision_for(domain:, **)
  value = generative_model.precision_for(domain: domain)
  log.debug "[predictive_coding] precision_for domain=#{domain} precision=#{value.round(3)}"
  { success: true, domain: domain, precision: value }
end

#predictive_coding_statsObject



122
123
124
125
126
127
128
129
# File 'lib/legion/extensions/agentic/inference/predictive_coding/runners/predictive_coding.rb', line 122

def predictive_coding_stats(**)
  {
    success:            true,
    model:              generative_model.to_h,
    active_inferences:  active_inferences.size,
    pending_inferences: active_inferences.count { |_, v| v[:status] == :pending }
  }
end

#register_active_inference(domain:, action:, expected_outcome:) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/legion/extensions/agentic/inference/predictive_coding/runners/predictive_coding.rb', line 67

def register_active_inference(domain:, action:, expected_outcome:, **)
  inference_id = SecureRandom.uuid
  active_inferences[inference_id] = {
    inference_id:     inference_id,
    domain:           domain,
    action:           action,
    expected_outcome: expected_outcome,
    status:           :pending,
    registered_at:    Time.now.utc
  }

  prune_active_inferences

  log.debug "[predictive_coding] register_active_inference domain=#{domain} id=#{inference_id[0..7]}"
  { success: true, inference_id: inference_id, domain: domain, status: :pending }
end

#report_outcome(domain:, predicted:, actual:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/legion/extensions/agentic/inference/predictive_coding/runners/predictive_coding.rb', line 22

def report_outcome(domain:, predicted:, actual:, **)
  error = generative_model.update(domain: domain, predicted: predicted, actual: actual)
  log.debug "[predictive_coding] report_outcome domain=#{domain} " \
            "error_magnitude=#{error.error_magnitude.round(3)} surprising=#{error.surprising?}"
  {
    success:         true,
    domain:          domain,
    error_magnitude: error.error_magnitude,
    weighted_error:  error.weighted_error,
    precision:       error.precision,
    surprising:      error.surprising?,
    level:           error.level
  }
end

#resolve_active_inference(domain:, action:, actual_outcome:, inference_id: nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/legion/extensions/agentic/inference/predictive_coding/runners/predictive_coding.rb', line 84

def resolve_active_inference(domain:, action:, actual_outcome:, inference_id: nil, **)
  record = find_inference(domain, action, inference_id)
  unless record
    log.debug "[predictive_coding] resolve_active_inference not found domain=#{domain}"
    return { success: false, reason: :not_found }
  end

  expected = record[:expected_outcome]
  error    = generative_model.update(
    domain:    domain,
    predicted: expected,
    actual:    actual_outcome
  )

  record[:status]           = :resolved
  record[:actual_outcome]   = actual_outcome
  record[:resolved_at]      = Time.now.utc
  record[:error_magnitude]  = error.error_magnitude

  log.info "[predictive_coding] resolve_active_inference domain=#{domain} " \
           "error=#{error.error_magnitude.round(3)} id=#{record[:inference_id][0..7]}"

  {
    success:         true,
    inference_id:    record[:inference_id],
    domain:          domain,
    error_magnitude: error.error_magnitude,
    action_helpful:  error.error_magnitude < Legion::Extensions::Agentic::Inference::PredictiveCoding::Helpers::Constants::SURPRISE_THRESHOLD
  }
end

#surprising_errorsObject



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

def surprising_errors(**)
  errors = generative_model.surprising_errors
  log.debug "[predictive_coding] surprising_errors count=#{errors.size}"
  { success: true, errors: errors.map(&:to_h), count: errors.size }
end

#update_predictive_codingObject



115
116
117
118
119
120
# File 'lib/legion/extensions/agentic/inference/predictive_coding/runners/predictive_coding.rb', line 115

def update_predictive_coding(**)
  generative_model.decay_all
  pruned = prune_resolved_inferences
  log.debug "[predictive_coding] update_predictive_coding pruned_inferences=#{pruned}"
  { success: true, pruned_inferences: pruned }
end