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): preserve Windows checkout identity across Python versions (#2541)
Michael D'Angelof91c51e51419
diff --git a/plugins/codex-security/scripts/windows_scan_local_files.py b/plugins/codex-security/scripts/windows_scan_local_files.pyindex 3c80911db..f7d5c3b89 100644--- a/plugins/codex-security/scripts/windows_scan_local_files.py+++ b/plugins/codex-security/scripts/windows_scan_local_files.py@@ -71,6 +71,7 @@ _OPEN_EXISTING = 3_FILE_ATTRIBUTE_DIRECTORY = 0x00000010_FILE_ATTRIBUTE_NORMAL = 0x00000080_FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400+_REPARSE_TAG_NAME_SURROGATE = 0x20000000_FILE_FLAG_BACKUP_SEMANTICS = 0x02000000_FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000@@ -468,9 +469,11 @@ def _verify_handle_path(handle: int, expected_path: Path, *, opened_name: bool =raise _invalid_path(expected_path, "opened file resolved outside its verified scan path")-def _verify_directory(handle: int, expected_path: Path) -> None:+def _verify_directory(handle: int, expected_path: Path, checkout: bool = False) -> None:info = _attributes(handle)- if info.FileAttributes & _FILE_ATTRIBUTE_REPARSE_POINT:+ if info.FileAttributes & _FILE_ATTRIBUTE_REPARSE_POINT and (+ not checkout or not info.ReparseTag or info.ReparseTag & _REPARSE_TAG_NAME_SURROGATE+ ):raise _invalid_path(expected_path, "scan-local directories must not be reparse points")if not info.FileAttributes & _FILE_ATTRIBUTE_DIRECTORY:raise _invalid_path(expected_path, "expected a scan-local directory")@@ -506,6 +509,7 @@ def _open_directory(missing_ok: bool = False,missing_is_path_error: bool = True,movable: bool = False,+ checkout: bool = False,) -> _OwnedHandle | None:try:handle = _create_file(@@ -526,7 +530,7 @@ def _open_directory(if handle is not None:try:assert handle.value is not None- _verify_directory(handle.value, path)+ _verify_directory(handle.value, path, checkout)except BaseException:handle.close()raise@@ -731,24 +735,44 @@ def deep_resume_output_identity(scan_dir: Path) -> list[int]:return _pinned_resume_identity(parent, child, retained, grants)-def _resume_directory_id(handle: int, path: Path) -> tuple[int, int]:+def _full_directory_id(handle: int, path: Path) -> tuple[int, int] | None:info = _FileIdInfo()- if _GetFileInformationByHandleEx(+ if not _GetFileInformationByHandleEx(handle, _FILE_ID_INFO_CLASS, ctypes.byref(info), ctypes.sizeof(info)):- volume, file_id = info.VolumeSerialNumber, int.from_bytes(info.FileId, "little")- elif ctypes.get_last_error() in {50, 87}: # ERROR_NOT_SUPPORTED or ERROR_INVALID_PARAMETER.- # Some filesystems expose only the legacy 64-bit file index.- legacy = _ByHandleFileInformation()- if not _GetFileInformationByHandle(handle, ctypes.byref(legacy)):- _raise_last_error("GetFileInformationByHandle", path)- volume = legacy.dwVolumeSerialNumber- file_id = (legacy.nFileIndexHigh << 32) | legacy.nFileIndexLow- else:+ if ctypes.get_last_error() in {50, 87}: # ERROR_NOT_SUPPORTED or ERROR_INVALID_PARAMETER.+ return None_raise_last_error("GetFileInformationByHandleEx(FileIdInfo)", path)+ file_id = int.from_bytes(info.FileId, "little")+ if not file_id:+ raise _invalid_path(path, "The resume directory has no stable file identity.")+ return info.VolumeSerialNumber, file_id+++def _resume_directory_id(handle: int, path: Path) -> tuple[int, int]:+ if (identity := _full_directory_id(handle, path)) is not None:+ return identity+ # Some filesystems expose only the legacy 64-bit file index.+ legacy = _ByHandleFileInformation()+ if not _GetFileInformationByHandle(handle, ctypes.byref(legacy)):+ _raise_last_error("GetFileInformationByHandle", path)+ file_id = (legacy.nFileIndexHigh << 32) | legacy.nFileIndexLowif not file_id:raise _invalid_path(path, "The resume directory has no stable file identity.")- return volume, file_id+ return legacy.dwVolumeSerialNumber, file_id+++def pinned_resume_checkout(target: Path) -> Iterator[tuple[int, int] | None]:+ """Keep the canonical checkout fixed while reading its full native identity and snapshot."""+ with contextlib.ExitStack() as pins:+ for directory in (*reversed(target.parents), target):+ # A source provider directory may carry a reparse tag without redirecting its name.+ handle = _open_directory(directory, checkout=True)+ assert handle is not None+ pins.enter_context(handle)+ assert handle.value is not None+ yield _full_directory_id(handle.value, target)def _pinned_resume_identity(parent: int, child: int, retained: Path, grants: int) -> list[int]:diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.pyindex fb8c6bbd8..d97437868 100644--- a/plugins/codex-security/scripts/workbench_db.py+++ b/plugins/codex-security/scripts/workbench_db.py@@ -86,6 +86,7 @@ from workbench_scan_start import (archive_scan,insert_running_scan,loaded_plugin_version,+ prepare_cli_scan_target,register_windows_resume_output,safe_segment,scan_diff_identity,@@ -118,6 +119,7 @@ from workbench_source_scopes import (safe_source_path,)from workbench_target import (+ checked_scan_target,clean_worktree_content_digest,copy_directory_excluding,copy_git_worktree_files,@@ -1566,7 +1568,9 @@ def register_cli_scan(raise SystemExit("Working-tree HEAD changed before the scan started.")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)+ target_identity, resume_target = prepare_cli_scan_target(+ repository, diff_target, mode, launch_kind+ )source_scopes = capture_source_scopes(repository,target_identity,@@ -1681,9 +1685,12 @@ def register_cli_scan()if workflow_id is not None:register_workflow_scan(connection, workflow_id, scan_id, str(scan_dir), timestamp)- if os.name == "nt" and mode == "deep" and launch_kind == "scan":- acknowledge_output_change()- archive.enter_context(register_windows_resume_output(connection, scan_id, scan_dir))+ if resume_target is not None:+ archive.enter_context(+ register_windows_resume_output(+ connection, scan_id, scan_dir, resume_target, acknowledge_output_change+ )+ )scan = connection.execute("SELECT scans.*, snapshot AS lifecycle_json FROM scans ""JOIN scan_lifecycles ON scan_id = scans.id WHERE scans.id = ?",@@ -2886,7 +2893,8 @@ def remediation_availability(scan: sqlite3.Row) -> tuple[bool, str | None]:if scan["status"] != "complete":return False, remediation_state.SCAN_STATUS_ERRORtry:- current_revision = git_revision(require_scan_target_identity(scan))+ with checked_scan_target(scan) as target:+ current_revision = git_revision(target)except SystemExit as exc:return False, str(exc)expected_revision = scan["target_revision"]diff --git a/plugins/codex-security/scripts/workbench_scan_history.py b/plugins/codex-security/scripts/workbench_scan_history.pyindex 47527ef7b..f87ba5d86 100644--- a/plugins/codex-security/scripts/workbench_scan_history.py+++ b/plugins/codex-security/scripts/workbench_scan_history.py@@ -29,7 +29,12 @@ from workbench_constants import ARTIFACTS, FINDINGS_PAGE_MAX, PRIMARY_FINDING_LOfrom workbench_scan_start import scan_target_identityfrom workbench_scan_usage import stored_scan_cost_fieldsfrom workbench_source_scopes import public_scan_recipe-from workbench_target import git_output, require_scan_target_identity+from workbench_target import (+ git_output,+ require_remediation_target,+ require_scan_target_identity,+ verified_windows_checkout,+)from workbench_validation import timestamp_key@@ -68,6 +73,19 @@ def preserve_sealed_completion(return binding+def _windows_resume_checkout(scan: sqlite3.Row) -> Path:+ replaced = "Cannot resume: the original checkout is missing or was replaced."+ try:+ repository = require_remediation_target(scan["target_path"])+ except SystemExit as exc:+ raise SystemExit(replaced) from exc+ with verified_windows_checkout(scan, repository, replaced_message=replaced):+ snapshot = (scan["target_revision"], scan["target_snapshot_digest"])+ if scan_target_identity(repository, None)[:2] != snapshot:+ raise SystemExit("Cannot resume: the original checkout revision or contents changed.")+ return repository++def cli_scan_resume(wb: Any,connection: sqlite3.Connection,@@ -97,19 +115,25 @@ def cli_scan_resume(run["status"] not in {"running", "succeeded"} or run["cancel_requested"]):raise SystemExit("This Deep Scan has stopped and cannot resume.")- try:- repository = require_scan_target_identity(scan)- except SystemExit as exc:- raise SystemExit(- "Cannot resume: the original checkout is missing or was replaced."- ) from exc- if scan_target_identity(repository, None) != (- scan["target_revision"],- scan["target_snapshot_digest"],- scan["target_device"],- scan["target_inode"],- ):- raise SystemExit("Cannot resume: the original checkout revision or contents changed.")+ if os.name == "nt":+ try:+ repository = _windows_resume_checkout(scan)+ except OSError as exc:+ raise SystemExit(f"Cannot verify the original Windows checkout: {exc}") from exc+ else:+ try:+ repository = require_scan_target_identity(scan)+ except SystemExit as exc:+ raise SystemExit(+ "Cannot resume: the original checkout is missing or was replaced."+ ) from exc+ if scan_target_identity(repository, None) != (+ scan["target_revision"],+ scan["target_snapshot_digest"],+ scan["target_device"],+ scan["target_inode"],+ ):+ raise SystemExit("Cannot resume: the original checkout revision or contents changed.")recipe = wb.parse_scan_recipe(scan["recipe_json"], repository)scan_dir = wb.require_canonical_scan_directory(Path(scan["scan_dir"]))if os.name == "nt":diff --git a/plugins/codex-security/scripts/workbench_scan_start.py b/plugins/codex-security/scripts/workbench_scan_start.pyindex 075d9ef38..df73b5c71 100644--- a/plugins/codex-security/scripts/workbench_scan_start.py+++ b/plugins/codex-security/scripts/workbench_scan_start.py@@ -12,6 +12,7 @@ import sysimport tempfilefrom collections.abc import Callable, Iteratorfrom contextlib import contextmanager+from dataclasses import dataclassfrom datetime import datetime, timezonefrom pathlib import Pathfrom typing import Any@@ -88,6 +89,25 @@ def scan_target_identity()+@dataclass(frozen=True)+class WindowsResumeTarget:+ path: Path+ snapshot: tuple[str, str | None, int | str, int | str]+ native_id: tuple[int, int] | None+++def prepare_cli_scan_target(+ target: Path, diff_target: dict[str, str] | None, mode: str, launch_kind: str+) -> tuple[tuple[str, str | None, int | str, int | str], WindowsResumeTarget | None]:+ if os.name == "nt" and mode == "deep" and launch_kind == "scan":+ from windows_scan_local_files import pinned_resume_checkout++ with pinned_resume_checkout(target) as native:+ snapshot = scan_target_identity(target, diff_target)+ return snapshot, WindowsResumeTarget(target, snapshot, native)+ return scan_target_identity(target, diff_target), None++def scan_diff_identity(diff_target: dict[str, str] | None,) -> tuple[str | None, str | None, str | None, str | None]:@@ -285,9 +305,13 @@ def archive_scan(@contextmanagerdef register_windows_resume_output(- connection: sqlite3.Connection, scan_id: str, scan_dir: Path+ connection: sqlite3.Connection,+ scan_id: str,+ scan_dir: Path,+ target: WindowsResumeTarget,+ before_create: Callable[[], None],) -> Iterator[None]:- from windows_scan_local_files import create_deep_resume_output+ from windows_scan_local_files import create_deep_resume_output, pinned_resume_checkoutdef committed() -> bool:# A signal can arrive after commit. The registered scan then owns its child.@@ -297,12 +321,24 @@ def register_windows_resume_output(is not None)- with create_deep_resume_output(scan_dir, committed) as identity:- connection.execute(- "UPDATE scans SET windows_resume_output_identity = ? WHERE id = ?",- (json.dumps(identity), scan_id),- )- yield+ with pinned_resume_checkout(target.path) as current:+ if current != target.native_id or (+ current is None and scan_target_identity(target.path, None) != target.snapshot+ ):+ raise SystemExit(+ "The selected scan target changed while the scan was starting. Try again."+ )+ before_create()+ with create_deep_resume_output(scan_dir, committed) as identity:+ connection.execute(+ "UPDATE scans SET windows_resume_output_identity = ?, windows_resume_target_identity = ? WHERE id = ?",+ (+ json.dumps(identity),+ json.dumps(target.native_id) if target.native_id is not None else None,+ scan_id,+ ),+ )+ yielddef insert_running_scan(diff --git a/plugins/codex-security/scripts/workbench_target.py b/plugins/codex-security/scripts/workbench_target.pyindex b6a836ea7..85fc06b28 100644--- a/plugins/codex-security/scripts/workbench_target.py+++ b/plugins/codex-security/scripts/workbench_target.py@@ -5,6 +5,7 @@ from __future__ import annotationsimport argparseimport codecsimport hashlib+import jsonimport osimport reimport shutil@@ -14,13 +15,15 @@ import subprocessimport sysimport tempfileimport unicodedata+from collections.abc import Iteratorfrom concurrent.futures import ThreadPoolExecutor+from contextlib import ExitStack, contextmanagerfrom pathlib import Path, PurePosixPathfrom typing import Any, BinaryIO, Callable, cast# Some plugin hosts launch Python with safe-path isolation enabled.sys.path.insert(0, str(Path(__file__).resolve().parent))-from filesystem_identity import stored_filesystem_identity_matches+from filesystem_identity import serialize_filesystem_identity, stored_filesystem_identity_matchesfrom windows_scan_local_files import copy_directory_junction, pinned_root_git_markerfrom workbench_constants import GIT_REPOSITORY_ENVIRONMENT@@ -624,17 +627,17 @@ def remediation_checkout_snapshot(),) -> tuple[str, str | None]:"""Return the current revision and content digest after verifying the recorded checkout identity and required revision."""- target = require_scan_target_identity(scan)- revision = git_revision(target)- required_revision = expected_revision or scan["target_revision"]- if revision != required_revision:- raise SystemExit(head_changed_message)- content_digest = (- worktree_content_digest(target)- if revision != "unversioned"- else directory_content_digest(target, excluded=(Path(scan["scan_dir"]),))- )- return revision, content_digest+ with checked_scan_target(scan) as target:+ revision = git_revision(target)+ required_revision = expected_revision or scan["target_revision"]+ if revision != required_revision:+ raise SystemExit(head_changed_message)+ content_digest = (+ worktree_content_digest(target)+ if revision != "unversioned"+ else directory_content_digest(target, excluded=(Path(scan["scan_dir"]),))+ )+ return revision, content_digestdef require_scan_target_unchanged(scan: sqlite3.Row) -> None:@@ -646,10 +649,10 @@ def require_scan_target_unchanged(scan: sqlite3.Row) -> None:and scan["target_revision"] != "unversioned"):# Legacy Git records bind only the revision, not working-tree contents.- target = require_scan_target_identity(scan)- if git_revision(target) != scan["target_revision"]:- raise SystemExit(head_changed_message)- require_clean_submodule_worktrees(target)+ with checked_scan_target(scan) as target:+ if git_revision(target) != scan["target_revision"]:+ raise SystemExit(head_changed_message)+ require_clean_submodule_worktrees(target)return_, current_digest = remediation_checkout_snapshot(scan, head_changed_message=head_changed_message@@ -1383,9 +1386,78 @@ def require_remediation_target(value: str) -> Path:return stored-def require_scan_target_identity(scan: sqlite3.Row) -> Path:- """Return the recorded checkout path or raise SystemExit if it no longer identifies the original directory."""+@contextmanager+def verified_windows_checkout(+ scan: sqlite3.Row, target: Path, *, replaced_message: str+) -> Iterator[None]:+ """Bind a Windows Deep checkout for the full operation, including legacy stat-width changes."""+ from windows_scan_local_files import pinned_resume_checkout++ binding = scan["windows_resume_target_identity"]+ stored = (scan["target_device"], scan["target_inode"])+ if binding is None:++ def stat_identity() -> tuple[int | str, int | str]:+ try:+ metadata = target.stat()+ except OSError as exc:+ raise SystemExit(f"Cannot verify the original Windows checkout: {exc}") from exc+ return serialize_filesystem_identity(metadata.st_dev), serialize_filesystem_identity(+ metadata.st_ino+ )++ if stat_identity() == stored:+ yield+ if stat_identity() != stored:+ raise SystemExit(replaced_message)+ return++ with pinned_resume_checkout(target) as native:+ if binding is not None:+ if native is None:+ raise SystemExit("Cannot verify the original Windows checkout identity.")+ if list(native) != json.loads(binding):+ raise SystemExit(replaced_message)+ # Legacy stat returned at most a 32-bit volume and 64-bit file index.+ elif (+ native is None+ or (native[0] < 1 << 32 and native[1] < 1 << 64)+ or tuple(map(serialize_filesystem_identity, native)) != stored+ ):+ raise SystemExit(+ "Cannot verify the saved Windows checkout identity with this Python runtime. "+ "Use its original Python runtime or start a new scan with "+ f"'codex-security scans rerun {scan['id']}'."+ )+ yield+++@contextmanager+def checked_scan_target(scan: sqlite3.Row) -> Iterator[Path]:+ """Keep any recorded Windows checkout binding intact while a caller reads the target."""target = require_remediation_target(scan["target_path"])+ replaced = "Remediation is unavailable because the selected checkout path was replaced. Start a new scan."+ if (+ os.name == "nt"+ and "windows_resume_target_identity" in scan.keys()+ and (+ scan["windows_resume_target_identity"] is not None+ or (+ scan["mode"] == "deep"+ and scan["launch_kind"] == "scan"+ and scan["recipe_json"] is not None+ )+ )+ ):+ with ExitStack() as pins:+ try:+ pins.enter_context(+ verified_windows_checkout(scan, target, replaced_message=replaced)+ )+ except OSError as exc:+ raise SystemExit(f"Cannot verify the original Windows checkout: {exc}") from exc+ yield target+ returnexpected_inode = scan["target_inode"]if expected_inode is None:raise SystemExit(@@ -1399,11 +1471,14 @@ def require_scan_target_identity(scan: sqlite3.Row) -> Path:"Remediation is unavailable because the selected checkout is no longer accessible.") from excif not stored_filesystem_identity_matches(expected_inode, metadata.st_ino):- raise SystemExit(- "Remediation is unavailable because the selected checkout path was replaced. "- "Start a new scan."- )- return target+ raise SystemExit(replaced)+ yield target+++def require_scan_target_identity(scan: sqlite3.Row) -> Path:+ """Return the recorded checkout path after checking its filesystem identity."""+ with checked_scan_target(scan) as target:+ return targetdef require_git_worktree_head(target: Path) -> str:@@ -1417,37 +1492,37 @@ def scan_target_warning(scan: sqlite3.Row) -> str | None:if scan["diff_target_kind"] != "working_tree" and not scan["target_snapshot_digest"]:return Nonetry:- target = require_scan_target_identity(scan)- if scan["target_revision"] == "unversioned":- if (- directory_content_digest(target, excluded=(Path(scan["scan_dir"]),))- != scan["target_snapshot_digest"]- ):+ with checked_scan_target(scan) as target:+ if scan["target_revision"] == "unversioned":+ if (+ directory_content_digest(target, excluded=(Path(scan["scan_dir"]),))+ != scan["target_snapshot_digest"]+ ):+ return (+ "Directory contents changed while the scan was running; "+ "results were saved for the original snapshot."+ )+ return None+ if git_revision(target) == "unversioned":return (- "Directory contents changed while the scan was running; "- "results were saved for the original snapshot."+ "The scanned Git repository became unavailable while the scan was running; "+ "results were saved for the original revision.")- return None- if git_revision(target) == "unversioned":- return (- "The scanned Git repository became unavailable while the scan was running; "- "results were saved for the original revision."- )- working_tree = scan["diff_target_kind"] == "working_tree"- expected_head = scan["diff_head_revision"] if working_tree else scan["target_revision"]- if require_git_worktree_head(target) != expected_head:- return (- "Repository HEAD changed while the scan was running; "- "results were saved for the original revision."- )- expected_digest = (- scan["diff_content_digest"] if working_tree else scan["target_snapshot_digest"]- )- if worktree_content_digest(target) != expected_digest:- return (- "Working-tree contents changed while the scan was running; "- "results were saved for the original snapshot."+ working_tree = scan["diff_target_kind"] == "working_tree"+ expected_head = scan["diff_head_revision"] if working_tree else scan["target_revision"]+ if require_git_worktree_head(target) != expected_head:+ return (+ "Repository HEAD changed while the scan was running; "+ "results were saved for the original revision."+ )+ expected_digest = (+ scan["diff_content_digest"] if working_tree else scan["target_snapshot_digest"])+ if worktree_content_digest(target) != expected_digest:+ return (+ "Working-tree contents changed while the scan was running; "+ "results were saved for the original snapshot."+ )except (OSError, SystemExit) as error:return (f"Could not verify the scan target after scanning: {error}; "diff --git a/plugins/codex-security/shared/workbench-migrations.json b/plugins/codex-security/shared/workbench-migrations.jsonindex d47ceafb1..a3bf99381 100644--- a/plugins/codex-security/shared/workbench-migrations.json+++ b/plugins/codex-security/shared/workbench-migrations.json@@ -529,5 +529,12 @@"statements": ["ALTER TABLE scans ADD COLUMN windows_resume_output_identity TEXT;"]+ },+ {+ "version": 66,+ "name": "bind Windows Deep Scan resume checkouts",+ "statements": [+ "ALTER TABLE scans ADD COLUMN windows_resume_target_identity TEXT;"+ ]}]diff --git a/plugins/codex-security/tests/test_windows_workbench_output_permissions.py b/plugins/codex-security/tests/test_windows_workbench_output_permissions.pyindex 3983b3b9a..ef22a717c 100644--- a/plugins/codex-security/tests/test_windows_workbench_output_permissions.py+++ b/plugins/codex-security/tests/test_windows_workbench_output_permissions.py@@ -5,6 +5,7 @@ from __future__ import annotationsimport argparseimport base64import ctypes+import ioimport jsonimport osimport sqlite3@@ -23,6 +24,7 @@ sys.path.insert(0, str(SCRIPTS))import windows_scan_local_files # noqa: E402import workbench_db # noqa: E402+from filesystem_identity import serialize_filesystem_identity # noqa: E402from workbench import storage # noqa: E402pytestmark = [@@ -76,6 +78,302 @@ def deep_registration(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):return state, output, register+def test_resume_distinguishes_a_changed_checkout_from_different_stat_widths(+ deep_registration,+) -> None:+ state, output, register = deep_registration+ target = output.parent / "repository"+ scan_id = register()["scanId"]+ scan_command(state, "set-scan-thread", scan_id, "--thread-id", "original-thread")+ with closing(sqlite3.connect(state / "workbench.sqlite3")) as connection:+ (stored,) = connection.execute(+ "SELECT windows_resume_target_identity FROM scans WHERE id = ?", (scan_id,)+ ).fetchone()+ volume, file_id = json.loads(stored)+ connection.execute(+ "UPDATE scans SET target_device = ?, target_inode = ? WHERE id = ?",+ (+ volume & 0xFFFFFFFF,+ serialize_filesystem_identity(file_id & 0xFFFFFFFFFFFFFFFF),+ scan_id,+ ),+ )+ connection.commit()+ assert scan_command(state, "get-cli-scan-resume", scan_id)["threadId"] == "original-thread"++ connection.execute(+ "UPDATE scans SET windows_resume_target_identity = ? WHERE id = ?",+ (json.dumps([volume ^ (1 << 32), file_id]), scan_id),+ )+ connection.commit()+ refused = scan_command(state, "get-cli-scan-resume", scan_id, check=False)+ assert (+ refused["returncode"] != 0+ and "checkout is missing or was replaced" in refused["stderr"]+ )++ connection.execute(+ "UPDATE scans SET windows_resume_target_identity = ? WHERE id = ?", (stored, scan_id)+ )+ connection.commit()+ previous = target.with_name("previous checkout")+ target.rename(previous)+ target.mkdir()+ (target / "source.py").write_bytes((previous / "source.py").read_bytes())+ replacement = scan_command(state, "get-cli-scan-resume", scan_id, check=False)+ assert (+ replacement["returncode"] != 0+ and "checkout is missing or was replaced" in replacement["stderr"]+ )+++ ("tag", "source_allowed"),+ [(0x9000001A, True), (0x80000017, True), (0xA0000003, False), (0xA000000C, False)],+ ids=["cloud-files", "wof", "junction", "symlink"],+)+def test_checkout_pins_distinguish_name_surrogates_from_provider_directories(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, tag: int, source_allowed: bool+) -> None:+ windows = windows_scan_local_files+ provider = tmp_path / "provider"+ repository = provider / "repository"+ repository.mkdir(parents=True)+ attrs = windows._attributes++ def provider_attributes(handle):+ info = attrs(handle)+ if windows._final_path(handle) == windows._extended_path(provider):+ info.FileAttributes |= windows._FILE_ATTRIBUTE_REPARSE_POINT+ info.ReparseTag = tag+ return info++ monkeypatch.setattr(windows, "_attributes", provider_attributes)+ if source_allowed:+ with windows.pinned_resume_checkout(repository) as identity:+ assert identity is not None+ with pytest.raises(PermissionError):+ provider.rename(tmp_path / "moved")+ else:+ with pytest.raises(OSError, match="reparse"):+ with windows.pinned_resume_checkout(repository):+ pytest.fail("a redirecting ancestor was followed")+ with pytest.raises(OSError, match="reparse"):+ windows.require_windows_output_path(repository)++[email protected]("legacy", [False, True], ids=["stored-native-id", "legacy-full-id"])+def test_high_windows_file_id_survives_resume_validation_and_remediation(+ deep_registration,+ monkeypatch: pytest.MonkeyPatch,+ capsys: pytest.CaptureFixture[str],+ legacy: bool,+) -> None:+ import workbench_target++ state, output, register = deep_registration+ target = output.parent / "repository"+ monkeypatch.setenv("CODEX_SECURITY_STATE_DIR", str(state))+ monkeypatch.setenv("CODEX_HOME", str(state.parent / "home"))+ monkeypatch.setattr(sys, "stdin", io.TextIOWrapper(io.BytesIO()))+ scan_id = register()["scanId"]+ scan_command(state, "set-scan-thread", scan_id, "--thread-id", "original-thread")+ read_identity = windows_scan_local_files._full_directory_id+ high_bits = 1 << 96++ def read_wide_identity(handle, path):+ identity = read_identity(handle, path)+ if path == target:+ return identity[0], identity[1] | high_bits+ return identity++ monkeypatch.setattr(windows_scan_local_files, "_full_directory_id", read_wide_identity)+ with closing(workbench_db.connect()) as connection:+ with windows_scan_local_files.pinned_resume_checkout(target) as native:+ assert native is not None and native[1] != target.stat().st_ino+ connection.execute(+ "UPDATE scans SET target_device = ?, target_inode = ?, windows_resume_target_identity = ? WHERE id = ?",+ (+ *map(serialize_filesystem_identity, native),+ None if legacy else json.dumps(native),+ scan_id,+ ),+ )+ connection.commit()+ row = workbench_db.require_scan(connection, scan_id)+ monkeypatch.setattr(sys, "argv", ["workbench", "get-cli-scan-resume", "--scan-id", scan_id])+ workbench_db.main()+ assert json.loads(capsys.readouterr().out)["threadId"] == "original-thread"+ monkeypatch.setattr(+ sys, "argv", ["workbench", "get-scan", "--scan-id", scan_id, "--check-target"]+ )+ directory_digest = workbench_target.directory_content_digest++ def digest_while_pinned(directory, **options):+ with pytest.raises(PermissionError):+ directory.rename(directory.with_name("different checkout"))+ return directory_digest(directory, **options)++ monkeypatch.setattr(workbench_target, "directory_content_digest", digest_while_pinned)+ workbench_db.main()+ assert json.loads(capsys.readouterr().out)["scan"]["scanId"] == scan_id+ assert workbench_db.remediation_checkout_snapshot(row) == (+ row["target_revision"],+ row["target_snapshot_digest"],+ )+ connection.execute("UPDATE scans SET status='complete' WHERE id=?", (scan_id,))+ if not legacy:+ # A matching legacy stat tuple must never override a differing stored full ID.+ info = target.stat()+ connection.execute(+ "UPDATE scans SET target_device=?, target_inode=? WHERE id=?",+ (+ serialize_filesystem_identity(info.st_dev),+ serialize_filesystem_identity(info.st_ino),+ scan_id,+ ),+ )+ connection.commit()+ row = workbench_db.require_scan(connection, scan_id)+ assert workbench_db.remediation_availability(row) == (True, None)+ high_bits = 1 << 97+ with pytest.raises(SystemExit):+ workbench_db.main()+ with pytest.raises(SystemExit):+ workbench_db.remediation_checkout_snapshot(row)+ assert not workbench_db.remediation_availability(row)[0]+++def test_history_remains_available_if_a_legacy_checkout_moves_after_the_revision_read(+ deep_registration, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]+) -> None:+ state, output, register = deep_registration+ target = output.parent / "repository"+ moved = target.with_name("moved checkout")+ scan_id = register()["scanId"]+ monkeypatch.setenv("CODEX_SECURITY_STATE_DIR", str(state))+ monkeypatch.setenv("CODEX_HOME", str(state.parent / "home"))+ monkeypatch.setattr(sys, "argv", ["workbench", "get-scan", "--scan-id", scan_id])+ monkeypatch.setattr(sys, "stdin", io.TextIOWrapper(io.BytesIO()))+ info = target.stat()+ with closing(workbench_db.connect()) as connection, connection:+ connection.execute(+ "UPDATE scans SET status='complete', windows_resume_target_identity=NULL, "+ "target_device=?, target_inode=? WHERE id=?",+ (*map(serialize_filesystem_identity, (info.st_dev, info.st_ino)), scan_id),+ )+ git_revision = workbench_db.git_revision++ def revision_before_move(directory: Path) -> str:+ revision = git_revision(directory)+ if directory == target and not moved.exists():+ target.rename(moved)+ return revision++ with monkeypatch.context() as patch:+ patch.setattr(workbench_db, "git_revision", revision_before_move)+ workbench_db.main()+ result = json.loads(capsys.readouterr().out)["scan"]+ assert moved.is_dir() and result["scanId"] == scan_id+ assert result["remediationAvailable"] is False+ assert "Cannot verify the original Windows checkout" in result["remediationUnavailableReason"]+ moved.rename(target)+ workbench_db.main()+ assert json.loads(capsys.readouterr().out)["scan"]["remediationAvailable"] is True+++def test_legacy_checkout_keeps_exact_stat_matches_and_requires_full_native_proof(+ deep_registration, monkeypatch: pytest.MonkeyPatch+) -> None:+ state, output, register = deep_registration+ target = output.parent / "repository"+ scan_id = register()["scanId"]+ scan_command(state, "set-scan-thread", scan_id, "--thread-id", "original-thread")+ with closing(sqlite3.connect(state / "workbench.sqlite3")) as connection:+ connection.row_factory = sqlite3.Row+ (stored,) = connection.execute(+ "SELECT windows_resume_target_identity FROM scans WHERE id = ?", (scan_id,)+ ).fetchone()+ volume, file_id = json.loads(stored)+ connection.execute(+ "UPDATE scans SET windows_resume_target_identity = NULL WHERE id = ?", (scan_id,)+ )+ row = connection.execute("SELECT * FROM scans WHERE id = ?", (scan_id,)).fetchone()+ with monkeypatch.context() as patch:+ patch.setattr(+ windows_scan_local_files,+ "pinned_resume_checkout",+ lambda _: pytest.fail(+ "An exact legacy match unexpectedly required a native witness"+ ),+ )+ assert workbench_db.scan_history._windows_resume_checkout(row) == target+ connection.execute(+ "UPDATE scans SET target_device = ?, target_inode = ? WHERE id = ?",+ (+ serialize_filesystem_identity(volume),+ serialize_filesystem_identity(file_id),+ scan_id,+ ),+ )+ connection.commit()+ assert scan_command(state, "get-cli-scan-resume", scan_id)["scanId"] == scan_id++ narrowed = (volume & 0xFFFFFFFF, file_id & 0xFFFFFFFFFFFFFFFF)+ connection.execute(+ "UPDATE scans SET target_device = ?, target_inode = ? WHERE id = ?",+ (*map(serialize_filesystem_identity, narrowed), scan_id),+ )+ connection.commit()+ metadata = target.stat()+ if narrowed == (metadata.st_dev, metadata.st_ino):+ assert scan_command(state, "get-cli-scan-resume", scan_id)["scanId"] == scan_id+ else:+ refused = scan_command(state, "get-cli-scan-resume", scan_id, check=False)+ assert refused["returncode"] != 0 and f"scans rerun {scan_id}" in refused["stderr"]++[email protected]("archive", [False, True], ids=["fresh", "archive-existing"])[email protected]("full_ids", [True, False], ids=["native-id", "stat-only"])+def test_registration_restores_output_when_checkout_is_replaced_before_commit(+ deep_registration, monkeypatch: pytest.MonkeyPatch, archive: bool, full_ids: bool+) -> None:+ state, output, register = deep_registration+ target = output.parent / "repository"+ monkeypatch.setenv("CODEX_SECURITY_STATE_DIR", str(state))+ previous_file = output / "previous result"+ with closing(workbench_db.connect()) as connection:+ previous_id = None+ if archive:+ previous_id = register("standard", connection=connection)["scanId"]+ connection.execute("UPDATE scans SET status = 'failed' WHERE id = ?", (previous_id,))+ connection.commit()+ previous_file.write_bytes(b"previous result")+ count_files = workbench_db.scan_scope_file_count++ def replace_target(*args):+ count = count_files(*args)+ original = target.with_name("original checkout")+ target.rename(original)+ target.mkdir()+ (target / "source.py").write_bytes((original / "source.py").read_bytes())+ return count++ with monkeypatch.context() as patch:+ patch.setattr(workbench_db, "scan_scope_file_count", replace_target)+ if not full_ids:+ patch.setattr(windows_scan_local_files, "_full_directory_id", lambda *args: None)+ with pytest.raises(SystemExit, match="target changed while the scan was starting"):+ register(archive=archive, connection=connection)+ ids = [row[0] for row in connection.execute("SELECT id FROM scans")]+ assert not list(output.parent.glob(f"{output.name}.previous-*"))+ if archive:+ assert ids == [previous_id]+ assert previous_file.read_bytes() == b"previous result"+ else:+ assert ids == [] and list(output.iterdir()) == []++@pytest.mark.parametrize("legacy_ids", [False, True], ids=["full-file-id", "legacy-file-index"])def test_deep_resume_reads_both_pinned_directory_identities(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, legacy_ids: booldiff --git a/plugins/codex-security/tests/test_workbench_deep_scan.py b/plugins/codex-security/tests/test_workbench_deep_scan.pyindex ecdf1dc67..d5d713235 100644--- a/plugins/codex-security/tests/test_workbench_deep_scan.py+++ b/plugins/codex-security/tests/test_workbench_deep_scan.py@@ -133,6 +133,7 @@ def restore_v53_schema(state_dir: Path) -> None:connection.execute("ALTER TABLE deep_scan_runs DROP COLUMN worker_usage_gap")connection.execute("DROP TRIGGER classify_legacy_scan_recipe")connection.execute("DROP VIEW scan_lifecycles")+ connection.execute("ALTER TABLE scans DROP COLUMN windows_resume_target_identity")connection.execute("ALTER TABLE scans DROP COLUMN windows_resume_output_identity")connection.execute("ALTER TABLE scans DROP COLUMN log_completed_at")connection.execute("ALTER TABLE scans DROP COLUMN launch_kind")diff --git a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py b/plugins/codex-security/tests/test_workbench_setup_and_migrations.pyindex d951e03b8..f78172ad5 100644--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py@@ -110,6 +110,7 @@ EXPECTED_MIGRATIONS = [(63, "retain unverified Deep Scan worker attempts"),(64, "persist scan log completion boundaries"),(65, "bind Windows Deep Scan resume output directories"),+ (66, "bind Windows Deep Scan resume checkouts"),]@@ -1216,7 +1217,13 @@ def test_log_completion_migration_preserves_unknown_legacy_boundaries() -> None:after = [dict(row) for row in connection.execute("SELECT * FROM scans ORDER BY id")]assert after == [- {**row, "log_completed_at": None, "windows_resume_output_identity": None} for row in before+ {+ **row,+ "log_completed_at": None,+ "windows_resume_output_identity": None,+ "windows_resume_target_identity": None,+ }+ for row in before]diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.mdindex 220db7353..4e0c4d594 100644--- a/sdk/typescript/docs/cli.md+++ b/sdk/typescript/docs/cli.md@@ -2148,6 +2148,13 @@ output. It lets the workbench verify the original output even before the firstcheckpoint. If an older scan stopped with an empty output, start a new scan with`codex-security scans rerun SCAN_ID`.+New Windows Deep Scans also record the checkout's native volume and file ID+when the filesystem supplies them. Validation and remediation verify the same+checkout identity. An older scan can resume across Python+versions when its saved checkout identity can be verified. If the workbench+cannot verify it, resume with the Python runtime that started the scan or use+`codex-security scans rerun SCAN_ID` to start a new scan.+Resume retains scan ID, completed workers, artifacts, accumulated cost, and savedsettings/instructions. Knowledge-bearing scans save their extracted documents inthe private `.scan-knowledge.json` continuation file beside the scan artifacts.