File size: 46,083 Bytes
9abc0a5 | 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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 | # Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
"""TB2 environment server implementation (Spaces-compatible local mode)."""
from __future__ import annotations
import io
import logging
import os
import re
import shlex
import shutil
import tarfile
import threading
import sys
import urllib.request
import zipfile
from pathlib import Path
from typing import Any
from uuid import uuid4
if sys.version_info >= (3, 11):
import tomllib
else:
import tomli as tomllib
from openenv.core.env_server.interfaces import Environment
# Support both in-repo and standalone imports
try:
# In-repo imports (when running from OpenEnv repository)
from tbench2_env.models import Tbench2Action, Tbench2Observation, Tbench2State
except ImportError:
# Standalone imports (when environment is standalone with openenv from pip)
from models import Tbench2Action, Tbench2Observation, Tbench2State
_CAMEL_IMPORT_ERROR: Exception | None = None
# Official TB2 fixed paths: tests/test.sh assumes it runs from /tests and
# writes /logs/verifier/reward.txt. Module-level only so unit tests can
# redirect them off the real root filesystem.
_VERIFY_TESTS_DIR = "/tests"
_VERIFIER_LOG_DIR = "/logs/verifier"
def _hard_remove(path: str) -> None:
"""Remove whatever is at *path* β file, symlink, or directory tree.
shutil.rmtree refuses to remove a symlink and, with ignore_errors=True,
no-ops on one. That leaves an escape hatch: a root agent that symlinks a
fixed path (/tests, /logs/verifier) at a directory it controls would keep
that target alive across the "wipe", so a pre-planted conftest.py survives
into scoring. Unlink the symlink itself before falling back to rmtree.
"""
p = Path(path)
if p.is_symlink() or p.is_file():
p.unlink(missing_ok=True)
else:
shutil.rmtree(path, ignore_errors=True)
def _extractall(tar: tarfile.TarFile, dest: str) -> None:
"""extractall with the 3.12 data filter when available. The tarball is one
this server wrote from a trusted tests/ dir, so the filter is defence in
depth; on <3.12 (the ``filter`` kwarg predates it) fall back cleanly rather
than raising TypeError mid-verify (the package targets Python >=3.10)."""
try:
tar.extractall(dest, filter="data")
except TypeError:
tar.extractall(dest)
def _require_terminal_toolkit() -> Any:
global _CAMEL_IMPORT_ERROR
if _CAMEL_IMPORT_ERROR is not None:
raise RuntimeError(
"camel-ai (TerminalToolkit) is required for TB2. Install from PyPI or from the CAMEL repo."
) from _CAMEL_IMPORT_ERROR
try:
from camel.toolkits import TerminalToolkit
except Exception as exc: # pragma: no cover
_CAMEL_IMPORT_ERROR = exc
raise RuntimeError(
"camel-ai (TerminalToolkit) is required for TB2. Install from PyPI or from the CAMEL repo."
) from exc
return TerminalToolkit
def _download_tb2_repo(cache_dir: Path) -> Path:
repo_url = os.getenv(
"TB2_REPO_URL",
"https://github.com/laude-institute/terminal-bench-2/archive/refs/heads/main.zip",
)
cache_dir.mkdir(parents=True, exist_ok=True)
archive_path = cache_dir / "terminal-bench-2.zip"
if not archive_path.exists():
urllib.request.urlretrieve(repo_url, archive_path)
with zipfile.ZipFile(archive_path) as zf:
root = zf.namelist()[0].split("/")[0]
extract_dir = cache_dir / root
if not extract_dir.exists():
zf.extractall(cache_dir)
return extract_dir
def _read_instruction(task_dir: Path) -> str:
instruction_path = task_dir / "instruction.md"
if instruction_path.exists():
return instruction_path.read_text(encoding="utf-8")
return ""
def _task_image_workdir(task_dir: Path) -> str:
"""WORKDIR pinned by the task's own image (environment/Dockerfile).
Empty string when the task ships no Dockerfile or no WORKDIR; multi-stage
Dockerfiles resolve to the last WORKDIR (the final stage's).
"""
dockerfile = task_dir / "environment" / "Dockerfile"
workdir = ""
if dockerfile.is_file():
for line in dockerfile.read_text(encoding="utf-8").splitlines():
m = re.match(r"^\s*WORKDIR\s+(\S+)", line, re.IGNORECASE)
if m:
workdir = m.group(1)
return workdir
def _workdir_is_server_tree(workdir: str) -> bool:
"""True when *workdir* contains this server's own code.
A Dockerfile-parsed WORKDIR is only meaningful when the server runs
inside the task image (per-task sandboxes, where the server layer lives
elsewhere, e.g. /opt/envserver). The standard env-server image also
installs to /app β the most common task WORKDIR β so the path merely
existing is not enough: if the server's own code sits under it, it is
the server tree, not the task tree.
"""
try:
return Path(__file__).resolve().is_relative_to(Path(workdir).resolve())
except OSError:
return True # unresolvable path: fail toward the task-dir fallback
def _read_timeout(task_dir: Path, fallback: float) -> float:
task_toml = task_dir / "task.toml"
if not task_toml.exists():
return fallback
try:
data = tomllib.loads(task_toml.read_text(encoding="utf-8"))
except Exception:
return fallback
verifier = data.get("verifier", {})
return float(verifier.get("timeout_sec", fallback))
# The scoring exec echoes its verdict on a marker line so the caller can parse
# it out of mixed stdout. Shared by both execution modes (local terminal
# toolkit and Docker container exec).
_REWARD_MARKER = "__TB2_REWARD__:"
_EXIT_CODE_MARKER = "__TB2_EXIT_CODE__:"
def _canonical_eval_cmd(workdir: str, timeout_s: float | None = None) -> str:
"""The official-harness scoring command, run from the agent's workdir.
/tests/test.sh (staged by the caller) pins its own pytest toolchain (uvx:
Python 3.13 + pytest 8.4.1 + ctrf), runs tests/test_outputs.py from
_VERIFY_TESTS_DIR against the task workdir, and writes the binary result
to /logs/verifier/reward.txt. test.sh stdout goes under _VERIFIER_LOG_DIR
(wiped with the verify window) rather than a fixed /tmp path that would
outlive scoring: its pytest -rA output can spell out expected values. Its
tail is echoed back into the returned output β the episode is over by
then, and callers need the pytest diagnostics on failure; the reward
marker line stays last for parsing.
``timeout_s`` bounds test.sh with coreutils ``timeout`` when present in
the image. The local mode passes None: its terminal toolkit enforces the
budget itself. Docker exec has no server-side timeout, so the task's own
verifier budget is enforced in-shell.
"""
run = f"bash {_VERIFY_TESTS_DIR}/test.sh"
if timeout_s is not None:
run = f"if command -v timeout >/dev/null 2>&1; then timeout {int(timeout_s)} {run}; else {run}; fi"
return (
f"cd {shlex.quote(workdir)} && "
f"{run} > {_VERIFIER_LOG_DIR}/testsh.log 2>&1; "
f"tail -c 20000 {_VERIFIER_LOG_DIR}/testsh.log 2>/dev/null; "
f"echo {_REWARD_MARKER}$(cat {_VERIFIER_LOG_DIR}/reward.txt 2>/dev/null)"
)
def _parse_canonical_reward(output: str) -> float | None:
"""The verdict test.sh's verifier wrote to reward.txt, echoed on the
marker line. None when no verdict can be recovered β no marker line, an
empty value (reward.txt absent: test.sh crashed or was killed before its
verifier wrote one), or a non-numeric value. A missing verdict is a
scoring failure, not a task failure: callers raise so ``evaluate``
reports an error (observation.error set, reward None) instead of a 0.0
indistinguishable from tests genuinely failing β RL consumers must be
able to drop such episodes rather than train on a false negative.
"""
for line in output.splitlines()[::-1]:
if _REWARD_MARKER in line:
raw = line.split(_REWARD_MARKER, 1)[1].strip()
if not raw:
return None
try:
return float(raw)
except ValueError:
return None
return None
def _require_canonical_verdict(reward: float | None, output: str) -> float:
"""Raise when the canonical harness produced no verdict (see above)."""
if reward is None:
raise RuntimeError(
"canonical harness produced no verdict (reward.txt missing after "
f"tests/test.sh ran); test.sh log tail: {output[-800:]!r}"
)
return reward
def _fallback_eval_cmd(workdir: str) -> str:
"""pytest against the staged tests copy, for task dirs without the
canonical harness (none of the 89 official TB2 tasks β they all ship
test.sh β but custom task dirs may only have bare pytest tests). Prefer
uvx so pytest comes with its own toolchain like the canonical harness
does. Verify from the same directory the agent worked in.
"""
return (
f"cd {shlex.quote(workdir)} && "
"if command -v uvx >/dev/null 2>&1; "
f"then uvx --with pytest==8.4.1 pytest -q {_VERIFY_TESTS_DIR} -rA; "
f"else python -m pytest -q {_VERIFY_TESTS_DIR} -rA; fi; "
f"echo {_EXIT_CODE_MARKER}$?"
)
def _parse_exit_code_marker(output: str) -> int:
for line in output.splitlines()[::-1]:
if _EXIT_CODE_MARKER in line:
try:
return int(line.split(_EXIT_CODE_MARKER, 1)[1].strip())
except Exception:
return 1
return 1
class Tbench2Environment(Environment[Tbench2Action, Tbench2Observation, Tbench2State]):
"""OpenEnv wrapper around Terminal-Bench 2 tasks (local execution)."""
SUPPORTS_CONCURRENT_SESSIONS: bool = True
def __init__(
self,
tasks_dir: str | None = None,
output_dir: str | None = None,
command_timeout_s: float | None = None,
safe_mode: bool = False,
default_task_id: str | None = None,
withhold_tests: bool | None = None,
) -> None:
super().__init__()
self.tasks_dir = tasks_dir or os.getenv("TB2_TASKS_DIR", "")
# RL hygiene: the env server and the agent share one container
# filesystem, so tests/ and solution/ in the task dir are readable
# (and writable) by the agent. When enabled, reset() moves them out
# of reach β see _withhold_verifier_assets.
if withhold_tests is None:
withhold_tests = os.getenv("TB2_WITHHOLD_TESTS", "0") == "1"
self.withhold_tests = withhold_tests
self.output_dir = Path(
output_dir or os.getenv("TB2_OUTPUT_DIR", "/tmp/tbench2_env_runs")
)
# Overridable via TB2_COMMAND_TIMEOUT_S: create_app instantiates the
# environment with no arguments, and real TB2 agent commands / the
# canonical tests/test.sh routinely exceed the old 20s default.
if command_timeout_s is None:
command_timeout_s = float(os.getenv("TB2_COMMAND_TIMEOUT_S", "20.0"))
self.command_timeout_s = command_timeout_s
self.safe_mode = safe_mode
self.default_task_id = default_task_id or os.getenv(
"TB2_DEFAULT_TASK_ID", "headless-terminal"
)
self._state = Tbench2State()
self._task_dir: Path | None = None
self._terminal_toolkit = None
self._instruction = ""
self._workdir = ""
def reset(
self,
seed: int | None = None,
episode_id: str | None = None,
**kwargs: Any,
) -> Tbench2Observation:
del seed
TerminalToolkit = _require_terminal_toolkit()
task_id = (
kwargs.get("task_id") or kwargs.get("task_name") or self.default_task_id
)
task_path = kwargs.get("task_path") or kwargs.get("path")
task_dir = self._resolve_task_path(task_id, task_path)
resolved_task_id = task_id or task_dir.name
self._instruction = _read_instruction(task_dir)
self._task_dir = task_dir
if self.withhold_tests:
self._withhold_verifier_assets(task_dir)
trial_name = f"{resolved_task_id}.{episode_id or uuid4().hex}"
session_logs_dir = (
self.output_dir / trial_name / "terminal_toolkit_session_logs"
)
session_logs_dir.mkdir(parents=True, exist_ok=True)
# Commands run in the task image's real WORKDIR β where agents and
# the canonical harness expect to operate, and not always /app
# (fix-git: /app/personal-site, prove-plus-comm: /workspace).
# TB2_TASK_WORKDIR overrides; otherwise the task's own Dockerfile is
# parsed, trusted only when the path isn't the server's own install
# tree (see _workdir_is_server_tree); plain local mode falls back to
# the task source dir.
workdir = os.getenv("TB2_TASK_WORKDIR", "").strip()
if not workdir:
workdir = _task_image_workdir(task_dir)
if workdir and _workdir_is_server_tree(workdir):
workdir = ""
if not (workdir and Path(workdir).is_dir()):
workdir = str(task_dir)
self._workdir = workdir
# No install_dependencies: evaluation runs the task's canonical
# tests/test.sh, which pins its own pytest toolchain via uvx (see
# _evaluate_canonical), so the toolkit venv does not need pytest.
self._terminal_toolkit = TerminalToolkit(
timeout=self.command_timeout_s,
working_directory=workdir,
use_docker_backend=False,
session_logs_dir=session_logs_dir,
safe_mode=self.safe_mode,
)
self._state = Tbench2State(
episode_id=episode_id or str(uuid4()),
step_count=0,
task_id=resolved_task_id,
task_path=str(task_dir),
terminal_ready=True,
)
return Tbench2Observation(
instruction=self._instruction,
output="",
success=True,
error="",
task_id=resolved_task_id,
task_path=str(task_dir),
session_id=None,
action_type="reset",
info={},
reward=0.0,
done=False,
)
def step(
self,
action: Tbench2Action,
timeout_s: float | None = None,
**kwargs: Any,
) -> Tbench2Observation:
del timeout_s, kwargs
if not isinstance(action, Tbench2Action):
raise TypeError(f"Expected Tbench2Action, got {type(action)}")
if self._terminal_toolkit is None or self._task_dir is None:
raise RuntimeError("TB2 environment not initialized. Call reset() first.")
self._state.step_count += 1
self._state.last_action_type = action.action_type
self._state.last_command = action.command
output = ""
error = ""
success = True
reward = None
done = False
info: dict[str, Any] = {}
session_id = action.session_id or "tb2-session"
try:
if action.action_type == "exec":
# Pass the timeout explicitly: camel's shell_exec has its own
# per-call default (20s) and ignores the constructor timeout,
# which silently truncates any longer-running agent command.
output = self._terminal_toolkit.shell_exec(
command=action.command,
block=action.block,
id=session_id,
timeout=self.command_timeout_s,
)
elif action.action_type == "write":
self._ensure_session_id(action.session_id, action.action_type)
output = self._terminal_toolkit.shell_write_to_process(
id=action.session_id,
command=action.command,
)
elif action.action_type == "view":
self._ensure_session_id(action.session_id, action.action_type)
output = self._terminal_toolkit.shell_view(id=action.session_id)
elif action.action_type == "wait":
self._ensure_session_id(action.session_id, action.action_type)
wait_seconds = action.wait_seconds or 0.0
output = self._terminal_toolkit.shell_wait(
id=action.session_id,
wait_seconds=wait_seconds,
)
elif action.action_type == "kill":
self._ensure_session_id(action.session_id, action.action_type)
self._terminal_toolkit.shell_kill_process(id=action.session_id)
output = f"Killed session {action.session_id}"
elif action.action_type == "write_file":
self._terminal_toolkit.shell_write_content_to_file(
content=action.content,
file_path=action.file_path,
)
output = f"Wrote content to {action.file_path}"
elif action.action_type == "evaluate":
output, reward, info = self._evaluate_task()
done = True
elif action.action_type == "close":
self.close()
output = "Closed TB2 environment."
done = True
else:
raise ValueError(f"Unsupported action_type: {action.action_type}")
except Exception as exc: # pragma: no cover
success = False
error = str(exc)
self._state.last_output = output
self._state.session_id = session_id or ""
return Tbench2Observation(
instruction=self._instruction,
output=output,
success=success,
error=error,
task_id=self._state.task_id,
task_path=self._state.task_path,
session_id=session_id or "",
action_type=action.action_type,
info=info,
reward=reward,
done=done,
)
@property
def state(self) -> Tbench2State:
return self._state
def close(self) -> None:
self._terminal_toolkit = None
self._task_dir = None
self._instruction = ""
def _resolve_task_path(self, task_id: str | None, task_path: str | None) -> Path:
if task_path:
resolved = Path(task_path).expanduser().resolve()
if not resolved.exists():
raise FileNotFoundError(f"Task path not found: {resolved}")
return resolved
if not task_id:
raise ValueError("Provide task_id or task_path to reset TB2 environment.")
if not self.tasks_dir:
cache_dir = Path(
os.getenv("TB2_CACHE_DIR", str(self.output_dir / "repo_cache"))
)
repo_dir = _download_tb2_repo(cache_dir)
resolved = repo_dir / task_id
else:
resolved = Path(self.tasks_dir).expanduser().resolve() / task_id
if not resolved.exists():
raise FileNotFoundError(f"Task path not found: {resolved}")
return resolved
def _ensure_session_id(self, session_id: str | None, action_type: str) -> None:
if not session_id:
raise ValueError(f"session_id is required for action_type='{action_type}'")
# tests/ tarballs withheld out of agent reach at reset(), keyed by task
# dir. Class-level: the server may construct one environment instance per
# session, and a later reset of the same task must still find the copy
# after the on-disk source is gone. In-process only β withhold mode
# therefore assumes a single server process owns the task filesystem for
# its lifetime (the intended per-task-sandbox deployment: one uvicorn
# worker, one ephemeral sandbox per episode). It is not safe with
# ``uvicorn --workers N`` sharing one on-disk checkout, where a sibling
# worker would see tests/ already deleted with no cache to restore from.
_WITHHELD_TESTS: dict[str, bytes] = {}
_WITHHOLD_LOCK = threading.Lock()
def _withhold_verifier_assets(self, task_dir: Path) -> None:
"""Move verifier assets out of the agent's reach before it can act.
The task directory is agent-readable (same filesystem, root agent):
tests/test_outputs.py spells out the expected outputs and solution/ is
the literal answer β for RL training both are reward-hacking bait.
tests/ is read into server memory (the staging source for
_stage_tests_for_verify) and removed from disk; solution/ is simply
removed (nothing in this env reads it β it exists only for oracle
runs). Gated behind TB2_WITHHOLD_TESTS=1 because in plain local mode
task_dir may be a developer's working checkout, where deleting from
it would be hostile.
"""
key = str(task_dir)
with self._WITHHOLD_LOCK:
tests_dir = task_dir / "tests"
if tests_dir.is_dir():
if key not in self._WITHHELD_TESTS:
# realpath so a symlinked tests/ caches the target's files,
# not just a link member that would restore empty (tar.add
# does not follow a top-level symlink).
tests_real = os.path.realpath(tests_dir)
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w") as tar:
tar.add(tests_real, arcname=".")
self._WITHHELD_TESTS[key] = buf.getvalue()
_hard_remove(str(tests_dir))
_hard_remove(str(task_dir / "solution"))
def _stage_tests_for_verify(self) -> bool:
"""Stage a pristine tests/ copy at the official fixed path for exactly
the verify window. Caller must hold _CANONICAL_EVAL_LOCK.
Both fixed paths are recreated from scratch (symlink included β see
_hard_remove) so nothing an agent may have pre-planted survives into
scoring: a /tests/conftest.py pytest would auto-load, or a symlinked
/tests or /logs/verifier redirecting the stage at a dir the agent
controls. When the source dir was withheld at reset, the staged copy
comes from server memory: a copy the agent never had filesystem
access to.
"""
_hard_remove(_VERIFY_TESTS_DIR)
_hard_remove(_VERIFIER_LOG_DIR)
Path(_VERIFIER_LOG_DIR).mkdir(parents=True, exist_ok=True)
blob = self._WITHHELD_TESTS.get(str(self._task_dir))
if blob is not None:
Path(_VERIFY_TESTS_DIR).mkdir(parents=True, exist_ok=True)
with tarfile.open(fileobj=io.BytesIO(blob)) as tar:
_extractall(tar, _VERIFY_TESTS_DIR)
return True
tests_dir = Path(self._task_dir) / "tests"
if not tests_dir.is_dir():
return False
shutil.copytree(tests_dir, _VERIFY_TESTS_DIR, symlinks=True)
return True
def _evaluate_task(self) -> tuple[str, float, dict[str, Any]]:
if self._task_dir is None:
raise RuntimeError("TB2 environment not initialized. Call reset() first.")
if self._terminal_toolkit is None:
raise RuntimeError("Terminal toolkit not initialized.")
# The task's own verifier budget (task.toml [verifier].timeout_sec) β
# heavy tests legitimately run minutes (circuit-fibsqrt declares 3600s).
verifier_timeout_s = _read_timeout(self._task_dir, fallback=900.0)
with self._CANONICAL_EVAL_LOCK:
try:
# Staging happens inside the try: a stage that errors partway
# (I/O failure mid-extract) must be wiped like a completed
# one β step() reports scoring errors without ending the
# episode, so the still-live session must not find a partial
# tests/ copy left behind.
if not self._stage_tests_for_verify():
return (
f"no tests/ found for task {self._state.task_id}",
0.0,
{"tests_passed": False, "error": "missing tests"},
)
if (Path(_VERIFY_TESTS_DIR) / "test.sh").is_file():
return self._evaluate_canonical(verifier_timeout_s)
return self._evaluate_fallback(verifier_timeout_s)
finally:
# Verifier artifacts live on disk only for the verify window:
# don't leave them readable to a later episode / concurrent
# session sharing this filesystem β or to the agent itself if
# scoring times out / errors (done stays false, so it keeps
# its session). /logs/verifier holds reward.txt and the
# test.sh log, whose pytest -rA output can reveal expected
# values.
_hard_remove(_VERIFY_TESTS_DIR)
_hard_remove(_VERIFIER_LOG_DIR)
# The canonical harness uses the official fixed paths (/tests,
# /logs/verifier/reward.txt), which are per-container, not per-session.
# Serialize staging + evaluation so concurrent sessions on one server
# cannot overwrite each other's staged tests or read another session's
# reward.
_CANONICAL_EVAL_LOCK = threading.Lock()
def _evaluate_canonical(
self, timeout_s: float
) -> tuple[str, float, dict[str, Any]]:
"""Score via the task's OFFICIAL harness, exactly as the TB2 verifier does.
/tests/test.sh (staged by _stage_tests_for_verify) pins its own pytest
toolchain (uvx: Python 3.13 + pytest 8.4.1 + ctrf), runs
tests/test_outputs.py from /tests against the task workdir, and writes
the binary result to /logs/verifier/reward.txt. Bare ``pytest tests/``
(the fallback below) skips that toolchain pinning and mis-scores any
task whose tests need it, so the canonical harness is preferred
whenever the task ships it.
"""
# Same working dir the agent operated in (resolved during reset).
# The toolkit enforces the verifier budget itself, so the command
# carries no in-shell timeout (see _canonical_eval_cmd).
workdir = self._workdir or str(self._task_dir)
output = self._terminal_toolkit.shell_exec(
id="tb2-tests",
command=_canonical_eval_cmd(workdir),
block=True,
timeout=timeout_s,
)
reward = _require_canonical_verdict(_parse_canonical_reward(output), output)
info = {"tests_passed": reward == 1.0, "harness": "tests/test.sh"}
return output, reward, info
def _evaluate_fallback(self, timeout_s: float) -> tuple[str, float, dict[str, Any]]:
"""Scoring for task dirs without the canonical harness (see
_fallback_eval_cmd)."""
fallback_cwd = self._workdir or str(self._task_dir)
output = self._terminal_toolkit.shell_exec(
id="tb2-tests",
command=_fallback_eval_cmd(fallback_cwd),
block=True,
timeout=timeout_s,
)
exit_code = _parse_exit_code_marker(output)
reward = 1.0 if exit_code == 0 else 0.0
info = {"tests_passed": exit_code == 0, "exit_code": exit_code}
return output, reward, info
class Tbench2DockerEnvironment(
Environment[Tbench2Action, Tbench2Observation, Tbench2State]
):
"""OpenEnv wrapper around Terminal-Bench 2 tasks with Docker isolation.
This environment runs each task in its own Docker container built from
the task's official image (task.toml's [environment] docker_image β tasks
without one are rejected at reset; there is no host-execution fallback).
Agent commands run in the image's own WORKDIR, and ``evaluate`` scores
with the task's canonical tests/test.sh staged at the official fixed
paths for exactly the verify window β the same scoring contract as the
local mode, just delivered over container exec instead of the in-process
terminal toolkit.
Requires:
- Docker socket mounted (/var/run/docker.sock)
- Sufficient disk space for container images
"""
SUPPORTS_CONCURRENT_SESSIONS: bool = True
def __init__(
self,
tasks_dir: str | None = None,
output_dir: str | None = None,
command_timeout_s: float = 300.0,
safe_mode: bool = True,
default_task_id: str | None = None,
) -> None:
super().__init__()
self.tasks_dir = tasks_dir or os.getenv("TB2_TASKS_DIR", "")
self.output_dir = Path(
output_dir or os.getenv("TB2_OUTPUT_DIR", "/tmp/tbench2_env_runs")
)
self.command_timeout_s = command_timeout_s
self.safe_mode = safe_mode
self.default_task_id = default_task_id or os.getenv(
"TB2_DEFAULT_TASK_ID", "headless-terminal"
)
self._state = Tbench2State()
self._task_dir: Path | None = None
self._docker_client = None
self._container = None
self._instruction = ""
self._task_image = ""
self._task_config: dict[str, Any] = {}
self._workdir = ""
def _get_docker_client(self) -> Any:
"""Lazy initialization of Docker client."""
if self._docker_client is None:
try:
import docker
self._docker_client = docker.from_env()
except Exception as exc:
raise RuntimeError(
f"Docker client not available. Ensure Docker socket is mounted. Error: {exc}"
) from exc
return self._docker_client
def reset(
self,
seed: int | None = None,
episode_id: str | None = None,
**kwargs: Any,
) -> Tbench2Observation:
del seed
task_id = (
kwargs.get("task_id") or kwargs.get("task_name") or self.default_task_id
)
task_path = kwargs.get("task_path") or kwargs.get("path")
task_dir = self._resolve_task_path(task_id, task_path)
resolved_task_id = task_id or task_dir.name
# Read task configuration including Docker image
task_toml_path = task_dir / "task.toml"
if task_toml_path.exists():
task_config = tomllib.loads(task_toml_path.read_text(encoding="utf-8"))
task_image = task_config.get("environment", {}).get("docker_image", "")
else:
task_image = ""
task_config = {}
instruction = _read_instruction(task_dir)
# No host-execution fallback: silently running agent commands on the
# env-server host would be a containment hole (arbitrary agent shell
# outside any container) and a scoring-fidelity hole (no official
# image, no canonical harness) at once. Every official TB2 task
# declares its image; a task dir that doesn't is a bug to surface.
if not task_image:
# Fail closed: a rejected reset must not leave a previous
# container usable with metadata from this new task.
self.close()
raise RuntimeError(
f"task {resolved_task_id} declares no [environment] docker_image "
"in task.toml; Docker mode runs every episode in the task's "
"official container and does not fall back to executing on the "
"server host. Fix the task, or use TB2_MODE=local."
)
# Create trial directory for logs
trial_name = f"{resolved_task_id}.{episode_id or uuid4().hex}"
trial_dir = self.output_dir / trial_name
trial_dir.mkdir(parents=True, exist_ok=True)
# A successful reset replaces the previous episode. Commit the new
# metadata only after all task inputs have been validated, and leave
# the session closed if container startup fails.
self.close()
self._task_config = task_config
self._task_image = task_image
self._instruction = instruction
self._task_dir = task_dir
try:
self._start_container(task_dir, trial_dir)
except Exception:
self.close()
raise
return Tbench2Observation(
instruction=self._instruction,
output="",
success=True,
error="",
task_id=resolved_task_id,
task_path=str(task_dir),
session_id=None,
action_type="reset",
info={"docker_image": self._task_image},
reward=0.0,
done=False,
)
def _start_container(self, task_dir: Path, trial_dir: Path) -> None:
"""Start a Docker container for the task.
Uses file copying instead of bind mounts to support Docker-in-Docker
scenarios where the server runs inside a container. Bind mounts reference
host paths, which don't exist when the server is containerized.
"""
docker = self._get_docker_client()
try:
# Pull image if needed
try:
image = docker.images.get(self._task_image)
except Exception:
logging.info(f"Pulling image {self._task_image}...")
image = docker.images.pull(self._task_image)
if isinstance(image, list): # pull without a tag returns a list
image = image[0]
self._workdir = self._resolve_workdir(image, task_dir)
# Start container WITHOUT bind mounts (for DinD compatibility).
# working_dir=/task only guarantees the task-source copy target
# exists; every exec cd's into the resolved image WORKDIR.
self._container = docker.containers.run(
image=self._task_image,
command="sleep infinity",
detach=True,
network_mode="host",
working_dir="/task",
remove=False,
)
# Copy task files into container using tar archive
# This works in Docker-in-Docker because we read files from our
# filesystem and stream them to the container via the Docker API.
# Verifier assets stay out: the agent works in /task and must not
# be able to read solution/ (the literal answer) or tests/ (the
# expected outputs) β tests are staged only at verify time by
# _evaluate_docker, like the official TB2 harness does.
self._copy_dir_to_container(
task_dir, "/task", exclude_top=("solution", "tests")
)
self._state = Tbench2State(
episode_id=str(uuid4()),
step_count=0,
task_id=task_dir.name,
task_path=str(task_dir),
terminal_ready=True,
)
except Exception as exc:
raise RuntimeError(f"Failed to start container: {exc}") from exc
def _copy_dir_to_container(
self, src_dir: Path, dest_path: str, exclude_top: tuple[str, ...] = ()
) -> None:
"""Copy a directory into the container using tar archive.
This method streams files via the Docker API, avoiding bind mount
issues in Docker-in-Docker scenarios. Top-level entries named in
*exclude_top* are skipped entirely.
"""
if self._container is None:
raise RuntimeError("Container not started")
# Create tar archive in memory (recursive=False: rglob already yields
# every descendant, so per-entry recursion would duplicate members β
# and would re-add excluded subtrees through their parents).
tar_stream = io.BytesIO()
with tarfile.open(fileobj=tar_stream, mode="w") as tar:
for item in src_dir.rglob("*"):
rel = item.relative_to(src_dir)
if rel.parts and rel.parts[0] in exclude_top:
continue
tar.add(str(item), arcname=str(rel), recursive=False)
tar_stream.seek(0)
# Copy to container
self._container.put_archive(dest_path, tar_stream.getvalue())
def _resolve_workdir(self, image: Any, task_dir: Path) -> str:
"""The dir agent commands and scoring run in: the task image's own
WORKDIR β the real TB2 task state, not the /task source copy.
Image metadata is authoritative (it sees a WORKDIR inherited from a
base image, which Dockerfile parsing misses); fall back to parsing
the task's Dockerfile, then to /task. The local mode's server-tree
guard does not apply here: this server sits outside the container,
so the image's /app is always the task tree.
"""
try:
workdir = (image.attrs.get("Config") or {}).get("WorkingDir") or ""
except Exception:
workdir = ""
return workdir or _task_image_workdir(task_dir) or "/task"
def _exec_in_container(
self, command: str, workdir: str | None = None
) -> tuple[int, str]:
"""Execute a command inside the container, from the task workdir.
The command rides as an argv element (bash -c <command>), not spliced
into a quoted shell string β an agent command containing a single
quote must arrive in the container byte-identical.
"""
if self._container is None:
raise RuntimeError("Container not started. Call reset() first.")
cwd = workdir or self._workdir or "/task"
exit_code, output = self._container.exec_run(
cmd=["bash", "-c", f"cd {shlex.quote(cwd)} && {command}"],
stdout=True,
stderr=True,
)
return exit_code, output.decode("utf-8", errors="replace")
def step(
self,
action: Tbench2Action,
timeout_s: float | None = None,
**kwargs: Any,
) -> Tbench2Observation:
del timeout_s, kwargs
if not isinstance(action, Tbench2Action):
raise TypeError(f"Expected Tbench2Action, got {type(action)}")
if self._task_dir is None:
raise RuntimeError("TB2 environment not initialized. Call reset() first.")
self._state.step_count += 1
self._state.last_action_type = action.action_type
self._state.last_command = action.command
output = ""
error = ""
success = True
reward = None
done = False
info: dict[str, Any] = {}
session_id = action.session_id or "tb2-session"
try:
if action.action_type == "exec":
exit_code, output = self._exec_in_container(action.command)
success = exit_code == 0
elif action.action_type == "write_file":
exit_code, _ = self._exec_in_container(
f"cat > {action.file_path} << 'EOF'\n{action.content}\nEOF"
)
success = exit_code == 0
output = f"Wrote to {action.file_path}"
elif action.action_type == "evaluate":
output, reward, info = self._evaluate_docker()
done = True
elif action.action_type == "close":
self.close()
output = "Closed TB2 environment."
done = True
else:
raise ValueError(
f"Unsupported action_type in Docker mode: {action.action_type}"
)
except Exception as exc:
success = False
error = str(exc)
self._state.last_output = output
self._state.session_id = session_id or ""
return Tbench2Observation(
instruction=self._instruction,
output=output,
success=success,
error=error,
task_id=self._state.task_id,
task_path=self._state.task_path,
session_id=session_id or "",
action_type=action.action_type,
info=info,
reward=reward,
done=done,
)
def _evaluate_docker(self) -> tuple[str, float, dict[str, Any]]:
"""Score with the canonical harness inside the task container.
Same scoring contract as the local mode's _evaluate_task: stage a
pristine tests/ copy at the official fixed path for exactly the
verify window, run the task's tests/test.sh from the dir the agent
worked in (the image WORKDIR), read the binary verdict from
/logs/verifier/reward.txt; task dirs without test.sh fall back to
pytest. No cross-session lock is needed here, unlike local mode:
every session drives its OWN container, so the fixed paths are
session-private.
"""
if self._container is None:
raise RuntimeError("Container not started.")
assert self._task_dir is not None, "Task directory not set"
# Stage-at-verify, like the official TB2 harness: the initial /task
# copy excludes tests/, and this server sits OUTSIDE the container, so
# the copy staged here is one the agent never saw. rm -rf first so
# nothing an agent pre-planted at the fixed paths survives into
# scoring β a /tests/conftest.py pytest would auto-load, a symlink
# redirecting the stage at a dir the agent controls, or a stale
# /logs/verifier/reward.txt read back as a verdict.
tests_src = self._task_dir / "tests"
if not tests_src.is_dir():
return (
f"no tests/ found for task {self._state.task_id}",
0.0,
{"tests_passed": False, "error": "missing tests"},
)
# The task's own verifier budget (task.toml [verifier].timeout_sec) β
# heavy tests legitimately run minutes (circuit-fibsqrt declares 3600s).
verifier_timeout_s = _read_timeout(self._task_dir, fallback=900.0)
workdir = self._workdir or "/task"
wipe_ec, wipe_out = self._exec_in_container(
f"rm -rf {_VERIFY_TESTS_DIR} {_VERIFIER_LOG_DIR} && "
f"mkdir -p {_VERIFY_TESTS_DIR} {_VERIFIER_LOG_DIR}"
)
if wipe_ec != 0:
# Fail closed: put_archive into a dir that survived the wipe would
# merge the staged tests into whatever the agent planted there.
raise RuntimeError(
f"could not reset {_VERIFY_TESTS_DIR} before verify: {wipe_out.strip()}"
)
try:
self._copy_dir_to_container(tests_src, _VERIFY_TESTS_DIR)
if (tests_src / "test.sh").is_file():
_, output = self._exec_in_container(
_canonical_eval_cmd(workdir, timeout_s=verifier_timeout_s)
)
reward = _require_canonical_verdict(
_parse_canonical_reward(output), output
)
info = {"tests_passed": reward == 1.0, "harness": "tests/test.sh"}
else:
_, output = self._exec_in_container(_fallback_eval_cmd(workdir))
exit_code = _parse_exit_code_marker(output)
reward = 1.0 if exit_code == 0 else 0.0
info = {"tests_passed": exit_code == 0, "exit_code": exit_code}
finally:
# Verifier artifacts live in the container only for the verify
# window, matching the local-mode staging: neither the staged
# tests (expected values) nor /logs/verifier (reward + pytest -rA
# log) may stay readable afterward β the agent keeps its session
# if scoring errored (step() reports the failure without ending
# the episode).
try:
rm_ec, rm_out = self._exec_in_container(
f"rm -rf {_VERIFY_TESTS_DIR} {_VERIFIER_LOG_DIR}"
)
if rm_ec != 0:
logging.warning(
"failed to remove staged %s after verify: %s",
_VERIFY_TESTS_DIR,
rm_out.strip(),
)
except Exception:
logging.warning(
"failed to remove staged %s after verify",
_VERIFY_TESTS_DIR,
exc_info=True,
)
return output, reward, info
@property
def state(self) -> Tbench2State:
return self._state
def close(self) -> None:
if self._container:
try:
self._container.stop(timeout=10)
self._container.remove(force=True)
except Exception:
pass
self._container = None
self._task_dir = None
self._instruction = ""
self._workdir = ""
def _resolve_task_path(self, task_id: str | None, task_path: str | None) -> Path:
if task_path:
resolved = Path(task_path).expanduser().resolve()
if not resolved.exists():
raise FileNotFoundError(f"Task path not found: {resolved}")
return resolved
if not task_id:
raise ValueError("Provide task_id or task_path to reset TB2 environment.")
if not self.tasks_dir:
cache_dir = Path(
os.getenv("TB2_CACHE_DIR", str(self.output_dir / "repo_cache"))
)
repo_dir = _download_tb2_repo(cache_dir)
resolved = repo_dir / task_id
else:
resolved = Path(self.tasks_dir).expanduser().resolve() / task_id
if not resolved.exists():
raise FileNotFoundError(f"Task path not found: {resolved}")
return resolved
|