From 46764fe9060661f156b30f6e5a764aa57e61f9b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Val=20Alexander=20=F0=9F=91=91?= Date: Sat, 14 Nov 2026 02:21:21 -0600 Subject: [PATCH 1/5] fix: retain Codex ownership through input transport failure Handle stdin errors and write receipts without an uncaught exception, fence replacement and turn completion until closure, and preserve uncertain answer delivery. --- apps/desktop/e2e/codex-transport.spec.ts | 52 +++++ .../desktop/src/main/engine/backends/codex.ts | 112 +++++++++-- apps/desktop/test/codex-transport.test.ts | 179 ++++++++++++++++++ .../test/human-requests-runner.test.ts | 33 ++++ 4 files changed, 359 insertions(+), 17 deletions(-) create mode 100644 apps/desktop/e2e/codex-transport.spec.ts create mode 100644 apps/desktop/test/codex-transport.test.ts diff --git a/apps/desktop/e2e/codex-transport.spec.ts b/apps/desktop/e2e/codex-transport.spec.ts new file mode 100644 index 00000000..1d69036f --- /dev/null +++ b/apps/desktop/e2e/codex-transport.spec.ts @@ -0,0 +1,52 @@ +import { test, expect } from "@playwright/test"; +import fs from "node:fs"; +import path from "node:path"; +import { launch, seedHome, tid } from "./support.js"; +import { observeElectronErrors, recordElectronStderr } from "../test/support/electron-errors.js"; + +test("a Codex input pipe failure leaves Settings responsive and reports model discovery failure", async () => { + const { home } = seedHome({ default_backend: "codex" }); + const statePath = path.join(home, "app/state.json"), state = JSON.parse(fs.readFileSync(statePath, "utf8")); + const cli = path.join(home, "closed-input-codex.cjs"), calls = path.join(home, "codex-methods.jsonl"); + state.settings.codex_bin = cli; + for (const family of ["claude", "opencode", "mimo"]) state.settings[`${family}_bin`] = path.join(home, `missing-${family}`); + fs.writeFileSync(statePath, JSON.stringify(state)); + fs.writeFileSync(cli, `#!${process.execPath} +const fs = require('node:fs'); +if (process.argv.includes('--version')) { console.log('codex-cli 0.164.0'); process.exit(0); } +if (!process.argv.includes('app-server')) process.exit(2); +let line = ''; const byte = Buffer.alloc(1); +while (!line.endsWith('\\n')) { + try { if (!fs.readSync(0, byte, 0, 1, null)) process.exit(0); line += byte.toString(); } + catch (error) { if (error.code !== 'EAGAIN') throw error; Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1); } +} +const request = JSON.parse(line); +fs.appendFileSync(${JSON.stringify(calls)}, JSON.stringify({method:request.method})+'\\n'); +fs.closeSync(0); +console.log(JSON.stringify({id:request.id,result:{userAgent:'synthetic-closed-input'}})); +setInterval(() => {}, 1000); +`, { mode: 0o755 }); + const { app, page } = await launch(home); + const stderr = path.join(test.info().outputDir, `electron-${app.process().pid}.stderr.log`); + recordElectronStderr(app.process(), test.info().outputDir); + await app.evaluate(observeElectronErrors); + try { + const result = await page.evaluate(() => window.modex!.invoke("models:list", { backend: "codex" })); + expect(result.models).toEqual([]); + expect(result.error).toContain("input transport failed"); + await tid(page, "open-settings").click(); + await expect(tid(page, "settings")).toBeVisible(); + await page.getByRole("button", { name: "Coding CLIs", exact: true }).click(); + await expect(page.getByText(/Account status unavailable · check in Codex CLI/)).toBeVisible(); + await page.getByRole("button", { name: "General", exact: true }).click(); + await page.screenshot({ path: test.info().outputPath("settings-after-input-failure.png"), animations: "disabled" }); + await tid(page, "settings").getByRole("button", { name: "Cancel", exact: true }).click(); + await expect(tid(page, "settings")).toHaveCount(0); + await tid(page, "open-settings").click(); + await expect(tid(page, "settings")).toBeVisible(); + const methods = fs.readFileSync(calls, "utf8").trim().split("\n").map(line => JSON.parse(line).method); + expect(methods.length).toBeGreaterThan(0); + expect(methods.every(method => method === "initialize")).toBe(true); + } finally { await app.close(); } + expect(fs.readFileSync(stderr, "utf8")).not.toContain("MODEX_E2E_MAIN_EXCEPTION"); +}); diff --git a/apps/desktop/src/main/engine/backends/codex.ts b/apps/desktop/src/main/engine/backends/codex.ts index 8027937e..fe0c6a7d 100644 --- a/apps/desktop/src/main/engine/backends/codex.ts +++ b/apps/desktop/src/main/engine/backends/codex.ts @@ -55,6 +55,12 @@ export class CodexBackend implements Backend { * here means Codex settled it itself (codex-cli 0.163.0): the card is withdrawn and no answer is sent. */ private readonly openRequests = new Map(); + /** A broken input pipe cannot release the generation until its owned process actually closes. */ + private inputFailure: { child: ChildProcess; error: CodexError; closed: Promise } | null = null; + private readonly closures = new WeakMap>(); + private readonly deliveries = new WeakMap>>(); + private readonly writeReceipts = new WeakMap void>>(); + /** * How long a turn stays open after Codex's final answer with nothing else in progress, when Codex @@ -76,6 +82,7 @@ export class CodexBackend implements Backend { local?: { model: string; provider: string; externalSandbox?: true; admit?: (reads: { config: unknown; requirements: unknown }) => void } }) {} private ensure(): Promise { + if (this.inputFailure) return Promise.reject(this.inputFailure.error); if (this.ready && this.child && this.child.exitCode === null) return this.ready; this.ready = new Promise((resolve, reject) => { let child: ChildProcess; @@ -88,6 +95,8 @@ export class CodexBackend implements Backend { return reject(new CodexError(`could not start ${bin} app-server: ${(err as Error).message}`, { bin, spawnError: (err as Error).message })); } this.child = child; + let closed!: () => void; + this.closures.set(child, new Promise(resolve => { closed = resolve; })); this.generation = randomUUID(); this.loaded.clear(); this.metadataConnections.clear(); @@ -99,20 +108,36 @@ export class CodexBackend implements Backend { // Redact across chunks before the bound can discard a token's beginning. stderr = (this.configuration?.redact(output) ?? output).slice(-16_384); }); - child.stdout?.on("data", (d: Buffer) => { if (this.child === child) lines.push(d, (line) => this.dispatch(line)); }); + child.stdout?.on("data", (d: Buffer) => { if (this.child === child && this.inputFailure?.child !== child) lines.push(d, (line) => this.dispatch(line)); }); + // This is a separate emitter from ChildProcess.error. Retain the listener for the + // stream's whole lifetime so a superseded child's late error is still consumed. + child.stdin?.on("error", error => { this.inputFailed(child, error); }); + child.stdin?.on("close", () => { if (this.child === child && child.exitCode === null) this.inputFailed(child); }); child.on("error", (err) => { + if (this.inputFailure?.child === child) return; // A failed termination is not closure evidence. const error = new CodexError(`${bin}: ${err.message}. Is the Codex CLI installed and on PATH?`, { bin, spawnError: err.message, errno: (err as NodeJS.ErrnoException).code, pid: child.pid }); if (this.child === child) this.disconnect(error); reject(error); }); child.on("close", (code, signal) => { - const output = this.configuration?.redact(stderr) ?? stderr; - if (this.child === child) this.disconnect(new CodexError(`codex app-server exited (${code}) ${output.trim().split("\n").slice(-2).join(" ")}`, { exitCode: code, signal, stderr: stderrTail(output), pid: child.pid, userAgent: this.userAgent })); + const finalize = async () => { + const fault = this.inputFailure?.child === child ? this.inputFailure : undefined; + // Broker delivery persists sent/uncertain before request withdrawal or turn end. + if (fault) await Promise.allSettled([...(this.deliveries.get(child) ?? [])]); + try { + if (this.child === child) { + const output = this.configuration?.redact(stderr) ?? stderr; + this.inputFailure = null; + this.disconnect(fault?.error ?? new CodexError(`codex app-server exited (${code}) ${output.trim().split("\n").slice(-2).join(" ")}`, { exitCode: code, signal, stderr: stderrTail(output), pid: child.pid, userAgent: this.userAgent })); + } + } finally { closed(); } + }; + void finalize(); }); this.request<{ userAgent?: string }>("initialize", { clientInfo: { name: "modex", title: "Modex", version: process.env.MODEX_VERSION ?? "0.0.1" }, capabilities: { experimentalApi: true } }, 5000) - .then((r) => { + .then(async (r) => { this.userAgent = typeof r?.userAgent === "string" ? r.userAgent : null; - this.notify("initialized", {}); + await this.notify("initialized", {}); resolve(); }) .catch(reject); @@ -121,6 +146,7 @@ export class CodexBackend implements Backend { } private disconnect(error: Error): void { + if (this.child) for (const finish of this.writeReceipts.get(this.child) ?? []) finish(error); this.child = null; this.ready = null; this.loaded.clear(); @@ -214,14 +240,50 @@ export class CodexBackend implements Backend { return fields.length ? { displayOnlyInputs: true, unavailableInputs: fields } : {}; } - private send(o: unknown): void { - this.child?.stdin?.write(JSON.stringify(o) + "\n"); + /** Idempotent transport failure; closing the pipe or submitting SIGKILL is not process closure. */ + private inputFailed(child: ChildProcess, cause?: unknown): CodexError { + if (this.inputFailure?.child === child) return this.inputFailure.error; + const code = cause && typeof cause === "object" && "code" in cause ? cause.code : undefined; + const error = new CodexError("Codex app-server input transport failed.", { + stage: "stdin", pid: child.pid, ...(typeof code === "string" && /^[A-Z0-9_]{1,32}$/.test(code) ? { errno: code } : {}), + }); + if (this.child !== child) return error; // Consume old errors without touching the replacement. + this.inputFailure = { child, error, closed: this.closures.get(child)! }; + for (const finish of this.writeReceipts.get(child) ?? []) finish(error); + for (const pending of this.pending.values()) pending.reject(error); + this.pending.clear(); + void Promise.allSettled([...(this.deliveries.get(child) ?? [])]).then(() => { + if (this.inputFailure?.child !== child) return; + for (const request of this.openRequests.values()) request.controller.abort(); + }); + try { killGroup(child); } catch { /* Keep ownership until close even when termination fails. */ } + return error; + } + + private send(o: unknown, written?: (error?: Error) => void): void { + const child = this.child; + if (!child) { written?.(new CodexError("codex app-server is not running", {})); return; } + if (this.inputFailure) { written?.(this.inputFailure.error); return; } + const stdin = child.stdin; + if (!stdin?.writable || stdin.destroyed || stdin.writableEnded) { written?.(this.inputFailed(child)); return; } + let settled = false; + const receipts = written ? this.writeReceipts.get(child) ?? new Set<(error?: Error) => void>() : undefined; + const finish = (error?: Error) => { + if (settled) return; + settled = true; receipts?.delete(finish); written?.(error); + }; + if (receipts) { this.writeReceipts.set(child, receipts); receipts.add(finish); } + try { + stdin.write(JSON.stringify(o) + "\n", error => { + finish(error ? this.inputFailed(child, error) : this.inputFailure?.child === child ? this.inputFailure.error : undefined); + }); + } catch (error) { finish(this.inputFailed(child, error)); } } private request(method: string, params: unknown, timeoutMs?: number): Promise { - if (!this.child || this.child.exitCode !== null || !this.child.stdin?.writable) { - return Promise.reject(new CodexError("codex app-server is not running", { method })); - } + if (this.inputFailure) return Promise.reject(this.inputFailure.error); + if (!this.child || this.child.exitCode !== null) return Promise.reject(new CodexError("codex app-server is not running", { method })); + if (!this.child.stdin?.writable || this.child.stdin.destroyed || this.child.stdin.writableEnded) return Promise.reject(this.inputFailed(this.child)); const id = this.nextId++; return new Promise((resolve, reject) => { const timer = timeoutMs === undefined ? undefined : setTimeout(() => { @@ -237,8 +299,8 @@ export class CodexBackend implements Backend { }); } - private notify(method: string, params: unknown): void { - this.send({ method, params }); + private notify(method: string, params: unknown): Promise { + return new Promise((resolve, reject) => this.send({ method, params }, error => error ? reject(error) : resolve())); } private respond(id: unknown, result: unknown): void { @@ -318,12 +380,22 @@ export class CodexBackend implements Backend { private restart(reason: string): void { const child = this.child; if (!child) return; + if (this.inputFailure?.child === child) { + try { killGroup(child); } catch { /* A repeated Stop still needs closure. */ } + return; + } this.disconnect(new CodexError(reason, { pid: child.pid })); killGroup(child); } async dispose(): Promise { const child = this.child; + if (this.inputFailure && this.inputFailure.child === child) { + const fault = this.inputFailure; + try { killGroup(child!); } catch { /* Do not release a failed generation on signal submission. */ } + await fault.closed; + return; + } this.disconnect(new CodexError("codex app-server disposed", {})); if (child) await new Promise((resolve) => { child.once("close", () => resolve()); @@ -374,6 +446,8 @@ export class CodexBackend implements Backend { if (second.status !== "failed") return second; return { ...second, detail: { ...second.detail, firstAttempt: { error: first.error, ...first.detail } }, recovery: [`${step}: failed again`] }; } finally { + // Setup cancellation, RPC failure and timeouts cannot free this turn while its failed child lives. + if (this.inputFailure) await this.inputFailure.closed; this.activeTurns--; } } @@ -511,7 +585,7 @@ export class CodexBackend implements Backend { }; const onDisconnect = (error: Error) => finish(signal.aborted ? { status: "interrupted" } : { status: "failed", error: error.message, detail: detail(detailOf(error)) }); const finish = (r: TurnResult) => { - if (done) return; + if (done || this.inputFailure?.child === child) return; done = true; for (const key of this.changePaths.keys()) if (key.startsWith(`${tid}:`)) this.changePaths.delete(key); agents.finish(signal.aborted || r.status === "interrupted"); @@ -854,11 +928,15 @@ export class CodexBackend implements Backend { const deliver = async (result: unknown) => { const child = this.child; const write = () => { - if (withdrawn.aborted || this.generation !== native.runtime || !child || child !== this.child || child.exitCode !== null) return Promise.reject(new Error("request transport unavailable")); - return writeJsonResponse(child.stdin, { id: msg.id, result }); + if (withdrawn.aborted || this.generation !== native.runtime || !child || child !== this.child || child.exitCode !== null || this.inputFailure?.child === child) return Promise.reject(new Error("request transport unavailable")); + return writeJsonResponse(child.stdin, { id: msg.id, result }).catch(error => { this.inputFailed(child, error); throw error; }); }; - if (sink.deliverRequest) await sink.deliverRequest(native, write); - else await write(); + if (!child) return; + const receipts = this.deliveries.get(child) ?? new Set>(); + this.deliveries.set(child, receipts); + const delivery = (async () => { if (sink.deliverRequest) await sink.deliverRequest(native, write); else await write(); })(); + receipts.add(delivery); + try { await delivery; } finally { receipts.delete(delivery); } }; if (msg.method === "item/commandExecution/requestApproval") { const cmd = stripShell(String(p.command ?? "")); diff --git a/apps/desktop/test/codex-transport.test.ts b/apps/desktop/test/codex-transport.test.ts new file mode 100644 index 00000000..fddde775 --- /dev/null +++ b/apps/desktop/test/codex-transport.test.ts @@ -0,0 +1,179 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { spawnSync } from "node:child_process"; +import { setTimeout as delay } from "node:timers/promises"; +import { CodexBackend } from "../src/main/engine/backends/codex.js"; +import { FakeProcess, fakeSpawn, collectSink } from "./fakeproc.js"; + +const tick = () => new Promise(resolve => setImmediate(resolve)); +function wire(proc: FakeProcess): void { + let threads = 0; + proc.stdin.on("data", data => { + for (const line of data.toString().split("\n")) { + if (!line.trim()) continue; + const m = JSON.parse(line); + if (m.method === "initialize") proc.emitLine({ id: m.id, result: { userAgent: "synthetic-transport" } }); + else if (m.method === "model/list") proc.emitLine({ id: m.id, result: { data: [] } }); + else if (m.method === "thread/start") proc.emitLine({ id: m.id, result: { thread: { id: `thr-${++threads}` }, model: m.params.model, modelProvider: m.params.modelProvider } }); + else if (m.method === "turn/start") { + proc.emitLine({ id: m.id, result: { turn: { id: `tu-${m.params.threadId}` } } }); + proc.emitLine({ method: "turn/started", params: { threadId: m.params.threadId, turn: { id: `tu-${m.params.threadId}` } } }); + } + } + }); +} + +test("a failed initialize pipe settles before the RPC timeout", async () => { + const proc = new FakeProcess(); + proc.stdin.on("error", () => {}); // Test lifecycle separately; real-pipe controls detect uncaught errors. + proc.stdin._write = (_chunk, _encoding, callback) => callback(Object.assign(new Error("private-input-sentinel"), { code: "EPIPE" })); + const backend = new CodexBackend("fixture", fakeSpawn(proc).spawn); + const discovery = backend.listModels().then(() => "success", () => "failed"); + try { + assert.equal(await Promise.race([discovery, delay(1000).then(() => "pending")]), "failed"); + assert.ok(proc.killed); + } finally { await backend.dispose(); await discovery; } +}); + +test("late old-stream errors stay consumed after replacement startup", async () => { + const old = new FakeProcess(), current = new FakeProcess(); + wire(old); wire(current); + const backend = new CodexBackend("fixture", fakeSpawn([old, current]).spawn); + try { + assert.deepEqual(await backend.listModels(), []); + await backend.dispose(); + assert.deepEqual(await backend.listModels(), []); + assert.doesNotThrow(() => old.stdin.emit("error", Object.assign(new Error("late-private-sentinel"), { code: "EPIPE" }))); + assert.deepEqual(await backend.listModels(), []); + assert.equal(current.killed, false); + } finally { await backend.dispose(); } +}); + +test("Stop after pipe failure retains the active turn until the child closes", async () => { + const proc = new FakeProcess(); wire(proc); + proc.holdStdio = true; proc.ignore.add("SIGKILL"); proc.stdin.on("error", () => {}); + const backend = new CodexBackend("fixture", fakeSpawn(proc).spawn), control = new AbortController(); + let settled = false; + const running = backend.runTurn("synthetic fixture", { cwd: "/fixture", mode: "chat", plan: false, model: "" }, collectSink().sink, control.signal).then(result => { settled = true; return result; }); + try { + await proc.waitFor(line => line.includes('"turn/start"')); await tick(); + proc.stdin._write = (_chunk, _encoding, callback) => callback(Object.assign(new Error("write EPIPE"), { code: "EPIPE" })); + control.abort(); await tick(); + void backend.forceStop(); await tick(); + assert.equal(proc.exitCode, null); + assert.equal(settled, false, "signal submission cannot release turn ownership before closure"); + proc.ignore.clear(); proc.close(null, "SIGKILL"); proc.releaseStdio(); await running; + } finally { + proc.ignore.clear(); proc.close(null, "SIGKILL"); proc.releaseStdio(); await running; await backend.dispose(); + } +}); + +test("closing input before initialized's write callback settles setup", async () => { + const proc = new FakeProcess(); wire(proc); + const original = proc.stdin.write.bind(proc.stdin); + proc.stdin.write = ((chunk: unknown, ...args: unknown[]) => { + if (String(chunk).includes('"method":"initialized"')) { + setImmediate(() => proc.stdin.emit("close")); + return false; // A local callback is deliberately missing; closure must reject its receipt. + } + return Reflect.apply(original, proc.stdin, [chunk, ...args]); + }) as typeof proc.stdin.write; + const backend = new CodexBackend("fixture", fakeSpawn(proc).spawn); + const discovery = backend.listModels().then(() => "success", () => "failed"); + try { + assert.equal(await Promise.race([discovery, delay(1000).then(() => "pending")]), "failed"); + } finally { await backend.dispose(); } +}); + +test("setup cancellation cannot release a failed generation before closure", async () => { + const proc = new FakeProcess(); wire(proc); + const spawn = fakeSpawn(proc), backend = new CodexBackend("fixture", spawn.spawn); + await backend.listModels(); + proc.holdStdio = true; proc.ignore.add("SIGKILL"); + proc.stdin._write = (_chunk, _encoding, callback) => callback(Object.assign(new Error("private-setup-body"), { code: "EPIPE" })); + const control = new AbortController(); let settled = false; + const running = backend.runTurn("synthetic", { cwd: "/fixture", mode: "chat", plan: false, model: "" }, collectSink().sink, control.signal).then(result => { settled = true; return result; }); + try { + await tick(); control.abort(); await tick(); + assert.equal(settled, false); + await assert.rejects(backend.listModels(), error => String(error).includes("transport") && !JSON.stringify(error).includes("private-setup-body")); + assert.equal(spawn.calls.length, 1); + proc.ignore.clear(); proc.close(null, "SIGKILL"); await tick(); + assert.equal(settled, false, "exit without closed stdio still retains this generation"); + proc.releaseStdio(); assert.notEqual((await running).status, "completed"); + } finally { proc.ignore.clear(); proc.close(null, "SIGKILL"); proc.releaseStdio(); await running; await backend.dispose(); } +}); + +test("a failed shared server holds both turns, rejects replacement, and ignores late old events", async () => { + const old = new FakeProcess(), next = new FakeProcess(); wire(old); wire(next); + old.holdStdio = true; old.ignore.add("SIGKILL"); + const spawn = fakeSpawn([old, next]), backend = new CodexBackend("fixture", spawn.spawn); + const control = new AbortController(), settled: string[] = []; + const first = backend.runTurn("first", { cwd: "/one", mode: "chat", plan: false, model: "" }, collectSink().sink, control.signal).then(result => { settled.push(result.status); return result; }); + const second = backend.runTurn("second", { cwd: "/two", mode: "chat", plan: false, model: "" }, collectSink().sink, new AbortController().signal).then(result => { settled.push(result.status); return result; }); + try { + for (let i = 0; i < 20 && old.sent(m => m.method === "turn/start").length < 2; i++) await tick(); + const starts = old.sent(m => m.method === "turn/start"); assert.equal(starts.length, 2); + old.stdin._write = (_chunk, _encoding, callback) => callback(Object.assign(new Error("private-shared-body"), { code: "EPIPE" })); + control.abort(); await tick(); + for (const m of starts) old.emitLine({ method: "turn/completed", params: { threadId: m.params.threadId, turn: { id: `tu-${m.params.threadId}`, status: "completed" } } }); + old.emit("error", new Error("termination has not been confirmed")); await tick(); + assert.deepEqual(settled, []); + await assert.rejects(backend.listModels(), /transport/); assert.equal(spawn.calls.length, 1); + await backend.forceStop(); await tick(); assert.deepEqual(settled, []); + old.ignore.clear(); old.close(null, "SIGKILL"); await tick(); assert.deepEqual(settled, []); + old.releaseStdio(); + assert.equal((await first).status, "interrupted"); + const failed = await second; assert.equal(failed.status, "failed"); assert.match(failed.error ?? "", /transport/); + assert.equal(JSON.stringify(failed).includes("private-shared-body"), false); + assert.deepEqual(await backend.listModels(), []); assert.equal(spawn.calls.length, 2); + old.stdin.emit("error", Object.assign(new Error("late-private-body"), { code: "EPIPE" })); + old.stdout.emit("data", Buffer.from(JSON.stringify({ method: "turn/completed", params: { threadId: "old", turn: { status: "completed" } } }) + "\n")); + old.emit("close", null, "SIGKILL"); await tick(); + assert.deepEqual(await backend.listModels(), []); assert.equal(next.killed, false); + } finally { old.ignore.clear(); old.close(null, "SIGKILL"); old.releaseStdio(); await Promise.all([first, second]); await backend.dispose(); } +}); + +for (const phase of ["initialize", "stop"] as const) test(`real Node pipe: ${phase} failure is handled without an uncaught error or retry`, () => { + // Never construct Node's special stdin stream: direct synchronous fd0 reads allow a real close + // without the libuv active-reader assertion or stdin.destroy()'s special stdio behavior. + const peer = `const fs = require('node:fs'); + const emit = value => console.log(JSON.stringify(value)); + function read() { let line = ''; const byte = Buffer.alloc(1); + while (!line.endsWith('\\n')) { try { if (!fs.readSync(0, byte, 0, 1, null)) process.exit(0); line += byte.toString(); } + catch (error) { if (error.code !== 'EAGAIN') throw error; Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1); } } + return JSON.parse(line); } + while (true) { const m = read(); + if (m.method === 'initialize') { + if (${JSON.stringify(phase)} === 'initialize') fs.closeSync(0); + emit({id:m.id,result:{userAgent:'synthetic-closed-input'}}); + if (${JSON.stringify(phase)} === 'initialize') break; + } else if (m.method === 'thread/start') emit({id:m.id,result:{thread:{id:'thr'},model:m.params.model,modelProvider:m.params.modelProvider}}); + else if (m.method === 'turn/start') { emit({id:m.id,result:{turn:{id:'tu'}}}); fs.closeSync(0); + emit({method:'turn/started',params:{threadId:'thr',turn:{id:'tu'}}}); break; } + } + setInterval(() => {}, 1000);`; + const probe = `import {spawn} from 'node:child_process'; + import {CodexBackend} from ${JSON.stringify(new URL("../src/main/engine/backends/codex.js", import.meta.url).href)}; + import {collectSink} from ${JSON.stringify(new URL("./fakeproc.js", import.meta.url).href)}; + const control = new AbortController(); let child, starts = 0; + const spawnImpl = (_bin, _args, options) => { starts++; child = spawn(process.execPath, ['-e', ${JSON.stringify(peer)}], options); + let output = ''; child.stdout.on('data', bytes => { output += bytes; if (output.includes('turn/started')) setImmediate(() => control.abort()); }); return child; }; + process.on('uncaughtExceptionMonitor', error => { console.error('UNCAUGHT:'+error.code); child?.kill('SIGKILL'); }); + process.on('SIGTERM', () => { child?.kill('SIGKILL'); process.exit(124); }); + const backend = new CodexBackend('synthetic-fixture', spawnImpl); + let result; + try { if (${JSON.stringify(phase)} === 'initialize') { try { await backend.listModels(); result = {status:'unexpected-success'}; } + catch (error) { result = {status:'failed',message:error.message}; } } + else result = await backend.runTurn('synthetic fixture', {cwd:process.cwd(),mode:'chat',plan:false,model:''}, collectSink().sink, control.signal); + } finally { await backend.dispose(); } + console.log(JSON.stringify({status:result.status,message:result.message,starts}));`; + const run = spawnSync(process.execPath, ["--input-type=module", "-e", probe], { encoding: "utf8", timeout: 5000, maxBuffer: 256 * 1024 }); + assert.equal(run.error, undefined); + assert.equal(run.status, 0, run.stderr); + assert.equal(run.stderr.includes("UNCAUGHT:"), false); + const result = JSON.parse(run.stdout.trim()); + assert.equal(result.status, phase === "initialize" ? "failed" : "interrupted"); + assert.equal(result.starts, 1, "transport failure cannot retry on another server"); + if (phase === "initialize") assert.match(result.message, /transport/i); +}); diff --git a/apps/desktop/test/human-requests-runner.test.ts b/apps/desktop/test/human-requests-runner.test.ts index 5dfd65d9..7e50a1e3 100644 --- a/apps/desktop/test/human-requests-runner.test.ts +++ b/apps/desktop/test/human-requests-runner.test.ts @@ -753,3 +753,36 @@ test("offline image denial is acknowledged without generating an image", async ( assert.equal(fs.readdirSync(thread.cwd).some(name => name.endsWith(".png")), false); } finally { await runner.dispose(); } }); + + +test("Codex pipe failure preserves the uncertain answer and blocks deletion until closure", async () => { + const proc = new FakeProcess(); + codexAsking(proc, { questions }, "item/tool/requestUserInput", false); + const codex = new CodexBackend("codex", fakeSpawn(proc).spawn), h = await harness(codex); + const sent = h.runner.send(h.thread.id, "synthetic fixture"); + let deleting: Promise | undefined; + try { + await waitFor(() => h.runner.items(h.thread.id).some(i => i.kind === "question")); + const card = h.runner.items(h.thread.id).find(i => i.kind === "question")!; + proc.holdStdio = true; proc.ignore.add("SIGKILL"); + proc.stdin._write = (_chunk, _encoding, callback) => callback(Object.assign(new Error("private-answer-fault"), { code: "EPIPE" })); + h.runner.answerQuestion(h.thread.id, card.id, { "q-env": ["staging"], "q-token": ["private-answer-token"] }); + await waitFor(() => (h.runner.items(h.thread.id).find(i => i.id === card.id) as { delivery?: string }).delivery === "uncertain"); + const saved = new Store(h.home).items(h.thread.id).find(i => i.id === card.id) as Extract; + assert.equal(saved.delivery, "uncertain"); assert.deepEqual(saved.answers, { "q-env": ["staging"], "q-token": ["(hidden)"] }); + assert.equal(saved.withdrawn, undefined); + let deleted = false; + deleting = h.runner.deleteThread(h.thread.id).then(() => { deleted = true; }); + await new Promise(resolve => setImmediate(resolve)); h.runner.stop(h.thread.id); + await new Promise(resolve => setImmediate(resolve)); + assert.equal(deleted, false); assert.ok(h.store.thread(h.thread.id)); + proc.ignore.clear(); proc.close(null, "SIGKILL"); + await new Promise(resolve => setImmediate(resolve)); assert.equal(deleted, false); + proc.releaseStdio(); await sent; await deleting; + assert.equal(h.store.thread(h.thread.id), undefined); + assert.equal(JSON.stringify(h.events).includes("private-answer-token"), false); + } finally { + proc.ignore.clear(); proc.close(null, "SIGKILL"); proc.releaseStdio(); + await sent; await deleting; await h.runner.dispose(); await codex.dispose(); + } +}); From 860ecc78c4fbec0cd28d8465e0fa93b5d6567282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Val=20Alexander=20=F0=9F=91=91?= Date: Sat, 14 Nov 2026 03:06:27 -0600 Subject: [PATCH 2/5] fix: bind Codex continuations to their server generation Keep metadata, admission, thread opens, terminal cleanup and response writes on the original server lease. Refuse retired-generation auth recovery, wait for closing servers, and report only that child's native version. --- .../desktop/src/main/engine/backends/codex.ts | 183 +++++++++++------ apps/desktop/test/codex-transport.test.ts | 193 +++++++++++++++++- 2 files changed, 311 insertions(+), 65 deletions(-) diff --git a/apps/desktop/src/main/engine/backends/codex.ts b/apps/desktop/src/main/engine/backends/codex.ts index fe0c6a7d..fa2f20cb 100644 --- a/apps/desktop/src/main/engine/backends/codex.ts +++ b/apps/desktop/src/main/engine/backends/codex.ts @@ -24,6 +24,14 @@ import { actionDigest, codexAnswerResponse, codexQuestions } from "../../../shar */ const STALE_AUTH = /(?:access token|authentication session) could not be refreshed/i; +interface ServerLease { + readonly child: ChildProcess; + readonly generation: string; + readonly closed: Promise; + userAgent: string | null; + retired: boolean; +} + /** * Drives the Codex CLI through `codex app-server`, the JSON-RPC-over-stdio protocol the * official Codex App uses. One server process is shared by all Codex threads; each Modex @@ -33,7 +41,7 @@ const STALE_AUTH = /(?:access token|authentication session) could not be refresh export class CodexBackend implements Backend { readonly id = "codex" as const; private child: ChildProcess | null = null; - private ready: Promise | null = null; + private ready: Promise | null = null; private nextId = 1; /** Includes turns still awaiting initialize/thread creation, before they subscribe to events. */ private activeTurns = 0; @@ -46,10 +54,8 @@ export class CodexBackend implements Backend { private readonly threadModels = new Map(); /** File paths of in-flight file changes, keyed `threadId:itemId`, so an approval can name them without the patch. */ private readonly changePaths = new Map(); - /** From the `initialize` reply: which codex build the server is. Kept for failure reports. */ - private userAgent: string | null = null; /** Opaque id of the running app-server process; native thread, turn and item ids are unique only within it. */ - private generation = ""; + private lease: ServerLease | null = null; /** * Server requests waiting on a person, keyed `generation:id`. Codex's `serverRequest/resolved` for one still * here means Codex settled it itself (codex-cli 0.163.0): the card is withdrawn and no answer is sent. @@ -57,7 +63,6 @@ export class CodexBackend implements Backend { private readonly openRequests = new Map(); /** A broken input pipe cannot release the generation until its owned process actually closes. */ private inputFailure: { child: ChildProcess; error: CodexError; closed: Promise } | null = null; - private readonly closures = new WeakMap>(); private readonly deliveries = new WeakMap>>(); private readonly writeReceipts = new WeakMap void>>(); @@ -81,9 +86,12 @@ export class CodexBackend implements Backend { /** `admit` sees the child's effective configuration and requirements before every thread open, and throws to refuse it (#380). */ local?: { model: string; provider: string; externalSandbox?: true; admit?: (reads: { config: unknown; requirements: unknown }) => void } }) {} - private ensure(): Promise { + private ensure(): Promise { if (this.inputFailure) return Promise.reject(this.inputFailure.error); - if (this.ready && this.child && this.child.exitCode === null) return this.ready; + if (this.ready && this.child) { + if (this.child.exitCode !== null || this.child.signalCode !== null) return Promise.reject(new CodexError("Codex app-server is closing; retry after it closes.", { stage: "close server" })); + return this.ready; + } this.ready = new Promise((resolve, reject) => { let child: ChildProcess; let bin = "codex"; @@ -96,8 +104,8 @@ export class CodexBackend implements Backend { } this.child = child; let closed!: () => void; - this.closures.set(child, new Promise(resolve => { closed = resolve; })); - this.generation = randomUUID(); + const lease: ServerLease = { child, generation: randomUUID(), closed: new Promise(resolve => { closed = resolve; }), userAgent: null, retired: false }; + this.lease = lease; this.loaded.clear(); this.metadataConnections.clear(); this.threadModels.clear(); @@ -108,7 +116,7 @@ export class CodexBackend implements Backend { // Redact across chunks before the bound can discard a token's beginning. stderr = (this.configuration?.redact(output) ?? output).slice(-16_384); }); - child.stdout?.on("data", (d: Buffer) => { if (this.child === child && this.inputFailure?.child !== child) lines.push(d, (line) => this.dispatch(line)); }); + child.stdout?.on("data", (d: Buffer) => { if (this.current(lease)) lines.push(d, line => { if (this.current(lease)) this.dispatch(lease, line); }); }); // This is a separate emitter from ChildProcess.error. Retain the listener for the // stream's whole lifetime so a superseded child's late error is still consumed. child.stdin?.on("error", error => { this.inputFailed(child, error); }); @@ -128,17 +136,19 @@ export class CodexBackend implements Backend { if (this.child === child) { const output = this.configuration?.redact(stderr) ?? stderr; this.inputFailure = null; - this.disconnect(fault?.error ?? new CodexError(`codex app-server exited (${code}) ${output.trim().split("\n").slice(-2).join(" ")}`, { exitCode: code, signal, stderr: stderrTail(output), pid: child.pid, userAgent: this.userAgent })); + this.disconnect(fault?.error ?? new CodexError(`codex app-server exited (${code}) ${output.trim().split("\n").slice(-2).join(" ")}`, { exitCode: code, signal, stderr: stderrTail(output), pid: child.pid, userAgent: lease.userAgent })); } } finally { closed(); } }; void finalize(); }); - this.request<{ userAgent?: string }>("initialize", { clientInfo: { name: "modex", title: "Modex", version: process.env.MODEX_VERSION ?? "0.0.1" }, capabilities: { experimentalApi: true } }, 5000) + this.request<{ userAgent?: string }>(lease, "initialize", { clientInfo: { name: "modex", title: "Modex", version: process.env.MODEX_VERSION ?? "0.0.1" }, capabilities: { experimentalApi: true } }, 5000) .then(async (r) => { - this.userAgent = typeof r?.userAgent === "string" ? r.userAgent : null; - await this.notify("initialized", {}); - resolve(); + this.requireLease(lease); + await this.notify(lease, "initialized", {}); + this.requireLease(lease); + lease.userAgent = typeof r?.userAgent === "string" ? r.userAgent : null; + resolve(lease); }) .catch(reject); }); @@ -148,6 +158,7 @@ export class CodexBackend implements Backend { private disconnect(error: Error): void { if (this.child) for (const finish of this.writeReceipts.get(this.child) ?? []) finish(error); this.child = null; + this.lease = null; this.ready = null; this.loaded.clear(); this.metadataConnections.clear(); @@ -160,7 +171,7 @@ export class CodexBackend implements Backend { for (const finish of this.disconnects) finish(error); } - private dispatch(line: string): void { + private dispatch(lease: ServerLease, line: string): void { let msg: RpcMessage; try { const fields = this.redactedInputs, protectedValues = this.protectedValues, rawIds = this.protectedTransportIds, redact = this.configuration?.redact; @@ -194,7 +205,7 @@ export class CodexBackend implements Backend { const p = msg.params as Record | undefined; const patch = msg.method === "item/fileChange/requestApproval" ? this.changePaths.get(`${String(p?.threadId ?? "")}:${String(p?.itemId ?? "")}`) : undefined; if (this.protectedValues.has(msg) || patch?.protected) { - this.send({ id: this.protectedTransportIds.get(msg) ?? msg.id, error: { code: -32602, message: "Modex refused this request because it contains protected credential data." } }); + this.send(lease, { id: this.protectedTransportIds.get(msg) ?? msg.id, error: { code: -32602, message: "Modex refused this request because it contains protected credential data." } }); return; } } @@ -248,7 +259,7 @@ export class CodexBackend implements Backend { stage: "stdin", pid: child.pid, ...(typeof code === "string" && /^[A-Z0-9_]{1,32}$/.test(code) ? { errno: code } : {}), }); if (this.child !== child) return error; // Consume old errors without touching the replacement. - this.inputFailure = { child, error, closed: this.closures.get(child)! }; + this.inputFailure = { child, error, closed: this.lease!.closed }; for (const finish of this.writeReceipts.get(child) ?? []) finish(error); for (const pending of this.pending.values()) pending.reject(error); this.pending.clear(); @@ -260,8 +271,18 @@ export class CodexBackend implements Backend { return error; } - private send(o: unknown, written?: (error?: Error) => void): void { - const child = this.child; + private current(lease: ServerLease): boolean { + return !lease.retired && this.lease === lease && this.child === lease.child && this.inputFailure?.child !== lease.child; + } + + private requireLease(lease: ServerLease): void { + if (lease.retired || this.lease !== lease || this.child !== lease.child) throw new CodexError("Codex app-server generation is no longer available.", { stage: "server generation" }); + if (this.inputFailure?.child === lease.child) throw this.inputFailure.error; + } + + private send(lease: ServerLease, o: unknown, written?: (error?: Error) => void): void { + try { this.requireLease(lease); } catch (error) { written?.(error as Error); return; } + const child = lease.child; if (!child) { written?.(new CodexError("codex app-server is not running", {})); return; } if (this.inputFailure) { written?.(this.inputFailure.error); return; } const stdin = child.stdin; @@ -280,8 +301,8 @@ export class CodexBackend implements Backend { } catch (error) { finish(this.inputFailed(child, error)); } } - private request(method: string, params: unknown, timeoutMs?: number): Promise { - if (this.inputFailure) return Promise.reject(this.inputFailure.error); + private request(lease: ServerLease, method: string, params: unknown, timeoutMs?: number): Promise { + try { this.requireLease(lease); } catch (error) { return Promise.reject(error); } if (!this.child || this.child.exitCode !== null) return Promise.reject(new CodexError("codex app-server is not running", { method })); if (!this.child.stdin?.writable || this.child.stdin.destroyed || this.child.stdin.writableEnded) return Promise.reject(this.inputFailed(this.child)); const id = this.nextId++; @@ -295,27 +316,30 @@ export class CodexBackend implements Backend { resolve: (value) => { clearTimeout(timer); resolve(value as T); }, reject: (error) => { clearTimeout(timer); reject(error); }, }); - this.send({ id, method, params }); + this.send(lease, { id, method, params }); }); } - private notify(method: string, params: unknown): Promise { - return new Promise((resolve, reject) => this.send({ method, params }, error => error ? reject(error) : resolve())); + private notify(lease: ServerLease, method: string, params: unknown): Promise { + return new Promise((resolve, reject) => this.send(lease, { method, params }, error => error ? reject(error) : resolve())); } - private respond(id: unknown, result: unknown): void { - this.send({ id, result }); + private respond(lease: ServerLease, id: unknown, result: unknown): void { + this.send(lease, { id, result }); } async quota(): Promise { if (this.configuration?.local) return undefined; - await this.ensure(); - return codexQuota(await this.request("account/rateLimits/read", {}, 3000)); + const lease = await this.ensure(); + const result = await this.request(lease, "account/rateLimits/read", {}, 3000); + this.requireLease(lease); + return codexQuota(result); } async listModels(): Promise { - await this.ensure(); - const res = await this.request<{ data: CodexModel[] }>("model/list", {}); + const lease = await this.ensure(); + const res = await this.request<{ data: CodexModel[] }>(lease, "model/list", {}); + this.requireLease(lease); return res.data .filter((m) => !m.hidden) .map((m) => ({ @@ -332,7 +356,10 @@ export class CodexBackend implements Backend { let timer: ReturnType | undefined; try { const result = await Promise.race([ - this.ensure().then(() => this.request<{ account: { type?: unknown } | null; requiresOpenaiAuth?: boolean }>("account/read", { refreshToken: false }, 5000)), + this.ensure().then(async lease => { + const value = await this.request<{ account: { type?: unknown } | null; requiresOpenaiAuth?: boolean }>(lease, "account/read", { refreshToken: false }, 5000); + this.requireLease(lease); return value; + }), new Promise((_, reject) => { timer = setTimeout(() => reject(new Error("timeout")), 5000); }), ]); if (result.account?.type === "chatgpt") return report("authenticated", "ChatGPT signed in · model access unverified"); @@ -360,13 +387,16 @@ export class CodexBackend implements Backend { * Ends this turn's own terminals, never an earlier turn's, and calls a command interrupted only * once Codex no longer lists it. Anything Codex cannot confirm stays unknown, with a notice. */ - private async endTerminals(threadId: string, commands: ReadonlySet, settle: (item: string, outcome: "interrupted" | "unknown") => void, sink: TurnSink): Promise { + private async endTerminals(lease: ServerLease, threadId: string, commands: ReadonlySet, settle: (item: string, outcome: "interrupted" | "unknown") => void, sink: TurnSink): Promise { type Terminal = { itemId?: string; processId?: string }; - const list = async () => (await this.request<{ data?: Terminal[] }>("thread/backgroundTerminals/list", { threadId }, 3000))?.data ?? []; + const list = async () => { + const value = await this.request<{ data?: Terminal[] }>(lease, "thread/backgroundTerminals/list", { threadId }, 3000); + this.requireLease(lease); return value?.data ?? []; + }; let running: Set; try { for (const terminal of (await list()).filter((t) => t.itemId && commands.has(t.itemId) && t.processId)) { - await this.request("thread/backgroundTerminals/terminate", { threadId, processId: terminal.processId }, 3000).catch(() => undefined); + await this.request(lease, "thread/backgroundTerminals/terminate", { threadId, processId: terminal.processId }, 3000).catch(() => undefined); } running = new Set((await list()).map((t) => t.itemId ?? "").filter((id) => commands.has(id))); } catch { @@ -380,6 +410,7 @@ export class CodexBackend implements Backend { private restart(reason: string): void { const child = this.child; if (!child) return; + if (this.lease) this.lease.retired = true; if (this.inputFailure?.child === child) { try { killGroup(child); } catch { /* A repeated Stop still needs closure. */ } return; @@ -390,6 +421,7 @@ export class CodexBackend implements Backend { async dispose(): Promise { const child = this.child; + if (this.lease) this.lease.retired = true; if (this.inputFailure && this.inputFailure.child === child) { const fault = this.inputFailure; try { killGroup(child!); } catch { /* Do not release a failed generation on signal submission. */ } @@ -446,18 +478,35 @@ export class CodexBackend implements Backend { if (second.status !== "failed") return second; return { ...second, detail: { ...second.detail, firstAttempt: { error: first.error, ...first.detail } }, recovery: [`${step}: failed again`] }; } finally { - // Setup cancellation, RPC failure and timeouts cannot free this turn while its failed child lives. - if (this.inputFailure) await this.inputFailure.closed; this.activeTurns--; } } private async attempt(text: string, opts: TurnOptions, sink: TurnSink, signal: AbortSignal): Promise { if (signal.aborted) return { status: "interrupted" }; + const readiness = this.ensure(), owner = this.lease; + try { + const result = await this.attemptOn(readiness, owner, text, opts, sink, signal); + // The stale-sign-in policy can recover its own server, never a disposed or superseded one. + // An unexpected close with no replacement keeps the established recovery behavior. + if (owner && result.status === "failed" && STALE_AUTH.test(result.error ?? "") && (owner.retired || (this.lease && this.lease !== owner))) { + return { ...result, error: "Codex app-server generation is no longer available.", detail: { ...result.detail, supersededError: result.error, stage: "server generation" } }; + } + return result; + } finally { + // Only this attempt's failed generation fences its completion, never a replacement's. + const fault = this.inputFailure; + if (owner && fault?.child === owner.child) await fault.closed; + } + } + + private async attemptOn(readiness: Promise, owner: ServerLease | null, text: string, opts: TurnOptions, sink: TurnSink, signal: AbortSignal): Promise { const failed = (err: unknown, extra: Record): TurnResult => - signal.aborted ? { status: "interrupted" } : { status: "failed", error: (err as Error).message, detail: { ...extra, ...detailOf(err), userAgent: this.userAgent, pid: this.child?.pid } }; + signal.aborted ? { status: "interrupted" } : { status: "failed", error: (err as Error).message, detail: { ...extra, ...detailOf(err), userAgent: owner?.userAgent, pid: owner?.child.pid } }; + let lease: ServerLease; try { - await duringSetup(this.ensure(), signal); + lease = await duringSetup(readiness, signal); + this.requireLease(lease); } catch (err) { return failed(err, { stage: "start app-server" }); } @@ -486,7 +535,7 @@ export class CodexBackend implements Backend { // thread is opened: confirming the provider's name on open does not say where its requests go (#380). if (local?.admit) { try { - const reads = await duringSetup(Promise.all([this.request("config/read", { includeLayers: true, cwd: opts.cwd }), this.request("configRequirements/read", {})]) + const reads = await duringSetup(Promise.all([this.request(lease, "config/read", { includeLayers: true, cwd: opts.cwd }), this.request(lease, "configRequirements/read", {})]) .catch((err: unknown) => { const detail = detailOf(err), reason = (err as Error).message; const recovery = /failed to synchronize managed preferences/i.test(reason) @@ -494,6 +543,7 @@ export class CodexBackend implements Backend { : detail.rpcCode === -32601 ? " Local execution needs codex-cli 0.164.0 or later." : ""; throw new CodexError(`Codex did not report its effective configuration (${reason}).${recovery}`, detail); }), signal); + this.requireLease(lease); local.admit({ config: reads[0], requirements: reads[1] }); } catch (err) { return failed(err, { stage: "verify configuration" }); @@ -507,13 +557,15 @@ export class CodexBackend implements Backend { }; try { if (threadId && (!this.loaded.has(threadId) || rebindMetadata)) { - const r = await duringSetup(this.request<{ thread: { id: string }; model?: unknown; modelProvider?: unknown }>("thread/resume", { threadId, cwd: opts.cwd, approvalPolicy: pol.approvalPolicy, sandbox: pol.sandbox, model: opts.model || null, ...pinned, config }), signal); + const r = await duringSetup(this.request<{ thread: { id: string }; model?: unknown; modelProvider?: unknown }>(lease, "thread/resume", { threadId, cwd: opts.cwd, approvalPolicy: pol.approvalPolicy, sandbox: pol.sandbox, model: opts.model || null, ...pinned, config }), signal); + this.requireLease(lease); confirmed(r); if (rebindMetadata && r.thread.id !== threadId) throw new CodexError("Codex changed conversation identity while rebinding metadata connections.", { stage: "bind metadata connections" }); threadId = r.thread.id; if (typeof r.model === "string" && r.model) this.threadModels.set(threadId, r.model); } else if (!threadId) { - const r = await duringSetup(this.request<{ thread: { id: string }; model?: unknown; modelProvider?: unknown }>("thread/start", { cwd: opts.cwd, approvalPolicy: pol.approvalPolicy, sandbox: pol.sandbox, model: opts.model || null, ...pinned, config }), signal); + const r = await duringSetup(this.request<{ thread: { id: string }; model?: unknown; modelProvider?: unknown }>(lease, "thread/start", { cwd: opts.cwd, approvalPolicy: pol.approvalPolicy, sandbox: pol.sandbox, model: opts.model || null, ...pinned, config }), signal); + this.requireLease(lease); confirmed(r); threadId = r.thread.id; if (typeof r.model === "string" && r.model) this.threadModels.set(threadId, r.model); @@ -523,9 +575,11 @@ export class CodexBackend implements Backend { ...((taskContext || priorMetadata?.taskContext) ? { taskContextCapability: { state: "unavailable" as const, reason: "configuration-not-bound" as const } } : {}) }; } if (signal.aborted) return { status: "interrupted" }; + try { this.requireLease(lease); } catch (error) { return failed(error, { stage: "publish session" }); } this.loaded.add(threadId); this.metadataConnections.set(threadId, metadata); if (taskContext) sink.taskContextCapability?.({ state: "available" }); + try { this.requireLease(lease); } catch (error) { return failed(error, { stage: "publish session" }); } sink.session(threadId); const tid = threadId; @@ -538,7 +592,7 @@ export class CodexBackend implements Backend { const collaborationMode = planModel ? { mode: "plan", settings: { model: planModel, reasoning_effort: opts.effort ?? null, developer_instructions: null } } : undefined; const prefixed = opts.plan ? `${PLAN_PREFIX}\n\n${context}` : context; const input = collaborationMode ? context : prefixed; - const runtime = this.generation; + const runtime = lease.generation; const seenRequests = new Map(); const agents = new AgentTracker(sink, runtime); const activities = new Set(); @@ -574,14 +628,14 @@ export class CodexBackend implements Backend { // Some native reconnect paths still retry with provider retry counts set to zero. // Local coding must stop on that notification rather than continue after possible effects. let retryRefused: { message: string; nativeError: Record } | null = null; - const child = this.child; + const child = lease.child; const detail = (extra: Record): Record => ({ - codexThreadId: tid, turnId, userAgent: this.userAgent, pid: child?.pid, ...(retried.length ? { retriedErrors: retried } : {}), ...(lastError ? { errorNotification: lastError } : {}), ...extra, + codexThreadId: tid, turnId, userAgent: lease.userAgent, pid: child.pid, ...(retried.length ? { retriedErrors: retried } : {}), ...(lastError ? { errorNotification: lastError } : {}), ...extra, }); const interrupt = () => { if (interrupted || !turnId || this.child !== child) return; interrupted = true; - void this.request("turn/interrupt", { threadId: tid, turnId }).catch(() => {}); + void this.request(lease, "turn/interrupt", { threadId: tid, turnId }).catch(() => {}); }; const onDisconnect = (error: Error) => finish(signal.aborted ? { status: "interrupted" } : { status: "failed", error: error.message, detail: detail(detailOf(error)) }); const finish = (r: TurnResult) => { @@ -608,7 +662,7 @@ export class CodexBackend implements Backend { interrupt(); }; const onMsg = (msg: RpcMessage) => { - if (done || stopping) return; + if (done || stopping || !this.current(lease)) return; const p = (msg.params ?? {}) as Record; const native = (item: unknown): NativeRef => ({ runtime, thread: tid, ...(p.turnId || turnId ? { turn: String(p.turnId ?? turnId) } : {}), item: String(item) }); if (msg.method === "thread/started") { @@ -635,7 +689,7 @@ export class CodexBackend implements Backend { const state = status.type === "active" ? (status.activeFlags?.length ? "waiting" : "running") : "failed"; agents.update(id, { state, detail: state === "waiting" ? "Waiting for approval or input" : undefined }); } else if (msg.id !== undefined && msg.method) { - void this.handleServerRequest(msg, sink, seenRequests); + void this.handleServerRequest(lease, msg, sink, seenRequests); } else if (msg.method === "turn/completed") { const turn = p.turn as { status?: string; error?: { message?: string } } | undefined; agents.update(id, { state: turn?.status === "completed" ? "completed" : turn?.status === "interrupted" ? "stopped" : "failed", detail: turn?.error?.message }, turn?.error?.message); @@ -672,7 +726,7 @@ export class CodexBackend implements Backend { } // Server → client requests (approvals). if (msg.id !== undefined && msg.method) { - void this.handleServerRequest(msg, sink, seenRequests); + void this.handleServerRequest(lease, msg, sink, seenRequests); return; } if (msg.method === "item/started" || msg.method === "item/completed") { @@ -838,7 +892,7 @@ export class CodexBackend implements Backend { if (commands.size) { stopping = true; clearTimeout(abortTimer); - void this.endTerminals(tid, commands, (id, outcome) => { const t = tools.get(id); sink.observe({ type: "tool.done", native: native(id), outcome, progress: { ...(t?.output ? { output: t.output } : {}), ...(t ? { durationMs: Date.now() - t.started } : {}) } }); }, sink).then(() => finish(failure)); + void this.endTerminals(lease, tid, commands, (id, outcome) => { const t = tools.get(id); sink.observe({ type: "tool.done", native: native(id), outcome, progress: { ...(t?.output ? { output: t.output } : {}), ...(t ? { durationMs: Date.now() - t.started } : {}) } }); }, sink).then(() => finish(failure)); } else finish(failure); } else { @@ -851,7 +905,7 @@ export class CodexBackend implements Backend { const t = tools.get(id); sink.observe({ type: "tool.done", native: native(id), outcome, progress: { ...(t?.output ? { output: t.output } : {}), ...(t ? { durationMs: Date.now() - t.started } : {}) } }); }; - void this.endTerminals(tid, commands, settle, sink).then(() => finish({ status: "interrupted" })); + void this.endTerminals(lease, tid, commands, settle, sink).then(() => finish({ status: "interrupted" })); } else finish(turn.status === "interrupted" ? { status: "interrupted" } : { status: "completed" }); } break; @@ -863,7 +917,7 @@ export class CodexBackend implements Backend { this.subscribers.add(onMsg); this.disconnects.add(onDisconnect); signal.addEventListener("abort", onAbort, { once: true }); - const turnStart = (text: string, mode: typeof collaborationMode) => this.request<{ turn: { id: string } }>("turn/start", { + const turnStart = (text: string, mode: typeof collaborationMode) => this.request<{ turn: { id: string } }>(lease, "turn/start", { threadId: tid, // Images go by path (the app-server reads them); other attached files are named in the text. input: [{ type: "text", text, text_elements: [] }, ...(opts.attachments ?? []).filter((a) => a.kind === "image").map((a) => ({ type: "localImage", path: a.path }))], @@ -878,12 +932,14 @@ export class CodexBackend implements Backend { }); turnStart(input, collaborationMode) .catch((err: unknown) => { + this.requireLease(lease); // A Codex that does not take the collaboration mode refuses the request before starting a turn. if (!collaborationMode || signal.aborted || !refusedCollaborationMode(err)) throw err; sink.notice("info", "This Codex does not offer its plan mode, so this Plan turn runs with Modex's plan instructions."); return turnStart(prefixed, undefined); }) .then((r) => { + this.requireLease(lease); turnId = r.turn.id; starting = false; if (signal.aborted) interrupt(); @@ -902,8 +958,9 @@ export class CodexBackend implements Backend { return paths; } - private async handleServerRequest(msg: RpcMessage, sink: TurnSink, seen: Map): Promise { - const key = `${this.generation}:${JSON.stringify(msg.id)}`; + private async handleServerRequest(lease: ServerLease, msg: RpcMessage, sink: TurnSink, seen: Map): Promise { + if (!this.current(lease)) return; + const key = `${lease.generation}:${JSON.stringify(msg.id)}`; const digest = actionDigest({ method: msg.method, params: msg.params }); if (seen.has(key)) { if (seen.get(key) !== digest) { @@ -916,19 +973,19 @@ export class CodexBackend implements Backend { const withdrawn = new AbortController(); this.openRequests.set(key, { controller: withdrawn, thread: (msg.params as Record | undefined)?.threadId }); try { - await this.answerServerRequest(msg, sink, withdrawn.signal); + await this.answerServerRequest(lease, msg, sink, withdrawn.signal); } finally { if (this.openRequests.get(key)?.controller === withdrawn) this.openRequests.delete(key); } } - private async answerServerRequest(msg: RpcMessage, sink: TurnSink, withdrawn: AbortSignal): Promise { + private async answerServerRequest(lease: ServerLease, msg: RpcMessage, sink: TurnSink, withdrawn: AbortSignal): Promise { const p = (msg.params ?? {}) as Record; - const native: NativeRequest = { runtime: this.generation, request: JSON.stringify(msg.id), ...(typeof p.itemId === "string" && p.itemId ? { item: p.itemId } : {}) }; + const native: NativeRequest = { runtime: lease.generation, request: JSON.stringify(msg.id), ...(typeof p.itemId === "string" && p.itemId ? { item: p.itemId } : {}) }; const deliver = async (result: unknown) => { - const child = this.child; + const child = lease.child; const write = () => { - if (withdrawn.aborted || this.generation !== native.runtime || !child || child !== this.child || child.exitCode !== null || this.inputFailure?.child === child) return Promise.reject(new Error("request transport unavailable")); + if (withdrawn.aborted || !this.current(lease) || child.exitCode !== null) return Promise.reject(new Error("request transport unavailable")); return writeJsonResponse(child.stdin, { id: msg.id, result }).catch(error => { this.inputFailed(child, error); throw error; }); }; if (!child) return; @@ -967,7 +1024,7 @@ export class CodexBackend implements Backend { const permissions = requestedPermissions(p.permissions); if (!permissions) { sink.notice("warn", "Codex requested malformed or unsupported permissions; no permissions were granted."); - this.respond(msg.id, { permissions: {}, scope: "turn" }); + this.respond(lease, msg.id, { permissions: {}, scope: "turn" }); return; } const answer = await sink.approval({ question: "Allow these additional permissions for this turn?", detail: JSON.stringify({ ...p, permissions }, null, 2), canAlways: false, action: { backend: "codex", tool: "permissions", title: "grant additional permissions", escalation: true }, native }, withdrawn); @@ -978,17 +1035,17 @@ export class CodexBackend implements Backend { const asked = codexQuestions(p); if (!asked) { sink.notice("warn", "Codex asked a question Modex could not read; it was declined."); - this.send({ id: msg.id, error: { code: -32602, message: "modex could not read this requestUserInput" } }); + this.send(lease, { id: msg.id, error: { code: -32602, message: "modex could not read this requestUserInput" } }); return; } const reply = await sink.question({ ...asked, native }, withdrawn); if (withdrawn.aborted) return; // A withdrawn question is an error reply, never an empty answer the model could take as the user's. if ("answers" in reply) await deliver(codexAnswerResponse(reply.answers)); - else this.send({ id: msg.id, error: { code: -32000, message: reply.withdrawn === "stopped" ? "withdrawn by modex: the turn was stopped" : "withdrawn by modex: the turn ended before anyone answered" } }); + else this.send(lease, { id: msg.id, error: { code: -32000, message: reply.withdrawn === "stopped" ? "withdrawn by modex: the turn was stopped" : "withdrawn by modex: the turn ended before anyone answered" } }); } else { sink.notice("warn", `Codex asked for ${msg.method}, which Modex does not support yet; declined.`); - this.send({ id: msg.id, error: { code: -32601, message: `unsupported by modex: ${msg.method}` } }); + this.send(lease, { id: msg.id, error: { code: -32601, message: `unsupported by modex: ${msg.method}` } }); } } } diff --git a/apps/desktop/test/codex-transport.test.ts b/apps/desktop/test/codex-transport.test.ts index fddde775..2d954dc9 100644 --- a/apps/desktop/test/codex-transport.test.ts +++ b/apps/desktop/test/codex-transport.test.ts @@ -1,19 +1,25 @@ import test from "node:test"; import assert from "node:assert/strict"; -import { spawnSync } from "node:child_process"; +import { spawnSync, type SpawnOptions } from "node:child_process"; +import type { QuestionReply } from "../src/main/engine/backends/types.js"; import { setTimeout as delay } from "node:timers/promises"; import { CodexBackend } from "../src/main/engine/backends/codex.js"; import { FakeProcess, fakeSpawn, collectSink } from "./fakeproc.js"; const tick = () => new Promise(resolve => setImmediate(resolve)); -function wire(proc: FakeProcess): void { +function wire(proc: FakeProcess, hold: ReadonlySet = new Set()): void { let threads = 0; proc.stdin.on("data", data => { for (const line of data.toString().split("\n")) { if (!line.trim()) continue; const m = JSON.parse(line); + if (hold.has(m.method)) continue; if (m.method === "initialize") proc.emitLine({ id: m.id, result: { userAgent: "synthetic-transport" } }); else if (m.method === "model/list") proc.emitLine({ id: m.id, result: { data: [] } }); + else if (m.method === "account/read") proc.emitLine({ id: m.id, result: { account: { type: "chatgpt" } } }); + else if (m.method === "account/rateLimits/read") proc.emitLine({ id: m.id, result: { rateLimits: {} } }); + else if (["config/read", "configRequirements/read", "thread/backgroundTerminals/terminate"].includes(m.method)) proc.emitLine({ id: m.id, result: {} }); + else if (m.method === "thread/backgroundTerminals/list") proc.emitLine({ id: m.id, result: { data: [] } }); else if (m.method === "thread/start") proc.emitLine({ id: m.id, result: { thread: { id: `thr-${++threads}` }, model: m.params.model, modelProvider: m.params.modelProvider } }); else if (m.method === "turn/start") { proc.emitLine({ id: m.id, result: { turn: { id: `tu-${m.params.threadId}` } } }); @@ -177,3 +183,186 @@ for (const phase of ["initialize", "stop"] as const) test(`real Node pipe: ${pha assert.equal(result.starts, 1, "transport failure cannot retry on another server"); if (phase === "initialize") assert.match(result.message, /transport/i); }); + +test("a queued old initialize continuation cannot use the replacement", async () => { + const old = new FakeProcess(), next = new FakeProcess(); wire(old, new Set(["initialize"])); wire(next); + const backend = new CodexBackend("fixture", fakeSpawn([old, next]).spawn); + const first = backend.listModels().then(() => "success", () => "failed"); + await old.waitFor(line => line.includes('"initialize"')); + const request = old.sent(m => m.method === "initialize")[0]!; + old.emitLine({ id: request.id, result: { userAgent: "old" } }); + const disposing = backend.dispose(), replacement = backend.listModels(); + try { + assert.equal(await first, "failed"); await replacement; await disposing; + assert.equal(next.sent(m => m.method === "initialized").length, 1); + assert.equal(next.sent(m => m.method === "model/list").length, 1); + } finally { await disposing; await replacement; await backend.dispose(); } +}); + +/** Dispose in the microtask between readiness resolving and its caller continuing. */ +function replaceAfterReady(old: FakeProcess, replace: () => void): void { + const original = old.stdin.write.bind(old.stdin); + old.stdin.write = ((chunk: unknown, ...args: unknown[]) => { + if (String(chunk).includes('"method":"initialized"')) { + const callback = args.find(value => typeof value === "function") as () => void; + setImmediate(() => { callback(); queueMicrotask(replace); }); + return true; + } + return Reflect.apply(original, old.stdin, [chunk, ...args]); + }) as typeof old.stdin.write; +} + +for (const kind of ["models", "quota", "account"] as const) test(`a disposed ready generation cannot migrate its ${kind} read`, async () => { + const old = new FakeProcess(), next = new FakeProcess(); wire(old); wire(next); + const servers = fakeSpawn([old, next]); + const spawn = ((file: string, args: string[], options: SpawnOptions) => { + if (args.includes("--version")) { + const version = new FakeProcess(); + setImmediate(() => { version.emitRaw("codex-cli 0.164.0\n"); version.close(0); }); + return fakeSpawn(version).spawn(file, args, options); + } + return servers.spawn(file, args, options); + }) as typeof servers.spawn; + const backend = new CodexBackend("fixture", spawn); + let disposing: Promise | undefined, replacement: Promise | undefined; + replaceAfterReady(old, () => { disposing = backend.dispose(); replacement = backend.listModels(); }); + const first = kind === "account" ? backend.health().then(value => value.authentication) + : (kind === "models" ? backend.listModels() : backend.quota()).then(() => "success", () => "failed"); + try { + assert.equal(await first, kind === "account" ? "unknown" : "failed"); + await disposing; await replacement; + assert.equal(next.sent(m => m.method === "initialized").length, 1); + assert.equal(next.sent(m => m.method === "model/list").length, 1); + assert.equal(next.sent(m => m.method === "account/read" || m.method === "account/rateLimits/read").length, 0); + } finally { await disposing; await replacement; await backend.dispose(); } +}); + +test("an old thread-open reply cannot publish a session or start a replacement turn", async () => { + const old = new FakeProcess(), next = new FakeProcess(); wire(old, new Set(["thread/start"])); wire(next); + const backend = new CodexBackend("fixture", fakeSpawn([old, next]).spawn), sessions: string[] = []; + const { sink } = collectSink(); + const running = backend.runTurn("synthetic", { cwd: "/fixture", mode: "chat", plan: false, model: "" }, { ...sink, session: value => { sessions.push(value); } }, new AbortController().signal); + await old.waitFor(line => line.includes('"thread/start"')); + const request = old.sent(m => m.method === "thread/start")[0]!; + old.emitLine({ id: request.id, result: { thread: { id: "old-thread" }, model: "old-model" } }); + const disposing = backend.dispose(), replacement = backend.listModels(); + // The baseline may incorrectly start a turn on next. Complete only that synthetic turn so the RED cannot hang. + next.stdin.on("data", bytes => { for (const line of bytes.toString().split("\n")) { + if (!line.trim()) continue; const m = JSON.parse(line); + if (m.method === "turn/start") setImmediate(() => next.emitLine({ method: "turn/completed", params: { threadId: m.params.threadId, turn: { id: `tu-${m.params.threadId}`, status: "completed" } } })); + } }); + try { + assert.equal((await running).status, "failed"); await replacement; await disposing; + assert.deepEqual(sessions, []); + assert.equal(next.sent(m => m.method === "turn/start").length, 0); + } finally { await disposing; await replacement; await backend.dispose(); await running; } +}); + +test("old configuration replies cannot admit execution on a replacement", async () => { + const old = new FakeProcess(), next = new FakeProcess(); wire(old, new Set(["config/read", "configRequirements/read"])); wire(next); + let admitted = 0; + const backend = new CodexBackend("fixture", fakeSpawn([old, next]).spawn, { args: [], env: {}, redact: value => value, local: { provider: "fixture", model: "selected", admit: () => { admitted++; } } }); + const running = backend.runTurn("synthetic", { cwd: "/fixture", mode: "chat", plan: false, model: "selected" }, collectSink().sink, new AbortController().signal); + await old.waitFor(line => line.includes('"configRequirements/read"')); + for (const m of old.sent(m => m.method === "config/read" || m.method === "configRequirements/read")) old.emitLine({ id: m.id, result: {} }); + const disposing = backend.dispose(), replacement = backend.listModels(); + next.stdin.on("data", bytes => { for (const line of bytes.toString().split("\n")) { + if (!line.trim()) continue; const m = JSON.parse(line); + if (m.method === "turn/start") setImmediate(() => next.emitLine({ method: "turn/completed", params: { threadId: m.params.threadId, turn: { id: `tu-${m.params.threadId}`, status: "completed" } } })); + } }); + try { + assert.equal((await running).status, "failed"); await replacement; await disposing; + assert.equal(admitted, 0); + assert.equal(next.sent(m => m.method === "thread/start" || m.method === "turn/start").length, 0); + } finally { await disposing; await replacement; await backend.dispose(); await running; } +}); + +test("an old terminal-list reply cannot terminate a replacement's process", async () => { + const old = new FakeProcess(), next = new FakeProcess(); wire(old, new Set(["thread/backgroundTerminals/list"])); wire(next); + const backend = new CodexBackend("fixture", fakeSpawn([old, next]).spawn); + const running = backend.runTurn("synthetic", { cwd: "/fixture", mode: "chat", plan: false, model: "" }, collectSink().sink, new AbortController().signal); + await old.waitFor(line => line.includes('"turn/start"')); await tick(); + const start = old.sent(m => m.method === "turn/start")[0]!; + const threadId = start.params.threadId, turnId = `tu-${threadId}`; + old.emitLine({ method: "item/started", params: { threadId, turnId, item: { type: "commandExecution", id: "old-command", command: "synthetic", cwd: "/fixture", status: "inProgress" } } }); + old.emitLine({ method: "turn/completed", params: { threadId, turn: { id: turnId, status: "interrupted" } } }); + await old.waitFor(line => line.includes('"thread/backgroundTerminals/list"')); + const list = old.sent(m => m.method === "thread/backgroundTerminals/list")[0]!; + old.emitLine({ id: list.id, result: { data: [{ itemId: "old-command", processId: "old-process" }] } }); + const disposing = backend.dispose(), replacement = backend.listModels(); + try { + await running; await replacement; await disposing; + for (let i = 0; i < 5; i++) await tick(); + assert.equal(next.sent(m => m.method === "thread/backgroundTerminals/terminate").length, 0); + assert.equal(next.sent(m => m.method === "thread/backgroundTerminals/list").length, 0); + } finally { await disposing; await replacement; await backend.dispose(); await running; } +}); + +test("withdrawing an old native question cannot reply through the replacement", async () => { + const old = new FakeProcess(), next = new FakeProcess(); wire(old); wire(next); + const backend = new CodexBackend("fixture", fakeSpawn([old, next]).spawn), { sink } = collectSink(); + let displayed = false; + const running = backend.runTurn("synthetic", { cwd: "/fixture", mode: "chat", plan: false, model: "" }, { ...sink, question: (_request, withdrawn) => new Promise(resolve => { + displayed = true; withdrawn?.addEventListener("abort", () => resolve({ withdrawn: "native" }), { once: true }); + }) }, new AbortController().signal); + await old.waitFor(line => line.includes('"turn/start"')); await tick(); + const start = old.sent(m => m.method === "turn/start")[0]!; + old.emitLine({ id: 777, method: "item/tool/requestUserInput", params: { threadId: start.params.threadId, turnId: `tu-${start.params.threadId}`, itemId: "old-question", questions: [{ id: "q", header: "Choice", question: "Pick one", isOther: false, isSecret: false, options: [{ label: "a", description: "" }, { label: "b", description: "" }] }] } }); + for (let i = 0; i < 5 && !displayed; i++) await tick(); assert.equal(displayed, true); + const disposing = backend.dispose(), replacement = backend.listModels(); + try { + await running; await replacement; await disposing; + for (let i = 0; i < 5; i++) await tick(); + assert.equal(next.sent(m => m.id === 777 && !m.method).length, 0); + } finally { await disposing; await replacement; await backend.dispose(); await running; } +}); + +test("an old stale-auth failure cannot recover on or terminate a replacement", async () => { + const old = new FakeProcess(), next = new FakeProcess(), unintended = new FakeProcess(); + wire(old, new Set(["thread/start"])); wire(next); wire(unintended); + for (const peer of [next, unintended]) peer.stdin.on("data", bytes => { for (const line of bytes.toString().split("\n")) { + if (!line.trim()) continue; const m = JSON.parse(line); + if (m.method === "turn/start") setImmediate(() => peer.emitLine({ method: "turn/completed", params: { threadId: m.params.threadId, turn: { id: `tu-${m.params.threadId}`, status: "completed" } } })); + } }); + const spawn = fakeSpawn([old, next, unintended]), backend = new CodexBackend("fixture", spawn.spawn); + const running = backend.runTurn("synthetic", { cwd: "/fixture", mode: "chat", plan: false, model: "" }, collectSink().sink, new AbortController().signal); + await old.waitFor(line => line.includes('"thread/start"')); + const open = old.sent(m => m.method === "thread/start")[0]!; + old.emitLine({ id: open.id, error: { code: -32000, message: "Your access token could not be refreshed because you have signed in to another account." } }); + const disposing = backend.dispose(), replacement = backend.listModels().then(() => "success", () => "failed"); + try { + assert.equal((await running).status, "failed"); + assert.equal(next.killed, false); assert.equal(spawn.calls.length, 2); + assert.equal(next.sent(m => m.method === "thread/start" || m.method === "turn/start").length, 0); + } finally { + await disposing; await replacement; await backend.dispose(); await running; + } +}); + +test("an exited server with held stdio cannot be replaced before close", async () => { + const old = new FakeProcess(), next = new FakeProcess(); wire(old); wire(next); old.holdStdio = true; + const spawn = fakeSpawn([old, next]), backend = new CodexBackend("fixture", spawn.spawn); + let ended = false; + const running = backend.runTurn("synthetic", { cwd: "/fixture", mode: "chat", plan: false, model: "" }, collectSink().sink, new AbortController().signal).then(result => { ended = true; return result; }); + await old.waitFor(line => line.includes('"turn/start"')); await tick(); old.close(0); await tick(); + try { + await assert.rejects(backend.listModels(), /closing/); + assert.equal(spawn.calls.length, 1); assert.equal(ended, false); + old.releaseStdio(); assert.equal((await running).status, "failed"); + assert.deepEqual(await backend.listModels(), []); assert.equal(spawn.calls.length, 2); + } finally { old.releaseStdio(); await backend.dispose(); await running; } +}); + +test("a child that closes before initialization cannot report the previous agent version", async () => { + const old = new FakeProcess(), next = new FakeProcess(); wire(old); wire(next, new Set(["initialize"])); + const backend = new CodexBackend("fixture", fakeSpawn([old, next]).spawn); + await backend.listModels(); await backend.dispose(); + const pending = backend.listModels(); + await next.waitFor(line => line.includes('"initialize"')); next.close(0); + try { + await assert.rejects(pending, error => { + assert.equal((error as { detail: Record }).detail.userAgent, null); + return true; + }); + } finally { await backend.dispose(); } +}); From 017fb244bae02d8c73eb31dfb95dc64e6228cdeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Val=20Alexander=20=F0=9F=91=91?= Date: Sat, 14 Nov 2026 03:43:08 -0600 Subject: [PATCH 3/5] fix: retain Codex ownership at recovery and cleanup consumers Carry the server lease through health publication and the actual stale-auth restart/replay decision. Detach old cleanup registries before native withdrawal callbacks, and retain latest-issued identity across closure so old attempts cannot regain recovery authority. Validation: main build, 136 affected cases passed with 13 opt-in native/live skips, and one private Electron regression passed without retries. Full local qualification and protected delivery remain pending. --- .../desktop/src/main/engine/backends/codex.ts | 105 +++++++--- apps/desktop/test/codex-transport.test.ts | 183 ++++++++++++++++++ .../2026-11-14-codex-input-transport.md | 19 ++ docs/status.md | 2 + 4 files changed, 278 insertions(+), 31 deletions(-) create mode 100644 docs/reviews/2026-11-14-codex-input-transport.md diff --git a/apps/desktop/src/main/engine/backends/codex.ts b/apps/desktop/src/main/engine/backends/codex.ts index fa2f20cb..fea918d3 100644 --- a/apps/desktop/src/main/engine/backends/codex.ts +++ b/apps/desktop/src/main/engine/backends/codex.ts @@ -32,6 +32,15 @@ interface ServerLease { retired: boolean; } +interface ServerPreparation { + readonly readiness: Promise; + readonly owner: ServerLease | null; +} +interface AttemptReceipt { + readonly result: TurnResult; + readonly owner: ServerLease | null; +} + /** * Drives the Codex CLI through `codex app-server`, the JSON-RPC-over-stdio protocol the * official Codex App uses. One server process is shared by all Codex threads; each Modex @@ -56,6 +65,8 @@ export class CodexBackend implements Backend { private readonly changePaths = new Map(); /** Opaque id of the running app-server process; native thread, turn and item ids are unique only within it. */ private lease: ServerLease | null = null; + /** Retained across closure so an older queued result cannot regain recovery ownership. */ + private latestGeneration: string | null = null; /** * Server requests waiting on a person, keyed `generation:id`. Codex's `serverRequest/resolved` for one still * here means Codex settled it itself (codex-cli 0.163.0): the card is withdrawn and no answer is sent. @@ -106,6 +117,7 @@ export class CodexBackend implements Backend { let closed!: () => void; const lease: ServerLease = { child, generation: randomUUID(), closed: new Promise(resolve => { closed = resolve; }), userAgent: null, retired: false }; this.lease = lease; + this.latestGeneration = lease.generation; this.loaded.clear(); this.metadataConnections.clear(); this.threadModels.clear(); @@ -156,7 +168,15 @@ export class CodexBackend implements Backend { } private disconnect(error: Error): void { - if (this.child) for (const finish of this.writeReceipts.get(this.child) ?? []) finish(error); + const child = this.child; + const requests = [...this.openRequests.values()]; + const pending = [...this.pending.values()]; + const disconnects = [...this.disconnects]; + // Withdrawal callbacks can synchronously initialize a replacement. Detach the + // old generation's registries before any callback can register its new work. + this.openRequests.clear(); + this.pending.clear(); + this.disconnects.clear(); this.child = null; this.lease = null; this.ready = null; @@ -164,11 +184,10 @@ export class CodexBackend implements Backend { this.metadataConnections.clear(); this.changePaths.clear(); this.threadModels.clear(); - for (const request of this.openRequests.values()) request.controller.abort(); - this.openRequests.clear(); - for (const p of this.pending.values()) p.reject(error); - this.pending.clear(); - for (const finish of this.disconnects) finish(error); + if (child) for (const finish of this.writeReceipts.get(child) ?? []) finish(error); + for (const p of pending) p.reject(error); + for (const request of requests) request.controller.abort(); + for (const finish of disconnects) finish(error); } private dispatch(lease: ServerLease, line: string): void { @@ -259,6 +278,7 @@ export class CodexBackend implements Backend { stage: "stdin", pid: child.pid, ...(typeof code === "string" && /^[A-Z0-9_]{1,32}$/.test(code) ? { errno: code } : {}), }); if (this.child !== child) return error; // Consume old errors without touching the replacement. + this.lease!.retired = true; // This failure never becomes eligible for stale-auth replay after close. this.inputFailure = { child, error, closed: this.lease!.closed }; for (const finish of this.writeReceipts.get(child) ?? []) finish(error); for (const pending of this.pending.values()) pending.reject(error); @@ -276,8 +296,8 @@ export class CodexBackend implements Backend { } private requireLease(lease: ServerLease): void { + if (this.lease === lease && this.inputFailure?.child === lease.child) throw this.inputFailure.error; if (lease.retired || this.lease !== lease || this.child !== lease.child) throw new CodexError("Codex app-server generation is no longer available.", { stage: "server generation" }); - if (this.inputFailure?.child === lease.child) throw this.inputFailure.error; } private send(lease: ServerLease, o: unknown, written?: (error?: Error) => void): void { @@ -355,13 +375,15 @@ export class CodexBackend implements Backend { const report = (authentication: BackendHealth["authentication"], detail: string) => health(authentication, detail, "available", installed.version); let timer: ReturnType | undefined; try { - const result = await Promise.race([ + const read = await Promise.race([ this.ensure().then(async lease => { const value = await this.request<{ account: { type?: unknown } | null; requiresOpenaiAuth?: boolean }>(lease, "account/read", { refreshToken: false }, 5000); - this.requireLease(lease); return value; + this.requireLease(lease); return { lease, value }; }), new Promise((_, reject) => { timer = setTimeout(() => reject(new Error("timeout")), 5000); }), ]); + this.requireLease(read.lease); + const result = read.value; if (result.account?.type === "chatgpt") return report("authenticated", "ChatGPT signed in · model access unverified"); if (result.account?.type === "apiKey") return report("authenticated", "API key configured · model access unverified"); if (result.account !== null) return report("unknown", "Account status unavailable · check in Codex CLI"); @@ -407,16 +429,22 @@ export class CodexBackend implements Backend { } /** Ends the current server (and its commands); the next request starts a fresh one. */ - private restart(reason: string): void { + private canRecover(owner: ServerLease | null): owner is ServerLease { + return owner !== null && !owner.retired && owner.generation === this.latestGeneration && (this.lease === owner || this.lease === null); + } + + private restart(reason: string, expected?: ServerLease): boolean { + if (expected && !this.canRecover(expected)) return false; const child = this.child; - if (!child) return; + if (!child) return true; if (this.lease) this.lease.retired = true; if (this.inputFailure?.child === child) { try { killGroup(child); } catch { /* A repeated Stop still needs closure. */ } - return; + return true; } this.disconnect(new CodexError(reason, { pid: child.pid })); killGroup(child); + return true; } async dispose(): Promise { @@ -460,21 +488,32 @@ export class CodexBackend implements Backend { if (local && opts.resume && !this.loaded.has(opts.resume)) return { status: "failed", error: "This conversation's native session belongs to another Codex process. Select the local target again to start a fresh conversation." }; this.activeTurns++; try { - const first = await this.attempt(text, opts, sink, signal); + const firstReceipt = await this.attempt(text, opts, sink, signal), first = firstReceipt.result; // A local provider has no sign-in to refresh: its failures are reported once, never retried. if (local || first.status !== "failed" || signal.aborted || !STALE_AUTH.test(first.error ?? "")) return first; - // A fresh server reads ~/.codex/auth.json again. While other threads are mid-turn on this one, - // keep it and retry in place instead; never interrupt another thread to refresh auth. - const shared = this.activeTurns > 1 || this.pending.size > 0; + const owner = firstReceipt.owner; + const refused = (): TurnResult => ({ ...first, error: "Codex app-server generation is no longer available.", detail: { ...first.detail, supersededError: first.error, stage: "server generation" } }); + // Validate at the consumer, after attempt()'s own completion and closure await. + if (!this.canRecover(owner)) return refused(); + const shared = this.lease === owner && (this.activeTurns > 1 || this.pending.size > 0); const step = shared ? "retried once on the running codex app-server (other Codex turns were active)" : "restarted codex app-server so it reads the current sign-in, then retried once"; - sink.notice("info", "Codex's sign-in changed since it started. Reconnecting and retrying…"); - if (!shared) this.restart("Codex was restarted to pick up a new sign-in."); const opened = first.detail?.codexThreadId; const resume = typeof opened === "string" ? opened : opts.resume; + let prepared: ServerPreparation; + if (shared) prepared = this.prepareServer(owner); + else { + if (!this.restart("Codex was restarted to pick up a new sign-in.", owner)) return refused(); + // A synchronous shutdown subscriber may already have started a different server. + if (this.lease !== null) return refused(); + prepared = this.prepareServer(); + } + // Capture the recovery lease before reporting callbacks; they may synchronously dispose it. + // Preparation only initializes a server. Coding starts below, after these callbacks succeed. + sink.notice("info", "Codex's sign-in changed since it started. Reconnecting and retrying…"); sink.taskContextCapability?.({ state: "unavailable", reason: "configuration-not-bound" }); - const second = await this.attempt(text, { ...opts, resume }, sink, signal); + const second = (await this.attempt(text, { ...opts, resume }, sink, signal, prepared)).result; if (second.status !== "failed") return second; return { ...second, detail: { ...second.detail, firstAttempt: { error: first.error, ...first.detail } }, recovery: [`${step}: failed again`] }; } finally { @@ -482,18 +521,22 @@ export class CodexBackend implements Backend { } } - private async attempt(text: string, opts: TurnOptions, sink: TurnSink, signal: AbortSignal): Promise { - if (signal.aborted) return { status: "interrupted" }; - const readiness = this.ensure(), owner = this.lease; - try { - const result = await this.attemptOn(readiness, owner, text, opts, sink, signal); - // The stale-sign-in policy can recover its own server, never a disposed or superseded one. - // An unexpected close with no replacement keeps the established recovery behavior. - if (owner && result.status === "failed" && STALE_AUTH.test(result.error ?? "") && (owner.retired || (this.lease && this.lease !== owner))) { - return { ...result, error: "Codex app-server generation is no longer available.", detail: { ...result.detail, supersededError: result.error, stage: "server generation" } }; - } - return result; - } finally { + private prepareServer(expected?: ServerLease): ServerPreparation { + const readiness = expected + ? this.current(expected) && this.ready ? this.ready : Promise.reject(new CodexError("Codex app-server generation is no longer available.", { stage: "server generation" })) + : this.ensure(); + const owner = expected ?? this.lease; + // A reporting callback may fail before this preparation is consumed; it must not start coding + // or leave an unobserved setup rejection. The original promise still rejects for its consumer. + void readiness.catch(() => {}); + return { readiness, owner }; + } + + private async attempt(text: string, opts: TurnOptions, sink: TurnSink, signal: AbortSignal, prepared?: ServerPreparation): Promise { + if (signal.aborted && !prepared) return { result: { status: "interrupted" }, owner: null }; + const { readiness, owner } = prepared ?? this.prepareServer(); + try { return { result: await this.attemptOn(readiness, owner, text, opts, sink, signal), owner }; } + finally { // Only this attempt's failed generation fences its completion, never a replacement's. const fault = this.inputFailure; if (owner && fault?.child === owner.child) await fault.closed; diff --git a/apps/desktop/test/codex-transport.test.ts b/apps/desktop/test/codex-transport.test.ts index 2d954dc9..e6c78505 100644 --- a/apps/desktop/test/codex-transport.test.ts +++ b/apps/desktop/test/codex-transport.test.ts @@ -366,3 +366,186 @@ test("a child that closes before initialization cannot report the previous agent }); } finally { await backend.dispose(); } }); + +test("account status cannot publish after its validated reply is superseded", async () => { + const old = new FakeProcess(), next = new FakeProcess(); wire(old, new Set(["account/read"])); wire(next); + const servers = fakeSpawn([old, next]); + const spawn = ((file: string, args: string[], options: SpawnOptions) => { + if (args.includes("--version")) { + const version = new FakeProcess(); setImmediate(() => { version.emitRaw("codex-cli 0.164.0\n"); version.close(0); }); + return fakeSpawn(version).spawn(file, args, options); + } + return servers.spawn(file, args, options); + }) as typeof servers.spawn; + const backend = new CodexBackend("fixture", spawn), reading = backend.health(); + await old.waitFor(line => line.includes('"account/read"')); + const request = old.sent(m => m.method === "account/read")[0]!; + old.emitLine({ id: request.id, result: { account: { type: "chatgpt" } } }); + let disposing: Promise | undefined, replacement: Promise | undefined; + queueMicrotask(() => { disposing = backend.dispose(); replacement = backend.listModels(); }); + try { + assert.equal((await reading).authentication, "unknown"); + await disposing; await replacement; assert.equal(next.killed, false); + } finally { await disposing; await replacement; await backend.dispose(); } +}); + +for (const hops of Array.from({ length: 13 }, (_, index) => index)) test(`stale-auth recovery stays owned across microtask handoff ${hops}`, async () => { + const old = new FakeProcess(), replacementPeer = new FakeProcess(); + wire(old, new Set(["thread/start"])); wire(replacementPeer); + let initial = true, explicitReplacement = false, afterHandoff = false, unauthorizedStarts = 0; + const spawn = ((file: string, args: string[], options: SpawnOptions) => { + let peer: FakeProcess; + if (initial) { initial = false; peer = old; } + else if (explicitReplacement) { explicitReplacement = false; peer = replacementPeer; } + else { + if (afterHandoff) unauthorizedStarts++; + peer = new FakeProcess(); wire(peer); + peer.stdin.on("data", bytes => { for (const line of bytes.toString().split("\n")) { + if (!line.trim()) continue; const m = JSON.parse(line); + if (m.method === "turn/start") setImmediate(() => { if (!peer.killed) peer.emitLine({ method: "turn/completed", params: { threadId: m.params.threadId, turn: { id: `tu-${m.params.threadId}`, status: "completed" } } }); }); + } }); + } + return fakeSpawn(peer).spawn(file, args, options); + }) as typeof import("node:child_process").spawn; + // A faulty recovery may replay directly on the replacement; complete only that synthetic run. + replacementPeer.stdin.on("data", bytes => { for (const line of bytes.toString().split("\n")) { + if (!line.trim()) continue; const m = JSON.parse(line); + if (m.method === "turn/start") setImmediate(() => { if (!replacementPeer.killed) replacementPeer.emitLine({ method: "turn/completed", params: { threadId: m.params.threadId, turn: { id: `tu-${m.params.threadId}`, status: "completed" } } }); }); + } }); + const backend = new CodexBackend("fixture", spawn); + const running = backend.runTurn("synthetic", { cwd: "/fixture", mode: "chat", plan: false, model: "" }, collectSink().sink, new AbortController().signal); + await old.waitFor(line => line.includes('"thread/start"')); + const request = old.sent(m => m.method === "thread/start")[0]!; + old.emitLine({ id: request.id, error: { code: -32000, message: "Your access token could not be refreshed because you have signed in to another account." } }); + let disposing: Promise | undefined, replacement: Promise | undefined; + const handoff = (remaining: number): void => { + if (remaining) { queueMicrotask(() => handoff(remaining - 1)); return; } + afterHandoff = true; disposing = backend.dispose(); explicitReplacement = true; + replacement = backend.listModels().then(() => "success", () => "failed"); + }; + handoff(hops); + try { + assert.equal((await running).status, "failed"); await disposing; await replacement; + assert.equal(replacementPeer.killed, false); + assert.equal(unauthorizedStarts, 0); + assert.equal(replacementPeer.sent(m => m.method === "thread/start" || m.method === "thread/resume" || m.method === "turn/start").length, 0); + } finally { await disposing; await replacement; await backend.dispose(); await running; } +}); + +test("a transport fault after stale-auth rejection cannot replay the turn", async () => { + const old = new FakeProcess(), next = new FakeProcess(); wire(old, new Set(["thread/start"])); wire(next); + next.stdin.on("data", bytes => { for (const line of bytes.toString().split("\n")) { + if (!line.trim()) continue; const m = JSON.parse(line); + if (m.method === "turn/start") setImmediate(() => { if (!next.killed) next.emitLine({ method: "turn/completed", params: { threadId: m.params.threadId, turn: { id: `tu-${m.params.threadId}`, status: "completed" } } }); }); + } }); + const spawn = fakeSpawn([old, next]), backend = new CodexBackend("fixture", spawn.spawn); + const running = backend.runTurn("synthetic", { cwd: "/fixture", mode: "chat", plan: false, model: "" }, collectSink().sink, new AbortController().signal); + await old.waitFor(line => line.includes('"thread/start"')); + const request = old.sent(m => m.method === "thread/start")[0]!; + old.emitLine({ id: request.id, error: { code: -32000, message: "Your access token could not be refreshed." } }); + queueMicrotask(() => old.stdin.emit("error", Object.assign(new Error("write EPIPE"), { code: "EPIPE" }))); + try { + assert.equal((await running).status, "failed"); + assert.equal(spawn.calls.length, 1); assert.equal(next.killed, false); + } finally { await backend.dispose(); await running; } +}); + +test("recovery reporting callbacks cannot redirect the prepared attempt", async () => { + const old = new FakeProcess(), replacementPeer = new FakeProcess(), automatic: FakeProcess[] = []; + wire(old, new Set(["thread/start"])); wire(replacementPeer); + let initial = true, explicit = false; + const spawn = ((file: string, args: string[], options: SpawnOptions) => { + let peer: FakeProcess; + if (initial) { initial = false; peer = old; } + else if (explicit) { explicit = false; peer = replacementPeer; } + else { peer = new FakeProcess(); wire(peer); automatic.push(peer); } + return fakeSpawn(peer).spawn(file, args, options); + }) as typeof import("node:child_process").spawn; + const backend = new CodexBackend("fixture", spawn), { sink } = collectSink(); + let disposing: Promise | undefined, replacement: Promise | undefined; + const running = backend.runTurn("synthetic", { cwd: "/fixture", mode: "chat", plan: false, model: "" }, { ...sink, notice: (level, text) => { + sink.notice(level, text); + if (text.includes("sign-in changed")) { disposing = backend.dispose(); explicit = true; replacement = backend.listModels(); } + } }, new AbortController().signal); + await old.waitFor(line => line.includes('"thread/start"')); + const request = old.sent(m => m.method === "thread/start")[0]!; + old.emitLine({ id: request.id, error: { code: -32000, message: "Your access token could not be refreshed." } }); + try { + assert.equal((await running).status, "failed"); await disposing; await replacement; + assert.equal(replacementPeer.killed, false); + for (const peer of [...automatic, replacementPeer]) assert.equal(peer.sent(m => m.method === "thread/start" || m.method === "turn/start").length, 0); + } finally { await disposing; await replacement; await backend.dispose(); await running; } +}); + +test("failed recovery reporting cannot leave an orphan coding turn", async () => { + const old = new FakeProcess(), next = new FakeProcess(); wire(old, new Set(["thread/start"])); wire(next); + const backend = new CodexBackend("fixture", fakeSpawn([old, next]).spawn), { sink } = collectSink(); + const running = backend.runTurn("synthetic", { cwd: "/fixture", mode: "chat", plan: false, model: "" }, { ...sink, notice: (level, text) => { + if (text.includes("sign-in changed")) throw new Error("synthetic reporting failure"); + sink.notice(level, text); + } }, new AbortController().signal); + await old.waitFor(line => line.includes('"thread/start"')); + const request = old.sent(m => m.method === "thread/start")[0]!; + old.emitLine({ id: request.id, error: { code: -32000, message: "Your access token could not be refreshed." } }); + try { + await assert.rejects(running, /synthetic reporting failure/); + for (let i = 0; i < 5; i++) await tick(); + assert.equal(next.sent(m => m.method === "thread/start" || m.method === "turn/start").length, 0); + } finally { await backend.dispose(); } +}); + +test("a native withdrawal callback cannot reject replacement initialization", async () => { + const old = new FakeProcess(), next = new FakeProcess(); wire(old); wire(next, new Set(["initialize"])); + const backend = new CodexBackend("fixture", fakeSpawn([old, next]).spawn), { sink } = collectSink(); + let displayed = false, settled = false, replacement: Promise | undefined; + const running = backend.runTurn("synthetic", { cwd: "/fixture", mode: "chat", plan: false, model: "" }, { ...sink, question: (_request, withdrawn) => new Promise(resolve => { + displayed = true; + withdrawn?.addEventListener("abort", () => { + replacement = backend.listModels().then(() => { settled = true; return "success"; }, () => { settled = true; return "failed"; }); + resolve({ withdrawn: "native" }); + }, { once: true }); + }) }, new AbortController().signal); + await old.waitFor(line => line.includes('"turn/start"')); await tick(); + const start = old.sent(m => m.method === "turn/start")[0]!; + old.emitLine({ id: 778, method: "item/tool/requestUserInput", params: { threadId: start.params.threadId, turnId: `tu-${start.params.threadId}`, itemId: "old-question", questions: [{ id: "q", header: "Choice", question: "Pick one", isOther: false, isSecret: false, options: [{ label: "a", description: "" }, { label: "b", description: "" }] }] } }); + for (let i = 0; i < 5 && !displayed; i++) await tick(); assert.equal(displayed, true); + const disposing = backend.dispose(); await tick(); + const initialize = next.sent(m => m.method === "initialize")[0]!; + try { + assert.equal(settled, false, "old disconnect must not clear a newly registered initialization RPC"); + next.emitLine({ id: initialize.id, result: { userAgent: "replacement" } }); + assert.equal(await replacement, "success"); await disposing; await running; + assert.equal(next.killed, false); + } finally { + if (initialize) next.emitLine({ id: initialize.id, result: { userAgent: "replacement" } }); + await disposing; await backend.dispose(); await running; await replacement; + } +}); + +for (const intervening of [false, true]) test(`natural-close stale-auth recovery ${intervening ? "refuses a closed intervening generation" : "retains its single retry without a replacement"}`, async () => { + const old = new FakeProcess(), replacement = new FakeProcess(), recovery = new FakeProcess(); + wire(old, new Set(["thread/start"])); wire(replacement); wire(recovery); + recovery.stdin.on("data", bytes => { for (const line of bytes.toString().split("\n")) { + if (!line.trim()) continue; const m = JSON.parse(line); + if (m.method === "turn/start") setImmediate(() => recovery.emitLine({ method: "turn/completed", params: { threadId: m.params.threadId, turn: { id: `tu-${m.params.threadId}`, status: "completed" } } })); + } }); + const spawn = fakeSpawn(intervening ? [old, replacement, recovery] : [old, recovery]); + const backend = new CodexBackend("fixture", spawn.spawn); + const running = backend.runTurn("synthetic", { cwd: "/fixture", mode: "chat", plan: false, model: "" }, collectSink().sink, new AbortController().signal); + await old.waitFor(line => line.includes('"thread/start"')); + old.stderr.write("Your access token could not be refreshed."); + // Deliver the native close events synchronously so both generations close before + // the old rejected RPC's Promise continuations consume their stale-auth result. + old.exitCode = 1; old.emit("close", 1, null); + let reading: Promise | undefined; + if (intervening) { + reading = backend.listModels().catch(() => undefined); + replacement.exitCode = 0; replacement.emit("close", 0, null); + } + try { + assert.equal((await running).status, intervening ? "failed" : "completed"); + await reading; + assert.equal(spawn.calls.length, 2, "only the latest issued generation may recover after natural closure"); + assert.equal(replacement.sent(m => m.method === "thread/start" || m.method === "turn/start").length, 0); + } finally { await reading; await backend.dispose(); await running; } +}); diff --git a/docs/reviews/2026-11-14-codex-input-transport.md b/docs/reviews/2026-11-14-codex-input-transport.md new file mode 100644 index 00000000..aa620677 --- /dev/null +++ b/docs/reviews/2026-11-14-codex-input-transport.md @@ -0,0 +1,19 @@ +# Codex input transport and generation ownership + +## Observed failure + +PR #525's first merge-queue run `43565223289` tested `81fec064c5ef70ecd8dd827de3811871d2ef1dd6` and failed the Jev contrast Settings-click case. Retained Electron stderr contains an uncaught `write EPIPE` from Codex stdin; the process sample shows `NSAlert runModal`. Injected Writable failures and real subprocess pipes reproduced the uncaught transport error. These observations establish the transport defect, while the original queue timing remains unexplained. The PR remains unmerged and has not been requeued for this repair. + +## Candidate behavior + +Signed source commits `46764fe9060661f156b30f6e5a764aa57e61f9b9` and `860ecc78c4fbec0cd28d8465e0fa93b5d6567282`, plus this follow-up, install a persistent stdin error handler, settle pending RPCs and initialization write receipts, and report a fixed body-free transport failure with a bounded errno. A failed generation remains owned until its child closes. Broker delivery promises drain before request withdrawal, preserving an accepted answer as uncertain when delivery fails. Stop, disposal and late events cannot release or complete the faulted attempt early. + +Every asynchronous read, write, coding setup and terminal-cleanup operation retains its server lease. Health keeps that lease through its outer race before publishing account status. Stale-auth recovery carries the attempt owner to the actual restart/replay decision, prepares only its captured generation before reporting callbacks, and starts no coding turn if reporting fails. Cleanup detaches the old request registries before withdrawal callbacks can initialize a replacement. A latest-issued generation guard prevents old recovery permission returning after an intervening server starts and closes; ordinary recovery after natural closure without a replacement retains its single retry. + +## Validation and limits + +Meaningful failing controls precede the fixes: queued initialization and post-ready work crossing generations; stale-auth replay after replacement or input failure; old health publication after replacement; replacement initialization rejected by an old withdrawal callback; and old recovery becoming eligible after an intervening generation closed. The no-replacement natural-close control already passed before the latest-issued guard. The first Node fixture using `process.stdin.destroy()` was inconclusive and is retained separately; the valid real-pipe fixture uses synchronous fd0 reads without constructing Node's special stdin stream. + +The final affected suite passes 136 cases with 13 explicit opt-in local CLI/live skips and zero failures or cancellations. The actual private Electron regression passes once without retries: a real synthetic Node pipe fails, discovery reports the fixed error, and Settings remains responsive. Main compilation passes. Scoped agent source review found no remaining Critical or Important findings; the reviewer ran no tests or app sessions. Full local qualification, hosted checks, protected queue and independent actual-merge proof remain required. The earlier full run on `46764fe` passed build, workspace typechecks and 2,861 unit cases with 36 skips, then stopped at a stage boundary before E2E because later generation findings superseded it. That run is not a full qualification pass. + +Evidence lives outside disposable worktrees in `/Users/buns/Documents/Codex/2026-11-11/modex-closeout/`: the original queue artifacts and diagnosis, all first-failure logs, `codex-consumer-final-source-review.json`, `codex-latest-issued-generation-red.log`, `codex-latest-issued-generation-green-focused.log` and the private Electron results. The app check uses a synthetic Node peer in a private development profile and performs no inference. It does not establish installed-release, live-provider, physical-descendant, device or human acceptance, and no open-issue checkbox is changed on this evidence alone. diff --git a/docs/status.md b/docs/status.md index ce79c713..d93a82bb 100644 --- a/docs/status.md +++ b/docs/status.md @@ -147,6 +147,8 @@ before relying on a tick. ## In flight +- **Codex input-pipe failure and server ownership.** The owned `codex-pipe-lifecycle` candidate handles failed stdin writes, preserves uncertain native-answer receipts and retains ownership until the failed child closes. Continuations, account-health publication and stale-auth recovery stay bound to their originating server; old cleanup cannot reject a replacement's initialization. Main compilation and 136 affected tests pass with 13 opt-in skips; the private Electron regression passes without retries. Complete local qualification and protected delivery remain pending. PR #525's failed queue run has not been retried. [Evidence and limits](reviews/2026-11-14-codex-input-transport.md). + - **Code Review person-run seed and header-panel focus.** The owned source candidate repairs the actual human-review seed: long description, 101 paged comments, checks/stack/deployment and coherent diffs for all listed synthetic PRs. A normal Electron regression reproduced header Review panel Close dropping focus; Review/Merge Close and scoped Escape now return to their own split-pane button, preserving instructions Escape. Build, workspace typechecks, 2,588 units with 36 skips, 531 desktop E2E cases with 17 opt-in skips, eight site cases and four web cases pass. The isolated seeded overview/activity/panel/diff journey passes; the final fixture-only D5 scroll-density correction passes actual wide/narrow PageDown checks with unchanged diff/inbox positions, its final full units also pass, and protected delivery is pending. Human Result cells stay blank. The checklist uses explicit private-home General opt-in, removes C07 and describes conditional tree folding. Initial signed source `1b2c58b` passed hosted units and iPhone suites but failed two desktop E2E cases. The immutable-comparison fixture race is reproduced and repaired with two deterministic request orderings; the unexplained Markdown ArrowRight failure has validated observation-only diagnostics, with all original assertions retained. The follow-up full build, workspace/E2E typechecks and 2,588 units with 36 skips pass. Full follow-up E2E passes 532 desktop cases with 17 opt-in skips, eight site cases and four web cases, zero failures/retries; exact-head hosted/protected qualification remains pending. The original human candidate is independently preserved and natively qualified in a portable runtime, with human rows still blank. State-change panel focus/Escape remains a named follow-up. [Repair and limits](reviews/2026-11-13-cr-human-seed-focus.md), [CI follow-up](reviews/2026-11-13-pr522-ci-followup.md). - **Code Review line comments integration** (#513, #520). Protected #520 delivered signed squash `967b703768d040d64456c7d118548b97a3bae35b`, tree `6a241c26d089ac4d7daf3e4b30b23a28257010c5`, from qualified source `f9bef70`; #513 is closed as superseded and its owner’s checkout/branch remain preserved. Activity loads the first 100 REST review comments with truthful file/line/reply/unavailable-location labels and partial/capped states. The actual protected checkout passed 2,587 units with 36 skips, 533 desktop cases with fourteen opt-in skips, eight site cases and four web cases, with no retry annotations; both artifacts passed digest/ZIP checks. Corrected source CI passed all twelve checks, including 65 native iPhone units and 43 ordinary UI cases plus one intentional expected-failure control. The first iPhone job’s 30-minute cancellation remains retained; the reviewed whole-job correction to 45 minutes changed no test assertions, individual waits or retries. Hosted simulator, scripted-provider installed CLI and real authenticated-read evidence remain distinct from physical phone, human, live-provider/write and activation acceptance. Resolved-thread state, inline placement and further comment pages remain separate requirements. [Integration and delivery receipt](reviews/2026-11-13-cr-inline-integration.md); [new real-PR benchmark](reviews/2026-11-13-cr-real-pr-benchmark.md). From 2069430bff9378843de83038fbcb46cc67f540d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Val=20Alexander=20=F0=9F=91=91?= Date: Sun, 15 Nov 2026 17:47:54 -0600 Subject: [PATCH 4/5] test: isolate large context confirmation from OS metadata timing Supply deterministic missing-file selection and scope readers only for the 1,001-receipt fixture. Preserve the shared scheduler, production deadlines, original assertions and real-filesystem boundary cases. Assert every intended unique selection read and both scope reads. Main compilation and 18 context/scope tests pass; the identical slow-realpath control passes with zero realpath calls. Retain the original full failure and incomplete scope-only correction separately. Fresh full qualification remains pending. --- apps/desktop/test/task-context-checks.test.ts | 16 +++++++++++++++- docs/reviews/2026-11-14-codex-input-transport.md | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/apps/desktop/test/task-context-checks.test.ts b/apps/desktop/test/task-context-checks.test.ts index 74506c11..325a4833 100644 --- a/apps/desktop/test/task-context-checks.test.ts +++ b/apps/desktop/test/task-context-checks.test.ts @@ -17,7 +17,19 @@ for (const boundary of ["both", "missing-start", "late-start", "outside", "unkno if (boundary === "symlink-cwd" || boundary === "symlink-cd" || boundary === "symlink-parent") fs.symlinkSync(tmpdir("modex-check-outside-"), path.join(project.path, "external"), "dir"); if (boundary === "cd-inside") fs.mkdirSync(path.join(project.path, "inside")); let captureCalls = 0, finishStart: (() => void) | undefined; - const capture = new TaskContextCapture(); + let scopeReads = 0; + const selectionReads = new Set(); + const selectedPaths = new Set(Array.from({ length: boundary === "large-confirmation" ? 1001 : 0 }, (_, i) => `selected-${i}.ts`)); + // This case exercises 1,001 synthetic receipts, not OS metadata latency. Real + // canonical scope and its unchanged deadline are covered in task-context-scope. + const capture = new TaskContextCapture(boundary === "large-confirmation" ? { reader: async (cwd, relative) => { + assert.equal(cwd, project.path); assert.ok(selectedPaths.has(relative)); + assert.equal(selectionReads.has(relative), false); selectionReads.add(relative); + return { state: "unavailable", reason: "unreadable" }; + }, scopeReader: async (cwd, directory) => { + assert.equal(cwd, project.path); assert.equal(directory, project.path); + scopeReads++; return "project"; + } } : {}); capture.capture = async (_cwd, targets, source, phase) => { captureCalls++; if (boundary === "late-start" && phase === "check-start-observed") await new Promise(resolve => { finishStart = resolve; }); @@ -41,6 +53,7 @@ for (const boundary of ["both", "missing-start", "late-start", "outside", "unkno const thread = await runner.createThread(project.id, { backend: "codex", auto: false }); const opened = runner.openContextDraft({ kind: "thread", threadId: thread.id }); const selected = await runner.selectTaskContext(opened, boundary === "large-confirmation" ? Array.from({ length: 1001 }, (_, i) => ({ kind: "file" as const, path: `selected-${i}.ts` })) : [{ kind: "file", path: "README.md" }]); + if (boundary === "large-confirmation") assert.equal(selectionReads.size, 1001, "selection must retain every synthetic missing-file receipt without OS reads"); await runner.send(thread.id, "Check", [], "desktop", selected.binding); const items = runner.items(thread.id), user = items.find(item => item.kind === "user")!; assert.ok(user.kind === "user" && user.contextEvidence); @@ -56,6 +69,7 @@ for (const boundary of ["both", "missing-start", "late-start", "outside", "unkno assert.equal(check.eventIds.length, 2); assert.equal(new Set(check.eventIds).size, 2); assert.equal(check.exitCode, 0); assert.equal(check.relevance, "unestablished"); assert.equal(check.scope, boundary === "outside" || boundary === "cd-outside" || boundary === "symlink-cwd" || boundary === "symlink-cd" || boundary === "symlink-parent" ? "outside" : boundary === "unknown-cwd" || boundary === "relative-cwd" || boundary === "cwd-flag" ? "unknown" : "project"); + if (boundary === "large-confirmation") assert.equal(scopeReads, 2, "both native boundaries must establish scope for the intended project"); assert.ok(handlers.has("task:context"), "existing task context needs a scoped read command"); assert.ok(handlers.has("task:check-confirm"), "check relevance requires an explicit main-owned confirmation command"); const confirm = (_thread: string, _turn: string, revision: number, item: string, refs: string[]) => handlers.get("task:check-confirm")!({ threadId: _thread, turnId: _turn, expectedRevision: revision, itemId: item, refIds: refs }) as TaskContextEvidence; diff --git a/docs/reviews/2026-11-14-codex-input-transport.md b/docs/reviews/2026-11-14-codex-input-transport.md index aa620677..444abbac 100644 --- a/docs/reviews/2026-11-14-codex-input-transport.md +++ b/docs/reviews/2026-11-14-codex-input-transport.md @@ -16,4 +16,6 @@ Meaningful failing controls precede the fixes: queued initialization and post-re The final affected suite passes 136 cases with 13 explicit opt-in local CLI/live skips and zero failures or cancellations. The actual private Electron regression passes once without retries: a real synthetic Node pipe fails, discovery reports the fixed error, and Settings remains responsive. Main compilation passes. Scoped agent source review found no remaining Critical or Important findings; the reviewer ran no tests or app sessions. Full local qualification, hosted checks, protected queue and independent actual-merge proof remain required. The earlier full run on `46764fe` passed build, workspace typechecks and 2,861 unit cases with 36 skips, then stopped at a stage boundary before E2E because later generation findings superseded it. That run is not a full qualification pass. +Full qualification 2 on signed `017fb244bae02d8c73eb31dfb95dc64e6228cdeb` passed build and workspace typechecks, then stopped in units: 230 worktree and 16 core passes; desktop had 2,598 passes, 30 skips and one failure. The synthetic 1,001-receipt confirmation fixture expected project scope but received unknown scope; remaining unit groups and all E2E were not reached. An isolated diagnostic passed with scope reads below the two-second budget. A controlled 2.1-second OS metadata delay reproduced the exact assertion while the unchanged production deadline returned unknown, consistent with budget expiry in the original loaded run; that original deadline firing was not directly instrumented. A scope-only fixture correction passed 18 focused cases but still failed the slow-metadata control: human selection uses `captureSelection`, whose independent file reader retained the shared scheduler slots while OS reads were delayed. The completed fixture supplies deterministic missing-file results for exactly 1,001 validated unique paths as well as scope metadata for the intended project. It retains the production scheduler and all existing confirmation, verification and saved-state assertions, adding exact path/count and two-scope-read assertions. Main compilation and all 18 context/scope cases pass. The identical 2.1-second slow-realpath control now passes, records zero realpath calls and retains both project-scope results. Other real-directory cases and the separate canonical-path, symlink and deadline tests remain unchanged. Scoped agent source review reports no remaining Critical or Important issue; fresh full qualification remains pending. + Evidence lives outside disposable worktrees in `/Users/buns/Documents/Codex/2026-11-11/modex-closeout/`: the original queue artifacts and diagnosis, all first-failure logs, `codex-consumer-final-source-review.json`, `codex-latest-issued-generation-red.log`, `codex-latest-issued-generation-green-focused.log` and the private Electron results. The app check uses a synthetic Node peer in a private development profile and performs no inference. It does not establish installed-release, live-provider, physical-descendant, device or human acceptance, and no open-issue checkbox is changed on this evidence alone. From c2d4bcb47c74487c74674de65e2e8a9dcf369eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Val=20Alexander=20=F0=9F=91=91?= Date: Sun, 15 Nov 2026 18:30:24 -0600 Subject: [PATCH 5/5] docs: retain complete local transport qualification outcomes Record the frozen source, passing coverage, six-second Claude test cancellation, isolated diagnostic and repeated window-position failure. Preserve first failures and explicit native/live acceptance limits; require fresh hosted and protected queue qualification before delivery. --- docs/reviews/2026-11-14-codex-input-transport.md | 6 ++++++ docs/status.md | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/reviews/2026-11-14-codex-input-transport.md b/docs/reviews/2026-11-14-codex-input-transport.md index 444abbac..70ed9ed2 100644 --- a/docs/reviews/2026-11-14-codex-input-transport.md +++ b/docs/reviews/2026-11-14-codex-input-transport.md @@ -18,4 +18,10 @@ The final affected suite passes 136 cases with 13 explicit opt-in local CLI/live Full qualification 2 on signed `017fb244bae02d8c73eb31dfb95dc64e6228cdeb` passed build and workspace typechecks, then stopped in units: 230 worktree and 16 core passes; desktop had 2,598 passes, 30 skips and one failure. The synthetic 1,001-receipt confirmation fixture expected project scope but received unknown scope; remaining unit groups and all E2E were not reached. An isolated diagnostic passed with scope reads below the two-second budget. A controlled 2.1-second OS metadata delay reproduced the exact assertion while the unchanged production deadline returned unknown, consistent with budget expiry in the original loaded run; that original deadline firing was not directly instrumented. A scope-only fixture correction passed 18 focused cases but still failed the slow-metadata control: human selection uses `captureSelection`, whose independent file reader retained the shared scheduler slots while OS reads were delayed. The completed fixture supplies deterministic missing-file results for exactly 1,001 validated unique paths as well as scope metadata for the intended project. It retains the production scheduler and all existing confirmation, verification and saved-state assertions, adding exact path/count and two-scope-read assertions. Main compilation and all 18 context/scope cases pass. The identical 2.1-second slow-realpath control now passes, records zero realpath calls and retains both project-scope results. Other real-directory cases and the separate canonical-path, symlink and deadline tests remain unchanged. Scoped agent source review reports no remaining Critical or Important issue; fresh full qualification remains pending. +## Local qualification on `2069430` + +Signed `2069430bff9378843de83038fbcb46cc67f540d4`, tree `bd28d85487a95dabed27fac7e160145d7b89427c`, passed the full build and workspace typechecks. Across its frozen unit groups there are 2,891 passes, 36 explicit skips and one cancellation. The full `pnpm test` invocation stopped when the unchanged Claude-only unconfirmed-stop/worktree test reached its six-second test timeout; the remaining unit groups were then run separately. An isolated diagnostic of that unchanged case passed in 1.43 seconds with all ownership assertions and the six-second bound intact. Its full-run timeout cause is unproven. The Claude backend, runner and test files match main; this does not substitute the diagnostic for a passing full invocation. + +Desktop E2E completed with 546 passes, 17 explicit skips, one failure and no retries. The persistence case retained size 960×620 but reopened at 1157,1148 instead of 40,60, matching the position mismatch retained from the earlier CF-7 run on this host; its cause remains unproven. The window-state implementation, main window setup, persistence test and launch helper match main. No assertion, retry setting or Spaces preference changed. The actual Codex pipe-failure regression and both Jev Code Review contrast cases passed in this sequence. Site E2E passed all eight cases and web E2E all four when run separately after the desktop failure. All tracked source bytes stayed unchanged through these runs. The desktop archive contains 192 files and passed ZIP CRC and SHA-256 verification. Local full qualification is not green; fresh hosted qualification and the protected merge queue must validate the candidate before delivery. + Evidence lives outside disposable worktrees in `/Users/buns/Documents/Codex/2026-11-11/modex-closeout/`: the original queue artifacts and diagnosis, all first-failure logs, `codex-consumer-final-source-review.json`, `codex-latest-issued-generation-red.log`, `codex-latest-issued-generation-green-focused.log` and the private Electron results. The app check uses a synthetic Node peer in a private development profile and performs no inference. It does not establish installed-release, live-provider, physical-descendant, device or human acceptance, and no open-issue checkbox is changed on this evidence alone. diff --git a/docs/status.md b/docs/status.md index d93a82bb..a6007667 100644 --- a/docs/status.md +++ b/docs/status.md @@ -147,7 +147,7 @@ before relying on a tick. ## In flight -- **Codex input-pipe failure and server ownership.** The owned `codex-pipe-lifecycle` candidate handles failed stdin writes, preserves uncertain native-answer receipts and retains ownership until the failed child closes. Continuations, account-health publication and stale-auth recovery stay bound to their originating server; old cleanup cannot reject a replacement's initialization. Main compilation and 136 affected tests pass with 13 opt-in skips; the private Electron regression passes without retries. Complete local qualification and protected delivery remain pending. PR #525's failed queue run has not been retried. [Evidence and limits](reviews/2026-11-14-codex-input-transport.md). +- **Codex input-pipe failure and server ownership.** The owned `codex-pipe-lifecycle` candidate handles failed stdin writes, preserves uncertain native-answer receipts and retains ownership until the failed child closes. Continuations, account-health publication and stale-auth recovery stay bound to their originating server; old cleanup cannot reject a replacement's initialization. Main compilation, 136 affected tests with 13 opt-in skips and the private Electron regression pass; all 18 context/scope cases and the slow-metadata control pass. The frozen full local run retains one Claude test cancellation and one window-position E2E failure; site and web E2E pass. Fresh hosted qualification and protected delivery remain pending. PR #525's failed queue run has not been retried. [Evidence and limits](reviews/2026-11-14-codex-input-transport.md). - **Code Review person-run seed and header-panel focus.** The owned source candidate repairs the actual human-review seed: long description, 101 paged comments, checks/stack/deployment and coherent diffs for all listed synthetic PRs. A normal Electron regression reproduced header Review panel Close dropping focus; Review/Merge Close and scoped Escape now return to their own split-pane button, preserving instructions Escape. Build, workspace typechecks, 2,588 units with 36 skips, 531 desktop E2E cases with 17 opt-in skips, eight site cases and four web cases pass. The isolated seeded overview/activity/panel/diff journey passes; the final fixture-only D5 scroll-density correction passes actual wide/narrow PageDown checks with unchanged diff/inbox positions, its final full units also pass, and protected delivery is pending. Human Result cells stay blank. The checklist uses explicit private-home General opt-in, removes C07 and describes conditional tree folding. Initial signed source `1b2c58b` passed hosted units and iPhone suites but failed two desktop E2E cases. The immutable-comparison fixture race is reproduced and repaired with two deterministic request orderings; the unexplained Markdown ArrowRight failure has validated observation-only diagnostics, with all original assertions retained. The follow-up full build, workspace/E2E typechecks and 2,588 units with 36 skips pass. Full follow-up E2E passes 532 desktop cases with 17 opt-in skips, eight site cases and four web cases, zero failures/retries; exact-head hosted/protected qualification remains pending. The original human candidate is independently preserved and natively qualified in a portable runtime, with human rows still blank. State-change panel focus/Escape remains a named follow-up. [Repair and limits](reviews/2026-11-13-cr-human-seed-focus.md), [CI follow-up](reviews/2026-11-13-pr522-ci-followup.md).