Skip to content

About

RL environment where an AI agent learns to review code like a senior engineer. Given a Python snippet with a planted bug, the agent detects it, identifies the line, classifies severity (critical/major/minor), and suggests a fix. Rewards 0.0–1.0 with partial credit.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

14 Commits

Folders and files

Repository files navigation

Code Review Agent — OpenEnv Environment

An RL environment where an AI agent learns to review code like a senior engineer.

What it does

The agent is shown a Python code snippet with a bug planted inside. It must:

  1. Detect whether a bug exists
  2. Identify the line number
  3. Classify the severity (critical / major / minor)
  4. Write a description explaining the bug
  5. Suggest a fix

Three difficulty levels

Level Bug type Example
Easy Obvious — typo, off-by-one proifle instead of profile
Medium Logic — wrong formula, edge case fahrenheit_to_celsius with wrong formula
Hard Security/perf — injection, races SQL injection via f-string

Reward function (0.0 – 1.0)

Component Points
Correct bug detection +0.40
Correct line number +0.20
Correct severity +0.20
Meaningful description +0.20

Partial credit is awarded for partially correct answers.

Observation space

{
  "code_snippet": "def get_user(db_conn, username): ...",
  "language": "python",
  "file_name": "db_utils.py",
  "task_level": "hard",
  "instructions": "Review the code..."
}

Action space

{
  "bug_found": true,
  "bug_line": 4,
  "severity": "critical",
  "description": "SQL injection via f-string interpolation in query",
  "suggested_fix": "Use parameterized queries: cursor.execute('SELECT * FROM users WHERE username = ?', (username,))"
}

API endpoints

Method Endpoint Description
GET /health Health check
POST /reset Start new episode
POST /step Submit agent action
GET /state Get current env state

Example: reset

curl -X POST https://your-space.hf.space/reset \
  -H "Content-Type: application/json" \
  -d '{"task_level": "easy", "seed": 42}'

Example: step

curl -X POST https://your-space.hf.space/step \
  -H "Content-Type: application/json" \
  -d '{
    "bug_found": true,
    "bug_line": 3,
    "severity": "major",
    "description": "Off-by-one error in range causes IndexError",
    "suggested_fix": "Change range(len(numbers) + 1) to range(len(numbers))"
  }'

Setup & run locally

# Install
pip install -r requirements.txt

# Run server
python server.py

# Run inference (set env vars first)
export API_BASE_URL="https://api-inference.huggingface.co/v1"
export MODEL_NAME="meta-llama/Llama-3.1-8B-Instruct"
export HF_TOKEN="hf_..."
export ENV_URL="http://localhost:7860"

python inference.py

Required environment variables (inference)

Variable Description
API_BASE_URL LLM API endpoint (OpenAI-compatible)
MODEL_NAME Model identifier
HF_TOKEN Hugging Face API token
ENV_URL URL of this running Space

File structure

code-review-env/
├── environment.py      # Core env: reset/step/state logic
├── server.py           # FastAPI HTTP server
├── inference.py        # Inference script (required)
├── openenv.yaml        # OpenEnv spec
├── Dockerfile          # Container config
├── requirements.txt
├── README.md
└── tasks/
    ├── task_easy.py    # 5 easy bug scenarios
    ├── task_medium.py  # 5 medium bug scenarios
    └── task_hard.py    # 5 hard bug scenarios

About

RL environment where an AI agent learns to review code like a senior engineer. Given a Python snippet with a planted bug, the agent detects it, identifies the line, classifies severity (critical/major/minor), and suggests a fix. Rewards 0.0–1.0 with partial credit.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages