Lesson 3 · AI-native software engineering
Sandboxing and blast radius isolation
Isolate agent tool execution with Docker containers, WebContainers, microVMs, filesystem virtualization, and least-privilege network controls.
What is sandboxing in AI engineering?
Sandboxing is the defensive architecture that confines an agent's code execution, tool calls, and shell commands to an isolated, ephemeral runtime environment. When an autonomous agent runs bash commands, tests code, installs packages, or makes network requests, giving it direct host access exposes infrastructure to catastrophic risk: accidental file deletion, credential extraction, process hijacking, or prompt-injected SSRF attacks.
Modern isolation tiers include:
1. Process jail & Linux namespaces: unprivileged user namespaces and chroot.
2. Container isolation (Docker, gVisor): kernel-level resource virtualization with dropped capabilities.
3. In-browser WebContainers (Wasm): zero-installation client sandboxes for interactive playgrounds.
4. Hardware-virtualized microVMs (AWS Firecracker, Fly Machines): lightweight virtual machines that boot in milliseconds with dedicated guest kernels.
Why sandboxing matters
Autonomous coding agents regularly execute untrusted third-party packages, run build scripts with unverified dependencies, and process user-supplied code. A single prompt injection or agent hallucination can exfiltrate environment variables or destroy production systems unless the execution environment has a strict, zero-trust blast radius.
Three-tier sandbox architecture
Production sandbox architecture:
Agent Orchestrator (Host/Cloud Runtime)
├── [Secure RPC / MCP Gateway (mTLS, scoped tokens)]
│ ↓
└── [Ephemeral Sandbox Container / microVM]
├── Read-only root filesystem (with tmpfs workspace copy)
├── Restricted Linux capabilities (drop ALL, no root escalation)
├── Network egress proxy (domain allowlist, metadata IP blocked: 169.254.169.254)
└── CPU, memory, and timeout limits (cgroups v2).
Sandboxed command execution
import subprocess
def execute_sandboxed_command(command: list[str], workspace_dir: str, timeout_sec: int = 60) -> dict:
"""Executes agent shell commands inside a hardened, unprivileged Docker container."""
# Security flags:
# --read-only: prevents root filesystem modification
# --cap-drop ALL: drops all Linux root capabilities
# --security-opt=no-new-privileges: blocks setuid privilege escalation
# --network=agent-isolated: custom network blocking 169.254.169.254
docker_cmd = [
"docker", "run", "--rm",
"--read-only",
"--cap-drop", "ALL",
"--security-opt=no-new-privileges",
"--network", "agent-isolated",
"--memory", "2g",
"--cpus", "2.0",
"-v", f"{workspace_dir}:/workspace:rw",
"-w", "/workspace",
"agent-sandbox-runner:v1.2",
*command,
]
try:
res = subprocess.run(docker_cmd, capture_output=True, text=True, timeout=timeout_sec)
return {"exit_code": res.returncode, "stdout": res.stdout, "stderr": res.stderr}
except subprocess.TimeoutExpired:
return {"exit_code": -1, "stdout": "", "stderr": f"Command timed out after {timeout_sec}s"}Experiment: isolation tiers
Evaluate how different sandbox tiers protect against adversarial attacks and accidental command execution.
What to observe
Standard Docker containers share the host Linux kernel and allow egress to cloud metadata services by default. True production isolation requires dropping kernel capabilities, blocking private link-local IP addresses (169.254.169.254), and running unprivileged user namespaces.
Production sandboxing
Block cloud metadata endpoints (169.254.169.254) on all agent egress networks. Use ephemeral workspaces destroyed immediately after each task. Mount code repositories as non-root users. Enforce cgroups resource limits (CPU, memory, disk, process count). Externalize execution timeouts in the orchestrator.
Challenge
An agent analyzing a customer's repository runs npm test. The repository contains a malicious postinstall script that attempts to curl your AWS Secrets Manager endpoint. Detail the egress proxy and network namespace configuration that stops this exfiltration.
Production checklist
Production checklist
0 of 7 checked
Key takeaway
In cloud environments, 169.254.169.254 is the instance metadata service (IMDS). Unless blocked by iptables or network policy, any process inside an agent container can query this endpoint to retrieve IAM role credentials, user data, and host tokens. Strict egress filtering is mandatory.
Complete
You can now design secure sandboxed environments for agent code execution. Next: automated verification and self-healing CI.
Mark this chapter as complete
Track your progress and unlock the next chapter.