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): fail-closed account selection + switch-recycle eligibility (Property 4)

Claude Codeb82ab2831e56
diff --git a/src/renderer/canvas/codex-account-switch.test.ts b/src/renderer/canvas/codex-account-switch.test.ts
new file mode 100644
index 000000000..a4cc5360d
--- /dev/null
+++ b/src/renderer/canvas/codex-account-switch.test.ts
@@ -0,0 +1,84 @@
+import { describe, expect, it } from 'vitest'
+import type { CodexAccount } from '@shared/codex-account'
+import { codexAccountSelectable, codexAccountSwitchStillEligible } from './codex-account-switch'
+
+const expected = {
+ accountId: 'account-a',
+ agentId: 'codex',
+ cwd: '/repo',
+ sessionId: 'thread-a',
+ ssh: false
+}
+
+describe('codexAccountSwitchStillEligible', () => {
+ it('accepts only the unchanged idle source conversation', () => {
+ expect(codexAccountSwitchStillEligible(expected, { ...expected, state: 'done' })).toBe(true)
+ })
+
+ it('accepts an unchanged remote source without pretending it is local', () => {
+ const remote = { ...expected, ssh: true }
+ expect(codexAccountSwitchStillEligible(remote, { ...remote, state: 'done' })).toBe(true)
+ })
+
+ it.each([
+ { name: 'still working', current: { ...expected, state: 'working' } },
+ // MUTATION PIN: drop the `sessionId` equality from the eligibility check → this case
+ // (a diverged thread) goes green, i.e. a forked pane would be recycled. Must stay red.
+ { name: 'sessionId diverged', current: { ...expected, sessionId: 'thread-new', state: 'done' } },
+ { name: 'accountId changed', current: { ...expected, accountId: 'account-b', state: 'done' } },
+ { name: 'cwd moved', current: { ...expected, cwd: '/other', state: 'done' } },
+ { name: 'ssh flag flipped', current: { ...expected, ssh: true, state: 'done' } }
+ ])('rejects state drift before recycle: $name', ({ current }) => {
+ expect(codexAccountSwitchStillEligible(expected, current)).toBe(false)
+ })
+})
+
+describe('codexAccountSelectable (fail-closed selection — Property 4)', () => {
+ const local: CodexAccount = { id: 'account-a', label: 'Work' }
+ const remote: CodexAccount = { id: 'account-r', label: 'Box', host: 'u@box' }
+ const accounts = [local, remote]
+ const connected = (host: string) => (host === 'u@box' ? 'proj-1' : undefined)
+ const noConnection = () => undefined
+
+ it('allows the system account (no id) on this Mac', () => {
+ expect(codexAccountSelectable(undefined, accounts, noConnection)).toEqual({ ok: true })
+ expect(codexAccountSelectable('', accounts, noConnection)).toEqual({ ok: true })
+ })
+
+ it('allows a present local managed account', () => {
+ expect(codexAccountSelectable('account-a', accounts, noConnection)).toEqual({ ok: true })
+ })
+
+ it('refuses an explicitly selected MISSING account instead of falling back', () => {
+ // The user picked an id that is no longer in the settings list — refuse, never resolve it to
+ // the system login or any other account.
+ expect(codexAccountSelectable('account-gone', accounts, connected)).toEqual({
+ ok: false,
+ reason: 'unavailable'
+ })
+ })
+
+ it('refuses a hostile account id even when a matching row is PRESENT in settings', () => {
+ // settings.json is attacker-controlled (Constraint 7): a hostile row can carry an escaping id.
+ // MUTATION PIN: drop the isSafeAccountId guard → this row is FOUND (local, no host) and returns
+ // { ok: true }, i.e. a path-escaping id becomes a launch target. Must stay red.
+ const hostile: CodexAccount = { id: '../escape', label: 'evil' }
+ expect(codexAccountSelectable('../escape', [...accounts, hostile], connected)).toEqual({
+ ok: false,
+ reason: 'unavailable'
+ })
+ })
+
+ it('refuses a remote account with no live connection — never runs it locally', () => {
+ // MUTATION PIN: drop the `account.host && !connection` gate → this goes { ok: true } and the
+ // remote switch would run against the LOCAL login. Must stay red.
+ expect(codexAccountSelectable('account-r', accounts, noConnection)).toEqual({
+ ok: false,
+ reason: 'no-connection'
+ })
+ })
+
+ it('allows a remote account once its host is connected', () => {
+ expect(codexAccountSelectable('account-r', accounts, connected)).toEqual({ ok: true })
+ })
+})
diff --git a/src/renderer/canvas/codex-account-switch.ts b/src/renderer/canvas/codex-account-switch.ts
new file mode 100644
index 000000000..65233ce7f
--- /dev/null
+++ b/src/renderer/canvas/codex-account-switch.ts
@@ -0,0 +1,89 @@
+// S6 PR 8 — the renderer-side gates for the Codex account switch/create UI. The actual
+// authorization is main-side (PR 5's three-phase, owner-authorized switch); NOTHING here is the
+// security boundary. These are the renderer's own fail-closed refusals so the UI can never
+// ORIGINATE an unauthorized or fail-open switch:
+// 1. `codexAccountSwitchStillEligible` — the source pane may be recycled only if it is still the
+// exact idle conversation the user chose when the fork began (Corvin's #112 guard).
+// 2. `codexAccountSelectable` — an explicitly selected account that is MISSING, unsafe, or a
+// remote account with no live connection is REFUSED; the UI never silently falls back to
+// another login (§5 Property 4 / Decision 2).
+
+import type { CodexAccount } from '@shared/codex-account'
+import { isSafeAccountId } from '@shared/codex-account'
+import { restartEligibility } from '../terminal/agent-restart'
+
+export interface CodexAccountSwitchState {
+ accountId?: string
+ agentId?: string
+ cwd?: string
+ sessionId?: string
+ ssh?: boolean
+ state?: string
+}
+
+/**
+ * The account fork can take seconds. Nothing may recycle the source pane unless it is still the
+ * exact idle conversation that the user chose when the fork began. The `sessionId` equality is the
+ * "resume the same conversation id, never fork" enforcement — a pane whose thread has diverged is
+ * never recycled onto the switched account.
+ */
+export function codexAccountSwitchStillEligible(
+ expected: Required<Pick<CodexAccountSwitchState, 'agentId' | 'cwd' | 'sessionId'>> &
+ Pick<CodexAccountSwitchState, 'accountId' | 'ssh'>,
+ current: CodexAccountSwitchState
+): boolean {
+ return (
+ current.agentId === expected.agentId &&
+ current.agentId === 'codex' &&
+ !!current.ssh === !!expected.ssh &&
+ current.cwd === expected.cwd &&
+ current.accountId === expected.accountId &&
+ current.sessionId === expected.sessionId &&
+ restartEligibility('codex', current.state, current.sessionId).ok
+ )
+}
+
+/**
+ * Why a chosen Codex account cannot be launched/switched to right now. Fail-closed verdicts only —
+ * the absence of a reason (`{ ok: true }`) is the sole path that proceeds.
+ */
+export type CodexAccountSelectability =
+ | { ok: true }
+ | {
+ /** `unavailable` — the id names no known/safe account (a missing or hostile selection).
+ * `no-connection` — a remote account whose host is not currently connected. */
+ ok: false
+ reason: 'unavailable' | 'no-connection'
+ }
+
+/**
+ * Fail-closed gate for the create/switch menus (§5 Property 4). `selectedId` empty/undefined = the
+ * SYSTEM account on this Mac (always selectable). Any managed id must:
+ * - pass `isSafeAccountId` — a hostile settings/relay id never becomes a launch target;
+ * - name an account still present in `accounts` — an explicitly selected MISSING account is
+ * refused, never silently resolved to the system or another login;
+ * - for a remote account, have a live connection — `connectedProjectIdForHost(host)` must return
+ * a project id, else the switch is refused rather than run against the local login.
+ *
+ * The UI must proceed ONLY on `{ ok: true }`; every other verdict shows the account as unavailable
+ * and does nothing.
+ */
+export function codexAccountSelectable(
+ selectedId: string | undefined,
+ accounts: readonly CodexAccount[],
+ connectedProjectIdForHost: (host: string) => string | undefined
+): CodexAccountSelectability {
+ // The system account (no id) always lives on this Mac — nothing to connect to, nothing to miss.
+ if (!selectedId) return { ok: true }
+ // Supply-chain: an id that could escape the filesystem is never a valid selection.
+ if (!isSafeAccountId(selectedId)) return { ok: false, reason: 'unavailable' }
+ const account = accounts.find((candidate) => candidate.id === selectedId)
+ // Explicitly selected but absent from the known set ⇒ refuse (never fall back to another login).
+ if (!account) return { ok: false, reason: 'unavailable' }
+ // A remote account is only operable while its host is connected; without a live connection the
+ // op would run against the LOCAL login — the exact fail-open Property 4 forbids.
+ if (account.host && !connectedProjectIdForHost(account.host)) {
+ return { ok: false, reason: 'no-connection' }
+ }
+ return { ok: true }
+}