File size: 13,105 Bytes
54c7f90
afc1ff7
 
 
 
 
 
54c7f90
afc1ff7
54c7f90
afc1ff7
887572f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4898dbf
c70d39c
 
 
 
887572f
c70d39c
887572f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4898dbf
 
 
 
 
 
 
 
 
 
887572f
 
 
 
 
c70d39c
887572f
c70d39c
 
 
887572f
 
 
 
 
 
c70d39c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
887572f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4898dbf
 
887572f
 
 
7e7ad80
 
 
 
887572f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e7ad80
 
 
 
 
 
 
 
 
 
 
887572f
 
 
 
 
 
 
 
7e7ad80
887572f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: API Debug Env
emoji: πŸ”§
colorFrom: blue
colorTo: green
sdk: docker
pinned: false
---

# API Debug Environment

> A real-world OpenEnv environment where AI agents learn to debug broken HTTP API requests.

Built for the **Scaler Γ— Meta PyTorch Γ— HuggingFace OpenEnv Hackathon 2026**.

---

## What This Environment Does

An AI agent is given a **broken HTTP request** and a task description. It interacts with a live mock REST API running inside the same container, reads the real HTTP error responses (401, 422, 429, 500), and iteratively fixes its requests β€” adjusting headers, methods, body types, and authentication flows β€” until it receives a successful 200 response.

Every step produces a real HTTP call. The grader is fully deterministic: no LLM, no fuzzy matching. The reward is derived entirely from HTTP status codes and response schema matching.

---

## Environment Summary

| Property | Value |
|---|---|
| **Framework** | OpenEnv (openenv-core 0.2.3) |
| **Tasks** | 4 difficulty levels β€” 12 tasks total |
| **Max Steps per Episode** | Dynamic (10 by default) |
| **Reward Range** | 0.001 – 0.999 (Strictly bounded) |
| **Curriculum** | Supports automatic difficulty progression (`task_id: "auto"`) with exploration bonuses |
| **Grader** | Fully deterministic (no LLM), robust to negative reward hacking. Supports multi-step stateful evaluation. |
| **Mock API** | Internal FastAPI router, same container |
| **State Reset** | Secure `/_admin/reset` injects completely dynamic schemas, tokens, and routes per episode! |
| **Port** | 7860 (HF Spaces) |

---

## Action Space

The agent sends a structured HTTP request at each step.

| Field | Type | Description |
|---|---|---|
| `method` | `str` | HTTP method β€” `GET`, `POST`, `PUT`, `DELETE`, `PATCH` |
| `url` | `str` | Endpoint path e.g. `/mock_api/users` |
| `headers` | `dict` | Request headers e.g. `{"Authorization": "Bearer token"}` |
| `body` | `dict` | Request body β€” `null` for GET requests |
| `query_params` | `dict` | URL query parameters e.g. `{"q": "python"}` |

---

## Observation Space

After each step the agent receives:

| Field | Type | Description |
|---|---|---|
| `task_id` | `str` | Current task β€” `easy`, `medium`, or `hard` |
| `task_description` | `str` | Plain-English description of what needs to be fixed |
| `broken_request` | `dict` | The original broken request shown at episode start |
| `last_status_code` | `int` | HTTP status from last step (0 = not yet tried) |
| `last_response_headers` | `dict` | Response headers from last step |
| `last_response_body` | `str` | Raw response body from last step |
| `step_feedback` | `str` | Human-readable hint based on last error |
| `current_score` | `float` | Running reward 0.0–1.0 |
| `reward` | `float` | Reward for the last step |
| `done` | `bool` | Whether the episode has ended |
| `attempt` | `int` | Current step number |

---

## Tasks

### Easy β€” Auth and Header Errors

Simple fixes to authentication headers and request format.

| Task ID | Bug | What the Agent Needs to Fix |
|---|---|---|
| `easy_auth` | Missing `Authorization` header | Add `Authorization: Bearer demo_token_123` to GET `/mock_api/users` |
| `easy_content_type` | Wrong `Content-Type: text/plain` | Change to `application/json` and add `{"name": "book"}` body to POST `/mock_api/items` |
| `easy_query_param` | Missing `?q=` query param | Add `q=python` to GET `/mock_api/search` |

### Medium β€” Method and Body Errors

Requires understanding API semantics and request structure.

| Task ID | Bug | What the Agent Needs to Fix |
|---|---|---|
| `medium_wrong_method` | `GET` instead of `POST` | Change method to POST on `/mock_api/orders` |
| `medium_type_mismatch` | `product_id: "five"` (string) | Fix to integer `5` in body |
| `medium_nested_field` | `address: "123 Main St"` (string) | Fix to dict `{"street": "123 Main St", "city": "NY"}` |

### Hard β€” Rate Limiting and Multi-step Auth

Requires multi-step reasoning and handling stateful API behaviour.

| Task ID | Bug | What the Agent Needs to Fix |
|---|---|---|
| `hard_token_exchange` | Stale/expired Bearer token | First POST to `/mock_api/auth/token` to get a fresh token, then use it on GET `/mock_api/protected` |
| `hard_rate_limit` | No backoff after 429 | After receiving 429, include `X-Retry-After: 2` header on next request |
| `hard_pagination` | Never follows cursor | Call GET `/mock_api/logs?cursor=<value>` until `has_more` is `false` |

### Expert β€” Sandbox & Undocumented APIs

Advanced RL environment forcing pure systematic exploration and state tracking.

| Task ID | Bug | What the Agent Needs to Fix |
|---|---|---|
| `expert_openapi_discovery` | Completely undocumented endpoint | Query `/mock_api/system/metrics` utilizing OPTIONS/Swagger to find correct schema |
| `expert_stateful_chain` | UUIDs change every run | Create a user, extract the generated `user_id`, and immediately update their preferences |
| `expert_transaction_rollback` | Multi-step transaction | Begin a transaction, fail it intentionally, and issue a rollback command cleanly |

---

## Reward Function

```
base_score = schema_match_score
attempt_bonus = max(0.0, 1.0 βˆ’ (attempt / max_steps) Γ— 0.15)
exploration_bonus = 0.05 per successfully reached novel endpoint

reward = round(min(0.999, max(0.001, base_score Γ— attempt_bonus + exploration_bonus)), 4)
```

Agents are rewarded more for solving tasks in fewer steps. Partial credit is given at every step:

| HTTP Status Received | Partial Reward | Meaning |
|---|---|---|
| 0 | 0.001 | Request never sent / connection error |
| 401 / 403 / 404 | ~0.05 | Hit the endpoint, auth failed |
| 405 / 415 | ~0.10 | Auth ok, wrong method or content-type |
| 422 | ~0.15 | Auth ok, body validation failed |
| 429 | ~0.20 | Hit endpoint, needs rate limit handling |
| 200 (schema mismatch) | ~0.25 | Right status, wrong response structure |
| 200 (schema match) | 0.85–0.999 | Correct β€” higher reward for fewer attempts |

---

## πŸš€ Advanced Environment Capabilities

* **Dynamic Data Generation**: To destroy brute-force LLM memorization, the `/_admin/reset` logic generates random `uuid`-based API tokens and randomly shifts required JSON key structures (e.g. `body["name"]` vs `body["dynamic_item_field"]`) at the start of every episode.
* **Exploration Bonus & Curriculum**: Agents receive a steady $+0.05$ reward for investigating new HTTP routes successfully. By specifying `task_id="auto"`, the RL environment tracks cross-episode metrics, automatically promoting the model's difficulty curriculum without external scaffolding!
* **Multi-Step Array Grading**: The deterministic grader processes tasks over multi-step state arrays. Tasks like `expert_stateful_chain` require sequenced POST & DELETE combos cleanly scored progressively.

---

## Mock API Endpoints

All endpoints are mounted at `/mock_api/` inside the same container β€” no external calls, fully reproducible.

| Method | Path | What it Does | Common Error |
|---|---|---|---|
| `GET` | `/mock_api/users` | Returns user list | 401 if no Bearer token |
| `POST` | `/mock_api/items` | Creates an item | 415 if wrong Content-Type, 422 if no `name` |
| `GET` | `/mock_api/search` | Search results | 422 if no `?q=` param |
| `POST` | `/mock_api/orders` | Creates an order | 405 if GET, 422 if wrong types |
| `POST` | `/mock_api/profile` | Updates profile | 422 if `address` is not a dict |
| `POST` | `/mock_api/auth/token` | Issues a token | 401 if wrong credentials |
| `GET` | `/mock_api/protected` | Protected resource | 401 if token not from `/auth/token` |
| `GET` | `/mock_api/rate_limited` | Rate-limited resource | 429 after 3 requests without `X-Retry-After` |
| `GET` | `/mock_api/logs` | Paginated log entries | Returns cursor β€” must follow chain |

---

## API Endpoints (OpenEnv)

| Method | Path | Description |
|---|---|---|
| `GET` | `/health` | Health check β€” returns `{"status": "ok"}` |
| `POST` | `/reset` | Start new episode. Body: `{"task_id": "easy"}` |
| `POST` | `/step` | Execute action. Body: `{"action": {...}}` |
| `GET` | `/state` | Get current episode state |
| `GET` | `/docs` | Auto-generated Swagger UI |

---

## Quick Start

### Connect to the deployed Space

```python
from client import APIDebugEnv
from models import APIAction

with APIDebugEnv(base_url="https://ProthamD-api-debug-env.hf.space").sync() as env:
    # Start an episode
    obs = env.reset(task_id="easy")
    print(obs.task_description)
    print(obs.broken_request)

    # Fix the request and step
    result = env.step(APIAction(
        method="GET",
        url="/mock_api/users",
        headers={"Authorization": "Bearer demo_token_123"},
        body=None,
        query_params={}
    ))
    print(result.reward)       # 0.85+
    print(result.observation.step_feedback)
```

### Install the client

```bash
pip install git+https://huggingface.co/spaces/ProthamD/api-debug-env
```

### Run locally with Docker

```bash
docker pull registry.hf.space/ProthamD-api-debug-env:latest
docker run -p 7860:7860 registry.hf.space/ProthamD-api-debug-env:latest
```

---

## Run the Baseline Inference Script

```bash
# Set environment variables
export API_BASE_URL=https://router.huggingface.co/v1
export MODEL_NAME=mistralai/Mistral-7B-Instruct-v0.3
export HF_TOKEN=hf_your_token_here
export ENV_URL=https://ProthamD-api-debug-env.hf.space

# Run
python inference.py
```

### Expected log format

```json
[START] task=easy env=api_debug_env model=mistralai/Mistral-7B-Instruct-v0.3
[STEP] step=1 action={"method":"GET","url":"/mock_api/users",...} reward=0.05 done=false error=null
[STEP] step=2 action={"method":"GET","url":"/mock_api/users","headers":{"Authorization":"Bearer demo_token_123"},...} reward=0.92 done=true error=null
[END] success=true steps=2 score=0.920 rewards=0.05,0.92
```

---

## Project Structure

```
api_debug_env/
β”œβ”€β”€ inference.py              ← Baseline inference script (root, mandatory)
β”œβ”€β”€ models.py                 ← APIAction, APIObservation, APIState
β”œβ”€β”€ client.py                 ← APIDebugEnv(EnvClient)
β”œβ”€β”€ openenv.yaml              ← Environment manifest
β”œβ”€β”€ pyproject.toml            ← Package config
β”œβ”€β”€ Dockerfile                ← HF Spaces Dockerfile (port 7860)
β”œβ”€β”€ tasks/
β”‚   β”œβ”€β”€ easy.py               ← 3 easy tasks
β”‚   β”œβ”€β”€ medium.py             ← 3 medium tasks
β”‚   β”œβ”€β”€ hard.py               ← 3 hard/multi-step tasks
β”‚   β”œβ”€β”€ expert.py             ← 3 expert advanced RL tasks
β”‚   └── registry.py           ← TASK_REGISTRY dict
β”œβ”€β”€ graders/
β”‚   └── grader.py             ← Deterministic reward logic
β”œβ”€β”€ tests/                    ← New! Unit tests for environment & grader
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── test_environment.py
β”œβ”€β”€ server/
    β”œβ”€β”€ app.py                ← FastAPI app with create_app()
    β”œβ”€β”€ api_debug_environment.py  ← Environment logic
    β”œβ”€β”€ mock_api.py           ← Internal mock REST API router
    └── requirements.txt
```

---

## Setup from Source

```bash
git clone https://huggingface.co/spaces/ProthamD/api-debug-env
cd api-debug-env

pip install openenv-core fastapi uvicorn httpx pydantic openai python-dotenv

# Windows
$env:PYTHONPATH = "path\to\api-debug-env"

# Linux/Mac
export PYTHONPATH=$(pwd)

uvicorn server.app:app --host 0.0.0.0 --port 7860
```

Test it:
```bash
curl http://localhost:7860/health
curl -X POST http://localhost:7860/reset \
  -H "Content-Type: application/json" \
  -d '{"task_id": "easy"}'
```

---

## πŸ§ͺ Testing

The environment includes a comprehensive test suite covering the grader, task registry, and episode lifecycle.

```bash
# Run all tests
python -m pytest tests/ -v
```

---

## Environment Variables

| Variable | Required | Description |
|---|---|---|
| `API_BASE_URL` | Yes | LLM API endpoint e.g. `https://router.huggingface.co/v1` |
| `MODEL_NAME` | Yes | Model identifier e.g. `mistralai/Mistral-7B-Instruct-v0.3` |
| `HF_TOKEN` | Yes | HuggingFace token with inference access |
| `ENV_URL` | No | Override environment URL (default: localhost) |
| `MOCK_BASE_URL` | No | Internal URL for the Mock API (default: http://localhost:7860) |

---

## Why API Debugging?

Debugging broken HTTP requests is one of the most common real-world developer tasks. Every backend developer, DevOps engineer, and API integrator does this daily. Unlike existing OpenEnv environments (games, code execution, financial simulations), there was no environment for this domain.

Key advantages of this domain for RL training:
- **Deterministic grading** β€” HTTP status codes are binary, no LLM judge needed
- **Rich partial reward signal** β€” agent gets meaningful feedback at every step
- **Stateful multi-turn reasoning** β€” hard tasks require chaining multiple requests
- **Real-world transferability** β€” skills learned here apply directly to production debugging

---

## License

MIT

## Author

Pratham Dey (ProthamD) β€” IIEST Shibpur, Information Technology