Skip to content

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

test(plugin): consolidate recovery and worker fixtures (#2044)

Michael D'Angelof546ba6bc7ec
diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_coverage_cases.ts b/plugins/codex-security/mcp-app/tests/deep_scan_coverage_cases.ts
deleted file mode 100644
index 5a1c48801..000000000
--- a/plugins/codex-security/mcp-app/tests/deep_scan_coverage_cases.ts
+++ /dev/null
@@ -1,88 +0,0 @@
-import assert from "node:assert/strict";
-import { mkdir, writeFile } from "node:fs/promises";
-import path from "node:path";
-import { readJson, writeJson } from "./support/json.ts";
-import type { ScanDraftInput } from "../src/artifact-scan-draft.js";
-import type { JsonObject } from "../src/types.js";
-import {
- coordinatorFixture,
- FakeExecutor,
- runCoordinator,
-} from "./deep_scan_coordinator_fixture.ts";
-
-export async function testSuccessfulDeepCoveragePreservesWorkerReviewStatus() {
- const { fixture, store } = await coordinatorFixture({
- workers: 2,
- stopAfterNoNew: 2,
- maxDiscoveryRuns: 2,
- });
- const executor = new FakeExecutor();
- const run = executor.run.bind(executor);
- const reviewed = { label: "Reviewed query", disposition: "no_issue_found" };
- const workerReviewed = {
- ...reviewed,
- receiptRefs: ["artifacts/review.md"],
- };
- const followUp = {
- label: "Worker follow-up",
- disposition: "needs_follow_up",
- };
- executor.run = async (request) => {
- const outcome = await run(request);
- if (request.kind !== "discovery") return outcome;
- const receiptPath = path.join(
- request.artifactContext!.root,
- workerReviewed.receiptRefs[0],
- );
- await mkdir(path.dirname(receiptPath), { recursive: true });
- await writeFile(receiptPath, "Independent source review receipt.\n");
- const resultPath = path.join(request.artifactContext!.root, "result.json");
- const draft = await readJson(resultPath);
- draft.coverage = {
- completeness:
- request.kind === "discovery" &&
- request.promptPath.includes("discovery-0002")
- ? "unknown"
- : "partial",
- surfaces: [workerReviewed, followUp],
- explicitExclusions: [],
- deferred: [
- { reason: "An independent review left this question unresolved." },
- ],
- };
- await writeJson(resultPath, draft);
- return outcome;
- };
- const completed: ScanDraftInput[] = [];
- const terminal = await runCoordinator(fixture, store, executor, {
- onComplete: async (draft) => void completed.push(structuredClone(draft)),
- });
- assert.equal(terminal?.status, "succeeded", terminal?.error);
- assert.equal(completed.length, 1);
- const coverage = completed[0].coverage;
- assert.equal(coverage.completeness, undefined);
- assert.equal((coverage.deferred as JsonObject[]).length, 2);
- assert.equal((coverage.surfaces as JsonObject[]).length, 4);
- const reviews = coverage.reviews as { workerId: string }[];
- assert.equal(reviews.length, 2);
- assert.deepEqual(
- new Set(reviews.map((review) => review.workerId)),
- new Set(
- [...store.workers.values()]
- .filter((worker) => worker.kind === "discovery")
- .map((worker) => worker.id),
- ),
- );
- for (const item of coverage.deferred as JsonObject[]) {
- const provenance = item.provenance as { workerId: string; attempt: number };
- assert.equal(provenance.attempt, 1);
- assert.ok(store.workers.has(provenance.workerId));
- }
- for (const worker of store.workers.values()) {
- if (worker.kind !== "discovery") continue;
- const draft = await readJson(worker.resultManifestPath!);
- assert.notEqual(draft.coverage.completeness, "complete");
- assert.deepEqual(draft.coverage.surfaces, [workerReviewed, followUp]);
- assert.equal(draft.coverage.deferred.length, 1);
- }
-}
diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_coverage_fixture.ts b/plugins/codex-security/mcp-app/tests/deep_scan_coverage_fixture.ts
index f0602288b..0a694a58e 100644
--- a/plugins/codex-security/mcp-app/tests/deep_scan_coverage_fixture.ts
+++ b/plugins/codex-security/mcp-app/tests/deep_scan_coverage_fixture.ts
@@ -849,12 +849,10 @@ runpy.run_path(sys.argv[0], run_name="__main__")
});
const resultManifestPath = path.join(artifactDir, "result.json");
// Legacy accepted reducers omitted coverage entirely.
+ const findings = retryFindings?.flat() ?? [];
await writeFile(
resultManifestPath,
- JSON.stringify({
- scanId: run.scanId,
- findings: retryFindings?.flat() ?? [],
- }),
+ JSON.stringify({ scanId: run.scanId, findings }),
);
rawSources.set(
resultManifestPath,
@@ -863,7 +861,7 @@ runpy.run_path(sys.argv[0], run_name="__main__")
await store.commitDedup({
id,
scanId: run.scanId,
- newFindings: retryFindings ? 1 : 0,
+ newFindings: findings.length,
resultManifestPath,
});
run = await store.get(run.scanId, threadId);
diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_lifecycle_cases.ts b/plugins/codex-security/mcp-app/tests/deep_scan_lifecycle_cases.ts
index 378cbd546..25f3f59a8 100644
--- a/plugins/codex-security/mcp-app/tests/deep_scan_lifecycle_cases.ts
+++ b/plugins/codex-security/mcp-app/tests/deep_scan_lifecycle_cases.ts
@@ -1326,9 +1326,9 @@ export async function testSuccessfulDeepCoverageRetainsIndependentWorkerReviewSt
});
const executor = new FakeExecutor();
const run = executor.run.bind(executor);
- const reviewed = { label: "Reviewed query", disposition: "no_issue_found" };
const workerReviewed = {
- ...reviewed,
+ label: "Reviewed query",
+ disposition: "no_issue_found",
id: "shared-surface",
receiptRefs: ["artifacts/worker-receipt.md"],
};
@@ -1342,6 +1342,9 @@ export async function testSuccessfulDeepCoverageRetainsIndependentWorkerReviewSt
const resultPath = path.join(request.artifactContext!.root, "result.json");
const draft = await readJson(resultPath);
draft.coverage = {
+ completeness: request.promptPath.includes("discovery-0002")
+ ? "unknown"
+ : "partial",
surfaces: [workerReviewed, followUp],
explicitExclusions: [
{ pattern: "vendor/**", reason: "Third-party code." },
@@ -1372,6 +1375,8 @@ export async function testSuccessfulDeepCoverageRetainsIndependentWorkerReviewSt
assert.equal(terminal?.status, "succeeded", terminal?.error);
assert.equal(completed.length, 1);
const coverage = completed[0].coverage as unknown as {
+ completeness?: string;
+ reviews: { workerId: string }[];
surfaces: { id: string; workerId: string; receiptRefs?: string[] }[];
explicitExclusions: { workerId: string; pattern: string }[];
deferred: {
@@ -1382,6 +1387,15 @@ export async function testSuccessfulDeepCoverageRetainsIndependentWorkerReviewSt
openQuestions: { question: string; workerId: string }[];
};
+ const workers = [...store.workers.values()].filter(
+ (worker) => worker.kind === "discovery",
+ );
+ assert.equal(coverage.completeness, undefined);
+ assert.deepEqual(
+ coverage.reviews.map((review) => review.workerId).sort(),
+ workers.map((worker) => worker.id).sort(),
+ );
+ assert.equal(coverage.deferred.length, 2);
assert.equal(coverage.surfaces.length, 4);
assert.equal(new Set(coverage.surfaces.map((row) => row.id)).size, 4);
assert.equal(new Set(coverage.deferred.map((row) => row.id)).size, 2);
@@ -1403,9 +1417,9 @@ export async function testSuccessfulDeepCoverageRetainsIndependentWorkerReviewSt
);
}
}
- for (const worker of store.workers.values()) {
- if (worker.kind !== "discovery") continue;
+ for (const worker of workers) {
const draft = await readJson(worker.resultManifestPath!);
+ assert.notEqual(draft.coverage.completeness, "complete");
assert.deepEqual(draft.coverage.surfaces, [workerReviewed, followUp]);
assert.equal(draft.coverage.deferred.length, 1);
}
diff --git a/plugins/codex-security/mcp-app/tests/deep_scan_startup_cases.ts b/plugins/codex-security/mcp-app/tests/deep_scan_startup_cases.ts
deleted file mode 100644
index 20e5f65f3..000000000
--- a/plugins/codex-security/mcp-app/tests/deep_scan_startup_cases.ts
+++ /dev/null
@@ -1,32 +0,0 @@
-import assert from "node:assert/strict";
-
-export async function testArtifactStartupTimeoutClassification(
- runFixtureWorker: (prompt: string) => Promise<unknown>,
-) {
- for (const prompt of [
- "ARTIFACT_MCP_STARTUP_TIMEOUT",
- "LEGACY_ARTIFACT_MCP_STARTUP_TIMEOUT",
- "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT",
- "ARTIFACT_MCP_STARTUP_TIMEOUT_SYNC_AUTH_WARNING",
- "LEGACY_ARTIFACT_MCP_STARTUP_TIMEOUT_SYNC_AUTH_WARNING",
- "ARTIFACT_MCP_STARTUP_TIMEOUT_BOTH_AUTH_WARNINGS",
- "LEGACY_ARTIFACT_MCP_STARTUP_TIMEOUT_BOTH_AUTH_WARNINGS",
- "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_SYNC_AUTH_WARNING",
- "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_BOTH_AUTH_WARNINGS",
- "ARTIFACT_MCP_STARTUP_TIMEOUT_WITH_MISSING_API_KEY",
- "ARTIFACT_MCP_STARTUP_TIMEOUT_WITH_POLICY_REFUSAL",
- "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_SYNC_AUTH_WARNING_WITH_MISSING_API_KEY",
- "ARTIFACT_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_BOTH_AUTH_WARNINGS_WITH_POLICY_REFUSAL",
- "OTHER_MCP_STARTUP_TIMEOUT",
- "OTHER_MCP_STARTUP_TIMEOUT_REQUEST_TIMED_OUT_SYNC_AUTH_WARNING",
- "CATALOG_AUTH_ONLY",
- "SYNC_AUTH_ONLY",
- ]) {
- await assert.rejects(
- runFixtureWorker(prompt),
- (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>
- error?.name === "Error",
- `${prompt} should remain retryable`,
- );
- }
-}
diff --git a/plugins/codex-security/mcp-app/tests/support/fake-codex-source.ts b/plugins/codex-security/mcp-app/tests/support/fake-codex-source.ts
index 863aec0e9..1a3409317 100644
--- a/plugins/codex-security/mcp-app/tests/support/fake-codex-source.ts
+++ b/plugins/codex-security/mcp-app/tests/support/fake-codex-source.ts
@@ -79,18 +79,11 @@ if (stdin.includes('COMPLETE_THEN_HANG')) process.on('SIGTERM', () => { if (!std
if (stdin.includes('THREAD_START_CONFIG_ERROR')) { console.error('Error: thread/start: thread/start failed: agents.max_threads cannot be set when features.multi_agent_v2 is enabled (code -32600)'); process.exit(1); }
if (stdin.includes('CONFIG_ERROR')) { console.error('failed to load configuration: invalid value'); process.exit(2); }
-if (stdin.includes('MCP_STARTUP_TIMEOUT') || stdin.includes('CATALOG_AUTH_ONLY') || stdin.includes('SYNC_AUTH_ONLY')) {
- const syncWarning = stdin.includes('SYNC_AUTH_WARNING') || stdin.includes('SYNC_AUTH_ONLY');
- const bothWarnings = stdin.includes('BOTH_AUTH_WARNINGS');
- if (!syncWarning || bothWarnings) console.error('chatgpt authentication required for remote plugin catalog; api key auth is not supported');
- if (syncWarning || bothWarnings) console.error('chatgpt authentication required to sync remote plugins; api key auth is not supported');
- if (!stdin.includes('CATALOG_AUTH_ONLY') && !stdin.includes('SYNC_AUTH_ONLY')) {
- const serverName = stdin.includes('OTHER_MCP_STARTUP_TIMEOUT') ? 'other_server' : stdin.includes('LEGACY_ARTIFACT_MCP_STARTUP_TIMEOUT') ? 'codex_security_artifacts' : 'cs_artifacts';
- const timeout = stdin.includes('REQUEST_TIMED_OUT') ? 'request timed out' : 'timed out handshaking with MCP server after 30s';
- console.error('required MCP servers failed to initialize: ' + serverName + ': ' + timeout);
- }
- if (stdin.includes('WITH_MISSING_API_KEY')) console.error('missing API key');
- if (stdin.includes('WITH_POLICY_REFUSAL')) console.error('Request blocked by cyberPolicy.');
+if (stdin.includes('ARTIFACT_MCP_STARTUP_TIMEOUT')) {
+ console.error('chatgpt authentication required to sync remote plugins; api key auth is not supported');
+ console.error('required MCP servers failed to initialize: cs_artifacts: request timed out');
+ console.error('missing API key');
+ console.error('Request blocked by cyberPolicy.');
process.exit(1);
}
const resumeIndex = process.argv.indexOf('resume');
diff --git a/plugins/codex-security/mcp-app/tests/support/rpc-server.ts b/plugins/codex-security/mcp-app/tests/support/rpc-server.ts
index dbb910827..d3b98d8d1 100644
--- a/plugins/codex-security/mcp-app/tests/support/rpc-server.ts
+++ b/plugins/codex-security/mcp-app/tests/support/rpc-server.ts
@@ -1,6 +1,5 @@
import { spawn } from "node:child_process";
import { EventEmitter, once } from "node:events";
-import { setTimeout as delay } from "node:timers/promises";
import { consumeStreamLines, writeMessage } from "./streams.ts";
export function startRpcServer(
@@ -118,8 +117,8 @@ export function startRpcServer(
child.stdin!.end();
if (options.terminateOnStop) child.kill();
else {
- await Promise.race([exited, delay(2_000)]);
- child.kill("SIGKILL");
+ const timer = setTimeout(() => child.kill("SIGKILL"), 2_000);
+ child.once("exit", () => clearTimeout(timer));
}
await exited;
},
diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts
index c72e4544e..fc613d0ca 100644
--- a/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts
+++ b/plugins/codex-security/mcp-app/tests/test_artifact_inventory.ts
@@ -4,9 +4,9 @@ import { execFile as nodeExecFile } from "node:child_process";
import {
chmod,
cp,
- readdir,
mkdir,
readFile,
+ readdir,
rename,
symlink,
unlink,
@@ -37,7 +37,6 @@ const inventory = await importSource(
try {
await testSchemasAreBoundAndExact();
await testPrepareUsesTheExistingStandardGenerator();
- await testPrepareDoesNotWritePluginBytecode();
if (!["win32", "darwin"].includes(process.platform))
await testNonUtf8InventoryPaths();
await testPrepareListsIgnoredTrackedFilesOnce();
@@ -55,7 +54,6 @@ try {
await testInventoryReadErrorsAreReported();
await testWorkersCannotPrepareInventory();
await testBoundScopeFailurePreservesPreviousInventory();
- await testInventoryDoesNotLaunchPython();
await testInvalidDiffTargetPreservesPreviousInventory();
} finally {
await temporaryDirectories.cleanup();
@@ -95,7 +93,29 @@ async function testPrepareUsesTheExistingStandardGenerator() {
"export const c = 3;\n",
);
- const result = await inventory.prepareCodexSecurityReviewItems(fixture.scan);
+ const pluginRoot = path.join(fixture.root, "installed-plugin");
+ for (const directory of ["mcp", "scripts"]) {
+ await cp(
+ path.join(fixture.scan.pluginRoot, directory),
+ path.join(pluginRoot, directory),
+ {
+ recursive: true,
+ filter: (file) => path.basename(file) !== "__pycache__",
+ },
+ );
+ }
+ const result = await inventory.prepareCodexSecurityReviewItems({
+ ...fixture.scan,
+ pluginRoot,
+ pythonCommand: process.env.PYTHON ?? "python3",
+ });
+ const scripts = await readdir(path.join(pluginRoot, "scripts"), {
+ recursive: true,
+ });
+ assert.deepEqual(
+ scripts.filter((name) => name.endsWith(".pyc")),
+ [],
+ );
const stored = await readFile(fixture.scanInventory, "utf8");
const { stdout } = await execFile(
"rg",
@@ -145,9 +165,12 @@ async function testPrepareUsesTheExistingStandardGenerator() {
),
expectedPaths,
);
+ const nonExecutablePython = path.join(fixture.root, "non-executable-python");
+ await writeFile(nonExecutablePython, "", { mode: 0o600 });
assert.deepEqual(
await inventory.prepareCodexSecurityReviewItems({
...fixture.scan,
+ pythonCommand: nonExecutablePython,
scope: "-source.py",
}),
{ reviewItemsTotal: 1 },
@@ -355,35 +378,6 @@ async function testInventoryChildFailuresPreserveOutput() {
}
}
-async function testPrepareDoesNotWritePluginBytecode() {
- const fixture = await createFixture("plugin bytecode");
- const pluginRoot = path.join(fixture.root, "plugin");
- const scripts = path.join(pluginRoot, "scripts");
- await cp(path.resolve(import.meta.dirname, "../../scripts"), scripts, {
- recursive: true,
- filter: (file) => path.basename(file) !== "__pycache__",
- });
- await cp(
- path.join(fixture.scan.pluginRoot, "mcp"),
- path.join(pluginRoot, "mcp"),
- {
- recursive: true,
- },
- );
- await fixture.writeRepositoryFile("example.py", "value = 1\n");
- const result = await inventory.prepareCodexSecurityReviewItems({
- ...fixture.scan,
- pluginRoot,
- });
- assert.equal(result.reviewItemsTotal, 1);
- assert.equal(
- (await readdir(scripts, { recursive: true })).some((file) =>
- file.endsWith(".pyc"),
- ),
- false,
- );
-}
-
async function testNonUtf8InventoryPaths() {
const fixture = await createFixture("non-UTF-8 repository names");
const rawPath = Buffer.concat([
@@ -759,26 +753,6 @@ async function testInvalidDiffTargetPreservesPreviousInventory() {
);
}
-async function testInventoryDoesNotLaunchPython() {
- const fixture = await createFixture("unused Python selection");
- await fixture.writeRepositoryFile("source.py", "source\n");
- const pythonCommand = path.join(fixture.root, "python");
- for (const present of [false, true]) {
- if (present) await writeFile(pythonCommand, "", { mode: 0o600 });
- assert.deepEqual(
- await inventory.prepareCodexSecurityReviewItems({
- ...fixture.scan,
- pythonCommand,
- }),
- { reviewItemsTotal: 1 },
- );
- assert.equal(
- await readFile(fixture.scanInventory, "utf8"),
- "./source.py\n",
- );
- }
-}
-
async function createFixture(label: string) {
const root = await temporaryDirectories.create(
"security-artifact-inventory-",
@@ -817,7 +791,7 @@ async function createFixture(label: string) {
layout: "scan",
scope: ".",
pluginRoot,
- pythonCommand: process.env.PYTHON ?? "python3",
+ pythonCommand: path.join(root, "missing-python"),
},
worker: {
root: workerRoot,
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.ts
index a1e6fbdf2..739486ef2 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
@@ -48,7 +48,13 @@ const findingFor = (candidateId: string): FixtureFinding => ({
provenance: { source: "local_plugin", candidateId },
});
-for (const layout of ["standard", "diff", "deep"] as const) {
+// Finding reconciliation is shared by failed and canceled scans. Exercise both
+// terminal bindings without repeating every identity case for each status.
+for (const [layout, status] of [
+ ["standard", "failed"],
+ ["diff", "canceled"],
+ ["deep", "failed"],
+] as const) {
for (const variant of [
"one",
"three",
@@ -71,109 +77,107 @@ for (const layout of ["standard", "diff", "deep"] as const) {
"new-sibling-checkpoints",
"new-worker-sibling-checkpoints",
]) {
- for (const status of ["failed", "canceled"]) {
- test(`${layout}: stopped ${status} recovery retains ${variant} candidate identities`, async (t) => {
- const f = await fixture(t, layout);
- const count =
- variant === "three"
- ? 3
- : variant.includes("sibling") || variant === "preserved"
+ test(`${layout}: stopped ${status} recovery retains ${variant} candidate identities`, async (t) => {
+ const f = await fixture(t, layout);
+ const count =
+ variant === "three"
+ ? 3
+ : variant.includes("sibling") || variant === "preserved"
+ ? 2
+ : variant.endsWith("-id")
? 2
- : variant.endsWith("-id")
- ? 2
- : 1;
- const findings = Array.from({ length: count }, (_, index) => {
- const finding = findingFor(
- variant.includes("sibling") || variant.endsWith("-id")
- ? "shared-review"
- : `review-${index + 1}`,
- );
- if (
- variant === "owner" ||
- variant.startsWith("new-worker-") ||
- variant.startsWith("worker-")
- )
- finding.provenance.workerId = "worker-1";
- if (variant.includes("sibling"))
- finding.title = `Synthetic sibling ${index + 1}`;
- if (variant.endsWith("-id"))
- finding.extensions = {
- [variant.includes("report") ||
- (variant === "cross-field-id" && index === 0)
- ? "reportId"
- : "ledgerRowId"]: `synthetic-report-${index + 1}`,
- };
- if (variant === "preserved")
- finding.locations[0].startLine = index + 1;
- return finding;
- });
- if (variant === "legacy")
- Object.assign(findings[0], {
- identity: { anchor: "synthetic-review-finding" },
- });
- if (variant === "severity-revision") {
- const initial = structuredClone(findings[0]);
- initial.severity.level = "high";
- initial.summary = "Initial assessment before reviewing the control.";
- await saveScanDraftCheckpoint(
- f.context,
- { ...f.draft(), findings: [initial] },
- false,
- );
- }
- await f.write({
- ...f.draft(),
- findings: variant.endsWith("-id") ? findings.slice(0, 1) : findings,
- });
+ : 1;
+ const findings = Array.from({ length: count }, (_, index) => {
+ const finding = findingFor(
+ variant.includes("sibling") || variant.endsWith("-id")
+ ? "shared-review"
+ : `review-${index + 1}`,
+ );
+ if (
+ variant === "owner" ||
+ variant.startsWith("new-worker-") ||
+ variant.startsWith("worker-")
+ )
+ finding.provenance.workerId = "worker-1";
+ if (variant.includes("sibling"))
+ finding.title = `Synthetic sibling ${index + 1}`;
if (variant.endsWith("-id"))
- await saveScanDraftCheckpoint(
- f.context,
- { ...f.draft(), findings: findings.slice(1) },
- false,
- );
- if (variant.endsWith("-added")) {
- findings[0].extensions = {
- [variant === "report-added" ? "reportId" : "ledgerRowId"]:
- "synthetic-report-1",
+ finding.extensions = {
+ [variant.includes("report") ||
+ (variant === "cross-field-id" && index === 0)
+ ? "reportId"
+ : "ledgerRowId"]: `synthetic-report-${index + 1}`,
};
- await f.write({ ...f.draft(), findings });
- }
- if (variant === "title-revision") {
- findings[0].title = "Revised synthetic review finding";
- await f.write({ ...f.draft(), findings });
- }
- if (variant === "range-refinement") {
- findings[0].locations[0].endLine = 1;
- await f.write({ ...f.draft(), findings });
- }
- if (variant.startsWith("new-")) {
- const file = path.join(f.root, "findings.json");
- const saved = JSON.parse(await readFile(file, "utf8"));
- saved.findings = saved.findings.slice(0, 1);
- await writeFile(file, JSON.stringify(saved));
- }
- if (variant.endsWith("-checkpoints")) {
- findings[1].severity.level = "high";
- await saveScanDraftCheckpoint(
- f.context,
- { ...f.draft(), findings: findings.slice(1) },
- false,
- );
- }
- if (variant === "preserved") {
- const file = path.join(f.root, "findings.json");
- const saved = JSON.parse(await readFile(file, "utf8"));
- saved.findings[1].provenance.preservedIdentity = structuredClone(
- saved.findings[1].identity,
- );
- saved.findings[1].identity.instance = "saved-distinct-location";
- await writeFile(file, JSON.stringify(saved));
- }
- const { stdout } = await execFileAsync(
- process.env.PYTHON?.trim() || "python3",
- [
- "-c",
- `import sys,json
+ if (variant === "preserved") finding.locations[0].startLine = index + 1;
+ return finding;
+ });
+ if (variant === "legacy")
+ Object.assign(findings[0], {
+ identity: { anchor: "synthetic-review-finding" },
+ });
+ if (variant === "severity-revision") {
+ const initial = structuredClone(findings[0]);
+ initial.severity.level = "high";
+ initial.summary = "Initial assessment before reviewing the control.";
+ await saveScanDraftCheckpoint(
+ f.context,
+ { ...f.draft(), findings: [initial] },
+ false,
+ );
+ }
+ await f.write({
+ ...f.draft(),
+ findings: variant.endsWith("-id") ? findings.slice(0, 1) : findings,
+ });
+ if (variant.endsWith("-id"))
+ await saveScanDraftCheckpoint(
+ f.context,
+ { ...f.draft(), findings: findings.slice(1) },
+ false,
+ );
+ if (variant.endsWith("-added")) {
+ findings[0].extensions = {
+ [variant === "report-added" ? "reportId" : "ledgerRowId"]:
+ "synthetic-report-1",
+ };
+ await f.write({ ...f.draft(), findings });
+ }
+ if (variant === "title-revision") {
+ findings[0].title = "Revised synthetic review finding";
+ await f.write({ ...f.draft(), findings });
+ }
+ if (variant === "range-refinement") {
+ findings[0].locations[0].endLine = 1;
+ await f.write({ ...f.draft(), findings });
+ }
+ if (variant.startsWith("new-")) {
+ const file = path.join(f.root, "findings.json");
+ const saved = JSON.parse(await readFile(file, "utf8"));
+ saved.findings = saved.findings.slice(0, 1);
+ await writeFile(file, JSON.stringify(saved));
+ }
+ if (variant.endsWith("-checkpoints")) {
+ findings[1].severity.level = "high";
+ await saveScanDraftCheckpoint(
+ f.context,
+ { ...f.draft(), findings: findings.slice(1) },
+ false,
+ );
+ }
+ if (variant === "preserved") {
+ const file = path.join(f.root, "findings.json");
+ const saved = JSON.parse(await readFile(file, "utf8"));
+ saved.findings[1].provenance.preservedIdentity = structuredClone(
+ saved.findings[1].identity,
+ );
+ saved.findings[1].identity.instance = "saved-distinct-location";
+ await writeFile(file, JSON.stringify(saved));
+ }
+ const { stdout } = await execFileAsync(
+ process.env.PYTHON?.trim() || "python3",
+ [
+ "-c",
+ `import sys,json
from pathlib import Path
sys.path.insert(0,sys.argv[1])
from workbench_saved_results import merge_saved_results
@@ -190,23 +194,23 @@ for document in documents[1:]: document['scanId']=scan_id
prepared=_prepare_scan_finalization(root,completion_binding=binding,recover_unsealed=True,draft_documents=documents)
warnings.extend(prepared.warnings)
published=_write_prepared_scan_finalization(prepared)
-print(json.dumps({'count':len(published[1]['findings']),'sealed':bool(published[0]['scan'].get('sealedAt')),'warnings':warnings,'assessments':sorted([row['title'],row['severity']['level']] for row in published[1]['findings'])}))`,
- fileURLToPath(new URL("../../scripts", import.meta.url)),
- f.root,
- f.context.scanId!,
- status,
- ],
- );
- assert.deepEqual(JSON.parse(stdout), {
- count,
- sealed: true,
- warnings: [],
- assessments: findings
- .map((finding) => [finding.title, finding.severity.level])
- .sort(),
- });
+print(json.dumps({'count':len(published[1]['findings']),'status':published[0]['scan']['status'],'sealed':bool(published[0]['scan'].get('sealedAt')),'warnings':warnings,'assessments':sorted([row['title'],row['severity']['level']] for row in published[1]['findings'])}))`,
+ fileURLToPath(new URL("../../scripts", import.meta.url)),
+ f.root,
+ f.context.scanId!,
+ status,
+ ],
+ );
+ assert.deepEqual(JSON.parse(stdout), {
+ count,
+ status,
+ sealed: true,
+ warnings: [],
+ assessments: findings
+ .map((finding) => [finding.title, finding.severity.level])
+ .sort(),
});
- }
+ });
}
}
diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_scan_identity.ts b/plugins/codex-security/mcp-app/tests/test_artifact_scan_identity.ts
index b75bf799b..17382a17d 100644
--- a/plugins/codex-security/mcp-app/tests/test_artifact_scan_identity.ts
+++ b/plugins/codex-security/mcp-app/tests/test_artifact_scan_identity.ts
@@ -30,6 +30,32 @@ import {
} from "./scan-identity-recovery-fixture.ts";
const execFileAsync = promisify(execFile);
+const savedWorker = (root: string) => ({
+ id: "reviewer",
+ kind: "discovery",
+ status: "canceled",
+ artifact_dir: root,
+ result_manifest_path: null,
+ attempt: 1,
+});
+
+const checkpointName = (input: unknown) =>
+ createHash("sha256").update(JSON.stringify(input)).digest("hex") + ".json";
+
+async function saveCheckpointAt(
+ writer: DraftFixture,
+ draft: ReturnType<DraftFixture["draft"]>,
+ time: number,
+) {
+ await draftApi.saveScanDraftCheckpoint(writer.context, draft, false);
+ const { handoffClaimToken: _claim, ...snapshot } = draft;
+ await utimes(
+ path.join(writer.root, "checkpoints", checkpointName(snapshot)),
+ time,
+ time,
+ );
+}
+
const variants = {
provenance: [
finding("Synthetic review", {
@@ -865,14 +891,7 @@ for (const [kind, metadata, identifier] of [
})),
});
const comparison = await recoverAndFinalize(normal, recovered, [
- {
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: recoveryRoot,
- result_manifest_path: null,
- attempt: 1,
- },
+ savedWorker(recoveryRoot),
]);
assert.equal(
comparison.normal.length,
@@ -974,14 +993,7 @@ for (const variant of [
})),
});
const comparison = await recoverAndFinalize(normal, recovered, [
- {
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: rwroot,
- result_manifest_path: null,
- attempt: 1,
- },
+ savedWorker(rwroot),
]);
assert.equal(comparison.normal.length, withSibling ? 2 : 1);
assert.deepEqual(comparison.recovered, comparison.normal);
@@ -1036,14 +1048,7 @@ for (const layout of ["standard", "diff", "worker"] as const) {
path.join(recovered.root, "reviewer"),
"worker",
);
- workers.push({
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: recoveredSource.root,
- result_manifest_path: null,
- attempt: 1,
- });
+ workers.push(savedWorker(recoveredSource.root));
}
const first = finding("First review", {
provenance: { source: "local_plugin", candidateId: "candidate-1" },
@@ -1261,14 +1266,7 @@ for (const field of ["reportId", "ledgerRowId"]) {
})),
});
const result = await recoverAndFinalize(normal, recovered, [
- {
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: recoveredRoot,
- result_manifest_path: null,
- attempt: 1,
- },
+ savedWorker(recoveredRoot),
]);
assert.equal(result.normal.length, 1);
assert.equal(result.recovered.length, 1);
@@ -1410,16 +1408,7 @@ test("deep: refined candidate retains independent cross-location report", async
const result = await recoverAndFinalize(
normal,
recovered,
- [
- {
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: recoveredRoot,
- result_manifest_path: null,
- attempt: 1,
- },
- ],
+ [savedWorker(recoveredRoot)],
true,
);
@@ -1595,8 +1584,6 @@ for (const authored of [false, true]) {
});
}
-const checkpointName = (input: unknown) =>
- createHash("sha256").update(JSON.stringify(input)).digest("hex") + ".json";
for (const order of ["earlier-first", "later-first"]) {
for (const metadata of ["extensions", "provenance"] as const) {
for (const initialLevel of ["low", "high"]) {
@@ -1634,16 +1621,7 @@ for (const order of ["earlier-first", "later-first"]) {
);
for (const [index, draft] of [oldDraft, newDraft].entries()) {
await worker.write(draft);
- await draftApi.saveScanDraftCheckpoint(
- recoveryWorker.context,
- draft,
- false,
- );
- await utimes(
- path.join(recoveredRoot, "checkpoints", checkpointName(draft)),
- 100 + index * 100,
- 100 + index * 100,
- );
+ await saveCheckpointAt(recoveryWorker, draft, 100 + index * 100);
}
const saved = JSON.parse(
await readFile(path.join(normalRoot, "result.json"), "utf8"),
@@ -1662,16 +1640,7 @@ for (const order of ["earlier-first", "later-first"]) {
const result = await recoverAndFinalize(
normal,
recovered,
- [
- {
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: recoveredRoot,
- result_manifest_path: null,
- attempt: 1,
- },
- ],
+ [savedWorker(recoveredRoot)],
true,
);
assert.deepEqual(result.warnings, []);
@@ -1831,15 +1800,6 @@ for (const authored of [false, true]) {
});
}
-const savedWorker = (root: string) => ({
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: root,
- result_manifest_path: null,
- attempt: 1,
-});
-
for (const metadata of ["provenance", "extensions"] as const) {
for (const shape of ["shared siblings", "reportId", "ledgerRowId"]) {
test(`raw group authority preserves ${shape} with ${metadata} candidates`, async (t) => {
@@ -1927,13 +1887,7 @@ for (const layout of ["standard", "diff", "deep"] as const) {
};
const update = { ...normal.draft(), findings: [revised] };
await normal.write(update);
- await draftApi.saveScanDraftCheckpoint(recovered.context, update, false);
- const { handoffClaimToken: _claim, ...checkpoint } = update;
- await utimes(
- path.join(recovered.root, "checkpoints", checkpointName(checkpoint)),
- 200,
- 200,
- );
+ await saveCheckpointAt(recovered, update, 200);
const result = await recoverAndFinalize(
normal,
recovered,
@@ -1978,13 +1932,7 @@ for (const layout of ["standard", "diff"] as const) {
saved.findings.map((row: FixtureFinding) => row.identity),
before.findings.map((row: FixtureFinding) => row.identity),
);
- await draftApi.saveScanDraftCheckpoint(recovered.context, update, false);
- const { handoffClaimToken: _claim, ...checkpoint } = update;
- await utimes(
- path.join(recovered.root, "checkpoints", checkpointName(checkpoint)),
- 200,
- 200,
- );
+ await saveCheckpointAt(recovered, update, 200);
const result = await recoverAndFinalize(
normal,
recovered,
@@ -2084,17 +2032,7 @@ for (const layout of ["standard", "diff", "worker"] as const) {
for (const f of [writer, interrupted]) await f.write(terminal);
await dateDraftFiles(recoveredRoot, 100);
await writer.write(progress);
- await draftApi.saveScanDraftCheckpoint(
- interrupted.context,
- progress,
- false,
- );
- const { handoffClaimToken: _claim, ...rawProgress } = progress;
- await utimes(
- path.join(recoveredRoot, "checkpoints", checkpointName(rawProgress)),
- 200,
- 200,
- );
+ await saveCheckpointAt(interrupted, progress, 200);
if (layout === "worker") {
const saved = JSON.parse(
await readFile(path.join(normalRoot, "result.json"), "utf8"),
@@ -2365,17 +2303,7 @@ for (const layout of ["standard", "diff", "deep"] as const) {
const rows = reverse ? [second, revised] : [revised, second];
await normal.write({ ...normal.draft(), findings: rows });
const update = { ...recovered.draft(), findings: rows };
- await draftApi.saveScanDraftCheckpoint(
- recovered.context,
- update,
- false,
- );
- const { handoffClaimToken: _claim, ...checkpoint } = update;
- await utimes(
- path.join(recovered.root, "checkpoints", checkpointName(checkpoint)),
- 200,
- 200,
- );
+ await saveCheckpointAt(recovered, update, 200);
const result = await recoverAndFinalize(
normal,
recovered,
@@ -2405,17 +2333,7 @@ for (const layout of ["standard", "diff", "deep"] as const) {
for (const [index, rows] of [[first], [second], finalRows].entries()) {
await normal.write({ ...normal.draft(), findings: rows });
const update = { ...recovered.draft(), findings: rows };
- await draftApi.saveScanDraftCheckpoint(
- recovered.context,
- update,
- false,
- );
- const { handoffClaimToken: _claim, ...checkpoint } = update;
- await utimes(
- path.join(recovered.root, "checkpoints", checkpointName(checkpoint)),
- (index + 1) * 100,
- (index + 1) * 100,
- );
+ await saveCheckpointAt(recovered, update, (index + 1) * 100);
}
const result = await recoverAndFinalize(
normal,
@@ -2518,17 +2436,7 @@ for (const layout of ["standard", "diff", "deep"] as const) {
const rows = reversed ? [sibling, first] : [first, sibling];
await normal.write({ ...normal.draft(), findings: rows });
const update = { ...recovered.draft(), findings: rows };
- await draftApi.saveScanDraftCheckpoint(
- recovered.context,
- update,
- false,
- );
- const { handoffClaimToken: _claim, ...checkpoint } = update;
- await utimes(
- path.join(recovered.root, "checkpoints", checkpointName(checkpoint)),
- 200,
- 200,
- );
+ await saveCheckpointAt(recovered, update, 200);
const result = await recoverAndFinalize(
normal,
recovered,
@@ -2573,17 +2481,7 @@ for (const layout of ["standard", "diff", "deep"] as const) {
const rows = reversed ? [added, second] : [second, added];
await normal.write({ ...normal.draft(), findings: rows });
const update = { ...recovered.draft(), findings: rows };
- await draftApi.saveScanDraftCheckpoint(
- recovered.context,
- update,
- false,
- );
- const { handoffClaimToken: _claim, ...checkpoint } = update;
- await utimes(
- path.join(recovered.root, "checkpoints", checkpointName(checkpoint)),
- 200,
- 200,
- );
+ await saveCheckpointAt(recovered, update, 200);
const result = await recoverAndFinalize(
normal,
recovered,
@@ -2613,13 +2511,7 @@ for (const layout of ["standard", "diff", "deep"] as const) {
const refined = { ...first, extensions: { [field]: "report-1" } };
await normal.write({ ...normal.draft(), findings: [refined] });
const update = { ...recovered.draft(), findings: [refined] };
- await draftApi.saveScanDraftCheckpoint(recovered.context, update, false);
- const { handoffClaimToken: _claim, ...checkpoint } = update;
- await utimes(
- path.join(recovered.root, "checkpoints", checkpointName(checkpoint)),
- 200,
- 200,
- );
+ await saveCheckpointAt(recovered, update, 200);
const result = await recoverAndFinalize(
normal,
recovered,
@@ -2667,16 +2559,7 @@ for (const field of ["description", "source", "version"]) {
await readFile(path.join(workerRoot, "result.json"), "utf8"),
);
await normal.write({ ...normal.draft(), findings: saved.findings });
- const workers = [
- {
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: workerRoot,
- result_manifest_path: null,
- attempt: 1,
- },
- ];
+ const workers = [savedWorker(workerRoot)];
const result = await recoverAndFinalize(
normal,
recovered,
@@ -2715,16 +2598,7 @@ for (const metadata of ["extensions", "provenance"] as const) {
saved.findings = malformedFirst ? [malformed, row] : [row, malformed];
await writeFile(resultFile, JSON.stringify(saved));
await utimes(resultFile, 200, 200);
- const workers = [
- {
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: workerRoot,
- result_manifest_path: null,
- attempt: 1,
- },
- ];
+ const workers = [savedWorker(workerRoot)];
const result = await recoverAndFinalize(
normal,
recovered,
@@ -2784,16 +2658,7 @@ for (const field of ["description", "source", "version"]) {
);
for (const [index, draft] of [oldDraft, newDraft].entries()) {
await worker.write(draft);
- await draftApi.saveScanDraftCheckpoint(
- recoveryWorker.context,
- draft,
- false,
- );
- await utimes(
- path.join(recoveredRoot, "checkpoints", checkpointName(draft)),
- 100 + index * 100,
- 100 + index * 100,
- );
+ await saveCheckpointAt(recoveryWorker, draft, 100 + index * 100);
}
const saved = JSON.parse(
await readFile(path.join(normalRoot, "result.json"), "utf8"),
@@ -2801,16 +2666,7 @@ for (const field of ["description", "source", "version"]) {
assert.equal(saved.findings.length, 1);
assert.equal(saved.findings[0].identity[field], "revised");
await normal.write({ ...normal.draft(), findings: saved.findings });
- const workers = [
- {
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: recoveredRoot,
- result_manifest_path: null,
- attempt: 1,
- },
- ];
+ const workers = [savedWorker(recoveredRoot)];
const result = await recoverAndFinalize(
normal,
recovered,
@@ -2905,12 +2761,7 @@ for (const sameLocation of [false, true]) {
await dateDraftFiles(recovered.root, 200);
await dateDraftFiles(workerRoot, 100);
const checkpoint = { ...worker.draft(), findings: [first, second] };
- await draftApi.saveScanDraftCheckpoint(worker.context, checkpoint, false);
- await utimes(
- path.join(workerRoot, "checkpoints", checkpointName(checkpoint)),
- 300,
- 300,
- );
+ await saveCheckpointAt(worker, checkpoint, 300);
await normal.write({
...normal.draft({}, true),
findings: [parent, second],
@@ -2918,16 +2769,7 @@ for (const sameLocation of [false, true]) {
const result = await recoverAndFinalize(
normal,
recovered,
- [
- {
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: workerRoot,
- result_manifest_path: null,
- attempt: 1,
- },
- ],
+ [savedWorker(workerRoot)],
true,
true,
);
@@ -2951,14 +2793,7 @@ for (const layout of ["standard", "diff", "deep", "worker"] as const) {
for (const root of [normalRoot, recoveredRoot]) await mkdir(root);
normalWriter = draftFixture(normalRoot, "worker");
recoveredWriter = draftFixture(recoveredRoot, "worker");
- workers.push({
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: recoveredRoot,
- result_manifest_path: null,
- attempt: 1,
- });
+ workers.push(savedWorker(recoveredRoot));
}
const raw = finding("Synthetic finding", {
identity: { anchor: "stable" },
@@ -3021,21 +2856,7 @@ for (const layout of ["standard", "diff", "deep", "worker"] as const) {
findings: [revised],
});
const checkpoint = { ...recoveredWriter.draft(), findings: [revised] };
- await draftApi.saveScanDraftCheckpoint(
- recoveredWriter.context,
- checkpoint,
- false,
- );
- const { handoffClaimToken: _claim, ...snapshot } = checkpoint;
- await utimes(
- path.join(
- recoveredWriter.root,
- "checkpoints",
- checkpointName(snapshot),
- ),
- 200,
- 200,
- );
+ await saveCheckpointAt(recoveredWriter, checkpoint, 200);
if (layout === "worker") {
const accepted = JSON.parse(
await readFile(path.join(normalWriter.root, "result.json"), "utf8"),
@@ -3070,14 +2891,7 @@ for (const layout of ["standard", "diff", "deep", "worker"] as const) {
for (const root of [normalRoot, recoveredRoot]) await mkdir(root);
normalWriter = draftFixture(normalRoot, "worker");
recoveredWriter = draftFixture(recoveredRoot, "worker");
- workers.push({
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: recoveredRoot,
- result_manifest_path: null,
- attempt: 1,
- });
+ workers.push(savedWorker(recoveredRoot));
}
const first = finding(
"First review",
@@ -3100,21 +2914,7 @@ for (const layout of ["standard", "diff", "deep", "worker"] as const) {
findings: [revised],
});
const checkpoint = { ...recoveredWriter.draft(), findings: [revised] };
- await draftApi.saveScanDraftCheckpoint(
- recoveredWriter.context,
- checkpoint,
- false,
- );
- const { handoffClaimToken: _claim, ...snapshot } = checkpoint;
- await utimes(
- path.join(
- recoveredWriter.root,
- "checkpoints",
- checkpointName(snapshot),
- ),
- 200,
- 200,
- );
+ await saveCheckpointAt(recoveredWriter, checkpoint, 200);
if (layout === "worker") {
const accepted = JSON.parse(
await readFile(path.join(normalWriter.root, "result.json"), "utf8"),
@@ -3396,14 +3196,7 @@ for (const layout of ["standard", "diff", "deep", "worker"] as const) {
for (const root of [normalRoot, recoveredRoot]) await mkdir(root);
normalWriter = draftFixture(normalRoot, "worker");
recoveredWriter = draftFixture(recoveredRoot, "worker");
- workers.push({
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: recoveredRoot,
- result_manifest_path: null,
- attempt: 1,
- });
+ workers.push(savedWorker(recoveredRoot));
}
const original = finding("Synthetic historical revision", {
identity: { anchor: "stable-report" },
@@ -3928,14 +3721,7 @@ for (const missing of [false, true]) {
await utimes(path.join(recovered.root, "checkpoints", name), 1.5, 1.5);
await utimes(path.join(workerRoot, "result.json"), 1, 1);
const result = await recoverAndFinalize(normal, recovered, [
- {
- id: "reviewer",
- kind: "discovery",
- status: "canceled",
- artifact_dir: workerRoot,
- result_manifest_path: null,
- attempt: 1,
- },
+ savedWorker(workerRoot),
]);
assert.equal(result.normal.length, 1);
assert.deepEqual(result.recovered, result.normal);
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
index 659d3e656..245adb8ec 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_coordinator.ts
@@ -1,5 +1,4 @@
import { testSharedReceiptSurvivesWorkerRetry } from "./deep_scan_receipt_cases.ts";
-import { testSuccessfulDeepCoveragePreservesWorkerReviewStatus } from "./deep_scan_coverage_cases.ts";
import { readJson, writeJson } from "./support/json.ts";
import type { CoordinatorOptions } from "../src/deep-scan/coordinator.js";
import type { ScanDraftInput } from "../src/artifact-scan-draft.js";
@@ -825,46 +824,37 @@ async function testExhaustedRetryFailsScan() {
assert.equal(terminal.manifestPath, undefined);
}
-async function testProviderCybersecurityRiskMessagesReplaceRefusedDiscoveryImmediately() {
- for (const message of [
- "Request blocked by cyberPolicy.",
- "Request blocked by a safety policy violation.",
- "This content was flagged for possible cybersecurity risk.",
- "This content was flagged for potentially high-risk cyber activity.",
- ]) {
- const { fixture, store } = await coordinatorFixture({
- stopAfterNoNew: 4,
- stopAfterConsecutiveErrors: 3,
- maxDiscoveryRuns: 3,
- });
- const executor = new FakeExecutor({
- discoveryFailureMessages: { "discovery-0001": message },
- discoveryCandidateId: "candidate-1",
- });
- const sleeps: number[] = [];
- const terminal = await runCoordinator(fixture, store, executor, {
- random: () => 0,
- retryDelaysMs: [1, 3, 9],
- clock: recordingClock(sleeps),
- });
+async function testPolicyRefusalReplacesDiscoveryImmediately() {
+ const { fixture, store } = await coordinatorFixture({
+ stopAfterNoNew: 4,
+ stopAfterConsecutiveErrors: 3,
+ maxDiscoveryRuns: 3,
+ });
+ const executor = new FakeExecutor({
+ discoveryFailureMessages: {
+ "discovery-0001": "Request blocked by cyberPolicy.",
+ },
+ discoveryCandidateId: "candidate-1",
+ });
+ const sleeps: number[] = [];
+ const terminal = await runCoordinator(fixture, store, executor, {
+ random: () => 0,
+ retryDelaysMs: [1, 3, 9],
+ clock: recordingClock(sleeps),
+ });
- assert.equal(terminal?.status, "succeeded", message);
- assert.equal(terminal?.terminalReason, "capped");
- assert.equal(executor.discoveryAttempts.size, 3);
- assert.equal(executor.discoveryAttempts.get("discovery-0001"), 1, message);
- assert.deepEqual(
- sleeps,
- [],
- `a refused conversation must not be resumed: ${message}`,
- );
- assert.equal(store.run.consecutiveErrors, 0, message);
- const refusal = [...store.workers.values()].find(
- (worker) =>
- path.basename(path.dirname(worker.promptPath)) === "discovery-0001",
- );
- assert.equal(refusal?.replaceableFailureKind, "policy_refusal", message);
- assert.equal(refusal?.status, "canceled");
- }
+ assert.equal(terminal?.status, "succeeded");
+ assert.equal(terminal?.terminalReason, "capped");
+ assert.equal(executor.discoveryAttempts.size, 3);
+ assert.equal(executor.discoveryAttempts.get("discovery-0001"), 1);
+ assert.deepEqual(sleeps, [], "a refused conversation must not be resumed");
+ assert.equal(store.run.consecutiveErrors, 0);
+ const refusal = [...store.workers.values()].find(
+ (worker) =>
+ path.basename(path.dirname(worker.promptPath)) === "discovery-0001",
+ );
+ assert.equal(refusal?.replaceableFailureKind, "policy_refusal");
+ assert.equal(refusal?.status, "canceled");
}
async function testTransientDiscoveryErrorsRetainRecovery() {
@@ -4264,7 +4254,6 @@ try {
await testCompletionOrdering();
await testSaturationPreservesFindingAlreadyBuffered();
await testSaturationOmitsWorkerAcceptedDuringCancellation();
- await testSuccessfulDeepCoveragePreservesWorkerReviewStatus();
await testSuccessfulDeepCoverageRetainsIndependentWorkerReviewStatus();
await testSaturationIgnoresDiscoveryCancellationWriteFailure();
await testSaturationIgnoresDiscoveryCancellationWriteFailure(true);
@@ -4281,7 +4270,7 @@ try {
await testSlowReducerPausesDiscoveryRefill();
await testSingletonHardCapReduction();
await testExhaustedRetryFailsScan();
- await testProviderCybersecurityRiskMessagesReplaceRefusedDiscoveryImmediately();
+ await testPolicyRefusalReplacesDiscoveryImmediately();
await testTransientDiscoveryErrorsRetainRecovery();
await testConsecutiveCybersecurityRefusalsFailAtConfiguredThreshold();
await testRecoverableWorkerErrorsCannotFailScan();
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
index 3a5e11fd1..dd97f60e3 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_executor.ts
@@ -1,4 +1,3 @@
-import { testArtifactStartupTimeoutClassification } from "./deep_scan_startup_cases.ts";
import {
workerGitEnvironment,
workerRuntimeEnvironment,
@@ -217,7 +216,6 @@ try {
await check(t, testCodeModeFrameDiagnosticBoundaries);
await check(t, testUsageLimitClassification);
await check(t, testOsErrorCodesRemainRetryable);
- await check(t, testTextualMissingPathErrorsRemainRetryable);
await check(t, testWorkerErrorClassificationPreservesExplicitFailures);
if (process.platform !== "win32") {
await check(t, testRetryNotificationDoesNotInterruptTurn);
@@ -234,16 +232,11 @@ try {
"artifact retry diagnostic: Unicode",
);
await check(t, testCodeModeFrameDiagnosticSurvivesSuccessfulTurn);
- await check(t, testUnstructuredConfigurationFailureRemainsRetryable);
- await check(t, testUnstructuredThreadStartFailureRemainsRetryable);
+ await check(t, testSetupExitDiagnosticsRemainRetryable);
await check(t, testPolicyFailuresRemainWorkerErrors);
await check(t, testMalformedCommandEventsRemainRetryable);
await check(t, testRateLimitPolicyFailureRemainsRetryable);
- await check(
- t,
- () => testArtifactStartupTimeoutClassification(runFixtureWorker),
- testArtifactStartupTimeoutClassification.name,
- );
+ await check(t, testArtifactStartupTimeoutRemainsRetryable);
}
});
} finally {
@@ -330,19 +323,6 @@ function testOsErrorCodesRemainRetryable() {
}
}
-function testTextualMissingPathErrorsRemainRetryable() {
- for (const diagnostic of [
- "Error: No such file or directory (os error 2)",
- "Error: The system cannot find the file specified. (os error 2)",
- ]) {
- const original = new Error(
- ["Codex Exec exited with code 1:", diagnostic].join("\n"),
- );
- const classified = classifyCodexWorkerError(original);
- assert.equal(classified, original);
- }
-}
-
function testWorkerErrorClassificationPreservesExplicitFailures() {
const explicit = new DeepScanNonRetryableError(
"Explicitly rejected worker permission profile.",
@@ -391,8 +371,12 @@ function testWorkerErrorClassificationPreservesExplicitFailures() {
"not logged in",
"missing API key",
"Codex Exec exited with code 2: failed to load configuration: invalid value",
+ "Codex Exec exited with code 1:\nError: No such file or directory (os error 2)",
+ "Codex Exec exited with code 1:\nError: The system cannot find the file specified. (os error 2)",
"chatgpt authentication required for remote plugin catalog; api key auth is not supported",
"chatgpt authentication required to sync remote plugins; api key auth is not supported",
+ "required MCP servers failed to initialize: codex_security_artifacts: timed out handshaking with MCP server after 30s",
+ "required MCP servers failed to initialize: other_server: request timed out",
]) {
const original = new Error(message);
assert.equal(classifyCodexWorkerError(original), original);
@@ -3663,22 +3647,29 @@ async function completedWorkerSettlesWithoutWaitingForProcessExit(
}
}
-async function testUnstructuredConfigurationFailureRemainsRetryable() {
+async function testArtifactStartupTimeoutRemainsRetryable() {
await assert.rejects(
- runFixtureWorker("CONFIG_ERROR", "setup"),
- (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>
- error?.name === "Error" &&
- error.message.startsWith("Codex Exec exited with code 2:"),
+ runFixtureWorker("ARTIFACT_MCP_STARTUP_TIMEOUT"),
+ (error: Error) =>
+ error.name === "Error" &&
+ error.message.includes("cs_artifacts: request timed out") &&
+ !isCodexCybersecurityPolicyRefusal(error),
);
}
-async function testUnstructuredThreadStartFailureRemainsRetryable() {
- await assert.rejects(
- runFixtureWorker("THREAD_START_CONFIG_ERROR", "setup"),
- (error: NodeJS.ErrnoException & { cause?: NodeJS.ErrnoException }) =>
- error?.name === "Error" &&
- error.message.startsWith("Codex Exec exited with code 1:"),
- );
+async function testSetupExitDiagnosticsRemainRetryable() {
+ for (const [prompt, code] of [
+ ["CONFIG_ERROR", 2],
+ ["THREAD_START_CONFIG_ERROR", 1],
+ ] as const) {
+ await assert.rejects(
+ runFixtureWorker(prompt, "setup"),
+ (error: Error) =>
+ error.name === "Error" &&
+ error.message.startsWith(`Codex Exec exited with code ${code}:`),
+ prompt,
+ );
+ }
}
async function testPolicyFailuresRemainWorkerErrors() {
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts
index 9d00b4220..18f2d31de 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_permission_profile_preflight.ts
@@ -65,8 +65,6 @@ const unsupportedConfiguration = (error: Error) =>
error.message.includes("with this Codex configuration");
await testAllowedProfileAndRawArgv();
-await testPreflightStartsInWorkerCwd();
-await testProvidedEnvForwardedWithoutMutation();
await testOpenAiApiKeyFallbackPreservesNativeAuthentication();
await testSequentialPaginatedResponsesAcrossChunks();
await testMalformedStreamFailsClosed();
@@ -260,6 +258,13 @@ async function testDirectScanSession() {
}
async function testAllowedProfileAndRawArgv() {
+ const codexHome = "/fixture/canonical-codex-home";
+ const env = Object.freeze({
+ ...process.env,
+ CODEX_HOME: codexHome,
+ DEEP_SCAN_PREFLIGHT_ENV_SENTINEL: "same-snapshot",
+ });
+
await withFakeCodex(
{
configResult: configReadResult({
@@ -288,8 +293,19 @@ async function testAllowedProfileAndRawArgv() {
},
],
},
- async ({ codexPath, cwd, argvPath, callsPath }) => {
+ async ({
+ codexPath,
+ cwd,
+ argvPath,
+ callsPath,
+ envPath,
+ cwdPath,
+ children,
+ terminatedPath,
+ }) => {
+ assert.notEqual(cwd, process.cwd());
await preflightDeepScanWorkerPermissionProfile({
+ env,
codexPath,
cwd,
configOverrides: [
@@ -323,31 +339,17 @@ async function testAllowedProfileAndRawArgv() {
assert.deepEqual(calls[0].params.capabilities, { experimentalApi: true });
assert.deepEqual(calls[2].params, { cwd, includeLayers: false });
assert.deepEqual(calls[3].params, { cwd });
- },
- { longExecutable: true },
- );
-}
-
-async function testProvidedEnvForwardedWithoutMutation() {
- const codexHome = "/fixture/canonical-codex-home";
- const env = Object.freeze({
- ...process.env,
- CODEX_HOME: codexHome,
- DEEP_SCAN_PREFLIGHT_ENV_SENTINEL: "same-snapshot",
- });
-
- await withFakeCodex(
- {
- configResult: configReadResult(expectedProfile),
- catalogResults: [catalogResult(true)],
- },
- async ({ codexPath, cwd, envPath }) => {
- await preflight(codexPath, cwd, env);
+ assert.equal(
+ await realpath(await readJson(cwdPath)),
+ await realpath(cwd),
+ );
assert.deepEqual(await readJson(envPath), {
codexHome,
sentinel: "same-snapshot",
});
+ await assertPreflightStopped(children, terminatedPath);
},
+ { longExecutable: true },
);
}
@@ -406,44 +408,6 @@ async function testOpenAiApiKeyFallbackPreservesNativeAuthentication() {
}
}
-async function testPreflightStartsInWorkerCwd() {
- await withFakeCodex(
- {
- configResult: configReadResult(expectedProfile),
- catalogResults: [catalogResult(true)],
- },
- async ({
- codexPath,
- cwd,
- cwdPath,
- callsPath,
- terminatedPath,
- children,
- }) => {
- assert.notEqual(cwd, process.cwd());
- await preflight(codexPath, cwd);
-
- const childCwd = await readJson(cwdPath);
- assert.equal(await realpath(childCwd), await realpath(cwd));
- const calls = await readJsonLines(callsPath);
- assert.deepEqual(
- calls.find((call) => call.method === "config/read").params,
- {
- cwd,
- includeLayers: false,
- },
- );
- assert.deepEqual(
- calls.find((call) => call.method === "permissionProfile/list").params,
- {
- cwd,
- },
- );
- await assertPreflightStopped(children, terminatedPath);
- },
- );
-}
-
async function testSequentialPaginatedResponsesAcrossChunks() {
await withFakeCodex(
{
@@ -1064,17 +1028,12 @@ async function assertPreflightStopped(
}
}
-function preflight(
- codexPath: string,
- cwd: string,
- env?: Record<string, string>,
-) {
+function preflight(codexPath: string, cwd: string) {
return preflightDeepScanWorkerPermissionProfile({
codexPath,
cwd,
configOverrides: rawOverrides,
expectedProfile,
- ...(env === undefined ? {} : { env }),
signal: new AbortController().signal,
});
}
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_publication_integration.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_publication_integration.ts
index 8b07dfadf..844dc54e8 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_publication_integration.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_publication_integration.ts
@@ -18,6 +18,7 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import { buildServer } from "./build-server.ts";
import { importModule } from "./import-module.ts";
+import { readJson } from "./support/json.ts";
import type { RunArtifactWorkbench } from "../src/artifact-context.ts";
import type {
DeepScanRunState,
@@ -683,9 +684,12 @@ async function assertInterleavedProgressRetained(fixture: PublicationFixture) {
async function createFixture(t: TestContext, paths: FixturePaths = {}) {
const root = await mkdtemp(path.join(tmpdir(), "deep-publication-"));
- let client: Client | undefined;
+ const client = new Client({
+ name: "publication-integration",
+ version: "1.0.0",
+ });
t.after(async () => {
- await client?.close();
+ await client.close();
await rm(root, { recursive: true, force: true });
});
const targetPath = path.join(root, paths.target ?? "target");
@@ -727,7 +731,6 @@ async function createFixture(t: TestContext, paths: FixturePaths = {}) {
threadId: owner,
scanRoot: environment.CODEX_SECURITY_SCAN_ROOT,
});
- client = new Client({ name: "publication-integration", version: "1.0.0" });
const transport = new StdioClientTransport({
command: process.execPath,
args: [serverPath, "--stdio"],
@@ -736,7 +739,7 @@ async function createFixture(t: TestContext, paths: FixturePaths = {}) {
stderr: "pipe",
});
if (transport.stderr instanceof Readable) transport.stderr.resume();
- await client.connect(transport);
+ let connected: Promise<void> | undefined;
return {
run,
store,
@@ -745,9 +748,10 @@ async function createFixture(t: TestContext, paths: FixturePaths = {}) {
environment,
setTime(offset: number) {
now = new Date(Date.parse(instant) + offset * 1_000).toISOString();
+ return now;
},
- call(name: string, args: Record<string, unknown> | ScanDraftInput) {
- assert.ok(client);
+ async call(name: string, args: Record<string, unknown> | ScanDraftInput) {
+ await (connected ??= client.connect(transport));
return client.callTool({
name,
arguments: { ...args },
@@ -1171,30 +1175,33 @@ test("accepted response recovery cannot replace a later publication from the sam
assert.deepEqual(await snapshot(f.run), newer);
});
-for (const code of ["EPERM", "EACCES", "EIO"]) {
- test(`optional receipt cleanup ${code} preserves publication success`, async (t) => {
- const f = await publicationFixture(t);
- const filesystem = await import("node:fs");
- const original = filesystem.promises.rm;
- t.mock.method(
+test("receipt cleanup failures preserve publication success", async (t) => {
+ const f = await publicationFixture(t);
+ const filesystem = await import("node:fs");
+ const original = filesystem.promises.rm;
+ for (const [index, code] of ["EPERM", "EACCES", "EIO"].entries()) {
+ const completedAt = f.setTime(10 + index);
+ let attempted = false;
+ const remove = t.mock.method(
filesystem.promises,
"rm",
async (...[file, options]: Parameters<typeof original>) => {
- if (String(file).endsWith(".accepted.json"))
+ if (String(file).endsWith(".accepted.json")) {
+ attempted = true;
throw Object.assign(new Error(`Synthetic receipt cleanup ${code}`), {
code,
});
+ }
return original(file, options);
},
);
assert.equal((await f.publish()).status, "draft_written");
- assert.ok(
- JSON.parse((await snapshot(f.run)).files["scan-manifest.json"]).scan
- .completedAt,
- );
- t.mock.restoreAll();
- });
-}
+ const manifest = await readJson(f.run.scanDir, "scan-manifest.json");
+ assert.equal(manifest.scan.completedAt, completedAt, code);
+ assert.ok(attempted, code);
+ remove.mock.restore();
+ }
+});
test("receipt I/O failure cannot authorize replay after response loss", async (t) => {
const f = await publicationFixture(t);
@@ -1220,13 +1227,13 @@ test("receipt I/O failure cannot authorize replay after response loss", async (t
assert.deepEqual(await readdir(path.join(f.run.scanDir, "drafts")), []);
});
-for (const code of ["EACCES", "ECONNRESET"]) {
- for (const matching of [false, true]) {
- test(`publication ${code} before acceptance is one attempt (matching=${matching})`, async (t) => {
- const f = await publicationFixture(t);
- if (matching) await f.publish();
- const before = await snapshot(f.run);
- const workers = await f.workers();
+for (const matching of [false, true]) {
+ test(`failure before acceptance is one attempt (matching=${matching})`, async (t) => {
+ const f = await publicationFixture(t);
+ if (matching) await f.publish();
+ const before = await snapshot(f.run);
+ const workers = await f.workers();
+ for (const code of ["EACCES", "ECONNRESET"]) {
const original = Object.assign(new Error(`Synthetic precommit ${code}`), {
code,
});
@@ -1244,8 +1251,8 @@ for (const code of ["EACCES", "ECONNRESET"]) {
assert.deepEqual(await snapshot(f.run), before);
assert.deepEqual(await f.workers(), workers);
assert.deepEqual(await readdir(path.join(f.run.scanDir, "drafts")), []);
- });
- }
+ }
+ });
}
for (const takeover of ["coordinator", "continuation"]) {
diff --git a/plugins/codex-security/mcp-app/tests/test_inventory_paths.ts b/plugins/codex-security/mcp-app/tests/test_inventory_paths.ts
index c98e74ecf..2dbe96d70 100644
--- a/plugins/codex-security/mcp-app/tests/test_inventory_paths.ts
+++ b/plugins/codex-security/mcp-app/tests/test_inventory_paths.ts
@@ -8,6 +8,14 @@ import { join } from "node:path";
import { importSource } from "./import-module.ts";
import type { WindowsBinding } from "../../native/windows-binding.mjs";
+const helperUrl = new URL(
+ "../../../../sdk/typescript/_bundled_plugin/mcp/helpers.mjs",
+ import.meta.url,
+);
+const { git, runTool } = await importSource("src/helpers/inventory-git.ts", {
+ define: { "import.meta.url": JSON.stringify(helperUrl.href) },
+});
+
for (const [name, details] of [
["named", { code: "ENOEXEC" }],
[
@@ -19,16 +27,6 @@ for (const [name, details] of [
`POSIX tools run executable text after a ${name} exec-format launch error`,
{ skip: process.platform === "win32" },
async () => {
- const { runTool } = await importSource("src/helpers/inventory-git.ts", {
- define: {
- "import.meta.url": JSON.stringify(
- new URL(
- "../../../../sdk/typescript/_bundled_plugin/mcp/helpers.mjs",
- import.meta.url,
- ).href,
- ),
- },
- });
const root = fs.mkdtempSync(join(tmpdir(), "inventory-exec-format-"));
const tool = join(root, "plain tool");
const diagnostic =
@@ -96,16 +94,6 @@ test(
"POSIX tools keep helper input separate from stdin and startup status",
{ skip: process.platform === "win32" },
async () => {
- const { runTool } = await importSource("src/helpers/inventory-git.ts", {
- define: {
- "import.meta.url": JSON.stringify(
- new URL(
- "../../../../sdk/typescript/_bundled_plugin/mcp/helpers.mjs",
- import.meta.url,
- ).href,
- ),
- },
- });
const root = fs.mkdtempSync(join(tmpdir(), "inventory-input-fds-"));
const denied = join(root, "not-executable");
fs.writeFileSync(denied, "#!/bin/sh\n", { mode: 0o600 });
@@ -180,16 +168,6 @@ test(
"POSIX tools retain loader settings across the raw argument shell bridge",
{ skip: process.platform === "win32" },
async () => {
- const { runTool } = await importSource("src/helpers/inventory-git.ts", {
- define: {
- "import.meta.url": JSON.stringify(
- new URL(
- "../../../../sdk/typescript/_bundled_plugin/mcp/helpers.mjs",
- import.meta.url,
- ).href,
- ),
- },
- });
const names = ["LD_LIBRARY_PATH", "DYLD_LIBRARY_PATH"];
const saved = names.map((name) => [name, process.env[name]] as const);
try {
@@ -226,16 +204,6 @@ test(
"Windows inventory honors an explicit Git .com executable beside a .com.exe sibling",
{ skip: process.platform !== "win32" },
async () => {
- const helperUrl = new URL(
- "../../../../sdk/typescript/_bundled_plugin/mcp/helpers.mjs",
- import.meta.url,
- );
- const { git, runTool } = await importSource(
- "src/helpers/inventory-git.ts",
- {
- define: { "import.meta.url": JSON.stringify(helperUrl.href) },
- },
- );
const binary = createRequire(helperUrl).resolve(
`./native/win32-${process.arch}/windows.node`,
);
@@ -325,16 +293,6 @@ test(
"Windows tools preserve PATH selection and resolve relative PATH and cwd-only executables",
{ skip: process.platform !== "win32" },
async () => {
- const { runTool } = await importSource("src/helpers/inventory-git.ts", {
- define: {
- "import.meta.url": JSON.stringify(
- new URL(
- "../../../../sdk/typescript/_bundled_plugin/mcp/helpers.mjs",
- import.meta.url,
- ).href,
- ),
- },
- });
const root = fs.mkdtempSync(join(tmpdir(), "inventory-relative-path-"));
const repo = join(root, "repo");
const tools = join(repo, "tools");
@@ -401,12 +359,7 @@ for (const platform of ["linux", "win32"]) {
{
define: {
"process.platform": JSON.stringify(platform),
- "import.meta.url": JSON.stringify(
- new URL(
- "../../../../sdk/typescript/_bundled_plugin/mcp/helpers.mjs",
- import.meta.url,
- ).href,
- ),
+ "import.meta.url": JSON.stringify(helperUrl.href),
},
},
);
@@ -429,12 +382,7 @@ for (const platform of ["linux", "win32"]) {
const { sampleFile, createSourceSampler, PREVIEW_READ_BYTES } =
await importSource("src/helpers/source-preview.ts", {
define: {
- "import.meta.url": JSON.stringify(
- new URL(
- "../../../../sdk/typescript/_bundled_plugin/mcp/helpers.mjs",
- import.meta.url,
- ).href,
- ),
+ "import.meta.url": JSON.stringify(helperUrl.href),
},
});
@@ -477,12 +425,7 @@ test("POSIX file identities retain all 64 inode bits", async () => {
const { sameFile } = await importSource("src/helpers/inventory-paths.ts", {
define: {
"process.platform": '"linux"',
- "import.meta.url": JSON.stringify(
- new URL(
- "../../../../sdk/typescript/_bundled_plugin/mcp/helpers.mjs",
- import.meta.url,
- ).href,
- ),
+ "import.meta.url": JSON.stringify(helperUrl.href),
},
});
assert.equal(sameFile("left", "right"), false);
diff --git a/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts b/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts
index b8b514bcb..7616108d2 100644
--- a/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts
+++ b/plugins/codex-security/mcp-app/tests/test_mcp_app_smoke.ts
@@ -30,6 +30,20 @@ const continuationOptions = [
},
];
+const continuationQuestion = {
+ name: "request_codex_security_user_input",
+ arguments: {
+ questions: [
+ {
+ header: "Continue?",
+ id: "continue_scan",
+ question: "Should Codex Security continue?",
+ options: continuationOptions,
+ },
+ ],
+ },
+};
+
const sourcePluginRoot = path.resolve(import.meta.dirname, "../..");
const pluginRoot = process.env.CODEX_SECURITY_TEST_PLUGIN_ROOT
? path.resolve(process.env.CODEX_SECURITY_TEST_PLUGIN_ROOT)
@@ -230,6 +244,9 @@ async function assertBundledNodeLauncher() {
await bundledNodeServer.initialize("codex-security-bundled-node-smoke"),
);
assertNoError(await bundledNodeServer.request(2, "tools/list"));
+ const input = await bundledNodeServer.callTool(3, continuationQuestion);
+ assertNoError(input);
+ assert.deepEqual(input.result.structuredContent, { status: "unavailable" });
if (!windows) {
assert.equal(
await readFile(path.join(emptyPath, "bundled-node-used"), "utf8"),
@@ -237,109 +254,58 @@ async function assertBundledNodeLauncher() {
);
}
} finally {
- bundledNodeServer.stop();
+ await bundledNodeServer.stop();
await rm(emptyPath, { recursive: true, force: true });
}
}
-async function assertPythonLaunchError(code: "ENOENT" | "EACCES") {
+async function assertPythonHelperFailures() {
const python = path.join(tmpdir(), `codex-security-python-${randomUUID()}`);
- if (code === "EACCES") await writeFile(python, "", { mode: 0o600 });
- const missingPythonServer = startTestServer({
+ const server = startTestServer({
cwd: pluginRoot,
- env: {
- CODEX_SECURITY_STATE_DIR: stateDir,
- PYTHON: python,
- },
+ env: { CODEX_SECURITY_STATE_DIR: stateDir, PYTHON: python },
});
- try {
- assertNoError(
- await missingPythonServer.initialize(
- "codex-security-missing-python-smoke",
- ),
- );
- const response = await missingPythonServer.callTool(2, {
+ async function inspectErrorText(id: number) {
+ const response = await server.callTool(id, {
name: "inspect_codex_security_target",
arguments: { targetPath: target },
});
assert.equal(response.result.isError, true);
- const errorText = response.result.content
- .map((item: { text: string }) => item.text)
+ return response.result.content
+ .map((item: { text?: string }) => item.text)
.join(" ");
- assert.ok(errorText.includes(`spawn ${python} ${code}`), errorText);
- if (code === "ENOENT") {
- assert.match(errorText, /could not start its Python 3 helper/);
- assert.match(errorText, /bundled Python runtime/);
- assert.match(errorText, /set the PYTHON environment variable/);
- } else {
- assert.doesNotMatch(errorText, /Reinstall or update/);
- }
- } finally {
- await missingPythonServer.stop();
- await rm(python, { force: true });
}
-}
-
-async function assertWorkbenchStdinFailureDoesNotCrashServer() {
- if (process.platform === "win32") return;
- const helperRoot = await temporaryDirectory(
- "codex-security-early-exit-helper-",
- );
- const helper = path.join(helperRoot, "python");
- await writeFile(helper, "#!/bin/sh\nexit 2\n");
- await chmod(helper, 0o755);
- const server = startTestServer({
- cwd: pluginRoot,
- env: { CODEX_SECURITY_STATE_DIR: stateDir, PYTHON: helper },
- });
- try {
- assertNoError(await server.initialize("codex-security-early-exit-smoke"));
- const response = await server.callTool(2, {
- name: "update_codex_security_scan_context",
- arguments: {
- scanId: randomUUID(),
- userContext: "x".repeat(2 * 1024 * 1024),
- },
- _meta: { "openai/threadId": "fixture-thread" },
- });
- assert.equal(response.result.isError, true);
- assertNoError(await server.request(3, "tools/list"));
- } finally {
- server.stop();
- await rm(helperRoot, { recursive: true, force: true });
- }
-}
-
-async function assertUnavailableUserInputFallback() {
- const noElicitationServer = startTestServer({
- cwd: pluginRoot,
- env: { CODEX_SECURITY_STATE_DIR: stateDir },
- });
try {
assertNoError(
- await noElicitationServer.initialize(
- "codex-security-no-elicitation-smoke",
- ),
+ await server.initialize("codex-security-python-errors-smoke"),
);
- const response = await noElicitationServer.callTool(2, {
- name: "request_codex_security_user_input",
- arguments: {
- questions: [
- {
- header: "Continue?",
- id: "continue_scan",
- question: "Should Codex Security continue?",
- options: continuationOptions,
- },
- ],
- },
- });
- assertNoError(response);
- assert.deepEqual(response.result.structuredContent, {
- status: "unavailable",
- });
+ const missingText = await inspectErrorText(2);
+ assert.ok(missingText.includes(`spawn ${python} ENOENT`), missingText);
+ assert.match(missingText, /could not start its Python 3 helper/);
+ assert.match(missingText, /bundled Python runtime/);
+ assert.match(missingText, /set the PYTHON environment variable/);
+
+ if (process.platform !== "win32") {
+ await writeFile(python, "#!/bin/sh\nexit 2\n", { mode: 0o600 });
+ const deniedText = await inspectErrorText(3);
+ assert.ok(deniedText.includes(`spawn ${python} EACCES`), deniedText);
+ assert.doesNotMatch(deniedText, /Reinstall or update/);
+
+ await chmod(python, 0o755);
+ const earlyExit = await server.callTool(4, {
+ name: "update_codex_security_scan_context",
+ arguments: {
+ scanId: randomUUID(),
+ userContext: "x".repeat(2 * 1024 * 1024),
+ },
+ _meta: { "openai/threadId": "fixture-thread" },
+ });
+ assert.equal(earlyExit.result.isError, true);
+ assertNoError(await server.request(5, "tools/list"));
+ }
} finally {
- await noElicitationServer.stop();
+ await server.stop();
+ await rm(python, { force: true });
}
}
@@ -874,67 +840,44 @@ with sqlite3.connect(sys.argv[1]) as connection:
}
async function assertUserInputFailureLogging() {
- const failingElicitationServer = startTestServer({
- cwd: pluginRoot,
- env: { CODEX_SECURITY_STATE_DIR: stateDir },
- });
- try {
- assertNoError(
- await failingElicitationServer.initialize(
- "codex-security-failing-elicitation-smoke",
- { elicitation: { form: {} } },
+ testServer.sendRequest(9011, "tools/call", continuationQuestion);
+ const elicitationRequest = await testServer.waitForMessage(
+ (message) =>
+ message.method === "elicitation/create" &&
+ message.params.message.includes("Should Codex Security continue?"),
+ "failing Codex Security elicitation request",
+ );
+ testServer.sendError(
+ elicitationRequest.id,
+ -32603,
+ "Fixture elicitation failure",
+ );
+ const logMessage = await testServer.waitForMessage(
+ (message) =>
+ message.method === "notifications/message" &&
+ message.params.data?.error?.message?.includes(
+ "Fixture elicitation failure",
),
- );
- failingElicitationServer.sendRequest(2, "tools/call", {
- name: "request_codex_security_user_input",
- arguments: {
- questions: [
- {
- header: "Continue?",
- id: "continue_scan",
- question: "Should Codex Security continue?",
- options: continuationOptions,
- },
- ],
- },
- });
- const elicitationRequest = await failingElicitationServer.waitForMessage(
- (message) => message.method === "elicitation/create",
- "failing Codex Security elicitation request",
- );
- failingElicitationServer.sendError(
- elicitationRequest.id,
- -32603,
- "Fixture elicitation failure",
- );
- const logMessage = await failingElicitationServer.waitForMessage(
- (message) => message.method === "notifications/message",
- "Codex Security elicitation failure log",
- );
- assert.equal(logMessage.params.level, "warning");
- assert.equal(logMessage.params.logger, "codex-security.user-input");
- assert.equal(logMessage.params.data.event, "elicitation_failed");
- assert.match(
- logMessage.params.data.error.message,
- /Fixture elicitation failure/,
- );
- assert.equal(logMessage.params.data.error.stack, undefined);
- assert.doesNotMatch(
- JSON.stringify(logMessage.params.data),
- /Should Codex Security continue/,
- );
+ "Codex Security elicitation failure log",
+ );
+ assert.equal(logMessage.params.level, "warning");
+ assert.equal(logMessage.params.logger, "codex-security.user-input");
+ assert.equal(logMessage.params.data.event, "elicitation_failed");
+ assert.match(
+ logMessage.params.data.error.message,
+ /Fixture elicitation failure/,
+ );
+ assert.equal(logMessage.params.data.error.stack, undefined);
+ assert.doesNotMatch(
+ JSON.stringify(logMessage.params.data),
+ /Should Codex Security continue/,
+ );
- const response = await failingElicitationServer.waitForMessage(
- (message) => message.id === 2,
- "Codex Security unavailable response after elicitation failure",
- );
- assertNoError(response);
- assert.deepEqual(response.result.structuredContent, {
- status: "unavailable",
- });
- } finally {
- await failingElicitationServer.stop();
- }
+ const response = await testServer.waitForResponse(9011);
+ assertNoError(response);
+ assert.deepEqual(response.result.structuredContent, {
+ status: "unavailable",
+ });
}
async function assertBundledPythonRuntime() {
@@ -1201,6 +1144,17 @@ try {
assert.match(hostedTrustedAccess.result.content[0].text, /Daybreak Blue/);
assert.match(hostedTrustedAccess.result.content[0].text, /Daybreak Red/);
+ const isolatedOtherTrustedAccess = await testServer.callTool(9613, {
+ name: "get_codex_security_daybreak_access",
+ arguments: {},
+ _meta: { threadId: "fixture-isolated-trusted-access-other" },
+ });
+ assertNoError(isolatedOtherTrustedAccess);
+ assert.equal(
+ isolatedOtherTrustedAccess.result.structuredContent.status,
+ "unknown",
+ );
+
const invalidGrantPairs = [
{ level: "tac2", source: "project" },
{ level: "tac2", source: "current_account" },
@@ -1435,41 +1389,9 @@ try {
"unknown",
);
- const isolatedHostedTrustedAccess = await testServer.callTool(9612, {
- name: "get_codex_security_daybreak_access",
- arguments: {},
- _meta: {
- threadId: "fixture-isolated-trusted-access-owner",
- "openai/entitlementContext": {
- schemaVersion: 1,
- entitlements: {
- cyber_trusted_access: grantedTrustedAccess,
- },
- },
- },
- });
- assertNoError(isolatedHostedTrustedAccess);
- assert.deepEqual(
- isolatedHostedTrustedAccess.result.structuredContent,
- grantedDaybreakAccess,
- );
- const isolatedOtherTrustedAccess = await testServer.callTool(9613, {
- name: "get_codex_security_daybreak_access",
- arguments: {},
- _meta: { threadId: "fixture-isolated-trusted-access-other" },
- });
- assertNoError(isolatedOtherTrustedAccess);
- assert.equal(
- isolatedOtherTrustedAccess.result.structuredContent.status,
- "unknown",
- );
-
await assertBundledNodeLauncher();
await assertBundledPythonRuntime();
- await assertPythonLaunchError("ENOENT");
- if (process.platform !== "win32") await assertPythonLaunchError("EACCES");
- await assertWorkbenchStdinFailureDoesNotCrashServer();
- await assertUnavailableUserInputFallback();
+ await assertPythonHelperFailures();
for (const scope of [
"-sub",
" component",
@@ -1478,7 +1400,6 @@ try {
await assertWorkspaceWorksWithoutUiCapability(scope);
await assertHeadlessStandardScanWorksWithoutUiCapability();
await assertDeepScanPersistsRetryableWorkerStartupError();
- await assertUserInputFailureLogging();
if (process.platform !== "win32") {
await rm(launchCwd, { recursive: true, force: true });
}
@@ -1493,8 +1414,13 @@ try {
code: "ENOENT",
});
- const findTool = (name: string) =>
- toolList.result.tools.find((tool: { name: string }) => tool.name === name);
+ const findTool = (name: string) => {
+ const tool = toolList.result.tools.find(
+ (tool: { name: string }) => tool.name === name,
+ );
+ assert.ok(tool, `Missing MCP tool: ${name}`);
+ return tool;
+ };
const launcher = findTool("open_codex_security_workspace");
const startPromptOnlyScan = findTool("start_codex_security_prompt_only_scan");
@@ -1522,10 +1448,6 @@ try {
const inspectTarget = findTool("inspect_codex_security_target");
const inspectSetup = findTool("inspect_codex_security_setup");
const requestUserInput = findTool("request_codex_security_user_input");
- assert.ok(
- requestUserInput,
- "Expected the Codex Security user-input fallback tool.",
- );
assert.deepEqual(requestUserInput.inputSchema.required, ["questions"]);
testServer.sendRequest(9000, "tools/call", {
name: "request_codex_security_user_input",
@@ -1725,11 +1647,12 @@ try {
assertNoError(response);
assert.deepEqual(response.result.structuredContent, { status });
}
+ await assertUserInputFailureLogging();
const start = findTool("start_codex_security_scan");
const startDeepScan = findTool("start_codex_security_deep_scan");
const cancel = findTool("cancel_codex_security_scan");
const cancelFromApp = findTool("cancel_codex_security_scan_from_app");
- const markHandoff = findTool("mark_codex_security_scan_handoff_delivered");
+ findTool("mark_codex_security_scan_handoff_delivered");
const claimHandoff = findTool("claim_codex_security_scan_handoff_delivery");
const releaseHandoff = findTool(
"release_codex_security_scan_handoff_delivery",
@@ -1754,9 +1677,7 @@ try {
const releaseFindingRemediationClaim = findTool(
"release_codex_security_finding_remediation_claim",
);
- const cancelFindingRemediationRequest = findTool(
- "cancel_codex_security_finding_remediation_request",
- );
+ findTool("cancel_codex_security_finding_remediation_request");
const markFindingRemediationDelivered = findTool(
"mark_codex_security_finding_remediation_delivered",
);
@@ -1765,30 +1686,8 @@ try {
);
const exportFindings = findTool("export_codex_security_findings");
const listFindings = findTool("list_codex_security_findings");
- assert.ok(launcher);
- assert.ok(startPromptOnlyScan);
- assert.ok(startHeadlessStandardScan);
- assert.ok(getScan);
- assert.ok(listScans);
- assert.ok(listGlobalFindings);
- assert.ok(listRepositories);
- assert.ok(getScanContext);
- assert.ok(updateScanContext);
- assert.ok(updateScanContextFromApp);
- assert.ok(submit);
- assert.ok(inspectTarget);
- assert.ok(inspectSetup);
- assert.ok(start);
assert.deepEqual(start.inputSchema.required, ["sessionId"]);
- assert.ok(startDeepScan);
assert.equal(startDeepScan.annotations.idempotentHint, true);
- assert.ok(cancel);
- assert.ok(cancelFromApp);
- assert.ok(markHandoff);
- assert.ok(claimHandoff);
- assert.ok(releaseHandoff);
- assert.ok(attachHandoff);
- assert.ok(progress);
assert.equal(
progress.inputSchema.properties.phase.description,
"Current workflow phase. Send it immediately when the scan enters a new phase so persisted progress advances.",
@@ -1813,18 +1712,6 @@ try {
progress.inputSchema.properties.reviewItemsTotal.description,
"Expected reviews or coverage surfaces in the current discovery pass. Increase it before assigning newly discovered work.",
);
- assert.ok(complete);
- assert.ok(fail);
- assert.ok(setFindingTriage);
- assert.ok(requestFindingRemediation);
- assert.ok(requestFindingRemediationAction);
- assert.ok(claimFindingRemediationResend);
- assert.ok(releaseFindingRemediationClaim);
- assert.ok(cancelFindingRemediationRequest);
- assert.ok(markFindingRemediationDelivered);
- assert.ok(setFindingRemediation);
- assert.ok(exportFindings);
- assert.ok(listFindings);
for (const tool of [
listScans,
listGlobalFindings,
@@ -1881,7 +1768,6 @@ try {
assert.deepEqual(listGlobalFindings._meta.ui.visibility, ["app"]);
assert.deepEqual(listGlobalFindings.inputSchema.required ?? [], []);
assert.equal(listGlobalFindings.inputSchema.properties.limit.maximum, 20);
- assert.ok(listGlobalFindings.inputSchema.properties.offset);
assert.equal(listRepositories.annotations.readOnlyHint, true);
assert.deepEqual(listRepositories._meta.ui.visibility, ["app"]);
assert.deepEqual(listRepositories.inputSchema.required ?? [], []);
@@ -1897,10 +1783,8 @@ try {
startHeadlessStandardScan.inputSchema.properties.userContext.maxLength,
undefined,
);
- assert.ok(getScanContext.inputSchema.properties.handoffClaimToken);
assert.deepEqual(updateScanContext._meta.ui.visibility, ["model"]);
assert.deepEqual(updateScanContextFromApp._meta.ui.visibility, ["app"]);
- assert.ok(updateScanContext.inputSchema.properties.handoffClaimToken);
assert.equal(
updateScanContext.inputSchema.properties.userContext.maxLength,
undefined,
@@ -1930,21 +1814,6 @@ try {
assert.deepEqual(complete._meta.ui.visibility, ["model"]);
assert.deepEqual(finalizeUsage._meta.ui.visibility, ["app"]);
assert.deepEqual(fail._meta.ui.visibility, ["model"]);
- for (const tool of [
- launcher,
- getScan,
- submit,
- start,
- cancelFromApp,
- claimHandoff,
- releaseHandoff,
- attachHandoff,
- ]) {
- assert.ok(
- tool._meta.ui.visibility.includes("app"),
- `${tool.name} must be callable by the native Codex Security workbench.`,
- );
- }
for (const tool of [
getScanContext,
updateScanContext,
@@ -2053,7 +1922,6 @@ try {
assert.match(missingPersistedDeepScanText, /complete_codex_security_scan/);
assert.match(missingPersistedDeepScanText, /emit benchmark JSON/);
assert.equal(listFindings.annotations.readOnlyHint, false);
- assert.equal(progress._meta.ui.resourceUri, undefined);
assert.equal(launcher.annotations.idempotentHint, false);
assert.equal(start.annotations.idempotentHint, false);
assert.equal(cancel.annotations.destructiveHint, true);
@@ -2066,20 +1934,6 @@ try {
const urlContext =
"Deployment details came from https://example.test/internal.";
- const urlContextAccepted = await testServer.callTool(1290, {
- name: "open_codex_security_workspace",
- arguments: {
- targetPath: target,
- userContext: urlContext,
- },
- _meta: { "openai/threadId": "fixture-url-context-thread" },
- });
- assertNoError(urlContextAccepted);
- assert.equal(
- urlContextAccepted.result.structuredContent.workspace.userContext,
- urlContext,
- );
-
const opened = await testServer.callTool(4, {
name: "open_codex_security_workspace",
arguments: {
@@ -2087,7 +1941,7 @@ try {
targetTitle: "Fixture Repository",
targetSummary: "Revision fixture123.",
scope: ".",
- userContext: "Focus on uploaded archives.",
+ userContext: urlContext,
},
_meta: { "openai/threadId": "fixture-thread" },
});
@@ -2095,7 +1949,7 @@ try {
const workspace = opened.result.structuredContent.workspace;
assert.equal(workspace.setup.submitted, false);
assert.equal(workspace.mode, "standard");
- assert.equal(workspace.userContext, "Focus on uploaded archives.");
+ assert.equal(workspace.userContext, urlContext);
assert.deepEqual(workspace.targetMetadata, {
hasHead: false,
isGit: false,
@@ -2170,12 +2024,10 @@ try {
},
});
assertNoError(savedOtherWorkspace);
- await new Promise((resolve) => setTimeout(resolve, 100));
testServer.notify("notifications/cancelled", {
requestId: 2021,
reason: "Smoke-test cancellation",
});
- await new Promise((resolve) => setTimeout(resolve, 100));
const otherStarted = await testServer.callTool(2023, {
name: "start_codex_security_scan",
arguments: {
@@ -2563,7 +2415,7 @@ try {
});
assertNoError(deliveredWithoutToken);
- const longUserContext = "Prioritize tenant isolation. ".repeat(120).trim();
+ const longUserContext = `${"Prioritize tenant isolation. ".repeat(120)}Read https://example.test/context.`;
const renamedScan = await testServer.callTool(92020, {
name: "rename_codex_security_scan",
arguments: { scanId, name: " - Release audit " },
@@ -2608,26 +2460,6 @@ try {
appUserContext,
);
- const urlContextUpdate = "Read https://example.test/context.";
- const updatedContextUrl = await testServer.callTool(92012, {
- name: "update_codex_security_scan_context",
- arguments: {
- handoffClaimToken,
- scanId,
- userContext: urlContextUpdate,
- },
- _meta: { "openai/threadId": "fixture-thread" },
- });
- assertNoError(updatedContextUrl);
- assert.equal(
- updatedContextUrl.result.structuredContent.scan.userContext,
- urlContextUpdate,
- );
- assert.equal(
- updatedContextUrl.result.structuredContent.workspace.userContext,
- urlContextUpdate,
- );
-
const readyPreflightProgress = await testServer.callTool(90164, {
name: "update_codex_security_scan_progress",
_meta: {
@@ -2661,7 +2493,7 @@ try {
);
assert.equal(
readyPreflightProgress.result.structuredContent.scan.userContext,
- urlContextUpdate,
+ appUserContext,
);
const nextPhaseUserContext = "Prioritize password-reset token validation.";
diff --git a/plugins/codex-security/mcp-app/tests/test_retry_coverage_dispositions.ts b/plugins/codex-security/mcp-app/tests/test_retry_coverage_dispositions.ts
index 071cd7044..832565d3d 100644
--- a/plugins/codex-security/mcp-app/tests/test_retry_coverage_dispositions.ts
+++ b/plugins/codex-security/mcp-app/tests/test_retry_coverage_dispositions.ts
@@ -1,164 +1,143 @@
import assert from "node:assert/strict";
-import { mkdtemp, readFile, rm } from "node:fs/promises";
-import { tmpdir } from "node:os";
-import path from "node:path";
+import { rm } from "node:fs/promises";
import test from "node:test";
import {
publishCoverageFixture,
readFixtureCoverage,
} from "./deep_scan_coverage_fixture.ts";
+import { finding } from "./scan-draft-fixture.ts";
+import { readJson } from "./support/json.ts";
+import { temporaryDirectory } from "./support/temporary-directories.ts";
-for (const surfaceOnly of [false, true]) {
- for (const resume of [false, true]) {
- for (const dispositions of [["rejected", "not_applicable"], ["reported"]]) {
- for (const resolveOnRetry of [false, true]) {
- test(`${resume ? "resumed" : "live"} ${surfaceOnly ? "surface-only" : "deferred"} stopped recovery preserves ${resolveOnRetry ? "the later disposition" : "newer gaps"} after ${dispositions.join(" and ")} retries`, async () => {
- const root = await mkdtemp(path.join(tmpdir(), "retry-disposition-"));
- try {
- const cases = dispositions.map((disposition) => ({
+for (const resume of [false, true]) {
+ for (const dispositions of [["rejected", "not_applicable"], ["reported"]]) {
+ for (const resolveOnRetry of [false, true]) {
+ test(`${resume ? "resumed" : "live"} stopped recovery preserves ${resolveOnRetry ? "the later disposition" : "newer gaps"} after ${dispositions.join(" and ")} retries`, async (t) => {
+ const root = await temporaryDirectory("retry-disposition-");
+ t.after(() => rm(root, { recursive: true, force: true }));
+ const cases = dispositions.flatMap((disposition) =>
+ ["surface-only", "deferred"].map((kind) => {
+ const candidateId = `${disposition}-${kind}`;
+ return {
+ candidateId,
disposition,
- candidateId: `candidate-${disposition}`,
- label: `Retry boundary: ${disposition}`,
+ label: `Retry boundary: ${candidateId}`,
+ surfaceOnly: kind === "surface-only",
gapReasons: ["First", "Second"].map(
(boundary) =>
- `${boundary} ${disposition} boundary needs proof.`,
- ),
- }));
- const resolved = {
- completeness: "complete",
- surfaces: cases.map(({ candidateId, disposition }) => ({
- id: `disposition-${disposition}`,
- label: `Disposition: ${disposition}`,
- candidateId,
- disposition,
- receiptRefs: [],
- })),
- explicitExclusions: [],
- deferred: [],
- openQuestions: [],
- };
- const pending = {
- completeness: "partial",
- surfaces: cases.flatMap(({ candidateId, disposition, label }) =>
- (surfaceOnly ? ["first", "second"] : ["first"]).map(
- (suffix) => ({
- id: `follow-up-${disposition}-${suffix}`,
- label,
- candidateId,
- disposition: "needs_follow_up",
- receiptRefs: [],
- }),
- ),
+ `${boundary} ${candidateId} boundary needs proof.`,
),
- explicitExclusions: [],
- deferred: surfaceOnly
- ? []
- : cases.flatMap(({ candidateId, disposition, gapReasons }) =>
- gapReasons.map((reason, index) => ({
- id: `retry-gap-${disposition}-${index}`,
- candidateId,
- reason,
- surfaceIds: [`follow-up-${disposition}-first`],
- })),
- ),
- openQuestions: [],
};
- const findings = dispositions.includes("reported")
- ? [
- {
- ruleId: "synthetic.retained-boundary",
- title: "Synthetic boundary finding",
- summary: "A finding retained from an earlier review.",
- severity: {
- level: "high",
- score: 8.1,
- scoringSystem: "CVSS:3.1",
- },
- confidence: {
- level: "high",
- rationale: "Synthetic evidence.",
- },
- taxonomy: { category: "path-traversal", cwe: ["CWE-22"] },
- locations: [
- { path: "source.py", startLine: 1, endLine: 1 },
- ],
- remediation: "Review the synthetic boundary.",
- provenance: {
- source: "local_plugin",
- candidateId: "candidate-reported",
- },
- },
- ]
- : undefined;
- const { scanDir } = await publishCoverageFixture(root, "partial", {
- stopBeforeDraft: true,
- resume,
- otherWorkerCandidateIds: cases.map((row) => row.candidateId),
- retryCoverage: resolveOnRetry
- ? [pending, resolved]
- : [resolved, pending],
- retryFindings:
- findings && (resolveOnRetry ? [[], findings] : [findings, []]),
- });
- const coverage = await readFixtureCoverage(scanDir);
- if (findings) {
- const retained = JSON.parse(
- await readFile(path.join(scanDir, "findings.json"), "utf8"),
- );
- assert.equal(retained.findings.length, 1);
- assert.equal(retained.findings[0].ruleId, findings[0].ruleId);
- }
- for (const {
+ }),
+ );
+ const resolved = {
+ completeness: "complete",
+ surfaces: cases.map(({ candidateId, disposition }) => ({
+ id: `disposition-${candidateId}`,
+ label: `Disposition: ${candidateId}`,
+ candidateId,
+ disposition,
+ receiptRefs: [],
+ })),
+ explicitExclusions: [],
+ deferred: [],
+ openQuestions: [],
+ };
+ const pending = {
+ completeness: "partial",
+ surfaces: cases.flatMap(({ candidateId, label, surfaceOnly }) =>
+ (surfaceOnly ? ["first", "second"] : ["first"]).map((suffix) => ({
+ id: `follow-up-${candidateId}-${suffix}`,
+ label,
candidateId,
- disposition,
+ disposition: "needs_follow_up",
+ receiptRefs: [],
+ })),
+ ),
+ explicitExclusions: [],
+ deferred: cases
+ .filter((row) => !row.surfaceOnly)
+ .flatMap(({ candidateId, gapReasons }) =>
+ gapReasons.map((reason, index) => ({
+ id: `retry-gap-${candidateId}-${index}`,
+ candidateId,
+ reason,
+ surfaceIds: [`follow-up-${candidateId}-first`],
+ })),
+ ),
+ openQuestions: [],
+ };
+ const findings = cases
+ .filter(({ disposition }) => disposition === "reported")
+ .map(({ candidateId }) => ({
+ ...finding(candidateId, "source.py"),
+ provenance: { source: "local_plugin", candidateId },
+ }));
+ const { scanDir } = await publishCoverageFixture(root, "partial", {
+ stopBeforeDraft: true,
+ resume,
+ otherWorkerCandidateIds: cases.map((row) => row.candidateId),
+ retryCoverage: resolveOnRetry
+ ? [pending, resolved]
+ : [resolved, pending],
+ retryFindings: findings.length
+ ? resolveOnRetry
+ ? [[], findings]
+ : [findings, []]
+ : undefined,
+ });
+ const coverage = await readFixtureCoverage(scanDir);
+ const retained = await readJson(scanDir, "findings.json");
+ assert.deepEqual(
+ retained.findings.map((row: { ruleId: string }) => row.ruleId).sort(),
+ findings.map((row) => row.ruleId).sort(),
+ );
+ for (const disposition of dispositions) {
+ const expected = cases.filter(
+ (row) =>
+ row.disposition === disposition &&
+ (resolveOnRetry ||
+ (row.surfaceOnly && disposition !== "reported")),
+ );
+ assert.deepEqual(
+ coverage.surfaces
+ .filter((row) => row.disposition === disposition)
+ .map((row) => row.provenance?.candidateId)
+ .sort(),
+ expected.map((row) => row.candidateId).sort(),
+ );
+ }
+ for (const { candidateId, label, gapReasons, surfaceOnly } of cases) {
+ const retryGaps = coverage.deferred.filter((item) =>
+ gapReasons.includes(item.reason),
+ );
+ assert.deepEqual(
+ retryGaps.map((item) => item.reason),
+ resolveOnRetry || surfaceOnly ? [] : gapReasons,
+ );
+ assert.equal(
+ coverage.surfaces.filter((row) => row.label === label).length,
+ resolveOnRetry ? 0 : surfaceOnly ? 2 : 1,
+ );
+ assert.ok(
+ coverage.deferred.some(
+ (item) =>
+ item.reason === "Verify symbolic links." &&
+ item.provenance?.candidateId === candidateId,
+ ),
+ `another worker's gap for ${candidateId} remains independent`,
+ );
+ for (const gap of retryGaps) {
+ assert.equal(gap.provenance?.candidateId, candidateId);
+ assert.equal(
+ coverage.surfaces.find(
+ (surface) => surface.id === gap.surfaceIds[0],
+ )?.label,
label,
- gapReasons,
- } of cases) {
- const retryGaps = coverage.deferred.filter((item) =>
- gapReasons.includes(item.reason),
- );
- assert.deepEqual(
- retryGaps.map((item) => item.reason),
- resolveOnRetry || surfaceOnly ? [] : gapReasons,
- );
- assert.equal(
- coverage.surfaces.filter((row) => row.label === label).length,
- resolveOnRetry ? 0 : surfaceOnly ? 2 : 1,
- );
- const dispositionRows = coverage.surfaces.filter(
- (item) => item.disposition === disposition,
- );
- assert.equal(
- dispositionRows.length,
- resolveOnRetry || (surfaceOnly && disposition !== "reported")
- ? 1
- : 0,
- );
- for (const row of dispositionRows) {
- assert.equal(row.provenance?.candidateId, candidateId);
- }
- assert.ok(
- coverage.deferred.some(
- (item) =>
- item.reason === "Verify symbolic links." &&
- item.provenance?.candidateId === candidateId,
- ),
- `another worker's gap for ${candidateId} remains independent`,
- );
- for (const gap of retryGaps) {
- assert.equal(
- coverage.surfaces.find(
- (surface) => surface.id === gap.surfaceIds[0],
- )?.label,
- label,
- );
- }
- }
- } finally {
- await rm(root, { recursive: true, force: true });
+ );
}
- });
- }
+ }
+ });
}
}
}
diff --git a/plugins/codex-security/mcp-app/tests/test_threat_model_document.ts b/plugins/codex-security/mcp-app/tests/test_threat_model_document.ts
index 9a31af7b1..a1bae4ee6 100644
--- a/plugins/codex-security/mcp-app/tests/test_threat_model_document.ts
+++ b/plugins/codex-security/mcp-app/tests/test_threat_model_document.ts
@@ -173,7 +173,7 @@ test("terminal Deep model inheritance retries a changed canonical draft", async
}
});
-test("model-only worker checkpoints save Markdown before findings and retain it on retry", async () => {
+test("model-only worker checkpoints save Markdown without Python and retain it on retry", async () => {
const root = await temporaryDirectory("threatmodel-worker-");
try {
const context = {
@@ -183,6 +183,7 @@ test("model-only worker checkpoints save Markdown before findings and retain it
scanId,
pluginRoot,
scope: ".",
+ pythonCommand: join(root, "missing-interpreter"),
};
const checkpoint = {
scanId,
@@ -219,6 +220,10 @@ test("model-only worker checkpoints save Markdown before findings and retain it
checkpoint,
);
assert.equal(result.warnings, undefined);
+ assert.deepEqual(
+ (await readJson(root, "result.json")).threatModel,
+ threatModel,
+ );
const contents = await readFile(join(root, "threatmodel.md"), "utf8");
assert.ok(contents.startsWith(markdown));
assert.match(contents, /Model scope: services\/api/);
@@ -294,35 +299,6 @@ test("a renderer failure preserves saved canonical bytes and reports a warning",
}
});
-test("saved-model rendering does not require the worker's Python interpreter", async () => {
- const root = await temporaryDirectory("threatmodel-python-");
- try {
- const result = await recordCodexSecurityWorkerScanDraft(
- {
- root,
- repoRoot: root,
- layout: "worker",
- scanId,
- pluginRoot,
- pythonCommand: join(root, "missing-interpreter"),
- },
- { scanId, complete: false, threatModel, findings: [], coverage },
- );
- assert.equal(result.warnings, undefined);
- assert.ok(
- (await readFile(join(root, "threatmodel.md"), "utf8")).startsWith(
- markdown,
- ),
- );
- assert.deepEqual(
- (await readJson(root, "result.json")).threatModel,
- threatModel,
- );
- } finally {
- await rm(root, { recursive: true, force: true });
- }
-});
-
test("overlapping draft projections retain the latest committed model", async (t) => {
const root = await temporaryDirectory("threatmodel-concurrent-", true);
const firstStarted = Promise.withResolvers<string>();
diff --git a/plugins/codex-security/mcp-app/tests/test_threat_model_render.ts b/plugins/codex-security/mcp-app/tests/test_threat_model_render.ts
index d002553fb..719dec236 100644
--- a/plugins/codex-security/mcp-app/tests/test_threat_model_render.ts
+++ b/plugins/codex-security/mcp-app/tests/test_threat_model_render.ts
@@ -134,7 +134,7 @@ equivalent(
},
{ target: "R&D &lt;workspace&gt;" },
);
-for (const url of [
+const footerUrls = [
"https://example.com/?first=1&second=2",
"http://example.com/?payload=&#x3C;tag&#x3E;&mode=1",
"ftp://example.com/?first=1&second=2",
@@ -157,11 +157,14 @@ for (const url of [
"{//example.com/?first=1&word;=2}",
"?example.com/?first=1&word;=2",
"mailto:user&tag@localhost",
-]) {
- equivalent(`footer URL ampersands ${url}`, markdownModel, {
- target: `${url} then &amp; and \`https://example.com/?a=1&b=2 &amp;\`.`,
- });
-}
+];
+equivalent("footer URL ampersands", markdownModel, {
+ target: footerUrls
+ .map(
+ (url) => `${url} then &amp; and \`https://example.com/?a=1&b=2 &amp;\`.`,
+ )
+ .join(" "),
+});
for (const [paths, expected] of [
[
[
diff --git a/plugins/codex-security/tests/test_checkpoint_publication_authority.py b/plugins/codex-security/tests/test_checkpoint_publication_authority.py
index 5bfc7e332..1f2f3e0fb 100644
--- a/plugins/codex-security/tests/test_checkpoint_publication_authority.py
+++ b/plugins/codex-security/tests/test_checkpoint_publication_authority.py
@@ -12,7 +12,13 @@ from argparse import Namespace
from pathlib import Path
import pytest
-from test_deep_scan_successful_publication import add_worker, complete
+from test_deep_scan_successful_publication import (
+ add_worker,
+ complete,
+ fail_scan,
+ preserve_scan_results,
+ recover_scan_results,
+)
from test_deep_scan_successful_publication import publication_scan as publication_scan
from test_workbench_standard_deep_results import deep_scan_fixture, worker_paths
from workbench_test_support import (
@@ -63,13 +69,7 @@ def test_flat_retained_source_name_replays(
"UPDATE deep_scan_workers SET result_manifest_path = ? WHERE scan_id = ?",
(str(source), scan.scan_id),
)
- saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
- stopped = saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped scan."),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Stopped scan.")
assert stopped["findingCount"] == 1
findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]
retained = json.loads(
@@ -79,20 +79,9 @@ def test_flat_retained_source_name_replays(
)
assert isinstance(retained[source_name], str)
if replay == "preserve":
- replayed = saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id,
- claim_token=None,
- thread_id=None,
- coordinator_generation=None,
- ),
- )["scan"]
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
else:
- replayed = saved.recover_scan_results(
- context, workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ replayed = recover_scan_results(workbench_api, workbench_db, scan)
assert replayed["findingCount"] == 1
assert json.loads((scan.scan_dir / "findings.json").read_text())["findings"] == findings
@@ -259,11 +248,7 @@ def test_recovery_honors_rejection_committed_before_result_replacement(
os.utime(head, ns=(observed, observed))
saved_bytes = {path: path.read_bytes() for path in (result_path, old_checkpoint, checkpoint)}
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]
assert stopped["findingCount"] == len(findings) == (1 if has_head else 2)
@@ -333,7 +318,6 @@ def test_recovery_needed_compares_checkpoint_observation(
os.utime(head, ns=(100, 100))
directory = result.parent.relative_to(scan.scan_dir).as_posix()
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
capture = saved._capture_saved_source
def capture_legacy_head(scan_dir, relative, scan_id, **kwargs):
@@ -350,16 +334,10 @@ def test_recovery_needed_compares_checkpoint_observation(
if legacy_publication:
patch.setattr("workbench.retained_coverage._capture_saved_source", capture_legacy_head)
patch.setattr(saved, "_capture_saved_source", capture_legacy_head)
- stopped = saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan)
assert stopped["findingCount"] == 1
os.utime(head, ns=(300, 300))
- published = saved.recover_scan_results(
- context, workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ published = recover_scan_results(workbench_api, workbench_db, scan)
assert published["findingCount"] == 0
manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]
@@ -379,9 +357,7 @@ def test_recovery_needed_compares_checkpoint_observation(
assert workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"][
"resultsRecoveryNeeded"
] is (selected_time == 100)
- recovered = saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))[
- "scan"
- ]
+ recovered = recover_scan_results(workbench_api, workbench_db, scan)
assert recovered["findingCount"] == (1 if selected_time == 100 else 0)
assert not recovered["resultsRecoveryNeeded"]
final = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]
@@ -407,11 +383,7 @@ def test_newer_checkpoint_disposition_precedes_older_archived_head(
draft = save_disposition(scan, current, disposition)
(current / "result.json").write_text(json.dumps(draft))
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
assert stopped["findingCount"] == (1 if disposition == "reported" else 0)
@@ -461,13 +433,7 @@ def test_frozen_stopped_replay_ignores_later_worker_head_changes(
with monkeypatch.context() as patch:
patch.setattr(finalize_scan_contract, "write_scan_local_bytes", fail_coverage_write)
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."
- ),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
row = workbench_db.execute("SELECT * FROM scans WHERE id = ?", (scan.scan_id,)).fetchone()
if not root_head:
assert (head.read_bytes(), head.stat().st_mtime_ns) == original_head
@@ -495,13 +461,7 @@ def test_frozen_stopped_replay_ignores_later_worker_head_changes(
else:
head.write_text(json.dumps({"checkpoint": "a" * 64 + ".json"}))
- replayed = workbench_api["saved_results"].preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
assert replayed["findingCount"] == 0
assert json.loads((scan.scan_dir / "findings.json").read_text())["findings"] == []
@@ -532,20 +492,14 @@ def test_explicit_recovery_observes_head_change_between_existing_checkpoints(
previous = save_disposition(scan, result.parent, "reported")
result.write_text(json.dumps(previous))
save_disposition(scan, result.parent, "rejected")
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
assert stopped["findingCount"] == 0
save_disposition(scan, result.parent, "reported")
context = workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"]
assert context["resultsRecoveryNeeded"] is True
- recovered = workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ recovered = recover_scan_results(workbench_api, workbench_db, scan)
assert recovered["findingCount"] == 1
assert recovered["resultsRecoveryNeeded"] is False
@@ -567,26 +521,14 @@ def test_legacy_frozen_publication_keeps_result_fallback_without_saved_heads(
"_write_prepared_scan_finalization",
interrupt_publication,
)
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."
- ),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
with workbench_db:
workbench_db.execute(
"UPDATE scans SET retained_checkpoint_heads_json = NULL WHERE id = ?", (scan.scan_id,)
)
save_disposition(scan, result.parent, "rejected")
- replayed = workbench_api["saved_results"].preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
assert replayed["findingCount"] == 1
@@ -607,16 +549,10 @@ def test_recovery_uses_live_selection_regardless_of_head_timestamp(
save_disposition(scan, result.parent, "rejected")
head = result.parent / "checkpoint-head.json"
observed = head.stat().st_mtime_ns
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
save_disposition(scan, result.parent, "reported")
os.utime(head, ns=(observed + head_time, observed + head_time))
- recovered = workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ recovered = recover_scan_results(workbench_api, workbench_db, scan)
assert recovered["findingCount"] == 1
assert recovered["resultsRecoveryNeeded"] is False
@@ -682,11 +618,7 @@ def test_head_reselection_preserves_equal_or_newer_pending_evidence(
os.utime(pending, ns=(pending_time, pending_time))
sort_checkpoint_entries(monkeypatch, reverse=reverse_order)
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
coverage = json.loads((scan.scan_dir / "coverage.json").read_text())
key, value = next(iter(identity.items()))
@@ -727,11 +659,7 @@ def test_reselection_keeps_the_selected_model_and_newer_result_precedence(
)
sort_checkpoint_entries(monkeypatch, reverse=reverse_order)
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]
expected = "The current result model." if result_time > head_time else "The selected model."
@@ -762,11 +690,7 @@ def test_partial_reselection_keeps_independent_accepted_dispositions(
os.utime(head, ns=(head_time, head_time))
sort_checkpoint_entries(monkeypatch, reverse=reverse_order)
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
assert stopped["findingCount"] == 0
@@ -798,13 +722,7 @@ def test_publication_metadata_uses_the_captured_head(
with monkeypatch.context() as patch:
patch.setattr("workbench.retained_coverage._capture_saved_source", replace_before_capture)
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."
- ),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]
directory = result.parent.relative_to(scan.scan_dir).as_posix()
assert manifest["preservedCheckpointHeads"] == {
@@ -838,25 +756,13 @@ def test_legacy_checkpoint_selection_is_reconstructed_from_frozen_sources(
patch.setattr(saved, "_prepare_scan_finalization", legacy_documents)
else:
patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."
- ),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
with workbench_db:
workbench_db.execute(
"UPDATE scans SET retained_checkpoint_heads_json = NULL WHERE id = ?", (scan.scan_id,)
)
if legacy_state == "failed-publication":
- saved.preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )
+ preserve_scan_results(workbench_api, workbench_db, scan)
context = workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"]
assert context["findingCount"] == 0
assert context["resultsRecoveryNeeded"] is False
@@ -885,11 +791,7 @@ def test_legacy_omitted_root_head_does_not_require_recovery(
with monkeypatch.context() as patch:
patch.setattr(saved, "_prepare_scan_finalization", legacy_documents)
- saved.fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
manifest_path = scan.scan_dir / "scan-manifest.json"
original = manifest_path.read_bytes()
manifest = json.loads(original)["scan"]
@@ -898,13 +800,7 @@ def test_legacy_omitted_root_head_does_not_require_recovery(
assert not workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"][
"resultsRecoveryNeeded"
]
- preserved = saved.preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ preserved = preserve_scan_results(workbench_api, workbench_db, scan)
assert not preserved["resultsRecoveryNeeded"]
assert manifest_path.read_bytes() == original
observed = head.stat().st_mtime_ns + 1_000_000
@@ -934,11 +830,7 @@ def test_explicit_recovery_preserves_database_only_source_times(
with monkeypatch.context() as patch:
patch.setattr("workbench.saved_sources.write_scan_local_bytes", fail_source_order)
- saved.fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
retained = json.loads(
workbench_db.execute(
"SELECT retained_source_digests_json FROM scans WHERE id = ?", (scan.scan_id,)
@@ -952,9 +844,7 @@ def test_explicit_recovery_preserves_database_only_source_times(
else:
result.write_bytes(result.read_bytes())
os.utime(result, ns=(300, 300))
- recovered = saved.recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ recovered = recover_scan_results(workbench_api, workbench_db, scan)
assert recovered["findingCount"] == (0 if change == "new-checkpoint" else 1)
assert not recovered["resultsRecoveryNeeded"]
manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]
@@ -981,11 +871,7 @@ def test_failed_explicit_recovery_replays_its_frozen_selection(
)
result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))
save_disposition(scan, result.parent, "rejected")
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
original = {
name: (scan.scan_dir / name).read_bytes()
for name in ("findings.json", "coverage.json", "scan-manifest.json")
@@ -1013,11 +899,7 @@ def test_failed_explicit_recovery_replays_its_frozen_selection(
with pytest.raises(
(OSError, saved.ContractError), match="Synthetic publication interruption"
):
- workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id),
- )
+ recover_scan_results(workbench_api, workbench_db, scan)
assert all((scan.scan_dir / name).read_bytes() == data for name, data in original.items())
assert (
workbench_db.execute(
@@ -1054,17 +936,9 @@ def test_failed_explicit_recovery_replays_its_frozen_selection(
head.write_bytes(selected_head)
os.utime(head, ns=(selected_time, selected_time))
if retry == "recover":
- recovered = workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ recovered = recover_scan_results(workbench_api, workbench_db, scan)
else:
- recovered = saved.preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ recovered = preserve_scan_results(workbench_api, workbench_db, scan)
assert recovered["findingCount"] == 1
assert recovered["failureMessage"] == "Audit stopped."
retained = json.loads(
@@ -1083,11 +957,7 @@ def test_failed_recovery_keeps_model_when_worker_registers_an_unselected_checkpo
result = add_worker(workbench_db, scan, status="canceled")
original = save_disposition(scan, result.parent, "reported")
result.write_text(json.dumps(original))
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
saved = workbench_api["saved_results"]
head = result.parent / "checkpoint-head.json"
unselected = write_checkpoint(
@@ -1167,11 +1037,7 @@ def test_unreadable_worker_head_does_not_hide_other_recoverable_evidence(
scan.findings[0]["locations"][0]["endLine"] = 21
good = add_worker(workbench_db, scan, status="canceled")
save_disposition(scan, good.parent, "rejected")
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
assert (head.read_bytes(), head.stat().st_mtime_ns) == original_head
directory = bad.parent.relative_to(scan.scan_dir).as_posix()
prior_head = captured_head(scan.scan_dir, directory)
@@ -1190,9 +1056,7 @@ def test_unreadable_worker_head_does_not_hide_other_recoverable_evidence(
workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"]["resultsRecoveryNeeded"]
is True
)
- recovered = workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ recovered = recover_scan_results(workbench_api, workbench_db, scan)
assert recovered["findingCount"] == (2 if prior_disposition == "reported" else 1)
assert recovered["resultsRecoveryNeeded"] is False
manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]
@@ -1218,13 +1082,7 @@ def test_failed_head_snapshot_does_not_claim_uncaptured_authority(
with monkeypatch.context() as patch:
patch.setattr("workbench.saved_sources.write_scan_local_bytes", fail_snapshot)
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."
- ),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
row = workbench_db.execute("SELECT * FROM scans WHERE id = ?", (scan.scan_id,)).fetchone()
assert row["seal_manifest_digest"] is None
heads = json.loads(row["retained_checkpoint_heads_json"])
@@ -1242,13 +1100,7 @@ def test_failed_head_snapshot_does_not_claim_uncaptured_authority(
).fetchone()[0]
)
assert any("Synthetic snapshot write failure" in warning for warning in warnings)
- replayed = saved.preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
assert replayed["findingCount"] == 0
@@ -1266,11 +1118,7 @@ def test_legacy_recovery_can_reselect_an_already_frozen_head(
result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))
head = result.parent / "checkpoint-head.json"
original_head, original_time = head.read_bytes(), head.stat().st_mtime_ns
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
save_disposition(scan, result.parent, "rejected")
saved = workbench_api["saved_results"]
prepare = saved._prepare_scan_finalization
@@ -1281,9 +1129,7 @@ def test_legacy_recovery_can_reselect_an_already_frozen_head(
with monkeypatch.context() as patch:
patch.setattr(saved, "_prepare_scan_finalization", legacy_documents)
- rejected = workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ rejected = recover_scan_results(workbench_api, workbench_db, scan)
assert rejected["findingCount"] == 0
with workbench_db:
workbench_db.execute(
@@ -1295,9 +1141,7 @@ def test_legacy_recovery_can_reselect_an_already_frozen_head(
workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"]["resultsRecoveryNeeded"]
is True
)
- recovered = workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ recovered = recover_scan_results(workbench_api, workbench_db, scan)
assert recovered["findingCount"] == 1
assert recovered["resultsRecoveryNeeded"] is False
@@ -1329,9 +1173,7 @@ def test_recovery_keeps_newer_result_with_legacy_frozen_live_head(
"WHERE id = ?",
(scan.timestamp, json.dumps(sources), scan.scan_id),
)
- recovered = workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ recovered = recover_scan_results(workbench_api, workbench_db, scan)
assert recovered["findingCount"] == 1
assert recovered["resultsRecoveryNeeded"] is False
@@ -1352,25 +1194,12 @@ def test_worker_directory_does_not_collide_with_recovery_metadata(
(str(directory), str(result), scan.scan_id, str(original.parent)),
)
result.write_text(json.dumps(save_disposition(scan, directory, "reported")))
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
assert stopped["findingCount"] == 1
- saved = workbench_api["saved_results"]
- preserved = saved.preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ preserved = preserve_scan_results(workbench_api, workbench_db, scan)
assert preserved["findingCount"] == 1
save_disposition(scan, directory, "rejected")
- recovered = workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ recovered = recover_scan_results(workbench_api, workbench_db, scan)
assert recovered["findingCount"] == 0
assert recovered["resultsRecoveryNeeded"] is False
@@ -1423,13 +1252,7 @@ def test_unreadable_heads_preserve_legacy_accepted_results(
"WHERE id = ?",
(scan.timestamp, json.dumps(sources), scan.scan_id),
)
- replayed = saved.preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
assert replayed["findingCount"] == 1
assert replayed["resultsRecoveryNeeded"] is False
@@ -1449,9 +1272,7 @@ def test_unreadable_heads_preserve_legacy_accepted_results(
scan.findings[0]["locations"][0]["endLine"] = 21
good = add_worker(workbench_db, scan, status="canceled")
save_disposition(scan, good.parent, "reported")
- recovered = workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ recovered = recover_scan_results(workbench_api, workbench_db, scan)
assert recovered["findingCount"] == 2
assert recovered["resultsRecoveryNeeded"] is False
@@ -1467,11 +1288,7 @@ def test_invalid_explicit_recovery_does_not_freeze_unpublishable_selection(
result = add_worker(workbench_db, scan, status="canceled")
original = save_disposition(scan, result.parent, "reported")
result.write_text(json.dumps(original))
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
prior = tuple(
workbench_db.execute(
"SELECT retained_source_digests_json, retained_checkpoint_heads_json FROM scans WHERE id = ?",
@@ -1574,17 +1391,11 @@ def test_model_less_selected_head_keeps_latest_accepted_model(
)
os.utime(result, ns=(150, 150) if result_model else (400, 400))
originals = {path: path.read_bytes() for path in [result, older, newer, checkpoint, head]}
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
expected = "Selected replacement model." if selected_model else modeled[newer]
manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())
assert manifest["scan"]["threatModel"]["summary"] == expected
- workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
- )
+ recover_scan_results(workbench_api, workbench_db, scan)
assert (
json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]["threatModel"][
"summary"
@@ -1617,7 +1428,6 @@ def test_restored_head_keeps_latest_model_fallback(
directory = result.parent
head = directory / "checkpoint-head.json"
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
def select(checkpoint, observed):
head.write_text(json.dumps({"checkpoint": checkpoint.name}))
@@ -1645,16 +1455,10 @@ def test_restored_head_keeps_latest_model_fallback(
os.utime(checkpoint, ns=(50, 50))
older, newer = sorted(models, reverse=newer_sorts_first)
modeled_head = select(older, 200)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Synthetic stop."
- ),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Synthetic stop.")
assert model() == models[older]
select(newer, 300)
- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))
+ recover_scan_results(workbench_api, workbench_db, scan)
assert model() == models[newer]
originals = {
filename: (filename.read_bytes(), filename.stat().st_mtime_ns)
@@ -1664,22 +1468,14 @@ def test_restored_head_keeps_latest_model_fallback(
restored_contents, restored_time = modeled_head if restore_model else unmodeled_head
head.write_bytes(restored_contents)
os.utime(head, ns=(restored_time, restored_time))
- recovered = saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))[
- "scan"
- ]
+ recovered = recover_scan_results(workbench_api, workbench_db, scan)
assert model() == models[older if restore_model else newer]
assert not recovered["resultsRecoveryNeeded"]
published = {
name: (scan.scan_dir / name).read_bytes()
for name in ("scan-manifest.json", "findings.json", "coverage.json", "report.md")
}
- replayed = saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
assert not replayed["resultsRecoveryNeeded"]
assert all(
(scan.scan_dir / name).read_bytes() == contents for name, contents in published.items()
@@ -1729,11 +1525,7 @@ def test_registered_checkpoint_result_keeps_newer_accepted_outcome(
os.utime(older, ns=(200, 200))
head.write_text(json.dumps({"checkpoint": older.name}))
os.utime(head, ns=(200, 200))
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
assert stopped["findingCount"] == 1
assert stopped["resultsRecoveryNeeded"] is False
@@ -1743,13 +1535,7 @@ def test_registered_checkpoint_result_keeps_newer_accepted_outcome(
]
== "Newer accepted model."
)
- replayed = workbench_api["saved_results"].preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
assert replayed["findingCount"] == 1
assert (
json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]["threatModel"][
@@ -1793,7 +1579,6 @@ def test_restored_observation_keeps_independent_registered_result(
os.utime(result, ns=(200, 200))
head = result.parent / "checkpoint-head.json"
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
def select(observed):
head.write_text(json.dumps({"checkpoint": accepted.name}))
@@ -1810,11 +1595,7 @@ def test_restored_observation_keeps_independent_registered_result(
"UPDATE deep_scan_workers SET result_manifest_path = ? WHERE id = ?",
(str(accepted), result.parent.name),
)
- stopped = saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan)
assert stopped["findingCount"] == 1
sources = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"][
"preservedSources"
@@ -1840,17 +1621,11 @@ def test_restored_observation_keeps_independent_registered_result(
patch.setattr(saved, "_write_prepared_scan_finalization", publish)
if interrupted:
with pytest.raises(OSError, match="Synthetic publication interruption"):
- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))
+ recover_scan_results(workbench_api, workbench_db, scan)
else:
- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))
+ recover_scan_results(workbench_api, workbench_db, scan)
assert prepared_counts == [int(registered)]
- recovered = saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ recovered = preserve_scan_results(workbench_api, workbench_db, scan)
assert recovered["findingCount"] == int(registered)
assert not recovered["resultsRecoveryNeeded"]
manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]
@@ -1903,7 +1678,6 @@ def test_inherited_model_uses_same_selection_on_initial_and_frozen_publication(
os.utime(selected, ns=(50, 50))
head = scan.scan_dir / "checkpoint-head.json"
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
if model_choice in {"canonical-fallback", "older-accepted"}:
unselected = write_checkpoint(
@@ -1953,23 +1727,13 @@ def test_inherited_model_uses_same_selection_on_initial_and_frozen_publication(
"_encode_retained_sources",
lambda sources, model_source, source_times=None: encode(sources, [], source_times),
)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
retained = workbench_db.execute(
"SELECT retained_source_digests_json FROM scans WHERE id = ?", (scan.scan_id,)
).fetchone()[0]
if legacy_format:
- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))
- preserved = saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ recover_scan_results(workbench_api, workbench_db, scan)
+ preserved = preserve_scan_results(workbench_api, workbench_db, scan)
expected = {
"latest-accepted": "Latest accepted model.",
"selected-head": "Selected root model.",
@@ -2087,7 +1851,6 @@ def test_recovery_keeps_known_model_source_with_equal_model_duplicates(
for name in ("scan-manifest.json", "findings.json", "coverage.json"):
os.utime(scan.scan_dir / name, ns=(150, 150))
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
_, canonical = saved._read_saved_parent_result(scan.scan_dir, scan.scan_id)
older = write_checkpoint(
scan.scan_dir / "checkpoints", {**canonical, "threatModel": {"summary": "Older model."}}
@@ -2118,11 +1881,7 @@ def test_recovery_keeps_known_model_source_with_equal_model_duplicates(
selected = write_checkpoint(scan.scan_dir / "checkpoints", empty)
os.utime(selected, ns=(50, 50))
select(selected, 200)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
retained = json.loads(
workbench_db.execute(
"SELECT retained_source_digests_json FROM scans WHERE id = ?", (scan.scan_id,)
@@ -2160,18 +1919,12 @@ def test_recovery_keeps_known_model_source_with_equal_model_duplicates(
patch.setattr(saved, "_write_prepared_scan_finalization", publish)
if interrupted:
with pytest.raises(OSError, match="Synthetic publication interruption"):
- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))
+ recover_scan_results(workbench_api, workbench_db, scan)
else:
- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))
+ recover_scan_results(workbench_api, workbench_db, scan)
expected = "New model." if new_model else "Canonical model."
assert prepared_models == [expected]
- saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )
+ preserve_scan_results(workbench_api, workbench_db, scan)
assert json.loads(manifest_path.read_text())["scan"]["threatModel"]["summary"] == expected
@@ -2188,7 +1941,6 @@ def test_skipped_canonical_checkpoint_keeps_valid_model_and_findings(
for name in ("scan-manifest.json", "findings.json", "coverage.json"):
os.utime(scan.scan_dir / name, ns=(150, 150))
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
_, canonical = saved._read_saved_parent_result(scan.scan_dir, scan.scan_id)
checkpoint_dir = scan.scan_dir / "checkpoints"
checkpoint_dir.mkdir()
@@ -2212,24 +1964,10 @@ def test_skipped_canonical_checkpoint_keeps_valid_model_and_findings(
with monkeypatch.context() as patch:
if flow == "interrupted-stop":
patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."
- ),
- )
- published = saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ fail_scan(workbench_api, workbench_db, scan)
+ published = preserve_scan_results(workbench_api, workbench_db, scan)
os.utime(head, ns=(300, 300))
- published = saved.recover_scan_results(
- context, workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ published = recover_scan_results(workbench_api, workbench_db, scan)
assert len(json.loads((scan.scan_dir / "findings.json").read_text())["findings"]) == 1
if flow != "prepare":
assert published["findingCount"] == 1
@@ -2264,7 +2002,6 @@ def test_selected_terminal_coverage_excludes_unselected_history(
(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))
result = add_worker(workbench_db, scan, status="canceled")
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
checkpoints = {}
for name in ("A", "B"):
draft = saved_draft(
@@ -2303,11 +2040,7 @@ def test_selected_terminal_coverage_excludes_unselected_history(
"UPDATE deep_scan_workers SET result_manifest_path = ? WHERE id = ?",
(str(checkpoints[registered]), result.parent.name),
)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
select("A", restored_time)
expected = "B" if registered == "B" and restored_time == 100 else "A"
@@ -2316,16 +2049,10 @@ def test_selected_terminal_coverage_excludes_unselected_history(
patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)
if interrupted and restored_time == 100:
with pytest.raises(OSError, match="Synthetic publication interruption"):
- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))
+ recover_scan_results(workbench_api, workbench_db, scan)
else:
- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))
- replayed = saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ recover_scan_results(workbench_api, workbench_db, scan)
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
coverage = json.loads((scan.scan_dir / "coverage.json").read_text())
assert [row["label"] for row in coverage["surfaces"]] == [f"{expected} surface"]
assert [row["question"] for row in coverage["openQuestions"]] == [f"{expected} question"]
@@ -2364,7 +2091,6 @@ def test_selected_model_identity_survives_restore_then_modelless_head(
else add_worker(workbench_db, scan, status="canceled").parent
)
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
draft = saved_draft(scan.scan_id, complete=True)
draft["coverage"] = {**scan.coverage, **draft["coverage"]}
checkpoints = {
@@ -2390,32 +2116,16 @@ def test_selected_model_identity_survives_restore_then_modelless_head(
head.write_text(json.dumps({"checkpoint": checkpoints[name].name}))
os.utime(head, ns=(observed, observed))
if index == 0:
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."
- ),
- )
+ fail_scan(workbench_api, workbench_db, scan)
else:
with monkeypatch.context() as patch:
if phase == failure_at:
patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)
with pytest.raises(OSError, match="Synthetic publication interruption"):
- saved.recover_scan_results(
- context, workbench_db, Namespace(scan_id=scan.scan_id)
- )
+ recover_scan_results(workbench_api, workbench_db, scan)
else:
- saved.recover_scan_results(
- context, workbench_db, Namespace(scan_id=scan.scan_id)
- )
- replayed = saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ recover_scan_results(workbench_api, workbench_db, scan)
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
models.append(
json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]["threatModel"][
"summary"
@@ -2454,7 +2164,6 @@ def test_selected_terminal_coverage_has_root_worker_parity(
else add_worker(workbench_db, scan, status="canceled").parent
)
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
checkpoints = {}
for name in ("A", "B"):
draft = saved_draft(scan.scan_id, complete=terminal if name == "A" else True)
@@ -2481,11 +2190,7 @@ def test_selected_terminal_coverage_has_root_worker_parity(
saved._capture_saved_source(
scan.scan_dir, head.relative_to(scan.scan_dir).as_posix(), scan.scan_id
)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
head.write_text(json.dumps({"checkpoint": checkpoints["A"].name}))
os.utime(head, ns=(100, 100))
@@ -2493,16 +2198,10 @@ def test_selected_terminal_coverage_has_root_worker_parity(
if interrupted:
patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)
with pytest.raises(OSError, match="Synthetic publication interruption"):
- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))
+ recover_scan_results(workbench_api, workbench_db, scan)
else:
- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))
- replayed = saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ recover_scan_results(workbench_api, workbench_db, scan)
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
coverage = json.loads((scan.scan_dir / "coverage.json").read_text())
expected = {"A"} if terminal else {"A", "B"}
assert {row["label"] for row in coverage["surfaces"]} == expected
@@ -2575,12 +2274,7 @@ def test_terminal_coverage_replacement_does_not_cross_into_a_partial_attempt(
(str(accepted_path), directory.name),
)
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
current = write_checkpoint(
directory / "checkpoints", saved_draft(scan.scan_id, complete=current_terminal)
)
@@ -2592,16 +2286,10 @@ def test_terminal_coverage_replacement_does_not_cross_into_a_partial_attempt(
if interrupted:
patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)
with pytest.raises(OSError, match="Synthetic publication interruption"):
- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))
+ recover_scan_results(workbench_api, workbench_db, scan)
else:
- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))
- replayed = saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ recover_scan_results(workbench_api, workbench_db, scan)
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
coverage = json.loads((scan.scan_dir / "coverage.json").read_text())
expected = [] if current_terminal else ["Accepted"]
assert [row["label"] for row in coverage["surfaces"]] == expected
@@ -2634,11 +2322,7 @@ def test_tied_rejection_requires_valid_surface_before_removing_finding(
)
os.utime(result.parent / "checkpoint-head.json", ns=(300, 300))
original = result.read_bytes()
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
assert stopped["findingCount"] == (0 if valid_rejection else 1)
assert result.read_bytes() == original
@@ -2702,24 +2386,14 @@ def test_rejection_receipt_is_verified_before_suppressing_accepted_finding(
originals = {
path: path.read_bytes() for path in (result, result.parent / "checkpoint-head.json")
}
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
unverified = receipt_state in {"missing", "shared-collision"}
assert stopped["findingCount"] == int(unverified)
coverage = json.loads((scan.scan_dir / "coverage.json").read_text())
assert ("needs_follow_up" if unverified else "rejected") in {
surface["disposition"] for surface in coverage["surfaces"]
}
- replayed = workbench_api["saved_results"].preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
assert replayed["findingCount"] == stopped["findingCount"]
assert all(path.read_bytes() == value for path, value in originals.items())
@@ -2743,11 +2417,7 @@ def test_checkpoint_registered_as_result_keeps_its_own_finding(
(str(checkpoint), result.parent.name),
)
original = checkpoint.read_bytes()
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
assert stopped["findingCount"] == 1
assert checkpoint.read_bytes() == original
@@ -2790,16 +2460,7 @@ def test_cancellation_snapshot_failure_keeps_the_original_source_boundary(
saved = workbench_api["saved_results"]
def replay():
- return saved.preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id,
- claim_token=None,
- thread_id=None,
- coordinator_generation=None,
- ),
- )["scan"]
+ return preserve_scan_results(workbench_api, workbench_db, scan)
if not writable and replace_head:
with pytest.raises(
@@ -2844,13 +2505,7 @@ def test_snapshot_write_failure_keeps_stopped_publication_pending(
snapshots.chmod(0o500)
if os.access(snapshots, os.W_OK):
pytest.skip("Current user can write through POSIX directory permissions")
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Audit stopped."
- ),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")
row = workbench_db.execute("SELECT * FROM scans WHERE id = ?", (scan.scan_id,)).fetchone()
assert row["status"] == "failed"
if not writable:
@@ -2880,13 +2535,7 @@ def test_snapshot_write_failure_keeps_stopped_publication_pending(
assert row["seal_manifest_digest"]
finally:
snapshots.chmod(0o700)
- replayed = workbench_api["saved_results"].preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
assert replayed["findingCount"] == (1 if disposition == "reported" else 0)
assert all(path.read_bytes() == value for path, value in originals.items())
@@ -2945,13 +2594,7 @@ def test_cancellation_captures_each_remaining_workers_current_head(
if advance_after_cancel:
scan.findings[0]["identity"]["anchor"] = "second-selected-after-cancel"
save_disposition(scan, second.parent, "reported")
- retained = saved.preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ retained = preserve_scan_results(workbench_api, workbench_db, scan)
assert retained["findingCount"] >= 2
anchors = {
finding["identity"]["anchor"]
@@ -2989,13 +2632,7 @@ def test_cancellation_freezes_sources_before_report_preparation(
report.rmdir()
scan.findings[0]["identity"]["anchor"] = "published-after-cancellation"
save_disposition(scan, result.parent, "reported")
- retained = saved.preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ retained = preserve_scan_results(workbench_api, workbench_db, scan)
assert retained["findingCount"] == 1
assert all(
finding["identity"]["anchor"] != "published-after-cancellation"
@@ -3029,11 +2666,7 @@ def test_legacy_unreadable_head_does_not_hide_another_workers_new_evidence(
with monkeypatch.context() as patch:
patch.setattr(saved, "_prepare_scan_finalization", legacy_publication)
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
with workbench_db:
workbench_db.execute(
"UPDATE scans SET retained_checkpoint_heads_json = NULL WHERE id = ?", (scan.scan_id,)
@@ -3074,20 +2707,10 @@ def test_first_stop_keeps_registered_result_when_head_is_unreadable(
"{" if head_state == "malformed" else json.dumps({"checkpoint": "a" * 64 + ".json"})
)
original = result.read_bytes()
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan)
assert stopped["findingCount"] == 1
assert not stopped["resultsRecoveryNeeded"]
- replayed = workbench_api["saved_results"].preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
assert replayed["findingCount"] == 1
assert result.read_bytes() == original
@@ -3109,11 +2732,7 @@ def test_legacy_ordering_failure_does_not_hide_new_worker_evidence(
with monkeypatch.context() as patch:
patch.setattr(saved, "_prepare_scan_finalization", legacy_publication)
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
with workbench_db:
workbench_db.execute(
"UPDATE scans SET retained_checkpoint_heads_json = NULL WHERE id = ?", (scan.scan_id,)
@@ -3141,20 +2760,9 @@ def test_legacy_ordering_failure_does_not_hide_new_worker_evidence(
)
if ordering != "valid":
with pytest.raises(saved.ContractError):
- workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id),
- )
+ recover_scan_results(workbench_api, workbench_db, scan)
else:
- assert (
- workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id),
- )["scan"]["findingCount"]
- == 2
- )
+ assert recover_scan_results(workbench_api, workbench_db, scan)["findingCount"] == 2
@pytest.mark.parametrize("canceled", [False, True], ids=["failed", "canceled"])
@@ -3165,11 +2773,7 @@ def test_legacy_retained_heads_survive_loss_after_terminal_output(
(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))
result = add_worker(workbench_db, scan, status="canceled")
result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
with workbench_db:
workbench_db.execute(
"UPDATE scans SET retained_checkpoint_heads_json = NULL, seal_manifest_digest = NULL, "
@@ -3216,13 +2820,7 @@ raise AssertionError("terminal write was not reached")
with sqlite3.connect(database) as connection:
connection.row_factory = sqlite3.Row
connection.execute("PRAGMA foreign_keys = ON")
- replayed = workbench_api["saved_results"].preserve_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- connection,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ replayed = preserve_scan_results(workbench_api, connection, scan)
assert replayed["findingCount"] == 1
assert not replayed["resultsRecoveryNeeded"]
assert (
diff --git a/plugins/codex-security/tests/test_checkpoint_recovery_fallbacks.py b/plugins/codex-security/tests/test_checkpoint_recovery_fallbacks.py
index 27cb7a998..6545ba431 100644
--- a/plugins/codex-security/tests/test_checkpoint_recovery_fallbacks.py
+++ b/plugins/codex-security/tests/test_checkpoint_recovery_fallbacks.py
@@ -7,7 +7,12 @@ from argparse import Namespace
from pathlib import Path
import pytest
-from test_deep_scan_successful_publication import add_worker
+from test_deep_scan_successful_publication import (
+ add_worker,
+ fail_scan,
+ preserve_scan_results,
+ recover_scan_results,
+)
from test_deep_scan_successful_publication import publication_scan as publication_scan
from workbench_test_support import interrupt_publication, saved_draft, write_checkpoint
@@ -45,11 +50,7 @@ def test_published_raw_head_compatibility_converts_before_later_selection(
with monkeypatch.context() as patch:
patch.setattr("workbench.retained_coverage._capture_saved_source", legacy_capture)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
original = (scan.scan_dir / "scan-manifest.json").read_bytes()
assert (
head.relative_to(scan.scan_dir).as_posix()
@@ -74,9 +75,7 @@ def test_published_raw_head_compatibility_converts_before_later_selection(
later = write_checkpoint(directory / "checkpoints", {**initial, "findings": scan.findings})
head.write_text(json.dumps({"checkpoint": later.name}))
os.utime(head, ns=(200, 200))
- recovered = saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))[
- "scan"
- ]
+ recovered = recover_scan_results(workbench_api, workbench_db, scan)
assert recovered["findingCount"] == 1
assert not recovered["resultsRecoveryNeeded"]
@@ -132,11 +131,7 @@ def test_early_head_error_keeps_captured_records_during_sibling_capture(
with monkeypatch.context() as patch:
patch.setattr("workbench.saved_sources.write_scan_local_bytes", capture_sibling)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
assert changed
args = Namespace(
scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
@@ -154,7 +149,7 @@ def test_early_head_error_keeps_captured_records_during_sibling_capture(
assert {row["identity"]["anchor"] for row in findings["findings"]} == {"before", "second"}
head.write_bytes(later)
os.utime(head, ns=(200, 200))
- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))
+ recover_scan_results(workbench_api, workbench_db, scan)
_, findings, _ = saved.finalize_scan(scan.scan_dir)
assert "after" in {row["identity"]["anchor"] for row in findings["findings"]}
@@ -165,7 +160,6 @@ def test_frozen_model_capture_keeps_earlier_worker_finding(
):
scan = publication_scan()
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
model = {"summary": "Retained scan model."}
manifest_path = scan.scan_dir / "scan-manifest.json"
manifest = json.loads(manifest_path.read_text())
@@ -202,11 +196,7 @@ def test_frozen_model_capture_keeps_earlier_worker_finding(
patch.setattr("workbench.retained_coverage.write_scan_local_bytes", interrupt_capture)
else:
patch.setattr(saved, "_write_prepared_scan_finalization", interrupt)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
if not restore_capture:
assert len(initial) == len(initial[0]) == 1
retained = json.loads(
@@ -217,13 +207,7 @@ def test_frozen_model_capture_keeps_earlier_worker_finding(
capture = scan.scan_dir / retained["threatModelSource"]
assert retained["threatModelSource"] == model_path
assert capture.exists() is not restore_capture
- saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )
+ preserve_scan_results(workbench_api, workbench_db, scan)
final_manifest, findings, _ = saved.finalize_scan(scan.scan_dir)
assert final_manifest["scan"]["threatModel"] == model
assert len(findings["findings"]) == 1
@@ -284,13 +268,7 @@ def test_frozen_model_ignores_unselected_live_manifest_changes(
if late_model:
manifest["scan"]["threatModel"] = {"summary": "Late unselected model"}
manifest_path.write_text(json.dumps(manifest))
- replayed = saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
published, findings, _ = saved.finalize_scan(scan.scan_dir)
assert published["scan"]["threatModel"] == model
assert len(findings["findings"]) == replayed["findingCount"] == 1
@@ -345,11 +323,7 @@ def test_resumed_head_snapshot_allows_later_recovery(
with monkeypatch.context() as patch:
if snapshot_failure:
patch.setattr("workbench.saved_sources.write_scan_local_bytes", fail_head)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
args = Namespace(
scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
)
@@ -377,9 +351,7 @@ def test_resumed_head_snapshot_allows_later_recovery(
os.utime(later, ns=(50, 50))
head.write_text(json.dumps({"checkpoint": later.name}))
os.utime(head, ns=(200, 200))
- result = saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))[
- "scan"
- ]
+ result = recover_scan_results(workbench_api, workbench_db, scan)
assert result["findingCount"] == 1
assert not result["resultsRecoveryNeeded"]
published, findings, _ = saved.finalize_scan(scan.scan_dir)
@@ -411,7 +383,6 @@ def test_early_head_failure_keeps_frozen_model_choice(
):
scan = publication_scan(mode=mode)
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
manifest_path = scan.scan_dir / "scan-manifest.json"
manifest = json.loads(manifest_path.read_text())
manifest["scan"]["threatModel"] = {"summary": "Canonical model"}
@@ -458,11 +429,7 @@ def test_early_head_failure_keeps_frozen_model_choice(
with monkeypatch.context() as patch:
patch.setattr("workbench.saved_sources.write_scan_local_bytes", fail_snapshot)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
if failure != "none":
if live_change == "coverage":
coverage_path = scan.scan_dir / "coverage.json"
@@ -472,13 +439,7 @@ def test_early_head_failure_keeps_frozen_model_choice(
else:
manifest["scan"]["threatModel"] = {"summary": "Late unselected model"}
manifest_path.write_text(json.dumps(manifest))
- result = saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ result = preserve_scan_results(workbench_api, workbench_db, scan)
published, findings, _ = saved.finalize_scan(scan.scan_dir)
expected = {
"canonical": "Canonical model",
@@ -532,11 +493,7 @@ def test_failed_head_fallback_rejects_changes_before_resume(
with monkeypatch.context() as patch:
patch.setattr("workbench.saved_sources.write_scan_local_bytes", fail_head)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
if change == "head":
later = write_checkpoint(directory / "checkpoints", {**initial, "findings": scan.findings})
head.write_text(json.dumps({"checkpoint": later.name}))
@@ -611,11 +568,7 @@ def test_rejected_historical_model_does_not_poison_recovery(
select({**draft, "threatModel": {"summary": "Older accepted model"}}, 100)
saved._capture_saved_source(scan.scan_dir, "checkpoint-head.json", scan.scan_id)
invalid_source = select({**draft, "threatModel": invalid}, 200)
- stopped = saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan)
assert stopped["findingCount"] == 1
assert any("Skipped malformed saved threat model" in warning for warning in stopped["warnings"])
assert json.loads(manifest_path.read_text())["scan"]["threatModel"] == model
@@ -639,13 +592,7 @@ def test_rejected_historical_model_does_not_poison_recovery(
saved.recover_scan_results(context, workbench_db, args)
else:
saved.recover_scan_results(context, workbench_db, args)
- replayed = saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
published, findings, _ = saved.finalize_scan(scan.scan_dir)
assert published["scan"]["threatModel"] == (
{"summary": "Replacement model"} if replacement == "valid" else model
@@ -703,7 +650,6 @@ def test_partial_head_retains_previous_terminal_coverage_boundary(
else add_worker(workbench_db, scan, status="canceled").parent
)
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
for label, observed in [("Obsolete", 100), ("Replacement", 200), ("Partial", partial_time)]:
draft = saved_draft(scan.scan_id, complete=label != "Partial")
draft["coverage"] = {**coverage, **draft["coverage"]}
@@ -732,15 +678,9 @@ def test_partial_head_retains_previous_terminal_coverage_boundary(
(str(checkpoint), directory.name),
)
if label == "Obsolete":
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."
- ),
- )
+ fail_scan(workbench_api, workbench_db, scan)
elif label == "Replacement":
- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))
+ recover_scan_results(workbench_api, workbench_db, scan)
if repeated_terminal:
os.utime(head, ns=(400, 400))
saved._capture_saved_source(
@@ -751,20 +691,10 @@ def test_partial_head_retains_previous_terminal_coverage_boundary(
if interrupted:
patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)
with pytest.raises(OSError, match="Synthetic publication interruption"):
- saved.recover_scan_results(
- context, workbench_db, Namespace(scan_id=scan.scan_id)
- )
+ recover_scan_results(workbench_api, workbench_db, scan)
else:
- saved.recover_scan_results(
- context, workbench_db, Namespace(scan_id=scan.scan_id)
- )
- replayed = saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ recover_scan_results(workbench_api, workbench_db, scan)
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
_, _, coverage = saved.finalize_scan(scan.scan_dir)
expected = {"Replacement"} if partial_time == 300 or registered else {"Obsolete", "Replacement"}
assert {row["label"] for row in coverage["surfaces"]} == expected
@@ -781,7 +711,6 @@ def test_retry_keeps_canonical_finding_when_its_checkpoint_copy_is_unreadable(
):
scan = publication_scan(mode="standard")
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
manifest_path = scan.scan_dir / "scan-manifest.json"
manifest = json.loads(manifest_path.read_text())
model = {"summary": "Canonical model"}
@@ -808,22 +737,12 @@ def test_retry_keeps_canonical_finding_when_its_checkpoint_copy_is_unreadable(
with monkeypatch.context() as patch:
patch.setattr(saved, "_write_prepared_scan_finalization", interrupt)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
assert attempted == [(scan.findings[0]["title"], model)]
with pytest.raises(
saved.ContractError, match="Frozen stopped-scan checkpoint set is incomplete"
):
- saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )
+ preserve_scan_results(workbench_api, workbench_db, scan)
assert broken.read_bytes() == b"{"
assert "sealedAt" not in json.loads(manifest_path.read_text())["scan"]
@@ -878,11 +797,7 @@ def test_inherited_model_capture_is_bound_before_publication_retry(
patch.setattr("workbench.retained_coverage.write_scan_local_bytes", fail_capture)
else:
patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
row = workbench_db.execute(
"SELECT retained_source_digests_json, completion_warnings_json FROM scans WHERE id = ?",
(scan.scan_id,),
@@ -946,7 +861,6 @@ def test_restored_terminal_keeps_surfaces_for_surviving_deferred_work(
else add_worker(workbench_db, scan, status="canceled").parent
)
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
terminal = saved_draft(scan.scan_id, complete=True)
terminal["coverage"] = {**coverage, **terminal["coverage"]}
task = {"id": "review-task", "reason": "Review remains pending."}
@@ -973,32 +887,16 @@ def test_restored_terminal_keeps_surfaces_for_surviving_deferred_work(
head.write_text(json.dumps({"checkpoint": checkpoint.name}))
os.utime(head, ns=(observed, observed))
if index == 0:
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."
- ),
- )
+ fail_scan(workbench_api, workbench_db, scan)
else:
with monkeypatch.context() as patch:
if interrupted and index == len(sequence) - 1:
patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)
with pytest.raises(OSError, match="Synthetic publication interruption"):
- saved.recover_scan_results(
- context, workbench_db, Namespace(scan_id=scan.scan_id)
- )
+ recover_scan_results(workbench_api, workbench_db, scan)
else:
- saved.recover_scan_results(
- context, workbench_db, Namespace(scan_id=scan.scan_id)
- )
- replayed = saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )["scan"]
+ recover_scan_results(workbench_api, workbench_db, scan)
+ replayed = preserve_scan_results(workbench_api, workbench_db, scan)
_, _, coverage = saved.finalize_scan(scan.scan_dir)
if owner == "worker":
surface = {**surface, "id": f"{directory.name}/pending-surface", "workerId": directory.name}
@@ -1018,7 +916,6 @@ def test_source_order_failure_retains_accepted_parent_input(
scan = publication_scan()
saved = workbench_api["saved_results"]
- context = workbench_api["_WORKBENCH_DB_CONTEXT"]
scan_dir = scan.scan_dir
manifest = scan_dir / "scan-manifest.json"
contents = json.loads(manifest.read_text())
@@ -1060,21 +957,11 @@ def test_source_order_failure_retains_accepted_parent_input(
with monkeypatch.context() as patch:
patch.setattr(saved_sources, "write_scan_local_bytes", fail_order)
- saved.fail_scan(
- context,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
assert "sealedAt" not in json.loads(manifest.read_text())["scan"]
head.write_text(json.dumps({"checkpoint": abandoned.name, "inputCheckpoint": abandoned.name}))
os.utime(head, ns=(400, 400))
- saved.preserve_scan_results(
- context,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )
+ preserve_scan_results(workbench_api, workbench_db, scan)
_, findings, _ = saved.finalize_scan(scan_dir)
assert [finding["title"] for finding in findings["findings"]] == [scan.findings[0]["title"]]
@@ -1086,7 +973,6 @@ def test_headless_canonical_snapshot_failure(
scan = publication_scan()
saved = workbench_api["saved_results"]
- db = workbench_api["_WORKBENCH_DB_CONTEXT"]
write = saved_sources.write_scan_local_bytes
original = scan.findings[0]["title"]
head = scan.scan_dir / "checkpoint-head.json"
@@ -1099,11 +985,7 @@ def test_headless_canonical_snapshot_failure(
with monkeypatch.context() as patch:
patch.setattr(saved_sources, "write_scan_local_bytes", fail_snapshot)
- saved.fail_scan(
- db,
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ fail_scan(workbench_api, workbench_db, scan)
assert not head.exists()
assert "sealedAt" not in json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]
raw = write_checkpoint(
@@ -1115,13 +997,7 @@ def test_headless_canonical_snapshot_failure(
)
head.write_text(json.dumps({"checkpoint": raw.name, "inputCheckpoint": raw.name}))
(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))
- saved.preserve_scan_results(
- db,
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
- ),
- )
+ preserve_scan_results(workbench_api, workbench_db, scan)
manifest, findings, _ = saved.finalize_scan(scan.scan_dir)
assert [f["title"] for f in findings["findings"]] == [original]
assert "." not in manifest["scan"]["preservedCheckpointHeads"]
diff --git a/plugins/codex-security/tests/test_cli_dry_runs.py b/plugins/codex-security/tests/test_cli_dry_runs.py
index 4cd4230f2..e5d63a71a 100644
--- a/plugins/codex-security/tests/test_cli_dry_runs.py
+++ b/plugins/codex-security/tests/test_cli_dry_runs.py
@@ -11,9 +11,9 @@ PLUGIN_ROOT = Path(__file__).resolve().parents[1]
SCRIPT_DIR = PLUGIN_ROOT / "scripts"
-def run_script(name: str, *args: str, isolated: bool = False) -> subprocess.CompletedProcess[str]:
+def run_script(name: str, *args: str) -> subprocess.CompletedProcess[str]:
return subprocess.run(
- [sys.executable, *(["-I"] if isolated else []), str(SCRIPT_DIR / name), *args],
+ [sys.executable, "-I", str(SCRIPT_DIR / name), *args],
check=False,
capture_output=True,
text=True,
@@ -30,9 +30,10 @@ def run_script(name: str, *args: str, isolated: bool = False) -> subprocess.Comp
"validate_scan_contract.py",
"validate_tracking_source.py",
"workbench_db.py",
+ "workbench_publication.py",
),
)
-def test_commands_support_help(name: str) -> None:
+def test_commands_support_help_with_safe_path(name: str) -> None:
result = run_script(name, "--help")
assert result.returncode == 0, result.stderr
assert "usage:" in result.stdout.lower()
@@ -52,23 +53,6 @@ def test_script_modules_import_in_a_fresh_interpreter() -> None:
)
- "name",
- (
- "workbench_publication.py",
- "finalize_scan_contract.py",
- "validate_scan_contract.py",
- "validate_tracking_source.py",
- "threat_model_projection.py",
- ),
-)
-def test_helpers_support_help_with_safe_path(name: str) -> None:
- result = run_script(name, "--help", isolated=True)
-
- assert result.returncode == 0, result.stderr
- assert "usage:" in result.stdout.lower()
-
-
def test_recover_scan_results_help_describes_its_contract() -> None:
result = run_script("workbench_db.py", "recover-scan-results", "--help")
@@ -77,8 +61,7 @@ def test_recover_scan_results_help_describes_its_contract() -> None:
assert "retained checkpoints" in result.stdout
[email protected]("isolated", (False, True))
-def test_finalizer_cli_completes_checked_in_scan_bundle(tmp_path: Path, isolated: bool) -> None:
+def test_finalizer_cli_completes_checked_in_scan_bundle(tmp_path: Path) -> None:
scan_dir = tmp_path / "completed-scan"
shutil.copytree(PLUGIN_ROOT / "examples" / "completed-scan", scan_dir)
@@ -88,7 +71,6 @@ def test_finalizer_cli_completes_checked_in_scan_bundle(tmp_path: Path, isolated
str(scan_dir),
"--schema-dir",
str(PLUGIN_ROOT / "schemas"),
- isolated=isolated,
)
assert result.returncode == 0, result.stderr
diff --git a/plugins/codex-security/tests/test_deep_scan_deadline_contracts.py b/plugins/codex-security/tests/test_deep_scan_deadline_contracts.py
index 789fd62a5..ea01a9840 100644
--- a/plugins/codex-security/tests/test_deep_scan_deadline_contracts.py
+++ b/plugins/codex-security/tests/test_deep_scan_deadline_contracts.py
@@ -102,8 +102,15 @@ def test_deadline_requires_boolean_completion_before_superseding_worker_history(
)
[email protected]("source", ["result", "checkpoint", "archived-result"])
[email protected]("invalid", ["rationale", "code_evidence", "sourceFindingIds"])
+# Each source feeds the same per-finding validator. Exercise each rejection in isolation.
+ ("source", "invalid"),
+ [
+ ("result", "rationale"),
+ ("checkpoint", "code_evidence"),
+ ("archived-result", "sourceFindingIds"),
+ ],
+)
def test_deadline_validates_each_recovered_finding_against_the_completed_contract(
workbench_api, workbench_db, deadline_scan, source, invalid
):
diff --git a/plugins/codex-security/tests/test_deep_scan_successful_publication.py b/plugins/codex-security/tests/test_deep_scan_successful_publication.py
index 560596b96..f6ab18371 100644
--- a/plugins/codex-security/tests/test_deep_scan_successful_publication.py
+++ b/plugins/codex-security/tests/test_deep_scan_successful_publication.py
@@ -171,6 +171,30 @@ def complete(workbench_api, connection, scan, *, prepare_only=False):
)["scan"]
+def fail_scan(workbench_api, connection, scan, message="Stopped."):
+ return workbench_api["saved_results"].fail_scan(
+ workbench_api["_WORKBENCH_DB_CONTEXT"],
+ connection,
+ Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message=message),
+ )["scan"]
+
+
+def preserve_scan_results(workbench_api, connection, scan):
+ return workbench_api["saved_results"].preserve_scan_results(
+ workbench_api["_WORKBENCH_DB_CONTEXT"],
+ connection,
+ Namespace(
+ scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None
+ ),
+ )["scan"]
+
+
+def recover_scan_results(workbench_api, connection, scan):
+ return workbench_api["saved_results"].recover_scan_results(
+ workbench_api["_WORKBENCH_DB_CONTEXT"], connection, Namespace(scan_id=scan.scan_id)
+ )["scan"]
+
+
def assert_published_aggregate(scan):
findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]
for finding in findings:
@@ -663,13 +687,7 @@ def test_stopped_deep_scan_still_salvages_saved_findings(
result.write_text("{interrupted worker output")
result_bytes = result.read_bytes()
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Scan interrupted."
- ),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Scan interrupted.")
assert stopped["progress"]["status"] == "failed"
manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())
@@ -683,9 +701,7 @@ def test_stopped_deep_scan_still_salvages_saved_findings(
artifact_names = ("scan-manifest.json", "findings.json", "coverage.json")
published = {name: (scan.scan_dir / name).read_bytes() for name in artifact_names}
- recovered = workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ recovered = recover_scan_results(workbench_api, workbench_db, scan)
assert recovered["findingCount"] == len(expected_summaries)
assert {name: (scan.scan_dir / name).read_bytes() for name in artifact_names} == published
if checkpoint is not None:
@@ -726,13 +742,7 @@ def test_stopped_deep_scan_ignores_non_reducer_sources_without_coverage(
source_bytes = source_path.read_bytes()
source_relative = source_path.relative_to(scan.scan_dir).as_posix()
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Scan interrupted."
- ),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Scan interrupted.")
assert stopped["progress"]["status"] == "failed"
assert stopped["resultsRecoveryNeeded"] is False
@@ -749,9 +759,7 @@ def test_stopped_deep_scan_ignores_non_reducer_sources_without_coverage(
artifact_names = ("scan-manifest.json", "findings.json", "coverage.json")
published = {name: (scan.scan_dir / name).read_bytes() for name in artifact_names}
- recovered = workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
- )["scan"]
+ recovered = recover_scan_results(workbench_api, workbench_db, scan)
assert recovered["findingCount"] == 1
assert recovered["resultsRecoveryNeeded"] is False
@@ -943,13 +951,7 @@ def test_deep_publication_preserves_original_scope_kinds_after_checkout_changes(
== file_paths
)
if stopped:
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="interrupted"
- ),
- )
+ fail_scan(workbench_api, workbench_db, scan, "interrupted")
else:
assert complete(workbench_api, workbench_db, scan)["progress"]["status"] == "complete"
expected_paths = ["subdir/extract.py"] + ([] if file_paths else ["subdir/extract.py/generated"])
@@ -998,13 +1000,7 @@ def test_stopped_deep_projection_filters_the_canonical_parent(
scan = publication_scan(paths=[scope, "background jobs"])
scan.findings[0]["locations"] = [{"path": location, "startLine": 1}]
(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": scan.findings}))
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(
- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Scan interrupted."
- ),
- )["scan"]
+ stopped = fail_scan(workbench_api, workbench_db, scan, "Scan interrupted.")
assert stopped["progress"]["status"] == "failed"
assert stopped["findingCount"] == 0
assert json.loads((scan.scan_dir / "findings.json").read_text())["findings"] == []
@@ -1028,19 +1024,13 @@ def test_stopped_file_scope_filters_siblings_before_assigning_identity(
checkpoint_bytes = checkpoint.read_bytes()
for name in ("scan-manifest.json", "findings.json", "coverage.json"):
(scan.scan_dir / name).unlink()
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Interrupted."),
- )
+ fail_scan(workbench_api, workbench_db, scan, "Interrupted.")
findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]
assert len(findings) == 1
assert findings[0]["locations"][0]["path"] == "subdir/extract.py"
assert findings[0]["identity"] == {"anchor": "candidate-review"}
names = ("scan-manifest.json", "findings.json", "coverage.json")
published = {name: (scan.scan_dir / name).read_bytes() for name in names}
- workbench_api["saved_results"].recover_scan_results(
- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
- )
+ recover_scan_results(workbench_api, workbench_db, scan)
assert {name: (scan.scan_dir / name).read_bytes() for name in names} == published
assert checkpoint.read_bytes() == checkpoint_bytes
diff --git a/plugins/codex-security/tests/test_finalize_scan_contract.py b/plugins/codex-security/tests/test_finalize_scan_contract.py
index da4ebd529..78a7c2a89 100644
--- a/plugins/codex-security/tests/test_finalize_scan_contract.py
+++ b/plugins/codex-security/tests/test_finalize_scan_contract.py
@@ -118,32 +118,7 @@ class FinalizeScanContractTest(FinalizerSarifCases, ScanFixtureTestCase):
self.write_json("scan-manifest.json", self.manifest)
self.write_json("findings.json", self.findings)
self.write_json("coverage.json", self.coverage)
- (self.scan_dir / "report.md").write_text(
- """# Security Review: example/repo
-
-## Findings
-
-| Title | Severity |
-| --- | --- |
-| [Unsafe archive extraction can escape the output directory](#1-unsafe-archive-extraction-can-escape-the-output-directory) | high |
-
-### [1] Unsafe archive extraction can escape the output directory
-
-| Field | Value |
-| --- | --- |
-| Severity | high |
-| Confidence | high |
-| Confidence rationale | Direct source trace reaches the filesystem write without a containment check. |
-| Category | Path traversal |
-| CWE | CWE-22 |
-| Affected lines | src/extract.py:41-44 |
-
-#### Summary
-
-The extraction root is not enforced.
-""",
- encoding="utf-8",
- )
+ (self.scan_dir / "report.md").write_text("# Existing report\n", encoding="utf-8")
def completion_binding(self) -> dict[str, object]:
return {
@@ -162,18 +137,6 @@ The extraction root is not enforced.
"coverageMode": "repository",
}
- def rewrite_sealed_artifact(self, relative_path: str, payload: object) -> None:
- self.write_json(relative_path, payload)
- manifest = self.read_json("scan-manifest.json")
- artifacts = manifest["scan"]["artifacts"]
- for artifact in artifacts:
- if artifact["path"] == relative_path:
- artifact["sha256"] = self.sha256_file(relative_path)
- break
- else:
- raise AssertionError(f"missing sealed artifact: {relative_path}")
- self.write_json("scan-manifest.json", manifest)
-
@contextmanager
def preserving_sealed_findings(self, findings: dict) -> Iterator[None]:
self.rewrite_sealed_artifact("findings.json", findings)
@@ -627,15 +590,7 @@ The extraction root is not enforced.
)
coverage = self.read_json("coverage.json")
coverage["inventoryStrategy"] = "deep_repository_repeated_discovery"
- self.write_json("coverage.json", coverage)
- manifest = self.read_json("scan-manifest.json")
- coverage_artifact = next(
- artifact
- for artifact in manifest["scan"]["artifacts"]
- if artifact["path"] == "coverage.json"
- )
- coverage_artifact["sha256"] = self.sha256_file("coverage.json")
- self.write_json("scan-manifest.json", manifest)
+ self.rewrite_sealed_artifact("coverage.json", coverage)
with self.assertRaisesRegex(
FINALIZER.ContractError,
@@ -1453,19 +1408,13 @@ The extraction root is not enforced.
def test_rejects_sealed_findings_missing_derived_identity(self) -> None:
self.write_sealed_scan()
- sealed_manifest = self.read_json("scan-manifest.json")
sealed_findings = self.read_json("findings.json")
for field in ("findingId", "occurrenceId", "fingerprints"):
with self.subTest(field=field):
- manifest = copy.deepcopy(sealed_manifest)
findings = copy.deepcopy(sealed_findings)
findings["findings"][0].pop(field)
- self.write_json("findings.json", findings)
- for artifact in manifest["scan"]["artifacts"]:
- if artifact["path"] == "findings.json":
- artifact["sha256"] = self.sha256_file("findings.json")
- self.write_json("scan-manifest.json", manifest)
+ self.rewrite_sealed_artifact("findings.json", findings)
with self.assertRaisesRegex(FINALIZER.ContractError, field):
FINALIZER.finalize_scan(self.scan_dir)
@@ -2356,40 +2305,27 @@ The extraction root is not enforced.
):
FINALIZER.finalize_scan(self.scan_dir)
- def test_rejects_remote_url_credentials(self) -> None:
- self.manifest["scan"]["target"]["remote"] = "https://[email protected]/repo"
+ def test_rejects_remote_credentials_and_query(self) -> None:
self.write_scan()
- with self.assertRaisesRegex(FINALIZER.ContractError, "must not contain credentials"):
- FINALIZER.finalize_scan(self.scan_dir)
-
- def test_rejects_remote_url_query(self) -> None:
- self.manifest["scan"]["target"]["remote"] = "https://example.com/repo?token=secret"
- self.write_scan()
- with self.assertRaisesRegex(FINALIZER.ContractError, "must not contain credentials"):
- FINALIZER.finalize_scan(self.scan_dir)
-
- def test_rejects_remote_control_characters(self) -> None:
- for character in ("\0", "\t", "\n", "\r", "\x7f", "\x85", "\u2028", "\u2029"):
- with self.subTest(character=repr(character)):
- self.manifest["scan"]["target"]["remote"] = f"https://example.com{character}/repo"
- self.write_scan()
- with self.assertRaisesRegex(
- FINALIZER.ContractError, "expected a sanitized canonical absolute URL"
- ):
- FINALIZER.finalize_scan(self.scan_dir)
-
- def test_rejects_remote_unsupported_port(self) -> None:
- for remote in ("https://example.com:bad/repo", "https://example.com:99999/repo"):
+ for remote in (
+ "https://[email protected]/repo",
+ "https://example.com/repo?token=secret",
+ "https://@example.com/repo",
+ ):
with self.subTest(remote=remote):
self.manifest["scan"]["target"]["remote"] = remote
- self.write_scan()
+ self.write_json("scan-manifest.json", self.manifest)
with self.assertRaisesRegex(
- FINALIZER.ContractError, "expected a sanitized canonical absolute URL"
+ FINALIZER.ContractError, "must not contain credentials"
):
FINALIZER.finalize_scan(self.scan_dir)
def test_rejects_remote_unsafe_authority(self) -> None:
+ self.write_scan()
for remote in (
+ *(f"https://example.com{char}/repo" for char in "\0\t\n\r\x7f\x85\u2028\u2029"),
+ "https://example.com:bad/repo",
+ "https://example.com:99999/repo",
"https://exa mple.com/repo",
"https://example.com>/repo",
"https://exa^mple.com/repo",
@@ -2401,18 +2337,12 @@ The extraction root is not enforced.
):
with self.subTest(remote=remote):
self.manifest["scan"]["target"]["remote"] = remote
- self.write_scan()
+ self.write_json("scan-manifest.json", self.manifest)
with self.assertRaisesRegex(
FINALIZER.ContractError, "expected a sanitized canonical absolute URL"
):
FINALIZER.finalize_scan(self.scan_dir)
- def test_rejects_remote_bare_userinfo_separator(self) -> None:
- self.manifest["scan"]["target"]["remote"] = "https://@example.com/repo"
- self.write_scan()
- with self.assertRaisesRegex(FINALIZER.ContractError, "must not contain credentials"):
- FINALIZER.finalize_scan(self.scan_dir)
-
def test_seals_remote_port_and_ipv6_authority(self) -> None:
for remote in (
"https://example.com:8022/repo",
diff --git a/plugins/codex-security/tests/test_finding_issue_receipts.py b/plugins/codex-security/tests/test_finding_issue_receipts.py
index e835cf884..1b690ded0 100644
--- a/plugins/codex-security/tests/test_finding_issue_receipts.py
+++ b/plugins/codex-security/tests/test_finding_issue_receipts.py
@@ -242,25 +242,27 @@ def test_readback_replay_preserves_the_accepted_receipt_url(tmp_path: Path, oper
assert issues(state, scan_dir, "inspect")["receipts"] == [expected]
- "destination",
- [
+def test_destination_scope_is_part_of_receipt_identity(tmp_path: Path) -> None:
+ scan_dir, validated = source(tmp_path)
+ state = tmp_path / "state"
+ accepted = receipt_for(validated)
+ issues(state, scan_dir, "record", receipts=[accepted])
+ destinations = (
{"type": "linear", "teamId": "different-team"},
{"type": "linear", "teamId": "team-example", "projectId": "project-example"},
JIRA,
{"type": "github-issue", "hostname": "github.com", "repository": "example/repo"},
{"type": "github-advisory", "hostname": "github.com", "repository": "example/repo"},
- ],
-)
-def test_destination_scope_is_part_of_receipt_identity(tmp_path: Path, destination: dict) -> None:
- scan_dir, validated = source(tmp_path)
- state = tmp_path / "state"
- accepted = receipt_for(validated)
- issues(state, scan_dir, "record", receipts=[accepted])
- assert issues(state, scan_dir, "inspect", destination=destination)["receipts"] == []
- issues(state, scan_dir, "record", destination=destination, receipts=[accepted])
- assert len(issues(state, scan_dir, "inspect", destination=destination)["receipts"]) == 1
- assert len(issues(state, scan_dir, "inspect")["receipts"]) == 1
+ )
+ for destination in destinations:
+ inspected = issues(state, scan_dir, "inspect", destination=destination)
+ assert inspected["receipts"] == [], destination
+ issues(state, scan_dir, "record", destination=destination, receipts=[accepted])
+
+ expected = [{**accepted, "scanId": validated["manifest"]["scan"]["id"]}]
+ for destination in (LINEAR, *destinations):
+ inspected = issues(state, scan_dir, "inspect", destination=destination)
+ assert inspected["receipts"] == expected, destination
def test_same_issue_can_track_a_later_occurrence(tmp_path: Path) -> None:
diff --git a/plugins/codex-security/tests/test_finding_preview.py b/plugins/codex-security/tests/test_finding_preview.py
index b19925c95..1bfcf4c5c 100644
--- a/plugins/codex-security/tests/test_finding_preview.py
+++ b/plugins/codex-security/tests/test_finding_preview.py
@@ -1,19 +1,17 @@
from __future__ import annotations
import json
-import runpy
from copy import deepcopy
-from pathlib import Path
import pytest
+from workbench_test_support import load_script
-FINDING_PREVIEW_SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "finding_preview.py"
+preview = load_script("finding_preview")
pytestmark = pytest.mark.cross_platform
def test_compact_finding_fields_survive_verbose_code_evidence() -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
compact = {
"severity": {"level": "high", "rationale": "Synthetic impact"},
"confidence": {"level": "high", "rationale": "Synthetic validation"},
@@ -32,13 +30,13 @@ def test_compact_finding_fields_survive_verbose_code_evidence() -> None:
],
}
- bounded = preview["bounded_finding_details"](finding)
+ bounded = preview.bounded_finding_details(finding)
assert {key: bounded[key] for key in compact} == compact
assert bounded["codeEvidence"]
assert (
len(json.dumps(bounded, separators=(",", ":")).encode())
- <= preview["FINDING_DETAILS_PREVIEW_BYTES"]
+ <= preview.FINDING_DETAILS_PREVIEW_BYTES
)
@@ -57,7 +55,6 @@ def test_compact_finding_fields_survive_verbose_code_evidence() -> None:
def test_verbose_finding_metadata_retains_diagnostics_and_report(
metadata: str, value: object
) -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
finding = {
"identity": {"anchor": "example-finding"},
"status": "validated",
@@ -69,7 +66,7 @@ def test_verbose_finding_metadata_retains_diagnostics_and_report(
metadata: value,
}
- bounded = preview["bounded_finding_details"](finding)
+ bounded = preview.bounded_finding_details(finding)
assert {key: bounded[key] for key in finding if key != metadata} == {
key: value for key, value in finding.items() if key != metadata
@@ -82,13 +79,12 @@ def test_verbose_finding_metadata_retains_diagnostics_and_report(
assert bounded[metadata]["rationale"]
assert (
len(json.dumps(bounded, separators=(",", ":")).encode())
- <= preview["FINDING_DETAILS_PREVIEW_BYTES"]
+ <= preview.FINDING_DETAILS_PREVIEW_BYTES
)
@pytest.mark.parametrize("extra_metadata", [False, True])
def test_multiple_verbose_metadata_sections_each_retain_a_preview(extra_metadata: bool) -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
length = 20_000
finding = {
"severity": {"level": "high", "rationale": "s" * length},
@@ -104,7 +100,7 @@ def test_multiple_verbose_metadata_sections_each_retain_a_preview(extra_metadata
taxonomy={"category": "t" * length},
)
- bounded = preview["bounded_finding_details"](finding)
+ bounded = preview.bounded_finding_details(finding)
assert set(bounded) == set(finding)
for key in ("severity", "confidence"):
@@ -115,18 +111,17 @@ def test_multiple_verbose_metadata_sections_each_retain_a_preview(extra_metadata
assert bounded["validation"] == finding["validation"]
assert (
len(json.dumps(bounded, separators=(",", ":")).encode())
- <= preview["FINDING_DETAILS_PREVIEW_BYTES"]
+ <= preview.FINDING_DETAILS_PREVIEW_BYTES
)
def test_nested_attack_path_stays_within_its_preview_budget() -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
attack_path = {
f"branch-{branch}": {f"node-{node}": "\n😀" * 40 for node in range(3)}
for branch in range(3)
}
- bounded = preview["bounded_finding_details"]({"attackPath": attack_path})["attackPath"]
+ bounded = preview.bounded_finding_details({"attackPath": attack_path})["attackPath"]
assert bounded["branch-0"]["node-0"] == "\n😀" * 40
assert len(json.dumps(bounded, separators=(",", ":")).encode()) <= 4_000
@@ -134,7 +129,6 @@ def test_nested_attack_path_stays_within_its_preview_budget() -> None:
def test_bounded_finding_details_normalizes_scalar_attack_path_assessments() -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
scalar_finding = {
"attackPath": {
"impact": "Native memory corruption is possible.",
@@ -142,7 +136,7 @@ def test_bounded_finding_details_normalizes_scalar_attack_path_assessments() ->
}
}
- bounded = preview["bounded_finding_details"](scalar_finding)
+ bounded = preview.bounded_finding_details(scalar_finding)
assert bounded["attackPath"]["impact"] == {"rationale": "Native memory corruption is possible."}
assert bounded["attackPath"]["likelihood"] == {"level": "medium"}
@@ -159,19 +153,18 @@ def test_bounded_finding_details_normalizes_scalar_attack_path_assessments() ->
"likelihood": None,
}
}
- structured = preview["bounded_finding_details"](structured_finding)
+ structured = preview.bounded_finding_details(structured_finding)
assert structured["attackPath"]["impact"] == structured_finding["attackPath"]["impact"]
assert structured["attackPath"]["likelihood"] is None
- missing = preview["bounded_finding_details"]({"attackPath": {"summary": "An upload is read."}})
+ missing = preview.bounded_finding_details({"attackPath": {"summary": "An upload is read."}})
assert "impact" not in missing["attackPath"]
assert "likelihood" not in missing["attackPath"]
def test_nested_attack_path_string_arrays_survive_without_relaxing_section_depth() -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
original = {
"attackPath": {
"dataflow": {
@@ -183,7 +176,7 @@ def test_nested_attack_path_string_arrays_survive_without_relaxing_section_depth
}
finding = deepcopy(original)
- bounded = preview["bounded_finding_details"](finding)
+ bounded = preview.bounded_finding_details(finding)
assert bounded == {
"attackPath": {
@@ -198,7 +191,6 @@ def test_nested_attack_path_string_arrays_survive_without_relaxing_section_depth
def test_bounded_finding_details_merges_root_cause_aliases() -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
original = {
"rootCause": {"code": "SELECT * FROM users"},
"root_cause": {
@@ -209,7 +201,7 @@ def test_bounded_finding_details_merges_root_cause_aliases() -> None:
},
}
- bounded = preview["bounded_finding_details"](original)
+ bounded = preview.bounded_finding_details(original)
assert bounded["rootCause"] == {
"code": "SELECT * FROM users",
@@ -221,7 +213,6 @@ def test_bounded_finding_details_merges_root_cause_aliases() -> None:
def test_bounded_finding_details_ignores_malformed_root_cause_alias() -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
original = {
"rootCause": {"summary": 42},
"root_cause": {
@@ -231,7 +222,7 @@ def test_bounded_finding_details_ignores_malformed_root_cause_alias() -> None:
"code_evidence": [{"id": "legacy-source", "code": "legacy_source()"}],
}
- bounded = preview["bounded_finding_details"](original)
+ bounded = preview.bounded_finding_details(original)
assert bounded["rootCause"] == {
"evidenceRefs": ["legacy-source"],
@@ -241,7 +232,6 @@ def test_bounded_finding_details_ignores_malformed_root_cause_alias() -> None:
def test_bounded_finding_details_strips_invalid_legacy_evidence_fields() -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
original = {
"code_evidence": [
{
@@ -255,7 +245,7 @@ def test_bounded_finding_details_strips_invalid_legacy_evidence_fields() -> None
]
}
- bounded = preview["bounded_finding_details"](original)
+ bounded = preview.bounded_finding_details(original)
assert bounded["code_evidence"] == [{"id": "legacy-source", "code": "dangerous_call()"}]
assert original["code_evidence"][0]["startLine"] == 0
@@ -263,8 +253,7 @@ def test_bounded_finding_details_strips_invalid_legacy_evidence_fields() -> None
def test_bounded_finding_details_reserves_core_sections() -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
- bounded = preview["bounded_finding_details"](
+ bounded = preview.bounded_finding_details(
{
"attackPath": {
"blindspots": ["x" * 20_000],
@@ -319,23 +308,22 @@ def test_bounded_finding_details_reserves_core_sections() -> None:
assert bounded["attackPath"]["preconditions"] == ["Automatic extraction is enabled."]
assert bounded["attackPath"]["evidenceRefs"] == ["evidence-0"]
assert bounded["writeup"] == {"reportPath": "findings/archive-traversal/archive-traversal.md"}
- assert len(bounded["codeEvidence"]) == preview["FINDING_CODE_EVIDENCE_LIMIT"]
+ assert len(bounded["codeEvidence"]) == preview.FINDING_CODE_EVIDENCE_LIMIT
assert bounded["codeEvidence"][0]["role"] == "user_input"
assert all(
len(json.dumps(item["code"], separators=(",", ":")).encode("utf-8"))
- <= preview["FINDING_CODE_EVIDENCE_SNIPPET_BYTES"]
+ <= preview.FINDING_CODE_EVIDENCE_SNIPPET_BYTES
for item in bounded["codeEvidence"]
)
assert (
len(json.dumps(bounded, separators=(",", ":")).encode("utf-8"))
- <= preview["FINDING_DETAILS_PREVIEW_BYTES"]
+ <= preview.FINDING_DETAILS_PREVIEW_BYTES
)
def test_bounded_finding_details_keeps_guidance_and_both_evidence_aliases() -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
- bounded = preview["bounded_finding_details"](
+ bounded = preview.bounded_finding_details(
{
"codeEvidence": [
{"id": "canonical", "code": "canonical_source()"},
@@ -356,9 +344,8 @@ def test_bounded_finding_details_keeps_guidance_and_both_evidence_aliases() -> N
def test_bounded_finding_details_deduplicates_evidence_before_limiting() -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
- bounded = preview["bounded_finding_details"](
+ bounded = preview.bounded_finding_details(
{
"codeEvidence": [
{"id": "shared", "code": "canonical_shared()"},
@@ -384,9 +371,8 @@ def test_bounded_finding_details_deduplicates_evidence_before_limiting() -> None
def test_bounded_finding_details_filters_malformed_evidence_before_limiting() -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
- bounded = preview["bounded_finding_details"](
+ bounded = preview.bounded_finding_details(
{
"code_evidence": [
None,
@@ -402,9 +388,7 @@ def test_bounded_finding_details_filters_malformed_evidence_before_limiting() ->
def test_large_code_preview_bounds_sizing_probes(monkeypatch: pytest.MonkeyPatch) -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
- project = preview["bounded_finding_details"]
- original_size = project.__globals__["json_size"]
+ original_size = preview.json_size
probe_lengths: list[int] = []
def observed_size(value: object) -> int:
@@ -412,7 +396,7 @@ def test_large_code_preview_bounds_sizing_probes(monkeypatch: pytest.MonkeyPatch
probe_lengths.append(len(value))
return original_size(value)
- monkeypatch.setitem(project.__globals__, "json_size", observed_size)
+ monkeypatch.setattr(preview, "json_size", observed_size)
finding = {
"preventiveControls": ["Keep the authorization check centralized."],
"codeEvidence": [
@@ -440,12 +424,12 @@ def test_large_code_preview_bounds_sizing_probes(monkeypatch: pytest.MonkeyPatch
"preventiveControls": original["preventiveControls"],
}
- bounded = project(finding)
+ bounded = preview.bounded_finding_details(finding)
assert bounded == expected
encoded = json.dumps(bounded, separators=(",", ":")).encode("ascii")
assert encoded == json.dumps(expected, separators=(",", ":")).encode("ascii")
- assert len(encoded) <= preview["FINDING_DETAILS_PREVIEW_BYTES"]
+ assert len(encoded) <= preview.FINDING_DETAILS_PREVIEW_BYTES
assert finding == original
assert 1_498 in probe_lengths
assert max(probe_lengths) <= 1_500
@@ -457,7 +441,6 @@ def test_large_code_preview_bounds_sizing_probes(monkeypatch: pytest.MonkeyPatch
ids=["empty", "ascii", "escapes", "bmp", "supplementary", "mixed", "lone-surrogate"],
)
def test_bounded_json_text_matches_exhaustive_prefix_oracle(value: str) -> None:
- preview = runpy.run_path(str(FINDING_PREVIEW_SCRIPT))
prefixes = [
(value[:end], len(json.dumps(value[:end]).encode("ascii"))) for end in range(len(value) + 1)
]
@@ -467,8 +450,8 @@ def test_bounded_json_text_matches_exhaustive_prefix_oracle(value: str) -> None:
for budget in sorted(budgets):
expected = next((item for item in reversed(prefixes) if item[1] <= budget), ("", 2))
- assert preview["bounded_json_text"](value, budget) == expected, (repr(value), budget)
+ assert preview.bounded_json_text(value, budget) == expected, (repr(value), budget)
if budget <= 0:
remaining = [budget]
- assert preview["bounded_json_value"](value, remaining) is None
+ assert preview.bounded_json_value(value, remaining) is None
assert remaining == [budget]
diff --git a/plugins/codex-security/tests/test_git_inventory_regressions.py b/plugins/codex-security/tests/test_git_inventory_regressions.py
index b70137d6b..ddc0c2ac6 100644
--- a/plugins/codex-security/tests/test_git_inventory_regressions.py
+++ b/plugins/codex-security/tests/test_git_inventory_regressions.py
@@ -123,6 +123,16 @@ def test_excerpt_uses_target_relative_committed_path(tmp_path: Path) -> None:
)
[email protected](scope="module")
+def excerpt_repository(tmp_path_factory):
+ target = tmp_path_factory.mktemp("source-excerpts") / "target"
+ revision = initialize_git_repository(target)
+ scopes = load_script("workbench_source_scopes").capture_source_scopes(
+ target, (revision, None, 0, 0), ["."]
+ )
+ return target, revision, json.dumps(scopes)
+
+
@pytest.mark.parametrize("captured", [False, True], ids=["legacy-scopes", "saved-scopes"])
@pytest.mark.parametrize(
("mode", "kind", "expected"),
@@ -136,10 +146,9 @@ def test_excerpt_uses_target_relative_committed_path(tmp_path: Path) -> None:
],
)
def test_saved_excerpt_requires_a_known_diff_source(
- tmp_path: Path, mode: str, kind: str | None, expected: str | None, captured: bool
+ excerpt_repository, mode: str, kind: str | None, expected: str | None, captured: bool
) -> None:
- target = tmp_path / "target"
- revision = initialize_git_repository(target)
+ target, revision, scopes_json = excerpt_repository
scan = {
"mode": mode,
"diff_target_kind": kind,
@@ -147,10 +156,7 @@ def test_saved_excerpt_requires_a_known_diff_source(
"target_snapshot_digest": None,
}
if captured:
- scopes = load_script("workbench_source_scopes").capture_source_scopes(
- target, (revision, None, 0, 0), ["."]
- )
- scan["source_scopes_json"] = json.dumps(scopes)
+ scan["source_scopes_json"] = scopes_json
assert (
load_script("workbench_source_excerpt").finding_source_excerpt(
scan, target, [{"path": "README.md", "startLine": 1}], ["."]
diff --git a/plugins/codex-security/tests/test_partial_coverage_projection.py b/plugins/codex-security/tests/test_partial_coverage_projection.py
index 783614a5a..3b51f36c1 100644
--- a/plugins/codex-security/tests/test_partial_coverage_projection.py
+++ b/plugins/codex-security/tests/test_partial_coverage_projection.py
@@ -13,6 +13,29 @@ from test_deep_scan_successful_publication import publication_scan as publicatio
from workbench_test_support import write_checkpoint
+def stop_scan(workbench_api, workbench_db, monkeypatch):
+ saved = workbench_api["saved_results"]
+
+ def fail_publication(*args, **kwargs):
+ raise OSError("Synthetic publication interruption.")
+
+ def stop(scan, interrupt_publication):
+ with monkeypatch.context() as patch:
+ if interrupt_publication:
+ patch.setattr(saved, "_write_prepared_scan_finalization", fail_publication)
+ stopped = saved.fail_scan(
+ workbench_api["_WORKBENCH_DB_CONTEXT"],
+ workbench_db,
+ Namespace(
+ scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."
+ ),
+ )["scan"]
+ assert stopped["resultsRecoveryNeeded"] is interrupt_publication
+
+ return stop
+
+
@pytest.mark.parametrize("attempt", [1, 2])
@pytest.mark.parametrize("has_head", [False, True])
@pytest.mark.parametrize("newer_parent", [False, True])
@@ -21,7 +44,7 @@ def test_parent_candidate_outcome_compares_headless_worker_chronology(
workbench_api,
workbench_db,
publication_scan,
- monkeypatch,
+ stop_scan,
attempt,
has_head,
newer_parent,
@@ -90,22 +113,7 @@ def test_parent_candidate_outcome_compares_headless_worker_chronology(
)
modified = 400 if newer_parent else 200
os.utime(parent, ns=(modified, modified))
- with monkeypatch.context() as interrupted:
- if retry_publication:
-
- def fail_publication(*args, **kwargs):
- raise OSError("Synthetic publication interruption.")
-
- interrupted.setattr(
- workbench_api["saved_results"],
- "_write_prepared_scan_finalization",
- fail_publication,
- )
- workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ stop_scan(scan, retry_publication)
workbench_api["saved_results"].recover_scan_results(
workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
)
@@ -602,7 +610,7 @@ def test_missing_projection_keeps_surface_links_and_independent_reviews(
@pytest.mark.parametrize("attempt", [1, 2])
@pytest.mark.parametrize("retry_publication", [False, True])
def test_previous_attempt_resolution_does_not_clear_current_gap(
- workbench_api, workbench_db, publication_scan, attempt, retry_publication, monkeypatch
+ workbench_api, workbench_db, publication_scan, attempt, retry_publication, stop_scan
):
scan = publication_scan()
result = add_worker(workbench_db, scan)
@@ -651,23 +659,7 @@ def test_previous_attempt_resolution_does_not_clear_current_gap(
os.utime(result, ns=(observed, observed))
parent_observed = observed + 2_000_000_000
os.utime(parent_path, ns=(parent_observed, parent_observed))
- with monkeypatch.context() as interrupted:
- if retry_publication:
-
- def fail_publication(*args, **kwargs):
- raise OSError("Synthetic publication interruption.")
-
- interrupted.setattr(
- workbench_api["saved_results"],
- "_write_prepared_scan_finalization",
- fail_publication,
- )
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )["scan"]
- assert stopped["resultsRecoveryNeeded"] is retry_publication
+ stop_scan(scan, retry_publication)
recovered = workbench_api["saved_results"].recover_scan_results(
workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
)["scan"]
@@ -737,7 +729,7 @@ def test_recovery_compares_open_questions_using_canonical_normalization(
@pytest.mark.parametrize("parent_projection", ["raw", "projected", "missing"])
@pytest.mark.parametrize("retry_publication", [False, True])
def test_rejected_retry_preserves_original_finding_history(
- workbench_api, workbench_db, publication_scan, monkeypatch, parent_projection, retry_publication
+ workbench_api, workbench_db, publication_scan, stop_scan, parent_projection, retry_publication
):
scan = publication_scan()
result = add_worker(workbench_db, scan)
@@ -817,23 +809,7 @@ def test_rejected_retry_preserves_original_finding_history(
for name in ("scan-manifest.json", "findings.json", "coverage.json"):
(scan.scan_dir / name).unlink()
originals = {path: path.read_bytes() for path in (previous, result, other_result)}
- with monkeypatch.context() as interrupted:
- if retry_publication:
-
- def fail_publication(*args, **kwargs):
- raise OSError("Synthetic publication interruption.")
-
- interrupted.setattr(
- workbench_api["saved_results"],
- "_write_prepared_scan_finalization",
- fail_publication,
- )
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )["scan"]
- assert stopped["resultsRecoveryNeeded"] is retry_publication
+ stop_scan(scan, retry_publication)
recovered = workbench_api["saved_results"].recover_scan_results(
workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
)["scan"]
@@ -861,7 +837,7 @@ def test_rejected_retry_preserves_original_finding_history(
"recovery", [None, "retry", "missing", "changed", "incomplete", "late", "fallback-late"]
)
def test_stopped_recovery_keeps_current_parent_projection(
- workbench_api, workbench_db, publication_scan, monkeypatch, older_complete, recovery
+ workbench_api, workbench_db, publication_scan, stop_scan, older_complete, recovery
):
retry_publication = recovery not in {None, "fallback-late"}
scan = publication_scan()
@@ -944,23 +920,7 @@ def test_stopped_recovery_keeps_current_parent_projection(
(scan.scan_dir / name).unlink()
obsolete.unlink()
del original_sources[obsolete]
- with monkeypatch.context() as interrupted:
- if retry_publication:
-
- def fail_publication(*args, **kwargs):
- raise OSError("Synthetic publication interruption.")
-
- interrupted.setattr(
- workbench_api["saved_results"],
- "_write_prepared_scan_finalization",
- fail_publication,
- )
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )["scan"]
- assert stopped["resultsRecoveryNeeded"] is retry_publication
+ stop_scan(scan, retry_publication)
frozen_sources = {
path: path.read_bytes() for path in (scan.scan_dir / "checkpoints").glob("*.json")
}
@@ -1087,7 +1047,7 @@ def test_matching_accepted_checkpoint_preserves_receipt_path(
@pytest.mark.parametrize("retry_publication", [False, True])
def test_accepted_worker_missing_completeness_keeps_valid_findings(
- workbench_api, workbench_db, publication_scan, monkeypatch, retry_publication
+ workbench_api, workbench_db, publication_scan, stop_scan, retry_publication
):
scan = publication_scan()
result = add_worker(workbench_db, scan)
@@ -1108,23 +1068,7 @@ def test_accepted_worker_missing_completeness_keeps_valid_findings(
)
)
original = result.read_bytes()
- with monkeypatch.context() as interrupted:
- if retry_publication:
-
- def fail_publication(*args, **kwargs):
- raise OSError("Synthetic publication interruption.")
-
- interrupted.setattr(
- workbench_api["saved_results"],
- "_write_prepared_scan_finalization",
- fail_publication,
- )
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )["scan"]
- assert stopped["resultsRecoveryNeeded"] is retry_publication
+ stop_scan(scan, retry_publication)
recovered = workbench_api["saved_results"].recover_scan_results(
workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
)["scan"]
@@ -1139,7 +1083,7 @@ def test_accepted_worker_missing_completeness_keeps_valid_findings(
@pytest.mark.parametrize("retry_publication", [False, True])
def test_idless_accepted_surface_matches_projection_after_checkpoint_id_inference(
- workbench_api, workbench_db, publication_scan, monkeypatch, retry_publication
+ workbench_api, workbench_db, publication_scan, stop_scan, retry_publication
):
scan = publication_scan()
result = add_worker(workbench_db, scan)
@@ -1181,23 +1125,7 @@ def test_idless_accepted_surface_matches_projection_after_checkpoint_id_inferenc
)
)
originals = {path: path.read_bytes() for path in (result, checkpoint)}
- with monkeypatch.context() as interrupted:
- if retry_publication:
-
- def fail_publication(*args, **kwargs):
- raise OSError("Synthetic publication interruption.")
-
- interrupted.setattr(
- workbench_api["saved_results"],
- "_write_prepared_scan_finalization",
- fail_publication,
- )
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )["scan"]
- assert stopped["resultsRecoveryNeeded"] is retry_publication
+ stop_scan(scan, retry_publication)
recovered = workbench_api["saved_results"].recover_scan_results(
workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
)["scan"]
@@ -1220,7 +1148,7 @@ def test_parent_candidate_provenance_cannot_override_accepted_worker_evidence(
workbench_api,
workbench_db,
publication_scan,
- monkeypatch,
+ stop_scan,
claimed_worker,
retry_publication,
selected_checkpoint,
@@ -1287,23 +1215,7 @@ def test_parent_candidate_provenance_cannot_override_accepted_worker_evidence(
)
)
os.utime(scan.scan_dir / "coverage.json", ns=(400, 400))
- with monkeypatch.context() as interrupted:
- if retry_publication:
-
- def fail_publication(*args, **kwargs):
- raise OSError("Synthetic publication interruption.")
-
- interrupted.setattr(
- workbench_api["saved_results"],
- "_write_prepared_scan_finalization",
- fail_publication,
- )
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )["scan"]
- assert stopped["resultsRecoveryNeeded"] is retry_publication
+ stop_scan(scan, retry_publication)
recovered = workbench_api["saved_results"].recover_scan_results(
workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
)["scan"]
@@ -1316,7 +1228,7 @@ def test_parent_candidate_provenance_cannot_override_accepted_worker_evidence(
@pytest.mark.parametrize("other_worker", [False, True])
@pytest.mark.parametrize("retry_publication", [False, True])
def test_projected_generic_closure_updates_parent_copy_in_its_worker_namespace(
- workbench_api, workbench_db, publication_scan, monkeypatch, other_worker, retry_publication
+ workbench_api, workbench_db, publication_scan, stop_scan, other_worker, retry_publication
):
scan = publication_scan()
result = add_worker(workbench_db, scan)
@@ -1388,23 +1300,7 @@ def test_projected_generic_closure_updates_parent_copy_in_its_worker_namespace(
json.dumps({"checkpoint": checkpoint.name})
)
originals = {path: path.read_bytes() for path in {result, closure_result, checkpoint}}
- with monkeypatch.context() as interrupted:
- if retry_publication:
-
- def fail_publication(*args, **kwargs):
- raise OSError("Synthetic publication interruption.")
-
- interrupted.setattr(
- workbench_api["saved_results"],
- "_write_prepared_scan_finalization",
- fail_publication,
- )
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )["scan"]
- assert stopped["resultsRecoveryNeeded"] is retry_publication
+ stop_scan(scan, retry_publication)
workbench_api["saved_results"].recover_scan_results(
workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
)
@@ -1435,7 +1331,7 @@ def test_selected_candidate_outcome_removes_its_projected_parent_pending_rows(
workbench_api,
workbench_db,
publication_scan,
- monkeypatch,
+ stop_scan,
disposition,
retry_publication,
parent_review,
@@ -1524,23 +1420,7 @@ def test_selected_candidate_outcome_removes_its_projected_parent_pending_rows(
head.write_text(json.dumps({"checkpoint": checkpoint.name}))
os.utime(head, ns=(300, 300))
originals = {path: path.read_bytes() for path in (result, checkpoint)}
- with monkeypatch.context() as interrupted:
- if retry_publication:
-
- def fail_publication(*args, **kwargs):
- raise OSError("Synthetic publication interruption.")
-
- interrupted.setattr(
- workbench_api["saved_results"],
- "_write_prepared_scan_finalization",
- fail_publication,
- )
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )["scan"]
- assert stopped["resultsRecoveryNeeded"] is retry_publication
+ stop_scan(scan, retry_publication)
workbench_api["saved_results"].recover_scan_results(
workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
)
@@ -1581,7 +1461,7 @@ def test_selected_candidate_outcome_removes_its_projected_parent_pending_rows(
],
)
def test_selected_worker_projection_keeps_links_and_pending_authority(
- workbench_api, workbench_db, publication_scan, monkeypatch, change, retry_publication
+ workbench_api, workbench_db, publication_scan, stop_scan, change, retry_publication
):
scan = publication_scan()
result = add_worker(workbench_db, scan)
@@ -1751,23 +1631,7 @@ def test_selected_worker_projection_keeps_links_and_pending_authority(
originals = {
path: path.read_bytes() for path in [result] + ([checkpoint] if checkpoint else [])
}
- with monkeypatch.context() as interrupted:
- if retry_publication:
-
- def fail_publication(*args, **kwargs):
- raise OSError("Synthetic publication interruption.")
-
- interrupted.setattr(
- workbench_api["saved_results"],
- "_write_prepared_scan_finalization",
- fail_publication,
- )
- stopped = workbench_api["saved_results"].fail_scan(
- workbench_api["_WORKBENCH_DB_CONTEXT"],
- workbench_db,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )["scan"]
- assert stopped["resultsRecoveryNeeded"] is retry_publication
+ stop_scan(scan, retry_publication)
workbench_api["saved_results"].recover_scan_results(
workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)
)
@@ -1849,21 +1713,10 @@ def publish_review_projection(workbench_api, connection, scan, coverage):
)
-def stop_and_recover_projection(workbench_api, connection, scan, monkeypatch, retry):
+def stop_and_recover_projection(workbench_api, connection, scan, stop_scan, retry):
saved = workbench_api["saved_results"]
context = workbench_api["_WORKBENCH_DB_CONTEXT"]
- with monkeypatch.context() as interrupted:
- if retry:
-
- def fail_publication(*args, **kwargs):
- raise OSError("Synthetic publication interruption.")
-
- interrupted.setattr(saved, "_write_prepared_scan_finalization", fail_publication)
- saved.fail_scan(
- context,
- connection,
- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),
- )
+ stop_scan(scan, retry)
saved.recover_scan_results(context, connection, Namespace(scan_id=scan.scan_id))
published = (scan.scan_dir / "coverage.json").read_bytes()
saved.recover_scan_results(context, connection, Namespace(scan_id=scan.scan_id))
@@ -1871,77 +1724,11 @@ def stop_and_recover_projection(workbench_api, connection, scan, monkeypatch, re
return json.loads(published)
- "receipt_ref",
- ["artifacts/review.txt", "artifacts/./review.txt", "scan-relative", "scan-relative-equivalent"],
- ids=["canonical", "equivalent", "scan-relative", "scan-relative-equivalent"],
-)
[email protected]("closure", [False, True], ids=["unchanged", "generic-closure"])
[email protected]("retry", [False, True], ids=["direct", "failed-retry"])
-def test_generic_surface_receipt_union_keeps_each_source_directory(
- workbench_api, workbench_db, publication_scan, monkeypatch, closure, retry, receipt_ref
-):
- scan = publication_scan()
- initial = add_worker(workbench_db, scan)
- worker_id = initial.parent.name
- output = scan.scan_dir / "artifacts" / worker_id / "output"
- output.mkdir(parents=True)
- result = output / "result.json"
- 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 = output / "artifacts" / "review.txt"
- receipt.parent.mkdir()
- receipt.write_text("Synthetic completed source review.\n")
- if receipt_ref.startswith("scan-relative"):
- canonical_ref = receipt.relative_to(scan.scan_dir).as_posix()
- receipt_ref = (
- canonical_ref.replace("artifacts/", "artifacts/./", 1)
- if receipt_ref == "scan-relative-equivalent"
- else canonical_ref
- )
- surface = {
- "id": "review",
- "label": "Completed source review",
- "disposition": "no_issue_found",
- "receiptRefs": [receipt_ref],
- }
- source = {**scan.coverage, "surfaces": [surface], "deferred": []}
- if closure:
- source["resolvedDeferred"] = [{"id": "review", "reason": "Source review completed."}]
- result.write_text(
- json.dumps({"scanId": scan.scan_id, "complete": True, "findings": [], "coverage": source})
- )
- original = result.read_bytes()
- projected = {
- **surface,
- "id": f"{worker_id}-attempt-1-surface-1",
- "receiptRefs": [receipt.relative_to(scan.scan_dir).as_posix()],
- "provenance": {"workerId": worker_id, "attempt": 1, "sourceId": "review"},
- }
- publish_review_projection(
- workbench_api,
- workbench_db,
- scan,
- {
- **scan.coverage,
- "surfaces": [projected],
- "deferred": [],
- "reviews": [{"workerId": worker_id, "attempt": 1, "completeness": "complete"}],
- },
- )
- coverage = stop_and_recover_projection(workbench_api, workbench_db, scan, monkeypatch, retry)
- assert coverage["surfaces"] == [projected]
- assert result.read_bytes() == original
-
-
@pytest.mark.parametrize("spelling", ["unchanged", "equivalent", "scan relative", "omitted"])
@pytest.mark.parametrize("parent", [False, True])
@pytest.mark.parametrize("retry", [False, True])
def test_linked_archived_receipts_preserve_accepted_surface_links(
- workbench_api, workbench_db, publication_scan, monkeypatch, spelling, parent, retry
+ workbench_api, workbench_db, publication_scan, stop_scan, spelling, parent, retry
):
scan = publication_scan()
initial = add_worker(workbench_db, scan)
@@ -2015,7 +1802,7 @@ def test_linked_archived_receipts_preserve_accepted_surface_links(
for name in ("scan-manifest.json", "findings.json", "coverage.json"):
(scan.scan_dir / name).unlink()
originals = {path: path.read_bytes() for path in (result, checkpoint, head)}
- coverage = stop_and_recover_projection(workbench_api, workbench_db, scan, monkeypatch, retry)
+ coverage = stop_and_recover_projection(workbench_api, workbench_db, scan, stop_scan, retry)
pending = [
row for row in coverage["deferred"] if row.get("reason") == "Verify accepted source review."
]
@@ -2025,12 +1812,32 @@ def test_linked_archived_receipts_preserve_accepted_surface_links(
assert all(path.read_bytes() == value for path, value in originals.items())
[email protected]("source", ["worker", "shared", "worker-collision"])
+ ("source", "parent", "closure"),
+ [
+ ("worker", False, False),
+ ("worker", True, False),
+ ("worker", True, True),
+ ("worker-scan-relative", True, False),
+ ("worker-scan-relative", True, True),
+ ("shared", False, False),
+ ("shared", True, False),
+ ("worker-collision", False, False),
+ ("worker-collision", True, False),
+ ],
+)
@pytest.mark.parametrize("equivalent", [False, True])
[email protected]("parent", [False, True])
@pytest.mark.parametrize("retry", [False, True])
-def test_shared_scan_receipts_preserve_existing_context_evidence(
- workbench_api, workbench_db, publication_scan, monkeypatch, source, equivalent, parent, retry
+def test_recovery_projects_receipts_from_their_source_directory(
+ workbench_api,
+ workbench_db,
+ publication_scan,
+ stop_scan,
+ source,
+ parent,
+ closure,
+ equivalent,
+ retry,
):
scan = publication_scan()
initial = add_worker(workbench_db, scan)
@@ -2052,20 +1859,27 @@ def test_shared_scan_receipts_preserve_existing_context_evidence(
receipt = (scan.scan_dir if shared else output) / ref
receipt.parent.mkdir(exist_ok=True, parents=True)
receipt.write_text("Synthetic completed source review.\n")
- source_ref = ref.replace("artifacts/", "artifacts/./") if equivalent else ref
+ if source == "worker-scan-relative":
+ ref = receipt.relative_to(scan.scan_dir).as_posix()
+ source_ref = ref.replace("artifacts/", "artifacts/./", 1) if equivalent else ref
surface = {
"id": "review",
"label": "Completed source review",
"disposition": "no_issue_found",
"receiptRefs": [source_ref],
}
+ worker_coverage = {**scan.coverage, "surfaces": [surface], "deferred": []}
+ if closure:
+ worker_coverage["resolvedDeferred"] = [
+ {"id": "review", "reason": "Source review completed."}
+ ]
result.write_text(
json.dumps(
{
"scanId": scan.scan_id,
"complete": True,
"findings": [],
- "coverage": {**scan.coverage, "surfaces": [surface], "deferred": []},
+ "coverage": worker_coverage,
}
)
)
@@ -2091,7 +1905,7 @@ def test_shared_scan_receipts_preserve_existing_context_evidence(
else:
for name in ("scan-manifest.json", "findings.json", "coverage.json"):
(scan.scan_dir / name).unlink()
- coverage = stop_and_recover_projection(workbench_api, workbench_db, scan, monkeypatch, retry)
+ coverage = stop_and_recover_projection(workbench_api, workbench_db, scan, stop_scan, retry)
assert coverage["surfaces"] == [projected]
assert result.read_bytes() == original
diff --git a/plugins/codex-security/tests/test_plugin_contract.py b/plugins/codex-security/tests/test_plugin_contract.py
index 411d89955..23fb257e5 100644
--- a/plugins/codex-security/tests/test_plugin_contract.py
+++ b/plugins/codex-security/tests/test_plugin_contract.py
@@ -21,20 +21,6 @@ def read_json(path: Path) -> dict[str, Any]:
def test_plugin_manifest_references_existing_assets_and_skills() -> None:
manifest = read_json(PLUGIN_ROOT / ".codex-plugin" / "plugin.json")
- required_fields = {
- "name",
- "version",
- "description",
- "author",
- "homepage",
- "repository",
- "license",
- "keywords",
- "skills",
- "mcpServers",
- "interface",
- }
- assert required_fields <= manifest.keys()
assert manifest["name"] == "codex-security"
assert re.fullmatch(r"\d+\.\d+\.\d+(?:-alpha)?", manifest["version"])
assert isinstance(manifest["description"], str) and manifest["description"].strip()
@@ -51,23 +37,6 @@ def test_plugin_manifest_references_existing_assets_and_skills() -> None:
interface = manifest["interface"]
assert isinstance(interface, dict)
- required_interface_fields = {
- "displayName",
- "shortDescription",
- "longDescription",
- "developerName",
- "category",
- "capabilities",
- "websiteURL",
- "privacyPolicyURL",
- "termsOfServiceURL",
- "defaultPrompt",
- "brandColor",
- "composerIcon",
- "logo",
- "screenshots",
- }
- assert required_interface_fields <= interface.keys()
assert interface["displayName"] == "Codex Security"
for field in (
"shortDescription",
diff --git a/plugins/codex-security/tests/test_saved_deferred_identity.py b/plugins/codex-security/tests/test_saved_deferred_identity.py
index d7e57510a..50d366d68 100644
--- a/plugins/codex-security/tests/test_saved_deferred_identity.py
+++ b/plugins/codex-security/tests/test_saved_deferred_identity.py
@@ -55,6 +55,21 @@ def setup_workbench(monkeypatch, workbench_api):
return run
+def saved_finding(title, **fields):
+ return {
+ "ruleId": "fixture.review",
+ "title": title,
+ "summary": "Synthetic evidence.",
+ "severity": {"level": "low"},
+ "confidence": {"level": "high", "rationale": "Synthetic evidence."},
+ "taxonomy": {"category": "other", "cwe": []},
+ "locations": [{"path": "src/example.py", "startLine": 1}],
+ "remediation": "Complete the review.",
+ "provenance": {"source": "local_plugin"},
+ **fields,
+ }
+
+
def save_worker(root: Path, module, worker_id: str, drafts: list[dict], result: dict):
output = root / worker_id
checkpoints = output / "checkpoints"
@@ -105,18 +120,11 @@ def test_repeated_colliding_variant_keeps_its_assigned_identity(
):
from finalize_scan_contract import _recover_unsealed_findings
- first = {
- "ruleId": "fixture.review",
- "identity": {"anchor": "shared-review"},
- "title": "First independent review",
- "summary": "Synthetic evidence at the first location.",
- "severity": {"level": "low"},
- "confidence": {"level": "high", "rationale": "Synthetic evidence."},
- "taxonomy": {"category": "other", "cwe": []},
- "locations": [{"path": "src/example.py", "startLine": 1}],
- "remediation": "Complete the review.",
- "provenance": {"source": "local_plugin"},
- }
+ first = saved_finding(
+ "First independent review",
+ identity={"anchor": "shared-review"},
+ summary="Synthetic evidence at the first location.",
+ )
second = {
**copy.deepcopy(first),
"title": "Second independent review",
@@ -165,18 +173,10 @@ def test_repeated_colliding_variant_keeps_its_assigned_identity(
def test_legacy_frozen_source_order_parent_context_remains_readable(
tmp_path, saved_results, retain_roles
):
- finding = {
- "ruleId": "fixture.review",
- "identity": {"anchor": "review"},
- "title": "Review",
- "summary": "Synthetic evidence.",
- "severity": {"level": "low"},
- "confidence": {"level": "high", "rationale": "Synthetic evidence."},
- "taxonomy": {"category": "other", "cwe": []},
- "locations": [{"path": "src/example.py", "startLine": 1}],
- "remediation": "Complete the review.",
- "provenance": {"source": "local_plugin"},
- }
+ finding = saved_finding(
+ "Review",
+ identity={"anchor": "review"},
+ )
write_saved_parent(tmp_path, saved_draft("identity-scan", findings=[finding]), 100)
first = recover(tmp_path, saved_results, [])
legacy_sources = dict(first[0]["scan"]["preservedSources"])
@@ -217,17 +217,10 @@ def test_malformed_coverage_does_not_interrupt_saved_evidence(
def test_missing_parent_identity_is_identical_on_frozen_replay(tmp_path: Path, saved_results):
- finding = {
- "ruleId": "fixture.review",
- "title": "Synthetic review finding",
- "summary": "Retain the saved result.",
- "severity": {"level": "low"},
- "confidence": {"level": "high", "rationale": "Synthetic evidence."},
- "taxonomy": {"category": "other", "cwe": []},
- "locations": [{"path": "src/example.py", "startLine": 1}],
- "remediation": "Complete the review.",
- "provenance": {"source": "local_plugin"},
- }
+ finding = saved_finding(
+ "Synthetic review finding",
+ summary="Retain the saved result.",
+ )
draft = saved_draft("identity-scan", findings=[finding])
write_saved_parent(tmp_path, draft, 100)
first = recover(tmp_path, saved_results, [])
@@ -247,17 +240,10 @@ def test_missing_parent_identity_reuses_established_checkpoint(
from finalize_scan_contract import _recover_unsealed_findings
from workbench_test_support import saved_binding
- finding = {
- "ruleId": "fixture.review",
- "title": "Synthetic review finding",
- "summary": "Retain the saved result.",
- "severity": {"level": "low"},
- "confidence": {"level": "high", "rationale": "Synthetic evidence."},
- "taxonomy": {"category": "other", "cwe": []},
- "locations": [{"path": "src/example.py", "startLine": 1}],
- "remediation": "Complete the review.",
- "provenance": {"source": "local_plugin"},
- }
+ finding = saved_finding(
+ "Synthetic review finding",
+ summary="Retain the saved result.",
+ )
checkpoint = write_checkpoint(
tmp_path / "checkpoints",
saved_draft(
@@ -298,9 +284,11 @@ def test_missing_parent_identity_reuses_established_checkpoint(
@pytest.mark.parametrize("artifacts", ["omitted", None, []])
@pytest.mark.parametrize("outcome", ["failed", "canceled"])
-def test_empty_artifact_envelope_remains_a_recoverable_draft(tmp_path: Path, artifacts, outcome):
- state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path)
- accepted_standard_worker(state, codex_home, scan_dir, scan_id)
+def test_empty_artifact_envelope_remains_a_recoverable_draft(
+ tmp_path: Path, setup_workbench, artifacts, outcome
+):
+ state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path, run=setup_workbench)
+ accepted_standard_worker(state, codex_home, scan_dir, scan_id, run=setup_workbench)
write_saved_parent(
scan_dir,
saved_draft(scan_id, deferred=[{"id": "review", "reason": "Review remains."}]),
@@ -359,21 +347,17 @@ def test_empty_artifact_envelope_remains_a_recoverable_draft(tmp_path: Path, art
assert manifest_path.read_bytes() == original
-def test_stopped_deep_recovery_uses_host_mode_for_terminal_replacement(tmp_path: Path):
- state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path)
- accepted_standard_worker(state, codex_home, scan_dir, scan_id)
- first = {
- "ruleId": "fixture.review",
- "identity": {"anchor": "first-review"},
- "title": "First review",
- "summary": "Earlier synthetic evidence.",
- "severity": {"level": "low"},
- "confidence": {"level": "high", "rationale": "Synthetic evidence."},
- "taxonomy": {"category": "other", "cwe": []},
- "locations": [{"path": "app.py", "startLine": 1}],
- "remediation": "Complete the review.",
- "provenance": {"source": "local_plugin"},
- }
+def test_stopped_deep_recovery_uses_host_mode_for_terminal_replacement(
+ tmp_path: Path, setup_workbench
+):
+ state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path, run=setup_workbench)
+ accepted_standard_worker(state, codex_home, scan_dir, scan_id, run=setup_workbench)
+ first = saved_finding(
+ "First review",
+ identity={"anchor": "first-review"},
+ summary="Earlier synthetic evidence.",
+ locations=[{"path": "app.py", "startLine": 1}],
+ )
write_saved_parent(scan_dir, saved_draft(scan_id, findings=[first], complete=True), 100)
revised = {**first, "title": "Revised review", "summary": "Revised synthetic evidence."}
del revised["identity"]
@@ -575,9 +559,11 @@ def test_accepted_generic_update_keeps_its_id_in_frozen_recovery(tmp_path: Path,
assert all(path.read_bytes() == contents for path, contents in originals.items())
-def test_unsafe_deferred_update_keeps_the_earlier_valid_path(tmp_path: Path) -> None:
- state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path)
- _, result = accepted_standard_worker(state, codex_home, scan_dir, scan_id)
+def test_unsafe_deferred_update_keeps_the_earlier_valid_path(
+ tmp_path: Path, setup_workbench
+) -> None:
+ state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path, run=setup_workbench)
+ _, result = accepted_standard_worker(state, codex_home, scan_dir, scan_id, run=setup_workbench)
original = {"id": "review", "reason": "Review remains.", "paths": ["app.py"]}
checkpoint = write_checkpoint(
result.parent / "checkpoints", saved_draft(scan_id, deferred=[original])
@@ -1010,17 +996,11 @@ def test_legacy_summary_stays_pending_after_an_explicit_closure(
def test_worker_local_candidates_remain_distinct_on_frozen_recovery(
tmp_path, saved_results, same_title
):
- finding = {
- "ruleId": "fixture.review",
- "title": "First review",
- "summary": "Retain the saved result.",
- "severity": {"level": "low"},
- "confidence": {"level": "high", "rationale": "Synthetic evidence."},
- "taxonomy": {"category": "other", "cwe": []},
- "locations": [{"path": "src/example.py", "startLine": 1}],
- "remediation": "Complete the review.",
- "provenance": {"source": "local_plugin", "candidateId": "candidate-1"},
- }
+ finding = saved_finding(
+ "First review",
+ summary="Retain the saved result.",
+ provenance={"source": "local_plugin", "candidateId": "candidate-1"},
+ )
workers = [
save_worker(
tmp_path,
@@ -1051,19 +1031,12 @@ def test_recovery_finalization_coalesces_repeated_authored_reports(
):
from finalize_scan_contract import _recover_unsealed_findings
- first = {
- "ruleId": "fixture.review",
- "title": "Existing report",
- "summary": "Synthetic evidence.",
- "severity": {"level": "low"},
- "confidence": {"level": "high", "rationale": "Synthetic evidence."},
- "taxonomy": {"category": "other", "cwe": []},
- "locations": [{"path": "src/example.py", "startLine": 1}],
- "remediation": "Complete the review.",
- "provenance": {"source": "local_plugin", "candidateId": "candidate-1"},
- "identity": {"anchor": "shared"},
- "extensions": {"reportId": "report-1"},
- }
+ first = saved_finding(
+ "Existing report",
+ provenance={"source": "local_plugin", "candidateId": "candidate-1"},
+ identity={"anchor": "shared"},
+ extensions={"reportId": "report-1"},
+ )
latest = (
{
**first,
@@ -1094,18 +1067,12 @@ def test_recovery_finalization_preserves_distinct_reports_at_a_revised_location(
):
from finalize_scan_contract import _recover_unsealed_findings
- first = {
- "ruleId": "fixture.review",
- "title": "Existing report",
- "summary": "Synthetic evidence.",
- "severity": {"level": "low"},
- "confidence": {"level": "high", "rationale": "Synthetic evidence."},
- "taxonomy": {"category": "other", "cwe": []},
- "locations": [{"path": "src/example.py", "startLine": 2}],
- "remediation": "Complete the review.",
- "provenance": {"source": "local_plugin", "candidateId": "candidate-1"},
- "identity": {"anchor": "shared"},
- }
+ first = saved_finding(
+ "Existing report",
+ locations=[{"path": "src/example.py", "startLine": 2}],
+ provenance={"source": "local_plugin", "candidateId": "candidate-1"},
+ identity={"anchor": "shared"},
+ )
independent = {
**first,
"title": "Independent report",
@@ -1167,18 +1134,11 @@ def test_recovery_finalization_preserves_distinct_reports_at_a_revised_location(
def test_worker_report_metadata_enrichment_matches_published_identity(
tmp_path, saved_results, metadata, identifier
):
- first = {
- "ruleId": "fixture.review",
- "title": "Synthetic report",
- "summary": "Retain the saved result.",
- "severity": {"level": "low"},
- "confidence": {"level": "high", "rationale": "Synthetic evidence."},
- "taxonomy": {"category": "other", "cwe": []},
- "locations": [{"path": "src/example.py", "startLine": 1}],
- "remediation": "Complete the review.",
- "provenance": {"source": "local_plugin"},
- "extensions": {identifier: "report-1"},
- }
+ first = saved_finding(
+ "Synthetic report",
+ summary="Retain the saved result.",
+ extensions={identifier: "report-1"},
+ )
second = {**first, metadata: {**first[metadata], "candidateId": "candidate-1"}}
drafts = [saved_draft("identity-scan", findings=[value]) for value in (first, second)]
worker = save_worker(tmp_path, saved_results, "reviewer", drafts, drafts[1])
@@ -1197,17 +1157,7 @@ def test_worker_report_metadata_enrichment_matches_published_identity(
def test_parent_represents_worker_versions_before_candidate_enrichment(
tmp_path, saved_results, metadata
):
- first = {
- "ruleId": "fixture.review",
- "title": "Synthetic review",
- "summary": "Synthetic evidence.",
- "severity": {"level": "low"},
- "confidence": {"level": "high", "rationale": "Synthetic evidence."},
- "taxonomy": {"category": "other", "cwe": []},
- "locations": [{"path": "src/example.py", "startLine": 1}],
- "remediation": "Complete the review.",
- "provenance": {"source": "local_plugin"},
- }
+ first = saved_finding("Synthetic review")
latest = {**first, metadata: {**first.get(metadata, {}), "candidateId": "candidate-1"}}
reduced = {
**latest,
@@ -1264,18 +1214,12 @@ def test_parent_represents_worker_versions_before_candidate_enrichment(
def test_worker_revision_keeps_assigned_sibling_on_frozen_recovery(
tmp_path, saved_results, reversed_rows, revised_identity, preserve_reduction
):
- first = {
- "ruleId": "fixture.review",
- "title": "Synthetic first review",
- "summary": "Original first evidence.",
- "identity": {"anchor": "shared-review", "instance": "first"},
- "severity": {"level": "low"},
- "confidence": {"level": "high", "rationale": "Synthetic evidence."},
- "taxonomy": {"category": "other", "cwe": []},
- "locations": [{"path": "src/example.py", "startLine": 1}],
- "remediation": "Complete the review.",
- "provenance": {"source": "local_plugin", "candidateId": "candidate-1"},
- }
+ first = saved_finding(
+ "Synthetic first review",
+ summary="Original first evidence.",
+ identity={"anchor": "shared-review", "instance": "first"},
+ provenance={"source": "local_plugin", "candidateId": "candidate-1"},
+ )
sibling = {
**{key: value for key, value in first.items() if key != "identity"},
"title": "Independent review",
@@ -1336,18 +1280,10 @@ def test_claimed_report_does_not_resolve_an_independent_recovery_match(
):
from finalize_scan_contract import _recover_unsealed_findings
- first = {
- "ruleId": "fixture.review",
- "title": "First independent report",
- "summary": "Synthetic evidence.",
- "severity": {"level": "low"},
- "confidence": {"level": "high", "rationale": "Synthetic evidence."},
- "taxonomy": {"category": "other", "cwe": []},
- "locations": [{"path": "src/example.py", "startLine": 1}],
- "remediation": "Complete the review.",
- "provenance": {"source": "local_plugin"},
- "extensions": {identifier: "report-1"},
- }
+ first = saved_finding(
+ "First independent report",
+ extensions={identifier: "report-1"},
+ )
independent = {
**{key: value for key, value in first.items() if key != "extensions"},
"title": "Second independent report",
@@ -1399,18 +1335,14 @@ def test_claimed_report_does_not_resolve_an_independent_recovery_match(
def test_latest_eligible_worker_revision_survives_out_of_scope_result(
tmp_path, saved_results, monkeypatch, reverse_checkpoints
):
- first = {
- "ruleId": "fixture.review",
- "title": "Synthetic review",
- "summary": "Older assessment 0",
- "severity": {"level": "low"},
- "confidence": {"level": "high", "rationale": "Synthetic fixture."},
- "taxonomy": {"category": "other", "cwe": []},
- "locations": [{"path": "src/example.py", "startLine": 1}],
- "remediation": "Complete review.",
- "provenance": {"source": "local_plugin", "candidateId": "candidate-1"},
- "identity": {"anchor": "stable"},
- }
+ first = saved_finding(
+ "Synthetic review",
+ summary="Older assessment 0",
+ confidence={"level": "high", "rationale": "Synthetic fixture."},
+ remediation="Complete review.",
+ provenance={"source": "local_plugin", "candidateId": "candidate-1"},
+ identity={"anchor": "stable"},
+ )
latest = {
**first,
"summary": "Confirmed newer high severity assessment",
diff --git a/plugins/codex-security/tests/test_saved_deferred_unicode.py b/plugins/codex-security/tests/test_saved_deferred_unicode.py
index c40cb2345..8cf921e7d 100644
--- a/plugins/codex-security/tests/test_saved_deferred_unicode.py
+++ b/plugins/codex-security/tests/test_saved_deferred_unicode.py
@@ -13,12 +13,16 @@ from workbench_test_support import fail_deep_scan, saved_draft, scan_command, wr
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
import workbench_saved_results as saved
+SURROGATE_IDS = (
+ ("\ud800", "deferred-7b02db7069064f21"),
+ ("\udfff", "deferred-042d3e8a3549a045"),
+)
+
@pytest.mark.parametrize(
("reason", "identity"),
[
- pytest.param("\ud800", "deferred-7b02db7069064f21", id="high-surrogate"),
- pytest.param("\udfff", "deferred-042d3e8a3549a045", id="low-surrogate"),
+ *SURROGATE_IDS,
pytest.param("\U0001f600", "deferred-6403b70908c737af", id="emoji"),
pytest.param("\\ud800", "deferred-6e08d26c2848d312", id="literal-escape"),
pytest.param("é", "deferred-397a18fac04fedaa", id="accent"),
@@ -47,24 +51,18 @@ def test_recovery_accepts_unicode_ids_from_legacy_writers(
assert replay[2] == result[2]
- ("reason", "identity"),
- [
- pytest.param("\ud800", "deferred-7b02db7069064f21", id="high-surrogate"),
- pytest.param("\udfff", "deferred-042d3e8a3549a045", id="low-surrogate"),
- ],
-)
-def test_stopped_publication_recovers_after_unicode_review_is_closed(
- tmp_path: Path, reason: str, identity: str
-) -> None:
+def test_stopped_publication_recovers_after_unicode_reviews_are_closed(tmp_path: Path) -> None:
state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path)
- _, result = accepted_standard_worker(state, codex_home, scan_dir, scan_id)
- pending = saved_draft(scan_id, deferred=[{"reason": reason, "paths": ["src/example.py"]}])
+ worker_id, result = accepted_standard_worker(state, codex_home, scan_dir, scan_id)
+ pending = saved_draft(
+ scan_id,
+ deferred=[{"reason": reason, "paths": ["src/example.py"]} for reason, _ in SURROGATE_IDS],
+ )
checkpoint = write_checkpoint(result.parent / "checkpoints", pending)
os.utime(checkpoint, ns=(100, 100))
closed = saved_draft(
scan_id,
- closures=[{"id": identity, "reason": "Review completed."}],
+ closures=[{"id": identity, "reason": "Review completed."} for _, identity in SURROGATE_IDS],
complete=True,
)
result.write_text(json.dumps(closed), encoding="utf-8")
@@ -73,6 +71,9 @@ def test_stopped_publication_recovers_after_unicode_review_is_closed(
fail_deep_scan(state, codex_home, scan_id, deep_status="failed")
coverage = json.loads((scan_dir / "coverage.json").read_text(encoding="utf-8"))
assert {row["id"] for row in coverage["deferred"]} == {"scan-stopped"}
+ assert {row["id"] for row in coverage["resolvedDeferred"]} == {
+ f"{worker_id}-attempt-1-resolved-{identity}" for _, identity in SURROGATE_IDS
+ }
manifest = (scan_dir / "scan-manifest.json").read_bytes()
recovered = scan_command(state, "recover-scan-results", scan_id, environment=environment)
assert recovered["scan"]["resultsRecoveryNeeded"] is False
diff --git a/plugins/codex-security/tests/test_scan_contract_examples.py b/plugins/codex-security/tests/test_scan_contract_examples.py
index 12ab94e26..0e9332637 100644
--- a/plugins/codex-security/tests/test_scan_contract_examples.py
+++ b/plugins/codex-security/tests/test_scan_contract_examples.py
@@ -175,27 +175,17 @@ class ScanContractExamplesTest(unittest.TestCase):
)
self.assertFalse(validator.is_valid({"candidates": [], "merges": []}))
- def test_documents_refer_to_one_scan(self) -> None:
+ def test_manifest_identifies_and_hashes_the_example_documents(self) -> None:
scan = self.manifest["scan"]
self.assertEqual(scan["id"], self.findings["scanId"])
self.assertEqual(scan["id"], self.coverage["scanId"])
self.assertEqual(scan["findingsRef"], "findings.json")
self.assertEqual(scan["coverageRef"], "coverage.json")
-
- def test_manifest_lists_canonical_documents(self) -> None:
- paths = {artifact["path"] for artifact in self.manifest["scan"]["artifacts"]}
self.assertEqual(
- paths,
- {
- "coverage.json",
- "findings.json",
- },
+ {item["path"] for item in scan["artifacts"]}, {"coverage.json", "findings.json"}
)
-
- def test_manifest_artifacts_exist_with_recorded_hashes(self) -> None:
- for artifact in self.manifest["scan"]["artifacts"]:
+ for artifact in scan["artifacts"]:
path = EXAMPLE_DIR / artifact["path"]
- self.assertTrue(path.is_file(), artifact["path"])
self.assertEqual(hashlib.sha256(path.read_bytes()).hexdigest(), artifact["sha256"])
def test_manifest_requires_snapshot_signal_for_target_kind(self) -> None:
diff --git a/plugins/codex-security/tests/test_validate_scan_contract.py b/plugins/codex-security/tests/test_validate_scan_contract.py
index d3ea78bd7..70eb59819 100644
--- a/plugins/codex-security/tests/test_validate_scan_contract.py
+++ b/plugins/codex-security/tests/test_validate_scan_contract.py
@@ -1,11 +1,9 @@
from __future__ import annotations
-import io
import json
import subprocess
import sys
import unittest
-from contextlib import redirect_stderr, redirect_stdout
from pathlib import Path
from workbench_test_support import ScanFixtureTestCase, load_script
@@ -36,15 +34,7 @@ class ValidateScanContractTest(ScanFixtureTestCase):
findings["findings"][0]["attackPath"] = {
"dataFlow": {"evidenceRefs": ["legacy-missing-evidence"]}
}
- self.write_json("findings.json", findings)
- manifest = self.read_json("scan-manifest.json")
- findings_artifact = next(
- artifact
- for artifact in manifest["scan"]["artifacts"]
- if artifact["path"] == "findings.json"
- )
- findings_artifact["sha256"] = self.sha256_file("findings.json")
- self.write_json("scan-manifest.json", manifest)
+ self.rewrite_sealed_artifact("findings.json", findings)
validated = VALIDATOR.validate_contract(self.scan_dir)
@@ -60,24 +50,14 @@ class ValidateScanContractTest(ScanFixtureTestCase):
self.assertEqual(len(validated["manifest"]["metadata"]), 16 * 1024 * 1024)
def test_rejects_unsafe_values_in_sealed_finding_extensions(self) -> None:
- original_manifest = (self.scan_dir / "scan-manifest.json").read_bytes()
- original_findings = (self.scan_dir / "findings.json").read_bytes()
+ findings = self.read_json("findings.json")
for value, reason in (
(1e20, "unsafe integer-valued JSON numbers"),
("bad-" + chr(0xD800), "well-formed Unicode"),
):
with self.subTest(reason=reason):
- (self.scan_dir / "scan-manifest.json").write_bytes(original_manifest)
- (self.scan_dir / "findings.json").write_bytes(original_findings)
- findings = self.read_json("findings.json")
findings["findings"][0].setdefault("extensions", {})["unsafe"] = value
- self.write_json("findings.json", findings)
- manifest = self.read_json("scan-manifest.json")
- for artifact in manifest["scan"]["artifacts"]:
- if artifact["path"] == "findings.json":
- artifact["sha256"] = self.sha256_file("findings.json")
- break
- self.write_json("scan-manifest.json", manifest)
+ self.rewrite_sealed_artifact("findings.json", findings)
with self.assertRaisesRegex(VALIDATOR.FINALIZER.ContractError, reason):
VALIDATOR.validate_contract(self.scan_dir)
@@ -97,22 +77,6 @@ class ValidateScanContractTest(ScanFixtureTestCase):
with self.assertRaisesRegex(VALIDATOR.FINALIZER.ContractError, "sealed"):
VALIDATOR.validate_contract(self.scan_dir)
- def test_cli_prints_machine_readable_receipt(self) -> None:
- stdout = io.StringIO()
- stderr = io.StringIO()
-
- with redirect_stdout(stdout), redirect_stderr(stderr):
- exit_code = VALIDATOR.main(["--scan-dir", str(self.scan_dir)])
-
- receipt = json.loads(stdout.getvalue())
- self.assertEqual(exit_code, 0)
- self.assertEqual(receipt["status"], "valid")
- self.assertEqual(
- receipt["manifestPath"],
- str(self.scan_dir.resolve() / "scan-manifest.json"),
- )
- self.assertEqual(stderr.getvalue(), "")
-
def test_finalizer_then_validator_cli_smoke(self) -> None:
(self.scan_dir / "report.md").unlink()
scan_dir = self.scan_dir.resolve()
@@ -134,7 +98,10 @@ class ValidateScanContractTest(ScanFixtureTestCase):
text=True,
)
self.assertEqual(validated.returncode, 0, validated.stderr)
- self.assertEqual(json.loads(validated.stdout)["status"], "valid")
+ receipt = json.loads(validated.stdout)
+ self.assertEqual(receipt["status"], "valid")
+ self.assertEqual(receipt["manifestPath"], str(scan_dir / "scan-manifest.json"))
+ self.assertEqual(validated.stderr, "")
if __name__ == "__main__":
diff --git a/plugins/codex-security/tests/test_validate_tracking_source.py b/plugins/codex-security/tests/test_validate_tracking_source.py
index 0c903a4df..2faf31401 100644
--- a/plugins/codex-security/tests/test_validate_tracking_source.py
+++ b/plugins/codex-security/tests/test_validate_tracking_source.py
@@ -27,30 +27,6 @@ class ValidateTrackingSourceTest(ScanFixtureTestCase):
validator = VALIDATOR
example_scan = EXAMPLE_SCAN
- def rewrite_findings_and_seal(self, findings: dict[str, object]) -> None:
- self.write_json("findings.json", findings)
- manifest = self.read_json("scan-manifest.json")
- findings_bytes = (self.scan_dir / "findings.json").read_bytes()
- artifact = next(
- item
- for item in manifest["scan"]["artifacts"]
- if item["path"] == manifest["scan"]["findingsRef"]
- )
- artifact["sha256"] = hashlib.sha256(findings_bytes).hexdigest()
- self.write_json("scan-manifest.json", manifest)
-
- def rewrite_coverage_and_seal(self, coverage: dict[str, object]) -> None:
- self.write_json("coverage.json", coverage)
- manifest = self.read_json("scan-manifest.json")
- coverage_bytes = (self.scan_dir / "coverage.json").read_bytes()
- artifact = next(
- item
- for item in manifest["scan"]["artifacts"]
- if item["path"] == manifest["scan"]["coverageRef"]
- )
- artifact["sha256"] = hashlib.sha256(coverage_bytes).hexdigest()
- self.write_json("scan-manifest.json", manifest)
-
def test_tampered_findings_are_rejected(self) -> None:
findings_path = self.scan_dir / "findings.json"
findings_path.write_text(
@@ -82,7 +58,7 @@ class ValidateTrackingSourceTest(ScanFixtureTestCase):
def test_digest_matching_invalid_coverage_is_rejected(self) -> None:
coverage = self.read_json("coverage.json")
coverage["scanId"] = "wrong-scan-id"
- self.rewrite_coverage_and_seal(coverage)
+ self.rewrite_sealed_artifact("coverage.json", coverage)
with self.assertRaisesRegex(
VALIDATOR.FINALIZER.ContractError,
@@ -114,28 +90,7 @@ class ValidateTrackingSourceTest(ScanFixtureTestCase):
with self.assertRaisesRegex(VALIDATOR.FINALIZER.ContractError, "scan-manifest.json"):
VALIDATOR.validate_source(report_dir)
- def test_multiple_findings_can_be_listed_or_selected(self) -> None:
- findings = self.read_json("findings.json")
- sibling = copy.deepcopy(findings["findings"][0])
- sibling["identity"]["instance"] = "second-archive-write"
- sibling.pop("findingId")
- sibling.pop("occurrenceId")
- sibling.pop("fingerprints")
- findings["findings"].append(sibling)
- manifest = self.read_json("scan-manifest.json")
- VALIDATOR.FINALIZER._populate_unsealed_finding_identities(manifest, findings)
- self.rewrite_findings_and_seal(findings)
-
- listed = VALIDATOR.validate_source(self.scan_dir)
- self.assertEqual(len(listed), 2)
-
- selected = VALIDATOR.validate_source(
- self.scan_dir,
- fingerprint=sibling["fingerprints"]["primary"],
- )
- self.assertEqual(selected[0]["findingId"], sibling["findingId"])
-
- def test_lists_twenty_five_findings_for_a_max_size_batch(self) -> None:
+ def test_max_size_batch_can_be_listed_or_selected_by_fingerprint(self) -> None:
findings = self.read_json("findings.json")
template = findings["findings"][0]
for index in range(1, 25):
@@ -148,7 +103,7 @@ class ValidateTrackingSourceTest(ScanFixtureTestCase):
manifest = self.read_json("scan-manifest.json")
VALIDATOR.FINALIZER._populate_unsealed_finding_identities(manifest, findings)
- self.rewrite_findings_and_seal(findings)
+ self.rewrite_sealed_artifact("findings.json", findings)
listed = VALIDATOR.validate_source(self.scan_dir)
finding_ids = [finding["findingId"] for finding in listed]
@@ -156,6 +111,12 @@ class ValidateTrackingSourceTest(ScanFixtureTestCase):
self.assertEqual(len(finding_ids), 25)
self.assertEqual(len(set(finding_ids)), 25)
+ finding = findings["findings"][-1]
+ selected = VALIDATOR.validate_source(
+ self.scan_dir, fingerprint=finding["fingerprints"]["primary"]
+ )
+ self.assertEqual([item["findingId"] for item in selected], [finding["findingId"]])
+
if __name__ == "__main__":
unittest.main()
diff --git a/plugins/codex-security/tests/test_workbench_archive.py b/plugins/codex-security/tests/test_workbench_archive.py
index a053e4445..3e5456ce7 100644
--- a/plugins/codex-security/tests/test_workbench_archive.py
+++ b/plugins/codex-security/tests/test_workbench_archive.py
@@ -319,22 +319,18 @@ def test_interrupted_commit_keeps_files_at_their_saved_paths(previous_scan, monk
assert list(output.iterdir()) == ([] if committed else [output / "report.md"])
[email protected]("layout", ["same", "nested", "alias"])
-def test_registration_does_not_archive_its_active_workbench_state(tmp_path, layout):
+def test_registration_does_not_archive_state_reached_through_an_external_alias(tmp_path):
target = tmp_path / "target"
target.mkdir()
- (target / "fixture.py").write_text("preserved source\n")
output = tmp_path / "output"
output.mkdir(mode=0o700)
- state = output if layout == "same" else output / "state"
- state.mkdir(mode=0o700, exist_ok=True)
- if layout == "alias":
- alias = tmp_path / "state-alias"
- try:
- alias.symlink_to(state, target_is_directory=True)
- except OSError:
- pytest.skip("Directory symlinks are unavailable")
- state = alias
+ nested_state = output / "state"
+ nested_state.mkdir(mode=0o700)
+ state = tmp_path / "state-alias"
+ try:
+ state.symlink_to(nested_state, target_is_directory=True)
+ except OSError:
+ pytest.skip("Directory symlinks are unavailable")
saved = tmp_path / "saved"
saved.mkdir(mode=0o700)
previous = register(state, target, saved)
diff --git a/plugins/codex-security/tests/test_workbench_cli.py b/plugins/codex-security/tests/test_workbench_cli.py
index c68c5c25f..9c95cc864 100644
--- a/plugins/codex-security/tests/test_workbench_cli.py
+++ b/plugins/codex-security/tests/test_workbench_cli.py
@@ -12,42 +12,7 @@ from workbench_test_support import SCRIPT, run_workbench
WORKSPACE_ID = "00000000-0000-4000-8000-000000000001"
SCAN_ID = "00000000-0000-4000-8000-000000000002"
-CONTEXT_COMMANDS = [
- ["create-workspace", "--workspace-id", WORKSPACE_ID],
- [
- "save-workspace",
- "--workspace-id",
- WORKSPACE_ID,
- "--target-path",
- ".",
- "--scope",
- ".",
- "--mode",
- "standard",
- ],
- [
- "start-prompt-only-scan",
- "--thread-id",
- "thread-context",
- "--target-path",
- ".",
- "--scope",
- ".",
- "--mode",
- "standard",
- ],
- [
- "start-headless-standard-scan",
- "--thread-id",
- "thread-context",
- "--target-path",
- ".",
- "--scope",
- ".",
- ],
- ["begin-deep-scan", "--thread-id", "thread-context", "--target-path", "."],
- ["update-scan-context", "--scan-id", SCAN_ID, "--workspace-id", WORKSPACE_ID],
-]
+CREATE_WORKSPACE = ["create-workspace", "--workspace-id", WORKSPACE_ID]
STDIN_FLAGS = [
["--user-context-stdin"],
["--user-context-s"],
@@ -56,13 +21,12 @@ STDIN_FLAGS = [
]
[email protected]("command", CONTEXT_COMMANDS, ids=lambda args: args[0])
@pytest.mark.parametrize("flags", STDIN_FLAGS)
@pytest.mark.parametrize("payload", [b"", " first\r\nsecond\rcafé ".encode()])
-def test_context_stdin_is_resolved_once(workbench_api, monkeypatch, command, flags, payload):
+def test_context_stdin_is_resolved_once(workbench_api, monkeypatch, flags, payload):
stdin = Mock(buffer=Mock(read=Mock(return_value=payload)))
monkeypatch.setattr(sys, "stdin", stdin)
- monkeypatch.setattr(sys, "argv", ["workbench_db.py", *command, *flags])
+ monkeypatch.setattr(sys, "argv", ["workbench_db.py", *CREATE_WORKSPACE, *flags])
args = workbench_api["parse_args"]("workbench")
@@ -74,7 +38,6 @@ def test_context_stdin_is_resolved_once(workbench_api, monkeypatch, command, fla
stdin.read.assert_not_called()
[email protected]("command", CONTEXT_COMMANDS, ids=lambda args: args[0])
@pytest.mark.parametrize(
"flags",
[
@@ -85,9 +48,10 @@ def test_context_stdin_is_resolved_once(workbench_api, monkeypatch, command, fla
["--user-context=inline", "--user-context-s", "--user-context-stdin"],
],
)
-def test_context_conflicts_do_not_read_stdin(workbench_api, monkeypatch, capsys, command, flags):
+def test_context_conflicts_do_not_read_stdin(workbench_api, monkeypatch, capsys, flags):
stdin = Mock()
monkeypatch.setattr(sys, "stdin", stdin)
+ command = ["update-scan-context", "--scan-id", SCAN_ID, "--workspace-id", WORKSPACE_ID]
monkeypatch.setattr(sys, "argv", ["workbench_db.py", *command, *flags])
with pytest.raises(SystemExit) as error:
@@ -106,7 +70,7 @@ def test_context_stdin_is_not_read_before_argument_parsing(
stdin = Mock()
monkeypatch.setattr(sys, "stdin", stdin)
monkeypatch.setattr(
- sys, "argv", ["workbench_db.py", *CONTEXT_COMMANDS[0], "--user-context-stdin", option]
+ sys, "argv", ["workbench_db.py", *CREATE_WORKSPACE, "--user-context-stdin", option]
)
with pytest.raises(SystemExit) as error:
@@ -120,7 +84,7 @@ def test_context_stdin_is_not_read_before_argument_parsing(
# use the shared workbench declaration exercised by the parser tests above.
@pytest.mark.parametrize(
"command",
- [args for args in CONTEXT_COMMANDS if args[0] in {"create-workspace", "begin-deep-scan"}],
+ [CREATE_WORKSPACE, ["begin-deep-scan", "--thread-id", "thread-context", "--target-path", "."]],
ids=lambda args: args[0],
)
@pytest.mark.parametrize(
@@ -149,7 +113,7 @@ def test_invalid_context_does_not_allocate_database(tmp_path, command, flags, pa
def test_context_read_failure_precedes_database_connection(workbench_api, monkeypatch):
stdin = Mock(buffer=Mock(read=Mock(side_effect=ValueError("read of closed file"))))
monkeypatch.setattr(sys, "stdin", stdin)
- monkeypatch.setattr(sys, "argv", ["workbench_db.py", *CONTEXT_COMMANDS[0], "--user-context-s"])
+ monkeypatch.setattr(sys, "argv", ["workbench_db.py", *CREATE_WORKSPACE, "--user-context-s"])
connect = Mock()
monkeypatch.setitem(workbench_api["main"].__globals__, "connect", connect)
diff --git a/plugins/codex-security/tests/test_workbench_db.py b/plugins/codex-security/tests/test_workbench_db.py
index 729b4013d..d92a888c2 100644
--- a/plugins/codex-security/tests/test_workbench_db.py
+++ b/plugins/codex-security/tests/test_workbench_db.py
@@ -3180,54 +3180,20 @@ def commit_source_fixture(target: Path, source: bytes) -> str:
).stdout.strip()
- "separator", ["\f", "\v", "\x1c", "\x1d", "\x1e", "\x85", "\u2028", "\u2029"]
-)
-def test_source_excerpt_breaks_lines_only_at_newlines(tmp_path: Path, separator: str) -> None:
- namespace = runpy.run_path(str(SCRIPT), run_name="codex_security_workbench_db")
- finding_source_excerpt = namespace["finding_source_excerpt"]
+def test_source_excerpt_breaks_lines_only_at_newlines(tmp_path: Path, workbench_api) -> None:
+ separators = "\f\v\x1c\x1d\x1e\x85\u2028\u2029"
+ source = f"one\r\ntwo{separators}\rthree\nfour\r\nfive\rsix\nseven\r\neight\rnine\n"
target = tmp_path / "target"
- revision = commit_source_fixture(
- target,
- "".join(
- f"source line {line_number}{separator if line_number == 2 else ''}\n"
- for line_number in range(1, 11)
- ).encode(),
- )
+ revision = commit_source_fixture(target, source.encode())
- excerpt = finding_source_excerpt(
+ excerpt = workbench_api["finding_source_excerpt"](
{"target_revision": revision, "target_snapshot_digest": None, "diff_target_kind": None},
target,
[{"path": "README.md", "startLine": 5, "endLine": 5}],
["."],
)
- assert excerpt == "\n".join(
- f"{line_number} source line {line_number}{separator if line_number == 2 else ''}"
- for line_number in range(2, 9)
- )
-
-
[email protected]("line_ending", ["\n", "\r\n", "\r"])
-def test_source_excerpt_numbers_standard_line_endings(tmp_path: Path, line_ending: str) -> None:
- namespace = runpy.run_path(str(SCRIPT), run_name="codex_security_workbench_db")
- finding_source_excerpt = namespace["finding_source_excerpt"]
- target = tmp_path / "target"
- revision = commit_source_fixture(
- target,
- "".join(f"source line {line_number}{line_ending}" for line_number in range(1, 11)).encode(),
- )
-
- excerpt = finding_source_excerpt(
- {"target_revision": revision, "target_snapshot_digest": None, "diff_target_kind": None},
- target,
- [{"path": "README.md", "startLine": 5, "endLine": 5}],
- ["."],
- )
-
- assert excerpt == "\n".join(
- f"{line_number} source line {line_number}" for line_number in range(2, 9)
- )
+ assert excerpt == f"2 two{separators}\n3 three\n4 four\n5 five\n6 six\n7 seven\n8 eight"
@pytest.mark.parametrize(
@@ -3244,10 +3210,9 @@ def test_source_excerpt_numbers_standard_line_endings(tmp_path: Path, line_endin
],
)
def test_source_excerpt_preserves_final_lines(
- tmp_path: Path, source: bytes, expected_lines: list[str]
+ tmp_path: Path, source: bytes, expected_lines: list[str], workbench_api
) -> None:
- namespace = runpy.run_path(str(SCRIPT), run_name="codex_security_workbench_db")
- finding_source_excerpt = namespace["finding_source_excerpt"]
+ finding_source_excerpt = workbench_api["finding_source_excerpt"]
target = tmp_path / "target"
revision = commit_source_fixture(target, source)
scan = {"target_revision": revision, "target_snapshot_digest": None, "diff_target_kind": None}
diff --git a/plugins/codex-security/tests/test_workbench_progress.py b/plugins/codex-security/tests/test_workbench_progress.py
index 56281fa46..345ceb1be 100644
--- a/plugins/codex-security/tests/test_workbench_progress.py
+++ b/plugins/codex-security/tests/test_workbench_progress.py
@@ -9,14 +9,6 @@ from workbench_test_support import (
)
-def test_usage_list_coverage_is_invalid(workbench_api):
- from workbench_validation import _valid_measured_scan_usage
-
- assert not _valid_measured_scan_usage(
- {"coverage": ["complete"], "source": "codex_rollout", "threadCount": 0}
- )
-
-
def test_progress_preserves_phase_receipts_until_validation(tmp_path: Path) -> None:
state_dir = tmp_path / "state"
target = tmp_path / "target"
diff --git a/plugins/codex-security/tests/test_workbench_scan_archive.py b/plugins/codex-security/tests/test_workbench_scan_archive.py
index b4c8822f7..368934956 100644
--- a/plugins/codex-security/tests/test_workbench_scan_archive.py
+++ b/plugins/codex-security/tests/test_workbench_scan_archive.py
@@ -229,31 +229,15 @@ def test_archive_cannot_move_the_active_workbench_database(
if state_subdirectory == "aliased-state-child":
state_dir /= "child"
previous = register_scan(state_dir, repository, previous_dir)
+ (previous_dir / "report.md").write_text("saved report")
(scan_dir / "previous.txt").write_text("keep existing output")
- rejected = run_workbench(
- state_dir,
- "register-cli-scan",
- "--repository",
- str(repository),
- "--scan-dir",
- str(scan_dir),
- "--recipe-json",
- json.dumps(
- {
- "config": {},
- "mode": "standard",
- "repository": str(repository),
- "target": {"kind": "repository", "paths": []},
- }
- ),
- "--archive-existing",
- check=False,
- )
+ rejected = register_scan(state_dir, repository, scan_dir, "--archive-existing", check=False)
assert rejected["returncode"] != 0
assert "workbench state or active database" in str(rejected["stderr"])
assert (scan_dir / "previous.txt").read_text() == "keep existing output"
+ assert (previous_dir / "report.md").read_text() == "saved report"
assert not list(tmp_path.glob("scan.previous-*"))
saved = run_workbench(state_dir, "get-scan", "--scan-id", str(previous["scanId"]))
assert saved["scan"]["scanId"] == previous["scanId"]
diff --git a/plugins/codex-security/tests/test_workbench_scan_history.py b/plugins/codex-security/tests/test_workbench_scan_history.py
index 688fe0975..53a429f33 100644
--- a/plugins/codex-security/tests/test_workbench_scan_history.py
+++ b/plugins/codex-security/tests/test_workbench_scan_history.py
@@ -77,22 +77,6 @@ def test_rename_rejects_blank_names(tmp_path: Path) -> None:
assert run_workbench(state_dir, "get-scan", "--scan-id", scan["scanId"])["scan"]["name"] is None
- ("name", "query"),
- [("Évaluation", "évaluation"), ("Straße", "STRASSE"), ("Проверка", "проверка")],
-)
-def test_scan_name_search_ignores_unicode_case(tmp_path: Path, name: str, query: str) -> None:
- state_dir = tmp_path / "state"
- repository = tmp_path / "repository"
- repository.mkdir()
- scan = create_cli_scan(state_dir, tmp_path / "scans", repository, complete=False)
- run_workbench(state_dir, "rename-scan", "--scan-id", scan["scanId"], f"--name={name}")
-
- listed = run_workbench(state_dir, "list-scans", "--query", query)["scans"]
-
- assert [(item["scanId"], item["name"]) for item in listed] == [(scan["scanId"], name)]
-
-
def test_scan_history_search_casefolds_displayed_fields(tmp_path: Path) -> None:
state_dir = tmp_path / "state"
repository = tmp_path / "Straße"
@@ -104,9 +88,11 @@ def test_scan_history_search_casefolds_displayed_fields(tmp_path: Path) -> None:
tmp_path / "scans",
extra_args=("--scope=Ä", "--target-summary=Überblick"),
)["scan"]
- for query in ("Straße", "STRASSE", "Ä", "ä", "Überblick", "ÜBERBLICK"):
+ name = "Évaluation Größe Проверка"
+ run_workbench(state_dir, "rename-scan", "--scan-id", scan["scanId"], f"--name={name}")
+ for query in ("STRASSE", "ä", "ÜBERBLICK", "évaluation", "GRÖSSE", "проверка"):
listed = run_workbench(state_dir, "list-scans", "--query", query)["scans"]
- assert [item["scanId"] for item in listed] == [scan["scanId"]]
+ assert [(item["scanId"], item["name"]) for item in listed] == [(scan["scanId"], name)]
assert run_workbench(state_dir, "list-scans", "--query", "unrelated")["scans"] == []
diff --git a/plugins/codex-security/tests/test_workbench_scan_usage.py b/plugins/codex-security/tests/test_workbench_scan_usage.py
index d9960d7c9..dd3496450 100644
--- a/plugins/codex-security/tests/test_workbench_scan_usage.py
+++ b/plugins/codex-security/tests/test_workbench_scan_usage.py
@@ -1,7 +1,6 @@
from __future__ import annotations
import json
-import runpy
import sqlite3
import sys
import tempfile
@@ -27,6 +26,11 @@ from workbench_test_support import (
)
[email protected](scope="session")
+def scan_usage(workbench_api):
+ return workbench_api["scan_usage"]
+
+
@dataclass(frozen=True)
class ScanFixture:
state_dir: Path
@@ -424,7 +428,7 @@ def test_completion_counts_native_responses_and_compaction_without_legacy_duplic
],
)
def test_legacy_compaction_usage_is_unverifiable_only_inside_scan_window(
- tmp_path: Path, scope: str, native: bool, expected_warning: bool
+ scan_usage, tmp_path: Path, scope: str, native: bool, expected_warning: bool
) -> None:
started = datetime.fromisoformat("2026-01-01T00:00:01+00:00")
completed = started + timedelta(seconds=1)
@@ -447,12 +451,9 @@ def test_legacy_compaction_usage_is_unverifiable_only_inside_scan_window(
events.insert(0 if scope == "before" else len(events), marker)
events.append(_event(completed, "event_msg", {"type": "task_complete"}))
parent = _rollout(tmp_path, "scan-parent", events)
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
- measurement = helper["_read_rollout_usage"](
- helper["RolloutSession"]("scan-parent", None, parent),
+ measurement = scan_usage._read_rollout_usage(
+ scan_usage.RolloutSession("scan-parent", None, parent),
started_at=started,
completed_at=completed,
completed_turn_id="turn-scan-parent" if scope == "completion_turn" else None,
@@ -464,7 +465,7 @@ def test_legacy_compaction_usage_is_unverifiable_only_inside_scan_window(
assert warnings == ({"compaction_usage_unavailable"} if expected_warning else set())
-def test_legacy_compaction_warning_survives_a_later_native_turn(tmp_path: Path) -> None:
+def test_legacy_compaction_warning_survives_a_later_native_turn(scan_usage, tmp_path: Path) -> None:
started = datetime.fromisoformat("2026-01-01T00:00:00+00:00")
parent = _rollout(
tmp_path,
@@ -477,12 +478,9 @@ def test_legacy_compaction_warning_survives_a_later_native_turn(tmp_path: Path)
_event(started, "event_msg", {"type": "task_complete"}),
],
)
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
- measurement = helper["_read_rollout_usage"](
- helper["RolloutSession"]("scan-parent", None, parent),
+ measurement = scan_usage._read_rollout_usage(
+ scan_usage.RolloutSession("scan-parent", None, parent),
started_at=started,
completed_at=started,
)
@@ -493,7 +491,9 @@ def test_legacy_compaction_warning_survives_a_later_native_turn(tmp_path: Path)
assert warnings == {"compaction_usage_unavailable"}
-def test_native_usage_respects_scan_window_and_preserves_cache_details(tmp_path: Path) -> None:
+def test_native_usage_respects_scan_window_and_preserves_cache_details(
+ scan_usage, tmp_path: Path
+) -> None:
started = datetime.fromisoformat("2026-01-01T00:00:00+00:00")
before = started - timedelta(seconds=1)
completed = started + timedelta(seconds=1)
@@ -523,12 +523,9 @@ def test_native_usage_respects_scan_window_and_preserves_cache_details(tmp_path:
),
],
)
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
- measurement = helper["_read_rollout_usage"](
- helper["RolloutSession"]("scan-parent", None, parent),
+ measurement = scan_usage._read_rollout_usage(
+ scan_usage.RolloutSession("scan-parent", None, parent),
started_at=started,
completed_at=completed,
)
@@ -539,7 +536,9 @@ def test_native_usage_respects_scan_window_and_preserves_cache_details(tmp_path:
assert warnings == set()
-def test_native_worker_usage_excludes_inherited_totals_and_survives_resume(tmp_path: Path) -> None:
+def test_native_worker_usage_excludes_inherited_totals_and_survives_resume(
+ scan_usage, tmp_path: Path
+) -> None:
started = datetime.fromisoformat("2026-01-01T00:00:00+00:00")
inherited = _response_event(started, "scan-parent", "parent-response", 1_000, 100)
owned = _response_event(started, "scan-worker", "worker-response", 20, 2)
@@ -566,12 +565,9 @@ def test_native_worker_usage_excludes_inherited_totals_and_survives_resume(tmp_p
parent_thread_id="scan-parent",
copied_events=[inherited, _token_event(started, 1_000, 100)],
)
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
- measurement = helper["_read_rollout_usage"](
- helper["RolloutSession"]("scan-worker", "scan-parent", worker),
+ measurement = scan_usage._read_rollout_usage(
+ scan_usage.RolloutSession("scan-worker", "scan-parent", worker),
started_at=started,
completed_at=started,
)
@@ -583,7 +579,9 @@ def test_native_worker_usage_excludes_inherited_totals_and_survives_resume(tmp_p
@pytest.mark.parametrize("gap", ["before", "after", "inside"])
-def test_native_usage_only_marks_receipt_gaps_inside_scan_window(tmp_path: Path, gap: str) -> None:
+def test_native_usage_only_marks_receipt_gaps_inside_scan_window(
+ scan_usage, tmp_path: Path, gap: str
+) -> None:
started = datetime.fromisoformat("2026-01-01T00:00:00+00:00")
baseline = 120 if gap == "before" else 20
in_window = baseline + (40 if gap == "inside" else 30)
@@ -612,12 +610,9 @@ def test_native_usage_only_marks_receipt_gaps_inside_scan_window(tmp_path: Path,
),
],
)
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
- measurement = helper["_read_rollout_usage"](
- helper["RolloutSession"]("scan-parent", None, parent),
+ measurement = scan_usage._read_rollout_usage(
+ scan_usage.RolloutSession("scan-parent", None, parent),
started_at=started,
completed_at=started,
)
@@ -629,6 +624,7 @@ def test_native_usage_only_marks_receipt_gaps_inside_scan_window(tmp_path: Path,
def test_native_usage_continues_cumulative_baseline_when_recovering_same_turn(
+ scan_usage,
tmp_path: Path,
) -> None:
started = datetime.fromisoformat("2026-01-01T00:00:00+00:00")
@@ -648,12 +644,9 @@ def test_native_usage_continues_cumulative_baseline_when_recovering_same_turn(
_event(started, "event_msg", {"type": "task_complete"}),
],
)
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
- measurement = helper["_read_rollout_usage"](
- helper["RolloutSession"]("scan-parent", None, parent),
+ measurement = scan_usage._read_rollout_usage(
+ scan_usage.RolloutSession("scan-parent", None, parent),
started_at=started,
completed_at=started,
)
@@ -677,7 +670,9 @@ def test_native_usage_continues_cumulative_baseline_when_recovering_same_turn(
"truncated",
],
)
-def test_native_usage_preserves_partial_measurement_warnings(tmp_path: Path, failure: str) -> None:
+def test_native_usage_preserves_partial_measurement_warnings(
+ scan_usage, tmp_path: Path, failure: str
+) -> None:
started = datetime.fromisoformat("2026-01-01T00:00:00+00:00")
first = _response_event(started, "scan-parent", "first", 10, 1)
second = _response_event(started, "scan-parent", "second", 20, 2, turn_usage=(30, 3))
@@ -698,12 +693,9 @@ def test_native_usage_preserves_partial_measurement_warnings(tmp_path: Path, fai
parent = _rollout(tmp_path, "scan-parent", events)
if failure == "truncated":
parent.write_bytes(parent.read_bytes().removesuffix(b"\n"))
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
- measurement = helper["_read_rollout_usage"](
- helper["RolloutSession"]("scan-parent", None, parent),
+ measurement = scan_usage._read_rollout_usage(
+ scan_usage.RolloutSession("scan-parent", None, parent),
started_at=started,
completed_at=started,
)
@@ -806,10 +798,9 @@ def test_completion_counts_only_scan_owned_parent_and_descendants(
@pytest.mark.parametrize("cache_write_field", ["cache_write_input_tokens", "cache_write_tokens"])
def test_scan_usage_helper_preserves_cached_token_totals(
+ scan_usage,
cache_write_field: str,
) -> None:
- helper = Path(__file__).resolve().parents[1] / "scripts" / "workbench_scan_usage.py"
- snapshot = runpy.run_path(str(helper))["_token_snapshot"]
usage = {
"input_tokens": 120,
"cached_input_tokens": 30,
@@ -819,7 +810,7 @@ def test_scan_usage_helper_preserves_cached_token_totals(
"total_tokens": 135,
}
- assert snapshot({"info": {"total_token_usage": usage}}) == _counts(
+ assert scan_usage._token_snapshot({"info": {"total_token_usage": usage}}) == _counts(
120, 30, 15, cache_write_input_tokens=12
)
@@ -829,11 +820,8 @@ def test_scan_usage_helper_preserves_cached_token_totals(
[(0, 12, 12), (0, 0, 0), (5, 12, 5), (False, 12, None), (0.0, 12, None)],
)
def test_scan_usage_helper_requires_integer_cache_write_placeholder(
- canonical: object, legacy: int, expected: int | None
+ scan_usage, canonical: object, legacy: int, expected: int | None
) -> None:
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
usage = {
"input_tokens": 120,
"cached_input_tokens": 30,
@@ -843,7 +831,7 @@ def test_scan_usage_helper_requires_integer_cache_write_placeholder(
"total_tokens": 135,
}
- assert helper["_usage_counts"](usage) == (
+ assert scan_usage._usage_counts(usage) == (
None if expected is None else _counts(120, 30, 15, cache_write_input_tokens=expected)
)
@@ -859,11 +847,10 @@ def test_scan_usage_helper_requires_integer_cache_write_placeholder(
("2026-01-01T00:00:00", None),
],
)
-def test_scan_usage_timestamp_handles_utc_boundaries(value: str, expected: str | None) -> None:
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
- assert helper["_timestamp"](value) == (
+def test_scan_usage_timestamp_handles_utc_boundaries(
+ scan_usage, value: str, expected: str | None
+) -> None:
+ assert scan_usage._timestamp(value) == (
datetime.fromisoformat(expected) if expected is not None else None
)
@@ -906,7 +893,7 @@ def test_scan_cost_preserves_diagnostics_for_overflowing_amount(
assert json.loads(workbench_api["parse_scan_cost"](json.dumps(value))) == value
[email protected]("coverage", [[], {}])
[email protected]("coverage", [[], ["complete"], {}])
def test_scan_cost_preserves_diagnostics_for_non_scalar_coverage(workbench_api, coverage) -> None:
value = {"usage": {"coverage": coverage, "source": "codex_rollout", "threadCount": 0}}
with pytest.raises(SystemExit) as error:
@@ -1068,7 +1055,7 @@ def test_usage_finds_state_database_in_configured_home(
home_variable: str,
home_kind: str,
monkeypatch: pytest.MonkeyPatch,
- workbench_api,
+ scan_usage,
) -> None:
home = tmp_path / ("native home" if sys.platform == "win32" else "native home ")
if home_kind == "literal_tilde":
@@ -1089,7 +1076,7 @@ def test_usage_finds_state_database_in_configured_home(
configured_home = "~" if home_kind == "home" else f"~/{home.name}"
monkeypatch.setenv(home_variable, configured_home)
- assert workbench_api["scan_usage"]._codex_state_database() == database.resolve()
+ assert scan_usage._codex_state_database() == database.resolve()
def test_completion_reports_unavailable_without_fabricating_zero(tmp_path: Path) -> None:
@@ -1278,7 +1265,7 @@ def test_completion_counts_deep_worker_retries_and_descendants(tmp_path: Path) -
@pytest.mark.parametrize("owner_first", [True, False])
def test_usage_counts_registered_worker_when_its_parent_rollout_is_missing(
- tmp_path: Path, monkeypatch: pytest.MonkeyPatch, owner_first: bool
+ scan_usage, tmp_path: Path, monkeypatch: pytest.MonkeyPatch, owner_first: bool
) -> None:
fixture = _start_scan(tmp_path)
counted = fixture.started_at + timedelta(microseconds=1)
@@ -1310,8 +1297,6 @@ def test_usage_counts_registered_worker_when_its_parent_rollout_is_missing(
)
for key, value in fixture.environment.items():
monkeypatch.setenv(key, value)
- helper = Path(__file__).resolve().parents[1] / "scripts" / "workbench_scan_usage.py"
- collect_usage = runpy.run_path(str(helper))["collect_scan_usage"]
with sqlite3.connect(fixture.state_dir / "workbench.sqlite3") as connection:
connection.row_factory = sqlite3.Row
connection.execute(
@@ -1319,7 +1304,7 @@ def test_usage_counts_registered_worker_when_its_parent_rollout_is_missing(
("registered-worker", fixture.scan_id),
)
scan = connection.execute("SELECT * FROM scans WHERE id = ?", (fixture.scan_id,)).fetchone()
- usage = collect_usage(
+ usage = scan_usage.collect_scan_usage(
connection,
scan,
thread_id="scan-parent" if owner_first else None,
@@ -1555,7 +1540,7 @@ def test_usage_has_no_separate_workbench_command(workbench_api, monkeypatch, cap
@pytest.mark.parametrize("native", [False, True])
def test_host_finalizes_exact_completion_turn_without_changing_artifacts(
- tmp_path: Path, native: bool
+ scan_usage, tmp_path: Path, native: bool
) -> None:
fixture = _start_scan(tmp_path)
counted = fixture.started_at + timedelta(microseconds=1)
@@ -1660,11 +1645,8 @@ def test_host_finalizes_exact_completion_turn_without_changing_artifacts(
]
with parent.open("a", encoding="utf-8") as output:
output.writelines(f"{json.dumps(event)}\n" for event in later_events)
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
- measurement = helper["_read_rollout_usage"](
- helper["RolloutSession"]("scan-parent", None, parent),
+ measurement = scan_usage._read_rollout_usage(
+ scan_usage.RolloutSession("scan-parent", None, parent),
started_at=fixture.started_at,
completed_at=datetime.fromisoformat(stopped_at.replace("Z", "+00:00")),
completed_turn_id=turn_id,
@@ -2022,6 +2004,7 @@ def test_completion_recovers_replaced_deep_worker_threads(tmp_path: Path, worker
],
)
def test_worker_discovery_matches_platform_path_spelling(
+ scan_usage,
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
platform_name: str,
@@ -2045,14 +2028,10 @@ def test_worker_discovery_matches_platform_path_spelling(
"UPDATE threads SET cwd = ?, created_at = ? WHERE id = ?",
(recorded_cwd, int(started.timestamp()), "replaced-worker"),
)
- scripts = Path(__file__).resolve().parents[1] / "scripts"
- monkeypatch.syspath_prepend(str(scripts))
- helper = runpy.run_path(str(scripts / "workbench_scan_usage.py"))
- discover = helper["_discover_rollout_sessions"]
- monkeypatch.setitem(discover.__globals__, "os", SimpleNamespace(name=platform_name))
+ monkeypatch.setattr(scan_usage, "os", SimpleNamespace(name=platform_name))
warnings: set[str] = set()
- sessions, missing = discover(
+ sessions, missing = scan_usage._discover_rollout_sessions(
state_database,
["current-worker"],
warnings,
@@ -2385,7 +2364,7 @@ def test_completion_preserves_receipts_when_later_worker_usage_is_missing(
@pytest.mark.parametrize("terminal", ["task_complete", "turn_aborted"])
@pytest.mark.parametrize("receipt", ["none", "native", "legacy", "zero"])
def test_closed_turn_coverage_survives_later_success(
- tmp_path: Path, worker: bool, terminal: str, receipt: str
+ scan_usage, tmp_path: Path, worker: bool, terminal: str, receipt: str
) -> None:
started = datetime.fromisoformat("2026-01-01T00:00:00+00:00")
thread_id = "worker" if worker else "scan-parent"
@@ -2415,11 +2394,8 @@ def test_closed_turn_coverage_survives_later_success(
rollout = _rollout(
tmp_path, thread_id, events, parent_thread_id=parent_id, include_task_start=False
)
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
- measurement = helper["_read_rollout_usage"](
- helper["RolloutSession"](thread_id, parent_id, rollout),
+ measurement = scan_usage._read_rollout_usage(
+ scan_usage.RolloutSession(thread_id, parent_id, rollout),
started_at=started,
completed_at=started,
)
@@ -2432,7 +2408,7 @@ def test_closed_turn_coverage_survives_later_success(
@pytest.mark.parametrize("receipt", [None, "legacy", "native"])
def test_closed_turn_coverage_retains_before_window_receipt_evidence(
- tmp_path: Path, receipt: str | None
+ scan_usage, tmp_path: Path, receipt: str | None
) -> None:
started = datetime.fromisoformat("2026-01-01T00:00:01+00:00")
before = started - timedelta(seconds=1)
@@ -2452,11 +2428,8 @@ def test_closed_turn_coverage_retains_before_window_receipt_evidence(
]
)
rollout = _rollout(tmp_path, "scan-parent", events)
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
- measurement = helper["_read_rollout_usage"](
- helper["RolloutSession"]("scan-parent", None, rollout),
+ measurement = scan_usage._read_rollout_usage(
+ scan_usage.RolloutSession("scan-parent", None, rollout),
started_at=started,
completed_at=started,
)
@@ -2465,7 +2438,9 @@ def test_closed_turn_coverage_retains_before_window_receipt_evidence(
assert warnings == (set() if receipt else {"token_record_missing"})
-def test_closed_turn_coverage_accepts_delayed_receipt_for_same_turn(tmp_path: Path) -> None:
+def test_closed_turn_coverage_accepts_delayed_receipt_for_same_turn(
+ scan_usage, tmp_path: Path
+) -> None:
started = datetime.fromisoformat("2026-01-01T00:00:00+00:00")
rollout = _rollout(
tmp_path,
@@ -2476,11 +2451,8 @@ def test_closed_turn_coverage_accepts_delayed_receipt_for_same_turn(tmp_path: Pa
_response_event(started, "scan-parent", "late", 10, 2, turn_id="first"),
],
)
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
- measurement = helper["_read_rollout_usage"](
- helper["RolloutSession"]("scan-parent", None, rollout),
+ measurement = scan_usage._read_rollout_usage(
+ scan_usage.RolloutSession("scan-parent", None, rollout),
started_at=started,
completed_at=started,
)
@@ -2490,6 +2462,7 @@ def test_closed_turn_coverage_accepts_delayed_receipt_for_same_turn(tmp_path: Pa
def test_closed_turn_coverage_preserves_interruption_after_same_turn_resumes(
+ scan_usage,
tmp_path: Path,
) -> None:
started = datetime.fromisoformat("2026-01-01T00:00:00+00:00")
@@ -2504,11 +2477,8 @@ def test_closed_turn_coverage_preserves_interruption_after_same_turn_resumes(
_event(started, "event_msg", {"type": "task_complete", "turn_id": "same-turn"}),
],
)
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
- measurement = helper["_read_rollout_usage"](
- helper["RolloutSession"]("scan-parent", None, rollout),
+ measurement = scan_usage._read_rollout_usage(
+ scan_usage.RolloutSession("scan-parent", None, rollout),
started_at=started,
completed_at=started,
)
@@ -2518,6 +2488,7 @@ def test_closed_turn_coverage_preserves_interruption_after_same_turn_resumes(
def test_closed_turn_coverage_ignores_inherited_and_outside_window_interruptions(
+ scan_usage,
tmp_path: Path,
) -> None:
started = datetime.fromisoformat("2026-01-01T00:00:00+00:00")
@@ -2537,11 +2508,8 @@ def test_closed_turn_coverage_ignores_inherited_and_outside_window_interruptions
_event(before, "event_msg", {"type": "turn_aborted", "turn_id": "inherited"}),
],
)
- helper = runpy.run_path(
- str(Path(__file__).resolve().parents[1] / "scripts/workbench_scan_usage.py")
- )
- measurement = helper["_read_rollout_usage"](
- helper["RolloutSession"]("worker", "scan-parent", rollout),
+ measurement = scan_usage._read_rollout_usage(
+ scan_usage.RolloutSession("worker", "scan-parent", rollout),
started_at=started,
completed_at=started,
)
diff --git a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
index 4113e8df5..333e50daf 100644
--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
@@ -1120,69 +1120,16 @@ def test_scan_model_migration_preserves_existing_scans() -> None:
assert scan["completion_warnings_json"] == "[]"
-def test_deep_discovery_error_migration_backfills_each_existing_threshold() -> None:
- connection, apply_migrations = create_historical_database(27)
-
- timestamp = "2026-07-01T00:00:00Z"
- for index, stop_after_no_new in enumerate((2, 7), start=1):
- workspace_id = f"legacy-deep-workspace-{index}"
- scan_id = f"legacy-deep-scan-{index}"
- connection.execute(
- "INSERT INTO workspaces (id, created_at, updated_at) VALUES (?, ?, ?)",
- (workspace_id, timestamp, timestamp),
- )
- connection.execute(
- """
- INSERT INTO scans (
- id, workspace_id, target_path, target_revision, scope, mode,
- scan_dir, status, phase, started_at, created_at, updated_at
- ) VALUES (?, ?, ?, ?, '.', 'deep', ?, 'running', 'discovery', ?, ?, ?)
- """,
- (
- scan_id,
- workspace_id,
- f"/legacy/target-{index}",
- "legacy-revision",
- f"/legacy/scan-{index}",
- timestamp,
- timestamp,
- timestamp,
- ),
- )
- connection.execute(
- """
- INSERT INTO deep_scan_runs (
- scan_id, schema_version, workflow_version, status, phase,
- workers, subagents, stop_after_no_new, max_discovery_runs,
- created_at, updated_at
- ) VALUES (?, 1, 'deep-scan-mcp/v1', 'running', 'discovery',
- 2, 0, ?, 10, ?, ?)
- """,
- (scan_id, stop_after_no_new, timestamp, timestamp),
- )
- connection.commit()
-
- apply_migrations(connection)
-
- rows = connection.execute(
- """
- SELECT stop_after_no_new, stop_after_consecutive_errors, consecutive_errors
- FROM deep_scan_runs
- ORDER BY scan_id
- """
- ).fetchall()
- assert [tuple(row) for row in rows] == [(2, 2, 0), (7, 7, 0)]
-
-
@pytest.mark.parametrize(
("legacy_name", "has_owner_columns"),
(
+ (None, False),
("durable deep scan coordinator ownership", True),
("repair deep scan orchestration state", False),
),
)
-def test_workbench_repairs_recorded_deep_scan_failure_counter_migration(
- legacy_name: str, has_owner_columns: bool
+def test_deep_scan_failure_counter_migration_backfills_existing_thresholds(
+ legacy_name: str | None, has_owner_columns: bool
) -> None:
connection, apply_migrations = create_historical_database(27)
timestamp = "2026-07-01T00:00:00Z"
@@ -1222,10 +1169,11 @@ def test_workbench_repairs_recorded_deep_scan_failure_counter_migration(
if has_owner_columns:
connection.execute("ALTER TABLE deep_scan_runs ADD COLUMN coordinator_owner_id TEXT")
connection.execute("ALTER TABLE deep_scan_runs ADD COLUMN coordinator_heartbeat_at TEXT")
- connection.execute(
- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (27, ?, ?)",
- (legacy_name, timestamp),
- )
+ if legacy_name is not None:
+ connection.execute(
+ "INSERT INTO schema_migrations (version, name, applied_at) VALUES (27, ?, ?)",
+ (legacy_name, timestamp),
+ )
connection.commit()
apply_migrations(connection)
assert [
@@ -1248,12 +1196,9 @@ def test_workbench_repairs_recorded_deep_scan_failure_counter_migration(
"FROM deep_scan_runs ORDER BY scan_id"
)
] == [(9, 3), (9, 3)]
- assert (
- connection.execute("SELECT name FROM schema_migrations WHERE version = 27").fetchone()[
- "name"
- ]
- == legacy_name
- )
+ assert connection.execute("SELECT name FROM schema_migrations WHERE version = 27").fetchone()[
+ "name"
+ ] == (legacy_name or "persist deep scan consecutive discovery failures")
if has_owner_columns:
assert {"coordinator_owner_id", "coordinator_heartbeat_at"} <= {
row[1] for row in connection.execute("PRAGMA table_info(deep_scan_runs)")
diff --git a/plugins/codex-security/tests/test_workbench_standard_deep_results.py b/plugins/codex-security/tests/test_workbench_standard_deep_results.py
index 0988f540c..8f2328793 100644
--- a/plugins/codex-security/tests/test_workbench_standard_deep_results.py
+++ b/plugins/codex-security/tests/test_workbench_standard_deep_results.py
@@ -1963,10 +1963,10 @@ def test_independent_worker_candidate_ids_do_not_share_rejection(tmp_path: Path)
assert "previousFindings" not in rejected
-def test_standard_worker_results_commit_and_recover_without_discovery_ledgers(
+def test_standard_worker_results_commit_recover_and_finish_without_discovery_ledgers(
tmp_path: Path,
) -> None:
- state_dir, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path)
+ state_dir, codex_home, target, scan_dir, scan_id = deep_scan_fixture(tmp_path)
worker_id, worker_result = accepted_standard_worker(state_dir, codex_home, scan_dir, scan_id)
reducer_id, reducer_result, committed = committed_standard_reducer(
state_dir,
@@ -1985,25 +1985,20 @@ def test_standard_worker_results_commit_and_recover_without_discovery_ledgers(
assert workers[reducer_id]["resultManifestPath"] == str(reducer_result)
assert not (scan_dir / "artifacts" / "02_discovery").exists()
+ environment = {"CODEX_HOME": str(codex_home)}
recovered = get_deep_scan(
- state_dir, scan_id, "standard-worker-thread", environment={"CODEX_HOME": str(codex_home)}
+ state_dir, scan_id, "standard-worker-thread", environment=environment
)["deepScan"]
assert recovered["canonicalArtifacts"] is None
assert recovered["workers"] == committed["workers"]
-
-def test_standard_worker_results_finish_with_only_canonical_parent_manifest(
- tmp_path: Path,
-) -> None:
- state_dir, codex_home, target, scan_dir, scan_id = standard_parent_results_fixture(tmp_path)
+ write_completed_contract(
+ scan_dir, scan_id, target, relative_path="app.py", coverage_mode="deep_repository"
+ )
manifest_path = scan_dir / "scan-manifest.json"
finished = finish_deep_scan(
- state_dir,
- scan_id,
- "capped",
- str(manifest_path),
- environment={"CODEX_HOME": str(codex_home)},
+ state_dir, scan_id, "capped", str(manifest_path), environment=environment
)["deepScan"]
assert finished["status"] == "succeeded"
diff --git a/plugins/codex-security/tests/test_worker_receipt_admission.py b/plugins/codex-security/tests/test_worker_receipt_admission.py
index b02918f2b..de36af8a1 100644
--- a/plugins/codex-security/tests/test_worker_receipt_admission.py
+++ b/plugins/codex-security/tests/test_worker_receipt_admission.py
@@ -11,6 +11,7 @@ import pytest
from test_deep_scan_successful_publication import add_worker
from test_deep_scan_successful_publication import publication_scan as publication_scan
from test_partial_coverage_projection import stop_and_recover_projection
+from test_partial_coverage_projection import stop_scan as stop_scan
from test_saved_deferred_identity import recover, save_worker
from workbench_test_support import saved_draft, write_checkpoint
@@ -67,7 +68,7 @@ def test_unselected_pending_checkpoint_links_to_accepted_worker_surface(
@pytest.mark.parametrize("retry", [False, True])
@pytest.mark.parametrize("archived", [False, True])
def test_stopped_receipt_ownership_requires_a_matching_checkpoint(
- workbench_api, workbench_db, publication_scan, monkeypatch, recorded, retry, archived
+ workbench_api, workbench_db, publication_scan, stop_scan, recorded, retry, archived
):
scan = publication_scan()
initial = add_worker(workbench_db, scan)
@@ -124,7 +125,7 @@ def test_stopped_receipt_ownership_requires_a_matching_checkpoint(
original = result.read_bytes()
for name in ("scan-manifest.json", "findings.json", "coverage.json"):
(scan.scan_dir / name).unlink()
- coverage = stop_and_recover_projection(workbench_api, workbench_db, scan, monkeypatch, retry)
+ coverage = stop_and_recover_projection(workbench_api, workbench_db, scan, stop_scan, retry)
assert len(coverage["surfaces"]) == 1
refs = coverage["surfaces"][0]["receiptRefs"]
selected = shared if recorded else local
diff --git a/plugins/codex-security/tests/workbench_test_support.py b/plugins/codex-security/tests/workbench_test_support.py
index 6cfdf6882..cf9e8be87 100644
--- a/plugins/codex-security/tests/workbench_test_support.py
+++ b/plugins/codex-security/tests/workbench_test_support.py
@@ -946,5 +946,16 @@ class ScanFixtureTestCase(TestCase):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
+ def rewrite_sealed_artifact(self, relative_path: str, payload: object) -> None:
+ self.write_json(relative_path, payload)
+ manifest = self.read_json("scan-manifest.json")
+ for artifact in manifest["scan"]["artifacts"]:
+ if artifact["path"] == relative_path:
+ artifact["sha256"] = self.sha256_file(relative_path)
+ break
+ else:
+ raise AssertionError(f"missing sealed artifact: {relative_path}")
+ self.write_json("scan-manifest.json", manifest)
+
def sha256_file(self, name: str) -> str:
return hashlib.sha256((self.scan_dir / name).read_bytes()).hexdigest()