devsandbox 0.10.0

Manage devcontainer-style sandboxes on docker, podman, or Apple container — many throwaway instances per repo from one TOML config.
---
title: Host and containers
description: How the host reaches into every container through devsbd, the static helper it writes there, and which process owns each end.
eyebrow: ARCHITECTURE · 01
order: 1
---

import HelperDiagram from '../../components/architecture/HelperDiagram.astro';

Nothing in a sandbox talks to the host directly. The host runs `exec -i … devsbd bridge` in the container and keeps
the pipe open. Everything that crosses the boundary (ssh-agent requests, `devsbd notify`, a URL the sandbox opens, a
dispatcher's `devsbd ensure`, a forwarded port, the host's own calls) is a stream in that pipe, which works the same
on every runtime.

<HelperDiagram />

## The helper binary

`devsbd` is a static musl binary (x86_64 and aarch64) embedded zstd-compressed in `devsandbox` (`build.rs`,
`src/devsbd.rs`). The host writes it through `exec -i -u root <c> sh -c 'cat > …'` to `/run/devsandbox/bin/devsbd`
(no `docker cp`, no file binds), unless `devsbd version` there already reports an embedded build hash. That check
runs in `run` after create, `start` after the container starts, `port`, and the host daemon before every bridge
(re)spawn, so a newer daemon upgrades a running helper. `run`, `start` and `inspect` then exec `devsbd cgroup check`
and warn when an exec lands outside the container's cgroup (rootless runc before 1.4.1: a dockerd started there
can't start containers).

The arch that worked is saved on the instance (`devsbd_arch`). `None` means no helper, and the instance falls back to
a bind-mounted agent socket.

## One daemon per container

`devsbd daemon` runs as root and owns the in-container ends:

| socket | mode | who connects |
|---|---|---|
| `/run/devsandbox/ssh-agent.sock` | 0666 | `ssh`, `git` (`SSH_AUTH_SOCK` points here in relay mode) |
| `/run/devsandbox/api.sock` | 0666 | `devsbd notify`'s poke, `xdg-open` (the helper stands in for it), the control verbs (`ensure`, `events`, `run`, …) |
| `/run/devsandbox/devsbd.ctl` | 0600 | `devsbd bridge` only |

It also answers the host's `RPC` streams, one per call (`rpc.methods` lists them): that's how the host attaches to
a helper session (`session.attach`) or drives a dispatcher child's tracked runs (`run.*`) without an `exec`.

An exclusive lock on `/run/devsandbox/devsbd.pid` (`<pid> <build hash>`) keeps it alone; a daemon of another build
takes over with `Quit` ([Daemon and bridge](/architecture/bridge#takeover)). Three things start one, and the lock
makes their races harmless: the host after each install (`exec -d -u root <c> devsbd daemon`), `devsbd boot` (the
container command's restart hook, which then runs the boot file's commands), and a bridge that finds nothing
listening (a container the runtime restarted on its own).

## Who holds a bridge

Each host process owns its bridges. The daemon routes each stream to the newest bridge whose host serves it, so
several per container are normal:

| host process | bridge | serves |
|---|---|---|
| `devsandbox serve` | one per running instance, reconciled on each container event (5 s timer while one is pending or failed, or there's no event stream) | ssh-agent, notify, control |
| a Ports-tab or `(open)` forward (inside `serve`) | one per forward | its `Connect` streams, ssh-agent |
| `devsandbox port` (foreground) | its own | its `Connect` streams, ssh-agent when its shell has one |

Commands that need the relay (`exec`, lifecycle commands, a dashboard terminal) don't bridge themselves. They send
`bridges.ensure` with their own `$SSH_AUTH_SOCK` ([Host daemon lifecycle](/architecture/serve-lifecycle#who-talks-to-the-daemon)).
A TTY `exec` runs its command in a helper session (`devsbd session new`) that outlives its client until the command
exits, `exec --end` ends it or the container stops. The dashboard lists sessions as rows under their instance.

## Where the streams end

On the host, an agent stream goes to the first live agent: up to 8 that clients reported, newest first, then one
another machine relays over its link ([Fleet](/architecture/fleet)), then the daemon's own `$SSH_AUTH_SOCK`. A notify
record lands in `inbox.json` with a popup; an `open` record (an `http(s)` URL, rate-limited, at most 30 s old) opens
in the desktop of the host the instance was last attached from, after forwarding the loopback ports it names as
`(open)` forwards, stopped after 10 min idle. Control requests are served by the daemon (`src/commands/dispatch.rs`).

The host opens `Connect` streams for forwards and an `RPC` stream per call (`HelperClient`, `src/serve/helper.rs`).

Design notes: `docs/architecture/helper/overview.md`, `docs/architecture/helper/bridges.md`,
`docs/architecture/helper/in-container.md`, `docs/architecture/helper/ssh-agent.md`,
`docs/architecture/daemon/forwards.md`.