File size: 2,369 Bytes
fd5366a | 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 | # 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")
|