Class: Legion::Extensions::Agentic::Inference::ExpectationViolation::Helpers::Expectation
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Inference::ExpectationViolation::Helpers::Expectation
- 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
-
#adaptation_count ⇒ Object
readonly
Returns the value of attribute adaptation_count.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#expected_value ⇒ Object
readonly
Returns the value of attribute expected_value.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#tolerance ⇒ Object
readonly
Returns the value of attribute tolerance.
-
#violation_count ⇒ Object
readonly
Returns the value of attribute violation_count.
Instance Method Summary collapse
- #adapt!(actual_value:) ⇒ Object
- #evaluate(actual_value:) ⇒ Object
-
#initialize(context:, domain:, expected_value: DEFAULT_EXPECTATION, tolerance: 0.2) ⇒ Expectation
constructor
A new instance of Expectation.
- #to_h ⇒ Object
- #violation_label(deviation) ⇒ Object
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_count ⇒ Object (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 |
#context ⇒ Object (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_at ⇒ Object (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 |
#domain ⇒ Object (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_value ⇒ Object (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 |
#id ⇒ Object (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 |
#tolerance ⇒ Object (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_count ⇒ Object (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_h ⇒ Object
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 |