diff --git a/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.ts b/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.ts index 9d75a036d..6906d679f 100644 --- a/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.ts +++ b/plugins/codex-security/mcp-app/tests/test_compact_artifact_server.ts @@ -76,6 +76,7 @@ try { [path.join(bundledPluginRoot, "mcp", "server.mjs"), "shipped"], ]) { await testParentToolList(bundle); + await testCustomDiscoveryHandoff(bundle, runtimeLabel); await testClaimedParentArtifactOperations(bundle, runtimeLabel); await testSemanticScanDraftCompletion(bundle, runtimeLabel); await testCompactDiffScanCompletion(bundle, runtimeLabel); @@ -324,6 +325,165 @@ async function testCompactDiffScanCompletion( } } +async function testCustomDiscoveryHandoff( + bundle: string, + runtimeLabel: string, +) { + for (const mode of ["standard", "diff"] as const) { + const label = `custom-discovery-${mode}-${runtimeLabel}`; + const { repoRoot, environment } = await createScanFixture(label); + const sourceLine = "return request.value"; + await writeFile(path.join(repoRoot, "src", "fixture.py"), sourceLine); + const selection: Record = { + targetPath: repoRoot, + scope: ".", + mode, + }; + if (mode === "diff") { + const git = (...args: string[]) => + gitText( + [ + "-c", + "user.name=Fixture", + "-c", + "user.email=fixture@example.com", + ...args, + ], + { cwd: repoRoot }, + ).trim(); + git("init", "-q"); + git("add", "."); + git("commit", "-qm", "base"); + const baseRevision = git("rev-parse", "HEAD"); + await writeFile( + path.join(repoRoot, "src", "fixture.py"), + `${sourceLine}\n`, + ); + git("add", "."); + git("commit", "-qm", "selected changes"); + selection.diffTarget = { + kind: "range", + baseRevision, + headRevision: git("rev-parse", "HEAD"), + }; + } + const client = await startClient(bundle, environment); + const ownerThread = `${label}-owner`; + const call = toolCaller(client, ownerThread); + try { + const { scanId, scanDir, handoffClaimToken } = await startScan( + call, + ownerThread, + selection, + { + workspace: `${label}: open workspace`, + setup: `${label}: submit setup`, + scan: `${label}: start scan`, + claim: `${label}: claim scan`, + owner: `${label}: attach owner`, + }, + ); + const progress = async () => + requireSuccessfulTool( + await call("get_codex_security_scan_context", { + scanId, + handoffClaimToken, + }), + ).scan.progress; + await progress(); + const provisional = { + ...finding("custom-discovery", "src/fixture.py"), + extensions: { customValidationSurfaceIds: ["surface-custom"] }, + }; + const draft = { + scanId, + handoffClaimToken, + scope: { validationMode: "custom_pending" }, + findings: [provisional], + coverage: { + surfaces: [ + { + id: "surface-custom", + label: "Request output", + disposition: "reported", + }, + ], + explicitExclusions: [], + deferred: [], + }, + }; + requireSuccessfulTool( + await call("record_codex_security_scan_draft", { + ...draft, + complete: false, + }), + `${label}: retain incomplete source discovery`, + ); + const before = await snapshotScanDraft(scanDir); + const checkpoint = await readFile( + path.join(scanDir, "checkpoint-head.json"), + "utf8", + ); + for (const completion of [{ complete: true }, {}]) { + requireToolError( + await call("record_codex_security_scan_draft", { + ...draft, + ...completion, + }), + /codeEvidence/, + `${label}: reject incomplete source proof before custom validation`, + ); + assert.deepEqual(await snapshotScanDraft(scanDir), before); + assert.equal( + await readFile(path.join(scanDir, "checkpoint-head.json"), "utf8"), + checkpoint, + ); + assert.equal((await progress()).status, "running"); + } + const repaired = { + ...provisional, + codeEvidence: [ + { + id: "request-output", + label: "Request-controlled output", + path: "src/fixture.py", + startLine: 1, + code: sourceLine, + explanation: "The request value is returned without HTML encoding.", + }, + ], + rootCause: + "Request-controlled output is returned without HTML encoding.", + attackPath: { + dataflow: + "The request value flows directly into the returned output.", + }, + }; + for (const completion of [{ complete: true }, {}]) { + requireSuccessfulTool( + await call("record_codex_security_scan_draft", { + ...draft, + ...completion, + findings: [repaired], + }), + `${label}: accept repaired source proof without claiming validation`, + ); + const manifest = await readJson(scanDir, "scan-manifest.json"); + assert.equal(manifest.scan.complete, undefined); + assert.equal(manifest.scan.sealedAt, undefined); + assert.equal(manifest.scan.scope.validationMode, "custom_pending"); + assert.equal( + (await readJson(scanDir, "findings.json")).findings[0].validation, + undefined, + ); + assert.equal((await progress()).status, "running"); + } + } finally { + await client.close(); + } + } +} + async function testSemanticScanDraftCompletion( bundle: string, runtimeLabel: string, diff --git a/plugins/codex-security/scripts/workbench_saved_results.py b/plugins/codex-security/scripts/workbench_saved_results.py index 58f18db6a..cfb82c510 100644 --- a/plugins/codex-security/scripts/workbench_saved_results.py +++ b/plugins/codex-security/scripts/workbench_saved_results.py @@ -2921,8 +2921,9 @@ def write_scan_draft(db: Any, connection: Any, args: Any) -> dict[str, Any]: _populate_unsealed_artifact_envelope(manifest, findings, coverage, binding) _validate_completion_binding(manifest, findings, coverage, binding) checkpoint = manifest["scan"] + # Completed semantic drafts omit complete; only false marks a checkpoint. if ( - checkpoint.get("complete") is True + checkpoint.get("complete") is not False and checkpoint.get("scope", {}).get("validationMode") == "custom_pending" ): # Return repair feedback while the discovery agent still owns its turn. diff --git a/plugins/codex-security/tests/test_workbench_completion_binding.py b/plugins/codex-security/tests/test_workbench_completion_binding.py index c3c6f163a..16557749e 100644 --- a/plugins/codex-security/tests/test_workbench_completion_binding.py +++ b/plugins/codex-security/tests/test_workbench_completion_binding.py @@ -1191,8 +1191,9 @@ def test_completion_requires_evidence_and_keeps_the_scan_repairable( @pytest.mark.parametrize("field", ["codeEvidence", "rootCause", "attackPath"]) +@pytest.mark.parametrize("explicit_complete", [False, True]) def test_custom_discovery_handoff_requires_source_evidence_before_validation( - tmp_path: Path, field: str + tmp_path: Path, field: str, explicit_complete: bool ) -> None: state_dir, scan_id, scan_dir = _start_scan_with_draft_findings(tmp_path) documents = { @@ -1218,7 +1219,10 @@ def test_custom_discovery_handoff_requires_source_evidence_before_validation( names = ("scan-manifest.json", "findings.json", "coverage.json", "checkpoint-head.json") before = {name: (scan_dir / name).read_bytes() for name in names} - scan["complete"] = True + if explicit_complete: + scan["complete"] = True + else: + scan.pop("complete") staged.write_text(json.dumps(documents)) rejected = run_workbench(state_dir, *args, check=False) assert rejected["returncode"] != 0