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
fix(workbench): read excerpts from the scanned source (#488)
Michael D'Angelo4a550a604146
diff --git a/plugins/codex-security/mcp-app/src/workbench/migrations.ts b/plugins/codex-security/mcp-app/src/workbench/migrations.tsindex bc917ec1f..86da3ab78 100644--- a/plugins/codex-security/mcp-app/src/workbench/migrations.ts+++ b/plugins/codex-security/mcp-app/src/workbench/migrations.ts@@ -224,7 +224,7 @@ export function applyMigrations();} else if (item.version === 16) backfill = repairStableTargets(database);else if (applied.has(item.version)) {- if ([2, 12, 13, 26, 28, 31, 32, 47].includes(item.version))+ if ([2, 12, 13, 26, 28, 31, 32, 42, 47, 50].includes(item.version))repairAdditive(database, item.version);else if (item.version === 11) repairDeepScan(database);} else {diff --git a/plugins/codex-security/mcp-app/tests/test_workbench_database.ts b/plugins/codex-security/mcp-app/tests/test_workbench_database.tsindex ff6dc51a7..258bd727d 100644--- a/plugins/codex-security/mcp-app/tests/test_workbench_database.ts+++ b/plugins/codex-security/mcp-app/tests/test_workbench_database.ts@@ -269,6 +269,193 @@ test(},);+for (const previewVersion of [34, 40, 41, 42, 43, 44, 48]) {+ test(`database-info upgrades source-scope preview ${previewVersion} without losing scan data`, async () => {+ const directory = await temporary.create("workbench-source-preview-");+ const databasePath = join(directory, "workbench.sqlite3");+ const old = new DatabaseSync(databasePath);+ applyMigrations(+ old,+ migrations.filter(+ (item) => item.version <= (previewVersion === 43 ? 42 : 32),+ ),+ );+ insertScan(old);+ const sourceScope = migrations.find((item) => item.version === 50)!;+ old.exec(sourceScope.statements.join("\n"));+ old+ .prepare("INSERT INTO schema_migrations VALUES (?, ?, 'original')")+ .run(previewVersion, sourceScope.name);+ const scopes = JSON.stringify({+ "synthetic/repository": ["src/original.ts"],+ });+ old.prepare("UPDATE scans SET source_scopes_json = ?").run(scopes);+ old.close();++ assert.equal((await databaseInfo(directory)).databasePath, databasePath);+ const upgraded = new DatabaseSync(databasePath);+ try {+ assertMigrationNames(+ upgraded,+ 34,+ 40,+ 41,+ 42,+ 43,+ 44,+ 45,+ 46,+ 47,+ 48,+ 49,+ 50,+ );+ assert.equal(+ upgraded+ .prepare(+ "SELECT applied_at FROM schema_migrations WHERE version = 50",+ )+ .get()?.applied_at,+ "original",+ );+ assert.equal(+ upgraded+ .prepare("SELECT source_scopes_json FROM scans WHERE id = 'scan'")+ .get()?.source_scopes_json,+ scopes,+ );+ assert.equal(+ upgraded.prepare("SELECT name FROM scans WHERE id = 'scan'").get()+ ?.name,+ null,+ );+ assert.deepEqual(upgraded.prepare("PRAGMA foreign_key_check").all(), []);+ const history = upgraded+ .prepare("SELECT * FROM schema_migrations ORDER BY version")+ .all();+ await databaseInfo(directory);+ assert.deepEqual(+ upgraded+ .prepare("SELECT * FROM schema_migrations ORDER BY version")+ .all(),+ history,+ );+ } finally {+ upgraded.close();+ }+ });+}++test("database-info retains scan names from installed migration 42", async () => {+ const directory = await temporary.create("workbench-released-names-");+ const databasePath = join(directory, "workbench.sqlite3");+ const old = new DatabaseSync(databasePath);+ applyMigrations(+ old,+ migrations.filter((item) => item.version <= 42),+ );+ insertScan(old);+ old.exec("UPDATE scans SET name = 'Original scan'");+ const originalAppliedAt = old+ .prepare("SELECT applied_at FROM schema_migrations WHERE version = 42")+ .get()?.applied_at;+ old.close();+ await databaseInfo(directory);+ const upgraded = new DatabaseSync(databasePath);+ try {+ assertMigrationNames(upgraded, 42, 43, 44, 45, 46, 47, 48, 49, 50);+ assert.equal(+ upgraded.prepare("SELECT name FROM scans").get()?.name,+ "Original scan",+ );+ assert.equal(+ upgraded.prepare("SELECT source_scopes_json FROM scans").get()+ ?.source_scopes_json,+ null,+ );+ assert.equal(+ upgraded+ .prepare("SELECT applied_at FROM schema_migrations WHERE version = 42")+ .get()?.applied_at,+ originalAppliedAt,+ );+ } finally {+ upgraded.close();+ }+});++test("database-info repairs recorded scan-name and source-scope columns", async () => {+ const directory = await temporary.create("workbench-source-repair-");+ const databasePath = join(directory, "workbench.sqlite3");+ const old = new DatabaseSync(databasePath);+ applyMigrations(old);+ insertScan(old);+ const history = old+ .prepare("SELECT * FROM schema_migrations ORDER BY version")+ .all();+ old.exec(+ "ALTER TABLE scans DROP COLUMN name; ALTER TABLE scans DROP COLUMN source_scopes_json",+ );+ old.close();+ await databaseInfo(directory);+ const repaired = new DatabaseSync(databasePath);+ try {+ assert.deepEqual(+ {+ ...repaired+ .prepare(+ "SELECT name, source_scopes_json, target_revision FROM scans",+ )+ .get(),+ },+ { name: null, source_scopes_json: null, target_revision: "revision" },+ );+ assert.deepEqual(+ repaired+ .prepare("SELECT * FROM schema_migrations ORDER BY version")+ .all(),+ history,+ );+ } finally {+ repaired.close();+ }+});++for (const version of [43, 50]) {+ test(`database-info rejects unsupported source-scope history ${version} without changing it`, async () => {+ const directory = await temporary.create("workbench-source-unknown-");+ const databasePath = join(directory, "workbench.sqlite3");+ const old = new DatabaseSync(databasePath);+ applyMigrations(old);+ old+ .prepare(+ "UPDATE schema_migrations SET name = 'unknown source history' WHERE version = ?",+ )+ .run(version);+ const history = old+ .prepare("SELECT * FROM schema_migrations ORDER BY version")+ .all();+ const originalSchema = schema(old);+ old.close();+ await assert.rejects(+ databaseInfo(directory),+ /unsupported source-scope migration history/u,+ );+ const unchanged = new DatabaseSync(databasePath);+ try {+ assert.deepEqual(schema(unchanged), originalSchema);+ assert.deepEqual(+ unchanged+ .prepare("SELECT * FROM schema_migrations ORDER BY version")+ .all(),+ history,+ );+ } finally {+ unchanged.close();+ }+ });+}+test("preview index history does not skip findings, embedding or checkpoint migrations", (t) => {const database = memory(t);applyMigrations(database, [diff --git a/plugins/codex-security/plugin-files.json b/plugins/codex-security/plugin-files.jsonindex 2411d5a98..c637c3308 100644--- a/plugins/codex-security/plugin-files.json+++ b/plugins/codex-security/plugin-files.json@@ -98,6 +98,7 @@"scripts/workbench_scan_usage.py","scripts/workbench_schema.py","scripts/workbench_source_excerpt.py",+ "scripts/workbench_source_scopes.py","scripts/workbench_target.py","scripts/workbench_target_state.py","scripts/workbench_validation.py",diff --git a/plugins/codex-security/scripts/deep_scan_workbench.py b/plugins/codex-security/scripts/deep_scan_workbench.pyindex cebf561ca..7d12f6576 100644--- a/plugins/codex-security/scripts/deep_scan_workbench.py+++ b/plugins/codex-security/scripts/deep_scan_workbench.py@@ -20,6 +20,7 @@ import workbench_saved_results as saved_resultsfrom deep_scan_config import resolve_deep_scan_configfrom workbench.handoff import require_current_continuationfrom workbench.storage import create_private_directory+from workbench_source_scopes import capture_source_scopesfrom workbench_target import directory_snapshot_regular_file_countfrom workbench_validation import optional_text, require_uuid, user_context_argument@@ -830,6 +831,11 @@ def begin_deep_scan_for_target(terminal["id"],start_disposition="joined",)+ source_scopes = capture_source_scopes(+ target,+ (revision, target_snapshot_digest, target_device, target_inode),+ [scope],+ )config = effective_deep_scan_config(args)workflow_version = optional_text(args.workflow_version, maximum=256)if workflow_version is None:@@ -875,6 +881,7 @@ def begin_deep_scan_for_target(scope=scope,diff_target=None,target_identity=target_identity,+ source_scopes=source_scopes,target_root=target_root,target_summary=None,scope_file_count=scope_file_count,diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.pyindex 1a741c885..cb41ba860 100644--- a/plugins/codex-security/scripts/workbench_db.py+++ b/plugins/codex-security/scripts/workbench_db.py@@ -96,7 +96,14 @@ from workbench_schema import (from workbench_schema import (apply_migrations as apply_schema_migrations,)-from workbench_source_excerpt import finding_source_excerpt, safe_source_path+from workbench_source_excerpt import finding_source_excerpt+from workbench_source_scopes import (+ capture_source_scopes,+ expected_target_kinds,+ public_scan_recipe,+ requested_scan_paths,+ safe_source_path,+)from workbench_target import (clean_worktree_content_digest,copy_directory_excluding,@@ -421,27 +428,6 @@ def require_scannable_target(target: Path) -> None:)-def expected_target_kinds(scan: sqlite3.Row) -> list[str]:- if scan["mode"] == "diff":- return ["git_diff"]- if scan["target_revision"] == "unversioned":- return ["directory_snapshot"]- if scan["target_snapshot_digest"] is None:- return ["git_worktree", "git_revision"]- if scan["target_snapshot_digest"] == clean_worktree_content_digest():- return ["git_revision"]- return ["git_worktree"]---def requested_scan_paths(scan: sqlite3.Row) -> list[str]:- if "recipe_json" in scan.keys() and scan["recipe_json"] is not None:- recipe = json.loads(scan["recipe_json"], parse_constant=reject_non_finite_json)- target = recipe["target"]- if target["kind"] == "paths":- return target["paths"]- return [scan["scope"]]--def requested_scan_file_paths(scan: sqlite3.Row) -> list[str]:# Older scans did not record path kinds. Retain their lexical scope semantics# rather than infer the original target from a possibly changed checkout.@@ -867,6 +853,12 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dictdiff_target,metadata=target_metadata,)+ source_scopes = capture_source_scopes(+ target,+ target_identity,+ [scope],+ diff_target_kind=diff_target["kind"] if diff_target is not None else None,+ )target_root = scan_target_root(args.scan_root, target)create_private_directory(target_root)if manages_transaction:@@ -915,6 +907,7 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dictscope=scope,diff_target=diff_target,target_identity=target_identity,+ source_scopes=source_scopes,target_root=target_root,target_summary=target_summary,scope_file_count=scope_file_count,@@ -959,6 +952,12 @@ def _start_prompt_driven_scan()diff_identity = scan_diff_identity(diff_target)target_identity = scan_target_identity(target, diff_target)+ source_scopes = capture_source_scopes(+ target,+ target_identity,+ [scope],+ diff_target_kind=diff_target["kind"] if diff_target is not None else None,+ )target_root = scan_target_root(args.scan_root, target)busy_timeout = int(connection.execute("PRAGMA busy_timeout").fetchone()[0])@@ -1071,6 +1070,7 @@ def _start_prompt_driven_scan(scope=scope,diff_target=diff_target,target_identity=target_identity,+ source_scopes=source_scopes,target_root=target_root,target_summary=target_summary,scope_file_count=scope_file_count,@@ -1755,6 +1755,7 @@ def register_cli_scan(recipe = parse_scan_recipe(recipe_json, repository)requested_target = recipe["target"]paths = requested_target["paths"]+ recipe.pop("_codexSecurityFileScopes", None)# Capture host-observed kinds once; resumes and publication use the saved recipe.recipe.pop("scopeFilePaths", None)if file_paths := [path for path in paths if (repository / path).is_file()]:@@ -1776,6 +1777,12 @@ def register_cli_scan(diff_target["contentDigest"] = worktree_content_digest(repository)mode = "diff" if diff_target is not None else recipe["mode"]target_identity = scan_target_identity(repository, diff_target)+ source_scopes = capture_source_scopes(+ repository,+ target_identity,+ paths or ["."],+ diff_target_kind=diff_target["kind"] if diff_target is not None else None,+ )scope_file_count = (directory_snapshot_regular_file_count(repository)if not paths@@ -1846,6 +1853,7 @@ def register_cli_scan(scope=scope,diff_target=diff_target,target_identity=target_identity,+ source_scopes=source_scopes,target_root=scan_dir.parent,target_summary=None,scope_file_count=scope_file_count,@@ -2727,7 +2735,7 @@ def scan_context(}if scan["recipe_json"] is not None:context["parentScanId"] = scan["parent_scan_id"]- context["recipe"] = json.loads(scan["recipe_json"], parse_constant=reject_non_finite_json)+ context["recipe"] = public_scan_recipe(scan)return context@@ -3025,6 +3033,7 @@ def finding_result(severity = details.get("severity")severity = severity if isinstance(severity, dict) else {}locations = []+ excerpt_locations = []try:target = require_scan_target_identity(scan)except SystemExit:@@ -3039,10 +3048,20 @@ def finding_result(""",(occurrence["id"], FINDING_LOCATIONS_LIMIT),):+ display_path = bounded_output_text(row["relative_path"], FINDING_LOCATION_PATH_BYTES)+ if display_path == row["relative_path"]:+ excerpt_locations.append(+ {+ "endLine": row["end_line"],+ "path": row["relative_path"],+ "role": row["role"],+ "startLine": row["start_line"],+ }+ )absolute_path = safe_source_path(target, row["relative_path"]) if target else Nonelocation = {"endLine": row["end_line"],- "path": bounded_output_text(row["relative_path"], FINDING_LOCATION_PATH_BYTES),+ "path": display_path,"role": (bounded_output_text(row["role"], FINDING_LOCATION_ROLE_BYTES)if row["role"] is not None@@ -3084,7 +3103,9 @@ def finding_result(result["knownScanIds"] = known_scan_idsif related:result["related"] = related- source_excerpt = finding_source_excerpt(scan, target, locations)+ source_excerpt = finding_source_excerpt(+ scan, target, excerpt_locations, requested_scan_paths(scan)+ )if source_excerpt:result["sourceExcerpt"] = source_excerptartifact_paths = finding_artifact_paths(Path(scan["scan_dir"]), details)diff --git a/plugins/codex-security/scripts/workbench_scan_history.py b/plugins/codex-security/scripts/workbench_scan_history.pyindex 33cccd12c..8bb095921 100644--- a/plugins/codex-security/scripts/workbench_scan_history.py+++ b/plugins/codex-security/scripts/workbench_scan_history.py@@ -19,8 +19,9 @@ from report_projection import SEVERITY_ORDERfrom workbench_constants import ARTIFACTS, FINDINGS_PAGE_MAXfrom workbench_scan_start import scan_target_identityfrom workbench_scan_usage import stored_scan_cost_fields+from workbench_source_scopes import public_scan_recipefrom workbench_target import git_output, require_scan_target_identity-from workbench_validation import reject_non_finite_json, timestamp_key+from workbench_validation import timestamp_keydef rename_scan(connection: sqlite3.Connection, scan: sqlite3.Row, name: str) -> dict[str, Any]:@@ -37,7 +38,7 @@ def scan_recipe(scan: sqlite3.Row) -> dict[str, Any]:raise SystemExit("This scan does not have a saved launch recipe.")return {"parentScanId": scan["parent_scan_id"],- "recipe": json.loads(scan["recipe_json"], parse_constant=reject_non_finite_json),+ "recipe": public_scan_recipe(scan),"scanId": scan["id"],}diff --git a/plugins/codex-security/scripts/workbench_scan_start.py b/plugins/codex-security/scripts/workbench_scan_start.pyindex ab37bd4fd..28c893773 100644--- a/plugins/codex-security/scripts/workbench_scan_start.py+++ b/plugins/codex-security/scripts/workbench_scan_start.py@@ -13,6 +13,7 @@ from collections.abc import Callable, Iteratorfrom contextlib import contextmanagerfrom datetime import datetime, timezonefrom pathlib import Path+from typing import Any# Some plugin hosts launch Python with safe-path isolation enabled.sys.path.insert(0, str(Path(__file__).resolve().parent))@@ -221,6 +222,7 @@ def insert_running_scan(scope: str,diff_target: dict[str, str] | None,target_identity: tuple[str, str | None, int | str, int | str],+ source_scopes: dict[str, Any],target_root: Path,target_summary: str | None,scope_file_count: int,@@ -244,11 +246,11 @@ def insert_running_scan("""INSERT INTO scans (id, workspace_id, target_id, target_path, target_revision, target_snapshot_digest,- target_device, target_inode, scope, mode, user_context,+ target_device, target_inode, source_scopes_json, scope, mode, user_context,deep_scan_owner_thread_id, diff_target_kind, diff_base_revision,diff_head_revision, diff_content_digest, target_summary, scan_dir, model,reasoning_effort, status, phase, handoff_status, started_at, created_at, updated_at- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,'running', 'preflight', ?, ?, ?, ?)""",(@@ -257,6 +259,7 @@ def insert_running_scan(workspace["target_id"],str(target),*target_identity,+ json.dumps(source_scopes, allow_nan=False, separators=(",", ":"), sort_keys=True),scope,workspace["default_mode"],user_context,diff --git a/plugins/codex-security/scripts/workbench_schema.py b/plugins/codex-security/scripts/workbench_schema.pyindex 90271d5c1..d2c940079 100644--- a/plugins/codex-security/scripts/workbench_schema.py+++ b/plugins/codex-security/scripts/workbench_schema.py@@ -75,7 +75,7 @@ def apply_migrations(elif version == 16:should_backfill_targets = repair_stable_targets_migration(connection)elif version in applied:- if version in (2, 12, 13, 26, 28, 31, 32, 47):+ if version in (2, 12, 13, 26, 28, 31, 32, 42, 47, 50):repair_additive_migration(connection, version)elif version == 11:repair_deep_scan_migration(connection)diff --git a/plugins/codex-security/scripts/workbench_source_excerpt.py b/plugins/codex-security/scripts/workbench_source_excerpt.pyindex 1cb80175d..6faaccf28 100644--- a/plugins/codex-security/scripts/workbench_source_excerpt.py+++ b/plugins/codex-security/scripts/workbench_source_excerpt.py@@ -8,9 +8,16 @@ import sysfrom pathlib import Path, PurePosixPathfrom typing import Any-# Some plugin hosts launch Python with safe-path isolation enabled.sys.path.insert(0, str(Path(__file__).resolve().parent))-from workbench_target import clean_worktree_content_digest, git_bytes+from workbench_source_scopes import (+ load_source_scopes,+ normalized_path_component,+ offline_git_bytes,+ relative_path,+ safe_source_path,+ source_object_for_path,+)+from workbench_target import clean_worktree_content_digestCONTEXT_LINES = 3MAX_BYTES = 16_000@@ -21,23 +28,70 @@ def finding_source_excerpt(scan: sqlite3.Row,target: Path | None,locations: list[dict[str, Any]],+ scopes: list[str],) -> str | None:- if target is None or not locations:+ if target is None or not locations or scan["target_revision"] == "unversioned":return None- location = next(- (- candidate- for candidate in locations- if "root_control" in str(candidate.get("role") or "").lower()- ),- locations[0],+ snapshot = scan["target_snapshot_digest"]+ if snapshot is not None and snapshot != clean_worktree_content_digest():+ return None+ locations = [+ location+ for location in locations+ if isinstance(path := location.get("path"), str)+ and isinstance(location.get("startLine"), int)+ and safe_source_path(target, path) is not None+ ]+ if not locations:+ return None+ try:+ context = load_source_scopes(scan, target, scopes)+ except (OSError, RuntimeError, SystemExit, UnicodeError, ValueError):+ return None+ if context is None:+ return None+ repository, tree, records = context+ indexed: dict[tuple[str, ...], list[dict[str, str]]] = {}+ for record in records:+ for name in ("path", "canonicalPath"):+ parts = tuple(+ normalized_path_component(part) for part in PurePosixPath(record[name]).parts+ )+ indexed.setdefault(parts, []).append(record)+ lengths = sorted({len(parts) for parts in indexed})++ def source_object(path: str) -> str | None:+ parsed = relative_path(path)+ if parsed is None:+ return None+ parts = tuple(normalized_path_component(part) for part in parsed.parts)+ try:+ for length in lengths:+ if length > len(parts):+ break+ for record in indexed.get(parts[:length], []):+ object_id = source_object_for_path(repository, tree, target, path, record)+ if object_id is not None:+ return object_id+ except (OSError, RuntimeError, SystemExit, UnicodeError, ValueError):+ # An unavailable source object leaves the optional excerpt absent.+ pass+ return None++ locations.sort(+ key=lambda location: (+ location.get("role") != "root_control",+ "root_control" not in str(location.get("role") or "").lower(),+ ))- path = location.get("path")- start_line = location.get("startLine")- end_line = location.get("endLine")- if not isinstance(path, str) or not isinstance(start_line, int):+ for location in locations:+ object_id = source_object(location["path"])+ if object_id is not None:+ break+ else:return None- source = scanned_source_text(scan, target, path)+ start_line, end_line = location["startLine"], location.get("endLine")+ source = scanned_source_text(repository, object_id)if not source or "\0" in source:return None# Match location validation and SARIF hashing: CR, LF, and CRLF are line breaks.@@ -58,46 +112,15 @@ def finding_source_excerpt(f"{line_number:>{width}} {lines[line_number - 1]}"for line_number in range(excerpt_start, excerpt_end + 1))- encoded = excerpt.encode("utf-8")[:MAX_BYTES]- return encoded.decode("utf-8", errors="ignore")+ return excerpt.encode("utf-8")[:MAX_BYTES].decode("utf-8", errors="ignore")-def scanned_source_text(scan: sqlite3.Row, target: Path, path: str) -> str | None:- if safe_source_path(target, path) is None:- return None- revision = scan["target_revision"]- if revision == "unversioned":- return None- snapshot_digest = scan["target_snapshot_digest"]- if (- scan["diff_target_kind"] == "working_tree"- and scan["diff_content_digest"] != clean_worktree_content_digest()- ):- return None- if snapshot_digest is not None and snapshot_digest != clean_worktree_content_digest():- return None- if (- scan["diff_target_kind"] == "working_tree"- and scan["diff_content_digest"] != clean_worktree_content_digest()- ):- return None- object_name = f"{revision}:./{path}"- content = git_bytes(target, "cat-file", "blob", object_name)- return content.decode("utf-8", errors="replace") if content is not None else None---def safe_source_path(target: Path, relative_path: str) -> Path | None:- if "\\" in relative_path:- return None- parsed = PurePosixPath(relative_path)- if parsed.is_absolute() or ".." in parsed.parts:- return None+def scanned_source_text(repository: Path, object_id: str) -> str | None:try:- path = (target / parsed.as_posix()).resolve()- path.relative_to(target)- except (OSError, RuntimeError, ValueError):+ content = offline_git_bytes(repository, "cat-file", "blob", object_id)+ except (OSError, RuntimeError, SystemExit):return None- return path+ return content.decode("utf-8", errors="replace") if content is not None else Noneif __name__ == "__main__":diff --git a/plugins/codex-security/scripts/workbench_source_scopes.py b/plugins/codex-security/scripts/workbench_source_scopes.pynew file mode 100644index 000000000..c3204e826--- /dev/null+++ b/plugins/codex-security/scripts/workbench_source_scopes.py@@ -0,0 +1,388 @@+"""Bind optional finding excerpts to the Git objects selected at scan start."""++from __future__ import annotations++import argparse+import json+import os+import re+import sqlite3+import stat+import sys+from functools import cache+from pathlib import Path, PurePosixPath+from typing import Any+from unicodedata import normalize++sys.path.insert(0, str(Path(__file__).resolve().parent))+from workbench_target import (+ clean_worktree_content_digest,+ git_bytes,+ git_worktree_context,+)+from workbench_validation import reject_non_finite_json++OBJECT_ID = re.compile(r"(?:[0-9a-f]{40}|[0-9a-f]{64})\Z")+++def expected_target_kinds(scan: sqlite3.Row) -> list[str]:+ if scan["mode"] == "diff":+ return ["git_diff"]+ if scan["target_revision"] == "unversioned":+ return ["directory_snapshot"]+ if scan["target_snapshot_digest"] is None:+ return ["git_worktree", "git_revision"]+ if scan["target_snapshot_digest"] == clean_worktree_content_digest():+ return ["git_revision"]+ return ["git_worktree"]+++def requested_scan_paths(scan: sqlite3.Row) -> list[str]:+ if "recipe_json" in scan.keys() and scan["recipe_json"] is not None:+ recipe = json.loads(scan["recipe_json"], parse_constant=reject_non_finite_json)+ target = recipe["target"]+ if target["kind"] == "paths":+ return target["paths"]+ return [scan["scope"]]+++def public_scan_recipe(scan: sqlite3.Row) -> dict[str, Any]:+ recipe = json.loads(scan["recipe_json"], parse_constant=reject_non_finite_json)+ recipe.pop("_codexSecurityFileScopes", None)+ return recipe+++def normalized_path_component(value: str) -> str:+ return normalize("NFC", normalize("NFD", value).casefold())+++def relative_path(value: str) -> PurePosixPath | None:+ path = PurePosixPath(value)+ return None if "\\" in value or path.is_absolute() or ".." in path.parts else path+++def safe_source_path(target: Path, value: str) -> Path | None:+ parsed = relative_path(value)+ if parsed is None:+ return None+ try:+ path = (target / parsed.as_posix()).resolve()+ path.relative_to(target)+ return path+ except (OSError, RuntimeError, ValueError):+ return None+++def offline_git_bytes(repository: Path, *arguments: str) -> bytes | None:+ names = ("GIT_NO_LAZY_FETCH", "GIT_ALLOW_PROTOCOL", "GIT_NO_REPLACE_OBJECTS")+ previous = {name: os.environ.get(name) for name in names}+ os.environ["GIT_NO_LAZY_FETCH"] = "1"+ os.environ["GIT_ALLOW_PROTOCOL"] = ""+ os.environ["GIT_NO_REPLACE_OBJECTS"] = "1"+ try:+ return git_bytes(repository, *arguments)+ finally:+ for name, value in previous.items():+ if value is None:+ os.environ.pop(name, None)+ else:+ os.environ[name] = value+++@cache+def tree_entries(repository: Path, tree: str) -> dict[str, tuple[tuple[str, str, str], ...]] | None:+ content = offline_git_bytes(repository, "ls-tree", "-z", tree)+ if content is None:+ return None+ entries: dict[str, list[tuple[str, str, str]]] = {}+ for record in content.split(b"\0"):+ if not record:+ continue+ metadata, separator, name = record.partition(b"\t")+ fields = metadata.split(b" ")+ if not separator or len(fields) != 3:+ return None+ mode, object_type, raw_object = fields+ object_id = raw_object.decode("ascii")+ if not OBJECT_ID.fullmatch(object_id):+ return None+ kind = (+ "directory"+ if mode == b"040000" and object_type == b"tree"+ else "file"+ if mode in {b"100644", b"100755"} and object_type == b"blob"+ else "other"+ )+ decoded_name = os.fsdecode(name)+ entries.setdefault(normalized_path_component(decoded_name), []).append(+ (decoded_name, kind, object_id)+ )+ return {name: tuple(matches) for name, matches in entries.items()}+++def exact_tree_path(+ repository: Path, tree: str, value: str, *, require_unambiguous: bool = False+) -> tuple[str, str, str] | None:+ path = relative_path(value)+ if path is None:+ return None+ kind, object_id = "directory", tree+ for name in path.parts:+ if kind != "directory":+ return None+ entries = tree_entries(repository, object_id) or {}+ aliases = entries.get(normalized_path_component(name), ())+ if require_unambiguous and len(aliases) != 1:+ return None+ entry = next((entry for entry in aliases if entry[0] == name), None)+ if entry is None:+ return None+ _, kind, object_id = entry+ return path.as_posix(), kind, object_id+++def ordinary_path(metadata: os.stat_result) -> bool:+ return not (+ getattr(metadata, "st_file_attributes", 0)+ & getattr(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0)+ ) and (stat.S_ISDIR(metadata.st_mode) or stat.S_ISREG(metadata.st_mode))+++def filesystem_alias(selected: Path, candidate: Path) -> bool:+ if normalized_path_component(selected.name) != normalized_path_component(candidate.name):+ return False+ try:+ if not all(ordinary_path(path.lstat()) for path in (selected, candidate)):+ return False+ if not selected.samefile(candidate):+ return False+ with os.scandir(selected.parent) as entries:+ return (+ sum(+ normalized_path_component(entry.name)+ == normalized_path_component(selected.name)+ for entry in entries+ )+ == 1+ )+ except OSError:+ return False+++def existing_tree_path(+ repository: Path, tree: str, value: str, filesystem_root: Path+) -> tuple[str, str, str] | None:+ path = relative_path(value)+ if path is None:+ return None+ canonical: list[str] = []+ kind, object_id = "directory", tree+ for index, name in enumerate(path.parts, start=1):+ if kind != "directory":+ return None+ entries = tree_entries(repository, object_id) or {}+ aliases = entries.get(normalized_path_component(name), ())+ selected = filesystem_root / PurePosixPath(*path.parts[:index])+ matches = []+ try:+ if not ordinary_path(selected.lstat()):+ return None+ for entry in aliases:+ candidate = filesystem_root / PurePosixPath(*canonical, entry[0])+ # Every colliding Git name needs a filesystem witness. Missing+ # names cannot establish which entry the user selected.+ if not ordinary_path(candidate.lstat()):+ return None+ if selected.samefile(candidate) and (+ entry[0] == name or filesystem_alias(selected, candidate)+ ):+ matches.append(entry)+ except OSError:+ return None+ if len(matches) != 1:+ return None+ name, kind, object_id = matches[0]+ canonical.append(name)+ return PurePosixPath(*canonical).as_posix(), kind, object_id+++def target_tree(target: Path, revision: str) -> tuple[Path, str] | None:+ repository, prefix = git_worktree_context(target)+ raw_tree = offline_git_bytes(repository, "rev-parse", "--verify", f"{revision}^{{tree}}")+ tree = raw_tree.decode("ascii").strip() if raw_tree is not None else ""+ if not OBJECT_ID.fullmatch(tree):+ return None+ selected = existing_tree_path(repository, tree, prefix, repository)+ if selected is None or selected[1] != "directory":+ return None+ return repository, selected[2]+++def capture_source_scopes(+ target: Path,+ target_identity: tuple[str, str | None, int | str, int | str],+ paths: list[str],+ *,+ diff_target_kind: str | None = None,+) -> dict[str, Any]:+ revision, snapshot = target_identity[:2]+ result: dict[str, Any] = {"version": 1, "revision": revision, "scopes": []}+ # HEAD cannot represent the uncommitted bytes reviewed by a working-tree scan.+ if diff_target_kind == "working_tree":+ return result+ if revision == "unversioned" or (+ snapshot is not None and snapshot != clean_worktree_content_digest()+ ):+ return result+ try:+ repository, _ = git_worktree_context(target)+ if offline_git_bytes(repository, "replace", "--list") != b"":+ # A revision alone does not identify the replacement view scanned.+ return result+ context = target_tree(target, revision)+ if context is None:+ return result+ repository, tree = context+ result["targetTree"] = tree+ for requested in paths:+ selected = safe_source_path(target, requested)+ if selected is None:+ continue+ canonical = existing_tree_path(repository, tree, requested, target)+ if canonical is None or canonical[1] not in {"file", "directory"}:+ continue+ try:+ metadata = selected.stat()+ except OSError:+ continue+ if (canonical[1] == "directory") != stat.S_ISDIR(metadata.st_mode):+ continue+ result["scopes"].append(+ {+ "path": PurePosixPath(requested).as_posix(),+ "canonicalPath": canonical[0],+ "kind": canonical[1],+ "objectId": canonical[2],+ }+ )+ except (OSError, RuntimeError, SystemExit, UnicodeError, ValueError):+ # Excerpts are optional. Failure to bind a scope never widens it and+ # must not prevent the scan itself from starting.+ pass+ return result+++def load_source_scopes(+ scan: sqlite3.Row, target: Path, requested: list[str]+) -> tuple[Path, str, list[dict[str, str]]] | None:+ if "diff_target_kind" in scan.keys() and scan["diff_target_kind"] == "working_tree":+ return None+ saved = scan["source_scopes_json"] if "source_scopes_json" in scan.keys() else None+ paths = {PurePosixPath(path).as_posix() for path in requested}+ if saved is not None:+ metadata = json.loads(saved)+ if (+ not isinstance(metadata, dict)+ or metadata.get("version") != 1+ or metadata.get("revision") != scan["target_revision"]+ ):+ return None+ tree = metadata.get("targetTree")+ records = metadata.get("scopes")+ if (+ not isinstance(tree, str)+ or not OBJECT_ID.fullmatch(tree)+ or not isinstance(records, list)+ ):+ return None+ scopes = [+ record+ for record in records+ if isinstance(record, dict)+ and isinstance(record.get("path"), str)+ and record.get("path") in paths+ and isinstance(record.get("canonicalPath"), str)+ and relative_path(record["canonicalPath"]) is not None+ and record.get("kind") in ("file", "directory")+ and isinstance(record.get("objectId"), str)+ and OBJECT_ID.fullmatch(record["objectId"])+ ]+ return (git_worktree_context(target)[0], tree, scopes) if scopes else None++ # Exact, unambiguous historical entries establish legacy scope kinds.+ # Keep any previously recorded file kinds as an additional constraint.+ recipe = scan["recipe_json"] if "recipe_json" in scan.keys() else None+ legacy_recipe = json.loads(recipe) if recipe else None+ legacy_files = (+ legacy_recipe.get("_codexSecurityFileScopes") if isinstance(legacy_recipe, dict) else None+ )+ repository, _ = git_worktree_context(target)+ if offline_git_bytes(repository, "replace", "--list") != b"":+ # Old records do not say which replacement view was scanned.+ return None+ context = target_tree(target, scan["target_revision"])+ if context is None:+ return None+ repository, tree = context+ scopes = []+ for path in paths:+ entry = exact_tree_path(repository, tree, path, require_unambiguous=True)+ if entry is None or entry[1] not in {"file", "directory"}:+ continue+ kind = entry[1]+ if isinstance(legacy_files, list) and kind != (+ "file" if path in legacy_files else "directory"+ ):+ continue+ scopes.append(+ {+ "path": path,+ "canonicalPath": path,+ "kind": kind,+ "objectId": entry[2],+ }+ )+ return (repository, tree, scopes) if scopes else None+++def source_object_for_path(+ repository: Path, tree: str, target: Path, value: str, scope: dict[str, str]+) -> str | None:+ path = relative_path(value)+ if path is None or safe_source_path(target, value) is None:+ return None+ prefixes = {PurePosixPath(scope[name]).parts for name in ("path", "canonicalPath")}+ suffix: tuple[str, ...] | None = None+ for prefix in sorted(prefixes, key=len, reverse=True):+ if path.parts[: len(prefix)] == prefix:+ suffix = path.parts[len(prefix) :]+ break+ if suffix is None:+ for length in {len(prefix) for prefix in prefixes}:+ if length > len(path.parts) or scope["kind"] == "file" and length != len(path.parts):+ continue+ selected = existing_tree_path(+ repository, tree, PurePosixPath(*path.parts[:length]).as_posix(), target+ )+ if selected == (scope["canonicalPath"], scope["kind"], scope["objectId"]):+ suffix = path.parts[length:]+ break+ if suffix is None:+ return None+ if scope["kind"] == "file":+ return scope["objectId"] if not suffix else None+ relative = PurePosixPath(*suffix).as_posix()+ entry = exact_tree_path(repository, scope["objectId"], relative, require_unambiguous=True)+ if entry is None:+ selected = safe_source_path(target, scope["canonicalPath"])+ if selected is not None:+ entry = existing_tree_path(repository, scope["objectId"], relative, selected)+ return entry[2] if entry is not None and entry[1] == "file" else None+++def main() -> None:+ argparse.ArgumentParser(description=__doc__).parse_args()+++if __name__ == "__main__":+ main()diff --git a/plugins/codex-security/shared/workbench-history-repairs.json b/plugins/codex-security/shared/workbench-history-repairs.jsonindex 951814db6..16feecea4 100644--- a/plugins/codex-security/shared/workbench-history-repairs.json+++ b/plugins/codex-security/shared/workbench-history-repairs.json@@ -1,4 +1,23 @@[+ [+ "-- Move source-scope previews before the shared history repairs.",+ "WITH input AS (SELECT :history AS history)",+ "SELECT 'sql' AS operation,",+ " 'UPDATE schema_migrations SET version = 50 WHERE version = ' || version AS value",+ "FROM schema_migrations",+ "WHERE version IN (34, 40, 41, 42, 43, 44, 48)",+ " AND name = 'persist authorized source excerpt scopes';"+ ],+ [+ "WITH history AS (",+ " SELECT json_extract(value, '$.version') AS version,",+ " json_extract(value, '$.name') AS name FROM json_each(:history)",+ ")",+ "SELECT 'error' AS operation,",+ " 'The Codex Security database has an unsupported source-scope migration history.' AS value",+ "FROM schema_migrations JOIN history USING (version)",+ "WHERE version IN (43, 50) AND schema_migrations.name != history.name;"+ ],["-- mirror and early-moves: relocate numbers before checking profiles.","WITH",diff --git a/plugins/codex-security/shared/workbench-migrations.json b/plugins/codex-security/shared/workbench-migrations.jsonindex 11e368286..29fe39e5c 100644--- a/plugins/codex-security/shared/workbench-migrations.json+++ b/plugins/codex-security/shared/workbench-migrations.json@@ -410,5 +410,10 @@"statements": ["CREATE INDEX finding_issue_receipts_by_destination_issue\nON finding_issue_receipts(destination_key, external_id, finding_id);"]+ },+ {+ "version": 50,+ "name": "persist authorized source excerpt scopes",+ "statements": ["ALTER TABLE scans ADD COLUMN source_scopes_json TEXT;"]}]diff --git a/plugins/codex-security/tests/test_git_inventory_regressions.py b/plugins/codex-security/tests/test_git_inventory_regressions.pyindex 70557a365..1de7926f4 100644--- a/plugins/codex-security/tests/test_git_inventory_regressions.py+++ b/plugins/codex-security/tests/test_git_inventory_regressions.py@@ -117,8 +117,10 @@ def test_excerpt_uses_target_relative_committed_path(tmp_path: Path) -> None:"diff_target_kind": "commit",}assert (- load_script("workbench_source_excerpt").scanned_source_text(scan, nested, "app.py")- == "package version\n"+ load_script("workbench_source_excerpt").finding_source_excerpt(+ scan, nested, [{"path": "app.py", "startLine": 1}], ["."]+ )+ == "1 package version")@@ -134,7 +136,9 @@ def test_working_tree_excerpt_declines_uncommitted_line_content(tmp_path: Path)"diff_content_digest": load_script("workbench_target").worktree_content_digest(target),}assert (- load_script("workbench_source_excerpt").scanned_source_text(scan, target, "README.md")+ load_script("workbench_source_excerpt").finding_source_excerpt(+ scan, target, [{"path": "README.md", "startLine": 1}], ["."]+ )is None)diff --git a/plugins/codex-security/tests/test_workbench_db.py b/plugins/codex-security/tests/test_workbench_db.pyindex e6770a0e0..268a49a74 100644--- a/plugins/codex-security/tests/test_workbench_db.py+++ b/plugins/codex-security/tests/test_workbench_db.py@@ -3016,8 +3016,23 @@ def test_completed_finding_projects_writeup_and_poc_artifact_paths(@pytest.mark.parametrize("nested", [False, True])+ "long_path",+ [+ False,+ pytest.param(+ True,+ marks=pytest.mark.skipif(+ sys.platform == "darwin",+ reason="macOS cannot represent the real Git pathname beyond its 1024-byte path limit",+ ),+ ),+ ],+ ids=["short-control", "long-real-git-path"],+)def test_workbench_populates_clean_git_scan_revision_with_large_source_excerpt(tmp_path: Path,+ long_path: bool,nested: bool,) -> None:state_dir = tmp_path / "state"@@ -3028,12 +3043,17 @@ def test_workbench_populates_clean_git_scan_revision_with_large_source_excerpt((repository / "README.md").write_text("different root source\n" * 50)target = repository / "nested"target.mkdir()- (target / "README.md").write_text(+ relative_path = (+ "/".join(["segment" + "a" * 178] * 12 + ["README.md"]) if long_path else "README.md"+ )+ source_path = target / relative_path+ source_path.parent.mkdir(parents=True, exist_ok=True)+ source_path.write_text("\n".join(f"source line {line_number}" for line_number in range(1, 51))+ "\n"+ "x" * (1024 * 1024))- subprocess.run(["git", "add", "."], cwd=repository, check=True)+ subprocess.run(["git", "-c", "core.longpaths=true", "add", "."], cwd=repository, check=True)subprocess.run(["git", "commit", "-qm", "Add source fixture"], cwd=repository, check=True)revision = subprocess.run(["git", "rev-parse", "HEAD"],@@ -3051,7 +3071,7 @@ def test_workbench_populates_clean_git_scan_revision_with_large_source_excerpt(Path(str(started["results"]["scanDir"])),scan_id,target,- relative_path="README.md",+ relative_path=relative_path,target_kind="git_revision",target_revision="wrong-revision",)@@ -3062,16 +3082,32 @@ def test_workbench_populates_clean_git_scan_revision_with_large_source_excerpt()assert manifest["scan"]["target"]["revision"] == revisionassert "snapshotDigest" not in manifest["scan"]["target"]- excerpt = completed["scan"]["findings"][0]["sourceExcerpt"]- assert excerpt.startswith("38 source line 38")- assert "41 source line 41" in excerpt- assert excerpt.endswith("47 source line 47")+ finding = completed["scan"]["findings"][0]+ saved_findings = json.loads(+ (Path(str(started["results"]["scanDir"])) / "findings.json").read_text()+ )+ assert saved_findings["findings"][0]["locations"][0]["path"] == relative_path+ if long_path:+ assert len(relative_path.encode()) > 2048+ assert finding["locations"][0]["path"] != relative_path+ assert "sourceExcerpt" not in finding+ else:+ assert finding["locations"][0]["path"] == relative_path+ excerpt = finding["sourceExcerpt"]+ assert excerpt.startswith("38 source line 38")+ assert "41 source line 41" in excerpt+ assert excerpt.endswith("47 source line 47")- (target / "README.md").write_text("replacement source\n")- subprocess.run(["git", "add", "README.md"], cwd=target, check=True)+ source_path.write_text("replacement source\n")+ subprocess.run(+ ["git", "-c", "core.longpaths=true", "add", "--", relative_path], cwd=target, check=True+ )subprocess.run(["git", "commit", "-qm", "Replace source fixture"], cwd=target, check=True)refreshed = get_scan(state_dir, scan_id)- assert refreshed["scan"]["findings"][0]["sourceExcerpt"] == excerpt+ if long_path:+ assert "sourceExcerpt" not in refreshed["scan"]["findings"][0]+ else:+ assert refreshed["scan"]["findings"][0]["sourceExcerpt"] == excerptdef commit_source_fixture(target: Path, source: bytes) -> str:@@ -3108,6 +3144,7 @@ def test_source_excerpt_breaks_lines_only_at_newlines(tmp_path: Path, separator:{"target_revision": revision, "target_snapshot_digest": None, "diff_target_kind": None},target,[{"path": "README.md", "startLine": 5, "endLine": 5}],+ ["."],)assert excerpt == "\n".join(@@ -3130,6 +3167,7 @@ def test_source_excerpt_numbers_standard_line_endings(tmp_path: Path, line_endin{"target_revision": revision, "target_snapshot_digest": None, "diff_target_kind": None},target,[{"path": "README.md", "startLine": 5, "endLine": 5}],+ ["."],)assert excerpt == "\n".join(@@ -3163,6 +3201,7 @@ def test_source_excerpt_preserves_final_lines(scan,target,[{"path": "README.md", "startLine": len(expected_lines)}],+ ["."],)assert excerpt == "\n".join(@@ -3173,6 +3212,7 @@ def test_source_excerpt_preserves_final_lines(scan,target,[{"path": "README.md", "startLine": len(expected_lines) + 1}],+ ["."],)is None)diff --git a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py b/plugins/codex-security/tests/test_workbench_setup_and_migrations.pyindex 64736fc12..edd2ebd3b 100644--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py@@ -91,6 +91,7 @@ EXPECTED_MIGRATIONS = [(47, "snapshot deep scan discovery context"),(48, "share finding issue receipts across trackers"),(49, "index finding issue receipt ownership"),+ (50, "persist authorized source excerpt scopes"),]@@ -2352,6 +2353,114 @@ def test_workbench_reconciles_legacy_execution_profile_migrations(assert_new_scan_model_is_independent(state_dir, target, tmp_path / "current-scans")[email protected]("installed_history", ["main-scan-names", "source-excerpt-scopes"])+def test_workbench_preserves_both_installed_migration_42_histories(+ tmp_path: Path, installed_history: str+) -> None:+ state_dir = tmp_path / "state"+ database = state_dir / "workbench.sqlite3"+ database.parent.mkdir()+ namespace = runpy.run_path(str(SCRIPT), run_name="codex_security_workbench_schema")+ timestamp = "2026-08-01T00:00:00Z"+ installed = [+ (42 if name == "persist authorized source excerpt scopes" else version, name, sql)+ for version, name, sql in namespace["MIGRATIONS"]+ if name+ != (+ "persist authorized source excerpt scopes"+ if installed_history == "main-scan-names"+ else "editable scan names"+ )+ ]+ with sqlite3.connect(database) as connection:+ create_migration_history(connection)+ apply_historical_migrations(connection, installed, timestamp)+ original = dict(connection.execute("SELECT name, applied_at FROM schema_migrations"))++ run_workbench(state_dir, "database-info")+ with sqlite3.connect(database) as connection:+ first = connection.execute(+ "SELECT version, name, applied_at FROM schema_migrations ORDER BY version"+ ).fetchall()+ assert [(version, name) for version, name, _ in first] == EXPECTED_MIGRATIONS+ assert all(+ {name: applied_at for _, name, applied_at in first}[name] == value+ for name, value in original.items()+ )+ assert {"name", "source_scopes_json"} <= {+ row[1] for row in connection.execute("PRAGMA table_info(scans)")+ }+ assert connection.execute("PRAGMA foreign_key_check").fetchall() == []++ run_workbench(state_dir, "database-info")+ with sqlite3.connect(database) as connection:+ assert (+ connection.execute(+ "SELECT version, name, applied_at FROM schema_migrations ORDER BY version"+ ).fetchall()+ == first+ )++[email protected]("preview_version", [34, 40, 41, 42, 43, 44, 48])+def test_workbench_upgrades_pre_release_source_scope_migration(+ tmp_path: Path, preview_version: int+) -> None:+ state_dir = tmp_path / "state"+ database = state_dir / "workbench.sqlite3"+ database.parent.mkdir()+ namespace = runpy.run_path(str(SCRIPT), run_name="codex_security_workbench_schema")+ migrations = namespace["MIGRATIONS"]+ source_scope = next(+ migration+ for migration in migrations+ if migration[1] == "persist authorized source excerpt scopes"+ )+ with sqlite3.connect(database) as connection:+ connection.execute(+ "CREATE TABLE schema_migrations ("+ "version INTEGER PRIMARY KEY, name TEXT NOT NULL, applied_at TEXT NOT NULL)"+ )+ for version, name, sql in migrations:+ if version > (42 if preview_version == 43 else 32):+ continue+ for statement in SCHEMA.sql_statements(sql):+ connection.execute(statement)+ connection.execute(+ "INSERT INTO schema_migrations VALUES (?, ?, ?)",+ (version, name, "2026-08-01T00:00:00Z"),+ )+ for statement in SCHEMA.sql_statements(source_scope[2]):+ connection.execute(statement)+ connection.execute(+ "INSERT INTO schema_migrations VALUES (?, ?, ?)",+ (preview_version, source_scope[1], "2026-08-01T00:00:00Z"),+ )++ run_workbench(state_dir, "database-info")++ with sqlite3.connect(database) as connection:+ assert connection.execute(+ "SELECT name FROM schema_migrations WHERE version = 34"+ ).fetchone() == ("associate findings with repositories",)+ assert connection.execute(+ "SELECT version, applied_at FROM schema_migrations WHERE name = ?",+ (source_scope[1],),+ ).fetchone() == (50, "2026-08-01T00:00:00Z")+ assert connection.execute(+ "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'finding_repositories'"+ ).fetchone() == ("finding_repositories",)+ assert connection.execute(+ "SELECT name FROM schema_migrations WHERE version = 43"+ ).fetchone() == ("preserve severity assessments per scan",)+ assert connection.execute("SELECT COUNT(*) FROM scan_severity_assessments").fetchone() == (+ 0,+ )+ assert "source_scopes_json" in {+ row[1] for row in connection.execute("PRAGMA table_info(scans)")+ }++def test_workbench_upgrades_released_database_schema(tmp_path: Path) -> None:state_dir = tmp_path / "state"database = state_dir / "workbench.sqlite3"diff --git a/sdk/typescript/README.md b/sdk/typescript/README.mdindex 50bd7fa14..a537ca694 100644--- a/sdk/typescript/README.md+++ b/sdk/typescript/README.md@@ -194,6 +194,11 @@ Review coverage alongside findings. A report with no findings does not establishthat the repository is secure. Reports and logs can contain source code,vulnerability details, and credentials; review them before sharing.+Saved-history excerpts use scanned Git objects without fetching missing data.+Working-tree diff scans omit excerpts, as do historical scans whose source or+scope cannot be established. Unambiguous older scans remain readable. An omitted+excerpt does not remove the finding.+The SDK does not set your process's exit status. Use `hasFindingsAtOrAbove()`as in the example to enforce a severity threshold.diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.tsindex 06dca91db..9e0f99929 100644--- a/sdk/typescript/tests-ts/api.test.ts+++ b/sdk/typescript/tests-ts/api.test.ts@@ -2499,7 +2499,7 @@ describe("CodexSecurity orchestration", () => {);test.each([false, true])(- "archives accepted output before starting, cancellation=%j",+ "archives accepted output before starting, cancellation=%p",async (cancelRegistration) => {const root = await temporaryDirectory();const repository = join(root, "repository");@@ -2754,10 +2754,9 @@ describe("CodexSecurity orchestration", () => {scanPrompt: "Review café boundaries.\nPreserve the second line.",})).catch((error: unknown) => error);- let socket: Socket | undefined;let closing: Promise<void> | undefined;try {- [socket] = (await Promise.race([+ const [socket] = (await Promise.race([connected,operation.then((error) => {throw error;@@ -2769,21 +2768,25 @@ describe("CodexSecurity orchestration", () => {expect(await readFile(join(root, "registration-input.json"), "utf8"),).toBe(submitted!);- // Terminating the paused child can reset its socket on Windows.- // Still wait for close, and retain unexpected errors and the deadline.+ // Terminating the paused child can reset its socket on Windows. Still+ // wait for close, and preserve the deadline and other socket errors.+ deadline.throwIfAborted();const closed = new Promise<void>((resolve, reject) => {+ const finish = (error?: unknown) => {+ socket.off("close", onClose);+ socket.off("error", onError);+ deadline.removeEventListener("abort", onAbort);+ if (error === undefined) resolve();+ else reject(error);+ };+ const onClose = () => finish();const onError = (error: NodeJS.ErrnoException) => {- if (error.code !== "ECONNRESET") reject(error);+ if (error.code !== "ECONNRESET") finish(error);};- const onAbort = () => reject(deadline.reason);- socket!.once("error", onError);- socket!.once("close", () => {- socket!.removeListener("error", onError);- deadline.removeEventListener("abort", onAbort);- resolve();- });- if (deadline.aborted) onAbort();- else deadline.addEventListener("abort", onAbort, { once: true });+ const onAbort = () => finish(deadline.reason);+ socket.once("close", onClose);+ socket.on("error", onError);+ deadline.addEventListener("abort", onAbort, { once: true });});if (cancel === "close") closing = client.close();else controller.abort();diff --git a/sdk/typescript/tests-ts/finding-preview.test.ts b/sdk/typescript/tests-ts/finding-preview.test.tsindex 3fcb319cc..5a4af3a37 100644--- a/sdk/typescript/tests-ts/finding-preview.test.ts+++ b/sdk/typescript/tests-ts/finding-preview.test.ts@@ -1,7 +1,174 @@import { join } from "node:path";-import { expect, test } from "bun:test";+import { fileURLToPath } from "node:url";+import { describe, expect, test } from "bun:test";import { PLUGIN_ROOT } from "./plugin-root.js";+function sourceScopeProbe(scenario: string): Record<string, unknown> {+ const python = Bun.which("python3") ?? Bun.which("python") ?? Bun.which("py");+ expect(python).not.toBeNull();+ const result = Bun.spawnSync(+ [+ python!,+ "-I",+ "-B",+ fileURLToPath(new URL("./fixtures/source_scopes.py", import.meta.url)),+ join(PLUGIN_ROOT, "scripts"),+ scenario,+ ],+ { stdout: "pipe", stderr: "pipe" },+ );+ expect(result.exitCode, new TextDecoder().decode(result.stderr)).toBe(0);+ return JSON.parse(new TextDecoder().decode(result.stdout)) as Record<+ string,+ unknown+ >;+}++describe("bundled finding previews", () => {+ test("reads only the immutable source objects selected for the scan", () => {+ expect(sourceScopeProbe("boundaries")).toEqual({+ selected: "1 public source",+ outside: null,+ additional: "1 selected file",+ repository: "1 private source",+ fileDescendant: null,+ traversal: null,+ absolute: null,+ redirected: null,+ escaped: null,+ legacyScoped: "1 public source",+ legacyUnmarkedFile: "1 selected file",+ legacyUnmarkedFileDescendant: null,+ legacyRoot: "1 private source",+ legacyKnownDirectory: "1 public source",+ legacyKnownFile: "1 selected file",+ legacyFileDescendant: null,+ emptyAuthority: null,+ dirty: null,+ fallback: "1 public source",+ rootControl: "1 public source",+ replacedFile: "1 selected file",+ replacedFileDescendant: null,+ removedDirectory: "1 nested source",+ offline: "1 public source",+ missingObject: null,+ });+ }, 30_000);++ test("keeps captured filesystem aliases usable after their source paths disappear", () => {+ expect(sourceScopeProbe("aliases")).toEqual({+ case: expect.any(Boolean),+ unicode: expect.any(Boolean),+ nonAscii: expect.any(Boolean),+ collisionChecked: true,+ });+ }, 30_000);++ test("requires distinct filesystem witnesses for colliding descendants", () => {+ expect(sourceScopeProbe("descendant_aliases")).toEqual({+ fileAndDirectoryCollisionsChecked: true,+ missingWitnessesOmitted: true,+ });+ }, 30_000);++ test("keeps saved immutable objects readable after replacement refs change", () => {+ expect(sourceScopeProbe("replacements")).toEqual({+ savedObjectsUnchanged: true,+ newReplacementViewOmitted: true,+ });+ }, 30_000);++ test("does not run working-tree filters when registering a committed-ref scan", () => {+ expect(sourceScopeProbe("replacement_filters")).toEqual({+ workingTreeFilterNotRun: true,+ registrationRecipeUnchanged: true,+ });+ }, 30_000);++ test("omits source authority when replacement refs changed the scanned tree", () => {+ expect(sourceScopeProbe("replacement_snapshot")).toEqual({+ mismatchedCaptureOmitted: true,+ ambiguousLegacyViewOmitted: true,+ });+ }, 30_000);++ test("omits stale committed excerpts for working-tree diff scans", () => {+ expect(sourceScopeProbe("working_tree_excerpt")).toEqual({+ authorityOmitted: true,+ currentExcerptOmitted: true,+ legacyExcerptOmitted: true,+ rangeExcerptPreserved: true,+ });+ }, 30_000);++ test("indexes Git names once for large explicit scope lists", () => {+ expect(sourceScopeProbe("indexed_scopes")).toEqual({+ selected: 256,+ linearNormalization: true,+ });+ }, 30_000);++ test("selects source excerpts from the displayed finding locations", () => {+ expect(sourceScopeProbe("display_locations")).toEqual({+ displayed: 8,+ excerptUsesDisplayedLocation: true,+ });+ }, 30_000);++ test("does not grant source scope through a selected directory link", () => {+ expect(sourceScopeProbe("selected_redirects")).toEqual({+ selectedLinkOmitted: true,+ linkedAncestorOmitted: true,+ registrationRecipeUnchanged: true,+ directSelectionPreserved: true,+ });+ }, 30_000);++ test("rejects unsafe finding locations before invoking Git", () => {+ expect(sourceScopeProbe("unsafe_locations")).toEqual({+ unsafePathsRejectedBeforeGit: true,+ invalidLocationRejectedBeforeGit: true,+ });+ }, 30_000);++ test("does not treat links or reparse points as filesystem alias evidence", () => {+ expect(sourceScopeProbe("alias_evidence")).toEqual({+ ordinary: true,+ hardlink: false,+ symlink: false,+ reparse: false,+ });+ }, 30_000);++ test("keeps subdirectory and linked-worktree targets bound to their selected tree", () => {+ expect(sourceScopeProbe("worktrees")).toEqual({+ subdirectoryBound: true,+ linkedWorktreeBound: true,+ });+ }, 30_000);++ test.each(["workspace", "prompt", "headless", "deep", "cli"])(+ "records source authority through the %s scan-start path",+ (writer) => {+ expect(sourceScopeProbe(`writer_${writer}`)).toEqual({+ writer,+ sourceAuthorityRecorded: true,+ launchRecipeUnchanged: true,+ legacyExactScopesPreserved: true,+ });+ },+ 30_000,+ );++ test("preserves legacy scans and separately owned migration history", () => {+ expect(sourceScopeProbe("migration")).toEqual({+ legacyAuthorityUnset: true,+ otherMigrationsPreserved: true,+ conflictRejected: true,+ });+ }, 30_000);+});+test("bundled finding previews normalize details without changing stored findings", () => {const python = Bun.which("python3") ?? Bun.which("python") ?? Bun.which("py");expect(python).not.toBeNull();diff --git a/sdk/typescript/tests-ts/fixtures/source_scopes.py b/sdk/typescript/tests-ts/fixtures/source_scopes.pynew file mode 100644index 000000000..d941f3bc5--- /dev/null+++ b/sdk/typescript/tests-ts/fixtures/source_scopes.py@@ -0,0 +1,1038 @@+"""Synthetic Git fixtures for finding-source authorization tests."""++from __future__ import annotations++import json+import os+import shlex+import shutil+import sqlite3+import stat+import subprocess+import sys+import tempfile+import unicodedata+import uuid+from contextlib import closing, nullcontext+from pathlib import Path+from types import SimpleNamespace+from unittest.mock import patch++os.environ.pop("GIT_NO_REPLACE_OBJECTS", None)+os.environ.pop("GIT_REPLACE_REF_BASE", None)++sys.path.insert(0, sys.argv[1])+import workbench_source_excerpt as excerpts+import workbench_source_scopes as scopes+from workbench_target import clean_worktree_content_digest+++def git(repository: Path, *arguments: str, input_data: bytes | None = None) -> str:+ environment = dict(+ os.environ, GIT_CONFIG_GLOBAL=os.devnull, GIT_CONFIG_NOSYSTEM="1"+ )+ result = subprocess.run(+ [+ "git",+ "-c",+ "user.name=Example",+ "-c",+ "[email protected]",+ "-c",+ "commit.gpgsign=false",+ "-C",+ str(repository),+ *arguments,+ ],+ input=input_data,+ stdout=subprocess.PIPE,+ stderr=subprocess.PIPE,+ env=environment,+ check=True,+ )+ return result.stdout.decode().strip()+++def write(repository: Path, name: str, content: str) -> None:+ path = repository / name+ path.parent.mkdir(parents=True, exist_ok=True)+ path.write_text(content)+++def commit(repository: Path) -> str:+ git(repository, "add", "--all")+ git(repository, "commit", "--quiet", "-m", "Synthetic fixture")+ return git(repository, "rev-parse", "HEAD")+++def scan(repository: Path, revision: str, paths: list[str]) -> dict:+ identity = (revision, clean_worktree_content_digest(), 0, 0)+ return {+ "target_revision": revision,+ "target_snapshot_digest": identity[1],+ "source_scopes_json": json.dumps(+ scopes.capture_source_scopes(repository, identity, paths)+ ),+ "recipe_json": None,+ }+++def excerpt(+ record: dict, repository: Path, path: str, selected: list[str]+) -> str | None:+ return excerpts.finding_source_excerpt(+ record, repository, [{"path": path, "startLine": 1}], selected+ )+++def register_recipe(+ repository: Path, recipe: dict, environment: dict[str, str] | None = None+) -> tuple[dict, dict]:+ state = repository.parent / "state"+ scan_dir = Path(tempfile.mkdtemp(prefix="cli-scan-", dir=repository.parent))+ result = subprocess.run(+ [+ sys.executable,+ "-I",+ "-B",+ str(Path(sys.argv[1]) / "workbench_db.py"),+ "register-cli-scan",+ "--repository",+ str(repository),+ "--scan-dir",+ str(scan_dir),+ "--recipe-json",+ json.dumps(recipe),+ ],+ env={+ **os.environ,+ **(environment or {}),+ "CODEX_SECURITY_STATE_DIR": str(state),+ },+ text=True,+ capture_output=True,+ check=True,+ )+ registered = json.loads(result.stdout)+ with closing(sqlite3.connect(state / "workbench.sqlite3")) as connection:+ connection.row_factory = sqlite3.Row+ record = dict(+ connection.execute(+ "SELECT * FROM scans WHERE id = ?", (registered["scanId"],)+ ).fetchone()+ )+ return registered, record+++def directory_link(target: Path, link: Path) -> None:+ if os.name == "nt":+ subprocess.run(+ [+ "node",+ "-e",+ "require('node:fs').symlinkSync(process.argv[1], process.argv[2], 'junction')",+ str(target),+ str(link),+ ],+ check=True,+ capture_output=True,+ )+ else:+ link.symlink_to(target, target_is_directory=True)+++def replacements(repository: Path) -> dict:+ write(repository, "selected.py", "selected source\n")+ write(repository, "private.py", "private source\n")+ write(repository, "selected/public.py", "selected directory\n")+ write(repository, "private/public.py", "private directory\n")+ revision = commit(repository)+ file_record = scan(repository, revision, ["selected.py"])+ directory_record = scan(repository, revision, ["selected"])+ file_object = git(repository, "rev-parse", "HEAD:selected.py")+ private_object = git(repository, "rev-parse", "HEAD:private.py")+ tree_object = git(repository, "rev-parse", "HEAD:selected")+ private_tree = git(repository, "rev-parse", "HEAD:private")+ git(repository, "replace", file_object, private_object)+ git(repository, "replace", tree_object, private_tree)+ scopes.tree_entries.cache_clear()+ assert (+ excerpt(file_record, repository, "selected.py", ["selected.py"])+ == "1 selected source"+ )+ assert (+ excerpt(directory_record, repository, "selected/public.py", ["selected"])+ == "1 selected directory"+ )+ scopes.tree_entries.cache_clear()+ captured = scan(repository, revision, ["selected.py", "selected"])+ assert json.loads(captured["source_scopes_json"])["scopes"] == []+ return {"savedObjectsUnchanged": True, "newReplacementViewOmitted": True}+++def replacement_filters(repository: Path) -> dict:+ write(repository, "tracked.py", "synthetic source\n")+ write(repository, ".gitattributes", "tracked.py filter=scope_probe\n")+ revision = commit(repository)+ first = git(repository, "hash-object", "-w", "--stdin", input_data=b"first\n")+ second = git(repository, "hash-object", "-w", "--stdin", input_data=b"second\n")+ git(repository, "replace", first, second)+ marker = repository.parent / "filter-executed"+ helper = repository.parent / "filter.py"+ helper.write_text(+ "import os,sys\n"+ "from pathlib import Path\n"+ "Path(os.environ['SYNTHETIC_FILTER_MARKER']).write_text(os.environ.get('GIT_CONFIG_VALUE_0',''))\n"+ "sys.stdout.buffer.write(sys.stdin.buffer.read())\n"+ )+ git(+ repository,+ "config",+ "filter.scope_probe.clean",+ " ".join(+ shlex.quote(Path(path).as_posix()) for path in (sys.executable, helper)+ ),+ )+ os.utime(repository / "tracked.py", (0, 0))+ recipe = {+ "config": {},+ "mode": "standard",+ "repository": str(repository),+ "target": {"kind": "refs", "paths": [], "base": revision, "head": revision},+ }+ _, record = register_recipe(+ repository,+ recipe,+ {+ "GIT_CONFIG_COUNT": "1",+ "GIT_CONFIG_KEY_0": "http.extraHeader",+ "GIT_CONFIG_VALUE_0": "SYNTHETIC_GIT_CREDENTIAL",+ "SYNTHETIC_FILTER_MARKER": str(marker),+ "GIT_CONFIG_GLOBAL": os.devnull,+ "GIT_CONFIG_NOSYSTEM": "1",+ },+ )+ assert not marker.exists(), "source capture executed a working-tree filter"+ assert json.loads(record["source_scopes_json"])["scopes"] == []+ assert json.loads(record["recipe_json"]) == recipe+ return {"workingTreeFilterNotRun": True, "registrationRecipeUnchanged": True}+++def replacement_snapshot(repository: Path) -> dict:+ from workbench_scan_start import scan_target_identity++ write(repository, "src/public.py", "historical source\n")+ write(repository, "src/removed.py", "historical-only source\n")+ historical = commit(repository)+ write(repository, "src/public.py", "scanned source\n")+ (repository / "src/removed.py").unlink()+ replacement = commit(repository)+ git(repository, "reset", "--hard", historical)+ git(repository, "replace", historical, replacement)+ git(repository, "reset", "--hard", historical)+ assert git(repository, "status", "--porcelain") == ""+ assert not (repository / "src/removed.py").exists()+ identity = scan_target_identity(repository, None)+ assert identity[1] == clean_worktree_content_digest()+ authority = scopes.capture_source_scopes(repository, identity, ["src"])+ assert authority["scopes"] == []+ record = {+ "target_revision": identity[0],+ "target_snapshot_digest": identity[1],+ "source_scopes_json": json.dumps(authority),+ "recipe_json": None,+ }+ assert excerpt(record, repository, "src/removed.py", ["src"]) is None+ legacy = {**record, "source_scopes_json": None}+ assert excerpt(legacy, repository, "src/removed.py", ["."]) is None+ git(repository, "replace", "--delete", historical)+ with patch.dict(+ os.environ, {"GIT_REPLACE_REF_BASE": "refs/synthetic-replacements/"}+ ):+ git(repository, "replace", historical, replacement)+ git(repository, "reset", "--hard", historical)+ assert (+ scopes.capture_source_scopes(+ repository, scan_target_identity(repository, None), ["src"]+ )["scopes"]+ == []+ )+ assert excerpt(legacy, repository, "src/removed.py", ["."]) is None+ return {"mismatchedCaptureOmitted": True, "ambiguousLegacyViewOmitted": True}+++def indexed_scopes(repository: Path) -> dict:+ selected = [f"source-{index}.py" for index in range(256)]+ for name in selected:+ write(repository, name, "synthetic source\n")+ revision = commit(repository)+ scopes.tree_entries.cache_clear()+ normalize = scopes.normalized_path_component+ calls = 0++ def counted(value: str) -> str:+ nonlocal calls+ calls += 1+ return normalize(value)++ with patch.object(scopes, "normalized_path_component", counted):+ record = scan(repository, revision, selected)+ assert len(json.loads(record["source_scopes_json"])["scopes"]) == len(selected)+ assert calls <= 3 * len(selected), calls+ return {"selected": len(selected), "linearNormalization": True}+++def display_locations(repository: Path) -> dict:+ import workbench_db as db+ from filesystem_identity import serialize_filesystem_identity++ selected = [f"selected/location{index}.py" for index in range(9)]+ for index, name in enumerate(selected):+ write(repository, name, f"source {index}\n")+ record = {+ **scan(repository, commit(repository), ["selected"]),+ "id": "synthetic-scan",+ "started_at": "2026-08-01T00:00:00Z",+ "scan_dir": str(repository.parent / "scan"),+ "target_path": str(repository),+ "target_inode": serialize_filesystem_identity(repository.stat().st_ino),+ "scope": "selected",+ }+ locations = [+ {+ "path": name,+ "startLine": 1,+ "endLine": 1,+ "role": "evidence:root_control" if index == 8 else "evidence",+ }+ for index, name in enumerate(selected)+ ]+ occurrence = {+ "id": "synthetic-occurrence",+ "finding_id": "synthetic-finding",+ "details_json": json.dumps({"locations": locations}),+ "confidence": "high",+ "created_at": record["started_at"],+ "remediation": "Synthetic remediation",+ "severity": "low",+ "summary": "Synthetic summary",+ "title": "Synthetic finding",+ }+ connection = sqlite3.connect(":memory:")+ connection.row_factory = sqlite3.Row+ connection.execute(+ "CREATE TABLE finding_locations (occurrence_id TEXT, relative_path TEXT, "+ "start_line INTEGER, end_line INTEGER, role TEXT, sort_order INTEGER)"+ )+ connection.executemany(+ "INSERT INTO finding_locations VALUES (?, ?, ?, ?, ?, ?)",+ [+ (occurrence["id"], item["path"], 1, 1, item["role"], index)+ for index, item in enumerate(locations)+ ],+ )+ with (+ patch.object(db, "finding_remediation_result", return_value=None),+ patch.object(db, "finding_triage_result", return_value=None),+ patch.object(db.scan_history, "finding_matches", return_value=([], None, [])),+ ):+ result = db.finding_result(connection, record, occurrence, related=[])+ assert [item["path"] for item in result["locations"]] == selected[:8]+ assert result["sourceExcerpt"] == "1 source 0"+ connection.close()+ return {"displayed": 8, "excerptUsesDisplayedLocation": True}+++def selected_redirects(repository: Path) -> dict:+ write(repository, ".gitignore", "/selected/link\n")+ write(repository, "selected/public.py", "selected source\n")+ write(repository, "private/secret.py", "private source\n")+ revision = commit(repository)+ directory_link(repository / "private", repository / "selected/link")+ assert git(repository, "status", "--porcelain") == ""++ requested = ["selected/link"]+ recipe = {+ "config": {},+ "mode": "standard",+ "repository": str(repository),+ "target": {"kind": "paths", "paths": requested},+ }+ registered, record = register_recipe(repository, recipe)+ assert registered["contract"]["scope"]["requiredIncludePaths"] == requested+ assert record["target_snapshot_digest"] == clean_worktree_content_digest()+ assert excerpt(record, repository, "private/secret.py", requested) is None+ assert json.loads(record["source_scopes_json"])["scopes"] == []+ assert json.loads(record["recipe_json"]) == recipe++ descendant = ["selected/link/secret.py"]+ record = scan(repository, revision, descendant)+ assert json.loads(record["source_scopes_json"])["scopes"] == []+ assert excerpt(record, repository, "private/secret.py", descendant) is None+ direct = scan(repository, revision, ["private"])+ assert (+ excerpt(direct, repository, "private/secret.py", ["private"])+ == "1 private source"+ )+ return {+ "selectedLinkOmitted": True,+ "linkedAncestorOmitted": True,+ "registrationRecipeUnchanged": True,+ "directSelectionPreserved": True,+ }+++def working_tree_excerpt(repository: Path) -> dict:+ write(repository, "source.py", "old committed source\n")+ revision = commit(repository)+ write(repository, "source.py", "new working-tree source\n")+ recipe = {+ "config": {},+ "mode": "standard",+ "repository": str(repository),+ "target": {+ "kind": "working_tree",+ "paths": [],+ "base": revision,+ "head": revision,+ },+ }+ _, record = register_recipe(repository, recipe)+ metadata = json.loads(record["source_scopes_json"])+ historical = {**record, "source_scopes_json": None}+ range_recipe = {+ **recipe,+ "target": {+ "kind": "refs",+ "paths": [],+ "base": revision,+ "head": revision,+ },+ }+ _, range_record = register_recipe(repository, range_recipe)+ return {+ "authorityOmitted": metadata["scopes"] == [],+ "currentExcerptOmitted": excerpt(record, repository, "source.py", ["."])+ is None,+ "legacyExcerptOmitted": excerpt(historical, repository, "source.py", ["."])+ is None,+ "rangeExcerptPreserved": excerpt(+ range_record, repository, "source.py", ["."]+ )+ == "1 old committed source",+ }+++def unsafe_locations(repository: Path) -> dict:+ write(repository, "source.py", "selected source\n")+ record = scan(repository, commit(repository), ["."])+ outside = repository.parent / "outside"+ outside.mkdir()+ write(outside, "source.py", "outside source\n")+ directory_link(outside, repository / "escaped")+ with patch.object(+ scopes, "git_worktree_context", side_effect=AssertionError("Git was invoked")+ ):+ for path in (+ "../outside/source.py",+ str(outside / "source.py"),+ "escaped/source.py",+ ):+ assert excerpt(record, repository, path, ["."]) is None+ assert (+ excerpts.finding_source_excerpt(+ record,+ repository,+ [{"path": "source.py", "startLine": "invalid"}],+ ["."],+ )+ is None+ )+ return {+ "unsafePathsRejectedBeforeGit": True,+ "invalidLocationRejectedBeforeGit": True,+ }+++def boundaries(repository: Path) -> dict:+ for name, content in {+ "src/public.py": "public source\n",+ "src/support.py": "support source\n",+ "src/nested/child.py": "nested source\n",+ "private/secret.py": "private source\n",+ "selected.py": "selected file\n",+ "other.py": "other file\n",+ }.items():+ write(repository, name, content)+ linked = False+ try:+ (repository / "src/redirected").symlink_to(+ repository / "private", target_is_directory=True+ )+ (repository / "src/escaped").symlink_to(+ repository.parent, target_is_directory=True+ )+ linked = True+ except OSError:+ # Some platforms or environments do not support these optional symlinks.+ pass+ revision = commit(repository)+ selected = scan(repository, revision, ["src"])+ single = scan(repository, revision, ["selected.py"])+ multiple = scan(repository, revision, ["src", "selected.py"])+ whole = scan(repository, revision, ["."])+ legacy = {+ "target_revision": revision,+ "target_snapshot_digest": None,+ "recipe_json": None,+ }+ legacy_kinds = {+ **legacy,+ "recipe_json": json.dumps({"_codexSecurityFileScopes": ["selected.py"]}),+ }+ result = {+ "selected": excerpt(selected, repository, "src/public.py", ["src"]),+ "outside": excerpt(selected, repository, "private/secret.py", ["src"]),+ "additional": excerpt(+ multiple, repository, "selected.py", ["src", "selected.py"]+ ),+ "repository": excerpt(whole, repository, "private/secret.py", ["."]),+ "fileDescendant": excerpt(+ single, repository, "selected.py/private.py", ["selected.py"]+ ),+ "traversal": excerpt(selected, repository, "src/../private/secret.py", ["src"]),+ "absolute": excerpt(+ selected, repository, str(repository / "src/public.py"), ["src"]+ ),+ "redirected": excerpt(selected, repository, "src/redirected/secret.py", ["src"])+ if linked+ else None,+ "escaped": excerpt(selected, repository, "src/escaped/secret.py", ["src"])+ if linked+ else None,+ "legacyScoped": excerpt(legacy, repository, "src/public.py", ["src"]),+ "legacyUnmarkedFile": excerpt(+ legacy, repository, "selected.py", ["selected.py"]+ ),+ "legacyUnmarkedFileDescendant": excerpt(+ legacy, repository, "selected.py/private.py", ["selected.py"]+ ),+ "legacyRoot": excerpt(legacy, repository, "private/secret.py", ["."]),+ "legacyKnownDirectory": excerpt(+ legacy_kinds, repository, "src/public.py", ["src"]+ ),+ "legacyKnownFile": excerpt(+ legacy_kinds, repository, "selected.py", ["selected.py"]+ ),+ "legacyFileDescendant": excerpt(+ legacy_kinds, repository, "selected.py/private.py", ["selected.py"]+ ),+ "emptyAuthority": excerpt(+ {+ **selected,+ "source_scopes_json": json.dumps(+ {"version": 1, "revision": revision, "scopes": []}+ ),+ },+ repository,+ "src/public.py",+ ["src"],+ ),+ "dirty": excerpt(+ {**selected, "target_snapshot_digest": "dirty"},+ repository,+ "src/public.py",+ ["src"],+ ),+ "fallback": excerpts.finding_source_excerpt(+ selected,+ repository,+ [+ {"path": "private/secret.py", "startLine": 1, "role": "root_control"},+ {"path": "src/public.py", "startLine": 1},+ ],+ ["src"],+ ),+ "rootControl": excerpts.finding_source_excerpt(+ selected,+ repository,+ [+ {+ "path": "src/support.py",+ "startLine": 1,+ "role": "evidence:root_control",+ },+ {"path": "src/public.py", "startLine": 1, "role": "root_control"},+ ],+ ["src"],+ ),+ }+ (repository / "selected.py").unlink()+ write(repository, "selected.py/private.py", "replacement private source\n")+ result["replacedFile"] = excerpt(single, repository, "selected.py", ["selected.py"])+ result["replacedFileDescendant"] = excerpt(+ single, repository, "selected.py/private.py", ["selected.py"]+ )+ shutil.rmtree(repository / "src")+ result["removedDirectory"] = excerpt(+ selected, repository, "src/nested/child.py", ["src"]+ )+ calls = []+ original = scopes.git_bytes++ def observed(target: Path, *arguments: str):+ calls.append(+ (+ os.environ.get("GIT_NO_LAZY_FETCH"),+ os.environ.get("GIT_ALLOW_PROTOCOL"),+ os.environ.get("GIT_NO_REPLACE_OBJECTS"),+ )+ )+ return original(target, *arguments)++ with patch.object(scopes, "git_bytes", observed):+ result["offline"] = excerpt(selected, repository, "src/public.py", ["src"])+ assert calls and all(call == ("1", "", "1") for call in calls)+ with patch.object(excerpts, "offline_git_bytes", return_value=None):+ result["missingObject"] = excerpt(+ selected, repository, "src/public.py", ["src"]+ )+ return result+++def aliases(repository: Path) -> dict:+ composed = unicodedata.normalize("NFC", "café")+ decomposed = unicodedata.normalize("NFD", composed)+ write(repository, "src/nested/public.py", "historical source\n")+ write(repository, f"{composed}/public.py", "unicode source\n")+ write(repository, "Ä/public.py", "non-ASCII source\n")+ revision = commit(repository)+ git(repository, "config", "core.precomposeunicode", "true")+ cases = [+ ("case", "SRC/NESTED", "src/nested", "historical source"),+ ("unicode", decomposed, composed, "unicode source"),+ ("nonAscii", "ä", "Ä", "non-ASCII source"),+ ]+ result = {}+ for name, requested, canonical, content in cases:+ supported = (repository / requested).exists() and (+ repository / requested+ ).samefile(repository / canonical)+ record = scan(repository, revision, [requested])+ saved = json.loads(record["source_scopes_json"])["scopes"]+ if supported:+ assert len(saved) == 1+ parent = repository / canonical+ moved = repository / f"moved-{name}"+ parent.rename(moved)+ try:+ assert (+ excerpt(record, repository, f"{canonical}/public.py", [requested])+ == f"1 {content}"+ )+ assert (+ excerpt(record, repository, f"{requested}/public.py", [requested])+ == f"1 {content}"+ )+ finally:+ moved.rename(parent)+ else:+ assert saved == []+ result[name] = supported++ blob = git(+ repository, "hash-object", "-w", "--stdin", input_data=b"colliding source\n"+ )+ git(+ repository,+ "update-index",+ "--add",+ "--cacheinfo",+ f"100644,{blob},SRC/hidden.py",+ )+ tree = git(repository, "write-tree")+ collision_revision = git(+ repository,+ "commit-tree",+ tree,+ "-p",+ revision,+ input_data=b"Synthetic collision\n",+ )+ upper = repository / "SRC"+ distinct = not upper.exists()+ if distinct:+ write(repository, "SRC/hidden.py", "colliding source\n")+ collision = scan(repository, collision_revision, ["src"])+ saved = json.loads(collision["source_scopes_json"])["scopes"]+ assert bool(saved) == distinct+ assert excerpt(collision, repository, "SRC/hidden.py", ["src"]) is None+ legacy_collision = {+ "target_revision": collision_revision,+ "target_snapshot_digest": None,+ "recipe_json": json.dumps({"_codexSecurityFileScopes": []}),+ }+ assert (+ excerpt(legacy_collision, repository, "src/nested/public.py", ["src"]) is None+ )+ if distinct:+ shutil.rmtree(upper)+ missing = scan(repository, collision_revision, ["src"])+ assert json.loads(missing["source_scopes_json"])["scopes"] == []+ result["collisionChecked"] = True+ return result+++def descendant_aliases(repository: Path) -> dict:+ pairs = [("foo.py", "FOO.py"), ("nested/public.py", "NESTED/public.py")]+ for lower, _ in pairs:+ write(repository, f"selected/{lower}", "lower source\n")+ revision = commit(repository)+ upper_blob = git(+ repository, "hash-object", "-w", "--stdin", input_data=b"upper source\n"+ )+ for _, upper in pairs:+ git(+ repository, "update-index", "--add", "--cacheinfo",+ f"100644,{upper_blob},selected/{upper}",+ )+ tree = git(repository, "write-tree")+ collision = git(+ repository, "commit-tree", tree, "-p", revision,+ input_data=b"Synthetic descendant collision\n",+ )+ for lower, upper in pairs:+ lower_path = repository / "selected" / lower+ upper_path = repository / "selected" / upper+ if not upper_path.exists():+ write(repository, f"selected/{upper}", "upper source\n")+ aliases = lower_path.samefile(upper_path)+ record = scan(repository, collision, ["selected"])+ assert len(json.loads(record["source_scopes_json"])["scopes"]) == 1+ for saved in (record, {**record, "source_scopes_json": None}):+ for path, content in ((lower, "lower"), (upper, "upper")):+ expected = None if aliases else f"1 {content} source"+ actual = excerpt(saved, repository, f"selected/{path}", ["selected"])+ assert actual == expected, (path, actual, expected)+ shutil.rmtree(repository / "selected")+ for lower, upper in pairs:+ for path in (lower, upper):+ assert excerpt(record, repository, f"selected/{path}", ["selected"]) is None+ return {"fileAndDirectoryCollisionsChecked": True, "missingWitnessesOmitted": True}+++def alias_evidence(_: Path) -> dict:+ selected, candidate = Path("/synthetic/SECRET.py"), Path("/synthetic/secret.py")+ result = {}+ for kind in ("ordinary", "hardlink", "symlink", "reparse"):+ names = ["SECRET.py", "secret.py"] if kind == "hardlink" else ["SECRET.py"]+ entries = [SimpleNamespace(name=name) for name in names]++ def metadata(path: Path):+ mode = (+ stat.S_IFLNK+ if kind == "symlink" and path == candidate+ else stat.S_IFREG+ )+ return SimpleNamespace(+ st_mode=mode, st_file_attributes=0x400 if kind == "reparse" else 0+ )++ with (+ patch.object(Path, "lstat", metadata),+ patch.object(Path, "samefile", return_value=True),+ patch.object(+ scopes.os, "scandir", side_effect=lambda _: nullcontext(entries)+ ),+ patch.object(stat, "FILE_ATTRIBUTE_REPARSE_POINT", 0x400, create=True),+ ):+ result[kind] = scopes.filesystem_alias(selected, candidate)+ return result+++def worktrees(repository: Path) -> dict:+ write(repository, "nested/src/public.py", "nested target source\n")+ write(repository, "private.py", "outside target source\n")+ revision = commit(repository)+ selected = repository / "nested"+ record = scan(selected, revision, ["src"])+ assert (+ excerpt(record, selected, "src/public.py", ["src"]) == "1 nested target source"+ )+ assert excerpt(record, selected, "../private.py", ["src"]) is None+ linked = repository.parent / "linked-worktree"+ git(repository, "worktree", "add", "--quiet", "--detach", str(linked), revision)+ linked_target = linked / "nested"+ assert (+ excerpt(record, linked_target, "src/public.py", ["src"])+ == "1 nested target source"+ )+ linked_record = scan(linked_target, revision, ["."])+ assert (+ excerpt(linked_record, linked_target, "src/public.py", ["."])+ == "1 nested target source"+ )+ assert excerpt(linked_record, linked_target, "private.py", ["."]) is None+ return {"subdirectoryBound": True, "linkedWorktreeBound": True}+++def writer(repository: Path, kind: str) -> dict:+ write(repository, "src/public.py", "public source\n")+ write(repository, "selected.py", "selected file\n")+ revision = commit(repository)+ state = repository.parent / "state"+ scan_root = repository.parent / "scans"+ environment = dict(os.environ, CODEX_SECURITY_STATE_DIR=str(state))+ script = str(Path(sys.argv[1]) / "workbench_db.py")++ def command(*arguments: str, succeeds: bool = True):+ result = subprocess.run(+ [sys.executable, "-I", "-B", script, *arguments],+ env=environment,+ text=True,+ capture_output=True,+ )+ if not succeeds:+ assert result.returncode != 0+ return result.stderr+ assert result.returncode == 0, result.stderr+ return json.loads(result.stdout)++ recipe = {+ "config": {},+ "mode": "standard",+ "repository": str(repository),+ "target": {"kind": "paths", "paths": ["src", "selected.py"]},+ }+ if kind == "workspace":+ workspace = str(uuid.uuid4())+ command(+ "create-workspace",+ "--workspace-id",+ workspace,+ "--thread-id",+ "synthetic-workspace",+ )+ command(+ "save-workspace",+ "--workspace-id",+ workspace,+ "--target-path",+ str(repository),+ "--scope",+ "src",+ "--mode",+ "standard",+ )+ command(+ "start-scan", "--workspace-id", workspace, "--scan-root", str(scan_root)+ )+ elif kind in {"prompt", "headless"}:+ arguments = [+ "start-prompt-only-scan"+ if kind == "prompt"+ else "start-headless-standard-scan",+ "--thread-id",+ "synthetic-" + kind,+ "--target-path",+ str(repository),+ "--scope",+ "src",+ "--scan-root",+ str(scan_root),+ ]+ if kind == "prompt":+ arguments.extend(("--mode", "standard"))+ command(*arguments)+ elif kind == "deep":+ command(+ "begin-deep-scan",+ "--thread-id",+ "synthetic-direct-deep",+ "--target-path",+ str(repository),+ "--scan-root",+ str(scan_root),+ )+ elif kind == "cli":+ registered, _ = register_recipe(+ repository, {**recipe, "_codexSecurityFileScopes": ["other.py"]}+ )+ assert (+ command("get-scan-recipe", "--scan-id", registered["scanId"])["recipe"]+ == {**recipe, "scopeFilePaths": ["selected.py"]}+ )+ else:+ raise AssertionError("Unknown scan writer: " + kind)++ with closing(sqlite3.connect(state / "workbench.sqlite3")) as connection:+ connection.row_factory = sqlite3.Row+ rows = connection.execute("SELECT * FROM scans").fetchall()+ assert len(rows) == 1+ row = dict(rows[0])+ metadata = json.loads(row["source_scopes_json"])+ assert metadata["revision"] == revision+ expected = (+ {"src", "selected.py"}+ if kind == "cli"+ else {"."}+ if kind == "deep"+ else {"src"}+ )+ assert {scope["path"] for scope in metadata["scopes"]} == expected+ historical = {**row, "source_scopes_json": None}+ assert (+ excerpt(historical, repository, "src/public.py", list(expected))+ == "1 public source"+ )+ if kind == "cli":+ assert (+ excerpt(historical, repository, "selected.py", list(expected))+ == "1 selected file"+ )+ else:+ assert row["recipe_json"] is None+ assert "does not have a saved launch recipe" in command(+ "get-scan-recipe", "--scan-id", row["id"], succeeds=False+ )+ if kind == "deep":+ git(+ repository,+ "commit",+ "--amend",+ "--quiet",+ "-m",+ "Rewritten synthetic fixture",+ )+ git(repository, "reflog", "expire", "--expire=now", "--all")+ git(repository, "gc", "--prune=now")+ try:+ git(repository, "cat-file", "-e", revision)+ except subprocess.CalledProcessError:+ pass+ else:+ raise AssertionError("Original commit was not pruned")+ assert excerpt(row, repository, "selected.py", ["."]) == "1 selected file"+ return {+ "writer": kind,+ "sourceAuthorityRecorded": True,+ "launchRecipeUnchanged": True,+ "legacyExactScopesPreserved": True,+ }+++def migration(_: Path) -> dict:+ from workbench_schema import MIGRATIONS, apply_migrations++ timestamp = "2026-08-01T00:00:00Z"+ historical = tuple(item for item in MIGRATIONS if item[0] < 43)+ for conflict in (False, True):+ connection = sqlite3.connect(":memory:")+ connection.row_factory = sqlite3.Row+ def apply(migrations: tuple[tuple[int, str, str], ...]) -> None:+ apply_migrations(connection, migrations, lambda: timestamp, lambda _: None)++ apply(historical)+ connection.execute(+ "INSERT INTO scans (id, workspace_id, target_path, target_revision, scope, mode, scan_dir, status, phase, started_at, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",+ (+ "synthetic-scan",+ "synthetic-workspace",+ "synthetic-target",+ "synthetic-revision",+ ".",+ "standard",+ "synthetic-output",+ "complete",+ "reporting",+ timestamp,+ timestamp,+ timestamp,+ ),+ )+ if conflict:+ connection.execute(+ "INSERT INTO schema_migrations VALUES (43, ?, ?)",+ ("unrelated migration", timestamp),+ )+ connection.commit()+ before = [+ tuple(row)+ for row in connection.execute(+ "SELECT * FROM schema_migrations ORDER BY version"+ )+ ]+ try:+ apply(MIGRATIONS)+ except SystemExit as error:+ assert conflict and "unsupported source-scope migration history" in str(+ error+ )+ assert [+ tuple(row)+ for row in connection.execute(+ "SELECT * FROM schema_migrations ORDER BY version"+ )+ ] == before+ assert "source_scopes_json" not in {+ row["name"] for row in connection.execute("PRAGMA table_info(scans)")+ }+ else:+ assert not conflict+ assert (+ connection.execute("SELECT source_scopes_json FROM scans").fetchone()[0]+ is None+ )+ assert [+ tuple(row)+ for row in connection.execute(+ "SELECT * FROM schema_migrations WHERE version < 43 ORDER BY version"+ )+ ] == before+ assert (+ connection.execute(+ "SELECT name FROM schema_migrations WHERE version=50"+ ).fetchone()[0]+ == "persist authorized source excerpt scopes"+ )+ assert (+ connection.execute(+ "SELECT name FROM schema_migrations WHERE version=43"+ ).fetchone()[0]+ == "preserve severity assessments per scan"+ )+ apply(MIGRATIONS)+ connection.close()+ return {+ "legacyAuthorityUnset": True,+ "otherMigrationsPreserved": True,+ "conflictRejected": True,+ }+++with tempfile.TemporaryDirectory(prefix="codex-security-source-scopes-") as temporary:+ repository = Path(temporary).resolve() / "repository"+ repository.mkdir()+ git(repository, "init", "--quiet")+ scenario = sys.argv[2]+ result = (+ writer(repository, scenario.removeprefix("writer_"))+ if scenario.startswith("writer_")+ else {+ "boundaries": boundaries,+ "replacements": replacements,+ "replacement_filters": replacement_filters,+ "replacement_snapshot": replacement_snapshot,+ "working_tree_excerpt": working_tree_excerpt,+ "indexed_scopes": indexed_scopes,+ "display_locations": display_locations,+ "selected_redirects": selected_redirects,+ "unsafe_locations": unsafe_locations,+ "aliases": aliases,+ "descendant_aliases": descendant_aliases,+ "alias_evidence": alias_evidence,+ "worktrees": worktrees,+ "migration": migration,+ }[scenario](repository)+ )+ print(json.dumps(result))diff --git a/sdk/typescript/tests-ts/plugin-report-limits.test.ts b/sdk/typescript/tests-ts/plugin-report-limits.test.tsindex 4f2617ea9..7ff66d9b6 100644--- a/sdk/typescript/tests-ts/plugin-report-limits.test.ts+++ b/sdk/typescript/tests-ts/plugin-report-limits.test.ts@@ -20,9 +20,9 @@ describe("bundled scan report and source limits", () => {" schema.write_text(json.dumps({'type': 'object', 'description': 'x' * (4 * 1024 * 1024), 'allOf': [{'type': 'object'}] * 129}))"," finalizer.validate_against_schema({'safe': True}, schema)"," source = b'x' * (1024 * 1024 + 1)",- " excerpts.git_bytes = lambda *args: source",+ " excerpts.offline_git_bytes = lambda *args: source"," target = pathlib.Path(directory).resolve()",- " excerpt = excerpts.scanned_source_text({'target_revision': 'deadbeef', 'target_snapshot_digest': None, 'diff_target_kind': None}, target, 'large.py')",+ " excerpt = excerpts.scanned_source_text(target, 'a' * 40)"," hashes = finalizer._github_line_hashes(io.StringIO('line\\n' * 100001), {100001})"," print(json.dumps({'documentBytes': len(document), 'sourceBytes': len(excerpt), 'lateSourceLine': 100001 in hashes, 'unsafePathRejected': excerpts.safe_source_path(target, '../outside') is None}))",].join("\n");diff --git a/sdk/typescript/tests-ts/runtime.test.ts b/sdk/typescript/tests-ts/runtime.test.tsindex a22d5eeda..43c8453b9 100644--- a/sdk/typescript/tests-ts/runtime.test.ts+++ b/sdk/typescript/tests-ts/runtime.test.ts@@ -2473,10 +2473,9 @@ for await (const line of createInterface({ input: process.stdin })) {]);});- test("upgrades a cached plugin without deleting persistent credentials", async () => {+ test("upgrades the prior 0.1.25 bundle without deleting persistent credentials", async () => {const root = await temporaryDirectory();- const previous = await plugin(join(root, "previous"), "1.2.3");- const next = await plugin(join(root, "next"), "1.2.4");+ const previous = await plugin(join(root, "previous"), "0.1.25");const home = join(root, "home");const configPath = join(home, "config.toml");const marketplace = join(home, "sdk-marketplace");@@ -2544,12 +2543,14 @@ for await (const line of createInterface({ input: process.stdin })) {};expect((await bootstrapPlugin(home, previous, options)).version).toBe(- "1.2.3",+ "0.1.25",);- const upgraded = await bootstrapPlugin(home, next, options);+ const upgraded = await bootstrapPlugin(home, PLUGIN_ROOT, options);- expect(upgraded.version).toBe("1.2.4");- expect(upgraded.installedRoot).toBe(join(pluginCache, "1.2.4"));+ expect(upgraded.version).toBe(BUNDLED_PLUGIN_VERSION);+ expect(upgraded.installedRoot).toBe(+ join(pluginCache, BUNDLED_PLUGIN_VERSION),+ );expect(await readFile(join(home, "auth.json"), "utf8")).toBe('{"token":"preserved"}\n',);@@ -2559,7 +2560,7 @@ for await (const line of createInterface({ input: process.stdin })) {expect(await readFile(configPath, "utf8")).toContain(`[projects.${JSON.stringify(join(root, "unrelated-project"))}]`,);- expect(existsSync(join(pluginCache, "1.2.3"))).toBe(false);+ expect(existsSync(join(pluginCache, "0.1.25"))).toBe(false);expect(calls).toEqual([["plugin", "marketplace", "add", marketplace],["plugin", "add", "--json", "codex-security@codex-security-sdk"],