Commit Β·
0cca1b6
1
Parent(s): e93bbca
Update README file
Browse files
README.md
CHANGED
|
@@ -11,159 +11,231 @@ tags:
|
|
| 11 |
- openenv
|
| 12 |
---
|
| 13 |
|
| 14 |
-
# ACRE
|
| 15 |
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
|
| 22 |
-
##
|
| 23 |
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|---|---|---|
|
| 28 |
-
| 0 | rename_variable | Rename generic variables like `x`, `tmp`, and `i` |
|
| 29 |
-
| 1 | remove_dead_code | Remove unreachable statements, `if False` branches, and unused assignments |
|
| 30 |
-
| 2 | simplify_loop | Convert append-loops into list comprehensions |
|
| 31 |
-
| 3 | optimize_condition | Simplify `not not x`, `if True`, `if False`, and boolean comparisons |
|
| 32 |
-
| 4 | inline_function | Inline simple single-return module-level functions |
|
| 33 |
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
- `complexity_score`
|
| 40 |
-
- `runtime_s`
|
| 41 |
-
- `error_flag`
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
- `ActionModel`
|
| 49 |
-
- `RewardModel`
|
| 50 |
-
- `StateResponse`
|
| 51 |
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
|
| 62 |
| Task ID | Difficulty | Objective |
|
| 63 |
-
|---|---|---|
|
| 64 |
-
| `rename_variables` | Easy |
|
| 65 |
-
| `remove_dead_code` | Medium |
|
| 66 |
-
| `full_refactor` | Hard | Combine
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
|---|---|---|
|
| 86 |
| GET | `/` | Health check |
|
| 87 |
-
| GET | `/health` | Compatibility
|
| 88 |
-
| POST | `/reset` | Reset environment
|
| 89 |
-
| POST | `/step` |
|
| 90 |
-
| GET | `/state` |
|
| 91 |
-
| GET | `/tasks` | List
|
| 92 |
-
| POST | `/tasks/{task_id}/grade` | Grade
|
| 93 |
|
| 94 |
-
|
| 95 |
|
| 96 |
-
##
|
| 97 |
|
| 98 |
```bash
|
| 99 |
pip install -r requirements.txt
|
| 100 |
python server.py
|
| 101 |
```
|
| 102 |
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
Set environment variables
|
| 106 |
|
| 107 |
```bash
|
| 108 |
export API_BASE_URL=https://api.openai.com/v1
|
| 109 |
export MODEL_NAME=gpt-4o-mini
|
| 110 |
export HF_TOKEN=your_key
|
| 111 |
export ENV_URL=http://localhost:7860
|
| 112 |
-
python inference.py
|
| 113 |
```
|
| 114 |
|
| 115 |
-
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
| 122 |
|
| 123 |
-
```
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
-e HF_TOKEN=your_key \
|
| 129 |
-
-e ENV_URL=http://localhost:7860 \
|
| 130 |
-
acre
|
| 131 |
```
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
|
| 136 |
|
| 137 |
-
|
| 138 |
|
| 139 |
```bash
|
| 140 |
python validate.py --url http://localhost:7860
|
| 141 |
```
|
| 142 |
|
| 143 |
-
|
| 144 |
|
| 145 |
```bash
|
| 146 |
openenv validate
|
| 147 |
```
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
Start the server and open:
|
| 152 |
-
|
| 153 |
-
```text
|
| 154 |
-
http://localhost:7860/demo
|
| 155 |
-
```
|
| 156 |
|
| 157 |
-
|
| 158 |
|
| 159 |
-
|
| 160 |
-
- Optimized code
|
| 161 |
-
- Unified diff
|
| 162 |
-
- Per-step action and reward logs
|
| 163 |
|
| 164 |
-
|
| 165 |
|
| 166 |
-
|
| 167 |
|
| 168 |
| Task | Score |
|
| 169 |
|---|---|
|
|
@@ -172,4 +244,17 @@ The deterministic fallback policy used by `inference.py` produces the following
|
|
| 172 |
| `full_refactor` | 0.7143 |
|
| 173 |
| Average | 0.6548 |
|
| 174 |
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
- openenv
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# π ACRE β Autonomous Code Refactoring Environment
|
| 15 |
|
| 16 |
+
> OpenEnv-powered AI system for real-world code optimization, refactoring, and evaluation.
|
| 17 |
|
| 18 |
+

|
| 19 |
+

|
| 20 |
+

|
| 21 |
|
| 22 |
+
---
|
| 23 |
|
| 24 |
+
## π₯ Overview
|
| 25 |
|
| 26 |
+
ACRE is an OpenEnv-compliant environment designed to simulate real-world software engineering workflows such as code cleanup, optimization, and refactoring using AI agents.
|
| 27 |
|
| 28 |
+
It enables agents to iteratively improve code through structured actions while receiving dense, step-wise reward feedback.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
---
|
| 31 |
|
| 32 |
+
## π‘ Why This Matters
|
| 33 |
|
| 34 |
+
Modern software systems require automated code optimization and intelligent tooling.
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
ACRE enables:
|
| 37 |
+
- π€ AI coding assistants
|
| 38 |
+
- π Automated code review systems
|
| 39 |
+
- β‘ Reinforcement learning-based optimization agents
|
| 40 |
+
- π§ Learning real developer workflows
|
| 41 |
|
| 42 |
+
---
|
| 43 |
|
| 44 |
+
## π How It Works
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
Code β Action β Refactor β Reward β Repeat
|
| 47 |
|
| 48 |
+
1. Load messy code
|
| 49 |
+
2. Apply transformation
|
| 50 |
+
3. Evaluate using grader
|
| 51 |
+
4. Compute reward
|
| 52 |
+
5. Iterate until optimal
|
| 53 |
+
|
| 54 |
+
---
|
| 55 |
+
|
| 56 |
+
## π§ Key Features
|
| 57 |
+
|
| 58 |
+
- β
Autonomous code refactoring
|
| 59 |
+
- β‘ Step-wise reward feedback
|
| 60 |
+
- π§ͺ OpenEnv compliant interface
|
| 61 |
+
- π Deterministic grading system
|
| 62 |
+
- π Reproducible inference pipeline
|
| 63 |
+
- π³ Fully containerized (Docker + Hugging Face Spaces)
|
| 64 |
|
| 65 |
+
---
|
| 66 |
+
|
| 67 |
+
## π Tasks
|
| 68 |
|
| 69 |
| Task ID | Difficulty | Objective |
|
| 70 |
+
|--------|----------|----------|
|
| 71 |
+
| `rename_variables` | Easy | Replace generic variable names |
|
| 72 |
+
| `remove_dead_code` | Medium | Remove unreachable logic |
|
| 73 |
+
| `full_refactor` | Hard | Combine multiple optimizations |
|
| 74 |
+
|
| 75 |
+
Each task uses AST-based transformations and deterministic grading.
|
| 76 |
+
|
| 77 |
+
---
|
| 78 |
+
|
| 79 |
+
## π― Reward System
|
| 80 |
+
|
| 81 |
+
Rewards are computed at every step:
|
| 82 |
+
|
| 83 |
+
- β
Valid executable code β positive reward
|
| 84 |
+
- π Reduced complexity β reward
|
| 85 |
+
- β‘ Improved performance β reward
|
| 86 |
+
- β Errors or invalid code β penalty
|
| 87 |
+
- π No progress β penalty
|
| 88 |
|
| 89 |
+
**Normalization:**
|
| 90 |
|
| 91 |
+
`(raw_reward + 32) / 52 β [0, 1]`
|
| 92 |
|
| 93 |
+
---
|
| 94 |
+
|
| 95 |
+
## π Example Execution
|
| 96 |
+
|
| 97 |
+
```text
|
| 98 |
+
START rename_variables
|
| 99 |
+
STEP 0
|
| 100 |
+
END 1.00
|
| 101 |
+
|
| 102 |
+
START remove_dead_code
|
| 103 |
+
STEP 1
|
| 104 |
+
END 0.25
|
| 105 |
+
|
| 106 |
+
START full_refactor
|
| 107 |
+
STEP 3
|
| 108 |
+
END 0.71
|
| 109 |
+
|
| 110 |
+
Final Score: 0.65
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
---
|
| 114 |
+
|
| 115 |
+
## ποΈ Architecture
|
| 116 |
+
|
| 117 |
+
- `server.py` β FastAPI entry point
|
| 118 |
+
- `openenv_interface.py` β OpenEnv wrapper
|
| 119 |
+
- `acre/env/` β Core environment logic
|
| 120 |
+
- `acre/tasks/` β Task definitions
|
| 121 |
+
- `acre/utils/` β Metrics and helpers
|
| 122 |
+
- `inference.py` β Evaluation pipeline
|
| 123 |
|
| 124 |
+
---
|
| 125 |
+
|
| 126 |
+
## βοΈ OpenEnv Interface
|
| 127 |
+
|
| 128 |
+
```python
|
| 129 |
+
observation = env.reset()
|
| 130 |
+
observation, reward, done, info = env.step(action)
|
| 131 |
+
state = env.state()
|
| 132 |
+
```
|
| 133 |
+
|
| 134 |
+
Uses Pydantic models:
|
| 135 |
|
| 136 |
+
- `ObservationModel`
|
| 137 |
+
- `ActionModel`
|
| 138 |
+
- `RewardModel`
|
| 139 |
|
| 140 |
+
---
|
| 141 |
|
| 142 |
+
## π HTTP API
|
| 143 |
+
|
| 144 |
+
| Method | Endpoint | Description |
|
| 145 |
|---|---|---|
|
| 146 |
| GET | `/` | Health check |
|
| 147 |
+
| GET | `/health` | Compatibility check |
|
| 148 |
+
| POST | `/reset` | Reset environment |
|
| 149 |
+
| POST | `/step` | Execute action |
|
| 150 |
+
| GET | `/state` | Get state |
|
| 151 |
+
| GET | `/tasks` | List tasks |
|
| 152 |
+
| POST | `/tasks/{task_id}/grade` | Grade code |
|
| 153 |
|
| 154 |
+
---
|
| 155 |
|
| 156 |
+
## π Run Locally
|
| 157 |
|
| 158 |
```bash
|
| 159 |
pip install -r requirements.txt
|
| 160 |
python server.py
|
| 161 |
```
|
| 162 |
|
| 163 |
+
---
|
| 164 |
+
|
| 165 |
+
## π³ Docker / Hugging Face Spaces
|
| 166 |
+
|
| 167 |
+
```bash
|
| 168 |
+
docker build -t acre .
|
| 169 |
+
docker run -p 7860:7860 \
|
| 170 |
+
-e API_BASE_URL=https://api.openai.com/v1 \
|
| 171 |
+
-e MODEL_NAME=gpt-4o-mini \
|
| 172 |
+
-e HF_TOKEN=your_key \
|
| 173 |
+
-e ENV_URL=http://localhost:7860 \
|
| 174 |
+
acre
|
| 175 |
+
```
|
| 176 |
+
|
| 177 |
+
---
|
| 178 |
+
|
| 179 |
+
## π§ͺ Inference
|
| 180 |
|
| 181 |
+
Set environment variables:
|
| 182 |
|
| 183 |
```bash
|
| 184 |
export API_BASE_URL=https://api.openai.com/v1
|
| 185 |
export MODEL_NAME=gpt-4o-mini
|
| 186 |
export HF_TOKEN=your_key
|
| 187 |
export ENV_URL=http://localhost:7860
|
|
|
|
| 188 |
```
|
| 189 |
|
| 190 |
+
Run:
|
| 191 |
|
| 192 |
+
```bash
|
| 193 |
+
python inference.py
|
| 194 |
+
```
|
| 195 |
|
| 196 |
+
Expected output:
|
| 197 |
|
| 198 |
+
```text
|
| 199 |
+
Easy: 1.00
|
| 200 |
+
Medium: 0.25
|
| 201 |
+
Hard: 0.71
|
| 202 |
+
Final: 0.65
|
|
|
|
|
|
|
|
|
|
| 203 |
```
|
| 204 |
|
| 205 |
+
---
|
| 206 |
+
|
| 207 |
+
## π OpenEnv Compliance
|
| 208 |
+
|
| 209 |
+
- β `step()` implemented
|
| 210 |
+
- β `reset()` implemented
|
| 211 |
+
- β `state()` implemented
|
| 212 |
+
- β reward shaping
|
| 213 |
+
- β deterministic grading
|
| 214 |
+
- β structured logs
|
| 215 |
|
| 216 |
+
---
|
| 217 |
|
| 218 |
+
## π§ͺ Validation
|
| 219 |
|
| 220 |
```bash
|
| 221 |
python validate.py --url http://localhost:7860
|
| 222 |
```
|
| 223 |
|
| 224 |
+
Or:
|
| 225 |
|
| 226 |
```bash
|
| 227 |
openenv validate
|
| 228 |
```
|
| 229 |
|
| 230 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
+
## π Live Demo
|
| 233 |
|
| 234 |
+
π Running on Hugging Face Spaces
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
+
---
|
| 237 |
|
| 238 |
+
## π Baseline Performance
|
| 239 |
|
| 240 |
| Task | Score |
|
| 241 |
|---|---|
|
|
|
|
| 244 |
| `full_refactor` | 0.7143 |
|
| 245 |
| Average | 0.6548 |
|
| 246 |
|
| 247 |
+
---
|
| 248 |
+
|
| 249 |
+
## π Use Cases
|
| 250 |
+
|
| 251 |
+
- AI-powered code optimization
|
| 252 |
+
- Automated refactoring tools
|
| 253 |
+
- Reinforcement learning environments
|
| 254 |
+
- Developer productivity systems
|
| 255 |
+
|
| 256 |
+
---
|
| 257 |
+
|
| 258 |
+
## π License
|
| 259 |
+
|
| 260 |
+
MIT License
|