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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
feat(codex): fail-closed Codex account scope at the PtyManager spawn (Property 4)
Claude Codedd4762f058bd
diff --git a/src/core/pty-codex-spawn.test.ts b/src/core/pty-codex-spawn.test.tsnew file mode 100644index 000000000..a0c838a65--- /dev/null+++ b/src/core/pty-codex-spawn.test.ts@@ -0,0 +1,110 @@+/**+ * S6 §5 property 4 / Decision 2 — the carried PR-1 obligation, now at the PtyManager layer.+ *+ * `resolveCodexSessionScope` (pty-codex-account.test.ts) proves the MODEL fails closed. This proves+ * the PtyManager CONSUMES it: a LOCAL Codex create that explicitly selected a managed account whose+ * home is MISSING refuses (`unavailable: 'codex-account'`) and spawns NOTHING — it never falls+ * through to a system-`~/.codex` shell wearing the switch's identity. When the home exists, the+ * same create spawns and the session runs under that managed CODEX_HOME + account id. The system+ * account (no id, agentId codex) always spawns and pins NODETERM_CODEX_ACCOUNT_ID='' explicitly.+ *+ * MUTATION (recorded in the PR body): delete the `isCodexScopeRefusal` refusal block in+ * pty-manager `spawnNew` → the missing-account create falls through and SPAWNS, so+ * `expect(spawned).toHaveLength(0)` and `res.unavailable` both redden.+ */+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'+import { mkdirSync, mkdtempSync, rmSync } from 'fs'+import os from 'os'+import path from 'path'+import { initPlatform, resetPlatformForTests } from './platform'+import { fakePlatform, type FakePlatform } from './platform-fake'+import { IPC } from '../shared/ipc'+import type { PtyCreateOptions, PtyCreateResult } from '../shared/types'+import { codexAccountHome } from './codex-accounts-core'++const spawned: Array<{ file: string; args: string[]; env: Record<string, string> }> = []++vi.mock('node-pty', () => ({+ spawn: (file: string, args: string[], options: { env: Record<string, string> }) => {+ spawned.push({ file, args, env: options?.env ?? {} })+ return {+ onData: () => {},+ onExit: () => {},+ write: () => {},+ resize: () => {},+ pause: () => {},+ resume: () => {},+ kill: () => {},+ pid: 4321+ }+ }+}))++// Never let the real /dev probe refuse the spawn on the developer's host (see pty-require-remote).+vi.mock('./pty-devices', async (importOriginal) => ({+ ...(await importOriginal<typeof import('./pty-devices')>()),+ readPtyDevices: () => ({ ceiling: 511, inUse: 8 })+}))++const ALICE = 1++describe('pty create: a local Codex spawn fails closed on a missing selected account', () => {+ let fake: FakePlatform+ let userDataDir: string++ beforeEach(async () => {+ spawned.length = 0+ userDataDir = mkdtempSync(path.join(os.tmpdir(), 'nodeterm-codex-spawn-'))+ fake = fakePlatform({ userDataDir })+ initPlatform(fake)+ const { PtyManager } = await import('./pty-manager')+ new PtyManager().registerIpc()+ })+ afterEach(() => {+ resetPlatformForTests()+ rmSync(userDataDir, { recursive: true, force: true })+ })++ const create = (options: Partial<PtyCreateOptions>) =>+ fake.handlers[IPC.ptyCreate](ALICE, {+ cols: 80,+ rows: 24,+ persistKey: 'node-1',+ cwd: '/srv/app',+ ...options+ }) as Promise<PtyCreateResult>++ it('refuses (spawning nothing) when the explicitly selected managed home is missing', async () => {+ // The account home is deliberately NOT created.+ const res = await create({ agentId: 'codex', accountId: 'acct-missing' })++ expect(spawned).toHaveLength(0) // the whole point: no system-~/.codex shell wearing the switch id+ expect(res.unavailable).toBe('codex-account')+ expect(res.sessionId).toBe('')+ expect(res.fresh).toBe(false)+ })++ it('spawns under the managed CODEX_HOME + account id once the home exists', async () => {+ const home = codexAccountHome(userDataDir, 'acct-real')+ mkdirSync(home, { recursive: true })++ const res = await create({ agentId: 'codex', accountId: 'acct-real', persistKey: 'node-2' })++ expect(res.unavailable).toBeUndefined()+ expect(res.sessionId).not.toBe('')+ expect(spawned).toHaveLength(1)+ expect(spawned[0].env.CODEX_HOME).toBe(home)+ expect(spawned[0].env.NODETERM_CODEX_ACCOUNT_ID).toBe('acct-real')+ })++ it('a system Codex node (no account id) always spawns and clears any managed scope explicitly', async () => {+ const res = await create({ agentId: 'codex', persistKey: 'node-3' })++ expect(res.unavailable).toBeUndefined()+ expect(spawned).toHaveLength(1)+ // Explicitly empty — a parent tmux server's leaked managed scope must be overwritten, not+ // inherited (§2.1).+ expect(spawned[0].env.NODETERM_CODEX_ACCOUNT_ID).toBe('')+ expect(path.isAbsolute(spawned[0].env.CODEX_HOME)).toBe(true)+ })+})diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.tsindex 2890a6aff..1a6db86ae 100644--- a/src/core/pty-manager.ts+++ b/src/core/pty-manager.ts@@ -71,6 +71,13 @@ import { writeScrollback, readScrollback, deleteScrollback } from './scrollback-import { claudeConfigDirFor } from './claude-config-dir'import { findExecutableSync, findInPathString, resolveShellPath, shellPathNow } from './exec-path'import { AUTH_ENV_STRIP, accountTmuxEnvArgs, remoteAccountConfigDirAbs } from './claude-accounts-core'+import {+ AUTH_ENV_STRIP as CODEX_AUTH_ENV_STRIP,+ codexSessionEnv,+ isCodexScopeRefusal,+ needsCodexAccountScope,+ resolveCodexSessionScope+} from './codex-accounts-core'import { NODE_ID_MAX, isSafeNodeId } from './remote-safety'import { presenceHub } from './presence/hub'import {@@ -1713,6 +1720,20 @@ export class PtyManager {if (options.requireRemote && !(options.sshRemote && options.persistKey && findSsh())) {return { sessionId: '', fresh: false, unavailable: 'ssh' }}+ // FAIL-CLOSED Codex account scope (S6 §5 property 4 / Decision 2, the carried PR-1 obligation).+ // A LOCAL Codex spawn that EXPLICITLY selected a managed account whose home is missing REFUSES+ // here — it must never fall through and spawn against the SYSTEM `~/.codex` (silently acting as+ // the wrong login is a worse failure for an explicit switch than for a first spawn). This is+ // deliberately STRICTER than the Claude account path below, which falls back with a warning+ // chip. `resolveCodexSessionScope` returns `{ unavailable: 'codex-account' }` for exactly that+ // case; we map it straight through to a real refusal and spawn NOTHING. The system account (no+ // id) always resolves. Remote (ssh) Codex sessions carry their account env via tmux `-e`.+ if (needsCodexAccountScope(options.agentId, options.accountId) && !options.sshRemote) {+ const scope = resolveCodexSessionScope(platform().userDataDir, options.accountId)+ if (isCodexScopeRefusal(scope)) {+ return { sessionId: '', fresh: false, unavailable: 'codex-account' }+ }+ }// A tmux-backed session is "fresh" (cold start) when no live session exists to reattach to// — i.e. first open, or after a machine reboot killed the tmux server. Plain (non-tmux)// sessions are always fresh: they have no cross-restart continuity. The renderer uses this@@ -2099,6 +2120,21 @@ export class PtyManager {for (const k of AUTH_ENV_STRIP) delete env[k]}+ // Managed/system Codex account scope (S6 §2.1). A LOCAL Codex session runs under an EXPLICIT+ // CODEX_HOME + NODETERM_CODEX_ACCOUNT_ID: a managed id points at that account's private home+ // (its own auth.json + thread DB); the SYSTEM account (no id) is written EXPLICITLY so it+ // overwrites any managed scope a parent tmux server leaked in, rather than silently acting as+ // the wrong login. The missing-explicit-account case already refused in spawnNew (fail-closed,+ // property 4), so `codexSessionEnv` here never resolves an explicit id to the system home. Also+ // strip env vars that would shadow the account's OAuth login with API-key auth. Remote (ssh)+ // Codex sessions get this via the remote tmux `-e` list, not the local ssh client env.+ if (needsCodexAccountScope(options.agentId, options.accountId) && !options.sshRemote) {+ const codexScope = codexSessionEnv(platform().userDataDir, options.accountId)+ env.CODEX_HOME = codexScope.CODEX_HOME+ env.NODETERM_CODEX_ACCOUNT_ID = codexScope.NODETERM_CODEX_ACCOUNT_ID+ for (const k of CODEX_AUTH_ENV_STRIP) delete env[k]+ }+// Shared model gateway: resolve through the node's BASE harness in one shared mapping, then// apply it before custom-agent env. That precedence is intentional: an inheriting custom agent// benefits from the one-click gateway by default, but env values it explicitly declares stilldiff --git a/src/shared/types.ts b/src/shared/types.tsindex b0cb1d7e4..3b025c0a4 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -181,8 +181,12 @@ export interface PtyCreateResult {* Only ever set for a create that would have SPAWNED: a co-attach to a live session for this* node id still joins (the session is already running wherever it runs), so a second view of a* healthy remote terminal is unaffected.+ *+ * `'codex-account'` is the S6 fail-closed twin: a LOCAL Codex node that explicitly selected a+ * managed account whose home is missing refuses rather than spawning against the system login+ * (§5 property 4). Same contract — nothing spawned, the renderer shows the node's refusal.*/- unavailable?: 'ssh'+ unavailable?: 'ssh' | 'codex-account'}/** Payload of `pty:recycled` — see IPC.ptyRecycled and `recycleAction` in the renderer. */