File size: 10,164 Bytes
ba52031
 
 
 
 
 
 
be369e0
ba52031
 
 
 
 
 
 
2058884
ba52031
2058884
 
 
ba52031
2058884
 
ba52031
 
 
2058884
ba52031
2058884
 
 
 
ba52031
2058884
 
 
ba52031
 
 
2058884
ba52031
2058884
 
 
 
 
ba52031
2058884
 
 
 
 
 
ba52031
2058884
ba52031
2058884
ba52031
2058884
 
 
 
 
 
ba52031
 
2058884
ba52031
2058884
ba52031
2058884
ba52031
2058884
 
 
 
 
ba52031
 
2058884
 
 
ba52031
2058884
ba52031
2058884
ba52031
2058884
 
 
 
 
 
 
 
 
 
 
 
 
 
ba52031
2058884
ba52031
2058884
ba52031
2058884
ba52031
2058884
 
ba52031
 
2058884
ba52031
 
2058884
ba52031
 
2058884
 
ba52031
 
2058884
ba52031
2058884
ba52031
2058884
 
 
 
 
 
ba52031
 
2058884
ba52031
2058884
 
 
 
ba52031
 
 
 
 
2058884
ba52031
 
2058884
 
 
ba52031
 
2058884
ba52031
 
2058884
 
ba52031
2058884
 
ba52031
2058884
 
ba52031
 
2058884
ba52031
2058884
 
 
ba52031
2058884
 
 
ba52031
2058884
 
 
ba52031
 
 
 
2058884
ba52031
2058884
ba52031
2058884
ba52031
2058884
 
 
 
 
ba52031
2058884
ba52031
 
2058884
 
 
 
 
 
ba52031
 
2058884
 
 
 
 
 
ba52031
 
2058884
 
ba52031
2058884
 
ba52031
 
2058884
ba52031
 
2058884
 
 
 
 
ba52031
2058884
ba52031
 
2058884
 
ba52031
2058884
 
ba52031
2058884
 
 
ba52031
2058884
 
ba52031
2058884
 
 
 
 
 
 
ba52031
 
 
 
 
2058884
ba52031
 
2058884
 
 
 
 
ba52031
 
2058884
ba52031
 
2058884
 
 
 
 
 
ba52031
 
2058884
ba52031
 
2058884
 
 
ba52031
 
 
 
2058884
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba52031
 
 
 
2058884
ba52031
2058884
ba52031
 
2058884
 
 
ba52031
2058884
 
ba52031
2058884
 
ba52031
 
2058884
ba52031
2058884
 
 
 
 
ba52031
 
 
 
 
2058884
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
---
title: Debug Env - AI Debugging Benchmark
emoji: πŸ›
colorFrom: blue
colorTo: indigo
sdk: docker
pinned: true
app_port: 7860
tags:
  - openenv
  - debugging
  - benchmark
  - ai-agents
---

# debug-env β€” AI Code Debugging Benchmark

An [OpenEnv](https://github.com/meta-pytorch/OpenEnv) benchmark where LLM agents fix broken Python code.
Agents use four tools to diagnose and repair bugs across 9 tasks of increasing complexity.
Rewards are shaped on test pass rate (0.0–1.0) with efficiency bonuses.

**Live Space**: [vanshgoel1-debug-env.hf.space](https://vanshgoel1-debug-env.hf.space)  
**GitHub**: [VanshGoel-1/debug-env](https://github.com/VanshGoel-1/debug-env)

---

## Environment Description

Real-world code debugging is a core developer skill and a meaningful benchmark for LLM agents.
debug-env places agents inside an isolated working directory containing buggy Python code and
a pytest test suite. The agent must use tools to understand the code, identify the bug, and
write a corrected file β€” all within a fixed step budget.

The environment models genuine debugging scenarios: syntax errors, logic bugs, multi-file
issues, type errors, code quality problems, and collaborative refactoring tasks. These are
tasks developers do every day.

---

## Action Space

```python
class DebugAction(Action):
    tool: str   # one of: "list_files", "run_tests", "read_file", "edit_file"
    args: dict  # tool-specific arguments (see below)
```

| Tool | Args | Description |
|------|------|-------------|
| `list_files` | `{}` | List all editable source files in the task workdir |
| `run_tests` | `{}` | Run pytest and return pass rate + full output |
| `read_file` | `{"path": "broken_code.py"}` | Read a source file |
| `edit_file` | `{"path": "broken_code.py", "content": "..."}` | Overwrite file, then run tests |

---

## Observation Space

```python
class DebugObservation(Observation):
    pass_rate: float  # fraction of tests passing (0.0–1.0)
    logs: str         # test output, file content, or error message
    reward: float     # shaped reward for this step
    done: bool        # True when all tests pass (pass_rate == 1.0)
```

---

## Reward Function

Defined in `debug_env/server/grader.py`:

```
reward = pass_rate
       βˆ’ min(max(0, (steps βˆ’ 3) Γ— 0.01), 0.3)   # step penalty after step 3
       + 0.1  if pass_rate == 1.0                  # completion bonus
       + 0.2 Γ— max(0, 1 βˆ’ steps/max_steps)         # efficiency bonus on full solve
```

- Partial credit for partial test passage (not sparse)
- Penalises thrashing (many steps without progress)
- Rewards solving quickly

---

## Tasks

| ID | Title | Difficulty | Type | Files |
|----|-------|-----------|------|-------|
| task1 | Fix Syntax Error | Easy | Bug fix | `broken_code.py` |
| task2 | Fix Logic Error | Medium | Bug fix | `broken_code.py` |
| task3 | Fix Multi-file Bug | Hard | Bug fix | `broken_code.py`, `helper.py` |
| task4 | Fix Type Errors | Medium | Type check | `typed_code.py` |
| task5 | Remove Dead Code | Medium | Code quality | `messy_code.py` |
| task6 | Architecture Refactor | Hard | Refactoring | 4 files |
| task7 | Code Review Workflow | Medium | Collaborative | `auth.py` |
| task8 | Cross-team Bug Investigation | Hard | Collaborative | 3 files |
| task9 | Collaborative Refactoring | Hard | Collaborative | 4 files |

Tasks 1–3 cover the mandatory competition range (easy β†’ medium β†’ hard).
Tasks 4–9 provide additional challenge for frontier models.

---

## Setup

### Prerequisites

- Python 3.10+
- [uv](https://github.com/astral-sh/uv) package manager

```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
```

### Install

```bash
git clone https://github.com/VanshGoel-1/debug-env
cd debug-env

uv sync
```

### Configure

```bash
cp .env.example .env
# Edit .env and set:
#   HF_TOKEN=hf_...          (required)
#   API_BASE_URL=https://router.huggingface.co/v1
#   MODEL_NAME=Qwen/Qwen2.5-72B-Instruct
```

### Verify

```bash
python verify_setup.py
# Expected: 18 passed, 0 failed
```

---

## Usage

### Start the server

```bash
# Terminal 1
uv run server
# Server runs at http://localhost:8000
```

### Run the benchmark

```bash
# Terminal 2
python inference.py

# Run a specific task
TASK=task2 python inference.py

# Multiple runs for Pass@k
TASK=task1 NUMBER_OF_RUNS=3 python inference.py
```

### Verify the server manually

```bash
curl http://localhost:8000/health
# {"status":"healthy","service":"debug-env"}

curl -X POST http://localhost:8000/reset \
  -H "Content-Type: application/json" \
  -d '{"task": "task1"}'

curl -X POST http://localhost:8000/step \
  -H "Content-Type: application/json" \
  -d '{"action": {"tool": "list_files", "args": {}}}'
```

---

## Inference Script

`inference.py` uses the OpenAI client against any OpenAI-compatible endpoint.

**Required environment variables:**

| Variable | Description |
|----------|-------------|
| `HF_TOKEN` | HuggingFace / API key |
| `API_BASE_URL` | LLM API endpoint (default: `https://router.huggingface.co/v1`) |
| `MODEL_NAME` | Model identifier (default: `Qwen/Qwen2.5-72B-Instruct`) |

**Competition stdout format:**

```
[START] task=task1 env=debug-env model=Qwen/Qwen2.5-72B-Instruct
[STEP]  step=1 action=list_files() reward=0.00 done=false error=null
[STEP]  step=2 action=run_tests() reward=0.00 done=false error=null
[STEP]  step=3 action=read_file('broken_code.py') reward=0.00 done=false error=null
[STEP]  step=4 action=edit_file('broken_code.py') reward=1.10 done=true error=null
[END]   success=true steps=4 score=1.10 rewards=0.00,0.00,0.00,1.10
```

Results are saved to `results_{task}_{timestamp}.json` with `pass@k`, `success_rate`,
`avg_steps`, and `avg_final_reward`.

---

## Docker

```bash
# Build
docker build -t debug-env .

# Run
docker run -p 8000:8000 debug-env

# With Docker Compose
docker-compose up --build
```

The Dockerfile uses a two-stage build: dependencies are installed in the builder stage
(cache-friendly), source code is copied into the runtime stage. The CMD runs uvicorn
directly against `debug_env.server.app:app`.

---

## Deployment to Hugging Face Spaces

```bash
# Authenticate
huggingface-cli login

# Validate before pushing
openenv validate

# Push
openenv push --repo-id your-username/debug-env
```

The `openenv.yaml` at repo root defines the Space configuration. After push, the Space
auto-builds from `Dockerfile` and exposes:

- `GET /health` β€” health check
- `POST /reset` β€” start episode
- `POST /step` β€” execute tool
- `GET /tasks` β€” list all tasks
- `POST /mcp` β€” MCP JSON-RPC interface
- `/web` β€” interactive web UI
- `/docs` β€” OpenAPI docs

---

## API Reference

### Core endpoints

```
GET  /health                              β†’ {"status":"healthy","service":"debug-env"}
POST /reset   {"task":"task1"}            β†’ initial observation
POST /step    {"action":{"tool":...}}     β†’ observation, reward, done
GET  /state                               β†’ current episode state
WS   /ws                                  β†’ persistent session (low latency)
```

### Task endpoints

```
GET  /tasks                               β†’ paginated task list
GET  /tasks/{taskId}                      β†’ task metadata
GET  /tasks/{taskId}/files                β†’ editable file list
POST /tasks/{taskId}/episodes             β†’ create episode
GET  /tasks/{taskId}/episodes/{id}        β†’ get episode
PATCH /tasks/{taskId}/episodes/{id}       β†’ update episode status
```

### MCP endpoint

```
POST /mcp   {"jsonrpc":"2.0","method":"initialize","params":{"meta":{"task":"task1"}},"id":1}
POST /mcp   {"jsonrpc":"2.0","method":"tools/list","id":2}
POST /mcp   {"jsonrpc":"2.0","method":"tools/call","params":{"name":"run_tests","arguments":{}},"id":3}
```

---

## Project Structure

```
debug-env/                    ← repo root, all commands run here
β”œβ”€β”€ pyproject.toml            ← single package definition
β”œβ”€β”€ uv.lock                   ← committed lock file
β”œβ”€β”€ Dockerfile                ← single Dockerfile
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ openenv.yaml              ← OpenEnv spec
β”œβ”€β”€ inference.py              ← competition entry point
β”œβ”€β”€ train.py                  ← RL training (GRPO)
β”œβ”€β”€ verify_setup.py
└── debug_env/
    β”œβ”€β”€ models.py             ← DebugAction, DebugObservation
    β”œβ”€β”€ client.py             ← DebugEnv HTTP/WS client
    β”œβ”€β”€ server/
    β”‚   β”œβ”€β”€ app.py            ← FastAPI entry point
    β”‚   β”œβ”€β”€ debug_env_environment.py
    β”‚   β”œβ”€β”€ grader.py         ← reward shaping
    β”‚   β”œβ”€β”€ core/apis.py      ← /health
    β”‚   β”œβ”€β”€ database/         ← SQLAlchemy + SQLite
    β”‚   β”œβ”€β”€ handlers/         ← MCP JSON-RPC dispatcher
    β”‚   β”œβ”€β”€ mcp/              ← MCP router + tool specs
    β”‚   β”œβ”€β”€ schemas/          ← Pydantic schemas
    β”‚   β”œβ”€β”€ tasks/            ← registry, loader, API routes
    β”‚   β”œβ”€β”€ tools/            ← run_tests, read_file, edit_file, list_files
    β”‚   β”‚                       + 6 advanced analysis tools
    β”‚   └── utils/            ← path validation
    β”œβ”€β”€ rl/
    β”‚   β”œβ”€β”€ dataset.py        ← curriculum dataset for GRPO
    β”‚   └── rollout.py        ← reward bridge for TRL
    └── tasks/
        β”œβ”€β”€ task1/ – task3/   ← static broken_code.py + test_code.py
        └── task4/ – task9/   ← seed_data.py (generated at runtime)
```

---

## RL Training (Optional)

GRPO training against the live environment using TRL + Unsloth:

```bash
uv sync --extra training
uv run server          # Terminal 1 β€” environment must be running
python train.py        # Terminal 2

# Smaller model for low VRAM:
MODEL=Qwen/Qwen2.5-1.5B-Instruct python train.py

# Easy tasks only (recommended starting point):
TASK_FILTER=easy python train.py
```

VRAM reference (Unsloth 4-bit QLoRA):

| Model | Min VRAM |
|-------|---------|
| Qwen2.5-1.5B | ~2 GB |
| Qwen2.5-7B | ~6 GB |
| Qwen2.5-14B | ~10 GB |

---

## License

BSD 3-Clause β€” see [LICENSE](LICENSE)