clsa-code / tests /test_env.py
fahadhafeezofficial's picture
Upload folder using huggingface_hub
fd5366a verified
Raw History Blame Contribute Delete
2.37 kB
# Copyright (c) 2026 CLSA contributors
# SPDX-License-Identifier: MIT
"""Sandbox semantics: determinism, forks, reversibility, task coverage."""
from __future__ import annotations
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from clsa.envs import tasks # noqa: E402
from clsa.envs.sandbox import SandboxEnv # noqa: E402
def _spec(family=None, seed=0):
fams = (family,) if family else None
return tasks.make_tasks(seed=seed, n_per_family=2, families=fams)[0]
def test_task_bank_covers_all_families():
assert len(tasks.FAMILIES) == 4
for fam in tasks.FAMILIES:
assert tasks.make_tasks(seed=0, n_per_family=1, families=(fam,))
def test_same_seed_same_state():
spec = _spec()
a, b = SandboxEnv(spec, seed=5), SandboxEnv(spec, seed=5)
a.reset()
b.reset()
assert a.snapshot() == b.snapshot()
def test_candidates_are_wellformed_and_fork_is_deterministic():
env = SandboxEnv(_spec(), seed=1)
env.reset()
cands = env.candidate_actions()
assert cands
first = cands[0]
assert {"name", "args"} <= set(first)
before = env.snapshot()
f1 = env.fork(first)
f2 = env.fork(first)
assert f1["state_digest"] == f2["state_digest"], "fork is not deterministic"
assert env.snapshot() == before, "fork mutated the parent state"
assert {"success", "harm", "irreversible_harm", "effects"} <= set(f1)
def test_irreversible_tools_are_known_to_the_harm_model():
from clsa.envs.sandbox import tool_risk
risks = {name: tool_risk(name) for name in ("delete_account", "pay", "read_email", "write_file")}
assert risks["delete_account"] == "irreversible"
assert risks["pay"] == "irreversible"
assert risks["read_email"] == "read"
assert risks["write_file"] == "reversible"
def test_undo_restores_a_reversible_action():
for fam in tasks.FAMILIES:
env = SandboxEnv(_spec(fam), seed=2)
env.reset()
safe = [c for c in env.candidate_actions() if c["name"] in ("read_email", "read_file", "read_screen", "get_price", "search_products")]
if not safe:
continue
before = env.snapshot()
env.apply(safe[0])
env.restore(before)
assert env.snapshot() == before
return
raise AssertionError("no reversible action found to test undo")