mirror of
https://github.com/yashshah9/agentbox.git
synced 2026-11-18 18:03:44 +00:00
feat(v0.7): Docker sandbox backend for real isolation
Context: - Subprocess + netns probes are not enough for production agent code execution. - Ephemeral docker run --rm with --network=none and optional --memory is the essential isolation path for agentbox-backed products like agentforge. Test Plan: - docker compose run --rm test - ruff check src tests - pytest tests/test_docker.py (mocks CLI; live skip without docker) Additional Notes: - Default remains subprocess for local/CI unit tests. - Set AGENTBOX_SANDBOX_BACKEND=docker (plus Docker CLI/daemon) for production.
This commit is contained in:
@@ -3,3 +3,6 @@ AGENTBOX_PORT=8080
|
||||
AGENTBOX_LOG_LEVEL=INFO
|
||||
AGENTBOX_DEFAULT_TIMEOUT_SECONDS=30
|
||||
AGENTBOX_SANDBOX_BACKEND=subprocess
|
||||
# AGENTBOX_SANDBOX_BACKEND=docker
|
||||
# AGENTBOX_DOCKER_IMAGE=python:3.12-slim
|
||||
# AGENTBOX_DOCKER_NODE_IMAGE=node:20-slim
|
||||
|
||||
@@ -1,5 +1,19 @@
|
||||
# Changelog
|
||||
|
||||
## [0.7.0] - 2026-09-14
|
||||
|
||||
### Added
|
||||
- Production-oriented **Docker sandbox backend** (`AGENTBOX_SANDBOX_BACKEND=docker`)
|
||||
- Ephemeral `docker run --rm` with workspace mounted at `/work`
|
||||
- `--network=none` by default; `--memory` from `limits.memory_mb`
|
||||
- Images: `AGENTBOX_DOCKER_IMAGE` (default `python:3.12-slim`), `AGENTBOX_DOCKER_NODE_IMAGE` (default `node:20-slim`)
|
||||
- Snapshots still work via host-side tar of the mounted workspace
|
||||
- Clear error when the `docker` CLI is missing
|
||||
- `sandbox_backend` values: `subprocess` (default) | `docker` | `unrestricted`
|
||||
|
||||
### Notes
|
||||
- Subprocess remains the default for easy local tests; Docker is recommended for stronger isolation
|
||||
|
||||
## [0.6.0] - 2026-09-14
|
||||
|
||||
### Added
|
||||
|
||||
@@ -7,7 +7,7 @@ Self-hosted **code execution sandbox** for AI agents — one `docker compose up`
|
||||
[](https://www.python.org/downloads/)
|
||||
[](https://github.com/yashshah9/agentbox/actions/workflows/ci.yml)
|
||||
|
||||
> **Status:** v0.6 — Python + Node subprocess sandbox, timeouts, memory limits, Linux default-deny egress (`unshare`/`bwrap`) with `network_isolated`, TypeScript client, workspace snapshots.
|
||||
> **Status:** v0.7 — Python + Node execution, **Docker sandbox backend** (recommended isolation), subprocess default for local tests, timeouts, memory limits, Linux netns egress deny, TypeScript client, workspace snapshots.
|
||||
|
||||
## 60-second try
|
||||
|
||||
@@ -25,7 +25,7 @@ curl -s -X POST http://localhost:8080/v1/run \
|
||||
|
||||
| Approach | Strength | Gap |
|
||||
|----------|----------|-----|
|
||||
| **agentbox** | Self-hosted HTTP API + SDK, one compose file | Subprocess isolation today, not gVisor |
|
||||
| **agentbox** | Self-hosted HTTP API + SDK; Docker or subprocess backends | Not gVisor/Firecracker (yet) |
|
||||
| Hosted sandboxes (E2B, etc.) | Strong isolation, managed | Per-second cost; data leaves your network |
|
||||
| Raw `docker exec` | Familiar | No agent-oriented API / snapshots / limits |
|
||||
| YOLO in the agent process | Zero infra | Full host compromise risk |
|
||||
@@ -34,25 +34,26 @@ curl -s -X POST http://localhost:8080/v1/run \
|
||||
|
||||
Every agent that writes and runs code needs a safe execution environment. Teams either YOLO in shared containers or pay per-second for hosted sandboxes. Self-hosting gVisor/Firecracker is weeks of work.
|
||||
|
||||
## Key features (v0.6)
|
||||
## Key features (v0.7)
|
||||
|
||||
- HTTP API: `POST /v1/run` executes Python or JavaScript
|
||||
- **Docker backend** (`AGENTBOX_SANDBOX_BACKEND=docker`): ephemeral `docker run --rm`, `--network=none`, optional `--memory`, workspace at `/work`
|
||||
- Subprocess backend remains the **default** for easy local tests
|
||||
- Per-request `limits.timeout_seconds` (HTTP 408 on timeout)
|
||||
- Per-request `limits.memory_mb` (sets `RLIMIT_AS`; 16–8192); response includes `limits_applied` + `oom_killed`
|
||||
- Per-request `limits.memory_mb` (Docker `--memory` or subprocess `RLIMIT_AS`); response includes `limits_applied` + `oom_killed`
|
||||
- `AGENTBOX_MAX_MEMORY_MB` clamps requested memory
|
||||
- Default-deny egress when possible: Linux wraps with `unshare --net` or `bwrap --unshare-net`; response includes `network_isolated`
|
||||
- Default-deny egress: Docker uses `--network=none`; subprocess uses Linux `unshare`/`bwrap` when available (`network_isolated` in response)
|
||||
- Workspace snapshots: `"snapshot": true` then `"snapshot_id"`
|
||||
- Python SDK + TypeScript client (`sdk/ts/client.ts`)
|
||||
- Docker image includes Node.js for the JS runtime
|
||||
- Credential stripping when the backend is not `unrestricted` (macOS stays scrub-only)
|
||||
- Credential stripping when the backend is not `unrestricted`
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
Agent / SDK
|
||||
└── POST /v1/run
|
||||
└── SubprocessSandbox (MVP)
|
||||
└── (next) gVisor / Docker backend
|
||||
├── SubprocessSandbox (default — easy tests)
|
||||
└── DockerSandbox (recommended — stronger isolation)
|
||||
```
|
||||
|
||||
| Component | Technology | Why |
|
||||
@@ -60,6 +61,7 @@ Agent / SDK
|
||||
| API | FastAPI | Async-ready, OpenAPI docs, widely adopted |
|
||||
| Server | uvicorn | Standard ASGI server |
|
||||
| Config | pydantic-settings | Typed env config |
|
||||
| Isolation | Docker / subprocess | Docker for production; subprocess for CI/dev |
|
||||
| Tests | pytest + httpx TestClient | Fast API testing |
|
||||
|
||||
## Installation
|
||||
@@ -79,6 +81,20 @@ agentbox serve
|
||||
docker compose up agentbox
|
||||
```
|
||||
|
||||
### Recommended: Docker sandbox backend
|
||||
|
||||
Requires the Docker CLI (and a reachable daemon) on the host running agentbox:
|
||||
|
||||
```bash
|
||||
export AGENTBOX_SANDBOX_BACKEND=docker
|
||||
# optional:
|
||||
# export AGENTBOX_DOCKER_IMAGE=python:3.12-slim
|
||||
# export AGENTBOX_DOCKER_NODE_IMAGE=node:20-slim
|
||||
agentbox serve
|
||||
```
|
||||
|
||||
Each `/v1/run` starts an ephemeral container, mounts a temp workspace at `/work`, applies timeout on `docker run`, and removes the container (`--rm`). Health and run responses report `backend: "docker"`.
|
||||
|
||||
### Run code
|
||||
|
||||
```bash
|
||||
@@ -113,6 +129,8 @@ docker compose up agentbox # start API on :8080
|
||||
docker compose run --rm test # run unit tests
|
||||
```
|
||||
|
||||
Note: using `AGENTBOX_SANDBOX_BACKEND=docker` from inside a container needs Docker socket access (DinD / mounted socket). Prefer running the API on the host with the Docker backend, or keep the compose service on `subprocess`.
|
||||
|
||||
## Configuration
|
||||
|
||||
| Variable | Default | Description |
|
||||
@@ -120,23 +138,28 @@ docker compose run --rm test # run unit tests
|
||||
| `AGENTBOX_HOST` | `0.0.0.0` | Bind host |
|
||||
| `AGENTBOX_PORT` | `8080` | Bind port |
|
||||
| `AGENTBOX_DEFAULT_TIMEOUT_SECONDS` | `30` | Execution timeout |
|
||||
| `AGENTBOX_DEFAULT_MEMORY_MB` | unset | Optional default `RLIMIT_AS` cap |
|
||||
| `AGENTBOX_MAX_MEMORY_MB` | unset | Clamp requested `memory_mb` to this max |
|
||||
| `AGENTBOX_SANDBOX_BACKEND` | `subprocess` | Backend selector |
|
||||
| `AGENTBOX_DEFAULT_MEMORY_MB` | unset | Optional default memory cap |
|
||||
| `AGENTBOX_MAX_MEMORY_MB` | `8192` | Clamp requested `memory_mb` to this max |
|
||||
| `AGENTBOX_SANDBOX_BACKEND` | `subprocess` | `subprocess` \| `docker` \| `unrestricted` |
|
||||
| `AGENTBOX_DOCKER_IMAGE` | `python:3.12-slim` | Image for Python runs (docker backend) |
|
||||
| `AGENTBOX_DOCKER_NODE_IMAGE` | `node:20-slim` | Image for JavaScript runs (docker backend) |
|
||||
| `AGENTBOX_SNAPSHOT_DIR` | `/tmp/agentbox-snapshots` | Workspace snapshot store |
|
||||
|
||||
## Running tests
|
||||
|
||||
```bash
|
||||
pytest tests/ -v
|
||||
# Docker unit tests mock the CLI; live Docker run is skipped if docker is unavailable
|
||||
pytest tests/test_docker.py -v
|
||||
```
|
||||
|
||||
## Roadmap
|
||||
|
||||
- [x] Node.js runtime + TypeScript client + per-run timeout
|
||||
- [x] Filesystem snapshot/restore (tar workspaces)
|
||||
- [x] `limits.memory_mb` via `RLIMIT_AS`
|
||||
- [x] `limits.memory_mb` via `RLIMIT_AS` / Docker `--memory`
|
||||
- [x] Default-deny egress via Linux netns (`unshare`/`bwrap`) when available
|
||||
- [x] Docker ephemeral-container backend
|
||||
- [ ] gVisor runsc backend with warm pool
|
||||
- [ ] Egress allowlists (beyond all-or-nothing netns)
|
||||
|
||||
@@ -144,10 +167,11 @@ pytest tests/ -v
|
||||
|
||||
MIT
|
||||
|
||||
## Known limitations (v0.6)
|
||||
## Known limitations (v0.7)
|
||||
|
||||
- Subprocess sandbox only — **not production-grade isolation**
|
||||
- `RLIMIT_AS` is a soft address-space cap, not a cgroup memory controller
|
||||
- Default-deny egress uses Linux `unshare`/`bwrap` when present; **macOS stays credential-scrub only** (`network_isolated: false`)
|
||||
- **Subprocess** default is convenient for tests — **not production-grade isolation**; use `AGENTBOX_SANDBOX_BACKEND=docker` for stronger isolation
|
||||
- Docker backend needs a local Docker CLI/daemon; missing CLI returns a clear 400
|
||||
- Subprocess `RLIMIT_AS` is a soft address-space cap, not a cgroup memory controller (Docker uses `--memory`)
|
||||
- Subprocess default-deny egress uses Linux `unshare`/`bwrap` when present; **macOS stays credential-scrub only** (`network_isolated: false`)
|
||||
- Single-node, no warm pool
|
||||
- TypeScript client is source-only (not published to npm)
|
||||
|
||||
+5
-4
@@ -6,10 +6,11 @@ Email **yash376351@gmail.com** with the repo name, a short description, and step
|
||||
|
||||
## Threat model (honest)
|
||||
|
||||
agentbox executes **untrusted code** on your machine via a subprocess sandbox.
|
||||
agentbox executes **untrusted code** on your machine via a configurable sandbox backend.
|
||||
|
||||
- The current backend is **subprocess + optional `RLIMIT_AS`**, not gVisor/Firecracker. Do **not** expose it to the public internet as-is.
|
||||
- Credential stripping and timeouts reduce accidents; they are **not** a kernel isolation story.
|
||||
- `limits.memory_mb` caps address space via `RLIMIT_AS` — it is not a cgroup memory controller and behavior differs by OS.
|
||||
- **Recommended:** `AGENTBOX_SANDBOX_BACKEND=docker` — ephemeral containers (`--rm`, `--network=none`, optional `--memory`). Still not gVisor/Firecracker; do **not** expose it to the public internet without additional hardening/auth.
|
||||
- **Default:** `subprocess` + optional `RLIMIT_AS` — convenient for local tests, **not** production-grade isolation.
|
||||
- Credential stripping and timeouts reduce accidents; they are **not** a full kernel isolation story.
|
||||
- `limits.memory_mb` uses Docker `--memory` (docker backend) or `RLIMIT_AS` (subprocess); behavior differs by OS/backend.
|
||||
- Snapshots persist workspace files on disk under `AGENTBOX_SNAPSHOT_DIR`.
|
||||
- Prefer running the API only on trusted networks, behind auth you add yourself.
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "agentbox-sandbox"
|
||||
version = "0.6.0"
|
||||
version = "0.7.0"
|
||||
description = "Self-hosted code execution sandbox for AI agents"
|
||||
readme = "README.md"
|
||||
license = "MIT"
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
"""Self-hosted code execution sandbox for AI agents."""
|
||||
|
||||
__version__ = "0.6.0"
|
||||
__version__ = "0.7.0"
|
||||
|
||||
+34
-7
@@ -7,16 +7,43 @@ from pydantic import BaseModel, Field
|
||||
|
||||
from agentbox import __version__
|
||||
from agentbox.config import Settings
|
||||
from agentbox.sandbox.docker import DockerSandbox
|
||||
from agentbox.sandbox.runner import SubprocessSandbox
|
||||
|
||||
|
||||
def create_sandbox(settings: Settings) -> SubprocessSandbox | DockerSandbox:
|
||||
"""Select execution backend from settings."""
|
||||
backend = settings.sandbox_backend.lower().strip()
|
||||
if backend == "docker":
|
||||
return DockerSandbox(
|
||||
timeout_seconds=settings.default_timeout_seconds,
|
||||
max_output_bytes=settings.max_output_bytes,
|
||||
snapshot_dir=settings.snapshot_dir,
|
||||
default_memory_mb=settings.default_memory_mb,
|
||||
deny_egress=True,
|
||||
docker_image=settings.docker_image,
|
||||
docker_node_image=settings.docker_node_image,
|
||||
)
|
||||
if backend == "unrestricted":
|
||||
return SubprocessSandbox(
|
||||
timeout_seconds=settings.default_timeout_seconds,
|
||||
max_output_bytes=settings.max_output_bytes,
|
||||
snapshot_dir=settings.snapshot_dir,
|
||||
default_memory_mb=settings.default_memory_mb,
|
||||
deny_egress=False,
|
||||
)
|
||||
# Default: subprocess (easy local tests; not production isolation)
|
||||
return SubprocessSandbox(
|
||||
timeout_seconds=settings.default_timeout_seconds,
|
||||
max_output_bytes=settings.max_output_bytes,
|
||||
snapshot_dir=settings.snapshot_dir,
|
||||
default_memory_mb=settings.default_memory_mb,
|
||||
deny_egress=True,
|
||||
)
|
||||
|
||||
|
||||
settings = Settings()
|
||||
sandbox = SubprocessSandbox(
|
||||
timeout_seconds=settings.default_timeout_seconds,
|
||||
max_output_bytes=settings.max_output_bytes,
|
||||
deny_egress=settings.sandbox_backend != "unrestricted",
|
||||
snapshot_dir=settings.snapshot_dir,
|
||||
default_memory_mb=settings.default_memory_mb,
|
||||
)
|
||||
sandbox = create_sandbox(settings)
|
||||
|
||||
app = FastAPI(title="agentbox", version=__version__)
|
||||
|
||||
|
||||
@@ -11,7 +11,9 @@ class Settings(BaseSettings):
|
||||
log_level: str = "INFO"
|
||||
default_timeout_seconds: int = 30
|
||||
max_output_bytes: int = 1_048_576
|
||||
sandbox_backend: str = "subprocess" # subprocess | gvisor (future)
|
||||
sandbox_backend: str = "subprocess" # subprocess | docker | unrestricted
|
||||
snapshot_dir: str = "/tmp/agentbox-snapshots"
|
||||
default_memory_mb: int | None = None
|
||||
max_memory_mb: int = 8192
|
||||
docker_image: str = "python:3.12-slim"
|
||||
docker_node_image: str = "node:20-slim"
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
"""Docker sandbox: ephemeral containers for stronger isolation than subprocess."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
from agentbox.sandbox.runner import RunResult, _likely_oom
|
||||
from agentbox.sandbox.snapshots import restore_snapshot, save_snapshot
|
||||
|
||||
DEFAULT_PYTHON_IMAGE = "python:3.12-slim"
|
||||
DEFAULT_NODE_IMAGE = "node:20-slim"
|
||||
|
||||
|
||||
class DockerSandbox:
|
||||
"""Run code in an ephemeral ``docker run --rm`` container.
|
||||
|
||||
Workspace is bind-mounted at ``/work``. With ``deny_egress`` (default),
|
||||
networking is ``--network=none``. Memory uses Docker ``--memory``.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
timeout_seconds: int = 30,
|
||||
max_output_bytes: int = 1_048_576,
|
||||
deny_egress: bool = True,
|
||||
snapshot_dir: str | Path | None = None,
|
||||
default_memory_mb: int | None = None,
|
||||
docker_image: str = DEFAULT_PYTHON_IMAGE,
|
||||
docker_node_image: str = DEFAULT_NODE_IMAGE,
|
||||
docker_binary: str | None = None,
|
||||
) -> None:
|
||||
self.timeout_seconds = timeout_seconds
|
||||
self.max_output_bytes = max_output_bytes
|
||||
self.deny_egress = deny_egress
|
||||
self.snapshot_dir = Path(snapshot_dir) if snapshot_dir else Path("/tmp/agentbox-snapshots")
|
||||
self.default_memory_mb = default_memory_mb
|
||||
self.docker_image = docker_image
|
||||
self.docker_node_image = docker_node_image
|
||||
self.docker_binary = docker_binary or "docker"
|
||||
|
||||
def run(
|
||||
self,
|
||||
code: str,
|
||||
language: str = "python",
|
||||
timeout_seconds: int | None = None,
|
||||
snapshot_id: str | None = None,
|
||||
persist_snapshot: bool = False,
|
||||
memory_mb: int | None = None,
|
||||
) -> RunResult:
|
||||
docker = self._resolve_docker()
|
||||
timeout = timeout_seconds or self.timeout_seconds
|
||||
mem = memory_mb if memory_mb is not None else self.default_memory_mb
|
||||
start = time.monotonic()
|
||||
with tempfile.TemporaryDirectory(prefix="agentbox-docker-") as tmp:
|
||||
workspace = Path(tmp)
|
||||
if snapshot_id:
|
||||
try:
|
||||
restore_snapshot(snapshot_id, workspace, self.snapshot_dir)
|
||||
except FileNotFoundError as exc:
|
||||
raise ValueError(str(exc)) from exc
|
||||
lang = language.lower()
|
||||
if lang == "python":
|
||||
(workspace / "main.py").write_text(code, encoding="utf-8")
|
||||
image = self.docker_image
|
||||
elif lang in {"javascript", "node"}:
|
||||
(workspace / "main.js").write_text(code, encoding="utf-8")
|
||||
image = self.docker_node_image
|
||||
else:
|
||||
raise ValueError(f"Unsupported language: {language}")
|
||||
|
||||
name = f"agentbox-{uuid.uuid4().hex[:12]}"
|
||||
argv = build_docker_argv(
|
||||
docker_binary=docker,
|
||||
workspace=workspace,
|
||||
language=lang,
|
||||
image=image,
|
||||
memory_mb=mem,
|
||||
network_none=self.deny_egress,
|
||||
container_name=name,
|
||||
)
|
||||
network_isolated = self.deny_egress
|
||||
try:
|
||||
proc = subprocess.run(
|
||||
argv,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=timeout,
|
||||
check=False,
|
||||
)
|
||||
except subprocess.TimeoutExpired:
|
||||
_force_remove_container(docker, name)
|
||||
raise
|
||||
except FileNotFoundError as exc:
|
||||
raise ValueError(
|
||||
"Docker backend requires the docker CLI; "
|
||||
"install Docker or set AGENTBOX_SANDBOX_BACKEND=subprocess"
|
||||
) from exc
|
||||
|
||||
new_snapshot = save_snapshot(workspace, self.snapshot_dir) if persist_snapshot else None
|
||||
stdout = proc.stdout[: self.max_output_bytes]
|
||||
stderr = proc.stderr[: self.max_output_bytes]
|
||||
exit_code = proc.returncode
|
||||
duration = int((time.monotonic() - start) * 1000)
|
||||
return RunResult(
|
||||
stdout=stdout,
|
||||
stderr=stderr,
|
||||
exit_code=exit_code,
|
||||
duration_ms=duration,
|
||||
snapshot_id=new_snapshot,
|
||||
oom_killed=_likely_oom(exit_code, stdout, stderr, mem),
|
||||
network_isolated=network_isolated,
|
||||
)
|
||||
|
||||
def run_python(self, code: str) -> RunResult:
|
||||
return self.run(code, "python")
|
||||
|
||||
def _resolve_docker(self) -> str:
|
||||
binary = self.docker_binary
|
||||
path = shutil.which(binary) if not Path(binary).is_file() else binary
|
||||
if not path:
|
||||
raise ValueError(
|
||||
"Docker backend requires the docker CLI; "
|
||||
"install Docker or set AGENTBOX_SANDBOX_BACKEND=subprocess"
|
||||
)
|
||||
return path
|
||||
|
||||
|
||||
def build_docker_argv(
|
||||
*,
|
||||
docker_binary: str,
|
||||
workspace: Path,
|
||||
language: str,
|
||||
image: str,
|
||||
memory_mb: int | None = None,
|
||||
network_none: bool = True,
|
||||
container_name: str | None = None,
|
||||
) -> list[str]:
|
||||
"""Build ``docker run`` argv for a language workspace (unit-testable)."""
|
||||
argv = [docker_binary, "run", "--rm"]
|
||||
if container_name:
|
||||
argv.extend(["--name", container_name])
|
||||
if network_none:
|
||||
argv.append("--network=none")
|
||||
if memory_mb is not None:
|
||||
argv.extend(["--memory", f"{max(1, memory_mb)}m"])
|
||||
argv.extend(
|
||||
[
|
||||
"-v",
|
||||
f"{workspace.resolve()}:/work",
|
||||
"-w",
|
||||
"/work",
|
||||
image,
|
||||
]
|
||||
)
|
||||
lang = language.lower()
|
||||
if lang == "python":
|
||||
argv.extend(["python", "/work/main.py"])
|
||||
elif lang in {"javascript", "node"}:
|
||||
argv.extend(["node", "/work/main.js"])
|
||||
else:
|
||||
raise ValueError(f"Unsupported language: {language}")
|
||||
return argv
|
||||
|
||||
|
||||
def _force_remove_container(docker: str, name: str) -> None:
|
||||
subprocess.run(
|
||||
[docker, "rm", "-f", name],
|
||||
capture_output=True,
|
||||
timeout=10,
|
||||
check=False,
|
||||
)
|
||||
|
||||
|
||||
def docker_available(docker_binary: str = "docker") -> bool:
|
||||
return shutil.which(docker_binary) is not None
|
||||
@@ -37,7 +37,7 @@ class SubprocessSandbox:
|
||||
When deny_egress is set, also tries Linux network-namespace isolation via
|
||||
unshare/bwrap when available. macOS stays credential-scrub only.
|
||||
|
||||
ponytail: not production isolation — upgrade path is gVisor/docker backend.
|
||||
ponytail: not production isolation — use DockerSandbox for stronger isolation.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
|
||||
+4
-1
@@ -19,7 +19,9 @@ def client() -> Iterator[TestClient]:
|
||||
def test_health(client: TestClient) -> None:
|
||||
resp = client.get("/health")
|
||||
assert resp.status_code == 200
|
||||
assert resp.json()["status"] == "ok"
|
||||
body = resp.json()
|
||||
assert body["status"] == "ok"
|
||||
assert body["backend"] == "subprocess"
|
||||
|
||||
|
||||
def test_run_python(client: TestClient) -> None:
|
||||
@@ -33,6 +35,7 @@ def test_run_python(client: TestClient) -> None:
|
||||
assert body["oom_killed"] is False
|
||||
assert "network_isolated" in body
|
||||
assert isinstance(body["network_isolated"], bool)
|
||||
assert body["backend"] == "subprocess"
|
||||
|
||||
|
||||
def test_run_rejects_unknown_language(client: TestClient) -> None:
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
"""Tests for Docker sandbox backend."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
from agentbox.api.app import create_sandbox
|
||||
from agentbox.config import Settings
|
||||
from agentbox.sandbox.docker import (
|
||||
DEFAULT_NODE_IMAGE,
|
||||
DEFAULT_PYTHON_IMAGE,
|
||||
DockerSandbox,
|
||||
build_docker_argv,
|
||||
docker_available,
|
||||
)
|
||||
from agentbox.sandbox.runner import SubprocessSandbox
|
||||
|
||||
|
||||
def test_build_docker_argv_python_defaults(tmp_path: Path) -> None:
|
||||
argv = build_docker_argv(
|
||||
docker_binary="docker",
|
||||
workspace=tmp_path,
|
||||
language="python",
|
||||
image=DEFAULT_PYTHON_IMAGE,
|
||||
)
|
||||
assert argv[0] == "docker"
|
||||
assert argv[1:3] == ["run", "--rm"]
|
||||
assert "--network=none" in argv
|
||||
assert "-v" in argv
|
||||
vol = argv[argv.index("-v") + 1]
|
||||
assert vol.endswith(":/work")
|
||||
assert str(tmp_path.resolve()) in vol
|
||||
assert argv[argv.index("-w") + 1] == "/work"
|
||||
assert DEFAULT_PYTHON_IMAGE in argv
|
||||
assert argv[-2:] == ["python", "/work/main.py"]
|
||||
assert "--memory" not in argv
|
||||
assert "--name" not in argv
|
||||
|
||||
|
||||
def test_build_docker_argv_javascript_with_memory_and_name(tmp_path: Path) -> None:
|
||||
argv = build_docker_argv(
|
||||
docker_binary="/usr/bin/docker",
|
||||
workspace=tmp_path,
|
||||
language="javascript",
|
||||
image=DEFAULT_NODE_IMAGE,
|
||||
memory_mb=256,
|
||||
network_none=True,
|
||||
container_name="agentbox-test",
|
||||
)
|
||||
assert argv[0] == "/usr/bin/docker"
|
||||
assert "--name" in argv
|
||||
assert argv[argv.index("--name") + 1] == "agentbox-test"
|
||||
assert "--network=none" in argv
|
||||
assert "--memory" in argv
|
||||
assert argv[argv.index("--memory") + 1] == "256m"
|
||||
assert argv[-2:] == ["node", "/work/main.js"]
|
||||
|
||||
|
||||
def test_build_docker_argv_allows_network_when_not_deny(tmp_path: Path) -> None:
|
||||
argv = build_docker_argv(
|
||||
docker_binary="docker",
|
||||
workspace=tmp_path,
|
||||
language="python",
|
||||
image="python:3.12-slim",
|
||||
network_none=False,
|
||||
)
|
||||
assert "--network=none" not in argv
|
||||
|
||||
|
||||
def test_build_docker_argv_rejects_unknown_language(tmp_path: Path) -> None:
|
||||
with pytest.raises(ValueError, match="Unsupported language"):
|
||||
build_docker_argv(
|
||||
docker_binary="docker",
|
||||
workspace=tmp_path,
|
||||
language="ruby",
|
||||
image="python:3.12-slim",
|
||||
)
|
||||
|
||||
|
||||
def test_docker_sandbox_missing_binary_clear_error(tmp_path: Path) -> None:
|
||||
box = DockerSandbox(snapshot_dir=tmp_path, docker_binary="docker-not-installed-xyz")
|
||||
with pytest.raises(ValueError, match="Docker backend requires the docker CLI"):
|
||||
box.run("print(1)")
|
||||
|
||||
|
||||
def test_docker_sandbox_run_mocked(tmp_path: Path) -> None:
|
||||
box = DockerSandbox(snapshot_dir=tmp_path, timeout_seconds=10)
|
||||
fake_proc = MagicMock()
|
||||
fake_proc.stdout = "hi\n"
|
||||
fake_proc.stderr = ""
|
||||
fake_proc.returncode = 0
|
||||
|
||||
with (
|
||||
patch("agentbox.sandbox.docker.shutil.which", return_value="/usr/bin/docker"),
|
||||
patch("agentbox.sandbox.docker.subprocess.run", return_value=fake_proc) as run_mock,
|
||||
):
|
||||
result = box.run("print('hi')", language="python", memory_mb=128)
|
||||
|
||||
assert result.stdout == "hi\n"
|
||||
assert result.exit_code == 0
|
||||
assert result.network_isolated is True
|
||||
assert result.oom_killed is False
|
||||
argv = run_mock.call_args.args[0]
|
||||
assert argv[0] == "/usr/bin/docker"
|
||||
assert "--network=none" in argv
|
||||
assert "--memory" in argv
|
||||
assert "128m" in argv
|
||||
assert "python" in argv
|
||||
assert "/work/main.py" in argv
|
||||
|
||||
|
||||
def test_create_sandbox_selects_backends() -> None:
|
||||
sub = create_sandbox(Settings(sandbox_backend="subprocess"))
|
||||
assert isinstance(sub, SubprocessSandbox)
|
||||
assert sub.deny_egress is True
|
||||
|
||||
unr = create_sandbox(Settings(sandbox_backend="unrestricted"))
|
||||
assert isinstance(unr, SubprocessSandbox)
|
||||
assert unr.deny_egress is False
|
||||
|
||||
dock = create_sandbox(
|
||||
Settings(
|
||||
sandbox_backend="docker",
|
||||
docker_image="python:3.12-slim",
|
||||
docker_node_image="node:20-slim",
|
||||
)
|
||||
)
|
||||
assert isinstance(dock, DockerSandbox)
|
||||
assert dock.deny_egress is True
|
||||
assert dock.docker_image == "python:3.12-slim"
|
||||
assert dock.docker_node_image == "node:20-slim"
|
||||
|
||||
|
||||
@pytest.mark.skipif(not docker_available(), reason="docker CLI not available")
|
||||
def test_docker_live_python(tmp_path: Path) -> None:
|
||||
"""Optional integration check when Docker is installed and daemon reachable."""
|
||||
# Probe daemon; skip if CLI exists but daemon is down.
|
||||
probe = shutil.which("docker")
|
||||
assert probe
|
||||
import subprocess
|
||||
|
||||
ping = subprocess.run(
|
||||
[probe, "info"],
|
||||
capture_output=True,
|
||||
timeout=15,
|
||||
check=False,
|
||||
)
|
||||
if ping.returncode != 0:
|
||||
pytest.skip("docker daemon not reachable")
|
||||
|
||||
box = DockerSandbox(snapshot_dir=tmp_path, timeout_seconds=60)
|
||||
result = box.run("print('docker-ok')", language="python")
|
||||
assert result.exit_code == 0
|
||||
assert "docker-ok" in result.stdout
|
||||
assert result.network_isolated is True
|
||||
@@ -20,13 +20,13 @@ def client() -> Iterator[TestClient]:
|
||||
yield test_client
|
||||
|
||||
|
||||
def test_health_version_060(client: TestClient) -> None:
|
||||
def test_health_version_070(client: TestClient) -> None:
|
||||
resp = client.get("/health")
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["status"] == "ok"
|
||||
assert body["version"] == "0.6.0"
|
||||
assert __version__ == "0.6.0"
|
||||
assert body["version"] == "0.7.0"
|
||||
assert __version__ == "0.7.0"
|
||||
|
||||
|
||||
def test_python_success(client: TestClient) -> None:
|
||||
|
||||
Reference in New Issue
Block a user