Skip to content

sandbox

agentic_ml.core.sandbox

Run untrusted, agent-generated code out-of-process.

This module is deliberately ML-agnostic: it only knows how to launch a Python subprocess with a timeout, a clean-ish environment and captured output. The evaluation logic lives in :mod:agentic_ml.core.evaluation, which is what gets executed here.

ExecResult dataclass

ExecResult(
    returncode: int | None,
    stdout: str,
    stderr: str,
    timed_out: bool,
    duration: float,
)

Outcome of a subprocess run.

run

run(
    args: list[str],
    cwd: str | Path,
    timeout: float,
    env: dict[str, str] | None = None,
) -> ExecResult

Run args in cwd with a hard timeout (seconds).

Source code in src/agentic_ml/core/sandbox.py
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
def run(
    args: list[str],
    cwd: str | Path,
    timeout: float,
    env: dict[str, str] | None = None,
) -> ExecResult:
    """Run ``args`` in ``cwd`` with a hard ``timeout`` (seconds)."""
    start = time.perf_counter()
    try:
        proc = subprocess.run(
            args,
            cwd=str(cwd),
            capture_output=True,
            text=True,
            timeout=timeout,
            env=_sanitized_env(env),
            check=False,
        )
    except subprocess.TimeoutExpired as exc:
        return ExecResult(
            returncode=None,
            stdout=exc.stdout or "" if isinstance(exc.stdout, str) else "",
            stderr=exc.stderr or "" if isinstance(exc.stderr, str) else "",
            timed_out=True,
            duration=time.perf_counter() - start,
        )
    return ExecResult(
        returncode=proc.returncode,
        stdout=proc.stdout,
        stderr=proc.stderr,
        timed_out=False,
        duration=time.perf_counter() - start,
    )