Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(deep-scan): preserve worker configuration, retry and failure diagnostics (#2309)

Michael D'Angelo3a64935adc0d
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/executor.ts b/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
index d9ce81d57..64e92ba1f 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
@@ -127,10 +127,6 @@ export class CodexSdkWorkerExecutor implements CodexWorkerExecutor {
) {
const discovery = request.kind === "discovery" ? request : undefined;
const includePaths = this.modelSettings.artifactContext?.includePaths;
- const scopeInput = JSON.stringify({
- paths: includePaths,
- filePaths: this.modelSettings.artifactContext?.filePaths ?? [],
- });
const includePathsFile =
!discovery || includePaths === undefined
? undefined
@@ -151,7 +147,11 @@ export class CodexSdkWorkerExecutor implements CodexWorkerExecutor {
const childEnv = await snapshotWorkerEnvironment();
// Snapshot the SDK's per-scan config once for this coordinator, including resumes.
const runtimeSettings = await (this.runtimeSettings ??=
- workerRuntimeSettings(childEnv));
+ workerRuntimeSettings(childEnv).catch((error: unknown) => {
+ // A failed read produced no snapshot. Let the next worker retry it.
+ this.runtimeSettings = undefined;
+ throw error;
+ }));
for (const [name, value] of Object.entries(
runtimeSettings.environment ?? {},
)) {
@@ -189,7 +189,13 @@ export class CodexSdkWorkerExecutor implements CodexWorkerExecutor {
}
if (includePathsFile !== undefined) {
// Keep host-bound inputs beside the prompt, outside the worker's output root.
- await writePrivateFile(includePathsFile, scopeInput);
+ await writePrivateFile(
+ includePathsFile,
+ JSON.stringify({
+ paths: includePaths,
+ filePaths: this.modelSettings.artifactContext?.filePaths ?? [],
+ }),
+ );
}
// Keep one native configuration for the policy check and the worker turn.
// Worker-owned tool and permission settings take precedence over inheritance.
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/worker-runner.ts b/plugins/codex-security/mcp-app/src/deep-scan/worker-runner.ts
index ee3470df7..927387d5f 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/worker-runner.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/worker-runner.ts
@@ -173,7 +173,15 @@ export class DeepScanWorkerRunner {
);
}
- // Once SQLite accepts the scan, leave its result available for recovery.
+ // A validation success can still race with cancellation or a lease change.
+ // Report worker success only once SQLite accepts the result.
+ this.options.log({
+ event: "worker_succeeded",
+ scanId: run.scanId,
+ workerId,
+ kind: "discovery",
+ attempt: persisted.attempt,
+ });
this.options.log({
event: "discovery_accepted",
scanId: run.scanId,
@@ -299,13 +307,6 @@ export class DeepScanWorkerRunner {
if (signal.aborted) {
return await this.cancelAttempt(input, attempt, activeThreadId);
}
- this.options.log({
- event: "worker_succeeded",
- scanId: run.scanId,
- workerId: input.workerId,
- kind: "discovery",
- attempt,
- });
return {
status: "succeeded",
attempt,
@@ -390,6 +391,7 @@ export class DeepScanWorkerRunner {
kind: "discovery",
attempt,
count: delayMs,
+ reason: deepScanErrorMessage(normalized),
});
try {
await this.options.clock.sleep(delayMs, signal);
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
index 687f2a3c4..5805deb49 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
@@ -725,15 +725,15 @@ async function testCancellationClearsRetryWait() {
failFirstDiscoveryAttempt: true,
blockDiscoveryAfterCalls: 1,
});
- const sleepStarted = Promise.withResolvers<void>();
+ const sleepStarted = Promise.withResolvers<DeepScanLogEvent>();
const coordinator = createCoordinator(fixture, store, executor, {
clock: undefined,
log: (event) => {
- if (event.event === "worker_retry_scheduled") sleepStarted.resolve();
+ if (event.event === "worker_retry_scheduled") sleepStarted.resolve(event);
},
});
coordinator.start();
- await sleepStarted.promise;
+ assert.equal((await sleepStarted.promise).reason, "transient worker failure");
const callsAtCancellation = executor.discoveryCalls;
coordinator.cancel("cancel retry wait");
const terminal = await coordinator.wait(undefined, 5_000);
@@ -1098,7 +1098,11 @@ async function testCancellationDuringDiscoveryAcceptanceRejectsLateSuccess() {
events.some((event) => event.event === "coordinator_cleanup_settled"),
);
assert.equal(
- events.some((event) => event.event === "discovery_accepted"),
+ events.some(
+ (event) =>
+ event.event === "discovery_accepted" ||
+ event.event === "worker_succeeded",
+ ),
false,
);
const worker = [...store.workers.values()].find(
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
index c70fab0d7..606064431 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
@@ -60,7 +60,6 @@ import { createRequire, syncBuiltinESMExports } from "node:module";
import path from "node:path";
import { createInterface } from "node:readline";
import { rootCertificates } from "node:tls";
-import { importModule } from "./import-module.ts";
import type {
CodexWorkerDiagnostic,
@@ -72,6 +71,12 @@ import { testDeepScanContextSnapshots } from "./deep_scan_context_snapshot_cases
import { workerTelemetryFixture } from "./deep_scan_worker_telemetry_fixture.ts";
import { testWorkerCyberAccessSettings } from "./deep_scan_worker_settings_cases.ts";
+const sdkRequire = createRequire(import.meta.resolve("@openai/codex-sdk"));
+const nativeLauncher = path.join(
+ path.dirname(sdkRequire.resolve("@openai/codex/package.json")),
+ "bin",
+ "codex.js",
+);
const previousMarker = process.env.FAKE_CODEX_MARKER;
const ipcFrameError =
"code-mode delegate response exceeds the IPC frame limit: code-mode IPC frame length 76008279 exceeds 67108864 bytes";
@@ -119,6 +124,7 @@ try {
await check(t, testUnsupportedProviderSnapshotFailsBeforeLaunch);
await check(t, testWorkerCyberAccessSettings, "cyber access settings");
await check(t, testReportOnlyComparison);
+ await check(t, testRuntimeSnapshotReadRetry);
if (process.platform !== "win32") {
await check(
t,
@@ -1002,6 +1008,29 @@ async function testSdkInvocationAndThreadCapture() {
}, deniedWorkerPermissionProfile);
}
+async function testRuntimeSnapshotReadRetry() {
+ await withWorkerFixture(async (fixture, promptPath, workingDirectory) => {
+ const configPath = path.join(fixture.root, "runtime.toml");
+ const savedPath = process.env.CODEX_SECURITY_CONFIG_PATH;
+ process.env.CODEX_SECURITY_CONFIG_PATH = configPath;
+ try {
+ const executor = new CodexSdkWorkerExecutor({
+ parentSandbox: trustedParentSandbox,
+ });
+ const request = workerRequest(promptPath, workingDirectory);
+ await writeFile(promptPath, "fixture worker");
+ await assert.rejects(executor.run(request), { code: "ENOENT" });
+ await writeFile(configPath, 'model_reasoning_effort = "low"');
+ await executor.run(request);
+ assertConfigOverrides((await readJson(fixture.markerPath)).argv, {
+ model_reasoning_effort: "low",
+ });
+ } finally {
+ restoreEnv("CODEX_SECURITY_CONFIG_PATH", savedPath);
+ }
+ });
+}
+
async function testReportOnlyComparison() {
return withWorkerFixture(async (fixture, _promptPath, workingDirectory) => {
const executor = new CodexSdkWorkerExecutor({
@@ -1041,13 +1070,41 @@ async function testReportOnlyComparison() {
assertConfigOverrides(preflight.argv, {
"mcp_servers.codex-security.command": undefined,
});
- const servers = nativeConfigOverrides(invocation.argv)
+ const overrides = nativeConfigOverrides(invocation.argv);
+ const servers = overrides
.map((arg) => parseToml(arg).mcp_servers)
.findLast(Boolean);
assert.deepEqual(JSON.parse(JSON.stringify(servers)), {
"fixture.catalog": { enabled: false },
"codex-security": { enabled: false },
});
+ // Check the actual CLI parser. A TOML parser alone accepts quoted dotted
+ // override paths that the pinned native executable splits literally.
+ await writeFile(
+ path.join(fixture.root, "config.toml"),
+ '[mcp_servers."fixture.catalog"]\ncommand = "fixture-command"\n' +
+ '[mcp_servers.codex-security]\nurl = "https://example.invalid/mcp"\n',
+ );
+ const effective = spawnSync(
+ process.execPath,
+ [
+ nativeLauncher,
+ "mcp",
+ "get",
+ "fixture.catalog",
+ "--json",
+ ...overrides.flatMap((override) => ["--config", override]),
+ ],
+ {
+ encoding: "utf8",
+ cwd: fixture.root,
+ env: { ...process.env, CODEX_HOME: fixture.root },
+ },
+ );
+ assert.equal(effective.status, 0, effective.stderr);
+ const server = JSON.parse(effective.stdout);
+ assert.equal(server.enabled, false);
+ assert.equal(server.transport.command, "fixture-command");
});
}
@@ -1072,12 +1129,6 @@ async function testOpenAiCredentialsReachWorker() {
);
assert.equal(created.status, 0, created.stderr);
}
- const sdkRequire = createRequire(import.meta.resolve("@openai/codex-sdk"));
- const nativeLauncher = path.join(
- path.dirname(sdkRequire.resolve("@openai/codex/package.json")),
- "bin",
- "codex.js",
- );
const cases: {
openai?: string;
codex?: string;
diff --git a/sdk/typescript/src/deep-scan.ts b/sdk/typescript/src/deep-scan.ts
index 9c5dc7bec..1e171126d 100644
--- a/sdk/typescript/src/deep-scan.ts
+++ b/sdk/typescript/src/deep-scan.ts
@@ -18,6 +18,7 @@ import {
import { isRecord } from "./record.js";
import {
executablePathForSpawn,
+ isDisconnectedChildStdin,
PLUGIN_NAME,
runCodexCommand,
type CodexCommand,
@@ -484,12 +485,7 @@ function runEngine(
processError = error;
});
child.stdin.on("error", (error: NodeJS.ErrnoException) => {
- if (
- !["EPIPE", "ECONNRESET", "EOF", "ERR_STREAM_DESTROYED"].includes(
- error.code ?? "",
- )
- )
- processError = error;
+ if (!isDisconnectedChildStdin(error)) processError = error;
});
const result = new Promise<string>((resolve, reject) => {
child.once("close", (code) => {
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index bf1526db1..57f993513 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -3274,6 +3274,19 @@ export async function cleanupSdkDirectory(path: string): Promise<void> {
await rm(path, { recursive: true, force: true });
}
+// A child can finish or reject a request before the parent finishes writing it.
+export function isDisconnectedChildStdin(
+ error: NodeJS.ErrnoException,
+): boolean {
+ return [
+ "EPIPE",
+ "ECONNRESET",
+ "ENOTCONN",
+ "EOF",
+ "ERR_STREAM_DESTROYED",
+ ].includes(error.code ?? "");
+}
+
export async function runCodexCommand(
command: CodexCommand,
args: readonly string[],
@@ -3322,16 +3335,7 @@ export async function runCodexCommand(
processError = error;
});
child.stdin.on("error", (error: NodeJS.ErrnoException) => {
- if (
- ![
- "EPIPE",
- "ECONNRESET",
- "ENOTCONN",
- "EOF",
- "ERR_STREAM_DESTROYED",
- ].includes(error.code ?? "")
- )
- processError = error;
+ if (!isDisconnectedChildStdin(error)) processError = error;
});
child.once("close", (exitCode) =>
processError === undefined
diff --git a/sdk/typescript/tests-ts/direct-deep-scan.test.ts b/sdk/typescript/tests-ts/direct-deep-scan.test.ts
index 670b3996c..fe4b46310 100644
--- a/sdk/typescript/tests-ts/direct-deep-scan.test.ts
+++ b/sdk/typescript/tests-ts/direct-deep-scan.test.ts
@@ -350,6 +350,34 @@ test("preserves complete worker usage and cost after direct engine completion",
assert.equal((await events.next()).done, true);
});
+test("preserves the engine diagnostic when the child exits before reading its request", async () => {
+ const { root, pluginRoot } = await fixture();
+ await writeFile(
+ join(pluginRoot, "mcp", "server.mjs"),
+ 'process.stderr.write("synthetic engine startup failure"); process.exitCode = 23;',
+ );
+ // Exceed the control pipe capacity so the engine closes with a write pending.
+ const events = runDeepScan({
+ preflightCommand: { command: "/synthetic/codex" },
+ pluginRoot,
+ repository: root,
+ scanDir: root,
+ scanId: "startup-failure",
+ prompt: "Synthetic audit.",
+ signal: new AbortController().signal,
+ codexOptions: {
+ configOverrides: [`synthetic="${"x".repeat(2 * 1024 * 1024)}"`],
+ env: {
+ PATH: process.versions["bun"] ? process.env["PATH"]! : "",
+ CODEX_HOME: join(root, "codex-home"),
+ SYNTHETIC_THREAD_ID: "startup-session",
+ },
+ },
+ });
+ await events.next();
+ await assert.rejects(events.next(), /synthetic engine startup failure/);
+});
+
test("waits for the direct engine to stop when the scan is canceled", async () => {
const { root, pluginRoot } = await fixture();
const receipt = join(root, "engine.json");