# Runners and hosts

**Spinbox is experimental: APIs change and data may be lost.** What is built and what proves it is on [Status](https://docs.spinbox.dev/reference/status.md); what Spinbox does not do is on [Limitations](https://docs.spinbox.dev/overview/limitations.md).

> What a runner is, how it joins, how workspaces are placed on it, how runners scale from zero, and what happens when one is lost.

A **runner** is a machine that runs workspaces. In the dashboard and the API a runner is also
called a **host**. On AWS, runners are autoscaling groups of large EC2 machines with local
NVMe disks and nested virtualization, spot by default: one group per host class, which the control
plane makes.

## What a runner does

- Keeps a connection open to the control plane, and runs what the control plane says should
  run on it.
- Starts each workspace as a QEMU/KVM virtual machine, confined to its own user, cgroup and
  empty network namespace.
- Keeps each workspace's disk on its local NVMe, and uploads its sealed layers to the bucket.
- Enforces each workspace's network policies, in nftables, outside the machine.
- Opens sessions to the relay when someone connects to a workspace.

A runner opens every connection it has. It has no inbound rule at all.

## Joining

A runner in one of the installation's groups joins **by who it is**: it signs an AWS request
with its instance role, which the control plane has AWS check, and sends a NitroTPM document that
says which build of Spin OS it booted, which the control plane checks against the builds it
allows. There is no token to create, store or rotate. [NitroTPM](https://docs.spinbox.dev/security/nitrotpm.md#how-a-runner-is-admitted)
is the whole of it.

A machine outside the groups can join with a one-time registration token from Admin → Runners.

A runner must run the control plane's release. One of any other, older or newer, is refused when it
registers, and the refusal says which release to run.

## Placement

The control plane places a workspace on an **active** runner with room for its disk and
memory. It prefers the runner holding fewer of the same team's workspaces, so one team does not
pile onto one machine. A runner whose local disk is more than 70% full stops taking new
workspaces until it is back under 65%.

A preset may name a **host class**, a kind of runner, and its workspaces are placed only on
runners of that class. The module seeds one class,
`default`, from its `runner_*` variables, beside any the installation file declares; the dashboard
changes them after that. A spot class can be set to fall back: when AWS refuses spot
capacity for its group, the control plane runs the group on demand for 30 minutes, then tries spot
again.

## Scaling from zero

On AWS [the control plane sizes the runners' groups itself](https://docs.spinbox.dev/reference/status.md#administration). It knows what nothing else does: that
a workspace is waiting, and when the last one stopped.

- With no runner up, the first workspace to start waits in *Waiting for a host* while the control
  plane asks for one. It is placed on the runner as soon as it registers.
- If a workspace has waited ten minutes, and no runner was added in that time, the control plane
  adds one.
- When nothing has run for [`runner_idle_minutes`](https://docs.spinbox.dev/reference/terraform/root.md#runner_idle_minutes), it scales the groups back to
  none. During the installation's `quiet_hours`, it does so as soon as nothing is running.
- It never asks for more than `max_runners`.

While workspaces run and none waits, a runner that holds nothing only it has, for as long as
`runner_idle_minutes`, is emptied and given back to its group, one at a time.

## When a runner goes away

A runner that is going away empties itself first. It suspends each of its workspaces, with
memory, to the bucket, and each one resumes on another runner from where it was. This happens
when:

- [AWS reclaims a spot runner](https://docs.spinbox.dev/reference/status.md#administration) (about two minutes' notice),
- its autoscaling group removes a runner (the group holds it for
  [`runner_drain_seconds`](https://docs.spinbox.dev/reference/terraform/controlplane.md#runner_drain_seconds)),
- a release is rolled out and runners are replaced.

A runner restarting its own process is not a drain: the virtual machines keep running, and the
new process adopts them.

## Operating runners

An administrator can **drain** a runner, so it takes no new workspace, **activate** it again, or
**revoke** it for good. [Taking a runner out](https://docs.spinbox.dev/admin/runners.md) is what each does and how.

## When a runner is lost

A runner can vanish without draining: the machine loses power, or its network. The control plane
does not guess. Its workspaces stay where they are, and each one has lost only what the runner
had not yet uploaded.

- [The owner moves one, at a cost they accept](https://docs.spinbox.dev/reference/status.md#workspaces):

  ```bash
  spin workspace recover api                      # says what moving it would lose
  spin workspace recover api --accept-data-loss   # moves it
  ```

- The control plane moves workspaces on its own, after a runner has been silent for
  `rehome_after_seconds`, when no more than `rehome_max_unpublished_bytes` would be lost: [the workspace is moved, and its owner is told when it waits for them instead](https://docs.spinbox.dev/reference/status.md#administration).
  By default it moves one only when nothing would be lost, and `rehome_after_seconds` set to 0
  turns this off.

[The owner is told by email](https://docs.spinbox.dev/reference/status.md#observability):
when a workspace is held after its host was lost, when a stop could not be taken, when it was
stopped for not responding, and when it was moved off a lost host. Each event can be switched
off.
