Files
aji/tools/eval/ajieval/sandbox.py
T
dev b49003bdb9 E0-3: language-neutral agent scaffold and run recorder
tools/eval (stdlib Python): one agent loop, system prompt and tool set
(list/read/write/edit files, run, finish) for every language; language
profiles for aji_air, rust, zig carry only toolchain invocation, docs
location and command classification; bubblewrap sandbox without
network, read-only toolchain and /docs; providers: Anthropic Messages,
OpenAI-compatible (incl. local servers), scripted; run record per
protocol §28 (hashes, versions, limits, hardware, seed, all events,
patches, commands, final repository, first-generation snapshot,
evaluator verdicts, cost); metrics §14-§17; resumable matrix plans;
report (VSR, repair cycles, context, cost per correct solution);
classify (§27 categories, caught-by, smoke-test causes).

docs/evaluation/harness.md (task format for the private benchmark
repository, evaluator contract, record schema); dev-tasks/dev-echo
(harness development task, no benchmark family); smoke plan shape for
the owner's E-S choice (binary/stream parser, bounded pipeline);
self-tests in CI
2026-10-04 17:17:43 +03:00

74 lines
3.2 KiB
Python

"""Command execution for the agent's shell tool.
`bwrap` (bubblewrap): no network (protocol §4: no public internet), a
read-only system, the toolchain bound read-only, the workspace at /work
read-write, local documentation at /docs read-only, a fresh /tmp and HOME,
and a clean environment. `none` runs on the host and is for harness
development only; it is recorded in the run record.
"""
import os
import shutil
import subprocess
SYSTEM_RO = ["/usr", "/etc"]
LINKS = [("usr/lib", "/lib"), ("usr/lib64", "/lib64"), ("usr/bin", "/bin"), ("usr/sbin", "/sbin")]
class Sandbox:
def __init__(self, kind, workspace, toolchain_binds, path_dirs, docs=None, env=None):
if kind == "bwrap" and shutil.which("bwrap") is None:
raise SystemExit("sandbox bwrap requested but bubblewrap is not installed")
self.kind = kind
self.workspace = os.path.realpath(workspace)
self.binds = toolchain_binds
self.path = ":".join(path_dirs + ["/usr/bin", "/bin"])
self.docs = os.path.realpath(docs) if docs else None
self.env = dict(env or {})
def describe(self):
return {"kind": self.kind, "network": self.kind != "bwrap", "toolchain_binds": self.binds, "docs": self.docs}
def argv(self, command):
if self.kind == "none":
return ["sh", "-c", command]
a = ["bwrap", "--unshare-all", "--die-with-parent", "--new-session"]
for d in SYSTEM_RO:
if os.path.exists(d):
a += ["--ro-bind", d, d]
for src, dst in LINKS:
if os.path.exists("/" + src):
a += ["--symlink", src, dst]
a += ["--proc", "/proc", "--dev", "/dev", "--tmpfs", "/tmp"]
for d in self.binds:
a += ["--ro-bind", d, d]
if self.docs:
target = "/docs" if os.path.isdir(self.docs) else "/docs/" + os.path.basename(self.docs)
a += ["--ro-bind", self.docs, target]
a += ["--bind", self.workspace, "/work", "--chdir", "/work", "--clearenv"]
env = {"PATH": self.path, "HOME": "/tmp", "TMPDIR": "/tmp", "LANG": "C.UTF-8"}
env.update(self.env)
for k, v in sorted(env.items()):
a += ["--setenv", k, v]
return a + ["sh", "-c", command]
def run(self, command, timeout, output_limit):
"""Runs `command`; returns (exit code or None on timeout, output, truncated)."""
env = None
cwd = self.workspace
if self.kind == "none":
env = {"PATH": self.path + ":" + os.environ.get("PATH", ""), "HOME": os.environ.get("HOME", "/tmp"), "LANG": "C.UTF-8"}
env.update(self.env)
try:
p = subprocess.run(self.argv(command), cwd=cwd, env=env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, timeout=timeout)
code = p.returncode
out = p.stdout
except subprocess.TimeoutExpired as e:
code, out = None, (e.stdout or b"")
text = out.decode("utf-8", errors="replace")
truncated = len(text) > output_limit
if truncated:
half = output_limit // 2
text = text[:half] + f"\n... [{len(text) - output_limit} characters omitted] ...\n" + text[-half:]
return code, text, truncated