Skip to content

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

fix(diagnostics): preserve saved-scan errors and recovery advice (#1266)

Michael D'Angelo5d65d253f7eb
diff --git a/plugins/codex-security/mcp-app/server.ts b/plugins/codex-security/mcp-app/server.ts
index e704b3203..81f03b1bb 100644
--- a/plugins/codex-security/mcp-app/server.ts
+++ b/plugins/codex-security/mcp-app/server.ts
@@ -853,8 +853,7 @@ export function createCodexSecurityServer(): McpServer {
);
const setup = await runWorkbench([
"inspect-setup",
- "--target-path",
- selected.targetPath,
+ `--target-path=${selected.targetPath}`,
`--scope=${scope}`,
"--mode",
mode,
@@ -899,8 +898,7 @@ export function createCodexSecurityServer(): McpServer {
"save-workspace",
"--workspace-id",
sessionId,
- "--target-path",
- selected.targetPath,
+ `--target-path=${selected.targetPath}`,
`--scope=${scope}`,
"--mode",
mode,
@@ -2040,8 +2038,7 @@ async function startPromptOnlyScan(
"start-prompt-only-scan",
"--thread-id",
threadId,
- "--target-path",
- targetPath,
+ `--target-path=${targetPath}`,
`--scope=${scope}`,
"--mode",
mode,
@@ -2072,8 +2069,7 @@ async function startHeadlessStandardScan(
"start-headless-standard-scan",
"--thread-id",
threadId,
- "--target-path",
- input.targetPath,
+ `--target-path=${input.targetPath}`,
`--scope=${input.scope ?? "."}`,
...optionalArg("--model", modelSettings.model),
...optionalArg("--reasoning-effort", modelSettings.reasoningEffort),
diff --git a/plugins/codex-security/mcp-app/src/helpers/inventory-git.ts b/plugins/codex-security/mcp-app/src/helpers/inventory-git.ts
index d6df55961..94f28ebb1 100644
--- a/plugins/codex-security/mcp-app/src/helpers/inventory-git.ts
+++ b/plugins/codex-security/mcp-app/src/helpers/inventory-git.ts
@@ -597,19 +597,28 @@ export interface Change {
status: string;
}
async function changed(repo: string, args: string[]): Promise<Change[]> {
- const fields = paths(
- requireSuccess(
- await git(repo, [
- "diff",
- "--ignore-submodules=all",
- "--raw",
- "-z",
- "--diff-filter=ACMRDT",
- ...args,
- "--",
- ]),
- ),
- );
+ const result = await git(repo, [
+ "diff",
+ "--ignore-submodules=all",
+ "--raw",
+ "-z",
+ "--diff-filter=ACMRDT",
+ ...args,
+ "--",
+ ]);
+ if (result.status !== 0 && result.stderr.trim()) {
+ const detail = result.stderr
+ .trim()
+ .replace(
+ /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/gu,
+ (control) =>
+ `\\u${control.charCodeAt(0).toString(16).padStart(4, "0")}`,
+ );
+ throw new Error(
+ `Git diff failed with ${result.signal ?? `exit code ${result.status}`}: ${detail}`,
+ );
+ }
+ const fields = paths(requireSuccess(result));
const changes: Change[] = [];
for (let index = 0; index < fields.length;) {
const metadata = fields[index++]!.split(" "),
diff --git a/plugins/codex-security/mcp-app/src/helpers/normalize-candidates.ts b/plugins/codex-security/mcp-app/src/helpers/normalize-candidates.ts
index 6e16cfd45..2df9c164e 100644
--- a/plugins/codex-security/mcp-app/src/helpers/normalize-candidates.ts
+++ b/plugins/codex-security/mcp-app/src/helpers/normalize-candidates.ts
@@ -4,6 +4,7 @@ import { artifactSchemaDocuments } from "../artifact-contracts.js";
import { discoveryLocationRoles } from "../discovery-location-roles.js";
import { isRecord as object } from "../record.js";
import { decodeUtf8 } from "./utf8";
+import { escapeControls } from "./json";
import { createHash, randomBytes } from "node:crypto";
import {
closeSync,
@@ -544,7 +545,10 @@ export function normalizeCandidatesCommand(
else unlinkSync(temporary);
}
} catch (error) {
- if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT")
+ console.error(
+ `normalize_candidates: Could not remove temporary file: ${escapeControls((error as Error).message)}`,
+ );
}
}
console.log(
diff --git a/plugins/codex-security/mcp-app/src/sealed-contract.ts b/plugins/codex-security/mcp-app/src/sealed-contract.ts
index 951fdca71..aa6b99125 100644
--- a/plugins/codex-security/mcp-app/src/sealed-contract.ts
+++ b/plugins/codex-security/mcp-app/src/sealed-contract.ts
@@ -807,7 +807,7 @@ function openHeldFile(
error instanceof Error
)
throw new ContractValidationError(
- `${context}: unable to open the checked regular file.`,
+ `${context}: unable to open the checked regular file. ${filesystemErrorMessage(error)}`,
{
cause: Object.assign(error, { path: join(root.path, normalized) }),
},
diff --git a/plugins/codex-security/mcp-app/src/server/finding-issue-tools.ts b/plugins/codex-security/mcp-app/src/server/finding-issue-tools.ts
index b1ad13e03..9de8e08ec 100644
--- a/plugins/codex-security/mcp-app/src/server/finding-issue-tools.ts
+++ b/plugins/codex-security/mcp-app/src/server/finding-issue-tools.ts
@@ -60,10 +60,27 @@ export function registerFindingIssueTools(
): void {
const meta = { ui: { visibility: ["model"] } };
const execute = async (action: "inspect" | "record", input: object) => {
- const result = await runWorkbench(
- ["finding-issues"],
- JSON.stringify({ action, ...input }),
- );
+ let result: JsonObject;
+ try {
+ result = await runWorkbench(
+ ["finding-issues"],
+ JSON.stringify({ action, ...input }),
+ );
+ } catch (error) {
+ const processError = error instanceof Error ? error.cause : undefined;
+ if (
+ action !== "inspect" ||
+ !(processError instanceof Error) ||
+ !("code" in processError) ||
+ processError.code !== 73
+ )
+ throw error;
+ return {
+ isError: true,
+ content: [{ type: "text" as const, text: String(error) }],
+ structuredContent: { code: "state_unavailable" },
+ };
+ }
return {
content: [{ type: "text" as const, text: JSON.stringify(result) }],
structuredContent: result,
diff --git a/plugins/codex-security/mcp-app/src/server/workbench-process.ts b/plugins/codex-security/mcp-app/src/server/workbench-process.ts
index cabe2ddea..7a62de447 100644
--- a/plugins/codex-security/mcp-app/src/server/workbench-process.ts
+++ b/plugins/codex-security/mcp-app/src/server/workbench-process.ts
@@ -7,7 +7,11 @@ args = json.loads(sys.stdin.buffer.readline())
sys.argv = [str(script), *args]
if args and args[0] in {"inspect-target", "inspect-setup"}:
from workbench_setup import main
+ main()
else:
- from workbench_db import main
-main()
+ import workbench_db
+ try:
+ workbench_db.main()
+ except workbench_db.ContractError as error:
+ raise SystemExit(str(error)) from None
`;
diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_discovery.ts b/plugins/codex-security/mcp-app/tests/test_artifact_discovery.ts
index 5a5cdbf9c..7f1e51706 100644
--- a/plugins/codex-security/mcp-app/tests/test_artifact_discovery.ts
+++ b/plugins/codex-security/mcp-app/tests/test_artifact_discovery.ts
@@ -17,6 +17,7 @@ import {
writeFile,
} from "node:fs/promises";
import path from "node:path";
+import { pathToFileURL } from "node:url";
import { syncBuiltinESMExports } from "node:module";
import { mock } from "node:test";
import { importSource } from "./import-module.ts";
@@ -113,6 +114,7 @@ try {
await verifyReaderPreservesSharedPhaseRecords(scan);
await verifyNormalizerFailuresPreserveOutput(scan);
await verifyDiffInventoryAllowsDeletedFiles();
+ await verifyNormalizerDiagnostics(scan);
await verifyEmptyReplacement(scan);
await verifyWorkerContext();
await verifyMalformedLedgerIsNotModified();
@@ -364,9 +366,8 @@ async function verifyNormalizerFailuresPreserveOutput(
),
(error) =>
error instanceof Error &&
- /line range/u.test(error.message) &&
- !error.message.includes(context.root) &&
- !error.message.includes(context.repoRoot),
+ error.message ===
+ "discovery candidates: candidate input row 1: line range 9-9 exceeds src/routes.ts:3",
);
assert.equal(await readFile(destination, "utf8"), original);
@@ -879,3 +880,31 @@ function rawCandidate(overrides = {}) {
...overrides,
};
}
+
+async function verifyNormalizerDiagnostics(context: ArtifactContext) {
+ const workerRoot = path.join(root, "diagnostic-worker");
+ await mkdir(workerRoot);
+ const detail = `Cannot normalize ${context.repoRoot}; unrelated services${context.repoRoot}/main.py`;
+ await writeFile(
+ path.join(workerRoot, "helpers.mjs"),
+ `throw new Error(${JSON.stringify(detail)});`,
+ );
+ const { recordCodexSecurityDiscoveryCandidates: record } = await importSource(
+ path.join(import.meta.dirname, "../src/artifact-discovery.ts"),
+ {
+ define: {
+ "import.meta.url": JSON.stringify(
+ pathToFileURL(path.join(workerRoot, "server.mjs")).href,
+ ),
+ },
+ },
+ );
+ await assert.rejects(
+ record({ candidates: [rawCandidate()] }, context),
+ (error) => {
+ assert.ok(error instanceof Error);
+ assert.equal(error.message, detail);
+ return true;
+ },
+ );
+}
diff --git a/plugins/codex-security/mcp-app/tests/test_finding_issue_tools.ts b/plugins/codex-security/mcp-app/tests/test_finding_issue_tools.ts
index bee0865e2..6a5dae920 100644
--- a/plugins/codex-security/mcp-app/tests/test_finding_issue_tools.ts
+++ b/plugins/codex-security/mcp-app/tests/test_finding_issue_tools.ts
@@ -1,5 +1,13 @@
import assert from "node:assert/strict";
-import { cp, readFile, rm, stat, writeFile } from "node:fs/promises";
+import {
+ chmod,
+ cp,
+ mkdir,
+ readFile,
+ rm,
+ stat,
+ writeFile,
+} from "node:fs/promises";
import path from "node:path";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
@@ -72,6 +80,26 @@ try {
code: "ENOENT",
});
+ if (process.platform !== "win32" && process.getuid?.() !== 0) {
+ const database = path.join(stateDirectory, "workbench.sqlite3");
+ await mkdir(stateDirectory, { recursive: true, mode: 0o700 });
+ await writeFile(database, "");
+ await chmod(database, 0);
+ try {
+ const unavailable = await client.callTool({
+ name: "get_codex_security_finding_issues",
+ arguments: { ...source, destination: jira },
+ });
+ assert.equal(unavailable.isError, true);
+ assert.deepEqual(unavailable.structuredContent, {
+ code: "state_unavailable",
+ });
+ } finally {
+ await chmod(database, 0o600);
+ await rm(database);
+ }
+ }
+
const accepted = await call("record_codex_security_finding_issues", {
...source,
destination: jira,
diff --git a/plugins/codex-security/mcp-app/tests/test_large_findings_page.ts b/plugins/codex-security/mcp-app/tests/test_large_findings_page.ts
new file mode 100644
index 000000000..8e51b1fdd
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/test_large_findings_page.ts
@@ -0,0 +1,109 @@
+import assert from "node:assert/strict";
+import { execFile } from "node:child_process";
+import { rm } from "node:fs/promises";
+import path from "node:path";
+import { test } from "node:test";
+import { promisify } from "node:util";
+import { Client } from "@modelcontextprotocol/sdk/client/index.js";
+import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
+import { applicationRoot, buildServer } from "./build-server.ts";
+import { temporaryDirectory } from "./support/temporary-directories.ts";
+
+const exec = promisify(execFile);
+const pluginRoot = path.dirname(applicationRoot);
+const python = process.env.PYTHON || "python3";
+
+await test("returns an accepted 50-finding Unicode page through the MCP subprocess", async () => {
+ const root = await temporaryDirectory("codex-security-large-findings-");
+ const client = new Client({
+ name: "large-findings-fixture",
+ version: "1.0.0",
+ });
+ try {
+ const fixture = await exec(
+ python,
+ [
+ "-c",
+ `import copy, json, sys
+from pathlib import Path
+sys.path.insert(0, sys.argv[1])
+from workbench_test_support import empty_target_scan, run_workbench, write_completed_contract
+state, target, scan_id, scan_dir = empty_target_scan(Path(sys.argv[2]))
+write_completed_contract(scan_dir, scan_id, target)
+file = scan_dir / "findings.json"
+document = json.loads(file.read_text())
+template = document["findings"][0]
+document["findings"] = []
+for index in range(50):
+ finding = copy.deepcopy(template)
+ finding["identity"]["anchor"] = f"synthetic-large-page-{index}"
+ finding["locations"] = [{"path": "/".join(["é" * 64] * 15 + [f"fixture-{index}-{location}.py"]), "startLine": 1, "endLine": 1, "role": "sink"} for location in range(8)]
+ document["findings"].append(finding)
+file.write_text(json.dumps(document))
+run_workbench(state, "complete-scan", "--scan-id", scan_id)
+print(json.dumps({"scanId": scan_id, "target": str(target), "state": str(state)}))`,
+ path.join(pluginRoot, "tests"),
+ root,
+ ],
+ { maxBuffer: Infinity },
+ );
+ const { scanId, target, state } = JSON.parse(fixture.stdout);
+ const environment = {
+ ...process.env,
+ CODEX_SECURITY_STATE_DIR: state,
+ CODEX_HOME: path.join(root, "home"),
+ };
+ for (const limit of [20, 50]) {
+ const { stdout } = await exec(
+ python,
+ [
+ path.join(pluginRoot, "scripts/workbench_db.py"),
+ "list-findings",
+ "--scan-id",
+ scanId,
+ "--limit",
+ String(limit),
+ ],
+ { env: environment, maxBuffer: Infinity },
+ );
+ assert.equal(JSON.parse(stdout).findingsPage.findings.length, limit);
+ assert.equal(Buffer.byteLength(stdout) > 4 * 1024 * 1024, limit === 50);
+ }
+
+ const bundle = path.join(root, "server.cjs");
+ await buildServer(bundle, {
+ define: {
+ __dirname: JSON.stringify(applicationRoot),
+ "import.meta.url": "__filename",
+ },
+ });
+ const env = Object.fromEntries(
+ Object.entries(environment).filter(
+ (entry): entry is [string, string] => entry[1] !== undefined,
+ ),
+ );
+ await client.connect(
+ new StdioClientTransport({
+ command: process.execPath,
+ args: [bundle, "--stdio"],
+ cwd: target,
+ env,
+ }),
+ );
+ for (const limit of [20, 50]) {
+ const result = await client.callTool({
+ name: "list_codex_security_findings",
+ arguments: { scanId, limit },
+ });
+ assert.notEqual(result.isError, true, JSON.stringify(result));
+ const { findingsPage: page } = result.structuredContent as {
+ findingsPage: { findings: unknown[]; limit: number };
+ };
+ assert.equal(page.findings.length, limit);
+ assert.equal(page.limit, limit);
+ }
+ } finally {
+ await client.close();
+ await rm(root, { recursive: true, force: true });
+ }
+});
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.ts
index 817d272e9..c87daa76c 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
@@ -1349,6 +1349,13 @@ try {
code: "ENOENT",
});
+ const dashQuery = await request(8999, "tools/call", {
+ name: "list_codex_security_scans",
+ arguments: { query: "--synthetic-query", limit: 50 },
+ });
+ assertNoError(dashQuery);
+ assert.deepEqual(dashQuery.result.structuredContent.scans, []);
+
const findTool = (name: string) => {
const tool = toolList.result.tools.find(
(tool: { name: string }) => tool.name === name,
@@ -2474,9 +2481,13 @@ try {
occurrenceId,
status: "closed",
closeReason: "false_positive",
- note: "The archive path is normalized before the write.",
+ note: "--synthetic triage note",
},
});
+ assert.equal(
+ closedFinding.structuredContent.scan.findings[0].triage.note,
+ "--synthetic triage note",
+ );
assert.equal(
closedFinding.structuredContent.scan.findings[0].triage.status,
"closed",
diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_process.ts b/plugins/codex-security/mcp-app/tests/test_workbench_process.ts
index ba3d18542..0fa009fec 100644
--- a/plugins/codex-security/mcp-app/tests/test_workbench_process.ts
+++ b/plugins/codex-security/mcp-app/tests/test_workbench_process.ts
@@ -281,6 +281,24 @@ try {
["read-artifact", ...artifactArgs],
);
assert.deepEqual(Buffer.from(saved.content, "base64"), binary);
+ await assert.rejects(
+ executeWorkbench(process.env.PYTHON?.trim() || "python3", [
+ "read-artifact",
+ "--artifact-root",
+ artifactRoot,
+ "--artifact-path",
+ "missing.bin",
+ ]),
+ (error: unknown) => {
+ assert.ok(error instanceof Error && "stderr" in error);
+ assert.match(
+ String(error.stderr),
+ /Saved artifact: expected a file inside the scan directory/,
+ );
+ assert.doesNotMatch(String(error.stderr), /Traceback/);
+ return true;
+ },
+ );
const replacementTarget = path.join(root, "target-\ufffd");
await mkdir(replacementTarget);
for (const rawBytePath of [false, true]) {
diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.py
index a10e49efb..37633caa4 100644
--- a/plugins/codex-security/scripts/finalize_scan_contract.py
+++ b/plugins/codex-security/scripts/finalize_scan_contract.py
@@ -470,24 +470,24 @@ def _scan_local_directory_error(message: str, error: OSError) -> ContractError:
return ScanLocalIOError(message)
-def _require_scan_directory(scan_dir: Path) -> Path:
+def _require_scan_directory(scan_dir: Path, context: str = "scan directory") -> Path:
scan_dir = scan_dir.absolute()
try:
metadata = scan_dir.lstat()
except OSError as exc:
raise _scan_local_directory_error(
- "scan directory: expected an existing non-symlink directory", exc
+ f"{context} {scan_dir}: expected an existing non-symlink directory: {exc}", exc
) from exc
if not stat.S_ISDIR(metadata.st_mode):
- raise ContractError("scan directory: expected an existing non-symlink directory")
+ raise ContractError(f"{context}: expected an existing non-symlink directory")
try:
resolved = scan_dir.resolve(strict=True)
except OSError as exc:
raise _scan_local_directory_error(
- "scan directory: expected an existing non-symlink directory", exc
+ f"{context} {scan_dir}: expected an existing non-symlink directory: {exc}", exc
) from exc
if os.path.normcase(resolved) != os.path.normcase(scan_dir):
- raise ContractError("scan directory: expected a canonical non-symlink directory")
+ raise ContractError(f"{context}: expected a canonical non-symlink directory")
return resolved
@@ -796,7 +796,9 @@ def scan_local_file_version(descriptor: int) -> tuple[os.stat_result, int]:
def _scan_local_write_path(
scan_dir: Path, relative_path: str, external_name: bool
) -> tuple[Path, str]:
- scan_dir = _require_scan_directory(scan_dir)
+ scan_dir = _require_scan_directory(
+ scan_dir, "export output directory" if external_name else "scan directory"
+ )
if external_name:
if relative_path in {"", ".", ".."} or "/" in relative_path or "\0" in relative_path:
raise ContractError("external output path: expected a safe file name")
@@ -964,7 +966,9 @@ def _validate_remote(remote: str, context: str) -> None:
parsed = urlsplit(remote)
except ValueError as exc:
# urlsplit rejects authorities with unbalanced brackets.
- raise ContractError(f"{context}: expected a sanitized canonical absolute URL") from exc
+ raise ContractError(
+ f"{context}: expected a sanitized canonical absolute URL: {exc}"
+ ) from exc
if "\\" in remote or not parsed.scheme or not parsed.netloc:
raise ContractError(f"{context}: expected a sanitized canonical absolute URL")
if "@" in parsed.netloc or parsed.query or parsed.fragment:
@@ -3451,8 +3455,11 @@ def main() -> int:
)
else:
finalize_scan(args.scan_dir, args.schema_dir, args.source_root)
- except ContractError as exc:
- parser.error(str(exc))
+ except (ContractError, OSError) as exc:
+ # Keep canonical paths readable and escape control characters at the terminal boundary.
+ parser.error(
+ "".join(char if char.isprintable() else ascii(char)[1:-1] for char in str(exc))
+ )
return 0
diff --git a/plugins/codex-security/scripts/threat_model_projection.py b/plugins/codex-security/scripts/threat_model_projection.py
index 30e06043b..825584e36 100644
--- a/plugins/codex-security/scripts/threat_model_projection.py
+++ b/plugins/codex-security/scripts/threat_model_projection.py
@@ -143,6 +143,8 @@ def main() -> int:
parser.parse_args()
try:
payload = json.load(sys.stdin)
+ if not isinstance(payload["threatModel"], dict):
+ raise ValueError("threatModel must be an object")
sys.stdout.buffer.write(
render_threat_model(payload["threatModel"], payload.get("provenance"))
)
diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.py
index 30a848fe5..88ac47f22 100644
--- a/plugins/codex-security/scripts/workbench_db.py
+++ b/plugins/codex-security/scripts/workbench_db.py
@@ -74,7 +74,6 @@ from workbench_constants import (
FINDING_REMEDIATION_BYTES,
FINDING_SUMMARY_BYTES,
FINDING_TITLE_BYTES,
- FINDINGS_PAGE_MAX,
FINDINGS_RESULT_LIMIT,
PATCH_PREVIEW_BYTES,
SQLITE_RETRY_ATTEMPTS,
@@ -2553,7 +2552,7 @@ def get_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[s
def list_findings(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
scan = require_scan(connection, args.scan_id)
backfill_legacy_finding_details(connection, scan)
- limit = min(args.limit, FINDINGS_PAGE_MAX)
+ limit = args.limit
rows = scan_history.finding_occurrence_rows(
connection,
scan["id"],
@@ -3223,7 +3222,15 @@ def main(*, before_archive: Callable[[], None] | None = None) -> None:
print(json.dumps(saved_results.read_or_save_artifact(args)))
return
if args.command == "finding-issues":
- result = publication.finding_issues(_WORKBENCH_PUBLICATION_CONTEXT, json.load(sys.stdin))
+ try:
+ result = publication.finding_issues(
+ _WORKBENCH_PUBLICATION_CONTEXT, json.load(sys.stdin)
+ )
+ except sqlite3.OperationalError as exc:
+ if not getattr(exc, "_codex_security_state_unavailable", False):
+ raise
+ print(f"sqlite3.OperationalError: {exc}", file=sys.stderr)
+ raise SystemExit(73) from None
print(json.dumps(result, allow_nan=False, sort_keys=True))
return
with closing(
@@ -3363,4 +3370,7 @@ def main(*, before_archive: Callable[[], None] | None = None) -> None:
if __name__ == "__main__":
- main()
+ try:
+ main()
+ except ContractError as exc:
+ raise SystemExit(str(exc)) from None
diff --git a/plugins/codex-security/scripts/workbench_native_indexes.py b/plugins/codex-security/scripts/workbench_native_indexes.py
index 20b1e60d2..98c7a7501 100644
--- a/plugins/codex-security/scripts/workbench_native_indexes.py
+++ b/plugins/codex-security/scripts/workbench_native_indexes.py
@@ -255,7 +255,7 @@ def list_repositories(
)
]
if args.limit is not None or args.offset:
- limit = min(args.limit or FINDINGS_PAGE_MAX, FINDINGS_PAGE_MAX)
+ limit = args.limit or FINDINGS_PAGE_MAX
end = args.offset + limit
pagination = {
"limit": limit,
diff --git a/plugins/codex-security/scripts/workbench_scan_history.py b/plugins/codex-security/scripts/workbench_scan_history.py
index 76f8e3d09..3315ca0d8 100644
--- a/plugins/codex-security/scripts/workbench_scan_history.py
+++ b/plugins/codex-security/scripts/workbench_scan_history.py
@@ -424,7 +424,7 @@ def list_scans(
values.extend((query, query, query, query, query))
where = f"WHERE {' AND '.join(clauses)}" if clauses else ""
paginated = args is not None and (args.limit is not None or args.offset != 0)
- limit = min(args.limit or FINDINGS_PAGE_MAX, FINDINGS_PAGE_MAX) if paginated else None
+ limit = (args.limit or FINDINGS_PAGE_MAX) if paginated else None
pagination = "LIMIT ? OFFSET ?" if paginated else ""
if limit is not None:
values.extend((limit + 1, args.offset))
diff --git a/plugins/codex-security/tests/test_finalize_scan_contract.py b/plugins/codex-security/tests/test_finalize_scan_contract.py
index 1e1fb6083..7b09f24f7 100644
--- a/plugins/codex-security/tests/test_finalize_scan_contract.py
+++ b/plugins/codex-security/tests/test_finalize_scan_contract.py
@@ -74,6 +74,15 @@ class FinalizeScanContractTest(FinalizerSarifCases, ScanFixtureTestCase):
self.assertEqual(findings, original)
return compatible["findings"][0]
+ @skipIf(os.name == "nt", "POSIX control-character path fixture")
+ def test_missing_directory_escapes_terminal_controls(self) -> None:
+ path = self.scan_dir.parent / "missing\x1b[31m\x9b2J"
+ result = self.run_finalizer("--scan-dir", str(path))
+ self.assertNotEqual(result.returncode, 0)
+ self.assertNotIn("\x1b", result.stderr)
+ self.assertNotIn("\x9b", result.stderr)
+ self.assertIn(r"missing\x1b[31m\x9b2J", result.stderr)
+
def test_cli_resolves_noncanonical_scan_directory_arguments(self) -> None:
self.write_scan()
for spelling in (
@@ -969,6 +978,41 @@ class FinalizeScanContractTest(FinalizerSarifCases, ScanFixtureTestCase):
self.assertIn("JSON output path cannot overwrite a scan artifact", rejected.stderr)
self.assertEqual(findings.read_bytes(), before)
+ def test_export_failure_names_the_missing_output_directory(self) -> None:
+ self.write_sealed_scan()
+ output = self.scan_dir.parent / "missing-output" / "findings.csv"
+ result = self.run_finalizer("--export-format", "csv", "--export-output", str(output))
+ self.assertNotEqual(result.returncode, 0)
+ self.assertIn("output directory", result.stderr)
+ self.assertIn(str(output.parent), result.stderr)
+ self.assertNotIn("Traceback", result.stderr)
+
+ def test_unwritable_export_preserves_the_filesystem_diagnostic(self) -> None:
+ self.write_sealed_scan()
+ output = self.scan_dir.parent / "findings.csv"
+ diagnostic = PermissionError(13, "Synthetic permission denied", str(output))
+ stderr = io.StringIO()
+ argv = ["finalizer", "--scan-dir", str(self.scan_dir)]
+ argv += ["--export-format", "csv", "--export-output", str(output)]
+ with (
+ mock.patch.object(sys, "argv", argv),
+ mock.patch.object(FINALIZER, "write_export_output", side_effect=diagnostic),
+ mock.patch.object(sys, "stderr", stderr),
+ self.assertRaises(SystemExit) as raised,
+ ):
+ FINALIZER.main()
+ self.assertEqual(raised.exception.code, 2)
+ self.assertIn(str(diagnostic), stderr.getvalue())
+ self.assertNotIn("Traceback", stderr.getvalue())
+
+ def test_malformed_remote_is_a_contract_error(self) -> None:
+ self.manifest["scan"]["target"]["remote"] = "https://[invalid"
+ self.write_scan()
+ result = self.run_finalizer()
+ self.assertNotEqual(result.returncode, 0)
+ self.assertIn("remote", result.stderr)
+ self.assertNotIn("Traceback", result.stderr)
+
@pytest.mark.native_macos
def test_export_entrypoint_rejects_overwriting_a_sealed_reserved_export(self) -> None:
for artifact_path in (
diff --git a/plugins/codex-security/tests/test_threat_model_projection.py b/plugins/codex-security/tests/test_threat_model_projection.py
index a86845698..e1b19179b 100644
--- a/plugins/codex-security/tests/test_threat_model_projection.py
+++ b/plugins/codex-security/tests/test_threat_model_projection.py
@@ -5,6 +5,8 @@ from __future__ import annotations
import io
import json
import os
+import subprocess
+import sys
import tempfile
import unittest
import unittest.mock
@@ -658,3 +660,19 @@ class ThreatModelProjectionTest(ScanFixtureTestCase):
destination = self.scan_dir / "exports" / "threatmodel.md"
FINALIZER.write_export_output(self.scan_dir, destination, "md", b"model")
self.assertEqual(destination.read_bytes(), b"model")
+
+
+def test_null_threat_model_has_an_input_error_without_traceback() -> None:
+ result = subprocess.run(
+ [
+ sys.executable,
+ str(Path(FINALIZER.__file__).with_name("threat_model_projection.py")),
+ "--input-json-stdin",
+ ],
+ input='{"threatModel": null}',
+ text=True,
+ capture_output=True,
+ )
+ assert result.returncode != 0
+ assert "threatModel" in result.stderr
+ assert "Traceback" not in result.stderr
diff --git a/plugins/codex-security/tests/test_windows_scan_local_files.py b/plugins/codex-security/tests/test_windows_scan_local_files.py
index 1b55b9a60..5a10b3c1d 100644
--- a/plugins/codex-security/tests/test_windows_scan_local_files.py
+++ b/plugins/codex-security/tests/test_windows_scan_local_files.py
@@ -389,9 +389,9 @@ def test_native_directory_change_after_probe_remains_a_path_error(
name = "_canonical_scan_directory" if os.name == "nt" else "_require_scan_directory"
original = getattr(owner, name)
- def replace_after_probe(path):
+ def replace_after_probe(path, *args):
nonlocal mutated
- result = original(path)
+ result = original(path, *args)
if path == scan_dir and not mutated:
mutated = True
target.rename(moved)
diff --git a/plugins/codex-security/tests/test_workbench_db_exports.py b/plugins/codex-security/tests/test_workbench_db_exports.py
index 293f33702..b5c7e7737 100644
--- a/plugins/codex-security/tests/test_workbench_db_exports.py
+++ b/plugins/codex-security/tests/test_workbench_db_exports.py
@@ -860,9 +860,9 @@ def test_completed_findings_are_returned_in_bounded_pages(tmp_path: Path) -> Non
state_dir, "list-findings", scan_id, "--offset", "20", "--limit", "50"
)["findingsPage"]
assert second_page["offset"] == 20
- assert second_page["nextOffset"] == 40
+ assert second_page["nextOffset"] == 70
assert second_page["total"] == 75
- assert len(second_page["findings"]) == 20
+ assert len(second_page["findings"]) == 50
assert embedded_occurrence_ids.isdisjoint(
finding["occurrenceId"] for finding in second_page["findings"]
)
diff --git a/plugins/codex-security/tests/test_workbench_native_index_queries.py b/plugins/codex-security/tests/test_workbench_native_index_queries.py
index 27e1933b6..3b0a4a148 100644
--- a/plugins/codex-security/tests/test_workbench_native_index_queries.py
+++ b/plugins/codex-security/tests/test_workbench_native_index_queries.py
@@ -383,6 +383,22 @@ def test_scan_list_probes_requested_repository_once(
]
[email protected]("collection", ["scans", "repositories", "findings"])
+def test_collection_pages_honor_the_advertised_limit(
+ workbench_api, indexed_collections, collection
+):
+ connection, _targets = indexed_collections
+ arguments = query_args(limit=50, scan_id=SCAN_IDS[0])
+ if collection == "scans":
+ page = workbench_api["scan_history"].list_scans(connection, arguments)
+ elif collection == "repositories":
+ page = workbench_api["native_indexes"].list_repositories(connection, arguments)
+ else:
+ page = workbench_api["list_findings"](connection, arguments)["findingsPage"]
+ assert page["limit"] == 50
+ assert page["nextOffset"] is None
+
+
def test_unscanned_repositories_follow_scanned_rows_and_paginate(
workbench_api, indexed_collections, tmp_path, monkeypatch
):
diff --git a/plugins/codex-security/tests/test_workbench_scan_history.py b/plugins/codex-security/tests/test_workbench_scan_history.py
index c451b658e..cb9433ced 100644
--- a/plugins/codex-security/tests/test_workbench_scan_history.py
+++ b/plugins/codex-security/tests/test_workbench_scan_history.py
@@ -1947,3 +1947,16 @@ def test_cli_diff_launch_accepts_equal_refs_and_distinct_working_tree_base(tmp_p
base_revision,
head,
)
+
+
+def test_scan_list_honors_advertised_page_size(tmp_path: Path) -> None:
+ state_dir = tmp_path / "state"
+ repository = tmp_path / "repository"
+ initialize_git_repository(repository)
+ for index in range(21):
+ output = tmp_path / f"scan-{index}"
+ output.mkdir(mode=0o700)
+ create_cli_scan(state_dir, output, repository, complete=False)
+ page = run_workbench(state_dir, "list-scans", "--limit", "50")
+ assert len(page["scans"]) == 21
+ assert page["nextOffset"] is None
diff --git a/plugins/codex-security/tests/test_workbench_standard_deep_results.py b/plugins/codex-security/tests/test_workbench_standard_deep_results.py
index d96cba887..132895d1d 100644
--- a/plugins/codex-security/tests/test_workbench_standard_deep_results.py
+++ b/plugins/codex-security/tests/test_workbench_standard_deep_results.py
@@ -345,6 +345,7 @@ def test_explicit_recovery_rejects_changed_frozen_source(tmp_path: Path) -> None
assert rejected["returncode"] != 0
assert "Frozen stopped-scan checkpoint set is incomplete" in str(rejected["stderr"])
+ assert "Traceback" not in str(rejected["stderr"])
assert (scan_dir / "scan-manifest.json").read_bytes() == original_manifest
assert (scan_dir / "findings.json").read_bytes() == original_findings
with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.md
index f60adc334..4127f1963 100644
--- a/sdk/typescript/docs/cli.md
+++ b/sdk/typescript/docs/cli.md
@@ -533,7 +533,9 @@ or JSONL output produce:
{ "status": "failed", "code": "SCAN_FAILED", "message": "..." }
```
-With `--full-output`, the error appears under `error` in an `ok: false` envelope.
+With `--full-output`, JSON and JSONL failures put the error under `error` in an
+`ok: false` envelope. YAML and TOON use the same failed envelope when retained
+sealed scan results are available (including TOON selected by default).
If the scan target changed, the error explains that the results no longer
represent the current checkout. Retained sealed results remain available under
`data`, including their manifest, findings, review summary, scan directory, and report
@@ -545,8 +547,8 @@ selecting an unavailable field can return `null` or omit `data`.
Saved-scan setup failures use the same output shape with
`SCAN_REPLAY_UNAVAILABLE` for `scans rerun` (including when no completed scan is
available) or `SCAN_RESUME_UNAVAILABLE` for `scans resume`. Rerunning an imported
-scan uses `SCAN_IMPORT_FAILED` if the import fails. Other output formats retain
-stderr-only failures, including when `--full-output` is selected.
+scan uses `SCAN_IMPORT_FAILED` if the import fails. YAML and TOON failures remain
+on stderr without `--full-output`, or when retained results are unavailable.
Diagnostics stay on stderr. Each command's `--schema --format json` describes
its successful output and failure codes.
@@ -2839,7 +2841,9 @@ retain exit codes `130` and `143` without a result.
Use `--llms` for the command manifest, `scan --schema --format json` for its
schema, and `completions bash|zsh|fish` for shell completions. Scan output supports
-`--format toon|json|yaml|jsonl` and `--full-output`.
+`--format toon|json|yaml|jsonl` and `--full-output`. The existing `--format`,
+`--filter-output`, `--token-limit`, and `--token-offset` options accept either
+`--option value` or `--option=value`.
`skills add` syncs generated CLI command guidance; it does not install the full
Codex Security plugin. `mcp add` registers the CLI as an MCP server, exposing
diff --git a/sdk/typescript/scripts/fixtures/package-deep-codex.mjs b/sdk/typescript/scripts/fixtures/package-deep-codex.mjs
index 7b6b55a04..3c752383a 100644
--- a/sdk/typescript/scripts/fixtures/package-deep-codex.mjs
+++ b/sdk/typescript/scripts/fixtures/package-deep-codex.mjs
@@ -74,6 +74,7 @@ async function run() {
},
},
})}\n`,
+ { mode: 0o600 },
);
result = {};
break;
diff --git a/sdk/typescript/scripts/fixtures/package-deep-scan.mjs b/sdk/typescript/scripts/fixtures/package-deep-scan.mjs
index 99aa159da..310e06c51 100644
--- a/sdk/typescript/scripts/fixtures/package-deep-scan.mjs
+++ b/sdk/typescript/scripts/fixtures/package-deep-scan.mjs
@@ -396,6 +396,7 @@ async function runInstalledSdk(pluginRoot, executable) {
await readFile(join(pluginRoot, ".codex-plugin", "plugin.json"), "utf8"),
);
const owner = "package-sdk-owner";
+ f.env.PACKAGE_DEEP_PARENT_THREAD = owner;
let scanId;
const client = new sdk.CodexSecurity(
{ pythonPath: f.env.PYTHON },
@@ -420,7 +421,7 @@ async function runInstalledSdk(pluginRoot, executable) {
return {
startThread() {
return {
- id: owner,
+ id: null,
async runStreamed() {
assert.fail(
"The direct engine must not start a parent model turn.",
@@ -435,15 +436,15 @@ async function runInstalledSdk(pluginRoot, executable) {
try {
const result = await client.run(f.target, {
mode: "deep",
+ onScanRegistered: (scan) => {
+ scanId = scan.scanId;
+ },
auth: "api-key",
workers: 1,
subagents: 0,
maxDiscoveryRuns: 2,
stopAfterNoNew: 2,
outputDir: join(f.directory, "output"),
- onScanRegistered(scan) {
- scanId = scan.scanId;
- },
});
assert.equal(result.threadId, owner);
assert.equal(result.manifest.scan.status, "completed");
diff --git a/sdk/typescript/src/auth.ts b/sdk/typescript/src/auth.ts
index b81de4028..5fa15597a 100644
--- a/sdk/typescript/src/auth.ts
+++ b/sdk/typescript/src/auth.ts
@@ -64,9 +64,10 @@ export async function readCodexHomeConfig(
environment: ProcessEnvironment,
signal?: AbortSignal,
): Promise<JsonObject> {
+ const path = join(configuredCodexHome(environment), "config.toml");
try {
return parse(
- await readFile(join(configuredCodexHome(environment), "config.toml"), {
+ await readFile(path, {
encoding: "utf8",
signal,
}),
@@ -75,7 +76,7 @@ export async function readCodexHomeConfig(
signal?.throwIfAborted();
if ((error as NodeJS.ErrnoException).code === "ENOENT") return {};
throw new CodexSecurityError(
- `Could not read the configured Codex provider: ${errorMessage(error)}`,
+ `Could not read the configured Codex provider from ${path}: ${errorMessage(error)}`,
{ cause: error },
);
}
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index b3d239c88..782692c17 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -63,7 +63,7 @@ import {
promisify,
stripVTControlCharacters,
} from "node:util";
-import { Cli, z } from "incur";
+import { Cli, Formatter, z } from "incur";
import stringWidth from "string-width";
import Ajv2020, { type ValidateFunction } from "ajv/dist/2020.js";
import { formatCliHelp } from "./cli-help.js";
@@ -1181,6 +1181,7 @@ interface ScanArguments extends ResolvedScanSettings {
interface ScanOutcome {
exitCode: number;
+ patchReviewCanceled?: true;
data?: Record<string, unknown>;
error?: string;
}
@@ -2295,9 +2296,22 @@ export async function main(
let renderedOutput: string | undefined;
let structuredError = false;
let failedResultOutput:
- { format: "json" | "jsonl"; code?: string; message: string } | undefined;
+ | {
+ format: "json" | "jsonl" | "toon" | "yaml";
+ code?: string;
+ message: string;
+ }
+ | undefined;
let filteredScanFailure:
{ format: string; data: Record<string, unknown> } | undefined;
+ const includeFailureResults = (
+ format: string,
+ ): format is "json" | "jsonl" | "toon" | "yaml" =>
+ argv.includes("--full-output") &&
+ (format === "json" ||
+ format === "jsonl" ||
+ format === "toon" ||
+ format === "yaml");
const finishScan = (
outcome: ScanOutcome,
format: string,
@@ -2310,11 +2324,7 @@ export async function main(
): Record<string, unknown> | undefined => {
exitCode = outcome.exitCode;
if (outcome.error === undefined || outcome.data !== undefined) {
- if (
- outcome.error !== undefined &&
- (format === "json" || format === "jsonl") &&
- argv.includes("--full-output")
- ) {
+ if (outcome.error !== undefined && includeFailureResults(format)) {
failedResultOutput = { format, message: outcome.error };
}
return outcome.data;
@@ -2332,6 +2342,22 @@ export async function main(
}
return incurError({ code, message: outcome.error, exitCode });
};
+ const finishScanCommand: typeof finishScan = (
+ outcome,
+ format,
+ incurError,
+ code,
+ ) => {
+ if (
+ (outcome.error === undefined || outcome.patchReviewCanceled) &&
+ format === "toon" &&
+ !argv.some((argument) => OUTPUT_OPTION.test(argument))
+ ) {
+ exitCode = outcome.exitCode;
+ return;
+ }
+ return finishScan(outcome, format, incurError, code);
+ };
const runImport = (options: ImportScanOptions) =>
withTerminalErrorsHandled(errorOutput, () =>
runScanImport(options, errorOutput, dependencies),
@@ -2854,7 +2880,7 @@ export async function main(
})
.command("resume", {
description: "Resume an interrupted Deep Scan in its original session.",
- hint: "Execution failures in full-output JSON or JSONL use ok: false and keep available scan results under data.",
+ hint: "Execution failures keep available scan results under data in failed full-output JSON, JSONL, YAML, or TOON envelopes.",
mcp: false,
args: z.object({
scanId: z.string().min(1).describe("Interrupted Deep Scan identifier."),
@@ -2868,6 +2894,13 @@ export async function main(
}),
output: scanOutputSchema("SCAN_FAILED", "SCAN_RESUME_UNAVAILABLE"),
async run({ args, error: incurError, format, options }) {
+ if (format === "md") {
+ errorOutput.write(
+ "codex-security: Markdown output is not supported for scan results.\n",
+ );
+ exitCode = 2;
+ return;
+ }
let scanArguments: ScanArguments;
try {
const saved = await dependencies.runWorkbench(
@@ -2912,24 +2945,27 @@ export async function main(
} catch (error) {
const message = errorMessage(error);
errorOutput.write(`codex-security: ${diagnosticLines(message)}\n`);
- return finishScan(
+ return finishScanCommand(
{ exitCode: 2, error: message },
format,
incurError,
"SCAN_RESUME_UNAVAILABLE",
);
}
- scanArguments.includeFailureResults =
- argv.includes("--full-output") &&
- (format === "json" || format === "jsonl");
- const outcome = await runScan(scanArguments, errorOutput, dependencies);
- return finishScan(outcome, format, incurError);
+ scanArguments.includeFailureResults = includeFailureResults(format);
+ const outcome = await runScan(
+ scanArguments,
+ errorOutput,
+ dependencies,
+ format !== "json" && format !== "jsonl",
+ );
+ return finishScanCommand(outcome, format, incurError);
},
})
.command("rerun", {
description:
"Start a new scan using saved settings and current checkout/context files.",
- hint: "Execution failures in full-output JSON or JSONL use ok: false and keep available scan results under data.",
+ hint: "Execution failures keep available scan results under data in failed full-output JSON, JSONL, YAML, or TOON envelopes.",
destructive: true,
mcp: false,
args: z.object({
@@ -2977,7 +3013,7 @@ export async function main(
const message = errorMessage(error);
if (exitCode === 0)
errorOutput.write(`codex-security: ${diagnosticLines(message)}\n`);
- return finishScan(
+ return finishScanCommand(
{ exitCode: 2, error: message },
format,
incurError,
@@ -3025,7 +3061,7 @@ export async function main(
format: importFormat,
parentScanId,
});
- return finishScan(
+ return finishScanCommand(
outcome,
format,
incurError,
@@ -3063,23 +3099,21 @@ export async function main(
} catch (error) {
const message = errorMessage(error);
errorOutput.write(`codex-security: ${diagnosticLines(message)}\n`);
- return finishScan(
+ return finishScanCommand(
{ exitCode: 2, error: message },
format,
incurError,
"SCAN_REPLAY_UNAVAILABLE",
);
}
- scanArguments.includeFailureResults =
- argv.includes("--full-output") &&
- (format === "json" || format === "jsonl");
+ scanArguments.includeFailureResults = includeFailureResults(format);
const outcome = await runScan(
scanArguments,
errorOutput,
dependencies,
format !== "json" && format !== "jsonl",
);
- return finishScan(outcome, format, incurError);
+ return finishScanCommand(outcome, format, incurError);
},
})
.command("match", {
@@ -5240,9 +5274,7 @@ export async function main(
headless: options.headless,
dryRun: options.dryRun,
mock: options.mock,
- includeFailureResults:
- argv.includes("--full-output") &&
- (format === "json" || format === "jsonl"),
+ includeFailureResults: includeFailureResults(format),
},
errorOutput,
dependencies,
@@ -5254,16 +5286,9 @@ export async function main(
outcome = { exitCode: 2, error: message };
}
- if (
- outcome.error === undefined &&
- !options.dryRun &&
- format === "toon" &&
- !argv.some((argument) => OUTPUT_OPTION.test(argument))
- ) {
- exitCode = outcome.exitCode;
- return;
- }
- return finishScan(outcome, format, incurError);
+ return options.dryRun
+ ? finishScan(outcome, format, incurError)
+ : finishScanCommand(outcome, format, incurError);
},
})
.command("install-hook", {
@@ -7861,7 +7886,27 @@ export async function main(
streamedLogs,
frameworkOutput ? JSON.parse(frameworkOutput).cta : undefined,
);
- if (failedResultOutput !== undefined) {
+ if (
+ failedResultOutput?.format === "toon" ||
+ failedResultOutput?.format === "yaml"
+ ) {
+ const success = Formatter.format({ ok: true }, failedResultOutput.format);
+ const prefix = success.endsWith("\n") ? success : `${success}\n`;
+ // Keep Incur's rendered data, metadata, and output controls intact.
+ if (frameworkOutput.startsWith(prefix)) {
+ const failure = Formatter.format(
+ {
+ ok: false,
+ error: {
+ code: failedResultOutput.code ?? "SCAN_FAILED",
+ message: failedResultOutput.message,
+ },
+ },
+ failedResultOutput.format,
+ );
+ frameworkOutput = `${failure}${failure.endsWith("\n") ? "" : "\n"}${frameworkOutput.slice(prefix.length)}`;
+ }
+ } else if (failedResultOutput !== undefined) {
const envelope: JsonValue = JSON.parse(frameworkOutput);
// Token-count output is a number, not a full-output envelope.
if (isJsonObject(envelope) && envelope["ok"] === true) {
@@ -9973,14 +10018,78 @@ export function skillCommandFailure(
detail: string,
authentication: ScanAuthentication | null = null,
): string {
- if (
- /401|invalid.api.key|token.expired|unauthori[sz]ed|authorizationrequired/iu.test(
+ // Paths may contain provider error codes as filenames. Keep the original for display.
+ const containsParserError =
+ /\b(?:syntax\s*error|pars(?:e|er|ing)|unexpected token|at (?:line|column) \d+)\b/iu.test(
detail,
+ );
+ // Keep a neutral token so a quoted provider/model remains a named model in the message.
+ const withoutPaths = detail
+ .replace(/\bhttps?:\/\/[^\s"'`<>]+/giu, "")
+ .replace(/[^\s"'`()<>,]+/gu, (token) =>
+ /[/\\]/u.test(token) && !/^HTTP\/\d+(?:\.\d+)?$/iu.test(token)
+ ? "[path]"
+ : token,
+ );
+ const diagnosticDetail = containsParserError
+ ? withoutPaths.replace(/\binvalid[ _-]token\b/giu, "")
+ : withoutPaths;
+ const classificationDetail = diagnosticDetail.replace(
+ /(?<![\w./\\-])(?:http(?:\/\d+(?:\.\d+)?)?(?:[ \t]*(?:response(?:[ \t]+|[_-])?code|error(?:[ \t]+code)?|status(?:[ _-]?code)?|code))?|response(?:[ \t]+|[_-])?(?:code|status)|(?:(?:http\.)?response\.)?status(?:[ _-]?code)?)(?:["']?[ \t]*[:=][ \t]*|[ \t]+)["']?(401|403|429)(?![\w/\\-]|\.(?=\S))|\b(?:401|403|429)\b/giu,
+ (match, status: string | undefined) => (status ? match : ""),
+ );
+ const classification = classifyConnectionFailure(classificationDetail);
+ let advice: string | undefined;
+ if (
+ classification === "unauthorized" ||
+ /\b(?:authorizationrequired|invalid[._ -]api[._ -]key|token[._ -]expired)\b/iu.test(
+ classificationDetail,
+ )
+ ) {
+ advice = authenticationFailureMessage(authentication);
+ } else if (
+ /\bpermissions? to use (?:this |the )?model\b/iu.test(
+ classificationDetail,
+ ) ||
+ (!/\b(?:sqlite3?|database|workbench)\b/iu.test(classificationDetail) &&
+ /\bmodel[ \t]+(?:'[^'\r\n]+'|"[^"\r\n]+"|`[^`\r\n]+`)[ \t]+not[ \t]+found\b/iu.test(
+ classificationDetail,
+ )) ||
+ /\b(?:model[._ -]not[._ -]found|model[._ -]?access[._ -]?denied|access[._ -]?(?:denied[._ -]?to[._ -]?model|to[._ -]?model[._ -]?denied))\b/iu.test(
+ classificationDetail,
+ ) ||
+ (classification === "forbidden" &&
+ /\b403\b|\bforbidden\b|\bmodel[ _-]?(?:not[ _-]?found|access)\b|\baccess.*model\b/iu.test(
+ classificationDetail,
+ ))
+ ) {
+ advice = "The selected model is unavailable for the current credentials.";
+ } else if (
+ classification === "rate_limited" ||
+ /\b(?:rate[._ -]limit(?:ed|[._ -]exceeded)?|tokens[._ -]per[._ -]minute)\b/iu.test(
+ classificationDetail,
)
) {
- return `${detail}\n${authenticationFailureMessage(authentication)}`;
+ advice = "The request was rate limited. Wait and retry.";
+ } else if (
+ /\b(?:models?[ _.-]cache|supports_reasoning_summaries)\b/iu.test(
+ classificationDetail,
+ )
+ ) {
+ advice =
+ "Codex could not load its model metadata. Update Codex or refresh its model cache.";
+ } else if (
+ classification === "network_error" ||
+ classification === "timeout" ||
+ (!/\b(?:sqlite3?|database|workbench)\b/iu.test(classificationDetail) &&
+ /\b(?:ECONNABORTED|timed[._ -]out|network[._ -]?error|(?:(?:network|request)[._ -]?)?timeout(?:[._ -]?error)?)\b/iu.test(
+ classificationDetail,
+ ))
+ ) {
+ advice =
+ "Codex could not connect to the model service. Check the network and retry.";
}
- return detail || `${command} failed with exit code ${status}.`;
+ return `${command} failed with exit code ${status}.${advice ? ` ${advice}` : ""}${detail ? `\n${detail}` : ""}`;
}
async function incurErrorMessage(output: string): Promise<string> {
@@ -10984,6 +11093,8 @@ async function executeScan(
}
}
} catch (error) {
+ if (error instanceof Error && error.name === "ExitPromptError")
+ requestedSignal ??= "SIGINT";
failed = true;
failure = error;
} finally {
@@ -11229,34 +11340,43 @@ async function executeScan(
? (arguments_.patchSeverity ?? "low")
: undefined;
let patchSelection: PatchSelection | null = null;
- if (
- !arguments_.mock &&
- actionableFindings.length > 0 &&
- arguments_.patchSeverity === undefined &&
- progress?.interactive === true &&
- (dependencies.patchEditor !== undefined || process.stdin.isTTY === true)
- ) {
- const confirmed =
- arguments_.patch ||
- (await (
- dependencies.confirmPatchReview ??
- createTerminalPrompt(errorOutput).confirm
- )("Open findings for review?"));
- if (confirmed) {
- const selectPatches =
- dependencies.patchEditor ??
- (async (target: string, candidates: readonly Finding[]) => {
- const { runPatchTui } = await import("./patch-tui.js");
- return runPatchTui(target, candidates, {
- stdout: errorOutput as NodeJS.WriteStream,
- color:
- dependencies.environment["NO_COLOR"] === undefined &&
- dependencies.environment["TERM"] !== "dumb",
+ try {
+ if (
+ !arguments_.mock &&
+ actionableFindings.length > 0 &&
+ arguments_.patchSeverity === undefined &&
+ progress?.interactive === true &&
+ (dependencies.patchEditor !== undefined || process.stdin.isTTY === true)
+ ) {
+ const confirmed =
+ arguments_.patch ||
+ (await (
+ dependencies.confirmPatchReview ??
+ createTerminalPrompt(errorOutput).confirm
+ )("Open findings for review?"));
+ if (confirmed) {
+ const selectPatches =
+ dependencies.patchEditor ??
+ (async (target: string, candidates: readonly Finding[]) => {
+ const { runPatchTui } = await import("./patch-tui.js");
+ return runPatchTui(target, candidates, {
+ stdout: errorOutput as NodeJS.WriteStream,
+ color:
+ dependencies.environment["NO_COLOR"] === undefined &&
+ dependencies.environment["TERM"] !== "dumb",
+ });
});
- });
- patchSelection = await selectPatches(repository, actionableFindings);
- patchThreshold = patchSelection?.severity;
+ patchSelection = await selectPatches(repository, actionableFindings);
+ patchThreshold = patchSelection?.severity;
+ }
}
+ } catch (error) {
+ if (!(error instanceof Error && error.name === "ExitPromptError"))
+ throw error;
+ const message =
+ "Patch review canceled by Ctrl-C. Saved scan results were kept.";
+ errorOutput.write(`codex-security: ${message}\n`);
+ return { ...finishResult(130, message), patchReviewCanceled: true };
}
let patches: FindingPatch[] = [];
@@ -11363,8 +11483,12 @@ const LOCAL_SYSCALL_CODES = new Set([
"EXDEV",
]);
-function isLocalScanFailure(error: unknown): boolean {
- if (error instanceof IncompleteScanError) error = error.cause;
+function isLocalScanFailure(
+ error: unknown,
+ seen = new Set<unknown>(),
+): boolean {
+ if (seen.has(error)) return false;
+ seen.add(error);
if (
error instanceof InvalidTargetError ||
error instanceof OutputDirectoryError ||
@@ -11376,11 +11500,12 @@ function isLocalScanFailure(error: unknown): boolean {
return true;
}
return (
- typeof error === "object" &&
- error !== null &&
- "code" in error &&
- typeof (error as { code: unknown }).code === "string" &&
- LOCAL_SYSCALL_CODES.has((error as { code: string }).code)
+ (typeof error === "object" &&
+ error !== null &&
+ "code" in error &&
+ typeof (error as { code: unknown }).code === "string" &&
+ LOCAL_SYSCALL_CODES.has((error as { code: string }).code)) ||
+ (error instanceof Error && isLocalScanFailure(error.cause, seen))
);
}
@@ -11511,6 +11636,11 @@ function scanFailureMessage(
if (authentication?.method === "command") {
return `${detail}The configured Codex provider denied access. Check the command credentials and provider permissions.`;
}
+ if (
+ authentication?.method === "stored_credentials" &&
+ authentication.credentialType === "api_key"
+ )
+ return `${detail}The stored API key cannot access the configured model. Use an API key with model access.`;
return authentication?.method === "api_key"
? `${detail}The API key from ${authentication.source} cannot access the configured model. ` +
"Retry with '--auth chatgpt' or use an API key with model access."
diff --git a/sdk/typescript/src/cloud-publish.ts b/sdk/typescript/src/cloud-publish.ts
index ea329b2f9..fe2e69de5 100644
--- a/sdk/typescript/src/cloud-publish.ts
+++ b/sdk/typescript/src/cloud-publish.ts
@@ -196,11 +196,12 @@ async function publishCloudPayload(
redirect: "error",
signal,
});
- } catch {
+ } catch (error) {
dependencies.signal?.throwIfAborted();
// A lost response does not establish whether the server accepted the POST.
throw new CodexSecurityError(
- "Cloud publication was not confirmed. The request was not retried; check whether it was accepted before submitting again.",
+ `Cloud publication was not confirmed: ${errorMessage(error)}. The request was not retried; check whether it was accepted before submitting again.`,
+ { cause: error },
);
}
if (!response.ok) {
diff --git a/sdk/typescript/src/component-scan.ts b/sdk/typescript/src/component-scan.ts
index 49d8f405d..ede16e84c 100644
--- a/sdk/typescript/src/component-scan.ts
+++ b/sdk/typescript/src/component-scan.ts
@@ -152,7 +152,7 @@ export async function runComponentScans(
if (!Number.isSafeInteger(workers) || workers < 1)
throw new Error("Component workers must be a positive integer.");
if (Boolean(options.auto) === (options.components !== undefined))
- throw new Error("Choose components or automatic planning, not both.");
+ throw new Error("Choose exactly one of components or automatic planning.");
const auth = options.scanOptions?.auth;
let environment = options.environment;
if (auth !== undefined && auth !== "auto") {
diff --git a/sdk/typescript/src/deduplication/refusal.ts b/sdk/typescript/src/deduplication/refusal.ts
index f1560a343..fe27fd1f4 100644
--- a/sdk/typescript/src/deduplication/refusal.ts
+++ b/sdk/typescript/src/deduplication/refusal.ts
@@ -8,13 +8,40 @@ export function isReviewRefusal(
codexErrorInfo === "misalignmentPolicyViolation"
)
return true;
+ // Structured transport failures take precedence over fallback refusal wording.
+ if (
+ codexErrorInfo !== null &&
+ typeof codexErrorInfo === "object" &&
+ [
+ "httpConnectionFailed",
+ "responseStreamConnectionFailed",
+ "responseStreamDisconnected",
+ "responseTooManyFailedAttempts",
+ ].some((variant) => Object.hasOwn(codexErrorInfo, variant))
+ )
+ return false;
+ const text = message.trim();
+ const policyRefusal =
+ /\b(?:cybersecurity|cyber|content|safety)[ _-]*policy\s*:\s*(?:(?:this|the|your)\s+)?(?:request|review)\s+(?:(?:is|was|has been)\s+)?(?:refused|blocked)\b([\s\S]*)$/iu.exec(
+ text,
+ );
+ if (policyRefusal) {
+ // The explicit refusal predicate decides; its explanation has no controlled vocabulary.
+ // A leading service-outage cause still describes an operational failure.
+ return !/^\s*(?:(?:(?:because|as|due to)\s+|[.!:]\s*)(?:the\s+)?(?:upstream\s+)?service\s+(?:is\s+)?(?:unavailable|unavailability|timed? out|timeout)\b|by\s+upstream\s+with\s+[45]\d{2}\b|(?:because|as)\s+(?:the\s+)?upstream(?:\s+service)?\s+returned\s+(?:HTTP\s+)?[45]\d{2}\b)/iu.test(
+ policyRefusal[1] ?? "",
+ );
+ }
return [
/\bflagged for possible cybersecurity risk\b/iu,
/\bflagged for potentially high-risk cyber activity\b/iu,
/\bcyber[_\s-]?policy\b/iu,
- /\b(?:cybersecurity|cyber|content|safety)[ _-]*policy[ _-]*(?:violation|refusal|refused)\b/iu,
- /\b(?:refusal|refused)\b[^\n]*\b(?:cybersecurity|cyber|safety policy)\b/iu,
- /\b(?:cybersecurity|cyber|safety policy)\b[^\n]*\b(?:refusal|refused)\b/iu,
- /^(?:I(?:['’]m| am) sorry[,.:]?\s*(?:but\s+)?|Sorry[,.:]?\s*)?I(?:\s+(?:cannot|can['’]t|won['’]t|am unable to)|['’]m unable to)\s+(?:help|assist|comply)\b/iu,
- ].some((pattern) => pattern.test(message.trim()));
+ /\b(?:cybersecurity|cyber)[ _-]*policy[ _-]*(?:violation|refusal|refused)\b/iu,
+ /(?:^(?:refusal|refused)|\b(?:request|review)\s+(?:refusal|(?:(?:is|was|has been)\s+)?(?:refused|blocked)))\s+(?:under|by|due to|because of|because (?:it|this request) violates|for violating|in accordance with)\s+(?:(?:the|a)\s+)?(?:cybersecurity|cyber|content|safety)[ _-]*policy\b/iu,
+ /^(?:(?:this|the|your)\s+)?(?:request|review)(?:\s+(?:is|was|has been))?\s+(?:refusal|refused|blocked)\s*:\s*(?:(?:the|a)\s+)?(?:cybersecurity|cyber|content|safety)[ _-]*policy(?:[.!](?:\s+(?:I can offer|Please ask)\b[^\n]*)?)?$/iu,
+ /^(?:cybersecurity|cyber|content|safety)[ _-]*policy\s*:\s*(?:(?:(?:this|the|your)\s+)?request\s+)?(?:(?:is|was|has been)\s+)?(?:violation|refusal|refused|blocked)[.!]?$/iu,
+ /\b(?:cybersecurity|cyber|content|safety)[ _-]*policy\s+requires\s+(?:a\s+)?refusal\s+of\s+(?:(?:this|the|your)\s+)?(?:request|review)\b/iu,
+ /\bunder\s+(?:(?:the|a)\s+)?(?:cybersecurity|cyber|content|safety)[ _-]*policy[, :]\s*(?:this|the|your) request (?:is|was|has been) (?:refused|blocked)\b/iu,
+ /^(?:I(?:['’]m| am) sorry[,.:]?\s*(?:but\s+)?|Sorry[,.:]?\s*)?I(?:\s+(?:cannot|can['’]t|won['’]t|am (?:unable|not able) to)|['’]m (?:unable|not able) to)\s+(?:help|assist|comply)\b/iu,
+ ].some((pattern) => pattern.test(text));
}
diff --git a/sdk/typescript/src/multiscan.ts b/sdk/typescript/src/multiscan.ts
index 7fffcc375..6f0f01b91 100644
--- a/sdk/typescript/src/multiscan.ts
+++ b/sdk/typescript/src/multiscan.ts
@@ -640,7 +640,7 @@ async function runCampaign(
}
requiresRecovery = error instanceof OutputDirectoryNotEmptyError;
failure = requiresRecovery
- ? `Bulk attempt directory is not empty: ${scanDir}. Existing artifacts and checkout were preserved. Run the same bulk-scan command with --recover to recover interrupted scans or retry failed scans in new attempt directories.`
+ ? `Bulk attempt directory is not empty: ${scanDir}. Existing artifacts were preserved. Run the same bulk-scan command with --recover to recover interrupted scans or retry failed scans in new attempt directories.`
: errorMessage(error);
} finally {
if (options.recoverScan === undefined && checkout !== undefined) {
@@ -1185,7 +1185,7 @@ function parseInventory(
if (errors.length > 0) {
throw new Error(`Multiscan CSV could not be parsed: ${errors[0]!.message}`);
}
- const headers = rows.shift();
+ const headers = rows.shift()?.map((header) => header.trim());
if (
headers === undefined ||
!["id", "repository", "revision"].every((name) => headers.includes(name)) ||
diff --git a/sdk/typescript/src/publish.ts b/sdk/typescript/src/publish.ts
index 9bd05f2e6..9ffce19f2 100644
--- a/sdk/typescript/src/publish.ts
+++ b/sdk/typescript/src/publish.ts
@@ -743,7 +743,7 @@ export async function checkScanPublicationInternal(
options.signal?.throwIfAborted();
if (error instanceof ConfigurationError) throw error;
throw new CodexSecurityError(
- `Could not verify Linear ${step}. Check the API key and publication destination.`,
+ `Could not verify Linear ${step}: ${errorMessage(error)}. Check the API key and publication destination.`,
{ cause: error },
);
}
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index 812e95985..2f5f2071a 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -3411,6 +3411,11 @@ export async function probeCodexSandbox(
`Codex exited with status ${result.exitCode}.`;
} catch (error) {
signal?.throwIfAborted();
+ if (timeout.aborted)
+ throw new SandboxUnavailableError(
+ `Codex sandbox probe timed out after ${SANDBOX_PROBE_TIMEOUT_MILLISECONDS / 1000} seconds.`,
+ { cause: error },
+ );
detail = processErrorDetail(error);
}
const guidance =
diff --git a/sdk/typescript/tests-ts/api-events.test.ts b/sdk/typescript/tests-ts/api-events.test.ts
index 827f3f05c..7a79ad7b9 100644
--- a/sdk/typescript/tests-ts/api-events.test.ts
+++ b/sdk/typescript/tests-ts/api-events.test.ts
@@ -947,6 +947,16 @@ describe("one-shot scan events", () => {
});
});
+test("preserves the reason a required scan artifact cannot be read", async () => {
+ const scanDir = await copyCompletedScan(await temporaryDirectory());
+ const artifact = join(scanDir, "findings.json");
+ await rm(artifact);
+ await mkdir(artifact);
+ await expect(runEvents(scanDir, completedEvents())).rejects.toThrow(
+ /findings\.json:.*Expected a regular non-(?:symlink|reparse) scan file/u,
+ );
+});
+
test.each(["scan-manifest.json", "findings.json", "report.md"])(
"missing required artifact %s remains an incomplete scan",
async (name) => {
diff --git a/sdk/typescript/tests-ts/auth.test.ts b/sdk/typescript/tests-ts/auth.test.ts
index 9860c9022..4c4b6e21c 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 { chmod, writeFile } from "node:fs/promises";
+import { chmod, mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { setTimeout as delay } from "node:timers/promises";
import { pathToFileURL } from "node:url";
@@ -566,3 +566,20 @@ setInterval(() => {}, 1000);
expect(observeSucceeded).not.toHaveBeenCalled();
});
});
+
+test("retains the Codex configuration path and parsing error", async () => {
+ const root = join(
+ await temporaryDirectory(),
+ "configuration-\u009b-sk-proj-SYNTHETIC_SECRET",
+ );
+ await mkdir(root);
+ const path = join(root, "config.toml");
+ await writeFile(path, "invalid = [");
+ try {
+ await readCodexHomeConfig({ CODEX_HOME: root });
+ throw new Error("Expected invalid configuration to fail");
+ } catch (error) {
+ expect(String(error)).toContain(path);
+ expect(String(error)).toContain("Invalid TOML");
+ }
+});
diff --git a/sdk/typescript/tests-ts/candidate-normalizer.test.ts b/sdk/typescript/tests-ts/candidate-normalizer.test.ts
index 4c683c279..6b7da931a 100644
--- a/sdk/typescript/tests-ts/candidate-normalizer.test.ts
+++ b/sdk/typescript/tests-ts/candidate-normalizer.test.ts
@@ -97,6 +97,33 @@ function ledger(f: Fixture): Row[] {
afterEach(roots.cleanup);
describe("built candidate normalizer", () => {
+ test.skipIf(process.platform === "win32")(
+ "retains the output error when temporary-file cleanup also fails",
+ () => {
+ const f = fixture();
+ const preload = join(f.root, "fail-filesystem.cjs");
+ write(
+ preload,
+ `
+const fs = require("node:fs");
+fs.renameSync = () => { throw new Error("Synthetic output rename failed"); };
+fs.unlinkSync = () => { throw new Error("Synthetic temporary unlink failed" + String.fromCharCode(27, 155)); };
+require("node:module").syncBuiltinESMExports();
+`,
+ );
+ const result = run(f, [[candidate()]], [], {
+ ...process.env,
+ NODE_OPTIONS: `--require ${JSON.stringify(preload)}`,
+ });
+ expect(result.status).toBe(2);
+ expect(result.stderr).toContain("Synthetic output rename failed");
+ expect(result.stderr).toContain("Synthetic temporary unlink failed");
+ expect(result.stderr).not.toContain("\u001b");
+ expect(result.stderr).not.toContain("\u009b");
+ expect(existsSync(f.output)).toBe(false);
+ },
+ );
+
test.skipIf(process.platform !== "linux")(
"accepts many input paths through the packaged launcher",
() => {
diff --git a/sdk/typescript/tests-ts/cli-diagnostics.test.ts b/sdk/typescript/tests-ts/cli-diagnostics.test.ts
index 8c305e83f..e2416bb8f 100644
--- a/sdk/typescript/tests-ts/cli-diagnostics.test.ts
+++ b/sdk/typescript/tests-ts/cli-diagnostics.test.ts
@@ -1,6 +1,12 @@
import { stripVTControlCharacters } from "node:util";
+import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
+import { join } from "node:path";
import { describe, expect, test } from "bun:test";
+import { Formatter } from "incur";
+import { parse as parseYaml } from "yaml";
import { main } from "../src/cli.js";
+import { readCodexHomeConfig } from "../src/auth.js";
+import { matchScanFindings } from "../src/scan-comparison.js";
import { CodexSecurityError, OutputDirectoryError } from "../src/errors.js";
import {
warningResult,
@@ -8,11 +14,239 @@ import {
fakeResult,
fakeSecurity,
} from "./cli-fixtures.js";
+import { git } from "./git-fixture.js";
import { throwing } from "./support/errors.js";
import { createCliTest } from "./support/cli-run.js";
+import { temporaryDirectory } from "./support/temporary-directories.js";
+import { runCommand } from "./support/shell.js";
describe("CLI diagnostics", () => {
+ test.skipIf(process.platform === "win32")(
+ "escapes terminal controls from public export errors",
+ async () => {
+ const directory = await temporaryDirectory("codex-security-export-cli-");
+ try {
+ const home = join(directory, "home");
+ await mkdir(home, { mode: 0o700 });
+ const scan = join(
+ directory,
+ "missing-\u001b[2J\u009b2J-token=SYNTHETIC_VALUE",
+ );
+ const result = await runCommand(
+ process.execPath,
+ [
+ join(import.meta.dir, "..", "src", "cli.ts"),
+ "export",
+ scan,
+ "--output",
+ join(directory, "results.sarif"),
+ ],
+ {
+ env: {
+ ...process.env,
+ CODEX_HOME: home,
+ CODEX_SECURITY_STATE_DIR: join(directory, "state"),
+ },
+ cwd: directory,
+ timeout: 30_000,
+ },
+ );
+ expect(result.status, result.stderr).toBe(2);
+ expect(result.stdout).toBe("");
+ expect(result.stderr).toContain(
+ "expected an existing non-symlink directory",
+ );
+ expect(result.stderr).toContain("No such file or directory");
+ expect(result.stderr).toContain("token=SYNTHETIC_VALUE");
+ expect(result.stderr).not.toMatch(/[\u001b\u009b]/u);
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+ },
+ );
+
+ test("escapes malformed configuration source from automatic component planning", async () => {
+ const directory = await temporaryDirectory(
+ "codex-security-component-config-",
+ );
+ try {
+ const home = join(directory, "home");
+ const repository = join(directory, "repository");
+ await mkdir(home, { mode: 0o700 });
+ await mkdir(repository);
+ git(repository, "init", "-q");
+ await writeFile(
+ join(repository, "synthetic.ts"),
+ "export const value = 1;\n",
+ );
+ await writeFile(
+ join(home, "config.toml"),
+ 'synthetic = "\u001b[2J\u009b2J-token=SYNTHETIC_VALUE',
+ );
+ const result = await runCommand(
+ process.execPath,
+ [
+ join(import.meta.dir, "..", "src", "cli.ts"),
+ "scan-components",
+ repository,
+ "--auto",
+ "--plan-only",
+ "--headless",
+ "--output-dir",
+ join(directory, "output"),
+ ],
+ {
+ env: {
+ ...process.env,
+ CODEX_HOME: home,
+ CODEX_SECURITY_STATE_DIR: join(directory, "state"),
+ },
+ cwd: directory,
+ timeout: 30_000,
+ },
+ );
+ expect(result.status, result.stderr).toBe(2);
+ expect(result.stderr).toContain(
+ "Could not read the configured Codex provider from",
+ );
+ expect(result.stderr).toContain("token=SYNTHETIC_VALUE");
+ expect(result.stderr).not.toMatch(/[\u001b\u009b]/u);
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+ });
+
+ test.each([
+ {
+ label: "matching",
+ args: ["scans", "match", "synthetic-before", "synthetic-after"],
+ },
+ { label: "feedback", args: ["feedback", "--reason", "Synthetic feedback"] },
+ ])(
+ "escapes malformed configuration source in $label diagnostics",
+ async ({ args }) => {
+ const directory = await temporaryDirectory(
+ "codex-security-config-source-",
+ );
+ try {
+ const home = join(directory, "home");
+ await mkdir(home, { mode: 0o700 });
+ await writeFile(
+ join(home, "config.toml"),
+ 'synthetic = "\u001b[2J\u009b2J-token=SYNTHETIC_VALUE',
+ );
+ const { stderr, runCli } = createCliTest(main);
+ const deps = dependencies({
+ currentDirectory: directory,
+ environment: {
+ CODEX_HOME: home,
+ CODEX_SECURITY_STATE_DIR: join(directory, "state"),
+ },
+ onMatch: matchScanFindings,
+ onWorkbench: () => ({
+ scans: [],
+ matchingInputs: {
+ before: [{ occurrenceId: "synthetic-before" }],
+ after: [{ occurrenceId: "synthetic-after" }],
+ },
+ }),
+ });
+ expect(await runCli(args, deps)).toBe(2);
+ expect(stderr.text()).toContain(
+ "Could not read the configured Codex provider from",
+ );
+ expect(stderr.text()).toContain("token=SYNTHETIC_VALUE");
+ expect(stderr.text()).not.toMatch(/[\u001b\u009b]/u);
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+ },
+ );
+
+ test.skipIf(process.platform === "win32")(
+ "escapes controls in expanded validation configuration diagnostics",
+ async () => {
+ const directory = await temporaryDirectory(
+ "codex-security-config-diagnostic-",
+ );
+ try {
+ const home = join(
+ directory,
+ "synthetic-\u001b[2J\u009b2J-token=SYNTHETIC_VALUE-home",
+ );
+ await mkdir(home);
+ await writeFile(join(home, "config.toml"), "synthetic = [");
+ const { stderr, runCli } = createCliTest(main);
+ const deps = dependencies({
+ environment: { CODEX_HOME: home, OPENAI_API_KEY: "SYNTHETIC_VALUE" },
+ onCodex: async (_args, _output, environment) => {
+ await readCodexHomeConfig(environment!);
+ return 0;
+ },
+ });
+
+ expect(await runCli(["validate", "Synthetic finding"], deps)).toBe(2);
+ expect(stderr.text()).toContain(
+ "Could not read the configured Codex provider from",
+ );
+ expect(stderr.text()).toContain("token=SYNTHETIC_VALUE-home");
+ expect(stderr.text()).not.toMatch(/[\u001b\u009b]/u);
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+ },
+ );
+
+ test.each([{ flags: [] }, { flags: ["--json"] }])(
+ "reports rerun history failures once: $flags",
+ async ({ flags }) => {
+ for (const lookupFails of [false, true]) {
+ const message = lookupFails
+ ? "Synthetic history failure."
+ : "No completed scans found for the current repository.";
+ const calls: string[] = [];
+ const deps = dependencies({
+ onWorkbench: (args) => {
+ calls.push(args[0]!);
+ if (lookupFails) throw new Error(message);
+ return { scans: [] };
+ },
+ });
+ const { stderr, runCli } = createCliTest(main);
+ expect(await runCli(["scans", "rerun", ...flags], deps)).toBe(2);
+ expect(stderr.text()).toBe(`codex-security: ${message}\n`);
+ expect(calls).toEqual(["list-scans"]);
+ }
+ },
+ );
+
+ test("retains local filesystem errors through artifact failure wrappers", async () => {
+ // Reading a directory produces a real local errno on the supported platforms.
+ const cause = await readFile(import.meta.dir).then(
+ () => {
+ throw new Error("Expected a filesystem failure");
+ },
+ (error: unknown) => error,
+ );
+ const wrapped = new CodexSecurityError(
+ "Could not read artifact: permission denied",
+ {
+ cause: new Error("Required artifact could not be read", { cause }),
+ },
+ );
+ const deps = dependencies();
+ deps.createSecurity = () =>
+ fakeSecurity(async () => {
+ throw wrapped;
+ });
+ const { stdout, stderr, runCli } = createCliTest(main);
+ expect(await runCli(["scan", ".", "--json"], deps)).toBe(2);
+ expect(JSON.parse(stdout.text()).message).toBe(wrapped.message);
+ expect(stderr.text()).toContain(wrapped.message);
+ expect(stderr.text()).not.toContain("model access");
+ });
+
test.each<string[]>([
["publish", "scan", "completed-scan", "--to", "cloud"],
["publish", "check", "completed-scan", "--to", "linear"],
@@ -106,6 +340,11 @@ describe("CLI diagnostics", () => {
],
structured: false,
},
+ {
+ command: "validate",
+ args: ["validate", "Synthetic finding"],
+ structured: false,
+ },
{
command: "verify-fix",
args: ["verify-fix", "Synthetic finding"],
@@ -135,7 +374,7 @@ describe("CLI diagnostics", () => {
"escapes terminal controls in $command failures while preserving details",
async ({ command, args, structured }) => {
const message =
- "Operation failed: token=SYNTHETIC_VALUE\u001b[2J\ncontinued\r\ttail";
+ "Operation failed: token=SYNTHETIC_VALUE\u001b[2J\ncontinued\r\ttail\u009b2J\u009d0;title\u009c";
const fail = throwing(message);
const deps = dependencies({ onCodex: fail, onRepositoryCommand: fail });
deps.classifyScanSeverity = fail;
@@ -147,8 +386,9 @@ describe("CLI diagnostics", () => {
const { stdout, stderr, runCli } = createCliTest(main);
expect(await runCli(args, deps)).toBe(2);
+ const separator = command === "validate" ? "\n" : " ";
expect(stderr.text()).toContain(
- "codex-security: Operation failed: token=SYNTHETIC_VALUE [2J continued tail\n",
+ `codex-security: Operation failed: token=SYNTHETIC_VALUE [2J${separator}continued tail 2J 0;title \n`,
);
expect(stderr.text()).not.toContain("\u001b");
if (structured) {
@@ -318,3 +558,319 @@ describe("CLI diagnostics", () => {
expect(stderr.text()).toContain("\u001B[?1049l");
});
});
+
+test.each([
+ { args: ["scans", "resume", "saved"] },
+ { args: ["scans", "rerun", "saved"] },
+ { args: ["scans", "rerun"] },
+])("$args emits structured lookup failures", async ({ args }) => {
+ for (const flags of [
+ ["--json"],
+ ["--format", "jsonl"],
+ ["--json", "--full-output"],
+ ]) {
+ const { stdout, stderr, runCli } = createCliTest(main);
+ const code = await runCli(
+ [...args, ...flags],
+ dependencies({
+ onWorkbench: throwing("Synthetic saved scan lookup failure"),
+ }),
+ );
+ expect(code).toBe(2);
+ const output = JSON.parse(stdout.text());
+ expect(output.error?.message ?? output.message).toBe(
+ "Synthetic saved scan lookup failure",
+ );
+ expect(stderr.text()).toContain("Synthetic saved scan lookup failure");
+ }
+});
+
+test("saved resume rejects Markdown before loading state", async () => {
+ const { stderr, runCli } = createCliTest(main);
+ const code = await runCli(
+ ["scans", "resume", "saved", "--format", "md"],
+ dependencies({
+ onWorkbench: throwing("Must not load state"),
+ }),
+ );
+ expect(code).toBe(2);
+ expect(stderr.text()).toContain("Markdown output is not supported");
+});
+
+test.each([
+ { args: ["classify-severity", "--scan", "--rubric", "rules.md"] },
+ {
+ args: [
+ "dedupe",
+ "--scan",
+ "--findings-url",
+ "https://example.test/findings",
+ ],
+ },
+])("requires values for saved scan selectors in $args", async ({ args }) => {
+ const { stderr, runCli } = createCliTest(main);
+ expect(await runCli(args, dependencies())).toBe(2);
+ expect(stderr.text()).toContain("Missing value for flag: --scan");
+});
+
+test.each(["authentication", "patch review"])(
+ "treats dismissed %s prompts as cancellation",
+ async (stage) => {
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+ const error = new Error("User dismissed the prompt");
+ error.name = "ExitPromptError";
+ const deps = dependencies({
+ environment: { OPENAI_API_KEY: "synthetic-key" },
+ });
+ if (stage === "authentication") {
+ deps.hasStoredChatGPTSignIn = async () => true;
+ deps.scanAuthenticationPrompt = {
+ isInteractive: () => true,
+ select: async () => {
+ throw error;
+ },
+ };
+ } else {
+ deps.createSecurity = () =>
+ fakeSecurity(async () => fakeResult(["high"]));
+ deps.confirmPatchReview = async () => {
+ throw error;
+ };
+ deps.patchEditor = async () => null;
+ }
+ expect(await runCli(["scan", "--format", "toon"], deps)).toBe(130);
+ expect(stderr.text()).toContain("canceled");
+ if (stage === "patch review") expect(stdout.text()).toContain("manifest:");
+ },
+);
+
+test.each(
+ ["scan", "resume", "rerun"].flatMap((command) =>
+ ["confirmation", "selector"].flatMap((stage) =>
+ (
+ [
+ { format: undefined, fullOutput: false },
+ { format: undefined, fullOutput: true },
+ { format: "toon", fullOutput: false },
+ { format: "toon", fullOutput: true },
+ { format: "yaml", fullOutput: false },
+ { format: "yaml", fullOutput: true },
+ ] as const
+ ).map((output) => ({ command, stage, ...output })),
+ ),
+ ),
+)(
+ "retains $command results when patch $stage is canceled, format: $format, full output: $fullOutput",
+ async ({ command, stage, format, fullOutput }) => {
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+ const result = fakeResult(["high"]);
+ const cancellation = new Error("User dismissed the prompt");
+ cancellation.name = "ExitPromptError";
+ const message =
+ "Patch review canceled by Ctrl-C. Saved scan results were kept.";
+ let confirmations = 0;
+ let selections = 0;
+ let patches = 0;
+ const deps = dependencies({
+ result,
+ onWorkbench: () => ({
+ scanId: "saved-canonical",
+ scanDir: "/synthetic/scan-output",
+ recipe: {
+ repository: "/synthetic/repository",
+ target: { kind: "repository", paths: [] },
+ mode: "deep",
+ config: {},
+ },
+ }),
+ environment: { OPENAI_API_KEY: "synthetic-key" },
+ onCodex: () => {
+ patches += 1;
+ return 0;
+ },
+ });
+ deps.confirmPatchReview = async () => {
+ confirmations += 1;
+ if (stage === "confirmation") throw cancellation;
+ return true;
+ };
+ deps.patchEditor = async () => {
+ selections += 1;
+ throw cancellation;
+ };
+ const code = await runCli(
+ [
+ ...(command === "scan" ? ["scan"] : ["scans", command, "saved"]),
+ ...(command === "scan" && stage === "selector" ? ["--patch"] : []),
+ ...(format === undefined ? [] : ["--format", format]),
+ ...(fullOutput ? ["--full-output"] : []),
+ ],
+ deps,
+ );
+ expect(code).toBe(130);
+ expect(confirmations).toBe(
+ command === "scan" && stage === "selector" ? 0 : 1,
+ );
+ expect(selections).toBe(stage === "selector" ? 1 : 0);
+ expect(patches).toBe(0);
+ expect(stderr.text()).toContain(`codex-security: ${message}\n`);
+ const output = stdout.text();
+ if (fullOutput) {
+ expect(output).toStartWith("ok: false\n");
+ expect(output).toContain(
+ Formatter.format(
+ { error: { code: "SCAN_FAILED", message } },
+ format,
+ ).trimEnd(),
+ );
+ expect(output).toContain(
+ Formatter.format({ data: result.toJSON() }, format).trimEnd(),
+ );
+ expect(output).toContain(
+ `meta:\n command: ${command === "scan" ? "scan" : `scans ${command}`}\n`,
+ );
+ } else if (format !== undefined) {
+ expect(output).toBe(
+ `${Formatter.format(result.toJSON(), format).trimEnd()}\n`,
+ );
+ } else {
+ expect(output).toBe("");
+ }
+ },
+);
+
+test.each(
+ ["scan", "resume", "rerun"].flatMap((command) =>
+ ["json", "jsonl"].map((format) => ({ command, format })),
+ ),
+)(
+ "keeps $format $command output noninteractive with full output",
+ async ({ command, format }) => {
+ const { stdout, runCli } = createCliTest(main, { stderr: true });
+ const result = fakeResult(["high"]);
+ let prompts = 0;
+ const deps = dependencies({
+ result,
+ onWorkbench: () => ({
+ scanId: "saved-canonical",
+ scanDir: "/synthetic/scan-output",
+ recipe: {
+ repository: "/synthetic/repository",
+ target: { kind: "repository", paths: [] },
+ mode: "deep",
+ config: {},
+ },
+ }),
+ environment: { OPENAI_API_KEY: "synthetic-key" },
+ });
+ deps.confirmPatchReview = async () => {
+ prompts += 1;
+ throw new Error("Must not prompt");
+ };
+ deps.patchEditor = async () => {
+ prompts += 1;
+ throw new Error("Must not select patches");
+ };
+ expect(
+ await runCli(
+ [
+ ...(command === "scan" ? ["scan"] : ["scans", command, "saved"]),
+ "--format",
+ format,
+ "--full-output",
+ ],
+ deps,
+ ),
+ ).toBe(0);
+ expect(prompts).toBe(0);
+ expect(JSON.parse(stdout.text())).toMatchObject({
+ ok: true,
+ data: JSON.parse(JSON.stringify(result.toJSON())),
+ });
+ },
+);
+
+test("preserves complete YAML diagnostics with retained scan results", async () => {
+ const { stdout, stderr, runCli } = createCliTest(main);
+ const result = fakeResult(["high"]);
+ const message = "Synthetic patch preparation failure.\nDetail retained.\n\n";
+ let commands = 0;
+ const deps = dependencies({
+ result,
+ onRepositoryCommand: () => {
+ commands += 1;
+ throw new Error(message);
+ },
+ });
+ expect(
+ await runCli(
+ [
+ "scan",
+ "--patch",
+ "--patch-severity",
+ "high",
+ "--format",
+ "yaml",
+ "--full-output",
+ ],
+ deps,
+ ),
+ ).toBe(2);
+ expect(commands).toBeGreaterThan(0);
+ expect(stderr.text()).toContain(
+ "codex-security: Synthetic patch preparation failure. Detail retained. \n",
+ );
+ const output = parseYaml(stdout.text());
+ expect(output.ok).toBe(false);
+ expect(output.error).toEqual({ code: "SCAN_FAILED", message });
+ expect(output.data).toEqual({
+ ...JSON.parse(JSON.stringify(result.toJSON())),
+ patches: [],
+ });
+});
+
+test("uses stored API-key advice for a forbidden model request", async () => {
+ const { stderr, runCli } = createCliTest(main);
+ const deps = dependencies();
+ deps.createSecurity = () =>
+ fakeSecurity(async (_repository, options) => {
+ options?.onAuthentication?.({
+ method: "stored_credentials",
+ credentialType: "api_key",
+ verified: false,
+ });
+ throw new Error("403 model access denied");
+ });
+ expect(await runCli(["scan", "--json"], deps)).toBe(2);
+ expect(stderr.text()).toContain("stored API key");
+ expect(stderr.text()).not.toContain("stored ChatGPT credentials");
+});
+
+test.each(["resume", "rerun"])(
+ "saved %s honors structured output on a terminal",
+ async (command) => {
+ for (const flags of [["--json"], ["--format", "jsonl"], []]) {
+ const { stdout, stderr, runCli } = createCliTest(main, { stderr: true });
+ const code = await runCli(
+ ["scans", command, "saved", ...flags],
+ dependencies({
+ onWorkbench: () => ({
+ scanId: "saved-canonical",
+ scanDir: "/synthetic/scan-output",
+ recipe: {
+ repository: "/synthetic/repository",
+ target: { kind: "repository", paths: [] },
+ mode: "deep",
+ config: {},
+ },
+ }),
+ }),
+ );
+ expect(code, stderr.text()).toBe(0);
+ if (flags.length > 0) {
+ expect(JSON.parse(stdout.text())).toHaveProperty("manifest");
+ expect(stderr.text()).not.toContain("\u001b[?1049h");
+ } else expect(stdout.text()).toBe("");
+ }
+ },
+);
diff --git a/sdk/typescript/tests-ts/cli-error-diagnostics.test.ts b/sdk/typescript/tests-ts/cli-error-diagnostics.test.ts
index 46726b524..ed937006b 100644
--- a/sdk/typescript/tests-ts/cli-error-diagnostics.test.ts
+++ b/sdk/typescript/tests-ts/cli-error-diagnostics.test.ts
@@ -1,12 +1,13 @@
-import { mkdir, rm, writeFile } from "node:fs/promises";
+import { chmod, mkdir, 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 { IncompleteScanError } from "../src/errors.js";
+import { ContractValidationError, IncompleteScanError } from "../src/errors.js";
+import { completedEvents, runEvents } from "./support/api-events.js";
import { parseImportedFindings } from "../src/findings-import.js";
import { runWorkbench } from "../src/runtime.js";
-import { PLUGIN_ROOT } from "./plugin-root.js";
+import { PLUGIN_ROOT, copyCompletedScan } from "./plugin-root.js";
import { dependencies } from "./cli-fixtures.js";
import { createCliTest } from "./support/cli-run.js";
import { temporaryDirectory } from "./support/temporary-directories.js";
@@ -182,6 +183,12 @@ test.each([
diagnostic: "403 synthetic model access denied",
advice: "cannot access the configured model",
},
+ {
+ label: "stored API key authorization",
+ diagnostic: "403 synthetic model access denied",
+ advice: "The stored API key cannot access the configured model",
+ storedApiKey: true,
+ },
{
label: "rate limit",
diagnostic: "429 synthetic quota exceeded",
@@ -199,6 +206,14 @@ test.each([
for (const json of [false, true]) {
const { stdout, stderr, runCli } = createCliTest(main);
const deps = dependencies({
+ onTurn: (_repository, options) => {
+ if ("storedApiKey" in scenario)
+ options.onAuthentication?.({
+ method: "stored_credentials",
+ credentialType: "api_key",
+ verified: false,
+ });
+ },
onRun: () => {
throw failure;
},
@@ -292,3 +307,43 @@ for (const command of ["import", "owners"] as const) {
}
});
}
+
+test.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
+ "preserves native artifact failures through the SDK and CLI",
+ async () => {
+ const root = await temporaryDirectory(
+ "artifact-permission-diagnostic-",
+ true,
+ );
+ const scanDir = await copyCompletedScan(root);
+ try {
+ await chmod(scanDir, 0o600);
+ const failure = await runEvents(scanDir, completedEvents()).catch(
+ (error: unknown) => error,
+ );
+ expect(failure).toBeInstanceOf(ContractValidationError);
+ const cause = (failure as Error).cause as NodeJS.ErrnoException;
+ expect(cause.code).toBe("EACCES");
+ expect(cause.path).toBe(join(scanDir, "scan-manifest.json"));
+ expect((failure as Error).message).toContain(cause.message);
+
+ const { stdout, stderr, runCli } = createCliTest(main);
+ expect(
+ await runCli(
+ ["scan", ".", "--json"],
+ dependencies({
+ onRun: () => {
+ throw failure;
+ },
+ }),
+ ),
+ ).toBe(2);
+ expect(stderr.text()).toContain(cause.message);
+ expect(JSON.parse(stdout.text()).message).toContain(cause.message);
+ expect((failure as Error).cause).toBe(cause);
+ } finally {
+ await chmod(scanDir, 0o700);
+ await rm(root, { recursive: true, force: true });
+ }
+ },
+);
diff --git a/sdk/typescript/tests-ts/cli-export.test.ts b/sdk/typescript/tests-ts/cli-export.test.ts
index 76a870dbe..a2604f780 100644
--- a/sdk/typescript/tests-ts/cli-export.test.ts
+++ b/sdk/typescript/tests-ts/cli-export.test.ts
@@ -951,6 +951,44 @@ describe("CLI", () => {
expect(stderr.text()).not.toContain("ENOENT");
});
+ (process.platform === "win32" ? test.skip : test).each(["\u001b", "\u009b"])(
+ "escapes terminal controls in Python export errors: %j",
+ async (control) => {
+ const directory = await temporaryDirectory("codex-security-export-");
+ try {
+ const scan = join(
+ directory,
+ `missing-${control}sk-proj-SYNTHETIC_KEEP`,
+ );
+ const output = join(scan, "exports", "results.sarif");
+ const stdout = capture();
+ const stderr = capture();
+ expect(
+ await main(
+ ["export", scan, "--output", output],
+ stdout.stream,
+ stderr.stream,
+ ),
+ ).toBe(2);
+ expect(stdout.text()).toBe("");
+ expect(stderr.text()).not.toContain(control);
+ const escapedScan = scan.replace(
+ control,
+ `\\x${control.charCodeAt(0).toString(16)}`,
+ );
+ expect(stderr.text()).toContain(
+ `scan directory ${escapedScan}: expected an existing non-symlink directory`,
+ );
+ expect(stderr.text()).toContain("No such file or directory");
+ await expect(
+ exportArtifact({ source: { directory: scan }, output }),
+ ).rejects.toThrow(escapedScan);
+ } finally {
+ await rm(directory, { recursive: true, force: true });
+ }
+ },
+ );
+
test("rejects a repository-controlled output symlink without following it", async () => {
await using temp = await scopedTemporaryDirectory("codex-security-export-");
const directory = temp.path;
diff --git a/sdk/typescript/tests-ts/cli-launcher.test.ts b/sdk/typescript/tests-ts/cli-launcher.test.ts
index b9eb57d2a..0f09d38d5 100644
--- a/sdk/typescript/tests-ts/cli-launcher.test.ts
+++ b/sdk/typescript/tests-ts/cli-launcher.test.ts
@@ -64,6 +64,34 @@ describe("CLI launcher", () => {
);
});
+ test("preserves installed-launcher startup failures while escaping C1 controls", async () => {
+ await using temp = await scopedTemporaryDirectory(
+ "codex-security-cli-bin-failure-",
+ );
+ const root = temp.path;
+ const controls = String.fromCharCode(
+ ...Array.from({ length: 33 }, (_, index) => 0x7f + index),
+ );
+ 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}${controls} details`)});\n`,
+ );
+ const child = await runCommand("node", [launcher], {
+ env: { ...process.env, NODE_NO_WARNINGS: "1" },
+ timeout: 30_000,
+ });
+
+ expect(child.status).toBe(2);
+ expect(child.stdout).toBe("");
+ expect(child.stderr).toBe(
+ `codex-security: Failed to start Codex Security: failed ${SYNTHETIC_CREDENTIALS}${" ".repeat(controls.length)} details\n`,
+ );
+ });
+
test.each(["import", "main", "missing", "success"])(
"preserves installed-launcher %s results",
async (scenario) => {
diff --git a/sdk/typescript/tests-ts/cli-project-config.test.ts b/sdk/typescript/tests-ts/cli-project-config.test.ts
index 838dbac46..acb8ccbe5 100644
--- a/sdk/typescript/tests-ts/cli-project-config.test.ts
+++ b/sdk/typescript/tests-ts/cli-project-config.test.ts
@@ -1380,7 +1380,7 @@ test.each([
["scans", "rerun", "saved", "--json", "--full-output"],
dependencies({
currentDirectory: input.repository,
- onWorkbench: async () => ({ recipe }),
+ onWorkbench: async () => ({ recipe, scanId: "saved" }),
onRun,
}),
),
diff --git a/sdk/typescript/tests-ts/cli-publish.test.ts b/sdk/typescript/tests-ts/cli-publish.test.ts
index 65828353d..4dca890bd 100644
--- a/sdk/typescript/tests-ts/cli-publish.test.ts
+++ b/sdk/typescript/tests-ts/cli-publish.test.ts
@@ -821,6 +821,25 @@ describe("publish check", () => {
expect(stdout.text().trim()).toBe("");
});
+ test("escapes terminal controls in check failures while preserving diagnostic text", async () => {
+ const { stdout, stderr, runCli } = createCliTest(main);
+ const deps = dependencies();
+ deps.checkScanPublication = async () =>
+ fail(
+ "Linear check failed: \u001B[2Jrequest\rdenied\nkey=sk-synthetic-example",
+ );
+ expect(
+ await runCli(
+ ["publish", "check", "completed-scan", ...DESTINATION_OPTIONS],
+ deps,
+ ),
+ ).toBe(2);
+ expect(stderr.text()).toBe(
+ "codex-security: Linear check failed: [2Jrequest denied\nkey=sk-synthetic-example\n",
+ );
+ expect(stdout.text().trim()).toBe("");
+ });
+
test("waits for read-only publication cleanup on terminal signals", async () => {
for (const command of [
["check"],
diff --git a/sdk/typescript/tests-ts/cli-saved-failure-formats.test.ts b/sdk/typescript/tests-ts/cli-saved-failure-formats.test.ts
new file mode 100644
index 000000000..3a057c4cd
--- /dev/null
+++ b/sdk/typescript/tests-ts/cli-saved-failure-formats.test.ts
@@ -0,0 +1,128 @@
+import { readFile, rm } from "node:fs/promises";
+import { join } from "node:path";
+import { expect, test } from "bun:test";
+import { Formatter } from "incur";
+import { parse as parseYaml } from "yaml";
+import { loadContract } from "../src/index.js";
+import { copyCompletedScan, PLUGIN_ROOT } from "./plugin-root.js";
+import { runCommand } from "./support/shell.js";
+import { temporaryDirectory } from "./support/temporary-directories.js";
+
+const message = "Synthetic execution failure: token=SYNTHETIC_SAVED_FAILURE";
+const commands = ["scan", "resume", "rerun"] as const;
+const formats = ["json", "jsonl", "yaml", "toon", undefined] as const;
+
+async function failedCommand(
+ command: (typeof commands)[number],
+ format: (typeof formats)[number],
+ { fullOutput = true, invalid = false, controls = [] as string[] } = {},
+) {
+ const root = await temporaryDirectory("cli-saved-failure-format-");
+ try {
+ const scanDir = await copyCompletedScan(root);
+ const contract = await loadContract(scanDir, { pluginRoot: PLUGIN_ROOT });
+ const scanId = contract.manifest.scan.id;
+ const originalManifest = await readFile(
+ join(scanDir, "scan-manifest.json"),
+ );
+ if (invalid) await rm(join(scanDir, "findings.json"));
+ const args = [
+ ...(command === "scan" ? ["scan", root] : ["scans", command, scanId]),
+ ...(format === undefined ? [] : ["--format", format]),
+ ...(fullOutput ? ["--full-output"] : []),
+ ...controls,
+ ];
+ const cliUrl = new URL("../src/cli.ts", import.meta.url).href;
+ const fixtureUrl = new URL("./cli-fixtures.ts", import.meta.url).href;
+ const script = `
+ import { main } from ${JSON.stringify(cliUrl)};
+ import { dependencies } from ${JSON.stringify(fixtureUrl)};
+ const scan = ${JSON.stringify({ scanId, scanDir })};
+ const deps = dependencies({
+ currentDirectory: ${JSON.stringify(root)},
+ onWorkbench: () => ({ ...scan, recipe: {
+ repository: ${JSON.stringify(root)},
+ target: { kind: "repository", paths: [] }, mode: "standard", config: {},
+ }}),
+ onTurn: (_repository, options) => options.onScanRegistered?.(scan),
+ onRun: () => { throw new Error(${JSON.stringify(message)}); },
+ });
+ process.exitCode = await main(${JSON.stringify(args)}, process.stdout, process.stderr, deps);
+ `;
+ const result = await runCommand(process.execPath, ["-e", script], {
+ cwd: root,
+ timeout: 30_000,
+ });
+ expect(result.status, result.stderr).toBe(2);
+ expect(result.signal).toBeNull();
+ expect(result.stderr).toContain(message);
+ expect(result.stderr).toContain(scanId);
+ expect(await readFile(join(scanDir, "scan-manifest.json"))).toEqual(
+ originalManifest,
+ );
+ return {
+ ...result,
+ data: { ...contract, scanDir, reportPath: join(scanDir, "report.md") },
+ };
+ } finally {
+ await rm(root, { recursive: true, force: true });
+ }
+}
+
+test.each(
+ commands.flatMap((command) => formats.map((format) => ({ command, format }))),
+)(
+ "retains saved $command execution results in full $format output",
+ async ({ command, format }) => {
+ const result = await failedCommand(command, format);
+ if (format === "json" || format === "jsonl" || format === "yaml") {
+ const output =
+ format === "yaml"
+ ? parseYaml(result.stdout)
+ : JSON.parse(result.stdout);
+ expect(output).toMatchObject({
+ ok: false,
+ error: { code: "SCAN_FAILED", message },
+ data: result.data,
+ meta: { command: command === "scan" ? "scan" : `scans ${command}` },
+ });
+ expect(output.data).toEqual(result.data);
+ } else {
+ expect(result.stdout).toStartWith("ok: false\n");
+ expect(result.stdout).toContain(
+ Formatter.format({ error: { code: "SCAN_FAILED", message } }, "toon"),
+ );
+ expect(result.stdout).toContain(
+ Formatter.format({ data: result.data }, "toon"),
+ );
+ expect(result.stdout).toContain(
+ `meta:\n command: ${command === "scan" ? "scan" : `scans ${command}`}\n`,
+ );
+ }
+ },
+);
+
+test.each(
+ commands.flatMap((command) =>
+ (["yaml", "toon"] as const).map((format) => ({ command, format })),
+ ),
+)(
+ "keeps $command $format execution failures on stderr without full output",
+ async ({ command, format }) => {
+ const result = await failedCommand(command, format, { fullOutput: false });
+ expect(result.stdout).toBe("");
+ },
+);
+
+test.each(
+ commands.flatMap((command) =>
+ (["yaml", "toon"] as const).map((format) => ({ command, format })),
+ ),
+)(
+ "preserves the $command $format execution error when retained data is invalid",
+ async ({ command, format }) => {
+ const result = await failedCommand(command, format, { invalid: true });
+ expect(result.stdout).toBe("");
+ expect(result.stderr).not.toContain("findings.json");
+ },
+);
diff --git a/sdk/typescript/tests-ts/cli-signals.test.ts b/sdk/typescript/tests-ts/cli-signals.test.ts
index 43dad7aee..a0499e512 100644
--- a/sdk/typescript/tests-ts/cli-signals.test.ts
+++ b/sdk/typescript/tests-ts/cli-signals.test.ts
@@ -1,4 +1,5 @@
import { describe, expect, test, mock } from "bun:test";
+import { Formatter } from "incur";
import { main } from "../src/cli.js";
import { ScanInterruptedError } from "../src/errors.js";
import {
@@ -83,6 +84,51 @@ describe("CLI signals", () => {
expect(stderr.text()).not.toContain("scans show");
});
+ test.each([
+ ["SIGINT", 130],
+ ["SIGTERM", 143],
+ ] as const)(
+ "retains completed scan output when %s interrupts validation",
+ async (signal, expectedExit) => {
+ const { stdout, stderr, runCli } = createCliTest(main);
+ const signals = new FakeSignals();
+ const result = fakeResult();
+ const exit = await runCli(
+ ["scan", ".", "--validate"],
+ dependencies({
+ currentDirectory: process.cwd(),
+ result,
+ signals,
+ onWorkbench: (args) => {
+ expect(args).toEqual([
+ "get-scan",
+ "--scan-id",
+ result.manifest.scan.id,
+ "--check-target",
+ ]);
+ signals.emit(signal);
+ return { scan: { targetPath: process.cwd() } };
+ },
+ }),
+ );
+ expect(exit).toBe(expectedExit);
+ expect(stderr.text()).toContain("Validation interrupted.");
+ expect(stdout.text()).toBe(
+ Formatter.format(
+ {
+ ...result.toJSON(),
+ validation: {
+ status: "failed",
+ findings: 0,
+ exitCode: expectedExit,
+ },
+ },
+ "toon",
+ ).trimEnd() + "\n",
+ );
+ },
+ );
+
test("preserves signals received during client cleanup", async () => {
const { stdout, stderr, runCli } = createCliTest(main);
diff --git a/sdk/typescript/tests-ts/cli-skills.test.ts b/sdk/typescript/tests-ts/cli-skills.test.ts
index 2f10467a9..ba4eddc3d 100644
--- a/sdk/typescript/tests-ts/cli-skills.test.ts
+++ b/sdk/typescript/tests-ts/cli-skills.test.ts
@@ -1535,27 +1535,194 @@ process.stdout.write(JSON.stringify({
});
});
- 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.each([
+ ...(["validate", "patch", "verify-fix"] as const).map(
+ (command) =>
+ [
+ command,
+ "insufficient permissions to use this model sk-proj-SYNTHETIC_KEEP",
+ "The selected model is unavailable for the current credentials.",
+ ] as const,
+ ),
+ ...(["validate", "patch", "verify-fix"] as const).flatMap((command) =>
+ [
+ "model 'gpt-5' not found",
+ 'model "synthetic/provider-v2" not found',
+ "model `synthetic-v2` not found",
+ "model `synthetic/provider-v2` not found",
+ ].map(
+ (detail) =>
+ [
+ command,
+ detail,
+ "The selected model is unavailable for the current credentials.",
+ ] as const,
+ ),
+ ),
+ ...(["validate", "patch", "verify-fix"] as const).flatMap((command) =>
+ [
+ "model cache not found",
+ "model metadata file not found",
+ "permission denied opening model cache",
+ "model 'synthetic'\nconfiguration entry not found",
+ "Safety policy: request refused.",
+ "permission denied opening /synthetic/response_status=403/cache",
+ ].map((detail) => [command, detail, null] as const),
+ ),
+ ...(["validate", "patch", "verify-fix"] as const).flatMap((command) =>
+ (
+ [
+ ["response_code=401", "Authentication failed."],
+ ["response.status=401", "Authentication failed."],
+ ["response.status_code=401", "Authentication failed."],
+ ["http.response.status_code=401", "Authentication failed."],
+ ["HTTP 401: model 'gpt-5' not found", "Authentication failed."],
+ [
+ "response-code=403",
+ "The selected model is unavailable for the current credentials.",
+ ],
+ ["responseCode=429", "The request was rate limited. Wait and retry."],
+ ["responseStatus=401", "Authentication failed."],
+ [
+ "response_status=403",
+ "The selected model is unavailable for the current credentials.",
+ ],
+ [
+ "HTTP responseStatus=429",
+ "The request was rate limited. Wait and retry.",
+ ],
+ [
+ '{"response_status":"429"}',
+ "The request was rate limited. Wait and retry.",
+ ],
+ ] as const
+ ).map(([detail, advice]) => [command, detail, advice] as const),
+ ),
+ ] as const)(
+ "retains failure advice and diagnostics from the %s child: %s",
+ async (command, detail, advice) => {
+ const stdout = capture();
+ const stderr = capture();
+ const source = `process.stdout.write(JSON.stringify({type:"turn.failed",error:{message:${JSON.stringify(detail)}}})+"\\n");process.exitCode=7`;
+ expect(
+ await runCodexSkillCommand(
+ ["-e", source],
+ { command, stdout: stdout.stream, stderr: stderr.stream },
+ { command: process.execPath },
+ ),
+ ).toBe(7);
+ expect(stdout.text()).toBe("");
+ if (advice === null)
+ expect(stderr.text()).not.toContain("selected model is unavailable");
+ else expect(stderr.text()).toContain(advice);
+ expect(stderr.text()).toContain(detail);
+ },
+ );
+
+ test.each(
+ (["validate", "patch", "verify-fix"] as const).flatMap((command) =>
+ (
+ [
+ ["database model 'User' not found", false],
+ ['sqlite3 model "User" not found', false],
+ ["workbench model `User` not found", false],
+ [
+ "model 'synthetic' not found https://synthetic.invalid/database",
+ true,
+ ],
+ ] as const
+ ).map(([detail, modelAccess]) => [command, detail, modelAccess] as const),
+ ),
+ )(
+ "distinguishes local and service named-model failures from the %s child: %s",
+ async (command, detail, modelAccess) => {
+ const stdout = capture();
+ const stderr = capture();
+ const source = `process.stdout.write(JSON.stringify({type:"turn.failed",error:{message:${JSON.stringify(detail)}}})+"\\n");process.exitCode=7`;
+ expect(
+ await runCodexSkillCommand(
+ ["-e", source],
+ { command, stdout: stdout.stream, stderr: stderr.stream },
+ { command: process.execPath },
+ ),
+ ).toBe(7);
+ expect(stdout.text()).toBe("");
+ expect(stderr.text()).toBe(
+ `codex-security: ${command} failed with exit code 7.${modelAccess ? " The selected model is unavailable for the current credentials." : ""}\n${detail}\n`,
+ );
+ },
+ );
+
+ test.each(
+ (["validate", "patch", "verify-fix"] as const).flatMap((command) =>
+ (
+ [
+ ["SQLite database request timed_out waiting for lock", false],
+ ["sqlite3.OperationalError: timeout waiting for lock", false],
+ ["workbench request timed-out waiting for lock", false],
+ ["database network_error reading local state", false],
+ ["request timed_out", true],
+ ["network.error", true],
+ ["ECONNABORTED", true],
+ ["request timed_out https://synthetic.invalid/database", true],
+ ] as const
+ ).map(([detail, network]) => [command, detail, network] as const),
+ ),
+ )(
+ "preserves local and service timeout diagnostics from the %s child: %s",
+ async (command, detail, network) => {
+ const stdout = capture();
+ const stderr = capture();
+ const source = `process.stdout.write(JSON.stringify({type:"turn.failed",error:{message:${JSON.stringify(detail)}}})+"\\n");process.exitCode=7`;
+ expect(
+ await runCodexSkillCommand(
+ ["-e", source],
+ { command, stdout: stdout.stream, stderr: stderr.stream },
+ { command: process.execPath },
+ ),
+ ).toBe(7);
+ expect(stdout.text()).toBe("");
+ expect(stderr.text()).toBe(
+ `codex-security: ${command} failed with exit code 7.${network ? " Codex could not connect to the model service. Check the network and retry." : ""}\n${detail}\n`,
+ );
+ },
+ );
+
+ test("preserves skill failure details alongside helpful advice", () => {
+ const cases = [
+ ["HTTP 401 sk-proj-SYNTHETIC_SECRET", "Authentication failed"],
+ [
+ "403 model access denied /private/repository",
+ "selected model is unavailable",
+ ],
+ [
+ "You do not have permission to use this model",
+ "selected model is unavailable",
+ ],
+ [
+ "You do not have permissions to use the model",
+ "selected model is unavailable",
+ ],
+ ["429 tokens per minute sk-proj-SYNTHETIC_SECRET", "rate limited"],
+ [
+ "tokens per minute limit exceeded sk-proj-SYNTHETIC_SECRET",
+ "rate limited",
+ ],
+ ["tokens-per-minute limit exceeded", "rate limited"],
+ ["tokens_per_minute limit exceeded", "rate limited"],
+ [
+ "models cache supports_reasoning_summaries /private/home",
+ "model metadata",
+ ],
+ ["ENOTFOUND /private/repository", "could not connect"],
+ ["ECONNABORTED sk-proj-SYNTHETIC_SECRET", "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).toContain(detail!);
+ }
});
test.each([7, 130, 143])(
@@ -1614,6 +1781,178 @@ process.stdout.write(JSON.stringify({
expect(stderr.text()).not.toContain("\r");
});
+ test.each([
+ "Failed after 1401 bytes",
+ "permission denied opening cache",
+ "permission denied opening model cache",
+ "line 1429 could not be parsed",
+ "count_tokens_per_minute_limit is undefined",
+ "token_expired_cache is undefined",
+ "ModelAccessDeniedCache is undefined",
+ ])("does not misclassify an operational skill failure: %s", (detail) => {
+ const message = skillCommandFailure("patch", 1, detail);
+ expect(message).toContain(detail);
+ expect(message).not.toContain("Authentication failed");
+ expect(message).not.toContain("selected model is unavailable");
+ expect(message).not.toContain("rate limited");
+ });
+
+ test.each([
+ "cache lookup failed at C:\\synthetic\\model_access_denied\\cache",
+ "permission denied opening /synthetic/rate_limit/cache",
+ "could not open /synthetic/request_timeout/cache",
+ "database cache schema migration failed",
+ "SyntaxError: invalid token at line 5",
+ ])("retains local diagnostics without provider advice: %s", (detail) => {
+ expect(skillCommandFailure("validate", 7, detail)).toBe(
+ `validate failed with exit code 7.\n${detail}`,
+ );
+ });
+
+ const diagnosticAdviceCases = [
+ ...(
+ [
+ ["invalid api key", "Authentication failed"],
+ ["token expired", "Authentication failed"],
+ ["model not found", "selected model is unavailable"],
+ ["model access denied", "selected model is unavailable"],
+ ["access denied to model", "selected model is unavailable"],
+ ["access to model denied", "selected model is unavailable"],
+ ["rate limit", "rate limited"],
+ ["rate limited", "rate limited"],
+ ["rate limit exceeded", "rate limited"],
+ ["tokens per minute", "rate limited"],
+ ["timed out", "could not connect"],
+ ["timeout error", "could not connect"],
+ ["network error", "could not connect"],
+ ["network timeout", "could not connect"],
+ ] as const
+ ).flatMap(([words, advice]) =>
+ [" ", "_", "-", "."].map((separator) => {
+ const code = words.replaceAll(" ", separator);
+ return [
+ words === "timed out" ? `request ${code}` : code,
+ advice,
+ ] as const;
+ }),
+ ),
+ ...(
+ [
+ ["401", "Authentication failed"],
+ ["403", "selected model is unavailable"],
+ ["429", "rate limited"],
+ ] as const
+ ).flatMap(([status, advice]) => [
+ ...[
+ `HTTP ${status}`,
+ `HTTP/1.1 ${status}`,
+ `HTTP code: ${status}`,
+ `HTTPError:${status}`,
+ `HTTP error code: ${status}`,
+ `HTTP response code ${status}`,
+ `response code: ${status}`,
+ `response code: "${status}"`,
+ `response_code=${status}`,
+ `response-code=${status}`,
+ `responseCode=${status}`,
+ `responseStatus=${status}`,
+ `response_status=${status}`,
+ `HTTP response_code=${status}`,
+ `status code ${status}`,
+ `status_code=${status}`,
+ `{"status":${status}}`,
+ `{"status":"${status}"}`,
+ `status code: "${status}"`,
+ `status='${status}'`,
+ ].map((detail) => [detail, advice] as const),
+ ...[
+ `parse failed on line ${status}`,
+ `parse failed on line "${status}"`,
+ `failed after ${status} bytes`,
+ `error code: ${status}`,
+ `response_code_cache=${status}`,
+ `response-code-count ${status}`,
+ `responseCodeCount=${status}`,
+ `responseStatusCount=${status}`,
+ `response_status_cache=${status}`,
+ `${status} bytes read`,
+ status,
+ ].map((detail) => [detail, null] as const),
+ ]),
+ ["database responseStatus=401", null],
+ ["sqlite3 response_status=403", null],
+ ["workbench responseStatus=429", null],
+ ["permission denied opening /synthetic/response_status=403/cache", null],
+ [
+ "permission denied opening C:\\synthetic\\response_status=403\\cache",
+ null,
+ ],
+ ["could not open responseStatus=401/cache", null],
+ ["permission denied opening C:\\synthetic\\response_code=403\\cache", null],
+ ["could not open /synthetic/status_code=429/cache", null],
+ ["could not open /synthetic/http.response.status_code=401/cache", null],
+ ["401 Unauthorized", "Authentication failed"],
+ ["403 Forbidden", "selected model is unavailable"],
+ ["429 Too Many Requests", "rate limited"],
+ ["ThrottlingException", "rate limited"],
+ ["ExpiredTokenException", "Authentication failed"],
+ ["HTTP 403 ExpiredTokenException", "Authentication failed"],
+ ["HTTP 401; parsed 429 bytes", "Authentication failed"],
+ ["HTTP request failed while parsing line 429", null],
+ ["permission denied opening /synthetic/403/cache", null],
+ ["401 sk-proj-SYNTHETIC_SECRET", null],
+ ["NetworkError", "could not connect"],
+ ["TimeoutError", "could not connect"],
+ ["RequestTimeout", "could not connect"],
+ ["ModelAccessDenied", "selected model is unavailable"],
+ ["accessDeniedToModel", "selected model is unavailable"],
+ ["accessToModelDenied", "selected model is unavailable"],
+ ] as const;
+
+ test.each(
+ (["validate", "patch", "verify-fix"] as const).flatMap((command) =>
+ diagnosticAdviceCases.map(
+ ([detail, advice]) => [command, detail, advice] as const,
+ ),
+ ),
+ )("preserves diagnostic advice from %s for %s", (command, detail, advice) => {
+ const message = skillCommandFailure(command, 7, detail);
+ if (advice === null) {
+ expect(message).not.toContain("Authentication failed");
+ expect(message).not.toContain("selected model is unavailable");
+ expect(message).not.toContain("rate limited");
+ } else {
+ expect(message).toContain(advice);
+ }
+ expect(message).toContain(detail);
+ });
+
+ test.each([
+ "response_code=401",
+ "response-code=403",
+ "responseCode=429",
+ "responseStatus=401",
+ "response_status=403",
+ ])("keeps response-code URL components separate from advice: %s", (code) => {
+ for (const url of [
+ `https://example.test/v1?${code}`,
+ `https://example.test/v1#${code}`,
+ ]) {
+ const detail = `error sending request for url (${url})`;
+ const message = skillCommandFailure("validate", 7, detail);
+ expect(message).toContain(detail);
+ expect(message).toContain("could not connect");
+ expect(message).not.toContain("Authentication failed");
+ expect(message).not.toContain("selected model is unavailable");
+ expect(message).not.toContain("rate limited");
+ }
+ });
+
+ test("retains raw diagnostic lines until the terminal boundary", () => {
+ const detail = "raw detail \u001b[31m\rnext\nline café C1 \u009b2J";
+ expect(skillCommandFailure("validate", 7, detail)).toContain(detail);
+ });
+
test("keeps unknown credential failures neutral", () => {
for (const authentication of [
null,
@@ -1658,11 +1997,11 @@ process.stdout.write(JSON.stringify({
{
source:
'process.stderr.write("/private/repository sk-proj-SYNTHETIC_SECRET\\n");' +
- 'process.stdout.write(JSON.stringify({type:"turn.failed",error:{message:"401 sk-proj-SYNTHETIC_SECRET"}})+"\\n");' +
+ 'process.stdout.write(JSON.stringify({type:"turn.failed",error:{message:"HTTP 401 sk-proj-SYNTHETIC_SECRET"}})+"\\n");' +
"process.exitCode=7",
status: 7,
stdout: "",
- stderr: "401 sk-proj-SYNTHETIC_SECRET",
+ stderr: "HTTP 401 sk-proj-SYNTHETIC_SECRET",
},
{
source:
@@ -1713,6 +2052,59 @@ process.stdout.write(JSON.stringify({
}
});
+ test.each(["turn.failed", "stderr"])(
+ "escapes C1 controls in child %s failures while preserving diagnostic text",
+ async (channel) => {
+ const controls = String.fromCharCode(
+ ...Array.from({ length: 33 }, (_, index) => 0x7f + index),
+ );
+ const prefix = "Synthetic failure token=SYNTHETIC_VALUE Café 🔒 ";
+ const suffix = " retained detail";
+ const detail = `${prefix}${controls}${suffix}`;
+ const source =
+ channel === "turn.failed"
+ ? `process.stdout.write(JSON.stringify({type:"turn.failed",error:{message:${JSON.stringify(detail)}}})+"\\n");process.exitCode=7`
+ : `process.stderr.write(${JSON.stringify(detail)});process.exitCode=7`;
+ const stdout = capture();
+ const stderr = capture();
+
+ expect(
+ await runCodexSkillCommand(
+ ["-e", source],
+ { command: "validate", stdout: stdout.stream, stderr: stderr.stream },
+ { command: process.execPath },
+ { PATH: process.env["PATH"] },
+ ),
+ ).toBe(7);
+ expect(stdout.text()).toBe("");
+ expect(stderr.text()).toBe(
+ `codex-security: validate failed with exit code 7.\n${prefix}${" ".repeat(33)}${suffix}\n`,
+ );
+ },
+ );
+
+ test.each(["validate", "patch", "verify-fix"] as const)(
+ "preserves token-per-minute advice from a %s child failure",
+ async (command) => {
+ const detail = "tokens per minute limit exceeded token=SYNTHETIC_VALUE";
+ const source = `process.stdout.write(JSON.stringify({type:"turn.failed",error:{message:${JSON.stringify(detail)}}})+"\\n");process.exitCode=7`;
+ const stdout = capture();
+ const stderr = capture();
+
+ expect(
+ await runCodexSkillCommand(
+ ["-e", source],
+ { command, stdout: stdout.stream, stderr: stderr.stream },
+ { command: process.execPath },
+ { PATH: process.env["PATH"] },
+ ),
+ ).toBe(7);
+ expect(stdout.text()).toBe("");
+ expect(stderr.text()).toContain("Wait and retry.");
+ expect(stderr.text()).toContain(detail);
+ },
+ );
+
test("runs patching in a saved app-server thread", async () => {
const source = `
const assert = require("node:assert/strict");
@@ -1947,7 +2339,7 @@ lines.on("line", (line) => {
'const readline=require("node:readline");',
"const lines=readline.createInterface({input:process.stdin});",
"lines.once('line',()=>process.stdout.write(JSON.stringify({",
- 'id:1,error:{code:-1,message:"401 sk-proj-SYNTHETIC_SECRET /private/repository"}',
+ 'id:1,error:{code:-1,message:"HTTP 401 sk-proj-SYNTHETIC_SECRET /private/repository"}',
'})+"\\n"));',
].join("");
const stdout = capture();
@@ -1972,7 +2364,7 @@ lines.on("line", (line) => {
expect(stdout.text()).toBe("");
expect(stderr.text()).toContain("Authentication failed");
expect(stderr.text()).toContain(
- "401 sk-proj-SYNTHETIC_SECRET /private/repository",
+ "HTTP 401 sk-proj-SYNTHETIC_SECRET /private/repository",
);
});
diff --git a/sdk/typescript/tests-ts/cli-workbench.test.ts b/sdk/typescript/tests-ts/cli-workbench.test.ts
index 45bcab461..386b49a6c 100644
--- a/sdk/typescript/tests-ts/cli-workbench.test.ts
+++ b/sdk/typescript/tests-ts/cli-workbench.test.ts
@@ -1742,7 +1742,7 @@ describe("CLI workbench", () => {
expect(
await runCapturedCli(
main,
- ["scans", "rerun", "scan-original"],
+ ["scans", "rerun", "scan-orig"],
dependencies({
onConfig,
onTurn,
diff --git a/sdk/typescript/tests-ts/cli.test.ts b/sdk/typescript/tests-ts/cli.test.ts
index 5b83ed11e..f1799aa36 100644
--- a/sdk/typescript/tests-ts/cli.test.ts
+++ b/sdk/typescript/tests-ts/cli.test.ts
@@ -2192,7 +2192,7 @@ describe("CLI", () => {
expect(
await runCli(
- ["info", "--json", "--filter-output", "model,reasoningEffort,nextStep"],
+ ["info", "--json", "--filter-output=model,reasoningEffort,nextStep"],
deps,
),
).toBe(0);
@@ -3201,6 +3201,7 @@ describe("CLI", () => {
{ model: "gpt-5.6-terra", model_reasoning_effort: "high" },
],
[["--codex", 'model="gpt-5.6-terra"'], { model: "gpt-5.6-terra" }],
+ [["--codex", 'model = "gpt-5.6-terra"'], { model: "gpt-5.6-terra" }],
[
["--codex", 'model_reasoning_effort="high"'],
{ model_reasoning_effort: "high" },
@@ -5161,6 +5162,7 @@ describe("CLI", () => {
["--token-limit", "4"],
["--token-offset", "1"],
["--token-offset", "1", "--token-limit", "4"],
+ ["--token-offset=1", "--token-limit=4"],
] as const) {
const stdout = captureCli(main, "stdout");
expect(await stdout.run(["scan", ...arguments_], dependencies())).toBe(0);
diff --git a/sdk/typescript/tests-ts/cloud-publish.test.ts b/sdk/typescript/tests-ts/cloud-publish.test.ts
index 891398565..42cbde45e 100644
--- a/sdk/typescript/tests-ts/cloud-publish.test.ts
+++ b/sdk/typescript/tests-ts/cloud-publish.test.ts
@@ -767,7 +767,7 @@ describe("Cloud publication", () => {
environment,
fetch: fetchMock,
}),
- ).rejects.toThrow("Cloud publication was not confirmed");
+ ).rejects.toThrow("synthetic-access-token");
expect(fetchMock).toHaveBeenCalledTimes(1);
});
diff --git a/sdk/typescript/tests-ts/codex-review.test.ts b/sdk/typescript/tests-ts/codex-review.test.ts
index b7ed9abba..56937ded2 100644
--- a/sdk/typescript/tests-ts/codex-review.test.ts
+++ b/sdk/typescript/tests-ts/codex-review.test.ts
@@ -119,14 +119,61 @@ test.each(["defaults", "configured", "luna-model", "legacy-profile"] as const)(
},
);
+const transportStatusScenarios = [408, 429, 500, 502, 503, 504, 401].flatMap(
+ (status) =>
+ ["request", "turn"].map((phase) => ({
+ scenario: `transport-${phase}-${status}`,
+ message: `Safety policy: request refused by upstream with ${status}`,
+ phase,
+ retryable: status !== 401,
+ })),
+);
const failureReasons: Record<string, string> = {
+ ...Object.fromEntries(
+ transportStatusScenarios.map(({ scenario, message }) => [
+ scenario,
+ message,
+ ]),
+ ),
+ "transport-null-turn": "Request flagged for possible cybersecurity risk.",
+ "transport-wording-request":
+ "Request flagged for possible cybersecurity risk.",
+ "transport-timeout-turn":
+ "Safety policy: request refused because the service timed out",
+ "service-timeout-text": "Codex did not submit a validated review",
"policy-turn-code": "Request blocked.",
"policy-request-code": "Request blocked.",
"policy-turn": "Request flagged for possible cybersecurity risk.",
+ "policy-accordance-turn":
+ "This request was refused in accordance with the safety policy.",
+ "policy-terse-turn": "Refused due to the safety policy",
"policy-request": "Request rejected: cyber_policy.",
"refusal-text": "I'm sorry, but I can't assist with that request.",
+ "policy-causal-refusal-text":
+ "This request was refused because it violates the safety policy.",
+ "policy-refusal-text":
+ "This request was refused because of the safety policy.",
+ "policy-first-refusal-text": "Cybersecurity policy: this request is refused.",
+ "policy-colon-refusal-text": "Request refused: safety policy.",
+ "policy-review-text": "Review refused: safety policy.",
+ "policy-suffix-explanation-text":
+ "Request refused: safety policy. I can offer a defensive alternative.",
+ "policy-colon-explanation-text":
+ "Safety policy: request refused due to disallowed content.",
+ "policy-harmful-content-text":
+ "Safety policy: this request is refused due to harmful content.",
+ "policy-bare-harmful-content-text":
+ "Safety policy: request refused due to harmful content.",
+ "policy-restrictions-text":
+ "Safety policy: this request is refused due to policy restrictions.",
+ "policy-bare-restrictions-text":
+ "Safety policy: request refused due to policy restrictions.",
+ "policy-service-impact-text":
+ "Safety policy: this request is refused because it would make the service unavailable.",
+ "policy-multiline-explanation-text":
+ "Safety policy: this request is refused.\nI can offer a defensive alternative.",
"policy-reported-error":
- "Required review check could not be completed: Request refused due to cybersecurity policy violation.",
+ "Required review check could not be completed: Request refusal due to the safety policy.",
"text-only": "Codex did not submit a validated review",
"failed-turn": "Rate limit exceeded",
"rate-limit-turn": "Rate limit reached",
@@ -139,6 +186,20 @@ const failureReasons: Record<string, string> = {
"credential-error": "Authentication failed: Bearer synthetic-review-key",
"invalid-json": "Codex returned malformed JSON",
"invalid-submission": "Review validation failed: Invalid decision",
+ "required-source-error-policy-file":
+ "Required review check could not be completed: Connection refused while fetching content-policy.ts",
+ "required-source-error-upload-policy":
+ "Required review check could not be completed: Artifact upload was blocked by the content policy.",
+ "required-source-error-upload-content-policy-violation":
+ "Required review check could not be completed: Artifact upload failed due to content policy violation",
+ "required-source-error-upload-safety-policy-violation":
+ "Required review check could not be completed: Artifact upload failed due to safety policy violation",
+ "required-source-error-publication-policy":
+ "Required review check could not be completed: Artifact publication was refused due to the content policy.",
+ "required-source-error-publication-refusal":
+ "Required review check could not be completed: Artifact publication refusal due to the content policy.",
+ "required-source-error-policy-requires-refusal":
+ "Required review check could not be completed: Artifact upload failed because the content policy requires refusal of unsupported media",
"required-source-error":
"Required review check could not be completed: Required source revision could not be read.",
"required-source-error-after-verdict":
@@ -152,6 +213,13 @@ const failureReasons: Record<string, string> = {
"Permission profile synthetic_profile was rejected: café 🔒\n",
};
const retriedFailures = new Set([
+ ...transportStatusScenarios
+ .filter(({ retryable }) => retryable)
+ .map(({ scenario }) => scenario),
+ "transport-null-turn",
+ "transport-wording-request",
+ "transport-timeout-turn",
+ "service-timeout-text",
"text-only",
"failed-turn",
"rate-limit-turn",
@@ -173,8 +241,15 @@ const recoveredScenarios: Record<string, string> = {
"recover-no-submission": "text-only",
};
const modelFailures = new Set([
+ ...transportStatusScenarios
+ .filter(({ phase }) => phase === "turn")
+ .map(({ scenario }) => scenario),
+ "transport-null-turn",
+ "transport-timeout-turn",
"policy-turn-code",
"policy-turn",
+ "policy-accordance-turn",
+ "policy-terse-turn",
"failed-turn",
"rate-limit-turn",
"server-error",
@@ -499,7 +574,7 @@ for (const {
? "refusal"
: scenario === "invalid-submission"
? "validation"
- : scenario === "text-only"
+ : ["text-only", "service-timeout-text"].includes(scenario)
? "no-submission"
: modelFailures.has(scenario) || reportsBlocker
? "model"
@@ -508,6 +583,7 @@ for (const {
([
"invalid-submission",
"text-only",
+ "service-timeout-text",
"required-source-error-after-text",
].includes(scenario)
? 2
@@ -516,13 +592,15 @@ for (const {
? "The model refused the deduplication review."
: scenario === "invalid-submission"
? "The submitted review failed semantic validation."
- : scenario === "text-only"
+ : ["text-only", "service-timeout-text"].includes(scenario)
? "Codex did not submit a validated review."
: modelFailures.has(scenario)
? "Codex review turn failed."
: reportsBlocker
? "A required review check could not be completed."
- : ["request-error", "credential-error"].includes(scenario)
+ : scenario.startsWith("transport-request-") ||
+ scenario === "transport-wording-request" ||
+ ["request-error", "credential-error"].includes(scenario)
? "Codex rejected the review request."
: "Codex review transport failed.",
});
@@ -620,6 +698,7 @@ for (const {
: ([
"invalid-submission",
"text-only",
+ "service-timeout-text",
"retry-correction",
"text-only-correction",
"cancel-continuation",
@@ -627,6 +706,10 @@ for (const {
].includes(scenario)
? 2
: [
+ ...transportStatusScenarios
+ .filter(({ phase }) => phase === "request")
+ .map(({ scenario }) => scenario),
+ "transport-wording-request",
"request-error",
"credential-error",
"policy-request",
@@ -867,19 +950,82 @@ test.each(["capture", "throw", "reject", "pending"])(
test.each([
"cyber_policy",
"Request blocked by a safety policy violation.",
+ "This request was blocked by the content policy.",
+ "The review has been blocked under the safety policy.",
+ "Review blocked because it violates the safety policy.",
+ "The review was refused due to the content policy.",
+ "This request was refused under the safety policy",
+ "This request was refused under the content policy",
"Request flagged for potentially high-risk cyber activity.",
"I cannot help with this request.",
"I’m sorry, but I can’t assist with your request.",
+ "Request refused by cybersecurity policy.",
+ "Request refusal due to the safety policy.",
+ "Review refusal due to the content policy.",
+ "Refusal due to the content policy.",
+ "Refused due to the safety policy",
+ "This request was refused because of the safety policy.",
+ "This request was refused in accordance with the safety policy.",
+ "This request was refused because it violates the safety policy.",
+ "The content policy requires refusal of this request.",
+ "The content policy requires refusal of review.",
+ "The safety policy requires a refusal of your review.",
+ "Under the safety policy, this request is refused.",
+ "Cybersecurity policy: this request is refused.",
+ "Content policy: your request has been blocked.",
+ "Safety policy: the request was refused.",
+ "Request refused: safety policy.",
+ "Review refused: safety policy.",
+ "Request refused: safety policy. I can offer a defensive alternative.",
+ "Request refused: safety policy! Please ask for defensive guidance instead.",
+ "Safety policy: request refused.",
+ "Safety policy: request was refused.",
+ "Safety policy: request refused due to disallowed content.",
+ "Safety policy: request refused due to potentially harmful content.",
+ "Safety policy: request refused due to harmful assistance.",
+ "Safety policy: this review is refused because complying would violate applicable restrictions.",
+ "Safety policy: request refused under the applicable policy restrictions. I can offer a defensive alternative.",
+ "Safety policy: request refused under the applicable restrictions.",
+ "Safety policy: request refused because it would facilitate harm.",
+ "This request was refused for violating the content policy.",
"I can't help analyze or merge these vulnerability reports.",
"I'm unable to assist with this comparison.",
+ "I'm not able to help with that.",
+ "Safety policy: this request is refused because it would make the service time out.",
])("recognizes explicit review refusals: %s", (message) => {
expect(isReviewRefusal(message)).toBe(true);
});
+test.each([
+ "httpConnectionFailed",
+ "responseStreamConnectionFailed",
+ "responseStreamDisconnected",
+ "responseTooManyFailedAttempts",
+])("preserves structured transport failure semantics: %s", (variant) => {
+ for (const status of [null, 401, 502]) {
+ const info = { [variant]: { httpStatusCode: status } };
+ expect(
+ isReviewRefusal("Request flagged for possible cybersecurity risk.", info),
+ ).toBe(false);
+ expect(
+ isReviewRefusal(
+ "Safety policy: request refused because the service timed out",
+ info,
+ ),
+ ).toBe(false);
+ }
+});
+
test.each(["cyberPolicy", "misalignmentPolicyViolation"])(
"recognizes structured policy refusal codes independently of message wording: %s",
(code) => {
expect(isReviewRefusal("Request blocked.", code)).toBe(true);
+ expect(
+ isReviewRefusal(
+ "Safety policy: request refused because the service timed out",
+ code,
+ ),
+ ).toBe(true);
expect(isReviewRefusal("Request blocked.", "unauthorized")).toBe(false);
},
);
@@ -890,6 +1036,34 @@ test.each([
"Required source revision could not be read.",
"I cannot complete the review because the source is unavailable.",
"Connection refused",
+ "Connection refused: safety policy service unavailable",
+ "Request refused: safety policy service unavailable",
+ "Review refused: safety policy service unavailable",
+ "Request refused: safety policy. Service unavailable.",
+ "Review refused: safety policy. Service unavailable.",
+ "Safety policy: request refused because the service timed out",
+ "Safety policy: request refused due to upstream service timeout.",
+ "Safety policy: request refused by upstream with 401",
+ "Safety policy: request refused by upstream with 502",
+ "Safety policy: request refused by upstream with 504",
+ "Safety policy: request refused by upstream with 503",
+ "Safety policy: request refused because the upstream returned HTTP 503",
+ "Safety policy: request refused. Service unavailable.",
+ "Safety policy: this request is refused.\nService unavailable.",
+ "Safety policy: request refused due to upstream service unavailability.",
+ "Safety policy: request refused due to upstream service unavailability for content processing.",
+ "Safety policy: request refused due to upstream service unavailability while fetching content.",
+ "Connection refused while fetching content-policy.ts",
+ "Artifact upload was blocked by the content policy.",
+ "Artifact upload failed due to content policy violation",
+ "Artifact upload failed due to safety policy violation",
+ "Artifact publication was refused due to the content policy.",
+ "Artifact publication refusal due to the content policy.",
+ "Artifact upload failed because the content policy requires refusal of unsupported media",
+ "The review failed because the content policy requires refusal of unsupported media",
+ "git fetch was refused by the remote for the cybersecurity-platform repository",
+ "Cybersecurity policy: this request is invalid.",
+ "Content policy: connection refused while fetching the source.",
"Here is the review JSON.",
])("does not turn other failures into refused reviews: %s", (message) => {
expect(isReviewRefusal(message)).toBe(false);
diff --git a/sdk/typescript/tests-ts/component-scan.test.ts b/sdk/typescript/tests-ts/component-scan.test.ts
index 8426f00c9..7bc8f1d7a 100644
--- a/sdk/typescript/tests-ts/component-scan.test.ts
+++ b/sdk/typescript/tests-ts/component-scan.test.ts
@@ -2298,6 +2298,12 @@ test("CLI rejects ambiguous component selection", async () => {
}
});
+test("explains the component selector requirement when neither is set", async () => {
+ const paths = await fixture();
+ await expect(scan(paths, { components: undefined })).rejects.toThrow(
+ "Choose exactly one",
+ );
+});
(process.platform === "win32" ? test.skip : test)(
"component inventory excludes checkout-local Git when planning a subdirectory",
async () => {
diff --git a/sdk/typescript/tests-ts/contract.test.ts b/sdk/typescript/tests-ts/contract.test.ts
index a391e873e..7bd9ac95e 100644
--- a/sdk/typescript/tests-ts/contract.test.ts
+++ b/sdk/typescript/tests-ts/contract.test.ts
@@ -279,7 +279,9 @@ describe("canonical scan contract", () => {
const reading = readScanFile(scanDir, "report.md", "scan report");
await expect(reading).rejects.toBeInstanceOf(ContractValidationError);
await expect(reading).rejects.toMatchObject({
- message: "scan report: unable to open the checked regular file.",
+ message: expect.stringContaining(
+ "scan report: unable to open the checked regular file.",
+ ),
cause: expect.objectContaining({ code: "EACCES", path }),
});
},
diff --git a/sdk/typescript/tests-ts/fixtures/codex-review.mjs b/sdk/typescript/tests-ts/fixtures/codex-review.mjs
index 3a404d21a..626a8dea9 100644
--- a/sdk/typescript/tests-ts/fixtures/codex-review.mjs
+++ b/sdk/typescript/tests-ts/fixtures/codex-review.mjs
@@ -4,14 +4,49 @@ import { createInterface } from "node:readline";
const [scenario, transcript, checkout, stage = "pair-review"] =
process.argv.slice(2);
+const transportStatus = /^transport-(?:request|turn)-(\d+)$/.exec(
+ scenario,
+)?.[1];
+const transportFailure = transportStatus
+ ? {
+ message: `Safety policy: request refused by upstream with ${transportStatus}`,
+ codexErrorInfo: {
+ httpConnectionFailed: { httpStatusCode: Number(transportStatus) },
+ },
+ }
+ : scenario === "transport-wording-request"
+ ? {
+ message: "Request flagged for possible cybersecurity risk.",
+ codexErrorInfo: { httpConnectionFailed: { httpStatusCode: 503 } },
+ }
+ : undefined;
const turnFailures = {
+ ...(scenario.startsWith("transport-turn-")
+ ? { [scenario]: transportFailure }
+ : {}),
+ "transport-null-turn": {
+ message: "Request flagged for possible cybersecurity risk.",
+ codexErrorInfo: { responseStreamDisconnected: { httpStatusCode: null } },
+ },
+ "transport-timeout-turn": {
+ message: "Safety policy: request refused because the service timed out",
+ codexErrorInfo: { httpConnectionFailed: { httpStatusCode: 504 } },
+ },
"policy-turn-code": {
message: "Request blocked.",
codexErrorInfo: "cyberPolicy",
},
"policy-turn": {
message: "Request flagged for possible cybersecurity risk.",
- codexErrorInfo: { httpConnectionFailed: { httpStatusCode: 503 } },
+ codexErrorInfo: "other",
+ },
+ "policy-accordance-turn": {
+ message: "This request was refused in accordance with the safety policy.",
+ codexErrorInfo: "other",
+ },
+ "policy-terse-turn": {
+ message: "Refused due to the safety policy",
+ codexErrorInfo: "other",
},
"failed-turn": {
message: "Rate limit exceeded",
@@ -78,6 +113,17 @@ for await (const line of createInterface({ input: process.stdin })) {
assert.equal(message.params.apiKey, "synthetic-review-key");
send({ id: message.id, result: { type: "apiKey" } });
} else if (message.method === "thread/start") {
+ if (transportFailure && !scenario.startsWith("transport-turn-")) {
+ send({
+ id: message.id,
+ error: {
+ code: -32000,
+ message: transportFailure.message,
+ data: { codexErrorInfo: transportFailure.codexErrorInfo },
+ },
+ });
+ continue;
+ }
if (
[
"request-error",
@@ -231,7 +277,21 @@ for await (const line of createInterface({ input: process.stdin })) {
submit("invalid", { decision: "UNKNOWN" });
} else if (
scenario === "text-only" ||
+ scenario === "service-timeout-text" ||
scenario === "refusal-text" ||
+ scenario === "policy-causal-refusal-text" ||
+ scenario === "policy-refusal-text" ||
+ scenario === "policy-first-refusal-text" ||
+ scenario === "policy-colon-refusal-text" ||
+ scenario === "policy-review-text" ||
+ scenario === "policy-suffix-explanation-text" ||
+ scenario === "policy-colon-explanation-text" ||
+ scenario === "policy-harmful-content-text" ||
+ scenario === "policy-bare-harmful-content-text" ||
+ scenario === "policy-restrictions-text" ||
+ scenario === "policy-bare-restrictions-text" ||
+ scenario === "policy-service-impact-text" ||
+ scenario === "policy-multiline-explanation-text" ||
([
"text-only-correction",
"cancel-continuation",
@@ -247,9 +307,38 @@ for await (const line of createInterface({ input: process.stdin })) {
item: {
type: "agentMessage",
text:
- scenario === "refusal-text"
- ? "I'm sorry, but I can't assist with that request."
- : '{"decision":"SAME"}',
+ scenario === "service-timeout-text"
+ ? "Safety policy: request refused because the service timed out"
+ : scenario === "policy-multiline-explanation-text"
+ ? "Safety policy: this request is refused.\nI can offer a defensive alternative."
+ : scenario === "policy-restrictions-text"
+ ? "Safety policy: this request is refused due to policy restrictions."
+ : scenario === "policy-bare-restrictions-text"
+ ? "Safety policy: request refused due to policy restrictions."
+ : scenario === "policy-service-impact-text"
+ ? "Safety policy: this request is refused because it would make the service unavailable."
+ : scenario === "policy-harmful-content-text"
+ ? "Safety policy: this request is refused due to harmful content."
+ : scenario === "policy-bare-harmful-content-text"
+ ? "Safety policy: request refused due to harmful content."
+ : scenario === "policy-review-text"
+ ? "Review refused: safety policy."
+ : scenario === "policy-suffix-explanation-text"
+ ? "Request refused: safety policy. I can offer a defensive alternative."
+ : scenario === "policy-colon-refusal-text"
+ ? "Request refused: safety policy."
+ : scenario === "policy-colon-explanation-text"
+ ? "Safety policy: request refused due to disallowed content."
+ : scenario === "refusal-text"
+ ? "I'm sorry, but I can't assist with that request."
+ : scenario === "policy-first-refusal-text"
+ ? "Cybersecurity policy: this request is refused."
+ : scenario ===
+ "policy-causal-refusal-text"
+ ? "This request was refused because it violates the safety policy."
+ : scenario === "policy-refusal-text"
+ ? "This request was refused because of the safety policy."
+ : '{"decision":"SAME"}',
},
},
});
@@ -279,8 +368,25 @@ for await (const line of createInterface({ input: process.stdin })) {
{
reason:
scenario === "policy-reported-error"
- ? "Request refused due to cybersecurity policy violation."
- : "Required source revision could not be read.",
+ ? "Request refusal due to the safety policy."
+ : scenario === "required-source-error-policy-file"
+ ? "Connection refused while fetching content-policy.ts"
+ : scenario === "required-source-error-upload-policy"
+ ? "Artifact upload was blocked by the content policy."
+ : scenario === "required-source-error-publication-policy"
+ ? "Artifact publication was refused due to the content policy."
+ : scenario === "required-source-error-publication-refusal"
+ ? "Artifact publication refusal due to the content policy."
+ : scenario ===
+ "required-source-error-policy-requires-refusal"
+ ? "Artifact upload failed because the content policy requires refusal of unsupported media"
+ : scenario ===
+ "required-source-error-upload-content-policy-violation"
+ ? "Artifact upload failed due to content policy violation"
+ : scenario ===
+ "required-source-error-upload-safety-policy-violation"
+ ? "Artifact upload failed due to safety policy violation"
+ : "Required source revision could not be read.",
},
{ tool: "submit_error" },
);
diff --git a/sdk/typescript/tests-ts/inventory-input.test.ts b/sdk/typescript/tests-ts/inventory-input.test.ts
index 5bebcd0c3..173c84cbe 100644
--- a/sdk/typescript/tests-ts/inventory-input.test.ts
+++ b/sdk/typescript/tests-ts/inventory-input.test.ts
@@ -1173,6 +1173,18 @@ require("node:module").syncBuiltinESMExports();
},
);
+test("diff ranking retains Git diagnostics and escapes terminal controls", () => {
+ const f = fixture();
+ f.write("source");
+ f.commit();
+ const revision = "missing-café-\u009b\u2028\u2029";
+ const result = f.run("make-diff-rank-input", ["--base", revision]);
+ expect(result.status).toBe(1);
+ expect(result.stderr).toContain("Git diff failed with exit code");
+ expect(result.stderr).toContain("missing-café-\\u009b\\u2028\\u2029");
+ expect(result.stderr).not.toContain(revision);
+});
+
test("failed inventory generation preserves the existing file", () => {
const f = fixture();
f.write("source");
@@ -1181,6 +1193,14 @@ test("failed inventory generation preserves the existing file", () => {
["--scope", "missing"],
["--scope", ".."],
["--scope", ".", "--diff-base", "missing-revision"],
+ [
+ "--scope",
+ ".",
+ "--diff-base",
+ "missing-revision",
+ "--diff-mode",
+ "local-patch",
+ ],
]) {
expect(f.run("generate-in-scope-files", args).status).toBe(2);
expect(readFileSync(f.out, "utf8")).toBe("previous\n");
diff --git a/sdk/typescript/tests-ts/multiscan.test.ts b/sdk/typescript/tests-ts/multiscan.test.ts
index 2c2771e04..9c8ffc142 100644
--- a/sdk/typescript/tests-ts/multiscan.test.ts
+++ b/sdk/typescript/tests-ts/multiscan.test.ts
@@ -103,7 +103,7 @@ describe("multiscan", () => {
const source = await repository(paths.root, "comma, quoted");
await writeFile(
paths.input,
- `\uFEFF"id","repository","revision","scope","mode","prompt","notes"\r\n"payments","${source.path}","${source.revision}","src","deep","Focus on authentication, authorization.","contains ""quotes"""\r\n\r\n`,
+ `\uFEFF" id "," repository "," revision ","scope","mode","prompt","notes"\r\n"payments","${source.path}","${source.revision}","src","deep","Focus on authentication, authorization.","contains ""quotes"""\r\n\r\n`,
);
const summary = await runMultiscan(
diff --git a/sdk/typescript/tests-ts/publication-check.test.ts b/sdk/typescript/tests-ts/publication-check.test.ts
index 9feb6ab18..5b55bfbbb 100644
--- a/sdk/typescript/tests-ts/publication-check.test.ts
+++ b/sdk/typescript/tests-ts/publication-check.test.ts
@@ -431,19 +431,17 @@ describe("read-only publication preflight", () => {
expect(inspectPublicationStore).not.toHaveBeenCalled();
});
- test("does not echo provider response data on an access failure", async () => {
+ test("preserves provider response details on an access failure", async () => {
const key = "lin_api_SYNTHETIC_PRIVATE_KEY";
const client = readClient([]);
client.team = (() =>
fail(`Provider response included ${key}`)) as ReadClient["team"];
- await expect(
- checkScanPublicationInternal(
- "scan",
- { ...OPTIONS, linearApiKey: key },
- dependencies({ linearClient: () => client }),
- ),
- ).rejects.toThrow(
- "Could not verify Linear team access. Check the API key and publication destination.",
+ const checking = checkScanPublicationInternal(
+ "scan",
+ { ...OPTIONS, linearApiKey: key },
+ dependencies({ linearClient: () => client }),
);
+ await expect(checking).rejects.toThrow(`Provider response included ${key}`);
+ await expect(checking).rejects.toThrow(/API key.*publication destination/u);
});
});
diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.ts
index a7a2a01fc..b5db000ea 100644
--- a/sdk/typescript/tests-ts/runtime.test.ts
+++ b/sdk/typescript/tests-ts/runtime.test.ts
@@ -1,7 +1,10 @@
import { runNodePython } from "./support/python-probe.js";
import { nodeCommand } from "./support/shell.js";
import { scanRegistrationArguments } from "./support/workbench-command.js";
-import { createApiTestFixtures } from "./support/temporary-directories.js";
+import {
+ createApiTestFixtures,
+ removeTemporaryDirectory,
+} from "./support/temporary-directories.js";
import { parseJsonLines, jsonLines } from "./support/json.js";
import { execFile, spawnSync } from "node:child_process";
import { Codex } from "@openai/codex-sdk";
@@ -2853,6 +2856,7 @@ for await (const line of createInterface({ input: process.stdin })) {
stderr: "",
});
}
+ await removeTemporaryDirectory(root);
},
);
diff --git a/sdk/typescript/tests-ts/sandbox-probe.test.ts b/sdk/typescript/tests-ts/sandbox-probe.test.ts
index 357378dfe..578a1cdd8 100644
--- a/sdk/typescript/tests-ts/sandbox-probe.test.ts
+++ b/sdk/typescript/tests-ts/sandbox-probe.test.ts
@@ -1,6 +1,6 @@
import { chmod, mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
-import { afterEach, describe, expect, test } from "bun:test";
+import { afterEach, describe, expect, test, spyOn } from "bun:test";
import { SandboxUnavailableError } from "../src/index.js";
import { probeCodexSandbox } from "../src/runtime.js";
import { TestClient } from "./support/api-client.js";
@@ -46,6 +46,31 @@ async function syntheticCodex(root: string, exitCode: number): Promise<string> {
}
describe("Codex sandbox probe", () => {
+ test.skipIf(process.platform === "win32")(
+ "reports its timeout without sandbox-denial advice",
+ async () => {
+ const timeout = spyOn(AbortSignal, "timeout").mockReturnValue(
+ AbortSignal.abort(
+ new DOMException("Synthetic timeout", "TimeoutError"),
+ ),
+ );
+ try {
+ const root = await temporaryDirectory();
+ const command = await syntheticCodex(root, 0);
+ const failure = await probeCodexSandbox({ command }, {}).then(
+ () => null,
+ (error: Error) => error,
+ );
+ expect(failure).toBeInstanceOf(SandboxUnavailableError);
+ expect(failure?.cause).toBeDefined();
+ expect(failure?.message).toContain("timed out");
+ expect(failure?.message).not.toContain("user namespaces");
+ } finally {
+ timeout.mockRestore();
+ }
+ },
+ );
+
test.skipIf(process.platform === "win32")(
"reports what the sandbox said and how to reproduce it",
async () => {