Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Keep starting Codex processes on their own conversations (#945)

Dee61803a403f8e
diff --git a/cli/e2e/harness/daemon.ts b/cli/e2e/harness/daemon.ts
index 8b21fb48d..53f512633 100644
--- a/cli/e2e/harness/daemon.ts
+++ b/cli/e2e/harness/daemon.ts
@@ -39,6 +39,8 @@ export interface EngineConfig {
version?: string
without?: string[]
startDelayMs?: number
+ /** Hold startup before the transcript opens until this disposable path's .release file exists. */
+ startupGate?: string
firstHookDelayMs?: number
updateAvailable?: string
/** Ask whether to trust a folder the engine's own config has no answer for, as the real CLIs do. */
diff --git a/cli/e2e/harness/fakeEngine.mjs b/cli/e2e/harness/fakeEngine.mjs
index 9c93d8aa1..354cc30c8 100644
--- a/cli/e2e/harness/fakeEngine.mjs
+++ b/cli/e2e/harness/fakeEngine.mjs
@@ -171,6 +171,17 @@ export async function run(engine, config = {}, { native = false } = {}) {
process.stdin.setRawMode?.(true)
// Slow to start, as an engine is on its first run after an update: the process is there and holds
// the terminal, and nothing is drawn, announced or read until it is ready.
+ // October 6 sibling-conversation incident: hold startup before this process has a transcript, while
+ // another process in the same folder writes its own. Only the disposable fixture can release it.
+ if (config.startupGate) {
+ if (!within(config.root, config.startupGate)) throw new Error('startup gate outside its throwaway home')
+ writeFileSync(`${config.startupGate}.waiting`, String(process.pid))
+ const deadline = performance.now() + 60_000
+ while (!existsSync(`${config.startupGate}.release`)) {
+ if (performance.now() > deadline) throw new Error('startup gate was not released')
+ await new Promise((resolve) => setTimeout(resolve, 10))
+ }
+ }
if (config.startDelayMs) await new Promise((resolve) => setTimeout(resolve, config.startDelayMs))
// A newer release out (`updateAvailable`): Codex 0.160 asks first, before its session starts
// (update_prompt.rs, snapshot `update_prompt_modal`). It drops a paste; Enter takes the highlighted row,
diff --git a/cli/e2e/races.e2e.ts b/cli/e2e/races.e2e.ts
index 5f950c919..fae923596 100644
--- a/cli/e2e/races.e2e.ts
+++ b/cli/e2e/races.e2e.ts
@@ -4,12 +4,13 @@
* message is being written, and two agents created in one folder at once. Whatever order they land in,
* every request is answered, an agent ends with one engine in one pane, and it takes the next message.
*/
-import { mkdirSync, renameSync, writeFileSync } from 'node:fs'
+import { existsSync, mkdirSync, renameSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { pathToFileURL } from 'node:url'
import { afterEach, describe, expect, it, onTestFailed } from 'vitest'
import { LocalClient, type Frame } from './harness/client.js'
import { CLI_ROOT, IsolatedDaemon, until } from './harness/daemon.js'
+import { isolatedTmux } from '../src/testing/isolatedTmux.js'
type Engine = 'claude' | 'codex'
const engines: Engine[] = ['claude', 'codex']
@@ -198,6 +199,48 @@ describe('lifecycle requests that race', () => {
client.close()
})
+ it('codex: a process waiting for its own transcript never adopts a sibling conversation', async () => {
+ const d = await fresh()
+ const client = await LocalClient.connect(d)
+ const cwd = await folder(d, 'pending-codex-transcript')
+ const gate = join(d.root, 'codex-startup')
+ const sibling = await isolatedTmux(d.env)
+ try {
+ install(d, 'codex', { startupGate: gate })
+ const created = await client.request('agent_create', { engine: 'codex', cwd, bypassPermission: true }, 90_000)
+ expect(created.error, JSON.stringify(created)).toBeUndefined()
+ await until('Codex to wait before opening its transcript', () => existsSync(`${gate}.waiting`))
+
+ // A second real process writes into the same profile/folder. Its separate private tmux server
+ // and late startup hook keep it undiscovered, just like a Codex started in another terminal.
+ // Its transcript is born AFTER our waiting process: the old directory fallback picks it.
+ install(d, 'codex', { firstHookDelayMs: 60_000 })
+ await sibling.run('new-session', '-d', '-s', 'sibling', '-c', cwd, join(d.root, 'bin', 'codex'))
+ const siblingId = await until('the sibling to write its conversation', async () =>
+ /session ([\da-f-]{36})/.exec(await sibling.run('capture-pane', '-p', '-t', 'sibling'))?.[1])
+
+ // More than two normal discovery intervals, with the process still held before any hook or
+ // transcript of its own. Check every published observation, including a temporary wrong bind.
+ const untilScanned = Date.now() + 12_000
+ while (Date.now() < untilScanned) {
+ const pending = await row(client, created.agent.id)
+ expect(pending?.sessionId, JSON.stringify(pending)).toBeFalsy()
+ await new Promise((resolve) => setTimeout(resolve, 100))
+ }
+ writeFileSync(`${gate}.release`, '')
+ const own = await active(client, created.agent.id)
+ expect(own.sessionId).not.toBe(siblingId)
+ const screen = await d.capture(own.tmuxPane)
+ expect(screen).toContain(`session ${own.sessionId}`)
+ await turn(client, created.agent.id, 'only my own conversation')
+ expect((await row(client, created.agent.id))?.sessionId).toBe(own.sessionId)
+ } finally {
+ writeFileSync(`${gate}.release`, '')
+ client.close()
+ await sibling.close()
+ }
+ })
+
it('two agents created in one folder at once are two agents, each with its own conversation', async () => {
const d = await fresh()
const client = await LocalClient.connect(d)
diff --git a/cli/src/lib/sessionRepair.spec.ts b/cli/src/lib/sessionRepair.spec.ts
index 8b2616a8c..ed8eb6f3f 100644
--- a/cli/src/lib/sessionRepair.spec.ts
+++ b/cli/src/lib/sessionRepair.spec.ts
@@ -499,6 +499,47 @@ describe('session repair — Codex profiles', () => {
})
describe('session repair — a Codex process names its own rollout', () => {
+ it('reads the native child of the npm launcher, never another launcher or a nested tool', async () => {
+ const { codexProcessFiles } = await import('./sessionRepair.js')
+ const own = '/tmp/profile/sessions/rollout-own.jsonl'
+ const sibling = '/tmp/profile/sessions/rollout-sibling.jsonl'
+ const files = vi.fn(async (pid: number) => pid === 43 ? [own] : pid === 50 ? [sibling] : ['/dev/null'])
+ const rows = [
+ { pid: 42, parentPid: 1, executable: '/usr/bin/node', args: 'node /opt/codex/bin/codex.js' },
+ { pid: 43, parentPid: 42, executable: '/opt/vendor/codex', args: '/opt/vendor/codex' },
+ { pid: 50, parentPid: 1, executable: '/opt/vendor/codex', args: '/opt/vendor/codex' },
+ { pid: 51, parentPid: 43, executable: '/opt/vendor/codex', args: '/opt/vendor/codex' },
+ ]
+ await expect(codexProcessFiles(42, files, async () => rows)).resolves.toEqual(['/dev/null', own])
+ expect(files.mock.calls.map(([pid]) => pid)).toEqual([42, 43])
+ // macOS's comm column can truncate the full Node path; argv still names the executable.
+ await expect(codexProcessFiles(42, files, async () => rows.map(row => row.pid === 42
+ ? { ...row, executable: '/Users/demo/.ha', args: '/Users/demo/.harness/node/bin/node /tmp/bin/codex' } : row)))
+ .resolves.toEqual(['/dev/null', own])
+ await expect(codexProcessFiles(42, files, async () => null)).resolves.toEqual(['/dev/null'])
+ await expect(codexProcessFiles(42, files, async () => [])).resolves.toEqual(['/dev/null'])
+ await expect(codexProcessFiles(42, files, async () => rows.filter(row => row.pid !== 43))).resolves.toEqual(['/dev/null'])
+ await expect(codexProcessFiles(42, files, async () => [...rows, { ...rows[1], pid: 44 }])).resolves.toEqual(['/dev/null'])
+ // A native process owns its file directly. No walk into the tools it launched.
+ const table = vi.fn(async () => rows)
+ await expect(codexProcessFiles(43, files, table)).resolves.toEqual([own])
+ expect(table).not.toHaveBeenCalled()
+ await expect(codexProcessFiles(51, files, table)).resolves.toEqual(['/dev/null'])
+ })
+
+ it('does not give a starting process the only sibling rollout before its own file opens', async () => {
+ const profile = tempRoot()
+ const sibling = 'a1b2c3d4-1111-4a4a-8a8a-000000000009'
+ writeCodexRollout(profile, sibling, CWD, Date.now())
+ vi.resetModules()
+ const { codexProcessSession, findLiveSession } = await import('./sessionRepair.js')
+ // October 6 full E2E: two Codex processes start together, but only one has written
+ // its rollout yet. This process holds neither: a directory match is not ownership.
+ await expect(codexProcessSession(process.pid, join(profile, 'sessions'), CWD)).resolves.toBeNull()
+ await expect(findLiveSession('codex', CWD, Date.now() - 1_000,
+ { codexHome: profile, bornOnly: true, pid: process.pid })).resolves.toBeNull()
+ })
+
it('names a fork among its siblings by the rollout its process holds open, where a scan must refuse', async () => {
const profile = tempRoot()
const fork = 'a1b2c3d4-1111-4a4a-8a8a-000000000003'
diff --git a/cli/src/lib/sessionRepair.ts b/cli/src/lib/sessionRepair.ts
index 355504c07..bc4cfc013 100644
--- a/cli/src/lib/sessionRepair.ts
+++ b/cli/src/lib/sessionRepair.ts
@@ -2,8 +2,8 @@
* Find the session a live discovered engine process is running, when no hook has bound one yet.
*
* Process discovery can see an engine immediately, including after a daemon restart, but a session id is
- * owned by the engine and may not be present in argv. Ask the engine's own store which session belongs to
- * that pane's directory and process start time.
+ * owned by the engine and may not be present in argv. Prefer the engine's process evidence; when no
+ * exact process lookup applies, narrow the engine's store by directory and process start time.
*
* Two rules keep it honest:
* - **Started after the engine process did.** A session older than the process cannot be the one it is
@@ -28,6 +28,7 @@ import { sqlitePreflightMessage } from './sqliteAvailability.js'
import { sqliteReadAll, type SqliteParam } from './sqliteRead.js'
import { piSessionFolder, readPiHead } from './sessionSearch/externals/pi.js'
import { claudeProjectsRoots, codexHomeRoots } from './engineHomes.js'
+import { argvTokens, engineProcessMatchScore, processRows, type ProcessRow } from './tmux.js'
/**
* Clock granularity only. `ps` reports start time to the second, so a file created in the same second
@@ -304,13 +305,15 @@ export async function findLiveSession(
// The agent's own profile alone, else the daemon's home and every one the person moved, as
// findResumedTranscript looks: the default alone missed a rollout in a moved CODEX_HOME.
const sessions = (opts?.codexHome ? [opts.codexHome] : codexHomeRoots(env.CODEX_HOME)).map((home) => join(home, 'sessions'))
- // Exact first, as for Claude: the rollout the process holds open (see `codexProcessSession`).
- const exact = opts?.pid ? await codexProcessSession(opts.pid, sessions, cwd) : null
+ // October 6 parallel-create incident: the only rollout in a folder can belong to a sibling
+ // whose file opened first. A known Codex process must name its own open rollout; until then,
+ // leave it unbound for its next discovery pass or startup hook, never guess from the folder.
+ if (opts?.pid) return codexProcessSession(opts.pid, sessions, cwd)
// Codex writes no `cwd` on line one; its rollout meta carries it — and says whether the rollout
// belongs to a subagent, which must never become an agent of its own.
// Its session id lives INSIDE the file: the name is `rollout-<timestamp>-<id>.jsonl`, so deriving
// the id from the filename produced the literal string "rollout-…" (seen on a live pane).
- return exact ?? fileEngineSession(sessions, cwd, startedAtMs, async (path) => {
+ return fileEngineSession(sessions, cwd, startedAtMs, async (path) => {
const meta = readCodexRolloutMeta(path)
return meta && !meta.isSubagent ? { cwd: meta.cwd, sessionId: meta.id || undefined } : null
}, opts)
@@ -640,6 +643,24 @@ export async function openFiles(pid: number): Promise<string[]> {
return stdout.split('\n').filter((line) => line.startsWith('n/')).map((line) => line.slice(1))
}
+/** The rollout may be held by the native child of npm's Node launcher, not the launcher itself.
+ * October 6 ownership E2E: removing the folder guess exposed that distinction for a manually started
+ * Codex with no hook. Read only one direct Codex child of a Node launcher, never a nested tool. */
+export async function codexProcessFiles(
+ pid: number,
+ files: typeof openFiles = openFiles,
+ processes: () => Promise<readonly Pick<ProcessRow, 'pid' | 'parentPid' | 'executable' | 'args'>[] | null> = processRows,
+): Promise<string[]> {
+ const own = await files(pid)
+ if (own.some(path => /rollout-[^/]*\.jsonl$/.test(path))) return own
+ const rows = await processes()
+ const wrapper = rows?.find(row => row.pid === pid)
+ if (!wrapper || ![wrapper.executable, argvTokens(wrapper.args)[0] ?? '']
+ .some(path => /^node(?:js)?$/.test(basename(path)))) return own
+ const children = rows!.filter(row => row.parentPid === pid && engineProcessMatchScore(row, 'codex') > 0)
+ return children.length === 1 ? [...own, ...await files(children[0].pid)] : own
+}
+
/**
* The conversation a Codex process is writing: the rollout it holds open.
*
@@ -652,7 +673,7 @@ export async function codexProcessSession(
pid: number,
sessionsRoot: string | string[],
cwd: string,
- files: (pid: number) => Promise<string[]> = openFiles,
+ files: (pid: number) => Promise<string[]> = codexProcessFiles,
): Promise<RepairedSession | null> {
const roots = await Promise.all((typeof sessionsRoot === 'string' ? [sessionsRoot] : sessionsRoot)
.map((one) => realpath(one).catch(() => one)))