Spaces:
Sleeping
Sleeping
fix: resolve create_fastapi_app TypeError, restore _SYSTEM_PROMPT, fix /api/train body parsing
Browse files
agent.py
CHANGED
|
@@ -47,6 +47,36 @@ _check_api_keys()
|
|
| 47 |
# System Prompt
|
| 48 |
# =========================================================================
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
_RETRY_SUFFIX = (
|
| 52 |
"\n\nYour previous response was not valid JSON. "
|
|
|
|
| 47 |
# System Prompt
|
| 48 |
# =========================================================================
|
| 49 |
|
| 50 |
+
_SYSTEM_PROMPT = """\
|
| 51 |
+
You are an expert security-focused code reviewer.
|
| 52 |
+
Your ONLY job is to return a JSON array of bug objects — no markdown, no prose.
|
| 53 |
+
|
| 54 |
+
Each bug object MUST follow this exact schema:
|
| 55 |
+
{
|
| 56 |
+
"type": "bug" | "approve",
|
| 57 |
+
"category": "security" | "logic" | "style" | "ok",
|
| 58 |
+
"line_hint": "<brief description of the suspicious line or line number>",
|
| 59 |
+
"comment": "<clear explanation of the issue>",
|
| 60 |
+
"severity": 1-5,
|
| 61 |
+
"confidence": 0.0-1.0,
|
| 62 |
+
"fix": "<suggested fixed code snippet>"
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
Return [] (empty array) if the code is clean.
|
| 66 |
+
Return multiple objects if multiple bugs exist.
|
| 67 |
+
|
| 68 |
+
ALWAYS look for:
|
| 69 |
+
SECURITY (severity 4-5):
|
| 70 |
+
- SQL built with f-strings / concatenation
|
| 71 |
+
- Passwords stored without hashing
|
| 72 |
+
- Hardcoded secrets, API keys, or tokens
|
| 73 |
+
- eval(), exec(), pickle.load() with untrusted data
|
| 74 |
+
LOGIC (severity 2-4):
|
| 75 |
+
- range(len(x) - 1) off-by-one
|
| 76 |
+
- Strict > / < where >= / <= is needed
|
| 77 |
+
- Division without zero-check
|
| 78 |
+
- Mutable default arguments (def f(x=[]))
|
| 79 |
+
"""
|
| 80 |
|
| 81 |
_RETRY_SUFFIX = (
|
| 82 |
"\n\nYour previous response was not valid JSON. "
|
main.py
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
CodeBug FastAPI server with comprehensive code review and training endpoints.
|
| 3 |
"""
|
| 4 |
|
| 5 |
-
from fastapi import FastAPI,
|
| 6 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 7 |
from pydantic import BaseModel
|
| 8 |
import os, json, re, logging
|
|
@@ -20,11 +20,7 @@ except ImportError as e:
|
|
| 20 |
logger = logging.getLogger(__name__)
|
| 21 |
|
| 22 |
# Create the OpenEnv app (has /ws, /reset, /step, /state, /health, etc.)
|
| 23 |
-
openenv_app = create_fastapi_app(
|
| 24 |
-
CodeBugEnvironment,
|
| 25 |
-
CodeReviewAction,
|
| 26 |
-
CodeReviewObservation
|
| 27 |
-
)
|
| 28 |
|
| 29 |
# Create a wrapper FastAPI app (takes precedence for /api/* routes)
|
| 30 |
app = FastAPI(title="CodeBug API", version="2.0")
|
|
@@ -32,11 +28,14 @@ app = FastAPI(title="CodeBug API", version="2.0")
|
|
| 32 |
# ========================================================================
|
| 33 |
# POST /api/train - Run RL training
|
| 34 |
# ========================================================================
|
|
|
|
|
|
|
|
|
|
| 35 |
@app.post("/api/train")
|
| 36 |
-
async def run_real_training(
|
| 37 |
"""Run the RL training loop for the specified number of episodes."""
|
| 38 |
try:
|
| 39 |
-
stats = run_training(num_episodes=episodes, verbose=False)
|
| 40 |
return {"status": "ok", "stats": stats}
|
| 41 |
except Exception as e:
|
| 42 |
import traceback
|
|
|
|
| 2 |
CodeBug FastAPI server with comprehensive code review and training endpoints.
|
| 3 |
"""
|
| 4 |
|
| 5 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 6 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 7 |
from pydantic import BaseModel
|
| 8 |
import os, json, re, logging
|
|
|
|
| 20 |
logger = logging.getLogger(__name__)
|
| 21 |
|
| 22 |
# Create the OpenEnv app (has /ws, /reset, /step, /state, /health, etc.)
|
| 23 |
+
openenv_app = create_fastapi_app(CodeBugEnvironment)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Create a wrapper FastAPI app (takes precedence for /api/* routes)
|
| 26 |
app = FastAPI(title="CodeBug API", version="2.0")
|
|
|
|
| 28 |
# ========================================================================
|
| 29 |
# POST /api/train - Run RL training
|
| 30 |
# ========================================================================
|
| 31 |
+
class TrainRequest(BaseModel):
|
| 32 |
+
episodes: int = 5
|
| 33 |
+
|
| 34 |
@app.post("/api/train")
|
| 35 |
+
async def run_real_training(body: TrainRequest):
|
| 36 |
"""Run the RL training loop for the specified number of episodes."""
|
| 37 |
try:
|
| 38 |
+
stats = run_training(num_episodes=body.episodes, verbose=False)
|
| 39 |
return {"status": "ok", "stats": stats}
|
| 40 |
except Exception as e:
|
| 41 |
import traceback
|