Skip to content

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

fix(deep-scan): recover the latest public findings on interruption (#2310)

Michael D'Angelo2aea01bee0af
diff --git a/plugins/codex-security/scripts/workbench/retained_coverage.py b/plugins/codex-security/scripts/workbench/retained_coverage.py
index f247c1c4c..b4e394d3b 100644
--- a/plugins/codex-security/scripts/workbench/retained_coverage.py
+++ b/plugins/codex-security/scripts/workbench/retained_coverage.py
@@ -70,6 +70,7 @@ from workbench.saved_sources import (
_frozen_checkpoint_heads,
_frozen_source_times,
_is_source_order_snapshot,
+ _iteration_order,
_latest_iteration,
_model_capture,
_parent_publication_ancestors,
@@ -679,7 +680,7 @@ def merge_saved_results(
workers_by_id = {worker["id"]: worker for worker in workers}
latest_reducer_key = (
- (reducer["completed_at"] or "", reducer["id"], int(reducer["attempt"] or 0))
+ (_iteration_order(reducer), int(reducer["attempt"] or 0))
if reducer is not None and latest_reducer in drafts_by_path
else None
)
@@ -692,7 +693,7 @@ def merge_saved_results(
):
continue
current_results.add(result_path)
- candidate_key = (worker["completed_at"] or "", worker["id"], attempt)
+ candidate_key = (_iteration_order(worker), attempt)
if latest_reducer_key is None or candidate_key > latest_reducer_key:
latest_reducer_key = candidate_key
latest_reducer = result_path
diff --git a/plugins/codex-security/scripts/workbench/saved_sources.py b/plugins/codex-security/scripts/workbench/saved_sources.py
index 9b1c6aebf..55caff0a6 100644
--- a/plugins/codex-security/scripts/workbench/saved_sources.py
+++ b/plugins/codex-security/scripts/workbench/saved_sources.py
@@ -241,14 +241,18 @@ def _read_saved_result(
return draft, _digest(draft), int(observed)
if draft.get("scanId") != scan_id:
raise ContractError("checkpoint belongs to a different scan")
- if not _is_source_order_snapshot(relative) and (
- not isinstance(draft.get("findings"), list)
- or not isinstance(
- draft.get("reviewState", draft.get("coverage", {} if kind == "dedup" else None)), dict
- )
+ if _is_source_order_snapshot(relative):
+ return draft, _digest(draft), metadata.st_mtime_ns
+ if not isinstance(draft.get("findings"), list) or not isinstance(
+ draft.get("reviewState", draft.get("coverage", {} if kind == "dedup" else None)), dict
):
raise ContractError("checkpoint has no semantic findings or coverage")
digest = _digest(draft)
+ if kind == "dedup" and normalize_review:
+ # The host keeps grouping state in iteration checkpoints for resume only.
+ for finding in draft["findings"]:
+ if isinstance(finding, dict) and isinstance(finding.get("provenance"), dict):
+ finding["provenance"].pop("deepScanRepairGroups", None)
if normalize_review and "coverage" in draft and "reviewState" not in draft:
draft["reviewState"] = draft.pop("coverage")
return draft, digest, metadata.st_mtime_ns
@@ -651,13 +655,15 @@ if __name__ == "__main__":
argparse.ArgumentParser(description=__doc__).parse_args()
-def _latest_iteration(workers: Iterable[Any]) -> Any | None:
- def iteration_order(worker: Any) -> tuple[int, str, str]:
- output = Path(worker["result_manifest_path"]).parent
- label = output.parent.name if output.name == "output" else output.name
- match = re.fullmatch(r"dedup-([0-9]+)", label)
- return (int(match[1]) if match else 0, worker["completed_at"] or "", worker["id"])
+def _iteration_order(worker: Any) -> tuple[int, str, str]:
+ result = worker["result_manifest_path"]
+ output = Path(result).parent if result else Path(worker["artifact_dir"])
+ label = output.parent.name if output.name == "output" else output.name
+ match = re.fullmatch(r"dedup-([0-9]+)", label)
+ return (int(match[1]) if match else 0, worker["completed_at"] or "", worker["id"])
+
+def _latest_iteration(workers: Iterable[Any]) -> Any | None:
return max(
(
worker
@@ -666,7 +672,7 @@ def _latest_iteration(workers: Iterable[Any]) -> Any | None:
and worker["status"] == "succeeded"
and worker["result_manifest_path"]
),
- key=iteration_order,
+ key=_iteration_order,
default=None,
)
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 23f814370..2c6e759bf 100644
--- a/plugins/codex-security/tests/test_workbench_standard_deep_results.py
+++ b/plugins/codex-security/tests/test_workbench_standard_deep_results.py
@@ -1663,6 +1663,53 @@ def test_canceled_reducer_checkpoint_supersedes_discovery_result(tmp_path: Path)
assert coverage["surfaces"][0]["notes"] == "Reducer-validated merged evidence."
+def test_stopped_scan_recovers_latest_numbered_iteration_after_upgrade(tmp_path: Path) -> None:
+ state_dir, codex_home, target, scan_dir, scan_id = deep_scan_fixture(tmp_path, workers=2)
+ first_id, first = accepted_standard_worker(state_dir, codex_home, scan_dir, scan_id)
+ legacy_id, legacy = running_standard_reducer(
+ state_dir, codex_home, scan_dir, scan_id, first_id, name="dedup-0001"
+ )
+ legacy.write_text(first.read_text())
+ write_checkpoint(legacy.parent / "checkpoints", json.loads(legacy.read_text()))
+ seal_legacy_reducer(state_dir, scan_id, legacy_id, str(legacy), "0")
+
+ worker_id, _ = accepted_standard_worker(
+ state_dir, codex_home, scan_dir, scan_id, name="second-worker"
+ )
+ finding = standard_finding(tmp_path, scan_id, target)
+ finding["provenance"]["deepScanRepairGroups"] = [{"order": 0, "sources": [f"{worker_id}:1"]}]
+ _, _, result = worker_paths(scan_dir, "dedup-0002")
+ model = {"summary": "The most recent scan found another trust boundary."}
+ result.write_text(json.dumps({"scanId": scan_id, "findings": [finding], "threatModel": model}))
+ scan_command(
+ state_dir,
+ "record-deep-scan-iteration",
+ scan_id,
+ "--worker-id",
+ str(uuid.UUID(int=1)),
+ "--result-manifest-path",
+ str(result),
+ "--input-worker-id",
+ worker_id,
+ "--no-new-streak",
+ "0",
+ "--consecutive-errors",
+ "0",
+ )
+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
+ connection.execute(
+ "UPDATE deep_scan_workers SET completed_at = ? WHERE scan_id = ? AND kind = 'dedup'",
+ ("2026-10-27T00:00:00Z", scan_id),
+ )
+ fail_deep_scan(state_dir, codex_home, scan_id)
+ assert json.loads((scan_dir / "scan-manifest.json").read_text())["scan"]["threatModel"] == model
+ retained = json.loads((scan_dir / "findings.json").read_text())["findings"]
+ assert len(retained) == 1
+ assert retained[0]["summary"] == finding["summary"]
+ assert "deepScanRepairGroups" not in retained[0]["provenance"]
+ assert json.loads(result.read_text())["findings"] == [finding]
+
+
def test_archived_reducer_checkpoint_supersedes_discovery_result(tmp_path: Path) -> None:
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)