Previews: a reverse tunnel over the agent API — labctl expose <name> <port> dials out from the run, and lab serves that port at http://<name>.<preview-domain> on an opt-in, unauthenticated preview listener¶
An agent often serves something the operator should look at: a Vite dev server, a built docs site, a Storybook. Lab had no way to show it. A container run sits behind --network=pasta:--map-guest-addr,none with no published ports (ADR-0052), so a dev server bound inside the container cannot be reached from the host. The run's only route to lab is the mounted agent unix socket. A host run's dev server is on the host loopback, but nothing proxies it. Short of SSHing to the lab host, the operator could not open anything an agent was serving. labctl's verbs were issue, label, pr and secret, and the agent API (/agent/v1, run-token authenticated) had no streaming or upgrade endpoint.
The feature had to work the same for both Runners. It also had to leave ADR-0052's network pins alone, and dev servers should keep their default loopback binding. Those three constraints settle the shape: the run dials out to lab, exactly as labctl already does, and lab never reaches into the run.
Decisions, pinned:
- A reverse tunnel, opened by the agent. In a run,
labctl expose <name> <port>registers the exposure with lab over the agent API, prints the resulting URL as its first stdout line, and then forwards traffic until it is stopped. This is the only transport that works for both Runners without changing ADR-0052's network pins, without port allocation state, and with dev servers on their default loopback binding. The connection goes out over the route labctl already uses: the unix socket for every container run and for host runs by default, or a TCP--agent-url. Lab never reaches into a run. - The protocol: a hijacked agent-API upgrade carrying
hashicorp/yamux. labctl sendsPOST /agent/v1/exposureswithConnection: Upgrade,Upgrade: lab-tunneland a small JSON body ({"name":"web","port":5173}). It is run-token authenticated like every other agent route. Every refusal is answered before the hijack, with the agent API's usual{"error": msg}envelope. On success lab answers101 Switching ProtocolswithLab-Preview-URL: <url>, hijacks the connection and runs a yamux session on it. Lab is the yamux client and labctl the server. The first stream lab opens is the control stream. Lab writes one newline-terminated message on it (stopped,run-endedorreplaced) and then closes the session. A message is final, and labctl does not reconnect after one. A session that dies with no message is a dropped connection, and labctl reconnects. Every later stream lab opens carries one proxied request: labctl dials the run's loopback port (below) and writes one status byte (0x00dial ok,0x01nothing listening), then copies bytes both ways. Inside each stream lab speaks plain HTTP/1.1 with keep-alives off, so a WebSocket upgrade (Vite HMR, Next's dev overlay) is just another proxied request and needs no special handling. yamux is MPL-2.0, which is compatible with lab's AGPL-3.0, and v0.1.2 has no transitive dependencies.DELETE /agent/v1/exposures/{name}ends an exposure. One long-lived connection per exposure, one stream per browser request. - The agent picks the name; lab validates it and answers 409 on a conflict. This was the maintainer's call. A readable URL is one the agent can announce in a PR or in the chat, and lab needs no slug scheme of its own. A name is a single DNS label: 1–63 characters of lowercase
a-z,0-9and-, with no leading or trailing-. Uppercase is rejected, not folded. The 409 message (name "X" is already exposed by another run) says nothing about the other run. - A name is held for the run, not for the connection. A lab restart drops every hijacked connection, while
KillMode=processkeeps the run alive. If a disconnect released the name, a sibling run could take it during the reconnect window and serve a different app to a tab the operator still has open. So a name belongs to the run that first registered it until the agent stops it or the run ends. A reconnect from the same run re-attaches; any other run gets 409. - A foreground command that the agent runs in the background.
labctl exposeblocks. The agent starts it with&, and the process lifetime is the exposure lifetime, plus an explicitlabctl expose stop <name>. There are no pidfiles and no daemon discovery, which would have to be solved twice, once per Runner. On a dropped connection labctl reconnects with backoff and reclaims its name. It exits non-zero only when lab answers that the run or the name is gone. On SIGINT or SIGTERM it releases the name on a best-effort basis and exits 0. - Loopback only, port only. labctl never accepts a host. Traffic goes to
127.0.0.1:<port>in labctl's own network namespace, or to[::1]:<port>when the IPv4 dial fails: inside the container for a container run, the host loopback for a host run. IPv6 is tried because Node ≥17 listens on the first addresslocalhostresolves to, and on the lab host and in the run image that is::1. A default Vite server (server.host: 'localhost') therefore listens on[::1]only and would otherwise get the 502 page. IPv4 goes first, so a server listening on both is reached over IPv4. Both addresses are the run's own loopback, so the preview proxy can never become a door to other host services or to the operator's network. - Eight exposures per run. Each live exposure pins a long-lived connection and a set of goroutines on lab. An agent that needs more than a handful is far more likely looping than serving eight apps. The ninth is refused with 422
exposure limit reached (8 per run). - One hostname per exposure. Every preview is its own browser origin, so two runs' pages share no cookies, no storage and no service workers, and none of them shares an origin with lab.
- A new listener, routed by
Host. The preview listener is a separatehttp.Serveron--preview-addr, next to the main, agent-socket and OneCLI-dashboard servers. It routes every request by itsHostheader (lowercased, port stripped). Only<label>.<preview-domain>with a label that is currently held gets anywhere near a tunnel. The bare domain, any other host, a dotted multi-label name and a released name all get the same 404 page. A held name with no live tunnel (labctl reconnecting) gets 503 "the preview's tunnel is reconnecting". A live tunnel whose port refuses gets 502 "nothing is listening on port N in the run". The pages are short, escapedtext/htmland markedno-store, so a browser never caches a 503 over a preview whose tunnel has come back. Upstream requests keep the browser'sHostand carryX-Forwarded-For,X-Forwarded-HostandX-Forwarded-Proto. Hop-by-hop headers are stripped (httputil.ReverseProxywithRewrite). - No authentication on the preview listener. This was the maintainer's explicit call: reachable equals allowed. It is the stance the LAN path to lab itself already takes. There is no login, token or cookie check, so anyone who can reach the listener's address can open every live preview. The operator controls who that is by where the listener binds and which network the wildcard DNS name resolves on. If this ever changes, the knob is a future
--preview-authoption with a capability-link mode (an unguessable per-exposure link instead of a session check). It is named here and not built. - Opt-in. Nothing listens until
--preview-addris set, the same as the OneCLI dashboard'sportmode (ADR-0067). The operator has to add wildcard DNS anyway, so an explicit switch is honest. While it is off,labctl exposerefuses with 404previews are not enabled on this lab (no preview listener configured), and the worktree notes leave out the Previews section. - The default domain is
lab.internal..internalis ICANN's reserved private-use TLD..localwas rejected: Apple platforms resolve it by mDNS first, and systemd-resolved never sends it to unicast DNS, so a wildcard record in the LAN's DNS server would not be consulted.--preview-domainis validated as a hostname at boot, even while previews are off, so a typo shows up then and not on the day previews are switched on. The printed URL ishttp://<name>.<domain>, with the listener's port appended unless it is 80. - The registry is in memory, with no migration. It maps each name to a run. A tunnel cannot survive a lab restart (the hijacked connection dies with the process), and a surviving run's labctl re-registers when it reconnects. Persisting names would only create stale reservations. Lab starts with an empty registry, so after a restart a preview answers 404 until its labctl reconnects and re-registers. A connection that drops while lab keeps running leaves the name held, and the host answers 503 until labctl re-attaches.
- The run-end chokepoint releases a run's exposures. Every site that deletes a run's tokens today also closes the run's exposures, through one helper: manual Stop (
internal/instance/stop.go), the AFK reaper (internal/afk/reaper.go), AFK Stop (internal/afk/stop.go) and the parked reconciliation (internal/reconcile/parked.go). The live tunnel getsrun-ended, its labctl exits, and the names become free. Deleting the run token is what ends a run's access to the agent API, so this is the same moment its exposures end. - The operator sees exposures where they see the run. The run's API representation (
/api/v1/runs,/api/v1/runs/{id}) carriesexposures(name,url,port,live). Every register, attach, detach and stop publishesrun.changed. The chat header shows a Preview chip while the run has at least one exposure, in the exposed-secrets badge pattern: an icon below 640px, a text chip above, a menu when there are several. Run details lists every exposure with its URL and live state. - The agent learns the verb from its worktree notes. When the preview listener is configured, the generated
CLAUDE.local.mdgains a## Previewssection. It gives the twolabctl exposelines, the URL shape, the 8-per-run and DNS-label rules, the fact that traffic goes to127.0.0.1:<port>(else[::1]:<port>) inside the run, and the Vite notes (server.hmr.clientPortbehind a public port other than 5173,server.allowedHostsfor the preview domain). - Plain HTTP for now. The listener serves no TLS. A lab-minted CA, TLS on the listener, the
:80redirect, the bind capability in the NixOS module and the CA download are a follow-up issue.
Status¶
Accepted 2026-10-18. Resolves issue #105. Decided with the maintainer in a grill session on 2026-10-18; the issue's agent brief is the contract.
Amended by ADR-0085 (issue #106, 2026-10-18): the "Plain HTTP for now" decision no longer holds. The preview listener terminates TLS and previews are served at https://<name>.<preview-domain> (port appended unless 443), with an optional --preview-redirect-addr, --preview-cert-file/--preview-key-file, a lab-minted CA, and in the NixOS module preview.enable as the switch in place of listenAddr. The tunnel, routing and no-authentication decisions stand.
- ADR-0052: unchanged. The network pins and the podman argv are untouched, and a container run still reaches lab only through the mounted agent socket, which the tunnel uses.
- ADR-0067: the OneCLI dashboard's
portmode is the precedent for an opt-in second listener. The preview listener differs on purpose: the dashboard's listener sits behind lab's session gate, and the preview listener has no authentication. - ADR-0068: unchanged. Warpgate stays the SSH bastion. Its HTTP targets were considered for previews and rejected (below).
Considered options¶
- Publish ports at spawn time (
podman run -p, one host port per run or per declared app). Rejected. It changes ADR-0052's network pins, which exist precisely so that a run has no route to the host other than the agent socket. It needs port allocation state that has to survive restarts. It does nothing for host runs. And it only works if the dev server binds the container's external interface, while most dev servers bind loopback by default. - Lab reaches in via
podman exec(lab runs a forwarder inside the container on demand). Rejected. It works for container runs only, since a host run has nothing to exec into. It also inverts the direction lab has kept everywhere else: a run talks to lab, and lab does not reach into a run. - pasta's port auto-forwarding (
-t auto, which forwards whatever the guest binds). Rejected. It changes the network ADR-0052 pins, which runs pasta with port forwarding off (-t none,-u none). It is container-only, and it exposes every bound port to the host, not the one the agent chose. - Warpgate HTTP targets. Rejected. Warpgate is the SSH bastion (ADR-0068), with no role in HTTP. Every preview would need a target created in Warpgate's admin state per run, which is the per-target admin state lab avoids creating. The browser would face an extra authentication hop through Warpgate's own login. And Warpgate would still need a route to the dev server, which a container run's loopback does not offer, so it would not cover both Runners.
- A path prefix under lab's own origin (
https://lab.example.com/preview/<name>/…). Rejected. Dev servers assume they own/: absolute asset URLs, client-side routers and the HMR WebSocket path all break under a prefix unless each app is configured with a base path. Worse, every preview would share lab's origin, so a previewed page's script could read lab's cookies and storage and call lab's API as the operator. The dashboard'ssubdomainandportmodes rejected the same shape for the same reasons. - HTTP/2 in reverse (
x/net/http2over the hijacked connection instead of yamux). Rejected. WebSocket over HTTP/2 (RFC 8441, extended CONNECT) is supported unevenly, and HMR is the main reason the feature exists. yamux carries plain HTTP/1.1 streams, so an upgrade needs nothing special. - Lab-derived names (a slug from the run ID or the repo). Rejected on the maintainer's call. The agent cannot announce an unreadable URL usefully, and a fixed scheme leaves no room for two apps in one run.
- Release a name when its connection drops. Rejected for the reconnect-window race above.
- A background daemon with pidfiles (
labctl exposereturns at once, and something else forwards). Rejected. It needs process discovery that works the same on both Runners, and it leaves orphaned forwarders when the agent forgets them. A foreground process the agent puts in the background has neither problem. .localas the default domain. Rejected for mDNS (above).- Persist the registry. Rejected. The tunnel is gone after a restart either way, and a persisted name could only be a stale reservation.
Consequences¶
- An agent in either Runner can show the operator what it is serving with one backgrounded command, and the operator opens it from the chat header. Nothing about a run's network changes, and a dev server keeps its default
localhostbinding, whether that resolves to127.0.0.1or::1. - Anyone who can reach the preview listener can open every live preview. That is the decided stance, and the operator owns it: bind
--preview-addrto a trusted interface (a LAN or VPN address, not a public one), and serve the wildcard name only on the network that should see previews. A previewed app is whatever the agent is running, and it is reachable with no login while the exposure lives. A host run can reach the listener from inside the run, since it shares the host network. A container run can reach it only by raw IP on a host address ADR-0052's--add-hostpins do not shadow, the residual that ADR already admits for every wildcard-bound host service. Either way, a run can open another run's previews. The future--preview-authknob is where this changes. - The operator must provide a wildcard DNS record for
*.<preview-domain>pointing at the lab host. Lab cannot do that for them, and without it the printed URL resolves nowhere. - Previews are plain HTTP until the TLS follow-up lands. Browser features that need a secure context (service workers,
crypto.subtle, clipboard writes) do not work on a preview page served from a non-localhost host. - A lab restart interrupts every preview. The registry starts empty, so each preview's host answers 404 until its labctl reconnects and serves again with no agent action. A dropped connection without a restart keeps the name held, and the host answers 503 until labctl re-attaches.
hashicorp/yamuxjoins the dependency set (andnix/package.nix's vendorHash).internal/previewowns both ends of the protocol and imports nothing of lab's own, so labctl can link it and stay small.- A new site that ends a run without deleting its tokens would leak exposures. The chokepoint is the token delete, so a fifth run-end site must call the same helper.