NILESH1003/codeguardian-env
0
CodeGuardian
1. Problem Description
CodeGuardian is a Reinforcement Learning environment where an AI agent learns to review code. By analyzing code snippets, identifying vulnerabilities, and suggesting robust fixes, the AI functions as an autonomous code reviewer. It adheres to the OpenEnv standard by Meta PyTorch, making it easy to benchmark and deploy to Hugging Face Spaces.
2. Environment Explanation
- State: Tracks the task, episode, total reward, step count, and completion status.
- Action: Actions an AI agent takes on the code (e.g., flagging bugs, suggesting a fix).
- Reward: Floating-point values are always clamped to the strict open interval
(0, 1)so validation never sees0.0or1.0.
3. Observation Space
The observation space provides details for evaluating the code.
{
"code": "def add(a, b):\n return a + b",
"filename": "calculator.py",
"language": "python",
"task_id": "syntax_check",
"task_difficulty": "easy",
"issues_found": [],
"step_count": 0,
"done": false
}4. Action Space
The agent responds using a structured JSON object.
{
"action": "flag_bug",
"line": 4,
"comment": "Missing colon after function definition.",
"bug_type": "syntax_error"
}5. Reward Logic
All rewards, accumulated total_reward, and final task scores use the same strict clamp:
value = max(0.001, min(0.999, value))6. Task Descriptions
- Easy (syntax_check): Detect trivial syntax errors across 15-20 lines. (Max steps: 5)
- Medium (logic_review): Discover off-by-one errors and illogical comparators. (Max steps: 10)
- Hard (full_review): Audit Python functions for syntax errors along with unvalidated state and SQL injection flaws. (Max steps: 15)
7. Setup Steps
- Install requirements:
pip install -r requirements.txt- Build the Docker image:
docker build -t codeguardian .- Run the Docker container:
docker run -p 7860:7860 codeguardian8. Baseline Scores
Using inference.py with standard open-weights LLMs:
- Easy: Expected ~0.9 - 0.999
- Medium: Expected ~0.5 - 0.999
- Hard: Expected ~0.3 - 0.6
9. API Endpoints
POST /reset- Resets the environment and returns the initial state. Optional{ "task_id": "syntax_check" }payload.POST /step- Exert an action on the environment; returnsobservation,reward,done, andinfo.GET /state- Emits current environmental state.GET /health- Liveness check for the container running FastAPI.
