# Code interpreters

A language context is a supervised process inside a native container or supported process sandbox. Its variables
survive calls. The interpreter templates automatically start warming the default context at agent startup;
`box.python()` or `box.javascript()` reuses it and waits for its Jupyter-ready handshake. HTTP health stays responsive while scientific imports warm. Named contexts are independent. The process
appears in `box.processes()` and its id/PID appears in `box.kernels()`.

```sh
hermitbox template create python-interpreter
hermitbox template create node-interpreter
hermitbox sandbox python -c 'print(2+2)'
```

The CLI without an id creates and deletes a sandbox from the warmed Python template. With an
id it attaches to that sandbox. Without `-c` it keeps a multiline REPL; Jupyter's `is_complete`
reply decides when to submit input. Ctrl-C interrupts the kernel.

### Start a context

```ts
// A sandbox from the warmed Python template.
const box = await hb.sandbox({ template: 'python-interpreter', isolation: 'container' })

// The default Python context, already warming.
const py = await box.python()
```

### Run code

```ts
await box.write('sales.csv', 'sales\n10\n20\n')

// Variables live on between calls.
const summary = await py.run(`
import pandas as pd
df = pd.read_csv('sales.csv')
df.describe()
`)
console.log(summary.text)
```

`summary.html` and `summary.json` hold the other representations.

### Save a chart

```ts
import { writeFile } from 'node:fs/promises'

// Plots come back as PNG bytes.
const chart = await py.run(`
import matplotlib.pyplot as plt
plt.plot([1, 2, 3])
plt.show()
`)
await writeFile('chart.png', chart.png!)
```

### Stream output

```ts
// Events arrive while the code runs.
for await (const event of py.stream('for i in range(3): print(i)')) {
  console.log(event)
}
```

### Use another context

```ts
// A named context keeps its own variables.
const analysis = await box.python({ context: 'analysis' })
await analysis.close()
```

### Interrupt, restart and close

```ts
await py.interrupt()
await py.restart()
await py.close()

await box.delete()
```

Python has the same handles in synchronous and asynchronous client contexts. `result.png` is
`bytes`; TypeScript returns `Uint8Array`. Python `kernel.run` in an asynchronous context starts
an eager task, so an interrupt can be issued while awaiting that task. `stream` yields events
as they arrive. Every MIME bundle and its metadata remains available in `result.results` and
`result.events`; the convenience views search the last representation of each MIME type.
`stdout`, `stderr` and structured `error = {name, value, traceback}` remain separate.

## Agent contract

The [Rust-owned agent document](https://github.com/hermitbox/hermitbox/blob/f9b5d4aa3cc4636cd916d6b84e56d6ba38b271a7/agent/openapi.json) advertises `kernelsVersion: 1` on health,
independently of agent API version 2. Public control and node protocols are unchanged.

| Request under `/_hermitbox/kernels` | Effect |
| --- | --- |
| `POST {language: "python" or "javascript", context?: string}` | Start/reuse a context; return id, language, context, processId, pid, state. |
| `GET` | List contexts, including dead contexts. |
| `POST /{id}/execute {code, timeout?: seconds}` | NDJSON, one event per line, ending in `done`. |
| `POST /{id}/interrupt` | Signal the supervised kernel group independently of its execution queue. |
| `POST /{id}/restart` | Stop and reap the old kernel, then start a fresh process with the same context id. |
| `POST /{id}/complete {code}` | Jupyter input-completeness reply, for the REPL. |
| `DELETE /{id}` | Stop the context and remove it; idempotent after removal. |

| Event `type` | Other fields |
| --- | --- |
| `stream` | `stream: stdout or stderr`, `text` |
| `result` | `data` (all MIME representations), `metadata`, `executionCount` |
| `display` | `data`, `metadata`, nullable `displayId`, `update` |
| `error` | `name`, `value`, `traceback: string[]` |
| `done` | Nullable `executionCount`, `status` (`ok`, `error`, or an explicit transport/timeout state). |

Language errors are result data. Missing completion or a broken transport is an unknown outcome;
neither SDK retries code. A dead context stays dead until explicitly restarted. Executions are
serialized per context; interrupt/close do not wait on the execution mutex. Queues and capture
are bounded, timeouts enforce capability limits, and timeout kills the kernel instead of
allowing its code to continue silently. The default is 30 seconds, or the token's lower ceiling
when a project sets one; the SDKs send a timeout only when the caller names it, so the agent's
default applies otherwise. Each sandbox holds at most 16 contexts.
MCP tools `run_python` and `run_javascript` use these same owners and return image MIME output
as MCP image content, alongside text events.

Connection files are mode 0600 in a private temporary directory outside persistent data.
Container Jupyter channels use a random HMAC key on sandbox-local loopback. Process kernels
use abstract Unix sockets inside the supervisor's enforced Landlock ABI 6 scope; they add no
TCP listener. A plain pathname socket is insufficient for shared-UID isolation. The owner's
connection succeeds while a separate same-UID agent cannot connect. Fresh signed scope proof from every managed peer is required; unverified
legacy peers or unknown process history keep native tools off with a reason.

The agent launches kernel executables directly, never a `jupyter` command. Container images
run as the sandbox user with existing resource limits. Process kernels lazily fetch the
[verified upstream bundle](https://docs.hermitbox.dev/v0.2.0/native-tools.md#process-tools), using its unchanged Python/Node
builds, bundle-owned libraries and host glibc >=2.34. They currently refuse `network: none`
because private channels cannot yet be prebound under that policy; ordinary egress denial
remains enforced. Wasm refuses native contexts. CI runs both installed kernels through
the TypeScript and sync/async Python SDKs, including state, charts, errors, interrupt and
restart. Startup measurements are in [speed.md](https://docs.hermitbox.dev/v0.2.0/speed.md).
Snapshots preserve data, not live Python variables; waking a stopped sandbox starts a fresh
kernel. Live state exists only while that kernel survives.

## Versions and licences

| Template/tool | Pinned version | Licence |
| --- | --- | --- |
| Python image | CPython 3.12.14, Debian bookworm | PSF-2.0 and historical grants; Debian package notices retained. |
| ipykernel | 7.4.0 | BSD-3-Clause |
| NumPy | 2.5.4 | BSD-3-Clause AND 0BSD AND MIT AND Zlib AND CC0-1.0; wheel notices include numerical runtimes. |
| pandas | 3.1.1 | BSD-3-Clause |
| matplotlib | 3.11.2 | Matplotlib permissive agreement and PSF-2.0; this is not Mozilla MPL. |
| SciPy | 1.18.1 | BSD-3-Clause and bundled numerical-library notices. |
| scikit-learn | 1.9.1 | BSD-3-Clause |
| requests | 2.34.2 | Apache-2.0 |
| Pillow | 13.0.0 | MIT-CMU |
| Node image | Node 24.22.1, Debian bookworm | MIT and bundled notices; Debian package notices retained. |
| IJavascript / jp-kernel / NEL | 5.2.1 / 2.0.0 / 1.3.0 | BSD-3-Clause |
| Chart.js / napi-rs canvas | 4.6.1 / 1.1.0 | MIT; bundled Skia/Chromium BSD-3-Clause notices retained. |
| ZeroMQ.js | 6.8.0 | MIT AND MPL-2.0, official v5 compatibility API for jmp. |
| Rust jupyter-zmq-client / jupyter-protocol | 3.0.1 / 2.0.3 | BSD-3-Clause |
| Rust zeromq | 0.7.0 | MIT |

The [Python](https://github.com/hermitbox/hermitbox/blob/f9b5d4aa3cc4636cd916d6b84e56d6ba38b271a7/templates/python-interpreter/README.md) and
[Node](https://github.com/hermitbox/hermitbox/blob/f9b5d4aa3cc4636cd916d6b84e56d6ba38b271a7/templates/node-interpreter/README.md) image descriptions list layer notices and source
availability. The resolved [Python](https://github.com/hermitbox/hermitbox/blob/f9b5d4aa3cc4636cd916d6b84e56d6ba38b271a7/docs/licences/python-interpreter.json),
[Node](https://github.com/hermitbox/hermitbox/blob/f9b5d4aa3cc4636cd916d6b84e56d6ba38b271a7/docs/licences/node-interpreter.json) and [base-package](https://github.com/hermitbox/hermitbox/blob/f9b5d4aa3cc4636cd916d6b84e56d6ba38b271a7/docs/licences/interpreter-base.json)
inventories record every version and licence; Rust dependency licences are enforced by
cargo-deny ([deny.toml](https://github.com/hermitbox/hermitbox/blob/f9b5d4aa3cc4636cd916d6b84e56d6ba38b271a7/deny.toml)). SHA-256 wheel hashes, npm integrity hashes and base manifest digests are pinned.
The Python lock targets Linux x86_64 / CPython 3.12, matching release builds.

Jupyter messaging 5.5 was read at jupyter_client 8.10.0 commit
`978361b3785dcd9cba6c733f4555e833e88fc0df`; the adapter uses the upstream Rust client.
IJavascript 5.2.1 is pinned to published commit `7011c92ebcb858d1aca4edca3adb4e0ea64db174`.
Neither evaluated JavaScript kernel has a recent core release. TSLab 1.0.22 is not used because
it emits values and errors as console text. IJavascript provides Jupyter rich
results and errors; its legacy ZeroMQ dependency is routed to the current official compatibility
API without copying upstream code or implementing a wire server.

Python preloads NumPy, pandas and pyplot, renders inline PNG/SVG with Agg, and registers JSON
representations for scalar values and DataFrame rows. Other installed scientific packages load
on demand. Measurements and their limits are in [speed.md](https://docs.hermitbox.dev/v0.2.0/speed.md). The
[acceptance suite](https://github.com/hermitbox/hermitbox/blob/f9b5d4aa3cc4636cd916d6b84e56d6ba38b271a7/control/e2e/interpreter.e2e.test.ts) covers both templates, both SDK modes,
state, named contexts, PNG, traceback, interrupt, restart, dead processes, MCP and the CLI.
