Spaces:
Running
Running
File size: 6,561 Bytes
2ce1061 d510c1d 2ce1061 d510c1d 2ce1061 d510c1d 2ce1061 d510c1d 2ce1061 c01667e 2ce1061 d510c1d 2ce1061 d510c1d 2ce1061 d510c1d 2ce1061 d510c1d 2ce1061 d510c1d 2ce1061 d510c1d 2ce1061 d510c1d 2ce1061 d510c1d 2ce1061 d510c1d 2ce1061 d510c1d c01667e d510c1d c01667e d510c1d c01667e d510c1d c01667e d510c1d d298b6d 2ce1061 d510c1d 2ce1061 d510c1d 2ce1061 d510c1d 2ce1061 d510c1d | 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 | # server/environment.py
# Core environment: manages episode state, dispatches to task banks and graders.
import random
from uuid import uuid4
from typing import Optional
from openenv.core.env_server.interfaces import Environment
from openenv.core.env_server.types import State
from models import DebugAction, DebugObservation, DebugState
from server.tasks.task_easy import get_random_easy_task
from server.tasks.task_medium import get_random_medium_task
from server.tasks.task_hard import get_random_hard_task
from server.graders.grader_easy import grade_easy
from server.graders.grader_medium import grade_medium
from server.graders.grader_hard import grade_hard
TASK_GETTERS = {
"easy": get_random_easy_task,
"medium": get_random_medium_task,
"hard": get_random_hard_task,
}
GRADERS = {
"easy": grade_easy,
"medium": grade_medium,
"hard": grade_hard,
}
MAX_STEPS = 5
class CodeDebugEnvironment(Environment):
"""
OpenEnv environment for LLM-based code debugging.
Supports 3 difficulty levels with partial rewards and cumulative tracking.
"""
def __init__(self):
self._episode_id: str = str(uuid4())
self._difficulty: str = "easy"
self._current_task: Optional[dict] = None
self._step_count: int = 0
self._cumulative_reward: float = 0.0
self._best_reward: float = 0.0
self._current_reward: float = 0.0
self._done: bool = False
def reset(self, difficulty: Optional[str] = None) -> DebugObservation:
"""Start a new episode. Optionally specify difficulty: easy | medium | hard."""
self._episode_id = str(uuid4())
self._step_count = 0
self._cumulative_reward = 0.0
self._best_reward = 0.0
self._current_reward = 0.0
self._done = False
if difficulty and difficulty in TASK_GETTERS:
self._difficulty = difficulty
else:
self._difficulty = random.choice(["easy", "medium", "hard"])
self._current_task = TASK_GETTERS[self._difficulty]()
return DebugObservation(
task_id=self._current_task["task_id"],
difficulty=self._difficulty,
buggy_code=self._current_task["buggy_code"],
instructions=self._current_task["instructions"],
test_cases_description=self._current_task["test_cases_description"],
reward=None,
cumulative_reward=0.0,
best_reward=0.0,
passed_tests=None,
total_tests=len(self._current_task["test_cases"]),
feedback=None,
done=False,
)
def step(self, action: DebugAction) -> DebugObservation:
"""Submit fixed_code. Returns observation with reward, cumulative_reward, feedback, done."""
if self._done:
return DebugObservation(
task_id=self._current_task["task_id"] if self._current_task else "none",
difficulty=self._difficulty,
buggy_code=self._current_task["buggy_code"] if self._current_task else "",
instructions="Episode done. Call reset() to start a new episode.",
test_cases_description="",
reward=self._best_reward,
cumulative_reward=self._cumulative_reward,
best_reward=self._best_reward,
passed_tests=None,
total_tests=0,
feedback="Episode ended. Call reset() to start a new task.",
done=True,
)
self._step_count += 1
# ββ Invalid action penalty βββββββββββββββββββββββββββββββββββββββββ
code = action.fixed_code.strip() if action.fixed_code else ""
if not code:
done = self._step_count >= MAX_STEPS
self._done = done
self._cumulative_reward += 0.0
return DebugObservation(
task_id=self._current_task["task_id"],
difficulty=self._difficulty,
buggy_code=self._current_task["buggy_code"],
instructions=self._current_task["instructions"],
test_cases_description=self._current_task["test_cases_description"],
reward=0.0,
cumulative_reward=self._cumulative_reward,
best_reward=self._best_reward,
passed_tests=0,
total_tests=len(self._current_task["test_cases"]),
feedback="β Invalid action: fixed_code is empty. Submit valid Python code.",
done=done,
)
# ββ Grade the submission βββββββββββββββββββββββββββββββββββββββββββ
grader = GRADERS[self._difficulty]
if self._difficulty == "hard":
reward, passed, total, feedback, _ = grader(
action.fixed_code, self._current_task, action.explanation
)
else:
reward, passed, total, feedback, _ = grader(
action.fixed_code, self._current_task
)
self._current_reward = reward
self._cumulative_reward += reward
self._best_reward = max(self._best_reward, reward)
done = (reward == 1.0) or (self._step_count >= MAX_STEPS)
self._done = done
return DebugObservation(
task_id=self._current_task["task_id"],
difficulty=self._difficulty,
buggy_code=self._current_task["buggy_code"],
instructions=self._current_task["instructions"],
test_cases_description=self._current_task["test_cases_description"],
reward=reward,
cumulative_reward=self._cumulative_reward,
best_reward=self._best_reward,
passed_tests=passed,
total_tests=total,
feedback=feedback,
done=done,
)
@property
def state(self) -> DebugState:
"""Return current episode metadata."""
return DebugState(
episode_id=self._episode_id,
step_count=self._step_count,
task_id=self._current_task["task_id"] if self._current_task else "none",
difficulty=self._difficulty,
max_steps=MAX_STEPS,
current_reward=self._current_reward,
cumulative_reward=self._cumulative_reward,
best_reward=self._best_reward,
done=self._done,
)
|