Spaces:
Sleeping
Sleeping
metadata
title: CodeDebugger
emoji: π
colorFrom: blue
colorTo: purple
sdk: docker
pinned: false
π CodeDebugger
An RL Environment for LLM-Powered Python Code Debugging
Meta + Scalar OpenEnv Hackathon 2026
π― What Is This?
CodeDebugger is an OpenEnv-compliant reinforcement learning environment where an LLM agent:
- Sees buggy Python code + error messages
- Proposes fixes step by step
- Gets rewarded based on how many test cases pass after each fix
The agent learns to debug code through trial and error, with an 8-component reward function and anti-reward-hacking safeguards.
ποΈ Architecture
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Orchestrator β
β ββββββββββββββββ ββββββββββββββββ βββββββββββββ β
β β Bug Dataset βββββΆβ Environment βββββΆβ Executor β β
β β (30 problems)β β (OpenEnv) β β (Sandbox) β β
β ββββββββββββββββ ββββββββ¬ββββββββ βββββββββββββ β
β β β
β ββββββββββββββββΌβββββββββββββββ β
β βΌ βΌ βΌ β
β βββββββββββββ ββββββββββββ βββββββββββββββββ β
β β LLM Fixerβ β Reward β β Anti-Hacking β β
β β (3-tier) β β (8 comp) β β Critic (4 ck)β β
β βββββββββββββ ββββββββββββ βββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π¦ Project Structure
codedebugger/
βββ env/
β βββ codedebugger_env.py # OpenEnv environment + FastAPI
β βββ executor.py # Sandboxed code runner (subprocess)
βββ agents/
β βββ fixer.py # LLM fixer agent (3-tier: Groq β simplified β heuristic)
β βββ reward.py # 8-component reward function
β βββ critic.py # 4 anti-hacking checks
βββ data/
β βββ bug_dataset.py # 30 buggy problems + test cases
βββ training/
β βββ train_grpo.py # GRPO training script (Unsloth + TRL)
β βββ run_baseline.py # Baseline evaluation
β βββ train_colab.ipynb # Google Colab notebook
βββ utils/
β βββ prompts.py # All LLM prompts
βββ outputs/ # Results saved here
βββ app.py # Streamlit UI (3 tabs)
βββ orchestrator.py # Main debug loop controller
βββ openenv.yaml # OpenEnv registry manifest
βββ Dockerfile # HF Spaces deployment
βββ requirements.txt
βββ README.md
π Quick Start
1. Install Dependencies
cd codedebugger
pip install -r requirements.txt
2. Set API Key
export GROQ_API_KEY="your-key-here"
3. Run Baselines
python training/run_baseline.py
4. Launch Streamlit UI
streamlit run app.py
5. Start FastAPI Server
uvicorn env.codedebugger_env:app --host 0.0.0.0 --port 8000
Then interact via HTTP:
# Health check
curl http://localhost:8000/health
# Reset with a problem dict
curl -X POST http://localhost:8000/reset \
-H "Content-Type: application/json" \
-d '{"id": "bug_001", "difficulty": "easy", ...}'
# Step with fixed code
curl -X POST http://localhost:8000/step \
-H "Content-Type: application/json" \
-d '{"code": "def get_last(lst):\n return lst[-1]"}'
# Render current state
curl http://localhost:8000/render
π― Reward Function (8 Components)
| Component | Range | Description |
|---|---|---|
| test_score | 0β50 | Proportional to test cases passed, +10 bonus for 100% |
| fix_quality | 0β20 | Rewards minimal, targeted changes (1β3 lines = 20 pts) |
| format_score | 0β15 | Valid AST, preserved function names, consistent style |
| speed_score | 0β10 | Rewards solving in fewer iterations (10/7/4 for iter 1/2/3) |
| safety_score | 0β10 | No forbidden imports or dangerous patterns |
| improvement_score | 0β15 | Rewards passing more tests than previous iteration |
| anti_hack_penalty | β€ 0 | Negative penalty for detected cheating |
| process_bonus | 0β3 | Bonus for fix addressing the root cause |
Total range: 0β120 (clamped to β₯ 0)
π‘οΈ Anti-Reward-Hacking (4 Checks)
| Check | What It Detects | Penalty |
|---|---|---|
| C1: Hardcoded Returns | If-chains returning literal test answers (β₯2 matches, excludes constants) | β30 |
| C2: Function Deletion | Removal or renaming of the original function signature | β25 |
| C3: Trivial Pass | Suspiciously fast execution (all pass in <0.01s) | β20 |
| C4: Code Gutting | Submitted code is <30% of original length, or trivial (pass/return None) |
β35 |
π Dataset
30 buggy Python problems across 3 difficulty levels:
- Easy (10 problems): off-by-one, missing return, wrong operator, wrong method, etc.
- Medium (10 problems): wrong base case, wrong sort order, missing edge case, wrong range, etc.
- Hard (10 problems): binary search, DP transitions, graph traversal, backtracking, etc.
Each problem includes:
- Buggy code with a single injected bug
- Known correct solution
- 4 test cases
- Error type classification
- Hint for debugging
ποΈ Training with GRPO
Local (requires GPU)
python training/train_grpo.py
- Model:
unsloth/Llama-3.2-1B-Instruct(4-bit quantized) - Method: GRPO via TRL + Unsloth
- Steps: 100 (configurable)
- GPU: Tesla T4 or better
Google Colab
Open training/train_colab.ipynb in Colab with a T4 GPU.
π Results
Baseline (Groq LLM β llama-3.1-8b-instant)
| Metric | Baseline | After GRPO Training |
|---|---|---|
| Solved | 25/30 (83%) | 23/30 (77%) |
| Peak Reward | 88 | 100 |
| Avg Reward | 75.2 | ~65 (100 steps) |
| RL Evidence | Starting point | Reward 76β100 in epoch 1 |
By Difficulty
| Difficulty | Baseline Solved | Baseline Avg Reward |
|---|---|---|
| Easy | 9/10 | 79.2 |
| Medium | 8/10 | 67.9 |
| Hard | 8/10 | 78.6 |
π³ Deploy to Hugging Face Spaces
# Build and test locally
docker build -t codedebugger .
docker run -p 7860:7860 codedebugger
# Push to HF Spaces
# 1. Create a Space on huggingface.co (Docker type)
# 2. Push this repo to the Space's git remote
π License
MIT
Built with β€οΈ for the Meta + Scalar OpenEnv Hackathon 2026