Class: Legion::Extensions::Agentic::Inference::UncertaintyTolerance::Helpers::ToleranceEngine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_tolerance: Constants::DEFAULT_TOLERANCE) ⇒ ToleranceEngine

Returns a new instance of ToleranceEngine.



12
13
14
15
16
17
18
19
# File 'lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb', line 12

def initialize(initial_tolerance: Constants::DEFAULT_TOLERANCE)
  @current_tolerance = initial_tolerance.clamp(
    Constants::TOLERANCE_FLOOR,
    Constants::TOLERANCE_CEILING
  )
  @decisions = {}
  @history   = []
end

Instance Attribute Details

#current_toleranceObject (readonly)

Returns the value of attribute current_tolerance.



10
11
12
# File 'lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb', line 10

def current_tolerance
  @current_tolerance
end

#decisionsObject (readonly)

Returns the value of attribute decisions.



10
11
12
# File 'lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb', line 10

def decisions
  @decisions
end

#historyObject (readonly)

Returns the value of attribute history.



10
11
12
# File 'lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb', line 10

def history
  @history
end

Instance Method Details

#comfort_zone_expansion_rateObject



80
81
82
83
84
85
86
87
# File 'lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb', line 80

def comfort_zone_expansion_rate
  return 0.0 if @history.size < 2

  tolerances = @history.last(10).map { |h| h[:tolerance_snapshot] }
  return 0.0 if tolerances.size < 2

  (tolerances.last - tolerances.first) / (tolerances.size - 1).to_f
end

#decisions_under_uncertainty(threshold: nil) ⇒ Object



50
51
52
53
# File 'lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb', line 50

def decisions_under_uncertainty(threshold: nil)
  cutoff = threshold || @current_tolerance
  @decisions.values.select { |d| d.certainty_level < cutoff }
end

#domain_tolerance(domain:) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb', line 67

def domain_tolerance(domain:)
  resolved = @decisions.values.select do |d|
    d.domain == domain && d.successful? && !d.actual_outcome.nil?
  end
  return nil if resolved.empty?

  resolved.sum(&:certainty_level) / resolved.size
end

#record_decision(description:, certainty_level:, domain: :general) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb', line 28

def record_decision(description:, certainty_level:, domain: :general)
  decision = Decision.new(
    description:       description,
    domain:            domain,
    certainty_level:   certainty_level,
    tolerance_at_time: @current_tolerance
  )
  @decisions[decision.id] = decision
  prune_decisions
  decision
end

#resolve_decision(decision_id:, outcome:) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb', line 40

def resolve_decision(decision_id:, outcome:)
  decision = @decisions[decision_id]
  return nil unless decision

  decision.resolve!(outcome: outcome)
  record_history(decision)
  adapt_tolerance(decision)
  decision
end

#risk_profileObject



61
62
63
64
65
# File 'lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb', line 61

def risk_profile
  breakdown = Constants::DECISION_TYPES.to_h { |t| [t, 0] }
  @decisions.each_value { |d| breakdown[d.decision_type] += 1 }
  breakdown
end

#should_act?(certainty:) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb', line 76

def should_act?(certainty:)
  certainty >= @current_tolerance
end

#successful_uncertain_decisionsObject



55
56
57
58
59
# File 'lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb', line 55

def successful_uncertain_decisions
  @decisions.values.select do |d|
    d.acted_despite_uncertainty && d.successful?
  end
end

#to_hObject



89
90
91
92
93
94
95
96
97
# File 'lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb', line 89

def to_h
  {
    current_tolerance: @current_tolerance,
    tolerance_label:   tolerance_label,
    total_decisions:   @decisions.size,
    risk_profile:      risk_profile,
    history_count:     @history.size
  }
end

#tolerance_labelObject



21
22
23
24
25
26
# File 'lib/legion/extensions/agentic/inference/uncertainty_tolerance/helpers/tolerance_engine.rb', line 21

def tolerance_label
  Constants::TOLERANCE_LABELS.each do |range, label|
    return label if range.cover?(@current_tolerance)
  end
  :unknown
end