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

#expire_stale_predictionsObject



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

def expire_stale_predictions(**)
  expired_count = 0

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

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

  remaining = prediction_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:) ⇒ Object



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

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

#pending_predictionsObject



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

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

#predict(mode:, context: {}, confidence: nil, description: 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
# File 'lib/legion/extensions/agentic/inference/prediction/runners/prediction.rb', line 15

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

  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
  }

  prediction_store.store(prediction)

  actionable = prediction[:confidence] >= Helpers::Modes::PREDICTION_CONFIDENCE_MIN
  rolling_accuracy = prediction_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:         prediction_store.recently_resolved
  }
end

#prediction_accuracy(window: 100) ⇒ Object



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

def prediction_accuracy(window: 100, **)
  acc = prediction_store.accuracy(window: window)
  total = prediction_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) ⇒ Object



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

def resolve_prediction(prediction_id:, outcome:, actual: nil, **)
  pred = prediction_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