# Lifecycle

A sandbox keeps its work when it isn't running. Put it to sleep and it frees its CPU; the next
command, connection or API call wakes it with its files and, where it can keep memory, with its
running processes too. Archive it (`rest`) and it uses no machine at all until it wakes. Snapshot
it, and you can fork independent copies from that point.

Every verb but `delete` keeps the sandbox's data. They differ in what else they keep and what they
release.

| 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

Sleep lets a sandbox wait without holding CPU, then carry on where it left off. In the
[paired budget checks](https://docs.hermitbox.dev/v0.3.2/reference/tiers.md#paired-budget-checks), a suspended wake measured
27.56 ms at p50 (n=30). A `vm` sandbox with one vCPU and 256 MiB woke with its memory in
220.05 ms at p50, its process still running
([Memory sleep and wake](https://docs.hermitbox.dev/v0.3.2/reference/tiers.md#memory-sleep-and-wake)).

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

- `process`, always.
- `container` and `gvisor`, on a rootful engine with memory limits; otherwise they stop.
- `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. The client sees
  an ordinary connection.

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

The wake's `tier` field 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()
```

```rust
// Sleep: compute stops, the data stays.
sandbox.sleep().await?;

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

You don't have to remember to sleep a sandbox. With an `idle` timer, control sleeps one that has had
no activity and no live session for that long. With `autoDelete` as well, it deletes the sandbox
instead.

## Stop and start

Stop a sandbox for a clean start with the same files. `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

Rest archives a sandbox you won't need for a while. It keeps its ID, and its data waits in the
object store while it uses no node at all.

`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 wake places it on a node with room, downloads and restores the snapshot, and boots it under a
new placement epoch, the fleet's guard against two copies of one sandbox
([Nodes and fleet](https://docs.hermitbox.dev/v0.3.2/concepts/fleet.md#what-control-decides)). It can land on a different node from the one
it left.

## Snapshot and fork

A snapshot saves a sandbox's files at one point, and a fork starts a new sandbox from them. Use
them to start several sandboxes in the same state, or to try something on a copy.

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()
```

```rust
// Save the sandbox as it is.
let snapshot = sandbox.snapshot().await?;

// Start an independent copy from that snapshot.
let copy = snapshot.fork().await?;
```

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

Delete a sandbox when you're done with it and its snapshots. `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.3.2/concepts/sandboxes.md): what the data directory holds.
- [Templates](https://docs.hermitbox.dev/v0.3.2/concepts/templates.md): start every sandbox from the same snapshot.
- [Public API](https://docs.hermitbox.dev/v0.3.2/reference/api.md): request and answer shapes for every verb.
