OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix(diagnostics): retain underlying artifact and process failures (#1504)
mldangelo-oai04bec6a3f6b1
diff --git a/plugins/codex-security/mcp-app/server.ts b/plugins/codex-security/mcp-app/server.tsindex a6f92409e..f6cc923ca 100644--- a/plugins/codex-security/mcp-app/server.ts+++ b/plugins/codex-security/mcp-app/server.ts@@ -2250,7 +2250,7 @@ async function runWorkbench(? missingPythonHelperMessage(error, pythonCommand): undefined;if (launchError) {- throw new Error(launchError);+ throw new Error(launchError, { cause: error });}if (isExecError(error) && error.stderr.trim()) {throw new Error(error.stderr.trim(), { cause: error });diff --git a/plugins/codex-security/mcp-app/src/artifact-inventory.ts b/plugins/codex-security/mcp-app/src/artifact-inventory.tsindex 991bef38d..c71452f89 100644--- a/plugins/codex-security/mcp-app/src/artifact-inventory.ts+++ b/plugins/codex-security/mcp-app/src/artifact-inventory.ts@@ -170,12 +170,12 @@ async function readReviewItems(return rows;}-function helperError(error: unknown): string | undefined {- if (!error || typeof error !== "object" || !("stderr" in error))- return undefined;- const stderr = error.stderr;- if (typeof stderr === "string") return stderr.trim() || undefined;- if (Buffer.isBuffer(stderr))- return stderr.toString("utf8").trim() || undefined;- return undefined;+function helperError(error: unknown): string {+ if (error && typeof error === "object" && "stderr" in error) {+ const stderr = error.stderr;+ if (typeof stderr === "string" && stderr.trim()) return stderr.trim();+ if (Buffer.isBuffer(stderr) && stderr.length)+ return stderr.toString("utf8").trim();+ }+ return error instanceof Error ? error.message : String(error);}diff --git a/plugins/codex-security/mcp-app/src/artifact-io.ts b/plugins/codex-security/mcp-app/src/artifact-io.tsindex 60e2d7122..e78f9b680 100644--- a/plugins/codex-security/mcp-app/src/artifact-io.ts+++ b/plugins/codex-security/mcp-app/src/artifact-io.ts@@ -61,8 +61,11 @@ export async function readArtifactText(const canonical = await artifactSourcePath(context, components, label);try {return await fs.readFile(canonical, "utf8");- } catch {- throw new Error(label + ": the requested artifact cannot be read.");+ } catch (error) {+ throw new Error(+ `${label}: the requested artifact cannot be read: ${error instanceof Error ? error.message : String(error)}`,+ { cause: error },+ );}}@@ -81,8 +84,11 @@ export async function readArtifactTextWithMetadata(} finally {await handle.close();}- } catch {- throw new Error(label + ": the requested artifact cannot be read.");+ } catch (error) {+ throw new Error(+ `${label}: the requested artifact cannot be read: ${error instanceof Error ? error.message : String(error)}`,+ { cause: error },+ );}}@@ -97,7 +103,7 @@ export async function artifactSourcePath(for (const [index, component] of components.entries()) {current = join(current, component);- const metadata = await fs.lstat(current).catch(() => undefined);+ const metadata = await inspectOptionalPath(current, label);if (!metadata) {throw new Error(label + ": the requested artifact is unavailable.");}@@ -320,7 +326,10 @@ async function inspectOptionalPath(return await fs.lstat(path);} catch (error) {if (isNodeError(error) && error.code === "ENOENT") return undefined;- throw new Error(label + ": artifact path cannot be inspected.");+ throw new Error(+ `${label}: artifact path cannot be inspected: ${error instanceof Error ? error.message : String(error)}`,+ { cause: error },+ );}}diff --git a/plugins/codex-security/mcp-app/src/deep-scan/executor.ts b/plugins/codex-security/mcp-app/src/deep-scan/executor.tsindex b69638298..579d5f192 100644--- a/plugins/codex-security/mcp-app/src/deep-scan/executor.ts+++ b/plugins/codex-security/mcp-app/src/deep-scan/executor.ts@@ -212,7 +212,7 @@ export class CodexSdkWorkerExecutor implements CodexWorkerExecutor {threadId = event.thread_id;await request.onThreadStarted?.(threadId);} else if (event.type === "item.completed") {- appendSafeItemDiagnostic(diagnostics, event.item);+ appendItemDiagnostic(diagnostics, event.item);} else if (event.type === "turn.completed") {turnCompleted = true;request.signal.removeEventListener("abort", forwardAbort);@@ -222,7 +222,7 @@ export class CodexSdkWorkerExecutor implements CodexWorkerExecutor {} else if (event.type === "error") {// Codex exec currently emits retry-in-progress notifications as error events.lastStreamError = event.message;- appendCodeModeFrameDiagnostic(diagnostics, event.message);+ appendStreamDiagnostic(diagnostics, event.message);}}if (!turnCompleted) {@@ -358,36 +358,35 @@ function workerPermissionProfile(sandbox: DeepWorkerParentSandbox) {};}-/**- * Convert SDK item failures into bounded classifications without retaining the- * command, output, or paths carried by the event. Those fields can contain- * repository contents and credentials, while the coordinator only needs the- * reason a later deterministic artifact check failed.- */-function appendSafeItemDiagnostic(+/** Retain SDK failure messages for a later deterministic artifact check. */+function appendItemDiagnostic(diagnostics: CodexWorkerDiagnostic[],item: unknown,): void {if (!isRecord(item) || typeof item.type !== "string") return;- if (item.type === "error") {- appendCodeModeFrameDiagnostic(diagnostics, item.message);+ if (item.type === "error" && typeof item.message === "string") {+ appendStreamDiagnostic(diagnostics, item.message);return;}if (item.status !== "failed") return;if (item.type === "mcp_tool_call" &&isRecord(item.error) &&- appendCodeModeFrameDiagnostic(diagnostics, item.error.message)- )- return;+ typeof item.error.message === "string" &&+ isCodeModeFrameError(item.error.message)+ ) {+ appendUniqueDiagnostic(diagnostics, {+ code: "artifact_tool_failed",+ message: item.error.message,+ });+ }if (item.type === "command_execution") {const output =typeof item.aggregated_output === "string" ? item.aggregated_output : "";if (isSandboxNamespaceExhaustion(output)) {appendUniqueDiagnostic(diagnostics, {code: "sandbox_namespace_exhausted",- message:- "Codex worker sandbox namespace creation failed (bwrap ENOSPC).",+ message: output,});}return;@@ -405,50 +404,51 @@ function appendSafeItemDiagnostic(item.server === "codex_security_artifacts") &&typeof item.tool === "string") {- if (isRecord(item.result) && Array.isArray(item.result.content)) {- for (const content of item.result.content) {- if (- isRecord(content) &&- content.type === "text" &&- appendCodeModeFrameDiagnostic(diagnostics, content.text)- )- return;- }- }+ const messages = [+ ...(isRecord(item.error) ? [item.error.message] : []),+ ...(isRecord(item.result) && Array.isArray(item.result.content)+ ? item.result.content.flatMap((content) =>+ isRecord(content) && content.type === "text" ? [content.text] : [],+ )+ : []),+ ].filter(+ (message): message is string =>+ typeof message === "string" && message.length > 0,+ );const reason = isRecord(item.result)? "returned an error": isRecord(item.error)? "transport failed": "failed";- appendUniqueDiagnostic(diagnostics, {- code: "artifact_tool_failed",- message: `Codex worker artifact tool ${item.tool} ${reason}.`,- });+ for (const message of messages.length > 0+ ? messages+ : [`Codex worker artifact tool ${item.tool} ${reason}.`]) {+ appendUniqueDiagnostic(diagnostics, {+ code: "artifact_tool_failed",+ message,+ });+ }}}-function appendCodeModeFrameDiagnostic(- diagnostics: CodexWorkerDiagnostic[],- message: unknown,-): boolean {- // Codex exposes this transport error as text, without a structured code.- // Preserve only its complete numeric template, never surrounding tool output.- if (typeof message !== "string") return false;- const match =+function isCodeModeFrameError(message: string): boolean {+ return (/^code-mode delegate response exceeds the IPC frame limit: code-mode IPC frame length [0-9]+ exceeds [0-9]+ bytes$/u.exec(message,- );- if (match?.[0] !== message) return false;- const diagnostic: CodexWorkerDiagnostic = {- code: "artifact_tool_failed",- message,- };- const index = diagnostics.findIndex(- (existing) => existing.code === diagnostic.code,+ )?.[0] === message);- if (index === -1) diagnostics.push(diagnostic);- else diagnostics[index] = diagnostic;- return true;+}++function appendStreamDiagnostic(+ diagnostics: CodexWorkerDiagnostic[],+ message: string,+): void {+ appendUniqueDiagnostic(diagnostics, {+ code: isCodeModeFrameError(message)+ ? "artifact_tool_failed"+ : "worker_error",+ message,+ });}function isSandboxNamespaceExhaustion(output: string): boolean {@@ -461,7 +461,13 @@ function appendUniqueDiagnostic(diagnostics: CodexWorkerDiagnostic[],diagnostic: CodexWorkerDiagnostic,): void {- if (!diagnostics.some((existing) => existing.code === diagnostic.code)) {+ if (+ !diagnostics.some(+ (existing) =>+ existing.code === diagnostic.code &&+ existing.message === diagnostic.message,+ )+ ) {diagnostics.push(diagnostic);}}diff --git a/plugins/codex-security/mcp-app/src/deep-scan/types.ts b/plugins/codex-security/mcp-app/src/deep-scan/types.tsindex 666cd1984..bafd2af0f 100644--- a/plugins/codex-security/mcp-app/src/deep-scan/types.ts+++ b/plugins/codex-security/mcp-app/src/deep-scan/types.ts@@ -129,18 +129,13 @@ export interface CodexWorkerResult {diagnostics?: CodexWorkerDiagnostic[];}-/**- * Sanitized SDK evidence that is safe to persist in SQLite and manifests.- *- * Never add raw command text, command output, prompts, or repository paths- * here. The coordinator only needs stable classifications that explain why a- * worker could not satisfy its artifact contract.- */+/** SDK failure diagnostics retained alongside artifact-validation failures. */export interface CodexWorkerDiagnostic {code:| "sandbox_namespace_exhausted"| "file_change_failed"- | "artifact_tool_failed";+ | "artifact_tool_failed"+ | "worker_error";message: string;}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.tsindex e95d546c7..84d952647 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@@ -667,7 +667,10 @@ function withWorkerDiagnostics(const namespaceFailure = diagnostics.find((diagnostic) => diagnostic.code === "sandbox_namespace_exhausted",);- const diagnostic = namespaceFailure ?? diagnostics[0];+ const diagnostic =+ namespaceFailure ??+ diagnostics.find((item) => item.code !== "worker_error") ??+ diagnostics[0];const combined = new Error(`${diagnostics.map((item) => item.message).join(" ")} Deterministic artifact validation also reported: ${normalized.message}`,{ cause: normalized },@@ -684,7 +687,9 @@ function withWorkerDiagnostics(function isMissingWorkerResult(error: Error, artifactDir: string): boolean {const diagnosed = error as NodeJS.ErrnoException;const original =- diagnosed.code === "artifact_tool_failed" && error.cause instanceof Error+ (diagnosed.code === "artifact_tool_failed" ||+ diagnosed.code === "worker_error") &&+ error.cause instanceof Error? (error.cause as NodeJS.ErrnoException): diagnosed;return (diff --git a/plugins/codex-security/mcp-app/src/python_command.ts b/plugins/codex-security/mcp-app/src/python_command.tsindex aef8c7927..124417f41 100644--- a/plugins/codex-security/mcp-app/src/python_command.ts+++ b/plugins/codex-security/mcp-app/src/python_command.ts@@ -89,7 +89,7 @@ export async function isUsablePythonExecutable(}}-/** Translate operating-system spawn failures without hiding Python process errors. */+/** Add installation advice to missing-runtime errors without hiding their cause. */export function missingPythonHelperMessage(error: unknown,pythonCommand: string,@@ -98,13 +98,13 @@ export function missingPythonHelperMessage(!error ||typeof error !== "object" ||!("code" in error) ||- typeof error.code !== "string" ||+ error.code !== "ENOENT" ||!("path" in error) ||error.path !== pythonCommand) {return undefined;}- return MISSING_PYTHON_HELPER_MESSAGE;+ return `${error instanceof Error ? error.message : String(error)}\n${MISSING_PYTHON_HELPER_MESSAGE}`;}/** Run an existing Python helper with in-memory input and preserve its output. */diff --git a/plugins/codex-security/mcp-app/src/workbench/database.ts b/plugins/codex-security/mcp-app/src/workbench/database.tsindex 73c5cde24..49bcf82e4 100644--- a/plugins/codex-security/mcp-app/src/workbench/database.ts+++ b/plugins/codex-security/mcp-app/src/workbench/database.ts@@ -55,24 +55,30 @@ export async function openWorkbenchDatabase(databasePath: string,{ deferred = false }: { deferred?: boolean } = {},): Promise<DatabaseSync> {- createStateDirectory(dirname(databasePath));- for (let attempt = 0; ; attempt++) {- const database = new DatabaseSync(databasePath);- try {- database.exec("PRAGMA foreign_keys = ON; PRAGMA busy_timeout = 5000;");- applyMigrations(database, undefined, !deferred || attempt > 0);- database.exec("PRAGMA journal_mode = WAL");- chmodSync(databasePath, 0o600);- return database;- } catch (error) {- database.close();- const busy =- error instanceof Error &&- "errcode" in error &&- [5, 6].includes(Number(error.errcode) & 0xff);- if (attempt === 4 || !busy) throw error;- await setTimeout(50 * 2 ** attempt);+ try {+ createStateDirectory(dirname(databasePath));+ for (let attempt = 0; ; attempt++) {+ const database = new DatabaseSync(databasePath);+ try {+ database.exec("PRAGMA foreign_keys = ON; PRAGMA busy_timeout = 5000;");+ applyMigrations(database, undefined, !deferred || attempt > 0);+ database.exec("PRAGMA journal_mode = WAL");+ chmodSync(databasePath, 0o600);+ return database;+ } catch (error) {+ database.close();+ const busy =+ error instanceof Error &&+ "errcode" in error &&+ [5, 6].includes(Number(error.errcode) & 0xff);+ if (attempt === 4 || !busy) throw error;+ await setTimeout(50 * 2 ** attempt);+ }}+ } catch (error) {+ if (error instanceof Error && "errcode" in error)+ error.message += `\nWorkbench database: ${databasePath}. Ensure the state directory and SQLite journal files are writable, or set CODEX_SECURITY_STATE_DIR to a writable directory outside the scanned repository.`;+ throw error;}}diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_foundation.ts b/plugins/codex-security/mcp-app/tests/test_artifact_foundation.tsindex 107747940..c66115c50 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_foundation.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_foundation.ts@@ -3,6 +3,7 @@ import assert from "node:assert/strict";import { spawn } from "node:child_process";import { once } from "node:events";import {+ chmod,mkdir,mkdtemp,readFile,@@ -408,6 +409,27 @@ async function testSafeJsonAndJsonl() {),/row 1 does not match its artifact schema: candidate_id: required/,);+ if (process.platform !== "win32" && process.getuid?.() !== 0) {+ try {+ await chmod(destination, 0o000);+ for (const read of [+ io.readArtifactText,+ io.readArtifactTextWithMetadata,+ ]) {+ await assert.rejects(+ read(context, components, "discovery_candidates"),+ (error: Error & { cause?: NodeJS.ErrnoException }) => {+ assert.equal(error.cause?.code, "EACCES");+ assert.ok(error.message.includes("EACCES"));+ assert.ok(error.message.includes(destination));+ return true;+ },+ );+ }+ } finally {+ await chmod(destination, 0o600);+ }+ }}async function testAtomicReplacement() {diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.tsindex 9d04f8520..c91869aaa 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts@@ -28,6 +28,7 @@ try {await testMissingInventoryIsReported();await testWorkersCannotPrepareInventory();await testBoundScopeFailurePreservesPreviousInventory();+ await testPythonLaunchErrors();await testInvalidDiffTargetPreservesPreviousInventory();} finally {await temporaryDirectories.cleanup();@@ -426,6 +427,34 @@ async function testInvalidDiffTargetPreservesPreviousInventory() {}}+async function testPythonLaunchErrors() {+ const fixture = await createFixture("python launch failure");+ const pythonCommand = path.join(fixture.root, "python");+ for (const code of process.platform === "win32"+ ? ["ENOENT"]+ : ["ENOENT", "EACCES"]) {+ if (code === "EACCES") await writeFile(pythonCommand, "", { mode: 0o600 });+ await assert.rejects(+ inventory.prepareCodexSecurityReviewItems({+ ...fixture.scan,+ pythonCommand,+ }),+ (error: Error & { cause?: NodeJS.ErrnoException }) => {+ assert.equal(error.cause?.code, code);+ assert.ok(+ error.message.includes(`spawn ${pythonCommand} ${code}`),+ error.message,+ );+ assert.equal(+ error.message.includes("Reinstall or update"),+ code === "ENOENT",+ );+ return true;+ },+ );+ }+}+async function createFixture(label: string) {const root = await temporaryDirectories.create("security-artifact-inventory-",diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.ts b/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.tsindex ab020ec89..f425f2d68 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.ts@@ -2136,6 +2136,28 @@ try {},);+ if (process.platform !== "win32" && process.getuid?.() !== 0) {+ const beforePermissionFailure = await snapshotScanDraft(root);+ for (const [file, mode, message] of [+ [hardeningDirectory, 0o600, /artifact path cannot be inspected.*EACCES/],+ [hardeningPortfolio, 0o000, /requested artifact cannot be read.*EACCES/],+ ] as const) {+ try {+ await fsPromises.chmod(file, mode);+ await rejectsDraft(input, message);+ assert.deepEqual(+ await snapshotScanDraft(root),+ beforePermissionFailure,+ );+ } finally {+ await fsPromises.chmod(+ file,+ file === hardeningDirectory ? 0o700 : 0o600,+ );+ }+ }+ }+await rm(hardeningPortfolio);await recordFreshScanDraft(context, input);assert.equal(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.tsindex bb7769e59..f345a3dbb 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@@ -4,6 +4,7 @@ import type { ScanDraftInput } from "../src/artifact-scan-draft.js";import type {DeepScanConfig,DeepScanRunState,+ CodexWorkerDiagnostic,PersistedDeepScanWorker,DeepScanLogEvent,} from "../src/deep-scan/types.js";@@ -1187,6 +1188,51 @@ async function testCancellationClearsRetryWait() {);}+async function testGenericDiagnosticsPreserveMissingResultRetries() {+ for (const code of [+ undefined,+ "file_change_failed",+ "artifact_tool_failed",+ ] as const) {+ for (const genericFirst of [false, true]) {+ const diagnostics: CodexWorkerDiagnostic[] = [+ {+ code: "worker_error",+ message: "Synthetic stream reconnect notification.",+ },+ ];+ if (code)+ diagnostics.push({+ code,+ message: "Synthetic recognized worker failure.",+ });+ if (!genericFirst) diagnostics.reverse();+ const { fixture, store } = await coordinatorFixture();+ const executor = new FakeExecutor({+ invalidDiscoveryAttempts: 1,+ discoveryDiagnostics: diagnostics,+ });+ const terminal = await runCoordinator(fixture, store, executor, {+ retryDelaysMs: [1],+ });+ assert.equal(terminal?.status, "succeeded", terminal?.error);+ assert.deepEqual(executor.discoveryResumeThreadIds, [+ undefined,+ code === "file_change_failed"+ ? undefined+ : executor.discoveryThreadIds[0],+ ]);+ for (const diagnostic of diagnostics) {+ assert.ok(+ store.workerUpdates.some((update) =>+ update.error?.includes(diagnostic.message),+ ),+ );+ }+ }+ }+}+async function testMissingDiscoveryResultResumesExistingThread(withToolFailure = false,) {@@ -1199,7 +1245,7 @@ async function testMissingDiscoveryResultResumesExistingThread({code: "artifact_tool_failed" as const,message:- "Codex worker artifact tool record_codex_security_scan_draft failed.",+ "--provider-error EACCES: permission denied, open /synthetic/result.json\nArtifact transport closed.",},],}@@ -1219,6 +1265,12 @@ async function testMissingDiscoveryResultResumesExistingThread(executor.discoveryThreadIds[0],]);assert.equal(new Set(executor.discoveryThreadIds).size, 1);+ if (withToolFailure) {+ const message = executor.options.discoveryDiagnostics![0].message;+ assert.ok(+ store.workerUpdates.some((update) => update.error?.includes(message)),+ );+ }const continuation = executor.discoveryContinuationPrompts[1] ?? "";assert.match(continuation, /completed source analysis/);assert.match(@@ -1380,7 +1432,7 @@ async function testInvalidReducerResultRetriesFromSnapshot(}async function testMissingReducerResultResumesExistingThread(- diagnosticMessage = "Codex worker artifact tool record_codex_security_deep_reduction failed.",+ diagnosticMessage = "--provider-error EACCES: permission denied, open /synthetic/result.json\nArtifact transport closed.",) {const { fixture, store } = await coordinatorFixture({stopAfterConsecutiveErrors: 2,@@ -1465,6 +1517,10 @@ async function testMissingReducerResultRetainsSizeDiagnosticAfterOtherFailures()const sizeMessage ="code-mode delegate response exceeds the IPC frame limit: code-mode IPC frame length 76008279 exceeds 67108864 bytes";for (const earlierDiagnostic of [+ {+ code: "artifact_tool_failed",+ message: "--provider-error EACCES: permission denied.",+ },{ code: "file_change_failed", message: "Codex worker file change failed." },{code: "sandbox_namespace_exhausted",@@ -1489,9 +1545,16 @@ async function testMissingReducerResultRetainsSizeDiagnosticAfterOtherFailures()assert.ok(failure!.error!.includes(earlierDiagnostic.message));assert.ok(failure!.error!.includes(sizeMessage));assert.ok(failure!.error!.includes("result.json"));- const retryPrompt = await readFile(executor.dedupPromptPaths[1], "utf8");- assert.ok(retryPrompt.includes(earlierDiagnostic.message));- assert.ok(retryPrompt.includes(sizeMessage));+ if (earlierDiagnostic.code === "artifact_tool_failed") {+ assert.equal(+ executor.dedupResumeThreadIds[1],+ executor.dedupThreadIds[0],+ );+ } else {+ const retryPrompt = await readFile(executor.dedupPromptPaths[1], "utf8");+ assert.ok(retryPrompt.includes(earlierDiagnostic.message));+ assert.ok(retryPrompt.includes(sizeMessage));+ }}}@@ -4133,6 +4196,7 @@ try {await testLongWorkerErrorIsBoundedOnlyAtPersistenceBoundary();await testDiscoveryPhasePersistenceFailureStopsDispatch();await testCancellationClearsRetryWait();+ await testGenericDiagnosticsPreserveMissingResultRetries();await testMissingDiscoveryResultResumesExistingThread();await testMissingDiscoveryResultResumesExistingThread(true);await testInvalidArtifactsRetry();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.tsindex 8e274f341..22d4b3aa5 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@@ -1,4 +1,7 @@import { readJson, writeJson } from "./support/json.ts";+import { randomUUID } from "node:crypto";+import { loadWorkbenchProcess } from "./support/workbench-process.ts";+import { finding, workerDraft } from "./scan-draft-fixture.ts";import { assertFlagPair } from "./assertions.ts";import { createTemporaryDirectories } from "./support/temporary-directories.ts";import { mock } from "node:test";@@ -47,7 +50,7 @@ const {CodexSdkWorkerExecutor,resolveCodexPath,snapshotWorkerEnvironment,- appendSafeItemDiagnostic,+ appendItemDiagnostic,classifyCodexWorkerError,DeepScanNonRetryableError,isCodexCybersecurityPolicyRefusal,@@ -58,7 +61,7 @@ const {stdin: {// Test the environment snapshot without adding a production export.contents: `${await readFile(executorSource, "utf8")}-export { snapshotWorkerEnvironment, appendSafeItemDiagnostic };+export { snapshotWorkerEnvironment, appendItemDiagnostic };export * from "./errors.js";`,loader: "ts",resolveDir: path.dirname(fileURLToPath(executorSource)),@@ -123,8 +126,11 @@ try {await testZeroSubagentsPreservesHostRestrictions();await testSdkResumesExistingThread();await testRetryNotificationDoesNotInterruptTurn();- await testSandboxNamespaceDiagnosticIsSanitized();- await testOwnedArtifactToolFailureDiagnosticIsSanitized();+ await testSandboxNamespaceDiagnosticIsPreserved();+ await testOwnedArtifactToolFailureDiagnosticIsPreserved();+ for (const reference of ["discovery:\0" + "0", "😀".repeat(1300)]) {+ await testArtifactDiagnosticSurvivesWorkerRetries(reference);+ }await testCodeModeFrameDiagnosticSurvivesSuccessfulTurn();await testStreamTerminationWithoutTerminalEventFails();await testAbortPropagation();@@ -2467,6 +2473,13 @@ async function testRetryNotificationDoesNotInterruptTurn() {subagents: 3,});assert.equal(result.threadId, "fixture-thread-id");+ assert.deepEqual(result.diagnostics, [+ {+ code: "worker_error",+ message:+ "Reconnecting... 2/5 (stream disconnected before completion: websocket closed by server before response.completed)",+ },+ ]);const invocation = await readJson(fixture.markerPath);assert.equal(invocation.argv.includes("--model"), false);assertConfigOverrides(invocation.argv, {@@ -2475,7 +2488,7 @@ async function testRetryNotificationDoesNotInterruptTurn() {});}-async function testSandboxNamespaceDiagnosticIsSanitized() {+async function testSandboxNamespaceDiagnosticIsPreserved() {const result = await runFixtureWorker("BWRAP_NAMESPACE_FAILURE","discovery",@@ -2484,14 +2497,222 @@ async function testSandboxNamespaceDiagnosticIsSanitized() {assert.deepEqual(result.diagnostics, [{code: "sandbox_namespace_exhausted",- message: "Codex worker sandbox namespace creation failed (bwrap ENOSPC).",+ message:+ "private source text\nbwrap: Creating new namespace failed: nesting depth or /proc/sys/user/max_user_namespaces exceeded (ENOSPC)",},]);const serialized = JSON.stringify(result);- assert.doesNotMatch(serialized, /super-secret-command|private source text/);+ assert.doesNotMatch(serialized, /super-secret-command/);+}++async function testArtifactDiagnosticSurvivesWorkerRetries(reference: string) {+ const { executeWorkbench } = await loadWorkbenchProcess();+ const {+ WorkbenchDeepScanStore,+ DeepScanWorkerRunner,+ createDeepScanArtifacts,+ requireRegularFile,+ recordCodexSecurityDeepReduction,+ } = await importModule({+ stdin: {+ contents: `export { WorkbenchDeepScanStore } from "./src/deep-scan/store.ts";+export { DeepScanWorkerRunner } from "./src/deep-scan/worker-runner.ts";+export { createDeepScanArtifacts, requireRegularFile } from "./src/deep-scan/artifacts.ts";+export { recordCodexSecurityDeepReduction } from "./src/artifact-deep-reducer.ts";`,+ resolveDir: path.resolve(import.meta.dirname, ".."),+ },+ loader: { ".md": "text" },+ });+ await withWorkerFixture(async (fixture) => {+ const saved = ["CODEX_HOME", "FAKE_CODEX_ARTIFACT_EVENT"].map(+ (name) => [name, process.env[name]] as const,+ );+ process.env.CODEX_HOME = fixture.root;+ try {+ const targetPath = path.join(fixture.root, "target");+ await mkdir(targetPath);+ await writeFile(+ path.join(targetPath, "fixture.py"),+ "print(1)\nprint(2)\n",+ );+ const store = new WorkbenchDeepScanStore(+ (args: string[], input?: string) =>+ executeWorkbench(+ process.env.PYTHON?.trim() || "python3",+ args,+ path.join(fixture.root, "state"),+ input,+ ),+ );+ const started = await store.begin({+ targetPath,+ threadId: "diagnostic-owner",+ scanRoot: path.join(fixture.root, "scans"),+ });+ const { run } = await store.claimCoordinator({+ scanId: started.scanId,+ threadId: "diagnostic-owner",+ });+ const artifacts = createDeepScanArtifacts(run.scanDir);+ const sourcePath = path.join(+ artifacts.workersRoot,+ "discovery/output/result.json",+ );+ const artifactDir = path.join(+ artifacts.dedupRoot,+ randomUUID(),+ "output",+ );+ const promptPath = path.join(path.dirname(artifactDir), "prompt.md");+ await mkdir(path.dirname(sourcePath), { recursive: true });+ await mkdir(artifactDir, { recursive: true });+ await writeFile(promptPath, "Record the reducer result.");+ const sourceFinding = finding("fixture", "fixture.py");+ await writeJson(+ sourcePath,+ workerDraft([sourceFinding], { scanId: run.scanId }),+ );+ const workerIds = [randomUUID(), randomUUID()];+ for (const id of workerIds) {+ const worker = {+ id,+ scanId: run.scanId,+ kind: "discovery",+ promptPath,+ artifactDir: path.dirname(sourcePath),+ attempt: 1,+ };+ await store.updateWorker({ ...worker, status: "running" });+ await store.updateWorker({+ ...worker,+ status: "succeeded",+ resultManifestPath: sourcePath,+ });+ }+ const workerId = randomUUID();+ await store.claimDedup({+ id: workerId,+ scanId: run.scanId,+ workerIds,+ promptPath,+ artifactDir,+ });+ const deepReducer = {+ scanRoot: run.scanDir,+ claimedWorkers: [{ id: "discovery", resultPath: sourcePath }],+ };+ let message = "";+ await assert.rejects(+ () =>+ recordCodexSecurityDeepReduction(+ {+ root: artifactDir,+ repoRoot: targetPath,+ layout: "reducer",+ scanId: run.scanId,+ deepReducer,+ },+ {+ scanId: run.scanId,+ findings: [+ {+ ...sourceFinding,+ provenance: {+ ...sourceFinding.provenance,+ sourceFindingIds: [reference],+ },+ },+ ],+ },+ ),+ (error: unknown) => {+ assert.ok(error instanceof Error);+ message = error.message;+ return message.includes(reference);+ },+ );+ // Truncating the long, well-formed emoji message splits a surrogate pair.+ const persistedDiagnostic = reference.includes("\0")+ ? message+ : "\ufffd\n...[truncated; sha256:";+ process.env.FAKE_CODEX_ARTIFACT_EVENT = JSON.stringify({+ type: "item.completed",+ item: {+ id: "diagnostic",+ type: "mcp_tool_call",+ server: "cs_artifacts",+ tool: "record_codex_security_deep_reduction",+ status: "failed",+ error: null,+ result: { isError: true, content: [{ type: "text", text: message }] },+ },+ });+ let retries = 0;+ const runner = new DeepScanWorkerRunner({+ run,+ store,+ executor: new CodexSdkWorkerExecutor({+ parentSandbox: trustedParentSandbox,+ }),+ artifacts,+ pluginRoot: path.resolve(import.meta.dirname, "../.."),+ clock: {+ now: () => new Date(),+ sleep: async () => {+ retries++;+ const state = await store.get(run.scanId, "diagnostic-owner");+ const worker = state.persistedWorkers.find(+ (worker: { id: string }) => worker.id === workerId,+ );+ assert.equal(worker.attempt, 1);+ assert.ok(worker.error.includes(persistedDiagnostic));+ assert.equal(+ (await readJson(fixture.markerPath)).argv.includes("resume"),+ false,+ );+ },+ },+ random: () => 0,+ log: () => {},+ retryDelaysMs: [0],+ signal: new AbortController().signal,+ });+ const outcome = await runner.runWorkerWithRetries({+ workerId,+ kind: "dedup",+ promptPath,+ promptRoot: path.join(path.dirname(artifactDir), "prompts"),+ artifactDir,+ artifactContext: { root: artifactDir, layout: "reducer", deepReducer },+ subagents: 0,+ validate: () =>+ requireRegularFile(+ path.join(artifactDir, "result.json"),+ artifactDir,+ ),+ beforeRetry: async () => {},+ });+ assert.equal(outcome.status, "failed");+ assert.equal(retries, 1);+ const state = await store.get(run.scanId, "diagnostic-owner");+ const worker = state.persistedWorkers.find(+ (worker: { id: string }) => worker.id === workerId,+ );+ assert.equal(worker.status, "failed");+ assert.equal(worker.attempt, 2);+ assert.ok(worker.error.includes(persistedDiagnostic));+ const invocation = await readJson(fixture.markerPath);+ assert.equal(+ invocation.argv[invocation.argv.indexOf("resume") + 1],+ "fixture-thread-id",+ );+ } finally {+ for (const [name, value] of saved) restoreEnv(name, value);+ }+ });}-async function testOwnedArtifactToolFailureDiagnosticIsSanitized() {+async function testOwnedArtifactToolFailureDiagnosticIsPreserved() {for (const { prompt, tool, reason } of [{prompt: "OWNED_ARTIFACT_TOOL_REJECTED",@@ -2530,7 +2751,11 @@ async function testOwnedArtifactToolFailureDiagnosticIsSanitized() {assert.deepEqual(result.diagnostics, [{code: "artifact_tool_failed",- message: `Codex worker artifact tool ${tool} ${reason}.`,+ message: prompt.includes("TRANSPORT_FAILED")+ ? "--provider-error transport closed sk-proj-synthetic-secret /private/customer/path"+ : prompt.includes("REJECTED")+ ? "private output sk-proj-synthetic-secret"+ : `Codex worker artifact tool ${tool} ${reason}.`,},]);} else {@@ -2538,7 +2763,7 @@ async function testOwnedArtifactToolFailureDiagnosticIsSanitized() {}assert.doesNotMatch(JSON.stringify(result),- /synthetic-secret|private source|private output|private\/customer\/path/i,+ /Bearer synthetic-secret|private source text/i,);}}@@ -2556,13 +2781,23 @@ function testCodeModeFrameDiagnosticBoundaries() {for (const item of [{ type: "error", message: ipcFrameError },{ ...failedArtifactTool, error: { message: ipcFrameError } },+ {+ ...failedArtifactTool,+ server: "foreign_server",+ error: { message: ipcFrameError },+ },{ ...failedArtifactTool, result: resultWithText(ipcFrameError) },]) {const diagnostics: CodexWorkerDiagnostic[] = [];- appendSafeItemDiagnostic(diagnostics, failedArtifactTool);- appendSafeItemDiagnostic(diagnostics, item);- appendSafeItemDiagnostic(diagnostics, failedArtifactTool);+ appendItemDiagnostic(diagnostics, failedArtifactTool);+ appendItemDiagnostic(diagnostics, item);+ appendItemDiagnostic(diagnostics, failedArtifactTool);assert.deepEqual(diagnostics, [+ {+ code: "artifact_tool_failed",+ message:+ "Codex worker artifact tool get_codex_security_deep_reducer_inputs failed.",+ },{ code: "artifact_tool_failed", message: ipcFrameError },]);}@@ -2575,17 +2810,14 @@ function testCodeModeFrameDiagnosticBoundaries() {"private path /customer/repo: IPC frame limit exceeded",]) {const diagnostics: CodexWorkerDiagnostic[] = [];- appendSafeItemDiagnostic(diagnostics, { type: "error", message });- appendSafeItemDiagnostic(diagnostics, {+ appendItemDiagnostic(diagnostics, { type: "error", message });+ appendItemDiagnostic(diagnostics, {...failedArtifactTool,result: resultWithText(message),});assert.deepEqual(diagnostics, [- {- code: "artifact_tool_failed",- message:- "Codex worker artifact tool get_codex_security_deep_reducer_inputs returned an error.",- },+ { code: "worker_error", message },+ { code: "artifact_tool_failed", message },]);}for (const item of [@@ -2594,6 +2826,11 @@ function testCodeModeFrameDiagnosticBoundaries() {server: "foreign_server",result: resultWithText(ipcFrameError),},+ {+ ...failedArtifactTool,+ server: "foreign_server",+ error: { message: "Synthetic unrelated transport failure" },+ },{type: "command_execution",status: "failed",@@ -2603,7 +2840,7 @@ function testCodeModeFrameDiagnosticBoundaries() {{ type: "unknown", status: "failed", error: { message: ipcFrameError } },]) {const diagnostics: CodexWorkerDiagnostic[] = [];- appendSafeItemDiagnostic(diagnostics, item);+ appendItemDiagnostic(diagnostics, item);assert.deepEqual(diagnostics, []);}}@@ -3106,6 +3343,7 @@ 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('IPC_DIAGNOSTIC_EVENT')) console.log(stdin.split('\\n')[1]);+if (process.env.FAKE_CODEX_ARTIFACT_EVENT) console.log(process.env.FAKE_CODEX_ARTIFACT_EVENT);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' } };@@ -3129,7 +3367,7 @@ if (stdin.includes('BWRAP_NAMESPACE_FAILURE')) console.log(JSON.stringify({ typeif (stdin.includes('ARTIFACT_TOOL_')) {const server = stdin.includes('FOREIGN_ARTIFACT_TOOL_') ? 'untrusted_server' : stdin.includes('LEGACY_OWNED_ARTIFACT_TOOL_') ? 'codex_security_artifacts' : 'cs_artifacts';const tool = stdin.includes('ADDITIONAL_OWNED_ARTIFACT_TOOL_') ? 'additional_codex_security_worker_tool' : stdin.includes('DISCOVERY_OWNED_ARTIFACT_TOOL_') ? 'record_codex_security_discovery_candidates' : 'record_codex_security_deep_reduction';- const item = { id: 'mcp-1', type: 'mcp_tool_call', server, tool, arguments: { secret: 'Bearer synthetic-secret', source: 'private source text' }, result: stdin.includes('REJECTED') ? { content: [{ type: 'text', text: 'private output sk-proj-synthetic-secret' }] } : null, error: stdin.includes('TRANSPORT_FAILED') ? { message: 'transport closed sk-proj-synthetic-secret /private/customer/path' } : null, status: 'failed' };+ const item = { id: 'mcp-1', type: 'mcp_tool_call', server, tool, arguments: { secret: 'Bearer synthetic-secret', source: 'private source text' }, result: stdin.includes('REJECTED') ? { content: [{ type: 'text', text: 'private output sk-proj-synthetic-secret' }] } : null, error: stdin.includes('TRANSPORT_FAILED') ? { message: '--provider-error transport closed sk-proj-synthetic-secret /private/customer/path' } : null, status: 'failed' };console.log(JSON.stringify({ type: 'item.completed', item }));}console.log(JSON.stringify({ type: 'item.completed', item: { id: 'message-1', type: 'agent_message', text: 'fixture final response' } }));diff --git a/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts b/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.tsindex ffa0b9b47..ad405f8b5 100644--- a/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts+++ b/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts@@ -273,15 +273,14 @@ async function assertBundledNodeLauncher() {}}-async function assertMissingPythonError() {+async function assertPythonLaunchError(code: "ENOENT" | "EACCES") {+ const python = path.join(tmpdir(), `codex-security-python-${randomUUID()}`);+ if (code === "EACCES") await writeFile(python, "", { mode: 0o600 });const missingPythonServer = startTestServer({cwd: pluginRoot,env: {CODEX_SECURITY_STATE_DIR: stateDir,- PYTHON: path.join(- tmpdir(),- `codex-security-missing-python-${randomUUID()}`,- ),+ PYTHON: python,},});try {@@ -298,15 +297,17 @@ async function assertMissingPythonError() {const errorText = response.result.content.map((item: { text: string }) => item.text).join(" ");- assert.match(errorText, /could not start its Python 3 helper/);- assert.match(errorText, /bundled Python runtime/);- assert.match(errorText, /set the PYTHON environment variable/);- assert.doesNotMatch(- errorText,- /ENOENT|spawn .*codex-security-missing-python/,- );+ assert.ok(errorText.includes(`spawn ${python} ${code}`), errorText);+ if (code === "ENOENT") {+ assert.match(errorText, /could not start its Python 3 helper/);+ assert.match(errorText, /bundled Python runtime/);+ assert.match(errorText, /set the PYTHON environment variable/);+ } else {+ assert.doesNotMatch(errorText, /Reinstall or update/);+ }} finally {await missingPythonServer.stop();+ await rm(python, { force: true });}}@@ -1438,7 +1439,8 @@ try {await assertBundledNodeLauncher();await assertBundledPythonRuntime();- await assertMissingPythonError();+ await assertPythonLaunchError("ENOENT");+ if (process.platform !== "win32") await assertPythonLaunchError("EACCES");await assertWorkbenchStdinFailureDoesNotCrashServer();await assertUnavailableUserInputFallback();for (const scope of [diff --git a/plugins/codex-security/mcp-app/tests/test_python_command.ts b/plugins/codex-security/mcp-app/tests/test_python_command.tsindex 80b78c3e7..fe738f8cd 100644--- a/plugins/codex-security/mcp-app/tests/test_python_command.ts+++ b/plugins/codex-security/mcp-app/tests/test_python_command.ts@@ -179,7 +179,9 @@ try {process.execPath,["-e",- `process.stdin.resume(); process.stdin.on('end', () => { const text = Buffer.from(${JSON.stringify(diagnostic)}); process.stderr.write(text.subarray(0, 26)); setTimeout(() => { process.stderr.write(text.subarray(26)); process.exitCode = 2; }, 1); });`,+ "process.stdin.resume(); process.stdin.on('end', () => { const text = Buffer.from(process.argv[1]); process.stderr.write(text.subarray(0, 26)); setTimeout(() => { process.stderr.write(text.subarray(26)); process.exitCode = 2; }, 1); });",+ "--",+ diagnostic,],"","Fixture helper",@@ -191,7 +193,9 @@ try {process.execPath,["-e",- `process.stderr.write(${JSON.stringify(diagnostic)}); process.exit(1);`,+ "process.stderr.write(process.argv[1]); process.exit(1);",+ "--",+ diagnostic,],input,"Fixture helper",@@ -237,10 +241,18 @@ try {}const pythonCommand = "/selected/python";-for (const code of ["ENOENT", "EACCES", "ENOEXEC", "UNKNOWN"]) {- assert.match(- missingPythonHelperMessage({ code, path: pythonCommand }, pythonCommand)!,- /could not start its Python 3 helper/,+const launchError = Object.assign(new Error(`spawn ${pythonCommand} ENOENT`), {+ code: "ENOENT",+ path: pythonCommand,+});+assert.match(+ missingPythonHelperMessage(launchError, pythonCommand)!,+ /spawn \/selected\/python ENOENT\n.*could not start its Python 3 helper/,+);+for (const code of ["EACCES", "ENOEXEC", "UNKNOWN"]) {+ assert.equal(+ missingPythonHelperMessage({ code, path: pythonCommand }, pythonCommand),+ undefined,);}assert.equal(diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_database.ts b/plugins/codex-security/mcp-app/tests/test_workbench_database.tsindex 8b04b5327..ff6dc51a7 100644--- a/plugins/codex-security/mcp-app/tests/test_workbench_database.ts+++ b/plugins/codex-security/mcp-app/tests/test_workbench_database.ts@@ -75,6 +75,27 @@ function insertScan(database: DatabaseSync, target = "/synthetic/repository") {.run(target);}+test("retains SQLite open errors and recovery guidance", async () => {+ const directory = await temporary.create("workbench-database-error-");+ const databasePath = join(directory, "workbench.sqlite3");+ await mkdir(databasePath);+ await assert.rejects(+ openWorkbenchDatabase(databasePath),+ (error: unknown) => {+ assert.ok(error instanceof Error);+ assert.equal((error as Error & { errcode: number }).errcode, 14);+ assert.equal(+ (error as Error & { code: string }).code,+ "ERR_SQLITE_ERROR",+ );+ assert.ok(error.message.includes("unable to open database file"));+ assert.ok(error.message.includes(databasePath));+ assert.ok(error.message.includes("CODEX_SECURITY_STATE_DIR"));+ return true;+ },+ );+});+test("opens a private WAL database at the configured state path", async () => {const directory = await temporary.create("workbench-database-");const home = join(directory, "codex-home");diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.pyindex fc09b4e3b..1217d33b7 100644--- a/plugins/codex-security/scripts/workbench_db.py+++ b/plugins/codex-security/scripts/workbench_db.py@@ -218,6 +218,11 @@ def release_completion_file_lock(descriptor: int) -> None:def connect(*, deferred: bool = False) -> sqlite3.Connection:path = database_path()+ guidance = (+ f"Workbench database: {path}. Ensure the state directory and SQLite journal files "+ "are writable, or set CODEX_SECURITY_STATE_DIR to a writable directory outside "+ "the scanned repository."+ )try:create_private_directory(path.parent)except OSError as exc:@@ -230,6 +235,7 @@ def connect(*, deferred: bool = False) -> sqlite3.Connection:except sqlite3.OperationalError as exc:if str(exc) == "unable to open database file":exc._codex_security_state_unavailable = True+ print(guidance, file=sys.stderr)raisetry:connection.row_factory = sqlite3.Row@@ -243,6 +249,7 @@ def connect(*, deferred: bool = False) -> sqlite3.Connection:except sqlite3.OperationalError as exc:connection.close()if attempt == SQLITE_RETRY_ATTEMPTS - 1 or not sqlite_busy(exc):+ print(guidance, file=sys.stderr)raisetime.sleep(0.05 * (2**attempt))raise AssertionError("SQLite retry loop exhausted unexpectedly.")diff --git a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py b/plugins/codex-security/tests/test_workbench_setup_and_migrations.pyindex f5b9d64ae..5e12899bf 100644--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py@@ -114,7 +114,7 @@ def test_state_directory_failure_preserves_original_exception(workbench_api, tmp@pytest.mark.parametrize("during_open", [True, False])def test_state_open_failure_metadata_is_limited_to_sqlite_connect(- workbench_api, tmp_path, during_open+ workbench_api, tmp_path, during_open, capsys):error = sqlite3.OperationalError("unable to open database file")connect = workbench_api["connect"]@@ -138,7 +138,12 @@ def test_state_open_failure_metadata_is_limited_to_sqlite_connect():connect()assert failure.value is error+ assert str(error) == "unable to open database file"assert getattr(error, "_codex_security_state_unavailable", False) is during_open+ detail = capsys.readouterr().err+ assert str(tmp_path / "state" / "workbench.sqlite3") in detail+ assert "SQLite journal files" in detail+ assert "CODEX_SECURITY_STATE_DIR" in detailfinally:connection.close()diff --git a/sdk/typescript/bin/codex-security.mjs b/sdk/typescript/bin/codex-security.mjsindex 7d1dbec57..848590a00 100755--- a/sdk/typescript/bin/codex-security.mjs+++ b/sdk/typescript/bin/codex-security.mjs@@ -19,8 +19,15 @@ void launch().then((exitCode) => {process.exitCode = exitCode;},- () => {- process.stderr.write("codex-security: Failed to start Codex Security.\n");+ (error) => {+ const message = error instanceof Error ? error.message : String(error);+ const display = message.replaceAll(+ /[\u0000-\u0009\u000B-\u001F\u007F-\u009F\u2028\u2029]/gu,+ " ",+ );+ process.stderr.write(+ `codex-security: Failed to start Codex Security: ${display}\n`,+ );process.exitCode = 2;},);diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.tsindex e13affa1c..17a21a644 100644--- a/sdk/typescript/src/api.ts+++ b/sdk/typescript/src/api.ts@@ -4274,6 +4274,10 @@ async function collectResult(await requireScanFile(scanDir, name, name, signal);} catch (error) {if (signal.aborted) throw signal.reason ?? error;+ let cause = error;+ while (cause instanceof Error && cause.cause !== undefined)+ cause = cause.cause;+ if (!isRecord(cause) || cause["code"] !== "ENOENT") throw error;missing.push(name);}}diff --git a/sdk/typescript/src/auth.ts b/sdk/typescript/src/auth.tsindex ee6f21af8..41ec1b544 100644--- a/sdk/typescript/src/auth.ts+++ b/sdk/typescript/src/auth.ts@@ -5,7 +5,11 @@ import { homedir } from "node:os";import { join, resolve } from "node:path";import { parse } from "smol-toml";import type { JsonObject } from "./config.js";-import { CodexSecurityError, PluginBootstrapError } from "./errors.js";+import {+ CodexSecurityError,+ PluginBootstrapError,+ errorMessage,+} from "./errors.js";import {executablePathForSpawn,expandHome,@@ -68,7 +72,8 @@ export async function readCodexHomeConfig(signal?.throwIfAborted();if ((error as NodeJS.ErrnoException).code === "ENOENT") return {};throw new CodexSecurityError(- "Could not read the configured Codex provider.",+ `Could not read the configured Codex provider: ${errorMessage(error)}`,+ { cause: error },);}}diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex d40a5331c..279a98c7f 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -1590,10 +1590,8 @@ export async function runCodexSkillCommand(process.on("SIGTERM", onTerminate);try {let diagnostic = "";- invocation.stderr?.on("data", (chunk: Buffer) => {- diagnostic = `${diagnostic}${chunk.toString("utf8")}`.slice(- -64 * 1_024,- );+ invocation.stderr?.setEncoding("utf8").on("data", (chunk: string) => {+ diagnostic += chunk;});const captured =output === undefined || invocation.stdout === null@@ -1641,7 +1639,7 @@ export async function runCodexSkillCommand(if (events?.sandboxUnavailable && requestedSignal === null) {throw new PatchCommandError("SANDBOX_UNAVAILABLE",- SANDBOX_UNAVAILABLE_MESSAGE,+ `${SANDBOX_UNAVAILABLE_MESSAGE}${events.error ? `\n${events.error}` : ""}`,);}if (status === 0 && output?.appServer !== undefined && events?.error) {@@ -1650,9 +1648,15 @@ export async function runCodexSkillCommand(if (output === undefined || status === 130 || status === 143)return status;if (status !== 0) {+ const failure = skillCommandFailure(+ output.command,+ status,+ events?.error ?? diagnostic,+ authentication,+ );await writeCliOutput(output.stderr,- `codex-security: ${skillCommandFailure(output.command, status, events?.error ?? diagnostic, authentication)}\n`,+ `codex-security: ${diagnosticLines(failure)}\n`,);return status;}@@ -1795,7 +1799,7 @@ export async function main(await dependencies.runWorkbench(args, undefined, undefined, pythonPath),);} catch (error) {- errorOutput.write(`codex-security: ${errorMessage(error)}\n`);+ errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);exitCode = 2;throw error;}@@ -1871,7 +1875,7 @@ export async function main(: interrupted === "SIGTERM"? "Finding matching terminated by SIGTERM. Saved comparisons are preserved.": errorMessage(error);- errorOutput.write(`codex-security: ${message}\n`);+ errorOutput.write(`codex-security: ${diagnosticLines(message)}\n`);throw error;} finally {removeListeners();@@ -2234,7 +2238,7 @@ export async function main(scanArguments.showCost = options.showCost;} catch (error) {const message = errorMessage(error);- errorOutput.write(`codex-security: ${message}\n`);+ errorOutput.write(`codex-security: ${diagnosticLines(message)}\n`);return finishScan({ exitCode: 2, error: message },format,@@ -2293,7 +2297,8 @@ export async function main(scanId = args.scanId ?? (await latestScans())?.[0]?.scanId;} catch (error) {const message = errorMessage(error);- if (exitCode === 0) errorOutput.write(`codex-security: ${message}\n`);+ if (exitCode === 0)+ errorOutput.write(`codex-security: ${diagnosticLines(message)}\n`);return finishScan({ exitCode: 2, error: message },format,@@ -2376,7 +2381,7 @@ export async function main(scanArguments.showCost = options.showCost;} catch (error) {const message = errorMessage(error);- errorOutput.write(`codex-security: ${message}\n`);+ errorOutput.write(`codex-security: ${diagnosticLines(message)}\n`);return finishScan({ exitCode: 2, error: message },format,@@ -2486,7 +2491,7 @@ export async function main(errorOutput.write(`codex-security: ${reason}${recovery}\n`);exitCode = signal === "SIGINT" ? 130 : 143;} else {- errorOutput.write(`codex-security: ${errorMessage(error)}\n`);+ errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);exitCode = 2;}};@@ -3644,7 +3649,7 @@ export async function main();} catch (error) {const message = errorMessage(error);- errorOutput.write(`${message}\n`);+ errorOutput.write(`${diagnosticLines(message)}\n`);outcome = { exitCode: 2, error: message };}@@ -3727,7 +3732,7 @@ export async function main(failOnSeverity: options.failOnSeverity,};} catch (error) {- errorOutput.write(`codex-security: ${errorMessage(error)}\n`);+ errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);exitCode = 2;return undefined;}@@ -4329,7 +4334,7 @@ export async function main(} catch (error) {stopDashboard();exitCode = interruptedExitCode(controller.signal) ?? 2;- errorOutput.write(`codex-security: ${errorMessage(error)}\n`);+ errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);} finally {stopDashboard();signalHandlers(dependencies, "remove", onInterrupt, onTerminate);@@ -4646,7 +4651,7 @@ export async function main((error instanceof Error && error.name === "ExitPromptError"? 130: 2);- errorOutput.write(`codex-security: ${errorMessage(error)}\n`);+ errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);} finally {removeSignals();}@@ -4781,12 +4786,12 @@ export async function main(}exitCode = 0;} catch (error) {- errorOutput.write(`codex-security: ${errorMessage(error)}\n`);+ errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);exitCode = 2;}} catch (error) {if (exitCode !== 2) {- errorOutput.write(`codex-security: ${errorMessage(error)}\n`);+ errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);}exitCode = 2;}@@ -4819,7 +4824,7 @@ export async function main();} catch (error) {exitCode = 2;- errorOutput.write(`codex-security: ${errorMessage(error)}\n`);+ errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);}},})@@ -5567,7 +5572,7 @@ export async function main(output,);} catch (error) {- errorOutput.write(`codex-security: ${errorMessage(error)}\n`);+ errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);exitCode = 1;}},@@ -5599,7 +5604,7 @@ export async function main(return { path };} catch (error) {exitCode = 2;- errorOutput.write(`codex-security: ${errorMessage(error)}\n`);+ errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);}},})@@ -6129,7 +6134,7 @@ export async function main(} else {if (exitCode !== 0) return exitCode;errorOutput.write(- `codex-security: ${errorMessage(incurErrorMessage(frameworkOutput))}\n`,+ `codex-security: ${diagnosticLines(incurErrorMessage(frameworkOutput))}\n`,);return 2;}@@ -6179,7 +6184,7 @@ export async function main();return exitCode;} catch (error) {- errorOutput.write(`codex-security: ${errorMessage(error)}\n`);+ errorOutput.write(`codex-security: ${diagnosticLines(error)}\n`);return 2;}}@@ -7903,10 +7908,14 @@ export async function readSkillCommandOutput(}startTurn();} else if (value["id"] === 5) {- const result = value["result"] as { exitCode: number };+ const result = value["result"] as {+ exitCode: number;+ stdout: string;+ stderr: string;+ };if (result.exitCode !== 0) {sandboxUnavailable = true;- error = SANDBOX_UNAVAILABLE_MESSAGE;+ error = result.stderr || result.stdout;appServer.input.end();continue;}@@ -7999,25 +8008,9 @@ export function skillCommandFailure(detail,)) {- return authenticationFailureMessage(authentication);- }- if (- /403|model.not.found|model.*access|access.*model|permission/iu.test(detail)- ) {- return "The selected model is unavailable for the current credentials.";+ return `${detail}\n${authenticationFailureMessage(authentication)}`;}- if (/429|rate.limit|tokens.per.minute/iu.test(detail)) {- return "The request was rate limited. Wait and retry.";- }- if (- /models?.cache|cache.*schema|supports_reasoning_summaries/iu.test(detail)- ) {- return "Codex could not load its model metadata. Update Codex or refresh its model cache.";- }- if (/econn|enotfound|network|timed.out|timeout/iu.test(detail)) {- return "Codex could not connect to the model service. Check the network and retry.";- }- return `${command} failed with exit code ${status}.`;+ return detail || `${command} failed with exit code ${status}.`;}function incurErrorMessage(output: string): string {@@ -8032,9 +8025,13 @@ function incurErrorMessage(output: string): string {type VerboseDiagnosticValue = string | number | boolean | null | undefined;+function diagnosticLines(value: unknown): string {+ return errorMessage(value).split("\n").map(diagnosticValue).join("\n");+}+function diagnosticValue(value: unknown): string {return errorMessage(value).replaceAll(- /[\u0000-\u001F\u007F\u0085\u2028\u2029]/gu,+ /[\u0000-\u001F\u007F-\u009F\u2028\u2029]/gu," ",);}@@ -9797,7 +9794,7 @@ if (invokedAsMain()) {process.exitCode = exitCode;},(error: unknown) => {- process.stderr.write(`codex-security: ${errorMessage(error)}\n`);+ process.stderr.write(`codex-security: ${diagnosticLines(error)}\n`);process.exitCode = 2;},);diff --git a/sdk/typescript/src/contract.ts b/sdk/typescript/src/contract.tsindex 35d11b429..aeca99e3a 100644--- a/sdk/typescript/src/contract.ts+++ b/sdk/typescript/src/contract.ts@@ -34,17 +34,6 @@ const DOCUMENTS = {"coverage.json": "coverage.schema.json",} as const;const PRODUCER_NAME = "codex-security-plugin";-const SAFE_SCHEMA_ERROR_PROPERTIES = new Set([- "scan",- "target",- "remote",- "completedAt",- "sealedAt",- "artifacts",- "findings",- "coverage",- "scope",-]);interface CheckedScanFile {path: string;metadata: Stats;@@ -969,7 +958,7 @@ function validateParsedJson(value: unknown, context: string): void {`${context}: expected well-formed Unicode JSON keys.`,);}- validateParsedJson(item, `${context}.<property>`);+ validateParsedJson(item, `${context}[${JSON.stringify(key)}]`);}}}@@ -1142,22 +1131,13 @@ function schemaError(errors: readonly ErrorObject[],): ContractValidationError {const first = errors[0];- const segments = first?.instancePath.split("/").filter(Boolean) ?? [];- const location =- segments.length === 0- ? "<root>"- : segments- .map((segment) => {- if (/^(?:0|[1-9]\d{0,9})$/.test(segment)) return segment;- return SAFE_SCHEMA_ERROR_PROPERTIES.has(segment)- ? segment- : "<property>";- })- .join(".");+ const location = first?.instancePath+ ? JSON.stringify(first.instancePath)+ : "<root>";const keyword = first?.keyword ?? "unknown";const count = errors.length;return new ContractValidationError(- `${filename}:${location}: schema validation failed (${keyword}${keyword === "format" && first?.params["format"] === "date-time" ? "; date-time" : ""}; ${count} ${count === 1 ? "error" : "errors"}).`,+ `${filename}:${location}: schema validation failed (${keyword}${keyword === "format" && first?.params["format"] === "date-time" ? "; date-time" : ""}; ${count} ${count === 1 ? "error" : "errors"})${first?.message ? `: ${first.message}` : ""}.`,);}diff --git a/sdk/typescript/src/findings-import.ts b/sdk/typescript/src/findings-import.tsindex 929ea85c3..078b43242 100644--- a/sdk/typescript/src/findings-import.ts+++ b/sdk/typescript/src/findings-import.ts@@ -4,7 +4,7 @@ import { isAbsolute, join, posix } from "node:path";import Ajv2020 from "ajv/dist/2020.js";import { z } from "incur";import Papa from "papaparse";-import { CodexSecurityError } from "./errors.js";+import { CodexSecurityError, errorMessage } from "./errors.js";import type { Finding, FindingsDocument } from "./models.js";export const CSV_TARGET_ID = "codex-security-csv-import";@@ -118,9 +118,10 @@ export async function parseImportedFindings(// the CSV parser already skips.payload = JSON.parse(source.replace(/^\uFEFF/u, ""));} catch (error) {- throw new CodexSecurityError("Findings JSON could not be parsed.", {- cause: error,- });+ throw new CodexSecurityError(+ `Findings JSON could not be parsed. ${errorMessage(error)}`,+ { cause: error },+ );}if (payload === null ||diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.tsindex 9987621d6..655266b35 100644--- a/sdk/typescript/src/runtime.ts+++ b/sdk/typescript/src/runtime.ts@@ -1828,22 +1828,9 @@ export async function runWorkbench(}if (options.signal?.aborted) throw error;const detail = processErrorDetail(error);- const databaseFailure =- /\b(?:unable to open database file|attempt to write a readonly database|readonly database|disk i\/o error)\b/iu.test(- detail,- );const failure =options.failureMessage ?? "Could not run the Codex Security workbench";- throw new CodexSecurityError(- databaseFailure- ? `${failure}: cannot open the workbench database at ${join(- options.stateDirectory ??- codexSecurityStateDirectory(options.environment),- "workbench.sqlite3",- )}. Ensure the state directory and SQLite journal files are writable, or set CODEX_SECURITY_STATE_DIR to a writable directory outside the scanned repository.`- : `${failure}: ${detail}`,- { cause: error },- );+ throw new CodexSecurityError(`${failure}: ${detail}`, { cause: error });}let result: unknown;try {diff --git a/sdk/typescript/src/saved-scan-bootstrap.ts b/sdk/typescript/src/saved-scan-bootstrap.tsindex 6de191f06..3fa7ce081 100644--- a/sdk/typescript/src/saved-scan-bootstrap.ts+++ b/sdk/typescript/src/saved-scan-bootstrap.ts@@ -8,7 +8,7 @@ import {gitMarkerRoot,gitProtectionRoots,} from "./targets.js";-import { CodexSecurityError } from "./errors.js";+import { CodexSecurityError, errorMessage } from "./errors.js";import type { SavedScanDependencies } from "./saved-scan.js";import {codexSecurityStateDirectory,@@ -253,7 +253,7 @@ async function readTargets(signal?.throwIfAborted();if (error instanceof CodexSecurityError) throw error;throw new CodexSecurityError(- "Could not read saved scan targets before Python discovery.",+ `Could not read saved scan targets before Python discovery: ${errorMessage(error)}`,{ cause: error },);} finally {diff --git a/sdk/typescript/src/server/embeddings.ts b/sdk/typescript/src/server/embeddings.tsindex d76c4b101..4f68a9ef4 100644--- a/sdk/typescript/src/server/embeddings.ts+++ b/sdk/typescript/src/server/embeddings.ts@@ -1,5 +1,6 @@import { Tiktoken } from "js-tiktoken/lite";import cl100kBase from "js-tiktoken/ranks/cl100k_base";+import { errorMessage } from "../errors.js";import type { Finding } from "../models.js";import { FindingsError } from "./errors.js";import type { FindingEmbedding } from "./storage.js";@@ -100,11 +101,11 @@ export class OpenAiFindingEmbedder implements FindingEmbedder {input: chunks.map(({ tokens }) => tokens),}),});- } catch {+ } catch (error) {this.signal?.throwIfAborted();throw new FindingsError("embedding_failed",- "Could not reach the embedding provider.",+ `Could not request finding embeddings: ${errorMessage(error)}`,);}if (!response.ok) {diff --git a/sdk/typescript/tests-ts/api-events.test.ts b/sdk/typescript/tests-ts/api-events.test.tsindex 2aa7fb9d1..8c786f53c 100644--- a/sdk/typescript/tests-ts/api-events.test.ts+++ b/sdk/typescript/tests-ts/api-events.test.ts@@ -1,6 +1,6 @@import { copyCompletedScan, PLUGIN_ROOT } from "./plugin-root.js";import { once } from "node:events";-import { mkdir, stat } from "node:fs/promises";+import { chmod, mkdir, rm, stat } from "node:fs/promises";import { existsSync } from "node:fs";import { join } from "node:path";import {@@ -11,6 +11,7 @@ import {import { afterEach, describe, expect, test, mock } from "bun:test";import {CodexSecurityError,+ ContractValidationError,IncompleteScanError,ScanInterruptedError,type ScanAuthentication,@@ -1146,3 +1147,26 @@ describe("one-shot scan events", () => {]);});});++test("missing required artifacts remain incomplete scans", async () => {+ const scanDir = await copyCompletedScan(await temporaryDirectory());+ await rm(join(scanDir, "report.md"));+ await expect(runEvents(scanDir, completedEvents())).rejects.toMatchObject({+ name: IncompleteScanError.name,+ message:+ "Codex Security scan completed without required artifacts: report.md",+ });+});++test.skipIf(process.platform === "win32")(+ "preserves unsafe scan-root diagnostics instead of reporting missing files",+ async () => {+ const scanDir = await copyCompletedScan(await temporaryDirectory());+ await chmod(scanDir, 0o755);+ await expect(runEvents(scanDir, completedEvents())).rejects.toMatchObject({+ name: ContractValidationError.name,+ message: expect.stringContaining("chmod 700"),+ cause: expect.any(Error),+ });+ },+);diff --git a/sdk/typescript/tests-ts/auth.test.ts b/sdk/typescript/tests-ts/auth.test.tsindex 073a5b23b..10188aa73 100644--- a/sdk/typescript/tests-ts/auth.test.ts+++ b/sdk/typescript/tests-ts/auth.test.ts@@ -1,5 +1,5 @@import { nodeCommand } from "./support/shell.js";-import { writeFile } from "node:fs/promises";+import { chmod, writeFile } from "node:fs/promises";import { join } from "node:path";import { setTimeout as delay } from "node:timers/promises";import { pathToFileURL } from "node:url";@@ -9,6 +9,7 @@ import {CodexLoginHandle,loginApiKey,logout,+ readCodexHomeConfig,} from "../src/auth.js";import { PluginBootstrapError } from "../src/index.js";import { runCodexCommand } from "../src/runtime.js";@@ -76,6 +77,48 @@ process.exit(process.exitCode ?? 0);}describe("Codex authentication process boundary", () => {+ test("preserves configuration parse details and allows absent configuration", async () => {+ const home = await temporaryDirectory();+ const environment = { CODEX_HOME: home };+ await expect(readCodexHomeConfig(environment)).resolves.toEqual({});+ await writeFile(+ join(home, "config.toml"),+ '[model_providers.synthetic\nwire_api = "responses"\n',+ );+ await expect(readCodexHomeConfig(environment)).rejects.toMatchObject({+ name: "CodexSecurityError",+ message: expect.stringContaining("illegal character in key"),+ cause: expect.objectContaining({+ message: expect.stringContaining("model_providers.synthetic"),+ }),+ });+ const controller = new AbortController();+ const canceled = new Error("Synthetic configuration cancellation");+ controller.abort(canceled);+ await expect(+ readCodexHomeConfig(environment, controller.signal),+ ).rejects.toBe(canceled);+ });++ test.skipIf(process.platform === "win32" || process.getuid?.() === 0)(+ "preserves native configuration permission failures",+ async () => {+ const home = await temporaryDirectory();+ const file = join(home, "config.toml");+ await writeFile(file, 'model = "synthetic"\n', { mode: 0o000 });+ try {+ await expect(+ readCodexHomeConfig({ CODEX_HOME: home }),+ ).rejects.toMatchObject({+ message: expect.stringContaining("EACCES"),+ cause: expect.objectContaining({ code: "EACCES", path: file }),+ });+ } finally {+ await chmod(file, 0o600);+ }+ },+ );+test("persists API keys through the exact public Codex executable", async () => {const { command, environment } = await fakeCodex();await expect(loginApiKey(command, environment, "")).rejects.toBeInstanceOf(diff --git a/sdk/typescript/tests-ts/cli-error-diagnostics.test.ts b/sdk/typescript/tests-ts/cli-error-diagnostics.test.tsnew file mode 100644index 000000000..7a406a662--- /dev/null+++ b/sdk/typescript/tests-ts/cli-error-diagnostics.test.ts@@ -0,0 +1,163 @@+import { rm, writeFile } from "node:fs/promises";+import { join } from "node:path";+import { expect, test } from "bun:test";+import { readCodexHomeConfig } from "../src/auth.js";+import { main } from "../src/cli.js";+import { parseImportedFindings } from "../src/findings-import.js";+import { PLUGIN_ROOT } from "./plugin-root.js";+import { dependencies } from "./cli-fixtures.js";+import { createCliTest } from "./support/cli-run.js";+import { temporaryDirectory } from "./support/temporary-directories.js";++const controls = "\u001b]52;c;U1lOVEhFVElD\u0007\u009b2J";+const detail = `Synthetic sk-proj-SYNTHETIC_KEY_123 ${controls}\nsecond diagnostic line`;+const escapedDetail =+ "Synthetic sk-proj-SYNTHETIC_KEY_123 ]52;c;U1lOVEhFVElD 2J\nsecond diagnostic line";+const escapedSingleLineDetail =+ "Synthetic sk-proj-SYNTHETIC_KEY_123 ]52;c;U1lOVEhFVElD 2J second diagnostic line";++test("feedback escapes real config diagnostics only at the text output boundary", async () => {+ const root = await temporaryDirectory("feedback-config-diagnostic-", true);+ const environment = { CODEX_HOME: root };+ try {+ await writeFile(join(root, "config.toml"), `broken = "${controls}"\n`, {+ mode: 0o600,+ });+ const failure = await readCodexHomeConfig(environment).catch(+ (error: unknown) => error,+ );+ expect(failure).toBeInstanceOf(Error);+ expect((failure as Error).message).toContain(controls);+ expect(((failure as Error).cause as Error).message).toContain(controls);+ const { stdout, stderr, runCli } = createCliTest(main);+ expect(+ await runCli(+ ["feedback", "--reason", "Synthetic problem"],+ dependencies({ environment }),+ ),+ ).toBe(2);+ expect(stdout.text()).toBe("");+ expect(stderr.text()).toContain(+ "Could not read the configured Codex provider",+ );+ expect(stderr.text()).toContain("]52;c;U1lOVEhFVElD");+ expect(stderr.text()).not.toMatch(+ /[\u0000-\u0009\u000b-\u001f\u007f-\u009f]/u,+ );+ expect((failure as Error).message).toContain(controls);+ expect(((failure as Error).cause as Error).message).toContain(controls);+ } finally {+ await rm(root, { recursive: true, force: true });+ }+});++for (const scenario of [+ "history",+ "resume",+ "rerun",+ "scan run",+ "export",+] as const) {+ test(`${scenario} escapes text failures while preserving structured and SDK details`, async () => {+ const cause = new Error("Synthetic cause");+ const failure = new Error(detail, { cause });+ const fail = () => {+ throw failure;+ };+ const deps = dependencies({+ ...(scenario === "history" ||+ scenario === "resume" ||+ scenario === "rerun"+ ? { onWorkbench: fail }+ : {}),+ ...(scenario === "scan run" ? { onRun: fail } : {}),+ });+ if (scenario === "export") deps.exportFindings = async () => fail();+ const args =+ scenario === "history"+ ? ["feedback", "scan-known", "--reason", "Synthetic problem"]+ : scenario === "resume"+ ? ["scans", "resume", "scan-known", "--json"]+ : scenario === "rerun"+ ? ["scans", "rerun", "--json"]+ : scenario === "export"+ ? ["export", "scan", "--export-format", "json", "--output", "-"]+ : ["scan", ".", "--json"];+ const { stdout, stderr, runCli } = createCliTest(main);+ expect(await runCli(args, deps)).toBe(2);+ expect(stderr.text()).toContain(+ scenario === "scan run" ? escapedSingleLineDetail : escapedDetail,+ );+ expect(stderr.text()).not.toContain(controls);+ if (+ scenario === "resume" ||+ scenario === "rerun" ||+ scenario.startsWith("scan")+ ) {+ expect(JSON.parse(stdout.text()).message).toContain(+ scenario === "scan run" ? escapedSingleLineDetail : detail,+ );+ } else {+ expect(stdout.text()).toBe("");+ }+ expect(failure.message).toBe(detail);+ expect(failure.cause).toBe(cause);+ });+}++test("successful raw exports preserve terminal controls as artifact bytes", async () => {+ const deps = dependencies();+ deps.exportFindings = async () => Buffer.from(detail);+ const { stdout, stderr, runCli } = createCliTest(main);+ expect(+ await runCli(+ ["export", "scan", "--export-format", "json", "--output", "-"],+ deps,+ ),+ ).toBe(0);+ expect(stdout.text()).toBe(detail);+ expect(stderr.text()).toBe("");+});++for (const command of ["import", "owners"] as const) {+ test(`${command} escapes actual malformed JSON diagnostics without changing parser details`, async () => {+ const root = await temporaryDirectory("malformed-json-diagnostic-", true);+ try {+ const source = "\u009b2J";+ const input = join(root, "findings.json");+ await writeFile(input, source);+ const failure = await parseImportedFindings(+ source,+ "json",+ PLUGIN_ROOT,+ ).catch((error: unknown) => error);+ expect(failure).toBeInstanceOf(Error);+ const message = (failure as Error).message;+ const { stdout, stderr, runCli } = createCliTest(main);+ expect(+ await runCli(+ command === "import"+ ? [+ "scan",+ "import",+ "--json",+ input,+ "--dry-run",+ "--format",+ "json",+ ]+ : ["suggest-owners", input],+ dependencies({ currentDirectory: root }),+ ),+ ).toBe(2);+ expect(stderr.text()).toContain("Findings JSON");+ expect(stderr.text()).not.toMatch(+ /[\u0000-\u0009\u000b-\u001f\u007f-\u009f]/u,+ );+ expect(stdout.text()).toBe("");+ expect((failure as Error).message).toBe(message);+ } finally {+ await rm(root, { recursive: true, force: true });+ }+ });+}diff --git a/sdk/typescript/tests-ts/cli-launcher.test.ts b/sdk/typescript/tests-ts/cli-launcher.test.tsindex bf269ad6c..15db72c14 100644--- a/sdk/typescript/tests-ts/cli-launcher.test.ts+++ b/sdk/typescript/tests-ts/cli-launcher.test.ts@@ -42,12 +42,18 @@ describe("CLI launcher", () => {const preload = join(root, "unavailable-cwd.mjs");await writeFile(preload,- `Object.defineProperty(process, "cwd", { value() { throw new Error(${JSON.stringify(`working directory is unavailable: ${SYNTHETIC_CREDENTIALS}`)}); } });\n`,+ 'Object.defineProperty(process, "cwd", { value() { throw new Error(process.env.TEST_DIAGNOSTIC); } });\n',);const { status, stdout, stderr } = await runCommand(process.execPath,["--preload", preload, join(packageRoot, "src", "cli.ts"), "scan"],- { timeout: 30_000 },+ {+ env: {+ ...process.env,+ TEST_DIAGNOSTIC: `working directory is unavailable: ${SYNTHETIC_CREDENTIALS}`,+ },+ timeout: 30_000,+ },);expect(status, stderr).toBe(2);@@ -60,29 +66,51 @@ describe("CLI launcher", () => {}});- test("maps installed-launcher failures to a fixed startup error", async () => {- const root = await temporaryDirectory("codex-security-cli-bin-failure-");- try {- const launcher = join(root, "bin", "codex-security.mjs");- await mkdir(join(root, "bin"), { recursive: true });- await mkdir(join(root, "dist"), { recursive: true });- await copyFile(join(packageRoot, "bin", "codex-security.mjs"), launcher);- await writeFile(- join(root, "dist", "cli.js"),- `throw new Error(${JSON.stringify(`failed ${SYNTHETIC_CREDENTIALS}`)});\n`,- );- const child = await runCommand("node", [launcher], {- env: { ...process.env, NODE_NO_WARNINGS: "1" },- timeout: 30_000,- });+ test.each(["import", "main", "missing", "success"])(+ "preserves installed-launcher %s results",+ async (scenario) => {+ const root = await temporaryDirectory("codex-security-cli-bin-failure-");+ try {+ const launcher = join(root, "bin", "codex-security.mjs");+ await mkdir(join(root, "bin"), { recursive: true });+ await mkdir(join(root, "dist"), { recursive: true });+ await copyFile(+ join(packageRoot, "bin", "codex-security.mjs"),+ launcher,+ );+ const detail = `EACCES: failed ${SYNTHETIC_CREDENTIALS}\u001b[31m\rnext\nline café 🔒\u001b]52;c;U1lOVEhFVElD\u0007 C1 \u0080\u009b2J\u009bH\u009d52;c;U1lOVEhFVElD\u009c\u009f end`;+ const display = `EACCES: failed ${SYNTHETIC_CREDENTIALS} [31m next\nline café 🔒 ]52;c;U1lOVEhFVElD C1 2J H 52;c;U1lOVEhFVElD end`;+ if (scenario !== "missing")+ await writeFile(+ join(root, "dist", "cli.js"),+ scenario === "success"+ ? "export const main = () => 7;\n"+ : scenario === "main"+ ? "export const main = async () => { throw process.env.TEST_DIAGNOSTIC; };\n"+ : "throw new Error(process.env.TEST_DIAGNOSTIC);\n",+ );+ const child = await runCommand("node", [launcher], {+ env: {+ ...process.env,+ NODE_NO_WARNINGS: "1",+ TEST_DIAGNOSTIC: detail,+ },+ timeout: 30_000,+ });- expect(child.status).toBe(2);- expect(child.stdout).toBe("");- expect(child.stderr).toBe(- "codex-security: Failed to start Codex Security.\n",- );- } finally {- await rm(root, { recursive: true, force: true });- }- });+ expect(child.status).toBe(scenario === "success" ? 7 : 2);+ expect(child.stdout).toBe("");+ if (scenario === "success") expect(child.stderr).toBe("");+ else if (scenario === "missing") {+ expect(child.stderr).toContain("Cannot find module");+ expect(child.stderr).toContain(join(root, "dist", "cli.js"));+ } else+ expect(child.stderr).toBe(+ `codex-security: Failed to start Codex Security: ${display}\n`,+ );+ } finally {+ await rm(root, { recursive: true, force: true });+ }+ },+ );});diff --git a/sdk/typescript/tests-ts/cli-patch-results.test.ts b/sdk/typescript/tests-ts/cli-patch-results.test.tsindex c54994901..03a41fabe 100644--- a/sdk/typescript/tests-ts/cli-patch-results.test.ts+++ b/sdk/typescript/tests-ts/cli-patch-results.test.ts@@ -238,10 +238,22 @@ lines.on("line", (line) => {expect(outcome.stderr).toContain("No patch was applied; 0 files changed.");});- test.each(["exit", "rpc"])(+ test.each(["exit", "rpc", "stdout", "empty"])("fails before starting a model turn when sandbox preflight returns %s failure",async (failure) => {await using fixture = await repositoryFixture();+ const diagnostic =+ "sandbox: unshare: Permission denied /synthetic/repository sk-proj-SYNTHETIC_SECRET";+ const response =+ failure === "rpc"+ ? { error: { code: -32603, message: diagnostic } }+ : {+ result: {+ exitCode: 1,+ stdout: failure === "stdout" ? diagnostic : "",+ stderr: failure === "exit" ? diagnostic : "",+ },+ };const source = `const assert = require("node:assert/strict");const lines = require("node:readline").createInterface({ input: process.stdin });@@ -253,7 +265,7 @@ lines.on("line", (line) => {if (request.method === "command/exec") {assert.equal(request.params.sandboxPolicy.type, "workspaceWrite");assert.deepEqual(request.params.command, [process.execPath, "-e", ""]);- send({ id: request.id, ${failure === "rpc" ? 'error: { code: -32603, message: "sandbox: unshare: Permission denied /private/repository sk-proj-SYNTHETIC_SECRET" }' : 'result: { exitCode: 1, stdout: "", stderr: "sandbox: unshare: Permission denied /private/repository sk-proj-SYNTHETIC_SECRET" }'} });+ send({ id: request.id, ...${JSON.stringify(response)} });}if (request.method === "turn/start") throw new Error("Model turn must not start");});`;@@ -268,9 +280,10 @@ lines.on("line", (line) => {ok: false,error: { code: "SANDBOX_UNAVAILABLE" },});- expect(outcome.stdout + outcome.stderr).not.toContain("SYNTHETIC_SECRET");- expect(outcome.stdout + outcome.stderr).not.toContain(- "/private/repository",+ if (failure !== "empty")+ expect(outcome.stdout + outcome.stderr).toContain(diagnostic);+ expect(outcome.stderr).toContain(+ "No patch was applied; 0 files changed.",);expect(await fixture.git("status", "--porcelain")).toBe("");},diff --git a/sdk/typescript/tests-ts/cli-publish.test.ts b/sdk/typescript/tests-ts/cli-publish.test.tsindex 89e745e18..5521fd1b3 100644--- a/sdk/typescript/tests-ts/cli-publish.test.ts+++ b/sdk/typescript/tests-ts/cli-publish.test.ts@@ -1,10 +1,23 @@-import { mkdir, rm, symlink, writeFile } from "node:fs/promises";+import {+ chmod,+ cp,+ mkdir,+ readFile,+ rm,+ symlink,+ writeFile,+} from "node:fs/promises";import { homedir, tmpdir } from "node:os";import { dirname, join, resolve } from "node:path";import { stripVTControlCharacters } from "node:util";import { afterEach, describe, expect, test, mock } from "bun:test";import { main } from "../src/cli.js";-import type { CheckScanPublicationResult } from "../src/publish.js";+import {+ checkScanPublicationInternal,+ type CheckScanPublicationResult,+} from "../src/publish.js";+import { ContractValidationError } from "../src/errors.js";+import { PLUGIN_ROOT } from "./plugin-root.js";import { capture, dependencies, FakeSignals } from "./cli-fixtures.js";import { createApiTestFixtures } from "./support/temporary-directories.js";import { fail } from "./support/errors.js";@@ -319,6 +332,86 @@ describe("publish scan to custom", () => {});describe("publish check", () => {+ test("escapes contract property controls at the CLI boundary without changing the SDK error", async () => {+ const scanDir = join(await publicationDirectory(), "scan");+ await cp(join(PLUGIN_ROOT, "examples", "completed-scan"), scanDir, {+ recursive: true,+ });+ if (process.platform !== "win32") await chmod(scanDir, 0o700);+ const file = join(scanDir, "findings.json");+ const finding = JSON.parse(await readFile(file, "utf8"));+ const key =+ "source\u009b2J\u009dtitle\u009c\u0085\u2028\u2029 café token=SYNTHETIC_VALUE";+ finding.extensions = { [key]: Number.MAX_SAFE_INTEGER + 1 };+ await writeFile(file, JSON.stringify(finding));+ const { stdout, stderr, runCli } = createCliTest(main);+ const deps = dependencies();+ let sdkError: unknown;+ deps.checkScanPublication = async (directory, options) => {+ try {+ return await checkScanPublicationInternal(directory, options, {+ environment: { CODEX_SECURITY_LINEAR_API_KEY: "" },+ inspectPublicationStore: async () =>+ fail("Invalid artifacts must not reach history."),+ linearClient: () => fail("Invalid artifacts must not reach Linear."),+ });+ } catch (error) {+ sdkError = error;+ throw error;+ }+ };+ deps.publishScan = async () => fail("Check must not publish.");+ expect(+ await runCli(+ ["publish", "check", scanDir, ...DESTINATION_OPTIONS, "--json"],+ deps,+ ),+ ).toBe(2);+ expect(sdkError).toBeInstanceOf(ContractValidationError);+ expect((sdkError as Error).message).toContain(key);+ expect((sdkError as Error).message).toContain(+ "unsafe integer-valued JSON numbers",+ );+ expect(stderr.text()).not.toMatch(+ /[\u0000-\u0009\u000b-\u001f\u007f-\u009f\u2028\u2029]/u,+ );+ expect(stderr.text()).toContain("source 2J title");+ expect(stderr.text()).toContain("café token=SYNTHETIC_VALUE");+ expect(stderr.text()).toContain("unsafe integer-valued JSON numbers");+ expect(stdout.text().trim()).toBe("");+ });++ test("preserves multiline publication-check diagnostics and their original cause", async () => {+ const cause = new Error("Synthetic original cause.");+ const message =+ "First café token=SYNTHETIC_VALUE\u001b[2J\r\nSecond 日本語\u009b2J\tend";+ const failure = new Error(message, { cause });+ const deps = dependencies();+ deps.checkScanPublication = async () => {+ throw failure;+ };+ const { stdout, stderr, runCli } = createCliTest(main);+ expect(+ await runCli(+ [+ "publish",+ "check",+ "completed-scan",+ ...DESTINATION_OPTIONS,+ "--json",+ ],+ deps,+ ),+ ).toBe(2);+ expect(stderr.text()).toContain(+ "First café token=SYNTHETIC_VALUE [2J \nSecond 日本語 2J end\n",+ );+ expect(stderr.text()).not.toMatch(/[\u001b\u009b\r\t]/u);+ expect(failure.message).toBe(message);+ expect(failure.cause).toBe(cause);+ expect(stdout.text().trim()).toBe("");+ });+test.each(["completed-scan", "~/completed-scan"])("resolves %s and shared options without invoking publication",async (scanDir) => {diff --git a/sdk/typescript/tests-ts/cli-skills.test.ts b/sdk/typescript/tests-ts/cli-skills.test.tsindex ad7562c64..31b10494e 100644--- a/sdk/typescript/tests-ts/cli-skills.test.ts+++ b/sdk/typescript/tests-ts/cli-skills.test.ts@@ -1263,26 +1263,60 @@ process.stdout.write(JSON.stringify({expect(stderr.text()).toBe("");});- test("summarizes skill failures without echoing credentials or private paths", () => {- const cases = [- ["401 sk-proj-SYNTHETIC_SECRET", "Authentication failed"],- [- "403 model access denied /private/repository",- "selected model is unavailable",- ],- ["429 tokens per minute sk-proj-SYNTHETIC_SECRET", "rate limited"],- [- "models cache supports_reasoning_summaries /private/home",- "model metadata",- ],- ["ENOTFOUND /private/repository", "could not connect"],- ["unknown sk-proj-SYNTHETIC_SECRET /private/repository", "exit code 7"],- ];- for (const [detail, expected] of cases) {- const message = skillCommandFailure("validate", 7, detail!);- expect(message).toContain(expected!);- expect(message).not.toContain("SYNTHETIC_SECRET");- expect(message).not.toContain("/private");+ test("preserves skill failures and adds authentication advice without replacing details", () => {+ for (const detail of [+ "403 model access denied /synthetic/repository",+ "429 tokens per minute sk-proj-SYNTHETIC_SECRET",+ "models cache supports_reasoning_summaries /synthetic/home",+ "ENOTFOUND /synthetic/repository",+ "EACCES: permission denied, open /synthetic/output/report.json",+ "Unsupported provider setting: synthetic_option",+ "raw detail \u001b[31m\rnext\nline sk-proj-SYNTHETIC_SECRET C1 \u0080\u009b2J\u009bH\u009d52;c;U1lOVEhFVElD\u009c\u009f end",+ ])+ expect(skillCommandFailure("validate", 7, detail)).toBe(detail);+ const authentication = "401 sk-proj-SYNTHETIC_SECRET";+ expect(skillCommandFailure("validate", 7, authentication)).toContain(+ authentication,+ );+ expect(skillCommandFailure("validate", 7, authentication)).toContain(+ "Authentication failed",+ );+ expect(skillCommandFailure("validate", 7, "")).toBe(+ "validate failed with exit code 7.",+ );+ });++ test("escapes native validation launch failures at the CLI boundary", async () => {+ const directory = await temporaryDirectory("validation-launch-failure-");+ try {+ const { stdout, stderr, runCli } = createCliTest(main);+ const command = join(+ directory,+ "missing-codex\u001b[31m\rnext\nline-café",+ );+ expect(+ await runCli(+ ["validate", "Synthetic finding"],+ dependencies({+ currentDirectory: directory,+ environment: {+ PATH: process.env["PATH"],+ CODEX_HOME: join(directory, "home"),+ CODEX_SECURITY_STATE_DIR: join(directory, "state"),+ OPENAI_API_KEY: "synthetic-validation-key",+ },+ onCodex: (_args, output, environment) =>+ runCodexSkillCommand([], output, { command }, environment),+ }),+ ),+ ).toBe(2);+ expect(stdout.text()).toBe("");+ expect(stderr.text()).toContain("missing-codex");+ expect(stderr.text()).toContain("line-café");+ expect(stderr.text()).not.toContain("\u001b");+ expect(stderr.text()).not.toContain("\r");+ } finally {+ await rm(directory, { recursive: true, force: true });}});@@ -1334,8 +1368,29 @@ process.stdout.write(JSON.stringify({"process.exitCode=7",status: 7,stdout: "",- stderr: "Authentication failed",+ stderr: "401 sk-proj-SYNTHETIC_SECRET",},+ {+ source:+ 'process.stderr.write("EACCES: synthetic permission denial\\n" + "é🔒".repeat(25000));process.exitCode=7;',+ status: 7,+ stdout: "",+ stderr: "EACCES: synthetic permission denial\n" + "é🔒".repeat(25000),+ },+ ...["stderr", "turn.failed"].map((transport) => {+ const detail =+ "EACCES: permission denied, open /synthetic/output/report.json\u001b[31m\rnext\nline café 🔒 sk-proj-SYNTHETIC_SECRET\u001b]52;c;U1lOVEhFVElD\u0007 C1 \u0080\u009b2J\u009bH\u009d52;c;U1lOVEhFVElD\u009c\u009f end";+ return {+ source:+ transport === "stderr"+ ? `process.stderr.write(${JSON.stringify(detail)}); process.exitCode=7;`+ : `process.stdout.write(JSON.stringify({type:"turn.failed",error:{message:${JSON.stringify(detail)}}})+"\\n"); process.exitCode=7;`,+ status: 7,+ stdout: "",+ stderr:+ "EACCES: permission denied, open /synthetic/output/report.json [31m next\nline café 🔒 sk-proj-SYNTHETIC_SECRET ]52;c;U1lOVEhFVElD C1 2J H 52;c;U1lOVEhFVElD end",+ };+ }),{source:'process.stdout.write(JSON.stringify({type:"turn.completed"})+"\\n")',@@ -1361,8 +1416,6 @@ process.stdout.write(JSON.stringify({} else {expect(stderr.text()).toContain(scenario.stderr);}- expect(stderr.text()).not.toContain("SYNTHETIC_SECRET");- expect(stderr.text()).not.toContain("/private");}});@@ -1614,8 +1667,9 @@ lines.on("line", (line) => {).resolves.toBe(1);expect(stdout.text()).toBe("");expect(stderr.text()).toContain("Authentication failed");- expect(stderr.text()).not.toContain("SYNTHETIC_SECRET");- expect(stderr.text()).not.toContain("/private");+ expect(stderr.text()).toContain(+ "401 sk-proj-SYNTHETIC_SECRET /private/repository",+ );});test.skipIf(process.platform === "win32")(diff --git a/sdk/typescript/tests-ts/contract.test.ts b/sdk/typescript/tests-ts/contract.test.tsindex d77adbbd0..5b712ab1e 100644--- a/sdk/typescript/tests-ts/contract.test.ts+++ b/sdk/typescript/tests-ts/contract.test.ts@@ -679,9 +679,9 @@ describe("canonical scan contract", () => {).resolves.toBeDefined();});- test("does not expose attacker-controlled keys in validation errors", async () => {+ test("identifies unusual property names unambiguously in validation errors", async () => {const scanDir = await copyExample();- const marker = "PRIVATE_JSON_KEY";+ const marker = 'synthetic/"~\nkey';await writeFile(join(scanDir, "findings.json"),JSON.stringify({ [marker]: 9007199254740992 }),@@ -696,7 +696,7 @@ describe("canonical scan contract", () => {expect(thrown).toBeInstanceOf(ContractValidationError);expect((thrown as Error).message).toContain("unsafe integer-valued");- expect((thrown as Error).message).not.toContain(marker);+ expect((thrown as Error).message).toContain(`[${JSON.stringify(marker)}]`);});test("accepts valid schemas beyond the previous complexity limit", async () => {@@ -1105,7 +1105,7 @@ describe("canonical scan contract", () => {manifest["scan"]["target"]["remote"] ="https://example.com\\@evil.test/repo";},- "scan.target.remote",+ "/scan/target/remote",],];@@ -1153,6 +1153,24 @@ describe("canonical scan contract", () => {);});+ test("identifies the failing schema field and required member", async () => {+ for (const field of ["severity", "confidence"]) {+ const scanDir = await copyExample();+ const path = join(scanDir, "findings.json");+ const document = await readJson(path);+ document["findings"][0][field].level = null;+ await writeJson(path, document);+ await expect(+ loadContract(scanDir, { pluginRoot: PLUGIN_ROOT }),+ ).rejects.toThrow(`/findings/0/${field}/level`);+ delete document["findings"][0][field].level;+ await writeJson(path, document);+ await expect(+ loadContract(scanDir, { pluginRoot: PLUGIN_ROOT }),+ ).rejects.toThrow("required property 'level'");+ }+ });+test("rejects unsafe and non-finite JSON numbers before contract typing", async () => {for (const [field, expected] of [["startLine", "unsafe integer-valued JSON numbers"],@@ -1179,7 +1197,7 @@ describe("canonical scan contract", () => {];}if (field === "overflow") {- finding["extensions"] = { overflow: 0 };+ finding["extensions"] = { 'evidence/"~\n': { overflow: 0 } };}await writeJson(findingsPath, findings);let text = await readFile(findingsPath, "utf8");@@ -1208,6 +1226,10 @@ describe("canonical scan contract", () => {await expect(loadContract(scanDir, { pluginRoot: PLUGIN_ROOT }),).rejects.toThrow(expected);+ if (field === "overflow")+ await expect(+ loadContract(scanDir, { pluginRoot: PLUGIN_ROOT }),+ ).rejects.toThrow('["extensions"]["evidence/\\"~\\n"]["overflow"]');}});diff --git a/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts b/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.tsindex e9ca03545..244e01a02 100644--- a/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts+++ b/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts@@ -125,63 +125,65 @@ test("advertises distinct Standard worker and Deep reducer contracts", async ()}});-test("classifies owned worker tool failures without exposing their contents", async () => {+test("preserves owned worker tool failure diagnostics", async () => {const runtime = await loadBundledRuntime();- const diagnosticSource = bundledFunction(runtime, "appendSafeItemDiagnostic");+ const diagnosticSource = bundledFunction(runtime, "appendItemDiagnostic");const recordHelper = /\b(isRecord\d*)\(item\)/u.exec(diagnosticSource)?.[1];expect(recordHelper).toBeDefined();const appendDiagnostic = new Function([bundledFunction(runtime, recordHelper!),bundledFunction(runtime, "isSandboxNamespaceExhaustion"),+ bundledFunction(runtime, "isCodeModeFrameError"),+ bundledFunction(runtime, "appendStreamDiagnostic"),bundledFunction(runtime, "appendUniqueDiagnostic"),- bundledFunction(runtime, "appendCodeModeFrameDiagnostic"),diagnosticSource,- "return appendSafeItemDiagnostic;",+ "return appendItemDiagnostic;",].join("\n"),)() as (diagnostics: Array<{ code: string; message: string }>,event: Record<string, unknown>,) => void;- const secret = "synthetic-secret-never-log";+ const detail = "synthetic-provider-detail";for (const fixture of [{server: "cs_artifacts",tool: "record_codex_security_deep_reduction",- result: { isError: true, content: [{ text: secret }] },+ result: { isError: true, content: [{ type: "text", text: detail }] },error: null,- reason: "returned an error",+ message: detail,},{server: "cs_artifacts",tool: "additional_codex_security_worker_tool",result: null,- error: { message: secret },- reason: "transport failed",+ error: { message: detail },+ message: detail,},{server: "codex_security_artifacts",tool: "record_codex_security_discovery_candidates",result: null,error: null,- reason: "failed",+ message:+ "Codex worker artifact tool record_codex_security_discovery_candidates failed.",},]) {const diagnostics: Array<{ code: string; message: string }> = [];appendDiagnostic(diagnostics, {type: "mcp_tool_call",status: "failed",- arguments: { token: secret },+ arguments: { token: "synthetic-input-only" },...fixture,});expect(diagnostics).toEqual([{code: "artifact_tool_failed",- message: `Codex worker artifact tool ${fixture.tool} ${fixture.reason}.`,+ message: fixture.message,},]);- expect(JSON.stringify(diagnostics)).not.toContain(secret);+ expect(JSON.stringify(diagnostics)).not.toContain("synthetic-input-only");}const unrelatedDiagnostics: Array<{ code: string; message: string }> = [];@@ -191,30 +193,16 @@ test("classifies owned worker tool failures without exposing their contents", asserver: "unrelated_server",tool: "record_codex_security_deep_reduction",result: { isError: true },- error: null,+ error: { message: "Synthetic unrelated transport failure" },});expect(unrelatedDiagnostics).toEqual([]);-});--test("keeps textual missing-path worker failures retryable", async () => {- const runtime = await loadBundledRuntime();- const classify = new Function(- [- bundledFunction(runtime, "classifyCodexWorkerError"),- "return classifyCodexWorkerError;",- ].join("\n"),- )() as (error: Error) => Error;-- for (const diagnostic of [- "Error: No such file or directory (os error 2)",- "Error: The system cannot find the file specified. (os error 2)",- ]) {- const original = new Error(- ["Codex Exec exited with code 1:", diagnostic].join("\n"),- );- const classified = classify(original);- expect(classified).toBe(original);- }+ appendDiagnostic(unrelatedDiagnostics, {+ type: "file_change",+ status: "failed",+ });+ expect(unrelatedDiagnostics).toEqual([+ { code: "file_change_failed", message: "Codex worker file change failed." },+ ]);});test("resumes only when the exact Standard worker or reducer result is missing", async () => {diff --git a/sdk/typescript/tests-ts/deep-scan-windows-executable.test.ts b/sdk/typescript/tests-ts/deep-scan-windows-executable.test.tsindex 0e95ea9bc..c297c44b7 100644--- a/sdk/typescript/tests-ts/deep-scan-windows-executable.test.ts+++ b/sdk/typescript/tests-ts/deep-scan-windows-executable.test.ts@@ -166,7 +166,7 @@ test.each([},);-test("keeps Windows executable permission failures retryable", async () => {+test("keeps operating-system worker failures retryable", async () => {const runtime = await loadBundledRuntime();const source =/function classifyCodexWorkerError\([^\n]*\) \{[\s\S]*?\n\}/u.exec(@@ -176,9 +176,16 @@ test("keeps Windows executable permission failures retryable", async () => {const classify = new Function(`${source}\nreturn classifyCodexWorkerError;`,)() as (error: Error) => Error;- const original = Object.assign(new Error("spawn codex EPERM"), {- code: "EPERM",- });- const result = classify(original);- expect(result).toBe(original);+ for (const original of [+ Object.assign(new Error("spawn codex EPERM"), { code: "EPERM" }),+ ...[+ "Error: No such file or directory (os error 2)",+ "Error: The system cannot find the file specified. (os error 2)",+ ].map(+ (diagnostic) =>+ new Error(`Codex Exec exited with code 1:\n${diagnostic}`),+ ),+ ]) {+ expect(classify(original)).toBe(original);+ }});diff --git a/sdk/typescript/tests-ts/deep-scan-worker-shutdown.test.ts b/sdk/typescript/tests-ts/deep-scan-worker-shutdown.test.tsindex 9425d263d..2ff4520ae 100644--- a/sdk/typescript/tests-ts/deep-scan-worker-shutdown.test.ts+++ b/sdk/typescript/tests-ts/deep-scan-worker-shutdown.test.ts@@ -63,7 +63,7 @@ async function bundledWorkerExecutor("resolveCodexPath","executablePathForSpawn","workerSubagentConfig",- "appendSafeItemDiagnostic",+ "appendItemDiagnostic","classifyCodexWorkerError",`${source}\nreturn CodexSdkWorkerExecutor;`,)(diff --git a/sdk/typescript/tests-ts/finding-embeddings.test.ts b/sdk/typescript/tests-ts/finding-embeddings.test.tsindex 2065ce3a7..acfef9fb1 100644--- a/sdk/typescript/tests-ts/finding-embeddings.test.ts+++ b/sdk/typescript/tests-ts/finding-embeddings.test.ts@@ -156,7 +156,8 @@ test("does not resolve credentials for empty input or reuse a key after renewal});const embedder = new OpenAiFindingEmbedder(() => {if (++credentials === 1) return "synthetic-key";- if (failure === "throw") throw new Error("synthetic-private-token");+ if (failure === "throw")+ throw new Error("Credential source unavailable: synthetic fixture");return "";}, observeRequests);expect(await embedder.embed([])).toEqual([]);@@ -164,13 +165,25 @@ test("does not resolve credentials for empty input or reuse a key after renewalawait embedder.embed([example]);await expect(embedder.embed([example])).rejects.toMatchObject({code: "embedding_failed",- message: "Could not reach the embedding provider.",+ message: `Could not request finding embeddings: ${failure === "throw" ? "Credential source unavailable: synthetic fixture" : "Missing embedding credentials"}`,});expect(credentials).toBe(2);expect(observeRequests).toHaveBeenCalledTimes(1);}});+test("preserves fetch failure diagnostics", async () => {+ const embedder = new OpenAiFindingEmbedder(+ "synthetic-key",+ rejecting("ECONNREFUSED: synthetic endpoint"),+ );+ await expect(embedder.embed([example])).rejects.toMatchObject({+ code: "embedding_failed",+ message:+ "Could not request finding embeddings: ECONNREFUSED: synthetic endpoint",+ });+});+test("does not call the provider for empty input or missing credentials", async () => {const observeCalls = mock(rejecting("Must not call"));const embedder = new OpenAiFindingEmbedder(undefined, observeCalls);diff --git a/sdk/typescript/tests-ts/findings-import.test.ts b/sdk/typescript/tests-ts/findings-import.test.tsindex 33c364460..37f5f5f87 100644--- a/sdk/typescript/tests-ts/findings-import.test.ts+++ b/sdk/typescript/tests-ts/findings-import.test.ts@@ -249,9 +249,22 @@ describe("findings import formats", () => {parseImportedFindings(JSON.stringify(payload), "json", PLUGIN_ROOT),).rejects.toThrow("Findings JSON");}- await expect(- parseImportedFindings("{", "json", PLUGIN_ROOT),- ).rejects.toThrow("could not be parsed");+ for (const source of ["{", '{"findings": [],}']) {+ let parseError: Error | undefined;+ try {+ JSON.parse(source);+ } catch (error) {+ parseError = error as Error;+ }+ expect(parseError).toBeInstanceOf(SyntaxError);+ await expect(+ parseImportedFindings(source, "json", PLUGIN_ROOT),+ ).rejects.toMatchObject({+ name: "CodexSecurityError",+ message: `Findings JSON could not be parsed. ${parseError!.message}`,+ cause: expect.objectContaining({ message: parseError!.message }),+ });+ }await expect(parseImportedFindings(JSON.stringify({diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.tsindex c9e34415e..34fa6cfae 100644--- a/sdk/typescript/tests-ts/runtime.test.ts+++ b/sdk/typescript/tests-ts/runtime.test.ts@@ -5943,46 +5943,84 @@ describe("runtime directories and plugin Python boundary", () => {expect(await readdir(root)).toEqual(["scan"]);});- test("reports an unwritable SQLite state directory without a Python traceback", async () => {- const root = await temporaryDirectory();- const pluginRoot = join(root, "plugin");- const stateDirectory = join(root, "persistent-state");- await mkdir(join(pluginRoot, "scripts"), { recursive: true });- await writeFile(- join(pluginRoot, "scripts", "workbench_db.py"),- [- "import sqlite3",- "def connect():",- " raise sqlite3.OperationalError('unable to open database file')",- "connect()",- ].join("\n"),+ for (const readonly of [false, true]) {+ test+ .skipIf(+ readonly && (process.platform === "win32" || process.getuid?.() === 0),+ )+ .each(["list-scans", "database-info"])(+ `preserves SQLite ${readonly ? "readonly" : "open"} diagnostics and recovery guidance for %s`,+ async (command) => {+ const root = await temporaryDirectory();+ const stateDirectory = join(root, "state");+ const databasePath = join(stateDirectory, "workbench.sqlite3");+ await mkdir(stateDirectory);+ if (readonly) await writeFile(databasePath, "", { mode: 0o400 });+ else await mkdir(databasePath);+ try {+ let failure: unknown;+ try {+ await runWorkbench(+ {+ pluginRoot: PLUGIN_ROOT,+ environment: {+ ...process.env,+ CODEX_SECURITY_STATE_DIR: stateDirectory,+ },+ failureMessage: "Could not read Codex Security scan history",+ },+ [command],+ );+ } catch (error) {+ failure = error;+ }++ expect(failure).toBeInstanceOf(Error);+ const message = (failure as Error).message;+ expect(message).toContain(+ "Could not read Codex Security scan history",+ );+ expect(message).toContain(+ readonly+ ? "attempt to write a readonly database"+ : "unable to open database file",+ );+ expect(message).toContain(databasePath);+ expect(message).toContain("SQLite journal files");+ expect(message).toContain("CODEX_SECURITY_STATE_DIR");+ if (command === "list-scans") {+ expect(message).toContain("sqlite3.OperationalError");+ expect(message).toContain("Traceback");+ }+ expect((failure as Error).cause).toBeInstanceOf(Error);+ } finally {+ if (readonly) await chmod(databasePath, 0o600);+ }+ },);- const python = Bun.which("python3") ?? Bun.which("python");- expect(python).not.toBeNull();+ }- let failure: unknown;- try {- await runWorkbench(- {- python: python!,- pluginRoot,- environment: { CODEX_SECURITY_STATE_DIR: stateDirectory },- failureMessage: "Could not save the Codex Security scan",- },- ["register-cli-scan"],+ test.each(["plain-missing-target", "readonly database", "disk i/o error"])(+ "preserves missing-target diagnostics containing %s",+ async (name) => {+ const root = await temporaryDirectory();+ const target = join(root, name);+ await expect(+ runWorkbench(+ {+ pluginRoot: PLUGIN_ROOT,+ environment: {+ ...process.env,+ CODEX_SECURITY_STATE_DIR: join(root, "state"),+ },+ },+ ["inspect-target", "--target-path", target],+ ),+ ).rejects.toThrow(+ `Scan target is not a readable local directory: ${target}`,);- } catch (error) {- failure = error;- }-- expect(failure).toBeInstanceOf(Error);- const message = (failure as Error).message;- expect(message).toContain("Could not save the Codex Security scan");- expect(message).toContain(join(stateDirectory, "workbench.sqlite3"));- expect(message).toContain("SQLite journal files are writable");- expect(message).toContain("CODEX_SECURITY_STATE_DIR");- expect(message).not.toContain("Traceback");- });+ },+ );testPosix("rejects private output directories owned by another user", () => {expect(() =>diff --git a/sdk/typescript/tests-ts/saved-scan-bootstrap.test.ts b/sdk/typescript/tests-ts/saved-scan-bootstrap.test.tsindex a00829510..3c1e9a78f 100644--- a/sdk/typescript/tests-ts/saved-scan-bootstrap.test.ts+++ b/sdk/typescript/tests-ts/saved-scan-bootstrap.test.ts@@ -491,17 +491,34 @@ test("bootstrap fails before Python for ambiguous, absent, malformed and changedawait expect(savedScanWorkbench(f.first.scanId + "}".repeat(100_000) + "x", options),).rejects.toThrow("not found");- const missing = join(f.root, "missing-state");- await expect(- savedScanWorkbench(f.first.scanId, {- ...options,- environment: {- ...options.environment,- CODEX_SECURITY_STATE_DIR: missing,- },- }),- ).rejects.toThrow("before Python discovery");- expect(existsSync(missing)).toBe(false);+ for (const [kind, detail] of [+ ["missing", "unable to open database file"],+ ["corrupt", "file is not a database"],+ ["schema", "no such table: scans"],+ ] as const) {+ const state = join(f.root, `${kind}-state`);+ if (kind !== "missing") {+ await mkdir(state);+ const file = join(state, "workbench.sqlite3");+ if (kind === "corrupt") await writeFile(file, "not a SQLite database");+ else new Database(file).close();+ }+ await expect(+ savedScanWorkbench(f.first.scanId, {+ ...options,+ environment: {+ ...options.environment,+ CODEX_SECURITY_STATE_DIR: state,+ },+ }),+ ).rejects.toMatchObject({+ message: expect.stringContaining(detail),+ cause: expect.objectContaining({+ message: expect.stringContaining(detail),+ }),+ });+ if (kind === "missing") expect(existsSync(state)).toBe(false);+ }const pinned = await savedScanWorkbench(f.first.scanId, {...options,environment: { ...f.environment, PYTHON: f.python },