Skip to content

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

fix(deep-scan): prevent replaced coordinators from overwriting scan results (#916)

Michael D'Angeloddffc29eeaa3
diff --git a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts b/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
index fde91abc4..468b9a07e 100644
--- a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
+++ b/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
@@ -60,6 +60,12 @@ interface PreparedScanDraft {
coverage: JsonObject;
}
+/** Host-selected Deep aggregate, separate from model-authored draft fields. */
+export interface DeepScanPublication {
+ coordinatorGeneration?: number;
+ resultPath: string | null;
+}
+
interface SavedScanDraft {
input: ScanDraftInput;
modifiedMs: number;
@@ -149,6 +155,7 @@ export async function recordCodexSecurityScanDraftViaWorkbench(
input: ScanDraftInput,
runWorkbench: RunArtifactWorkbench,
signal?: AbortSignal,
+ publication?: DeepScanPublication,
): Promise<ScanDraftResult> {
const parsed = parseScanDraft(input);
requireBoundScan(context, parsed, true);
@@ -231,7 +238,12 @@ export async function recordCodexSecurityScanDraftViaWorkbench(
const { handoffClaimToken: _claim, ...snapshot } = checkpoint;
await Promise.all([
replaceArtifactJson(checkpointPath, snapshot),
- replaceArtifactJson(draftPath, draft),
+ replaceArtifactJson(draftPath, {
+ ...draft,
+ ...(publication === undefined
+ ? {}
+ : { deepScanPublication: publication }),
+ }),
]);
const arguments_ = [
"write-scan-draft",
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 719ceb260..3b5b9a33e 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/coordinator.ts
@@ -14,6 +14,7 @@ import {
} from "./artifact-validation.js";
import {
scanDraftInputSchema,
+ type DeepScanPublication,
findingsWithinScope,
type ScanDraftInput,
} from "../artifact-scan-draft.js";
@@ -57,6 +58,7 @@ interface SchedulerResult {
reason: DeepScanTerminalReason;
omittedWorkerIds: string[];
result?: DeepReductionInput;
+ resultPath?: string;
}
export interface CoordinatorOptions {
@@ -76,7 +78,11 @@ export interface CoordinatorOptions {
run: DeepScanRunState,
signal: AbortSignal,
) => Promise<DeepScanRunState>;
- onComplete?: (draft: ScanDraftInput, signal: AbortSignal) => Promise<void>;
+ onComplete?: (
+ draft: ScanDraftInput,
+ signal: AbortSignal,
+ publication: DeepScanPublication,
+ ) => Promise<void>;
onStopped?: (run: DeepScanRunState) => Promise<void>;
}
@@ -316,6 +322,10 @@ export class DeepScanCoordinator {
await this.options.onComplete?.(
draft,
this.publicationAbortController.signal,
+ {
+ coordinatorGeneration: this.state.coordinatorGeneration,
+ resultPath: schedulerResult.resultPath ?? null,
+ },
);
if (this.canceled || this.externallyFailed) return;
const completed = await this.options.store.finish({
@@ -968,6 +978,7 @@ export class DeepScanCoordinator {
reason: stopReason,
omittedWorkerIds: [...new Set(omittedWorkerIds)],
result: latestResult,
+ resultPath: previousReducerResultPath,
};
}
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/engine.ts b/plugins/codex-security/mcp-app/src/deep-scan/engine.ts
index 598f37c56..86e536362 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/engine.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/engine.ts
@@ -48,7 +48,7 @@ export function startDeepScanEngine(options: {
log: options.log,
handoffClaimToken,
threadId,
- onComplete: async (draft, signal) => {
+ onComplete: async (draft, signal, publication) => {
const context = await createScanArtifactContext(
run.scanId,
runWorkbench,
@@ -67,6 +67,7 @@ export function startDeepScanEngine(options: {
},
runWorkbench,
signal,
+ publication,
);
},
onStopped: async (stopped) => {
diff --git a/plugins/codex-security/mcp-app/tests/clock_workbench.py b/plugins/codex-security/mcp-app/tests/clock_workbench.py
new file mode 100644
index 000000000..54c6e4475
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/clock_workbench.py
@@ -0,0 +1,15 @@
+"""Control the workbench clock while retaining its real commands and database writes."""
+
+import os
+import runpy
+import sys
+from pathlib import Path
+
+source = Path(sys.argv[1])
+sys.argv = [str(source), *sys.argv[2:]]
+sys.path.insert(0, str(source.parent))
+api = runpy.run_path(str(source), run_name="test_workbench")
+instant = os.environ["TEST_WORKBENCH_NOW"]
+api["main"].__globals__["now"] = lambda: instant
+api["_WORKBENCH_DB_CONTEXT"].now = lambda: instant
+api["main"]()
diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.ts b/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.ts
index f425f2d68..21826c0cd 100644
--- a/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.ts
+++ b/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.ts
@@ -840,6 +840,10 @@ try {
"obsolete.json",
);
await writeFile(obsoleteCheckpointPath, "{malformed obsolete checkpoint\n");
+ const deepPublication = {
+ coordinatorGeneration: 3,
+ resultPath: path.join(deepParentRoot, "workers", "reducer", "result.json"),
+ };
const deepWorkbenchWrites = mock.fn(async (arguments_: string[]) => {
assert.deepEqual(arguments_.slice(0, 3), [
"write-scan-draft",
@@ -853,6 +857,8 @@ try {
arguments_[arguments_.indexOf("--checkpoint-path") + 1];
const staged = await readJson(draftPath);
const stagedCheckpoint = await readJson(checkpointPath);
+ assert.deepEqual(staged.deepScanPublication, deepPublication);
+ assert.equal(stagedCheckpoint.deepScanPublication, undefined);
assert.deepEqual(staged.findings, acceptedDeepFindings);
assert.deepEqual(staged.coverage, acceptedDeepCoverage);
assert.deepEqual(stagedCheckpoint.findings, acceptedDeepDraft.findings);
@@ -862,6 +868,8 @@ try {
deepParentContext,
acceptedDeepDraft,
deepWorkbenchWrites,
+ undefined,
+ deepPublication,
);
assert.equal(
deepWorkbenchWrites.mock.callCount(),
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
index ea1025e8c..d8f0567f2 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
@@ -3853,6 +3853,8 @@ async function testSaturationIgnoresDiscoveryCancellationWriteFailure(
async function testPublicationUsesAcceptedReducerSnapshot() {
const { fixture, store } = await coordinatorFixture();
+ fixture.run.coordinatorGeneration = 3;
+ store.run.coordinatorGeneration = 3;
const commitDedup = store.commitDedup.bind(store);
store.commitDedup = async (commit) => {
const accepted = await commitDedup(commit);
@@ -3869,12 +3871,19 @@ async function testPublicationUsesAcceptedReducerSnapshot() {
return structuredClone(store.run);
};
const completed: ScanDraftInput[] = [];
+ const published: {
+ coordinatorGeneration?: number;
+ resultPath: string | null;
+ }[] = [];
const terminal = await runCoordinator(
fixture,
store,
new FakeExecutor({ discoveryCandidateId: "accepted-finding" }),
{
- onComplete: async (draft) => void completed.push(structuredClone(draft)),
+ onComplete: async (draft, _signal, publication) => {
+ completed.push(structuredClone(draft));
+ published.push(publication);
+ },
},
);
assert.equal(terminal?.status, "succeeded", terminal?.error);
@@ -3884,6 +3893,12 @@ async function testPublicationUsesAcceptedReducerSnapshot() {
"accepted-finding",
);
assert.equal(completed[0].coverage.completeness, "complete");
+ const reducer = [...store.workers.values()].find(
+ (worker) => worker.kind === "dedup",
+ );
+ assert.deepEqual(published, [
+ { coordinatorGeneration: 3, resultPath: reducer!.resultManifestPath },
+ ]);
}
async function testResumeDoesNotRequireHistoricalWorkerPrompt(
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_publication_integration.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_publication_integration.ts
new file mode 100644
index 000000000..d2804ddb9
--- /dev/null
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_publication_integration.ts
@@ -0,0 +1,903 @@
+import assert from "node:assert/strict";
+import { execFile } from "node:child_process";
+import {
+ mkdir,
+ mkdtemp,
+ readFile,
+ readdir,
+ rm,
+ writeFile,
+} from "node:fs/promises";
+import { tmpdir } from "node:os";
+import path from "node:path";
+import { after, test, type TestContext } from "node:test";
+import { Readable } from "node:stream";
+import { fileURLToPath } from "node:url";
+import { promisify } from "node:util";
+import { Client } from "@modelcontextprotocol/sdk/client/index.js";
+import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
+import { buildServer } from "./build-server.ts";
+import { importModule } from "./import-module.ts";
+import type { RunArtifactWorkbench } from "../src/artifact-context.ts";
+import type {
+ DeepScanRunState,
+ DeepScanWorkerMutation,
+} from "../src/deep-scan/types.ts";
+import type {
+ DeepScanPublication,
+ ScanDraftInput,
+} from "../src/artifact-scan-draft.ts";
+
+type HostModule = Pick<
+ typeof import("../src/deep-scan/store.ts"),
+ "WorkbenchDeepScanStore"
+> &
+ Pick<
+ typeof import("../src/deep-scan/coordinator.ts"),
+ "DeepScanCoordinator"
+ > &
+ Pick<
+ typeof import("../src/artifact-context.ts"),
+ "createScanArtifactContext"
+ > &
+ Pick<
+ typeof import("../src/artifact-scan-draft.ts"),
+ "recordCodexSecurityScanDraftViaWorkbench"
+ >;
+type ToolResult = Awaited<ReturnType<Client["callTool"]>>;
+interface FixturePaths {
+ target?: string;
+ scanRoot?: string;
+}
+type Scenario = readonly [
+ label: string,
+ offsets: readonly [number, number],
+ ids: readonly [string, string],
+ paths?: FixturePaths,
+ recoveryOnly?: boolean,
+ reducerLabels?: readonly [string, string],
+];
+type PublicationFixture = Awaited<ReturnType<typeof createFixture>>;
+
+const execFileAsync = promisify(execFile);
+const testsRoot = path.dirname(fileURLToPath(import.meta.url));
+const mcpAppRoot = path.resolve(testsRoot, "..");
+const pluginRoot = path.resolve(mcpAppRoot, "..");
+const workbenchPath = path.join(pluginRoot, "scripts", "workbench_db.py");
+const python = process.env.PYTHON?.trim() || "python3";
+const owner = "publication-test-owner";
+const coverage = {
+ completeness: "complete",
+ surfaces: [],
+ explicitExclusions: [],
+ deferred: [],
+};
+const highId = "ffffffff-ffff-4fff-8fff-ffffffffffff";
+const lowId = "00000000-0000-4000-8000-000000000001";
+const bundleRoot = await mkdtemp(
+ path.join(tmpdir(), "deep-publication-bundle-"),
+);
+after(() => rm(bundleRoot, { recursive: true, force: true }));
+const serverPath = path.join(bundleRoot, "server.cjs");
+await buildServer(serverPath, {
+ define: {
+ __dirname: JSON.stringify(mcpAppRoot),
+ "import.meta.url": "__filename",
+ },
+ target: "node20",
+});
+const host = (await importModule({
+ stdin: {
+ contents: [
+ 'export { WorkbenchDeepScanStore } from "./src/deep-scan/store.ts";',
+ 'export { DeepScanCoordinator } from "./src/deep-scan/coordinator.ts";',
+ 'export { createScanArtifactContext } from "./src/artifact-context.ts";',
+ 'export { recordCodexSecurityScanDraftViaWorkbench } from "./src/artifact-scan-draft.ts";',
+ ].join("\n"),
+ resolveDir: mcpAppRoot,
+ },
+ loader: { ".md": "text" },
+})) as HostModule;
+const {
+ WorkbenchDeepScanStore,
+ DeepScanCoordinator,
+ createScanArtifactContext,
+ recordCodexSecurityScanDraftViaWorkbench,
+} = host;
+
+test("public partial drafts retain checkpoints after coordinator adoption", async (t) => {
+ const fixture = await createFixture(t);
+ const { run, store, call, runWorkbench } = fixture;
+ assertSuccess(
+ await call(
+ "record_codex_security_scan_draft",
+ partial(run, "generation-one"),
+ ),
+ );
+ const first = await checkpoints(run);
+ // Main retains the submitted checkpoint and normalized parent snapshot.
+ assert.equal(Object.keys(first).length, 2);
+ const claim = await store.claimCoordinator({
+ scanId: run.scanId,
+ threadId: owner,
+ });
+ assert.equal(claim.acquired, true);
+ assert.equal(claim.run.coordinatorGeneration, 2);
+
+ assertSuccess(
+ await call(
+ "record_codex_security_scan_draft",
+ partial(run, "generation-two"),
+ ),
+ );
+ const retained = await checkpoints(run);
+ assert.equal(Object.keys(retained).length, 4);
+ for (const [name, bytes] of Object.entries(first))
+ assert.equal(retained[name], bytes);
+ assert.deepEqual(
+ new Set(
+ Object.values(retained).flatMap((bytes) =>
+ JSON.parse(bytes).coverage.deferred.map(
+ (item: { candidateId: string }) => item.candidateId,
+ ),
+ ),
+ ),
+ new Set(["generation-one", "generation-two"]),
+ );
+
+ const before = await snapshot(run);
+ assertToolError(
+ await call("record_codex_security_scan_draft", {
+ ...partial(run, "unfenced-final"),
+ complete: true,
+ }),
+ /current coordinator lease/,
+ );
+ const context = await createScanArtifactContext(run.scanId, runWorkbench, {
+ requireRunning: true,
+ });
+ await assert.rejects(
+ recordCodexSecurityScanDraftViaWorkbench(
+ context,
+ partial(run, "stale-generation"),
+ runWorkbench,
+ undefined,
+ { coordinatorGeneration: 1, resultPath: null },
+ ),
+ /newer generation/,
+ );
+ assertToolError(
+ await call("complete_codex_security_scan", { scanId: run.scanId }),
+ /deep/i,
+ );
+ assert.deepEqual(await snapshot(run), before);
+ assert.deepEqual(await readdir(path.join(run.scanDir, "drafts")), []);
+});
+
+const scenarios: Scenario[] = [
+ ["increasing timestamps", [1, 2], [highId, lowId]],
+ ["equal timestamps and descending UUIDs", [1, 1], [highId, lowId]],
+ ["decreasing timestamps", [2, 1], [lowId, highId]],
+ ["equal timestamps and ascending UUIDs", [1, 1], [lowId, highId]],
+ [
+ "legacy reducer label",
+ [1, 1],
+ [highId, lowId],
+ undefined,
+ false,
+ ["dedup-0002-legacy", "dedup-0001"],
+ ],
+ [
+ "large reducer sequence",
+ [1, 1],
+ [highId, lowId],
+ undefined,
+ false,
+ ["dedup-9007199254740992", "dedup-9007199254740993"],
+ ],
+ [
+ "scan root collision, live publication",
+ [1, 1],
+ [highId, lowId],
+ { scanRoot: "dedup-123/scans" },
+ ],
+ [
+ "scan root collision, recovery",
+ [1, 1],
+ [highId, lowId],
+ { scanRoot: "dedup-123/scans" },
+ true,
+ ],
+ [
+ "target name collision, live publication",
+ [1, 1],
+ [highId, lowId],
+ { target: "dedup-456-target" },
+ ],
+ [
+ "target name collision, recovery",
+ [1, 1],
+ [highId, lowId],
+ { target: "dedup-456-target" },
+ true,
+ ],
+];
+for (const [
+ label,
+ offsets,
+ ids,
+ paths,
+ recoveryOnly,
+ reducerLabels,
+] of scenarios) {
+ test(`selected reducer survives recovery and public completion: ${label}`, async (t) => {
+ const fixture = await createFixture(t, paths);
+ const { run, store, call, runWorkbench, instant } = fixture;
+ assertSuccess(
+ await call(
+ "record_codex_security_scan_draft",
+ partial(run, "parent-checkpoint-only"),
+ ),
+ );
+ const parentCheckpoints = await checkpoints(run);
+ assert.equal(Object.keys(parentCheckpoints).length, 2);
+ const claimed = await store.claimCoordinator({
+ scanId: run.scanId,
+ threadId: owner,
+ });
+ assert.equal(claimed.run.coordinatorGeneration, 2);
+ assert.equal(claimed.run.config.stopAfterNoNew, 4);
+ const results = await commitReducers(fixture, offsets, ids, reducerLabels);
+ const persisted = await store.get(run.scanId, owner);
+ assert.equal(persisted.noNewStreak, 4);
+ assert.ok(persisted.persistedWorkers);
+ assert.deepEqual(
+ persisted.persistedWorkers
+ .filter((worker) => worker.kind === "discovery")
+ .map((worker) => worker.mergeState),
+ Array(4).fill("merged"),
+ );
+ const context = await createScanArtifactContext(run.scanId, runWorkbench, {
+ requireRunning: true,
+ });
+ const publish = async (index: number, coordinatorGeneration = 2) =>
+ recordCodexSecurityScanDraftViaWorkbench(
+ context,
+ {
+ ...JSON.parse(await readFile(results[index], "utf8")),
+ coverage,
+ },
+ runWorkbench,
+ undefined,
+ { coordinatorGeneration, resultPath: results[index] },
+ );
+
+ if (!recoveryOnly) {
+ await publish(1);
+ const selected = await snapshot(run);
+ await assert.rejects(publish(0), /superseded publication selection/);
+ await assert.rejects(publish(1, 1), /newer generation/);
+ assert.deepEqual(await snapshot(run), selected);
+ }
+
+ const publications: DeepScanPublication[] = [];
+ const coordinator = new DeepScanCoordinator({
+ run: persisted,
+ store,
+ pluginRoot,
+ executor: {
+ run() {
+ throw new Error("Recovery must reuse the committed workers.");
+ },
+ },
+ clock: { now: () => Date.parse(instant), sleep: async () => {} },
+ onComplete: async (draft, signal, publication) => {
+ publications.push(publication);
+ await recordCodexSecurityScanDraftViaWorkbench(
+ context,
+ draft,
+ runWorkbench,
+ signal,
+ publication,
+ );
+ await assertInterleavedProgressRetained(fixture);
+ },
+ });
+ coordinator.start();
+ const terminal = await coordinator.wait(undefined, 30_000);
+ assert.equal(
+ terminal?.status,
+ "succeeded",
+ terminal?.error ?? "Coordinator did not succeed",
+ );
+ assert.deepEqual(publications, [
+ { coordinatorGeneration: 2, resultPath: results[1] },
+ ]);
+
+ const beforeCompletion = await snapshot(run);
+ const lateDraft = partial(run, "late-parent-checkpoint");
+ assertToolError(await call("record_codex_security_scan_draft", lateDraft));
+ assert.deepEqual(await snapshot(run), beforeCompletion);
+ assertSuccess(
+ await call("complete_codex_security_scan", { scanId: run.scanId }),
+ );
+ const sealed = await snapshot(run);
+ const manifest = JSON.parse(sealed.files["scan-manifest.json"]);
+ assert.equal(
+ manifest.scan.threatModel.summary,
+ reducerLabels?.[1] ?? "dedup-0002",
+ );
+ assert.ok(manifest.scan.sealedAt);
+ assert.deepEqual(JSON.parse(sealed.files["findings.json"]).findings, []);
+ assert.deepEqual(JSON.parse(sealed.files["coverage.json"]).deferred, []);
+ assert.ok(sealed.files["report.md"]);
+ for (const [name, bytes] of Object.entries(parentCheckpoints)) {
+ assert.equal(
+ sealed.checkpoints[name],
+ bytes,
+ "completion retains the immutable parent checkpoint",
+ );
+ }
+ assert.deepEqual(sealed.checkpoints, beforeCompletion.checkpoints);
+ assertSuccess(
+ await call("complete_codex_security_scan", { scanId: run.scanId }),
+ );
+ assert.deepEqual(
+ await snapshot(run),
+ sealed,
+ "completion replay is byte-stable",
+ );
+ assertToolError(await call("record_codex_security_scan_draft", lateDraft));
+ assert.deepEqual(
+ await snapshot(run),
+ sealed,
+ "late writes cannot replace sealed results",
+ );
+ });
+}
+
+for (const [claimed, outputFailure] of [
+ [false, false],
+ [false, true],
+ [true, false],
+ [true, true],
+]) {
+ test(`legacy discovery parent with claimed=${claimed} ${outputFailure ? "retains failure after an output write error" : "retains progress before first completion"}`, async (t) => {
+ const fixture = await createFixture(t);
+ const { run, store, call, runWorkbench } = fixture;
+ if (claimed) {
+ const claim = await store.claimCoordinator({
+ scanId: run.scanId,
+ threadId: owner,
+ });
+ assert.equal(claim.acquired, true);
+ assert.equal(claim.run.coordinatorGeneration, 2);
+ }
+ await commitReducers(fixture, [1, 2], [highId, lowId]);
+ const discovery = path.join(run.scanDir, "artifacts", "02_discovery");
+ await mkdir(discovery, { recursive: true });
+ await writeFile(path.join(discovery, "in_scope_files.txt"), "fixture.py\n");
+ await writeFile(path.join(discovery, "candidate_ledger.jsonl"), "");
+ const manifestPath = path.join(run.scanDir, "coordinator-manifest.json");
+ await writeFile(manifestPath, "{}\n");
+ const terminal = await store.finish({
+ scanId: run.scanId,
+ reason: "saturated",
+ manifestPath,
+ omittedWorkerIds: [],
+ });
+ assert.equal(terminal.status, "succeeded");
+ assert.equal(terminal.coordinatorGeneration, claimed ? 2 : 1);
+ assert.equal(terminal.manifestPath, manifestPath);
+ if (!outputFailure) {
+ assertSuccess(
+ await call(
+ "record_codex_security_scan_draft",
+ partial(run, "legacy-progress"),
+ ),
+ );
+ const progress = await checkpoints(run);
+ assert.ok(Object.keys(progress).length > 0);
+ assertToolError(
+ await call("complete_codex_security_scan", { scanId: run.scanId }),
+ /incomplete/,
+ );
+ assert.deepEqual(await checkpoints(run), progress);
+ }
+ assertSuccess(
+ await call("record_codex_security_scan_draft", {
+ scanId: run.scanId,
+ complete: true,
+ findings: [],
+ coverage,
+ }),
+ );
+ if (outputFailure) {
+ const report = path.join(run.scanDir, "report.html");
+ await mkdir(report);
+ assertToolError(
+ await call("complete_codex_security_scan", { scanId: run.scanId }),
+ /report\.html/,
+ );
+ await rm(report, { recursive: true });
+ const context = await runWorkbench(["get-scan", "--scan-id", run.scanId]);
+ assert.equal(
+ (context.scan as { progress: { status: string } }).progress.status,
+ "failed",
+ );
+ const interrupted = await snapshot(run);
+ assertToolError(
+ await call("record_codex_security_scan_draft", {
+ ...partial(run, "late-legacy-draft"),
+ complete: true,
+ threatModel: { summary: "Late replacement" },
+ }),
+ );
+ assert.deepEqual(await snapshot(run), interrupted);
+ assertToolError(
+ await call("complete_codex_security_scan", { scanId: run.scanId }),
+ /running/,
+ );
+ } else {
+ assertSuccess(
+ await call("complete_codex_security_scan", { scanId: run.scanId }),
+ );
+ }
+ });
+}
+
+for (const complete of [false, true]) {
+ test(`legacy terminal aggregate rejects late public drafts with complete=${complete}`, async (t) => {
+ const fixture = await createFixture(t);
+ const { run, store, call, runWorkbench } = fixture;
+ await commitReducers(fixture, [1, 2], [highId, lowId]);
+ const context = await createScanArtifactContext(run.scanId, runWorkbench, {
+ requireRunning: true,
+ });
+ const draft: ScanDraftInput = {
+ scanId: run.scanId,
+ complete: true,
+ findings: [],
+ coverage,
+ threatModel: { summary: "Selected aggregate" },
+ };
+ await assert.rejects(
+ recordCodexSecurityScanDraftViaWorkbench(context, draft, async (args) => {
+ await runWorkbench(args);
+ throw new Error("Synthetic lost publication response");
+ }),
+ /lost publication response/,
+ );
+ const published = await snapshot(run);
+ await recordCodexSecurityScanDraftViaWorkbench(
+ context,
+ draft,
+ runWorkbench,
+ );
+ assert.deepEqual(await snapshot(run), published);
+ await assertInterleavedProgressRetained(fixture);
+ const terminal = await store.finish({
+ scanId: run.scanId,
+ reason: "saturated",
+ manifestPath: path.join(run.scanDir, "scan-manifest.json"),
+ omittedWorkerIds: [],
+ });
+ assert.equal(terminal.status, "succeeded");
+ assert.equal(terminal.coordinatorGeneration, 1);
+ const report = path.join(run.scanDir, "report.html");
+ await mkdir(report);
+ assertToolError(
+ await call("complete_codex_security_scan", { scanId: run.scanId }),
+ /report\.html/,
+ );
+ await rm(report, { recursive: true });
+ const interrupted = await snapshot(run);
+ assertToolError(
+ await call("record_codex_security_scan_draft", {
+ ...partial(run, "late-progress"),
+ complete,
+ threatModel: { summary: "Late replacement" },
+ }),
+ /terminal|publication/,
+ );
+ assert.deepEqual(await snapshot(run), interrupted);
+ assertSuccess(
+ await call("complete_codex_security_scan", { scanId: run.scanId }),
+ );
+ const completed = await snapshot(run);
+ assert.deepEqual(
+ JSON.parse(completed.files["coverage.json"]),
+ JSON.parse(interrupted.files["coverage.json"]),
+ );
+ assert.equal(
+ JSON.parse(completed.files["scan-manifest.json"]).scan.threatModel
+ .summary,
+ "Selected aggregate",
+ );
+ assert.deepEqual(completed.checkpoints, interrupted.checkpoints);
+ });
+}
+
+for (const claimed of [false, true]) {
+ test(`partial drafts retain a capped publication with claimed=${claimed}`, async (t) => {
+ const fixture = await createFixture(t);
+ const { run, store, call, runWorkbench, instant } = fixture;
+ const selectedRun = claimed
+ ? (await store.claimCoordinator({ scanId: run.scanId, threadId: owner }))
+ .run
+ : run;
+ const context = await createScanArtifactContext(run.scanId, runWorkbench, {
+ requireRunning: true,
+ });
+ const elapsedSeconds = ((run.config.maxTimeHours ?? 1) + 1) * 3_600;
+ fixture.setTime(elapsedSeconds);
+ const deadline = Date.parse(instant) + elapsedSeconds * 1_000;
+ const coordinator = new DeepScanCoordinator({
+ run: selectedRun,
+ store,
+ pluginRoot,
+ executor: {
+ run() {
+ throw new Error("Expired coordinator must not start a worker.");
+ },
+ },
+ clock: { now: () => deadline, sleep: async () => {} },
+ onComplete: async (draft, signal, publication) => {
+ await recordCodexSecurityScanDraftViaWorkbench(
+ context,
+ draft,
+ runWorkbench,
+ signal,
+ claimed ? publication : undefined,
+ );
+ await assertInterleavedProgressRetained(fixture);
+ },
+ });
+ coordinator.start();
+ const terminal = await coordinator.wait(undefined, 30_000);
+ assert.equal(
+ terminal?.status,
+ "succeeded",
+ terminal?.error ?? "Coordinator did not succeed",
+ );
+ assert.equal(terminal?.terminalReason, "capped");
+
+ const report = path.join(run.scanDir, "report.html");
+ await mkdir(report);
+ assertToolError(
+ await call("complete_codex_security_scan", { scanId: run.scanId }),
+ /report\.html/,
+ );
+ await rm(report, { recursive: true });
+ const interrupted = await snapshot(run);
+ assert.equal(
+ JSON.parse(interrupted.files["coverage.json"]).completeness,
+ "partial",
+ );
+ assertToolError(
+ await call(
+ "record_codex_security_scan_draft",
+ partial(run, "late-progress"),
+ ),
+ /coordinator lease|terminal/,
+ );
+ assert.deepEqual(await snapshot(run), interrupted);
+ assertSuccess(
+ await call("complete_codex_security_scan", { scanId: run.scanId }),
+ );
+ const completed = await snapshot(run);
+ assert.ok(JSON.parse(completed.files["scan-manifest.json"]).scan.sealedAt);
+ assert.deepEqual(
+ JSON.parse(completed.files["coverage.json"]),
+ JSON.parse(interrupted.files["coverage.json"]),
+ );
+ assert.deepEqual(completed.checkpoints, interrupted.checkpoints);
+ assert.equal((await store.get(run.scanId, owner)).terminalReason, "capped");
+ });
+}
+
+for (const status of ["failed", "interrupted"] as const) {
+ test(`selected publication retains ${status} and exact failure replay`, async (t) => {
+ const { run, store, runWorkbench } = await createFixture(t);
+ await store.claimCoordinator({ scanId: run.scanId, threadId: owner });
+ const context = await createScanArtifactContext(run.scanId, runWorkbench, {
+ requireRunning: true,
+ });
+ const draft: ScanDraftInput = {
+ scanId: run.scanId,
+ findings: [],
+ coverage,
+ };
+ const publication = { coordinatorGeneration: 2, resultPath: null };
+ await assert.rejects(
+ recordCodexSecurityScanDraftViaWorkbench(
+ context,
+ draft,
+ async (args) => {
+ await runWorkbench(args);
+ throw new Error("Synthetic lost publication response");
+ },
+ undefined,
+ publication,
+ ),
+ /lost publication response/,
+ );
+ await recordCodexSecurityScanDraftViaWorkbench(
+ context,
+ draft,
+ runWorkbench,
+ undefined,
+ publication,
+ );
+ const message = "Synthetic failure after publication";
+ const failed = await store.fail(run.scanId, message, status);
+ assert.equal(failed.status, status);
+ assert.equal(
+ failed.manifestPath,
+ path.join(run.scanDir, "scan-manifest.json"),
+ );
+ assert.deepEqual(await store.fail(run.scanId, message, status), failed);
+ await assert.rejects(
+ store.fail(run.scanId, message + " changed", status),
+ /immutable/,
+ );
+ const differentManifest = path.join(run.scanDir, "different-manifest.json");
+ await writeFile(differentManifest, "{}\n");
+ await assert.rejects(
+ store.fail(run.scanId, message, status, differentManifest),
+ /immutable/,
+ );
+ });
+}
+
+async function assertInterleavedProgressRetained(fixture: PublicationFixture) {
+ const { run, call } = fixture;
+ const before = await snapshot(run);
+ const progress = {
+ scanId: run.scanId,
+ complete: false,
+ findings: [],
+ coverage: {
+ ...coverage,
+ completeness: "partial",
+ deferred: [
+ {
+ id: "interleaved-progress",
+ candidateId: "interleaved-progress",
+ reason: "Synthetic review remains pending.",
+ paths: ["fixture.py"],
+ },
+ ],
+ },
+ threatModel: { summary: "Unselected progress model" },
+ };
+ assertSuccess(await call("record_codex_security_scan_draft", progress));
+ const after = await snapshot(run);
+ assert.deepEqual(after.files, before.files);
+ for (const [name, contents] of Object.entries(before.checkpoints))
+ assert.equal(after.checkpoints[name], contents);
+ const added = Object.keys(after.checkpoints).filter(
+ (name) => !(name in before.checkpoints),
+ );
+ assert.equal(added.length, 1);
+ assert.deepEqual(JSON.parse(after.checkpoints[added[0]]), progress);
+}
+
+async function createFixture(t: TestContext, paths: FixturePaths = {}) {
+ const root = await mkdtemp(path.join(tmpdir(), "deep-publication-"));
+ let client: Client | undefined;
+ t.after(async () => {
+ await client?.close();
+ await rm(root, { recursive: true, force: true });
+ });
+ const targetPath = path.join(root, paths.target ?? "target");
+ await mkdir(targetPath);
+ await writeFile(
+ path.join(targetPath, "fixture.py"),
+ "# Synthetic publication fixture\n",
+ );
+ const environment: Record<string, string> = Object.fromEntries(
+ Object.entries({
+ ...process.env,
+ CODEX_HOME: path.join(root, "home"),
+ CODEX_SECURITY_STATE_DIR: path.join(root, "state"),
+ CODEX_SECURITY_SCAN_ROOT: path.join(root, paths.scanRoot ?? "scans"),
+ }).filter((entry): entry is [string, string] => entry[1] !== undefined),
+ );
+ delete environment.CODEX_SECURITY_DEEP_SCAN_CONFIG_PATH;
+ const instant = new Date().toISOString();
+ let now = instant;
+ const runWorkbench: RunArtifactWorkbench = async (args, input) => {
+ const child = execFileAsync(
+ python,
+ [path.join(testsRoot, "clock_workbench.py"), workbenchPath, ...args],
+ {
+ cwd: pluginRoot,
+ env: { ...environment, TEST_WORKBENCH_NOW: now },
+ timeout: 30_000,
+ maxBuffer: 4 * 1024 * 1024,
+ },
+ );
+ assert.ok(child.child.stdin);
+ child.child.stdin.end(input);
+ return JSON.parse((await child).stdout) as Record<string, unknown>;
+ };
+ const store = new WorkbenchDeepScanStore(runWorkbench);
+ const run = await store.begin({
+ targetPath,
+ scope: ".",
+ threadId: owner,
+ scanRoot: environment.CODEX_SECURITY_SCAN_ROOT,
+ });
+ client = new Client({ name: "publication-integration", version: "1.0.0" });
+ const transport = new StdioClientTransport({
+ command: process.execPath,
+ args: [serverPath, "--stdio"],
+ cwd: mcpAppRoot,
+ env: environment,
+ stderr: "pipe",
+ });
+ if (transport.stderr instanceof Readable) transport.stderr.resume();
+ await client.connect(transport);
+ return {
+ run,
+ store,
+ runWorkbench,
+ instant,
+ setTime(offset: number) {
+ now = new Date(Date.parse(instant) + offset * 1_000).toISOString();
+ },
+ call(name: string, args: Record<string, unknown> | ScanDraftInput) {
+ assert.ok(client);
+ return client.callTool({
+ name,
+ arguments: { ...args },
+ _meta: { "openai/threadId": owner },
+ });
+ },
+ };
+}
+
+async function commitReducers(
+ fixture: PublicationFixture,
+ offsets: readonly [number, number],
+ ids: readonly [string, string],
+ reducerLabels?: readonly [string, string],
+) {
+ const { run, store } = fixture;
+ const results = [];
+ for (let batch = 0; batch < 2; batch++) {
+ const workerIds = [];
+ for (let index = batch * 2 + 1; index <= batch * 2 + 2; index++) {
+ const id = `10000000-0000-4000-8000-${String(index).padStart(12, "0")}`;
+ const label = `discovery-${String(index).padStart(4, "0")}`;
+ const artifact = await workerArtifact(run, "workers", label);
+ const update: DeepScanWorkerMutation = {
+ id,
+ scanId: run.scanId,
+ kind: "discovery",
+ status: "running",
+ attempt: 1,
+ ...artifact,
+ };
+ await store.updateWorker(update);
+ const resultManifestPath = path.join(artifact.artifactDir, "result.json");
+ await writeFile(
+ resultManifestPath,
+ JSON.stringify({ scanId: run.scanId, findings: [], coverage }),
+ );
+ await store.updateWorker({
+ ...update,
+ status: "succeeded",
+ resultManifestPath,
+ });
+ workerIds.push(id);
+ }
+ const label =
+ reducerLabels?.[batch] ?? `dedup-${String(batch + 1).padStart(4, "0")}`;
+ const artifact = await workerArtifact(run, "dedup", label);
+ const id = ids[batch];
+ await store.claimDedup({ id, scanId: run.scanId, workerIds, ...artifact });
+ await store.updateWorker({
+ id,
+ scanId: run.scanId,
+ kind: "dedup",
+ status: "running",
+ attempt: 1,
+ ...artifact,
+ });
+ const resultManifestPath = path.join(artifact.artifactDir, "result.json");
+ await writeFile(
+ resultManifestPath,
+ JSON.stringify({
+ scanId: run.scanId,
+ findings: [],
+ threatModel: { summary: label },
+ }),
+ );
+ fixture.setTime(offsets[batch]);
+ await store.commitDedup({
+ id,
+ scanId: run.scanId,
+ resultManifestPath,
+ newFindings: 0,
+ });
+ fixture.setTime(0);
+ results.push(resultManifestPath);
+ }
+ return results;
+}
+
+async function workerArtifact(
+ run: DeepScanRunState,
+ kind: "workers" | "dedup",
+ label: string,
+) {
+ const artifactDir = path.join(
+ run.scanDir,
+ "artifacts",
+ "deep_discovery",
+ kind,
+ label,
+ "output",
+ );
+ const promptPath = path.join(path.dirname(artifactDir), "prompt.md");
+ await mkdir(artifactDir, { recursive: true });
+ await writeFile(promptPath, `Synthetic ${label}\n`);
+ return { artifactDir, promptPath };
+}
+
+function partial(run: DeepScanRunState, candidateId: string): ScanDraftInput {
+ return {
+ scanId: run.scanId,
+ complete: false,
+ findings: [],
+ coverage: {
+ ...coverage,
+ completeness: "partial",
+ deferred: [
+ {
+ candidateId,
+ reason: "Synthetic review remains pending.",
+ paths: ["fixture.py"],
+ },
+ ],
+ },
+ };
+}
+
+async function checkpoints(run: DeepScanRunState) {
+ const directory = path.join(run.scanDir, "checkpoints");
+ const files: Record<string, string> = {};
+ for (const name of await readdir(directory))
+ files[name] = await readFile(path.join(directory, name), "utf8");
+ return files;
+}
+
+async function snapshot(run: DeepScanRunState) {
+ const files: Record<string, string> = {};
+ for (const name of [
+ "scan-manifest.json",
+ "findings.json",
+ "coverage.json",
+ "report.md",
+ "checkpoint-head.json",
+ "threatmodel.md",
+ ]) {
+ try {
+ files[name] = await readFile(path.join(run.scanDir, name), "utf8");
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
+ }
+ }
+ return { files, checkpoints: await checkpoints(run) };
+}
+
+function assertSuccess(result: ToolResult) {
+ assert.notEqual(result.isError, true, JSON.stringify(result));
+}
+
+function assertToolError(result: ToolResult, pattern?: RegExp) {
+ assert.equal(result.isError, true, JSON.stringify(result));
+ if (pattern) assert.match(JSON.stringify(result), pattern);
+}
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.ts
index 961fb4b76..adb2bc629 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_store_integration.ts
@@ -10,7 +10,10 @@ import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
import path from "node:path";
import { promisify } from "node:util";
import { importModule } from "./import-module.ts";
-import type { ScanDraftInput } from "../src/artifact-scan-draft.js";
+import type {
+ DeepScanPublication,
+ ScanDraftInput,
+} from "../src/artifact-scan-draft.js";
import {
DeepScanCoordinator,
FakeExecutor,
@@ -120,7 +123,11 @@ async function testCoordinatorCommitResponseRecovery(
threadId,
executor: new FakeExecutor(),
heartbeatIntervalMs: 60_000,
- onComplete: async (draft: ScanDraftInput, signal: AbortSignal) => {
+ onComplete: async (
+ draft: ScanDraftInput,
+ signal: AbortSignal,
+ publication: DeepScanPublication,
+ ) => {
const context = await createScanArtifactContext(
run.scanId,
runWorkbench,
@@ -134,6 +141,7 @@ async function testCoordinatorCommitResponseRecovery(
draft,
runWorkbench,
signal,
+ publication,
);
},
});
@@ -279,6 +287,11 @@ async function testRecoveredPublicationRejectsLateFailure() {
"Publication recovery remains pending.",
),
runWorkbench,
+ undefined,
+ {
+ coordinatorGeneration: claim.run.coordinatorGeneration,
+ resultPath: null,
+ },
);
await runWorkbench([
"cancel-scan",
diff --git a/plugins/codex-security/scripts/deep_scan_workbench.py b/plugins/codex-security/scripts/deep_scan_workbench.py
index ae32a98fc..e4553cbb9 100644
--- a/plugins/codex-security/scripts/deep_scan_workbench.py
+++ b/plugins/codex-security/scripts/deep_scan_workbench.py
@@ -1876,6 +1876,8 @@ def fail_deep_scan_locked(
kind="file",
)
)
+ elif run["manifest_path"] == str(Path(scan["scan_dir"]) / "scan-manifest.json"):
+ manifest_path = run["manifest_path"]
if run["status"] in {"failed", "interrupted"} or scan["status"] == "failed":
if (
run["status"] == args.deep_status
diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.py
index 95309919f..9b4492450 100644
--- a/plugins/codex-security/scripts/workbench_db.py
+++ b/plugins/codex-security/scripts/workbench_db.py
@@ -1550,8 +1550,13 @@ def complete_scan_locked(
prepared, projection_warnings=warnings
)
except ContractError as exc:
- if wrote or (
+ # Only a coordinator-selected parent is fenced for output-write replay.
+ selected_deep_parent = scan["mode"] == "deep" and deep_scan.require_deep_scan_run(
+ connection, scan_id
+ )["manifest_path"] == str(scan_dir / ARTIFACTS["manifest"])
+ if (wrote and not selected_deep_parent) or (
scan["mode"] == "deep"
+ and not wrote
and not already_sealed
and not isinstance(exc, RecoverableContractError)
):
diff --git a/plugins/codex-security/scripts/workbench_saved_results.py b/plugins/codex-security/scripts/workbench_saved_results.py
index ae0a7620e..1ec8b5260 100644
--- a/plugins/codex-security/scripts/workbench_saved_results.py
+++ b/plugins/codex-security/scripts/workbench_saved_results.py
@@ -2387,6 +2387,66 @@ def save_scan_artifact(db: Any, connection: Any, args: Any) -> dict[str, Any]:
return {"scanId": scan_id, "path": str(scan_dir / output)}
+def _require_current_deep_publication(
+ db: Any, connection: Any, scan: Any, draft: dict[str, Any]
+) -> None:
+ scan_id = scan["id"]
+ run = connection.execute(
+ "SELECT * FROM deep_scan_runs WHERE scan_id = ?", (scan_id,)
+ ).fetchone()
+ publication = draft.get("deepScanPublication")
+ if (
+ publication is None
+ and draft["manifest"]["scan"].get("complete") is False
+ and (run is None or run["status"] == "running")
+ ):
+ return
+ if run is None:
+ run = db.deep_scan.require_deep_scan_run(connection, scan_id)
+ # Finished discovery-only runs hand remaining publication back to the parent.
+ if (
+ publication is None
+ and run["status"] != "running"
+ and run["manifest_path"] != str(Path(scan["scan_dir"]) / "scan-manifest.json")
+ ):
+ return
+ db.deep_scan.require_current_coordinator(
+ run,
+ argparse.Namespace(
+ coordinator_generation=publication.get("coordinatorGeneration") if publication else None
+ ),
+ )
+ # Once finish selects the canonical parent, only completion may replay it.
+ if publication is None:
+ if run["status"] != "running" and run["manifest_path"] == str(
+ Path(scan["scan_dir"]) / "scan-manifest.json"
+ ):
+ raise SystemExit("Deep Scan is terminal; drafts cannot replace its publication.")
+ return
+
+ # Match the durable reducer sequence used by coordinator recovery.
+ def reducer_order(worker: Any) -> tuple[int, str]:
+ match = re.fullmatch(r"dedup-([0-9]+)", Path(worker["prompt_path"]).parent.name)
+ return (int(match[1]) if match else 0, worker["id"])
+
+ reducer = max(
+ (
+ worker
+ for worker in connection.execute(
+ "SELECT * FROM deep_scan_workers WHERE scan_id = ?", (scan_id,)
+ )
+ if worker["kind"] == "dedup"
+ and worker["status"] == "succeeded"
+ and worker["result_manifest_path"]
+ ),
+ key=reducer_order,
+ default=None,
+ )
+ selected_result = reducer["result_manifest_path"] if reducer is not None else None
+ if publication["resultPath"] != selected_result:
+ raise SystemExit("Deep Scan aggregate belongs to a superseded publication selection.")
+
+
def write_scan_draft(db: Any, connection: Any, args: Any) -> dict[str, Any]:
scan_id = db.require_uuid(args.scan_id, "scan-id")
with db.scan_completion_lock(scan_id):
@@ -2421,6 +2481,8 @@ def write_scan_draft(db: Any, connection: Any, args: Any) -> dict[str, Any]:
_populate_unsealed_manifest_envelope(manifest, manifest["scan"], binding)
_populate_unsealed_artifact_envelope(manifest, findings, coverage, binding)
_validate_completion_binding(manifest, findings, coverage, binding)
+ checkpoint = manifest["scan"]
+ checkpoint_contents = None
if args.checkpoint_path is not None:
try:
checkpoint_relative = Path(args.checkpoint_path).relative_to(scan_dir).as_posix()
@@ -2437,18 +2499,36 @@ def write_scan_draft(db: Any, connection: Any, args: Any) -> dict[str, Any]:
)
if checkpoint.get("scanId") != scan_id:
raise SystemExit("Staged scan checkpoint belongs to another scan.")
+ checkpoint_only = (
+ scan["mode"] == "deep"
+ and draft.get("deepScanPublication") is None
+ and checkpoint.get("complete") is False
+ and connection.execute(
+ "SELECT 1 FROM deep_scan_runs WHERE scan_id = ? "
+ "AND status = 'running' AND manifest_path = ?",
+ (scan_id, str(scan_dir / "scan-manifest.json")),
+ ).fetchone()
+ is not None
+ )
+ if scan["mode"] == "deep" and not checkpoint_only:
+ _require_current_deep_publication(db, connection, scan, draft)
+ if checkpoint_contents is not None:
checkpoint_digest = hashlib.sha256(checkpoint_contents).hexdigest()
write_scan_local_bytes(
scan_dir,
f"checkpoints/{checkpoint_digest}.json",
checkpoint_contents,
)
+ if checkpoint_only:
+ return {"scanId": scan_id, "status": "draft_written"}
checkpoint = _parent_scan_draft(scan_id, manifest["scan"], findings, coverage)
checkpoint_contents = _encoded(checkpoint)
checkpoint_name = f"{hashlib.sha256(checkpoint_contents).hexdigest()}.json"
checkpoint_relative = f"checkpoints/{checkpoint_name}"
if not (scan_dir / checkpoint_relative).exists():
write_scan_local_bytes(scan_dir, checkpoint_relative, checkpoint_contents)
+ if checkpoint_only:
+ return {"scanId": scan_id, "status": "draft_written"}
write_scan_local_bytes(
scan_dir, "checkpoint-head.json", _encoded({"checkpoint": checkpoint_name})
)
@@ -2462,6 +2542,14 @@ def write_scan_draft(db: Any, connection: Any, args: Any) -> dict[str, Any]:
filename,
(json.dumps(document, allow_nan=False, indent=2) + "\n").encode(),
)
+ if scan["mode"] == "deep" and manifest["scan"].get("complete") is not False:
+ # Select the accepted final publication before releasing the write lock.
+ with connection:
+ connection.execute(
+ "UPDATE deep_scan_runs SET manifest_path = ? "
+ "WHERE scan_id = ? AND status = 'running'",
+ (str(scan_dir / "scan-manifest.json"), scan_id),
+ )
model_warning = write_threat_model_projection_if_possible(scan_dir, manifest)
# Accepted Standard drafts are evidence of review or report assembly,
# even when the parent omitted its explicit progress call.
diff --git a/plugins/codex-security/tests/test_deep_scan_publication_authority.py b/plugins/codex-security/tests/test_deep_scan_publication_authority.py
new file mode 100644
index 000000000..e30b9cc24
--- /dev/null
+++ b/plugins/codex-security/tests/test_deep_scan_publication_authority.py
@@ -0,0 +1,215 @@
+from __future__ import annotations
+
+import copy
+import json
+import uuid
+from argparse import Namespace
+
+import pytest
+from test_deep_scan_successful_publication import add_worker, complete
+from test_deep_scan_successful_publication import publication_scan as publication_scan
+
+
+def stage_publication(scan, *, generation, result_path, title, complete=True):
+ draft_dir = scan.scan_dir / "drafts"
+ draft_dir.mkdir(exist_ok=True)
+ draft_path = draft_dir / f"{uuid.uuid4()}.json"
+ checkpoint_path = draft_dir / f"{uuid.uuid4()}.checkpoint.json"
+ findings = copy.deepcopy(scan.findings)
+ findings[0]["title"] = title
+ draft = {
+ "manifest": json.loads((scan.scan_dir / "scan-manifest.json").read_text()),
+ "findings": {"findings": findings},
+ "coverage": scan.coverage,
+ }
+ if not complete:
+ draft["manifest"]["scan"]["complete"] = False
+ if generation is not None:
+ draft["deepScanPublication"] = {
+ "coordinatorGeneration": generation,
+ "resultPath": str(result_path),
+ }
+ draft_path.write_text(json.dumps(draft))
+ checkpoint_path.write_text(
+ json.dumps({"scanId": scan.scan_id, "findings": findings, "coverage": scan.coverage})
+ )
+ return Namespace(
+ scan_id=scan.scan_id,
+ claim_token=None,
+ draft_path=str(draft_path),
+ checkpoint_path=str(checkpoint_path),
+ expected_draft_digest=None,
+ )
+
+
+ ("stale", "complete"),
+ [
+ ("generation", True),
+ ("generation", False),
+ ("aggregate", True),
+ ("aggregate", False),
+ ("unfenced", True),
+ ],
+)
+def test_stale_coordinator_cannot_replace_newer_canonical_publication(
+ workbench_api, workbench_db, publication_scan, stale, complete
+):
+ scan = publication_scan()
+ old_result = add_worker(workbench_db, scan)
+ new_result = add_worker(workbench_db, scan)
+ with workbench_db:
+ workbench_db.execute(
+ "UPDATE deep_scan_runs SET coordinator_generation = 3 WHERE scan_id = ?",
+ (scan.scan_id,),
+ )
+ for sequence, result in enumerate((old_result, new_result), start=1):
+ workbench_db.execute(
+ "UPDATE deep_scan_workers SET kind = 'dedup', merge_state = 'none', "
+ "prompt_path = ?, completed_at = ? "
+ "WHERE result_manifest_path = ?",
+ (
+ str(scan.scan_dir / f"dedup-{sequence:04d}" / "prompt.md"),
+ f"2026-01-0{sequence}",
+ str(result),
+ ),
+ )
+ current = stage_publication(
+ scan, generation=3, result_path=new_result, title="Current accepted aggregate"
+ )
+ workbench_api["saved_results"].write_scan_draft(
+ workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, current
+ )
+ saved = {
+ path: path.read_bytes()
+ for path in scan.scan_dir.rglob("*.json")
+ if "drafts" not in path.parts
+ }
+ old = stage_publication(
+ scan,
+ generation=None if stale == "unfenced" else 2 if stale == "generation" else 3,
+ result_path=old_result if stale == "aggregate" else new_result,
+ title="Superseded aggregate",
+ complete=complete,
+ )
+
+ with pytest.raises(SystemExit, match="coordinator|aggregate"):
+ workbench_api["saved_results"].write_scan_draft(
+ workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, old
+ )
+
+ assert {
+ path: path.read_bytes()
+ for path in scan.scan_dir.rglob("*.json")
+ if "drafts" not in path.parts
+ } == saved
+
+
[email protected]("generation", [None, 3], ids=["legacy-generation-one", "current-lease"])
+def test_current_publication_replays_without_changing_checkpoint_or_worker_state(
+ workbench_api, workbench_db, publication_scan, generation, monkeypatch
+):
+ # Keep replay time fixed while comparing the preserved publication bytes.
+ instant = workbench_api["now"]()
+ monkeypatch.setattr(workbench_api["_WORKBENCH_DB_CONTEXT"], "now", lambda: instant)
+ scan = publication_scan()
+ result = add_worker(workbench_db, scan)
+ with workbench_db:
+ workbench_db.execute(
+ "UPDATE deep_scan_runs SET coordinator_generation = ?, status = 'running' "
+ "WHERE scan_id = ?",
+ (generation or 1, scan.scan_id),
+ )
+ workbench_db.execute(
+ "UPDATE deep_scan_workers SET kind = 'dedup', merge_state = 'none' WHERE scan_id = ?",
+ (scan.scan_id,),
+ )
+ draft = stage_publication(
+ scan, generation=generation, result_path=result, title="Accepted aggregate"
+ )
+ run_before = dict(workbench_db.execute("SELECT * FROM deep_scan_runs").fetchone())
+ worker_before = dict(workbench_db.execute("SELECT * FROM deep_scan_workers").fetchone())
+
+ workbench_api["saved_results"].write_scan_draft(
+ workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, draft
+ )
+ published = {
+ path: path.read_bytes()
+ for path in scan.scan_dir.rglob("*.json")
+ if "drafts" not in path.parts
+ }
+ replay = workbench_api["saved_results"].write_scan_draft(
+ workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, draft
+ )
+
+ assert replay == {"scanId": scan.scan_id, "status": "draft_written"}
+ assert {
+ path: path.read_bytes()
+ for path in scan.scan_dir.rglob("*.json")
+ if "drafts" not in path.parts
+ } == published
+ # Main retains both the submitted checkpoint and its normalized parent snapshot.
+ assert len(list((scan.scan_dir / "checkpoints").glob("*.json"))) == 2
+ assert dict(workbench_db.execute("SELECT * FROM deep_scan_runs").fetchone()) == run_before
+ assert dict(workbench_db.execute("SELECT * FROM deep_scan_workers").fetchone()) == worker_before
+ findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]
+ assert findings[0]["title"] == "Accepted aggregate"
+
+
+def test_legacy_terminal_aggregate_survives_a_late_partial_draft_after_write_failure(
+ workbench_api, workbench_db, publication_scan, monkeypatch
+):
+ scan = publication_scan()
+ scan.coverage["completeness"] = "partial"
+ scan.coverage["deferred"] = [
+ {"id": "remaining-review", "reason": "The bounded scan retained unfinished review."}
+ ]
+ (scan.scan_dir / "coverage.json").write_text(json.dumps(scan.coverage))
+ finalizer_globals = workbench_api["_write_prepared_scan_finalization"].__globals__
+ write_bytes = finalizer_globals["write_scan_local_bytes"]
+
+ def fail_report(scan_dir, relative_path, payload, **kwargs):
+ if relative_path == "report.md":
+ raise finalizer_globals["ContractError"]("Synthetic report write interruption")
+ return write_bytes(scan_dir, relative_path, payload, **kwargs)
+
+ with monkeypatch.context() as patch:
+ patch.setitem(finalizer_globals, "write_scan_local_bytes", fail_report)
+ with pytest.raises(SystemExit, match="Synthetic report write interruption"):
+ complete(workbench_api, workbench_db, scan)
+
+ run = workbench_db.execute(
+ "SELECT status, coordinator_generation FROM deep_scan_runs WHERE scan_id = ?",
+ (scan.scan_id,),
+ ).fetchone()
+ assert tuple(run) == ("succeeded", 1)
+ assert (
+ workbench_db.execute(
+ "SELECT status, seal_manifest_digest FROM scans WHERE id = ?", (scan.scan_id,)
+ ).fetchone()[0]
+ == "running"
+ )
+ saved = {
+ path: path.read_bytes()
+ for path in scan.scan_dir.rglob("*.json")
+ if "drafts" not in path.parts
+ }
+ late = stage_publication(
+ scan, generation=None, result_path=None, title="Late incomplete progress", complete=False
+ )
+ with pytest.raises(SystemExit, match="terminal|publication"):
+ workbench_api["saved_results"].write_scan_draft(
+ workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, late
+ )
+ assert {
+ path: path.read_bytes()
+ for path in scan.scan_dir.rglob("*.json")
+ if "drafts" not in path.parts
+ } == saved
+ assert complete(workbench_api, workbench_db, scan)["progress"]["status"] == "complete"
+ findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]
+ assert [finding["title"] for finding in findings] == [scan.findings[0]["title"]]
+ assert (
+ json.loads((scan.scan_dir / "coverage.json").read_text())["deferred"]
+ == scan.coverage["deferred"]
+ )
diff --git a/plugins/codex-security/tests/test_deep_scan_publication_replay.py b/plugins/codex-security/tests/test_deep_scan_publication_replay.py
new file mode 100644
index 000000000..4060d1e2a
--- /dev/null
+++ b/plugins/codex-security/tests/test_deep_scan_publication_replay.py
@@ -0,0 +1,153 @@
+from __future__ import annotations
+
+import json
+import sqlite3
+import subprocess
+import sys
+from argparse import Namespace
+from pathlib import Path
+
+import pytest
+from test_deep_scan_publication_authority import stage_publication
+from test_deep_scan_successful_publication import add_worker
+from test_deep_scan_successful_publication import publication_scan as publication_scan
+
+_CRASH_PUBLICATION = """
+import json, os, runpy, sqlite3, sys
+from argparse import Namespace
+
+api = runpy.run_path(sys.argv[1], run_name="publication_crash_test")
+args = Namespace(**json.loads(sys.argv[3]))
+boundary = sys.argv[4]
+
+class CrashConnection(sqlite3.Connection):
+ def __exit__(self, exc_type, exc_value, traceback):
+ completing = exc_type is None and self.execute(
+ "SELECT status FROM scans WHERE id = ?", (args.scan_id,)
+ ).fetchone()[0] == "complete"
+ if completing and boundary == "sqlite-before":
+ os._exit(72)
+ result = super().__exit__(exc_type, exc_value, traceback)
+ if completing and boundary == "sqlite-after":
+ os._exit(73)
+ return result
+
+connection = sqlite3.connect(sys.argv[2], factory=CrashConnection)
+connection.row_factory = sqlite3.Row
+connection.execute("PRAGMA foreign_keys = ON")
+if boundary.startswith("sqlite-"):
+ api["complete_scan"](connection, Namespace(
+ scan_id=args.scan_id, claim_token=None, cost_json=None
+ ))
+else:
+ saved = api["saved_results"]
+ original_write = saved.write_scan_local_bytes
+ def crash_after_write(root, relative, contents):
+ original_write(root, relative, contents)
+ if relative == boundary:
+ os._exit(71)
+ saved.write_scan_local_bytes = crash_after_write
+ api["saved_results"].write_scan_draft(api["_WORKBENCH_DB_CONTEXT"], connection, args)
+raise AssertionError("publication never reached the requested crash boundary")
+"""
+
+
+ "boundary",
+ ["findings.json", "coverage.json", "scan-manifest.json", "sqlite-before", "sqlite-after"],
+)
+def test_publication_crash_replays_selected_input_without_stale_overwrite(
+ workbench_api, workbench_db, publication_scan, tmp_path, boundary
+):
+ scan = publication_scan()
+ result_path = add_worker(workbench_db, scan)
+ with workbench_db:
+ workbench_db.execute(
+ "UPDATE deep_scan_runs SET coordinator_generation = 3 WHERE scan_id = ?",
+ (scan.scan_id,),
+ )
+ workbench_db.execute(
+ "UPDATE deep_scan_workers SET kind = 'dedup', merge_state = 'none' WHERE scan_id = ?",
+ (scan.scan_id,),
+ )
+ current = stage_publication(
+ scan, generation=3, result_path=result_path, title="Selected aggregate"
+ )
+ stale = stage_publication(
+ scan, generation=2, result_path=result_path, title="Obsolete coordinator draft"
+ )
+ database_path = tmp_path / "publication.sqlite3"
+ with sqlite3.connect(database_path) as connection:
+ workbench_db.backup(connection)
+ connection.row_factory = sqlite3.Row
+ if boundary.startswith("sqlite-"):
+ workbench_api["saved_results"].write_scan_draft(
+ workbench_api["_WORKBENCH_DB_CONTEXT"], connection, current
+ )
+
+ child = subprocess.run(
+ [
+ sys.executable,
+ "-c",
+ _CRASH_PUBLICATION,
+ str(Path(__file__).resolve().parents[1] / "scripts" / "workbench_db.py"),
+ str(database_path),
+ json.dumps(vars(current)),
+ boundary,
+ ],
+ capture_output=True,
+ text=True,
+ )
+ assert child.returncode == {"sqlite-before": 72, "sqlite-after": 73}.get(boundary, 71), (
+ child.stdout,
+ child.stderr,
+ )
+ interrupted = {
+ path: path.read_bytes()
+ for path in scan.scan_dir.rglob("*")
+ if path.is_file() and "drafts" not in path.parts
+ }
+ with sqlite3.connect(database_path) as connection:
+ connection.row_factory = sqlite3.Row
+ connection.execute("PRAGMA foreign_keys = ON")
+ row = connection.execute("SELECT * FROM scans WHERE id = ?", (scan.scan_id,)).fetchone()
+ assert row["status"] == ("complete" if boundary == "sqlite-after" else "running")
+ assert bool(row["seal_manifest_digest"]) == (boundary == "sqlite-after")
+ run_before = dict(connection.execute("SELECT * FROM deep_scan_runs").fetchone())
+ workers_before = [
+ dict(row) for row in connection.execute("SELECT * FROM deep_scan_workers")
+ ]
+
+ with pytest.raises(SystemExit, match="coordinator|stopped"):
+ workbench_api["saved_results"].write_scan_draft(
+ workbench_api["_WORKBENCH_DB_CONTEXT"], connection, stale
+ )
+ assert all(path.read_bytes() == contents for path, contents in interrupted.items())
+
+ if not boundary.startswith("sqlite-"):
+ workbench_api["saved_results"].write_scan_draft(
+ workbench_api["_WORKBENCH_DB_CONTEXT"], connection, current
+ )
+ completed = workbench_api["complete_scan"](
+ connection, Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None)
+ )["scan"]
+ assert completed["progress"]["status"] == "complete"
+ assert completed["findingCount"] == 1
+ assert dict(connection.execute("SELECT * FROM deep_scan_runs").fetchone()) == run_before
+ assert [
+ dict(row) for row in connection.execute("SELECT * FROM deep_scan_workers")
+ ] == workers_before
+ assert connection.execute("SELECT COUNT(*) FROM finding_occurrences").fetchone()[0] == 1
+ published = {
+ path: path.read_bytes()
+ for path in scan.scan_dir.rglob("*")
+ if path.is_file() and "drafts" not in path.parts
+ }
+ workbench_api["complete_scan"](
+ connection, Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None)
+ )
+ assert all(path.read_bytes() == contents for path, contents in published.items())
+ if boundary.startswith("sqlite-"):
+ assert published == interrupted
+ findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]
+ assert findings[0]["title"] == "Selected aggregate"
diff --git a/plugins/codex-security/tests/test_deep_scan_successful_publication.py b/plugins/codex-security/tests/test_deep_scan_successful_publication.py
index 593f0155b..66bcd3c79 100644
--- a/plugins/codex-security/tests/test_deep_scan_successful_publication.py
+++ b/plugins/codex-security/tests/test_deep_scan_successful_publication.py
@@ -76,7 +76,7 @@ def publication_scan(workbench_api, workbench_db, tmp_path, monkeypatch):
"INSERT INTO deep_scan_runs (scan_id, schema_version, workflow_version, "
"status, phase, workers, subagents, stop_after_no_new, max_discovery_runs, "
"manifest_path, terminal_reason, created_at, updated_at, completed_at) "
- "VALUES (?, 1, 'publication-test', 'succeeded', 'terminal', 1, 0, 1, 1, "
+ "VALUES (?, 1, 'deep-security-scan/v1', 'succeeded', 'terminal', 1, 0, 1, 1, "
"?, 'saturated', ?, ?, ?)",
(
scan_id,
@@ -541,6 +541,35 @@ def test_standard_publication_preserves_deliberately_partial_coverage(
assert_published_aggregate(scan)
+def test_deep_publication_write_failure_keeps_original_terminal_cause(
+ workbench_api, workbench_db, publication_scan, monkeypatch
+):
+ scan = publication_scan()
+ finalizer_globals = workbench_api["_write_prepared_scan_finalization"].__globals__
+ write_bytes = finalizer_globals["write_scan_local_bytes"]
+
+ def fail_report(scan_dir, relative_path, payload, **kwargs):
+ if relative_path == "report.md":
+ raise finalizer_globals["ContractError"]("Synthetic report write interruption")
+ return write_bytes(scan_dir, relative_path, payload, **kwargs)
+
+ with monkeypatch.context() as patch:
+ patch.setitem(finalizer_globals, "write_scan_local_bytes", fail_report)
+ with pytest.raises(SystemExit, match="Synthetic report write interruption"):
+ complete(workbench_api, workbench_db, scan)
+
+ assert (
+ workbench_db.execute("SELECT status FROM scans WHERE id = ?", (scan.scan_id,)).fetchone()[0]
+ == "running"
+ )
+ run = workbench_db.execute(
+ "SELECT status, terminal_reason FROM deep_scan_runs WHERE scan_id = ?", (scan.scan_id,)
+ ).fetchone()
+ assert tuple(run) == ("succeeded", "saturated")
+ assert complete(workbench_api, workbench_db, scan)["progress"]["status"] == "complete"
+ assert_published_aggregate(scan)
+
+
@pytest.mark.parametrize("resumed", [False, True])
def test_scoped_deep_completion_filters_old_parent_and_preserves_accepted_sources(
workbench_api, workbench_db, publication_scan, resumed
diff --git a/plugins/codex-security/tests/test_workbench_draft_publication.py b/plugins/codex-security/tests/test_workbench_draft_publication.py
index 47a4bae1c..0b731d380 100644
--- a/plugins/codex-security/tests/test_workbench_draft_publication.py
+++ b/plugins/codex-security/tests/test_workbench_draft_publication.py
@@ -91,3 +91,104 @@ def test_terminal_deep_draft_compares_previous_bytes_without_parsing_review_docu
checkpoint = json.loads((scan_dir / "checkpoints" / head["checkpoint"]).read_text())
assert checkpoint["threatModel"] == model
assert (scan_dir / "threatmodel.md").read_text().startswith(model["content"])
+
+
[email protected]("initialized", [False, True])
[email protected]("complete", [False, True])
+def test_deep_setup_draft_before_orchestration(
+ tmp_path: Path, initialized: bool, complete: bool
+) -> None:
+ if initialized:
+ state_dir, codex_home, target, scan_dir, scan_id = deep_scan_fixture(tmp_path)
+ else:
+ state_dir, codex_home = tmp_path / "state", tmp_path / "codex-home"
+ target, scan_dir = tmp_path / "target", tmp_path / "scan"
+ target.mkdir()
+ (target / "app.py").write_text("value = request.args['value']\n")
+ scan_dir.mkdir(mode=0o700)
+ registered = run_workbench(
+ state_dir,
+ "register-cli-scan",
+ "--scan-dir",
+ str(scan_dir),
+ "--repository",
+ str(target),
+ "--recipe-json",
+ json.dumps(
+ {
+ "config": {},
+ "mode": "deep",
+ "repository": str(target),
+ "target": {"kind": "repository", "paths": []},
+ }
+ ),
+ )
+ scan_id = str(registered["scanId"])
+ contract_dir = tmp_path / "contract"
+ contract_dir.mkdir()
+ write_completed_contract(
+ contract_dir, scan_id, target, relative_path="app.py", coverage_mode="deep_repository"
+ )
+ documents = {
+ key: json.loads((contract_dir / name).read_text())
+ for key, name in (
+ ("manifest", "scan-manifest.json"),
+ ("findings", "findings.json"),
+ ("coverage", "coverage.json"),
+ )
+ }
+ model = {"format": "markdown", "content": "# Supplied setup model\n"}
+ documents["manifest"] = {
+ "scan": {
+ **{key: documents["manifest"]["scan"][key] for key in ("target", "scope")},
+ "threatModel": model,
+ **({} if complete else {"complete": False}),
+ }
+ }
+ documents["findings"]["findings"] = []
+ documents["coverage"].update(completeness="complete", surfaces=[], deferred=[])
+ drafts = scan_dir / "drafts"
+ drafts.mkdir(exist_ok=True)
+ staged = drafts / f"{uuid.uuid4()}.json"
+ staged.write_text(json.dumps(documents))
+ args = ("write-scan-draft", "--scan-id", scan_id, "--draft-path", str(staged))
+ environment = {"CODEX_HOME": str(codex_home)}
+ if complete and not initialized:
+ rejected = run_workbench(state_dir, *args, environment=environment, check=False)
+ assert rejected["returncode"] != 0
+ assert "orchestration state not found" in str(rejected["stderr"])
+ assert not (scan_dir / "checkpoint-head.json").exists()
+ return
+ result = run_workbench(state_dir, *args, environment=environment)
+ assert result["status"] == "draft_written"
+ head = json.loads((scan_dir / "checkpoint-head.json").read_text())
+ checkpoint = json.loads((scan_dir / "checkpoints" / head["checkpoint"]).read_text())
+ assert checkpoint["threatModel"] == model
+ assert (checkpoint.get("complete") is not False) is complete
+ assert json.loads((scan_dir / "scan-manifest.json").read_text())["scan"]["threatModel"] == model
+ assert (scan_dir / "threatmodel.md").read_text().startswith(model["content"])
+ if initialized and complete:
+ selected = {
+ name: (scan_dir / name).read_bytes()
+ for name in (
+ "scan-manifest.json",
+ "findings.json",
+ "coverage.json",
+ "checkpoint-head.json",
+ "threatmodel.md",
+ )
+ }
+ checkpoints = set((scan_dir / "checkpoints").iterdir())
+ documents["manifest"]["scan"].update(
+ complete=False, threatModel={"summary": "Unselected progress"}
+ )
+ staged.write_text(json.dumps(documents))
+ assert run_workbench(state_dir, *args, environment=environment)["status"] == "draft_written"
+ assert all(
+ (scan_dir / name).read_bytes() == contents for name, contents in selected.items()
+ )
+ added = set((scan_dir / "checkpoints").iterdir()) - checkpoints
+ assert len(added) == 1
+ retained = json.loads(added.pop().read_text())
+ assert retained["complete"] is False
+ assert retained["threatModel"] == {"summary": "Unselected progress"}