Skip to content

Sandboxed Execution

megasthenes can execute all repository operations inside an isolated sandbox, providing multiple layers of security.

Architecture

Sandbox architecture diagram

Enabling Sandbox Mode

const client = new Client({
provider: "openrouter",
model: "anthropic/claude-sonnet-4-20250514",
sandbox: {
baseUrl: "http://localhost:8080",
},
});

When sandbox mode is enabled:

  • Repository cloning happens inside the sandbox container
  • All tool execution (file reads, searches, git operations) runs in isolation
  • The host filesystem is never accessed directly

Security Layers

LayerMechanismPurpose
ContainerPodman/DockerProcess and filesystem isolation
Filesystembubblewrap (bwrap)Read-only bind mounts, no network
SyscallseccompRestricts allowed system calls
ProcessNamespace isolationSeparate PID/network/mount namespaces

Running the Sandbox Server

Generate a docker-compose file and start the sandbox:

Terminal window
# Generate docker-compose.sandbox.yml
bunx megasthenes setup-sandbox
# Start the sandbox
docker-compose -f docker-compose.sandbox.yml up -d

Prerequisites: Docker with gVisor runtime.

The setup-sandbox command accepts configuration flags:

Terminal window
bunx megasthenes setup-sandbox \
--port 9090 \
--generate-secret \
--output ./docker-compose.sandbox.yml
FlagDefaultDescription
--port8080Host port to expose
--secret(none)Bearer token for API authentication
--generate-secretGenerate a random 32-char hex secret
--output./docker-compose.sandbox.ymlOutput file path
--image-tagLibrary versionContainer image tag

Resetting the Sandbox

To clean up all cloned repositories:

await client.resetSandbox();