# Kandev hosts

> Let the Kandev plugin run one microVM per agent session on this host, over ssh, with no daemon and no secret stored here.

[Kandev](https://github.com/kdlbs/kandev) runs each agent session in its own
environment. Its microsandbox plugin puts every session in a microVM on a
sandbox host: an ordinary msb-manager install, usually also a
[remote host](remote-hosts.md). The plugin drives the host the way your laptop
does, over ssh, through a forced command that admits the `msbctl kandev` verbs
and nothing else. The only thing listening on the host is a small TLS proxy
that carries Kandev's connections to a sandbox's ports, each one checked for a
short-lived lease token.

Kandev's sandboxes are kept apart from yours:

- The plugin finds them only through registry entries with
  `source = "kandev"`, named `k-…`. Nothing its key runs can reach one of your
  sandboxes, whatever it's called, and new names of yours can't start with `k-`.
- Your own verbs may `ls`, `show`, `shell` and `exec` into one while it runs,
  and, on the host itself, `stop` and `purge` it. They refuse everything else,
  and your laptop's key can't stop or purge one. Kandev starts its sandboxes,
  with secrets only its requests carry.
- None of msbctl's own credential setup applies to them: no bootstrap, no
  CLAUDE.md, no git identity or [git push](credentials.md#git-push-gh-or-ssh)
  rewrites, no ssh agent, no Claude token or `GH_TOKEN`. Kandev delivers agent
  and git credentials itself, through its agentctl.
- Profile secrets, registry passwords and agentctl's bootstrap nonce arrive on
  stdin. They go into the environment of one `msb` process, or the stdin of one
  `msb exec`, and are never written to a file, argv or a log. A host reboot
  leaves Kandev's sandboxes stopped, because their secrets exist nowhere on it;
  Kandev's next attach starts them again with the values.

## Setting a host up

1. Set the host up as a [remote host](remote-hosts.md) (`msbctl remote setup`
   from your laptop), or install msb-manager on it by hand.
2. Turn Kandev on and set its limits in the host's `~/.config/msb/config.toml`:

   ```toml title="~/.config/msb/config.toml"
   [kandev]
   enabled      = true
   base_url     = "https://sbx-01.tailnet.example"
   proxy_listen = "100.64.0.7"
   allow_extra  = ["pypi.org", "*.pythonhosted.org"]
   ```

   `base_url` names this host as the Kandev server will reach it, and
   `proxy_listen` is its address on that network (a tailnet or LAN IP). Every
   key is described in `defaults.toml` and [below](#the-kandev-section).

   A script (the plugin's bootstrap, say) sets the same keys with
   `msbctl host kandev configure`, which edits only the keys it is given and
   keeps the rest of the file and its comments. It checks the result as every
   verb does at load, so a bad value (`--proxy-listen sbx-01`, a name) is
   refused and the file is left as it was:

   ```sh
   msbctl host kandev configure --enabled --serve proxy \
     --proxy-listen 100.64.0.7 --base-url https://sbx-01.tailnet.example \
     --allow-extra pypi.org --allow-extra '*.pythonhosted.org' --json
   ```

   `--allow-extra` gives the whole list, one entry per flag (`--allow-extra ''`
   empties it), and `--no-enabled` turns Kandev off. With `--json` it prints
   `{"changed": [keys], "kandev": {…}, "warnings": [...]}`, the section as verbs
   now load it. A warning names your own sandboxes that publish a port inside
   `port_pool` or `proxy_port_pool` (see [the port pools](#checking-a-host)).
   Neither restricted key may run it: what the plugin's sandboxes may reach is
   yours to decide, from the host or an admin login.
3. Authorize the plugin's public key, which the plugin's Sandbox Hosts page
   shows: `msbctl host authorize 'ssh-ed25519 AAAA… kandev' --kandev`.
4. Install the reaper and the TLS proxy: `msbctl host kandev install`.
   Lingering has to be on for them to run with nobody logged in; the command
   says so if it isn't.
5. Let only the Kandev server reach `proxy_listen` on `proxy_port_pool`
   (30000–39999), with your tailnet's ACL or a firewall.

Then add the host on the plugin's Sandbox Hosts page. Its test runs
`msbctl kandev health`, and the plugin then issues the host a certificate and
installs it with [`kandev tls install`](#the-tls-proxy). `health` reports
`serve.ok` once the proxy runs with it.

## Checking a host

With `serve = "proxy"` there is **nothing listening on `base_url`'s port, and
no `/health` URL**. `base_url` only supplies the host name a lease's URL and the
certificate use; every lease points at a port of its own from
`proxy_port_pool`. So `curl https://<base_url's host>/health` fails (refused,
or answered by whatever else holds that port) even on a host that works, and while no Kandev sandbox exists the proxy holds no
port at all: that is an idle proxy, not a failed one.

To check a host, use what the plugin uses:

1. `ssh <host> msbctl kandev health` (or the plugin's Test on its Sandbox Hosts
   page): `serve.ok`, `serve.running`, and `serve.ports`, the public ports the
   proxy has open right now (`[]` when no sandbox is live).
2. A real lease: provision a sandbox from Kandev, then reach the port its lease
   names with the lease's `X-Msb-Lease` header. Only that tests the network
   path, the certificate and the token together.

The proxy's journal (`journalctl --user -u msb-kandev-proxy`) says the same:
a `serving N ports` line whenever the count changes (`serving 0 ports: no
Kandev sandbox is published through the proxy` when idle), and a line for each
port it opens or closes, with the sandbox and the guest port behind it.

**The port pools.** Every Kandev sandbox takes four ports from `port_pool` (on
127.0.0.1, one per guest port) and, with the proxy, four from
`proxy_port_pool` (on `proxy_listen`, the ones its leases name). Both skip every
host port any registry entry publishes, yours included and whether that sandbox
runs or not, so Kandev never takes one of your ports. The other way round is
up to you: a port you publish later inside a pool may be one Kandev already
holds. `host kandev configure` names your sandboxes with ports inside either
pool, and `health` counts the ones in `proxy_port_pool` as
`serve.pool_overlap`; move them, or the pool.

## The `[kandev]` section

| Key | Default | Meaning |
| --- | --- | --- |
| `enabled` | `false` | Kandev may create sandboxes here only when true. Otherwise every verb but `health` answers `denied` |
| `max_sandboxes` | `12` | Kandev sandboxes that exist at once. Yours don't count |
| `max_cpus` | `32` | summed over them |
| `max_memory` | `"96G"` | summed over them |
| `port_pool` | `"20000-29999"` | host ports published on 127.0.0.1, four per sandbox. Ports your own sandboxes publish are skipped |
| `base_url` | `""` | the HTTPS address Kandev reaches this host's sandboxes at. With `serve = "proxy"`, only its host name counts, and the certificate must name it |
| `serve` | `"proxy"` | what serves `base_url`: the [TLS proxy](#the-tls-proxy), `"tailscale"` for `tailscale serve`, or `"none"` to route it yourself. A change applies to new sandboxes |
| `proxy_listen` | `""` | the address the proxy's ports bind: this host's tailnet or LAN IP. Required for `"proxy"` |
| `proxy_port_pool` | `"30000-39999"` | the proxy's public TLS ports, four per sandbox, each opened only while that sandbox exists. Must not overlap `port_pool`; ports your own sandboxes publish are skipped |
| `rule_groups` | `["github", "npm", "python", "claude"]` | egress every Kandev sandbox gets, after `base` |
| `allow_extra` | `[]` | what a profile's extra egress may add: hostname patterns (`"pypi.org"`, `"*.example.com"`, any port), addresses (`"192.168.2.6"`, any port), `address:port` (`"192.168.2.6:8788"`, `"[fd00::1]:22"`) and CIDRs (`"192.168.2.0/24"`, any port, no wider than /8 or IPv6 /32). A hostname pattern never matches an address. Empty allows nothing extra; `["*"]` trusts Kandev's profiles |
| `private_names` | `[]` | exact hostnames a profile may ask to have resolved to a private address (a LAN forge published as `192.168.x.x`). See [names on your LAN](#names-on-your-lan) |
| `allow_nested_virt` | `false` | whether a provision may ask for `/dev/kvm` in its sandbox (`"nested_virt": true`). See [KVM in a sandbox](#kvm-in-a-sandbox) |

Nothing in a request can raise these limits or remove a base rule. Profiles add
egress only within `allow_extra`.

Kandev is one client of this host. [Another orchestrator](managed.md) can own
sandboxes beside Kandev's, with a table of its own, `[clients.NAME]`, and its
own key. Kandev's own keys, `enabled`, `max_sandboxes`, `max_cpus`,
`max_memory`, `rule_groups`, `allow_extra`, `private_names` and
`allow_nested_virt`, may live in
`[clients.kandev]` instead of `[kandev]` (one or the other, not both). The
host-wide keys, `serve`, `proxy_listen`, `base_url`, `port_pool` and
`proxy_port_pool`, never go in `[clients.kandev]` (it refuses them): they stay
in `[kandev]` or move to `[managed]`, where the optional totals over every
client go too; a key in both `[managed]` and `[kandev]` is refused. A host
that runs Kandev alone needs none of this.

## How Kandev reaches a sandbox

Each sandbox publishes four guest ports on 127.0.0.1: agentctl's control port
(8765) and the three instance ports it is told to use (41001–41003). Previews
and the embedded editor travel inside those through agentctl's own proxy, so no
other port is ever needed.

With `serve = "proxy"`, the default, each of the four also gets a public TLS
port on `proxy_listen`, and a lease is `https://<base_url's host>:<that port>`
with a token for its `X-Msb-Lease` header, reported to expire in 10 minutes (see
[the TLS proxy](#the-tls-proxy)). Kandev talks to this host directly, and needs
no Tailscale: headscale, WireGuard or a LAN work as well.

With `serve = "tailscale"`, each one is mapped under `base_url` with `tailscale
serve --set-path=/vm/<name>/p/<port>`, using the node's own certificate. Only
Tailscale's hosted control plane issues those: on headscale, `health` reports
`serve.ok = false`. Grant your user serve rights once, as root: `sudo tailscale
set --operator=$USER`. A lease is that URL with a 10-minute expiry, and has no
token: reaching :443 takes the tailnet ACL, and reaching agentctl takes Kandev's
own token from its nonce handshake.

The guest reaches Kandev's API through one rule naming the address the API host
resolved to **on the host**, and that name is written into the guest's
`/etc/hosts`. The address is usually a MagicDNS `100.x`, which DNS rebind
protection would otherwise discard in the guest. That protection stays on.
msb writes a fresh `/etc/hosts` on every boot, so `kandev start` writes the
line again.

## Names on your LAN

msb discards a DNS answer with a private address (DNS rebind protection), so a
name like `git.example.com → 192.168.2.6` comes back NXDOMAIN in the guest.
Pinning the address in `/etc/hosts`, as the Kandev API is, gets a connection
through but not a secret: msb substitutes a bound secret only on a connection
to an address it resolved for that name, and blocks the request otherwise.

So a host can let profiles name such hosts. List them in `[kandev]
private_names` (or `host kandev configure --private-name git.example.com`, one
per flag, `''` alone empties it), and a provision request's `private_names`
may then name any of them that its `egress_allow` also reaches. msb's
protection is all or nothing, so that sandbox is created with it off, and
msbctl orders its rules to stand in for it:

1. `base`;
2. the rules for the requested names;
3. every rule that names an address (the Kandev API pin, address egress);
4. a deny of this host, cloud metadata, loopback, private (RFC 1918, CGNAT,
   ULA), link-local, multicast and the reserved IPv4 ranges;
5. every other rule.

Rules are first-match-wins, so only the requested names may resolve inward:
any other allowed name that answers with a private address is refused at
connect. Patterns and addresses are refused in the list. `health` reports it
as `egress.private_names` and the feature as `private_names`, so the plugin can
tell before a launch whether a profile asking for one would be refused here.

## KVM in a sandbox

A Kandev sandbox never has `/dev/kvm` unless its provision asks for it with
`"nested_virt": true`, and the host allows that with `allow_nested_virt = true`
in `[kandev]` (`msbctl host managed configure --client kandev
--allow-nested-virt`). A request that asks where it isn't allowed is refused
with `denied`, never given less than it asked for. This host's own
`[defaults] nested_virt`, which is for your sandboxes, never applies to
Kandev's, and neither does msb's own default: msbctl always tells msb which.
`health` lists the `nested_virt` feature only where asking would be granted
(allowed, and this host has `/dev/kvm`), so the plugin can tell before it asks;
on a host without `/dev/kvm` a request is `unsupported` before anything is made.
The host also needs nested virtualization (not every VM passes it on, and ARM64
Linux has none), or msb refuses to start such a sandbox. Setting
`allow_nested_virt` back to `false` can't take KVM away from a sandbox that has
it, so `start` refuses such a sandbox (`denied`) from then on.

`host kandev configure` doesn't set this key (its output is frozen at protocol
1's); use `host managed configure --client kandev`.

## The workspace disk

Kandev starts every session's agent in `/workspace`, and nothing else creates
that directory. So each Kandev sandbox gets its own disk there, `workspace_gib`
from the provision request: an empty ext4 filesystem, writable by root, which
agentctl runs as. It's kept across `stop` and `start`, like the root disk.

It's an *owned* disk (`msb create --mount-owned`): msb deletes it with the VM.
Nothing of a workspace outlives its sandbox, whichever way the sandbox goes
(`purge`, the reaper's expiry, or an orphan's removal), and there's no separate
volume that a second step could fail to delete. It's never a host folder: a
host mount is virtiofs, which is slow for git and builds and has corrupted
git's index; it has no quota, so one build could fill the host; and it would
put what untrusted code wrote on the host's own filesystem.

## Private images

A request's `registry` credentials are used for one `msb pull`. The password is
in that process's environment. msb finds it through a throwaway copy of its
config, which names the variable and the username, never the value. The
sandbox is then created with `--pull never`, so the credential never becomes
part of its configuration. A private image must name its registry
(`ghcr.io/…`).

## The verbs

`msbctl kandev` holds only what needs Kandev's own input. For the rest, the
plugin uses the ordinary verbs with `--json`, which under its key see Kandev's
sandboxes and nothing else (any other name is `not_found`, or `absent` for
`purge`):

| Plugin needs | Runs | Gets |
| --- | --- | --- |
| the list | `ls --json` | `sandboxes`, each with `kandev`, the record (its `operation_id` is how the plugin finds a lost provision) |
| one record | `show NAME --json` | `kandev`, the record, with agentctl checked live; `not_found` with no such sandbox |
| to stop one | `stop NAME --json` | `sandboxes`: what `show` says afterwards. Its disk is kept; a stopped one is a no-op |
| to remove one | `purge NAME -y --json` | `{"purged": [...], "absent": [...]}`. Serve mappings, VM and entry go; nothing there is a success too. A VM under that name without the Kandev label is `conflict`, and left alone |

`NAME` is the `name` every record carries, which the plugin stores and never
derives. Each `kandev` verb, and each of these, prints one JSON document on
stdout. Exit 0 is success. Exit 3 comes with
`{"error": {"code": …, "message": …}}`, where `code` is one of Kandev's
(`invalid_config`, `denied`, `unsupported`, `not_found`, `expired`, `conflict`,
`unavailable`, `rate_limited` with `retry_after_seconds`, `cleanup_pending`).
Exit 126 is a refusal by `host serve`. Everything else msbctl prints goes to
stderr. `ENV` is Kandev's environment id, `[A-Za-z0-9_-]{1,128}`.

| Verb | Stdin | Does |
| --- | --- | --- |
| `health` | – | protocol (1), `features` (what this host does within protocol 1, e.g. `workspace_disk`), `egress.allow_extra` (the loaded list, so the plugin can check its default egress against it), `egress.private_names`, `host` (the machine for the plugin's host list, not for placement: `os`, `kernel`, `cpus`, `memory_total_mib`, `memory_available_mib`, `load_1m`, `disk_total_gib`/`disk_free_gib` for the filesystem msb keeps sandboxes on, `uptime_seconds`; a field that can't be read is left out), versions, KVM, capacity used and free, whether `serve` works and why not (`mode`, `listen`, `running`, `version` (the running proxy's release), `reason`; with the `serve_ports` feature also `ports`, the public ports the proxy has open, `[]` when idle and absent from a proxy too old to say, and `pool_overlap`, how many ports in `proxy_port_pool` your own sandboxes publish, absent when none), and the installed certificate (`tls`: `subject`, `not_after`, `issuer_sha256`) |
| `provision ENV` | a JSON line: image (digest-pinned), resources, `workspace_gib` (required, 1–500), `expires_at`, `kandev_api`, `egress_allow`, `private_names`, `nested_virt` (optional, [KVM](#kvm-in-a-sandbox)), `env`, `secrets`, `registry` | creates the sandbox. The same operation and input again returns it; anything else under that ENV is `conflict` |
| `agentctl ENV` | a JSON line (`sha256`, `size`, `nonce`, `runtime_port`, `args`, `env`), then the binary | verifies, installs and starts Kandev's agentctl, and waits for it to answer |
| `start ENV` | a JSON line with exactly the secrets it binds | starts a stopped sandbox; bumps `generation` |
| `expiry ENV --at TIME` | – | moves `expires_at`, never past the one it was created with |
| `lease ENV PORT` | – | `base_url`, `expires_at` and `generation` for one guest port, plus `headers` with its token through the proxy |
| `tls install` | a JSON line: `cert_pem` (the leaf, then its issuer), `key_pem` | checks and stores the proxy's certificate, and prints `{"tls": …}` as `health` does |
| `restart-proxy` | – | restarts the proxy's service if it runs another release than this msbctl, else does nothing: `{"proxy": {"state", "version"}}`, `state` being `restarted`, `current` or `not_running`; a restart that fails is `unavailable` |

The record `provision`, `start` and `expiry` return, and `ls`/`show` give as `kandev`,
has the env and name, the operation and input digest, `state` (`creating`,
`running`, `stopped`, or `absent` when the VM is gone), platform, image,
timestamps, `generation`, the port map and agentctl's state. It never holds a
secret.

## The TLS proxy

`msbctl host kandev proxy` is the one long-running piece, a systemd user service
written by `host kandev install`. With `serve = "proxy"` it listens on
`proxy_listen`, one TLS port per published guest port. For each connection it
completes the handshake, reads the first request head (16 KiB, 10 s at most)
and checks the token in its one `X-Msb-Lease` header. A missing token, two of
them, or one for another sandbox, another port or an earlier provision of the
same name gets a fixed `401`, and the connection closes. A valid one is spliced
to that port's `127.0.0.1` port, and from then on bytes pass both ways
untouched until either side closes, so WebSockets and streaming need nothing
special. `Authorization` is never looked at: it carries Kandev's own token for
agentctl, and reaches the guest as it was sent (which is also why the lease
can't use it: Kandev refuses a lease that sets it). There is no path routing:
one port per lease means a connection can only ever reach one VM port.

- **The certificate** comes from the Kandev plugin, which acts as a small
  certificate authority: a root name-constrained to your hosts' domain,
  trusted on the Kandev server only. `kandev tls install` takes the pair on
  stdin and checks it whole before writing anything: the key matches the leaf,
  the leaf is valid now and names `base_url`'s host, and the certificate after
  it issued it. The pair is stored in `~/.config/msb/kandev/tls/` (the key
  0600), and the proxy picks a new one up without a restart. Once another
  client's table sets `tls = true` that client installs it instead, and the
  Kandev key's `tls install` is `denied` ([managed clients](managed.md#the-proxys-certificate)).
  Until one is
  installed it listens on nothing, and `health` says why. `health.tls` gives
  `not_after`, which drives the plugin's renewal, and `issuer_sha256`, the
  SHA-256 of the issuing certificate, so the plugin can tell a certificate from
  an old root.
- **Tokens** are `v1.<valid_until>.<HMAC-SHA256>` over the sandbox's name, the
  guest port, its incarnation (its `created_at` and a value drawn at
  provision) and `valid_until`, keyed by 32 random bytes in
  `~/.local/state/msb/kandev/lease.key`. `lease` and the proxy share nothing
  else, so a proxy restart invalidates no lease. A lease reports `expires_at`
  10 minutes out, but its token is accepted until `valid_until`, 15 minutes
  out, plus 30 s of clock skew: Kandev asks for a new lease only 30 s before
  `expires_at`, and keeps using the one it has until then, whatever it's
  answered. For the same reason a token survives a stop and start (they bump
  `generation`, not the incarnation); only a destroy and a new provision
  retire it.
- **Rotating the key:** `msbctl host kandev rotate-lease-key` makes a new one
  and keeps the old one as `lease.key.prev`, still accepted for 15 minutes,
  so no lease already handed out breaks. Rotate at most every 15 minutes.
- **What it follows:** the Kandev entries, and those of every other enabled
  [client](managed.md). It looks every 2 seconds, so a new
  sandbox's ports open within that. A destroyed sandbox's ports close, and
  their open connections are cut. It also re-reads the entry on every
  connection, so a destroyed sandbox refuses at once.
- **What it logs:** the sandbox, the guest port and the outcome, one line per
  connection, in the service's journal; a line for each port it opens or
  closes, with the sandbox and guest port behind it; and `serving N ports`
  whenever that count changes, 0 included. Never a token, a request head or
  anything that passes through.

## The reaper

`msbctl host kandev reap` runs every minute from the timer `host kandev install`
writes. It isn't a daemon: each run makes one pass and exits.

- A sandbox past its `expires_at` is destroyed.
- A create that never finished is destroyed after 15 minutes.
- An entry whose VM is gone is marked `absent`, and waits for Kandev's purge
  or its expiry.
- A VM carrying the Kandev label with no entry, older than an hour, is removed.

It never touches a sandbox that lacks either the `kandev` source or the label.
It makes the same pass over every other [client](managed.md)'s sandboxes,
each by that client's own label: a VM labelled for one client is never
removed as another's. Only a problem in the host's keys stops it: while a
client's table (`[kandev]`'s own keys included) does not check out, its
sandboxes and everyone else's still expire, and only its orphans wait.

## The plugin key

The line `host authorize --kandev` writes is
`restrict,command="…/msbctl host serve --kandev" <key>`, with no `pty`. That key
runs `msbctl kandev …`, `ls`, `show`, `stop` and `purge -y` with `--json` on
one named sandbox, `self-update --json`, `doctor --json` (the host's checks and
Kandev's own entries and VMs, so provisioning can gate on the host; your own
sandboxes, their GitHub token, autostart and secret store are left out, and a
loose secrets file is counted, never named), and
`msbctl --version`. Nothing else,
and none of `host`: the reaper, the proxy and `host kandev configure` are this
host's own. Your laptop's everyday key is refused every `kandev` verb, since
one could create a sandbox with any egress.

`self-update --json` is the plugin's Update. It installs the latest release
(get.sh's output goes to stderr) and prints `{"msbctl_version": "0.6.1",
"updated": true, "proxy": {"state": "restarted", "version": "0.6.1"}}`, with
the version now installed. `--version X` (`0.6.1` or `v0.6.1`) is allowed only
when X is not older than the running version, as for the everyday key, since a
downgrade could weaken `host serve` itself. A git checkout answers
`unsupported`, and a host with Kandev off `denied`.

systemd keeps the old proxy process, and its old code, until the service is
restarted, so an update restarts it whenever it runs another release than the
one just installed. That cuts its open connections; Kandev reconnects, and the
leases it holds stay valid. `proxy.state` is `restarted`, `current`,
`not_running` or `failed` (with a `reason`; the update itself still
succeeded). An update from 0.6.0 or older runs the old release's code, which
never restarts the proxy: there, `health.serve.version` (the proxy's own
release, `""` before 0.6.1) differs from `msbctl_version`, and
`kandev restart-proxy` finishes the job, at a moment when no session needs
the proxy.

The Kandev key does set egress and secret bindings, which the everyday key may
never do. A Kandev profile is the policy for its own sandboxes, and the Kandev
server is yours to run. What bounds it is `[kandev]`, the reserved variable
names (`GH_TOKEN`, Claude's, `AGENTCTL_*`, `MSB_*` and the like), and the fact
that its verbs only ever see Kandev's sandboxes. `ANTHROPIC_API_KEY` is not
among those names: a request may bind its own, as any other secret, to the
hosts it names.
