OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix(deep-scan): include accepted worker coverage in reports (#1593)
Michael D'Angelo9cc1a8728fd7
diff --git a/evals/tsconfig.json b/evals/tsconfig.jsonindex 52d5541c7..354d2a787 100644--- a/evals/tsconfig.json+++ b/evals/tsconfig.json@@ -4,6 +4,7 @@"typeRoots": ["../sdk/typescript/node_modules/@types"]},"include": [+ "../plugins/codex-security/mcp-app/src/assets.d.ts","deep-reducer/*.mts","secret-discovery/*.mts","workflow-routing/*.mts",diff --git a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts b/plugins/codex-security/mcp-app/src/artifact-scan-draft.tsindex d8d2c52b6..79e25f089 100644--- a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts+++ b/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts@@ -1,4 +1,8 @@import type { JsonObject } from "./types.js";+import {+ coverageReceiptComponents,+ validateRetainedCoverage,+} from "./coverage-publication.js";import { isRecord as isObject } from "./record.js";import { createHash, randomUUID } from "node:crypto";import { promises as fs } from "node:fs";@@ -9,6 +13,7 @@ import type { ArtifactContext } from "./artifact-context.js";import type { RunArtifactWorkbench } from "./artifact-context.js";import {artifactDestination,+ artifactSourcePath,readArtifactJsonObject,readArtifactText,readArtifactTextWithMetadata,@@ -198,6 +203,10 @@ export async function recordCodexSecurityScanDraftViaWorkbench(fs.rm(draftPath, { force: true }),]);}+ const warnings = [+ ...(preserved.warnings ?? []),+ ...(documentWarnings ?? []),+ ];return {scanId: reconciled.scanId,findingCount: findings.length,@@ -205,7 +214,7 @@ export async function recordCodexSecurityScanDraftViaWorkbench(coverage,operation: "replace",status: "draft_written",- ...(documentWarnings?.length ? { warnings: documentWarnings } : {}),+ ...(warnings.length ? { warnings } : {}),};}}@@ -227,6 +236,8 @@ export async function recordCodexSecurityWorkerScanDraft();}+ validateRetainedCoverage(parsed.coverage);+const includePaths = context.includePaths ?? [context.scope || "."];let scoped = {...parsed,@@ -236,7 +247,8 @@ export async function recordCodexSecurityWorkerScanDraft(context.filePaths,),};- scoped = (await preserveScanDraft(context, scoped)).input;+ const preserved = await preserveScanDraft(context, scoped);+ scoped = preserved.input;// Older checkpoints predate the coordinator's requested-path binding.scoped.findings = findingsWithinScope(scoped.findings,@@ -254,6 +266,10 @@ export async function recordCodexSecurityWorkerScanDraft(scoped.threatModel,);+ const warnings = [+ ...(preserved.warnings ?? []),+ ...(documentWarning === undefined ? [] : [documentWarning]),+ ];return {scanId: parsed.scanId,findingCount: scoped.findings.length,@@ -261,7 +277,7 @@ export async function recordCodexSecurityWorkerScanDraft(coverage: scoped.coverage,operation: "replace",status: "draft_written",- ...(documentWarning === undefined ? {} : { warnings: [documentWarning] }),+ ...(warnings.length === 0 ? {} : { warnings }),};}@@ -332,8 +348,13 @@ async function preserveScanDraft(context: ArtifactContext,input: ScanDraftInput,saveCheckpoint = true,-): Promise<{ input: ScanDraftInput; previousDigest: string }> {+): Promise<{+ input: ScanDraftInput;+ previousDigest: string;+ warnings?: string[];+}> {requireFiniteNumbers(input);+ const warnings: string[] = [];const currentCheckpointName = scanDraftCheckpointName(input);const requiresClosureValidation = resolvedDeferred(input.coverage).length > 0;if (saveCheckpoint && !requiresClosureValidation)@@ -362,6 +383,44 @@ async function preserveScanDraft(right.modifiedMs - left.modifiedMs ||Number(right.head ?? false) - Number(left.head ?? false),);+ const currentReceipts = new Set(+ current.flatMap(({ input }) =>+ input.coverage.surfaces.flatMap((row) =>+ Array.isArray(row.receiptRefs) ? row.receiptRefs : [],+ ),+ ),+ );+ const archivedReceipts = new Map<+ string,+ { context: ArtifactContext; components: string[] }+ >();+ for (const source of archived) {+ const attempt = source.attempt!;+ for (const row of source.input.coverage.surfaces as JsonObject[]) {+ if (!Array.isArray(row.receiptRefs)) continue;+ row.receiptRefs = (row.receiptRefs as string[]).map((ref) => {+ // Mapping saved references is data-only. Validate retained paths at I/O,+ // after a corrected same-ID surface can replace an invalid old receipt.+ const components = ref+ .split("/")+ .filter((part) => part !== "" && part !== ".");+ const retained = [+ "artifacts",+ "archived-receipts",+ attempt,+ ...components,+ ].join("/");+ archivedReceipts.set(retained, {+ context: {+ ...context,+ root: join(dirname(context.root), "attempts", attempt),+ },+ components,+ });+ return retained;+ });+ }+ }const savedSources = [...current, ...archived];const sources = savedSources.map(({ input }) => input);// Older checkpoints can omit IDs already assigned in their published output.@@ -510,6 +569,18 @@ async function preserveScanDraft(sources.unshift(progress);}}+ const selectedReceiptsBySurface = new Map<string, Set<string>>();+ for (const selected of [result, ...sources]) {+ const rows = (selected.coverage.surfaces as JsonObject[]).filter(+ (row) =>+ typeof row.id === "string" && !selectedReceiptsBySurface.has(row.id),+ );+ for (const row of rows)+ selectedReceiptsBySurface.set(+ row.id as string,+ new Set((row.receiptRefs as string[] | undefined) ?? []),+ );+ }const currentCandidateIds = completedCandidateIds(result);const resolvedCandidateIds = completedCandidateIds(result, sources);const { closedDeferredIds, resolvedSurfaces } = reconcileResolvedDeferred(@@ -671,8 +742,193 @@ async function preserveScanDraft(}result.coverage.deferred = normalizeDeferred(result.coverage.deferred);result.coverage.surfaces = normalizeSurfaces(result.coverage.surfaces);+ // Archived attempts move their receipt files with them. Copy only evidence+ // still retained by this draft into its current, independently bound output.+ // Superseded surfaces must not reserve a discarded receipt's spelling.+ for (const ref of new Set(+ result.coverage.surfaces.flatMap((row) => row.receiptRefs ?? []),+ )) {+ if (!currentReceipts.has(ref) || !archivedReceipts.has(ref)) continue;+ try {+ await artifactSourcePath(+ context,+ coverageReceiptComponents(ref),+ "saved coverage receipt",+ );+ archivedReceipts.delete(ref);+ } catch {+ // A saved reference may outlive its copied receipt. Keep its archive+ // origin for the normal copy/repair path, including destination checks.+ }+ }+ const retainedSelectedReceiptRefs = new Set(+ (result.coverage.surfaces as JsonObject[]).flatMap((row) =>+ ((row.receiptRefs as string[] | undefined) ?? []).filter((ref) =>+ selectedReceiptsBySurface.get(row.id as string)?.has(ref),+ ),+ ),+ );+ const receiptPaths = new Map<string, string>();+ for (const ref of retainedSelectedReceiptRefs) {+ const origin = archivedReceipts.get(ref);+ let components: string[];+ try {+ components = coverageReceiptComponents(ref);+ await artifactSourcePath(+ origin?.context ?? context,+ origin+ ? coverageReceiptComponents(origin.components.join("/"))+ : components,+ "selected coverage receipt",+ );+ } catch {+ // Unavailable evidence cannot win a spelling preference. The normal+ // copy/repair and discovery validation paths still report its error.+ continue;+ }+ const normalized = components.join("/");+ if (!receiptPaths.has(normalized.toLowerCase()))+ receiptPaths.set(normalized.toLowerCase(), normalized);+ }+ for (const row of result.coverage.surfaces as JsonObject[]) {+ for (const ref of (row.receiptRefs as string[] | undefined) ?? []) {+ const normalized = ref+ .split("/")+ .filter((part) => part !== "" && part !== ".")+ .join("/");+ const key = normalized.toLowerCase();+ const origin = archivedReceipts.get(ref);+ if (+ !origin &&+ selectedReceiptsBySurface.get(row.id as string)?.has(ref)+ ) {+ if (!receiptPaths.has(key)) receiptPaths.set(key, normalized);+ continue;+ }+ let source: string;+ try {+ const components = coverageReceiptComponents(+ origin ? origin.components.join("/") : ref,+ );+ coverageReceiptComponents(ref);+ const previous = receiptPaths.get(key);+ if (previous !== undefined && previous !== normalized)+ throw Object.assign(+ new Error(+ `coverage receipt: duplicate artifact path: ${previous} and ${normalized}`,+ ),+ { code: "invalid_coverage_receipt_path" },+ );+ source = await artifactSourcePath(+ origin?.context ?? context,+ components,+ origin ? "archived coverage receipt" : "saved coverage receipt",+ );+ } catch (error) {+ const closesSavedSurface = [...resolvedSurfaces].some((surface) =>+ (surface.receiptRefs as string[] | undefined)?.includes(ref),+ );+ const unchangedDisposition = sources.some((saved) =>+ (saved.coverage.surfaces as JsonObject[]).some(+ (surface) =>+ surface.id === row.id &&+ surface.disposition === row.disposition &&+ (surface.receiptRefs as string[] | undefined)?.includes(ref),+ ),+ );+ if (+ (!unchangedDisposition &&+ (closedDeferredIds.size === 0 || !closesSavedSurface)) ||+ !(error instanceof Error) ||+ !(+ error.message.includes("the requested artifact is unavailable") ||+ ("code" in error && error.code === "invalid_coverage_receipt_path")+ )+ ) {+ if (!origin) continue;+ throw error;+ }+ // Saved, validated closeouts remain authoritative on later writes and+ // fresh retries; the caller need not repeat their completed surfaces.+ let repaired = false;+ let replacementError: unknown = error;+ const firstBrokenSource = sources.findIndex((saved) =>+ (saved.coverage.surfaces as JsonObject[]).some(+ (surface) =>+ surface.id === row.id &&+ (surface.receiptRefs as string[] | undefined)?.includes(ref),+ ),+ );+ for (const candidate of [+ input,+ ...sources.slice(0, firstBrokenSource),+ ]) {+ if (+ candidate.complete === false ||+ (!unchangedDisposition &&+ !resolvedDeferred(candidate.coverage).some((closure) =>+ closedDeferredIds.has(closure.id as string),+ ))+ )+ continue;+ const replacement = (+ candidate.coverage.surfaces as JsonObject[]+ ).find(+ (surface) =>+ surface.id === row.id &&+ surface.disposition === row.disposition &&+ surface.disposition !== "needs_follow_up",+ );+ const refs = replacement?.receiptRefs as string[] | undefined;+ if (!refs?.length || refs.includes(ref)) continue;+ try {+ for (const replacementRef of refs) {+ const saved = archivedReceipts.get(replacementRef);+ await artifactSourcePath(+ saved?.context ?? context,+ coverageReceiptComponents(+ saved ? saved.components.join("/") : replacementRef,+ ),+ "replacement coverage receipt",+ );+ }+ repaired = true;+ break;+ } catch (failure) {+ replacementError = failure;+ }+ }+ if (!repaired) {+ // Current drafts may still be incomplete; ordinary discovery admission+ // diagnoses unrepaired refs as before. Archived repair keeps its errors.+ if (!origin) continue;+ throw replacementError;+ }+ row.receiptRefs = (row.receiptRefs as string[]).filter(+ (value) => value !== ref,+ );+ warnings.push(+ `Replaced ${error.message.includes("unavailable") ? "missing" : "invalid"} ${origin ? "archived" : "saved"} coverage receipt ${ref}: ${error.message}`,+ );+ continue;+ }+ if (origin) {+ const destination = await artifactDestination(+ context,+ coverageReceiptComponents(ref),+ "retained coverage receipt",+ );+ await fs.copyFile(source, destination);+ }+ receiptPaths.set(key, normalized);+ }+ }if (saveCheckpoint) await saveScanDraftCheckpoint(context, result);- return { input: result, previousDigest: previousState.digest };+ return {+ input: result,+ previousDigest: previousState.digest,+ ...(warnings.length ? { warnings } : {}),+ };}function completedCandidateIds(@@ -1247,7 +1503,11 @@ async function readPreviousScanDocuments(context: ArtifactContext) {async function preserveDeepThreatModel(context: ArtifactContext,input: ScanDraftInput,-): Promise<{ input: ScanDraftInput; previousDigest?: string }> {+): Promise<{+ input: ScanDraftInput;+ previousDigest?: string;+ warnings?: string[];+}> {if (input.threatModel !== undefined) return { input };const { contents, digest } = await readPreviousScanDocuments(context);const previous =diff --git a/plugins/codex-security/mcp-app/src/coverage-publication.ts b/plugins/codex-security/mcp-app/src/coverage-publication.tsnew file mode 100644index 000000000..48a1f9424--- /dev/null+++ b/plugins/codex-security/mcp-app/src/coverage-publication.ts@@ -0,0 +1,78 @@+import type { JsonObject } from "./types.js";++// Match finalize_scan_contract.py's existing retained-JSON and path contract.+// Repository paths are not subject to portable artifact filename restrictions.+const windowsUnsafeComponent =+ /[<>:"|?*\x00-\x1f]|[ .]$|^(?:con|prn|aux|nul|con[iİı]n\$|conout\$|com[1-9¹²³]|lpt[1-9¹²³])(?:\..*)?$/iu;++function requirePublicationJson(value: unknown, label: string): void {+ if (typeof value === "string") {+ if (!value.isWellFormed())+ throw new Error(`${label}: expected well-formed Unicode JSON strings`);+ } else if (typeof value === "number") {+ if (!Number.isFinite(value))+ throw new Error(`${label}: non-finite JSON numbers are not supported`);+ if (Number.isInteger(value) && !Number.isSafeInteger(value))+ throw new Error(+ `${label}: unsafe integer-valued JSON numbers are not supported`,+ );+ } else if (Array.isArray(value)) {+ value.forEach((item, index) =>+ requirePublicationJson(item, `${label}[${index}]`),+ );+ } else if (value && typeof value === "object") {+ for (const [key, item] of Object.entries(value)) {+ requirePublicationJson(key, label);+ requirePublicationJson(item, `${label}.<property>`);+ }+ }+}++function relativeComponents(+ value: string,+ label: string,+ allowDot: boolean,+): string[] {+ requirePublicationJson(value, label);+ const parts = value.split("/");+ const normalized = parts.filter((part) => part !== "" && part !== ".");+ if (+ /^[\p{White_Space}\x1c-\x1f]*$/u.test(value) ||+ (!allowDot && normalized.length === 0) ||+ /^\p{L}:/u.test(value) ||+ /[\\\x00-\x1f]/u.test(value) ||+ value.startsWith("/") ||+ parts.includes("..")+ )+ throw new Error(`${label}: expected a safe repository-relative POSIX path`);+ return normalized;+}++export function coverageReceiptComponents(value: string): string[] {+ const label = "coverage receipt";+ try {+ const parts = relativeComponents(value, label, false);+ if (parts.some((part) => windowsUnsafeComponent.test(part)))+ throw new Error(`${label}: expected a safe scan-relative POSIX path`);+ return parts;+ } catch (error) {+ if (error instanceof Error)+ Object.assign(error, { code: "invalid_coverage_receipt_path" });+ throw error;+ }+}++export function validateRetainedCoverage(coverage: JsonObject): void {+ for (const field of [+ "surfaces",+ "explicitExclusions",+ "deferred",+ "openQuestions",+ ] as const)+ requirePublicationJson(coverage[field], `coverage.${field}`);+ for (const [index, row] of (coverage.deferred as JsonObject[]).entries()) {+ if (Array.isArray(row.paths))+ for (const value of row.paths as string[])+ relativeComponents(value, `coverage.deferred[${index}].paths`, true);+ }+}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.tsindex b3a99b3ae..7ad6f7440 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,6 +1,12 @@import { isDeepStrictEqual } from "node:util";import { dirname, relative, sep } from "node:path";import type { JsonObject } from "../types.js";+import type { AcceptedDiscovery } from "./worker-runner.js";+import { artifactSourcePath } from "../artifact-io.js";+import {+ coverageReceiptComponents,+ validateRetainedCoverage,+} from "../coverage-publication.js";import { artifactSchemaDocuments } from "../artifact-contracts.js";import { projectArtifactToolSchema } from "../artifact-schema-loader.js";import { requireFiniteNumbers } from "../helpers/json.js";@@ -37,7 +43,7 @@ export interface ReducerArtifactValidation {/** Reduction combines findings; it does not complete the source workers' review. */export function aggregateDiscoveryCoverage(artifacts: DeepScanArtifacts,- discoveries: { id: string; resultPath: string; result: ScanDraftInput }[],+ discoveries: Pick<AcceptedDiscovery, "id" | "resultPath" | "result">[],): ScanDraftInput["coverage"] {const coverage: ScanDraftInput["coverage"] = {surfaces: [],@@ -55,29 +61,46 @@ export function aggregateDiscoveryCoverage("explicitExclusions","deferred",] as const) {- for (const [index, row] of (source[field] as JsonObject[]).entries()) {+ 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.- retained.id = scopedId(- typeof row.id === "string" ? row.id : String(index),- );- if (Array.isArray(row.surfaceIds)) {+ 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 (Array.isArray(row.receiptRefs)) {+ if (field === "surfaces" && Array.isArray(row.receiptRefs)) {retained.receiptRefs = (row.receiptRefs as string[]).map(- (ref) => `${prefix}/${ref}`,+ (ref) => `${prefix}/${coverageReceiptComponents(ref).join("/")}`,);}(coverage[field] as JsonObject[]).push(retained);}}- for (const question of source.openQuestions ?? []) {- (coverage.openQuestions ??= []).push({- ...(typeof question === "string" ? { question } : question),- workerId: worker.id,- });+ 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;@@ -106,6 +129,27 @@ export async function validateDiscoveryArtifacts(throw new Error("Standard scan worker wrote only a checkpoint; its audit is not complete.",);+ validateRetainedCoverage(result.coverage);+ const root = dirname(resultPath);+ const receiptPaths = new Map<string, string>();+ for (const row of result.coverage.surfaces as JsonObject[]) {+ for (const ref of (row.receiptRefs as string[] | undefined) ?? []) {+ const components = coverageReceiptComponents(ref);+ const normalized = components.join("/");+ const key = normalized.toLowerCase();+ const previous = receiptPaths.get(key);+ if (previous !== undefined && previous !== normalized)+ throw new Error(+ `Standard scan worker coverage receipt: duplicate artifact path: ${previous} and ${normalized}`,+ );+ receiptPaths.set(key, normalized);+ await artifactSourcePath(+ { root, repoRoot: root, layout: "worker" },+ components,+ "Standard scan worker coverage receipt",+ );+ }+ }return result;}diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft_recovery.ts b/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft_recovery.tsindex aefd1954e..14a0f2ebc 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft_recovery.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft_recovery.ts@@ -40,6 +40,65 @@ const findingFor = (candidateId: string) => ({provenance: { source: "local_plugin", candidateId },});+for (const layout of ["standard", "diff"] as const) {+ test(`${layout}: parent writes return receipt repairs and workbench warnings`, async (t) => {+ const f = await fixture(t, layout);+ await mkdir(path.join(f.root, "artifacts"));+ await writeFile(+ path.join(f.root, "artifacts/original.md"),+ "Original review.",+ );+ await writeFile(+ path.join(f.root, "artifacts/current.md"),+ "Current review.",+ );+ const surface = {+ id: "api",+ label: "API review",+ disposition: "needs_follow_up",+ receiptRefs: ["artifacts/original.md"],+ };+ await f.write(+ f.draft({+ surfaces: [surface],+ deferred: [+ { id: "review", reason: "Review remains.", surfaceIds: ["api"] },+ ],+ }),+ );+ await rm(path.join(f.root, "artifacts/original.md"));+ const result = await recordCodexSecurityScanDraftViaWorkbench(+ f.context,+ f.draft(+ {+ surfaces: [+ {+ ...surface,+ disposition: "no_issue_found",+ receiptRefs: ["artifacts/current.md"],+ },+ ],+ resolvedDeferred: [close("review")],+ },+ true,+ ),+ async (args: string[]) => {+ const staged = await readJson(args[args.indexOf("--draft-path") + 1]);+ assert.deepEqual(staged.coverage.surfaces[0].receiptRefs, [+ "artifacts/current.md",+ ]);+ return { warnings: ["Workbench publication warning."] };+ },+ );+ assert.equal(result.warnings?.length, 2);+ assert.match(+ result.warnings[0],+ /Replaced missing saved coverage receipt artifacts\/original.md/,+ );+ assert.equal(result.warnings[1], "Workbench publication warning.");+ });+}+for (const source of ["title", "candidateId", "reportId", "ledgerRowId"]) {for (const [value, expected] of [["_Unsafe archive_", "unsafe-archive_"],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.tsindex 45961b157..1a13b09cb 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@@ -2,19 +2,70 @@ import { readJson, writeJsonLine as writeResult } from "./support/json.ts";import { finding, scanId, workerDraft as draft } from "./scan-draft-fixture.ts";import { temporaryDirectory } from "./support/temporary-directories.ts";import assert from "node:assert/strict";-import { mkdir, readFile, rm, symlink, writeFile } from "node:fs/promises";+import {+ mkdir,+ readFile,+ readdir,+ utimes,+ rename,+ rm,+ symlink,+ writeFile,+} from "node:fs/promises";import path from "node:path";import { importSource } from "./import-module.ts";+import type { ScanDraftInput } from "../src/artifact-scan-draft.ts";+import type { JsonObject } from "../src/types.ts";-const { validateDiscoveryArtifacts, validateReducerArtifacts } =- await importSource(- path.join(import.meta.dirname, "../src/deep-scan/artifact-validation.ts"),- );+const {+ validateDiscoveryArtifacts,+ validateReducerArtifacts,+ aggregateDiscoveryCoverage,+} = await importSource(+ path.join(import.meta.dirname, "../src/deep-scan/artifact-validation.ts"),+);++const {+ recordCodexSecurityWorkerScanDraft,+ parseScanDraft,+ saveScanDraftCheckpoint,+} = await importSource(+ path.join(import.meta.dirname, "../src/artifact-scan-draft.ts"),+);const otherScanId = "12c17317-9594-49e0-b06a-d72fd7e14bba";const root = await temporaryDirectory("deep-scan-artifact-validation-", true);try {await testDiscoveryValidation(root);+ await testDiscoveryReceipts(root);+ for (const includeValidCheckpoint of [false, true])+ await testArchivedReceiptPathValidation(root, includeValidCheckpoint);+ await testCoveragePublicationParity(root);+ await testWorkerPublicationBeforeCheckpoint(root);+ await testLegacyDeferredPublication(root);+ await testMissingReceiptRepair(root);+ for (const reverse of [false, true])+ await testMissingReceiptRepair(root, { shared: true, reverse });+ for (const complete of [false, true])+ await testMissingReceiptRepair(root, { archived: false, complete });+ for (const complete of [false, true])+ for (const reverse of [false, true])+ await testMissingReceiptRepair(root, {+ archived: false,+ shared: true,+ complete,+ reverse,+ });+ await testSharedMissingReceiptRepairHistory(root);+ for (const resolution of [+ "generic",+ "candidate",+ "linked-candidate",+ ] as const)+ await testCurrentReceiptSelectionBeforeArchivedHistory(root, resolution);+ for (const candidate of [false, true])+ for (const reverse of [false, true])+ await testCurrentAnonymousReceiptSelection(root, candidate, reverse);await testReducerValidation(root);await testEmptyDiscoveryAndReduction(root);} finally {@@ -23,6 +74,676 @@ try {console.log("deep scan artifact validation tests passed");+async function testDiscoveryReceipts(root: string) {+ const result = draft([], {+ coverage: {+ surfaces: [+ {+ id: "api",+ label: "API",+ disposition: "no_issue_found",+ receiptRefs: ["artifacts/review.md"],+ },+ ],+ explicitExclusions: [],+ deferred: [],+ },+ });+ const worker = await createWorker(+ path.join(root, "receipts"),+ "worker-001",+ result,+ );+ const output = path.dirname(worker.resultPath);+ const context = {+ root: output,+ repoRoot: root,+ layout: "worker" as const,+ scanId,+ };+ await assert.rejects(+ validateDiscoveryArtifacts(worker.artifacts, worker.resultPath, scanId),+ /coverage receipt.*unavailable/,+ "missing evidence must fail admission before parent publication",+ );+ await mkdir(path.join(output, "artifacts"));+ const receipt = path.join(output, "artifacts/review.md");+ await writeFile(receipt, "Original attempt review evidence.\n");+ await validateDiscoveryArtifacts(worker.artifacts, worker.resultPath, scanId);+ await rm(receipt);+ await symlink(worker.resultPath, receipt);+ await assert.rejects(+ validateDiscoveryArtifacts(worker.artifacts, worker.resultPath, scanId),+ /safe regular file/,+ );+ await rm(receipt);+ await writeFile(receipt, "Original attempt review evidence.\n");+ await recordCodexSecurityWorkerScanDraft(context, {+ ...result,+ complete: false,+ });+ const archived = path.join(path.dirname(output), "attempts", "attempt-01");+ await mkdir(path.dirname(archived));+ await rename(output, archived);+ await mkdir(output);+ await recordCodexSecurityWorkerScanDraft(context, {+ ...draft([]),+ complete: true,+ });+ const accepted = await validateDiscoveryArtifacts(+ worker.artifacts,+ worker.resultPath,+ scanId,+ );+ const [retained] = accepted.coverage.surfaces;+ assert.equal(retained.id, "api");+ assert.deepEqual(retained.receiptRefs, [+ "artifacts/archived-receipts/attempt-01/artifacts/review.md",+ ]);+ assert.equal(+ await readFile(path.join(output, retained.receiptRefs[0]), "utf8"),+ "Original attempt review evidence.\n",+ );+ assert.equal(+ await readFile(path.join(archived, "artifacts/review.md"), "utf8"),+ "Original attempt review evidence.\n",+ );+ await rename(output, path.join(path.dirname(archived), "attempt-02"));+ await mkdir(output);+ await recordCodexSecurityWorkerScanDraft(context, {+ ...draft([]),+ complete: true,+ });+ const retried = await validateDiscoveryArtifacts(+ worker.artifacts,+ worker.resultPath,+ scanId,+ );+ assert.equal(retried.coverage.surfaces.length, 1);+ for (const ref of retried.coverage.surfaces[0].receiptRefs) {+ assert.equal(+ await readFile(path.join(output, ref), "utf8"),+ "Original attempt review evidence.\n",+ );+ }+}++async function testLegacyDeferredPublication(root: string) {+ const tasks = [+ { id: "review", reason: "Review authentication." },+ { id: "review", reason: "Review authorization." },+ { id: "review-2", reason: "Review routing." },+ ];+ const result = draft([], {+ coverage: {+ surfaces: [],+ explicitExclusions: [],+ deferred: tasks,+ },+ });+ const worker = await createWorker(+ path.join(root, "legacy-tasks"),+ "first",+ result,+ );+ const accepted = await validateDiscoveryArtifacts(+ worker.artifacts,+ worker.resultPath,+ scanId,+ );+ const coverage = aggregateDiscoveryCoverage(worker.artifacts, [+ { ...worker, result: accepted },+ ]);+ const parent = parseScanDraft({ ...result, coverage });+ assert.deepEqual(+ parent.coverage.deferred.map((row: { id: string }) => row.id),+ ["first/review", "first/review-3", "first/review-2"],+ );+ assert.deepEqual(+ parent.coverage.deferred.map((row: { reason: string }) => row.reason),+ tasks.map((row) => row.reason),+ );+}++async function testArchivedReceiptPathValidation(+ root: string,+ includeValidCheckpoint: boolean,+) {+ const surface = {+ id: "api",+ label: "API",+ disposition: "no_issue_found",+ receiptRefs: ["/artifacts/review.md"],+ };+ const result = draft([], {+ coverage: {+ surfaces: [surface],+ explicitExclusions: [],+ deferred: [],+ },+ });+ const worker = await createWorker(+ path.join(root, `archived-absolute-${includeValidCheckpoint}`),+ "first",+ result,+ );+ const output = path.dirname(worker.resultPath);+ await mkdir(path.join(output, "artifacts"));+ await writeFile(+ path.join(output, "artifacts/review.md"),+ "Original review.\n",+ );+ const archive = path.join(path.dirname(output), "attempts/attempt-01");+ await mkdir(path.dirname(archive));+ await rename(output, archive);+ const archivedBytes = await readFile(path.join(archive, "result.json"));+ await mkdir(path.join(output, "artifacts"), { recursive: true });+ const context = {+ root: output,+ repoRoot: root,+ layout: "worker" as const,+ scanId,+ };+ if (includeValidCheckpoint)+ await saveScanDraftCheckpoint(+ { ...context, root: archive },+ {+ ...result,+ complete: false,+ coverage: {+ ...result.coverage,+ surfaces: [+ {+ ...surface,+ receiptRefs: ["artifacts/review.md"],+ },+ ],+ },+ },+ false,+ );+ await recordCodexSecurityWorkerScanDraft(+ context,+ draft([], { complete: true }),+ );+ const accepted = await validateDiscoveryArtifacts(+ worker.artifacts,+ worker.resultPath,+ scanId,+ );+ assert.equal(+ accepted.coverage.surfaces.length,+ includeValidCheckpoint ? 1 : 0,+ );+ if (includeValidCheckpoint) {+ const refs = accepted.coverage.surfaces[0].receiptRefs;+ assert.deepEqual(refs, [+ "artifacts/archived-receipts/attempt-01/artifacts/review.md",+ ]);+ assert.equal(+ await readFile(path.join(output, refs[0]), "utf8"),+ "Original review.\n",+ );+ } else {+ assert.deepEqual(await readdir(path.join(output, "artifacts")), []);+ }+ assert.deepEqual(+ await readFile(path.join(archive, "result.json")),+ archivedBytes,+ );+}++async function testMissingReceiptRepair(+ root: string,+ {+ shared = false,+ reverse = false,+ archived = true,+ complete = true,+ }: {+ shared?: boolean;+ reverse?: boolean;+ archived?: boolean;+ complete?: boolean;+ } = {},+) {+ const missing = shared ? "artifacts/review.md" : "artifacts/missing.md";+ const existing = shared ? "artifacts/Review.md" : "artifacts/existing.md";+ const surfaces: JsonObject[] = [+ {+ id: "api",+ label: "API",+ disposition: "needs_follow_up",+ receiptRefs: reverse ? [existing, missing] : [missing, existing],+ },+ ...(shared+ ? [+ {+ id: "candidate",+ candidateId: "candidate",+ label: "Candidate",+ disposition: "needs_follow_up",+ receiptRefs: [missing],+ },+ ]+ : []),+ ];+ if (reverse) surfaces.reverse();+ const result = draft([], {+ complete,+ coverage: {+ explicitExclusions: [],+ surfaces,+ deferred: [+ { id: "review", reason: "Review pending.", surfaceIds: ["api"] },+ ...(shared+ ? [+ {+ id: "candidate",+ candidateId: "candidate",+ reason: "Validate candidate.",+ },+ ]+ : []),+ ],+ },+ });+ const worker = await createWorker(+ path.join(root, `repair-${shared}-${reverse}-${archived}-${complete}`),+ "first",+ result,+ );+ const output = path.dirname(worker.resultPath);+ const context = {+ root: output,+ repoRoot: root,+ layout: "worker" as const,+ scanId,+ };+ await mkdir(path.join(output, "artifacts"));+ await writeFile(path.join(output, existing), "Original valid evidence.\n");+ if (+ shared &&+ (await readFile(path.join(output, missing)).then(+ () => true,+ (error: NodeJS.ErrnoException) => {+ if (error.code === "ENOENT") return false;+ throw error;+ },+ ))+ )+ return; // A missing case variant requires a case-sensitive filesystem.+ await recordCodexSecurityWorkerScanDraft(context, result);+ await assert.rejects(+ validateDiscoveryArtifacts(worker.artifacts, worker.resultPath, scanId),+ complete ? /coverage receipt.*unavailable/ : /only a checkpoint/,+ );+ const archive = path.join(path.dirname(output), "attempts/attempt-01");+ await mkdir(path.dirname(archive));+ const history = archived ? archive : output;+ if (archived) {+ await rename(output, archive);+ await mkdir(path.join(output, "artifacts"), { recursive: true });+ }+ const checkpoints = await readdir(path.join(history, "checkpoints"));+ const checkpointBytes = await Promise.all(+ checkpoints.map((name) =>+ readFile(path.join(history, "checkpoints", name), "utf8"),+ ),+ );+ const replacementDraft = draft(+ shared+ ? [+ {+ ...finding("candidate", "src/fixture.py"),+ provenance: { source: "local_plugin", candidateId: "candidate" },+ },+ ]+ : [],+ {+ complete: true,+ coverage: {+ explicitExclusions: [],+ deferred: [],+ surfaces: [+ {+ id: "api",+ label: "API",+ disposition: "no_issue_found",+ receiptRefs: ["artifacts/replacement.md"],+ },+ ],+ resolvedDeferred: [+ { id: "review", reason: "Source review completed." },+ ],+ },+ },+ );+ if (archived)+ await assert.rejects(+ recordCodexSecurityWorkerScanDraft(context, replacementDraft),+ /replacement coverage receipt.*unavailable/,+ );+ await writeFile(+ path.join(output, "artifacts/replacement.md"),+ "Corrected review evidence.\n",+ );+ if (!archived && !complete) {+ await recordCodexSecurityWorkerScanDraft(+ context,+ draft([], {+ complete: false,+ coverage: {+ ...result.coverage,+ surfaces: [+ { ...surfaces[0], receiptRefs: ["artifacts/replacement.md"] },+ ],+ },+ }),+ );+ }+ const repaired = await recordCodexSecurityWorkerScanDraft(+ context,+ replacementDraft,+ );+ const accepted = await validateDiscoveryArtifacts(+ worker.artifacts,+ worker.resultPath,+ scanId,+ );+ assert.match(+ repaired.warnings.join("\n"),+ shared+ ? /Replaced (missing|invalid) (archived|saved) coverage receipt /+ : archived+ ? /Replaced missing archived coverage receipt /+ : /Replaced missing saved coverage receipt /,+ );+ assert.deepEqual(accepted.coverage.deferred, []);+ assert.equal(accepted.findings.length, shared ? 1 : 0);+ const receipts = accepted.coverage.surfaces[0].receiptRefs;+ assert.equal(receipts.length, 2);+ assert.deepEqual(+ new Set(+ await Promise.all(+ receipts.map((ref: string) => readFile(path.join(output, ref), "utf8")),+ ),+ ),+ new Set(["Original valid evidence.\n", "Corrected review evidence.\n"]),+ );+ assert.equal(+ await readFile(path.join(history, existing), "utf8"),+ "Original valid evidence.\n",+ );+ assert.deepEqual(+ await Promise.all(+ checkpoints.map((name) =>+ readFile(path.join(history, "checkpoints", name), "utf8"),+ ),+ ),+ checkpointBytes,+ );+ // The saved closeout remains valid when later writes omit unchanged work.+ for (const complete of [false, true]) {+ await recordCodexSecurityWorkerScanDraft(context, draft([], { complete }));+ }+ await rename(output, path.join(path.dirname(archive), "attempt-02"));+ await mkdir(output);+ await recordCodexSecurityWorkerScanDraft(+ context,+ draft([], { complete: true }),+ );+ const retried = await validateDiscoveryArtifacts(+ worker.artifacts,+ worker.resultPath,+ scanId,+ );+ assert.deepEqual(retried.coverage.deferred, []);+ assert.equal(retried.coverage.surfaces[0].disposition, "no_issue_found");+ assert.deepEqual(+ new Set(+ await Promise.all(+ retried.coverage.surfaces[0].receiptRefs.map((ref: string) =>+ readFile(path.join(output, ref), "utf8"),+ ),+ ),+ ),+ new Set(["Original valid evidence.\n", "Corrected review evidence.\n"]),+ );+}++async function testWorkerPublicationBeforeCheckpoint(root: string) {+ for (const complete of [false, true]) {+ for (const samples of [Number.MAX_SAFE_INTEGER + 1, "\ud800"]) {+ const workersRoot = path.join(+ root,+ `metadata-${complete}-${typeof samples}`,+ );+ const workerRoot = path.join(workersRoot, "first");+ const output = path.join(workerRoot, "output");+ await mkdir(output, { recursive: true });+ const context = {+ root: output,+ repoRoot: root,+ layout: "worker" as const,+ scanId,+ };+ const exclusion = {+ pattern: "vendor/**",+ reason: "External code.",+ metrics: { samples },+ };+ const input = draft([finding("preserved", "fixture.py")], {+ complete,+ coverage: {+ surfaces: [],+ explicitExclusions: [exclusion],+ deferred: [],+ },+ });+ await assert.rejects(+ recordCodexSecurityWorkerScanDraft(context, input),+ /unsafe integer|well-formed Unicode/,+ );+ assert.deepEqual(+ await readdir(output),+ [],+ "invalid metadata must not create a checkpoint or result",+ );+ const corrected = {+ ...input,+ complete: false,+ coverage: {+ ...input.coverage,+ explicitExclusions: [{ ...exclusion, metrics: { samples: 1 } }],+ },+ };+ await recordCodexSecurityWorkerScanDraft(context, corrected);+ const checkpoint = await readFile(+ path.join(output, "checkpoint-head.json"),+ "utf8",+ );+ const files = await readdir(output, { recursive: true });+ await assert.rejects(+ recordCodexSecurityWorkerScanDraft(context, input),+ /unsafe integer|well-formed Unicode/,+ );+ assert.equal(+ await readFile(path.join(output, "checkpoint-head.json"), "utf8"),+ checkpoint,+ );+ assert.deepEqual(await readdir(output, { recursive: true }), files);+ const archived = path.join(workerRoot, "attempts", "attempt-01");+ await mkdir(path.dirname(archived));+ await rename(output, archived);+ await mkdir(output);+ await recordCodexSecurityWorkerScanDraft(context, {+ ...corrected,+ findings: [],+ complete: true,+ });+ const accepted = await validateDiscoveryArtifacts(+ { workersRoot },+ path.join(output, "result.json"),+ scanId,+ );+ assert.equal(accepted.findings.length, 1);+ assert.deepEqual(+ accepted.coverage.explicitExclusions,+ corrected.coverage.explicitExclusions,+ );+ }+ }+}++async function testCoveragePublicationParity(root: string) {+ const original = draft([], {+ coverage: {+ explicitExclusions: [],+ deferred: [],+ surfaces: [+ {+ id: "api",+ label: "API",+ disposition: "no_issue_found",+ receiptRefs: ["artifacts/review.md"],+ },+ ],+ },+ }) as ScanDraftInput & {+ coverage: {+ surfaces: JsonObject[];+ deferred: JsonObject[];+ explicitExclusions: JsonObject[];+ };+ };+ const worker = await createWorker(+ path.join(root, "publication-parity"),+ "first",+ original,+ );+ const output = path.dirname(worker.resultPath);+ await mkdir(path.join(output, "artifacts"));+ await writeFile(+ path.join(output, "artifacts/review.md"),+ "Review evidence.\n",+ );+ for (const ref of [+ "artifacts/review.md",+ "artifacts/./review.md",+ "artifacts//review.md",+ ]) {+ const result = structuredClone(original);+ result.coverage.surfaces[0].receiptRefs = [ref];+ result.coverage.deferred = [+ {+ id: "review",+ reason: "Review another route.",+ paths: [".", "./src//api.ts"],+ surfaceIds: ["api"],+ receiptRefs: { external: "opaque metadata" },+ },+ ];+ result.coverage.explicitExclusions = [+ {+ id: "external",+ pattern: "vendor/**",+ reason: "External code.",+ receiptRefs: ["opaque/missing.md"],+ surfaceIds: ["opaque-id"],+ },+ ];+ await writeResult(worker.resultPath, result);+ const accepted = await validateDiscoveryArtifacts(+ worker.artifacts,+ worker.resultPath,+ scanId,+ );+ const coverage = aggregateDiscoveryCoverage(worker.artifacts, [+ { ...worker, result: accepted },+ ]);+ assert.deepEqual(coverage.deferred[0].receiptRefs, {+ external: "opaque metadata",+ });+ assert.deepEqual(coverage.deferred[0].surfaceIds, ["first/api"]);+ assert.deepEqual(coverage.explicitExclusions[0].receiptRefs, [+ "opaque/missing.md",+ ]);+ assert.deepEqual(coverage.explicitExclusions[0].surfaceIds, ["opaque-id"]);+ }+ const cases: { patch: (result: typeof original) => void; error: RegExp }[] = [+ {+ patch: (result) => {+ result.coverage.deferred = [+ { reason: "Review route.", paths: ["/repository/src/api.ts"] },+ ];+ },+ error: /safe repository-relative/,+ },+ {+ patch: (result) => {+ result.coverage.surfaces[0].receiptRefs = ["artifacts/review?.md"];+ },+ error: /safe scan-relative/,+ },+ {+ patch: (result) => {+ result.coverage.surfaces[0].metrics = {+ samples: Number.MAX_SAFE_INTEGER + 1,+ };+ },+ error: /unsafe integer/,+ },+ {+ patch: (result) => {+ result.coverage.openQuestions = [+ { question: "Review \ud800 deployment?" },+ ];+ },+ error: /well-formed Unicode/,+ },+ ];+ for (const ref of ["artifacts/conİn$", "artifacts/conın$"]) {+ cases.push({+ patch: (result) => {+ result.coverage.surfaces[0].receiptRefs = [ref];+ },+ error: /safe scan-relative/,+ });+ }+ cases.push({+ patch: (result) => {+ result.coverage.deferred = [+ { reason: "Review route.", paths: ["\u0085"] },+ ];+ },+ // Either validation layer can reject the whitespace-only path first.+ error: /safe repository-relative|Invalid string: must match pattern/,+ });+ await writeFile(+ path.join(output, "artifacts/Review.md"),+ "Other case-sensitive evidence.\n",+ );+ cases.push({+ patch: (result) => {+ result.coverage.surfaces[0].receiptRefs = [+ "artifacts/review.md",+ "artifacts/Review.md",+ ];+ },+ error: /duplicate artifact path/,+ });+ for (const { patch, error } of cases) {+ const result = structuredClone(original);+ patch(result);+ await writeResult(worker.resultPath, result);+ await assert.rejects(+ validateDiscoveryArtifacts(worker.artifacts, worker.resultPath, scanId),+ error,+ );+ }+}+async function testDiscoveryValidation(root: string) {const result = draft([finding("shared", "src/a.js")], {threatModel: { summary: "Requests reach shared code." },@@ -603,6 +1324,391 @@ async function createWorker(scanDir: string, id: string, result: unknown) {return { artifacts, id, resultPath };}+async function testSharedMissingReceiptRepairHistory(root: string) {+ const surface = (id: string, disposition: string, receipt: string) => ({+ id,+ label: id,+ disposition,+ receiptRefs: [receipt],+ });+ const task = (id: string) => ({+ id,+ reason: "Review remains.",+ surfaceIds: [id],+ });+ const close = (id: string) => ({ id, reason: "Review completed." });+ const makeDraft = (+ surfaces: JsonObject[],+ deferred: JsonObject[] = [],+ resolvedDeferred: JsonObject[] = [],+ ) =>+ draft([], {+ complete: true,+ coverage: {+ surfaces,+ deferred,+ resolvedDeferred,+ explicitExclusions: [],+ },+ });+ const broken = makeDraft(+ [surface("writer", "needs_follow_up", "artifacts/shared-missing.md")],+ [task("writer")],+ );+ const worker = await createWorker(+ path.join(root, "shared-receipt-history"),+ "first",+ broken,+ );+ const output = path.dirname(worker.resultPath);+ const archive = path.join(path.dirname(output), "attempts/attempt-01");+ await mkdir(path.dirname(archive));+ await rename(output, archive);+ await mkdir(path.join(archive, "artifacts"));+ await mkdir(path.join(output, "artifacts"), { recursive: true });+ const archivedContext = {+ root: archive,+ repoRoot: root,+ layout: "worker" as const,+ scanId,+ };+ const context = { ...archivedContext, root: output };+ await saveScanDraftCheckpoint(archivedContext, broken, false);+ const previousNames = await readdir(path.join(archive, "checkpoints"));+ for (const name of previousNames)+ await utimes(path.join(archive, "checkpoints", name), 100, 100);+ await writeFile(+ path.join(archive, "artifacts/writer.md"),+ "Previously completed writer review.\n",+ );+ const repaired = makeDraft(+ [surface("writer", "no_issue_found", "artifacts/writer.md")],+ [],+ [close("writer")],+ );+ await saveScanDraftCheckpoint(archivedContext, repaired, false);+ for (const name of await readdir(path.join(archive, "checkpoints"))) {+ if (!previousNames.includes(name))+ await utimes(path.join(archive, "checkpoints", name), 200, 200);+ }+ const latest = makeDraft(+ [+ surface("reader", "needs_follow_up", "artifacts/shared-missing.md"),+ surface("writer", "no_issue_found", "artifacts/writer.md"),+ ],+ [task("reader")],+ [close("writer")],+ );+ await writeResult(path.join(archive, "result.json"), latest);+ await utimes(path.join(archive, "result.json"), 300, 300);+ const original = await readFile(path.join(archive, "result.json"), "utf8");+ await writeFile(+ path.join(output, "artifacts/reader.md"),+ "Current reader review.\n",+ );+ await recordCodexSecurityWorkerScanDraft(+ context,+ makeDraft(+ [surface("reader", "no_issue_found", "artifacts/reader.md")],+ [],+ [close("reader")],+ ),+ );+ const accepted = await validateDiscoveryArtifacts(+ worker.artifacts,+ worker.resultPath,+ scanId,+ );+ assert.deepEqual(accepted.coverage.deferred, []);+ assert.equal(accepted.coverage.surfaces.length, 2);+ for (const row of accepted.coverage.surfaces) {+ assert.equal(row.disposition, "no_issue_found");+ assert.equal(row.receiptRefs.length, 1);+ const content = await readFile(+ path.join(output, row.receiptRefs[0]),+ "utf8",+ );+ assert.equal(+ content,+ row.id === "writer"+ ? "Previously completed writer review.\n"+ : "Current reader review.\n",+ );+ }+ const retainedWriter = accepted.coverage.surfaces.find(+ (row: JsonObject) => row.id === "writer",+ )!;+ const retainedPath = path.join(output, retainedWriter.receiptRefs[0]);+ await rm(retainedPath);+ await recordCodexSecurityWorkerScanDraft(context, accepted);+ await validateDiscoveryArtifacts(worker.artifacts, worker.resultPath, scanId);+ assert.equal(+ await readFile(retainedPath, "utf8"),+ "Previously completed writer review.\n",+ "a saved reference must restore a missing copy from its intact archive",+ );+ await writeFile(retainedPath, "Updated writer review.\n");+ await recordCodexSecurityWorkerScanDraft(context, accepted);+ assert.equal(+ await readFile(retainedPath, "utf8"),+ "Updated writer review.\n",+ );+ await rm(path.join(archive, "artifacts/writer.md"));+ await recordCodexSecurityWorkerScanDraft(context, accepted);+ assert.equal(+ await readFile(retainedPath, "utf8"),+ "Updated writer review.\n",+ );+ assert.equal(+ await readFile(path.join(archive, "result.json"), "utf8"),+ original,+ );+}++async function testCurrentAnonymousReceiptSelection(+ root: string,+ candidate: boolean,+ reverse: boolean,+) {+ const selectedRef = reverse ? "artifacts/Review.md" : "artifacts/review.md";+ const historicalRef = reverse ? "artifacts/review.md" : "artifacts/Review.md";+ const old = draft([], {+ complete: true,+ coverage: {+ explicitExclusions: [],+ surfaces: [+ {+ id: "writer",+ label: "Retained review",+ disposition: "needs_follow_up",+ receiptRefs: [historicalRef],+ },+ ],+ deferred: [+ { id: "review", reason: "Review remains.", surfaceIds: ["writer"] },+ ],+ },+ });+ const worker = await createWorker(+ path.join(root, `anonymous-receipt-${candidate}-${reverse}`),+ "first",+ old,+ );+ const output = path.dirname(worker.resultPath);+ await mkdir(path.join(output, "artifacts"));+ await writeFile(path.join(output, historicalRef), "Historical review.\n");+ await writeFile(+ path.join(output, selectedRef),+ "Current anonymous review.\n",+ );+ await writeFile(+ path.join(output, "artifacts/replacement.md"),+ "Replacement review.\n",+ );+ const originalReceipts = await Promise.all(+ [selectedRef, historicalRef].map((ref) => readFile(path.join(output, ref))),+ );+ const context = {+ root: output,+ repoRoot: root,+ layout: "worker" as const,+ scanId,+ };+ await saveScanDraftCheckpoint(context, old, false);+ const checkpoints = await readdir(path.join(output, "checkpoints"));+ const originals = await Promise.all(+ checkpoints.map((name) => readFile(path.join(output, "checkpoints", name))),+ );+ const current = draft([], {+ complete: true,+ coverage: {+ explicitExclusions: [],+ deferred: [],+ resolvedDeferred: [{ id: "review", reason: "Review completed." }],+ surfaces: [+ {+ id: "writer",+ label: "Retained review",+ disposition: "no_issue_found",+ receiptRefs: ["artifacts/replacement.md"],+ },+ {+ ...(candidate ? { candidateId: "anonymous" } : {}),+ label: "Current anonymous review",+ disposition: "no_issue_found",+ receiptRefs: [selectedRef],+ },+ ],+ },+ });+ for (let attempt = 0; attempt < 2; attempt++) {+ await recordCodexSecurityWorkerScanDraft(context, current);+ const accepted = await validateDiscoveryArtifacts(+ worker.artifacts,+ worker.resultPath,+ scanId,+ );+ assert.equal(accepted.coverage.surfaces.length, 2);+ const selected = accepted.coverage.surfaces.find(+ (row: JsonObject) => row.label === "Current anonymous review",+ );+ assert.deepEqual(selected.receiptRefs, [selectedRef]);+ assert.equal(selected.disposition, "no_issue_found");+ const writer = accepted.coverage.surfaces.find(+ (row: JsonObject) => row.id === "writer",+ );+ assert.deepEqual(writer.receiptRefs, ["artifacts/replacement.md"]);+ }+ assert.deepEqual(+ await Promise.all(+ checkpoints.map((name) =>+ readFile(path.join(output, "checkpoints", name)),+ ),+ ),+ originals,+ );+ assert.deepEqual(+ await Promise.all(+ [selectedRef, historicalRef].map((ref) =>+ readFile(path.join(output, ref)),+ ),+ ),+ originalReceipts,+ );+}++async function testCurrentReceiptSelectionBeforeArchivedHistory(+ root: string,+ resolution: "generic" | "candidate" | "linked-candidate",+) {+ const candidate = resolution !== "generic";+ for (const repairId of ["reader", "writer"]) {+ for (const reverse of [false, true]) {+ const refs = {+ reader: "artifacts/review.md",+ writer: "artifacts/Review.md",+ };+ const keptId = repairId === "reader" ? "writer" : "reader";+ const old = draft([], {+ complete: true,+ coverage: {+ explicitExclusions: [],+ deferred: [+ {+ id: resolution === "candidate" ? repairId : "review",+ ...(candidate ? { candidateId: repairId } : {}),+ reason: "Review remains.",+ surfaceIds: [repairId],+ },+ ],+ surfaces: Object.entries(refs).map(([id, ref]) => ({+ id,+ label: id,+ disposition: id === repairId ? "needs_follow_up" : "no_issue_found",+ ...(candidate && id === repairId ? { candidateId: repairId } : {}),+ receiptRefs: [ref],+ })),+ },+ });+ if (reverse) old.coverage.surfaces.reverse();+ const worker = await createWorker(+ path.join(root, `selected-${repairId}-${reverse}-${resolution}`),+ "first",+ old,+ );+ const output = path.dirname(worker.resultPath);+ const archive = path.join(path.dirname(output), "attempts/attempt-01");+ await mkdir(path.dirname(archive));+ await rename(output, archive);+ await mkdir(path.join(archive, "artifacts"));+ for (const [id, ref] of Object.entries(refs))+ await writeFile(path.join(archive, ref), `Original ${id} review.\n`);+ const originals = await Promise.all(+ Object.values(refs).map((ref) =>+ readFile(path.join(archive, ref), "utf8"),+ ),+ );+ const oldResult = await readFile(+ path.join(archive, "result.json"),+ "utf8",+ );+ await mkdir(path.join(output, "artifacts"), { recursive: true });+ await writeFile(+ path.join(output, "artifacts/replacement.md"),+ "Corrected selected review.\n",+ );+ await recordCodexSecurityWorkerScanDraft(+ { root: output, repoRoot: root, layout: "worker", scanId },+ draft(+ candidate+ ? [+ {+ ...finding(repairId, "src/fixture.py"),+ provenance: { source: "local_plugin", candidateId: repairId },+ },+ ]+ : [],+ {+ complete: true,+ coverage: {+ explicitExclusions: [],+ deferred: [],+ surfaces: candidate+ ? []+ : [+ {+ id: repairId,+ label: repairId,+ disposition: "no_issue_found",+ receiptRefs: ["artifacts/replacement.md"],+ },+ ],+ resolvedDeferred: candidate+ ? []+ : [{ id: "review", reason: "Review completed." }],+ },+ },+ ),+ );+ const result = await validateDiscoveryArtifacts(+ worker.artifacts,+ worker.resultPath,+ scanId,+ );+ assert.deepEqual(result.coverage.deferred, []);+ assert.equal(result.coverage.surfaces.length, candidate ? 1 : 2);+ assert.equal(result.findings.length, candidate ? 1 : 0);+ const kept = result.coverage.surfaces.find(+ (row: JsonObject) => row.id === keptId,+ );+ assert.deepEqual(kept.receiptRefs, [+ `artifacts/archived-receipts/attempt-01/${refs[keptId]}`,+ ]);+ assert.equal(+ await readFile(path.join(output, kept.receiptRefs[0]), "utf8"),+ await readFile(path.join(archive, refs[keptId]), "utf8"),+ );+ const repaired = result.coverage.surfaces.find(+ (row: JsonObject) => row.id === repairId,+ );+ if (candidate) assert.equal(repaired, undefined);+ else assert.deepEqual(repaired.receiptRefs, ["artifacts/replacement.md"]);+ assert.equal(+ await readFile(path.join(archive, "result.json"), "utf8"),+ oldResult,+ );+ assert.deepEqual(+ await Promise.all(+ Object.values(refs).map((ref) =>+ readFile(path.join(archive, ref), "utf8"),+ ),+ ),+ originals,+ );+ }+ }+}+function reduction<Finding extends Record<string, unknown>>(findings: Finding[],extra = {},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.tsindex 44f10a6fb..839bf2211 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@@ -43,8 +43,13 @@ await testWorkerRetryThreadsRemainInScanUsage();await testReducerCommitAndFinishAgainstRealWorkbench();await testReducerCommitAndFinishAgainstRealWorkbench(true);await testExpiredDeadlineWithoutCompletedDiscoveryAgainstRealWorkbench();+await testPartialWorkerCoverageSurvivesParentCompletion();+await testReceiptRepairSurvivesStoppedRecovery();+await testReceiptRepairSurvivesStoppedRecovery("nonportable");+await testReceiptRepairSurvivesStoppedRecovery("case_collision");+await testReceiptRepairSurvivesStoppedRecovery("control");+await testReceiptRepairSurvivesStoppedRecovery("cross_surface_collision");await testDeadlinePreservesValidWorkerCheckpoints();-await testWorkerCoverageSurvivesParentCompletion();await testLateParentDraftPreservesCheckpointWithoutOverwritingTerminalSeal();await testRecoveredPublicationRejectsLateFailure();await testNoopStoppedRefreshRetainsPublicationFailure();@@ -1020,7 +1025,7 @@ async function testExpiredDeadlineWithoutCompletedDiscoveryAgainstRealWorkbench(}}-async function testWorkerCoverageSurvivesParentCompletion() {+async function testPartialWorkerCoverageSurvivesParentCompletion() {const { fixtureRoot, targetPath, environment } = await createWorkbenchFixture("deep-scan-partial-coverage-",);@@ -1054,21 +1059,24 @@ async function testWorkerCoverageSurvivesParentCompletion() {draft.coverage = {surfaces: [{- id: "entry",+ id: "1",label: "Request entry",disposition: "needs_follow_up",receiptRefs: ["artifacts/review.md"],},+ { label: "Response writer", disposition: "no_issue_found" },],explicitExclusions: [- { pattern: "vendor/**", reason: "Third-party source." },+ { id: "1", pattern: "vendor/**", reason: "Third-party source." },+ { pattern: "generated/**", reason: "Generated output." },],deferred: [{- id: "follow-up",- surfaceIds: ["entry"],+ id: "1",+ surfaceIds: ["1"],reason: "Deployment configuration remains unreviewed.",},+ { reason: "Parent routing review remains pending." },],openQuestions: ["Which deployments enable the handler?"],};@@ -1111,23 +1119,29 @@ async function testWorkerCoverageSurvivesParentCompletion() {assert.equal(terminal?.status, "succeeded", terminal?.error);await runWorkbench(["complete-scan", "--scan-id", run.scanId]);const coverage = await readJson(run.scanDir, "coverage.json");-- assert.equal(coverage.surfaces.length, 2);- assert.equal(- new Set(coverage.surfaces.map((row: { id: string }) => row.id)).size,- 2,- );- assert.equal(coverage.explicitExclusions.length, 2);- assert.equal(coverage.deferred.length, 2);+ for (const field of ["surfaces", "explicitExclusions", "deferred"]) {+ assert.equal(coverage[field].length, 4, field);+ assert.equal(+ new Set(coverage[field].map((row: { id: string }) => row.id)).size,+ 4,+ field,+ );+ }assert.equal(coverage.openQuestions.length, 2);for (const row of coverage.surfaces) {+ if (row.label !== "Request entry") continue;+ assert.equal(+ row.id,+ `${row.workerId}/1`,+ "explicit source IDs retain their links",+ );assert.equal(await readFile(path.join(run.scanDir, row.receiptRefs[0]), "utf8"),"Independent source review receipt.\n",);assert.equal(coverage.deferred.some((item: { surfaceIds: string[] }) =>- item.surfaceIds.includes(row.id),+ item.surfaceIds?.includes(row.id),),true,);@@ -1397,6 +1411,340 @@ async function writePrivateFile(filePath: string, content: string) {console.log("deep scan store integration tests passed");+async function testReceiptRepairSurvivesStoppedRecovery(+ receiptFailure:+ | "missing"+ | "nonportable"+ | "case_collision"+ | "control"+ | "cross_surface_collision" = "missing",+) {+ const { recordCodexSecurityWorkerScanDraft } = await importModule({+ stdin: {+ contents:+ 'export { recordCodexSecurityWorkerScanDraft } from "./src/artifact-scan-draft.ts";',+ resolveDir: mcpAppRoot,+ },+ });+ const { fixtureRoot, targetPath, environment } = await createWorkbenchFixture(+ "deep-receipt-repair-",+ );+ const runWorkbench = createWorkbenchRunner(environment);+ const store = new WorkbenchDeepScanStore(runWorkbench);+ const threadId = "receipt-repair-owner";+ try {+ await mkdir(path.join(environment.CODEX_HOME, "codex-security"), {+ recursive: true,+ });+ await writeFile(+ path.join(environment.CODEX_HOME, "codex-security/config.toml"),+ "[deep_scan]\nworkers = 1\nmax_discovery_runs = 1\n",+ );+ const run = await store.begin({+ targetPath,+ threadId,+ scanRoot: path.join(fixtureRoot, "scans"),+ });+ const claim = await store.claimCoordinator({+ scanId: run.scanId,+ threadId,+ });+ const executor = new FakeExecutor();+ const execute = executor.run.bind(executor);+ let discoveryAttempts = 0;+ let firstOutput: string | undefined;+ let originalResult: string | undefined;+ const crossSurface = receiptFailure === "cross_surface_collision";+ const caseCollision = receiptFailure === "case_collision" || crossSurface;+ const createInvalidFiles =+ receiptFailure !== "missing" &&+ !(+ process.platform === "win32" &&+ (receiptFailure === "nonportable" || receiptFailure === "control")+ );+ const caseProbe = path.join(fixtureRoot, "case-probe");+ await writeFile(caseProbe, "filesystem case probe");+ const caseInsensitive = await readFile(+ path.join(fixtureRoot, "CASE-PROBE"),+ ).then(+ () => true,+ (error: NodeJS.ErrnoException) => {+ if (error.code === "ENOENT") return false;+ throw error;+ },+ );+ await rm(caseProbe);+ const archivedContents = (ref: string) =>+ caseInsensitive && caseCollision+ ? "Archived case-insensitive evidence.\n"+ : `Archived evidence for ${ref}.\n`;+ const pending =+ receiptFailure === "missing" ||+ receiptFailure === "control" ||+ crossSurface;+ const invalidRefs = caseCollision+ ? ["artifacts/review.md", "artifacts/Review.md"]+ : [+ receiptFailure === "nonportable"+ ? "artifacts/review?.md"+ : receiptFailure === "control"+ ? "artifacts/rev\tiew.md"+ : "artifacts/missing.md",+ ];+ executor.run = async (request) => {+ const execution = await execute(request);+ if (request.kind !== "discovery") return execution;+ discoveryAttempts++;+ const context = { ...request.artifactContext!, scanId: run.scanId };+ firstOutput ??= context.root;+ const draft = await readJson(context.root, "result.json");+ await rm(path.join(context.root, "result.json"));+ await mkdir(path.join(context.root, "artifacts"), { recursive: true });+ if (discoveryAttempts === 1) {+ if (createInvalidFiles) {+ for (const ref of invalidRefs)+ await writeFile(+ path.join(context.root, ref),+ archivedContents(ref),+ );+ }+ await writeFile(+ path.join(context.root, "artifacts/original.md"),+ "Original valid evidence.\n",+ );+ await recordCodexSecurityWorkerScanDraft(context, {+ ...draft,+ complete: true,+ coverage: {+ completeness: pending ? "partial" : "complete",+ explicitExclusions: [],+ surfaces: [+ {+ id: "api",+ label: "API",+ disposition: pending ? "needs_follow_up" : "no_issue_found",+ receiptRefs: [+ ...(crossSurface ? [invalidRefs[0]] : invalidRefs),+ "artifacts/original.md",+ ],+ },+ ...(crossSurface+ ? [+ {+ id: "api-other",+ label: "Other API",+ disposition: "needs_follow_up",+ receiptRefs: [invalidRefs[1]],+ },+ ]+ : []),+ ],+ deferred: pending+ ? [+ {+ id: "review",+ reason: "Review remains.",+ surfaceIds: crossSurface ? ["api", "api-other"] : ["api"],+ },+ ]+ : [],+ },+ });+ originalResult = await readFile(+ path.join(context.root, "result.json"),+ "utf8",+ );+ } else if (discoveryAttempts === 2) {+ if (crossSurface)+ await writeFile(+ path.join(context.root, "artifacts/replacement-other.md"),+ "Replacement other evidence.\n",+ );+ await writeFile(+ path.join(context.root, "artifacts/replacement.md"),+ "Replacement evidence.\n",+ );+ await recordCodexSecurityWorkerScanDraft(context, {+ ...draft,+ complete: true,+ coverage: {+ completeness: "complete",+ explicitExclusions: [],+ deferred: [],+ surfaces: [+ {+ id: "api",+ label: "API",+ disposition: "no_issue_found",+ receiptRefs: ["artifacts/replacement.md"],+ },+ ...(crossSurface+ ? [+ {+ id: "api-other",+ label: "Other API",+ disposition: "no_issue_found",+ receiptRefs: ["artifacts/replacement-other.md"],+ },+ ]+ : []),+ ],+ resolvedDeferred: pending+ ? [{ id: "review", reason: "Review completed." }]+ : [],+ },+ });+ for (const complete of [false, true]) {+ await recordCodexSecurityWorkerScanDraft(context, {+ ...draft,+ complete,+ coverage: {+ completeness: "complete",+ surfaces: [],+ explicitExclusions: [],+ deferred: [],+ },+ });+ }+ throw Object.assign(+ new Error("Worker transport timed out after checkpoint."),+ { code: "ETIMEDOUT" },+ );+ } else {+ await recordCodexSecurityWorkerScanDraft(context, {+ ...draft,+ complete: true,+ coverage: {+ completeness: "complete",+ surfaces: [],+ explicitExclusions: [],+ deferred: [],+ },+ });+ }+ return execution;+ };+ const coordinator = new DeepScanCoordinator({+ run: claim.run,+ store,+ pluginRoot,+ threadId,+ executor,+ retryDelaysMs: [0, 0],+ onComplete: async (+ draft: ScanDraftInput,+ signal: AbortSignal,+ publication: DeepScanPublication,+ ) => {+ const context = await createScanArtifactContext(+ run.scanId,+ runWorkbench,+ { requireRunning: true },+ );+ await recordCodexSecurityScanDraftViaWorkbench(+ context,+ draft,+ runWorkbench,+ signal,+ publication,+ );+ },+ });+ coordinator.start();+ const terminal = await coordinator.wait(undefined, 30_000);+ assert.equal(terminal?.status, "succeeded", terminal?.error);+ assert.equal(+ discoveryAttempts,+ 3,+ "invalid receipts must be rejected before a repaired worker is accepted",+ );+ const before = await readJson(run.scanDir, "coverage.json");+ assert.equal(before.surfaces[0].disposition, "no_issue_found");+ const originalTitles = (+ await readJson(run.scanDir, "findings.json")+ ).findings.map((row: { title: string }) => row.title);+ await runWorkbench([+ "cancel-scan",+ "--scan-id",+ run.scanId,+ "--thread-id",+ threadId,+ ]);+ const recovered = await readJson(run.scanDir, "coverage.json");+ const surfaces = recovered.surfaces as {+ id: string;+ disposition: string;+ receiptRefs: string[];+ }[];+ assert.equal(surfaces.length, crossSurface ? 2 : 1);+ assert.deepEqual(+ surfaces.map((row) => row.id).sort(),+ before.surfaces.map((row: { id: string }) => row.id).sort(),+ );+ assert.ok(surfaces.every((row) => row.disposition === "no_issue_found"));+ assert.equal(+ recovered.deferred.some((row: { id: string }) =>+ row.id.endsWith("/review"),+ ),+ false,+ );+ const evidence = await Promise.all(+ surfaces+ .flatMap((row) => row.receiptRefs)+ .map((ref: string) => readFile(path.join(run.scanDir, ref), "utf8")),+ );+ const expected = ["Original valid evidence.\n", "Replacement evidence.\n"];+ if (crossSurface) expected.push("Replacement other evidence.\n");+ if (caseCollision) {+ const retained = [+ ...new Set(+ invalidRefs+ .map(archivedContents)+ .filter((contents) => evidence.includes(contents)),+ ),+ ];+ assert.equal(+ retained.length,+ 1,+ "keep representable earlier evidence without case-colliding manifest entries",+ );+ expected.push(...retained);+ }+ assert.deepEqual(new Set(evidence), new Set(expected));+ assert.equal(+ await readFile(+ path.join(+ path.dirname(firstOutput!),+ "attempts/attempt-01/result.json",+ ),+ "utf8",+ ),+ originalResult,+ );+ if (createInvalidFiles) {+ for (const ref of invalidRefs)+ assert.equal(+ await readFile(+ path.join(path.dirname(firstOutput!), "attempts/attempt-01", ref),+ "utf8",+ ),+ archivedContents(ref),+ );+ }+ assert.deepEqual(+ (await readJson(run.scanDir, "findings.json")).findings.map(+ (row: { title: string }) => row.title,+ ),+ originalTitles,+ );+ const manifest = await readJson(run.scanDir, "scan-manifest.json");+ assert.equal(manifest.scan.status, "canceled");+ assert.equal(typeof manifest.scan.sealedAt, "string");+ } finally {+ await rm(fixtureRoot, { recursive: true, force: true });+ }+}+async function testReplacementFailureDuringCancellation() {const { fixtureRoot, targetPath, environment } = await createWorkbenchFixture("deep-scan-cancel-replacement-",diff --git a/plugins/codex-security/mcp-app/tests/test_scan_draft_finite_writes.ts b/plugins/codex-security/mcp-app/tests/test_scan_draft_finite_writes.tsindex 7927b6112..4d1141b68 100644--- a/plugins/codex-security/mcp-app/tests/test_scan_draft_finite_writes.ts+++ b/plugins/codex-security/mcp-app/tests/test_scan_draft_finite_writes.ts@@ -147,7 +147,15 @@ for (const layout of ["worker", "deep"] as const) {: {}),});const before = structuredClone(input);- await assert.rejects(f.write(input), finiteError);+ await assert.rejects(+ f.write(input),+ layout === "worker" && field !== "threatModel"+ ? {+ name: "Error",+ message: `coverage.${field}[0].<property>.<property>: non-finite JSON numbers are not supported`,+ }+ : finiteError,+ );assert.deepEqual(await readdir(f.root), []);assert.deepEqual(input, before);});diff --git a/plugins/codex-security/plugin-files.json b/plugins/codex-security/plugin-files.jsonindex c637c3308..9cbe2c5bc 100644--- a/plugins/codex-security/plugin-files.json+++ b/plugins/codex-security/plugin-files.json@@ -80,6 +80,7 @@"scripts/workbench/__init__.py","scripts/workbench/handoff.py","scripts/workbench/runtime_toml.py",+ "scripts/workbench/saved_coverage.py","scripts/workbench/storage.py","scripts/workbench_cli.py","scripts/workbench_constants.py",diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.pyindex 9996576c2..ae8fdbd34 100644--- a/plugins/codex-security/scripts/finalize_scan_contract.py+++ b/plugins/codex-security/scripts/finalize_scan_contract.py@@ -1256,6 +1256,7 @@ def _recover_unsealed_coverage(warnings.append("Recovered malformed Deep Scan inventory strategy.")surface_ids: set[str] = set()+ receipt_paths: dict[str, str] = {}for field, label in (("surfaces", "coverage surface"),("explicitExclusions", "coverage exclusion"),@@ -1276,6 +1277,7 @@ def _recover_unsealed_coverage(if not isinstance(item, dict):raise ContractError(f"{context}: expected an object")if field == "surfaces":+ surface_receipt_paths = dict(receipt_paths)surface_id = _require_str(item, "id", context)if surface_id in surface_ids:raise ContractError(f"{context}.id: duplicate surface id")@@ -1310,6 +1312,11 @@ def _recover_unsealed_coverage(f"{ref_context}: expected a file under artifacts/")_require_scan_local_file(scan_dir, normalized_ref, ref_context)+ key = normalized_ref.lower()+ previous = surface_receipt_paths.get(key)+ if previous is not None and previous != normalized_ref:+ raise ContractError(f"{ref_context}: duplicate artifact path")+ surface_receipt_paths[key] = normalized_refexcept ContractError as exc:warnings.append(f"Skipped malformed coverage receipt "@@ -1332,6 +1339,7 @@ def _recover_unsealed_coverage(if field == "surfaces":surface_ids.add(surface_id)+ receipt_paths = surface_receipt_pathsrecovered.append(item)coverage[field] = recovereddiff --git a/plugins/codex-security/scripts/workbench/saved_coverage.py b/plugins/codex-security/scripts/workbench/saved_coverage.pynew file mode 100644index 000000000..d4a439ee9--- /dev/null+++ b/plugins/codex-security/scripts/workbench/saved_coverage.py@@ -0,0 +1,234 @@+"""Project saved worker coverage and reconcile its published parent copies."""++from __future__ import annotations++import copy+import hashlib+import json+from pathlib import Path+from typing import Any++from finalize_scan_contract import (+ ContractError,+ _normalize_unsealed_open_questions,+ _require_portable_relative_path,+)+++def _saved_coverage_id(item: dict[str, Any]) -> str:+ if candidate_id := item.get("candidateId"):+ return candidate_id+ payload = json.dumps(+ item, ensure_ascii=True, allow_nan=False, sort_keys=True, separators=(",", ":")+ ).encode()+ return f"saved-{hashlib.sha256(payload).hexdigest()[:16]}"+++def _project_worker_coverage_row(item: Any, worker_id: str | None, field: str) -> Any:+ row = copy.deepcopy(item)+ if worker_id is None or not isinstance(row, dict):+ return row+ row["workerId"] = worker_id+ if field == "openQuestions":+ return row+ # Reconcile raw checkpoint identities first. Anonymous output rows use the+ # existing content identity, never their position in an earlier snapshot.+ row.setdefault("id", _saved_coverage_id(row))+ if isinstance(row.get("id"), str):+ row["id"] = f"{worker_id}/{row['id']}"+ if field == "deferred" and isinstance(row.get("surfaceIds"), list):+ row["surfaceIds"] = [+ f"{worker_id}/{identity}" if isinstance(identity, str) else identity+ for identity in row["surfaceIds"]+ ]+ return row+++def _coverage_comparison_row(row: Any, field: str) -> Any:+ if field == "surfaces" and isinstance(row, dict):+ row = {"receiptRefs": [], **row}+ if isinstance(row["receiptRefs"], list):+ refs = []+ for ref in row["receiptRefs"]:+ if isinstance(ref, str):+ try:+ ref = _require_portable_relative_path(ref, "Saved coverage receipt")+ except ContractError:+ pass # Leave malformed refs to the existing per-record recovery.+ refs.append(ref)+ row["receiptRefs"] = refs+ return row+ if field == "openQuestions":+ coverage = {"openQuestions": [copy.deepcopy(row)]}+ _normalize_unsealed_open_questions(coverage)+ return next(iter(coverage["openQuestions"]), row)+ return row+++def _aggregate_worker_coverage_rows(rows: list[Any], worker_id: str, field: str) -> list[Any]:+ """Match accepted aggregate IDs without changing raw recovery identities."""+ reserved = {+ row["id"] for row in rows if isinstance(row, dict) and isinstance(row.get("id"), str)+ }+ emitted: set[str] = set()+ projected = []+ for index, original in enumerate(rows):+ row = copy.deepcopy(original)+ if isinstance(row, dict) and field != "openQuestions":+ generated = not isinstance(row.get("id"), str)+ identity = str(index) if generated else row["id"]+ base = identity+ if generated or identity in emitted:+ suffix = 2+ while identity in reserved:+ identity = f"{base}-{suffix}"+ suffix += 1+ reserved.add(identity)+ emitted.add(identity)+ row["id"] = identity+ projected.append(+ _coverage_comparison_row(_project_worker_coverage_row(row, worker_id, field), field)+ )+ return projected+++def _prepare_worker_coverage_recovery(+ sources: list[tuple[str, dict[str, Any], str | None]],+ parent: dict[str, Any] | None,+ source_order: dict[str, tuple[int, int]],+ *,+ enabled: bool,+) -> tuple[+ bool, list[tuple[dict[str, Any], str, dict[str, Any], dict[str, Any], dict[str, Any] | None]]+]:+ """Put bound worker evidence in parent coordinates without changing source files."""+ workers = {owner for _, _, owner in sources if owner is not None}+ parents = [+ draft+ for _, draft, owner in sorted(+ sources, key=lambda source: source_order[source[0]], reverse=True+ )+ if owner is None+ ]+ if parent is not None:+ # Prefer the selected parent over historical copies of a task's surface.+ parents.insert(0, parent)+ fields = ("surfaces", "explicitExclusions", "deferred", "openQuestions")+ if not enabled:+ enabled = any(+ isinstance(row, dict)+ and isinstance(row.get("workerId"), str)+ and row["workerId"] in workers+ and (+ field == "openQuestions"+ or (isinstance(row.get("id"), str) and row["id"].startswith(f"{row['workerId']}/"))+ )+ for draft in parents+ for field in fields+ for row in (+ draft["coverage"].get(field, [])+ if isinstance(draft["coverage"].get(field, []), list)+ else []+ )+ )+ if not enabled:+ return False, []+ projected: dict[str, list[Any]] = {field: [] for field in fields}+ surface_sources: list[tuple[dict[str, Any], dict[str, Any]]] = []+ task_sources: list[tuple[dict[str, Any], dict[str, Any]]] = []+ surface_links: list[+ tuple[dict[str, Any], str, dict[str, Any], dict[str, Any], dict[str, Any] | None]+ ] = []+ for relative, draft, owner in sources:+ if owner is None:+ continue+ directory = Path(relative).parent+ if directory.name == "checkpoints":+ directory = directory.parent+ for field in fields:+ rows = draft["coverage"].get(field, [])+ if not isinstance(rows, list):+ continue+ for index, row in enumerate(rows):+ if field == "openQuestions" and isinstance(row, str):+ row = {"question": row}+ rows[index] = row+ if not isinstance(row, dict):+ continue+ row["workerId"] = owner+ if field == "surfaces" and isinstance(row.get("receiptRefs"), list):+ refs = []+ for ref in row["receiptRefs"]:+ if isinstance(ref, str):+ try:+ normalized = _require_portable_relative_path(+ ref, "Worker coverage receipt"+ )+ except ContractError:+ pass # Leave malformed refs to the existing per-record recovery.+ else:+ if normalized.startswith("artifacts/"):+ ref = f"{directory.as_posix()}/{normalized}"+ refs.append(ref)+ row["receiptRefs"] = refs+ if field == "surfaces":+ row.setdefault("receiptRefs", [])+ aggregate_rows = _aggregate_worker_coverage_rows(rows, owner, field)+ projected[field].extend(aggregate_rows)+ if field in {"surfaces", "deferred"}:+ originals = surface_sources if field == "surfaces" else task_sources+ originals.extend(+ (aggregate, row)+ for aggregate, row in zip(aggregate_rows, rows)+ if isinstance(aggregate, dict) and isinstance(row, dict)+ )+ # A canonical aggregate contains copies of the same worker evidence. Let+ # the existing per-worker chronology reconcile those originals exactly once.+ # Keep parent-only evidence when its bound worker source is unavailable.+ for draft in parents:+ if not draft["coverage"]:+ continue+ for field in fields:+ rows = draft["coverage"].get(field, [])+ if isinstance(rows, list):+ if field == "surfaces":+ tasks = draft["coverage"].get("deferred", [])+ for row in rows:+ if not isinstance(row, dict) or not isinstance(row.get("id"), str):+ continue+ matching_sources = [+ original+ for aggregate, original in surface_sources+ if aggregate == _coverage_comparison_row(row, field)+ ]+ if not matching_sources:+ continue+ for task in tasks if isinstance(tasks, list) else []:+ if (+ isinstance(task, dict)+ and isinstance(task.get("surfaceIds"), list)+ and row["id"] in task["surfaceIds"]+ ):+ surface_links.extend(+ (+ task,+ row["id"],+ source,+ row,+ next(+ (+ original+ for aggregate, original in task_sources+ if aggregate == task+ ),+ None,+ ),+ )+ for source in matching_sources+ )+ draft["coverage"][field] = [+ row+ for row in rows+ if _coverage_comparison_row(row, field) not in projected[field]+ ]+ return True, surface_linksdiff --git a/plugins/codex-security/scripts/workbench_saved_results.py b/plugins/codex-security/scripts/workbench_saved_results.pyindex a77258549..429967a83 100644--- a/plugins/codex-security/scripts/workbench_saved_results.py+++ b/plugins/codex-security/scripts/workbench_saved_results.py@@ -12,7 +12,7 @@ import reimport sqlite3import statimport sys-from collections.abc import Iterator+from collections.abc import Callable, Iteratorfrom contextlib import contextmanagerfrom pathlib import Pathfrom typing import Any@@ -31,6 +31,8 @@ from finalize_scan_contract import (_read_scan_local_json_with_metadata,_recover_unsealed_findings,_remove_scan_local_file_if_exists,+ _require_portable_relative_path,+ _require_str,_validate_completion_binding,_validate_resolved_deferred,_validate_schema_node,@@ -41,6 +43,11 @@ from finalize_scan_contract import (write_scan_local_bytes,write_threat_model_projection_if_possible,)+from workbench.saved_coverage import (+ _prepare_worker_coverage_recovery,+ _project_worker_coverage_row,+ _saved_coverage_id,+)from workbench_constants import PHASESfrom workbench_target import committed_diff_snapshot_digestfrom workbench_validation import path_within_scope@@ -714,10 +721,6 @@ def _merge_tied_parent_observations(return merged-def _saved_coverage_id(item: dict[str, Any]) -> str:- return item.get("candidateId") or f"saved-{_digest(item)[:16]}"--def _deferred_candidate_id(row: dict[str, Any],owner: str | None,@@ -743,10 +746,28 @@ def _generic_surface_updates(surface_schema: dict[str, Any],deferred_rows: dict[str, list[Any]],ambiguous_deferred: set[tuple[str | None, str]],-) -> tuple[set[int], list[dict[str, Any]]]:- if not closed_deferred and not reopened_generic:+ *,+ surface_is_retained: Callable[[str, str | None, dict[str, Any]], bool],+ eligible_receipt_sources: set[str],+ receipt_root: Path | None = None,+ receipt_warnings: list[str] | None = None,+ fallback_surfaces: list[dict[str, Any]] | None = None,+) -> tuple[set[int], list[tuple[str | None, dict[str, Any], set[int]]]]:+ if not closed_deferred and not reopened_generic and receipt_root is None:return set(), []+ def receipt_error(ref: Any) -> ContractError | None:+ if receipt_root is None or not isinstance(ref, str):+ return ContractError("Saved coverage receipt is unavailable.")+ try:+ descriptor = open_scan_local_file_descriptor(+ receipt_root, ref, "Saved coverage receipt"+ )+ os.close(descriptor)+ except ContractError as exc:+ return exc+ return None+def linked(row: dict[str, Any], identity: str | None) -> bool:surface_ids = row.get("surfaceIds", [])return identity is not None and (@@ -760,7 +781,51 @@ def _generic_surface_updates(if isinstance(row, dict) and isinstance(row.get("id"), str):saved_surfaces.setdefault((owner, row["id"]), []).append((relative, row))replaced: set[int] = set()- updates: list[dict[str, Any]] = []+ updates: list[tuple[str | None, dict[str, Any], set[int]]] = []+ # Current selections across all surfaces take precedence over historical refs.+ receipt_paths: dict[str, str] = {}+ retained_surfaces: dict[tuple[str | None, str], dict[str, Any]] = {}+ if receipt_root is not None:+ receipt_surfaces: list[tuple[str | None, dict[str, Any]]] = []+ for key, matches in saved_surfaces.items():+ retained = [+ match for match in matches if surface_is_retained(match[0], key[0], match[1])+ ]+ if not retained:+ continue+ latest_source, latest_surface = max(retained, key=lambda match: source_order[match[0]])+ retained_surfaces[key] = latest_surface+ receipt_surfaces.extend(+ (key[0], row)+ for relative, row in retained+ if source_order[relative] == source_order[latest_source]+ )+ for relative, draft, owner in sources:+ rows = draft["coverage"].get("surfaces", [])+ for row in rows if isinstance(rows, list) else []:+ if (+ isinstance(row, dict)+ and "id" not in row+ and surface_is_retained(relative, owner, row)+ ):+ receipt_surfaces.append((owner, row))+ receipt_surfaces.extend((None, row) for row in fallback_surfaces or [])+ for owner, latest_surface in receipt_surfaces:+ # Recovery repairs disposition and receipts before validating row+ # metadata. A row discarded there cannot reserve a receipt spelling.+ metadata = _project_worker_coverage_row(latest_surface, owner, "surfaces")+ metadata.setdefault("id", _saved_coverage_id(metadata))+ metadata.update(disposition="needs_follow_up", receiptRefs=[])+ try:+ _require_str(metadata, "id", "coverage.surfaces")+ _validate_schema_node(metadata, surface_schema, "coverage.surfaces")+ except ContractError:+ continue+ refs = latest_surface.get("receiptRefs", [])+ for ref in refs if isinstance(refs, list) else []:+ if receipt_error(ref) is None:+ normalized = _require_portable_relative_path(ref, "Saved coverage receipt")+ receipt_paths.setdefault(normalized.lower(), normalized)for relative, draft, owner in sources:closed_ids = {identity@@ -772,7 +837,7 @@ def _generic_surface_updates(for (saved_owner, identity), (_, _, source) in active_deferred.items()if saved_owner == owner and source == relative and (owner, identity) in reopened_generic}- if not closed_ids and not reopened_ids:+ if not closed_ids and not reopened_ids and receipt_root is None:continuecurrent = draft["coverage"].get("surfaces", [])for surface in current if isinstance(current, list) else []:@@ -780,12 +845,18 @@ def _generic_surface_updates(continuereopening = surface.get("disposition") == "needs_follow_up"work_ids = reopened_ids if reopening else closed_ids- if not work_ids:+ repair_only = not work_ids+ if repair_only and receipt_root is None:continueidentity = surface.get("id")if not isinstance(identity, str):continuematches = saved_surfaces[(owner, identity)]+ # Receipt repair alone cannot close or reopen a saved surface.+ if repair_only and any(+ row.get("disposition") != surface.get("disposition") for _, row in matches+ ):+ continue# Older writers could assign one ID to distinct surfaces in a draft.# A closure cannot identify which of those observations it replaces.by_source = dict(matches)@@ -832,27 +903,93 @@ def _generic_surface_updates(continue# An accepted checkpoint can update a saved surface by ID without# optional surfaceIds links on its generic task.- latest_surface = max(matches, key=lambda match: source_order[match[0]])[1]- update = copy.deepcopy(latest_surface)- refs = update.setdefault("receiptRefs", [])+ receipt_matches = [+ (saved_path, row)+ for saved_path, row in matches+ if receipt_root is None+ or saved_path in eligible_receipt_sources+ or surface_is_retained(saved_path, owner, row)+ ]+ if not receipt_matches:+ continue+ latest_surface = max(receipt_matches, key=lambda match: source_order[match[0]])[1]+ target = retained_surfaces.get((owner, identity)) if repair_only else latest_surface+ if target is None:+ continue+ update = copy.deepcopy(target)+ # Historical evidence can repair a retained row without promoting+ # an unselected observation's label, notes, or other metadata.+ refs = copy.deepcopy(latest_surface.get("receiptRefs", []))+ update["receiptRefs"] = refs+ replaced_receipt = False+ replacement_paths = dict(receipt_paths)if isinstance(refs, list):- for _, row in matches:+ repaired = (+ not reopening and bool(refs) and all(receipt_error(ref) is None for ref in refs)+ )+ if repaired:+ for ref in refs:+ normalized = _require_portable_relative_path(ref, "Saved coverage receipt")+ key = normalized.lower()+ if key in replacement_paths and replacement_paths[key] != normalized:+ repaired = False+ break+ replacement_paths[key] = normalized+ for _, row in receipt_matches:previous_refs = row.get("receiptRefs", [])for ref in previous_refs if isinstance(previous_refs, list) else []:if ref not in refs:+ failure = receipt_error(ref) if repaired else None+ replaceable = failure is not None and isinstance(+ failure.__cause__, FileNotFoundError+ )+ if repaired and isinstance(ref, str):+ try:+ normalized = _require_portable_relative_path(+ ref, "Saved coverage receipt"+ )+ except ContractError as error:+ failure = error+ replaceable = True+ else:+ key = normalized.lower()+ previous = replacement_paths.get(key)+ if previous is not None and previous != normalized:+ failure = ContractError(+ f"Saved coverage receipt: duplicate artifact path: {previous} and {normalized}"+ )+ replaceable = True+ elif not replaceable:+ replacement_paths[key] = normalized+ if replaceable:+ replaced_receipt = True+ if receipt_warnings is not None:+ receipt_warnings.append(+ f"Replaced invalid saved coverage receipt {ref}: {failure}"+ )+ continuerefs.append(ref)+ if repair_only and not replaced_receipt:+ continuetry:_validate_schema_node(update, surface_schema, "coverage.surfaces")except ContractError:continue- replaced.update(+ replaced_sources = {id(row)for _, row in matchesif reopeningor row.get("disposition") in {"needs_follow_up", surface.get("disposition")}+ }+ replaced.update(replaced_sources)+ existing = next(+ (entry for entry in updates if entry[0] == owner and entry[1] == update), None)- if update not in updates:- updates.append(update)+ if existing is None:+ receipt_paths.update(replacement_paths)+ updates.append((owner, update, replaced_sources))+ else:+ existing[2].update(replaced_sources)return replaced, updates@@ -908,6 +1045,7 @@ def merge_saved_results(allow_frozen_legacy_parent: bool = False,frozen_model_source: str | None = None,selected_model_source: list[str] | None = None,+ project_worker_coverage: bool = False,) -> tuple[dict[str, Any], dict[str, Any], dict[str, Any]] | None:"""Read only bound parent/worker files; return an unsealed loss-preserving union."""initial_warnings = set(warnings)@@ -1173,6 +1311,70 @@ def merge_saved_results(if parent is None and latest_reducer is not None:parent = drafts_by_path[latest_reducer]+ project_worker_coverage, surface_links = _prepare_worker_coverage_recovery(+ sources, parent, source_order, enabled=project_worker_coverage+ )+ # Keep the actual retained row even when recovery adds previous findings.+ linked_surface_sources = {id(surface) for _, _, surface, _, _ in surface_links}+ linked_surface_outputs: dict[int, dict[str, Any]] = {}+ worker_row_owners: dict[int, str] = {}+ worker_surface_outputs: dict[tuple[str, str], dict[int, dict[str, Any]]] = {}++ def record_coverage_owner(retained: Any, item: Any, owner: str | None, field: str) -> None:+ # Parent aggregates can carry worker metadata without originating in+ # that worker's retained source. Track the actual source of output rows.+ if project_worker_coverage and owner is not None and isinstance(retained, dict):+ worker_row_owners[id(retained)] = owner+ if field == "surfaces" and isinstance(item.get("id"), str):+ worker_surface_outputs.setdefault((owner, item["id"]), {})[id(retained)] = retained++ def append_coverage_row(output: list[Any], item: Any, owner: str | None, field: str) -> Any:+ retained = _project_worker_coverage_row(+ item, owner if project_worker_coverage else None, field+ )+ if retained not in output:+ output.append(retained)+ retained = output[output.index(retained)]+ record_coverage_owner(retained, item, owner, field)+ return retained++ def linked_tasks(+ task: dict[str, Any],+ published_id: str,+ source_task: dict[str, Any] | None,+ candidates: list[tuple[str | None, Any]],+ ) -> list[Any]:+ expected = (+ _project_worker_coverage_row(source_task, source_task["workerId"], "deferred")+ if source_task is not None+ else None+ )+ return [+ retained+ for owner, retained in candidates+ if retained == task+ or (+ expected is not None+ and owner == expected["workerId"]+ and isinstance(retained, dict)+ and retained.get("id") == expected.get("id")+ and isinstance(retained.get("surfaceIds"), list)+ and published_id in retained["surfaceIds"]+ )+ ]++ def linked_surface(surface, outputs, named_outputs):+ retained = outputs.get(id(surface))+ if (+ retained is None+ and isinstance(owner := surface.get("workerId"), str)+ and isinstance(identity := surface.get("id"), str)+ ):+ matches = named_outputs.get((owner, identity), {})+ if len(matches) == 1:+ retained = next(iter(matches.values()))+ return retained+all_sources = ([("parent", parent, None)] if parent else []) + sources# Older checkpoints can omit IDs already assigned in their published output.for field in ("deferred", "surfaces"):@@ -1544,91 +1746,6 @@ def merge_saved_results(represented_candidate_history.setdefault(candidate_key, set()).add(_digest(_finding_content(original["finding"])))- replaced_surfaces, surface_updates = _generic_surface_updates(- all_sources,- source_order,- closed_deferred,- active_deferred,- resolved,- reopened_generic,- coverage_schema["surfaces"]["items"],- deferred_rows,- ambiguous_deferred,- )- replaced_rows = {- "surfaces": replaced_surfaces,- "deferred": {- id(row)- for relative, _, owner in all_sources- for row in deferred_rows[relative]- if isinstance(row, dict)- and isinstance(identity := row.get("id"), str)- and (key := (owner, identity)) not in candidate_ids- and key not in ambiguous_deferred- and (updated := accepted_deferred_orders.get(key)) is not None- and updated > source_order[relative]- },- }- for field, replaced in replaced_rows.items():- if parent and isinstance(coverage.get(field), list):- previous = parent["coverage"].get(field, [])- if isinstance(previous, list):- removed_parent = [row for row in previous if id(row) in replaced]- coverage[field] = [row for row in coverage[field] if row not in removed_parent]- if isinstance(coverage.get("surfaces"), list):- for surface in surface_updates:- if surface not in coverage["surfaces"]:- coverage["surfaces"].append(surface)- for surface in coverage["surfaces"]:- if isinstance(surface, dict):- surface.setdefault("receiptRefs", [])-- # Reopened work survives a superseded checkpoint, but current candidate- # outcomes still apply. Parent closures cannot remove another worker's row.- for owner, item in reopened_rows:- if (identity := _deferred_candidate_id(item, owner, ambiguous_deferred)) is not None and (- owner,- identity,- ) in resolved:- continue- pending = coverage.setdefault("deferred", [])- if isinstance(pending, list) and item not in pending:- pending.append(copy.deepcopy(item))- ambiguous_surface_ids = {- (owner, identity)- for owner, row in reopened_rows- if isinstance(row.get("id"), str)- and (owner, row["id"]) in ambiguous_deferred- and not any(key in row for key in ("candidateId", "candidate", "finding"))- for identity in [- row["id"],- *(row.get("surfaceIds", []) if isinstance(row.get("surfaceIds", []), list) else []),- ]- if isinstance(identity, str)- }- for _, draft, owner in all_sources:- surfaces = draft["coverage"].get("surfaces", [])- by_id: dict[str, dict[str, Any]] = {}- for surface in surfaces if isinstance(surfaces, list) else []:- if not isinstance(surface, dict) or not isinstance(identity := surface.get("id"), str):- continue- if identity in by_id and surface != by_id[identity]:- ambiguous_surface_ids.add((owner, identity))- by_id[identity] = surface- for _, draft, owner in all_sources:- surfaces = draft["coverage"].get("surfaces", [])- for surface in surfaces if isinstance(surfaces, list) else []:- if (- isinstance(surface, dict)- and isinstance(surface.get("id"), str)- and (owner, surface["id"]) in ambiguous_surface_ids- and surface.get("disposition") == "needs_follow_up"- and not any(key in surface for key in ("candidateId", "candidate", "finding"))- and isinstance(coverage.get("surfaces"), list)- ):- retained_surface = {**surface, "receiptRefs": surface.get("receiptRefs", [])}- if retained_surface not in coverage["surfaces"]:- coverage["surfaces"].append(copy.deepcopy(retained_surface))selected_terminal_orders: dict[str, tuple[int, int]] = {}for relative, draft, worker_id in sources:if (@@ -1647,7 +1764,11 @@ def merge_saved_results(terminal_worker_orders[worker_id] = max(terminal_worker_orders.get(worker_id, order), order)- for relative, draft, worker_id in all_sources:++ def source_coverage_selection(+ relative: str,+ worker_id: str | None,+ ) -> tuple[bool, set[str], bool, bool]:worker_result_order = terminal_worker_orders.get(worker_id)selected_coverage_superseded = worker_id in selected_terminal_orders and (source_order[relative] < selected_terminal_orders[worker_id]@@ -1689,6 +1810,221 @@ def merge_saved_results(and source_order[relative] >= accepted_orderand any(isinstance(row, dict) for row in deferred_rows[relative]))+ return selected_coverage_superseded, selected_candidates, superseded, retain_pending++ def coverage_row_is_retained(+ field: str,+ item: Any,+ worker_id: str | None,+ selection: tuple[bool, set[str], bool, bool],+ ) -> bool:+ selected_coverage_superseded, selected_candidates, superseded, retain_pending = selection+ # A selected terminal checkpoint replaces ordinary coverage, while+ # candidate evidence and pending tasks retain their own reconciliation.+ if (+ selected_coverage_superseded+ and field != "deferred"+ and not (+ isinstance(item, dict)+ and any(key in item for key in ("candidateId", "candidate", "finding"))+ )+ ):+ return False+ # A selected outcome must retain its evidence even if its result write failed.+ if superseded and not (+ isinstance(item, dict)+ and (+ (field == "deferred" and retain_pending)+ or (+ isinstance(item.get("candidateId"), str)+ and item["candidateId"] in selected_candidates+ and item.get("disposition") == resolved.get((worker_id, item["candidateId"]))+ )+ )+ ):+ return False+ if (+ isinstance(item, dict)+ and isinstance(+ identity := _deferred_candidate_id(item, worker_id, ambiguous_deferred)+ if field == "deferred"+ else item.get("candidateId"),+ str,+ )+ and (worker_id, identity) in resolved+ and (field == "deferred" or item.get("disposition") == "needs_follow_up")+ ):+ return False+ return True++ ambiguous_surface_ids = {+ (owner, identity)+ for owner, row in reopened_rows+ if isinstance(row.get("id"), str)+ and (owner, row["id"]) in ambiguous_deferred+ and not any(key in row for key in ("candidateId", "candidate", "finding"))+ for identity in [+ row["id"],+ *(row.get("surfaceIds", []) if isinstance(row.get("surfaceIds", []), list) else []),+ ]+ if isinstance(identity, str)+ }+ for _, draft, owner in all_sources:+ surfaces = draft["coverage"].get("surfaces", [])+ by_id: dict[str, dict[str, Any]] = {}+ for surface in surfaces if isinstance(surfaces, list) else []:+ if not isinstance(surface, dict) or not isinstance(identity := surface.get("id"), str):+ continue+ if identity in by_id and surface != by_id[identity]:+ ambiguous_surface_ids.add((owner, identity))+ by_id[identity] = surface++ replaced_deferred = {+ id(row)+ for relative, _, owner in all_sources+ for row in deferred_rows[relative]+ if isinstance(row, dict)+ and isinstance(identity := row.get("id"), str)+ and (key := (owner, identity)) not in candidate_ids+ and key not in ambiguous_deferred+ and (updated := accepted_deferred_orders.get(key)) is not None+ and updated > source_order[relative]+ }+ fallback_surfaces = []+ if surface_links and isinstance(coverage.get("surfaces"), list):+ # Plan the surviving associations before historical receipts can claim+ # a spelling needed by a surface that must be restored for its task.+ prospective_tasks = []+ prospective_surfaces = {}+ prospective_named = {}+ for relative, draft, owner in all_sources:+ selection = source_coverage_selection(relative, owner)+ for row in deferred_rows[relative]:+ if (+ coverage_row_is_retained("deferred", row, owner, selection)+ and id(row) not in replaced_deferred+ and not (+ isinstance(row, dict)+ and isinstance(row.get("id"), str)+ and (owner, row["id"]) in closed_deferred+ )+ ):+ prospective_tasks.append(+ (owner, _project_worker_coverage_row(row, owner, "deferred"))+ )+ rows = draft["coverage"].get("surfaces", [])+ for row in rows if isinstance(rows, list) else []:+ if owner is None or not isinstance(row, dict):+ continue+ emitted = coverage_row_is_retained("surfaces", row, owner, selection)+ ambiguous = (+ isinstance(row.get("id"), str)+ and (owner, row["id"]) in ambiguous_surface_ids+ and row.get("disposition") == "needs_follow_up"+ and not any(key in row for key in ("candidateId", "candidate", "finding"))+ )+ if not emitted and not ambiguous:+ continue+ projected = _project_worker_coverage_row(row, owner, "surfaces")+ if isinstance(row.get("id"), str):+ named = prospective_named.setdefault((owner, row["id"]), {})+ projected = next(+ (existing for existing in named.values() if existing == projected),+ projected,+ )+ named[id(projected)] = projected+ if emitted:+ prospective_surfaces[id(row)] = projected+ prospective_tasks.extend(+ (owner, _project_worker_coverage_row(row, owner, "deferred"))+ for owner, row in reopened_rows+ if (owner, _deferred_candidate_id(row, owner, ambiguous_deferred)) not in resolved+ )+ bound = {+ (id(retained), published_id)+ for task, published_id, surface, _, task_source in surface_links+ if linked_surface(surface, prospective_surfaces, prospective_named) is not None+ for retained in linked_tasks(task, published_id, task_source, prospective_tasks)+ }+ for task, published_id, _, parent_surface, task_source in surface_links:+ for retained in linked_tasks(task, published_id, task_source, prospective_tasks):+ key = (id(retained), published_id)+ if key not in bound:+ fallback_surfaces.append(parent_surface)+ bound.add(key)++ replaced_surfaces, surface_updates = _generic_surface_updates(+ all_sources,+ source_order,+ closed_deferred,+ active_deferred,+ resolved,+ reopened_generic,+ coverage_schema["surfaces"]["items"],+ deferred_rows,+ ambiguous_deferred,+ receipt_root=scan_dir if project_worker_coverage else None,+ receipt_warnings=warnings,+ fallback_surfaces=fallback_surfaces,+ surface_is_retained=lambda relative, owner, row: coverage_row_is_retained(+ "surfaces", row, owner, source_coverage_selection(relative, owner)+ ),+ eligible_receipt_sources={+ relative+ for relative, draft, _ in all_sources+ if draft.get("complete") is not False or relative in selected_observations+ },+ )+ replaced_rows = {+ "surfaces": replaced_surfaces,+ "deferred": replaced_deferred,+ }+ for field, replaced in replaced_rows.items():+ if parent and isinstance(coverage.get(field), list):+ previous = parent["coverage"].get(field, [])+ if isinstance(previous, list):+ removed_parent = [row for row in previous if id(row) in replaced]+ coverage[field] = [row for row in coverage[field] if row not in removed_parent]+ if isinstance(coverage.get("surfaces"), list):+ for owner, surface, replaced_sources in surface_updates:+ retained = append_coverage_row(coverage["surfaces"], surface, owner, "surfaces")+ for source_id in replaced_sources & linked_surface_sources:+ linked_surface_outputs[source_id] = retained+ for surface in coverage["surfaces"]:+ if isinstance(surface, dict):+ surface.setdefault("receiptRefs", [])++ # Reopened work survives a superseded checkpoint, but current candidate+ # outcomes still apply. Parent closures cannot remove another worker's row.+ for owner, item in reopened_rows:+ if (identity := _deferred_candidate_id(item, owner, ambiguous_deferred)) is not None and (+ owner,+ identity,+ ) in resolved:+ continue+ pending = coverage.setdefault("deferred", [])+ if isinstance(pending, list):+ append_coverage_row(pending, item, owner, "deferred")+ for _, draft, owner in all_sources:+ surfaces = draft["coverage"].get("surfaces", [])+ for surface in surfaces if isinstance(surfaces, list) else []:+ if (+ isinstance(surface, dict)+ and isinstance(surface.get("id"), str)+ and (owner, surface["id"]) in ambiguous_surface_ids+ and surface.get("disposition") == "needs_follow_up"+ and not any(key in surface for key in ("candidateId", "candidate", "finding"))+ and isinstance(coverage.get("surfaces"), list)+ ):+ append_coverage_row(+ coverage["surfaces"],+ {**surface, "receiptRefs": surface.get("receiptRefs", [])},+ owner,+ "surfaces",+ )+ for relative, draft, worker_id in all_sources:+ selection = source_coverage_selection(relative, worker_id)+ selected_coverage_superseded, selected_candidates, superseded, retain_pending = selectionskip_superseded_findings = (supersededand not stopped@@ -1884,30 +2220,8 @@ def merge_saved_results(# recovery and warnings rather than silently changing its contract.continuefor item in items:- # A selected terminal checkpoint replaces ordinary coverage, while- # candidate evidence and pending tasks retain their own reconciliation.- if (- selected_coverage_superseded- and field != "deferred"- and not (- isinstance(item, dict)- and any(key in item for key in ("candidateId", "candidate", "finding"))- )- ):- continue- # A selected outcome must retain its evidence even if its result write failed.- if superseded and not (- isinstance(item, dict)- and (- (field == "deferred" and retain_pending)- or (- isinstance(item.get("candidateId"), str)- and item["candidateId"] in selected_candidates- and item.get("disposition")- == resolved.get((worker_id, item["candidateId"]))- )- )- ):+ source_item = item+ if not coverage_row_is_retained(field, item, worker_id, selection):continueif id(item) in replaced_rows.get(field, ()):continue@@ -1939,31 +2253,29 @@ def merge_saved_results(for previous in history):history.append(copy.deepcopy(finding))- if (- isinstance(item, dict)- and isinstance(- identity := _deferred_candidate_id(item, worker_id, ambiguous_deferred)- if field == "deferred"- else item.get("candidateId"),- str,- )- and (worker_id, identity) in resolved- and (field == "deferred" or item.get("disposition") == "needs_follow_up")- ):- continueif field == "surfaces" and isinstance(item, dict):item = {**item, "receiptRefs": item.get("receiptRefs", [])}if isinstance(item, dict) and "id" not in item:semantic_item = dict(item)- if any(- isinstance(existing, dict)- and {key: value for key, value in existing.items() if key != "id"}- == semantic_item- for existing in output- ):+ existing = next(+ (+ row+ for row in output+ if isinstance(row, dict)+ and {key: value for key, value in row.items() if key != "id"}+ == semantic_item+ ),+ None,+ )+ if existing is not None:+ if project_worker_coverage and worker_id is not None:+ worker_row_owners[id(existing)] = worker_id+ if field == "surfaces" and id(source_item) in linked_surface_sources:+ linked_surface_outputs[id(source_item)] = existingcontinue- if item not in output:- output.append(copy.deepcopy(item))+ retained = append_coverage_row(output, item, worker_id, field)+ if field == "surfaces" and id(source_item) in linked_surface_sources:+ linked_surface_outputs[id(source_item)] = retainedidentities: dict[str, str] = {}for finding in findings:@@ -1978,9 +2290,77 @@ def merge_saved_results(finding.setdefault("provenance", {})["preservedIdentity"] = copy.deepcopy(identity)identity["instance"] = f"{identity.get('instance', 'saved')}-{variant[:16]}"identities[key] = variant+ # Bind links to retained output objects before their IDs become unique.+ # Candidate metadata can give an anonymous row the same preliminary ID as+ # another surface, so only the final surface ID can be used for its links.+ retained_surface_links: dict[int, tuple[dict[str, Any], dict[str, dict[str, Any]]]] = {}+ output_tasks = coverage.get("deferred", [])+ task_candidates = [+ (worker_row_owners.get(id(row)), row)+ for row in (output_tasks if isinstance(output_tasks, list) else [])+ ]+ for task, published_id, surface, _, task_source in surface_links:+ retained_surface = linked_surface(surface, linked_surface_outputs, worker_surface_outputs)+ if retained_surface is None or not isinstance(retained_surface.get("id"), str):+ continue+ for retained_task in linked_tasks(task, published_id, task_source, task_candidates):+ _, links = retained_surface_links.setdefault(id(retained_task), (retained_task, {}))+ links.setdefault(published_id, retained_surface)+ # A newer worker checkpoint may omit a surface still needed by a retained+ # task. Restore its parent copy only when no worker replacement survived.+ for task, published_id, surface, parent_surface, task_source in surface_links:+ for retained_task in linked_tasks(task, published_id, task_source, task_candidates):+ _, links = retained_surface_links.setdefault(id(retained_task), (retained_task, {}))+ if published_id not in links and isinstance(coverage.get("surfaces"), list):+ retained_surface = append_coverage_row(+ coverage["surfaces"], parent_surface, None, "surfaces"+ )+ record_coverage_owner(+ retained_surface, surface, surface.get("workerId"), "surfaces"+ )+ links[published_id] = retained_surface+ if project_worker_coverage:+ worker_surfaces: dict[tuple[str, str], dict[str, Any]] = {}+ named_surface_outputs = {+ identity for outputs in worker_surface_outputs.values() for identity in outputs+ }+ surfaces = coverage.get("surfaces", [])+ for surface in surfaces if isinstance(surfaces, list) else []:+ if not isinstance(surface, dict):+ continue+ owner, identity = worker_row_owners.get(id(surface)), surface.get("id")+ if isinstance(owner, str) and isinstance(identity, str):+ key = (owner, identity)+ existing = worker_surfaces.get(key)+ # An anonymous candidate ID can collide with an explicit surface ID.+ # Bind a named worker link to the explicitly named source.+ if existing is None or (+ id(surface) in named_surface_outputs+ and id(existing) not in named_surface_outputs+ ):+ worker_surfaces[key] = surface+ for task in output_tasks if isinstance(output_tasks, list) else []:+ if (+ not isinstance(task, dict)+ or (owner := worker_row_owners.get(id(task))) is None+ or not isinstance(task.get("surfaceIds"), list)+ ):+ continue+ for identity in task["surfaceIds"]:+ if (+ isinstance(identity, str)+ and (surface := worker_surfaces.get((owner, identity))) is not None+ ):+ _, links = retained_surface_links.setdefault(id(task), (task, {}))+ links.setdefault(identity, surface)for field in ("surfaces", "explicitExclusions", "deferred"):used: set[str] = set()items = coverage.setdefault(field, [])+ reserved = {+ row["id"]+ for row in (items if isinstance(items, list) else [])+ if isinstance(row, dict) and isinstance(row.get("id"), str)+ }for item in items if isinstance(items, list) else []:if not isinstance(item, dict):continue@@ -1993,10 +2373,22 @@ def merge_saved_results(# Preserve malformed rows for per-record recovery, including frozen replay.continueif item["id"] in used:- item["id"] = f"{item['id']}-{_digest(item)[:16]}"+ if project_worker_coverage:+ base = item["id"]+ suffix = 2+ while item["id"] in used or item["id"] in reserved:+ item["id"] = f"{base}-{suffix}"+ suffix += 1+ else:+ item["id"] = f"{item['id']}-{_digest(item)[:16]}"used.add(item["id"])if field == "surfaces":item.setdefault("receiptRefs", [])+ for task, links in retained_surface_links.values():+ task["surfaceIds"] = [+ links[identity]["id"] if isinstance(identity, str) and identity in links else identity+ for identity in task["surfaceIds"]+ ]coverage.pop("completeness", None)if stopped:if not isinstance(coverage.get("deferred"), list):@@ -2085,6 +2477,7 @@ def preserve_deep_deadline_draft(db: Any, connection: Any, scan: Any) -> Iterato"The configured discovery time limit elapsed before any source review completed. ""Saved findings and pending review were preserved without a completed reduction."),+ project_worker_coverage=True,)if documents is None:raise ContractError("Deep Scan deadline has no saved parent draft.")@@ -2271,6 +2664,7 @@ def preserve_scan_results_locked(frozen_source_digests=frozen_source_digests,frozen_model_source=model_source[0] if model_source else None,selected_model_source=model_source,+ project_worker_coverage=scan["mode"] == "deep",allow_frozen_legacy_parent=(include_parent_with_recoveryor (diff --git a/plugins/codex-security/tests/test_deep_coverage_recovery.py b/plugins/codex-security/tests/test_deep_coverage_recovery.pynew file mode 100644index 000000000..39a13cbfb--- /dev/null+++ b/plugins/codex-security/tests/test_deep_coverage_recovery.py@@ -0,0 +1,1677 @@+from __future__ import annotations++import copy+import hashlib+import json+import os+import sys+from pathlib import Path++import pytest+from test_workbench_standard_deep_results import write_saved_parent+from workbench_test_support import (+ saved_binding,+ saved_discovery_worker,+ saved_draft,+ write_checkpoint,+ write_completed_contract,+)++sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))+import finalize_scan_contract as finalizer+import workbench_saved_results as saved+++def worker(root: Path, name: str, draft: dict):+ output = root / "artifacts" / "deep_discovery" / "workers" / name / "output"+ (output / "artifacts").mkdir(parents=True)+ (output / "artifacts/review.md").write_text(f"{name} source review\n")+ result = output / "result.json"+ result.write_text(json.dumps(draft))+ os.utime(result, ns=(100, 100))+ return saved_discovery_worker(output, name, 1)+++def recover(root, workers, *, project=False, frozen=None, warnings=None):+ expected_warnings = warnings is not None+ warnings = [] if warnings is None else warnings+ result = saved.merge_saved_results(+ root,+ "coverage-scan",+ saved_binding("deep_repository", repository="synthetic"),+ workers,+ warnings,+ stopped=True,+ reason="Source review stopped.",+ project_worker_coverage=project,+ frozen_source_digests=frozen,+ )+ assert result is not None+ finalizer._normalize_unsealed_open_questions(result[2])+ finalizer._recover_unsealed_coverage(+ result[2], Path(saved.__file__).resolve().parent.parent / "schemas", root, warnings, []+ )+ if not expected_warnings:+ assert not warnings+ return result++[email protected]("identical_labels", [False, True])+def test_worker_projection_retains_independent_links_receipts_and_frozen_replay(+ tmp_path, identical_labels+):+ workers = []+ for name in ("first", "second"):+ draft = saved_draft(+ "coverage-scan",+ complete=False,+ surfaces=[+ {+ "id": "api",+ "label": "API" if identical_labels else name,+ "disposition": "no_issue_found",+ "receiptRefs": ["artifacts/review.md"],+ }+ ],+ deferred=[{"id": "review", "reason": "Review remains.", "surfaceIds": ["api"]}],+ )+ workers.append(worker(tmp_path, name, draft))+ originals = {p: p.read_bytes() for p in tmp_path.rglob("result.json")}+ first = recover(tmp_path, workers, project=True)+ replay = recover(tmp_path, workers, project=True, frozen=first[0]["scan"]["preservedSources"])+ assert replay[2] == first[2]+ for coverage in (first[2], replay[2]):+ assert {row["id"] for row in coverage["surfaces"]} == {"first/api", "second/api"}+ for name in ("first", "second"):+ surface = next(row for row in coverage["surfaces"] if row["workerId"] == name)+ task = next(row for row in coverage["deferred"] if row.get("workerId") == name)+ assert task["surfaceIds"] == [surface["id"]]+ assert surface["disposition"] == "no_issue_found"+ assert (tmp_path / surface["receiptRefs"][0]).read_text() == f"{name} source review\n"+ assert all(p.read_bytes() == contents for p, contents in originals.items())++[email protected]("collision", [False, True])[email protected]("parent_has_worker_metadata", [False, True])+def test_worker_links_follow_final_ids_without_redirecting_parent_links(+ tmp_path, collision, parent_has_worker_metadata+):+ surface = {"id": "api", "label": "Worker review", "disposition": "needs_follow_up"}+ source = worker(+ tmp_path,+ "first",+ saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[surface],+ deferred=[{"id": "review", "reason": "Review worker.", "surfaceIds": ["api"]}],+ ),+ )+ parent_id = "first/api" if collision else "parent-api"+ parent_metadata = {"workerId": "first"} if parent_has_worker_metadata else {}+ write_saved_parent(+ tmp_path,+ saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[{**surface, "id": parent_id, "label": "Parent review", **parent_metadata}],+ deferred=[+ {+ "id": "parent-review",+ "reason": "Review parent.",+ "surfaceIds": [parent_id],+ **parent_metadata,+ }+ ],+ ),+ 200,+ )+ first = recover(tmp_path, [source], project=True)+ replay = recover(tmp_path, [source], project=True, frozen=first[0]["scan"]["preservedSources"])+ for documents in (first, replay):+ surfaces = {row["label"]: row for row in documents[2]["surfaces"]}+ tasks = {row["id"]: row for row in documents[2]["deferred"]}+ assert surfaces["Parent review"]["id"] == parent_id+ assert surfaces["Worker review"]["id"] == ("first/api-2" if collision else "first/api")+ assert tasks["first/review"]["surfaceIds"] == [surfaces["Worker review"]["id"]]+ assert tasks["parent-review"]["surfaceIds"] == [parent_id]++[email protected]("receipt", [None, "artifacts/./review.md", "artifacts//review.md"])+def test_aggregate_copies_reconcile_once_and_later_worker_closure_removes_pending_work(+ tmp_path, receipt+):+ surface = {"id": "api", "label": "API", "disposition": "needs_follow_up"}+ if receipt is not None:+ surface["receiptRefs"] = [receipt]+ task = {"id": "review", "reason": "Review remains.", "surfaceIds": ["api"]}+ draft = saved_draft("coverage-scan", complete=True, surfaces=[surface], deferred=[task])+ source = worker(tmp_path, "first", draft)+ parent = copy.deepcopy(draft)+ parent["coverage"]["surfaces"] = [{**surface, "id": "first/api", "workerId": "first"}]+ if receipt is not None:+ parent["coverage"]["surfaces"][0]["receiptRefs"] = [+ f"artifacts/deep_discovery/workers/first/output/{receipt}"+ ]+ parent["coverage"]["deferred"] = [+ {**task, "id": "first/review", "surfaceIds": ["first/api"], "workerId": "first"}+ ]+ write_saved_parent(tmp_path, parent, 200)+ initial = recover(tmp_path, [source])[2]+ assert [row["id"] for row in initial["surfaces"]] == ["first/api"]+ assert [row["id"] for row in initial["deferred"]] == ["first/review", "scan-stopped"]+ output = Path(source["artifact_dir"])+ closed = saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[{**surface, "disposition": "no_issue_found"}],+ closures=[{"id": "review", "reason": "Review completed."}],+ )+ checkpoint = write_checkpoint(output / "checkpoints", closed)+ os.utime(checkpoint, ns=(300, 300))+ head = output / "checkpoint-head.json"+ head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ os.utime(head, ns=(400, 400))+ originals = {path: path.read_bytes() for path in tmp_path.rglob("*.json")}+ documents = recover(tmp_path, [source])+ replay = recover(tmp_path, [source], frozen=documents[0]["scan"]["preservedSources"])+ assert replay[2] == documents[2]+ result = documents[2]+ assert [row["id"] for row in result["deferred"]] == ["scan-stopped"]+ assert result["surfaces"] == [+ {+ **surface,+ "id": "first/api",+ "workerId": "first",+ "disposition": "no_issue_found",+ "receiptRefs": (+ ["artifacts/deep_discovery/workers/first/output/artifacts/review.md"]+ if receipt is not None+ else []+ ),+ }+ ]+ assert all(path.read_bytes() == contents for path, contents in originals.items())++[email protected]("old_label", ["Older review", "Earlier review"])[email protected]("selected_parent", [False, True])[email protected]("complete_worker", [False, True])+def test_parent_surface_fallback_uses_current_snapshot(+ tmp_path, old_label, selected_parent, complete_worker+):+ current_surface = {+ "id": "api",+ "label": "Current review",+ "disposition": "needs_follow_up",+ "receiptRefs": ["artifacts/new.md"],+ }+ current = saved_draft("coverage-scan", complete=complete_worker, surfaces=[current_surface])+ source = worker(tmp_path, "first", current)+ output = Path(source["artifact_dir"])+ older = copy.deepcopy(current)+ older["coverage"]["surfaces"][0].update(label=old_label, receiptRefs=["artifacts/old.md"])+ for name, draft, modified in [("old", older, 50), ("new", current, 100)]:+ (output / "artifacts" / f"{name}.md").write_text(f"{name} evidence\n")+ checkpoint = write_checkpoint(output / "checkpoints", draft)+ os.utime(checkpoint, ns=(modified, modified))+ parent = copy.deepcopy(draft)+ parent["coverage"]["surfaces"][0].update(+ id="first/api",+ workerId="first",+ receiptRefs=[f"{output.relative_to(tmp_path).as_posix()}/artifacts/{name}.md"],+ )+ parent["coverage"]["deferred"] = [+ {"id": "parent-review", "reason": "Review remains.", "surfaceIds": ["first/api"]}+ ]+ parent_path = write_checkpoint(tmp_path / "checkpoints", parent)+ os.utime(parent_path, ns=(modified + 100, modified + 100))+ if selected_parent:+ head = tmp_path / "checkpoint-head.json"+ head.write_text(json.dumps({"checkpoint": parent_path.name}))+ os.utime(head, ns=(250, 250))+ else:+ write_saved_parent(tmp_path, parent, 200)+ template = tmp_path / "template"+ template.mkdir()+ write_completed_contract(template, "coverage-scan", tmp_path, target_kind="git_revision")+ finding = json.loads((template / "findings.json").read_text())["findings"][0]+ finding["provenance"].update(workerId="first", candidateId="api")+ selected = write_checkpoint(+ output / "checkpoints",+ saved_draft("coverage-scan", complete=complete_worker, findings=[finding]),+ )+ os.utime(selected, ns=(300, 300))+ head = output / "checkpoint-head.json"+ head.write_text(json.dumps({"checkpoint": selected.name}))+ os.utime(head, ns=(400, 400))+ originals = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}++ result = recover(tmp_path, [source])+ replay = recover(tmp_path, [source], frozen=result[0]["scan"]["preservedSources"])++ assert replay[2] == result[2]+ for documents in (result, replay):+ assert len(documents[1]["findings"]) == 1+ assert len(documents[2]["surfaces"]) == (1 if complete_worker else 2)+ surface = next(row for row in documents[2]["surfaces"] if row["label"] == "Current review")+ assert (tmp_path / surface["receiptRefs"][0]).read_text() == "new evidence\n"+ task = next(row for row in documents[2]["deferred"] if row["id"] == "parent-review")+ assert task["surfaceIds"] == [surface["id"]]+ assert all(path.read_bytes() == contents for path, contents in originals.items())++[email protected]("mode", ["fallback", "worker-replacement", "no-task"])[email protected]("reverse", [False, True])+def test_fallback_receipts_take_precedence_over_historical_repairs(tmp_path, mode, reverse):+ current_name, historical_name = (+ ("review.md", "Review.md") if reverse else ("Review.md", "review.md")+ )+ surface = {+ "id": "api",+ "label": "Current review",+ "disposition": "no_issue_found",+ "receiptRefs": [f"artifacts/{current_name}"],+ }+ writer = {+ **surface,+ "id": "writer",+ "label": "Other review",+ "receiptRefs": ["artifacts/other.md"],+ }+ draft = saved_draft("coverage-scan", complete=True, surfaces=[surface, writer])+ source = worker(tmp_path, "first", draft)+ output = Path(source["artifact_dir"])+ prefix = output.relative_to(tmp_path).as_posix() + "/artifacts/"+ for name in [current_name, historical_name, "other.md", "replacement.md"]:+ (output / "artifacts" / name).write_text(f"{name} evidence\n")+ older = saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[+ {**writer, "receiptRefs": [f"artifacts/{historical_name}", "artifacts/missing.md"]}+ ],+ )+ checkpoint = write_checkpoint(output / "checkpoints", older)+ os.utime(checkpoint, ns=(50, 50))+ parent = copy.deepcopy(draft)+ for row in parent["coverage"]["surfaces"]:+ row.update(+ id=f"first/{row['id']}",+ workerId="first",+ receiptRefs=[prefix + ref.rsplit("/", 1)[1] for ref in row["receiptRefs"]],+ )+ parent["coverage"]["deferred"] = (+ []+ if mode == "no-task"+ else [{"id": "parent-review", "reason": "Review remains.", "surfaceIds": ["first/api"]}]+ )+ write_saved_parent(tmp_path, parent, 200)+ selected_surfaces = [writer]+ if mode == "worker-replacement":+ selected_surfaces.append({**surface, "receiptRefs": ["artifacts/replacement.md"]})+ selected = write_checkpoint(+ output / "checkpoints",+ saved_draft("coverage-scan", complete=True, surfaces=selected_surfaces),+ )+ os.utime(selected, ns=(300, 300))+ head = output / "checkpoint-head.json"+ head.write_text(json.dumps({"checkpoint": selected.name}))+ os.utime(head, ns=(400, 400))+ originals = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}+ warnings = []+ result = recover(tmp_path, [source], warnings=warnings)+ replay = recover(tmp_path, [source], frozen=result[0]["scan"]["preservedSources"], warnings=[])+ assert replay[2] == result[2]+ rows = {row["id"]: row for row in result[2]["surfaces"]}+ expected = {prefix + "other.md"}+ if mode != "fallback":+ expected.add(prefix + historical_name)+ assert set(rows["first/writer"]["receiptRefs"]) == expected+ if mode != "no-task":+ assert rows["first/api"]["disposition"] == "no_issue_found"+ name = current_name if mode == "fallback" else "replacement.md"+ assert rows["first/api"]["receiptRefs"] == [prefix + name]+ else:+ assert "first/api" not in rows+ assert any("missing.md" in warning for warning in warnings)+ assert all(path.read_bytes() == contents for path, contents in originals.items())++[email protected]("earlier_identity", [None, "restored"])[email protected]("colliding_candidate", [False, True])[email protected]("selected_checkpoint", [False, True])+def test_parent_only_task_keeps_link_to_recovered_anonymous_surface(+ tmp_path, earlier_identity, colliding_candidate, selected_checkpoint+):+ surface = {"label": "API", "disposition": "needs_follow_up"}+ surfaces = [surface]+ published_id = "first/0"+ if colliding_candidate:+ surface["candidateId"] = "api"+ surfaces.insert(0, {"id": "api", "label": "Other", "disposition": "needs_follow_up"})+ published_id = "first/1"+ draft = saved_draft("coverage-scan", complete=True, surfaces=surfaces)+ source = worker(tmp_path, "first", draft)+ if earlier_identity is not None:+ older = copy.deepcopy(draft)+ older["coverage"]["surfaces"][-1]["id"] = earlier_identity+ checkpoint = write_checkpoint(Path(source["artifact_dir"]) / "checkpoints", older)+ os.utime(checkpoint, ns=(50, 50))+ if selected_checkpoint:+ output = Path(source["artifact_dir"])+ checkpoint = write_checkpoint(output / "checkpoints", draft)+ os.utime(checkpoint, ns=(300, 300))+ head = output / "checkpoint-head.json"+ head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ os.utime(head, ns=(400, 400))+ parent = copy.deepcopy(draft)+ parent["coverage"]["surfaces"][-1].update(id=published_id, workerId="first")+ if colliding_candidate:+ parent["coverage"]["surfaces"][0].update(id="first/api", workerId="first")+ parent["coverage"]["deferred"] = [+ {"id": "parent-review", "reason": "Review remains.", "surfaceIds": [published_id]}+ ]+ write_saved_parent(tmp_path, parent, 200)+ originals = {p: p.read_bytes() for p in tmp_path.rglob("*.json")}+ result = recover(tmp_path, [source])+ replay = recover(tmp_path, [source], frozen=result[0]["scan"]["preservedSources"])+ assert replay[2] == result[2]+ for coverage in (result[2], replay[2]):+ assert len(coverage["surfaces"]) == len(surfaces)+ recovered_surface = next(row for row in coverage["surfaces"] if row["label"] == "API")+ task = next(row for row in coverage["deferred"] if row["id"] == "parent-review")+ assert task["surfaceIds"] == [recovered_surface["id"]]+ assert recovered_surface["workerId"] == "first"+ if earlier_identity is not None:+ assert recovered_surface["id"] == f"first/{earlier_identity}"+ if colliding_candidate:+ other = next(row for row in coverage["surfaces"] if row["label"] == "Other")+ assert task["surfaceIds"] != [other["id"]]+ assert all(p.read_bytes() == contents for p, contents in originals.items())++[email protected]("anonymous_first", [False, True])[email protected]("checkpoint", ["none", "same", "updated"])+def test_parent_only_task_keeps_link_to_explicit_worker_surface(+ tmp_path, anonymous_first, checkpoint+):+ anonymous = {"candidateId": "api", "label": "Anonymous", "disposition": "needs_follow_up"}+ explicit = {"id": "api", "label": "Explicit", "disposition": "needs_follow_up"}+ draft = saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[anonymous, explicit] if anonymous_first else [explicit, anonymous],+ )+ source = worker(tmp_path, "first", draft)+ parent = copy.deepcopy(draft)+ for index, surface in enumerate(parent["coverage"]["surfaces"]):+ surface.update(id=f"first/{surface.get('id', index)}", workerId="first")+ parent["coverage"]["deferred"] = [+ {"id": "parent-review", "reason": "Review explicit surface.", "surfaceIds": ["first/api"]}+ ]+ write_saved_parent(tmp_path, parent, 200)+ expected_label = "Explicit"+ if checkpoint != "none":+ selected = copy.deepcopy(draft)+ if checkpoint == "updated":+ expected_label = "Updated explicit review"+ next(row for row in selected["coverage"]["surfaces"] if row.get("id") == "api")[+ "label"+ ] = expected_label+ output = Path(source["artifact_dir"])+ selected_path = write_checkpoint(output / "checkpoints", selected)+ os.utime(selected_path, ns=(300, 300))+ head = output / "checkpoint-head.json"+ head.write_text(json.dumps({"checkpoint": selected_path.name}))+ os.utime(head, ns=(400, 400))+ originals = {path: path.read_bytes() for path in tmp_path.rglob("*.json")}++ result = recover(tmp_path, [source])+ replay = recover(tmp_path, [source], frozen=result[0]["scan"]["preservedSources"])++ assert replay[2] == result[2]+ for documents in (result, replay):+ surfaces = {row["label"]: row for row in documents[2]["surfaces"]}+ assert len(surfaces) == 2+ assert surfaces[expected_label]["id"] != surfaces["Anonymous"]["id"]+ task = next(row for row in documents[2]["deferred"] if row["id"] == "parent-review")+ assert task["surfaceIds"] == [surfaces[expected_label]["id"]]+ assert "workerId" not in task+ assert all(path.read_bytes() == contents for path, contents in originals.items())++[email protected]("colliding_parent", [False, True])[email protected]("anonymous", [False, True])+ "task_kind,updated,anonymous_task",+ [+ ("parent", False, False),+ ("worker", False, False),+ ("worker", True, False),+ ("both", True, False),+ ("worker", False, True),+ ],+)+def test_parent_task_retains_surface_omitted_by_selected_worker_checkpoint(+ tmp_path, anonymous, colliding_parent, task_kind, updated, anonymous_task+):+ surface = {+ "label": "Worker review",+ "disposition": "needs_follow_up",+ "receiptRefs": ["artifacts/review.md"],+ }+ if not anonymous:+ surface["id"] = "api"+ worker_tasks = (+ [+ {+ **({} if anonymous_task else {"id": "review"}),+ "reason": "Worker review remains.",+ "surfaceIds": ["0" if anonymous else "api"],+ }+ ]+ if task_kind != "parent"+ else []+ )+ draft = saved_draft("coverage-scan", complete=True, surfaces=[surface], deferred=worker_tasks)+ source = worker(tmp_path, "first", draft)+ output = Path(source["artifact_dir"])+ parent_id = "first/0" if anonymous else "first/api"+ parent = copy.deepcopy(draft)+ parent["coverage"]["surfaces"][0].update(+ id=parent_id,+ workerId="first",+ receiptRefs=[f"{output.relative_to(tmp_path).as_posix()}/artifacts/review.md"],+ )+ parent["coverage"]["deferred"] = [+ {+ **task,+ "id": "first/0" if anonymous_task else "first/review",+ "workerId": "first",+ "surfaceIds": [parent_id],+ }+ for task in worker_tasks+ ]+ if task_kind != "worker":+ parent["coverage"]["deferred"].append(+ {"id": "parent-review", "reason": "Parent review remains.", "surfaceIds": [parent_id]}+ )+ if colliding_parent:+ parent["coverage"]["surfaces"].append(+ {+ "id": parent_id,+ "label": "Independent parent review",+ "disposition": "needs_follow_up",+ }+ )+ write_saved_parent(tmp_path, parent, 200)+ selected_tasks = [+ {**task, "reason": "Updated worker review." if updated else task["reason"]}+ for task in worker_tasks+ ]+ selected = write_checkpoint(+ output / "checkpoints",+ saved_draft("coverage-scan", complete=True, deferred=selected_tasks),+ )+ os.utime(selected, ns=(300, 300))+ head = output / "checkpoint-head.json"+ head.write_text(json.dumps({"checkpoint": selected.name}))+ os.utime(head, ns=(400, 400))+ originals = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}++ result = recover(tmp_path, [source])+ replay = recover(tmp_path, [source], frozen=result[0]["scan"]["preservedSources"])++ assert replay[2] == result[2]+ for documents in (result, replay):+ coverage = documents[2]+ retained = next(row for row in coverage["surfaces"] if row["label"] == "Worker review")+ assert len(coverage["surfaces"]) == 1 + colliding_parent+ assert retained["id"] == parent_id + ("-2" if colliding_parent else "")+ tasks = {row["id"]: row for row in coverage["deferred"]}+ if task_kind != "worker":+ assert tasks["parent-review"]["surfaceIds"] == [retained["id"]]+ assert "workerId" not in tasks["parent-review"]+ if task_kind != "parent":+ task = next(row for row in tasks.values() if row.get("workerId") == "first")+ assert task["surfaceIds"] == [retained["id"]]+ assert task["reason"] == selected_tasks[0]["reason"]+ assert (tmp_path / retained["receiptRefs"][0]).read_text() == "first source review\n"+ assert all(path.read_bytes() == contents for path, contents in originals.items())++[email protected]("colliding_parent", [False, True])+def test_parent_link_follows_updated_terminal_checkpoint(tmp_path, colliding_parent):+ surface = {"label": "Worker review", "disposition": "needs_follow_up"}+ draft = saved_draft("coverage-scan", complete=True, surfaces=[surface])+ source = worker(tmp_path, "first", draft)+ output = Path(source["artifact_dir"])+ older = copy.deepcopy(draft)+ older["coverage"]["surfaces"][0]["id"] = "restored"+ checkpoint = write_checkpoint(output / "checkpoints", older)+ os.utime(checkpoint, ns=(50, 50))+ selected = copy.deepcopy(older)+ selected["coverage"]["surfaces"][0]["notes"] = "Updated review."+ checkpoint = write_checkpoint(output / "checkpoints", selected)+ os.utime(checkpoint, ns=(300, 300))+ head = output / "checkpoint-head.json"+ head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ os.utime(head, ns=(400, 400))+ parent = copy.deepcopy(draft)+ parent["coverage"]["surfaces"][0].update(id="first/0", workerId="first")+ parent["coverage"]["deferred"] = [+ {"id": "parent-review", "reason": "Review remains.", "surfaceIds": ["first/0"]}+ ]+ if colliding_parent:+ parent["coverage"]["surfaces"].append(+ {**surface, "id": "first/restored", "label": "Parent review", "workerId": "first"}+ )+ parent["coverage"]["deferred"].append(+ {+ "id": "unrelated-review",+ "reason": "Review parent evidence.",+ "surfaceIds": ["first/restored"],+ "workerId": "first",+ }+ )+ write_saved_parent(tmp_path, parent, 200)+ originals = {p: p.read_bytes() for p in tmp_path.rglob("*.json")}+ result = recover(tmp_path, [source])+ replay = recover(tmp_path, [source], frozen=result[0]["scan"]["preservedSources"])+ assert replay[2] == result[2]+ for coverage in (result[2], replay[2]):+ assert len(coverage["surfaces"]) == 1 + colliding_parent+ retained = next(row for row in coverage["surfaces"] if row["label"] == "Worker review")+ assert retained["notes"] == "Updated review."+ assert retained["id"] == ("first/restored-2" if colliding_parent else "first/restored")+ task = next(row for row in coverage["deferred"] if row["id"] == "parent-review")+ assert task["surfaceIds"] == [retained["id"]]+ if colliding_parent:+ other = next(row for row in coverage["deferred"] if row["id"] == "unrelated-review")+ assert other["surfaceIds"] == ["first/restored"]+ assert all(p.read_bytes() == contents for p, contents in originals.items())+++def test_parent_link_recovery_leaves_malformed_collection_for_existing_finalizer(tmp_path):+ draft = saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[{"id": "api", "label": "API", "disposition": "needs_follow_up"}],+ )+ source = worker(tmp_path, "first", draft)+ parent = copy.deepcopy(draft)+ parent["coverage"]["surfaces"][0].update(id="first/api", workerId="first")+ parent["coverage"]["deferred"] = [+ {"id": "parent-review", "reason": "Review remains.", "surfaceIds": ["first/api"]}+ ]+ checkpoint = write_checkpoint(tmp_path / "checkpoints", parent)+ os.utime(checkpoint, ns=(150, 150))+ parent["coverage"]["surfaces"] = {"malformed": True}+ write_saved_parent(tmp_path, parent, 200)+ output = Path(source["artifact_dir"])+ selected = write_checkpoint(output / "checkpoints", saved_draft("coverage-scan", complete=True))+ os.utime(selected, ns=(300, 300))+ head = output / "checkpoint-head.json"+ head.write_text(json.dumps({"checkpoint": selected.name}))+ os.utime(head, ns=(400, 400))++ result = saved.merge_saved_results(+ tmp_path,+ "coverage-scan",+ saved_binding("deep_repository", repository="synthetic"),+ [source],+ [],+ stopped=True,+ reason="Source review stopped.",+ )++ assert result is not None+ assert result[2]["surfaces"] == {"malformed": True}+ assert any(row["id"] == "parent-review" for row in result[2]["deferred"])+++def test_parent_only_task_remaps_anonymous_surface_links_once(tmp_path):+ draft = saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[+ {"candidateId": "1", "label": "API", "disposition": "needs_follow_up"},+ {"label": "Other", "disposition": "needs_follow_up"},+ ],+ )+ source = worker(tmp_path, "first", draft)+ parent = copy.deepcopy(draft)+ parent["coverage"]["surfaces"][0].update(id="first/0", workerId="first")+ parent["coverage"]["surfaces"][1].update(id="first/1", workerId="first")+ parent["coverage"]["deferred"] = [+ {"id": "parent-review", "reason": "Review remains.", "surfaceIds": ["first/0", "first/1"]}+ ]+ write_saved_parent(tmp_path, parent, 200)+ result = recover(tmp_path, [source])+ replay = recover(tmp_path, [source], frozen=result[0]["scan"]["preservedSources"])+ assert replay[2] == result[2]+ for coverage in (result[2], replay[2]):+ surfaces = {row["label"]: row["id"] for row in coverage["surfaces"]}+ task = next(row for row in coverage["deferred"] if row["id"] == "parent-review")+ assert task["surfaceIds"] == [surfaces["API"], surfaces["Other"]]+ assert surfaces["API"] != surfaces["Other"]++[email protected]("repair_only", [False, True])+def test_parent_link_follows_replaced_anonymous_surface(tmp_path, repair_only):+ surface = {+ "label": "API",+ "disposition": "no_issue_found" if repair_only else "needs_follow_up",+ "receiptRefs": ["artifacts/missing.md"] if repair_only else [],+ }+ draft = saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[surface],+ deferred=[]+ if repair_only+ else [{"id": "review", "reason": "Review remains.", "surfaceIds": ["restored"]}],+ )+ source = worker(tmp_path, "first", draft)+ output = Path(source["artifact_dir"])+ older = copy.deepcopy(draft)+ older["coverage"]["surfaces"][0]["id"] = "restored"+ checkpoint = write_checkpoint(output / "checkpoints", older)+ os.utime(checkpoint, ns=(50, 50))+ parent = copy.deepcopy(draft)+ parent["coverage"]["surfaces"][0].update(+ id="first/0",+ workerId="first",+ receiptRefs=[+ f"{output.relative_to(tmp_path).as_posix()}/{ref}" for ref in surface["receiptRefs"]+ ],+ )+ parent["coverage"]["deferred"] = [+ {"id": "parent-review", "reason": "Parent review remains.", "surfaceIds": ["first/0"]}+ ]+ write_saved_parent(tmp_path, parent, 200)+ replacement = saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[+ {+ **surface,+ "id": "restored",+ "disposition": "no_issue_found",+ "receiptRefs": ["artifacts/review.md"],+ }+ ],+ closures=[] if repair_only else [{"id": "review", "reason": "Worker review completed."}],+ )+ checkpoint = write_checkpoint(output / "checkpoints", replacement)+ os.utime(checkpoint, ns=(300, 300))+ head = output / "checkpoint-head.json"+ head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ os.utime(head, ns=(400, 400))+ originals = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}+ frozen = None+ for _ in range(2):+ warnings = []+ result = saved.merge_saved_results(+ tmp_path,+ "coverage-scan",+ saved_binding("deep_repository", repository="synthetic"),+ [source],+ warnings,+ stopped=True,+ reason="Source review stopped.",+ project_worker_coverage=True,+ frozen_source_digests=frozen,+ )+ assert result is not None+ frozen = result[0]["scan"]["preservedSources"]+ coverage = result[2]+ assert [row["id"] for row in coverage["surfaces"]] == ["first/restored"]+ task = next(row for row in coverage["deferred"] if row["id"] == "parent-review")+ assert task["surfaceIds"] == ["first/restored"]+ assert (tmp_path / coverage["surfaces"][0]["receiptRefs"][0]).read_text() == (+ "first source review\n"+ )+ assert all(path.read_bytes() == contents for path, contents in originals.items())+++def test_parent_only_task_links_to_surface_with_retained_finding_history(tmp_path):+ template = tmp_path / "template"+ template.mkdir()+ write_completed_contract(template, "coverage-scan", tmp_path, target_kind="git_revision")+ finding = json.loads((template / "findings.json").read_text())["findings"][0]+ finding["provenance"].update(workerId="first", candidateId="review")+ draft = saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[{"candidateId": "review", "label": "API", "disposition": "rejected"}],+ )+ source = worker(tmp_path, "first", draft)+ parent = copy.deepcopy(draft)+ parent["findings"] = [finding]+ parent["coverage"]["surfaces"][0].update(id="first/0", workerId="first")+ parent["coverage"]["deferred"] = [+ {"id": "parent-review", "reason": "Review remains.", "surfaceIds": ["first/0"]}+ ]+ write_saved_parent(tmp_path, parent, 200)+ manifest = tmp_path / "scan-manifest.json"+ parent_manifest = json.loads((template / "scan-manifest.json").read_text())+ parent_manifest["scan"].update(+ complete=True, sealedAt="2026-01-01T00:00:00Z", status="interrupted"+ )+ manifest.write_text(json.dumps(parent_manifest))+ result = recover(tmp_path, [source])+ surface = result[2]["surfaces"][0]+ task = next(row for row in result[2]["deferred"] if row["id"] == "parent-review")+ assert len(surface["previousFindings"]) == 1+ assert task["surfaceIds"] == [surface["id"]]++[email protected]("worker_metadata", ["first", ["first"], {"hint": "first"}])+def test_parent_evidence_is_retained_without_matching_saved_worker_source(+ tmp_path, worker_metadata+):+ parent = saved_draft(+ "coverage-scan",+ deferred=[{"id": "first/review", "workerId": worker_metadata, "reason": "Review remains."}],+ )+ write_saved_parent(tmp_path, parent, 100)+ result = recover(tmp_path, [])[2]+ assert result["deferred"][0] == parent["coverage"]["deferred"][0]++[email protected]("project", [False, True])+ "question",+ [+ " Which deployments enable the handler? ",+ {+ "question": "Which deployments enable the handler?",+ "id": "question-1",+ "surfaceIds": ["extension-surface"],+ "receiptRefs": ["extension-receipt"],+ },+ ],+)+def test_question_only_aggregate_reconciles_once_without_rewriting_extensions(+ tmp_path, project, question+):+ draft = saved_draft("coverage-scan", complete=True)+ draft["coverage"]["openQuestions"] = [copy.deepcopy(question)]+ source = worker(tmp_path, "first", draft)+ parent = copy.deepcopy(draft)+ expected = {+ **({"question": question} if isinstance(question, str) else question),+ "workerId": "first",+ }+ parent["coverage"]["openQuestions"] = [copy.deepcopy(expected)]+ write_saved_parent(tmp_path, parent, 200)+ result = recover(tmp_path, [source], project=project)+ expected["question"] = expected["question"].strip()+ assert result[2]["openQuestions"] == [expected]+ replay = recover(+ tmp_path, [source], project=project, frozen=result[0]["scan"]["preservedSources"]+ )+ assert replay[2]["openQuestions"] == [expected]+++def test_distinct_anonymous_checkpoint_tasks_survive_reconciliation_before_projection(tmp_path):+ output = tmp_path / "artifacts/deep_discovery/workers/first/output"+ output.mkdir(parents=True)+ for index, reason in enumerate(["Review authentication.", "Review authorization."], 1):+ checkpoint = write_checkpoint(+ output / "checkpoints",+ saved_draft("coverage-scan", deferred=[{"reason": reason}], complete=False),+ )+ os.utime(checkpoint, ns=(index * 100, index * 100))+ head = output / "checkpoint-head.json"+ head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ os.utime(head, ns=(250, 250))+ source = saved_discovery_worker(output, "first", 1)+ result = recover(tmp_path, [source], project=True)+ replay = recover(tmp_path, [source], project=True, frozen=result[0]["scan"]["preservedSources"])+ for documents in [result, replay]:+ tasks = [row for row in documents[2]["deferred"] if row.get("workerId") == "first"]+ assert {row["reason"] for row in tasks} == {+ "Review authentication.",+ "Review authorization.",+ }+ assert len({row["id"] for row in tasks}) == 2+ assert all(row["id"].startswith("first/saved-") for row in tasks)+ assert replay[2] == result[2]+++def test_legacy_duplicate_tasks_match_the_accepted_aggregate_and_keep_distinct_output_ids(tmp_path):+ tasks = [+ {"id": "review", "reason": "Review authentication."},+ {"id": "review", "reason": "Review authorization."},+ {"id": "review-2", "reason": "Review routing."},+ ]+ draft = saved_draft("coverage-scan", deferred=tasks, complete=True)+ source = worker(tmp_path, "first", draft)+ parent = copy.deepcopy(draft)+ parent["coverage"]["deferred"] = [+ {**row, "workerId": "first", "id": identity}+ for row, identity in zip(+ tasks, ["first/review", "first/review-3", "first/review-2"], strict=True+ )+ ]+ write_saved_parent(tmp_path, parent, 200)+ result = recover(tmp_path, [source])+ replay = recover(tmp_path, [source], frozen=result[0]["scan"]["preservedSources"])+ for documents in [result, replay]:+ retained = [row for row in documents[2]["deferred"] if row.get("workerId") == "first"]+ assert retained == parent["coverage"]["deferred"]+ assert replay[2] == result[2]+++ "receipt", ["artifacts/review.md", "artifacts/./review.md", "artifacts//review.md"]+)[email protected]("exclusion_id", [{}, {"id": None}, {"id": 7}, {"id": "external"}])+def test_projection_normalizes_surface_receipts_without_rewriting_extension_metadata(+ tmp_path, receipt, exclusion_id+):+ draft = saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[+ {"id": "api", "label": "API", "disposition": "no_issue_found", "receiptRefs": [receipt]}+ ],+ deferred=[+ {+ "id": "review",+ "reason": "Review another route.",+ "surfaceIds": ["api"],+ "receiptRefs": {"external": "opaque metadata"},+ }+ ],+ )+ draft["coverage"]["explicitExclusions"] = [+ {+ **exclusion_id,+ "pattern": "vendor/**",+ "reason": "External code.",+ "receiptRefs": ["opaque/missing.md"],+ "surfaceIds": ["opaque-id"],+ }+ ]+ source = worker(tmp_path, "first", draft)+ parent = copy.deepcopy(draft)+ for field in ("surfaces", "explicitExclusions", "deferred"):+ for index, row in enumerate(parent["coverage"][field]):+ row["workerId"] = "first"+ identity = row["id"] if isinstance(row.get("id"), str) else str(index)+ row["id"] = f"first/{identity}"+ parent["coverage"]["surfaces"][0]["receiptRefs"] = [+ "artifacts/deep_discovery/workers/first/output/artifacts/review.md"+ ]+ parent["coverage"]["deferred"][0]["surfaceIds"] = ["first/api"]+ write_saved_parent(tmp_path, parent, 200)+ coverage = recover(tmp_path, [source])[2]+ assert len(coverage["surfaces"]) == 1+ assert (+ coverage["surfaces"][0]["receiptRefs"] == parent["coverage"]["surfaces"][0]["receiptRefs"]+ )+ assert len(coverage["explicitExclusions"]) == 1+ exclusion = coverage["explicitExclusions"][0]+ expected = parent["coverage"]["explicitExclusions"][0]+ if isinstance(exclusion_id.get("id"), str):+ assert exclusion == expected+ else:+ assert {key: value for key, value in exclusion.items() if key != "id"} == {+ key: value for key, value in expected.items() if key != "id"+ }+ task = next(row for row in coverage["deferred"] if row["id"] == "first/review")+ assert task["receiptRefs"] == {"external": "opaque metadata"}+ assert task["surfaceIds"] == ["first/api"]+ assert json.loads((Path(source["artifact_dir"]) / "result.json").read_text()) == draft++[email protected]("reverse_sources", [False, True])[email protected]("reverse_surfaces", [False, True])+def test_current_receipt_spelling_precedes_historical_receipts(+ tmp_path, reverse_sources, reverse_surfaces+):+ prefix = "artifacts/deep_discovery/workers/first/output/artifacts/"+ directory = tmp_path / prefix+ directory.mkdir(parents=True)+ for name in ("review.md", "Review.md", "new-review.md"):+ (directory / name).write_text(f"Evidence from {name}.\n")+ originals = {path: path.read_bytes() for path in directory.iterdir()}++ def surface(identity, disposition, receipt):+ return {+ "id": identity,+ "label": identity,+ "disposition": disposition,+ "receiptRefs": [prefix + receipt],+ }++ old = {+ "coverage": {+ "surfaces": [+ surface("reader", "needs_follow_up", "review.md"),+ surface("writer", "needs_follow_up", "Review.md"),+ ]+ }+ }+ current = {+ "coverage": {+ "surfaces": [+ surface("reader", "no_issue_found", "new-review.md"),+ surface("writer", "no_issue_found", "Review.md"),+ ]+ }+ }+ if reverse_surfaces:+ for draft in (old, current):+ draft["coverage"]["surfaces"].reverse()+ sources = [("old", old, "first"), ("current", current, "first")]+ if reverse_sources:+ sources.reverse()+ task = {"id": "review", "reason": "Review remains.", "surfaceIds": ["reader", "writer"]}+ schema = json.loads(+ (Path(saved.__file__).resolve().parent.parent / "schemas/coverage.schema.json").read_text()+ )["properties"]["surfaces"]["items"]+ warnings = []+ _, updates = saved._generic_surface_updates(+ sources,+ {"old": (0, 0), "current": (1, 0)},+ {("first", "review"): ((1, 0), {"id": "review", "reason": "Review completed."}, "current")},+ {},+ {},+ set(),+ schema,+ {"old": [task], "current": []},+ set(),+ surface_is_retained=lambda _relative, _owner, _row: True,+ eligible_receipt_sources={"old", "current"},+ receipt_root=tmp_path,+ receipt_warnings=warnings,+ )+ receipts = {row["id"]: row["receiptRefs"] for _, row, _ in updates}+ assert receipts == {"reader": [prefix + "new-review.md"], "writer": [prefix + "Review.md"]}+ assert any("duplicate artifact path" in warning for warning in warnings)+ scan = {+ "completedAt": "synthetic-time",+ "sealedAt": "synthetic-time",+ "artifacts": [+ {"path": ref, "sha256": hashlib.sha256((tmp_path / ref).read_bytes()).hexdigest()}+ for refs in receipts.values()+ for ref in refs+ ],+ }+ finalizer._validate_existing_seal(tmp_path, scan)+ assert all(path.read_bytes() == contents for path, contents in originals.items())++[email protected]("split_surfaces", [False, True])+def test_stopped_publication_recovers_unrepaired_failed_worker_receipt_collisions(+ tmp_path, split_surfaces+):+ target = tmp_path / "target"+ target.mkdir()+ (target / "app.py").write_text("synthetic_handler()\n")+ contract = tmp_path / "contract"+ contract.mkdir()+ write_completed_contract(contract, "coverage-scan", target, coverage_mode="deep_repository")+ fixture_manifest = json.loads((contract / "scan-manifest.json").read_text())+ finding = json.loads((contract / "findings.json").read_text())["findings"][0]+ binding = {+ "scanId": "coverage-scan",+ "startedAt": fixture_manifest["scan"]["startedAt"],+ "completedAt": fixture_manifest["scan"]["completedAt"],+ "producer": fixture_manifest["scan"]["producer"],+ "status": "interrupted",+ "allowedTargetKinds": [fixture_manifest["scan"]["target"]["kind"]],+ "target": fixture_manifest["scan"]["target"],+ "scope": fixture_manifest["scan"]["scope"],+ "coverageMode": "deep_repository",+ }+ root = tmp_path / "scan"+ root.mkdir(mode=0o700)+ good = saved_draft(+ "coverage-scan",+ complete=True,+ findings=[finding],+ surfaces=[+ {+ "id": "reviewed",+ "label": "Reviewed surface",+ "disposition": "reported",+ "receiptRefs": [],+ }+ ],+ )+ accepted = worker(root, "accepted", good)+ accepted["status"] = "succeeded"+ failed_rows = [+ {+ "id": "reader",+ "label": "Reader",+ "disposition": "no_issue_found",+ "receiptRefs": ["artifacts/review.md"],+ },+ {+ "id": "writer",+ "label": "Writer",+ "disposition": "no_issue_found",+ "receiptRefs": ["artifacts/Review.md"],+ },+ ]+ if not split_surfaces:+ failed_rows[0]["receiptRefs"].extend(failed_rows.pop()["receiptRefs"])+ failed = worker(+ root, "failed", saved_draft("coverage-scan", complete=False, surfaces=failed_rows)+ )+ failed["status"] = "failed"+ (Path(failed["artifact_dir"]) / "artifacts/Review.md").write_text(+ "Other saved source review.\n"+ )+ parent = copy.deepcopy(good)+ parent["coverage"]["surfaces"][0].update(id="accepted/reviewed", workerId="accepted")+ write_saved_parent(root, parent, 300)+ originals = {p: p.read_bytes() for p in (root / "artifacts").rglob("*") if p.is_file()}+ warnings = []+ recovered = saved.merge_saved_results(+ root,+ "coverage-scan",+ binding,+ [accepted, failed],+ warnings,+ stopped=True,+ reason="Review stopped.",+ )+ assert recovered is not None+ prepared = finalizer._prepare_scan_finalization(+ root,+ completion_binding=binding,+ recover_unsealed=True,+ expected_coverage_mode="deep_repository",+ draft_documents=recovered,+ )+ _, published_findings, published_coverage = finalizer._write_prepared_scan_finalization(+ prepared+ )+ assert len(published_findings["findings"]) == 1+ assert published_findings["findings"][0]["title"] == finding["title"]+ assert "completeness" not in published_coverage+ failed_surfaces = [+ row for row in published_coverage["surfaces"] if row.get("workerId") == "failed"+ ]+ assert any(row["disposition"] == "needs_follow_up" for row in failed_surfaces)+ assert any("duplicate artifact path" in warning for warning in prepared.warnings)+ assert (root / "report.md").is_file()+ manifest = json.loads((root / "scan-manifest.json").read_text())+ assert manifest["scan"]["status"] == "interrupted"+ assert isinstance(manifest["scan"]["sealedAt"], str)+ assert all(path.read_bytes() == contents for path, contents in originals.items())++[email protected]("reverse", [False, True])+def test_missing_historical_receipt_does_not_hide_valid_case_variant(tmp_path, reverse):+ refs = ["artifacts/review.md", "artifacts/Review.md"]+ if reverse:+ refs.reverse()+ surface = {"id": "api", "label": "API", "disposition": "no_issue_found"}+ old = saved_draft("coverage-scan", complete=True, surfaces=[{**surface, "receiptRefs": refs}])+ source = worker(tmp_path, "first", old)+ output = Path(source["artifact_dir"])+ (output / "artifacts/review.md").unlink()+ (output / "artifacts/Review.md").write_text("Retained historical evidence.\n")+ if (output / "artifacts/review.md").exists():+ pytest.skip("This regression requires case-sensitive receipt paths.")+ (output / "artifacts/replacement.md").write_text("Current review evidence.\n")+ prefix = output.relative_to(tmp_path).as_posix() + "/"+ parent = copy.deepcopy(old)+ parent["coverage"]["surfaces"] = [+ {+ **surface,+ "id": "first/api",+ "workerId": "first",+ "receiptRefs": [prefix + ref for ref in refs],+ }+ ]+ write_saved_parent(tmp_path, parent, 200)+ current = saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[{**surface, "receiptRefs": ["artifacts/replacement.md"]}],+ )+ checkpoint = write_checkpoint(output / "checkpoints", current)+ os.utime(checkpoint, ns=(300, 300))+ warnings = []+ documents = saved.merge_saved_results(+ tmp_path,+ "coverage-scan",+ saved_binding("deep_repository", repository="synthetic"),+ [source],+ warnings,+ stopped=True,+ reason="Interrupted retry before checkpoint-head publication.",+ )+ assert documents is not None+ retained = documents[2]["surfaces"][0]["receiptRefs"]+ assert set(retained) == {prefix + "artifacts/Review.md", prefix + "artifacts/replacement.md"}+ assert any("Replaced invalid saved coverage receipt" in warning for warning in warnings)+ scan = {+ "completedAt": "synthetic-time",+ "sealedAt": "synthetic-time",+ "artifacts": [+ {"path": ref, "sha256": hashlib.sha256((tmp_path / ref).read_bytes()).hexdigest()}+ for ref in retained+ ],+ }+ finalizer._validate_existing_seal(tmp_path, scan)++[email protected]("projected_parent", [False, True])[email protected]("reverse", [False, True])+def test_superseded_surface_cannot_reserve_historical_receipt(tmp_path, projected_parent, reverse):+ reader = {+ "id": "reader",+ "label": "Reader",+ "disposition": "needs_follow_up",+ "receiptRefs": ["artifacts/review.md"],+ }+ writer = {+ "id": "writer",+ "label": "Writer",+ "disposition": "no_issue_found",+ "receiptRefs": ["artifacts/Review.md"],+ }+ task = {"id": "review-reader", "reason": "Review reader.", "surfaceIds": ["reader"]}+ surfaces = [writer, reader] if reverse else [reader, writer]+ old = saved_draft("coverage-scan", complete=True, surfaces=surfaces, deferred=[task])+ source = worker(tmp_path, "first", old)+ output = Path(source["artifact_dir"])+ if (output / "artifacts/Review.md").exists():+ pytest.skip("This regression requires case-sensitive receipt paths.")+ (output / "artifacts/Review.md").write_text("Superseded writer evidence.\n")+ archive = output.parent / "attempts/attempt-01"+ archive.parent.mkdir()+ output.rename(archive)+ (output / "artifacts").mkdir(parents=True)+ (output / "artifacts/replacement.md").write_text("Completed reader evidence.\n")+ current = saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[+ {**reader, "disposition": "no_issue_found", "receiptRefs": ["artifacts/replacement.md"]}+ ],+ closures=[{"id": "review-reader", "reason": "Reader review complete."}],+ )+ (output / "result.json").write_text(json.dumps(current))+ os.utime(output / "result.json", ns=(300, 300))+ prefix = archive.relative_to(tmp_path).as_posix() + "/"+ if projected_parent:+ parent = copy.deepcopy(old)+ parent["coverage"]["surfaces"] = [+ {+ **row,+ "id": "first/" + row["id"],+ "workerId": "first",+ "receiptRefs": [prefix + ref for ref in row["receiptRefs"]],+ }+ for row in surfaces+ ]+ parent["coverage"]["deferred"] = [+ {+ **task,+ "id": "first/review-reader",+ "workerId": "first",+ "surfaceIds": ["first/reader"],+ }+ ]+ write_saved_parent(tmp_path, parent, 200)+ originals = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}+ warnings = []+ documents = saved.merge_saved_results(+ tmp_path,+ "coverage-scan",+ saved_binding("deep_repository", repository="synthetic"),+ [source],+ warnings,+ stopped=True,+ reason="Source review stopped.",+ project_worker_coverage=not projected_parent,+ )+ assert documents is not None+ retained = documents[2]["surfaces"]+ assert [row["id"] for row in retained] == ["first/reader"]+ assert set(retained[0]["receiptRefs"]) == {+ prefix + "artifacts/review.md",+ output.relative_to(tmp_path).as_posix() + "/artifacts/replacement.md",+ }+ assert prefix + "artifacts/review.md" in finalizer._supplementary_artifact_refs(+ documents[0]["scan"], documents[2]+ )+ assert not warnings+ assert all(path.read_bytes() == contents for path, contents in originals.items())++[email protected]("scenario", ["superseded-omitted", "unselected-metadata"])[email protected]("missing", [False, True])+def test_receipt_repair_preserves_selected_surface_metadata(tmp_path, scenario, missing):+ source = worker(tmp_path, "first", saved_draft("coverage-scan", complete=True))+ output = Path(source["artifact_dir"])+ (output / "artifacts/accepted.md").write_text("Accepted completed review.\n")+ (output / "artifacts/unselected.md").write_text("Unselected interrupted observation.\n")+ surface = {+ "id": "api",+ "label": "Archived label",+ "notes": "Archived notes",+ "disposition": "no_issue_found",+ "receiptRefs": ["artifacts/missing.md"],+ }+ if scenario == "superseded-omitted":+ for modified, row in [+ (100, surface),+ (+ 200,+ {+ **surface,+ "label": "Intermediate label",+ "receiptRefs": ["artifacts/accepted.md"],+ },+ ),+ ]:+ checkpoint = write_checkpoint(+ output / "checkpoints", saved_draft("coverage-scan", surfaces=[row])+ )+ os.utime(checkpoint, ns=(modified, modified))+ missing_path = output / "artifacts/missing.md"+ else:+ archive = output.parent / "attempts/attempt-01"+ (archive / "artifacts").mkdir(parents=True)+ (archive / "result.json").write_text(+ json.dumps(saved_draft("coverage-scan", complete=True, surfaces=[surface]))+ )+ os.utime(archive / "result.json", ns=(100, 100))+ missing_path = archive / "artifacts/missing.md"+ accepted = saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[+ {+ **surface,+ "label": "Accepted label",+ "notes": "Accepted notes",+ "receiptRefs": ["artifacts/accepted.md"],+ }+ ],+ )+ checkpoint = write_checkpoint(output / "checkpoints", accepted)+ os.utime(checkpoint, ns=(200, 200))+ (output / "checkpoint-head.json").write_text(json.dumps({"checkpoint": checkpoint.name}))+ os.utime(output / "checkpoint-head.json", ns=(250, 250))+ (output / "result.json").write_text(json.dumps(accepted))+ pending = saved_draft(+ "coverage-scan",+ surfaces=[+ {+ **surface,+ "label": "Unselected label",+ "notes": "Unselected notes",+ "receiptRefs": ["artifacts/unselected.md"],+ }+ ],+ )+ checkpoint = write_checkpoint(output / "checkpoints", pending)+ os.utime(checkpoint, ns=(400, 400))+ source["attempt"] = 2+ os.utime(output / "result.json", ns=(300, 300))+ if not missing:+ missing_path.write_text("Existing historical receipt.\n")+ originals = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}+ documents = saved.merge_saved_results(+ tmp_path,+ "coverage-scan",+ saved_binding("deep_repository", repository="synthetic"),+ [source],+ [],+ stopped=True,+ reason="Source review stopped.",+ project_worker_coverage=True,+ )+ assert documents is not None+ surfaces = documents[2]["surfaces"]+ if scenario == "superseded-omitted":+ assert surfaces == []+ else:+ assert len(surfaces) == 1+ assert surfaces[0]["label"] == "Accepted label"+ assert surfaces[0]["notes"] == "Accepted notes"+ assert (+ output.relative_to(tmp_path).as_posix() + "/artifacts/accepted.md"+ in surfaces[0]["receiptRefs"]+ )+ assert not any(ref.endswith("/unselected.md") for ref in surfaces[0]["receiptRefs"])+ assert all(path.read_bytes() == contents for path, contents in originals.items())++[email protected]("accepted_receipt_exists", [False, True])+def test_receipt_repair_does_not_promote_unselected_checkpoint_evidence(+ tmp_path, accepted_receipt_exists+):+ surface = {+ "id": "api",+ "label": "Accepted review",+ "disposition": "no_issue_found",+ "receiptRefs": ["artifacts/accepted.md"],+ }+ accepted = saved_draft("coverage-scan", complete=True, surfaces=[surface])+ source = worker(tmp_path, "first", accepted)+ output = Path(source["artifact_dir"])+ if accepted_receipt_exists:+ (output / "artifacts/accepted.md").write_text("Accepted receipt.\n")+ historical = write_checkpoint(+ output / "checkpoints",+ saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[{**surface, "receiptRefs": ["artifacts/missing.md"]}],+ ),+ )+ os.utime(historical, ns=(100, 100))+ selected = write_checkpoint(output / "checkpoints", accepted)+ os.utime(selected, ns=(200, 200))+ (output / "checkpoint-head.json").write_text(json.dumps({"checkpoint": selected.name}))+ os.utime(output / "checkpoint-head.json", ns=(250, 250))+ os.utime(output / "result.json", ns=(300, 300))+ (output / "artifacts/unselected.md").write_text("Unselected interrupted observation.\n")+ pending = write_checkpoint(+ output / "checkpoints",+ saved_draft(+ "coverage-scan",+ complete=False,+ surfaces=[{**surface, "receiptRefs": ["artifacts/unselected.md"]}],+ ),+ )+ os.utime(pending, ns=(400, 400))+ originals = {path: path.read_bytes() for path in output.rglob("*") if path.is_file()}+ frozen = None+ for _ in range(2):+ warnings = []+ documents = saved.merge_saved_results(+ tmp_path,+ "coverage-scan",+ saved_binding("deep_repository", repository="synthetic"),+ [source],+ warnings,+ stopped=True,+ reason="Source review stopped.",+ project_worker_coverage=True,+ frozen_source_digests=frozen,+ )+ assert documents is not None+ frozen = documents[0]["scan"]["preservedSources"]+ finalizer._recover_unsealed_coverage(+ documents[2],+ Path(saved.__file__).resolve().parent.parent / "schemas",+ tmp_path,+ warnings,+ [],+ )+ retained = documents[2]["surfaces"]+ assert len(retained) == 1+ assert retained[0]["disposition"] == (+ "no_issue_found" if accepted_receipt_exists else "needs_follow_up"+ )+ assert retained[0]["receiptRefs"] == (+ [output.relative_to(tmp_path).as_posix() + "/artifacts/accepted.md"]+ if accepted_receipt_exists+ else []+ )+ assert all(path.read_bytes() == contents for path, contents in originals.items())++[email protected]("reverse", [False, True])[email protected]("checkpoint", ["absent", "same", "conflicting", "distinct"])+def test_unselected_receipt_spelling_cannot_replace_accepted_evidence(+ tmp_path, reverse, checkpoint+):+ accepted_name, unselected_name = (+ ("Review.md", "review.md") if reverse else ("review.md", "Review.md")+ )+ surface = {+ "id": "api",+ "label": "Accepted review",+ "disposition": "no_issue_found",+ "receiptRefs": [f"artifacts/{accepted_name}"],+ }+ accepted = saved_draft("coverage-scan", complete=True, surfaces=[surface])+ source = worker(tmp_path, "first", accepted)+ output = Path(source["artifact_dir"])+ for name, text in (+ (accepted_name, "Accepted receipt bytes."),+ (unselected_name, "Unselected receipt bytes."),+ ("distinct.md", "Independent receipt bytes."),+ ):+ (output / "artifacts" / name).write_text(text)+ selected = write_checkpoint(output / "checkpoints", accepted)+ os.utime(selected, ns=(200, 200))+ (output / "checkpoint-head.json").write_text(json.dumps({"checkpoint": selected.name}))+ os.utime(output / "checkpoint-head.json", ns=(250, 250))+ os.utime(output / "result.json", ns=(300, 300))+ if checkpoint != "absent":+ name = {+ "same": accepted_name,+ "conflicting": unselected_name,+ "distinct": "distinct.md",+ }[checkpoint]+ pending = write_checkpoint(+ output / "checkpoints",+ saved_draft(+ "coverage-scan",+ surfaces=[{**surface, "receiptRefs": [f"artifacts/{name}"]}],+ ),+ )+ os.utime(pending, ns=(400, 400))+ originals = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}+ documents = recover(tmp_path, [source], project=True)+ replay = recover(+ tmp_path, [source], project=True, frozen=documents[0]["scan"]["preservedSources"]+ )+ for recovered in (documents, replay):+ assert recovered[2]["surfaces"] == [+ {+ **surface,+ "id": "first/api",+ "workerId": "first",+ "receiptRefs": [+ f"{output.relative_to(tmp_path).as_posix()}/artifacts/{accepted_name}"+ ],+ }+ ]+ assert all(path.read_bytes() == content for path, content in originals.items())++[email protected]("unselected", [False, True])+def test_non_emitted_repair_cannot_reserve_another_surfaces_receipt(tmp_path, unselected):+ def surface(identity, names):+ return {+ "id": identity,+ "label": identity,+ "disposition": "no_issue_found",+ "receiptRefs": [f"artifacts/{name}" for name in names],+ }++ accepted = saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[surface("reader", ["reader.md"]), surface("writer", ["writer.md"])],+ )+ source = worker(tmp_path, "first", accepted)+ output = Path(source["artifact_dir"])+ for name in ["reader.md", "writer.md", "review.md", "Review.md"]:+ (output / "artifacts" / name).write_text(f"Evidence from {name}.")+ history = saved_draft(+ "coverage-scan", complete=True, surfaces=[surface("writer", ["Review.md", "missing.md"])]+ )+ for modified, draft in [(100, history), (200, accepted)]:+ checkpoint = write_checkpoint(output / "checkpoints", draft)+ os.utime(checkpoint, ns=(modified, modified))+ (output / "checkpoint-head.json").write_text(json.dumps({"checkpoint": checkpoint.name}))+ os.utime(output / "checkpoint-head.json", ns=(250, 250))+ os.utime(output / "result.json", ns=(300, 300))+ if unselected:+ checkpoint = write_checkpoint(+ output / "checkpoints",+ saved_draft("coverage-scan", surfaces=[surface("reader", ["review.md"])]),+ )+ os.utime(checkpoint, ns=(400, 400))+ originals = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}+ warnings = []+ documents = saved.merge_saved_results(+ tmp_path,+ "coverage-scan",+ saved_binding("deep_repository", repository="synthetic"),+ [source],+ warnings,+ stopped=True,+ reason="Source review stopped.",+ project_worker_coverage=True,+ )+ assert documents is not None+ surfaces = {row["id"]: row for row in documents[2]["surfaces"]}+ prefix = output.relative_to(tmp_path).as_posix() + "/artifacts/"+ assert surfaces["first/reader"]["receiptRefs"] == [prefix + "reader.md"]+ assert set(surfaces["first/writer"]["receiptRefs"]) == {+ prefix + "writer.md",+ prefix + "Review.md",+ }+ assert all("missing.md" in warning for warning in warnings)+ assert all(path.read_bytes() == content for path, content in originals.items())++[email protected]("reverse", [False, True])+ "metadata",+ [+ "valid",+ "anonymous",+ "anonymous-candidate",+ "duplicate-id",+ "missing-label",+ "invalid-label",+ "disposition",+ "receipt",+ "empty-id",+ ],+)+def test_only_recoverable_surface_metadata_reserves_receipt_spelling(tmp_path, reverse, metadata):+ current_name, historical_name = (+ ("review.md", "Review.md") if reverse else ("Review.md", "review.md")+ )+ selected = {+ "id": "selected",+ "label": "Current selected review",+ "disposition": "no_issue_found",+ "receiptRefs": [f"artifacts/{current_name}"],+ }+ if metadata in {"anonymous", "anonymous-candidate"}:+ selected.pop("id")+ if metadata == "anonymous-candidate":+ selected["candidateId"] = "selected"+ elif metadata == "empty-id":+ selected["id"] = ""+ elif metadata == "missing-label":+ selected.pop("label")+ elif metadata == "invalid-label":+ selected["label"] = 7+ elif metadata == "disposition":+ selected["disposition"] = "unverified"+ elif metadata == "receipt":+ selected["receiptRefs"].append(7)+ writer = {+ "id": "writer",+ "label": "Retained review",+ "disposition": "no_issue_found",+ "receiptRefs": ["artifacts/replacement.md"],+ }+ surfaces = [selected, writer]+ if metadata == "duplicate-id":+ surfaces.insert(+ 0,+ {+ **selected,+ "label": "Independent selected review",+ "receiptRefs": ["artifacts/other.md"],+ },+ )+ accepted = saved_draft("coverage-scan", complete=True, surfaces=surfaces)+ source = worker(tmp_path, "first", accepted)+ output = Path(source["artifact_dir"])+ for name in [current_name, historical_name, "replacement.md", "other.md"]:+ (output / "artifacts" / name).write_text(f"Evidence from {name}.\n")+ history = saved_draft(+ "coverage-scan",+ complete=True,+ surfaces=[+ {**writer, "receiptRefs": [f"artifacts/{historical_name}", "artifacts/missing.md"]}+ ],+ )+ for modified, draft in [(100, history), (200, accepted)]:+ checkpoint = write_checkpoint(output / "checkpoints", draft)+ os.utime(checkpoint, ns=(modified, modified))+ (output / "checkpoint-head.json").write_text(json.dumps({"checkpoint": checkpoint.name}))+ os.utime(output / "checkpoint-head.json", ns=(250, 250))+ os.utime(output / "result.json", ns=(300, 300))+ originals = {p: p.read_bytes() for p in output.rglob("*") if p.is_file()}+ warnings = []+ documents = saved.merge_saved_results(+ tmp_path,+ "coverage-scan",+ saved_binding("deep_repository", repository="synthetic"),+ [source],+ warnings,+ stopped=True,+ reason="Source review stopped.",+ project_worker_coverage=True,+ )+ assert documents is not None+ finalizer._recover_unsealed_coverage(+ documents[2],+ Path(saved.__file__).resolve().parent.parent / "schemas",+ tmp_path,+ warnings,+ [],+ )+ rows = {row["id"]: row for row in documents[2]["surfaces"]}+ prefix = output.relative_to(tmp_path).as_posix() + "/artifacts/"+ expected = {prefix + "replacement.md"}+ if metadata in {"missing-label", "invalid-label"}:+ expected.add(prefix + historical_name)+ assert "first/selected" not in rows+ assert any("Skipped malformed coverage surface" in warning for warning in warnings)+ else:+ selected_row = next(row for row in rows.values() if row["label"] == selected["label"])+ assert selected_row["receiptRefs"] == [prefix + current_name]+ if metadata in {"anonymous", "anonymous-candidate", "duplicate-id"}:+ assert selected_row["disposition"] == "no_issue_found"+ assert set(rows["first/writer"]["receiptRefs"]) == expected+ replay_warnings = []+ replay = saved.merge_saved_results(+ tmp_path,+ "coverage-scan",+ saved_binding("deep_repository", repository="synthetic"),+ [source],+ replay_warnings,+ stopped=True,+ reason="Source review stopped.",+ project_worker_coverage=True,+ frozen_source_digests=documents[0]["scan"]["preservedSources"],+ )+ assert replay is not None+ finalizer._recover_unsealed_coverage(+ replay[2],+ Path(saved.__file__).resolve().parent.parent / "schemas",+ tmp_path,+ replay_warnings,+ [],+ )+ assert replay[2] == documents[2]+ assert all(path.read_bytes() == content for path, content in originals.items())diff --git a/plugins/codex-security/tests/test_deep_scan_stop_conditions.py b/plugins/codex-security/tests/test_deep_scan_stop_conditions.pyindex 62f5268f0..1c2389e15 100644--- a/plugins/codex-security/tests/test_deep_scan_stop_conditions.py+++ b/plugins/codex-security/tests/test_deep_scan_stop_conditions.py@@ -223,3 +223,72 @@ def test_rejected_deadline_publication_does_not_leave_parent_observations(if existing_head:coverage = json.loads((scan.scan_dir / "coverage.json").read_text())assert any(item["reason"] == pending["reason"] for item in coverage["deferred"])+++def test_zero_accepted_deadline_preserves_worker_checkpoint_links_and_receipts(+ workbench_api, workbench_db, publication_scan+):+ scan = publication_scan()+ workers = {}+ originals = {}+ for name in ("first", "second"):+ result = add_worker(workbench_db, scan, status="canceled")+ worker_id = result.parent.name+ output = scan.scan_dir / "artifacts" / "deep_discovery" / "workers" / worker_id / "output"+ output.parent.mkdir(parents=True)+ result.parent.rename(output)+ result = output / result.name+ with workbench_db:+ workbench_db.execute(+ "UPDATE deep_scan_workers SET artifact_dir = ?, result_manifest_path = ? "+ "WHERE id = ?",+ (str(output), str(result), worker_id),+ )+ receipt = result.parent / "artifacts" / "review.md"+ receipt.parent.mkdir()+ receipt.write_text(f"Synthetic {name} source review.\n")+ checkpoint = write_checkpoint(+ result.parent / "checkpoints",+ saved_draft(+ scan.scan_id,+ complete=False,+ surfaces=[+ {+ "id": "api",+ "label": name,+ "disposition": "no_issue_found",+ "receiptRefs": ["artifacts/review.md"],+ }+ ],+ deferred=[{"id": "review", "reason": "Review remains.", "surfaceIds": ["api"]}],+ ),+ )+ head = result.parent / "checkpoint-head.json"+ head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ workers[worker_id] = receipt+ originals.update({path: path.read_bytes() for path in (receipt, checkpoint, head)})+ with workbench_db:+ workbench_db.execute(+ "UPDATE deep_scan_runs SET status = 'running', phase = 'discovery', "+ "workers = 2, max_discovery_runs = 3, discovery_runs_dispatched = 2, "+ "created_at = '2000-01-01T00:00:00+00:00', manifest_path = NULL, "+ "terminal_reason = NULL, completed_at = NULL WHERE scan_id = ?",+ (scan.scan_id,),+ )++ finished = finish(workbench_api, workbench_db, scan, terminal_reason="capped")++ assert finished["status"] == "succeeded"+ assert finished["completionSequence"] == 0+ coverage = json.loads((scan.scan_dir / "coverage.json").read_text())+ assert {row["id"] for row in coverage["surfaces"]} == {+ f"{worker_id}/api" for worker_id in workers+ }+ for worker_id, receipt in workers.items():+ surface = next(row for row in coverage["surfaces"] if row["workerId"] == worker_id)+ task = next(row for row in coverage["deferred"] if row.get("workerId") == worker_id)+ assert surface["disposition"] == "no_issue_found"+ assert surface["receiptRefs"] == [receipt.relative_to(scan.scan_dir).as_posix()]+ assert task["id"] == f"{worker_id}/review"+ assert task["surfaceIds"] == [surface["id"]]+ assert all(path.read_bytes() == contents for path, contents in originals.items())diff --git a/plugins/codex-security/tests/test_saved_deferred_identity.py b/plugins/codex-security/tests/test_saved_deferred_identity.pyindex 031a09431..055165edb 100644--- a/plugins/codex-security/tests/test_saved_deferred_identity.py+++ b/plugins/codex-security/tests/test_saved_deferred_identity.py@@ -16,6 +16,7 @@ from workbench_test_support import (preserve_scan_results,saved_discovery_worker,saved_draft,+ worker_coverage_id,write_checkpoint,)@@ -346,7 +347,7 @@ def test_generic_surface_recovery_uses_resolved_candidate_identity(outcome: str,):state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path, workers=2)- _, result_path = accepted_standard_worker(state, codex_home, scan_dir, scan_id)+ worker_id, result_path = accepted_standard_worker(state, codex_home, scan_dir, scan_id)candidate = {"id": "caller-review","reason": "Caller needs validation.",@@ -401,8 +402,9 @@ def test_generic_surface_recovery_uses_resolved_candidate_identity(head.write_text(json.dumps({"checkpoint": checkpoints[2 if rejected_here else 1].name}))observed = 300 if rejected_here else 200os.utime(head, ns=(observed, observed))+ rejection_worker_id = worker_idif outcome == "other_worker":- _, other_result = accepted_standard_worker(+ rejection_worker_id, other_result = accepted_standard_worker(state, codex_home, scan_dir, scan_id, name="other-worker")other_result.write_text(@@ -414,16 +416,28 @@ def test_generic_surface_recovery_uses_resolved_candidate_identity(monkeypatch, saved_results, state, codex_home, scan_dir, scan_id)for coverage in (first_coverage, recovered):- api_surfaces = [row for row in coverage["surfaces"] if row["id"] == "api"]+ api_surfaces = [+ row for row in coverage["surfaces"] if row["id"] == worker_coverage_id(worker_id, "api")+ ]assert len(api_surfaces) == 1expected_disposition = "no_issue_found" if rejected_here else "needs_follow_up"assert api_surfaces[0]["disposition"] == expected_disposition- assert not any(row["id"] == generic["id"] for row in coverage["deferred"])- assert (- any(row["id"] == candidate["id"] for row in coverage["deferred"]) is not rejected_here+ assert not any(+ row["id"] == worker_coverage_id(worker_id, generic["id"])+ for row in coverage["deferred"])+ assert (+ any(+ row["id"] == worker_coverage_id(worker_id, candidate["id"])+ for row in coverage["deferred"]+ )+ ) is not rejected_hereif outcome != "unresolved":- assert rejection in coverage["surfaces"]+ assert {+ **rejection,+ "id": worker_coverage_id(rejection_worker_id, rejection["id"]),+ "workerId": rejection_worker_id,+ } in coverage["surfaces"]assert recovered["surfaces"] == first_coverage["surfaces"]assert recovered["deferred"] == first_coverage["deferred"]@@ -443,7 +457,7 @@ def test_split_deferred_rows_keep_distinct_ids_in_frozen_recovery(close_first: bool,):state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path)- _, result_path = accepted_standard_worker(state, codex_home, scan_dir, scan_id)+ worker_id, result_path = accepted_standard_worker(state, codex_home, scan_dir, scan_id)combined = {"id": "combined-review","reason": "Review remains.",@@ -491,8 +505,10 @@ def test_split_deferred_rows_keep_distinct_ids_in_frozen_recovery(assert len(retained) == (1 if close_first and explicit_ids else 2)assert len({row["id"] for row in retained}) == len(retained)if explicit_ids:- assert {row["id"] for row in retained} == expected_ids- assert all(row["id"] != combined["id"] for row in retained)+ assert {row["id"] for row in retained} == {+ worker_coverage_id(worker_id, identity) for identity in expected_ids+ }+ assert all(row["id"] != worker_coverage_id(worker_id, combined["id"]) for row in retained)if payload_field:assert all(row[payload_field] == combined[payload_field] for row in retained)assert replay == first_coverage@@ -510,7 +526,7 @@ def test_unnamed_observation_does_not_replace_saved_context(observation: str,):state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path)- _, result_path = accepted_standard_worker(state, codex_home, scan_dir, scan_id)+ worker_id, result_path = accepted_standard_worker(state, codex_home, scan_dir, scan_id)broad = {"id": "caller-review","reason": "Review the caller paths.",@@ -548,12 +564,18 @@ def test_unnamed_observation_does_not_replace_saved_context(first_coverage, replay = cancel_and_preserve(monkeypatch, saved_results, state, codex_home, scan_dir, scan_id)+ expected_output = (+ {**expected, "id": worker_coverage_id(worker_id, expected["id"]), "workerId": worker_id}+ if layout == "worker"+ else expected+ )+ expected_raw = {**raw_row, "workerId": worker_id} if layout == "worker" else raw_rowfor coverage in (first_coverage, replay):pending = [row for row in coverage["deferred"] if row["id"] != "scan-stopped"]- assert expected in pending- unnamed = [row for row in pending if row.get("id") != expected["id"]]+ assert expected_output in pending+ unnamed = [row for row in pending if row.get("id") != expected_output["id"]]assert len(unnamed) == 1- assert {key: value for key, value in unnamed[0].items() if key != "id"} == raw_row+ assert {key: value for key, value in unnamed[0].items() if key != "id"} == expected_rawassert "candidateId" not in unnamed[0]assert replay == first_coveragediff --git a/plugins/codex-security/tests/test_workbench_checkpoint_heads.py b/plugins/codex-security/tests/test_workbench_checkpoint_heads.pyindex 9fdb08db9..7ba90e1f7 100644--- a/plugins/codex-security/tests/test_workbench_checkpoint_heads.py+++ b/plugins/codex-security/tests/test_workbench_checkpoint_heads.py@@ -25,6 +25,7 @@ from workbench_test_support import (saved_draft,scan_command,start_delivered_scan,+ worker_coverage_id,write_checkpoint,write_completed_contract,)@@ -58,7 +59,7 @@ def test_late_head_changes_require_explicit_recovery(tmp_path: Path, selection: str, has_result: bool) -> None:state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path)- _, result = accepted_standard_worker(state, codex_home, scan_dir, scan_id)+ worker_id, result = accepted_standard_worker(state, codex_home, scan_dir, scan_id)pending, closed = drafts(scan_id)reopened = write_checkpoint(result.parent / "checkpoints", pending)completed = write_checkpoint(result.parent / "checkpoints", closed)@@ -78,7 +79,7 @@ def test_late_head_changes_require_explicit_recovery(assert any("/checkpoint-heads/" in path for path in frozen)assert not any(path.endswith("checkpoint-head.json") for path in frozen)assert not any(- row["id"] == "review"+ row["id"] == worker_coverage_id(worker_id, "review")for row in json.loads((scan_dir / "coverage.json").read_text())["deferred"])assert get_scan(state, scan_id)["scan"]["resultsRecoveryNeeded"] is False@@ -92,7 +93,9 @@ def test_late_head_changes_require_explicit_recovery(recovered = scan_command(state, "recover-scan-results", scan_id, environment=environment)assert recovered["scan"]["resultsRecoveryNeeded"] is Falsecoverage = json.loads((scan_dir / "coverage.json").read_text())- assert any(row["id"] == "review" for row in coverage["deferred"]) is (selection == "reopened")+ assert any(+ row["id"] == worker_coverage_id(worker_id, "review") for row in coverage["deferred"]+ ) is (selection == "reopened")published = manifest_path.read_bytes()scan_command(state, "recover-scan-results", scan_id, environment=environment)assert manifest_path.read_bytes() == published@@ -925,7 +928,11 @@ def test_parent_head_selection_matches_frozen_publication_retry(json.loads((scan_dir / "coverage.json").read_text()),)for findings, coverage in (first[0], replay):- assert child_work in coverage["deferred"]+ assert {+ **child_work,+ "id": worker_coverage_id(worker_id, child_work["id"]),+ "workerId": worker_id,+ } in coverage["deferred"]if evidence == "deferred":assert (parent_work in coverage["deferred"]) is (head_time <= 100)else:diff --git a/plugins/codex-security/tests/test_workbench_saved_source_order.py b/plugins/codex-security/tests/test_workbench_saved_source_order.pyindex ef45cbc95..ed427f1e3 100644--- a/plugins/codex-security/tests/test_workbench_saved_source_order.py+++ b/plugins/codex-security/tests/test_workbench_saved_source_order.py@@ -15,6 +15,7 @@ from workbench_test_support import (saved_binding,saved_discovery_worker,scan_command,+ worker_coverage_id,write_checkpoint,)@@ -37,7 +38,7 @@ def test_failed_publication_keeps_order_after_identical_result_rewrite(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, latest_pending: bool) -> None:state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path)- _, result = accepted_standard_worker(state, codex_home, scan_dir, scan_id)+ worker_id, result = accepted_standard_worker(state, codex_home, scan_dir, scan_id)pending, closed = drafts(scan_id)old, latest = (closed, pending) if latest_pending else (pending, closed)result.write_text(json.dumps(old))@@ -73,7 +74,10 @@ def test_failed_publication_keeps_order_after_identical_result_rewrite("standard-worker-thread",)coverage = json.loads((scan_dir / "coverage.json").read_text())- assert any(row["id"] == "review" for row in coverage["deferred"]) is latest_pending+ assert (+ any(row["id"] == worker_coverage_id(worker_id, "review") for row in coverage["deferred"])+ is latest_pending+ )assert (json.loads((scan_dir / "scan-manifest.json").read_text())["scan"]["preservedSources"]== frozen@@ -86,7 +90,7 @@ def test_head_capture_includes_checkpoint_published_after_enumeration(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, explicit_recovery: bool, layout: str) -> None:state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path)- _, result = accepted_standard_worker(state, codex_home, scan_dir, scan_id)+ worker_id, result = accepted_standard_worker(state, codex_home, scan_dir, scan_id)pending, closed = drafts(scan_id)result.write_text(json.dumps(closed))os.utime(result, ns=(100, 100))@@ -122,7 +126,8 @@ def test_head_capture_includes_checkpoint_published_after_enumeration()assert len(published) == 1coverage = json.loads((scan_dir / "coverage.json").read_text())- assert any(row["id"] == "review" for row in coverage["deferred"])+ expected_id = worker_coverage_id(worker_id, "review") if layout == "worker" else "review"+ assert any(row["id"] == expected_id for row in coverage["deferred"])sources = json.loads((scan_dir / "scan-manifest.json").read_text())["scan"]["preservedSources"]assert published[0].relative_to(scan_dir).as_posix() in sourcesassert any("/checkpoint-heads/" in f"/{path}" for path in sources)diff --git a/plugins/codex-security/tests/test_workbench_standard_deep_results.py b/plugins/codex-security/tests/test_workbench_standard_deep_results.pyindex d7bd75d2b..2b6717589 100644--- a/plugins/codex-security/tests/test_workbench_standard_deep_results.py+++ b/plugins/codex-security/tests/test_workbench_standard_deep_results.py@@ -1238,7 +1238,10 @@ def standard_parent_results_fixture(return state_dir, codex_home, target, scan_dir, scan_id-def test_failure_preserves_last_committed_reducer_without_parent_draft(tmp_path: Path) -> None:[email protected]("with_coverage", [False, True])+def test_failure_preserves_last_committed_reducer_without_parent_draft(+ tmp_path: Path, with_coverage: bool+) -> None:state_dir, codex_home, target, scan_dir, scan_id = deep_scan_fixture(tmp_path)worker_id, result_path = accepted_standard_worker(state_dir, codex_home, scan_dir, scan_id)contract_dir = standard_contract(tmp_path, scan_id, target)@@ -1252,12 +1255,15 @@ def test_failure_preserves_last_committed_reducer_without_parent_draft(tmp_path:reduced["findings"][0]["summary"] = ("The reducer retained additional independently reviewed evidence.")+ if not with_coverage:+ reduced.pop("coverage")reducer_path.write_text(json.dumps(reduced))fail_deep_scan(state_dir, codex_home, scan_id, message="Later reducer failed.")failed = get_scan(state_dir, scan_id)["scan"]assert failed["progress"]["status"] == "failed"assert failed["findingCount"] == 1assert failed["findings"][0]["summary"] == reduced["findings"][0]["summary"]+ assert json.loads((scan_dir / "coverage.json").read_text())["mode"] == "deep_repository"@pytest.mark.parametrize("tied_head", [False, True])diff --git a/plugins/codex-security/tests/test_workbench_stopped_worker_coverage.py b/plugins/codex-security/tests/test_workbench_stopped_worker_coverage.pynew file mode 100644index 000000000..41f1fcb01--- /dev/null+++ b/plugins/codex-security/tests/test_workbench_stopped_worker_coverage.py@@ -0,0 +1,174 @@+from __future__ import annotations++import json+from pathlib import Path++import pytest+from test_workbench_standard_deep_results import (+ accepted_standard_worker,+ deep_scan_fixture,+ standard_finding,+)+from workbench_test_support import (+ cancel_scan,+ fail_deep_scan,+ get_scan,+ preserve_scan_results,+ saved_draft,+)++[email protected]("termination", ["failed", "interrupted", "canceled"])+def test_stopped_deep_scan_projects_worker_coverage_without_parent_aggregate(+ tmp_path: Path, termination: str+) -> None:+ state_dir, codex_home, target, scan_dir, scan_id = deep_scan_fixture(tmp_path, workers=2)+ environment = {"CODEX_HOME": str(codex_home)}+ finding = standard_finding(tmp_path, scan_id, target)+ workers = {}+ originals = {}+ for name in ("first", "second"):+ worker_id, result = accepted_standard_worker(+ state_dir, codex_home, scan_dir, scan_id, name=name+ )+ receipt = result.parent / "artifacts/review.md"+ receipt.parent.mkdir()+ receipt.write_text(f"Synthetic {name} review.\n")+ result.write_text(+ json.dumps(+ saved_draft(+ scan_id,+ complete=True,+ findings=[finding] if name == "first" else [],+ surfaces=[+ {+ "id": "api",+ "label": name,+ "disposition": "no_issue_found",+ "receiptRefs": ["artifacts/review.md"],+ }+ ],+ deferred=[+ {+ "id": "review",+ "reason": f"Review {name} remains.",+ "surfaceIds": ["api"],+ }+ ],+ )+ )+ )+ workers[name] = (worker_id, receipt)+ originals.update({path: path.read_bytes() for path in (result, receipt)})+ assert not (scan_dir / "coverage.json").exists()+ assert not (scan_dir / "checkpoints").exists()++ if termination == "canceled":+ cancel_scan(state_dir, scan_id, "standard-worker-thread", environment=environment)+ else:+ fail_deep_scan(state_dir, codex_home, scan_id, deep_status=termination)+ stopped = get_scan(state_dir, scan_id)["scan"]+ assert stopped["findingCount"] == 1+ assert not any("coverage receipt" in warning for warning in stopped["warnings"])+ coverage = json.loads((scan_dir / "coverage.json").read_text())+ assert "completeness" not in coverage+ assert {row["id"] for row in coverage["surfaces"]} == {+ f"{worker_id}/api" for worker_id, _ in workers.values()+ }+ for name, (worker_id, receipt) in workers.items():+ surface = next(row for row in coverage["surfaces"] if row["workerId"] == worker_id)+ task = next(row for row in coverage["deferred"] if row.get("workerId") == worker_id)+ assert surface["disposition"] == "no_issue_found"+ assert surface["receiptRefs"] == [receipt.relative_to(scan_dir).as_posix()]+ assert task["id"] == f"{worker_id}/review"+ assert task["surfaceIds"] == [surface["id"]]+ assert (scan_dir / surface["receiptRefs"][0]).read_text() == f"Synthetic {name} review.\n"+ manifest = json.loads((scan_dir / "scan-manifest.json").read_text())+ assert manifest["scan"]["status"] == termination+ assert isinstance(manifest["scan"]["sealedAt"], str)+ sealed = {+ name: (scan_dir / name).read_bytes()+ for name in ("scan-manifest.json", "findings.json", "coverage.json", "report.md")+ }+ replay = preserve_scan_results(+ state_dir, scan_id, "standard-worker-thread", environment=environment+ )["scan"]+ assert replay["findingCount"] == 1+ assert all((scan_dir / name).read_bytes() == content for name, content in sealed.items())+ assert all(path.read_bytes() == content for path, content in originals.items())+++def test_stopped_worker_rejects_receipts_outside_its_artifacts_directory(tmp_path: Path) -> None:+ state_dir, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path)+ worker_id, result = accepted_standard_worker(state_dir, codex_home, scan_dir, scan_id)+ result.write_text(+ json.dumps(+ saved_draft(+ scan_id,+ complete=True,+ surfaces=[+ {+ "id": "api",+ "label": "API",+ "disposition": "no_issue_found",+ "receiptRefs": ["result.json"],+ }+ ],+ )+ )+ )+ original = result.read_bytes()++ fail_deep_scan(state_dir, codex_home, scan_id)++ coverage_path = scan_dir / "coverage.json"+ surface = json.loads(coverage_path.read_text())["surfaces"][0]+ assert surface["id"] == f"{worker_id}/api"+ assert surface["disposition"] == "needs_follow_up"+ assert surface["receiptRefs"] == []+ assert any(+ "under artifacts/" in warning+ for warning in get_scan(state_dir, scan_id)["scan"]["warnings"]+ )+ sealed = coverage_path.read_bytes()+ preserve_scan_results(+ state_dir, scan_id, "standard-worker-thread", environment={"CODEX_HOME": str(codex_home)}+ )+ assert coverage_path.read_bytes() == sealed+ assert result.read_bytes() == original++[email protected]("anonymous_first", [True, False])+def test_stopped_worker_links_follow_explicit_surface_identity_through_collisions(+ tmp_path: Path, anonymous_first: bool+) -> None:+ state_dir, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path)+ _, result = accepted_standard_worker(state_dir, codex_home, scan_dir, scan_id)+ anonymous = {"candidateId": "api", "label": "Anonymous", "disposition": "needs_follow_up"}+ explicit = {"id": "api", "label": "Explicit", "disposition": "needs_follow_up"}+ result.write_text(+ json.dumps(+ saved_draft(+ scan_id,+ complete=True,+ surfaces=[anonymous, explicit] if anonymous_first else [explicit, anonymous],+ deferred=[{"id": "review", "reason": "Review remains.", "surfaceIds": ["api"]}],+ )+ )+ )+ original = result.read_bytes()++ fail_deep_scan(state_dir, codex_home, scan_id)++ coverage_path = scan_dir / "coverage.json"+ coverage = json.loads(coverage_path.read_text())+ surfaces = {row["label"]: row for row in coverage["surfaces"]}+ assert len(surfaces) == 2+ assert surfaces["Explicit"]["id"] != surfaces["Anonymous"]["id"]+ assert coverage["deferred"][0]["surfaceIds"] == [surfaces["Explicit"]["id"]]+ sealed = coverage_path.read_bytes()+ preserve_scan_results(+ state_dir, scan_id, "standard-worker-thread", environment={"CODEX_HOME": str(codex_home)}+ )+ assert coverage_path.read_bytes() == sealed+ assert result.read_bytes() == originaldiff --git a/plugins/codex-security/tests/workbench_test_support.py b/plugins/codex-security/tests/workbench_test_support.pyindex 421dc01b7..d9c7a0680 100644--- a/plugins/codex-security/tests/workbench_test_support.py+++ b/plugins/codex-security/tests/workbench_test_support.py@@ -94,6 +94,10 @@ def saved_binding(coverage_mode="repository", *, repository="test", status="inte}+def worker_coverage_id(worker_id: str, local_id: str) -> str:+ return f"{worker_id}/{local_id}"++def saved_discovery_worker(output: Path, worker_id: str = "worker", attempt: int = 1) -> dict:return {"id": worker_id,