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: separate workflow orchestration from database rules (#733)

mldangelo-oai3f00aaa36fab
diff --git a/plugins/codex-security/tests/conftest.py b/plugins/codex-security/tests/conftest.py
new file mode 100644
index 000000000..f1d9d1975
--- /dev/null
+++ b/plugins/codex-security/tests/conftest.py
@@ -0,0 +1,33 @@
+from __future__ import annotations
+
+import runpy
+import sqlite3
+from contextlib import closing
+from pathlib import Path
+
+import pytest
+
+
[email protected](scope="session")
+def workbench_api():
+ script = Path(__file__).resolve().parents[1] / "scripts" / "workbench_db.py"
+ return runpy.run_path(str(script), run_name="codex_security_test_workbench")
+
+
[email protected](scope="session")
+def workbench_schema(workbench_api):
+ # Only the schema is shared. Each test receives its own database below.
+ with closing(sqlite3.connect(":memory:")) as connection:
+ connection.row_factory = sqlite3.Row
+ connection.execute("PRAGMA foreign_keys = ON")
+ workbench_api["apply_migrations"](connection)
+ yield connection
+
+
+def workbench_db(workbench_schema):
+ with closing(sqlite3.connect(":memory:")) as connection:
+ connection.row_factory = sqlite3.Row
+ connection.execute("PRAGMA foreign_keys = ON")
+ workbench_schema.backup(connection)
+ yield connection
diff --git a/plugins/codex-security/tests/test_workbench_finding_workflows.py b/plugins/codex-security/tests/test_workbench_finding_workflows.py
new file mode 100644
index 000000000..c4a69b8de
--- /dev/null
+++ b/plugins/codex-security/tests/test_workbench_finding_workflows.py
@@ -0,0 +1,305 @@
+from __future__ import annotations
+
+import json
+import sqlite3
+
+import pytest
+
+TIMESTAMP = "2026-08-01T00:00:00Z"
+
+
+def workflow(api, connection, action, *, workflow_id="synthetic-workflow", **payload):
+ return api["finding_workflow"](
+ connection, {"id": workflow_id, "action": action, **payload}, TIMESTAMP
+ )
+
+
[email protected]("stage", ["scan", "publish", "dedupe"])
+def test_failed_stages_resume_and_completed_results_are_immutable(
+ workbench_api, workbench_db, stage
+):
+ assert workflow(workbench_api, workbench_db, "get") == {"workflow": None}
+ bound = workflow(workbench_api, workbench_db, "bind", binding={})["workflow"]
+ assert bound["stages"] == {
+ name: {"status": "pending"} for name in ("scan", "publish", "dedupe")
+ }
+ assert (
+ workflow(workbench_api, workbench_db, "begin", stage=stage)["workflow"]["stages"][stage][
+ "status"
+ ]
+ == "running"
+ )
+ failed = workflow(workbench_api, workbench_db, "fail", stage=stage, error="Synthetic failure")
+ assert failed["workflow"]["stages"][stage] == {"status": "failed", "error": "Synthetic failure"}
+ assert (
+ workflow(workbench_api, workbench_db, "begin", stage=stage)["workflow"]["stages"][stage][
+ "status"
+ ]
+ == "running"
+ )
+ result = {"findingIds": [], "duplicateGroups": []}
+ completed = workflow(workbench_api, workbench_db, "complete", stage=stage, result=result)
+ assert completed["workflow"]["stages"][stage] == {"status": "completed", "result": result}
+ for action, payload in [
+ ("begin", {}),
+ ("fail", {"error": "Late failure"}),
+ ("complete", {"result": {"unexpected": True}}),
+ ]:
+ assert workflow(workbench_api, workbench_db, action, stage=stage, **payload) == completed
+
+
+ ("binding", "changed"),
+ [
+ ({"scanId": "scan-a"}, {"scanId": "scan-b"}),
+ ({"scanDir": "/synthetic/scan-a"}, {"scanDir": "/synthetic/scan-b"}),
+ ({"destination": "https://synthetic.invalid/"}, {"destination": "https://other.invalid/"}),
+ ({"scope": {"repositoryId": "repository-a"}}, {"scope": {"allRepositories": True}}),
+ ({"scope": {"allRepositories": True}}, {"scope": {"repositoryId": "repository-a"}}),
+ ],
+)
+def test_bindings_and_workflow_identities_remain_separate(
+ workbench_api, workbench_db, binding, changed
+):
+ workflow(workbench_api, workbench_db, "bind", binding=binding)
+ saved = workflow(
+ workbench_api, workbench_db, "complete", stage="dedupe", result={"duplicateGroups": []}
+ )
+ with pytest.raises(SystemExit, match="already bound to a different"):
+ workflow(workbench_api, workbench_db, "bind", binding=changed)
+ assert not workbench_db.in_transaction
+ assert workflow(workbench_api, workbench_db, "get") == saved
+ separate = workflow(
+ workbench_api, workbench_db, "bind", workflow_id="separate", binding=changed
+ )
+ assert separate["workflow"]["id"] == "separate"
+ assert separate["workflow"]["stages"]["dedupe"] == {"status": "pending"}
+ for key, value in changed.items():
+ assert separate["workflow"][key] == value
+ assert workflow(workbench_api, workbench_db, "get") == saved
+
+
[email protected]("result", [{"duplicateGroups": []}, {"duplicateGroups": [["a", "b"]]}])
+def test_pending_publication_payload_survives_failure_until_completion(
+ workbench_api, workbench_db, result
+):
+ workflow(workbench_api, workbench_db, "bind", binding={})
+ workflow(workbench_api, workbench_db, "begin", stage="dedupe")
+ pending = {"groups": result["duplicateGroups"]}
+ prepared = workflow(
+ workbench_api,
+ workbench_db,
+ "prepare-dedupe",
+ stage="dedupe",
+ result=result,
+ pendingWrite=pending,
+ )
+ assert prepared["workflow"]["stages"]["dedupe"] == {
+ "status": "running",
+ "result": result,
+ "pendingWrite": pending,
+ }
+ workflow(workbench_api, workbench_db, "fail", stage="dedupe", error="Lost acknowledgement")
+ resumed = workflow(workbench_api, workbench_db, "begin", stage="dedupe")["workflow"]["stages"][
+ "dedupe"
+ ]
+ assert resumed["result"] == result
+ assert resumed["pendingWrite"] == pending
+ completed = workflow(workbench_api, workbench_db, "complete", stage="dedupe", result=result)
+ assert completed["workflow"]["stages"]["dedupe"] == {"status": "completed", "result": result}
+
+
+def test_review_checkpoints_keep_the_first_valid_result_and_enforce_workflow_ownership(
+ workbench_api, workbench_db
+):
+ binding = {
+ "version": 1,
+ "codexVersion": "synthetic-version",
+ "source": {
+ "repository": "/synthetic/repository",
+ "revision": "synthetic-revision",
+ "refsDigest": "synthetic-refs",
+ "content": "synthetic-content",
+ },
+ "scope": {"repositoryId": "synthetic-repository"},
+ "model": "synthetic-model",
+ "effort": "high",
+ "settingsDigest": "synthetic-settings",
+ "promptDigest": "synthetic-prompt",
+ "contractDigest": "synthetic-contract",
+ }
+ result = {"decision": "DISTINCT", "rationale": "Independent corrections"}
+ with pytest.raises(sqlite3.IntegrityError):
+ workflow(
+ workbench_api,
+ workbench_db,
+ "save-review",
+ key="review-1",
+ binding=binding,
+ result=result,
+ )
+ workflow(workbench_api, workbench_db, "bind", binding={})
+ assert workflow(workbench_api, workbench_db, "get-review", key="review-1") == {"review": None}
+ workflow(
+ workbench_api, workbench_db, "save-review", key="review-1", binding=binding, result=result
+ )
+ workflow(
+ workbench_api,
+ workbench_db,
+ "save-review",
+ key="review-1",
+ binding=binding,
+ result={"unexpected": True},
+ )
+ assert workflow(workbench_api, workbench_db, "get-review", key="review-1") == {"review": result}
+ assert workflow(
+ workbench_api, workbench_db, "get-review", workflow_id="another", key="review-1"
+ ) == {"review": None}
+ row = workbench_db.execute("SELECT * FROM finding_workflow_reviews").fetchone()
+ assert row["source_repository_path"] == binding["source"]["repository"]
+ assert row["settings_digest"] == binding["settingsDigest"]
+ assert json.loads(row["result_json"]) == result
+
+
+ "payload",
+ [
+ {"action": "bind", "binding": {"unknown": "value"}},
+ {"action": "begin", "stage": "unknown"},
+ {"action": "unknown", "stage": "scan"},
+ ],
+)
+def test_rejected_workflow_mutations_roll_back_without_leaking_state(
+ workbench_api, workbench_db, payload
+):
+ saved = workflow(workbench_api, workbench_db, "bind", binding={})
+ with pytest.raises(SystemExit):
+ workflow(workbench_api, workbench_db, **payload)
+ assert not workbench_db.in_transaction
+ assert workflow(workbench_api, workbench_db, "get") == saved
+
+
+def test_workflow_column_migration_is_atomic_and_preserves_resume_state(workbench_api, tmp_path):
+ database = tmp_path / "legacy.sqlite3"
+ completed = {
+ "id": "migrated-completed",
+ "repositoryPath": str(tmp_path / "repository"),
+ "scanRequestDigest": "synthetic-request-hash",
+ "scanId": "synthetic-scan",
+ "scanDir": str(tmp_path / "scan"),
+ "artifactDigest": "synthetic-artifact-hash",
+ "destination": "https://synthetic.invalid/",
+ "scope": {"repositoryId": "synthetic-repository"},
+ "stages": {
+ "scan": {"status": "completed", "result": None},
+ "publish": {"status": "completed", "result": {"findingIds": []}},
+ "dedupe": {"status": "completed", "result": {"duplicateGroups": []}},
+ },
+ }
+ unfinished = {
+ "id": "migrated-unfinished",
+ "scope": {"allRepositories": True},
+ "stages": {
+ "scan": {"status": "failed", "error": "Synthetic interruption"},
+ "publish": {"status": "running", "error": "Synthetic earlier failure"},
+ "dedupe": {
+ "status": "failed",
+ "error": "Synthetic lost acknowledgement",
+ "result": {"duplicateGroups": [["a", "b"]]},
+ "pendingWrite": {"groups": [["a", "b"]]},
+ },
+ },
+ }
+ pending = {
+ "id": "migrated-pending",
+ "stages": {stage: {"status": "pending"} for stage in ("scan", "publish", "dedupe")},
+ }
+ migrations = workbench_api["MIGRATIONS"]
+ connection = sqlite3.connect(database)
+ connection.row_factory = sqlite3.Row
+ connection.execute("PRAGMA foreign_keys = ON")
+
+ def migrate(history):
+ workbench_api["apply_schema_migrations"](
+ connection, history, lambda: TIMESTAMP, workbench_api["backfill_security_targets"]
+ )
+
+ try:
+ migrate(tuple(m for m in migrations if m[0] <= 36))
+ with connection:
+ for state in (completed, unfinished, pending):
+ connection.execute(
+ "INSERT INTO finding_workflows VALUES (?, ?, ?, ?)",
+ (state["id"], json.dumps(state), TIMESTAMP, "2026-08-02T00:00:00Z"),
+ )
+ connection.execute(
+ "CREATE TABLE synthetic_workflow_references "
+ "(workflow_id TEXT REFERENCES finding_workflows(id) ON DELETE CASCADE)"
+ )
+ connection.execute(
+ "INSERT INTO synthetic_workflow_references VALUES (?)", (completed["id"],)
+ )
+ before = list(connection.iterdump())
+ with pytest.raises(sqlite3.OperationalError):
+ migrate(
+ (
+ *migrations,
+ (999, "synthetic failure", "INSERT INTO synthetic_missing_table VALUES (1);"),
+ )
+ )
+ assert not connection.in_transaction
+ assert list(connection.iterdump()) == before
+ migrate(migrations)
+ after = list(connection.iterdump())
+ migrate(migrations)
+ assert list(connection.iterdump()) == after
+ assert "state_json" not in {
+ row["name"] for row in connection.execute("PRAGMA table_info(finding_workflows)")
+ }
+ assert (
+ connection.execute("SELECT count(*) FROM synthetic_workflow_references").fetchone()[0]
+ == 1
+ )
+ assert connection.execute("PRAGMA integrity_check").fetchone()[0] == "ok"
+ assert list(connection.execute("PRAGMA foreign_key_check")) == []
+ row = connection.execute(
+ "SELECT * FROM finding_workflows WHERE id = ?", (completed["id"],)
+ ).fetchone()
+ assert row["repository_path"] == completed["repositoryPath"]
+ assert row["scan_request_digest"] == completed["scanRequestDigest"]
+ assert row["artifact_digest"] == completed["artifactDigest"]
+ assert row["scope_all_repositories"] is None
+ assert row["created_at"] == TIMESTAMP
+ assert row["updated_at"] == "2026-08-02T00:00:00Z"
+ finally:
+ connection.close()
+
+ # Reopen the actual file so this remains a persistence and migration test.
+ connection = sqlite3.connect(database)
+ connection.row_factory = sqlite3.Row
+ connection.execute("PRAGMA foreign_keys = ON")
+ try:
+ for state in (completed, unfinished, pending):
+ assert workflow(workbench_api, connection, "get", workflow_id=state["id"]) == {
+ "workflow": state
+ }
+ for stage in ("scan", "publish", "dedupe"):
+ state = workflow(
+ workbench_api, connection, "begin", workflow_id=completed["id"], stage=stage
+ )
+ assert state == {"workflow": completed}
+ workflow(workbench_api, connection, "begin", workflow_id=unfinished["id"], stage="scan")
+ resumed = workflow(
+ workbench_api,
+ connection,
+ "complete",
+ workflow_id=unfinished["id"],
+ stage="scan",
+ result={"scanId": "resumed-scan"},
+ )
+ assert resumed["workflow"]["stages"]["scan"] == {
+ "status": "completed",
+ "result": {"scanId": "resumed-scan"},
+ }
+ finally:
+ connection.close()
diff --git a/plugins/codex-security/tests/test_workbench_native_index_queries.py b/plugins/codex-security/tests/test_workbench_native_index_queries.py
new file mode 100644
index 000000000..1fa9df24c
--- /dev/null
+++ b/plugins/codex-security/tests/test_workbench_native_index_queries.py
@@ -0,0 +1,190 @@
+from __future__ import annotations
+
+from argparse import Namespace
+
+import pytest
+from workbench_test_support import stable_target_id
+
+SCAN_IDS = [f"00000000-0000-4000-8000-{index:012d}" for index in range(3)]
+
+
+def query_args(**values):
+ return Namespace(
+ **{
+ "repository": None,
+ "scan_root": None,
+ "target_id": None,
+ "mode": None,
+ "query": None,
+ "severity": None,
+ "status": None,
+ "limit": 100,
+ "offset": 0,
+ **values,
+ }
+ )
+
+
+def indexed_collections(workbench_db, tmp_path):
+ timestamp = "2026-08-01T00:00:00Z"
+ targets = []
+ # Populate query inputs directly. Scan completion and index registration stay
+ # covered by test_workbench_native_indexes.py's real-process lifecycle tests.
+ with workbench_db:
+ for index, name in enumerate(("needle-first", "needle-second", "unrelated")):
+ target = tmp_path / name
+ target.mkdir()
+ target = target.resolve()
+ targets.append(target)
+ target_id = stable_target_id(target)
+ workbench_db.execute(
+ "INSERT INTO security_targets (id, current_path, display_name, created_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?)",
+ (target_id, str(target), name, timestamp, timestamp),
+ )
+ workbench_db.execute(
+ "INSERT INTO workspaces (id, target_id, created_at, updated_at) VALUES (?, ?, ?, ?)",
+ (f"10000000-0000-4000-8000-{index:012d}", target_id, timestamp, timestamp),
+ )
+ workbench_db.execute(
+ "INSERT INTO scans (id, workspace_id, target_id, target_path, target_revision, "
+ "scope, mode, scan_dir, status, phase, started_at, completed_at, created_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?, '.', 'standard', ?, 'complete', 'reporting', ?, ?, ?, ?)",
+ (
+ SCAN_IDS[index],
+ f"10000000-0000-4000-8000-{index:012d}",
+ target_id,
+ str(target),
+ "synthetic-revision",
+ str(tmp_path / SCAN_IDS[index]),
+ timestamp,
+ timestamp,
+ timestamp,
+ timestamp,
+ ),
+ )
+ workbench_db.execute(
+ "INSERT INTO scan_progress (scan_id, updated_at) VALUES (?, ?)",
+ (SCAN_IDS[index], timestamp),
+ )
+ workbench_db.execute(
+ "INSERT INTO findings (id, fingerprint, rule_id, identity_anchor, created_at, updated_at) "
+ "VALUES (?, ?, ?, ?, ?, ?)",
+ (
+ f"finding-{index}",
+ f"fingerprint-{index}",
+ "synthetic-rule",
+ name,
+ timestamp,
+ timestamp,
+ ),
+ )
+ workbench_db.execute(
+ "INSERT INTO finding_occurrences (id, finding_id, scan_id, title, summary, severity, "
+ "confidence, remediation, details_json, created_at) "
+ "VALUES (?, ?, ?, ?, ?, 'high', 'high', ?, ?, ?)",
+ (
+ f"occurrence-{index}",
+ f"finding-{index}",
+ SCAN_IDS[index],
+ "Output directory traversal",
+ "Synthetic finding summary",
+ "Constrain the path",
+ '{"taxonomy":{"category":"path-traversal"}}',
+ timestamp,
+ ),
+ )
+ return workbench_db, targets
+
+
+def test_global_finding_filters_apply_before_pagination(workbench_api, indexed_collections):
+ connection, targets = indexed_collections
+ query = workbench_api["native_indexes"].list_global_findings
+ filters = {"query": "NeEdLe", "severity": "high", "status": "open"}
+ first = query(connection, query_args(**filters, limit=1))
+ second = query(connection, query_args(**filters, limit=1, offset=1))
+ assert first["nextOffset"] == 1
+ assert second["nextOffset"] is None
+ assert {first["findings"][0]["scanId"], second["findings"][0]["scanId"]} == {
+ SCAN_IDS[0],
+ SCAN_IDS[1],
+ }
+
+ targeted = query(connection, query_args(**filters, target_id=stable_target_id(targets[0])))
+ assert [finding["scanId"] for finding in targeted["findings"]] == [SCAN_IDS[0]]
+ assert query(connection, query_args(query="needle", severity="low"))["findings"] == []
+ unfiltered = query(connection, query_args())
+ assert set(unfiltered) == {"findings", "limit", "nextOffset", "offset"}
+ assert len(unfiltered["findings"]) == 3
+
+
+def test_scan_findings_support_search_severity_and_triage_filters(
+ workbench_api, indexed_collections
+):
+ connection, _ = indexed_collections
+ workbench_api["set_finding_triage"](
+ connection,
+ Namespace(
+ occurrence_id="occurrence-0",
+ status="closed",
+ close_reason="false_positive",
+ note="The reported path is not reachable.",
+ ),
+ )
+ query = workbench_api["list_findings"]
+ filtered = query(
+ connection,
+ query_args(
+ scan_id=SCAN_IDS[0],
+ query="OUTPUT DIRECTORY",
+ severity="high",
+ status="closed",
+ limit=1,
+ ),
+ )["findingsPage"]
+ assert [finding["occurrenceId"] for finding in filtered["findings"]] == ["occurrence-0"]
+ assert filtered["total"] == 1
+ assert filtered["nextOffset"] is None
+ assert (
+ query(connection, query_args(scan_id=SCAN_IDS[0], status="open"))["findingsPage"][
+ "findings"
+ ]
+ == []
+ )
+
+
+ ("command", "collection", "status"),
+ [("list-scans", "scans", "complete"), ("list-repositories", "repositories", "scanned")],
+)
+def test_collection_filters_apply_before_pagination(
+ workbench_api, indexed_collections, command, collection, status
+):
+ connection, targets = indexed_collections
+ query = (
+ workbench_api["scan_history"].list_scans
+ if command == "list-scans"
+ else workbench_api["native_indexes"].list_repositories
+ )
+ filters = {"query": "NeEdLe", "status": status}
+ if command == "list-scans":
+ filters["mode"] = "standard"
+ first = query(connection, query_args(**filters, limit=1))
+ second = query(connection, query_args(**filters, limit=1, offset=1))
+ assert first["nextOffset"] == 1
+ assert second["nextOffset"] is None
+ assert {first[collection][0]["targetId"], second[collection][0]["targetId"]} == {
+ stable_target_id(targets[0]),
+ stable_target_id(targets[1]),
+ }
+ targeted = query(
+ connection, query_args(**filters, target_id=stable_target_id(targets[0]), limit=None)
+ )
+ assert [item["targetId"] for item in targeted[collection]] == [stable_target_id(targets[0])]
+ unfiltered = query(connection, query_args(limit=None))
+ assert set(unfiltered) == {collection}
+ assert len(unfiltered[collection]) == 3
+ if command == "list-repositories":
+ assert query(connection, query_args(status="not_scanned"))[collection] == []
+ assert query(connection, query_args(status="open_findings"))[collection]
diff --git a/plugins/codex-security/tests/test_workbench_native_indexes.py b/plugins/codex-security/tests/test_workbench_native_indexes.py
index f57170930..2c562c92d 100644
--- a/plugins/codex-security/tests/test_workbench_native_indexes.py
+++ b/plugins/codex-security/tests/test_workbench_native_indexes.py
@@ -135,138 +135,6 @@ def test_global_findings_apply_pagination_to_historical_findings(
}
-def indexed_collection_targets(
- tmp_path: Path,
-) -> tuple[Path, Path, dict[str, object], dict[str, object]]:
- state_dir = tmp_path / "state"
- first_target, second_target, unrelated_target = (
- tmp_path / name for name in ("needle-first", "needle-second", "unrelated")
- )
- for target in (first_target, second_target, unrelated_target):
- target.mkdir()
- first = complete_scan(state_dir, first_target, identity_anchor="first-finding")
- second = complete_scan(state_dir, second_target, identity_anchor="second-finding")
- complete_scan(state_dir, unrelated_target, identity_anchor="unrelated-finding")
- return state_dir, first_target, first, second
-
-
-def test_global_finding_filters_apply_before_pagination(
- indexed_collection_targets: tuple[Path, Path, dict[str, object], dict[str, object]],
-) -> None:
- state_dir, first_target, first, second = indexed_collection_targets
- filters = ("--query", "NeEdLe", "--severity", "high", "--status", "open")
- first_page = run_workbench(state_dir, "list-global-findings", *filters, "--limit", "1")
- second_page = run_workbench(
- state_dir, "list-global-findings", *filters, "--limit", "1", "--offset", "1"
- )
- assert first_page["nextOffset"] == 1
- assert second_page["nextOffset"] is None
- assert {first_page["findings"][0]["scanId"], second_page["findings"][0]["scanId"]} == {
- first["scanId"],
- second["scanId"],
- }
-
- targeted = run_workbench(
- state_dir,
- "list-global-findings",
- *filters,
- "--target-id",
- stable_target_id(first_target),
- )
- assert [finding["scanId"] for finding in targeted["findings"]] == [first["scanId"]]
- assert (
- run_workbench(state_dir, "list-global-findings", "--query", "needle", "--severity", "low")[
- "findings"
- ]
- == []
- )
- unfiltered = run_workbench(state_dir, "list-global-findings")
- assert set(unfiltered) == {"findings", "limit", "nextOffset", "offset"}
- assert len(unfiltered["findings"]) == 3
-
-
-def test_scan_findings_support_search_severity_and_triage_filters(
- indexed_collection_targets: tuple[Path, Path, dict[str, object], dict[str, object]],
-) -> None:
- state_dir, _, scan, _ = indexed_collection_targets
- occurrence_id = str(scan["findings"][0]["occurrenceId"])
- run_workbench(
- state_dir,
- "set-finding-triage",
- "--occurrence-id",
- occurrence_id,
- "--status",
- "closed",
- "--close-reason",
- "false_positive",
- "--note",
- "The reported path is not reachable.",
- )
- scan_args = ("--scan-id", str(scan["scanId"]))
- filtered = run_workbench(
- state_dir,
- "list-findings",
- *scan_args,
- "--query",
- "OUTPUT DIRECTORY",
- "--severity",
- "high",
- "--status",
- "closed",
- "--limit",
- "1",
- )["findingsPage"]
- assert [finding["occurrenceId"] for finding in filtered["findings"]] == [occurrence_id]
- assert filtered["total"] == 1
- assert filtered["nextOffset"] is None
- assert (
- run_workbench(state_dir, "list-findings", *scan_args, "--status", "open")["findingsPage"][
- "findings"
- ]
- == []
- )
-
-
- ("command", "collection", "status"),
- [("list-scans", "scans", "complete"), ("list-repositories", "repositories", "scanned")],
-)
-def test_collection_filters_apply_before_pagination(
- indexed_collection_targets: tuple[Path, Path, dict[str, object], dict[str, object]],
- command: str,
- collection: str,
- status: str,
-) -> None:
- state_dir, first_target, _, _ = indexed_collection_targets
- filters = ("--query", "NeEdLe", "--status", status)
- if command == "list-scans":
- filters += ("--mode", "standard")
- first_page = run_workbench(state_dir, command, *filters, "--limit", "1")
- second_page = run_workbench(state_dir, command, *filters, "--limit", "1", "--offset", "1")
- assert first_page["nextOffset"] == 1
- assert second_page["nextOffset"] is None
- assert {
- first_page[collection][0]["targetId"],
- second_page[collection][0]["targetId"],
- } == {
- stable_target_id(first_target),
- stable_target_id(first_target.parent / "needle-second"),
- }
-
- targeted = run_workbench(
- state_dir, command, *filters, "--target-id", stable_target_id(first_target)
- )
- assert [item["targetId"] for item in targeted[collection]] == [stable_target_id(first_target)]
- unfiltered = run_workbench(state_dir, command)
- assert set(unfiltered) == {collection}
- assert len(unfiltered[collection]) == 3
-
- if command == "list-repositories":
- assert run_workbench(state_dir, command, "--status", "not_scanned")[collection] == []
- assert run_workbench(state_dir, command, "--status", "open_findings")[collection]
-
-
def test_global_findings_keep_latest_occurrence_and_stable_target_identity(tmp_path: Path) -> None:
state_dir = tmp_path / "state"
first_target = tmp_path / "first-repo"
diff --git a/sdk/typescript/TESTING.md b/sdk/typescript/TESTING.md
index 6e3f88df7..187a5b8fc 100644
--- a/sdk/typescript/TESTING.md
+++ b/sdk/typescript/TESTING.md
@@ -51,6 +51,16 @@ before proposing a coverage floor.
boundaries are the behavior under test. Do not use live model credentials.
- Use the typed `TestClient` and `createApiTestFixtures` helpers for API tests.
Do not add a production abstraction solely to support a mock.
+- Test workflow retry, resume, and checkpoint permutations with the existing
+ injected workbench dependency. `scriptedWorkbench` rejects unexpected
+ requests; `checkpointWorkbench` stores review results without reproducing the
+ Python workflow engine. Keep real workbench calls in the focused workflow
+ integration file for process termination, migration, and source snapshots.
+- Test Python database behavior by calling the production functions in pytest.
+ The `workbench_db` fixture copies a migrated, empty schema into a fresh
+ in-memory SQLite database for each test, with foreign keys enabled. Use
+ file-backed databases for migrations, reopening, locking, and crash recovery;
+ an in-memory database cannot exercise those boundaries.
- Restore spies, timers, and environment changes. Tests that change the process
cwd or install persistent ESM module mocks use `runTestInSubprocess`.
Per-file Bun isolation does not isolate process-wide state inside one file.
diff --git a/sdk/typescript/tests-ts/finding-workflow-integration.test.ts b/sdk/typescript/tests-ts/finding-workflow-integration.test.ts
new file mode 100644
index 000000000..ebfed82c1
--- /dev/null
+++ b/sdk/typescript/tests-ts/finding-workflow-integration.test.ts
@@ -0,0 +1,770 @@
+import { execFileSync, spawn } from "node:child_process";
+import { createHash } from "node:crypto";
+import { once } from "node:events";
+import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
+import { join } from "node:path";
+import { afterEach, expect, test } from "bun:test";
+import type { JsonObject } from "../src/config.js";
+import { FindingWorkflow } from "../src/finding-workflow.js";
+import { publishScanToCustomInternal } from "../src/custom-publish.js";
+import { deduplicateScanInternal } from "../src/deduplication/scan.js";
+import {
+ resolvePluginPython,
+ runCodexCommand,
+ runWorkbench,
+} from "../src/runtime.js";
+import type { Finding, ScanManifest } from "../src/models.js";
+import type { CodexReview } from "../src/deduplication/codex-review.js";
+import { CheckpointedReviewRunner } from "../src/deduplication/checkpointed-review.js";
+import {
+ CodexDeduplicationReviewer,
+ type DuplicateDecision,
+} from "../src/deduplication/deduplication-reviewer.js";
+import { PLUGIN_ROOT } from "./plugin-root.js";
+import { workflowFixture } from "./support/workflow-fixture.js";
+
+const fixtures: Array<Awaited<ReturnType<typeof workflowFixture>>> = [];
+afterEach(async () => {
+ await Promise.all(
+ fixtures.splice(0).map((fixture) => fixture[Symbol.asyncDispose]()),
+ );
+});
+
+async function fixture() {
+ const value = await workflowFixture();
+ fixtures.push(value);
+ const { environment, document, scanDir, repository } = value;
+ const workbenchOptions = {
+ environment,
+ pluginRoot: PLUGIN_ROOT,
+ python: await resolvePluginPython({ environment }),
+ };
+ const history = async (args: readonly string[], input?: string) =>
+ args[0] === "get-scan"
+ ? {
+ scan: {
+ scanId: document.scanId,
+ scanDir,
+ targetPath: repository,
+ progress: { status: "complete" },
+ },
+ }
+ : await runWorkbench(workbenchOptions, args, input);
+ return { ...value, history, workbenchOptions };
+}
+
+async function restoreLegacyWorkflow(
+ environment: NodeJS.ProcessEnv,
+ state: object,
+ reviews: object[],
+) {
+ const probe = await runCodexCommand(
+ { command: await resolvePluginPython({ environment }) },
+ [
+ "-I",
+ "-B",
+ "-c",
+ `import json, sqlite3, sys
+sys.path.insert(0, sys.argv[1])
+from workbench_schema import MIGRATIONS, apply_migrations, sql_statements
+db = sqlite3.connect(sys.argv[2])
+db.row_factory = sqlite3.Row
+db.execute("PRAGMA foreign_keys = ON")
+payload = json.load(sys.stdin)
+state = payload["state"]
+timestamp = "2026-08-01T00:00:00Z"
+with db:
+ db.execute("DROP TABLE finding_workflow_reviews")
+ db.execute("DROP TABLE finding_workflows")
+ db.execute("DELETE FROM schema_migrations WHERE version >= 38")
+ for version, _, sql in MIGRATIONS:
+ if version in (36, 37):
+ for statement in sql_statements(sql):
+ db.execute(statement)
+ db.execute("INSERT INTO finding_workflows VALUES (?, ?, ?, ?)",
+ (state["id"], json.dumps(state), timestamp, timestamp))
+ for review in payload["reviews"]:
+ db.execute("INSERT INTO finding_workflow_reviews VALUES (?, ?, ?, ?, ?)",
+ (state["id"], review["key"], json.dumps(review["binding"]), json.dumps(review["result"]), timestamp))
+before = list(db.iterdump())
+try:
+ apply_migrations(db, (*MIGRATIONS, (999, "synthetic failure", "INSERT INTO synthetic_missing_table VALUES (1);")), lambda: timestamp, lambda _: None)
+except sqlite3.OperationalError:
+ pass
+else:
+ raise AssertionError("Migration should fail")
+assert not db.in_transaction
+assert list(db.iterdump()) == before, "Failed migration must preserve workflow and review rows together"
+db.close()`,
+ join(PLUGIN_ROOT, "scripts"),
+ join(environment["CODEX_SECURITY_STATE_DIR"]!, "workbench.sqlite3"),
+ ],
+ environment,
+ JSON.stringify({ state, reviews }),
+ );
+ expect(probe.exitCode, probe.stderr).toBe(0);
+}
+
+test("scan registration commits its workflow identity atomically and rolls back failed registration", async () => {
+ const { root, repository, environment, workbenchOptions } = await fixture();
+ const workflow = new FindingWorkflow("registered-workflow", environment);
+ await workflow.bind({ repositoryPath: repository });
+ await workflow.begin("scan");
+ const register = async (workflowId: string, suffix: string) => {
+ const directory = join(root, suffix);
+ await mkdir(directory, { mode: 0o700 });
+ return await runWorkbench(
+ workbenchOptions,
+ [
+ "register-cli-scan",
+ "--repository",
+ repository,
+ "--scan-dir",
+ directory,
+ "--registration-json-stdin",
+ ],
+ JSON.stringify({
+ workflowId,
+ recipe: {
+ repository,
+ mode: "standard",
+ target: { kind: "repository", paths: [] },
+ config: {},
+ },
+ }),
+ );
+ };
+ const registered = await register("registered-workflow", "registered-scan");
+ expect(
+ await new FindingWorkflow("registered-workflow", environment).get(),
+ ).toMatchObject({
+ scanId: registered["scanId"],
+ scanDir: registered["scanDir"],
+ stages: { scan: { status: "running" } },
+ });
+ await expect(
+ register("unknown-workflow", "rolled-back-scan"),
+ ).rejects.toThrow("must be started");
+ const history = await runWorkbench(workbenchOptions, [
+ "list-scans",
+ "--repository",
+ repository,
+ ]);
+ expect(history["scans"]).toHaveLength(1);
+ const retried = await register("registered-workflow", "retried-scan");
+ expect(retried["scanId"]).not.toBe(registered["scanId"]);
+ expect((await workflow.get())?.scanId).toBe(retried["scanId"] as string);
+});
+
+test.each(["running", "completed"])(
+ "survives termination with a %s stage and resumes in a fresh process",
+ async (status) => {
+ const { environment } = await fixture();
+ const source = `import { FindingWorkflow } from ${JSON.stringify(new URL("../src/finding-workflow.ts", import.meta.url).href)};
+const workflow = new FindingWorkflow("interrupted", process.env);
+const timer = setInterval(() => {}, 1000);
+await workflow.run("publish", async () => {
+ if (${JSON.stringify(status)} === "running") {
+ process.stdout.write("ready\\n");
+ await new Promise(() => {});
+ }
+ return { findingIds: [] };
+});
+process.stdout.write("ready\\n");`;
+ const child = spawn(process.execPath, ["--eval", source], {
+ env: environment,
+ stdio: ["ignore", "pipe", "pipe"],
+ windowsHide: true,
+ });
+ const closed = once(child, "close");
+ let errors = "";
+ child.stderr.on("data", (chunk) => {
+ errors += String(chunk);
+ });
+ try {
+ const ready = await Promise.race([
+ once(child.stdout, "data").then(([data]) => String(data)),
+ closed.then(() => {
+ throw new Error(
+ errors || "Workflow child exited before the checkpoint",
+ );
+ }),
+ ]);
+ expect(ready).toContain("ready");
+ child.kill();
+ await closed;
+ const workflow = new FindingWorkflow("interrupted", environment);
+ expect((await workflow.get())?.stages.publish.status).toBe(status);
+ let attempts = 0;
+ expect(
+ await workflow.run("publish", async () => {
+ attempts++;
+ return { findingIds: [] };
+ }),
+ ).toEqual({ findingIds: [] });
+ expect(attempts).toBe(status === "running" ? 1 : 0);
+ expect(
+ (await new FindingWorkflow("interrupted", environment).get())?.stages
+ .publish,
+ ).toEqual({ status: "completed", result: { findingIds: [] } });
+ } finally {
+ if (child.exitCode === null && child.signalCode === null) child.kill();
+ await closed;
+ }
+ },
+);
+
+test("reuses publication after dedupe failure and persists a successful empty duplicate result", async () => {
+ const { scanDir, environment, document, history } = await fixture();
+ const original = await readFile(join(scanDir, "findings.json"), "utf8");
+ const workflowId = "resume-example";
+ const options = { workflowId, findingsUrl: "http://synthetic.test/service" };
+ let publications = 0;
+ let lookups = 0;
+ let unavailable = true;
+ const request = async (url: URL) => {
+ if (url.pathname.endsWith("/bulk/findings")) {
+ publications++;
+ return Response.json(
+ document.findings.map((finding) => finding.findingId),
+ );
+ }
+ lookups++;
+ if (unavailable) throw new Error("Synthetic lookup failure");
+ return Response.json({
+ finding: document.findings[0],
+ potentialDuplicates: [],
+ });
+ };
+ const receipt = await publishScanToCustomInternal(scanDir, options, {
+ environment,
+ fetch: request,
+ });
+ await expect(
+ deduplicateScanInternal(document.scanId, options, {
+ environment,
+ fetch: request,
+ runWorkbench: history,
+ }),
+ ).rejects.toThrow("Synthetic lookup failure");
+ const workflow = new FindingWorkflow(workflowId, environment);
+ expect((await workflow.get())?.stages).toMatchObject({
+ scan: { status: "completed" },
+ publish: { status: "completed", result: receipt },
+ dedupe: { status: "failed", error: "Synthetic lookup failure" },
+ });
+ expect(publications).toBe(1);
+ unavailable = false;
+ const result = await deduplicateScanInternal(document.scanId, options, {
+ environment,
+ fetch: request,
+ runWorkbench: history,
+ });
+ expect(result).toEqual({
+ scanId: document.scanId,
+ uniqueFindingIds: document.findings.map((finding) => finding.findingId),
+ duplicateGroups: [],
+ deduplicationStatus: "completed",
+ });
+ expect((await workflow.get())?.stages.dedupe).toEqual({
+ status: "completed",
+ result,
+ });
+ expect(
+ await deduplicateScanInternal(document.scanId, options, {
+ environment,
+ runWorkbench: history,
+ fetch: async () => {
+ throw new Error("Completed stages must not make HTTP requests");
+ },
+ }),
+ ).toEqual(result);
+ expect(publications).toBe(1);
+ expect(lookups).toBe(2);
+ expect(await readFile(join(scanDir, "findings.json"), "utf8")).toBe(original);
+});
+
+test("an empty scan completes publication and dedupe and remains retrievable", async () => {
+ const { scanDir, environment, document, history } = await fixture();
+ document.findings = [];
+ const content = JSON.stringify(document);
+ await writeFile(join(scanDir, "findings.json"), content);
+ const manifestPath = join(scanDir, "scan-manifest.json");
+ const manifest = JSON.parse(
+ await readFile(manifestPath, "utf8"),
+ ) as ScanManifest;
+ manifest.scan.artifacts!.find(
+ (artifact) => artifact.path === "findings.json",
+ )!.sha256 = createHash("sha256").update(content).digest("hex");
+ await writeFile(manifestPath, JSON.stringify(manifest));
+ const options = {
+ workflowId: "empty-scan",
+ findingsUrl: "http://synthetic.test",
+ };
+ let requests = 0;
+ const fetch = async (_url: URL, init: RequestInit) => {
+ requests++;
+ expect(JSON.parse(init.body as string).findings).toEqual([]);
+ return Response.json([]);
+ };
+ const result = await deduplicateScanInternal(document.scanId, options, {
+ environment,
+ runWorkbench: history,
+ fetch,
+ });
+ expect(result).toEqual({
+ scanId: document.scanId,
+ uniqueFindingIds: [],
+ duplicateGroups: [],
+ deduplicationStatus: "completed",
+ });
+ expect(
+ await deduplicateScanInternal(document.scanId, options, {
+ environment,
+ runWorkbench: history,
+ fetch,
+ }),
+ ).toEqual(result);
+ expect(requests).toBe(1);
+ expect(
+ (await new FindingWorkflow(options.workflowId, environment).get())?.stages
+ .publish,
+ ).toMatchObject({
+ status: "completed",
+ result: { findingIds: [], findingCount: 0 },
+ });
+});
+
+test("does not write workflow metadata into sealed artifacts", async () => {
+ const { scanDir, environment } = await fixture();
+ await expect(
+ publishScanToCustomInternal(
+ scanDir,
+ { workflowId: "unsafe-location", findingsUrl: "http://synthetic.test" },
+ {
+ environment: {
+ ...environment,
+ CODEX_SECURITY_STATE_DIR: join(scanDir, "state"),
+ },
+ fetch: async () => {
+ throw new Error("Must not publish");
+ },
+ },
+ ),
+ ).rejects.toThrow("outside the sealed scan artifacts");
+});
+
+function merged(findings: readonly Finding[]) {
+ return {
+ decision: "SAME" as const,
+ rationale: "REVIEW_OUTPUT_ONLY: one correction covers the supplied paths.",
+ canonicalFindingId: findings[0]!.findingId,
+ mergedFinding: {
+ ...findings[0]!,
+ title: "MERGED_OUTPUT_ONLY",
+ extensions: { originals: findings },
+ },
+ };
+}
+const distinct: DuplicateDecision = {
+ decision: "DISTINCT",
+ rationale: "REVIEW_OUTPUT_ONLY: independent corrections are required.",
+};
+
+test.each(["screen", "pair"])(
+ "resumes unfinished %s reviews using validated checkpoints and original inputs",
+ async (interruptAt) => {
+ const { scanDir, environment, document, history } = await fixture();
+ const findings = [
+ document.findings[0]!,
+ ...[1, 2, 3].map((index) => ({
+ ...structuredClone(document.findings[0]!),
+ findingId: `csf_${"f".repeat(23)}${index}`,
+ title: `Synthetic original ${index}`,
+ })),
+ ];
+ const options = {
+ workflowId: `reviews-${interruptAt}`,
+ findingsUrl: "http://synthetic.test",
+ };
+ const calls: string[] = [];
+ let interrupted = false;
+ const reviewRunner = {
+ async run<T>(review: CodexReview<T>): Promise<T> {
+ expect(review.prompt).not.toContain("REVIEW_OUTPUT_ONLY");
+ expect(review.prompt).not.toContain("MERGED_OUTPUT_ONLY");
+ const originals = JSON.parse(
+ review.prompt.slice(review.prompt.lastIndexOf("\n\n") + 2),
+ ).findings as Finding[];
+ for (const finding of originals)
+ expect(finding).toEqual(
+ findings.find(
+ (original) => original.findingId === finding.findingId,
+ )!,
+ );
+ const stage = review.model === "gpt-5.6-luna" ? "screen" : "pair";
+ calls.push(stage);
+ if (
+ !interrupted &&
+ stage === interruptAt &&
+ (stage !== "pair" ||
+ calls.filter((call) => call === "pair").length === 2)
+ ) {
+ interrupted = true;
+ throw new Error("Synthetic interrupted review");
+ }
+ return review.validate(
+ stage === "screen"
+ ? {
+ decisions: originals.slice(1).map((finding) => ({
+ findingIds: [originals[0]!.findingId, finding.findingId],
+ ...merged([originals[0]!, finding]),
+ })),
+ }
+ : originals.some(
+ (finding) => finding.findingId === findings[3]!.findingId,
+ )
+ ? distinct
+ : merged(originals),
+ );
+ },
+ };
+ const fetch = async (url: URL) =>
+ url.pathname.endsWith("/bulk/findings")
+ ? Response.json([findings[0]!.findingId])
+ : url.pathname.endsWith("/dedupe-groups")
+ ? Response.json([])
+ : Response.json({
+ finding: findings[0],
+ potentialDuplicates: findings.slice(1),
+ });
+ await publishScanToCustomInternal(scanDir, options, { environment, fetch });
+ await expect(
+ deduplicateScanInternal(document.scanId, options, {
+ environment,
+ runWorkbench: history,
+ reviewRunner,
+ fetch,
+ }),
+ ).rejects.toThrow("Synthetic interrupted review");
+ const result = await deduplicateScanInternal(document.scanId, options, {
+ environment,
+ runWorkbench: history,
+ reviewRunner,
+ fetch,
+ });
+ expect(result.duplicateGroups).toEqual([
+ findings.slice(0, 3).map((finding) => finding.findingId),
+ ]);
+ expect(calls.filter((stage) => stage === "screen")).toHaveLength(
+ interruptAt === "screen" ? 2 : 1,
+ );
+ expect(calls.filter((stage) => stage === "pair")).toHaveLength(
+ interruptAt === "pair" ? 4 : 3,
+ );
+ const count = calls.length;
+ expect(
+ await deduplicateScanInternal(document.scanId, options, {
+ environment,
+ runWorkbench: history,
+ reviewRunner,
+ fetch: async () => {
+ throw new Error("Completed workflow must use its saved result");
+ },
+ }),
+ ).toEqual(result);
+ expect(calls).toHaveLength(count);
+ },
+);
+
+test("replays an unacknowledged group write after migrating its workflow database", async () => {
+ const { environment, document, history } = await fixture();
+ const originals = [
+ document.findings[0]!,
+ { ...document.findings[0]!, findingId: `csf_${"f".repeat(24)}` },
+ ];
+ const options = {
+ workflowId: "migrated-lost-ack",
+ findingsUrl: "http://synthetic.test",
+ };
+ const bodies: string[] = [];
+ const checkpoints: object[] = [];
+ let modelCalls = 0;
+ const reviewRunner = {
+ async run<T>(review: CodexReview<T>): Promise<T> {
+ modelCalls++;
+ return review.validate(
+ review.model === "gpt-5.6-luna"
+ ? {
+ decisions: [
+ {
+ findingIds: originals.map((finding) => finding.findingId),
+ ...merged(originals),
+ },
+ ],
+ }
+ : merged(originals),
+ );
+ },
+ };
+ const fetch = async (url: URL, init: RequestInit) => {
+ if (url.pathname.endsWith("/bulk/findings"))
+ return Response.json([originals[0]!.findingId]);
+ if (!url.pathname.endsWith("/dedupe-groups"))
+ return Response.json({
+ finding: originals[0],
+ potentialDuplicates: [originals[1]],
+ });
+ const saved = (await new FindingWorkflow(
+ options.workflowId,
+ environment,
+ ).get())!.stages.dedupe;
+ expect(saved.status).toBe("running");
+ expect(saved.result).toMatchObject({
+ duplicateGroups: [originals.map((finding) => finding.findingId)],
+ });
+ expect(saved.pendingWrite).toEqual(JSON.parse(init.body as string));
+ bodies.push(init.body as string);
+ return bodies.length === 1
+ ? new Response("incomplete acknowledgement", { status: 201 })
+ : Response.json([]);
+ };
+ await expect(
+ deduplicateScanInternal(document.scanId, options, {
+ environment,
+ runWorkbench: async (args, input) => {
+ const response = await history(args, input);
+ const payload = input ? JSON.parse(input) : {};
+ if (payload.action === "save-review") checkpoints.push(payload);
+ return response;
+ },
+ reviewRunner,
+ fetch,
+ }),
+ ).rejects.toThrow();
+ const workflow = new FindingWorkflow(options.workflowId, environment);
+ await restoreLegacyWorkflow(
+ environment,
+ (await workflow.get())!,
+ checkpoints,
+ );
+ expect((await workflow.get())?.stages.dedupe.status).toBe("failed");
+ const result = await deduplicateScanInternal(document.scanId, options, {
+ environment,
+ runWorkbench: history,
+ reviewRunner,
+ fetch,
+ });
+ expect(bodies).toHaveLength(2);
+ expect(new Set(bodies).size).toBe(1);
+ expect(modelCalls).toBe(2);
+ expect(
+ (await new FindingWorkflow(options.workflowId, environment).get())?.stages
+ .dedupe,
+ ).toEqual({ status: "completed", result });
+});
+
+test.each(["current", "legacy", "workflow-columns"])(
+ "persists DISTINCT and complete SAME checkpoints across %s databases",
+ async (version) => {
+ const { environment, repository, document, workbenchOptions } =
+ await fixture();
+ const workflow = new FindingWorkflow("all-decisions", environment);
+ await workflow.bind({});
+ if (version === "workflow-columns") {
+ const probe = await runCodexCommand(
+ { command: workbenchOptions.python },
+ [
+ "-I",
+ "-B",
+ "-c",
+ `import sqlite3, sys
+with sqlite3.connect(sys.argv[1]) as db:
+ db.execute("DROP TABLE finding_workflow_reviews")
+ db.execute("DELETE FROM schema_migrations WHERE version IN (37, 39)")`,
+ join(environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
+ ],
+ environment,
+ );
+ expect(probe.exitCode, probe.stderr).toBe(0);
+ }
+ const originals = [
+ document.findings[0]!,
+ { ...document.findings[0]!, findingId: `csf_${"f".repeat(24)}` },
+ ];
+ let calls = 0;
+ const checkpoints: Array<{
+ key: string;
+ binding: JsonObject;
+ result: unknown;
+ }> = [];
+ const recordCheckpoint: typeof runWorkbench = async (
+ options,
+ args,
+ input,
+ ) => {
+ const result = await runWorkbench(options, args, input);
+ const payload = input ? JSON.parse(input) : {};
+ if (payload.action === "save-review") checkpoints.push(payload);
+ return result;
+ };
+ const runner = {
+ async run<T>(review: CodexReview<T>): Promise<T> {
+ calls++;
+ return review.validate(
+ review.model === "gpt-5.6-luna"
+ ? {
+ decisions: [
+ {
+ findingIds: originals.map((finding) => finding.findingId),
+ ...distinct,
+ },
+ ],
+ }
+ : merged(originals),
+ );
+ },
+ };
+ const makeReviewer = async () =>
+ new CodexDeduplicationReviewer(
+ new CheckpointedReviewRunner(
+ new FindingWorkflow(workflow.id, environment, recordCheckpoint),
+ runner,
+ await workflow.sourceSnapshot(repository),
+ { allRepositories: true },
+ "synthetic-settings-hash",
+ ),
+ );
+ const first = await makeReviewer();
+ const screening = await first.screen(originals);
+ const pair = await first.reviewPair(originals);
+ if (version === "legacy")
+ await restoreLegacyWorkflow(
+ environment,
+ (await workflow.get())!,
+ checkpoints,
+ );
+ const resumed = await makeReviewer();
+ expect(await resumed.screen(originals)).toEqual(screening);
+ expect(await resumed.reviewPair(originals)).toEqual(pair);
+ expect(pair).toEqual(merged(originals));
+ expect(calls).toBe(2);
+ expect(checkpoints).toHaveLength(2);
+ const probe = await runCodexCommand(
+ { command: workbenchOptions.python },
+ [
+ "-I",
+ "-B",
+ "-c",
+ `import json, sqlite3, sys
+db = sqlite3.connect(sys.argv[1])
+db.row_factory = sqlite3.Row
+assert "binding_json" not in {row["name"] for row in db.execute("PRAGMA table_info(finding_workflow_reviews)")}
+assert list(db.execute("PRAGMA foreign_key_check")) == []
+assert db.execute("PRAGMA integrity_check").fetchone()[0] == "ok"
+print(json.dumps([dict(row) for row in db.execute("SELECT * FROM finding_workflow_reviews ORDER BY review_key")]))`,
+ join(environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
+ ],
+ environment,
+ );
+ expect(probe.exitCode, probe.stderr).toBe(0);
+ const rows = JSON.parse(probe.stdout);
+ for (const { key, binding, result } of checkpoints) {
+ const source = binding["source"] as JsonObject;
+ const row = rows.find(
+ (row: { review_key: string }) => row.review_key === key,
+ );
+ expect(row).toMatchObject({
+ workflow_id: workflow.id,
+ review_contract_version: binding["version"],
+ codex_version: binding["codexVersion"],
+ source_repository_path: source["repository"],
+ source_revision: source["revision"],
+ source_refs_digest: source["refsDigest"],
+ source_content_digest: source["content"],
+ scope_repository_id: null,
+ scope_all_repositories: 1,
+ model: binding["model"],
+ effort: binding["effort"],
+ settings_digest: binding["settingsDigest"],
+ prompt_digest: binding["promptDigest"],
+ contract_digest: binding["contractDigest"],
+ });
+ if (version === "legacy")
+ expect(row.created_at).toBe("2026-08-01T00:00:00Z");
+ expect(JSON.parse(row.result_json)).toEqual(result);
+ }
+ },
+);
+
+test("source snapshots include revisions and ignored content without following directory links", async () => {
+ const { environment, repository, root } = await fixture();
+ const git = (...args: string[]) =>
+ execFileSync("git", args, { cwd: repository, stdio: "pipe" });
+ git("init", "--quiet");
+ await writeFile(join(repository, ".gitignore"), "ignored.txt\n");
+ await writeFile(join(repository, "tracked.txt"), "original");
+ await mkdir(join(repository, "tracked-directory"));
+ await writeFile(
+ join(repository, "tracked-directory", "source.txt"),
+ "tracked source",
+ );
+ git("add", ".");
+ git(
+ "-c",
+ "user.name=Example",
+ "-c",
+ "commit",
+ "--quiet",
+ "-m",
+ "Synthetic source",
+ );
+ const workflow = new FindingWorkflow("source-snapshots", environment);
+ const first = await workflow.sourceSnapshot(repository);
+ await writeFile(join(repository, "ignored.txt"), "ignored source");
+ const second = await workflow.sourceSnapshot(repository);
+ expect(second["revision"]).toBe(first["revision"]);
+ expect(second["content"]).not.toBe(first["content"]);
+ git(
+ "-c",
+ "user.name=Example",
+ "-c",
+ "commit",
+ "--quiet",
+ "--allow-empty",
+ "-m",
+ "Next synthetic revision",
+ );
+ expect((await workflow.sourceSnapshot(repository))["revision"]).not.toBe(
+ first["revision"],
+ );
+ const beforeRef = await workflow.sourceSnapshot(repository);
+ git("branch", "synthetic-source-reference");
+ const afterRef = await workflow.sourceSnapshot(repository);
+ expect(afterRef["revision"]).toBe(beforeRef["revision"]);
+ expect(afterRef["refsDigest"]).not.toBe(beforeRef["refsDigest"]);
+ const outside = join(root, "outside");
+ await mkdir(outside);
+ await writeFile(join(outside, "private.txt"), "synthetic outside content");
+ const { symlink } = await import("node:fs/promises");
+ await symlink(
+ outside,
+ join(repository, "linked"),
+ process.platform === "win32" ? "junction" : "dir",
+ );
+ const linked = await workflow.sourceSnapshot(repository);
+ await writeFile(join(outside, "private.txt"), "changed outside content");
+ expect(await workflow.sourceSnapshot(repository)).toEqual(linked);
+ await rm(join(repository, "tracked-directory"), { recursive: true });
+ await writeFile(join(outside, "source.txt"), "synthetic outside source");
+ await symlink(
+ outside,
+ join(repository, "tracked-directory"),
+ process.platform === "win32" ? "junction" : "dir",
+ );
+ const replaced = await workflow.sourceSnapshot(repository);
+ await writeFile(join(outside, "source.txt"), "changed outside source");
+ expect(await workflow.sourceSnapshot(repository)).toEqual(replaced);
+});
diff --git a/sdk/typescript/tests-ts/finding-workflow-publication.test.ts b/sdk/typescript/tests-ts/finding-workflow-publication.test.ts
new file mode 100644
index 000000000..163bc9c63
--- /dev/null
+++ b/sdk/typescript/tests-ts/finding-workflow-publication.test.ts
@@ -0,0 +1,294 @@
+import { readFile } from "node:fs/promises";
+import { join } from "node:path";
+import { expect, test } from "bun:test";
+import type { JsonObject } from "../src/config.js";
+import { publishScanToCustomInternal } from "../src/custom-publish.js";
+import {
+ deduplicateScanInternal,
+ type DeduplicateScanResult,
+} from "../src/deduplication/scan.js";
+import type { DeduplicationReviewer } from "../src/deduplication/deduplication-reviewer.js";
+import type { ScanManifest } from "../src/models.js";
+import { PLUGIN_ROOT } from "./plugin-root.js";
+import { scriptedWorkbench } from "./support/workbench-fakes.js";
+import { workflowFixture } from "./support/workflow-fixture.js";
+
+type Step = Parameters<typeof scriptedWorkbench>[0][number];
+
+function publicationBinding(
+ id: string,
+ scanId: string,
+ scanDir: string,
+): Step[] {
+ return [
+ {
+ request: {
+ id,
+ action: "bind",
+ binding: {
+ scanId,
+ scanDir,
+ artifactDigest: expect.stringMatching(/^[a-f0-9]{64}$/),
+ destination: "http://synthetic.test/",
+ },
+ },
+ },
+ { request: { id, action: "complete", stage: "scan", result: null } },
+ ];
+}
+
+test("failed publication records its error, dry-run does not advance it, and retry records the receipt", async () => {
+ await using fixture = await workflowFixture();
+ const { scanDir, environment, document } = fixture;
+ const manifest = JSON.parse(
+ await readFile(join(scanDir, "scan-manifest.json"), "utf8"),
+ ) as ScanManifest;
+ const id = "publication-retry";
+ const options = { workflowId: id, findingsUrl: "http://synthetic.test" };
+ const result = {
+ scanId: document.scanId,
+ repositoryId: manifest.scan.target.targetId,
+ findingIds: document.findings.map((finding) => finding.findingId),
+ findingCount: document.findings.length,
+ };
+ const begin: Step = {
+ request: { id, action: "begin", stage: "publish" },
+ response: { workflow: { stages: { publish: { status: "running" } } } },
+ };
+ const workbench = scriptedWorkbench([
+ ...publicationBinding(id, document.scanId, scanDir),
+ begin,
+ {
+ request: {
+ id,
+ action: "fail",
+ stage: "publish",
+ error: expect.stringContaining("HTTP 503"),
+ },
+ },
+ ...publicationBinding(id, document.scanId, scanDir),
+ begin,
+ { request: { id, action: "complete", stage: "publish", result } },
+ ]);
+ await expect(
+ publishScanToCustomInternal(scanDir, options, {
+ environment,
+ runWorkbench: workbench.run,
+ fetch: async () => new Response("", { status: 503 }),
+ }),
+ ).rejects.toThrow("HTTP 503");
+ const dryRunWorkbench = scriptedWorkbench([]);
+ expect(
+ await publishScanToCustomInternal(
+ scanDir,
+ { ...options, dryRun: true },
+ {
+ environment,
+ runWorkbench: dryRunWorkbench.run,
+ fetch: async () => {
+ throw new Error("Dry-run must not publish");
+ },
+ },
+ ),
+ ).toEqual({ ...result, dryRun: true, findings: document.findings });
+ dryRunWorkbench.assertDone();
+ expect(
+ await publishScanToCustomInternal(scanDir, options, {
+ environment,
+ runWorkbench: workbench.run,
+ fetch: async () => Response.json(result.findingIds),
+ }),
+ ).toEqual(result);
+ workbench.assertDone();
+});
+
+test.each(["before-post", "before-write", "lost-ack", "lost-completion"])(
+ "replays the exact saved group payload after %s without repeating reviews",
+ async (failure) => {
+ await using fixture = await workflowFixture();
+ const { scanDir, repository, environment, document } = fixture;
+ const originals = [
+ document.findings[0]!,
+ { ...document.findings[0]!, findingId: `csf_${"f".repeat(24)}` },
+ ];
+ const id = `write-${failure}`;
+ const options = { workflowId: id, findingsUrl: "http://synthetic.test" };
+ const groups = [originals.map((finding) => finding.findingId)];
+ const result: DeduplicateScanResult = {
+ scanId: document.scanId,
+ uniqueFindingIds: [originals[0]!.findingId],
+ duplicateGroups: groups,
+ deduplicationStatus: "completed",
+ };
+ const source = {
+ repository,
+ revision: "synthetic-revision",
+ refsDigest: "synthetic-refs",
+ content: "synthetic-content",
+ };
+ const prefix: Step[] = [
+ ...publicationBinding(id, document.scanId, scanDir),
+ ...publicationBinding(id, document.scanId, scanDir),
+ {
+ request: { id, action: "begin", stage: "publish" },
+ response: {
+ workflow: {
+ stages: {
+ publish: {
+ status: "completed",
+ result: { findingIds: [originals[0]!.findingId] },
+ },
+ },
+ },
+ },
+ },
+ {
+ request: { id, action: "begin", stage: "dedupe" },
+ response: { workflow: { stages: { dedupe: { status: "running" } } } },
+ },
+ ];
+ let prepared: JsonObject | undefined;
+ const complete: Step = {
+ request: { id, action: "complete", stage: "dedupe", result },
+ };
+ const workbench = scriptedWorkbench([
+ ...prefix,
+ {
+ request: { id, action: "get" },
+ response: { workflow: { stages: { dedupe: { status: "running" } } } },
+ },
+ { request: { id, action: "source", repository }, response: { source } },
+ { request: { id, action: "source", repository }, response: { source } },
+ {
+ request: {
+ id,
+ action: "prepare-dedupe",
+ stage: "dedupe",
+ result,
+ pendingWrite: { groups },
+ },
+ response(payload) {
+ prepared = payload;
+ if (failure === "before-post")
+ throw new Error("Synthetic stop before posting");
+ return {};
+ },
+ },
+ ...(failure === "lost-completion"
+ ? [
+ {
+ ...complete,
+ error: new Error("Synthetic completion receipt failure"),
+ },
+ ]
+ : []),
+ {
+ request: {
+ id,
+ action: "fail",
+ stage: "dedupe",
+ error: expect.any(String),
+ },
+ },
+ ...prefix,
+ {
+ request: { id, action: "get" },
+ response: {
+ workflow: {
+ stages: {
+ dedupe: {
+ status: "running",
+ result,
+ pendingWrite: { groups },
+ },
+ },
+ },
+ },
+ },
+ complete,
+ ]);
+ const history = async (args: readonly string[], input?: string) => {
+ if (args[0] === "get-scan") {
+ expect(args).toEqual(["get-scan", "--scan-id", document.scanId]);
+ return {
+ scan: {
+ scanId: document.scanId,
+ scanDir,
+ targetPath: repository,
+ progress: { status: "complete" },
+ },
+ };
+ }
+ return await workbench.run(
+ { environment, pluginRoot: PLUGIN_ROOT, python: "unused" },
+ args,
+ input,
+ );
+ };
+ let reviews = 0;
+ const decision = {
+ decision: "SAME" as const,
+ rationale: "One correction covers both findings.",
+ canonicalFindingId: originals[0]!.findingId,
+ mergedFinding: originals[0]!,
+ };
+ const reviewer: DeduplicationReviewer = {
+ async screen(findings) {
+ reviews++;
+ expect(findings).toEqual(originals);
+ return {
+ decisions: [
+ {
+ ...decision,
+ findingIds: [originals[0]!.findingId, originals[1]!.findingId],
+ },
+ ],
+ };
+ },
+ async reviewPair(findings) {
+ reviews++;
+ expect(findings).toEqual(originals);
+ return decision;
+ },
+ };
+ const bodies: string[] = [];
+ let lookups = 0;
+ const fetch = async (url: URL, init: RequestInit) => {
+ if (url.pathname.endsWith("/potential-duplicates")) {
+ lookups++;
+ return Response.json({
+ finding: originals[0],
+ potentialDuplicates: [originals[1]],
+ });
+ }
+ expect(url.pathname).toBe("/v1/dedupe-groups");
+ expect(prepared).toMatchObject({
+ result,
+ pendingWrite: JSON.parse(init.body as string),
+ });
+ bodies.push(init.body as string);
+ if (bodies.length === 1 && failure === "before-write")
+ return new Response("", { status: 503 });
+ if (bodies.length === 1 && failure === "lost-ack")
+ return new Response("incomplete acknowledgement", { status: 201 });
+ return Response.json([]);
+ };
+ const dependencies = {
+ environment,
+ runWorkbench: history,
+ reviewer,
+ fetch,
+ };
+ await expect(
+ deduplicateScanInternal(document.scanId, options, dependencies),
+ ).rejects.toThrow();
+ expect(
+ await deduplicateScanInternal(document.scanId, options, dependencies),
+ ).toEqual(result);
+ expect(bodies).toHaveLength(failure === "before-post" ? 1 : 2);
+ expect(new Set(bodies).size).toBe(1);
+ expect(reviews).toBe(2);
+ expect(lookups).toBe(1);
+ workbench.assertDone();
+ },
+);
diff --git a/sdk/typescript/tests-ts/finding-workflow.test.ts b/sdk/typescript/tests-ts/finding-workflow.test.ts
index 1ab5967f8..64f3b76f5 100644
--- a/sdk/typescript/tests-ts/finding-workflow.test.ts
+++ b/sdk/typescript/tests-ts/finding-workflow.test.ts
@@ -1,987 +1,27 @@
-import { execFileSync, spawn } from "node:child_process";
-import { createHash } from "node:crypto";
-import { once } from "node:events";
-import {
- chmod,
- cp,
- mkdir,
- mkdtemp,
- readFile,
- rm,
- writeFile,
-} from "node:fs/promises";
-import { tmpdir } from "node:os";
+import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
-import { afterEach, expect, test } from "bun:test";
-import type { JsonObject } from "../src/config.js";
+import { expect, test } from "bun:test";
import {
FindingWorkflow,
workflowDestination,
- type WorkflowState,
} from "../src/finding-workflow.js";
-import { publishScanToCustomInternal } from "../src/custom-publish.js";
-import { deduplicateScanInternal } from "../src/deduplication/scan.js";
-import {
- resolvePluginPython,
- runCodexCommand,
- runWorkbench,
-} from "../src/runtime.js";
-import type { Finding, FindingsDocument, ScanManifest } from "../src/models.js";
import type { CodexReview } from "../src/deduplication/codex-review.js";
import {
CheckpointedReviewRunner,
reviewSettingsDigest,
} from "../src/deduplication/checkpointed-review.js";
+import type { DuplicateDecision } from "../src/deduplication/deduplication-reviewer.js";
import {
- CodexDeduplicationReviewer,
- type DuplicateDecision,
-} from "../src/deduplication/deduplication-reviewer.js";
-import { PLUGIN_ROOT } from "./plugin-root.js";
-
-const directories: string[] = [];
-afterEach(async () => {
- await Promise.all(
- directories
- .splice(0)
- .map((directory) => rm(directory, { recursive: true, force: true })),
- );
-});
-
-async function fixture() {
- const root = await mkdtemp(join(tmpdir(), "findings-workflow-"));
- directories.push(root);
- const scanDir = join(root, "scan");
- const repository = join(root, "repository");
- await mkdir(repository);
- await cp(join(PLUGIN_ROOT, "examples/completed-scan"), scanDir, {
- recursive: true,
- });
- if (process.platform !== "win32") await chmod(scanDir, 0o700);
- const environment = {
- PATH: process.env["PATH"],
- SystemRoot: process.env["SystemRoot"],
- TEMP: process.env["TEMP"],
- TMP: process.env["TMP"],
- CODEX_HOME: join(root, "codex"),
- CODEX_SECURITY_STATE_DIR: join(root, "state"),
- };
- const document = JSON.parse(
- await readFile(join(scanDir, "findings.json"), "utf8"),
- ) as FindingsDocument;
- const workbenchOptions = {
- environment,
- pluginRoot: PLUGIN_ROOT,
- python: await resolvePluginPython({ environment }),
- };
- const history = async (args: readonly string[], input?: string) =>
- args[0] === "get-scan"
- ? {
- scan: {
- scanId: document.scanId,
- scanDir,
- targetPath: repository,
- progress: { status: "complete" },
- },
- }
- : await runWorkbench(workbenchOptions, args, input);
- return {
- root,
- repository,
- scanDir,
- environment,
- document,
- history,
- workbenchOptions,
- };
-}
-
-async function restoreLegacyWorkflow(
- environment: NodeJS.ProcessEnv,
- state: object,
- reviews: object[],
-) {
- const probe = await runCodexCommand(
- { command: await resolvePluginPython({ environment }) },
- [
- "-I",
- "-B",
- "-c",
- `import json, sqlite3, sys
-sys.path.insert(0, sys.argv[1])
-from workbench_schema import MIGRATIONS, apply_migrations, sql_statements
-db = sqlite3.connect(sys.argv[2])
-db.row_factory = sqlite3.Row
-db.execute("PRAGMA foreign_keys = ON")
-payload = json.load(sys.stdin)
-state = payload["state"]
-timestamp = "2026-08-01T00:00:00Z"
-with db:
- db.execute("DROP TABLE finding_workflow_reviews")
- db.execute("DROP TABLE finding_workflows")
- db.execute("DELETE FROM schema_migrations WHERE version >= 38")
- for version, _, sql in MIGRATIONS:
- if version in (36, 37):
- for statement in sql_statements(sql):
- db.execute(statement)
- db.execute("INSERT INTO finding_workflows VALUES (?, ?, ?, ?)",
- (state["id"], json.dumps(state), timestamp, timestamp))
- for review in payload["reviews"]:
- db.execute("INSERT INTO finding_workflow_reviews VALUES (?, ?, ?, ?, ?)",
- (state["id"], review["key"], json.dumps(review["binding"]), json.dumps(review["result"]), timestamp))
-before = list(db.iterdump())
-try:
- apply_migrations(db, (*MIGRATIONS, (999, "synthetic failure", "INSERT INTO synthetic_missing_table VALUES (1);")), lambda: timestamp, lambda _: None)
-except sqlite3.OperationalError:
- pass
-else:
- raise AssertionError("Migration should fail")
-assert not db.in_transaction
-assert list(db.iterdump()) == before, "Failed migration must preserve workflow and review rows together"
-db.close()`,
- join(PLUGIN_ROOT, "scripts"),
- join(environment["CODEX_SECURITY_STATE_DIR"]!, "workbench.sqlite3"),
- ],
- environment,
- JSON.stringify({ state, reviews }),
- );
- expect(probe.exitCode, probe.stderr).toBe(0);
-}
-
-test("migrates workflow columns atomically without losing receipts, pending writes, or resume state", async () => {
- const { environment, repository, scanDir, workbenchOptions } =
- await fixture();
- const completed = {
- id: "migrated-completed",
- repositoryPath: repository,
- scanRequestDigest: "scan-request-hash",
- scanId: "synthetic-scan",
- scanDir,
- artifactDigest: "artifact-hash",
- destination: "http://synthetic.test/",
- scope: { repositoryId: "synthetic-repository" },
- stages: {
- scan: { status: "completed", result: null },
- publish: { status: "completed", result: { findingIds: [] } },
- dedupe: { status: "completed", result: { duplicateGroups: [] } },
- },
- } as const;
- const unfinished = {
- id: "migrated-unfinished",
- scope: { allRepositories: true },
- stages: {
- scan: { status: "failed", error: "Synthetic scan interruption" },
- publish: { status: "running", error: "Synthetic earlier failure" },
- dedupe: {
- status: "failed",
- error: "Synthetic lost acknowledgement",
- result: { duplicateGroups: [["a", "b"]] },
- pendingWrite: { groups: [["a", "b"]] },
- },
- },
- } satisfies WorkflowState;
- const pending = {
- id: "migrated-pending",
- stages: {
- scan: { status: "pending" },
- publish: { status: "pending" },
- dedupe: { status: "pending" },
- },
- } as const;
- await mkdir(environment.CODEX_SECURITY_STATE_DIR);
- const probe = await runCodexCommand(
- { command: workbenchOptions.python },
- [
- "-I",
- "-B",
- "-c",
- `import json, sqlite3, sys
-sys.path.insert(0, sys.argv[1])
-from workbench_schema import MIGRATIONS, apply_migrations
-db = sqlite3.connect(sys.argv[2])
-db.row_factory = sqlite3.Row
-db.execute("PRAGMA foreign_keys = ON")
-timestamp = "2026-08-01T00:00:00Z"
-apply = lambda migrations: apply_migrations(db, migrations, lambda: timestamp, lambda _: None)
-apply(tuple(m for m in MIGRATIONS if m[0] <= 36))
-states = json.load(sys.stdin)
-with db:
- for state in states:
- db.execute("INSERT INTO finding_workflows VALUES (?, ?, ?, ?)",
- (state["id"], json.dumps(state), timestamp, "2026-08-02T00:00:00Z"))
- db.execute("CREATE TABLE synthetic_workflow_references (workflow_id TEXT REFERENCES finding_workflows(id) ON DELETE CASCADE)")
- db.execute("INSERT INTO synthetic_workflow_references VALUES (?)", (states[0]["id"],))
-before = list(db.iterdump())
-try:
- apply((*MIGRATIONS, (999, "synthetic failure", "INSERT INTO synthetic_missing_table VALUES (1);")))
-except sqlite3.OperationalError:
- pass
-else:
- raise AssertionError("Migration should fail")
-assert not db.in_transaction
-assert list(db.iterdump()) == before, "Migration must roll back schema and data together"
-apply(MIGRATIONS)
-after = list(db.iterdump())
-apply(MIGRATIONS)
-assert list(db.iterdump()) == after
-assert "state_json" not in {row["name"] for row in db.execute("PRAGMA table_info(finding_workflows)")}
-assert db.execute("SELECT COUNT(*) FROM synthetic_workflow_references").fetchone()[0] == 1
-assert list(db.execute("PRAGMA foreign_key_check")) == []
-assert db.execute("PRAGMA integrity_check").fetchone()[0] == "ok"
-print(json.dumps([dict(row) for row in db.execute("SELECT * FROM finding_workflows ORDER BY id")]))`,
- join(PLUGIN_ROOT, "scripts"),
- join(environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
- ],
- environment,
- JSON.stringify([completed, unfinished, pending]),
- );
- expect(probe.exitCode, probe.stderr).toBe(0);
- const rows = JSON.parse(probe.stdout);
- expect(rows[0]).toMatchObject({
- id: completed.id,
- repository_path: repository,
- scan_request_digest: completed.scanRequestDigest,
- scan_id: completed.scanId,
- scan_dir: scanDir,
- artifact_digest: completed.artifactDigest,
- destination: completed.destination,
- scope_repository_id: completed.scope.repositoryId,
- scope_all_repositories: null,
- scan_status: "completed",
- scan_error: null,
- publish_status: "completed",
- publish_error: null,
- dedupe_status: "completed",
- dedupe_error: null,
- created_at: "2026-08-01T00:00:00Z",
- updated_at: "2026-08-02T00:00:00Z",
- });
- expect(JSON.parse(rows[0].results_json)).toEqual({
- scan: null,
- publish: completed.stages.publish.result,
- dedupe: completed.stages.dedupe.result,
- });
- for (const state of [completed, unfinished, pending]) {
- expect(await new FindingWorkflow(state.id, environment).get()).toEqual(
- state,
- );
- }
- const workflow = new FindingWorkflow(completed.id, environment);
- for (const stage of ["scan", "publish", "dedupe"] as const) {
- expect(
- await workflow.run<unknown>(stage, async () => {
- throw new Error("Completed work must not run again after migration");
- }),
- ).toEqual(completed.stages[stage].result);
- }
- const resumed = new FindingWorkflow(unfinished.id, environment);
- await resumed.run("scan", async () => ({ scanId: "resumed-scan" }));
- expect((await resumed.get())?.stages.scan).toEqual({
- status: "completed",
- result: { scanId: "resumed-scan" },
- });
- expect((await resumed.get())?.stages.dedupe).toEqual(
- unfinished.stages.dedupe,
- );
-});
-
-test("scan registration commits its workflow identity atomically and rolls back failed registration", async () => {
- const { root, repository, environment, workbenchOptions } = await fixture();
- const workflow = new FindingWorkflow("registered-workflow", environment);
- await workflow.bind({ repositoryPath: repository });
- await workflow.begin("scan");
- const register = async (workflowId: string, suffix: string) => {
- const directory = join(root, suffix);
- await mkdir(directory, { mode: 0o700 });
- return await runWorkbench(
- workbenchOptions,
- [
- "register-cli-scan",
- "--repository",
- repository,
- "--scan-dir",
- directory,
- "--registration-json-stdin",
- ],
- JSON.stringify({
- workflowId,
- recipe: {
- repository,
- mode: "standard",
- target: { kind: "repository", paths: [] },
- config: {},
- },
- }),
- );
- };
- const registered = await register("registered-workflow", "registered-scan");
- expect(
- await new FindingWorkflow("registered-workflow", environment).get(),
- ).toMatchObject({
- scanId: registered["scanId"],
- scanDir: registered["scanDir"],
- stages: { scan: { status: "running" } },
- });
- await expect(
- register("unknown-workflow", "rolled-back-scan"),
- ).rejects.toThrow("must be started");
- const history = await runWorkbench(workbenchOptions, [
- "list-scans",
- "--repository",
- repository,
- ]);
- expect(history["scans"]).toHaveLength(1);
- const retried = await register("registered-workflow", "retried-scan");
- expect(retried["scanId"]).not.toBe(registered["scanId"]);
- expect((await workflow.get())?.scanId).toBe(retried["scanId"] as string);
-});
-
-test.each(["running", "completed"])(
- "survives termination with a %s stage and resumes in a fresh process",
- async (status) => {
- const { environment } = await fixture();
- const source = `import { FindingWorkflow } from ${JSON.stringify(new URL("../src/finding-workflow.ts", import.meta.url).href)};
-const workflow = new FindingWorkflow("interrupted", process.env);
-const timer = setInterval(() => {}, 1000);
-await workflow.run("publish", async () => {
- if (${JSON.stringify(status)} === "running") {
- process.stdout.write("ready\\n");
- await new Promise(() => {});
- }
- return { findingIds: [] };
-});
-process.stdout.write("ready\\n");`;
- const child = spawn(process.execPath, ["--eval", source], {
- env: environment,
- stdio: ["ignore", "pipe", "pipe"],
- windowsHide: true,
- });
- const closed = once(child, "close");
- let errors = "";
- child.stderr.on("data", (chunk) => {
- errors += String(chunk);
- });
- try {
- const ready = await Promise.race([
- once(child.stdout, "data").then(([data]) => String(data)),
- closed.then(() => {
- throw new Error(
- errors || "Workflow child exited before the checkpoint",
- );
- }),
- ]);
- expect(ready).toContain("ready");
- child.kill();
- await closed;
- const workflow = new FindingWorkflow("interrupted", environment);
- expect((await workflow.get())?.stages.publish.status).toBe(status);
- let attempts = 0;
- expect(
- await workflow.run("publish", async () => {
- attempts++;
- return { findingIds: [] };
- }),
- ).toEqual({ findingIds: [] });
- expect(attempts).toBe(status === "running" ? 1 : 0);
- expect(
- (await new FindingWorkflow("interrupted", environment).get())?.stages
- .publish,
- ).toEqual({ status: "completed", result: { findingIds: [] } });
- } finally {
- if (child.exitCode === null && child.signalCode === null) child.kill();
- await closed;
- }
- },
-);
-
-test("reuses publication after dedupe failure and persists a successful empty duplicate result", async () => {
- const { scanDir, environment, document, history } = await fixture();
- const original = await readFile(join(scanDir, "findings.json"), "utf8");
- const workflowId = "resume-example";
- const options = { workflowId, findingsUrl: "http://synthetic.test/service" };
- let publications = 0;
- let lookups = 0;
- let unavailable = true;
- const request = async (url: URL) => {
- if (url.pathname.endsWith("/bulk/findings")) {
- publications++;
- return Response.json(
- document.findings.map((finding) => finding.findingId),
- );
- }
- lookups++;
- if (unavailable) throw new Error("Synthetic lookup failure");
- return Response.json({
- finding: document.findings[0],
- potentialDuplicates: [],
- });
- };
- const receipt = await publishScanToCustomInternal(scanDir, options, {
- environment,
- fetch: request,
- });
- await expect(
- deduplicateScanInternal(document.scanId, options, {
- environment,
- fetch: request,
- runWorkbench: history,
- }),
- ).rejects.toThrow("Synthetic lookup failure");
- const workflow = new FindingWorkflow(workflowId, environment);
- expect((await workflow.get())?.stages).toMatchObject({
- scan: { status: "completed" },
- publish: { status: "completed", result: receipt },
- dedupe: { status: "failed", error: "Synthetic lookup failure" },
- });
- expect(publications).toBe(1);
- unavailable = false;
- const result = await deduplicateScanInternal(document.scanId, options, {
- environment,
- fetch: request,
- runWorkbench: history,
- });
- expect(result).toEqual({
- scanId: document.scanId,
- uniqueFindingIds: document.findings.map((finding) => finding.findingId),
- duplicateGroups: [],
- deduplicationStatus: "completed",
- });
- expect((await workflow.get())?.stages.dedupe).toEqual({
- status: "completed",
- result,
- });
- expect(
- await deduplicateScanInternal(document.scanId, options, {
- environment,
- runWorkbench: history,
- fetch: async () => {
- throw new Error("Completed stages must not make HTTP requests");
- },
- }),
- ).toEqual(result);
- expect(publications).toBe(1);
- expect(lookups).toBe(2);
- expect(await readFile(join(scanDir, "findings.json"), "utf8")).toBe(original);
-});
-
-test("an empty scan completes publication and dedupe and remains retrievable", async () => {
- const { scanDir, environment, document, history } = await fixture();
- document.findings = [];
- const content = JSON.stringify(document);
- await writeFile(join(scanDir, "findings.json"), content);
- const manifestPath = join(scanDir, "scan-manifest.json");
- const manifest = JSON.parse(
- await readFile(manifestPath, "utf8"),
- ) as ScanManifest;
- manifest.scan.artifacts!.find(
- (artifact) => artifact.path === "findings.json",
- )!.sha256 = createHash("sha256").update(content).digest("hex");
- await writeFile(manifestPath, JSON.stringify(manifest));
- const options = {
- workflowId: "empty-scan",
- findingsUrl: "http://synthetic.test",
- };
- let requests = 0;
- const fetch = async (_url: URL, init: RequestInit) => {
- requests++;
- expect(JSON.parse(init.body as string).findings).toEqual([]);
- return Response.json([]);
- };
- const result = await deduplicateScanInternal(document.scanId, options, {
- environment,
- runWorkbench: history,
- fetch,
- });
- expect(result).toEqual({
- scanId: document.scanId,
- uniqueFindingIds: [],
- duplicateGroups: [],
- deduplicationStatus: "completed",
- });
- expect(
- await deduplicateScanInternal(document.scanId, options, {
- environment,
- runWorkbench: history,
- fetch,
- }),
- ).toEqual(result);
- expect(requests).toBe(1);
- expect(
- (await new FindingWorkflow(options.workflowId, environment).get())?.stages
- .publish,
- ).toMatchObject({
- status: "completed",
- result: { findingIds: [], findingCount: 0 },
- });
-});
-
-test("failed publication stays unfinished, dry-run does not advance it, and retry records the receipt", async () => {
- const { scanDir, environment, document } = await fixture();
- const options = {
- workflowId: "publication-retry",
- findingsUrl: "http://synthetic.test",
- };
- await expect(
- publishScanToCustomInternal(scanDir, options, {
- environment,
- fetch: async () => new Response("", { status: 503 }),
- }),
- ).rejects.toThrow("HTTP 503");
- const workflow = new FindingWorkflow(options.workflowId, environment);
- const failed = await workflow.get();
- expect(failed?.stages.publish.status).toBe("failed");
- expect(failed?.stages.publish.result).toBeUndefined();
- await publishScanToCustomInternal(
- scanDir,
- { ...options, dryRun: true },
- { environment },
- );
- expect(await workflow.get()).toEqual(failed);
- const result = await publishScanToCustomInternal(scanDir, options, {
- environment,
- fetch: async () =>
- Response.json(document.findings.map((finding) => finding.findingId)),
- });
- expect((await workflow.get())?.stages.publish).toEqual({
- status: "completed",
- result,
- });
-});
+ checkpointWorkbench,
+ scriptedWorkbench,
+} from "./support/workbench-fakes.js";
+import { workflowFixture } from "./support/workflow-fixture.js";
-test("separates workflow identities and rejects different scans, destinations, and scopes", async () => {
- const { environment } = await fixture();
- const binding = {
- scanId: "scan-a",
- scanDir: "synthetic-artifacts",
- destination: workflowDestination(
- "http://synthetic:[email protected]",
- ),
- scope: { repositoryId: "repository-a" },
- };
- const first = new FindingWorkflow("first", environment);
- await first.bind(binding);
- expect((await first.get())?.destination).toBe("http://synthetic.test/");
- await first.run("dedupe", async () => ({ duplicateGroups: [] }));
- for (const changed of [
- { scanId: "scan-b" },
- { destination: "http://other.test/" },
- { scope: { allRepositories: true as const } },
- ]) {
- await expect(first.bind({ ...binding, ...changed })).rejects.toThrow(
- "already bound to a different",
- );
- const separate = new FindingWorkflow(
- `separate-${Object.keys(changed)[0]}`,
- environment,
- );
- expect(
- (await separate.bind({ ...binding, ...changed })).stages.dedupe.status,
- ).toBe("pending");
- }
- expect((await first.get())?.stages.dedupe).toEqual({
- status: "completed",
- result: { duplicateGroups: [] },
- });
-});
-
-test("does not write workflow metadata into sealed artifacts", async () => {
- const { scanDir, environment } = await fixture();
- await expect(
- publishScanToCustomInternal(
- scanDir,
- { workflowId: "unsafe-location", findingsUrl: "http://synthetic.test" },
- {
- environment: {
- ...environment,
- CODEX_SECURITY_STATE_DIR: join(scanDir, "state"),
- },
- fetch: async () => {
- throw new Error("Must not publish");
- },
- },
- ),
- ).rejects.toThrow("outside the sealed scan artifacts");
-});
-
-function merged(findings: readonly Finding[]) {
- return {
- decision: "SAME" as const,
- rationale: "REVIEW_OUTPUT_ONLY: one correction covers the supplied paths.",
- canonicalFindingId: findings[0]!.findingId,
- mergedFinding: {
- ...findings[0]!,
- title: "MERGED_OUTPUT_ONLY",
- extensions: { originals: findings },
- },
- };
-}
const distinct: DuplicateDecision = {
decision: "DISTINCT",
- rationale: "REVIEW_OUTPUT_ONLY: independent corrections are required.",
+ rationale: "Independent corrections are required.",
};
-test.each(["screen", "pair"])(
- "resumes unfinished %s reviews using validated checkpoints and original inputs",
- async (interruptAt) => {
- const { scanDir, environment, document, history } = await fixture();
- const findings = [
- document.findings[0]!,
- ...[1, 2, 3].map((index) => ({
- ...structuredClone(document.findings[0]!),
- findingId: `csf_${"f".repeat(23)}${index}`,
- title: `Synthetic original ${index}`,
- })),
- ];
- const options = {
- workflowId: `reviews-${interruptAt}`,
- findingsUrl: "http://synthetic.test",
- };
- const calls: string[] = [];
- let interrupted = false;
- const reviewRunner = {
- async run<T>(review: CodexReview<T>): Promise<T> {
- expect(review.prompt).not.toContain("REVIEW_OUTPUT_ONLY");
- expect(review.prompt).not.toContain("MERGED_OUTPUT_ONLY");
- const originals = JSON.parse(
- review.prompt.slice(review.prompt.lastIndexOf("\n\n") + 2),
- ).findings as Finding[];
- for (const finding of originals)
- expect(finding).toEqual(
- findings.find(
- (original) => original.findingId === finding.findingId,
- )!,
- );
- const stage = review.model === "gpt-5.6-luna" ? "screen" : "pair";
- calls.push(stage);
- if (
- !interrupted &&
- stage === interruptAt &&
- (stage !== "pair" ||
- calls.filter((call) => call === "pair").length === 2)
- ) {
- interrupted = true;
- throw new Error("Synthetic interrupted review");
- }
- return review.validate(
- stage === "screen"
- ? {
- decisions: originals.slice(1).map((finding) => ({
- findingIds: [originals[0]!.findingId, finding.findingId],
- ...merged([originals[0]!, finding]),
- })),
- }
- : originals.some(
- (finding) => finding.findingId === findings[3]!.findingId,
- )
- ? distinct
- : merged(originals),
- );
- },
- };
- const fetch = async (url: URL) =>
- url.pathname.endsWith("/bulk/findings")
- ? Response.json([findings[0]!.findingId])
- : url.pathname.endsWith("/dedupe-groups")
- ? Response.json([])
- : Response.json({
- finding: findings[0],
- potentialDuplicates: findings.slice(1),
- });
- await publishScanToCustomInternal(scanDir, options, { environment, fetch });
- await expect(
- deduplicateScanInternal(document.scanId, options, {
- environment,
- runWorkbench: history,
- reviewRunner,
- fetch,
- }),
- ).rejects.toThrow("Synthetic interrupted review");
- const result = await deduplicateScanInternal(document.scanId, options, {
- environment,
- runWorkbench: history,
- reviewRunner,
- fetch,
- });
- expect(result.duplicateGroups).toEqual([
- findings.slice(0, 3).map((finding) => finding.findingId),
- ]);
- expect(calls.filter((stage) => stage === "screen")).toHaveLength(
- interruptAt === "screen" ? 2 : 1,
- );
- expect(calls.filter((stage) => stage === "pair")).toHaveLength(
- interruptAt === "pair" ? 4 : 3,
- );
- const count = calls.length;
- expect(
- await deduplicateScanInternal(document.scanId, options, {
- environment,
- runWorkbench: history,
- reviewRunner,
- fetch: async () => {
- throw new Error("Completed workflow must use its saved result");
- },
- }),
- ).toEqual(result);
- expect(calls).toHaveLength(count);
- },
-);
-
-test.each([
- "before-post",
- "before-write",
- "lost-ack",
- "lost-completion",
- "migrated-lost-ack",
-])(
- "replays the exact saved group payload after %s without models",
- async (failure) => {
- const { environment, document, history } = await fixture();
- const originals = [
- document.findings[0]!,
- { ...document.findings[0]!, findingId: `csf_${"f".repeat(24)}` },
- ];
- const options = {
- workflowId: `write-${failure}`,
- findingsUrl: "http://synthetic.test",
- };
- const bodies: string[] = [];
- const checkpoints: object[] = [];
- let modelCalls = 0;
- let failed = false;
- const reviewRunner = {
- async run<T>(review: CodexReview<T>): Promise<T> {
- modelCalls++;
- return review.validate(
- review.model === "gpt-5.6-luna"
- ? {
- decisions: [
- {
- findingIds: originals.map((finding) => finding.findingId),
- ...merged(originals),
- },
- ],
- }
- : merged(originals),
- );
- },
- };
- const fetch = async (url: URL, init: RequestInit) => {
- if (url.pathname.endsWith("/bulk/findings"))
- return Response.json([originals[0]!.findingId]);
- if (!url.pathname.endsWith("/dedupe-groups"))
- return Response.json({
- finding: originals[0],
- potentialDuplicates: [originals[1]],
- });
- const saved = (await new FindingWorkflow(
- options.workflowId,
- environment,
- ).get())!.stages.dedupe;
- expect(saved.status).toBe("running");
- expect(saved.result).toMatchObject({
- duplicateGroups: [originals.map((finding) => finding.findingId)],
- });
- expect(saved.pendingWrite).toEqual(JSON.parse(init.body as string));
- bodies.push(init.body as string);
- if (!failed && failure !== "lost-completion") {
- failed = true;
- if (failure === "before-write")
- return new Response("", { status: 503 });
- return new Response("incomplete acknowledgement", { status: 201 });
- }
- return Response.json([]);
- };
- const runWorkbench = async (args: readonly string[], input?: string) => {
- const payload = input ? JSON.parse(input) : {};
- if (
- !failed &&
- failure === "before-post" &&
- payload.action === "prepare-dedupe"
- ) {
- await history(args, input);
- failed = true;
- throw new Error("Synthetic stop before posting");
- }
- if (
- !failed &&
- failure === "lost-completion" &&
- payload.action === "complete" &&
- payload.stage === "dedupe"
- ) {
- failed = true;
- throw new Error("Synthetic completion receipt failure");
- }
- const result = await history(args, input);
- if (payload.action === "save-review") checkpoints.push(payload);
- return result;
- };
- await expect(
- deduplicateScanInternal(document.scanId, options, {
- environment,
- runWorkbench,
- reviewRunner,
- fetch,
- }),
- ).rejects.toThrow();
- if (failure === "migrated-lost-ack") {
- await restoreLegacyWorkflow(
- environment,
- (await new FindingWorkflow(options.workflowId, environment).get())!,
- checkpoints,
- );
- }
- expect(
- (await new FindingWorkflow(options.workflowId, environment).get())?.stages
- .dedupe.status,
- ).toBe("failed");
- const result = await deduplicateScanInternal(document.scanId, options, {
- environment,
- runWorkbench: history,
- reviewRunner,
- fetch,
- });
- expect(bodies).toHaveLength(failure === "before-post" ? 1 : 2);
- expect(new Set(bodies).size).toBe(1);
- expect(modelCalls).toBe(2);
- expect(
- (await new FindingWorkflow(options.workflowId, environment).get())?.stages
- .dedupe,
- ).toEqual({ status: "completed", result });
- },
-);
-
-test.each(["current", "legacy", "workflow-columns"])(
- "persists DISTINCT and complete SAME checkpoints across %s databases",
- async (version) => {
- const { environment, repository, document, workbenchOptions } =
- await fixture();
- const workflow = new FindingWorkflow("all-decisions", environment);
- await workflow.bind({});
- if (version === "workflow-columns") {
- const probe = await runCodexCommand(
- { command: workbenchOptions.python },
- [
- "-I",
- "-B",
- "-c",
- `import sqlite3, sys
-with sqlite3.connect(sys.argv[1]) as db:
- db.execute("DROP TABLE finding_workflow_reviews")
- db.execute("DELETE FROM schema_migrations WHERE version IN (37, 39)")`,
- join(environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
- ],
- environment,
- );
- expect(probe.exitCode, probe.stderr).toBe(0);
- }
- const originals = [
- document.findings[0]!,
- { ...document.findings[0]!, findingId: `csf_${"f".repeat(24)}` },
- ];
- let calls = 0;
- const checkpoints: Array<{
- key: string;
- binding: JsonObject;
- result: unknown;
- }> = [];
- const recordCheckpoint: typeof runWorkbench = async (
- options,
- args,
- input,
- ) => {
- const result = await runWorkbench(options, args, input);
- const payload = input ? JSON.parse(input) : {};
- if (payload.action === "save-review") checkpoints.push(payload);
- return result;
- };
- const runner = {
- async run<T>(review: CodexReview<T>): Promise<T> {
- calls++;
- return review.validate(
- review.model === "gpt-5.6-luna"
- ? {
- decisions: [
- {
- findingIds: originals.map((finding) => finding.findingId),
- ...distinct,
- },
- ],
- }
- : merged(originals),
- );
- },
- };
- const makeReviewer = async () =>
- new CodexDeduplicationReviewer(
- new CheckpointedReviewRunner(
- new FindingWorkflow(workflow.id, environment, recordCheckpoint),
- runner,
- await workflow.sourceSnapshot(repository),
- { allRepositories: true },
- "synthetic-settings-hash",
- ),
- );
- const first = await makeReviewer();
- const screening = await first.screen(originals);
- const pair = await first.reviewPair(originals);
- if (version === "legacy")
- await restoreLegacyWorkflow(
- environment,
- (await workflow.get())!,
- checkpoints,
- );
- const resumed = await makeReviewer();
- expect(await resumed.screen(originals)).toEqual(screening);
- expect(await resumed.reviewPair(originals)).toEqual(pair);
- expect(pair).toEqual(merged(originals));
- expect(calls).toBe(2);
- expect(checkpoints).toHaveLength(2);
- const probe = await runCodexCommand(
- { command: workbenchOptions.python },
- [
- "-I",
- "-B",
- "-c",
- `import json, sqlite3, sys
-db = sqlite3.connect(sys.argv[1])
-db.row_factory = sqlite3.Row
-assert "binding_json" not in {row["name"] for row in db.execute("PRAGMA table_info(finding_workflow_reviews)")}
-assert list(db.execute("PRAGMA foreign_key_check")) == []
-assert db.execute("PRAGMA integrity_check").fetchone()[0] == "ok"
-print(json.dumps([dict(row) for row in db.execute("SELECT * FROM finding_workflow_reviews ORDER BY review_key")]))`,
- join(environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
- ],
- environment,
- );
- expect(probe.exitCode, probe.stderr).toBe(0);
- const rows = JSON.parse(probe.stdout);
- for (const { key, binding, result } of checkpoints) {
- const source = binding["source"] as JsonObject;
- const row = rows.find(
- (row: { review_key: string }) => row.review_key === key,
- );
- expect(row).toMatchObject({
- workflow_id: workflow.id,
- review_contract_version: binding["version"],
- codex_version: binding["codexVersion"],
- source_repository_path: source["repository"],
- source_revision: source["revision"],
- source_refs_digest: source["refsDigest"],
- source_content_digest: source["content"],
- scope_repository_id: null,
- scope_all_repositories: 1,
- model: binding["model"],
- effort: binding["effort"],
- settings_digest: binding["settingsDigest"],
- prompt_digest: binding["promptDigest"],
- contract_digest: binding["contractDigest"],
- });
- if (version === "legacy")
- expect(row.created_at).toBe("2026-08-01T00:00:00Z");
- expect(JSON.parse(row.result_json)).toEqual(result);
- }
- },
-);
-
test.each([
"finding",
"source",
@@ -992,9 +32,19 @@ test.each([
"contract",
"configuration",
])("does not reuse a checkpoint for changed %s inputs", async (changed) => {
- const { environment, repository, document } = await fixture();
- const workflow = new FindingWorkflow(`changed-${changed}`, environment);
- await workflow.bind({});
+ await using fixture = await workflowFixture();
+ const { environment, repository, document } = fixture;
+ const store = checkpointWorkbench(`changed-${changed}`, {
+ repository,
+ revision: "synthetic-revision",
+ refsDigest: "synthetic-refs",
+ content: "synthetic-content",
+ });
+ const workflow = new FindingWorkflow(
+ `changed-${changed}`,
+ environment,
+ store.run,
+ );
let calls = 0;
const runner = {
async run<T>(review: CodexReview<T>): Promise<T> {
@@ -1022,7 +72,7 @@ test.each([
await initial.run(review);
expect(calls).toBe(1);
if (changed === "source")
- await writeFile(join(repository, "source.txt"), "changed source");
+ store.source = { ...store.source, content: "changed-content" };
if (changed === "configuration") {
await mkdir(environment.CODEX_HOME);
await writeFile(
@@ -1041,7 +91,7 @@ test.each([
if (changed === "contract")
next.schema = { type: "object", required: ["decision"] };
const resumed = new CheckpointedReviewRunner(
- new FindingWorkflow(workflow.id, environment),
+ new FindingWorkflow(workflow.id, environment, store.run),
runner,
await workflow.sourceSnapshot(repository),
changed === "scope" ? { allRepositories: true } : scope,
@@ -1051,91 +101,22 @@ test.each([
expect(calls).toBe(2);
});
-test("source snapshots include revisions and ignored content without following directory links", async () => {
- const { environment, repository, root } = await fixture();
- const git = (...args: string[]) =>
- execFileSync("git", args, { cwd: repository, stdio: "pipe" });
- git("init", "--quiet");
- await writeFile(join(repository, ".gitignore"), "ignored.txt\n");
- await writeFile(join(repository, "tracked.txt"), "original");
- await mkdir(join(repository, "tracked-directory"));
- await writeFile(
- join(repository, "tracked-directory", "source.txt"),
- "tracked source",
- );
- git("add", ".");
- git(
- "-c",
- "user.name=Example",
- "-c",
- "commit",
- "--quiet",
- "-m",
- "Synthetic source",
- );
- const workflow = new FindingWorkflow("source-snapshots", environment);
- const first = await workflow.sourceSnapshot(repository);
- await writeFile(join(repository, "ignored.txt"), "ignored source");
- const second = await workflow.sourceSnapshot(repository);
- expect(second["revision"]).toBe(first["revision"]);
- expect(second["content"]).not.toBe(first["content"]);
- git(
- "-c",
- "user.name=Example",
- "-c",
- "commit",
- "--quiet",
- "--allow-empty",
- "-m",
- "Next synthetic revision",
- );
- expect((await workflow.sourceSnapshot(repository))["revision"]).not.toBe(
- first["revision"],
- );
- const beforeRef = await workflow.sourceSnapshot(repository);
- git("branch", "synthetic-source-reference");
- const afterRef = await workflow.sourceSnapshot(repository);
- expect(afterRef["revision"]).toBe(beforeRef["revision"]);
- expect(afterRef["refsDigest"]).not.toBe(beforeRef["refsDigest"]);
- const outside = join(root, "outside");
- await mkdir(outside);
- await writeFile(join(outside, "private.txt"), "synthetic outside content");
- const { symlink } = await import("node:fs/promises");
- await symlink(
- outside,
- join(repository, "linked"),
- process.platform === "win32" ? "junction" : "dir",
- );
- const linked = await workflow.sourceSnapshot(repository);
- await writeFile(join(outside, "private.txt"), "changed outside content");
- expect(await workflow.sourceSnapshot(repository)).toEqual(linked);
- await rm(join(repository, "tracked-directory"), { recursive: true });
- await writeFile(join(outside, "source.txt"), "synthetic outside source");
- await symlink(
- outside,
- join(repository, "tracked-directory"),
- process.platform === "win32" ? "junction" : "dir",
- );
- const replaced = await workflow.sourceSnapshot(repository);
- await writeFile(join(outside, "source.txt"), "changed outside source");
- expect(await workflow.sourceSnapshot(repository)).toEqual(replaced);
-});
-
test("does not checkpoint a review when source changes during its execution", async () => {
- const { environment, repository, document } = await fixture();
- const workflow = new FindingWorkflow("source-drift", environment);
- await workflow.bind({});
+ await using fixture = await workflowFixture();
+ const { environment, repository, document } = fixture;
+ const store = checkpointWorkbench("source-drift", {
+ repository,
+ revision: "synthetic-revision",
+ refsDigest: "synthetic-refs",
+ content: "synthetic-content",
+ });
+ const workflow = new FindingWorkflow("source-drift", environment, store.run);
const source = await workflow.sourceSnapshot(repository);
const runner = new CheckpointedReviewRunner(
workflow,
{
async run<T>(review: CodexReview<T>): Promise<T> {
- await writeFile(
- join(repository, "changed.txt"),
- "changed during review",
- );
+ store.source = { ...store.source, content: "changed-during-review" };
return review.validate(distinct);
},
},
@@ -1152,7 +133,8 @@ test("does not checkpoint a review when source changes during its execution", as
await expect(runner.run(review)).rejects.toThrow(
"Source changed during deduplication",
);
- await rm(join(repository, "changed.txt"));
+ expect(store.saved).toHaveLength(0);
+ store.source = source;
let calls = 0;
await new CheckpointedReviewRunner(
workflow,
@@ -1167,3 +149,97 @@ test("does not checkpoint a review when source changes during its execution", as
).run(review);
expect(calls).toBe(1);
});
+
+test("returns a completed stage's saved result without running the operation", async () => {
+ await using fixture = await workflowFixture();
+ const result = { duplicateGroups: [] };
+ const workbench = scriptedWorkbench([
+ {
+ request: { id: "completed", action: "begin", stage: "dedupe" },
+ response: {
+ workflow: { stages: { dedupe: { status: "completed", result } } },
+ },
+ },
+ ]);
+ const workflow = new FindingWorkflow(
+ "completed",
+ fixture.environment,
+ workbench.run,
+ );
+ expect(
+ await workflow.run<typeof result>("dedupe", async () => {
+ throw new Error("Completed work must not run again");
+ }),
+ ).toEqual(result);
+ workbench.assertDone();
+});
+
+test("preserves the operation error when recording a failed stage also fails", async () => {
+ await using fixture = await workflowFixture();
+ const failure = new Error("Synthetic publication failure");
+ const workbench = scriptedWorkbench([
+ {
+ request: { id: "failed", action: "begin", stage: "publish" },
+ response: { workflow: { stages: { publish: { status: "running" } } } },
+ },
+ {
+ request: {
+ id: "failed",
+ action: "fail",
+ stage: "publish",
+ error: failure.message,
+ },
+ error: new Error("Synthetic unavailable persistence"),
+ },
+ ]);
+ const workflow = new FindingWorkflow(
+ "failed",
+ fixture.environment,
+ workbench.run,
+ );
+ await expect(
+ workflow.run("publish", async () => {
+ throw failure;
+ }),
+ ).rejects.toBe(failure);
+ workbench.assertDone();
+});
+
+test("checks failure-recording requests even when the caller handles persistence errors", async () => {
+ await using fixture = await workflowFixture();
+ const workbench = scriptedWorkbench([
+ {
+ request: {
+ id: "handled-failure",
+ action: "fail",
+ stage: "publish",
+ error: "Synthetic expected failure",
+ },
+ },
+ ]);
+ const workflow = new FindingWorkflow(
+ "handled-failure",
+ fixture.environment,
+ workbench.run,
+ );
+ await workflow.fail("publish", new Error("Synthetic different failure"));
+ expect(() => workbench.assertDone()).toThrow("Synthetic expected failure");
+});
+
+test("rejects unexpected workbench commands after their errors are caught", async () => {
+ const workbench = scriptedWorkbench([]);
+ await expect(
+ workbench.run(
+ { environment: {}, pluginRoot: "unused", python: "unused" },
+ ["unexpected-command"],
+ "{}",
+ ),
+ ).rejects.toThrow();
+ expect(() => workbench.assertDone()).toThrow();
+});
+
+test("normalizes a workflow destination without storing URL credentials", () => {
+ expect(workflowDestination("http://synthetic:[email protected]")).toBe(
+ "http://synthetic.test/",
+ );
+});
diff --git a/sdk/typescript/tests-ts/support/workbench-fakes.ts b/sdk/typescript/tests-ts/support/workbench-fakes.ts
new file mode 100644
index 000000000..370a66d21
--- /dev/null
+++ b/sdk/typescript/tests-ts/support/workbench-fakes.ts
@@ -0,0 +1,93 @@
+import { expect } from "bun:test";
+import type { JsonObject } from "../../src/config.js";
+import type { runWorkbench } from "../../src/runtime.js";
+
+function request(
+ args: readonly string[],
+ input: string | undefined,
+): JsonObject {
+ expect(args).toEqual(["finding-workflow"]);
+ expect(input).toBeDefined();
+ return JSON.parse(input!);
+}
+
+export function scriptedWorkbench(
+ steps: readonly {
+ request: object;
+ response?: object | ((request: JsonObject) => object);
+ error?: Error;
+ }[],
+) {
+ const requests: {
+ args: readonly string[];
+ input: string | undefined;
+ }[] = [];
+ const run: typeof runWorkbench = async (_options, args, input) => {
+ const step = steps[requests.length];
+ requests.push({ args, input });
+ if (!step)
+ throw new Error(
+ `Unexpected workbench request: ${JSON.stringify({ args, input })}`,
+ );
+ if (step.error) throw step.error;
+ const response =
+ typeof step.response === "function"
+ ? step.response(request(args, input))
+ : step.response;
+ return structuredClone(response ?? {}) as JsonObject;
+ };
+ return {
+ run,
+ assertDone() {
+ // The caller may intentionally catch workbench errors.
+ expect(
+ requests.map(({ args, input }) => request(args, input)),
+ ).toMatchObject(steps.map((step) => step.request));
+ },
+ };
+}
+
+/** A bound workflow's checkpoint storage; no workflow transitions or SQL behavior. */
+export function checkpointWorkbench(workflowId: string, source: JsonObject) {
+ const reviews = new Map<string, unknown>();
+ const saved: JsonObject[] = [];
+ const fixture: {
+ source: JsonObject;
+ saved: JsonObject[];
+ run: typeof runWorkbench;
+ } = {
+ source,
+ saved,
+ run: (async (_options, args, input) => {
+ const payload = request(args, input);
+ expect(payload["id"]).toBe(workflowId);
+ switch (payload["action"]) {
+ case "source":
+ expect(payload["repository"]).toBe(fixture.source["repository"]);
+ return { source: structuredClone(fixture.source) };
+ case "get-review":
+ expect(typeof payload["key"]).toBe("string");
+ return {
+ review: structuredClone(
+ reviews.get(payload["key"] as string) ?? null,
+ ),
+ } as JsonObject;
+ case "save-review":
+ expect(typeof payload["key"]).toBe("string");
+ expect(payload["binding"]).toBeObject();
+ expect(payload).toHaveProperty("result");
+ saved.push(structuredClone(payload));
+ reviews.set(
+ payload["key"] as string,
+ structuredClone(payload["result"]),
+ );
+ return {};
+ default:
+ throw new Error(
+ `Unexpected checkpoint request: ${JSON.stringify(payload)}`,
+ );
+ }
+ }) satisfies typeof runWorkbench,
+ };
+ return fixture;
+}
diff --git a/sdk/typescript/tests-ts/support/workflow-fixture.ts b/sdk/typescript/tests-ts/support/workflow-fixture.ts
new file mode 100644
index 000000000..376421f84
--- /dev/null
+++ b/sdk/typescript/tests-ts/support/workflow-fixture.ts
@@ -0,0 +1,52 @@
+import {
+ chmod,
+ cp,
+ mkdir,
+ mkdtemp,
+ readFile,
+ realpath,
+ rm,
+} from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import type { FindingsDocument } from "../../src/models.js";
+import { PLUGIN_ROOT } from "../plugin-root.js";
+
+export async function workflowFixture() {
+ const root = await realpath(
+ await mkdtemp(join(tmpdir(), "findings-workflow-")),
+ );
+ const scanDir = join(root, "scan");
+ const repository = join(root, "repository");
+ try {
+ await mkdir(repository);
+ await cp(join(PLUGIN_ROOT, "examples/completed-scan"), scanDir, {
+ recursive: true,
+ });
+ if (process.platform !== "win32") await chmod(scanDir, 0o700);
+ const environment = {
+ PATH: process.env["PATH"],
+ SystemRoot: process.env["SystemRoot"],
+ TEMP: process.env["TEMP"],
+ TMP: process.env["TMP"],
+ CODEX_HOME: join(root, "codex"),
+ CODEX_SECURITY_STATE_DIR: join(root, "state"),
+ };
+ const document = JSON.parse(
+ await readFile(join(scanDir, "findings.json"), "utf8"),
+ ) as FindingsDocument;
+ return {
+ root,
+ scanDir,
+ repository,
+ environment,
+ document,
+ async [Symbol.asyncDispose]() {
+ await rm(root, { recursive: true, force: true });
+ },
+ };
+ } catch (error) {
+ await rm(root, { recursive: true, force: true });
+ throw error;
+ }
+}