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): require explicit scan-fatal worker errors (#996)

Dane Schneider39f2401957e5
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/errors.ts b/plugins/codex-security/mcp-app/src/deep-scan/errors.ts
index b91bdb881..1e734a9aa 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/errors.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/errors.ts
@@ -2,28 +2,22 @@ import { createHash } from "node:crypto";
const MAX_PERSISTED_ERROR_LENGTH = 2_400;
-const RATE_LIMIT_PATTERN =
- /\b(?:429|rate[ _-]*limit(?:ed|ing)?|too many requests)\b/iu;
-
-const ARTIFACT_MCP_STARTUP_TIMEOUT_PATTERN =
- /\b(?:cs_artifacts|codex_security_artifacts)\b[^\r\n]*(?:timed out handshaking with MCP server|timed out after \d+(?:\.\d+)?\s*(?:seconds?|s)\b|request timed out\b)/iu;
-
-const REMOTE_PLUGIN_AUTH_WARNING_PATTERN =
- /\bchatgpt authentication required (?:for remote plugin catalog|to sync remote plugins)(?:; api key auth is not supported)?/giu;
-
const STALE_COORDINATOR_GENERATION_MESSAGE =
"Deep Scan coordinator lease belongs to a newer generation.";
-const CYBERSECURITY_POLICY_REFUSAL_PATTERNS = [
- /\bflagged for possible cybersecurity risk\b/iu,
- /\bflagged for potentially high-risk cyber activity\b/iu,
- /\bcyber[_\s-]?policy\b/iu,
- /\b(?:cybersecurity|cyber)[ _-]*policy[ _-]*(?:violation|refusal|refused)\b/iu,
- /\b(?:content|safety)[ _-]*policy[ _-]*(?:violation|refusal|refused)\b/iu,
- /\b(?:refusal|refused)\b[^\n]*\b(?:cybersecurity|cyber|safety policy)\b/iu,
- /\b(?:cybersecurity|cyber|safety policy)\b[^\n]*\b(?:refusal|refused)\b/iu,
-] as const;
+// The SDK currently exposes only the message for turn errors. Match known
+// refusal messages exactly so repository output embedded in another error
+// cannot be mistaken for a refusal.
+const CYBERSECURITY_POLICY_REFUSAL_MESSAGES = new Set([
+ "Request blocked by cyberPolicy.",
+ "Request blocked by a safety policy violation.",
+ "This content was flagged for possible cybersecurity risk.",
+ "This content was flagged for potentially high-risk cyber activity.",
+ "This request has been flagged for possible cybersecurity risk.",
+ "This request has been flagged for potentially high-risk cyber activity.",
+]);
+/** Explicitly opt a confirmed scan-wide prerequisite failure out of retries. */
export class DeepScanNonRetryableError extends Error {
constructor(message: string, options?: ErrorOptions) {
super(message, options);
@@ -87,55 +81,13 @@ export function isStaleCoordinatorGenerationError(error: unknown): boolean {
/** A safety refusal retires the refused thread; it is not a broken scan. */
export function isCodexCybersecurityPolicyRefusal(error: unknown): boolean {
const message = error instanceof Error ? error.message : String(error);
- if (RATE_LIMIT_PATTERN.test(message)) return false;
- return CYBERSECURITY_POLICY_REFUSAL_PATTERNS.some((pattern) =>
- pattern.test(message),
- );
+ return CYBERSECURITY_POLICY_REFUSAL_MESSAGES.has(message);
}
export function classifyCodexWorkerError(error: unknown): Error {
- const normalized = error instanceof Error ? error : new Error(String(error));
- if (normalized instanceof DeepScanNonRetryableError) return normalized;
- const code =
- "code" in normalized && typeof normalized.code === "string"
- ? normalized.code
- : undefined;
- if (
- code === "ENOENT" ||
- code === "EACCES" ||
- code === "ENOEXEC" ||
- code === "EPERM"
- ) {
- return new DeepScanNonRetryableError(normalized.message, {
- cause: normalized,
- });
- }
- // API-key workers cannot catalog or sync remote plugins, but those warnings
- // are unrelated when their local artifact MCP server merely starts too slowly.
- const configurationMessage = ARTIFACT_MCP_STARTUP_TIMEOUT_PATTERN.test(
- normalized.message,
- )
- ? normalized.message.replace(REMOTE_PLUGIN_AUTH_WARNING_PATTERN, "")
- : normalized.message;
- if (
- isCodexConfigurationFailure(configurationMessage) ||
- isCodexCybersecurityPolicyRefusal(normalized)
- ) {
- return new DeepScanNonRetryableError(normalized.message, {
- cause: normalized,
- });
- }
- return normalized;
-}
-
-function isCodexConfigurationFailure(message: string): boolean {
- return [
- /Codex Exec exited with code 2:/i,
- /Codex Exec exited with code 1:[\s\S]*\(os error 2\)/i,
- /agents\.max_threads cannot be set when features\.multi_agent_v2 is enabled/i,
- /failed to (?:load|parse|read) (?:the )?(?:Codex )?config(?:uration)?/i,
- /(?:config(?:uration)?|config\.toml).*(?:invalid|parse|syntax|unknown)/i,
- /(?:invalid|unknown).*(?:--config|config(?:uration)? key)/i,
- /not logged in|authentication required|missing (?:an? )?(?:api key|credentials)/i,
- ].some((pattern) => pattern.test(message));
+ // OS error codes alone do not establish a permanent failure, and SDK errors
+ // can contain arbitrary command output. Leave both on the normal retry path
+ // unless a producer explicitly identifies the failure as nonretryable. The
+ // worker runner handles exact policy refusals without failing the scan.
+ return error instanceof Error ? error : new Error(String(error));
}
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/permission-profile-preflight.ts b/plugins/codex-security/mcp-app/src/deep-scan/permission-profile-preflight.ts
index 2de0d2f83..8bea56e65 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/permission-profile-preflight.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/permission-profile-preflight.ts
@@ -2,10 +2,7 @@ import { spawn, type ChildProcessWithoutNullStreams } from "node:child_process";
import { createInterface, type Interface } from "node:readline";
import { isDeepStrictEqual } from "node:util";
import { MCP_APP_VERSION } from "../version.js";
-import {
- classifyCodexWorkerError,
- DeepScanNonRetryableError,
-} from "./errors.js";
+import { DeepScanNonRetryableError } from "./errors.js";
import { executablePathForSpawn } from "./executable-path.js";
export const DEEP_SCAN_WORKER_PERMISSION_PROFILE_ID =
@@ -194,12 +191,6 @@ class AppServerPreflightClient {
if (!entry || typeof entry.id !== "string") {
throw malformedPreflightError();
}
- if (!Object.prototype.hasOwnProperty.call(entry, "allowed")) {
- throw unsupportedCodexApiError(
- this.options.codexPath,
- "permissionProfile/list.allowed",
- );
- }
if (typeof entry.allowed !== "boolean") throw malformedPreflightError();
if (
entry.description !== null &&
@@ -368,7 +359,12 @@ function verifyPreflightResult(
const actualProfile = permissions
? record(permissions[options.profileId])
: undefined;
- if (!config || !permissions || !actualProfile)
+ if (
+ !config ||
+ !permissions ||
+ !actualProfile ||
+ typeof config.default_permissions !== "string"
+ )
throw malformedPreflightError();
if (config.default_permissions !== options.profileId) {
@@ -474,6 +470,8 @@ function hasOwn(value: JsonRecord, key: string): boolean {
return Object.prototype.hasOwnProperty.call(value, key);
}
+// Verified permission incompatibilities explicitly stop the scan. A failed
+// transport attempt alone does not establish that the scan cannot proceed.
function disallowedProfileAllowlistError(
profileId: string,
): DeepScanNonRetryableError {
@@ -502,8 +500,8 @@ function profileCollisionError(profileId: string): DeepScanNonRetryableError {
);
}
-function malformedPreflightError(): DeepScanNonRetryableError {
- return new DeepScanNonRetryableError(
+function malformedPreflightError(): Error {
+ return new Error(
"Deep Scan cannot safely verify its read-only worker permission profile with this Codex configuration. Deep Scan did not run.",
);
}
@@ -529,11 +527,11 @@ function jsonRpcPreflightError(
codexPath: string,
method: string,
value: unknown,
-): DeepScanNonRetryableError {
+): Error {
const code = jsonRpcErrorCode(value);
if (code === -32601) return unsupportedCodexApiError(codexPath, method);
const codeDetail = code === undefined ? "" : " (JSON-RPC code " + code + ")";
- return new DeepScanNonRetryableError(
+ return new Error(
"Deep Scan cannot safely verify its read-only worker permission profile because " +
"the selected Codex executable " +
quotedExecutable(codexPath) +
@@ -551,17 +549,14 @@ function codexExecutableStartError(codexPath: string, error: Error): Error {
codexPath,
"could not start" + codeDetail,
);
- const classified = classifyCodexWorkerError(error);
- return classified instanceof DeepScanNonRetryableError
- ? new DeepScanNonRetryableError(message, { cause: classified })
- : new Error(message, { cause: classified });
+ return new Error(message, { cause: error });
}
function codexExecutableExitError(
codexPath: string,
code: number | null,
signal: NodeJS.Signals | null,
-): DeepScanNonRetryableError {
+): Error {
const detail =
code !== null
? "exited before permission-profile verification completed with code " +
@@ -573,22 +568,15 @@ function codexExecutableExitError(
return codexExecutableFailureError(codexPath, detail);
}
-function codexExecutableStdioError(
- codexPath: string,
-): DeepScanNonRetryableError {
+function codexExecutableStdioError(codexPath: string): Error {
return codexExecutableFailureError(
codexPath,
"could not exchange app-server JSON-RPC over stdio",
);
}
-function codexExecutableFailureError(
- codexPath: string,
- detail: string,
-): DeepScanNonRetryableError {
- return new DeepScanNonRetryableError(
- codexExecutableFailureMessage(codexPath, detail),
- );
+function codexExecutableFailureError(codexPath: string, detail: string): Error {
+ return new Error(codexExecutableFailureMessage(codexPath, detail));
}
function codexExecutableFailureMessage(
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 21966e167..fd442c5f7 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
@@ -640,13 +640,12 @@ export class DeepScanWorkerRunner {
);
}
const normalized = asError(error);
- const policyRefusal =
- input.kind === "discovery" &&
- isCodexCybersecurityPolicyRefusal(normalized);
const retryable = !(normalized instanceof DeepScanNonRetryableError);
+ const policyRefusal =
+ retryable && isCodexCybersecurityPolicyRefusal(normalized);
if (policyRefusal || !retryable || attempt === maximumAttempts) {
const replaceableFailureKind =
- input.kind === "discovery" && (policyRefusal || retryable)
+ input.kind === "discovery" && retryable
? policyRefusal
? "policy_refusal"
: validationStarted
diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_worker_failure_cases.mjs b/plugins/codex-security/mcp-app/tests/deep_scan_worker_failure_cases.mjs
new file mode 100644
index 000000000..f0d55341b
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/deep_scan_worker_failure_cases.mjs
@@ -0,0 +1,252 @@
+import assert from "node:assert/strict";
+import { readFile } from "node:fs/promises";
+
+export function createDeepScanWorkerFailureCases({
+ fixtureRun,
+ FakeStore,
+ FakeExecutor,
+ DeepScanCoordinator,
+ DeepScanNonRetryableError,
+ classifyCodexWorkerError,
+ deferred,
+ immediateClock,
+ workerIdFromPrompt,
+ promptContext,
+}) {
+ async function testRecoverableWorkerErrorsCannotFailScan() {
+ const failures = ["config unknown", "authentication required"].map(
+ (output) =>
+ new Error(
+ `Failed to parse item: ${JSON.stringify({
+ type: "item.completed",
+ item: {
+ id: "fixture-command",
+ type: "command_execution",
+ command: "synthetic-command",
+ aggregated_output: output,
+ },
+ })}`,
+ ),
+ );
+ failures.push(
+ ...["ENOENT", "EACCES", "ENOEXEC", "EPERM"].map((code) =>
+ Object.assign(new Error("fixture worker I/O failure"), { code }),
+ ),
+ );
+ for (const failure of failures) {
+ const fixture = await fixtureRun({
+ workers: 2,
+ subagents: 0,
+ stopAfterNoNew: 4,
+ stopAfterConsecutiveErrors: 2,
+ maxDiscoveryRuns: 4,
+ });
+ const store = new FakeStore(fixture.run);
+ const normalExecutor = new FakeExecutor();
+ const attempts = [];
+ const events = [];
+ const coordinator = new DeepScanCoordinator({
+ run: fixture.run,
+ store,
+ executor: {
+ async run(request) {
+ if (
+ request.kind === "discovery" &&
+ (await workerIdFromPrompt(request.promptPath)) ===
+ "discovery-0001"
+ ) {
+ attempts.push(request.resumeThreadId);
+ await request.onThreadStarted?.(
+ request.resumeThreadId ?? "fixture-parse-thread",
+ );
+ throw classifyCodexWorkerError(failure);
+ }
+ return normalExecutor.run(request);
+ },
+ },
+ pluginRoot: fixture.pluginRoot,
+ retryDelaysMs: [1, 3, 9],
+ clock: immediateClock,
+ log: (event) => events.push(event),
+ });
+ coordinator.start();
+
+ const terminal = await coordinator.wait(undefined, 5_000);
+
+ assert.equal(terminal?.status, "succeeded", terminal?.error);
+ assert.deepEqual(attempts, [
+ undefined,
+ "fixture-parse-thread",
+ "fixture-parse-thread",
+ "fixture-parse-thread",
+ ]);
+ const discoveries = [...store.workers.values()].filter(
+ (worker) => worker.kind === "discovery",
+ );
+ assert.equal(
+ discoveries.filter((worker) => worker.status === "succeeded").length,
+ 3,
+ "the sibling and subsequent discoveries must finish",
+ );
+ assert.equal(
+ discoveries.filter((worker) => worker.status === "failed").length,
+ 0,
+ );
+ assert.equal(
+ discoveries.find((worker) => worker.status === "canceled")
+ ?.replaceableFailureKind,
+ "transient_error",
+ );
+ assert.equal(
+ events.filter((event) => event.event === "discovery_worker_replaced")
+ .length,
+ 1,
+ );
+ assert.equal(store.failCalls, 0);
+ }
+ }
+
+ async function testPolicyRefusedReducerPreservesInputsAndCommittedAggregate() {
+ const fixture = await fixtureRun({
+ workers: 2,
+ subagents: 0,
+ stopAfterNoNew: 10,
+ stopAfterConsecutiveErrors: 2,
+ maxDiscoveryRuns: 4,
+ });
+ const store = new FakeStore(fixture.run);
+ const nextDiscovery = deferred();
+ const siblingDiscovery = deferred();
+ const normalExecutor = new FakeExecutor({
+ discoveryCandidateId: "candidate-1",
+ canonicalCandidateId: "candidate-1",
+ discoveryGates: {
+ "discovery-0003": nextDiscovery.promise,
+ "discovery-0004": siblingDiscovery.promise,
+ },
+ });
+ let committedResultPath;
+ let committedContent;
+ const failedAttempts = [];
+ const coordinator = new DeepScanCoordinator({
+ run: fixture.run,
+ store,
+ executor: {
+ async run(request) {
+ if (request.kind === "dedup") {
+ const label = (await promptContext(request.promptPath))
+ .reducerLabel;
+ if (label === "dedup-0002") {
+ failedAttempts.push(request.resumeThreadId);
+ await request.onThreadStarted?.("fixture-retired-reducer");
+ throw classifyCodexWorkerError(
+ new Error("Request blocked by cyberPolicy."),
+ );
+ }
+ if (label === "dedup-0003") {
+ assert.equal(request.resumeThreadId, undefined);
+ assert.equal(
+ request.artifactContext.deepReducer.previousReducerResultPath,
+ committedResultPath,
+ );
+ assert.deepEqual(
+ store.dedupClaims[2].workerIds,
+ store.dedupClaims[1].workerIds,
+ "the replacement must receive every uncommitted input",
+ );
+ assert.deepEqual(
+ await readFile(committedResultPath),
+ committedContent,
+ );
+ siblingDiscovery.resolve();
+ }
+ }
+ return normalExecutor.run(request);
+ },
+ },
+ pluginRoot: fixture.pluginRoot,
+ retryDelaysMs: [1, 3, 9],
+ clock: immediateClock,
+ });
+ coordinator.start();
+ await store.dedupCommitted.promise;
+ committedResultPath = store.dedupCommits[0].resultManifestPath;
+ committedContent = await readFile(committedResultPath);
+ nextDiscovery.resolve();
+
+ const terminal = await coordinator.wait(undefined, 5_000);
+
+ assert.equal(terminal?.status, "succeeded", terminal?.error);
+ assert.deepEqual(failedAttempts, [undefined]);
+ const discoveries = [...store.workers.values()].filter(
+ (worker) => worker.kind === "discovery",
+ );
+ assert.equal(discoveries.length, 4);
+ assert.equal(
+ discoveries.every((worker) => worker.status === "succeeded"),
+ true,
+ );
+ const retiredReducer = [...store.workers.values()].find(
+ (worker) => worker.kind === "dedup" && worker.status === "failed",
+ );
+ assert.equal(retiredReducer?.attempt, 1);
+ assert.deepEqual(await readFile(committedResultPath), committedContent);
+ const manifest = JSON.parse(await readFile(terminal.manifestPath, "utf8"));
+ assert.deepEqual(
+ manifest.findings.map((finding) => finding.provenance.candidateId),
+ ["candidate-1"],
+ );
+ assert.equal(store.failCalls, 0);
+ }
+
+ async function testNonRetryableReducerAbortsScanWithoutRetry(
+ failureMessage = "fixture fatal reducer failure",
+ ) {
+ const fixture = await fixtureRun({
+ workers: 2,
+ subagents: 0,
+ stopAfterNoNew: 10,
+ stopAfterConsecutiveErrors: 2,
+ maxDiscoveryRuns: 4,
+ });
+ const store = new FakeStore(fixture.run);
+ const normalExecutor = new FakeExecutor({ blockDiscoveryAfterCalls: 2 });
+ const attempts = [];
+ const sleeps = [];
+ const coordinator = new DeepScanCoordinator({
+ run: fixture.run,
+ store,
+ executor: {
+ async run(request) {
+ if (request.kind === "dedup") {
+ attempts.push(request.resumeThreadId);
+ throw new DeepScanNonRetryableError(failureMessage);
+ }
+ return normalExecutor.run(request);
+ },
+ },
+ pluginRoot: fixture.pluginRoot,
+ clock: {
+ now: immediateClock.now,
+ sleep: async (delayMs) => sleeps.push(delayMs),
+ },
+ });
+ coordinator.start();
+
+ const terminal = await coordinator.wait(undefined, 5_000);
+
+ assert.equal(terminal?.status, "failed");
+ assert.equal(terminal?.error, failureMessage);
+ assert.deepEqual(attempts, [undefined]);
+ assert.deepEqual(sleeps, []);
+ assert.equal(normalExecutor.runningDiscovery, 0);
+ assert.equal(store.failCalls, 1);
+ assert.equal(store.dedupCommits.length, 0);
+ }
+
+ return {
+ testRecoverableWorkerErrorsCannotFailScan,
+ testPolicyRefusedReducerPreservesInputsAndCommittedAggregate,
+ testNonRetryableReducerAbortsScanWithoutRetry,
+ };
+}
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.mjs b/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.mjs
index 60b35cf02..28596eccf 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.mjs
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.mjs
@@ -15,12 +15,17 @@ import path from "node:path";
import { build } from "esbuild";
import { testDeepScanDeadlines } from "./deep_scan_deadline_cases.mjs";
import { testDeepScanPublication } from "./deep_scan_publication_cases.mjs";
+import { createDeepScanWorkerFailureCases } from "./deep_scan_worker_failure_cases.mjs";
const bundle = await build({
bundle: true,
- entryPoints: [
- new URL("../src/deep-scan/registry.ts", import.meta.url).pathname,
- ],
+ stdin: {
+ contents: `
+ export * from "./registry.ts";
+ export { classifyCodexWorkerError } from "./errors.ts";
+ `,
+ resolveDir: new URL("../src/deep-scan/", import.meta.url).pathname,
+ },
format: "esm",
loader: { ".md": "text" },
platform: "node",
@@ -32,6 +37,7 @@ const {
DeepScanNonRetryableError,
DeepScanRemoteCoordinator,
DeepScanStartLock,
+ classifyCodexWorkerError,
startOrJoinDeepScanCoordinator,
} = await import(
`data:text/javascript;base64,${Buffer.from(bundle.outputFiles[0].contents).toString("base64")}`
@@ -934,7 +940,6 @@ async function testProviderCybersecurityRiskMessagesReplaceRefusedDiscoveryImmed
const executor = new FakeExecutor({
policyRefusalWorkers: ["discovery-0001"],
policyRefusalMessages: { "discovery-0001": message },
- nonRetryablePolicyRefusalWorkers: ["discovery-0001"],
dedupNewFindings: [1],
});
const sleeps = [];
@@ -1042,7 +1047,6 @@ async function testConsecutiveCybersecurityRefusalsFailAtConfiguredThreshold() {
"discovery-0001": "Request blocked by cyberPolicy.",
"discovery-0002": "Request blocked by cyberPolicy.",
},
- nonRetryablePolicyRefusalWorkers: ["discovery-0001", "discovery-0002"],
});
const coordinator = new DeepScanCoordinator({
run: fixture.run,
@@ -1282,7 +1286,9 @@ async function testTransientExecutionFailureResumesWorkerThread() {
);
}
-async function testConfigurationFailureDoesNotRetry() {
+async function testConfigurationFailureDoesNotRetry(
+ failureMessage = "fixture configuration failure",
+) {
const fixture = await fixtureRun({
workers: 1,
subagents: 0,
@@ -1290,7 +1296,10 @@ async function testConfigurationFailureDoesNotRetry() {
maxDiscoveryRuns: 1,
});
const store = new FakeStore(fixture.run);
- const executor = new FakeExecutor({ nonRetryableDiscovery: true });
+ const executor = new FakeExecutor({
+ nonRetryableDiscovery: true,
+ nonRetryableDiscoveryMessage: failureMessage,
+ });
const sleeps = [];
const coordinator = new DeepScanCoordinator({
run: fixture.run,
@@ -1305,8 +1314,10 @@ async function testConfigurationFailureDoesNotRetry() {
coordinator.start();
const terminal = await coordinator.wait(undefined, 5_000);
assert.equal(terminal?.status, "failed");
+ assert.equal(terminal?.error, failureMessage);
assert.equal(executor.discoveryCalls, 1);
assert.deepEqual(sleeps, []);
+ assert.equal([...store.workers.values()][0]?.status, "failed");
}
async function testFailureManifestWriteDoesNotMaskOriginalError() {
@@ -4160,12 +4171,8 @@ class FakeExecutor {
if (this.options.policyRefusalWorkers?.includes(workerId)) {
const message =
this.options.policyRefusalMessages?.[workerId] ??
- "Request refused due to cybersecurity policy violation";
- throw this.options.nonRetryablePolicyRefusalWorkers?.includes(
- workerId,
- )
- ? new DeepScanNonRetryableError(message)
- : new Error(message);
+ "Request blocked by cyberPolicy.";
+ throw new Error(message);
}
if (this.options.transientFailureWorkers?.includes(workerId)) {
throw new Error("transient worker failure");
@@ -4187,7 +4194,10 @@ class FakeExecutor {
throw new Error("transient worker failure");
}
if (this.options.nonRetryableDiscovery) {
- throw new DeepScanNonRetryableError("fixture configuration failure");
+ throw new DeepScanNonRetryableError(
+ this.options.nonRetryableDiscoveryMessage ??
+ "fixture configuration failure",
+ );
}
if (this.options.nonRetryableDiscoveryWorkers?.includes(workerId)) {
throw new DeepScanNonRetryableError("fixture configuration failure");
@@ -4540,6 +4550,23 @@ function boundedFixtureErrorText(message, maximum) {
return message.slice(0, maximum - suffix.length) + suffix;
}
+const {
+ testRecoverableWorkerErrorsCannotFailScan,
+ testPolicyRefusedReducerPreservesInputsAndCommittedAggregate,
+ testNonRetryableReducerAbortsScanWithoutRetry,
+} = createDeepScanWorkerFailureCases({
+ fixtureRun,
+ FakeStore,
+ FakeExecutor,
+ DeepScanCoordinator,
+ DeepScanNonRetryableError,
+ classifyCodexWorkerError,
+ deferred,
+ immediateClock,
+ workerIdFromPrompt,
+ promptContext,
+});
+
try {
await testCappedQueueAndSerialDedup();
await testStandardWorkersReceiveExistingFalsePositiveFeedback();
@@ -4581,11 +4608,13 @@ try {
await testRateLimitAndUnrelatedRefusalsRetainTransientRecovery();
await testConsecutiveCybersecurityRefusalsFailAtConfiguredThreshold();
await testExhaustedTransientDiscoveryIsReplaced();
+ await testRecoverableWorkerErrorsCannotFailScan();
await testSuccessfulDiscoveryResetsConsecutiveFailureThreshold();
await testExhaustedInvalidDiscoveryArtifactsAreReplaced();
await testExhaustedMalformedDiscoveryDoesNotRemainPublishable();
await testTransientExecutionFailureResumesWorkerThread();
await testConfigurationFailureDoesNotRetry();
+ await testConfigurationFailureDoesNotRetry("Request blocked by cyberPolicy.");
await testFailureManifestWriteDoesNotMaskOriginalError();
await testFinishPersistenceFailureRewritesManifestAsFailure();
await testLostFinishResponseReplaysWithoutOverwritingSuccessManifest();
@@ -4601,6 +4630,11 @@ try {
await testInvalidReducerResultRetriesFromSnapshot(true);
await testMissingReducerResultResumesExistingThread();
await testExhaustedReducerIsReplacedAtDiscoveryLimit();
+ await testPolicyRefusedReducerPreservesInputsAndCommittedAggregate();
+ await testNonRetryableReducerAbortsScanWithoutRetry();
+ await testNonRetryableReducerAbortsScanWithoutRetry(
+ "Request blocked by cyberPolicy.",
+ );
await testExhaustedReducerPreservesCommittedArtifacts();
await testCommittedAggregateIsNotSalvagedWhenUntrusted("missing-owner");
await testCommittedAggregateIsNotSalvagedWhenUntrusted("wrong-scan");
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.mjs b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.mjs
index 041b10c39..70c0356d6 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.mjs
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.mjs
@@ -48,7 +48,11 @@ const errorsBundle = await build({
platform: "node",
write: false,
});
-const { classifyCodexWorkerError } = await import(
+const {
+ classifyCodexWorkerError,
+ DeepScanNonRetryableError,
+ isCodexCybersecurityPolicyRefusal,
+} = await import(
`data:text/javascript;base64,${Buffer.from(errorsBundle.outputFiles[0].contents).toString("base64")}`
);
const temporaryRoots = [];
@@ -105,9 +109,10 @@ try {
await testStreamTerminationWithoutTerminalEventFails();
await testCompletedWorkerSettlesWithoutWaitingForProcessExit();
await testAbortPropagation();
- await testConfigurationFailureIsNonRetryable();
- await testThreadStartConfigurationFailureIsNonRetryable();
- await testPolicyFailuresAreNonRetryable();
+ await testUnstructuredConfigurationFailureRemainsRetryable();
+ await testUnstructuredThreadStartFailureRemainsRetryable();
+ await testPolicyFailuresRemainWorkerErrors();
+ await testMalformedCommandEventsRemainRetryable();
await testRateLimitPolicyFailureRemainsRetryable();
await testArtifactStartupTimeoutClassification();
}
@@ -169,8 +174,9 @@ try {
),
path.join(originalCwd, "codex"),
);
- testSpawnPermissionErrorsAreNonRetryable();
- testTextualMissingPathErrorsAreNonRetryable();
+ testOsErrorCodesRemainRetryable();
+ testTextualMissingPathErrorsRemainRetryable();
+ testWorkerErrorClassificationPreservesExplicitFailures();
await testWindowsAppsCodexFallsBackToRelocatedBinary();
await testWindowsNpmPackageResolution();
await testWindowsNpmPackageResolution("managed");
@@ -187,16 +193,16 @@ try {
);
}
-function testSpawnPermissionErrorsAreNonRetryable() {
+function testOsErrorCodesRemainRetryable() {
for (const code of ["ENOENT", "EACCES", "ENOEXEC", "EPERM"]) {
const original = Object.assign(new Error(`spawn codex ${code}`), { code });
const classified = classifyCodexWorkerError(original);
- assert.equal(classified.name, "DeepScanNonRetryableError");
- assert.equal(classified.cause, original);
+ assert.equal(classified, original);
+ assert.equal(classified.name, "Error");
}
}
-function testTextualMissingPathErrorsAreNonRetryable() {
+function testTextualMissingPathErrorsRemainRetryable() {
for (const diagnostic of [
"Error: No such file or directory (os error 2)",
"Error: The system cannot find the file specified. (os error 2)",
@@ -205,8 +211,69 @@ function testTextualMissingPathErrorsAreNonRetryable() {
["Codex Exec exited with code 1:", diagnostic].join("\n"),
);
const classified = classifyCodexWorkerError(original);
- assert.equal(classified.name, "DeepScanNonRetryableError");
- assert.equal(classified.cause, original);
+ assert.equal(classified, original);
+ }
+}
+
+function testWorkerErrorClassificationPreservesExplicitFailures() {
+ const explicit = new DeepScanNonRetryableError(
+ "Explicitly rejected worker permission profile.",
+ );
+ assert.equal(classifyCodexWorkerError(explicit), explicit);
+
+ const refusalMessages = [
+ "Request blocked by cyberPolicy.",
+ "Request blocked by a safety policy violation.",
+ "This content was flagged for possible cybersecurity risk.",
+ "This content was flagged for potentially high-risk cyber activity.",
+ "This request has been flagged for possible cybersecurity risk.",
+ "This request has been flagged for potentially high-risk cyber activity.",
+ ];
+ for (const message of refusalMessages) {
+ const original = new Error(message);
+ const classified = classifyCodexWorkerError(original);
+ assert.equal(classified, original);
+ assert.equal(classified.name, "Error");
+ assert.equal(isCodexCybersecurityPolicyRefusal(classified), true);
+
+ const explicitRefusal = new DeepScanNonRetryableError(message);
+ assert.equal(classifyCodexWorkerError(explicitRefusal), explicitRefusal);
+
+ for (const unrecognized of [
+ `Source fixture: ${message}`,
+ `${message} Source fixture.`,
+ JSON.stringify({ message }),
+ `Codex Exec exited with code 1: ${message}`,
+ `429 Too Many Requests: ${message}`,
+ ` ${message}`,
+ `${message}\n`,
+ message.toLowerCase(),
+ ]) {
+ const unrelated = new Error(unrecognized);
+ assert.equal(classifyCodexWorkerError(unrelated), unrelated);
+ assert.equal(isCodexCybersecurityPolicyRefusal(unrelated), false);
+ }
+ }
+
+ for (const message of [
+ "config parser handles unknown keys",
+ "failed to load configuration: invalid value",
+ "agents.max_threads cannot be set when features.multi_agent_v2 is enabled",
+ "authentication required",
+ "not logged in",
+ "missing API key",
+ "Codex Exec exited with code 2: failed to load configuration: invalid value",
+ "chatgpt authentication required for remote plugin catalog; api key auth is not supported",
+ "chatgpt authentication required to sync remote plugins; api key auth is not supported",
+ ]) {
+ const original = new Error(message);
+ assert.equal(classifyCodexWorkerError(original), original);
+ }
+ for (const code of ["ECONNRESET", "EPIPE", "ETIMEDOUT", "cyber_policy"]) {
+ const original = Object.assign(new Error("Unrecognized worker failure"), {
+ code,
+ });
+ assert.equal(classifyCodexWorkerError(original), original);
}
}
@@ -1552,7 +1619,7 @@ async function testCompletedWorkerSettlesWithoutWaitingForProcessExit() {
}
}
-async function testConfigurationFailureIsNonRetryable() {
+async function testUnstructuredConfigurationFailureRemainsRetryable() {
const fixture = await fakeCodexFixture();
const previousPath = process.env.CODEX_CLI_PATH;
process.env.CODEX_CLI_PATH = fixture.executablePath;
@@ -1571,14 +1638,16 @@ async function testConfigurationFailureIsNonRetryable() {
subagents: 0,
signal: new AbortController().signal,
}),
- (error) => error?.name === "DeepScanNonRetryableError",
+ (error) =>
+ error?.name === "Error" &&
+ error.message.startsWith("Codex Exec exited with code 2:"),
);
} finally {
restoreEnv("CODEX_CLI_PATH", previousPath);
}
}
-async function testThreadStartConfigurationFailureIsNonRetryable() {
+async function testUnstructuredThreadStartFailureRemainsRetryable() {
const fixture = await fakeCodexFixture();
const previousPath = process.env.CODEX_CLI_PATH;
process.env.CODEX_CLI_PATH = fixture.executablePath;
@@ -1597,19 +1666,23 @@ async function testThreadStartConfigurationFailureIsNonRetryable() {
subagents: 0,
signal: new AbortController().signal,
}),
- (error) => error?.name === "DeepScanNonRetryableError",
+ (error) =>
+ error?.name === "Error" &&
+ error.message.startsWith("Codex Exec exited with code 1:"),
);
} finally {
restoreEnv("CODEX_CLI_PATH", previousPath);
}
}
-async function testPolicyFailuresAreNonRetryable() {
+async function testPolicyFailuresRemainWorkerErrors() {
for (const prompt of [
"CYBER_POLICY_ERROR",
"SAFETY_POLICY_ERROR",
"CYBERSECURITY_RISK_ERROR",
"HIGH_RISK_CYBER_ACTIVITY_ERROR",
+ "UPSTREAM_CYBERSECURITY_RISK_ERROR",
+ "UPSTREAM_HIGH_RISK_CYBER_ACTIVITY_ERROR",
]) {
const fixture = await fakeCodexFixture();
const previousPath = process.env.CODEX_CLI_PATH;
@@ -1629,7 +1702,8 @@ async function testPolicyFailuresAreNonRetryable() {
subagents: 0,
signal: new AbortController().signal,
}),
- (error) => error?.name === "DeepScanNonRetryableError",
+ (error) =>
+ error?.name === "Error" && isCodexCybersecurityPolicyRefusal(error),
);
} finally {
restoreEnv("CODEX_CLI_PATH", previousPath);
@@ -1657,7 +1731,7 @@ async function testRateLimitPolicyFailureRemainsRetryable() {
signal: new AbortController().signal,
}),
(error) =>
- error?.name !== "DeepScanNonRetryableError" &&
+ error?.name === "Error" &&
/429 Too Many Requests/.test(error?.message ?? ""),
);
} finally {
@@ -1665,6 +1739,54 @@ async function testRateLimitPolicyFailureRemainsRetryable() {
}
}
+async function testMalformedCommandEventsRemainRetryable() {
+ for (const output of [
+ "ordinary source text",
+ "config parser handles unknown keys",
+ "authentication required",
+ "Request blocked by cyberPolicy.",
+ "This request has been flagged for possible cybersecurity risk.",
+ ]) {
+ const fixture = await fakeCodexFixture();
+ const previousPath = process.env.CODEX_CLI_PATH;
+ process.env.CODEX_CLI_PATH = fixture.executablePath;
+ try {
+ const promptPath = path.join(fixture.root, "prompt.md");
+ const workingDirectory = path.join(fixture.root, "artifacts");
+ await mkdir(workingDirectory);
+ await writeFile(
+ promptPath,
+ `MALFORMED_COMMAND_EVENT\n${JSON.stringify(output)}\n`,
+ );
+ let threadId;
+ await assert.rejects(
+ new CodexSdkWorkerExecutor({
+ parentSandbox: trustedParentSandbox,
+ }).run({
+ kind: "discovery",
+ promptPath,
+ workingDirectory,
+ subagents: 0,
+ signal: new AbortController().signal,
+ onThreadStarted: (value) => {
+ threadId = value;
+ },
+ }),
+ (error) =>
+ error?.name === "Error" &&
+ error.cause instanceof SyntaxError &&
+ error.message.startsWith("Failed to parse item: ") &&
+ error.message.includes('"id":"fixture-command"') &&
+ error.message.includes(JSON.stringify(output)),
+ `Malformed command output must remain retryable: ${output}`,
+ );
+ assert.equal(threadId, "fixture-thread-id");
+ } finally {
+ restoreEnv("CODEX_CLI_PATH", previousPath);
+ }
+ }
+}
+
async function testArtifactStartupTimeoutClassification() {
for (const { prompt, retryable } of [
{ prompt: "ARTIFACT_MCP_STARTUP_TIMEOUT", retryable: true },
@@ -1701,29 +1823,29 @@ async function testArtifactStartupTimeoutClassification() {
},
{
prompt: "ARTIFACT_MCP_STARTUP_TIMEOUT_WITH_MISSING_API_KEY",
- retryable: false,
+ retryable: true,
},
{
prompt: "ARTIFACT_MCP_STARTUP_TIMEOUT_WITH_POLICY_REFUSAL",
- retryable: false,
+ retryable: true,
},
{
prompt:
"ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_SYNC_AUTH_WARNING_WITH_MISSING_API_KEY",
- retryable: false,
+ retryable: true,
},
{
prompt:
"ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_BOTH_AUTH_WARNINGS_WITH_POLICY_REFUSAL",
- retryable: false,
+ retryable: true,
},
- { prompt: "OTHER_MCP_STARTUP_TIMEOUT", retryable: false },
+ { prompt: "OTHER_MCP_STARTUP_TIMEOUT", retryable: true },
{
prompt: "OTHER_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_SYNC_AUTH_WARNING",
- retryable: false,
+ retryable: true,
},
- { prompt: "CATALOG_AUTH_ONLY", retryable: false },
- { prompt: "SYNC_AUTH_ONLY", retryable: false },
+ { prompt: "CATALOG_AUTH_ONLY", retryable: true },
+ { prompt: "SYNC_AUTH_ONLY", retryable: true },
]) {
const fixture = await fakeCodexFixture();
const previousPath = process.env.CODEX_CLI_PATH;
@@ -1743,7 +1865,7 @@ async function testArtifactStartupTimeoutClassification() {
subagents: 0,
signal: new AbortController().signal,
}),
- (error) => (error?.name !== "DeepScanNonRetryableError") === retryable,
+ (error) => (error?.name === "Error") === retryable,
`${prompt} should ${retryable ? "remain retryable" : "remain terminal"}`,
);
} finally {
@@ -1937,6 +2059,12 @@ async function fakeCodexFixture(
"const resumeIndex = process.argv.indexOf('resume');",
"const threadId = resumeIndex === -1 ? 'fixture-thread-id' : process.argv[resumeIndex + 1];",
"console.log(JSON.stringify({ type: 'thread.started', thread_id: threadId }));",
+ "if (stdin.includes('MALFORMED_COMMAND_EVENT')) {",
+ " const output = JSON.parse(stdin.split('\\n')[1]);",
+ " const event = { type: 'item.completed', item: { id: 'fixture-command', type: 'command_execution', command: 'cat example.ts', aggregated_output: output, exit_code: 0, status: 'completed' } };",
+ " console.log(JSON.stringify(event).slice(0, -1));",
+ " process.exit(0);",
+ "}",
"const permissionProfileFallbackWarning = 'Configured value for `permission_profile` is disallowed by requirements; falling back from `codex_security_deep_scan_worker` to required value `:read-only`.';",
"if (stdin.includes('PERMISSION_PROFILE_FALLBACK_ITEM')) console.log(JSON.stringify({ type: 'item.completed', item: { id: 'warning-1', type: 'error', message: permissionProfileFallbackWarning } }));",
"if (stdin.includes('PERMISSION_PROFILE_FALLBACK_EVENT')) console.log(JSON.stringify({ type: 'error', message: permissionProfileFallbackWarning }));",
@@ -1944,6 +2072,8 @@ async function fakeCodexFixture(
"if (stdin.includes('RATE_LIMIT_CYBER_POLICY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: '429 Too Many Requests: Request blocked by cyberPolicy.' } })); process.exit(0); }",
"if (stdin.includes('CYBER_POLICY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'Request blocked by cyberPolicy.' } })); process.exit(0); }",
"if (stdin.includes('SAFETY_POLICY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'Request blocked by a safety policy violation.' } })); process.exit(0); }",
+ "if (stdin.includes('UPSTREAM_CYBERSECURITY_RISK_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'This request has been flagged for possible cybersecurity risk.' } })); process.exit(0); }",
+ "if (stdin.includes('UPSTREAM_HIGH_RISK_CYBER_ACTIVITY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'This request has been flagged for potentially high-risk cyber activity.' } })); process.exit(0); }",
"if (stdin.includes('CYBERSECURITY_RISK_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'This content was flagged for possible cybersecurity risk.' } })); process.exit(0); }",
"if (stdin.includes('HIGH_RISK_CYBER_ACTIVITY_ERROR')) { console.log(JSON.stringify({ type: 'turn.failed', error: { message: 'This content was flagged for potentially high-risk cyber activity.' } })); process.exit(0); }",
"if (stdin.includes('RETRYABLE_STREAM_ERROR')) console.log(JSON.stringify({ type: 'error', message: 'Reconnecting... 2/5 (stream disconnected before completion: websocket closed by server before response.completed)' }));",
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.mjs b/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.mjs
index 8fa4d8487..f090e3acd 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.mjs
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.mjs
@@ -64,11 +64,15 @@ await testDisallowedProfileGivesAdminGuidance();
await testOtherManagedPolicyRejectionIsGeneric();
await testMergedProfileCollisionFailsClosed();
await testLiteralProtoKeyCollisionFailsClosed();
+await testSelectedProfileClassificationRequiresVerifiedString();
await testMalformedAndUnsupportedResponsesFailClosed();
await testEarlyExecutableExitIsNotVersionError();
-await testNonVersionJsonRpcFailureIsSafe();
+await testUnexpectedTerminationRemainsRetryable();
+await testStdioFailuresRemainRetryable();
+await testUnknownJsonRpcFailuresRemainRetryableAndSafe();
await testRuntimeFallbackWarningClassification();
await testSpawnErrorFailsClosed();
+await testMissingWorkerDirectoryRemainsRetryable();
await testAbortKillsPreflightChild();
async function testAllowedProfileAndRawArgv() {
@@ -289,7 +293,7 @@ async function testMalformedStreamFailsClosed() {
await assert.rejects(
preflight(codexPath, cwd),
(error) =>
- error?.name === "DeepScanNonRetryableError" &&
+ error?.name === "Error" &&
error.message.includes("with this Codex configuration"),
);
await assertPreflightStopped(children, terminatedPath);
@@ -311,7 +315,7 @@ async function testRepeatedCatalogCursorFailsClosed() {
await assert.rejects(
preflight(codexPath, cwd),
(error) =>
- error?.name === "DeepScanNonRetryableError" &&
+ error?.name === "Error" &&
error.message.includes("with this Codex configuration"),
);
const calls = await readJsonLines(callsPath);
@@ -434,6 +438,29 @@ async function testLiteralProtoKeyCollisionFailsClosed() {
);
}
+async function testSelectedProfileClassificationRequiresVerifiedString() {
+ for (const selected of [undefined, null, 17, ":read-only"]) {
+ const configResult = configReadResult(expectedProfile);
+ configResult.config.default_permissions = selected;
+ await withFakeCodex(
+ {
+ configResult,
+ catalogResults: [catalogResult(true)],
+ },
+ async ({ codexPath, cwd, terminatedPath, children }) => {
+ await assert.rejects(preflight(codexPath, cwd), (error) =>
+ selected === ":read-only"
+ ? error?.name === "DeepScanNonRetryableError" &&
+ error.message.includes("did not select the required")
+ : error?.name === "Error" &&
+ error.message.includes("with this Codex configuration"),
+ );
+ await assertPreflightStopped(children, terminatedPath);
+ },
+ );
+ }
+}
+
async function testMalformedAndUnsupportedResponsesFailClosed() {
await withFakeCodex(
{
@@ -449,7 +476,7 @@ async function testMalformedAndUnsupportedResponsesFailClosed() {
await assert.rejects(
preflight(codexPath, cwd),
(error) =>
- error?.name === "DeepScanNonRetryableError" &&
+ error?.name === "Error" &&
error.message.includes("with this Codex configuration"),
);
},
@@ -466,16 +493,9 @@ async function testMalformedAndUnsupportedResponsesFailClosed() {
await assert.rejects(
preflight(codexPath, cwd),
(error) =>
- error?.name === "DeepScanNonRetryableError" &&
- error.message.includes(JSON.stringify(codexPath)) &&
- error.message.includes("permissionProfile/list.allowed") &&
- error.message.includes("does not support") &&
- error.message.includes(
- "Update the Codex installation at that path",
- ) &&
- error.message.includes("desktop app if it is bundled") &&
- error.message.includes("otherwise the selected CLI") &&
- !error.message.includes("host/CLI"),
+ error?.name === "Error" &&
+ error.message.includes("with this Codex configuration") &&
+ !error.message.includes("does not support"),
);
},
);
@@ -516,7 +536,7 @@ async function testEarlyExecutableExitIsNotVersionError() {
await assert.rejects(
preflight(codexPath, cwd),
(error) =>
- error?.name === "DeepScanNonRetryableError" &&
+ error?.name === "Error" &&
error.message.includes(JSON.stringify(codexPath)) &&
error.message.includes(
"exited before permission-profile verification completed",
@@ -531,37 +551,93 @@ async function testEarlyExecutableExitIsNotVersionError() {
);
}
-async function testNonVersionJsonRpcFailureIsSafe() {
+async function testUnexpectedTerminationRemainsRetryable() {
await withFakeCodex(
{
- methodErrors: {
- "config/read": {
- code: -32603,
- message: "SECRET_REPOSITORY_PATH=/repo/private",
- },
- },
+ hangAt: "config/read",
},
- async ({ codexPath, cwd }) => {
+ async ({ codexPath, cwd, readyPath, children }) => {
+ const running = preflight(codexPath, cwd);
+ await waitForFile(readyPath);
+ assert.equal(children.length, 1);
+ children[0].kill("SIGKILL");
await assert.rejects(
- preflight(codexPath, cwd),
+ running,
(error) =>
- error?.name === "DeepScanNonRetryableError" &&
+ error?.name === "Error" &&
error.message.includes(JSON.stringify(codexPath)) &&
- error.message.includes("config/read") &&
- error.message.includes("JSON-RPC code -32603") &&
- !error.message.includes("SECRET_REPOSITORY_PATH") &&
+ error.message.includes("signal SIGKILL") &&
!error.message.includes("does not support"),
);
+ assert.equal(children[0].signalCode, "SIGKILL");
},
);
}
+async function testStdioFailuresRemainRetryable() {
+ for (const stream of ["stdin", "stdout"]) {
+ await withFakeCodex(
+ { hangAt: "initialize" },
+ async ({ codexPath, cwd, readyPath, terminatedPath, children }) => {
+ const running = preflight(codexPath, cwd);
+ await waitForFile(readyPath);
+ assert.equal(children.length, 1);
+ children[0][stream].destroy(
+ new Error("SECRET_REPOSITORY_PATH=/repo/private"),
+ );
+ await assert.rejects(
+ running,
+ (error) =>
+ error?.name === "Error" &&
+ error.message.includes(JSON.stringify(codexPath)) &&
+ error.message.includes(
+ "could not exchange app-server JSON-RPC over stdio",
+ ) &&
+ !error.message.includes("SECRET_REPOSITORY_PATH"),
+ );
+ await assertPreflightStopped(children, terminatedPath);
+ },
+ );
+ }
+}
+
+async function testUnknownJsonRpcFailuresRemainRetryableAndSafe() {
+ for (const code of [-32603, -32602, -32000, undefined]) {
+ await withFakeCodex(
+ {
+ methodErrors: {
+ "config/read": {
+ ...(code === undefined ? {} : { code }),
+ message: "SECRET_REPOSITORY_PATH=/repo/private",
+ },
+ },
+ },
+ async ({ codexPath, cwd, terminatedPath, children }) => {
+ await assert.rejects(
+ preflight(codexPath, cwd),
+ (error) =>
+ error?.name === "Error" &&
+ error.message.includes(JSON.stringify(codexPath)) &&
+ error.message.includes("config/read") &&
+ (code === undefined
+ ? !error.message.includes("JSON-RPC code")
+ : error.message.includes(`JSON-RPC code ${code}`)) &&
+ !error.message.includes("SECRET_REPOSITORY_PATH") &&
+ !error.message.includes("does not support"),
+ );
+ await assertPreflightStopped(children, terminatedPath);
+ },
+ );
+ }
+}
+
async function testSpawnErrorFailsClosed() {
const missingCodex = path.join(tmpdir(), `missing-codex-${process.pid}`);
await assert.rejects(
preflight(missingCodex, tmpdir()),
(error) =>
- error?.name === "DeepScanNonRetryableError" &&
+ error?.name === "Error" &&
+ error.cause?.code === "ENOENT" &&
error.message.includes(JSON.stringify(missingCodex)) &&
error.message.includes("could not start") &&
error.message.includes("with --version") &&
@@ -570,6 +646,19 @@ async function testSpawnErrorFailsClosed() {
);
}
+async function testMissingWorkerDirectoryRemainsRetryable() {
+ await withFakeCodex({}, async ({ codexPath, cwd, argvPath }) => {
+ await assert.rejects(
+ preflight(codexPath, path.join(cwd, "missing-worker-directory")),
+ (error) =>
+ error?.name === "Error" &&
+ error.cause?.code === "ENOENT" &&
+ error.message.includes("could not start"),
+ );
+ await assert.rejects(readFile(argvPath), { code: "ENOENT" });
+ });
+}
+
async function testRuntimeFallbackWarningClassification() {
const warning = `Configured value for \`permission_profile\` is disallowed by requirements; falling back from \`${profileId}\` to required value \`enterprise-default\`.`;
const error = deepScanPermissionProfileFallbackError(warning, profileId);
@@ -721,20 +810,24 @@ async function withFakeCodex(
await copyFile(process.execPath, codexPath);
assert.ok(codexPath.length > 260);
}
- childProcess.spawn = (command, args, options) => {
- if (
- command !== codexPath &&
- command !== path.toNamespacedPath(codexPath)
- ) {
- return originalSpawn(command, args, options);
- }
- // Reuse the protocol script without replacing or normalizing the executable.
- const child = originalSpawn(command, [scriptPath, ...args], options);
- children.push(child);
- return child;
- };
- syncBuiltinESMExports();
}
+ childProcess.spawn = (command, args, options) => {
+ if (
+ command !== codexPath &&
+ command !== path.toNamespacedPath(codexPath)
+ ) {
+ return originalSpawn(command, args, options);
+ }
+ // Reuse the protocol script without replacing or normalizing the executable.
+ const child = originalSpawn(
+ command,
+ process.platform === "win32" ? [scriptPath, ...args] : args,
+ options,
+ );
+ children.push(child);
+ return child;
+ };
+ syncBuiltinESMExports();
await callback({
codexPath,
cwd: root,
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.mjs b/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.mjs
index ab4cae9ae..831a98320 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.mjs
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.mjs
@@ -40,6 +40,7 @@ const {
);
await testReducerCommitAndFinishAgainstRealWorkbench();
+await testReducerCommitAndFinishAgainstRealWorkbench(true);
await testExpiredDeadlineWithoutCompletedDiscoveryAgainstRealWorkbench();
await testLateParentDraftPreservesCheckpointWithoutOverwritingTerminalSeal();
await testRecoveredPublicationRejectsLateFailure();
@@ -390,7 +391,9 @@ async function testLateParentDraftPreservesCheckpointWithoutOverwritingTerminalS
}
}
-async function testReducerCommitAndFinishAgainstRealWorkbench() {
+async function testReducerCommitAndFinishAgainstRealWorkbench(
+ replaceFailedReducer = false,
+) {
const fixtureRoot = await mkdtemp(
path.join(tmpdir(), "deep-scan-store-integration-"),
);
@@ -542,6 +545,41 @@ async function testReducerCommitAndFinishAgainstRealWorkbench() {
const first = await createSucceededDiscovery(store, run, "first");
const second = await createSucceededDiscovery(store, run, "second");
const late = await createRunningDiscovery(store, run, "late");
+ if (replaceFailedReducer) {
+ const retired = await createWorkerFixture(run.scanDir, "retired-reducer");
+ await store.claimDedup({
+ id: retired.id,
+ scanId: run.scanId,
+ workerIds: [first.id, second.id],
+ promptPath: retired.promptPath,
+ artifactDir: retired.artifactDir,
+ });
+ const mutation = {
+ id: retired.id,
+ scanId: run.scanId,
+ kind: "dedup",
+ promptPath: retired.promptPath,
+ artifactDir: retired.artifactDir,
+ attempt: 1,
+ };
+ await store.updateWorker({ ...mutation, status: "running" });
+ await store.updateWorker({
+ ...mutation,
+ status: "failed",
+ error: "Request blocked by cyberPolicy.",
+ });
+ const afterRetirement = await store.get(run.scanId, threadId);
+ assert.equal(afterRetirement.status, "running");
+ for (const workerId of [first.id, second.id]) {
+ assert.equal(
+ afterRetirement.persistedWorkers.find(
+ (worker) => worker.id === workerId,
+ )?.mergeState,
+ "buffered",
+ "a retired reducer must release every input for its replacement",
+ );
+ }
+ }
const reducer = await createReducerFixture(run.scanDir, [
first.id,
second.id,
diff --git a/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.mjs b/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.mjs
index 0ff884c43..9bd763c2b 100644
--- a/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.mjs
+++ b/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.mjs
@@ -769,7 +769,7 @@ async function assertHeadlessStandardScanWorksWithoutUiCapability() {
}
}
-async function assertDeepScanPersistsWorkerStartupFailure() {
+async function assertDeepScanPersistsRetryableWorkerStartupError() {
const fixtureRoot = await mkdtemp(
path.join(tmpdir(), "codex-security-deep-inventory-"),
);
@@ -779,13 +779,14 @@ async function assertDeepScanPersistsWorkerStartupFailure() {
await mkdir(path.join(fixtureTarget, "app"), { recursive: true });
await writeFile(path.join(fixtureTarget, "app", "routes.py"), "route = 1\n");
+ const fixtureEnvironment = {
+ CODEX_CLI_PATH: path.join(fixtureRoot, "missing-deep-scan-codex"),
+ CODEX_SECURITY_SCAN_ROOT: fixtureScanRoot,
+ CODEX_SECURITY_STATE_DIR: fixtureState,
+ };
const deepServer = startTestServer({
cwd: pluginRoot,
- env: {
- CODEX_CLI_PATH: path.join(fixtureRoot, "missing-deep-scan-codex"),
- CODEX_SECURITY_SCAN_ROOT: fixtureScanRoot,
- CODEX_SECURITY_STATE_DIR: fixtureState,
- },
+ env: fixtureEnvironment,
});
try {
assertNoError(
@@ -799,7 +800,7 @@ async function assertDeepScanPersistsWorkerStartupFailure() {
}),
);
- const started = await deepServer.requestAndWait(2, "tools/call", {
+ deepServer.sendRequest(2, "tools/call", {
name: "start_codex_security_deep_scan",
arguments: { targetPath: fixtureTarget },
_meta: {
@@ -807,20 +808,64 @@ async function assertDeepScanPersistsWorkerStartupFailure() {
"codex/sandbox-state-meta": parentSandboxState,
},
});
- assert.equal(started.result.isError, true);
- assert.match(
- started.result.content.map((item) => item.text).join(" "),
- /missing-deep-scan-codex/,
+ let scan;
+ let startupErrorWorker;
+ const pollingStarted = Date.now();
+ for (
+ let requestId = 100;
+ Date.now() - pollingStarted < 30_000;
+ requestId++
+ ) {
+ const listed = await deepServer.requestAndWait(requestId, "tools/call", {
+ name: "list_codex_security_scans",
+ arguments: {},
+ });
+ assertNoError(listed);
+ [scan] = listed.result.structuredContent.scans;
+ if (scan) {
+ assert.equal(listed.result.structuredContent.scans.length, 1);
+ assert.equal(scan.progress.status, "running");
+ const { deepScan } = JSON.parse(
+ execFileSync(
+ process.env.PYTHON?.trim() || "python3",
+ [
+ path.join(pluginRoot, "scripts", "workbench_db.py"),
+ "get-deep-scan",
+ "--scan-id",
+ scan.scanId,
+ "--thread-id",
+ "fixture-deep-inventory-thread",
+ ],
+ {
+ env: { ...process.env, ...fixtureEnvironment },
+ encoding: "utf8",
+ },
+ ),
+ );
+ assert.equal(deepScan.status, "running");
+ startupErrorWorker = deepScan.workers.find((worker) =>
+ worker.error?.includes("missing-deep-scan-codex"),
+ );
+ if (startupErrorWorker) break;
+ }
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ }
+ assert.ok(
+ startupErrorWorker,
+ "Expected a persisted retryable startup error",
);
+ assert.equal(startupErrorWorker.status, "running");
- const listed = await deepServer.requestAndWait(3, "tools/call", {
- name: "list_codex_security_scans",
- arguments: {},
+ const canceled = await deepServer.requestAndWait(3, "tools/call", {
+ name: "cancel_codex_security_scan",
+ arguments: { scanId: scan.scanId },
+ _meta: { "openai/threadId": "fixture-deep-inventory-thread" },
});
- assertNoError(listed);
- assert.equal(listed.result.structuredContent.scans.length, 1);
- const scan = listed.result.structuredContent.scans[0];
- assert.equal(scan.progress.status, "failed");
+ assertNoError(canceled);
+ await deepServer.waitForMessage(
+ (message) => message.id === 2,
+ "Deep Scan start response after cancellation",
+ );
await assert.rejects(
readFile(
path.join(
@@ -1506,7 +1551,7 @@ try {
await assertUnavailableUserInputFallback();
await assertWorkspaceWorksWithoutUiCapability();
await assertHeadlessStandardScanWorksWithoutUiCapability();
- await assertDeepScanPersistsWorkerStartupFailure();
+ await assertDeepScanPersistsRetryableWorkerStartupError();
await assertUserInputFailureLogging();
if (process.platform !== "win32") {
await rm(launchCwd, { recursive: true, force: true });
diff --git a/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts b/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts
index 471e3b83e..71bf5f893 100644
--- a/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts
+++ b/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts
@@ -197,24 +197,14 @@ test("classifies owned worker tool failures without exposing their contents", as
expect(unrelatedDiagnostics).toEqual([]);
});
-test("does not retry textual missing-path worker failures", async () => {
+test("keeps textual missing-path worker failures retryable", async () => {
const runtime = await loadBundledRuntime();
- const errorClass =
- /var DeepScanNonRetryableError = class extends Error \{[\s\S]*?\n\};/u.exec(
- runtime,
- )?.[0];
- expect(errorClass).toBeDefined();
const classify = new Function(
- "ARTIFACT_MCP_STARTUP_TIMEOUT_PATTERN",
- "REMOTE_PLUGIN_AUTH_WARNING_PATTERN",
- "isCodexCybersecurityPolicyRefusal",
[
- errorClass!,
bundledFunction(runtime, "classifyCodexWorkerError"),
- bundledFunction(runtime, "isCodexConfigurationFailure"),
"return classifyCodexWorkerError;",
].join("\n"),
- )(/$^/u, /$^/u, () => false) as (error: Error) => Error;
+ )() as (error: Error) => Error;
for (const diagnostic of [
"Error: No such file or directory (os error 2)",
@@ -224,8 +214,7 @@ test("does not retry textual missing-path worker failures", async () => {
["Codex Exec exited with code 1:", diagnostic].join("\n"),
);
const classified = classify(original);
- expect(classified.name).toBe("DeepScanNonRetryableError");
- expect(classified.cause).toBe(original);
+ expect(classified).toBe(original);
}
});
diff --git a/sdk/typescript/tests-ts/deep-scan-windows-executable.test.ts b/sdk/typescript/tests-ts/deep-scan-windows-executable.test.ts
index d24ab3863..0e95ea9bc 100644
--- a/sdk/typescript/tests-ts/deep-scan-windows-executable.test.ts
+++ b/sdk/typescript/tests-ts/deep-scan-windows-executable.test.ts
@@ -166,22 +166,19 @@ test.each([
},
);
-test("does not retry Windows executable permission failures", async () => {
+test("keeps Windows executable permission failures retryable", async () => {
const runtime = await loadBundledRuntime();
const source =
/function classifyCodexWorkerError\([^\n]*\) \{[\s\S]*?\n\}/u.exec(
runtime,
)?.[0];
expect(source).toBeDefined();
- class NonRetryableError extends Error {}
const classify = new Function(
- "DeepScanNonRetryableError",
`${source}\nreturn classifyCodexWorkerError;`,
- )(NonRetryableError) as (error: Error) => Error;
+ )() as (error: Error) => Error;
const original = Object.assign(new Error("spawn codex EPERM"), {
code: "EPERM",
});
const result = classify(original);
- expect(result).toBeInstanceOf(NonRetryableError);
- expect(result.cause).toBe(original);
+ expect(result).toBe(original);
});