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): read scan counts and latest scans together (#1771)

Michael D'Angelo75f4b9ef74e9
diff --git a/plugins/codex-security/scripts/workbench_native_indexes.py b/plugins/codex-security/scripts/workbench_native_indexes.py
index 6fd1a52cf..e473bbe2a 100644
--- a/plugins/codex-security/scripts/workbench_native_indexes.py
+++ b/plugins/codex-security/scripts/workbench_native_indexes.py
@@ -203,15 +203,18 @@ def list_repositories(
connection: sqlite3.Connection,
args: argparse.Namespace | None = None,
) -> dict[str, Any]:
- scans = scan_history.list_scans(connection)["scans"]
- scans_by_id = {scan["scanId"]: scan for scan in scans}
- scan_count_by_target = dict(Counter(scan["targetId"] for scan in scans))
-
+ scan_count_by_target = Counter()
latest_scan_by_target: dict[str, dict[str, Any]] = {}
- for row in connection.execute(
- "SELECT id, target_id FROM scans ORDER BY julianday(upper(started_at)) DESC, id DESC"
+ for scan in (
+ scan_history.scan_summary(row)
+ for row in connection.execute(
+ f"""{scan_history.SCAN_SUMMARY_QUERY}
+ ORDER BY julianday(upper(scans.started_at)) DESC, scans.id DESC
+ """
+ ).fetchall()
):
- latest_scan_by_target.setdefault(row["target_id"], scans_by_id[row["id"]])
+ scan_count_by_target[scan["targetId"]] += 1
+ latest_scan_by_target.setdefault(scan["targetId"], scan)
open_findings_by_target = Counter(
row["target_id"] for row in _indexed_findings(connection) if row["status"] == "open"
diff --git a/plugins/codex-security/scripts/workbench_scan_history.py b/plugins/codex-security/scripts/workbench_scan_history.py
index 0f5eacc33..31fb064e6 100644
--- a/plugins/codex-security/scripts/workbench_scan_history.py
+++ b/plugins/codex-security/scripts/workbench_scan_history.py
@@ -202,6 +202,69 @@ def _repository_origin(target: Path) -> tuple[str, str] | None:
return (host.lower(), path) if host and path else None
+SCAN_SUMMARY_QUERY = """
+SELECT
+ scans.*,
+ progress.reportable_findings_count,
+ progress.scope_file_count,
+ progress.review_items_completed,
+ progress.review_items_total,
+ progress.updated_at AS progress_updated_at,
+ (
+ SELECT COUNT(*)
+ FROM finding_occurrences AS occurrences
+ WHERE occurrences.scan_id = scans.id
+ ) AS finding_count
+FROM scans
+JOIN scan_progress AS progress ON progress.scan_id = scans.id
+"""
+
+
+def scan_summary(row: sqlite3.Row) -> dict[str, Any]:
+ return {
+ "completedAt": row["completed_at"],
+ "continuationThreadId": row["continuation_thread_id"],
+ **stored_scan_cost_fields(row["cost_json"]),
+ "findingCount": row["finding_count"],
+ "handoffStatus": row["handoff_status"],
+ "mode": row["mode"],
+ "model": row["model"],
+ "name": row["name"],
+ "parentScanId": row["parent_scan_id"],
+ "progress": {
+ "candidates": {"reportable": row["reportable_findings_count"]},
+ "coverage": {
+ "closedRows": row["review_items_completed"],
+ "filesTotal": row["scope_file_count"],
+ "worklistRows": row["review_items_total"],
+ },
+ "phase": row["phase"],
+ "status": "canceled" if row["canceled_at"] else row["status"],
+ "updatedAt": row["progress_updated_at"],
+ },
+ "recipeAvailable": row["recipe_json"] is not None,
+ "reasoningEffort": row["reasoning_effort"],
+ "scanDir": row["scan_dir"],
+ "scanId": row["id"],
+ "scope": row["scope"],
+ "startedAt": row["started_at"],
+ "targetId": row["target_id"],
+ "targetPath": row["target_path"],
+ "targetRevision": row["target_revision"],
+ "targetSummary": row["target_summary"],
+ "updatedAt": max(
+ row["updated_at"],
+ row["progress_updated_at"],
+ key=lambda value: (timestamp_key(value), value),
+ ),
+ **(
+ {"warnings": json.loads(row["completion_warnings_json"])}
+ if row["completion_warnings_json"] != "[]"
+ else {}
+ ),
+ }
+
+
def list_scans(
connection: sqlite3.Connection, args: argparse.Namespace | None = None
) -> dict[str, Any]:
@@ -281,20 +344,7 @@ def list_scans(
values.extend((limit + 1, args.offset))
rows = connection.execute(
f"""
- SELECT
- scans.*,
- progress.reportable_findings_count,
- progress.scope_file_count,
- progress.review_items_completed,
- progress.review_items_total,
- progress.updated_at AS progress_updated_at,
- (
- SELECT COUNT(*)
- FROM finding_occurrences AS occurrences
- WHERE occurrences.scan_id = scans.id
- ) AS finding_count
- FROM scans
- JOIN scan_progress AS progress ON progress.scan_id = scans.id
+ {SCAN_SUMMARY_QUERY}
{where}
ORDER BY
CASE WHEN scans.status = 'running' AND scans.canceled_at IS NULL THEN 0 ELSE 1 END,
@@ -305,53 +355,7 @@ def list_scans(
""",
values,
).fetchall()
- result = {
- "scans": [
- {
- "completedAt": row["completed_at"],
- "continuationThreadId": row["continuation_thread_id"],
- **stored_scan_cost_fields(row["cost_json"]),
- "findingCount": row["finding_count"],
- "handoffStatus": row["handoff_status"],
- "mode": row["mode"],
- "model": row["model"],
- "name": row["name"],
- "parentScanId": row["parent_scan_id"],
- "progress": {
- "candidates": {"reportable": row["reportable_findings_count"]},
- "coverage": {
- "closedRows": row["review_items_completed"],
- "filesTotal": row["scope_file_count"],
- "worklistRows": row["review_items_total"],
- },
- "phase": row["phase"],
- "status": "canceled" if row["canceled_at"] else row["status"],
- "updatedAt": row["progress_updated_at"],
- },
- "recipeAvailable": row["recipe_json"] is not None,
- "reasoningEffort": row["reasoning_effort"],
- "scanDir": row["scan_dir"],
- "scanId": row["id"],
- "scope": row["scope"],
- "startedAt": row["started_at"],
- "targetId": row["target_id"],
- "targetPath": row["target_path"],
- "targetRevision": row["target_revision"],
- "targetSummary": row["target_summary"],
- "updatedAt": max(
- row["updated_at"],
- row["progress_updated_at"],
- key=lambda value: (timestamp_key(value), value),
- ),
- **(
- {"warnings": json.loads(row["completion_warnings_json"])}
- if row["completion_warnings_json"] != "[]"
- else {}
- ),
- }
- for row in rows[:limit]
- ]
- }
+ result = {"scans": [scan_summary(row) for row in rows[:limit]]}
if limit is not None:
result.update(
{
diff --git a/plugins/codex-security/tests/test_workbench_native_index_queries.py b/plugins/codex-security/tests/test_workbench_native_index_queries.py
index 8aa6a8ab9..635284e81 100644
--- a/plugins/codex-security/tests/test_workbench_native_index_queries.py
+++ b/plugins/codex-security/tests/test_workbench_native_index_queries.py
@@ -1,7 +1,9 @@
from __future__ import annotations
import ntpath
+import sqlite3
from argparse import Namespace
+from contextlib import closing
from types import SimpleNamespace
import pytest
@@ -480,3 +482,80 @@ def test_scan_start_chronology_keeps_latest_and_first_seen_queries_consistent(
]
assert [scan["scanId"] for scan in history] == (SCAN_IDS[:2] if equal else SCAN_IDS[1::-1])
assert list(connection.iterdump()) == before
+
+
[email protected]("in_transaction", [False, True])
+def test_repository_index_keeps_one_scan_cohort(
+ workbench_api, indexed_collections, tmp_path, in_transaction
+):
+ source, _ = indexed_collections
+ indexes = workbench_api["native_indexes"]
+ database = tmp_path / "history.sqlite"
+ new_id = "00000000-0000-4000-8000-999999999999"
+ with closing(sqlite3.connect(database)) as reader, closing(sqlite3.connect(database)) as writer:
+ source.backup(reader)
+ reader.row_factory = sqlite3.Row
+ assert reader.execute("PRAGMA journal_mode=WAL").fetchone()[0] == "wal"
+ before = indexes.list_repositories(reader)
+ if in_transaction:
+ reader.execute("BEGIN")
+
+ def execute(sql, parameters=()):
+ cursor = reader.execute(sql, parameters)
+ if "JOIN scan_progress AS progress" not in sql:
+ return cursor
+ rows = cursor.fetchall()
+ with writer:
+ writer.execute(
+ "INSERT INTO scans (id, workspace_id, target_id, target_path, target_revision, "
+ "scope, mode, scan_dir, status, phase, started_at, created_at, updated_at) "
+ "SELECT ?, workspace_id, target_id, target_path, target_revision, scope, mode, "
+ "scan_dir || '/next', 'complete', 'reporting', ?, created_at, updated_at "
+ "FROM scans WHERE id = ?",
+ (new_id, "2030-01-01T00:00:00Z", SCAN_IDS[0]),
+ )
+ writer.execute(
+ "INSERT INTO scan_progress (scan_id, updated_at) "
+ "SELECT ?, updated_at FROM scan_progress WHERE scan_id = ?",
+ (new_id, SCAN_IDS[0]),
+ )
+ return SimpleNamespace(fetchall=lambda: rows)
+
+ proxy = SimpleNamespace(execute=execute, create_function=reader.create_function)
+ assert indexes.list_repositories(proxy) == before
+ assert reader.in_transaction is in_transaction
+ reader.commit()
+ after = indexes.list_repositories(reader)["repositories"]
+ assert after[0]["latestScan"]["scanId"] == new_id
+ assert after[0]["scanCount"] == 2
+
+
+def test_repository_latest_keeps_sqlite_millisecond_ties(workbench_api, indexed_collections):
+ connection, targets = indexed_collections
+ target_id = stable_target_id(targets[0])
+ for scan_id, fraction in zip(SCAN_IDS[:2], ("123200", "123100")):
+ connection.execute(
+ "UPDATE scans SET target_id = ?, started_at = ? WHERE id = ?",
+ (target_id, f"2026-10-08T01:00:00.{fraction}Z", scan_id),
+ )
+ query = workbench_api["native_indexes"].list_repositories
+ repositories = query(connection)["repositories"]
+ assert repositories[0]["latestScan"]["scanId"] == SCAN_IDS[1]
+ assert repositories[0]["scanCount"] == 2
+
+ connection.execute("UPDATE scans SET cost_json = '{' WHERE id = ?", (SCAN_IDS[0],))
+ with pytest.raises(ValueError, match="Expecting property name"):
+ query(connection)
+
+
+def test_repository_summary_excludes_scans_without_progress(workbench_api, indexed_collections):
+ connection, targets = indexed_collections
+ target_id = stable_target_id(targets[0])
+ connection.execute("UPDATE scans SET target_id = ? WHERE id = ?", (target_id, SCAN_IDS[1]))
+ connection.execute("DELETE FROM scan_progress WHERE scan_id = ?", (SCAN_IDS[1],))
+ repositories = workbench_api["native_indexes"].list_repositories(
+ connection, query_args(target_id=target_id)
+ )["repositories"]
+ assert len(repositories) == 1
+ assert repositories[0]["latestScan"]["scanId"] == SCAN_IDS[0]
+ assert repositories[0]["scanCount"] == 1