# Sandboxes

A sandbox is an isolated place to run commands, keep files and serve ports. Each one has its own
data directory, network policy and limits, and runs on one isolation tier for its whole life.

```ts
import { Hermitbox } from 'hermitbox'

const hb = new Hermitbox()

// Create a sandbox on the host's default tier.
const box = await hb.sandbox()

// Run a command in it.
const result = await box.run('echo hello')
console.log(result.stdout) // hello

// Delete it when you're done.
await box.delete()
```

```python
from hermitbox import Hermitbox

hb = Hermitbox()

# Create a sandbox on the host's default tier.
box = hb.sandbox()

# Run a command in it.
result = box.run('echo hello')
print(result.stdout)  # hello

# Delete it when you're done.
box.delete()
```

| Part | What it is |
| --- | --- |
| Commands | One-shot commands, streams, background processes, shells and terminals |
| Files | A persistent data directory; file paths are relative to it |
| Ports | Declared ports and spare ports, reached through the edge |
| Network | `internet` (the default) or `none`, plus egress rules where the node enforces them |
| Limits | Units of memory and CPU, timers and project quotas |
| Tier | `process`, `wasm`, `container`, `gvisor` or `vm`, fixed at creation |

## Run commands

Every sandbox runs a Hermitbox agent that starts commands and serves files. `box.run` waits for the
result, `box.stream` yields output as it arrives, and `box.spawn` starts a background process.

A string command runs with `sh -c`. An argument array, such as `['cat', 'app.py']`, skips the
shell. Commands start in the data directory unless `cwd` or the sandbox's `workdir` says otherwise.

Each command carries one idempotency key, and Hermitbox never retries it on its own. A stream that
ends without an exit has an unknown outcome: inspect the sandbox before you run it again.

## Files and what persists

The data directory is the part of a sandbox that lasts. Stop, sleep, rest and moves keep it, and
snapshots and forks copy it. On the `container`, `gvisor` and `vm` tiers it is mounted at `/data`.
On the `process` tier it is a host directory: commands start there, and `HOME` and `TMPDIR` point
to it.

```ts
// Write a file into the data directory, then read it back.
await box.write('notes/today.txt', 'hello')
console.log(await box.read('notes/today.txt')) // hello
```

```python
# Write a file into the data directory, then read it back.
box.write('notes/today.txt', 'hello')
print(box.read('notes/today.txt'))  # hello
```

Treat everything outside the data directory as temporary. By default a container's root
filesystem is read-only and its `/tmp` lives in memory. File operations work while the sandbox is
stopped or sleeping too: the node serves them without starting compute.

## Ports and endpoints

A sandbox can declare ports at creation, each `public` or `private`. Public ports get an address on
the edge, and private ones are reachable only on the fleet's private network. A sandbox also has
three spare ports by default, which `box.expose(port)` publishes on demand and returns as a URL.

Workload ports are never bound to a public address on the host. Only the edge and control can dial
them, and a connection that arrives through the edge wakes a sleeping sandbox.

## Network and limits

`network: 'none'` cuts a sandbox off on every tier. With `internet`, egress rules for hostnames or
addresses apply where the node enforces them, and a node that can't refuses them by name.
`box.network('none')` replaces a running or stopped sandbox's compute with the new policy and
keeps its data.

Resources come in units: one unit is 512 MiB and 250 CPU millis unless the operator sets other
values. How strictly a tier enforces them differs, and [Isolation tiers](https://docs.hermitbox.dev/v0.2.1/concepts/isolation.md) says how.

Timers put a sandbox to sleep or end it for you. `ttl` sets a deadline after which access stops and the
sandbox is deleted. `idle` sleeps it after that long without activity, or deletes it when
`autoDelete` is set. `box.keepAlive(duration)` extends access and counts as activity. Project
ceilings bound all three.

## Projects and keys

Every sandbox belongs to one project, and every API key belongs to one project. The key you use
chooses the project, and another project's sandbox IDs answer 404 to it.

An operator creates projects and keys on the private operator listener. The installer creates the
first key and saves it for the CLI and the SDKs.

```sh
# Create a project with limits, then a key for it. The key prints once.
hermitbox project create --max-units 5 --max-running 3
hermitbox key create --project <project-id>
```

Project limits cap units, running sandboxes, snapshots, timers and allowed images. A request over
a limit gets 403 `limit_exceeded` with the limit's name.

## Address a sandbox

Each sandbox has an ID, assigned at creation (`box.id`). It can also have a `name`, unique within
its project, plus `labels` and `metadata` you set.

```ts
// Find a sandbox again by its ID. A sleeping one wakes unless wake is false.
const box = await hb.sandbox('<sandbox-id>')

// List the project's sandboxes that carry a label.
const boxes = await hb.sandboxes({ labels: { team: 'ci' } })
```

```python
# Find a sandbox again by its ID. A sleeping one wakes unless wake is false.
box = hb.sandbox('<sandbox-id>')

# List the project's sandboxes that carry a label.
boxes = hb.sandboxes(labels={'team': 'ci'})
```

The same ID works in the CLI (`hermitbox sandbox get <sandbox-id>`) and in the HTTP API
(`/v1/sandboxes/<sandbox-id>`).

## Next steps

- [Lifecycle](https://docs.hermitbox.dev/v0.2.1/concepts/lifecycle.md): what each verb keeps.
- [Isolation tiers](https://docs.hermitbox.dev/v0.2.1/concepts/isolation.md): how each tier separates a sandbox from the host.
- [Public API](https://docs.hermitbox.dev/v0.2.1/reference/api.md) and [The sandbox agent](https://docs.hermitbox.dev/v0.2.1/reference/sandbox-agent.md): every field and route.
