Metadata-Version: 2.5
Name: kern-sandbox
Version: 0.4.1
Summary: Your model writes the code. This runs it where it can't touch your machine: a rootless Linux container, no daemon, no cloud, no account. A kernel boundary, not a microVM: for deliberately hostile code, use one.
Project-URL: Homepage, https://getkern.dev
Project-URL: Repository, https://github.com/getkern/kern
Project-URL: Source, https://github.com/getkern/kern/tree/main/bindings/python
Author: getkern
License: Apache-2.0
Keywords: agent,ci,code-execution,code-interpreter,e2b,edge,isolation,jupyter,llm,namespaces,sandbox,seccomp
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Security
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.9
Provides-Extra: langchain
Requires-Dist: langchain-core>=0.3; extra == 'langchain'
Provides-Extra: langchain-shell
Requires-Dist: langchain>=1.3; extra == 'langchain-shell'
Description-Content-Type: text/markdown

<div align="center">

<img src="https://raw.githubusercontent.com/getkern/kern/main/assets/brand/kern-logo.png" width="220" alt="kern">

# Kern Sandbox

**Your model writes the code. This runs it where it can't reach your files unless you hand them
over.**

<sub>**Works with** Claude Code · Cursor · Claude Desktop · LM Studio · LangChain · pi</sub>

[![CI](https://github.com/getkern/kern/actions/workflows/ci.yml/badge.svg)](https://github.com/getkern/kern/actions/workflows/ci.yml)
[![License: Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://github.com/getkern/kern/blob/main/LICENSE)
[![PyPI](https://img.shields.io/pypi/v/kern-sandbox?label=pypi%20kern-sandbox&color=blue)](https://pypi.org/project/kern-sandbox/)
[![npm](https://img.shields.io/npm/v/kern-sandbox?label=npm%20kern-sandbox&color=blue)](https://www.npmjs.com/package/kern-sandbox)

<sub>rootless · no daemon · no socket · no cloud · no account</sub>

**[The runtime](https://github.com/getkern/kern)** ·
**[MCP server](https://github.com/getkern/kern/blob/main/docs/MCP.md)** ·
**[Security model](https://github.com/getkern/kern/blob/main/SECURITY.md)** ·
**[Benchmarks](https://github.com/getkern/kern/blob/main/docs/BENCHMARKS.md)**

</div>

An agent's tool-call, a generated snippet, a notebook cell, a CI step: code nobody has read, run in
a container of its own.

```bash
# if the venv line fails: sudo apt install python3-venv
python3 -m venv .venv && . .venv/bin/activate
pip install -U kern-sandbox   # on Linux x86_64 and aarch64, this brings kern with it
```

On a Mac, install kern first with the line in
[docs/INSTALL.md](https://github.com/getkern/kern/blob/main/docs/INSTALL.md#macos) (it sets kern up in a small
Linux VM); then this package runs on the Mac itself and finds `kern` in `~/.local/bin`, even from an app
started from the Dock.

```python
import kern_sandbox as kern

r = kern.run_code("print(sum(range(100)))")
print(r.stdout, r.fault)   # 4950  None
```

That ran in a fresh container from an OCI image, with **no network**, memory and PID caps and a
deadline set from outside, and the container was thrown away before the call returned.

- **Cheap enough for every call**: a hundred calls are a hundred containers, nothing left behind.
- **State when you want it**: a `Sandbox()` you keep open shares `/workspace`; `kernel()` keeps one
  warm interpreter where variables carry too.
- **Imports compiled once**: the image's standard library is precompiled the first time you use it
  and mounted READ-ONLY into later boxes, so `import json, re` is about 3x cheaper.
  `pyc_cache=False` turns it off.
- **One install**: the Linux wheels carry the `kern` binary, so the venv has `kern doctor` and
  `kern ps` too.

A hallucinated `rm -rf ~` hits the container's own `/root`, mounted read-only. A step that hangs or
runs out of memory comes back as a value your loop can branch on.

## The basics, one call each

Each block starts from `import kern_sandbox as kern` and prints what the comment says.

<!-- tabs -->

<!-- tab: files.py -->

**Give it a file, get a file back.** A `Sandbox()` has a `/workspace` you and the code both see.

```python
job = """
import csv
rows = list(csv.DictReader(open("in.csv")))
hottest = max(rows, key=lambda row: int(row["temp"]))
open("out.txt", "w").write(hottest["city"])
"""
with kern.Sandbox() as sb:
    sb.write_file("in.csv", "city,temp\nRome,24\nOslo,9\n")
    sb.run_code(job)
    print(sb.read_file("out.txt"))   # b'Rome'
```

<!-- tab: state.py -->

**Keep variables between steps.** `kernel()` is one warm interpreter, like a notebook.

```python
with kern.Sandbox() as sb, sb.kernel() as k:
    k.run_code("x = 40")
    r = k.run_code("print(x + 2)")
    print(r.stdout)   # 42
```

<!-- tab: package.py -->

**Install a package first.** `setup=` runs once, with network; your code still runs without.

```python
with kern.Sandbox(setup="pip install numpy") as sb:
    r = sb.run_code("import numpy as np; print(np.pi)")
    print(r.stdout)   # 3.141592653589793
```

<!-- tab: network.py -->

**Let it reach one host.** No network by default. `egress_allow` opens the hosts you name and
nothing else; `network=True` opens everything, your own loopback included. A key the code needs and
must not hold goes in `Sandbox(egress_secrets=[...])`: kern adds it to the requests on the way out,
and it is never in the box ([EGRESS.md](https://github.com/getkern/kern/blob/main/docs/EGRESS.md)).

```python
code = ("import urllib.request as u; "
        "print(u.urlopen('https://pypi.org').status)")
r = kern.run_code(code)
print(r.exit_code)   # 1: no network
r = kern.run_code(code, egress_allow=["pypi.org"])
print(r.stdout)      # 200
```

<!-- tab: shell.py -->

**Run a shell command.**

```python
r = kern.run_code("uname -s && python3 --version", language="bash")
print(r.stdout)   # Linux, then the image's Python 3.12
```

<!-- tab: chart.py -->

**Get a chart back.** A matplotlib figure open when the code ends comes back as a PNG on
`r.results`; `fig` or `display(fig)` draws one the code holds. In a `kernel()` a figure is closed
once its cell returned it, as in Jupyter.

```python
plot = """
import matplotlib.pyplot as plt
plt.plot([1, 4, 9, 16])
plt.show()
"""
with kern.Sandbox(setup="pip install matplotlib") as sb:
    r = sb.run_code(plot)
    open("chart.png", "wb").write(r.results[0].png)
```

<!-- /tabs -->

## The result says who stopped the run

Exit 137 alone does not say whether it was your timeout or the OOM killer. `fault` does. Every row
was run:

| the code | `fault.type` | `exit_code` |
|---|---|---|
| `print(sum(range(100)))` | `None` | 0 |
| `while True: pass`, `timeout_s=3` | **`timeout`** | 137 |
| `bytearray(400<<20)`, `memory_mb=128` | **`oom`** | 137 |
| `urlopen(...)`, network off | `None` | 1 |
| `print(1)` on `alpine:3.19`, no python3 | **`exec_failed`** | 127 |
| `print(1)` on an image that is not there | **`startup_failed`** | 1 |

In the fourth row the **code** raised and the sandbox did nothing. `fault` is read from a pipe kern
writes, not from stdout, so code that prints `[exit 0]` can't fake it. Also `killed`,
`workspace_full` and `escape_blocked`. These values and exit codes are stable: one changes only to
correct an outcome reported wrong, and that release's
[CHANGELOG](https://github.com/getkern/kern/blob/main/CHANGELOG.md) entry says so first.

**What comes back, and what raises.** A run returns a result whenever there is something to report
about it, `fault` included. Three things raise instead, each a `SandboxError`, because no code ran:

- a mount refused before any box starts, by this package or by kern (`MountRefused`);
- a box that kern began and that failed to start (kern's exit 125);
- an argument the sandbox cannot take (`persist=True` without `name=`, a bad `tmpfs` size).

kern refusing an image or a GPU before any box exists comes back as `startup_failed` in `fault`,
with kern's reason in `stderr`.

## Works with

- **Any MCP client**: Claude Code, Cursor, Claude Desktop, LM Studio, Zed. The package ships
  `kern-mcp`, a stdio server; charts come back as images the model can see. One session backs the
  connection, so files carry between tool calls and variables do not, unless `KERN_MCP_KERNEL=1`:
  [docs/MCP.md](https://github.com/getkern/kern/blob/main/docs/MCP.md).
- **LangChain**: `kern_code_tool()` is a `StructuredTool`, and a fault comes back labelled for the
  model. Shell policy:
  [LANGCHAIN-SHELL.md](https://github.com/getkern/kern/blob/main/bindings/python/LANGCHAIN-SHELL.md).
- **[pi](https://github.com/earendil-works/pi)**: [`kern-pi`](https://www.npmjs.com/package/kern-pi)
  routes its file and shell tools into a box, your working directory at `/workspace`.
- **Python and Node**: the same API, `pip install kern-sandbox` and
  [`npm i kern-sandbox`](https://www.npmjs.com/package/kern-sandbox).

```json
{
  "mcpServers": {
    "kern": { "command": "uvx", "args": ["--from", "kern-sandbox", "kern-mcp"] }
  }
}
```

A client spawns the server from **its own** PATH, so it cannot see a venv: `uvx` fetches the
package, kern included on Linux, or use `pipx install kern-sandbox`. On a Mac with kern installed the
same line works; from Windows, swap the command for `wsl` or `ssh`.

## Safe by default

A bare `Sandbox()` runs `python:3.12-slim` with a 30 second deadline, no network, no host mounts,
seccomp on and capabilities dropped. The timeout is **mandatory**: no value disables it. Every
relaxation is a named argument.

- **A compromised dependency** is stopped by the filesystem and the network. **A prompt-injected
  agent** runs the code you asked for, so the defence is that credentials are never in the box.
- **Mounts over sensitive sources are refused, with no opt-out**: the host's own directories, kern's
  own state, credential directories (`.ssh`, `.aws`, `.kube`, `.gnupg` and the rest) and any parent
  of them. Mount a copy. A refused mount raises `MountRefused`.
- **Each call's code and output are kept** for `kern sandbox show` and `kern sandbox history`, until
  reboot by default; `Sandbox(transcripts=False)` keeps none.
- **`network=True` includes the host's loopback**, where unauthenticated services live: a test read
  the host's SSH banner off `127.0.0.1:22`. `egress_allow` is the route-level middle setting.
- **No network means no interface, not no channel**: a mounted Unix socket of a process with network
  reaches through. Mount only what the code may use.

`gpus=` hands the box NVIDIA cards: `gpus="all"`, `gpus="2"` (how many), `gpus="device=0,1"` or a
card's UUID. The host's driver libraries come with them, so any image works. A granted card is
**not** a boundary against the code in the box; a card you did not ask for is not there at all.

## How fast

Most of a call's cost is CPython starting inside the box, not the box. A
[precompiled image](https://github.com/getkern/kern/tree/main/examples/precompiled-image) removes
the stock image's compile step, and a prewarm pool serves calls from boxes already started. Method
and numbers: [BENCHMARKS.md](https://github.com/getkern/kern/blob/main/docs/BENCHMARKS.md).

## Current limitations

- **Not a boundary against deliberately hostile code.** Namespaces, cgroups and seccomp are for your
  own or semi-trusted code. For hostile or someone else's code, use a microVM or gVisor:
  [SECURITY.md](https://github.com/getkern/kern/blob/main/SECURITY.md).
- **Caps bind only where your host delegates a cgroup.** `kern doctor` says whether yours does;
  `require_limits=True` refuses to start rather than run uncapped.
- **The workspace is bounded only if you ask.** It is a host directory, so by default a job can fill
  your disk; `workspace_max_bytes` stops the box with a `workspace_full` fault.
- **A non-root `user=` needs a filesystem with ACLs** and a host range in `/etc/subuid`.
- **Not inside a container without `--privileged`, and not on Google Colab**: a box needs to mount a
  fresh `/proc`, which a container's masked one does not allow.
- **The binary ships with the package only on Linux x86_64 and aarch64.** Elsewhere pip picks the
  wheel without it, and the package drives a `kern` on `PATH` or in `$KERN_BIN`, installed with the
  [install script](https://github.com/getkern/kern/blob/main/docs/INSTALL.md). `$KERN_BIN` always
  wins over the bundled one.

## More

The full API, mime-typed results, snapshots and the measured sharp edges:
[SANDBOX-NOTES.md](https://github.com/getkern/kern/blob/main/bindings/python/SANDBOX-NOTES.md).

Linux with unprivileged user namespaces and cgroup v2, Python 3.9+. Windows through WSL2; on a Mac,
after installing kern, the package runs on the Mac itself:
[install notes](https://github.com/getkern/kern/blob/main/docs/INSTALL.md). Apache-2.0.
