File size: 16,829 Bytes
e999a76
22f91bc
 
 
e999a76
56f6a4d
22f91bc
 
e999a76
 
5235bfa
 
358905d
e999a76
56f6a4d
8b06b9c
56f6a4d
e3f03e2
 
 
 
 
 
 
8b06b9c
 
5235bfa
22f91bc
 
 
f7a26b5
bf5444d
22f91bc
f63e772
22f91bc
af2db79
 
 
e999a76
af2db79
bf5444d
af2db79
e999a76
 
 
22f91bc
 
e999a76
822c184
22f91bc
f63e772
22f91bc
822c184
 
 
e999a76
822c184
 
 
1fc2334
 
05cb78e
f63e772
1fc2334
 
 
 
f63e772
22f91bc
 
 
 
f63e772
22f91bc
f63e772
05cb78e
 
 
22f91bc
 
 
 
 
05cb78e
 
 
 
f63e772
 
 
 
d2ce69f
05cb78e
 
f63e772
1fc2334
 
 
22f91bc
 
822c184
05cb78e
22f91bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f63e772
22f91bc
f63e772
e3f03e2
 
8b06b9c
56f6a4d
 
 
 
 
8b06b9c
22f91bc
8b06b9c
 
f63e772
22f91bc
f63e772
22f91bc
 
f63e772
 
 
 
 
 
 
22f91bc
 
 
 
748fdb2
b850afb
 
f63e772
 
 
22f91bc
f63e772
22f91bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f63e772
22f91bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f63e772
748fdb2
22f91bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f63e772
22f91bc
 
 
 
 
 
 
 
 
f63e772
 
22f91bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
748fdb2
d2ce69f
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
"""
CodeBug β€” OpenEnv-compliant FastAPI server.
The openenv app IS the main app so /reset /step /state are at root level.
Custom routes /api/review /api/train / are added on top.
"""

import os, re, logging, traceback, uuid
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse, JSONResponse
from pydantic import BaseModel
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
    _AGENT_IMPORTED = True
except ImportError:
    _AGENT_IMPORTED = False

_HAS_ANTHROPIC = bool(os.environ.get("ANTHROPIC_API_KEY", "").strip())
_HAS_OPENAI    = bool(os.environ.get("OPENAI_API_KEY", "").strip())
HAS_LLM = _AGENT_IMPORTED and (_HAS_ANTHROPIC or _HAS_OPENAI)

logger = logging.getLogger(__name__)

# ── The openenv app IS the main app ──────────────────────────────────────
# This puts /reset  /step  /state  /ws  at the ROOT so the checker finds them.
app = create_fastapi_app(CodeBugEnvironment, CodeReviewAction, CodeReviewObservation)


# =========================================================================
# POST /api/train
# =========================================================================
class TrainRequest(BaseModel):
    episodes: int = 5

@app.post("/api/train")
async def run_real_training(body: TrainRequest):
    try:
        stats = run_training(num_episodes=body.episodes, verbose=False)
        return {"status": "ok", "stats": stats}
    except Exception as e:
        logger.error(f"Training failed: {e}\n{traceback.format_exc()}")
        return JSONResponse(status_code=500,
                            content={"error": str(e), "details": traceback.format_exc()[:500]})


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

@app.post("/api/review")
async def review_code(request: ReviewRequest):
    try:
        code = request.code.strip()
        lang = request.language.lower().strip()

        if not code:
            raise HTTPException(status_code=400, detail="Code cannot be empty")
        if len(code) > 5000:
            raise HTTPException(status_code=400, detail="Code exceeds 5000 characters")

        supported = ['python', 'javascript', 'typescript', 'java', 'go', 'cpp', 'c++']
        if lang not in supported:
            raise HTTPException(status_code=400,
                                detail=f"Language '{lang}' not supported. Use: {', '.join(supported)}")

        results = _analyze_with_llm(code, lang) if HAS_LLM else analyze_code_comprehensively(code, lang)

        response = {
            "bugs_found": len(results),
            "bugs": results,
            "primary_bug": results[0] if results else {
                "type": "approve", "rule_id": None, "category": "style",
                "severity": 1, "line_hint": None,
                "comment": "No bugs detected.", "fix": "", "confidence": 1.0
            },
            "analysis_metadata": {
                "language": lang,
                "code_length": len(code),
                "lines_of_code": len(code.split('\n')),
                "rules_engine": "30-rule official spec",
                "average_confidence": round(
                    sum(b.get("confidence", 0.5) for b in results) / max(len(results), 1), 2
                ) if results else 1.0
            }
        }
        return response

    except HTTPException:
        raise
    except Exception as e:
        logger.error(traceback.format_exc())
        return JSONResponse(status_code=500, content={"error": str(e)})


# =========================================================================
# GET / β€” Serve frontend
# =========================================================================
@app.get("/")
def serve_frontend():
    html_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates", "index.html")
    try:
        with open(html_path, encoding="utf-8") as f:
            return HTMLResponse(f.read())
    except Exception:
        return HTMLResponse("<h1>CodeBug API</h1><p>Visit /docs for API docs. POST /reset to start.</p>")


# =========================================================================
# GET /api/health
# =========================================================================
@app.get("/api/health")
async def health_check():
    return {
        "status": "ok", "version": "2.0",
        "openenv_routes": ["/reset", "/step", "/state"],
        "custom_routes": ["/api/review", "/api/train"],
        "llm_active": HAS_LLM,
    }


# =========================================================================
# GET /api/stats
# =========================================================================
@app.get("/api/stats")
async def get_stats():
    return {"rules_total": 30, "categories": 6, "languages": 6, "llm_active": HAS_LLM}


# =========================================================================
# LLM wrapper
# =========================================================================
def _analyze_with_llm(code: str, lang: str) -> list:
    if not HAS_LLM:
        return analyze_code_comprehensively(code, lang)
    try:
        bugs = agent_review(code)
        if not isinstance(bugs, list):
            bugs = []
        bugs.sort(key=lambda x: (x.get('severity', 0), x.get('confidence', 0)), reverse=True)
        return bugs
    except Exception as e:
        logger.warning(f"LLM failed: {e}. Using pattern analysis.")
        return analyze_code_comprehensively(code, lang)


# =========================================================================
# OFFICIAL 30-RULE BUG ENGINE
# R-N1..R-N5 | R-C1..R-C5 | R-D1..R-D5 | R-S1..R-S5 | R-CF1..R-CF5 | R-L1..R-L5
# =========================================================================

def _fl(code: str, pattern: str, flags: int = re.I) -> int | None:
    for i, line in enumerate(code.splitlines(), 1):
        if re.search(pattern, line, flags):
            return i
    return None

def _bug(rule_id, category, severity, line_hint, comment, fix, confidence):
    return {"type": "bug", "rule_id": rule_id, "category": category,
            "severity": severity, "line_hint": line_hint,
            "comment": comment, "fix": fix, "confidence": confidence}

def analyze_code_comprehensively(code: str, lang: str) -> list:
    bugs = []
    IS_JS = lang in ('javascript', 'typescript')
    IS_PY = lang == 'python'

    # ── API & Network ─────────────────────────────────────────────────
    if IS_JS and re.search(r'\bfetch\s*\(', code):
        if not re.search(r'\.catch\s*\(|try\s*\{', code) or not re.search(r'\.ok\b|\.status\b', code):
            bugs.append(_bug("R-N1","api_network",4,_fl(code,r'\bfetch\s*\('),
                "R-N1 Β· Missing error handling on fetch: fetch() does not throw on 4xx/5xx.",
                "if (!res.ok) throw new Error(`HTTP ${res.status}`);\n// Wrap in try/catch",0.88))

    if IS_JS and re.search(r'\bfetch\s*\(', code) and not re.search(r'AbortController|signal|timeout', code):
        bugs.append(_bug("R-N2","api_network",3,_fl(code,r'\bfetch\s*\('),
            "R-N2 Β· No timeout: fetch() can hang indefinitely.",
            "const ctrl = new AbortController();\nsetTimeout(()=>ctrl.abort(),5000);\nfetch(url,{signal:ctrl.signal})",0.85))

    if IS_PY and re.search(r'requests\.(get|post|put|delete)\s*\(', code) and not re.search(r'timeout\s*=', code):
        bugs.append(_bug("R-N2","api_network",3,_fl(code,r'requests\.(get|post)'),
            "R-N2 Β· No timeout on requests call.",
            "requests.get(url, timeout=10)",0.90))

    if re.search(r'allow_origins\s*=\s*\[.*\*.*\]|Access-Control-Allow-Origin.*\*', code):
        bugs.append(_bug("R-N4","security",4,_fl(code,r'allow_origins|Access-Control'),
            "R-N4 Β· CORS wildcard (*) on authenticated endpoints is a security issue.",
            "allow_origins=['https://yourapp.com']  # not ['*']",0.95))

    if IS_JS and re.search(r'\bfetch\s*\(', code) and not re.search(r'Authorization|Bearer', code, re.I):
        bugs.append(_bug("R-N5","security",3,_fl(code,r'\bfetch\s*\('),
            "R-N5 Β· Auth token not sent in fetch request.",
            "headers: {'Authorization': `Bearer ${token}`}",0.70))

    # ── Runtime Crashes ────────────────────────────────────────────────
    if IS_PY and re.search(r'@app\.(get|post|put|delete|patch)', code) and not re.search(r'try\s*:', code):
        bugs.append(_bug("R-C1","runtime",4,_fl(code,r'@app\.(get|post)'),
            "R-C1 Β· Unhandled exception in route: any error will expose a raw 500 with traceback.",
            "try:\n    return process(body)\nexcept Exception as e:\n    logger.error(e,exc_info=True)\n    raise HTTPException(500,'Internal error')",0.85))

    if IS_PY and re.search(r'os\.environ\.get\(|os\.getenv\(', code) and not re.search(r'sys\.exit|raise.*environ|if not', code, re.I):
        bugs.append(_bug("R-C2","runtime",4,_fl(code,r'os\.environ'),
            "R-C2 Β· Missing env var validation: None returned silently if key missing.",
            "if not os.environ.get('KEY'):\n    sys.exit('[FATAL] KEY not set')",0.87))

    if IS_PY and re.search(r'(model|pipeline|tokenizer)\s*=\s*None', code) and not re.search(r'lifespan|on_event|startup', code, re.I):
        bugs.append(_bug("R-C3","runtime",4,_fl(code,r'(model|pipeline)\s*=\s*None'),
            "R-C3 Β· Model not loaded at startup β€” first request hangs or crashes.",
            "@asynccontextmanager\nasync def lifespan(app):\n    app.state.model = load_model()\n    yield",0.80))

    if IS_PY and re.search(r'request\.(json|form|args|query_params)', code) and not re.search(r'BaseModel|int\(|float\(', code):
        bugs.append(_bug("R-C5","runtime",3,_fl(code,r'request\.(json|form)'),
            "R-C5 Β· Type mismatch: no Pydantic validation on request params.",
            "class Body(BaseModel):\n    value: int  # auto-cast",0.82))

    # ── Data & State ───────────────────────────────────────────────────
    if IS_PY and re.search(r'\.(connect|cursor|session|execute)\s*\(', code) and not re.search(r'\bwith\b', code):
        bugs.append(_bug("R-D1","data_state",4,_fl(code,r'\.(connect|cursor)'),
            "R-D1 Β· DB connection not closed β€” use context manager.",
            "with db.connect() as conn:\n    conn.execute(query)",0.88))

    if IS_PY and re.search(r'\bglobal\s+\w+', code) and re.search(r'Thread|threading|asyncio', code):
        bugs.append(_bug("R-D2","data_state",5,_fl(code,r'\bglobal\s+'),
            "R-D2 Β· Race condition: global mutation in threaded context.",
            "_lock = threading.Lock()\nwith _lock:\n    counter += 1",0.90))

    if re.search(r'(cache|lru_cache)', code, re.I) and not re.search(r'ttl|expire|maxsize|timeout', code, re.I):
        ln = _fl(code, r'cache|lru_cache')
        if ln:
            bugs.append(_bug("R-D3","data_state",3,ln,
                "R-D3 Β· Stale cache: no TTL or expiry defined.",
                "cachetools.TTLCache(maxsize=256, ttl=300)",0.78))

    if IS_PY and re.search(r'@app\.(post|put|patch)', code) and not re.search(r'BaseModel|HTTPException|validator', code):
        bugs.append(_bug("R-D4","data_state",4,_fl(code,r'@app\.(post|put)'),
            "R-D4 Β· Missing input validation: no Pydantic model on POST route.",
            "class Body(BaseModel):\n    code: str\n    language: str",0.83))

    # ── Security ───────────────────────────────────────────────────────
    if re.search(r'(api_key|secret|password|token)\s*[=:]\s*["\'][^"\']{4,}["\']', code, re.I):
        bugs.append(_bug("R-S1","security",5,_fl(code,r'(api_key|secret|password|token)\s*[=:]'),
            "R-S1 Β· Hardcoded secret in source code. Rotate immediately.",
            "API_KEY = os.environ.get('API_KEY')\nif not API_KEY: raise RuntimeError('not set')",0.97))

    if re.search(r'(f["\'].*?\{.*?(SELECT|INSERT|DELETE|UPDATE)|os\.system\s*\(|shell\s*=\s*True)', code, re.I|re.S):
        bugs.append(_bug("R-S2","security",5,_fl(code,r'SELECT|INSERT|os\.system|shell\s*=\s*True'),
            "R-S2 Β· SQL/command injection: user input in query or shell.",
            "cursor.execute('SELECT * FROM t WHERE id=?',(uid,))",0.98))

    if re.search(r'allow_origins\s*=\s*\[.*\*|cors.*\*', code, re.I):
        bugs.append(_bug("R-S3","security",4,_fl(code,r'allow_origins|cors'),
            "R-S3 Β· Overly permissive CORS wildcard.",
            "allow_origins=['https://yourapp.com']",0.95))

    if IS_PY and re.search(r'@app\.(post|get)\s*\(', code) and not re.search(r'limiter|rate_limit|slowapi', code, re.I):
        bugs.append(_bug("R-S4","security",3,_fl(code,r'@app\.(post|get)'),
            "R-S4 Β· No rate limiting on public endpoint.",
            "@limiter.limit('20/minute')",0.80))

    # ── Config & Deploy ────────────────────────────────────────────────
    if re.search(r'port\s*[=:]\s*(?!7860)\d{4,5}|--port\s+(?!7860)\d{4,5}|EXPOSE\s+(?!7860)\d{4,5}', code):
        bugs.append(_bug("R-CF2","config_deploy",5,_fl(code,r'port\s*[=:]|EXPOSE'),
            "R-CF2 Β· Port mismatch: HF Spaces requires port 7860.",
            'CMD ["uvicorn","main:app","--host","0.0.0.0","--port","7860"]',0.92))

    if IS_PY and re.search(r'open\s*\(\s*["\'](?!/)', code) and not re.search(r'__file__|os\.path\.dirname', code):
        bugs.append(_bug("R-CF4","config_deploy",4,_fl(code,r'open\s*\(\s*["\']'),
            "R-CF4 Β· Relative path breaks in Docker β€” anchor to __file__.",
            "BASE=os.path.dirname(os.path.abspath(__file__))\nopen(os.path.join(BASE,'file.txt'))",0.88))

    if IS_PY:
        imports = re.findall(r'^(?:import|from)\s+(\w+)', code, re.M)
        stdlib = {'os','sys','re','json','time','math','io','abc','copy','enum','typing','pathlib',
                  'datetime','collections','functools','itertools','logging','threading','asyncio',
                  'contextlib','inspect','traceback','hashlib','hmac','base64','urllib','http',
                  'socket','struct','random','string','shutil','glob','tempfile','subprocess'}
        third = sorted(set(m for m in imports if m not in stdlib and not m.startswith('_')))
        if third:
            bugs.append(_bug("R-CF3","config_deploy",3,1,
                f"R-CF3 Β· Verify these are in requirements.txt: {', '.join(third)}",
                "pip freeze > requirements.txt",0.75))

    # ── Logic & Testing ────────────────────────────────────────────────
    if IS_PY and re.search(r'def \w+\s*\(', code) and not re.search(r'def test_|pytest|unittest|assert\b', code):
        bugs.append(_bug("R-L1","logic_testing",2,1,
            "R-L1 Β· No tests found for this code.",
            "def test_happy_path():\n    assert func(input) == expected",0.75))

    if re.search(r'range\s*\(\s*len\s*\([^)]+\)\s*-\s*1\s*\)|for.+\[:-1\]', code):
        bugs.append(_bug("R-L3","logic_testing",3,_fl(code,r'range\s*\(\s*len|:-1'),
            "R-L3 Β· Off-by-one: range(len(x)-1) skips the last element.",
            "for i in range(len(numbers)):  # not -1",0.90))

    if IS_PY and re.search(r'def\s+\w+\s*\([^)]*=\s*(\[\]|\{\}|set\(\))', code):
        bugs.append(_bug("R-L4","logic_testing",4,_fl(code,r'def\s+\w+\s*\([^)]*=\s*(\[\]|\{\})'),
            "R-L4 Β· Mutable default argument β€” shared across all calls.",
            "def func(items=None):\n    if items is None: items = []",0.98))

    if re.search(r'async\s+(def|function)', code) and not re.search(r'\bawait\b', code):
        bugs.append(_bug("R-L5","logic_testing",4,_fl(code,r'async\s+(def|function)'),
            "R-L5 Β· async function with no await β€” returns coroutine, not result.",
            "result = await fetch_from_db()  # not fetch_from_db()",0.88))

    bugs.sort(key=lambda b: (b["severity"], b["confidence"]), reverse=True)
    return bugs