"""Every task: the oracle artifact scores 1, no artifact scores 0, a corrupted one scores 0; the server mirrors the operator's tbench2_env contract; the sandbox limits hold.""" import json import os import tempfile import pytest os.environ.setdefault("TERMINAL_DESK_APP", tempfile.mkdtemp(prefix="td-test-")) from terminal_desk.core import shell # noqa: E402 from terminal_desk.core.grading import oracle_actions, render_artifact # noqa: E402 from terminal_desk.families import all_task_ids # noqa: E402 from terminal_desk.models import Tbench2Action # noqa: E402 from terminal_desk.server import environment # noqa: E402 from terminal_desk.server.environment import MAX_CALLS, TerminalDeskEnvironment # noqa: E402 TASKS = all_task_ids() FIXTURE = os.path.join(os.path.dirname(__file__), "fixtures", "tb2_schema.json") def _write_oracle(env: TerminalDeskEnvironment) -> str: art = env._ep["built"].artifact for act in oracle_actions(art.path, render_artifact(art)): obs = env.step(act) assert obs.success, obs.error return art.path def _evaluate(env): return env.step(Tbench2Action(action_type="evaluate")) def test_schema_matches_operator_fixture(): from fastapi.testclient import TestClient from terminal_desk.server.app import app ours = TestClient(app).get("/schema").json() want = json.load(open(FIXTURE)) for part in ("action", "observation"): assert json.dumps(ours[part], sort_keys=True) == json.dumps(want[part], sort_keys=True), part @pytest.mark.parametrize("task_id", TASKS) @pytest.mark.parametrize("seed", [1, 2, 3]) def test_oracle_scores_one(task_id, seed): env = TerminalDeskEnvironment(allow_seed=True) obs = env.reset(task_id=task_id, seed=seed) built = env._ep["built"] assert not obs.done and obs.action_type == "reset" and obs.output == "" and obs.reward == 0.0 assert obs.instruction == built.instruction and 0 < len(built.instruction) <= 700, len(built.instruction) assert built.files, "a task needs input files" assert "/app/" in built.instruction, "the instruction must name the output path under /app" _write_oracle(env) obs = _evaluate(env) assert obs.done and obs.reward == 1.0 and obs.info["tests_passed"], obs.output @pytest.mark.parametrize("task_id", TASKS) def test_evaluate_without_artifact_scores_zero(task_id): env = TerminalDeskEnvironment(allow_seed=True) env.reset(task_id=task_id, seed=7) obs = _evaluate(env) assert obs.done and obs.reward == 0.0 and "FAILED" in obs.output @pytest.mark.parametrize("task_id", TASKS) def test_corrupted_artifact_scores_zero(task_id): env = TerminalDeskEnvironment(allow_seed=True) env.reset(task_id=task_id, seed=11) art = env._ep["built"].artifact content = render_artifact(art) if isinstance(content, bytes): env.step(Tbench2Action(action_type="exec", command=f"head -c 44 /dev/zero > {art.path}")) else: env.step(Tbench2Action(action_type="write_file", file_path=art.path, content=content[: len(content) // 2] + "x")) obs = _evaluate(env) assert obs.done and obs.reward == 0.0 def test_step_echoes_instruction_and_none_reward(): env = TerminalDeskEnvironment(allow_seed=True) first = env.reset(task_id="sci_kinetics-L1", seed=1) obs = env.step(Tbench2Action(action_type="exec", command="ls")) assert obs.instruction == first.instruction and obs.reward is None and not obs.done assert obs.success and obs.error == "" and obs.action_type == "exec" and obs.session_id == "tb2-session" assert obs.task_path == environment.APP def test_session_actions_fail_softly_and_episode_continues(): env = TerminalDeskEnvironment(allow_seed=True) env.reset(task_id="sci_flood-L1", seed=1) obs = env.step(Tbench2Action(action_type="write", command="x")) assert not obs.success and "session_id is required" in obs.error and not obs.done obs = env.step(Tbench2Action(action_type="view", session_id="s1")) assert not obs.success and not obs.done obs = env.step(Tbench2Action(action_type="dance")) assert not obs.success and obs.error == "Unsupported action_type: dance" and not obs.done obs = env.step(Tbench2Action(action_type="write_file")) assert not obs.success and "file_path" in obs.error assert env.step(Tbench2Action(action_type="exec", command="echo still here")).output == "still here" def test_write_file_creates_parents_and_cannot_touch_system_paths(): env = TerminalDeskEnvironment(allow_seed=True) env.reset(task_id="sci_stoich-L1", seed=1) obs = env.step(Tbench2Action(action_type="write_file", file_path="out/a/b.txt", content="hi\n")) assert obs.success and obs.output == "Wrote content to out/a/b.txt" assert env.step(Tbench2Action(action_type="exec", command="cat out/a/b.txt")).output == "hi" obs = env.step(Tbench2Action(action_type="write_file", file_path="/etc/terminal-desk-test", content="x")) assert not obs.success and not os.path.exists("/etc/terminal-desk-test") def test_close_ends_without_reward(): env = TerminalDeskEnvironment(allow_seed=True) env.reset(task_id="sci_units-L1", seed=1) obs = env.step(Tbench2Action(action_type="close")) assert obs.done and obs.reward is None and obs.output == "Closed TB2 environment." def test_call_cap_auto_evaluates(): env = TerminalDeskEnvironment(allow_seed=True) env.reset(task_id="sci_stoich-L1", seed=1) _write_oracle(env) obs = None for _ in range(MAX_CALLS + 1): obs = env.step(Tbench2Action(action_type="exec", command="echo hi")) if obs.done: break assert obs.done and obs.reward == 1.0 and obs.info["calls"] == MAX_CALLS + 1 assert env.step(Tbench2Action(action_type="exec", command="echo again")).done def test_reset_builds_the_same_fixed_instance_per_task_id(): env = TerminalDeskEnvironment() a = env.reset(task_id="sci_kinetics-L1").instruction, dict(env._ep["built"].files) b = env.reset(task_id="sci_kinetics-L1", seed=5).instruction, dict(env._ep["built"].files) c = env.reset(task_id="sci_kinetics-L1-i1").instruction, dict(env._ep["built"].files) assert a == b, "a client seed must not change the fixed instance" assert a != c, "another instance suffix is another problem" def test_reset_wipes_the_workspace(): env = TerminalDeskEnvironment(allow_seed=True) env.reset(task_id="sci_quake-L1", seed=1) env.step(Tbench2Action(action_type="write_file", file_path="leftover.txt", content="x")) env.reset(task_id="sci_quake-L1", seed=1) assert env.step(Tbench2Action(action_type="exec", command="ls leftover.txt")).output != "leftover.txt" def test_shell_output_is_capped_and_timeouts_stop(): env = TerminalDeskEnvironment(allow_seed=True) env.reset(task_id="sci_units-L1", seed=1) obs = env.step(Tbench2Action(action_type="exec", command="yes | head -c 200000")) assert len(obs.output) < shell.MAX_OUTPUT_CHARS + 200 and "cut" in obs.output obs = env.step(Tbench2Action(action_type="exec", command="sleep 30")) assert "time limit" in obs.output and obs.success def test_verifier_refuses_symlink_and_fifo(): env = TerminalDeskEnvironment(allow_seed=True) env.reset(task_id="sci_stoich-L1", seed=2) art = env._ep["built"].artifact content = render_artifact(art) env.step(Tbench2Action(action_type="write_file", file_path="elsewhere.json", content=content)) env.step(Tbench2Action(action_type="exec", command=f"ln -s elsewhere.json {art.path}")) assert _evaluate(env).reward == 0.0 env.reset(task_id="sci_stoich-L1", seed=2) env.step(Tbench2Action(action_type="exec", command=f"mkfifo {art.path}")) assert _evaluate(env).reward == 0.0