OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix(discovery): normalize candidates against selected sources (#1502)
mldangelo-oaibdb9a77623b3
diff --git a/plugins/codex-security/mcp-app/helpers-main.ts b/plugins/codex-security/mcp-app/helpers-main.tsindex cc32f2751..b0ff2a643 100644--- a/plugins/codex-security/mcp-app/helpers-main.ts+++ b/plugins/codex-security/mcp-app/helpers-main.ts@@ -1,9 +1,13 @@import { closeSync, readFileSync } from "node:fs";import { parseArgs } from "node:util";+import { isMainThread, parentPort, workerData } from "node:worker_threads";import { resolveSecurityMdCommand } from "./src/helpers/resolve-security-md";import { decodePosixBytes } from "./src/helpers/posix-path";import { windowsBinding } from "./src/native";-import { normalizeCandidatesCommand } from "./src/helpers/normalize-candidates";+import {+ normalizeCandidateBatch,+ normalizeCandidatesCommand,+} from "./src/helpers/normalize-candidates";import { validatePatchRiskAssessmentCommand } from "./src/helpers/validate-patch-risk-assessment";import { deepReviewInputCommand } from "./src/helpers/deep-review-input";import { rankShardsCommand } from "./src/helpers/rank-shards";@@ -12,8 +16,8 @@ import { bindRepoScopesCommand } from "./src/helpers/bind-repo-scopes";import { escapeControls, stringifyJson } from "./src/helpers/json";import { decodeUtf8 } from "./src/helpers/utf8";-let commandLine = process.argv.slice(2);-if (process.platform === "win32") {+let commandLine = isMainThread ? process.argv.slice(2) : [];+if (isMainThread && process.platform === "win32") {const original = windowsBinding().windowsArguments();commandLine = original.slice(original.length - commandLine.length)@@ -52,7 +56,9 @@ const workbenchUsage: Record<string, string> = {dashboard:"Usage: dashboard\nReads a JSON object from stdin with an absolute stateDirectory and payload containing view (findings or groups), sort, limit and offset; direction, query, repository and id are optional.",};-if (command === "resolve-security-md") {+if (!isMainThread) {+ parentPort!.postMessage(normalizeCandidateBatch(workerData));+} else if (command === "resolve-security-md") {process.exitCode = resolveSecurityMdCommand(args, posixHome);} else if (command === "normalize-candidates") {process.exitCode = normalizeCandidatesCommand(args, posixHome);diff --git a/plugins/codex-security/mcp-app/src/artifact-discovery.ts b/plugins/codex-security/mcp-app/src/artifact-discovery.tsindex f6546a32b..de57eb78b 100644--- a/plugins/codex-security/mcp-app/src/artifact-discovery.ts+++ b/plugins/codex-security/mcp-app/src/artifact-discovery.ts@@ -1,7 +1,16 @@-import { execFile } from "node:child_process";-import { promises as fs } from "node:fs";-import { dirname, join } from "node:path";-import { promisify } from "node:util";+import { readFile } from "node:fs/promises";+import { createRequire } from "node:module";+import { join } from "node:path";+import { Worker } from "node:worker_threads";+import { decodeUtf8 } from "./helpers/utf8.js";+import { resolvePythonCommand, runPythonWithInput } from "./python_command.js";+import {+ stableJson,+ candidateRelativePath,+ type CandidateNormalizationInput,+ type CandidateSource,+ type normalizeCandidateBatch,+} from "./helpers/normalize-candidates.js";import type * as z from "zod/v4";import discoveryCandidateDefinitions from "../../schemas/definitions/discovery-candidate.schema.json";import discoveryCandidatesToolSchema from "../../schemas/tools/discovery-candidates.schema.json";@@ -10,7 +19,8 @@ import {artifactDestination,paginateArtifactRows,readArtifactJsonl,- readArtifactText,+ artifactSourcePath,+ replaceArtifactText,} from "./artifact-io.js";import {loadArtifactZodSchema,@@ -18,7 +28,6 @@ import {} from "./artifact-schema-loader.js";import { candidateSchemaV1 } from "./deep-scan/artifact-contracts.js";-const execFileAsync = promisify(execFile);const discoveryComponents = ["artifacts", "02_discovery"] as const;const discoveryLabel = "discovery candidates";const discoverySchemaDocuments = [@@ -95,109 +104,131 @@ export const workbenchListCodexSecurityCandidatesInputSchema ="workbenchListCandidatesInput",) as z.ZodType<ListCodexSecurityCandidatesInput & { scanId: string }>;-/**- * Invoke the shared discovery normalizer. Only its canonical output persists;- * raw candidate input is kept in a private temporary directory and always removed.- */+/** Normalize in memory and replace the bound canonical candidate ledger. */export async function recordCodexSecurityDiscoveryCandidates(input: DiscoveryCandidatesInput,context: ArtifactContext,) {const { candidates } = discoveryCandidatesInputSchema.parse(input);- const pluginRoot = context.pluginRoot?.trim();- if (!pluginRoot) {- throw new Error(- "discovery candidates: the plugin runtime is not bound to this scan; " +- "restore the scan context before retrying.",- );- }-const inventoryComponents = [...discoveryComponents, "in_scope_files.txt"];- const candidateComponents = [- ...discoveryComponents,- "candidate_ledger.jsonl",- ];-- // Verify the inventory is a context-bound regular file before normalization.- await readArtifactText(- context,- inventoryComponents,- "discovery review inventory",- );- const inventoryPath = await artifactDestination(+ const inventory = await artifactSourcePath(context,inventoryComponents,"discovery review inventory",);const destination = await artifactDestination(context,- candidateComponents,+ [...discoveryComponents, "candidate_ledger.jsonl"],discoveryLabel,);- const temporaryDirectory = await fs.mkdtemp(- join(dirname(destination), ".discovery-candidates-"),+ const workerData: CandidateNormalizationInput = {+ repoRoot: context.repoRoot,+ scopePath: inventory,+ allowMissing: context.mode === "diff",+ sources:+ context.mode === "diff"+ ? await diffCandidateSources(context, inventory, candidates)+ : undefined,+ candidates,+ };+ // Inventory traversal and source reads must not block other MCP requests.+ const rows = await new Promise<ReturnType<typeof normalizeCandidateBatch>>(+ (resolve, reject) => {+ const worker = new Worker(+ createRequire(import.meta.url).resolve("./helpers.mjs"),+ { workerData },+ );+ worker.once("message", resolve);+ worker.once("error", reject);+ worker.once("exit", (code) =>+ reject(new Error(`Candidate normalizer exited with code ${code}.`)),+ );+ },+ );+ await replaceArtifactText(+ destination,+ rows.map((row) => `${stableJson(row)}\n`).join(""),);- const temporaryInput = join(temporaryDirectory, "candidates.jsonl");-- try {- await fs.chmod(temporaryDirectory, 0o700);- const content = candidates- .map((candidate) => `${JSON.stringify(candidate)}\n`)- .join("");- await fs.writeFile(temporaryInput, content, {- encoding: "utf8",- flag: "wx",- mode: 0o600,- });+ return { operation: "replace" as const, candidatesRecorded: rows.length };+}+async function diffCandidateSources(+ context: ArtifactContext,+ inventory: string,+ candidates: RawDiscoveryCandidate[],+): Promise<Map<string, CandidateSource>> {+ const target = context.targetContract?.diffTarget as+ Record<string, unknown> | undefined;+ if (+ !context.pluginRoot ||+ !target ||+ !["working_tree", "commit", "range"].includes(String(target.kind)) ||+ typeof target.baseRevision !== "string" ||+ !target.baseRevision ||+ typeof target.headRevision !== "string" ||+ !target.headRevision+ ) {+ throw new Error(+ "discovery candidates: the diff scan has no authoritative change set.",+ );+ }+ const nativePath = (value: string) =>+ process.platform === "win32" ? value.replaceAll("\\", "/") : value;+ const paths = decodeUtf8(await readFile(inventory))+ .split("\n")+ .flatMap((row, index, lines) =>+ row.endsWith("\r") && index < lines.length - 1+ ? process.platform === "win32"+ ? [row.slice(0, -1)]+ : [row, row.slice(0, -1)]+ : [row],+ )+ .filter(Boolean)+ .map(nativePath);+ const locations = candidates.flatMap((candidate, index) => {try {- await execFileAsync(- process.execPath,- [- join(pluginRoot, "mcp", "helpers.mjs"),- "normalize-candidates",- "--input",- temporaryInput,- "--out",- destination,- "--repo-root",- context.repoRoot,- "--in-scope-files",- inventoryPath,- ...(context.mode === "diff" ? ["--allow-missing-in-scope"] : []),- ],- {- cwd: pluginRoot,- encoding: "utf8",- shell: false,- },+ return candidate.locations.map((location) =>+ candidateRelativePath(location.path),);} catch (error) {- throw discoveryNormalizationError(error, [- [temporaryInput, "candidate input"],- [temporaryDirectory, "private candidate input"],- [inventoryPath, "the assigned review inventory"],- [destination, "the candidate set"],- [context.repoRoot, "the repository"],- [pluginRoot, "the plugin runtime"],- ]);+ throw new Error(+ `${discoveryLabel}: candidate input row ${index + 1}: ${(error as Error).message}`,+ { cause: error },+ );}-- const normalized = await readArtifactJsonl(- context,- candidateComponents,- discoveryLabel,- candidateSchemaV1,- );- return {- operation: "replace" as const,- candidatesRecorded: normalized.length,- };- } finally {- await fs- .rm(temporaryDirectory, { recursive: true, force: true })- .catch(() => undefined);- }+ });+ const python = context.pythonCommand ?? (await resolvePythonCommand());+ const output = await runPythonWithInput(+ python,+ [+ "-I",+ "-X",+ "utf8",+ "-c",+ `import json, subprocess, sys+from pathlib import Path+sys.path.insert(0, sys.argv[1])+from workbench_target import candidate_source_lines+try:+ json.dump(candidate_source_lines(Path(sys.argv[2]), **json.load(sys.stdin)), sys.stdout)+except subprocess.CalledProcessError as error:+ if error.stderr:+ sys.stderr.buffer.write(error.stderr)+ raise`,+ join(context.pluginRoot, "scripts"),+ context.repoRoot,+ ],+ JSON.stringify({+ diff_target: target,+ paths,+ locations,+ case_insensitive: process.platform === "win32",+ }),+ "Diff source reader",+ );+ return new Map(+ Object.entries(JSON.parse(output) as Record<string, CandidateSource>),+ );}/** Read the actual compact ledger, including records added by later shared phases. */@@ -214,35 +245,3 @@ export async function listCodexSecurityCandidates();return paginateArtifactRows(rows, page, discoveryLabel);}--function discoveryNormalizationError(- error: unknown,- privateValues: Array<readonly [string, string]>,-): Error {- const stderr =- error && typeof error === "object" && "stderr" in error- ? error.stderr- : undefined;- let detail =- typeof stderr === "string"- ? stderr.trim()- : Buffer.isBuffer(stderr)- ? stderr.toString("utf8").trim()- : "";-- if (!detail) {- return new Error(- `${discoveryLabel}: normalization could not be confirmed; ` +- "read the current candidate set before retrying.",- { cause: error },- );- }-- for (const [source, replacement] of [...privateValues].sort(- ([left], [right]) => right.length - left.length,- )) {- if (source) detail = detail.replaceAll(source, replacement);- }- detail = detail.replace(/^normalize_candidates:\s*/u, "");- return new Error(`${discoveryLabel}: ${detail}`, { cause: error });-}diff --git a/plugins/codex-security/mcp-app/src/artifact-io.ts b/plugins/codex-security/mcp-app/src/artifact-io.tsindex 4b5930db9..60e2d7122 100644--- a/plugins/codex-security/mcp-app/src/artifact-io.ts+++ b/plugins/codex-security/mcp-app/src/artifact-io.ts@@ -86,7 +86,7 @@ export async function readArtifactTextWithMetadata(}}-async function artifactSourcePath(+export async function artifactSourcePath(context: ArtifactContext,components: readonly string[],label: string,diff --git a/plugins/codex-security/mcp-app/src/helpers/normalize-candidates.ts b/plugins/codex-security/mcp-app/src/helpers/normalize-candidates.tsindex 16b42f6e5..b4d8d8249 100644--- a/plugins/codex-security/mcp-app/src/helpers/normalize-candidates.ts+++ b/plugins/codex-security/mcp-app/src/helpers/normalize-candidates.ts@@ -62,7 +62,7 @@ function compare(left: string, right: string): number {return left < right ? -1 : left > right ? 1 : 0;}-function stableJson(value: unknown): string {+export function stableJson(value: unknown): string {return JSON.stringify(value, jsonFields);}@@ -73,7 +73,8 @@ const readFile = (path: string) =>windows ? windowsFiles().readFile(fsPath(path)) : readFileSync(fsPath(path));const stat = (path: string) =>windows ? windowsFiles().stat(fsPath(path)) : statSync(fsPath(path));-const pathKey = (value: string) => (windows ? value.toLowerCase() : value);+export const pathKey = (value: string) =>+ windows ? value.toLowerCase() : value;function resolvedPath(value: string, strict = true): string {const path = resolveFilePath(fsPath(value), strict);@@ -98,7 +99,10 @@ function inside(path: string, root: string, allowMissing = false): string {return result.split(sep).join("/");}-function relativeFile(value: unknown, root: string): [string, string] {+export type CandidateSource =+ { path: string; lineCount?: number } | { error: "missing" | "not_file" };++export function candidateRelativePath(value: unknown): string {if (typeof value !== "string" || value === "" || value.includes("\0"))throw new Error("path: expected a non-empty repository-relative path");const raw = windows ? value.replaceAll("\\", "/") : value;@@ -110,6 +114,28 @@ function relativeFile(value: unknown, root: string): [string, string] {throw new Error("path: expected a repository-relative path without traversal",);+ return raw;+}++export function relativeFile(+ value: unknown,+ root: string,+ sources?: ReadonlyMap<string, CandidateSource>,+): [string, string, number?] {+ const raw = candidateRelativePath(value);+ const source = sources?.get(raw);+ if (source) {+ if ("error" in source)+ throw Object.assign(+ new Error(+ source.error === "missing"+ ? `path: no selected source for ${raw}`+ : "path: expected a regular file",+ ),+ source.error === "missing" ? { code: "ENOENT" } : {},+ );+ return [source.path, source.path, source.lineCount];+ }const path = resolvedPath(join(root, raw));const name = inside(path, root);if (!stat(path).isFile()) throw new Error("path: expected a regular file");@@ -120,13 +146,14 @@ function readScope(path: string,root: string,allowMissing: boolean,+ sources?: ReadonlyMap<string, CandidateSource>,): Set<string> {const contents = decodeUtf8(readFile(path));const lines = contents.split("\n");const listedRows = new Set(lines);const isScopeFile = (value: string): boolean => {try {- relativeFile(value, root);+ relativeFile(value, root, sources);return true;} catch {return false;@@ -183,7 +210,7 @@ function readScope(}if (line === "") continue;try {- scope.add(relativeFile(line, root)[0]);+ scope.add(relativeFile(line, root, sources)[0]);} catch (error) {if (allowMissing && (error as NodeJS.ErrnoException).code === "ENOENT") {try {@@ -245,6 +272,7 @@ function normalizeLocations(row: Row,root: string,lineCounts: Map<string, number>,+ sources?: ReadonlyMap<string, CandidateSource>,): Location[] {if (!Array.isArray(row.locations) || row.locations.length === 0)throw new Error("locations: expected a non-empty array");@@ -256,7 +284,11 @@ function normalizeLocations(.sort();if (unknown.length)throw new Error(`locations: unsupported fields ${unknown.join(", ")}`);- const [name, source] = relativeFile(item.path, root);+ const [name, source, selectedLineCount] = relativeFile(+ item.path,+ root,+ sources,+ );if (name.trim() === "" || name.includes("\\") || name.includes(":"))throw new Error("path: expected a safe repository-relative POSIX path");const start = positiveLine(item.start_line, "start_line");@@ -267,7 +299,7 @@ function normalizeLocations(if (end < start)throw new Error("end_line: must be greater than or equal to start_line");const key = pathKey(source);- if (!lineCounts.has(key)) {+ if (selectedLineCount === undefined && !lineCounts.has(key)) {const bytes = readFile(source);const contents = bytes.toString("latin1");const lines =@@ -275,7 +307,7 @@ function normalizeLocations((contents === "" || /[\r\n]$/u.test(contents) ? 1 : 0);lineCounts.set(key, lines);}- const count = lineCounts.get(key)!;+ const count = selectedLineCount ?? lineCounts.get(key)!;if (end > count)throw new Error(`line range ${start}-${end} exceeds ${name}:${count}`);if (typeof item.role !== "string" || !roles.includes(item.role))@@ -302,6 +334,7 @@ function normalizeCandidate(root: string,scope: Set<string>,lineCounts: Map<string, number>,+ sources?: ReadonlyMap<string, CandidateSource>,): Candidate {const unknown = Object.keys(row).filter((key) => !fields.has(key))@@ -309,7 +342,7 @@ function normalizeCandidate(if (unknown.length)throw new Error(`unsupported fields ${unknown.join(", ")}`);if ("candidate_id" in row) textField(row, "candidate_id");- const locations = normalizeLocations(row, root, lineCounts);+ const locations = normalizeLocations(row, root, lineCounts, sources);if (!locations.some((item) => scope.has(item.path)))throw new Error("locations: expected at least one in-scope file");const result: Candidate = {@@ -351,6 +384,69 @@ function combine(groups: Map<string, Candidate[]>) {});}+export function createCandidateNormalizer(+ repoRoot: string,+ scopePath: string,+ allowMissing = false,+ sources?: ReadonlyMap<string, CandidateSource>,+) {+ const root = resolvedPath(repoRoot);+ if (!stat(root).isDirectory())+ throw new Error("--repo-root: expected a directory");+ const scope = readScope(scopePath, root, allowMissing, sources);+ const lineCounts = new Map<string, number>();+ const groups = new Map<string, Candidate[]>();+ return {+ add(row: unknown) {+ if (!object(row)) throw new Error("expected a JSON object");+ const candidate = normalizeCandidate(+ row,+ root,+ scope,+ lineCounts,+ sources,+ );+ const key = stableJson({+ cwe_ids: candidate.cwe_ids,+ locations: candidate.locations,+ instance: candidate.instance ?? null,+ });+ const group = groups.get(key) ?? [];+ group.push(candidate);+ groups.set(key, group);+ },+ finish: () => combine(groups),+ };+}++export interface CandidateNormalizationInput {+ repoRoot: string;+ scopePath: string;+ allowMissing: boolean;+ sources?: ReadonlyMap<string, CandidateSource>;+ candidates: unknown[];+}++export function normalizeCandidateBatch(input: CandidateNormalizationInput) {+ const normalizer = createCandidateNormalizer(+ input.repoRoot,+ input.scopePath,+ input.allowMissing,+ input.sources,+ );+ for (const [index, candidate] of input.candidates.entries()) {+ try {+ normalizer.add(candidate);+ } catch (error) {+ throw new Error(+ `discovery candidates: candidate input row ${index + 1}: ${(error as Error).message}`,+ { cause: error },+ );+ }+ }+ return normalizer.finish();+}+function argumentsFor(args: string[]) {const { values, tokens } = parseArgs({args,@@ -415,40 +511,27 @@ export function normalizeCandidatesCommand(throw new Error("--out: must not also be an input");if (pathKey(output) === pathKey(scopePath))throw new Error("--out: must not replace --in-scope-files");- const scope = readScope(- scopePath,+ const normalizer = createCandidateNormalizer(root,+ scopePath,values["allow-missing-in-scope"] ?? false,);- const lineCounts = new Map<string, number>();- const groups = new Map<string, Candidate[]>();let rowCount = 0;for (const source of inputs) {const lines = decodeUtf8(readFile(source)).split(/\r?\n/u);for (const [index, line] of lines.entries()) {if (line.trim() === "") continue;- let candidate: Candidate;try {- const row: unknown = JSON.parse(line);- if (!object(row)) throw new Error("expected a JSON object");- candidate = normalizeCandidate(row, root, scope, lineCounts);+ normalizer.add(JSON.parse(line));} catch (error) {throw new Error(`${source} row ${index + 1}: ${(error as Error).message}`,);}- const key = stableJson({- cwe_ids: candidate.cwe_ids,- locations: candidate.locations,- instance: candidate.instance ?? null,- });- const group = groups.get(key) ?? [];- group.push(candidate);- groups.set(key, group);rowCount++;}}- const combined = combine(groups);+ const combined = normalizer.finish();if (windows) windowsFiles().mkdir(fsPath(dirname(output)));else mkdirSync(fsPath(dirname(output)), { recursive: true });const temporary = fsPath(diff --git a/plugins/codex-security/mcp-app/src/python_command.ts b/plugins/codex-security/mcp-app/src/python_command.tsindex 24a6027aa..aef8c7927 100644--- a/plugins/codex-security/mcp-app/src/python_command.ts+++ b/plugins/codex-security/mcp-app/src/python_command.ts@@ -1,3 +1,4 @@+import { spawn } from "node:child_process";import { constants, promises as fs } from "node:fs";import { homedir } from "node:os";import path from "node:path";@@ -105,3 +106,39 @@ export function missingPythonHelperMessage(}return MISSING_PYTHON_HELPER_MESSAGE;}++/** Run an existing Python helper with in-memory input and preserve its output. */+export function runPythonWithInput(+ python: string,+ args: string[],+ input: string | Buffer,+ label: string,+): Promise<string> {+ return new Promise((resolve, reject) => {+ const child = spawn(python, args, {+ stdio: ["pipe", "pipe", "pipe"],+ windowsHide: true,+ });+ const output: string[] = [],+ errors: string[] = [];+ child.stdout+ .setEncoding("utf8")+ .on("data", (chunk: string) => output.push(chunk));+ child.stderr+ .setEncoding("utf8")+ .on("data", (chunk: string) => errors.push(chunk));+ let inputError: Error | undefined;+ child.on("error", reject);+ child.stdin.on("error", (error: Error) => {+ inputError = error;+ });+ child.on("close", (code, signal) => {+ const detail = errors.join("").trim();+ if (code !== 0 && detail) reject(new Error(detail));+ else if (inputError) reject(inputError);+ else if (code === 0) resolve(output.join(""));+ else reject(new Error(`${label} exited with ${signal ?? code}.`));+ });+ child.stdin.end(input);+ });+}diff --git a/plugins/codex-security/mcp-app/src/threat-model-document.ts b/plugins/codex-security/mcp-app/src/threat-model-document.tsindex 3ded2045d..5626f1bda 100644--- a/plugins/codex-security/mcp-app/src/threat-model-document.ts+++ b/plugins/codex-security/mcp-app/src/threat-model-document.ts@@ -1,4 +1,3 @@-import { spawn } from "node:child_process";import { join } from "node:path";import type { ArtifactContext } from "./artifact-context.js";import {@@ -6,7 +5,7 @@ import {readArtifactJsonObject,replaceArtifactText,} from "./artifact-io.js";-import { resolvePythonCommand } from "./python_command.js";+import { resolvePythonCommand, runPythonWithInput } from "./python_command.js";import { asRecord } from "./record.js";const pendingDocuments = new Map<string, Promise<void>>();@@ -80,38 +79,16 @@ function renderThreatModel(pluginRoot: string,input: Record<string, unknown>,): Promise<string> {- return new Promise((resolve, reject) => {- const child = spawn(- python,- [- "-I",- "-X",- "utf8",- join(pluginRoot, "scripts", "threat_model_projection.py"),- "--input-json-stdin",- ],- { stdio: ["pipe", "pipe", "pipe"], windowsHide: true },- );- const output: string[] = [];- const errors: string[] = [];- child.stdout- .setEncoding("utf8")- .on("data", (chunk: string) => output.push(chunk));- child.stderr- .setEncoding("utf8")- .on("data", (chunk: string) => errors.push(chunk));- child.on("error", reject);- child.stdin.on("error", reject);- child.on("close", (code, signal) => {- if (code === 0) resolve(output.join(""));- else- reject(- new Error(- errors.join("").trim() ||- `Threat-model renderer exited with ${signal ?? code}.`,- ),- );- });- child.stdin.end(JSON.stringify(input));- });+ return runPythonWithInput(+ python,+ [+ "-I",+ "-X",+ "utf8",+ join(pluginRoot, "scripts", "threat_model_projection.py"),+ "--input-json-stdin",+ ],+ JSON.stringify(input),+ "Threat-model renderer",+ );}diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_discovery.ts b/plugins/codex-security/mcp-app/tests/test_artifact_discovery.tsindex 849fd56df..acc3e75f6 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_discovery.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_discovery.ts@@ -6,16 +6,19 @@ import type {CompactDiscoveryCandidate,} from "../src/artifact-discovery.js";import assert from "node:assert/strict";+import { execFileSync } from "node:child_process";+import fs from "node:fs";import {- copyFile,mkdir,readFile,readdir,+ rm,symlink,writeFile,} from "node:fs/promises";import path from "node:path";-import { build } from "esbuild";+import { syncBuiltinESMExports } from "node:module";+import { mock } from "node:test";import { importSource } from "./import-module.ts";const {@@ -27,7 +30,17 @@ const {workbenchDiscoveryCandidatesInputSchema,workbenchListCodexSecurityCandidatesInputSchema,} = await importSource(- new URL("../src/artifact-discovery.ts", import.meta.url).pathname,+ path.join(import.meta.dirname, "../src/artifact-discovery.ts"),+ {+ define: {+ "import.meta.url": JSON.stringify(+ new URL(+ "../../../../sdk/typescript/_bundled_plugin/mcp/server.mjs",+ import.meta.url,+ ).href,+ ),+ },+ },);const pluginRoot = path.join(import.meta.dirname, "../../");@@ -83,22 +96,6 @@ const root = await rootDirectories.create("security-artifact-discovery-");const runtimePluginRoot = path.join(root, "plugin");const repoRoot = path.join(root, "repository");try {- await build({- bundle: true,- entryPoints: [path.join(pluginRoot, "mcp-app", "helpers-main.ts")],- outfile: path.join(runtimePluginRoot, "mcp", "helpers.mjs"),- format: "esm",- platform: "node",- });- if (process.platform === "win32") {- const target = `win32-${process.arch}`;- const destination = path.join(runtimePluginRoot, "mcp", "native", target);- await mkdir(destination, { recursive: true });- await copyFile(- path.join(pluginRoot, "native", "prebuilt", target, "windows.node"),- path.join(destination, "windows.node"),- );- }await mkdir(path.join(repoRoot, "src"), { recursive: true });await mkdir(path.join(repoRoot, "support"), { recursive: true });await writeFile(@@ -117,6 +114,7 @@ try {const scan = await createContext("scan", "scan");await verifyInputSchema();await verifyNormalizationAndPagination(scan);+ await verifyNormalizationRunsOffThread(scan);await verifyReaderPreservesSharedPhaseRecords(scan);await verifyNormalizerFailuresPreserveOutput(scan);await verifyDiffInventoryAllowsDeletedFiles();@@ -341,6 +339,29 @@ async function verifyNormalizerFailuresPreserveOutput();const original = await readFile(destination, "utf8");+ await assert.rejects(+ recordCodexSecurityDiscoveryCandidates(+ {+ candidates: [+ rawCandidate(),+ rawCandidate({+ locations: [+ { path: "src/missing.ts", start_line: 1, role: "sink" },+ ],+ }),+ ],+ },+ context,+ ),+ (error) =>+ error instanceof Error &&+ /candidate input row 2:.*(?:ENOENT|Windows filesystem error 2)/u.test(+ error.message,+ ) &&+ error.message.includes("missing.ts"),+ );+ assert.equal(await readFile(destination, "utf8"), original);+await assert.rejects(recordCodexSecurityDiscoveryCandidates({@@ -392,51 +413,308 @@ async function verifyNormalizerFailuresPreserveOutput();assert.equal(await readFile(destination, "utf8"), original);+ const inventory = path.join(path.dirname(destination), "in_scope_files.txt");+ const scope = await readFile(inventory);+ await writeFile(inventory, Buffer.from([0xff]));await assert.rejects(- recordCodexSecurityDiscoveryCandidates(- {- candidates: [rawCandidate()],- },+ recordCodexSecurityDiscoveryCandidates({ candidates: [] }, context),+ /UTF-8|encoded data/,+ );+ assert.equal(await readFile(destination, "utf8"), original);+ await writeFile(inventory, scope);++ assert.deepEqual(+ await recordCodexSecurityDiscoveryCandidates(+ { candidates: [rawCandidate()] },{ ...context, pluginRoot: undefined },),- /plugin runtime is not bound/u,+ { operation: "replace", candidatesRecorded: 1 },);- assert.equal(await readFile(destination, "utf8"), original);+}++async function verifyNormalizationRunsOffThread(context: ArtifactContext) {+ // POSIX normalization uses these synchronous APIs; workers have their own+ // built-in modules, so reads there remain available while this thread refuses.+ if (process.platform === "win32") return;+ const assertOutsideScan = (file: fs.PathOrFileDescriptor) => {+ const value = Buffer.isBuffer(file) ? file.toString("utf8") : String(file);+ assert.equal(value.startsWith(context.repoRoot), false, value);+ assert.equal(value.startsWith(context.root), false, value);+ };+ const read = fs.readFileSync;+ const realpath = fs.realpathSync.native;+ const readMock = mock.method(+ fs,+ "readFileSync",+ (...args: Parameters<typeof read>) => {+ assertOutsideScan(args[0]);+ return read(...args);+ },+ );+ const realpathMock = mock.method(+ fs.realpathSync,+ "native",+ (...args: Parameters<typeof realpath>) => {+ assertOutsideScan(args[0]);+ return realpath(...args);+ },+ );+ syncBuiltinESMExports();+ try {+ assert.deepEqual(+ await recordCodexSecurityDiscoveryCandidates(+ { candidates: [rawCandidate()] },+ context,+ ),+ { operation: "replace", candidatesRecorded: 1 },+ );+ } finally {+ readMock.mock.restore();+ realpathMock.mock.restore();+ syncBuiltinESMExports();+ }}async function verifyDiffInventoryAllowsDeletedFiles() {const context = await createContext("diff-output", "scan");+ const repository = path.join(root, "diff-repository");+ await mkdir(repository);+ const git = (...args: string[]) =>+ execFileSync("git", ["-C", repository, ...args], {+ encoding: "utf8",+ }).trim();+ const commit = (stage = true) => {+ if (stage) git("add", "-A");+ git(+ "-c",+ "user.name=Synthetic",+ "-c",+ "[email protected]",+ "commit",+ "-qm",+ "fixture",+ );+ return git("rev-parse", "HEAD");+ };+ git("init", "-q");+ await writeFile(+ path.join(repository, "deleted.ts"),+ "one\rtwo\r\nthree\nfour",+ );+ await writeFile(path.join(repository, "changed.ts"), "one\ntwo\n");+ await writeFile(path.join(repository, "support.ts"), "support\n");+ const linkBlob = execFileSync(+ "git",+ ["-C", repository, "hash-object", "-w", "--stdin"],+ { input: "support.ts", encoding: "utf8" },+ ).trim();+ git("add", "-A");+ git(+ "update-index",+ "--add",+ "--cacheinfo",+ `120000,${linkBlob},base-link.ts`,+ );+ const baseRevision = commit(false);+ await rm(path.join(repository, "deleted.ts"));+ await writeFile(path.join(repository, "changed.ts"), "one\ntwo\nthree\n");+ await writeFile(path.join(repository, "head-only.ts"), "one\ntwo\n");+ git("add", "-A");+ git("update-index", "--add", "--cacheinfo", `120000,${linkBlob},link.ts`);+ const headRevision = commit(false);+ await rm(path.join(repository, "head-only.ts"));+ await rm(path.join(repository, "support.ts"));+ await writeFile(path.join(repository, "changed.ts"), "current\n");+ await writeFile(+ path.join(repository, "unrelated.ts"),+ "unrelated current source\n",+ );+ commit();+ if (process.platform === "win32") {+ git("mv", "changed.ts", "temporary.ts");+ git("mv", "temporary.ts", "CHANGED.ts");+ commit();+ }const inventory = path.join(context.root,"artifacts","02_discovery","in_scope_files.txt",);- await writeFile(- inventory,- "src/deleted-guard.ts\nsrc/routes.ts\nsrc/query.ts\n",- );-- await assert.rejects(+ await writeFile(inventory, "deleted.ts\r\nchanged.ts\r\nhead-only.ts\r\n");+ const record = (scan: ArtifactContext, paths: [string, number][]) =>recordCodexSecurityDiscoveryCandidates(- { candidates: [rawCandidate()] },- context,- ),- /in-scope file row 1/u,- );-- const diffContext = { ...context, mode: "diff" };- assert.deepEqual(- await recordCodexSecurityDiscoveryCandidates(- { candidates: [rawCandidate()] },- diffContext,- ),- { operation: "replace", candidatesRecorded: 1 },- );- assert.equal(- (await listCodexSecurityCandidates({}, diffContext)).rows.length,- 1,- );+ {+ candidates: [+ rawCandidate({+ locations: paths.map(([path, end_line]) => ({+ path,+ start_line: 1,+ end_line,+ role: "sink",+ })),+ }),+ ],+ },+ scan,+ );+ for (const kind of ["commit", "range", "working_tree"]) {+ if (kind === "working_tree") {+ git("reset", "--hard", baseRevision);+ await rm(path.join(repository, "deleted.ts"));+ await writeFile(path.join(repository, "changed.ts"), "current\n");+ }+ const scan: ArtifactContext = {+ ...context,+ repoRoot: repository,+ pluginRoot,+ pythonCommand: undefined,+ mode: "diff",+ targetContract: {+ diffTarget: {+ kind,+ baseRevision,+ headRevision: kind === "working_tree" ? baseRevision : headRevision,+ },+ },+ };+ if (kind === "working_tree")+ await writeFile(inventory, "deleted.ts\nchanged.ts\n");+ const sources: [string, number][] =+ kind === "working_tree"+ ? [+ ["deleted.ts", 4],+ ["changed.ts", 1],+ ["support.ts", 1],+ ]+ : [+ ["deleted.ts", 4],+ ["changed.ts", 3],+ ["head-only.ts", 2],+ ["support.ts", 1],+ ];+ assert.deepEqual(await record(scan, sources), {+ operation: "replace",+ candidatesRecorded: 1,+ });+ const destination = path.join(+ path.dirname(inventory),+ "candidate_ledger.jsonl",+ );+ const accepted = await readFile(destination, "utf8");+ await assert.rejects(+ record(+ { ...scan, pythonCommand: path.join(root, "missing-python") },+ sources,+ ),+ { code: "ENOENT" },+ );+ assert.equal(await readFile(destination, "utf8"), accepted);+ for (const invalid of [+ "../outside.ts",+ path.join(repository, "changed.ts"),+ ]) {+ await assert.rejects(+ recordCodexSecurityDiscoveryCandidates(+ {+ candidates: [+ rawCandidate({+ locations: [+ { path: "changed.ts", start_line: 1, role: "sink" },+ ],+ }),+ rawCandidate({+ locations: [{ path: invalid, start_line: 1, role: "sink" }],+ }),+ ],+ },+ scan,+ ),+ /candidate input row 2: path: expected a repository-relative path without traversal/u,+ );+ assert.equal(await readFile(destination, "utf8"), accepted);+ }+ await assert.rejects(+ record(+ {+ ...scan,+ targetContract: {+ diffTarget: {+ kind,+ baseRevision: "missing-revision",+ headRevision,+ },+ },+ },+ sources,+ ),+ /fatal: (?:ambiguous argument|bad revision)/,+ );+ for (const [file, line] of sources.filter(+ ([file]) => file !== "support.ts",+ )) {+ await assert.rejects(record(scan, [[file, line + 1]]), /line range/);+ assert.equal(await readFile(destination, "utf8"), accepted);+ }+ for (const file of [+ "missing.ts",+ "link.ts",+ "base-link.ts",+ "../outside.ts",+ ]) {+ await assert.rejects(record(scan, [[file, 1]]));+ assert.equal(await readFile(destination, "utf8"), accepted);+ }+ await assert.rejects(+ record(scan, [["support.ts", 1]]),+ /at least one in-scope/,+ );+ if (kind === "working_tree") {+ git("rm", "--cached", "changed.ts");+ await writeFile(+ path.join(repository, ".git", "info", "exclude"),+ "changed.ts\nCHANGED.ts\n",+ );+ await record(scan, [["changed.ts", 2]]);+ await assert.rejects(record(scan, [["changed.ts", 3]]), /line range/);+ await mkdir(path.join(repository, "deleted.ts"));+ await record(scan, [["deleted.ts", 4]]);+ await assert.rejects(record(scan, [["deleted.ts", 5]]), /line range/);+ await rm(path.join(repository, "deleted.ts"), { recursive: true });+ }+ if (process.platform === "win32") {+ for (const prefix of ["", "./", ".\\", ".//./"]) {+ await record(scan, [+ [`${prefix}SUPPORT.TS`, 1],+ [`${prefix}DELETED.TS`, 4],+ [`${prefix}CHANGED.ts`, kind === "working_tree" ? 2 : 3],+ ]);+ const candidate = JSON.parse(await readFile(destination, "utf8"));+ assert.ok(+ candidate.locations.some(+ (location: { path: string }) => location.path === "deleted.ts",+ ),+ );+ if (kind !== "working_tree")+ assert.ok(+ candidate.locations.some(+ (location: { path: string }) => location.path === "changed.ts",+ ),+ );+ }+ if (kind !== "working_tree") {+ await assert.rejects(+ record(scan, [["UNRELATED.TS", 1]]),+ /no selected source/,+ );+ }+ }+ if (kind !== "working_tree")+ await assert.rejects(+ record(scan, [["unrelated.ts", 1]]),+ /no selected source/,+ );+ }}async function verifyReaderPreservesSharedPhaseRecords(diff --git a/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.ts b/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.tsindex 2bab97e39..ae019c10d 100644--- a/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.ts+++ b/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.ts@@ -46,6 +46,7 @@ import {writeFile,} from "node:fs/promises";import path from "node:path";+import { pathToFileURL } from "node:url";import { Client } from "@modelcontextprotocol/sdk/client/index.js";import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";@@ -62,7 +63,9 @@ try {await buildServer(runtimeBundle, {define: {__dirname: JSON.stringify(path.join(bundledPluginRoot, "mcp")),- "import.meta.url": "__filename",+ "import.meta.url": JSON.stringify(+ pathToFileURL(path.join(bundledPluginRoot, "mcp", "server.mjs")).href,+ ),},logOverride: { "empty-import-meta": "silent" },target: "node20",diff --git a/plugins/codex-security/mcp-app/tests/test_python_command.ts b/plugins/codex-security/mcp-app/tests/test_python_command.tsindex b0ddc0fc2..80b78c3e7 100644--- a/plugins/codex-security/mcp-app/tests/test_python_command.ts+++ b/plugins/codex-security/mcp-app/tests/test_python_command.ts@@ -13,6 +13,7 @@ import {isUsablePythonExecutable,missingPythonHelperMessage,resolvePythonCommand,+ runPythonWithInput,} from "../src/python_command.ts";const windowsHome = "C:\\Users\\fixture";@@ -160,9 +161,66 @@ try {await resolvePythonCommand({ configuredPython: " /custom/python " }),"/custom/python",);+ const input = "λ😀\n".repeat(250_000);+ assert.equal(+ await runPythonWithInput(+ await resolvePythonCommand(),+ ["-e", "process.stdin.pipe(process.stdout)"],+ Buffer.from(input),+ "Fixture helper",+ ),+ input,+ );delete process.env.CODEX_SECURITY_PYTHON_COMMAND;assert.equal(await resolvePythonCommand(), fileCandidate);+ const diagnostic = "--provider-error EACCES: λ😀\npermission denied";+ await assert.rejects(+ runPythonWithInput(+ process.execPath,+ [+ "-e",+ `process.stdin.resume(); process.stdin.on('end', () => { const text = Buffer.from(${JSON.stringify(diagnostic)}); process.stderr.write(text.subarray(0, 26)); setTimeout(() => { process.stderr.write(text.subarray(26)); process.exitCode = 2; }, 1); });`,+ ],+ "",+ "Fixture helper",+ ),+ (error: Error) => error.message === diagnostic,+ );+ await assert.rejects(+ runPythonWithInput(+ process.execPath,+ [+ "-e",+ `process.stderr.write(${JSON.stringify(diagnostic)}); process.exit(1);`,+ ],+ input,+ "Fixture helper",+ ),+ (error: Error) => error.message === diagnostic,+ );+ await assert.rejects(+ runPythonWithInput(+ process.execPath,+ ["-e", "process.exit(0)"],+ input,+ "Fixture helper",+ ),+ (error: NodeJS.ErrnoException) => typeof error.code === "string",+ );+ await assert.rejects(+ runPythonWithInput(+ path.join(executableFixtureRoot, "missing"),+ [],+ "",+ "Fixture helper",+ ),+ { code: "ENOENT" },+ );if (process.platform !== "win32") {+ await assert.rejects(+ runPythonWithInput(fileCandidate, [], "", "Fixture helper"),+ { code: "EACCES" },+ );assert.equal(await isUsablePythonExecutable(fileCandidate, "linux"), false);await chmod(fileCandidate, 0o755);assert.equal(await isUsablePythonExecutable(fileCandidate, "linux"), true);diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.pyindex 034368830..9ea0de267 100644--- a/plugins/codex-security/scripts/workbench_target.py+++ b/plugins/codex-security/scripts/workbench_target.py@@ -6,6 +6,7 @@ import argparseimport codecsimport hashlibimport os+import reimport shutilimport sqlite3import stat@@ -13,7 +14,7 @@ import subprocessimport sysimport tempfilefrom concurrent.futures import ThreadPoolExecutor-from pathlib import Path+from pathlib import Path, PurePosixPathfrom typing import Any, BinaryIO# Some plugin hosts launch Python with safe-path isolation enabled.@@ -301,6 +302,80 @@ def git_command(return subprocess.CompletedProcess(full_command, 127, empty_output, empty_output)+def candidate_source_lines(+ target: Path,+ diff_target: dict[str, str],+ paths: list[str],+ locations: list[str],+ case_insensitive: bool = False,+) -> dict[str, dict[str, Any]]:+ """Resolve candidate sources in the same selected view as the diff inventory."""+ from generate_rank_input import git_changed_paths++ base, head = diff_target["baseRevision"], diff_target["headRevision"]+ local = diff_target["kind"] == "working_tree"+ changed = {+ path.relative_to(target).as_posix(): status+ for path, status in git_changed_paths(+ target, base, head, "local-patch" if local else "revisions"+ )+ }+ selected_names = set(changed)+ aliases: dict[str, str | None] = {}+ if case_insensitive:+ if not local:+ tree = git_command(target, "ls-tree", "-r", "-t", "-z", "--name-only", head, text=False)+ tree.check_returncode()+ selected_names.update(os.fsdecode(name) for name in tree.stdout.split(b"\0") if name)+ for name in selected_names:+ key = name.lower()+ aliases[key] = None if key in aliases and aliases[key] != name else name+ location_set = set(locations)+ result: dict[str, dict[str, Any]] = {}+ counts: dict[str, int] = {}+ for raw in dict.fromkeys([*paths, *locations]):+ path = PurePosixPath(raw)+ if not raw or path.is_absolute() or ".." in path.parts or "\0" in raw:+ raise ValueError("path: expected a repository-relative path without traversal")+ name = path.as_posix()+ if case_insensitive and name not in selected_names:+ selected = aliases.get(name.lower(), name)+ if selected is None:+ result[raw] = {"error": "missing"}+ continue+ name = selected+ deleted = changed.get(name) == "D"+ if local and not deleted:+ continue+ revision = base if deleted else head+ if name not in changed:+ entry = git_command(target, "ls-tree", "-z", revision, "--", name, text=False)+ entry.check_returncode()+ metadata = next(+ (+ row.partition(b"\t")[0].split()+ for row in entry.stdout.split(b"\0")+ if row.partition(b"\t")[2] == os.fsencode(name)+ ),+ [],+ )+ if not metadata or not metadata[0].startswith(b"100"):+ result[raw] = {"error": "not_file" if metadata else "missing"}+ continue+ source: dict[str, Any] = {"path": name}+ if raw in location_set:+ if name not in counts:+ blob = git_command(target, "cat-file", "blob", f"{revision}:{name}", text=False)+ blob.check_returncode()+ data = blob.stdout+ counts[name] = len(re.split(rb"\r\n|[\r\n]", data)) - int(+ not data or data.endswith((b"\r", b"\n"))+ )+ source["lineCount"] = counts[name]+ result[raw] = source+ return result++def _update_digest_field_header(digest: Any, label: bytes, value_size: int) -> None:digest.update(len(label).to_bytes(4, "big"))digest.update(label)diff --git a/plugins/codex-security/tests/test_workbench_target.py b/plugins/codex-security/tests/test_workbench_target.pyindex 49fed90cd..a005430a6 100644--- a/plugins/codex-security/tests/test_workbench_target.py+++ b/plugins/codex-security/tests/test_workbench_target.py@@ -671,3 +671,105 @@ def test_copy_retains_alias_rooted_gitlink_exclusions(assert len(calls) == 1assert (copied / "fixture.py").read_text() == "synthetic = True\n"assert not (copied / "submodule").exists()++[email protected]("replacement", ["directory", "ignored_file", "link"])[email protected]("case_insensitive", [False, True])+def test_deleted_candidate_sources_keep_base_blob_when_path_is_recreated(+ tmp_path: Path, replacement: str, case_insensitive: bool+) -> None:+ target = tmp_path / "repository"+ initialize_git_repository(target)+ source = target / "deleted.ts"+ source.write_bytes(b"one\rtwo\r\nthree\nfour")+ subprocess.run(["git", "add", "deleted.ts"], cwd=target, check=True)+ subprocess.run(["git", "commit", "-qm", "selected source"], cwd=target, check=True)+ revision = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=target, text=True).strip()+ subprocess.run(["git", "rm", "-q", "deleted.ts"], cwd=target, check=True)+ (target / ".git" / "info" / "exclude").write_text("deleted.ts\n")+ if replacement == "directory":+ source.mkdir()+ elif replacement == "ignored_file":+ source.write_text("replacement\n")+ else:+ outside = tmp_path / "outside"+ outside.mkdir()+ (outside / "private.txt").write_text("Synthetic unrelated content.\n")+ if os.name == "nt":+ subprocess.run(+ ["cmd", "/c", "mklink", "/J", str(source), str(outside)],+ check=True,+ capture_output=True,+ )+ else:+ source.symlink_to(outside, target_is_directory=True)+ from generate_rank_input import git_changed_paths++ assert dict(git_changed_paths(target, revision, revision, "local-patch"))[source] == "D"+ sources = WORKBENCH_TARGET["candidate_source_lines"](+ target,+ {"kind": "working_tree", "baseRevision": revision, "headRevision": revision},+ ["deleted.ts"],+ ["deleted.ts", "DELETED.TS"],+ case_insensitive=case_insensitive,+ )+ expected = {"deleted.ts": {"path": "deleted.ts", "lineCount": 4}}+ if case_insensitive:+ expected["DELETED.TS"] = expected["deleted.ts"]+ assert sources == expected++[email protected]("case_insensitive", [False, True])+def test_candidate_aliases_use_selected_tree_and_preserve_ambiguity(+ tmp_path: Path, case_insensitive: bool+) -> None:+ target = tmp_path / "repository"+ initialize_git_repository(target)+ support = target / "Support.ts"+ support.write_text("support\nsecond\n")+ subprocess.run(["git", "add", "Support.ts"], cwd=target, check=True)+ subprocess.run(["git", "commit", "-qm", "support source"], cwd=target, check=True)+ base = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=target, text=True).strip()+ for name, mode, content in (+ ("Foo.ts", "100644", b"upper\n"),+ ("foo.ts", "100644", b"lower\nsecond\nthird\n"),+ ("folder/file.ts", "100644", b"nested\n"),+ ("link.ts", "120000", b"Support.ts"),+ ):+ blob = (+ subprocess.check_output(+ ["git", "hash-object", "-w", "--stdin"], cwd=target, input=content+ )+ .decode()+ .strip()+ )+ subprocess.run(+ ["git", "update-index", "--add", "--cacheinfo", f"{mode},{blob},{name}"],+ cwd=target,+ check=True,+ )+ subprocess.run(["git", "commit", "-qm", "selected tree"], cwd=target, check=True)+ head = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=target, text=True).strip()+ support.unlink()+ subprocess.run(+ ["git", "update-index", "--force-remove", "Support.ts", "Foo.ts"], cwd=target, check=True+ )+ subprocess.run(["git", "commit", "-qm", "later tree"], cwd=target, check=True)+ (target / "foo.ts").write_text("different current content\n")+ requested = ["Foo.ts", "foo.ts", "FOO.ts", "SUPPORT.TS", "./SUPPORT.TS", "LINK.TS", "FOLDER"]+ sources = WORKBENCH_TARGET["candidate_source_lines"](+ target,+ {"kind": "range", "baseRevision": base, "headRevision": head},+ ["Foo.ts", "foo.ts"],+ requested,+ case_insensitive=case_insensitive,+ )+ assert sources["Foo.ts"] == {"path": "Foo.ts", "lineCount": 1}+ assert sources["foo.ts"] == {"path": "foo.ts", "lineCount": 3}+ assert sources["FOO.ts"] == {"error": "missing"}+ for name in ("SUPPORT.TS", "./SUPPORT.TS"):+ assert sources[name] == (+ {"path": "Support.ts", "lineCount": 2} if case_insensitive else {"error": "missing"}+ )+ for name in ("LINK.TS", "FOLDER"):+ assert sources[name] == {"error": "not_file" if case_insensitive else "missing"}