OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
refactor(plugin): port deep-review input to TypeScript (#839)
Kyle Brownae4fff7b2e83
diff --git a/plugins/codex-security/mcp-app/helpers-main.ts b/plugins/codex-security/mcp-app/helpers-main.tsindex 8aad95453..63920a951 100644--- a/plugins/codex-security/mcp-app/helpers-main.ts+++ b/plugins/codex-security/mcp-app/helpers-main.ts@@ -4,6 +4,7 @@ import { decodePosixBytes } from "./src/helpers/posix-path";import { windowsBinding } from "./src/native";import { normalizeCandidatesCommand } from "./src/helpers/normalize-candidates";import { validatePatchRiskAssessmentCommand } from "./src/helpers/validate-patch-risk-assessment";+import { deepReviewInputCommand } from "./src/helpers/deep-review-input";let commandLine = process.argv.slice(2);if (process.platform === "win32") {@@ -35,9 +36,14 @@ if (command === "resolve-security-md") {process.exitCode = normalizeCandidatesCommand(args, posixHome);} else if (command === "validate-patch-risk-assessment") {process.exitCode = validatePatchRiskAssessmentCommand(args);+} else if (+ command === "copy-deep-review-input" ||+ command === "select-deep-review-input"+) {+ process.exitCode = deepReviewInputCommand(command, args, posixHome);} else {console.error(- "Usage: launch_codex_security_mcp[.cmd] --helper <resolve-security-md | normalize-candidates | validate-patch-risk-assessment> [options]",+ "Usage: launch_codex_security_mcp[.cmd] --helper <resolve-security-md | normalize-candidates | validate-patch-risk-assessment | copy-deep-review-input | select-deep-review-input> [options]",);process.exitCode = 2;}diff --git a/plugins/codex-security/mcp-app/src/helpers/deep-review-input.ts b/plugins/codex-security/mcp-app/src/helpers/deep-review-input.tsnew file mode 100644index 000000000..cb04e62ec--- /dev/null+++ b/plugins/codex-security/mcp-app/src/helpers/deep-review-input.ts@@ -0,0 +1,65 @@+import {+ argumentsFor,+ compare,+ loadRankRows,+ print,+ reportCommandError,+ requireUniquePaths,+ worklistPath,+ writeRankRows,+} from "./rank-worklists";++type Command = "copy-deep-review-input" | "select-deep-review-input";++export function deepReviewInputCommand(+ command: Command,+ args: string[],+ posixHome = process.env.HOME,+): number {+ const selection = command === "select-deep-review-input";+ const input = selection ? "rank-output" : "rank-input";+ const usage = `usage: launch_codex_security_mcp[.cmd] --helper ${command} [-h] --${input} PATH --out PATH${selection ? " [--top-percent INT]" : ""}`;+ try {+ const values = argumentsFor(+ args,+ [input, "out"],+ selection ? ["top-percent"] : [],+ );+ if (values.help) {+ print(+ `${usage}\n\nCreate deep_review_input.jsonl from ${selection ? "worker-produced rank_output.jsonl" : "rank_input.jsonl"}.\n\noptions:\n -h, --help show this help message and exit\n --${input} PATH ${selection ? "Worker ranking output" : "Deterministic rank input"} JSONL.\n --out PATH Output deep_review_input.jsonl path.${selection ? "\n --top-percent INT Percent of included files to keep for deep review. Defaults to 100." : ""}`,+ );+ return 0;+ }+ const path = (name: string) =>+ worklistPath(values[name] as string, posixHome);+ const rows = loadRankRows(path(input), selection);+ requireUniquePaths(rows, selection ? "Rank output" : "Rank input");+ let selected = rows,+ total = rows.length;+ if (selection) {+ const included = rows.filter((row) => row.include);+ const base = included.length ? included : rows;+ base.sort(+ (left, right) =>+ right.score! - left.score! || compare(left.path, right.path),+ );+ total = base.length;+ const percent = (values["top-percent"] ?? 100) as number;+ const keep = total ? Math.max(1, Math.trunc(total * (percent / 100))) : 0;+ selected = base.slice(0, keep);+ }+ const output = path("out");+ writeRankRows(+ output,+ selected.map(({ path, area }) => ({ path, area })),+ );+ const message = selection+ ? `Selected ${selected.length} of ${total} rows into ${output}`+ : `Copied ${selected.length} rows into ${output}`;+ print(message);+ return 0;+ } catch (error) {+ return reportCommandError(error, command, usage);+ }+}diff --git a/plugins/codex-security/mcp-app/src/helpers/helper-files.ts b/plugins/codex-security/mcp-app/src/helpers/helper-files.tsindex ce6b4fff9..5144bdd7d 100644--- a/plugins/codex-security/mcp-app/src/helpers/helper-files.ts+++ b/plugins/codex-security/mcp-app/src/helpers/helper-files.ts@@ -1,9 +1,17 @@-import { readFileSync } from "node:fs";+import {+ closeSync,+ mkdirSync,+ openSync,+ readFileSync,+ readSync,+ writeFileSync,+} from "node:fs";import { windowsBinding } from "../native";import { widePath, windowsFileSystem } from "../../../native/windows-files.mjs";import { encodePosixPath } from "./posix-path";import { escapeControls } from "./json";import { parsedPath } from "./resolve-security-md";+import { decodeUtf8 } from "./utf8";export function filesystemErrorMessage(error: unknown): string {const message = error instanceof Error ? error.message : String(error);@@ -31,9 +39,92 @@ export function normalizePath(path: string): string {);}-export function readFile(path: string | number): Buffer {+function readError(error: unknown, path: string, label?: string): never {+ const { code, winerror } = error as NodeJS.ErrnoException & {+ winerror?: number;+ };+ // Preserve pathlib.exists() diagnostics only for callers with missing-file labels.+ if (+ label !== undefined &&+ (["ENOENT", "ENOTDIR", "ELOOP"].includes(code ?? "") ||+ winerror === 21 ||+ winerror === 123)+ )+ throw new Error(`${label} missing: ${path}`);+ throw error;+}++export function readFile(path: string | number, label?: string): Buffer {if (typeof path === "number") return readFileSync(path);- return process.platform === "win32"- ? windowsFileSystem(windowsBinding()).readFile(widePath(path))- : readFileSync(encodePosixPath(path));+ try {+ return process.platform === "win32"+ ? windowsFileSystem(windowsBinding()).readFile(widePath(path))+ : readFileSync(encodePosixPath(path));+ } catch (error) {+ return readError(error, path, label);+ }+}++function* readChunks(path: string, label?: string): Iterable<Buffer> {+ if (process.platform === "win32") {+ yield readFile(path, label);+ return;+ }+ let descriptor: number | undefined;+ try {+ descriptor = openSync(encodePosixPath(path), "r");+ while (true) {+ const chunk = Buffer.allocUnsafe(64 * 1024);+ const length = readSync(descriptor, chunk, 0, chunk.length, null);+ if (length === 0) return;+ yield chunk.subarray(0, length);+ }+ } catch (error) {+ readError(error, path, label);+ } finally {+ if (descriptor !== undefined) closeSync(descriptor);+ }+}++export function* readUtf8Lines(path: string, label?: string): Iterable<string> {+ let parts: Buffer[] = [];+ let skipLineFeed = false;+ for (const chunk of readChunks(path, label)) {+ let start = skipLineFeed && chunk[0] === 10 ? 1 : 0;+ skipLineFeed = false;+ for (let index = start; index < chunk.length; index++) {+ const byte = chunk[index];+ if (byte !== 10 && byte !== 13) continue;+ parts.push(chunk.subarray(start, index));+ yield decodeUtf8(parts.length === 1 ? parts[0]! : Buffer.concat(parts));+ parts = [];+ if (byte === 13) {+ if (chunk[index + 1] === 10) index++;+ else if (index + 1 === chunk.length) skipLineFeed = true;+ }+ start = index + 1;+ }+ if (start < chunk.length) parts.push(chunk.subarray(start));+ }+ if (parts.length)+ yield decodeUtf8(parts.length === 1 ? parts[0]! : Buffer.concat(parts));+}++export function mkdir(path: string): void {+ if (process.platform === "win32")+ windowsFileSystem(windowsBinding()).mkdir(widePath(path));+ else mkdirSync(encodePosixPath(path), { recursive: true });+}++export function writeFile(path: string, chunks: Iterable<Buffer>): void {+ if (process.platform === "win32") {+ windowsFileSystem(windowsBinding()).writeFile(widePath(path), chunks);+ return;+ }+ const descriptor = openSync(encodePosixPath(path), "w");+ try {+ for (const chunk of chunks) writeFileSync(descriptor, chunk);+ } finally {+ closeSync(descriptor);+ }}diff --git a/plugins/codex-security/mcp-app/src/helpers/rank-worklists.ts b/plugins/codex-security/mcp-app/src/helpers/rank-worklists.tsnew file mode 100644index 000000000..039d9b480--- /dev/null+++ b/plugins/codex-security/mcp-app/src/helpers/rank-worklists.ts@@ -0,0 +1,203 @@+import { dirname } from "node:path";+import { parseArgs } from "node:util";+import {+ filesystemErrorMessage,+ mkdir,+ normalizePath,+ readUtf8Lines,+ writeFile,+} from "./helper-files";+import { encodePosixPath } from "./posix-path";+import { formatDiagnostic, object, parseJson } from "./json";+import { expandHome } from "./resolve-security-md";++export interface RankRow {+ path: string;+ area: string;+ preview?: string;+ reason?: string;+ score?: number;+ include?: boolean;+}+const blank = (value: string) => value.trim().length === 0;+export function compare(left: string, right: string): number {+ const a = Array.from(left, (character) => character.codePointAt(0)!);+ const b = Array.from(right, (character) => character.codePointAt(0)!);+ for (let index = 0; index < Math.min(a.length, b.length); index++) {+ if (a[index] !== b[index]) return a[index]! - b[index]!;+ }+ return a.length - b.length;+}++export function missingNames(+ expected: Iterable<string>,+ actual: Iterable<string>,+): string[] {+ const present = new Set(actual);+ return Array.from(expected)+ .filter((name) => !present.has(name))+ .sort(compare);+}++export function loadRankRows(+ path: string,+ selection: boolean,+ label = selection ? "Rank output" : "Rank input",+): RankRow[] {+ const fields = selection+ ? ["path", "area", "score", "include", "reason"]+ : ["path", "area", "preview"];+ const rows = Array.from(readUtf8Lines(path, label), (line, index) => {+ const fail = (message: string): never => {+ throw new Error(`${path}:${index + 1}: ${message}`);+ };+ if (blank(line)) fail("blank JSONL rows are not allowed");+ let row: unknown;+ try {+ row = parseJson(line);+ } catch (error) {+ if (error instanceof SyntaxError) fail(`invalid JSON: ${error.message}`);+ throw error;+ }+ if (!object(row)) return fail("expected a JSON object");+ const actual = Object.keys(row);+ const missing = missingNames(fields, actual);+ const unexpected = missingNames(actual, fields);+ const details: string[] = [];+ if (missing.length)+ details.push(`missing fields ${formatDiagnostic(missing)}`);+ if (unexpected.length)+ details.push(`unexpected fields ${formatDiagnostic(unexpected)}`);+ if (details.length) fail(details.join("; "));+ for (const field of selection+ ? ["path", "area"]+ : ["path", "area", "preview"]) {+ if (+ typeof row[field] !== "string" ||+ (field === "path" && blank(row[field]))+ )+ fail(+ `${field} must be ${field === "path" ? "a non-empty string" : "a string"}`,+ );+ }+ if (selection) {+ if (typeof row.score !== "number" || !Number.isInteger(row.score))+ fail("score must be an integer from 1 through 10");+ if ((row.score as number) < 1 || (row.score as number) > 10)+ fail("score must be from 1 through 10");+ if (typeof row.include !== "boolean") fail("include must be a boolean");+ if (typeof row.reason !== "string" || blank(row.reason))+ fail("reason must be a non-empty string");+ }+ return row as unknown as RankRow;+ });+ return rows;+}++export function requireUniquePaths(rows: RankRow[], label: string): void {+ const seen = new Set<string>(),+ duplicates = new Set<string>();+ for (const row of rows) {+ if (seen.has(row.path)) duplicates.add(row.path);+ seen.add(row.path);+ }+ if (duplicates.size)+ throw new Error(+ `${label} contains duplicate paths: ${formatDiagnostic([...duplicates].sort(compare))}`,+ );+}++export function writeRankRows(output: string, rows: RankRow[]): void {+ mkdir(dirname(output));+ function* contents(): Iterable<Buffer> {+ for (const row of rows) {+ const json = JSON.stringify(row);+ yield Buffer.from(json + (process.platform === "win32" ? "\r\n" : "\n"));+ }+ }+ writeFile(output, contents());+}++export class ArgumentError extends Error {}+export function argumentsFor(+ args: string[],+ required: readonly string[],+ integerOptions: readonly string[] = [],+): Record<string, string | number | true> {+ try {+ const { values }: { values: Record<string, string | boolean | undefined> } =+ parseArgs({+ args,+ options: {+ ...Object.fromEntries(+ [...required, ...integerOptions].map((name) => [+ name,+ { type: "string" as const },+ ]),+ ),+ help: { type: "boolean", short: "h" },+ },+ });+ if (values.help) return { help: true };+ const missing = required.filter((name) => values[name] === undefined);+ if (missing.length)+ throw new Error(+ `Missing required options: ${missing.map((name) => `--${name}`).join(", ")}`,+ );+ const result: Record<string, string | number | true> = {};+ for (const [name, value] of Object.entries(values)) {+ if (typeof value !== "string") continue;+ if (!integerOptions.includes(name)) result[name] = value;+ else {+ const number = Number(value);+ if (!/^[+-]?\d+$/u.test(value.trim()) || !Number.isInteger(number))+ throw new Error(`--${name} must be an integer`);+ result[name] = number;+ }+ }+ return result;+ } catch (error) {+ throw new ArgumentError(+ error instanceof Error ? error.message : String(error),+ );+ }+}++export function print(message: string, stderr = false): void {+ let text = message + "\n";+ if (stderr)+ text = text.replace(+ /[\ud800-\udfff]/gu,+ (character) =>+ `\\u${character.charCodeAt(0).toString(16).padStart(4, "0")}`,+ );+ (stderr ? process.stderr : process.stdout).write(+ process.platform === "win32"+ ? text.replaceAll("\n", "\r\n")+ : stderr+ ? text+ : encodePosixPath(text),+ );+}++export function worklistPath(+ value: string,+ posixHome: string | undefined,+): string {+ return normalizePath(expandHome(normalizePath(value), posixHome));+}++export function reportCommandError(+ error: unknown,+ command: string,+ usage: string,+): number {+ const message = filesystemErrorMessage(error);+ print(+ error instanceof ArgumentError+ ? `${usage}\n${command}: error: ${message}`+ : message,+ true,+ );+ return error instanceof ArgumentError ? 2 : 1;+}diff --git a/plugins/codex-security/native/README.md b/plugins/codex-security/native/README.mdindex 0a325cd3e..f1ecced8e 100644--- a/plugins/codex-security/native/README.md+++ b/plugins/codex-security/native/README.md@@ -33,7 +33,7 @@ Windows uses `windows-binding.mts` and the same Rust crate. `WindowsHandle` ownsThe binding exposes synchronous file and recursive directory creation, attributes and reparse tags, identity and final/opened names, read/write, and exact-handle rename and deletion. Rust's `File` supplies ordinary I/O. Calls return numeric Windows errors, including 6 for closed handles. Buffer ranges and path encoding are checked before use. Overlapped handles are unsupported because pending operations could retain native buffers beyond the call. Path authorization, ancestor traversal, and reparse-point policy remain the caller's responsibility.-Four additional operations preserve Windows strings at the Node boundary. `windowsArguments` returns the complete OS argument vector, including the executable and Node options, using Rust's CRT-compatible parser. `windowsEnvironment` reads one wide environment name and distinguishes an absent value (`null`) from an empty buffer. `windowsAbsolutePath` resolves against the native current directory and drive directories without requiring the destination to exist. `windowsDirectoryEntries` uses `std::fs::read_dir` and cached `DirEntry::file_type()` values without opening each child; names remain UTF-16LE, and construction or iteration failures return their numeric Windows error and an empty array. Directory symlinks and junctions have both directory and symbolic-link flags. The typed adapter exposes this enumerator through `entriesWithTypes`, which `resolve-security-md --list` uses on Windows. Assessment validation shares the typed wide-path reader.+Four additional operations preserve Windows strings at the Node boundary. `windowsArguments` returns the complete OS argument vector, including the executable and Node options, using Rust's CRT-compatible parser. `windowsEnvironment` reads one wide environment name and distinguishes an absent value (`null`) from an empty buffer. `windowsAbsolutePath` resolves against the native current directory and drive directories without requiring the destination to exist. `windowsDirectoryEntries` uses `std::fs::read_dir` and cached `DirEntry::file_type()` values without opening each child; names remain UTF-16LE, and construction or iteration failures return their numeric Windows error and an empty array. Directory symlinks and junctions have both directory and symbolic-link flags. The typed adapter exposes this enumerator through `entriesWithTypes`, which `resolve-security-md --list` uses on Windows. Assessment validation and deep-review worklists share the typed wide-path reader and writer.`windows-files.mts` uses native absolute and final paths, preserving raw UTF-16 and the returned device prefix. Non-strict `realpath` resolves the nearest existing ancestor of a missing path; dangling links and other access errors remain errors. Callers must check containment independently. It follows native Windows path behavior rather than emulating Python's special cases for device names, whitespace, verbatim dot segments, and prefix removal. `stat(path, false)` retains exact symbolic-link and reparse-point metadata so callers can reject junction traversal independently of the enumerator's link label. The SDK's public runtime floor remains Node 22.13.0. Node 20.0.0 is an additional native-foundation compatibility proof; it does not change the SDK engine requirement.diff --git a/plugins/codex-security/native/examples/windows-wide-launcher.rs b/plugins/codex-security/native/examples/windows-wide-launcher.rsindex 672144e8b..5deefe2cf 100644--- a/plugins/codex-security/native/examples/windows-wide-launcher.rs+++ b/plugins/codex-security/native/examples/windows-wide-launcher.rs@@ -402,8 +402,52 @@ fn main() -> std::io::Result<()> {{return Err(io::Error::other("Assessment validation changed its input"));}+ let worklist_dir = raw("worklist-", 0xdc80);+ let worklist_output = repo.join(&worklist_dir).join(&output_name);+ let worklist_sentinel = repo+ .join(raw("worklist-", 0xfffd))+ .join(&replacement_output);+ fs::create_dir_all(worklist_sentinel.parent().unwrap())?;+ fs::write(&worklist_sentinel, "worklist output sentinel")?;+ for (command, input_flag, row) in [+ (+ "copy-deep-review-input",+ "--rank-input",+ "{\"path\":\"source.py\",\"area\":\"src\",\"preview\":\"source line\"}\n",+ ),+ (+ "select-deep-review-input",+ "--rank-output",+ "{\"path\":\"source.py\",\"area\":\"src\",\"score\":5,\"include\":true,\"reason\":\"source line\"}\n",+ ),+ ] {+ fs::write(repo.join(&input_name), row)?;+ for prefix in [repo.clone(), PathBuf::from("~"), PathBuf::from(".")] {+ let child = invoke(command, &[+ input_flag.into(), prefix.join(&input_name),+ "--out".into(), prefix.join(&worklist_dir).join(&output_name),+ ])?;+ if !child.status.success()+ || !child.stderr.is_empty()+ || fs::read(&worklist_output)? != b"{\"path\":\"source.py\",\"area\":\"src\"}\r\n"+ {+ return Err(io::Error::other(format!(+ "Wide deep-review helper failed: {}",+ String::from_utf8_lossy(&child.stderr)+ )));+ }+ fs::remove_file(&worklist_output)?;+ }+ }+ if fs::read(&worklist_sentinel)? != b"worklist output sentinel"+ || fs::read(repo.join(raw("input-", 0xfffd)))? != b"invalid replacement input"+ {+ return Err(io::Error::other(+ "Deep-review helper changed a replacement path",+ ));+ }println!(- "{{\"policyHelperRawPaths\":true,\"candidateHelperRawPaths\":true,\"assessmentHelperRawPaths\":true,\"directoryIdentity\":true,\"policySymlinkBoundary\":{symlinks}}}"+ "{{\"policyHelperRawPaths\":true,\"candidateHelperRawPaths\":true,\"assessmentHelperRawPaths\":true,\"deepReviewHelperRawPaths\":true,\"directoryIdentity\":true,\"policySymlinkBoundary\":{symlinks}}}");Ok(())}diff --git a/plugins/codex-security/scripts/generate_rank_input.py b/plugins/codex-security/scripts/generate_rank_input.pyindex f6c5a4b33..a096f8d4e 100644--- a/plugins/codex-security/scripts/generate_rank_input.py+++ b/plugins/codex-security/scripts/generate_rank_input.py@@ -17,9 +17,6 @@ This script stays deliberately model-free:coordinator accepts it.- `validate-rank-pool` validates the pool plan and every assigned shard output.- `merge-rank-outputs` validates and combines worker-local shard outputs.-- `copy-deep-review-input` copies every candidate into the deep-review worklist- for exhaustive mode.-- `select-deep-review-input` selects the ranked rows for deep review."""from __future__ import annotations@@ -264,25 +261,6 @@ def parse_args() -> argparse.Namespace:merge.add_argument("--shard-dir", required=True, help="Directory of input and output shards.")merge.add_argument("--out", required=True, help="Output rank_output.jsonl path.")- copy = subparsers.add_parser(- "copy-deep-review-input",- help="Create deep_review_input.jsonl directly from rank_input.jsonl.",- )- copy.add_argument("--rank-input", required=True, help="Deterministic rank input JSONL.")- copy.add_argument("--out", required=True, help="Output deep_review_input.jsonl path.")-- select = subparsers.add_parser(- "select-deep-review-input",- help="Create deep_review_input.jsonl from worker-produced rank_output.jsonl.",- )- select.add_argument("--rank-output", required=True, help="Worker ranking output JSONL.")- select.add_argument("--out", required=True, help="Output deep_review_input.jsonl path.")- select.add_argument(- "--top-percent",- type=int,- default=100,- help="Percent of included files to keep for deep review.",- )return parser.parse_args()@@ -1099,33 +1077,6 @@ def merge_rank_outputs(args: argparse.Namespace) -> None:print(f"Merged {len(merged)} ranking rows into {output}")-def copy_deep_review_input(args: argparse.Namespace) -> None:- rank_input = Path(args.rank_input).expanduser()- rows = load_jsonl(rank_input, "Rank input", validate_rank_input_row)- require_unique_paths(rows, "Rank input")- selected = [{"path": row["path"], "area": row["area"]} for row in rows]-- output = Path(args.out).expanduser()- write_jsonl(output, selected)- print(f"Copied {len(selected)} rows into {output}")---def select_deep_review_input(args: argparse.Namespace) -> None:- rank_output = Path(args.rank_output).expanduser()- rows = load_jsonl(rank_output, "Rank output", validate_rank_output_row)- require_unique_paths(rows, "Rank output")-- included = [row for row in rows if row["include"]]- base_rows = included if included else rows- base_rows.sort(key=lambda row: (-int(row["score"]), str(row["path"])))- keep = max(1, int(len(base_rows) * (args.top_percent / 100.0))) if base_rows else 0- selected = [{"path": row["path"], "area": row["area"]} for row in base_rows[:keep]]-- output = Path(args.out).expanduser()- write_jsonl(output, selected)- print(f"Selected {len(selected)} of {len(base_rows)} rows into {output}")--def main() -> None:args = parse_args()if args.command == "make-repo-rank-input":@@ -1148,10 +1099,6 @@ def main() -> None:validate_rank_pool_command(args)elif args.command == "merge-rank-outputs":merge_rank_outputs(args)- elif args.command == "copy-deep-review-input":- copy_deep_review_input(args)- elif args.command == "select-deep-review-input":- select_deep_review_input(args)else:raise SystemExit(f"Unknown command: {args.command}")diff --git a/plugins/codex-security/skills/finding-discovery/SKILL.md b/plugins/codex-security/skills/finding-discovery/SKILL.mdindex 46d333422..d0794ff3c 100644--- a/plugins/codex-security/skills/finding-discovery/SKILL.md+++ b/plugins/codex-security/skills/finding-discovery/SKILL.md@@ -34,7 +34,7 @@ For a targeted code diff without an existing compact inventory:- Read `../security-scan/references/scan-artifacts-and-ledger.md`.- Generate `rank_input.jsonl` deterministically from changed source-like files with `<python_command> <plugin_dir>/scripts/generate_rank_input.py make-diff-rank-input --repo <repo_root> --base <base> --mode revisions --head <head> --out <discovery_dir>/rank_input.jsonl` for PR, commit, and branch diffs, or `<python_command> <plugin_dir>/scripts/generate_rank_input.py make-diff-rank-input --repo <repo_root> --base <base> --mode local-patch --out <discovery_dir>/rank_input.jsonl` for a local patch.-- Copy every diff row into `deep_review_input.jsonl` with `<python_command> <plugin_dir>/scripts/generate_rank_input.py copy-deep-review-input --rank-input <discovery_dir>/rank_input.jsonl --out <discovery_dir>/deep_review_input.jsonl`. Diff scans do not rank or drop changed files before deep review.+- Copy every diff row into `deep_review_input.jsonl` with `<plugin_dir>/scripts/launch_codex_security_mcp --helper copy-deep-review-input --rank-input <discovery_dir>/rank_input.jsonl --out <discovery_dir>/deep_review_input.jsonl`. On Windows, use the [PowerShell copy command](../security-scan/references/scan-artifacts-and-ledger.md#windows-worklist-copy) to preserve literal paths. Diff scans do not rank or drop changed files before deep review.- Add directly supporting files required to understand the changed security behavior only when repository evidence shows they are needed. Do not use them to broaden into unrelated repository-wide enumeration.- Deep-review every file in `deep_review_input.jsonl` using the shared scoped file-review rules.- Stay anchored to the changed code and directly supporting files. Unchanged siblings are context or negative controls unless the diff newly reaches them, weakens their shared control, or changes a shared sink/helper they depend on.diff --git a/plugins/codex-security/skills/security-scan/references/scan-artifacts-and-ledger.md b/plugins/codex-security/skills/security-scan/references/scan-artifacts-and-ledger.mdindex cd2face07..4680a2f16 100644--- a/plugins/codex-security/skills/security-scan/references/scan-artifacts-and-ledger.md+++ b/plugins/codex-security/skills/security-scan/references/scan-artifacts-and-ledger.md@@ -56,7 +56,7 @@ The parent agent must reconcile validation and attack-path subagent outputs befo## Scoped Deep Review- Use `deep_review_input.jsonl` as the canonical changed-file review worklist for diff scans.-- For diff-scoped scans, generate `rank_input.jsonl` deterministically from changed source-like files with `<python_command> <plugin_dir>/scripts/generate_rank_input.py make-diff-rank-input --repo <repo_root> --base <base> --mode revisions --head <head> --out <discovery_dir>/rank_input.jsonl` for PR, commit, and branch diffs, or `<python_command> <plugin_dir>/scripts/generate_rank_input.py make-diff-rank-input --repo <repo_root> --base <base> --mode local-patch --out <discovery_dir>/rank_input.jsonl` for a local patch, then copy every row into `deep_review_input.jsonl` with `<python_command> <plugin_dir>/scripts/generate_rank_input.py copy-deep-review-input --rank-input <discovery_dir>/rank_input.jsonl --out <discovery_dir>/deep_review_input.jsonl`.+- For diff-scoped scans, generate `rank_input.jsonl` deterministically from changed source-like files with `<python_command> <plugin_dir>/scripts/generate_rank_input.py make-diff-rank-input --repo <repo_root> --base <base> --mode revisions --head <head> --out <discovery_dir>/rank_input.jsonl` for PR, commit, and branch diffs, or `<python_command> <plugin_dir>/scripts/generate_rank_input.py make-diff-rank-input --repo <repo_root> --base <base> --mode local-patch --out <discovery_dir>/rank_input.jsonl` for a local patch, then copy every row into `deep_review_input.jsonl` with `<plugin_dir>/scripts/launch_codex_security_mcp --helper copy-deep-review-input --rank-input <discovery_dir>/rank_input.jsonl --out <discovery_dir>/deep_review_input.jsonl`. On Windows, use the [PowerShell copy command](#windows-worklist-copy) below.- Diff-scoped scans do not rank or drop changed files before deep review. Every row in diff `rank_input.jsonl` must be copied into `deep_review_input.jsonl` and receive a full-file review receipt.- Add directly supporting files required to understand the changed security behavior only when repository evidence shows they are needed; record the add-back reason in the work ledger or per-file result.- Deep-review every file selected into `deep_review_input.jsonl`.@@ -71,6 +71,18 @@ The parent agent must reconcile validation and attack-path subagent outputs befo- Append normalized, pre-dedupe candidate objects to `<discovery_dir>/raw_candidates.jsonl` when multiple file-review results are being aggregated or cross-file dedupe is needed.- Do not stop until every `deep_review_input.jsonl` row has a completion receipt.+### Windows Worklist Copy++In PowerShell, replace the placeholders inside single quotes with literal paths, doubling any single quote in a path:++```powershell+$env:worklistPluginRoot = Convert-Path -LiteralPath '<plugin_dir>' -ErrorAction Stop+$env:worklistDiscoveryDir = Convert-Path -LiteralPath '<discovery_dir>' -ErrorAction Stop+cmd.exe /d /v:off /s /c '""%worklistPluginRoot%\scripts\launch_codex_security_mcp.cmd" --helper copy-deep-review-input --rank-input "%worklistDiscoveryDir%\rank_input.jsonl" --out "%worklistDiscoveryDir%\deep_review_input.jsonl""'+```++These variables are temporary values in the calling shell, not application settings. Resolve paths against PowerShell's current location before CMD starts, including when that location is a UNC share. Missing paths stop the invocation with PowerShell's path error. The discovery directory already contains `rank_input.jsonl`; the output file does not need to exist. CMD expands the references once, preserving literal `%` and `!` in the paths.+## Candidate Finding Coverage- Track candidate-finding coverage separately from file coverage.diff --git a/plugins/codex-security/tests/test_generate_rank_input.py b/plugins/codex-security/tests/test_generate_rank_input.pyindex 1c82a5a9d..ba6f7442b 100644--- a/plugins/codex-security/tests/test_generate_rank_input.py+++ b/plugins/codex-security/tests/test_generate_rank_input.py@@ -1297,121 +1297,6 @@ def test_make_rank_input_decodes_bom_marked_utf16_source(tmp_path: Path, mode: sassert {row["path"]: row["preview"] for row in read_jsonl(output)} == expected-def test_copy_and_select_deep_review_inputs(tmp_path: Path) -> None:- rank_input = tmp_path / "rank_input.jsonl"- write_jsonl(- rank_input,- [- {"path": "a.py", "area": "core", "preview": "a"},- {"path": "b.py", "area": "api", "preview": "b"},- ],- )- copied = tmp_path / "copied.jsonl"- run_cli(- "copy-deep-review-input",- "--rank-input",- str(rank_input),- "--out",- str(copied),- )- assert read_jsonl(copied) == [- {"path": "a.py", "area": "core"},- {"path": "b.py", "area": "api"},- ]-- rank_output = tmp_path / "rank_output.jsonl"- write_jsonl(- rank_output,- [- {"path": "c.py", "area": "api", "score": 8, "include": True, "reason": "c"},- {"path": "a.py", "area": "core", "score": 10, "include": True, "reason": "a"},- {"path": "b.py", "area": "api", "score": 8, "include": True, "reason": "b"},- {"path": "d.py", "area": "core", "score": 2, "include": False, "reason": "d"},- ],- )- selected = tmp_path / "selected.jsonl"- run_cli(- "select-deep-review-input",- "--rank-output",- str(rank_output),- "--top-percent",- "67",- "--out",- str(selected),- )- assert read_jsonl(selected) == [- {"path": "a.py", "area": "core"},- {"path": "b.py", "area": "api"},- ]---def test_select_honors_explicit_top_percent_20(tmp_path: Path) -> None:- rank_output = tmp_path / "rank_output.jsonl"- rows = make_rank_rows(5)- write_jsonl(- rank_output,- [rank_result(row, score=10 - index) for index, row in enumerate(rows)],- )- selected = tmp_path / "selected.jsonl"-- run_cli(- "select-deep-review-input",- "--rank-output",- str(rank_output),- "--top-percent",- "20",- "--out",- str(selected),- )-- assert read_jsonl(selected) == [{"path": "src/file_00.py", "area": "src"}]---def test_select_defaults_to_top_percent_100(tmp_path: Path) -> None:- rank_output = tmp_path / "rank_output.jsonl"- rows = make_rank_rows(5)- write_jsonl(- rank_output,- [rank_result(row, score=10 - index) for index, row in enumerate(rows)],- )- selected = tmp_path / "selected.jsonl"-- run_cli(- "select-deep-review-input",- "--rank-output",- str(rank_output),- "--out",- str(selected),- )-- assert len(read_jsonl(selected)) == 5---def test_select_falls_back_to_all_rows_when_workers_exclude_everything(tmp_path: Path) -> None:- rank_output = tmp_path / "rank_output.jsonl"- write_jsonl(- rank_output,- [- {"path": "b.py", "area": "api", "score": 2, "include": False, "reason": "b"},- {"path": "a.py", "area": "core", "score": 9, "include": False, "reason": "a"},- ],- )- selected = tmp_path / "selected.jsonl"-- run_cli(- "select-deep-review-input",- "--rank-output",- str(rank_output),- "--out",- str(selected),- )-- assert read_jsonl(selected) == [- {"path": "a.py", "area": "core"},- {"path": "b.py", "area": "api"},- ]--def test_make_rank_shards_is_deterministic_and_bounded(tmp_path: Path) -> None:rank_input = tmp_path / "rank_input.jsonl"rows = make_rank_rows(312)@@ -1581,16 +1466,6 @@ def test_empty_rank_input_closes_with_zero_shards_and_workers(tmp_path: Path) ->assert merge_result.stdout == f"Merged 0 ranking rows into {rank_output}\n"assert rank_output.read_bytes() == b""- deep_review_input = tmp_path / "deep_review_input.jsonl"- run_cli(- "select-deep-review-input",- "--rank-output",- str(rank_output),- "--out",- str(deep_review_input),- )- assert deep_review_input.read_bytes() == b""-def test_make_rank_pool_plan_requires_sibling_rank_shards_directory(tmp_path: Path) -> None:rank_input = tmp_path / "rank_input.jsonl"diff --git a/sdk/typescript/tests-ts/deep-review-input.test.ts b/sdk/typescript/tests-ts/deep-review-input.test.tsnew file mode 100644index 000000000..154ecfa1d--- /dev/null+++ b/sdk/typescript/tests-ts/deep-review-input.test.ts@@ -0,0 +1,694 @@+import { spawnSync } from "node:child_process";+import {+ existsSync,+ mkdirSync,+ mkdtempSync,+ readFileSync,+ realpathSync,+ rmSync,+ statSync,+ symlinkSync,+ writeFileSync,+} from "node:fs";+import { tmpdir } from "node:os";+import { join, relative } from "node:path";+import { afterEach, describe, expect, spyOn, test } from "bun:test";+import { PLUGIN_ROOT } from "./plugin-root.js";+import { runCommand } from "./support/shell.js";+import { removeTemporaryDirectory } from "./support/temporary-directories.js";+import { runTestInSubprocess } from "./support/test-subprocess.js";+import { windowsHelperFixture } from "./windows-helper-command.js";+import {+ hasWindowsLoopbackShare,+ windowsLoopbackPath,+} from "./windows-helper-location.js";++const node = Bun.which("node")!;+const helper = join(PLUGIN_ROOT, "mcp", "helpers.mjs");+const roots: string[] = [];+const newline = process.platform === "win32" ? "\r\n" : "\n";+type Row = Record<string, unknown>;+const candidate = (path: string, area = "src") => ({+ path,+ area,+ preview: "source preview",+});+const ranked = (path: string, score = 5, include = true, area = "src") => ({+ path,+ area,+ score,+ include,+ reason: "runtime surface",+});+function fixture() {+ const root = realpathSync(mkdtempSync(join(tmpdir(), "deep-review-input-")));+ roots.push(root);+ return {+ root,+ input: join(root, "input.jsonl"),+ output: join(root, "output.jsonl"),+ };+}+function write(path: string, rows: Row[]) {+ writeFileSync(path, rows.map((row) => JSON.stringify(row) + "\n").join(""));+}+function read(path: string) {+ const text = readFileSync(path, "utf8").trim();+ return text === ""+ ? []+ : text.split(/\r?\n/u).map((line) => JSON.parse(line));+}+function run(+ f: ReturnType<typeof fixture>,+ selection: boolean,+ args: string[] = [],+ env = process.env,+) {+ return spawnSync(+ node,+ [+ helper,+ selection ? "select-deep-review-input" : "copy-deep-review-input",+ selection ? "--rank-output" : "--rank-input",+ f.input,+ "--out",+ f.output,+ ...args,+ ],+ { cwd: f.root, encoding: "utf8", env },+ );+}+afterEach(async () => {+ await Promise.all(roots.splice(0).map(removeTemporaryDirectory));+});++describe("deep-review worklists", () => {+ test("selects rows with long internal whitespace without rescanning suffixes", async () => {+ const f = fixture();+ write(f.input, [+ { ...ranked("a.py"), reason: "a" + " ".repeat(128000) + "b" },+ ]);+ const result = await runCommand(+ node,+ [+ helper,+ "select-deep-review-input",+ "--rank-output",+ f.input,+ "--out",+ f.output,+ ],+ { cwd: f.root, timeout: 5000 },+ );+ expect(result.status, result.stderr).toBe(0);+ expect(read(f.output)).toEqual([{ path: "a.py", area: "src" }]);+ });+ test("copies all candidates in input order and selects included ranked rows", () => {+ const f = fixture();+ write(f.input, [candidate("a.py", "core"), candidate("b.py", "api")]);+ expect(run(f, false).status).toBe(0);+ expect(read(f.output)).toEqual([+ { path: "a.py", area: "core" },+ { path: "b.py", area: "api" },+ ]);+ write(f.input, [+ ranked("c.py", 8, true, "api"),+ ranked("a.py", 10, true, "core"),+ ranked("b.py", 8, true, "api"),+ ranked("d.py", 2, false, "core"),+ ]);+ const result = run(f, true, ["--top-percent", "67"]);+ expect(result.status).toBe(0);+ expect(result.stdout).toBe(+ `Selected 2 of 3 rows into ${f.output}${newline}`,+ );+ expect(read(f.output)).toEqual([+ { path: "a.py", area: "core" },+ { path: "b.py", area: "api" },+ ]);+ });+ test("honors explicit 20 percent and defaults to 100 percent", () => {+ const f = fixture();+ write(+ f.input,+ Array.from({ length: 5 }, (_, index) =>+ ranked(`src/file_${index}.py`, 10 - index),+ ),+ );+ expect(run(f, true, ["--top-percent", "20"]).status).toBe(0);+ expect(read(f.output)).toEqual([{ path: "src/file_0.py", area: "src" }]);+ expect(run(f, true).status).toBe(0);+ expect(read(f.output)).toHaveLength(5);+ });+ test("falls back to all rows when workers exclude everything", () => {+ const f = fixture();+ write(f.input, [+ ranked("b.py", 2, false, "api"),+ ranked("a.py", 9, false, "core"),+ ]);+ expect(run(f, true).status).toBe(0);+ expect(read(f.output)).toEqual([+ { path: "a.py", area: "core" },+ { path: "b.py", area: "api" },+ ]);+ });+ test.each([false, true])(+ "closes an empty worklist with an empty output (selection=%j)",+ (selection) => {+ const f = fixture();+ write(f.input, []);+ writeFileSync(f.output, "previous contents");+ expect(run(f, selection).status).toBe(0);+ expect(readFileSync(f.output)).toHaveLength(0);+ },+ );+ test.each([+ ["0", 1],+ ["-10", 1],+ ["1", 1],+ ["200", 3],+ ["+20", 1],+ ] as const)(+ "selects the requested integer percentage %s",+ (percent, count) => {+ const f = fixture();+ write(f.input, [ranked("a.py"), ranked("b.py"), ranked("c.py")]);+ expect(run(f, true, [`--top-percent=${percent}`]).status).toBe(0);+ expect(read(f.output)).toHaveLength(count);+ },+ );+ test("orders tied Unicode paths and preserves their contents", () => {+ const f = fixture();+ write(f.input, [+ ranked("𐀀.py", 5, true, "é"),+ ranked("\ue000.py", 5, true, "\udcff"),+ ranked("\u007f.py", 5, true, "\t"),+ ]);+ expect(run(f, true).status).toBe(0);+ expect(read(f.output)).toEqual([+ { path: "\u007f.py", area: "\t" },+ { path: "\ue000.py", area: "\udcff" },+ { path: "𐀀.py", area: "é" },+ ]);+ });+ test.each([+ ["\n", false],+ ["\n", true],+ ["\r\n", false],+ ["\r\n", true],+ ["\r", false],+ ["\r", true],+ ] as const)(+ "accepts UTF-8 rows with newline %j and final terminator %j",+ (separator, terminated) => {+ const f = fixture();+ writeFileSync(+ f.input,+ JSON.stringify(candidate("é.py", "雪")) ++ separator ++ JSON.stringify(candidate("b.py")) ++ (terminated ? separator : ""),+ );+ expect(run(f, false).status).toBe(0);+ expect(read(f.output)).toEqual([+ { path: "é.py", area: "雪" },+ { path: "b.py", area: "src" },+ ]);+ },+ );+ test("reads split UTF-8 sequences and CRLF without adding or losing rows", () => {+ const f = fixture();+ const prefix = '{"path":"a.py","area":"src","preview":"';+ const first = prefix + "x".repeat(65_535 - prefix.length) + '雪"}';+ const second = JSON.stringify(candidate("b.py"));+ const padding = 2 * 65_536 - Buffer.byteLength(first + "\n" + second) - 1;+ writeFileSync(+ f.input,+ first ++ "\n" ++ second ++ " ".repeat(padding) ++ "\r\n" ++ JSON.stringify(candidate("c.py")) ++ "\r",+ );+ expect(run(f, false).status).toBe(0);+ expect(read(f.output)).toEqual([+ { path: "a.py", area: "src" },+ { path: "b.py", area: "src" },+ { path: "c.py", area: "src" },+ ]);+ });+ test.skipIf(process.platform === "win32")(+ "closes worklist descriptors after completion or early failure",+ async () => {+ if (+ runTestInSubprocess(+ import.meta.path,+ "closes worklist descriptors after completion or early failure",+ )+ )+ return;+ const files = await import("node:fs");+ const opening = spyOn(files, "openSync");+ const helperSource = new URL(+ "../../../plugins/codex-security/mcp-app/src/helpers/helper-files.ts",+ import.meta.url,+ );+ const { readUtf8Lines } = await import(helperSource.href);+ const f = fixture();+ try {+ for (const mode of ["complete", "return", "decode", "consumer"]) {+ writeFileSync(+ f.input,+ mode === "decode" ? Buffer.from([0x61, 10, 0xc3]) : "a\nb\n",+ );+ opening.mockClear();+ const iterator = readUtf8Lines(f.input)[Symbol.iterator]();+ try {+ expect(iterator.next().value).toBe("a");+ const descriptor = opening.mock.results[0]!.value as number;+ expect(files.fstatSync(descriptor).isFile()).toBe(true);+ const remaining = { [Symbol.iterator]: () => iterator };+ if (mode === "return") iterator.return!();+ else if (mode === "decode")+ expect(() => iterator.next()).toThrow("encoded data");+ else if (mode === "consumer")+ expect(() =>+ Array.from(remaining, () => {+ throw new Error("consumer failed");+ }),+ ).toThrow("consumer failed");+ else expect(Array.from(remaining)).toEqual(["b"]);+ expect(() => files.fstatSync(descriptor)).toThrow("EBADF");+ } finally {+ iterator.return!();+ }+ }+ } finally {+ opening.mockRestore();+ }+ },+ );+ const invalid: Array<[boolean, string | Buffer, string]> = [+ [false, "\n", "blank JSONL rows are not allowed"],+ [+ false,+ JSON.stringify(candidate("a.py")) + "\r\n\r",+ ":2: blank JSONL rows are not allowed",+ ],+ [false, "\u001c\n", "invalid JSON"],+ [false, "{}\n\n", "missing fields"],+ [false, "{not json}\n", "invalid JSON"],+ [false, "[]\n", "expected a JSON object"],+ [+ false,+ JSON.stringify({ ...candidate("a.py"), extra: true }),+ 'unexpected fields ["extra"]',+ ],+ [false, JSON.stringify(candidate(" ")), "path must be a non-empty string"],+ [+ false,+ JSON.stringify({ ...candidate("a.py"), preview: 1 }),+ "preview must be a string",+ ],+ [+ false,+ JSON.stringify({ ...candidate("a.py"), area: null }),+ "area must be a string",+ ],+ [+ true,+ JSON.stringify({ ...ranked("a.py"), score: true }),+ "score must be an integer",+ ],+ [+ true,+ '{"path":"a.py","area":"src","score":5.5,"include":true,"reason":"x"}',+ "score must be an integer",+ ],+ [+ true,+ JSON.stringify(ranked("a.py", 11)),+ "score must be from 1 through 10",+ ],+ [+ true,+ JSON.stringify({ ...ranked("a.py"), include: 1 }),+ "include must be a boolean",+ ],+ [+ true,+ JSON.stringify({ ...ranked("a.py"), reason: "\t" }),+ "reason must be a non-empty string",+ ],+ [false, "\ufeff" + JSON.stringify(candidate("a.py")), "invalid JSON"],+ [false, Buffer.from([0xff]), "encoded data"],+ [+ false,+ Buffer.concat([+ Buffer.from(JSON.stringify(candidate("é.py")) + "\r\n"),+ Buffer.from([0xc3]),+ ]),+ "encoded data",+ ],+ ];+ test.each(invalid)(+ "rejects invalid worklist data (selection=%j, data=%j)",+ (selection, text, message) => {+ const f = fixture();+ writeFileSync(f.input, text);+ writeFileSync(f.output, "preserve existing output");+ const result = run(f, selection);+ expect(result.status).toBe(1);+ expect(result.stderr).toContain(message);+ expect(readFileSync(f.output, "utf8")).toBe("preserve existing output");+ },+ );+ test.each([false, true])(+ "rejects repeated paths before output (selection=%j)",+ (selection) => {+ const f = fixture();+ write(+ f.input,+ selection+ ? [ranked("a-\u007f\u009b.py"), ranked("a-\u007f\u009b.py")]+ : [candidate("a-\u007f\u009b.py"), candidate("a-\u007f\u009b.py")],+ );+ const result = run(f, selection);+ expect(result.status).toBe(1);+ expect(result.stderr).toContain(+ 'contains duplicate paths: ["a-\\u007f\\u009b.py"]',+ );+ expect(result.stderr).not.toMatch(/[\u007f-\u009f]/u);+ expect(existsSync(f.output)).toBe(false);+ },+ );+ test("accepts duplicate JSON object keys and empty area/preview as before", () => {+ const f = fixture();+ writeFileSync(+ f.input,+ '{"path":"unused.py","path":"a.py","area":"","preview":""}',+ );+ expect(run(f, false).status).toBe(0);+ expect(read(f.output)).toEqual([{ path: "a.py", area: "" }]);+ });+ test("allows replacing the input after all rows have been read", () => {+ const f = fixture();+ write(f.input, [candidate("b.py"), candidate("a.py")]);+ expect(run({ ...f, output: f.input }, false).status).toBe(0);+ expect(read(f.input)).toEqual([+ { path: "b.py", area: "src" },+ { path: "a.py", area: "src" },+ ]);+ });+ test("expands homes, creates output parents, and preserves path spelling in status", () => {+ const f = fixture();+ write(f.input, [candidate("a.py")]);+ const env = { ...process.env, HOME: f.root, USERPROFILE: f.root };+ const result = run(+ { ...f, input: "~/input.jsonl", output: "./nested/./output.jsonl" },+ false,+ [],+ env,+ );+ expect(result.status).toBe(0);+ expect(result.stdout).toBe(+ `Copied 1 rows into ${join("nested", "output.jsonl")}${newline}`,+ );+ expect(read(join(f.root, "nested", "output.jsonl"))).toHaveLength(1);+ });+ test.each([false, true])(+ "removes pathlib dot and empty components from input and output (selection=%j)",+ (selection) => {+ const f = fixture();+ write(f.input, [selection ? ranked("a.py") : candidate("a.py")]);+ for (const suffix of ["/.", "///", "/./."]) {+ const result = run(+ { ...f, input: f.input + suffix, output: f.output + suffix },+ selection,+ );+ expect(result.status, result.stderr).toBe(0);+ expect(result.stdout).toBe(+ `${selection ? "Selected 1 of 1" : "Copied 1"} rows into ${f.output}${newline}`,+ );+ expect(read(f.output)).toEqual([{ path: "a.py", area: "src" }]);+ }+ },+ );+ test("reports missing input and preserves argument parsing status", () => {+ const f = fixture();+ writeFileSync(f.output, "previous output\n");+ const inputs = [f.input, join(f.output, "child.jsonl")];+ if (process.platform !== "win32") {+ const loop = join(f.root, "loop-\x1b[2J.jsonl");+ symlinkSync(loop, loop);+ inputs.push(loop);+ }+ for (const selection of [false, true]) {+ for (const input of inputs) {+ const result = run({ ...f, input }, selection);+ expect(result.status).toBe(1);+ expect(result.stderr).toBe(+ `Rank ${selection ? "output" : "input"} missing: ${input}${newline}`,+ );+ }+ expect(readFileSync(f.output, "utf8")).toBe("previous output\n");+ }+ write(f.input, [ranked("a.py")]);+ expect(run(f, true, ["--top-percent=20"]).status).toBe(0);+ expect(run(f, true, ["--top-percent", "20"]).status).toBe(0);+ for (const args of [+ ["--top-percent", "1.0"],+ ["--top-percent"],+ ["--unknown"],+ ["extra"],+ ["--top-percent", "1__0"],+ ["--top-percent", "\u001c20"],+ ])+ expect(run(f, true, args).status).toBe(2);+ expect(run(f, true, ["--help"]).stdout).toContain("Defaults to 100");+ });+ test.skipIf(process.platform === "win32")(+ "escapes controls in filesystem errors but preserves other command text",+ () => {+ const f = fixture();+ f.input = join(f.root, "input-\x1b[2J.jsonl");+ f.output = join(f.root, "output-\x1b[2J.jsonl");+ for (const selection of [false, true]) {+ const missing = run(f, selection);+ expect(missing.status).toBe(1);+ expect(missing.stderr).toBe(+ `Rank ${selection ? "output" : "input"} missing: ${f.input}\n`,+ );+ writeFileSync(f.input, "{}\n");+ const invalid = run(f, selection);+ expect(invalid.status).toBe(1);+ expect(invalid.stderr).toStartWith(`${f.input}:1: missing fields`);+ write(f.input, [selection ? ranked("a.py") : candidate("a.py")]);+ mkdirSync(f.output);+ const collision = run(f, selection);+ expect(collision.status).toBe(1);+ expect(collision.stderr).toMatch(/\\(?:x1b|u001b)\[2J/);+ expect(collision.stderr).not.toContain("\x1b");+ rmSync(f.output, { recursive: true });+ const success = run(f, selection);+ expect(success.status).toBe(0);+ expect(success.stdout).toBe(+ `${selection ? "Selected 1 of 1" : "Copied 1"} rows into ${f.output}\n`,+ );+ rmSync(f.input);+ rmSync(f.output);+ }+ },+ );+ test.skipIf(process.platform === "win32")(+ "follows existing input/output symlinks and preserves existing output permissions",+ () => {+ const f = fixture();+ const target = join(f.root, "target.jsonl");+ writeFileSync(target, "existing", { mode: 0o640 });+ symlinkSync(target, f.output);+ write(f.input, [candidate("a.py")]);+ const inputLink = join(f.root, "input-link");+ symlinkSync(f.input, inputLink);+ expect(run({ ...f, input: inputLink }, false).status).toBe(0);+ expect(read(target)).toHaveLength(1);+ expect(statSync(target).mode & 0o777).toBe(0o640);+ },+ );+ test.skipIf(process.platform === "win32")(+ "preserves symlink-sensitive parent traversal for both input and output",+ () => {+ const f = fixture();+ const child = join(f.root, "child");+ mkdirSync(join(child, "nested"), { recursive: true });+ symlinkSync(join(child, "nested"), join(f.root, "link"));+ write(f.input, [candidate("wrong.py")]);+ writeFileSync(f.output, "output sentinel");+ const input = join(child, "input.jsonl");+ for (const selection of [false, true]) {+ write(input, [selection ? ranked("right.py") : candidate("right.py")]);+ const result = run(+ {+ ...f,+ input: "link/../input.jsonl/.",+ output: "link/../output.jsonl/.",+ },+ selection,+ );+ expect(result.status, result.stderr).toBe(0);+ expect(read(join(child, "output.jsonl"))).toEqual([+ { path: "right.py", area: "src" },+ ]);+ expect(readFileSync(f.output, "utf8")).toBe("output sentinel");+ }+ },+ );+ test.skipIf(process.platform === "win32")(+ "launcher preserves POSIX input and output path bytes",+ () => {+ const f = fixture();+ // APFS requires valid UTF-8 filenames; Linux also permits undecodable bytes.+ const [inputBytes, outputBytes] =+ process.platform === "darwin"+ ? [Buffer.from("é"), Buffer.from("ö")]+ : [Buffer.from([0xff]), Buffer.from([0xfe])];+ const shellBytes = (bytes: Buffer) =>+ Array.from(+ bytes,+ (byte) => `\\${byte.toString(8).padStart(3, "0")}`,+ ).join("");+ const input = Buffer.concat([Buffer.from(f.root + "/in-"), inputBytes!]);+ const output = Buffer.concat([+ Buffer.from(f.root + "/nested-"),+ inputBytes!,+ Buffer.from("/out-"),+ outputBytes!,+ ]);+ writeFileSync(input, JSON.stringify(candidate("a.py")) + "\n");+ const launcher = join(+ PLUGIN_ROOT,+ "scripts",+ "launch_codex_security_mcp",+ );+ const result = spawnSync(+ "sh",+ [+ "-c",+ `exec "$1" --helper copy-deep-review-input --rank-input "$2/in-$(printf '${shellBytes(inputBytes!)}')" --out "$2/nested-$(printf '${shellBytes(inputBytes!)}')/out-$(printf '${shellBytes(outputBytes!)}')"`,+ "sh",+ launcher,+ f.root,+ ],+ { env: { ...process.env, CODEX_MCP_NODE_PATH: node } },+ );+ expect(result.status).toBe(0);+ expect(readFileSync(output, "utf8")).toBe(+ '{"path":"a.py","area":"src"}\n',+ );+ expect(result.stdout).toEqual(+ Buffer.concat([+ Buffer.from("Copied 1 rows into "),+ output,+ Buffer.from("\n"),+ ]),+ );+ },+ );+ for (const location of ["absolute", "relative", "unc"] as const)+ test.skipIf(+ process.platform !== "win32" ||+ (location === "unc" && !hasWindowsLoopbackShare),+ )(+ `documented PowerShell copy command preserves ${location} literal paths and worklist contents`,+ async () => {+ const f = fixture();+ const launcher = windowsHelperFixture(f.root);+ const discovery = join(+ f.root,+ "discovery %USERNAME% !EXPAND! \u96ea's",+ );+ const expandedDiscovery = discovery.replace(+ "%USERNAME%",+ "expanded-user",+ );+ for (const directory of [discovery, expandedDiscovery])+ mkdirSync(directory);+ write(join(discovery, "rank_input.jsonl"), [+ candidate("caf\u00e9/\u96ea.py"),+ ]);+ write(join(expandedDiscovery, "rank_input.jsonl"), [+ candidate("wrong.py"),+ ]);+ const output = join(discovery, "deep_review_input.jsonl");+ const expandedOutput = join(+ expandedDiscovery,+ "deep_review_input.jsonl",+ );+ const caller = join(f.root, "caller");+ mkdirSync(caller);+ const workingDirectory =+ location === "unc" ? windowsLoopbackPath(caller) : caller;+ const argument = (path: string) =>+ location === "absolute" ? path : relative(caller, path);+ for (const powershell of launcher.powershells) {+ rmSync(output, { force: true });+ writeFileSync(expandedOutput, "expanded output sentinel");+ const result = await launcher.run(+ powershell,+ "skills/security-scan/references/scan-artifacts-and-ledger.md",+ {+ "<plugin_dir>": argument(launcher.plugin),+ "<discovery_dir>": argument(discovery),+ },+ undefined,+ workingDirectory,+ );+ expect(result.stdout, result.diagnostics).not.toContain(+ "expanded-plugin-used",+ );+ expect(result.status, result.diagnostics).toBe(0);+ if (location !== "unc")+ expect(result.stderr, result.diagnostics).toBe("");+ expect(existsSync(output), result.diagnostics).toBe(true);+ expect(read(output), result.diagnostics).toEqual([+ { path: "caf\u00e9/\u96ea.py", area: "src" },+ ]);+ expect(readFileSync(expandedOutput, "utf8"), result.diagnostics).toBe(+ "expanded output sentinel",+ );+ }+ },+ );+ test.skipIf(process.platform !== "win32")(+ "Windows launcher reads and writes paths with spaces",+ () => {+ const f = fixture();+ const input = join(f.root, "rank input.jsonl"),+ output = join(f.root, "deep review.jsonl");+ write(input, [candidate("a.py")]);+ const caller = join(f.root, "caller.cmd");+ const launcher = join(+ PLUGIN_ROOT,+ "scripts",+ "launch_codex_security_mcp.cmd",+ );+ writeFileSync(+ caller,+ `@echo off\r\ncall "${launcher}" --helper copy-deep-review-input --rank-input "${input}" --out "${output}"\r\nexit /b %errorlevel%\r\n`,+ );+ const result = spawnSync(+ process.env["ComSpec"] ?? "cmd.exe",+ ["/d", "/s", "/c", `""${caller}""`],+ {+ env: { ...process.env, CODEX_MCP_NODE_PATH: node },+ encoding: "utf8",+ windowsVerbatimArguments: true,+ },+ );+ expect(result.status, result.stderr || result.error?.message).toBe(0);+ expect(read(output)).toEqual([{ path: "a.py", area: "src" }]);+ },+ );+});diff --git a/sdk/typescript/tests-ts/patch-risk-contract.test.ts b/sdk/typescript/tests-ts/patch-risk-contract.test.tsindex b0d6aaaa3..03e31eeef 100644--- a/sdk/typescript/tests-ts/patch-risk-contract.test.ts+++ b/sdk/typescript/tests-ts/patch-risk-contract.test.ts@@ -1,7 +1,5 @@import { spawnSync } from "node:child_process";import {- copyFile,- cp,mkdir,mkdtemp,readFile,@@ -15,6 +13,7 @@ import Ajv2020 from "ajv/dist/2020.js";import { afterEach, describe, expect, test } from "bun:test";import { PLUGIN_ROOT } from "./plugin-root.js";import { runCommand } from "./support/shell.js";+import { windowsHelperFixture } from "./windows-helper-command.js";import { removeTemporaryDirectory } from "./support/temporary-directories.js";import {hasWindowsLoopbackShare,@@ -805,27 +804,13 @@ describe("patch risk assessment contract", () => {async () => {const outside = await mkdtemp(join(tmpdir(), "patch-risk-powershell-"));powershellDirectories.push(outside);- const plugin = join(outside, "plugin %PLUGIN% !EXPAND! caf\u00e9's");- const expandedPlugin = plugin.replace("%PLUGIN%", "expanded-plugin");+ const launcher = windowsHelperFixture(outside);const file = join(outside,"review-%USERNAME% !EXPAND! \u96ea's","assessment.json",);const expandedFile = file.replace("%USERNAME%", "expanded-user");- await mkdir(join(plugin, "scripts"), { recursive: true });- await cp(join(PLUGIN_ROOT, "mcp"), join(plugin, "mcp"), {- recursive: true,- });- await copyFile(- join(PLUGIN_ROOT, "scripts", "launch_codex_security_mcp.cmd"),- join(plugin, "scripts", "launch_codex_security_mcp.cmd"),- );- await mkdir(join(expandedPlugin, "scripts"), { recursive: true });- await writeFile(- join(expandedPlugin, "scripts", "launch_codex_security_mcp.cmd"),- "@echo expanded-plugin-used\r\n@exit /b 0\r\n",- );for (const path of [file, expandedFile])await mkdir(dirname(path), { recursive: true });const original = JSON.stringify(assessment()).replace(@@ -833,52 +818,13 @@ describe("patch risk assessment contract", () => {"example/caf\u00e9-\u96ea",);await writeFile(expandedFile, original);- const skill = await readFile(- join(PLUGIN_ROOT, "skills", "assess-patch-risk", "SKILL.md"),- "utf8",- );- const command = /```powershell\r?\n([\s\S]*?)\r?\n```/u.exec(- skill,- )?.[1];- expect(command).toBeDefined();- const quote = (value: string) => value.replaceAll("'", "''");const caller = join(outside, "caller");await mkdir(caller);const workingDirectory =location === "unc" ? windowsLoopbackPath(caller) : caller;const argument = (path: string) =>location === "absolute" ? path : relative(caller, path);- const powershells = [- join(- process.env["SystemRoot"]!,- "System32",- "WindowsPowerShell",- "v1.0",- "powershell.exe",- ),- Bun.which("pwsh"),- ].filter((value): value is string => value !== null);- const fixtureEnvironment = {- SystemRoot: process.env["SystemRoot"],- PATH: join(process.env["SystemRoot"]!, "System32"),- HOME: outside,- USERPROFILE: outside,- LOCALAPPDATA: outside,- XDG_CACHE_HOME: outside,- CODEX_MCP_NODE_PATH: node,- PLUGIN: "expanded-plugin",- USERNAME: "expanded-user",- EXPAND: "expanded-bang",- };- const overrides = new Set(- Object.keys(fixtureEnvironment).map((key) => key.toUpperCase()),- );- const inherited = Object.fromEntries(- Object.entries(process.env).filter(- ([key]) => !overrides.has(key.toUpperCase()),- ),- );- for (const powershell of powershells) {+ for (const powershell of launcher.powershells) {for (const input of ["invalid","valid",@@ -888,66 +834,21 @@ describe("patch risk assessment contract", () => {]) {await writeFile(file, input === "invalid" ? "{}" : original);if (input === "missing") await rm(file);- const script =- "$ProgressPreference = 'SilentlyContinue'\n" +- `Set-Location -LiteralPath '${quote(workingDirectory)}' -ErrorAction Stop\n` +- "$ExecutionContext.SessionState.LanguageMode = 'ConstrainedLanguage'\n" +- command!- .replace("<plugin-root>", quote(argument(plugin)))- .replace(- "<assessment.json>",- quote(- input === "stdin" || input === "pipeline"- ? "-"- : argument(file),- ),- ) +- "\n";- const result = await runCommand(+ const result = await launcher.run(powershell,- [- "-NoLogo",- "-NoProfile",- "-NonInteractive",- "-EncodedCommand",- Buffer.from(- input === "pipeline"- ? script.replace(- /^cmd\.exe/m,- `'${quote(original)}' | cmd.exe`,- )- : script,- "utf16le",- ).toString("base64"),- ],- {- cwd: outside,- env: { ...inherited, ...fixtureEnvironment },- timeout: 30_000,- input: input === "pipeline" ? "" : original,- windowsHide: true,- },- );- const diagnostics = JSON.stringify(+ "skills/assess-patch-risk/SKILL.md",{- powershell,- location,- input,- workingDirectory,- plugin: argument(plugin),- assessment:+ "<plugin-root>": argument(launcher.plugin),+ "<assessment.json>":input === "stdin" || input === "pipeline"? "-": argument(file),- status: result.status,- signal: result.signal,- error: result.error?.message,- stdout: result.stdout,- stderr: result.stderr,},- null,- 2,+ input === "pipeline" ? "" : original,+ workingDirectory,+ input === "pipeline" ? original : undefined,);+ const diagnostics = `${location} ${input}\n${result.diagnostics}`;expect(result.stdout, diagnostics).not.toContain("expanded-plugin-used",);diff --git a/sdk/typescript/tests-ts/windows-helper-command.ts b/sdk/typescript/tests-ts/windows-helper-command.tsnew file mode 100644index 000000000..e93185452--- /dev/null+++ b/sdk/typescript/tests-ts/windows-helper-command.ts@@ -0,0 +1,119 @@+import {+ copyFileSync,+ cpSync,+ mkdirSync,+ readFileSync,+ writeFileSync,+} from "node:fs";+import { join } from "node:path";+import { PLUGIN_ROOT } from "./plugin-root.js";+import { runCommand } from "./support/shell.js";++export function windowsHelperFixture(root: string) {+ const plugin = join(root, "plugin %PLUGIN% !EXPAND! caf\u00e9's");+ mkdirSync(join(plugin, "scripts"), { recursive: true });+ cpSync(join(PLUGIN_ROOT, "mcp"), join(plugin, "mcp"), { recursive: true });+ copyFileSync(+ join(PLUGIN_ROOT, "scripts", "launch_codex_security_mcp.cmd"),+ join(plugin, "scripts", "launch_codex_security_mcp.cmd"),+ );+ const expandedPlugin = plugin.replace("%PLUGIN%", "expanded-plugin");+ mkdirSync(join(expandedPlugin, "scripts"), { recursive: true });+ writeFileSync(+ join(expandedPlugin, "scripts", "launch_codex_security_mcp.cmd"),+ "@echo expanded-plugin-used\r\n@exit /b 0\r\n",+ );+ const systemRoot = process.env["SystemRoot"]!;+ const powershells = [+ join(systemRoot, "System32", "WindowsPowerShell", "v1.0", "powershell.exe"),+ Bun.which("pwsh"),+ ].filter((value): value is string => value !== null);+ return {+ plugin,+ powershells,+ async run(+ powershell: string,+ document: string,+ paths: Record<string, string>,+ input?: string,+ workingDirectory = root,+ pipelineInput?: string,+ ) {+ const source = readFileSync(join(PLUGIN_ROOT, document), "utf8");+ let command = /```powershell\r?\n([\s\S]*?)\r?\n```/u.exec(source)?.[1];+ if (command === undefined)+ throw new Error(`No PowerShell command in ${document}`);+ for (const [placeholder, path] of Object.entries(paths))+ command = command.replaceAll(placeholder, path.replaceAll("'", "''"));+ if (pipelineInput !== undefined)+ command = command.replace(+ /^cmd\.exe/m,+ `'${pipelineInput.replaceAll("'", "''")}' | cmd.exe`,+ );+ command =+ "$ProgressPreference = 'SilentlyContinue'\n" ++ `Set-Location -LiteralPath '${workingDirectory.replaceAll("'", "''")}' -ErrorAction Stop\n` ++ "$ExecutionContext.SessionState.LanguageMode = 'ConstrainedLanguage'\n" ++ command;+ const fixtureEnvironment = {+ SystemRoot: systemRoot,+ PATH: join(systemRoot, "System32"),+ HOME: root,+ USERPROFILE: root,+ LOCALAPPDATA: root,+ XDG_CACHE_HOME: root,+ CODEX_MCP_NODE_PATH: Bun.which("node")!,+ PLUGIN: "expanded-plugin",+ USERNAME: "expanded-user",+ EXPAND: "expanded-bang",+ };+ const overrides = new Set(+ Object.keys(fixtureEnvironment).map((key) => key.toUpperCase()),+ );+ const inheritedEnvironment = Object.fromEntries(+ Object.entries(process.env).filter(+ ([key]) => !overrides.has(key.toUpperCase()),+ ),+ );+ const result = await runCommand(+ powershell,+ [+ "-NoLogo",+ "-NoProfile",+ "-NonInteractive",+ "-EncodedCommand",+ Buffer.from(command, "utf16le").toString("base64"),+ ],+ {+ cwd: root,+ env: {+ ...inheritedEnvironment,+ ...fixtureEnvironment,+ },+ timeout: 30_000,+ input,+ windowsHide: true,+ },+ );+ return {+ ...result,+ diagnostics: JSON.stringify(+ {+ powershell,+ document,+ workingDirectory,+ paths,+ command,+ status: result.status,+ signal: result.signal,+ error: result.error?.message,+ stdout: result.stdout,+ stderr: result.stderr,+ },+ null,+ 2,+ ),+ };+ },+ };+}