# Lifecycle

A sandbox is created, started, stopped, put to sleep, woken, rested, snapshotted, forked and
deleted. Every verb but `delete` keeps the sandbox's data; they differ in what else they keep.

| Verb | SDK | Afterwards | Released |
| --- | --- | --- | --- |
| `create` | `hb.sandbox(options)` | A new sandbox from an image, a snapshot or a template, started unless `start: false` | Nothing |
| `start` | `box.start()` | Running, with its data | Nothing |
| `stop` | `box.stop()` | Stopped; data stays on its node | Processes and memory |
| `sleep` | `box.sleep()` | Sleeping with its data, and with memory and processes where the tier suspends | CPU, and memory when the tier stops instead |
| `wake` | `box.wake()` | Running again, with whatever sleep or rest kept | Nothing |
| `rest` | `box.rest()` | Resting; a snapshot in the object store | Compute and local data on the node |
| `snapshot` | `box.snapshot()` | A saved copy of the data directory | Nothing; a running sandbox restarts |
| `fork` | `snapshot.fork()` | A new, independent sandbox from a snapshot | Nothing |
| `delete` | `box.delete()` | Gone | Compute, data and the sandbox's snapshots |

The HTTP API uses the same verbs: `POST /v1/sandboxes/<sandbox-id>/start`, `/stop`, `/sleep`,
`/wake` and `/rest`, `POST /v1/sandboxes/<sandbox-id>/snapshots` for a snapshot, and
`POST /v1/snapshots/<snapshot-id>/sandboxes` for a fork.

## Sandbox states

A sandbox reports one of these in `box.state`:

| State | Means |
| --- | --- |
| `creating` | Hermitbox recorded the request and is placing it |
| `starting` | Compute is booting or resuming |
| `running` | Commands and connections reach it |
| `sleeping` | Suspended, or stopped by a sleep, on its node |
| `stopped` | No compute; data on its node |
| `resting` | No compute and no node; its data is a snapshot in the object store |
| `restoring` | A wake is downloading and restoring that snapshot |
| `failed` | The last change didn't complete; the sandbox's events say why |

## Sleep and wake

`box.sleep()` suspends the sandbox where its tier can keep memory, and stops it otherwise. The
answer's `tier` says which: `suspended` or `stopped`. A node that announces `memory-sleep` keeps
processes and terminals across sleep:

- `process`, always.
- `container`, on a rootful engine with memory limits.
- `vm`, as an ordinary user, once its start test has restored a guest on that host.
- `wasm` never does: sleep ends its processes and keeps its data.

A sandbox wakes in any of these ways:

- **An API call:** `box.wake()`, `box.start()` or `POST /v1/sandboxes/<sandbox-id>/wake`.
- **A lookup:** `hb.sandbox('<sandbox-id>')` in the SDK, or `hermitbox sandbox exec`, wakes a
  sleeping or resting sandbox. Pass `wake: false` to the SDK to look without waking.
- **A command or a connection:** an SDK command or a connection to one of its ports reaches the
  edge, which holds the bytes while control wakes the sandbox, then relays them.

`POST /v1/sandboxes/<sandbox-id>/exec` doesn't wake: it answers 409 `not_running` until you do.

The wake's `tier` says what happened: `resumed` (memory restored), `booted` (started from its files)
or `restored` (rebuilt from a resting snapshot).

```ts
// Sleep: compute stops, the data stays.
await box.sleep()

// Wake it yourself, or let the next command or connection wake it.
await box.wake()
```

```python
# Sleep: compute stops, the data stays.
box.sleep()

# Wake it yourself, or let the next command or connection wake it.
box.wake()
```

With an `idle` timer, control sleeps a sandbox that has had no activity and no live session for
that long. With `autoDelete` as well, it deletes the sandbox instead.

## Stop and start

`box.stop()` ends every process: the stop signal first, then a kill after the grace period. Data and
placement stay on the node. `box.start()` boots compute again from those files, and an `entrypoint`
runs again from the beginning.

## Rest and restore

`box.rest()` takes a snapshot, uploads it to the object store, and only then releases the
sandbox's compute and local data. If the upload doesn't complete, the local data stays and rest
fails. A resting sandbox keeps its ID and uses no node at all.

A wake places it on a node with room, downloads and restores the snapshot, and boots it under a
new placement epoch. It can land on a different node from the one it left.

## Snapshot and fork

A snapshot copies the sandbox's persistent files, not its memory. A running sandbox is stopped for
the copy and started again, so its processes restart. A `vm` sandbox asleep with its memory is
copied asleep, and a fork on the same node wakes with the source's processes.

```ts
// Save the box as it is.
const snapshot = await box.snapshot()

// Start an independent copy from that snapshot.
const copy = await snapshot.fork()
```

```python
# Save the box as it is.
snapshot = box.snapshot()

# Start an independent copy from that snapshot.
copy = snapshot.fork()
```

A fork is a new sandbox with its own ID and agent key, on the snapshot's tier. Changes to one never
reach the other. `snapshot.delete()` removes a snapshot, except while a template version uses it.

## Delete a sandbox

`box.delete()` removes the sandbox's compute, data and snapshots, and retires its addresses. It is
refused while a template still uses the sandbox's data. After a `ttl` passes, access stops at once
(410 `gone`) and upkeep deletes the sandbox.

## Next steps

- [Sandboxes](https://docs.hermitbox.dev/v0.2.4/concepts/sandboxes.md): what the data directory holds.
- [Templates](https://docs.hermitbox.dev/v0.2.4/concepts/templates.md): start every sandbox from the same snapshot.
- [Public API](https://docs.hermitbox.dev/v0.2.4/reference/api.md): request and answer shapes for every verb.
