File size: 9,303 Bytes
c8d0576
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""

Debugging agent - analyzes and fixes code errors.

"""

import re
from typing import Dict, Any, List, Optional, Tuple

from reproagent.models import LLMClient


class Debugger:
    """

    Debugging agent that:

    1. Analyzes error messages

    2. Searches for solutions

    3. Proposes fixes

    4. Applies patches

    """
    
    def __init__(self, llm_client: LLMClient):
        """

        Args:

            llm_client: LLM for error analysis

        """
        self.llm = llm_client
        
        # Common error patterns
        self.error_patterns = {
            'ImportError': r'ImportError: No module named [\'"](.+)[\'"]',
            'ModuleNotFoundError': r'ModuleNotFoundError: No module named [\'"](.+)[\'"]',
            'FileNotFoundError': r'FileNotFoundError: \[Errno 2\] No such file or directory: [\'"](.+)[\'"]',
            'RuntimeError': r'RuntimeError: (.+)',
            'ValueError': r'ValueError: (.+)',
            'TypeError': r'TypeError: (.+)',
            'AttributeError': r'AttributeError: (.+)',
        }
    
    def analyze_error(self, error_message: str, code_context: Optional[str] = None) -> Dict[str, Any]:
        """

        Analyze error and determine cause.

        

        Args:

            error_message: Full error message/traceback

            code_context: Relevant code snippet (optional)

            

        Returns:

            Analysis dict with error type, cause, and suggested fixes

        """
        print(f"🔍 Analyzing error...")
        
        # Classify error type
        error_type = self._classify_error(error_message)
        
        # Extract error details
        error_details = self._extract_error_details(error_message, error_type)
        
        # Get LLM analysis
        llm_analysis = self._llm_analyze_error(error_message, code_context)
        
        analysis = {
            'error_type': error_type,
            'error_details': error_details,
            'root_cause': llm_analysis.get('root_cause', 'Unknown'),
            'suggested_fixes': llm_analysis.get('fixes', []),
            'confidence': llm_analysis.get('confidence', 0.5)
        }
        
        print(f"✅ Error analyzed: {error_type}")
        print(f"   Cause: {analysis['root_cause']}")
        
        return analysis
    
    def _classify_error(self, error_message: str) -> str:
        """Classify error type."""
        for error_type, pattern in self.error_patterns.items():
            if re.search(pattern, error_message):
                return error_type
        
        # Check for common error types in message
        if 'import' in error_message.lower():
            return 'ImportError'
        elif 'file' in error_message.lower() and 'not found' in error_message.lower():
            return 'FileNotFoundError'
        elif 'cuda' in error_message.lower() or 'gpu' in error_message.lower():
            return 'CUDAError'
        elif 'memory' in error_message.lower():
            return 'MemoryError'
        
        return 'UnknownError'
    
    def _extract_error_details(self, error_message: str, error_type: str) -> Dict[str, str]:
        """Extract specific details from error."""
        details = {}
        
        if error_type in self.error_patterns:
            pattern = self.error_patterns[error_type]
            match = re.search(pattern, error_message)
            if match:
                details['detail'] = match.group(1)
        
        # Extract file and line number
        file_pattern = r'File "(.+)", line (\d+)'
        file_match = re.search(file_pattern, error_message)
        if file_match:
            details['file'] = file_match.group(1)
            details['line'] = file_match.group(2)
        
        return details
    
    def _llm_analyze_error(self, error_message: str, code_context: Optional[str]) -> Dict[str, Any]:
        """Use LLM to analyze error."""
        
        prompt = f"""

Analyze this Python error and provide solutions.



Error:

{error_message[:1000]}

"""
        
        if code_context:
            prompt += f"\n\nRelevant code:\n{code_context[:500]}"
        
        prompt += """



Respond with JSON:

{

    "root_cause": "explanation of what caused the error",

    "fixes": ["fix 1", "fix 2", "fix 3"],

    "confidence": 0.9

}

"""
        
        try:
            result = self.llm.generate_structured(prompt)
            return result
        except:
            return self._fallback_analysis(error_message)
    
    def _fallback_analysis(self, error_message: str) -> Dict[str, Any]:
        """Fallback analysis without LLM."""
        
        # Common fixes for common errors
        fixes = []
        
        if 'ModuleNotFoundError' in error_message or 'ImportError' in error_message:
            match = re.search(r"module named ['\"](.+)['\"]", error_message)
            if match:
                module = match.group(1)
                fixes = [
                    f"Install missing package: pip install {module}",
                    f"Check if {module} is in requirements.txt",
                    "Activate correct virtual environment"
                ]
        
        elif 'FileNotFoundError' in error_message:
            fixes = [
                "Check if file path is correct",
                "Ensure data is downloaded",
                "Check working directory"
            ]
        
        elif 'CUDA' in error_message or 'GPU' in error_message:
            fixes = [
                "Check CUDA installation",
                "Verify GPU availability",
                "Try running on CPU: device='cpu'"
            ]
        
        elif 'memory' in error_message.lower():
            fixes = [
                "Reduce batch size",
                "Use gradient accumulation",
                "Clear GPU cache: torch.cuda.empty_cache()"
            ]
        
        return {
            'root_cause': 'Error detected',
            'fixes': fixes or ['Debug manually'],
            'confidence': 0.6
        }
    
    def generate_fix(self, error_analysis: Dict[str, Any]) -> str:
        """

        Generate code fix based on error analysis.

        

        Args:

            error_analysis: Output from analyze_error()

            

        Returns:

            Fix as code or command

        """
        error_type = error_analysis['error_type']
        details = error_analysis['error_details']
        
        # Generate specific fix based on error type
        if error_type in ['ImportError', 'ModuleNotFoundError']:
            module = details.get('detail', '')
            return f"pip install {module}"
        
        elif error_type == 'FileNotFoundError':
            file_path = details.get('detail', '')
            return f"# Check if {file_path} exists or download it"
        
        elif error_type == 'CUDAError':
            return "# Try: model.to('cpu') or install CUDA"
        
        elif error_type == 'MemoryError':
            return "# Reduce batch_size or use gradient accumulation"
        
        # Use LLM for complex fixes
        return self._llm_generate_fix(error_analysis)
    
    def _llm_generate_fix(self, error_analysis: Dict[str, Any]) -> str:
        """Use LLM to generate code fix."""
        
        prompt = f"""

Generate a code fix for this error:



Error Type: {error_analysis['error_type']}

Root Cause: {error_analysis['root_cause']}



Provide the fix as Python code or shell command.

"""
        
        try:
            fix = self.llm.generate(prompt, max_tokens=200)
            return fix.strip()
        except:
            return "# Manual fix required"
    
    def search_solution(self, error_message: str) -> List[str]:
        """

        Search for solutions to error.

        Simulates searching StackOverflow, documentation, etc.

        

        Args:

            error_message: Error message

            

        Returns:

            List of solution suggestions

        """
        # In full implementation, would search:
        # - StackOverflow API
        # - GitHub Issues
        # - Documentation
        
        # For now, use LLM to generate solutions
        prompt = f"""

This error occurred: {error_message[:500]}



List 3 common solutions to this error.

Respond with JSON:

{{

    "solutions": ["solution 1", "solution 2", "solution 3"]

}}

"""
        
        try:
            result = self.llm.generate_structured(prompt)
            return result.get('solutions', [])
        except:
            return ["Check dependencies", "Review code", "Search documentation"]


# Test
if __name__ == "__main__":
    from reproagent.models import LLMClient
    
    llm = LLMClient()
    debugger = Debugger(llm)
    
    # Test error
    error = """

Traceback (most recent call last):

  File "train.py", line 10, in <module>

    import torch

ModuleNotFoundError: No module named 'torch'

"""
    
    analysis = debugger.analyze_error(error)
    print(analysis)
    
    fix = debugger.generate_fix(analysis)
    print(f"\nFix: {fix}")