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.

feat(codex): account-scoped, fail-closed ownership records in the identity proxy

Claude Code1740c343e8d6
diff --git a/src/core/codex-identity-proxy.test.ts b/src/core/codex-identity-proxy.test.ts
index b653b2fb4..3ab82b97d 100644
--- a/src/core/codex-identity-proxy.test.ts
+++ b/src/core/codex-identity-proxy.test.ts
@@ -4,18 +4,24 @@ import os from 'node:os'
import path from 'node:path'
import { randomBytes } from 'node:crypto'
import {
+ accountScope,
bindCodexThreadIdentity,
codexLauncherDir,
+ codexThreadIdentityHasLiveConflict,
forgetCodexThreadIdentitiesForNode,
codexThreadIdentityRoot,
+ identityCandidates,
installCodexLauncher,
isSafeThreadId,
readCodexThreadIdentity,
+ readIdentityCandidate,
resetCodexThreadIdentityAuthSecret,
resolveCodexThreadNodeIdentity,
setCodexThreadIdentityAuthSecret,
+ SYSTEM_ACCOUNT_SCOPE,
writeCodexThreadIdentity
} from './codex-identity-proxy'
+import { createHmac } from 'node:crypto'
import { initPlatform, resetPlatformForTests } from './platform'
import { fakePlatform } from './platform-fake'
@@ -201,4 +207,158 @@ describe('forgetting a permanently deleted node', () => {
fs.rmSync(recordsRoot, { recursive: true, force: true })
expect(() => forgetCodexThreadIdentitiesForNode('node-1', recordsRoot)).not.toThrow()
})
+
+ it('forgets a node across managed account scopes, not just the bare system root', () => {
+ writeCodexThreadIdentity('thread-1', 'node-1', '/data/e', recordsRoot) // system
+ writeCodexThreadIdentity('thread-2', 'node-1', '/data/e', recordsRoot, 'acct-A')
+ writeCodexThreadIdentity('thread-3', 'node-2', '/data/e', recordsRoot, 'acct-A')
+ forgetCodexThreadIdentitiesForNode('node-1', recordsRoot)
+ expect(resolveCodexThreadNodeIdentity('thread-1', recordsRoot)).toBeUndefined()
+ expect(readCodexThreadIdentity('thread-2', recordsRoot, 'acct-A')).toBeUndefined()
+ // Another node's managed record in the same scope is left alone.
+ expect(readCodexThreadIdentity('thread-3', recordsRoot, 'acct-A')?.nodeId).toBe('node-2')
+ })
+})
+
+// ── S6 PR 2: the account-scoped ownership spine (Properties 3, 7, 8) ──────────────────────────
+//
+// A FIXED secret is used here rather than the per-test random one, so the tests can forge both a
+// legacy 3-tuple record (Constraint 12 back-compat) and a hand-signed managed record.
+const FIXED_SECRET = Buffer.alloc(32, 7)
+const b64url = (b: Buffer): string => b.toString('base64url')
+const sign4 = (threadId: string, scope: string, nodeId: string, endpoint: string): string =>
+ b64url(createHmac('sha256', FIXED_SECRET).update(`${threadId}\0${scope}\0${nodeId}\0${endpoint}`).digest())
+const sign3 = (threadId: string, nodeId: string, endpoint: string): string =>
+ b64url(createHmac('sha256', FIXED_SECRET).update(`${threadId}\0${nodeId}\0${endpoint}`).digest())
+
+describe('account-scoped ownership', () => {
+ beforeEach(() => setCodexThreadIdentityAuthSecret(FIXED_SECRET))
+
+ it('accountScope normalises the system account and refuses an id that escapes the directory', () => {
+ // Property 8: the account id is a directory scope, so `..`/`a/b`/absolute/`system` are refused.
+ expect(accountScope(undefined)).toBe(SYSTEM_ACCOUNT_SCOPE)
+ expect(accountScope('')).toBe(SYSTEM_ACCOUNT_SCOPE)
+ expect(accountScope('acct-A')).toBe('acct-A')
+ for (const bad of ['..', '../x', 'a/b', '/abs', '.', 'system', ' sp']) {
+ expect(() => accountScope(bad), bad).toThrow()
+ }
+ })
+
+ it('refuses to write a record under an account scope that could escape the mapping directory', () => {
+ // Property 8: `..` as a scope resolves to the record dir's PARENT. Refused, and nothing written.
+ expect(() => writeCodexThreadIdentity('thread-1', 'node-1', '/data/e', recordsRoot, '..')).toThrow()
+ expect(() => writeCodexThreadIdentity('thread-1', 'node-1', '/data/e', recordsRoot, 'a/b')).toThrow()
+ expect(fs.existsSync(recordsRoot)).toBe(false)
+ expect(fs.readdirSync(dir)).toEqual([])
+ })
+
+ it('round-trips a managed record under its scope directory, isolated from the system record', () => {
+ writeCodexThreadIdentity('thread-1', 'node-sys', '/data/e', recordsRoot) // system, bare root
+ writeCodexThreadIdentity('thread-1', 'node-mgd', '/data/e', recordsRoot, 'acct-A')
+ expect(fs.existsSync(path.join(recordsRoot, 'thread-1'))).toBe(true)
+ expect(fs.existsSync(path.join(recordsRoot, 'acct-A', 'thread-1'))).toBe(true)
+ expect(readCodexThreadIdentity('thread-1', recordsRoot)?.nodeId).toBe('node-sys')
+ expect(readCodexThreadIdentity('thread-1', recordsRoot, 'acct-A')?.nodeId).toBe('node-mgd')
+ })
+
+ it('fails closed for the same thread id owned by different accounts (Property 3)', () => {
+ // Two accounts each own thread-1, naming DIFFERENT nodes. An unscoped resolve (the shared tool
+ // shell that only knows the bare thread id) must return NOTHING — ambiguity is refusal.
+ writeCodexThreadIdentity('thread-1', 'node-A', '/data/e', recordsRoot, 'acct-A')
+ writeCodexThreadIdentity('thread-1', 'node-B', '/data/e', recordsRoot, 'acct-B')
+ expect(identityCandidates('thread-1', recordsRoot)).toHaveLength(2)
+ // MUTATION TARGET: return the first owner instead of requiring owners.size === 1 ⇒ this reddens.
+ expect(resolveCodexThreadNodeIdentity('thread-1', recordsRoot)).toBeUndefined()
+ // But a resolve SCOPED to one account still answers unambiguously.
+ expect(resolveCodexThreadNodeIdentity('thread-1', recordsRoot, 'acct-A')).toBe('node-A')
+ expect(resolveCodexThreadNodeIdentity('thread-1', recordsRoot, 'acct-B')).toBe('node-B')
+ })
+
+ it('resolves the single owner when the same thread id names the SAME node in two scopes', () => {
+ // Same owner in two places is not a conflict — owners.size === 1.
+ writeCodexThreadIdentity('thread-1', 'node-1', '/data/e', recordsRoot)
+ writeCodexThreadIdentity('thread-1', 'node-1', '/data/e', recordsRoot, 'acct-A')
+ expect(resolveCodexThreadNodeIdentity('thread-1', recordsRoot)).toBe('node-1')
+ })
+
+ it('reports a live conflict only when two DIFFERENT live nodes own the thread', () => {
+ writeCodexThreadIdentity('thread-1', 'node-A', '/data/e', recordsRoot, 'acct-A')
+ writeCodexThreadIdentity('thread-1', 'node-B', '/data/e', recordsRoot, 'acct-B')
+ expect(codexThreadIdentityHasLiveConflict('thread-1', live(['node-A', 'node-B']), recordsRoot)).toBe(true)
+ // Only one of them live ⇒ no conflict; the other is a stale record free to be reclaimed.
+ expect(codexThreadIdentityHasLiveConflict('thread-1', live(['node-A']), recordsRoot)).toBe(false)
+ })
+
+ it('rejects a managed record whose owner was edited without the signing key (Property 7)', () => {
+ writeCodexThreadIdentity('thread-1', 'node-1', '/data/e', recordsRoot, 'acct-A')
+ const file = path.join(recordsRoot, 'acct-A', 'thread-1')
+ fs.writeFileSync(file, fs.readFileSync(file, 'utf8').replace('node-1', 'node-evil'))
+ // MUTATION TARGET: skip recordSignatureValid ⇒ this returns node-evil and reddens.
+ expect(readCodexThreadIdentity('thread-1', recordsRoot, 'acct-A')).toBeUndefined()
+ })
+
+ it('rejects a record whose accountId line disagrees with its directory scope (Property 7)', () => {
+ // A perfectly valid record for acct-A, physically moved into acct-B's directory. Its account
+ // line still says acct-A, so it must not be honoured as acct-B's — nor re-verify under acct-B.
+ writeCodexThreadIdentity('thread-1', 'node-1', '/data/e', recordsRoot, 'acct-A')
+ fs.mkdirSync(path.join(recordsRoot, 'acct-B'), { recursive: true })
+ fs.copyFileSync(
+ path.join(recordsRoot, 'acct-A', 'thread-1'),
+ path.join(recordsRoot, 'acct-B', 'thread-1')
+ )
+ expect(readCodexThreadIdentity('thread-1', recordsRoot, 'acct-A')?.nodeId).toBe('node-1')
+ // MUTATION TARGET: drop the line-vs-directory agreement check ⇒ acct-B honours it and reddens.
+ expect(readCodexThreadIdentity('thread-1', recordsRoot, 'acct-B')).toBeUndefined()
+ })
+
+ it('hand-forged managed record with a correct 4-tuple signature is honoured', () => {
+ // Proves the 4-tuple preimage is exactly (threadId ␀ scope ␀ nodeId ␀ endpoint) — the mutation
+ // check for the ambiguity/HMAC tests rests on being able to mint a genuinely valid record.
+ fs.mkdirSync(path.join(recordsRoot, 'acct-A'), { recursive: true })
+ fs.writeFileSync(
+ path.join(recordsRoot, 'acct-A', 'thread-9'),
+ `accountId=acct-A\nnodeId=node-9\nendpoint=/data/e\nsignature=${sign4('thread-9', 'acct-A', 'node-9', '/data/e')}\n`
+ )
+ expect(readIdentityCandidate('thread-9', 'acct-A', recordsRoot)?.nodeId).toBe('node-9')
+ // The same bytes with a scope-mismatched signature (signed as system) do NOT verify at acct-A.
+ fs.writeFileSync(
+ path.join(recordsRoot, 'acct-A', 'thread-9'),
+ `accountId=acct-A\nnodeId=node-9\nendpoint=/data/e\nsignature=${sign4('thread-9', 'system', 'node-9', '/data/e')}\n`
+ )
+ expect(readIdentityCandidate('thread-9', 'acct-A', recordsRoot)).toBeUndefined()
+ })
+
+ it('keeps resolving a legacy 3-tuple system record with no accountId line (Constraint 12)', () => {
+ // A record written by pre-S6 S4 code: bare root, no accountId line, 3-tuple signature. A
+ // system-only user must keep resolving after the account scoping lands.
+ fs.mkdirSync(recordsRoot, { recursive: true })
+ fs.writeFileSync(
+ path.join(recordsRoot, 'legacy-thread'),
+ `nodeId=node-legacy\nendpoint=/data/e\nsignature=${sign3('legacy-thread', 'node-legacy', '/data/e')}\n`
+ )
+ expect(readCodexThreadIdentity('legacy-thread', recordsRoot)?.nodeId).toBe('node-legacy')
+ expect(resolveCodexThreadNodeIdentity('legacy-thread', recordsRoot)).toBe('node-legacy')
+ })
+
+ it('never accepts the legacy 3-tuple fallback for a managed scope', () => {
+ // The back-compat door is system-only: a 3-tuple signature under a managed subdir is refused,
+ // so the account dimension cannot be stripped by omitting the account line in acct-A's dir.
+ fs.mkdirSync(path.join(recordsRoot, 'acct-A'), { recursive: true })
+ fs.writeFileSync(
+ path.join(recordsRoot, 'acct-A', 'thread-1'),
+ `nodeId=node-1\nendpoint=/data/e\nsignature=${sign3('thread-1', 'node-1', '/data/e')}\n`
+ )
+ expect(readIdentityCandidate('thread-1', 'acct-A', recordsRoot)).toBeUndefined()
+ })
+
+ it('bind refuses to steal a managed thread from a live node in the SAME scope', () => {
+ writeCodexThreadIdentity('thread-1', 'node-1', '/data/e', recordsRoot, 'acct-A')
+ expect(() =>
+ bindCodexThreadIdentity('thread-1', 'node-2', '/data/e', live(['node-1']), recordsRoot, 'acct-A')
+ ).toThrow()
+ expect(resolveCodexThreadNodeIdentity('thread-1', recordsRoot, 'acct-A')).toBe('node-1')
+ // A different account's identically-named thread is a different owner, freely bindable.
+ bindCodexThreadIdentity('thread-1', 'node-2', '/data/e', live(['node-1']), recordsRoot, 'acct-B')
+ expect(resolveCodexThreadNodeIdentity('thread-1', recordsRoot, 'acct-B')).toBe('node-2')
+ })
})
diff --git a/src/core/codex-identity-proxy.ts b/src/core/codex-identity-proxy.ts
index ce0c955bd..1e4918a9a 100644
--- a/src/core/codex-identity-proxy.ts
+++ b/src/core/codex-identity-proxy.ts
@@ -18,9 +18,15 @@
* (`src/core/agents/node-auth-secret.ts` — sealed via safeStorage on the desktop, raw 0600 bytes on
* the Server Edition). Unsigned/mis-signed records are ignored, not repaired.
*
- * ACCOUNT SCOPING is deliberately absent here: managed Codex accounts are a later slice. The
- * storage shape is one file per thread id; scoping adds a directory level above it without
- * changing anything this file promises.
+ * ACCOUNT SCOPING (S6): managed Codex accounts add a directory level above the thread id. A
+ * SYSTEM record still lives at the bare root (`<root>/<threadId>`), so a machine with no managed
+ * accounts keeps the exact S4 layout and its legacy records keep resolving (Constraint 12). A
+ * MANAGED record lives under `<root>/<accountId>/<threadId>`. The HMAC now binds the full 4-tuple
+ * (threadId, accountScope, nodeId, hookEndpoint) — an empty account id is normalised to the scope
+ * string `system`, and a record whose `accountId=` line disagrees with the directory it sits in is
+ * rejected, so an account can never be edited to speak for another's threads. Records written
+ * before this slice carry no `accountId=` line and are verified with the original 3-tuple preimage
+ * at the system scope only — the one back-compat door, and it is a system-scope door.
*/
import {
chmodSync,
@@ -34,6 +40,35 @@ import {
import path from 'path'
import { createHmac, timingSafeEqual } from 'crypto'
import { platform } from './platform'
+import { ACCOUNT_ID_RE, isSafeAccountId } from '../shared/codex-account'
+
+/**
+ * The scope string for the SYSTEM account (`~/.codex`, no managed id). Empty/undefined account ids
+ * normalise to this both in the HMAC preimage and — for the directory — to the bare record root.
+ */
+export const SYSTEM_ACCOUNT_SCOPE = 'system'
+
+/**
+ * The one shape of a safe managed account id, re-exported from the shared seam so there is a single
+ * definition of the alphabet the path builders and this proxy both trust. Must start alphanumeric,
+ * which blocks `.`/`..`/leading-separator ids at the door before an id becomes a directory scope.
+ */
+export const SAFE_ACCOUNT_ID = ACCOUNT_ID_RE
+
+/**
+ * The scope string for an account id: `system` for empty/undefined, else the validated id itself.
+ * THROWS on an id that could escape the mapping directory (`..`, `a/b`, `/abs`, whitespace) — the
+ * supply-chain guard, since account ids arrive from hand-editable `settings.json` / `project.json`.
+ * A literal id of `system` is refused so the reserved bare-root scope can never be impersonated by
+ * a managed subdirectory.
+ */
+export function accountScope(accountId?: string): string {
+ if (!accountId) return SYSTEM_ACCOUNT_SCOPE
+ if (accountId === SYSTEM_ACCOUNT_SCOPE || !isSafeAccountId(accountId)) {
+ throw new Error('Invalid NodeTerm Codex account scope')
+ }
+ return accountId
+}
/**
* How long the SERVER gives itself to mint a thread (`startCodexThreadAt`'s default).
@@ -103,105 +138,270 @@ export function codexThreadIdentityRoot(): string {
return path.join(platform().userDataDir, 'codex-thread-nodes')
}
-function identitySignature(threadId: string, nodeId: string, hookEndpoint: string): string {
+/**
+ * The current 4-tuple preimage: HMAC-SHA256(threadId ␀ accountScope ␀ nodeId ␀ hookEndpoint). The
+ * account scope binds the record to ONE account; without it a record for account A could be moved,
+ * byte-for-byte, into account B's directory and still verify.
+ */
+function identitySignature(
+ threadId: string,
+ scope: string,
+ nodeId: string,
+ hookEndpoint: string
+): string {
+ if (!identityAuthSecret) throw new Error('NodeTerm Codex identity authentication is unavailable')
+ return createHmac('sha256', identityAuthSecret)
+ .update(`${threadId}\0${scope}\0${nodeId}\0${hookEndpoint}`)
+ .digest('base64url')
+}
+
+/**
+ * The pre-S6 3-tuple preimage (no account dimension). Accepted ONLY for a system-scope record that
+ * carries no `accountId=` line — i.e. one written before this slice. Every record this slice writes
+ * (system included) carries the account line and the 4-tuple, so the legacy door is system-only and
+ * closes itself the first time a machine rewrites the record.
+ */
+function legacyIdentitySignature(threadId: string, nodeId: string, hookEndpoint: string): string {
if (!identityAuthSecret) throw new Error('NodeTerm Codex identity authentication is unavailable')
return createHmac('sha256', identityAuthSecret)
.update(`${threadId}\0${nodeId}\0${hookEndpoint}`)
.digest('base64url')
}
-function signatureMatches(threadId: string, identity: CodexThreadIdentity): boolean {
- if (!identity.signature || !identityAuthSecret) return false
+function signatureEquals(presented: string, expected: string): boolean {
+ const a = Buffer.from(presented)
+ const b = Buffer.from(expected)
+ return a.length === b.length && timingSafeEqual(a, b)
+}
+
+/**
+ * Verify a parsed record sitting in the `dirScope` directory (Property 7). Two checks, in order:
+ * 1. the record's own `accountId=` line must AGREE with the directory it was found in, and
+ * 2. the HMAC must match — the 4-tuple for any record with an account line, with a system-only
+ * 3-tuple fallback for a legacy record that has no line at all.
+ *
+ * Check (1) (line-vs-directory) is defence in depth, kept legible rather than removed. What carries
+ * Property 7 is the SCOPE-bound HMAC in check (2): a record signed for `acct-A` verifies only with
+ * the preimage scope `acct-A`, so re-filing it under `acct-B` fails the MAC regardless of the line
+ * check. The legacy 3-tuple fallback is the ONE back-compat door and is restricted to the SYSTEM
+ * scope, so a scope-less signature can never be honoured under a managed account. (The mutations
+ * that redden these tests are the HMAC one and the `scope === SYSTEM_ACCOUNT_SCOPE` fallback guard;
+ * check (1) is redundant with the scope-bound HMAC and is documented as such.)
+ */
+function recordSignatureValid(threadId: string, dirScope: string, record: ParsedRecord): boolean {
+ if (!record.signature || !identityAuthSecret) return false
+ const scope = dirScope || SYSTEM_ACCOUNT_SCOPE
+ if (record.accountLinePresent) {
+ const lineScope = record.accountId || SYSTEM_ACCOUNT_SCOPE
+ if (lineScope !== scope) return false
+ }
let expected = ''
try {
- expected = identitySignature(threadId, identity.nodeId, identity.hookEndpoint)
+ expected = identitySignature(threadId, scope, record.nodeId, record.hookEndpoint)
} catch {
return false
}
- const actual = Buffer.from(identity.signature)
- const wanted = Buffer.from(expected)
- return actual.length === wanted.length && timingSafeEqual(actual, wanted)
+ if (signatureEquals(record.signature, expected)) return true
+ if (!record.accountLinePresent && scope === SYSTEM_ACCOUNT_SCOPE) {
+ try {
+ return signatureEquals(
+ record.signature,
+ legacyIdentitySignature(threadId, record.nodeId, record.hookEndpoint)
+ )
+ } catch {
+ return false
+ }
+ }
+ return false
}
export interface CodexThreadIdentity {
+ /** '' for the system account; a managed account id otherwise. */
+ accountId: string
nodeId: string
hookEndpoint: string
signature: string
}
+interface ParsedRecord extends CodexThreadIdentity {
+ accountLinePresent: boolean
+}
+
export function validCodexIdentity(nodeId: string, hookEndpoint: string): boolean {
return SAFE_NODE_ID.test(nodeId) && SAFE_ENDPOINT.test(hookEndpoint)
}
-function identityFile(threadId: string, root = codexThreadIdentityRoot()): string {
+/** The absolute path of a record for a thread under a scope (`system` ⇒ bare root). */
+function recordFilePath(root: string, scope: string, threadId: string): string {
+ return scope && scope !== SYSTEM_ACCOUNT_SCOPE
+ ? path.join(root, scope, threadId)
+ : path.join(root, threadId)
+}
+
+function identityFile(threadId: string, scope: string, root = codexThreadIdentityRoot()): string {
if (!isSafeThreadId(threadId)) throw new Error('Invalid NodeTerm Codex thread identity')
- return path.join(root, threadId)
+ return recordFilePath(root, scope, threadId)
}
-function parseCodexThreadIdentity(raw: string): CodexThreadIdentity {
+function parseCodexThreadIdentity(raw: string): ParsedRecord {
const values: Record<string, string> = {}
for (const line of raw.split('\n')) {
const separator = line.indexOf('=')
if (separator < 1) continue
- values[line.slice(0, separator)] = line.slice(separator + 1)
+ const key = line.slice(0, separator)
+ // First occurrence wins, matching the `head -n 1` the sh prelude uses: a second `accountId=`
+ // line cannot be smuggled in to disagree with the first.
+ if (!(key in values)) values[key] = line.slice(separator + 1)
}
return {
+ accountId: values.accountId ?? '',
nodeId: values.nodeId ?? '',
hookEndpoint: values.endpoint ?? '',
- signature: values.signature ?? ''
+ signature: values.signature ?? '',
+ accountLinePresent: 'accountId' in values
}
}
-/** The record for a thread, or undefined when it is absent, malformed or badly signed. */
-export function readCodexThreadIdentity(
+/**
+ * Read and verify the record for a thread AT A SPECIFIC SCOPE (`system` = bare root). Returns
+ * undefined when it is absent, malformed, badly signed, or its account line disagrees with the
+ * directory. The scope itself is validated so a hostile scan entry can never escape the root.
+ */
+export function readIdentityCandidate(
threadId: string,
+ scope: string,
root = codexThreadIdentityRoot()
): CodexThreadIdentity | undefined {
if (!isSafeThreadId(threadId)) return undefined
+ const norm = scope || SYSTEM_ACCOUNT_SCOPE
+ if (norm !== SYSTEM_ACCOUNT_SCOPE && !isSafeAccountId(norm)) return undefined
let raw: string
try {
- raw = readFileSync(path.join(root, threadId), 'utf8')
+ raw = readFileSync(recordFilePath(root, norm, threadId), 'utf8')
+ } catch {
+ return undefined
+ }
+ const record = parseCodexThreadIdentity(raw)
+ if (!validCodexIdentity(record.nodeId, record.hookEndpoint)) return undefined
+ if (!recordSignatureValid(threadId, norm, record)) return undefined
+ return {
+ accountId: record.accountId,
+ nodeId: record.nodeId,
+ hookEndpoint: record.hookEndpoint,
+ signature: record.signature
+ }
+}
+
+/**
+ * Every verified record for a thread, across every scope present on disk: the bare-root system
+ * record plus one per managed-account subdirectory. Unsafe scope names are skipped, never read.
+ */
+export function identityCandidates(
+ threadId: string,
+ root = codexThreadIdentityRoot()
+): Array<{ scope: string; identity: CodexThreadIdentity }> {
+ const out: Array<{ scope: string; identity: CodexThreadIdentity }> = []
+ const system = readIdentityCandidate(threadId, SYSTEM_ACCOUNT_SCOPE, root)
+ if (system) out.push({ scope: SYSTEM_ACCOUNT_SCOPE, identity: system })
+ let entries: Array<{ name: string; isDirectory(): boolean }>
+ try {
+ entries = readdirSync(root, { withFileTypes: true })
+ } catch {
+ return out
+ }
+ for (const entry of entries) {
+ if (!entry.isDirectory()) continue
+ if (entry.name === SYSTEM_ACCOUNT_SCOPE || !isSafeAccountId(entry.name)) continue
+ const identity = readIdentityCandidate(threadId, entry.name, root)
+ if (identity) out.push({ scope: entry.name, identity })
+ }
+ return out
+}
+
+/** The record for a thread at a scope (`accountId` empty/undefined ⇒ system), or undefined. */
+export function readCodexThreadIdentity(
+ threadId: string,
+ root = codexThreadIdentityRoot(),
+ accountId?: string
+): CodexThreadIdentity | undefined {
+ let scope: string
+ try {
+ scope = accountScope(accountId)
} catch {
return undefined
}
- const identity = parseCodexThreadIdentity(raw)
- if (!validCodexIdentity(identity.nodeId, identity.hookEndpoint)) return undefined
- if (!signatureMatches(threadId, identity)) return undefined
- return identity
+ return readIdentityCandidate(threadId, scope, root)
}
/**
* Recover a thread's owning node after the app restarts. tmux sessions outlive the app, so a
* running Codex client can outlive every in-memory map we hold; the file is the only thing that
* still knows which node it belongs to.
+ *
+ * With `accountId` given the answer is that one scope's record. WITHOUT it — the shared-app-server
+ * tool shell that only knows a bare thread id — every scope is scanned and an owner is returned
+ * ONLY when it is unambiguous (`owners.size === 1`). The same thread id owned by two accounts is
+ * NOT resolved: ambiguous ownership fails closed (Property 3), the same posture as an unproven pane
+ * claim in `pane-ownership.ts`.
*/
export function resolveCodexThreadNodeIdentity(
threadId: string,
- root = codexThreadIdentityRoot()
+ root = codexThreadIdentityRoot(),
+ accountId?: string
): string | undefined {
- return readCodexThreadIdentity(threadId, root)?.nodeId
+ if (accountId !== undefined) {
+ return readCodexThreadIdentity(threadId, root, accountId)?.nodeId
+ }
+ const candidates = identityCandidates(threadId, root)
+ const owners = new Set(candidates.map((c) => c.identity.nodeId))
+ return owners.size === 1 ? candidates[0].identity.nodeId : undefined
+}
+
+/**
+ * Whether a thread id is owned by MORE THAN ONE currently-live node across scopes — a genuine
+ * ownership conflict the caller must refuse rather than pick a winner for. A single live owner (or
+ * none) is not a conflict. Fail-closed twin of the single-live-owner rule in `bindCodexThreadIdentity`.
+ */
+export function codexThreadIdentityHasLiveConflict(
+ threadId: string,
+ isNodeLive: (nodeId: string) => boolean,
+ root = codexThreadIdentityRoot()
+): boolean {
+ const liveOwners = new Set(
+ identityCandidates(threadId, root)
+ .map((c) => c.identity.nodeId)
+ .filter(isNodeLive)
+ )
+ return liveOwners.size > 1
}
-/** Write (or replace) the record for `threadId`, atomically. */
+/** Write (or replace) the record for `threadId` under its account scope, atomically. */
export function writeCodexThreadIdentity(
threadId: string,
nodeId: string,
hookEndpoint: string,
- root = codexThreadIdentityRoot()
+ root = codexThreadIdentityRoot(),
+ accountId?: string
): void {
if (!isSafeThreadId(threadId) || !validCodexIdentity(nodeId, hookEndpoint)) {
throw new Error('Invalid NodeTerm Codex thread identity')
}
- const signature = identitySignature(threadId, nodeId, hookEndpoint)
- const file = identityFile(threadId, root)
- const tmp = path.join(root, `.${threadId}.${process.pid}.${Date.now()}`)
- mkdirSync(root, { recursive: true, mode: 0o700 })
+ const scope = accountScope(accountId) // throws on an id that could escape the mapping directory
+ const signature = identitySignature(threadId, scope, nodeId, hookEndpoint)
+ const file = identityFile(threadId, scope, root)
+ const dir = path.dirname(file)
+ const tmp = path.join(dir, `.${threadId}.${process.pid}.${Date.now()}`)
+ mkdirSync(dir, { recursive: true, mode: 0o700 })
let renamed = false
try {
- writeFileSync(tmp, `nodeId=${nodeId}\nendpoint=${hookEndpoint}\nsignature=${signature}\n`, {
- encoding: 'utf8',
- mode: 0o600
- })
+ writeFileSync(
+ tmp,
+ `accountId=${accountId ?? ''}\nnodeId=${nodeId}\nendpoint=${hookEndpoint}\nsignature=${signature}\n`,
+ {
+ encoding: 'utf8',
+ mode: 0o600
+ }
+ )
renameSync(tmp, file)
renamed = true
} finally {
@@ -226,17 +426,19 @@ export function bindCodexThreadIdentity(
nodeId: string,
hookEndpoint: string,
isNodeLive: (nodeId: string) => boolean,
- root = codexThreadIdentityRoot()
+ root = codexThreadIdentityRoot(),
+ accountId?: string
): void {
if (!isSafeThreadId(threadId) || !validCodexIdentity(nodeId, hookEndpoint)) {
throw new Error('Invalid NodeTerm Codex thread identity')
}
- const existing = readCodexThreadIdentity(threadId, root)
+ accountScope(accountId) // reject an escaping account id before any read or write
+ const existing = readCodexThreadIdentity(threadId, root, accountId)
if (existing && existing.nodeId !== nodeId && isNodeLive(existing.nodeId)) {
throw new Error('Codex thread is already bound to another live node')
}
if (existing && existing.nodeId === nodeId && existing.hookEndpoint === hookEndpoint) return
- writeCodexThreadIdentity(threadId, nodeId, hookEndpoint, root)
+ writeCodexThreadIdentity(threadId, nodeId, hookEndpoint, root, accountId)
}
/**
@@ -255,21 +457,38 @@ export function forgetCodexThreadIdentitiesForNode(
root = codexThreadIdentityRoot()
): void {
if (!SAFE_NODE_ID.test(nodeId)) return
- let entries: string[]
+ const forgetInScope = (scope: string, dir: string): void => {
+ let entries: Array<{ name: string; isFile(): boolean }>
+ try {
+ entries = readdirSync(dir, { withFileTypes: true })
+ } catch {
+ return
+ }
+ for (const entry of entries) {
+ if (!entry.isFile()) continue // a managed-scope SUBDIRECTORY at the root is not a thread id
+ const threadId = entry.name
+ if (!isSafeThreadId(threadId)) continue
+ // Reads through the signature check, so a record we do not trust is also one we do not delete.
+ if (readIdentityCandidate(threadId, scope, root)?.nodeId !== nodeId) continue
+ try {
+ unlinkSync(path.join(dir, threadId))
+ } catch {
+ /* nothing to forget */
+ }
+ }
+ }
+ // System records live at the bare root; managed records one directory down, per account scope.
+ forgetInScope(SYSTEM_ACCOUNT_SCOPE, root)
+ let scopes: Array<{ name: string; isDirectory(): boolean }>
try {
- entries = readdirSync(root)
+ scopes = readdirSync(root, { withFileTypes: true })
} catch {
return
}
- for (const threadId of entries) {
- if (!isSafeThreadId(threadId)) continue
- // Reads through the signature check, so a record we do not trust is also one we do not delete.
- if (readCodexThreadIdentity(threadId, root)?.nodeId !== nodeId) continue
- try {
- unlinkSync(path.join(root, threadId))
- } catch {
- /* nothing to forget */
- }
+ for (const entry of scopes) {
+ if (!entry.isDirectory()) continue
+ if (entry.name === SYSTEM_ACCOUNT_SCOPE || !isSafeAccountId(entry.name)) continue
+ forgetInScope(entry.name, path.join(root, entry.name))
}
}
@@ -364,6 +583,15 @@ nt_preflight() {
if [ "\${1-}" = resume ]; then
case "\${2-}" in ''|*[!A-Za-z0-9._-]*) nt_fail thread-id-unavailable; return ;; esac
fi
+ # The account scope (S6). Empty ⇒ the system account. A non-empty id is validated to the SAME
+ # shape the record store's accountScope() enforces — must START alphanumeric (so '.'/'..'/leading
+ # separators can never become a directory scope) — BEFORE it rides a POST body onward. A bad id
+ # falls back to plain codex rather than binding a thread under an attacker-shaped scope; the id
+ # travels in the request body, never on argv (Constraint 6).
+ case "\${NODETERM_CODEX_ACCOUNT_ID-}" in
+ '') ;;
+ [!A-Za-z0-9]*|*[!A-Za-z0-9._-]*) nt_fail codex-account-invalid; return ;;
+ esac
# The authoritative check runs FIRST and unchanged; the stat below only decides WHICH reason to
# report. Ordering it the other way would let a codex that no longer needs the standalone runtime
# fall back on a stat we had no business trusting more than the command itself.
@@ -422,6 +650,7 @@ if [ "\${1-}" = resume ]; then
# Claim the caller-supplied thread for THIS node before Codex opens it. A refusal means another
# live node owns it; two clients on one thread is worse than one plain session, so we fall back.
if nt_post 20 --data-urlencode "nodeId=$NODETERM_NODE_ID" --data-urlencode "threadId=\${2-}" \\
+ --data-urlencode "accountId=\${NODETERM_CODEX_ACCOUNT_ID-}" \\
"http://localhost:\${NODETERM_HOOK_PORT-0}/codex-thread/bind" >/dev/null; then
exec codex --remote unix:// "$@"
fi
@@ -430,6 +659,7 @@ if [ "\${1-}" = resume ]; then
fi
nt_thread=$(nt_post ${CODEX_THREAD_START_CLIENT_MAX_S} --data-urlencode "nodeId=$NODETERM_NODE_ID" --data-urlencode "cwd=$PWD" \\
+ --data-urlencode "accountId=\${NODETERM_CODEX_ACCOUNT_ID-}" \\
"http://localhost:\${NODETERM_HOOK_PORT-0}/codex-thread/start") || nt_thread=''
nt_thread=$(printf %s "$nt_thread" | tr -d '\\r\\n')
case "$nt_thread" in