carlos / platform Public

docs: implementation plan for the hub console socket

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 38f8af18aa9c82eff491522aaff1338f4959924b parent 17a503e
2 files changed, +280 −0
  • docs/superpowers/plans/2026-11-24-hub-console-socket.md +203 −0
  • docs/superpowers/plans/CODEX-REVIEW-BRIEF-hub-console-socket-plan.md +77 −0
diff --git a/docs/superpowers/plans/2026-11-24-hub-console-socket.md b/docs/superpowers/plans/2026-11-24-hub-console-socket.md
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.
diff --git a/docs/superpowers/plans/CODEX-REVIEW-BRIEF-hub-console-socket-plan.md b/docs/superpowers/plans/CODEX-REVIEW-BRIEF-hub-console-socket-plan.md
new file mode 100644
index 000000000..bfca23815
--- /dev/null
+++ b/docs/superpowers/plans/CODEX-REVIEW-BRIEF-hub-console-socket-plan.md
@@ -0,0 +1,77 @@
+# Review brief: hub console socket (implementation plan), round 1 of 3
+
+Repo carlos/platform, branch `console-addr-from-install`, commit <SHA>.
+Under review: `docs/superpowers/plans/2026-11-24-hub-console-socket.md`.
+Authorities: `docs/superpowers/specs/2026-11-24-hub-console-address-from-install-design.md`
+rev 3 on this branch, whose design review is CLOSED (Paul's decisions of
+2026-11-24 are its §12, §14 and §15; its findings files sit beside it), and
+the code on `main` (this branch is main plus docs). Do not review against
+other unmerged branches; if this work needs one, say so as a P1 and stop.
+
+You are an independent reviewer. Read the plan, then the spec, then the code
+it names, and check every claim against that code.
+
+## Fixed: do not re-argue
+
+- The design (spec rev 3) and Paul's decisions: the `carlos-` runtime
+ directory prefix reserved in `internal/caps` with a guard test; a
+ root-written drop-in plus a console-owned socket is "the console install
+ writes it"; the route scan stays one release, then goes. Report only plan
+ steps that fail to implement the spec, or that would not work.
+- Round 1 P1-2 of the design review (version-path provenance) is declined;
+ the design review agreed in round 2.
+
+## What to check
+
+1. Does every task implement its spec section exactly, and does the plan
+ miss anything the spec requires (§4, §4a, §5, §6, §7, §8, §10)?
+2. Task 1: does routing everything through `ValidCapabilities` really
+ cover ship, adopt, converge and render? Is the guard test's scan sound
+ (what could spell the directive and escape it)? Is `RuntimeDirClaimant`'s
+ fail-closed behaviour right?
+3. Task 3: the `systemctl show` properties and their output format, the
+ marker ordering, `hostfloor.Durable` and `rootstate` store semantics,
+ placement in `runAdoptUnderRootRelease` (`internal/host/agent.go`).
+4. Task 4: the edge's selection, the unix proxy, the last-call record, and
+ whether the tests would catch a regression in the trust rule at
+ `internal/edge/replicaconsole.go:16-26`.
+5. Task 5: serving a second listener through `consoleStop`
+ (`cmd/console/stop.go`) and its shutdown; `http.LocalAddrContextKey`
+ for a unix listener.
+6. Task 6: the heartbeat to API to CLI path, the UX pass and copy steps
+ in the order AGENTS.md requires.
+7. Task 8 and the review sizing: census scope, the guard tests named, the
+ merge steps.
+
+## Severity
+
+- **P1**: unsafe, would not compile, a test fails, or it would not work.
+ Blocks merge.
+- **P2**: wrong or missing. Fixed without reopening this review. (The
+ census, and review of any newly changed security, root, termination
+ or production-storage behaviour, still check the fix; see AGENTS.md,
+ "Stopping rules".)
+- **P3**: clarity or polish. Goes on a follow-up list; never a reason for
+ another round.
+
+A P1 states its failure scenario and the evidence (file:line). Do not
+raise a P2 to P1 to keep the review open.
+
+## Output
+
+Findings ranked P1, P2, P3. Each gives the section or task, the problem,
+the evidence (file:line, verified), the failure scenario, a concrete
+fix and the invariant or trust boundary it breaks (that is its fault
+family). End with exactly one verdict:
+
+- **READY**: no P1 and no P2.
+- **READY AFTER FIXES**: no P1. The listed P2s are fixed without another
+ round.
+- **NOT READY**: one or more P1s, listed.
+
+In round 3, a NOT READY also says whether the P1s share one cause, and
+whether refusing the case up front or cutting the feature would remove
+them.
+
+Do not edit files. Do not spawn Anthropic-model reviewers or subagents of
+your own. Do not run the full test suites.