Sandbox Mode¶
Sandbox mode runs PatchPal inside an isolated Docker or Podman container, providing security boundaries for AI agent operations. This is the recommended way to run PatchPal, especially for autopilot mode or when working with untrusted code.
Quick Start¶
# Interactive mode
patchpal-sandbox --env-file .env -- --model anthropic/claude-sonnet-4-5
# Autopilot mode
patchpal-sandbox --env-file .env -- autopilot --prompt-file task.md
# With local Ollama (see Local Models below for other setups)
patchpal-sandbox --host-network -- --model ollama_chat/glm-4.7-flash:q4_K_M
The -- separator distinguishes sandbox options (left side) from PatchPal arguments (right side).
Why Use Sandbox Mode?¶
Security Benefits¶
1. Ephemeral Filesystem
- Container is destroyed after each session (--rm flag)
- Malicious code can't persist backdoors
- File modifications isolated to mounted /workspace
2. Network Isolation (with --restrict-network)
- Blocks data exfiltration attempts
- Prevents backdoor downloads
- Allows only whitelisted endpoints
3. Resource Limits - Memory and CPU constraints prevent resource exhaustion - Protects host system from runaway processes
4. Process Isolation - Agent runs in separate namespace - Can't access host processes or system files (outside mounts)
Convenience Benefits¶
- ✅ Pre-built image with patchpal installed (fast startup)
- ✅ Auto-detects Docker/Podman (prefers Podman rootless)
- ✅ Loads API keys from
.envfiles - ✅ Mounts custom tools automatically
- ✅ Clean environment on each run
Installation¶
No separate installation needed - patchpal-sandbox is included with PatchPal:
First Run: Downloads pre-built container image (~150MB, one-time) Subsequent Runs: Start instantly (no pip install needed)
Updating the image: Run docker pull ghcr.io/amaiya/patchpal-sandbox:latest (or podman pull) to get the latest version.
Windows Setup (Podman)¶
On Windows, if using Podman, you need to set up a Podman machine before using patchpal-sandbox:
Step 1: Create and start Podman machine
Note: Use --rootful mode to avoid socket permission issues. This is the recommended configuration for Windows.
Step 2: Configure corporate firewall certificates (if behind a corporate firewall)
If you're behind a corporate firewall with SSL inspection, you need to add your company's CA certificates to the Podman machine so it can pull Docker images:
Inside the Podman machine:
# Create directory for CA certificates
sudo mkdir -p /etc/pki/ca-trust/source/anchors/
# Copy your company's CA bundle
# (You should already have this if Python/pip works with your firewall)
sudo vi /etc/pki/ca-trust/source/anchors/your-company-ca-bundle.crt
# Paste the certificate content and save
# Update the CA trust store
sudo update-ca-trust
# Exit the SSH session
exit
Finding your CA bundle:
- If Python/pip already works, check: %SSL_CERT_FILE% or %REQUESTS_CA_BUNDLE%
- Contact your IT department for the corporate root CA certificate
- Common locations: C:\Program Files\CompanyCerts\ or similar
After completing these steps, patchpal-sandbox should work correctly.
Troubleshooting:
- If podman machine start fails, try: podman machine stop && podman machine start
- If certificate errors persist, verify the certificate was copied correctly: podman machine ssh -- cat /etc/pki/ca-trust/source/anchors/your-company-ca-bundle.crt
- To recreate the machine: podman machine stop && podman machine rm && podman machine init --rootful && podman machine start
Basic Usage¶
Interactive Mode¶
# Using .env file for API keys
patchpal-sandbox --env-file .env -- --model anthropic/claude-sonnet-4-5
# Passing an API key (or any env var) directly
patchpal-sandbox -e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY -- --model anthropic/claude-sonnet-4-5
Note: --env-file accepts a single file. Use -e KEY=VALUE (repeatable) to pass
individual variables, or rely on your host environment (see
Environment Variables).
In interactive mode, permissions are enabled and you'll be prompted before destructive operations.
Autopilot Mode¶
# From prompt file
patchpal-sandbox --env-file .env -- autopilot \
--model anthropic/claude-sonnet-4-5 \
--prompt-file task.md \
--max-iterations 30
# Inline prompt
patchpal-sandbox --env-file .env -- autopilot \
--model openai/gpt-4o \
--prompt "Build a calculator with tests"
In autopilot mode, permissions are automatically disabled for safety. See Autopilot Mode for comprehensive documentation.
Local Models (Ollama)¶
How to reach a local Ollama depends on where Ollama and patchpal-sandbox run, and on whether you use --restrict-network. Find your setup:
| Ollama runs on | patchpal-sandbox runs on |
Without --restrict-network |
With --restrict-network |
|---|---|---|---|
| Windows | WSL (rootless Podman) | --host-network |
allow_host_loopback (Podman before 6) |
| Windows | Windows (Podman machine) | --host-network |
host.containers.internal (needs OLLAMA_HOST=0.0.0.0) |
| Linux | Linux (rootless Podman) | --host-network |
allow_host_loopback (Podman before 6) |
| Linux | Linux (Docker) | --host-network |
172.17.0.1 (needs OLLAMA_HOST=0.0.0.0) |
| Windows | Docker Desktop | host.docker.internal (may need OLLAMA_HOST=0.0.0.0) |
host.docker.internal (may need OLLAMA_HOST=0.0.0.0) |
--host-network and allow_host_loopback reach Ollama on its default address (127.0.0.1), so Ollama needs no configuration. OLLAMA_HOST=0.0.0.0 makes Ollama listen on all network interfaces, which lets other machines reach it too (see the warning under Option 2 in With network restrictions).
Podman 6: slirp4netns is removed in Podman 6. Rootless Podman uses pasta instead (the default since Podman 5), and the allow_host_loopback option below doesn't apply to it. pasta has a --map-host-loopback option that is the likely equivalent, but it hasn't been tested with PatchPal (see Option 1 below). Check your version with podman --version.
The Windows rows need WSL mirrored networking: add networkingMode=mirrored under [wsl2] in %UserProfile%\.wslconfig and restart WSL. This shares localhost between Windows and WSL, including the WSL2 VM that Podman uses on Windows.
OLLAMA_API_BASE (and HOSTED_VLLM_API_BASE for vLLM) is passed into the container. With --restrict-network, it's also auto-detected, and the firewall allows only that host and port.
Set the context window on the Ollama server (OLLAMA_CONTEXT_LENGTH), not in the container, and set PATCHPAL_CONTEXT_LIMIT to match it. PATCHPAL_* variables are passed into the container. See Small Context Windows.
The examples use bash syntax. In the Windows Command Prompt, use set VAR=value instead of export VAR=value. In PowerShell, use $env:VAR = "value". Write commands on one line, since \ line breaks only work in bash.
Host network (no network restrictions)¶
When Ollama runs on the same computer, --host-network lets the container reach it on localhost. Don't set OLLAMA_API_BASE.
patchpal-sandbox --host-network -- --model ollama_chat/glm-4.7-flash:q4_K_M
# Autopilot with Ollama
patchpal-sandbox --host-network -- autopilot \
--model ollama_chat/qwen2.5-coder:32b \
--prompt-file task.md
Note: --host-network shares the host's network stack with the container. This bypasses network isolation - only use with trusted local models. On macOS, containers run in a VM, so --host-network reaches the VM's localhost rather than the Mac's, and won't find Ollama.
Docker Desktop (no network restrictions)¶
Docker Desktop provides host.docker.internal to reach the host. It isn't available by default with Docker on Linux. Untested: it may work with Ollama on its default 127.0.0.1, or Ollama may need to listen on all interfaces (OLLAMA_HOST=0.0.0.0, see the warning under Option 2 below). To check, run curl http://host.docker.internal:11434 inside a container; it prints Ollama is running if Ollama is reachable.
export OLLAMA_API_BASE=http://host.docker.internal:11434
patchpal-sandbox -- --model ollama_chat/qwen3.5:4b
With network restrictions¶
--host-network can't be combined with --restrict-network, because the firewall would apply to the host's network. The container needs another way to reach Ollama.
Option 1: allow_host_loopback (rootless Podman before Podman 6). The container reaches localhost at 10.0.2.2, and Ollama stays on 127.0.0.1, so nothing is exposed to your network:
export OLLAMA_API_BASE=http://10.0.2.2:11434
patchpal-sandbox --network slirp4netns:allow_host_loopback=true \
--restrict-network -- --model ollama_chat/qwen3.5:4b
- Tested with Ollama on Windows and rootless Podman 3.4.4 in WSL, with mirrored networking. In WSL,
10.0.2.2leads to WSL's localhost, which mirrored networking shares with Windows. - This uses
slirp4netns. Podman 5 defaults topasta, soslirp4netnsmust be installed separately, and Podman 6 removes it. Check your version withpodman --version. - Podman 6 (untested):
pasta's--map-host-loopback ADDRoption makesADDRin the container lead to the host's localhost, which should serve the same purpose. Podman passespastaoptions afterpasta:, separated by commas. Pick an address that isn't otherwise used inside the container (not169.254.1.2, which Podman uses forhost.containers.internal), and setOLLAMA_API_BASEto it. For example:
export OLLAMA_API_BASE=http://169.254.1.10:11434
patchpal-sandbox --network pasta:--map-host-loopback,169.254.1.10 \
--restrict-network -- --model ollama_chat/qwen3.5:4b
This hasn't been tested with PatchPal. If you try it, run with --test-restrictions first to check that Ollama is reachable.
- On Linux with rootless Podman before 6 and Ollama on the same machine, it's expected to work the same way, but this hasn't been tested.
Warning:
allow_host_loopbacklets the container reach every service listening on localhost (in WSL and, with mirrored networking, on Windows). Only use it with--restrict-network, which allows only the port inOLLAMA_API_BASE.
Option 2: expose Ollama to the container network (other setups, untested). Ollama must listen on an address the container can reach. By default it listens only on 127.0.0.1, so host.containers.internal gets Connection refused when Ollama runs on Windows (host.docker.internal may too, untested). One way is to listen on all interfaces (OLLAMA_HOST=0.0.0.0):
# Podman: http://host.containers.internal:11434
# Docker Desktop: http://host.docker.internal:11434
# Docker on Linux: http://172.17.0.1:11434 (check with: ip -4 addr show docker0)
# Ollama on Windows: http://<windows-ip>:11434
export OLLAMA_API_BASE=http://host.containers.internal:11434
patchpal-sandbox --restrict-network -- --model ollama_chat/qwen3.5:4b
Warning: Ollama has no authentication. With
OLLAMA_HOST=0.0.0.0, anyone who can reach your machine on port 11434 can use your models and manage them through Ollama's API. Use a host firewall to allow port 11434 only from the container network, or use--host-networkwithout--restrict-networkinstead.
Network Restrictions¶
For maximum security, use --restrict-network to isolate the container from the external network while allowing access to required LLM APIs:
# Auto-detects LLM endpoints from environment variables
patchpal-sandbox \
--restrict-network \
--env-file .env \
-- autopilot --prompt-file task.md
# With custom allowed URLs
patchpal-sandbox \
--restrict-network \
--allow-url https://api.openai.com \
--allow-url https://custom-api.example.com \
--env-file .env \
-- autopilot --prompt-file task.md
What Gets Blocked¶
With --restrict-network, the firewall blocks:
- ❌ GitHub, PyPI, npm, Docker Hub
- ❌ General internet access (google.com, etc.)
- ❌ Data exfiltration attempts
- ❌ Backdoor downloads
Auto-Detected LLM Endpoints¶
The sandbox automatically detects and allows these endpoints based on your environment variables:
| Provider | Environment Variable | Endpoint Allowed |
|---|---|---|
| OpenAI | OPENAI_API_KEY |
https://api.openai.com |
| Anthropic | ANTHROPIC_API_KEY |
https://api.anthropic.com |
| Google AI | GEMINI_API_KEY |
https://generativelanguage.googleapis.com |
| AWS Bedrock | AWS_REGION (+ AWS_ACCESS_KEY_ID or AWS_PROFILE) |
Region-specific endpoint |
| Azure OpenAI | AZURE_OPENAI_RESOURCE |
https://<resource>.openai.azure.com |
| Groq | GROQ_API_KEY |
https://api.groq.com |
| Cohere | COHERE_API_KEY |
https://api.cohere.ai |
| Together | TOGETHER_API_KEY |
https://api.together.xyz |
| Replicate | REPLICATE_API_KEY |
https://api.replicate.com |
Custom endpoints are also detected from variables like:
- OPENAI_BASE_URL, OPENAI_API_BASE, OPENAI_ENDPOINT
- ANTHROPIC_BASE_URL, ANTHROPIC_API_BASE
- AWS_BEDROCK_ENDPOINT, AWS_ENDPOINT_URL_BEDROCK_RUNTIME
- OLLAMA_API_BASE, HOSTED_VLLM_API_BASE (local servers)
- Any *_BASE_URL, *_API_BASE, *_ENDPOINT pattern
Each endpoint is allowed on its own port: 443 for https://, 80 for http://, or the port in the URL (e.g., http://host.containers.internal:11434 allows port 11434).
Testing Network Restrictions¶
Validate firewall rules without running PatchPal:
This tests that: - Blocked sites (google.com, github.com) are inaccessible - Allowed LLM endpoints are accessible - Returns exit code 0 on success, 1 on failure
AWS Bedrock GovCloud Example¶
# .env.govcloud file:
# AWS_ACCESS_KEY_ID=...
# AWS_SECRET_ACCESS_KEY=...
# AWS_BEDROCK_REGION=us-gov-east-1
patchpal-sandbox \
--restrict-network \
--env-file .env.govcloud \
-- autopilot \
--model bedrock/arn:aws-us-gov:bedrock:us-gov-east-1::inference-profile/us-gov.anthropic.claude-sonnet-4-5-20250929-v1:0 \
--prompt-file task.md
The sandbox automatically detects us-gov-east-1 and allows https://bedrock-runtime.us-gov-east-1.amazonaws.com.
AWS Bedrock with SSO / ADFS¶
If your organization authenticates to AWS via SSO/ADFS (instead of static access keys), set up a named profile on the host and let the sandbox pick it up:
# One-time: create an SSO profile, then authenticate on the HOST
aws configure sso # e.g., profile name "Bedrock"
aws sso login --profile Bedrock # refresh when the ~8h session expires
export AWS_PROFILE=Bedrock
export AWS_REGION=us-gov-east-1
patchpal-sandbox \
--restrict-network \
-- autopilot \
--model bedrock/arn:aws-us-gov:bedrock:us-gov-east-1::inference-profile/us-gov.anthropic.claude-sonnet-4-5-20250929-v1:0 \
--prompt-file task.md
The sandbox automatically:
- Mounts your host
~/.awsdirectory (read-only) into the container, so boto3/LiteLLM can resolve the profile config (~/.aws/config) and the cached SSO token (~/.aws/sso/cache/). - Allowlists the Bedrock endpoints for the detected region when
AWS_PROFILEis set (not only when staticAWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEYare present).
Because the SSO token is short-lived, run aws sso login --profile Bedrock on the host
before launching the sandbox (and again when it expires). With --env-file (isolated mode),
remember to include AWS_PROFILE and AWS_REGION in the env file, since host environment
variables are otherwise excluded.
Security Considerations¶
⚠️ Container credentials: AWS credentials passed via --env-file are visible to the container. However:
- Exfiltration is still blocked by network restrictions
- Credentials don't persist after container exit
- Only affects that specific session
- .env file is not readable/accessible by agent.
For maximum security: - Use short-lived credentials (IAM roles, temporary tokens) - Use restrictive IAM policies (minimum required permissions) - Monitor AWS CloudTrail for unexpected API calls
Firewall tamper-resistance¶
The agent process runs as root inside the container. Setting up the iptables
firewall requires network-admin privileges (CAP_NET_ADMIN), and if the agent
retained that privilege it could simply flush the firewall (iptables -F OUTPUT)
and bypass --restrict-network.
To prevent this, the sandbox configures the firewall first (as root with the
required capability) and then permanently drops CAP_NET_ADMIN/CAP_NET_RAW
(via setpriv) before launching the agent. The capabilities are removed from the
inheritable and bounding sets so they cannot be re-raised. The firewall rules stay
in force for the whole session, but the agent can no longer modify netfilter — so
the network restrictions are enforced, not merely advisory.
Note on rootless Podman: granting
CAP_NET_ADMINto a rootless container is not always sufficient to run iptables, so the sandbox falls back to--privilegedfor the brief firewall-setup phase. Those elevated privileges are only held while the (sandbox-controlled) setup script runs; they are dropped before any agent code executes. On Docker/rootful Podman, only--cap-add=NET_ADMINis used.
Advanced Options¶
Container Runtime Selection¶
The container runtime is auto-detected — there is no flag to select it manually:
- Check if
podmanis available → use Podman (rootless) - Otherwise fall back to
docker
If you need to force a specific runtime, make only that runtime available on your
PATH (e.g. temporarily rename or remove the other).
Custom Container Image¶
# Use latest PyPI version (slower startup - requires pip install)
patchpal-sandbox --image python:3.11-slim -- --model anthropic/claude-sonnet-4-5
# Use specific patchpal version
patchpal-sandbox --image ghcr.io/amaiya/patchpal-sandbox:0.21.8 -- --model openai/gpt-4o
Default image: ghcr.io/amaiya/patchpal-sandbox:latest (pre-built, fast startup)
Additional Volume Mounts¶
# Mount additional directories
patchpal-sandbox \
-v /path/to/data:/data:ro \
-v /path/to/output:/output \
-- --model anthropic/claude-sonnet-4-5
Environment Variables¶
Environment variable behavior depends on whether --env-file is provided:
Without --env-file (uses host environment):
# Host environment variables (AWS_*, OPENAI_*, etc.) are passed through
patchpal-sandbox -- --model openai/gpt-4o
With --env-file (isolated mode):
# ONLY variables from .env file are used (better isolation)
# Host environment variables are NOT passed
patchpal-sandbox --env-file .env -- --model anthropic/claude-sonnet-4-5
Note: When using --env-file, host environment variables are intentionally excluded for better security isolation. This prevents accidental credential leakage from your shell environment.
Resource Limits¶
# Memory and CPU limits
patchpal-sandbox \
--memory 4g \
--cpus 2 \
-- autopilot --prompt-file task.md
Custom Tools¶
Custom tools work automatically in sandbox mode:
- Global tools:
~/.patchpal/tools/(auto-mounted) - Repository tools:
<repo>/.patchpal/tools/(in/workspace)
See Custom Tools for details on creating tools.
Troubleshooting¶
"docker: command not found"¶
Install Docker or Podman:
# Ubuntu/Debian
sudo apt-get install podman
# macOS
brew install podman
podman machine init
podman machine start
# RHEL/Fedora
sudo dnf install podman
"Permission denied" errors¶
Podman rootless (recommended): No sudo required, works out of the box.
Docker: Add your user to the docker group:
Firewall rules not working¶
If --restrict-network isn't blocking traffic:
-
Test restrictions explicitly:
-
Verify iptables inside container:
-
Check that Podman has necessary capabilities (automatically added by patchpal-sandbox)
Slow startup times¶
The default pre-built image should start in under 1 second after the initial download.
To pre-download or update the image:
docker pull ghcr.io/amaiya/patchpal-sandbox:latest
# or
podman pull ghcr.io/amaiya/patchpal-sandbox:latest
Note: The sandbox image is updated periodically with PatchPal releases.
Corporate proxy/firewall issues¶
If you're behind a corporate proxy:
# Pass proxy settings
patchpal-sandbox \
-e HTTP_PROXY=$HTTP_PROXY \
-e HTTPS_PROXY=$HTTPS_PROXY \
-e NO_PROXY=$NO_PROXY \
-- --model anthropic/claude-sonnet-4-5
SSL certificates are auto-mounted when present on the host:
- /etc/ssl/certs/ca-certificates.crt (Debian/Ubuntu system trust store)
- /usr/local/share/ca-certificates/ (additional custom CA certificates)
The sandbox container is Debian-based, so these Debian paths map directly into the container's trust store. On other distributions (e.g. RHEL/Fedora/SUSE), set a cert environment variable and the sandbox will mount it into the container and point Python/requests/curl at it automatically:
# RHEL/Fedora
export SSL_CERT_FILE=/etc/pki/tls/certs/ca-bundle.crt
# SUSE
export SSL_CERT_FILE=/etc/ssl/ca-bundle.pem
patchpal-sandbox -- --model anthropic/claude-sonnet-4-5
SSL_CERT_FILE, REQUESTS_CA_BUNDLE, and CURL_CA_BUNDLE are all honored this way.
For Windows Podman with corporate firewalls, see the Windows Setup section above.
Comparison with Manual Docker/Podman¶
Using patchpal-sandbox (Recommended)¶
✅ Automatic container setup ✅ Pre-built image (fast startup) ✅ Auto-detects runtime (Podman/Docker) ✅ Auto-mounts custom tools ✅ Auto-mounts SSL certs ✅ Network restriction support
Manual Docker Command¶
docker run -it --rm \
-v $(pwd):/workspace \
-v ~/.patchpal:/root/.patchpal \
-e ANTHROPIC_API_KEY=$ANTHROPIC_API_KEY \
--memory="2g" --cpus="2" \
ghcr.io/amaiya/patchpal-sandbox:latest \
bash -c "patchpal-autopilot --prompt-file task.md"
❌ More verbose ❌ Manual volume mounts ❌ Manual environment setup ❌ No auto-detection ❌ No network restrictions
Recommendation: Use patchpal-sandbox for simplicity and consistency.
Best Practices¶
Always Use Sandbox For¶
- ✅ Autopilot mode (REQUIRED for safety)
- ✅ Untrusted code/repositories
- ✅ Automated workflows
- ✅ Production environments
- ✅ Repositories with sensitive data
Consider Sandbox For¶
- ✅ General development (extra safety layer)
- ✅ Testing/experimentation
- ✅ Learning PatchPal
Optional Without Sandbox¶
- Direct use on trusted throwaway projects
- One-off interactive sessions on isolated VMs
- Cases where you need direct host access
Network Restriction Recommendations¶
| Scenario | Use --restrict-network? |
|---|---|
| Autopilot mode | ✅ YES (strongly recommended) |
| Untrusted code | ✅ YES |
| Production automation | ✅ YES |
| Personal projects | ⚠️ Optional (adds defense-in-depth) |
| Trusted code + human oversight | ⚠️ Optional |
| Local models only (Ollama) | ⚠️ Optional (see Local Models for which setups support it) |
Examples¶
Multi-Phase Development Workflow¶
# Phase 1: Core functionality with Claude
patchpal-sandbox --env-file .env -- autopilot \
--model anthropic/claude-sonnet-4-5 \
--prompt-file phase1-core.md
# Phase 2: Add tests with GPT-4o
patchpal-sandbox --env-file .env -- autopilot \
--model openai/gpt-4o \
--prompt-file phase2-tests.md
# Phase 3: Documentation with local model
patchpal-sandbox --host-network -- autopilot \
--model ollama_chat/qwen2.5-coder:32b \
--prompt-file phase3-docs.md
Secure Autopilot with Network Restrictions¶
# Maximum security: network isolation + autopilot
patchpal-sandbox \
--restrict-network \
--env-file .env \
--memory 4g \
-- autopilot \
--model anthropic/claude-sonnet-4-5 \
--prompt-file secure-task.md \
--max-iterations 20
Debugging Inside Container¶
# Start shell inside container with model pre-configured
patchpal-sandbox --env-file .env --shell -- --model groq/llama-3.3-70b-versatile
# Inside container
$ pwd
/workspace
$ ls -la ~/.patchpal/tools/
# Your custom tools are here
$ echo $PATCHPAL_MODEL
groq/llama-3.3-70b-versatile
$ patchpal
# Runs with the pre-configured model automatically
Tip: When you provide --model after --, it's automatically set as the PATCHPAL_MODEL environment variable.
Using Different Providers¶
# Google Gemini
patchpal-sandbox --env-file .env -- autopilot \
--model gemini/gemini-2.0-flash-exp \
--prompt-file task.md
# Groq (fast inference)
patchpal-sandbox --env-file .env -- autopilot \
--model groq/llama-3.3-70b-versatile \
--prompt-file task.md
# Azure OpenAI
patchpal-sandbox --env-file .env.azure -- autopilot \
--model azure/gpt-4o \
--prompt-file task.md
Learn More¶
- Autopilot Mode - Comprehensive autopilot documentation
- Configuration - Environment variables and settings
- Custom Tools - Creating custom tool integrations
- Safety - Security best practices
- Examples: Ralph - Real-world autopilot examples