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(plugin): simplify workbench owner calls (#1205)
mldangelo-oai37cd0ed191f0
diff --git a/plugins/codex-security/mcp-app/src/python_command.ts b/plugins/codex-security/mcp-app/src/python_command.tsindex e8af5e646..76bba7fec 100644--- a/plugins/codex-security/mcp-app/src/python_command.ts+++ b/plugins/codex-security/mcp-app/src/python_command.ts@@ -67,8 +67,7 @@ export async function isUsablePythonExecutable(platform: PythonPlatform,): Promise<boolean> {try {- const candidateStat = await fs.stat(candidate);- if (!candidateStat.isFile()) {+ if (!(await fs.stat(candidate)).isFile()) {return false;}if (platform !== "win32") {diff --git a/plugins/codex-security/scripts/config_preflight.py b/plugins/codex-security/scripts/config_preflight.pyindex c30614a08..a0e7127e2 100644--- a/plugins/codex-security/scripts/config_preflight.py+++ b/plugins/codex-security/scripts/config_preflight.py@@ -17,6 +17,10 @@ try:except ModuleNotFoundError: # pragma: no cover - Python 3.10 onlyimport tomli as tomllib+# Some plugin hosts launch Python with safe-path isolation enabled.+sys.path.insert(0, str(Path(__file__).resolve().parent))+from workbench_constants import positive_int+PLUGIN_ROOT = Path(__file__).resolve().parents[1]DEFAULT_REGISTRY = PLUGIN_ROOT / "preflight" / "capability-profiles.toml"DEFAULT_CODEX_HOME = Path(os.environ.get("CODEX_HOME", "~/.codex")).expanduser()@@ -147,13 +151,6 @@ def parse_bool(value: str) -> bool:raise ValueError(f"expected true or false, got {value!r}")-def positive_int(value: str) -> int:- parsed = int(value)- if parsed < 1:- raise argparse.ArgumentTypeError("expected a positive integer")- return parsed--def parse_assignment(raw: str) -> tuple[str, str]:key, separator, value = raw.partition("=")if not separator or not key or not value:@@ -865,7 +862,6 @@ def evaluate(args: argparse.Namespace) -> dict[str, Any]:if path in project_config_paths_set:# Codex strips project-local profile selection and definitions before# resolving the merged config. Mirror that denylist here.- config = dict(config)config.pop("profile", None)config.pop("profiles", None)config_layers.append((path, config))diff --git a/plugins/codex-security/scripts/deep_scan_workbench.py b/plugins/codex-security/scripts/deep_scan_workbench.pyindex 1ccb1a8f7..bfe93f061 100644--- a/plugins/codex-security/scripts/deep_scan_workbench.py+++ b/plugins/codex-security/scripts/deep_scan_workbench.py@@ -150,10 +150,6 @@ def dependencies() -> Any:return _dependencies-def now() -> str:- return dependencies().now()--def _bounded_error_text(message: str, maximum: int) -> str:if len(message) <= maximum:return message@@ -188,18 +184,6 @@ def _parse_timestamp(value: str) -> datetime:return datetime.fromisoformat(value)-def require_scan(connection: sqlite3.Connection, scan_id: str) -> sqlite3.Row:- return dependencies().require_scan(connection, scan_id)---def require_workspace(connection: sqlite3.Connection, workspace_id: str) -> sqlite3.Row:- return dependencies().require_workspace(connection, workspace_id)---def require_canonical_scan_directory(scan_dir: Path) -> Path:- return dependencies().require_canonical_scan_directory(scan_dir)--def require_deep_scan_run(connection: sqlite3.Connection, scan_id: str) -> sqlite3.Row:scan_id = require_uuid(scan_id, "scan-id")row = connection.execute(@@ -211,7 +195,7 @@ def require_deep_scan_run(connection: sqlite3.Connection, scan_id: str) -> sqlitdef deep_scan_deadline_reached(run: sqlite3.Row) -> bool:- elapsed = _parse_timestamp(now()) - _parse_timestamp(str(run["created_at"]))+ elapsed = _parse_timestamp(dependencies().now()) - _parse_timestamp(str(run["created_at"]))return elapsed.total_seconds() / 3600 >= run["max_time_hours"]@@ -234,8 +218,8 @@ def require_deep_scan_ready_for_parent_completion(def require_owned_scan(connection: sqlite3.Connection, scan_id: str, thread_id: str) -> tuple[sqlite3.Row, sqlite3.Row]:- scan = require_scan(connection, scan_id)- workspace = require_workspace(connection, scan["workspace_id"])+ scan = dependencies().require_scan(connection, scan_id)+ workspace = dependencies().require_workspace(connection, scan["workspace_id"])owner = optional_text(thread_id, maximum=512)if owner is None:raise SystemExit("thread-id is required.")@@ -257,7 +241,7 @@ def deep_scan_path(raise SystemExit(f"{label} must be an absolute path inside the scan directory.")try:resolved = supplied.resolve(strict=True)- scan_dir = require_canonical_scan_directory(Path(scan["scan_dir"]))+ scan_dir = dependencies().require_canonical_scan_directory(Path(scan["scan_dir"]))resolved.relative_to(scan_dir)except (OSError, RuntimeError, ValueError) as exc:raise SystemExit(f"{label} must be an existing path inside the scan directory.") from exc@@ -352,7 +336,7 @@ def canonical_discovery_artifacts(scan: sqlite3.Row) -> dict[str, str]:def deep_scan_state(connection: sqlite3.Connection, scan_id: str) -> dict[str, Any]:run = require_deep_scan_run(connection, scan_id)- scan = require_scan(connection, run["scan_id"])+ scan = dependencies().require_scan(connection, run["scan_id"])worker_rows = connection.execute("""SELECT *@@ -627,8 +611,8 @@ def begin_deep_scan_for_scan(args: argparse.Namespace,) -> dict[str, Any]:scan_id = require_uuid(scan_id, "scan-id")- candidate = require_scan(connection, scan_id)- workspace = require_workspace(connection, candidate["workspace_id"])+ candidate = dependencies().require_scan(connection, scan_id)+ workspace = dependencies().require_workspace(connection, candidate["workspace_id"])if (candidate["mode"] == "deep"and candidate["status"] == "running"@@ -642,7 +626,7 @@ def begin_deep_scan_for_scan(args.claim_token,error_message="Deep Scan orchestration is owned by another continuation.",)- timestamp = now()+ timestamp = dependencies().now()with connection:claimed_workspace = connection.execute("UPDATE workspaces SET thread_id = ?, updated_at = ? "@@ -694,7 +678,7 @@ def begin_deep_scan_for_scan(args.claim_token,error_message="Deep Scan orchestration is owned by another continuation.",)- ensure_deep_scan_run(connection, scan, config, workflow_version, now())+ ensure_deep_scan_run(connection, scan, config, workflow_version, dependencies().now())connection.commit()except BaseException:connection.rollback()@@ -736,7 +720,9 @@ def begin_deep_scan_for_target(workflow_version = optional_text(args.workflow_version, maximum=256)if workflow_version is None:raise SystemExit("workflow-version is required.")- ensure_deep_scan_run(connection, existing, config, workflow_version, now())+ ensure_deep_scan_run(+ connection, existing, config, workflow_version, dependencies().now()+ )connection.commit()return deep_scan_result(connection,@@ -787,7 +773,7 @@ def begin_deep_scan_for_target(reasoning_effort = optional_text(args.reasoning_effort, maximum=32)workspace_id = str(uuid.uuid4())scan_id = str(uuid.uuid4())- timestamp = now()+ timestamp = dependencies().now()target_id = dependencies().ensure_security_target(connection, target_path)scan_dir = Path(tempfile.mkdtemp(@@ -857,7 +843,7 @@ def begin_deep_scan_for_target("UPDATE workspaces SET active_scan_id = ?, updated_at = ? WHERE id = ?",(scan_id, timestamp, workspace_id),)- scan = require_scan(connection, scan_id)+ scan = dependencies().require_scan(connection, scan_id)ensure_deep_scan_run(connection, scan, config, workflow_version, timestamp)connection.commit()except BaseException:@@ -953,7 +939,7 @@ def claim_deep_scan_coordinator_locked(error_message="Deep Scan orchestration is owned by another continuation.",)run, _ = require_running_deep_scan(connection, scan_id)- timestamp = now()+ timestamp = dependencies().now()if args.coordinator_generation is not None:require_current_coordinator(run, args)disposition = "claimed"@@ -1065,7 +1051,7 @@ def recover_expired_coordinator(def recover_candidate_ledger_publication(connection: sqlite3.Connection, scan_id: str) -> None:- scan = require_scan(connection, scan_id)+ scan = dependencies().require_scan(connection, scan_id)ledger = Path(scan["scan_dir"]) / "artifacts" / "02_discovery" / "candidate_ledger.jsonl"backups = sorted(ledger.parent.glob(f".{ledger.name}.*.backup"),@@ -1116,7 +1102,7 @@ def require_running_deep_scan(connection: sqlite3.Connection, scan_id: str) -> tuple[sqlite3.Row, sqlite3.Row]:run = require_deep_scan_run(connection, scan_id)- scan = require_scan(connection, run["scan_id"])+ scan = dependencies().require_scan(connection, run["scan_id"])if run["status"] != "running" or run["cancel_requested"]:raise SystemExit("Only a running Deep Scan can update orchestration state.")if scan["status"] != "running" or scan["canceled_at"] is not None:@@ -1145,7 +1131,7 @@ def upsert_deep_scan_worker(try:run = require_deep_scan_run(connection, scan_id)require_current_coordinator(run, args)- scan = require_scan(connection, scan_id)+ scan = dependencies().require_scan(connection, scan_id)existing = connection.execute("SELECT * FROM deep_scan_workers WHERE id = ?", (worker_id,)).fetchone()@@ -1188,7 +1174,7 @@ def upsert_deep_scan_worker(if args.result_manifest_pathelse None)- timestamp = now()+ timestamp = dependencies().now()if existing is None:if args.kind == "dedup":raise SystemExit("Create dedup workers with claim-deep-scan-dedup.")@@ -1469,7 +1455,7 @@ def claim_deep_scan_dedup(if minimum_inputs == 2else "A Deep Scan dedup requires at least one buffered discovery result.")- timestamp = now()+ timestamp = dependencies().now()connection.execute("""INSERT INTO deep_scan_workers (@@ -1533,7 +1519,7 @@ def commit_deep_scan_dedup_locked(try:run = require_deep_scan_run(connection, scan_id)require_current_coordinator(run, args)- scan = require_scan(connection, scan_id)+ scan = dependencies().require_scan(connection, scan_id)worker = require_deep_scan_worker(connection, worker_id)if worker["scan_id"] != scan_id or worker["kind"] != "dedup":raise SystemExit("Dedup worker does not belong to this Deep Scan.")@@ -1595,7 +1581,7 @@ def commit_deep_scan_dedup_locked(str(publication_copy),canonical_candidate_ledger_path,)- timestamp = now()+ timestamp = dependencies().now()connection.execute("""UPDATE deep_scan_workers@@ -1662,7 +1648,7 @@ def finish_deep_scan_locked(try:run = require_deep_scan_run(connection, scan_id)require_current_coordinator(run, args)- scan = require_scan(connection, scan_id)+ scan = dependencies().require_scan(connection, scan_id)manifest_path = (deep_scan_output_path(scan, args.manifest_path, "Deep Scan coordinator manifest path")if args.staged_manifest_path@@ -1727,7 +1713,7 @@ def finish_deep_scan_locked(AND reducers.status IN ('failed', 'canceled'))""",- (now(), scan_id, scan_id),+ (dependencies().now(), scan_id, scan_id),)buffered_worker_ids = [row["id"]@@ -1759,7 +1745,7 @@ def finish_deep_scan_locked(if run["manifest_path"] is None:connection.execute("UPDATE deep_scan_runs SET manifest_path = ?, updated_at = ? WHERE scan_id = ?",- (manifest_path, now(), scan_id),+ (manifest_path, dependencies().now(), scan_id),)connection.commit()return deep_scan_result(connection, scan_id)@@ -1853,7 +1839,7 @@ def finish_deep_scan_locked(if args.terminal_reason == "saturated":# Mark any remaining workers canceled, including those whose own# cancellation writes failed, so they cannot block completion.- cancel_active_workers(connection, scan_id, now())+ cancel_active_workers(connection, scan_id, dependencies().now())active_worker = connection.execute("""SELECT 1 FROM deep_scan_workers@@ -1893,7 +1879,7 @@ def finish_deep_scan_locked(kind="file",)promotion = promote_staged_file(staged_manifest_path, manifest_path)- timestamp = now()+ timestamp = dependencies().now()connection.execute("""UPDATE deep_scan_runs@@ -1932,7 +1918,7 @@ def fail_deep_scan_locked(try:run = require_deep_scan_run(connection, scan_id)require_current_coordinator(run, args)- scan = require_scan(connection, scan_id)+ scan = dependencies().require_scan(connection, scan_id)manifest_path = Noneif args.manifest_path:manifest_path = (@@ -1979,7 +1965,7 @@ def fail_deep_scan_locked(kind="file",)promotion = promote_staged_file(staged_manifest_path, manifest_path)- timestamp = now()+ timestamp = dependencies().now()connection.execute("""UPDATE deep_scan_runs@@ -2028,7 +2014,7 @@ def record_deep_scan_publication_failure(try:run = require_deep_scan_run(connection, scan_id)require_current_coordinator(run, args)- scan = require_scan(connection, scan_id)+ scan = dependencies().require_scan(connection, scan_id)if (run["status"] not in {"failed", "canceled", "interrupted"}or scan["status"] != "failed"@@ -2040,7 +2026,7 @@ def record_deep_scan_publication_failure(connection.commit()return deep_scan_result(connection, scan_id)if run["publication_error_message"] != message:- timestamp = now()+ timestamp = dependencies().now()connection.execute("""UPDATE deep_scan_runs@@ -2061,7 +2047,7 @@ def clear_deep_scan_publication_failure(connection: sqlite3.Connection, scan_id:connection.execute("UPDATE deep_scan_runs SET publication_error_message = NULL, updated_at = ? ""WHERE scan_id = ? AND publication_error_message IS NOT NULL",- (now(), scan_id),+ (dependencies().now(), scan_id),)diff --git a/plugins/codex-security/scripts/filesystem_identity.py b/plugins/codex-security/scripts/filesystem_identity.pyindex dea3d3cb5..91d6c5346 100644--- a/plugins/codex-security/scripts/filesystem_identity.py+++ b/plugins/codex-security/scripts/filesystem_identity.py@@ -22,9 +22,5 @@ def stored_filesystem_identity_matches(stored: object, current: int) -> bool:return stored == serialize_filesystem_identity(current)-def main() -> None:- argparse.ArgumentParser(description=__doc__).parse_args()--if __name__ == "__main__":- main()+ argparse.ArgumentParser(description=__doc__).parse_args()diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.pyindex 2c7181d0a..e89bd3d58 100644--- a/plugins/codex-security/scripts/finalize_scan_contract.py+++ b/plugins/codex-security/scripts/finalize_scan_contract.py@@ -352,10 +352,6 @@ def write_threat_model_projection_if_possible(return warning-def _validate_report_output_paths(scan_dir: Path) -> None:- _validate_scan_local_output_path(scan_dir, scan_dir / "report.md", "report.md")--def _json_bytes(payload: Any) -> bytes:try:encoded = json.dumps(payload, allow_nan=False, indent=2, sort_keys=True)@@ -406,22 +402,6 @@ def _require_safe_json_string(value: str, context: str) -> None:raise ContractError(f"{context}: expected well-formed Unicode JSON strings") from exc-def _sha256_text(value: str) -> str:- return hashlib.sha256(value.encode("utf-8")).hexdigest()---def _sha256_bytes(value: bytes) -> str:- return hashlib.sha256(value).hexdigest()---def _sha256_file(path: Path) -> str:- digest = hashlib.sha256()- with path.open("rb") as handle:- for chunk in iter(lambda: handle.read(1024 * 1024), b""):- digest.update(chunk)- return digest.hexdigest()--def _require_dict(payload: dict[str, Any], key: str, context: str) -> dict[str, Any]:value = payload.get(key)if not isinstance(value, dict):@@ -508,10 +488,6 @@ def _validate_scan_local_output_path(scan_dir: Path, path: Path, relative_path:raise ContractError(f"{relative_path}: expected a regular file")-def _descriptor_relative_reads_available() -> bool:- return os.open in os.supports_dir_fd and hasattr(os, "O_NOFOLLOW")--def _is_windows() -> bool:return os.name == "nt"@@ -610,7 +586,7 @@ def _open_scan_local_directory(root_fd: int, parts: tuple[str, ...], *, create:def open_scan_local_file_descriptor(scan_dir: Path, relative_path: str, context: str) -> int:scan_dir = _require_scan_directory(scan_dir)relative_path = _require_portable_relative_path(relative_path, context)- if not _descriptor_relative_reads_available():+ if not (os.open in os.supports_dir_fd and hasattr(os, "O_NOFOLLOW")):if not _is_windows():raise ContractError("scan-local input requires descriptor-relative file operations")try:@@ -915,11 +891,7 @@ def _validate_target(target: dict[str, Any]) -> None:_validate_remote(remote, "scan.target.remote")if kind == "git_revision":_require_str(target, "revision", "scan.target")- elif kind == "git_worktree":- _require_str(target, "snapshotDigest", "scan.target")- elif kind == "git_diff":- _require_str(target, "snapshotDigest", "scan.target")- elif kind == "directory_snapshot":+ elif kind in ("git_worktree", "git_diff", "directory_snapshot"):_require_str(target, "snapshotDigest", "scan.target")@@ -937,11 +909,11 @@ def _fingerprint(target_id: str, finding: dict[str, Any]) -> str:if not SLUG_RE.fullmatch(rule_id):raise ContractError("finding.ruleId: expected a stable lowercase rule slug")material = "\0".join((FINGERPRINT_ALGORITHM, target_id, rule_id, anchor, instance))- return f"{FINGERPRINT_ALGORITHM}:sha256:{_sha256_text(material)}"+ return f"{FINGERPRINT_ALGORITHM}:sha256:{hashlib.sha256(material.encode('utf-8')).hexdigest()}"def _stable_id(prefix: str, *parts: str) -> str:- return f"{prefix}_{_sha256_text(chr(0).join(parts))[:24]}"+ return f"{prefix}_{hashlib.sha256(chr(0).join(parts).encode('utf-8')).hexdigest()[:24]}"def _validate_location(location: dict[str, Any], context: str) -> None:@@ -2582,7 +2554,7 @@ def _artifact_record("mediaType": media_type,"path": relative_path,"sha256": (- _sha256_bytes(contents)+ hashlib.sha256(contents).hexdigest()if contents is not Noneelse _sha256_scan_local_file(scan_dir, relative_path, relative_path)),@@ -2633,7 +2605,7 @@ def _validate_existing_seal(expected_sha256 = _require_str(artifact, "sha256", context)contents = (artifact_contents or {}).get(path)actual_sha256 = (- _sha256_bytes(contents)+ hashlib.sha256(contents).hexdigest()if contents is not Noneelse _sha256_scan_local_file(scan_dir, path, context))@@ -3024,12 +2996,12 @@ def _prepare_scan_finalization(_validate_manifest(manifest)validate_against_schema(manifest, schema_dir / "scan-manifest.schema.json")report_markdown_bytes = _generate_report_projection(manifest, findings, coverage)- _validate_report_output_paths(scan_dir)+ _validate_scan_local_output_path(scan_dir, scan_dir / "report.md", "report.md")else:findings_bytes = _contract_json_bytes("findings.json", findings)coverage_bytes = _contract_json_bytes("coverage.json", coverage)report_markdown_bytes = _generate_report_projection(manifest, findings, coverage)- _validate_report_output_paths(scan_dir)+ _validate_scan_local_output_path(scan_dir, scan_dir / "report.md", "report.md")scan["artifacts"] = [_artifact_record(scan_dir, "findings.json", "application/json", findings_bytes),_artifact_record(scan_dir, "coverage.json", "application/json", coverage_bytes),diff --git a/plugins/codex-security/scripts/generate_rank_input.py b/plugins/codex-security/scripts/generate_rank_input.pyindex 02b1f5924..b589cb061 100644--- a/plugins/codex-security/scripts/generate_rank_input.py+++ b/plugins/codex-security/scripts/generate_rank_input.py@@ -32,11 +32,12 @@ import reimport subprocessimport sysfrom collections import Counter-from collections.abc import Callable+from collections.abc import Callable, KeysViewfrom pathlib import Path# Some plugin hosts launch Python with safe-path isolation enabled.sys.path.insert(0, str(Path(__file__).resolve().parent))+from generate_in_scope_files import windows_stream_componentfrom rank_preview import (DEFAULT_PREVIEW_BYTES,TEXT_CODE_EXTENSIONS,@@ -129,7 +130,6 @@ RANK_POOL_STRATEGY = "round_robin"RANK_POOL_WORKER_CAP = 6JsonRow = dict[str, object]RowValidator = Callable[[JsonRow, Path, int], None]-RankWorkerAssignment = tuple[int, list[str], list[str]]def parse_args() -> argparse.Namespace:@@ -299,17 +299,6 @@ def path_is_diff_excluded(path: Path) -> bool:return path_is_excluded(path)-def windows_stream_component(path: Path) -> str | None:- """Return the first NTFS alternate-data-stream component."""-- if os.name != "nt":- return None- return next(- (component for component in path.parts if component != path.anchor and ":" in component),- None,- )--def resolve_scope(repo: Path,scope: str,@@ -394,12 +383,11 @@ def load_jsonl(path: Path, label: str, validator: RowValidator) -> list[JsonRow]if not raw_line.strip():raise SystemExit(f"{path}:{line_number}: blank JSONL rows are not allowed")try:- parsed: object = json.loads(raw_line)+ row: object = json.loads(raw_line)except json.JSONDecodeError as exc:raise SystemExit(f"{path}:{line_number}: invalid JSON: {exc.msg}") from exc- if not isinstance(parsed, dict):+ if not isinstance(row, dict):raise SystemExit(f"{path}:{line_number}: expected a JSON object")- row = {str(key): value for key, value in parsed.items()}validator(row, path, line_number)rows.append(row)return rows@@ -448,16 +436,12 @@ def validate_rank_output_row(row: JsonRow, path: Path, line_number: int) -> Nonerequire_string(row, "reason", path, line_number, allow_empty=False)-def require_unique_paths(rows: list[JsonRow], label: str) -> None:- seen: set[str] = set()- duplicates: set[str] = set()- for row in rows:- path = str(row["path"])- if path in seen:- duplicates.add(path)- seen.add(path)- if duplicates:- raise SystemExit(f"{label} contains duplicate paths: {sorted(duplicates)}")+def require_unique_paths(rows: list[JsonRow], label: str) -> KeysView[str]:+ counts = Counter(str(row["path"]) for row in rows)+ if len(counts) < len(rows):+ duplicates = sorted(path for path, count in counts.items() if count > 1)+ raise SystemExit(f"{label} contains duplicate paths: {duplicates}")+ return counts.keys()def make_repo_rank_input(args: argparse.Namespace) -> None:@@ -710,9 +694,8 @@ def make_diff_rank_input(args: argparse.Namespace) -> None:for path, status in changed:rel = path.relative_to(repo)- if status == "D":- preview = ""- elif args.mode == "revisions":+ preview = ""+ if status != "D" and args.mode == "revisions":content = revision_blobs[rel]if content is None:raise SystemExit(@@ -721,9 +704,7 @@ def make_diff_rank_input(args: argparse.Namespace) -> None:preview, is_binary = preview_for_bytes(rel, content, args.preview_bytes)if is_binary:continue- elif path.is_symlink():- preview = ""- elif path.is_file():+ elif status != "D" and not path.is_symlink() and path.is_file():try:path.resolve(strict=True).relative_to(repo)except (OSError, ValueError):@@ -732,8 +713,6 @@ def make_diff_rank_input(args: argparse.Namespace) -> None:preview, is_binary = preview_for(path, args.preview_bytes)if is_binary:continue- else:- preview = ""rows.append({"path": rel.as_posix(), "area": args.area, "preview": preview})rows.sort(key=lambda row: str(row["path"]))@@ -757,8 +736,7 @@ def make_rank_shards(args: argparse.Namespace) -> None:raise SystemExit(f"Rank shard directory already contains shard files: {output_dir}")shard_count = 0- for start in range(0, len(rows), args.max_rows):- shard_count += 1+ for shard_count, start in enumerate(range(0, len(rows), args.max_rows), start=1):shard_path = output_dir / f"rank-shard-{shard_count:04d}.input.jsonl"write_jsonl(shard_path, rows[start : start + args.max_rows])@@ -858,7 +836,7 @@ def load_rank_pool_plan(plan_path: Path) -> tuple[dict[str, object], bytes]:raise SystemExit(f"{plan_path}: invalid JSON: {exc.msg}") from excif not isinstance(payload, dict):raise SystemExit(f"{plan_path}: expected a JSON object")- return {str(key): value for key, value in payload.items()}, plan_bytes+ return payload, plan_bytesdef require_integer(value: object, label: str, *, minimum: int) -> int:@@ -875,24 +853,13 @@ def require_string_list(value: object, label: str) -> list[str]:return [item for item in value if isinstance(item, str)]-def assignment_differences(- assigned_names: list[str], expected_names: list[str]-) -> tuple[list[str], list[str], list[str]]:- counts = Counter(assigned_names)- duplicates = sorted(name for name, count in counts.items() if count > 1)- assigned = set(assigned_names)- expected = set(expected_names)- return sorted(expected - assigned), duplicates, sorted(assigned - expected)--def validate_rank_pool_plan(plan_path: Path, shard_dir: Path-) -> tuple[list[Path], list[str], list[RankWorkerAssignment], bytes]:+) -> tuple[list[Path], list[tuple[list[str], list[str]]], bytes]:require_plan_shard_dir(plan_path, shard_dir)require_no_misplaced_rank_shards(plan_path)input_shards = discover_input_shards(shard_dir)input_names = [path.name for path in input_shards]- output_names = [output_name_for(name) for name in input_names]plan, plan_bytes = load_rank_pool_plan(plan_path)expected_fields = {"schema_version",@@ -940,15 +907,13 @@ def validate_rank_pool_plan(f"{plan_path}: workers must contain exactly {worker_count} worker assignments")- assigned_inputs: list[str] = []- assigned_outputs: list[str] = []- parsed_workers: list[RankWorkerAssignment] = []+ counts: Counter[str] = Counter()+ parsed_workers: list[tuple[list[str], list[str]]] = []worker_fields = {"slot", "input_shards", "output_shards"}- for worker_index, raw_worker in enumerate(workers):+ for worker_index, worker in enumerate(workers):label = f"{plan_path}: workers[{worker_index}]"- if not isinstance(raw_worker, dict):+ if not isinstance(worker, dict):raise SystemExit(f"{label} must be a JSON object")- worker = {str(key): value for key, value in raw_worker.items()}if set(worker) != worker_fields:raise SystemExit(f"{label} fields do not match schema; "@@ -965,47 +930,40 @@ def validate_rank_pool_plan(expected_worker_outputs = [output_name_for(name) for name in worker_inputs]if worker_outputs != expected_worker_outputs:raise SystemExit(f"{label}.output_shards do not match its input_shards")- assigned_inputs.extend(worker_inputs)- assigned_outputs.extend(worker_outputs)- parsed_workers.append((slot, worker_inputs, worker_outputs))+ counts.update(worker_inputs)+ parsed_workers.append((worker_inputs, worker_outputs))- missing, duplicates, unexpected = assignment_differences(assigned_inputs, input_names)+ # Canonical input names map one-to-one to the per-worker outputs checked above.+ duplicates = sorted(name for name, count in counts.items() if count > 1)+ expected = set(input_names)+ missing = sorted(expected - counts.keys())+ unexpected = sorted(counts.keys() - expected)if missing or duplicates or unexpected:raise SystemExit(f"{plan_path}: pool plan must assign each input shard exactly once; "f"missing={missing}; duplicates={duplicates}; unexpected={unexpected}")- missing, duplicates, unexpected = assignment_differences(assigned_outputs, output_names)- if missing or duplicates or unexpected:- raise SystemExit(- f"{plan_path}: pool plan must assign each output shard exactly once; "- f"missing={missing}; duplicates={duplicates}; unexpected={unexpected}"- )-- for worker_index, (_, worker_inputs, worker_outputs) in enumerate(parsed_workers):+ for worker_index, (worker_inputs, _) in enumerate(parsed_workers):expected_inputs = input_names[worker_index::worker_count]- expected_outputs = output_names[worker_index::worker_count]- if worker_inputs != expected_inputs or worker_outputs != expected_outputs:+ if worker_inputs != expected_inputs:raise SystemExit(f"{plan_path}: worker slot {worker_index + 1} does not match the deterministic "f"{RANK_POOL_STRATEGY} assignment")- return input_shards, output_names, parsed_workers, plan_bytes+ return input_shards, parsed_workers, plan_bytesdef validate_rank_worker_command(args: argparse.Namespace) -> None:plan_path = Path(args.plan).expanduser()shard_dir = Path(args.shard_dir).expanduser()- _, _, workers, plan_bytes = validate_rank_pool_plan(plan_path, shard_dir)+ _, workers, plan_bytes = validate_rank_pool_plan(plan_path, shard_dir)slot = require_integer(args.slot, "--slot", minimum=1)worker_count = len(workers)if slot > worker_count:raise SystemExit(f"--slot must be at most {worker_count}")- assigned_slot, input_names, output_names = workers[slot - 1]- if assigned_slot != slot:- raise SystemExit(f"{plan_path}: worker assignment for slot {slot} is inconsistent")+ input_names, output_names = workers[slot - 1]row_count = 0outputs_digest = hashlib.sha256()@@ -1036,10 +994,10 @@ def validate_rank_worker_command(args: argparse.Namespace) -> None:def validate_rank_pool_command(args: argparse.Namespace) -> None:plan_path = Path(args.plan).expanduser()shard_dir = Path(args.shard_dir).expanduser()- input_shards, expected_output_names, workers, _ = validate_rank_pool_plan(plan_path, shard_dir)+ input_shards, workers, _ = validate_rank_pool_plan(plan_path, shard_dir)actual_output_names = {path.name for path in shard_dir.glob(SHARD_OUTPUT_GLOB)}- expected_outputs = set(expected_output_names)+ expected_outputs = {output_name_for(path.name) for path in input_shards}if actual_output_names != expected_outputs:missing = sorted(expected_outputs - actual_output_names)unexpected = sorted(actual_output_names - expected_outputs)@@ -1063,12 +1021,10 @@ def validate_rank_shard(input_shard: Path, output_shard: Path) -> tuple[list[JsonRow], list[JsonRow]]:shard_inputs = load_jsonl(input_shard, "Rank input shard", validate_rank_input_row)- require_unique_paths(shard_inputs, f"Rank input shard {input_shard.name}")+ expected_paths = require_unique_paths(shard_inputs, f"Rank input shard {input_shard.name}")shard_outputs = load_jsonl(output_shard, "Rank output shard", validate_rank_output_row)- require_unique_paths(shard_outputs, f"Rank output shard {output_shard.name}")+ actual_paths = require_unique_paths(shard_outputs, f"Rank output shard {output_shard.name}")- expected_paths = {str(row["path"]) for row in shard_inputs}- actual_paths = {str(row["path"]) for row in shard_outputs}if expected_paths != actual_paths:missing = sorted(expected_paths - actual_paths)unknown = sorted(actual_paths - expected_paths)diff --git a/plugins/codex-security/scripts/rank_preview.py b/plugins/codex-security/scripts/rank_preview.pyindex 76084a998..95e54e4b6 100644--- a/plugins/codex-security/scripts/rank_preview.py+++ b/plugins/codex-security/scripts/rank_preview.py@@ -679,8 +679,7 @@ def strip_leading_annotations(original: str, masked: str) -> tuple[str, str, lisdef brace_language_outline(text: str, suffix: str) -> list[str]:original_lines = text.splitlines()masked_lines = mask_c_style_source(text, suffix).splitlines()- outline: list[str] = []- seen: set[str] = set()+ outline: dict[str, None] = {}type_stack: list[tuple[str, int]] = []function_depths: list[int] = []blocked_depths: list[int] = []@@ -691,9 +690,7 @@ def brace_language_outline(text: str, suffix: str) -> list[str]:def add(value: str) -> None:decorated = f"{' '.join(pending_annotations[-2:])} {value}".strip()- if decorated not in seen:- seen.add(decorated)- outline.append(decorated)+ outline[decorated] = Nonedef next_lines_open_body(line_index: int) -> bool:inspected = 0@@ -805,20 +802,14 @@ def brace_language_outline(text: str, suffix: str) -> list[str]:type_stack.pop()while blocked_depths and depth < blocked_depths[-1]:blocked_depths.pop()- return outline+ return list(outline)def ruby_outline(text: str) -> list[str]:- outline: list[str] = []- seen: set[str] = set()+ outline: dict[str, None] = {}type_stack: list[tuple[int, str]] = []function_indents: list[int] = []- def add(value: str) -> None:- if value not in seen:- seen.add(value)- outline.append(value)-for raw_line in text.splitlines():stripped = raw_line.strip()if not stripped or stripped.startswith("#"):@@ -832,7 +823,7 @@ def ruby_outline(text: str) -> list[str]:type_match = re.match(r"^(class|module)\s+([A-Z]\w*(?:::[A-Z]\w*)*)", stripped)if type_match and not function_indents:kind, name = type_match.groups()- add(f"{kind} {name}")+ outline[f"{kind} {name}"] = Nonetype_stack.append((indent, name))continue@@ -840,21 +831,15 @@ def ruby_outline(text: str) -> list[str]:if function_match and not function_indents:name = function_match.group(1)if type_stack:- add(f"method {type_stack[-1][1]}.{name}")+ outline[f"method {type_stack[-1][1]}.{name}"] = Noneelse:- add(f"function {name}")+ outline[f"function {name}"] = Nonefunction_indents.append(indent)- return outline+ return list(outline)def simple_language_outline(text: str, suffix: str) -> list[str]:- outline: list[str] = []- seen: set[str] = set()-- def add(value: str) -> None:- if value not in seen:- seen.add(value)- outline.append(value)+ outline: dict[str, None] = {}for raw_line in text.splitlines():line = compact_preview_line(raw_line)@@ -864,61 +849,63 @@ def simple_language_outline(text: str, suffix: str) -> list[str]:if suffix == ".py":match = re.match(r"^(async\s+)?def\s+([A-Za-z_]\w*)\s*\(", line)if match:- add(f"{'async function' if match.group(1) else 'function'} {match.group(2)}")+ outline[+ f"{'async function' if match.group(1) else 'function'} {match.group(2)}"+ ] = Nonecontinuematch = re.match(r"^class\s+([A-Za-z_]\w*)", line)if match:- add(f"class {match.group(1)}")+ outline[f"class {match.group(1)}"] = Noneelif suffix in {".ex", ".exs"}:match = re.match(r"^defmodule\s+([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*)", line)if match:- add(f"module {match.group(1)}")+ outline[f"module {match.group(1)}"] = Nonecontinuematch = re.match(r"^(?:def|defp|defmacro)\s+([A-Za-z_]\w*[!?]?)", line)if match:- add(f"function {match.group(1)}")+ outline[f"function {match.group(1)}"] = Noneelif suffix == ".clj":match = re.match(r"^\((defn|defmacro|defprotocol|defrecord|deftype|defmulti|defmethod)\s+([^\s)]+)",line,)if match:- add(f"{match.group(1)} {match.group(2)}")+ outline[f"{match.group(1)} {match.group(2)}"] = Noneelif suffix == ".lua":match = re.match(r"^(?:local\s+)?function\s+([A-Za-z_]\w*(?:[.:][A-Za-z_]\w*)*)", line)if match:- add(f"function {match.group(1)}")+ outline[f"function {match.group(1)}"] = Noneelif suffix == ".sh":match = re.match(r"^(?:function\s+)?([A-Za-z_]\w*)\s*\(\s*\)\s*\{?", line)if match:- add(f"function {match.group(1)}")+ outline[f"function {match.group(1)}"] = Noneelif suffix == ".hs":match = re.match(r"^(data|newtype|type|class)\s+([A-Z]\w*)", line)if match:- add(f"{match.group(1)} {match.group(2)}")+ outline[f"{match.group(1)} {match.group(2)}"] = Nonecontinuematch = re.match(r"^([a-z_]\w*)\s*::", line)if match:- add(f"function {match.group(1)}")+ outline[f"function {match.group(1)}"] = Noneelif suffix == ".proto":match = re.match(r"^(message|service|enum)\s+([A-Za-z_]\w*)", line)if match:- add(f"{match.group(1)} {match.group(2)}")+ outline[f"{match.group(1)} {match.group(2)}"] = Nonecontinuematch = re.match(r"^rpc\s+([A-Za-z_]\w*)\s*\(", line)if match:- add(f"rpc {match.group(1)}")+ outline[f"rpc {match.group(1)}"] = Noneelif suffix == ".graphql":match = re.match(r"^(type|interface|input|enum|union|scalar|directive)\s+([A-Za-z_]\w*)",line,)if match:- add(f"{match.group(1)} {match.group(2)}")+ outline[f"{match.group(1)} {match.group(2)}"] = Nonecontinuematch = re.match(r"^(query|mutation|subscription)\s+([A-Za-z_]\w*)", line)if match:- add(f"{match.group(1)} {match.group(2)}")+ outline[f"{match.group(1)} {match.group(2)}"] = Noneelif suffix == ".sql":match = re.match(r"^CREATE\s+(?:OR\s+REPLACE\s+)?(FUNCTION|PROCEDURE|TABLE|VIEW|TRIGGER)\s+"@@ -927,16 +914,16 @@ def simple_language_outline(text: str, suffix: str) -> list[str]:flags=re.IGNORECASE,)if match:- add(f"{match.group(1).lower()} {match.group(2)}")+ outline[f"{match.group(1).lower()} {match.group(2)}"] = Noneelif suffix in {".yaml", ".yml"}:match = re.match(r"^([A-Za-z0-9_.-]+):(?:\s|$)", raw_line)if match:- add(f"key {match.group(1)}")+ outline[f"key {match.group(1)}"] = Noneelif suffix in {".toml", ".cfg"}:match = re.match(r"^\[([^]]+)]$", line)if match:- add(f"section {match.group(1)}")- return outline+ outline[f"section {match.group(1)}"] = None+ return list(outline)def json_outline(text: str) -> list[str]:@@ -996,12 +983,8 @@ def preview_for_bytes(path: Path, data: bytes, preview_bytes: int) -> tuple[str,return fit_preview_lines(preview_lines, preview_bytes), False-def main() -> None:+if __name__ == "__main__":parser = argparse.ArgumentParser(description="Build structural or sampled previews for Codex Security rank inputs.")parser.parse_args()---if __name__ == "__main__":- main()diff --git a/plugins/codex-security/scripts/report_projection.py b/plugins/codex-security/scripts/report_projection.pyindex de1f1e093..d230ac173 100644--- a/plugins/codex-security/scripts/report_projection.py+++ b/plugins/codex-security/scripts/report_projection.py@@ -63,10 +63,6 @@ def _cell(value: Any) -> str:return _text(value, "none").replace("|", "\\|").replace("\n", "<br>")-def _link_label(value: Any, fallback: str) -> str:- return _cell(value) or _cell(fallback)--def _deep_report_id(finding: dict[str, Any]) -> str:extensions = finding.get("extensions")if isinstance(extensions, dict):@@ -141,10 +137,6 @@ def _deep_title_parts(finding: dict[str, Any]) -> tuple[str, str | None]:return normalized, None-def _deep_finding_title(finding: dict[str, Any]) -> str:- return _deep_title_parts(finding)[0]--def _deep_finding_groups(findings: list[dict[str, Any]], writeup_paths: list[str | None]) -> list[list[tuple[int, dict[str, Any], str | None]]]:@@ -157,7 +149,7 @@ def _deep_finding_groups(def _deep_group_titles(group: list[tuple[int, dict[str, Any], str | None]]) -> str:titles: list[str] = []for _, finding, _ in group:- title = _cell(_deep_finding_title(finding))+ title = _cell(_deep_title_parts(finding)[0])if title not in titles:titles.append(title)return "<br>".join(titles)@@ -197,7 +189,7 @@ def _deep_group_report_labels(def _deep_group_report_links(group: list[tuple[int, dict[str, Any], str | None]]) -> str:labels = _deep_group_report_labels(group)return "<br>".join(- f"[{_link_label(label, 'Unidentified report')}](#finding-{number})"+ f"[{_cell(label)}](#finding-{number})"for label, (number, _, _) in zip(labels, group, strict=True))@@ -206,7 +198,7 @@ def _deep_group_writeup_links(group: list[tuple[int, dict[str, Any], str | None]labels = _deep_group_report_labels(group)links: list[str] = []for label, (_, _, report_path) in zip(labels, group, strict=True):- report_id = _link_label(label, "Unidentified report")+ report_id = _cell(label)links.append(f"[Open {report_id}]({report_path})" if report_path else f"{report_id}: inline below")@@ -282,15 +274,11 @@ def _section_code_evidence(and isinstance(item.get("code"), str)and item["code"].strip())- unique: list[dict[str, Any]] = []- seen: set[tuple[str, str]] = set()+ unique: dict[tuple[str, str], dict[str, Any]] = {}for item in resolved:key = (str(item.get("id", "")), item["code"])- if key in seen:- continue- seen.add(key)- unique.append(item)- return unique+ unique.setdefault(key, item)+ return list(unique.values())def merged_root_cause(value: dict[str, Any]) -> tuple[str | None, Any]:@@ -919,7 +907,7 @@ def build_report_markdown(for number, (finding, report_path) in enumerate(zip(findings, writeup_paths, strict=True), 1):- title = _link_label(finding["title"], "Untitled finding")+ title = _cell(finding["title"])finding_link = f"[{title}](#finding-{number})"writeup_link = f"[Open report]({report_path})" if report_path else "inline below"lines.append(@@ -1084,11 +1072,7 @@ def generate_report_markdown(return build_report_markdown(manifest, findings, coverage).encode("utf-8")-def main() -> int:+if __name__ == "__main__":parser = argparse.ArgumentParser(description=__doc__)parser.parse_args()- return 0---if __name__ == "__main__":- raise SystemExit(main())+ raise SystemExit(0)diff --git a/plugins/codex-security/scripts/windows_scan_local_files.py b/plugins/codex-security/scripts/windows_scan_local_files.pyindex c67c86eb6..3375178ef 100644--- a/plugins/codex-security/scripts/windows_scan_local_files.py+++ b/plugins/codex-security/scripts/windows_scan_local_files.py@@ -686,9 +686,5 @@ def unlink_if_exists(scan_dir: Path, relative_path: str) -> None:_mark_handle_for_deletion(handle.value)-def main() -> None:- argparse.ArgumentParser(description=__doc__).parse_args()--if __name__ == "__main__":- main()+ argparse.ArgumentParser(description=__doc__).parse_args()diff --git a/plugins/codex-security/scripts/workbench_cli.py b/plugins/codex-security/scripts/workbench_cli.pyindex 18d21d9b4..ab38dd9de 100644--- a/plugins/codex-security/scripts/workbench_cli.py+++ b/plugins/codex-security/scripts/workbench_cli.py@@ -22,6 +22,7 @@ from workbench_constants import (PHASE_PROGRESS_UNITS,PHASES,REMEDIATION_UPDATE_STATES,+ positive_int,)@@ -400,12 +401,5 @@ def parse_args(description: str) -> argparse.Namespace:return parser.parse_args(arguments)-def positive_int(value: str) -> int:- parsed = int(value)- if parsed < 1:- raise argparse.ArgumentTypeError("expected a positive integer")- return parsed--if __name__ == "__main__":parse_args(__doc__)diff --git a/plugins/codex-security/scripts/workbench_constants.py b/plugins/codex-security/scripts/workbench_constants.pyindex 2019115f8..fc0a19c3c 100644--- a/plugins/codex-security/scripts/workbench_constants.py+++ b/plugins/codex-security/scripts/workbench_constants.py@@ -70,9 +70,12 @@ GIT_REPOSITORY_ENVIRONMENT = (EMPTY_GIT_TREE = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"-def main() -> None:- argparse.ArgumentParser(description=__doc__).parse_args()+def positive_int(value: str) -> int:+ parsed = int(value)+ if parsed < 1:+ raise argparse.ArgumentTypeError("expected a positive integer")+ return parsedif __name__ == "__main__":- main()+ argparse.ArgumentParser(description=__doc__).parse_args()diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.pyindex 05b6418c3..c1d08a3c5 100644--- a/plugins/codex-security/scripts/workbench_db.py+++ b/plugins/codex-security/scripts/workbench_db.py@@ -126,7 +126,7 @@ from workbench_target import (git_command,git_output,git_revision,- git_submodule_paths,+ git_submodule_entries,git_target_metadata,git_worktree_context,remediation_checkout_snapshot,@@ -399,18 +399,6 @@ def inspect_setup_values(}-def inspect_setup(args: argparse.Namespace) -> dict[str, Any]:- return inspect_setup_values(- args.target_path,- args.scope,- args.mode,- args.diff_target_kind,- args.diff_base_revision,- args.diff_head_revision,- args.diff_content_digest,- )--def require_review_changes_target(target: Path) -> str:revision = require_git_worktree_head(target)repository_root = git_output(target, "rev-parse", "--show-toplevel")@@ -922,18 +910,6 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dictreturn workspace_state(connection, workspace["id"])-def start_prompt_only_scan(- connection: sqlite3.Connection, args: argparse.Namespace-) -> dict[str, Any]:- return _start_prompt_driven_scan(connection, args, headless_standard=False)---def start_headless_standard_scan(- connection: sqlite3.Connection, args: argparse.Namespace-) -> dict[str, Any]:- return _start_prompt_driven_scan(connection, args, headless_standard=True)--def _start_prompt_driven_scan(connection: sqlite3.Connection, args: argparse.Namespace, *, headless_standard: bool) -> dict[str, Any]:@@ -1878,10 +1854,6 @@ def recover_scan_results(return saved_results.recover_scan_results(_WORKBENCH_DB_CONTEXT, connection, args)-def write_scan_draft(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:- return saved_results.write_scan_draft(_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)@@ -2450,7 +2422,7 @@ def require_reviewed_patch_applied(git_dir = git_output(target, "rev-parse", "--absolute-git-dir")if git_dir is None:raise SystemExit("Could not inspect the selected Git working tree.")- excluded += git_submodule_paths(target)+ excluded += tuple(path for path, _ in git_submodule_entries(target))with tempfile.TemporaryDirectory(prefix="codex-security-remediation-") as temporary:reviewed_patch = Path(temporary) / "reviewed.patch"digest = hashlib.sha256()@@ -3079,13 +3051,10 @@ def finding_artifact_paths(scan_dir: Path, details: dict[str, Any]) -> list[str]except OSError:return artifacts- directories_seen = 0- for current_directory, directory_names, file_names in os.walk(- poc_root, topdown=True, followlinks=False+ for directories_seen, (current_directory, directory_names, file_names) in enumerate(+ os.walk(poc_root, topdown=True, followlinks=False), start=1):- directories_seen += 1if directories_seen > FINDING_ARTIFACT_DIRECTORIES_LIMIT:- directory_names[:] = []breakcurrent_path = Path(current_directory)directory_names[:] = [@@ -3366,7 +3335,15 @@ def main() -> None:print(json.dumps(result, allow_nan=False, sort_keys=True))returnif args.command == "inspect-setup":- result = inspect_setup(args)+ result = inspect_setup_values(+ args.target_path,+ args.scope,+ args.mode,+ args.diff_target_kind,+ args.diff_base_revision,+ args.diff_head_revision,+ args.diff_content_digest,+ )print(json.dumps(result, allow_nan=False, sort_keys=True))returnif args.command in {"save-artifact", "read-artifact"}:@@ -3395,9 +3372,9 @@ def main() -> None:elif args.command == "start-scan":result = start_scan(connection, args)elif args.command == "start-prompt-only-scan":- result = start_prompt_only_scan(connection, args)+ result = _start_prompt_driven_scan(connection, args, headless_standard=False)elif args.command == "start-headless-standard-scan":- result = start_headless_standard_scan(connection, args)+ result = _start_prompt_driven_scan(connection, args, headless_standard=True)elif args.command == "begin-deep-scan":result = deep_scan.begin_deep_scan(connection, args)elif args.command == "get-deep-scan":@@ -3498,7 +3475,7 @@ def main() -> None:elif args.command == "recover-scan-results":result = recover_scan_results(connection, args)elif args.command == "write-scan-draft":- result = write_scan_draft(connection, args)+ result = saved_results.write_scan_draft(_WORKBENCH_DB_CONTEXT, connection, args)elif args.command == "save-scan-artifact":result = saved_results.save_scan_artifact(_WORKBENCH_DB_CONTEXT, connection, args)elif args.command == "mark-handoff-delivered":diff --git a/plugins/codex-security/scripts/workbench_native_indexes.py b/plugins/codex-security/scripts/workbench_native_indexes.pyindex d3d458b3d..71291b280 100644--- a/plugins/codex-security/scripts/workbench_native_indexes.py+++ b/plugins/codex-security/scripts/workbench_native_indexes.py@@ -192,10 +192,7 @@ def list_repositories() -> dict[str, Any]:scans = scan_history.list_scans(connection)["scans"]scans_by_id = {scan["scanId"]: scan for scan in scans}- scan_count_by_target: dict[str, int] = {}- for scan in scans:- target_id = scan["targetId"]- scan_count_by_target[target_id] = scan_count_by_target.get(target_id, 0) + 1+ scan_count_by_target = dict(Counter(scan["targetId"] for scan in scans))latest_scan_by_target: dict[str, dict[str, Any]] = {}for row in connection.execute(diff --git a/plugins/codex-security/scripts/workbench_progress.py b/plugins/codex-security/scripts/workbench_progress.pyindex fb1b5303c..7793abf4d 100644--- a/plugins/codex-security/scripts/workbench_progress.py+++ b/plugins/codex-security/scripts/workbench_progress.py@@ -16,15 +16,12 @@ from workbench_validation import optional_text, require_uuid, user_context_argumMAX_PREFLIGHT_ISSUES = 32-def _javascript_string_length(value: str) -> int:- return len(value.encode("utf-16-le", errors="surrogatepass")) // 2--def _preflight_issue_text(value: Any, maximum: int, label: str) -> str:if not isinstance(value, str):raise SystemExit(f"Preflight issue {label} must be text.")normalized = value.strip()- if not normalized or _javascript_string_length(normalized) > maximum:+ # Match JavaScript's UTF-16 code-unit length.+ if not normalized or len(normalized.encode("utf-16-le", errors="surrogatepass")) // 2 > maximum:raise SystemExit(f"Preflight issue {label} must contain 1 to {maximum} characters.")return normalizeddiff --git a/plugins/codex-security/scripts/workbench_remediation.py b/plugins/codex-security/scripts/workbench_remediation.pyindex 275eb683a..2f5870d5c 100644--- a/plugins/codex-security/scripts/workbench_remediation.py+++ b/plugins/codex-security/scripts/workbench_remediation.py@@ -193,9 +193,5 @@ def _cancel_generation()-def main() -> None:- argparse.ArgumentParser(description=__doc__).parse_args()--if __name__ == "__main__":- main()+ argparse.ArgumentParser(description=__doc__).parse_args()diff --git a/plugins/codex-security/scripts/workbench_scan_start.py b/plugins/codex-security/scripts/workbench_scan_start.pyindex 7cb1f6217..b31f2543e 100644--- a/plugins/codex-security/scripts/workbench_scan_start.py+++ b/plugins/codex-security/scripts/workbench_scan_start.py@@ -22,7 +22,7 @@ from workbench_target import (git_revision,worktree_content_digest,)-from workbench_validation import optional_text, user_text+from workbench_validation import optional_textdef safe_segment(value: str) -> str:@@ -168,7 +168,7 @@ def insert_running_scan() -> str:revision = target_identity[0]native_scan = scan_dir is None- user_context = user_text(workspace["user_context"])+ user_context = optional_text(workspace["user_context"])if scan_dir is None:scan_dir = Path(tempfile.mkdtemp(@@ -236,9 +236,5 @@ def insert_running_scan(return scan_id-def main() -> None:- argparse.ArgumentParser(description=__doc__).parse_args()--if __name__ == "__main__":- main()+ argparse.ArgumentParser(description=__doc__).parse_args()diff --git a/plugins/codex-security/scripts/workbench_scan_usage.py b/plugins/codex-security/scripts/workbench_scan_usage.pyindex 648924d92..d3e437e81 100644--- a/plugins/codex-security/scripts/workbench_scan_usage.py+++ b/plugins/codex-security/scripts/workbench_scan_usage.py@@ -117,7 +117,6 @@ def collect_scan_usage(return _unavailable_usage("scan_thread_unavailable", warnings=warnings)total = _empty_token_usage()- observed_thread_count = 0accepted_thread_ids: set[str] = set()excluded_thread_ids: set[str] = set()for session in sessions:@@ -151,17 +150,16 @@ def collect_scan_usage(missing_thread_ids.add(session.thread_id)continueaccepted_thread_ids.add(session.thread_id)- observed_thread_count += 1_add_token_usage(total, session_usage)- if not observed_thread_count:+ if not accepted_thread_ids:return _unavailable_usage("scan_thread_unavailable", warnings=warnings)result: dict[str, Any] = {"coverage": "partial" if missing_thread_ids or warnings else "complete","source": "codex_rollout",**total,- "threadCount": observed_thread_count,+ "threadCount": len(accepted_thread_ids),}if missing_thread_ids:result["missingThreadCount"] = len(missing_thread_ids)@@ -202,13 +200,11 @@ def _scan_root_thread_ids((scan["id"],),))- roots: list[str] = []- seen: set[str] = set()+ roots: dict[str, None] = {}for candidate in candidates:- if isinstance(candidate, str) and candidate.strip() and candidate not in seen:- roots.append(candidate)- seen.add(candidate)- return roots+ if isinstance(candidate, str) and candidate.strip():+ roots[candidate] = None+ return list(roots)def _scan_execution_thread_ids(connection: sqlite3.Connection, scan: sqlite3.Row) -> list[str]:diff --git a/plugins/codex-security/scripts/workbench_schema.py b/plugins/codex-security/scripts/workbench_schema.pyindex 66baa2b4f..9efb0be4b 100644--- a/plugins/codex-security/scripts/workbench_schema.py+++ b/plugins/codex-security/scripts/workbench_schema.py@@ -1543,9 +1543,5 @@ def sql_statements(script: str) -> list[str]:return statements-def main() -> None:- argparse.ArgumentParser(description=__doc__).parse_args()--if __name__ == "__main__":- main()+ argparse.ArgumentParser(description=__doc__).parse_args()diff --git a/plugins/codex-security/scripts/workbench_source_excerpt.py b/plugins/codex-security/scripts/workbench_source_excerpt.pyindex 06c4075c7..63e9fbe25 100644--- a/plugins/codex-security/scripts/workbench_source_excerpt.py+++ b/plugins/codex-security/scripts/workbench_source_excerpt.py@@ -90,9 +90,5 @@ def safe_source_path(target: Path, relative_path: str) -> Path | None:return path-def main() -> None:- argparse.ArgumentParser(description=__doc__).parse_args()--if __name__ == "__main__":- main()+ argparse.ArgumentParser(description=__doc__).parse_args()diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.pyindex 25f8d8d30..2353cf019 100644--- a/plugins/codex-security/scripts/workbench_target.py+++ b/plugins/codex-security/scripts/workbench_target.py@@ -454,10 +454,6 @@ def git_submodule_entries(target: Path) -> tuple[tuple[Path, str], ...]:return tuple(entries)-def git_submodule_paths(target: Path) -> tuple[Path, ...]:- return tuple(path for path, _ in git_submodule_entries(target))--def require_clean_submodule_worktrees(target: Path) -> None:for submodule, expected_revision in git_submodule_entries(target):relative_path = str(submodule.relative_to(target))@@ -872,9 +868,5 @@ def scan_target_warning(scan: sqlite3.Row) -> str | None:return None-def main() -> None:- argparse.ArgumentParser(description=__doc__).parse_args()--if __name__ == "__main__":- main()+ argparse.ArgumentParser(description=__doc__).parse_args()diff --git a/plugins/codex-security/scripts/workbench_target_state.py b/plugins/codex-security/scripts/workbench_target_state.pyindex 47d2ea6d3..7d24a5f20 100644--- a/plugins/codex-security/scripts/workbench_target_state.py+++ b/plugins/codex-security/scripts/workbench_target_state.py@@ -55,9 +55,5 @@ def ensure_security_target(connection: sqlite3.Connection, target_path: str) ->return target_id-def main() -> None:- argparse.ArgumentParser(description=__doc__).parse_args()--if __name__ == "__main__":- main()+ argparse.ArgumentParser(description=__doc__).parse_args()diff --git a/plugins/codex-security/scripts/workbench_validation.py b/plugins/codex-security/scripts/workbench_validation.pyindex a65aff1e7..4459ed626 100644--- a/plugins/codex-security/scripts/workbench_validation.py+++ b/plugins/codex-security/scripts/workbench_validation.py@@ -14,10 +14,10 @@ from typing import Any# Some plugin hosts launch Python with safe-path isolation enabled.sys.path.insert(0, str(Path(__file__).resolve().parent))+import finalize_scan_contract as finalizer+from workbench_scan_usage import _reject_nonstandard_json_number as reject_nonstandard_json_number--def reject_non_finite_json(value: str) -> None:- raise ValueError(f"non-finite JSON number {value!r} is not supported")+reject_non_finite_json = finalizer._reject_non_finite_jsondef require_uuid(value: str, label: str) -> str:@@ -57,17 +57,9 @@ def require_close_note(close_reason: str | None, note: str | None) -> None:raise SystemExit("Explain why this finding will not be fixed.")-def user_text(value: str | None) -> str | None:- return optional_text(value)--def user_context_argument(args: argparse.Namespace) -> str | None:value = sys.stdin.read() if getattr(args, "user_context_stdin", False) else args.user_context- return user_text(value)---def reject_nonstandard_json_number(value: str) -> None:- raise ValueError(f"invalid JSON number {value}")+ return optional_text(value)SCAN_USAGE_TOKEN_KEYS = (@@ -195,9 +187,5 @@ def require_occurrence(connection: sqlite3.Connection, occurrence_id: str) -> sqreturn row-def main() -> None:- argparse.ArgumentParser(description=__doc__).parse_args()--if __name__ == "__main__":- main()+ argparse.ArgumentParser(description=__doc__).parse_args()diff --git a/plugins/codex-security/tests/test_capability_profiles.py b/plugins/codex-security/tests/test_capability_profiles.pyindex 8a93ac3d8..a609d1efe 100644--- a/plugins/codex-security/tests/test_capability_profiles.py+++ b/plugins/codex-security/tests/test_capability_profiles.py@@ -10,6 +10,8 @@ import sysfrom pathlib import Path, PureWindowsPathfrom types import SimpleNamespace+import pytest+try:import tomllibexcept ModuleNotFoundError: # pragma: no cover - Python 3.10 only@@ -52,23 +54,14 @@ def test_preflight_matches_windows_project_path_aliases(monkeypatch) -> None:assert trust_level(layers, root) == "untrusted"-def test_preflight_falls_back_to_tomli_without_stdlib_tomllib(monkeypatch) -> None:- real_import = builtins.__import__- monkeypatch.setitem(sys.modules, "tomli", tomllib)-- def import_without_tomllib(name, globals=None, locals=None, fromlist=(), level=0):- if name == "tomllib":- raise ModuleNotFoundError("No module named 'tomllib'", name="tomllib")- return real_import(name, globals, locals, fromlist, level)-- monkeypatch.setattr(builtins, "__import__", import_without_tomllib)-- namespace = runpy.run_path(str(PREFLIGHT_SCRIPT), run_name="config_preflight_test")-- assert namespace["tomllib"] is tomllib---def test_deep_scan_config_falls_back_to_tomli_without_stdlib_tomllib(monkeypatch) -> None:+ ("script", "run_name"),+ [+ (PREFLIGHT_SCRIPT, "config_preflight_test"),+ (DEEP_SCAN_CONFIG_SCRIPT, "deep_scan_config_test"),+ ],+)+def test_config_falls_back_to_tomli_without_stdlib_tomllib(monkeypatch, script, run_name) -> None:real_import = builtins.__import__monkeypatch.setitem(sys.modules, "tomli", tomllib)@@ -79,7 +72,7 @@ def test_deep_scan_config_falls_back_to_tomli_without_stdlib_tomllib(monkeypatchmonkeypatch.setattr(builtins, "__import__", import_without_tomllib)- namespace = runpy.run_path(str(DEEP_SCAN_CONFIG_SCRIPT), run_name="deep_scan_config_test")+ namespace = runpy.run_path(str(script), run_name=run_name)assert namespace["tomllib"] is tomllibdiff --git a/plugins/codex-security/tests/test_finalize_scan_contract.py b/plugins/codex-security/tests/test_finalize_scan_contract.pyindex 5864a7dbc..bdd7d1220 100644--- a/plugins/codex-security/tests/test_finalize_scan_contract.py+++ b/plugins/codex-security/tests/test_finalize_scan_contract.py@@ -3,7 +3,6 @@ from __future__ import annotationsimport copyimport csvimport hashlib-import importlib.utilimport ioimport jsonimport os@@ -14,25 +13,15 @@ import threadingimport unittestfrom datetime import datetime, timezonefrom pathlib import Path-from types import ModuleTypefrom unittest import mock+from workbench_test_support import ScanFixtureTestCase, load_script, windows_file_backend-def load_finalizer() -> ModuleType:- script = Path(__file__).resolve().parent.parent / "scripts" / "finalize_scan_contract.py"- spec = importlib.util.spec_from_file_location("finalize_scan_contract", script)- if spec is None or spec.loader is None:- raise RuntimeError(f"could not load {script}")- module = importlib.util.module_from_spec(spec)- spec.loader.exec_module(module)- return module---FINALIZER = load_finalizer()+FINALIZER = load_script("finalize_scan_contract")EXAMPLE_DIR = Path(__file__).resolve().parent.parent / "examples" / "completed-scan"-class FinalizeScanContractTest(unittest.TestCase):+class FinalizeScanContractTest(ScanFixtureTestCase):def setUp(self) -> None:self.temp_dir = tempfile.TemporaryDirectory()self.scan_dir = Path(self.temp_dir.name).resolve()@@ -127,9 +116,6 @@ class FinalizeScanContractTest(unittest.TestCase):"deferred": [],}- def tearDown(self) -> None:- self.temp_dir.cleanup()-def write_scan(self) -> None:self.write_json("scan-manifest.json", self.manifest)self.write_json("findings.json", self.findings)@@ -166,9 +152,6 @@ The extraction root is not enforced.path.parent.mkdir(parents=True, exist_ok=True)path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")- def read_json(self, relative_path: str) -> dict[str, object]:- return json.loads((self.scan_dir / relative_path).read_text(encoding="utf-8"))-def completion_binding(self) -> dict[str, object]:return {"scanId": "scan_001",@@ -195,9 +178,7 @@ The extraction root is not enforced.artifacts = manifest["scan"]["artifacts"]for artifact in artifacts:if artifact["path"] == relative_path:- artifact["sha256"] = FINALIZER._sha256_bytes(- (self.scan_dir / relative_path).read_bytes()- )+ artifact["sha256"] = self.sha256_file(relative_path)breakelse:raise AssertionError(f"missing sealed artifact: {relative_path}")@@ -375,7 +356,7 @@ The extraction root is not enforced.manifest = self.read_json("scan-manifest.json")for artifact in manifest["scan"]["artifacts"]:if artifact["path"] == "findings.json":- artifact["sha256"] = FINALIZER._sha256_file(self.scan_dir / "findings.json")+ artifact["sha256"] = self.sha256_file("findings.json")breakself.write_json("scan-manifest.json", manifest)before = {@@ -401,7 +382,7 @@ The extraction root is not enforced.manifest = self.read_json("scan-manifest.json")for artifact in manifest["scan"]["artifacts"]:if artifact["path"] == "findings.json":- artifact["sha256"] = FINALIZER._sha256_bytes(compact)+ artifact["sha256"] = hashlib.sha256(compact).hexdigest()breakself.write_json("scan-manifest.json", manifest)self.assertGreater(len(FINALIZER._json_bytes(findings)), len(compact))@@ -645,9 +626,7 @@ The extraction root is not enforced.for artifact in manifest["scan"]["artifacts"]if artifact["path"] == "coverage.json")- coverage_artifact["sha256"] = FINALIZER._sha256_bytes(- (self.scan_dir / "coverage.json").read_bytes()- )+ coverage_artifact["sha256"] = self.sha256_file("coverage.json")self.write_json("scan-manifest.json", manifest)with self.assertRaisesRegex(@@ -1494,7 +1473,7 @@ The extraction root is not enforced.manifest = self.read_json("scan-manifest.json")for artifact in manifest["scan"]["artifacts"]:if artifact["path"] == "findings.json":- artifact["sha256"] = FINALIZER._sha256_file(self.scan_dir / "findings.json")+ artifact["sha256"] = self.sha256_file("findings.json")self.write_json("scan-manifest.json", manifest)with self.assertRaisesRegex(FINALIZER.ContractError, "safe repository-relative"):@@ -1662,28 +1641,7 @@ The extraction root is not enforced.self.coverage["surfaces"][0]["disposition"] = "no_issue_found"self.write_scan()- backend = mock.Mock()-- def open_read_fd(scan_dir: Path, relative_path: str, _context: str) -> int:- return os.open(scan_dir / relative_path, os.O_RDONLY)-- def atomic_write(- scan_dir: Path,- relative_path: str,- payload: bytes,- *,- expected_root_identity: tuple[int, int] | None = None,- ) -> None:- path = scan_dir / relative_path- path.parent.mkdir(parents=True, exist_ok=True)- path.write_bytes(payload)-- def unlink_if_exists(scan_dir: Path, relative_path: str) -> None:- (scan_dir / relative_path).unlink(missing_ok=True)-- backend.open_read_fd.side_effect = open_read_fd- backend.atomic_write.side_effect = atomic_write- backend.unlink_if_exists.side_effect = unlink_if_exists+ backend = windows_file_backend()with (mock.patch.object(FINALIZER.os, "supports_dir_fd", set()),@@ -1754,7 +1712,7 @@ The extraction root is not enforced.self.write_json("findings.json", findings)for artifact in manifest["scan"]["artifacts"]:if artifact["path"] == "findings.json":- artifact["sha256"] = FINALIZER._sha256_file(self.scan_dir / "findings.json")+ artifact["sha256"] = self.sha256_file("findings.json")self.write_json("scan-manifest.json", manifest)with self.assertRaisesRegex(FINALIZER.ContractError, field):@@ -2350,7 +2308,7 @@ The extraction root is not enforced.manifest = self.read_json("scan-manifest.json")for artifact in manifest["scan"]["artifacts"]:if artifact["path"] == "coverage.json":- artifact["sha256"] = FINALIZER._sha256_file(self.scan_dir / "coverage.json")+ artifact["sha256"] = self.sha256_file("coverage.json")elif artifact["path"] == receipt_ref:artifact["path"] = legacy_refself.write_json("scan-manifest.json", manifest)diff --git a/plugins/codex-security/tests/test_report_projection.py b/plugins/codex-security/tests/test_report_projection.pyindex 3537d07fc..d797ce326 100644--- a/plugins/codex-security/tests/test_report_projection.py+++ b/plugins/codex-security/tests/test_report_projection.py@@ -1,24 +1,9 @@from __future__ import annotationsimport copy-import importlib.util-from pathlib import Path-from types import ModuleTypeimport pytest--PLUGIN_DIR = Path(__file__).resolve().parent.parent---def load_script(name: str) -> ModuleType:- path = PLUGIN_DIR / "scripts" / f"{name}.py"- spec = importlib.util.spec_from_file_location(name, path)- if spec is None or spec.loader is None:- raise RuntimeError(f"could not load {path}")- module = importlib.util.module_from_spec(spec)- spec.loader.exec_module(module)- return module-+from workbench_test_support import load_scriptPROJECTION = load_script("report_projection")diff --git a/plugins/codex-security/tests/test_sarif_projection.py b/plugins/codex-security/tests/test_sarif_projection.pyindex d1c369a6a..f7e151f5e 100644--- a/plugins/codex-security/tests/test_sarif_projection.py+++ b/plugins/codex-security/tests/test_sarif_projection.py@@ -1,19 +1,15 @@from __future__ import annotationsimport copy-import importlib.utilimport jsonimport unittestfrom pathlib import Path+from workbench_test_support import load_script+PLUGIN_ROOT = Path(__file__).resolve().parent.parentEXAMPLE_DIR = PLUGIN_ROOT / "examples" / "completed-scan"-spec = importlib.util.spec_from_file_location(- "sarif_finalizer", PLUGIN_ROOT / "scripts" / "finalize_scan_contract.py"-)-assert spec is not None and spec.loader is not None-FINALIZER = importlib.util.module_from_spec(spec)-spec.loader.exec_module(FINALIZER)+FINALIZER = load_script("finalize_scan_contract", module_name="sarif_finalizer")class SarifProjectionTest(unittest.TestCase):diff --git a/plugins/codex-security/tests/test_validate_scan_contract.py b/plugins/codex-security/tests/test_validate_scan_contract.pyindex a99993927..b637d3af4 100644--- a/plugins/codex-security/tests/test_validate_scan_contract.py+++ b/plugins/codex-security/tests/test_validate_scan_contract.py@@ -1,50 +1,25 @@from __future__ import annotations-import importlib.utilimport ioimport json-import shutilimport subprocessimport sys-import tempfileimport unittestfrom contextlib import redirect_stderr, redirect_stdoutfrom pathlib import Path-from types import ModuleType++from workbench_test_support import ScanFixtureTestCase, load_scriptPLUGIN_ROOT = Path(__file__).resolve().parents[1]EXAMPLE_SCAN = PLUGIN_ROOT / "examples" / "completed-scan"-def load_validator() -> ModuleType:- script = PLUGIN_ROOT / "scripts" / "validate_scan_contract.py"- spec = importlib.util.spec_from_file_location("validate_scan_contract", script)- if spec is None or spec.loader is None:- raise RuntimeError(f"could not load {script}")- module = importlib.util.module_from_spec(spec)- spec.loader.exec_module(module)- return module---VALIDATOR = load_validator()+VALIDATOR = load_script("validate_scan_contract")-class ValidateScanContractTest(unittest.TestCase):- def setUp(self) -> None:- self.temp_dir = tempfile.TemporaryDirectory()- self.scan_dir = Path(self.temp_dir.name) / "scan"- shutil.copytree(EXAMPLE_SCAN, self.scan_dir)- manifest = json.loads((self.scan_dir / "scan-manifest.json").read_text())- findings = json.loads((self.scan_dir / "findings.json").read_text())- coverage = json.loads((self.scan_dir / "coverage.json").read_text())- report = VALIDATOR.FINALIZER._generate_report_projection(manifest, findings, coverage)- (self.scan_dir / "report.md").write_bytes(report)-- def tearDown(self) -> None:- self.temp_dir.cleanup()-- def read_json(self, name: str) -> dict[str, object]:- return json.loads((self.scan_dir / name).read_text(encoding="utf-8"))+class ValidateScanContractTest(ScanFixtureTestCase):+ validator = VALIDATOR+ example_scan = EXAMPLE_SCANdef write_json(self, name: str, payload: object) -> None:(self.scan_dir / name).write_text(json.dumps(payload, indent=2) + "\n")@@ -71,9 +46,7 @@ class ValidateScanContractTest(unittest.TestCase):for artifact in manifest["scan"]["artifacts"]if artifact["path"] == "findings.json")- findings_artifact["sha256"] = VALIDATOR.FINALIZER._sha256_file(- self.scan_dir / "findings.json"- )+ findings_artifact["sha256"] = self.sha256_file("findings.json")self.write_json("scan-manifest.json", manifest)validated = VALIDATOR.validate_contract(self.scan_dir)@@ -105,9 +78,7 @@ class ValidateScanContractTest(unittest.TestCase):manifest = self.read_json("scan-manifest.json")for artifact in manifest["scan"]["artifacts"]:if artifact["path"] == "findings.json":- artifact["sha256"] = VALIDATOR.FINALIZER._sha256_file(- self.scan_dir / "findings.json"- )+ artifact["sha256"] = self.sha256_file("findings.json")breakself.write_json("scan-manifest.json", manifest)diff --git a/plugins/codex-security/tests/test_validate_tracking_source.py b/plugins/codex-security/tests/test_validate_tracking_source.pyindex 5a725c2c1..0d83bdf35 100644--- a/plugins/codex-security/tests/test_validate_tracking_source.py+++ b/plugins/codex-security/tests/test_validate_tracking_source.py@@ -1,53 +1,29 @@from __future__ import annotationsimport copy-import importlib.util+import hashlibimport ioimport json-import shutil-import tempfileimport unittestfrom contextlib import redirect_stderr, redirect_stdoutfrom pathlib import Path-from types import ModuleType++from workbench_test_support import ScanFixtureTestCase, load_scriptPLUGIN_ROOT = Path(__file__).resolve().parents[1]EXAMPLE_SCAN = PLUGIN_ROOT / "examples" / "completed-scan"-def load_validator() -> ModuleType:- script = PLUGIN_ROOT / "scripts" / "validate_tracking_source.py"- spec = importlib.util.spec_from_file_location("validate_tracking_source", script)- if spec is None or spec.loader is None:- raise RuntimeError(f"could not load {script}")- module = importlib.util.module_from_spec(spec)- spec.loader.exec_module(module)- return module---VALIDATOR = load_validator()+VALIDATOR = load_script("validate_tracking_source")def test_completed_example_passes_tracking_validation() -> None:assert VALIDATOR.validate_source(EXAMPLE_SCAN)-class ValidateTrackingSourceTest(unittest.TestCase):- def setUp(self) -> None:- self.temp_dir = tempfile.TemporaryDirectory()- self.scan_dir = Path(self.temp_dir.name) / "scan"- shutil.copytree(EXAMPLE_SCAN, self.scan_dir)- manifest = json.loads((self.scan_dir / "scan-manifest.json").read_text())- findings = json.loads((self.scan_dir / "findings.json").read_text())- coverage = json.loads((self.scan_dir / "coverage.json").read_text())- report = VALIDATOR.FINALIZER._generate_report_projection(manifest, findings, coverage)- (self.scan_dir / "report.md").write_bytes(report)-- def tearDown(self) -> None:- self.temp_dir.cleanup()-- def read_json(self, name: str) -> dict[str, object]:- return json.loads((self.scan_dir / name).read_text(encoding="utf-8"))+class ValidateTrackingSourceTest(ScanFixtureTestCase):+ validator = VALIDATOR+ example_scan = EXAMPLE_SCANdef write_json(self, name: str, payload: object) -> None:(self.scan_dir / name).write_text(@@ -64,7 +40,7 @@ class ValidateTrackingSourceTest(unittest.TestCase):for item in manifest["scan"]["artifacts"]if item["path"] == manifest["scan"]["findingsRef"])- artifact["sha256"] = VALIDATOR.FINALIZER._sha256_bytes(findings_bytes)+ artifact["sha256"] = hashlib.sha256(findings_bytes).hexdigest()self.write_json("scan-manifest.json", manifest)def rewrite_coverage_and_seal(self, coverage: dict[str, object]) -> None:@@ -76,7 +52,7 @@ class ValidateTrackingSourceTest(unittest.TestCase):for item in manifest["scan"]["artifacts"]if item["path"] == manifest["scan"]["coverageRef"])- artifact["sha256"] = VALIDATOR.FINALIZER._sha256_bytes(coverage_bytes)+ artifact["sha256"] = hashlib.sha256(coverage_bytes).hexdigest()self.write_json("scan-manifest.json", manifest)def test_selects_the_finding_from_a_sealed_scan(self) -> None:@@ -103,7 +79,7 @@ class ValidateTrackingSourceTest(unittest.TestCase):for item in manifest["scan"]["artifacts"]if item["path"] == manifest["scan"]["findingsRef"])- artifact["sha256"] = VALIDATOR.FINALIZER._sha256_bytes(findings_bytes)+ artifact["sha256"] = hashlib.sha256(findings_bytes).hexdigest()self.write_json("scan-manifest.json", manifest)with self.assertRaisesRegex(diff --git a/plugins/codex-security/tests/test_windows_report_e2e.py b/plugins/codex-security/tests/test_windows_report_e2e.pyindex 562f9ac66..fa52dab42 100644--- a/plugins/codex-security/tests/test_windows_report_e2e.py+++ b/plugins/codex-security/tests/test_windows_report_e2e.py@@ -13,6 +13,7 @@ from workbench_test_support import (SCRIPT,create_saved_workspace,start_delivered_scan,+ windows_file_backend,write_completed_contract,)@@ -44,27 +45,7 @@ def test_workbench_completion_and_exports_use_windows_file_backend(tmp_path: Pat(scan_dir / "report.html").write_text("stale report")namespace = runpy.run_path(str(SCRIPT), run_name="codex_security_workbench_db")finalizer = sys.modules[namespace["finalize_scan"].__module__]- backend = mock.Mock()-- def open_read_fd(root: Path, relative_path: str, _context: str) -> int:- return os.open(root / relative_path, os.O_RDONLY)-- def atomic_write(- root: Path,- relative_path: str,- payload: bytes,- *,- expected_root_identity: tuple[int, int] | None = None,- ) -> None:- path = root / relative_path- path.parent.mkdir(parents=True, exist_ok=True)- path.write_bytes(payload)-- backend.open_read_fd.side_effect = open_read_fd- backend.atomic_write.side_effect = atomic_write- backend.unlink_if_exists.side_effect = lambda root, relative_path: (- root / relative_path- ).unlink(missing_ok=True)+ backend = windows_file_backend()with (mock.patch.object(finalizer.os, "supports_dir_fd", set()),diff --git a/plugins/codex-security/tests/test_windows_scan_local_files.py b/plugins/codex-security/tests/test_windows_scan_local_files.pyindex 454818433..e52ee9257 100644--- a/plugins/codex-security/tests/test_windows_scan_local_files.py+++ b/plugins/codex-security/tests/test_windows_scan_local_files.py@@ -1,24 +1,12 @@from __future__ import annotations-import importlib.utilimport osfrom pathlib import Path-from types import ModuleTypeimport pytest+from workbench_test_support import load_script--def load_windows_scan_local_files() -> ModuleType:- script = Path(__file__).resolve().parent.parent / "scripts" / "windows_scan_local_files.py"- spec = importlib.util.spec_from_file_location("windows_scan_local_files", script)- if spec is None or spec.loader is None:- raise RuntimeError(f"could not load {script}")- module = importlib.util.module_from_spec(spec)- spec.loader.exec_module(module)- return module---WINDOWS_FILES = load_windows_scan_local_files()+WINDOWS_FILES = load_script("windows_scan_local_files")@pytest.mark.parametrize(diff --git a/plugins/codex-security/tests/test_windows_scope_streams.py b/plugins/codex-security/tests/test_windows_scope_streams.pyindex b2157a41a..d6bc968c2 100644--- a/plugins/codex-security/tests/test_windows_scope_streams.py+++ b/plugins/codex-security/tests/test_windows_scope_streams.py@@ -17,12 +17,13 @@ def test_scope_rejects_ntfs_streams_before_resolving_paths(monkeypatch, script:path = Path(__file__).resolve().parents[1] / "scripts" / scriptnamespace = runpy.run_path(str(path), run_name="scope_stream_test")resolve_scope = namespace["resolve_scope"]- monkeypatch.setitem(resolve_scope.__globals__, "os", SimpleNamespace(name="nt"))+ stream_component = namespace["windows_stream_component"]+ monkeypatch.setitem(stream_component.__globals__, "os", SimpleNamespace(name="nt"))monkeypatch.setitem(resolve_scope.__globals__, "Path", WindowsScopePath)root = PureWindowsPath(r"C:\Repository")error = namespace.get("InventoryError", SystemExit)with pytest.raises(error, match="alternate data stream"):resolve_scope(root, "src/app.py:stream")assert namespace["windows_stream_component"](root / "src/app.py") is None- monkeypatch.setitem(resolve_scope.__globals__, "os", SimpleNamespace(name="posix"))+ monkeypatch.setitem(stream_component.__globals__, "os", SimpleNamespace(name="posix"))assert namespace["windows_stream_component"](Path("src/app.py:fixture")) is Nonediff --git a/plugins/codex-security/tests/test_workbench_db_exports.py b/plugins/codex-security/tests/test_workbench_db_exports.pyindex 9ae58c681..eff80d588 100644--- a/plugins/codex-security/tests/test_workbench_db_exports.py+++ b/plugins/codex-security/tests/test_workbench_db_exports.py@@ -1721,9 +1721,9 @@ def test_parent_draft_preserves_reconciled_candidate_identity_before_publicationwith monkeypatch.context() as context:context.setattr(results, "write_scan_local_bytes", interrupt_coverage)with pytest.raises(OSError, match="Interrupted canonical publication"):- db.write_scan_draft(connection, args)+ results.write_scan_draft(db._WORKBENCH_DB_CONTEXT, connection, args)else:- db.write_scan_draft(connection, args)+ results.write_scan_draft(db._WORKBENCH_DB_CONTEXT, connection, args)head = json.loads((scan_dir / "checkpoint-head.json").read_text())normalized_path = scan_dir / "checkpoints" / head["checkpoint"]normalized_bytes = normalized_path.read_bytes()@@ -1748,7 +1748,7 @@ def test_parent_draft_preserves_reconciled_candidate_identity_before_publicationraw["coverage"]["surfaces"] = documents["coverage"]["surfaces"]raw_path.write_text(json.dumps(raw))staged.write_text(json.dumps(documents))- db.write_scan_draft(connection, args)+ results.write_scan_draft(db._WORKBENCH_DB_CONTEXT, connection, args)assert normalized_path.read_bytes() == normalized_bytesassert retained_raw.read_bytes() == raw_bytescanonical = json.loads((scan_dir / "coverage.json").read_text())@@ -1758,7 +1758,7 @@ def test_parent_draft_preserves_reconciled_candidate_identity_before_publicationraw["coverage"]["deferred"] = [pending]raw_path.write_text(json.dumps(raw))staged.write_text(json.dumps(documents))- db.write_scan_draft(connection, args)+ results.write_scan_draft(db._WORKBENCH_DB_CONTEXT, connection, args)head_bytes = (scan_dir / "checkpoint-head.json").read_bytes()checkpoints = set((scan_dir / "checkpoints").iterdir())# A rejected write must not leave a fresh closure for stopped recovery.@@ -1769,7 +1769,7 @@ def test_parent_draft_preserves_reconciled_candidate_identity_before_publicationraw_path.write_text(json.dumps(raw))args.expected_draft_digest = "0" * 64with pytest.raises(SystemExit, match="scan_draft_conflict"):- db.write_scan_draft(connection, args)+ results.write_scan_draft(db._WORKBENCH_DB_CONTEXT, connection, args)assert (scan_dir / "checkpoint-head.json").read_bytes() == head_bytesassert set((scan_dir / "checkpoints").iterdir()) == checkpointsargs.expected_draft_digest = None@@ -1777,14 +1777,14 @@ def test_parent_draft_preserves_reconciled_candidate_identity_before_publicationdocuments["manifest"]["scan"]["target"] = Nonestaged.write_text(json.dumps(documents))with pytest.raises(results.ContractError, match="target"):- db.write_scan_draft(connection, args)+ results.write_scan_draft(db._WORKBENCH_DB_CONTEXT, connection, args)assert (scan_dir / "checkpoint-head.json").read_bytes() == head_bytesassert set((scan_dir / "checkpoints").iterdir()) == checkpointsdocuments["manifest"]["scan"]["target"] = targetdocuments["coverage"]["deferred"].remove(pending)documents["coverage"]["resolvedDeferred"] = raw["coverage"]["resolvedDeferred"]staged.write_text(json.dumps(documents))- db.write_scan_draft(connection, args)+ results.write_scan_draft(db._WORKBENCH_DB_CONTEXT, connection, args)canonical = json.loads((scan_dir / "coverage.json").read_text())assert [row["id"] for row in canonical["deferred"]] == ["review-b"]assert canonical["resolvedDeferred"] == raw["coverage"]["resolvedDeferred"]diff --git a/plugins/codex-security/tests/test_workbench_timestamps.py b/plugins/codex-security/tests/test_workbench_timestamps.pyindex e47650c64..ca2e8ecc4 100644--- a/plugins/codex-security/tests/test_workbench_timestamps.py+++ b/plugins/codex-security/tests/test_workbench_timestamps.py@@ -5,6 +5,7 @@ import jsonimport sqlite3from datetime import datetime, timezonefrom pathlib import Path+from types import SimpleNamespaceimport pytest@@ -52,7 +53,7 @@ def test_deep_scan_deadline_and_heartbeat_on_python310(monkeypatch, tmp_path: Pamonkeypatch.syspath_prepend(str(Path(__file__).resolve().parents[1] / "scripts"))deep = importlib.import_module("deep_scan_workbench")monkeypatch.setattr(deep, "datetime", Python310DateTime)- monkeypatch.setattr(deep, "now", lambda: "2026-08-15T12:00:00Z")+ monkeypatch.setattr(deep, "_dependencies", SimpleNamespace(now=lambda: "2026-08-15T12:00:00Z"))assert deep.deep_scan_deadline_reached({"created_at": "2026-08-15T11:00:00z", "max_time_hours": 1})diff --git a/plugins/codex-security/tests/workbench_test_support.py b/plugins/codex-security/tests/workbench_test_support.pyindex b0ec2919e..454783b07 100644--- a/plugins/codex-security/tests/workbench_test_support.py+++ b/plugins/codex-security/tests/workbench_test_support.py@@ -1,22 +1,37 @@from __future__ import annotationsimport hashlib+import importlib.utilimport jsonimport osimport shlex+import shutilimport sqlite3import statimport subprocessimport sys+import tempfileimport uuidfrom pathlib import Path+from types import ModuleTypefrom typing import Any+from unittest import TestCase, mockSCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "workbench_db.py"SNAPSHOT_SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "snapshot_sqlite.py"PLUGIN_MANIFEST = Path(__file__).resolve().parents[1] / ".codex-plugin" / "plugin.json"+def load_script(name: str, *, module_name: str | None = None) -> ModuleType:+ script = SCRIPT.parent / f"{name}.py"+ spec = importlib.util.spec_from_file_location(module_name or name, script)+ if spec is None or spec.loader is None:+ raise RuntimeError(f"could not load {script}")+ module = importlib.util.module_from_spec(spec)+ spec.loader.exec_module(module)+ return module++def source_plugin_version() -> str:manifest = json.loads(PLUGIN_MANIFEST.read_text(encoding="utf-8"))version = manifest.get("version")@@ -446,3 +461,50 @@ def write_completed_contract((scan_dir / "coverage.json").write_text(json.dumps(coverage))(scan_dir / "scan-manifest.json").write_text(json.dumps(manifest))(scan_dir / "report.md").write_text("# Fixture report\n")+++def windows_file_backend() -> mock.Mock:+ backend = mock.Mock()++ def open_read_fd(scan_dir: Path, relative_path: str, _context: str) -> int:+ return os.open(scan_dir / relative_path, os.O_RDONLY)++ def atomic_write(+ scan_dir: Path,+ relative_path: str,+ payload: bytes,+ *,+ expected_root_identity: tuple[int, int] | None = None,+ ) -> None:+ path = scan_dir / relative_path+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_bytes(payload)++ def unlink_if_exists(scan_dir: Path, relative_path: str) -> None:+ (scan_dir / relative_path).unlink(missing_ok=True)++ backend.open_read_fd.side_effect = open_read_fd+ backend.atomic_write.side_effect = atomic_write+ backend.unlink_if_exists.side_effect = unlink_if_exists+ return backend+++class ScanFixtureTestCase(TestCase):+ def setUp(self) -> None:+ self.temp_dir = tempfile.TemporaryDirectory()+ self.scan_dir = Path(self.temp_dir.name) / "scan"+ shutil.copytree(self.example_scan, self.scan_dir)+ manifest = json.loads((self.scan_dir / "scan-manifest.json").read_text())+ findings = json.loads((self.scan_dir / "findings.json").read_text())+ coverage = json.loads((self.scan_dir / "coverage.json").read_text())+ report = self.validator.FINALIZER._generate_report_projection(manifest, findings, coverage)+ (self.scan_dir / "report.md").write_bytes(report)++ def tearDown(self) -> None:+ self.temp_dir.cleanup()++ def read_json(self, name: str) -> dict[str, object]:+ return json.loads((self.scan_dir / name).read_text(encoding="utf-8"))++ def sha256_file(self, name: str) -> str:+ return hashlib.sha256((self.scan_dir / name).read_bytes()).hexdigest()diff --git a/sdk/typescript/tests-ts/deep-scan-timestamp-compat.test.ts b/sdk/typescript/tests-ts/deep-scan-timestamp-compat.test.tsindex f6e6629c9..a3810901d 100644--- a/sdk/typescript/tests-ts/deep-scan-timestamp-compat.test.ts+++ b/sdk/typescript/tests-ts/deep-scan-timestamp-compat.test.ts@@ -19,6 +19,7 @@ if sys.version_info >= (3, 11):deep_scan.datetime = Python310Datetimecase = json.loads(sys.argv[2])deep_scan.now = lambda: case["now"]+deep_scan.configure(deep_scan)connection = sqlite3.connect(":memory:")connection.execute("CREATE TABLE deep_scan_workers (scan_id TEXT, status TEXT)")if case.get("activeWorker"):diff --git a/sdk/typescript/tests-ts/deep-scan-workbench.test.ts b/sdk/typescript/tests-ts/deep-scan-workbench.test.tsindex 7da35e97a..aa23340c0 100644--- a/sdk/typescript/tests-ts/deep-scan-workbench.test.ts+++ b/sdk/typescript/tests-ts/deep-scan-workbench.test.ts@@ -1,7 +1,7 @@import { mkdir, readFile, writeFile } from "node:fs/promises";import { dirname, join } from "node:path";import { afterEach, describe, expect, test } from "bun:test";-import { runWorkbench } from "../src/runtime.js";+import { workbenchCommand as workbench } from "./support/workbench-command.js";import { loadBundledRuntime, PLUGIN_ROOT } from "./plugin-root.js";import { runPython } from "./support/python-probe.js";import { createTemporaryDirectories } from "./support/temporary-directories.js";@@ -36,6 +36,7 @@ const deepScanOwnershipProbe = ["deep_scan.require_scan = lambda database, value: database.execute('SELECT * FROM scans WHERE id = ?', (value,)).fetchone()","deep_scan.require_workspace = lambda database, value: database.execute('SELECT * FROM workspaces WHERE id = ?', (value,)).fetchone()","deep_scan.now = lambda: 'after'",+ "deep_scan.configure(deep_scan)","deep_scan.deep_scan_result = lambda database, value, *, start_disposition=None: {'startDisposition': start_disposition}","try:"," result = deep_scan.begin_deep_scan_for_scan(connection, scan_id, 'requesting-thread', argparse.Namespace(claim_token=case['suppliedToken'], model=None, reasoning_effort=None))",@@ -138,6 +139,7 @@ test("recovers an interrupted copied Deep Scan publication", async () => {"connection.execute('INSERT INTO scans VALUES (?, ?)', ('scan', str(scan_dir)))","connection.execute('INSERT INTO deep_scan_workers VALUES (?, ?, ?, ?, ?)', ('scan', 'dedup', 'running', str(snapshot.parent.parent), 'now'))","deep_scan.require_scan = lambda database, value: database.execute('SELECT * FROM scans WHERE id = ?', (value,)).fetchone()",+ "deep_scan.configure(deep_scan)","deep_scan.recover_candidate_ledger_publication(connection, 'scan')","print(json.dumps({'ledger': ledger.read_text(encoding='utf-8'), 'backup': backup.exists()}))",].join("\n"),@@ -169,20 +171,11 @@ test.each([await writeFile(join(repository, "source.py"), "# synthetic source\n");const python = Bun.which("python3") ?? Bun.which("python");expect(python).not.toBeNull();- const command = (args: string[], input?: string) =>- runWorkbench(- {- python: python!,- pluginRoot: PLUGIN_ROOT,- environment: {- ...process.env,- CODEX_SECURITY_STATE_DIR: join(root, "state"),- CODEX_HOME: join(root, "codex-home"),- },- },- args,- input,- );+ const command = workbench(python!, () => ({+ ...process.env,+ CODEX_SECURITY_STATE_DIR: join(root, "state"),+ CODEX_HOME: join(root, "codex-home"),+ }));const registration = await command(["register-cli-scan",diff --git a/sdk/typescript/tests-ts/support/workbench-command.ts b/sdk/typescript/tests-ts/support/workbench-command.tsnew file mode 100644index 000000000..b11051fc5--- /dev/null+++ b/sdk/typescript/tests-ts/support/workbench-command.ts@@ -0,0 +1,19 @@+import { runWorkbench } from "../../src/runtime.js";+import { PLUGIN_ROOT } from "../plugin-root.js";++export function workbenchCommand(+ python: string,+ environment: string | (() => NodeJS.ProcessEnv),+) {+ if (typeof environment === "string")+ return workbenchCommand(python, () => ({+ PATH: process.env["PATH"],+ CODEX_SECURITY_STATE_DIR: environment,+ }));+ return (args: readonly string[], input?: string) =>+ runWorkbench(+ { python, pluginRoot: PLUGIN_ROOT, environment: environment() },+ args,+ input,+ );+}diff --git a/sdk/typescript/tests-ts/workbench-canonical-paths.test.ts b/sdk/typescript/tests-ts/workbench-canonical-paths.test.tsindex c15134c6d..bfd9ab755 100644--- a/sdk/typescript/tests-ts/workbench-canonical-paths.test.ts+++ b/sdk/typescript/tests-ts/workbench-canonical-paths.test.ts@@ -35,6 +35,7 @@ const simulatedPathProbe = ["deep_scan.Path = SimulatedPath","deep_scan.os = SimpleNamespace(path=path_module)","deep_scan.require_canonical_scan_directory = lambda path: path",+ "deep_scan.configure(deep_scan)","try:"," result = deep_scan.deep_scan_path({'scan_dir': root}, supplied, 'Worker prompt path', kind='file')","except SystemExit:",@@ -65,6 +66,7 @@ const realFilesystemProbe = [" artifact_name = 'prompts/prompt.txt'"," candidate_name = 'prompt.txt'","deep_scan.require_canonical_scan_directory = workbench.require_canonical_scan_directory",+ "deep_scan.configure(deep_scan)","def accepted(action):"," try:"," action()",