AgentEscapeBench / tests /corpus_docker.py
edbeeching's picture
edbeeching HF Staff
Port 270 AgentEscapeBench tasks to Harbor with OpenEnv integration (part 13)
1049942 verified
Raw History Blame
3.34 kB
"""Run corpus and worker checks without ever executing puzzle code on the host."""
import json
import subprocess
import time
import urllib.request
import uuid
from pathlib import Path
import yaml
root = Path(__file__).resolve().parents[1]
task = next((root / "tasks").glob("agentescape-*/environment/docker-compose.yaml"))
compose = yaml.safe_load(task.read_text())
image = compose["services"]["worker"]["image"]
name = "aeb-port-validation-" + uuid.uuid4().hex[:10]
subprocess.run(
["docker", "compose", "-p", name, "-f", str(task), "build", "worker"], check=True
)
subprocess.run(
[
"docker",
"run",
"--rm",
"--detach",
"--name",
name,
"--read-only",
"--tmpfs",
"/tmp:size=64m,mode=1777",
"--pids-limit",
"64",
"--memory",
"384m",
"--security-opt",
"no-new-privileges:true",
"--cap-drop",
"ALL",
"--cap-add",
"SETUID",
"--cap-add",
"SETGID",
"--cap-add",
"KILL",
"--cap-add",
"CHOWN",
"--cap-add",
"DAC_OVERRIDE",
"-p",
"127.0.0.1::8001",
image,
],
check=True,
)
try:
url = (
"http://"
+ subprocess.check_output(
["docker", "port", name, "8001/tcp"], text=True
).strip()
)
for attempt in range(40):
try:
urllib.request.urlopen(url + "/health", timeout=2).close()
break
except OSError:
if attempt == 39:
raise
time.sleep(0.25)
results = []
checks = [
("normal Python", "print(7 * 6)", lambda s: s == "42\n"),
(
"no scenario files",
"print(open('/private/scenario.json').read())",
lambda s: "FileNotFoundError" in s,
),
(
"no server module",
"import server; print(server.SCENARIOS)",
lambda s: "ModuleNotFoundError" in s,
),
(
"no network sockets",
"import socket; socket.socket()",
lambda s: "PermissionError" in s,
),
(
"no parent environment",
"print(open('/proc/1/environ').read())",
lambda s: "PermissionError" in s,
),
(
"CPU limit",
"while True: pass",
lambda s: "resource limit" in s or "timed out" in s,
),
]
for label, code, check in checks:
req = urllib.request.Request(
url + "/execute",
data=json.dumps({"code": code}).encode(),
headers={"Content-Type": "application/json"},
)
with urllib.request.urlopen(req, timeout=15) as response:
result = json.load(response)
assert check(result["stdout"]), (label, result)
results.append({"check": label, "passed": True})
(root / "reports/worker-isolation.json").write_text(json.dumps(results, indent=2))
subprocess.run(
[
str(root / ".venv/bin/python"),
"-m",
"agentescape_port.validate",
"--worker-url",
url,
],
cwd=root,
check=True,
)
finally:
subprocess.run(["docker", "stop", name], check=True, capture_output=True)