Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

docs(codex): S6 machine-scoped accounts — reconcile the docs with shipped reality

enes03e1d2d66dbe
diff --git a/docs/SERVER.md b/docs/SERVER.md
index 3aa131c41..c5c71fe77 100644
--- a/docs/SERVER.md
+++ b/docs/SERVER.md
@@ -529,6 +529,27 @@ than a generic failure, see above), full **two-master flow-control coordination*
a single actuator with the renderer), and the web folder picker's **hardcoded start
directory**.
+### Managed Codex accounts (S6)
+
+The Server Edition **arms the Codex record-signing secret** but does **not** host the
+account-management IPC — that surface is desktop-driven over SSH.
+
+- **Arms the record secret (Decision 1).** At boot `armServerNodeIdentity`
+ (`src/server/node-identity-arm.ts`) loads the raw node-auth secret and calls
+ `setCodexThreadIdentityAuthSecret(...)` with it, so a managed Codex account's thread→node→account
+ ownership records can **sign and verify on a headless host**. Headless Linux has no OS keychain, so
+ the secret is 32 **raw bytes** at `node-auth-key.bin`, mode `0600` — the same both-shells channel the
+ desktop seals via `safeStorage`. This only makes the record layer *able to sign*; it is orthogonal to
+ the shared-app-server degrade (Codex nodes still launch bare here, exactly as before).
+- **Does NOT host the account-management verbs.** `initCodexAccounts` registers its IPC over Electron's
+ `ipcMain` with WebContents-owner authorization, which the headless bridge does not provide, so
+ `startServer` never calls it. This is **not** a silent gap: managed Codex logins **on an SSH host**
+ are driven by the **desktop** over SSH (the remote account-add / device-login / import legs + the
+ relay) — the host runs the relay + import, not its own copy of the account IPC.
+- **Fail-closed, both ways.** With no secret armed (unwritable key file), the record layer throws
+ rather than writing anything unsigned, and Codex nodes keep working bare. A machine with **no** managed
+ accounts is byte-for-byte the pre-S6 layout (a bare-root record per thread).
+
## Manual browser smoke checklist
Run against a real browser (this is the human-verified path; the automated harness
diff --git a/docs/node-identity.md b/docs/node-identity.md
index 623f1e508..3981ad471 100644
--- a/docs/node-identity.md
+++ b/docs/node-identity.md
@@ -408,6 +408,40 @@ and the other node reads it. The materialiser refuses tokens for the **whole col
sweeps anything an earlier pass wrote for them; those nodes fall back to `legacy`, the designed
fail-open state.
+## Account scoping (S6): the same secret signs Codex account-scoped records
+
+The restart-stable node-auth secret above does a second job: it signs the Codex thread→node→**account**
+ownership records (`src/core/codex-identity-proxy.ts`). S6 gives a machine the system Codex login
+(`~/.codex`) alongside managed logins, and a record now binds the full 4-tuple
+`(threadId, accountScope, nodeId, hookEndpoint)` so one account can never be made to speak for
+another's threads. The full model is [Shared Codex node identity](shared-codex-node-identity.md); the
+parts that touch this secret and the invariants here:
+
+- **The codex identity secret is armed on both shells.** Desktop and Server Edition both call
+ `setCodexThreadIdentityAuthSecret(loadOrCreateNodeAuthSecret())` at boot — the desktop in
+ `src/main/index.ts`, the Server Edition in `src/server/node-identity-arm.ts` — so **managed records
+ sign on a headless host too** (raw `0600` `node-auth-key.bin`, no keychain). This is the both-shells
+ half of Decision 1; a keychain-only secret would regress Server Edition. With **no** secret armed, a
+ record write **throws** rather than writing an unsigned record (fail-closed) — nothing is minted.
+- **The switch is owner-authorized, main-side.** Moving a running node's conversation to another
+ account is a three-phase, TTL-bounded protocol keyed off `event.sender.id` — only the WebContents
+ that reserved the switch may commit or finish it. It resumes the **same conversation id** (an atomic
+ hardlink of the rollout inode), never a fork. The phone **never originates** an add/switch/copy.
+- **Ambiguity fails closed, like the latch.** The same thread id owned by two account scopes resolves
+ to **no owner** unless `owners.size === 1` — the same house rule as the pane-ownership and
+ invented-`kid` refusals: an owner that cannot be *proven* is denied, never picked by order.
+- **Ids are path segments, still.** A managed account id is a directory scope
+ (`<codexThreadIdentityRoot>/<accountId>/<threadId>`), so it passes `isSafeAccountId` (must start
+ alphanumeric — blocks `.`/`..`/leading-separator) before it becomes a path, exactly as `isSafeNodeId`
+ / `isSafeThreadId` do. The account id travels in the launcher's POST **body**, never on an argv
+ (invariant 1 — no credential *or* scope-shaping input on a command line).
+
+Two S6 surfaces remain **owed device verifications** rather than shipped-and-verified: the imperative
+pane-recycle glue behind a live switch, and the remote-host account **lifecycle** UI (add/login/remove
+*on* an SSH host — the local picker + switch are wired, the remote lifecycle surface is display-only,
+fail-closed). Both, plus the live-daemon and real-WAN legs, are in
+[the acceptance gate](codex-accounts-acceptance.md).
+
## Invariants a future change must not break
1. **No credential on any argv or command line — local or SSH.** Not tmux `-e`, not `curl -H`, not a
diff --git a/docs/shared-codex-node-identity.md b/docs/shared-codex-node-identity.md
index a793da318..954f21d10 100644
--- a/docs/shared-codex-node-identity.md
+++ b/docs/shared-codex-node-identity.md
@@ -6,10 +6,52 @@ The node↔thread mapping is what survives resumes, in-pane restarts, and app re
managed logins, and a thread's ownership record must name **which account** it belongs to, not just
which node — so one account can never be made to speak for another's threads.
-This document covers the ownership spine shipped in S6 PR 2. The account **model** and on-disk home
-layout are PR 1 (`codex-accounts-core.ts`); the spawn wiring, relay, and switch/transfer protocol
-are later PRs. This slice **ships inert** — nothing sets `NODETERM_CODEX_ACCOUNT_ID` on a pane yet —
-but it is fully tested against real primitives (real `/bin/sh`, real fs, real HMAC).
+This document covers the whole S6 feature — the account × machine model, the on-disk layout, the
+switch and cross-machine protocols, the fail-closed properties, and the same-filesystem (U3) /
+discovery (U1) constraints the probes settled. S6 shipped across nine PRs (the account model, the
+identity proxy documented here, the rollout primitive, the relay daemon, local accounts + the switch,
+SSH remote accounts, per-account usage, the Settings UI, and the per-node account picker); this
+feature is **fully operable** — a Codex node carries an account scope, the picker stamps it, and the
+switch/transfer verbs run. Every layer is tested against real primitives (real `/bin/sh`, real fs,
+real HMAC, the real main-side switch handler); the composition is walked once end-to-end in
+`src/main/codex-accounts-e2e.test.ts` (Task 9.2), and the human/device verifications that headless CI
+cannot run are enumerated in [the acceptance gate](codex-accounts-acceptance.md).
+
+Based on @Corvin's S6 design in external PR #112.
+
+## The account × machine model
+
+An account is **system** or **managed**, and it lives on **one machine**:
+
+- **System account** — the login at `$CODEX_HOME` (or `~/.codex`). No id. Always resolves. A machine
+ with no managed accounts behaves exactly as it did before S6 (Constraint 12).
+- **Managed account** — a second (or third…) logged-in Codex identity, addressed by a validated id.
+ Its home is private (`0700`) and isolated; only NON-secret installation assets (`config.toml`,
+ `AGENTS.md`, `skills`, …) are symlinked in from the system home — never `auth.json`, never the
+ thread DB, so a managed account acts only as **its own** login.
+- **Remote account** — the same, but its home lives on an SSH host (`host` set). The desktop drives
+ its lifecycle over SSH; the host runs the relay + import, not its own account-management IPC.
+
+`id` empty/undefined at any call site means the system account. Ids arrive from hand-editable,
+git-shared `settings.json` / `project.json`, so every id passes `isSafeAccountId`
+(`^[A-Za-z0-9][A-Za-z0-9._-]*$`, must start alphanumeric) **before** it becomes a path component.
+
+## On-disk home layout
+
+| What | Where |
+| --- | --- |
+| System home | `$CODEX_HOME` or `~/.codex` |
+| Managed home (local) | `~/.nodeterm/cx/<sha256(userDataDir ␀ accountId)[0..16]>`, mode `0700` |
+| Managed home (remote) | `<remoteHome>/.nodeterm/cx/<sha256(accountId)[0..16]>` |
+| App-server control socket | `<home>/app-server-control/app-server-control.sock` |
+| Ownership records | `<userDataDir>/codex-thread-nodes/` (system: bare root; managed: `<accountId>/`) |
+
+The digest is deliberately **short**: the app-server control Unix socket lives two levels below the
+home and must stay under macOS `SUN_LEN`, which an Electron userData path plus a UUID already
+overshoots. `userDataDir` is folded into the LOCAL digest so separate NodeTerm profiles never
+collide; a remote host has one home root, so the remote digest is over `accountId` only. A
+pre-migration long home (`<userData>/codex-accounts/<id>`) is moved to its short home at boot,
+fail-closed (no-op if absent, if the target exists, or on an invalid id).
## The signing secret (both shells — Decision 1)
@@ -100,3 +142,90 @@ directory scope, so `.`/`..`/leading-separator/`a/b`/absolute ids are refused at
launcher threads the account id through the `/codex-thread/{start,bind}` POST **body**, never on
argv (Constraint 6); a bad id falls back to plain `codex` rather than binding under a hostile scope,
and the server refuses it at `400` before the start handler can create an orphan thread.
+
+## Spawn scope resolution (fail-closed)
+
+`resolveCodexSessionScope(userDataDir, accountId, homeExists)` decides the `CODEX_HOME` +
+`NODETERM_CODEX_ACCOUNT_ID` a Codex spawn runs under:
+
+- **No id** → the system home. Always resolves, and it is written **explicitly** so it overwrites any
+ managed scope inherited from a parent (tmux shares one server env) rather than silently acting as
+ the wrong login.
+- **An explicitly selected managed account whose home is missing** → **refuses**
+ (`{ unavailable: 'codex-account' }`). It never falls back to the system home. This is deliberately
+ **stricter** than the Claude sibling's first-spawn fallback: silently acting as the wrong login is a
+ worse failure for an explicit switch than a refused spawn. The `pty-manager` maps `unavailable`
+ straight through to the renderer.
+
+The OAuth-shadowing env vars (`OPENAI_API_KEY`, `CODEX_API_KEY` — `AUTH_ENV_STRIP`) are removed from
+the session env at spawn so a managed account always acts as its own `auth.json` login. That strip
+touches only the **env**; no credential is ever placed on an argv (Constraint 6).
+
+## The same-machine switch (three-phase, owner-authorized)
+
+Moving a running node's conversation to another account **resumes the same conversation id, never
+forks it**. The switch is a three-phase, TTL-bounded, owner-authorized protocol driven by the
+renderer (`src/main/codex-accounts.ts`):
+
+1. **plan** (`switch-thread`) — reserve both account ids (so a concurrent removal is blocked —
+ Property 10), read the source rollout path off its app-server, and `planCodexRolloutExposure`
+ (validate only, mutate nothing). Returns a `rollbackToken`.
+2. **commit** (`commit-switch`) — `commitCodexRolloutExposure`: **atomically hardlink** the source
+ rollout inode into the target account's `sessions/<same relative path>`. Same inode ⇒
+ byte-identical conversation id. Only the **owning WebContents** may commit.
+3. **finish** (`finish-switch`) — release the reservation. Refuses until commit actually ran, so a
+ premature finish can never make the renderer recycle a node onto a target that has no rollout.
+
+`rollback-switch`, the reservation TTL, and the owner being `destroyed` all release a reservation
+without committing. The exposure is an **atomic, never-overwrite hardlink**: `link(2)` is
+no-overwrite, an `EEXIST` collision is tolerated **only** when the existing target is already the same
+inode (an idempotent re-expose), and the copy refuses to write **through** a symlinked directory
+segment. The source `dev`/`ino` is re-verified before and after the link, so a mid-flight source swap
+fails closed.
+
+## The cross-machine transfer (local → SSH)
+
+`transfer-thread-to-ssh` moves an **idle** local conversation to a remote account. The SOURCE leg
+reuses `planCodexRolloutExposure`'s guards (regular file under `<home>/sessions/`, basename
+`<threadId>.jsonl`, no symlinked segment) and then hands the upload + atomic remote install to the
+SSH importer. Properties: the conversation id survives; the far side must **discover** the hardlinked
+rollout before the node is recycled (verify-then-recycle — a false discovery is a **refused copy +
+rollback**, never a silent one); the **local copy remains** (it is a hardlink, never moved); and an
+existing remote rollout is **never overwritten** (`link` / `mv` fail closed on `EEXIST` / `exit 17`).
+An absent importer (no live SSH manager) **fails closed** with a named error, never a silent success.
+
+## Per-account usage (no mixing)
+
+Usage is one **system row** plus one row **per managed account**, each keyed by its own `accountId`
+and built independently — there is no reduce/merge step, so one account's numbers can never be
+attributed to another. Every `fetchCodexUsage` return path (including `unavailable`) is stamped with
+the account's identity, so an empty or failed read fails closed to **that** account, never to a
+fabricated one; the system row stays explicitly un-owned (`accountId: undefined`). A throwing account
+source yields **system-only**, never a made-up account. A cache fingerprint over the account set
+busts stale numbers when an account is added, removed, or relabelled.
+
+## Fail-closed properties (the house rules)
+
+| Situation | Verdict |
+| --- | --- |
+| Explicitly selected account, home missing | Refuse the spawn — no system fallback |
+| Same thread id owned by two account scopes, no hint | No owner (proxy, sh resolver, relay catalog all fail closed) |
+| Rollout collision at target, different inode | Never overwrite (throws / `exit 17`) |
+| Cross-mount rollout link | Named `EXDEV` error — no silent byte-copy fallback (U3) |
+| Far side cannot discover the imported rollout | Roll back the import |
+| Record signed for account A, filed under B | MAC fails — rejected |
+| No signing secret armed | Record write throws; nothing unsigned is written |
+| Usage source throws | System-only; never a fabricated account |
+| Account id from `settings.json` that could escape the root | Refused at the door by `isSafeAccountId` |
+
+## U1 / U3 — the two constraints the probes settled
+
+- **U3 — same filesystem.** A same-machine switch links a rollout inode between two LOCAL homes, both
+ under `$HOME` (`~/.codex` and `~/.nodeterm/cx`), measured same-device on the build host (ext4).
+ `link(2)` throws `EXDEV` across mounts, and a `$HOME` subtree **can** be bind-mounted onto another
+ volume, so v1 does **not** silently fall back to a byte copy — `commit` surfaces a named `EXDEV`
+ error. A cross-mount copy fallback is explicitly out of scope for S6 v1.
+- **U1 — live discovery.** The cross-machine copy rests on a running `app-server` discovering a
+ hardlinked rollout **without a reindex**. Mitigated in code by verify-then-recycle (a false U1 is a
+ refused copy + rollback, never silent), but the live-daemon confirmation is a **device
+ verification**, owed on a real host — see the acceptance gate.