Skip to content
Pages

Architecture

Parcela turns one checkout's configuration into a supervised environment. It owns identity, process lifecycle, and reservations. A dedicated Caddy instance owns TLS and HTTP routing. The project owns commands, preparation, and data policy.

Ownership

Component Responsibility
CLI Finds configuration, selects an environment, and renders text or JSON.
Registry Stores projects, environments, sessions, names, and port leases in SQLite.
Environment owner Holds the ownership lock, runs the process graph, records state, and handles lifecycle requests.
Shared proxy process Supervises Caddy and refreshes routes for running environments.
Caddy Terminates TLS and routes requests using private configuration, storage, and an admin socket.
Project Defines commands, dependencies, hooks, and external effects in parcela.yaml.

Checkout and data ownership

create delegates checkout creation to the repository's VCS, then registers its preview identity. It never starts the process graph. discovery remains read-only. Source creation and data restoration are separate operations; failures retain any created checkout with an explicit error rather than deleting source.

Snapshot policy is parsed from data in parcela.yaml. It is not serialized into the supervised graph: the proxy and environment owners need process configuration, not permission to copy data. Snapshot metadata stores the data policy separately. snapshot and restore take the environment's restart, owner, and task locks and require a stopped session. Data is staged in a private directory and published with an atomic no-replacement rename. SQLite uses its backup API, never a live main-file/WAL copy.

The CLI accepts only standard input, output, and error as inherited descriptors. Before starting threads it closes other inherited copies, so a caller's temporary socket or ownership lock cannot survive in Parcela or its children.

Startup and readiness

parcela up performs these steps:

  1. Find and validate the nearest parcela.yaml.
  2. Register the checkout on first use. A main checkout gets the project URL. A linked jj workspace or Git worktree gets a preview name.
  3. Reserve service hostnames and lease backend ports from 20000 through 29999 (the host's ports.backend changes the range), including each port a compose service's containers publish. Port leases use one transaction.
  4. Start the shared proxy if HTTP endpoints are configured. Watcher-only environments skip the proxy and trust setup.
  5. Build the process graph. Start hook dependencies, setup if due, prestart, and the remaining services in dependency order.
  6. Wait for hooks to complete, services to start, and explicit readiness checks to pass. For HTTP services, confirm that listeners belong to the graph and readiness paths return 2xx.
  7. Publish the environment's HTTP routes, if any. A detached start returns when its owner reports readiness.

The owner holds owner.lock, serves its control socket, and saves owner.json before any process starts. Each command in the graph runs under bash -c, or sh -c without bash, in a separate process group. Each process writes its own log. parcela task and hooks.teardown run outside the graph under /bin/sh -c, with the latest run's variables, or those of no run yet.

A dependency is ready after its readiness check passes, after it starts if it has no check, or after it exits 0 if it is a hook. Setup enters the graph only until it has succeeded. Ordinary services do not restart automatically. Sharing publishers can restart up to 10 times after failure.

Shutdown and recovery

down asks the owner to withdraw routes and stop the graph. Once nothing that depends on a service is running or stopping, the owner runs the service's stop command, if it has one, then sends SIGTERM to its process group. After 10 seconds, remaining groups receive SIGKILL. A stop command runs even when its service already exited, because it manages what lives outside the process tree, like containers. Each success is saved in owner.json, so an owner that adopts a half-stopped run runs only the commands still owed. The run is stopped only when no recorded process is alive and every stop command succeeded. A failed one leaves the run interrupted until down runs it again.

restart validates the replacement configuration before stopping the old run, then starts a new generation. Sharing belongs to that generation. Sharing defaults come from sharing in the host configuration (parcela config), never the project; CLI and environment values add to or override them. --no-share disables all sharing for the new generation. Detached owners receive the resolved sharing flags without reapplying defaults.

If the owner dies, services may survive. The next lifecycle operation acquires the ownership lock and compares recorded PIDs, start times, and boot identity with live processes. Matching processes are adopted. Parcela never signals a bare PID from disk. A small check-to-signal PID reuse race remains possible.

Proxy and route leases

The background parcela proxy run process supervises one Caddy instance. Every second it renews a lease file per active route; Caddy matches a route only while the file for the current second exists, so routes expire five seconds after the last renewal even if the proxy process dies. Owners publish and withdraw their routes at once; renewal only keeps them alive.

Listener ownership is checked with ps and lsof. An owner checks its own process tree and listeners every 200 ms while starting and every 2 seconds once ready, re-entering the starting phase (and withdrawing its routes) when a listener changes. The proxy re-checks every routed listener at most every 5 seconds.

ps and lsof can miss their deadline on a loaded machine, so a failed check is retried. After 30 seconds of failures a start fails, but a run that was ready keeps its processes and loses only its routes until the checks work again; its readiness timeout restarts then.

Status requests do not probe a slow app inline. While someone keeps asking, such as the dashboard every 2 seconds, the owner checks each service's health URL every 2 seconds on its own thread, and a reply reuses a healthy answer up to 3 seconds old. The checks stop 10 seconds after the last request, so an idle app's log stays quiet.

If the proxy dies, environment owners attempt to restart it within about two seconds and republish routes. An explicit proxy stop leaves a stopped marker that prevents this recovery. App services keep running while the proxy is stopped.

The proxy does not start at login or reboot. The next up starts it. By default it listens only on 127.0.0.1:1443. A busy listener produces an error naming its owner.

CLI updates and proxy refresh

Normal foreground CLI commands reconcile an older running proxy with the current executable. This is local maintenance even for an overview command; it never starts a deliberately stopped proxy. Background owners, shutdown commands, and explicit diagnostic commands skip this step, so an old owner cannot replace a newer proxy after the CLI on disk is upgraded.

Refresh holds the existing start lock across stop and launch. Concurrent foreground commands converge on one replacement; an explicit stop that wins the lock stays stopped. Refresh verifies Caddy before withdrawing routes and preserves the running front door, state, and private CA. Existing environment owners rebind to the new router token and republish routes. An owner still running an older release renders routes with its own code whenever it publishes: when it rebinds, and when its readiness or listeners change. The proxy notices each such load and replaces it with its own rendering within one sync, so until those owners restart, each of their publishes is briefly served as they rendered it. Restart environments after upgrading. App processes and hooks are not restarted. A proxy with a newer protocol or release version is preserved.

Interactive installed CLIs check GitHub's latest stable release at most once a day. Check results and prompt time live in private update-check.json, with a lock preventing concurrent prompts. Accepting a shell-installer update runs the installer and then executes the original arguments with the new binary. The installer invokes a maintenance-only proxy refresh command and respects the selected state directory. JSON, pipes, and internal commands never perform a network update check. Release checks can be disabled independently of local proxy maintenance with PARCELA_NO_UPDATE=1.

Browser inventory

The shared proxy also owns a dashboard at parcela-dashboard.localhost, on its existing loopback HTTPS listener. That hostname is reserved when adopting service names. Caddy forwards dashboard requests to a private Unix socket; the dashboard adds no public TCP listener and never exposes Caddy's admin API or owner control sockets.

An inventory worker reads the registry and probes live owners outside the route-renewal loop. Owner probes share a short budget; missed replies appear as unknown. A separate worker serves a cached, allowlisted browser response with a timestamp. It omits saved sessions, owner credentials, commands, and environment variables. Checkout discovery remains read-only. The CLI overview and dashboard share the inventory collector.

The dashboard never frames an app, and Parcela never relaxes an app's CSP. Its page and CSS are built from lostcoords-ui while developing Parcela, compiled into the binary, and served only on its own origin, so app pages load none of it; they get only the self-contained badge script. The dashboard's own name and full control of /_parcela/* exist only on the loopback HTTPS server. Domain names get a read-only part of that path while badge is on, and LAN names a smaller one behind the device gate; with badge off the path is the app's own on both, except /_parcela/certificate and /_parcela/ca.crt on LAN names. Tailscale still reaches app backends directly.

Only the dashboard's own name lists devices and decides about them, at /_parcela/devices, with the same-origin and X-Parcela-Action rule below.

Every loopback app route also sends /_parcela/* to the dashboard socket, with Caddy setting X-Parcela-Environment, X-Parcela-Hostname and X-Parcela-Access: local. Caddy always sets them, and the dashboard name deletes them, so a client cannot claim an environment; the dashboard refuses app requests without local or shared access. That path serves the debug menu script; its own environment's opened URLs, the opened files and the page assets beside them, never dotfiles; the inventory and process logs of registered environments; and start, stop and restart, which the proxy runs as background parcela --workspace ID CLI commands. Actions name registered environments only, so a page never starts a checkout nobody has started. They ignore the share variables in the proxy's environment, but the host's sharing settings apply, as for any up. Actions also require a same-origin request with X-Parcela-Action, so a cross-site page cannot trigger them. An app page lists, manages, and reads the logs of only its own environment; /_parcela/ on parcela-dashboard.localhost, whose environment headers Caddy deletes, manages every one. parcela browse writes those requests to opens.json in the runtime directory under a lock, keeping the last 20.

These checks stop other websites, not other programs. Anything on this machine that can reach the loopback listener can send the same headers as a page, then read any environment's logs or start and stop it. The same goes for other users on a shared machine. Treat the loopback listener as trusted by everything local.

While nothing serves a loopback name, its page loads get a 503 page from the same socket, with the same headers, showing that environment's state, a Start or Restart button under the same action rules, and links to its logs and the dashboard. It never starts anything by itself, and reloads once the app answers. Other requests get a plain 503. LAN and domain names are unchanged.

The badge on app pages

Stock Caddy cannot edit response bodies, and a custom build would rule out proxy.caddy. So on loopback, domain and LAN app names Caddy sends one kind of request through a small injector in the proxy process, over a second private Unix socket: a GET with Sec-Fetch-Dest: document, which is a top-level page load. Assets, API calls, WebSockets, frames and clients that send no fetch metadata go straight to the app. On a LAN name the device gate runs first, so only an approved device's page loads reach the injector, which forwards the vouching headers and drops the pairing cookie like the app's own route. Tailscale names have no injector route.

For those requests the injector asks the app for an uncompressed page without conditional or range headers, then adds <script defer src="/_parcela/widget.js"> before </head>, or before <body when there is no </head>. Comments, quoted attribute values and raw-text elements such as <script> do not count as those tags. A page with markup but neither tag gets the script at its end. The script reuses the nonce of the response's CSP header when its script policy has one. A tagged response loses Content-Length, ETag, Last-Modified and other headers that describe the app's bytes, and streams to the browser as it arrives. Anything other than a whole, uncompressed text/html page passes through unchanged, as does a page with no place for the tag within its first 1 MiB or 5 seconds.

Caddy names the backend in X-Parcela-Upstream, replacing any value from the client. The injector accepts only loopback addresses and removes the header before the app. badge: false in the host configuration removes these routes within a second or two, with no restart.

A domain name can be reachable by other people, so its /_parcela/* carries X-Parcela-Access: shared and is read-only: the script and its icon, a menu page, this environment's state, and the same project's ready environments with URLs under the page's own domain. Rows carry no environment IDs, processes or checkout paths. Actions, logs and opened files, which can hold secrets and sign-in links, answer 403. Caddy replaces the access header, so a client cannot claim local. A LAN name's /_parcela/widget.js, /_parcela/badge.png and /_parcela/lan pass the device gate and then carry X-Parcela-Access: lan: the script, without the dashboard's address, its icon, and this environment's name and state. Every other request with that access answers 403, and the name's other /_parcela/ paths are the app's. A sibling's URL assumes that run shares the same domain; one started with --no-share answers with Parcela's 404.

Sharing for one run

LAN sharing records the selected interface address with the run. Caddy adds a server on that address and the HTTPS port for the shared environment's .local names. Each of its routes first asks the device gate: a forward_auth request over the dashboard socket with X-Parcela-Access: device, the .local name and the client's address, which Caddy sets and the client cannot. The gate looks up the parcela_device cookie's SHA-256 hash in proxy/devices.json. An approved device gets 200, and Caddy proxies its request to the app without that cookie and without the headers that name a client's address: X-Forwarded-For, Forwarded, X-Real-IP and similar ones, Caddy's own and the device's. X-Forwarded-Proto and X-Forwarded-Host stay. The app sees a loopback request, which is the trust approval grants: an approved device is as trusted as this machine. Anything else gets the gate's own answer: a page that registers a pending request, offers the root certificate and checks every 3 seconds without reloading until approval, or a plain 403 for requests that are not page loads. A proxy older than protocol 10 serves no LAN names, rather than serve them ungated. Ahead of the gate, the LAN server sends /_parcela/certificate and /_parcela/ca.crt to the dashboard socket with X-Parcela-Access: certificate, which answers only those two paths: a page with the root certificate's fingerprint and install steps, and the root certificate in DER. It reads root.crt and never the key. The LAN listener redirects plain HTTP to HTTPS before any route runs, so these paths need the browser's warning accepted once. It serves those names only while the interface still holds the address. The run's .local URLs carry that port, unless the LAN redirect targets it on the interface holding the address; then they use 443. If the LAN listener cannot load, the proxy retries configuration without it so loopback routes can remain available.

mDNS publishers use dns-sd on macOS or avahi-publish on Linux. Tailscale sharing chooses free HTTPS ports from tailscale serve status and starts one foreground serve process per service. Tailnet requests reach backend ports directly, so they pass no device gate and keep their client address. Domain names have no device gate either.

Domain sharing records the domains and one loopback port with the run. Caddy adds a plain-HTTP server on 127.0.0.1 at that port for <name>.<domain> under each domain, trusting forwarded headers from loopback. Any local process can therefore send those headers; the domain listener is no stronger a boundary than loopback itself. The person's own proxy terminates TLS in front of it, so Parcela never holds a public certificate or DNS credentials. Like the LAN server, a domain listener that cannot load is dropped so loopback routes remain available.

Publishers belong to the process graph and stop with the run. Foreground Tailscale serve entries disappear when their owning process exits, including after SIGKILL. Parcela never uses Funnel.

Durable state and sockets

The state directory follows this precedence:

  1. --state-dir.
  2. PARCELA_STATE_DIR.
  3. $XDG_STATE_HOME/parcela.
  4. ~/.local/state/parcela.

The directory has mode 700. It contains the following records:

Path under state Contents
registry.sqlite3 Projects, registrations, names, and port leases.
proxy/ Caddy configuration, frontdoor configuration, logs, certificates, and the CA.
environments/<uuid>/ Context and owner records, hook markers, logs, data, temporary files, and cache.

Control and Caddy admin sockets use a hash of the state path under a per-user temporary directory. The base is parcela inside macOS's per-user temporary directory or /tmp/parcela-<uid> on Linux. Socket placement does not depend on environment variables. This keeps processes in agreement and paths within Unix socket length limits.

rm removes registration, not source, and runtime files only with --data, after the registration is gone. rm --orphans finds runtime directories no registration owns; it lists them before the registry is read, since up registers an ID before creating its directory, and refuses without a readable registry. A configured teardown hook can change external data. Read-only inspection does not register a checkout or start processes.

Source map

The crate is grouped by subsystem. The groups organize the code; they are not strict layers, since an environment publishes routes and the proxy reads environment status.

Group Module Responsibility
src process, http, paint Process identity, groups, signals and listener ownership; a small HTTP client for readiness and Caddy's admin socket; terminal color.
project config Parses format version 1 and validates manifests.
compose Runs a compose service: reads the ports its containers publish from docker compose config on up, writes the environment's Compose override, and forwards a compose.http service's $PORT to its container, so routing still reaches only listeners the graph holds.
startup Chooses the identity a checkout registers with: the project name or a preview name.
discovery Reads jj and Git checkout inventory without modifying it. Unresolvable jj roots are reported and omitted without hiding live checkouts.
checkout Creates explicit Git worktrees or jj workspaces without starting services or copying data.
snapshots Saves declared environment data with SQLite backups and checksums; restores only to absent data directories.
state registry, ports Stores identity and reservations, leases backend ports.
host Reads and writes the machine's own settings, parcela config.
private, sockets Owns private files, locks, and socket placement.
context Reports an environment's resolved context for parcela context.
environment launch Validates up and run, leases ports, records the session and starts an owner.
variables Builds the variables every process, hook and task sees, and prepares the runtime directory.
owner, record, health, echo The owner loop: readiness, route publication, control requests and shutdown; owner.json; cached health checks; process output on the terminal or owner.log.
supervisor, logs Runs the process graph and retains process output.
control, lifecycle The owner's control socket, and the client side of down, restart, status, logs and detached starts.
recovery Adopts a run whose owner died, or marks it stopped once nothing of it is left.
tasks Runs parcela task and hooks.teardown.
inventory Collects read-only status of every environment for the CLI overview and the dashboard.
autostart Installs the per-user login service and finds environments a reboot interrupted.
proxy mod Starts, stops, refreshes and revives the proxy, and installs or removes CA trust.
daemon parcela proxy run: owns Caddy, the dashboard and the injector, and renews route leases.
router Builds Caddy configuration and route leases, and loads them over the admin socket.
frontdoor, relay The proxy's HTTPS port, the optional 443/80 relay, and the LAN redirect.
devices Pairs LAN devices: the gate Caddy asks for each .local request, and decisions.
dashboard, inject, opens Serves the local browser dashboard, switcher and app debug menu; adds the badge script to HTML page loads on loopback, domain and LAN names; records what processes hand to BROWSER.
share Plans LAN, Tailscale and domain sharing and allowed host variables.
cli Parses arguments, selects commands, and renders output.

Privileged changes

CA trust changes occur only through parcela trust or a first terminal up, behind the system password prompt. Nonterminal runs never change trust.

parcela autostart enable installs a per-user service (a LaunchAgent, or a systemd user unit) that runs parcela autostart run once at login. It runs up --detach for each environment whose last session was running but whose owner is gone, which is what a reboot leaves; down marks a session stopped, so stopped environments stay stopped. It needs no root.

parcela relay install is the only system-service installation path. The service manager owns loopback port 443 (and 80 when free) and relays connections as the user: launchd with nc on macOS, systemd socket units with systemd-socket-proxyd on Linux. Parcela code does not run as root.

parcela relay install --lan adds a packet-filter redirect, also behind the password prompt. On one interface, connections to its address on 443, and on 80 when nothing answers there, go to the proxy's port on the same address, which only the LAN listener holds. Loopback is never a target, so unshared environments stay off the LAN. It is macOS only. Root's launchd loads pf rules into the com.apple/parcela anchor at boot with pfctl -E; the stock /etc/pf.conf evaluates that anchor and is not edited. The rules are plain rdr, without pass, so filter rules still see the redirected connections. pf does not redirect this Mac's own connections to its LAN address, so on the machine itself the port-less .local URL does not connect; <name>.localhost serves it there. The install checks the rules with pfctl -n before the password prompt and removes the rules file and daemon if a later step fails, since a rules file left behind makes .local URLs drop the port. A program that starts listening on that address's 443 after the install is hidden by the redirect.

For contributor commands and live verification, see Contribute to Parcela. For site checks, see Build the documentation site.