| new file mode 100644 |
| index 000000000..758bbcc5a |
| --- /dev/null |
| +++ b/docs/superpowers/plans/2026-11-24-hub-console-socket.md |
| @@ -0,0 +1,203 @@ |
| +# Hub reaches the console through a root-created socket: implementation plan |
| + |
| +**Goal:** A tunnel hub with no `CARLOS_REPLICA_CONSOLE_ADDR` forwards fleet |
| +boxes' console calls to `/run/carlos-console/fleet.sock`, a socket the |
| +console binds in a runtime directory root's install gives `console.service`, |
| +used only while root's record says the console holds that directory alone. |
| + |
| +**Spec:** `docs/superpowers/specs/2026-11-24-hub-console-address-from-install-design.md` |
| +rev 3 (approved by Paul 2026-11-24; design review closed, §12, §14, §15). |
| +Read it first. One slice, one branch: `console-addr-from-install`. |
| + |
| +## Global constraints |
| + |
| +- Constants, in one new leaf package `internal/consolefleet` (no imports |
| + beyond the standard library and `net/http`): |
| + - `RuntimeDirName = "carlos-console"`, `Dir = "/run/carlos-console"`, |
| + `Socket = "/run/carlos-console/fleet.sock"`; |
| + - `DropInPath = "/etc/systemd/system/console.service.d/70-carlos-fleet-socket.conf"`, |
| + `DropInBody = "[Service]\nRuntimeDirectory=carlos-console\n"`; |
| + - `StatusDir = "/var/lib/carlos-console-fleet"`, record `status.json`, |
| + marker `reload-pending`; |
| + - `Record{Schema int; Outcome, Reason string; Exclusive bool; At time.Time}` |
| + and `DecodeRecord(raw []byte) (Record, error)`: at most 4096 bytes, one |
| + document, unknown fields refused, schema 1; |
| + - `BoxDoor(r *http.Request) bool`: today's `boxConsolePath` |
| + (`internal/edge/replicaconsole.go:134-145`), moved here so the edge and |
| + the console share it. |
| +- Outcomes, exactly: `installed`, `unchanged`, `removed`, `absent`, |
| + `failed`. |
| +- Edge sources, exactly: `env`, `socket`, `route-scan`. Results: `ok`, or |
| + the HTTP status the edge answered (`502`, `503`). |
| +- Nothing here reads a route record or the registry to decide the socket. |
| +- Tests first in every task. Linux-only tests run in Docker: |
| + `docker run --rm -v <worktree>:/src -v $HOME/go/pkg/mod:/go/pkg/mod -w /src golang:1.26.10 go test ...`. |
| + On the Mac, `GOCACHE=/tmp/g3/gocache`. |
| +- Every Go change: `gofmt`, `go vet`, `make code-health` before pushing. |
| + |
| +## Tasks |
| + |
| +### Task 1: the reserved prefix (security gate, own Codex review) |
| + |
| +Files: `internal/caps/caps.go`, `internal/caps/converge.go`, new |
| +`internal/caps/runtimedir_guard_test.go`, `internal/caps/caps_test.go`. |
| + |
| +- Add `ReservedRuntimeDir(name string) bool`: true for the existing map, |
| + the `systemd-` prefix and the `carlos-` prefix. `ValidCapabilities` |
| + calls it in place of its inline check (`caps.go:355`). Ship |
| + (`cli.go:2818`), adopt (`release.go:485`), converge (`converge.go:228`) |
| + and render (`render.go:152`) all go through `ValidCapabilities`, so all |
| + refuse. |
| +- Add `RuntimeDirClaimant(etcDir, name string) (host string, ok bool)` |
| + over the existing `scanClaims` (`converge.go:775`) with no excepted host. |
| + An unreadable `etcDir` returns `ok=true` with host `"?"` (fail closed: |
| + root cannot show the name is free); `scanClaims` keeps its current |
| + behaviour for its existing caller. |
| +- Guard test `TestRuntimeDirectoryRenderSites`: parse every non-test `.go` |
| + file in the module (`go/parser`), collect string literals containing |
| + `RuntimeDirectory`, and every file under `internal/host/units` and |
| + `infra/` containing it. The set of files must equal an allowlist: |
| + `internal/caps/render.go`, `internal/consolefleet/consolefleet.go`, |
| + `internal/demobuild/gvisor.go`, the three `carlos-runsc` units and |
| + `infra/devbox/carlos-devbox.service` (`carlos-devbox`). Each |
| + allowlisted literal outside caps names a `carlos-` directory. A new site |
| + fails the test, naming the file, and needs a reviewed allowlist edit. |
| +- Tests: `carlos-console`, `carlos-x` and `carlos-` refused by |
| + `ValidCapabilities` and by `RenderService`; `carlosx` (no hyphen) still |
| + allowed; `RuntimeDirClaimant` finds a claim in a fake etc dir, and fails |
| + closed on an unreadable one. |
| + |
| +### Task 2: the shared package |
| + |
| +Files: new `internal/consolefleet/consolefleet.go` and its test; |
| +`internal/edge/replicaconsole.go` uses `consolefleet.BoxDoor`. |
| + |
| +Tests: `DecodeRecord` refuses oversize, two documents, unknown fields, |
| +schema 0 and 2; `BoxDoor` keeps every case of the existing |
| +`boxConsolePath` tests. |
| + |
| +### Task 3: root's console step (root, own Codex review) |
| + |
| +Files: new `internal/host/consolefleet.go` and |
| +`internal/host/consolefleet_test.go`; one call in |
| +`runAdoptUnderRootRelease` (`internal/host/agent.go`, after |
| +`runNamedUnitsStep`, before the release-key parse, never fatal). |
| + |
| +A `consoleFleetStep` struct with injected seams (production values in |
| +`productionConsoleFleetStep`): |
| +- `show() (unitProps, error)`: one `systemctl show console.service -p |
| + Id,LoadState,ActiveState,RuntimeDirectory,RuntimeDirectoryMode,RuntimeDirectoryPreserve` |
| + through `systemd.Command` with its query timeout, parsed strictly (one |
| + block, `Id=console.service`, every asked key present once); |
| +- `reload() error`: `daemonReload` (`unitops.go`); |
| +- drop-in read through `rootstate.OpenTrustedFile`, write with |
| + `hostfloor.Durable(path, body, 0644)` after `os.MkdirAll(dir, 0755)`, |
| + remove with `os.Remove` and a directory sync; |
| +- the store: `rootstate.Open(StatusDir, true, true)` for `status.json` |
| + and the `reload-pending` marker; |
| +- `claimant`: `caps.RuntimeDirClaimant("/etc/systemd/system", RuntimeDirName)`; |
| +- `runDirExists`: `os.Lstat(consolefleet.Dir)`. |
| + |
| +`run()` follows spec §4 steps 1-5 exactly, then writes the record only |
| +when its bytes (with `At` from the step's clock) differ in anything but |
| +`At`, so an idle hourly pass does not churn the file. Tests cover every |
| +branch in spec §10's root list, with fakes for every seam, plus the |
| +`systemctl show` parser over real-format fixtures (missing key, two |
| +blocks, wrong Id, `RuntimeDirectory=` empty and with several names). |
| + |
| +### Task 4: the edge (gate, own Codex review) |
| + |
| +Files: `internal/edge/replicaconsole.go`, new |
| +`internal/edge/replicaconsole_socket_test.go`. |
| + |
| +- `Edge` gains `consoleFleet consoleFleetSource` (seams: `record() |
| + (consolefleet.Record, error)`, `dir`, `socket` paths), set in `New` |
| + to production values; tests replace it. |
| +- `serveReplicaConsole`: env proxy as today (source `env`); otherwise |
| + `socketReady()` (record decodes and `Exclusive`; `lstat` of the dir is a |
| + directory, not a symlink, mode `&022 == 0`; `lstat` of the socket is a |
| + socket) selects one cached unix-socket proxy built like |
| + `newReplicaConsoleProxy` with a `DialContext` to the socket path (source |
| + `socket`); otherwise the route scan unchanged (source `route-scan`). |
| + A failed check logs its reason once per distinct reason. |
| +- Last call: one `atomic.Pointer[ReplicaConsoleCall]` on the edge |
| + (`Source`, `Result`, `At`), set after each box call from a status- |
| + recording wrapper; exported `(*Edge).LastReplicaConsoleCall()`. |
| +- Production record read: `rootstate.OpenTrustedFile` + |
| + `rootstate.CheckPublicConfig` + `consolefleet.DecodeRecord`. |
| +- Tests (real unix listener in `t.TempDir()`): env wins over a ready |
| + socket; record missing, malformed, `exclusive:false` each fall to the |
| + route scan; dir symlink, dir 0777, socket a regular file each fall to |
| + the route scan; ready socket forwards with the hub's version header and |
| + the box's token; a route row naming another upstream changes nothing |
| + while the socket is used; listener closed after the check answers 502; |
| + last-call source, result and time for each path; "no call yet" is nil. |
| + |
| +### Task 5: the console's listener |
| + |
| +Files: `cmd/console/main.go` (serve), `cmd/console/stop.go`, new |
| +`cmd/console/fleetsocket.go` and test. Out of census scope. |
| + |
| +- After the TCP listener: if `consolefleet.Dir` is a directory, not a |
| + symlink, owned by `os.Geteuid()`, mode `&022 == 0`: remove a stale |
| + socket, `net.Listen("unix", consolefleet.Socket)`, `chmod 0666`. |
| + Otherwise log once and continue on TCP only. |
| +- The server's handler is wrapped: a request whose |
| + `http.LocalAddrContextKey` is a unix address and that is not |
| + `consolefleet.BoxDoor` gets 404. |
| +- `consoleStop.serve` serves the extra listener on the same `http.Server` |
| + (one `Shutdown` closes both); an error from the fleet listener is logged, |
| + never fatal. |
| +- Tests: the wrapper's 404 and pass-through; the directory checks (owner, |
| + mode, symlink); a request over the socket reaches the box-door handler. |
| + |
| +### Task 6: visibility (UX pass, then copy, then build) |
| + |
| +Files: `internal/status/snapshot.go`, `internal/host/agent.go` (probe), |
| +`internal/console/apisystemupdate.go`, `system.go`, the CLI golden files, |
| +`docs/site/cli.md` if it shows the output. |
| + |
| +1. **UX pass** first, recorded in |
| + `docs/superpowers/notes/copy/hub-console-socket.md`: the suffix on a |
| + box's `carlos system status` line, leading with state: `console: socket |
| + · ok 2m ago`, `console: env · ok 2m ago`, `console: route scan · 503 1m |
| + ago`, `console: no call yet`, and, when root's record is not exclusive, |
| + `console: socket unused (<reason>)`. Colour carries ok, warn and bad. |
| +2. **Copy review** of those strings and any new log lines by a Luna pass |
| + (`codex exec -s read-only`), recorded in the same note. |
| +3. Build: `status.ConsoleFleet{Record *consolefleet.Record; Source, |
| + Result string; At time.Time}` on `Probes` and `BoxMetrics` |
| + (`console_fleet`, omitempty, private like `named_units`); the agent |
| + fills it from root's record and `LastReplicaConsoleCall()`; the API row |
| + adds `console_fleet`; the CLI decodes and prints it. Tests at each hop |
| + and `TestCLIGolden`. |
| + |
| +### Task 7: docs |
| + |
| +- `replicaconsole.go:16-26`: the comment names the three sources and the |
| + reserved prefix. |
| +- README's replica or self-host section: the env var remains the override; |
| + platform hubs need nothing. |
| +- The spec's §9 rollout stays the operator's checklist. |
| + |
| +### Task 8: census, reviews, merge |
| + |
| +1. `make census-card`; a CLAIMS file for every changed unit (caps gate, |
| + root step, edge, status, shared package); `make census-propose |
| + NAME=console-addr-from-install CLAIMS=<file>`; add any new census file |
| + to `scripts/gitleaks-gate/notscanned.go` if gitleaks reports digests. |
| +2. Per-task Codex reviews (Tasks 1, 3, 4) under the stopping rules, with |
| + briefs and findings beside this plan. |
| +3. Codex census review of the card; verdict recorded on #280. |
| +4. One whole-branch review by Fable. |
| +5. Before pushing: `go build ./...`, `go vet ./...`, the touched packages' |
| + tests, the claim-route census, linked policy, record creators, copy |
| + pins, `TestCLIGolden`, `make code-health`, `gofmt -l .`. |
| +6. Push, describe the branch, wait for green CI, then |
| + `amadan branch merge carlos/platform console-addr-from-install -squash |
| + -expect <tip> -message …`. |
| + |
| +## Review sizing (AGENTS.md stopping rule 3) |
| + |
| +Own reviews: Task 1 (security gate), Task 3 (root), Task 4 (gate). Tasks |
| +2, 5, 6 and 7 are covered by the whole-branch review. |