File size: 10,383 Bytes
ad69c81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import json, os, random, subprocess, tempfile, sys, time
from typing import Optional, List

from server.executor import run_code_with_tests
from server.grader import calculate_codearena_reward

app = FastAPI(title="CodeArena RL Environment")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"]
)

# -- State ------------------------------------------------
current_task = {}
step_count = 0
previous_attempts = []
episode_rewards = []
TASKS_DIR = os.path.join(os.path.dirname(__file__), "..", "tasks")

# -- Models -----------------------------------------------
class ResetRequest(BaseModel):
    task_id: str = "easy"
    buggy_code: Optional[str] = None

class StepRequest(BaseModel):
    proposed_fix: str


class AgentPerformanceTracker:
    def __init__(self):
        self.episode_rewards = []
        self.current_difficulty = "easy"
        self.steps_at_difficulty = 0

    def record_episode(self, avg_reward: float):
        self.episode_rewards.append(round(avg_reward, 4))
        if len(self.episode_rewards) > 10:
            self.episode_rewards.pop(0)
        self.steps_at_difficulty += 1
        self._maybe_escalate()

    def _maybe_escalate(self):
        if self.steps_at_difficulty < 3:
            return
        if not self.episode_rewards:
            return
        avg = sum(self.episode_rewards) / len(self.episode_rewards)
        old = self.current_difficulty
        if avg > 0.80 and self.current_difficulty == "easy":
            self.current_difficulty = "medium"
            self.steps_at_difficulty = 0
        elif avg > 0.75 and self.current_difficulty == "medium":
            self.current_difficulty = "hard"
            self.steps_at_difficulty = 0
        elif avg < 0.35 and self.current_difficulty == "hard":
            self.current_difficulty = "medium"
            self.steps_at_difficulty = 0
        elif avg < 0.35 and self.current_difficulty == "medium":
            self.current_difficulty = "easy"
            self.steps_at_difficulty = 0
        if self.current_difficulty != old:
            print(
                f"[CURRICULUM] {old} -> {self.current_difficulty} "
                f"after avg={avg:.3f}"
            )

    def get_difficulty(self) -> str:
        return self.current_difficulty

    def get_stats(self) -> dict:
        avg = (
            sum(self.episode_rewards) / len(self.episode_rewards)
            if self.episode_rewards else 0.0
        )
        return {
            "current_difficulty": self.current_difficulty,
            "recent_avg_reward": round(avg, 3),
            "episodes_tracked": len(self.episode_rewards),
            "steps_at_current_difficulty": self.steps_at_difficulty
        }


tracker = AgentPerformanceTracker()

# -- Helpers ----------------------------------------------
def load_random_task(difficulty: str):
    folder = os.path.join(TASKS_DIR, difficulty)
    files = [f for f in os.listdir(folder) if f.endswith(".json")]
    if not files:
        raise ValueError(f"No tasks found in {folder}")
    path = os.path.join(folder, random.choice(files))
    with open(path) as f:
        return json.load(f)

def run_tests(code: str, tests: list):
    passed = 0
    total = len(tests)
    compile_ok = True
    error_log = ""
    start_time = time.time()

    with tempfile.NamedTemporaryFile(mode="w", suffix=".py",
                                     delete=False, dir=tempfile.gettempdir()) as tmp:
        tmp.write(code)
        tmp_path = tmp.name

    try:
        for test in tests:
            inp = test["input"]
            expected = test["expected"]

            # Build a test runner script
            test_script = f"""
import sys
sys.path.insert(0, '')
exec(open(r'{tmp_path}').read())

inp = {repr(inp)}
if isinstance(inp, list):
    result = list(locals().values())[-1](*inp) if callable(list(locals().values())[-1]) else None
    # find the function
    import types
    funcs = {{k:v for k,v in locals().items() if isinstance(v, types.FunctionType)}}
    if funcs:
        fn = list(funcs.values())[0]
        result = fn(*inp)
    else:
        result = None
else:
    result = None

expected = {repr(expected)}
print('PASS' if result == expected else f'FAIL got {{result}} expected {{expected}}')
"""
            runner = tempfile.NamedTemporaryFile(mode="w", suffix=".py",
                                                  delete=False, dir=tempfile.gettempdir())
            runner.write(test_script)
            runner.close()

            try:
                proc = subprocess.run(
                    [sys.executable, runner.name],
                    capture_output=True, text=True, timeout=5
                )
                if "PASS" in proc.stdout:
                    passed += 1
                elif proc.returncode != 0:
                    compile_ok = False
                    error_log = proc.stderr[:300]
            except subprocess.TimeoutExpired:
                error_log = "Timeout"
            finally:
                os.unlink(runner.name)

    finally:
        os.unlink(tmp_path)

    return compile_ok, passed, total, error_log, time.time() - start_time

def evaluate_fix(code: str, task: dict):
    if "test_code" in task:
        result = run_code_with_tests(
            code=code,
            test_code=task.get("test_code", ""),
            timeout=max(float(task.get("optimal_time_seconds", 0.05)) * 10, 2.0),
        )
        return (
            result.compile_success,
            result.test_passed,
            max(result.test_total, 1),
            result.runtime_errors,
            result.execution_time_seconds,
        )

    return run_tests(code, task.get("tests", []))

# -- Endpoints --------------------------------------------
@app.get("/")
def health():
    return {"status": "ok", "environment": "CodeArena"}

@app.post("/reset")
def reset(req: ResetRequest):
    global current_task, step_count, previous_attempts, episode_rewards

    step_count = 0
    previous_attempts = []
    episode_rewards = []

    if req.buggy_code:
        # Custom mode -- user pasted their own broken code
        current_task = {
            "task_id": "custom",
            "buggy_code": req.buggy_code,
            "description": "User-provided code -- fix the bug",
            "tests": [
                {"input": [1, 2], "expected": None},
                {"input": [0, 0], "expected": None},
                {"input": [5, 5], "expected": None}
            ]
        }
    elif req.task_id in ("easy", "medium", "hard", "type_errors", "security_bugs"):
        current_task = load_random_task(req.task_id)
    elif req.task_id == "auto":
        current_task = load_random_task(tracker.get_difficulty())
    else:
        current_task = load_random_task("easy")

    tests_count = len(current_task.get("tests", []))
    if "test_code" in current_task:
        tests_count = 1

    return {
        "task_id": current_task["task_id"],
        "curriculum_info": tracker.get_stats(),
        "observation": {
            "buggy_code": current_task["buggy_code"],
            "error_log": "",
            "test_results": f"0/{tests_count} tests passing",
            "previous_attempts": []
        }
    }

@app.post("/step")
def step(req: StepRequest):
    global step_count, previous_attempts, episode_rewards

    step_count += 1
    is_repeated_fix = req.proposed_fix in previous_attempts

    tests = current_task.get("tests", [])
    
    # For custom tasks with no expected values, just check compile + run
    if current_task["task_id"] == "custom":
        compile_ok, passed, total, error_log, execution_time_seconds = True, 0, 1, "", 0.0
        try:
            with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
                f.write(req.proposed_fix)
                tmp_path = f.name
            start_time = time.time()
            proc = subprocess.run([sys.executable, tmp_path],
                                   capture_output=True, text=True, timeout=5)
            execution_time_seconds = time.time() - start_time
            compile_ok = proc.returncode == 0
            passed = 1 if compile_ok else 0
            error_log = proc.stderr[:300] if not compile_ok else ""
            os.unlink(tmp_path)
        except Exception as e:
            compile_ok = False
            error_log = str(e)
    else:
        compile_ok, passed, total, error_log, execution_time_seconds = evaluate_fix(
            req.proposed_fix,
            current_task,
        )
        total = max(total, 1)

    reward, reward_components = calculate_codearena_reward(
        compile_ok=compile_ok,
        passed=passed,
        total=total,
        execution_time_seconds=execution_time_seconds,
        optimal_time_seconds=float(current_task.get("optimal_time_seconds", 0.05)),
        buggy_code=current_task.get("buggy_code", ""),
        proposed_fix=req.proposed_fix,
        task_category=current_task.get("difficulty", current_task.get("task_id", "easy").split("-")[0]),
        step_count=step_count,
        is_repeated_fix=is_repeated_fix,
    )
    episode_rewards.append(reward)
    previous_attempts.append(req.proposed_fix)
    done = (reward > 0.95) or (step_count >= 5)
    if done and episode_rewards:
        tracker.record_episode(sum(episode_rewards) / len(episode_rewards))

    tests_total = len(tests) if tests else total

    return {
        "reward": reward,
        "done": done,
        "reward_components": reward_components,
        "curriculum_info": tracker.get_stats(),
        "observation": {
            "buggy_code": current_task["buggy_code"],
            "error_log": error_log,
            "test_results": f"{passed}/{tests_total} tests passing",
            "previous_attempts": previous_attempts[-3:]
        }
    }

@app.get("/state")
def state():
    return {
        "task_id": current_task.get("task_id", "none"),
        "step_count": step_count,
        "observation": {
            "buggy_code": current_task.get("buggy_code", ""),
            "error_log": "",
            "test_results": "",
            "previous_attempts": previous_attempts
        }
    }

@app.get("/curriculum")
async def get_curriculum():
    return tracker.get_stats()