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(history): order automatic matches by scan start time (#1779)

Michael D'Angelo92483fc9f0ab
diff --git a/plugins/codex-security/scripts/workbench_scan_history.py b/plugins/codex-security/scripts/workbench_scan_history.py
index 31fb064e6..11e705d46 100644
--- a/plugins/codex-security/scripts/workbench_scan_history.py
+++ b/plugins/codex-security/scripts/workbench_scan_history.py
@@ -384,19 +384,19 @@ def list_unmatched_scan_pairs(
).fetchone()
selected = [
scan
- for scan in connection.execute(
- "SELECT * FROM scans WHERE status = 'complete' ORDER BY started_at, id"
- )
+ for scan in connection.execute("SELECT * FROM scans WHERE status = 'complete'")
if _same_repository(scan, requested)
]
+ selected.sort(key=lambda scan: (timestamp_key(scan["started_at"]), scan["id"]))
+
available = []
- for scan in selected:
+ for selected_index, scan in enumerate(selected):
try:
read_coverage(scan)
except SystemExit:
continue
- available.append(scan)
+ available.append((selected_index, scan))
saved_pairs = {
(row["before_scan_id"], row["after_scan_id"])
@@ -406,10 +406,10 @@ def list_unmatched_scan_pairs(
skipped = 0
matching_findings: dict[str, list[dict[str, Any]]] = {}
known_links: list[sqlite3.Row] | None = None
- for index, after in enumerate(available):
+ for index, (selected_index, after) in enumerate(available):
previous = [
before
- for before in available[:index]
+ for _, before in available[:index]
if args.force or (before["id"], after["id"]) not in saved_pairs
]
skipped += index - len(previous)
@@ -429,11 +429,7 @@ def list_unmatched_scan_pairs(
]
known_groups = _known_finding_groups(
known_links,
- {
- scan["id"]
- for scan in selected
- if (scan["started_at"], scan["id"]) <= (after["started_at"], after["id"])
- },
+ {scan["id"] for scan in selected[: selected_index + 1]},
)
batches.append(
{
diff --git a/plugins/codex-security/tests/test_workbench_scan_history.py b/plugins/codex-security/tests/test_workbench_scan_history.py
index b2cd6eb6c..22bc8338a 100644
--- a/plugins/codex-security/tests/test_workbench_scan_history.py
+++ b/plugins/codex-security/tests/test_workbench_scan_history.py
@@ -967,6 +967,103 @@ def test_list_unmatched_scan_pairs_groups_pending_pairs_and_skips_saved_results(
]
+ ("before_started", "after_started"),
+ [
+ ("2026-01-02T00:15:00+02:00", "2026-01-01T21:00:00-02:00"),
+ ("2026-01-01T00:00:00Z", "2026-01-01T00:00:00.000001Z"),
+ ("2026-01-01t02:00:00.000000+02:00", "2026-01-01T00:00:00Z"),
+ ],
+)
+def test_automatic_matching_orders_instants_and_preserves_directed_cache(
+ tmp_path: Path, before_started: str, after_started: str
+) -> None:
+ state_dir = tmp_path / "state"
+ repository = tmp_path / "repository"
+ repository.mkdir()
+ before, after = sorted(
+ (create_cli_scan(state_dir, tmp_path / "results", repository) for _ in range(2)),
+ key=lambda scan: scan["scanId"],
+ )
+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
+ connection.executemany(
+ "UPDATE scans SET started_at = ? WHERE id = ?",
+ [(before_started, before["scanId"]), (after_started, after["scanId"])],
+ )
+ save_scan_matches(state_dir, after, before)
+
+ pending = run_workbench(state_dir, "list-unmatched-scan-pairs", "--repository", str(repository))
+ assert pending["skippedPairs"] == 0
+ assert [batch["afterScanId"] for batch in pending["batches"]] == [after["scanId"]]
+ assert [scan["scanId"] for scan in pending["batches"][0]["beforeScans"]] == [before["scanId"]]
+
+ save_scan_matches(state_dir, before, after)
+ cached = run_workbench(state_dir, "list-unmatched-scan-pairs", "--repository", str(repository))
+ assert cached["batches"] == []
+ assert cached["skippedPairs"] == 1
+ forced = run_workbench(
+ state_dir, "list-unmatched-scan-pairs", "--repository", str(repository), "--force"
+ )
+ assert forced == pending
+
+
+def test_automatic_matching_keeps_unavailable_history_without_future_links(tmp_path: Path) -> None:
+ state_dir = tmp_path / "state"
+ repository = tmp_path / "repository"
+ repository.mkdir()
+ earlier, unavailable, current, future = (
+ create_cli_scan(
+ state_dir, tmp_path / "results", repository, identity_anchor=f"archive-check-{index}"
+ )
+ for index in range(4)
+ )
+ with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:
+ connection.executemany(
+ "UPDATE scans SET started_at = ? WHERE id = ?",
+ [
+ ("2026-01-01T00:00:00Z", earlier["scanId"]),
+ ("2026-01-01T03:00:00+02:00", unavailable["scanId"]),
+ ("2026-01-01T02:00:00Z", current["scanId"]),
+ ("2026-01-01T00:30:00-03:00", future["scanId"]),
+ ],
+ )
+ findings = [
+ get_scan(state_dir, scan["scanId"])["scan"]["findings"][0]
+ for scan in (unavailable, current, future)
+ ]
+ for before, after, before_finding, after_finding in (
+ (unavailable, current, findings[0], findings[1]),
+ (current, future, findings[1], findings[2]),
+ ):
+ save_scan_matches(
+ state_dir,
+ before,
+ after,
+ confirmed_match(before_finding["occurrenceId"], after_finding["occurrenceId"]),
+ )
+ shutil.rmtree(unavailable["scanDir"])
+
+ pending = run_workbench(state_dir, "list-unmatched-scan-pairs", "--repository", str(repository))
+ assert (pending["scanCount"], pending["unavailableScans"], pending["skippedPairs"]) == (4, 1, 1)
+ assert [batch["afterScanId"] for batch in pending["batches"]] == [
+ current["scanId"],
+ future["scanId"],
+ ]
+ assert [scan["scanId"] for scan in pending["batches"][0]["beforeScans"]] == [earlier["scanId"]]
+ assert pending["batches"][0]["knownFindingGroups"] == [
+ sorted(finding["findingId"] for finding in findings[:2])
+ ]
+ assert pending["batches"][1]["knownFindingGroups"] == [
+ sorted(finding["findingId"] for finding in findings)
+ ]
+
+ forced = run_workbench(
+ state_dir, "list-unmatched-scan-pairs", "--repository", str(repository), "--force"
+ )
+ assert (forced["scanCount"], forced["unavailableScans"], forced["skippedPairs"]) == (4, 1, 0)
+ assert all("knownFindingGroups" not in batch for batch in forced["batches"])
+
+
def test_list_unmatched_scan_pairs_skips_unavailable_scan_artifacts(tmp_path: Path) -> None:
state_dir = tmp_path / "state"
repository = tmp_path / "repository"
diff --git a/sdk/typescript/tests-ts/workbench-scan-history.test.ts b/sdk/typescript/tests-ts/workbench-scan-history.test.ts
index 169556bd5..9ea2526e0 100644
--- a/sdk/typescript/tests-ts/workbench-scan-history.test.ts
+++ b/sdk/typescript/tests-ts/workbench-scan-history.test.ts
@@ -330,7 +330,7 @@ test("loads each scan once and scopes saved links to uncached history", async ()
"''')",
"for index in range(3):",
" scan = f'scan-{index}'",
- " connection.execute('INSERT INTO scans VALUES (?, ?, NULL, ?, ?)', (scan, sys.argv[2], 'complete', str(index)))",
+ " connection.execute('INSERT INTO scans VALUES (?, ?, NULL, ?, ?)', (scan, sys.argv[2], 'complete', f'2026-01-01T00:00:0{index}Z'))",
" connection.execute('INSERT INTO finding_occurrences VALUES (?, ?, ?, ?, ?, ?, ?, ?)', (scan, scan, scan, '{}', 'fix', 'high', 'summary', 'title'))",
"queries = []",
"connection.set_trace_callback(queries.append)",
@@ -349,7 +349,7 @@ test("loads each scan once and scopes saved links to uncached history", async ()
"link_queries = [query for query in queries if 'FROM scan_comparison_matches' in query]",
"for index in (3, 4):",
" scan = f'scan-{index}'",
- " connection.execute('INSERT INTO scans VALUES (?, ?, NULL, ?, ?)', (scan, sys.argv[2], 'complete', str(index)))",
+ " connection.execute('INSERT INTO scans VALUES (?, ?, NULL, ?, ?)', (scan, sys.argv[2], 'complete', f'2026-01-01T00:00:0{index}Z'))",
" connection.execute('INSERT INTO finding_occurrences VALUES (?, ?, ?, ?, ?, ?, ?, ?)', (scan, f'scan-{index - 3}', scan, '{}', 'fix', 'high', 'summary', 'title'))",
"def coverage(scan):",
" if scan['id'] in {'scan-0', 'scan-1', 'scan-2'}:",