OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
refactor(workbench): simplify command preparation and transactions (#1390)
mldangelo-oai4c231da1dc0b
diff --git a/plugins/codex-security/scripts/config_preflight.py b/plugins/codex-security/scripts/config_preflight.pyindex a0e7127e2..0132f7980 100644--- a/plugins/codex-security/scripts/config_preflight.py+++ b/plugins/codex-security/scripts/config_preflight.py@@ -374,28 +374,6 @@ def config_profile_layer_path(profile: str | None) -> Path | None:return path if path.is_file() else None-def parse_multi_agent_v2_enabled(feature_config: Any) -> bool | None:- if isinstance(feature_config, bool):- return feature_config- if not isinstance(feature_config, dict):- raise ValueError("features.multi_agent_v2 must be a boolean or table")- if "enabled" not in feature_config:- return None- enabled = feature_config["enabled"]- if not isinstance(enabled, bool):- raise ValueError("features.multi_agent_v2.enabled must be a boolean")- return enabled---def merge_toml_value(base: Any, overlay: Any) -> Any:- if isinstance(base, dict) and isinstance(overlay, dict):- merged = dict(base)- for key, value in overlay.items():- merged[key] = merge_toml_value(merged[key], value) if key in merged else value- return merged- return overlay--def lookup_multi_agent_v2_enabled(*,config_layers: list[tuple[Path, dict[str, Any]]],@@ -409,38 +387,29 @@ def lookup_multi_agent_v2_enabled(return True, enabled, "effective-config"v2_configs = []- config_views = list(iter_config_views(config_layers, config_profile))- for source, config in reversed(config_views):+ if "features.multi_agent_v2" in effective_config:+ v2_configs.append(("effective-config", effective_config["features.multi_agent_v2"]))+ for source, config in iter_config_views(config_layers, config_profile):found, feature_config = lookup_dotted(config, "features.multi_agent_v2")if found:v2_configs.append((source, feature_config))- if "features.multi_agent_v2" in effective_config:- v2_configs.append(("effective-config", effective_config["features.multi_agent_v2"]))-- if not v2_configs:- return False, None, None-- merged_v2_config: Any = None- enabled_source: str | None = None- for index, (source, feature_config) in enumerate(v2_configs):- tables_merge = isinstance(merged_v2_config, dict) and isinstance(feature_config, dict)- if index and not tables_merge:- enabled_source = None- merged_v2_config = (- merge_toml_value(merged_v2_config, feature_config) if index else feature_config- )- if isinstance(feature_config, bool) or (- isinstance(feature_config, dict) and "enabled" in feature_config- ):- enabled_source = source-- enabled = parse_multi_agent_v2_enabled(merged_v2_config)- return (- (True, enabled, enabled_source)- if enabled is not None- else (True, False, "documented-default")- )+ table_override = False+ for source, feature_config in v2_configs:+ # Higher tables inherit enabled only through uninterrupted lower tables.+ if table_override and not isinstance(feature_config, dict):+ break+ if isinstance(feature_config, bool):+ return True, feature_config, source+ if not isinstance(feature_config, dict):+ raise ValueError("features.multi_agent_v2 must be a boolean or table")+ if "enabled" in feature_config:+ enabled = feature_config["enabled"]+ if not isinstance(enabled, bool):+ raise ValueError("features.multi_agent_v2.enabled must be a boolean")+ return True, enabled, source+ table_override = True+ return (True, False, "documented-default") if table_override else (False, None, None)def resolve_multi_agent_context(diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.pyindex 4a28a4e3c..d0bc61497 100644--- a/plugins/codex-security/scripts/workbench_db.py+++ b/plugins/codex-security/scripts/workbench_db.py@@ -42,12 +42,6 @@ import workbench_saved_results as saved_resultsimport workbench_scan_history as scan_historyimport workbench_scan_usage as scan_usageimport workbench_severity as severity-from filesystem_identity import (- serialize_filesystem_identity as serialize_filesystem_identity,-)-from filesystem_identity import (- stored_filesystem_identity_matches as stored_filesystem_identity_matches,-)from finalize_scan_contract import (PRODUCER_NAME,ContractError,@@ -944,7 +938,7 @@ def _start_prompt_driven_scan(target_root = scan_target_root(args.scan_root, target)connection.execute("BEGIN IMMEDIATE")- try:+ with connection:current_target = require_remediation_target(target_path)current_diff_target = (require_diff_target(@@ -1063,10 +1057,6 @@ def _start_prompt_driven_scan()if claimed.rowcount != 1:raise SystemExit("Codex Security headless scan ownership could not be recorded.")- connection.commit()- except BaseException:- connection.rollback()- raisereturn {**scan_context(connection, scan_id), "startDisposition": "created"}@@ -1831,24 +1821,6 @@ def coverage_for_comparison(scan: sqlite3.Row) -> dict[str, Any]:return saved_results.coverage_for_comparison(_WORKBENCH_DB_CONTEXT, scan)-def preserve_scan_results_locked(connection: sqlite3.Connection, scan_id: str) -> bool:- return saved_results.preserve_scan_results_locked(_WORKBENCH_DB_CONTEXT, connection, scan_id)---def recover_scan_results(- connection: sqlite3.Connection, args: argparse.Namespace-) -> dict[str, Any]:- return saved_results.recover_scan_results(_WORKBENCH_DB_CONTEXT, connection, args)---def fail_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:- return saved_results.fail_scan(_WORKBENCH_DB_CONTEXT, connection, args)---def cancel_scan_locked(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:- return saved_results.cancel_scan_locked(_WORKBENCH_DB_CONTEXT, connection, args)--def preserve_stopped_results_after_transition(connection: sqlite3.Connection, scan_id: str) -> None:saved_results.preserve_stopped_results_after_transition(_WORKBENCH_DB_CONTEXT, connection, scan_id@@ -1953,7 +1925,7 @@ def request_finding_remediation() -> dict[str, Any]:request_id = require_uuid(args.request_id, "request-id")action_token = require_uuid(args.action_token, "action-token")- try:+ with connection:occurrence = require_occurrence(connection, args.occurrence_id)require_finding_open(connection, occurrence["id"])scan = require_scan(connection, occurrence["scan_id"])@@ -2042,10 +2014,6 @@ def request_finding_remediation(timestamp,),)- connection.commit()- except BaseException:- connection.rollback()- raisereturn scan_context(connection, occurrence["scan_id"])@@ -2054,7 +2022,7 @@ def request_finding_remediation_action() -> dict[str, Any]:request_id = require_uuid(args.request_id, "request-id")action_token = require_uuid(args.action_token, "action-token")- try:+ with connection:occurrence = require_occurrence(connection, args.occurrence_id)require_finding_open(connection, occurrence["id"])scan = require_scan(connection, occurrence["scan_id"])@@ -2118,10 +2086,6 @@ def request_finding_remediation_action(raise SystemExit("This remediation request changed. Refresh it before recording an update.")- connection.commit()- except BaseException:- connection.rollback()- raisereturn scan_context(connection, occurrence["scan_id"])@@ -2131,7 +2095,7 @@ def claim_finding_remediation_resend(request_id = require_uuid(args.request_id, "request-id")action_token = require_uuid(args.action_token, "action-token")connection.execute("BEGIN IMMEDIATE")- try:+ with connection:timestamp = now()occurrence = require_occurrence(connection, args.occurrence_id)require_finding_open(connection, occurrence["id"])@@ -2198,10 +2162,6 @@ def claim_finding_remediation_resend(unavailable = "This remediation host request is still owned by another panel. Retry after its lease expires."if updated.rowcount != 1:raise SystemExit(unavailable)- connection.commit()- except BaseException:- connection.rollback()- raiseresult = scan_context(connection, occurrence["scan_id"])result["actionToken"] = claimed_tokenreturn result@@ -2377,10 +2337,6 @@ def set_finding_remediation(return scan_context(connection, occurrence["scan_id"])-def export_findings(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:- return publication.export_findings(_WORKBENCH_PUBLICATION_CONTEXT, connection, args)--def require_reviewed_patch_applied(scan: sqlite3.Row, remediation: sqlite3.Row, patch_path: str) -> str | None:@@ -3282,10 +3238,7 @@ def read_json_object(path: Path) -> dict[str, Any]:return payload-_WORKBENCH_PUBLICATION_CONTEXT = SimpleNamespace(**globals())---_WORKBENCH_DB_CONTEXT = SimpleNamespace(**globals())+_WORKBENCH_PUBLICATION_CONTEXT = _WORKBENCH_DB_CONTEXT = SimpleNamespace(**globals())def main() -> None:@@ -3436,11 +3389,11 @@ def main() -> None:elif args.command == "cancel-scan":result = saved_results.cancel_scan(_WORKBENCH_DB_CONTEXT, connection, args)elif args.command == "fail-scan":- result = fail_scan(connection, args)+ result = saved_results.fail_scan(_WORKBENCH_DB_CONTEXT, connection, args)elif args.command == "preserve-scan-results":result = saved_results.preserve_scan_results(_WORKBENCH_DB_CONTEXT, connection, args)elif args.command == "recover-scan-results":- result = recover_scan_results(connection, args)+ result = saved_results.recover_scan_results(_WORKBENCH_DB_CONTEXT, connection, args)elif args.command == "write-scan-draft":result = saved_results.write_scan_draft(_WORKBENCH_DB_CONTEXT, connection, args)elif args.command == "save-scan-artifact":@@ -3481,7 +3434,7 @@ def main() -> None:_WORKBENCH_PUBLICATION_CONTEXT, connection, args)elif args.command == "export-findings":- result = export_findings(connection, args)+ result = publication.export_findings(_WORKBENCH_PUBLICATION_CONTEXT, connection, args)elif args.command == "database-info":result = {"databasePath": str(database_path())}elif args.command == "severity-classification":diff --git a/plugins/codex-security/scripts/workbench_scan_start.py b/plugins/codex-security/scripts/workbench_scan_start.pyindex cac00420f..e4aa20191 100644--- a/plugins/codex-security/scripts/workbench_scan_start.py+++ b/plugins/codex-security/scripts/workbench_scan_start.py@@ -5,6 +5,7 @@ from __future__ import annotationsimport argparseimport jsonimport os+import reimport sqlite3import sysimport tempfile@@ -26,10 +27,7 @@ from workbench_validation import optional_textdef safe_segment(value: str) -> str:- segment = "".join(- character if character.isalnum() or character in "._-" else "-" for character in value- )- return segment.strip("-") or "scan"+ return re.sub(r"[^\w.-]", "-", value).strip("-") or "scan"def compact_timestamp() -> str:diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.pyindex 9918cc84b..04fed9335 100644--- a/plugins/codex-security/scripts/workbench_target.py+++ b/plugins/codex-security/scripts/workbench_target.py@@ -605,12 +605,9 @@ def source_directory_snapshot_paths(target: Path) -> list[Path]:def directory_content_digest(target: Path, *, excluded: tuple[Path, ...] = (), include_ignored: bool = False) -> str:- excluded_relative = []- for path in excluded:- try:- excluded_relative.append(path.relative_to(target))- except ValueError:- continue+ excluded_relative = [+ path.relative_to(target) for path in excluded if path.is_relative_to(target)+ ]paths = (source_directory_snapshot_paths(target)if include_ignored@@ -622,10 +619,7 @@ def directory_content_digest(update_digest_field(digest, b"format", b"codex-security-directory/v1")for path in paths:relative_path = path.relative_to(target)- if any(- relative_path == excluded_path or excluded_path in relative_path.parents- for excluded_path in excluded_relative- ):+ if any(relative_path.is_relative_to(path) for path in excluded_relative):continuetry:metadata = path.lstat()@@ -675,12 +669,9 @@ def directory_snapshot_regular_file_count(target: Path) -> int:def copy_directory_excluding(source: Path, destination: Path, excluded: tuple[Path, ...]) -> None:- excluded_relative = []- for path in excluded:- try:- excluded_relative.append(path.relative_to(source))- except ValueError:- continue+ excluded_relative = [+ path.relative_to(source) for path in excluded if path.is_relative_to(source)+ ]def ignored(directory: str, names: list[str]) -> list[str]:relative = Path(directory).relative_to(source)@@ -707,19 +698,13 @@ def copy_git_worktree_files(source: Path, destination: Path, excluded: tuple[Pat)if listed is None:raise SystemExit("Could not inspect files in the selected Git working tree.")- excluded_relative = []- for path in excluded:- try:- excluded_relative.append(path.relative_to(repository))- except ValueError:- continue+ excluded_relative = [+ path.relative_to(repository) for path in excluded if path.is_relative_to(repository)+ ]destination.mkdir()for raw_path in sorted(path for path in listed.split(b"\0") if path):relative = Path(os.fsdecode(raw_path))- if any(- relative == excluded_path or excluded_path in relative.parents- for excluded_path in excluded_relative- ):+ if any(relative.is_relative_to(path) for path in excluded_relative):continuesource_path = repository / relativetry:diff --git a/plugins/codex-security/tests/test_deep_scan_successful_publication.py b/plugins/codex-security/tests/test_deep_scan_successful_publication.pyindex 9dcadb55e..4fbb5376c 100644--- a/plugins/codex-security/tests/test_deep_scan_successful_publication.py+++ b/plugins/codex-security/tests/test_deep_scan_successful_publication.py@@ -339,7 +339,8 @@ def test_stopped_deep_scan_still_salvages_saved_findings(result.write_text("{interrupted worker output")result_bytes = result.read_bytes()- stopped = workbench_api["fail_scan"](+ stopped = workbench_api["saved_results"].fail_scan(+ workbench_api["_WORKBENCH_DB_CONTEXT"],workbench_db,Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Scan interrupted."@@ -359,8 +360,8 @@ def test_stopped_deep_scan_still_salvages_saved_findings(artifact_names = ("scan-manifest.json", "findings.json", "coverage.json")published = {name: (scan.scan_dir / name).read_bytes() for name in artifact_names}- recovered = workbench_api["recover_scan_results"](- workbench_db, Namespace(scan_id=scan.scan_id)+ recovered = workbench_api["saved_results"].recover_scan_results(+ workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id))["scan"]assert recovered["findingCount"] == len(expected_summaries)assert {name: (scan.scan_dir / name).read_bytes() for name in artifact_names} == published@@ -402,7 +403,8 @@ def test_stopped_deep_scan_ignores_non_reducer_sources_without_coverage(source_bytes = source_path.read_bytes()source_relative = source_path.relative_to(scan.scan_dir).as_posix()- stopped = workbench_api["fail_scan"](+ stopped = workbench_api["saved_results"].fail_scan(+ workbench_api["_WORKBENCH_DB_CONTEXT"],workbench_db,Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Scan interrupted."@@ -424,8 +426,8 @@ def test_stopped_deep_scan_ignores_non_reducer_sources_without_coverage(artifact_names = ("scan-manifest.json", "findings.json", "coverage.json")published = {name: (scan.scan_dir / name).read_bytes() for name in artifact_names}- recovered = workbench_api["recover_scan_results"](- workbench_db, Namespace(scan_id=scan.scan_id)+ recovered = workbench_api["saved_results"].recover_scan_results(+ workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id))["scan"]assert recovered["findingCount"] == 1diff --git a/plugins/codex-security/tests/test_windows_report_e2e.py b/plugins/codex-security/tests/test_windows_report_e2e.pyindex 6a578a48d..84404e84b 100644--- a/plugins/codex-security/tests/test_windows_report_e2e.py+++ b/plugins/codex-security/tests/test_windows_report_e2e.py@@ -49,8 +49,10 @@ def test_workbench_completion_and_exports_use_windows_file_backend(tmp_path: Patconnection,SimpleNamespace(claim_token=None, cost_json=None, scan_id=scan_id),)["scan"]- exported = namespace["export_findings"](- connection, SimpleNamespace(scan_id=scan_id, format="csv")+ exported = namespace["publication"].export_findings(+ namespace["_WORKBENCH_PUBLICATION_CONTEXT"],+ connection,+ SimpleNamespace(scan_id=scan_id, format="csv"),)assert completed["progress"]["status"] == "complete"diff --git a/plugins/codex-security/tests/test_workbench_db.py b/plugins/codex-security/tests/test_workbench_db.pyindex 7ffb4fa89..ce1bf87e3 100644--- a/plugins/codex-security/tests/test_workbench_db.py+++ b/plugins/codex-security/tests/test_workbench_db.py@@ -1312,7 +1312,7 @@ def test_completed_finding_triage_and_remediation_persist(def test_filesystem_identity_serialization_supports_windows_stat_values() -> None:- namespace = runpy.run_path(str(SCRIPT), run_name="codex_security_workbench_db")+ namespace = runpy.run_path(str(SCRIPT.with_name("filesystem_identity.py")))serialize_identity = namespace["serialize_filesystem_identity"]identity_matches = namespace["stored_filesystem_identity_matches"]windows_device_id = (1 << 64) - 1diff --git a/plugins/codex-security/tests/test_workbench_prompt_only_scan.py b/plugins/codex-security/tests/test_workbench_prompt_only_scan.pyindex a29d1ab24..2ec1e131a 100644--- a/plugins/codex-security/tests/test_workbench_prompt_only_scan.py+++ b/plugins/codex-security/tests/test_workbench_prompt_only_scan.py@@ -263,7 +263,8 @@ def test_setup_scan_reuses_checked_target_metadata(tmp_path: Path) -> None:"SELECT target_device, target_inode FROM scans WHERE id = ?",(scan_id,),).fetchone()- serialize_identity = start_globals["serialize_filesystem_identity"]+ identity_helpers = runpy.run_path(str(SCRIPT.with_name("filesystem_identity.py")))+ serialize_identity = identity_helpers["serialize_filesystem_identity"]assert identity == (serialize_identity(metadata.st_dev),serialize_identity(metadata.st_ino),diff --git a/sdk/typescript/tests-ts/stopped-scan-results.test.ts b/sdk/typescript/tests-ts/stopped-scan-results.test.tsindex 290b28996..e0b562e68 100644--- a/sdk/typescript/tests-ts/stopped-scan-results.test.ts+++ b/sdk/typescript/tests-ts/stopped-scan-results.test.ts@@ -80,7 +80,7 @@ const stoppedScanProbe = [" connection = workbench_db.connect()"," original_write = workbench_db.saved_results._write_prepared_scan_finalization"," workbench_db.saved_results._write_prepared_scan_finalization = lambda prepared, *, projection_warnings=None: (_ for _ in ()).throw(OSError('synthetic cancellation publication failure'))",- " workbench_db.cancel_scan_locked(connection, argparse.Namespace(scan_id=scan_id, thread_id=None))",+ " workbench_db.saved_results.cancel_scan_locked(workbench_db._WORKBENCH_DB_CONTEXT, connection, argparse.Namespace(scan_id=scan_id, thread_id=None))"," workbench_db.saved_results._write_prepared_scan_finalization = original_write"," connection.close()"," run('preserve-scan-results', '--scan-id', scan_id, '--thread-id', 'stopped-result-owner')",@@ -109,12 +109,12 @@ const stoppedScanProbe = [" workbench_db.saved_results._write_prepared_scan_finalization = lambda prepared, *, projection_warnings=None: (_ for _ in ()).throw(OSError('synthetic publication failure'))"," first_failed = False"," try:",- " workbench_db.preserve_scan_results_locked(connection, scan_id)",+ " workbench_db.saved_results.preserve_scan_results_locked(workbench_db._WORKBENCH_DB_CONTEXT, connection, scan_id)"," except OSError:"," first_failed = True"," frozen_after_failure = connection.execute('SELECT retained_source_digests_json FROM scans WHERE id = ?', (scan_id,)).fetchone()[0]"," workbench_db.saved_results._write_prepared_scan_finalization = original_write",- " retry_published = workbench_db.preserve_scan_results_locked(connection, scan_id)",+ " retry_published = workbench_db.saved_results.preserve_scan_results_locked(workbench_db._WORKBENCH_DB_CONTEXT, connection, scan_id)"," frozen_after_success = connection.execute('SELECT retained_source_digests_json FROM scans WHERE id = ?', (scan_id,)).fetchone()[0]"," final_manifest = json.loads(manifest_path.read_text(encoding='utf-8'))"," final_findings = json.loads((scan_dir / 'findings.json').read_text(encoding='utf-8'))['findings']",