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): streamline Deep Scan recovery fixtures (#1972)

Michael D'Angelod6dcbb5c6cfe
diff --git a/plugins/codex-security/tests/deep_deadline_crash.py b/plugins/codex-security/tests/deep_deadline_crash.py
new file mode 100644
index 000000000..d729b477b
--- /dev/null
+++ b/plugins/codex-security/tests/deep_deadline_crash.py
@@ -0,0 +1,44 @@
+"""Test child that exits after deadline publication, immediately before SQLite commits."""
+
+import os
+import runpy
+import sqlite3
+import sys
+from pathlib import Path
+from types import SimpleNamespace
+
+COMMIT_EXIT = 23
+
+
+class CrashBeforeCommit(sqlite3.Connection):
+ def commit(self):
+ os._exit(COMMIT_EXIT)
+
+
+def finish_until_commit(deep_scan, database, scan_id, scan_dir):
+ with sqlite3.connect(database, factory=CrashBeforeCommit) as connection:
+ connection.row_factory = sqlite3.Row
+ deep_scan.finish_deep_scan(
+ connection,
+ SimpleNamespace(
+ scan_id=scan_id,
+ terminal_reason="capped",
+ manifest_path=str(scan_dir / "scan-manifest.json"),
+ staged_manifest_path=None,
+ omitted_worker_id=[],
+ ),
+ )
+ raise AssertionError("Deadline publication returned without committing")
+
+
+if __name__ == "__main__":
+ database, scan_id, scan_dir = sys.argv[1:]
+ script = Path(__file__).resolve().parents[1] / "scripts" / "workbench_db.py"
+ api = runpy.run_path(str(script), run_name="deadline_crash_test")
+ deep_scan = api["deep_scan"]
+ deep_scan.configure(
+ SimpleNamespace(
+ **api, preserve_stopped_results=api["preserve_stopped_results_after_transition"]
+ )
+ )
+ finish_until_commit(deep_scan, database, scan_id, Path(scan_dir))
diff --git a/plugins/codex-security/tests/test_deep_deadline_recovery_edges.py b/plugins/codex-security/tests/test_deep_deadline_recovery_edges.py
index 1eedf264f..a2085a11b 100644
--- a/plugins/codex-security/tests/test_deep_deadline_recovery_edges.py
+++ b/plugins/codex-security/tests/test_deep_deadline_recovery_edges.py
@@ -130,43 +130,53 @@ def assert_retained_finding(scan: SimpleNamespace):
assert findings[0]["severity"] == scan.finding["severity"]
[email protected]("canonical", [False, True])
[email protected]("same_identity", [False, True])
- ("field", "value"), [("severity", None), ("code_evidence", []), ("root_cause", None)]
-)
-def test_deadline_recovers_partial_worker_findings_before_completion(
- tmp_path, canonical, same_identity, field, value
-):
- scan = deadline_fixture(tmp_path, "valid")
- if canonical:
- canonical_parent(scan, saved_draft(scan.scan_id))
+def append_worker_findings(scan, findings):
head = next(scan.directory.glob("artifacts/**/checkpoint-head.json"))
- selected = head.parent / "checkpoints" / json.loads(head.read_text())["checkpoint"]
- draft = json.loads(selected.read_text())
- partial = copy.deepcopy(scan.finding)
- if not same_identity:
- partial["ruleId"] = "synthetic.partial"
- partial["identity"] = {"anchor": "partial"}
- partial["provenance"]["candidateId"] = "partial-candidate"
- if field == "severity":
- partial.pop(field)
- else:
- partial[field] = value
- partial["severity"]["level"] = "critical"
- draft["findings"].append(partial)
+ previous = head.parent / "checkpoints" / json.loads(head.read_text())["checkpoint"]
+ draft = json.loads(previous.read_text())
+ draft["findings"].extend(findings)
checkpoint = write_checkpoint(head.parent / "checkpoints", draft)
head.write_text(json.dumps({"checkpoint": checkpoint.name}))
- source_bytes = {path: path.read_bytes() for path in (selected, checkpoint, head)}
+ return previous, checkpoint, head
+
+
[email protected]("canonical", [False, True], ids=["coordinator", "canonical"])
+def test_deadline_recovers_partial_worker_findings_before_completion(tmp_path, canonical):
+ scan = deadline_fixture(tmp_path, "valid")
+ if canonical:
+ canonical_parent(scan, saved_draft(scan.scan_id))
+
+ defects = {"severity": None, "codeEvidence": [None], "rootCause": {"summary": 7}}
+ partials = []
+ for same_identity in (False, True):
+ for field, value in defects.items():
+ partial = copy.deepcopy(scan.finding)
+ if not same_identity:
+ anchor = f"partial-{field.lower()}"
+ partial.update(ruleId="synthetic.partial", identity={"anchor": anchor})
+ partial["provenance"]["candidateId"] = anchor
+ if field == "severity":
+ partial.pop(field)
+ else:
+ partial[field] = value
+ partial["severity"]["level"] = "critical"
+ partials.append(partial)
+ # Reject the old spelling even when the canonical rootCause is present.
+ partials.append({**scan.finding, "root_cause": None})
+ source_bytes = {path: path.read_bytes() for path in append_worker_findings(scan, partials)}
terminal = finish(scan, canonical=canonical)["deepScan"]
assert terminal["status"] == "succeeded"
assert terminal["terminalReason"] == "capped"
assert_retained_finding(scan)
warnings = get_scan(scan.state, scan.scan_id)["scan"]["warnings"]
- assert any(field in warning for warning in warnings)
coverage = json.loads((scan.directory / "coverage.json").read_text())
- assert any(field in row["reason"] for row in coverage["deferred"])
+ pending = {row["reason"] for row in coverage["deferred"]}
+ for field in defects:
+ field_warnings = [warning for warning in warnings if field in warning]
+ assert len(field_warnings) == 2, (field, warnings)
+ assert pending.issuperset(field_warnings)
+ assert any("root_cause" in warning and warning in pending for warning in warnings)
assert all(path.read_bytes() == contents for path, contents in source_bytes.items())
run_workbench(
scan.state,
@@ -249,16 +259,38 @@ def test_deadline_recovery_does_not_snapshot_untouched_optional_sarif(tmp_path):
assert external.read_text() == "Unchanged optional export.\n"
[email protected]("include_paths", [None, ["app.py"], ["unrelated.py"]])
[email protected]("source", ["parent", "worker", "both"])
+ ("source", "include_paths", "distinct"),
+ [
+ ("both", None, ["parent-app.py", "parent-other.py", "worker-app.py", "worker-other.py"]),
+ ("both", ["app.py"], ["parent-app.py", "worker-app.py"]),
+ ("parent", ["app.py"], ["parent-app.py"]),
+ ("worker", ["app.py"], ["worker-app.py"]),
+ ("both", ["unrelated.py"], []),
+ ],
+ ids=["repository", "scoped", "parent-only", "worker-only", "no-matches"],
+)
def test_deadline_recovery_applies_requested_scope_to_parent_and_worker_findings(
- tmp_path, include_paths, source
+ tmp_path, source, include_paths, distinct
):
scan = deadline_fixture(
tmp_path, "empty" if source == "parent" else "valid", include_paths=include_paths
)
+
+ def source_findings(source_name):
+ rows = []
+ for path in ("app.py", "other.py"):
+ row = copy.deepcopy(scan.finding)
+ row["identity"] = {"anchor": f"{source_name}-{path}"}
+ row["locations"][0]["path"] = row["codeEvidence"][0]["path"] = path
+ rows.append(row)
+ return rows
+
+ (tmp_path / "target" / "other.py").write_text("value = 1\n")
+ if source != "parent":
+ append_worker_findings(scan, source_findings("worker"))
if source != "worker":
- draft = saved_draft(scan.scan_id, findings=[scan.finding])
+ draft = saved_draft(scan.scan_id, findings=[scan.finding, *source_findings("parent")])
draft["coverage"].update(
mode="scoped_path" if include_paths is not None else "deep_repository",
inventoryStrategy="scoped_path" if include_paths is not None else "repository",
@@ -272,13 +304,18 @@ def test_deadline_recovery_applies_requested_scope_to_parent_and_worker_findings
findings = json.loads((scan.directory / "findings.json").read_text())["findings"]
head = json.loads((scan.directory / "checkpoint-head.json").read_text())["checkpoint"]
checkpoint = json.loads((scan.directory / "checkpoints" / head).read_text())
- expected = 0 if include_paths == ["unrelated.py"] else 1
- assert len(findings) == len(checkpoint["findings"]) == expected
+ # Each available source starts with the same app.py finding; keep it exactly once in scope.
+ expected = [scan.finding["identity"]["anchor"], *distinct] if distinct else []
+ for rows in (findings, checkpoint["findings"]):
+ assert sorted(row["identity"]["anchor"] for row in rows) == sorted(expected)
assert all(path.read_bytes() == contents for path, contents in original.items())
[email protected]("with_worker", [False, True])
[email protected]("inventory", [None, "", "invalid_strategy", 7, "scoped_path"])
+ ("with_worker", "inventory"),
+ [(False, None), (True, "invalid_strategy")],
+ ids=["missing-without-worker", "invalid-with-worker"],
+)
def test_scoped_deadline_recovers_interrupted_inventory_before_final_publication(
tmp_path: Path, with_worker: bool, inventory: object
):
@@ -476,9 +513,8 @@ def test_mid_merge_failure_removes_only_created_recovery_evidence(
assert unrelated.read_text() == "Unrelated worker evidence.\n"
[email protected]("snapshot_failure", [False, True])
def test_failed_head_snapshot_does_not_interrupt_publication_rollback(
- tmp_path, monkeypatch, workbench_api, snapshot_failure
+ tmp_path, monkeypatch, workbench_api
):
from workbench import retained_coverage, saved_sources
@@ -494,7 +530,7 @@ def test_failed_head_snapshot_does_not_interrupt_publication_rollback(
failed_snapshots = []
def fail_before_snapshot_directory(directory, relative, contents):
- if snapshot_failure and "/checkpoint-heads/" in relative:
+ if "/checkpoint-heads/" in relative:
assert not (directory / relative).parent.exists()
failed_snapshots.append(relative)
raise OSError("Synthetic head snapshot write failure")
@@ -512,7 +548,7 @@ def test_failed_head_snapshot_does_not_interrupt_publication_rollback(
record = workbench_api["require_scan"](connection, scan.scan_id)
with pytest.raises(sqlite3.IntegrityError, match="synthetic publication failure"):
saved.preserve_deep_deadline_draft(SimpleNamespace(**workbench_api), connection, record)
- assert bool(failed_snapshots) is snapshot_failure
+ assert failed_snapshots
assert all(not (scan.directory / path).exists() for path in failed_snapshots)
after = {
path.relative_to(scan.directory): (path.read_bytes(), path.stat().st_mtime_ns)
diff --git a/plugins/codex-security/tests/test_deep_scan_deadline_recovery.py b/plugins/codex-security/tests/test_deep_scan_deadline_recovery.py
index d86b711de..c35ddcaad 100644
--- a/plugins/codex-security/tests/test_deep_scan_deadline_recovery.py
+++ b/plugins/codex-security/tests/test_deep_scan_deadline_recovery.py
@@ -8,6 +8,7 @@ import sqlite3
import traceback
import pytest
+from deep_deadline_crash import COMMIT_EXIT, finish_until_commit
from test_deep_scan_stop_conditions import (
canceled_discovery_output,
complete,
@@ -94,23 +95,13 @@ def test_deadline_completion_preserves_existing_tasks_and_recovered_findings(
def interrupt_finish_before_commit(workbench_api, database, scan):
child = os.fork()
if child == 0:
- connection = sqlite3.connect(database)
- connection.row_factory = sqlite3.Row
-
- class CrashBeforeCommit:
- def __getattr__(self, name):
- return getattr(connection, name)
-
- def commit(self):
- os._exit(0)
-
try:
- finish(workbench_api, CrashBeforeCommit(), scan, terminal_reason="capped")
+ finish_until_commit(workbench_api["deep_scan"], database, scan.scan_id, scan.scan_dir)
except BaseException:
traceback.print_exc()
os._exit(1)
_, status = os.waitpid(child, 0)
- assert os.waitstatus_to_exitcode(status) == 0
+ assert os.waitstatus_to_exitcode(status) == COMMIT_EXIT
def test_discarded_worker_finding_leaves_its_review_pending(
diff --git a/plugins/codex-security/tests/test_deep_scan_deadline_surface_links.py b/plugins/codex-security/tests/test_deep_scan_deadline_surface_links.py
index 13b693904..b0804fecf 100644
--- a/plugins/codex-security/tests/test_deep_scan_deadline_surface_links.py
+++ b/plugins/codex-security/tests/test_deep_scan_deadline_surface_links.py
@@ -5,8 +5,8 @@ import os
import sqlite3
import subprocess
import sys
-from pathlib import Path
+import deep_deadline_crash
import pytest
from test_deep_scan_stop_conditions import canceled_discovery_output, complete, finish
from test_deep_scan_stop_conditions import deadline_scan as deadline_scan
@@ -30,44 +30,25 @@ def pending(identity, reason, surface_id="entry"):
def interrupt_in_fresh_python(database, scan, hash_seed):
- script = Path(__file__).resolve().parents[1] / "scripts" / "workbench_db.py"
result = subprocess.run(
[
sys.executable,
"-B",
- "-c",
- """
-import runpy
-import sys
-from pathlib import Path
-from types import SimpleNamespace
-sys.path.insert(0, str(Path(sys.argv[1]).parents[1] / "tests"))
-from test_deep_scan_deadline_recovery import interrupt_finish_before_commit
-
-api = runpy.run_path(sys.argv[1], run_name="surface_link_recovery_test")
-api["deep_scan"]._dependencies = SimpleNamespace(
- **api, preserve_stopped_results=api["preserve_stopped_results_after_transition"]
-)
-scan = SimpleNamespace(scan_id=sys.argv[3], scan_dir=Path(sys.argv[4]))
-interrupt_finish_before_commit(api, Path(sys.argv[2]), scan)
-""",
- str(script),
+ deep_deadline_crash.__file__,
str(database),
scan.scan_id,
str(scan.scan_dir),
],
- cwd=Path(__file__).parent,
env={**os.environ, "PYTHONHASHSEED": str(hash_seed)},
capture_output=True,
text=True,
)
- assert result.returncode == 0, result.stdout + result.stderr
+ assert result.returncode == deep_deadline_crash.COMMIT_EXIT, result.stdout + result.stderr
- not hasattr(os, "fork"), reason="Crash boundary uses a forked workbench process."
+ "parent_collision", [pytest.param(False, marks=pytest.mark.cross_platform), True]
)
[email protected]("parent_collision", [False, True])
def test_deadline_surface_links_follow_sources_across_repeated_crashes(
workbench_api, workbench_db, deadline_scan, tmp_path, parent_collision
):
@@ -276,9 +257,6 @@ def test_stopped_recovery_preserves_source_link_union(workbench_api, tmp_path):
assert {path: path.read_bytes() for path in originals} == originals
- not hasattr(os, "fork"), reason="Crash boundary uses a forked workbench process."
-)
@pytest.mark.parametrize("changed_task", [False, True], ids=["copied-task", "updated-task"])
def test_deadline_retains_equal_task_origins_from_selected_worker_result(
workbench_api, workbench_db, deadline_scan, tmp_path, changed_task
@@ -351,9 +329,6 @@ def test_deadline_retains_equal_task_origins_from_selected_worker_result(
assert {path: path.read_bytes() for path in originals} == originals
- not hasattr(os, "fork"), reason="Crash boundary uses a forked workbench process."
-)
@pytest.mark.parametrize(
"selected_result", [False, True], ids=["raw-checkpoint", "selected-result"]
)
@@ -428,9 +403,6 @@ def test_deadline_dropped_legacy_surface_does_not_rebind_its_pending_task(
assert {path: path.read_bytes() for path in originals} == originals
- not hasattr(os, "fork"), reason="Crash boundary uses a forked workbench process."
-)
@pytest.mark.parametrize(
"history",
["parent-collision", "current-checkpoints", "retry-checkpoints", "coalesced-history"],
@@ -523,9 +495,6 @@ def test_deadline_inherited_surface_links_follow_historical_observations(
assert {path: path.read_bytes() for path in originals} == originals
- not hasattr(os, "fork"), reason="Crash boundary uses a forked workbench process."
-)
@pytest.mark.parametrize(
("changed_surface_id", "legacy_duplicate_id"),
[(False, False), (True, False), (True, True)],
diff --git a/plugins/codex-security/tests/test_deep_scan_publication_authority.py b/plugins/codex-security/tests/test_deep_scan_publication_authority.py
index b8ccb096c..cba3c41f7 100644
--- a/plugins/codex-security/tests/test_deep_scan_publication_authority.py
+++ b/plugins/codex-security/tests/test_deep_scan_publication_authority.py
@@ -338,12 +338,18 @@ def emulate_windows_atomic_write(monkeypatch, finalizer, files, *, reparse_point
return backend
[email protected]("workflow", ["deep-scan-mcp/v1", "deep-security-scan/v1"])
@pytest.mark.parametrize(
- ("backend_kind", "failure"),
- [("host", failure) for failure in ("write", "rename")]
- + [("posix", failure) for failure in ("root-stat", "root-resolve", "root-open", "parent-open")]
- + [("windows-emulated", failure) for failure in ("create", "write", "rename")],
+ ("workflow", "backend_kind", "failure"),
+ [
+ ("deep-security-scan/v1", backend, failure)
+ for backend, failures in (
+ ("host", ("write", "rename")),
+ ("posix", ("root-stat", "root-resolve", "root-open", "parent-open")),
+ ("windows-emulated", ("create", "write", "rename")),
+ )
+ for failure in failures
+ ]
+ + [("deep-scan-mcp/v1", "host", "write")],
)
def test_receipt_io_failure_preserves_successful_publication(
workbench_api, workbench_db, publication_scan, monkeypatch, workflow, backend_kind, failure
@@ -470,7 +476,6 @@ def test_receipt_io_failure_preserves_successful_publication(
assert len(list((scan.scan_dir / "checkpoints").glob("*.json"))) == 2
[email protected]("workflow", ["deep-scan-mcp/v1", "deep-security-scan/v1"])
@pytest.mark.parametrize(
("operation", "failure"),
[
@@ -490,7 +495,6 @@ def test_windows_receipt_root_probe_classifies_storage_and_path_failures(
publication_scan,
monkeypatch,
tmp_path,
- workflow,
operation,
failure,
):
@@ -498,9 +502,8 @@ def test_windows_receipt_root_probe_classifies_storage_and_path_failures(
result = add_worker(workbench_db, scan)
with workbench_db:
workbench_db.execute(
- "UPDATE deep_scan_runs SET workflow_version = ?, coordinator_generation = 2 "
- "WHERE scan_id = ?",
- (workflow, scan.scan_id),
+ "UPDATE deep_scan_runs SET coordinator_generation = 2 WHERE scan_id = ?",
+ (scan.scan_id,),
)
workbench_db.execute(
"UPDATE deep_scan_workers SET kind = 'dedup', merge_state = 'none' WHERE scan_id = ?",
diff --git a/plugins/codex-security/tests/test_deep_scan_request_identity.py b/plugins/codex-security/tests/test_deep_scan_request_identity.py
index da5f32d51..b0cbd048c 100644
--- a/plugins/codex-security/tests/test_deep_scan_request_identity.py
+++ b/plugins/codex-security/tests/test_deep_scan_request_identity.py
@@ -59,12 +59,10 @@ def test_target_request_does_not_reuse_different_user_context(
(None, "Review SQL injection only."),
),
)
[email protected]("request_original", (False, True))
def test_terminal_reuse_matches_discovery_context_after_edit(
tmp_path: Path,
original_context: str | None,
edited_context: str | None,
- request_original: bool,
) -> None:
state_dir, codex_home = tmp_path / "state", tmp_path / "codex-home"
target, scan_root = tmp_path / "target", tmp_path / "scans"
@@ -87,19 +85,18 @@ def test_terminal_reuse_matches_discovery_context_after_edit(
)
assert edited["scan"]["userContext"] == edited_context
- requested = begin_target_scan(
- state_dir,
- codex_home,
- target,
- scan_root,
- thread_id="thread-new-request",
- user_context=original_context if request_original else edited_context,
- )
- assert requested["startDisposition"] == ("joined" if request_original else "created")
- assert (requested["deepScan"]["scanId"] == scan_id) is request_original
- assert requested["deepScan"]["userContext"] == (
- original_context if request_original else edited_context
- )
+ for context, disposition in ((original_context, "joined"), (edited_context, "created")):
+ requested = begin_target_scan(
+ state_dir,
+ codex_home,
+ target,
+ scan_root,
+ thread_id="thread-new-request",
+ user_context=context,
+ )
+ assert requested["startDisposition"] == disposition
+ assert (requested["deepScan"]["scanId"] == scan_id) == (disposition == "joined")
+ assert requested["deepScan"]["userContext"] == context
assert (
run_workbench(state_dir, "get-scan", "--scan-id", scan_id)["scan"]["userContext"]
== edited_context
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 9deb22653..133c56780 100644
--- a/plugins/codex-security/tests/test_deep_scan_successful_publication.py
+++ b/plugins/codex-security/tests/test_deep_scan_successful_publication.py
@@ -344,6 +344,13 @@ def test_deep_prepare_and_complete_preserve_the_same_aggregate(
complete(workbench_api, workbench_db, scan, prepare_only=True)
complete(workbench_api, workbench_db, scan)
+ # A capped scan with a failed worker cannot publish anew, but its sealed result can be read.
+ add_worker(workbench_db, scan, status="failed")
+ with workbench_db:
+ workbench_db.execute(
+ "UPDATE deep_scan_runs SET terminal_reason = 'capped' WHERE scan_id = ?",
+ (scan.scan_id,),
+ )
repeated = complete(workbench_api, workbench_db, scan)
assert repeated["progress"]["status"] == "complete"
@@ -422,9 +429,8 @@ def test_completion_preserves_non_utf8_filename_warning(
@pytest.mark.cross_platform
@pytest.mark.parametrize("scope", [".", "subdir"])
[email protected]("missing_parent", [False, True])
def test_deep_completion_preserves_findings_with_missing_optional_writeups(
- workbench_api, workbench_db, publication_scan, scope, missing_parent
+ workbench_api, workbench_db, publication_scan, scope
):
scan = publication_scan(scope=scope)
valid_path = "findings/valid/valid.md"
@@ -432,16 +438,17 @@ def test_deep_completion_preserves_findings_with_missing_optional_writeups(
valid_file.parent.mkdir(parents=True)
valid_file.write_text("# Retained writeup\n")
scan.findings[0]["writeup"] = {"reportPath": valid_path}
- missing = copy.deepcopy(scan.findings[0])
- missing["identity"]["anchor"] = "missing-writeup"
- missing["summary"] = "A separate finding whose optional writeup was not transferred."
- missing_path = "findings/missing/missing.md"
- if not missing_parent:
- (scan.scan_dir / missing_path).parent.mkdir()
- missing["writeup"] = {"reportPath": missing_path}
- scan.findings.append(missing)
+ (scan.scan_dir / "findings" / "missing-file").mkdir()
+ missing_findings = []
+ for anchor in ("missing-file", "missing-directory"):
+ finding = copy.deepcopy(scan.findings[0])
+ finding["identity"]["anchor"] = anchor
+ finding["summary"] = f"An independent finding with a missing writeup ({anchor})."
+ finding["writeup"] = {"reportPath": f"findings/{anchor}/{anchor}.md"}
+ missing_findings.append(finding)
+ scan.findings.extend(missing_findings)
if scope != ".":
- excluded = copy.deepcopy(missing)
+ excluded = copy.deepcopy(missing_findings[0])
excluded["identity"]["anchor"] = "excluded-writeup"
excluded["locations"] = [{"path": "unrelated/extract.py", "startLine": 1}]
excluded["writeup"] = {"reportPath": "findings/excluded/excluded.md"}
@@ -467,20 +474,21 @@ def test_deep_completion_preserves_findings_with_missing_optional_writeups(
completed = complete(workbench_api, workbench_db, scan)
assert completed["progress"]["status"] == "complete"
- assert completed["findingCount"] == 2
- assert len(completed["warnings"]) == 1
+ assert completed["findingCount"] == 3
assert completed["warnings"] == prepared["warnings"]
- assert completed["warnings"][0].startswith("Skipped missing writeup for finding 2:")
- assert "findings[1].writeup.reportPath" in completed["warnings"][0]
+ assert len(completed["warnings"]) == 2
if scope != ".":
scan.findings.pop(0)
- missing.pop("writeup")
+ report = (scan.scan_dir / "report.md").read_text()
+ for index, (finding, warning) in enumerate(zip(missing_findings, completed["warnings"]), 1):
+ assert warning.startswith(f"Skipped missing writeup for finding {index + 1}:")
+ assert f"findings[{index}].writeup.reportPath" in warning
+ missing_path = finding.pop("writeup")["reportPath"]
+ assert missing_path not in report
+ assert finding["summary"] in report
assert_published_aggregate(scan)
assert {path: path.read_bytes() for path in original_files} == original_files
- report = (scan.scan_dir / "report.md").read_text()
assert valid_path in report
- assert missing_path not in report
- assert missing["summary"] in report
manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())
assert manifest["scan"]["status"] == "completed"
assert manifest["scan"]["sealedAt"]
@@ -975,21 +983,3 @@ def test_stopped_file_scope_filters_siblings_before_assigning_identity(
)
assert {name: (scan.scan_dir / name).read_bytes() for name in names} == published
assert checkpoint.read_bytes() == checkpoint_bytes
-
-
-def test_already_sealed_legacy_capped_scan_remains_immutable(
- workbench_api, workbench_db, publication_scan
-):
- scan = publication_scan()
- complete(workbench_api, workbench_db, scan)
- names = ("scan-manifest.json", "findings.json", "coverage.json")
- sealed = {name: (scan.scan_dir / name).read_bytes() for name in names}
- add_worker(workbench_db, scan, status="failed")
- with workbench_db:
- workbench_db.execute(
- "UPDATE deep_scan_runs SET terminal_reason = 'capped' WHERE scan_id = ?",
- (scan.scan_id,),
- )
- repeated = complete(workbench_api, workbench_db, scan)
- assert repeated["progress"]["status"] == "complete"
- assert {name: (scan.scan_dir / name).read_bytes() for name in names} == sealed
diff --git a/plugins/codex-security/tests/test_deep_security_scan_concurrency_guard.py b/plugins/codex-security/tests/test_deep_security_scan_concurrency_guard.py
index d8bdbc506..84093455e 100644
--- a/plugins/codex-security/tests/test_deep_security_scan_concurrency_guard.py
+++ b/plugins/codex-security/tests/test_deep_security_scan_concurrency_guard.py
@@ -1,69 +1,67 @@
from __future__ import annotations
-import sqlite3
+import uuid
+from argparse import Namespace
from pathlib import Path
-from workbench_test_support import (
- create_saved_git_workspace,
- fail_scan,
- get_scan,
- start_scan_command,
-)
-
-def test_scan_context_reports_only_other_running_deep_scans(tmp_path: Path) -> None:
- state_dir = tmp_path / "state"
- targets = {
- name: tmp_path / name for name in ("current", "other", "standard", "failed", "complete")
- }
- for target in targets.values():
+def test_scan_context_reports_only_other_running_deep_scans(
+ workbench_api, workbench_db, tmp_path: Path
+) -> None:
+ scans = {}
+ for name, mode, status in (
+ ("current", "deep", "running"),
+ ("other", "deep", "running"),
+ ("standard", "standard", "running"),
+ ("failed", "deep", "failed"),
+ ("complete", "deep", "complete"),
+ ):
+ target = tmp_path / name
target.mkdir()
+ workspace_id = str(uuid.uuid4())
+ now = workbench_api["now"]()
+ with workbench_db:
+ workbench_db.execute(
+ "INSERT INTO workspaces (id, target_path, default_mode, submitted, created_at, "
+ "updated_at) VALUES (?, ?, ?, 1, ?, ?)",
+ (workspace_id, str(target), mode, now, now),
+ )
+ scans[name] = workbench_api["start_scan"](
+ workbench_db,
+ Namespace(
+ workspace_id=workspace_id,
+ scan_root=str(tmp_path / "scans"),
+ model=None,
+ reasoning_effort=None,
+ ),
+ )["results"]
+ if status != "running":
+ with workbench_db:
+ workbench_db.execute(
+ "UPDATE scans SET status = ?, completed_at = updated_at WHERE id = ?",
+ (status, scans[name]["scanId"]),
+ )
- workspaces = {
- name: create_saved_git_workspace(
- state_dir,
- target,
- mode="standard" if name == "standard" else "deep",
- )
- for name, target in targets.items()
- }
- scans = {
- name: start_scan_command(state_dir, str(workspace["id"]))
- for name, workspace in workspaces.items()
- }
-
- failed_scan_id = str(scans["failed"]["results"]["scanId"])
- other_scan_id = str(scans["other"]["results"]["scanId"])
- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
- connection.execute(
+ with workbench_db:
+ workbench_db.execute(
"UPDATE scans SET handoff_status = 'delivered' WHERE id IN (?, ?)",
- (failed_scan_id, other_scan_id),
- )
- fail_scan(state_dir, failed_scan_id, "Stopped for the fixture.")
- complete_scan_id = str(scans["complete"]["results"]["scanId"])
- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
- connection.execute(
- """
- UPDATE scans
- SET status = 'complete', completed_at = updated_at
- WHERE id = ?
- """,
- (complete_scan_id,),
+ (scans["other"]["scanId"], scans["failed"]["scanId"]),
)
-
- current_scan_id = str(scans["current"]["results"]["scanId"])
- context = get_scan(state_dir, current_scan_id)
-
- assert context["otherRunningDeepScans"] == [
+ current = Namespace(scan_id=scans["current"]["scanId"], occurrence_id=None)
+ other = scans["other"]
+ assert workbench_api["get_scan"](workbench_db, current)["otherRunningDeepScans"] == [
{
"phase": "preflight",
- "scanId": other_scan_id,
- "startedAt": scans["other"]["results"]["updatedAt"],
- "targetPath": str(targets["other"].resolve()),
- "updatedAt": scans["other"]["results"]["updatedAt"],
+ "scanId": other["scanId"],
+ "startedAt": other["updatedAt"],
+ "targetPath": str((tmp_path / "other").resolve()),
+ "updatedAt": other["updatedAt"],
}
]
- fail_scan(state_dir, other_scan_id, "Stopped for the fixture.")
- context = get_scan(state_dir, current_scan_id)
- assert context["otherRunningDeepScans"] == []
+ with workbench_db:
+ workbench_db.execute(
+ "UPDATE scans SET status = 'failed', completed_at = updated_at WHERE id = ?",
+ (other["scanId"],),
+ )
+ assert workbench_api["get_scan"](workbench_db, current)["otherRunningDeepScans"] == []
diff --git a/plugins/codex-security/tests/test_runtime_settings.py b/plugins/codex-security/tests/test_runtime_settings.py
index d6051a4f0..bf078ec05 100644
--- a/plugins/codex-security/tests/test_runtime_settings.py
+++ b/plugins/codex-security/tests/test_runtime_settings.py
@@ -1,17 +1,12 @@
from __future__ import annotations
-import builtins
import runpy
import sys
from pathlib import Path
+from types import ModuleType
import pytest
-try:
- import tomllib
-except ModuleNotFoundError: # pragma: no cover - Python 3.10 only
- import tomli as tomllib
-
SCRIPTS = Path(__file__).resolve().parents[1] / "scripts"
@@ -91,15 +86,11 @@ def test_config_and_storage_preserve_literal_home_spaces(tmp_path, monkeypatch,
assert (selected_state / "existing-state.txt").read_text(encoding="utf-8") == "selected state\n"
-def test_deep_config_falls_back_to_tomli_without_stdlib_tomllib(monkeypatch):
- real_import = builtins.__import__
- monkeypatch.setitem(sys.modules, "tomli", tomllib)
+def test_toml_loader_uses_tomli_when_the_stdlib_module_is_missing(monkeypatch):
+ fallback = ModuleType("tomli")
+ monkeypatch.setitem(sys.modules, "tomllib", None)
+ monkeypatch.setitem(sys.modules, "tomli", fallback)
- def import_without_tomllib(name, globals=None, locals=None, fromlist=(), level=0):
- if name == "tomllib":
- raise ModuleNotFoundError("No module named 'tomllib'", name="tomllib")
- return real_import(name, globals, locals, fromlist, level)
+ namespace = runpy.run_path(str(SCRIPTS / "workbench" / "runtime_toml.py"))
- monkeypatch.setattr(builtins, "__import__", import_without_tomllib)
- namespace = runpy.run_path(str(SCRIPTS / "deep_scan_config.py"))
- assert namespace["tomllib"] is tomllib
+ assert namespace["tomllib"] is fallback
diff --git a/plugins/codex-security/tests/test_workbench_deep_scan.py b/plugins/codex-security/tests/test_workbench_deep_scan.py
index 7f9a4d31d..75146583c 100644
--- a/plugins/codex-security/tests/test_workbench_deep_scan.py
+++ b/plugins/codex-security/tests/test_workbench_deep_scan.py
@@ -808,10 +808,7 @@ def test_expired_generation_refunds_shutdown_canceled_discovery(
)
- "mutation", ("worker", "progress", "failure", "dedup", "commit", "finish", "renewal")
-)
-def test_expired_generation_cannot_mutate_recovered_scan(tmp_path: Path, mutation: str) -> None:
+def test_expired_generation_cannot_mutate_recovered_scan(tmp_path: Path) -> None:
state_dir, codex_home, target = tmp_path / "state", tmp_path / "codex-home", tmp_path / "target"
target.mkdir()
run = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")["deepScan"]
@@ -828,14 +825,10 @@ def test_expired_generation_cannot_mutate_recovered_scan(tmp_path: Path, mutatio
)
result.write_text("{}\n")
expire_deep_scan_coordinator(state_dir, scan_id)
- assert (
- claim_deep_scan_coordinator(state_dir, codex_home, scan_id)["deepScan"][
- "coordinatorGeneration"
- ]
- == 3
- )
- operations = {
- "worker": (
+ recovered = claim_deep_scan_coordinator(state_dir, codex_home, scan_id)["deepScan"]
+ assert (recovered["status"], recovered["coordinatorGeneration"]) == ("running", 3)
+ operations = (
+ (
"upsert-deep-scan-worker",
"--worker-id",
worker,
@@ -850,9 +843,9 @@ def test_expired_generation_cannot_mutate_recovered_scan(tmp_path: Path, mutatio
"--result-manifest-path",
str(result),
),
- "progress": ("update-progress", "--phase", "discovery"),
- "failure": ("fail-deep-scan", "--message", "stale coordinator"),
- "dedup": (
+ ("update-progress", "--phase", "discovery"),
+ ("fail-deep-scan", "--message", "stale coordinator"),
+ (
"claim-deep-scan-dedup",
"--worker-id",
str(uuid.uuid4()),
@@ -863,7 +856,7 @@ def test_expired_generation_cannot_mutate_recovered_scan(tmp_path: Path, mutatio
"--input-worker-id",
worker,
),
- "commit": (
+ (
"commit-deep-scan-dedup",
"--worker-id",
worker,
@@ -872,33 +865,32 @@ def test_expired_generation_cannot_mutate_recovered_scan(tmp_path: Path, mutatio
"--new-findings-count",
"0",
),
- "finish": (
+ (
"finish-deep-scan",
"--terminal-reason",
"capped",
"--manifest-path",
str(result),
),
- "renewal": ("claim-deep-scan-coordinator", "--thread-id", "thread-deep-scan"),
- }
- command, *arguments = operations[mutation]
- rejected = scan_command(
- state_dir,
- command,
- scan_id,
- *arguments,
- "--coordinator-generation",
- "2",
- check=False,
- environment=deep_environment(codex_home),
+ ("claim-deep-scan-coordinator", "--thread-id", "thread-deep-scan"),
)
-
- assert rejected["returncode"] != 0
- assert "coordinator" in str(rejected["stderr"]).lower()
- persisted = get_deep_scan(
- state_dir, scan_id, "thread-deep-scan", environment=deep_environment(codex_home)
- )["deepScan"]
- assert (persisted["status"], persisted["coordinatorGeneration"]) == ("running", 3)
+ for command, *arguments in operations:
+ rejected = scan_command(
+ state_dir,
+ command,
+ scan_id,
+ *arguments,
+ "--coordinator-generation",
+ "2",
+ check=False,
+ environment=deep_environment(codex_home),
+ )
+ assert rejected["returncode"] != 0, command
+ assert "coordinator" in str(rejected["stderr"]).lower(), command
+ persisted = get_deep_scan(
+ state_dir, scan_id, "thread-deep-scan", environment=deep_environment(codex_home)
+ )["deepScan"]
+ assert persisted == recovered, command
@pytest.mark.parametrize(
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 e5a62c98d..0988f540c 100644
--- a/plugins/codex-security/tests/test_workbench_standard_deep_results.py
+++ b/plugins/codex-security/tests/test_workbench_standard_deep_results.py
@@ -1444,21 +1444,7 @@ def test_stopped_scan_rebinds_prepared_completion_seal(tmp_path: Path) -> None:
assert seal_digest
- "command",
- [
- ("request-finding-remediation",),
- ("request-finding-remediation-action", "--expected-version", "1", "--action", "apply"),
- ("claim-finding-remediation-resend",),
- ("mark-finding-remediation-delivered",),
- ("release-finding-remediation-claim",),
- ("cancel-finding-remediation-request",),
- ("set-finding-remediation", "--expected-version", "1", "--state", "failed"),
- ],
-)
-def test_stopped_findings_cannot_enter_remediation(
- tmp_path: Path, command: tuple[str, ...]
-) -> None:
+def test_stopped_findings_cannot_enter_remediation(tmp_path: Path) -> None:
state_dir, codex_home, target, scan_dir, scan_id = deep_scan_fixture(tmp_path)
_, result_path = accepted_standard_worker(state_dir, codex_home, scan_dir, scan_id)
contract_dir = standard_contract(tmp_path, scan_id, target)
@@ -1473,20 +1459,36 @@ def test_stopped_findings_cannot_enter_remediation(
)
occurrence_id = failed["findings"][0]["occurrenceId"]
- blocked = run_workbench(
- state_dir,
- *command,
- "--occurrence-id",
- occurrence_id,
- "--request-id",
- str(uuid.uuid4()),
- "--action-token",
- str(uuid.uuid4()),
- check=False,
+ commands = (
+ ("request-finding-remediation",),
+ ("request-finding-remediation-action", "--expected-version", "1", "--action", "apply"),
+ ("claim-finding-remediation-resend",),
+ ("mark-finding-remediation-delivered",),
+ ("release-finding-remediation-claim",),
+ ("cancel-finding-remediation-request",),
+ ("set-finding-remediation", "--expected-version", "1", "--state", "failed"),
)
+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
+ for command in commands:
+ blocked = run_workbench(
+ state_dir,
+ *command,
+ "--occurrence-id",
+ occurrence_id,
+ "--request-id",
+ str(uuid.uuid4()),
+ "--action-token",
+ str(uuid.uuid4()),
+ check=False,
+ )
- assert blocked["returncode"] != 0
- assert "successfully completed scans" in blocked["stderr"]
+ assert blocked["returncode"] != 0, command[0]
+ assert "successfully completed scans" in blocked["stderr"], command[0]
+ attempt = connection.execute(
+ "SELECT 1 FROM finding_remediation_attempts WHERE occurrence_id = ?",
+ (occurrence_id,),
+ ).fetchone()
+ assert attempt is None, command[0]
@pytest.mark.parametrize("explicit_complete", [True, False], ids=["explicit", "default"])