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
- #erase_partner!(identity:) ⇒ Object
- #estimate_confidence(mode, context) ⇒ Object
- #expire_stale_predictions(identity: nil) ⇒ Object
- #get_prediction(prediction_id:, identity: nil) ⇒ Object
- #pending_predictions(identity: nil) ⇒ Object
- #predict(mode:, context: {}, confidence: nil, description: nil, identity: nil) ⇒ Object
- #prediction_accuracy(window: 100, identity: nil) ⇒ Object
- #prediction_store(identity: nil) ⇒ Object
- #record_outcome_trace(prediction, outcome) ⇒ Object
- #resolve_prediction(prediction_id:, outcome:, actual: nil, identity: nil) ⇒ Object
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 |
#estimate_confidence(mode, context) ⇒ Object
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/legion/extensions/agentic/inference/prediction/runners/prediction.rb', line 112 def estimate_confidence(mode, context) base = case mode when :fault_localization then 0.7 when :functional_mapping then 0.6 when :counterfactual then 0.4 else 0.5 end richness_bonus = [context.size * 0.02, 0.2].min [base + richness_bonus, 1.0].min 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 |
#prediction_store(identity: nil) ⇒ Object
107 108 109 110 |
# File 'lib/legion/extensions/agentic/inference/prediction/runners/prediction.rb', line 107 def prediction_store(identity: nil) @prediction_stores ||= {} @prediction_stores[identity] ||= Helpers::PredictionStore.new(identity: identity) end |
#record_outcome_trace(prediction, outcome) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/legion/extensions/agentic/inference/prediction/runners/prediction.rb', line 123 def record_outcome_trace(prediction, outcome) return unless defined?(Legion::Extensions::Agentic::Memory::Trace::Runners::Traces) trace_params = case outcome when :correct { type: :semantic, valence: 0.3, intensity: 0.3, unresolved: false } when :incorrect { type: :episodic, valence: -0.5, intensity: 0.6, unresolved: true } when :partial { type: :episodic, valence: -0.2, intensity: 0.4, unresolved: true } else return end runner = Object.new.extend(Legion::Extensions::Agentic::Memory::Trace::Runners::Traces) runner.store_trace( type: trace_params[:type], content_payload: "prediction #{outcome}: mode=#{prediction[:mode]} confidence=#{prediction[:confidence]}", domain_tags: ['prediction', prediction[:mode].to_s], origin: :direct_experience, emotional_valence: trace_params[:valence], emotional_intensity: trace_params[:intensity], unresolved: trace_params[:unresolved], confidence: prediction[:confidence] ) store = runner.send(:default_store) store.flush if store.respond_to?(:flush) log.debug "[prediction] created #{trace_params[:type]} trace for #{outcome} prediction" rescue StandardError => e log.warn "[prediction] failed to create outcome trace: #{e.}" 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 |