Class: Legion::Extensions::Agentic::Inference::Debugging::Helpers::DebuggingEngine

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDebuggingEngine

Returns a new instance of DebuggingEngine.



12
13
14
15
16
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 12

def initialize
  @errors      = {}
  @traces      = {}
  @corrections = {}
end

Instance Attribute Details

#correctionsObject (readonly)

Returns the value of attribute corrections.



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

def corrections
  @corrections
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#tracesObject (readonly)

Returns the value of attribute traces.



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

def traces
  @traces
end

Instance Method Details

#active_errorsObject



78
79
80
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 78

def active_errors
  @errors.values.select(&:active?)
end

#apply_correction(correction_id:) ⇒ Object



57
58
59
60
61
62
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 57

def apply_correction(correction_id:)
  correction = @corrections[correction_id]
  return nil unless correction

  correction.apply!
end

#correction_success_rateObject



117
118
119
120
121
122
123
124
125
126
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 117

def correction_success_rate
  applied = @corrections.values.select(&:applied)
  return 0.0 if applied.empty?

  measured = applied.reject { |c| c.effectiveness.nil? }
  return 0.0 if measured.empty?

  effective_count = measured.count(&:effective?)
  (effective_count.to_f / measured.size).round(10)
end

#debugging_reportObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 128

def debugging_report
  {
    total_errors:            @errors.size,
    active_errors:           active_errors.size,
    resolved_errors:         resolved_errors.size,
    total_traces:            @traces.size,
    total_corrections:       @corrections.size,
    correction_success_rate: correction_success_rate,
    most_common_error_type:  most_common_error_type,
    most_effective_strategy: most_effective_strategy,
    errors_by_type:          errors_by_type,
    error_rate_by_phase:     error_rate_by_phase
  }
end

#detect_error(error_type:, description:, severity:, source_phase:, confidence_at_detection:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 18

def detect_error(error_type:, description:, severity:, source_phase:, confidence_at_detection:)
  return nil unless Constants::ERROR_TYPES.include?(error_type)
  return nil if @errors.size >= Constants::MAX_ERRORS

  err = ReasoningError.new(
    error_type:              error_type,
    description:             description,
    severity:                severity,
    source_phase:            source_phase,
    confidence_at_detection: confidence_at_detection
  )
  @errors[err.id] = err
  err
end

#error_rate_by_phaseObject



113
114
115
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 113

def error_rate_by_phase
  errors_by_phase
end

#errors_by_phaseObject



90
91
92
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 90

def errors_by_phase
  @errors.values.group_by(&:source_phase).transform_values(&:length)
end

#errors_by_typeObject



86
87
88
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 86

def errors_by_type
  @errors.values.group_by(&:error_type).transform_values(&:length)
end

#measure_correction(correction_id:, effectiveness:) ⇒ Object



64
65
66
67
68
69
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 64

def measure_correction(correction_id:, effectiveness:)
  correction = @corrections[correction_id]
  return nil unless correction

  correction.measure_effectiveness!(effectiveness)
end

#most_common_error_typeObject



94
95
96
97
98
99
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 94

def most_common_error_type
  tally = errors_by_type
  return nil if tally.empty?

  tally.max_by { |_, count| count }&.first
end

#most_effective_strategyObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 101

def most_effective_strategy
  applied = @corrections.values.reject { |c| c.effectiveness.nil? }
  return nil if applied.empty?

  by_strategy = applied.group_by(&:strategy)
  best = by_strategy.max_by do |_, list|
    scores = list.map(&:effectiveness)
    scores.sum.round(10) / scores.size
  end
  best&.first
end

#propose_correction(error_id:, strategy:, description:) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 45

def propose_correction(error_id:, strategy:, description:)
  err = @errors[error_id]
  return nil unless err
  return nil unless Constants::CORRECTION_STRATEGIES.include?(strategy)
  return nil if @corrections.size >= Constants::MAX_CORRECTIONS

  correction = Correction.new(error_id: error_id, strategy: strategy, description: description)
  @corrections[correction.id] = correction
  err.correct!(correction.id)
  correction
end

#resolve_error(error_id:) ⇒ Object



71
72
73
74
75
76
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 71

def resolve_error(error_id:)
  err = @errors[error_id]
  return nil unless err

  err.resolve!
end

#resolved_errorsObject



82
83
84
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 82

def resolved_errors
  @errors.values.select(&:resolved?)
end

#to_hObject



143
144
145
146
147
148
149
# File 'lib/legion/extensions/agentic/inference/debugging/helpers/debugging_engine.rb', line 143

def to_h
  {
    errors:      @errors.transform_values(&:to_h),
    traces:      @traces.transform_values(&:to_h),
    corrections: @corrections.transform_values(&:to_h)
  }
end

#trace_error(error_id:, steps:, root_cause:, confidence:) ⇒ Object



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

def trace_error(error_id:, steps:, root_cause:, confidence:)
  err = @errors[error_id]
  return nil unless err
  return nil if @traces.size >= Constants::MAX_TRACES

  trace = CausalTrace.new(error_id: error_id, root_cause: root_cause, confidence: confidence)
  steps.each { |s| trace.add_step!(phase: s.fetch(:phase), description: s.fetch(:description)) }
  @traces[trace.id] = trace
  err.trace!(trace.id)
  trace
end