Class: Legion::Extensions::Agentic::Defense::ErrorMonitoring::Helpers::ErrorMonitor

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb

Constant Summary

Constants included from Constants

Constants::CONFIDENCE_ALPHA, Constants::CONFLICT_ALPHA, Constants::CONFLICT_THRESHOLD, Constants::CORRECTION_BOOST, Constants::DEFAULT_CONFIDENCE, Constants::DEFAULT_CONFLICT_LEVEL, Constants::DEFAULT_ERROR_RATE, Constants::ERROR_RATE_ALPHA, Constants::ERROR_SEVERITY_LABELS, Constants::MAX_CONFLICT_LOG, Constants::MAX_CORRECTIONS, Constants::MAX_ERROR_LOG, Constants::MAX_SLOWDOWN, Constants::MONITORING_STATE_LABELS, Constants::POST_ERROR_SLOWDOWN, Constants::SEVERE_ERROR_THRESHOLD, Constants::SLOWDOWN_DECAY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeErrorMonitor

Returns a new instance of ErrorMonitor.



15
16
17
18
19
20
21
22
23
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 15

def initialize
  @error_log      = []
  @conflict_log   = []
  @corrections    = []
  @error_rate     = DEFAULT_ERROR_RATE
  @conflict_level = DEFAULT_CONFLICT_LEVEL
  @confidence     = DEFAULT_CONFIDENCE
  @slowdown       = 0.0
end

Instance Attribute Details

#confidenceObject (readonly)

Returns the value of attribute confidence.



12
13
14
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 12

def confidence
  @confidence
end

#conflict_levelObject (readonly)

Returns the value of attribute conflict_level.



12
13
14
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 12

def conflict_level
  @conflict_level
end

#conflict_logObject (readonly)

Returns the value of attribute conflict_log.



12
13
14
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 12

def conflict_log
  @conflict_log
end

#correctionsObject (readonly)

Returns the value of attribute corrections.



12
13
14
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 12

def corrections
  @corrections
end

#error_logObject (readonly)

Returns the value of attribute error_log.



12
13
14
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 12

def error_log
  @error_log
end

#error_rateObject (readonly)

Returns the value of attribute error_rate.



12
13
14
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 12

def error_rate
  @error_rate
end

#slowdownObject (readonly)

Returns the value of attribute slowdown.



12
13
14
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 12

def slowdown
  @slowdown
end

Instance Method Details

#conflict_active?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 85

def conflict_active?
  @conflict_level >= CONFLICT_THRESHOLD
end

#correction_rateObject



110
111
112
113
114
115
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 110

def correction_rate
  return 0.0 if @error_log.empty?

  corrected = @error_log.count(&:corrected)
  corrected.to_f / @error_log.size
end

#error_countObject



106
107
108
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 106

def error_count
  @error_log.size
end

#errors_in(domain:) ⇒ Object



77
78
79
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 77

def errors_in(domain:)
  @error_log.select { |e| e.domain == domain }
end

#monitoring_stateObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 89

def monitoring_state
  if @error_rate > 0.5
    :overwhelmed
  elsif @slowdown > 0.1
    :vigilant
  elsif @error_rate < 0.05
    :relaxed
  else
    :normal
  end
end

#recent_errors(limit: 10) ⇒ Object



73
74
75
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 73

def recent_errors(limit: 10)
  @error_log.last(limit)
end

#register_conflict(action_a:, action_b:, domain:, intensity:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 46

def register_conflict(action_a:, action_b:, domain:, intensity:)
  entry = {
    action_a: action_a, action_b: action_b,
    domain: domain, intensity: intensity.to_f.clamp(0.0, 1.0),
    detected_at: Time.now.utc
  }
  @conflict_log << entry
  @conflict_log.shift while @conflict_log.size > MAX_CONFLICT_LOG

  update_conflict_level(intensity.to_f)
  entry
end

#register_correction(action:, domain:, original_error:, correction:) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 59

def register_correction(action:, domain:, original_error:, correction:)
  entry = {
    action: action, domain: domain,
    original_error: original_error, correction: correction,
    applied_at: Time.now.utc
  }
  @corrections << entry
  @corrections.shift while @corrections.size > MAX_CORRECTIONS

  mark_error_corrected(action, domain)
  update_confidence_boost
  entry
end

#register_error(action:, domain:, intended:, actual:, severity:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 25

def register_error(action:, domain:, intended:, actual:, severity:)
  signal = ErrorSignal.new(
    action: action, domain: domain,
    intended: intended, actual: actual, severity: severity
  )
  @error_log << signal
  @error_log.shift while @error_log.size > MAX_ERROR_LOG

  update_error_rate(severity.to_f)
  apply_post_error_slowdown(severity.to_f)
  update_confidence(correct: false)
  signal
end

#register_success(action:, domain:) ⇒ Object



39
40
41
42
43
44
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 39

def register_success(action:, domain:)
  update_error_rate(0.0)
  update_confidence(correct: true)
  decay_slowdown
  { action: action, domain: domain, error_rate: @error_rate.round(4) }
end

#tickObject



101
102
103
104
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 101

def tick
  decay_slowdown
  decay_conflict
end

#to_hObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 117

def to_h
  {
    error_rate:      @error_rate.round(4),
    conflict_level:  @conflict_level.round(4),
    confidence:      @confidence.round(4),
    slowdown:        @slowdown.round(4),
    state:           monitoring_state,
    state_label:     MONITORING_STATE_LABELS[monitoring_state],
    total_errors:    @error_log.size,
    uncorrected:     uncorrected_errors.size,
    correction_rate: correction_rate.round(4),
    conflict_active: conflict_active?
  }
end

#uncorrected_errorsObject



81
82
83
# File 'lib/legion/extensions/agentic/defense/error_monitoring/helpers/error_monitor.rb', line 81

def uncorrected_errors
  @error_log.reject(&:corrected)
end