Download source/codedesk/environment.py from rca-umb/five-desks-code: direct link, hf CLI and curl.
- Browser
- Download file 4.66 kB
-
https://huggingface.co/datasets/rca-umb/five-desks-code/resolve/main/source/codedesk/environment.py
- Command line
-
hf download hf://datasets/rca-umb/five-desks-code/source/codedesk/environment.py
-
curl -L -o environment.py https://huggingface.co/datasets/rca-umb/five-desks-code/resolve/main/source/codedesk/environment.py
4.66 kB
| """The Five Desks Code OpenEnv environment: agentic coding on real libraries from every arena domain.""" | |
| import random | |
| import secrets | |
| from typing import Any, Optional | |
| from uuid import uuid4 | |
| from openenv.core.env_server.interfaces import Environment | |
| from openenv.core.env_server.types import State | |
| from .families import FAMILIES | |
| from .interface import Interface | |
| from .models import ToolAction, ToolObservation | |
| MAX_OUTPUT_CHARS = 6000 | |
| def task_ids() -> list[str]: | |
| return [f"{fam}-{group}-l{level}" for fam, cls in FAMILIES.items() for group in cls.groups() for level in cls.levels] | |
| def build(task_id: str, seed: int): | |
| fam, _, rest = task_id.partition("-") | |
| group, _, level = rest.rpartition("-l") | |
| cls = FAMILIES.get(fam) | |
| if cls is None or group not in cls.groups() or level not in {str(lv) for lv in cls.levels}: | |
| raise ValueError(f"unknown task_id {task_id!r}; known ids: {', '.join(task_ids())}") | |
| return cls(random.Random(f"{task_id}:{seed}"), int(level), group) | |
| class CodeEnvironment(Environment): | |
| SUPPORTS_CONCURRENT_SESSIONS = True | |
| def __init__(self): | |
| self._state = State(episode_id=str(uuid4()), step_count=0) | |
| self._task = None | |
| self._tools = {} | |
| self._interface = None | |
| self._steps_left = 0 | |
| self._done = True | |
| def _close_task(self): | |
| if self._task is not None: | |
| self._task.close() | |
| def reset(self, seed: Optional[int] = None, episode_id: Optional[str] = None, task_id: Optional[str] = None, | |
| canonical_names: bool = False, **kwargs: Any) -> ToolObservation: | |
| task_id = task_id or task_ids()[0] | |
| seed = seed if seed is not None else secrets.randbits(48) | |
| self._close_task() | |
| self._task = build(task_id, seed) | |
| tools = self._task.tools() | |
| self._interface = None if canonical_names else Interface(tools, random.Random(f"names:{task_id}:{seed}")) | |
| listed = self._interface.tools if self._interface else tools | |
| self._tools = {t.name: t for t in listed} | |
| self._state = State(episode_id=episode_id or str(uuid4()), step_count=0) | |
| self._steps_left = self._task.max_steps | |
| self._done = False | |
| brief = self._interface.text(self._task.brief()) if self._interface else self._task.brief() | |
| lines = [f"- {t.name} {t.signature()}: {t.help}" for t in listed] | |
| text = (f"{brief}\n\nReply with exactly one JSON object per turn: {{\"tool\": \"<name>\", \"args\": {{...}}}}. " | |
| "Tools:\n" + "\n".join(lines) + f"\nYou have {self._task.max_steps} tool calls.") | |
| return ToolObservation(output=text, steps_left=self._steps_left, done=False, reward=0.0) | |
| def _end(self, output: str, reward: float) -> ToolObservation: | |
| self._done = True | |
| self._close_task() | |
| return ToolObservation(output=output, steps_left=self._steps_left, done=True, | |
| reward=round(min(1.0, max(0.0, reward)), 4)) | |
| def step(self, action: ToolAction, timeout_s: Optional[float] = None, **kwargs: Any) -> ToolObservation: # type: ignore[override] | |
| if self._task is None or self._done: | |
| return ToolObservation(output="Episode is over. Call reset.", steps_left=0, done=True, reward=0.0) | |
| self._state.step_count += 1 | |
| self._steps_left -= 1 | |
| name = action.tool.strip() | |
| tool = self._tools.get(name) | |
| if tool is not None and tool.final: # submit (also the arena's finish action) grades the work so far | |
| return self._end("Submitted.", float(tool.fn(dict(action.args)))) | |
| if tool is None and name == "submit": | |
| return self._end("Submitted.", float(self._task.grade({}))) | |
| if tool is None: | |
| output = f"Unknown tool {name!r}. Tools: {', '.join(self._tools)}." | |
| elif (error := tool.check(action.args)) is not None: | |
| output = error | |
| else: | |
| try: | |
| output = tool.fn(action.args) | |
| except Exception as exc: # a tool bug must not kill the episode | |
| output = f"Tool error: {type(exc).__name__}: {exc}" | |
| if self._interface: | |
| output = self._interface.text(output) | |
| if len(output) > MAX_OUTPUT_CHARS: | |
| output = output[:MAX_OUTPUT_CHARS] + "\n... [output truncated]" | |
| if self._steps_left <= 0: | |
| return self._end(output + "\nNo tool calls left; the work so far was submitted.", float(self._task.grade({}))) | |
| return ToolObservation(output=output, steps_left=self._steps_left, done=False, reward=0.0) | |
| def state(self) -> State: | |
| return self._state | |