Module: Legion::Extensions::Agentic::Inference::Prediction::Runners::Prediction

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

Instance Method Summary collapse

Instance Method Details

#erase_partner!(identity:) ⇒ Object



100
101
102
103
104
105
# File 'lib/legion/extensions/agentic/inference/prediction/runners/prediction.rb', line 100

def erase_partner!(identity:, **)
  store = prediction_store(identity: identity)
  result = store.erase_partner!(identity: identity)
  log.info "[prediction] erased partner identity=#{identity}"
  result
end

#expire_stale_predictions(identity: nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/legion/extensions/agentic/inference/prediction/runners/prediction.rb', line 76

def expire_stale_predictions(identity: nil, **)
  store = prediction_store(identity: identity)
  expired_count = 0

  store.pending.each do |pred|
    age = Time.now.utc - pred[:created_at]
    next unless age > pred[:horizon]

    store.resolve(pred[:prediction_id], outcome: :expired, actual: nil)
    expired_count += 1
  end

  remaining = store.pending.size
  log.debug "[prediction] expire sweep: expired=#{expired_count} remaining=#{remaining}"

  { expired_count: expired_count, remaining_pending: remaining }
end

#get_prediction(prediction_id:, identity: nil) ⇒ Object



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

def get_prediction(prediction_id:, identity: nil, **)
  store = prediction_store(identity: identity)
  pred = store.get(prediction_id)
  pred ? { found: true, prediction: pred } : { found: false }
end

#pending_predictions(identity: nil) ⇒ Object



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

def pending_predictions(identity: nil, **)
  store = prediction_store(identity: identity)
  preds = store.pending
  log.debug "[prediction] pending count=#{preds.size}"
  { predictions: preds, count: preds.size }
end

#predict(mode:, context: {}, confidence: nil, description: nil, identity: nil) ⇒ Object



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
41
42
43
44
45
46
# File 'lib/legion/extensions/agentic/inference/prediction/runners/prediction.rb', line 15

def predict(mode:, context: {}, confidence: nil, description: nil, identity: nil, **)
  return { error: :invalid_mode, valid_modes: Helpers::Modes::REASONING_MODES } unless Helpers::Modes.valid_mode?(mode)

  store = prediction_store(identity: identity)
  prediction = {
    prediction_id: SecureRandom.uuid,
    mode:          mode,
    context:       context,
    confidence:    confidence || estimate_confidence(mode, context),
    description:   description,
    status:        :pending,
    created_at:    Time.now.utc,
    horizon:       Helpers::Modes::PREDICTION_HORIZON
  }

  store.store(prediction)

  actionable = prediction[:confidence] >= Helpers::Modes::PREDICTION_CONFIDENCE_MIN
  rolling_accuracy = store.accuracy
  log.debug "[prediction] new: mode=#{mode} confidence=#{prediction[:confidence].round(2)} " \
            "actionable=#{actionable} accuracy=#{rolling_accuracy.round(2)} id=#{prediction[:prediction_id][0..7]}"

  {
    prediction_id:    prediction[:prediction_id],
    mode:             mode,
    confidence:       prediction[:confidence],
    actionable:       actionable,
    rolling_accuracy: rolling_accuracy,
    error_rate:       (1.0 - rolling_accuracy).round(4),
    resolved:         store.recently_resolved
  }
end

#prediction_accuracy(window: 100, identity: nil) ⇒ Object



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

def prediction_accuracy(window: 100, identity: nil, **)
  store = prediction_store(identity: identity)
  acc = store.accuracy(window: window)
  total = store.outcomes.size
  log.debug "[prediction] accuracy=#{acc.round(2)} total_outcomes=#{total}"
  { accuracy: acc, total_outcomes: total }
end

#resolve_prediction(prediction_id:, outcome:, actual: nil, identity: nil) ⇒ Object



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

def resolve_prediction(prediction_id:, outcome:, actual: nil, identity: nil, **)
  store = prediction_store(identity: identity)
  pred = store.resolve(prediction_id, outcome: outcome, actual: actual)
  if pred
    log.info "[prediction] resolved #{prediction_id[0..7]} outcome=#{outcome}"
    record_outcome_trace(pred, outcome)
    { resolved: true, prediction_id: prediction_id, outcome: outcome }
  else
    log.debug "[prediction] resolve failed: #{prediction_id[0..7]} not found"
    { resolved: false, reason: :not_found }
  end
end