Sandbox Guide

CortexPrism provides sandboxed code execution using Docker containers (with subprocess fallback). This guide covers usage, configuration, and security.

Overview

The sandbox system allows agents to execute code safely in isolated environments with resource constraints. Code runs in ephemeral containers that are destroyed after execution.

How It Works

cortex run script.py                 # Auto-detect language, run in sandbox
cortex run script.py --no-sandbox    # Skip sandbox, run as subprocess
cortex run script.py --fix           # Enable auto-fix on failure

Docker Sandbox

When Docker is available, containers are created with strict resource limits:

ConstraintValue
Network--network=none (no external access)
Memory256MB limit
CPU0.5 cores
Process limit64 PIDs
Securityno-new-privileges
Timeout30 seconds
Max output64KB

Supported Languages

LanguageExtensionDocker ImageInterpreter
Python.pypython:3.12-slimpython3
JavaScript.jsnode:22-slimnode
TypeScript.tsnode:22-slimnpx tsx
Bash.shubuntu:24.04bash
Ruby.rbruby:3.3-slimruby
Go.gogolang:1.23-bookwormgo run
Rust.rsrust:1.78-slimrustc -o /tmp/out && /tmp/out

Subprocess Fallback

When docker info fails (Docker not installed or daemon not running), the sandbox falls back to subprocess mode. The code runs directly on the host machine with the same resource limits applied at the process level.

Auto-Fix Loop

When --fix is enabled, the system can automatically fix broken code:

runInSandbox(code)
  → exit != 0?
     → LLM receives: "Fix this error: <stderr>\n\nCode:\n<code>"
     → LLM returns fixed code
     → runInSandbox(fixedCode)
     → repeat up to maxRounds (default: 4)
# Enable auto-fix
cortex run buggy-code.py --fix

# Increase max fix attempts
cortex run complex.js --fix --max-fix 8

Security Considerations

  • Docker sandbox has no network access (--network=none)
  • Memory and CPU are strictly limited to prevent resource exhaustion
  • Containers are ephemeral — no data persists after execution
  • The no-new-privileges security flag prevents privilege escalation
  • Output is capped at 64KB to prevent log flooding

Configuration

The sandbox can be configured through environment variables:

VariableDescriptionDefault
CORTEX_SANDBOX_TIMEOUTExecution timeout in seconds30
CORTEX_SANDBOX_MEMORYMemory limit in MB256
CORTEX_SANDBOX_MAX_OUTPUTOutput cap in KB64

Best Practices

  1. Always use the sandbox for untrusted code — avoid --no-sandbox unless necessary
  2. Enable auto-fix during development to iterate faster
  3. Keep scripts simple — the sandbox has limited memory and no network
  4. Use Python for data analysis — the Python sandbox includes pip for package installation