edge-roll-blip: revise design spec after round 1 (INV-R, D10b)
P1-1: state INV-R and enforce it in one place: a persistent drop-in on carlos-edge.service (E-start) that stops the socket before any start whose on-disk binary fails edge-socket-check. The socket's own gate refuses to start until that drop-in is loaded, and only this build writes it, so activation is two-step and no old applier, recovery or unit set ever meets an active socket. Every mix row names its enforcement point; an INV-R simulation joins the test plan. P1-2: D10b holds probed routes with a local upstream, waiting for prober readmission (localDialHold + rise_after x interval, cap 30 s) without dialing an ejected upstream. Console route shape settled from the repo: either, and both are covered. P2s: D5 compatible/absent/incompatible listeners with the gate refusing non-shipped argv; v0.25.0 conditional floor corrected and no baseline relied on; W2 binary paths and assertions; fd store Preserve=yes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+463
−360
docs/superpowers/specs/2026-10-27-edge-roll-blip-design.md+463 −360
Intent
diff --git a/docs/superpowers/specs/2026-10-27-edge-roll-blip-design.md b/docs/superpowers/specs/2026-10-27-edge-roll-blip-design.md| index e84694215..6ce701464 100644 |
| --- a/docs/superpowers/specs/2026-10-27-edge-roll-blip-design.md |
| +++ b/docs/superpowers/specs/2026-10-27-edge-roll-blip-design.md |
| @@ -1,21 +1,26 @@ |
| # Edge roll blip (#434 part B): design |
| -Status: draft for review, 2026-10-27, branch `edge-roll-blip`. Rulings are the brief's |
| -(`.superpowers/sdd/2026-10-27-edge-roll-blip/BRIEF.md`) and are not re-argued. Review |
| -rounds: none yet. |
| +Status: revised for review round 2 of at most 3, 2026-10-28, branch `edge-roll-blip`. |
| +Rulings are the brief's (`.superpowers/sdd/2026-10-27-edge-roll-blip/BRIEF.md`) and are |
| +not re-argued. Round 1 (`e0b2877a`): NOT READY, 2 P1, 4 P2. Fault families named there: |
| +**INV-R** (rollback compatibility, P1-1) and **INV-H** (restart admission, P1-2). A |
| +round-2 finding in either family stops the review for Paul's choice (AGENTS.md rule 2). |
| +Dispositions are in §11. |
| Goal: a platform roll (`carlos system update` restarting `carlos-edge.service`; root's |
| adopt restarting `console.service`) is a short queued wait for clients, with no refused |
| -connection and no 502 caused by the restart itself. |
| +connection and no 502/503 caused by the restart itself. |
| In one paragraph: a `carlos-edge.socket` unit holds :80 and :443 in PID 1, and the edge |
| takes them over through `LISTEN_FDS`, binding them itself when it is handed none (D1, D5). |
| -The socket is enabled but never started by an arm list. The service only `Wants=` it, |
| -and its `ExecStartPre=` asks the binary on disk whether it understands the socket (D2-D4). |
| -Rollback needs no change to old binaries: every apply since v0.18.0 journals |
| -`UnitsAdded`, and its rollback and recovery run `disable --now` on those units before they |
| -restart the edge (§4). Dials to loopback TCP upstreams get the 13 s refused-retry that |
| -unix-socket upstreams already have, so a console restart queues instead of 502ing (D10). |
| +One invariant makes every binary/unit mix safe (INV-R, D4). It is enforced in one place: a |
| +drop-in on the edge service that, before every start of the edge, stops the socket unless |
| +the binary on disk understands it. The socket refuses to start until that drop-in is |
| +loaded, and only code from this build on writes the drop-in. So activation takes two |
| +rolls on an existing box (§4), and no old binary, old applier or old unit set can ever meet |
| +an active socket. Local upstreams are held for a bounded window instead of 502/503: |
| +unprobed loopback routes retry the dial (D10), and probed routes wait for a live upstream |
| +(D10b). |
| ## 1. Evidence |
| @@ -24,140 +29,134 @@ unix-socket upstreams already have, so a console restart queues instead of 502in |
| There are two, both TCP, both bound by the agent process itself. `-http` defaults to `:80` |
| (`internal/edge/edge.go:3550`) and serves ACME HTTP-01 and the redirect |
| (`httpSrv.ListenAndServe()`, `:3819`). `-https` defaults to `:443` (`:3549`) and serves |
| -routes (`httpsSrv.ListenAndServeTLS("", "")`, `:3826`). Dev mode binds `-http` only |
| -(`:3646`). The unit passes no flags (`ExecStart=/opt/carlos/carlos agent`, |
| -`internal/host/units/carlos-edge.service:19`), and `carlos agent` reaches the same function |
| -through `edge.RunWithActivatorReady` (`internal/host/agent.go:1025`). |
| - |
| -Nothing else in the edge process is public. The edge door, the purge channel and |
| -`agentsock` are unix sockets (`internal/edge/door.go:182`, `purge_listen_unix.go:36`, |
| -`internal/agentsock/agentsock.go:286`). Backhaul dials a peer's :443 (`edge.go:3202`) and |
| -has no listener of its own. There is no UDP, HTTP/3, metrics or admin port. A grep finds |
| -no `SO_REUSEPORT` anywhere in `internal/` or `cmd/`. The relay's UDP and TCP listeners |
| -(`internal/relay/server.go:307,321`) belong to `carlos-relay.service`, not to the edge. |
| +routes (`:3826`). Dev mode binds `-http` only (`:3646`). Both come only from argv: there is |
| +no environment override. The unit passes no flags (`ExecStart=/opt/carlos/carlos agent`, |
| +`internal/host/units/carlos-edge.service:19`); a guarded box replaces it with |
| +`/opt/carlos/system/guard/carlos system guard-exec edge` (`internal/host/guardarm.go:166`), |
| +which execs `/opt/carlos/carlos agent` (`guardexec.go:90-91`). |
| + |
| +Nothing else in the edge process is public. The door, the purge channel and `agentsock` |
| +are unix sockets (`internal/edge/door.go:182`, `purge_listen_unix.go:36`, |
| +`internal/agentsock/agentsock.go:286`). Backhaul only dials (`edge.go:3202`). There is no |
| +UDP, HTTP/3, metrics or admin port, and no `SO_REUSEPORT` in `internal/` or `cmd/`. The |
| +relay's listeners (`internal/relay/server.go:307,321`) belong to `carlos-relay.service`. |
| ### 1.2 Listen-first (#281) and the admission latch |
| The edge configures fully before it binds: the cert mirror's synchronous refresh |
| -(`edge.go:3686`, up to 20 s, `certsync.go:423`), the home map (`:3699`, 20 s, |
| -`homemap.go:125`), the door (`:3712`) and the TLS chain (`tlsCfg.GetCertificate = getCert`, |
| -`:3793`). Only then does it serve (`:3814-3830`). #281 moved the agent's slow startup (sweep, layout |
| -recovery, moves, runs) into the background, because "the edge below must listen within the |
| -update gate" (`agent.go:646-650`). That startup is behind the `started` latch, which `Ready` |
| -closes (`agent.go:701`). Every activator-owned request waits on the latch in `awaitStartup` |
| -(`internal/activator/activator.go:1572`), bounded by `wakeWaitBound = 30s` (`:1141`, used |
| -at `:5804-5823`), and gets a 503 with `Retry-After: 5` if the bound runs out. On SIGTERM |
| -both servers `Shutdown` with a 10 s drain (`edge.go:3835-3842`). |
| - |
| -### 1.3 How units reach a box, and how the edge is restarted |
| - |
| -**Where unit files come from.** They are embedded in the binary |
| -(`internal/host/bootstrap.go:36`; `units/*.socket` is included), and the binary's own |
| -`carlos bootstrap` writes them. `enabledUnits` (`:167-173`) says what gets enabled. |
| -`armedUnits` (`:218-227`) says what also gets started, and today that includes every |
| -`.socket` (`:222`). |
| - |
| -**Fresh box.** As root: stage the binary and `host.env`, run `carlos bootstrap` |
| -(daemon-reload, `enable`, then `start` of the armed set), then |
| -`systemctl start carlos-edge` (`infra/modules/region-host/README.md:67-75`). |
| - |
| -**Update.** `carlos-system-update.path` runs `carlos system apply` as root, from the binary |
| -installed at that moment, which is the OLD one (`units/carlos-system-update.service:47`). |
| -`install` (`internal/host/systemapply.go:551`) then: (1) stages both builds' unit sets with |
| -`bootstrap -root <dir> -plan-json` (`stagePlan`, `:1259`); (2) journals |
| -`UnitsAdded`/`UnitsRemoved` (`:610`); (3) renames the candidate over `/opt/carlos/carlos` |
| -(`:632`); (4) runs `installUnits(newDir, UnitsRemoved)` (`:646`), which does `disable --now` |
| -for each removed unit (`:1388-1390`), copies, runs sysusers and tmpfiles, and |
| -`daemon-reload` (`:1478`); (5) restarts instance-control, then the edge (`:658`), where |
| -`restartEdge` is `reset-failed` then `systemctl restart carlos-edge` (`:1490-1494`); (6) |
| -gates: within 20 s (`:59`) the edge is active, answers `http://127.0.0.1/` under a 3 s |
| -client timeout (`:214-224`) and reports the target, then a soak keeps `NRestarts` at 0 |
| -(`:1569-1614`); (7) `finish`, whose `arm` starts the new plan's `Armed` list |
| -(`:1728-1764`) and already refuses to start the edge (`:1751`). |
| - |
| -**Gate rollback.** `rollback` (`:1776`) calls `restoreFiles` (`:1849`). `restoreFiles` |
| -renames `carlos.prev` back (`:1892`), then runs `installUnits(prevDir, j.UnitsAdded)` |
| -(`:1927`), which runs `disable --now` on every unit the failed build added. `rollback` then |
| -runs `restartEdge` (`:1800`) and gates the previous build. |
| - |
| -**Recovery.** `carlos-system-recover.service` runs once per boot, |
| -`Before=carlos-edge.service`, and as the update's `OnFailure=` |
| -(`units/carlos-system-recover.service:34,41`; `carlos-system-update.service:41`). With a |
| -journal open it runs `carlos.prev system apply --recover`. `recoverIfNeeded` (`:1996`) |
| -calls the same `restoreFiles`. At runtime it then issues `restart --no-block` on the edge |
| -(`:2082`); at boot it leaves the start to systemd. |
| - |
| -**Every build a box can roll back to has this machinery.** `UnitsAdded` and its |
| -`disable --now` date from `1c33565d`/`6017307a` (2026-09-06, first in v0.18.0). The layout |
| -floor already refuses anything below v0.24.2 (`systemapply.go:568-588`). |
| - |
| -**The belt pass leaves the edge alone.** `ensureSystemUnits` |
| -(`internal/host/systemunits.go:53`) arms a fixed list (`:28,32`), and neither edge unit is |
| -on it. |
| +(`edge.go:3686`, up to 20 s, `certsync.go:423`), the home map (`:3699`, 20 s), the door |
| +(`:3712`) and the TLS chain (`:3793`). Only then does it serve (`:3814-3830`). #281 moved the |
| +agent's slow startup behind the `started` latch (`agent.go:646-650,701`). Activator-owned |
| +requests wait on it in `awaitStartup` (`internal/activator/activator.go:1572`), bounded by |
| +`wakeWaitBound = 30s` (`:1141`), then get 503 with `Retry-After: 5`. On SIGTERM both |
| +servers `Shutdown` with a 10 s drain (`edge.go:3835-3842`). |
| + |
| +### 1.3 How binaries and units reach a box |
| + |
| +**Units** are embedded in the binary (`internal/host/bootstrap.go:36`, `units/*.socket` |
| +included). `carlos bootstrap` writes them; `-root <dir>` writes them into a staging tree |
| +instead. `enabledUnits` (`:167-173`) is what gets enabled; `armedUnits` (`:218-227`) is what |
| +also gets started, today including every `.socket` (`:222`). |
| + |
| +**`carlos system update`.** `carlos-system-update.path` runs `system apply` as root from the |
| +installed binary, or from `carlos.prev` while a journal is open |
| +(`units/carlos-system-update.service:47`). `install` (`internal/host/systemapply.go:551`): |
| +`decidePrev` refreshes `carlos.prev` from the installed binary only when that binary is the |
| +recorded applied build, else keeps the old `carlos.prev` (`:851-875`); both builds' units are |
| +staged with `bootstrap -root <dir> -plan-json` (`stagePlan`); the journal records |
| +`UnitsAdded`/`UnitsRemoved` (`:610`); the candidate is renamed in (`:632`); `installUnits` |
| +(`:1379`) first runs `disable --now` on each removed unit, logging and ignoring a failure |
| +(`:1388-1391`), then copies the staged files, skipping directories (`:1408`), then sysusers, |
| +tmpfiles and `daemon-reload`; the edge is restarted (`restartEdge`, `:1490-1494`) and |
| +gated (20 s, 3 s HTTP probe, then an `NRestarts` soak); `finish`'s `arm` starts the plan's |
| +`Armed` list and refuses the edge (`:1748-1752`). |
| + |
| +**Rollback and recovery** share `restoreFiles` (`:1849`): it renames `carlos.prev` in |
| +(`:1892`), syncs the directory (`:1895`), restages the previous units if needed (`:1924`), |
| +and only then runs `installUnits(prevDir, UnitsAdded)` (`:1927`). Any error between the rename |
| +and the `disable` returns with the old binary on disk and the new units in place; |
| +`recoverIfNeeded` then returns (`:2061`). `carlos-system-recover.service` runs once per boot |
| +`Before=carlos-edge.service` and as the update's `OnFailure=`; with a journal open it runs |
| +**`carlos.prev`**, i.e. the older build's code (`units/carlos-system-recover.service:34,41`). |
| +At runtime it then issues `restart --no-block` on the edge (`:2082`). |
| + |
| +**The hand roll.** `scripts/update-platform-binary.sh` in platform-infrastructure is the |
| +documented way to put a release on one box (`README.md:289`). It installs the binary over |
| +`/opt/carlos/carlos`, runs `carlos bootstrap -root /tmp/carlos-unitstage`, `cp`s |
| +`etc/systemd/system/*` into place (files only; under `set -euo pipefail` a directory there |
| +would abort it), reloads and restarts the edge. It never touches `carlos.prev` and never |
| +removes a unit or a drop-in. |
| + |
| +**What no old code does.** No applier from v0.18.0 to today removes anything under |
| +`/etc/systemd/system` but the files named in `UnitsAdded`/`UnitsRemoved`: every `RemoveAll` |
| +in `systemapply.go` targets the staging directory (checked at v0.18.0, v0.22.0, v0.25.0, |
| +v0.28.30), and the copy loop skips directories. So a drop-in directory written by this |
| +build survives every old applier and the hand script. And every build exits non-zero on |
| +`carlos system edge-socket-check`: since v0.18.0 the `system` verb's default branch exits 2 |
| +(`system.go:116-121`, same at v0.18.0), and v0.17.0's `main` exits 2 on an unknown verb. |
| + |
| +**Version floors are not a baseline here.** `release.LayoutFloor` is v0.25.0 |
| +(`internal/release/layoutintent.go:211`), and `install` refuses below it only when |
| +`layoutReversalReasons()` is non-empty (`systemapply.go:583`), so it is no installation |
| +floor. The design does not need one: nothing in an old binary is load-bearing for INV-R. |
| + |
| +**The belt pass** (`ensureSystemUnits`, `internal/host/systemunits.go:53`) arms a fixed |
| +list (`:28,32`) without either edge unit. |
| ### 1.4 systemd on the boxes |
| -The boxes are Debian 13 arm64 (`infra/modules/region-host/variables.tf:158`, |
| -`debian-13-arm64-*`), which runs **systemd 257**. The repo records 257.13 on the boxes' AMI |
| -and on DOCS-T1 (`docs/superpowers/plans/2026-10-24-rootfs-apps-2-verify.md:34`), and here |
| -`systemctl --version` prints `systemd 257 (257.13-1~deb13u1)`. Off-cloud hosts are |
| -"systemd Debian-family" (`infra/remote-host.md:7`). |
| - |
| -This design uses nothing newer than socket units, `Wants=`, `ExecStartPre=` in a socket, |
| -and `BindIPv6Only=`, all long-standing. By contrast `FileDescriptorStorePreserve=` is |
| -"Added in version 254" (`man systemd.service`). |
| - |
| -The `sd_listen_fds(3)` and `sd_notify(3)` pages are not installed here (no libsystemd-dev). |
| -The protocol is the one the repo already implements in |
| -`internal/instancectl/peer_linux.go:90-104` and `testdata/echoapp/main.go:62-75`: |
| -`LISTEN_PID` is our pid, `LISTEN_FDS=N`, and the fds start at 3. |
| +Debian 13 arm64 (`infra/modules/region-host/variables.tf:158`), **systemd 257** (257.13 on |
| +the AMI and on DOCS-T1, `docs/superpowers/plans/2026-10-24-rootfs-apps-2-verify.md:34`). |
| +The `LISTEN_FDS` protocol is the one the repo already implements |
| +(`internal/instancectl/peer_linux.go:90-104`, `testdata/echoapp/main.go:62-75`). Quotes |
| +below are from this machine's man pages. |
| ### 1.5 Local upstream dials today |
| -**Unix-socket routes** use `dialUnixWithRetry` (`edge.go:2721,3509`), and generation pools |
| -use `dialGen` (`genproxy.go:86`). Both retry `ECONNREFUSED`/`ENOENT` every 100 ms for |
| -`13 * time.Second` (`genproxy.go:40,86`), a window sized for "the instance drain (10s) |
| -plus rebind" (`edge.go:3502-3508`). |
| - |
| -**Unprobed TCP addr routes** use `tcpProxy` (`edge.go:851,864` → `:2736`). That is a zero |
| -`http.Transport` with no retry, so a refused dial is a bare 502 from `defaultProxyError` |
| -(`:627-633`), with cause `CauseRefused` (`:603`). |
| - |
| -**The console** listens on TCP `127.0.0.1:8090` (`cmd/console/main.go:758-759`) behind an |
| -addr route stamped `--unit console.service` (`cli.go:610-616`). On SIGTERM it releases its |
| -policy-writer lease and calls `os.Exit(0)` without a drain (`cmd/console/main.go:592-601`). |
| -Every dial refused between that exit and the next `net.Listen` is part of today's ~10 s of |
| -502. `console.service` is operator-run and is not in this repo (`cli.go:1741`, |
| -`README.md:291`), so socket-activating it is not on the table. |
| +**Unix routes** (`dialUnixWithRetry`, `edge.go:2721,3509`; `dialGen`, `genproxy.go:86`) |
| +retry `ECONNREFUSED`/`ENOENT` every 100 ms for 13 s (`genproxy.go:40,86`), sized for "the |
| +instance drain (10s) plus rebind" (`edge.go:3502-3508`). |
| + |
| +**Addr routes** pick by `Health` (`edge.go:846-870`). Empty → `tcpProxy` (`:2736`), a zero |
| +`http.Transport`: a refused dial is a bare 502 (`:627-633`, `CauseRefused`). Set → |
| +`multiProxy` (`:2817`): it dials only the prober's live set; an empty set is |
| +`errNoLiveUpstream` (`:2839`), a refused upstream is ejected (`:2876`), and a pass with no |
| +connection answers 503 + `Retry-After: 2` (`:2892`). The prober readmits an upstream after |
| +`rise_after` consecutive passes at `interval_ms` (defaults 2 and 2000, |
| +`internal/provision/provision.go:295-325`; `prober.go:508-530`). Go 1.26 runs the dial on a |
| +context detached from the request (`$GOROOT/src/net/http/transport.go:1524-1529`); the |
| +waiting request returns on its own context at once. |
| + |
| +**The console** listens on `127.0.0.1:8090` (`cmd/console/main.go:758-759`) and exits on |
| +SIGTERM without a drain (`:592-601`); `console.service` is operator-run and not in this |
| +repo (`cli.go:1741`, `README.md:291`). Its route can have either shape, and the repo |
| +cannot say which a given box has. `carlos add --addr` has no health flag, and |
| +`carlos route --unit console.service` (`cli.go:610-616`) only stamps the unit, so a |
| +hand-added console route is **unprobed**. A route minted from a record with `Health` is |
| +**probed** (`provision/reconcile.go:573,627-650`). Root's named-unit proof expects |
| +`/healthz` for the console (`internal/host/proofnamed.go:140`), but that feeds root's |
| +proof, not the route row. D10 and D10b cover both shapes, so nothing waits on the answer. |
| ## 2. D1: the mechanism |
| -| | `carlos-edge.socket` | fd store (`FDSTORE=1` + `FileDescriptorStoreMax=`) | |
| +| | `carlos-edge.socket` | fd store (`FDSTORE=1`, `FileDescriptorStoreMax=`) | |
| |---|---|---| |
| -| `systemctl restart` | A separate unit, so it stays active; connections queue (the `FlushPending=` quote below) | Kept: "will remain intact as long as the unit has a job pending" (`man systemctl`, restart) | |
| -| `stop` then `start` | Kept. A connection that arrives meanwhile starts the edge. | **Lost.** The fds are closed "when the service is fully stopped and no job is queued" (`man systemd.service`), and `man systemctl` names stop+start as the way to flush the store | |
| +| `systemctl restart` | Separate unit, stays active; `FlushPending=no` keeps queued connections | Kept with the default `FileDescriptorStorePreserve=restart`: "as long as the unit is neither inactive nor failed, or a job is queued" | |
| +| `stop` then `start` | Kept | Lost with `restart`/`no`. Kept with `yes`: "until the unit is removed from memory (i.e. is not referenced anymore and inactive)" (254+) | |
| | Boot, crash loop, edge never started | PID 1 listens from `sockets.target` | Nothing held until the edge has bound once and stored its fds | |
| -| Old binary after a rollback | Already handled: rollback runs `disable --now` on the added socket before it restarts the edge (§1.3) | A store kept across the rollback's `restart` job is handed back to an old binary that binds by itself, so it gets EADDRINUSE. Clearing the store needs `systemctl clean --what=fdstore` (254+), which no old applier runs | |
| -| Edge code | Read `LISTEN_FDS` (a pattern already in the repo) | `sd_notify` with `SCM_RIGHTS` and `NotifyAccess=`, plus the same read | |
| - |
| -**Decision: the socket unit.** It is the only option that survives `stop`+`start` and boot, |
| -and the code that removes it on rollback is already in every binary a box can roll back to. |
| -An `SO_REUSEPORT` handoff was rejected too. `restart` never runs the two processes |
| -together, and an old binary does not set the option, so a mixed pair cannot share the |
| -port. |
| +| INV-R: an older binary must not inherit the ports | The edge's `ExecStartPre=` can stop the socket, a separate unit (D4) | The store is handed to whatever starts next. Clearing it is `systemctl clean --what=fdstore`, and "the specified units must be stopped" (`man systemctl`), so the edge's own `ExecStartPre=` cannot. `yes` makes the store outlive a stop too | |
| +| Edge code | Read `LISTEN_FDS` | `sd_notify` with `SCM_RIGHTS` and `NotifyAccess=`, plus the same read | |
| -Two facts from the man pages on this machine carry the design: |
| - |
| -- "No implicit WantedBy= or RequiredBy= dependency from the socket to the service is added. |
| - This means that the service may be started without the socket, in which case it must be |
| - able to open sockets by itself." (`man systemd.socket`) |
| -- `FlushPending=`: "If no, the socket's buffers will not be cleared, permitting the service |
| - to handle any pending connections after restart, which is the usually expected |
| - behaviour. Defaults to no." (`man systemd.socket`) |
| +**Decision: the socket unit.** It holds the ports at boot and before the first bind, and it |
| +can be released from inside the edge's start, which is what INV-R needs. `SO_REUSEPORT` was |
| +rejected: `restart` never runs both processes, and an old binary does not set it. From |
| +`man systemd.socket`: the service "may be started without the socket, in which case it must |
| +be able to open sockets by itself", and `FlushPending=no` lets "the service … handle any |
| +pending connections after restart". |
| ## 3. Design |
| -**D2. The units.** A new file, `internal/host/units/carlos-edge.socket`: |
| +**D2. The units.** New `internal/host/units/carlos-edge.socket`: |
| ``` |
| [Unit] |
| @@ -168,263 +167,367 @@ ListenStream=443 |
| BindIPv6Only=both |
| Accept=no |
| ExecStartPre=/opt/carlos/carlos system edge-socket-check |
| -User=carlos |
| -Group=carlos |
| [Install] |
| WantedBy=sockets.target |
| ``` |
| -A bare port is "read as port number to listen on via IPv6", and with `both` it is reachable |
| -"via both IPv4 and IPv6" (`man systemd.socket`). That is the same dual-stack wildcard that |
| -Go's `net.Listen("tcp", ":80")` opens today. Everything else stays at its default: |
| -`Backlog=` (capped by `net.core.somaxconn`, 4096 here), `FlushPending=no`, and |
| -`TriggerLimit*`; if that trips, the socket fails and the edge binds by itself (D5). |
| - |
| -`User=`/`Group=` reach only the `ExecStartPre=` ("the execution environment the |
| -ExecStartPre= … commands are executed in", `man systemd.socket`). PID 1 still binds the |
| -ports as root. The plan runs `systemd-analyze verify` on the pair. |
| - |
| -`carlos-edge.service` gains `Wants=carlos-edge.socket` and `After=carlos-edge.socket` and |
| -nothing else. It gets **no `Requires=`, `BindsTo=` or `PartOf=` on the socket**. With |
| -`Requires=`, "this unit will be stopped (or restarted) if one of the other units is |
| -explicitly stopped" (`man systemd.unit`). But rollback stops the socket while the edge must |
| -keep serving (§4 row 6), and at boot it stops the socket while the edge's start job is |
| -queued (row 8). With `Wants=`, even "if the listed units fail to start … this unit will |
| +A bare port with `BindIPv6Only=both` is the dual-stack wildcard Go's `net.Listen("tcp", |
| +":80")` opens today. `Backlog=` (capped by `somaxconn`, 4096), `FlushPending=no` and |
| +`TriggerLimit*` stay at their defaults. The check runs as root, like `system apply`: root |
| +already runs this binary, and the check needs PID 1's private socket (D4). No `User=`. |
| + |
| +`carlos-edge.service` gains `Wants=carlos-edge.socket` and `After=carlos-edge.socket`, and |
| +**no `Requires=`, `BindsTo=` or `PartOf=`** on the socket. D4 stops the socket during the |
| +edge's own start, and rollback stops it while the edge keeps serving; with `Requires=` "this |
| +unit will be stopped (or restarted) if one of the other units is explicitly stopped" |
| +(`man systemd.unit`). With `Wants=`, "if the listed units fail to start … this unit will |
| still be started." |
| -**D3. Enabled, never armed.** The socket joins `enabledUnits`, so `enable` links it into |
| -`sockets.target` for boot. It is excluded from `armedUnits`, so neither bootstrap nor |
| -`finish`'s `arm` nor any plan's `Armed` ever runs `start` on it. `arm` refuses it as it |
| -refuses the edge (`:1748-1752`), and `ensureSystemUnits` does not touch it. The reason: a |
| -`start` while an edge process holds :80 itself fails with EADDRINUSE, and in a batched |
| -`systemctl start` that failure lands on the whole arm. That edge process can be the old one |
| -at first install, or the running one when bootstrap is re-run on a live box. So the socket |
| -starts only through the edge's own start (`Wants=`) or at boot. |
| - |
| -**D4. Refusing an old binary up front.** `carlos system edge-socket-check` is a new hidden |
| -verb that exits 0 and prints nothing. Earlier builds exit 2 on an unknown `system` verb |
| -(`system.go:116-121`). So when the binary on disk predates the socket, the socket's |
| -`ExecStartPre=` fails, the socket does not start, nothing holds :80, and the old edge binds |
| -as it always has. |
| - |
| -This covers an old binary meeting the socket when the socket *starts*: boot after an |
| -interrupted rollback, or a manual downgrade followed by a reboot. A socket that is already |
| -active when an old binary arrives is §4 rows 6, 13 and 14. |
| - |
| -**D5. Taking listeners: inherit, else bind.** |
| - |
| -*At process start, before anything else.* `takeSystemdListeners` is the first statement of |
| -the `edge` and `agent` verbs (`main.go:65,189`). When `LISTEN_PID == os.Getpid()` and |
| -`LISTEN_FDS=N` (N ≥ 1), it takes fds 3 through 3+N−1. Each fd must be `SOCK_STREAM`, |
| -`AF_INET`/`AF_INET6` and listening (`SO_ACCEPTCONN`). It is wrapped with |
| -`net.FileListener`, which dups it with close-on-exec (`$GOROOT/src/net/file_unix.go:18`), |
| -and the original is closed. Then `LISTEN_PID`, `LISTEN_FDS` and `LISTEN_FDNAMES` are |
| -unset. |
| - |
| -It runs first because systemd passes these fds without close-on-exec, and the activator |
| -gives exec children the agent's whole environment (`backend_exec.go:2997`). No tenant |
| -process may inherit the public listeners, or a `LISTEN_FDS` that names them. The guard |
| -drop-in (`guardarm.go:166`) reaches the binary through `syscall.Exec` |
| -(`internal/hostfloor/image_linux.go:61`), which keeps the pid, so `LISTEN_PID` stays valid. |
| - |
| -*At serve time, where `ListenAndServe` is today.* Each wanted address (`-http`, plus |
| -`-https` outside dev mode) is matched to an inherited listener by local port, and also by |
| -IP when the flag names one. A match is served with `Serve` or `ServeTLS("", "")`, exactly |
| -what `ListenAndServe*` does after its bind. An unmatched address is bound by the process |
| -itself, with today's fatal message on failure (`edge: http listener %s: %v`; the large-box |
| -tests grep it). An inherited fd that matches nothing or fails a check is closed and logged. |
| -A malformed handoff costs the blip, never the box. The fd code is `//go:build linux` (`internal/edge/listeners_linux.go`, using |
| -`golang.org/x/sys/unix`). A `!linux` stub returns nothing, so every address binds itself as |
| -today. |
| - |
| -**D6. Graceful stop leaves the shared socket alone.** This needs no code change, plus one |
| -rule. `Shutdown` closes the edge's listener, and on Linux that is `close(2)` of the edge's |
| -own dup (`net/fd_posix.go:47-50` → `internal/poll/fd_unix.go:91-105`), never `shutdown(2)`. |
| -PID 1's copy keeps the socket and its accept queue, so connections arriving during the drain |
| -and the new process's startup queue up. (`FileListener`'s `SetNonblock`, `file_unix.go:25`, |
| -changes the description PID 1 shares; harmless, PID 1 only polls.) The rule, enforced by a |
| -guard test: no `shutdown(2)`, `SO_LINGER` or `SO_REUSEPORT` on edge |
| -listeners, and never `FlushPending=yes` in the unit. |
| - |
| -**D7. `restartEdge` in this build.** When `/etc/systemd/system/carlos-edge.socket` exists, |
| -`restartEdge` runs `reset-failed` on the socket as well as the service, then |
| -`restart carlos-edge` as before. This mirrors `restartInstanceControl`'s stat |
| -(`:1526-1536`). |
| - |
| -`Wants=` pulls the socket into the restart. By `man systemd.unit` ("if one unit is shut down |
| -and the other is started up, the shutdown is ordered before the start-up"), the old edge |
| -exits before the socket starts. So a socket that failed, or never started, comes back at the |
| -next restart. Socket state is logged, not gated: the edge serves either way (D5). |
| - |
| -**D8. The log line.** One line at serve time, of the form `edge: listeners: inherited 2 from |
| -systemd (:80 fd 3, :443 fd 4)`, `edge: listeners: bound :80 and :443 itself (no systemd |
| -sockets: LISTEN_PID unset)`, or a mixed form. There is also one line per fd closed unused. |
| -The morning check is `journalctl -u carlos-edge -g 'edge: listeners:'`. These are operator |
| -logs, not member copy. |
| - |
| -**D9. Hardening is unchanged.** `AmbientCapabilities=`/`CapabilityBoundingSet= |
| -CAP_NET_BIND_SERVICE` stay (`carlos-edge.service:32-33`), because D5's fallback binds :80 |
| -itself; dropping them is a later change, once every box runs the socket. |
| -`RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX` (`:51`) restricts `socket(2)`, not |
| -inherited fds, and the edge still needs all three to dial out. `@system-service` covers |
| -`getsockname`, `getsockopt` and `fcntl`. The socket unit runs only the one-shot check, as |
| -`carlos`. |
| - |
| -**D10. Local upstreams are held, not 502'd.** One constant, `localDialHold = 13 * |
| +**D3. Enabled, never armed.** The socket joins `enabledUnits` and stays out of |
| +`armedUnits`, every plan's `Armed`, `ensureSystemUnits`, and `arm` (which refuses it like |
| +the edge). A `start` while an edge process holds :80 itself fails with EADDRINUSE, and in a |
| +batched `start` that fails the whole arm. So the socket starts only through the edge's |
| +`Wants=` or at boot, and always through its check. |
| + |
| +**D4. INV-R and where it is enforced.** |
| + |
| +> **INV-R:** `carlos-edge.service` never runs its main process while `carlos-edge.socket` |
| +> is active unless `/opt/carlos/carlos system edge-socket-check` exits 0. |
| + |
| +*The predicate.* `carlos system edge-socket-check` is a new hidden verb. It exits 0 only |
| +when (a) `systemctl show carlos-edge.service` lists the guard drop-in below in |
| +`DropInPaths` (loaded, not just on disk) and the file is byte-equal to the copy this build |
| +embeds, and (b) that unit's effective `ExecStart` argv is exactly one of the two shipped |
| +command lines (§1.1), with no flags. Anything else exits 1 with one line naming why. With |
| +`-self` it exits 0 without checking (witnesses only). Every older build exits non-zero |
| +(§1.3). It reuses `systemdUnits.Show` (`unitsettle.go:60`), extended to keep `ExecStart`'s |
| +argv. As root, `systemctl` uses PID 1's private socket, not D-Bus, so the check also works |
| +at `sockets.target`, before `dbus.service` is up. |
| + |
| +*E-start, the enforcement point.* A drop-in, |
| +`/etc/systemd/system/carlos-edge.service.d/10-edge-socket-guard.conf`: |
| + |
| +``` |
| +[Service] |
| +ExecStartPre=+/bin/sh -c '/opt/carlos/carlos system edge-socket-check && exit 0; systemctl stop carlos-edge.socket || ! systemctl -q is-active carlos-edge.socket' |
| +``` |
| + |
| +It runs before every start of the edge's main process, however triggered (`restart`, |
| +`restart --no-block`, `Restart=always`, boot, socket activation), under any main unit file, |
| +old or new, and on guarded boxes too: a drop-in adds an `ExecStartPre=` without resetting |
| +the others. If the binary on disk fails the predicate, it stops the socket synchronously |
| +and the binary then binds by itself. If the stop fails and the socket is still active, the |
| +line fails, the start fails, and `Restart=always` tries again: fail closed. A socket unit |
| +that is no longer loaded (its file was removed and it is inactive) makes `stop` fail and |
| +`is-active` false, so the line passes. `+` runs this one command as root, exempt from |
| +`User=`, the capability bound and namespacing (`man systemd.service`, table of prefixes). |
| +The line has no `$` or `%`. |
| + |
| +*Why the stop cannot deadlock.* The edge's start job is running when the stop job for the |
| +socket is queued. `man systemd.unit`: "if one unit is shut down and the other is started |
| +up, the shutdown is ordered before the start-up … in all cases". The stop does not wait on |
| +the edge's start. W2 transition 4 checks this on 257. |
| + |
| +*The gate, so E-start is always there.* The socket's own `ExecStartPre=` runs the same |
| +predicate. A socket start fails until the drop-in is loaded. So whenever the socket is |
| +active, E-start runs at every later edge start. Nothing removes the drop-in: no old |
| +applier or script touches directories (§1.3), and this build never removes it. |
| + |
| +*Writers.* Only this build's root code writes the drop-in: `installUnits` (every install, |
| +rollback and recovery this build runs) and `carlos bootstrap` on the live root. Both write |
| +it durably before their `daemon-reload`. It is never written into a `-root` staging tree: |
| +old appliers skip directories there, and the hand script would abort on one. An older build |
| +never writes it. This is what makes activation two-step (§4). |
| + |
| +*Conflicting bind configuration* (an operator's drop-in giving the edge `-http |
| +127.0.0.1:80`, or `-dev`) fails clause (b). So the socket never activates for it, and an |
| +already active socket is stopped at the next edge start. |
| + |
| +**D5. Taking listeners.** |
| + |
| +*At process start.* `takeSystemdListeners` is the first statement of the `edge` and `agent` |
| +verbs (`main.go:65,189`). When `LISTEN_PID == os.Getpid()` and `LISTEN_FDS=N`, it takes |
| +fds 3 to 3+N−1. Each must be `SOCK_STREAM`, `AF_INET`/`AF_INET6` and listening |
| +(`SO_ACCEPTCONN`). It wraps each with `net.FileListener` (a close-on-exec dup, |
| +`$GOROOT/src/net/file_unix.go:18`), closes the original, and unsets `LISTEN_PID`, |
| +`LISTEN_FDS` and `LISTEN_FDNAMES`. It runs first because systemd passes the fds without |
| +close-on-exec and exec children get the agent's environment (`backend_exec.go:2997`). The |
| +guard's `syscall.Exec` keeps the pid (`internal/hostfloor/image_linux.go:61`). |
| + |
| +*At serve time.* Each wanted address (`-http`, plus `-https` outside dev mode) is classified |
| +against the inherited listeners by port: |
| +- **compatible**: same port, and the scope matches. A wanted address with no host takes |
| + a wildcard listener (`::` dual-stack or `0.0.0.0`); a wanted IP literal takes a listener |
| + bound to that IP. It is served with `Serve`/`ServeTLS("", "")`. |
| +- **absent**: no inherited listener on that port. The process binds it, with today's fatal |
| + message on failure (`edge: http listener %s: %v`). |
| +- **incompatible**: same port, different scope. PID 1 holds the port, so a self-bind would |
| + fail. The edge exits at once with `edge: listener %s: systemd passes %s; the socket and |
| + the flags disagree`. The D4 gate keeps this from activating on a box. It is reachable only |
| + by hand. |
| + |
| +An inherited listener on a port nothing wants is closed and logged. PID 1 keeps its copy; |
| +the gate's clause (b) means that cannot happen on a box. The fd code is `//go:build linux` |
| +(`internal/edge/listeners_linux.go`); a `!linux` stub returns nothing. |
| + |
| +**D6. Graceful stop leaves the shared socket alone.** `Shutdown` closes the edge's own dup |
| +(`close(2)`, `net/fd_posix.go:47-50` → `internal/poll/fd_unix.go:91-105`), never |
| +`shutdown(2)`. PID 1's copy keeps the accept queue through the drain and the next startup. |
| +`FileListener`'s `SetNonblock` (`file_unix.go:25`) changes the shared description; PID 1 |
| +only polls. A guard test forbids `shutdown(2)`, `SO_LINGER` and `SO_REUSEPORT` on edge |
| +listeners, and `FlushPending=yes` in the unit. |
| + |
| +**D7. `restartEdge`.** When `/etc/systemd/system/carlos-edge.socket` exists, `reset-failed` |
| +covers the socket as well as the service (a socket refused at an earlier roll is `failed`), |
| +then `restart carlos-edge` as before, like `restartInstanceControl`'s stat |
| +(`systemapply.go:1526-1536`). `Wants=` pulls the socket into the restart, and the old edge's |
| +stop is ordered before the socket's start. Socket state is logged, not gated. |
| + |
| +**D8. Log lines.** At serve time, one of `edge: listeners: inherited 2 from systemd (:80 fd |
| +3, :443 fd 4)`, `edge: listeners: bound :80 and :443 itself (no systemd sockets: LISTEN_PID |
| +unset)`, or a mixed form, plus one line per fd closed unused. The predicate's refusal line |
| +(`edge-socket-check: refused: <why>`) lands in the socket's or the edge's journal. The |
| +morning check is `journalctl -u carlos-edge -u carlos-edge.socket -g 'edge: listeners:|edge-socket-check'`. |
| +These are operator logs, not member copy. |
| + |
| +**D9. Hardening.** `CAP_NET_BIND_SERVICE` stays (`carlos-edge.service:32-33`), because D5 |
| +still binds by itself; dropping it is a later change. `RestrictAddressFamilies=` (`:51`) |
| +filters `socket(2)`, not inherited fds. `@system-service` covers `getsockname`, |
| +`getsockopt` and `fcntl`. E-start's `+` line is the only unconfined command in the edge |
| +unit. It runs the same root-owned binary that `system apply` and recovery already run as |
| +root. |
| + |
| +**D10. Unprobed loopback routes: retry the dial.** One constant, `localDialHold = 13 * |
| time.Second`, replaces the literals at `genproxy.go:40,86`. `tcpProxy` gets a `DialContext` |
| -when the addr is local (a literal loopback IP by `netip.Addr.IsLoopback`, or `localhost`): |
| -it retries `ECONNREFUSED` every 100 ms until it connects, its context ends or the hold runs |
| -out, then returns the last error. Non-local addrs keep the zero dialer; a refusal from |
| -another machine is not a restart on this box. This covers every loopback addr route, the |
| -console included, and activator TCP routes, with no console special case. Probed |
| -`multiProxy` routes keep their eject-and-503 behaviour. Why 13 s: it is the window already |
| -in production for unix upstreams, sized for drain plus rebind; the console's ~10 s gap fits |
| -inside it; it is under the wake hold's 30 s; and one policy beats two for "a local upstream |
| -is briefly absent". |
| - |
| -Safety: a failed dial never reached the upstream, so a retry is safe for any method. |
| -`RoundTrip` reads no body before it has a connection, so a POST arrives intact; upgrade |
| -requests (WebSockets) take the same dial and are held the same way. Go 1.26 detaches the |
| -dial from the request ("The dial should proceed even if the request is canceled", |
| -`$GOROOT/src/net/http/transport.go:1524-1529`): a client that leaves is released at once on |
| -its own context, the background dial ends at the cap, and a late success goes to the idle |
| -pool, as for unix routes today. A dial still refused at the cap stays `CauseRefused` and |
| -502; the `client_gone`/abandoned rules (`answered.go:55-65`) are unchanged, and their |
| -comment gains loopback TCP. |
| - |
| -**D11. Cross-compile.** Only D5's fd code is linux-only, and it has a stub. D10 goes through |
| -the existing `isRetryableDialErr` (`edge.go:3513`), which already builds for darwin and |
| -windows. CI's darwin and windows builds are the check. |
| +when its addr is local (a loopback IP literal by `netip.Addr.IsLoopback`, or `localhost`). |
| +It retries `isRetryableDialErr` (`edge.go:3513`) every 100 ms until it connects or the hold |
| +runs out, then returns the last error (502, `CauseRefused`). A non-local addr keeps the zero |
| +dialer. 13 s is the window already used for unix upstreams and covers the console's ~10 s |
| +gap. |
| + |
| +**D10b. Probed routes with a local upstream: wait for a live one.** The prober records, per |
| +host, whether any configured addr is local (same rule, computed in `newHostProber`; |
| +`multiProxy` still never sees the addr list). `multiProxy`'s dialer then works in passes. |
| +Each pass reads `Live(host)` and dials the live upstreams, ejecting a refused one exactly as |
| +today. When a pass ends with no connection (the live set was empty, or every live upstream |
| +failed) and the host has a local addr, it waits 100 ms and makes another pass. It stops |
| +when a dial succeeds or the hold ends, and then returns `errNoLiveUpstream` (503 + |
| +`Retry-After: 2`, as today). Hosts with no local addr answer at once, as today. |
| + |
| +- *Admission is unchanged.* Only the live set is dialed, and only the prober readmits: |
| + `rise_after` consecutive passing health probes at `interval_ms`. An ejected upstream is |
| + never dialed. A held request just waits for that. |
| +- *The hold:* `localDialHold + rise_after × interval`, capped at 30 s (`localHoldCap`, |
| + `wakeWaitBound`'s value). The first term is the time the upstream is gone. The second is |
| + the most the prober takes to readmit it once it is back, without probing faster than the |
| + operator configured. Defaults: 13 + 2 × 2 = 17 s. |
| +- *Both cases the review named.* A refusal that comes before the probe has noticed anything |
| + ejects the upstream, and the next passes wait for its readmission. A live set already |
| + empty when the request arrives waits from the first pass. |
| +- *Mixed sets.* A live remote upstream that answers serves the request at once. The hold |
| + begins only when nothing live connects. |
| + |
| +D10 and D10b are safe for any method: a failed dial sent nothing, and `RoundTrip` reads no |
| +body before it has a connection. Upgrades take the same dial. Because the dial context is |
| +detached, a client that leaves is released at once and the background dial ends at the |
| +hold. `client_gone`/abandoned rules (`answered.go:55-65`) are unchanged; the comment gains |
| +loopback TCP and probed routes. |
| + |
| +**D11. Cross-compile.** Only D5's fd code is linux-only, with a stub; the predicate verb |
| +shells out to `systemctl` and builds everywhere. CI's darwin and windows builds check it. |
| ## 4. Every binary/unit mix |
| -"Gap" means connections are refused during that one restart, as today. |
| - |
| -| # | Binary × units: how a box gets there | Who holds :80/:443 | Serves? / failure mode | |
| -|---|---|---|---| |
| -| 1 | Old × old: today | The edge process | Yes. Gap on every restart | |
| -| 2 | New × old: crash restart between rename (`:632`) and `installUnits` (`:646`); hand-run `carlos agent`/`edge`; socket left failed | The edge process (D5: no fds, binds itself) | Yes. Gap | |
| -| 3 | New × new, steady state: apply to a newer build, `restart`, a `Restart=always` crash, `stop`+`start` | PID 1 throughout | **Yes, queued, 0 refused.** The wait is the old edge's drain plus the new one's startup | |
| -| 4 | First install: old applier, old process, new units after `daemon-reload` | The old process until it exits, then the socket. Its check runs the new binary, already renamed in | Yes. The old applier's plain `restart` pulls the socket in through `Wants=`, ordered after the stop (D7). Gap this once. If the socket started first and hit EADDRINUSE, the edge would still start (`Wants=`) and bind by itself, and row 3 would begin at the next restart. W2 records which happened | |
| -| 5 | Install `finish`, by an old or new applier | Unchanged | The new plan's `Armed` excludes the socket (D3), so it is only enabled | |
| -| 6 | Gate rollback from the first socket build (old applier) | The socket and the new edge, until `disable --now carlos-edge.socket` (`:1389`, via `UnitsAdded`). Then the new edge's dup alone, until its restart closes it. Then the old edge binds | Yes, with a gap in the rollback's restart. **Residual R1:** a crash in the milliseconds between `restoreFiles`' rename (`:1892`) and that `disable` puts the old binary under the socket. It gets EADDRINUSE and `Restart=always` loops until the `disable` lands and `restartEdge` resets and restarts it. It self-heals within the same rollback | |
| -| 7 | Rollback or downgrade between two socket-aware builds | PID 1 throughout | Yes. 0 refused | |
| -| 8 | Boot with a journal open (`carlos-system-recover`, `Before=` the edge) | `sockets.target` starts the socket only if the binary on disk passes D4. Recovery's `restoreFiles` runs `disable --now` on it if `UnitsAdded` names it, and the edge starts last | Yes. If recovery fails before units are restored (**R2**), D4 still means an active socket implies a socket-aware binary, so the edge inherits or binds | |
| -| 9 | Runtime recovery (update `OnFailure=`) | As row 6, with `restart --no-block` (`:2082`) | As row 6 | |
| -| 10 | Recovery forward from `gated` | The socket, already active | `finish` does not start it (D3) | |
| -| 11 | Fresh bootstrap with the new binary | Nothing until `systemctl start carlos-edge`; then the socket (`Wants=`), and from `sockets.target` on every boot after | Yes | |
| -| 12 | Live re-run of the new bootstrap on a pre-socket box (old script road) | The old process, until the script's edge restart | Only `enable` runs (D3), so bootstrap cannot fail on EADDRINUSE. The restart is row 4 | |
| -| 13 | Apply of an older build over a socket box (`system update --file`, a downgrade promote) | The socket, until `installUnits(newDir, UnitsRemoved)` runs `disable --now` on it. D7's stat then finds no file and leaves the socket out | Yes. Gap, with the same R1 window between `:632` and the `disable` | |
| -| 14 | **Off-road manual downgrade, no reboot:** an old binary copied by hand, or the old script running an old bootstrap on a socket box | The socket stays active, because the old bootstrap does not know it | **No: EADDRINUSE, box dark** until `systemctl disable --now carlos-edge.socket`. This is the one mix not made safe. It bypasses the journaled road that rolls units back, and old binaries and the infra script cannot change from here. D4 covers it only after a reboot. The morning notes must say to downgrade only through `carlos system update`. Paul's option: a later infra change to the script | |
| +A = this build or later; O = any older build. "Gap" = refused during that restart, as |
| +today. **G** = the socket's gate (D4), **E** = E-start (D4). |
| + |
| +| # | How a box gets there | Socket | Serves? | INV-R held by | |
| +|---|---|---|---|---| |
| +| 1 | O × O units: today | none | Yes, gap | n/a | |
| +| 2 | A × O units: crash between rename `:632` and `installUnits` `:646`; hand-run `carlos agent` | not loaded or inactive | Yes, binds itself, gap | E (if the drop-in exists) | |
| +| 3 | A × A units with the drop-in: steady state, `restart`, `Restart=always`, `stop`+`start` | active throughout | **Yes, 0 refused** | G at start, E passes | |
| +| 4 | **Roll 1:** an O applier installs A (or the hand script does) | enabled by the plan; pulled in by the restart; **G refuses** (no drop-in) and the socket fails | Yes, binds itself, gap as today | G: never active, so nothing in this roll's rollback, recovery or boot can break INV-R, at any failure point | |
| +| 5 | **Roll 2:** an A applier installs A′ | A's `installUnits` writes the drop-in and reloads; `restartEdge` resets the failed socket (D7); A stops, G passes, A′ inherits | Yes. Refused only during A's drain (the switch-over). Row 3 from then on | G then E | |
| +| 6 | Rollback/recovery to an A `carlos.prev` | active throughout | Yes, 0 refused | E passes | |
| +| 7 | Rollback/recovery to an O `carlos.prev` while active (`decidePrev` can keep an old one, `:862-869`), by an A applier or by O running recovery; a failure anywhere in `restoreFiles`/`installUnits`, including a failed `disable --now` (`:1390`) | may stay active | Yes, gap. The next edge start, whatever unit file and however triggered, runs E: O fails, the socket stops, O binds | **E**: the drop-in survives the old unit set (§1.3) | |
| +| 8 | Boot with a journal open | started at `sockets.target` only if G passes for the binary on disk then; recovery may then rename another in | Yes | G, then E at the edge's start (`After=` recovery) | |
| +| 9 | Downgrade through `system update --file` (A applier, O target) | `UnitsRemoved` disables it; if that fails it stays active | Yes, gap | E | |
| +| 10 | Hand script or manual copy of O over an active-socket box | stays active (the script removes nothing) | Yes, gap: E stops it at the script's restart | **E** (formerly "box dark") | |
| +| 11 | Fresh `carlos bootstrap` with A | drop-in written, socket enabled; first `start carlos-edge` activates it | Yes, 0 refused from the first restart | G, E | |
| +| 12 | Live re-run of A's bootstrap on a pre-socket box | only `enable` (D3); drop-in written | The next restart is the switch-over | G | |
| +| 13 | `finish`/`arm`, by any applier | `Armed` never has the socket (D3) | Unchanged | G regardless: `start` cannot skip `ExecStartPre=` | |
| +| 14 | Operator overrides the edge's `ExecStart` with flags | G refuses; E stops an active socket at the next start | Binds per its flags | G, E (clause b) | |
| + |
| +**What is left.** Only someone deleting the drop-in by hand while the socket is active and |
| +then starting an older binary. That is off-road; the morning notes say not to. |
| + |
| +**Two-step activation, and why.** A box updated from a pre-socket build gets the socket at |
| +roll 2, and roll 3 is the first with 0 refused. Fresh boxes get it at once. One-step |
| +(activate at roll 1) would put the socket in front of rollback machinery we cannot change: |
| +the old `installUnits` logs and ignores a failed `disable --now`, its `restoreFiles` can |
| +stop between rename and `disable`, recovery runs `carlos.prev`'s code, and the hand script |
| +copies an old unit set over an active socket. Only a guard that survives an old unit set |
| +covers all of these, which means a drop-in, and only this build writes drop-ins. The cost |
| +is one roll. §10 has a one-roll-sooner variant for Paul. |
| ## 5. TLS readiness and the latch |
| -With the socket, the kernel completes the TCP handshake at once, and the connection waits in |
| -the queue until the new edge calls `Serve`. That still happens only after the cert chain is |
| -assembled (`edge.go:3793` before `:3814-3830`). So no TLS handshake is answered before the |
| -mirror, wildcard and autocert are in place. The 20 s mirror and home-map refreshes now cost |
| -a queued wait instead of a refusal. |
| - |
| -The admission latch is unchanged. Activator-owned requests accepted early still wait up to |
| -30 s on `started`, then answer 503 with `Retry-After`. Other routes and redirects serve as |
| -soon as they are accepted. |
| - |
| -The gate still needs an HTTP answer within its 3 s client timeout, so a queued connect |
| -proves nothing. The gate is unchanged. |
| +With the socket, the kernel completes the handshake and the connection waits until the new |
| +edge calls `Serve`, which still follows the cert chain (`edge.go:3793` before `:3814`). No |
| +TLS handshake is answered before the mirror, wildcard and autocert are in place. The latch |
| +is unchanged: early activator requests wait up to 30 s, then 503 with `Retry-After`. The |
| +update gate still needs an HTTP answer within its 3 s timeout; it is unchanged. |
| ## 6. Known limits |
| -- **Drain.** In-flight requests on the old edge get up to 10 s, then they are cut, as today. |
| - New connections queue through that drain plus the new edge's startup: the registry open, |
| - and in the worst case 20 s each for the mirror and the home map. A client with a shorter |
| - timeout times out instead of being refused. Beyond `somaxconn` (4096) queued connections, |
| - SYNs are dropped and clients retransmit (`tcp_abort_on_overflow=0`). |
| -- **WebSockets.** Hijacked WebSockets on the old edge drop when it exits, as today. |
| - Instances restart with the edge (`BindsTo=carlos-edge.service`, |
| - `units/carlos-instance@.service:14`), which is part C. |
| -- **Console requests in flight.** Requests in flight at the console's `os.Exit` are reset |
| - and still answer 502. So does a request with a body on a pooled keep-alive connection |
| - that dies under it. The edge cannot replay a request it has already sent. |
| -- **Stopping the edge.** A stopped edge is now socket-activated by the next connection. |
| - Keeping it down means stopping the socket too. The shelved claim migration's runtime |
| - mask (`claimjournal_migration_linux.go:32`) would make the socket fail on the next |
| - connection and close; after the unmask the edge binds by itself (row 2). |
| -- **Held dials.** A held loopback dial costs a goroutine and a client connection for up to |
| - 13 s. A loopback upstream that stays down now answers 502 after 13 s rather than at |
| - once, the trade unix routes already make. |
| -- **Changing ports.** Changing the socket's `Listen*` lines later needs a socket restart, |
| - which is a gap. |
| +- **Drain.** In-flight requests on the old edge get 10 s, then are cut, as today. New |
| + connections queue through the drain and the new edge's startup (worst case 20 s each for |
| + mirror and home map). A client with a shorter timeout times out. Beyond `somaxconn` |
| + (4096) SYNs are dropped and retransmitted (`tcp_abort_on_overflow=0`). |
| +- **WebSockets** on the old edge drop when it exits; instances restart with the edge |
| + (`BindsTo=`, `units/carlos-instance@.service:14`): part C. |
| +- **Console requests in flight** at its `os.Exit` are reset and still 502, as is a request |
| + with a body on a pooled connection that dies under it. |
| +- **Stopping the edge.** A stopped edge is socket-activated by the next connection; keeping |
| + it down means stopping the socket too. The shelved claim migration's runtime mask |
| + (`claimjournal_migration_linux.go:32`) makes the socket fail on the next connection; after |
| + the unmask the edge binds by itself (row 2). |
| +- **Held dials.** A local upstream that stays down now answers after the hold (13 s for |
| + unprobed routes, about 17 s by default for probed ones, at most 30 s) rather than at once. |
| + That is the trade unix routes already make. A client that leaves is released at once; its |
| + dial goroutine runs to the hold. |
| +- **Changing the socket's ports** later needs a socket restart, which is a gap. |
| ## 7. Local witness (no cloud, no sudo, 127.0.0.1 only) |
| -**W1: a Go test, run in CI, linux only** (`internal/edge/listeners_witness_linux_test.go`, |
| -in the subprocess pattern of `internal/edge/startup_test.go:18-60`). The parent plays PID 1: |
| -it binds `127.0.0.1:0` twice, keeps both fds, and launches the child through |
| -`/bin/sh -c 'LISTEN_PID=$$ LISTEN_FDS=2 exec "$0" "$@"'` with `ExtraFiles` set to them (`$$` |
| -survives the `exec`, as systemd's post-fork `LISTEN_PID` does). The child runs the **real** |
| -`RunWithActivatorReady` (non-dev) on a temp `CARLOS_DATA_DIR`, `-http`/`-https` on those |
| -addresses, no store environment. About 8 workers send fresh-connection |
| -`GET http://127.0.0.1:P/` (any HTTP answer counts, as for the gate probe) and TLS dials to Q |
| -(a TLS alert counts as answered). Three times over, the parent sends SIGTERM, waits for the |
| -exit and starts the next child after an injected 1 s delay. Pass: 0 refused, reset or |
| -timed-out requests, and every child logs `inherited 2`. Reported: the measured gap (worst |
| -latency across a restart) and each restart's duration. A negative control, whose children |
| -inherit nothing and bind by themselves, must count refusals > 0. |
| - |
| -**W2: a script, run once by hand on DOCS-T1 with the sandbox off** |
| -(`scripts/edge-roll-witness.sh`; `systemd-socket-activate` cannot restart its child, and the |
| -user bus is closed inside the sandbox). It builds `carlos` into `mktemp -d` and writes |
| -`erb-<rand>.socket`/`.service` into `$XDG_RUNTIME_DIR/systemd/user`: D2's lines, except |
| -listening on `127.0.0.1:P`/`Q`, no `User=`, and `ExecStart=carlos edge -http 127.0.0.1:P |
| --https 127.0.0.1:Q -registry … -acme-cache …`. A curl load loop (`--max-time 30`) runs |
| -through four transitions: `restart` (0 refused); `stop`+`start` (0 refused); the |
| -first-install shape, the service binding itself with no socket and then `restart` with |
| -`Wants=` (must serve; reports which side bound first); and the rollback shape, `disable |
| ---now` the socket, remove it, `daemon-reload`, `restart` (must serve, `bound … itself`). It |
| -reports refusals and worst latency per transition, and a `trap` stops the units, removes |
| -the files, reloads and deletes the temp dir. W2's numbers go in the #434 reply; a dev-box |
| -measurement is Paul's call in the morning. |
| +**W1: Go test in CI, linux only** (`internal/edge/listeners_witness_linux_test.go`, in the |
| +subprocess pattern of `internal/edge/startup_test.go:18-60`). The parent plays PID 1: it |
| +binds `127.0.0.1:0` twice and launches the child through `/bin/sh -c 'LISTEN_PID=$$ |
| +LISTEN_FDS=2 exec "$0" "$@"'` with `ExtraFiles` (`$$` survives `exec`). The child runs the |
| +real `RunWithActivatorReady` (non-dev) on a temp `CARLOS_DATA_DIR`, with `-http`/`-https` on |
| +exactly those addresses. About 8 workers send fresh-connection `GET`s to P and TLS dials to Q |
| +(any HTTP answer or TLS alert counts). Three times over, the parent sends SIGTERM, waits, and |
| +starts the next child after a 1 s delay. Pass: 0 refused, reset or timed-out requests, and |
| +every child logs `inherited 2`. Reported: the worst latency across a restart and each |
| +restart's length. A negative control (children inherit nothing) must count refusals > 0. |
| + |
| +**W2: script, run once by hand on DOCS-T1 with the sandbox off** |
| +(`scripts/edge-roll-witness.sh`; the user bus is closed in the sandbox). It builds this tree |
| +into `$T/new/carlos` and the merge base with main (pre-socket, from `git archive`) into |
| +`$T/old/carlos`, with `T=$(mktemp -d)` and `$T/current` a symlink to `new`. It writes user |
| +units under `$XDG_RUNTIME_DIR/systemd/user`, with every path written literally (no `$` in |
| +unit text), no `User=`/`Group=`: |
| +- `erb-R.socket`: `ListenStream=127.0.0.1:P` and `:Q`, `ExecStartPre=$T/current system |
| + edge-socket-check -self`. |
| +- `erb-R.service`: `Wants=`/`After=` the socket, `ExecStart=$T/current edge -http |
| + 127.0.0.1:P -https 127.0.0.1:Q -registry $T/registry.db -acme-cache $T/acme`. |
| +- `erb-R.service.d/10-guard.conf`: D4's E-start line with `$T/current … -self` and |
| + `systemctl --user` (no `+`). |
| + |
| +Before measuring, it asserts that the socket is active and that the journal shows |
| +`inherited 2`. A curl loop (`--max-time 30`) then runs through five transitions: |
| +1. `restart`: 0 refused. |
| +2. `stop`+`start`: 0 refused. |
| +3. The switch-over shape: the socket stopped, the service bound itself, then `restart`. |
| + It must serve, and the script reports which side bound first. |
| +4. `current` → old, `restart`: it must serve, the socket must be inactive afterwards, and |
| + the restart must return within 10 s (no job deadlock). |
| +5. `current` → new, `restart`: the socket is back and `inherited 2`. |
| + |
| +It reports refusals and worst latency per transition. A `trap` stops the units, removes the |
| +files, reloads and deletes `$T`. W2 exercises the job engine on the user manager of the |
| +same systemd 257; the system manager's `+` is covered by `systemd-analyze verify` and the |
| +unit test of the line. Its numbers go in the #434 reply. |
| ## 8. Test plan |
| -Run locally only for touched packages, with `-run`; CI does the full run. |
| - |
| -- **D5** (linux, subprocess re-exec): `LISTEN_PID` unset or another pid takes nothing; two |
| - fds matched by port, IP honoured when the flag names one; originals closed and dups |
| - close-on-exec (`F_GETFD`); env unset; a non-listening, non-TCP or wrong-port fd closed and |
| - logged; a missing port bound by the process. **D8:** each log form pinned. |
| -- **D2/D3:** the embedded units pinned (the socket's lines; the service's `Wants=`/`After=` |
| - with no `Requires=`/`BindsTo=`/`PartOf=` naming the socket); the socket in `enabledUnits` |
| - and absent from `armedUnits`, `currentBootstrapPlan().Armed` and `ensureSystemArmed`; |
| - `arm` refuses it. **D4:** `carlos system edge-socket-check` exits 0, prints nothing, and |
| - is not in the usage. |
| -- **D7** (fake `sysOps`): with the socket file, `restartEdge` runs `reset-failed` on both |
| - then `restart`; without it, only the edge is touched. **Rollback invariant** (fake |
| - `sysOps`): with `UnitsAdded` naming the socket, `rollback` and `recoverIfNeeded` run |
| - `disable --now carlos-edge.socket` before any `restart carlos-edge`. This pins the order |
| - the old binaries already follow. |
| -- **D6:** a guard test finds no `shutdown`, `SO_LINGER` or `SO_REUSEPORT` on listener code |
| - in `internal/edge`. |
| -- **D10:** refused then listening within the hold gives 200; refused past a shortened hold |
| - gives 502 at about the hold; a non-loopback addr gives 502 at once; a client cancel |
| - returns at once; a POST body arrives intact after a hold; a WebSocket upgrade works after |
| - a hold; `multiProxy` is unchanged. |
| -- **Witnesses:** W1 in CI; W2 once by hand before handoff, its output in the #434 reply. |
| +Locally only touched packages, with `-run`; CI runs the full set. |
| + |
| +- **INV-R simulation** (`internal/host`, production wiring): a model of PID 1 (unit files |
| + vs loaded state, drop-ins, `daemon-reload`, socket active/failed/enabled, job order) and |
| + of the disk (binary A/O, `carlos.prev`, units, journal). Actors: |
| + - the real `install`, `rollback` and `recoverIfNeeded` over a fake `sysOps` bound to the |
| + model; |
| + - the v0.25.0 `restoreFiles`/`installUnits` and the hand script, transcribed step by step; |
| + - boot: `sockets.target` (G), recovery, then the edge start. |
| + |
| + Each run injects a crash after any step, or a failed `systemctl` call at any step, |
| + including `disable --now`. Every edge start runs the line parsed from the embedded |
| + drop-in, and every socket start runs the real predicate. The invariant is checked at every |
| + modelled `ExecStart` of the edge: never socket active with O on disk. CI runs a fixed 200 |
| + seeds; one 3000-seed sweep by hand per revision (nice 19). |
| +- **Predicate:** passes only with the drop-in loaded and byte-equal, and the shipped argv |
| + (both forms); fails on file-only (not reloaded), a different body, extra flags or `-dev`; |
| + `-self` exits 0; hidden from usage. |
| +- **E-start line**, run under `/bin/sh` with a fake binary and `systemctl` on `PATH`: |
| + - pass: no stop; |
| + - fail with the socket active: stop; |
| + - fail and stop fails while still active: exits non-zero; |
| + - socket not loaded: exits 0. |
| + |
| + `systemd-analyze verify` on the units and the drop-in. |
| +- **Guard tests:** nothing writes into a `-root` staging tree's `carlos-edge.service.d`; |
| + no code removes the drop-in; both writers write it before `daemon-reload`. |
| +- **D2/D3:** embedded units pinned (no `User=`, no `Requires=`/`BindsTo=`/`PartOf=` naming |
| + the socket); socket enabled, never armed; `arm` refuses it. |
| +- **D5:** env and close-on-exec handling; compatible, absent and incompatible cases (wildcard |
| + inherited vs `-http 127.0.0.1:80` exits with the message); non-listening or non-TCP fds |
| + closed. **D8** forms pinned. |
| +- **D6** guard test; **D7** with and without the socket file. |
| +- **D10:** refused then listening within the hold gives 200; past a shortened hold, 502; a |
| + non-loopback addr gives 502 at once; a client cancel returns at once; a POST body arrives |
| + intact; a WebSocket upgrade works after a hold. |
| +- **D10b:** |
| + - a single local upstream refusing before the probe notices, then back: 200 once |
| + readmitted, never a dial to it while ejected; |
| + - the live set already empty: same; |
| + - still down past the hold: 503 with `Retry-After`; |
| + - remote-only route: 503 at once; |
| + - mixed set with a live remote: served at once; |
| + - the hold from `rise_after`/`interval_ms`, capped at 30 s. |
| +- **Witnesses:** W1 in CI; W2 once by hand before handoff. |
| ## 9. Out of scope |
| -Part C (instances surviving an edge restart: `BindsTo=`, #440 item 1). Socket-activating |
| -`console.service` (not in this repo). The relay's ports. `Type=notify` readiness. Dropping |
| -`CAP_NET_BIND_SERVICE`. The infra script (row 14). Any AWS, DNS or box change; release, |
| -roll and merge. A drain shorter than 10 s. |
| - |
| -## 10. Open questions (not settled by evidence here) |
| - |
| -1. **Row 4's order.** At first install, does the socket start after the old edge stops? |
| - `man systemd.unit`'s ordering rule says yes, and W2's third transition shows it on 257. |
| - Both outcomes serve. |
| -2. **The console route's shape on real boxes.** The design assumes it is unprobed (`Health` |
| - empty, so `tcpProxy`). If it is probed, it already answers 503 with `Retry-After`, and |
| - D10 does not reach it. Check with `carlos routes` on dev. |
| -3. **Does 13 s cover a real console restart?** The observed gap is ~10 s, and the dev roll |
| - measures it. Raising the one constant (to at most 30 s) is a one-line follow-up. |
| +Part C (`BindsTo=`, #440 item 1). Socket-activating `console.service`. The relay's ports. |
| +`Type=notify`. Dropping `CAP_NET_BIND_SERVICE`. Infra and the hand script. Any AWS, DNS or |
| +box change; release, roll, merge. A drain under 10 s. |
| + |
| +## 10. Open questions and Paul's option |
| + |
| +1. **The switch-over order (row 5).** Does the socket start only after the old edge has |
| + stopped? `man systemd.unit` says shutdown is ordered first, and W2 transition 3 shows it |
| + on 257. Both outcomes serve. |
| +2. **Does 17 s / 13 s cover a real console restart?** It is ~10 s observed; the dev roll |
| + measures it. Raising `localDialHold` (cap 30 s) is a one-line follow-up. |
| +3. **Paul's option, one roll sooner (not taken).** `bootstrap -root` runs as root with the |
| + candidate binary, under both an old applier and the hand script. Letting it also write the |
| + live drop-in (best-effort, root only) would make roll 1 the switch-over. The same INV-R |
| + argument holds, because E-start would exist before the restart. The cost is that a |
| + staging command changes the live box, before the journal and even for a candidate that |
| + is then refused. Not taken without Paul's say. |
| + |
| +## 11. Round 1 dispositions |
| + |
| +1. **P1-1, INV-R: fixed at the root.** INV-R is now stated and enforced in one place |
| + (E-start, a drop-in that survives old unit sets). The socket's gate keeps it from being |
| + active without E-start. Activation became two-step, so no old code meets an active |
| + socket (§4 rows 4-10). The false "active socket implies socket-aware binary" claim is |
| + gone. Every failure point in `restoreFiles`/`installUnits`, boot recovery (which runs |
| + `carlos.prev`'s code), runtime `OnFailure`, rollback by an old applier and the hand |
| + script is covered by row. An INV-R simulation is added (§8). |
| +2. **P1-2, INV-H: fixed.** D10b holds probed routes with a local upstream. It waits for |
| + readmission by the prober and never dials an ejected upstream, and its hold is derived |
| + from `rise_after × interval`. The console route's shape is settled from the repo as |
| + "either", and both are now covered (§1.5). |
| +3. **P2 D5: fixed.** Absent, compatible and incompatible listeners are distinct. An |
| + incompatible one exits with a message, and the gate refuses non-shipped argv before |
| + activation. The availability claim is gone. |
| +4. **P2 floor: fixed.** v0.25.0 and conditional (`systemapply.go:583`). It is no longer |
| + cited, and the design needs no applier baseline (§1.3). |
| +5. **P2 W2: fixed.** `$T/current` is used in every command, there is no `User=`/`Group=`, |
| + and socket active plus `inherited 2` are asserted before measuring. |
| +6. **P2 fd store: fixed.** The table distinguishes `restart`, `no` and `yes`. The deciding |
| + reason is now INV-R: an fd store cannot be cleared from the edge's own start. |
| + |
| +None declined. |