capsulerun/runtime

Secure runtime to sandbox AI agent tasks. Run untrusted code in isolated WebAssembly environments.

What it solves

Running untrusted code safely. The problem: you want to execute code from external sources (like user-provided scripts or AI-generated snippets) without risking your host system. Capsule isolates each task in its own WebAssembly sandbox, so failures or malicious actions are contained and cannot cascade to the rest of your workflow.

How it works

You mark functions with the @task decorator (Python) or the task() wrapper (TypeScript) to define a sandboxed task. When you run capsule run main.py or main.ts, your code is compiled into a WebAssembly module and executed in isolated sandboxes. Each task runs with configurable resource limits: CPU (via WebAssembly fuel metering), memory, timeout, and automatic retries. You can control file access via allowed_files, network access via allowed_hosts, and environment variables via env_variables. Tasks return a structured JSON envelope containing the result and execution metrics like duration, fuel consumed, and peak memory. You can also invoke tasks programmatically using the run() function from your own code, or use the CLI with --mount to map host directories into the sandbox. For production, you can precompile tasks to .wasm files using capsule build to skip compilation latency.

Who it's for

Developers who need to execute untrusted code in a safe, isolated environment - for example, user-submitted scripts, AI-generated code, or plugin systems. It's also suitable for anyone who wants per-task resource limits, automatic retries, and detailed execution tracking without managing VMs or containers. The project supports both Python and TypeScript/JavaScript, making it accessible to a wide range of developers.

Highlights

  • Each task runs in its own WebAssembly sandbox, fully isolated from the host system
  • Configurable CPU (LOW, MEDIUM, HIGH, or custom fuel), RAM, timeout, and retry policies per task
  • Automatic retries handle transient failures without manual intervention
  • Lifecycle tracking shows which tasks are running, completed, or failed
  • Tasks can access specific host files (with read-only or read-write modes), domains, and environment variables via allowlists
  • The run() function lets you execute tasks from your own Python or TypeScript code
  • capsule build precompiles tasks to .wasm for low-latency production execution
  • Caching speeds up repeated runs - the .capsule folder stores compiled artifacts

Related

  • Project
  • Project
  • Project
  • Project