Spaces:
Sleeping
Sleeping
| title: SQL Debugger & Optimizer | |
| emoji: π οΈ | |
| colorFrom: blue | |
| colorTo: indigo | |
| sdk: docker | |
| app_file: app.py | |
| pinned: false | |
| # π οΈ SQL Debugger & Optimizer β OpenEnv Environment | |
| > π **NeuroHack β OpenEnv Submission** | |
| > A real-world reinforcement learning environment where an AI agent debugs broken SQL queries using **deterministic SQLite execution** β no LLM-based scoring. | |
| --- | |
| ## π Table of Contents | |
| - [Overview](#overview) | |
| - [Why This Wins](#why-this-wins) | |
| - [Live Demo](#live-demo) | |
| - [Project Structure](#project-structure) | |
| - [Getting Started](#getting-started) | |
| - [Run Locally](#run-locally) | |
| - [Docker](#docker) | |
| - [API Example](#api-example) | |
| - [Environment Details](#environment-details) | |
| - [Tasks](#tasks) | |
| - [Reward System](#reward-system) | |
| - [Performance](#performance) | |
| - [License](#license) | |
| --- | |
| ## Overview | |
| The **SQL Debugger & Optimizer** is an OpenEnv-compatible reinforcement learning environment where an AI agent receives broken SQL queries and must output corrected versions. | |
| The agent receives: | |
| - π΄ A **broken SQL query** | |
| - ποΈ The **database schema** | |
| - π A **natural language description** of the intended behavior | |
| It must output a **corrected SQL query**, which is then: | |
| 1. Executed against a real SQLite database | |
| 2. Compared against the reference (correct) query output | |
| 3. Scored with a layered reward from `0.0 β 1.0` | |
| --- | |
| ## Why This Wins | |
| Unlike traditional LLM-evaluated systems: | |
| | Feature | Description | | |
| |--------|-------------| | |
| | β **100% Deterministic Grading** | Real SQLite execution β no subjective LLM scoring | | |
| | β **Layered Reward System** | Syntax β Logic β Data β Optimization | | |
| | β **Real-World Bugs** | JOIN errors, N+1 queries, SQL injection vulnerabilities | | |
| | β **Engineering Relevance** | Mirrors production database debugging scenarios | | |
| | β **OpenEnv-Compatible API** | Drop-in `/reset` and `/step` endpoints | | |
| --- | |
| ## Live Demo | |
| Open the interactive UI at: | |
| ``` | |
| http://localhost:7860/ui | |
| ``` | |
| With the UI you can: | |
| - Fix broken SQL queries interactively | |
| - View the corrected SQL output | |
| - See reward scores in real-time | |
| - Track agent performance with graphs | |
| --- | |
| ## Project Structure | |
| ``` | |
| sql-debugger-env/ | |
| βββ app.py # FastAPI server β /reset, /step, /ui endpoints | |
| βββ sql_debugger_env.py # Core RL environment logic & SQLite execution | |
| βββ inference.py # Agent inference utilities | |
| βββ openenv.yaml # OpenEnv environment specification | |
| βββ requirements.txt # Python dependencies | |
| βββ Dockerfile # Container build configuration | |
| βββ README.md # This file | |
| ``` | |
| --- | |
| ## Getting Started | |
| ### Run Locally | |
| ```bash | |
| pip install -r requirements.txt | |
| python app.py | |
| ``` | |
| Then open: [http://localhost:7860/ui](http://localhost:7860/ui) | |
| --- | |
| ### Docker | |
| ```bash | |
| # Build the image | |
| docker build -t sql-debugger-env . | |
| # Run the container | |
| docker run -p 7860:7860 sql-debugger-env | |
| ``` | |
| --- | |
| ### API Example | |
| The environment exposes an OpenEnv-compatible REST API: | |
| ```python | |
| import requests | |
| # 1. Start a new episode | |
| r = requests.post("http://localhost:7860/reset", json={"task": "medium"}) | |
| session_id = r.json()["session_id"] | |
| obs = r.json()["observation"] | |
| # 2. Submit a fix action | |
| action = { | |
| "challenge_id": obs["challenge"]["id"], | |
| "fixed_sql": "SELECT users.id, orders.total FROM users JOIN orders ON users.id = orders.user_id", | |
| "explanation": "Fixed JOIN logic β was using wrong foreign key", | |
| "detected_issues": ["wrong_join"] | |
| } | |
| r = requests.post("http://localhost:7860/step", json={ | |
| "session_id": session_id, | |
| "action": action | |
| }) | |
| print(r.json()["reward"]) # e.g. 0.92 | |
| ``` | |
| **Endpoints:** | |
| | Method | Endpoint | Description | | |
| |--------|----------|-------------| | |
| | `POST` | `/reset` | Start a new episode. Accepts `{"task": "easy" \| "medium" \| "hard"}` | | |
| | `POST` | `/step` | Submit a fix. Returns reward, done flag, and next observation | | |
| | `GET` | `/ui` | Interactive web interface | | |
| --- | |
| ## Environment Details | |
| ### Tasks | |
| | Difficulty | Bug Types | | |
| |------------|-----------| | |
| | π’ **Easy** | Syntax errors (`SELCT`, `FORM`, missing `WHERE`) | | |
| | π‘ **Medium** | JOIN logic errors, aggregation mistakes | | |
| | π΄ **Hard** | N+1 query patterns, optimization issues, complex aggregations | | |
| --- | |
| ### Reward System | |
| Each submitted query is evaluated across multiple components: | |
| | Component | Weight | Description | | |
| |-----------|--------|-------------| | |
| | Syntax Correctness | `0.20` | Query parses and executes without error | | |
| | Row Count Match | `0.25` | Output row count matches reference | | |
| | Column Match | `0.15` | Returned columns match expected schema | | |
| | Data Exact Match | `0.30` | Row-by-row data comparison | | |
| | Security Fix | `0.10` | SQL injection or unsafe patterns resolved | | |
| | Optimization | `0.05` | Query avoids N+1 or redundant scans | | |
| | Explanation Quality | `0.05` | Detected issues and explanation provided | | |
| | **Total** | **1.00** | | | |
| --- | |
| ### Performance | |
| Benchmarks from the reference agent (`inference.py`): | |
| | Task | Score | | |
| |------|-------| | |
| | Easy | ~0.90 | | |
| | Medium | ~0.90 | | |
| | Hard | ~0.76 | | |
| | **Average** | π **~0.85** | | |
| --- | |
| ## License | |
| This project is licensed under the **MIT License** β see [`LICENSE`](LICENSE) for details. | |
| --- | |
| <div align="center"> | |
| Built for <strong>NeuroHack</strong> Β· OpenEnv Track Β· 2025 | |
| </div> |