Class: EnhanceSwarm::ErrorRecovery

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/enhance_swarm/error_recovery.rb

Constant Summary collapse

RECOVERY_STRATEGIES_FILE =
'.enhance_swarm/error_recovery_strategies.json'
ERROR_PATTERNS_FILE =
'.enhance_swarm/error_patterns.json'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeErrorRecovery

Returns a new instance of ErrorRecovery.



15
16
17
18
19
20
# File 'lib/enhance_swarm/error_recovery.rb', line 15

def initialize
  ensure_recovery_directory
  @recovery_strategies = load_recovery_strategies
  @error_patterns = load_error_patterns
  @recovery_history = []
end

Class Method Details

.analyze_error(*args) ⇒ Object



760
761
762
# File 'lib/enhance_swarm/error_recovery.rb', line 760

def analyze_error(*args)
  instance.analyze_error(*args)
end

.attempt_recovery(*args) ⇒ Object



764
765
766
# File 'lib/enhance_swarm/error_recovery.rb', line 764

def attempt_recovery(*args)
  instance.attempt_recovery(*args)
end

.cleanup_old_data(*args) ⇒ Object



780
781
782
# File 'lib/enhance_swarm/error_recovery.rb', line 780

def cleanup_old_data(*args)
  instance.cleanup_old_data(*args)
end

.explain_error(*args) ⇒ Object



768
769
770
# File 'lib/enhance_swarm/error_recovery.rb', line 768

def explain_error(*args)
  instance.explain_error(*args)
end

.learn_from_manual_recovery(*args) ⇒ Object



772
773
774
# File 'lib/enhance_swarm/error_recovery.rb', line 772

def learn_from_manual_recovery(*args)
  instance.learn_from_manual_recovery(*args)
end

.recovery_statisticsObject



776
777
778
# File 'lib/enhance_swarm/error_recovery.rb', line 776

def recovery_statistics
  instance.recovery_statistics
end

Instance Method Details

#analyze_error(error, context = {}) ⇒ Object

Analyze error and suggest recovery actions



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/enhance_swarm/error_recovery.rb', line 23

def analyze_error(error, context = {})
  error_info = {
    message: error.message,
    type: error.class.name,
    context: context,
    timestamp: Time.now.iso8601
  }

  # Find matching patterns
  matching_patterns = find_matching_patterns(error_info)
  
  # Generate recovery suggestions
  suggestions = generate_recovery_suggestions(error_info, matching_patterns)
  
  # Log error for pattern learning
  log_error_occurrence(error_info)
  
  {
    error: error_info,
    patterns: matching_patterns,
    suggestions: suggestions,
    auto_recoverable: auto_recoverable?(error_info, matching_patterns)
  }
end

#attempt_recovery(error_analysis, agent_context = {}) ⇒ Object

Attempt automatic recovery



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/enhance_swarm/error_recovery.rb', line 49

def attempt_recovery(error_analysis, agent_context = {})
  return false unless error_analysis[:auto_recoverable]

  recovery_attempts = []
  
  error_analysis[:suggestions].each do |suggestion|
    auto_executable = suggestion['auto_executable'] || suggestion[:auto_executable]
    next unless auto_executable
    
    begin
      description = suggestion['description'] || suggestion[:description]
      Logger.info("Attempting automatic recovery: #{description}")
      
      result = execute_recovery_action(suggestion, agent_context)
      
      recovery_attempts << {
        suggestion: suggestion,
        result: result,
        success: result[:success],
        timestamp: Time.now.iso8601
      }
      
      # If recovery succeeds, stop trying other strategies
      if result[:success]
        log_successful_recovery(error_analysis[:error], suggestion)
        return result
      end
      
    rescue StandardError => recovery_error
      Logger.error("Recovery attempt failed: #{recovery_error.message}")
      recovery_attempts << {
        suggestion: suggestion,
        result: { success: false, error: recovery_error.message },
        success: false,
        timestamp: Time.now.iso8601
      }
    end
  end
  
  # Log all recovery attempts for learning
  log_recovery_attempts(error_analysis[:error], recovery_attempts)
  
  { success: false, attempts: recovery_attempts }
end

#cleanup_old_data(days_to_keep = 30) ⇒ Object

Clear old recovery data



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/enhance_swarm/error_recovery.rb', line 172

def cleanup_old_data(days_to_keep = 30)
  cutoff_time = Time.now - (days_to_keep * 24 * 60 * 60)
  
  # Clean recovery history
  @recovery_history.reject! { |h| Time.parse(h[:timestamp]) < cutoff_time }
  
  # Clean old error patterns that haven't been seen recently
  @error_patterns.reject! do |_, pattern|
    last_seen = pattern['last_seen'] || pattern[:last_seen]
    last_seen && Time.parse(last_seen) < cutoff_time
  end
  
  save_error_patterns
  
  Logger.info("Cleaned up error recovery data older than #{days_to_keep} days")
end

#explain_error(error, context = {}) ⇒ Object

Get human-readable error explanation



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/enhance_swarm/error_recovery.rb', line 95

def explain_error(error, context = {})
  error_info = {
    message: error.message,
    type: error.class.name,
    context: context
  }

  matching_patterns = find_matching_patterns(error_info)
  
  if matching_patterns.any?
    primary_pattern = matching_patterns.first
    {
      explanation: primary_pattern[:explanation],
      likely_cause: primary_pattern[:likely_cause],
      prevention_tips: primary_pattern[:prevention_tips] || []
    }
  else
    generate_generic_explanation(error_info)
  end
end

#learn_from_manual_recovery(error, recovery_steps, context = {}) ⇒ Object

Learn from successful manual recovery



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/enhance_swarm/error_recovery.rb', line 117

def learn_from_manual_recovery(error, recovery_steps, context = {})
  error_info = {
    message: error.message,
    type: error.class.name,
    context: context,
    timestamp: Time.now.iso8601
  }

  # Create or update pattern
  pattern_key = generate_pattern_key(error_info)
  
  @error_patterns[pattern_key] ||= {
    'error_signatures' => [],
    'successful_recoveries' => [],
    'failure_rate' => 0.0,
    'last_seen' => nil
  }

  pattern = @error_patterns[pattern_key]
  
  # Add error signature if not already present
  signature = extract_error_signature(error_info)
  unless pattern['error_signatures'].any? { |sig| sig['message_pattern'] == signature[:message_pattern] }
    pattern['error_signatures'] << signature
  end

  # Add successful recovery
  pattern['successful_recoveries'] << {
    'steps' => recovery_steps,
    'context' => context,
    'timestamp' => Time.now.iso8601
  }

  pattern['last_seen'] = Time.now.iso8601
  
  save_error_patterns
  
  Logger.info("Learned new recovery pattern for #{error.class.name}")
end

#recovery_statisticsObject

Get recovery statistics



158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/enhance_swarm/error_recovery.rb', line 158

def recovery_statistics
  total_errors = @recovery_history.count
  successful_recoveries = @recovery_history.count { |h| h[:recovery_successful] }
  
  {
    total_errors_processed: total_errors,
    successful_automatic_recoveries: successful_recoveries,
    recovery_success_rate: total_errors > 0 ? (successful_recoveries.to_f / total_errors * 100).round(1) : 0.0,
    most_common_errors: most_common_error_types,
    recovery_patterns_learned: @error_patterns.count
  }
end