File size: 14,634 Bytes
e999a76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from fastapi import FastAPI, Query, Request, HTTPException
from fastapi.responses import HTMLResponse, JSONResponse
from pydantic import BaseModel
import os, json, re, logging
from openenv.core.env_server import create_fastapi_app
from environment import CodeBugEnvironment
from models import CodeReviewAction, CodeReviewObservation, CodeReviewState
from training_loop import run_training

try:
    from agent import review as agent_review
    HAS_LLM = True
except ImportError as e:
    HAS_LLM = False
    logger = logging.getLogger(__name__)
    logger.warning(f"Failed to import agent: {e}")

logger = logging.getLogger(__name__)

# Create the FastAPI app with OpenEnv integration
app = create_fastapi_app(
    CodeBugEnvironment,
    CodeReviewAction,
    CodeReviewObservation
)

# ============================================================================
# ENDPOINT: POST /api/train
# ============================================================================
@app.post("/api/train")
async def run_real_training(episodes: int = Query(5, description="Number of training episodes to run")):
    """Run the RL training loop for the specified number of episodes."""
    try:
        stats = run_training(num_episodes=episodes, verbose=False)
        return {"status": "ok", "stats": stats}
    except Exception as e:
        import traceback
        logger.error(f"Training failed: {e}\n{traceback.format_exc()}")
        return JSONResponse(
            status_code=500,
            content={"error": f"Training failed: {str(e)}", "details": traceback.format_exc()[:500]}
        )


# ============================================================================
# ENDPOINT: POST /api/review
# ============================================================================
class ReviewRequest(BaseModel):
    code: str
    language: str
    export_format: str = None

@app.post("/api/review")
async def review_code(request: ReviewRequest):
    """Analyze code and return ALL bug findings with confidence scores"""
    try:
        code = request.code.strip()
        lang = request.language.lower().strip()
        
        # Validation
        if not code:
            raise HTTPException(status_code=400, detail="Code cannot be empty")
        if not lang:
            raise HTTPException(status_code=400, detail="Language must be specified")
        if len(code) > 5000:
            raise HTTPException(status_code=400, detail="Code exceeds 5000 characters")
        
        supported_langs = ['python', 'javascript', 'typescript', 'java', 'go', 'cpp', 'c++']
        if lang not in supported_langs:
            raise HTTPException(
                status_code=400,
                detail=f"Language '{lang}' not supported. Supported: {', '.join(supported_langs)}"
            )
        
        # Analyze code
        logger.info(f"Analyzing {lang} code ({len(code)} chars)...")
        if HAS_LLM:
            results = analyze_code_with_llm(code, lang)
        else:
            logger.warning("LLM not available, using fallback analysis")
            results = analyze_code_comprehensively(code, lang)
        
        # Build response
        response = {
            "bugs_found": len(results),
            "bugs": results,
            "primary_bug": results[0] if results else None,
            "analysis_metadata": {
                "language": lang,
                "code_length": len(code),
                "lines_of_code": len(code.split('\n')),
                "average_confidence": round(sum(b.get("confidence", 0.5) for b in results) / max(len(results), 1), 2) if results else 1.0
            }
        }
        
        if not results:
            response["primary_bug"] = {
                "type": "approve",
                "category": "style",
                "severity": 1,
                "line_hint": None,
                "comment": "No obvious bugs detected.",
                "fix": "",
                "confidence": 1.0
            }
        
        if request.export_format == 'json':
            response['export_url'] = '/api/export'
        
        logger.info(f"Returned {len(results)} bugs for {lang}")
        return response
        
    except HTTPException:
        raise
    except Exception as e:
        import traceback
        error_trace = traceback.format_exc()
        logger.error(f"Review endpoint failed: {e}\n{error_trace}")

def analyze_code_with_llm(code: str, lang: str) -> list:
    try:
        bugs = agent_review(code)
        if not isinstance(bugs, list):
            bugs = []
        for bug in bugs:
            if "languages" not in bug:
                bug["languages"] = [lang]
        bugs.sort(key=lambda x: (x.get('severity', 0), x.get('confidence', 0)), reverse=True)
        logger.info(f"LLM found {len(bugs)} bugs in {lang} code")
        return bugs
    except Exception as e:
        logger.warning(f"LLM analysis failed ({type(e).__name__}: {e}). Using fallback pattern-based analysis.")
        return analyze_code_comprehensively(code, lang)

def analyze_code_comprehensively(code: str, lang: str) -> list:
    """Fallback pattern-based bug detection when LLM is unavailable"""
    bugs = []
    code_lower = code.lower()
    lines = code.split('\n')
    
    # SQL Injection
    if ('f"' in code or "f'" in code) and any(sql in code_lower for sql in ['select', 'insert', 'delete', 'update', 'from', 'where']) and '{' in code:
        bugs.append({
            "type": "bug", "category": "security", "severity": 5, "line_hint": "SQL query line",
            "comment": "SQL injection: f-string with variable interpolation in SQL query.",
            "fix": "Use parameterized queries: cursor.execute('SELECT * FROM users WHERE id = ?', (uid,))",
            "confidence": 0.95,
            "languages": ["python", "javascript", "typescript"]
        })
    
    # Hardcoded credentials
    if re.search(r'(password|passwd|pwd|secret|credential)\s*[=:]\s*["\'][^"\']{6,}["\']', code, re.I):
        bugs.append({
            "type": "bug", "category": "security", "severity": 5, "line_hint": "credential line",
            "comment": "Hardcoded password/credential detected.",
            "fix": "Use environment variables: os.getenv('DB_PASSWORD')",
            "confidence": 0.98,
            "languages": ["python", "javascript", "typescript", "java", "go", "cpp"]
        })
    
    # Code execution vulnerabilities
    if re.search(r'\b(eval|exec|compile)\s*\(', code) or 'pickle.load' in code or 'yaml.load(' in code:
        bugs.append({
            "type": "bug", "category": "security", "severity": 5, "line_hint": "eval/exec/pickle line",
            "comment": "Arbitrary code execution: eval/exec/pickle are unsafe with untrusted data.",
            "fix": "Use: json.loads(), ast.literal_eval(), or safe alternatives",
            "confidence": 0.99,
            "languages": ["python", "javascript"]
        })
    
    # Hardcoded API keys
    if re.search(r'(api_key|apikey|secret_key|token|bearer)\s*[=:]\s*["\'][a-zA-Z0-9_\-]{8,}["\']', code, re.I):
        bugs.append({
            "type": "bug", "category": "security", "severity": 4, "line_hint": "API key line",
            "comment": "Hardcoded API key/token detected.",
            "fix": "Use environment variables: api_key = os.getenv('API_KEY')",
            "confidence": 0.96,
            "languages": ["python", "javascript", "typescript", "java", "go"]
        })
    
    # Mutable default argument (Python)
    if lang == 'python' and re.search(r'def\s+\w+\s*\([^)]*=\s*\[\]|def\s+\w+\s*\([^)]*=\s*\{\}', code):
        bugs.append({
            "type": "bug", "category": "logic", "severity": 3, "line_hint": "function definition",
            "comment": "Mutable default argument: List/dict is shared across all function calls.",
            "fix": "Use None as default: def func(lst=None): if lst is None: lst = []",
            "confidence": 0.98,
            "languages": ["python"]
        })
    
    # Off-by-one errors
    if re.search(r'range\(len\([^)]+\)\s*-\s*1\)', code):
        bugs.append({
            "type": "bug", "category": "logic", "severity": 3, "line_hint": "loop range",
            "comment": "Off-by-one error: range(len(x)-1) skips the last element.",
            "fix": "Use range(len(arr)) or enumerate(arr)",
            "confidence": 0.85,
            "languages": ["python", "javascript", "java", "go", "cpp"]
        })
    
    # Division by zero
    if re.search(r'\/\s*0(?![a-zA-Z0-9])', code):
        bugs.append({
            "type": "bug", "category": "logic", "severity": 3, "line_hint": "division line",
            "comment": "Potential division by zero. Check denominator is non-zero.",
            "fix": "Add check: if denominator != 0: result = numerator / denominator",
            "confidence": 0.99,
            "languages": ["python", "javascript", "java", "go", "cpp"]
        })
    
    return bugs


# ============================================================================
# FRONTEND + HELPER ENDPOINTS
# ============================================================================

@app.get("/")
def serve_frontend():
    """Serve the CodeBug frontend UI"""
    html_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates", "index.html")
    try:
        with open(html_path, "r", encoding="utf-8") as f:
            return HTMLResponse(f.read())
    except:
        return HTMLResponse("<h1>CodeBug API is Running.</h1><p>Visit /docs to see available endpoints.</p>")


@app.get("/api/health")
async def health_check():
    """Health check endpoint"""
    return {
        "status": "ok",
        "version": "2.0",
        "supported_languages": ["python", "javascript", "typescript", "java", "go", "cpp"],
        "features": [
            "comprehensive_bug_detection",
            "confidence_scores",
            "multi_language_support",
            "export_json_reports"
        ],
        "api_endpoints": [
            "/api/review (POST) - Analyze code",
            "/api/train (POST) - Run training",
            "/api/health (GET) - This endpoint",
            "/api/stats (GET) - Usage statistics"
        ]
    }


@app.get("/api/stats")
async def get_stats():
    """Return usage statistics"""
    return {
        "message": "Stats endpoint - tracks would be stored in session/DB",
        "supported_check_types": 30,
        "languages": 6,
        "security_checks": 12,
        "logic_checks": 8,
        "style_checks": 10
    }

    
    # Resource leak
    if re.search(r'open\s*\([^)]+\)\s*(?!with|as)', code) and '.close()' not in code:
        bugs.append({
            "type": "bug", "category": "logic", "severity": 3, "line_hint": 2,
            "comment": "Resource leak: File opened without 'with' statement. May not close properly.",
            "fix": "Use: with open(file) as f: ... (automatic close)",
            "confidence": 0.93,
            "languages": ["python"]
        })
    
    # Infinite recursion (Python)
    if lang == 'python' and re.search(r'def\s+(\w+).*:\s*.*return\s+\1\s*\(', code, re.DOTALL):
        bugs.append({
            "type": "bug", "category": "logic", "severity": 3, "line_hint": 2,
            "comment": "Infinite recursion: Function calls itself without base case.",
            "fix": "Add base case: if condition: return value",
            "confidence": 0.87,
            "languages": ["python", "javascript", "java", "go", "cpp"]
        })
    
    # Bare except (Python)
    if lang == 'python' and re.search(r'except\s*:\s*(?!.*#)', code):
        bugs.append({
            "type": "bug", "category": "logic", "severity": 2, "line_hint": 2,
            "comment": "Bare except clause: Catches all exceptions including KeyboardInterrupt.",
            "fix": "Specify exception: except ValueError: or except Exception:",
            "confidence": 0.96,
            "languages": ["python"]
        })
    
    # Type errors
    if lang == 'python' and re.search(r'["\'][^"\']*["\'].*\+.*[0-9]|["\'][^"\']*["\'].*\+\s*\w+(?![\"\')])', code):
        bugs.append({
            "type": "bug", "category": "logic", "severity": 2, "line_hint": 2,
            "comment": "Type error: Cannot concatenate string with number directly.",
            "fix": "Convert to string: str(value) or use f-string: f'text {value}'",
            "confidence": 0.82,
            "languages": ["python"]
        })
    
    # Missing return statement (Python)
    if lang == 'python' and re.search(r'def\s+\w+\s*\([^)]*\):[^}]*\n\s{4,}result\s*=', code) and not re.search(r'return\s+result', code):
        bugs.append({
            "type": "bug", "category": "logic", "severity": 2, "line_hint": 3,
            "comment": "Missing return statement: Function computes value but returns None.",
            "fix": "Add return: return result",
            "confidence": 0.89,
            "languages": ["python"]
        })
    
    # Race condition (threading/multiprocessing)
    if re.search(r'threading\.|multiprocessing\.|Thread|Process|concurrent', code, re.I) and not re.search(r'Lock|RLock|Semaphore|Mutex|synchronized', code, re.I):
        bugs.append({
            "type": "bug", "category": "logic", "severity": 3, "line_hint": 2,
            "comment": "Race condition: Multithreaded code without synchronization.",
            "fix": "Use locks: lock = threading.Lock(); with lock: shared_var += 1",
            "confidence": 0.80,
            "languages": ["python", "java", "go", "cpp"]
        })
    
    # Inefficient nested loops
    if code.count('for ') >= 2 and ('range(10000)' in code or 'range(1000)' in code):
        bugs.append({
            "type": "bug", "category": "logic", "severity": 2, "line_hint": 2,
            "comment": "Performance issue: Nested loops with large ranges (O(n²) complexity).",
            "fix": "Use set/dict for O(1) lookup or optimize algorithm",
            "confidence": 0.75,
            "languages": ["python", "javascript", "java", "go", "cpp"]
        })
    
    # Sort bugs by severity (highest first)
    bugs.sort(key=lambda x: x['severity'], reverse=True)
    
    return bugs


def analyze_code_locally(code: str, lang: str) -> dict:
    """Pattern-based code analysis - DEPRECATED: Use analyze_code_comprehensively instead"""
    bugs = analyze_code_comprehensively(code, lang)
    if bugs:
        return bugs[0]
    return {
        "type": "approve",
        "category": "style",
        "severity": 1,
        "line_hint": None,
        "comment": "No obvious bugs detected. Code looks clean.",
        "fix": "",
        "confidence": 1.0
    }