five-desks-control / source /codedesk /tests /test_codedesk.py
rca-umb's picture
Five Desks v5 collapse test (positive control)
ab59633 verified
Raw History Blame Contribute Delete
5.19 kB
"""Checks for Five Desks Code. Run inside the image (the libraries' tests run here):
docker run --rm five-desks-code:test python -m pytest -q /app/codedesk/tests
"""
import os
import pytest
from codedesk import runner
from codedesk.environment import CodeEnvironment, build, task_ids
from codedesk.models import ToolAction
SEEDS = range(3)
@pytest.mark.parametrize("task_id", task_ids())
def test_oracle_scores_one_and_untouched_scores_zero(task_id):
for seed in SEEDS:
task = build(task_id, seed)
try:
assert task.grade({}) == 0.0, (task_id, seed, "untouched workspace must fail")
task.oracle()
assert task.grade({}) == 1.0, (task_id, seed, "oracle must pass")
finally:
task.close()
def test_bugfix_ignores_test_edits_and_config():
ids = [t for t in task_ids() if t.startswith("bugfix-") and t.endswith("-l1")]
for task_id in ids[:4]:
task = build(task_id, 1)
try:
for key in task.killed: # gut the test files and add a conftest that skips everything
path = task.ws / key.split("::")[0]
if path.exists():
path.write_text("def test_nothing():\n pass\n")
(task.ws / "conftest.py").write_text("import pytest\ncollect_ignore_glob = ['*']\n")
assert task.grade({}) == 0.0, task_id
finally:
task.close()
def test_bugfix_package_symlink_out_scores_zero():
task_id = next(t for t in task_ids() if t.startswith("bugfix-") and t.endswith("-l1"))
task = build(task_id, 2)
try:
pkg = task.ws / task.pkg
target = pkg / "evil_link" if pkg.is_dir() else task.ws / "evil_link"
os.symlink("/etc/passwd", target)
task.oracle()
if pkg.is_dir():
assert task.grade({}) == 0.0
finally:
task.close()
def test_level2_removes_the_catching_tests():
for task_id in [t for t in task_ids() if t.startswith("bugfix-") and t.endswith("-l2")]:
task = build(task_id, 0)
try:
for key in task.killed:
path = task.ws / key.split("::")[0]
name = key.split("::")[-1].split("[")[0]
if path.exists():
assert f"def {name}(" not in path.read_text(), (task_id, key)
assert "pytest '" not in (task.ws / "ISSUE.md").read_text()
finally:
task.close()
def test_policy_commands_cannot_read_the_server():
if not runner.AS_ROOT:
pytest.skip("only meaningful in the root container")
task = build(next(t for t in task_ids() if t.startswith("bugfix-")), 0)
try:
code, out = runner.run("id -u; cat /app/codedesk/data/catalog.json | head -c 50", task.ws)
assert out.splitlines()[0] == "65534"
assert "Permission denied" in out
finally:
task.close()
def test_episode_flow_with_random_names_and_finish():
env = CodeEnvironment()
for task_id in task_ids()[:6]:
obs = env.reset(task_id=task_id, seed=4)
assert "Tools:" in obs.output and not obs.done
obs = env.step(ToolAction(tool="submit", args={}))
assert obs.done and obs.reward == 0.0, task_id
obs = env.reset(task_id=task_ids()[0], seed=5)
names = [line.split()[1] for line in obs.output.split("Tools:")[1].splitlines() if line.startswith("- ")]
listing = next(n for n in names if n in ("list_files", "ls", "tree", "list_dir"))
out = env.step(ToolAction(tool=listing, args={})).output
assert "ISSUE.md" in out
unknown = env.step(ToolAction(tool="no_such_tool", args={}))
assert "Unknown tool" in unknown.output and not unknown.done
def test_hostile_tool_calls_do_not_escape_or_crash():
env = CodeEnvironment()
env.reset(task_id=next(t for t in task_ids() if t.startswith("bugfix-")), seed=7, canonical_names=True)
calls = [
("read_file", {"path": "../../etc/passwd"}), ("read_file", {"path": "/app/codedesk/data/catalog.json"}),
("write_file", {"path": "../escape.txt", "content": "x"}), ("write_file", {"path": "/tmp/x", "content": "x"}),
("edit_file", {"path": "/etc/hostname", "old": "a", "new": "b"}), ("list_files", {"path": "/"}),
("search", {"pattern": "(((", "path": "."}), ("search", {"pattern": "x", "path": "/app"}),
("run", {"command": "yes | head -c 10000000"}), ("run", {"command": "sleep 100"}),
("run", {"command": ":(){ :|:& };:"}), ("run", {"command": "cat /app/codedesk/data/catalog.json"}),
("run", {"command": "python -c 'import os; os.kill(1, 9)'"}), ("read_file", {"path": "ISSUE.md", "start": -5, "end": 10**9}),
("run_tests", {"target": "/etc"}), ("run_tests", {"target": "; rm -rf /"}),
]
for tool, args in calls:
obs = env.step(ToolAction(tool=tool, args=args))
assert not obs.done, (tool, args)
assert len(obs.output) <= 7000
assert '"cases"' not in obs.output and '"killed"' not in obs.output, (tool, args, obs.output[:200])
assert not (env._task.ws.parent / "escape.txt").exists()
obs = env.step(ToolAction(tool="submit", args={}))
assert obs.done and obs.reward == 0.0