Class: Legion::Extensions::Agentic::Inference::Prediction::Helpers::PredictionStore

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identity: nil) ⇒ PredictionStore

Returns a new instance of PredictionStore.



14
15
16
17
18
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 14

def initialize(identity: nil)
  @identity = identity
  @predictions = {}
  @outcomes = []
end

Instance Attribute Details

#identityObject (readonly)

Returns the value of attribute identity.



12
13
14
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 12

def identity
  @identity
end

#outcomesObject (readonly)

Returns the value of attribute outcomes.



12
13
14
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 12

def outcomes
  @outcomes
end

#predictionsObject (readonly)

Returns the value of attribute predictions.



12
13
14
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 12

def predictions
  @predictions
end

Instance Method Details

#accuracy(window: 100) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 50

def accuracy(window: 100)
  recent = @outcomes.last(window)
  return 0.0 if recent.empty?

  correct = recent.count { |o| o[:outcome] == :correct }
  correct.to_f / recent.size
end

#countObject



58
59
60
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 58

def count
  @predictions.size
end

#erase_partner!(identity:) ⇒ Object

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
90
91
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 83

def erase_partner!(identity:)
  raise ArgumentError, "identity mismatch: expected #{@identity.inspect}, got #{identity.inspect}" if @identity != identity

  predictions_erased = @predictions.size
  outcomes_erased = @outcomes.size
  @predictions.clear
  @outcomes.clear
  { erased: true, identity: identity, predictions_erased: predictions_erased, outcomes_erased: outcomes_erased }
end

#get(prediction_id) ⇒ Object



29
30
31
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 29

def get(prediction_id)
  @predictions[prediction_id]
end

#pendingObject



46
47
48
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 46

def pending
  @predictions.values.select { |p| p[:status] == :pending }
end

#recently_resolved(limit: 20) ⇒ Object



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

def recently_resolved(limit: 20)
  @outcomes.last(limit).filter_map do |entry|
    pred = @predictions[entry[:prediction_id]]
    next unless pred

    {
      prediction_id:  entry[:prediction_id],
      domain:         pred[:mode],
      outcome_domain: pred.dig(:context, :outcome_domain) || pred[:mode],
      outcome:        entry[:outcome],
      accurate:       entry[:outcome] == :correct,
      confidence:     pred[:confidence],
      resolved_at:    entry[:at]
    }
  end
end

#resolve(prediction_id, outcome:, actual: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 33

def resolve(prediction_id, outcome:, actual: nil)
  pred = @predictions[prediction_id]
  return nil unless pred

  pred[:status] = outcome # :correct, :incorrect, :partial, :expired
  pred[:resolved_at] = Time.now.utc
  pred[:actual] = actual

  @outcomes << { prediction_id: prediction_id, outcome: outcome, at: Time.now.utc }
  @outcomes.shift while @outcomes.size > 500
  pred
end

#resolved_countObject



62
63
64
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 62

def resolved_count
  @predictions.values.count { |p| p[:status] != :pending }
end

#store(prediction) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/legion/extensions/agentic/inference/prediction/helpers/prediction_store.rb', line 20

def store(prediction)
  id = prediction[:prediction_id] || SecureRandom.uuid
  prediction[:prediction_id] = id
  prediction[:created_at] ||= Time.now.utc
  prediction[:status] ||= :pending
  @predictions[id] = prediction
  id
end