edge-roll-blip: design spec (#434 part B)
carlos-edge.socket holds :80/:443 in PID 1; the edge inherits via LISTEN_FDS and binds itself when handed none. Enabled, never armed; ExecStartPre refuses a pre-socket binary; rollback relies on the UnitsAdded disable --now every applier since v0.18.0 runs. Loopback TCP upstreams get the existing 13 s refused-dial hold. Every binary/unit mix walked; one off-road manual downgrade case documented as unsafe.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+486
−0
docs/superpowers/specs/2026-10-27-edge-roll-blip-design.md+486 −0
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| new file mode 100644 |
| index 000000000..152cfa0ca |
| --- /dev/null |
| +++ b/docs/superpowers/specs/2026-10-27-edge-roll-blip-design.md |
| @@ -0,0 +1,486 @@ |
| +# 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. |
| + |
| +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. |
| + |
| +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). |
| + |
| +## 1. Evidence |
| + |
| +### 1.1 Public ports the edge binds |
| + |
| +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. |
| + |
| +### 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`); |
| +- 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`): `disable --now` for each removed |
| + unit (`:1388-1390`), copy, sysusers, tmpfiles, `daemon-reload` (`:1478`); |
| +5. restarts instance-control, then the edge (`:658`). `restartEdge` is `reset-failed` |
| + then `systemctl restart carlos-edge` (`:1490-1494`); |
| +6. runs the gate: within 20 s (`:59`) the edge must be active, answer |
| + `http://127.0.0.1/` under a 3 s client timeout (`:214-224`) and report the target. A |
| + soak follows, during which `NRestarts` must stay 0 (`:1569-1614`); |
| +7. `finish`, whose `arm` starts the new plan's `Armed` list (`:1728-1764`). `arm` 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. |
| + |
| +### 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. |
| + |
| +### 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. |
| + |
| +## 2. D1: the mechanism |
| + |
| +| | `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 | |
| +| 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. |
| +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. |
| + |
| +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`) |
| + |
| +## 3. Design |
| + |
| +**D2. The units.** A new file, `internal/host/units/carlos-edge.socket`: |
| + |
| +``` |
| +[Unit] |
| +Description=carlos edge public listeners (:80, :443) |
| +[Socket] |
| +ListenStream=80 |
| +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=` is capped by `net.core.somaxconn`, which is 4096 here; |
| +- `FlushPending=` is no; |
| +- `TriggerLimit*` is unchanged. If it 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 |
| +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. |
| + |
| +*Layout.* 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 open file description that |
| +PID 1 shares. That is 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. The edge still needs all three for its outbound dials. |
| +- `@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 * |
| +time.Second`, replaces the two literals at `genproxy.go:40,86`. |
| + |
| +`tcpProxy` gets a `DialContext` when the addr is local, meaning a literal loopback IP |
| +(`netip.Addr.IsLoopback`) or `localhost`. The dial retries `ECONNREFUSED` every 100 ms until |
| +it connects, its context ends, or the hold runs out, and 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 |
| +through `tcpProxy`, 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 measured gap (~10 s) fits inside it; |
| +- it is under the wake hold's 30 s; |
| +- one policy for "a local upstream is briefly absent", not two. |
| + |
| +Safety. A dial that fails 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`). So 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. That is how unix routes already behave. |
| + |
| +A dial still refused at the cap stays `CauseRefused`, answered with a 502. The |
| +`client_gone` and 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. |
| + |
| +## 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 | |
| + |
| +## 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. |
| + |
| +## 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. |
| + |
| +## 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 and keeps both fds. |
| +- It launches the child through `/bin/sh -c 'LISTEN_PID=$$ LISTEN_FDS=2 exec "$0" "$@"'` |
| + with `ExtraFiles` set to the two fds. `$$` 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`, with `-http`/`-https` set to those addresses and 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`. The test |
| +reports the measured gap (the worst latency across a restart) and each restart's duration. |
| +A negative control runs children that inherit nothing and bind by themselves; it must count |
| +refusals greater than 0. |
| + |
| +**W2: a script, run once by hand on DOCS-T1 with the sandbox off.** File: |
| +`scripts/edge-roll-witness.sh`. `systemd-socket-activate` cannot restart its child, and the |
| +user bus is closed inside the sandbox, so this uses `systemctl --user`. |
| + |
| +- It builds `carlos` into `mktemp -d`. |
| +- It writes `erb-<rand>.socket` and `.service` into `$XDG_RUNTIME_DIR/systemd/user`, with |
| + D2's lines except: listening on `127.0.0.1:P`/`Q`, no `User=`, and running |
| + `carlos edge -http 127.0.0.1:P -https 127.0.0.1:Q -registry … -acme-cache …`. |
| +- It drives a curl load loop (`--max-time 30`) through four transitions: |
| + 1. `restart`: 0 refused; |
| + 2. `stop`+`start`: 0 refused; |
| + 3. the first-install shape: the service running without the socket and binding itself, |
| + then `restart` with `Wants=`. It must serve, and the script reports which bound first; |
| + 4. the rollback shape: `disable --now` the socket, remove it, `daemon-reload`, `restart`. |
| + It must serve, with `bound … itself`. |
| +- It reports refusals and the worst latency for each transition. |
| +- 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. |
| + |
| +## 8. Test plan |
| + |
| +Run locally only for touched packages, with `-run`. CI does the full run. |
| + |
| +- **D5** (linux, via subprocess re-exec): |
| + - `LISTEN_PID` unset or naming another pid: nothing is taken; |
| + - two fds matched by port, with the IP honoured when the flag names one; |
| + - the originals are closed and the dups are close-on-exec (`F_GETFD`); |
| + - the env is unset; |
| + - a non-listening, non-TCP or wrong-port fd is closed and logged; |
| + - a missing port is bound by the process itself. |
| +- **D8:** each log form is pinned. |
| +- **D2/D3:** |
| + - the embedded units are pinned: the socket's lines, and the service's `Wants=`/`After=` |
| + with no `Requires=`/`BindsTo=`/`PartOf=` naming the socket; |
| + - the socket is in `enabledUnits` and absent from `armedUnits`, |
| + `currentBootstrapPlan().Armed` and `ensureSystemArmed`; |
| + - `arm` refuses it. |
| +- **D4:** `carlos system edge-socket-check` exits 0 with no output and is not in the usage. |
| +- **D7** (fake `sysOps`): with the socket file present, `restartEdge` runs `reset-failed` |
| + on both, then `restart`. Without the file it touches only the edge. |
| +- **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:** |
| + - a loopback upstream that is refused, then listens within the hold, gives 200; |
| + - one 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, with its output in the #434 |
| + reply. |
| + |
| +## 9. Out of scope |
| + |
| +- Part C (instances surviving an edge restart: `BindsTo=`, #440 item 1). |
| +- Socket-activating `console.service`, which is 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. |