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
fix(shims): under a sandboxed Codex, name the sandbox instead of "nodeterm unreachable"
Claude Code2ad7debaad0c
diff --git a/src/core/agents/hook-sandbox-hint-sh.ts b/src/core/agents/hook-sandbox-hint-sh.tsnew file mode 100644index 000000000..f9fcb3971--- /dev/null+++ b/src/core/agents/hook-sandbox-hint-sh.ts@@ -0,0 +1,60 @@+/**+ * The Codex-sandbox self-diagnosis both generated sh shims share (issue #367).+ *+ * Codex's command sandbox denies network connect() outright when its network policy is disabled —+ * on Linux a seccomp filter EPERMs `connect()` for EVERY address family (unix sockets included,+ * with no allowlist), and on macOS the seatbelt base profile is `(deny default)` with no+ * network-outbound allowance. Either way curl dies with exit != 0 / HTTP 000 while the nodeterm+ * hook server is perfectly healthy, and the shims' generic "nodeterm unreachable" message sent+ * agents (and users) off to relink or restart a server that was never the problem.+ *+ * Codex exports `CODEX_SANDBOX_NETWORK_DISABLED=1` into every sandboxed command on both+ * platforms, so the shims can tell the two failures apart at the exact moment they happen. The+ * remedies differ by platform:+ * - both: retry the SAME command once with command-specific escalated permissions (the sandbox+ * is per-command; an escalated retry runs outside it);+ * - macOS only (codex >= 0.146): `network.allow_unix_sockets = ["<hook sock>"]` in+ * ~/.codex/config.toml permanently re-opens exactly the hook socket inside the sandbox, no+ * escalation needed. Linux has no such allowlist, so that line would be a false promise there+ * — the fragment gates it on `uname -s` = Darwin as well as on the socket being advertised.+ *+ * The message text lives in exported constants so the agent-facing docs (skill bodies + AGENTS.md+ * instruction blocks) can carry the same sentences and the parity tests can hold the two ends+ * together; the sh fragment is exercised against real /bin/sh in the shim test suites.+ */++/** Both platforms, line 1: what actually failed, and what NOT to do about it. */+export const CODEX_SANDBOX_BLOCKED_LINE =+ "Codex's sandbox blocked this connection to nodeterm. nodeterm itself is running — do not relink or restart it."++/** Both platforms, line 2: the one action that works everywhere. */+export const CODEX_SANDBOX_RETRY_LINE =+ 'Retry the SAME command once with command-specific escalated permissions (approve the escalation prompt for this command).'++/** macOS only, wrapped around the advertised hook socket path. */+export const CODEX_SANDBOX_MACOS_BEFORE =+ 'Permanent fix on macOS: add network.allow_unix_sockets = ['+export const CODEX_SANDBOX_MACOS_AFTER =+ '] to ~/.codex/config.toml — the sandbox then allows this hook socket with no escalation.'++/**+ * POSIX-sh fragment defining `nt_codex_sandbox_hint`: prints the actionable error to stderr and+ * returns 0 when the codex sandbox is the diagnosis, returns 1 (printing nothing) otherwise —+ * so call sites read `nt_codex_sandbox_hint || echo "<the old generic message>" >&2` and the+ * genuine-unreachable case keeps its original text to the byte.+ */+export const CODEX_SANDBOX_HINT_SH = `# Codex-sandbox self-diagnosis (issue #367). Codex exports CODEX_SANDBOX_NETWORK_DISABLED=1 into+# every sandboxed command; when it is present, a dead transport (curl exit != 0 / HTTP 000) means+# the SANDBOX denied connect() — Linux seccomp EPERMs every address family, macOS seatbelt is+# deny-by-default — and the generic "unreachable" message would misdirect the agent into+# relinking/restarting a healthy server. Prints the actionable error and returns 0 only in that+# state; callers fall back to their own generic message on 1.+nt_codex_sandbox_hint() {+ [ -n "$CODEX_SANDBOX_NETWORK_DISABLED" ] || return 1+ echo "${CODEX_SANDBOX_BLOCKED_LINE}" >&2+ echo "${CODEX_SANDBOX_RETRY_LINE}" >&2+ if [ -n "$NODETERM_HOOK_SOCK" ] && [ "$(uname -s)" = "Darwin" ]; then+ echo "${CODEX_SANDBOX_MACOS_BEFORE}\\"$NODETERM_HOOK_SOCK\\"${CODEX_SANDBOX_MACOS_AFTER}" >&2+ fi+ return 0+}`diff --git a/src/core/context-link-core.test.ts b/src/core/context-link-core.test.tsindex c7bc516f1..2edae4ea1 100644--- a/src/core/context-link-core.test.ts+++ b/src/core/context-link-core.test.ts@@ -5,7 +5,9 @@ import {mergeInstructionsBlock,resolveLinkTranscript,setNodeTranscript,- transcriptPathOf+ transcriptPathOf,+ CONTEXT_SHIM_SCRIPT,+ CONTEXT_UNREACHABLE_MSG} from './context-link-core'describe('buildLinkDoc', () => {@@ -202,4 +204,10 @@ describe('buildLinkedContextInstructions', () => {expect(s).toContain('transcript')expect(s).toContain('terminal')})++ it('the shim embeds the sandbox hint, keeping the generic sentence for the non-sandbox case', () => {+ expect(CONTEXT_SHIM_SCRIPT).toContain('nt_codex_sandbox_hint() {')+ expect(CONTEXT_SHIM_SCRIPT).toContain('nt_codex_sandbox_hint && exit 1')+ expect(CONTEXT_SHIM_SCRIPT).toContain(`echo "${CONTEXT_UNREACHABLE_MSG}" >&2`)+ })})diff --git a/src/core/context-link-core.ts b/src/core/context-link-core.tsindex f1f00a1fc..ff770d235 100644--- a/src/core/context-link-core.ts+++ b/src/core/context-link-core.ts@@ -4,6 +4,11 @@import type { ContextLinkInfo } from '../shared/types'import { sessionName } from './tmux-naming'import { HOOK_CURL_HEADERS_SH } from './agents/hook-curl-config-sh'+import { CODEX_SANDBOX_HINT_SH } from './agents/hook-sandbox-hint-sh'++/** The shim's generic transport-failure sentence — exported so the agent-facing docs below can+ * quote it verbatim and the parity test holds the two ends together. */+export const CONTEXT_UNREACHABLE_MSG = 'Could not read linked context (nodeterm unreachable).'// nodeId -> latest known transcript path, fed from the raw hook listener (see index.ts).const nodeTranscript = new Map<string, string>()@@ -168,6 +173,8 @@ fi${HOOK_CURL_HEADERS_SH}+${CODEX_SANDBOX_HINT_SH}+nt_verb="list"if [ $# -gt 0 ]; then nt_verb="$1"; shift; fi@@ -215,7 +222,12 @@ if [ "$nt_code" = "200" ]; thenficat "$nt_out" >&2 2>/dev/nullrm -f "$nt_out"-echo "Could not read linked context (nodeterm unreachable)." >&2+# Empty / 000 = the TRANSPORT failed, not the server. Under a codex sandbox that is the sandbox's+# own connect() denial (issue #367), and the generic sentence below would misdirect the agent.+if [ -z "$nt_code" ] || [ "$nt_code" = "000" ]; then+ nt_codex_sandbox_hint && exit 1+fi+echo "${CONTEXT_UNREACHABLE_MSG}" >&2exit 1`diff --git a/src/core/context-link.cli.test.ts b/src/core/context-link.cli.test.tsindex c74ac2afb..bd29ee85e 100644--- a/src/core/context-link.cli.test.ts+++ b/src/core/context-link.cli.test.ts@@ -460,3 +460,52 @@ describe('context-link shim keeps credentials off curl\'s command line', () => {expect(argv).not.toContain('X-Nodeterm-Node-Token')})})++// Issue #367, context-link leg. The sh fragment is the SAME one the canvas-control shim embeds+// (hook-sandbox-hint-sh.ts) and the full platform/transport matrix lives in+// canvas-control-shim.test.ts; what this suite pins is that the CONTEXT shim wired it into ITS+// failure tail — with its own generic sentence kept byte-for-byte for the non-sandbox case.+describe('codex-sandbox self-diagnosis (issue #367)', () => {+ function deadRun(env: Record<string, string>): Promise<{ stderr: string; code?: number }> {+ return run('/bin/sh', [shim, 'list'], {+ env: {+ PATH: process.env.PATH ?? '',+ NODETERM_NODE_ID: 'node-A',+ NODETERM_HOOK_SOCK: join(dir, 'nobody-listens.sock'),+ NODETERM_HOOK_TOKEN: 'x',+ ...env+ }+ }).then(+ () => ({ stderr: '' }),+ (e) => e as { stderr: string; code?: number }+ )+ }++ it('under the sandbox, a dead transport names the sandbox + escalated retry, not "unreachable"', async () => {+ const err = await deadRun({ CODEX_SANDBOX_NETWORK_DISABLED: '1' })+ expect(err.code).toBe(1)+ expect(err.stderr).toContain("Codex's sandbox blocked this connection to nodeterm")+ expect(err.stderr).toContain('escalated permissions')+ expect(err.stderr).not.toContain('Could not read linked context (nodeterm unreachable).')+ })++ // The mutation guard: drop the env-var branch and the case above reddens; invert it and this does.+ it('without the env var the original genuine-unreachable sentence stands', async () => {+ const err = await deadRun({})+ expect(err.stderr).toContain('Could not read linked context (nodeterm unreachable).')+ expect(err.stderr).not.toContain("Codex's sandbox")+ })++ it('a healthy endpoint is untouched by the sandbox var (failure-path only)', async () => {+ const out = await run('/bin/sh', [shim, 'list'], {+ env: {+ PATH: process.env.PATH ?? '',+ NODETERM_NODE_ID: 'node-A',+ NODETERM_HOOK_PORT: String(hookServer.getPort()),+ NODETERM_HOOK_TOKEN: hookServer.getToken(),+ CODEX_SANDBOX_NETWORK_DISABLED: '1'+ }+ })+ expect(out.stdout).toContain('Builder')+ })+})diff --git a/src/main/canvas-control-core.test.ts b/src/main/canvas-control-core.test.tsindex f5a7964c2..d93a9d715 100644--- a/src/main/canvas-control-core.test.ts+++ b/src/main/canvas-control-core.test.ts@@ -5,8 +5,13 @@ import {mergeCanvasControlBlock,buildCanvasControlInstructions,buildCanvasSkillBody,- CONTROL_SHIM_SCRIPT+ CONTROL_SHIM_SCRIPT,+ CONTROL_UNREACHABLE_MSG} from './canvas-control-core'+import {+ CODEX_SANDBOX_BLOCKED_LINE,+ CODEX_SANDBOX_RETRY_LINE+} from '../core/agents/hook-sandbox-hint-sh'import { RETRYABLE } from '../core/agents/agent-message-decide'import { PROJECT_TARGETABLE_VERBS } from './project-grants'import { STRICT_CONTROL_VERBS } from '../core/agents/node-identity-policy'@@ -223,6 +228,17 @@ describe('parseControlRequest', () => {expect(body).toMatch(/starts? with `--`/)})+ it('the shim embeds the sandbox hint and its call sites in both failure tails', () => {+ // The fragment (one definition)...+ expect(CONTROL_SHIM_SCRIPT).toContain('nt_codex_sandbox_hint() {')+ expect(CONTROL_SHIM_SCRIPT).toContain(CODEX_SANDBOX_BLOCKED_LINE)+ expect(CONTROL_SHIM_SCRIPT).toContain(CODEX_SANDBOX_RETRY_LINE)+ // ...and the fallback shape: the genuine-unreachable sentence survives to the byte.+ expect(CONTROL_SHIM_SCRIPT).toContain(+ `nt_codex_sandbox_hint || echo "${CONTROL_UNREACHABLE_MSG}" >&2`+ )+ })+it('send/reply require --text (Task 5.4)', () => {expect(parseControlRequest('send', { node: 'n1' })).toEqual({ error: 'send requires --text' })expect(parseControlRequest('reply', { node: 'n1' })).toEqual({ error: 'reply requires --text' })diff --git a/src/main/canvas-control-core.ts b/src/main/canvas-control-core.tsindex 1fe2dbdf1..bb7146906 100644--- a/src/main/canvas-control-core.ts+++ b/src/main/canvas-control-core.ts@@ -2,6 +2,7 @@// CLI source. No electron imports, so this module + CONTROL_CLI_SCRIPT are unit-testable.// Electron/ipc/server wiring lives in canvas-control.ts + index.ts + hook-server.ts.import { HOOK_CURL_HEADERS_SH } from '../core/agents/hook-curl-config-sh'+import { CODEX_SANDBOX_HINT_SH } from '../core/agents/hook-sandbox-hint-sh'import { AGENT_CONFIG, AGENT_HOOK_TARGETS, BUILTIN_AGENT_IDS } from '@shared/agents/config'import { RETRYABLE } from '../core/agents/agent-message-decide'import { FANOUT_PER_TURN, PAIR_MIN_INTERVAL_MS } from '../core/agents/agent-message-flow'@@ -403,6 +404,10 @@ export function buildCanvasControlInstructions(shimPath: string): string {// remote agent nodes only after a reconnect, with no signal on the wire — the same shape as the// managed hook script's stale window. Verbs are therefore designed to parse identically under both// the old and the new loop: give every flag a value, and the two loops agree.+/** The shim's generic transport-failure sentence — exported so the agent-facing docs can quote it+ * verbatim and the parity test holds the two ends together (issue #367). */+export const CONTROL_UNREACHABLE_MSG = 'Could not reach nodeterm (control endpoint unreachable).'+export const CONTROL_SHIM_SCRIPT = `#!/bin/sh# nodeterm canvas-control CLI (auto-generated — do not edit).@@ -429,6 +434,8 @@ fi${HOOK_CURL_HEADERS_SH}+${CODEX_SANDBOX_HINT_SH}+nt_verb="list"if [ $# -gt 0 ]; then nt_verb="$1"; shift; fi@@ -510,8 +517,10 @@ if [ "$nt_code" = "200" ]; thenficat "$nt_out" >&2 2>/dev/nullrm -f "$nt_out"+# Empty / 000 = the TRANSPORT failed, not the server. Under a codex sandbox that is the sandbox's+# own connect() denial (issue #367), and the generic sentence would misdirect the agent.if [ -z "$nt_code" ] || [ "$nt_code" = "000" ]; then- echo "Could not reach nodeterm (control endpoint unreachable)." >&2+ nt_codex_sandbox_hint || echo "${CONTROL_UNREACHABLE_MSG}" >&2fiexit 1`diff --git a/src/main/canvas-control-shim.test.ts b/src/main/canvas-control-shim.test.tsindex 593add5a6..ae21f6427 100644--- a/src/main/canvas-control-shim.test.ts+++ b/src/main/canvas-control-shim.test.ts@@ -497,6 +497,106 @@ describe('canvas-control shim keeps credentials off curl\'s command line', () =>})})+// Issue #367: Codex's command sandbox denies connect() for every address family (Linux seccomp;+// macOS seatbelt deny-default), so curl dies with HTTP 000 while nodeterm is perfectly healthy —+// and the old message ("control endpoint unreachable") sent agents off to relink/restart a server+// that was never the problem. Codex exports CODEX_SANDBOX_NETWORK_DISABLED=1 into every sandboxed+// command, so the shim can tell the two failures apart. Run against real /bin/sh with a genuinely+// dead endpoint; `uname` is faked on PATH so the macOS-only remedy line is deterministic on any CI.+describe('codex-sandbox self-diagnosis (issue #367)', () => {+ let deadSock = ''+ let unameDir = (os: 'Darwin' | 'Linux'): string => os // reassigned in beforeAll++ beforeAll(() => {+ deadSock = path.join(dir, 'nobody-listens.sock')+ const bins: Record<string, string> = {}+ unameDir = (osName) => {+ if (!bins[osName]) {+ const d = path.join(dir, `fake-uname-${osName.toLowerCase()}`)+ fs.mkdirSync(d, { recursive: true })+ fs.writeFileSync(path.join(d, 'uname'), `#!/bin/sh\necho ${osName}\n`, { mode: 0o755 })+ bins[osName] = d+ }+ return bins[osName]+ }+ })++ const sandboxEnv = (osName: 'Darwin' | 'Linux', extra: Record<string, string> = {}): Record<string, string> => ({+ PATH: `${unameDir(osName)}:${process.env.PATH ?? ''}`,+ NODETERM_HOOK_PORT: '',+ NODETERM_HOOK_SOCK: deadSock,+ CODEX_SANDBOX_NETWORK_DISABLED: '1',+ ...extra+ })++ it('under the sandbox, a dead socket names the sandbox and the escalated retry — not "unreachable"', async () => {+ await expect(callShim(['list'], sandboxEnv('Linux'))).rejects.toMatchObject({+ code: 1,+ stderr: expect.stringContaining("Codex's sandbox blocked this connection to nodeterm")+ })+ await expect(callShim(['list'], sandboxEnv('Linux'))).rejects.toMatchObject({+ stderr: expect.stringContaining('escalated permissions')+ })+ const err = await callShim(['list'], sandboxEnv('Linux')).catch((e) => e as { stderr: string })+ expect(err.stderr).toContain('do not relink or restart it')+ expect(err.stderr).not.toContain('Could not reach nodeterm')+ })++ it('on Darwin with a socket advertised, it also prints the config.toml allowlist remedy with the path', async () => {+ const err = await callShim(['list'], sandboxEnv('Darwin')).catch((e) => e as { stderr: string })+ expect(err.stderr).toContain(`network.allow_unix_sockets = ["${deadSock}"]`)+ expect(err.stderr).toContain('~/.codex/config.toml')+ })++ it('on Linux the allowlist line is withheld — the Linux sandbox has no such allowlist', async () => {+ const err = await callShim(['list'], sandboxEnv('Linux')).catch((e) => e as { stderr: string })+ expect(err.stderr).not.toContain('network.allow_unix_sockets')+ })++ it('the same diagnosis fires on the TCP branch (a sandboxed curl to loopback dies identically)', async () => {+ // A port with no listener: bind-then-close frees it, and nothing else grabs it mid-test.+ const http = await import('node:http')+ const probe = http.createServer()+ await new Promise<void>((r) => probe.listen(0, '127.0.0.1', r))+ const freePort = (probe.address() as { port: number }).port+ await new Promise<void>((r) => probe.close(() => r()))+ const err = await callShim(['list'], {+ PATH: `${unameDir('Linux')}:${process.env.PATH ?? ''}`,+ NODETERM_HOOK_PORT: String(freePort),+ CODEX_SANDBOX_NETWORK_DISABLED: '1'+ }).catch((e) => e as { stderr: string })+ expect(err.stderr).toContain("Codex's sandbox blocked this connection to nodeterm")+ })++ // THE MUTATION GUARD for the env-var branch: without the variable, the same dead transport must+ // keep the original sentence to the byte — dropping the branch turns the sandbox cases above+ // red, and inverting it turns this one red.+ it('without CODEX_SANDBOX_NETWORK_DISABLED the original genuine-unreachable message stands', async () => {+ const err = await callShim(['list'], {+ PATH: `${unameDir('Darwin')}:${process.env.PATH ?? ''}`,+ NODETERM_HOOK_PORT: '',+ NODETERM_HOOK_SOCK: deadSock+ }).catch((e) => e as { stderr: string })+ expect(err.stderr).toContain('Could not reach nodeterm (control endpoint unreachable).')+ expect(err.stderr).not.toContain("Codex's sandbox")+ })++ it('never fires on a genuine HTTP error — the server answered, so the transport is fine', async () => {+ // `boom` reaches the real hook server (which answers 400 with a body): sandbox var set, but+ // nt_code is a real status, not 000.+ const err = await callShim(['boom'], { CODEX_SANDBOX_NETWORK_DISABLED: '1' }).catch(+ (e) => e as { stderr: string }+ )+ expect(err.stderr).toContain('that verb exploded')+ expect(err.stderr).not.toContain("Codex's sandbox")+ })++ it('a healthy endpoint stays healthy with the sandbox var set (the hint is failure-path only)', async () => {+ const { stdout } = await callShim(['list'], { CODEX_SANDBOX_NETWORK_DISABLED: '1' })+ expect(stdout.trim()).toBe('did list')+ })+})+describe('parseControlBody', () => {it('reads the shim dialect: nodeId plus arg.<name> fields', () => {expect(