Skip to content

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

fix(deep-scan): preserve selected scope through recovery (#1602)

Michael D'Angeloecc637beee27
diff --git a/plugins/codex-security/mcp-app/artifact-writer-main.ts b/plugins/codex-security/mcp-app/artifact-writer-main.ts
index b6507307b..74783ac55 100644
--- a/plugins/codex-security/mcp-app/artifact-writer-main.ts
+++ b/plugins/codex-security/mcp-app/artifact-writer-main.ts
@@ -43,20 +43,24 @@ export async function createCodexSecurityArtifactWriterServer(
const scope = environment.CODEX_SECURITY_SCOPE
? environment.CODEX_SECURITY_SCOPE
: undefined;
- const includePaths: unknown =
+ const scopeInput: unknown =
environment.CODEX_SECURITY_INCLUDE_PATHS_FILE === undefined
? undefined
: JSON.parse(
await readFile(environment.CODEX_SECURITY_INCLUDE_PATHS_FILE, "utf8"),
);
- if (
- includePaths !== undefined &&
- (!Array.isArray(includePaths) ||
- includePaths.some((path) => typeof path !== "string"))
- ) {
- throw new Error(
- "The coordinator-bound include paths must be an array of paths.",
- );
+ // Older coordinators sent only the requested paths, with lexical scope matching.
+ const includePaths = isRecord(scopeInput) ? scopeInput.paths : scopeInput;
+ const filePaths = isRecord(scopeInput) ? scopeInput.filePaths : undefined;
+ for (const paths of [includePaths, filePaths]) {
+ if (
+ paths !== undefined &&
+ (!Array.isArray(paths) || paths.some((path) => typeof path !== "string"))
+ ) {
+ throw new Error(
+ "The coordinator-bound include paths must be an array of paths.",
+ );
+ }
}
const pluginRoot = environment.CODEX_SECURITY_PLUGIN_ROOT
? environment.CODEX_SECURITY_PLUGIN_ROOT
@@ -75,6 +79,7 @@ export async function createCodexSecurityArtifactWriterServer(
...defined("scanId", scanId),
...defined("scope", scope),
...defined("includePaths", includePaths as string[] | undefined),
+ ...defined("filePaths", filePaths as string[] | undefined),
...defined("pluginRoot", pluginRoot),
...defined("pythonCommand", pythonCommand),
...defined("deepReducer", deepReducer),
diff --git a/plugins/codex-security/mcp-app/src/artifact-io.ts b/plugins/codex-security/mcp-app/src/artifact-io.ts
index ca4255ed7..c83b0eb78 100644
--- a/plugins/codex-security/mcp-app/src/artifact-io.ts
+++ b/plugins/codex-security/mcp-app/src/artifact-io.ts
@@ -25,6 +25,7 @@ export interface ArtifactContext {
scanId?: string;
scope?: string;
includePaths?: readonly string[];
+ filePaths?: readonly string[];
pluginRoot?: string;
pythonCommand?: string;
targetContract?: Readonly<Record<string, unknown>>;
diff --git a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts b/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
index 8fde77fb2..716d00905 100644
--- a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
+++ b/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
@@ -194,6 +194,7 @@ export async function recordCodexSecurityScanDraftViaWorkbench(
? findingsWithinScope(
reconciled.findings,
scope.includePaths as string[],
+ trustedScope.filePaths as string[] | undefined,
)
: reconciled.findings;
const findings = buildFindings(scopedFindings, context.mode);
@@ -311,11 +312,19 @@ export async function recordCodexSecurityWorkerScanDraft(
const includePaths = context.includePaths ?? [context.scope || "."];
let scoped = {
...parsed,
- findings: findingsWithinScope(parsed.findings, includePaths),
+ findings: findingsWithinScope(
+ parsed.findings,
+ includePaths,
+ context.filePaths,
+ ),
};
scoped = (await preserveScanDraft(context, scoped)).input;
// Older checkpoints predate the coordinator's requested-path binding.
- scoped.findings = findingsWithinScope(scoped.findings, includePaths);
+ scoped.findings = findingsWithinScope(
+ scoped.findings,
+ includePaths,
+ context.filePaths,
+ );
const destination = await artifactDestination(
context,
["result.json"],
@@ -342,13 +351,26 @@ export async function recordCodexSecurityWorkerScanDraft(
export function findingsWithinScope(
findings: JsonObject[],
includePaths: readonly string[],
+ filePaths: readonly string[] = [],
): JsonObject[] {
- if (includePaths.includes(".")) return findings;
+ const scopes = includePaths.map((scope) =>
+ posix.normalize(scope).replace(/\/$/u, ""),
+ );
+ if (scopes.includes(".")) return findings;
+ const files = new Set(
+ filePaths.map((path) => posix.normalize(path).replace(/\/$/u, "")),
+ );
+ const targets = scopes.map((scope) => ({
+ path: scope,
+ isFile: files.has(scope),
+ }));
return findings.filter((finding) =>
(finding.locations as JsonObject[]).some((location) => {
- const path = (location.path as string).replace(/^\.\//u, "");
- return includePaths.some(
- (scope) => path === scope || path.startsWith(`${scope}/`),
+ const path = posix.normalize(location.path as string);
+ return targets.some(
+ (scope) =>
+ path === scope.path ||
+ (!scope.isFile && path.startsWith(`${scope.path}/`)),
);
}),
);
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts b/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts
index 3b5b9a33e..09586aeda 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts
@@ -318,6 +318,7 @@ export class DeepScanCoordinator {
draft.findings = findingsWithinScope(
draft.findings,
this.state.includePaths ?? [this.state.scope],
+ this.state.filePaths,
);
await this.options.onComplete?.(
draft,
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/engine.ts b/plugins/codex-security/mcp-app/src/deep-scan/engine.ts
index 86e536362..0316ab8bb 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/engine.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/engine.ts
@@ -41,6 +41,7 @@ export function startDeepScanEngine(options: {
scanId: run.scanId,
scope: run.scope,
includePaths: run.includePaths,
+ filePaths: run.filePaths,
pythonCommand: options.pythonCommand,
},
}),
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/executor.ts b/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
index 8731eb4ad..0ab2dccf3 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/executor.ts
@@ -64,6 +64,7 @@ export interface CodexSdkWorkerArtifactContext {
scanId: string;
scope?: string;
includePaths?: readonly string[];
+ filePaths?: readonly string[];
pythonCommand?: string;
}
@@ -83,6 +84,15 @@ export class CodexSdkWorkerExecutor implements CodexWorkerExecutor {
) {}
async run(request: CodexWorkerRequest): Promise<CodexWorkerResult> {
+ const includePaths = this.modelSettings.artifactContext?.includePaths;
+ const scopeInput = JSON.stringify({
+ paths: includePaths,
+ filePaths: this.modelSettings.artifactContext?.filePaths ?? [],
+ });
+ const includePathsFile =
+ includePaths === undefined
+ ? undefined
+ : resolve(dirname(request.promptPath), `scope-${randomUUID()}.json`);
try {
const parentSandbox = this.modelSettings.parentSandbox;
if (!parentSandbox) {
@@ -106,6 +116,10 @@ export class CodexSdkWorkerExecutor implements CodexWorkerExecutor {
}
childEnv[name] = value;
}
+ if (includePathsFile !== undefined) {
+ // Keep host-bound inputs beside the prompt, outside the worker's output root.
+ await writePrivateFile(includePathsFile, scopeInput);
+ }
// Keep one native configuration for the policy check and the worker turn.
// Worker-owned tool and permission settings take precedence over inheritance.
const configOverrides = profileConfigOverrides({
@@ -116,7 +130,7 @@ export class CodexSdkWorkerExecutor implements CodexWorkerExecutor {
mcp_servers: {
// A disabled server still needs a valid transport during native resolution.
"codex-security": { command: "node", enabled: false },
- ...(await this.compactArtifactServer(request)),
+ ...this.compactArtifactServer(request, includePathsFile),
},
...workerSubagentConfig(
request.subagents,
@@ -245,21 +259,25 @@ export class CodexSdkWorkerExecutor implements CodexWorkerExecutor {
}
} catch (error) {
throw classifyCodexWorkerError(error);
+ } finally {
+ if (includePathsFile !== undefined)
+ await fs.rm(includePathsFile, { force: true });
}
}
- private async compactArtifactServer(request: CodexWorkerRequest): Promise<
- Record<
- string,
- {
- command: string;
- args: string[];
- env: Record<string, string>;
- required: true;
- startup_timeout_sec: number;
- tool_timeout_sec: number;
- }
- >
+ private compactArtifactServer(
+ request: CodexWorkerRequest,
+ includePathsFile?: string,
+ ): Record<
+ string,
+ {
+ command: string;
+ args: string[];
+ env: Record<string, string>;
+ required: true;
+ startup_timeout_sec: number;
+ tool_timeout_sec: number;
+ }
> {
const scan = this.modelSettings.artifactContext;
if (!scan) return {};
@@ -285,17 +303,6 @@ export class CodexSdkWorkerExecutor implements CodexWorkerExecutor {
);
}
- const includePathsFile =
- scan.includePaths === undefined
- ? undefined
- : resolve(dirname(request.promptPath), `scope-${randomUUID()}.json`);
- if (includePathsFile !== undefined) {
- // Keep host-bound inputs beside the prompt, outside the worker's output root.
- await writePrivateFile(
- includePathsFile,
- JSON.stringify(scan.includePaths),
- );
- }
return {
// Keep every qualified worker tool within Codex's existing name limit.
cs_artifacts: {
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/store.ts b/plugins/codex-security/mcp-app/src/deep-scan/store.ts
index ae41ea1c5..24699d879 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/store.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/store.ts
@@ -695,13 +695,14 @@ export function parseDeepScan(result: JsonObject): DeepScanRunState {
),
}),
};
- const includePaths = value.includePaths;
- if (
- includePaths !== undefined &&
- (!Array.isArray(includePaths) ||
- includePaths.some((path) => typeof path !== "string"))
- ) {
- throw new Error("deepScan.includePaths must be an array of paths.");
+ for (const field of ["includePaths", "filePaths"] as const) {
+ const paths = value[field];
+ if (
+ paths !== undefined &&
+ (!Array.isArray(paths) || paths.some((path) => typeof path !== "string"))
+ ) {
+ throw new Error(`deepScan.${field} must be an array of paths.`);
+ }
}
return {
scanId: requiredString(value.scanId, "deepScan.scanId"),
@@ -711,7 +712,8 @@ export function parseDeepScan(result: JsonObject): DeepScanRunState {
updatedAt: optionalString(value.updatedAt),
targetPath: requiredString(value.targetPath, "deepScan.targetPath"),
scope: requiredString(value.scope, "deepScan.scope"),
- includePaths,
+ includePaths: value.includePaths as string[] | undefined,
+ filePaths: value.filePaths as string[] | undefined,
userContext: optionalString(value.userContext),
scanDir: requiredString(value.scanDir, "deepScan.scanDir"),
config,
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/types.ts b/plugins/codex-security/mcp-app/src/deep-scan/types.ts
index 9556f3f50..5b68ef075 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/types.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/types.ts
@@ -32,6 +32,7 @@ export interface DeepScanRunState {
targetPath: string;
scope: string;
includePaths?: string[];
+ filePaths?: string[];
userContext?: string;
scanDir: string;
config: DeepScanConfig;
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
index 5e4e0837e..2c7aca295 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
@@ -18,6 +18,7 @@ import {
copyFile,
mkdir,
readFile,
+ readdir,
realpath,
stat,
symlink,
@@ -1792,6 +1793,7 @@ env_key = "SYNTHETIC_GATEWAY_KEY"`,
index % 2 === 0
? ["api", `background jobs ${index}`]
: undefined,
+ filePaths: index % 2 === 0 ? [`background jobs ${index}`] : [],
pythonCommand: `${helperPython}-${index} `,
},
}),
@@ -2079,10 +2081,14 @@ env_key = "SYNTHETIC_GATEWAY_KEY"`,
servers["cs_artifacts"]!.env["CODEX_SECURITY_INCLUDE_PATHS_FILE"];
if (index % 2 === 0) {
assert.ok(scopeFile && path.isAbsolute(scopeFile));
- assert.deepEqual(JSON.parse(await readFile(scopeFile, "utf8")), [
+ assert.deepEqual(invocation.scopeInput.paths, [
"api",
`background jobs ${index}`,
]);
+ assert.deepEqual(invocation.scopeInput.filePaths, [
+ `background jobs ${index}`,
+ ]);
+ await assert.rejects(readFile(scopeFile), { code: "ENOENT" });
} else assert.equal(scopeFile, undefined);
assert.equal(process.env.PYTHON, python);
assert.equal(
@@ -2549,6 +2555,7 @@ async function testLargeScopeTransport() {
scanId,
scope: ".",
includePaths,
+ filePaths: includePaths.filter((_, index) => index % 2 === 0),
},
});
await executor.run(
@@ -2581,7 +2588,14 @@ async function testLargeScopeTransport() {
);
const invocation = await readJson(output, "invocation.json");
assert.deepEqual(invocation.scopeInput.paths, includePaths);
+ assert.deepEqual(
+ invocation.scopeInput.filePaths,
+ includePaths.filter((_, index) => index % 2 === 0),
+ );
assert.equal(path.isAbsolute(invocation.scopeInput.file), true);
+ await assert.rejects(readFile(invocation.scopeInput.file), {
+ code: "ENOENT",
+ });
assert.equal(invocation.argv.includes("resume"), resumed);
const argument = nativeConfigOverrides(invocation.argv).find(
(value) => value.startsWith("mcp_servers="),
@@ -3497,12 +3511,11 @@ ${jsonLineInputSource(` if (message.method === 'initialized') continue;
} else {
const stdin = (await process.stdin.toArray()).join('');
let scopeInput;
-if (stdin.includes('CAPTURE_SCOPE_INPUT')) {
+{
const { parse } = await import(${JSON.stringify(import.meta.resolve("smol-toml"))});
const configuration = process.argv.flatMap((arg, index) => arg === '--config' || arg === '-c' ? [parse(process.argv[index + 1])] : []).findLast(value => value.mcp_servers?.cs_artifacts);
- const environment = configuration.mcp_servers.cs_artifacts.env;
- const file = environment.CODEX_SECURITY_INCLUDE_PATHS_FILE;
- scopeInput = { file, paths: JSON.parse(file ? readFileSync(file, 'utf8') : environment.CODEX_SECURITY_INCLUDE_PATHS_JSON) };
+ const file = configuration?.mcp_servers.cs_artifacts.env.CODEX_SECURITY_INCLUDE_PATHS_FILE;
+ if (file) scopeInput = { file, ...JSON.parse(readFileSync(file, 'utf8')) };
}
const profileIndex = process.argv.indexOf('--profile');
@@ -3732,6 +3745,12 @@ async function testWorkerCancellation() {
await mkdir(workingDirectory);
const executor = new CodexSdkWorkerExecutor({
parentSandbox: trustedParentSandbox,
+ artifactContext: {
+ pluginRoot: fixture.root,
+ repoRoot: fixture.root,
+ scanId: randomUUID(),
+ includePaths: ["api"],
+ },
});
const run = (controller: AbortController, onThreadStarted?: () => void) =>
executor.run({
@@ -3741,6 +3760,7 @@ async function testWorkerCancellation() {
subagents: 0,
signal: controller.signal,
onThreadStarted,
+ artifactContext: { root: workingDirectory, layout: "worker" },
});
const preAborted = new AbortController();
const beforeStartup = new Error(
@@ -3752,6 +3772,10 @@ async function testWorkerCancellation() {
(error: Error) => error === beforeStartup,
);
assert.equal(worker, undefined);
+ assert.equal(
+ (await readdir(fixture.root)).some((name) => name.startsWith("scope-")),
+ false,
+ );
await assert.rejects(readFile(fixture.preflightMarkerPath), {
code: "ENOENT",
});
@@ -3806,6 +3830,10 @@ async function testWorkerCancellation() {
assert(workerSignal);
assert.equal(workerSignal.aborted, false);
}
+ assert.equal(
+ (await readdir(fixture.root)).some((name) => name.startsWith("scope-")),
+ false,
+ );
assert.notEqual(workerSignal, controller.signal);
const currentWorker = worker as ChildProcess | undefined;
assert(currentWorker);
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_scope.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_scope.ts
index 3c854ec77..a0d79074e 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_scope.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_scope.ts
@@ -1,5 +1,5 @@
import assert from "node:assert/strict";
-import { readFile, readdir, writeFile } from "node:fs/promises";
+import { mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import test from "node:test";
import { finding } from "./scan-draft-fixture.ts";
@@ -18,14 +18,36 @@ for (const resumed of [false, true]) {
[
"multi-path",
{ scope: ".", includePaths: ["api", "background jobs"] },
- ["api", "jobs"],
+ ["api", "jobs", "file-descendant"],
+ ],
+ ["single-path", { scope: "api" }, ["api", "file-descendant"]],
+ [
+ "normalized-paths",
+ { includePaths: ["./api/", "background jobs/."] },
+ ["api", "jobs", "file-descendant"],
+ ],
+ [
+ "file-path",
+ { includePaths: ["./api/source.py"], filePaths: ["api/source.py/"] },
+ ["api"],
+ ],
+ [
+ "repository",
+ { scope: "." },
+ ["api", "jobs", "unrelated", "prefix", "file-descendant"],
],
- ["single-path", { scope: "api" }, ["api"]],
- ["repository", { scope: "." }, ["api", "jobs", "unrelated", "prefix"]],
] as const) {
test(`${label} worker filters reconciled findings with resumed=${resumed}`, async (t) => {
const f = await fixture(t, "worker");
- const input = { ...f.draft({}, true), findings };
+ await mkdir(join(f.root, "api"));
+ await writeFile(join(f.root, "api/source.py"), "# scoped file\n");
+ const input = {
+ ...f.draft({}, true),
+ findings: [
+ ...findings,
+ finding("file-descendant", "api/source.py/generated"),
+ ],
+ };
let checkpoints: [string, Buffer][] = [];
if (resumed) {
await f.write(input);
@@ -40,7 +62,7 @@ for (const resumed of [false, true]) {
);
}
Object.assign(f.context, binding);
- await f.write({ ...input, findings: resumed ? [] : findings });
+ await f.write({ ...input, findings: resumed ? [] : input.findings });
const result = await readJson(f.root, "result.json");
assert.deepEqual(
result.findings.map(
@@ -82,55 +104,67 @@ for (const mode of ["deep", "standard"] as const) {
});
}
-test("artifact MCP uses the coordinator's multi-path binding", async (t) => {
- const { importSource } = await import("./import-module.ts");
- const { Client } = await import("@modelcontextprotocol/sdk/client/index.js");
- const { InMemoryTransport } =
- await import("@modelcontextprotocol/sdk/inMemory.js");
- const { createCodexSecurityArtifactWriterServer } = await importSource(
- "../artifact-writer-main.ts",
- { absWorkingDir: import.meta.dirname },
- );
- const f = await fixture(t, "worker");
- const scopeFile = join(f.root, "../scope café.json");
- await writeFile(
- scopeFile,
- JSON.stringify([
- "api",
+for (const legacy of [false, true]) {
+ test(`artifact MCP uses the coordinator's multi-path binding with legacy=${legacy}`, async (t) => {
+ const { importSource } = await import("./import-module.ts");
+ const { Client } =
+ await import("@modelcontextprotocol/sdk/client/index.js");
+ const { InMemoryTransport } =
+ await import("@modelcontextprotocol/sdk/inMemory.js");
+ const { createCodexSecurityArtifactWriterServer } = await importSource(
+ "../artifact-writer-main.ts",
+ { absWorkingDir: import.meta.dirname },
+ );
+ const f = await fixture(t, "worker");
+ const scopeFile = join(f.root, "../scope café.json");
+ const paths = [
+ "api/source.py",
"background jobs",
...Array.from({ length: 1024 }, (_, index) => `source ${index}/café`),
- ]),
- );
- const server = await createCodexSecurityArtifactWriterServer({
- CODEX_SECURITY_ARTIFACT_ROOT: f.root,
- CODEX_SECURITY_REPO_ROOT: f.root,
- CODEX_SECURITY_SCAN_ID: f.context.scanId,
- CODEX_SECURITY_SCOPE: ".",
- CODEX_SECURITY_INCLUDE_PATHS_FILE: scopeFile,
- });
- const client = new Client({ name: "scope-fixture", version: "1.0.0" });
- const [clientTransport, serverTransport] =
- InMemoryTransport.createLinkedPair();
- await server.connect(serverTransport);
- await client.connect(clientTransport);
- try {
- const response = await client.callTool({
- name: "record_codex_security_scan_draft",
- arguments: { ...f.draft({}, true), findings },
- });
- assert.equal(response.isError, undefined);
- const result = await readJson(f.root, "result.json");
- assert.deepEqual(
- result.findings.map(
- (row: { identity: { anchor: string } }) => row.identity.anchor,
+ ];
+ await writeFile(
+ scopeFile,
+ JSON.stringify(
+ legacy ? paths : { paths, filePaths: ["./api/source.py"] },
),
- ["api", "jobs"],
);
- } finally {
- await client.close();
- await server.close();
- }
-});
+ const server = await createCodexSecurityArtifactWriterServer({
+ CODEX_SECURITY_ARTIFACT_ROOT: f.root,
+ CODEX_SECURITY_REPO_ROOT: f.root,
+ CODEX_SECURITY_SCAN_ID: f.context.scanId,
+ CODEX_SECURITY_SCOPE: ".",
+ CODEX_SECURITY_INCLUDE_PATHS_FILE: scopeFile,
+ });
+ const client = new Client({ name: "scope-fixture", version: "1.0.0" });
+ const [clientTransport, serverTransport] =
+ InMemoryTransport.createLinkedPair();
+ await server.connect(serverTransport);
+ await client.connect(clientTransport);
+ try {
+ const response = await client.callTool({
+ name: "record_codex_security_scan_draft",
+ arguments: {
+ ...f.draft({}, true),
+ findings: [
+ ...findings,
+ finding("descendant", "api/source.py/generated"),
+ ],
+ },
+ });
+ assert.equal(response.isError, undefined);
+ const result = await readJson(f.root, "result.json");
+ assert.deepEqual(
+ result.findings.map(
+ (row: { identity: { anchor: string } }) => row.identity.anchor,
+ ),
+ legacy ? ["api", "jobs", "descendant"] : ["api", "jobs"],
+ );
+ } finally {
+ await client.close();
+ await server.close();
+ }
+ });
+}
test("worker scope keeps a finding with any matching location and supports exact files", async (t) => {
const f = await fixture(t, "worker");
@@ -152,3 +186,87 @@ test("worker scope keeps a finding with any matching location and supports exact
);
assert.deepEqual(result.findings.at(-1).locations, mixed.locations);
});
+
+for (const scope of ["./api/", "api/source.py"]) {
+ test(`Deep parent publication respects ${scope}`, async (t) => {
+ const f = await fixture(t, "deep");
+ await mkdir(join(f.root, "api"));
+ await writeFile(join(f.root, "api/source.py"), "# scoped file\n");
+ f.context.targetContract = {
+ ...f.context.targetContract,
+ scope: {
+ requiredIncludePaths: [scope],
+ requiredExcludePaths: [],
+ filePaths: scope === "api/source.py" ? [scope] : [],
+ },
+ };
+ await f.write({
+ ...f.draft({}, true),
+ findings: [findings[0], finding("descendant", "api/source.py/generated")],
+ });
+ const result = await readJson(f.root, "findings.json");
+ assert.deepEqual(
+ result.findings.map(
+ (row: { identity: { anchor: string } }) => row.identity.anchor,
+ ),
+ scope === "./api/" ? ["api", "descendant"] : ["api"],
+ );
+ });
+}
+
+for (const layout of ["worker", "deep"] as const) {
+ for (const scope of ["./api/", "./api/source.py"]) {
+ test(`${layout} preserves original ${scope} scope after checkout changes`, async (t) => {
+ const f = await fixture(t, layout);
+ await mkdir(join(f.root, "api"));
+ await writeFile(join(f.root, "api/source.py"), "# original target\n");
+ const filePaths = scope.endsWith(".py") ? ["api/source.py"] : [];
+ Object.assign(f.context, { includePaths: [scope], filePaths });
+ f.context.targetContract = {
+ ...f.context.targetContract,
+ scope: {
+ requiredIncludePaths: [scope],
+ requiredExcludePaths: [],
+ filePaths,
+ },
+ };
+ const input = {
+ ...f.draft({}, true),
+ findings: [
+ findings[0],
+ finding("descendant", "api/source.py/generated"),
+ ],
+ };
+ await f.write(input);
+ const checkpoints = await Promise.all(
+ (await readdir(join(f.root, "checkpoints"))).map(
+ async (name) =>
+ [name, await readFile(join(f.root, "checkpoints", name))] as const,
+ ),
+ );
+ if (filePaths.length) {
+ await rm(join(f.root, "api/source.py"));
+ await mkdir(join(f.root, "api/source.py"));
+ } else {
+ await rm(join(f.root, "api"), { recursive: true });
+ await writeFile(join(f.root, "api"), "new checkout file\n");
+ }
+ await f.write(layout === "worker" ? { ...input, findings: [] } : input);
+ const result = await readJson(
+ f.root,
+ layout === "worker" ? "result.json" : "findings.json",
+ );
+ assert.deepEqual(
+ result.findings.map(
+ (row: { identity: { anchor: string } }) => row.identity.anchor,
+ ),
+ filePaths.length ? ["api"] : ["api", "descendant"],
+ );
+ for (const [name, bytes] of checkpoints)
+ assert.deepEqual(
+ await readFile(join(f.root, "checkpoints", name)),
+ bytes,
+ );
+ });
+ }
+}
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts
index f153cd0d5..8c466772f 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_store.ts
@@ -94,6 +94,16 @@ async function testBeginProtocolAndParsing() {
});
assert.equal(joined.scanId, scanId);
assert.equal(flagValue(joinedArgs, "--claim-token"), claimToken);
+ const scoped = parseDeepScan(
+ stateResult(scanId, {
+ deepScan: {
+ includePaths: ["api", "README.md"],
+ filePaths: ["README.md"],
+ },
+ }),
+ );
+ assert.deepEqual(scoped.includePaths, ["api", "README.md"]);
+ assert.deepEqual(scoped.filePaths, ["README.md"]);
}
async function testWriteSerializationAndRecovery() {
diff --git a/plugins/codex-security/scripts/deep_scan_workbench.py b/plugins/codex-security/scripts/deep_scan_workbench.py
index e4553cbb9..0cefb4ba2 100644
--- a/plugins/codex-security/scripts/deep_scan_workbench.py
+++ b/plugins/codex-security/scripts/deep_scan_workbench.py
@@ -372,6 +372,7 @@ def deep_scan_state(connection: sqlite3.Connection, scan_id: str) -> dict[str, A
"targetPath": scan["target_path"],
"scope": scan["scope"],
"includePaths": dependencies().requested_scan_paths(scan),
+ "filePaths": dependencies().requested_scan_file_paths(scan),
"userContext": (
json.loads(run["discovery_user_context_json"])
if run["discovery_user_context_json"] is not None
diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.py
index a1b7f143d..df8a11095 100644
--- a/plugins/codex-security/scripts/finalize_scan_contract.py
+++ b/plugins/codex-security/scripts/finalize_scan_contract.py
@@ -2930,7 +2930,7 @@ def _prepare_scan_finalization(
completion_binding: dict[str, Any] | None = None,
completion_warnings: list[str] | None = None,
draft_documents: tuple[dict[str, Any], dict[str, Any], dict[str, Any]] | None = None,
- finding_scope: list[str] | None = None,
+ finding_scope: list[tuple[str, bool]] | None = None,
) -> PreparedScanFinalization:
"""Read, populate, and validate a scan without writing any output files."""
@@ -3019,9 +3019,11 @@ def _prepare_scan_finalization(
finding
for finding in findings["findings"]
if any(
- path_within_scope(location["path"], path)
+ PurePosixPath(location["path"]) == PurePosixPath(path)
+ if is_file
+ else path_within_scope(location["path"], path)
for location in finding["locations"]
- for path in finding_scope
+ for path, is_file in finding_scope
)
]
if was_sealed and len(scoped_findings) != len(findings["findings"]):
diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.py
index 34016d168..6fff8bc67 100644
--- a/plugins/codex-security/scripts/workbench_db.py
+++ b/plugins/codex-security/scripts/workbench_db.py
@@ -441,6 +441,20 @@ def requested_scan_paths(scan: sqlite3.Row) -> list[str]:
return [scan["scope"]]
+def requested_scan_file_paths(scan: sqlite3.Row) -> list[str]:
+ # Older scans did not record path kinds. Retain their lexical scope semantics
+ # rather than infer the original target from a possibly changed checkout.
+ if scan["recipe_json"] is None:
+ return []
+ recipe = json.loads(scan["recipe_json"], parse_constant=reject_non_finite_json)
+ return recipe.get("scopeFilePaths", [])
+
+
+def requested_finding_scope(scan: sqlite3.Row) -> list[tuple[str, bool]]:
+ files = {PurePosixPath(path) for path in requested_scan_file_paths(scan)}
+ return [(path, PurePosixPath(path) in files) for path in requested_scan_paths(scan)]
+
+
def scan_contract(scan: sqlite3.Row) -> dict[str, Any]:
target = Path(scan["target_path"])
target_contract = {
@@ -462,6 +476,7 @@ def scan_contract(scan: sqlite3.Row) -> dict[str, Any]:
"scope": {
"requiredExcludePaths": [],
"requestedPath": scan["scope"],
+ "filePaths": requested_scan_file_paths(scan),
**(
{"requiredIncludePaths": requested_scan_paths(scan)}
if scan["mode"] != "diff"
@@ -1539,7 +1554,7 @@ def complete_scan_locked(
completion_binding=completion_binding,
completion_warnings=warnings if scan["mode"] != "deep" else None,
draft_documents=draft_documents,
- finding_scope=requested_scan_paths(scan) if scan["mode"] == "deep" else None,
+ finding_scope=requested_finding_scope(scan) if scan["mode"] == "deep" else None,
)
add_warning()
wrote = True
@@ -1682,6 +1697,10 @@ def register_cli_scan(
recipe = parse_scan_recipe(recipe_json, repository)
requested_target = recipe["target"]
paths = requested_target["paths"]
+ # Capture host-observed kinds once; resumes and publication use the saved recipe.
+ recipe.pop("scopeFilePaths", None)
+ if file_paths := [path for path in paths if (repository / path).is_file()]:
+ recipe["scopeFilePaths"] = file_paths
scope = paths[0] if len(paths) == 1 else "."
diff_target = None
if requested_target["kind"] in {"refs", "working_tree"}:
diff --git a/plugins/codex-security/scripts/workbench_saved_results.py b/plugins/codex-security/scripts/workbench_saved_results.py
index 1ec8b5260..97091bd6c 100644
--- a/plugins/codex-security/scripts/workbench_saved_results.py
+++ b/plugins/codex-security/scripts/workbench_saved_results.py
@@ -2274,7 +2274,7 @@ def preserve_scan_results_locked(
completion_binding=binding,
completion_warnings=warnings,
draft_documents=documents,
- finding_scope=db.requested_scan_paths(scan) if scan["mode"] == "deep" else None,
+ finding_scope=db.requested_finding_scope(scan) if scan["mode"] == "deep" else None,
)
snapshots = _snapshot_published_outputs(scan_dir)
try:
diff --git a/plugins/codex-security/tests/test_deep_scan_request_identity.py b/plugins/codex-security/tests/test_deep_scan_request_identity.py
index 8914a2262..da5f32d51 100644
--- a/plugins/codex-security/tests/test_deep_scan_request_identity.py
+++ b/plugins/codex-security/tests/test_deep_scan_request_identity.py
@@ -26,6 +26,7 @@ def test_target_request_does_not_reuse_different_user_context(
target.mkdir()
scan_root = tmp_path / "scans"
first = begin_target_scan(state_dir, codex_home, target, scan_root, user_context=first_context)
+ assert first["deepScan"]["filePaths"] == []
first_scan_id = str(first["deepScan"]["scanId"])
mark_deep_coordinator_succeeded(
state_dir, first_scan_id, Path(str(first["deepScan"]["scanDir"]))
diff --git a/plugins/codex-security/tests/test_deep_scan_successful_publication.py b/plugins/codex-security/tests/test_deep_scan_successful_publication.py
index 66bcd3c79..57e5d4d28 100644
--- a/plugins/codex-security/tests/test_deep_scan_successful_publication.py
+++ b/plugins/codex-security/tests/test_deep_scan_successful_publication.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import copy
import json
+import shutil
import uuid
from argparse import Namespace
from pathlib import Path
@@ -37,8 +38,9 @@ def publication_scan(workbench_api, workbench_db, tmp_path, monkeypatch):
(target / "subdir" / "extract.py").write_text("# Synthetic scan target\n")
if paths is not None:
for directory in [*paths, "unrelated"]:
- (target / directory).mkdir(exist_ok=True)
- (target / directory / "extract.py").write_text("# Synthetic scoped target\n")
+ if not (target / directory).is_file():
+ (target / directory).mkdir(exist_ok=True)
+ (target / directory / "extract.py").write_text("# Synthetic scoped target\n")
include_paths = paths if paths is not None else [scope]
scan_dir = tmp_path / "scan"
scan_dir.mkdir(mode=0o700)
@@ -571,12 +573,15 @@ def test_deep_publication_write_failure_keeps_original_terminal_cause(
@pytest.mark.parametrize("resumed", [False, True])
[email protected]("scope", ["subdir", "./subdir/", "subdir/extract.py"])
def test_scoped_deep_completion_filters_old_parent_and_preserves_accepted_sources(
- workbench_api, workbench_db, publication_scan, resumed
+ workbench_api, workbench_db, publication_scan, resumed, scope
):
- scan = publication_scan(paths=["subdir", "background jobs"])
+ scan = publication_scan(paths=[scope, "background jobs"])
findings = []
- for index, directory in enumerate(["subdir", "background jobs", "unrelated"]):
+ for index, directory in enumerate(
+ ["subdir", "background jobs", "unrelated", "subdir/extract.py"]
+ ):
finding = copy.deepcopy(scan.findings[0])
finding["identity"]["anchor"] = f"scope-{index}"
finding["title"] = f"Scope finding {directory}"
@@ -603,7 +608,9 @@ def test_scoped_deep_completion_filters_old_parent_and_preserves_accepted_source
row["locations"][0]["path"]
for row in json.loads((scan.scan_dir / "findings.json").read_text())["findings"]
]
- assert paths == ["subdir/extract.py", "background jobs/extract.py"]
+ assert paths == ["subdir/extract.py", "background jobs/extract.py"] + (
+ [] if scope == "subdir/extract.py" else ["subdir/extract.py/extract.py"]
+ )
assert "Scope finding unrelated" not in (scan.scan_dir / "report.md").read_text()
indexed = workbench_db.execute(
"SELECT details_json FROM findings",
@@ -649,11 +656,102 @@ def test_deep_scope_filter_does_not_hide_unsafe_finding_locations(
assert manifest["scan"].get("status") != "completed"
-def test_stopped_deep_projection_filters_the_canonical_parent(
[email protected]("stopped", [False, True])
[email protected]("scope", ["./subdir/", "./subdir/extract.py"])
+def test_deep_publication_preserves_original_scope_kinds_after_checkout_changes(
+ workbench_api, workbench_db, publication_scan, scope, stopped
+):
+ scan = publication_scan(paths=[scope, "background jobs"])
+ row = workbench_api["require_scan"](workbench_db, scan.scan_id)
+ file_paths = [scope] if scope.endswith(".py") else []
+ assert workbench_api["requested_scan_file_paths"](row) == file_paths
+ assert workbench_api["scan_contract"](row)["scope"]["filePaths"] == file_paths
+ descendant = copy.deepcopy(scan.findings[0])
+ descendant["identity"]["anchor"] = "descendant"
+ descendant["locations"] = [{"path": "subdir/extract.py/generated", "startLine": 1}]
+ for key in ("findingId", "occurrenceId", "fingerprints"):
+ descendant.pop(key, None)
+ findings = [*scan.findings, descendant]
+ (scan.scan_dir / "findings.json").write_text(json.dumps({"findings": findings}))
+ accepted = []
+ for kind in ("discovery", "dedup"):
+ result = add_worker(workbench_db, scan)
+ result.write_text(json.dumps(saved_draft(scan.scan_id, findings=findings, complete=True)))
+ with workbench_db:
+ workbench_db.execute(
+ "UPDATE deep_scan_workers SET kind = ?, merge_state = 'none' "
+ "WHERE result_manifest_path = ?",
+ (kind, str(result)),
+ )
+ accepted.append((result, result.read_bytes()))
+ target_path = Path(row["target_path"]) / scope
+ if file_paths:
+ target_path.unlink()
+ target_path.mkdir()
+ else:
+ shutil.rmtree(target_path)
+ target_path.write_text("new checkout file\n")
+ assert (
+ workbench_api["deep_scan"].deep_scan_result(workbench_db, scan.scan_id)["deepScan"][
+ "filePaths"
+ ]
+ == file_paths
+ )
+ if stopped:
+ workbench_api["saved_results"].fail_scan(
+ workbench_api["_WORKBENCH_DB_CONTEXT"],
+ workbench_db,
+ Namespace(
+ scan_id=scan.scan_id, claim_token=None, cost_json=None, message="interrupted"
+ ),
+ )
+ else:
+ assert complete(workbench_api, workbench_db, scan)["progress"]["status"] == "complete"
+ expected_paths = ["subdir/extract.py"] + ([] if file_paths else ["subdir/extract.py/generated"])
+ assert [
+ item["locations"][0]["path"]
+ for item in json.loads((scan.scan_dir / "findings.json").read_text())["findings"]
+ ] == expected_paths
+ for result, contents in accepted:
+ assert result.read_bytes() == contents
+ # Reading a sealed report keeps the original projection even after the checkout changes.
+ sealed = (scan.scan_dir / "findings.json").read_bytes()
+ workbench_api["finalize_scan"](scan.scan_dir)
+ assert (scan.scan_dir / "findings.json").read_bytes() == sealed
+
+
+def test_legacy_deep_scope_does_not_reclassify_a_changed_checkout(
workbench_api, workbench_db, publication_scan
):
- scan = publication_scan(paths=["subdir", "background jobs"])
- scan.findings[0]["locations"] = [{"path": "unrelated/extract.py", "startLine": 1}]
+ scan = publication_scan(paths=["subdir"])
+ row = workbench_api["require_scan"](workbench_db, scan.scan_id)
+ recipe = json.loads(row["recipe_json"])
+ recipe.pop("scopeFilePaths", None)
+ with workbench_db:
+ workbench_db.execute(
+ "UPDATE scans SET recipe_json = ? WHERE id = ?", (json.dumps(recipe), scan.scan_id)
+ )
+ row = workbench_api["require_scan"](workbench_db, scan.scan_id)
+ target = Path(row["target_path"]) / "subdir"
+ shutil.rmtree(target)
+ target.write_text("new checkout file\n")
+ assert complete(workbench_api, workbench_db, scan)["progress"]["status"] == "complete"
+ findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]
+ assert len(findings) == 1
+
+
+ "scope, location",
+ [
+ ("subdir", "unrelated/extract.py"),
+ ("./subdir/extract.py", "subdir/extract.py/generated"),
+ ],
+)
+def test_stopped_deep_projection_filters_the_canonical_parent(
+ workbench_api, workbench_db, publication_scan, scope, location
+):
+ scan = publication_scan(paths=[scope, "background jobs"])
+ scan.findings[0]["locations"] = [{"path": location, "startLine": 1}]
(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": scan.findings}))
stopped = workbench_api["saved_results"].fail_scan(
workbench_api["_WORKBENCH_DB_CONTEXT"],
diff --git a/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts b/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts
index 860af6506..57e3b4ef9 100644
--- a/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts
+++ b/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts
@@ -18,7 +18,7 @@ function bundledFunction(runtime: string, name: string): string {
test("advertises distinct Standard worker and Deep reducer contracts", async () => {
const runtime = await loadBundledRuntime();
const method =
- / async compactArtifactServer\(request\) \{[\s\S]*?\n \}/u.exec(
+ / compactArtifactServer\(request, includePathsFile\) \{[\s\S]*?\n \}/u.exec(
runtime,
)?.[0];
expect(method).toBeDefined();
@@ -30,7 +30,7 @@ test("advertises distinct Standard worker and Deep reducer contracts", async ()
)({ join }) as (
this: { modelSettings: { artifactContext: Record<string, string> } },
request: Record<string, unknown>,
- ) => Promise<Record<string, { args: string[]; env: NodeJS.ProcessEnv }>>;
+ ) => Record<string, { args: string[]; env: NodeJS.ProcessEnv }>;
const node = Bun.which("node");
expect(node).not.toBeNull();
@@ -44,7 +44,7 @@ test("advertises distinct Standard worker and Deep reducer contracts", async ()
for (const layout of ["worker", "reducer"] as const) {
const artifactRoot = join(scanRoot, layout);
mkdirSync(artifactRoot);
- const servers = await compactArtifactServer.call(
+ const servers = compactArtifactServer.call(
{
modelSettings: {
artifactContext: {