Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(windows): resume Deep Scans interrupted before their first artifact (#2515)

Michael D'Angelo91c01f08ab2e
diff --git a/plugins/codex-security/scripts/windows_scan_local_files.py b/plugins/codex-security/scripts/windows_scan_local_files.py
index c3c6b6ad4..46f3cb191 100644
--- a/plugins/codex-security/scripts/windows_scan_local_files.py
+++ b/plugins/codex-security/scripts/windows_scan_local_files.py
@@ -78,6 +78,7 @@ _FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000
_FILE_RENAME_INFO_CLASS = 3
_FILE_DISPOSITION_INFO_CLASS = 4
_FILE_ATTRIBUTE_TAG_INFO_CLASS = 9
+_FILE_ID_INFO_CLASS = 18
_FILE_TYPE_DISK = 0x0001
_FILE_NAME_OPENED = 0x00000008
@@ -130,6 +131,25 @@ class _FileAttributeTagInfo(ctypes.Structure):
]
+class _FileIdInfo(ctypes.Structure):
+ _fields_ = [("VolumeSerialNumber", ctypes.c_uint64), ("FileId", ctypes.c_ubyte * 16)]
+
+
+class _ByHandleFileInformation(ctypes.Structure):
+ _fields_ = [
+ ("dwFileAttributes", wintypes.DWORD),
+ ("ftCreationTime", wintypes.FILETIME),
+ ("ftLastAccessTime", wintypes.FILETIME),
+ ("ftLastWriteTime", wintypes.FILETIME),
+ ("dwVolumeSerialNumber", wintypes.DWORD),
+ ("nFileSizeHigh", wintypes.DWORD),
+ ("nFileSizeLow", wintypes.DWORD),
+ ("nNumberOfLinks", wintypes.DWORD),
+ ("nFileIndexHigh", wintypes.DWORD),
+ ("nFileIndexLow", wintypes.DWORD),
+ ]
+
+
class _SecurityAttributes(ctypes.Structure):
_fields_ = [
("length", wintypes.DWORD),
@@ -230,6 +250,12 @@ if os.name == "nt":
wintypes.DWORD,
]
_GetFileInformationByHandleEx.restype = wintypes.BOOL
+ _GetFileInformationByHandle = _kernel32.GetFileInformationByHandle
+ _GetFileInformationByHandle.argtypes = [
+ wintypes.HANDLE,
+ ctypes.POINTER(_ByHandleFileInformation),
+ ]
+ _GetFileInformationByHandle.restype = wintypes.BOOL
_GetFileType = _kernel32.GetFileType
_GetFileType.argtypes = [wintypes.HANDLE]
@@ -550,7 +576,7 @@ def _current_user_sid() -> str:
return _sid_text(wintypes.LPVOID.from_buffer(user))
-def _output_directory_grants(handle: int, path: Path) -> int:
+def _output_directory_grants(handle: int, path: Path, *, expected_descriptor=None) -> int:
"""Inspect the descriptor of the opened directory, even if its pathname changes."""
owner, acl, descriptor = wintypes.LPVOID(), wintypes.LPVOID(), wintypes.LPVOID()
status = _advapi32.GetSecurityInfo(
@@ -566,6 +592,10 @@ def _output_directory_grants(handle: int, path: Path) -> int:
if status:
raise ctypes.WinError(status)
try:
+ if expected_descriptor is not None and _generated_directory_policy(
+ descriptor
+ ) != _generated_directory_policy(expected_descriptor):
+ raise _invalid_path(path, "new scan directory lacks its private owner or DACL")
trusted = {
_current_user_sid(),
"S-1-5-18",
@@ -651,8 +681,8 @@ def _require_stable_output(path: Path, parent_grants: int, child_grants: int = 0
@contextlib.contextmanager
-def _checked_output_directory(path: Path) -> Iterator[tuple[int, int]]:
- """Hold the checked canonical chain and expose its final directory handle and grants."""
+def _checked_output_directory(path: Path) -> Iterator[tuple[int | None, int, int]]:
+ """Hold the checked chain and expose its parent, final directory handle and grants."""
parent_grants = 0
parent = None
with contextlib.ExitStack() as pins:
@@ -678,18 +708,57 @@ def _checked_output_directory(path: Path) -> Iterator[tuple[int, int]]:
_verify_directory(handle.value, directory)
grants = _output_directory_grants(handle.value, directory)
_require_stable_output(directory, parent_grants, grants)
- parent, parent_grants = handle.value, grants
- yield parent, parent_grants
+ containing, parent, parent_grants = parent, handle.value, grants
+ yield containing, parent, parent_grants
def require_windows_output_path(path: Path, *, writable_output: bool = False) -> None:
- with _checked_output_directory(path) as (_, grants):
+ with _checked_output_directory(path) as (_, _, grants):
if writable_output and grants & _SET_REPARSE_POINT:
raise _invalid_path(
path, "Another user can turn the output directory into a reparse point when empty."
)
+DEEP_RESUME_DIRECTORY = ".codex-security-resume"
+
+
+def deep_resume_output_identity(scan_dir: Path) -> list[int]:
+ """Bind the output and its protected child while every directory is pinned."""
+ retained = scan_dir / DEEP_RESUME_DIRECTORY
+ with _checked_output_directory(retained) as (parent, child, grants):
+ assert parent is not None
+ return _pinned_resume_identity(parent, child, retained, grants)
+
+
+def _resume_directory_id(handle: int, path: Path) -> tuple[int, int]:
+ info = _FileIdInfo()
+ if _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:
+ _raise_last_error("GetFileInformationByHandleEx(FileIdInfo)", path)
+ if not file_id:
+ raise _invalid_path(path, "The resume directory has no stable file identity.")
+ return volume, file_id
+
+
+def _pinned_resume_identity(parent: int, child: int, retained: Path, grants: int) -> list[int]:
+ # A nonreplaceable, protected child keeps the scan root nonempty after
+ # the sandbox receives Modify there. The caller holds both directories.
+ if grants:
+ raise _invalid_path(retained, "The retained resume directory must be protected.")
+ return [*_resume_directory_id(parent, retained.parent), *_resume_directory_id(child, retained)]
+
+
class _UnicodeString(ctypes.Structure):
_fields_ = [
("length", wintypes.USHORT),
@@ -710,7 +779,7 @@ class _ObjectAttributes(ctypes.Structure):
def _relative_output_directory(
- parent: int, basename: str, descriptor=None
+ parent: int, basename: str, descriptor=None, *, rollback_handle: bool = False
) -> tuple[int, int | None]:
"""Open or create one component relative to its checked parent without reparsing it."""
size = len(basename.encode("utf-16-le", errors="surrogatepass"))
@@ -729,12 +798,16 @@ def _relative_output_directory(
io_status = (ctypes.c_void_p * 2)() # IO_STATUS_BLOCK
status = _ntdll.NtCreateFile(
ctypes.byref(handle),
- _FILE_TRAVERSE | _FILE_READ_ATTRIBUTES | _READ_CONTROL | 0x100000,
+ _FILE_TRAVERSE
+ | _FILE_READ_ATTRIBUTES
+ | _READ_CONTROL
+ | 0x100000
+ | (_DELETE if rollback_handle else 0),
ctypes.byref(attributes),
ctypes.byref(io_status),
None,
_FILE_ATTRIBUTE_NORMAL,
- _FILE_SHARE_READ | _FILE_SHARE_WRITE,
+ 0 if rollback_handle else _FILE_SHARE_READ | _FILE_SHARE_WRITE,
1 if descriptor is None else 2, # FILE_OPEN or FILE_CREATE
0x200021, # FILE_OPEN_REPARSE_POINT | FILE_SYNCHRONOUS_IO_NONALERT | FILE_DIRECTORY_FILE
None,
@@ -744,7 +817,7 @@ def _relative_output_directory(
def create_windows_output_directory(path: Path, descriptor: wintypes.LPVOID) -> int:
- with _checked_output_directory(path.parent) as (parent, grants):
+ with _checked_output_directory(path.parent) as (_, parent, grants):
_require_stable_output(path, grants)
error, handle = _relative_output_directory(parent, path.name, descriptor)
if not error:
@@ -752,8 +825,8 @@ def create_windows_output_directory(path: Path, descriptor: wintypes.LPVOID) ->
return error
-def create_private_directory(path: Path, *, exist_ok: bool, scan_output: bool) -> Path:
- """Create one Windows directory with a protected inheritable DACL."""
+def _private_directory_descriptor(path: Path, *, explicit_owner: bool) -> Iterator[wintypes.LPVOID]:
_require_windows()
# Older supported Python releases ignore mkdir's 0700 mode on Windows.
# Apply the same owner/SYSTEM/Administrators policy at creation so a
@@ -770,7 +843,7 @@ def create_private_directory(path: Path, *, exist_ok: bool, scan_output: bool) -
raise ValueError("Select a directory on a Windows volume that supports ACLs.")
sid = _current_user_sid()
- owner = "" if exist_ok else f"O:{sid}"
+ owner = f"O:{sid}" if explicit_owner else ""
dacl = f"{owner}D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;FA;;;{sid})"
descriptor = wintypes.LPVOID()
@@ -779,6 +852,14 @@ def create_private_directory(path: Path, *, exist_ok: bool, scan_output: bool) -
):
raise ctypes.WinError(ctypes.get_last_error())
try:
+ yield descriptor
+ finally:
+ _kernel32.LocalFree(descriptor)
+
+
+def create_private_directory(path: Path, *, exist_ok: bool, scan_output: bool) -> Path:
+ """Create one Windows directory with a protected inheritable DACL."""
+ with _private_directory_descriptor(path, explicit_owner=not exist_ok) as descriptor:
if scan_output:
error = create_windows_output_directory(path, descriptor)
else:
@@ -795,8 +876,34 @@ def create_private_directory(path: Path, *, exist_ok: bool, scan_output: bool) -
require_windows_output_path(path)
return path
raise ctypes.WinError(error)
- finally:
- _kernel32.LocalFree(descriptor)
+
+
+def create_deep_resume_output(scan_dir: Path, committed: Callable[[], bool]) -> Iterator[list[int]]:
+ """Retain the exact creation handle through verification and database commit."""
+ retained = scan_dir / DEEP_RESUME_DIRECTORY
+ with (
+ _private_directory_descriptor(retained, explicit_owner=True) as descriptor,
+ _checked_output_directory(scan_dir) as (_, parent, grants),
+ ):
+ _require_stable_output(retained, grants)
+ error, child = _relative_output_directory(
+ parent, retained.name, descriptor, rollback_handle=True
+ )
+ if error:
+ raise ctypes.WinError(error)
+ assert child is not None
+ with _OwnedHandle(child):
+ try:
+ _verify_directory(child, retained)
+ child_grants = _output_directory_grants(
+ child, retained, expected_descriptor=descriptor
+ )
+ yield _pinned_resume_identity(parent, child, retained, child_grants)
+ except BaseException:
+ if not committed():
+ _mark_handle_for_deletion(child)
+ raise
def create_directory_with_same_dacl(path: Path, source: Path) -> None:
diff --git a/plugins/codex-security/scripts/workbench_db.py b/plugins/codex-security/scripts/workbench_db.py
index ee4cee6eb..fb8c6bbd8 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,
+ register_windows_resume_output,
safe_segment,
scan_diff_identity,
scan_target_identity,
@@ -1504,6 +1505,17 @@ def register_cli_scan(
an output directory, coordinate archiving with registration so previous results
can still be located. Return the scan ID, contract, and target metadata.
"""
+ # A selected plugin may replace __main__ while delegating to this workbench.
+ before_output_change = before_archive or getattr(
+ sys, "_codex_security_before_output_change", None
+ )
+
+ def acknowledge_output_change() -> None:
+ nonlocal before_output_change
+ if before_output_change is not None:
+ before_output_change()
+ before_output_change = None
+
repository = require_target(args.repository)
require_scannable_target(repository)
scan_dir = None
@@ -1588,7 +1600,7 @@ def register_cli_scan(
scan_dir,
timestamp,
require_canonical_scan_directory,
- before_archive=before_archive,
+ before_archive=acknowledge_output_change,
)
)
break
@@ -1607,7 +1619,6 @@ def register_cli_scan(
if remaining <= 0:
raise
connection.rollback()
- before_archive = None # The host already acknowledged this registration.
scan_dir = None # Revalidate the directory and its owner under the next lock.
time.sleep(min(0.05, remaining))
connection.execute("BEGIN IMMEDIATE")
@@ -1670,6 +1681,9 @@ 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))
scan = connection.execute(
"SELECT scans.*, snapshot AS lifecycle_json FROM scans "
"JOIN scan_lifecycles ON scan_id = scans.id WHERE scans.id = ?",
diff --git a/plugins/codex-security/scripts/workbench_scan_history.py b/plugins/codex-security/scripts/workbench_scan_history.py
index aba6045db..47527ef7b 100644
--- a/plugins/codex-security/scripts/workbench_scan_history.py
+++ b/plugins/codex-security/scripts/workbench_scan_history.py
@@ -112,6 +112,26 @@ def cli_scan_resume(
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":
+ from windows_scan_local_files import DEEP_RESUME_DIRECTORY, deep_resume_output_identity
+
+ identity = scan["windows_resume_output_identity"]
+ if identity is not None:
+ try:
+ current = deep_resume_output_identity(scan_dir)
+ except OSError as exc:
+ raise SystemExit(
+ f"Cannot verify the registered Windows resume output: {exc}"
+ ) from exc
+ if current != json.loads(identity):
+ raise SystemExit(
+ "Cannot resume: the original Windows output directory was replaced."
+ )
+ elif not any(entry != scan_dir / DEEP_RESUME_DIRECTORY for entry in scan_dir.iterdir()):
+ raise SystemExit(
+ "Cannot resume this empty Windows output because its original directory identity "
+ f"was not saved. Start a new scan with 'codex-security scans rerun {scan['id']}'."
+ )
result = {
"contract": wb.scan_contract(scan),
"lifecycle": json.loads(scan["lifecycle_json"]),
diff --git a/plugins/codex-security/scripts/workbench_scan_start.py b/plugins/codex-security/scripts/workbench_scan_start.py
index 89ca041b6..de93d0429 100644
--- a/plugins/codex-security/scripts/workbench_scan_start.py
+++ b/plugins/codex-security/scripts/workbench_scan_start.py
@@ -269,6 +269,28 @@ def archive_scan(
raise
+@contextmanager
+def register_windows_resume_output(
+ connection: sqlite3.Connection, scan_id: str, scan_dir: Path
+) -> Iterator[None]:
+ from windows_scan_local_files import create_deep_resume_output
+
+ def committed() -> bool:
+ # A signal can arrive after commit. The registered scan then owns its child.
+ return (
+ not connection.in_transaction
+ and connection.execute("SELECT 1 FROM scans WHERE id = ?", (scan_id,)).fetchone()
+ 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
+
+
def insert_running_scan(
connection: sqlite3.Connection,
*,
diff --git a/plugins/codex-security/shared/workbench-migrations.json b/plugins/codex-security/shared/workbench-migrations.json
index b41c16a32..d47ceafb1 100644
--- a/plugins/codex-security/shared/workbench-migrations.json
+++ b/plugins/codex-security/shared/workbench-migrations.json
@@ -522,5 +522,12 @@
"version": 64,
"name": "persist scan log completion boundaries",
"statements": ["ALTER TABLE scans ADD COLUMN log_completed_at TEXT;"]
+ },
+ {
+ "version": 65,
+ "name": "bind Windows Deep Scan resume output directories",
+ "statements": [
+ "ALTER TABLE scans ADD COLUMN windows_resume_output_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.py
index a453a6d59..3983b3b9a 100644
--- a/plugins/codex-security/tests/test_windows_workbench_output_permissions.py
+++ b/plugins/codex-security/tests/test_windows_workbench_output_permissions.py
@@ -4,13 +4,19 @@ from __future__ import annotations
import argparse
import base64
+import ctypes
+import json
import os
+import sqlite3
import string
import subprocess
import sys
+from concurrent.futures import ThreadPoolExecutor
+from contextlib import closing
from pathlib import Path
import pytest
+from workbench_test_support import run_workbench, scan_command
SCRIPTS = Path(__file__).resolve().parents[1] / "scripts"
sys.path.insert(0, str(SCRIPTS))
@@ -33,6 +39,279 @@ def make_junction(path: Path, target: Path) -> None:
subprocess.run(["cmd", "/c", "mklink", "/J", str(path), str(target)], check=True)
+def deep_registration(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
+ repository = tmp_path / "repository"
+ repository.mkdir()
+ (repository / "source.py").write_text("# synthetic source\n")
+ output = tmp_path / "output"
+ output.mkdir()
+ state = tmp_path / "state"
+ recipe = {
+ "config": {},
+ "repository": str(repository),
+ "target": {"kind": "repository", "paths": []},
+ }
+
+ def register(mode="deep", *, archive=False, connection=None, **options):
+ arguments = [
+ "register-cli-scan",
+ "--repository",
+ str(repository),
+ "--scan-dir",
+ str(output),
+ "--recipe-json",
+ json.dumps({**recipe, "mode": mode}),
+ ]
+ if archive:
+ arguments.append("--archive-existing")
+ if connection is None:
+ return run_workbench(state, *arguments, **options)
+ with monkeypatch.context() as patch:
+ patch.setattr(sys, "argv", ["workbench", *arguments])
+ return workbench_db.register_cli_scan(
+ connection, workbench_db.parse_args("test workbench")
+ )
+
+ return state, output, register
+
+
[email protected]("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: bool
+) -> None:
+ output = tmp_path / "output"
+ output.mkdir()
+ child = output / windows_scan_local_files.DEEP_RESUME_DIRECTORY
+ lstat = Path.lstat
+
+ def directory_entry_stat(path: Path) -> os.stat_result:
+ if path not in (output, child):
+ return lstat(path)
+ # Windows directory enumeration can supply attributes without a file index.
+ with os.scandir(path.parent) as entries:
+ entry = next(entry for entry in entries if entry.name == path.name)
+ return entry.stat(follow_symlinks=False)
+
+ monkeypatch.setattr(Path, "lstat", directory_entry_stat)
+ if legacy_ids:
+ get_info = windows_scan_local_files._GetFileInformationByHandleEx
+
+ def without_file_id(handle, info_class, *arguments):
+ if info_class == windows_scan_local_files._FILE_ID_INFO_CLASS:
+ ctypes.set_last_error(50) # ERROR_NOT_SUPPORTED.
+ return False
+ return get_info(handle, info_class, *arguments)
+
+ monkeypatch.setattr(
+ windows_scan_local_files, "_GetFileInformationByHandleEx", without_file_id
+ )
+
+ with windows_scan_local_files.create_deep_resume_output(output, lambda: False) as saved:
+ assert output.lstat().st_ino == child.lstat().st_ino == 0
+ root_volume, root_id, child_volume, child_id = saved
+ assert root_volume == child_volume
+ assert root_id and child_id and root_id != child_id
+ assert windows_scan_local_files.deep_resume_output_identity(output) == saved
+
+
+ "change",
+ [
+ "replaced root",
+ "replaced child",
+ "different recorded volume",
+ "missing child",
+ "deletable child",
+ "delete child right",
+ ],
+)
+def test_deep_resume_requires_the_registered_protected_directories(
+ deep_registration, tmp_path: Path, change: str
+) -> None:
+ state, output, register = deep_registration
+ scan_id = register()["scanId"]
+ scan_command(state, "set-scan-thread", scan_id, "--thread-id", "original-thread")
+ retained = output / windows_scan_local_files.DEEP_RESUME_DIRECTORY
+ assert list(output.iterdir()) == [retained]
+ assert list(retained.iterdir()) == []
+ grant(output, "(OI)(CI)M")
+ assert scan_command(state, "get-cli-scan-resume", scan_id)["threadId"] == "original-thread"
+
+ if change == "replaced root":
+ previous = tmp_path / "original root"
+ output.rename(previous)
+ output.mkdir()
+ (previous / retained.name).rename(retained)
+ elif change == "replaced child":
+ retained.rename(tmp_path / "original child")
+ windows_scan_local_files.create_private_directory(
+ retained, exist_ok=False, scan_output=True
+ )
+ elif change == "different recorded volume":
+ identity = windows_scan_local_files.deep_resume_output_identity(output)
+ identity[0] ^= 1 << 32 # The low 32 bits and both file IDs still match.
+ with closing(sqlite3.connect(state / "workbench.sqlite3")) as connection, connection:
+ connection.execute(
+ "UPDATE scans SET windows_resume_output_identity = ? WHERE id = ?",
+ (json.dumps(identity), scan_id),
+ )
+ elif change == "missing child":
+ retained.rmdir()
+ elif change == "deletable child":
+ grant(retained, "(DE)")
+ else:
+ grant(output, "(DC)")
+
+ # Even a populated replacement cannot downgrade a recorded binding to legacy recovery.
+ (output / "checkpoint.json").write_text("{}")
+ rejected = scan_command(state, "get-cli-scan-resume", scan_id, check=False)
+ assert rejected["returncode"] != 0
+ assert "Windows" in rejected["stderr"]
+ assert (output / "checkpoint.json").read_text() == "{}"
+
+
+def test_legacy_windows_resume_keeps_populated_output_and_rejects_unbound_empty_output(
+ deep_registration,
+) -> None:
+ state, output, register = deep_registration
+ 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:
+ connection.execute(
+ "UPDATE scans SET windows_resume_output_identity = NULL WHERE id = ?", (scan_id,)
+ )
+ retained = output / windows_scan_local_files.DEEP_RESUME_DIRECTORY
+ # Neither a private directory nor a lookalike retained child proves legacy ownership.
+ for has_guard in (True, False):
+ if not has_guard:
+ retained.rmdir()
+ grant(output, "(OI)(CI)M")
+ rejected = scan_command(state, "get-cli-scan-resume", scan_id, check=False)
+ assert rejected["returncode"] != 0
+ assert f"scans rerun {scan_id}" in rejected["stderr"]
+ (output / "checkpoint.json").write_text("{}")
+ assert scan_command(state, "get-cli-scan-resume", scan_id)["scanDir"] == str(output)
+
+
+def test_deep_registration_rolls_back_the_retained_child(deep_registration) -> None:
+ state, output, register = deep_registration
+ run_workbench(state, "list-scans")
+ with closing(sqlite3.connect(state / "workbench.sqlite3")) as connection, connection:
+ connection.execute(
+ "CREATE TRIGGER fail_binding BEFORE UPDATE OF windows_resume_output_identity ON scans "
+ "BEGIN SELECT RAISE(ABORT, 'synthetic registration failure'); END"
+ )
+ assert "synthetic registration failure" in register(check=False)["stderr"]
+ assert list(output.iterdir()) == []
+ with closing(sqlite3.connect(state / "workbench.sqlite3")) as connection, connection:
+ assert connection.execute("SELECT count(*) FROM scans").fetchone()[0] == 0
+ connection.execute("DROP TRIGGER fail_binding")
+ with ThreadPoolExecutor(max_workers=2) as retries:
+ outcomes = [retries.submit(register, check=False) for _ in range(2)]
+ assert sorted(outcome.result()["returncode"] for outcome in outcomes) == [0, 1]
+ with closing(sqlite3.connect(state / "workbench.sqlite3")) as connection:
+ [(scan_id,)] = connection.execute("SELECT id FROM scans").fetchall()
+ scan_command(state, "set-scan-thread", scan_id, "--thread-id", "original-thread")
+ assert scan_command(state, "get-cli-scan-resume", scan_id)["scanId"] == scan_id
+
+
[email protected]("archive", [False, True], ids=["fresh", "archive-existing"])
+def test_resume_verification_failure_rolls_back_the_created_handle(
+ deep_registration, monkeypatch: pytest.MonkeyPatch, archive: bool
+) -> None:
+ state, output, register = deep_registration
+ monkeypatch.setenv("CODEX_SECURITY_STATE_DIR", str(state))
+ retained = output / windows_scan_local_files.DEEP_RESUME_DIRECTORY
+ previous_file = output / "previous result"
+
+ with closing(workbench_db.connect()) as connection:
+ old_id = None
+ if archive:
+ old_id = register("standard", connection=connection)["scanId"]
+ connection.execute("UPDATE scans SET status = 'failed' WHERE id = ?", (old_id,))
+ connection.commit()
+ previous_file.write_bytes(b"original result")
+ original_grants = windows_scan_local_files._output_directory_grants
+
+ def competing_reader(handle, path, **options):
+ if path == retained and "expected_descriptor" in options:
+ # The original handle is already held when verification starts.
+ # Propagate the real sharing error to interrupt this registration.
+ contender = windows_scan_local_files._create_file(
+ retained,
+ access=windows_scan_local_files._FILE_TRAVERSE,
+ share=0,
+ disposition=windows_scan_local_files._OPEN_EXISTING,
+ flags=windows_scan_local_files._FILE_FLAG_BACKUP_SEMANTICS
+ | windows_scan_local_files._FILE_FLAG_OPEN_REPARSE_POINT,
+ )
+ with contender:
+ pytest.fail("An exclusive reader opened the tentative resume directory")
+ return original_grants(handle, path, **options)
+
+ with monkeypatch.context() as patch:
+ patch.setattr(windows_scan_local_files, "_output_directory_grants", competing_reader)
+ with pytest.raises(OSError) as failure:
+ register(archive=archive, connection=connection)
+ assert (getattr(failure.value, "winerror", None) or failure.value.errno) == 32
+ assert not retained.exists()
+ assert not list(output.parent.glob(f"{output.name}.previous-*"))
+ rows = connection.execute("SELECT id, scan_dir FROM scans").fetchall()
+ if archive:
+ assert [tuple(row) for row in rows] == [(old_id, str(output))]
+ assert previous_file.read_bytes() == b"original result"
+ else:
+ assert rows == []
+ assert list(output.iterdir()) == []
+ assert register(archive=archive, connection=connection)["scanDir"] == str(output)
+
+
+def test_resume_cleanup_cannot_remove_an_existing_or_substituted_child(tmp_path: Path) -> None:
+ output = tmp_path / "output"
+ output.mkdir()
+ retained = output / windows_scan_local_files.DEEP_RESUME_DIRECTORY
+ original = tmp_path / "another directory"
+ original.mkdir()
+ sentinel = original / "keep"
+ sentinel.write_bytes(b"owned by another operation")
+ original.rename(retained)
+ with (
+ pytest.raises(FileExistsError),
+ windows_scan_local_files.create_deep_resume_output(output, lambda: False),
+ ):
+ pytest.fail("An existing child was adopted")
+ retained.rename(original)
+ with (
+ pytest.raises(PermissionError) as failure,
+ windows_scan_local_files.create_deep_resume_output(output, lambda: False),
+ ):
+ retained.rename(tmp_path / "moved")
+ assert failure.value.winerror == 32
+ assert not retained.exists()
+ assert sentinel.read_bytes() == b"owned by another operation"
+
+
+def test_post_commit_interrupt_keeps_the_registered_resume_directory(deep_registration) -> None:
+ state, output, register = deep_registration
+ run_workbench(state, "list-scans")
+
+ class InterruptedCommit(sqlite3.Connection):
+ def commit(self):
+ super().commit()
+ raise KeyboardInterrupt
+
+ with closing(
+ sqlite3.connect(state / "workbench.sqlite3", factory=InterruptedCommit)
+ ) as connection:
+ connection.row_factory = sqlite3.Row
+ with pytest.raises(KeyboardInterrupt):
+ register(connection=connection)
+ [(scan_id,)] = connection.execute("SELECT id FROM scans").fetchall()
+ scan_command(state, "set-scan-thread", scan_id, "--thread-id", "original-thread")
+ assert scan_command(state, "get-cli-scan-resume", scan_id)["scanDir"] == str(output)
+
+
def test_missing_output_drive_reports_a_filesystem_error() -> None:
for letter in reversed(string.ascii_uppercase):
drive = Path(f"{letter}:/")
diff --git a/plugins/codex-security/tests/test_workbench_deep_scan.py b/plugins/codex-security/tests/test_workbench_deep_scan.py
index 9885462af..ecdf1dc67 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_output_identity")
connection.execute("ALTER TABLE scans DROP COLUMN log_completed_at")
connection.execute("ALTER TABLE scans DROP COLUMN launch_kind")
connection.execute("ALTER TABLE scans DROP COLUMN launch_plugin_version")
diff --git a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
index 9568bbc3f..1bad134a3 100644
--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py
@@ -109,6 +109,7 @@ EXPECTED_MIGRATIONS = [
(62, "project scan lifecycle snapshots"),
(63, "retain unverified Deep Scan worker attempts"),
(64, "persist scan log completion boundaries"),
+ (65, "bind Windows Deep Scan resume output directories"),
]
@@ -1196,7 +1197,9 @@ def test_log_completion_migration_preserves_unknown_legacy_boundaries() -> None:
apply_migrations(connection)
after = [dict(row) for row in connection.execute("SELECT * FROM scans ORDER BY id")]
- assert after == [{**row, "log_completed_at": None} for row in before]
+ assert after == [
+ {**row, "log_completed_at": None, "windows_resume_output_identity": None} for row in before
+ ]
@pytest.mark.parametrize(
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.md
index 2c29bf59a..f0fb95fd7 100644
--- a/sdk/typescript/docs/cli.md
+++ b/sdk/typescript/docs/cli.md
@@ -2133,6 +2133,11 @@ The scan must still be `running`, with its original checkout, output directory,
and Codex session in the same state directory. Checkout identity, revision, and
contents must match. Completed, failed, and canceled scans need a rerun instead.
+On Windows, keep the protected `.codex-security-resume` directory in the scan
+output. It lets the workbench verify the original output even before the first
+checkpoint. If an older scan stopped with an empty output, start a new scan with
+`codex-security scans rerun SCAN_ID`.
+
Resume retains scan ID, completed workers, artifacts, accumulated cost, and saved
settings/instructions. Knowledge-bearing scans save their extracted documents in
the private `.scan-knowledge.json` continuation file beside the scan artifacts.
diff --git a/sdk/typescript/src/api.ts b/sdk/typescript/src/api.ts
index dc0f8a4db..9d2d39896 100644
--- a/sdk/typescript/src/api.ts
+++ b/sdk/typescript/src/api.ts
@@ -2411,7 +2411,7 @@ export class CodexSecurity {
);
requireOutputOutsideRepository(protectedRoot, scanDir);
requireModelSafeOutputDir(scanDir);
- if (!options.archiveExisting) {
+ if (!options.archiveExisting && options.resumeScanId === undefined) {
notifyObserver(options, "onOutputDirReady")(scanDir);
}
checkOpen();
@@ -3163,7 +3163,7 @@ export class CodexSecurity {
"onOutputArchived",
)(registration["archivedScanDir"]);
}
- if (options.archiveExisting) {
+ if (options.archiveExisting || options.resumeScanId !== undefined) {
notifyObserver(options, "onOutputDirReady")(scanDir);
}
throwIfAborted(signal, scanDir);
diff --git a/sdk/typescript/src/runtime.ts b/sdk/typescript/src/runtime.ts
index ec60f66e9..30ba1a704 100644
--- a/sdk/typescript/src/runtime.ts
+++ b/sdk/typescript/src/runtime.ts
@@ -61,6 +61,7 @@ import {
} from "node:path";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
+import type { Duplex } from "node:stream";
import { pipeline } from "node:stream/promises";
import { crc32 } from "node:zlib";
import { setTimeout as delay } from "node:timers/promises";
@@ -1800,20 +1801,21 @@ sys.argv = sys.argv[1:]
runpy.run_path(sys.argv[0], run_name="__main__")
`;
-const ARCHIVE_READY = "codex-security-archive-ready\n";
-const ARCHIVE_REGISTRATION_PROGRAM = String.raw`
-import io, json, runpy, sys
-control = sys.stdin
-payload = json.loads(control.readline())
-sys.stdin = io.TextIOWrapper(io.BytesIO(payload.encode("utf-8")), encoding="utf-8")
+const WORKBENCH_REGISTRATION_PROGRAM = String.raw`
+import os, runpy, sys
sys.argv = sys.argv[1:]
-workbench = runpy.run_path(sys.argv[0])
-def before_archive():
- sys.stdout.buffer.write(b"codex-security-archive-ready\n")
- sys.stdout.buffer.flush()
- if control.readline() != "\n":
- raise SystemExit("Scan registration canceled before archival.")
-workbench["main"](before_archive=before_archive)
+def before_output_change():
+ with os.fdopen(3, "r+b", buffering=0) as control:
+ control.write(b"?")
+ if control.read(1) != b"!":
+ raise SystemExit("Scan registration canceled before changing the output.")
+sys._codex_security_before_output_change = before_output_change
+if "--archive-existing" in sys.argv[1:] and "--archived-scan-dir" not in sys.argv[1:]:
+ # Released workbenches accept this callback before moving an existing output.
+ runpy.run_path(sys.argv[0])["main"](before_archive=before_output_change)
+else:
+ # A selected plugin keeps its script entrypoint, argv, stdin and stdout.
+ runpy.run_path(sys.argv[0], run_name="__main__")
`;
export async function runWorkbench(
@@ -1830,7 +1832,7 @@ export async function runWorkbench(
const run = async (
arguments_: readonly string[],
input?: string,
- archiveHandshake = false,
+ registrationHandshake = false,
): Promise<string> => {
const native =
nativeLocalDedupe ||
@@ -1885,8 +1887,8 @@ export async function runWorkbench(
"-B",
"-c",
WORKBENCH_ARGUMENTS_PROGRAM +
- (archiveHandshake
- ? ARCHIVE_REGISTRATION_PROGRAM
+ (registrationHandshake
+ ? WORKBENCH_REGISTRATION_PROGRAM
: WORKBENCH_SCRIPT_PROGRAM),
script,
],
@@ -1916,7 +1918,7 @@ export async function runWorkbench(
)
: input,
signal,
- archiveHandshake,
+ registrationHandshake,
// Frame Python arguments on stdin to preserve NUL and long diagnostics.
native
? undefined
@@ -1933,12 +1935,13 @@ export async function runWorkbench(
};
let stdout: string;
try {
- const archiveHandshake =
+ const registrationHandshake =
signal !== undefined &&
args[0] === "register-cli-scan" &&
- args.includes("--archive-existing") &&
- !args.includes("--archived-scan-dir");
- stdout = await run(args, input, archiveHandshake);
+ (process.platform === "win32" ||
+ (args.includes("--archive-existing") &&
+ !args.includes("--archived-scan-dir")));
+ stdout = await run(args, input, registrationHandshake);
} catch (error) {
if (options.signal?.aborted) throw error;
const detail = processErrorDetail(error);
@@ -3580,19 +3583,22 @@ export async function runCodexCommand(
environment: ProcessEnvironment,
input?: string | Uint8Array,
signal?: AbortSignal,
- archiveHandshake = false,
+ registrationHandshake = false,
stdinPrefix?: string,
): Promise<CodexCommandResult> {
- const cancellation = archiveHandshake ? new AbortController() : undefined;
+ const cancellation = registrationHandshake
+ ? new AbortController()
+ : undefined;
const abort = () => cancellation?.abort(signal?.reason);
const commandSignal = cancellation?.signal ?? signal;
- let awaitingArchive = archiveHandshake;
const child = spawn(
executablePathForSpawn(command.command),
[...(command.args ?? []), ...args],
{
env: environment,
- stdio: ["pipe", "pipe", "pipe"],
+ stdio: registrationHandshake
+ ? ["pipe", "pipe", "pipe", "pipe"]
+ : ["pipe", "pipe", "pipe"],
windowsHide: true,
signal: commandSignal,
},
@@ -3600,19 +3606,20 @@ export async function runCodexCommand(
let stdout = "";
let stderr = "";
let processError: Error | undefined;
+ const control = child.stdio[3] as Duplex | undefined;
+ control?.once("data", () => {
+ // The helper cannot change the output until the host allows the transaction.
+ signal?.removeEventListener("abort", abort);
+ if (signal?.aborted) abort();
+ else control.end("!");
+ });
+ control?.on("error", (error: NodeJS.ErrnoException) => {
+ if (!isDisconnectedChildStdin(error)) processError = error;
+ });
child.stdout.setEncoding("utf8");
child.stderr.setEncoding("utf8");
child.stdout.on("data", (chunk: string) => {
stdout += chunk;
- if (awaitingArchive && stdout.startsWith(ARCHIVE_READY)) {
- awaitingArchive = false;
- stdout = stdout.slice(ARCHIVE_READY.length);
- signal?.removeEventListener("abort", abort);
- // The helper cannot rename until this acknowledgement. Detach and check
- // cancellation before allowing the filesystem/database transaction.
- if (signal?.aborted) abort();
- else child.stdin.end("\n");
- }
});
child.stderr.on("data", (chunk: string) => {
stderr += chunk;
@@ -3641,21 +3648,22 @@ export async function runCodexCommand(
child.stdin.destroy();
child.stdout.destroy();
child.stderr.destroy();
+ control?.destroy();
};
commandSignal?.addEventListener("abort", settleCanceledChild, { once: true });
child.once("exit", settleCanceledChild);
if (commandSignal?.aborted) settleCanceledChild();
- if (archiveHandshake) {
+ if (registrationHandshake) {
signal?.addEventListener("abort", abort, { once: true });
if (signal?.aborted) abort();
}
try {
if (stdinPrefix !== undefined) child.stdin.write(stdinPrefix);
- if (archiveHandshake) child.stdin.write(`${JSON.stringify(input ?? "")}\n`);
- else child.stdin.end(input);
+ child.stdin.end(input);
return await completion;
} finally {
- if (archiveHandshake) signal?.removeEventListener("abort", abort);
+ if (registrationHandshake) signal?.removeEventListener("abort", abort);
+ control?.destroy();
clearTimeout(forceTermination);
commandSignal?.removeEventListener("abort", settleCanceledChild);
child.removeListener("exit", settleCanceledChild);
diff --git a/sdk/typescript/tests-ts/api.test.ts b/sdk/typescript/tests-ts/api.test.ts
index 5b15b5317..81586cd5e 100644
--- a/sdk/typescript/tests-ts/api.test.ts
+++ b/sdk/typescript/tests-ts/api.test.ts
@@ -2303,18 +2303,35 @@ describe("CodexSecurity orchestration", () => {
},
);
- test.each(
- (["preparation", "empty", "commit", "rollback"] as const).flatMap(
+ test.each([
+ ...(["preparation", "empty", "commit", "rollback"] as const).flatMap(
(boundary) =>
(["real", "mock", "import"] as const).flatMap((mode) =>
(mode === "import"
? (["signal"] as const)
: (["signal", "close"] as const)
- ).map((cancel) => ({ boundary, cancel, mode })),
+ ).map((cancel) => ({
+ registration: "archived registration",
+ boundary,
+ cancel,
+ mode,
+ })),
),
),
- )(
- "cancels archived registration at $boundary via $cancel, mode=$mode",
+ ...(process.platform === "win32"
+ ? (
+ [
+ { boundary: "before fresh guard", cancel: "signal", mode: "real" },
+ { boundary: "fresh guard", cancel: "signal", mode: "real" },
+ { boundary: "empty archive guard", cancel: "close", mode: "real" },
+ ] as const
+ ).map((entry) => ({
+ ...entry,
+ registration: "Windows Deep registration",
+ }))
+ : []),
+ ])(
+ "cancels $registration at $boundary via $cancel, mode=$mode",
async ({ boundary, cancel, mode }) => {
const { root, repository, codexHome, scanDir } = await scanDirectories();
const python = pythonExecutable()!;
@@ -2334,8 +2351,12 @@ describe("CodexSecurity orchestration", () => {
join(environment.CODEX_SECURITY_STATE_DIR, "workbench.sqlite3"),
),
);
+ const guardCreated =
+ boundary === "fresh guard" || boundary === "empty archive guard";
+ const deep = guardCreated || boundary === "before fresh guard";
+ const archiveExisting = !deep || boundary === "empty archive guard";
let previousId: string | undefined;
- if (boundary !== "empty") {
+ if (boundary !== "empty" && !deep) {
const previous = await runWorkbench(workbenchOptions, [
"register-cli-scan",
"--repository",
@@ -2375,36 +2396,52 @@ describe("CodexSecurity orchestration", () => {
const pluginRoot = join(root, "controlled-plugin");
for (const file of [
".codex-plugin/plugin.json",
- "skills/security-scan/SKILL.md",
+ `skills/${deep ? "deep-security-scan" : "security-scan"}/SKILL.md`,
"schemas/findings.schema.json",
]) {
await cp(join(PLUGIN_ROOT, file), join(pluginRoot, file));
}
await mkdir(join(pluginRoot, "scripts"), { recursive: true });
const afterRename = boundary === "commit" || boundary === "rollback";
- const hook = afterRename
- ? "insert_running_scan"
- : "directory_snapshot_regular_file_count";
+ const hook = guardCreated
+ ? "register_windows_resume_output"
+ : afterRename
+ ? "insert_running_scan"
+ : "directory_snapshot_regular_file_count";
const framedArgument = "--fixture-message=出力\0literal\nsecond line";
+ const entrypoint = join(pluginRoot, "scripts", "workbench_db.py");
+ const controlledScript = guardCreated
+ ? join(pluginRoot, "scripts", "registration_fixture.py")
+ : entrypoint;
await writeFile(
- join(pluginRoot, "scripts", "workbench_db.py"),
+ controlledScript,
`import io, runpy, socket, sys
+from contextlib import contextmanager
from pathlib import Path
workbench = runpy.run_path(${JSON.stringify(join(PLUGIN_ROOT, "scripts", "workbench_db.py"))})
namespace = workbench["main"].__globals__
original = namespace["${hook}"]
-def paused(*args, **options):
+def wait_at_boundary():
with socket.create_connection(("127.0.0.1", ${address.port})) as connection:
connection.sendall(b"ready")
if connection.recv(1) == b"p":
connection.sendall(b"alive")
connection.recv(1)
+
+def paused(*args, **options):
+ wait_at_boundary()
if ${JSON.stringify(boundary)} == "rollback":
raise RuntimeError("synthetic registration failure after rename")
return original(*args, **options)
+@contextmanager
+def paused_guard(*args, **options):
+ with original(*args, **options):
+ wait_at_boundary()
+ yield
+
def main(**kwargs):
sys.stdout.reconfigure(newline="\\r\\n")
if sys.argv[1:2] == ["register-cli-scan"] and "--help" not in sys.argv:
@@ -2412,13 +2449,25 @@ def main(**kwargs):
payload = sys.stdin.buffer.read()
Path(${JSON.stringify(join(root, "registration-input.json"))}).write_bytes(payload)
sys.stdin = io.TextIOWrapper(io.BytesIO(payload), encoding="utf-8")
- namespace["${hook}"] = paused
+ namespace["${hook}"] = ${guardCreated ? "paused_guard" : "paused"}
workbench["main"](**kwargs)
if __name__ == "__main__":
main()
`,
);
+ if (guardCreated) {
+ // Select a wrapper that starts the delegated workbench as a new __main__.
+ await writeFile(
+ entrypoint,
+ `import runpy
+def main(**kwargs):
+ runpy.run_path(${JSON.stringify(controlledScript)}, run_name="__main__")
+if __name__ == "__main__":
+ main()
+`,
+ );
+ }
let submitted: string | undefined;
const registration: typeof runWorkbench = async (
options,
@@ -2457,7 +2506,7 @@ if __name__ == "__main__":
const connected = once(server, "connection", { signal: deadline });
const options = {
outputDir: scanDir,
- archiveExisting: true,
+ archiveExisting,
signal: controller.signal,
};
const operation = (
@@ -2477,6 +2526,7 @@ if __name__ == "__main__":
)
: client.run(repository, {
...options,
+ mode: deep ? "deep" : "standard",
mock: mode === "mock",
scanPrompt: "Review café boundaries.\nPreserve the second line.",
})
@@ -2517,9 +2567,9 @@ if __name__ == "__main__":
});
if (cancel === "close") closing = client.close();
else controller.abort();
- if (afterRename) {
- // This child has already renamed the directory. A request/response
- // proves it remains alive after cancellation until we let it settle.
+ if (afterRename || guardCreated) {
+ // The filesystem transaction has started. Prove the child remains
+ // alive after cancellation until we allow its commit or rollback.
const alive = once(socket, "data", { signal: deadline });
socket.write("p");
expect(String((await alive)[0])).toBe("alive");
@@ -2536,8 +2586,11 @@ if __name__ == "__main__":
const scans = (await runWorkbench(workbenchOptions, ["list-scans"]))[
"scans"
] as Array<{ scanId: string; scanDir: string }>;
- expect(scans).toHaveLength(
- boundary === "empty" ? 0 : boundary === "commit" ? 2 : 1,
+ const registrations = scans.filter(
+ (scan) => scan.scanId !== previousId,
+ );
+ expect(registrations).toHaveLength(
+ boundary === "commit" || guardCreated ? 1 : 0,
);
if (previousId !== undefined) {
const previous = scans.find((scan) => scan.scanId === previousId)!;
@@ -2551,6 +2604,33 @@ if __name__ == "__main__":
name.startsWith("scan.previous-"),
),
).toHaveLength(boundary === "commit" ? 1 : 0);
+ if (deep) {
+ expect(await readdir(scanDir)).toEqual(
+ guardCreated ? [".codex-security-resume"] : [],
+ );
+ if (guardCreated)
+ expect(scans[0]).toMatchObject({
+ scanDir,
+ lifecycle: { status: "canceled" },
+ });
+ const retry = await runWorkbench(workbenchOptions, [
+ "register-cli-scan",
+ "--repository",
+ repository,
+ "--scan-dir",
+ scanDir,
+ ...(guardCreated ? ["--archive-existing"] : []),
+ "--recipe-json",
+ JSON.stringify({
+ repository,
+ target: { kind: "repository", paths: [] },
+ mode: "deep",
+ config: {},
+ }),
+ ]);
+ expect(retry["scanId"]).toBeString();
+ expect(retry["scanId"]).not.toBe(scans[0]?.scanId);
+ }
} finally {
cleaningUp = true;
clearTimeout(guardTimer);
diff --git a/sdk/typescript/tests-ts/fixtures/inherited-codex-pipes.mjs b/sdk/typescript/tests-ts/fixtures/inherited-codex-pipes.mjs
index 7453e4197..2686074a0 100644
--- a/sdk/typescript/tests-ts/fixtures/inherited-codex-pipes.mjs
+++ b/sdk/typescript/tests-ts/fixtures/inherited-codex-pipes.mjs
@@ -1,5 +1,11 @@
import { spawn } from "node:child_process";
-import { existsSync, renameSync, writeFileSync } from "node:fs";
+import {
+ existsSync,
+ readSync,
+ renameSync,
+ writeFileSync,
+ writeSync,
+} from "node:fs";
import { dirname } from "node:path";
import { createInterface } from "node:readline";
import { fileURLToPath } from "node:url";
@@ -45,12 +51,10 @@ if (role === "holder") {
await new Promise(() => {});
}
if (mode === "archive-committed") {
- const lines = createInterface({ input: process.stdin });
- let receivedInput = false;
- for await (const _line of lines) {
- if (receivedInput) break;
- receivedInput = true;
- process.stdout.write("codex-security-archive-ready\n");
+ writeSync(3, Buffer.from("?"));
+ const answer = Buffer.alloc(1);
+ if (readSync(3, answer) !== 1 || answer.toString() !== "!") {
+ throw new Error("Registration was not acknowledged");
}
await new Promise((resolve) =>
process.stdout.write("committed\n", resolve),
diff --git a/sdk/typescript/tests-ts/scan-resume.test.ts b/sdk/typescript/tests-ts/scan-resume.test.ts
index a6f6811e2..34ad458b2 100644
--- a/sdk/typescript/tests-ts/scan-resume.test.ts
+++ b/sdk/typescript/tests-ts/scan-resume.test.ts
@@ -62,7 +62,7 @@ async function interruptedScan(
resolvedDeep = false,
modelProvider?: string,
knowledgeState: "saved" | "legacy" = "saved",
- separateSetupCommands = false,
+ setup: "batched" | "separate" | "before-checkpoint" = "batched",
) {
const root = await temporaryDirectory();
const repository = bulk
@@ -227,7 +227,7 @@ async function interruptedScan(
payload: { id: threadId, cwd: scanDir },
}) + "\n",
);
- if (mode === "deep") {
+ if (mode === "deep" && setup !== "before-checkpoint") {
setupCommands.push([
"begin-deep-scan",
"--scan-id",
@@ -258,13 +258,14 @@ async function interruptedScan(
]);
}
}
- if (separateSetupCommands) {
+ if (setup === "separate") {
for (const args of setupCommands) await command(args);
} else {
await runWorkbenchFixtureCommands(python, environment, setupCommands);
}
const checkpoint = join(scanDir, "checkpoint.json");
- await writeFile(checkpoint, '{"completed":"setup"}\n');
+ if (setup !== "before-checkpoint")
+ await writeFile(checkpoint, '{"completed":"setup"}\n');
return {
root,
repository,
@@ -292,7 +293,7 @@ test("resume resolves an interrupted scan without changing its ID, recipe, or co
false,
undefined,
"saved",
- true,
+ "separate",
);
const before = await f.command([
"get-deep-scan",
@@ -323,6 +324,53 @@ test("resume resolves an interrupted scan without changing its ID, recipe, or co
expect(await readFile(f.checkpoint, "utf8")).toBe('{"completed":"setup"}\n');
});
+test.skipIf(process.platform !== "win32")(
+ "CLI resumes the original Windows session before its first output checkpoint",
+ async () => {
+ const f = await interruptedScan(
+ "deep",
+ false,
+ {},
+ false,
+ undefined,
+ "saved",
+ "before-checkpoint",
+ );
+ const grant = Bun.spawnSync([
+ join(process.env["SystemRoot"]!, "System32", "icacls.exe"),
+ f.scanDir,
+ "/grant",
+ "*S-1-5-11:(OI)(CI)M",
+ ]);
+ expect(grant.exitCode, grant.stderr.toString()).toBe(0);
+ const { stderr, runCli } = createCliTest(main);
+ let resumedThread: string | undefined;
+ await runCli(
+ ["scans", "resume", f.scanId, "--json"],
+ resumeDependencies(f, () => ({
+ startThread: () => fail("Unexpected new session"),
+ resumeThread(threadId) {
+ resumedThread = threadId;
+ return {
+ id: threadId,
+ async runStreamed() {
+ throw new Error("Synthetic continuation reached");
+ },
+ };
+ },
+ })),
+ );
+ expect(stderr.text()).toContain("Synthetic continuation reached");
+ expect(resumedThread).toBe(f.threadId);
+ expect(
+ (await f.command(["get-scan", "--scan-id", f.scanId]))["scan"],
+ ).toMatchObject({
+ continuationThreadId: f.threadId,
+ progress: { status: "running" },
+ });
+ },
+);
+
test.each([
"failed",
"canceled",
@@ -1174,6 +1222,7 @@ test.each([
const manifestPath = join(f.scanDir, "scan-manifest.json");
const manifest = JSON.parse(await readFile(manifestPath, "utf8"));
expect(manifest.scan.producer.version).toBe(f.recipe.pluginVersion);
+ expect(JSON.stringify(manifest)).not.toContain(".codex-security-resume");
if (scenario === "unsupported-schema") manifest.schemaVersion = "999.0";
if (scenario === "wrong-producer")
manifest.scan.producer.name = "different-producer";
@@ -1660,14 +1709,27 @@ test("missing session logs do not create another session or fail the original sc
await rm(f.sessionPath);
const { stderr, runCli } = createCliTest(main);
- const code = await runCli(
- ["scans", "resume", f.scanId],
- resumeDependencies(f, () =>
- fail("Must not invoke Codex without the original session"),
- ),
+ const deps = resumeDependencies(f, () =>
+ fail("Must not invoke Codex without the original session"),
);
+ const code = await runCli(["scans", "resume", f.scanId], deps);
expect(code).not.toBe(0);
expect(stderr.text()).toContain("original Codex session");
+ const client = deps.createSecurity({});
+ const ready: string[] = [];
+ try {
+ await expect(
+ client.run(f.repository, {
+ mode: "deep",
+ outputDir: f.scanDir,
+ resumeScanId: f.scanId,
+ onOutputDirReady: (directory) => ready.push(directory),
+ }),
+ ).rejects.toThrow("original Codex session");
+ expect(ready).toEqual([]);
+ } finally {
+ await client.close();
+ }
expect(
(await f.command(["get-scan", "--scan-id", f.scanId]))["scan"],
).toMatchObject({ progress: { status: "running" } });
diff --git a/sdk/typescript/tests-ts/windows-scan-output.test.ts b/sdk/typescript/tests-ts/windows-scan-output.test.ts
index 44560b36d..0c2e04b68 100644
--- a/sdk/typescript/tests-ts/windows-scan-output.test.ts
+++ b/sdk/typescript/tests-ts/windows-scan-output.test.ts
@@ -34,6 +34,37 @@ const example = join(PLUGIN_ROOT, "examples", "completed-scan");
const system = join(process.env["SystemRoot"] ?? "C:\\Windows", "System32");
const powershell = join(system, "WindowsPowerShell", "v1.0");
+windowsTest(
+ "a selected registration script keeps its ordinary entrypoint and stdin",
+ async () => {
+ const pluginRoot = await temporaryDirectory();
+ const scripts = join(pluginRoot, "scripts");
+ await mkdir(scripts);
+ await writeFile(
+ join(scripts, "workbench_db.py"),
+ [
+ "import json, sys",
+ "if __name__ == '__main__':",
+ " print(json.dumps({'args': sys.argv[1:], 'fd': sys.stdin.fileno(), 'input': sys.stdin.buffer.read().decode('utf-8')}))",
+ ].join("\n"),
+ );
+ const args = ["register-cli-scan", "--plugin-option", "出力\0line\ntwo"];
+ const input = "\ncafé\r\nlast";
+ expect(
+ await runtime.runWorkbench(
+ {
+ python: await runtime.resolvePluginPython(),
+ pluginRoot,
+ environment: { SystemRoot: process.env["SystemRoot"] },
+ signal: new AbortController().signal,
+ },
+ args,
+ input,
+ ),
+ ).toEqual({ args, fd: 0, input });
+ },
+);
+
async function icacls(path: string, ...args: string[]): Promise<void> {
await exec(join(system, "icacls.exe"), [path, ...args]);
}