ajinkyamulay's picture
Add v13 shell environment
10f3201 verified
Raw History Blame Contribute Delete
2.32 kB
"""Analyst Workbench v13: the OpenEnv tbench2 shell environment over our own tasks.
The model works in a shell (exec, write_file) and ends with evaluate, which runs the
task's hidden pytest file and scores 1.0 only when every test passes. Tests and the
reference solution are withheld from the agent at reset (TB2_WITHHOLD_TESTS=1).
Changes to tbench2:
- each reset copies the task's pristine input files into a fresh working directory, so
episodes that share one server never see each other's edits;
- the shell's Python environment (.initial_env) is made ahead of time over the system
Python, so the model has numpy and pandas and no episode spends time building one;
- only the reset observation carries the instruction. Later observations leave it empty,
because every observation after the first counts against the completion budget.
"""
import os
import shutil
import venv
from pathlib import Path
from openenv.core.env_server.http_server import create_app
from tbench2_env.models import Tbench2Action, Tbench2Observation
from tbench2_env.server.tbench2_env_environment import Tbench2Environment
TASKS = Path(os.environ.get("TB2_TASKS_DIR", "/opt/tasks"))
WORK = Path("/work")
class WorkbenchEnvironment(Tbench2Environment):
def reset(self, seed=None, episode_id=None, **kwargs):
task_id = kwargs.get("task_id") or kwargs.get("task_name") or self.default_task_id
data = TASKS / task_id / "data"
work = WORK / task_id
if work.exists() or work.is_symlink():
shutil.rmtree(work, ignore_errors=True)
if work.is_symlink() or work.exists():
work.unlink()
shutil.copytree(data, work) if data.is_dir() else work.mkdir(parents=True)
venv.create(work / ".initial_env", system_site_packages=True, with_pip=False, symlinks=True)
os.environ["TB2_TASK_WORKDIR"] = str(work)
return super().reset(seed=seed, episode_id=episode_id, **kwargs)
def step(self, action, timeout_s=None, **kwargs):
obs = super().step(action, timeout_s=timeout_s, **kwargs)
obs.instruction = ""
return obs
app = create_app(
WorkbenchEnvironment,
Tbench2Action,
Tbench2Observation,
env_name="analyst_workbench_v13",
max_concurrent_envs=int(os.getenv("MAX_CONCURRENT_ENVS", "1")),
)