-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.py
More file actions
166 lines (120 loc) · 4.11 KB
/
Copy pathenv.py
File metadata and controls
166 lines (120 loc) · 4.11 KB
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
import os
import sys
from typing import Dict, Any
from pydantic import BaseModel, Field
import random
# Fix import path
sys.path.append(os.path.abspath("."))
from data.buggy_codes import get_challenge_by_id
from data.test_cases import grade
# ─────────────────────────
# MODELS
# ─────────────────────────
class Observation(BaseModel):
challenge_id: str
difficulty: str
description: str
buggy_code: str
error_message: str
hint: str
step: int
max_steps: int
class Action(BaseModel):
fixed_code: str
class StepResult(BaseModel):
observation: Observation
reward: float
done: bool
info: Dict[str, Any]
# ─────────────────────────
# ENVIRONMENT
# ─────────────────────────
class CodeDebugEnv:
def __init__(self, difficulty="easy", seed=42, task="easy_001"):
self.difficulty = difficulty
self.seed = seed
self.task = task
if difficulty == "hard":
self.max_steps = 6
elif difficulty == "medium":
self.max_steps = 4
else:
self.max_steps = 3
self._challenge = {}
self._step = 0
self._done = False
self._rewards = []
self._best_score = 0.0
# ─────────────────────────
def reset(self):
random.seed(self.seed)
self._challenge = get_challenge_by_id(self.task)
self._step = 0
self._done = False
self._rewards = []
self._best_score = 0.0
return self._make_observation()
# ─────────────────────────
def step(self, action: Action):
if self._done:
raise RuntimeError("Episode done. Call reset() first.")
self._step += 1
grade_result = grade(action.fixed_code, self._challenge)
score = float(grade_result.get("score", 0.0))
passed = bool(grade_result.get("passed", False))
# Reward shaping
reward = score
if score > 0:
reward += 0.1
if score > self._best_score:
reward += 0.1
if score == 0:
reward = 0.0
reward = min(reward, 1.0)
self._rewards.append(reward)
self._best_score = max(self._best_score, score)
# Multi-step enforcement
if passed and self._step >= 2:
done = True
else:
done = self._step >= self.max_steps
self._done = done
return StepResult(
observation=self._make_observation(),
reward=reward,
done=done,
info={"step": self._step}
)
# ─────────────────────────
def _make_observation(self):
c = self._challenge
return Observation(
challenge_id=c.get("id", ""),
difficulty=c.get("difficulty", self.difficulty),
description=c.get("description", ""),
buggy_code=c.get("buggy_code", ""),
error_message=c.get("error_message", ""),
hint=c.get("hint", ""),
step=self._step,
max_steps=self.max_steps
)
# ─────────────────────────
# MAIN TEST (VERY IMPORTANT)
# ─────────────────────────
if __name__ == "__main__":
print("=== TEST RUN ===")
env = CodeDebugEnv(difficulty="easy", task="easy_001")
obs = env.reset()
print("BUGGY CODE:\n", obs.buggy_code)
for i in range(3):
# STEP-WISE IMPROVEMENT
if i == 0:
fixed = "def add(a,b): return a" # partial fix
else:
fixed = "def add(a,b): return a+b" # correct fix
result = env.step(Action(fixed_code=fixed))
print(f"\nStep {i+1}")
print("Reward:", result.reward)
print("Done:", result.done)
if result.done:
break