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): the account-scoped sh resolver, proven under real /bin/sh (probe U5)
Claude Codec0c58e2dfc45
diff --git a/src/core/agents/hooks/managed-script.test.ts b/src/core/agents/hooks/managed-script.test.tsindex 444aa2261..87cf82846 100644--- a/src/core/agents/hooks/managed-script.test.ts+++ b/src/core/agents/hooks/managed-script.test.ts@@ -156,8 +156,9 @@ describe('buildManagedScript', () => {// under /bin/sh in core/codex-thread-identity-sh.test.ts.)it('is present in every agent script when an identity root is known', () => {for (const agent of ['claude', 'codex', 'gemini', 'grok', 'opencode']) {+ // The account-scoped resolver bakes the (quoted) record root in and scans scopes below it.expect(buildManagedScript(agent, '/data/codex-thread-nodes')).toContain(- "nt_codex_map='/data/codex-thread-nodes'/\"$CODEX_THREAD_ID\""+ "nt_codex_root='/data/codex-thread-nodes'")}})diff --git a/src/core/codex-thread-identity-sh.test.ts b/src/core/codex-thread-identity-sh.test.tsindex 20006d3b1..b3465bbb5 100644--- a/src/core/codex-thread-identity-sh.test.ts+++ b/src/core/codex-thread-identity-sh.test.ts@@ -2,7 +2,7 @@// quoting slip here does not break "codex identity recovery" — it breaks every agent's hooks on// every machine. It is also the one place where a data file's contents become environment// variables, which is why the negative cases below matter as much as the positive one.-import { describe, it, expect, beforeAll, afterAll } from 'vitest'+import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'vitest'import { execFile } from 'node:child_process'import fs from 'node:fs'import os from 'node:os'@@ -36,6 +36,18 @@ function record(threadId: string, body: string): void {fs.writeFileSync(path.join(root, threadId), body)}+/** Write a managed-scope record under `<root>/<accountId>/<threadId>`. */+function scopedRecord(accountId: string, threadId: string, body: string): void {+ fs.mkdirSync(path.join(root, accountId), { recursive: true })+ fs.writeFileSync(path.join(root, accountId, threadId), body)+}++/** A well-formed record body for a scope (empty account = system, no accountId line = legacy). */+function body(nodeId: string, accountId?: string): string {+ const acct = accountId === undefined ? '' : `accountId=${accountId}\n`+ return `${acct}nodeId=${nodeId}\nendpoint=${dir}/hook-endpoint.env\nsignature=x\n`+}+async function resolve(env: Record<string, string>): Promise<string> {const { stdout } = await run('/bin/sh', [script], {env: { PATH: process.env.PATH ?? '', HOME: dir, ...env }@@ -80,3 +92,71 @@ describe('codex thread identity prelude', () => {expect(await resolve({ CODEX_THREAD_ID: 'thread-bad-ep2' })).toBe('||')})})++// ── S6 PR 2: the account-scoped sh resolver, proven under real /bin/sh (Property 8, Constraint 8) ──+describe('codex thread identity prelude — account scoping', () => {+ // The subdir layout differs per test; clear the record store before each so scopes never bleed.+ beforeEach(() => {+ fs.rmSync(root, { recursive: true, force: true })+ fs.mkdirSync(root, { recursive: true })+ })++ it('binds the one matching managed scope when the daemon carries that account id', async () => {+ scopedRecord('acct-A', 'thr-1', body('node-A', 'acct-A'))+ expect(+ await resolve({ CODEX_THREAD_ID: 'thr-1', NODETERM_CODEX_ACCOUNT_ID: 'acct-A' })+ ).toBe(`node-A|${dir}/hook-endpoint.env|1`)+ })++ it('binds the single candidate across scopes when no account id is present (system record)', async () => {+ record('thr-1', body('node-sys')) // bare-root legacy/system record, no account line+ expect(await resolve({ CODEX_THREAD_ID: 'thr-1' })).toBe(`node-sys|${dir}/hook-endpoint.env|1`)+ })++ it('binds the single candidate across scopes when no account id is present (one managed record)', async () => {+ scopedRecord('acct-A', 'thr-1', body('node-A', 'acct-A'))+ expect(await resolve({ CODEX_THREAD_ID: 'thr-1' })).toBe(`node-A|${dir}/hook-endpoint.env|1`)+ })++ it('fails closed: two scopes hold the same thread id and no account env clears the map', async () => {+ scopedRecord('acct-A', 'thr-1', body('node-A', 'acct-A'))+ scopedRecord('acct-B', 'thr-1', body('node-B', 'acct-B'))+ // MUTATION TARGET: bind on the first match (drop `-eq 1`) ⇒ this exports node-A and reddens.+ expect(await resolve({ CODEX_THREAD_ID: 'thr-1' })).toBe('||')+ })++ it('fails closed: a system record and a managed record collide with no account env', async () => {+ record('thr-1', body('node-sys'))+ scopedRecord('acct-A', 'thr-1', body('node-A', 'acct-A'))+ expect(await resolve({ CODEX_THREAD_ID: 'thr-1' })).toBe('||')+ })++ it('clears the map when the record account line disagrees with its directory scope', async () => {+ // A record physically under acct-B but whose own line still claims acct-A: not honoured as B.+ scopedRecord('acct-B', 'thr-1', body('node-A', 'acct-A'))+ expect(+ await resolve({ CODEX_THREAD_ID: 'thr-1', NODETERM_CODEX_ACCOUNT_ID: 'acct-B' })+ ).toBe('||')+ // And the unscoped scan skips it too (the scope check fails), so nothing binds.+ expect(await resolve({ CODEX_THREAD_ID: 'thr-1' })).toBe('||')+ })++ it('reads only the named account, not a same-thread record in another scope', async () => {+ scopedRecord('acct-A', 'thr-1', body('node-A', 'acct-A'))+ scopedRecord('acct-B', 'thr-1', body('node-B', 'acct-B'))+ // With the account id pinned, the scan never touches the other account — no ambiguity, binds B.+ expect(+ await resolve({ CODEX_THREAD_ID: 'thr-1', NODETERM_CODEX_ACCOUNT_ID: 'acct-B' })+ ).toBe(`node-B|${dir}/hook-endpoint.env|1`)+ })++ it('resolves nothing for a daemon account id that could escape the mapping directory', async () => {+ scopedRecord('acct-A', 'thr-1', body('node-A', 'acct-A'))+ expect(+ await resolve({ CODEX_THREAD_ID: 'thr-1', NODETERM_CODEX_ACCOUNT_ID: '../acct-A' })+ ).toBe('||')+ expect(+ await resolve({ CODEX_THREAD_ID: 'thr-1', NODETERM_CODEX_ACCOUNT_ID: 'system' })+ ).toBe('||')+ })+})diff --git a/src/core/codex-thread-identity-sh.ts b/src/core/codex-thread-identity-sh.tsindex a5e7d6f81..a2150ab45 100644--- a/src/core/codex-thread-identity-sh.ts+++ b/src/core/codex-thread-identity-sh.ts@@ -12,10 +12,20 @@* `codex-identity-proxy.ts`; this prelude cannot verify that signature (no key in an agent's* shell), which is why the charset re-validation below is not redundant.*- * Inert for every other agent: without `CODEX_THREAD_ID` the whole block is skipped, and the- * generated script is otherwise byte-identical to what it was. That is intentional — the prelude- * is prepended once in `buildManagedScript`, so it lands in claude/gemini/codex/grok/opencode- * scripts alike rather than in a codex-only fork of the builder.+ * ACCOUNT SCOPING (S6): a SYSTEM record lives at the bare root (`<root>/<threadId>`) and a MANAGED+ * record under `<root>/<accountId>/<threadId>`. This prelude reads `NODETERM_CODEX_ACCOUNT_ID` from+ * the daemon's env to pick the scope:+ * - a known, safe account id ⇒ read ONLY that account's record, and require the record's+ * `accountId=` line to agree with the daemon scope;+ * - an EMPTY account id (the classic shared tool shell that knows only a bare thread id) ⇒ scan+ * EVERY scope (bare-root system + each managed subdir) and bind ONLY when exactly one candidate+ * matches (`nt_codex_matches -eq 1`). Two accounts holding the same thread id ⇒ ambiguous ⇒+ * change nothing (Property 3 / Constraint 8, the same fail-closed posture as the TypeScript+ * `resolveCodexThreadNodeIdentity`).+ *+ * Inert for every other agent: without `CODEX_THREAD_ID` the whole block is skipped. A machine with+ * no managed accounts has no subdirs, so the scan reduces to the one bare-root read S4 did — the+ * legacy layout keeps resolving byte-for-byte (Constraint 12).** Deliberately free of Node/Electron imports beyond the path it is given: the generated-script* cores are shared by the desktop and the Server Edition.@@ -23,36 +33,78 @@import { posixQuote } from '../shared/ssh'/**+ * PROBE U5 (Codex 0.146.0, 2026-08-19 — docs/superpowers/probes/2026-08-codex-tool-shell-env.md):+ * a shared `app-server`-spawned tool shell carries `CODEX_THREAD_ID` (measured — a fresh id per+ * thread) and NOT the per-pane `NODETERM_*` (the tool shell forks from the shared daemon, not the+ * connecting client; corroborated by the deployed S4 resolver that depends on exactly this). The+ * premise HOLDS, so the account-scoped scan below is warranted. Caveat: an in-process app-server+ * (`codex exec`) leaks `NODETERM_*`, but the outer `[ -z "$NODETERM_NODE_ID" ]` guard no-ops there.+ ** @param identityRoot absolute path of the thread → node record directory* (`codexThreadIdentityRoot()`, i.e. under `CorePlatform.userDataDir` — NOT `~`).*/export function codexThreadIdentityResolverSh(identityRoot: string): string {return `# A shared-app-server Codex tool shell inherits CODEX_THREAD_ID, not the TUI client's-# NODETERM_* env. Recover this thread's exact node binding, or change nothing at all.+# NODETERM_* env. Recover this thread's exact node binding (account-scoped), or change nothing.if [ -z "\${NODETERM_NODE_ID-}" ] && [ -n "\${CODEX_THREAD_ID-}" ]; thencase "$CODEX_THREAD_ID" in# '.' and '..' MATCH the charset and are path segments: "$identityRoot"/.. is the record dir's# PARENT. Refused by name here for the same reason isSafeThreadId refuses them in TypeScript.''|.|..|*[!A-Za-z0-9._-]*) ;;*)- nt_codex_map=${posixQuote(identityRoot)}/"$CODEX_THREAD_ID"- if [ -r "$nt_codex_map" ]; then- nt_codex_node=$(sed -n 's/^nodeId=//p' "$nt_codex_map" | head -n 1)- nt_codex_endpoint=$(sed -n 's/^endpoint=//p' "$nt_codex_map" | head -n 1)- case "$nt_codex_node" in ''|*[!A-Za-z0-9._-]*) nt_codex_node='' ;; esac- case "$nt_codex_endpoint" in /*) ;; *) nt_codex_endpoint='' ;; esac- if [ -n "$nt_codex_endpoint" ] &&- [ "$(printf %s "$nt_codex_endpoint" | tr -cd 'A-Za-z0-9._/ -')" != "$nt_codex_endpoint" ]- then- nt_codex_endpoint=''- fi- if [ -n "$nt_codex_node" ] && [ -n "$nt_codex_endpoint" ]; then- NODETERM_NODE_ID="$nt_codex_node"- NODETERM_HOOK_ENDPOINT="$nt_codex_endpoint"- NODETERM_AGENT_ID=codex- NODETERM_CANVAS_CONTROL=1- export NODETERM_NODE_ID NODETERM_HOOK_ENDPOINT NODETERM_AGENT_ID NODETERM_CANVAS_CONTROL- fi+ nt_codex_root=${posixQuote(identityRoot)}+ nt_codex_matches=0+ nt_codex_node=''+ nt_codex_endpoint=''+ # Validate one candidate record file for an expected scope, parsed as DATA. On success it+ # records the node/endpoint and counts the match. $1=file, $2=expected scope ('' = system).+ nt_codex_try() {+ [ -r "$1" ] || return 0+ nt_a=$(sed -n 's/^accountId=//p' "$1" | head -n 1)+ # The record's own account line must AGREE with the directory it was found in. A system+ # record's line is empty or the reserved word 'system'; a managed record's line is its id.+ case "$2" in+ '') case "$nt_a" in ''|system) ;; *) return 0 ;; esac ;;+ *) [ "$nt_a" = "$2" ] || return 0 ;;+ esac+ nt_n=$(sed -n 's/^nodeId=//p' "$1" | head -n 1)+ nt_e=$(sed -n 's/^endpoint=//p' "$1" | head -n 1)+ case "$nt_n" in ''|*[!A-Za-z0-9._-]*) return 0 ;; esac+ case "$nt_e" in /*) ;; *) return 0 ;; esac+ [ "$(printf %s "$nt_e" | tr -cd 'A-Za-z0-9._/ -')" = "$nt_e" ] || return 0+ nt_codex_node=$nt_n+ nt_codex_endpoint=$nt_e+ nt_codex_matches=$((nt_codex_matches + 1))+ }+ case "\${NODETERM_CODEX_ACCOUNT_ID-}" in+ '')+ # Unknown account: scan every scope, bind only if exactly one candidate matches.+ nt_codex_try "$nt_codex_root/$CODEX_THREAD_ID" ''+ for nt_codex_dir in "$nt_codex_root"/*/; do+ [ -d "$nt_codex_dir" ] || continue+ nt_codex_scope=\${nt_codex_dir%/}+ nt_codex_scope=\${nt_codex_scope##*/}+ case "$nt_codex_scope" in+ ''|.|..|system|*[!A-Za-z0-9._-]*) continue ;;+ esac+ nt_codex_try "$nt_codex_dir$CODEX_THREAD_ID" "$nt_codex_scope"+ done+ ;;+ # A daemon scope that could escape the mapping directory, or the reserved system word used+ # as a managed id: resolve nothing.+ .|..|system|*[!A-Za-z0-9._-]*) ;;+ *)+ nt_codex_try "$nt_codex_root/\${NODETERM_CODEX_ACCOUNT_ID}/$CODEX_THREAD_ID" \\+ "$NODETERM_CODEX_ACCOUNT_ID"+ ;;+ esac+ if [ "$nt_codex_matches" -eq 1 ] && [ -n "$nt_codex_node" ] && [ -n "$nt_codex_endpoint" ]+ then+ NODETERM_NODE_ID="$nt_codex_node"+ NODETERM_HOOK_ENDPOINT="$nt_codex_endpoint"+ NODETERM_AGENT_ID=codex+ NODETERM_CANVAS_CONTROL=1+ export NODETERM_NODE_ID NODETERM_HOOK_ENDPOINT NODETERM_AGENT_ID NODETERM_CANVAS_CONTROLfi;;esac