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

refactor: simplify workbench and Deep Scan processing (#2137)

Michael D'Angelob310458b1d2e
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/artifact-validation.ts b/plugins/codex-security/mcp-app/src/deep-scan/artifact-validation.ts
index a8409f143..601068909 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/artifact-validation.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/artifact-validation.ts
@@ -1,9 +1,8 @@
import { createHash } from "node:crypto";
-import { dirname, posix, relative, sep } from "node:path";
+import { dirname, posix } from "node:path";
import { isDeepStrictEqual } from "node:util";
import type { JsonObject } from "../types.js";
import { asRecord } from "../record.js";
-import type { AcceptedDiscovery } from "./worker-runner.js";
import { artifactSourcePath, type ArtifactContext } from "../artifact-io.js";
import {
coverageReceiptComponents,
@@ -66,72 +65,6 @@ export function deepReductionForPersistence(
return persisted;
}
-/** Reduction combines findings; it does not complete the source workers' review. */
-export function aggregateDiscoveryCoverage(
- artifacts: DeepScanArtifacts,
- discoveries: Pick<AcceptedDiscovery, "id" | "resultPath" | "result">[],
-): ScanDraftInput["reviewState"] {
- const coverage: ScanDraftInput["reviewState"] = {
- surfaces: [],
- explicitExclusions: [],
- deferred: [],
- };
- for (const worker of discoveries) {
- const source = structuredClone(worker.result.reviewState);
- const prefix = relative(artifacts.scanDir, dirname(worker.resultPath))
- .split(sep)
- .join("/");
- const scopedId = (id: string) => `${worker.id}/${id}`;
- for (const field of [
- "surfaces",
- "explicitExclusions",
- "deferred",
- ] as const) {
- const rows = source[field] as JsonObject[];
- const usedIds = new Set(
- rows.flatMap((row) => (typeof row.id === "string" ? [row.id] : [])),
- );
- const emittedIds = new Set<string>();
- for (const [index, row] of rows.entries()) {
- const retained: JsonObject = { ...row, workerId: worker.id };
- // Independent reviews reuse local IDs. Preserve their links without
- // allowing one worker's disposition to stand in for another's work.
- const baseId = typeof row.id === "string" ? row.id : String(index);
- let id = baseId;
- if (typeof row.id !== "string" || emittedIds.has(id)) {
- for (let suffix = 2; usedIds.has(id); suffix++) {
- id = `${baseId}-${suffix}`;
- }
- }
- usedIds.add(id);
- emittedIds.add(id);
- retained.id = scopedId(id);
- if (field === "deferred" && Array.isArray(row.surfaceIds)) {
- retained.surfaceIds = (row.surfaceIds as string[]).map(scopedId);
- }
- if (field === "surfaces" && Array.isArray(row.receiptRefs)) {
- retained.receiptRefs = (row.receiptRefs as string[]).map(
- (ref) => `${prefix}/${coverageReceiptComponents(ref).join("/")}`,
- );
- }
- (coverage[field] as JsonObject[]).push(retained);
- }
- }
- if (Array.isArray(source.openQuestions)) {
- const questions = (coverage.openQuestions ??= []) as JsonObject[];
- for (const question of source.openQuestions) {
- questions.push({
- ...(typeof question === "string"
- ? { question }
- : (question as JsonObject)),
- workerId: worker.id,
- });
- }
- }
- }
- return coverage;
-}
-
export function parseDeepReduction(input: unknown): DeepReductionInput {
const parsed = deepReductionInputSchema.parse(input);
validateFindingSemantics(parsed.findings);
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 80fdd5d21..5f7b7392a 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts
@@ -62,7 +62,6 @@ interface SchedulerResult {
omittedWorkerIds: string[];
result?: DeepReductionInput;
resultPath?: string;
- discoveries: AcceptedDiscovery[];
}
export interface ParentScanState {
@@ -724,8 +723,7 @@ export class DeepScanCoordinator {
const config = this.state.config;
const active = new Map<string, Promise<DiscoveryOutcome>>();
const recovered = await this.recoverAcceptedDiscoveries();
- const accepted = new Map(recovered.map((worker) => [worker.id, worker]));
- let reducerInputs: AcceptedDiscovery[] = [];
+ const acceptedIds = new Set(recovered.map((worker) => worker.id));
const mergedIds = new Set(
(this.state.persistedWorkers ?? [])
.filter(
@@ -893,7 +891,7 @@ export class DeepScanCoordinator {
phase: "discovery",
handoffClaimToken: this.options.handoffClaimToken,
});
- this.logProgress(accepted.size);
+ this.logProgress(acceptedIds.size);
while (!stopReason) {
if (this.abortController.signal.aborted) {
@@ -934,7 +932,6 @@ export class DeepScanCoordinator {
)
) {
const consumed = [...buffer].sort(compareCompletionSequence);
- reducerInputs = consumed;
buffer = [];
reducerSequence += 1n;
reducer = this.trackSchedulerWork(
@@ -1011,10 +1008,10 @@ export class DeepScanCoordinator {
}
continue;
}
- accepted.set(outcome.worker.id, outcome.worker);
+ acceptedIds.add(outcome.worker.id);
this.state = { ...this.state, consecutiveErrors: 0 };
buffer.push(outcome.worker);
- this.logProgress(accepted.size);
+ this.logProgress(acceptedIds.size);
continue;
}
@@ -1039,7 +1036,6 @@ export class DeepScanCoordinator {
return await settleFailure(thresholdError);
}
reducerFailures = 0;
- for (const worker of reducerInputs) mergedIds.add(worker.id);
if (!this.canceled && !this.externallyFailed) this.state = outcome.run;
previousReducerResultPath = outcome.resultPath;
latestResult = outcome.result;
@@ -1062,7 +1058,7 @@ export class DeepScanCoordinator {
if (
!previousReducerResultPath &&
- !(this.discoveryDeadlineReached && accepted.size === 0)
+ !(this.discoveryDeadlineReached && acceptedIds.size === 0)
) {
throw new Error(
"Deep Scan ended without a successfully reduced Standard scan.",
@@ -1073,9 +1069,6 @@ export class DeepScanCoordinator {
omittedWorkerIds: [...new Set(omittedWorkerIds)],
result: latestResult,
resultPath: previousReducerResultPath,
- discoveries: [...accepted.values()].filter((worker) =>
- mergedIds.has(worker.id),
- ),
};
}
@@ -1089,7 +1082,7 @@ export class DeepScanCoordinator {
`Accepted discovery ${worker.id} has incomplete persisted evidence.`,
);
}
- const result = await validateDiscoveryArtifacts(
+ await validateDiscoveryArtifacts(
this.artifacts,
worker.resultManifestPath,
this.state.scanId,
@@ -1098,7 +1091,6 @@ export class DeepScanCoordinator {
recovered.push({
id: worker.id,
resultPath: worker.resultManifestPath,
- result,
completionSequence: worker.completionSequence,
attempt: worker.attempt,
});
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/worker-runner.ts b/plugins/codex-security/mcp-app/src/deep-scan/worker-runner.ts
index 4e72b5c42..9c3d41a55 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/worker-runner.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/worker-runner.ts
@@ -1,5 +1,4 @@
import { promises as fs } from "node:fs";
-import type { ScanDraftInput } from "../artifact-scan-draft.js";
import { dirname, join } from "node:path";
import { readDeepReductionSources } from "../artifact-deep-reducer.js";
import {
@@ -37,7 +36,6 @@ import type {
export interface AcceptedDiscovery {
id: string;
resultPath: string;
- result: ScanDraftInput;
completionSequence: number;
attempt?: number;
}
@@ -147,7 +145,7 @@ export class DeepScanWorkerRunner {
artifactDir,
attempt: 1,
});
- let discoveryResult: ScanDraftInput | undefined;
+ let validated = false;
let outcome = await this.runWorkerWithRetries({
workerId,
kind: "discovery",
@@ -157,12 +155,13 @@ export class DeepScanWorkerRunner {
artifactContext: { root: artifactDir, layout: "worker" },
subagents: run.config.subagents,
validate: async () => {
- discoveryResult = await validateDiscoveryArtifacts(
+ await validateDiscoveryArtifacts(
artifacts,
resultPath,
run.scanId,
run,
);
+ validated = true;
},
});
if (outcome.status === "succeeded" && this.options.signal.aborted) {
@@ -177,7 +176,7 @@ export class DeepScanWorkerRunner {
outcome.threadId,
);
}
- if (!discoveryResult) {
+ if (!validated) {
await fs.rm(resultPath, { force: true });
}
if (outcome.status === "failed") {
@@ -226,7 +225,6 @@ export class DeepScanWorkerRunner {
worker: {
id: workerId,
resultPath,
- result: discoveryResult!,
completionSequence: persisted.completionSequence,
attempt: persisted.attempt,
},
diff --git a/plugins/codex-security/mcp-app/tests/import-module.ts b/plugins/codex-security/mcp-app/tests/import-module.ts
index 8fe422814..997e07d0a 100644
--- a/plugins/codex-security/mcp-app/tests/import-module.ts
+++ b/plugins/codex-security/mcp-app/tests/import-module.ts
@@ -25,7 +25,7 @@ export async function importModule(options: BuildOptions) {
write: false,
...options,
});
- const contents = result.outputFiles![0].contents;
+ const contents = result.outputFiles![0]!.contents;
// Keep content-based module identity without embedding the bundle in stacks.
const digest = createHash("sha256").update(contents).digest("hex");
const modulePath = join(moduleDirectory, `${digest}.mjs`);
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.ts
index 1170725b9..fa9d42502 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.ts
@@ -21,7 +21,6 @@ import type { JsonObject } from "../src/types.ts";
const {
validateDiscoveryArtifacts,
validateReducerArtifacts,
- aggregateDiscoveryCoverage,
aggregateSourceCoverage,
deepReductionForPersistence,
projectDiscoveryCoverage,
@@ -239,13 +238,19 @@ async function testLegacyDeferredPublication(root: string) {
worker.resultPath,
scanId,
);
- const coverage = aggregateDiscoveryCoverage(worker.artifacts, [
- { ...worker, result: accepted },
- ]);
+ const coverage = projectDiscoveryCoverage(
+ accepted.reviewState,
+ { id: worker.id, attempt: 1 },
+ "artifacts/deep_discovery/workers/discovery-0001/output",
+ );
const parent = parseScanDraft({ ...result, reviewState: coverage });
assert.deepEqual(
parent.reviewState.deferred.map((row: { id: string }) => row.id),
- ["first/review", "first/review-3", "first/review-2"],
+ [
+ "first-attempt-1-deferred-1",
+ "first-attempt-1-deferred-2",
+ "first-attempt-1-deferred-3",
+ ],
);
assert.deepEqual(
parent.reviewState.deferred.map((row: { reason: string }) => row.reason),
@@ -712,13 +717,17 @@ async function testCoveragePublicationParity(root: string) {
worker.resultPath,
scanId,
);
- const coverage = aggregateDiscoveryCoverage(worker.artifacts, [
- { ...worker, result: accepted },
- ]);
+ const coverage = projectDiscoveryCoverage(
+ accepted.reviewState,
+ { id: worker.id, attempt: 1 },
+ "artifacts/deep_discovery/workers/discovery-0001/output",
+ );
assert.deepEqual(coverage.deferred[0].receiptRefs, {
external: "opaque metadata",
});
- assert.deepEqual(coverage.deferred[0].surfaceIds, ["first/api"]);
+ assert.deepEqual(coverage.deferred[0].surfaceIds, [
+ "first-attempt-1-surface-1",
+ ]);
assert.deepEqual(coverage.explicitExclusions[0].receiptRefs, [
"opaque/missing.md",
]);
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_errors.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_errors.ts
index 695cf46ab..6737c2216 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_errors.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_errors.ts
@@ -90,6 +90,8 @@ function testWorkerErrorClassificationPreservesExplicitFailures() {
"Codex Exec exited with code 2: failed to load configuration: invalid value",
"chatgpt authentication required for remote plugin catalog; api key auth is not supported",
"chatgpt authentication required to sync remote plugins; api key auth is not supported",
+ "required MCP servers failed to initialize: codex_security_artifacts: timed out handshaking with MCP server after 30s",
+ "required MCP servers failed to initialize: other_server: request timed out",
]) {
const original = new Error(message);
assert.equal(classifyCodexWorkerError(original), original);
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 31052d7aa..b81bf6351 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
@@ -81,9 +81,6 @@ const {
resolveCodexPath,
snapshotWorkerEnvironment,
appendItemDiagnostic,
- classifyCodexWorkerError,
- codexWorkerTurnError,
- DeepScanNonRetryableError,
isCodexCybersecurityPolicyRefusal,
parseDeepScan,
} = await importModule({
@@ -94,7 +91,7 @@ const {
// Expose the environment snapshot only for tests.
contents: `${await readFile(executorSource, "utf8")}
export { snapshotWorkerEnvironment, appendItemDiagnostic };
-export * from "./errors.js";
+export { isCodexCybersecurityPolicyRefusal } from "./errors.js";
export { parseDeepScan } from "./response.js";`,
loader: "ts",
resolveDir: path.dirname(fileURLToPath(executorSource)),
@@ -208,6 +205,7 @@ try {
() => testWindowsNpmPackageResolution("managed"),
"managed Windows npm package resolution",
);
+ await check(t, testHoistedManagedWindowsPackages);
if (process.platform === "win32") {
await check(t, testWindowsLongExecutableLaunches);
await check(t, testWindowsRootRelativePathsStayBoundToOriginalDrive);
@@ -217,10 +215,8 @@ try {
});
await test("Deep worker diagnostics", async (t) => {
+ await check(t, testFailedWorkerItems);
await check(t, testCodeModeFrameDiagnosticBoundaries);
- await check(t, testUsageLimitClassification);
- await check(t, testOsErrorCodesRemainRetryable);
- await check(t, testWorkerErrorClassificationPreservesExplicitFailures);
if (process.platform !== "win32") {
await check(t, testRetryNotificationDoesNotInterruptTurn);
await check(t, testSandboxNamespaceDiagnosticIsPreserved);
@@ -318,81 +314,6 @@ function testConfiguredExecutablePaths() {
);
}
-function testOsErrorCodesRemainRetryable() {
- for (const code of ["ENOENT", "EACCES", "ENOEXEC", "EPERM"]) {
- const original = Object.assign(new Error(`spawn codex ${code}`), { code });
- const classified = classifyCodexWorkerError(original);
- assert.equal(classified, original);
- assert.equal(classified.name, "Error");
- }
-}
-
-function testWorkerErrorClassificationPreservesExplicitFailures() {
- const explicit = new DeepScanNonRetryableError(
- "Explicitly rejected worker permission profile.",
- );
- assert.equal(classifyCodexWorkerError(explicit), explicit);
-
- const refusalMessages = [
- "Request blocked by cyberPolicy.",
- "Request blocked by a safety policy violation.",
- "This content was flagged for possible cybersecurity risk.",
- "This content was flagged for potentially high-risk cyber activity.",
- "This request has been flagged for possible cybersecurity risk.",
- "This request has been flagged for potentially high-risk cyber activity.",
- ];
- for (const message of refusalMessages) {
- const original = new Error(message);
- const classified = classifyCodexWorkerError(original);
- assert.equal(classified, original);
- assert.equal(classified.name, "Error");
- assert.equal(isCodexCybersecurityPolicyRefusal(classified), true);
-
- const explicitRefusal = new DeepScanNonRetryableError(message);
- assert.equal(classifyCodexWorkerError(explicitRefusal), explicitRefusal);
-
- for (const unrecognized of [
- `Source fixture: ${message}`,
- `${message} Source fixture.`,
- JSON.stringify({ message }),
- `Codex Exec exited with code 1: ${message}`,
- `429 Too Many Requests: ${message}`,
- ` ${message}`,
- `${message}\n`,
- message.toLowerCase(),
- ]) {
- const unrelated = new Error(unrecognized);
- assert.equal(classifyCodexWorkerError(unrelated), unrelated);
- assert.equal(isCodexCybersecurityPolicyRefusal(unrelated), false);
- }
- }
-
- for (const message of [
- "config parser handles unknown keys",
- "failed to load configuration: invalid value",
- "agents.max_threads cannot be set when features.multi_agent_v2 is enabled",
- "authentication required",
- "not logged in",
- "missing API key",
- "Codex Exec exited with code 2: failed to load configuration: invalid value",
- "Codex Exec exited with code 1:\nError: No such file or directory (os error 2)",
- "Codex Exec exited with code 1:\nError: The system cannot find the file specified. (os error 2)",
- "chatgpt authentication required for remote plugin catalog; api key auth is not supported",
- "chatgpt authentication required to sync remote plugins; api key auth is not supported",
- "required MCP servers failed to initialize: codex_security_artifacts: timed out handshaking with MCP server after 30s",
- "required MCP servers failed to initialize: other_server: request timed out",
- ]) {
- const original = new Error(message);
- assert.equal(classifyCodexWorkerError(original), original);
- }
- for (const code of ["ECONNRESET", "EPIPE", "ETIMEDOUT", "cyber_policy"]) {
- const original = Object.assign(new Error("Unrecognized worker failure"), {
- code,
- });
- assert.equal(classifyCodexWorkerError(original), original);
- }
-}
-
async function testWindowsRootRelativePathsStayBoundToOriginalDrive() {
assert.equal(
resolveCodexPath(
@@ -839,6 +760,38 @@ async function testWindowsNpmPackageResolution(installation = "global") {
}
}
+async function testHoistedManagedWindowsPackages() {
+ const root = await temporaryDirectories.create("codex-managed-cli-");
+ const packages = path.join(root, "managed CLI", "node_modules", "@openai");
+ const codex = path.join(packages, "codex");
+ await mkdir(codex, { recursive: true });
+ await writeJson(path.join(codex, "package.json"), {});
+ const env = { PATH: "", CODEX_MANAGED_PACKAGE_ROOT: ` ${codex} ` };
+ for (const [arch, triple] of [
+ ["x64", "x86_64-pc-windows-msvc"],
+ ["arm64", "aarch64-pc-windows-msvc"],
+ ] as const) {
+ const platform = path.join(packages, `codex-win32-${arch}`);
+ const executable = path.join(
+ platform,
+ "vendor",
+ triple,
+ "bin",
+ "codex.exe",
+ );
+ await mkdir(path.dirname(executable), { recursive: true });
+ await writeJson(path.join(platform, "package.json"), {});
+ await writeFile(executable, "synthetic executable");
+ assert.equal(resolveCodexPath(env, "win32", arch), executable);
+ const override = path.join(root, "custom CLI", "codex.exe");
+ assert.equal(
+ resolveCodexPath({ ...env, CODEX_CLI_PATH: override }, "win32", arch),
+ override,
+ );
+ assert.equal(resolveCodexPath(env, "linux", arch), path.resolve("codex"));
+ }
+}
+
function windowsLauncherEnvironment(...directories: string[]) {
const environment = { ...process.env };
for (const key of Object.keys(environment)) {
@@ -2337,6 +2290,7 @@ env_key = "SYNTHETIC_GATEWAY_KEY"`,
}
assert.equal(invocation.python, python);
assertConfigOverrides(invocation.argv, {
+ "mcp_servers.codex-security.enabled": false,
"mcp_servers.cs_artifacts.command": process.execPath,
"mcp_servers.cs_artifacts.env.CODEX_SECURITY_INCLUDE_PATHS_JSON":
undefined,
@@ -2360,6 +2314,10 @@ env_key = "SYNTHETIC_GATEWAY_KEY"`,
string,
{ env: Record<string, string> }
>;
+ assert.deepEqual(Object.keys(servers).sort(), [
+ "codex-security",
+ "cs_artifacts",
+ ]);
const scopeFile =
servers["cs_artifacts"]!.env["CODEX_SECURITY_INCLUDE_PATHS_FILE"];
if (index % 2 === 0) {
@@ -3372,6 +3330,51 @@ async function testOwnedArtifactToolFailureDiagnosticIsPreserved() {
}
}
+function testFailedWorkerItems() {
+ const diagnostics: CodexWorkerDiagnostic[] = [];
+ for (const [server, tool, error, result, expected] of [
+ [
+ "cs_artifacts",
+ "record_codex_security_deep_reduction",
+ null,
+ { isError: true, content: [{ type: "text", text: "provider detail" }] },
+ "provider detail",
+ ],
+ [
+ "cs_artifacts",
+ "additional_codex_security_worker_tool",
+ { message: "transport failure" },
+ null,
+ "transport failure",
+ ],
+ [
+ "codex_security_artifacts",
+ "record_codex_security_discovery_candidates",
+ null,
+ null,
+ "Codex worker artifact tool record_codex_security_discovery_candidates failed.",
+ ],
+ ] as const) {
+ appendItemDiagnostic(diagnostics, {
+ type: "mcp_tool_call",
+ status: "failed",
+ server,
+ tool,
+ error,
+ result,
+ arguments: { token: "synthetic-input-only" },
+ });
+ assert.deepEqual(diagnostics.pop(), {
+ code: "artifact_tool_failed",
+ message: expected,
+ });
+ }
+ appendItemDiagnostic(diagnostics, { type: "file_change", status: "failed" });
+ assert.deepEqual(diagnostics, [
+ { code: "file_change_failed", message: "Codex worker file change failed." },
+ ]);
+}
+
function testCodeModeFrameDiagnosticBoundaries() {
const failedArtifactTool = {
type: "mcp_tool_call",
@@ -4060,30 +4063,6 @@ async function testWorkerCancellation() {
}
}
-function testUsageLimitClassification() {
- for (const message of [
- "Quota exceeded. Check your plan and billing details.",
- "Your workspace is out of credits. Add credits to continue.",
- "Your workspace is out of credits. Ask your workspace owner to refill in order to continue.",
- "You hit your spend cap set in your workspace. Increase your spend cap to continue.",
- "You hit your spend cap set by the owner of your workspace. Ask an owner to increase your spend cap to continue.",
- "You’ve hit your usage limit. Try again later.",
- "You’ve hit your usage limit for the selected model. Switch to another model to continue.",
- ]) {
- const error = codexWorkerTurnError(message);
- assert.ok(error instanceof DeepScanNonRetryableError);
- assert.equal(error.message, message);
- }
- for (const message of [
- "429 Too Many Requests",
- "Command failed: Quota exceeded. Check your plan and billing details.",
- ]) {
- const error = codexWorkerTurnError(message);
- assert.equal(error.name, "Error");
- assert.equal(error.message, message);
- }
-}
-
async function testWorkerTerminalEvents() {
const fixture = await fakeCodexFixture();
const previousPath = process.env.CODEX_CLI_PATH;
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_parent_sandbox.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_parent_sandbox.ts
index 1fd7b135a..f7ad9ca67 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_parent_sandbox.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_parent_sandbox.ts
@@ -66,6 +66,7 @@ assert.deepEqual(
restricted(
[
rootRead,
+ { path: { type: "path", path: "/repo" }, access: "write" },
{
path: { type: "path", path: "/repo/.env" },
access: "deny",
@@ -190,6 +191,21 @@ assert.deepEqual(
},
);
+assert.throws(
+ () => resolveDeepWorkerParentSandbox({}),
+ /trusted parent sandbox metadata/,
+);
+for (const entry of [
+ { access: "deny", path: { type: "path", path: "relative/secret" } },
+ { access: "deny", path: { type: "special", value: { kind: "tmpdir" } } },
+ { access: "write", path: { type: "glob_pattern", pattern: "/repo/*" } },
+]) {
+ assert.throws(
+ () => resolveDeepWorkerParentSandbox(restricted([rootRead, entry])),
+ /cannot be preserved/,
+ );
+}
+
for (const invalid of [
undefined,
null,
diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_process.ts b/plugins/codex-security/mcp-app/tests/test_workbench_process.ts
index 34b4a7e6e..def632274 100644
--- a/plugins/codex-security/mcp-app/tests/test_workbench_process.ts
+++ b/plugins/codex-security/mcp-app/tests/test_workbench_process.ts
@@ -8,20 +8,6 @@ import { promisify } from "node:util";
import { temporaryDirectory } from "./support/temporary-directories.ts";
import { loadWorkbenchProcess } from "./support/workbench-process.ts";
import { importSource } from "./import-module.ts";
-import { PassThrough } from "node:stream";
-import type { ExecFileOptionsWithStringEncoding } from "node:child_process";
-
-type WorkbenchProcessFixture = (
- python: string,
- args: string[],
- options: ExecFileOptionsWithStringEncoding,
-) => Promise<{ stdout: string; stderr: string }> & {
- child: { stdin: PassThrough };
-};
-
-declare global {
- var workbenchProcessFixture: WorkbenchProcessFixture | undefined;
-}
interface WorkbenchModule {
executeWorkbench(
@@ -31,88 +17,6 @@ interface WorkbenchModule {
options?: { isolatedPython?: boolean },
): Promise<Record<string, unknown>>;
}
-const invocations: {
- args: string[];
- options: ExecFileOptionsWithStringEncoding;
-}[] = [];
-let failure: Error | undefined;
-globalThis.workbenchProcessFixture = (_python, args, options) => {
- invocations.push({ args, options });
- return Object.assign(
- failure
- ? Promise.reject(failure)
- : Promise.resolve({ stdout: "{}", stderr: "" }),
- { child: { stdin: new PassThrough() } },
- );
-};
-try {
- const { executeWorkbench } = (await loadWorkbenchProcess((source) =>
- source.replace(
- "const execFileAsync = promisify(execFile);",
- "const execFileAsync = globalThis.workbenchProcessFixture;",
- ),
- )) as WorkbenchModule;
- for (const command of [
- "cancel-scan",
- "fail-scan",
- "preserve-scan-results",
- "complete-scan",
- "finding-issues",
- "start-prompt-only-scan",
- ]) {
- await executeWorkbench("fixture-python", [command, "--scan-id", "fixture"]);
- assert.equal(
- invocations.at(-1)!.options.timeout,
- 300_000,
- `${command} must allow its saved-result publication to finish`,
- );
- }
- await executeWorkbench("fixture-python", [
- "inspect-target",
- "--target-path",
- "/fixture",
- ]);
- assert.equal(invocations.at(-1)!.options.timeout, 30_000);
- failure = Object.assign(
- new Error("Command failed: fixture-python\nfixture diagnostic"),
- {
- killed: true,
- signal: "SIGTERM",
- stderr: "fixture diagnostic",
- stdout: "",
- },
- );
- await assert.rejects(
- executeWorkbench("fixture-python", ["cancel-scan"]),
- (error: unknown) => {
- assert.ok(error instanceof Error);
- assert.match(error.message, /cancel-scan.*timed out.*300/);
- assert.match(error.message, /fixture diagnostic/);
- assert.equal(error.cause, failure);
- return true;
- },
- );
- await assert.rejects(
- promisify(execFile)(
- process.execPath,
- ["-e", 'process.stdout.write("x".repeat(65536))'],
- { maxBuffer: 1024 },
- ),
- (error: unknown) => {
- assert.ok(error instanceof Error);
- assert.ok("code" in error);
- assert.equal(error.code, "ERR_CHILD_PROCESS_STDIO_MAXBUFFER");
- failure = error;
- return true;
- },
- );
- await assert.rejects(
- executeWorkbench("fixture-python", ["cancel-scan"]),
- (error) => error === failure,
- );
-} finally {
- delete globalThis.workbenchProcessFixture;
-}
const { executeWorkbench, runWorkbench } = await loadWorkbenchProcess();
const { workbenchArgs } = await importSource(
diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_timeouts.ts b/plugins/codex-security/mcp-app/tests/test_workbench_timeouts.ts
new file mode 100644
index 000000000..4d755cabd
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/test_workbench_timeouts.ts
@@ -0,0 +1,104 @@
+import assert from "node:assert/strict";
+import {
+ execFile,
+ type ExecFileOptionsWithStringEncoding,
+} from "node:child_process";
+import { PassThrough } from "node:stream";
+import { promisify } from "node:util";
+import { loadWorkbenchProcess } from "./support/workbench-process.ts";
+
+type WorkbenchProcessFixture = (
+ python: string,
+ args: string[],
+ options: ExecFileOptionsWithStringEncoding,
+) => Promise<{ stdout: string; stderr: string }> & {
+ child: { stdin: PassThrough };
+};
+
+declare global {
+ var workbenchProcessFixture: WorkbenchProcessFixture | undefined;
+}
+
+const invocations: {
+ args: string[];
+ options: ExecFileOptionsWithStringEncoding;
+}[] = [];
+let failure: Error | undefined;
+globalThis.workbenchProcessFixture = (_python, args, options) => {
+ invocations.push({ args, options });
+ return Object.assign(
+ failure
+ ? Promise.reject(failure)
+ : Promise.resolve({ stdout: "{}", stderr: "" }),
+ { child: { stdin: new PassThrough() } },
+ );
+};
+try {
+ const { executeWorkbench } = await loadWorkbenchProcess((source) =>
+ source.replace(
+ "const execFileAsync = promisify(execFile);",
+ "const execFileAsync = globalThis.workbenchProcessFixture;",
+ ),
+ );
+ for (const command of [
+ "cancel-scan",
+ "fail-scan",
+ "preserve-scan-results",
+ "complete-scan",
+ "finding-issues",
+ "start-prompt-only-scan",
+ "start-scan",
+ ]) {
+ await executeWorkbench("fixture-python", [command, "--scan-id", "fixture"]);
+ assert.equal(
+ invocations.at(-1)!.options.timeout,
+ 300_000,
+ `${command} must allow its saved-result publication to finish`,
+ );
+ }
+ await executeWorkbench("fixture-python", [
+ "inspect-target",
+ "--target-path",
+ "/fixture",
+ ]);
+ assert.equal(invocations.at(-1)!.options.timeout, 30_000);
+ failure = Object.assign(
+ new Error("Command failed: fixture-python\nfixture diagnostic"),
+ {
+ killed: true,
+ signal: "SIGTERM",
+ stderr: "fixture diagnostic",
+ stdout: "",
+ },
+ );
+ await assert.rejects(
+ executeWorkbench("fixture-python", ["cancel-scan"]),
+ (error: unknown) => {
+ assert.ok(error instanceof Error);
+ assert.match(error.message, /cancel-scan.*timed out.*300/);
+ assert.match(error.message, /fixture diagnostic/);
+ assert.equal(error.cause, failure);
+ return true;
+ },
+ );
+ await assert.rejects(
+ promisify(execFile)(
+ process.execPath,
+ ["-e", 'process.stdout.write("x".repeat(65536))'],
+ { maxBuffer: 1024 },
+ ),
+ (error: unknown) => {
+ assert.ok(error instanceof Error);
+ assert.ok("code" in error);
+ assert.equal(error.code, "ERR_CHILD_PROCESS_STDIO_MAXBUFFER");
+ failure = error;
+ return true;
+ },
+ );
+ await assert.rejects(
+ executeWorkbench("fixture-python", ["cancel-scan"]),
+ (error) => error === failure,
+ );
+} finally {
+ delete globalThis.workbenchProcessFixture;
+}
diff --git a/plugins/codex-security/scripts/workbench_coverage_receipts.py b/plugins/codex-security/scripts/workbench_coverage_receipts.py
index f5fb0402d..8bdde383e 100644
--- a/plugins/codex-security/scripts/workbench_coverage_receipts.py
+++ b/plugins/codex-security/scripts/workbench_coverage_receipts.py
@@ -4,7 +4,6 @@ from __future__ import annotations
import copy
import os
-import re
from collections.abc import Callable, Sequence
from pathlib import Path, PurePosixPath
from typing import Any
@@ -20,22 +19,6 @@ from finalize_scan_contract import (
from workbench.saved_coverage import _project_worker_coverage_row, _saved_coverage_id
-def _checkpoint_head_directory(relative: str) -> Path | None:
- path = Path(relative)
- if path.name == "checkpoint-head.json":
- return path.parent
- if path.parent.name == "checkpoint-heads":
- return path.parent.parent
- return None
-
-
-def _is_source_order_snapshot(relative: str) -> bool:
- path = Path(relative)
- return path.parent == Path("source-order") and bool(
- re.fullmatch(r"[0-9a-f]{64}\.json", path.name)
- )
-
-
def capture_receipt_checkpoints(
relative: str,
kind: str | None,
diff --git a/plugins/codex-security/scripts/workbench_saved_results.py b/plugins/codex-security/scripts/workbench_saved_results.py
index af22cc1d9..12125ca48 100644
--- a/plugins/codex-security/scripts/workbench_saved_results.py
+++ b/plugins/codex-security/scripts/workbench_saved_results.py
@@ -39,55 +39,7 @@ from finalize_scan_contract import (
)
from finalize_scan_contract import finding_candidate_id as finding_candidate_id
from review_state import checkpoint_review_state, review_state, review_summary, uses_legacy_review
-from workbench.retained_coverage import (
- _append_previous_finding as _append_previous_finding,
-)
-from workbench.retained_coverage import (
- _coverage_rows as _coverage_rows,
-)
-from workbench.retained_coverage import (
- _ensure_finding_identities as _ensure_finding_identities,
-)
-from workbench.retained_coverage import (
- _ensure_finding_identity as _ensure_finding_identity,
-)
-from workbench.retained_coverage import (
- _finding_content as _finding_content,
-)
-from workbench.retained_coverage import (
- _finding_identity as _finding_identity,
-)
-from workbench.retained_coverage import (
- _finding_key as _finding_key,
-)
-from workbench.retained_coverage import (
- _has_candidate_metadata as _has_candidate_metadata,
-)
-from workbench.retained_coverage import (
- _identity_candidate as _identity_candidate,
-)
-from workbench.retained_coverage import (
- _inherit_coverage_sources as _inherit_coverage_sources,
-)
-from workbench.retained_coverage import (
- _retained_findings as _retained_findings,
-)
-from workbench.retained_coverage import (
- _semantic_identity_key as _semantic_identity_key,
-)
-from workbench.retained_coverage import (
- _worker_candidate_key as _worker_candidate_key,
-)
from workbench.retained_coverage import merge_saved_results as merge_saved_results
-from workbench.saved_coverage import (
- _deferred_rows as _deferred_rows,
-)
-from workbench.saved_coverage import (
- _merge_tied_parent_observations as _merge_tied_parent_observations,
-)
-from workbench.saved_coverage import (
- _resolved_deferred_rows as _resolved_deferred_rows,
-)
from workbench.saved_coverage import (
_saved_coverage_id as _saved_coverage_id,
)
@@ -115,28 +67,10 @@ from workbench.saved_sources import (
_saved_result_paths,
_source_digests,
)
-from workbench.saved_sources import (
- _capture_parent_observation as _capture_parent_observation,
-)
-from workbench.saved_sources import (
- _checkpoint_head_directory as _checkpoint_head_directory,
-)
from workbench.saved_sources import (
_digest as _digest,
)
-from workbench.saved_sources import (
- _latest_successful_reducer as _latest_successful_reducer,
-)
-from workbench.saved_sources import (
- _parent_publication_ancestors as _parent_publication_ancestors,
-)
-from workbench.saved_sources import (
- _worker_outputs as _worker_outputs,
-)
from workbench_constants import PHASES
-from workbench_coverage_receipts import (
- _deferred_candidate_id as _deferred_candidate_id,
-)
from workbench_coverage_receipts import (
_generic_surface_updates as _generic_surface_updates,
)
diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.py
index ecff064fb..f20e0442b 100644
--- a/plugins/codex-security/scripts/workbench_target.py
+++ b/plugins/codex-security/scripts/workbench_target.py
@@ -3,7 +3,6 @@
from __future__ import annotations
import argparse
-import codecs
import hashlib
import os
import re
@@ -13,7 +12,6 @@ import stat
import subprocess
import sys
import tempfile
-from concurrent.futures import ThreadPoolExecutor
from pathlib import Path, PurePosixPath
from typing import Any, BinaryIO, Callable
@@ -84,112 +82,6 @@ def git_bytes(
return completed.stdout if completed.returncode == 0 else None
-def git_blob_samples(
- target: Path,
- object_names: list[str],
- *,
- git_dir: Path | None = None,
- work_tree: Path | None = None,
-) -> list[tuple[bytes, bool] | None]:
- """Sample ordered blobs and classify all their bytes without buffering the batch."""
- if not object_names:
- return []
-
- # Git before 2.38 rejects -z, so only names with line breaks need it.
- nul_input = any("\n" in name or "\r" in name for name in object_names)
- separator = b"\0" if nul_input else b"\n"
- request = separator.join(os.fsencode(name) for name in object_names) + separator
- read_fd, write_fd = os.pipe()
- with (
- ThreadPoolExecutor(max_workers=1) as reader,
- os.fdopen(read_fd, "rb") as output,
- os.fdopen(write_fd, "wb") as sink,
- ):
-
- def read_samples() -> list[tuple[bytes, bool] | None]:
- # Closing the reader also unblocks Git if framing is invalid.
- with output:
- return _read_git_batch_samples(output, object_names)
-
- samples = reader.submit(read_samples)
- try:
- completed = git_command(
- target,
- "cat-file",
- "--batch",
- *(["-z"] if nul_input else []),
- text=False,
- input_data=request,
- git_dir=git_dir,
- work_tree=work_tree,
- stdout_file=sink,
- )
- finally:
- # Git may fail before producing all requested records.
- sink.close()
- if completed.returncode != 0:
- return [None] * len(object_names)
- try:
- return samples.result()
- except ValueError:
- return [None] * len(object_names)
-
-
-def _read_git_batch_samples(
- output: BinaryIO, object_names: list[str]
-) -> list[tuple[bytes, bool] | None]:
- """Read the older-Git-compatible -z protocol without buffering blob bodies."""
- samples: list[tuple[bytes, bool] | None] = []
- for name in object_names:
- header = output.readline()
- raw_name = os.fsencode(name)
- # Missing lookups echo the whole request, including any embedded newlines.
- if header and raw_name.startswith(header):
- header += output.read(len(raw_name) - len(header)) + output.readline()
- if header in (raw_name + b" missing\n", raw_name + b" ambiguous\n"):
- samples.append(None)
- continue
- fields = header.split(b" ")
- if len(fields) != 3 or not header.endswith(b"\n"):
- raise ValueError("invalid batch header")
- size = int(fields[2])
- if size < 0:
- raise ValueError("invalid blob size")
- # Consume non-blob objects too, so subsequent records stay aligned.
- sample = _read_git_blob_sample(output, size)
- samples.append(sample if fields[1] == b"blob" else None)
- return samples
-
-
-def _is_binary_sample(data: bytes) -> bool:
- if data.startswith((codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE)):
- return "\0" in data.decode("utf-16", errors="ignore")
- return b"\0" in data
-
-
-def _read_git_blob_sample(output: BinaryIO, size: int) -> tuple[bytes, bool]:
- read_size = 64 * 1024
- sample_size = min(size, read_size)
- sample = output.read(sample_size)
- if len(sample) != sample_size:
- raise ValueError("truncated blob")
- binary = _is_binary_sample(sample)
- bom = sample[:2] if sample.startswith((codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE)) else b""
- remaining = size - sample_size
- while remaining:
- chunk_size = min(remaining, read_size)
- chunk = output.read(chunk_size)
- if len(chunk) != chunk_size:
- raise ValueError("truncated blob")
- # Even-sized reads keep UTF-16 code units aligned; preserve the original byte order.
- if not binary:
- binary = _is_binary_sample(bom + chunk) if bom else b"\0" in chunk
- remaining -= chunk_size
- if output.read(1) != b"\n":
- raise ValueError("missing blob terminator")
- return (b"", True) if binary else (sample, False)
-
-
def _protected_repository_root(target: Path) -> Path:
root = target.resolve()
if root.is_file():
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 b0cbd048c..edda16ae6 100644
--- a/plugins/codex-security/tests/test_deep_scan_request_identity.py
+++ b/plugins/codex-security/tests/test_deep_scan_request_identity.py
@@ -6,7 +6,11 @@ from pathlib import Path
import pytest
from test_workbench_deep_scan import begin_target_scan
-from workbench_test_support import mark_deep_coordinator_succeeded, run_workbench
+from workbench_test_support import (
+ empty_target_paths,
+ mark_deep_coordinator_succeeded,
+ run_workbench,
+)
@pytest.mark.parametrize(
@@ -20,10 +24,8 @@ from workbench_test_support import mark_deep_coordinator_succeeded, run_workbenc
def test_target_request_does_not_reuse_different_user_context(
tmp_path: Path, first_context: str | None, next_context: str | None
) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
scan_root = tmp_path / "scans"
first = begin_target_scan(state_dir, codex_home, target, scan_root, user_context=first_context)
assert first["deepScan"]["filePaths"] == []
diff --git a/plugins/codex-security/tests/test_git_inventory_regressions.py b/plugins/codex-security/tests/test_git_inventory_regressions.py
index ddc0c2ac6..9e0965bd3 100644
--- a/plugins/codex-security/tests/test_git_inventory_regressions.py
+++ b/plugins/codex-security/tests/test_git_inventory_regressions.py
@@ -42,26 +42,6 @@ def test_sha256_root_commit_uses_the_sha256_empty_tree(tmp_path: Path, workbench
assert git(target, "diff", "--name-only", *revisions) == b"README.md"
-def test_blob_batch_preserves_following_blobs_after_tree_and_commit(tmp_path: Path) -> None:
- target = tmp_path / "target"
- initialize_git_repository(target)
- source = target / ("fixture.py" if os.name == "nt" else "line\nbreak.py")
- source.write_bytes(b"print('fixture')\npayload\n")
- git(target, "add", "--", source.name)
- git(target, "commit", "-qm", "Add unusual fixture")
- reader = load_script("workbench_target").git_blob_samples
- assert reader(
- target,
- ["HEAD^{tree}", "HEAD", f"HEAD:{source.name}", "HEAD:missing\nfile", "HEAD:README.md"],
- ) == [
- None,
- None,
- (source.read_bytes(), False),
- None,
- (b"fixture\n", False),
- ]
-
-
def test_plain_directory_inventory_ignores_nested_git_metadata(tmp_path: Path) -> None:
target = tmp_path / "sources"
target.mkdir()
diff --git a/plugins/codex-security/tests/test_scan_contract_examples.py b/plugins/codex-security/tests/test_scan_contract_examples.py
index 2badc4315..d1236add7 100644
--- a/plugins/codex-security/tests/test_scan_contract_examples.py
+++ b/plugins/codex-security/tests/test_scan_contract_examples.py
@@ -7,7 +7,7 @@ import unittest
from pathlib import Path
from typing import Any
-from jsonschema import Draft202012Validator, FormatChecker
+from jsonschema import Draft202012Validator, FormatChecker, ValidationError
from referencing import Registry, Resource
from workbench_test_support import saved_coverage
@@ -16,56 +16,52 @@ EXAMPLE_DIR = PLUGIN_DIR / "examples" / "legacy-completed-scan"
SCHEMA_DIR = PLUGIN_DIR / "schemas"
-def read_json(path: Path) -> dict[str, Any]:
+def read_object(path: Path) -> dict[str, Any]:
payload = json.loads(path.read_text(encoding="utf-8"))
if not isinstance(payload, dict):
raise AssertionError(f"{path}: expected an object")
return payload
-def validate_schema_node(value: Any, schema: dict[str, Any], context: str) -> None:
- validator = Draft202012Validator(schema, format_checker=FormatChecker())
- errors = sorted(validator.iter_errors(value), key=lambda error: list(error.absolute_path))
- if not errors:
- return
- error = errors[0]
- path = ".".join(str(part) for part in error.absolute_path)
- location = f"{context}.{path}" if path else context
- raise AssertionError(f"{location}: {error.message}") from error
+def example(name: str) -> dict[str, Any]:
+ return read_object(EXAMPLE_DIR / f"{name}.json")
+
+
+def schema_validator(name: str, *references: str) -> Draft202012Validator:
+ def load(name: str) -> dict[str, Any]:
+ return read_object(SCHEMA_DIR / f"{name}.schema.json")
+
+ resources = [load(ref) for ref in references]
+ registry = Registry().with_resources(
+ (resource["$id"], Resource.from_contents(resource)) for resource in resources
+ )
+ return Draft202012Validator(load(name), registry=registry, format_checker=FormatChecker())
class ScanContractExamplesTest(unittest.TestCase):
- def setUp(self) -> None:
- self.manifest = read_json(EXAMPLE_DIR / "scan-manifest.json")
- self.findings = read_json(EXAMPLE_DIR / "findings.json")
- self.coverage = read_json(EXAMPLE_DIR / "coverage.json")
+ def assert_invalid(self, schema: str, payload: dict[str, Any], message: str) -> None:
+ with self.assertRaises(ValidationError) as caught:
+ schema_validator(schema).validate(payload)
+ self.assertRegex(caught.exception.message, message)
def test_examples_match_schemas(self) -> None:
- for name, payload in (
- ("scan-manifest", self.manifest),
- ("findings", self.findings),
- ("coverage", self.coverage),
- ):
- schema = read_json(SCHEMA_DIR / f"{name}.schema.json")
- validate_schema_node(payload, schema, name)
+ for name in ("scan-manifest", "findings", "coverage"):
+ with self.subTest(name=name):
+ schema_validator(name).validate(example(name))
def test_schemas_are_valid_draft_2020_12(self) -> None:
for schema_path in sorted(SCHEMA_DIR.glob("*.schema.json")):
with self.subTest(schema=schema_path.name):
- Draft202012Validator.check_schema(read_json(schema_path))
+ Draft202012Validator.check_schema(read_object(schema_path))
def test_deep_reducer_schema_accepts_standard_findings_without_coverage(self) -> None:
- common_schema = read_json(SCHEMA_DIR / "definitions" / "artifact-common.schema.json")
- scan_draft_schema = read_json(SCHEMA_DIR / "tools" / "scan-draft.schema.json")
- reducer_schema = read_json(SCHEMA_DIR / "tools" / "deep-reducer.schema.json")
- reduction_input = reducer_schema["$defs"]["reductionInput"]
+ validator = schema_validator(
+ "tools/deep-reducer", "definitions/artifact-common", "tools/scan-draft"
+ )
+ reduction_input = validator.schema["$defs"]["reductionInput"]
self.assertNotIn("reviewState", reduction_input["properties"])
self.assertEqual(set(reduction_input["required"]), {"scanId", "findings"})
self.assertFalse(reduction_input["additionalProperties"])
- registry = Registry().with_resources(
- (schema["$id"], Resource.from_contents(schema))
- for schema in (common_schema, scan_draft_schema)
- )
finding = {
"ruleId": "cross-site-scripting.response",
"identity": {"anchor": "response-output"},
@@ -86,12 +82,7 @@ class ScanContractExamplesTest(unittest.TestCase):
"explicitExclusions": [{"pattern": "docs/", "reason": "Documentation only."}],
}
- Draft202012Validator.check_schema(reducer_schema)
- validator = Draft202012Validator(
- reducer_schema,
- registry=registry,
- format_checker=FormatChecker(),
- )
+ Draft202012Validator.check_schema(validator.schema)
request = {
"scanId": "7fc17317-9594-49e0-b06a-d72fd7e14bba",
"findings": [finding],
@@ -110,9 +101,7 @@ class ScanContractExamplesTest(unittest.TestCase):
validator.validate({**request, "findings": []})
self.assertFalse(validator.is_valid({**request, "reviewState": coverage}))
- standard_validator = Draft202012Validator(
- scan_draft_schema, registry=registry, format_checker=FormatChecker()
- )
+ standard_validator = schema_validator("tools/scan-draft", "definitions/artifact-common")
standard_request = {
**request,
"reviewState": {**coverage, "deferred": []},
@@ -122,17 +111,10 @@ class ScanContractExamplesTest(unittest.TestCase):
self.assertFalse(standard_validator.is_valid({**request, "reviewState": coverage}))
for missing_field in ("surfaces", "explicitExclusions", "deferred"):
with self.subTest(standard_missing_coverage_field=missing_field):
+ partial = dict(standard_request["reviewState"])
+ del partial[missing_field]
self.assertFalse(
- standard_validator.is_valid(
- {
- **standard_request,
- "reviewState": {
- field: value
- for field, value in standard_request["reviewState"].items()
- if field != missing_field
- },
- }
- )
+ standard_validator.is_valid({**standard_request, "reviewState": partial})
)
for extra_field in (
"source_worker_id",
@@ -176,9 +158,9 @@ class ScanContractExamplesTest(unittest.TestCase):
self.assertFalse(validator.is_valid({"candidates": [], "merges": []}))
def test_manifest_identifies_and_hashes_the_example_documents(self) -> None:
- scan = self.manifest["scan"]
- self.assertEqual(scan["id"], self.findings["scanId"])
- self.assertEqual(scan["id"], self.coverage["scanId"])
+ scan = example("scan-manifest")["scan"]
+ self.assertEqual(scan["id"], example("findings")["scanId"])
+ self.assertEqual(scan["id"], example("coverage")["scanId"])
self.assertEqual(scan["findingsRef"], "findings.json")
self.assertEqual(scan["coverageRef"], "coverage.json")
self.assertEqual(
@@ -189,7 +171,6 @@ class ScanContractExamplesTest(unittest.TestCase):
self.assertEqual(hashlib.sha256(path.read_bytes()).hexdigest(), artifact["sha256"])
def test_manifest_requires_snapshot_signal_for_target_kind(self) -> None:
- schema = read_json(SCHEMA_DIR / "scan-manifest.schema.json")
for kind, missing_field in (
("git_revision", "revision"),
("git_worktree", "snapshotDigest"),
@@ -197,45 +178,37 @@ class ScanContractExamplesTest(unittest.TestCase):
("directory_snapshot", "snapshotDigest"),
):
with self.subTest(kind=kind):
- manifest = copy.deepcopy(self.manifest)
+ manifest = example("scan-manifest")
target = manifest["scan"]["target"]
target["kind"] = kind
target.pop(missing_field, None)
- with self.assertRaisesRegex(AssertionError, missing_field):
- validate_schema_node(manifest, schema, "scan-manifest")
+ self.assert_invalid("scan-manifest", manifest, missing_field)
def test_manifest_rejects_unversioned_snapshot_digest(self) -> None:
- schema = read_json(SCHEMA_DIR / "scan-manifest.schema.json")
- manifest = copy.deepcopy(self.manifest)
+ manifest = example("scan-manifest")
manifest["scan"]["target"]["snapshotDigest"] = "sha256:worktree-example"
- with self.assertRaisesRegex(AssertionError, "does not match"):
- validate_schema_node(manifest, schema, "scan-manifest")
+ self.assert_invalid("scan-manifest", manifest, "does not match")
def test_manifest_rejects_invalid_timestamp(self) -> None:
- schema = read_json(SCHEMA_DIR / "scan-manifest.schema.json")
- manifest = copy.deepcopy(self.manifest)
+ manifest = example("scan-manifest")
manifest["scan"]["startedAt"] = "2026-99-99T00:00:00Z"
- with self.assertRaisesRegex(AssertionError, "is not a 'date-time'"):
- validate_schema_node(manifest, schema, "scan-manifest")
+ self.assert_invalid("scan-manifest", manifest, "is not a 'date-time'")
def test_manifest_rejects_credential_bearing_remote(self) -> None:
- schema = read_json(SCHEMA_DIR / "scan-manifest.schema.json")
for remote in (
"https://[email protected]/repo",
"https://example.com/repo?token=secret",
"https://example.com/repo#token",
):
with self.subTest(remote=remote):
- manifest = copy.deepcopy(self.manifest)
+ manifest = example("scan-manifest")
manifest["scan"]["target"]["remote"] = remote
- with self.assertRaisesRegex(AssertionError, "does not match"):
- validate_schema_node(manifest, schema, "scan-manifest")
+ self.assert_invalid("scan-manifest", manifest, "does not match")
def test_manifest_rejects_unsafe_artifact_path(self) -> None:
- schema = read_json(SCHEMA_DIR / "scan-manifest.schema.json")
for path in ("../../secret.json", "/tmp/secret.json", "exports\\results.sarif"):
with self.subTest(path=path):
- manifest = copy.deepcopy(self.manifest)
+ manifest = example("scan-manifest")
manifest["scan"]["artifacts"].append(
{
"path": path,
@@ -243,59 +216,43 @@ class ScanContractExamplesTest(unittest.TestCase):
"mediaType": "application/json",
}
)
- with self.assertRaisesRegex(AssertionError, "does not match"):
- validate_schema_node(manifest, schema, "scan-manifest")
+ self.assert_invalid("scan-manifest", manifest, "does not match")
def test_manifest_requires_one_record_for_each_canonical_document(self) -> None:
- schema = read_json(SCHEMA_DIR / "scan-manifest.schema.json")
for path in ("findings.json", "coverage.json"):
with self.subTest(path=path):
- manifest = copy.deepcopy(self.manifest)
+ manifest = example("scan-manifest")
artifacts = manifest["scan"]["artifacts"]
manifest["scan"]["artifacts"] = [
artifact for artifact in artifacts if artifact["path"] != path
]
- with self.assertRaisesRegex(AssertionError, "does not contain items matching"):
- validate_schema_node(manifest, schema, "scan-manifest")
+ self.assert_invalid("scan-manifest", manifest, "does not contain items matching")
manifest["scan"]["artifacts"] = artifacts + [copy.deepcopy(artifacts[0])]
manifest["scan"]["artifacts"][-1]["path"] = path
- with self.assertRaisesRegex(AssertionError, "Too many items match"):
- validate_schema_node(manifest, schema, "scan-manifest")
+ self.assert_invalid("scan-manifest", manifest, "Too many items match")
def test_manifest_accepts_only_fixed_hardening_portfolio_path(self) -> None:
- schema = read_json(SCHEMA_DIR / "scan-manifest.schema.json")
- manifest = copy.deepcopy(self.manifest)
+ manifest = example("scan-manifest")
manifest["scan"]["hardening"] = {"portfolioPath": "hardening/hardening.md"}
- validate_schema_node(manifest, schema, "scan-manifest")
+ schema_validator("scan-manifest").validate(manifest)
manifest["scan"]["hardening"]["portfolioPath"] = "../hardening.md"
- with self.assertRaisesRegex(AssertionError, "was expected"):
- validate_schema_node(manifest, schema, "scan-manifest")
+ self.assert_invalid("scan-manifest", manifest, "was expected")
def test_findings_accept_safe_writeup_and_reject_unsafe_path(self) -> None:
- schema = read_json(SCHEMA_DIR / "findings.schema.json")
- findings = copy.deepcopy(self.findings)
+ findings = example("findings")
findings["findings"][0]["writeup"] = {
"reportPath": "findings/unsafe-archive/unsafe-archive.md"
}
- validate_schema_node(findings, schema, "findings")
+ schema_validator("findings").validate(findings)
findings["findings"][0]["writeup"]["reportPath"] = "../outside.md"
- with self.assertRaisesRegex(AssertionError, "does not match"):
- validate_schema_node(findings, schema, "findings")
+ self.assert_invalid("findings", findings, "does not match")
def test_findings_accept_code_evidence_call_stack_role(self) -> None:
- schema = read_json(SCHEMA_DIR / "findings.schema.json")
- common_schema = read_json(SCHEMA_DIR / "definitions" / "artifact-common.schema.json")
- draft_schema = read_json(SCHEMA_DIR / "tools" / "scan-draft.schema.json")
- registry = Registry().with_resource(
- common_schema["$id"], Resource.from_contents(common_schema)
- )
- draft_validator = Draft202012Validator(
- draft_schema, registry=registry, format_checker=FormatChecker()
- )
- findings = copy.deepcopy(self.findings)
+ draft_validator = schema_validator("tools/scan-draft", "definitions/artifact-common")
+ findings = example("findings")
findings["findings"][0]["codeEvidence"] = [
{
"id": "request-input",
@@ -307,7 +264,7 @@ class ScanContractExamplesTest(unittest.TestCase):
"explanation": "The request controls value before the handler forwards it.",
}
]
- validate_schema_node(findings, schema, "findings")
+ schema_validator("findings").validate(findings)
draft_finding = {
key: value
for key, value in findings["findings"][0].items()
@@ -327,29 +284,26 @@ class ScanContractExamplesTest(unittest.TestCase):
value = evidence.pop(field)
if replacement is not None:
evidence[replacement] = value
- with self.assertRaisesRegex(AssertionError, f"'{field}' is a required property"):
- validate_schema_node(invalid_findings, schema, "findings")
+ self.assert_invalid(
+ "findings", invalid_findings, f"'{field}' is a required property"
+ )
invalid_draft = copy.deepcopy(draft)
invalid_draft["findings"][0]["codeEvidence"][0] = evidence
self.assertFalse(draft_validator.is_valid(invalid_draft))
findings["findings"][0]["codeEvidence"][0]["role"] = ""
- with self.assertRaisesRegex(AssertionError, "should be non-empty"):
- validate_schema_node(findings, schema, "findings")
+ self.assert_invalid("findings", findings, "should be non-empty")
def test_coverage_rejects_receipt_ref_outside_artifacts(self) -> None:
- schema = read_json(SCHEMA_DIR / "coverage.schema.json")
for ref in ("report.md", "artifacts/../report.md", "artifacts\\receipt.jsonl"):
with self.subTest(ref=ref):
- coverage = copy.deepcopy(self.coverage)
+ coverage = example("coverage")
coverage["surfaces"][0]["receiptRefs"] = [ref]
- with self.assertRaisesRegex(AssertionError, "does not match"):
- validate_schema_node(coverage, schema, "coverage")
+ self.assert_invalid("coverage", coverage, "does not match")
def test_coverage_accepts_unresolved_work(self) -> None:
- schema = read_json(SCHEMA_DIR / "coverage.schema.json")
- coverage = copy.deepcopy(self.coverage)
+ coverage = example("coverage")
coverage["surfaces"][0]["disposition"] = "needs_follow_up"
coverage["deferred"] = [
{
@@ -357,7 +311,7 @@ class ScanContractExamplesTest(unittest.TestCase):
"reason": "Runtime verification remains unavailable.",
}
]
- validate_schema_node(coverage, schema, "coverage")
+ schema_validator("coverage").validate(coverage)
if __name__ == "__main__":
diff --git a/plugins/codex-security/tests/test_windows_report_e2e.py b/plugins/codex-security/tests/test_windows_report_e2e.py
index 84404e84b..6bdb8a6a3 100644
--- a/plugins/codex-security/tests/test_windows_report_e2e.py
+++ b/plugins/codex-security/tests/test_windows_report_e2e.py
@@ -11,6 +11,7 @@ from unittest import mock
from workbench_test_support import (
SCRIPT,
+ empty_target_paths,
start_saved_scan,
windows_file_backend,
write_completed_contract,
@@ -24,9 +25,7 @@ def test_workbench_completion_and_exports_use_windows_file_backend(tmp_path: Pat
separately cover the Win32 handle semantics.
"""
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
source = target / "src" / "extract.py"
source.parent.mkdir()
source.write_text("".join(f"line {line}\n" for line in range(1, 46)))
diff --git a/plugins/codex-security/tests/test_workbench_cancellation.py b/plugins/codex-security/tests/test_workbench_cancellation.py
index 4ab6a5dfe..5c4329dc3 100644
--- a/plugins/codex-security/tests/test_workbench_cancellation.py
+++ b/plugins/codex-security/tests/test_workbench_cancellation.py
@@ -6,6 +6,7 @@ from pathlib import Path
from workbench_test_support import (
cancel_scan,
create_saved_workspace,
+ empty_target_paths,
fail_scan,
get_scan,
mark_handoff_delivered,
@@ -18,9 +19,7 @@ from workbench_test_support import (
def test_workbench_records_scan_failure(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_scan_command(
state_dir, str(saved["id"]), "--scan-root", str(tmp_path / "scans")
@@ -41,9 +40,7 @@ def test_workbench_records_scan_failure(tmp_path: Path) -> None:
def test_workbench_cancels_running_scan_and_rejects_late_updates(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
thread_id = "thread-cancel-owner"
saved = create_saved_workspace(state_dir, target, thread_id=thread_id)
started = start_scan_command(state_dir, str(saved["id"]))
@@ -91,9 +88,7 @@ def test_workbench_cancels_running_scan_and_rejects_late_updates(tmp_path: Path)
def test_workbench_rejects_unconfirmed_cross_thread_handoff_delivery(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
thread_id = "thread-handoff-owner"
saved = create_saved_workspace(state_dir, target, thread_id=thread_id)
started = start_scan_command(state_dir, str(saved["id"]))
@@ -119,9 +114,7 @@ def test_workbench_rejects_unconfirmed_cross_thread_handoff_delivery(tmp_path: P
def test_workbench_allows_user_confirmed_recovery_in_another_thread(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target, thread_id="thread-recovery-owner")
started = start_scan_command(state_dir, str(saved["id"]))
scan_id = str(started["results"]["scanId"])
diff --git a/plugins/codex-security/tests/test_workbench_completion_binding.py b/plugins/codex-security/tests/test_workbench_completion_binding.py
index 6a6c3ca75..f651ec841 100644
--- a/plugins/codex-security/tests/test_workbench_completion_binding.py
+++ b/plugins/codex-security/tests/test_workbench_completion_binding.py
@@ -16,6 +16,7 @@ import pytest
from workbench_test_support import (
create_saved_workspace,
create_workspace,
+ empty_target_paths,
empty_target_scan,
fail_scan,
get_scan,
@@ -44,9 +45,7 @@ def _start_scan_with_draft_findings(tmp_path: Path) -> tuple[Path, str, Path]:
def _start_deep_scan_with_draft_findings(tmp_path: Path) -> tuple[Path, str, Path]:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(
state_dir, target, thread_id="thread-completion-binding", mode="deep"
)
@@ -392,9 +391,7 @@ def test_cli_scan_completes_when_draft_omits_registered_scope(
def test_prepared_completion_does_not_publish_scan_before_acceptance(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
scan_dir = tmp_path / "scan"
registered = register_cli_scan(state_dir, target, scan_dir)
scan_id = str(registered["scanId"])
@@ -477,9 +474,7 @@ def test_incompatible_sealed_deep_scan_remains_recoverable(tmp_path: Path) -> No
def test_rejected_prepared_completion_can_be_marked_failed(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
scan_dir = tmp_path / "scan"
registered = register_cli_scan(state_dir, target, scan_dir)
scan_id = str(registered["scanId"])
diff --git a/plugins/codex-security/tests/test_workbench_db.py b/plugins/codex-security/tests/test_workbench_db.py
index 99cf34aa7..ade7a1109 100644
--- a/plugins/codex-security/tests/test_workbench_db.py
+++ b/plugins/codex-security/tests/test_workbench_db.py
@@ -22,6 +22,7 @@ from workbench_test_support import (
create_saved_git_workspace,
create_saved_workspace,
create_workspace,
+ empty_target_paths,
empty_target_scan,
fail_scan,
get_scan,
@@ -113,9 +114,7 @@ def budget_scan_fixture(
terminal: bool = True,
terminal_reason: str = "saturated",
) -> tuple[Path, Path, Path, str, Path]:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "app.py").write_text("value = request.args['value']\n")
scan_dir = tmp_path / "scan"
scan_dir.mkdir(mode=0o700)
@@ -578,9 +577,7 @@ def test_budget_exhaustion_rejects_symlink_candidate_ledger(tmp_path: Path) -> N
def test_workbench_reopens_workspace_only_from_owning_thread(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace = create_saved_workspace(state_dir, target, thread_id="thread-a")
reopened = workspace_command(
@@ -638,9 +635,7 @@ def test_completion_normalizes_unsealed_deep_inventory_strategy_alias(
def test_completion_warns_after_plain_directory_changes(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
source = target / "app.py"
source.write_text("version = 1\n")
saved = create_saved_workspace(state_dir, target)
@@ -668,9 +663,7 @@ def test_completion_warns_after_plain_directory_changes(tmp_path: Path) -> None:
def test_completion_warns_when_scanned_directory_becomes_unavailable(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "app.py").write_text("version = 1\n")
scan_id, scan_dir = start_saved_scan(state_dir, target, tmp_path / "scans")
write_completed_contract(scan_dir, scan_id, target, relative_path="app.py")
@@ -690,9 +683,7 @@ def test_completion_warns_when_scanned_directory_becomes_unavailable(tmp_path: P
def test_scan_start_rejects_artifact_root_inside_target(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
failed = start_scan_command(
@@ -728,9 +719,7 @@ def test_workbench_serializes_concurrent_scan_completion(tmp_path: Path) -> None
def test_workbench_persists_scan_model_and_updates_it_from_progress(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_delivered_scan(
state_dir,
@@ -769,9 +758,7 @@ def test_workbench_persists_scan_model_and_updates_it_from_progress(tmp_path: Pa
def test_workbench_persists_progress_and_indexes_completed_findings(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
workspace_id = str(saved["id"])
assert saved["userContext"] == "Pay attention to uploaded archives."
@@ -1025,9 +1012,7 @@ def test_reviewed_patch_matches_unversioned_checkout_bytes(
def test_completed_finding_triage_and_remediation_persist(tmp_path: Path, git_autocrlf) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
source = target / "source.txt"
source.write_bytes(b"vulnerable\r\n")
scan_id, scan_dir = start_saved_scan(state_dir, target, tmp_path / "scans")
@@ -1413,9 +1398,7 @@ def test_completed_scan_disables_remediation_after_checkout_revision_changes(
def assert_completed_scan_disables_remediation_after_checkout_path_is_replaced(
tmp_path: Path, *, replacement_kind: str
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "source.txt").write_text("vulnerable\n")
scan_id, scan_dir = start_saved_scan(state_dir, target, tmp_path / "scans")
write_completed_contract(scan_dir, scan_id, target, relative_path="source.txt")
@@ -1722,9 +1705,7 @@ def test_finding_remediation_rejects_delayed_update_after_superseding_patch(tmp_
def test_finding_remediation_rejects_unversioned_directory_changes(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
source = target / "source.txt"
source.write_text("original\n")
scan_id, scan_dir = start_saved_scan(state_dir, target, tmp_path / "scans")
@@ -1773,9 +1754,7 @@ def test_finding_remediation_rejects_unversioned_directory_changes(tmp_path: Pat
def test_workbench_defaults_scan_artifacts_to_persistent_state_dir(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_scan_command(state_dir, str(saved["id"]))
scan_dir = Path(str(started["results"]["scanDir"]))
@@ -1783,9 +1762,7 @@ def test_workbench_defaults_scan_artifacts_to_persistent_state_dir(tmp_path: Pat
def test_workbench_serializes_concurrent_scan_starts(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
workspace_id = str(saved["id"])
with ThreadPoolExecutor(max_workers=2) as executor:
@@ -1805,9 +1782,7 @@ def test_workbench_serializes_concurrent_scan_starts(tmp_path: Path) -> None:
def test_workbench_rejects_setup_changes_after_scan_starts(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
start_scan_command(state_dir, str(saved["id"]))
@@ -1818,9 +1793,7 @@ def test_workbench_rejects_setup_changes_after_scan_starts(tmp_path: Path) -> No
def test_workbench_rejects_scoped_deep_scan(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace_id = str(uuid.uuid4())
create_workspace(state_dir, workspace_id)
failed = save_workspace(state_dir, workspace_id, str(target), "src", "deep", check=False)
@@ -1829,9 +1802,7 @@ def test_workbench_rejects_scoped_deep_scan(tmp_path: Path) -> None:
def test_workbench_rejects_diff_scan_for_non_git_target(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace_id = str(uuid.uuid4())
create_workspace(state_dir, workspace_id)
failed = save_workspace(state_dir, workspace_id, str(target), ".", "diff", check=False)
@@ -2148,9 +2119,7 @@ def test_workbench_preserves_long_user_context(tmp_path: Path) -> None:
def test_workbench_preserves_url_user_context_on_workspace_creation(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
user_context = (
"Repository: https://github.com/example/security-review\n"
"OAuth callback: https://accounts.example.test/oauth/callback"
@@ -2166,9 +2135,7 @@ def test_workbench_preserves_url_user_context_on_workspace_creation(tmp_path: Pa
def test_workbench_replaces_context_only_for_running_owned_scan(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace = create_saved_workspace(state_dir, target)
started = start_delivered_scan(state_dir, "--workspace-id", str(workspace["id"]))
scan_id = str(started["results"]["scanId"])
@@ -2227,9 +2194,7 @@ def test_workbench_replaces_context_only_for_running_owned_scan(tmp_path: Path)
def test_workbench_marks_invalid_initial_scope_for_correction(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace = create_workspace(
state_dir, str(uuid.uuid4()), "--target-path", str(target), "--scope", "missing"
)
@@ -2628,9 +2593,7 @@ def test_workbench_populates_completed_manifest_with_exact_diff_target(tmp_path:
def test_workbench_preserves_invalid_requested_initial_deep_scope(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "src").mkdir()
workspace = create_workspace(
state_dir,
@@ -2655,9 +2618,7 @@ def test_workbench_preserves_invalid_requested_initial_deep_scope(
def test_workbench_discards_diff_target_for_non_diff_mode(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace = create_workspace(
state_dir,
str(uuid.uuid4()),
@@ -2686,9 +2647,7 @@ def test_workbench_discards_diff_target_for_non_diff_mode(tmp_path: Path) -> Non
def test_workbench_rejects_diff_target_when_saving_non_diff_mode(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace_id = str(uuid.uuid4())
create_workspace(state_dir, workspace_id)
@@ -2819,9 +2778,7 @@ def test_review_changes_rejects_unborn_and_bare_git_repositories(tmp_path: Path)
def test_workbench_rejects_missing_or_file_scope(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "file.py").write_text("print('fixture')\n")
workspace_id = str(uuid.uuid4())
create_workspace(state_dir, workspace_id)
@@ -2850,9 +2807,7 @@ def test_workbench_normalizes_absolute_scope_inside_target(tmp_path: Path) -> No
def test_workbench_rejects_scope_escape(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace_id = str(uuid.uuid4())
create_workspace(state_dir, workspace_id)
failed = save_workspace(
@@ -2877,9 +2832,7 @@ def test_workbench_rejects_symlinked_scope_escape(tmp_path: Path) -> None:
def test_workbench_rejects_progress_completed_above_total(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))
failed = update_progress(
@@ -2896,9 +2849,7 @@ def test_workbench_rejects_progress_completed_above_total(tmp_path: Path) -> Non
def test_workbench_rejects_regressive_progress(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))
scan_id = str(started["results"]["scanId"])
@@ -2925,9 +2876,7 @@ def test_workbench_rejects_regressive_progress(tmp_path: Path) -> None:
def test_workbench_tracks_review_pass_for_deep_scan_only(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace_id = str(uuid.uuid4())
create_workspace(state_dir, workspace_id, "--target-path", str(target), "--mode", "deep")
saved = save_workspace(state_dir, workspace_id, str(target), ".", "deep")
@@ -2971,9 +2920,7 @@ def test_workbench_tracks_review_pass_for_deep_scan_only(tmp_path: Path) -> None
def test_workbench_updates_progress_timestamp_for_phase_and_failure(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))
scan_id = str(started["results"]["scanId"])
@@ -2988,9 +2935,7 @@ def test_workbench_updates_progress_timestamp_for_phase_and_failure(tmp_path: Pa
def test_workbench_preserves_scan_when_git_revision_cannot_be_rechecked(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))
scan_id = str(started["results"]["scanId"])
@@ -3028,9 +2973,7 @@ def test_workbench_preserves_scan_when_git_revision_cannot_be_rechecked(tmp_path
def test_completed_finding_projects_writeup_and_poc_artifact_paths(
tmp_path: Path, non_utf8_artifact: bool
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))
scan_id = str(started["results"]["scanId"])
@@ -3327,9 +3270,7 @@ def test_workbench_preserves_dirty_git_scan_after_worktree_changes(tmp_path: Pat
def test_workbench_generates_reports_during_completion(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))
scan_id = str(started["results"]["scanId"])
@@ -3363,9 +3304,7 @@ def test_workbench_omits_unsafe_symlink_source_path_without_hiding_finding(tmp_p
def test_workbench_hides_missing_artifact_on_reopen(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))
scan_id = str(started["results"]["scanId"])
diff --git a/plugins/codex-security/tests/test_workbench_db_exports.py b/plugins/codex-security/tests/test_workbench_db_exports.py
index c2344c739..5aaeb25c9 100644
--- a/plugins/codex-security/tests/test_workbench_db_exports.py
+++ b/plugins/codex-security/tests/test_workbench_db_exports.py
@@ -19,6 +19,7 @@ from workbench_test_support import (
create_saved_git_workspace,
create_saved_workspace,
create_workspace,
+ empty_target_paths,
empty_target_scan,
fail_scan,
get_scan,
@@ -137,9 +138,7 @@ def test_late_parent_draft_is_retained_without_mutating_frozen_stopped_seal(
def test_canceled_scan_does_not_accept_checkpoints_written_after_cancellation(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_scan_command(
state_dir, str(saved["id"]), "--scan-root", str(tmp_path / "scans")
@@ -441,9 +440,7 @@ def test_incomplete_parent_checkpoint_cannot_complete_scan(tmp_path: Path) -> No
def test_model_only_draft_is_available_before_findings_and_survives_stop(
tmp_path: Path, termination: str, projection_failure_at: str
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
scan_id, scan_dir = start_workspace_scan(state_dir, str(saved["id"]), tmp_path / "scans")
update_progress(state_dir, scan_id, "--phase", "threat_model")
@@ -553,9 +550,7 @@ def test_history_does_not_expose_a_model_from_changed_sealed_artifacts(
def test_invalid_model_or_binding_keeps_history_available_for_semantic_repair(
tmp_path: Path, malformation: str
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
scan_id, scan_dir = start_workspace_scan(state_dir, str(saved["id"]), tmp_path / "scans")
write_completed_contract(scan_dir, scan_id, target)
@@ -664,9 +659,7 @@ def test_export_validation_checks_binding_without_writing_or_pinning_legacy_arti
def test_completed_findings_export_inside_scan_directory(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
source = target / "src/extract.py"
source.parent.mkdir()
source.write_text("".join(f"line {line}\n" for line in range(1, 46)))
@@ -740,10 +733,8 @@ def test_deep_csv_export_adds_only_candidate_id_column(
monkeypatch: pytest.MonkeyPatch,
workbench_api,
) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace_id = str(uuid.uuid4())
create_workspace(
state_dir, workspace_id, "--thread-id", "thread-deep-export", "--target-path", str(target)
@@ -1064,9 +1055,7 @@ def test_embedded_and_paged_findings_normalize_scalar_attack_path_assessments(
def test_primary_location_prefers_root_control_in_bounded_and_csv_results(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
for index in range(9):
(target / f"support-{index}.py").write_text("support\n")
(target / "root.py").write_text("vulnerable\n")
diff --git a/plugins/codex-security/tests/test_workbench_deep_scan.py b/plugins/codex-security/tests/test_workbench_deep_scan.py
index 2c94e064e..5afe63aa8 100644
--- a/plugins/codex-security/tests/test_workbench_deep_scan.py
+++ b/plugins/codex-security/tests/test_workbench_deep_scan.py
@@ -21,6 +21,7 @@ from workbench_test_support import (
commit_deep_dedup,
create_saved_workspace,
create_workspace,
+ empty_target_paths,
fail_scan,
finish_deep_scan,
get_deep_scan,
@@ -80,6 +81,19 @@ def begin_target_scan(
)
+def start_empty_target_scan(
+ tmp_path: Path, *, config: str | None = None
+) -> tuple[Path, Path, str, Path]:
+ state_dir = tmp_path / "state"
+ codex_home = tmp_path / "codex-home"
+ if config is not None:
+ write_deep_config(codex_home, config)
+ target = tmp_path / "target"
+ target.mkdir()
+ run = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")["deepScan"]
+ return state_dir, codex_home, str(run["scanId"]), Path(str(run["scanDir"]))
+
+
def restore_v53_schema(state_dir: Path) -> None:
with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
connection.execute("ALTER TABLE scans DROP COLUMN retained_checkpoint_heads_json")
@@ -96,6 +110,7 @@ def claim_deep_scan_coordinator(
thread_id: str = "thread-deep-scan",
handoff_claim_token: str | None = None,
coordinator_generation: int | None = None,
+ check: bool = True,
) -> dict[str, object]:
return scan_command(
state_dir,
@@ -110,6 +125,7 @@ def claim_deep_scan_coordinator(
else ()
),
environment=deep_environment(codex_home),
+ check=check,
)
@@ -321,10 +337,7 @@ def test_existing_generation_safely_claims_and_reclaims_without_schema_migration
def test_legacy_generation_with_active_worker_observes_grace_then_adopts(tmp_path: Path) -> None:
- state_dir, codex_home, target = tmp_path / "state", tmp_path / "codex-home", tmp_path / "target"
- target.mkdir()
- run = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")["deepScan"]
- scan_id, scan_dir = str(run["scanId"]), Path(str(run["scanDir"]))
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)
dispatch_discovery_worker(
state_dir,
codex_home,
@@ -373,6 +386,7 @@ def test_expired_coordinator_preserves_incomplete_setup(tmp_path: Path) -> None:
assert not inventory.exists()
def test_paused_discovery_resumes_after_restart_with_original_handoff_claim(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
@@ -462,14 +476,37 @@ def test_paused_discovery_resumes_after_restart_with_original_handoff_claim(
environment=deep_environment(codex_home),
)
assert resumed["startDisposition"] == "joined"
- adopted = claim_deep_scan_coordinator(
+
+ def claim(token: str | None, *, check: bool = True) -> dict[str, object]:
+ return claim_deep_scan_coordinator(
+ state_dir,
+ codex_home,
+ scan_id,
+ thread_id=thread_id,
+ handoff_claim_token=token,
+ check=check,
+ )
+
+ rejected = claim(None, check=False)
+ assert rejected["returncode"] != 0
+ assert "another continuation" in rejected["stderr"]
+ adopted = claim(handoff_claim_token)
+ assert adopted["coordinatorDisposition"] == "adopted"
+ assert claim(handoff_claim_token)["coordinatorDisposition"] == "observing"
+ stale = update_progress(
state_dir,
- codex_home,
scan_id,
- thread_id=thread_id,
- handoff_claim_token=handoff_claim_token,
+ "--phase",
+ "discovery",
+ "--claim-token",
+ handoff_claim_token,
+ "--coordinator-generation",
+ "1",
+ check=False,
+ environment=deep_environment(codex_home),
)
- assert adopted["coordinatorDisposition"] == "adopted"
+ assert stale["returncode"] != 0
+ assert "newer generation" in stale["stderr"]
recovered = adopted["deepScan"]
assert (recovered["status"], recovered["phase"], recovered["coordinatorGeneration"]) == (
"running",
@@ -541,10 +578,7 @@ def test_paused_discovery_resumes_after_restart_with_original_handoff_claim(
def test_expired_coordinator_recovers_reducer_publication_after_process_death(
tmp_path: Path, crash_point: str, copy_publication: bool
) -> None:
- state_dir, codex_home, target = tmp_path / "state", tmp_path / "codex-home", tmp_path / "target"
- target.mkdir()
- run = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")["deepScan"]
- scan_id, scan_dir = str(run["scanId"]), Path(str(run["scanDir"]))
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)
claim_deep_scan_coordinator(state_dir, codex_home, scan_id)
accepted = [
dispatch_discovery_worker(
@@ -654,17 +688,14 @@ def test_expired_coordinator_recovers_reducer_publication_after_process_death(
("reducer_status", "replace_all_inputs", "should_finish"),
(("running", True, True), ("failed", True, True), ("failed", False, False)),
)
def test_expired_generation_preserves_receipts_and_recovers_abandoned_workers(
tmp_path: Path, reducer_status: str, replace_all_inputs: bool, should_finish: bool
) -> None:
- state_dir, codex_home, target = tmp_path / "state", tmp_path / "codex-home", tmp_path / "target"
- write_deep_config(
- codex_home,
- "[deep_scan]\nworkers = 3\nsubagents = 0\nstop_after_no_new = 1\nmax_discovery_runs = 4\n",
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(
+ tmp_path,
+ config="[deep_scan]\nworkers = 3\nsubagents = 0\nstop_after_no_new = 1\nmax_discovery_runs = 4\n",
)
- target.mkdir()
- run = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")["deepScan"]
- scan_id, scan_dir = str(run["scanId"]), Path(str(run["scanDir"]))
claim_deep_scan_coordinator(state_dir, codex_home, scan_id)
accepted = [
dispatch_discovery_worker(
@@ -761,10 +792,7 @@ def test_expired_generation_preserves_receipts_and_recovers_abandoned_workers(
def test_expired_generation_refunds_shutdown_canceled_discovery(
tmp_path: Path, legacy: bool
) -> None:
- state_dir, codex_home, target = tmp_path / "state", tmp_path / "codex-home", tmp_path / "target"
- target.mkdir()
- run = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")["deepScan"]
- scan_id, scan_dir = str(run["scanId"]), Path(str(run["scanDir"]))
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)
coordinator_generation = None if legacy else 2
if not legacy:
claim_deep_scan_coordinator(state_dir, codex_home, scan_id)
@@ -810,11 +838,9 @@ def test_expired_generation_refunds_shutdown_canceled_discovery(
)
def test_expired_generation_cannot_mutate_recovered_scan(tmp_path: Path) -> None:
- state_dir, codex_home, target = tmp_path / "state", tmp_path / "codex-home", tmp_path / "target"
- target.mkdir()
- run = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")["deepScan"]
- scan_id, scan_dir = str(run["scanId"]), Path(str(run["scanDir"]))
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)
claim_deep_scan_coordinator(state_dir, codex_home, scan_id)
worker, prompt, artifacts, result = dispatch_discovery_worker(
state_dir,
@@ -923,13 +949,7 @@ def test_expired_generation_cannot_mutate_recovered_scan(tmp_path: Path) -> None
def test_reducer_commit_requires_safe_standard_canonical_artifacts(
tmp_path: Path, artifact_name: str, failure_kind: str, expected_error: str | None
) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
- begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
- scan_id = str(begun["deepScan"]["scanId"])
- scan_dir = Path(str(begun["deepScan"]["scanDir"]))
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)
input_worker_ids = [
dispatch_discovery_worker(
state_dir,
@@ -1042,13 +1062,7 @@ def test_reducer_commit_requires_safe_standard_canonical_artifacts(
def test_scan_progress_projects_active_and_completed_independent_reviews(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
- started = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
- scan_id = str(started["deepScan"]["scanId"])
- scan_dir = Path(str(started["deepScan"]["scanDir"]))
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)
def independent_reviews() -> dict[str, int | bool]:
context = get_scan(state_dir, scan_id)
@@ -1126,13 +1140,7 @@ def test_scan_progress_projects_active_and_completed_independent_reviews(
def test_reducer_claim_updates_review_pass_once_and_projects_consolidation(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
- started = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
- scan_id = str(started["deepScan"]["scanId"])
- scan_dir = Path(str(started["deepScan"]["scanDir"]))
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)
first_worker_id, _, _, _ = dispatch_discovery_worker(
state_dir,
codex_home,
@@ -1337,10 +1345,8 @@ def test_target_begin_is_atomic_idempotent_and_snapshots_config(tmp_path: Path)
def test_sdk_scan_begin_claims_its_existing_scan(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "source.py").write_text("print('fixture')\n")
scan_dir = tmp_path / "scan"
scan_dir.mkdir(mode=0o700)
@@ -1403,10 +1409,8 @@ def test_sdk_scan_begin_claims_its_existing_scan(tmp_path: Path) -> None:
def test_sdk_scan_rejects_invalid_handoff_before_claiming_ownership(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
scan_dir = tmp_path / "scan"
scan_dir.mkdir(mode=0o700)
registered = run_workbench(
@@ -1463,10 +1467,8 @@ def test_sdk_scan_rejects_invalid_handoff_before_claiming_ownership(tmp_path: Pa
def test_target_begin_preserves_url_user_context(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "source.py").write_text("print('fixture')\n")
user_context = "Repository: https://github.com/example/security-review"
begun = begin_deep_scan(
@@ -1548,13 +1550,7 @@ def test_deep_scan_snapshots_configured_discovery_time_limit(
def test_replaceable_discovery_failures_count_once_and_reset_on_success(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
- begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
- scan_id = str(begun["deepScan"]["scanId"])
- scan_dir = Path(str(begun["deepScan"]["scanDir"]))
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)
failed_id = str(uuid.uuid4())
prompt, artifact_dir, _ = worker_paths(scan_dir, "refused-discovery")
upsert_worker(
@@ -1600,21 +1596,21 @@ def test_replaceable_discovery_failures_count_once_and_reset_on_success(
assert replacement["status"] == "succeeded"
[email protected]("deadline", [False, True], ids=["dispatch-limit", "deadline"])
def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacement(
tmp_path: Path,
+ deadline: bool,
) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- write_deep_config(
- codex_home,
- "[deep_scan]\nworkers = 3\nsubagents = 0\nstop_after_no_new = 6\nmax_discovery_runs = 6\n",
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(
+ tmp_path,
+ config="[deep_scan]\nworkers = 3\nsubagents = 0\nstop_after_no_new = 6\nmax_discovery_runs = 6\n"
+ + ("max_time_hours = 0.5\n" if deadline else ""),
)
- target = tmp_path / "target"
- target.mkdir()
- begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")["deepScan"]
- scan_id = str(begun["scanId"])
- scan_dir = Path(str(begun["scanDir"]))
- claim_deep_scan_coordinator(state_dir, codex_home, scan_id)
+ initial = claim_deep_scan_coordinator(state_dir, codex_home, scan_id)["deepScan"]
+ assert initial["config"]["maxDiscoveryRuns"] == 6
+ if deadline:
+ assert initial["config"]["maxTimeHours"] == 0.5
previously_merged = [
dispatch_discovery_worker(
@@ -1659,28 +1655,30 @@ def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacement
name="unclaimed-discovery",
coordinator_generation=2,
)[0]
- discovery_failure, discovery_prompt, discovery_dir, _ = dispatch_discovery_worker(
- state_dir,
- codex_home,
- scan_id=scan_id,
- scan_dir=scan_dir,
- name="failed-discovery",
- succeed=False,
- coordinator_generation=2,
- )
- counter_before_failure = upsert_worker(
- state_dir,
- codex_home,
- scan_id=scan_id,
- worker_id=discovery_failure,
- status="canceled",
- prompt_path=discovery_prompt,
- artifact_dir=discovery_dir,
- error="transient_error: fixture discovery failure",
- replaceable_failure_kind="transient_error",
- coordinator_generation=2,
- )["deepScan"]["consecutiveErrors"]
- assert counter_before_failure == 1
+ expected_errors = 0 if deadline else 1
+ if not deadline:
+ discovery_failure, discovery_prompt, discovery_dir, _ = dispatch_discovery_worker(
+ state_dir,
+ codex_home,
+ scan_id=scan_id,
+ scan_dir=scan_dir,
+ name="failed-discovery",
+ succeed=False,
+ coordinator_generation=2,
+ )
+ failure = upsert_worker(
+ state_dir,
+ codex_home,
+ scan_id=scan_id,
+ worker_id=discovery_failure,
+ status="canceled",
+ prompt_path=discovery_prompt,
+ artifact_dir=discovery_dir,
+ error="transient_error: fixture discovery failure",
+ replaceable_failure_kind="transient_error",
+ coordinator_generation=2,
+ )["deepScan"]
+ assert failure["consecutiveErrors"] == 1
failed_reducer = str(uuid.uuid4())
failed_prompt, failed_dir, _ = worker_paths(scan_dir, "failed-reducer")
@@ -1719,8 +1717,14 @@ def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacement
coordinator_generation=2,
)["deepScan"]
failed_workers = {worker["id"]: worker for worker in failed["workers"]}
+ assert failed["status"] == "running"
assert failed["phase"] == "discovery"
- assert failed["consecutiveErrors"] == counter_before_failure
+ assert failed["dispatchedCount"] == (5 if deadline else 6)
+ assert all(
+ failed_workers[worker]["status"] == "succeeded"
+ for worker in [*previously_merged, *claimed_inputs, unclaimed_input]
+ )
+ assert failed["consecutiveErrors"] == expected_errors
assert all(failed_workers[worker]["mergeState"] == "merged" for worker in previously_merged)
assert all(failed_workers[worker]["mergeState"] == "buffered" for worker in claimed_inputs)
assert failed_workers[unclaimed_input]["mergeState"] == "buffered"
@@ -1760,7 +1764,7 @@ def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacement
)["deepScan"]
replayed_workers = {worker["id"]: worker for worker in replayed["workers"]}
assert replayed["phase"] == "reducing"
- assert replayed["consecutiveErrors"] == counter_before_failure
+ assert replayed["consecutiveErrors"] == expected_errors
assert all(replayed_workers[worker]["mergeState"] == "merging" for worker in replacement_inputs)
upsert_worker(
@@ -1785,7 +1789,7 @@ def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacement
environment=deep_environment(codex_home),
)["deepScan"]
committed_workers = {worker["id"]: worker for worker in committed["workers"]}
- assert committed["consecutiveErrors"] == counter_before_failure
+ assert committed["consecutiveErrors"] == expected_errors
assert all(
committed_workers[worker]["mergeState"] == "merged"
for worker in [*previously_merged, *replacement_inputs]
@@ -1803,29 +1807,40 @@ def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacement
manifest = scan_dir / "coordinator-manifest.json"
manifest.write_text("{}\n")
- finished = finish_deep_scan(
- state_dir,
- scan_id,
- "capped",
- str(manifest),
- "--coordinator-generation",
- "2",
- environment=deep_environment(codex_home),
- )["deepScan"]
+
+ def finish(*, check: bool = True) -> dict[str, object]:
+ return finish_deep_scan(
+ state_dir,
+ scan_id,
+ "capped",
+ str(manifest),
+ "--coordinator-generation",
+ "2",
+ environment=deep_environment(codex_home),
+ check=check,
+ )
+
+ if deadline:
+ premature = finish(check=False)
+ assert premature["returncode"] != 0
+ assert "before reaching its configured maximum" in premature["stderr"]
+ assert canonical_ledger.read_text() == '{"candidate":"previously committed"}\n'
+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
+ connection.execute(
+ "UPDATE deep_scan_runs SET created_at = ? WHERE scan_id = ?",
+ ("2000-01-01T00:00:00+00:00", scan_id),
+ )
+ finished = finish()["deepScan"]
+ assert finished["terminalReason"] == "capped"
+ assert canonical_ledger.read_text() == '{"candidate":"previously committed"}\n'
assert finished["status"] == "succeeded"
- assert finished["consecutiveErrors"] == counter_before_failure
+ assert finished["consecutiveErrors"] == expected_errors
def test_ordinary_discovery_cancellation_does_not_increment_failure_streak(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
- begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
- scan_id = str(begun["deepScan"]["scanId"])
- scan_dir = Path(str(begun["deepScan"]["scanDir"]))
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)
worker_id = str(uuid.uuid4())
prompt, artifact_dir, _ = worker_paths(scan_dir, "user-canceled-discovery")
upsert_worker(
@@ -1915,10 +1930,8 @@ def test_target_continuation_reuses_terminal_coordinator_across_threads(
tmp_path: Path,
user_context: str | None,
) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "fixture.py").write_text("print('stable snapshot')\n")
scan_root = tmp_path / "scans"
@@ -1965,10 +1978,8 @@ def test_target_continuation_reuses_terminal_coordinator_across_threads(
def test_target_continuation_does_not_reuse_a_different_snapshot(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
fixture = target / "fixture.py"
fixture.write_text("print('first snapshot')\n")
scan_root = tmp_path / "scans"
@@ -2132,10 +2143,8 @@ def test_target_continuation_does_not_reuse_another_threads_app_scan(
tmp_path: Path,
sealed: bool,
) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "fixture.py").write_text("print('app scan')\n")
scan_root = tmp_path / "scans"
workspace_id = str(uuid.uuid4())
@@ -2203,10 +2212,8 @@ def test_target_continuation_does_not_reuse_another_threads_app_scan(
def test_parent_scan_cannot_complete_before_deep_orchestration_manifest(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
scan_id = str(begun["deepScan"]["scanId"])
@@ -2221,10 +2228,8 @@ def test_parent_scan_cannot_complete_before_deep_orchestration_manifest(
def test_concurrent_target_begin_creates_one_scan(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
scan_root = tmp_path / "scans"
with ThreadPoolExecutor(max_workers=2) as executor:
@@ -2242,10 +2247,8 @@ def test_concurrent_target_begin_creates_one_scan(tmp_path: Path) -> None:
def test_app_workspaces_cannot_start_duplicate_owned_target_scans(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace_ids = [str(uuid.uuid4()), str(uuid.uuid4())]
for workspace_id in workspace_ids:
create_workspace(
@@ -2302,10 +2305,8 @@ def test_app_workspaces_cannot_start_duplicate_owned_target_scans(tmp_path: Path
def test_app_scan_begin_validates_mode_and_owner(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace_id = str(uuid.uuid4())
create_workspace(
state_dir,
@@ -2388,10 +2389,8 @@ def test_app_scan_begin_validates_mode_and_owner(tmp_path: Path) -> None:
def test_stale_deep_continuation_cannot_begin_after_handoff_transfer(
tmp_path: Path, handoff_action: str
) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target, thread_id="workspace-thread", mode="deep")
started = start_scan_command(
state_dir,
@@ -2488,10 +2487,8 @@ def test_stale_deep_continuation_cannot_begin_after_handoff_transfer(
def test_pending_app_workspace_does_not_block_target_bootstrap(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace_id = str(uuid.uuid4())
create_workspace(
state_dir,
@@ -2827,16 +2824,10 @@ def test_zero_discovery_deadline_rejects_nonempty_candidate_ledger(
tmp_path: Path,
candidate_ledger: str,
) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- write_deep_config(
- codex_home, "[deep_scan]\nworkers = 1\nmax_discovery_runs = 3\nmax_time_hours = 0.5\n"
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(
+ tmp_path,
+ config="[deep_scan]\nworkers = 1\nmax_discovery_runs = 3\nmax_time_hours = 0.5\n",
)
- target = tmp_path / "target"
- target.mkdir()
- begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
- scan_id = str(begun["deepScan"]["scanId"])
- scan_dir = Path(str(begun["deepScan"]["scanDir"]))
canonical = write_canonical_artifacts(scan_dir)
canonical["candidateLedgerPath"].write_text(candidate_ledger)
manifest = scan_dir / "coordinator-manifest.json"
@@ -2865,14 +2856,10 @@ def test_zero_discovery_deadline_rejects_nonempty_candidate_ledger(
def test_capped_completion_requires_canonical_artifacts(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- write_deep_config(codex_home, "[deep_scan]\nworkers = 1\nmax_discovery_runs = 1\n")
- target = tmp_path / "target"
- target.mkdir()
- begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
- scan_id = str(begun["deepScan"]["scanId"])
- scan_dir = Path(str(begun["deepScan"]["scanDir"]))
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(
+ tmp_path,
+ config="[deep_scan]\nworkers = 1\nmax_discovery_runs = 1\n",
+ )
worker_id = str(uuid.uuid4())
prompt, artifact_dir, _ = worker_paths(scan_dir, "failed-discovery")
upsert_worker(
@@ -2908,16 +2895,10 @@ def test_capped_completion_requires_canonical_artifacts(tmp_path: Path) -> None:
def test_capped_completion_rejects_buffered_workers_and_omissions(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- write_deep_config(
- codex_home, "[deep_scan]\nworkers = 2\nstop_after_no_new = 10\nmax_discovery_runs = 3\n"
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(
+ tmp_path,
+ config="[deep_scan]\nworkers = 2\nstop_after_no_new = 10\nmax_discovery_runs = 3\n",
)
- target = tmp_path / "target"
- target.mkdir()
- begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
- scan_id = str(begun["deepScan"]["scanId"])
- scan_dir = Path(str(begun["deepScan"]["scanDir"]))
first_id, *_ = dispatch_discovery_worker(
state_dir,
codex_home,
@@ -3168,10 +3149,8 @@ def test_legacy_succeeded_capped_completion_replays_without_rewriting_artifacts(
def test_late_worker_rejection_supersedes_stopped_checkpoint_finding(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
scan_id = str(begun["deepScan"]["scanId"])
scan_dir = Path(str(begun["deepScan"]["scanDir"]))
@@ -3367,16 +3346,10 @@ def test_capped_completion_uses_configured_limits_independently_of_review_notes(
def test_saturated_completion_rejects_merging_workers(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- write_deep_config(
- codex_home, "[deep_scan]\nworkers = 2\nstop_after_no_new = 2\nmax_discovery_runs = 3\n"
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(
+ tmp_path,
+ config="[deep_scan]\nworkers = 2\nstop_after_no_new = 2\nmax_discovery_runs = 3\n",
)
- target = tmp_path / "target"
- target.mkdir()
- begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
- scan_id = str(begun["deepScan"]["scanId"])
- scan_dir = Path(str(begun["deepScan"]["scanDir"]))
first_id, *_ = dispatch_discovery_worker(
state_dir,
codex_home,
@@ -3446,10 +3419,8 @@ def test_saturated_completion_rejects_merging_workers(tmp_path: Path) -> None:
def test_running_worker_updates_preserve_identity_and_dispatch_count(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
scan_id = str(begun["deepScan"]["scanId"])
scan_dir = Path(str(begun["deepScan"]["scanDir"]))
@@ -3483,13 +3454,7 @@ def test_running_worker_updates_preserve_identity_and_dispatch_count(tmp_path: P
def test_terminal_worker_updates_are_exact_idempotent_replays(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
- begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
- scan_id = str(begun["deepScan"]["scanId"])
- scan_dir = Path(str(begun["deepScan"]["scanDir"]))
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)
worker_id = str(uuid.uuid4())
prompt, artifact_dir, result = worker_paths(scan_dir, "terminal-worker")
upsert_worker(
@@ -3565,17 +3530,10 @@ def test_terminal_worker_updates_are_exact_idempotent_replays(tmp_path: Path) ->
def test_discovery_buffer_prefix_dedup_and_saturation_are_transactional(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- write_deep_config(
- codex_home, "[deep_scan]\nworkers = 2\nstop_after_no_new = 3\nmax_discovery_runs = 4\n"
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(
+ tmp_path,
+ config="[deep_scan]\nworkers = 2\nstop_after_no_new = 3\nmax_discovery_runs = 4\n",
)
- target = tmp_path / "target"
- target.mkdir()
- begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
- deep_scan = begun["deepScan"]
- scan_id = str(deep_scan["scanId"])
- scan_dir = Path(str(deep_scan["scanDir"]))
for index in range(3):
worker_id = str(uuid.uuid4())
prompt, artifact_dir, result = worker_paths(scan_dir, f"worker-{index}")
@@ -3765,13 +3723,7 @@ def test_discovery_buffer_prefix_dedup_and_saturation_are_transactional(
def test_get_deep_scan_does_not_invent_compact_artifacts_for_legacy_success(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
- begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
- scan_id = str(begun["deepScan"]["scanId"])
- scan_dir = Path(str(begun["deepScan"]["scanDir"]))
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)
committed = commit_reducer(
state_dir,
codex_home,
@@ -3845,13 +3797,7 @@ def test_get_deep_scan_does_not_invent_compact_artifacts_for_legacy_success(
def test_finish_preserves_legacy_succeeded_without_manifest_compatibility(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
- begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
- scan_id = str(begun["deepScan"]["scanId"])
- scan_dir = Path(str(begun["deepScan"]["scanDir"]))
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)
with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
connection.execute(
"""
@@ -3888,14 +3834,7 @@ def test_finish_preserves_legacy_succeeded_without_manifest_compatibility(
def test_cancel_scan_cancels_coordinator_and_active_workers(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
- begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
- deep_scan = begun["deepScan"]
- scan_id = str(deep_scan["scanId"])
- scan_dir = Path(str(deep_scan["scanDir"]))
+ state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)
worker_id = str(uuid.uuid4())
prompt, artifact_dir, _ = worker_paths(scan_dir, "worker")
upsert_worker(
@@ -3920,10 +3859,8 @@ def test_cancel_scan_cancels_coordinator_and_active_workers(tmp_path: Path) -> N
def test_failure_manifest_is_confined_persisted_and_exactly_replayable(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
scan_id = str(begun["deepScan"]["scanId"])
scan_dir = Path(str(begun["deepScan"]["scanDir"]))
@@ -4032,10 +3969,8 @@ def test_failure_manifest_is_confined_persisted_and_exactly_replayable(
def test_cancel_scan_overrides_succeeded_deep_run_during_parent_tail(
tmp_path: Path, with_manifest: bool
) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
deep_scan = begun["deepScan"]
scan_id = str(deep_scan["scanId"])
@@ -4068,10 +4003,8 @@ def test_cancel_scan_overrides_succeeded_deep_run_during_parent_tail(
def test_saturated_run_without_manifest_can_be_marked_failed_or_interrupted(
tmp_path: Path, deep_status: str
) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
scan_id = str(begun["deepScan"]["scanId"])
database = state_dir / "workbench.sqlite3"
@@ -4105,10 +4038,8 @@ def test_saturated_run_without_manifest_can_be_marked_failed_or_interrupted(
def test_succeeded_run_with_manifest_cannot_be_marked_interrupted(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")
deep_scan = begun["deepScan"]
scan_id = str(deep_scan["scanId"])
diff --git a/plugins/codex-security/tests/test_workbench_diagnostics.py b/plugins/codex-security/tests/test_workbench_diagnostics.py
index 8731c364c..ca5dd3dd0 100644
--- a/plugins/codex-security/tests/test_workbench_diagnostics.py
+++ b/plugins/codex-security/tests/test_workbench_diagnostics.py
@@ -8,6 +8,7 @@ import pytest
from workbench_test_support import (
begin_deep_scan,
create_saved_workspace,
+ empty_target_paths,
fail_scan,
get_deep_scan,
get_scan,
@@ -24,9 +25,7 @@ LEGACY_ERROR = "Saved diagnostic. " * 100 + "\n...[truncated; sha256:" + "0" * 6
@pytest.fixture
def deep_scan(tmp_path: Path):
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
environment = {"CODEX_HOME": str(tmp_path / "codex-home")}
started = begin_deep_scan(
state_dir,
@@ -44,9 +43,7 @@ def deep_scan(tmp_path: Path):
def test_scan_failure_keeps_complete_diagnostic_and_existing_terminal_value(
tmp_path: Path, message: str
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace = create_saved_workspace(state_dir, target)
started = start_delivered_scan(
state_dir,
diff --git a/plugins/codex-security/tests/test_workbench_handoff.py b/plugins/codex-security/tests/test_workbench_handoff.py
index 4d4dca71f..cc45ac7fb 100644
--- a/plugins/codex-security/tests/test_workbench_handoff.py
+++ b/plugins/codex-security/tests/test_workbench_handoff.py
@@ -11,6 +11,7 @@ from workbench_test_support import (
cancel_scan,
create_saved_workspace,
create_workspace,
+ empty_target_paths,
get_scan,
mark_handoff_delivered,
run_workbench,
@@ -24,9 +25,7 @@ from workbench_test_support import (
@pytest.mark.parametrize("mode", ("standard", "deep"))
def test_running_scan_context_is_owned_by_attached_continuation(tmp_path: Path, mode: str) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
original_thread_id = "original-thread"
continuation_thread_id = "continuation-thread"
saved = create_saved_workspace(state_dir, target, thread_id=original_thread_id, mode=mode)
@@ -88,9 +87,7 @@ def test_running_scan_context_is_owned_by_attached_continuation(tmp_path: Path,
def test_workbench_serializes_concurrent_handoff_delivery(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_scan_command(state_dir, str(saved["id"]))
scan_id = str(started["results"]["scanId"])
@@ -122,9 +119,7 @@ def test_workbench_serializes_concurrent_handoff_delivery(tmp_path: Path) -> Non
def test_deep_handoff_leaves_preflight_progress_to_coordinator(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace_id = str(uuid.uuid4())
create_workspace(state_dir, workspace_id, "--target-path", str(target))
save_workspace(state_dir, workspace_id, str(target), ".", "deep")
@@ -182,9 +177,7 @@ def test_workbench_upgrade_keeps_preexisting_delivered_continuation_writable(
def test_workbench_handoff_claim_is_owned_by_one_token(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_scan_command(state_dir, str(saved["id"]))
scan_id = str(started["results"]["scanId"])
@@ -209,9 +202,7 @@ def test_workbench_handoff_claim_is_owned_by_one_token(tmp_path: Path) -> None:
def test_workbench_handoff_claim_only_allows_stale_takeover(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_scan_command(state_dir, str(saved["id"]))
scan_id = str(started["results"]["scanId"])
@@ -254,9 +245,7 @@ def test_workbench_handoff_claim_only_allows_stale_takeover(tmp_path: Path) -> N
def test_workbench_attaches_one_continuation_thread_to_claimed_scan(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_scan_command(state_dir, str(saved["id"]))
scan_id = str(started["results"]["scanId"])
@@ -288,9 +277,7 @@ def test_workbench_attaches_one_continuation_thread_to_claimed_scan(
def test_workbench_allows_attached_continuation_thread_to_cancel_scan(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target, thread_id="workspace-thread")
started = start_scan_command(state_dir, str(saved["id"]))
scan_id = str(started["results"]["scanId"])
@@ -306,9 +293,7 @@ def test_workbench_allows_attached_continuation_thread_to_cancel_scan(
def test_workbench_releases_attached_continuation_for_a_fresh_handoff(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_scan_command(state_dir, str(saved["id"]))
scan_id = str(started["results"]["scanId"])
@@ -338,9 +323,7 @@ def test_workbench_releases_attached_continuation_for_a_fresh_handoff(
def test_released_pending_handoff_rejects_tokenless_mutations(
tmp_path: Path, command: str, arguments: tuple[str, ...]
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_scan_command(state_dir, str(saved["id"]))
scan_id = str(started["results"]["scanId"])
diff --git a/plugins/codex-security/tests/test_workbench_output_permissions.py b/plugins/codex-security/tests/test_workbench_output_permissions.py
index 08368e41b..8613231cc 100644
--- a/plugins/codex-security/tests/test_workbench_output_permissions.py
+++ b/plugins/codex-security/tests/test_workbench_output_permissions.py
@@ -6,7 +6,7 @@ import uuid
from pathlib import Path
import pytest
-from workbench_test_support import create_saved_workspace, run_workbench
+from workbench_test_support import create_saved_workspace, empty_target_paths, run_workbench
pytestmark = pytest.mark.skipif(os.name == "nt", reason="POSIX directory permissions")
@@ -44,9 +44,7 @@ def start_scan_with_permissive_umask(
tmp_path: Path, scan_root: Path | None, entrypoint: tuple[str, str]
) -> Path:
command, result_key = entrypoint
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "fixture.py").write_text("print('fixture')\n")
if command == "start-scan":
workspace = create_saved_workspace(state_dir, target)
diff --git a/plugins/codex-security/tests/test_workbench_progress.py b/plugins/codex-security/tests/test_workbench_progress.py
index 345ceb1be..cd24961ae 100644
--- a/plugins/codex-security/tests/test_workbench_progress.py
+++ b/plugins/codex-security/tests/test_workbench_progress.py
@@ -4,15 +4,14 @@ from pathlib import Path
from workbench_test_support import (
create_saved_workspace,
+ empty_target_paths,
start_delivered_scan,
update_progress,
)
def test_progress_preserves_phase_receipts_until_validation(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))
scan_id = str(started["results"]["scanId"])
@@ -68,9 +67,7 @@ def test_progress_preserves_phase_receipts_until_validation(tmp_path: Path) -> N
def test_historical_preflight_issues_survive_progress_updates(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))
scan_id = str(started["results"]["scanId"])
diff --git a/plugins/codex-security/tests/test_workbench_prompt_only_scan.py b/plugins/codex-security/tests/test_workbench_prompt_only_scan.py
index 3c116be86..31609da90 100644
--- a/plugins/codex-security/tests/test_workbench_prompt_only_scan.py
+++ b/plugins/codex-security/tests/test_workbench_prompt_only_scan.py
@@ -18,7 +18,13 @@ from test_workbench_db import (
initialize_git_repository,
run_workbench,
)
-from workbench_test_support import get_scan, scan_command, start_scan_command, update_progress
+from workbench_test_support import (
+ empty_target_paths,
+ get_scan,
+ scan_command,
+ start_scan_command,
+ update_progress,
+)
def start_prompt_only_scan(
@@ -97,9 +103,7 @@ def prompt_scan_arguments(target: Path, root: Path) -> argparse.Namespace:
def test_headless_standard_scan_starts_without_setup_opt_out(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "fixture.py").write_text("print('fixture')\n")
user_context = (
@@ -148,9 +152,7 @@ def test_headless_standard_scan_joins_only_the_owning_thread(tmp_path: Path) ->
def test_headless_standard_scan_serializes_concurrent_starts(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "fixture.py").write_text("print('fixture')\n")
run_workbench(state_dir, "database-info")
@@ -169,9 +171,7 @@ def test_headless_standard_scan_serializes_concurrent_starts(tmp_path: Path) ->
def test_prompt_only_scan_creates_submitted_delivered_scan(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "fixture.py").write_text("print('fixture')\n")
started = start_prompt_only_scan(state_dir, target, tmp_path / "scans")
assert started["startDisposition"] == "created"
@@ -189,9 +189,7 @@ def test_prompt_only_scan_creates_submitted_delivered_scan(
def test_prompt_only_standard_phase_uses_latest_persisted_scan_context(
tmp_path: Path,
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
started = start_prompt_only_scan(state_dir, target, tmp_path / "scans")
scan_id = str(started["scan"]["scanId"])
@@ -218,9 +216,7 @@ def test_prompt_only_standard_phase_uses_latest_persisted_scan_context(
def test_prompt_scan_revalidates_target_after_concurrent_database_write(
tmp_path: Path, target_changes: bool, existing_scan: bool, headless_standard: bool
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
source = target / "fixture.py"
source.write_text("original\n")
saved = create_saved_workspace(state_dir, target)
@@ -278,9 +274,7 @@ def test_prompt_scan_revalidates_target_after_concurrent_database_write(
def test_prompt_scan_revalidates_target_after_database_lock_wait(
tmp_path: Path, target_changes: bool, writer_arrival: str
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
source = target / "fixture.py"
source.write_text("original\n")
namespace = runpy.run_path(str(SCRIPT), run_name="prompt_scan_lock_test")
@@ -369,9 +363,7 @@ def test_prompt_scan_revalidates_target_after_database_lock_wait(
def test_concurrent_prompt_scan_starts_join_the_winner(
tmp_path: Path, headless_standard: bool
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "fixture.py").write_text("original\n")
create_saved_workspace(state_dir, target)
namespace = runpy.run_path(str(SCRIPT), run_name="prompt_scan_concurrent_test")
@@ -410,9 +402,7 @@ def test_concurrent_prompt_scan_starts_join_the_winner(
def test_setup_scan_reuses_checked_target_metadata(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
namespace = runpy.run_path(str(SCRIPT), run_name="setup_scan_target_identity_test")
start = namespace["start_scan"]
@@ -465,9 +455,7 @@ def test_setup_scan_reuses_checked_target_metadata(tmp_path: Path) -> None:
def test_prompt_only_scan_does_not_join_setup_owned_scans(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
scan_root = tmp_path / "scans"
saved = create_saved_workspace(
state_dir,
@@ -507,9 +495,7 @@ def test_prompt_only_diff_scan_validates_and_persists_canonical_diff_identity(
def test_prompt_registration_keeps_existing_scans_readable(
tmp_path: Path, change_target: bool
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
source = target / "fixture.py"
source.write_text("original\n")
existing = start_prompt_only_scan(state_dir, target, tmp_path / "scans")
diff --git a/plugins/codex-security/tests/test_workbench_remediation_cancellation.py b/plugins/codex-security/tests/test_workbench_remediation_cancellation.py
index 6fccd5a09..178d2583f 100644
--- a/plugins/codex-security/tests/test_workbench_remediation_cancellation.py
+++ b/plugins/codex-security/tests/test_workbench_remediation_cancellation.py
@@ -8,6 +8,7 @@ from pathlib import Path
import pytest
from workbench_test_support import (
cancel_remediation_request,
+ empty_target_paths,
mark_remediation_delivered,
request_remediation,
request_remediation_action,
@@ -23,9 +24,7 @@ from workbench_test_support import (
def test_cancel_finding_remediation_request_restores_previous_state(
tmp_path: Path, verification_started: bool
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
source = target / "source.txt"
source.write_text("vulnerable\n")
scan_id, scan_dir = start_saved_scan(state_dir, target, tmp_path / "scans")
diff --git a/plugins/codex-security/tests/test_workbench_remediation_retry.py b/plugins/codex-security/tests/test_workbench_remediation_retry.py
index 9a37d5916..753a579f5 100644
--- a/plugins/codex-security/tests/test_workbench_remediation_retry.py
+++ b/plugins/codex-security/tests/test_workbench_remediation_retry.py
@@ -10,6 +10,7 @@ from typing import Any
from workbench_test_support import (
cancel_remediation_request,
claim_remediation_resend,
+ empty_target_paths,
mark_remediation_delivered,
request_remediation,
request_remediation_action,
@@ -38,9 +39,7 @@ def update_remediation(
def test_failed_remediation_steps_can_retry_or_regenerate(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
source = target / "source.txt"
source.write_text("vulnerable\n")
scan_id, scan_dir = start_saved_scan(state_dir, target, tmp_path / "scans")
diff --git a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
index 333e50daf..349152791 100644
--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
@@ -24,6 +24,7 @@ from workbench_test_support import (
create_saved_git_workspace,
create_saved_workspace,
create_workspace,
+ empty_target_paths,
empty_target_scan,
get_scan,
initialize_git_repository,
@@ -617,9 +618,7 @@ def test_workbench_does_not_run_fsmonitor_during_scan_start(tmp_path: Path) -> N
def test_workbench_counts_scope_before_taking_sqlite_writer_lock(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "source.py").write_text("print('fixture')\n")
workspace = create_saved_workspace(state_dir, target)
database = state_dir / "workbench.sqlite3"
@@ -1527,9 +1526,7 @@ def test_workbench_repairs_recorded_deep_scan_migration(
drop_deep_scan_tables: bool,
attach_continuation: bool,
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace = create_saved_workspace(state_dir, target, thread_id="thread-deep-scan", mode="deep")
started = start_scan_command(
state_dir, str(workspace["id"]), "--scan-root", str(tmp_path / "scans")
@@ -1779,9 +1776,7 @@ def test_workbench_repairs_pre_release_scan_continuation_migration(tmp_path: Pat
def test_workbench_repairs_recorded_scan_scope_file_count_migration(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace = create_saved_workspace(state_dir, target)
started = start_scan_command(state_dir, str(workspace["id"]))
scan_id = str(started["results"]["scanId"])
@@ -1952,9 +1947,7 @@ def test_workbench_upgrades_stable_scan_target_identity_migration(tmp_path: Path
def test_workbench_applies_shadowed_delivered_claim_cleanup_once(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace = create_saved_workspace(state_dir, target)
started = start_scan_command(state_dir, str(workspace["id"]))
scan_id = str(started["results"]["scanId"])
@@ -2030,9 +2023,7 @@ def test_workbench_repairs_shadowed_setup_preferences_migration(
def test_workbench_repairs_shadowed_phase_progress_migration(
tmp_path: Path, legacy_name: str
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace = create_saved_workspace(state_dir, target)
started = start_scan_command(state_dir, str(workspace["id"]))
scan_id = str(started["results"]["scanId"])
@@ -2079,9 +2070,7 @@ def test_workbench_repairs_shadowed_phase_progress_migration(
def test_workbench_repairs_shadowed_preflight_progress_migration(
tmp_path: Path, legacy_name: str
) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
saved = create_saved_workspace(state_dir, target)
started = start_scan_command(state_dir, str(saved["id"]))
scan_id = str(started["results"]["scanId"])
@@ -2120,9 +2109,7 @@ def test_workbench_repairs_shadowed_preflight_progress_migration(
def test_workbench_repairs_shadowed_scan_recipe_migration(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace = create_saved_workspace(state_dir, target)
started = start_scan_command(state_dir, str(workspace["id"]))
scan_id = str(started["results"]["scanId"])
@@ -2156,9 +2143,7 @@ def test_workbench_repairs_shadowed_scan_recipe_migration(tmp_path: Path) -> Non
def test_workbench_reconciles_released_completion_warning_version(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace = create_saved_workspace(state_dir, target)
started = start_scan_command(state_dir, str(workspace["id"]))
scan_id = str(started["results"]["scanId"])
@@ -2195,9 +2180,7 @@ def test_workbench_reconciles_released_completion_warning_version(tmp_path: Path
def test_workbench_repairs_recorded_completion_warnings_migration(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
workspace = create_saved_workspace(state_dir, target)
started = start_scan_command(state_dir, str(workspace["id"]))
scan_id = str(started["results"]["scanId"])
diff --git a/plugins/codex-security/tests/test_workbench_standard_deep_results.py b/plugins/codex-security/tests/test_workbench_standard_deep_results.py
index 5c592fa91..ece78efbd 100644
--- a/plugins/codex-security/tests/test_workbench_standard_deep_results.py
+++ b/plugins/codex-security/tests/test_workbench_standard_deep_results.py
@@ -17,6 +17,7 @@ from workbench_test_support import (
cancel_scan,
claim_deep_scan_dedup,
commit_deep_dedup,
+ empty_target_paths,
empty_target_scan,
fail_deep_scan,
fail_scan,
@@ -1108,10 +1109,8 @@ def deep_scan_fixture(
include_paths: list[str] | None = None,
run=run_workbench,
) -> tuple[Path, Path, Path, Path, str]:
- state_dir = tmp_path / "state"
codex_home = tmp_path / "codex-home"
- target = tmp_path / "target"
- target.mkdir()
+ state_dir, target = empty_target_paths(tmp_path)
(target / "app.py").write_text("value = request.args['value']\n")
for relative in include_paths or []:
included = target / relative
diff --git a/plugins/codex-security/tests/test_workbench_target.py b/plugins/codex-security/tests/test_workbench_target.py
index d19094448..cd79c1582 100644
--- a/plugins/codex-security/tests/test_workbench_target.py
+++ b/plugins/codex-security/tests/test_workbench_target.py
@@ -864,140 +864,6 @@ def test_stale_git_binding_does_not_spawn(tmp_path: Path, monkeypatch: pytest.Mo
assert result.args[0] == "git"
- ("encoding", "bom"),
- [("utf-8", b""), ("utf-16-le", b"\xff\xfe"), ("utf-16-be", b"\xfe\xff")],
-)
-def test_git_blob_samples_match_full_file_classification_with_bounded_reads(
- tmp_path: Path, monkeypatch: pytest.MonkeyPatch, encoding: str, bom: bytes
-) -> None:
- target = tmp_path / "target"
- initialize_git_repository(target)
- # The UTF-16 surrogate pair crosses the first 64 KiB sample boundary.
- text = bom + ("a" * 32766 + "😀" + "b" * 32768).encode(encoding)
- contents = {
- "text.bin": text + (b"\0" if bom else b""),
- "binary-tail.bin": text + "\0".encode(encoding),
- "binary-head.bin": b"\0" + text * 32,
- "empty.bin": b"",
- "after.txt": b"after\n",
- }
- for name, content in contents.items():
- (target / name).write_bytes(content)
- subprocess.run(["git", "add", "."], cwd=target, check=True)
- subprocess.run(["git", "commit", "-qm", "source samples"], cwd=target, check=True)
-
- git_blob_samples = WORKBENCH_TARGET["git_blob_samples"]
- function_globals = git_blob_samples.__globals__
- read_batch_samples = function_globals["_read_git_batch_samples"]
- reads: list[int] = []
-
- def require_bounded_output(output: Any, names: list[str]):
- assert not output.seekable()
- read = output.read
-
- def bounded_read(size: int = -1) -> bytes:
- assert 0 <= size <= 64 * 1024
- reads.append(size)
- return read(size)
-
- output.read = bounded_read
- return read_batch_samples(output, names)
-
- monkeypatch.setitem(function_globals, "_read_git_batch_samples", require_bounded_output)
- # Both request and response exceed pipe buffers, before the multi-megabyte binary.
- missing = ["HEAD:missing"] * 10_000
- samples = git_blob_samples(target, [*missing, *(f"HEAD:{name}" for name in contents)])
-
- assert samples == [
- *([None] * len(missing)),
- (text[: 64 * 1024], False),
- (b"", True),
- (b"", True),
- (b"", False),
- (b"after\n", False),
- ]
- assert max(reads) == 64 * 1024
-
-
- "output",
- [
- b"missing terminator",
- b"object blob invalid\n",
- b"object blob -1\n",
- b"object blob 4\nabc",
- b"object blob 4\nabcd!",
- b"object blob invalid\n" + b"x" * (4 * 1024 * 1024),
- b"object blob 131072\n" + b"x" * (64 * 1024 + 1),
- ],
- ids=[
- "unterminated-header",
- "invalid-size",
- "negative-size",
- "short-body",
- "invalid-terminator",
- "large-invalid-header",
- "truncated-large-body",
- ],
-)
-def test_git_blob_samples_reject_incomplete_framing(
- tmp_path: Path, monkeypatch: pytest.MonkeyPatch, output: bytes
-) -> None:
- git_blob_samples = WORKBENCH_TARGET["git_blob_samples"]
-
- def git_command(*args: Any, **kwargs: Any):
- remaining = memoryview(output)
- try:
- while remaining:
- written = os.write(kwargs["stdout_file"].fileno(), remaining)
- remaining = remaining[written:]
- except BrokenPipeError:
- return subprocess.CompletedProcess(args, 1)
- return subprocess.CompletedProcess(args, 0)
-
- monkeypatch.setitem(git_blob_samples.__globals__, "git_command", git_command)
-
- assert git_blob_samples(tmp_path, ["HEAD:source"]) == [None]
-
-
[email protected]("output", [b"", b"object blob 4\ntext\n"])
-def test_git_blob_samples_reject_git_failure(
- tmp_path: Path, monkeypatch: pytest.MonkeyPatch, output: bytes
-) -> None:
- git_blob_samples = WORKBENCH_TARGET["git_blob_samples"]
-
- def git_command(*args: Any, **kwargs: Any):
- kwargs["stdout_file"].write(output)
- return subprocess.CompletedProcess(args, 1)
-
- monkeypatch.setitem(git_blob_samples.__globals__, "git_command", git_command)
-
- assert git_blob_samples(tmp_path, ["HEAD:source"]) == [None]
-
-
-def test_git_blob_samples_work_with_git_before_2_38(
- tmp_path: Path, monkeypatch: pytest.MonkeyPatch
-) -> None:
- target = tmp_path / "target"
- initialize_git_repository(target)
- git_blob_samples = WORKBENCH_TARGET["git_blob_samples"]
- git_command = git_blob_samples.__globals__["git_command"]
-
- def git_before_2_38(*args: Any, **kwargs: Any):
- # Git before 2.38 rejects cat-file -z with a usage error.
- if "-z" in args:
- return subprocess.CompletedProcess(args, 129)
- return git_command(*args, **kwargs)
-
- monkeypatch.setitem(git_blob_samples.__globals__, "git_command", git_before_2_38)
-
- assert git_blob_samples(target, ["HEAD:README.md", "HEAD:missing"]) == [
- (b"fixture\n", False),
- None,
- ]
-
-
@pytest.mark.parametrize(
("log_encoding", "subject"),
[
diff --git a/plugins/codex-security/tests/workbench_test_support.py b/plugins/codex-security/tests/workbench_test_support.py
index 8a0e15739..9867eec45 100644
--- a/plugins/codex-security/tests/workbench_test_support.py
+++ b/plugins/codex-security/tests/workbench_test_support.py
@@ -726,10 +726,14 @@ def start_saved_scan(state_dir: Path, target: Path, scan_root: Path) -> tuple[st
return start_workspace_scan(state_dir, str(saved["id"]), scan_root)
-def empty_target_scan(tmp_path: Path) -> tuple[Path, Path, str, Path]:
- state_dir = tmp_path / "state"
+def empty_target_paths(tmp_path: Path) -> tuple[Path, Path]:
target = tmp_path / "target"
target.mkdir()
+ return tmp_path / "state", target
+
+
+def empty_target_scan(tmp_path: Path) -> tuple[Path, Path, str, Path]:
+ state_dir, target = empty_target_paths(tmp_path)
scan_id, scan_dir = start_saved_scan(state_dir, target, tmp_path / "scans")
return state_dir, target, scan_id, scan_dir
diff --git a/sdk/typescript/scripts/test-timings.json b/sdk/typescript/scripts/test-timings.json
index 450a1c0f4..41d9c8ab5 100644
--- a/sdk/typescript/scripts/test-timings.json
+++ b/sdk/typescript/scripts/test-timings.json
@@ -144,11 +144,6 @@
"darwin": 5,
"win32": 3.9
},
- "attack-path-tool-name.test.ts": {
- "linux": 0.2,
- "darwin": 0.2,
- "win32": 0.2
- },
"auth-status-concurrency.test.ts": {
"linux": 1.9,
"darwin": 1.8,
@@ -524,21 +519,11 @@
"darwin": 8.9,
"win32": 11.2
},
- "deep-scan-parent-denials.test.ts": {
- "linux": 0.1,
- "darwin": 0.1,
- "win32": 0.1
- },
"deep-scan-reducer-recovery.test.ts": {
"linux": 0.4,
"darwin": 0.5,
"win32": 0.6
},
- "deep-scan-windows-executable.test.ts": {
- "linux": 0.1,
- "darwin": 0.1,
- "win32": 0.1
- },
"deep-scan-workbench.test.ts": {
"linux": 21,
"darwin": 25.1,
@@ -739,11 +724,6 @@
"darwin": 3.2,
"win32": 8.6
},
- "linear-comment-pagination.test.ts": {
- "linux": 0.1,
- "darwin": 0.1,
- "win32": 0.1
- },
"linear-pagination.test.ts": {
"linux": 0.1,
"darwin": 0.1,
@@ -849,11 +829,6 @@
"darwin": 0.1,
"win32": 0.2
},
- "prompt-only-start-timeout.test.ts": {
- "linux": 0.1,
- "darwin": 0.1,
- "win32": 0.1
- },
"provider-profile.test.ts": {
"linux": 1.8,
"darwin": 3.3,
@@ -899,11 +874,6 @@
"darwin": 11.7,
"win32": 24.6
},
- "release-cut-workflow.test.ts": {
- "linux": 0.1,
- "darwin": 0.1,
- "win32": 0.1
- },
"release-pr.test.ts": {
"linux": 6.1,
"darwin": 50.6,
diff --git a/sdk/typescript/tests-ts/deep-scan-parent-denials.test.ts b/sdk/typescript/tests-ts/deep-scan-parent-denials.test.ts
deleted file mode 100644
index 65987eb77..000000000
--- a/sdk/typescript/tests-ts/deep-scan-parent-denials.test.ts
+++ /dev/null
@@ -1,154 +0,0 @@
-import * as path from "node:path";
-import * as url from "node:url";
-import * as util from "node:util";
-import { expect, test } from "bun:test";
-import { parse } from "smol-toml";
-import { loadBundledRuntime } from "./plugin-root.js";
-import { profileConfigOverrides } from "../../../plugins/codex-security/scripts/codex_profile.mjs";
-
-type Sandbox = {
- filesystemDenies: string[];
- literalFilesystemDenies?: string[];
- globScanMaxDepth?: number;
-};
-
-async function bundledPolicy() {
- const runtime = await loadBundledRuntime();
- const start = runtime.indexOf("var CODEX_SANDBOX_STATE_META_CAPABILITY =");
- const end = runtime.indexOf("\n// ", start);
- expect(start).toBeGreaterThan(0);
- expect(end).toBeGreaterThan(start);
- const source = runtime.slice(start, end);
- const recordSource = runtime.match(
- /\/\/ src\/record\.ts\n[\s\S]*?(?=\n\/\/ )/u,
- )?.[0];
- expect(recordSource).toBeDefined();
- const imports = [
- ...new Set(source.match(/import_node_(?:path|url|util)\d*/gu)),
- ];
- const resolve = new Function(
- ...imports,
- "DeepScanNonRetryableError",
- `${recordSource}\n${source}\nreturn resolveDeepWorkerParentSandbox;`,
- )(
- ...imports.map((name) =>
- name.startsWith("import_node_path")
- ? path
- : name.startsWith("import_node_url")
- ? url
- : util,
- ),
- Error,
- ) as (metadata: unknown) => Sandbox;
- const profileSource =
- /function workerPermissionProfile\([^\n]*\) \{[\s\S]*?\n\}/u.exec(
- runtime,
- )?.[0];
- expect(profileSource).toBeDefined();
- const profile = new Function(
- `${profileSource}\nreturn workerPermissionProfile;`,
- ) as () => (sandbox: Sandbox) => Record<string, unknown>;
- const workerProfile = profile();
- const overrides = (sandbox: Sandbox) =>
- profileConfigOverrides({
- default_permissions: "codex_security_deep_scan_worker",
- "permissions.codex_security_deep_scan_worker": workerProfile(sandbox),
- });
- return { resolve, overrides };
-}
-
-function metadata(entries: unknown[]) {
- return {
- _meta: {
- "codex/sandbox-state-meta": {
- permissionProfile: {
- type: "managed",
- network: "enabled",
- file_system: {
- type: "restricted",
- glob_scan_max_depth: 8,
- entries: [
- {
- access: "read",
- path: { type: "special", value: { kind: "root" } },
- },
- ...entries,
- ],
- },
- },
- },
- },
- };
-}
-
-test("preserves literal parent deny paths and globs without parent write grants", async () => {
- const policy = await bundledPolicy();
- const denied = path.resolve("synthetic", "secret.with.dots");
- const glob = path.resolve("synthetic", "**", "*.secret");
- const sandbox = policy.resolve(
- metadata([
- {
- access: "write",
- path: { type: "path", path: path.resolve("synthetic") },
- },
- { access: "deny", path: { type: "path", path: denied } },
- { access: "none", path: { type: "glob_pattern", pattern: glob } },
- ]),
- );
- expect(sandbox).toEqual({
- filesystemDenies: [denied, glob],
- globScanMaxDepth: 8,
- });
- expect(parse(policy.overrides(sandbox).join("\n"))).toEqual({
- default_permissions: "codex_security_deep_scan_worker",
- permissions: {
- codex_security_deep_scan_worker: {
- extends: ":read-only",
- filesystem: {
- ":root": "read",
- [denied]: "deny",
- [glob]: "deny",
- glob_scan_max_depth: 8,
- },
- network: { enabled: false },
- },
- },
- });
-});
-
-test("rejects parent denials that cannot be preserved", async () => {
- const policy = await bundledPolicy();
- for (const entry of [
- { access: "deny", path: { type: "path", path: "relative/secret" } },
- { access: "deny", path: { type: "special", value: { kind: "tmpdir" } } },
- {
- access: "write",
- path: { type: "glob_pattern", pattern: path.resolve("synthetic", "*") },
- },
- ]) {
- expect(() => policy.resolve(metadata([entry]))).toThrow(
- "cannot be preserved",
- );
- }
- expect(() => policy.resolve({})).toThrow("trusted parent sandbox metadata");
-});
-
-test("preserves bracket paths as literal denials at the bundled worker boundary", async () => {
- const policy = await bundledPolicy();
- const denied = path.resolve("synthetic", "temp[1]", "credential-home");
- const sandbox = policy.resolve(
- metadata([{ access: "deny", path: { type: "path", path: denied } }]),
- );
- const config = parse(policy.overrides(sandbox).join("\n"));
- expect(config["permissions"]).toEqual({
- codex_security_deep_scan_worker: {
- extends: ":read-only",
- filesystem: {
- ":root": "read",
- [denied]: { ".": "deny" },
- glob_scan_max_depth: 8,
- },
- network: { enabled: false },
- },
- });
-});
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 177847085..9fafd7db2 100644
--- a/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts
+++ b/sdk/typescript/tests-ts/deep-scan-reducer-recovery.test.ts
@@ -1,219 +1,100 @@
-import { readSubprocess } from "./support/shell.js";
-import { parseJsonLines } from "./support/json.js";
-import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
+import { mkdir, readFile } from "node:fs/promises";
import { tmpdir } from "node:os";
-import { join } from "node:path";
-import { expect, test } from "bun:test";
-import { loadBundledRuntime, PLUGIN_ROOT } from "./plugin-root.js";
+import { dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
+import { afterEach, expect, test } from "bun:test";
+import { importModule } from "../../../plugins/codex-security/mcp-app/tests/import-module.ts";
+import { startRpc } from "../scripts/fixtures/package-rpc.mjs";
+import { createTemporaryDirectories } from "./support/temporary-directories.js";
+import { PLUGIN_ROOT } from "./plugin-root.js";
-function bundledFunction(runtime: string, name: string): string {
- const source = new RegExp(
- `function ${name}\\([^\\n]*\\) \\{[\\s\\S]*?\\n\\}`,
- "u",
- ).exec(runtime)?.[0];
- if (!source) throw new Error(`Missing bundled runtime function: ${name}.`);
- return source;
-}
+const directories = createTemporaryDirectories(true);
+afterEach(directories.cleanup);
-test("advertises distinct Standard worker and Deep reducer contracts", async () => {
- const runtime = await loadBundledRuntime();
- const method =
- / compactArtifactServer\(request, includePathsFile\) \{[\s\S]*?\n \}/u.exec(
- runtime,
- )?.[0];
- expect(method).toBeDefined();
- const pathImport = /\(0, (import_node_path\d+)\.join\)/u.exec(method!)?.[1];
- expect(pathImport).toBeDefined();
- const compactArtifactServer = new Function(
- pathImport!,
- `return ({${method}}).compactArtifactServer;`,
- )({ join }) as (
- this: { modelSettings: { artifactContext: Record<string, string> } },
- request: Record<string, unknown>,
- ) => Record<string, { args: string[]; env: NodeJS.ProcessEnv }>;
-
- const node = Bun.which("node");
- expect(node).not.toBeNull();
- const root = mkdtempSync(join(tmpdir(), "codex-security-deep-tools-"));
- const repoRoot = join(root, "repository");
- const scanRoot = join(root, "scan");
- mkdirSync(repoRoot);
- mkdirSync(scanRoot);
-
- try {
- for (const layout of ["worker", "reducer"] as const) {
- const artifactRoot = join(scanRoot, layout);
- mkdirSync(artifactRoot);
- const servers = compactArtifactServer.call(
- {
- modelSettings: {
- artifactContext: {
- pluginRoot: PLUGIN_ROOT,
- repoRoot,
- scanId: "test-scan",
- },
- },
- },
- {
- kind: layout === "reducer" ? "dedup" : "discovery",
- artifactContext: {
- root: artifactRoot,
- layout,
- ...(layout === "reducer"
- ? { deepReducer: { scanRoot, claimedWorkers: [] } }
- : {}),
- },
+test.each(["worker", "reducer"])(
+ "advertises the packaged %s tool contract",
+ async (layout) => {
+ const root = await directories.create("codex-security-deep-tools-");
+ const artifacts = join(root, layout);
+ await mkdir(artifacts);
+ const client = await startRpc(
+ Bun.which("node")!,
+ [join(PLUGIN_ROOT, "mcp", "server.mjs"), "--artifact-writer", "--stdio"],
+ {
+ env: {
+ ...process.env,
+ CODEX_SECURITY_PLUGIN_ROOT: PLUGIN_ROOT,
+ CODEX_SECURITY_SCAN_ID: "test-scan",
+ CODEX_SECURITY_REPO_ROOT: root,
+ CODEX_SECURITY_ARTIFACT_ROOT: artifacts,
+ CODEX_SECURITY_ARTIFACT_LAYOUT: layout,
+ ...(layout === "reducer"
+ ? {
+ CODEX_SECURITY_REDUCER_CONTEXT_JSON: JSON.stringify({
+ scanRoot: root,
+ claimedWorkers: [],
+ }),
+ }
+ : {}),
},
+ },
+ );
+ try {
+ const { tools } = (await client.request("tools/list")) as {
+ tools: Array<{
+ name: string;
+ inputSchema: { properties: Record<string, unknown> };
+ }>;
+ };
+ expect(tools.map((tool) => tool.name).sort()).toEqual(
+ layout === "worker"
+ ? ["record_codex_security_scan_draft"]
+ : [
+ "get_codex_security_deep_reducer_inputs",
+ "record_codex_security_deep_reduction",
+ ],
);
- expect(Object.keys(servers)).toEqual(["cs_artifacts"]);
- const server = servers["cs_artifacts"]!;
- const child = Bun.spawn({
- cmd: [node!, ...server.args],
- env: { ...process.env, ...server.env },
- stdin: Buffer.from(
- [
- '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"codex-security-test","version":"1.0.0"}}}',
- '{"jsonrpc":"2.0","method":"notifications/initialized","params":{}}',
- '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}',
- "",
- ].join("\n"),
- ),
- stdout: "pipe",
- stderr: "pipe",
- timeout: 30_000,
- });
- const { status, stdout, stderr } = await readSubprocess(child);
- expect(status, stderr).toBe(0);
- const response = parseJsonLines<{ id?: number; result?: unknown }>(
- stdout,
- ).find((message) => message.id === 2)?.result as
- | {
- tools: Array<{
- name: string;
- inputSchema: { properties: Record<string, unknown> };
- }>;
- }
- | undefined;
- expect(response).toBeDefined();
- expect(response!.tools.length).toBeGreaterThan(0);
- for (const tool of response!.tools) {
+ for (const tool of tools)
expect(`mcp__cs_artifacts__${tool.name}`.length).toBeLessThanOrEqual(
64,
);
- }
- const recordTool = response!.tools.find(
+ const record = tools.find(
(tool) =>
tool.name ===
(layout === "reducer"
? "record_codex_security_deep_reduction"
: "record_codex_security_scan_draft"),
);
- expect(recordTool).toBeDefined();
- expect(recordTool!.inputSchema.properties).toHaveProperty("findings");
- expect(recordTool!.inputSchema.properties).toHaveProperty("scope");
- if (layout === "reducer")
- expect(recordTool!.inputSchema.properties).not.toHaveProperty(
- "reviewState",
- );
- else
- expect(recordTool!.inputSchema.properties).toHaveProperty(
- "reviewState",
- );
+ expect(record?.inputSchema.properties).toHaveProperty("findings");
+ expect(record?.inputSchema.properties).toHaveProperty("scope");
+ expect(Object.hasOwn(record!.inputSchema.properties, "reviewState")).toBe(
+ layout === "worker",
+ );
+ } finally {
+ await client.close();
}
- } finally {
- rmSync(root, { recursive: true, force: true });
- }
-});
-
-test("preserves owned worker tool failure diagnostics", async () => {
- const runtime = await loadBundledRuntime();
- const diagnosticSource = bundledFunction(runtime, "appendItemDiagnostic");
- const recordHelper = /\b(isRecord\d*)\(item\)/u.exec(diagnosticSource)?.[1];
- expect(recordHelper).toBeDefined();
- const appendDiagnostic = new Function(
- [
- bundledFunction(runtime, recordHelper!),
- bundledFunction(runtime, "isSandboxNamespaceExhaustion"),
- bundledFunction(runtime, "isCodeModeFrameError"),
- bundledFunction(runtime, "appendStreamDiagnostic"),
- bundledFunction(runtime, "appendUniqueDiagnostic"),
- diagnosticSource,
- "return appendItemDiagnostic;",
- ].join("\n"),
- )() as (
- diagnostics: Array<{ code: string; message: string }>,
- event: Record<string, unknown>,
- ) => void;
+ expect(client.child.exitCode).toBe(0);
+ },
+);
- const detail = "synthetic-provider-detail";
- for (const fixture of [
- {
- server: "cs_artifacts",
- tool: "record_codex_security_deep_reduction",
- result: { isError: true, content: [{ type: "text", text: detail }] },
- error: null,
- message: detail,
- },
- {
- server: "cs_artifacts",
- tool: "additional_codex_security_worker_tool",
- result: null,
- error: { message: detail },
- message: detail,
- },
- {
- server: "codex_security_artifacts",
- tool: "record_codex_security_discovery_candidates",
- result: null,
- error: null,
- message:
- "Codex worker artifact tool record_codex_security_discovery_candidates failed.",
+test("resumes only when the exact Standard worker or reducer result is missing", async () => {
+ const runner = new URL(
+ "../../../plugins/codex-security/mcp-app/src/deep-scan/worker-runner.ts",
+ import.meta.url,
+ );
+ const {
+ isMissingWorkerResult,
+ standardScanCompletionContinuation,
+ reducerCompletionContinuation,
+ } = await importModule({
+ stdin: {
+ contents: `${await readFile(runner, "utf8")}
+export { isMissingWorkerResult, standardScanCompletionContinuation, reducerCompletionContinuation };`,
+ resolveDir: dirname(fileURLToPath(runner)),
+ loader: "ts",
},
- ]) {
- const diagnostics: Array<{ code: string; message: string }> = [];
- appendDiagnostic(diagnostics, {
- type: "mcp_tool_call",
- status: "failed",
- arguments: { token: "synthetic-input-only" },
- ...fixture,
- });
- expect(diagnostics).toEqual([
- {
- code: "artifact_tool_failed",
- message: fixture.message,
- },
- ]);
- expect(JSON.stringify(diagnostics)).not.toContain("synthetic-input-only");
- }
-
- const unrelatedDiagnostics: Array<{ code: string; message: string }> = [];
- appendDiagnostic(unrelatedDiagnostics, {
- type: "mcp_tool_call",
- status: "failed",
- server: "unrelated_server",
- tool: "record_codex_security_deep_reduction",
- result: { isError: true },
- error: { message: "Synthetic unrelated transport failure" },
+ loader: { ".md": "text" },
});
- expect(unrelatedDiagnostics).toEqual([]);
- appendDiagnostic(unrelatedDiagnostics, {
- type: "file_change",
- status: "failed",
- });
- expect(unrelatedDiagnostics).toEqual([
- { code: "file_change_failed", message: "Codex worker file change failed." },
- ]);
-});
-
-test("resumes only when the exact Standard worker or reducer result is missing", async () => {
- const runtime = await loadBundledRuntime();
- const source = bundledFunction(runtime, "isMissingWorkerResult");
- const pathImport = /\(0, (import_node_path\d+)\.join\)/u.exec(source)?.[1];
- expect(pathImport).toBeDefined();
- const isMissingWorkerResult = new Function(
- pathImport!,
- `${source}\nreturn isMissingWorkerResult;`,
- )({ join }) as (error: Error, artifactDirectory: string) => boolean;
const artifactDirectory = join(tmpdir(), "codex-security-reducer-artifacts");
const missingResult = Object.assign(new Error("result missing"), {
code: "ENOENT",
@@ -247,19 +128,16 @@ test("resumes only when the exact Standard worker or reducer result is missing",
),
).toBe(false);
- const standardContinuation = new Function(
- `${bundledFunction(runtime, "standardScanCompletionContinuation")}\nreturn standardScanCompletionContinuation;`,
- )() as (attempt: number) => string;
- expect(standardContinuation(1)).toContain("record_codex_security_scan_draft");
- expect(standardContinuation(1)).toMatch(/retry.*until it succeeds/iu);
-
- const continuation = new Function(
- [
- bundledFunction(runtime, "reducerInputRecoveryInstructions"),
- bundledFunction(runtime, "reducerCompletionContinuation"),
- "return reducerCompletionContinuation;",
- ].join("\n"),
- )() as (attempt: number) => string;
- expect(continuation(1)).toContain("record_codex_security_deep_reduction");
- expect(continuation(1)).toMatch(/retry.*until it succeeds/iu);
+ expect(standardScanCompletionContinuation(1)).toContain(
+ "record_codex_security_scan_draft",
+ );
+ expect(standardScanCompletionContinuation(1)).toMatch(
+ /retry.*until it succeeds/iu,
+ );
+ expect(reducerCompletionContinuation(1)).toContain(
+ "record_codex_security_deep_reduction",
+ );
+ expect(reducerCompletionContinuation(1)).toMatch(
+ /retry.*until it succeeds/iu,
+ );
});
diff --git a/sdk/typescript/tests-ts/deep-scan-windows-executable.test.ts b/sdk/typescript/tests-ts/deep-scan-windows-executable.test.ts
deleted file mode 100644
index f576f887d..000000000
--- a/sdk/typescript/tests-ts/deep-scan-windows-executable.test.ts
+++ /dev/null
@@ -1,167 +0,0 @@
-import * as fs from "node:fs";
-import {
- mkdir,
- mkdtemp,
- readFile,
- realpath,
- rm,
- utimes,
- writeFile,
-} from "node:fs/promises";
-import * as module from "node:module";
-import { tmpdir } from "node:os";
-import * as path from "node:path";
-import { expect, test } from "bun:test";
-import { loadBundledRuntime, PLUGIN_ROOT } from "./plugin-root.js";
-
-async function bundledResolver() {
- const runtime = await loadBundledRuntime();
- const start = runtime.indexOf("function resolveCodexPath(");
- const end = runtime.indexOf("\n// ", start);
- expect(start).toBeGreaterThan(0);
- expect(end).toBeGreaterThan(start);
- const source = runtime.slice(start, end);
- const imports = [
- ...new Set(source.match(/import_node_(?:fs|path|module)\d*/gu)),
- ];
- return new Function(...imports, `${source}\nreturn resolveCodexPath;`)(
- ...imports.map((name) =>
- name.startsWith("import_node_fs")
- ? fs
- : name.startsWith("import_node_path")
- ? path
- : module,
- ),
- ) as (
- env: NodeJS.ProcessEnv,
- platform: NodeJS.Platform,
- architecture: NodeJS.Architecture,
- ) => string;
-}
-
-test("uses the newest relocated Windows executable when WindowsApps is inaccessible", async () => {
- const root = await realpath(
- await mkdtemp(path.join(tmpdir(), "codex-security-executable-")),
- );
- try {
- const older = path.join(
- root,
- "OpenAI",
- "Codex",
- "bin",
- "11111111",
- "codex.exe",
- );
- const newer = path.join(
- root,
- "OpenAI",
- "Codex",
- "bin",
- "22222222",
- "codex.exe",
- );
- const empty = path.join(
- root,
- "OpenAI",
- "Codex",
- "bin",
- "33333333",
- "codex.exe",
- );
- for (const executable of [older, newer, empty]) {
- await mkdir(path.dirname(executable), { recursive: true });
- await writeFile(
- executable,
- executable === empty ? "" : "synthetic executable",
- );
- }
- await utimes(older, 1, 1);
- await utimes(newer, 2, 2);
- const resolve = await bundledResolver();
- const environment = {
- PATH: "",
- LOCALAPPDATA: root,
- CODEX_CLI_PATH: "C:\\Program Files\\WindowsApps\\Codex\\codex.exe",
- };
- expect(resolve(environment, "win32", "x64")).toBe(newer);
- expect(
- resolve(
- { ...environment, CODEX_CLI_PATH: "C:\\Tools\\codex.exe" },
- "win32",
- "x64",
- ),
- ).toBe("C:\\Tools\\codex.exe");
- expect(resolve({ PATH: "" }, "win32", "x64")).toBe(
- path.resolve("codex.exe"),
- );
- expect(resolve({}, "linux", "x64")).toBe(path.resolve("codex"));
- } finally {
- await rm(root, { recursive: true, force: true });
- }
-});
-
-test.each([
- ["x64", "x86_64-pc-windows-msvc"],
- ["arm64", "aarch64-pc-windows-msvc"],
-] as const)(
- "resolves a managed Windows %s worker through the packaged MCP environment",
- async (architecture, targetTriple) => {
- const root = await realpath(
- await mkdtemp(path.join(tmpdir(), "codex-security-managed-cli-")),
- );
- try {
- const packages = path.join(
- root,
- "managed CLI",
- "node_modules",
- "@openai",
- );
- const packageRoot = path.join(packages, "codex");
- const platformPackage = path.join(
- packages,
- `codex-win32-${architecture}`,
- );
- const executable = path.join(
- platformPackage,
- "vendor",
- targetTriple,
- "bin",
- "codex.exe",
- );
- await mkdir(packageRoot, { recursive: true });
- await mkdir(path.dirname(executable), { recursive: true });
- await writeFile(path.join(packageRoot, "package.json"), "{}\n");
- await writeFile(path.join(platformPackage, "package.json"), "{}\n");
- await writeFile(executable, "synthetic executable\n");
-
- const configuration = JSON.parse(
- await readFile(path.join(PLUGIN_ROOT, ".mcp.json"), "utf8"),
- ) as { mcpServers: Record<string, { env_vars: string[] }> };
- const allowed = new Set(
- configuration.mcpServers["codex-security"]!.env_vars,
- );
- const environment = Object.fromEntries(
- Object.entries({
- PATH: "",
- CODEX_MANAGED_PACKAGE_ROOT: ` ${packageRoot} `,
- }).filter(([name]) => name === "PATH" || allowed.has(name)),
- );
- const resolve = await bundledResolver();
-
- expect(resolve(environment, "win32", architecture)).toBe(executable);
- const configured = path.join(root, "custom CLI", "codex.exe");
- expect(
- resolve(
- { ...environment, CODEX_CLI_PATH: configured },
- "win32",
- architecture,
- ),
- ).toBe(configured);
- expect(resolve(environment, "linux", architecture)).toBe(
- path.resolve("codex"),
- );
- } finally {
- await rm(root, { recursive: true, force: true });
- }
- },
-);
diff --git a/sdk/typescript/tests-ts/deep-scan-workbench.test.ts b/sdk/typescript/tests-ts/deep-scan-workbench.test.ts
index be7650da5..0d55af686 100644
--- a/sdk/typescript/tests-ts/deep-scan-workbench.test.ts
+++ b/sdk/typescript/tests-ts/deep-scan-workbench.test.ts
@@ -5,7 +5,9 @@ import { loadContract } from "../src/contract.js";
import { readReviewState } from "./support/review-state.js";
import { afterEach, describe, expect, test } from "bun:test";
import { workbenchCommand as workbench } from "./support/workbench-command.js";
-import { loadBundledRuntime, PLUGIN_ROOT } from "./plugin-root.js";
+import { PLUGIN_ROOT } from "./plugin-root.js";
+import { fileURLToPath } from "node:url";
+import { importSource } from "../../../plugins/codex-security/mcp-app/tests/import-module.ts";
import { runPython } from "./support/python-probe.js";
import { createTemporaryDirectories } from "./support/temporary-directories.js";
@@ -222,14 +224,15 @@ test.each([
},
});
- const templates =
- /\/\/ templates\/deep-scan\/discovery\.md\n([\s\S]*?)\/\/ src\/deep-scan\/worker-runner\.ts/u.exec(
- await loadBundledRuntime(),
- )?.[1];
- expect(templates).toBeDefined();
- const renderDiscoveryPrompt = new Function(
- `${templates}\nreturn renderDiscoveryPrompt;`,
- )() as (input: Record<string, unknown>) => string;
+ const { renderDiscoveryPrompt } = await importSource(
+ fileURLToPath(
+ new URL(
+ "../../../plugins/codex-security/mcp-app/src/deep-scan/templates.ts",
+ import.meta.url,
+ ),
+ ),
+ { loader: { ".md": "text" } },
+ );
const prompt = renderDiscoveryPrompt({
...deepScan,
pluginRoot: PLUGIN_ROOT,
@@ -732,188 +735,6 @@ describe("deep scan workbench ownership", () => {
});
});
- test("adopts an expired coordinator without repeating completed discovery", async () => {
- const root = await temporaryDirectories.create(
- "codex-security-deep-resume-",
- );
- const repository = join(root, "repository");
- const stateDir = join(root, "state");
- const codexHome = join(root, "codex-home");
- await mkdir(repository);
- await writeFile(join(repository, "source.py"), "# source fixture\n");
-
- const python = Bun.which("python3") ?? Bun.which("python");
- expect(python).not.toBeNull();
- const command = (args: string[], allowFailure = false) => {
- const result = runPython(
- python!,
- [join(PLUGIN_ROOT, "scripts", "workbench_db.py"), ...args],
- {
- env: {
- ...process.env,
- CODEX_SECURITY_STATE_DIR: stateDir,
- CODEX_HOME: codexHome,
- },
- },
- );
- const stdout = new TextDecoder().decode(result.stdout);
- const stderr = new TextDecoder().decode(result.stderr);
- if (allowFailure) return { status: result.exitCode, stderr };
- expect(result.exitCode, stderr).toBe(0);
- return JSON.parse(stdout) as Record<string, unknown>;
- };
-
- const started = command([
- "begin-deep-scan",
- "--thread-id",
- "thread-deep-scan",
- "--target-path",
- repository,
- "--scope",
- ".",
- "--scan-root",
- join(root, "scans"),
- "--available-parallelism",
- "4",
- ]);
- const initial = started["deepScan"] as Record<string, unknown>;
- const scanId = initial["scanId"] as string;
- const scanDir = initial["scanDir"] as string;
- expect(initial["coordinatorGeneration"]).toBe(1);
-
- const updateDatabase = (statement: string, ...values: string[]) => {
- const result = runPython(python!, [
- "-c",
- "import sqlite3,sys; connection=sqlite3.connect(sys.argv[1]); connection.execute(sys.argv[2],sys.argv[3:]); connection.commit()",
- join(stateDir, "workbench.sqlite3"),
- statement,
- ...values,
- ]);
- expect(result.exitCode, new TextDecoder().decode(result.stderr)).toBe(0);
- };
- updateDatabase(
- "UPDATE scans SET handoff_claim_token = ? WHERE id = ?",
- originalClaimToken,
- scanId,
- );
- command([
- "update-progress",
- "--scan-id",
- scanId,
- "--phase",
- "discovery",
- "--claim-token",
- originalClaimToken,
- ]);
-
- const completedWorkerId = "44444444-4444-4444-8444-444444444444";
- const interruptedWorkerId = "55555555-5555-4555-8555-555555555555";
- for (const workerId of [completedWorkerId, interruptedWorkerId]) {
- const artifactDir = join(
- scanDir,
- "artifacts",
- "deep_discovery",
- workerId,
- );
- const promptPath = join(artifactDir, "prompt.md");
- await mkdir(artifactDir, { recursive: true });
- await writeFile(promptPath, "Review the source.\n");
- const workerArgs = [
- "upsert-deep-scan-worker",
- "--scan-id",
- scanId,
- "--worker-id",
- workerId,
- "--kind",
- "discovery",
- "--prompt-path",
- promptPath,
- "--artifact-dir",
- artifactDir,
- "--attempt",
- "1",
- ];
- command([...workerArgs, "--status", "running"]);
- if (workerId === completedWorkerId) {
- const resultPath = join(artifactDir, "result.json");
- await writeFile(resultPath, "{}\n");
- command([
- ...workerArgs,
- "--status",
- "succeeded",
- "--result-manifest-path",
- resultPath,
- ]);
- }
- }
-
- updateDatabase(
- "UPDATE deep_scan_runs SET updated_at = ? WHERE scan_id = ?",
- "2000-01-01T00:00:00+00:00",
- scanId,
- );
- const claimArgs = [
- "claim-deep-scan-coordinator",
- "--scan-id",
- scanId,
- "--thread-id",
- "thread-deep-scan",
- ];
- const missingClaim = command(claimArgs, true);
- expect(missingClaim["status"]).not.toBe(0);
- expect(missingClaim["stderr"]).toContain("another continuation");
-
- const resumed = command([
- ...claimArgs,
- "--claim-token",
- originalClaimToken,
- ]);
- const recovered = resumed["deepScan"] as Record<string, unknown>;
- expect(resumed["coordinatorDisposition"]).toBe("adopted");
- expect(recovered).toMatchObject({
- status: "running",
- phase: "discovery",
- coordinatorGeneration: 2,
- dispatchedCount: 1,
- });
- expect(recovered["workers"]).toEqual(
- expect.arrayContaining([
- expect.objectContaining({
- id: completedWorkerId,
- status: "succeeded",
- }),
- expect.objectContaining({
- id: interruptedWorkerId,
- status: "canceled",
- }),
- ]),
- );
-
- const observing = command([
- ...claimArgs,
- "--claim-token",
- originalClaimToken,
- ]);
- expect(observing["coordinatorDisposition"]).toBe("observing");
-
- const staleProgress = command(
- [
- "update-progress",
- "--scan-id",
- scanId,
- "--phase",
- "discovery",
- "--claim-token",
- originalClaimToken,
- "--coordinator-generation",
- "1",
- ],
- true,
- );
- expect(staleProgress["status"]).not.toBe(0);
- expect(staleProgress["stderr"]).toContain("newer generation");
- });
-
test("completes an expired discovery deadline with no workers with retained limitations", async () => {
const root = await temporaryDirectories.create(
"codex-security-deep-empty-deadline-",
@@ -938,8 +759,12 @@ describe("deep scan workbench ownership", () => {
const python = Bun.which("python3") ?? Bun.which("python");
expect(python).not.toBeNull();
- const command = workbenchCommand(python, stateDir, codexHome);
- const registration = command([
+ const command = workbench(python!, () => ({
+ ...process.env,
+ CODEX_SECURITY_STATE_DIR: stateDir,
+ CODEX_HOME: codexHome,
+ }));
+ const registration = await command([
"register-cli-scan",
"--repository",
repository,
@@ -955,19 +780,21 @@ describe("deep scan workbench ownership", () => {
]);
const scanId = registration["scanId"] as string;
const targetId = registration["targetId"] as string;
- const begun = command([
- "begin-deep-scan",
- "--scan-id",
- scanId,
- "--thread-id",
- "empty-deadline-thread",
- "--scan-root",
- join(root, "scans"),
- "--available-parallelism",
- "4",
- "--workflow-version",
- "deep-scan-mcp/v1",
- ])["deepScan"] as Record<string, unknown>;
+ const begun = (
+ await command([
+ "begin-deep-scan",
+ "--scan-id",
+ scanId,
+ "--thread-id",
+ "empty-deadline-thread",
+ "--scan-root",
+ join(root, "scans"),
+ "--available-parallelism",
+ "4",
+ "--workflow-version",
+ "deep-scan-mcp/v1",
+ ])
+ )["deepScan"] as Record<string, unknown>;
expect(begun).toMatchObject({
status: "running",
completionSequence: 0,
@@ -998,15 +825,17 @@ describe("deep scan workbench ownership", () => {
]);
expect(expired.exitCode, new TextDecoder().decode(expired.stderr)).toBe(0);
- const capped = command([
- "finish-deep-scan",
- "--scan-id",
- scanId,
- "--terminal-reason",
- "capped",
- "--manifest-path",
- manifestPath,
- ])["deepScan"] as Record<string, unknown>;
+ const capped = (
+ await command([
+ "finish-deep-scan",
+ "--scan-id",
+ scanId,
+ "--terminal-reason",
+ "capped",
+ "--manifest-path",
+ manifestPath,
+ ])
+ )["deepScan"] as Record<string, unknown>;
expect(capped).toMatchObject({
status: "succeeded",
terminalReason: "capped",
@@ -1024,7 +853,7 @@ describe("deep scan workbench ownership", () => {
await writeBoundedScanArtifacts(scanDir, targetId, "repository", [
{ id: "source-review", reason },
]);
- const completed = command(["complete-scan", "--scan-id", scanId])[
+ const completed = (await command(["complete-scan", "--scan-id", scanId]))[
"scan"
] as {
progress: { status: string };
@@ -1043,294 +872,6 @@ describe("deep scan workbench ownership", () => {
expect(report).toContain("No findings were reported.");
expect(report).toContain(reason);
});
-
- test("requeues a failed reducer's inputs and preserves findings at the discovery deadline", async () => {
- const root = await temporaryDirectories.create(
- "codex-security-deep-reducer-recovery-",
- );
- const repository = join(root, "repository");
- const stateDir = join(root, "state");
- const codexHome = join(root, "codex-home");
- const configDir = join(codexHome, "codex-security");
- await mkdir(repository);
- await mkdir(configDir, { recursive: true });
- await writeFile(join(repository, "source.py"), "# source fixture\n");
- await writeFile(
- join(configDir, "config.toml"),
- "[deep_scan]\nworkers = 3\nmax_discovery_runs = 6\nmax_time_hours = 0.5\n",
- );
-
- const python = Bun.which("python3") ?? Bun.which("python");
- expect(python).not.toBeNull();
- const command = workbenchCommand(python, stateDir, codexHome);
- const state = (result: Record<string, unknown>) =>
- result["deepScan"] as Record<string, unknown>;
- const initial = state(
- command([
- "begin-deep-scan",
- "--thread-id",
- "thread-deep-scan",
- "--target-path",
- repository,
- "--scope",
- ".",
- "--scan-root",
- join(root, "scans"),
- "--available-parallelism",
- "4",
- ]),
- );
- const scanId = initial["scanId"] as string;
- const scanDir = initial["scanDir"] as string;
- expect(initial["config"]).toMatchObject({
- maxDiscoveryRuns: 6,
- maxTimeHours: 0.5,
- });
- const discoveryDir = join(scanDir, "artifacts", "02_discovery");
- const ledgerPath = join(discoveryDir, "candidate_ledger.jsonl");
- const existingFinding = '{"candidate":"previously committed"}\n';
- await mkdir(discoveryDir, { recursive: true });
- await writeFile(join(discoveryDir, "in_scope_files.txt"), "source.py\n");
- await writeFile(ledgerPath, existingFinding);
-
- const workerPaths = async (workerId: string) => {
- const artifactDir = join(
- scanDir,
- "artifacts",
- "deep_discovery",
- workerId,
- );
- const promptPath = join(artifactDir, "prompt.md");
- const resultPath = join(artifactDir, "result.json");
- await mkdir(artifactDir, { recursive: true });
- await writeFile(promptPath, "Review the source.\n");
- return { artifactDir, promptPath, resultPath };
- };
- const discoveryIds = [
- "10000000-0000-4000-8000-000000000001",
- "10000000-0000-4000-8000-000000000002",
- "10000000-0000-4000-8000-000000000003",
- "10000000-0000-4000-8000-000000000004",
- "10000000-0000-4000-8000-000000000005",
- ];
- for (const workerId of discoveryIds) {
- const paths = await workerPaths(workerId);
- const args = [
- "upsert-deep-scan-worker",
- "--scan-id",
- scanId,
- "--worker-id",
- workerId,
- "--kind",
- "discovery",
- "--prompt-path",
- paths.promptPath,
- "--artifact-dir",
- paths.artifactDir,
- "--attempt",
- "1",
- ];
- command([...args, "--status", "running"]);
- await writeFile(paths.resultPath, "{}\n");
- command([
- ...args,
- "--status",
- "succeeded",
- "--result-manifest-path",
- paths.resultPath,
- ]);
- }
-
- const startReducer = async (workerId: string, inputIds: string[]) => {
- const paths = await workerPaths(workerId);
- command([
- "claim-deep-scan-dedup",
- "--scan-id",
- scanId,
- "--worker-id",
- workerId,
- "--prompt-path",
- paths.promptPath,
- "--artifact-dir",
- paths.artifactDir,
- ...inputIds.flatMap((inputId) => ["--input-worker-id", inputId]),
- ]);
- const args = [
- "upsert-deep-scan-worker",
- "--scan-id",
- scanId,
- "--worker-id",
- workerId,
- "--kind",
- "dedup",
- "--prompt-path",
- paths.promptPath,
- "--artifact-dir",
- paths.artifactDir,
- "--attempt",
- "1",
- ];
- command([...args, "--status", "running"]);
- return { args, ...paths };
- };
- const commitReducer = async (
- workerId: string,
- resultPath: string,
- newFindingsCount: number,
- ) => {
- await writeFile(resultPath, "{}\n");
- return state(
- command([
- "commit-deep-scan-dedup",
- "--scan-id",
- scanId,
- "--worker-id",
- workerId,
- "--result-manifest-path",
- resultPath,
- "--new-findings-count",
- String(newFindingsCount),
- ]),
- );
- };
- const previousReducerId = "20000000-0000-4000-8000-000000000001";
- const previous = await startReducer(
- previousReducerId,
- discoveryIds.slice(0, 2),
- );
- await commitReducer(previousReducerId, previous.resultPath, 1);
-
- const failedReducerId = "20000000-0000-4000-8000-000000000002";
- const claimedInputs = discoveryIds.slice(2, 4);
- const failedReducer = await startReducer(failedReducerId, claimedInputs);
- const failureArgs = [
- ...failedReducer.args,
- "--status",
- "failed",
- "--error-message",
- "reducer exhausted its attempts",
- ];
- const failed = state(command(failureArgs));
- const workersById = (result: Record<string, unknown>) =>
- new Map(
- (result["workers"] as Record<string, unknown>[]).map((worker) => [
- worker["id"] as string,
- worker,
- ]),
- );
- const failedWorkers = workersById(failed);
- expect(failed).toMatchObject({
- status: "running",
- phase: "discovery",
- dispatchedCount: 5,
- consecutiveErrors: 0,
- });
- for (const workerId of discoveryIds.slice(0, 2)) {
- expect(failedWorkers.get(workerId)).toMatchObject({
- status: "succeeded",
- mergeState: "merged",
- });
- }
- for (const workerId of discoveryIds.slice(2)) {
- expect(failedWorkers.get(workerId)).toMatchObject({
- status: "succeeded",
- mergeState: "buffered",
- });
- }
- expect(failedWorkers.get(failedReducerId)).toMatchObject({
- status: "failed",
- error: "reducer exhausted its attempts",
- });
- expect(await readFile(ledgerPath, "utf8")).toBe(existingFinding);
-
- const replacementReducerId = "20000000-0000-4000-8000-000000000003";
- const replacementInputs = discoveryIds.slice(2);
- const replacement = await startReducer(
- replacementReducerId,
- replacementInputs,
- );
- const replayed = state(command(failureArgs));
- expect(replayed["phase"]).toBe("reducing");
- for (const workerId of replacementInputs) {
- expect(workersById(replayed).get(workerId)?.["mergeState"]).toBe(
- "merging",
- );
- }
-
- const committed = await commitReducer(
- replacementReducerId,
- replacement.resultPath,
- 0,
- );
- for (const workerId of discoveryIds) {
- expect(workersById(committed).get(workerId)?.["mergeState"]).toBe(
- "merged",
- );
- }
- expect(workersById(committed).get(failedReducerId)?.["status"]).toBe(
- "failed",
- );
- expect(await readFile(ledgerPath, "utf8")).toBe(existingFinding);
-
- const manifestPath = join(scanDir, "coordinator-manifest.json");
- await writeFile(manifestPath, "{}\n");
- const premature = runPython(
- python!,
- [
- join(PLUGIN_ROOT, "scripts", "workbench_db.py"),
- "finish-deep-scan",
- "--scan-id",
- scanId,
- "--terminal-reason",
- "capped",
- "--manifest-path",
- manifestPath,
- ],
- {
- env: {
- ...process.env,
- CODEX_SECURITY_STATE_DIR: stateDir,
- CODEX_HOME: codexHome,
- },
- },
- );
- expect(premature.exitCode).not.toBe(0);
- expect(new TextDecoder().decode(premature.stderr)).toContain(
- "before reaching its configured maximum",
- );
- expect(await readFile(ledgerPath, "utf8")).toBe(existingFinding);
-
- const expire = runPython(python!, [
- "-c",
- [
- "import sqlite3, sys",
- "connection = sqlite3.connect(sys.argv[1])",
- "connection.execute('UPDATE deep_scan_runs SET created_at = ? WHERE scan_id = ?', ('2000-01-01T00:00:00+00:00', sys.argv[2]))",
- "connection.commit()",
- ].join("\n"),
- join(stateDir, "workbench.sqlite3"),
- scanId,
- ]);
- expect(expire.exitCode, new TextDecoder().decode(expire.stderr)).toBe(0);
-
- const completed = state(
- command([
- "finish-deep-scan",
- "--scan-id",
- scanId,
- "--terminal-reason",
- "capped",
- "--manifest-path",
- manifestPath,
- ]),
- );
- expect(completed).toMatchObject({
- status: "succeeded",
- terminalReason: "capped",
- consecutiveErrors: 0,
- });
- expect(await readFile(ledgerPath, "utf8")).toBe(existingFinding);
- });
});
function writeBoundedScanArtifacts(
@@ -1365,28 +906,3 @@ function writeBoundedScanArtifacts(
),
]);
}
-
-function workbenchCommand(
- python: string | null,
- stateDir: string,
- codexHome: string,
-) {
- return (args: string[]): Record<string, unknown> => {
- const result = runPython(
- python!,
- [join(PLUGIN_ROOT, "scripts", "workbench_db.py"), ...args],
- {
- env: {
- ...process.env,
- CODEX_SECURITY_STATE_DIR: stateDir,
- CODEX_HOME: codexHome,
- },
- },
- );
- expect(result.exitCode, new TextDecoder().decode(result.stderr)).toBe(0);
- return JSON.parse(new TextDecoder().decode(result.stdout)) as Record<
- string,
- unknown
- >;
- };
-}
diff --git a/sdk/typescript/tests-ts/deep-scan-worker-shutdown.test.ts b/sdk/typescript/tests-ts/deep-scan-worker-shutdown.test.ts
index 296936b3f..4983f9479 100644
--- a/sdk/typescript/tests-ts/deep-scan-worker-shutdown.test.ts
+++ b/sdk/typescript/tests-ts/deep-scan-worker-shutdown.test.ts
@@ -45,3 +45,22 @@ for (const boundary of [
}
}, 120_000);
}
+
+it("preserves worker error classification, parent sandbox denials and workbench subprocess deadlines", async () => {
+ await promisify(execFile)("node", [
+ "--experimental-strip-types",
+ "--test",
+ ...[
+ "test_deep_scan_errors.ts",
+ "test_deep_scan_parent_sandbox.ts",
+ "test_workbench_timeouts.ts",
+ ].map((file) =>
+ fileURLToPath(
+ new URL(
+ `../../../plugins/codex-security/mcp-app/tests/${file}`,
+ import.meta.url,
+ ),
+ ),
+ ),
+ ]);
+}, 30_000);
diff --git a/sdk/typescript/tests-ts/finding-preview.test.ts b/sdk/typescript/tests-ts/finding-preview.test.ts
index 5a4af3a37..fc8b187b6 100644
--- a/sdk/typescript/tests-ts/finding-preview.test.ts
+++ b/sdk/typescript/tests-ts/finding-preview.test.ts
@@ -3,7 +3,7 @@ import { fileURLToPath } from "node:url";
import { describe, expect, test } from "bun:test";
import { PLUGIN_ROOT } from "./plugin-root.js";
-function sourceScopeProbe(scenario: string): Record<string, unknown> {
+function checkSourceScope(scenario: string, expected: unknown = null): void {
const python = Bun.which("python3") ?? Bun.which("python") ?? Bun.which("py");
expect(python).not.toBeNull();
const result = Bun.spawnSync(
@@ -18,15 +18,12 @@ function sourceScopeProbe(scenario: string): Record<string, unknown> {
{ stdout: "pipe", stderr: "pipe" },
);
expect(result.exitCode, new TextDecoder().decode(result.stderr)).toBe(0);
- return JSON.parse(new TextDecoder().decode(result.stdout)) as Record<
- string,
- unknown
- >;
+ expect(JSON.parse(new TextDecoder().decode(result.stdout))).toEqual(expected);
}
describe("bundled finding previews", () => {
test("reads only the immutable source objects selected for the scan", () => {
- expect(sourceScopeProbe("boundaries")).toEqual({
+ checkSourceScope("boundaries", {
selected: "1 public source",
outside: null,
additional: "1 selected file",
@@ -56,7 +53,7 @@ describe("bundled finding previews", () => {
}, 30_000);
test("keeps captured filesystem aliases usable after their source paths disappear", () => {
- expect(sourceScopeProbe("aliases")).toEqual({
+ checkSourceScope("aliases", {
case: expect.any(Boolean),
unicode: expect.any(Boolean),
nonAscii: expect.any(Boolean),
@@ -65,35 +62,23 @@ describe("bundled finding previews", () => {
}, 30_000);
test("requires distinct filesystem witnesses for colliding descendants", () => {
- expect(sourceScopeProbe("descendant_aliases")).toEqual({
- fileAndDirectoryCollisionsChecked: true,
- missingWitnessesOmitted: true,
- });
+ checkSourceScope("descendant_aliases");
}, 30_000);
test("keeps saved immutable objects readable after replacement refs change", () => {
- expect(sourceScopeProbe("replacements")).toEqual({
- savedObjectsUnchanged: true,
- newReplacementViewOmitted: true,
- });
+ checkSourceScope("replacements");
}, 30_000);
test("does not run working-tree filters when registering a committed-ref scan", () => {
- expect(sourceScopeProbe("replacement_filters")).toEqual({
- workingTreeFilterNotRun: true,
- registrationRecipeUnchanged: true,
- });
+ checkSourceScope("replacement_filters");
}, 30_000);
test("omits source authority when replacement refs changed the scanned tree", () => {
- expect(sourceScopeProbe("replacement_snapshot")).toEqual({
- mismatchedCaptureOmitted: true,
- ambiguousLegacyViewOmitted: true,
- });
+ checkSourceScope("replacement_snapshot");
}, 30_000);
test("omits stale committed excerpts for working-tree diff scans", () => {
- expect(sourceScopeProbe("working_tree_excerpt")).toEqual({
+ checkSourceScope("working_tree_excerpt", {
authorityOmitted: true,
currentExcerptOmitted: true,
legacyExcerptOmitted: true,
@@ -102,37 +87,23 @@ describe("bundled finding previews", () => {
}, 30_000);
test("indexes Git names once for large explicit scope lists", () => {
- expect(sourceScopeProbe("indexed_scopes")).toEqual({
- selected: 256,
- linearNormalization: true,
- });
+ checkSourceScope("indexed_scopes", 256);
}, 30_000);
test("selects source excerpts from the displayed finding locations", () => {
- expect(sourceScopeProbe("display_locations")).toEqual({
- displayed: 8,
- excerptUsesDisplayedLocation: true,
- });
+ checkSourceScope("display_locations", 8);
}, 30_000);
test("does not grant source scope through a selected directory link", () => {
- expect(sourceScopeProbe("selected_redirects")).toEqual({
- selectedLinkOmitted: true,
- linkedAncestorOmitted: true,
- registrationRecipeUnchanged: true,
- directSelectionPreserved: true,
- });
+ checkSourceScope("selected_redirects");
}, 30_000);
test("rejects unsafe finding locations before invoking Git", () => {
- expect(sourceScopeProbe("unsafe_locations")).toEqual({
- unsafePathsRejectedBeforeGit: true,
- invalidLocationRejectedBeforeGit: true,
- });
+ checkSourceScope("unsafe_locations");
}, 30_000);
test("does not treat links or reparse points as filesystem alias evidence", () => {
- expect(sourceScopeProbe("alias_evidence")).toEqual({
+ checkSourceScope("alias_evidence", {
ordinary: true,
hardlink: false,
symlink: false,
@@ -141,31 +112,19 @@ describe("bundled finding previews", () => {
}, 30_000);
test("keeps subdirectory and linked-worktree targets bound to their selected tree", () => {
- expect(sourceScopeProbe("worktrees")).toEqual({
- subdirectoryBound: true,
- linkedWorktreeBound: true,
- });
+ checkSourceScope("worktrees");
}, 30_000);
test.each(["workspace", "prompt", "headless", "deep", "cli"])(
"records source authority through the %s scan-start path",
(writer) => {
- expect(sourceScopeProbe(`writer_${writer}`)).toEqual({
- writer,
- sourceAuthorityRecorded: true,
- launchRecipeUnchanged: true,
- legacyExactScopesPreserved: true,
- });
+ checkSourceScope(`writer_${writer}`, writer);
},
30_000,
);
test("preserves legacy scans and separately owned migration history", () => {
- expect(sourceScopeProbe("migration")).toEqual({
- legacyAuthorityUnset: true,
- otherMigrationsPreserved: true,
- conflictRejected: true,
- });
+ checkSourceScope("migration");
}, 30_000);
});
diff --git a/sdk/typescript/tests-ts/fixtures/source_scopes.py b/sdk/typescript/tests-ts/fixtures/source_scopes.py
index d941f3bc5..9a99f9b85 100644
--- a/sdk/typescript/tests-ts/fixtures/source_scopes.py
+++ b/sdk/typescript/tests-ts/fixtures/source_scopes.py
@@ -141,7 +141,7 @@ def directory_link(target: Path, link: Path) -> None:
link.symlink_to(target, target_is_directory=True)
-def replacements(repository: Path) -> dict:
+def replacements(repository: Path) -> None:
write(repository, "selected.py", "selected source\n")
write(repository, "private.py", "private source\n")
write(repository, "selected/public.py", "selected directory\n")
@@ -167,10 +167,9 @@ def replacements(repository: Path) -> dict:
scopes.tree_entries.cache_clear()
captured = scan(repository, revision, ["selected.py", "selected"])
assert json.loads(captured["source_scopes_json"])["scopes"] == []
- return {"savedObjectsUnchanged": True, "newReplacementViewOmitted": True}
-def replacement_filters(repository: Path) -> dict:
+def replacement_filters(repository: Path) -> None:
write(repository, "tracked.py", "synthetic source\n")
write(repository, ".gitattributes", "tracked.py filter=scope_probe\n")
revision = commit(repository)
@@ -215,10 +214,9 @@ def replacement_filters(repository: Path) -> dict:
assert not marker.exists(), "source capture executed a working-tree filter"
assert json.loads(record["source_scopes_json"])["scopes"] == []
assert json.loads(record["recipe_json"]) == recipe
- return {"workingTreeFilterNotRun": True, "registrationRecipeUnchanged": True}
-def replacement_snapshot(repository: Path) -> dict:
+def replacement_snapshot(repository: Path) -> None:
from workbench_scan_start import scan_target_identity
write(repository, "src/public.py", "historical source\n")
@@ -258,10 +256,9 @@ def replacement_snapshot(repository: Path) -> dict:
== []
)
assert excerpt(legacy, repository, "src/removed.py", ["."]) is None
- return {"mismatchedCaptureOmitted": True, "ambiguousLegacyViewOmitted": True}
-def indexed_scopes(repository: Path) -> dict:
+def indexed_scopes(repository: Path) -> int:
selected = [f"source-{index}.py" for index in range(256)]
for name in selected:
write(repository, name, "synthetic source\n")
@@ -279,10 +276,10 @@ def indexed_scopes(repository: Path) -> dict:
record = scan(repository, revision, selected)
assert len(json.loads(record["source_scopes_json"])["scopes"]) == len(selected)
assert calls <= 3 * len(selected), calls
- return {"selected": len(selected), "linearNormalization": True}
+ return len(selected)
-def display_locations(repository: Path) -> dict:
+def display_locations(repository: Path) -> int:
import workbench_db as db
from filesystem_identity import serialize_filesystem_identity
@@ -340,10 +337,10 @@ def display_locations(repository: Path) -> dict:
assert [item["path"] for item in result["locations"]] == selected[:8]
assert result["sourceExcerpt"] == "1 source 0"
connection.close()
- return {"displayed": 8, "excerptUsesDisplayedLocation": True}
+ return len(result["locations"])
-def selected_redirects(repository: Path) -> dict:
+def selected_redirects(repository: Path) -> None:
write(repository, ".gitignore", "/selected/link\n")
write(repository, "selected/public.py", "selected source\n")
write(repository, "private/secret.py", "private source\n")
@@ -374,12 +371,6 @@ def selected_redirects(repository: Path) -> dict:
excerpt(direct, repository, "private/secret.py", ["private"])
== "1 private source"
)
- return {
- "selectedLinkOmitted": True,
- "linkedAncestorOmitted": True,
- "registrationRecipeUnchanged": True,
- "directSelectionPreserved": True,
- }
def working_tree_excerpt(repository: Path) -> dict:
@@ -423,7 +414,7 @@ def working_tree_excerpt(repository: Path) -> dict:
}
-def unsafe_locations(repository: Path) -> dict:
+def unsafe_locations(repository: Path) -> None:
write(repository, "source.py", "selected source\n")
record = scan(repository, commit(repository), ["."])
outside = repository.parent / "outside"
@@ -448,10 +439,6 @@ def unsafe_locations(repository: Path) -> dict:
)
is None
)
- return {
- "unsafePathsRejectedBeforeGit": True,
- "invalidLocationRejectedBeforeGit": True,
- }
def boundaries(repository: Path) -> dict:
@@ -683,7 +670,7 @@ def aliases(repository: Path) -> dict:
return result
-def descendant_aliases(repository: Path) -> dict:
+def descendant_aliases(repository: Path) -> None:
pairs = [("foo.py", "FOO.py"), ("nested/public.py", "NESTED/public.py")]
for lower, _ in pairs:
write(repository, f"selected/{lower}", "lower source\n")
@@ -718,7 +705,6 @@ def descendant_aliases(repository: Path) -> dict:
for lower, upper in pairs:
for path in (lower, upper):
assert excerpt(record, repository, f"selected/{path}", ["selected"]) is None
- return {"fileAndDirectoryCollisionsChecked": True, "missingWitnessesOmitted": True}
def alias_evidence(_: Path) -> dict:
@@ -750,7 +736,7 @@ def alias_evidence(_: Path) -> dict:
return result
-def worktrees(repository: Path) -> dict:
+def worktrees(repository: Path) -> None:
write(repository, "nested/src/public.py", "nested target source\n")
write(repository, "private.py", "outside target source\n")
revision = commit(repository)
@@ -773,10 +759,9 @@ def worktrees(repository: Path) -> dict:
== "1 nested target source"
)
assert excerpt(linked_record, linked_target, "private.py", ["."]) is None
- return {"subdirectoryBound": True, "linkedWorktreeBound": True}
-def writer(repository: Path, kind: str) -> dict:
+def writer(repository: Path, kind: str) -> str:
write(repository, "src/public.py", "public source\n")
write(repository, "selected.py", "selected file\n")
revision = commit(repository)
@@ -913,15 +898,10 @@ def writer(repository: Path, kind: str) -> dict:
else:
raise AssertionError("Original commit was not pruned")
assert excerpt(row, repository, "selected.py", ["."]) == "1 selected file"
- return {
- "writer": kind,
- "sourceAuthorityRecorded": True,
- "launchRecipeUnchanged": True,
- "legacyExactScopesPreserved": True,
- }
+ return kind
-def migration(_: Path) -> dict:
+def migration(_: Path) -> None:
from workbench_schema import MIGRATIONS, apply_migrations
timestamp = "2026-08-01T00:00:00Z"
@@ -1003,11 +983,6 @@ def migration(_: Path) -> dict:
)
apply(MIGRATIONS)
connection.close()
- return {
- "legacyAuthorityUnset": True,
- "otherMigrationsPreserved": True,
- "conflictRejected": True,
- }
with tempfile.TemporaryDirectory(prefix="codex-security-source-scopes-") as temporary:
diff --git a/sdk/typescript/tests-ts/plugin-root.ts b/sdk/typescript/tests-ts/plugin-root.ts
index d2f9c8892..562e79600 100644
--- a/sdk/typescript/tests-ts/plugin-root.ts
+++ b/sdk/typescript/tests-ts/plugin-root.ts
@@ -1,15 +1,7 @@
import { existsSync } from "node:fs";
-import {
- chmod,
- cp,
- mkdir,
- readdir,
- readFile,
- writeFile,
-} from "node:fs/promises";
+import { chmod, cp, mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
-import { brotliDecompressSync } from "node:zlib";
const bundledPlugin = new URL("../_bundled_plugin/", import.meta.url);
const hasMonorepoSdk = existsSync(
@@ -46,23 +38,6 @@ export const INTEGRATION_TARGET = hasMonorepoSdk
? "project/codex-security-sdk/src"
: "sdk/typescript/src";
-let bundledRuntime: Promise<string> | undefined;
-
-export function loadBundledRuntime(): Promise<string> {
- return (bundledRuntime ??= readdir(new URL("mcp/", bundledPlugin))
- .then((names) =>
- Promise.all(
- names
- .filter((name) => name.startsWith("server.mjs.br.part-"))
- .sort()
- .map((name) => readFile(new URL(`mcp/${name}`, bundledPlugin))),
- ),
- )
- .then((parts) =>
- brotliDecompressSync(Buffer.concat(parts)).toString("utf8"),
- ));
-}
-
export const copyCompletedScanFixture = (destination: string) =>
cp(new URL("examples/legacy-completed-scan/", bundledPlugin), destination, {
recursive: true,
diff --git a/sdk/typescript/tests-ts/prompt-only-start-timeout.test.ts b/sdk/typescript/tests-ts/prompt-only-start-timeout.test.ts
deleted file mode 100644
index 9f0d60788..000000000
--- a/sdk/typescript/tests-ts/prompt-only-start-timeout.test.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-import { expect, test } from "bun:test";
-import { PassThrough } from "node:stream";
-import { loadBundledRuntime, PLUGIN_ROOT } from "./plugin-root.js";
-
-test("gives prompt-only scan startup the five-minute scan timeout", async () => {
- const runtime = await loadBundledRuntime();
- const source =
- /async function executeWorkbench\([^\n]*\) \{[\s\S]*?\n\}/u.exec(
- runtime,
- )?.[0];
- expect(source).toBeDefined();
- const execFileHelper = /\b(execFileAsync\d*)\(/u.exec(source ?? "")?.[1];
- expect(execFileHelper).toBeDefined();
-
- const executeWorkbench = new Function(
- execFileHelper!,
- "workbenchScriptPath",
- "WORKBENCH_PYTHON",
- "PLUGIN_ROOT",
- "isRecord",
- `${source}\nreturn executeWorkbench;`,
- )(
- (_command: string, _args: string[], options: { timeout: number }) =>
- Object.assign(
- Promise.resolve({
- stdout: JSON.stringify({ timeout: options.timeout }),
- }),
- { child: { stdin: new PassThrough() } },
- ),
- () => "workbench.py",
- "fixture private workbench launcher",
- PLUGIN_ROOT,
- () => true,
- ) as (command: string, args: string[]) => Promise<{ timeout: number }>;
-
- expect(await executeWorkbench("python", ["start-prompt-only-scan"])).toEqual({
- timeout: 300_000,
- });
- expect(await executeWorkbench("python", ["start-scan"])).toEqual({
- timeout: 300_000,
- });
- expect(await executeWorkbench("python", ["other-operation"])).toEqual({
- timeout: 30_000,
- });
-});