Spaces:
Sleeping
Sleeping
File size: 5,755 Bytes
ff84a08 bd15dca 89a9ddb ff84a08 89a9ddb bd15dca ff84a08 2db0a88 ef2dce7 ff84a08 ef2dce7 ff84a08 2db0a88 df1e0f1 2db0a88 ff84a08 2db0a88 df1e0f1 2db0a88 df1e0f1 2db0a88 ff84a08 2db0a88 ff84a08 2db0a88 ff84a08 2db0a88 ef2dce7 2db0a88 ff84a08 2db0a88 ff84a08 ef2dce7 ff84a08 2db0a88 ff84a08 2db0a88 ff84a08 2db0a88 ef2dce7 2db0a88 bd15dca 2db0a88 | 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 | ---
title: Synapse Code Auditor
emoji: "⚡"
colorFrom: red
colorTo: pink
sdk: docker
app_port: 7860
pinned: false
license: mit
short_description: Code reviewer environment
---
# Synapse Code Auditor (OpenEnv Environment)
Synapse Code Auditor is a production-ready OpenEnv environment for evaluating AI code-review behavior across deterministic tasks.
## Real-World Use Case
Software teams use code review to catch bugs, improve quality, and enforce maintainability. This environment simulates that workflow so agent performance can be trained and measured with reproducible rewards.
## Project Structure
```text
.
├── server/
│ ├── __init__.py
│ └── app.py
├── inference.py
├── pyproject.toml
├── uv.lock
├── validate_submission.py
├── app/
│ ├── __init__.py
│ ├── env.py
│ ├── grader.py
│ ├── inference.py
│ ├── main.py
│ ├── models.py
│ └── tasks.py
├── .dockerignore
├── Dockerfile
├── openenv.yaml
├── README.md
└── requirements.txt
```
## OpenEnv Interface
This environment implements the required interface methods in the core environment class:
- reset(task_id=None, seed=42)
- step(action)
- state()
FastAPI endpoints expose the same functionality:
- POST /reset
- POST /step
- GET /state
## Action and Observation Schemas
Observation:
```json
{
"task_id": "easy",
"task_type": "easy",
"code": "def add_numbers(a, b)\n result = a + b\n return result\n",
"instructions": "Identify the exact syntax error in the code and propose a valid fix.",
"previous_feedback": null
}
```
Action:
```json
{
"review": "The function definition is missing a colon. Change it to def add_numbers(a, b):"
}
```
Reward:
```json
{
"score": 1.0,
"matched_criteria": ["syntax error", "missing colon", "def add_numbers(a, b):"],
"missed_criteria": [],
"penalties": {},
"rationale": "Coverage=1.00; Penalty=0.00; Matched=3/3"
}
```
## Tasks
1. Easy: Detect syntax errors
2. Medium: Suggest optimization
3. Hard: Full code review with scoring
Each task has deterministic grading criteria and a score range of 0.0 to 1.0.
## Deterministic Grader and Reward Design
- Deterministic keyword/criterion matching for each task.
- Partial reward from criterion coverage.
- Penalties for short, irrelevant, or incorrect responses.
- Hard task penalizes missing overall score/rating in the review.
Reward formula:
```text
score = clamp(criterion_coverage - penalties, 0.0, 1.0)
```
## Local Setup
### 1. Create environment and install dependencies
```bash
python -m venv .venv
. .venv/Scripts/activate
pip install -r requirements.txt
```
### 2. Run FastAPI server
3+ tasks and grader output range in `(0.0, 1.0)`
```bash
uvicorn app.main:app --host 0.0.0.0 --port 7860
```
### 3. Validate OpenEnv manifest
"score": 0.99,
[STEP] {"event":"task_completed","index":1,"task_id":"easy","score":0.99}
[STEP] {"event":"task_completed","index":2,"task_id":"medium","score":0.99}
```
[END] {"event":"run_finished","average_score":0.99,"status":"ok"}
## Docker Run
```bash
docker build -t synapse-code-auditor .
docker run --rm -p 7860:7860 synapse-code-auditor
```
Then test:
```bash
curl http://localhost:7860/health
curl -X POST http://localhost:7860/reset -H "Content-Type: application/json" -d "{\"task_id\":\"easy\",\"seed\":42}"
```
## Baseline Inference Script
The baseline runner is `inference.py` at the project root. It uses the OpenAI client and executes all 3 tasks.
Required environment variables for non-dry-run execution:
- API_BASE_URL: OpenAI-compatible API URL
- MODEL_NAME: model identifier
- HF_TOKEN: API key used by OpenAI client
Optional:
- ENV_BASE_URL: environment API base URL (default http://localhost:7860)
- DRY_RUN: set to `1` to skip external LLM calls and use deterministic local responses
- API_BASE_URL: defaults to https://api.openai.com/v1 when not provided
- MODEL_NAME: defaults to gpt-4o-mini when not provided
Structured logs:
- `[START]` one event at run start
- `[STEP]` one event per task
- `[END]` one event at run finish
Run:
```bash
python inference.py
```
### Required env var example
```bash
export API_BASE_URL="https://api.openai.com/v1"
export MODEL_NAME="gpt-4o-mini"
export HF_TOKEN="your_token_here"
export ENV_BASE_URL="http://localhost:7860"
python inference.py
```
### Pre-submission validator
```bash
python validate_submission.py
```
Validator checks:
- OpenEnv manifest keys
- `/health`, `/reset`, `/step`, `/state` endpoint behavior
- 3+ tasks and grader output range in `[0.0, 1.0]`
- root `inference.py` requirements and structured logs
## Example Baseline Output
```text
[START] {"event":"run_started","env_base_url":"inprocess","task_count":3,"dry_run":true}
[STEP] {"event":"task_completed","index":1,"task_id":"easy","score":1.0}
[STEP] {"event":"task_completed","index":2,"task_id":"medium","score":1.0}
[STEP] {"event":"task_completed","index":3,"task_id":"hard","score":0.9}
[END] {"event":"run_finished","average_score":0.9667,"status":"ok"}
```
## Hugging Face Spaces Deployment
This project is container-ready and compatible with Hugging Face Docker Spaces.
1. Push repository to Hugging Face Space configured for Docker.
2. Ensure port 7860 is exposed (already set in Dockerfile and openenv.yaml).
3. Build and run automatically in Space.
OpenEnv deployment command:
```bash
openenv push
```
## API Reference
- GET /health
- POST /reset with payload {"task_id": "easy|medium|hard", "seed": 42}
- POST /step with payload {"action": {"review": "..."}}
- GET /state
This environment is ready for end-to-end local execution, Docker execution, and OpenEnv workflow validation.
|