---
title: Daemon and bridge
description: How a host process attaches to the in-container devsbd daemon through a devsbd bridge, the frames they speak, and how streams are routed.
eyebrow: ARCHITECTURE · 02
order: 2
---
import Sequence from '../../components/architecture/Sequence.astro';
export const attachLanes = [
{ id: 'serve', label: 'devsandbox serve', sub: 'host · Bridges worker', tone: 'serve' },
{ id: 'bridge', label: 'devsbd bridge', sub: 'container · exec -i', tone: 'helper' },
{ id: 'daemon', label: 'devsbd daemon', sub: 'container · root', tone: 'helper' },
{ id: 'client', label: 'ssh · devsbd notify', sub: 'container · any uid', tone: 'client' },
];
export const attachSteps = [
{ kind: 'phase', label: 'attach' },
{ from: 'serve', to: 'bridge', label: 'exec -i -u root <c> devsbd bridge', sub: 'after reinstalling a stale helper', tone: 'runtime' },
{ from: 'bridge', to: 'daemon', label: 'connect devsbd.ctl', sub: 'retries 2 s; nothing listening → starts devsbd daemon once' },
{ from: 'bridge', to: 'daemon', both: true, label: 'Hello ⇄ Hello', sub: 'version · build hash · daemon caps TCP_FORWARD · PORT_INFO · RPC' },
{ from: 'bridge', to: 'serve', both: true, label: 'Hello ⇄ Hello', sub: 'caps = bridge ∧ daemon · host gives up after 10 s' },
{ from: 'serve', to: 'daemon', label: 'Caps', sub: 'SSH_AGENT · NOTIFY · CONTROL · HTTP_PROXY, through the bridge verbatim', tone: 'serve' },
{ kind: 'act', at: 'bridge', label: 'copies bytes both ways', sub: 'never parses another frame' },
{ kind: 'phase', label: 'streams' },
{ from: 'client', to: 'daemon', label: 'connect ssh-agent.sock', tone: 'agent' },
{ from: 'daemon', to: 'serve', label: 'Open(ssh-agent) · Data …', sub: 'id < 2³¹ · newest agent-capable bridge', tone: 'agent' },
{ from: 'serve', to: 'daemon', label: 'Data … · Close', sub: 'host dials the first live $SSH_AUTH_SOCK', tone: 'agent' },
{ from: 'client', to: 'daemon', label: 'devsbd notify → outbox, poke api.sock', tone: 'notify' },
{ from: 'daemon', to: 'serve', label: 'Open(notify) · Data · Eof', sub: 'one record (notice, thread, URL to open); resent until ok', tone: 'notify' },
{ from: 'serve', to: 'daemon', label: 'Open(rpc) · call … result', sub: 'id ≥ 2³¹ · one stream per call · cancel = Close', tone: 'serve' },
{ from: 'serve', to: 'daemon', label: 'Connect(host, port) · Window · Eof', sub: 'forward bridges · id ≥ 2³¹ · daemon dials TCP' },
{ kind: 'phase', label: 'liveness' },
{ from: 'serve', to: 'daemon', both: true, label: 'Ping ⇄ Pong', sub: 'every 15 s, from both ends', tone: 'runtime' },
{ kind: 'act', at: 'daemon', label: '45 s silent → drop the bridge', sub: 'routing falls back to the next newest', tone: 'fail' },
{ kind: 'act', at: 'serve', label: '45 s silent → kill the exec', sub: 'reconcile respawns, 10 s gap', tone: 'fail' },
];
export const takeoverLanes = [
{ id: 'serve', label: 'devsandbox serve', sub: 'host', tone: 'serve' },
{ id: 'start', label: 'devsandbox start', sub: 'host · newer build', tone: 'cli' },
{ id: 'new', label: 'devsbd daemon', sub: 'new build', tone: 'helper' },
{ id: 'old', label: 'devsbd daemon', sub: 'old build', tone: 'runtime' },
];
export const takeoverSteps = [
{ from: 'start', to: 'new', label: 'write the binary · exec -d devsbd daemon', sub: 'the container was already running' },
{ kind: 'act', at: 'new', label: 'devsbd.pid is locked', sub: 'by another build hash' },
{ from: 'new', to: 'old', label: 'Quit', sub: 'first frame on devsbd.ctl; frozen, so every build understands it' },
{ kind: 'act', at: 'old', label: 'exits', sub: 'its bridges and streams end', tone: 'fail' },
{ kind: 'act', at: 'new', label: 'takes the lock, rebinds', sub: 'waits up to 3 s' },
{ from: 'serve', to: 'new', label: 'next poll: exec -i devsbd bridge', sub: 'the bridge is the new build too', tone: 'serve' },
];
Each host process that reaches a container runs its own `devsbd bridge` there. The bridge handshakes on each side,
then copies bytes until either end closes; only the shared `Mux` (`src/devsbd/mux.rs`), in the daemon and on the
host, parses frames. The same `Mux` carries the links between machines ([Fleet](/architecture/fleet)).
<Sequence title="A host daemon's bridge, from attach to keepalive" lanes={attachLanes} steps={attachSteps} />
## Two handshakes
The bridge says `Hello` to the daemon first, then to the host, so a daemon of another protocol version fails the
bridge (exit `3`) before the host sends a frame it can't parse. Only `VERSION` must match. The bridge tells the host
`OWN_CAPS ∧ daemon caps`, and passes the host's `Caps` to the daemon unchanged.
## Frames
One byte stream each way, little-endian, each frame written with a single `write_all` so writers can share it:
```text title="src/devsbd/proto.rs"
u32 stream_id | u8 kind | u32 len | payload[len] (len ≤ 1 MiB)
```
| kind | name | stream | payload |
|---|---|---|---|
| 0 | `Hello` | 0 | `u32 version`, `u8` + build hash, `u32 caps` |
| 1 | `Open` | daemon id; host id for rpc | `u8 channel`: 1 ssh-agent, 2 http-proxy (the credential broker), 3 notify, 4 control, 5 rpc |
| 2 | `Data` | any | bytes |
| 3 | `Close` | any | optional reason (`connection refused`, …) |
| 4 / 5 | `Ping` / `Pong` | 0 | echoed |
| 6 | `Quit` | 0 | none: the daemon takeover |
| 7 | `Connect` | host id | `u16 port`, `u8` + host |
| 8 | `Window` | any | `u32 credit` (flow control on `Connect` and rpc streams) |
| 9 | `Eof` | any | none: half-close |
| 10 | `Caps` | 0 | `u32` host caps |
| 11 | `PortQuery` | 0 | `u32 id`, `u16 port`: who listens on that port in the container |
| 12 | `PortInfo` | 0 | `u32 id`, then per process `u32 pid`, `u8` + comm: the daemon's answer |
Channels 6–11 only run between machines ([Fleet](/architecture/fleet)). The header, `Hello`'s leading `version` and
`Quit` are frozen, so any two builds can evict each other. Unknown kinds are skipped. The daemon allocates stream ids
with the high bit clear, the host with it set.
## Routing in the daemon
The daemon keeps every attached bridge and routes each stream it opens to the newest one whose host serves it:
| stream | goes to | when there's none |
|---|---|---|
| ssh-agent | newest bridge with `SSH_AGENT` | held up to 1 s for one, then closed ("agent refused") |
| notify (notices, threads, URLs to open) | newest bridge with `NOTIFY` | stays in `/var/lib/devsandbox/outbox/`, retried every 30 s and on attach |
| control | newest bridge with `CONTROL` | answered `NoHost` (the CLI exits 75) |
| http-proxy (a brokered call; its first bytes name the upstream) | newest bridge with `HTTP_PROXY` | held up to 1 s for one, then answered `503` |
## Takeover
`start` on a running container rewrites the binary and starts a daemon of the new build. So does the serve daemon
before it respawns a bridge, which is how a daemon that took over after an upgrade (or a machine that followed the
authority's pinned build) reaches containers already running. The last start wins, upgrade or downgrade:
<Sequence title="A new build's daemon taking over a running container" lanes={takeoverLanes} steps={takeoverSteps} />
With the same build hash the second daemon exits 0, so two concurrent starts never evict each other.
Design notes: `docs/architecture/helper/protocol.md`, `docs/architecture/helper/bridges.md`,
`docs/decisions/0053-the-authority-pins-the-fleets-build.md` (the pinned build).