Skip to main content

Cocoon 🛡️

CI Crates.io PyPI Python Versions License Platforms Security Audit

A featherweight, cross-platform sandbox with virtual and read-only filesystems designed for AI agents and secure code execution.

Cocoon allows AI agents, code evaluation suites, and developers to safely run untrusted scripts in Python, Node.js, Bun, Deno, Go, Java, Rust, PowerShell, Bash, Ruby, PHP, Lua, and any binary compiler without the latency or complexity of heavyweight containers or virtual machines.

👉 Installation Guide & Prerequisites | User & Agent Integration Guide


⚡ Features

  • Sub-Millisecond Startup: Runs directly on native operating system primitives (< 5ms) instead of spinning up virtual machines.
  • Ironclad Read-Only Protection: Mount host directories with --ro where writes, overwrites, appends, and deletes are physically blocked by kernel ACLs / namespaces.
  • Ephemeral Virtual Scratchpad: Automatically redirects temporary files, bytecode caches (__pycache__), npm caches, and cargo targets to an isolated in-memory/temp scratchpad.
  • Rich Built-in Runtime Presets: Instant support for python, node, bun, deno, go, java, rust / cargo, pwsh / powershell, bash / sh, ruby, php, and lua.
  • Universal Command Execution: Run any installed compiler or binary toolchain (gcc, clang, dotnet, zig, julia, etc.) by passing trailing arguments: cocoon run -- <cmd> [args...].
  • Process & Resource Caps: Hard limits on memory (RAM) and execution wall-clock timeouts.
  • Agent-First JSON Protocol: Emits structured output (--json) with exit codes, execution duration, and captured standard streams.

🏗️ Architecture & Isolation Model

                    ┌────────────────────────────┐
                    │    AI Agent / Host App     │
                    └─────────────┬──────────────┘
                                  │ (CLI / Rust / JSON)
                    ┌─────────────▼──────────────┐
                    │       Cocoon Engine        │
                    └─────────────┬──────────────┘
                                  │
         ┌────────────────────────┼────────────────────────┐
         │                        │                        │
┌────────▼─────────┐    ┌─────────▼────────┐    ┌──────────▼─────────┐
│   Linux Driver   │    │   macOS Driver   │    │   Windows Driver   │
├──────────────────┤    ├──────────────────┤    ├────────────────────┤
│ • User Namespaces│    │ • Apple Seatbelt │    │ • Job Objects      │
│ • ro-bind mounts │    │ • SBPL Profile   │    │ • Inherited Deny   │
│ • Landlock LSM   │    │ • Ephemeral /tmp │    │   NTFS ACEs        │
│ • tmpfs scratch  │    │                  │    │ • Scratchpad VFS   │
└──────────────────┘    └──────────────────┘    └────────────────────┘

📦 Installation

Install Cocoon using your preferred package manager:

# Via Python / PyPI (Pre-compiled native binary, zero Rust toolchain needed)
pip install cocoon-sandbox

# Via Rust / Cargo (From Crates.io)
cargo install cocoon-sandbox

🚀 Quick Start

1. Inspect System Capabilities

cocoon info

2. Run Python with Read-Only Mount

Mount your project directory as read-only and execute an inspection script:

cocoon run --ro ./my_project --runtime python -c "import os; print(os.listdir('.'))"

3. Run Node.js with Memory & Timeout Limits

cocoon run --ro ./data --runtime node -c "console.log('Processed:', 42 * 2)" --timeout 5 --mem 256

4. Run PowerShell in Cocoon

cocoon run --ro ./scripts --runtime pwsh -c "Get-ChildItem"

5. Dedicated Work Directory (Create & Manipulate Files)

Specify an explicit workspace folder where scripts have full read-write permissions to create, manipulate, and delete files:

cocoon run --work ./agent_workspace --runtime python -c "open('report.txt', 'w').write('Analyzed')"

6. Safe Copy & Staging (Manipulate Copies While Leaving Originals Untouched)

Mount sensitive files/folders as read-only (--ro), and use --copy to stage copies into the writable work folder. The sandboxed process can edit and transform the files without altering the originals:

cocoon run --ro ./repo --copy ./repo/config.json --work ./agent_workspace \
  --runtime python -c "import json; d = json.load(open('config.json')); d['active'] = True; json.dump(d, open('config.json', 'w'))"

7. Custom Temp Directory

Redirect TMP, TEMP, and runtime bytecode caches to a specific directory:

cocoon run --temp ./my_scratch --runtime python -c "import os; print(os.environ['TMP'])"

8. Structured JSON Output for AI Agents

Add --json to receive structured execution telemetry:

cocoon run --ro . --runtime python -c "print('Computed:', 2**8)" --json

Output:

{
  "exit_code": 0,
  "stdout": "Computed: 256\n",
  "stderr": "",
  "duration_ms": 184.2,
  "timed_out": false,
  "oom_killed": false
}

9. Multi-Language Polyglot Pipelines (cocoon pipeline)

Run sequential steps across different programming languages in the exact same virtual filesystem. Steps share the workspace and pass files between each other:

cocoon pipeline \
  -s "python:import json; json.dump({'agent':'Cocoon','score':99}, open('data.json','w'))" \
  -s "node:const fs=require('fs'); const d=JSON.parse(fs.readFileSync('data.json')); d.score+=1; fs.writeFileSync('data.json', JSON.stringify(d));" \
  -s "bun:const d=await Bun.file('data.json').json(); console.log('Bun verified score:', d.score);" \
  -s "pwsh:Get-Content data.json"

You can also pass --json to cocoon pipeline to get a structured execution breakdown per step:

cocoon pipeline \
  -s "python:open('val.txt','w').write('42')" \
  -s "bun:console.log('Value read by Bun:', (await Bun.file('val.txt').text()).trim())" \
  --json

🔒 Security Guarantees & Built-in Invariants

  1. Read-Only by Default (--ro .):
    • Even if you omit --ro, the current working directory is automatically protected as strictly read-only.
    • Opening files in write mode ('w', 'a') or attempting deletions triggers immediate PermissionError / EPERM enforced at the OS kernel level.
  2. Zero Host Writes (Workdir Isolation):
    • Sandboxed processes can never write to anything other than the designated --work folder (or the auto-cleaned ephemeral scratchpad).
    • Use --copy <path> to stage copies of host files into the work folder and manipulate them freely without touching originals.
  3. 30-Second Execution Timeout by Default:
    • Every execution is automatically capped at 30 seconds by default (override with --timeout <secs>). Runaway scripts or infinite loops auto-terminate cleanly without hanging.
  4. No Bytecode or Cache Pollution:
    • Python bytecode caching (PYTHONPYCACHEPREFIX), pip caches (PIP_CACHE_DIR), npm caches (npm_config_cache), and cargo build artifacts are redirected to an ephemeral scratchpad that is destroyed upon exit.

📦 Project Workspace


📄 License

This project is licensed under the MIT License.

Metadata

Release files for cocoon-sandbox 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cocoon-sandbox 0.1.0
File Size Uploaded Provenance Actions
cocoon_sandbox-0.1.0.tar.gz 28.0 kB Without provenance Details for cocoon_sandbox-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for cocoon-sandbox 0.1.0
File Actions
cocoon_sandbox-0.1.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for cocoon_sandbox-0.1.0-py3-none-win_amd64.whl
cocoon_sandbox-0.1.0-py3-none-manylinux_2_39_x86_64.whl Python 3 none Linux glibc 2.39+ x86-64 Details for cocoon_sandbox-0.1.0-py3-none-manylinux_2_39_x86_64.whl
cocoon_sandbox-0.1.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64 Details for cocoon_sandbox-0.1.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
cocoon_sandbox-0.1.0-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details for cocoon_sandbox-0.1.0-py3-none-macosx_11_0_arm64.whl
cocoon_sandbox-0.1.0-py3-none-macosx_10_12_x86_64.whl Python 3 none macOS 10.12+ x86-64 Details for cocoon_sandbox-0.1.0-py3-none-macosx_10_12_x86_64.whl

Total release size: 4.0 MB

Release files / cocoon_sandbox-0.1.0.tar.gz

Download URL cocoon_sandbox-0.1.0.tar.gz
Size 28.0 kB
Tags Source
SHA-256 checksum
How to use checksums
6503dfeb183e9f23e61df608dc424acbde224655efc399e2848fc20b7acaad9d
BLAKE2b-256 checksum
How to use checksums
ca2588c5b89642e5d2869c958da90d221a6315e781421e1798f0fa856fc3ac24
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / cocoon_sandbox-0.1.0-py3-none-win_amd64.whl

Download URL cocoon_sandbox-0.1.0-py3-none-win_amd64.whl
Size 812.7 kB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
bf1679624c9409c398eafe52e23b27d4a0e354c6381d6f3f7c047a57108af7f6
BLAKE2b-256 checksum
How to use checksums
25a48705b9bdd698059c7987a0372037014e70632b74c7b2d660c4feeeacc730
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / cocoon_sandbox-0.1.0-py3-none-manylinux_2_39_x86_64.whl

Download URL cocoon_sandbox-0.1.0-py3-none-manylinux_2_39_x86_64.whl
Size 832.6 kB
Tags Linux glibc 2.39+ x86-64 Python 3
SHA-256 checksum
How to use checksums
995e21f5742d0ca6fda04012f8ca200b0dc07a134404c642477f17f6f4d15b57
BLAKE2b-256 checksum
How to use checksums
2e66f4e3bd0611e8d920590e08a9ac35b87efff636fc251eba71b928c8972713
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / cocoon_sandbox-0.1.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL cocoon_sandbox-0.1.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 785.6 kB
Tags Linux glibc 2.17+ ARM64 Python 3
SHA-256 checksum
How to use checksums
8396c3c7ac2133f13e6371f407041ecdac2287668d3c1915e423b4399dcac397
BLAKE2b-256 checksum
How to use checksums
0a711dfe534580eeae0f26e3dc389e3bdf21aef76b506e419910c72fced22370
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / cocoon_sandbox-0.1.0-py3-none-macosx_11_0_arm64.whl

Download URL cocoon_sandbox-0.1.0-py3-none-macosx_11_0_arm64.whl
Size 759.2 kB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
64de14117723bf51defb73ec53f87134119a138bec5f703639cc1df4be4879ca
BLAKE2b-256 checksum
How to use checksums
d7d43912ec62712ea0de8360a4d56a535ead66fc15aa4cbe6b6c8c5085a3438a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / cocoon_sandbox-0.1.0-py3-none-macosx_10_12_x86_64.whl

Download URL cocoon_sandbox-0.1.0-py3-none-macosx_10_12_x86_64.whl
Size 800.0 kB
Tags Python 3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
c5c2be8860474f3f98291468e5fb3666db13c498ad16d3041d0d400db70584c3
BLAKE2b-256 checksum
How to use checksums
f2165bb27e01871e43aeb3716ee2e91101c39df382d0240a2d56ffe9de8de409
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for cocoon-sandbox 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page