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): relay root before first use + distinct wording for a malformed id

Claude Code59be08f0df63
diff --git a/src/core/codex-identity-proxy.test.ts b/src/core/codex-identity-proxy.test.ts
index 3ab82b97d..28e8a5879 100644
--- a/src/core/codex-identity-proxy.test.ts
+++ b/src/core/codex-identity-proxy.test.ts
@@ -307,7 +307,12 @@ describe('account-scoped ownership', () => {
path.join(recordsRoot, 'acct-B', 'thread-1')
)
expect(readCodexThreadIdentity('thread-1', recordsRoot, 'acct-A')?.nodeId).toBe('node-1')
- // MUTATION TARGET: drop the line-vs-directory agreement check ⇒ acct-B honours it and reddens.
+ // The scope-bound HMAC is what actually carries this: the record was signed under scope
+ // `acct-A`, so recomputing the expected signature under scope `acct-B` mismatches and the record
+ // is rejected regardless. The explicit accountId-line-vs-directory agreement check is
+ // defence-in-depth layered on top (a clearer refusal, and belt-and-braces if the preimage ever
+ // changed). So this asserts the OUTCOME — acct-B never honours acct-A's record — not that the
+ // line check alone is load-bearing (carried PR-2 minor: the earlier comment overstated that).
expect(readCodexThreadIdentity('thread-1', recordsRoot, 'acct-B')).toBeUndefined()
})
diff --git a/src/main/codex-relay-daemon.test.ts b/src/main/codex-relay-daemon.test.ts
index d143aefd2..c178a572d 100644
--- a/src/main/codex-relay-daemon.test.ts
+++ b/src/main/codex-relay-daemon.test.ts
@@ -17,12 +17,14 @@ import path from 'path'
import { WebSocketServer } from 'ws'
import {
acquireProcessLock,
+ ensureCodexRelayRoot,
exposeForeignThread,
listThreadsAt,
mergeRelayThreadLists,
readThreadAt,
relayControlPost,
relayThreadReservationKey,
+ relayThreadResponseError,
resolveForeignThreadAt,
retargetRelayResumeByPath,
resolveRelayThreadResponse,
@@ -144,6 +146,39 @@ describe('Codex shared relay thread observation', () => {
})).toEqual({ ok: false, unexpectedThreadId: 'forked' })
})
+ it('labels a changed id as -32004 but a malformed id as a distinct error (PR-4 minor)', () => {
+ // The silent-fork case the guard exists for keeps the -32004 "changed the conversation id"
+ // wording...
+ expect(relayThreadResponseError({ ok: false, unexpectedThreadId: 'forked' })).toEqual({
+ code: -32004,
+ message: 'Codex changed the conversation id during account switch'
+ })
+ // ...while a plain malformed/missing id (no unexpectedThreadId) must NOT claim the id changed.
+ // MUTATION: collapse both branches back to the -32004 message ⇒ this reddens.
+ const malformed = relayThreadResponseError({ ok: false })
+ expect(malformed?.code).toBe(-32603)
+ expect(malformed?.message).not.toMatch(/changed the conversation id/)
+ // A successful observation carries no error at all.
+ expect(relayThreadResponseError({ ok: true, threadId: 'x' })).toBeNull()
+ })
+
+ it('ensureCodexRelayRoot creates the relay root 0o700 and is idempotent (PR-4 obligation)', () => {
+ const base = mkdtempSync(path.join(tmpdir(), 'nt-relay-root-'))
+ const root = path.join(base, 'nested', '.nodeterm')
+ try {
+ expect(existsSync(root)).toBe(false)
+ ensureCodexRelayRoot(root)
+ const st = statSync(root)
+ expect(st.isDirectory()).toBe(true)
+ // The private-state root must be owner-only (it holds the 0600 control-token state file).
+ if (process.platform !== 'win32') expect(st.mode & 0o777).toBe(0o700)
+ // Idempotent: a second call over an existing root does not throw.
+ expect(() => ensureCodexRelayRoot(root)).not.toThrow()
+ } finally {
+ rmSync(base, { recursive: true, force: true })
+ }
+ })
+
it('keeps two account catalogs visible while preserving the selected account as duplicate owner', () => {
const current = '/tmp/current/app-server-control.sock'
const foreign = '/tmp/foreign/app-server-control.sock'
diff --git a/src/main/codex-relay-daemon.ts b/src/main/codex-relay-daemon.ts
index 1b24aeecc..9c206b329 100644
--- a/src/main/codex-relay-daemon.ts
+++ b/src/main/codex-relay-daemon.ts
@@ -161,6 +161,25 @@ export function resolveRelayThreadResponse(
}
}
+/**
+ * The JSON-RPC error a rewritten relay response carries when the observation failed, or `null` when
+ * it succeeded. Two DISTINCT failures reach `resolveRelayThreadResponse` as `{ ok: false }`, and
+ * before this they were both rewritten to the SAME "Codex changed the conversation id" message:
+ * - `unexpectedThreadId` set — a `thread/resume` came back under a DIFFERENT id: the silent-fork
+ * case the `-32004` guard exists for (§4.2 step 5 / Property 5).
+ * - `unexpectedThreadId` UNSET — the response's thread id was missing or malformed: NOT an id
+ * change. Telling the operator "the id changed" here is wrong, so it gets a distinct internal
+ * error wording (carried PR-4 minor: tighten the -32004 wording).
+ */
+export function relayThreadResponseError(
+ observed: RelayThreadResponse
+): { code: number; message: string } | null {
+ if (observed.ok) return null
+ return observed.unexpectedThreadId !== undefined
+ ? { code: -32004, message: 'Codex changed the conversation id during account switch' }
+ : { code: -32603, message: 'Codex returned a malformed conversation id during account switch' }
+}
+
type RelayThread = Record<string, any> & {
id: string
path?: string | null
@@ -468,7 +487,21 @@ export function relayControlPost(
})
}
+/**
+ * Create the `~/.nodeterm` relay root, mode `0o700`, idempotently. `serve()` already makes it, but
+ * that runs in the DETACHED daemon child — a caller reaching for the relay from the app process
+ * (the state file, the process lock, `ensureServer`) needs the directory to exist first. Only
+ * `serve()` created it before S6 PR 5, so `ensureServer` / any first relay use in the app process
+ * could race a missing root (carried PR-4 obligation). Exported so the main-side account wiring can
+ * ensure it at boot too. `target` is injectable so a test never touches the real home.
+ */
+export function ensureCodexRelayRoot(target: string = root): void {
+ mkdirSync(target, { recursive: true, mode: 0o700 })
+}
+
async function ensureServer(): Promise<State> {
+ // The root must exist before we read the state file or take the process lock — both live under it.
+ ensureCodexRelayRoot()
const current = readState()
if (current) {
try {
@@ -1405,14 +1438,10 @@ function serve(): void {
}
const observed = resolveRelayThreadResponse(pending, message)
if (observed && !observed.ok) {
+ // Distinguish the silent-fork case (-32004) from a plain malformed-id response — see
+ // relayThreadResponseError. Both used to be labelled "changed the conversation id".
outbound = Buffer.from(
- JSON.stringify({
- id: message.id,
- error: {
- code: -32004,
- message: 'Codex changed the conversation id during account switch'
- }
- })
+ JSON.stringify({ id: message.id, error: relayThreadResponseError(observed) })
)
}
const committed = observed?.ok