# Architecture

Hermitbox has three parts. `hermitboxd`, on each host, runs the sandboxes and owns their local
files. Control decides what should run where: it owns each sandbox's desired state, its placement
epochs and the Postgres ledger, and it reaches hosts only through `hermitboxd`'s mutual-TLS API.
The edge takes public traffic to sandboxes and wakes them when it arrives.

```mermaid
flowchart LR
  Client -->|TLS / HTTP / TCP| Edge
  Client -->|Bearer key /v1| Control
  Edge -->|private /edge/v1| Control
  Control --> Postgres
  Control -->|mTLS| hermitboxd
  Edge -->|private allocated ports| Sandbox
  hermitboxd --> Sandbox
  hermitboxd -->|presigned transfers| S3
```

The edge is the public entry point. It polls routes and picks a sandbox by TLS SNI, by HTTP Host
or by a dedicated TCP port. If the sandbox sleeps, it holds the bytes while control wakes it, then
relays them. Applications get no platform greeting and need no integration of their own.

What each lifecycle verb does underneath:

- **Create** records the request before it asks a node, then places and boots the sandbox, or
  provisions it stopped.
- **Sleep** keeps the data and tries to suspend process memory. On a node without that feature,
  it stops the sandbox.
- **Wake** resumes memory, boots from files, or restores a resting snapshot under a new epoch.
- **Rest** captures and uploads a snapshot first, then releases local compute and data.
- **Snapshot** and **fork** copy persistent files, not memory.
- **Delete** removes the data and the snapshots too. Every other verb keeps state on purpose.

A sandbox that isn't running sits at one of three depths, each keeping less on the host:

| Tier | Retained resources | Wake |
| --- | --- | --- |
| Suspended | Memory, local disk, node reservation | Resume process memory |
| Stopped | Local disk and placement | Boot image from files |
| Resting | Object-store snapshot | Place, download, restore, boot |

Each depth still costs something: a suspended sandbox holds memory, a stopped one holds local
disk, and a resting copy uses object-store space and transfer requests. Prices come from the
operator's or provider's configuration, never from constants in the product. On XFS, reflink
shares unchanged blocks; other filesystems make a full copy.

## Isolation tiers

- **Container** fixes capabilities, filesystem mounts, network rules and allowed image prefixes
  through host policy.
- **Process** uses the bundled userland, Landlock, seccomp, rlimits and sampled resident memory
  without creating namespaces; its weaker boundary is described in
  [process-tier.md](https://docs.hermitbox.dev/v0.3.2/reference/process-tier.md).
- **Wasm** uses a virtual kernel with WebAssembly and V8 in a shared unprivileged sidecar; SQLite
  and the explicit data mount survive recreation, while processes and terminals end on sleep. Its
  limits and API mapping are in [agentos.md](https://docs.hermitbox.dev/v0.3.2/integrations/agentos.md).
- **VM** boots each sandbox in a Firecracker virtual machine with its own kernel. A node announces
  it only where KVM works for `hermitboxd`'s ordinary user and a signed installation supplies its
  bundle; a root node serves it only through the separate VM worker service ([virtual machines](https://docs.hermitbox.dev/v0.3.2/guides/virtual-machines.md)).

[tiers.md](https://docs.hermitbox.dev/v0.3.2/reference/tiers.md) compares the tiers. Placement gives a sandbox the isolation tier it asked
for, and no request can loosen host policy. Workload ports are private: only the configured
control and edge sources can dial them.

## Epochs and failures

An epoch names one placement of a sandbox, and it keeps an old copy from acting after a move.
Every mutating node request carries its epoch in `blocklyd-epoch`, so an older handle can't change
a newer copy. A move fences and retires the old copy, and the edge closes connections from older
epochs. Control's execution lease allows a restart without an operator only while it is current;
when it expires, a sandbox that is already running keeps running. With the optional store lease
off, failover still needs an operator to confirm the host is lost.

What happens when a part fails:

| Failure | Hermitbox behavior | Caller sees |
| --- | --- | --- |
| Daemon crash | Containers continue; the owned wasm worker stops, retaining files for a fresh control ensure | Operations wait/fail; data stays |
| Docker restart | live-restore keeps containers; otherwise report stopped/crashed | Unavailable operations until recovery |
| Host reboot | Resume only after control authorizes | Temporary interruption |
| Host loss / partition | No timer creates another running copy | Unknown/unavailable; operator fences before rebuild |
| Control / Postgres outage | Running sandboxes continue; no new decisions | API operations fail or wait |
| S3 outage | Keep local data/copies; rest/rebuild waits | Operation unavailable, local copy retained |
| Disk floor | Refuse writes/creates needing room | insufficient_disk; running work remains |
| Lost exec reply | Keep durable uncertain request identity | operation_unknown; inspect before new key |
| Edge route change | Apply incrementally; close stale epoch connections | Reconnect to current copy |
| Edge dies | Sessions age out; other edges continue | Connections through that edge close |
| TTL expires | Access refuses immediately; upkeep deletes | gone |

The node protocol keeps its original v1 names on the wire and on disk. Rust types generate the
node's OpenAPI document, Zod generates the public and edge contracts, and checks catch clients and
documentation that drift from them.

A create keeps a durable identity in control across retries, and an exec with an uncertain outcome
is never replayed blindly. Secrets stay out of views, events and node digests, and API keys are
stored only as hashes. [protocol.md](https://docs.hermitbox.dev/v0.3.2/reference/protocol.md) has the exact node semantics,
[operate](https://docs.hermitbox.dev/v0.3.2/guides/operate.md) covers recovery, and [edge.md](https://docs.hermitbox.dev/v0.3.2/reference/edge.md) has the measured client
wait bounds.
