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): run scan history queries in pytest (#1979)

Michael D'Angeloaf5c0f863007
diff --git a/plugins/codex-security/tests/test_workbench_scan_history_queries.py b/plugins/codex-security/tests/test_workbench_scan_history_queries.py
new file mode 100644
index 000000000..9e5a3c316
--- /dev/null
+++ b/plugins/codex-security/tests/test_workbench_scan_history_queries.py
@@ -0,0 +1,596 @@
+from __future__ import annotations
+
+import json
+import sqlite3
+from argparse import Namespace
+from contextlib import closing
+from itertools import combinations
+from pathlib import Path
+
+import pytest
+
+# These queries moved out of the bundled SDK tests; keep exercising native SQLite on all three OSes.
+pytestmark = pytest.mark.cross_platform
+
+NOW = "2026-01-01T00:00:00Z"
+
+
+def add_scan(db, scan_id, repository: Path, *, target_id=None, started_at=NOW):
+ if target_id is not None:
+ db.execute(
+ "INSERT INTO security_targets (id, current_path, display_name, created_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?) ON CONFLICT (id) DO NOTHING",
+ (target_id, str(repository), repository.name, NOW, NOW),
+ )
+ db.execute(
+ "INSERT INTO workspaces (id, target_id, created_at, updated_at) VALUES (?, ?, ?, ?)",
+ (scan_id, target_id, NOW, NOW),
+ )
+ db.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) "
+ "VALUES (?, ?, ?, ?, 'unversioned', '.', 'standard', ?, 'complete', 'reporting', ?, ?, ?)",
+ (
+ scan_id,
+ scan_id,
+ target_id,
+ str(repository),
+ str(repository / scan_id),
+ started_at,
+ NOW,
+ NOW,
+ ),
+ )
+
+
+def add_occurrence(db, scan_id, occurrence_id, *, finding_id=None, severity="high", path=None):
+ finding_id = finding_id or occurrence_id
+ db.execute(
+ "INSERT INTO findings (id, fingerprint, rule_id, identity_anchor, created_at, updated_at) "
+ "VALUES (?, ?, 'synthetic-control', ?, ?, ?) ON CONFLICT (id) DO NOTHING",
+ (finding_id, finding_id, finding_id, NOW, NOW),
+ )
+ db.execute(
+ "INSERT INTO finding_occurrences (id, finding_id, scan_id, title, summary, severity, "
+ "confidence, remediation, details_json, created_at) VALUES (?, ?, ?, ?, 'summary', ?, "
+ "'high', 'fix', '{}', ?)",
+ (occurrence_id, finding_id, scan_id, occurrence_id, severity, NOW),
+ )
+ if path is not None:
+ db.execute(
+ "INSERT INTO finding_locations (occurrence_id, relative_path, start_line, end_line, role, "
+ "sort_order) VALUES (?, ?, 1, 1, 'root_control', 0)",
+ (occurrence_id, path),
+ )
+
+
+def cache_comparison(db, before, after, payload=None):
+ db.execute(
+ "INSERT INTO scan_comparisons (before_scan_id, after_scan_id, result_json, created_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?) ON CONFLICT (before_scan_id, after_scan_id) DO NOTHING",
+ (before, after, '{"matches":[],"uncertain":[]}', NOW, NOW),
+ )
+ if payload is not None:
+ db.execute(
+ "UPDATE scan_comparisons SET result_json = ? WHERE before_scan_id = ? AND after_scan_id = ?",
+ (json.dumps(payload), before, after),
+ )
+
+
+def link(db, previous, current, reason="Confirmed link."):
+ before, after = [
+ db.execute(
+ "SELECT scan_id FROM finding_occurrences WHERE id = ?", (occurrence,)
+ ).fetchone()[0]
+ for occurrence in (previous, current)
+ ]
+ cache_comparison(db, before, after)
+ db.execute(
+ "INSERT INTO scan_comparison_matches "
+ "(before_scan_id, after_scan_id, before_occurrence_id, after_occurrence_id, reason) "
+ "VALUES (?, ?, ?, ?, ?)",
+ (before, after, previous, current, reason),
+ )
+
+
+def test_legacy_repository_matching_excludes_confirmed_links_from_another_checkout(
+ workbench_api, workbench_db, tmp_path
+):
+ for index, (scan, repository) in enumerate(
+ [
+ ("foreign-before", "other"),
+ ("foreign-after", "other"),
+ ("selected-before", "selected"),
+ ("selected-after", "selected"),
+ ]
+ ):
+ # Historical scans lack a target ID; their checkout paths decide repository identity.
+ add_scan(
+ workbench_db, scan, tmp_path / repository, started_at=f"2026-08-01T00:00:0{index}Z"
+ )
+ add_occurrence(workbench_db, "foreign-before", "foreign-a")
+ add_occurrence(workbench_db, "foreign-after", "foreign-b")
+ link(workbench_db, "foreign-a", "foreign-b")
+ comparison = workbench_api["scan_history"].compare_scans(
+ workbench_db,
+ Namespace(before_scan_id="selected-before", after_scan_id="selected-after"),
+ require_scan=workbench_api["require_scan"],
+ read_coverage=lambda _: {},
+ include_matching_inputs=True,
+ )
+ assert comparison["matchingInputs"] == {"before": [], "after": []}
+
+
+def test_comparison_index_migration_preserves_saved_history_and_indexes_both_directions(
+ workbench_api, tmp_path
+):
+ # Start before the comparison indexes and run the current migrations twice over saved history.
+ migrations = workbench_api["MIGRATIONS"]
+ with closing(sqlite3.connect(":memory:")) as db:
+ db.row_factory = sqlite3.Row
+ db.execute("PRAGMA foreign_keys = ON")
+ workbench_api["apply_schema_migrations"](
+ db, tuple(step for step in migrations if step[0] < 31), lambda: NOW, lambda _: None
+ )
+ scans = [f"scan-{i:03}" for i in range(200)]
+ for scan in scans:
+ add_scan(db, scan, tmp_path, target_id="target")
+ db.executemany(
+ "INSERT INTO scan_comparisons (before_scan_id, after_scan_id, result_json, created_at, updated_at) VALUES (?, ?, ?, ?, ?)",
+ (
+ (before, after, '{"matches":[],"uncertain":[]}', NOW, NOW)
+ for before, after in combinations(scans, 2)
+ ),
+ )
+
+ def rows():
+ return db.execute(
+ "SELECT * FROM scan_comparisons ORDER BY before_scan_id, after_scan_id"
+ ).fetchall()
+
+ original = rows()
+ assert len(original) == 19_900
+ for _ in range(2):
+ workbench_api["apply_migrations"](db)
+ assert rows() == original
+ assert db.execute("PRAGMA foreign_key_check").fetchall() == []
+
+ statements = []
+ db.set_trace_callback(statements.append)
+ workbench_api["scan_history"].finding_relations(db, "scan-100", ["missing"])
+ db.set_trace_callback(None)
+ (select,) = [query for query in statements if "FROM scan_comparisons" in query]
+ searches = [row["detail"] for row in db.execute("EXPLAIN QUERY PLAN " + select)]
+ assert any("before_scan_id=?" in step for step in searches), searches
+ assert any("after_scan_id=?" in step for step in searches), searches
+ assert not any(step.startswith("SCAN scan_comparisons") for step in searches), searches
+
+ for index, columns in [
+ ("scan_comparisons_by_after_scan", ["after_scan_id", "before_scan_id"]),
+ ("finding_occurrences_by_finding", ["finding_id", "id"]),
+ ]:
+ assert [row["name"] for row in db.execute(f"PRAGMA index_info({index})")] == columns
+ id_plan = db.execute(
+ "EXPLAIN QUERY PLAN SELECT id FROM finding_occurrences WHERE finding_id = ?",
+ ("synthetic-finding",),
+ )
+ assert any("finding_occurrences_by_finding" in row["detail"] for row in id_plan)
+
+
+def test_invalid_comparison_preserves_saved_groups_and_related_pairs(
+ workbench_db, workbench_api, tmp_path
+):
+ db = workbench_db
+ for scan, occurrences in [
+ ("scan-before", ("a1", "a2", "b", "c")),
+ ("scan-after", ("x1", "x2", "y", "z")),
+ ]:
+ add_scan(db, scan, tmp_path, target_id="target")
+ for occurrence in occurrences:
+ add_occurrence(db, scan, occurrence, path="src/covered.py")
+ db.commit()
+
+ def pair(before, after):
+ return {
+ "beforeOccurrenceId": before,
+ "afterOccurrenceId": after,
+ "reason": "Separate controls.",
+ }
+
+ def group(before, after):
+ return {
+ "beforeOccurrenceIds": before,
+ "afterOccurrenceIds": after,
+ "confidence": "high",
+ "reason": "Same control.",
+ }
+
+ def save(payload):
+ return workbench_api["scan_history"].save_scan_comparison(
+ db,
+ Namespace(
+ before_scan_id="scan-before",
+ after_scan_id="scan-after",
+ matches_json=json.dumps(payload),
+ ),
+ now=lambda: NOW,
+ require_scan=workbench_api["require_scan"],
+ read_coverage=lambda _: {
+ "includePaths": ["src"],
+ "excludePaths": [],
+ "explicitExclusions": [],
+ },
+ )
+
+ def snapshot():
+ return (
+ db.execute("SELECT result_json FROM scan_comparisons").fetchone()[0],
+ db.execute(
+ "SELECT * FROM scan_comparison_matches ORDER BY before_occurrence_id, after_occurrence_id"
+ ).fetchall(),
+ )
+
+ matches = [group(["a1", "a2"], ["x1", "x2"]), group(["b"], ["y"])]
+ payload = {"matches": matches, "uncertain": [], "related": [pair("a2", "y"), pair("c", "z")]}
+ saved = save(payload)
+ assert saved["summary"] == {
+ "new": 1,
+ "persisting": 2,
+ "not_observed": 1,
+ "reopened": 0,
+ "resolved": 0,
+ "unknown": 0,
+ }
+ assert [(p["beforeOccurrenceId"], p["afterOccurrenceId"]) for p in saved["related"]] == [
+ ("a2", "y"),
+ ("c", "z"),
+ ]
+ original = snapshot()
+ assert json.loads(original[0]) == payload
+ assert [(r["before_occurrence_id"], r["after_occurrence_id"]) for r in original[1]] == [
+ ("a1", "x1"),
+ ("a1", "x2"),
+ ("a2", "x1"),
+ ("a2", "x2"),
+ ("b", "y"),
+ ]
+ for field, values, error in [
+ ("related", [pair("a2", "x2")], "Related"),
+ ("related", [pair("b", "y")], "Related"),
+ ("related", [pair("a2", "y"), pair("a2", "y")], "Related"),
+ ("related", [pair("outside", "z")], "Related"),
+ ("uncertain", [pair("c", "z")], "Related"),
+ ("uncertain", [pair("a1", "z")], "Uncertain"),
+ ("uncertain", [pair("c", "y")], "Uncertain"),
+ ("matches", [matches[0], group(["b", "a1"], ["y"])], "distinct scan findings"),
+ ]:
+ with pytest.raises(SystemExit, match=error):
+ save({**payload, field: values})
+ assert snapshot() == original, (field, values)
+
+
+def test_cached_group_status_follows_coverage_uncertainty_and_confirmed_links(
+ workbench_db, workbench_api, tmp_path
+):
+ db = workbench_db
+ for scan, occurrences in [
+ ("scan-before", ("a1", "a2")),
+ ("scan-after", ("b1", "b2")),
+ ("scan-later", ("c1", "c2")),
+ ("scan-latest", ("d1",)),
+ ]:
+ add_scan(db, scan, tmp_path, target_id="target")
+ for occurrence in occurrences:
+ add_occurrence(
+ db,
+ scan,
+ occurrence,
+ severity="low" if occurrence.endswith("1") else "high",
+ path="src/excluded.py" if occurrence == "a1" else "src/covered.py",
+ )
+ for before, after in [("a1", "c1"), ("a2", "c1"), ("b1", "c2"), ("b2", "c2")]:
+ link(db, before, after)
+ payload = {
+ "matches": [],
+ "uncertain": [
+ {"beforeOccurrenceId": "a1", "afterOccurrenceId": "b1", "reason": "Uncertain."}
+ ],
+ "related": [{"beforeOccurrenceId": "a2", "afterOccurrenceId": "b2", "reason": "Separate."}],
+ }
+ coverage = {
+ "includePaths": ["src"],
+ "excludePaths": ["src/excluded.py"],
+ "explicitExclusions": [],
+ }
+
+ def compare():
+ return workbench_api["scan_history"].compare_scans(
+ db,
+ Namespace(before_scan_id="scan-before", after_scan_id="scan-after"),
+ require_scan=workbench_api["require_scan"],
+ read_coverage=lambda _: coverage,
+ require_matches=True,
+ )
+
+ def counts(comparison):
+ return {status: n for status, n in comparison["summary"].items() if n}
+
+ cache_comparison(db, "scan-before", "scan-after", payload)
+ uncertain = compare()
+ assert counts(uncertain) == {"unknown": 2}
+ previous, current = uncertain["findings"]
+ assert previous["beforeOccurrenceIds"] == ["a1", "a2"]
+ assert current["afterOccurrenceIds"] == ["b1", "b2"]
+ assert (previous["findingId"], previous["severity"], current["findingId"]) == (
+ "a2",
+ "high",
+ "b2",
+ )
+ assert all(
+ f["status"] == "unknown" and f["reason"] == "Uncertain." for f in (previous, current)
+ )
+
+ payload["uncertain"] = []
+ cache_comparison(db, "scan-before", "scan-after", payload)
+ assert counts(compare()) == {"new": 1, "unknown": 1}
+ coverage["excludePaths"] = []
+ unlinked = compare()
+ assert counts(unlinked) == {"new": 1, "not_observed": 1}
+
+ db.execute(
+ "INSERT INTO finding_triage (occurrence_id, status, close_reason, updated_at) VALUES ('a1', 'closed', 'already_fixed', ?)",
+ (NOW,),
+ )
+ link(db, "c1", "d1")
+ link(db, "c2", "d1")
+ linked = compare()
+ assert counts(linked) == {"reopened": 1}
+ (reopened,) = linked["findings"]
+ assert reopened["status"] == "reopened"
+ assert isinstance(reopened["matchReason"], str) and reopened["matchReason"]
+ assert reopened["beforeOccurrenceIds"] == ["a1", "a2"]
+ assert reopened["afterOccurrenceIds"] == ["b1", "b2"]
+ assert "related" not in linked
+ stored = db.execute(
+ "SELECT result_json FROM scan_comparisons WHERE before_scan_id='scan-before' AND after_scan_id='scan-after'"
+ ).fetchone()[0]
+ assert json.loads(stored) == payload
+
+ db.execute("DELETE FROM scan_comparison_matches WHERE after_scan_id='scan-latest'")
+ assert compare() == unlinked
+
+
+def test_matching_reads_each_scan_once_and_skips_cached_links(
+ workbench_api, workbench_db, tmp_path
+):
+ history = workbench_api["scan_history"]
+ db = workbench_db
+ repository = tmp_path / "repository"
+ for index in range(3):
+ scan = f"scan-{index}"
+ add_scan(db, scan, repository, started_at=f"2026-01-01T00:00:0{index}Z")
+ add_occurrence(db, scan, scan)
+ queries = []
+ backfilled = []
+ db.set_trace_callback(queries.append)
+
+ def pending(*, force=False, coverage=lambda _: {}):
+ return history.list_unmatched_scan_pairs(
+ db,
+ Namespace(repository=str(repository), force=force),
+ backfill_finding_details=lambda _, scan: backfilled.append(scan["id"]),
+ read_coverage=coverage,
+ )
+
+ def batches(result):
+ return [
+ (batch["afterScanId"], [scan["scanId"] for scan in batch["beforeScans"]])
+ for batch in result["batches"]
+ ]
+
+ result = pending()
+ assert result["scanCount"] == 3
+ assert batches(result) == [("scan-1", ["scan-0"]), ("scan-2", ["scan-0", "scan-1"])]
+ for batch in result["batches"]:
+ assert [row["occurrenceId"] for row in batch["afterFindings"]] == [batch["afterScanId"]]
+ for before in batch["beforeScans"]:
+ assert [row["occurrenceId"] for row in before["findings"]] == [before["scanId"]]
+ assert sorted(backfilled) == ["scan-0", "scan-1", "scan-2"]
+ assert sum("from finding_occurrences " in sql.lower() for sql in queries) <= 3
+
+ for before, after in [("scan-0", "scan-1"), ("scan-0", "scan-2"), ("scan-1", "scan-2")]:
+ cache_comparison(db, before, after)
+ queries.clear()
+ cached = pending()
+ assert (cached["batches"], cached["skippedPairs"]) == ([], 3)
+ assert not any("FROM scan_comparison_matches" in sql for sql in queries)
+
+ for scan in ("foreign-a", "foreign-b"):
+ add_scan(db, scan, tmp_path / "other")
+ add_occurrence(db, scan, scan)
+ link(db, "scan-0", "scan-1")
+ link(db, "foreign-a", "foreign-b")
+ queries.clear()
+ scoped = history._saved_finding_links(db, {"scan-0", "scan-1"})
+ assert [(row["before_finding_id"], row["after_finding_id"]) for row in scoped] == [
+ ("scan-0", "scan-1")
+ ]
+ (sql,) = queries
+ # A query over all saved links could return the same rows after filtering in Python.
+ assert any(
+ "before_scan_id=?" in row["detail"] for row in db.execute("EXPLAIN QUERY PLAN " + sql)
+ )
+
+ for index in (3, 4):
+ scan = f"scan-{index}"
+ add_scan(db, scan, repository, started_at=f"2026-01-01T00:00:0{index}Z")
+ add_occurrence(db, scan, scan, finding_id=f"scan-{index - 3}")
+
+ def coverage(scan):
+ if scan["id"] in {"scan-0", "scan-1", "scan-2"}:
+ raise SystemExit("Unavailable artifacts")
+ return {}
+
+ unavailable = pending(coverage=coverage)
+ assert (unavailable["scanCount"], unavailable["unavailableScans"]) == (5, 3)
+ assert batches(unavailable) == [("scan-4", ["scan-3"])]
+ assert unavailable["batches"][0]["knownFindingGroups"] == [["scan-0", "scan-1"]]
+ forced = pending(force=True, coverage=coverage)
+ assert batches(forced) == [("scan-4", ["scan-3"])]
+ assert "knownFindingGroups" not in forced["batches"][0]
+
+ for before, after in [
+ ("scan-1", "scan-2"),
+ ("scan-2", "scan-0"),
+ ("scan-0", "foreign-a"),
+ ("foreign-a", "scan-1"),
+ ]:
+ link(db, before, after)
+ if hasattr(db, "setlimit"):
+ db.setlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, 2)
+ queries.clear()
+ selected = history._saved_finding_links(db, {"scan-0", "scan-1", "scan-2"})
+ assert [(row["before_scan_id"], row["after_scan_id"]) for row in selected] == [
+ ("scan-0", "scan-1"),
+ ("scan-1", "scan-2"),
+ ("scan-2", "scan-0"),
+ ]
+ assert len(queries) <= 1
+ queries.clear()
+ assert history._saved_finding_links(db, set()) == []
+ assert queries == []
+
+
+def test_relations_follow_confirmed_identities_and_read_a_page_in_bulk(
+ workbench_api, workbench_db, tmp_path
+):
+ history = workbench_api["scan_history"]
+ db = workbench_db
+ for scan, target in [
+ ("one", "target"),
+ ("two", "target"),
+ ("three", "clone"),
+ ("four", "clone"),
+ ("foreign-one", "unrelated"),
+ ("foreign-two", "unrelated"),
+ ]:
+ add_scan(db, scan, tmp_path / target, target_id=target)
+ # Exceed SQLite's historical 999-bind limit, even if setlimit is unavailable.
+ left_ids = [f"left-{i}" for i in range(1001)]
+ pairs = []
+ for i, left in enumerate(left_ids):
+ right = f"right-{i}"
+ add_occurrence(db, "one", left, finding_id=f"left-identity-{i}")
+ add_occurrence(db, "two", right, finding_id=f"right-identity-{i}")
+ pairs.append(
+ {"beforeOccurrenceId": left, "afterOccurrenceId": right, "reason": "Separate controls."}
+ )
+ cache_comparison(db, "one", "two", {"matches": [], "uncertain": [], "related": pairs})
+
+ def saved():
+ return db.execute(
+ "SELECT result_json FROM scan_comparisons WHERE before_scan_id = 'one' AND after_scan_id = 'two'"
+ ).fetchone()[0]
+
+ payload = saved()
+ queries = []
+ db.set_trace_callback(queries.append)
+ scoped = history.finding_relations(db, "one", ["left-0"])
+ assert set(scoped) == {"left-0"}
+ (relation,) = scoped["left-0"]
+ assert (relation["occurrenceId"], relation["scanId"]) == ("right-0", "two")
+ assert len(queries) <= 3
+ queries.clear()
+ assert history.finding_relations(db, "one", []) == {}
+ assert queries == []
+
+ add_occurrence(db, "four", "recurring-left", finding_id="left-identity-0")
+ add_occurrence(db, "three", "bridge", finding_id="bridge-identity")
+ add_occurrence(db, "foreign-one", "foreign-a")
+ add_occurrence(db, "foreign-two", "foreign-b")
+ link(db, "recurring-left", "bridge")
+ link(db, "right-0", "bridge")
+ link(db, "foreign-a", "foreign-b")
+ assert set(history._confirmed_finding_aliases(db, ["left-0"])) == {
+ "left-identity-0",
+ "right-identity-0",
+ "bridge-identity",
+ }
+ assert history.finding_relations(db, "one", ["left-0"]) == {}
+ assert history.finding_relations(db, "two", ["right-0"]) == {}
+ assert set(history.finding_relations(db, "one", ["left-1"])) == {"left-1"}
+ assert saved() == payload
+ db.execute("DELETE FROM scan_comparison_matches WHERE before_occurrence_id = 'right-0'")
+ assert history.finding_relations(db, "one", ["left-0"]) == scoped
+
+ if hasattr(db, "setlimit"):
+ db.setlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, 8)
+ queries.clear()
+ page = history.finding_relations(db, "one", left_ids[1:11])
+ assert {
+ left: [relation["occurrenceId"] for relation in rows] for left, rows in page.items()
+ } == {f"left-{i}": [f"right-{i}"] for i in range(1, 11)}
+ assert len(queries) <= 3
+ queries.clear()
+ rows = history._rows_for_ids(
+ db,
+ "SELECT id FROM finding_occurrences WHERE id IN (SELECT value FROM json_each(?))",
+ iter(left_ids),
+ )
+ assert {row["id"] for row in rows} == set(left_ids)
+ assert len(queries) <= 1
+
+
+def test_confirmed_history_follows_recurring_ids_and_reverts_after_unlink(
+ workbench_api, workbench_db, tmp_path
+):
+ from finalize_scan_contract import _populate_unsealed_finding_identities
+
+ history = workbench_api["scan_history"]
+ db = workbench_db
+ scans = [
+ ("a", "a", "primary"),
+ ("b", "b", "primary"),
+ ("c", "c", "primary"),
+ ("a-repeat", "a", "primary"),
+ ("c-repeat", "c", "primary"),
+ ("unlinked", "a", "other"),
+ ]
+ started = {}
+ occurrences = {}
+ for index, (scan, anchor, target) in enumerate(scans):
+ started[scan] = f"2026-08-01T00:00:0{index}Z"
+ add_scan(db, scan, tmp_path / target, target_id=target, started_at=started[scan])
+ finding = {"ruleId": "synthetic-control", "identity": {"anchor": anchor}}
+ _populate_unsealed_finding_identities(
+ {"scan": {"id": scan, "target": {"targetId": target}}},
+ {"scanId": scan, "findings": [finding]},
+ )
+ occurrences[scan] = finding["occurrenceId"]
+ add_occurrence(db, scan, finding["occurrenceId"], finding_id=finding["findingId"])
+ link(db, occurrences["a"], occurrences["b"], reason="First confirmed link.")
+ link(db, occurrences["b"], occurrences["c"], reason="Second confirmed link.")
+
+ def assert_history(group):
+ # The group is in scan-start order; matches are sorted by scan ID.
+ for scan in group:
+ matches, first, bounds = history.finding_matches(
+ db, occurrences[scan], scan, started[scan]
+ )
+ assert [(match["scanId"], match["occurrenceId"]) for match in matches] == [
+ (other, occurrences[other]) for other in sorted(set(group) - {scan})
+ ]
+ assert first == started[group[0]]
+ assert bounds == (group if len(group) == 1 else [group[0], group[-1]])
+ assert all(match["reason"] for match in matches)
+ if scan == "a" and "b" in group:
+ assert (
+ next(match["reason"] for match in matches if match["scanId"] == "b")
+ == "First confirmed link."
+ )
+
+ assert_history(["a", "b", "c", "a-repeat", "c-repeat"])
+ assert_history(["unlinked"])
+ db.execute("DELETE FROM scan_comparison_matches")
+ for group in (["a", "a-repeat"], ["b"], ["c", "c-repeat"], ["unlinked"]):
+ assert_history(group)
diff --git a/sdk/typescript/tests-ts/workbench-scan-history.test.ts b/sdk/typescript/tests-ts/workbench-scan-history.test.ts
index 9ea2526e0..301169590 100644
--- a/sdk/typescript/tests-ts/workbench-scan-history.test.ts
+++ b/sdk/typescript/tests-ts/workbench-scan-history.test.ts
@@ -1,26 +1,8 @@
-import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, test } from "bun:test";
import { resolvePluginPython, runCodexCommand } from "../src/runtime.js";
import { PLUGIN_ROOT } from "./plugin-root.js";
-async function runPythonProbe(
- program: string,
- ...args: string[]
-): Promise<Record<string, unknown>> {
- const python = await resolvePluginPython();
- const result = await runCodexCommand(
- { command: python },
- ["-I", "-X", "utf8", "-B", "-", join(PLUGIN_ROOT, "scripts"), ...args],
- process.env,
- program,
- AbortSignal.timeout(10_000),
- );
- expect(result.exitCode, result.stderr).toBe(0);
- expect(result.stderr).toBe("");
- return JSON.parse(result.stdout) as Record<string, unknown>;
-}
-
test("keeps inline and stdin comparison transports compatible", async () => {
const python = await resolvePluginPython();
const probe = [
@@ -84,673 +66,11 @@ test("keeps inline and stdin comparison transports compatible", async () => {
expect(conflicting.stderr).toContain("not allowed with argument");
});
-test("keeps unrelated legacy repositories out of matching inputs", async () => {
- const observed = await runPythonProbe(
- `
-import argparse, json, sqlite3, sys
-from pathlib import Path
-sys.path.insert(0, sys.argv[1])
-import workbench_scan_history as history
-connection = sqlite3.connect(':memory:')
-connection.row_factory = sqlite3.Row
-connection.executescript('''
-CREATE TABLE scans (id TEXT PRIMARY KEY, target_id TEXT, target_path TEXT, status TEXT, started_at TEXT);
-CREATE TABLE finding_occurrences (id TEXT PRIMARY KEY, finding_id TEXT, scan_id TEXT);
-CREATE TABLE finding_triage (occurrence_id TEXT, status TEXT, close_reason TEXT);
-CREATE TABLE finding_locations (occurrence_id TEXT, relative_path TEXT, role TEXT, sort_order INTEGER);
-CREATE TABLE scan_comparisons (before_scan_id TEXT, after_scan_id TEXT, result_json TEXT);
-CREATE TABLE scan_comparison_matches (before_scan_id TEXT, after_scan_id TEXT, before_occurrence_id TEXT, after_occurrence_id TEXT);
-''')
-for index, (scan, repository) in enumerate([
- ('unrelated-before', 'unrelated'), ('unrelated-after', 'unrelated'),
- ('before', 'selected'), ('after', 'selected')
-]):
- connection.execute('INSERT INTO scans VALUES (?, NULL, ?, ?, ?)',
- (scan, str(Path(sys.argv[2]) / repository), 'complete', f'2026-08-01T00:00:0{index}Z'))
-connection.executemany('INSERT INTO finding_occurrences VALUES (?, ?, ?)', [
- ('unrelated-first', 'unrelated-identity-a', 'unrelated-before'),
- ('unrelated-second', 'unrelated-identity-b', 'unrelated-after')
-])
-connection.execute('INSERT INTO scan_comparison_matches VALUES (?, ?, ?, ?)',
- ('unrelated-before', 'unrelated-after', 'unrelated-first', 'unrelated-second'))
-comparison = history.compare_scans(
- connection, argparse.Namespace(before_scan_id='before', after_scan_id='after'),
- require_scan=lambda db, scan: db.execute('SELECT * FROM scans WHERE id = ?', (scan,)).fetchone(),
- read_coverage=lambda _: {}, include_matching_inputs=True)
-print(json.dumps(comparison['matchingInputs']))
-`,
- join(tmpdir(), "codex-security-legacy-repositories"),
- );
- expect(observed).toEqual({ before: [], after: [] });
-});
-
-test("validates related pairs by confirmed group without replacing saved results", async () => {
- const probe = `
-import argparse, json, sqlite3, sys
-sys.path.insert(0, sys.argv[1])
-import workbench_scan_history as history
-connection = sqlite3.connect(':memory:')
-connection.row_factory = sqlite3.Row
-connection.executescript('''
-PRAGMA foreign_keys = ON;
-CREATE TABLE scans (id TEXT PRIMARY KEY, target_path TEXT, target_id TEXT, status TEXT);
-CREATE TABLE finding_occurrences (
- id TEXT PRIMARY KEY, finding_id TEXT, scan_id TEXT, title TEXT, severity TEXT
-);
-CREATE TABLE finding_triage (occurrence_id TEXT, status TEXT, close_reason TEXT);
-CREATE TABLE finding_locations (occurrence_id TEXT, relative_path TEXT, role TEXT, sort_order INTEGER);
-CREATE TABLE scan_comparisons (
- before_scan_id TEXT, after_scan_id TEXT, result_json TEXT, created_at TEXT, updated_at TEXT,
- PRIMARY KEY(before_scan_id, after_scan_id)
-);
-CREATE TABLE scan_comparison_matches (
- before_scan_id TEXT, after_scan_id TEXT, before_occurrence_id TEXT, after_occurrence_id TEXT,
- reason TEXT,
- FOREIGN KEY(before_scan_id, after_scan_id)
- REFERENCES scan_comparisons(before_scan_id, after_scan_id) ON DELETE CASCADE
-);
-CREATE INDEX matches_before ON scan_comparison_matches(before_occurrence_id);
-CREATE INDEX matches_after ON scan_comparison_matches(after_occurrence_id);
-''')
-for scan, names in [('before', ('a1', 'a2', 'b', 'c')), ('after', ('x1', 'x2', 'y', 'z'))]:
- connection.execute('INSERT INTO scans VALUES (?, ?, ?, ?)', (scan, sys.argv[2], 'target', 'complete'))
- for name in names:
- connection.execute('INSERT INTO finding_occurrences VALUES (?, ?, ?, ?, ?)', (name, name, scan, name, 'high'))
- connection.execute('INSERT INTO finding_locations VALUES (?, ?, ?, ?)', (name, 'src/example.py', 'root_control', 0))
-connection.commit()
-def pair(before, after):
- return {'beforeOccurrenceId': before, 'afterOccurrenceId': after, 'reason': 'Separate synthetic controls.'}
-def group(before, after):
- return {'beforeOccurrenceIds': before, 'afterOccurrenceIds': after,
- 'confidence': 'high', 'reason': 'The same synthetic control.'}
-payload = {'matches': [group(['a1', 'a2'], ['x1', 'x2']), group(['b'], ['y'])],
- 'uncertain': [], 'related': [pair('a2', 'y'), pair('c', 'z')]}
-def save(value):
- return history.save_scan_comparison(
- connection, argparse.Namespace(before_scan_id='before', after_scan_id='after', matches_json=json.dumps(value)),
- now=lambda: '2026-01-01T00:00:00Z',
- require_scan=lambda db, scan: db.execute('SELECT * FROM scans WHERE id = ?', (scan,)).fetchone(),
- read_coverage=lambda _: {'includePaths': ['src'],
- 'excludePaths': [], 'explicitExclusions': []})
-def snapshot():
- return (connection.execute('SELECT result_json FROM scan_comparisons').fetchone()[0],
- [tuple(row) for row in connection.execute('SELECT * FROM scan_comparison_matches ORDER BY before_occurrence_id, after_occurrence_id')])
-accepted = save(payload)
-original = snapshot()
-invalid = [
- {**payload, 'related': [pair('a2', 'x2')]},
- {**payload, 'related': [pair('b', 'y')]},
- {**payload, 'related': [pair('a2', 'y'), pair('a2', 'y')]},
- {**payload, 'related': [pair('outside', 'z')]},
- {**payload, 'uncertain': [pair('c', 'z')]},
- {**payload, 'uncertain': [pair('a1', 'z')]},
- {**payload, 'uncertain': [pair('c', 'y')]},
- {**payload, 'matches': [payload['matches'][0], group(['b', 'a1'], ['y'])]},
-]
-for value in invalid:
- try:
- save(value)
- except SystemExit:
- pass
- else:
- raise AssertionError('Invalid comparison was accepted')
- assert snapshot() == original
-print(json.dumps({'summary': accepted['summary'],
- 'related': [(item['beforeOccurrenceId'], item['afterOccurrenceId']) for item in accepted['related']],
- 'savedPairs': len(original[1])}))
-`;
- expect(
- await runPythonProbe(
- probe,
- join(tmpdir(), "codex-security-validation-fixture"),
- ),
- ).toEqual({
- summary: {
- new: 1,
- persisting: 2,
- reopened: 0,
- resolved: 0,
- not_observed: 1,
- unknown: 0,
- },
- related: [
- ["a2", "y"],
- ["c", "z"],
- ],
- savedPairs: 5,
- });
-});
-
-test("upgrades existing history with indexed identity and reverse comparison lookups", async () => {
- const probe = `
-import json, sqlite3, sys
-from pathlib import Path
-sys.path.insert(0, sys.argv[1])
-from finalize_scan_contract import _derived_finding_identity_rows
-from workbench_schema import MIGRATIONS, apply_migrations
-connection = sqlite3.connect(':memory:')
-connection.row_factory = sqlite3.Row
-connection.execute('PRAGMA foreign_keys = ON')
-timestamp = '2026-01-01T00:00:00Z'
-def migrate(migrations):
- apply_migrations(connection, migrations, lambda: timestamp, lambda _: None)
-migrate(tuple(item for item in MIGRATIONS if item[0] < 31))
-connection.execute('INSERT INTO security_targets VALUES (?, ?, ?, ?, ?)', ('target', sys.argv[2], 'Synthetic target', timestamp, timestamp))
-connection.execute('INSERT INTO workspaces (id, target_id, created_at, updated_at) VALUES (?, ?, ?, ?)', ('workspace', 'target', timestamp, timestamp))
-connection.executemany('''INSERT INTO scans (
- id, workspace_id, target_id, target_path, target_revision, scope, mode, scan_dir,
- status, phase, started_at, created_at, updated_at
-) VALUES (?, 'workspace', 'target', ?, 'unversioned', '.', 'standard', ?, 'complete', 'reporting', ?, ?, ?)''', (
- (f'scan-{index:03d}', sys.argv[2], str(Path(sys.argv[2]) / f'scan-{index:03d}'), timestamp, timestamp, timestamp)
- for index in range(200)
-))
-connection.executemany('INSERT INTO scan_comparisons VALUES (?, ?, ?, ?, ?)', (
- (f'scan-{before:03d}', f'scan-{after:03d}', json.dumps({'matches': [], 'uncertain': []}), timestamp, timestamp)
- for after in range(200) for before in range(after)
-))
-def rows():
- return [tuple(row) for row in connection.execute('SELECT * FROM scan_comparisons ORDER BY before_scan_id, after_scan_id')]
-original = rows()
-migrate(MIGRATIONS)
-migrate(MIGRATIONS)
-query = '''SELECT before_scan_id, after_scan_id, result_json FROM scan_comparisons
- WHERE before_scan_id = ? OR after_scan_id = ? ORDER BY before_scan_id, after_scan_id'''
-plan = [row['detail'] for row in connection.execute('EXPLAIN QUERY PLAN ' + query, ('scan-100', 'scan-100'))]
-identity_plan = [row['detail'] for row in connection.execute(
- 'EXPLAIN QUERY PLAN SELECT id FROM finding_occurrences WHERE finding_id = ?', ('synthetic-finding',))]
-indexes = {
- name: [row['name'] for row in connection.execute(f'PRAGMA index_info({name})')]
- for name in ('finding_occurrences_by_finding', 'scan_comparisons_by_after_scan')
-}
-def identity(target, scan):
- finding = {'ruleId': 'synthetic-control', 'identity': {'anchor': 'synthetic-control'}}
- return _derived_finding_identity_rows(
- {'scan': {'id': scan, 'target': {'targetId': target}}},
- {'scanId': scan, 'findings': [finding]})[0][2:4]
-first_identity = identity('target', 'first')
-recurring_identity = identity('target', 'second')
-other_identity = identity('another-target', 'third')
-print(json.dumps({'unchanged': rows() == original, 'comparisons': len(original),
- 'plan': plan, 'identityPlan': identity_plan, 'indexes': indexes,
- 'stableIdentity': first_identity[0] == recurring_identity[0],
- 'distinctOccurrences': first_identity[1] != recurring_identity[1],
- 'targetScopedIdentity': first_identity[0] != other_identity[0],
- 'foreignKeyErrors': len(connection.execute('PRAGMA foreign_key_check').fetchall())}))
-`;
- const observed = (await runPythonProbe(
- probe,
- join(tmpdir(), "codex-security-index-fixture"),
- )) as {
- plan: string[];
- identityPlan: string[];
- };
- expect(observed).toMatchObject({
- unchanged: true,
- comparisons: 19_900,
- foreignKeyErrors: 0,
- stableIdentity: true,
- distinctOccurrences: true,
- targetScopedIdentity: true,
- indexes: {
- finding_occurrences_by_finding: ["finding_id", "id"],
- scan_comparisons_by_after_scan: ["after_scan_id", "before_scan_id"],
- },
- });
- expect(observed.plan.some((step) => step.includes("before_scan_id=?"))).toBe(
- true,
- );
- expect(observed.plan.some((step) => step.includes("after_scan_id=?"))).toBe(
- true,
- );
- expect(
- observed.plan.some((step) => step.startsWith("SCAN scan_comparisons")),
- ).toBe(false);
- expect(
- observed.identityPlan.some((step) =>
- step.includes("finding_occurrences_by_finding"),
- ),
- ).toBe(true);
-});
-
-test("loads each scan once and scopes saved links to uncached history", async () => {
- const probe = [
- "import argparse, json, sqlite3, sys",
- "sys.path.insert(0, sys.argv[1])",
- "import workbench_scan_history as history",
- "connection = sqlite3.connect(':memory:')",
- "connection.row_factory = sqlite3.Row",
- "connection.executescript('''",
- "CREATE TABLE security_targets (id TEXT, current_path TEXT);",
- "CREATE TABLE scans (id TEXT, target_path TEXT, target_id TEXT, status TEXT, started_at TEXT);",
- "CREATE TABLE scan_comparisons (before_scan_id TEXT, after_scan_id TEXT);",
- "CREATE TABLE scan_comparison_matches (before_scan_id TEXT, after_scan_id TEXT, before_occurrence_id TEXT, after_occurrence_id TEXT);",
- "CREATE TABLE finding_occurrences (id TEXT, finding_id TEXT, scan_id TEXT, details_json TEXT, remediation TEXT, severity TEXT, summary TEXT, title TEXT);",
- "CREATE TABLE finding_triage (occurrence_id TEXT, status TEXT, close_reason TEXT);",
- "CREATE TABLE finding_locations (occurrence_id TEXT, relative_path TEXT, role TEXT, sort_order INTEGER);",
- "''')",
- "for index in range(3):",
- " scan = f'scan-{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)",
- "backfilled = []",
- "result = history.list_unmatched_scan_pairs(connection, argparse.Namespace(repository=sys.argv[2], force=False), backfill_finding_details=lambda _connection, scan: backfilled.append(scan['id']), read_coverage=lambda _scan: {})",
- "finding_queries = sum('FROM finding_occurrences AS occurrences' in query for query in queries)",
- "connection.executemany('INSERT INTO scan_comparisons VALUES (?, ?)', [('scan-0', 'scan-1'), ('scan-0', 'scan-2'), ('scan-1', 'scan-2')])",
- "queries.clear()",
- "cached = history.list_unmatched_scan_pairs(connection, argparse.Namespace(repository=sys.argv[2], force=False), backfill_finding_details=lambda *_: None, read_coverage=lambda _scan: {})",
- "cached_link_queries = sum('FROM scan_comparison_matches' in query for query in queries)",
- "for name in ('foreign-a', 'foreign-b'):",
- " connection.execute('INSERT INTO finding_occurrences VALUES (?, ?, ?, ?, ?, ?, ?, ?)', (name, name, name, '{}', 'fix', 'high', 'summary', 'title'))",
- "connection.executemany('INSERT INTO scan_comparison_matches VALUES (?, ?, ?, ?)', [('scan-0', 'scan-1', 'scan-0', 'scan-1'), ('foreign-a', 'foreign-b', 'foreign-a', 'foreign-b')])",
- "queries.clear()",
- "scoped = history._saved_finding_links(connection, {'scan-0', 'scan-1'})",
- "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', 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'}:",
- " raise SystemExit('Synthetic unavailable artifacts')",
- " return {}",
- "unavailable = history.list_unmatched_scan_pairs(connection, argparse.Namespace(repository=sys.argv[2], force=False), backfill_finding_details=lambda *_: None, read_coverage=coverage)",
- "forced = history.list_unmatched_scan_pairs(connection, argparse.Namespace(repository=sys.argv[2], force=True), backfill_finding_details=lambda *_: None, read_coverage=coverage)",
- "connection.executemany('INSERT INTO scan_comparison_matches VALUES (?, ?, ?, ?)', [('scan-1', 'scan-2', 'scan-1', 'scan-2'), ('scan-2', 'scan-0', 'scan-2', 'scan-0'), ('scan-0', 'foreign-a', 'scan-0', 'foreign-a'), ('foreign-a', 'scan-1', 'foreign-a', 'scan-1')])",
- "limited = hasattr(connection, 'setlimit')",
- "if limited:",
- " old_limit = connection.setlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, 2)",
- "queries.clear()",
- "batched = history._saved_finding_links(connection, {'scan-2', 'scan-0', 'scan-1'})",
- "batched_queries = len(queries)",
- "if limited:",
- " connection.setlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, old_limit)",
- "queries.clear()",
- "empty = history._saved_finding_links(connection, set())",
- "print(json.dumps({",
- " 'result': result, 'backfilled': backfilled, 'findingQueries': finding_queries,",
- " 'cached': cached, 'cachedLinkQueries': cached_link_queries,",
- " 'scopedLinks': [dict(row) for row in scoped], 'scopedQueryCount': len(link_queries),",
- " 'unscopedQueries': sum('WHERE matches.before_scan_id' not in query for query in link_queries),",
- " 'unavailable': unavailable, 'forcedKnownGroups': [batch.get('knownFindingGroups') for batch in forced['batches']],",
- " 'batchedLinks': [[row['before_scan_id'], row['after_scan_id']] for row in batched],",
- " 'batchedQueryCount': batched_queries,",
- " 'emptyLinks': empty, 'emptyQueryCount': len(queries),",
- "}))",
- ].join("\n");
-
- const observed = await runPythonProbe(
- probe,
- join(tmpdir(), "codex-security-matching-fixture"),
- );
- expect(observed).toMatchObject({
- backfilled: ["scan-0", "scan-1", "scan-2"],
- findingQueries: 3,
- cached: { batches: [], skippedPairs: 3 },
- cachedLinkQueries: 0,
- scopedLinks: [{ before_finding_id: "scan-0", after_finding_id: "scan-1" }],
- scopedQueryCount: 1,
- unscopedQueries: 0,
- batchedLinks: [
- ["scan-0", "scan-1"],
- ["scan-1", "scan-2"],
- ["scan-2", "scan-0"],
- ],
- batchedQueryCount: 1,
- emptyLinks: [],
- emptyQueryCount: 0,
- unavailable: {
- scanCount: 5,
- unavailableScans: 3,
- batches: [
- {
- afterScanId: "scan-4",
- beforeScans: [{ scanId: "scan-3" }],
- knownFindingGroups: [["scan-0", "scan-1"]],
- },
- ],
- },
- forcedKnownGroups: [null],
- result: {
- scanCount: 3,
- batches: [
- { afterScanId: "scan-1", beforeScans: [{ scanId: "scan-0" }] },
- {
- afterScanId: "scan-2",
- beforeScans: [{ scanId: "scan-0" }, { scanId: "scan-1" }],
- },
- ],
- },
- });
-});
-
-test("reconciles cached statuses without losing grouped coverage or uncertainty", async () => {
- const probe = `
-import argparse, json, sqlite3, sys
-sys.path.insert(0, sys.argv[1])
-import workbench_scan_history as history
-connection = sqlite3.connect(':memory:')
-connection.row_factory = sqlite3.Row
-connection.executescript('''
-CREATE TABLE scans (id TEXT PRIMARY KEY, target_path TEXT, target_id TEXT, status TEXT);
-CREATE TABLE finding_occurrences (
- id TEXT PRIMARY KEY, finding_id TEXT, scan_id TEXT, title TEXT, severity TEXT
-);
-CREATE TABLE finding_triage (occurrence_id TEXT, status TEXT, close_reason TEXT);
-CREATE TABLE finding_locations (occurrence_id TEXT, relative_path TEXT, role TEXT, sort_order INTEGER);
-CREATE TABLE scan_comparisons (
- before_scan_id TEXT, after_scan_id TEXT, result_json TEXT,
- PRIMARY KEY(before_scan_id, after_scan_id)
-);
-CREATE TABLE scan_comparison_matches (
- before_scan_id TEXT, after_scan_id TEXT, before_occurrence_id TEXT, after_occurrence_id TEXT
-);
-CREATE INDEX matches_before ON scan_comparison_matches(before_occurrence_id);
-CREATE INDEX matches_after ON scan_comparison_matches(after_occurrence_id);
-''')
-for scan in ('before', 'after', 'later', 'latest'):
- connection.execute('INSERT INTO scans VALUES (?, ?, ?, ?)', (scan, sys.argv[2], 'target', 'complete'))
-for scan, names in [('before', ('a1', 'a2')), ('after', ('b1', 'b2')),
- ('later', ('c1', 'c2')), ('latest', ('d1',))]:
- for name in names:
- severity = 'low' if name.endswith('1') else 'high'
- path = 'src/excluded.py' if name == 'a1' else 'src/covered.py'
- connection.execute('INSERT INTO finding_occurrences VALUES (?, ?, ?, ?, ?)', (name, name, scan, name, severity))
- connection.execute('INSERT INTO finding_locations VALUES (?, ?, ?, ?)', (name, path, 'root_control', 0))
-def link(before, after):
- connection.execute('''INSERT INTO scan_comparison_matches
- SELECT previous.scan_id, current.scan_id, previous.id, current.id
- FROM finding_occurrences AS previous, finding_occurrences AS current
- WHERE previous.id = ? AND current.id = ?''', (before, after))
-for before, after in [('a1', 'c1'), ('a2', 'c1'), ('b1', 'c2'), ('b2', 'c2')]:
- link(before, after)
-payload = {
- 'matches': [],
- 'uncertain': [{'beforeOccurrenceId': 'a1', 'afterOccurrenceId': 'b1', 'reason': 'Synthetic uncertainty.'}],
- 'related': [{'beforeOccurrenceId': 'a2', 'afterOccurrenceId': 'b2', 'reason': 'Separate synthetic controls.'}]
-}
-def cache():
- connection.execute('INSERT OR REPLACE INTO scan_comparisons VALUES (?, ?, ?)', ('before', 'after', json.dumps(payload)))
-coverage = {'includePaths': ['src'],
- 'excludePaths': ['src/excluded.py'], 'explicitExclusions': []}
-def compare():
- return history.compare_scans(
- connection, argparse.Namespace(before_scan_id='before', after_scan_id='after'),
- require_scan=lambda db, scan: db.execute('SELECT * FROM scans WHERE id = ?', (scan,)).fetchone(),
- read_coverage=lambda _: coverage, require_matches=True)
-cache()
-uncertain = compare()
-payload['uncertain'] = []
-cache()
-excluded = compare()
-coverage['excludePaths'] = []
-resolved = compare()
-connection.execute('INSERT INTO finding_triage VALUES (?, ?, ?)', ('a1', 'closed', 'already_fixed'))
-link('c1', 'd1')
-link('c2', 'd1')
-linked = compare()
-unchanged = json.loads(connection.execute('SELECT result_json FROM scan_comparisons').fetchone()[0]) == payload
-connection.execute("DELETE FROM scan_comparison_matches WHERE after_scan_id = 'latest'")
-restored = compare()
-print(json.dumps({'uncertain': uncertain, 'excluded': excluded, 'resolved': resolved,
- 'linked': linked, 'unchanged': unchanged, 'restored': restored}))
-`;
- const observed = await runPythonProbe(
- probe,
- join(tmpdir(), "codex-security-comparison-fixture"),
- );
- expect(observed).toMatchObject({
- uncertain: {
- summary: { new: 0, resolved: 0, unknown: 2 },
- findings: [
- {
- findingId: "a2",
- beforeOccurrenceIds: ["a1", "a2"],
- severity: "high",
- status: "unknown",
- reason: "Synthetic uncertainty.",
- },
- {
- findingId: "b2",
- afterOccurrenceIds: ["b1", "b2"],
- status: "unknown",
- reason: "Synthetic uncertainty.",
- },
- ],
- },
- excluded: { summary: { new: 1, resolved: 0, unknown: 1 } },
- resolved: { summary: { new: 1, resolved: 0, not_observed: 1, unknown: 0 } },
- linked: {
- summary: { new: 0, persisting: 0, reopened: 1, resolved: 0, unknown: 0 },
- findings: [
- {
- beforeOccurrenceIds: ["a1", "a2"],
- afterOccurrenceIds: ["b1", "b2"],
- matchReason: expect.any(String),
- status: "reopened",
- },
- ],
- },
- unchanged: true,
- });
- expect(observed["linked"]).not.toHaveProperty("related");
- expect(observed["restored"]).toEqual(observed["resolved"]);
-});
-
-test("loads displayed relations in bulk and follows current confirmed identities", async () => {
- const probe = `
-import json, sqlite3, sys
-sys.path.insert(0, sys.argv[1])
-import workbench_scan_history as history
-connection = sqlite3.connect(':memory:')
-connection.row_factory = sqlite3.Row
-connection.executescript('''
-CREATE TABLE scans (id TEXT PRIMARY KEY, target_id TEXT);
-CREATE INDEX scans_by_target ON scans(target_id, id);
-CREATE TABLE finding_occurrences (
- id TEXT PRIMARY KEY, finding_id TEXT, scan_id TEXT, title TEXT,
- UNIQUE(scan_id, finding_id)
-);
-CREATE INDEX occurrences_by_finding ON finding_occurrences(finding_id, id);
-CREATE TABLE scan_comparisons (before_scan_id TEXT, after_scan_id TEXT, result_json TEXT);
-CREATE TABLE scan_comparison_matches (
- before_scan_id TEXT, after_scan_id TEXT, before_occurrence_id TEXT, after_occurrence_id TEXT
-);
-CREATE INDEX matches_before ON scan_comparison_matches(before_occurrence_id);
-CREATE INDEX matches_after ON scan_comparison_matches(after_occurrence_id);
-''')
-connection.executemany('INSERT INTO scans VALUES (?, ?)', [
- ('one', 'target'), ('two', 'target'), ('three', 'clone'),
- ('four', 'clone'), ('foreign-one', 'unrelated-target'),
- ('foreign-two', 'unrelated-target')
-])
-connection.executemany('INSERT INTO finding_occurrences VALUES (?, ?, ?, ?)', (
- (f'{side}-{index}', f'{side}-identity-{index}', scan, f'Synthetic {side} {index}')
- for index in range(10_000)
- for side, scan in [('left', 'one'), ('right', 'two')]
-))
-payload = json.dumps({'matches': [], 'uncertain': [], 'related': [
- {'beforeOccurrenceId': f'left-{index}', 'afterOccurrenceId': f'right-{index}',
- 'reason': 'Separate synthetic controls.'}
- for index in range(10_000)
-]})
-connection.execute('INSERT INTO scan_comparisons VALUES (?, ?, ?)', ('one', 'two', payload))
-queries = []
-connection.set_trace_callback(queries.append)
-scoped = history.finding_relations(connection, 'one', ['left-0'])
-scoped_queries = len(queries)
-queries.clear()
-empty = history.finding_relations(connection, 'one', [])
-empty_queries = len(queries)
-connection.executemany('INSERT INTO finding_occurrences VALUES (?, ?, ?, ?)', [
- ('recurring-left', 'left-identity-0', 'four', 'Recurring control'),
- ('bridge', 'bridge-identity', 'three', 'Renamed control'),
- ('foreign-a', 'foreign-identity-a', 'foreign-one', 'Unrelated A'),
- ('foreign-b', 'foreign-identity-b', 'foreign-two', 'Unrelated B')
-])
-connection.executemany('INSERT INTO scan_comparison_matches VALUES (?, ?, ?, ?)', [
- ('four', 'three', 'recurring-left', 'bridge'),
- ('two', 'three', 'right-0', 'bridge'),
- ('foreign-one', 'foreign-two', 'foreign-a', 'foreign-b')
-])
-aliases = history._confirmed_finding_aliases(connection, ['left-0'])
-forward = history.finding_relations(connection, 'one', ['left-0'])
-reverse = history.finding_relations(connection, 'two', ['right-0'])
-remaining = history.finding_relations(connection, 'one', ['left-1'])
-unchanged = connection.execute('SELECT result_json FROM scan_comparisons').fetchone()[0] == payload
-connection.execute('DELETE FROM scan_comparison_matches WHERE before_occurrence_id = ?', ('right-0',))
-restored = history.finding_relations(connection, 'one', ['left-0']) == scoped
-
-limited = hasattr(connection, 'setlimit')
-if limited:
- old_limit = connection.setlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, 8)
-queries.clear()
-batched = history.finding_relations(connection, 'one', [f'left-{index}' for index in range(1, 11)])
-batched_queries = len(queries)
-if limited:
- connection.setlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER, old_limit)
-
-queries.clear()
-legacy_rows = list(history._rows_for_ids(
- connection, 'SELECT id FROM finding_occurrences WHERE id IN (SELECT value FROM json_each(?))',
- (f'left-{index}' for index in range(1001))
-))
-print(json.dumps({
- 'scoped': scoped, 'scopedQueries': scoped_queries, 'empty': empty,
- 'emptyQueries': empty_queries, 'aliases': sorted(aliases), 'forward': forward,
- 'reverse': reverse, 'remaining': sorted(remaining), 'unchanged': unchanged,
- 'restoredAfterUnlink': restored,
- 'batchedCount': len(batched), 'batchedQueries': batched_queries,
- 'legacyCount': len(legacy_rows), 'legacyQueries': len(queries)
-}))
-`;
- const observed = await runPythonProbe(probe);
- expect(observed).toMatchObject({
- scoped: {
- "left-0": [{ occurrenceId: "right-0", scanId: "two" }],
- },
- scopedQueries: 3,
- empty: {},
- emptyQueries: 0,
- aliases: ["bridge-identity", "left-identity-0", "right-identity-0"],
- forward: {},
- reverse: {},
- remaining: ["left-1"],
- unchanged: true,
- restoredAfterUnlink: true,
- batchedCount: 10,
- batchedQueries: 3,
- legacyCount: 1001,
- legacyQueries: 1,
- });
-});
-
-test("includes recurring stable identities in confirmed finding history", async () => {
- const observed = await runPythonProbe(`
-import json, sqlite3, sys
-sys.path.insert(0, sys.argv[1])
-from workbench_scan_history import finding_matches
-connection = sqlite3.connect(':memory:')
-connection.row_factory = sqlite3.Row
-connection.executescript('''
-CREATE TABLE scans (id TEXT PRIMARY KEY, started_at TEXT);
-CREATE TABLE finding_occurrences (id TEXT PRIMARY KEY, finding_id TEXT, scan_id TEXT, title TEXT);
-CREATE TABLE scan_comparison_matches (
- before_scan_id TEXT, after_scan_id TEXT, before_occurrence_id TEXT, after_occurrence_id TEXT, reason TEXT
-);
-''')
-scans = [('a', 'a'), ('b', 'b'), ('c', 'c'), ('a-repeat', 'a'), ('c-repeat', 'c'), ('unlinked', 'unlinked')]
-for index, (scan, finding) in enumerate(scans):
- connection.execute('INSERT INTO scans VALUES (?, ?)', (scan, f'2026-08-01T00:00:0{index}Z'))
- connection.execute('INSERT INTO finding_occurrences VALUES (?, ?, ?, ?)', (scan, finding, scan, scan))
-connection.executemany('INSERT INTO scan_comparison_matches VALUES (?, ?, ?, ?, ?)', [
- ('a', 'b', 'a', 'b', 'First confirmed link.'),
- ('b', 'c', 'b', 'c', 'Second confirmed link.')
-])
-def collect_history():
- result = {}
- for index, (scan, finding) in enumerate(scans):
- matches, first, bounds = finding_matches(connection, scan, scan, f'2026-08-01T00:00:0{index}Z')
- result[scan] = {'linked': [match['occurrenceId'] for match in matches], 'first': first, 'bounds': bounds}
- for match in matches:
- assert match['reason']
- if scan == 'a' and match['occurrenceId'] == 'b':
- assert match['reason'] == 'First confirmed link.'
- return result
-with_links = collect_history()
-connection.execute('DELETE FROM scan_comparison_matches')
-print(json.dumps({'withLinks': with_links, 'withoutLinks': collect_history()}))
-`);
- expect(observed).toEqual({
- withLinks: {
- a: {
- linked: ["a-repeat", "b", "c", "c-repeat"],
- first: "2026-08-01T00:00:00Z",
- bounds: ["a", "c-repeat"],
- },
- b: {
- linked: ["a", "a-repeat", "c", "c-repeat"],
- first: "2026-08-01T00:00:00Z",
- bounds: ["a", "c-repeat"],
- },
- c: {
- linked: ["a", "a-repeat", "b", "c-repeat"],
- first: "2026-08-01T00:00:00Z",
- bounds: ["a", "c-repeat"],
- },
- "a-repeat": {
- linked: ["a", "b", "c", "c-repeat"],
- first: "2026-08-01T00:00:00Z",
- bounds: ["a", "c-repeat"],
- },
- "c-repeat": {
- linked: ["a", "a-repeat", "b", "c"],
- first: "2026-08-01T00:00:00Z",
- bounds: ["a", "c-repeat"],
- },
- unlinked: {
- linked: [],
- first: "2026-08-01T00:00:05Z",
- bounds: ["unlinked"],
- },
- },
- withoutLinks: {
- a: {
- linked: ["a-repeat"],
- first: "2026-08-01T00:00:00Z",
- bounds: ["a", "a-repeat"],
- },
- "a-repeat": {
- linked: ["a"],
- first: "2026-08-01T00:00:00Z",
- bounds: ["a", "a-repeat"],
- },
- b: { linked: [], first: "2026-08-01T00:00:01Z", bounds: ["b"] },
- c: {
- linked: ["c-repeat"],
- first: "2026-08-01T00:00:02Z",
- bounds: ["c", "c-repeat"],
- },
- "c-repeat": {
- linked: ["c"],
- first: "2026-08-01T00:00:02Z",
- bounds: ["c", "c-repeat"],
- },
- unlinked: {
- linked: [],
- first: "2026-08-01T00:00:05Z",
- bounds: ["unlinked"],
- },
- },
- });
-});
-
test("loads oversized comparison matches from stdin", async () => {
const python = await resolvePluginPython();
const probe = [
- "import argparse, io, json, sqlite3, sys",
+ "import argparse, sqlite3, sys",
"sys.path.insert(0, sys.argv[1])",
"import workbench_scan_history as history",
"connection = sqlite3.connect(':memory:')",
@@ -762,11 +82,9 @@ test("loads oversized comparison matches from stdin", async () => {
"scans = {'before': {'id': 'before', 'status': 'complete', 'target_id': 'target', 'target_path': '/repo'}, 'after': {'id': 'after', 'status': 'complete', 'target_id': 'target', 'target_path': '/repo'}}",
"findings = {'before': {'old': {'id': 'old'}}, 'after': {'new': {'id': 'new'}}}",
"history._scan_findings = lambda _connection, scan_id: findings[scan_id]",
- "history.compare_scans = lambda *_args, **_kwargs: {'saved': True}",
- "payload = sys.stdin.read()",
- "sys.stdin = io.StringIO(payload)",
- "result = history.save_scan_comparison(connection, argparse.Namespace(before_scan_id='before', after_scan_id='after', matches_json=None, matches_json_stdin=True), now=lambda: 'now', require_scan=lambda _connection, scan_id: scans[scan_id], read_coverage=lambda _scan: {})",
- "print(json.dumps(result))",
+ "history.compare_scans = lambda *_args, **_kwargs: {}",
+ "history.save_scan_comparison(connection, argparse.Namespace(before_scan_id='before', after_scan_id='after', matches_json=None, matches_json_stdin=True), now=lambda: 'now', require_scan=lambda _connection, scan_id: scans[scan_id], read_coverage=lambda _scan: {})",
+ "print(connection.execute('SELECT result_json FROM scan_comparisons').fetchone()[0])",
].join("\n");
const payload = JSON.stringify({
matches: [
@@ -774,7 +92,7 @@ test("loads oversized comparison matches from stdin", async () => {
beforeOccurrenceIds: ["old"],
afterOccurrenceIds: ["new"],
confidence: "high",
- reason: "x".repeat(64 * 1024),
+ reason: "x".repeat(64 * 1024) + "🙂",
},
],
uncertain: [],
@@ -782,7 +100,7 @@ test("loads oversized comparison matches from stdin", async () => {
const result = await runCodexCommand(
{ command: python },
- ["-I", "-B", "-c", probe, join(PLUGIN_ROOT, "scripts")],
+ ["-I", "-X", "utf8", "-B", "-c", probe, join(PLUGIN_ROOT, "scripts")],
process.env,
payload,
AbortSignal.timeout(10_000),
@@ -790,5 +108,5 @@ test("loads oversized comparison matches from stdin", async () => {
expect(result.exitCode, result.stderr).toBe(0);
expect(result.stderr).toBe("");
- expect(JSON.parse(result.stdout)).toEqual({ saved: true });
+ expect(JSON.parse(result.stdout)).toEqual(JSON.parse(payload));
});