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.

fix(codex): preserve SSH system early attach while merging main

reviewd0fb5c2d0252
diff --cc CLAUDE.md
index f108819c7,55aea8eb9..88a4ead86
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@@ -4685,3 -4829,3 +4829,9 @@@ sessions is a hazard whatever kills it
follow-up note rather than same-PR work — but flag it so it isn't forgotten.
When a change is genuinely desktop-only (native menus, auto-update, Keychain), say so; the
point is to make the call consciously, not to leave the other surfaces to rot.
++
++Remote Codex safety (#736): `spawnNew` refuses managed SSH Codex accounts (including custom
++Codex harnesses and known agent-less login terminals) until remote validation and per-account
++hooks are wired. System Codex retains the host environment through `remoteAccountScopeEnvArgs`,
++even before remoteHome discovery during early attach. Never guess HOME/CODEX_HOME. Desktop and
++Server share this core gate; remote account management remains unavailable.
diff --cc CONTRIBUTING.md
index 45a88684a,ac203f4e4..a63b3bb72
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@@ -691,9 -759,6 +759,14 @@@ Two files, two audiences
invariant that only lives in a commit message is one refactor away from being violated by someone
who never saw it.
+Remote Codex account safety (#736): managed SSH Codex sessions and login terminals are refused
+until host-side account validation and per-account hooks are wired. Do not remove that gate merely
- to enable the picker. System SSH Codex sessions require a resolved absolute remote home and set
- `CODEX_HOME` plus an empty `NODETERM_CODEX_ACCOUNT_ID` explicitly via tmux. Remote account add,
++to enable the picker. System SSH Codex sessions retain the remote host’s environment, including during early attach
++before remote home discovery; never inject a guessed `HOME` or `CODEX_HOME`. Custom Codex
++harnesses use the same account guard. Remote account add,
+login, retry, removal and identity UI remain follow-up work; the Server browser still explicitly
+rejects managed Codex account management.
++
+ Managed Codex login terminals are agent-less: core identifies their provider from the saved
+ account list. Before opening one, await `useSettings.getState().flush()` after adding the account.
+ The normal 300 ms coalesced save is too late: an unknown id can launch against the system home.
diff --cc src/core/codex-accounts-core.ts
index 582112136,582112136..cf3df999d
--- a/src/core/codex-accounts-core.ts
+++ b/src/core/codex-accounts-core.ts
@@@ -7,6 -7,6 +7,7 @@@
//
// Ships INERT (nothing spawns against it yet) but fully tested. Based on @Corvin's
// `codex-accounts-core.ts` in PR #112, re-sliced to the S6 PR-1 model layer.
++import { capabilityAgentId } from '../shared/agents/config'
import { createHash, randomUUID } from 'crypto'
import {
existsSync,
@@@ -240,7 -240,7 +241,7 @@@ export function needsCodexAccountScope
accountId: string | undefined,
isCodexAccount: (id: string) => boolean
): boolean {
-- if (agentId === 'codex') return true
++ if (agentId && capabilityAgentId(agentId) === 'codex') return true
if (!accountId) return false
return isCodexAccount(accountId)
}
diff --cc src/core/pty-manager.ts
index d885b2d64,238ca41ff..365d91ea4
--- a/src/core/pty-manager.ts
+++ b/src/core/pty-manager.ts
@@@ -86,8 -86,8 +86,7 @@@ import { findExecutableSync, findInPath
import {
AUTH_ENV_STRIP,
accountTmuxEnvArgs,
-- isReservedSpawnEnvKey,
-- remoteAccountConfigDirAbs
++ isReservedSpawnEnvKey
} from './claude-accounts-core'
import {
AUTH_ENV_STRIP as CODEX_AUTH_ENV_STRIP,
@@@ -2146,26 -2150,18 +2149,22 @@@ 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, (id) => this.isCodexAccount(id)) && !options.sshRemote) {
- const scope = resolveCodexSessionScope(platform().userDataDir, options.accountId)
- if (isCodexScopeRefusal(scope)) {
- return { sessionId: '', fresh: false, unavailable: 'codex-account' }
+ // Managed remote Codex accounts are not launchable yet: host-side account validation
+ // and per-account hook installation are not wired. Refuse agents AND login terminals;
+ // treating their id as Claude scope silently runs Codex against the host's system login.
+ if (needsCodexAccountScope(options.agentId, options.accountId, (id) => this.isCodexAccount(id))) {
+ if (options.sshRemote) {
- if (
- options.accountId ||
- !options.sshRemote.remoteHome ||
- !path.posix.isAbsolute(options.sshRemote.remoteHome)
- ) {
++ if (options.accountId) {
+ return { sessionId: '', fresh: false, unavailable: 'codex-account' }
+ }
+ if (!options.persistKey || !findSsh()) {
+ return { sessionId: '', fresh: false, unavailable: 'ssh' }
+ }
+ } else {
+ 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
@@@ -3071,14 -3067,14 +3070,15 @@@
// ABSOLUTE — tmux copies `-e` values verbatim (no `$HOME`/`~` expansion) — so we build it from
// the connection's resolved remote $HOME. Fail-open: an unknown remoteHome (home resolution
// failed on connect) skips the account env and the session runs under the remote `~/.claude`.
- // Codex system scope must overwrite a managed scope inherited by the remote tmux
- // server. spawnNew has already refused managed Codex and unresolved remote homes.
- const remoteAccountEnv =
- needsCodexAccountScope(options.agentId, options.accountId, (id) => this.isCodexAccount(id))
- ? remoteCodexTmuxEnvArgs(options.sshRemote.remoteHome!)
- : options.accountId && options.sshRemote.remoteHome
- ? accountTmuxEnvArgs(remoteAccountConfigDirAbs(options.sshRemote.remoteHome, options.accountId))
- : []
- // Routed by PROVIDER (`remoteAccountScopeEnvArgs`): a managed Codex account gets its
- // CODEX_HOME + NODETERM_CODEX_ACCOUNT_ID, a Claude one its CLAUDE_CONFIG_DIR.
++ // Routed by PROVIDER. spawnNew refuses managed Codex until remote validation/hooks
++ // are wired. System Codex retains the host defaults, including before home discovery
++ // during early attach; never guess a credential directory from the local environment.
+ const remoteAccountEnv = remoteAccountScopeEnvArgs({
+ agentId: options.agentId,
+ accountId: options.accountId,
+ remoteHome: options.sshRemote.remoteHome,
+ isCodexAccount: (id) => this.isCodexAccount(id)
+ })
// Custom-agent env for a REMOTE node: expand ${env:VAR} against the LOCAL process env (the
// key stays local; only the resolved VALUE travels over SSH). PATH is skipped — the local
// machine can't see the remote box's PATH, so a locally-resolved PATH would break CLI
diff --cc src/core/pty-project-settings.test.ts
index 65390aa08,801338b94..f8b231561
--- a/src/core/pty-project-settings.test.ts
+++ b/src/core/pty-project-settings.test.ts
@@@ -16,6 -16,6 +16,7 @@@ import { TMUX_SOCKET, sessionName } fro
import { DEFAULT_SETTINGS } from '../shared/types'
import { AUTH_ENV_STRIP, isReservedSpawnEnvKey } from './claude-accounts-core'
import { MODEL_GATEWAY_ENV_KEYS } from '../shared/agents/model-gateway'
++import { setCustomAgentBaseResolver } from '../shared/agents/config'
import { hookServer } from './agents/hook-server'
import type { ProjectSpawnOverrides } from './project-spawn-overrides'
@@@ -358,6 -358,6 +359,8 @@@ describe('project settings at the spaw
afterEach(() => {
setRemoteSessionEnvWriter(null)
resetPlatformForTests()
++ setCustomAgentBaseResolver(null)
++ vi.restoreAllMocks()
})
const sshRemote = {
@@@ -370,41 -370,6 +373,70 @@@
const create = async (options: Record<string, unknown>): Promise<unknown> =>
fake.handlers[IPC.ptyCreate](1, { cols: 80, rows: 24, ...options })
+ it.each(['remote-account', 'missing-account', '../unsafe'])(
+ 'refuses remote managed Codex account %s before any spawn', async (accountId) => {
+ await manager(null)
+ const result = await create({ agentId: 'codex', accountId, persistKey: NODE, sshRemote })
+ expect(result).toMatchObject({ sessionId: '', fresh: false, unavailable: 'codex-account' })
+ expect(spawns).toHaveLength(0)
+ expect(staged).toHaveLength(0)
+ }
+ )
+
+ it('refuses an agent-less remote Codex login terminal', async () => {
+ const mgr = await manager(null)
+ mgr.init(() => ({ ...DEFAULT_SETTINGS, codexAccounts: [{ id: 'remote-account', label: 'Work', host: 'u@h' }] }))
+ const result = await create({ accountId: 'remote-account', persistKey: NODE, sshRemote })
+ expect(result).toMatchObject({ unavailable: 'codex-account' })
+ expect(spawns).toHaveLength(0)
+ })
+
- it.each([undefined, '', 'relative/home', 'C:\\Users\\remote'])('refuses a remote Codex system scope with home %s', async (remoteHome) => {
- await manager(null)
- const result = await create({ agentId: 'codex', persistKey: NODE, sshRemote: { ...sshRemote, remoteHome } })
- expect(result).toMatchObject({ unavailable: 'codex-account' })
- expect(spawns).toHaveLength(0)
- })
++ describe.each(['codex', 'custom:codex'])('%s remote scope', (agentId) => {
++ async function codexManager() {
++ const mgr = await manager(null)
++ mgr.init(() => ({
++ ...DEFAULT_SETTINGS,
++ customAgents: [{ id: 'custom:codex', label: 'Codex wrapper', baseAgent: 'codex', launchCmd: 'codex' }]
++ }))
++ return mgr
++ }
+
- it('writes explicit remote system Codex scope into tmux, without Claude scope', async () => {
- await manager(null)
- await create({ agentId: 'codex', persistKey: NODE, sshRemote })
- expect(spawns).toHaveLength(1)
- const command = spawns[0].args.join(' ')
- expect(command).toContain('CODEX_HOME=/home/u/.codex')
- expect(command).toContain('NODETERM_CODEX_ACCOUNT_ID=')
- expect(command).not.toContain('CLAUDE_CONFIG_DIR=')
++ it.each(['remote-account', 'missing-account', '../unsafe'])(
++ 'refuses managed id %s even before home discovery', async (accountId) => {
++ await codexManager()
++ const result = await create({ agentId, accountId, persistKey: NODE, sshRemote: { ...sshRemote, remoteHome: undefined } })
++ expect(result).toMatchObject({ unavailable: 'codex-account' })
++ expect(spawns).toHaveLength(0)
++ expect(staged).toHaveLength(0)
++ }
++ )
++
++ it.each([undefined, '', 'relative/home', 'C:\\Users\\remote', '/home/u'])(
++ 'allows system scope with home %s without overriding host credentials', async (remoteHome) => {
++ await codexManager()
++ const result = await create({ agentId, persistKey: NODE, sshRemote: { ...sshRemote, remoteHome } })
++ expect(result).not.toHaveProperty('unavailable')
++ expect(spawns).toHaveLength(1)
++ expect(spawns[0].file).toBe('/usr/bin/ssh')
++ const command = spawns[0].args.join(' ')
++ expect(command).not.toMatch(/(?:CODEX_HOME|HOME|NODETERM_CODEX_ACCOUNT_ID|CLAUDE_CONFIG_DIR)=/)
++ expect(staged).toHaveLength(0)
++ }
++ )
++
++ it('attaches a confirmed existing session before home discovery', async () => {
++ const mgr = await codexManager()
++ vi.spyOn(mgr as unknown as { remoteSessionVerdict: () => Promise<string> }, 'remoteSessionVerdict').mockResolvedValue('present')
++ const result = await create({ agentId, persistKey: NODE, requireRemote: true, sshRemote: { ...sshRemote, remoteHome: undefined } })
++ expect(result).toMatchObject({ fresh: false })
++ expect(result).not.toHaveProperty('unavailable')
++ expect(spawns).toHaveLength(1)
++ expect(spawns[0].file).toBe('/usr/bin/ssh')
++ expect(spawns[0].args.join(' ')).toContain(sessionName(NODE))
++ expect(spawns[0].args.join(' ')).not.toContain('CODEX_HOME=')
++ })
+ })
+
it("stages the project's env in the 0600 file — never on the ssh argv", async () => {
await manager(async () => ({ env: { PROJECT_TOKEN: 'sekrit' } }))
await create({ persistKey: NODE, ownerProjectId: PROJECT, sshRemote })
diff --cc src/renderer/nodes/TerminalNode.tsx
index 2cee70a99,c5f1deb32..4dac6d06e
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@@ -1,4 -1,7 +1,8 @@@
+import { ptyRefusal } from '@shared/pty-refusal'
+ import { deliverRelayInitialLaunch } from '../terminal/relay-initial-launch'
+ import { commitLaunch } from '../terminal/launch-attempt'
+ import { isLaunchShell } from '@shared/agents/pane'
+ import { createLaunchWriter, deliverInitialLaunch, launchCommand, registerLaunchWriter } from '../terminal/launch-command'
import { Suspense, lazy, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
import { NODE_MIN_SIZES } from '../lib/nodeSizing'
import {
diff --cc src/shared/types.ts
index c09135462,45ff2b806..950b13236
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@@ -307,8 -307,7 +307,8 @@@ export interface PtyCreateResult
*
* `'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). Remote managed Codex accounts (validation/hooks not wired) and remote
- * system Codex with unresolved home also refuse. Nothing spawned; never use another account.
- * (§5 property 4). Same contract — nothing spawned, the renderer shows the node's refusal.
++ * (§5 property 4). Remote managed Codex accounts also refuse until validation/hooks are wired.
++ * System SSH Codex may attach before remote home discovery. Nothing spawned on refusal.
*/
unavailable?: 'ssh' | 'codex-account'
}