Class: Legion::Extensions::Agentic::Inference::ExpectationViolation::Helpers::Expectation

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb

Constant Summary

Constants included from Constants

Constants::ADAPTATION_RATE, Constants::AROUSAL_BASE, Constants::AROUSAL_LABELS, Constants::AROUSAL_MULTIPLIER, Constants::DECAY_RATE, Constants::DEFAULT_EXPECTATION, Constants::EXPECTATION_CEILING, Constants::EXPECTATION_FLOOR, Constants::MAX_EXPECTATIONS, Constants::MAX_HISTORY, Constants::MAX_VIOLATIONS, Constants::VIOLATION_LABELS, Constants::VIOLATION_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, domain:, expected_value: DEFAULT_EXPECTATION, tolerance: 0.2) ⇒ Expectation

Returns a new instance of Expectation.



17
18
19
20
21
22
23
24
25
26
# File 'lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb', line 17

def initialize(context:, domain:, expected_value: DEFAULT_EXPECTATION, tolerance: 0.2)
  @id               = SecureRandom.uuid
  @context          = context
  @domain           = domain
  @expected_value   = expected_value.clamp(EXPECTATION_FLOOR, EXPECTATION_CEILING)
  @tolerance        = tolerance.clamp(0.01, 0.5)
  @violation_count  = 0
  @adaptation_count = 0
  @created_at       = Time.now.utc
end

Instance Attribute Details

#adaptation_countObject (readonly)

Returns the value of attribute adaptation_count.



14
15
16
# File 'lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb', line 14

def adaptation_count
  @adaptation_count
end

#contextObject (readonly)

Returns the value of attribute context.



14
15
16
# File 'lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb', line 14

def context
  @context
end

#created_atObject (readonly)

Returns the value of attribute created_at.



14
15
16
# File 'lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb', line 14

def created_at
  @created_at
end

#domainObject (readonly)

Returns the value of attribute domain.



14
15
16
# File 'lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb', line 14

def domain
  @domain
end

#expected_valueObject (readonly)

Returns the value of attribute expected_value.



14
15
16
# File 'lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb', line 14

def expected_value
  @expected_value
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb', line 14

def id
  @id
end

#toleranceObject (readonly)

Returns the value of attribute tolerance.



14
15
16
# File 'lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb', line 14

def tolerance
  @tolerance
end

#violation_countObject (readonly)

Returns the value of attribute violation_count.



14
15
16
# File 'lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb', line 14

def violation_count
  @violation_count
end

Instance Method Details

#adapt!(actual_value:) ⇒ Object



44
45
46
47
48
# File 'lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb', line 44

def adapt!(actual_value:)
  @expected_value += (actual_value - @expected_value) * ADAPTATION_RATE
  @expected_value = @expected_value.clamp(EXPECTATION_FLOOR, EXPECTATION_CEILING)
  @adaptation_count += 1
end

#evaluate(actual_value:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb', line 28

def evaluate(actual_value:)
  deviation = actual_value - @expected_value
  violated = deviation.abs > @tolerance

  if violated
    @violation_count += 1
    arousal = compute_arousal(deviation)
    violation_type = deviation.positive? ? :positive : :negative
    { violated: true, deviation: deviation.round(3), type: violation_type,
      arousal: arousal.round(3), arousal_label: arousal_label(arousal) }
  else
    { violated: false, deviation: deviation.round(3), type: :neutral,
      arousal: 0.0, arousal_label: :unaffected }
  end
end

#to_hObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb', line 54

def to_h
  {
    id:               @id,
    context:          @context,
    domain:           @domain,
    expected_value:   @expected_value.round(3),
    tolerance:        @tolerance,
    violation_count:  @violation_count,
    adaptation_count: @adaptation_count,
    created_at:       @created_at
  }
end

#violation_label(deviation) ⇒ Object



50
51
52
# File 'lib/legion/extensions/agentic/inference/expectation_violation/helpers/expectation.rb', line 50

def violation_label(deviation)
  VIOLATION_LABELS.find { |range, _| range.cover?(deviation) }&.last || :neutral
end