carlos / platform Public

devbox on macOS (#488): plan (launchd socket activation, LOCAL_PEERCRED, --env-file)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 6c6cde4826b34cb7229f1f40c5309ff9a7c47ed3 parent a2e23ba
1 file changed, +101 −0
  • docs/superpowers/plans/2026-10-31-devbox-macos.md +101 −0

CI passed — run details

Intent

Attributed to this commit: 52.4 g CO₂e (6,337,600 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on devbox-macos, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.

diff --git a/docs/superpowers/plans/2026-10-31-devbox-macos.md b/docs/superpowers/plans/2026-10-31-devbox-macos.md
new file mode 100644
index 000000000..8fd228dd0
--- /dev/null
+++ b/docs/superpowers/plans/2026-10-31-devbox-macos.md
@@ -0,0 +1,101 @@
+# Devbox mode on macOS (#488) Implementation Plan
+
+> **For agentic workers:** steps use checkbox (`- [ ]`) syntax. One implementer builds Tasks 2–4 in order; the controller runs Task 1's copy review with Paul and the reviews.
+
+**Goal:** `carlos devbox` runs on a macOS devbox (the second shared devbox) exactly as on Linux: owned, tailnet-only `https://<name>.<devbox domain>` links on port 443, with nothing of ours running as root.
+
+**Architecture:** launchd (root) binds `<tailnet-ip>:443` from a `Sockets` entry in a LaunchDaemon plist, and hands the listening socket to `carlos devbox serve`, which runs as its own user (Paul's ruling, 2026-10-31: "launchd socket", over bind-then-drop, pf, or a port in URLs). The registrar identifies callers with `LOCAL_PEERCRED` instead of `SO_PEERCRED`. A LaunchDaemon plist is world-readable, so the DNS credential comes from an env file that `serve` reads itself (`--env-file`, mode 0600, owned by the service user).
+
+**Spec:** docs/superpowers/specs/2026-10-30-devbox-mode-design.md (devbox mode; its anti-goals all hold on macOS) and amadan #488. Evidence: on the macOS devbox, binding `100.76.110.54:443` as an ordinary user fails with EACCES (Doc's test, 2026-10-31).
+
+## Global Constraints
+
+- Nothing of ours runs as root. launchd does the bind; `carlos devbox serve` never calls setuid or checks euid.
+- Listen only on the configured tailnet address. A launchd-passed listener whose local address is unspecified (0.0.0.0, ::) or not the configured `CARLOS_DEVBOX_LISTEN` address is refused, the same as a self-bound one.
+- Peer identity fails closed: no uid, no identity. Every request on such a connection is refused (`bad-request`).
+- No cgo. CI cross-builds darwin with CGO_ENABLED=0. Call libSystem with the x/sys/unix pattern (`//go:cgo_import_dynamic` plus an assembly trampoline plus `syscall_syscall`), in `_darwin` files only.
+- Linux behaviour and Linux files are unchanged, apart from `--env-file`, which works on both.
+- User-facing strings come only from the approved copy deck (DBX-C58 onwards, appended to docs/superpowers/plans/2026-10-30-devbox-mode-copy.md). No em dashes. Neutral names (devbox.example.com, 100.64.0.10).
+- Tests that can only run on darwin are `//go:build darwin`. The gate is `GOOS=darwin go vet` and test compilation (`GOOS=darwin go test -c` per package) on DOCS-T1, plus whatever runs on linux.
+
+## Review Focus
+
+1. **A launchd listener on the wrong address** (unspecified, or not `CARLOS_DEVBOX_LISTEN`): refused before serving. Test in Task 3.
+2. **An env file readable by group or other, or owned by someone else:** refused, with the deck's message naming the path. Test in Task 2.
+3. **An env file line that isn't `KEY=VALUE`, has a duplicate key, or tries to override something already in the environment:** refused, or the rule is stated. The ruling: the file wins and duplicates are refused. Test in Task 2.
+4. **`launch_activate_socket` returning ESRCH, because the service wasn't started by launchd** (someone runs `serve` by hand on a Mac without `--listen`): a clear error, not a hang. Test in Task 3 (darwin).
+5. **A peer uid of 0 from LOCAL_PEERCRED:** treated like any other uid. Root can own links, the same as on Linux. No special case.
+
+---
+
+### Task 1: Copy (controller and Paul)
+
+Draft rows DBX-C58+ for:
+- the `--env-file` flag help;
+- the env-file refusals (unreadable, too open, wrong owner, bad line, duplicate key);
+- the "not started by launchd" serve error;
+- the runbook's macOS section: create the service user (`sysadminctl` or `dscl`), install the binary and plist, the env file, the launchd socket, `launchctl bootstrap`, the DNS and IAM steps, and the smoke checklist pointer.
+
+Run copy review with Paul, apply his edits verbatim, and bump the deck gate's pinned inventory.
+
+### Task 2: `--env-file` and macOS defaults
+
+**Files:** devbox_cli.go and devbox_cli_test.go (root package).
+
+- [ ] Tests:
+ - `TestDevboxServeEnvFileLoads`: a 0600 file owned by the current user, with `KEY=VALUE` lines, blanks and `#` comments, sets those variables before serve reads its config.
+ - `TestDevboxServeEnvFileRefusesOpenMode`: 0640 and 0604 are refused with the deck's message.
+ - `TestDevboxServeEnvFileRefusesBadLine`: a line without `=`, and an empty key, are refused.
+ - `TestDevboxServeEnvFileRefusesDuplicate`.
+ - `TestDevboxDefaultSocketPath`: the default socket is `/usr/local/var/run/carlos-devbox/devbox.sock` on darwin and `/run/carlos-devbox/devbox.sock` elsewhere. Use a GOOS seam; `CARLOS_DEVBOX_SOCKET` still overrides.
+- [ ] Implement `--env-file PATH` on `carlos devbox serve` (both OSes). Owner checks use the file's uid against `os.Geteuid()`. Values are never logged.
+- [ ] Run `TMPDIR=/tmp/g go test -run 'TestDevbox' .`, and the copy gate.
+- [ ] Commit.
+
+### Task 3: launchd socket activation and the registrar on darwin
+
+**Files:**
+- `internal/devbox/peer_darwin.go`;
+- `peer_other.go` (build tag becomes `!linux && !darwin`);
+- `internal/edge/devbox.go` (`DevboxConfig.Listener net.Listener`);
+- new `internal/launchd/` (`activate_darwin.go`, `activate_darwin_amd64.s` and `activate_darwin_arm64.s` trampolines, `activate_other.go` stub);
+- `devbox_cli.go` (serve takes the launchd listener when `CARLOS_DEVBOX_LAUNCHD_SOCKET` names one).
+
+**Interfaces:**
+- `func launchd.Listeners(name string) ([]net.Listener, error)`. It calls `launch_activate_socket(name, &fds, &cnt)`, wraps each fd with `net.FileListener(os.NewFile(...))`, and frees the C array with libSystem `free`. On non-darwin it returns `errors.ErrUnsupported`.
+- `edge.DevboxConfig.Listener`: when non-nil, ServeDevbox serves TLS on it instead of binding. It validates `Listener.Addr()` against `ListenAddr` (same IP and port), and the address rule from Review Focus 1 applies.
+
+- [ ] Tests:
+ - linux: `TestDevboxUsesGivenListener` (serves on an injected 127.0.0.1 listener when `ListenAddr` matches it);
+ - linux: `TestDevboxRefusesListenerOnOtherAddr`, and an unspecified listener address;
+ - darwin-only: `TestPeerUIDDarwin` (a Unix socket pair attributes `os.Getuid()`);
+ - darwin-only: `TestLaunchdListenersOutsideLaunchd` (returns an error, does not hang).
+- [ ] Implement. `PeerUID` on darwin uses `unix.GetsockoptXucred(fd, unix.SOL_LOCAL, unix.LOCAL_PEERCRED)` and checks `Version == XUCRED_VERSION`.
+- [ ] Run the linux tests with `go test ./internal/edge -run TestDevbox` and `go test ./internal/devbox`. Then run `GOOS=darwin GOARCH=arm64 go vet ./internal/devbox ./internal/edge ./internal/launchd .`, the same for amd64, and `GOOS=darwin go test -c -o /dev/null` for `./internal/devbox` and `./internal/launchd`.
+- [ ] Commit.
+
+### Task 4: The plist and the runbook
+
+**Files:** `infra/devbox/com.carlosframework.devbox.plist`, and a macOS section in `infra/devbox/README.md` (prose from Task 1's approved rows).
+
+- [ ] The plist sets:
+ - `Label com.carlosframework.devbox`;
+ - `ProgramArguments`: `/usr/local/bin/carlos devbox serve --env-file /usr/local/etc/carlos-devbox/env`;
+ - `UserName` and `GroupName`: `_carlos-devbox`;
+ - `Sockets`: `Listeners`, with `SockNodeName` 100.64.0.10 (the placeholder the runbook replaces), `SockServiceName` 443, `SockType` stream and `SockFamily` IPv4;
+ - `RunAtLoad`, and `KeepAlive` true;
+ - stdout and stderr to `/usr/local/var/log/carlos-devbox.log`;
+ - `EnvironmentVariables`: `CARLOS_DEVBOX_LAUNCHD_SOCKET=Listeners` only, with no secrets.
+- [ ] The runbook's macOS steps:
+ 1. create the `_carlos-devbox` role user;
+ 2. create `/usr/local/var/carlos-devbox` (0700) and `/usr/local/var/run/carlos-devbox` (0755), owned by it;
+ 3. write `/usr/local/etc/carlos-devbox/env` (0600, owned by it) with `CARLOS_DATA_DIR=/usr/local/var/carlos-devbox`;
+ 4. install the plist in `/Library/LaunchDaemons` (root:wheel 0644);
+ 5. `launchctl bootstrap system ...`.
+ Also note: if the tailnet address isn't up when launchd loads, the bind fails; re-run with `launchctl kickstart -k system/com.carlosframework.devbox`.
+- [ ] `plutil -lint` can't run on Linux. Validate the plist as XML with Go's `encoding/xml` in a test (`TestDevboxPlistIsWellFormed`), and check that it carries no `AWS_` key.
+- [ ] Commit.
+
+## After the build
+
+Astra reviews the whole branch: peer identity, listener validation and env-file handling are security-relevant. Then the census (`make census-card` / `census-propose`; darwin files are outside its linux scope, but devbox_cli.go and internal/edge change), CI, and a merge on Paul's go. Install on the macOS devbox: DNS `*.tito-studio.dev.platform.tito.io` → 100.76.110.54, an IAM user scoped to its challenge record, and the runbook run by someone with admin on the Mac.