OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
refactor(workbench): share saved coverage logic and checkpoint fixtures (#2185)
Michael D'Angelof615be2e708a
diff --git a/plugins/codex-security/scripts/deep_scan_workbench.py b/plugins/codex-security/scripts/deep_scan_workbench.pyindex e4371b7a4..8ca4dc727 100644--- a/plugins/codex-security/scripts/deep_scan_workbench.py+++ b/plugins/codex-security/scripts/deep_scan_workbench.py@@ -11,7 +11,7 @@ import sysimport uuidfrom datetime import datetime, timedeltafrom pathlib import Path-from typing import Any, Callable+from typing import Anysys.path.insert(0, str(Path(__file__).resolve().parent))import workbench_saved_results as saved_results@@ -22,118 +22,11 @@ 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-DEEP_SCAN_WORKER_KINDS = ("setup", "discovery", "dedup")-DEEP_SCAN_WORKER_STATUSES = ("queued", "running", "succeeded", "failed", "canceled")-DEEP_SCAN_REPLACEABLE_FAILURE_KINDS = (- "policy_refusal",- "transient_error",- "invalid_discovery_artifacts",-)-DEEP_SCAN_TERMINAL_REASONS = ("saturated", "capped")-DEEP_SCAN_WORKFLOW_VERSION = "deep-security-scan/v1"DEEP_SCAN_COORDINATOR_LEASE_SECONDS = 30DEEP_SCAN_LEGACY_COORDINATOR_GRACE_SECONDS = 120DEEP_SCAN_PUBLICATION_ERROR_SEPARATOR = "\nOriginal Deep Scan failure:\n"-def register_subcommands(subparsers: Any, positive_int: Callable[[str], int]) -> None:- begin_deep_scan = subparsers.add_parser("begin-deep-scan")- begin_deep_scan.add_argument("--thread-id", required=True)- begin_target = begin_deep_scan.add_mutually_exclusive_group(required=True)- begin_target.add_argument("--scan-id")- begin_target.add_argument("--target-path")- begin_deep_scan.add_argument("--scope", default=".")- begin_user_context = begin_deep_scan.add_mutually_exclusive_group()- begin_user_context.add_argument("--user-context")- begin_user_context.add_argument("--user-context-stdin", action="store_true")- begin_deep_scan.add_argument("--scan-root")- begin_deep_scan.add_argument("--claim-token")- begin_deep_scan.add_argument("--model")- begin_deep_scan.add_argument("--reasoning-effort")- begin_deep_scan.add_argument("--available-parallelism", type=positive_int)- begin_deep_scan.add_argument("--deep-scan-project-json")- begin_deep_scan.add_argument("--project-config-path")- begin_deep_scan.add_argument("--join-only", action="store_true")- begin_deep_scan.add_argument("--workflow-version", default=DEEP_SCAN_WORKFLOW_VERSION)-- get_deep_scan = subparsers.add_parser("get-deep-scan")- get_deep_scan.add_argument("--scan-id", required=True)- get_deep_scan.add_argument("--thread-id", required=True)-- acknowledge_result = subparsers.add_parser("acknowledge-deep-scan-result")- acknowledge_result.add_argument("--scan-id", required=True)-- claim_coordinator = subparsers.add_parser("claim-deep-scan-coordinator")- claim_coordinator.add_argument("--scan-id", required=True)- claim_coordinator.add_argument("--thread-id", required=True)- claim_coordinator.add_argument("--claim-token")- claim_coordinator.add_argument("--coordinator-generation", type=positive_int)-- upsert_deep_worker = subparsers.add_parser("upsert-deep-scan-worker")- upsert_deep_worker.add_argument("--scan-id", required=True)- upsert_deep_worker.add_argument("--worker-id", required=True)- upsert_deep_worker.add_argument("--kind", choices=DEEP_SCAN_WORKER_KINDS, required=True)- upsert_deep_worker.add_argument("--status", choices=DEEP_SCAN_WORKER_STATUSES, required=True)- upsert_deep_worker.add_argument("--prompt-path", required=True)- upsert_deep_worker.add_argument("--artifact-dir", required=True)- upsert_deep_worker.add_argument("--result-manifest-path")- upsert_deep_worker.add_argument("--attempt", type=non_negative_int)- upsert_deep_worker.add_argument("--sdk-thread-id")- upsert_deep_worker.add_argument("--error-message")- upsert_deep_worker.add_argument(- "--replaceable-failure-kind", choices=DEEP_SCAN_REPLACEABLE_FAILURE_KINDS- )- upsert_deep_worker.add_argument("--coordinator-generation", type=positive_int)-- claim_deep_dedup = subparsers.add_parser("claim-deep-scan-dedup")- claim_deep_dedup.add_argument("--scan-id", required=True)- claim_deep_dedup.add_argument("--worker-id", required=True)- claim_deep_dedup.add_argument("--prompt-path", required=True)- claim_deep_dedup.add_argument("--artifact-dir", required=True)- claim_deep_dedup.add_argument("--input-worker-id", action="append", required=True)- claim_deep_dedup.add_argument("--coordinator-generation", type=positive_int)-- commit_deep_dedup = subparsers.add_parser("commit-deep-scan-dedup")- commit_deep_dedup.add_argument("--scan-id", required=True)- commit_deep_dedup.add_argument("--worker-id", required=True)- commit_deep_dedup.add_argument("--result-manifest-path", required=True)- commit_deep_dedup.add_argument("--candidate-ledger-path")- commit_deep_dedup.add_argument("--new-findings-count", type=non_negative_int, required=True)- commit_deep_dedup.add_argument("--coordinator-generation", type=positive_int)-- finish_deep_scan = subparsers.add_parser("finish-deep-scan")- finish_deep_scan.add_argument("--scan-id", required=True)- finish_deep_scan.add_argument(- "--terminal-reason", choices=DEEP_SCAN_TERMINAL_REASONS, required=True- )- finish_deep_scan.add_argument("--manifest-path", required=True)- finish_deep_scan.add_argument("--staged-manifest-path")- finish_deep_scan.add_argument("--omitted-worker-id", action="append", default=[])- finish_deep_scan.add_argument("--coordinator-generation", type=positive_int)-- fail_deep_scan = subparsers.add_parser("fail-deep-scan")- fail_deep_scan.add_argument("--scan-id", required=True)- fail_deep_scan.add_argument("--message", required=True)- fail_deep_scan.add_argument("--manifest-path")- fail_deep_scan.add_argument("--staged-manifest-path")- fail_deep_scan.add_argument(- "--deep-status", choices=("failed", "interrupted"), default="failed"- )- fail_deep_scan.add_argument("--coordinator-generation", type=positive_int)-- publication_failure = subparsers.add_parser("record-deep-scan-publication-failure")- publication_failure.add_argument("--scan-id", required=True)- publication_failure.add_argument("--message", required=True)- publication_failure.add_argument("--coordinator-generation", type=positive_int)---def non_negative_int(value: str) -> int:- parsed = int(value)- if parsed < 0:- raise argparse.ArgumentTypeError("expected a non-negative integer")- return parsed--_dependencies: Any = Nonediff --git a/plugins/codex-security/scripts/workbench/retained_coverage.py b/plugins/codex-security/scripts/workbench/retained_coverage.pyindex 5f872095d..ee5b224e9 100644--- a/plugins/codex-security/scripts/workbench/retained_coverage.py+++ b/plugins/codex-security/scripts/workbench/retained_coverage.py@@ -47,6 +47,7 @@ from workbench.retained_records import (same_raw_value,)from workbench.saved_coverage import (+ SavedCoverageProjection,_coverage_rows,_has_candidate_metadata,_inherit_coverage_sources,@@ -730,14 +731,6 @@ def merge_saved_results(parent_model_source = latest_reducerprojected_coverage = saved_parent["reviewState"] if saved_parent else {}- reviews = projected_coverage.get("reviews")- reviewed_attempts = {- (review.get("workerId"), review.get("attempt"))- for review in (reviews if isinstance(reviews, list) else [])- if isinstance(review, dict)- and isinstance(review.get("workerId"), str)- and isinstance(review.get("attempt"), int)- }# Reconcile raw source IDs and timestamps before projecting accepted outputs.accepted_sources = {relative@@ -747,12 +740,7 @@ def merge_saved_results(and worker["result_manifest_path"] == str(scan_dir / relative)and draft.get("complete") is not False}- reviewed_attempts.update(- (worker_id, workers_by_id[worker_id]["attempt"])- for relative, _, worker_id in sources- if relative in accepted_sources- )- projection = prepare_saved_coverage_projection(+ project_worker_coverage, legacy_surface_links = prepare_saved_coverage_projection(sources,parent,source_order,@@ -762,7 +750,6 @@ def merge_saved_results(scan_dir, row, workers_by_id[owner], relative),)- project_worker_coverage = projection.project_worker_coveragedef retain_coverage_row(output, item, owner, field, original=None, qualified=False):retained = projection.retain_coverage_row(output, item, owner, field, original, qualified)@@ -800,15 +787,14 @@ def merge_saved_results(id(item): (worker_id, relative, field, index, draft["reviewState"])for relative, draft, worker_id in sourcesfor field in ("surfaces", "explicitExclusions", "deferred", "openQuestions")- for index, item in enumerate(- draft["reviewState"].get(field, [])- if isinstance(draft["reviewState"].get(field, []), list)- else [],- 1,- )+ for index, item in enumerate(_coverage_rows(draft["reviewState"], field), 1)if isinstance(item, dict)}+ projection = SavedCoverageProjection(+ project_worker_coverage, legacy_surface_links, projection_origins+ )+def project_record(field: str,item: dict[str, Any],@@ -861,7 +847,6 @@ def merge_saved_results(return resultall_sources = ([("parent", parent, None)] if parent else []) + sources- source_owners = {relative: owner for relative, _, owner in all_sources}inferred_coverage_ids: set[int] = set()# Older checkpoints can omit IDs already assigned in their published output.for field in ("deferred", "surfaces"):@@ -2675,7 +2660,7 @@ def merge_saved_results(projection = type(initial_projection)(initial_projection.project_worker_coverage,initial_projection.legacy_surface_links,- sources,+ projection_origins,)# Apply reconciled outcomes to coverage already copied from the parent too.for field in ("surfaces", "deferred"):@@ -2964,7 +2949,6 @@ def merge_saved_results(if coverage_superseded and not selected_candidates and not retain_pending:continueif project_coverage:- reviewed_attempts.add((worker_id, worker["attempt"]))reviews = coverage.setdefault("reviews", [])if isinstance(reviews, list) and not any(isinstance(review, dict)diff --git a/plugins/codex-security/scripts/workbench/saved_coverage.py b/plugins/codex-security/scripts/workbench/saved_coverage.pyindex 2a6319aa7..7b37c19a2 100644--- a/plugins/codex-security/scripts/workbench/saved_coverage.py+++ b/plugins/codex-security/scripts/workbench/saved_coverage.py@@ -5,6 +5,7 @@ from __future__ import annotationsimport copyimport hashlibimport json+from collections.abc import Iteratorfrom pathlib import Pathfrom typing import Any@@ -95,16 +96,18 @@ def _coverage_comparison_row(row: Any, field: str) -> Any:return row-def _aggregate_worker_coverage_rows(rows: list[Any], worker_id: str, field: str) -> list[Any]:+def _worker_coverage_sources(+ rows: list[Any], worker_id: str, field: str+) -> Iterator[tuple[Any, Any]]:"""Match accepted aggregate IDs without changing raw recovery identities."""reserved = {row["id"] for row in rows if isinstance(row, dict) and isinstance(row.get("id"), str)}emitted: set[str] = set()- projected = []for index, original in enumerate(rows):- row = copy.deepcopy(original)+ row = originalif isinstance(row, dict) and field != "openQuestions":+ row = row.copy()generated = not isinstance(row.get("id"), str)identity = str(index) if generated else row["id"]base = identity@@ -116,23 +119,28 @@ def _aggregate_worker_coverage_rows(rows: list[Any], worker_id: str, field: str)reserved.add(identity)emitted.add(identity)row["id"] = identity- projected.append(- _coverage_comparison_row(_project_worker_coverage_row(row, worker_id, field), field)- )- return projected+ qualified = _project_worker_coverage_row(row, worker_id, field)+ yield _coverage_comparison_row(qualified, field), original++_Row = dict[str, Any]+_LegacySurfaceLink = tuple[_Row, str, _Row, _Row, _Row | None]-def _prepare_worker_coverage_recovery(++def prepare_saved_coverage_projection(sources: list[tuple[str, dict[str, Any], str | None]],parent: dict[str, Any] | None,source_order: dict[str, tuple[int, int]],*,- enabled: bool,- receipt_resolver=None,-) -> tuple[- bool, list[tuple[dict[str, Any], str, dict[str, Any], dict[str, Any], dict[str, Any] | None]]-]:+ project_worker_coverage: bool,+ receipt_resolver,+ preserve_canonical_parent=False,+) -> tuple[bool, list[_LegacySurfaceLink]]:"""Put bound worker evidence in parent coordinates without changing source files."""+ if preserve_canonical_parent:+ return False, []+ # Older parent publications use worker/id identities. Reconcile those copies+ # against their bound sources before projecting accepted attempt provenance.workers = {owner for _, _, owner in sources if owner is not None}parents = [draft@@ -145,8 +153,8 @@ def _prepare_worker_coverage_recovery(# Prefer the selected parent over historical copies of a task's surface.parents.insert(0, parent)fields = ("surfaces", "explicitExclusions", "deferred", "openQuestions")- if not enabled:- enabled = any(+ if not project_worker_coverage:+ project_worker_coverage = any(isinstance(row, dict)and isinstance(row.get("workerId"), str)and row["workerId"] in workers@@ -156,135 +164,105 @@ def _prepare_worker_coverage_recovery()for draft in parentsfor field in fields- for row in (- draft["reviewState"].get(field, [])- if isinstance(draft["reviewState"].get(field, []), list)- else []- )+ for row in _coverage_rows(draft["reviewState"], field))- if not enabled:+ if not project_worker_coverage:return False, []- projected: dict[str, list[Any]] = {field: [] for field in fields}- surface_sources: list[tuple[dict[str, Any], dict[str, Any]]] = []- task_sources: list[tuple[dict[str, Any], dict[str, Any]]] = []- surface_links: list[- tuple[dict[str, Any], str, dict[str, Any], dict[str, Any], dict[str, Any] | None]- ] = []+ missing = object()+ original_owners = {}+ projected: dict[str, list[tuple[Any, Any]]] = {field: [] for field in fields}+ surface_links: list[_LegacySurfaceLink] = []for relative, draft, owner in sources:if owner is None:continue- directory = Path(relative).parent- if directory.name == "checkpoints":- directory = directory.parentfor field in fields:- rows = draft["reviewState"].get(field, [])- if not isinstance(rows, list):- continue+ rows = _coverage_rows(draft["reviewState"], field)for index, row in enumerate(rows):if field == "openQuestions" and isinstance(row, str):row = {"question": row}rows[index] = rowif not isinstance(row, dict):continue+ original_owners.setdefault(id(row), row.get("workerId", missing))row["workerId"] = owner- if field == "surfaces" and receipt_resolver is not None:- row["receiptRefs"] = receipt_resolver(relative, row, owner)- elif field == "surfaces" and isinstance(row.get("receiptRefs"), list):- refs = []- for ref in row["receiptRefs"]:- if isinstance(ref, str):- try:- normalized = _require_portable_relative_path(- ref, "Worker coverage receipt"- )- except ContractError:- pass # Leave malformed refs to the existing per-record recovery.- else:- if normalized.startswith("artifacts/"):- ref = f"{directory.as_posix()}/{normalized}"- refs.append(ref)- row["receiptRefs"] = refsif field == "surfaces":- row.setdefault("receiptRefs", [])- aggregate_rows = _aggregate_worker_coverage_rows(rows, owner, field)- projected[field].extend(aggregate_rows)- if field in {"surfaces", "deferred"}:- originals = surface_sources if field == "surfaces" else task_sources- originals.extend(- (aggregate, row)- for aggregate, row in zip(aggregate_rows, rows)- if isinstance(aggregate, dict) and isinstance(row, dict)- )+ row["receiptRefs"] = receipt_resolver(relative, row, owner)+ projected[field].extend(_worker_coverage_sources(rows, owner, field))++ def originals(field: str, row: Any):+ comparison = _coverage_comparison_row(row, field)+ return (+ original+ for aggregate, original in projected[field]+ if aggregate is comparison or aggregate == comparison+ )+# A canonical aggregate contains copies of the same worker evidence. Let# the existing per-worker chronology reconcile those originals exactly once.# Keep parent-only evidence when its bound worker source is unavailable.for draft in parents:- if not draft["reviewState"]:+ review = draft["reviewState"]+ if not review:continuefor field in fields:- rows = draft["reviewState"].get(field, [])- if isinstance(rows, list):- if field == "surfaces":- tasks = draft["reviewState"].get("deferred", [])- for row in rows:- if not isinstance(row, dict) or not isinstance(row.get("id"), str):- continue- matching_sources = [- original- for aggregate, original in surface_sources- if aggregate == _coverage_comparison_row(row, field)- ]- if not matching_sources:- continue- for task in tasks if isinstance(tasks, list) else []:- if (- isinstance(task, dict)- and isinstance(task.get("surfaceIds"), list)- and row["id"] in task["surfaceIds"]- ):- surface_links.extend(- (- task,- row["id"],- source,- row,- next(- (- original- for aggregate, original in task_sources- if aggregate == task- ),- None,- ),- )- for source in matching_sources- )- draft["reviewState"][field] = [- row- for row in rows- if _coverage_comparison_row(row, field) not in projected[field]- ]+ rows = review.get(field, [])+ if not isinstance(rows, list):+ continue+ remaining = []+ for row in rows:+ matches = originals(field, row)+ first = next(matches, missing)+ if first is missing:+ remaining.append(row)+ continue+ if (+ field != "surfaces"+ or not isinstance(row, dict)+ or not isinstance(row.get("id"), str)+ ):+ continue+ surface_sources = [first, *matches]+ for task in _coverage_rows(review, "deferred"):+ if (+ isinstance(task, dict)+ and isinstance(task.get("surfaceIds"), list)+ and row["id"] in task["surfaceIds"]+ ):+ source_task = next(originals("deferred", task), None)+ surface_links.extend(+ (task, row["id"], source, row, source_task)+ for source in surface_sources+ )+ review[field] = remaining+ # A selected parent may also be the result registered for a worker.+ # Restore the retained input rows after comparing the published worker IDs.+ for _, draft, owner in sources:+ if owner is not None:+ for field in fields:+ for row in _coverage_rows(draft["reviewState"], field):+ if isinstance(row, dict):+ previous = original_owners[id(row)]+ if previous is missing:+ row.pop("workerId", None)+ else:+ row["workerId"] = previousreturn True, surface_linksclass SavedCoverageProjection:"""Keep worker source links stable while legacy and accepted rows converge."""- def __init__(self, project_worker_coverage, legacy_surface_links, sources):+ def __init__(self, project_worker_coverage, legacy_surface_links, projection_origins):self.project_worker_coverage = project_worker_coverageself.legacy_surface_links = legacy_surface_linksself.legacy_source_outputs: dict[int, dict[str, Any]] = {}self.worker_row_owners: dict[int, str] = {}self.worker_surface_outputs: dict[tuple[str, str], dict[int, dict[str, Any]]] = {}- self.source_owner_by_id = {- id(row): owner- for _, draft, owner in sources- for field in ("surfaces", "deferred")- for rows in (draft["reviewState"].get(field, []),)- if isinstance(rows, list)- for row in rows- if isinstance(row, dict)- }+ self.projection_origins = projection_origins++ def source_owner(self, row, fallback=None):+ origin = self.projection_origins.get(id(row))+ return origin[0] if origin is not None else fallbackdef record_coverage_owner(self, retained, original, owner, field):if self.project_worker_coverage and owner is not None and isinstance(retained, dict):@@ -335,8 +313,8 @@ class SavedCoverageProjection:return Nonedef linked_surface(self, surface, outputs, named_outputs):+ owner = self.source_owner(surface, surface.get("workerId"))retained = outputs.get(id(surface))- owner = self.source_owner_by_id.get(id(surface), surface.get("workerId"))if retained is None and isinstance(owner, str) and isinstance(surface.get("id"), str):matches = named_outputs.get((owner, surface["id"]), {})if len(matches) == 1:@@ -395,32 +373,29 @@ class SavedCoverageProjection:coverage["surfaces"], retained, worker["id"], "surfaces", source, True)- def linked_tasks(self, task, published_id, source_task, candidates):- expected = None- if source_task is not None:- expected = self.legacy_source_outputs.get(id(source_task))- if expected is None:- expected = _project_worker_coverage_row(- source_task, self.source_owner_by_id.get(id(source_task)), "deferred"- )- return [- retained- for owner, retained in candidates- if retained == task- or (- expected is not None- and (- retained is expected- or (- owner == self.source_owner_by_id.get(id(source_task))- and isinstance(retained, dict)- and retained.get("id") == expected.get("id")- and isinstance(retained.get("surfaceIds"), list)- and published_id in retained["surfaceIds"]+ def task_surface_links(self, candidates):+ for task, published_id, surface, parent_surface, task_source in self.legacy_surface_links:+ expected = None+ if task_source is not None:+ task_owner = self.source_owner(task_source)+ expected = self.legacy_source_outputs.get(id(task_source))+ if expected is None:+ expected = _project_worker_coverage_row(task_source, task_owner, "deferred")+ for owner, retained in candidates:+ if retained == task or (+ expected is not None+ and (+ retained is expected+ or (+ owner == task_owner+ and isinstance(retained, dict)+ and retained.get("id") == expected.get("id")+ and isinstance(retained.get("surfaceIds"), list)+ and published_id in retained["surfaceIds"]+ ))- )- )- ]+ ):+ yield retained, published_id, surface, parent_surfacedef finalize(self,@@ -476,40 +451,31 @@ class SavedCoverageProjection:(self.worker_row_owners.get(id(row)), row)for row in (output_tasks if isinstance(output_tasks, list) else [])]- for task, published_id, surface, _, task_source in self.legacy_surface_links:+ for task, published_id, surface, _ in self.task_surface_links(task_candidates):retained_surface = self.linked_surface(- surface, self.legacy_source_outputs, self.worker_surface_outputs+ surface,+ self.legacy_source_outputs,+ self.worker_surface_outputs,)- if retained_surface is None or not isinstance(retained_surface.get("id"), str):- continue- for retained_task in self.linked_tasks(- task, published_id, task_source, task_candidates- ):- _, links = retained_legacy_surface_links.setdefault(- id(retained_task), (retained_task, {})- )+ if retained_surface is not None and isinstance(retained_surface.get("id"), str):+ _, links = retained_legacy_surface_links.setdefault(id(task), (task, {}))links.setdefault(published_id, retained_surface)# A newer worker checkpoint may omit a surface still needed by a retained# task. Restore its parent copy only when no worker replacement survived.- for task, published_id, surface, parent_surface, task_source in self.legacy_surface_links:- for retained_task in self.linked_tasks(- task, published_id, task_source, task_candidates- ):- _, links = retained_legacy_surface_links.setdefault(- id(retained_task), (retained_task, {})+ for task, published_id, surface, parent_surface in self.task_surface_links(task_candidates):+ _, links = retained_legacy_surface_links.setdefault(id(task), (task, {}))+ if published_id not in links and isinstance(coverage.get("surfaces"), list):+ retained_surface = self.retain_coverage_row(+ coverage["surfaces"], parent_surface, None, "surfaces")- if published_id not in links and isinstance(coverage.get("surfaces"), list):- retained_surface = self.retain_coverage_row(- coverage["surfaces"], parent_surface, None, "surfaces"- )- self.record_coverage_owner(- retained_surface,- surface,- self.source_owner_by_id.get(id(surface), surface.get("workerId")),- "surfaces",- )- _inherit_coverage_sources(retained_surface, [parent_surface], coverage_sources)- links[published_id] = retained_surface+ self.record_coverage_owner(+ retained_surface,+ surface,+ self.source_owner(surface, surface.get("workerId")),+ "surfaces",+ )+ _inherit_coverage_sources(retained_surface, [parent_surface], coverage_sources)+ links[published_id] = retained_surfaceif self.project_worker_coverage:worker_surfaces: dict[tuple[str, str], dict[str, Any]] = {}named_surface_outputs = {@@ -711,73 +677,19 @@ class SavedCoverageProjection:prospective_surfaces[id(row)] = projectedbound = {(id(retained), published_id)- for task, published_id, surface, _, task_source in self.legacy_surface_links+ for retained, published_id, surface, _ in self.task_surface_links(prospective_tasks)if self.linked_surface(surface, prospective_surfaces, prospective_named) is not None- for retained in self.linked_tasks(- task, published_id, task_source, prospective_tasks- )}- for task, published_id, _, parent_surface, task_source in self.legacy_surface_links:- for retained in self.linked_tasks(- task, published_id, task_source, prospective_tasks- ):- key = (id(retained), published_id)- if key not in bound:- fallback_surfaces.append(parent_surface)- bound.add(key)+ for retained, published_id, _, parent_surface in self.task_surface_links(+ prospective_tasks+ ):+ key = (id(retained), published_id)+ if key not in bound:+ fallback_surfaces.append(parent_surface)+ bound.add(key)return fallback_surfaces-def prepare_saved_coverage_projection(- sources,- parent,- source_order,- *,- project_worker_coverage,- receipt_resolver,- preserve_canonical_parent=False,-):- if preserve_canonical_parent:- return SavedCoverageProjection(False, [], sources)- # Older parent publications use worker/id identities. Reconcile those copies- # against their bound sources before projecting accepted attempt provenance.- projected_metadata = {- id(row): ("workerId" in row, row.get("workerId"))- for relative, draft, _ in sources- for field in ("surfaces", "explicitExclusions", "deferred", "openQuestions")- for rows in (draft["reviewState"].get(field, []),)- if isinstance(rows, list)- for row in rows- if isinstance(row, dict)- }- project_worker_coverage, legacy_surface_links = _prepare_worker_coverage_recovery(- sources,- parent,- source_order,- enabled=project_worker_coverage,- receipt_resolver=receipt_resolver,- )- for _, draft, _ in sources:- for field in ("surfaces", "explicitExclusions", "deferred", "openQuestions"):- rows = draft["reviewState"].get(field, [])- if not isinstance(rows, list):- continue- for row in rows:- if not isinstance(row, dict):- continue- present, value = projected_metadata.get(id(row), (False, None))- if present:- row["workerId"] = value- else:- row.pop("workerId", None)- return SavedCoverageProjection(project_worker_coverage, legacy_surface_links, sources)---def _deferred_rows(coverage: dict[str, Any]) -> list[Any]:- rows = coverage.get("deferred", [])- return rows if isinstance(rows, list) else []--def _resolved_deferred_rows(draft: dict[str, Any], schema: dict[str, Any]) -> list[dict[str, Any]]:if draft.get("complete") is False:return []@@ -786,7 +698,7 @@ def _resolved_deferred_rows(draft: dict[str, Any], schema: dict[str, Any]) -> litry:# Invalid closure metadata cannot discard the evidence it names._validate_schema_node(rows, schema, "coverage.resolvedDeferred")- _validate_resolved_deferred({**coverage, "deferred": _deferred_rows(coverage)})+ _validate_resolved_deferred({**coverage, "deferred": _coverage_rows(coverage, "deferred")})except ContractError:return []return rows@@ -855,7 +767,7 @@ def _merge_tied_parent_observations(output.append(copy.deepcopy(row))pending_ids = {identity- for row in _deferred_rows(coverage)+ for row in _coverage_rows(coverage, "deferred")if isinstance(row, dict)for identity in (row.get("id"), row.get("candidateId"))if isinstance(identity, str)diff --git a/plugins/codex-security/scripts/workbench_cli.py b/plugins/codex-security/scripts/workbench_cli.pyindex 8660419f9..df09335e1 100644--- a/plugins/codex-security/scripts/workbench_cli.py+++ b/plugins/codex-security/scripts/workbench_cli.py@@ -23,6 +23,13 @@ from workbench_constants import ()+def non_negative_int(value: str) -> int:+ parsed = int(value)+ if parsed < 0:+ raise argparse.ArgumentTypeError("expected a non-negative integer")+ return parsed++def add_user_context(parser: argparse.ArgumentParser, *, required: bool = False) -> None:context = parser.add_mutually_exclusive_group(required=required)context.add_argument("--user-context")@@ -87,8 +94,6 @@ def parse_args(description: str, *, inspection_only: bool = False) -> argparse.Nif not unknown:return args- import deep_scan_workbench as deep_scan-start_headless_standard_scan = add_command("start-headless-standard-scan", "--thread-id", "--target-path", "--scope")@@ -106,7 +111,83 @@ def parse_args(description: str, *, inspection_only: bool = False) -> argparse.Ndiff_content_digest=None,)- deep_scan.register_subcommands(subparsers, positive_int)+ begin_deep_scan = add_command("begin-deep-scan", "--thread-id")+ begin_target = begin_deep_scan.add_mutually_exclusive_group(required=True)+ begin_target.add_argument("--scan-id")+ begin_target.add_argument("--target-path")+ begin_deep_scan.add_argument("--scope", default=".")+ add_user_context(begin_deep_scan)+ begin_deep_scan.add_argument("--scan-root")+ begin_deep_scan.add_argument("--claim-token")+ begin_deep_scan.add_argument("--model")+ begin_deep_scan.add_argument("--reasoning-effort")+ begin_deep_scan.add_argument("--available-parallelism", type=positive_int)+ begin_deep_scan.add_argument("--deep-scan-project-json")+ begin_deep_scan.add_argument("--project-config-path")+ begin_deep_scan.add_argument("--join-only", action="store_true")+ begin_deep_scan.add_argument("--workflow-version", default="deep-security-scan/v1")++ add_command("get-deep-scan", "--scan-id", "--thread-id")++ add_command("acknowledge-deep-scan-result", "--scan-id")++ claim_coordinator = add_command("claim-deep-scan-coordinator", "--scan-id", "--thread-id")+ claim_coordinator.add_argument("--claim-token")+ claim_coordinator.add_argument("--coordinator-generation", type=positive_int)++ upsert_deep_worker = add_command("upsert-deep-scan-worker", "--scan-id", "--worker-id")+ upsert_deep_worker.add_argument(+ "--kind", choices=("setup", "discovery", "dedup"), required=True+ )+ upsert_deep_worker.add_argument(+ "--status", choices=("queued", "running", "succeeded", "failed", "canceled"), required=True+ )+ upsert_deep_worker.add_argument("--prompt-path", required=True)+ upsert_deep_worker.add_argument("--artifact-dir", required=True)+ upsert_deep_worker.add_argument("--result-manifest-path")+ upsert_deep_worker.add_argument("--attempt", type=non_negative_int)+ upsert_deep_worker.add_argument("--sdk-thread-id")+ upsert_deep_worker.add_argument("--error-message")+ upsert_deep_worker.add_argument(+ "--replaceable-failure-kind",+ choices=("policy_refusal", "transient_error", "invalid_discovery_artifacts"),+ )+ upsert_deep_worker.add_argument("--coordinator-generation", type=positive_int)++ claim_deep_dedup = add_command(+ "claim-deep-scan-dedup", "--scan-id", "--worker-id", "--prompt-path", "--artifact-dir"+ )+ claim_deep_dedup.add_argument("--input-worker-id", action="append", required=True)+ claim_deep_dedup.add_argument("--coordinator-generation", type=positive_int)++ commit_deep_dedup = add_command(+ "commit-deep-scan-dedup", "--scan-id", "--worker-id", "--result-manifest-path"+ )+ commit_deep_dedup.add_argument("--candidate-ledger-path")+ commit_deep_dedup.add_argument("--new-findings-count", type=non_negative_int, required=True)+ commit_deep_dedup.add_argument("--coordinator-generation", type=positive_int)++ finish_deep_scan = add_command("finish-deep-scan", "--scan-id")+ finish_deep_scan.add_argument(+ "--terminal-reason", choices=("saturated", "capped"), required=True+ )+ finish_deep_scan.add_argument("--manifest-path", required=True)+ finish_deep_scan.add_argument("--staged-manifest-path")+ finish_deep_scan.add_argument("--omitted-worker-id", action="append", default=[])+ finish_deep_scan.add_argument("--coordinator-generation", type=positive_int)++ fail_deep_scan = add_command("fail-deep-scan", "--scan-id", "--message")+ fail_deep_scan.add_argument("--manifest-path")+ fail_deep_scan.add_argument("--staged-manifest-path")+ fail_deep_scan.add_argument(+ "--deep-status", choices=("failed", "interrupted"), default="failed"+ )+ fail_deep_scan.add_argument("--coordinator-generation", type=positive_int)++ publication_failure = add_command(+ "record-deep-scan-publication-failure", "--scan-id", "--message"+ )+ publication_failure.add_argument("--coordinator-generation", type=positive_int)get_scan = add_command("get-scan", "--scan-id")get_scan.add_argument("--occurrence-id")@@ -130,7 +211,7 @@ def parse_args(description: str, *, inspection_only: bool = False) -> argparse.Nlist_scans.add_argument("--mode", choices=MODES)list_scans.add_argument("--repository")list_scans.add_argument("--scan-root")- list_scans.add_argument("--offset", type=deep_scan.non_negative_int, default=0)+ list_scans.add_argument("--offset", type=non_negative_int, default=0)list_scans.add_argument("--limit", type=positive_int)list_unmatched_scan_pairs = add_command("list-unmatched-scan-pairs", "--repository")@@ -178,30 +259,30 @@ def parse_args(description: str, *, inspection_only: bool = False) -> argparse.Nlist_global_findings.add_argument("--severity", choices=FINDING_SEVERITIES)list_global_findings.add_argument("--status", choices=FINDING_STATUSES)list_global_findings.add_argument("--target-id")- list_global_findings.add_argument("--offset", type=deep_scan.non_negative_int, default=0)+ list_global_findings.add_argument("--offset", type=non_negative_int, default=0)list_global_findings.add_argument("--limit", type=positive_int, default=FINDINGS_PAGE_MAX)list_repositories = subparsers.add_parser("list-repositories")list_repositories.add_argument("--query")list_repositories.add_argument("--target-id")list_repositories.add_argument("--status", choices=("scanned", "not_scanned", "open_findings"))- list_repositories.add_argument("--offset", type=deep_scan.non_negative_int, default=0)+ list_repositories.add_argument("--offset", type=non_negative_int, default=0)list_repositories.add_argument("--limit", type=positive_int)list_findings = add_command("list-findings", "--scan-id")list_findings.add_argument("--query")list_findings.add_argument("--severity", choices=FINDING_SEVERITIES)list_findings.add_argument("--status", choices=FINDING_STATUSES)- list_findings.add_argument("--offset", type=deep_scan.non_negative_int, default=0)+ list_findings.add_argument("--offset", type=non_negative_int, default=0)list_findings.add_argument("--limit", type=positive_int, default=FINDINGS_PAGE_MAX)update_progress = add_command("update-progress", "--scan-id")update_progress.add_argument("--phase", choices=PHASES)- update_progress.add_argument("--phase-items-total", type=deep_scan.non_negative_int)- update_progress.add_argument("--phase-items-completed", type=deep_scan.non_negative_int)+ update_progress.add_argument("--phase-items-total", type=non_negative_int)+ update_progress.add_argument("--phase-items-completed", type=non_negative_int)update_progress.add_argument("--phase-progress-unit", choices=PHASE_PROGRESS_UNITS)- update_progress.add_argument("--review-items-total", type=deep_scan.non_negative_int)- update_progress.add_argument("--review-items-completed", type=deep_scan.non_negative_int)- update_progress.add_argument("--reportable-findings-count", type=deep_scan.non_negative_int)+ update_progress.add_argument("--review-items-total", type=non_negative_int)+ update_progress.add_argument("--review-items-completed", type=non_negative_int)+ update_progress.add_argument("--reportable-findings-count", type=non_negative_int)update_progress.add_argument("--deep-review-pass", type=positive_int)update_progress.add_argument("--claim-token")update_progress.add_argument("--coordinator-generation", type=positive_int)diff --git a/plugins/codex-security/scripts/workbench_native_indexes.py b/plugins/codex-security/scripts/workbench_native_indexes.pyindex 8f333c6f9..623cef23e 100644--- a/plugins/codex-security/scripts/workbench_native_indexes.py+++ b/plugins/codex-security/scripts/workbench_native_indexes.py@@ -4,7 +4,6 @@ import argparseimport osimport sqlite3import sys-from collections import Counterfrom collections.abc import Iteratorfrom itertools import islicefrom pathlib import Path@@ -204,8 +203,7 @@ def list_repositories(connection: sqlite3.Connection,args: argparse.Namespace | None = None,) -> dict[str, Any]:- scan_count_by_target = Counter()- latest_by_target: dict[str, tuple[sqlite3.Row, dict[str, Any] | None]] = {}+ by_target: dict[str, dict[str, Any]] = {}for row in connection.execute(f"""SELECT targets.id AS repository_id, targets.display_name, targets.current_path,@@ -219,26 +217,24 @@ def list_repositories().fetchall():scan = scan_history.scan_summary(row) if row["id"] is not None else Nonetarget_id = row["repository_id"]+ if target_id not in by_target:+ by_target[target_id] = {+ "displayName": row["display_name"],+ "latestScan": scan,+ "openFindingsCount": 0,+ "scanCount": 0,+ "targetId": target_id,+ "targetPath": row["current_path"],+ }if scan is not None:- scan_count_by_target[target_id] += 1- latest_by_target.setdefault(target_id, (row, scan))+ by_target[target_id]["scanCount"] += 1- open_findings_by_target = Counter(- row["target_id"] for row in _indexed_findings(connection) if row["status"] == "open"- )- repositories = [- {- "displayName": target["display_name"],- "latestScan": latest_scan,- "openFindingsCount": open_findings_by_target[target_id]- if latest_scan is not None- else 0,- "scanCount": scan_count_by_target[target_id],- "targetId": target_id,- "targetPath": target["current_path"],- }- for target_id, (target, latest_scan) in latest_by_target.items()- ]+ # A writer can publish a target or its first scan after the repository query.+ for finding in _indexed_findings(connection):+ repository = by_target.get(finding["target_id"])+ if finding["status"] == "open" and repository is not None and repository["scanCount"]:+ repository["openFindingsCount"] += 1+ repositories = list(by_target.values())pagination = {}if args is not None:query = args.query.strip().casefold() if args.query else ""diff --git a/plugins/codex-security/scripts/workbench_scan_usage.py b/plugins/codex-security/scripts/workbench_scan_usage.pyindex 301ff9cbe..23f4cf1ef 100644--- a/plugins/codex-security/scripts/workbench_scan_usage.py+++ b/plugins/codex-security/scripts/workbench_scan_usage.py@@ -127,7 +127,6 @@ def collect_scan_usage(rate_limits: dict[str, dict[str, Any]] = {}tokens_updated_at = Noneaccepted_thread_ids: set[str] = set()- measured_thread_ids: set[str] = set()excluded_thread_ids: set[str] = set()for session in sessions:if session.parent_thread_id in excluded_thread_ids:@@ -171,7 +170,6 @@ def collect_scan_usage(if "token_usage_unavailable" in usage.warnings:missing_thread_ids.add(session.thread_id)continue- measured_thread_ids.add(session.thread_id)role = by_role["parent" if session.thread_id in parent_ids else "workers"]_add_token_usage(role, usage.tokens)role["threadCount"] += 1@@ -182,7 +180,8 @@ def collect_scan_usage(if not accepted_thread_ids:return _unavailable_usage("scan_thread_unavailable", warnings=warnings)- if not measured_thread_ids:+ thread_count = sum(role["threadCount"] for role in by_role.values())+ if not thread_count:return _unavailable_usage("token_usage_unavailable", warnings=warnings, rate_limits=rate_limits)@@ -202,7 +201,7 @@ def collect_scan_usage("coverage": "partial" if missing_thread_ids or warnings else "complete","source": "codex_rollout",**{key: sum(role[key] for role in by_role.values()) for key in TOKEN_FIELDS.values()},- "threadCount": len(measured_thread_ids),+ "threadCount": thread_count,"byRole": by_role,"window": {"startedAt": started_at.isoformat(), "endedAt": stopped_at.isoformat()},"observedAt": observed_at.isoformat(),@@ -506,6 +505,14 @@ def _read_rollout_usage(closed_turns: set[str] = set()measured_turns: set[str] = set()+ def past_scan_end(timestamp: datetime, turn_id: object) -> bool:+ # The requested completion turn may finish after the scan was marked complete.+ return (+ completed_at is not None+ and timestamp > completed_at+ and (completed_turn_id is None or turn_id != completed_turn_id)+ )+with session.path.open("rb") as source:for line_number, raw_line in enumerate(source, start=1):if not raw_line.endswith(b"\n"):@@ -559,10 +566,8 @@ def _read_rollout_usage(if task_started_at is None:warnings.add("thread_ownership_unavailable")return result- if task_started_at < started_at or (- completed_at is not None- and task_started_at > completed_at- and payload.get("turn_id") != completed_turn_id+ if task_started_at < started_at or past_scan_end(+ task_started_at, payload.get("turn_id")):warnings.add("thread_outside_scan_window")return result@@ -576,16 +581,8 @@ def _read_rollout_usage(if event.get("type") == "event_msg" and payload.get("type") == "task_started":# The same owner can resume as a model-backed coordinator. Only# a provably post-scan turn leaves the original control zero intact.- task_started_at = _timestamp(event.get("timestamp"))- if (- completed_at is None- or task_started_at is None- or task_started_at <= completed_at- or (- completed_turn_id is not None- and payload.get("turn_id") == completed_turn_id- )- ):+ timestamp = _timestamp(event.get("timestamp"))+ if timestamp is None or not past_scan_end(timestamp, payload.get("turn_id")):direct_control_session = Falseif (completed_turn_id is not None@@ -601,14 +598,9 @@ def _read_rollout_usage(if payload.get("turn_id") != current_turn_id:observed_response_usage = _empty_token_usage()current_turn_id = payload.get("turn_id")- timestamp = _timestamp(event.get("timestamp"))if not isinstance(current_turn_id, str) or not current_turn_id or timestamp is None:warnings.add("turn_record_invalid")- elif (- completed_at is None- or timestamp <= completed_at- or current_turn_id == completed_turn_id- ):+ elif not past_scan_end(timestamp, current_turn_id):started_turns.add(current_turn_id)open_turns.add(current_turn_id)continue@@ -624,11 +616,7 @@ def _read_rollout_usage(or timestamp is None):warnings.add("turn_record_invalid")- elif (- completed_at is None- or timestamp <= completed_at- or terminal_turn_id == completed_turn_id- ):+ elif not past_scan_end(timestamp, terminal_turn_id):open_turns.discard(terminal_turn_id)if timestamp >= started_at:if terminal_turn_id in started_turns:@@ -648,11 +636,7 @@ def _read_rollout_usage(if (timestamp is not Noneand timestamp >= started_at- and (- completed_at is None- or timestamp <= completed_at- or (completed_turn_id is not None and current_turn_id == completed_turn_id)- )+ and not past_scan_end(timestamp, current_turn_id)and "compaction_response_id" not in payloadand response_usage is None):@@ -675,15 +659,13 @@ def _read_rollout_usage():warnings.add("token_record_invalid")continue- is_completion_turn = (- completed_turn_id is not None and payload.get("turn_id") == completed_turn_id- )- if completed_at is not None and timestamp > completed_at and not is_completion_turn:+ receipt_turn_id = payload.get("turn_id")+ if past_scan_end(timestamp, receipt_turn_id):continueif response_usage is None:response_usage = _empty_token_usage()if current_turn_id is None:- current_turn_id = payload.get("turn_id")+ current_turn_id = receipt_turn_idif isinstance(current_turn_id, str):open_turns.add(current_turn_id)if response_id in response_ids:@@ -691,14 +673,13 @@ def _read_rollout_usage(response_ids.add(response_id)_add_token_usage(observed_response_usage, usage)turn_usage = _usage_counts(payload.get("turn_token_usage"))- receipt_turn_id = payload.get("turn_id")if isinstance(receipt_turn_id, str):measured_turns.add(receipt_turn_id)if timestamp < started_at:if turn_usage is not None:observed_response_usage = turn_usagecontinue- if is_completion_turn:+ if completed_turn_id is not None and receipt_turn_id == completed_turn_id:completed_turn_measured = Trueif turn_usage is not None and any(turn_usage[key] > observed_response_usage[key] for key in TOKEN_FIELDS.values()@@ -713,11 +694,7 @@ def _read_rollout_usage(in_window = (timestamp is not Noneand timestamp >= started_at- and (- completed_at is None- or timestamp <= completed_at- or (completed_turn_id is not None and current_turn_id == completed_turn_id)- )+ and not past_scan_end(timestamp, current_turn_id))if in_window:rate_limits = _rate_limit_snapshot(payload.get("rate_limits"), timestamp)@@ -730,7 +707,6 @@ def _read_rollout_usage(result.rate_limits[key] = rate_limitsif "info" in payload and payload["info"] is None:continue- timestamp = _timestamp(event.get("timestamp"))snapshot = _token_snapshot(payload)if timestamp is None or snapshot is None:warnings.add("token_record_invalid")@@ -740,16 +716,13 @@ def _read_rollout_usage(for key, value in snapshot.items()}previous = snapshot- is_completion_turn = (- completed_turn_id is not None and current_turn_id == completed_turn_id- )- if completed_at is not None and timestamp > completed_at and not is_completion_turn:+ if past_scan_end(timestamp, current_turn_id):continueif isinstance(current_turn_id, str):measured_turns.add(current_turn_id)if timestamp < started_at:continue- if is_completion_turn:+ if completed_turn_id is not None and current_turn_id == completed_turn_id:completed_turn_measured = Trueresult.tokens_updated_at = max(result.tokens_updated_at or timestamp, timestamp)if delta["totalTokens"] <= 0:diff --git a/plugins/codex-security/tests/deep_deadline_crash.py b/plugins/codex-security/tests/deep_deadline_crash.pyindex d729b477b..c5dda0a3b 100644--- a/plugins/codex-security/tests/deep_deadline_crash.py+++ b/plugins/codex-security/tests/deep_deadline_crash.py@@ -3,6 +3,7 @@import osimport runpyimport sqlite3+import subprocessimport sysfrom pathlib import Pathfrom types import SimpleNamespace@@ -15,6 +16,23 @@ class CrashBeforeCommit(sqlite3.Connection):os._exit(COMMIT_EXIT)+def interrupt_finish_before_commit(database, scan, hash_seed):+ result = subprocess.run(+ [+ sys.executable,+ "-B",+ __file__,+ str(database),+ scan.scan_id,+ str(scan.scan_dir),+ ],+ env={**os.environ, "PYTHONHASHSEED": str(hash_seed)},+ capture_output=True,+ text=True,+ )+ assert result.returncode == COMMIT_EXIT, result.stdout + result.stderr++def finish_until_commit(deep_scan, database, scan_id, scan_dir):with sqlite3.connect(database, factory=CrashBeforeCommit) as connection:connection.row_factory = sqlite3.Rowdiff --git a/plugins/codex-security/tests/test_checkpoint_publication_authority.py b/plugins/codex-security/tests/test_checkpoint_publication_authority.pyindex b956eed53..e7ba05168 100644--- a/plugins/codex-security/tests/test_checkpoint_publication_authority.py+++ b/plugins/codex-security/tests/test_checkpoint_publication_authority.py@@ -12,13 +12,6 @@ from argparse import Namespacefrom pathlib import Pathimport pytest-from test_deep_scan_successful_publication import (- add_worker,- complete,- fail_scan,- preserve_scan_results,- recover_scan_results,-)from test_deep_scan_successful_publication import publication_scan as publication_scanfrom test_workbench_standard_deep_results import deep_scan_fixture, worker_pathsfrom workbench_test_support import (@@ -26,9 +19,11 @@ from workbench_test_support import (read_review_state,run_workbench,saved_draft,+ select_checkpoint,sort_checkpoint_entries,write_checkpoint,write_completed_contract,+ write_result_draft,)@@ -48,12 +43,10 @@ def captured_head(scan_dir, directory):@pytest.mark.parametrize("source_name", ["ordinary-source.json", "sourceTimes"])@pytest.mark.parametrize("replay", ["preserve", "recover"])-def test_flat_retained_source_name_replays(- workbench_api, workbench_db, publication_scan, source_name, replay-):+def test_flat_retained_source_name_replays(workbench_db, publication_scan, source_name, replay):scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- add_worker(workbench_db, scan, status="canceled")+ scan.add_worker(status="canceled")source = scan.scan_dir / source_namesource.write_text(json.dumps(@@ -70,7 +63,7 @@ def test_flat_retained_source_name_replays("UPDATE deep_scan_workers SET result_manifest_path = ? WHERE scan_id = ?",(str(source), scan.scan_id),)- stopped = fail_scan(workbench_api, workbench_db, scan, "Stopped scan.")+ stopped = scan.fail("Stopped scan.")assert stopped["findingCount"] == 1findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]retained = json.loads(@@ -80,9 +73,9 @@ def test_flat_retained_source_name_replays()assert isinstance(retained[source_name], str)if replay == "preserve":- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ replayed = scan.preserve()else:- replayed = recover_scan_results(workbench_api, workbench_db, scan)+ replayed = scan.recover()assert replayed["findingCount"] == 1assert json.loads((scan.scan_dir / "findings.json").read_text())["findings"] == findings@@ -160,8 +153,7 @@ def test_public_stop_retains_accepted_partial_evidence_and_newer_rejection(tmp_p},}head = write_checkpoint(output / "checkpoints", rejected)- pointer = output / "checkpoint-head.json"- pointer.write_text(json.dumps({"checkpoint": head.name}))+ pointer = select_checkpoint(output, head)observed = result.stat().st_mtime_nsos.utime(pointer, ns=(observed, observed))workers.append(worker_id)@@ -200,7 +192,7 @@ def test_public_stop_retains_accepted_partial_evidence_and_newer_rejection(tmp_p@pytest.mark.parametrize("has_head", [True, False], ids=["committed-head", "legacy"])@pytest.mark.parametrize("complete", [False, True], ids=["checkpoint", "complete"])def test_recovery_honors_rejection_committed_before_result_replacement(- workbench_api, workbench_db, publication_scan, archived, has_head, complete+ workbench_db, publication_scan, archived, has_head, complete):scan = publication_scan()provisional = copy.deepcopy(scan.findings[0])@@ -211,7 +203,7 @@ def test_recovery_honors_rejection_committed_before_result_replacement(retained["locations"][0]["startLine"] = 20retained["locations"][0]["endLine"] = 21(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result_path = add_worker(workbench_db, scan, status="canceled")+ result_path = scan.add_worker(status="canceled")if archived:result_path = result_path.parent / "attempts" / "attempt-1" / "result.json"result_path.parent.mkdir(parents=True)@@ -241,15 +233,14 @@ def test_recovery_honors_rejection_committed_before_result_replacement(}checkpoint = write_checkpoint(result_path.parent / "checkpoints", rejected)if has_head:- head = result_path.parent / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ head = select_checkpoint(result_path.parent, checkpoint)# Model a committed replacement after the older result, even when the# filesystem assigns the same timestamp to these consecutive writes.observed = max(result_path.stat().st_mtime_ns, checkpoint.stat().st_mtime_ns) + 1os.utime(head, ns=(observed, observed))saved_bytes = {path: path.read_bytes() for path in (result_path, old_checkpoint, checkpoint)}- stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ stopped = scan.fail("Audit stopped.")findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert stopped["findingCount"] == len(findings) == (1 if has_head else 2)@@ -285,8 +276,7 @@ def save_disposition(scan, directory, disposition):},}checkpoint = write_checkpoint(directory / "checkpoints", draft)- head = directory / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ head = select_checkpoint(directory, checkpoint)result = directory / "result.json"observed = (max(@@ -311,7 +301,7 @@ def test_recovery_needed_compares_checkpoint_observation():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))os.utime(result, ns=(200, 200))save_disposition(scan, result.parent, "rejected")@@ -335,10 +325,10 @@ def test_recovery_needed_compares_checkpoint_observation(if legacy_publication:patch.setattr("workbench.retained_coverage._capture_saved_source", capture_legacy_head)patch.setattr(saved, "_capture_saved_source", capture_legacy_head)- stopped = fail_scan(workbench_api, workbench_db, scan)+ stopped = scan.fail()assert stopped["findingCount"] == 1os.utime(head, ns=(300, 300))- published = recover_scan_results(workbench_api, workbench_db, scan)+ published = scan.recover()assert published["findingCount"] == 0manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]@@ -358,7 +348,7 @@ def test_recovery_needed_compares_checkpoint_observation(assert workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"]["resultsRecoveryNeeded"] is (selected_time == 100)- recovered = recover_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.recover()assert recovered["findingCount"] == (1 if selected_time == 100 else 0)assert not recovered["resultsRecoveryNeeded"]final = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]@@ -369,11 +359,11 @@ def test_recovery_needed_compares_checkpoint_observation(@pytest.mark.parametrize("archived", [False, True], ids=["current-head", "newer-archive"])@pytest.mark.parametrize("disposition", ["reported", "rejected"])def test_newer_checkpoint_disposition_precedes_older_archived_head(- workbench_api, workbench_db, publication_scan, archived, disposition+ workbench_db, publication_scan, archived, disposition):scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")with workbench_db:workbench_db.execute("UPDATE deep_scan_workers SET attempt = 3 WHERE scan_id = ?", (scan.scan_id,)@@ -384,7 +374,7 @@ def test_newer_checkpoint_disposition_precedes_older_archived_head(draft = save_disposition(scan, current, disposition)(current / "result.json").write_text(json.dumps(draft))- stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ stopped = scan.fail("Audit stopped.")assert stopped["findingCount"] == (1 if disposition == "reported" else 0)@@ -392,17 +382,13 @@ def test_newer_checkpoint_disposition_precedes_older_archived_head(@pytest.mark.parametrize("head_change", ["replaced", "removed", "missing-checkpoint"])@pytest.mark.parametrize("root_head", [False, True], ids=["worker", "root"])def test_frozen_stopped_replay_ignores_later_worker_head_changes(- workbench_api, workbench_db, publication_scan, monkeypatch, head_change, root_head+ workbench_db, publication_scan, monkeypatch, head_change, root_head):import finalize_scan_contractscan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = (- scan.scan_dir / "result.json"- if root_head- else add_worker(workbench_db, scan, status="canceled")- )+ result = scan.scan_dir / "result.json" if root_head else scan.add_worker(status="canceled")previous = save_disposition(scan, result.parent, "reported")result.write_text(json.dumps(previous))save_disposition(scan, result.parent, "rejected")@@ -435,7 +421,7 @@ def test_frozen_stopped_replay_ignores_later_worker_head_changes(with monkeypatch.context() as patch:patch.setattr(finalize_scan_contract, "write_scan_local_bytes", fail_manifest_write)- fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ scan.fail("Audit stopped.")row = workbench_db.execute("SELECT * FROM scans WHERE id = ?", (scan.scan_id,)).fetchone()if not root_head:assert (head.read_bytes(), head.stat().st_mtime_ns) == original_head@@ -463,7 +449,7 @@ def test_frozen_stopped_replay_ignores_later_worker_head_changes(else:head.write_text(json.dumps({"checkpoint": "a" * 64 + ".json"}))- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ replayed = scan.preserve()assert replayed["findingCount"] == 0assert json.loads((scan.scan_dir / "findings.json").read_text())["findings"] == []@@ -490,18 +476,18 @@ def test_explicit_recovery_observes_head_change_between_existing_checkpoints():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")previous = save_disposition(scan, result.parent, "reported")result.write_text(json.dumps(previous))save_disposition(scan, result.parent, "rejected")- stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ stopped = scan.fail("Audit stopped.")assert stopped["findingCount"] == 0save_disposition(scan, result.parent, "reported")context = workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"]assert context["resultsRecoveryNeeded"] is True- recovered = recover_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.recover()assert recovered["findingCount"] == 1assert recovered["resultsRecoveryNeeded"] is False@@ -511,7 +497,7 @@ def test_legacy_frozen_publication_keeps_result_fallback_without_saved_heads():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")previous = save_disposition(scan, result.parent, "reported")result.write_text(json.dumps(previous))save_disposition(scan, result.parent, "rejected")@@ -523,14 +509,14 @@ def test_legacy_frozen_publication_keeps_result_fallback_without_saved_heads("_write_prepared_scan_finalization",interrupt_publication,)- fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ scan.fail("Audit stopped.")with workbench_db:workbench_db.execute("UPDATE scans SET retained_checkpoint_heads_json = NULL WHERE id = ?", (scan.scan_id,))save_disposition(scan, result.parent, "rejected")- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ replayed = scan.preserve()assert replayed["findingCount"] == 1@@ -538,23 +524,19 @@ def test_legacy_frozen_publication_keeps_result_fallback_without_saved_heads(@pytest.mark.parametrize("head_time", [0, 1, -1], ids=["tie", "newer", "older"])@pytest.mark.parametrize("root_head", [False, True], ids=["worker", "root"])def test_recovery_uses_live_selection_regardless_of_head_timestamp(- workbench_api, workbench_db, publication_scan, head_time, root_head+ publication_scan, head_time, root_head):scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = (- scan.scan_dir / "result.json"- if root_head- else add_worker(workbench_db, scan, status="canceled")- )+ result = scan.scan_dir / "result.json" if root_head else scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))save_disposition(scan, result.parent, "rejected")head = result.parent / "checkpoint-head.json"observed = head.stat().st_mtime_ns- fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ scan.fail("Audit stopped.")save_disposition(scan, result.parent, "reported")os.utime(head, ns=(observed + head_time, observed + head_time))- recovered = recover_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.recover()assert recovered["findingCount"] == 1assert recovered["resultsRecoveryNeeded"] is False@@ -577,11 +559,7 @@ def test_head_reselection_preserves_equal_or_newer_pending_evidence():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = (- scan.scan_dir / "result.json"- if root_head- else add_worker(workbench_db, scan, status="canceled")- )+ result = scan.scan_dir / "result.json" if root_head else scan.add_worker(status="canceled")if root_head:for name in ("findings.json", "coverage.json", "scan-manifest.json"):os.utime(scan.scan_dir / name, ns=(0, 0))@@ -599,7 +577,7 @@ def test_head_reselection_preserves_equal_or_newer_pending_evidence({"id": "review-task", "reason": "Review completed."}]checkpoint = write_checkpoint(result.parent / "checkpoints", selected)- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ select_checkpoint(result.parent, checkpoint)os.utime(head, ns=(head_time, head_time))identity = {"id": "review-task"} if generic else {"candidateId": "candidate-disposition"}pending = write_checkpoint(@@ -620,7 +598,7 @@ def test_head_reselection_preserves_equal_or_newer_pending_evidence(os.utime(pending, ns=(pending_time, pending_time))sort_checkpoint_entries(monkeypatch, reverse=reverse_order)- fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ scan.fail("Audit stopped.")coverage = read_review_state(workbench_db, scan.scan_dir)key, value = next(iter(identity.items()))@@ -635,7 +613,6 @@ def test_head_reselection_preserves_equal_or_newer_pending_evidence(@pytest.mark.parametrize("reverse_order", [False, True])def test_reselection_keeps_the_selected_model_and_newer_result_precedence(workbench_api,- workbench_db,publication_scan,monkeypatch,head_time,@@ -643,25 +620,23 @@ def test_reselection_keeps_the_selected_model_and_newer_result_precedence(reverse_order,):scan = publication_scan()- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")saved = workbench_api["saved_results"]head = result.parent / "checkpoint-head.json"draft = save_disposition(scan, result.parent, "reported")draft["threatModel"] = {"summary": "The current result model."}- result.write_text(json.dumps(draft))- os.utime(result, ns=(result_time, result_time))+ write_result_draft(result, draft, mtime_ns=result_time)for summary, observed in (("The earlier model.", 300), ("The selected model.", head_time)):draft["threatModel"] = {"summary": summary}checkpoint = write_checkpoint(result.parent / "checkpoints", draft)- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))+ select_checkpoint(result.parent, checkpoint, mtime_ns=observed)if summary == "The earlier model.":saved._capture_saved_source(scan.scan_dir, head.relative_to(scan.scan_dir).as_posix(), scan.scan_id)sort_checkpoint_entries(monkeypatch, reverse=reverse_order)- fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ scan.fail("Audit stopped.")manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]expected = "The current result model." if result_time > head_time else "The selected model."@@ -671,11 +646,11 @@ def test_reselection_keeps_the_selected_model_and_newer_result_precedence(@pytest.mark.parametrize("head_time", [100, 300], ids=["older-selection", "tied-selection"])@pytest.mark.parametrize("reverse_order", [False, True])def test_partial_reselection_keeps_independent_accepted_dispositions(- workbench_api, workbench_db, publication_scan, monkeypatch, head_time, reverse_order+ workbench_api, publication_scan, monkeypatch, head_time, reverse_order):scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))os.utime(result, ns=(50, 50))selected = save_disposition(scan, result.parent, "rejected")@@ -688,11 +663,10 @@ def test_partial_reselection_keeps_independent_accepted_dispositions(selected["complete"] = Falseselected["coverage"]["surfaces"][0]["candidateId"] = "independent-candidate"checkpoint = write_checkpoint(result.parent / "checkpoints", selected)- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(head_time, head_time))+ select_checkpoint(result.parent, checkpoint, mtime_ns=head_time)sort_checkpoint_entries(monkeypatch, reverse=reverse_order)- stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ stopped = scan.fail("Audit stopped.")assert stopped["findingCount"] == 0@@ -703,11 +677,7 @@ def test_publication_metadata_uses_the_captured_head():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = (- scan.scan_dir / "result.json"- if root_head- else add_worker(workbench_db, scan, status="canceled")- )+ result = scan.scan_dir / "result.json" if root_head else scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))saved = workbench_api["saved_results"]capture = saved._capture_saved_source@@ -724,7 +694,7 @@ def test_publication_metadata_uses_the_captured_head(with monkeypatch.context() as patch:patch.setattr("workbench.retained_coverage._capture_saved_source", replace_before_capture)- stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ stopped = scan.fail("Audit stopped.")manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]directory = result.parent.relative_to(scan.scan_dir).as_posix()assert manifest["preservedCheckpointHeads"] == {@@ -743,7 +713,7 @@ def test_legacy_checkpoint_selection_is_reconstructed_from_frozen_sources():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))save_disposition(scan, result.parent, "rejected")saved = workbench_api["saved_results"]@@ -758,13 +728,13 @@ def test_legacy_checkpoint_selection_is_reconstructed_from_frozen_sources(patch.setattr(saved, "_prepare_scan_finalization", legacy_documents)else:patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)- fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ scan.fail("Audit stopped.")with workbench_db:workbench_db.execute("UPDATE scans SET retained_checkpoint_heads_json = NULL WHERE id = ?", (scan.scan_id,))if legacy_state == "failed-publication":- preserve_scan_results(workbench_api, workbench_db, scan)+ scan.preserve()context = workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"]assert context["findingCount"] == 0assert context["resultsRecoveryNeeded"] is False@@ -774,13 +744,12 @@ def test_legacy_omitted_root_head_does_not_require_recovery(workbench_api, workbench_db, publication_scan, monkeypatch):scan = publication_scan()- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))saved = workbench_api["saved_results"]_, parent = saved._read_saved_parent_result(scan.scan_dir, scan.scan_id)checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", parent)- head = scan.scan_dir / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ head = select_checkpoint(scan.scan_dir, checkpoint)prepare = saved._prepare_scan_finalizationdef legacy_documents(*args, **kwargs):@@ -793,7 +762,7 @@ def test_legacy_omitted_root_head_does_not_require_recovery(with monkeypatch.context() as patch:patch.setattr(saved, "_prepare_scan_finalization", legacy_documents)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()manifest_path = scan.scan_dir / "scan-manifest.json"original = manifest_path.read_bytes()manifest = json.loads(original)["scan"]@@ -802,7 +771,7 @@ def test_legacy_omitted_root_head_does_not_require_recovery(assert not workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"]["resultsRecoveryNeeded"]- preserved = preserve_scan_results(workbench_api, workbench_db, scan)+ preserved = scan.preserve()assert not preserved["resultsRecoveryNeeded"]assert manifest_path.read_bytes() == originalobserved = head.stat().st_mtime_ns + 1_000_000@@ -819,7 +788,7 @@ def test_explicit_recovery_preserves_database_only_source_times():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))os.utime(result, ns=(100, 100))saved = workbench_api["saved_results"]@@ -832,7 +801,7 @@ def test_explicit_recovery_preserves_database_only_source_times(with monkeypatch.context() as patch:patch.setattr("workbench.saved_sources.write_scan_local_bytes", fail_source_order)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()retained = json.loads(workbench_db.execute("SELECT retained_source_digests_json FROM scans WHERE id = ?", (scan.scan_id,)@@ -846,7 +815,7 @@ def test_explicit_recovery_preserves_database_only_source_times(else:result.write_bytes(result.read_bytes())os.utime(result, ns=(300, 300))- recovered = recover_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.recover()assert recovered["findingCount"] == (0 if change == "new-checkpoint" else 1)assert not recovered["resultsRecoveryNeeded"]manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]@@ -866,14 +835,10 @@ def test_failed_explicit_recovery_replays_its_frozen_selection():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = (- scan.scan_dir / "result.json"- if root_head- else add_worker(workbench_db, scan, status="canceled")- )+ result = scan.scan_dir / "result.json" if root_head else scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))save_disposition(scan, result.parent, "rejected")- fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ scan.fail("Audit stopped.")original = {name: (scan.scan_dir / name).read_bytes()for name in ("findings.json", "coverage.json", "scan-manifest.json")@@ -902,7 +867,7 @@ def test_failed_explicit_recovery_replays_its_frozen_selection(with pytest.raises((OSError, saved.ContractError), match="Synthetic publication interruption"):- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()assert all((scan.scan_dir / name).read_bytes() == data for name, data in original.items())assert (workbench_db.execute(@@ -939,9 +904,9 @@ def test_failed_explicit_recovery_replays_its_frozen_selection(head.write_bytes(selected_head)os.utime(head, ns=(selected_time, selected_time))if retry == "recover":- recovered = recover_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.recover()else:- recovered = preserve_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.preserve()assert recovered["findingCount"] == 1assert recovered["failureMessage"] == "Audit stopped."retained = json.loads(@@ -957,30 +922,28 @@ def test_failed_recovery_keeps_model_when_worker_registers_an_unselected_checkpoworkbench_api, workbench_db, publication_scan, monkeypatch, failure):scan = publication_scan()- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")original = save_disposition(scan, result.parent, "reported")result.write_text(json.dumps(original))- fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ scan.fail("Audit stopped.")saved = workbench_api["saved_results"]head = result.parent / "checkpoint-head.json"unselected = write_checkpoint(result.parent / "checkpoints",{**original, "threatModel": {"summary": "Unselected worker model."}},+ mtime_ns=400,)- os.utime(unselected, ns=(400, 400))accepted = write_checkpoint(result.parent / "checkpoints",{**original, "threatModel": {"summary": "Latest accepted model."}},+ mtime_ns=200,)- os.utime(accepted, ns=(200, 200))- head.write_text(json.dumps({"checkpoint": accepted.name}))- os.utime(head, ns=(200, 200))+ select_checkpoint(result.parent, accepted, mtime_ns=200)saved._capture_saved_source(scan.scan_dir, head.relative_to(scan.scan_dir).as_posix(), scan.scan_id)selected = write_checkpoint(result.parent / "checkpoints", original)- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(300, 300))+ select_checkpoint(result.parent, selected, mtime_ns=300)args = Namespace(scan_id=scan.scan_id)write = saved.write_scan_local_bytes@@ -1030,7 +993,7 @@ def test_unreadable_worker_head_does_not_hide_other_recoverable_evidence():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- bad = add_worker(workbench_db, scan, status="canceled")+ bad = scan.add_worker(status="canceled")bad.write_text(json.dumps(save_disposition(scan, bad.parent, "reported")))save_disposition(scan, bad.parent, prior_disposition)head = bad.parent / "checkpoint-head.json"@@ -1038,9 +1001,9 @@ def test_unreadable_worker_head_does_not_hide_other_recoverable_evidence(scan.findings[0]["identity"]["anchor"] = "independent-worker"scan.findings[0]["locations"][0]["startLine"] = 20scan.findings[0]["locations"][0]["endLine"] = 21- good = add_worker(workbench_db, scan, status="canceled")+ good = scan.add_worker(status="canceled")save_disposition(scan, good.parent, "rejected")- fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ scan.fail("Audit stopped.")assert (head.read_bytes(), head.stat().st_mtime_ns) == original_headdirectory = bad.parent.relative_to(scan.scan_dir).as_posix()prior_head = captured_head(scan.scan_dir, directory)@@ -1059,7 +1022,7 @@ def test_unreadable_worker_head_does_not_hide_other_recoverable_evidence(workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"]["resultsRecoveryNeeded"]is True)- recovered = recover_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.recover()assert recovered["findingCount"] == (2 if prior_disposition == "reported" else 1)assert recovered["resultsRecoveryNeeded"] is Falsemanifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]@@ -1071,7 +1034,7 @@ def test_failed_head_snapshot_does_not_claim_uncaptured_authority():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))save_disposition(scan, result.parent, "rejected")saved = workbench_api["saved_results"]@@ -1085,7 +1048,7 @@ def test_failed_head_snapshot_does_not_claim_uncaptured_authority(with monkeypatch.context() as patch:patch.setattr("workbench.saved_sources.write_scan_local_bytes", fail_snapshot)- stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ stopped = scan.fail("Audit stopped.")row = workbench_db.execute("SELECT * FROM scans WHERE id = ?", (scan.scan_id,)).fetchone()assert row["seal_manifest_digest"] is Noneheads = json.loads(row["retained_checkpoint_heads_json"])@@ -1103,7 +1066,7 @@ def test_failed_head_snapshot_does_not_claim_uncaptured_authority().fetchone()[0])assert any("Synthetic snapshot write failure" in warning for warning in warnings)- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ replayed = scan.preserve()assert replayed["findingCount"] == 0@@ -1113,15 +1076,11 @@ def test_legacy_recovery_can_reselect_an_already_frozen_head():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = (- scan.scan_dir / "result.json"- if root_head- else add_worker(workbench_db, scan, status="canceled")- )+ result = scan.scan_dir / "result.json" if root_head else scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))head = result.parent / "checkpoint-head.json"original_head, original_time = head.read_bytes(), head.stat().st_mtime_ns- fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ scan.fail("Audit stopped.")save_disposition(scan, result.parent, "rejected")saved = workbench_api["saved_results"]prepare = saved._prepare_scan_finalization@@ -1132,7 +1091,7 @@ def test_legacy_recovery_can_reselect_an_already_frozen_head(with monkeypatch.context() as patch:patch.setattr(saved, "_prepare_scan_finalization", legacy_documents)- rejected = recover_scan_results(workbench_api, workbench_db, scan)+ rejected = scan.recover()assert rejected["findingCount"] == 0with workbench_db:workbench_db.execute(@@ -1144,7 +1103,7 @@ def test_legacy_recovery_can_reselect_an_already_frozen_head(workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"]["resultsRecoveryNeeded"]is True)- recovered = recover_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.recover()assert recovered["findingCount"] == 1assert recovered["resultsRecoveryNeeded"] is False@@ -1153,15 +1112,14 @@ def test_recovery_keeps_newer_result_with_legacy_frozen_live_head(workbench_api, workbench_db, publication_scan):scan = publication_scan()- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")reported = save_disposition(scan, result.parent, "reported")save_disposition(scan, result.parent, "rejected")head = result.parent / "checkpoint-head.json"checkpoint = result.parent / "checkpoints" / json.loads(head.read_text())["checkpoint"]os.utime(checkpoint, ns=(100, 100))os.utime(head, ns=(200, 200))- result.write_text(json.dumps(reported))- os.utime(result, ns=(300, 300))+ write_result_draft(result, reported, mtime_ns=300)saved = workbench_api["saved_results"]sources = {path.relative_to(scan.scan_dir).as_posix(): saved._read_saved_result(@@ -1176,18 +1134,18 @@ def test_recovery_keeps_newer_result_with_legacy_frozen_live_head("WHERE id = ?",(scan.timestamp, json.dumps(sources), scan.scan_id),)- recovered = recover_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.recover()assert recovered["findingCount"] == 1assert recovered["resultsRecoveryNeeded"] is False@pytest.mark.parametrize("directory_name", ["recoveryBaseDigest", "heads"])def test_worker_directory_does_not_collide_with_recovery_metadata(- workbench_api, workbench_db, publication_scan, directory_name+ workbench_db, publication_scan, directory_name):scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- original = add_worker(workbench_db, scan, status="canceled")+ original = scan.add_worker(status="canceled")directory = scan.scan_dir / directory_nameresult = directory / "result.json"with workbench_db:@@ -1197,12 +1155,12 @@ def test_worker_directory_does_not_collide_with_recovery_metadata((str(directory), str(result), scan.scan_id, str(original.parent)),)result.write_text(json.dumps(save_disposition(scan, directory, "reported")))- stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ stopped = scan.fail("Audit stopped.")assert stopped["findingCount"] == 1- preserved = preserve_scan_results(workbench_api, workbench_db, scan)+ preserved = scan.preserve()assert preserved["findingCount"] == 1save_disposition(scan, directory, "rejected")- recovered = recover_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.recover()assert recovered["findingCount"] == 0assert recovered["resultsRecoveryNeeded"] is False@@ -1220,14 +1178,13 @@ def test_unreadable_heads_preserve_legacy_accepted_results(legacy_heads,):scan = publication_scan()- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")saved = workbench_api["saved_results"]sources = {}for disposition in ("reported", "rejected"):draft = save_disposition(scan, result.parent, disposition)if disposition == "reported":- result.write_text(json.dumps(draft))- os.utime(result, ns=(300, 300))+ write_result_draft(result, draft, mtime_ns=300)head = result.parent / "checkpoint-head.json"os.utime(head, ns=(300, 300))sources.update(@@ -1255,7 +1212,7 @@ def test_unreadable_heads_preserve_legacy_accepted_results("WHERE id = ?",(scan.timestamp, json.dumps(sources), scan.scan_id),)- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ replayed = scan.preserve()assert replayed["findingCount"] == 1assert replayed["resultsRecoveryNeeded"] is False@@ -1273,9 +1230,9 @@ def test_unreadable_heads_preserve_legacy_accepted_results(scan.findings[0]["identity"]["anchor"] = "independent-worker"scan.findings[0]["locations"][0]["startLine"] = 20scan.findings[0]["locations"][0]["endLine"] = 21- good = add_worker(workbench_db, scan, status="canceled")+ good = scan.add_worker(status="canceled")save_disposition(scan, good.parent, "reported")- recovered = recover_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.recover()assert recovered["findingCount"] == 2assert recovered["resultsRecoveryNeeded"] is False@@ -1288,10 +1245,10 @@ def test_invalid_explicit_recovery_does_not_freeze_unpublishable_selection(workbench_api, workbench_db, publication_scan, invalid_binding, snapshot_failure, monkeypatch):scan = publication_scan()- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")original = save_disposition(scan, result.parent, "reported")result.write_text(json.dumps(original))- fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ scan.fail("Audit stopped.")prior = tuple(workbench_db.execute("SELECT retained_source_digests_json, retained_checkpoint_heads_json FROM scans WHERE id = ?",@@ -1303,8 +1260,7 @@ def test_invalid_explicit_recovery_does_not_freeze_unpublishable_selection("threatModel": {"summary": "Valid first model."},}checkpoint = write_checkpoint(result.parent / "checkpoints", candidate)- head = result.parent / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ select_checkpoint(result.parent, checkpoint)args = Namespace(scan_id=scan.scan_id)if invalid_binding:saved = workbench_api["saved_results"]@@ -1342,7 +1298,7 @@ def test_invalid_explicit_recovery_does_not_freeze_unpublishable_selection(result.parent / "checkpoints",{**original, "threatModel": {"summary": "Corrected selected model."}},)- head.write_text(json.dumps({"checkpoint": corrected.name}))+ select_checkpoint(result.parent, corrected)recovered = workbench_api["saved_results"].recover_scan_results(workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, args)["scan"]@@ -1356,10 +1312,10 @@ def test_invalid_explicit_recovery_does_not_freeze_unpublishable_selection("result_model", [False, True], ids=["unmodeled-result", "older-result-model"])def test_model_less_selected_head_keeps_latest_accepted_model(- workbench_api, workbench_db, publication_scan, selected_model, result_model+ workbench_api, publication_scan, selected_model, result_model):scan = publication_scan()- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")original = save_disposition(scan, result.parent, "reported")modeled = {}for summary in ("First synthetic accepted model.", "Second synthetic accepted model."):@@ -1371,8 +1327,7 @@ def test_model_less_selected_head_keeps_latest_accepted_model(saved = workbench_api["saved_results"]for checkpoint, observed in ((older, 100), (newer, 200)):os.utime(checkpoint, ns=(observed, observed))- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))+ select_checkpoint(result.parent, checkpoint, mtime_ns=observed)saved._capture_saved_source(scan.scan_dir, head.relative_to(scan.scan_dir).as_posix(), scan.scan_id)@@ -1380,10 +1335,8 @@ def test_model_less_selected_head_keeps_latest_accepted_model(**original,**({"threatModel": {"summary": "Selected replacement model."}} if selected_model else {}),}- checkpoint = write_checkpoint(result.parent / "checkpoints", selected)- os.utime(checkpoint, ns=(300, 300))- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(300, 300))+ checkpoint = write_checkpoint(result.parent / "checkpoints", selected, mtime_ns=300)+ select_checkpoint(result.parent, checkpoint, mtime_ns=300)result.write_text(json.dumps({@@ -1394,11 +1347,11 @@ def test_model_less_selected_head_keeps_latest_accepted_model()os.utime(result, ns=(150, 150) if result_model else (400, 400))originals = {path: path.read_bytes() for path in [result, older, newer, checkpoint, head]}- fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ scan.fail("Audit stopped.")expected = "Selected replacement model." if selected_model else modeled[newer]manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())assert manifest["scan"]["threatModel"]["summary"] == expected- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()assert (json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]["threatModel"]["summary"@@ -1414,7 +1367,7 @@ def test_model_less_selected_head_keeps_latest_accepted_model(@pytest.mark.parametrize("newer_sorts_first", [False, True])@pytest.mark.parametrize("restore_model", [False, True], ids=["model-less", "modeled"])def test_restored_head_keeps_latest_model_fallback(- workbench_api, workbench_db, publication_scan, mode, root_head, newer_sorts_first, restore_model+ workbench_api, publication_scan, mode, root_head, newer_sorts_first, restore_model):scan = publication_scan(mode=mode)(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))@@ -1425,16 +1378,14 @@ def test_restored_head_keeps_latest_model_fallback(if root_head:directory = scan.scan_direlse:- result = add_worker(workbench_db, scan, status="canceled")- result.write_text(json.dumps(draft))- os.utime(result, ns=(0, 0))+ result = scan.add_worker(status="canceled")+ write_result_draft(result, draft, mtime_ns=0)directory = result.parenthead = directory / "checkpoint-head.json"saved = workbench_api["saved_results"]def select(checkpoint, observed):- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))+ select_checkpoint(directory, checkpoint, mtime_ns=observed)return head.read_bytes(), observeddef model():@@ -1442,8 +1393,7 @@ def test_restored_head_keeps_latest_model_fallback("threatModel"]["summary"]- empty = write_checkpoint(directory / "checkpoints", draft)- os.utime(empty, ns=(50, 50))+ empty = write_checkpoint(directory / "checkpoints", draft, mtime_ns=50)unmodeled_head = select(empty, 100)saved._capture_saved_source(scan.scan_dir, head.relative_to(scan.scan_dir).as_posix(), scan.scan_id@@ -1458,10 +1408,10 @@ def test_restored_head_keeps_latest_model_fallback(os.utime(checkpoint, ns=(50, 50))older, newer = sorted(models, reverse=newer_sorts_first)modeled_head = select(older, 200)- fail_scan(workbench_api, workbench_db, scan, "Synthetic stop.")+ scan.fail("Synthetic stop.")assert model() == models[older]select(newer, 300)- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()assert model() == models[newer]originals = {filename: (filename.read_bytes(), filename.stat().st_mtime_ns)@@ -1471,7 +1421,7 @@ def test_restored_head_keeps_latest_model_fallback(restored_contents, restored_time = modeled_head if restore_model else unmodeled_headhead.write_bytes(restored_contents)os.utime(head, ns=(restored_time, restored_time))- recovered = recover_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.recover()assert model() == models[older if restore_model else newer]assert not recovered["resultsRecoveryNeeded"]published = {@@ -1479,7 +1429,7 @@ def test_restored_head_keeps_latest_model_fallback(for name in ("scan-manifest.json", "findings.json", "report.md")if (scan.scan_dir / name).exists()}- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ replayed = scan.preserve()assert not replayed["resultsRecoveryNeeded"]assert all((scan.scan_dir / name).read_bytes() == contents for name, contents in published.items()@@ -1499,14 +1449,14 @@ def test_registered_checkpoint_result_keeps_newer_accepted_outcome():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan)+ result = scan.add_worker()reported = {**save_disposition(scan, result.parent, "reported"),"threatModel": {"summary": "Newer accepted model."},}head = result.parent / "checkpoint-head.json"accepted = write_checkpoint(result.parent / "checkpoints", reported)- head.write_text(json.dumps({"checkpoint": accepted.name}))+ select_checkpoint(result.parent, accepted)os.utime(accepted, ns=(300, 300))os.utime(head, ns=(300, 300))workbench_api["saved_results"]._capture_saved_source(@@ -1519,17 +1469,14 @@ def test_registered_checkpoint_result_keeps_newer_accepted_outcome((str(accepted), result.parent.name),)else:- result.write_text(json.dumps(reported))- os.utime(result, ns=(300, 300))+ write_result_draft(result, reported, mtime_ns=300)rejected = {**save_disposition(scan, result.parent, "rejected"),"threatModel": {"summary": "Older selected model."},}- older = write_checkpoint(result.parent / "checkpoints", rejected)- os.utime(older, ns=(200, 200))- head.write_text(json.dumps({"checkpoint": older.name}))- os.utime(head, ns=(200, 200))- stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ older = write_checkpoint(result.parent / "checkpoints", rejected, mtime_ns=200)+ select_checkpoint(result.parent, older, mtime_ns=200)+ stopped = scan.fail("Audit stopped.")assert stopped["findingCount"] == 1assert stopped["resultsRecoveryNeeded"] is False@@ -1539,7 +1486,7 @@ def test_registered_checkpoint_result_keeps_newer_accepted_outcome(]== "Newer accepted model.")- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ replayed = scan.preserve()assert replayed["findingCount"] == 1assert (json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]["threatModel"][@@ -1556,7 +1503,7 @@ def test_restored_observation_keeps_independent_registered_result():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="succeeded")+ result = scan.add_worker(status="succeeded")reported = {**save_disposition(scan, result.parent, "reported"),"threatModel": {"summary": "Registered reporting model."},@@ -1568,8 +1515,7 @@ def test_restored_observation_keeps_independent_registered_result("receiptRefs": [],}reported["coverage"]["surfaces"].append(surface)- accepted = write_checkpoint(result.parent / "checkpoints", reported)- os.utime(accepted, ns=(50, 50))+ accepted = write_checkpoint(result.parent / "checkpoints", reported, mtime_ns=50)stale = {**save_disposition(scan, result.parent, "rejected"),"threatModel": {"summary": "Stale result model."},@@ -1579,27 +1525,22 @@ def test_restored_observation_keeps_independent_registered_result(stale["coverage"]["explicitExclusions"] = [{"pattern": "old-scope/**", "reason": "Obsolete synthetic exclusion."}]- result.write_text(json.dumps(stale))- os.utime(result, ns=(200, 200))+ write_result_draft(result, stale, mtime_ns=200)head = result.parent / "checkpoint-head.json"saved = workbench_api["saved_results"]- def select(observed):- head.write_text(json.dumps({"checkpoint": accepted.name}))- os.utime(head, ns=(observed, observed))-- select(100)+ select_checkpoint(result.parent, accepted, mtime_ns=100)saved._capture_saved_source(scan.scan_dir, head.relative_to(scan.scan_dir).as_posix(), scan.scan_id)- select(300)+ select_checkpoint(result.parent, accepted, mtime_ns=300)if registered:with workbench_db:workbench_db.execute("UPDATE deep_scan_workers SET result_manifest_path = ? WHERE id = ?",(str(accepted), result.parent.name),)- stopped = fail_scan(workbench_api, workbench_db, scan)+ stopped = scan.fail()assert stopped["findingCount"] == 1sources = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]["preservedSources"@@ -1608,7 +1549,7 @@ def test_restored_observation_keeps_independent_registered_result(assert not prior_coverage.get("openQuestions")assert not prior_coverage["explicitExclusions"]original = {path: (scan.scan_dir / path).read_bytes() for path in sources}- select(100)+ select_checkpoint(result.parent, accepted, mtime_ns=100)assert workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"]["resultsRecoveryNeeded"]@@ -1625,11 +1566,11 @@ def test_restored_observation_keeps_independent_registered_result(patch.setattr(saved, "_write_prepared_scan_finalization", publish)if interrupted:with pytest.raises(OSError, match="Synthetic publication interruption"):- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()else:- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()assert prepared_counts == [int(registered)]- recovered = preserve_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.preserve()assert recovered["findingCount"] == int(registered)assert not recovered["resultsRecoveryNeeded"]manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]@@ -1678,34 +1619,30 @@ def test_inherited_model_uses_same_selection_on_initial_and_frozen_publication(draft["coverage"] = {**scan.coverage, **draft["coverage"]}if model_choice == "selected-head":draft["threatModel"] = {"summary": "Selected root model."}- selected = write_checkpoint(scan.scan_dir / "checkpoints", draft)- os.utime(selected, ns=(50, 50))- head = scan.scan_dir / "checkpoint-head.json"+ selected = write_checkpoint(scan.scan_dir / "checkpoints", draft, mtime_ns=50)saved = workbench_api["saved_results"]if model_choice in {"canonical-fallback", "older-accepted"}:unselected = write_checkpoint(scan.scan_dir / "checkpoints",{**draft, "threatModel": {"summary": "Unaccepted model."}},+ mtime_ns=50,)- os.utime(unselected, ns=(50, 50))sort_checkpoint_entries(monkeypatch, key=lambda name: name != unselected.name)- def select(checkpoint, observed):- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))-- select(selected, 200)+ select_checkpoint(scan.scan_dir, selected, mtime_ns=200)saved._capture_saved_source(scan.scan_dir, "checkpoint-head.json", scan.scan_id)if model_choice != "canonical-fallback":other_model = write_checkpoint(scan.scan_dir / "checkpoints",{**draft, "threatModel": {"summary": "Latest accepted model."}},+ mtime_ns=50,+ )+ select_checkpoint(+ scan.scan_dir, other_model, mtime_ns=100 if model_choice == "older-accepted" else 300)- os.utime(other_model, ns=(50, 50))- select(other_model, 100 if model_choice == "older-accepted" else 300)saved._capture_saved_source(scan.scan_dir, "checkpoint-head.json", scan.scan_id)- select(selected, 200)+ select_checkpoint(scan.scan_dir, selected, mtime_ns=200)prepared_models = []write = saved._write_prepared_scan_finalization@@ -1731,13 +1668,13 @@ def test_inherited_model_uses_same_selection_on_initial_and_frozen_publication("_encode_retained_sources",lambda sources, model_source, source_times=None: encode(sources, [], source_times),)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()retained = workbench_db.execute("SELECT retained_source_digests_json FROM scans WHERE id = ?", (scan.scan_id,)).fetchone()[0]if legacy_format:- recover_scan_results(workbench_api, workbench_db, scan)- preserved = preserve_scan_results(workbench_api, workbench_db, scan)+ scan.recover()+ preserved = scan.preserve()expected = {"latest-accepted": "Latest accepted model.","selected-head": "Selected root model.",@@ -1770,7 +1707,7 @@ def test_inherited_model_uses_same_selection_on_initial_and_frozen_publication("model_choice", ["canonical-fallback", "latest-accepted", "selected-head", "older-accepted"])def test_successful_completion_keeps_accepted_root_model_fallback(- workbench_api, workbench_db, publication_scan, monkeypatch, prepare_only, model_choice+ workbench_api, publication_scan, monkeypatch, prepare_only, model_choice):scan = publication_scan(mode="standard")(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))@@ -1784,38 +1721,34 @@ def test_successful_completion_keeps_accepted_root_model_fallback(draft["coverage"] = {**scan.coverage, **draft["coverage"]}if model_choice == "selected-head":draft["threatModel"] = {"summary": "Selected root model."}- selected = write_checkpoint(scan.scan_dir / "checkpoints", draft)- os.utime(selected, ns=(50, 50))- head = scan.scan_dir / "checkpoint-head.json"+ selected = write_checkpoint(scan.scan_dir / "checkpoints", draft, mtime_ns=50)saved = workbench_api["saved_results"]- def select(checkpoint, observed):- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))-- select(selected, 200)+ select_checkpoint(scan.scan_dir, selected, mtime_ns=200)saved._capture_saved_source(scan.scan_dir, "checkpoint-head.json", scan.scan_id)unaccepted = write_checkpoint(scan.scan_dir / "checkpoints",{**draft, "threatModel": {"summary": "Unaccepted model."}},+ mtime_ns=50,)- os.utime(unaccepted, ns=(50, 50))sort_checkpoint_entries(monkeypatch, key=lambda name: name != unaccepted.name)if model_choice != "canonical-fallback":modeled = write_checkpoint(scan.scan_dir / "checkpoints",{**draft, "threatModel": {"summary": "Latest accepted model."}},+ mtime_ns=50,+ )+ select_checkpoint(+ scan.scan_dir, modeled, mtime_ns=100 if model_choice == "older-accepted" else 300)- os.utime(modeled, ns=(50, 50))- select(modeled, 100 if model_choice == "older-accepted" else 300)saved._capture_saved_source(scan.scan_dir, "checkpoint-head.json", scan.scan_id)- select(selected, 200)+ select_checkpoint(scan.scan_dir, selected, mtime_ns=200)original = {path: (path.read_bytes(), path.stat().st_mtime_ns)for directory in ("checkpoints", "checkpoint-heads")for path in (scan.scan_dir / directory).glob("*.json")}- completed = complete(workbench_api, workbench_db, scan, prepare_only=prepare_only)+ completed = scan.complete(prepare_only=prepare_only)expected = {"canonical-fallback": "Old canonical model.","latest-accepted": "Latest accepted model.",@@ -1857,14 +1790,14 @@ def test_recovery_keeps_known_model_source_with_equal_model_duplicates(saved = workbench_api["saved_results"]_, canonical = saved._read_saved_parent_result(scan.scan_dir, scan.scan_id)older = write_checkpoint(- scan.scan_dir / "checkpoints", {**canonical, "threatModel": {"summary": "Older model."}}+ scan.scan_dir / "checkpoints",+ {**canonical, "threatModel": {"summary": "Older model."}},+ mtime_ns=50,)- os.utime(older, ns=(50, 50))if duplicate_order is not None:duplicate = copy.deepcopy(canonical)duplicate["reviewState"]["openQuestions"] = [{"question": "Earlier synthetic review."}]- duplicate_path = write_checkpoint(scan.scan_dir / "checkpoints", duplicate)- os.utime(duplicate_path, ns=(50, 50))+ duplicate_path = write_checkpoint(scan.scan_dir / "checkpoints", duplicate, mtime_ns=50)sort_checkpoint_entries(monkeypatch,key=lambda name: (@@ -1873,19 +1806,13 @@ def test_recovery_keeps_known_model_source_with_equal_model_duplicates(else name == duplicate_path.name),)- head = scan.scan_dir / "checkpoint-head.json"- def select(checkpoint, observed):- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))-- select(older, 100)+ select_checkpoint(scan.scan_dir, older, mtime_ns=100)saved._capture_saved_source(scan.scan_dir, "checkpoint-head.json", scan.scan_id)empty = {key: value for key, value in canonical.items() if key != "threatModel"}- selected = write_checkpoint(scan.scan_dir / "checkpoints", empty)- os.utime(selected, ns=(50, 50))- select(selected, 200)- fail_scan(workbench_api, workbench_db, scan)+ selected = write_checkpoint(scan.scan_dir / "checkpoints", empty, mtime_ns=50)+ select_checkpoint(scan.scan_dir, selected, mtime_ns=200)+ scan.fail()retained = json.loads(workbench_db.execute("SELECT retained_source_digests_json FROM scans WHERE id = ?", (scan.scan_id,)@@ -1904,12 +1831,13 @@ def test_recovery_keeps_known_model_source_with_equal_model_duplicates()if new_model:newer = write_checkpoint(- scan.scan_dir / "checkpoints", {**canonical, "threatModel": {"summary": "New model."}}+ scan.scan_dir / "checkpoints",+ {**canonical, "threatModel": {"summary": "New model."}},+ mtime_ns=50,)- os.utime(newer, ns=(50, 50))- select(newer, 250)+ select_checkpoint(scan.scan_dir, newer, mtime_ns=250)saved._capture_saved_source(scan.scan_dir, "checkpoint-head.json", scan.scan_id)- select(selected, 300)+ select_checkpoint(scan.scan_dir, selected, mtime_ns=300)prepared_models = []write = saved._write_prepared_scan_finalization@@ -1923,12 +1851,12 @@ def test_recovery_keeps_known_model_source_with_equal_model_duplicates(patch.setattr(saved, "_write_prepared_scan_finalization", publish)if interrupted:with pytest.raises(OSError, match="Synthetic publication interruption"):- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()else:- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()expected = "New model." if new_model else "Canonical model."assert prepared_models == [expected]- preserve_scan_results(workbench_api, workbench_db, scan)+ scan.preserve()assert json.loads(manifest_path.read_text())["scan"]["threatModel"]["summary"] == expected@@ -1957,21 +1885,19 @@ def test_skipped_canonical_checkpoint_keeps_valid_model_and_findings(selected = write_checkpoint(scan.scan_dir / "checkpoints",{key: value for key, value in canonical.items() if key != "threatModel"},+ mtime_ns=50,)- os.utime(selected, ns=(50, 50))- head = scan.scan_dir / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(200, 200))+ head = select_checkpoint(scan.scan_dir, selected, mtime_ns=200)if flow in {"complete", "prepare"}:- published = complete(workbench_api, workbench_db, scan, prepare_only=flow == "prepare")+ published = scan.complete(prepare_only=flow == "prepare")else:with monkeypatch.context() as patch:if flow == "interrupted-stop":patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)- fail_scan(workbench_api, workbench_db, scan)- published = preserve_scan_results(workbench_api, workbench_db, scan)+ scan.fail()+ published = scan.preserve()os.utime(head, ns=(300, 300))- published = recover_scan_results(workbench_api, workbench_db, scan)+ published = scan.recover()assert len(json.loads((scan.scan_dir / "findings.json").read_text())["findings"]) == 1if flow != "prepare":assert published["findingCount"] == 1@@ -2004,7 +1930,7 @@ def test_selected_terminal_coverage_excludes_unselected_history():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")saved = workbench_api["saved_results"]checkpoints = {}for name in ("A", "B"):@@ -2025,16 +1951,11 @@ def test_selected_terminal_coverage_excludes_unselected_history(draft["coverage"]["explicitExclusions"] = [{"pattern": f"{name}/**", "reason": f"{name} exclusion"}]- checkpoints[name] = write_checkpoint(result.parent / "checkpoints", draft)- os.utime(checkpoints[name], ns=(50, 50))+ checkpoints[name] = write_checkpoint(result.parent / "checkpoints", draft, mtime_ns=50)head = result.parent / "checkpoint-head.json"- def select(name, observed):- head.write_text(json.dumps({"checkpoint": checkpoints[name].name}))- os.utime(head, ns=(observed, observed))-for name, observed in (("A", 100), ("B", 200), ("A", 300)):- select(name, observed)+ select_checkpoint(result.parent, checkpoints[name], mtime_ns=observed)saved._capture_saved_source(scan.scan_dir, head.relative_to(scan.scan_dir).as_posix(), scan.scan_id)@@ -2044,8 +1965,8 @@ def test_selected_terminal_coverage_excludes_unselected_history("UPDATE deep_scan_workers SET result_manifest_path = ? WHERE id = ?",(str(checkpoints[registered]), result.parent.name),)- fail_scan(workbench_api, workbench_db, scan)- select("A", restored_time)+ scan.fail()+ select_checkpoint(result.parent, checkpoints["A"], mtime_ns=restored_time)expected = "B" if registered == "B" and restored_time == 100 else "A"with monkeypatch.context() as patch:@@ -2053,10 +1974,10 @@ def test_selected_terminal_coverage_excludes_unselected_history(patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)if interrupted and restored_time == 100:with pytest.raises(OSError, match="Synthetic publication interruption"):- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()else:- recover_scan_results(workbench_api, workbench_db, scan)- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ scan.recover()+ replayed = scan.preserve()coverage = read_review_state(workbench_db, scan.scan_dir)assert [row["label"] for row in coverage["surfaces"]] == [f"{expected} surface"]assert [row["question"] for row in coverage["openQuestions"]] == [f"{expected} question"]@@ -2089,11 +2010,7 @@ def test_selected_model_identity_survives_restore_then_modelless_head((scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))for name in ("findings.json", "coverage.json", "scan-manifest.json"):os.utime(scan.scan_dir / name, ns=(0, 0))- directory = (- scan.scan_dir- if layout == "root"- else add_worker(workbench_db, scan, status="canceled").parent- )+ directory = scan.scan_dir if layout == "root" else scan.add_worker(status="canceled").parentsaved = workbench_api["saved_results"]draft = saved_draft(scan.scan_id, complete=True)draft["coverage"] = {**scan.coverage, **draft["coverage"]}@@ -2106,7 +2023,6 @@ def test_selected_model_identity_survives_restore_then_modelless_head(checkpoints["empty"] = write_checkpoint(directory / "checkpoints", draft)for checkpoint in checkpoints.values():os.utime(checkpoint, ns=(50, 50))- head = directory / "checkpoint-head.json"models = []for index, (name, observed, phase) in enumerate(@@ -2117,19 +2033,18 @@ def test_selected_model_identity_survives_restore_then_modelless_head(("empty", modelless_time, "modelless"),)):- head.write_text(json.dumps({"checkpoint": checkpoints[name].name}))- os.utime(head, ns=(observed, observed))+ select_checkpoint(directory, checkpoints[name], mtime_ns=observed)if index == 0:- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()else:with monkeypatch.context() as patch:if phase == failure_at:patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)with pytest.raises(OSError, match="Synthetic publication interruption"):- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()else:- recover_scan_results(workbench_api, workbench_db, scan)- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ scan.recover()+ replayed = scan.preserve()models.append(json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]["threatModel"]["summary"@@ -2162,11 +2077,7 @@ def test_selected_terminal_coverage_has_root_worker_parity(coverage_path.write_text(json.dumps(initial_coverage))for name in ("findings.json", "coverage.json", "scan-manifest.json"):os.utime(scan.scan_dir / name, ns=(0, 0))- directory = (- scan.scan_dir- if layout == "root"- else add_worker(workbench_db, scan, status="canceled").parent- )+ directory = scan.scan_dir if layout == "root" else scan.add_worker(status="canceled").parentsaved = workbench_api["saved_results"]checkpoints = {}for name in ("A", "B"):@@ -2185,27 +2096,24 @@ def test_selected_terminal_coverage_has_root_worker_parity(openQuestions=[{"question": f"{name} question"}],)draft["threatModel"] = {"summary": f"{name} model"}- checkpoints[name] = write_checkpoint(directory / "checkpoints", draft)- os.utime(checkpoints[name], ns=(50, 50))+ checkpoints[name] = write_checkpoint(directory / "checkpoints", draft, mtime_ns=50)head = directory / "checkpoint-head.json"for name, observed in (("A", 100), ("B", 200), ("A", 300)):- head.write_text(json.dumps({"checkpoint": checkpoints[name].name}))- os.utime(head, ns=(observed, observed))+ select_checkpoint(directory, checkpoints[name], mtime_ns=observed)saved._capture_saved_source(scan.scan_dir, head.relative_to(scan.scan_dir).as_posix(), scan.scan_id)- fail_scan(workbench_api, workbench_db, scan)- head.write_text(json.dumps({"checkpoint": checkpoints["A"].name}))- os.utime(head, ns=(100, 100))+ scan.fail()+ select_checkpoint(directory, checkpoints["A"], mtime_ns=100)with monkeypatch.context() as patch:if interrupted:patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)with pytest.raises(OSError, match="Synthetic publication interruption"):- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()else:- recover_scan_results(workbench_api, workbench_db, scan)- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ scan.recover()+ replayed = scan.preserve()coverage = read_review_state(workbench_db, scan.scan_dir)expected = {"A"} if terminal else {"A", "B"}assert {row["label"] for row in coverage["surfaces"]} == expected@@ -2240,7 +2148,7 @@ def test_terminal_coverage_replacement_does_not_cross_into_a_partial_attempt():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")directory = result.parentwith workbench_db:workbench_db.execute(@@ -2248,11 +2156,11 @@ def test_terminal_coverage_replacement_does_not_cross_into_a_partial_attempt()archive = directory / "attempts" / "attempt-1"old = write_checkpoint(- archive / "checkpoints", saved_draft(scan.scan_id, complete=archived_terminal)+ archive / "checkpoints",+ saved_draft(scan.scan_id, complete=archived_terminal),+ mtime_ns=50,)- os.utime(old, ns=(50, 50))- (archive / "checkpoint-head.json").write_text(json.dumps({"checkpoint": old.name}))- os.utime(archive / "checkpoint-head.json", ns=(100, 100))+ select_checkpoint(archive, old, mtime_ns=100)accepted = saved_draft(scan.scan_id, complete=True)accepted["coverage"].update(surfaces=[@@ -2266,11 +2174,8 @@ def test_terminal_coverage_replacement_does_not_cross_into_a_partial_attempt(explicitExclusions=[{"pattern": "accepted/**", "reason": "Accepted exclusion"}],openQuestions=[{"question": "Accepted question"}],)- accepted_path = write_checkpoint(directory / "checkpoints", accepted)- os.utime(accepted_path, ns=(200, 200))- head = directory / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": accepted_path.name}))- os.utime(head, ns=(200, 200))+ accepted_path = write_checkpoint(directory / "checkpoints", accepted, mtime_ns=200)+ select_checkpoint(directory, accepted_path, mtime_ns=200)if registered:with workbench_db:workbench_db.execute(@@ -2278,22 +2183,22 @@ def test_terminal_coverage_replacement_does_not_cross_into_a_partial_attempt((str(accepted_path), directory.name),)saved = workbench_api["saved_results"]- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()current = write_checkpoint(- directory / "checkpoints", saved_draft(scan.scan_id, complete=current_terminal)+ directory / "checkpoints",+ saved_draft(scan.scan_id, complete=current_terminal),+ mtime_ns=300,)- os.utime(current, ns=(300, 300))- head.write_text(json.dumps({"checkpoint": current.name}))- os.utime(head, ns=(300, 300))+ select_checkpoint(directory, current, mtime_ns=300)with monkeypatch.context() as patch:if interrupted:patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)with pytest.raises(OSError, match="Synthetic publication interruption"):- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()else:- recover_scan_results(workbench_api, workbench_db, scan)- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ scan.recover()+ replayed = scan.preserve()coverage = read_review_state(workbench_db, scan.scan_dir)expected = [] if current_terminal else ["Accepted"]assert [row["label"] for row in coverage["surfaces"]] == expected@@ -2310,23 +2215,21 @@ def test_terminal_coverage_replacement_does_not_cross_into_a_partial_attempt(@pytest.mark.parametrize("valid_rejection", [False, True], ids=["malformed", "valid"])def test_tied_rejection_requires_valid_surface_before_removing_finding(- workbench_api, workbench_db, publication_scan, valid_rejection+ publication_scan, valid_rejection):scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))os.utime(result, ns=(300, 300))rejected = save_disposition(scan, result.parent, "rejected")if not valid_rejection:rejected["coverage"]["surfaces"][0].pop("label")checkpoint = write_checkpoint(result.parent / "checkpoints", rejected)- (result.parent / "checkpoint-head.json").write_text(- json.dumps({"checkpoint": checkpoint.name})- )+ select_checkpoint(result.parent, checkpoint)os.utime(result.parent / "checkpoint-head.json", ns=(300, 300))original = result.read_bytes()- stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ stopped = scan.fail("Audit stopped.")assert stopped["findingCount"] == (0 if valid_rejection else 1)assert result.read_bytes() == original@@ -2337,11 +2240,11 @@ def test_tied_rejection_requires_valid_surface_before_removing_finding()@pytest.mark.parametrize("head_time", [300, 400], ids=["tied", "newer"])def test_rejection_receipt_is_verified_before_suppressing_accepted_finding(- workbench_api, workbench_db, publication_scan, receipt_state, head_time+ workbench_db, publication_scan, receipt_state, head_time):scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")# Worker receipts are published beneath the parent artifact directory.output = scan.scan_dir / "artifacts" / "workers" / result.parent.nameoutput.parent.mkdir(parents=True, exist_ok=True)@@ -2383,45 +2286,42 @@ def test_rejection_receipt_is_verified_before_suppressing_accepted_finding(receipt.parent.mkdir(exist_ok=True)receipt.write_text("Synthetic rejected candidate review.")checkpoint = write_checkpoint(result.parent / "checkpoints", rejected)- (result.parent / "checkpoint-head.json").write_text(- json.dumps({"checkpoint": checkpoint.name})- )+ select_checkpoint(result.parent, checkpoint)os.utime(result.parent / "checkpoint-head.json", ns=(head_time, head_time))originals = {path: path.read_bytes() for path in (result, result.parent / "checkpoint-head.json")}- stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ stopped = scan.fail("Audit stopped.")unverified = receipt_state in {"missing", "shared-collision"}assert stopped["findingCount"] == int(unverified)coverage = read_review_state(workbench_db, scan.scan_dir)assert ("needs_follow_up" if unverified else "rejected") in {surface["disposition"] for surface in coverage["surfaces"]}- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ replayed = scan.preserve()assert replayed["findingCount"] == stopped["findingCount"]assert all(path.read_bytes() == value for path, value in originals.items())@pytest.mark.parametrize("rejection", [False, True], ids=["finding", "finding-and-rejection"])def test_checkpoint_registered_as_result_keeps_its_own_finding(- workbench_api, workbench_db, publication_scan, rejection+ workbench_db, publication_scan, rejection):scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan)+ result = scan.add_worker()draft = save_disposition(scan, result.parent, "reported")if rejection:draft["coverage"]["surfaces"][0]["disposition"] = "rejected"checkpoint = write_checkpoint(result.parent / "checkpoints", draft)- head = result.parent / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ select_checkpoint(result.parent, checkpoint)with workbench_db:workbench_db.execute("UPDATE deep_scan_workers SET result_manifest_path = ? WHERE id = ?",(str(checkpoint), result.parent.name),)original = checkpoint.read_bytes()- stopped = fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ stopped = scan.fail("Audit stopped.")assert stopped["findingCount"] == 1assert checkpoint.read_bytes() == original@@ -2434,7 +2334,7 @@ def test_cancellation_snapshot_failure_keeps_the_original_source_boundary():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="running")+ result = scan.add_worker(status="running")draft = save_disposition(scan, result.parent, "reported")result.write_text(json.dumps(draft))head = result.parent / "checkpoint-head.json"@@ -2460,20 +2360,17 @@ def test_cancellation_snapshot_failure_keeps_the_original_source_boundary(late["coverage"]["surfaces"][0]["candidateId"] = "post-cancellation-candidate"checkpoint = write_checkpoint(result.parent / "checkpoints", late)if replace_head:- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ select_checkpoint(result.parent, checkpoint)saved = workbench_api["saved_results"]- def replay():- return preserve_scan_results(workbench_api, workbench_db, scan)-if not writable and replace_head:with pytest.raises(saved.ContractError, match="Frozen stopped-scan checkpoint set is incomplete"):- replay()+ scan.preserve()head.write_bytes(original_head)os.utime(head, ns=(original_head_time, original_head_time))- retained = replay()+ retained = scan.preserve()assert retained["findingCount"] == 1findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert all(item["identity"]["anchor"] != "written-after-cancellation" for item in findings)@@ -2491,7 +2388,7 @@ def test_snapshot_write_failure_keeps_stopped_publication_pending():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))if disposition == "rejected":save_disposition(scan, result.parent, disposition)@@ -2510,7 +2407,7 @@ def test_snapshot_write_failure_keeps_stopped_publication_pending(snapshots.chmod(0o500)if os.access(snapshots, os.W_OK):pytest.skip("Current user can write through POSIX directory permissions")- fail_scan(workbench_api, workbench_db, scan, "Audit stopped.")+ scan.fail("Audit stopped.")row = workbench_db.execute("SELECT * FROM scans WHERE id = ?", (scan.scan_id,)).fetchone()assert row["status"] == "failed"if not writable:@@ -2540,7 +2437,7 @@ def test_snapshot_write_failure_keeps_stopped_publication_pending(assert row["seal_manifest_digest"]finally:snapshots.chmod(0o700)- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ replayed = scan.preserve()assert replayed["findingCount"] == (1 if disposition == "reported" else 0)assert all(path.read_bytes() == value for path, value in originals.items())@@ -2557,9 +2454,9 @@ def test_cancellation_captures_each_remaining_workers_current_head():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- first = add_worker(workbench_db, scan, status="running")+ first = scan.add_worker(status="running")first.write_text(json.dumps(save_disposition(scan, first.parent, "reported")))- second = add_worker(workbench_db, scan, status="running")+ second = scan.add_worker(status="running")scan.findings[0]["identity"]["anchor"] = "second-original"second.write_text(json.dumps(save_disposition(scan, second.parent, "reported")))with workbench_db:@@ -2599,7 +2496,7 @@ def test_cancellation_captures_each_remaining_workers_current_head(if advance_after_cancel:scan.findings[0]["identity"]["anchor"] = "second-selected-after-cancel"save_disposition(scan, second.parent, "reported")- retained = preserve_scan_results(workbench_api, workbench_db, scan)+ retained = scan.preserve()assert retained["findingCount"] >= 2anchors = {finding["identity"]["anchor"]@@ -2618,7 +2515,7 @@ def test_cancellation_freezes_sources_before_report_preparation():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="running")+ result = scan.add_worker(status="running")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))report = scan.scan_dir / "report.md"if blocked_report:@@ -2637,7 +2534,7 @@ def test_cancellation_freezes_sources_before_report_preparation(report.rmdir()scan.findings[0]["identity"]["anchor"] = "published-after-cancellation"save_disposition(scan, result.parent, "reported")- retained = preserve_scan_results(workbench_api, workbench_db, scan)+ retained = scan.preserve()assert retained["findingCount"] == 1assert all(finding["identity"]["anchor"] != "published-after-cancellation"@@ -2655,7 +2552,7 @@ def test_legacy_unreadable_head_does_not_hide_another_workers_new_evidence():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))checkpoint = (result.parent@@ -2671,7 +2568,7 @@ def test_legacy_unreadable_head_does_not_hide_another_workers_new_evidence(with monkeypatch.context() as patch:patch.setattr(saved, "_prepare_scan_finalization", legacy_publication)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()with workbench_db:workbench_db.execute("UPDATE scans SET retained_checkpoint_heads_json = NULL WHERE id = ?", (scan.scan_id,)@@ -2688,7 +2585,7 @@ def test_legacy_unreadable_head_does_not_hide_another_workers_new_evidence(if missing_old_checkpoint:checkpoint.unlink()scan.findings[0]["identity"]["anchor"] = "independent-new-worker"- other = add_worker(workbench_db, scan, status="canceled")+ other = scan.add_worker(status="canceled")save_disposition(scan, other.parent, "reported")assert (workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"]["resultsRecoveryNeeded"]@@ -2698,12 +2595,10 @@ def test_legacy_unreadable_head_does_not_hide_another_workers_new_evidence(@pytest.mark.parametrize("head_state", ["malformed", "missing-checkpoint", "missing-head", "valid"])-def test_first_stop_keeps_registered_result_when_head_is_unreadable(- workbench_api, workbench_db, publication_scan, head_state-):+def test_first_stop_keeps_registered_result_when_head_is_unreadable(publication_scan, head_state):scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan)+ result = scan.add_worker()result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))head = result.parent / "checkpoint-head.json"if head_state == "missing-head":@@ -2713,10 +2608,10 @@ def test_first_stop_keeps_registered_result_when_head_is_unreadable("{" if head_state == "malformed" else json.dumps({"checkpoint": "a" * 64 + ".json"}))original = result.read_bytes()- stopped = fail_scan(workbench_api, workbench_db, scan)+ stopped = scan.fail()assert stopped["findingCount"] == 1assert not stopped["resultsRecoveryNeeded"]- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ replayed = scan.preserve()assert replayed["findingCount"] == 1assert result.read_bytes() == original@@ -2727,7 +2622,7 @@ def test_legacy_ordering_failure_does_not_hide_new_worker_evidence():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))saved = workbench_api["saved_results"]prepare = saved._prepare_scan_finalization@@ -2738,7 +2633,7 @@ def test_legacy_ordering_failure_does_not_hide_new_worker_evidence(with monkeypatch.context() as patch:patch.setattr(saved, "_prepare_scan_finalization", legacy_publication)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()with workbench_db:workbench_db.execute("UPDATE scans SET retained_checkpoint_heads_json = NULL WHERE id = ?", (scan.scan_id,)@@ -2757,7 +2652,7 @@ def test_legacy_ordering_failure_does_not_hide_new_worker_evidence(if (scan.scan_dir / name).exists()}scan.findings[0]["identity"]["anchor"] = "independent-new-worker"- other = add_worker(workbench_db, scan, status="canceled")+ other = scan.add_worker(status="canceled")save_disposition(scan, other.parent, "reported")assert workbench_api["scan_context"](workbench_db, scan.scan_id)["scan"]["resultsRecoveryNeeded"@@ -2767,9 +2662,9 @@ def test_legacy_ordering_failure_does_not_hide_new_worker_evidence()if ordering != "valid":with pytest.raises(saved.ContractError):- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()else:- assert recover_scan_results(workbench_api, workbench_db, scan)["findingCount"] == 2+ assert scan.recover()["findingCount"] == 2@pytest.mark.parametrize("canceled", [False, True], ids=["failed", "canceled"])@@ -2778,9 +2673,9 @@ def test_legacy_retained_heads_survive_loss_after_terminal_output():scan = publication_scan()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")result.write_text(json.dumps(save_disposition(scan, result.parent, "reported")))- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()with workbench_db:workbench_db.execute("UPDATE scans SET retained_checkpoint_heads_json = NULL, seal_manifest_digest = NULL, "@@ -2827,7 +2722,7 @@ raise AssertionError("terminal write was not reached")with sqlite3.connect(database) as connection:connection.row_factory = sqlite3.Rowconnection.execute("PRAGMA foreign_keys = ON")- replayed = preserve_scan_results(workbench_api, connection, scan)+ replayed = scan.preserve(connection)assert replayed["findingCount"] == 1assert not replayed["resultsRecoveryNeeded"]assert (diff --git a/plugins/codex-security/tests/test_checkpoint_recovery_fallbacks.py b/plugins/codex-security/tests/test_checkpoint_recovery_fallbacks.pyindex ba1546661..8019f39fc 100644--- a/plugins/codex-security/tests/test_checkpoint_recovery_fallbacks.py+++ b/plugins/codex-security/tests/test_checkpoint_recovery_fallbacks.py@@ -7,18 +7,14 @@ from argparse import Namespacefrom pathlib import Pathimport pytest-from test_deep_scan_successful_publication import (- add_worker,- fail_scan,- preserve_scan_results,- recover_scan_results,-)from test_deep_scan_successful_publication import publication_scan as publication_scanfrom workbench_test_support import (interrupt_publication,read_review_state,saved_draft,+ select_checkpoint,write_checkpoint,+ write_result_draft,)@@ -35,17 +31,11 @@ def test_published_raw_head_compatibility_converts_before_later_selection((scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))for name in ("scan-manifest.json", "coverage.json", "findings.json"):os.utime(scan.scan_dir / name, ns=(0, 0))- directory = (- scan.scan_dir- if owner == "root"- else add_worker(workbench_db, scan, status="canceled").parent- )+ directory = scan.scan_dir if owner == "root" else scan.add_worker(status="canceled").parentinitial = saved_draft(scan.scan_id, complete=True)initial["coverage"] = {**scan.coverage, **initial["coverage"]}checkpoint = write_checkpoint(directory / "checkpoints", initial)- head = directory / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(100, 100))+ head = select_checkpoint(directory, checkpoint, mtime_ns=100)capture = saved._capture_saved_sourcedef legacy_capture(root, relative, scan_id, *, checkpoint_heads=None, **kwargs):@@ -55,7 +45,7 @@ def test_published_raw_head_compatibility_converts_before_later_selection(with monkeypatch.context() as patch:patch.setattr("workbench.retained_coverage._capture_saved_source", legacy_capture)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()original = (scan.scan_dir / "scan-manifest.json").read_bytes()assert (head.relative_to(scan.scan_dir).as_posix()@@ -78,9 +68,8 @@ def test_published_raw_head_compatibility_converts_before_later_selection(Path(path).name == "checkpoint-head.json" for path in normalized["preservedSources"])later = write_checkpoint(directory / "checkpoints", {**initial, "findings": scan.findings})- head.write_text(json.dumps({"checkpoint": later.name}))- os.utime(head, ns=(200, 200))- recovered = recover_scan_results(workbench_api, workbench_db, scan)+ select_checkpoint(directory, later, mtime_ns=200)+ recovered = scan.recover()assert recovered["findingCount"] == 1assert not recovered["resultsRecoveryNeeded"]@@ -92,8 +81,8 @@ def test_early_head_error_keeps_captured_records_during_sibling_capture(saved = workbench_api["saved_results"]context = workbench_api["_WORKBENCH_DB_CONTEXT"](scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- first = add_worker(workbench_db, scan, status="canceled").parent- second = add_worker(workbench_db, scan, status="canceled").parent+ first = scan.add_worker(status="canceled").parent+ second = scan.add_worker(status="canceled").parentwith workbench_db:for directory, created_at in [(first, "2026-01-01T00:00:00Z"),@@ -110,9 +99,7 @@ def test_early_head_error_keeps_captured_records_during_sibling_capture(checkpoint = write_checkpoint(directory / "checkpoints", saved_draft(scan.scan_id, complete=True, findings=[finding]))- head = directory / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(100, 100))+ head = select_checkpoint(directory, checkpoint, mtime_ns=100)if directory == first:original = head.read_bytes()write = saved.write_scan_local_bytes@@ -136,7 +123,7 @@ def test_early_head_error_keeps_captured_records_during_sibling_capture(with monkeypatch.context() as patch:patch.setattr("workbench.saved_sources.write_scan_local_bytes", capture_sibling)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()assert changedargs = Namespace(scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None@@ -154,7 +141,7 @@ def test_early_head_error_keeps_captured_records_during_sibling_capture(assert {row["identity"]["anchor"] for row in findings["findings"]} == {"before", "second"}head.write_bytes(later)os.utime(head, ns=(200, 200))- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()_, findings, _ = saved.finalize_scan(scan.scan_dir)assert "after" in {row["identity"]["anchor"] for row in findings["findings"]}@@ -171,13 +158,12 @@ def test_frozen_model_capture_keeps_earlier_worker_finding(manifest["scan"]["threatModel"] = modelmanifest_path.write_text(json.dumps(manifest))(scan.scan_dir / "findings.json").unlink()- result = add_worker(workbench_db, scan, status="succeeded")+ result = scan.add_worker(status="succeeded")worker_draft = saved_draft(scan.scan_id, findings=scan.findings, complete=True)result.write_text(json.dumps(worker_draft))partial = saved_draft(scan.scan_id, complete=False)checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", partial)- head = scan.scan_dir / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ head = select_checkpoint(scan.scan_dir, checkpoint)for path, timestamp in ((result, 100_000), (manifest_path, 150_000), (head, 200_000)):os.utime(path, ns=(timestamp, timestamp))model_draft = saved._parent_scan_draft(@@ -201,7 +187,7 @@ def test_frozen_model_capture_keeps_earlier_worker_finding(patch.setattr("workbench.retained_coverage.write_scan_local_bytes", interrupt_capture)else:patch.setattr(saved, "_write_prepared_scan_finalization", interrupt)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()if not restore_capture:assert len(initial) == len(initial[0]) == 1retained = json.loads(@@ -212,7 +198,7 @@ def test_frozen_model_capture_keeps_earlier_worker_finding(capture = scan.scan_dir / retained["threatModelSource"]assert retained["threatModelSource"] == model_pathassert capture.exists() is not restore_capture- preserve_scan_results(workbench_api, workbench_db, scan)+ scan.preserve()final_manifest, findings, _ = saved.finalize_scan(scan.scan_dir)assert final_manifest["scan"]["threatModel"] == modelassert len(findings["findings"]) == 1@@ -240,12 +226,11 @@ def test_frozen_model_ignores_unselected_live_manifest_changes(os.utime(scan.scan_dir / name, ns=(150, 150))_, canonical = saved._read_saved_parent_result(scan.scan_dir, scan.scan_id)selected = write_checkpoint(- scan.scan_dir / "checkpoints", {k: v for k, v in canonical.items() if k != "threatModel"}+ scan.scan_dir / "checkpoints",+ {k: v for k, v in canonical.items() if k != "threatModel"},+ mtime_ns=50,)- os.utime(selected, ns=(50, 50))- head = scan.scan_dir / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(200, 200))+ select_checkpoint(scan.scan_dir, selected, mtime_ns=200)write = saved.write_scan_local_bytesdef fail_order(root, relative, contents):@@ -273,7 +258,7 @@ def test_frozen_model_ignores_unselected_live_manifest_changes(if late_model:manifest["scan"]["threatModel"] = {"summary": "Late unselected model"}manifest_path.write_text(json.dumps(manifest))- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ replayed = scan.preserve()published, findings, _ = saved.finalize_scan(scan.scan_dir)assert published["scan"]["threatModel"] == modelassert len(findings["findings"]) == replayed["findingCount"] == 1@@ -304,16 +289,11 @@ def test_resumed_head_snapshot_allows_later_recovery((scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))for name in ("scan-manifest.json", "coverage.json", "findings.json"):os.utime(scan.scan_dir / name, ns=(0, 0))- directory = (- scan.scan_dir- if owner == "root"- else add_worker(workbench_db, scan, status="canceled").parent- )+ directory = scan.scan_dir if owner == "root" else scan.add_worker(status="canceled").parentrelative = directory.relative_to(scan.scan_dir)initial = saved_draft(scan.scan_id, complete=True)initial["coverage"] = {**scan.coverage, **initial["coverage"]}- checkpoint = write_checkpoint(directory / "checkpoints", initial)- os.utime(checkpoint, ns=(50, 50))+ checkpoint = write_checkpoint(directory / "checkpoints", initial, mtime_ns=50)head = directory / "checkpoint-head.json"original_head = json.dumps({"checkpoint": checkpoint.name})head.write_text(original_head)@@ -328,7 +308,7 @@ def test_resumed_head_snapshot_allows_later_recovery(with monkeypatch.context() as patch:if snapshot_failure:patch.setattr("workbench.saved_sources.write_scan_local_bytes", fail_head)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()args = Namespace(scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None)@@ -352,11 +332,11 @@ def test_resumed_head_snapshot_allows_later_recovery("observedAtNs": "100",}assert head.read_text() == original_head- later = write_checkpoint(directory / "checkpoints", {**initial, "findings": scan.findings})- os.utime(later, ns=(50, 50))- head.write_text(json.dumps({"checkpoint": later.name}))- os.utime(head, ns=(200, 200))- result = recover_scan_results(workbench_api, workbench_db, scan)+ later = write_checkpoint(+ directory / "checkpoints", {**initial, "findings": scan.findings}, mtime_ns=50+ )+ select_checkpoint(directory, later, mtime_ns=200)+ result = scan.recover()assert result["findingCount"] == 1assert not result["resultsRecoveryNeeded"]published, findings, _ = saved.finalize_scan(scan.scan_dir)@@ -378,7 +358,6 @@ def test_resumed_head_snapshot_allows_later_recovery(@pytest.mark.parametrize("live_change", ["coverage", "model"])def test_early_head_failure_keeps_frozen_model_choice(workbench_api,- workbench_db,publication_scan,monkeypatch,mode,@@ -396,26 +375,21 @@ def test_early_head_failure_keeps_frozen_model_choice(os.utime(scan.scan_dir / name, ns=(150, 150))draft = saved_draft(scan.scan_id, complete=True, findings=scan.findings)draft["coverage"] = {**scan.coverage, **draft["coverage"]}- head = scan.scan_dir / "checkpoint-head.json"for label, observed in [("Accepted model", 180 if model_choice == "accepted" else 100),("Selected model" if model_choice == "selected" else None, 200),]:payload = {**draft, **({"threatModel": {"summary": label}} if label else {})}- checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", payload)- os.utime(checkpoint, ns=(50, 50))- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))+ checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", payload, mtime_ns=50)+ select_checkpoint(scan.scan_dir, checkpoint, mtime_ns=observed)if label == "Accepted model":saved._capture_saved_source(scan.scan_dir, "checkpoint-head.json", scan.scan_id)if failure == "worker-head":- directory = add_worker(workbench_db, scan, status="canceled").parent+ directory = scan.add_worker(status="canceled").parentworker_checkpoint = write_checkpoint(directory / "checkpoints", saved_draft(scan.scan_id, complete=True))- worker_head = directory / "checkpoint-head.json"- worker_head.write_text(json.dumps({"checkpoint": worker_checkpoint.name}))- os.utime(worker_head, ns=(200, 200))+ select_checkpoint(directory, worker_checkpoint, mtime_ns=200)write = saved.write_scan_local_bytesdef fail_snapshot(root, relative, contents):@@ -434,7 +408,7 @@ def test_early_head_failure_keeps_frozen_model_choice(with monkeypatch.context() as patch:patch.setattr("workbench.saved_sources.write_scan_local_bytes", fail_snapshot)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()if failure != "none":if live_change == "coverage":coverage_path = scan.scan_dir / "coverage.json"@@ -444,7 +418,7 @@ def test_early_head_failure_keeps_frozen_model_choice(else:manifest["scan"]["threatModel"] = {"summary": "Late unselected model"}manifest_path.write_text(json.dumps(manifest))- result = preserve_scan_results(workbench_api, workbench_db, scan)+ result = scan.preserve()published, findings, _ = saved.finalize_scan(scan.scan_dir)expected = {"canonical": "Canonical model",@@ -470,20 +444,14 @@ def test_failed_head_fallback_rejects_changes_before_resume((scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))for name in ("scan-manifest.json", "coverage.json", "findings.json"):os.utime(scan.scan_dir / name, ns=(0, 0))- directory = (- scan.scan_dir- if owner == "root"- else add_worker(workbench_db, scan, status="canceled").parent- )+ directory = scan.scan_dir if owner == "root" else scan.add_worker(status="canceled").parentrelative = directory.relative_to(scan.scan_dir)initial = saved_draft(scan.scan_id, complete=True)initial["coverage"] = {**scan.coverage, **initial["coverage"]}checkpoint = write_checkpoint(directory / "checkpoints", initial)original_checkpoint = checkpoint.read_bytes()os.utime(checkpoint, ns=(50, 50))- head = directory / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(100, 100))+ head = select_checkpoint(directory, checkpoint, mtime_ns=100)original_head = head.read_bytes()original_outputs = {name: (scan.scan_dir / name).read_bytes()@@ -499,11 +467,10 @@ def test_failed_head_fallback_rejects_changes_before_resume(with monkeypatch.context() as patch:patch.setattr("workbench.saved_sources.write_scan_local_bytes", fail_head)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()if change == "head":later = write_checkpoint(directory / "checkpoints", {**initial, "findings": scan.findings})- head.write_text(json.dumps({"checkpoint": later.name}))- os.utime(head, ns=(100, 100))+ select_checkpoint(directory, later, mtime_ns=100)elif change == "timestamp":os.utime(head, ns=(200, 200))else:@@ -562,11 +529,8 @@ def test_rejected_historical_model_does_not_poison_recovery(draft.update(findings=scan.findings, coverage={**scan.coverage, **draft["coverage"]})def select(value, observed):- path = write_checkpoint(scan.scan_dir / "checkpoints", value)- os.utime(path, ns=(50, 50))- head = scan.scan_dir / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": path.name}))- os.utime(head, ns=(observed, observed))+ path = write_checkpoint(scan.scan_dir / "checkpoints", value, mtime_ns=50)+ select_checkpoint(scan.scan_dir, path, mtime_ns=observed)return path.relative_to(scan.scan_dir).as_posix()_, canonical = saved._read_saved_parent_result(scan.scan_dir, scan.scan_id)@@ -574,7 +538,7 @@ def test_rejected_historical_model_does_not_poison_recovery(select({**draft, "threatModel": {"summary": "Older accepted model"}}, 100)saved._capture_saved_source(scan.scan_dir, "checkpoint-head.json", scan.scan_id)invalid_source = select({**draft, "threatModel": invalid}, 200)- stopped = fail_scan(workbench_api, workbench_db, scan)+ stopped = scan.fail()assert stopped["findingCount"] == 1assert any("Skipped malformed saved threat model" in warning for warning in stopped["warnings"])assert json.loads(manifest_path.read_text())["scan"]["threatModel"] == model@@ -598,7 +562,7 @@ def test_rejected_historical_model_does_not_poison_recovery(saved.recover_scan_results(context, workbench_db, args)else:saved.recover_scan_results(context, workbench_db, args)- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ replayed = scan.preserve()published, findings, _ = saved.finalize_scan(scan.scan_dir)assert published["scan"]["threatModel"] == ({"summary": "Replacement model"} if replacement == "valid" else model@@ -650,11 +614,7 @@ def test_partial_head_retains_previous_terminal_coverage_boundary((scan.scan_dir / "coverage.json").write_text(json.dumps(coverage))for name in ("findings.json", "coverage.json", "scan-manifest.json"):os.utime(scan.scan_dir / name, ns=(0, 0))- directory = (- scan.scan_dir- if owner == "root"- else add_worker(workbench_db, scan, status="canceled").parent- )+ directory = scan.scan_dir if owner == "root" else scan.add_worker(status="canceled").parentsaved = workbench_api["saved_results"]for label, observed in [("Obsolete", 100), ("Replacement", 200), ("Partial", partial_time)]:draft = saved_draft(scan.scan_id, complete=label != "Partial")@@ -672,11 +632,8 @@ def test_partial_head_retains_previous_terminal_coverage_boundary(explicitExclusions=[{"pattern": label + "/**", "reason": label}],openQuestions=[{"question": label}],)- checkpoint = write_checkpoint(directory / "checkpoints", draft)- os.utime(checkpoint, ns=(50, 50))- head = directory / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))+ checkpoint = write_checkpoint(directory / "checkpoints", draft, mtime_ns=50)+ head = select_checkpoint(directory, checkpoint, mtime_ns=observed)if label == "Replacement" and registered:with workbench_db:workbench_db.execute(@@ -684,9 +641,9 @@ def test_partial_head_retains_previous_terminal_coverage_boundary((str(checkpoint), directory.name),)if label == "Obsolete":- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()elif label == "Replacement":- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()if repeated_terminal:os.utime(head, ns=(400, 400))saved._capture_saved_source(@@ -697,10 +654,10 @@ def test_partial_head_retains_previous_terminal_coverage_boundary(if interrupted:patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)with pytest.raises(OSError, match="Synthetic publication interruption"):- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()else:- recover_scan_results(workbench_api, workbench_db, scan)- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ scan.recover()+ replayed = scan.preserve()saved.finalize_scan(scan.scan_dir)coverage = read_review_state(workbench_db, scan.scan_dir)expected = {"Replacement"} if partial_time == 300 or registered else {"Obsolete", "Replacement"}@@ -714,7 +671,7 @@ def test_partial_head_retains_previous_terminal_coverage_boundary(@pytest.mark.parametrize("head_time", [100_000, 200_000], ids=["older", "tied"])def test_retry_keeps_canonical_finding_when_its_checkpoint_copy_is_unreadable(- workbench_api, workbench_db, publication_scan, monkeypatch, head_time+ workbench_api, publication_scan, monkeypatch, head_time):scan = publication_scan(mode="standard")saved = workbench_api["saved_results"]@@ -730,9 +687,7 @@ def test_retry_keeps_canonical_finding_when_its_checkpoint_copy_is_unreadable(checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", {**empty, "findings": []})broken = checkpoint.parent / f"{saved._digest(canonical)}.json"broken.write_bytes(b"{")- head = scan.scan_dir / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(head_time, head_time))+ select_checkpoint(scan.scan_dir, checkpoint, mtime_ns=head_time)attempted = []@@ -744,12 +699,12 @@ def test_retry_keeps_canonical_finding_when_its_checkpoint_copy_is_unreadable(with monkeypatch.context() as patch:patch.setattr(saved, "_write_prepared_scan_finalization", interrupt)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()assert attempted == [(scan.findings[0]["title"], model)]with pytest.raises(saved.ContractError, match="Frozen stopped-scan checkpoint set is incomplete"):- preserve_scan_results(workbench_api, workbench_db, scan)+ scan.preserve()assert broken.read_bytes() == b"{"assert "sealedAt" not in json.loads(manifest_path.read_text())["scan"]@@ -780,14 +735,13 @@ def test_inherited_model_capture_is_bound_before_publication_retry(os.utime(scan.scan_dir / name, ns=(150, 150))_, canonical = saved._read_saved_parent_result(scan.scan_dir, scan.scan_id)selected = write_checkpoint(- scan.scan_dir / "checkpoints", {k: v for k, v in canonical.items() if k != "threatModel"}+ scan.scan_dir / "checkpoints",+ {k: v for k, v in canonical.items() if k != "threatModel"},+ mtime_ns=50,)- os.utime(selected, ns=(50, 50))broken = scan.scan_dir / f"checkpoints/{saved._digest(canonical)}.json"broken.write_text("{")- head = scan.scan_dir / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(200, 200))+ select_checkpoint(scan.scan_dir, selected, mtime_ns=200)model_draft = saved._parent_scan_draft(scan.scan_id, {"threatModel": model}, {"findings": []}, {})@@ -804,7 +758,7 @@ def test_inherited_model_capture_is_bound_before_publication_retry(patch.setattr("workbench.retained_coverage.write_scan_local_bytes", fail_capture)else:patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()row = workbench_db.execute("SELECT retained_source_digests_json, completion_warnings_json FROM scans WHERE id = ?",(scan.scan_id,),@@ -862,11 +816,7 @@ def test_restored_terminal_keeps_surfaces_for_surviving_deferred_work((scan.scan_dir / "coverage.json").write_text(json.dumps(coverage))for name in ("findings.json", "coverage.json", "scan-manifest.json"):os.utime(scan.scan_dir / name, ns=(0, 0))- directory = (- scan.scan_dir- if owner == "root"- else add_worker(workbench_db, scan, status="canceled").parent- )+ directory = scan.scan_dir if owner == "root" else scan.add_worker(status="canceled").parentsaved = workbench_api["saved_results"]terminal = saved_draft(scan.scan_id, complete=True)terminal["coverage"] = {**coverage, **terminal["coverage"]}@@ -888,22 +838,19 @@ def test_restored_terminal_keeps_surfaces_for_surviving_deferred_work(sequence.append((closed, 300))sequence.append((terminal, 100))for index, (draft, observed) in enumerate(sequence):- checkpoint = write_checkpoint(directory / "checkpoints", draft)- os.utime(checkpoint, ns=(50, 50))- head = directory / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))+ checkpoint = write_checkpoint(directory / "checkpoints", draft, mtime_ns=50)+ select_checkpoint(directory, checkpoint, mtime_ns=observed)if index == 0:- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()else:with monkeypatch.context() as patch:if interrupted and index == len(sequence) - 1:patch.setattr(saved, "_write_prepared_scan_finalization", interrupt_publication)with pytest.raises(OSError, match="Synthetic publication interruption"):- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()else:- recover_scan_results(workbench_api, workbench_db, scan)- replayed = preserve_scan_results(workbench_api, workbench_db, scan)+ scan.recover()+ replayed = scan.preserve()saved.finalize_scan(scan.scan_dir)coverage = read_review_state(workbench_db, scan.scan_dir)if owner == "worker":@@ -918,7 +865,7 @@ def test_restored_terminal_keeps_surfaces_for_surviving_deferred_work(def test_source_order_failure_retains_accepted_parent_input(- workbench_api, workbench_db, publication_scan, monkeypatch+ workbench_api, publication_scan, monkeypatch):from workbench import saved_sources@@ -965,18 +912,16 @@ def test_source_order_failure_retains_accepted_parent_input(with monkeypatch.context() as patch:patch.setattr(saved_sources, "write_scan_local_bytes", fail_order)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()assert "sealedAt" not in json.loads(manifest.read_text())["scan"]head.write_text(json.dumps({"checkpoint": abandoned.name, "inputCheckpoint": abandoned.name}))os.utime(head, ns=(400, 400))- preserve_scan_results(workbench_api, workbench_db, scan)+ scan.preserve()_, findings, _ = saved.finalize_scan(scan_dir)assert [finding["title"] for finding in findings["findings"]] == [scan.findings[0]["title"]]-def test_headless_canonical_snapshot_failure(- workbench_api, workbench_db, publication_scan, monkeypatch-):+def test_headless_canonical_snapshot_failure(workbench_api, publication_scan, monkeypatch):from workbench import saved_sourcesscan = publication_scan()@@ -993,7 +938,7 @@ def test_headless_canonical_snapshot_failure(with monkeypatch.context() as patch:patch.setattr(saved_sources, "write_scan_local_bytes", fail_snapshot)- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()assert not head.exists()assert "sealedAt" not in json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]raw = write_checkpoint(@@ -1005,7 +950,7 @@ def test_headless_canonical_snapshot_failure()head.write_text(json.dumps({"checkpoint": raw.name, "inputCheckpoint": raw.name}))(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))- preserve_scan_results(workbench_api, workbench_db, scan)+ scan.preserve()manifest, findings, _ = saved.finalize_scan(scan.scan_dir)assert [f["title"] for f in findings["findings"]] == [original]assert "." not in manifest["scan"]["preservedCheckpointHeads"]@@ -1034,12 +979,10 @@ def test_interrupted_recovery_keeps_frozen_parent_reconciliation("UPDATE scans SET status = 'failed', completed_at = started_at WHERE id = ?",(scan.scan_id,),)- worker = add_worker(workbench_db, scan, status="canceled")+ worker = scan.add_worker(status="canceled")draft = saved_draft(scan.scan_id, complete=True)- worker.write_text(json.dumps({**draft, "findings": scan.findings}))- os.utime(worker, ns=(100_000, 100_000))- checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", draft)- os.utime(checkpoint, ns=(250_000, 250_000))+ write_result_draft(worker, {**draft, "findings": scan.findings}, mtime_ns=100_000)+ write_checkpoint(scan.scan_dir / "checkpoints", draft, mtime_ns=250_000)manifest = scan.scan_dir / "scan-manifest.json"contents = json.loads(manifest.read_text())contents["scan"]["complete"] = Truediff --git a/plugins/codex-security/tests/test_deep_coverage_recovery.py b/plugins/codex-security/tests/test_deep_coverage_recovery.pyindex 068b2a6d2..016eb0761 100644--- a/plugins/codex-security/tests/test_deep_coverage_recovery.py+++ b/plugins/codex-security/tests/test_deep_coverage_recovery.py@@ -13,8 +13,10 @@ from workbench_test_support import (saved_binding,saved_discovery_worker,saved_draft,+ select_checkpoint,write_checkpoint,write_completed_contract,+ write_result_draft,)sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))@@ -26,9 +28,7 @@ def worker(root: Path, name: str, draft: dict):output = root / "artifacts" / "deep_discovery" / "workers" / name / "output"(output / "artifacts").mkdir(parents=True)(output / "artifacts/review.md").write_text(f"{name} source review\n")- result = output / "result.json"- result.write_text(json.dumps(draft))- os.utime(result, ns=(100, 100))+ write_result_draft(output / "result.json", draft, mtime_ns=100)return saved_discovery_worker(output, name, 1)@@ -104,7 +104,6 @@ def test_partial_root_rejections_close_worker_local_candidates_independently(tmp)workers.append(source)- head = tmp_path / "checkpoint-head.json"for name, observed, pending in [("first", 200, ["Review second."]), ("second", 300, [])]:rejection = {"id": name,@@ -116,8 +115,7 @@ def test_partial_root_rejections_close_worker_local_candidates_independently(tmpcheckpoint = write_checkpoint(tmp_path / "checkpoints", saved_draft("coverage-scan", surfaces=[rejection]))- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))+ select_checkpoint(tmp_path, checkpoint, mtime_ns=observed)documents = recover(tmp_path, workers, project=True)assert [row["reason"] for row in documents[2]["deferred"] if row["id"] != "scan-stopped"@@ -196,11 +194,8 @@ def test_partial_parent_without_arrays_retains_closed_worker_surface_and_frozen_source = worker(tmp_path, "first", closed)output = Path(source["artifact_dir"])for modified, draft in [(100, pending), (200, closed)]:- checkpoint = write_checkpoint(output / "checkpoints", draft)- os.utime(checkpoint, ns=(modified, modified))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(200, 200))+ checkpoint = write_checkpoint(output / "checkpoints", draft, mtime_ns=modified)+ select_checkpoint(output, checkpoint, mtime_ns=200)os.utime(output / "result.json", ns=(200, 200))parent = saved_draft("coverage-scan")parent["coverage"] = {@@ -233,11 +228,9 @@ def test_finding_only_reducer_preserves_earlier_partial_root_coverage(tmp_path):question = {"question": "Which routes are public?"}earlier = saved_draft(scan_id, surfaces=[surface])earlier["coverage"]["openQuestions"] = [question]- head = tmp_path / "checkpoint-head.json"for observed, draft in [(100_000, earlier), (200_000, saved_draft(scan_id))]:checkpoint = write_checkpoint(tmp_path / "checkpoints", draft)- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))+ select_checkpoint(tmp_path, checkpoint, mtime_ns=observed)saved._capture_saved_source(tmp_path, "checkpoint-head.json", scan_id)finding_path = (@@ -337,11 +330,8 @@ def test_aggregate_copies_reconcile_once_and_later_worker_closure_removes_pendinsurfaces=[{**surface, "disposition": "no_issue_found"}],closures=[{"id": "review", "reason": "Review completed."}],)- checkpoint = write_checkpoint(output / "checkpoints", closed)- os.utime(checkpoint, ns=(300, 300))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(400, 400))+ checkpoint = write_checkpoint(output / "checkpoints", closed, mtime_ns=300)+ select_checkpoint(output, checkpoint, mtime_ns=400)originals = {path: path.read_bytes() for path in tmp_path.rglob("*.json")}documents = recover(tmp_path, [source])replay = recover(tmp_path, [source], frozen=documents[0]["scan"]["preservedSources"])@@ -383,8 +373,7 @@ def test_parent_surface_fallback_uses_current_snapshot(older["coverage"]["surfaces"][0].update(label=old_label, receiptRefs=["artifacts/old.md"])for name, draft, modified in [("old", older, 50), ("new", current, 100)]:(output / "artifacts" / f"{name}.md").write_text(f"{name} evidence\n")- checkpoint = write_checkpoint(output / "checkpoints", draft)- os.utime(checkpoint, ns=(modified, modified))+ write_checkpoint(output / "checkpoints", draft, mtime_ns=modified)parent = copy.deepcopy(draft)parent["coverage"]["surfaces"][0].update(id="first/api",@@ -394,12 +383,9 @@ def test_parent_surface_fallback_uses_current_snapshot(parent["coverage"]["deferred"] = [{"id": "parent-review", "reason": "Review remains.", "surfaceIds": ["first/api"]}]- parent_path = write_checkpoint(tmp_path / "checkpoints", parent)- os.utime(parent_path, ns=(modified + 100, modified + 100))+ parent_path = write_checkpoint(tmp_path / "checkpoints", parent, mtime_ns=modified + 100)if selected_parent:- head = tmp_path / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": parent_path.name}))- os.utime(head, ns=(250, 250))+ select_checkpoint(tmp_path, parent_path, mtime_ns=250)else:write_saved_parent(tmp_path, parent, 200)template = tmp_path / "template"@@ -410,11 +396,9 @@ def test_parent_surface_fallback_uses_current_snapshot(selected = write_checkpoint(output / "checkpoints",saved_draft("coverage-scan", complete=complete_worker, findings=[finding]),+ mtime_ns=300,)- os.utime(selected, ns=(300, 300))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(400, 400))+ select_checkpoint(output, selected, mtime_ns=400)originals = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}result = recover(tmp_path, [source])@@ -462,8 +446,7 @@ def test_fallback_receipts_take_precedence_over_historical_repairs(tmp_path, mod{**writer, "receiptRefs": [f"artifacts/{historical_name}", "artifacts/missing.md"]}],)- checkpoint = write_checkpoint(output / "checkpoints", older)- os.utime(checkpoint, ns=(50, 50))+ write_checkpoint(output / "checkpoints", older, mtime_ns=50)parent = copy.deepcopy(draft)for row in parent["coverage"]["surfaces"]:row.update(@@ -483,11 +466,9 @@ def test_fallback_receipts_take_precedence_over_historical_repairs(tmp_path, modselected = write_checkpoint(output / "checkpoints",saved_draft("coverage-scan", complete=True, surfaces=selected_surfaces),+ mtime_ns=300,)- os.utime(selected, ns=(300, 300))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(400, 400))+ select_checkpoint(output, selected, mtime_ns=400)originals = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}warnings = []result = recover(tmp_path, [source], warnings=warnings)@@ -526,15 +507,13 @@ def test_parent_only_task_keeps_link_to_recovered_anonymous_surface(if earlier_identity is not None:older = copy.deepcopy(draft)older["coverage"]["surfaces"][-1]["id"] = earlier_identity- checkpoint = write_checkpoint(Path(source["artifact_dir"]) / "checkpoints", older)- os.utime(checkpoint, ns=(50, 50))+ checkpoint = write_checkpoint(+ Path(source["artifact_dir"]) / "checkpoints", older, mtime_ns=50+ )if selected_checkpoint:output = Path(source["artifact_dir"])- checkpoint = write_checkpoint(output / "checkpoints", draft)- os.utime(checkpoint, ns=(300, 300))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(400, 400))+ checkpoint = write_checkpoint(output / "checkpoints", draft, mtime_ns=300)+ select_checkpoint(output, checkpoint, mtime_ns=400)parent = copy.deepcopy(draft)parent["coverage"]["surfaces"][-1].update(id=published_id, workerId="first")if colliding_candidate:@@ -590,11 +569,8 @@ def test_parent_only_task_keeps_link_to_explicit_worker_surface("label"] = expected_labeloutput = Path(source["artifact_dir"])- selected_path = write_checkpoint(output / "checkpoints", selected)- os.utime(selected_path, ns=(300, 300))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected_path.name}))- os.utime(head, ns=(400, 400))+ selected_path = write_checkpoint(output / "checkpoints", selected, mtime_ns=300)+ select_checkpoint(output, selected_path, mtime_ns=400)originals = {path: path.read_bytes() for path in tmp_path.rglob("*.json")}result = recover(tmp_path, [source])@@ -683,11 +659,9 @@ def test_parent_task_retains_surface_omitted_by_selected_worker_checkpoint(selected = write_checkpoint(output / "checkpoints",saved_draft("coverage-scan", complete=True, deferred=selected_tasks),+ mtime_ns=300,)- os.utime(selected, ns=(300, 300))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(400, 400))+ select_checkpoint(output, selected, mtime_ns=400)originals = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}result = recover(tmp_path, [source])@@ -719,15 +693,11 @@ def test_parent_link_follows_updated_terminal_checkpoint(tmp_path, colliding_paroutput = Path(source["artifact_dir"])older = copy.deepcopy(draft)older["coverage"]["surfaces"][0]["id"] = "restored"- checkpoint = write_checkpoint(output / "checkpoints", older)- os.utime(checkpoint, ns=(50, 50))+ checkpoint = write_checkpoint(output / "checkpoints", older, mtime_ns=50)selected = copy.deepcopy(older)selected["coverage"]["surfaces"][0]["notes"] = "Updated review."- checkpoint = write_checkpoint(output / "checkpoints", selected)- os.utime(checkpoint, ns=(300, 300))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(400, 400))+ checkpoint = write_checkpoint(output / "checkpoints", selected, mtime_ns=300)+ select_checkpoint(output, checkpoint, mtime_ns=400)parent = copy.deepcopy(draft)parent["coverage"]["surfaces"][0].update(id="first/0", workerId="first")parent["coverage"]["deferred"] = [@@ -775,16 +745,14 @@ def test_parent_link_recovery_leaves_malformed_collection_for_existing_finalizerparent["coverage"]["deferred"] = [{"id": "parent-review", "reason": "Review remains.", "surfaceIds": ["first/api"]}]- checkpoint = write_checkpoint(tmp_path / "checkpoints", parent)- os.utime(checkpoint, ns=(150, 150))+ write_checkpoint(tmp_path / "checkpoints", parent, mtime_ns=150)parent["coverage"]["surfaces"] = {"malformed": True}write_saved_parent(tmp_path, parent, 200)output = Path(source["artifact_dir"])- selected = write_checkpoint(output / "checkpoints", saved_draft("coverage-scan", complete=True))- os.utime(selected, ns=(300, 300))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(400, 400))+ selected = write_checkpoint(+ output / "checkpoints", saved_draft("coverage-scan", complete=True), mtime_ns=300+ )+ select_checkpoint(output, selected, mtime_ns=400)result = saved.merge_saved_results(tmp_path,@@ -847,8 +815,7 @@ def test_parent_link_follows_replaced_anonymous_surface(tmp_path, repair_only):output = Path(source["artifact_dir"])older = copy.deepcopy(draft)older["coverage"]["surfaces"][0]["id"] = "restored"- checkpoint = write_checkpoint(output / "checkpoints", older)- os.utime(checkpoint, ns=(50, 50))+ checkpoint = write_checkpoint(output / "checkpoints", older, mtime_ns=50)parent = copy.deepcopy(draft)parent["coverage"]["surfaces"][0].update(id="first/0",@@ -874,11 +841,8 @@ def test_parent_link_follows_replaced_anonymous_surface(tmp_path, repair_only):],closures=[] if repair_only else [{"id": "review", "reason": "Worker review completed."}],)- checkpoint = write_checkpoint(output / "checkpoints", replacement)- os.utime(checkpoint, ns=(300, 300))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(400, 400))+ checkpoint = write_checkpoint(output / "checkpoints", replacement, mtime_ns=300)+ select_checkpoint(output, checkpoint, mtime_ns=400)originals = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}frozen = Nonefor _ in range(2):@@ -993,11 +957,9 @@ def test_distinct_anonymous_checkpoint_tasks_survive_reconciliation_before_projecheckpoint = write_checkpoint(output / "checkpoints",saved_draft("coverage-scan", deferred=[{"reason": reason}], complete=False),+ mtime_ns=index * 100,)- os.utime(checkpoint, ns=(index * 100, index * 100))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(250, 250))+ select_checkpoint(output, checkpoint, mtime_ns=250)source = saved_discovery_worker(output, "first", 1)result = recover(tmp_path, [source], project=True)replay = recover(tmp_path, [source], project=True, frozen=result[0]["scan"]["preservedSources"])@@ -1122,9 +1084,7 @@ def test_anonymous_worker_receipt_precedes_another_workers_rejection(tmp_path, echeckpoint = write_checkpoint(output / "checkpoints", saved_draft("coverage-scan", complete=True, surfaces=[rejection]))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(200, 200))+ select_checkpoint(output, checkpoint, mtime_ns=200)_, findings, coverage = recover(tmp_path, [first, second], project=True, warnings=[])collision = earlier_ref != ref@@ -1348,8 +1308,7 @@ def test_missing_historical_receipt_does_not_hide_valid_case_variant(tmp_path, rcomplete=True,surfaces=[{**surface, "receiptRefs": ["artifacts/replacement.md"]}],)- checkpoint = write_checkpoint(output / "checkpoints", current)- os.utime(checkpoint, ns=(300, 300))+ write_checkpoint(output / "checkpoints", current, mtime_ns=300)warnings = []documents = saved.merge_saved_results(tmp_path,@@ -1411,8 +1370,7 @@ def test_superseded_surface_cannot_reserve_historical_receipt(tmp_path, projecte],closures=[{"id": "review-reader", "reason": "Reader review complete."}],)- (output / "result.json").write_text(json.dumps(current))- os.utime(output / "result.json", ns=(300, 300))+ write_result_draft(output / "result.json", current, mtime_ns=300)prefix = archive.relative_to(tmp_path).as_posix() + "/"if projected_parent:parent = copy.deepcopy(old)@@ -1487,17 +1445,19 @@ def test_receipt_repair_preserves_selected_surface_metadata(tmp_path, scenario,),]:checkpoint = write_checkpoint(- output / "checkpoints", saved_draft("coverage-scan", surfaces=[row])+ output / "checkpoints",+ saved_draft("coverage-scan", surfaces=[row]),+ mtime_ns=modified,)- os.utime(checkpoint, ns=(modified, modified))missing_path = output / "artifacts/missing.md"else:archive = output.parent / "attempts/attempt-01"(archive / "artifacts").mkdir(parents=True)- (archive / "result.json").write_text(- json.dumps(saved_draft("coverage-scan", complete=True, surfaces=[surface]))+ write_result_draft(+ archive / "result.json",+ saved_draft("coverage-scan", complete=True, surfaces=[surface]),+ mtime_ns=100,)- os.utime(archive / "result.json", ns=(100, 100))missing_path = archive / "artifacts/missing.md"accepted = saved_draft("coverage-scan",@@ -1511,10 +1471,8 @@ def test_receipt_repair_preserves_selected_surface_metadata(tmp_path, scenario,}],)- checkpoint = write_checkpoint(output / "checkpoints", accepted)- os.utime(checkpoint, ns=(200, 200))- (output / "checkpoint-head.json").write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(output / "checkpoint-head.json", ns=(250, 250))+ checkpoint = write_checkpoint(output / "checkpoints", accepted, mtime_ns=200)+ select_checkpoint(output, checkpoint, mtime_ns=250)(output / "result.json").write_text(json.dumps(accepted))pending = saved_draft("coverage-scan",@@ -1527,8 +1485,7 @@ def test_receipt_repair_preserves_selected_surface_metadata(tmp_path, scenario,}],)- checkpoint = write_checkpoint(output / "checkpoints", pending)- os.utime(checkpoint, ns=(400, 400))+ checkpoint = write_checkpoint(output / "checkpoints", pending, mtime_ns=400)source["attempt"] = 2os.utime(output / "result.json", ns=(300, 300))if not missing:@@ -1575,30 +1532,28 @@ def test_receipt_repair_does_not_promote_unselected_checkpoint_evidence(output = Path(source["artifact_dir"])if accepted_receipt_exists:(output / "artifacts/accepted.md").write_text("Accepted receipt.\n")- historical = write_checkpoint(+ write_checkpoint(output / "checkpoints",saved_draft("coverage-scan",complete=True,surfaces=[{**surface, "receiptRefs": ["artifacts/missing.md"]}],),+ mtime_ns=100,)- os.utime(historical, ns=(100, 100))- selected = write_checkpoint(output / "checkpoints", accepted)- os.utime(selected, ns=(200, 200))- (output / "checkpoint-head.json").write_text(json.dumps({"checkpoint": selected.name}))- os.utime(output / "checkpoint-head.json", ns=(250, 250))+ selected = write_checkpoint(output / "checkpoints", accepted, mtime_ns=200)+ select_checkpoint(output, selected, mtime_ns=250)os.utime(output / "result.json", ns=(300, 300))(output / "artifacts/unselected.md").write_text("Unselected interrupted observation.\n")- pending = write_checkpoint(+ write_checkpoint(output / "checkpoints",saved_draft("coverage-scan",complete=False,surfaces=[{**surface, "receiptRefs": ["artifacts/unselected.md"]}],),+ mtime_ns=400,)- os.utime(pending, ns=(400, 400))originals = {path: path.read_bytes() for path in output.rglob("*") if path.is_file()}frozen = Nonefor _ in range(2):@@ -1659,10 +1614,8 @@ def test_unselected_receipt_spelling_cannot_replace_accepted_evidence(("distinct.md", "Independent receipt bytes."),):(output / "artifacts" / name).write_text(text)- selected = write_checkpoint(output / "checkpoints", accepted)- os.utime(selected, ns=(200, 200))- (output / "checkpoint-head.json").write_text(json.dumps({"checkpoint": selected.name}))- os.utime(output / "checkpoint-head.json", ns=(250, 250))+ selected = write_checkpoint(output / "checkpoints", accepted, mtime_ns=200)+ select_checkpoint(output, selected, mtime_ns=250)os.utime(output / "result.json", ns=(300, 300))if checkpoint != "absent":name = {@@ -1670,14 +1623,14 @@ def test_unselected_receipt_spelling_cannot_replace_accepted_evidence("conflicting": unselected_name,"distinct": "distinct.md",}[checkpoint]- pending = write_checkpoint(+ write_checkpoint(output / "checkpoints",saved_draft("coverage-scan",surfaces=[{**surface, "receiptRefs": [f"artifacts/{name}"]}],),+ mtime_ns=400,)- os.utime(pending, ns=(400, 400))originals = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}documents = recover(tmp_path, [source], project=True)replay = recover(@@ -1720,17 +1673,15 @@ def test_non_emitted_repair_cannot_reserve_another_surfaces_receipt(tmp_path, un"coverage-scan", complete=True, surfaces=[surface("writer", ["Review.md", "missing.md"])])for modified, draft in [(100, history), (200, accepted)]:- checkpoint = write_checkpoint(output / "checkpoints", draft)- os.utime(checkpoint, ns=(modified, modified))- (output / "checkpoint-head.json").write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(output / "checkpoint-head.json", ns=(250, 250))+ checkpoint = write_checkpoint(output / "checkpoints", draft, mtime_ns=modified)+ select_checkpoint(output, checkpoint, mtime_ns=250)os.utime(output / "result.json", ns=(300, 300))if unselected:checkpoint = write_checkpoint(output / "checkpoints",saved_draft("coverage-scan", surfaces=[surface("reader", ["review.md"])]),+ mtime_ns=400,)- os.utime(checkpoint, ns=(400, 400))originals = {path: path.read_bytes() for path in tmp_path.rglob("*") if path.is_file()}warnings = []documents = saved.merge_saved_results(@@ -1823,10 +1774,8 @@ def test_only_recoverable_surface_metadata_reserves_receipt_spelling(tmp_path, r],)for modified, draft in [(100, history), (200, accepted)]:- checkpoint = write_checkpoint(output / "checkpoints", draft)- os.utime(checkpoint, ns=(modified, modified))- (output / "checkpoint-head.json").write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(output / "checkpoint-head.json", ns=(250, 250))+ checkpoint = write_checkpoint(output / "checkpoints", draft, mtime_ns=modified)+ select_checkpoint(output, checkpoint, mtime_ns=250)os.utime(output / "result.json", ns=(300, 300))originals = {p: p.read_bytes() for p in output.rglob("*") if p.is_file()}warnings = []@@ -1883,3 +1832,32 @@ def test_only_recoverable_surface_metadata_reserves_receipt_spelling(tmp_path, r)assert replay[2] == documents[2]assert all(path.read_bytes() == content for path, content in originals.items())+++def test_reducer_relinks_its_deferred_task_when_reusing_a_worker_result(tmp_path):+ upstream = {"id": "route", "label": "Upstream", "disposition": "no_issue_found"}+ previous = {**upstream, "id": "upstream/route", "workerId": "upstream"}+ published = {**previous, "id": "local/upstream/route", "workerId": "local"}+ task = {"id": "review", "reason": "Pending review", "surfaceIds": [published["id"]]}+ first = worker(tmp_path, "upstream", saved_draft("coverage-scan", surfaces=[upstream]))+ local = worker(+ tmp_path,+ "local",+ saved_draft(+ "coverage-scan", complete=True, surfaces=[previous, published], deferred=[task]+ ),+ )+ result = str(Path(local["artifact_dir"]) / "result.json")+ local.update(status="succeeded", result_manifest_path=result)+ reducer = worker(tmp_path, "reducer", saved_draft("coverage-scan"))+ reducer.update(kind="dedup", status="succeeded", completed_at="", result_manifest_path=result)+ workers = [first, local, reducer]+ docs = recover(tmp_path, workers, project=True)+ replay = recover(tmp_path, workers, project=True, frozen=docs[0]["scan"]["preservedSources"])+ for coverage in (docs[2], replay[2]):+ reducer_task = next(row for row in coverage["deferred"] if row["id"] == task["id"])+ (surface_id,) = reducer_task["surfaceIds"]+ surface = next(row for row in coverage["surfaces"] if row["id"] == surface_id)+ assert surface["workerId"] == previous["workerId"]+ assert surface["provenance"]["workerId"] == local["id"]+ assert surface["provenance"]["sourceId"] == previous["id"]diff --git a/plugins/codex-security/tests/test_deep_deadline_recovery_edges.py b/plugins/codex-security/tests/test_deep_deadline_recovery_edges.pyindex 3f8587a91..d6a1e1249 100644--- a/plugins/codex-security/tests/test_deep_deadline_recovery_edges.py+++ b/plugins/codex-security/tests/test_deep_deadline_recovery_edges.py@@ -21,6 +21,7 @@ from workbench_test_support import (read_review_state,run_workbench,saved_draft,+ select_checkpoint,worker_paths,write_checkpoint,)@@ -71,7 +72,7 @@ def deadline_fixture()draft["threatModel"] = {"summary": "Synthetic worker model."}checkpoint = write_checkpoint(output / "checkpoints", draft)- (output / "checkpoint-head.json").write_text(json.dumps({"checkpoint": checkpoint.name}))+ select_checkpoint(output, checkpoint)run_workbench(state, *args, "--status", "canceled", environment=environment)artifacts = directory / "artifacts/02_discovery"artifacts.mkdir(parents=True, exist_ok=True)diff --git a/plugins/codex-security/tests/test_deep_deadline_rollback.py b/plugins/codex-security/tests/test_deep_deadline_rollback.pyindex aa2ca8f2a..3d1caf1a4 100644--- a/plugins/codex-security/tests/test_deep_deadline_rollback.py+++ b/plugins/codex-security/tests/test_deep_deadline_rollback.py@@ -11,7 +11,7 @@ from types import SimpleNamespaceimport pytestfrom test_deep_coverage_recovery import workerfrom test_workbench_standard_deep_results import write_saved_parent-from workbench_test_support import saved_binding, saved_draft, write_checkpoint+from workbench_test_support import saved_binding, saved_draft, select_checkpoint, write_checkpointsys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))import finalize_scan_contract as finalizer@@ -94,8 +94,7 @@ def test_deadline_rollback_does_not_reintroduce_a_later_rejected_worker_finding(worker_bytes = worker_checkpoint.read_bytes()previous = write_checkpoint(tmp_path / "checkpoints", saved_draft(scan_id))previous_bytes = previous.read_bytes()- head = tmp_path / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": previous.name}))+ head = select_checkpoint(tmp_path, previous)original_head = head.read_bytes()binding, db, scan = publication_context(tmp_path, scan_id)@@ -161,11 +160,8 @@ def test_deadline_rollback_preserves_parent_observation_order(older, newer = (pending, closed) if newer_source == "canonical" else (closed, pending)older["threatModel"] = {"summary": "The earlier model."}newer["threatModel"] = {"summary": "The current model."}- selected = write_checkpoint(tmp_path / "checkpoints", older)- os.utime(selected, ns=(100, 100))- head = tmp_path / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(100, 100))+ selected = write_checkpoint(tmp_path / "checkpoints", older, mtime_ns=100)+ head = select_checkpoint(tmp_path, selected, mtime_ns=100)canonical = newer if newer_source == "canonical" else oldermodified = 200 if newer_source == "canonical" else 100write_saved_parent(tmp_path, canonical, modified)@@ -176,8 +172,7 @@ def test_deadline_rollback_preserves_parent_observation_order(for name in ("scan-manifest.json", "findings.json", "coverage.json"):os.utime(tmp_path / name, ns=(modified, modified))if newer_source == "unselected-checkpoint":- unselected = write_checkpoint(tmp_path / "checkpoints", newer)- os.utime(unselected, ns=(200, 200))+ write_checkpoint(tmp_path / "checkpoints", newer, mtime_ns=200)binding, db, scan = publication_context(tmp_path, scan_id)class Connection:diff --git a/plugins/codex-security/tests/test_deep_scan_deadline_contracts.py b/plugins/codex-security/tests/test_deep_scan_deadline_contracts.pyindex d2d2aef82..831ddd1a9 100644--- a/plugins/codex-security/tests/test_deep_scan_deadline_contracts.py+++ b/plugins/codex-security/tests/test_deep_scan_deadline_contracts.py@@ -6,11 +6,16 @@ import osimport pytestfrom test_deep_scan_deadline_recovery import worker_finding-from test_deep_scan_stop_conditions import canceled_discovery_output, complete, finish+from test_deep_scan_stop_conditions import canceled_discovery_output, finishfrom test_deep_scan_stop_conditions import deadline_scan as deadline_scanfrom test_deep_scan_stop_conditions import publication_scan as publication_scanfrom test_deep_scan_stop_conditions import saturated_scan as saturated_scan-from workbench_test_support import read_review_state, saved_draft, write_checkpoint+from workbench_test_support import (+ read_review_state,+ saved_draft,+ select_checkpoint,+ write_checkpoint,+)@pytest.mark.parametrize("source", ["result", "checkpoint", "archived-checkpoint"])@@ -43,8 +48,8 @@ def test_deadline_requires_boolean_completion_before_superseding_worker_history(findings=[earlier, rejected],deferred=[{"id": "review", "reason": "Review remains."}],),+ mtime_ns=100,)- os.utime(checkpoint, ns=(100, 100))sibling = worker_finding(scan, "latest-valid-sibling")latest = saved_draft(scan.scan_id,@@ -68,14 +73,12 @@ def test_deadline_requires_boolean_completion_before_superseding_worker_history(result.write_text(json.dumps(latest))else:result = write_checkpoint(output / "checkpoints", latest)- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": result.name}))- os.utime(head, ns=(300, 300))+ select_checkpoint(output, result, mtime_ns=300)os.utime(result, ns=(200, 200))original = {path: path.read_bytes() for path in (checkpoint, result)}finish(workbench_api, workbench_db, scan, terminal_reason="capped")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()terminal = marker is True or marker == "omitted"invalid = not isinstance(marker, bool) and marker != "omitted"@@ -131,8 +134,8 @@ def test_deadline_validates_each_recovered_finding_against_the_completed_contrac{"id": "finished", "reason": "Separate review remains."},],),+ mtime_ns=100,)- os.utime(checkpoint, ns=(100, 100))sibling = worker_finding(scan, "valid-worker-sibling")malformed = worker_finding(scan, "malformed-worker-finding")malformed["provenance"]["candidateId"] = "malformed-worker-finding"@@ -166,24 +169,19 @@ def test_deadline_validates_each_recovered_finding_against_the_completed_contrac)if source == "checkpoint":result = write_checkpoint(output / "checkpoints", latest)- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": result.name}))- os.utime(head, ns=(300, 300))+ select_checkpoint(output, result, mtime_ns=300)else:result = output / "result.json"result.write_text(json.dumps(latest))os.utime(result, ns=(200, 200))original = {path: path.read_bytes() for path in (checkpoint, result)}if source == "archived-result":- selected = write_checkpoint(output / "checkpoints", latest)- os.utime(selected, ns=(200, 200))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(300, 300))+ selected = write_checkpoint(output / "checkpoints", latest, mtime_ns=200)+ select_checkpoint(output, selected, mtime_ns=300)original[selected] = selected.read_bytes()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert {finding["title"] for finding in findings} == {@@ -228,7 +226,7 @@ def test_deadline_recovers_optional_worker_model_without_losing_findings(original = checkpoint.read_bytes()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert len(json.loads((scan.scan_dir / "findings.json").read_text())["findings"]) == 2manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())["scan"]@@ -302,7 +300,7 @@ def test_deadline_applies_existing_nonblank_surface_label_contract(original = checkpoint.read_bytes()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert len(json.loads((scan.scan_dir / "findings.json").read_text())["findings"]) == 2coverage = read_review_state(workbench_db, scan.scan_dir)@@ -345,19 +343,18 @@ def test_deadline_applies_existing_unicode_contract_per_worker_record(draft["coverage"][field] = [{"id": "review", "reason": text}]if field == "resolvedDeferred":draft["complete"] = True- previous = write_checkpoint(+ write_checkpoint(output / "checkpoints",saved_draft(scan.scan_id, deferred=[{"id": "review", "reason": "Review pending."}]),+ mtime_ns=100,)- os.utime(previous, ns=(100, 100))- checkpoint = write_checkpoint(output / "checkpoints", draft)- os.utime(checkpoint, ns=(200, 200))+ checkpoint = write_checkpoint(output / "checkpoints", draft, mtime_ns=200)if field == "resolvedDeferred":- (output / "checkpoint-head.json").write_text(json.dumps({"checkpoint": checkpoint.name}))+ select_checkpoint(output, checkpoint)original = checkpoint.read_bytes()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert len(findings) == (1 if malformed and field == "finding" else 2)@@ -391,8 +388,8 @@ def test_deadline_recovers_historical_closure_text_without_losing_a_valid_siblin{"id": "sibling", "reason": "Valid sibling review pending."},],),+ mtime_ns=100,)- os.utime(previous, ns=(100, 100))draft = saved_draft(scan.scan_id,complete=True,@@ -404,14 +401,13 @@ def test_deadline_recovers_historical_closure_text_without_losing_a_valid_siblincheckpoint = write_checkpoint(output / "checkpoints", draft)result = output / "result.json"result.write_text(json.dumps(draft))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ head = select_checkpoint(output, checkpoint)for path in (checkpoint, result, head):os.utime(path, ns=(200, 200))original = {path: path.read_bytes() for path in (previous, checkpoint, result)}finish(workbench_api, workbench_db, scan, terminal_reason="capped")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()invalid = not reason.strip()assert completed["progress"]["status"] == "complete"@@ -492,8 +488,8 @@ def test_malformed_terminal_coverage_preserves_only_earlier_coverage({"id": "finished", "reason": "Separate review remains."},],),+ mtime_ns=100,)- os.utime(checkpoint, ns=(100, 100))invalid_surface = copy.deepcopy(surface)if malformed == "surface":invalid_surface.pop("label")@@ -526,7 +522,7 @@ def test_malformed_terminal_coverage_preserves_only_earlier_coverage()if selected_head:result = write_checkpoint(output / "checkpoints", terminal)- (output / "checkpoint-head.json").write_text(json.dumps({"checkpoint": result.name}))+ select_checkpoint(output, result)else:result = output / "result.json"result.write_text(json.dumps(terminal))@@ -534,7 +530,7 @@ def test_malformed_terminal_coverage_preserves_only_earlier_coverage(original = {path: path.read_bytes() for path in (checkpoint, result)}finish(workbench_api, workbench_db, scan, terminal_reason="capped")- complete(workbench_api, workbench_db, scan)+ scan.complete()findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert any(row["title"] == finding["title"] for row in findings) == terminal_findings@@ -579,7 +575,7 @@ def test_optional_export_failure_does_not_block_deadline()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert completed["progress"]["status"] == "complete"assert "automatic SARIF export failed" in capsys.readouterr().err@@ -588,14 +584,14 @@ def test_optional_export_failure_does_not_block_deadline(def test_standard_parent_closure_preserves_unrelated_task_without_id(- workbench_api, workbench_db, publication_scan+ workbench_db, publication_scan):scan = publication_scan(mode="standard")scan.coverage["resolvedDeferred"] = [{"id": "finished", "reason": "Review completed."}]scan.coverage["deferred"] = [{"reason": "Unrelated pending review."}](scan.scan_dir / "coverage.json").write_text(json.dumps(scan.coverage))- complete(workbench_api, workbench_db, scan)+ scan.complete()coverage = read_review_state(workbench_db, scan.scan_dir)assert len(coverage["deferred"]) == 1diff --git a/plugins/codex-security/tests/test_deep_scan_deadline_recovery.py b/plugins/codex-security/tests/test_deep_scan_deadline_recovery.pyindex f20858957..4b2306532 100644--- a/plugins/codex-security/tests/test_deep_scan_deadline_recovery.py+++ b/plugins/codex-security/tests/test_deep_scan_deadline_recovery.py@@ -5,13 +5,11 @@ import hashlibimport jsonimport osimport sqlite3-import tracebackimport pytest-from deep_deadline_crash import COMMIT_EXIT, finish_until_commit+from deep_deadline_crash import interrupt_finish_before_commitfrom test_deep_scan_stop_conditions import (canceled_discovery_output,- complete,finish,)from test_deep_scan_stop_conditions import (@@ -23,7 +21,13 @@ from test_deep_scan_stop_conditions import (from test_deep_scan_stop_conditions import (saturated_scan as saturated_scan,)-from workbench_test_support import read_review_state, saved_draft, write_checkpoint+from workbench_test_support import (+ read_review_state,+ saved_draft,+ select_checkpoint,+ write_checkpoint,+ write_result_draft,+)def worker_finding(scan, anchor):@@ -62,7 +66,7 @@ def test_deadline_completion_preserves_existing_tasks_and_recovered_findings(original_checkpoint = checkpoint.read_bytes()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert completed["progress"]["status"] == "complete"assert (@@ -100,16 +104,33 @@ def test_deadline_completion_preserves_existing_tasks_and_recovered_findings(assert checkpoint.read_bytes() == original_checkpoint-def interrupt_finish_before_commit(workbench_api, database, scan):- child = os.fork()- if child == 0:- try:- finish_until_commit(workbench_api["deep_scan"], database, scan.scan_id, scan.scan_dir)- except BaseException:- traceback.print_exc()- os._exit(1)- _, status = os.waitpid(child, 0)- assert os.waitstatus_to_exitcode(status) == COMMIT_EXIT+def finish_after_crashes(workbench_api, workbench_db, deadline_scan, tmp_path):+ def run():+ scan = deadline_scan+ database = tmp_path / "crash-recovery.db"+ with sqlite3.connect(database) as connection:+ workbench_db.backup(connection)++ publications = []+ for hash_seed in (1, 17, 83):+ interrupt_finish_before_commit(database, scan, hash_seed)+ publications.append(read_review_state(workbench_db, scan.scan_dir))+ with sqlite3.connect(database) as connection:+ status = connection.execute(+ "SELECT status FROM deep_scan_runs WHERE scan_id = ?", (scan.scan_id,)+ ).fetchone()[0]+ assert status == "running"+ with sqlite3.connect(database) as connection:+ connection.row_factory = sqlite3.Row+ finish(workbench_api, connection, scan, terminal_reason="capped")+ completed = scan.complete(connection)+ coverage = read_review_state(workbench_db, scan.scan_dir)+ assert completed["progress"]["status"] == "complete"+ assert all(publication == coverage for publication in publications)+ return completed, coverage++ return rundef test_discarded_worker_finding_leaves_its_review_pending(@@ -145,7 +166,7 @@ def test_discarded_worker_finding_leaves_its_review_pending(original = checkpoint.read_bytes()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()coverage = read_review_state(workbench_db, scan.scan_dir)assert canonical in coverage["surfaces"]@@ -176,8 +197,8 @@ def test_terminal_worker_authority_requires_valid_findings(findings=[previous],deferred=[{"id": "review", "reason": "Review pending."}],),+ mtime_ns=100,)- os.utime(checkpoint, ns=(100, 100))sibling = worker_finding(scan, "valid-terminal-sibling")terminal_findings = [sibling]if invalid_terminal:@@ -185,21 +206,20 @@ def test_terminal_worker_authority_requires_valid_findings(invalid.pop("remediation")terminal_findings.append(invalid)result = output / "result.json"- result.write_text(- json.dumps(- saved_draft(- scan.scan_id,- complete=True,- findings=terminal_findings,- closures=[{"id": "review", "reason": "Review completed."}],- )- )+ write_result_draft(+ result,+ saved_draft(+ scan.scan_id,+ complete=True,+ findings=terminal_findings,+ closures=[{"id": "review", "reason": "Review completed."}],+ ),+ mtime_ns=200,)- os.utime(result, ns=(200, 200))original = {path: path.read_bytes() for path in (checkpoint, result)}finish(workbench_api, workbench_db, scan, terminal_reason="capped")- complete(workbench_api, workbench_db, scan)+ scan.complete()findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert any(row["title"] == previous["title"] for row in findings) == invalid_terminal@@ -216,10 +236,11 @@ def test_malformed_worker_finding_preserves_a_valid_sibling_outcome(worker_id, output = canceled_discovery_output(workbench_db, scan)earlier = worker_finding(scan, "rejected-candidate")earlier["provenance"]["candidateId"] = "candidate"- checkpoint = write_checkpoint(- output / "checkpoints", saved_draft(scan.scan_id, findings=[earlier])+ write_checkpoint(+ output / "checkpoints",+ saved_draft(scan.scan_id, findings=[earlier]),+ mtime_ns=100,)- os.utime(checkpoint, ns=(100, 100))invalid = worker_finding(scan, "malformed-sibling")invalid.pop("remediation")outcome = {@@ -229,14 +250,14 @@ def test_malformed_worker_finding_preserves_a_valid_sibling_outcome("disposition": "rejected","receiptRefs": [],}- result = output / "result.json"- result.write_text(- json.dumps(saved_draft(scan.scan_id, complete=True, findings=[invalid], surfaces=[outcome]))+ write_result_draft(+ output / "result.json",+ saved_draft(scan.scan_id, complete=True, findings=[invalid], surfaces=[outcome]),+ mtime_ns=200,)- os.utime(result, ns=(200, 200))finish(workbench_api, workbench_db, scan, terminal_reason="capped")- complete(workbench_api, workbench_db, scan)+ scan.complete()findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert all(row["title"] != earlier["title"] for row in findings)@@ -314,7 +335,7 @@ def test_worker_writeups_remain_bound_to_their_source_directory(originals[checkpoint] = checkpoint.read_bytes()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert len(findings) == 3@@ -336,9 +357,6 @@ def test_worker_writeups_remain_bound_to_their_source_directory(assert {path: path.read_bytes() for path in originals} == originals- not hasattr(os, "fork"), reason="Crash boundary uses a forked workbench process."-)@pytest.mark.parametrize(("colliding_ids", "parent_closure", "reviewed", "worker_like_id"),[@@ -404,7 +422,7 @@ def test_deadline_recovery_is_idempotent_after_crash_before_commit(),)connection.close()- interrupt_finish_before_commit(workbench_api, database, scan)+ interrupt_finish_before_commit(database, scan, 1)before = read_review_state(workbench_db, scan.scan_dir)findings_before = json.loads((scan.scan_dir / "findings.json").read_text())assert len([row for row in before["surfaces"] if row.get("workerId") == worker_id]) == len(@@ -422,7 +440,7 @@ def test_deadline_recovery_is_idempotent_after_crash_before_commit(== "running")finish(workbench_api, connection, scan, terminal_reason="capped")- complete(workbench_api, connection, scan)+ scan.complete(connection)after = read_review_state(workbench_db, scan.scan_dir)findings_after = json.loads((scan.scan_dir / "findings.json").read_text())@@ -435,13 +453,10 @@ def test_deadline_recovery_is_idempotent_after_crash_before_commit(assert after == before- not hasattr(os, "fork"), reason="Crash boundary uses a forked workbench process."-)@pytest.mark.parametrize("earlier_checkpoint_first", [False, True])@pytest.mark.parametrize("colliding_ids", [False, True])def test_deadline_rejection_history_is_stable_across_repeated_crashes(- workbench_api, workbench_db, deadline_scan, tmp_path, earlier_checkpoint_first, colliding_ids+ workbench_db, deadline_scan, finish_after_crashes, earlier_checkpoint_first, colliding_ids):scan = deadline_scanworker_id, output = canceled_discovery_output(workbench_db, scan)@@ -475,31 +490,10 @@ def test_deadline_rejection_history_is_stable_across_repeated_crashes(assert (old_checkpoint.name < checkpoint.name) == earlier_checkpoint_firstos.utime(old_checkpoint, ns=(100, 100))os.utime(checkpoint, ns=(200, 200))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ head = select_checkpoint(output, checkpoint)(output / "result.json").unlink(missing_ok=True)originals = {path: path.read_bytes() for path in (old_checkpoint, checkpoint, head)}- database = tmp_path / "repeated-crash.db"- with sqlite3.connect(database) as connection:- workbench_db.backup(connection)-- publications = []- for _ in range(3):- interrupt_finish_before_commit(workbench_api, database, scan)- publications.append(read_review_state(workbench_db, scan.scan_dir))- with sqlite3.connect(database) as connection:- assert (- connection.execute(- "SELECT status FROM deep_scan_runs WHERE scan_id = ?", (scan.scan_id,)- ).fetchone()[0]- == "running"- )- with sqlite3.connect(database) as connection:- connection.row_factory = sqlite3.Row- finish(workbench_api, connection, scan, terminal_reason="capped")- complete(workbench_api, connection, scan)- coverage = read_review_state(workbench_db, scan.scan_dir)- assert all(publication == coverage for publication in publications)+ _, coverage = finish_after_crashes()surfaces = [row for row in coverage["surfaces"] if row.get("workerId") == worker_id]assert {row["label"] for row in surfaces} == {row["label"] for row in outcomes}assert len(surfaces) == len({row["id"] for row in surfaces}) == len(outcomes)diff --git a/plugins/codex-security/tests/test_deep_scan_deadline_surface_links.py b/plugins/codex-security/tests/test_deep_scan_deadline_surface_links.pyindex 63083a713..6af911ecc 100644--- a/plugins/codex-security/tests/test_deep_scan_deadline_surface_links.py+++ b/plugins/codex-security/tests/test_deep_scan_deadline_surface_links.py@@ -2,13 +2,10 @@ from __future__ import annotationsimport jsonimport os-import sqlite3-import subprocess-import sys-import deep_deadline_crashimport pytest-from test_deep_scan_stop_conditions import canceled_discovery_output, complete, finish+from test_deep_scan_deadline_recovery import finish_after_crashes as finish_after_crashes+from test_deep_scan_stop_conditions import canceled_discovery_output, finishfrom test_deep_scan_stop_conditions import deadline_scan as deadline_scanfrom test_deep_scan_stop_conditions import publication_scan as publication_scanfrom test_deep_scan_stop_conditions import saturated_scan as saturated_scan@@ -18,6 +15,7 @@ from workbench_test_support import (saved_binding,saved_discovery_worker,saved_draft,+ select_checkpoint,write_checkpoint,)@@ -30,28 +28,11 @@ def pending(identity, reason, surface_id="entry"):return {"id": identity, "reason": reason, "surfaceIds": [surface_id]}-def interrupt_in_fresh_python(database, scan, hash_seed):- result = subprocess.run(- [- sys.executable,- "-B",- deep_deadline_crash.__file__,- str(database),- scan.scan_id,- str(scan.scan_dir),- ],- env={**os.environ, "PYTHONHASHSEED": str(hash_seed)},- capture_output=True,- text=True,- )- assert result.returncode == deep_deadline_crash.COMMIT_EXIT, result.stdout + result.stderr--@pytest.mark.parametrize("parent_collision", [pytest.param(False, marks=pytest.mark.cross_platform), True])def test_deadline_surface_links_follow_sources_across_repeated_crashes(- workbench_api, workbench_db, deadline_scan, tmp_path, parent_collision+ workbench_db, deadline_scan, finish_after_crashes, parent_collision):scan = deadline_scanworker_id, output = canceled_discovery_output(workbench_db, scan, attempt=2)@@ -69,8 +50,8 @@ def test_deadline_surface_links_follow_sources_across_repeated_crashes(surfaces=[surface(label)],deferred=[pending(f"{label}-review", f"Review {label}."), shared],),+ mtime_ns=index * 100,)- os.utime(checkpoint, ns=(index * 100, index * 100))checkpoints.append(checkpoint)canonical_id = f"{worker_id}/entry" if parent_collision else "parent-entry"canonical_surface = surface("Canonical parent", canonical_id)@@ -78,29 +59,7 @@ def test_deadline_surface_links_follow_sources_across_repeated_crashes(scan.coverage.update(surfaces=[canonical_surface], deferred=[canonical_pending])(scan.scan_dir / "coverage.json").write_text(json.dumps(scan.coverage))originals = {path: path.read_bytes() for path in checkpoints}- database = tmp_path / "surface-links.db"- with sqlite3.connect(database) as connection:- workbench_db.backup(connection)-- publications = []- for hash_seed in (1, 17, 83):- interrupt_in_fresh_python(database, scan, hash_seed)- publications.append(read_review_state(workbench_db, scan.scan_dir))- with sqlite3.connect(database) as connection:- assert (- connection.execute(- "SELECT status FROM deep_scan_runs WHERE scan_id = ?", (scan.scan_id,)- ).fetchone()[0]- == "running"- )- with sqlite3.connect(database) as connection:- connection.row_factory = sqlite3.Row- finish(workbench_api, connection, scan, terminal_reason="capped")- completed = complete(workbench_api, connection, scan)-- coverage = read_review_state(workbench_db, scan.scan_dir)- assert completed["progress"]["status"] == "complete"- assert all(publication == coverage for publication in publications)+ _, coverage = finish_after_crashes()assert canonical_surface in coverage["surfaces"]assert canonical_pending in coverage["deferred"]surfaces = [row for row in coverage["surfaces"] if row.get("workerId") == worker_id]@@ -141,7 +100,7 @@ def test_deadline_retains_all_targets_of_one_legacy_surface_id(original = checkpoint.read_bytes()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- complete(workbench_api, workbench_db, scan)+ scan.complete()coverage = read_review_state(workbench_db, scan.scan_dir)surfaces = [row for row in coverage["surfaces"] if row["label"] in {"download", "upload"}]@@ -171,7 +130,7 @@ def test_deadline_deferred_only_checkpoint_keeps_prior_surface_link(originals = {path: path.read_bytes() for path in (earlier, current)}finish(workbench_api, workbench_db, scan, terminal_reason="capped")- complete(workbench_api, workbench_db, scan)+ scan.complete()coverage = read_review_state(workbench_db, scan.scan_dir)retained = next(row for row in coverage["surfaces"] if row["label"] == "Earlier review")@@ -208,15 +167,13 @@ def test_deadline_reopened_generic_surface_copy_retains_its_link(]originals = {}for index, draft in enumerate(drafts, start=1):- checkpoint = write_checkpoint(output / "checkpoints", draft)- os.utime(checkpoint, ns=(index * 100, index * 100))+ checkpoint = write_checkpoint(output / "checkpoints", draft, mtime_ns=index * 100)originals[checkpoint] = checkpoint.read_bytes()- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ head = select_checkpoint(output, checkpoint)originals[head] = head.read_bytes()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- complete(workbench_api, workbench_db, scan)+ scan.complete()coverage = read_review_state(workbench_db, scan.scan_dir)retained = [row for row in coverage["surfaces"] if row.get("workerId") == worker_id]@@ -260,7 +217,7 @@ def test_stopped_recovery_preserves_source_link_union(workbench_api, tmp_path):@pytest.mark.parametrize("changed_task", [False, True], ids=["copied-task", "updated-task"])def test_deadline_retains_equal_task_origins_from_selected_worker_result(- workbench_api, workbench_db, deadline_scan, tmp_path, changed_task+ workbench_db, deadline_scan, finish_after_crashes, changed_task):scan = deadline_scanworker_id, output = canceled_discovery_output(workbench_db, scan, attempt=2)@@ -269,11 +226,8 @@ def test_deadline_retains_equal_task_origins_from_selected_worker_result(archived_draft = saved_draft(scan.scan_id, surfaces=[surface("download")], deferred=[previous_task])- checkpoint = write_checkpoint(archived / "checkpoints", archived_draft)- os.utime(checkpoint, ns=(100, 100))- archived_head = archived / "checkpoint-head.json"- archived_head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(archived_head, ns=(110, 110))+ checkpoint = write_checkpoint(archived / "checkpoints", archived_draft, mtime_ns=100)+ select_checkpoint(archived, checkpoint, mtime_ns=110)archived_result = archived / "result.json"archived_result.write_bytes(checkpoint.read_bytes())os.utime(archived_result, ns=(120, 120))@@ -286,36 +240,18 @@ def test_deadline_retains_equal_task_origins_from_selected_worker_result(surfaces=[surface("upload")],deferred=[current_task, retained_task] if changed_task else [current_task],)- raw_checkpoint = write_checkpoint(output / "checkpoints", incoming)- os.utime(raw_checkpoint, ns=(200, 200))+ write_checkpoint(output / "checkpoints", incoming, mtime_ns=200)# The writer preserves the old pending task while publishing its latest surface.merged = saved_draft(scan.scan_id, surfaces=[surface("upload")], deferred=[current_task, retained_task])- selected = write_checkpoint(output / "checkpoints", merged)- os.utime(selected, ns=(210, 210))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(220, 220))+ selected = write_checkpoint(output / "checkpoints", merged, mtime_ns=210)+ select_checkpoint(output, selected, mtime_ns=220)result = output / "result.json"result.write_bytes(selected.read_bytes())os.utime(result, ns=(230, 230))originals = {path: path.read_bytes() for path in output.parent.rglob("*") if path.is_file()}- database = tmp_path / "selected-result.db"- with sqlite3.connect(database) as connection:- workbench_db.backup(connection)-- publications = []- for hash_seed in (1, 17, 83):- interrupt_in_fresh_python(database, scan, hash_seed)- publications.append(read_review_state(workbench_db, scan.scan_dir))- with sqlite3.connect(database) as connection:- connection.row_factory = sqlite3.Row- finish(workbench_api, connection, scan, terminal_reason="capped")- complete(workbench_api, connection, scan)-- coverage = read_review_state(workbench_db, scan.scan_dir)- assert all(publication == coverage for publication in publications)+ _, coverage = finish_after_crashes()surfaces = [row for row in coverage["surfaces"] if row.get("workerId") == worker_id]assert {row["label"] for row in surfaces} == {"download", "upload"}upload = next(row for row in surfaces if row["label"] == "upload")@@ -334,7 +270,7 @@ def test_deadline_retains_equal_task_origins_from_selected_worker_result("selected_result", [False, True], ids=["raw-checkpoint", "selected-result"])def test_deadline_dropped_legacy_surface_does_not_rebind_its_pending_task(- workbench_api, workbench_db, deadline_scan, tmp_path, selected_result+ workbench_db, deadline_scan, finish_after_crashes, selected_result):scan = deadline_scanworker_id, output = canceled_discovery_output(workbench_db, scan, attempt=2)@@ -343,8 +279,8 @@ def test_deadline_dropped_legacy_surface_does_not_rebind_its_pending_task(archived_checkpoint = write_checkpoint(archived / "checkpoints",saved_draft(scan.scan_id, surfaces=[surface("download")], deferred=[download_task]),+ mtime_ns=100,)- os.utime(archived_checkpoint, ns=(100, 100))archived_result = archived / "result.json"archived_result.write_bytes(archived_checkpoint.read_bytes())os.utime(archived_result, ns=(110, 110))@@ -354,8 +290,8 @@ def test_deadline_dropped_legacy_surface_does_not_rebind_its_pending_task(raw_checkpoint = write_checkpoint(output / "checkpoints",saved_draft(scan.scan_id, surfaces=[malformed_surface], deferred=[upload_task]),+ mtime_ns=200,)- os.utime(raw_checkpoint, ns=(200, 200))if selected_result:selected = write_checkpoint(output / "checkpoints",@@ -364,31 +300,14 @@ def test_deadline_dropped_legacy_surface_does_not_rebind_its_pending_task(surfaces=[malformed_surface],deferred=[upload_task, download_task],),+ mtime_ns=210,)- os.utime(selected, ns=(210, 210))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(220, 220))+ select_checkpoint(output, selected, mtime_ns=220)result = output / "result.json"result.write_bytes(selected.read_bytes())os.utime(result, ns=(230, 230))originals = {path: path.read_bytes() for path in output.parent.rglob("*") if path.is_file()}- database = tmp_path / "dropped-surface.db"- with sqlite3.connect(database) as connection:- workbench_db.backup(connection)-- publications = []- for hash_seed in (1, 17, 83):- interrupt_in_fresh_python(database, scan, hash_seed)- publications.append(read_review_state(workbench_db, scan.scan_dir))- with sqlite3.connect(database) as connection:- connection.row_factory = sqlite3.Row- finish(workbench_api, connection, scan, terminal_reason="capped")- completed = complete(workbench_api, connection, scan)-- coverage = read_review_state(workbench_db, scan.scan_dir)- assert completed["progress"]["status"] == "complete"- assert all(publication == coverage for publication in publications)+ completed, coverage = finish_after_crashes()surfaces = [row for row in coverage["surfaces"] if row.get("workerId") == worker_id]assert len(surfaces) == 1 and surfaces[0]["label"] == "download"tasks = [row for row in coverage["deferred"] if row.get("workerId") == worker_id]@@ -409,7 +328,7 @@ def test_deadline_dropped_legacy_surface_does_not_rebind_its_pending_task(["parent-collision", "current-checkpoints", "retry-checkpoints", "coalesced-history"],)def test_deadline_inherited_surface_links_follow_historical_observations(- workbench_api, workbench_db, deadline_scan, tmp_path, history+ workbench_db, deadline_scan, finish_after_crashes, history):scan = deadline_scanworker_id, output = canceled_discovery_output(@@ -417,26 +336,26 @@ def test_deadline_inherited_surface_links_follow_historical_observations()archived = output.parent / "attempts" / "attempt-01"first_output = archived if history == "retry-checkpoints" else output- earlier = write_checkpoint(+ write_checkpoint(first_output / "checkpoints",saved_draft(scan.scan_id, surfaces=[surface("Earlier observation")]),+ mtime_ns=100,)- os.utime(earlier, ns=(100, 100))expected_labels = {"Earlier observation"}if history != "parent-collision":- newer = write_checkpoint(+ write_checkpoint(output / "checkpoints",saved_draft(scan.scan_id, surfaces=[surface("Newer observation")]),+ mtime_ns=200,)- os.utime(newer, ns=(200, 200))expected_labels.add("Newer observation")inherited_task = pending("history-review", "Review the inherited surface.")unknown_task = pending("unknown-review", "Review an unresolved reference.", "not-declared")- deferred_only = write_checkpoint(+ write_checkpoint(output / "checkpoints",saved_draft(scan.scan_id, deferred=[inherited_task, unknown_task]),+ mtime_ns=300,)- os.utime(deferred_only, ns=(300, 300))if history == "coalesced-history":# The merged checkpoint copies a task whose earlier raw source inherited history.selected = write_checkpoint(@@ -446,40 +365,23 @@ def test_deadline_inherited_surface_links_follow_historical_observations(surfaces=[surface("Newer observation")],deferred=[inherited_task, unknown_task],),+ mtime_ns=400,)- os.utime(selected, ns=(400, 400))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(410, 410))+ select_checkpoint(output, selected, mtime_ns=410)if history == "current-checkpoints":# An interrupted later write cannot identify an earlier task's target.- future = write_checkpoint(+ write_checkpoint(output / "checkpoints",saved_draft(scan.scan_id, surfaces=[surface("Later observation")]),+ mtime_ns=400,)- os.utime(future, ns=(400, 400))canonical_id = f"{worker_id}/entry" if history == "parent-collision" else "parent-entry"canonical_surface = surface("Canonical parent", canonical_id)canonical_task = pending("parent-review", "Parent review remains.", canonical_id)scan.coverage.update(surfaces=[canonical_surface], deferred=[canonical_task])(scan.scan_dir / "coverage.json").write_text(json.dumps(scan.coverage))originals = {path: path.read_bytes() for path in output.parent.rglob("*") if path.is_file()}- database = tmp_path / "historical-links.db"- with sqlite3.connect(database) as connection:- workbench_db.backup(connection)-- publications = []- for hash_seed in (1, 17, 83):- interrupt_in_fresh_python(database, scan, hash_seed)- publications.append(read_review_state(workbench_db, scan.scan_dir))- with sqlite3.connect(database) as connection:- connection.row_factory = sqlite3.Row- finish(workbench_api, connection, scan, terminal_reason="capped")- completed = complete(workbench_api, connection, scan)-- coverage = read_review_state(workbench_db, scan.scan_dir)- assert completed["progress"]["status"] == "complete"- assert all(publication == coverage for publication in publications)+ _, coverage = finish_after_crashes()assert canonical_surface in coverage["surfaces"]assert canonical_task in coverage["deferred"]worker_surfaces = [row for row in coverage["surfaces"] if row.get("workerId") == worker_id]@@ -502,7 +404,7 @@ def test_deadline_inherited_surface_links_follow_historical_observations(ids=["same-id", "changed-id", "legacy-duplicate-id"],)def test_deadline_candidate_supersession_preserves_independent_surface_links(- workbench_api, workbench_db, deadline_scan, tmp_path, changed_surface_id, legacy_duplicate_id+ workbench_db, deadline_scan, finish_after_crashes, changed_surface_id, legacy_duplicate_id):scan = deadline_scanworker_id, output = canceled_discovery_output(workbench_db, scan)@@ -517,46 +419,30 @@ def test_deadline_candidate_supersession_preserves_independent_surface_links(if legacy_duplicate_id:# Legacy artifacts may reuse an ID for a candidate and an independent observation.earlier_surfaces.append(surface("Independent observation"))- earlier = write_checkpoint(+ write_checkpoint(output / "checkpoints",saved_draft(scan.scan_id, surfaces=earlier_surfaces, deferred=[independent]),+ mtime_ns=100,)- os.utime(earlier, ns=(100, 100))outcome_id = "outcome" if changed_surface_id else "entry"outcome = {**surface("Candidate reviewed", outcome_id, "rejected"),"candidateId": "candidate",}- raw_checkpoint = write_checkpoint(- output / "checkpoints", saved_draft(scan.scan_id, complete=True, surfaces=[outcome])+ write_checkpoint(+ output / "checkpoints",+ saved_draft(scan.scan_id, complete=True, surfaces=[outcome]),+ mtime_ns=200,)- os.utime(raw_checkpoint, ns=(200, 200))# The selected merged checkpoint retains independent work before result publication.selected = write_checkpoint(output / "checkpoints",saved_draft(scan.scan_id, complete=True, surfaces=[outcome], deferred=[independent]),+ mtime_ns=210,)- os.utime(selected, ns=(210, 210))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(220, 220))+ select_checkpoint(output, selected, mtime_ns=220)originals = {path: path.read_bytes() for path in output.rglob("*") if path.is_file()}- database = tmp_path / "candidate-links.db"- with sqlite3.connect(database) as connection:- workbench_db.backup(connection)-- publications = []- for hash_seed in (1, 17, 83):- interrupt_in_fresh_python(database, scan, hash_seed)- publications.append(read_review_state(workbench_db, scan.scan_dir))- with sqlite3.connect(database) as connection:- connection.row_factory = sqlite3.Row- finish(workbench_api, connection, scan, terminal_reason="capped")- completed = complete(workbench_api, connection, scan)-- coverage = read_review_state(workbench_db, scan.scan_dir)- assert completed["progress"]["status"] == "complete"- assert all(publication == coverage for publication in publications)+ _, coverage = finish_after_crashes()assert canonical_surface in coverage["surfaces"]assert canonical_task in coverage["deferred"]retained = [row for row in coverage["surfaces"] if row.get("workerId") == worker_id]diff --git a/plugins/codex-security/tests/test_deep_scan_publication_authority.py b/plugins/codex-security/tests/test_deep_scan_publication_authority.pyindex dc4d2714f..e20683c25 100644--- a/plugins/codex-security/tests/test_deep_scan_publication_authority.py+++ b/plugins/codex-security/tests/test_deep_scan_publication_authority.py@@ -12,7 +12,6 @@ from pathlib import Pathfrom types import SimpleNamespaceimport pytest-from test_deep_scan_successful_publication import add_worker, completefrom test_deep_scan_successful_publication import publication_scan as publication_scanfrom workbench_test_support import read_review_state@@ -63,8 +62,8 @@ def test_stale_coordinator_cannot_replace_newer_canonical_publication(workbench_api, workbench_db, publication_scan, stale, complete):scan = publication_scan()- old_result = add_worker(workbench_db, scan)- new_result = add_worker(workbench_db, scan)+ old_result = scan.add_worker()+ new_result = scan.add_worker()with workbench_db:workbench_db.execute("UPDATE deep_scan_runs SET coordinator_generation = 3 WHERE scan_id = ?",@@ -120,7 +119,7 @@ def test_current_publication_replays_without_changing_checkpoint_or_worker_stateinstant = workbench_api["now"]()monkeypatch.setattr(workbench_api["_WORKBENCH_DB_CONTEXT"], "now", lambda: instant)scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()with workbench_db:workbench_db.execute("UPDATE deep_scan_runs SET coordinator_generation = ?, status = 'running' "@@ -182,7 +181,7 @@ def test_legacy_terminal_aggregate_survives_a_late_partial_draft_after_write_faiwith monkeypatch.context() as patch:patch.setitem(finalizer_globals, "write_scan_local_bytes", fail_report)with pytest.raises(SystemExit, match="Synthetic report write interruption"):- complete(workbench_api, workbench_db, scan)+ scan.complete()run = workbench_db.execute("SELECT status, coordinator_generation FROM deep_scan_runs WHERE scan_id = ?",@@ -212,7 +211,7 @@ def test_legacy_terminal_aggregate_survives_a_late_partial_draft_after_write_faifor path in scan.scan_dir.rglob("*.json")if "drafts" not in path.parts} == saved- assert complete(workbench_api, workbench_db, scan)["progress"]["status"] == "complete"+ assert scan.complete()["progress"]["status"] == "complete"findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert [finding["title"] for finding in findings] == [scan.findings[0]["title"]]assert read_review_state(workbench_db, scan.scan_dir)["deferred"] == scan.coverage["deferred"]@@ -227,7 +226,7 @@ def test_failed_publication_does_not_acknowledge_staged_input(workbench_api, workbench_db, publication_scan, monkeypatch, failure):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()with workbench_db:workbench_db.execute("UPDATE deep_scan_runs SET workflow_version = 'deep-scan-mcp/v1', "@@ -352,7 +351,7 @@ def test_receipt_io_failure_preserves_successful_publication(if backend_kind == "posix" and os.name == "nt":pytest.skip("POSIX descriptor-relative directory errors")scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()with workbench_db:workbench_db.execute("UPDATE deep_scan_runs SET workflow_version = ?, coordinator_generation = 2 "@@ -501,7 +500,7 @@ def test_windows_receipt_root_probe_classifies_storage_and_path_failures(failure,):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()with workbench_db:workbench_db.execute("UPDATE deep_scan_runs SET coordinator_generation = 2 WHERE scan_id = ?",@@ -628,7 +627,7 @@ def test_receipt_contract_errors_still_reject_publication(workbench_api, workbench_db, publication_scan, monkeypatch, failure):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()with workbench_db:workbench_db.execute("UPDATE deep_scan_runs SET workflow_version = 'deep-scan-mcp/v1', "@@ -672,7 +671,7 @@ def test_accepted_publication_replay_retains_original_warning_and_bytes(workbench_api, workbench_db, publication_scan, monkeypatch):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()with workbench_db:workbench_db.execute("UPDATE deep_scan_runs SET coordinator_generation = 2 WHERE scan_id = ?",diff --git a/plugins/codex-security/tests/test_deep_scan_publication_replay.py b/plugins/codex-security/tests/test_deep_scan_publication_replay.pyindex 8c4fe52eb..ccae59463 100644--- a/plugins/codex-security/tests/test_deep_scan_publication_replay.py+++ b/plugins/codex-security/tests/test_deep_scan_publication_replay.py@@ -9,7 +9,6 @@ from pathlib import Pathimport pytestfrom test_deep_scan_publication_authority import stage_publication-from test_deep_scan_successful_publication import add_workerfrom test_deep_scan_successful_publication import publication_scan as publication_scan_CRASH_PUBLICATION = """@@ -60,7 +59,7 @@ def test_publication_crash_replays_selected_input_without_stale_overwrite(workbench_api, workbench_db, publication_scan, tmp_path, boundary):scan = publication_scan()- result_path = add_worker(workbench_db, scan)+ result_path = scan.add_worker()with workbench_db:workbench_db.execute("UPDATE deep_scan_runs SET coordinator_generation = 3 WHERE scan_id = ?",diff --git a/plugins/codex-security/tests/test_deep_scan_stop_conditions.py b/plugins/codex-security/tests/test_deep_scan_stop_conditions.pyindex 1e19a5732..d6a859265 100644--- a/plugins/codex-security/tests/test_deep_scan_stop_conditions.py+++ b/plugins/codex-security/tests/test_deep_scan_stop_conditions.py@@ -7,21 +7,22 @@ import sqlite3from argparse import Namespaceimport pytest-from test_deep_scan_successful_publication import (- add_worker,- assert_published_aggregate,- complete,-)+from test_deep_scan_successful_publication import assert_published_aggregatefrom test_deep_scan_successful_publication import (publication_scan as publication_scan,)-from workbench_test_support import read_review_state, saved_draft, write_checkpoint+from workbench_test_support import (+ read_review_state,+ saved_draft,+ select_checkpoint,+ write_checkpoint,+)@pytest.fixturedef saturated_scan(publication_scan, workbench_db):scan = publication_scan()- completed_result = add_worker(workbench_db, scan)+ completed_result = scan.add_worker()completed_result.write_text(json.dumps({@@ -32,7 +33,7 @@ def saturated_scan(publication_scan, workbench_db):}))- reducer_result = add_worker(workbench_db, scan)+ reducer_result = scan.add_worker()reducer_result.write_bytes(completed_result.read_bytes())with workbench_db:workbench_db.execute(@@ -84,7 +85,7 @@ def deadline_scan(saturated_scan, workbench_db):def canceled_discovery_output(connection, scan, *, attempt=1):- result = add_worker(connection, scan, status="canceled")+ result = scan.add_worker(connection, status="canceled")worker_id = result.parent.nameoutput = scan.scan_dir / "artifacts" / "deep_discovery" / "workers" / worker_id / "output"output.parent.mkdir(parents=True)@@ -117,7 +118,7 @@ def test_deadline_preserves_optional_reduced_writeup_behavior()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert completed["progress"]["status"] == "complete"finding = json.loads((scan.scan_dir / "findings.json").read_text())["findings"][0]@@ -153,7 +154,7 @@ def test_deadline_recovers_valid_findings_from_an_invalid_archived_retry()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert {item["identity"]["anchor"] for item in findings} == {@@ -222,7 +223,7 @@ def test_deadline_preserves_worker_findings_with_optional_and_shared_identity()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert len(findings) == 4@@ -237,19 +238,18 @@ def test_deadline_resolves_worker_local_deferred_ids_before_scoping():scan = deadline_scan_, output = canceled_discovery_output(workbench_db, scan)- previous = write_checkpoint(+ write_checkpoint(output / "checkpoints",saved_draft(scan.scan_id, deferred=[{"id": "review", "reason": "Review pending."}]),+ mtime_ns=100,)- os.utime(previous, ns=(100, 100))current = saved_draft(scan.scan_id, complete=True)current["coverage"]["resolvedDeferred"] = [{"id": "review", "reason": "Review completed."}]- latest = write_checkpoint(output / "checkpoints", current)- os.utime(latest, ns=(200, 200))- (output / "checkpoint-head.json").write_text(json.dumps({"checkpoint": latest.name}))+ latest = write_checkpoint(output / "checkpoints", current, mtime_ns=200)+ select_checkpoint(output, latest)finish(workbench_api, workbench_db, scan, terminal_reason="capped")- complete(workbench_api, workbench_db, scan)+ scan.complete()coverage = read_review_state(workbench_db, scan.scan_dir)assert not any(item["reason"] == "Review pending." for item in coverage["deferred"])@@ -277,7 +277,7 @@ def test_deadline_does_not_turn_invalid_worker_receipts_into_parent_artifacts()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()surface = read_review_state(workbench_db, scan.scan_dir)["surfaces"][0]assert surface["receiptRefs"] == []@@ -342,7 +342,7 @@ def test_deadline_preserves_worker_receipts_and_surface_links(expected[label] = (worker_id, receipt, checkpoint, checkpoint.read_bytes())finish(workbench_api, workbench_db, scan, terminal_reason="capped")- complete(workbench_api, workbench_db, scan)+ scan.complete()coverage = read_review_state(workbench_db, scan.scan_dir)assert len(coverage["surfaces"]) == 3@@ -372,7 +372,7 @@ def test_saturated_finish_cancels_unfinished_discovery_without_using_its_drafts(scan = saturated_scanunfinished = []for status in ("queued", "running"):- result = add_worker(workbench_db, scan, status=status)+ result = scan.add_worker(status=status)result.write_text("{unfinished worker draft")unfinished.append(result)@@ -391,7 +391,7 @@ def test_saturated_finish_cancels_unfinished_discovery_without_using_its_drafts(assert worker["mergeState"] == "none"assert result.read_text() == "{unfinished worker draft"- complete(workbench_api, workbench_db, scan)+ scan.complete()assert_published_aggregate(scan)@@ -399,7 +399,7 @@ def test_saturated_finish_retains_failed_discovery_diagnostic(workbench_api, workbench_db, saturated_scan):scan = saturated_scan- result = add_worker(workbench_db, scan, status="failed")+ result = scan.add_worker(status="failed")error = "Worker stopped while persisting its result."with workbench_db:workbench_db.execute(@@ -415,7 +415,7 @@ def test_saturated_finish_retains_failed_discovery_diagnostic()assert failed_worker["status"] == "failed"assert failed_worker["error"] == error- complete(workbench_api, workbench_db, scan)+ scan.complete()assert_published_aggregate(scan)@@ -424,7 +424,7 @@ def test_unfinished_checkpoints_after_a_completed_reduction(workbench_api, workbench_db, saturated_scan, stop):scan = saturated_scan- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")recovered = copy.deepcopy(scan.findings[0])recovered["identity"]["anchor"] = "unfinished-upload-review"recovered["title"] = "Separate upload handler bypasses containment"@@ -468,8 +468,7 @@ def test_unfinished_checkpoints_after_a_completed_reduction(saved_draft(scan.scan_id, findings=[discarded]),)if directory == scan.scan_dir:- head = directory / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": discarded_checkpoint.name}))+ head = select_checkpoint(directory, discarded_checkpoint)observed = (max((directory / name).stat().st_mtime_ns@@ -506,7 +505,7 @@ def test_unfinished_checkpoints_after_a_completed_reduction(assert tuple(row) == ("running", None)manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())assert "sealedAt" not in manifest["scan"]- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert completed["progress"]["status"] == "complete"findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]expected_anchors = {scan.findings[0]["identity"]["anchor"]}@@ -541,7 +540,7 @@ def test_unfinished_checkpoints_after_a_completed_reduction(names = ("scan-manifest.json", "findings.json", "report.md")sealed = {name: (scan.scan_dir / name).read_bytes() for name in names}finish(workbench_api, workbench_db, scan, terminal_reason=reason)- complete(workbench_api, workbench_db, scan)+ scan.complete()assert {name: (scan.scan_dir / name).read_bytes() for name in names} == sealed@@ -556,7 +555,7 @@ def test_deadline_after_all_reviews_complete_keeps_reduced_coverage()finish(workbench_api, workbench_db, scan, terminal_reason="capped")- complete(workbench_api, workbench_db, scan)+ scan.complete()assert_published_aggregate(scan)@@ -570,7 +569,7 @@ def test_finish_preserves_other_worker_failure_checks(workbench_api, workbench_db, saturated_scan, terminal_reason, kind):scan = saturated_scan- result = add_worker(workbench_db, scan, status="failed")+ result = scan.add_worker(status="failed")with workbench_db:workbench_db.execute("UPDATE deep_scan_workers SET kind = ? WHERE id = ?", (kind, result.parent.name)@@ -602,7 +601,7 @@ def test_rejected_deadline_publication_does_not_leave_parent_observations(os.utime(scan.scan_dir / name, ns=(1_000_000_000, 1_000_000_000))pending = {"id": "authorization-review", "reason": "Review the remaining authorization path."}if with_reduction:- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")write_checkpoint(result.parent / "checkpoints",saved_draft(scan.scan_id, deferred=[pending]),@@ -612,8 +611,7 @@ def test_rejected_deadline_publication_does_not_leave_parent_observations(scan.scan_dir / "checkpoints",saved_draft(scan.scan_id, findings=scan.findings, deferred=[pending]),)- head = scan.scan_dir / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ head = select_checkpoint(scan.scan_dir, checkpoint)for path in (checkpoint, head):os.utime(path, ns=(2_000_000_000, 2_000_000_000))saved = workbench_api["saved_results"]@@ -677,7 +675,7 @@ def test_zero_accepted_deadline_preserves_worker_checkpoint_links_and_receipts(workers = {}originals = {}for name in ("first", "second"):- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")worker_id = result.parent.nameoutput = scan.scan_dir / "artifacts" / "deep_discovery" / "workers" / worker_id / "output"output.parent.mkdir(parents=True)@@ -708,8 +706,7 @@ def test_zero_accepted_deadline_preserves_worker_checkpoint_links_and_receipts(deferred=[{"id": "review", "reason": "Review remains.", "surfaceIds": ["api"]}],),)- head = result.parent / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ head = select_checkpoint(result.parent, checkpoint)workers[worker_id] = receiptoriginals.update({path: path.read_bytes() for path in (receipt, checkpoint, head)})with workbench_db:diff --git a/plugins/codex-security/tests/test_deep_scan_successful_publication.py b/plugins/codex-security/tests/test_deep_scan_successful_publication.pyindex 63480d644..9128a1a2f 100644--- a/plugins/codex-security/tests/test_deep_scan_successful_publication.py+++ b/plugins/codex-security/tests/test_deep_scan_successful_publication.py@@ -18,6 +18,62 @@ from workbench_test_support import ()+class PublicationScan(SimpleNamespace):+ def add_worker(self, connection=None, *, status="succeeded") -> Path:+ worker_id = str(uuid.uuid4())+ output = self.scan_dir / "workers" / worker_id+ output.mkdir(parents=True)+ result = output / "result.json"+ connection = self.connection if connection is None else connection+ with connection:+ connection.execute(+ "INSERT INTO deep_scan_workers (id, scan_id, kind, status, merge_state, "+ "prompt_path, artifact_dir, result_manifest_path, attempt, created_at, "+ "updated_at, completed_at) VALUES (?, ?, 'discovery', ?, ?, ?, ?, ?, 1, ?, ?, ?)",+ (+ worker_id,+ self.scan_id,+ status,+ "merged" if status == "succeeded" else "none",+ str(output / "prompt.md"),+ str(output),+ str(result),+ self.timestamp,+ self.timestamp,+ self.timestamp,+ ),+ )+ return result++ def complete(self, connection=None, *, prepare_only=False):+ return self.api["complete_scan"](+ self.connection if connection is None else connection,+ Namespace(scan_id=self.scan_id, claim_token=None, cost_json=None),+ prepare_only=prepare_only,+ )["scan"]++ def fail(self, message="Stopped."):+ return self.api["saved_results"].fail_scan(+ self.api["_WORKBENCH_DB_CONTEXT"],+ self.connection,+ Namespace(scan_id=self.scan_id, claim_token=None, cost_json=None, message=message),+ )["scan"]++ def preserve(self, connection=None):+ return self.api["saved_results"].preserve_scan_results(+ self.api["_WORKBENCH_DB_CONTEXT"],+ self.connection if connection is None else connection,+ Namespace(+ scan_id=self.scan_id, claim_token=None, thread_id=None, coordinator_generation=None+ ),+ )["scan"]++ def recover(self):+ return self.api["saved_results"].recover_scan_results(+ self.api["_WORKBENCH_DB_CONTEXT"], self.connection, Namespace(scan_id=self.scan_id)+ )["scan"]++@pytest.fixturedef publication_scan(workbench_api, workbench_db, tmp_path, monkeypatch):monkeypatch.setenv("CODEX_SECURITY_STATE_DIR", str(tmp_path / "state"))@@ -126,7 +182,9 @@ def publication_scan(workbench_api, workbench_db, tmp_path, monkeypatch):("coverage.json", coverage),):(scan_dir / name).write_text(json.dumps(value))- return SimpleNamespace(+ return PublicationScan(+ api=workbench_api,+ connection=workbench_db,scan_id=scan_id,scan_dir=scan_dir,timestamp=timestamp,@@ -137,64 +195,6 @@ def publication_scan(workbench_api, workbench_db, tmp_path, monkeypatch):return create-def add_worker(connection, scan, *, status="succeeded") -> Path:- worker_id = str(uuid.uuid4())- output = scan.scan_dir / "workers" / worker_id- output.mkdir(parents=True)- result = output / "result.json"- with connection:- connection.execute(- "INSERT INTO deep_scan_workers (id, scan_id, kind, status, merge_state, "- "prompt_path, artifact_dir, result_manifest_path, attempt, created_at, "- "updated_at, completed_at) VALUES (?, ?, 'discovery', ?, ?, ?, ?, ?, 1, ?, ?, ?)",- (- worker_id,- scan.scan_id,- status,- "merged" if status == "succeeded" else "none",- str(output / "prompt.md"),- str(output),- str(result),- scan.timestamp,- scan.timestamp,- scan.timestamp,- ),- )- return result---def complete(workbench_api, connection, scan, *, prepare_only=False):- return workbench_api["complete_scan"](- connection,- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None),- prepare_only=prepare_only,- )["scan"]---def fail_scan(workbench_api, connection, scan, message="Stopped."):- return workbench_api["saved_results"].fail_scan(- workbench_api["_WORKBENCH_DB_CONTEXT"],- connection,- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message=message),- )["scan"]---def preserve_scan_results(workbench_api, connection, scan):- return workbench_api["saved_results"].preserve_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"],- connection,- Namespace(- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None- ),- )["scan"]---def recover_scan_results(workbench_api, connection, scan):- return workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], connection, Namespace(scan_id=scan.scan_id)- )["scan"]--def assert_published_aggregate(scan):findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]for finding in findings:@@ -218,9 +218,7 @@ def assert_published_aggregate(scan):@pytest.mark.parametrize("mode", ["standard", "deep", "deep-nested"])@pytest.mark.parametrize("linked_writeup", [False, True], ids=["inline", "linked"])-def test_publication_renders_each_source_remediation(- workbench_api, workbench_db, publication_scan, mode, linked_writeup-):+def test_publication_renders_each_source_remediation(publication_scan, mode, linked_writeup):scan = publication_scan(mode="deep" if mode == "deep-nested" else mode)finding = scan.findings[0]first = copy.deepcopy(finding)@@ -261,7 +259,7 @@ def test_publication_renders_each_source_remediation(writeup_bytes = writeup.read_bytes()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": scan.findings}))- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert completed["progress"]["status"] == "complete"assert_published_aggregate(scan)@@ -281,10 +279,10 @@ def test_publication_renders_each_source_remediation(@pytest.mark.parametrize("scope", [".", "subdir"], ids=["repository", "scoped"])def test_deep_publication_keeps_configured_scope_without_worker_observations(- workbench_api, workbench_db, publication_scan, scope+ publication_scan, scope):scan = publication_scan(scope=scope)- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_coverage = {"surfaces": [{@@ -310,7 +308,7 @@ def test_deep_publication_keeps_configured_scope_without_worker_observations()source_bytes = result.read_bytes()- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert completed["progress"]["status"] == "complete"assert_published_aggregate(scan)@@ -326,18 +324,16 @@ def test_deep_publication_keeps_configured_scope_without_worker_observations(assert "Which deployment controls apply?" not in report-def test_deep_publication_ignores_empty_canceled_checkpoint(- workbench_api, workbench_db, publication_scan-):+def test_deep_publication_ignores_empty_canceled_checkpoint(publication_scan):scan = publication_scan()- result = add_worker(workbench_db, scan, status="canceled")+ result = scan.add_worker(status="canceled")checkpoint = write_checkpoint(result.parent / "checkpoints",saved_draft(scan.scan_id),)source_bytes = checkpoint.read_bytes()- complete(workbench_api, workbench_db, scan)+ scan.complete()assert_published_aggregate(scan)assert checkpoint.read_bytes() == source_bytes@@ -345,16 +341,14 @@ def test_deep_publication_ignores_empty_canceled_checkpoint(@pytest.mark.parametrize("old_result", ["unreadable", "removed"])-def test_deep_publication_does_not_require_old_worker_files(- workbench_api, workbench_db, publication_scan, old_result-):+def test_deep_publication_does_not_require_old_worker_files(publication_scan, old_result):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()result.write_text("{old worker output is unavailable")if old_result == "removed":result.unlink()- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert completed["warnings"] == []assert_published_aggregate(scan)@@ -387,28 +381,26 @@ def reviewed_deep_scan(publication_scan, request):return scan-def test_deep_prepare_and_complete_preserve_the_same_aggregate(- workbench_api, workbench_db, reviewed_deep_scan-):+def test_deep_prepare_and_complete_preserve_the_same_aggregate(workbench_db, reviewed_deep_scan):scan = reviewed_deep_scancount = "SELECT terminal_reviewed_surface_count FROM deep_scan_runs WHERE scan_id = ?"assert workbench_db.execute(count, (scan.scan_id,)).fetchone()[0] is None- prepared = complete(workbench_api, workbench_db, scan, prepare_only=True)+ prepared = scan.complete(prepare_only=True)assert prepared["progress"]["status"] == "running"assert workbench_db.execute(count, (scan.scan_id,)).fetchone()[0] == 1names = ("scan-manifest.json", "findings.json")published = {name: (scan.scan_dir / name).read_bytes() for name in names}- complete(workbench_api, workbench_db, scan, prepare_only=True)- complete(workbench_api, workbench_db, scan)+ scan.complete(prepare_only=True)+ scan.complete()# A capped scan with a failed worker cannot publish anew, but its sealed result can be read.- add_worker(workbench_db, scan, status="failed")+ scan.add_worker(status="failed")with workbench_db:workbench_db.execute("UPDATE deep_scan_runs SET terminal_reason = 'capped' WHERE scan_id = ?",(scan.scan_id,),)- repeated = complete(workbench_api, workbench_db, scan)+ repeated = scan.complete()assert repeated["progress"]["status"] == "complete"assert workbench_db.execute(count, (scan.scan_id,)).fetchone()[0] == 1@@ -416,24 +408,22 @@ def test_deep_prepare_and_complete_preserve_the_same_aggregate(assert_published_aggregate(scan)-def test_complete_prepared_deep_scan_after_count_migration(- workbench_api, workbench_db, reviewed_deep_scan-):+def test_complete_prepared_deep_scan_after_count_migration(workbench_db, reviewed_deep_scan):scan = reviewed_deep_scan- complete(workbench_api, workbench_db, scan, prepare_only=True)+ scan.complete(prepare_only=True)count = "SELECT terminal_reviewed_surface_count FROM deep_scan_runs WHERE scan_id = ?"clear = "UPDATE deep_scan_runs SET terminal_reviewed_surface_count = NULL WHERE scan_id = ?"# A seal produced before the migration has no stored count.with workbench_db:workbench_db.execute(clear, (scan.scan_id,))- complete(workbench_api, workbench_db, scan)+ scan.complete()sealed = json.loads((scan.scan_dir / "scan-manifest.json").read_text())expected = 1 if "coverageRef" in sealed["scan"] else Noneassert workbench_db.execute(count, (scan.scan_id,)).fetchone()[0] == expectedwith workbench_db:workbench_db.execute(clear, (scan.scan_id,))- complete(workbench_api, workbench_db, scan)+ scan.complete()assert workbench_db.execute(count, (scan.scan_id,)).fetchone()[0] is None@@ -458,7 +448,7 @@ def test_complete_sealed_deep_scan_with_inline_review_state(sealed["scan"]["reviewState"] = statepath.write_text(json.dumps(sealed, indent=2, sort_keys=True) + "\n")- complete(workbench_api, workbench_db, scan)+ scan.complete()query = "SELECT terminal_reviewed_surface_count FROM deep_scan_runs WHERE scan_id = ?"assert workbench_db.execute(query, (scan.scan_id,)).fetchone()[0] == count@@ -468,7 +458,7 @@ def test_stopping_prepared_deep_scan_clears_its_surface_count(workbench_api, workbench_db, reviewed_deep_scan, stop):scan = reviewed_deep_scan- complete(workbench_api, workbench_db, scan, prepare_only=True)+ scan.complete(prepare_only=True)count = "SELECT terminal_reviewed_surface_count FROM deep_scan_runs WHERE scan_id = ?"assert workbench_db.execute(count, (scan.scan_id,)).fetchone()[0] == 1if stop == "cancel":@@ -478,14 +468,14 @@ def test_stopping_prepared_deep_scan_clears_its_surface_count(Namespace(scan_id=scan.scan_id, thread_id=None),)else:- fail_scan(workbench_api, workbench_db, scan)+ scan.fail()assert workbench_db.execute(count, (scan.scan_id,)).fetchone()[0] is None@pytest.mark.parametrize("mode", ["standard", "deep"])@pytest.mark.parametrize("after_sealing", [False, True])def test_completion_captures_target_warnings_before_first_seal(- workbench_api, workbench_db, publication_scan, monkeypatch, tmp_path, mode, after_sealing+ workbench_api, publication_scan, monkeypatch, tmp_path, mode, after_sealing):scan = publication_scan(mode=mode)source = tmp_path / "target" / "subdir" / "extract.py"@@ -500,13 +490,13 @@ def test_completion_captures_target_warnings_before_first_seal(with monkeypatch.context() as patch:patch.setitem(completion_globals, "_prepare_scan_finalization", prepare_then_change_target)- complete(workbench_api, workbench_db, scan, prepare_only=True)+ scan.complete(prepare_only=True)manifest_path = scan.scan_dir / "scan-manifest.json"sealed_bytes = manifest_path.read_bytes()if after_sealing:source.write_text("# Changed after preparation sealed the scan\n")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert completed["progress"]["status"] == "complete"assert len(completed["warnings"]) == 1@@ -526,13 +516,11 @@ def test_completion_captures_target_warnings_before_first_seal(@pytest.mark.skipif(os.name == "nt", reason="Unix filenames can contain non-UTF-8 bytes")@pytest.mark.parametrize("mode", ["standard", "deep"])-def test_completion_preserves_non_utf8_filename_warning(- workbench_api, workbench_db, publication_scan, tmp_path, mode-):+def test_completion_preserves_non_utf8_filename_warning(publication_scan, tmp_path, mode):scan = publication_scan(mode=mode)os.mkfifo(os.fsencode(tmp_path / "target") + b"/new-pipe-\xff")- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert completed["progress"]["status"] == "complete"warning = completed["warnings"][0]@@ -547,7 +535,7 @@ def test_completion_preserves_non_utf8_filename_warning(assert invocation["toolExecutionNotifications"] == [{"level": "warning", "message": {"text": portable_warning}}]- complete(workbench_api, workbench_db, scan)+ scan.complete()assert manifest_path.read_bytes() == sealed_bytes@@ -581,7 +569,7 @@ def test_preseal_target_warning_survives_failed_publication(with monkeypatch.context() as patch:patch.setitem(finalizer_globals, "write_scan_local_bytes", fail_first_report)with pytest.raises(SystemExit, match="Synthetic first report write failure"):- complete(workbench_api, workbench_db, scan)+ scan.complete()row = workbench_db.execute("SELECT * FROM scans WHERE id = ?", (scan.scan_id,)).fetchone()assert row["status"] == ("failed" if mode == "standard" else "running")@@ -589,7 +577,7 @@ def test_preseal_target_warning_survives_failed_publication(# The failed output write must not erase an earlier observation if the# next attempt finds that the target has returned to its original state.source_path.write_bytes(source_before)- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert completed["progress"]["status"] == "complete"row = workbench_db.execute("SELECT * FROM scans WHERE id = ?", (scan.scan_id,)).fetchone()manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())@@ -615,7 +603,7 @@ def test_preseal_target_warning_survives_failed_publication(@pytest.mark.cross_platform@pytest.mark.parametrize("scope", [".", "subdir"])def test_deep_completion_preserves_findings_with_missing_optional_writeups(- workbench_api, workbench_db, publication_scan, scope+ workbench_db, publication_scan, scope):scan = publication_scan(scope=scope)valid_path = "findings/valid/valid.md"@@ -641,7 +629,7 @@ def test_deep_completion_preserves_findings_with_missing_optional_writeups((scan.scan_dir / "findings.json").write_text(json.dumps({"findings": scan.findings}))original_files = {valid_file: valid_file.read_bytes()}for kind in ("discovery", "dedup"):- result = add_worker(workbench_db, scan)+ result = scan.add_worker()result.write_text(json.dumps(saved_draft(scan.scan_id, findings=scan.findings, complete=True)))@@ -655,8 +643,8 @@ def test_deep_completion_preserves_findings_with_missing_optional_writeups()# Preparing and then completing also exercises replay of the sealed parent.- prepared = complete(workbench_api, workbench_db, scan, prepare_only=True)- completed = complete(workbench_api, workbench_db, scan)+ prepared = scan.complete(prepare_only=True)+ completed = scan.complete()assert completed["progress"]["status"] == "complete"assert completed["findingCount"] == 3@@ -681,9 +669,7 @@ def test_deep_completion_preserves_findings_with_missing_optional_writeups(@pytest.mark.cross_platform@pytest.mark.parametrize("invalid", ["unsafe-path", "directory"])-def test_deep_scope_filter_does_not_hide_invalid_writeups(- workbench_api, workbench_db, publication_scan, invalid-):+def test_deep_scope_filter_does_not_hide_invalid_writeups(workbench_db, publication_scan, invalid):scan = publication_scan(scope="subdir")finding = scan.findings[0]finding["locations"] = [{"path": "unrelated/extract.py", "startLine": 1}]@@ -696,7 +682,7 @@ def test_deep_scope_filter_does_not_hide_invalid_writeups((scan.scan_dir / "findings.json").write_text(json.dumps({"findings": scan.findings}))with pytest.raises(SystemExit, match="reportPath|regular non-symlink file"):- complete(workbench_api, workbench_db, scan)+ scan.complete()assert (workbench_db.execute("SELECT status FROM scans WHERE id = ?", (scan.scan_id,)).fetchone()[0]!= "complete"@@ -706,7 +692,7 @@ def test_deep_scope_filter_does_not_hide_invalid_writeups(@pytest.mark.cross_platform@pytest.mark.parametrize("invalid", ["finding", "coverage"])def test_missing_deep_writeup_does_not_enable_general_recovery(- workbench_api, workbench_db, publication_scan, invalid+ workbench_db, publication_scan, invalid):scan = publication_scan()scan.findings[0]["writeup"] = {"reportPath": "findings/missing/missing.md"}@@ -718,7 +704,7 @@ def test_missing_deep_writeup_does_not_enable_general_recovery((scan.scan_dir / "coverage.json").write_text(json.dumps(scan.coverage))with pytest.raises(SystemExit, match="severity|surfaces"):- complete(workbench_api, workbench_db, scan)+ scan.complete()assert (workbench_db.execute("SELECT status FROM scans WHERE id = ?", (scan.scan_id,)).fetchone()[0]!= "complete"@@ -743,7 +729,7 @@ def test_missing_deep_writeup_does_not_enable_general_recovery(],)def test_stopped_deep_scan_still_salvages_saved_findings(- workbench_api, workbench_db, publication_scan, source, scope, has_parent+ workbench_db, publication_scan, source, scope, has_parent):scan = publication_scan(scope=scope)if not has_parent:@@ -755,9 +741,7 @@ def test_stopped_deep_scan_still_salvages_saved_findings("terminal_reason = NULL, completed_at = NULL WHERE scan_id = ?",(scan.scan_id,),)- result = add_worker(- workbench_db, scan, status="succeeded" if source == "deep-reducer-result" else "running"- )+ result = scan.add_worker(status="succeeded" if source == "deep-reducer-result" else "running")if source.startswith("deep-reducer"):with workbench_db:workbench_db.execute(@@ -787,7 +771,7 @@ def test_stopped_deep_scan_still_salvages_saved_findings(result.write_text("{interrupted worker output")result_bytes = result.read_bytes()- stopped = fail_scan(workbench_api, workbench_db, scan, "Scan interrupted.")+ stopped = scan.fail("Scan interrupted.")assert stopped["progress"]["status"] == "failed"manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())@@ -801,7 +785,7 @@ def test_stopped_deep_scan_still_salvages_saved_findings(artifact_names = ("scan-manifest.json", "findings.json")published = {name: (scan.scan_dir / name).read_bytes() for name in artifact_names}- recovered = recover_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.recover()assert recovered["findingCount"] == len(expected_summaries)assert {name: (scan.scan_dir / name).read_bytes() for name in artifact_names} == publishedif checkpoint is not None:@@ -811,7 +795,7 @@ def test_stopped_deep_scan_still_salvages_saved_findings(@pytest.mark.parametrize("source", ["result", "checkpoint", "parent-checkpoint"])def test_stopped_deep_scan_ignores_non_reducer_sources_without_coverage(- workbench_api, workbench_db, publication_scan, source+ workbench_db, publication_scan, source):scan = publication_scan()with workbench_db:@@ -831,9 +815,7 @@ def test_stopped_deep_scan_ignores_non_reducer_sources_without_coverage(if source == "parent-checkpoint":source_path = write_checkpoint(scan.scan_dir / "checkpoints", saved)else:- result = add_worker(- workbench_db, scan, status="succeeded" if source == "result" else "running"- )+ result = scan.add_worker(status="succeeded" if source == "result" else "running")if source == "result":result.write_text(json.dumps(saved))source_path = result@@ -842,7 +824,7 @@ def test_stopped_deep_scan_ignores_non_reducer_sources_without_coverage(source_bytes = source_path.read_bytes()source_relative = source_path.relative_to(scan.scan_dir).as_posix()- stopped = fail_scan(workbench_api, workbench_db, scan, "Scan interrupted.")+ stopped = scan.fail("Scan interrupted.")assert stopped["progress"]["status"] == "failed"assert stopped["resultsRecoveryNeeded"] is False@@ -859,7 +841,7 @@ def test_stopped_deep_scan_ignores_non_reducer_sources_without_coverage(artifact_names = ("scan-manifest.json", "findings.json")published = {name: (scan.scan_dir / name).read_bytes() for name in artifact_names}- recovered = recover_scan_results(workbench_api, workbench_db, scan)+ recovered = scan.recover()assert recovered["findingCount"] == 1assert recovered["resultsRecoveryNeeded"] is False@@ -867,14 +849,12 @@ def test_stopped_deep_scan_ignores_non_reducer_sources_without_coverage(assert source_path.read_bytes() == source_bytes-def test_standard_publication_preserves_deferred_work(- workbench_api, workbench_db, publication_scan-):+def test_standard_publication_preserves_deferred_work(publication_scan):scan = publication_scan(mode="standard")scan.coverage["deferred"] = [{"id": "remaining-review", "reason": "Another surface remains."}](scan.scan_dir / "coverage.json").write_text(json.dumps(scan.coverage))- complete(workbench_api, workbench_db, scan)+ scan.complete()assert_published_aggregate(scan)@@ -901,7 +881,7 @@ def test_deep_publication_write_failure_keeps_original_terminal_cause(patch.setitem(finalizer_globals, "write_scan_local_bytes", fail_report)for _ in range(2):with pytest.raises(SystemExit, match="Synthetic report write interruption"):- complete(workbench_api, workbench_db, scan)+ scan.complete()warnings = json.loads(workbench_db.execute("SELECT completion_warnings_json FROM scans WHERE id = ?", (scan.scan_id,)@@ -923,7 +903,7 @@ def test_deep_publication_write_failure_keeps_original_terminal_cause({"id": "entry", "label": "Entry review", "disposition": "no_issue_found", "receiptRefs": []}](scan.scan_dir / "coverage.json").write_text(json.dumps(scan.coverage))- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert completed["progress"]["status"] == "complete"assert (workbench_db.execute(@@ -941,7 +921,7 @@ def test_deep_publication_write_failure_keeps_original_terminal_cause(@pytest.mark.parametrize("resumed", [False, True])@pytest.mark.parametrize("scope", ["subdir", "./subdir/", "subdir/extract.py"])def test_scoped_deep_completion_filters_old_parent_and_preserves_accepted_sources(- workbench_api, workbench_db, publication_scan, resumed, scope+ workbench_db, publication_scan, resumed, scope):scan = publication_scan(paths=[scope, "background jobs"])findings = []@@ -959,7 +939,7 @@ def test_scoped_deep_completion_filters_old_parent_and_preserves_accepted_sourceaccepted = []if resumed:for kind in ("discovery", "dedup"):- result = add_worker(workbench_db, scan)+ result = scan.add_worker()result.write_text(json.dumps({"scanId": scan.scan_id, "findings": findings}))with workbench_db:workbench_db.execute(@@ -968,7 +948,7 @@ def test_scoped_deep_completion_filters_old_parent_and_preserves_accepted_source(kind, str(result)),)accepted.append((result, result.read_bytes()))- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert completed["progress"]["status"] == "complete"paths = [row["locations"][0]["path"]@@ -1003,21 +983,19 @@ def test_resumed_sealed_out_of_scope_deep_scan_is_not_rewritten(names = ["scan-manifest.json", "findings.json", "coverage.json", "report.md"]original = {name: (scan.scan_dir / name).read_bytes() for name in names}with pytest.raises(SystemExit, match="sealed Deep scan contains findings outside"):- complete(workbench_api, workbench_db, scan)+ scan.complete()assert {name: (scan.scan_dir / name).read_bytes() for name in names} == original# Historical sealed reads are retained; this change is not a migration.workbench_api["finalize_scan"](scan.scan_dir)assert {name: (scan.scan_dir / name).read_bytes() for name in names} == original-def test_deep_scope_filter_does_not_hide_unsafe_finding_locations(- workbench_api, workbench_db, publication_scan-):+def test_deep_scope_filter_does_not_hide_unsafe_finding_locations(publication_scan):scan = publication_scan(paths=["subdir", "background jobs"])scan.findings[0]["locations"] = [{"path": "../outside.py", "startLine": 1}](scan.scan_dir / "findings.json").write_text(json.dumps({"findings": scan.findings}))with pytest.raises(SystemExit, match="path"):- complete(workbench_api, workbench_db, scan)+ scan.complete()manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())assert manifest["scan"].get("status") != "completed"@@ -1041,7 +1019,7 @@ def test_deep_publication_preserves_original_scope_kinds_after_checkout_changes((scan.scan_dir / "findings.json").write_text(json.dumps({"findings": findings}))accepted = []for kind in ("discovery", "dedup"):- result = add_worker(workbench_db, scan)+ result = scan.add_worker()result.write_text(json.dumps(saved_draft(scan.scan_id, findings=findings, complete=True)))with workbench_db:workbench_db.execute(@@ -1064,9 +1042,9 @@ def test_deep_publication_preserves_original_scope_kinds_after_checkout_changes(== file_paths)if stopped:- fail_scan(workbench_api, workbench_db, scan, "interrupted")+ scan.fail("interrupted")else:- assert complete(workbench_api, workbench_db, scan)["progress"]["status"] == "complete"+ assert scan.complete()["progress"]["status"] == "complete"expected_paths = ["subdir/extract.py"] + ([] if file_paths else ["subdir/extract.py/generated"])assert [item["locations"][0]["path"]@@ -1095,7 +1073,7 @@ def test_legacy_deep_scope_does_not_reclassify_a_changed_checkout(target = Path(row["target_path"]) / "subdir"shutil.rmtree(target)target.write_text("new checkout file\n")- assert complete(workbench_api, workbench_db, scan)["progress"]["status"] == "complete"+ assert scan.complete()["progress"]["status"] == "complete"findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert len(findings) == 1@@ -1107,22 +1085,18 @@ def test_legacy_deep_scope_does_not_reclassify_a_changed_checkout(("./subdir/extract.py", "subdir/extract.py/generated"),],)-def test_stopped_deep_projection_filters_the_canonical_parent(- workbench_api, workbench_db, publication_scan, scope, location-):+def test_stopped_deep_projection_filters_the_canonical_parent(publication_scan, scope, location):scan = publication_scan(paths=[scope, "background jobs"])scan.findings[0]["locations"] = [{"path": location, "startLine": 1}](scan.scan_dir / "findings.json").write_text(json.dumps({"findings": scan.findings}))- stopped = fail_scan(workbench_api, workbench_db, scan, "Scan interrupted.")+ stopped = scan.fail("Scan interrupted.")assert stopped["progress"]["status"] == "failed"assert stopped["findingCount"] == 0assert json.loads((scan.scan_dir / "findings.json").read_text())["findings"] == []@pytest.mark.parametrize("scope", ["subdir/extract.py", "./subdir/extract.py"])-def test_stopped_file_scope_filters_siblings_before_assigning_identity(- workbench_api, workbench_db, publication_scan, scope-):+def test_stopped_file_scope_filters_siblings_before_assigning_identity(publication_scan, scope):scan = publication_scan(paths=[scope])finding = copy.deepcopy(scan.findings[0])for key in ("identity", "findingId", "occurrenceId", "fingerprints"):@@ -1137,13 +1111,13 @@ def test_stopped_file_scope_filters_siblings_before_assigning_identity(checkpoint_bytes = checkpoint.read_bytes()for name in ("scan-manifest.json", "findings.json", "coverage.json"):(scan.scan_dir / name).unlink()- fail_scan(workbench_api, workbench_db, scan, "Interrupted.")+ scan.fail("Interrupted.")findings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert len(findings) == 1assert findings[0]["locations"][0]["path"] == "subdir/extract.py"assert findings[0]["identity"] == {"anchor": "candidate-review"}names = ("scan-manifest.json", "findings.json")published = {name: (scan.scan_dir / name).read_bytes() for name in names}- recover_scan_results(workbench_api, workbench_db, scan)+ scan.recover()assert {name: (scan.scan_dir / name).read_bytes() for name in names} == publishedassert checkpoint.read_bytes() == checkpoint_bytesdiff --git a/plugins/codex-security/tests/test_partial_coverage_projection.py b/plugins/codex-security/tests/test_partial_coverage_projection.pyindex ebd87c3bc..decb01507 100644--- a/plugins/codex-security/tests/test_partial_coverage_projection.py+++ b/plugins/codex-security/tests/test_partial_coverage_projection.py@@ -8,13 +8,17 @@ import uuidfrom argparse import Namespaceimport pytest-from test_deep_scan_successful_publication import add_workerfrom test_deep_scan_successful_publication import publication_scan as publication_scan-from workbench_test_support import read_review_state, write_checkpoint+from workbench_test_support import (+ read_review_state,+ select_checkpoint,+ write_checkpoint,+ write_result_draft,+)@pytest.fixture-def stop_scan(workbench_api, workbench_db, monkeypatch):+def stop_scan(workbench_api, monkeypatch):saved = workbench_api["saved_results"]def fail_publication(*args, **kwargs):@@ -24,13 +28,7 @@ def stop_scan(workbench_api, workbench_db, monkeypatch):with monkeypatch.context() as patch:if interrupt_publication:patch.setattr(saved, "_write_prepared_scan_finalization", fail_publication)- stopped = saved.fail_scan(- workbench_api["_WORKBENCH_DB_CONTEXT"],- workbench_db,- Namespace(- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."- ),- )["scan"]+ stopped = scan.fail()assert stopped["resultsRecoveryNeeded"] is interrupt_publicationreturn stop@@ -41,7 +39,6 @@ def stop_scan(workbench_api, workbench_db, monkeypatch):@pytest.mark.parametrize("newer_parent", [False, True])@pytest.mark.parametrize("retry_publication", [False, True])def test_parent_candidate_outcome_compares_headless_worker_chronology(- workbench_api,workbench_db,publication_scan,stop_scan,@@ -51,7 +48,7 @@ def test_parent_candidate_outcome_compares_headless_worker_chronology(retry_publication,):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.namewith workbench_db:workbench_db.execute(@@ -77,15 +74,11 @@ def test_parent_candidate_outcome_compares_headless_worker_chronology("deferred": [task],},}- result.write_text(json.dumps(draft))- os.utime(result, ns=(300, 300))+ write_result_draft(result, draft, mtime_ns=300)originals = {result: result.read_bytes()}if has_head:- checkpoint = write_checkpoint(result.parent / "checkpoints", draft)- os.utime(checkpoint, ns=(300, 300))- head = result.parent / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(300, 300))+ checkpoint = write_checkpoint(result.parent / "checkpoints", draft, mtime_ns=300)+ head = select_checkpoint(result.parent, checkpoint, mtime_ns=300)originals.update({path: path.read_bytes() for path in (checkpoint, head)})(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))parent = scan.scan_dir / "coverage.json"@@ -114,9 +107,7 @@ def test_parent_candidate_outcome_compares_headless_worker_chronology(modified = 400 if newer_parent else 200os.utime(parent, ns=(modified, modified))stop_scan(scan, retry_publication)- workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )+ scan.recover()coverage = read_review_state(workbench_db, scan.scan_dir)pending = [row for row in coverage["deferred"] if row.get("reason") == task["reason"]]assert len(pending) == int(not newer_parent)@@ -133,10 +124,10 @@ def test_parent_candidate_outcome_compares_headless_worker_chronology(@pytest.mark.parametrize("tagged_surface", [False, True])@pytest.mark.parametrize("merge_state", ["buffered", "merging", "merged"])def test_missing_deferred_projection_links_first_duplicate_surface(- workbench_api, workbench_db, publication_scan, parent_surfaces, merge_state, tagged_surface+ workbench_db, publication_scan, parent_surfaces, merge_state, tagged_surface):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.namewith workbench_db:workbench_db.execute(@@ -229,20 +220,13 @@ def test_missing_deferred_projection_links_first_duplicate_surface(earlier["coverage"]["resolvedDeferred"] = [{"id": deferred["id"], "reason": "Earlier source follow-up completed."}]- checkpoint = write_checkpoint(result.parent / "checkpoints", earlier)- os.utime(checkpoint, ns=(100, 100))+ write_checkpoint(result.parent / "checkpoints", earlier, mtime_ns=100)os.utime(result, ns=(300, 300))if parent_surfaces != "no-parent":os.utime(scan.scan_dir / "coverage.json", ns=(200, 200))- workbench_api["saved_results"].fail_scan(- workbench_api["_WORKBENCH_DB_CONTEXT"],- workbench_db,- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),- )+ scan.fail()published = (scan.scan_dir / "scan-manifest.json").read_bytes()- recovered = workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )["scan"]+ recovered = scan.recover()assert recovered["resultsRecoveryNeeded"] is Falseassert (scan.scan_dir / "scan-manifest.json").read_bytes() == publishedcoverage = read_review_state(workbench_db, scan.scan_dir)@@ -282,9 +266,7 @@ def test_missing_deferred_projection_links_first_duplicate_surface(},)original_late = late.read_bytes()- workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )+ scan.recover()manifest = json.loads((scan.scan_dir / "scan-manifest.json").read_bytes())assert late.relative_to(scan.scan_dir).as_posix() in manifest["scan"]["preservedSources"]assert read_review_state(workbench_db, scan.scan_dir) == coverage@@ -294,10 +276,10 @@ def test_missing_deferred_projection_links_first_duplicate_surface(@pytest.mark.parametrize("parent_review", [False, True])def test_missing_accepted_result_does_not_project_an_archived_attempt(- workbench_api, workbench_db, publication_scan, parent_review+ workbench_db, publication_scan, parent_review):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.nameresult = scan.scan_dir / "artifacts" / worker_id / "output" / "result.json"result.parent.mkdir(parents=True)@@ -344,14 +326,8 @@ def test_missing_accepted_result_does_not_project_an_archived_attempt(else:for name in ("scan-manifest.json", "findings.json", "coverage.json"):(scan.scan_dir / name).unlink()- workbench_api["saved_results"].fail_scan(- workbench_api["_WORKBENCH_DB_CONTEXT"],- workbench_db,- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),- )- workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )+ scan.fail()+ scan.recover()coverage = read_review_state(workbench_db, scan.scan_dir)surface = next(item for item in coverage["surfaces"] if item["label"] == "Prior unfinished review"@@ -370,11 +346,11 @@ def test_missing_accepted_result_does_not_project_an_archived_attempt(@pytest.mark.parametrize("failed_first", [False, True])def test_failed_worker_reviews_do_not_replace_accepted_reviews(- workbench_api, workbench_db, publication_scan, failed_first+ workbench_db, publication_scan, failed_first):scan = publication_scan()# These fixture rows share created_at; recovery reads them in worker-ID order.- first, second = sorted([add_worker(workbench_db, scan), add_worker(workbench_db, scan)])+ first, second = sorted([scan.add_worker(), scan.add_worker()])failed, accepted = (first, second) if failed_first else (second, first)with workbench_db:workbench_db.execute(@@ -426,11 +402,7 @@ def test_failed_worker_reviews_do_not_replace_accepted_reviews(drafts[label] = draftfor name in ("scan-manifest.json", "findings.json", "coverage.json"):(scan.scan_dir / name).unlink()- workbench_api["saved_results"].fail_scan(- workbench_api["_WORKBENCH_DB_CONTEXT"],- workbench_db,- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),- )+ scan.fail()coverage = read_review_state(workbench_db, scan.scan_dir)assert coverage["reviews"] == [review]for field in ("surfaces", "deferred", "explicitExclusions", "openQuestions"):@@ -447,9 +419,7 @@ def test_failed_worker_reviews_do_not_replace_accepted_reviews(item.get("reason") == "accepted source needs proof." for item in coverage["deferred"])published = (scan.scan_dir / "scan-manifest.json").read_bytes()- workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )+ scan.recover()assert (scan.scan_dir / "scan-manifest.json").read_bytes() == publishedlate = write_checkpoint(accepted.parent / "checkpoints",@@ -460,9 +430,7 @@ def test_failed_worker_reviews_do_not_replace_accepted_reviews(},)originals[late] = late.read_bytes()- recovered = workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )["scan"]+ recovered = scan.recover()assert recovered["resultsRecoveryNeeded"] is Falseassert read_review_state(workbench_db, scan.scan_dir) == coveragemanifest = json.loads((scan.scan_dir / "scan-manifest.json").read_text())@@ -475,7 +443,6 @@ def test_failed_worker_reviews_do_not_replace_accepted_reviews(@pytest.mark.parametrize("retained_deferred", [False, True])@pytest.mark.parametrize("idless_surface", [False, True])def test_missing_projection_keeps_surface_links_and_independent_reviews(- workbench_api,workbench_db,publication_scan,worker_count,@@ -503,7 +470,7 @@ def test_missing_projection_keeps_surface_links_and_independent_reviews() + [surface]reviews, surfaces, deferred_records, originals = [], [], [], {}for _ in range(worker_count):- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.namereviews.append({"workerId": worker_id, "attempt": 1, "completeness": "partial"})if idless_surface:@@ -562,21 +529,8 @@ def test_missing_projection_keeps_surface_links_and_independent_reviews(}))- saved = workbench_api["saved_results"]- context = workbench_api["_WORKBENCH_DB_CONTEXT"]- saved.fail_scan(- context,- workbench_db,- Namespace(- scan_id=scan.scan_id,- claim_token=None,- cost_json=None,- message="Stopped.",- ),- )- recovered = saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))[- "scan"- ]+ scan.fail()+ recovered = scan.recover()assert recovered["resultsRecoveryNeeded"] is Falsecoverage = read_review_state(workbench_db, scan.scan_dir)pending = [item for item in coverage["deferred"] if item.get("reason") == deferred["reason"]]@@ -601,17 +555,17 @@ def test_missing_projection_keeps_surface_links_and_independent_reviews(assert "completeness" not in coverageassert all(path.read_bytes() == data for path, data in originals.items())published = (scan.scan_dir / "scan-manifest.json").read_bytes()- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))+ scan.recover()assert (scan.scan_dir / "scan-manifest.json").read_bytes() == published@pytest.mark.parametrize("attempt", [1, 2])@pytest.mark.parametrize("retry_publication", [False, True])def test_previous_attempt_resolution_does_not_clear_current_gap(- workbench_api, workbench_db, publication_scan, attempt, retry_publication, stop_scan+ workbench_db, publication_scan, attempt, retry_publication, stop_scan):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.namedeferred = {"candidateId": "candidate-1", "reason": "Validate the current attempt."}result.write_text(@@ -658,9 +612,7 @@ def test_previous_attempt_resolution_does_not_clear_current_gap(parent_observed = observed + 2_000_000_000os.utime(parent_path, ns=(parent_observed, parent_observed))stop_scan(scan, retry_publication)- recovered = workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )["scan"]+ recovered = scan.recover()assert recovered["resultsRecoveryNeeded"] is Falsecoverage = read_review_state(workbench_db, scan.scan_dir)pending = [item for item in coverage["deferred"] if item.get("reason") == deferred["reason"]]@@ -670,10 +622,10 @@ def test_previous_attempt_resolution_does_not_clear_current_gap(@pytest.mark.parametrize("selected_checkpoint", [False, True])def test_recovery_compares_open_questions_using_canonical_normalization(- workbench_api, workbench_db, publication_scan, selected_checkpoint+ workbench_db, publication_scan, selected_checkpoint):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.namequestions = [" Which deployment controls apply? ",@@ -694,9 +646,7 @@ def test_recovery_compares_open_questions_using_canonical_normalization(checkpoint = write_checkpoint(result.parent / "checkpoints", json.loads(result.read_text()))os.utime(result, ns=(100, 100))os.utime(checkpoint, ns=(150, 150))- (result.parent / "checkpoint-head.json").write_text(- json.dumps({"checkpoint": checkpoint.name})- )+ select_checkpoint(result.parent, checkpoint)projected = [{"question": question, "provenance": {"workerId": worker_id, "attempt": 1}}for question in ("Which deployment controls apply?", "Which runtime settings apply?")@@ -710,14 +660,8 @@ def test_recovery_compares_open_questions_using_canonical_normalization(}))- workbench_api["saved_results"].fail_scan(- workbench_api["_WORKBENCH_DB_CONTEXT"],- workbench_db,- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),- )- recovered = workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )["scan"]+ scan.fail()+ recovered = scan.recover()assert recovered["resultsRecoveryNeeded"] is Falsecoverage = read_review_state(workbench_db, scan.scan_dir)assert coverage["openQuestions"] == projected@@ -727,10 +671,10 @@ def test_recovery_compares_open_questions_using_canonical_normalization(@pytest.mark.parametrize("parent_projection", ["raw", "projected", "missing"])@pytest.mark.parametrize("retry_publication", [False, True])def test_rejected_retry_preserves_original_finding_history(- workbench_api, workbench_db, publication_scan, stop_scan, parent_projection, retry_publication+ workbench_db, publication_scan, stop_scan, parent_projection, retry_publication):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.namecandidate_id = "candidate-1"finding = copy.deepcopy(scan.findings[0])@@ -766,7 +710,7 @@ def test_rejected_retry_preserves_original_finding_history()with workbench_db:workbench_db.execute("UPDATE deep_scan_workers SET attempt = 2 WHERE id = ?", (worker_id,))- other_result = add_worker(workbench_db, scan)+ other_result = scan.add_worker()other_result.write_text(json.dumps({"scanId": scan.scan_id, "findings": [], "coverage": scan.coverage}))@@ -808,9 +752,7 @@ def test_rejected_retry_preserves_original_finding_history((scan.scan_dir / name).unlink()originals = {path: path.read_bytes() for path in (previous, result, other_result)}stop_scan(scan, retry_publication)- recovered = workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )["scan"]+ recovered = scan.recover()assert recovered["resultsRecoveryNeeded"] is Falseassert recovered["findingCount"] == 0coverage = read_review_state(workbench_db, scan.scan_dir)@@ -824,9 +766,7 @@ def test_rejected_retry_preserves_original_finding_history(assert "previousFindings" not in itemassert all(path.read_bytes() == original for path, original in originals.items())published = (scan.scan_dir / "scan-manifest.json").read_bytes()- workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )+ scan.recover()assert (scan.scan_dir / "scan-manifest.json").read_bytes() == published@@ -835,11 +775,11 @@ def test_rejected_retry_preserves_original_finding_history("recovery", [None, "retry", "missing", "changed", "incomplete", "late", "fallback-late"])def test_stopped_recovery_keeps_current_parent_projection(- workbench_api, workbench_db, publication_scan, stop_scan, older_complete, recovery+ workbench_db, publication_scan, stop_scan, older_complete, recovery):retry_publication = recovery not in {None, "fallback-late"}scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.namedeferred = {"candidateId": "candidate-1", "reason": "The current review remains unresolved."}result.write_text(@@ -852,7 +792,7 @@ def test_stopped_recovery_keeps_current_parent_projection(}))- reducer = add_worker(workbench_db, scan)+ reducer = scan.add_worker()reducer.write_text(json.dumps({"scanId": scan.scan_id, "findings": []}))reducer_checkpoint = write_checkpoint(reducer.parent / "checkpoints", json.loads(reducer.read_text())@@ -944,9 +884,7 @@ def test_stopped_recovery_keeps_current_parent_projection(},)original_sources[late] = late.read_bytes()- recovered = workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )["scan"]+ recovered = scan.recover()assert recovered["resultsRecoveryNeeded"] is Falsecoverage = read_review_state(workbench_db, scan.scan_dir)expected_questions = [] if recovery == "fallback-late" else [question]@@ -968,18 +906,16 @@ def test_stopped_recovery_keeps_current_parent_projection(assert all(path.read_bytes() == original for path, original in original_sources.items())assert all(path.read_bytes() == original for path, original in frozen_sources.items())published = (scan.scan_dir / "scan-manifest.json").read_bytes()- workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )+ scan.recover()assert (scan.scan_dir / "scan-manifest.json").read_bytes() == published@pytest.mark.parametrize("select_head", [False, True])def test_matching_accepted_checkpoint_preserves_receipt_path(- workbench_api, workbench_db, publication_scan, select_head+ workbench_db, publication_scan, select_head):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.nameresult = (scan.scan_dir@@ -1021,17 +957,11 @@ def test_matching_accepted_checkpoint_preserves_receipt_path(checkpoint = write_checkpoint(result.parent / "checkpoints", draft)os.utime(result, ns=(100, 100))os.utime(checkpoint, ns=(150, 150))- head = result.parent / "checkpoint-head.json"if select_head:- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(200, 200))+ select_checkpoint(result.parent, checkpoint, mtime_ns=200)for name in ("scan-manifest.json", "findings.json", "coverage.json"):(scan.scan_dir / name).unlink()- workbench_api["saved_results"].fail_scan(- workbench_api["_WORKBENCH_DB_CONTEXT"],- workbench_db,- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),- )+ scan.fail()coverage = read_review_state(workbench_db, scan.scan_dir)matching = [row for row in coverage["surfaces"] if row.get("label") == "Accepted source review"]assert matching@@ -1045,10 +975,10 @@ def test_matching_accepted_checkpoint_preserves_receipt_path(@pytest.mark.parametrize("retry_publication", [False, True])def test_accepted_worker_missing_completeness_keeps_valid_findings(- workbench_api, workbench_db, publication_scan, stop_scan, retry_publication+ workbench_db, publication_scan, stop_scan, retry_publication):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.namefinding = copy.deepcopy(scan.findings[0])finding["summary"] = "Synthetic accepted worker evidence."@@ -1067,9 +997,7 @@ def test_accepted_worker_missing_completeness_keeps_valid_findings()original = result.read_bytes()stop_scan(scan, retry_publication)- recovered = workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )["scan"]+ recovered = scan.recover()assert recovered["resultsRecoveryNeeded"] is Falsefindings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert any(row["summary"] == finding["summary"] for row in findings)@@ -1081,10 +1009,10 @@ def test_accepted_worker_missing_completeness_keeps_valid_findings(@pytest.mark.parametrize("retry_publication", [False, True])def test_idless_accepted_surface_matches_projection_after_checkpoint_id_inference(- workbench_api, workbench_db, publication_scan, stop_scan, retry_publication+ workbench_db, publication_scan, stop_scan, retry_publication):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.namesurface = {"label": "Accepted source review",@@ -1105,8 +1033,8 @@ def test_idless_accepted_surface_matches_projection_after_checkpoint_id_inferenc"complete": False,"coverage": {**scan.coverage, "surfaces": [{**surface, "id": "older-source-id"}]},},+ mtime_ns=100,)- os.utime(checkpoint, ns=(100, 100))os.utime(result, ns=(200, 200))projected = {**surface,@@ -1124,16 +1052,12 @@ def test_idless_accepted_surface_matches_projection_after_checkpoint_id_inferenc)originals = {path: path.read_bytes() for path in (result, checkpoint)}stop_scan(scan, retry_publication)- recovered = workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )["scan"]+ recovered = scan.recover()assert recovered["resultsRecoveryNeeded"] is Falsecoverage = read_review_state(workbench_db, scan.scan_dir)assert coverage["surfaces"] == [projected]published = (scan.scan_dir / "scan-manifest.json").read_bytes()- workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )+ scan.recover()assert (scan.scan_dir / "scan-manifest.json").read_bytes() == publishedassert all(path.read_bytes() == original for path, original in originals.items())@@ -1142,8 +1066,6 @@ def test_idless_accepted_surface_matches_projection_after_checkpoint_id_inferenc@pytest.mark.parametrize("retry_publication", [False, True])@pytest.mark.parametrize("selected_checkpoint", [False, True])def test_parent_candidate_provenance_cannot_override_accepted_worker_evidence(- workbench_api,- workbench_db,publication_scan,stop_scan,claimed_worker,@@ -1151,7 +1073,7 @@ def test_parent_candidate_provenance_cannot_override_accepted_worker_evidence(selected_checkpoint,):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.namefinding = copy.deepcopy(scan.findings[0])finding["provenance"]["candidateId"] = "source-candidate"@@ -1169,8 +1091,7 @@ def test_parent_candidate_provenance_cannot_override_accepted_worker_evidence(originals = {result: result.read_bytes()}if selected_checkpoint:checkpoint = write_checkpoint(result.parent / "checkpoints", json.loads(result.read_text()))- head = result.parent / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ head = select_checkpoint(result.parent, checkpoint)pending_result = json.loads(result.read_text())pending_result["findings"] = []pending_result["coverage"] = {@@ -1184,8 +1105,7 @@ def test_parent_candidate_provenance_cannot_override_accepted_worker_evidence(}],}- result.write_text(json.dumps(pending_result))- os.utime(result, ns=(100, 100))+ write_result_draft(result, pending_result, mtime_ns=100)os.utime(checkpoint, ns=(300, 300))os.utime(head, ns=(300, 300))originals = {path: path.read_bytes() for path in (result, checkpoint, head)}@@ -1213,9 +1133,7 @@ def test_parent_candidate_provenance_cannot_override_accepted_worker_evidence()os.utime(scan.scan_dir / "coverage.json", ns=(400, 400))stop_scan(scan, retry_publication)- recovered = workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )["scan"]+ recovered = scan.recover()assert recovered["resultsRecoveryNeeded"] is Falsefindings = json.loads((scan.scan_dir / "findings.json").read_text())["findings"]assert [row["summary"] for row in findings] == [finding["summary"]]@@ -1225,10 +1143,10 @@ def test_parent_candidate_provenance_cannot_override_accepted_worker_evidence(@pytest.mark.parametrize("other_worker", [False, True])@pytest.mark.parametrize("retry_publication", [False, True])def test_projected_generic_closure_updates_parent_copy_in_its_worker_namespace(- workbench_api, workbench_db, publication_scan, stop_scan, other_worker, retry_publication+ workbench_db, publication_scan, stop_scan, other_worker, retry_publication):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.namesurface = {"id": "source-surface",@@ -1277,7 +1195,7 @@ def test_projected_generic_closure_updates_parent_copy_in_its_worker_namespace(}))- closure_result = add_worker(workbench_db, scan) if other_worker else result+ closure_result = scan.add_worker() if other_worker else resultif other_worker:closure_result.write_text(json.dumps({**draft, "coverage": scan.coverage}))closure = {@@ -1293,14 +1211,10 @@ def test_projected_generic_closure_updates_parent_copy_in_its_worker_namespace(os.utime(result, ns=(100, 100))os.utime(parent_coverage, ns=(200, 200))os.utime(checkpoint, ns=(300, 300))- (closure_result.parent / "checkpoint-head.json").write_text(- json.dumps({"checkpoint": checkpoint.name})- )+ select_checkpoint(closure_result.parent, checkpoint)originals = {path: path.read_bytes() for path in {result, closure_result, checkpoint}}stop_scan(scan, retry_publication)- workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )+ scan.recover()coverage = read_review_state(workbench_db, scan.scan_dir)pending = [row for row in coverage["deferred"] if row.get("reason") == task["reason"]]assert len(pending) == int(other_worker)@@ -1315,9 +1229,7 @@ def test_projected_generic_closure_updates_parent_copy_in_its_worker_namespace()assert all(path.read_bytes() == value for path, value in originals.items())published = (scan.scan_dir / "scan-manifest.json").read_bytes()- workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )+ scan.recover()assert (scan.scan_dir / "scan-manifest.json").read_bytes() == published@@ -1325,7 +1237,6 @@ def test_projected_generic_closure_updates_parent_copy_in_its_worker_namespace(@pytest.mark.parametrize("retry_publication", [False, True])@pytest.mark.parametrize("parent_review", ["earlier", "copied-later", "changed-later"])def test_selected_candidate_outcome_removes_its_projected_parent_pending_rows(- workbench_api,workbench_db,publication_scan,stop_scan,@@ -1334,7 +1245,7 @@ def test_selected_candidate_outcome_removes_its_projected_parent_pending_rows(parent_review,):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.namecandidate = "source-candidate"surface = {@@ -1413,14 +1324,10 @@ def test_selected_candidate_outcome_removes_its_projected_parent_pending_rows(modified = 200 if parent_review == "earlier" else 400os.utime(coverage_path, ns=(modified, modified))os.utime(checkpoint, ns=(300, 300))- head = result.parent / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(300, 300))+ select_checkpoint(result.parent, checkpoint, mtime_ns=300)originals = {path: path.read_bytes() for path in (result, checkpoint)}stop_scan(scan, retry_publication)- workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )+ scan.recover()coverage = read_review_state(workbench_db, scan.scan_dir)if parent_review != "changed-later":assert not any(row.get("reason") == task["reason"] for row in coverage["deferred"])@@ -1436,9 +1343,7 @@ def test_selected_candidate_outcome_removes_its_projected_parent_pending_rows(assert len(findings) == int(disposition == "reported")assert all(path.read_bytes() == value for path, value in originals.items())published = (scan.scan_dir / "scan-manifest.json").read_bytes()- workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )+ scan.recover()assert (scan.scan_dir / "scan-manifest.json").read_bytes() == published@@ -1458,10 +1363,10 @@ def test_selected_candidate_outcome_removes_its_projected_parent_pending_rows(],)def test_selected_worker_projection_keeps_links_and_pending_authority(- workbench_api, workbench_db, publication_scan, stop_scan, change, retry_publication+ workbench_db, publication_scan, stop_scan, change, retry_publication):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.nameprefix = f"{worker_id}-attempt-1"candidate = "source-candidate"@@ -1612,11 +1517,8 @@ def test_selected_worker_projection_keeps_links_and_pending_authority("disposition": "rejected",})- checkpoint = write_checkpoint(result.parent / "checkpoints", updated)- os.utime(checkpoint, ns=(300, 300))- (result.parent / "checkpoint-head.json").write_text(- json.dumps({"checkpoint": checkpoint.name})- )+ checkpoint = write_checkpoint(result.parent / "checkpoints", updated, mtime_ns=300)+ select_checkpoint(result.parent, checkpoint)os.utime(result, ns=(100, 100))os.utime(coverage_path, ns=(200, 200))if change == "reopened mixed receipts":@@ -1629,9 +1531,7 @@ def test_selected_worker_projection_keeps_links_and_pending_authority(path: path.read_bytes() for path in [result] + ([checkpoint] if checkpoint else [])}stop_scan(scan, retry_publication)- workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )+ scan.recover()coverage = read_review_state(workbench_db, scan.scan_dir)pending = [row for row in coverage["deferred"] if row.get("reason") == task["reason"]]assert len(pending) == int(@@ -1673,9 +1573,7 @@ def test_selected_worker_projection_keeps_links_and_pending_authority(}assert all(path.read_bytes() == content for path, content in originals.items())published = (scan.scan_dir / "scan-manifest.json").read_bytes()- workbench_api["saved_results"].recover_scan_results(- workbench_api["_WORKBENCH_DB_CONTEXT"], workbench_db, Namespace(scan_id=scan.scan_id)- )+ scan.recover()assert (scan.scan_dir / "scan-manifest.json").read_bytes() == published@@ -1728,7 +1626,7 @@ def test_linked_archived_receipts_preserve_accepted_surface_links(workbench_api, workbench_db, publication_scan, stop_scan, spelling, parent, retry):scan = publication_scan()- initial = add_worker(workbench_db, scan)+ initial = scan.add_worker()worker_id = initial.parent.nameoutput = scan.scan_dir / "artifacts" / worker_id / "output"output.mkdir(parents=True)@@ -1791,8 +1689,7 @@ def test_linked_archived_receipts_preserve_accepted_surface_links(elif spelling == "omitted":selected["coverage"]["surfaces"] = []checkpoint = write_checkpoint(output / "checkpoints", selected)- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ head = select_checkpoint(output, checkpoint)for path, stamp in ((result, 100), (coverage_path, 200), (checkpoint, 300), (head, 300)):os.utime(path, ns=(stamp, stamp))if not parent:@@ -1837,7 +1734,7 @@ def test_recovery_projects_receipts_from_their_source_directory(retry,):scan = publication_scan()- initial = add_worker(workbench_db, scan)+ initial = scan.add_worker()worker_id = initial.parent.nameoutput = scan.scan_dir / "artifacts" / worker_id / "output"output.mkdir(parents=True)@@ -1908,10 +1805,10 @@ def test_recovery_projects_receipts_from_their_source_directory(def test_deferred_projection_preserves_each_referenced_source_surface(- workbench_api, workbench_db, publication_scan+ workbench_db, publication_scan):scan = publication_scan()- result = add_worker(workbench_db, scan)+ result = scan.add_worker()worker_id = result.parent.namesurfaces = [{@@ -1941,11 +1838,7 @@ def test_deferred_projection_preserves_each_referenced_source_surface(original = result.read_bytes()for name in ("scan-manifest.json", "findings.json", "coverage.json"):(scan.scan_dir / name).unlink()- workbench_api["saved_results"].fail_scan(- workbench_api["_WORKBENCH_DB_CONTEXT"],- workbench_db,- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),- )+ scan.fail()coverage = read_review_state(workbench_db, scan.scan_dir)projected = {row["provenance"]["sourceId"]: row for row in coverage["surfaces"]}assert set(projected) == {surface["id"] for surface in surfaces}diff --git a/plugins/codex-security/tests/test_recovered_worker_closeouts.py b/plugins/codex-security/tests/test_recovered_worker_closeouts.pyindex 95c3711d3..29f7f5938 100644--- a/plugins/codex-security/tests/test_recovered_worker_closeouts.py+++ b/plugins/codex-security/tests/test_recovered_worker_closeouts.py@@ -10,6 +10,7 @@ from workbench_test_support import (saved_binding,saved_discovery_worker,saved_draft,+ select_checkpoint,write_checkpoint,)@@ -37,8 +38,7 @@ def saved_worker(root: Path, worker_id: str, *, headed: bool = False):result.write_text(json.dumps(complete))observed(result, 20)if headed:- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ head = select_checkpoint(output, checkpoint)observed(head, 20)worker = saved_discovery_worker(output, worker_id, 2)worker.update(status="succeeded", result_manifest_path=str(result))@@ -103,8 +103,7 @@ def test_worker_reopening_invalidates_its_copied_parent_closeout(reopened = {"id": "review-a", "reason": "New evidence requires another review."}checkpoint = write_checkpoint(output / "checkpoints", saved_draft(SCAN_ID, deferred=[reopened]))observed(checkpoint, reopened_at)- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ head = select_checkpoint(output, checkpoint)observed(head, reopened_at)coverage = recover(tmp_path, workbench_api["saved_results"], [worker])assert sorted(coverage.get("resolvedDeferred", []), key=lambda row: row["id"]) == (diff --git a/plugins/codex-security/tests/test_root_candidate_authority.py b/plugins/codex-security/tests/test_root_candidate_authority.pyindex 266bd5c4a..c33be9b5a 100644--- a/plugins/codex-security/tests/test_root_candidate_authority.py+++ b/plugins/codex-security/tests/test_root_candidate_authority.py@@ -7,13 +7,14 @@ import osfrom argparse import Namespaceimport pytest-from test_deep_scan_successful_publication import add_worker, completefrom test_deep_scan_successful_publication import publication_scan as publication_scanfrom workbench_test_support import (read_review_state,saved_draft,+ select_checkpoint,sort_checkpoint_entries,write_checkpoint,+ write_result_draft,)@@ -69,12 +70,10 @@ def test_canonical_candidate_resolution_keeps_legacy_checkpoint_authority(scan.scan_id,deferred=[{"candidateId": "candidate-review", "reason": "Validation is pending."}],)- checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", pending)- os.utime(checkpoint, ns=(pending_time, pending_time))+ checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", pending, mtime_ns=pending_time)canonical_disposition(scan, disposition, complete_marker)original = checkpoint.read_bytes()saved = workbench_api["saved_results"]- context = workbench_api["_WORKBENCH_DB_CONTEXT"]prepared_coverage = []write = saved._write_prepared_scan_finalization@@ -85,18 +84,12 @@ def test_canonical_candidate_resolution_keeps_legacy_checkpoint_authority(return write(prepared, **kwargs)if flow in {"complete", "prepare"}:- completed = complete(workbench_api, workbench_db, scan, prepare_only=flow == "prepare")+ completed = scan.complete(prepare_only=flow == "prepare")assert completed["progress"]["status"] == ("running" if flow == "prepare" else "complete")else:with monkeypatch.context() as patch:patch.setattr(saved, "_write_prepared_scan_finalization", publish)- saved.fail_scan(- context,- workbench_db,- Namespace(- scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."- ),- )+ scan.fail()if flow == "interrupted-stop":row = workbench_db.execute("SELECT * FROM scans WHERE id = ?", (scan.scan_id,)@@ -107,13 +100,7 @@ def test_canonical_candidate_resolution_keeps_legacy_checkpoint_authority(# later file-authored draft reopens this candidate.late = {**scan.coverage, "deferred": pending["coverage"]["deferred"]}(scan.scan_dir / "coverage.json").write_text(json.dumps(late))- saved.preserve_scan_results(- context,- workbench_db,- Namespace(- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None- ),- )+ scan.preserve()coverage = read_review_state(workbench_db, scan.scan_dir)assert "completeness" not in coveragefor document in [coverage, *prepared_coverage]:@@ -127,9 +114,7 @@ def test_canonical_candidate_resolution_keeps_legacy_checkpoint_authority()if flow in {"stop", "interrupted-stop"}:before = (scan.scan_dir / "scan-manifest.json").read_bytes()- recovered = saved.recover_scan_results(- context, workbench_db, Namespace(scan_id=scan.scan_id)- )["scan"]+ recovered = scan.recover()assert not recovered["resultsRecoveryNeeded"]assert (scan.scan_dir / "scan-manifest.json").read_bytes() == beforeassert read_review_state(workbench_db, scan.scan_dir) == coverage@@ -145,24 +130,16 @@ def test_accepted_pending_root_tie_survives_canonical_disposition(scan.scan_id,deferred=[{"candidateId": "candidate-review", "reason": "Accepted pending review."}],)- checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", pending)- os.utime(checkpoint, ns=(100, 100))- head = scan.scan_dir / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(200, 200))+ checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", pending, mtime_ns=100)+ select_checkpoint(scan.scan_dir, checkpoint, mtime_ns=200)saved = workbench_api["saved_results"]saved._capture_saved_source(scan.scan_dir, "checkpoint-head.json", scan.scan_id)canonical_disposition(scan, disposition, True)- context = workbench_api["_WORKBENCH_DB_CONTEXT"]- saved.fail_scan(- context,- workbench_db,- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),- )+ scan.fail()coverage = read_review_state(workbench_db, scan.scan_dir)assert "completeness" not in coverageassert any(row.get("candidateId") == "candidate-review" for row in coverage["deferred"])- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))+ scan.recover()assert read_review_state(workbench_db, scan.scan_dir) == coverage@@ -184,19 +161,15 @@ def test_restored_root_observation_keeps_tied_pending_payload(_, terminal = saved._read_saved_parent_result(scan.scan_dir, scan.scan_id)for name in ("scan-manifest.json", "findings.json", "coverage.json"):os.utime(scan.scan_dir / name, ns=(0, 0))- checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", terminal)- os.utime(checkpoint, ns=(50, 50))- head = scan.scan_dir / "checkpoint-head.json"+ checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", terminal, mtime_ns=50)for observed in (100, 300, 100):- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))+ select_checkpoint(scan.scan_dir, checkpoint, mtime_ns=observed)saved._capture_saved_source(scan.scan_dir, "checkpoint-head.json", scan.scan_id)pending = saved_draft(scan.scan_id,deferred=[{"candidateId": "candidate-review", "reason": "Review remains pending."}],)- pending_path = write_checkpoint(scan.scan_dir / "checkpoints", pending)- os.utime(pending_path, ns=(pending_time, pending_time))+ write_checkpoint(scan.scan_dir / "checkpoints", pending, mtime_ns=pending_time)prepared_coverage = []write = saved._write_prepared_scan_finalization@@ -206,28 +179,17 @@ def test_restored_root_observation_keeps_tied_pending_payload(raise OSError("Synthetic restored-observation publication interruption")return write(prepared, **kwargs)- context = workbench_api["_WORKBENCH_DB_CONTEXT"]with monkeypatch.context() as patch:patch.setattr(saved, "_write_prepared_scan_finalization", publish)- saved.fail_scan(- context,- workbench_db,- Namespace(scan_id=scan.scan_id, claim_token=None, cost_json=None, message="Stopped."),- )- saved.preserve_scan_results(- context,- workbench_db,- Namespace(- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None- ),- )+ scan.fail()+ scan.preserve()coverage = read_review_state(workbench_db, scan.scan_dir)assert len(prepared_coverage) == 1for document in [coverage, *prepared_coverage]:assert any(row.get("candidateId") == "candidate-review" for row in document["deferred"]) is (pending_time >= 100)- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))+ scan.recover()assert read_review_state(workbench_db, scan.scan_dir) == coverage@@ -269,20 +231,19 @@ def test_root_outcome_uses_its_own_completion_marker(os.utime(scan.scan_dir / name, ns=(0, 0))results = sorted(- [add_worker(workbench_db, scan), add_worker(workbench_db, scan)],+ [scan.add_worker(), scan.add_worker()],key=lambda path: path.parent.name,)# Registration timestamps are equal, so IDs determine both arrival orders.for result, worker_complete in zip(results, [not complete_worker_last, complete_worker_last]):- result.write_text(- json.dumps(- saved_draft(- scan.scan_id,- complete=worker_complete,- )- )+ write_result_draft(+ result,+ saved_draft(+ scan.scan_id,+ complete=worker_complete,+ ),+ mtime_ns=100,)- os.utime(result, ns=(100, 100))assert [row["result_manifest_path"]for row in workbench_db.execute(@@ -296,19 +257,16 @@ def test_root_outcome_uses_its_own_completion_marker()if flow == "recover":saved.fail_scan(context, workbench_db, stop_args)- selected = write_checkpoint(scan.scan_dir / "checkpoints", root_draft)- os.utime(selected, ns=(100, 100))- head = scan.scan_dir / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(200, 200))+ selected = write_checkpoint(scan.scan_dir / "checkpoints", root_draft, mtime_ns=100)+ select_checkpoint(scan.scan_dir, selected, mtime_ns=200)pending = write_checkpoint(scan.scan_dir / "checkpoints",saved_draft(scan.scan_id,deferred=[{"candidateId": "candidate-review", "reason": "Validation is pending."}],),+ mtime_ns=200,)- os.utime(pending, ns=(200, 200))originals = {path: path.read_bytes() for path in [selected, pending, *results]}prepared_coverage = []write = saved._write_prepared_scan_finalization@@ -325,16 +283,10 @@ def test_root_outcome_uses_its_own_completion_marker(saved.fail_scan(context, workbench_db, stop_args)elif interrupted:with pytest.raises(OSError, match="Synthetic mixed-source publication interruption"):- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))+ scan.recover()else:- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))- published = saved.preserve_scan_results(- context,- workbench_db,- Namespace(- scan_id=scan.scan_id, claim_token=None, thread_id=None, coordinator_generation=None- ),- )["scan"]+ scan.recover()+ published = scan.preserve()assert not published["resultsRecoveryNeeded"]coverage = read_review_state(workbench_db, scan.scan_dir)assert len(prepared_coverage) == 1@@ -343,7 +295,7 @@ def test_root_outcome_uses_its_own_completion_marker(row.get("candidateId") == "candidate-review" for row in document["deferred"]) is (root_complete is False)manifest = (scan.scan_dir / "scan-manifest.json").read_bytes()- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))+ scan.recover()assert read_review_state(workbench_db, scan.scan_dir) == coverageassert (scan.scan_dir / "scan-manifest.json").read_bytes() == manifestassert all(path.read_bytes() == contents for path, contents in originals.items())@@ -406,8 +358,7 @@ def test_legacy_tied_root_parent_preserves_accepted_pending_evidence(checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", draft)observed = 100 if name == "reported" else 500os.utime(checkpoint, ns=(observed, observed))- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))+ select_checkpoint(scan.scan_dir, checkpoint, mtime_ns=observed)captures.update(saved._capture_saved_source(scan.scan_dir, "checkpoint-head.json", scan.scan_id))@@ -463,9 +414,7 @@ def test_legacy_tied_root_parent_preserves_accepted_pending_evidence(assert any(row.get("candidateId") == "candidate-review" for row in current_coverage["deferred"])- replayed = saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))[- "scan"- ]+ replayed = scan.recover()assert replayed["findingCount"] == 1assert not replayed["resultsRecoveryNeeded"]before_sources = json.loads(manifest)["scan"]["preservedSources"]@@ -490,14 +439,11 @@ def test_legacy_tied_root_parent_preserves_accepted_pending_evidence(if wrapped:assert manifest_path.read_bytes() == manifestmanifest = manifest_path.read_bytes()- saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))+ scan.recover()assert manifest_path.read_bytes() == manifest- head.write_text(json.dumps({"checkpoint": checkpoints[later_selection].name}))- os.utime(head, ns=(600, 600))- recovered = saved.recover_scan_results(context, workbench_db, Namespace(scan_id=scan.scan_id))[- "scan"- ]+ select_checkpoint(scan.scan_dir, checkpoints[later_selection], mtime_ns=600)+ recovered = scan.recover()assert recovered["findingCount"] == (0 if later_selection == "rejected" else 1)assert not recovered["resultsRecoveryNeeded"]coverage = read_review_state(workbench_db, scan.scan_dir)@@ -565,18 +511,15 @@ def test_legacy_pending_ties_aggregate_accepted_provenance(head = scan.scan_dir / "checkpoint-head.json"for draft, observed in observations:draft["coverage"] = {**scan.coverage, **draft["coverage"]}- checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", draft)- os.utime(checkpoint, ns=(observed, observed))- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))+ checkpoint = write_checkpoint(scan.scan_dir / "checkpoints", draft, mtime_ns=observed)+ select_checkpoint(scan.scan_dir, checkpoint, mtime_ns=observed)captures.update(saved._capture_saved_source(scan.scan_dir, "checkpoint-head.json", scan.scan_id))head.unlink()raw = saved_draft(scan.scan_id, deferred=[unobserved])raw["coverage"] = {**scan.coverage, **raw["coverage"]}- unobserved_path = write_checkpoint(scan.scan_dir / "checkpoints", raw)- os.utime(unobserved_path, ns=(100, 100))+ unobserved_path = write_checkpoint(scan.scan_dir / "checkpoints", raw, mtime_ns=100)captures.update(saved._capture_saved_source(scan.scan_dir, unobserved_path.relative_to(scan.scan_dir).as_posix(), scan.scan_iddiff --git a/plugins/codex-security/tests/test_saved_deferred_identity.py b/plugins/codex-security/tests/test_saved_deferred_identity.pyindex 6ddb317d5..4d538a8d2 100644--- a/plugins/codex-security/tests/test_saved_deferred_identity.py+++ b/plugins/codex-security/tests/test_saved_deferred_identity.py@@ -23,7 +23,9 @@ from workbench_test_support import (run_workbench,saved_discovery_worker,saved_draft,+ select_checkpoint,write_checkpoint,+ write_result_draft,)@@ -79,9 +81,7 @@ def save_worker(root: Path, module, worker_id: str, drafts: list[dict], result:path = checkpoints / f"{module._digest(draft)}.json"path.write_text(json.dumps(draft))os.utime(path, ns=(index * 100, index * 100))- result_path = output / "result.json"- result_path.write_text(json.dumps(result))- os.utime(result_path, ns=(1000, 1000))+ write_result_draft(output / "result.json", result, mtime_ns=1000)return saved_discovery_worker(output, worker_id, 1)@@ -245,15 +245,15 @@ def test_missing_parent_identity_reuses_established_checkpoint("Synthetic review finding",summary="Retain the saved result.",)- checkpoint = write_checkpoint(+ write_checkpoint(tmp_path / "checkpoints",saved_draft("identity-scan",complete=True,findings=[{**finding, "identity": {"anchor": saved_anchor}}],),+ mtime_ns=100,)- os.utime(checkpoint, ns=(100, 100))if explicit_end_line:finding["locations"][0]["endLine"] = 1write_saved_parent(@@ -362,10 +362,11 @@ def test_stopped_deep_recovery_uses_host_mode_for_terminal_replacement(write_saved_parent(scan_dir, saved_draft(scan_id, findings=[first], complete=True), 100)revised = {**first, "title": "Revised review", "summary": "Revised synthetic evidence."}del revised["identity"]- checkpoint = write_checkpoint(- scan_dir / "checkpoints", saved_draft(scan_id, findings=[revised], complete=True)+ write_checkpoint(+ scan_dir / "checkpoints",+ saved_draft(scan_id, findings=[revised], complete=True),+ mtime_ns=200,)- os.utime(checkpoint, ns=(200, 200))fail_deep_scan(state, codex_home, scan_id)findings_path = scan_dir / "findings.json"findings = json.loads(findings_path.read_text())["findings"]@@ -425,11 +426,9 @@ def test_torn_worker_closure_reuses_observed_legacy_identity(closures=[{"id": identity, "reason": "Review completed."}],complete=True,),+ mtime_ns=1100,)- os.utime(checkpoint, ns=(1100, 1100))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(1200, 1200))+ select_checkpoint(output, checkpoint, mtime_ns=1200)originals = {path: path.read_bytes() for path in output.rglob("*.json")}result = recover(tmp_path, saved_results, [worker, other])replay = recover(@@ -566,13 +565,13 @@ def test_unsafe_deferred_update_keeps_the_earlier_valid_path(state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path, run=setup_workbench)_, result = accepted_standard_worker(state, codex_home, scan_dir, scan_id, run=setup_workbench)original = {"id": "review", "reason": "Review remains.", "paths": ["app.py"]}- checkpoint = write_checkpoint(- result.parent / "checkpoints", saved_draft(scan_id, deferred=[original])+ write_checkpoint(+ result.parent / "checkpoints",+ saved_draft(scan_id, deferred=[original]),+ mtime_ns=100,)- os.utime(checkpoint, ns=(100, 100))update = {**original, "paths": ["../outside.py"]}- result.write_text(json.dumps(saved_draft(scan_id, deferred=[update])))- os.utime(result, ns=(200, 200))+ write_result_draft(result, saved_draft(scan_id, deferred=[update]), mtime_ns=200)fail_deep_scan(state, codex_home, scan_id, deep_status="failed")@@ -654,9 +653,7 @@ def test_new_work_survives_accepted_candidate_rejection(for index, checkpoint in enumerate(checkpoints, 1):os.utime(checkpoint, ns=(index * 100, index * 100))os.utime(checkpoints[2], ns=(raw_modified, raw_modified))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoints[1].name}))- os.utime(head, ns=(200, 200))+ select_checkpoint(output, checkpoints[1], mtime_ns=200)documents = recover(tmp_path, saved_results, workers)replay = recover(tmp_path, saved_results, workers, documents[0]["scan"]["preservedSources"])for result in (documents, replay):@@ -733,21 +730,18 @@ def test_generic_surface_recovery_uses_resolved_candidate_identity(]for index, checkpoint in enumerate(checkpoints, 1):os.utime(checkpoint, ns=(index * 100, index * 100))- result_path.write_text(json.dumps(accepted))- os.utime(result_path, ns=(200, 200))+ write_result_draft(result_path, accepted, mtime_ns=200)# The terminal checkpoint survives a failed replacement of result.json.- head = result_path.parent / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoints[2 if rejected_here else 1].name}))+ head = select_checkpoint(result_path.parent, checkpoints[2 if rejected_here else 1])observed = 300 if rejected_here else 200os.utime(head, ns=(observed, observed))if outcome == "other_worker":other_worker_id, other_result = accepted_standard_worker(state, codex_home, scan_dir, scan_id, name="other-worker", run=setup_workbench)- other_result.write_text(- json.dumps(saved_draft(scan_id, surfaces=[rejection], complete=True))+ write_result_draft(+ other_result, saved_draft(scan_id, surfaces=[rejection], complete=True), mtime_ns=200)- os.utime(other_result, ns=(200, 200))first_coverage, recovered = cancel_and_preserve(monkeypatch, saved_results, state, codex_home, scan_dir, scan_id@@ -842,11 +836,8 @@ def test_split_deferred_rows_keep_distinct_ids_in_frozen_recovery(]for index, checkpoint in enumerate(checkpoints, 1):os.utime(checkpoint, ns=(index * 100, index * 100))- result_path.write_text(json.dumps(initial))- os.utime(result_path, ns=(100, 100))- head = result_path.parent / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoints[0].name}))- os.utime(head, ns=(100, 100))+ write_result_draft(result_path, initial, mtime_ns=100)+ select_checkpoint(result_path.parent, checkpoints[0], mtime_ns=100)if close_first:first_id = split[0]["id"] if explicit_ids else saved_results._saved_coverage_id(split[0])terminal = saved_draft(@@ -854,10 +845,10 @@ def test_split_deferred_rows_keep_distinct_ids_in_frozen_recovery(closures=[{"id": first_id, "reason": "First path reviewed."}],complete=True,)- terminal_checkpoint = write_checkpoint(result_path.parent / "checkpoints", terminal)- os.utime(terminal_checkpoint, ns=(300, 300))- head.write_text(json.dumps({"checkpoint": terminal_checkpoint.name}))- os.utime(head, ns=(300, 300))+ terminal_checkpoint = write_checkpoint(+ result_path.parent / "checkpoints", terminal, mtime_ns=300+ )+ select_checkpoint(result_path.parent, terminal_checkpoint, mtime_ns=300)if explicit_ids:expected_ids.remove(first_id)first_coverage, replay = cancel_and_preserve(@@ -925,11 +916,8 @@ def test_unnamed_observation_does_not_replace_saved_context(accepted_index = 1 if observation == "explicit-update" else 0observed = (accepted_index + 1) * 100if layout == "worker":- result_path.write_text(json.dumps(accepted))- os.utime(result_path, ns=(observed, observed))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoints[accepted_index].name}))- os.utime(head, ns=(observed, observed))+ write_result_draft(result_path, accepted, mtime_ns=observed)+ select_checkpoint(output, checkpoints[accepted_index], mtime_ns=observed)first_coverage, replay = cancel_and_preserve(monkeypatch, saved_results, state, codex_home, scan_dir, scan_id)@@ -1093,12 +1081,9 @@ def test_recovery_finalization_preserves_distinct_reports_at_a_revised_location(]output = tmp_path / "reviewer"for sequence, draft in enumerate(drafts, 1):- path = write_checkpoint(output / "checkpoints", draft)- os.utime(path, ns=(sequence * 1_000_000_000, sequence * 1_000_000_000))+ write_checkpoint(output / "checkpoints", draft, mtime_ns=sequence * 1_000_000_000)if saved_result:- result = output / "result.json"- result.write_text(json.dumps(drafts[-1]))- os.utime(result, ns=(3_000_000_000, 3_000_000_000))+ write_result_draft(output / "result.json", drafts[-1], mtime_ns=3_000_000_000)worker = saved_discovery_worker(output, "reviewer")sources = {path: path.read_bytes() for path in output.rglob("*.json")}documents = recover(tmp_path, saved_results, [worker])@@ -1184,10 +1169,8 @@ def test_parent_represents_worker_versions_before_candidate_enrichment(output.mkdir()draft = {"scanId": "identity-scan", "complete": True, "findings": [reduced]}result = output / "result.json"- result.write_text(json.dumps(draft))- os.utime(result, ns=(1500, 1500))- checkpoint = write_checkpoint(output / "checkpoints", draft)- os.utime(checkpoint, ns=(1500, 1500))+ write_result_draft(result, draft, mtime_ns=1500)+ write_checkpoint(output / "checkpoints", draft, mtime_ns=1500)reducer = {"id": "reducer","kind": "dedup",diff --git a/plugins/codex-security/tests/test_saved_deferred_unicode.py b/plugins/codex-security/tests/test_saved_deferred_unicode.pyindex ae2234f8e..41062d488 100644--- a/plugins/codex-security/tests/test_saved_deferred_unicode.py+++ b/plugins/codex-security/tests/test_saved_deferred_unicode.py@@ -64,8 +64,7 @@ def test_stopped_publication_recovers_after_unicode_reviews_are_closed(tmp_path:scan_id,deferred=[{"reason": reason, "paths": ["src/example.py"]} for reason, _ in SURROGATE_IDS],)- checkpoint = write_checkpoint(result.parent / "checkpoints", pending)- os.utime(checkpoint, ns=(100, 100))+ write_checkpoint(result.parent / "checkpoints", pending, mtime_ns=100)closed = saved_draft(scan_id,closures=[{"id": identity, "reason": "Review completed."} for _, identity in SURROGATE_IDS],diff --git a/plugins/codex-security/tests/test_workbench_cancellation.py b/plugins/codex-security/tests/test_workbench_cancellation.pyindex 5c4329dc3..0e3cd2090 100644--- a/plugins/codex-security/tests/test_workbench_cancellation.py+++ b/plugins/codex-security/tests/test_workbench_cancellation.py@@ -72,15 +72,15 @@ def test_workbench_cancels_running_scan_and_rejects_late_updates(tmp_path: Path)assert delivered["results"]["progress"]["status"] == "canceled"assert delivered["results"]["handoffStatus"] == "delivered"- restarted = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- restarted_scan_id = str(restarted["results"]["scanId"])+ restarted = start_delivered_scan(state_dir, str(saved["id"]))+ restarted_scan_id = str(restarted["scanId"])assert restarted_scan_id != scan_id- assert restarted["results"]["progress"]["status"] == "running"+ assert restarted["progress"]["status"] == "running"previous_scan = get_scan(state_dir, scan_id)assert previous_scan["scan"]["scanId"] == scan_idassert previous_scan["workspace"]["results"]["scanId"] == scan_idassert previous_scan["workspace"]["results"]["progress"]["status"] == "canceled"- write_completed_contract(Path(str(restarted["results"]["scanDir"])), restarted_scan_id, target)+ write_completed_contract(Path(str(restarted["scanDir"])), restarted_scan_id, target)scan_command(state_dir, "complete-scan", restarted_scan_id)rejected = cancel_scan(state_dir, restarted_scan_id, thread_id, check=False)assert rejected["returncode"] != 0diff --git a/plugins/codex-security/tests/test_workbench_checkpoint_heads.py b/plugins/codex-security/tests/test_workbench_checkpoint_heads.pyindex 655ccaa0e..e2ccfb6da 100644--- a/plugins/codex-security/tests/test_workbench_checkpoint_heads.py+++ b/plugins/codex-security/tests/test_workbench_checkpoint_heads.py@@ -18,6 +18,7 @@ from workbench_test_support import (fail_deep_scan,get_scan,preserve_scan_results,+ read_draft_documents,read_review_state,replay_saved_results,run_workbench,@@ -25,9 +26,11 @@ from workbench_test_support import (saved_discovery_worker,saved_draft,scan_command,+ select_checkpoint,start_delivered_scan,write_checkpoint,write_completed_contract,+ write_result_draft,)sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))@@ -47,12 +50,6 @@ def drafts(scan_id: str) -> tuple[dict, dict]:return pending, closed-def select(output: Path, checkpoint: Path, observed: int) -> None:- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(observed, observed))--@pytest.mark.parametrize("selection", ["same", "reopened"])@pytest.mark.parametrize("has_result", [False, True])def test_late_head_changes_require_explicit_recovery(@@ -66,11 +63,10 @@ def test_late_head_changes_require_explicit_recovery(os.utime(reopened, ns=(100, 100))os.utime(completed, ns=(150, 150))if has_result:- result.write_text(json.dumps(pending))- os.utime(result, ns=(100, 100))+ write_result_draft(result, pending, mtime_ns=100)else:result.unlink()- select(result.parent, completed, 200)+ select_checkpoint(result.parent, completed, mtime_ns=200)environment = {"CODEX_HOME": str(codex_home)}fail_deep_scan(state, codex_home, scan_id, deep_status="failed")manifest_path = scan_dir / "scan-manifest.json"@@ -85,7 +81,9 @@ def test_late_head_changes_require_explicit_recovery()assert get_scan(state, scan_id)["scan"]["resultsRecoveryNeeded"] is False- select(result.parent, reopened if selection == "reopened" else completed, 300)+ select_checkpoint(+ result.parent, reopened if selection == "reopened" else completed, mtime_ns=300+ )snapshots = list((result.parent / "checkpoint-heads").iterdir())assert get_scan(state, scan_id)["scan"]["resultsRecoveryNeeded"] is Trueassert list((result.parent / "checkpoint-heads").iterdir()) == snapshots@@ -132,8 +130,7 @@ def test_frozen_parent_replay_ignores_nonreference_ancestry(previous.append(checkpoint.relative_to(tmp_path).as_posix())os.utime(checkpoint, ns=(100, 100))pending["previousParentCheckpoints"] = [*previous, *ancestry]- checkpoint = write_checkpoint(tmp_path / "checkpoints", pending)- os.utime(checkpoint, ns=(200, 200))+ checkpoint = write_checkpoint(tmp_path / "checkpoints", pending, mtime_ns=200)originals = {path: path.read_bytes() for path in (tmp_path / "checkpoints").iterdir()}warnings = []first = saved.merge_saved_results(@@ -165,7 +162,7 @@ def test_recovery_keeps_one_surface_with_optional_empty_receipts(if has_receipts:surface["receiptRefs"] = []checkpoint = write_checkpoint(tmp_path / "checkpoints", draft)- select(tmp_path, checkpoint, 50)+ select_checkpoint(tmp_path, checkpoint, mtime_ns=50)original = checkpoint.read_bytes()if has_canonical:canonical = copy.deepcopy(draft)@@ -199,8 +196,7 @@ def test_file_authored_resolution_can_leave_manifest_unchanged(pending = saved_draft(scan_id, deferred=[{"candidateId": "candidate", "reason": "Review remains."}, independent])- checkpoint = write_checkpoint(tmp_path / "checkpoints", pending)- os.utime(checkpoint, ns=(200, 200))+ write_checkpoint(tmp_path / "checkpoints", pending, mtime_ns=200)completed = saved_draft(scan_id,deferred=[independent] if retain_pending else [],@@ -233,8 +229,7 @@ def test_file_authored_deferred_update_preserves_valid_checkpoint_evidence() -> None:scan_id, pending, _, binding = checkpoint_scanpending["complete"] = False- checkpoint = write_checkpoint(tmp_path / "checkpoints", pending)- os.utime(checkpoint, ns=(100, 100))+ write_checkpoint(tmp_path / "checkpoints", pending, mtime_ns=100)current = copy.deepcopy(pending)current["coverage"]["deferred"][0].update(update)write_saved_parent(tmp_path, current, 200)@@ -274,8 +269,7 @@ def test_recovery_preserves_accepted_generic_surface_closeout(legacy = copy.deepcopy(pending)if anonymous:legacy["coverage"]["surfaces"][0].pop("id")- checkpoint = write_checkpoint(directory / "checkpoints", legacy)- os.utime(checkpoint, ns=(100, 100))+ write_checkpoint(directory / "checkpoints", legacy, mtime_ns=100)# A prior writer returned the assigned surface ID, but its checkpoint was anonymous.published = copy.deepcopy(pending)published["coverage"]["surfaces"][0]["receiptRefs"] = []@@ -283,8 +277,7 @@ def test_recovery_preserves_accepted_generic_surface_closeout(write_saved_parent(directory, published, 150)else:result = directory / "result.json"- result.write_text(json.dumps(published))- os.utime(result, ns=(150, 150))+ write_result_draft(result, published, mtime_ns=150)closed_surface = {**surface, "disposition": "no_issue_found", "receiptRefs": []}closed = saved_draft(scan_id,@@ -292,9 +285,8 @@ def test_recovery_preserves_accepted_generic_surface_closeout(closures=[{"id": "review", "reason": "Review completed."}],complete=True,)- completed = write_checkpoint(directory / "checkpoints", closed)- os.utime(completed, ns=(200, 200))- select(directory, completed, 300)+ completed = write_checkpoint(directory / "checkpoints", closed, mtime_ns=200)+ select_checkpoint(directory, completed, mtime_ns=300)workers = ([]if layout == "parent"@@ -321,8 +313,7 @@ def test_malformed_parent_deferred_id_reaches_existing_recovery_diagnostics() -> None:scan_id, pending, _, binding = checkpoint_scanpending["complete"] = False- checkpoint = write_checkpoint(tmp_path / "checkpoints", pending)- os.utime(checkpoint, ns=(100, 100))+ write_checkpoint(tmp_path / "checkpoints", pending, mtime_ns=100)current = copy.deepcopy(pending)current["complete"] = completecurrent["coverage"]["deferred"][0]["id"] = identity@@ -365,18 +356,15 @@ def test_recovery_preserves_distinct_legacy_tasks_with_one_id({"id": "review", "reason": "Review worker.", "paths": ["worker.py"]},]legacy = saved_draft(scan_id, deferred=tasks)- checkpoint = write_checkpoint(directory / "checkpoints", legacy)- os.utime(checkpoint, ns=(100, 100))+ write_checkpoint(directory / "checkpoints", legacy, mtime_ns=100)if rewrite:rewritten = saved_draft(scan_id, deferred=tasks[:1])- checkpoint = write_checkpoint(directory / "checkpoints", rewritten)- os.utime(checkpoint, ns=(150, 150))+ write_checkpoint(directory / "checkpoints", rewritten, mtime_ns=150)closed = saved_draft(scan_id, closures=[{"id": "review", "reason": "API reviewed."}], complete=True)- completed = write_checkpoint(directory / "checkpoints", closed)- os.utime(completed, ns=(200, 200))- select(directory, completed, 300)+ completed = write_checkpoint(directory / "checkpoints", closed, mtime_ns=200)+ select_checkpoint(directory, completed, mtime_ns=300)workers = ([]if layout == "parent"@@ -421,13 +409,13 @@ def test_recovery_keeps_generic_work_that_collides_with_candidate_aliases(other = {"id": "other-review", "reason": "Other review.", "surfaceIds": ["generic-surface"]}surface = {"id": "generic-surface", "label": "Generic review", "disposition": "needs_follow_up"}pending = saved_draft(scan_id, deferred=[generic, candidate, other], surfaces=[surface])- checkpoint = write_checkpoint(directory / "checkpoints", pending)- os.utime(checkpoint, ns=(100, 100))+ write_checkpoint(directory / "checkpoints", pending, mtime_ns=100)if rewrite:- checkpoint = write_checkpoint(- directory / "checkpoints", saved_draft(scan_id, deferred=[candidate, other])+ write_checkpoint(+ directory / "checkpoints",+ saved_draft(scan_id, deferred=[candidate, other]),+ mtime_ns=150,)- os.utime(checkpoint, ns=(150, 150))terminal = saved_draft(scan_id,closures=[{"id": "other-review", "reason": "Other review completed."}],@@ -449,9 +437,8 @@ def test_recovery_keeps_generic_work_that_collides_with_candidate_aliases(finding = json.loads((contract / "findings.json").read_text())["findings"][0]finding.setdefault("extensions", {})["candidateId"] = "candidate-a"terminal["findings"] = [finding]- completed = write_checkpoint(directory / "checkpoints", terminal)- os.utime(completed, ns=(200, 200))- select(directory, completed, 300)+ completed = write_checkpoint(directory / "checkpoints", terminal, mtime_ns=200)+ select_checkpoint(directory, completed, mtime_ns=300)workers = ([]if layout == "parent"@@ -491,9 +478,8 @@ def test_selected_parent_replaces_stale_completion_marker(progress = saved_draft(scan_id, deferred=tasks, complete=complete)if complete is None:progress.pop("complete")- checkpoint = write_checkpoint(tmp_path / "checkpoints", progress)- os.utime(checkpoint, ns=(200, 200))- select(tmp_path, checkpoint, 300)+ checkpoint = write_checkpoint(tmp_path / "checkpoints", progress, mtime_ns=200)+ select_checkpoint(tmp_path, checkpoint, mtime_ns=300)first = saved.merge_saved_results(tmp_path, scan_id, binding, [], [], stopped=True, reason="interrupted")@@ -518,8 +504,7 @@ def test_reported_surface_requires_its_own_valid_candidate_finding(scan_id,deferred=[{"id": "candidate-task", "candidateId": "candidate-a", "reason": "Review."}],)- checkpoint = write_checkpoint(directory / "checkpoints", pending)- os.utime(checkpoint, ns=(100, 100))+ write_checkpoint(directory / "checkpoints", pending, mtime_ns=100)terminal = saved_draft(scan_id,complete=True,@@ -545,14 +530,12 @@ def test_reported_surface_requires_its_own_valid_candidate_finding(if evidence == "malformed":finding.pop("title")terminal["findings"] = [finding]- completed = write_checkpoint(directory / "checkpoints", terminal)- os.utime(completed, ns=(200, 200))+ completed = write_checkpoint(directory / "checkpoints", terminal, mtime_ns=200)if layout == "result":result = directory / "result.json"- result.write_text(json.dumps(terminal))- os.utime(result, ns=(300, 300))+ write_result_draft(result, terminal, mtime_ns=300)else:- select(directory, completed, 300)+ select_checkpoint(directory, completed, mtime_ns=300)workers = [saved_discovery_worker(output, "worker", 2 if layout == "archived" else 1)]first = saved.merge_saved_results(tmp_path, scan_id, binding, workers, [], stopped=True, reason="interrupted"@@ -579,13 +562,12 @@ def test_completion_checks_selected_parent_checkpoint(workspace = create_saved_workspace(state, target)started = start_delivered_scan(state,- "--workspace-id",str(workspace["id"]),"--scan-root",str(tmp_path / "scans"),)- scan_id = str(started["results"]["scanId"])- scan_dir = Path(started["results"]["scanDir"])+ scan_id = str(started["scanId"])+ scan_dir = Path(started["scanDir"])write_completed_contract(scan_dir, scan_id, target, relative_path="app.py")manifest_path = scan_dir / "scan-manifest.json"manifest = json.loads(manifest_path.read_text())@@ -608,9 +590,8 @@ def test_completion_checks_selected_parent_checkpoint(artifacts = ("scan-manifest.json", "findings.json", "coverage.json")for name in artifacts:os.utime(scan_dir / name, ns=(100, 100))- checkpoint = write_checkpoint(scan_dir / "checkpoints", draft)- os.utime(checkpoint, ns=(200, 200))- select(scan_dir, checkpoint, 300)+ checkpoint = write_checkpoint(scan_dir / "checkpoints", draft, mtime_ns=200)+ select_checkpoint(scan_dir, checkpoint, mtime_ns=300)original = {name: (scan_dir / name).read_bytes() for name in (*artifacts, "checkpoint-head.json")}@@ -676,9 +657,8 @@ def test_selected_parent_retains_semantic_context_through_frozen_recovery(}for name in ("scan-manifest.json", "findings.json", "coverage.json"):os.utime(tmp_path / name, ns=(100, 100))- checkpoint = write_checkpoint(tmp_path / "checkpoints", selected)- os.utime(checkpoint, ns=(200, 200))- select(tmp_path, checkpoint, 300)+ checkpoint = write_checkpoint(tmp_path / "checkpoints", selected, mtime_ns=200)+ select_checkpoint(tmp_path, checkpoint, mtime_ns=300)first = saved.merge_saved_results(tmp_path, scan_id, binding, [], [], stopped=stopped, reason="interrupted")@@ -703,7 +683,7 @@ def test_frozen_observations_survive_live_head_changes(reopened = write_checkpoint(directory / "checkpoints", pending)os.utime(completed, ns=(100, 100))os.utime(reopened, ns=(200, 200))- select(directory, completed, 300)+ select_checkpoint(directory, completed, mtime_ns=300)workers = ([]if layout == "parent"@@ -727,7 +707,7 @@ def test_frozen_observations_survive_live_head_changes(snapshot = next(path for path in frozen if "/checkpoint-heads/" in f"/{path}")assert pending["coverage"]["deferred"][0] not in first[2]["deferred"]for checkpoint in (reopened, completed):- select(directory, checkpoint, 400)+ select_checkpoint(directory, checkpoint, mtime_ns=400)replay = merge(frozen)assert replay[2] == first[2]legacy = {@@ -754,13 +734,13 @@ def test_multiple_parent_observations_keep_latest_selection_and_pending_ties(os.utime(completed, ns=(100, 100))os.utime(reopened, ns=(200, 200))for checkpoint, observed in ((completed, 300), (reopened, 400), (completed, 500)):- select(tmp_path, checkpoint, observed)+ select_checkpoint(tmp_path, checkpoint, mtime_ns=observed)saved._capture_saved_source(tmp_path, "checkpoint-head.json", scan_id)first = saved.merge_saved_results(tmp_path, scan_id, binding, [], [], stopped=True, reason="interrupted")assert pending["coverage"]["deferred"][0] not in first[2]["deferred"]- select(tmp_path, reopened, 500)+ select_checkpoint(tmp_path, reopened, mtime_ns=500)tied = saved.merge_saved_results(tmp_path, scan_id, binding, [], [], stopped=True, reason="interrupted")@@ -776,7 +756,7 @@ def test_head_snapshot_reads_content_and_time_from_one_descriptor(scan_id, pending, closed, _ = checkpoint_scancompleted = write_checkpoint(tmp_path / "checkpoints", closed)reopened = write_checkpoint(tmp_path / "checkpoints", pending)- select(tmp_path, completed, observed)+ select_checkpoint(tmp_path, completed, mtime_ns=observed)replacement = tmp_path / "replacement.json"replacement.write_text(json.dumps({"checkpoint": reopened.name}))os.utime(replacement, ns=(observed + 1, observed + 1))@@ -803,9 +783,8 @@ def test_legacy_live_head_sources_convert_their_recorded_observation(tmp_path: Path, checkpoint_scan) -> None:scan_id, pending, closed, binding = checkpoint_scan- checkpoint = write_checkpoint(tmp_path / "checkpoints", closed)- os.utime(checkpoint, ns=(100, 100))- select(tmp_path, checkpoint, 200)+ checkpoint = write_checkpoint(tmp_path / "checkpoints", closed, mtime_ns=100)+ select_checkpoint(tmp_path, checkpoint, mtime_ns=200)paths = [checkpoint.relative_to(tmp_path).as_posix(), "checkpoint-head.json"]frozen = {path: saved._read_saved_result(tmp_path, path, scan_id)[1] for path in paths}first = saved.merge_saved_results(@@ -832,7 +811,7 @@ def test_legacy_live_head_sources_convert_their_recorded_observation("checkpoint": checkpoint.name,"observedAtNs": "200",}- select(tmp_path, checkpoint, 300)+ select_checkpoint(tmp_path, checkpoint, mtime_ns=300)with pytest.raises(saved.ContractError, match="Frozen stopped-scan checkpoint set is incomplete"):@@ -881,14 +860,7 @@ def test_parent_head_selection_matches_frozen_publication_retry(write_completed_contract(contract, scan_id, target, relative_path="app.py", coverage_mode="deep_repository")- initial = {- key: json.loads((contract / filename).read_text())- for key, filename in (- ("manifest", "scan-manifest.json"),- ("findings", "findings.json"),- ("coverage", "coverage.json"),- )- }+ initial = read_draft_documents(contract)if evidence == "reported":finalize_scan_contract._populate_unsealed_finding_identities(initial["manifest"], initial["findings"]@@ -928,7 +900,7 @@ def test_parent_head_selection_matches_frozen_publication_retry(os.utime(original_checkpoint, ns=(100, 100))for filename in ("findings.json", "scan-manifest.json"):os.utime(scan_dir / filename, ns=(100, 100))- select(scan_dir, original_checkpoint, 100)+ select_checkpoint(scan_dir, original_checkpoint, mtime_ns=100)terminal = copy.deepcopy(initial)terminal["manifest"]["scan"].pop("complete")@@ -961,7 +933,7 @@ def test_parent_head_selection_matches_frozen_publication_retry(terminal_checkpoint = scan_dir / "checkpoints" / terminal_head["checkpoint"]assert terminal_checkpoint != original_checkpointos.utime(terminal_checkpoint, ns=(head_time, head_time))- select(scan_dir, terminal_checkpoint, head_time)+ select_checkpoint(scan_dir, terminal_checkpoint, mtime_ns=head_time)assert read_review_state(state, scan_dir) == initial["coverage"]first = []@@ -1039,13 +1011,9 @@ def test_selected_worker_checkpoint_preserves_terminal_coverage(output = tmp_path / "worker"output.mkdir()result_path = output / "result.json"- result_path.write_text(json.dumps(result))- os.utime(result_path, ns=(200, 200))- checkpoint = write_checkpoint(output / "checkpoints", selected)- os.utime(checkpoint, ns=(head_time, head_time))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(head_time, head_time))+ write_result_draft(result_path, result, mtime_ns=200)+ checkpoint = write_checkpoint(output / "checkpoints", selected, mtime_ns=head_time)+ head = select_checkpoint(output, checkpoint, mtime_ns=head_time)unselected = write_checkpoint(output / "checkpoints",saved_draft(@@ -1053,8 +1021,8 @@ def test_selected_worker_checkpoint_preserves_terminal_coverage(surfaces=[{**surface, "id": "unselected", "label": "Unselected"}],complete=True,),+ mtime_ns=400,)- os.utime(unselected, ns=(400, 400))original_bytes = {path: path.read_bytes() for path in (result_path, checkpoint, head, unselected)}@@ -1093,14 +1061,9 @@ def test_terminal_selected_checkpoint_replaces_result_coverage(tmp_path, head_ti)output = tmp_path / "worker"output.mkdir()- result_path = output / "result.json"- result_path.write_text(json.dumps(result))- os.utime(result_path, ns=(200, 200))- checkpoint = write_checkpoint(output / "checkpoints", selected)- os.utime(checkpoint, ns=(head_time, head_time))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(head_time, head_time))+ write_result_draft(output / "result.json", result, mtime_ns=200)+ checkpoint = write_checkpoint(output / "checkpoints", selected, mtime_ns=head_time)+ select_checkpoint(output, checkpoint, mtime_ns=head_time)workers = [saved_discovery_worker(output)]first = saved.merge_saved_results(tmp_path, scan_id, binding, workers, [], stopped=True, reason="interrupted"@@ -1124,24 +1087,18 @@ def test_multiple_selected_terminal_observations(tmp_path, complete):output = tmp_path / "worker"output.mkdir()result = saved_draft(scan_id, surfaces=[before], complete=True)- (output / "result.json").write_text(json.dumps(result))- os.utime(output / "result.json", ns=(200, 200))- head = output / "checkpoint-head.json"+ write_result_draft(output / "result.json", result, mtime_ns=200)intermediate = copy.deepcopy(result)intermediate["coverage"]["openQuestions"] = [{"question": "Review question."}]- intermediate_checkpoint = write_checkpoint(output / "checkpoints", intermediate)- os.utime(intermediate_checkpoint, ns=(250, 250))- head.write_text(json.dumps({"checkpoint": intermediate_checkpoint.name}))- os.utime(head, ns=(250, 250))+ intermediate_checkpoint = write_checkpoint(output / "checkpoints", intermediate, mtime_ns=250)+ select_checkpoint(output, intermediate_checkpoint, mtime_ns=250)workers = [saved_discovery_worker(output)]saved.merge_saved_results(tmp_path, scan_id, binding, workers, [], stopped=True, reason="interrupted")terminal = saved_draft(scan_id, surfaces=[after], complete=complete)- checkpoint = write_checkpoint(output / "checkpoints", terminal)- os.utime(checkpoint, ns=(300, 300))- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(300, 300))+ checkpoint = write_checkpoint(output / "checkpoints", terminal, mtime_ns=300)+ select_checkpoint(output, checkpoint, mtime_ns=300)first = saved.merge_saved_results(tmp_path, scan_id, binding, workers, [], stopped=True, reason="interrupted")@@ -1166,18 +1123,18 @@ def test_older_attempt_selected_head_cannot_supersede_current_result(tmp_path):outdated = {**current, "label": "Old attempt"}output = tmp_path / "worker"output.mkdir()- (output / "result.json").write_text(- json.dumps(saved_draft(scan_id, surfaces=[current], complete=True))+ write_result_draft(+ output / "result.json",+ saved_draft(scan_id, surfaces=[current], complete=True),+ mtime_ns=200,)- os.utime(output / "result.json", ns=(200, 200))archived = output / "attempts" / "attempt-1"checkpoint = write_checkpoint(- archived / "checkpoints", saved_draft(scan_id, surfaces=[outdated], complete=True)+ archived / "checkpoints",+ saved_draft(scan_id, surfaces=[outdated], complete=True),+ mtime_ns=300,)- os.utime(checkpoint, ns=(300, 300))- head = archived / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(300, 300))+ select_checkpoint(archived, checkpoint, mtime_ns=300)workers = [saved_discovery_worker(output, attempt=2)]first = saved.merge_saved_results(tmp_path, scan_id, binding, workers, [], stopped=True, reason="interrupted"@@ -1216,21 +1173,17 @@ def test_generic_worker_closeout_preserves_receipt_owners(surfaces=[surface],deferred=[{"id": "review", "reason": "Review remains.", "surfaceIds": ["api"]}],)- prior_checkpoint = write_checkpoint(archived / "checkpoints", pending)- os.utime(prior_checkpoint, ns=(100, 100))- (archived / "result.json").write_text(json.dumps(pending))- os.utime(archived / "result.json", ns=(100, 100))+ write_checkpoint(archived / "checkpoints", pending, mtime_ns=100)+ write_result_draft(archived / "result.json", pending, mtime_ns=100)closed = saved_draft(scan_id,surfaces=[{**surface, "disposition": "no_issue_found", "receiptRefs": [current_ref]}],closures=[{"id": "review", "reason": "Review completed."}],complete=True,)- current_checkpoint = write_checkpoint(output / "checkpoints", closed)- os.utime(current_checkpoint, ns=(200, 200))- select(output, current_checkpoint, 300)- (output / "result.json").write_text(json.dumps(closed))- os.utime(output / "result.json", ns=(200, 200))+ current_checkpoint = write_checkpoint(output / "checkpoints", closed, mtime_ns=200)+ select_checkpoint(output, current_checkpoint, mtime_ns=300)+ write_result_draft(output / "result.json", closed, mtime_ns=200)worker = {**saved_discovery_worker(output, "worker", 2),"status": "succeeded",@@ -1361,7 +1314,7 @@ def test_legacy_tied_worker_heads_keep_pending_work_on_replay(tmp_path: Path, scfrozen = {}for checkpoint in (completed, reopened):os.utime(checkpoint, ns=(100, 100))- select(output, checkpoint, 500)+ select_checkpoint(output, checkpoint, mtime_ns=500)frozen.update({path: digestdiff --git a/plugins/codex-security/tests/test_workbench_cli.py b/plugins/codex-security/tests/test_workbench_cli.pyindex 9c95cc864..c43958a69 100644--- a/plugins/codex-security/tests/test_workbench_cli.py+++ b/plugins/codex-security/tests/test_workbench_cli.py@@ -80,8 +80,8 @@ def test_context_stdin_is_not_read_before_argument_parsing(stdin.buffer.read.assert_not_called()-# These two commands declare their option groups separately. All other commands-# use the shared workbench declaration exercised by the parser tests above.+# Exercise both workspace setup and Deep Scan creation before either can allocate state.+# The parser-only cases above also check that conflicting options leave stdin untouched.@pytest.mark.parametrize("command",[CREATE_WORKSPACE, ["begin-deep-scan", "--thread-id", "thread-context", "--target-path", "."]],diff --git a/plugins/codex-security/tests/test_workbench_completion_binding.py b/plugins/codex-security/tests/test_workbench_completion_binding.pyindex f651ec841..0bc9701f9 100644--- a/plugins/codex-security/tests/test_workbench_completion_binding.py+++ b/plugins/codex-security/tests/test_workbench_completion_binding.py@@ -117,11 +117,10 @@ def _start_diff_scan_with_draft(tmp_path: Path, kind: str) -> tuple[Path, Path,)registered = start_delivered_scan(state_dir,- "--workspace-id",workspace_id,"--scan-root",str(tmp_path / "scans"),- )["results"]+ )else:if kind == "working_tree":(target / "README.md").write_text("uncommitted fixture\n")@@ -542,23 +541,8 @@ def test_scoped_cli_completion_adopts_registered_revision_kind(tmp_path: Path) -text=True,).stdout.strip()scan_dir = tmp_path / "scan"- scan_dir.mkdir(mode=0o700)- registered = run_workbench(- state_dir,- "register-cli-scan",- "--repository",- str(target),- "--scan-dir",- str(scan_dir),- "--recipe-json",- json.dumps(- {- "config": {},- "mode": "standard",- "repository": str(target),- "target": {"kind": "paths", "paths": ["app"]},- }- ),+ registered = register_cli_scan(+ state_dir, target, scan_dir, scan_target={"kind": "paths", "paths": ["app"]})assert registered["contract"]["target"]["allowedKinds"] == ["git_revision"]scan_id = str(registered["scanId"])@@ -661,23 +645,11 @@ def test_cli_completion_adopts_diff_kind_with_bound_range_digest(tmp_path: Path)(target / "README.md").write_text("second fixture\n")subprocess.run(["git", "commit", "-qam", "Second commit"], cwd=target, check=True)scan_dir = tmp_path / "scan"- scan_dir.mkdir(mode=0o700)- registered = run_workbench(+ registered = register_cli_scan(state_dir,- "register-cli-scan",- "--repository",- str(target),- "--scan-dir",- str(scan_dir),- "--recipe-json",- json.dumps(- {- "config": {},- "mode": "standard",- "repository": str(target),- "target": {"kind": "refs", "paths": [], "base": "HEAD~1", "head": "HEAD"},- }- ),+ target,+ scan_dir,+ scan_target={"kind": "refs", "paths": [], "base": "HEAD~1", "head": "HEAD"},)assert registered["contract"]["target"]["allowedKinds"] == ["git_diff"]scan_id = str(registered["scanId"])@@ -930,7 +902,7 @@ def test_rejected_completion_restores_parent_head_before_corrected_retry(coverage = read_review_state(state_dir, scan_dir)coverage["deferred"] = [{"id": "review", "reason": "Review remains."}]- independent = write_checkpoint(+ write_checkpoint(scan_dir / "checkpoints",{"scanId": scan_id,@@ -940,15 +912,15 @@ def test_rejected_completion_restores_parent_head_before_corrected_retry("deferred": [{"id": "other-review", "reason": "Independent earlier review."}],},},+ mtime_ns=50,)- os.utime(independent, ns=(50, 50))head_path = scan_dir / "checkpoint-head.json"if existing_head:checkpoint = write_checkpoint(scan_dir / "checkpoints",saved_results._parent_scan_draft(scan_id, manifest["scan"], findings, coverage),+ mtime_ns=100,)- os.utime(checkpoint, ns=(100, 100))head_path.write_text(json.dumps({"checkpoint": checkpoint.name}, indent=3))os.utime(head_path, ns=(100, 100))saved_results._capture_saved_source(scan_dir, "checkpoint-head.json", scan_id)diff --git a/plugins/codex-security/tests/test_workbench_db.py b/plugins/codex-security/tests/test_workbench_db.pyindex ade7a1109..6342a6b12 100644--- a/plugins/codex-security/tests/test_workbench_db.py+++ b/plugins/codex-security/tests/test_workbench_db.py@@ -17,8 +17,8 @@ import pytestfrom workbench_test_support import (BUDGET_COST,SCRIPT,+ RemediationRequest,begin_deep_scan,- claim_remediation_resend,create_saved_git_workspace,create_saved_workspace,create_workspace,@@ -29,16 +29,12 @@ from workbench_test_support import (initialize_git_repository,load_script,mark_handoff_delivered,- mark_remediation_delivered,read_review_state,- request_remediation,- request_remediation_action,resume_deep_scan,run_workbench,save_workspace,scan_claim_command,scan_command,- set_remediation,set_triage,stable_target_id,start_delivered_scan,@@ -105,6 +101,17 @@ EXPECTED_TABLES = {}+def budget_candidate(path: str = "app.py", **fields: Any) -> dict[str, Any]:+ return {+ "candidate_id": "candidate-1",+ "cwe_ids": ["CWE-89"],+ "locations": [{"path": path, "start_line": 1, "end_line": 1, "role": "sink"}],+ "summary": "User input reaches a SQL statement",+ "evidence": "request.args['value'] reaches execute() without parameter binding",+ **fields,+ }++def budget_scan_fixture(tmp_path: Path,*,@@ -153,19 +160,7 @@ def budget_scan_fixture(discovery.mkdir(parents=True, exist_ok=True)(discovery / "in_scope_files.txt").write_text("app.py\n")ledger = discovery / "candidate_ledger.jsonl"- rows = (- candidates- if candidates is not None- else [- {- "candidate_id": "candidate-1",- "cwe_ids": ["CWE-89"],- "locations": [{"path": "app.py", "start_line": 1, "end_line": 1, "role": "sink"}],- "summary": "User input reaches a SQL statement",- "evidence": "request.args['value'] reaches execute() without parameter binding",- }- ]- )+ rows = [budget_candidate()] if candidates is None else candidatesledger.write_text("".join(f"{json.dumps(row)}\n" for row in rows))if terminal:manifest = scan_dir / "artifacts" / "deep_discovery" / "coordinator-manifest.json"@@ -311,13 +306,12 @@ def test_budget_exhaustion_preserves_existing_candidate_decisions(surface_disposition: str,deferred: bool,) -> None:- state_dir, _, scan_dir, scan_id, ledger = budget_scan_fixture(tmp_path)- candidate = json.loads(ledger.read_text())+ candidate = budget_candidate()if validation is not None:candidate["validation"] = {"disposition": validation}if attack_path is not None:candidate["attack_path"] = {"decision": attack_path}- ledger.write_text(f"{json.dumps(candidate)}\n")+ state_dir, _, scan_dir, scan_id, _ = budget_scan_fixture(tmp_path, candidates=[candidate])completed = complete_budget_scan(state_dir, scan_id)["scan"]@@ -332,10 +326,8 @@ def test_budget_exhaustion_preserves_existing_candidate_decisions(def test_budget_exhaustion_preserves_existing_terminal_surface(tmp_path: Path) -> None:- state_dir, target, scan_dir, scan_id, ledger = budget_scan_fixture(tmp_path)- candidate = json.loads(ledger.read_text())- candidate["validation"] = {"disposition": "suppressed"}- ledger.write_text(f"{json.dumps(candidate)}\n")+ candidate = budget_candidate(validation={"disposition": "suppressed"})+ state_dir, target, scan_dir, scan_id, _ = budget_scan_fixture(tmp_path, candidates=[candidate])write_completed_contract(scan_dir,scan_id,@@ -491,49 +483,30 @@ def test_budget_exhaustion_rejects_invalid_candidate_ledger(tmp_path: Path) -> Nassert "contains an invalid candidate" in str(rejected["stderr"])-def test_budget_exhaustion_rejects_unsafe_candidate_path(tmp_path: Path) -> None:- state_dir, _, _, scan_id, ledger = budget_scan_fixture(tmp_path)- candidate = json.loads(ledger.read_text())- candidate["locations"][0]["path"] = "../outside.py"- ledger.write_text(f"{json.dumps(candidate)}\n")-- rejected = complete_budget_scan(state_dir, scan_id, check=False)-- assert rejected["returncode"] != 0- assert "candidate location must be repository-relative" in str(rejected["stderr"])---def test_budget_exhaustion_rejects_candidate_outside_inventory(tmp_path: Path) -> None:- state_dir, _, _, scan_id, ledger = budget_scan_fixture(tmp_path)- candidate = json.loads(ledger.read_text())- candidate["locations"][0]["path"] = "outside-scope.py"- ledger.write_text(f"{json.dumps(candidate)}\n")-- rejected = complete_budget_scan(state_dir, scan_id, check=False)-- assert rejected["returncode"] != 0- assert "must include a location in its in-scope inventory" in str(rejected["stderr"])---def test_budget_exhaustion_rejects_windows_drive_candidate_path(tmp_path: Path) -> None:- state_dir, _, _, scan_id, ledger = budget_scan_fixture(tmp_path)- candidate = json.loads(ledger.read_text())- candidate["locations"][0]["path"] = "C:/outside.py"- ledger.write_text(f"{json.dumps(candidate)}\n")+ ("path", "error"),+ [+ ("../outside.py", "candidate location must be repository-relative"),+ ("outside-scope.py", "must include a location in its in-scope inventory"),+ ("C:/outside.py", "candidate location must be repository-relative"),+ ],+ ids=["unsafe_candidate_path", "candidate_outside_inventory", "windows_drive_candidate_path"],+)+def test_budget_exhaustion_rejects_candidate_path(tmp_path: Path, path: str, error: str) -> None:+ state_dir, _, _, scan_id, _ = budget_scan_fixture(tmp_path, candidates=[budget_candidate(path)])rejected = complete_budget_scan(state_dir, scan_id, check=False)assert rejected["returncode"] != 0- assert "candidate location must be repository-relative" in str(rejected["stderr"])+ assert error in str(rejected["stderr"])def test_budget_exhaustion_preserves_out_of_scope_supporting_evidence(tmp_path: Path) -> None:- state_dir, _, scan_dir, scan_id, ledger = budget_scan_fixture(tmp_path)- candidate = json.loads(ledger.read_text())+ candidate = budget_candidate()candidate["locations"].append({"path": "shared/control.py", "start_line": 9, "end_line": 9, "role": "root_control"})- ledger.write_text(f"{json.dumps(candidate)}\n")+ state_dir, _, scan_dir, scan_id, _ = budget_scan_fixture(tmp_path, candidates=[candidate])completed = complete_budget_scan(state_dir, scan_id)["scan"]@@ -543,13 +516,11 @@ def test_budget_exhaustion_preserves_out_of_scope_supporting_evidence(tmp_path:def test_budget_exhaustion_preserves_unicode_line_separator_inventory(tmp_path: Path) -> None:- candidate = {- "candidate_id": "candidate-1",- "cwe_ids": ["CWE-89"],- "locations": [{"path": "dir\u2028name.py", "start_line": 1, "end_line": 1, "role": "sink"}],- "summary": "Candidate in a Unicode filename",- "evidence": "The source contains an untrusted SQL expression.",- }+ candidate = budget_candidate(+ "dir\u2028name.py",+ summary="Candidate in a Unicode filename",+ evidence="The source contains an untrusted SQL expression.",+ )state_dir, target, scan_dir, scan_id, _ = budget_scan_fixture(tmp_path, candidates=[candidate])(target / "dir\u2028name.py").write_text("query = value\n")(scan_dir / "artifacts" / "02_discovery" / "in_scope_files.txt").write_text(@@ -641,15 +612,14 @@ def test_completion_warns_after_plain_directory_changes(tmp_path: Path) -> None:saved = create_saved_workspace(state_dir, target)started = start_delivered_scan(state_dir,- "--workspace-id",str(saved["id"]),"--scan-root",str(tmp_path / "scans"),)- scan_id = str(started["results"]["scanId"])- scan_dir = Path(str(started["results"]["scanDir"]))+ scan_id = str(started["scanId"])+ scan_dir = Path(str(started["scanDir"]))write_completed_contract(scan_dir, scan_id, target, relative_path="app.py")- original_digest = started["results"]["contract"]["target"]["requiredSnapshotDigest"]+ original_digest = started["contract"]["target"]["requiredSnapshotDigest"]source.write_text("version = 2\n")completed = scan_command(state_dir, "complete-scan", scan_id)@@ -723,14 +693,13 @@ def test_workbench_persists_scan_model_and_updates_it_from_progress(tmp_path: Pasaved = create_saved_workspace(state_dir, target)started = start_delivered_scan(state_dir,- "--workspace-id",str(saved["id"]),"--model","gpt-5.6-sol","--reasoning-effort","high",)- scan = started["results"]+ scan = startedscan_id = str(scan["scanId"])assert scan["model"] == "gpt-5.6-sol"assert scan["reasoningEffort"] == "high"@@ -1048,11 +1017,9 @@ def test_completed_finding_triage_and_remediation_persist(tmp_path: Path, git_au"Patched before this scan completed.",)- request_id = str(uuid.uuid4())+ request = RemediationRequest(state_dir, occurrence_id)generation_token = str(uuid.uuid4())- closed_request = request_remediation(- state_dir, occurrence_id, request_id, generation_token, check=False- )+ closed_request = request.start(generation_token, check=False)assert "Reopen this finding" in str(closed_request["stderr"])set_triage(state_dir, occurrence_id, "open")with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:@@ -1068,7 +1035,7 @@ def test_completed_finding_triage_and_remediation_persist(tmp_path: Path, git_au("closed", "already_fixed", "Patched before this scan completed."),("open", None, None),]- requested = request_remediation(state_dir, occurrence_id, request_id, generation_token)["scan"]+ requested = request.start(generation_token)["scan"]assert requested["findings"][0]["remediationState"]["state"] == "requested"assert requested["findings"][0]["remediationState"]["pendingAction"] == "generate"assert requested["findings"][0]["remediationState"]["actionClaimToken"] == generation_token@@ -1080,17 +1047,9 @@ def test_completed_finding_triage_and_remediation_persist(tmp_path: Path, git_aupatch_path = scan_dir / patch_namewrite_remediation_patch(patch_path, newline="\n")(scan_dir / patch_name.strip()).write_text("different patch contents\n")- generated = set_remediation(- state_dir,- occurrence_id,- request_id,+ generated = request.generate(generation_token,- "1",- "generated",- "--patch-path",- patch_name,- "--patch-digest",- f"sha256:{hashlib.sha256(patch_path.read_bytes()).hexdigest()}",+ patch_path,"--summary","Contained archive extraction under the selected output directory.",)["scan"]@@ -1108,23 +1067,15 @@ def test_completed_finding_triage_and_remediation_persist(tmp_path: Path, git_au"Contained archive extraction under the selected output directory.")apply_token = str(uuid.uuid4())- apply_requested = request_remediation_action(- state_dir, occurrence_id, request_id, "2", "apply", apply_token- )["scan"]+ apply_requested = request.request_action("2", "apply", apply_token)["scan"]assert apply_requested["findings"][0]["remediationState"]["pendingAction"] == "apply"assert apply_requested["findings"][0]["remediationState"]["actionClaimToken"] == apply_tokenassert apply_requested["findings"][0]["remediationState"]["version"] == 3- apply_retried = request_remediation_action(- state_dir, occurrence_id, request_id, "2", "apply", apply_token- )["scan"]+ apply_retried = request.request_action("2", "apply", apply_token)["scan"]assert apply_retried["findings"][0]["remediationState"]["version"] == 3- wrong_apply = request_remediation_action(- state_dir, occurrence_id, request_id, "2", "apply", generation_token, check=False- )+ wrong_apply = request.request_action("2", "apply", generation_token, check=False)assert "operation is already pending" in str(wrong_apply["stderr"])- live_resend = claim_remediation_resend(- state_dir, occurrence_id, request_id, str(uuid.uuid4()), check=False- )+ live_resend = request.claim_resend(str(uuid.uuid4()), check=False)assert "still owned by another panel" in str(live_resend["stderr"])with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:connection.execute(@@ -1133,53 +1084,23 @@ def test_completed_finding_triage_and_remediation_persist(tmp_path: Path, git_auSET pending_action_claimed_at = ?WHERE request_id = ?""",- ("2000-01-01T00:00:00Z", request_id),+ ("2000-01-01T00:00:00Z", request.id),)resend_token = str(uuid.uuid4())- stale_resend = claim_remediation_resend(state_dir, occurrence_id, request_id, resend_token)[- "scan"- ]+ stale_resend = request.claim_resend(resend_token)["scan"]assert stale_resend["findings"][0]["remediationState"]["actionClaimToken"] == resend_token- wrong_release = run_workbench(- state_dir,- "release-finding-remediation-claim",- "--occurrence-id",- occurrence_id,- "--request-id",- request_id,- "--action-token",- str(uuid.uuid4()),- )["scan"]+ wrong_release = request.release_claim(str(uuid.uuid4()))["scan"]assert wrong_release["findings"][0]["remediationState"]["actionClaimToken"] == resend_token- released = run_workbench(- state_dir,- "release-finding-remediation-claim",- "--occurrence-id",- occurrence_id,- "--request-id",- request_id,- "--action-token",- resend_token,- )["scan"]+ released = request.release_claim(resend_token)["scan"]assert released["findings"][0]["remediationState"]["actionClaimToken"] is None- unowned_apply = set_remediation(- state_dir,- occurrence_id,- request_id,- resend_token,- "3",- "applied",- "--base-revision",- "unversioned",- check=False,+ unowned_apply = request.update(+ resend_token, "3", "applied", "--base-revision", "unversioned", check=False)assert "does not have an owned pending host request" in str(unowned_apply["stderr"])retry_token = str(uuid.uuid4())- reclaimed = claim_remediation_resend(state_dir, occurrence_id, request_id, retry_token)["scan"]+ reclaimed = request.claim_resend(retry_token)["scan"]assert reclaimed["findings"][0]["remediationState"]["actionClaimToken"] == retry_token- delivered = mark_remediation_delivered(state_dir, occurrence_id, request_id, retry_token)[- "scan"- ]+ delivered = request.mark_delivered(retry_token)["scan"]assert delivered["findings"][0]["remediationState"]["actionDeliveredAt"]with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:connection.execute(@@ -1188,23 +1109,18 @@ def test_completed_finding_triage_and_remediation_persist(tmp_path: Path, git_auSET pending_action_claimed_at = ?, pending_action_delivered_at = ?WHERE request_id = ?""",- ("2000-01-01T00:00:00Z", "2000-01-01T00:00:00Z", request_id),+ ("2000-01-01T00:00:00Z", "2000-01-01T00:00:00Z", request.id),)recovery_token = str(uuid.uuid4())- recovered_delivery = claim_remediation_resend(- state_dir, occurrence_id, request_id, recovery_token- )["scan"]+ recovered_delivery = request.claim_resend(recovery_token)["scan"]assert (recovered_delivery["findings"][0]["remediationState"]["actionClaimToken"] == recovery_token)assert recovered_delivery["findings"][0]["remediationState"]["actionDeliveredAt"] is None- mark_remediation_delivered(state_dir, occurrence_id, request_id, recovery_token)+ request.mark_delivered(recovery_token)replacement_path = scan_dir / "replacement.patch"replacement_path.write_text("diff --git a/src/other.py b/src/other.py\n")- replaced_patch = set_remediation(- state_dir,- occurrence_id,- request_id,+ replaced_patch = request.update(recovery_token,"3","applied",@@ -1217,16 +1133,8 @@ def test_completed_finding_triage_and_remediation_persist(tmp_path: Path, git_aucheck=False,)assert "cannot replace its reviewed patch path" in str(replaced_patch["stderr"])- unchanged_apply = set_remediation(- state_dir,- occurrence_id,- request_id,- recovery_token,- "3",- "applied",- "--base-revision",- "unversioned",- check=False,+ unchanged_apply = request.update(+ recovery_token, "3", "applied", "--base-revision", "unversioned", check=False)assert "checkout is unchanged" in str(unchanged_apply["stderr"])subprocess.run(@@ -1237,23 +1145,12 @@ def test_completed_finding_triage_and_remediation_persist(tmp_path: Path, git_auapplied_source = source.read_bytes()unrelated = target / "unrelated.txt"unrelated.write_text("not part of the reviewed patch\n")- extra_changes = set_remediation(- state_dir,- occurrence_id,- request_id,- recovery_token,- "3",- "applied",- "--base-revision",- "unversioned",- check=False,+ extra_changes = request.update(+ recovery_token, "3", "applied", "--base-revision", "unversioned", check=False)assert "changes outside the reviewed patch" in str(extra_changes["stderr"])unrelated.unlink()- applied = set_remediation(- state_dir,- occurrence_id,- request_id,+ applied = request.update(recovery_token,"3","applied",@@ -1264,20 +1161,11 @@ def test_completed_finding_triage_and_remediation_persist(tmp_path: Path, git_au)["scan"]assert applied["findings"][0]["remediationState"]["state"] == "applied"verify_token = str(uuid.uuid4())- verify_requested = request_remediation_action(- state_dir, occurrence_id, request_id, "4", "verify", verify_token- )["scan"]+ verify_requested = request.request_action("4", "verify", verify_token)["scan"]assert verify_requested["findings"][0]["remediationState"]["pendingAction"] == "verify"- verifying = set_remediation(- state_dir,- occurrence_id,- request_id,- verify_token,- "5",- "verifying",- "--base-revision",- "unversioned",- )["scan"]+ verifying = request.update(verify_token, "5", "verifying", "--base-revision", "unversioned")[+ "scan"+ ]assert verifying["findings"][0]["remediationState"]["state"] == "verifying"assert verifying["findings"][0]["remediationState"]["pendingAction"] == "verify"assert verifying["findings"][0]["remediationState"]["actionClaimToken"] == verify_token@@ -1288,28 +1176,16 @@ def test_completed_finding_triage_and_remediation_persist(tmp_path: Path, git_auSET pending_action_claimed_at = ?WHERE request_id = ?""",- ("2000-01-01T00:00:00Z", request_id),+ ("2000-01-01T00:00:00Z", request.id),)recovery_token = str(uuid.uuid4())- recovered = claim_remediation_resend(state_dir, occurrence_id, request_id, recovery_token)[- "scan"- ]+ recovered = request.claim_resend(recovery_token)["scan"]assert recovered["findings"][0]["remediationState"]["actionClaimToken"] == recovery_token- verifying_again = set_remediation(- state_dir,- occurrence_id,- request_id,- recovery_token,- "6",- "verifying",- "--base-revision",- "unversioned",+ verifying_again = request.update(+ recovery_token, "6", "verifying", "--base-revision", "unversioned")["scan"]assert verifying_again["findings"][0]["remediationState"]["pendingAction"] == "verify"- verified = set_remediation(- state_dir,- occurrence_id,- request_id,+ verified = request.update(recovery_token,"7","verified",@@ -1425,9 +1301,7 @@ def assert_completed_scan_disables_remediation_after_checkout_path_is_replaced(assert refreshed["remediationAvailable"] is Falseassert "checkout path was replaced" in refreshed["remediationUnavailableReason"]assert "absolutePath" not in refreshed["findings"][0]["locations"][0]- rejected = request_remediation(- state_dir, occurrence_id, str(uuid.uuid4()), str(uuid.uuid4()), check=False- )+ rejected = RemediationRequest(state_dir, occurrence_id).start(str(uuid.uuid4()), check=False)assert "checkout path was replaced" in str(rejected["stderr"])@@ -1464,24 +1338,21 @@ def test_finding_management_rejects_invalid_state_transitions(tmp_path: Path) ->missing_wont_fix_rationale["stderr"])- invalid_transition = set_remediation(- state_dir, occurrence_id, str(uuid.uuid4()), str(uuid.uuid4()), "1", "verified", check=False+ invalid_transition = RemediationRequest(state_dir, occurrence_id).update(+ str(uuid.uuid4()), "1", "verified", check=False)assert "remediation request not found" in str(invalid_transition["stderr"])- request_id = str(uuid.uuid4())+ request = RemediationRequest(state_dir, occurrence_id)generation_token = str(uuid.uuid4())- request_remediation(state_dir, occurrence_id, request_id, generation_token)- overlapping_request = request_remediation(- state_dir, occurrence_id, str(uuid.uuid4()), str(uuid.uuid4()), check=False+ request.start(generation_token)+ overlapping_request = RemediationRequest(state_dir, occurrence_id).start(+ str(uuid.uuid4()), check=False)assert "active remediation operation" in str(overlapping_request["stderr"])patch_path = scan_dir / "remediation.patch"patch_path.write_text("diff --git a/src/extract.py b/src/extract.py\n")- digest_mismatch = set_remediation(- state_dir,- occurrence_id,- request_id,+ digest_mismatch = request.update(generation_token,"1","generated",@@ -1496,36 +1367,19 @@ def test_finding_management_rejects_invalid_state_transitions(tmp_path: Path) ->patch_path.write_bytes(b"diff --git a/src/extract.py b/src/extract.py\n+" + b"x" * (2 * 1024 * 1024))- set_remediation(- state_dir,- occurrence_id,- request_id,- generation_token,- "1",- "generated",- "--patch-path",- patch_path.name,- "--patch-digest",- f"sha256:{hashlib.sha256(patch_path.read_bytes()).hexdigest()}",- )+ request.generate(generation_token, patch_path)generated = get_scan(state_dir, scan_id)["scan"]remediation = generated["findings"][0]["remediationState"]assert remediation["patch"].endswith("... patch preview truncated ...")assert remediation["patchStats"]["previewTruncated"] is True- replayed_generation = set_remediation(- state_dir, occurrence_id, request_id, generation_token, "2", "generated", check=False- )+ replayed_generation = request.update(generation_token, "2", "generated", check=False)assert "does not have an owned pending host request" in str(replayed_generation["stderr"])patch_path.write_text("tampered after review\n")refreshed = get_scan(state_dir, scan_id)["scan"]assert refreshed["findings"][0]["remediationState"]["patch"] is None- tampered_apply = request_remediation_action(- state_dir, occurrence_id, request_id, "2", "apply", str(uuid.uuid4()), check=False- )+ tampered_apply = request.request_action("2", "apply", str(uuid.uuid4()), check=False)assert "Patch digest does not match" in str(tampered_apply["stderr"])- stale_replay = set_remediation(- state_dir, occurrence_id, request_id, generation_token, "1", "failed", check=False- )+ stale_replay = request.update(generation_token, "1", "failed", check=False)assert "changed. Refresh it" in str(stale_replay["stderr"])@@ -1551,13 +1405,12 @@ def test_finding_remediation_rejects_apply_after_checkout_changes(tmp_path: Pathworkspace_id = str(create_saved_git_workspace(state_dir, target)["id"])started = start_delivered_scan(state_dir,- "--workspace-id",workspace_id,"--scan-root",str(tmp_path / "scans"),)- scan_id = str(started["results"]["scanId"])- scan_dir = Path(str(started["results"]["scanDir"]))+ scan_id = str(started["scanId"])+ scan_dir = Path(str(started["scanDir"]))write_completed_contract(scan_dir,scan_id,@@ -1565,14 +1418,14 @@ def test_finding_remediation_rejects_apply_after_checkout_changes(tmp_path: Pathrelative_path="README.md",target_kind="git_worktree",target_revision=revision,- snapshot_digest=str(started["results"]["contract"]["target"]["requiredSnapshotDigest"]),+ snapshot_digest=str(started["contract"]["target"]["requiredSnapshotDigest"]),)completed = scan_command(state_dir, "complete-scan", scan_id)["scan"]scan_dir = Path(str(completed["scanDir"]))occurrence_id = str(completed["findings"][0]["occurrenceId"])- request_id = str(uuid.uuid4())+ request = RemediationRequest(state_dir, occurrence_id)generation_token = str(uuid.uuid4())- request_remediation(state_dir, occurrence_id, request_id, generation_token)+ request.start(generation_token)patch_path = scan_dir / "remediation.patch"patch_path.write_text("diff --git a/README.md b/README.md\n"@@ -1583,42 +1436,17 @@ def test_finding_remediation_rejects_apply_after_checkout_changes(tmp_path: Path"+fixed\n")(target / "README.md").write_text("changed while generating\n")- stale_generation = set_remediation(- state_dir,- occurrence_id,- request_id,- generation_token,- "1",- "generated",- "--patch-path",- patch_path.name,- "--patch-digest",- f"sha256:{hashlib.sha256(patch_path.read_bytes()).hexdigest()}",- check=False,- )+ stale_generation = request.generate(generation_token, patch_path, check=False)assert "Working-tree contents changed" in str(stale_generation["stderr"])(target / "README.md").write_text("fixture\n")- set_remediation(- state_dir,- occurrence_id,- request_id,- generation_token,- "1",- "generated",- "--patch-path",- patch_path.name,- "--patch-digest",- f"sha256:{hashlib.sha256(patch_path.read_bytes()).hexdigest()}",- )+ request.generate(generation_token, patch_path)(target / "README.md").write_text("changed after patch generation\n")- stale_checkout = request_remediation_action(- state_dir, occurrence_id, request_id, "2", "apply", str(uuid.uuid4()), check=False- )+ stale_checkout = request.request_action("2", "apply", str(uuid.uuid4()), check=False)assert stale_checkout["returncode"] != 0assert "Working-tree contents changed" in str(stale_checkout["stderr"])(target / "README.md").write_text("fixture\n")apply_token = str(uuid.uuid4())- request_remediation_action(state_dir, occurrence_id, request_id, "2", "apply", apply_token)+ request.request_action("2", "apply", apply_token)subprocess.run(["git", "apply", "--directory=nested-target", str(patch_path)],cwd=repository,@@ -1635,29 +1463,12 @@ def test_finding_remediation_rejects_apply_after_checkout_changes(tmp_path: Pathignored_entry.write_text("ignored runtime data\n")unrelated = target / "unrelated.txt"unrelated.write_text("not reviewed\n")- extra_changes = set_remediation(- state_dir,- occurrence_id,- request_id,- apply_token,- "3",- "applied",- "--base-revision",- revision,- check=False,+ extra_changes = request.update(+ apply_token, "3", "applied", "--base-revision", revision, check=False)assert "changes outside the reviewed patch" in str(extra_changes["stderr"])unrelated.unlink()- applied = set_remediation(- state_dir,- occurrence_id,- request_id,- apply_token,- "3",- "applied",- "--base-revision",- revision,- )+ applied = request.update(apply_token, "3", "applied", "--base-revision", revision)assert applied["scan"]["findings"][0]["remediationState"]["state"] == "applied"@@ -1667,40 +1478,21 @@ def test_finding_remediation_rejects_delayed_update_after_superseding_patch(tmp_completed = scan_command(state_dir, "complete-scan", scan_id)["scan"]scan_dir = Path(str(completed["scanDir"]))occurrence_id = str(completed["findings"][0]["occurrenceId"])- first_request_id = str(uuid.uuid4())+ first_request = RemediationRequest(state_dir, occurrence_id)first_generation_token = str(uuid.uuid4())- request_remediation(state_dir, occurrence_id, first_request_id, first_generation_token)+ first_request.start(first_generation_token)patch_path = scan_dir / "remediation.patch"patch_path.write_text("diff --git a/src/extract.py b/src/extract.py\n")- set_remediation(- state_dir,- occurrence_id,- first_request_id,- first_generation_token,- "1",- "generated",- "--patch-path",- patch_path.name,- "--patch-digest",- f"sha256:{hashlib.sha256(patch_path.read_bytes()).hexdigest()}",- )- request_remediation(state_dir, occurrence_id, str(uuid.uuid4()), str(uuid.uuid4()))- delayed = set_remediation(- state_dir,- occurrence_id,- first_request_id,- first_generation_token,- "2",- "applied",- "--base-revision",- "unversioned",- check=False,+ first_request.generate(first_generation_token, patch_path)+ RemediationRequest(state_dir, occurrence_id).start(str(uuid.uuid4()))+ delayed = first_request.update(+ first_generation_token, "2", "applied", "--base-revision", "unversioned", check=False)assert "changed. Refresh it" in str(delayed["stderr"])with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:assert connection.execute("SELECT state, version FROM finding_remediation_attempts WHERE request_id = ?",- (first_request_id,),+ (first_request.id,),).fetchone() == ("superseded", 3)@@ -1713,43 +1505,18 @@ def test_finding_remediation_rejects_unversioned_directory_changes(tmp_path: Patcompleted = scan_command(state_dir, "complete-scan", scan_id)["scan"]scan_dir = Path(str(completed["scanDir"]))occurrence_id = str(completed["findings"][0]["occurrenceId"])- request_id = str(uuid.uuid4())+ request = RemediationRequest(state_dir, occurrence_id)generation_token = str(uuid.uuid4())- request_remediation(state_dir, occurrence_id, request_id, generation_token)+ request.start(generation_token)patch_path = scan_dir / "remediation.patch"patch_path.write_text("diff --git a/source.txt b/source.txt\n")source.write_text("changed while generating\n")- stale_generation = set_remediation(- state_dir,- occurrence_id,- request_id,- generation_token,- "1",- "generated",- "--patch-path",- patch_path.name,- "--patch-digest",- f"sha256:{hashlib.sha256(patch_path.read_bytes()).hexdigest()}",- check=False,- )+ stale_generation = request.generate(generation_token, patch_path, check=False)assert "Working-tree contents changed" in str(stale_generation["stderr"])source.write_text("original\n")- set_remediation(- state_dir,- occurrence_id,- request_id,- generation_token,- "1",- "generated",- "--patch-path",- patch_path.name,- "--patch-digest",- f"sha256:{hashlib.sha256(patch_path.read_bytes()).hexdigest()}",- )+ request.generate(generation_token, patch_path)source.write_text("changed before apply\n")- stale_apply = request_remediation_action(- state_dir, occurrence_id, request_id, "2", "apply", str(uuid.uuid4()), check=False- )+ stale_apply = request.request_action("2", "apply", str(uuid.uuid4()), check=False)assert "Working-tree contents changed" in str(stale_apply["stderr"])@@ -2137,8 +1904,8 @@ def test_workbench_preserves_url_user_context_on_workspace_creation(tmp_path: Padef test_workbench_replaces_context_only_for_running_owned_scan(tmp_path: Path) -> None:state_dir, target = empty_target_paths(tmp_path)workspace = create_saved_workspace(state_dir, target)- started = start_delivered_scan(state_dir, "--workspace-id", str(workspace["id"]))- scan_id = str(started["results"]["scanId"])+ started = start_delivered_scan(state_dir, str(workspace["id"]))+ scan_id = str(started["scanId"])updated = scan_command(state_dir,@@ -2295,6 +2062,14 @@ def create_saved_working_tree_workspace()+def start_working_tree_scan(+ state_dir: Path, target: Path, revision: str, scan_root: Path+) -> dict[str, object]:+ workspace_id = str(uuid.uuid4())+ create_saved_working_tree_workspace(state_dir, target, workspace_id, revision)+ return start_delivered_scan(state_dir, workspace_id, "--scan-root", str(scan_root))++def test_workbench_rejects_working_tree_target_after_head_moves(tmp_path: Path) -> None:state_dir = tmp_path / "state"target = tmp_path / "target"@@ -2342,18 +2117,10 @@ def test_workbench_warns_after_working_tree_changes(tmp_path: Path) -> None:).stdout.strip()(target / "README.md").write_text("scanned working-tree source\n" * 50)(target / "new-file.txt").write_text("selected content\n")- workspace_id = str(uuid.uuid4())- create_saved_working_tree_workspace(state_dir, target, workspace_id, revision)- started = start_delivered_scan(- state_dir,- "--workspace-id",- workspace_id,- "--scan-root",- str(tmp_path / "scans"),- )- scan_id = str(started["results"]["scanId"])- scan_dir = Path(str(started["results"]["scanDir"]))- snapshot_digest = str(started["results"]["diffTarget"]["contentDigest"])+ started = start_working_tree_scan(state_dir, target, revision, tmp_path / "scans")+ scan_id = str(started["scanId"])+ scan_dir = Path(str(started["scanDir"]))+ snapshot_digest = str(started["diffTarget"]["contentDigest"])write_completed_contract(scan_dir,scan_id,@@ -2382,18 +2149,10 @@ def test_workbench_warns_after_working_tree_head_changes(state_dir = tmp_path / "state"target = tmp_path / "target"revision = initialize_git_repository(target)- workspace_id = str(uuid.uuid4())- create_saved_working_tree_workspace(state_dir, target, workspace_id, revision)- started = start_delivered_scan(- state_dir,- "--workspace-id",- workspace_id,- "--scan-root",- str(tmp_path / "scans"),- )- scan_id = str(started["results"]["scanId"])- scan_dir = Path(str(started["results"]["scanDir"]))- snapshot_digest = str(started["results"]["diffTarget"]["contentDigest"])+ started = start_working_tree_scan(state_dir, target, revision, tmp_path / "scans")+ scan_id = str(started["scanId"])+ scan_dir = Path(str(started["scanDir"]))+ snapshot_digest = str(started["diffTarget"]["contentDigest"])write_completed_contract(scan_dir,scan_id,@@ -2423,16 +2182,8 @@ def test_workbench_can_validate_legacy_nested_working_tree_scan(tmp_path: Path)revision = initialize_git_repository(target)nested_target = target / "nested"nested_target.mkdir()- workspace_id = str(uuid.uuid4())- create_saved_working_tree_workspace(state_dir, target, workspace_id, revision)- started = start_delivered_scan(- state_dir,- "--workspace-id",- workspace_id,- "--scan-root",- str(tmp_path / "scans"),- )- scan_id = str(started["results"]["scanId"])+ started = start_working_tree_scan(state_dir, target, revision, tmp_path / "scans")+ scan_id = str(started["scanId"])with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:connection.execute("UPDATE scans SET target_path = ? WHERE id = ?",@@ -2834,10 +2585,10 @@ def test_workbench_rejects_symlinked_scope_escape(tmp_path: Path) -> None:def test_workbench_rejects_progress_completed_above_total(tmp_path: Path) -> None:state_dir, target = empty_target_paths(tmp_path)saved = create_saved_workspace(state_dir, target)- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))+ started = start_delivered_scan(state_dir, str(saved["id"]))failed = update_progress(state_dir,- str(started["results"]["scanId"]),+ str(started["scanId"]),"--review-items-total","2","--review-items-completed",@@ -2851,8 +2602,8 @@ def test_workbench_rejects_progress_completed_above_total(tmp_path: Path) -> Nondef test_workbench_rejects_regressive_progress(tmp_path: Path) -> None:state_dir, target = empty_target_paths(tmp_path)saved = create_saved_workspace(state_dir, target)- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- scan_id = str(started["results"]["scanId"])+ started = start_delivered_scan(state_dir, str(saved["id"]))+ scan_id = str(started["scanId"])update_progress(state_dir,scan_id,@@ -2880,9 +2631,9 @@ def test_workbench_tracks_review_pass_for_deep_scan_only(tmp_path: Path) -> Noneworkspace_id = str(uuid.uuid4())create_workspace(state_dir, workspace_id, "--target-path", str(target), "--mode", "deep")saved = save_workspace(state_dir, workspace_id, str(target), ".", "deep")- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- scan_id = str(started["results"]["scanId"])- assert started["results"]["progress"]["reviewPass"] is None+ started = start_delivered_scan(state_dir, str(saved["id"]))+ scan_id = str(started["scanId"])+ assert started["progress"]["reviewPass"] is Noneupdate_progress(state_dir,@@ -2909,11 +2660,10 @@ def test_workbench_tracks_review_pass_for_deep_scan_only(tmp_path: Path) -> Nonestandard = create_saved_workspace(state_dir, standard_target)standard_scan = start_delivered_scan(state_dir,- "--workspace-id",str(standard["id"]),)failed = update_progress(- state_dir, str(standard_scan["results"]["scanId"]), "--deep-review-pass", "1", check=False+ state_dir, str(standard_scan["scanId"]), "--deep-review-pass", "1", check=False)assert failed["returncode"] != 0assert "Only Deep Scan" in str(failed["stderr"])@@ -2922,9 +2672,9 @@ def test_workbench_tracks_review_pass_for_deep_scan_only(tmp_path: Path) -> Nonedef test_workbench_updates_progress_timestamp_for_phase_and_failure(tmp_path: Path) -> None:state_dir, target = empty_target_paths(tmp_path)saved = create_saved_workspace(state_dir, target)- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- scan_id = str(started["results"]["scanId"])- started_at = str(started["results"]["progress"]["updatedAt"])+ started = start_delivered_scan(state_dir, str(saved["id"]))+ scan_id = str(started["scanId"])+ started_at = str(started["progress"]["updatedAt"])time.sleep(0.001)updated = update_progress(state_dir, scan_id, "--phase", "validation")updated_at = str(updated["scan"]["progress"]["updatedAt"])@@ -2937,12 +2687,12 @@ def test_workbench_updates_progress_timestamp_for_phase_and_failure(tmp_path: Padef test_workbench_preserves_scan_when_git_revision_cannot_be_rechecked(tmp_path: Path) -> None:state_dir, target = empty_target_paths(tmp_path)saved = create_saved_workspace(state_dir, target)- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- scan_id = str(started["results"]["scanId"])+ started = start_delivered_scan(state_dir, str(saved["id"]))+ scan_id = str(started["scanId"])with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:connection.execute("UPDATE scans SET target_revision = 'deadbeef' WHERE id = ?", (scan_id,))connection.commit()- scan_dir = Path(str(started["results"]["scanDir"]))+ scan_dir = Path(str(started["scanDir"]))write_completed_contract(scan_dir,scan_id,@@ -2975,9 +2725,9 @@ def test_completed_finding_projects_writeup_and_poc_artifact_paths() -> None:state_dir, target = empty_target_paths(tmp_path)saved = create_saved_workspace(state_dir, target)- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- scan_id = str(started["results"]["scanId"])- scan_dir = Path(str(started["results"]["scanDir"]))+ started = start_delivered_scan(state_dir, str(saved["id"]))+ scan_id = str(started["scanId"])+ scan_dir = Path(str(started["scanDir"]))write_completed_contract(scan_dir, scan_id, target)slug = "unsafe-archive-extraction"@@ -3059,12 +2809,12 @@ def test_workbench_populates_clean_git_scan_revision_with_large_source_excerpt(text=True,).stdout.strip()saved = create_saved_git_workspace(state_dir, target)- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- scan_id = str(started["results"]["scanId"])- assert started["results"]["contract"]["target"]["allowedKinds"] == ["git_revision"]- assert "requiredSnapshotDigest" not in started["results"]["contract"]["target"]+ started = start_delivered_scan(state_dir, str(saved["id"]))+ scan_id = str(started["scanId"])+ assert started["contract"]["target"]["allowedKinds"] == ["git_revision"]+ assert "requiredSnapshotDigest" not in started["contract"]["target"]write_completed_contract(- Path(str(started["results"]["scanDir"])),+ Path(str(started["scanDir"])),scan_id,target,relative_path=relative_path,@@ -3073,15 +2823,11 @@ def test_workbench_populates_clean_git_scan_revision_with_large_source_excerpt()completed = scan_command(state_dir, "complete-scan", scan_id)assert completed["scan"]["progress"]["status"] == "complete"- manifest = json.loads(- (Path(str(started["results"]["scanDir"])) / "scan-manifest.json").read_text()- )+ manifest = json.loads((Path(str(started["scanDir"])) / "scan-manifest.json").read_text())assert manifest["scan"]["target"]["revision"] == revisionassert "snapshotDigest" not in manifest["scan"]["target"]finding = completed["scan"]["findings"][0]- saved_findings = json.loads(- (Path(str(started["results"]["scanDir"])) / "findings.json").read_text()- )+ saved_findings = json.loads((Path(str(started["scanDir"])) / "findings.json").read_text())assert saved_findings["findings"][0]["locations"][0]["path"] == relative_pathif long_path:assert len(relative_path.encode()) > 2048@@ -3186,9 +2932,9 @@ def test_workbench_preserves_in_flight_git_scan_during_migration_normalization(target = tmp_path / "target"revision = initialize_git_repository(target)saved = create_saved_git_workspace(state_dir, target)- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- scan_id = str(started["results"]["scanId"])- allowed_kinds = started["results"]["contract"]["target"]["allowedKinds"]+ started = start_delivered_scan(state_dir, str(saved["id"]))+ scan_id = str(started["scanId"])+ allowed_kinds = started["contract"]["target"]["allowedKinds"]database = state_dir / "workbench.sqlite3"with sqlite3.connect(database) as connection:connection.execute("DELETE FROM schema_migrations WHERE version = 2")@@ -3208,7 +2954,7 @@ def test_workbench_preserves_in_flight_git_scan_during_migration_normalization(migrated = get_scan(state_dir, scan_id)assert migrated["scan"]["contract"]["target"]["allowedKinds"] == allowed_kindswrite_completed_contract(- Path(str(started["results"]["scanDir"])),+ Path(str(started["scanDir"])),scan_id,target,target_kind="git_revision",@@ -3225,12 +2971,12 @@ def test_workbench_preserves_dirty_git_scan_after_worktree_changes(tmp_path: Patdirty_content = "fixture\nlocal change\n"(target / "README.md").write_text(dirty_content)saved = create_saved_git_workspace(state_dir, target)- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- scan_id = str(started["results"]["scanId"])- contract = started["results"]["contract"]["target"]+ started = start_delivered_scan(state_dir, str(saved["id"]))+ scan_id = str(started["scanId"])+ contract = started["contract"]["target"]assert contract["allowedKinds"] == ["git_worktree"]write_completed_contract(- Path(str(started["results"]["scanDir"])),+ Path(str(started["scanDir"])),scan_id,target,target_kind="git_revision",@@ -3240,18 +2986,16 @@ def test_workbench_preserves_dirty_git_scan_after_worktree_changes(tmp_path: Pat# registered worktree kind and digest instead of discarding the scan.completed = scan_command(state_dir, "complete-scan", scan_id)assert completed["scan"]["progress"]["status"] == "complete"- manifest = json.loads(- (Path(str(started["results"]["scanDir"])) / "scan-manifest.json").read_text()- )+ manifest = json.loads((Path(str(started["scanDir"])) / "scan-manifest.json").read_text())assert manifest["scan"]["target"]["kind"] == "git_worktree"assert manifest["scan"]["target"]["snapshotDigest"] == contract["requiredSnapshotDigest"]- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- scan_id = str(started["results"]["scanId"])- snapshot_digest = str(started["results"]["contract"]["target"]["requiredSnapshotDigest"])+ started = start_delivered_scan(state_dir, str(saved["id"]))+ scan_id = str(started["scanId"])+ snapshot_digest = str(started["contract"]["target"]["requiredSnapshotDigest"])write_completed_contract(- Path(str(started["results"]["scanDir"])),+ Path(str(started["scanDir"])),scan_id,target,target_kind="git_worktree",@@ -3262,9 +3006,7 @@ def test_workbench_preserves_dirty_git_scan_after_worktree_changes(tmp_path: Patcompleted = scan_command(state_dir, "complete-scan", scan_id)assert completed["scan"]["progress"]["status"] == "complete"assert completed["scan"]["warnings"] == [WORKTREE_CHANGED_WARNING]- manifest = json.loads(- (Path(str(started["results"]["scanDir"])) / "scan-manifest.json").read_text()- )+ manifest = json.loads((Path(str(started["scanDir"])) / "scan-manifest.json").read_text())assert manifest["scan"]["target"]["revision"] == revisionassert manifest["scan"]["target"]["snapshotDigest"] == snapshot_digest@@ -3272,9 +3014,9 @@ def test_workbench_preserves_dirty_git_scan_after_worktree_changes(tmp_path: Patdef test_workbench_generates_reports_during_completion(tmp_path: Path) -> None:state_dir, target = empty_target_paths(tmp_path)saved = create_saved_workspace(state_dir, target)- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- scan_id = str(started["results"]["scanId"])- scan_dir = Path(str(started["results"]["scanDir"]))+ started = start_delivered_scan(state_dir, str(saved["id"]))+ scan_id = str(started["scanId"])+ scan_dir = Path(str(started["scanDir"]))write_completed_contract(scan_dir, scan_id, target)(scan_dir / "report.html").write_text("<p>Stale HTML report</p>")(scan_dir / "report.md").write_text("# Untrusted report\n")@@ -3294,9 +3036,9 @@ def test_workbench_omits_unsafe_symlink_source_path_without_hiding_finding(tmp_poutside.mkdir()(target / "src").symlink_to(outside, target_is_directory=True)saved = create_saved_workspace(state_dir, target)- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- scan_id = str(started["results"]["scanId"])- write_completed_contract(Path(str(started["results"]["scanDir"])), scan_id, target)+ started = start_delivered_scan(state_dir, str(saved["id"]))+ scan_id = str(started["scanId"])+ write_completed_contract(Path(str(started["scanDir"])), scan_id, target)completed = scan_command(state_dir, "complete-scan", scan_id)location = completed["scan"]["findings"][0]["locations"][0]assert location["path"] == "src/extract.py"@@ -3306,9 +3048,9 @@ def test_workbench_omits_unsafe_symlink_source_path_without_hiding_finding(tmp_pdef test_workbench_hides_missing_artifact_on_reopen(tmp_path: Path) -> None:state_dir, target = empty_target_paths(tmp_path)saved = create_saved_workspace(state_dir, target)- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- scan_id = str(started["results"]["scanId"])- scan_dir = Path(str(started["results"]["scanDir"]))+ started = start_delivered_scan(state_dir, str(saved["id"]))+ scan_id = str(started["scanId"])+ scan_dir = Path(str(started["scanDir"]))write_completed_contract(scan_dir, scan_id, target)completed = scan_command(state_dir, "complete-scan", scan_id)assert completed["scan"]["reportAvailable"] is Truediff --git a/plugins/codex-security/tests/test_workbench_db_exports.py b/plugins/codex-security/tests/test_workbench_db_exports.pyindex 5aaeb25c9..10f298816 100644--- a/plugins/codex-security/tests/test_workbench_db_exports.py+++ b/plugins/codex-security/tests/test_workbench_db_exports.py@@ -16,6 +16,7 @@ from types import SimpleNamespaceimport pytestfrom workbench_test_support import (+ RemediationRequest,create_saved_git_workspace,create_saved_workspace,create_workspace,@@ -25,16 +26,14 @@ from workbench_test_support import (get_scan,initialize_git_repository,mark_deep_coordinator_succeeded,+ read_draft_documents,read_review_state,- request_remediation,- request_remediation_action,resume_deep_scan,run_workbench,save_workspace,saved_coverage,saved_draft,scan_command,- set_remediation,set_triage,start_saved_scan,start_scan_command,@@ -85,14 +84,7 @@ def test_late_parent_draft_is_retained_without_mutating_frozen_stopped_seal() -> None:state_dir, target, scan_id, scan_dir = empty_target_scan(tmp_path)write_completed_contract(scan_dir, scan_id, target)- documents = {- key: json.loads((scan_dir / filename).read_text())- for key, filename in (- ("manifest", "scan-manifest.json"),- ("findings", "findings.json"),- ("coverage", "coverage.json"),- )- }+ documents = read_draft_documents(scan_dir)fail_scan(state_dir, scan_id, "Stopped.")stopped = get_scan(state_dir, scan_id)["scan"]seal = (scan_dir / "scan-manifest.json").read_bytes()@@ -445,14 +437,7 @@ def test_model_only_draft_is_available_before_findings_and_survives_stop(scan_id, scan_dir = start_workspace_scan(state_dir, str(saved["id"]), tmp_path / "scans")update_progress(state_dir, scan_id, "--phase", "threat_model")write_completed_contract(scan_dir, scan_id, target)- documents = {- key: json.loads((scan_dir / filename).read_text())- for key, filename in (- ("manifest", "scan-manifest.json"),- ("findings", "findings.json"),- ("coverage", "coverage.json"),- )- }+ documents = read_draft_documents(scan_dir)model = {"format": "markdown","content": "# Service Boundaries\n\nQueue producers and consumers.\n",@@ -554,14 +539,7 @@ def test_invalid_model_or_binding_keeps_history_available_for_semantic_repair(saved = create_saved_workspace(state_dir, target)scan_id, scan_dir = start_workspace_scan(state_dir, str(saved["id"]), tmp_path / "scans")write_completed_contract(scan_dir, scan_id, target)- documents = {- key: json.loads((scan_dir / filename).read_text())- for key, filename in (- ("manifest", "scan-manifest.json"),- ("findings", "findings.json"),- ("coverage", "coverage.json"),- )- }+ documents = read_draft_documents(scan_dir)model = {"summary": "Queue boundaries.", "assets": ["Stored messages"]}documents["manifest"]["scan"].update(threatModel=model, complete=False)malformed = json.loads(json.dumps(documents["manifest"]))@@ -1149,17 +1127,20 @@ def test_export_rejects_replaced_scan_directory(tmp_path: Path) -> None:assert not (outside / "exports" / "findings.csv").exists()-def test_csv_export_rejects_replaced_scan_directory_ancestor(tmp_path: Path) -> None:- state_dir, target, scan_id, scan_dir = empty_target_scan(tmp_path)- write_completed_contract(scan_dir, scan_id, target)- scan_command(state_dir, "complete-scan", scan_id)+def symlink_scan_parent(scan_dir: Path, replacement_parent: Path) -> Path:stored_parent = scan_dir.parentmoved_parent = stored_parent.with_name(f"{stored_parent.name}.moved")stored_parent.rename(moved_parent)- replacement_parent = tmp_path / "replacement-parent"shutil.copytree(moved_parent, replacement_parent)stored_parent.symlink_to(replacement_parent, target_is_directory=True)- replacement_scan_dir = replacement_parent / scan_dir.name+ return replacement_parent / scan_dir.name+++def test_csv_export_rejects_replaced_scan_directory_ancestor(tmp_path: Path) -> None:+ state_dir, target, scan_id, scan_dir = empty_target_scan(tmp_path)+ write_completed_contract(scan_dir, scan_id, target)+ scan_command(state_dir, "complete-scan", scan_id)+ replacement_scan_dir = symlink_scan_parent(scan_dir, tmp_path / "replacement-parent")failed = scan_command(state_dir, "export-findings", scan_id, "--format", "csv", check=False)assert failed["returncode"] != 0assert not (replacement_scan_dir / "exports" / "findings.csv").exists()@@ -1179,13 +1160,7 @@ def test_completion_rejects_replaced_scan_directory(tmp_path: Path) -> None:def test_completion_rejects_replaced_scan_directory_ancestor(tmp_path: Path) -> None:state_dir, target, scan_id, scan_dir = empty_target_scan(tmp_path)write_completed_contract(scan_dir, scan_id, target)- stored_parent = scan_dir.parent- moved_parent = stored_parent.with_name(f"{stored_parent.name}.moved")- stored_parent.rename(moved_parent)- replacement_parent = tmp_path / "replacement-parent"- shutil.copytree(moved_parent, replacement_parent)- stored_parent.symlink_to(replacement_parent, target_is_directory=True)- replacement_scan_dir = replacement_parent / scan_dir.name+ replacement_scan_dir = symlink_scan_parent(scan_dir, tmp_path / "replacement-parent")failed = scan_command(state_dir, "complete-scan", scan_id, check=False)assert failed["returncode"] != 0assert not (replacement_scan_dir / "exports" / "results.sarif").exists()@@ -1196,32 +1171,14 @@ def test_remediation_apply_rejects_replaced_scan_directory_ancestor(tmp_path: Pawrite_completed_contract(scan_dir, scan_id, target)completed = scan_command(state_dir, "complete-scan", scan_id)["scan"]occurrence_id = str(completed["findings"][0]["occurrenceId"])- request_id = str(uuid.uuid4())+ request = RemediationRequest(state_dir, occurrence_id)generation_token = str(uuid.uuid4())- request_remediation(state_dir, occurrence_id, request_id, generation_token)+ request.start(generation_token)patch_path = scan_dir / "remediation.patch"patch_path.write_text("diff --git a/src/extract.py b/src/extract.py\n")- set_remediation(- state_dir,- occurrence_id,- request_id,- generation_token,- "1",- "generated",- "--patch-path",- patch_path.name,- "--patch-digest",- f"sha256:{hashlib.sha256(patch_path.read_bytes()).hexdigest()}",- )- stored_parent = scan_dir.parent- moved_parent = stored_parent.with_name(f"{stored_parent.name}.moved")- stored_parent.rename(moved_parent)- replacement_parent = tmp_path / "replacement-parent"- shutil.copytree(moved_parent, replacement_parent)- stored_parent.symlink_to(replacement_parent, target_is_directory=True)- failed = request_remediation_action(- state_dir, occurrence_id, request_id, "2", "apply", str(uuid.uuid4()), check=False- )+ request.generate(generation_token, patch_path)+ symlink_scan_parent(scan_dir, tmp_path / "replacement-parent")+ failed = request.request_action("2", "apply", str(uuid.uuid4()), check=False)assert "canonical non-symlink directory" in str(failed["stderr"])@@ -1246,14 +1203,7 @@ def test_parent_draft_preserves_reconciled_candidate_identity_before_publicationtarget,coverage_mode="deep_repository" if mode == "deep" else "repository",)- documents = {- key: json.loads((scan_dir / filename).read_text())- for key, filename in (- ("manifest", "scan-manifest.json"),- ("findings", "findings.json"),- ("coverage", "coverage.json"),- )- }+ documents = read_draft_documents(scan_dir)if mode == "deep":documents["manifest"]["scan"]["complete"] = Falsedocuments["findings"]["findings"] = []@@ -1400,14 +1350,7 @@ def test_deep_draft_admission_survives_interrupted_projection(workspace = create_saved_workspace(state_dir, target, mode="deep")scan_id, scan_dir = start_workspace_scan(state_dir, str(workspace["id"]), tmp_path / "scans")write_completed_contract(scan_dir, scan_id, target, coverage_mode="deep_repository")- documents = {- key: json.loads((scan_dir / filename).read_text())- for key, filename in (- ("manifest", "scan-manifest.json"),- ("findings", "findings.json"),- ("coverage", "coverage.json"),- )- }+ documents = read_draft_documents(scan_dir)documents["manifest"]["scan"]["complete"] = Falseraw = {"scanId": scan_id,diff --git a/plugins/codex-security/tests/test_workbench_deep_scan.py b/plugins/codex-security/tests/test_workbench_deep_scan.pyindex 8e57b8131..77dd6eb95 100644--- a/plugins/codex-security/tests/test_workbench_deep_scan.py+++ b/plugins/codex-security/tests/test_workbench_deep_scan.py@@ -9,6 +9,7 @@ import subprocessimport sysimport uuidfrom concurrent.futures import ThreadPoolExecutor+from dataclasses import dataclassfrom datetime import datetime, timedelta, timezonefrom pathlib import Path@@ -81,9 +82,7 @@ def begin_target_scan()-def start_empty_target_scan(- tmp_path: Path, *, config: str | None = None-) -> tuple[Path, Path, str, Path]:+def start_empty_target_scan(tmp_path: Path, *, config: str | None = None) -> DeepScan:state_dir = tmp_path / "state"codex_home = tmp_path / "codex-home"if config is not None:@@ -91,7 +90,7 @@ def start_empty_target_scan(target = tmp_path / "target"target.mkdir()run = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")["deepScan"]- return state_dir, codex_home, str(run["scanId"]), Path(str(run["scanDir"]))+ return DeepScan(state_dir, codex_home, str(run["scanId"]), Path(str(run["scanDir"])))def restore_v53_schema(state_dir: Path) -> None:@@ -150,139 +149,185 @@ def write_canonical_artifacts(scan_dir: Path) -> dict[str, Path]:return paths-def upsert_worker(- state_dir: Path,- codex_home: Path,- *,- scan_id: str,- worker_id: str,- kind: str = "discovery",- status: str = "running",- prompt_path: Path,- artifact_dir: Path,- result_path: Path | None = None,- attempt: int | None = 1,- thread_id: str | None = None,- error: str | None = None,- replaceable_failure_kind: str | None = None,- coordinator_generation: int | None = None,-) -> dict[str, object]:- return upsert_deep_worker(- state_dir,- scan_id,- worker_id,- kind,- status,- str(prompt_path),- str(artifact_dir),- *(("--result-manifest-path", str(result_path)) if result_path is not None else ()),- *(("--attempt", str(attempt)) if attempt is not None else ()),- *(("--sdk-thread-id", thread_id) if thread_id is not None else ()),- *(("--error-message", error) if error is not None else ()),- *(- ("--replaceable-failure-kind", replaceable_failure_kind)- if replaceable_failure_kind is not None- else ()- ),- *(- ("--coordinator-generation", str(coordinator_generation))- if coordinator_generation is not None- else ()- ),- environment=deep_environment(codex_home),- )+@dataclass(frozen=True)+class DeepScan:+ state_dir: Path+ codex_home: Path+ id: str+ directory: Path+ def command(self, command: str, *args: str, check: bool = True) -> dict[str, object]:+ return scan_command(+ self.state_dir,+ command,+ self.id,+ *args,+ environment=deep_environment(self.codex_home),+ check=check,+ )-def dispatch_discovery_worker(- state_dir: Path,- codex_home: Path,- *,- scan_id: str,- scan_dir: Path,- name: str,- succeed: bool = True,- coordinator_generation: int | None = None,-) -> tuple[str, Path, Path, Path]:- worker_id = str(uuid.uuid4())- prompt_path, artifact_dir, result_path = worker_paths(scan_dir, name)- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,- worker_id=worker_id,- prompt_path=prompt_path,- artifact_dir=artifact_dir,- coordinator_generation=coordinator_generation,- )- if succeed:- result_path.write_text("{}\n")- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ def read(self, thread_id: str) -> dict[str, object]:+ return get_deep_scan(+ self.state_dir, self.id, thread_id, environment=deep_environment(self.codex_home)+ )["deepScan"]++ def commit_result(+ self, worker_id: str, result_path: Path, count: str, *extra: str+ ) -> dict[str, object]:+ return commit_deep_dedup(+ self.state_dir,+ self.id,+ worker_id,+ str(result_path),+ count,+ *extra,+ environment=deep_environment(self.codex_home),+ )++ def upsert_worker(+ self,+ *,+ worker_id: str,+ kind: str = "discovery",+ status: str = "running",+ prompt_path: Path,+ artifact_dir: Path,+ result_path: Path | None = None,+ attempt: int | None = 1,+ thread_id: str | None = None,+ error: str | None = None,+ replaceable_failure_kind: str | None = None,+ coordinator_generation: int | None = None,+ check: bool = True,+ ) -> dict[str, object]:+ return upsert_deep_worker(+ self.state_dir,+ self.id,+ worker_id,+ kind,+ status,+ str(prompt_path),+ str(artifact_dir),+ *(("--result-manifest-path", str(result_path)) if result_path is not None else ()),+ *(("--attempt", str(attempt)) if attempt is not None else ()),+ *(("--sdk-thread-id", thread_id) if thread_id is not None else ()),+ *(("--error-message", error) if error is not None else ()),+ *(+ ("--replaceable-failure-kind", replaceable_failure_kind)+ if replaceable_failure_kind is not None+ else ()+ ),+ *(+ ("--coordinator-generation", str(coordinator_generation))+ if coordinator_generation is not None+ else ()+ ),+ environment=deep_environment(self.codex_home),+ check=check,+ )++ def dispatch_discovery_worker(+ self,+ *,+ name: str,+ succeed: bool = True,+ coordinator_generation: int | None = None,+ ) -> tuple[str, Path, Path, Path]:+ worker_id = str(uuid.uuid4())+ prompt_path, artifact_dir, result_path = worker_paths(self.directory, name)+ self.upsert_worker(worker_id=worker_id,- status="succeeded",prompt_path=prompt_path,artifact_dir=artifact_dir,- result_path=result_path,coordinator_generation=coordinator_generation,)- return worker_id, prompt_path, artifact_dir, result_path+ if succeed:+ result_path.write_text("{}\n")+ self.upsert_worker(+ worker_id=worker_id,+ status="succeeded",+ prompt_path=prompt_path,+ artifact_dir=artifact_dir,+ result_path=result_path,+ coordinator_generation=coordinator_generation,+ )+ return worker_id, prompt_path, artifact_dir, result_path++ def claim_reducer(+ self,+ worker_id: str,+ prompt_path: Path,+ artifact_dir: Path,+ input_worker_ids: list[str],+ *,+ coordinator_generation: int | None = None,+ check: bool = True,+ ) -> dict[str, object]:+ return claim_deep_scan_dedup(+ self.state_dir,+ self.id,+ worker_id,+ str(prompt_path),+ str(artifact_dir),+ *(item for worker in input_worker_ids for item in ("--input-worker-id", worker)),+ *(+ ("--coordinator-generation", str(coordinator_generation))+ if coordinator_generation is not None+ else ()+ ),+ environment=deep_environment(self.codex_home),+ check=check,+ )+ def finish(+ self, reason: str, manifest: Path | str, *extra: str, check: bool = True+ ) -> dict[str, object]:+ return finish_deep_scan(+ self.state_dir,+ self.id,+ reason,+ str(manifest),+ *extra,+ environment=deep_environment(self.codex_home),+ check=check,+ )-def commit_reducer(- state_dir: Path,- codex_home: Path,- *,- scan_id: str,- scan_dir: Path,- name: str,- input_worker_ids: list[str],- new_findings_count: int,- coordinator_generation: int | None = None,-) -> dict[str, object]:- worker_id = str(uuid.uuid4())- prompt_path, artifact_dir, result_path = worker_paths(scan_dir, name)- claim_deep_scan_dedup(- state_dir,- scan_id,- worker_id,- str(prompt_path),- str(artifact_dir),- *(item for input_id in input_worker_ids for item in ("--input-worker-id", input_id)),- *(- ("--coordinator-generation", str(coordinator_generation))- if coordinator_generation is not None- else ()- ),- environment=deep_environment(codex_home),- )- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,- worker_id=worker_id,- kind="dedup",- prompt_path=prompt_path,- artifact_dir=artifact_dir,- coordinator_generation=coordinator_generation,- )- write_canonical_artifacts(scan_dir)- result_path.write_text("{}\n")- committed = commit_deep_dedup(- state_dir,- scan_id,- worker_id,- str(result_path),- str(new_findings_count),- *(- ("--coordinator-generation", str(coordinator_generation))- if coordinator_generation is not None- else ()- ),- environment=deep_environment(codex_home),- )["deepScan"]- return committed+ def commit_reducer(+ self,+ *,+ name: str,+ input_worker_ids: list[str],+ new_findings_count: int,+ coordinator_generation: int | None = None,+ ) -> dict[str, object]:+ worker_id = str(uuid.uuid4())+ prompt_path, artifact_dir, result_path = worker_paths(self.directory, name)+ self.claim_reducer(+ worker_id,+ prompt_path,+ artifact_dir,+ input_worker_ids,+ coordinator_generation=coordinator_generation,+ )+ self.upsert_worker(+ worker_id=worker_id,+ kind="dedup",+ prompt_path=prompt_path,+ artifact_dir=artifact_dir,+ coordinator_generation=coordinator_generation,+ )+ write_canonical_artifacts(self.directory)+ result_path.write_text("{}\n")+ return self.commit_result(+ worker_id,+ result_path,+ str(new_findings_count),+ *(+ ("--coordinator-generation", str(coordinator_generation))+ if coordinator_generation is not None+ else ()+ ),+ )["deepScan"]def test_existing_generation_safely_claims_and_reclaims_without_schema_migration(@@ -338,32 +383,28 @@ def test_existing_generation_safely_claims_and_reclaims_without_schema_migrationdef test_legacy_generation_with_active_worker_observes_grace_then_adopts(tmp_path: Path) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)- dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep = start_empty_target_scan(tmp_path)+ deep.dispatch_discovery_worker(name="legacy-active",succeed=False,)- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:connection.execute("UPDATE deep_scan_runs SET updated_at = ? WHERE scan_id = ?",- ((datetime.now(timezone.utc) - timedelta(seconds=60)).isoformat(), scan_id),+ ((datetime.now(timezone.utc) - timedelta(seconds=60)).isoformat(), deep.id),)- observed = claim_deep_scan_coordinator(state_dir, codex_home, scan_id)+ observed = claim_deep_scan_coordinator(deep.state_dir, deep.codex_home, deep.id)assert observed["coordinatorDisposition"] == "observing"assert observed["deepScan"]["coordinatorGeneration"] == 1assert observed["deepScan"]["workers"][0]["status"] == "running"- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:connection.execute("UPDATE deep_scan_runs SET updated_at = ? WHERE scan_id = ?",- ((datetime.now(timezone.utc) - timedelta(seconds=121)).isoformat(), scan_id),+ ((datetime.now(timezone.utc) - timedelta(seconds=121)).isoformat(), deep.id),)- adopted = claim_deep_scan_coordinator(state_dir, codex_home, scan_id)+ adopted = claim_deep_scan_coordinator(deep.state_dir, deep.codex_home, deep.id)assert adopted["coordinatorDisposition"] == "adopted"assert adopted["deepScan"]["coordinatorGeneration"] == 2assert adopted["deepScan"]["workers"][0]["status"] == "canceled"@@ -440,11 +481,8 @@ def test_paused_discovery_resumes_after_restart_with_original_handoff_claim(handoff_claim_token,environment=deep_environment(codex_home),)- completed_worker = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep = DeepScan(state_dir, codex_home, scan_id, scan_dir)+ completed_worker = deep.dispatch_discovery_worker(name="track-c-completed-discovery",)[0]paused = get_scan(state_dir, scan_id)["scan"]@@ -516,19 +554,11 @@ def test_paused_discovery_resumes_after_restart_with_original_handoff_claim()assert [worker["id"] for worker in recovered["workers"]] == [completed_worker]- replacement_worker = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ replacement_worker = deep.dispatch_discovery_worker(name="track-c-resumed-discovery",coordinator_generation=2,)[0]- reduced = commit_reducer(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ reduced = deep.commit_reducer(name="track-c-recovered-reducer",input_worker_ids=[completed_worker, replacement_worker],new_findings_count=0,@@ -536,15 +566,7 @@ def test_paused_discovery_resumes_after_restart_with_original_handoff_claim()assert reduced["noNewStreak"] == 2manifest.write_text('{"status":"succeeded"}\n')- finished = finish_deep_scan(- state_dir,- scan_id,- "saturated",- str(manifest),- "--coordinator-generation",- "2",- environment=deep_environment(codex_home),- )["deepScan"]+ finished = deep.finish("saturated", manifest, "--coordinator-generation", "2")["deepScan"]assert (finished["status"], finished["phase"], finished["manifestPath"]) == ("succeeded","terminal",@@ -579,43 +601,26 @@ def test_paused_discovery_resumes_after_restart_with_original_handoff_claim(def test_expired_coordinator_recovers_reducer_publication_after_process_death(tmp_path: Path, crash_point: str, copy_publication: bool) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)- claim_deep_scan_coordinator(state_dir, codex_home, scan_id)+ deep = start_empty_target_scan(tmp_path)+ claim_deep_scan_coordinator(deep.state_dir, deep.codex_home, deep.id)accepted = [- dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep.dispatch_discovery_worker(name=f"accepted-before-crash-{index}",coordinator_generation=2,)[0]for index in range(2)]reducer_id = str(uuid.uuid4())- prompt, artifact_dir, result = worker_paths(scan_dir, "crashed-reducer")- claim_deep_scan_dedup(- state_dir,- scan_id,- reducer_id,- str(prompt),- str(artifact_dir),- *(argument for worker_id in accepted for argument in ("--input-worker-id", worker_id)),- "--coordinator-generation",- "2",- environment=deep_environment(codex_home),- )- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ prompt, artifact_dir, result = worker_paths(deep.directory, "crashed-reducer")+ deep.claim_reducer(reducer_id, prompt, artifact_dir, accepted, coordinator_generation=2)+ deep.upsert_worker(worker_id=reducer_id,kind="dedup",prompt_path=prompt,artifact_dir=artifact_dir,coordinator_generation=2,)- canonical = write_canonical_artifacts(scan_dir)["candidateLedgerPath"]+ canonical = write_canonical_artifacts(deep.directory)["candidateLedgerPath"]canonical.write_text('{"candidate":"previously committed"}\n')if crash_point == "before_commit_without_baseline":canonical.unlink()@@ -648,7 +653,7 @@ def test_expired_coordinator_recovers_reducer_publication_after_process_death(str(wrapper),"commit-deep-scan-dedup","--scan-id",- scan_id,+ deep.id,"--worker-id",reducer_id,"--result-manifest-path",@@ -662,16 +667,16 @@ def test_expired_coordinator_recovers_reducer_publication_after_process_death(],env={**os.environ,- "CODEX_SECURITY_STATE_DIR": str(state_dir),- **deep_environment(codex_home),+ "CODEX_SECURITY_STATE_DIR": str(deep.state_dir),+ **deep_environment(deep.codex_home),},capture_output=True,text=True,)assert crashed.returncode == -signal.SIGKILL, crashed.stderr- expire_deep_scan_coordinator(state_dir, scan_id)- recovered = claim_deep_scan_coordinator(state_dir, codex_home, scan_id)["deepScan"]+ expire_deep_scan_coordinator(deep.state_dir, deep.id)+ recovered = claim_deep_scan_coordinator(deep.state_dir, deep.codex_home, deep.id)["deepScan"]reducer = next(worker for worker in recovered["workers"] if worker["id"] == reducer_id)if crash_point == "after_commit":assert reducer["status"] == "succeeded"@@ -693,49 +698,28 @@ def test_expired_coordinator_recovers_reducer_publication_after_process_death(def test_expired_generation_preserves_receipts_and_recovers_abandoned_workers(tmp_path: Path, reducer_status: str, replace_all_inputs: bool, should_finish: bool) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(+ deep = start_empty_target_scan(tmp_path,config="[deep_scan]\nworkers = 3\nsubagents = 0\nstop_after_no_new = 1\nmax_discovery_runs = 4\n",)- claim_deep_scan_coordinator(state_dir, codex_home, scan_id)+ claim_deep_scan_coordinator(deep.state_dir, deep.codex_home, deep.id)accepted = [- dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep.dispatch_discovery_worker(name=f"accepted-{index}",coordinator_generation=2,)[0]for index in range(3)]- active = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ active = deep.dispatch_discovery_worker(name="abandoned-discovery",succeed=False,coordinator_generation=2,)[0]reducer = str(uuid.uuid4())- prompt, artifacts, _ = worker_paths(scan_dir, "abandoned-reducer")- claim_deep_scan_dedup(- state_dir,- scan_id,- reducer,- str(prompt),- str(artifacts),- *(item for worker in accepted for item in ("--input-worker-id", worker)),- "--coordinator-generation",- "2",- environment=deep_environment(codex_home),- )+ prompt, artifacts, _ = worker_paths(deep.directory, "abandoned-reducer")+ deep.claim_reducer(reducer, prompt, artifacts, accepted, coordinator_generation=2)if reducer_status == "failed":- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=reducer,kind="dedup",status="failed",@@ -744,8 +728,8 @@ def test_expired_generation_preserves_receipts_and_recovers_abandoned_workers(error="fixture reducer failure",coordinator_generation=2,)- expire_deep_scan_coordinator(state_dir, scan_id)- recovered = claim_deep_scan_coordinator(state_dir, codex_home, scan_id)["deepScan"]+ expire_deep_scan_coordinator(deep.state_dir, deep.id)+ recovered = claim_deep_scan_coordinator(deep.state_dir, deep.codex_home, deep.id)["deepScan"]by_id = {worker["id"]: worker for worker in recovered["workers"]}assert (recovered["status"], recovered["dispatchedCount"]) == ("running", 3)@@ -754,24 +738,18 @@ def test_expired_generation_preserves_receipts_and_recovers_abandoned_workers(assert by_id[active]["status"] == "canceled"assert by_id[reducer]["status"] == ("canceled" if reducer_status == "running" else "failed")replacement_inputs = accepted if replace_all_inputs else accepted[:-1]- resumed = commit_reducer(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ resumed = deep.commit_reducer(name="recovered-reducer",input_worker_ids=replacement_inputs,new_findings_count=0,coordinator_generation=3,)assert resumed["status"] == "running"- manifest = scan_dir / "coordinator-manifest.json"+ manifest = deep.directory / "coordinator-manifest.json"manifest.write_text("{}\n")- result = finish_deep_scan(- state_dir,- scan_id,+ result = deep.finish("saturated",- str(manifest),+ manifest,"--coordinator-generation","3",*(@@ -779,7 +757,6 @@ def test_expired_generation_preserves_receipts_and_recovers_abandoned_workers(for worker in accepted[len(replacement_inputs) :]for item in ("--omitted-worker-id", worker)),- environment=deep_environment(codex_home),check=should_finish,)if not should_finish:@@ -793,31 +770,20 @@ def test_expired_generation_preserves_receipts_and_recovers_abandoned_workers(def test_expired_generation_refunds_shutdown_canceled_discovery(tmp_path: Path, legacy: bool) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)+ deep = start_empty_target_scan(tmp_path)coordinator_generation = None if legacy else 2if not legacy:- claim_deep_scan_coordinator(state_dir, codex_home, scan_id)- dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ claim_deep_scan_coordinator(deep.state_dir, deep.codex_home, deep.id)+ deep.dispatch_discovery_worker(name="accepted-before-shutdown",coordinator_generation=coordinator_generation,)- worker_id, prompt, artifacts, _ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ worker_id, prompt, artifacts, _ = deep.dispatch_discovery_worker(name="canceled-by-shutdown",succeed=False,coordinator_generation=coordinator_generation,)- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=worker_id,status="canceled",prompt_path=prompt,@@ -825,36 +791,34 @@ def test_expired_generation_refunds_shutdown_canceled_discovery(error=None if legacy else "coordinator_shutdown: mcp_transport_closed",coordinator_generation=coordinator_generation,)- expire_deep_scan_coordinator(state_dir, scan_id)+ expire_deep_scan_coordinator(deep.state_dir, deep.id)- recovered = claim_deep_scan_coordinator(state_dir, codex_home, scan_id)["deepScan"]+ recovered = claim_deep_scan_coordinator(deep.state_dir, deep.codex_home, deep.id)["deepScan"]assert recovered["dispatchedCount"] == 1assert next(worker for worker in recovered["workers"] if worker["id"] == worker_id)["error"].startswith("coordinator_shutdown_recovered:")- expire_deep_scan_coordinator(state_dir, scan_id)+ expire_deep_scan_coordinator(deep.state_dir, deep.id)assert (- claim_deep_scan_coordinator(state_dir, codex_home, scan_id)["deepScan"]["dispatchedCount"]+ claim_deep_scan_coordinator(deep.state_dir, deep.codex_home, deep.id)["deepScan"][+ "dispatchedCount"+ ]== 1)@pytest.mark.cross_platformdef test_expired_generation_cannot_mutate_recovered_scan(tmp_path: Path) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)- claim_deep_scan_coordinator(state_dir, codex_home, scan_id)- worker, prompt, artifacts, result = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep = start_empty_target_scan(tmp_path)+ claim_deep_scan_coordinator(deep.state_dir, deep.codex_home, deep.id)+ worker, prompt, artifacts, result = deep.dispatch_discovery_worker(name="stale-discovery",succeed=False,coordinator_generation=2,)result.write_text("{}\n")- expire_deep_scan_coordinator(state_dir, scan_id)- recovered = claim_deep_scan_coordinator(state_dir, codex_home, scan_id)["deepScan"]+ expire_deep_scan_coordinator(deep.state_dir, deep.id)+ recovered = claim_deep_scan_coordinator(deep.state_dir, deep.codex_home, deep.id)["deepScan"]assert (recovered["status"], recovered["coordinatorGeneration"]) == ("running", 3)operations = ((@@ -904,21 +868,10 @@ def test_expired_generation_cannot_mutate_recovered_scan(tmp_path: Path) -> None("claim-deep-scan-coordinator", "--thread-id", "thread-deep-scan"),)for command, *arguments in operations:- rejected = scan_command(- state_dir,- command,- scan_id,- *arguments,- "--coordinator-generation",- "2",- check=False,- environment=deep_environment(codex_home),- )+ rejected = deep.command(command, *arguments, "--coordinator-generation", "2", check=False)assert rejected["returncode"] != 0, commandassert "coordinator" in str(rejected["stderr"]).lower(), command- persisted = get_deep_scan(- state_dir, scan_id, "thread-deep-scan", environment=deep_environment(codex_home)- )["deepScan"]+ persisted = deep.read("thread-deep-scan")assert persisted == recovered, command@@ -950,40 +903,25 @@ def test_expired_generation_cannot_mutate_recovered_scan(tmp_path: Path) -> Nonedef test_reducer_commit_requires_safe_standard_canonical_artifacts(tmp_path: Path, artifact_name: str, failure_kind: str, expected_error: str | None) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)+ deep = start_empty_target_scan(tmp_path)input_worker_ids = [- dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep.dispatch_discovery_worker(name=f"canonical-input-{index}",)[0]for index in range(2)]reducer_id = str(uuid.uuid4())- prompt_path, artifact_dir, result_path = worker_paths(scan_dir, "canonical-reducer")- claim_deep_scan_dedup(- state_dir,- scan_id,- reducer_id,- str(prompt_path),- str(artifact_dir),- *(item for input_id in input_worker_ids for item in ("--input-worker-id", input_id)),- environment=deep_environment(codex_home),- )- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ prompt_path, artifact_dir, result_path = worker_paths(deep.directory, "canonical-reducer")+ deep.claim_reducer(reducer_id, prompt_path, artifact_dir, input_worker_ids)+ deep.upsert_worker(worker_id=reducer_id,kind="dedup",prompt_path=prompt_path,artifact_dir=artifact_dir,)- canonical = write_canonical_artifacts(scan_dir)+ canonical = write_canonical_artifacts(deep.directory)artifact = canonical[artifact_name]artifact.unlink()if failure_kind == "symlink":@@ -996,7 +934,7 @@ def test_reducer_commit_requires_safe_standard_canonical_artifacts(commit_args = ("commit-deep-scan-dedup","--scan-id",- scan_id,+ deep.id,"--worker-id",reducer_id,"--result-manifest-path",@@ -1008,22 +946,22 @@ def test_reducer_commit_requires_safe_standard_canonical_artifacts()if expected_error is None:committed = run_workbench(- state_dir,+ deep.state_dir,*commit_args,- environment=deep_environment(codex_home),+ environment=deep_environment(deep.codex_home),)["deepScan"]assert committed["canonicalArtifacts"] is Noneassert artifact.read_text() == staged_ledger.read_text()returnrejected = run_workbench(- state_dir,+ deep.state_dir,*commit_args,- environment=deep_environment(codex_home),+ environment=deep_environment(deep.codex_home),check=False,)assert expected_error in str(rejected["stderr"])- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:assert connection.execute("SELECT status FROM deep_scan_workers WHERE id = ?", (reducer_id,)).fetchone() == ("running",)@@ -1033,7 +971,7 @@ def test_reducer_commit_requires_safe_standard_canonical_artifacts(WHERE scan_id = ? AND kind = 'discovery'ORDER BY completion_sequence""",- (scan_id,),+ (deep.id,),).fetchall() == [("merging",), ("merging",)]assert (connection.execute(@@ -1044,7 +982,7 @@ def test_reducer_commit_requires_safe_standard_canonical_artifacts(FROM deep_scan_runsWHERE scan_id = ?""",- (scan_id,),+ (deep.id,),).fetchone()== (None,) * 9)@@ -1053,9 +991,9 @@ def test_reducer_commit_requires_safe_standard_canonical_artifacts(artifact.unlink()artifact.write_text("")committed = run_workbench(- state_dir,+ deep.state_dir,*commit_args,- environment=deep_environment(codex_home),+ environment=deep_environment(deep.codex_home),)["deepScan"]assert committed["canonicalArtifacts"] is None@@ -1063,10 +1001,10 @@ def test_reducer_commit_requires_safe_standard_canonical_artifacts(def test_scan_progress_projects_active_and_completed_independent_reviews(tmp_path: Path,) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)+ deep = start_empty_target_scan(tmp_path)def independent_reviews() -> dict[str, int | bool]:- context = get_scan(state_dir, scan_id)+ context = get_scan(deep.state_dir, deep.id)return context["scan"]["progress"]["independentReviews"]assert independent_reviews() == {@@ -1076,22 +1014,16 @@ def test_scan_progress_projects_active_and_completed_independent_reviews("consolidating": False,}- first_prompt, first_artifacts, first_result = worker_paths(scan_dir, "discovery-1")- second_prompt, second_artifacts, _ = worker_paths(scan_dir, "discovery-2")+ first_prompt, first_artifacts, first_result = worker_paths(deep.directory, "discovery-1")+ second_prompt, second_artifacts, _ = worker_paths(deep.directory, "discovery-2")first_worker_id = str(uuid.uuid4())second_worker_id = str(uuid.uuid4())- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=first_worker_id,prompt_path=first_prompt,artifact_dir=first_artifacts,)- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=second_worker_id,prompt_path=second_prompt,artifact_dir=second_artifacts,@@ -1104,10 +1036,7 @@ def test_scan_progress_projects_active_and_completed_independent_reviews(}first_result.write_text("{}\n")- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=first_worker_id,status="succeeded",prompt_path=first_prompt,@@ -1121,10 +1050,7 @@ def test_scan_progress_projects_active_and_completed_independent_reviews("consolidating": False,}- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=second_worker_id,status="canceled",prompt_path=second_prompt,@@ -1141,27 +1067,19 @@ def test_scan_progress_projects_active_and_completed_independent_reviews(def test_reducer_claim_updates_review_pass_once_and_projects_consolidation(tmp_path: Path,) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)- first_worker_id, _, _, _ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep = start_empty_target_scan(tmp_path)+ first_worker_id, _, _, _ = deep.dispatch_discovery_worker(name="discovery-1",)- second_worker_id, _, _, _ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ second_worker_id, _, _, _ = deep.dispatch_discovery_worker(name="discovery-2",)reducer_id = str(uuid.uuid4())- prompt_path, artifact_dir, _ = worker_paths(scan_dir, "dedup-1")+ prompt_path, artifact_dir, _ = worker_paths(deep.directory, "dedup-1")claim_args = ("claim-deep-scan-dedup","--scan-id",- scan_id,+ deep.id,"--worker-id",reducer_id,"--prompt-path",@@ -1175,8 +1093,8 @@ def test_reducer_claim_updates_review_pass_once_and_projects_consolidation()for _ in range(2):- run_workbench(state_dir, *claim_args, environment=deep_environment(codex_home))- progress = get_scan(state_dir, scan_id)["scan"]["progress"]+ run_workbench(deep.state_dir, *claim_args, environment=deep_environment(deep.codex_home))+ progress = get_scan(deep.state_dir, deep.id)["scan"]["progress"]assert progress["reviewPass"] == 1assert progress["independentReviews"] == {"active": 0,@@ -1551,23 +1469,17 @@ def test_deep_scan_snapshots_configured_discovery_time_limit(def test_replaceable_discovery_failures_count_once_and_reset_on_success(tmp_path: Path,) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)+ deep = start_empty_target_scan(tmp_path)failed_id = str(uuid.uuid4())- prompt, artifact_dir, _ = worker_paths(scan_dir, "refused-discovery")- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ prompt, artifact_dir, _ = worker_paths(deep.directory, "refused-discovery")+ deep.upsert_worker(worker_id=failed_id,prompt_path=prompt,artifact_dir=artifact_dir,)for _ in range(2):- result = upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ result = deep.upsert_worker(worker_id=failed_id,status="canceled",prompt_path=prompt,@@ -1581,16 +1493,10 @@ def test_replaceable_discovery_failures_count_once_and_reset_on_success(assert failed_worker["status"] == "canceled"assert "policy_refusal" in str(failed_worker["error"])- successful_id, _, _, _ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ successful_id, _, _, _ = deep.dispatch_discovery_worker(name="successful-replacement",)- recovered = get_deep_scan(- state_dir, scan_id, "thread-deep-scan", environment=deep_environment(codex_home)- )["deepScan"]+ recovered = deep.read("thread-deep-scan")assert recovered["consecutiveErrors"] == 0assert recovered["dispatchedCount"] == 2replacement = next(worker for worker in recovered["workers"] if worker["id"] == successful_id)@@ -1603,74 +1509,51 @@ def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacementtmp_path: Path,deadline: bool,) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(+ deep = start_empty_target_scan(tmp_path,config="[deep_scan]\nworkers = 3\nsubagents = 0\nstop_after_no_new = 6\nmax_discovery_runs = 6\n"+ ("max_time_hours = 0.5\n" if deadline else ""),)- initial = claim_deep_scan_coordinator(state_dir, codex_home, scan_id)["deepScan"]+ initial = claim_deep_scan_coordinator(deep.state_dir, deep.codex_home, deep.id)["deepScan"]assert initial["config"]["maxDiscoveryRuns"] == 6if deadline:assert initial["config"]["maxTimeHours"] == 0.5previously_merged = [- dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep.dispatch_discovery_worker(name=f"previously-merged-{index}",coordinator_generation=2,)[0]for index in range(2)]- commit_reducer(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep.commit_reducer(name="previous-reducer",input_worker_ids=previously_merged,new_findings_count=1,coordinator_generation=2,)- canonical_ledger = scan_dir / "artifacts" / "02_discovery" / "candidate_ledger.jsonl"+ canonical_ledger = deep.directory / "artifacts" / "02_discovery" / "candidate_ledger.jsonl"canonical_ledger.write_text('{"candidate":"previously committed"}\n')claimed_inputs = [- dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep.dispatch_discovery_worker(name=f"claimed-{index}",coordinator_generation=2,)[0]for index in range(2)]- unclaimed_input = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ unclaimed_input = deep.dispatch_discovery_worker(name="unclaimed-discovery",coordinator_generation=2,)[0]expected_errors = 0 if deadline else 1if not deadline:- discovery_failure, discovery_prompt, discovery_dir, _ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ discovery_failure, discovery_prompt, discovery_dir, _ = deep.dispatch_discovery_worker(name="failed-discovery",succeed=False,coordinator_generation=2,)- failure = upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ failure = deep.upsert_worker(worker_id=discovery_failure,status="canceled",prompt_path=discovery_prompt,@@ -1682,22 +1565,11 @@ def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacementassert failure["consecutiveErrors"] == 1failed_reducer = str(uuid.uuid4())- failed_prompt, failed_dir, _ = worker_paths(scan_dir, "failed-reducer")- claim_deep_scan_dedup(- state_dir,- scan_id,- failed_reducer,- str(failed_prompt),- str(failed_dir),- *(item for worker in claimed_inputs for item in ("--input-worker-id", worker)),- "--coordinator-generation",- "2",- environment=deep_environment(codex_home),+ failed_prompt, failed_dir, _ = worker_paths(deep.directory, "failed-reducer")+ deep.claim_reducer(+ failed_reducer, failed_prompt, failed_dir, claimed_inputs, coordinator_generation=2)- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=failed_reducer,kind="dedup",prompt_path=failed_prompt,@@ -1705,10 +1577,7 @@ def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacementcoordinator_generation=2,)- failed = upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ failed = deep.upsert_worker(worker_id=failed_reducer,kind="dedup",status="failed",@@ -1735,26 +1604,19 @@ def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacementreplacement_reducer = str(uuid.uuid4())replacement_prompt, replacement_dir, replacement_result = worker_paths(- scan_dir, "replacement-reducer"+ deep.directory, "replacement-reducer")replacement_inputs = [*claimed_inputs, unclaimed_input]- claimed = claim_deep_scan_dedup(- state_dir,- scan_id,+ claimed = deep.claim_reducer(replacement_reducer,- str(replacement_prompt),- str(replacement_dir),- *(item for worker in replacement_inputs for item in ("--input-worker-id", worker)),- "--coordinator-generation",- "2",- environment=deep_environment(codex_home),+ replacement_prompt,+ replacement_dir,+ replacement_inputs,+ coordinator_generation=2,)["deepScan"]assert claimed["phase"] == "reducing"- replayed = upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ replayed = deep.upsert_worker(worker_id=failed_reducer,kind="dedup",status="failed",@@ -1768,10 +1630,7 @@ def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacementassert replayed["consecutiveErrors"] == expected_errorsassert all(replayed_workers[worker]["mergeState"] == "merging" for worker in replacement_inputs)- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=replacement_reducer,kind="dedup",prompt_path=replacement_prompt,@@ -1779,15 +1638,8 @@ def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacementcoordinator_generation=2,)replacement_result.write_text("{}\n")- committed = commit_deep_dedup(- state_dir,- scan_id,- replacement_reducer,- str(replacement_result),- "0",- "--coordinator-generation",- "2",- environment=deep_environment(codex_home),+ committed = deep.commit_result(+ replacement_reducer, replacement_result, "0", "--coordinator-generation", "2")["deepScan"]committed_workers = {worker["id"]: worker for worker in committed["workers"]}assert committed["consecutiveErrors"] == expected_errors@@ -1798,7 +1650,7 @@ def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacementassert committed_workers[failed_reducer]["status"] == "failed"assert canonical_ledger.read_text() == '{"candidate":"previously committed"}\n'- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:receipts = connection.execute("SELECT discovery_worker_id FROM deep_scan_dedup_inputs ""WHERE dedup_worker_id = ? ORDER BY input_order",@@ -1806,30 +1658,21 @@ def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacement).fetchall()assert [worker for (worker,) in receipts] == claimed_inputs- manifest = scan_dir / "coordinator-manifest.json"+ manifest = deep.directory / "coordinator-manifest.json"manifest.write_text("{}\n")def finish(*, check: bool = True) -> dict[str, object]:- return finish_deep_scan(- state_dir,- scan_id,- "capped",- str(manifest),- "--coordinator-generation",- "2",- environment=deep_environment(codex_home),- check=check,- )+ return deep.finish("capped", manifest, "--coordinator-generation", "2", check=check)if deadline:premature = finish(check=False)assert premature["returncode"] != 0assert "before reaching its configured maximum" in premature["stderr"]assert canonical_ledger.read_text() == '{"candidate":"previously committed"}\n'- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:connection.execute("UPDATE deep_scan_runs SET created_at = ? WHERE scan_id = ?",- ("2000-01-01T00:00:00+00:00", scan_id),+ ("2000-01-01T00:00:00+00:00", deep.id),)finished = finish()["deepScan"]assert finished["terminalReason"] == "capped"@@ -1841,22 +1684,16 @@ def test_failed_reducer_rebuffers_claimed_inputs_for_same_generation_replacementdef test_ordinary_discovery_cancellation_does_not_increment_failure_streak(tmp_path: Path,) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)+ deep = start_empty_target_scan(tmp_path)worker_id = str(uuid.uuid4())- prompt, artifact_dir, _ = worker_paths(scan_dir, "user-canceled-discovery")- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ prompt, artifact_dir, _ = worker_paths(deep.directory, "user-canceled-discovery")+ deep.upsert_worker(worker_id=worker_id,prompt_path=prompt,artifact_dir=artifact_dir,)- result = upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ result = deep.upsert_worker(worker_id=worker_id,status="canceled",prompt_path=prompt,@@ -1866,18 +1703,10 @@ def test_ordinary_discovery_cancellation_does_not_increment_failure_streak(assert result["consecutiveErrors"] == 0[email protected]("invalid_threshold", ("0", "-1", "true", '"2"'))-def test_invalid_discovery_error_threshold_fails_before_scan_creation(- tmp_path: Path, invalid_threshold: str-) -> None:- state_dir = tmp_path / "state"+def assert_rejected_config(tmp_path: Path, setting: str, value: str, requirement: str) -> None:codex_home = tmp_path / "codex-home"- write_deep_config(- codex_home, f"[deep_scan]\nstop_after_consecutive_errors = {invalid_threshold}\n"- )- target = tmp_path / "target"- target.mkdir()-+ write_deep_config(codex_home, f"[deep_scan]\n{setting} = {value}\n")+ state_dir, target = empty_target_paths(tmp_path)failed = begin_deep_scan(state_dir,"thread-deep-scan",@@ -1888,42 +1717,29 @@ def test_invalid_discovery_error_threshold_fails_before_scan_creation(environment=deep_environment(codex_home),check=False,)-- assert "deep_scan.stop_after_consecutive_errors must be a positive integer" in str(- failed["stderr"]- )+ assert f"deep_scan.{setting} must be {requirement}" in str(failed["stderr"])with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:assert connection.execute("SELECT COUNT(*) FROM scans").fetchone() == (0,)[email protected]("invalid_threshold", ("0", "-1", "true", '"2"'))+def test_invalid_discovery_error_threshold_fails_before_scan_creation(+ tmp_path: Path, invalid_threshold: str+) -> None:+ assert_rejected_config(+ tmp_path, "stop_after_consecutive_errors", invalid_threshold, "a positive integer"+ )++@pytest.mark.parametrize("invalid_hours", ("0", "-0.5", "true", '"2"', "nan", "inf", "96.5", "9" * 400))def test_invalid_discovery_time_limit_fails_before_scan_creation(tmp_path: Path, invalid_hours: str) -> None:- state_dir = tmp_path / "state"- codex_home = tmp_path / "codex-home"- write_deep_config(codex_home, f"[deep_scan]\nmax_time_hours = {invalid_hours}\n")- target = tmp_path / "target"- target.mkdir()-- failed = begin_deep_scan(- state_dir,- "thread-deep-scan",- "--target-path",- str(target),- "--available-parallelism",- "8",- environment=deep_environment(codex_home),- check=False,- )-- assert "deep_scan.max_time_hours must be a positive finite number no greater than 96" in str(- failed["stderr"]+ assert_rejected_config(+ tmp_path, "max_time_hours", invalid_hours, "a positive finite number no greater than 96")- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:- assert connection.execute("SELECT COUNT(*) FROM scans").fetchone() == (0,)@pytest.mark.parametrize("user_context", (None, "Review authentication only."))@@ -2165,13 +1981,12 @@ def test_target_continuation_does_not_reuse_another_threads_app_scan()started = start_delivered_scan(state_dir,- "--workspace-id",workspace_id,"--scan-root",str(scan_root),environment=deep_environment(codex_home),)- app_scan_id = str(started["results"]["scanId"])+ app_scan_id = str(started["scanId"])begun = begin_deep_scan(state_dir, "app-thread", "--scan-id", app_scan_id, environment=deep_environment(codex_home))@@ -2362,20 +2177,15 @@ def test_app_scan_begin_validates_mode_and_owner(tmp_path: Path) -> None:scan_dir = Path(str(begun["deepScan"]["scanDir"]))prompt, artifact_dir, _ = worker_paths(scan_dir, "setup-worker")worker_id = str(uuid.uuid4())- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep = DeepScan(state_dir, codex_home, scan_id, scan_dir)+ deep.upsert_worker(worker_id=worker_id,kind="setup",prompt_path=prompt,artifact_dir=artifact_dir,attempt=None,)- setup_complete = upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ setup_complete = deep.upsert_worker(worker_id=worker_id,kind="setup",status="succeeded",@@ -2530,32 +2340,17 @@ def test_maximum_one_discovery_run_allows_hard_cap_singleton_reducer(assert deep_scan["config"]["maxDiscoveryRuns"] == 1scan_id = str(deep_scan["scanId"])scan_dir = Path(str(deep_scan["scanDir"]))- worker_id, prompt, artifact_dir, result = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep = DeepScan(state_dir, codex_home, scan_id, scan_dir)+ worker_id, prompt, artifact_dir, result = deep.dispatch_discovery_worker(name="singleton-worker",)reducer_id = str(uuid.uuid4())reducer_prompt, reducer_dir, reducer_result = worker_paths(scan_dir, "singleton-reducer")- claimed = claim_deep_scan_dedup(- state_dir,- scan_id,- reducer_id,- str(reducer_prompt),- str(reducer_dir),- "--input-worker-id",- worker_id,- environment=deep_environment(codex_home),- )["deepScan"]+ claimed = deep.claim_reducer(reducer_id, reducer_prompt, reducer_dir, [worker_id])["deepScan"]assert claimed["phase"] == "reducing"persisted_worker = next(worker for worker in claimed["workers"] if worker["id"] == worker_id)assert persisted_worker["mergeState"] == "merging"- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=reducer_id,kind="dedup",prompt_path=reducer_prompt,@@ -2563,41 +2358,21 @@ def test_maximum_one_discovery_run_allows_hard_cap_singleton_reducer()write_canonical_artifacts(scan_dir)reducer_result.write_text("{}\n")- committed = commit_deep_dedup(- state_dir,- scan_id,- reducer_id,- str(reducer_result),- "1",- environment=deep_environment(codex_home),- )["deepScan"]+ committed = deep.commit_result(reducer_id, reducer_result, "1")["deepScan"]assert committed["status"] == "running"manifest = scan_dir / "coordinator-manifest.json"manifest.write_text("{}\n")- below_threshold = finish_deep_scan(- state_dir,- scan_id,- "saturated",- str(manifest),- environment=deep_environment(codex_home),- check=False,- )+ below_threshold = deep.finish("saturated", manifest, check=False)assert "before reaching its no-new-findings threshold" in str(below_threshold["stderr"])failed_setup_id = str(uuid.uuid4())failed_setup_prompt, failed_setup_dir, _ = worker_paths(scan_dir, "failed-setup")- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=failed_setup_id,kind="setup",prompt_path=failed_setup_prompt,artifact_dir=failed_setup_dir,)- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=failed_setup_id,kind="setup",status="failed",@@ -2605,20 +2380,11 @@ def test_maximum_one_discovery_run_allows_hard_cap_singleton_reducer(artifact_dir=failed_setup_dir,error="Setup failed.",)- rejected = finish_deep_scan(- state_dir,- scan_id,- "capped",- str(manifest),- environment=deep_environment(codex_home),- check=False,- )+ rejected = deep.finish("capped", manifest, check=False)assert "after a worker has failed" in str(rejected["stderr"])with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:connection.execute("DELETE FROM deep_scan_workers WHERE id = ?", (failed_setup_id,))- capped = finish_deep_scan(- state_dir, scan_id, "capped", str(manifest), environment=deep_environment(codex_home)- )["deepScan"]+ capped = deep.finish("capped", manifest)["deepScan"]assert capped["status"] == "succeeded"assert capped["terminalReason"] == "capped"assert capped["manifestPath"] == str(manifest)@@ -2656,11 +2422,8 @@ def test_discovery_deadline_caps_after_reducing_a_single_discovery_result()scan_id = str(begun["deepScan"]["scanId"])scan_dir = Path(str(begun["deepScan"]["scanDir"]))- worker_id, *_ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep = DeepScan(state_dir, codex_home, scan_id, scan_dir)+ worker_id, *_ = deep.dispatch_discovery_worker(name="deadline-discovery",)with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:@@ -2671,37 +2434,18 @@ def test_discovery_deadline_caps_after_reducing_a_single_discovery_result(manifest = scan_dir / "coordinator-manifest.json"manifest.write_text("{}\n")- premature_completion = finish_deep_scan(- state_dir,- scan_id,- "capped",- str(manifest),- environment=deep_environment(codex_home),- check=False,- )+ premature_completion = deep.finish("capped", manifest, check=False)if not deadline_reached:assert "before reaching its configured maximum" in str(premature_completion["stderr"])reducer_prompt, reducer_dir, _ = worker_paths(scan_dir, "premature-reducer")- premature_reducer = claim_deep_scan_dedup(- state_dir,- scan_id,- str(uuid.uuid4()),- str(reducer_prompt),- str(reducer_dir),- "--input-worker-id",- worker_id,- environment=deep_environment(codex_home),- check=False,+ premature_reducer = deep.claim_reducer(+ str(uuid.uuid4()), reducer_prompt, reducer_dir, [worker_id], check=False)assert "requires two buffered discovery results" in str(premature_reducer["stderr"])returnassert "without canonical discovery artifacts" in str(premature_completion["stderr"])- reduced = commit_reducer(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ reduced = deep.commit_reducer(name="deadline-reducer",input_worker_ids=[worker_id],new_findings_count=1,@@ -2712,9 +2456,7 @@ def test_discovery_deadline_caps_after_reducing_a_single_discovery_result(existing_finding = '{"title": "Finding recorded before the deadline"}\n'ledger_path.write_text(existing_finding)- capped = finish_deep_scan(- state_dir, scan_id, "capped", str(manifest), environment=deep_environment(codex_home)- )["deepScan"]+ capped = deep.finish("capped", manifest)["deepScan"]assert capped["status"] == "succeeded"assert capped["terminalReason"] == "capped"assert capped["manifestPath"] == str(manifest)@@ -2747,39 +2489,24 @@ def test_discovery_deadline_caps_without_a_completed_discovery(manifest = scan_dir / "coordinator-manifest.json"manifest.write_text("{}\n")+ deep = DeepScan(state_dir, codex_home, scan_id, scan_dir)if cancel_discovery:- worker_id, prompt, artifact_dir, _ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ worker_id, prompt, artifact_dir, _ = deep.dispatch_discovery_worker(name="deadline-canceled-discovery",succeed=False,)- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=worker_id,status="canceled",prompt_path=prompt,artifact_dir=artifact_dir,)- running = get_deep_scan(- state_dir, scan_id, "thread-deep-scan", environment=deep_environment(codex_home)- )["deepScan"]+ running = deep.read("thread-deep-scan")assert running["status"] == "running"assert running["canonicalArtifacts"] is Noneif configured_hours > 1e-12:- premature = finish_deep_scan(- state_dir,- scan_id,- "capped",- str(manifest),- environment=deep_environment(codex_home),- check=False,- )+ premature = deep.finish("capped", manifest, check=False)assert "before reaching its configured maximum" in str(premature["stderr"])with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:@@ -2795,9 +2522,7 @@ def test_discovery_deadline_caps_without_a_completed_discovery(),)- capped = finish_deep_scan(- state_dir, scan_id, "capped", str(manifest), environment=deep_environment(codex_home)- )["deepScan"]+ capped = deep.finish("capped", manifest)["deepScan"]assert capped["status"] == "succeeded"assert capped["terminalReason"] == "capped"assert capped["completionSequence"] == 0@@ -2809,9 +2534,7 @@ def test_discovery_deadline_caps_without_a_completed_discovery(["canceled"] if cancel_discovery else [])- persisted = get_deep_scan(- state_dir, scan_id, "thread-deep-scan", environment=deep_environment(codex_home)- )["deepScan"]+ persisted = deep.read("thread-deep-scan")assert persisted["canonicalArtifacts"] == capped["canonicalArtifacts"]with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:assert connection.execute(@@ -2825,141 +2548,89 @@ def test_zero_discovery_deadline_rejects_nonempty_candidate_ledger(tmp_path: Path,candidate_ledger: str,) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(+ deep = start_empty_target_scan(tmp_path,config="[deep_scan]\nworkers = 1\nmax_discovery_runs = 3\nmax_time_hours = 0.5\n",)- canonical = write_canonical_artifacts(scan_dir)+ canonical = write_canonical_artifacts(deep.directory)canonical["candidateLedgerPath"].write_text(candidate_ledger)- manifest = scan_dir / "coordinator-manifest.json"+ manifest = deep.directory / "coordinator-manifest.json"manifest.write_text("{}\n")- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:connection.execute("UPDATE deep_scan_runs SET created_at = ? WHERE scan_id = ?",- ((datetime.now(timezone.utc) - timedelta(hours=1)).isoformat(), scan_id),+ ((datetime.now(timezone.utc) - timedelta(hours=1)).isoformat(), deep.id),)- rejected = finish_deep_scan(- state_dir,- scan_id,- "capped",- str(manifest),- environment=deep_environment(codex_home),- check=False,- )+ rejected = deep.finish("capped", manifest, check=False)assert "without a successful dedup worker" in str(rejected["stderr"])assert canonical["candidateLedgerPath"].read_text() == candidate_ledger- running = get_deep_scan(- state_dir, scan_id, "thread-deep-scan", environment=deep_environment(codex_home)- )["deepScan"]+ running = deep.read("thread-deep-scan")assert running["status"] == "running"assert running["canonicalArtifacts"] is Nonedef test_capped_completion_requires_canonical_artifacts(tmp_path: Path) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(+ deep = start_empty_target_scan(tmp_path,config="[deep_scan]\nworkers = 1\nmax_discovery_runs = 1\n",)worker_id = str(uuid.uuid4())- prompt, artifact_dir, _ = worker_paths(scan_dir, "failed-discovery")- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ prompt, artifact_dir, _ = worker_paths(deep.directory, "failed-discovery")+ deep.upsert_worker(worker_id=worker_id,prompt_path=prompt,artifact_dir=artifact_dir,)- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=worker_id,status="failed",prompt_path=prompt,artifact_dir=artifact_dir,error="Retries exhausted.",)- manifest = scan_dir / "coordinator-manifest.json"+ manifest = deep.directory / "coordinator-manifest.json"manifest.write_text("{}\n")- rejected = finish_deep_scan(- state_dir,- scan_id,- "capped",- str(manifest),- environment=deep_environment(codex_home),- check=False,- )+ rejected = deep.finish("capped", manifest, check=False)assert "without canonical discovery artifacts" in str(rejected["stderr"])def test_capped_completion_rejects_buffered_workers_and_omissions(tmp_path: Path) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(+ deep = start_empty_target_scan(tmp_path,config="[deep_scan]\nworkers = 2\nstop_after_no_new = 10\nmax_discovery_runs = 3\n",)- first_id, *_ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ first_id, *_ = deep.dispatch_discovery_worker(name="first",)- second_id, *_ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ second_id, *_ = deep.dispatch_discovery_worker(name="second",)- committed = commit_reducer(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ committed = deep.commit_reducer(name="first-reducer",input_worker_ids=[first_id, second_id],new_findings_count=1,)assert committed["status"] == "running"- buffered_id, *_ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ buffered_id, *_ = deep.dispatch_discovery_worker(name="buffered-at-cap",)- manifest = scan_dir / "coordinator-manifest.json"+ manifest = deep.directory / "coordinator-manifest.json"manifest.write_text("{}\n")- buffered_rejected = finish_deep_scan(- state_dir,- scan_id,- "capped",- str(manifest),- environment=deep_environment(codex_home),- check=False,- )+ buffered_rejected = deep.finish("capped", manifest, check=False)assert "while discovery output remains buffered" in str(buffered_rejected["stderr"])- omitted_rejected = finish_deep_scan(- state_dir,- scan_id,- "capped",- str(manifest),- "--omitted-worker-id",- buffered_id,- environment=deep_environment(codex_home),- check=False,+ omitted_rejected = deep.finish(+ "capped", manifest, "--omitted-worker-id", buffered_id, check=False)assert "cannot declare omitted buffered workers" in str(omitted_rejected["stderr"])def prepare_failure_capped_deep_scan(tmp_path: Path,-) -> tuple[Path, Path, Path, str]:+) -> DeepScan:state_dir = tmp_path / "state"codex_home = tmp_path / "codex-home"write_deep_config(codex_home, "[deep_scan]\nworkers = 2\nmax_discovery_runs = 10\n")@@ -2968,25 +2639,14 @@ def prepare_failure_capped_deep_scan(begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")scan_id = str(begun["deepScan"]["scanId"])scan_dir = Path(str(begun["deepScan"]["scanDir"]))- first_id, *_ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep = DeepScan(state_dir, codex_home, scan_id, scan_dir)+ first_id, *_ = deep.dispatch_discovery_worker(name="first-accepted",)- second_id, *_ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ second_id, *_ = deep.dispatch_discovery_worker(name="second-accepted",)- commit_reducer(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep.commit_reducer(name="committed-aggregate",input_worker_ids=[first_id, second_id],new_findings_count=1,@@ -2998,7 +2658,7 @@ def prepare_failure_capped_deep_scan({"reason": "Deep Scan stopped before completion: reducer retries were exhausted"})coverage_path.write_text(json.dumps(coverage))- return state_dir, codex_home, scan_dir, scan_id+ return deep@pytest.mark.parametrize("worker_kind", ["discovery", "dedup"])@@ -3006,42 +2666,23 @@ def prepare_failure_capped_deep_scan(def test_capped_completion_cannot_use_review_notes_to_hide_worker_failures(tmp_path: Path, worker_kind: str, legacy_succeeded: bool) -> None:- state_dir, codex_home, scan_dir, scan_id = prepare_failure_capped_deep_scan(tmp_path)- worker_id, prompt, artifact_dir, _ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep = prepare_failure_capped_deep_scan(tmp_path)+ worker_id, prompt, artifact_dir, _ = deep.dispatch_discovery_worker(name="after-accepted-aggregate",succeed=worker_kind == "dedup",)if worker_kind == "dedup":discovery_id = worker_idworker_id = str(uuid.uuid4())- prompt, artifact_dir, _ = worker_paths(scan_dir, "unrecovered-reducer")- claim_deep_scan_dedup(- state_dir,- scan_id,- worker_id,- str(prompt),- str(artifact_dir),- "--input-worker-id",- discovery_id,- environment=deep_environment(codex_home),- )- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ prompt, artifact_dir, _ = worker_paths(deep.directory, "unrecovered-reducer")+ deep.claim_reducer(worker_id, prompt, artifact_dir, [discovery_id])+ deep.upsert_worker(worker_id=worker_id,kind="dedup",prompt_path=prompt,artifact_dir=artifact_dir,)- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=worker_id,kind=worker_kind,status="failed",@@ -3049,104 +2690,72 @@ def test_capped_completion_cannot_use_review_notes_to_hide_worker_failures(artifact_dir=artifact_dir,error="Worker retries exhausted.",)- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:connection.execute("UPDATE deep_scan_runs SET max_discovery_runs = discovery_runs_dispatched WHERE scan_id = ?",- (scan_id,),+ (deep.id,),)artifacts = {- name: (scan_dir / name).read_bytes()+ name: (deep.directory / name).read_bytes()for name in ("scan-manifest.json", "findings.json", "coverage.json")}if legacy_succeeded:- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:connection.execute("UPDATE deep_scan_runs SET status = 'succeeded', phase = 'terminal', ""terminal_reason = 'capped', manifest_path = ? WHERE scan_id = ?",- (str(scan_dir / "scan-manifest.json"), scan_id),+ (str(deep.directory / "scan-manifest.json"), deep.id),)- rejected = scan_command(- state_dir,- "complete-scan",- scan_id,- environment=deep_environment(codex_home),- check=False,- )+ rejected = deep.command("complete-scan", check=False)else:- rejected = finish_deep_scan(- state_dir,- scan_id,- "capped",- str(scan_dir / "scan-manifest.json"),- environment=deep_environment(codex_home),- check=False,- )+ rejected = deep.finish("capped", deep.directory / "scan-manifest.json", check=False)assert "cannot finish after a worker has failed" in str(rejected["stderr"])- assert {name: (scan_dir / name).read_bytes() for name in artifacts} == artifacts- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ assert {name: (deep.directory / name).read_bytes() for name in artifacts} == artifacts+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:assert connection.execute("SELECT status FROM deep_scan_runs WHERE scan_id = ?",- (scan_id,),+ (deep.id,),).fetchone() == ("succeeded" if legacy_succeeded else "running",)stopped = fail_scan(- state_dir, scan_id, "Worker retries exhausted.", environment=deep_environment(codex_home)+ deep.state_dir,+ deep.id,+ "Worker retries exhausted.",+ environment=deep_environment(deep.codex_home),)["scan"]assert stopped["progress"]["status"] == "failed"assert stopped["findingCount"] == 1- assert json.loads((scan_dir / "scan-manifest.json").read_text())["scan"]["status"] == "failed"+ assert (+ json.loads((deep.directory / "scan-manifest.json").read_text())["scan"]["status"]+ == "failed"+ )def test_legacy_succeeded_capped_completion_replays_without_rewriting_artifacts(tmp_path: Path,) -> None:- state_dir, codex_home, scan_dir, scan_id = prepare_failure_capped_deep_scan(tmp_path)- buffered_id, *_ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep = prepare_failure_capped_deep_scan(tmp_path)+ buffered_id, *_ = deep.dispatch_discovery_worker(name="legacy-omitted",)- manifest_path = str(scan_dir / "scan-manifest.json")- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ manifest_path = str(deep.directory / "scan-manifest.json")+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:connection.execute("UPDATE deep_scan_runs SET status = 'succeeded', phase = 'terminal', ""terminal_reason = 'capped', manifest_path = ? WHERE scan_id = ?",- (manifest_path, scan_id),+ (manifest_path, deep.id),)artifacts = {- name: (scan_dir / name).read_bytes()+ name: (deep.directory / name).read_bytes()for name in ("scan-manifest.json", "findings.json", "coverage.json")}- replayed = finish_deep_scan(- state_dir,- scan_id,- "capped",- manifest_path,- "--omitted-worker-id",- buffered_id,- environment=deep_environment(codex_home),- )["deepScan"]+ replayed = deep.finish("capped", manifest_path, "--omitted-worker-id", buffered_id)["deepScan"]assert replayed["status"] == "succeeded"- assert {name: (scan_dir / name).read_bytes() for name in artifacts} == artifacts- rejected = finish_deep_scan(- state_dir,- scan_id,- "capped",- manifest_path,- environment=deep_environment(codex_home),- check=False,- )+ assert {name: (deep.directory / name).read_bytes() for name in artifacts} == artifacts+ rejected = deep.finish("capped", manifest_path, check=False)assert "terminal state is immutable" in str(rejected["stderr"])- publication = scan_command(- state_dir,- "complete-scan",- scan_id,- environment=deep_environment(codex_home),- check=False,- )+ publication = deep.command("complete-scan", check=False)assert "while discovery output remains buffered" in str(publication["stderr"])- assert {name: (scan_dir / name).read_bytes() for name in artifacts} == artifacts+ assert {name: (deep.directory / name).read_bytes() for name in artifacts} == artifactsdef test_late_worker_rejection_supersedes_stopped_checkpoint_finding(tmp_path: Path) -> None:@@ -3169,10 +2778,8 @@ def test_late_worker_rejection_supersedes_stopped_checkpoint_finding(tmp_path: Pworker_id = str(uuid.uuid4())prompt_path, artifact_dir, result_path = worker_paths(scan_dir, "late-rejection")- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep = DeepScan(state_dir, codex_home, scan_id, scan_dir)+ deep.upsert_worker(worker_id=worker_id,prompt_path=prompt_path,artifact_dir=artifact_dir,@@ -3210,9 +2817,7 @@ def test_late_worker_rejection_supersedes_stopped_checkpoint_finding(tmp_path: Precovery_needed = get_scan(state_dir, scan_id, environment=deep_environment(codex_home))["scan"]assert recovery_needed["resultsRecoveryNeeded"] is True- recovered = scan_command(- state_dir, "recover-scan-results", scan_id, environment=deep_environment(codex_home)- )["scan"]+ recovered = deep.command("recover-scan-results")["scan"]final_findings = json.loads(findings_path.read_text())["findings"]final_coverage = read_review_state(state_dir, scan_dir)assert recovered["findingCount"] == len(final_findings) == 0@@ -3224,60 +2829,41 @@ def test_late_worker_rejection_supersedes_stopped_checkpoint_finding(tmp_path: Pdef test_capped_completion_does_not_discard_canceled_reducer_inputs(tmp_path: Path) -> None:- state_dir, codex_home, scan_dir, scan_id = prepare_failure_capped_deep_scan(tmp_path)- buffered_id, *_ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep = prepare_failure_capped_deep_scan(tmp_path)+ buffered_id, *_ = deep.dispatch_discovery_worker(name="claimed-before-failure",)reducer_id = str(uuid.uuid4())- reducer_prompt, reducer_dir, _ = worker_paths(scan_dir, "canceled-reducer")- claim_deep_scan_dedup(- state_dir,- scan_id,- reducer_id,- str(reducer_prompt),- str(reducer_dir),- "--input-worker-id",- buffered_id,- environment=deep_environment(codex_home),- )- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ reducer_prompt, reducer_dir, _ = worker_paths(deep.directory, "canceled-reducer")+ deep.claim_reducer(reducer_id, reducer_prompt, reducer_dir, [buffered_id])+ deep.upsert_worker(worker_id=reducer_id,kind="dedup",prompt_path=reducer_prompt,artifact_dir=reducer_dir,)- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:connection.execute("UPDATE deep_scan_runs SET max_discovery_runs = discovery_runs_dispatched WHERE scan_id = ?",- (scan_id,),+ (deep.id,),)finish_args = ("finish-deep-scan","--scan-id",- scan_id,+ deep.id,"--terminal-reason","capped","--manifest-path",- str(scan_dir / "scan-manifest.json"),+ str(deep.directory / "scan-manifest.json"),"--omitted-worker-id",buffered_id,)active_rejected = run_workbench(- state_dir, *finish_args, environment=deep_environment(codex_home), check=False+ deep.state_dir, *finish_args, environment=deep_environment(deep.codex_home), check=False)assert "while workers are active" in str(active_rejected["stderr"])- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=reducer_id,kind="dedup",status="canceled",@@ -3285,32 +2871,26 @@ def test_capped_completion_does_not_discard_canceled_reducer_inputs(tmp_path: Paartifact_dir=reducer_dir,)rejected = run_workbench(- state_dir, *finish_args, environment=deep_environment(codex_home), check=False+ deep.state_dir, *finish_args, environment=deep_environment(deep.codex_home), check=False)assert "while discovery output is merging" in str(rejected["stderr"])- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:assert connection.execute("SELECT status FROM deep_scan_runs WHERE scan_id = ?",- (scan_id,),+ (deep.id,),).fetchone() == ("running",)assert connection.execute("SELECT merge_state FROM deep_scan_workers WHERE id = ?",(buffered_id,),).fetchone() == ("merging",)- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:connection.execute("UPDATE deep_scan_runs SET status = 'succeeded', phase = 'terminal', ""terminal_reason = 'capped', manifest_path = ? WHERE scan_id = ?",- (str(scan_dir / "scan-manifest.json"), scan_id),+ (str(deep.directory / "scan-manifest.json"), deep.id),)- publication = scan_command(- state_dir,- "complete-scan",- scan_id,- environment=deep_environment(codex_home),- check=False,- )+ publication = deep.command("complete-scan", check=False)assert "while discovery output is merging" in str(publication["stderr"])@@ -3325,79 +2905,44 @@ def test_capped_completion_does_not_discard_canceled_reducer_inputs(tmp_path: Padef test_capped_completion_uses_configured_limits_independently_of_review_notes(tmp_path: Path, stop_note: str | None) -> None:- state_dir, codex_home, scan_dir, scan_id = prepare_failure_capped_deep_scan(tmp_path)- coverage_path = scan_dir / "coverage.json"+ deep = prepare_failure_capped_deep_scan(tmp_path)+ coverage_path = deep.directory / "coverage.json"coverage = json.loads(coverage_path.read_text())coverage["deferred"] = [{"reason": stop_note}] if stop_note else []coverage_path.write_text(json.dumps(coverage))- rejected = finish_deep_scan(- state_dir,- scan_id,- "capped",- str(scan_dir / "scan-manifest.json"),- environment=deep_environment(codex_home),- check=False,- )+ rejected = deep.finish("capped", deep.directory / "scan-manifest.json", check=False)assert "cannot finish capped before reaching its configured maximum" in str(rejected["stderr"])- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:assert connection.execute("SELECT status, manifest_path FROM deep_scan_runs WHERE scan_id = ?",- (scan_id,),+ (deep.id,),).fetchone() == ("running", None)def test_saturated_completion_rejects_merging_workers(tmp_path: Path) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(+ deep = start_empty_target_scan(tmp_path,config="[deep_scan]\nworkers = 2\nstop_after_no_new = 2\nmax_discovery_runs = 3\n",)- first_id, *_ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ first_id, *_ = deep.dispatch_discovery_worker(name="first",)- second_id, *_ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ second_id, *_ = deep.dispatch_discovery_worker(name="second",)- committed = commit_reducer(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ committed = deep.commit_reducer(name="first-reducer",input_worker_ids=[first_id, second_id],new_findings_count=0,)assert committed["noNewStreak"] == 2- merging_id, *_ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ merging_id, *_ = deep.dispatch_discovery_worker(name="merging-input",)reducer_id = str(uuid.uuid4())- reducer_prompt, reducer_dir, _ = worker_paths(scan_dir, "abandoned-reducer")- claim_deep_scan_dedup(- state_dir,- scan_id,- reducer_id,- str(reducer_prompt),- str(reducer_dir),- "--input-worker-id",- merging_id,- environment=deep_environment(codex_home),- )- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ reducer_prompt, reducer_dir, _ = worker_paths(deep.directory, "abandoned-reducer")+ deep.claim_reducer(reducer_id, reducer_prompt, reducer_dir, [merging_id])+ deep.upsert_worker(worker_id=reducer_id,kind="dedup",status="canceled",@@ -3405,17 +2950,10 @@ def test_saturated_completion_rejects_merging_workers(tmp_path: Path) -> None:artifact_dir=reducer_dir,attempt=None,)- manifest = scan_dir / "coordinator-manifest.json"+ manifest = deep.directory / "coordinator-manifest.json"manifest.write_text("{}\n")- rejected = finish_deep_scan(- state_dir,- scan_id,- "saturated",- str(manifest),- environment=deep_environment(codex_home),- check=False,- )+ rejected = deep.finish("saturated", manifest, check=False)assert "while discovery output is merging" in str(rejected["stderr"])@@ -3428,15 +2966,15 @@ def test_running_worker_updates_preserve_identity_and_dispatch_count(tmp_path: Pworker_id = str(uuid.uuid4())prompt, artifact_dir, _ = worker_paths(scan_dir, "running-worker")update = {- "scan_id": scan_id,"worker_id": worker_id,"prompt_path": prompt,"artifact_dir": artifact_dir,}- initial = upsert_worker(state_dir, codex_home, **update)["deepScan"]+ deep = DeepScan(state_dir, codex_home, scan_id, scan_dir)+ initial = deep.upsert_worker(**update)["deepScan"]initial_worker = next(worker for worker in initial["workers"] if worker["id"] == worker_id)- repeated = upsert_worker(state_dir, codex_home, **update)["deepScan"]+ repeated = deep.upsert_worker(**update)["deepScan"]repeated_worker = next(worker for worker in repeated["workers"] if worker["id"] == worker_id)assert initial["dispatchedCount"] == repeated["dispatchedCount"] == 1@@ -3444,8 +2982,8 @@ def test_running_worker_updates_preserve_identity_and_dispatch_count(tmp_path: Passert repeated_worker["attempt"] == initial_worker["attempt"] == 1assert repeated_worker["sdkThreadId"] is None- started = upsert_worker(state_dir, codex_home, **update, thread_id="thread-worker")["deepScan"]- replayed = upsert_worker(state_dir, codex_home, **update, thread_id="thread-worker")["deepScan"]+ started = deep.upsert_worker(**update, thread_id="thread-worker")["deepScan"]+ replayed = deep.upsert_worker(**update, thread_id="thread-worker")["deepScan"]started_worker = next(worker for worker in started["workers"] if worker["id"] == worker_id)replayed_worker = next(worker for worker in replayed["workers"] if worker["id"] == worker_id)@@ -3455,22 +2993,16 @@ def test_running_worker_updates_preserve_identity_and_dispatch_count(tmp_path: Pdef test_terminal_worker_updates_are_exact_idempotent_replays(tmp_path: Path) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)+ deep = start_empty_target_scan(tmp_path)worker_id = str(uuid.uuid4())- prompt, artifact_dir, result = worker_paths(scan_dir, "terminal-worker")- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ prompt, artifact_dir, result = worker_paths(deep.directory, "terminal-worker")+ deep.upsert_worker(worker_id=worker_id,prompt_path=prompt,artifact_dir=artifact_dir,)result.write_text("{}\n")- accepted = upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ accepted = deep.upsert_worker(worker_id=worker_id,status="succeeded",prompt_path=prompt,@@ -3478,10 +3010,7 @@ def test_terminal_worker_updates_are_exact_idempotent_replays(tmp_path: Path) ->result_path=result,)["deepScan"]accepted_worker = next(worker for worker in accepted["workers"] if worker["id"] == worker_id)- replayed = upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ replayed = deep.upsert_worker(worker_id=worker_id,status="succeeded",prompt_path=prompt,@@ -3493,35 +3022,22 @@ def test_terminal_worker_updates_are_exact_idempotent_replays(tmp_path: Path) ->replacement_result = artifact_dir / "replacement-result.json"replacement_result.write_text("{}\n")- replacement = upsert_deep_worker(- state_dir,- scan_id,- worker_id,- "discovery",- "succeeded",- str(prompt),- str(artifact_dir),- "--result-manifest-path",- str(replacement_result),- "--attempt",- "1",- environment=deep_environment(codex_home),+ replacement = deep.upsert_worker(+ worker_id=worker_id,+ status="succeeded",+ prompt_path=prompt,+ artifact_dir=artifact_dir,+ result_path=replacement_result,check=False,)assert "terminal state is immutable" in str(replacement["stderr"])- later_attempt = upsert_deep_worker(- state_dir,- scan_id,- worker_id,- "discovery",- "succeeded",- str(prompt),- str(artifact_dir),- "--result-manifest-path",- str(result),- "--attempt",- "2",- environment=deep_environment(codex_home),+ later_attempt = deep.upsert_worker(+ worker_id=worker_id,+ status="succeeded",+ prompt_path=prompt,+ artifact_dir=artifact_dir,+ result_path=result,+ attempt=2,check=False,)assert later_attempt["returncode"] != 0@@ -3531,26 +3047,20 @@ def test_terminal_worker_updates_are_exact_idempotent_replays(tmp_path: Path) ->def test_discovery_buffer_prefix_dedup_and_saturation_are_transactional(tmp_path: Path,) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(+ deep = start_empty_target_scan(tmp_path,config="[deep_scan]\nworkers = 2\nstop_after_no_new = 3\nmax_discovery_runs = 4\n",)for index in range(3):worker_id = str(uuid.uuid4())- prompt, artifact_dir, result = worker_paths(scan_dir, f"worker-{index}")- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ prompt, artifact_dir, result = worker_paths(deep.directory, f"worker-{index}")+ deep.upsert_worker(worker_id=worker_id,prompt_path=prompt,artifact_dir=artifact_dir,)result.write_text("{}\n")- accepted = upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ accepted = deep.upsert_worker(worker_id=worker_id,status="succeeded",prompt_path=prompt,@@ -3565,37 +3075,21 @@ def test_discovery_buffer_prefix_dedup_and_saturation_are_transactional()assert [worker["completionSequence"] for worker in ordered_workers] == [1, 2, 3]reducer_id = str(uuid.uuid4())- reducer_prompt, reducer_dir, reducer_result = worker_paths(scan_dir, "reducer")- claimed = claim_deep_scan_dedup(- state_dir,- scan_id,- reducer_id,- str(reducer_prompt),- str(reducer_dir),- *(item for worker in ordered_workers[:2] for item in ("--input-worker-id", worker["id"])),- environment=deep_environment(codex_home),+ reducer_prompt, reducer_dir, reducer_result = worker_paths(deep.directory, "reducer")+ claimed = deep.claim_reducer(+ reducer_id, reducer_prompt, reducer_dir, [worker["id"] for worker in ordered_workers[:2]])assert claimed["deepScan"]["phase"] == "reducing"- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ deep.upsert_worker(worker_id=reducer_id,kind="dedup",prompt_path=reducer_prompt,artifact_dir=reducer_dir,error="Transient reducer failure.",)- write_canonical_artifacts(scan_dir)+ write_canonical_artifacts(deep.directory)reducer_result.write_text("{}\n")- committed = commit_deep_dedup(- state_dir,- scan_id,- reducer_id,- str(reducer_result),- "0",- environment=deep_environment(codex_home),- )+ committed = deep.commit_result(reducer_id, reducer_result, "0")assert committed["deepScan"]["status"] == "running"assert committed["deepScan"]["terminalReason"] is Noneassert committed["deepScan"]["noNewStreak"] == 2@@ -3612,43 +3106,20 @@ def test_discovery_buffer_prefix_dedup_and_saturation_are_transactional(assert omitted["status"] == "succeeded"assert omitted["mergeState"] == "buffered"second_reducer_id = str(uuid.uuid4())- second_prompt, second_dir, second_result = worker_paths(scan_dir, "second-reducer")- claim_deep_scan_dedup(- state_dir,- scan_id,- second_reducer_id,- str(second_prompt),- str(second_dir),- "--input-worker-id",- str(omitted["id"]),- environment=deep_environment(codex_home),- )- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ second_prompt, second_dir, second_result = worker_paths(deep.directory, "second-reducer")+ deep.claim_reducer(second_reducer_id, second_prompt, second_dir, [str(omitted["id"])])+ deep.upsert_worker(worker_id=second_reducer_id,kind="dedup",prompt_path=second_prompt,artifact_dir=second_dir,)second_result.write_text("{}\n")- late_worker_id, late_prompt, late_dir, late_result = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ late_worker_id, late_prompt, late_dir, late_result = deep.dispatch_discovery_worker(name="late-worker",succeed=False,)- saturated_reduction = commit_deep_dedup(- state_dir,- scan_id,- second_reducer_id,- str(second_result),- "0",- environment=deep_environment(codex_home),- )+ saturated_reduction = deep.commit_result(second_reducer_id, second_result, "0")assert saturated_reduction["deepScan"]["status"] == "running"assert saturated_reduction["deepScan"]["phase"] == "discovery"assert saturated_reduction["deepScan"]["terminalReason"] is None@@ -3660,14 +3131,11 @@ def test_discovery_buffer_prefix_dedup_and_saturation_are_transactional(if worker["id"] == late_worker_id)assert late_worker["status"] == "running"- manifest = scan_dir / "artifacts" / "deep_discovery" / "coordinator-manifest.json"+ manifest = deep.directory / "artifacts" / "deep_discovery" / "coordinator-manifest.json"manifest.write_text("{}\n")late_result.write_text("{}\n")- accepted_late = upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ accepted_late = deep.upsert_worker(worker_id=late_worker_id,status="succeeded",prompt_path=late_prompt,@@ -3679,76 +3147,38 @@ def test_discovery_buffer_prefix_dedup_and_saturation_are_transactional()assert accepted_late_worker["mergeState"] == "buffered"- omitted_rejected = finish_deep_scan(- state_dir,- scan_id,- "saturated",- str(manifest),- environment=deep_environment(codex_home),- check=False,- )+ omitted_rejected = deep.finish("saturated", manifest, check=False)assert "exactly identify all buffered discovery workers" in str(omitted_rejected["stderr"])- finished = finish_deep_scan(- state_dir,- scan_id,- "saturated",- str(manifest),- "--omitted-worker-id",- late_worker_id,- environment=deep_environment(codex_home),- )+ finished = deep.finish("saturated", manifest, "--omitted-worker-id", late_worker_id)assert finished["deepScan"]["status"] == "succeeded"assert finished["deepScan"]["terminalReason"] == "saturated"assert finished["deepScan"]["manifestPath"] == str(manifest)- replayed = finish_deep_scan(- state_dir,- scan_id,- "saturated",- str(manifest),- "--omitted-worker-id",- late_worker_id,- environment=deep_environment(codex_home),- )+ replayed = deep.finish("saturated", manifest, "--omitted-worker-id", late_worker_id)assert replayed == finished- mismatched_replay = finish_deep_scan(- state_dir,- scan_id,- "saturated",- str(manifest),- environment=deep_environment(codex_home),- check=False,- )+ mismatched_replay = deep.finish("saturated", manifest, check=False)assert "terminal state is immutable" in str(mismatched_replay["stderr"])def test_get_deep_scan_does_not_invent_compact_artifacts_for_legacy_success(tmp_path: Path,) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)- committed = commit_reducer(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep = start_empty_target_scan(tmp_path)+ committed = deep.commit_reducer(name="legacy-reducer",input_worker_ids=[- dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep.dispatch_discovery_worker(name=f"legacy-discovery-{index}",)[0]for index in range(2)],new_findings_count=0,)- compact_files = write_canonical_artifacts(scan_dir)+ compact_files = write_canonical_artifacts(deep.directory)assert committed["canonicalArtifacts"] is Nonefor path in compact_files.values():path.unlink()- legacy_dir = scan_dir / "artifacts" / "legacy-discovery"+ legacy_dir = deep.directory / "artifacts" / "legacy-discovery"legacy_dir.mkdir(parents=True)legacy_files = {"canonical_inventory_path": legacy_dir / "canonical_inventory.md",@@ -3770,10 +3200,10 @@ def test_get_deep_scan_does_not_invent_compact_artifacts_for_legacy_success(manifest.write_text('{"status":"succeeded"}\n')legacy_columns = ", ".join(legacy_files)legacy_updates = ", ".join(f"{name} = ?" for name in legacy_files)- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:assert connection.execute(f"SELECT {legacy_columns} FROM deep_scan_runs WHERE scan_id = ?",- (scan_id,),+ (deep.id,),).fetchone() == (None,) * len(legacy_files)connection.execute(f"""@@ -3782,12 +3212,10 @@ def test_get_deep_scan_does_not_invent_compact_artifacts_for_legacy_success(terminal_reason = 'saturated', manifest_path = ?, completed_at = updated_atWHERE scan_id = ?""",- (*map(str, legacy_files.values()), str(manifest), scan_id),+ (*map(str, legacy_files.values()), str(manifest), deep.id),)- persisted = get_deep_scan(- state_dir, scan_id, "thread-deep-scan", environment=deep_environment(codex_home)- )["deepScan"]+ persisted = deep.read("thread-deep-scan")assert persisted["status"] == "succeeded"assert persisted["manifestPath"] == str(manifest)assert persisted["canonicalArtifacts"] is None@@ -3798,8 +3226,8 @@ def test_get_deep_scan_does_not_invent_compact_artifacts_for_legacy_success(def test_finish_preserves_legacy_succeeded_without_manifest_compatibility(tmp_path: Path,) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:+ deep = start_empty_target_scan(tmp_path)+ with sqlite3.connect(deep.state_dir / "workbench.sqlite3") as connection:connection.execute("""UPDATE deep_scan_runs@@ -3807,50 +3235,36 @@ def test_finish_preserves_legacy_succeeded_without_manifest_compatibility(completed_at = updated_atWHERE scan_id = ?""",- (scan_id,),+ (deep.id,),)- manifest = scan_dir / "legacy-coordinator-manifest.json"+ manifest = deep.directory / "legacy-coordinator-manifest.json"manifest.write_text("{}\n")- finished = finish_deep_scan(- state_dir, scan_id, "saturated", str(manifest), environment=deep_environment(codex_home)- )+ finished = deep.finish("saturated", manifest)assert finished["deepScan"]["manifestPath"] == str(manifest)- replayed = finish_deep_scan(- state_dir, scan_id, "saturated", str(manifest), environment=deep_environment(codex_home)- )+ replayed = deep.finish("saturated", manifest)assert replayed == finished- replacement = scan_dir / "replacement-manifest.json"+ replacement = deep.directory / "replacement-manifest.json"replacement.write_text("{}\n")- mismatched = finish_deep_scan(- state_dir,- scan_id,- "saturated",- str(replacement),- environment=deep_environment(codex_home),- check=False,- )+ mismatched = deep.finish("saturated", replacement, check=False)assert "terminal state is immutable" in str(mismatched["stderr"])def test_cancel_scan_cancels_coordinator_and_active_workers(tmp_path: Path) -> None:- state_dir, codex_home, scan_id, scan_dir = start_empty_target_scan(tmp_path)+ deep = start_empty_target_scan(tmp_path)worker_id = str(uuid.uuid4())- prompt, artifact_dir, _ = worker_paths(scan_dir, "worker")- upsert_worker(- state_dir,- codex_home,- scan_id=scan_id,+ prompt, artifact_dir, _ = worker_paths(deep.directory, "worker")+ deep.upsert_worker(worker_id=worker_id,prompt_path=prompt,artifact_dir=artifact_dir,)- cancel_scan(state_dir, scan_id, "thread-deep-scan", environment=deep_environment(codex_home))- canceled = get_deep_scan(- state_dir, scan_id, "thread-deep-scan", environment=deep_environment(codex_home)- )["deepScan"]+ cancel_scan(+ deep.state_dir, deep.id, "thread-deep-scan", environment=deep_environment(deep.codex_home)+ )+ canceled = deep.read("thread-deep-scan")assert canceled["status"] == "canceled"assert canceled["phase"] == "terminal"assert canceled["cancelRequested"] is True@@ -3865,40 +3279,27 @@ def test_failure_manifest_is_confined_persisted_and_exactly_replayable(begun = begin_target_scan(state_dir, codex_home, target, tmp_path / "scans")scan_id = str(begun["deepScan"]["scanId"])scan_dir = Path(str(begun["deepScan"]["scanDir"]))- worker_id, *_ = dispatch_discovery_worker(- state_dir,- codex_home,- scan_id=scan_id,- scan_dir=scan_dir,+ deep = DeepScan(state_dir, codex_home, scan_id, scan_dir)+ worker_id, *_ = deep.dispatch_discovery_worker(name="active-worker",succeed=False,)outside_manifest = tmp_path / "outside-failure-manifest.json"outside_manifest.write_text("{}\n")- confined = scan_command(- state_dir,+ confined = deep.command("fail-deep-scan",- scan_id,"--message","Coordinator failed.","--manifest-path",str(outside_manifest),- environment=deep_environment(codex_home),check=False,)assert "inside the scan directory" in str(confined["stderr"])manifest = scan_dir / "failure-manifest.json"manifest.write_text("{}\n")- failed = scan_command(- state_dir,- "fail-deep-scan",- scan_id,- "--message",- "Coordinator failed.",- "--manifest-path",- str(manifest),- environment=deep_environment(codex_home),+ failed = deep.command(+ "fail-deep-scan", "--message", "Coordinator failed.", "--manifest-path", str(manifest))deep_scan = failed["deepScan"]assert deep_scan["status"] == "failed"@@ -3911,15 +3312,8 @@ def test_failure_manifest_is_confined_persisted_and_exactly_replayable(assert parent["progress"]["status"] == "failed"assert parent["failureMessage"] == "Coordinator failed."- replayed = scan_command(- state_dir,- "fail-deep-scan",- scan_id,- "--message",- "Coordinator failed.",- "--manifest-path",- str(manifest),- environment=deep_environment(codex_home),+ replayed = deep.command(+ "fail-deep-scan", "--message", "Coordinator failed.", "--manifest-path", str(manifest))assert replayed == failed@@ -3937,14 +3331,7 @@ def test_failure_manifest_is_confined_persisted_and_exactly_replayable(str(manifest),),):- mismatched = scan_command(- state_dir,- "fail-deep-scan",- scan_id,- *extra_args,- environment=deep_environment(codex_home),- check=False,- )+ mismatched = deep.command("fail-deep-scan", *extra_args, check=False)assert "terminal failure state is immutable" in str(mismatched["stderr"])with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:@@ -3952,15 +3339,12 @@ def test_failure_manifest_is_confined_persisted_and_exactly_replayable("UPDATE scans SET failure_message = 'Diverged parent failure.' WHERE id = ?",(scan_id,),)- incoherent_parent = scan_command(- state_dir,+ incoherent_parent = deep.command("fail-deep-scan",- scan_id,"--message","Coordinator failed.","--manifest-path",str(manifest),- environment=deep_environment(codex_home),check=False,)assert "parent failure must exactly match" in str(incoherent_parent["stderr"])@@ -4101,22 +3485,4 @@ def test_succeeded_run_with_manifest_cannot_be_marked_interrupted(tmp_path: Pathdef test_invalid_user_configuration_fails_before_scan_creation(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- codex_home = tmp_path / "codex-home"- write_deep_config(codex_home, "[deep_scan]\nsubagents = -1\n")- target = tmp_path / "target"- target.mkdir()-- failed = begin_deep_scan(- state_dir,- "thread-deep-scan",- "--target-path",- str(target),- "--available-parallelism",- "8",- environment=deep_environment(codex_home),- check=False,- )- assert "deep_scan.subagents must be a non-negative integer" in str(failed["stderr"])- with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:- assert connection.execute("SELECT COUNT(*) FROM scans").fetchone() == (0,)+ assert_rejected_config(tmp_path, "subagents", "-1", "a non-negative integer")diff --git a/plugins/codex-security/tests/test_workbench_diagnostics.py b/plugins/codex-security/tests/test_workbench_diagnostics.pyindex ca5dd3dd0..33339db0a 100644--- a/plugins/codex-security/tests/test_workbench_diagnostics.py+++ b/plugins/codex-security/tests/test_workbench_diagnostics.py@@ -47,11 +47,10 @@ def test_scan_failure_keeps_complete_diagnostic_and_existing_terminal_value(workspace = create_saved_workspace(state_dir, target)started = start_delivered_scan(state_dir,- "--workspace-id",str(workspace["id"]),"--scan-root",str(tmp_path / "scans"),- )["results"]+ )scan_id = str(started["scanId"])failed = fail_scan(state_dir, scan_id, message)["scan"]diff --git a/plugins/codex-security/tests/test_workbench_draft_publication.py b/plugins/codex-security/tests/test_workbench_draft_publication.pyindex 7d6435498..0c3da7d10 100644--- a/plugins/codex-security/tests/test_workbench_draft_publication.py+++ b/plugins/codex-security/tests/test_workbench_draft_publication.py@@ -7,33 +7,26 @@ from pathlib import Pathimport pytestfrom test_workbench_standard_deep_results import deep_scan_fixture-from workbench_test_support import read_review_state, run_workbench, write_completed_contract+from workbench_test_support import (+ read_draft_documents,+ read_review_state,+ run_workbench,+ write_completed_contract,+)[email protected]("filename", ["findings.json", "coverage.json"])[email protected]("previous", ["unfinished", "symlink"])-def test_terminal_deep_draft_compares_previous_bytes_without_parsing_review_documents(- tmp_path: Path, filename: str, previous: str-) -> None:- state_dir, codex_home, target, scan_dir, scan_id = deep_scan_fixture(tmp_path)+def stage_model_draft(tmp_path, scan_dir, scan_id, target, model, *, complete=True):contract_dir = tmp_path / "contract"contract_dir.mkdir()write_completed_contract(contract_dir, scan_id, target, relative_path="app.py", coverage_mode="deep_repository")- documents = {- key: json.loads((contract_dir / name).read_text())- for key, name in (- ("manifest", "scan-manifest.json"),- ("findings", "findings.json"),- ("coverage", "coverage.json"),- )- }- model = {"format": "markdown", "content": "# Saved model\n"}+ documents = read_draft_documents(contract_dir)documents["manifest"] = {"scan": {**{key: documents["manifest"]["scan"][key] for key in ("target", "scope")},"threatModel": model,+ **({} if complete else {"complete": False}),}}documents["findings"]["findings"] = []@@ -42,6 +35,17 @@ def test_terminal_deep_draft_compares_previous_bytes_without_parsing_review_docudrafts.mkdir(exist_ok=True)staged = drafts / f"{uuid.uuid4()}.json"staged.write_text(json.dumps(documents))+ return documents, staged++[email protected]("filename", ["findings.json", "coverage.json"])[email protected]("previous", ["unfinished", "symlink"])+def test_terminal_deep_draft_compares_previous_bytes_without_parsing_review_documents(+ tmp_path: Path, filename: str, previous: str+) -> None:+ state_dir, codex_home, target, scan_dir, scan_id = deep_scan_fixture(tmp_path)+ model = {"format": "markdown", "content": "# Saved model\n"}+ _, staged = stage_model_draft(tmp_path, scan_dir, scan_id, target, model)args = ("write-scan-draft", "--scan-id", scan_id, "--draft-path", str(staged))environment = {"CODEX_HOME": str(codex_home)}run_workbench(state_dir, *args, environment=environment)@@ -131,33 +135,10 @@ def test_deep_setup_draft_before_orchestration(),)scan_id = str(registered["scanId"])- contract_dir = tmp_path / "contract"- contract_dir.mkdir()- write_completed_contract(- contract_dir, scan_id, target, relative_path="app.py", coverage_mode="deep_repository"- )- documents = {- key: json.loads((contract_dir / name).read_text())- for key, name in (- ("manifest", "scan-manifest.json"),- ("findings", "findings.json"),- ("coverage", "coverage.json"),- )- }model = {"format": "markdown", "content": "# Supplied setup model\n"}- documents["manifest"] = {- "scan": {- **{key: documents["manifest"]["scan"][key] for key in ("target", "scope")},- "threatModel": model,- **({} if complete else {"complete": False}),- }- }- documents["findings"]["findings"] = []- documents["coverage"].update(surfaces=[], deferred=[])- drafts = scan_dir / "drafts"- drafts.mkdir(exist_ok=True)- staged = drafts / f"{uuid.uuid4()}.json"- staged.write_text(json.dumps(documents))+ documents, staged = stage_model_draft(+ tmp_path, scan_dir, scan_id, target, model, complete=complete+ )args = ("write-scan-draft", "--scan-id", scan_id, "--draft-path", str(staged))environment = {"CODEX_HOME": str(codex_home)}if complete and not initialized:diff --git a/plugins/codex-security/tests/test_workbench_feedback.py b/plugins/codex-security/tests/test_workbench_feedback.pyindex 0da7f232c..d4098a3aa 100644--- a/plugins/codex-security/tests/test_workbench_feedback.py+++ b/plugins/codex-security/tests/test_workbench_feedback.py@@ -32,11 +32,10 @@ def _create_workspace(state_dir: Path, target: Path) -> str:def _start_scan(state_dir: Path, workspace_id: str, scan_root: Path) -> dict[str, Any]:return start_delivered_scan(state_dir,- "--workspace-id",workspace_id,"--scan-root",str(scan_root),- )["results"]+ )def _complete_scan(diff --git a/plugins/codex-security/tests/test_workbench_finding_workflows.py b/plugins/codex-security/tests/test_workbench_finding_workflows.pyindex 9c7563e78..f1561b6c1 100644--- a/plugins/codex-security/tests/test_workbench_finding_workflows.py+++ b/plugins/codex-security/tests/test_workbench_finding_workflows.py@@ -4,6 +4,7 @@ import jsonimport osimport sqlite3from contextlib import closing+from functools import partialfrom pathlib import Pathimport pytest@@ -11,15 +12,20 @@ import pytestTIMESTAMP = "2026-08-01T00:00:00Z"-def workflow(api, connection, action, *, workflow_id="synthetic-workflow", **payload):+def run_workflow(api, connection, action, *, workflow_id="synthetic-workflow", **payload):return api["finding_workflow"](connection, {"id": workflow_id, "action": action, **payload}, TIMESTAMP)+def workflow(workbench_api, workbench_db):+ return partial(run_workflow, workbench_api, workbench_db)++@pytest.mark.parametrize("optional", [False, True])def test_source_snapshot_excludes_private_storage_without_hiding_source(- workbench_api, workbench_db, tmp_path, monkeypatch, optional+ workflow, tmp_path, monkeypatch, optional):repository = tmp_path / "repository"repository.mkdir()@@ -39,8 +45,6 @@ def test_source_snapshot_excludes_private_storage_without_hiding_source(def snapshot():return workflow(- workbench_api,- workbench_db,"source",repository=str(repository),privateStatePaths=[str(private)],@@ -54,26 +58,14 @@ def test_source_snapshot_excludes_private_storage_without_hiding_source(source.write_text("changed source")assert snapshot()["content"] != before["content"]with pytest.raises(SystemExit, match="repository root"):- workflow(- workbench_api,- workbench_db,- "source",- repository=str(repository),- privateStatePaths=[str(repository)],- )+ workflow("source", repository=str(repository), privateStatePaths=[str(repository)])with pytest.raises(SystemExit, match="absolute paths"):- workflow(- workbench_api,- workbench_db,- "source",- repository=str(repository),- privateStatePaths=["relative-state"],- )+ workflow("source", repository=str(repository), privateStatePaths=["relative-state"])@pytest.mark.parametrize("location", ["root", "descendant"])def test_source_snapshot_preserves_unreadable_directory_errors(- workbench_api, workbench_db, tmp_path, monkeypatch, location+ workflow, tmp_path, monkeypatch, location):repository = tmp_path / "repository"repository.mkdir()@@ -95,8 +87,6 @@ def test_source_snapshot_preserves_unreadable_directory_errors(def snapshot(excluded):return workflow(- workbench_api,- workbench_db,"source",repository=str(repository),gitDisabled=True,@@ -113,37 +103,25 @@ def test_source_snapshot_preserves_unreadable_directory_errors(@pytest.mark.parametrize("stage", ["scan", "publish", "dedupe"])-def test_failed_stages_resume_and_completed_results_are_immutable(- workbench_api, workbench_db, stage-):- assert workflow(workbench_api, workbench_db, "get") == {"workflow": None}- bound = workflow(workbench_api, workbench_db, "bind", binding={})["workflow"]+def test_failed_stages_resume_and_completed_results_are_immutable(workflow, stage):+ assert workflow("get") == {"workflow": None}+ bound = workflow("bind", binding={})["workflow"]assert bound["stages"] == {name: {"status": "pending"} for name in ("scan", "publish", "dedupe")}- assert (- workflow(workbench_api, workbench_db, "begin", stage=stage)["workflow"]["stages"][stage][- "status"- ]- == "running"- )- failed = workflow(workbench_api, workbench_db, "fail", stage=stage, error="Synthetic failure")+ assert workflow("begin", stage=stage)["workflow"]["stages"][stage]["status"] == "running"+ failed = workflow("fail", stage=stage, error="Synthetic failure")assert failed["workflow"]["stages"][stage] == {"status": "failed", "error": "Synthetic failure"}- assert (- workflow(workbench_api, workbench_db, "begin", stage=stage)["workflow"]["stages"][stage][- "status"- ]- == "running"- )+ assert workflow("begin", stage=stage)["workflow"]["stages"][stage]["status"] == "running"result = {"findingIds": [], "duplicateGroups": []}- completed = workflow(workbench_api, workbench_db, "complete", stage=stage, result=result)+ completed = workflow("complete", stage=stage, result=result)assert completed["workflow"]["stages"][stage] == {"status": "completed", "result": result}for action, payload in [("begin", {}),("fail", {"error": "Late failure", "result": {"unexpected": True}}),("complete", {"result": {"unexpected": True}}),]:- assert workflow(workbench_api, workbench_db, action, stage=stage, **payload) == completed+ assert workflow(action, stage=stage, **payload) == completed@pytest.mark.parametrize(@@ -156,59 +134,42 @@ def test_failed_stages_resume_and_completed_results_are_immutable(({"scope": {"allRepositories": True}}, {"scope": {"repositoryId": "repository-a"}}),],)-def test_bindings_and_workflow_identities_remain_separate(- workbench_api, workbench_db, binding, changed-):- workflow(workbench_api, workbench_db, "bind", binding=binding)- saved = workflow(- workbench_api, workbench_db, "complete", stage="dedupe", result={"duplicateGroups": []}- )+def test_bindings_and_workflow_identities_remain_separate(workflow, workbench_db, binding, changed):+ workflow("bind", binding=binding)+ saved = workflow("complete", stage="dedupe", result={"duplicateGroups": []})with pytest.raises(SystemExit, match="already bound to a different"):- workflow(workbench_api, workbench_db, "bind", binding=changed)+ workflow("bind", binding=changed)assert not workbench_db.in_transaction- assert workflow(workbench_api, workbench_db, "get") == saved- separate = workflow(- workbench_api, workbench_db, "bind", workflow_id="separate", binding=changed- )+ assert workflow("get") == saved+ separate = workflow("bind", workflow_id="separate", binding=changed)assert separate["workflow"]["id"] == "separate"assert separate["workflow"]["stages"]["dedupe"] == {"status": "pending"}for key, value in changed.items():assert separate["workflow"][key] == value- assert workflow(workbench_api, workbench_db, "get") == saved+ assert workflow("get") == saved@pytest.mark.parametrize("result", [{"duplicateGroups": []}, {"duplicateGroups": [["a", "b"]]}])-def test_pending_publication_payload_survives_failure_until_completion(- workbench_api, workbench_db, result-):- workflow(workbench_api, workbench_db, "bind", binding={})- workflow(workbench_api, workbench_db, "begin", stage="dedupe")+def test_pending_publication_payload_survives_failure_until_completion(workflow, result):+ workflow("bind", binding={})+ workflow("begin", stage="dedupe")pending = {"groups": result["duplicateGroups"]}- prepared = workflow(- workbench_api,- workbench_db,- "prepare-dedupe",- stage="dedupe",- result=result,- pendingWrite=pending,- )+ prepared = workflow("prepare-dedupe", stage="dedupe", result=result, pendingWrite=pending)assert prepared["workflow"]["stages"]["dedupe"] == {"status": "running","result": result,"pendingWrite": pending,}- workflow(workbench_api, workbench_db, "fail", stage="dedupe", error="Lost acknowledgement")- resumed = workflow(workbench_api, workbench_db, "begin", stage="dedupe")["workflow"]["stages"][- "dedupe"- ]+ workflow("fail", stage="dedupe", error="Lost acknowledgement")+ resumed = workflow("begin", stage="dedupe")["workflow"]["stages"]["dedupe"]assert resumed["result"] == resultassert resumed["pendingWrite"] == pending- completed = workflow(workbench_api, workbench_db, "complete", stage="dedupe", result=result)+ completed = workflow("complete", stage="dedupe", result=result)assert completed["workflow"]["stages"]["dedupe"] == {"status": "completed", "result": result}def test_review_checkpoints_keep_the_first_valid_result_and_enforce_workflow_ownership(- workbench_api, workbench_db+ workflow, workbench_db):binding = {"version": 1,@@ -228,59 +189,29 @@ def test_review_checkpoints_keep_the_first_valid_result_and_enforce_workflow_own}result = {"decision": "DISTINCT", "rationale": "Independent corrections"}with pytest.raises(sqlite3.IntegrityError):- workflow(- workbench_api,- workbench_db,- "save-review",- key="review-1",- binding=binding,- result=result,- )- workflow(workbench_api, workbench_db, "bind", binding={})- assert workflow(workbench_api, workbench_db, "get-review", key="review-1") == {"review": None}- workflow(- workbench_api, workbench_db, "save-review", key="review-1", binding=binding, result=result- )- workflow(- workbench_api,- workbench_db,- "save-review",- key="review-1",- binding=binding,- result={"unexpected": True},- )- assert workflow(workbench_api, workbench_db, "get-review", key="review-1") == {"review": result}- assert workflow(- workbench_api, workbench_db, "get-review", workflow_id="another", key="review-1"- ) == {"review": None}+ workflow("save-review", key="review-1", binding=binding, result=result)+ workflow("bind", binding={})+ assert workflow("get-review", key="review-1") == {"review": None}+ workflow("save-review", key="review-1", binding=binding, result=result)+ workflow("save-review", key="review-1", binding=binding, result={"unexpected": True})+ assert workflow("get-review", key="review-1") == {"review": result}+ assert workflow("get-review", workflow_id="another", key="review-1") == {"review": None}row = workbench_db.execute("SELECT * FROM finding_workflow_reviews").fetchone()assert row["source_repository_path"] == binding["source"]["repository"]assert row["settings_digest"] == binding["settingsDigest"]assert json.loads(row["result_json"]) == resultreplacement = {"assessment": {"report": "Regenerated validation"}, "evidenceDigest": "new"}+ workflow("save-review", key="review-1", binding=binding, result=replacement, replace=True)+ assert workflow("get-review", key="review-1") == {"review": result}workflow(- workbench_api,- workbench_db,- "save-review",- key="review-1",- binding=binding,- result=replacement,- replace=True,- )- assert workflow(workbench_api, workbench_db, "get-review", key="review-1") == {"review": result}- workflow(- workbench_api,- workbench_db,"save-review",key="review-1",binding={**binding, "stage": "scan-validation"},result=replacement,replace=True,)- assert workflow(workbench_api, workbench_db, "get-review", key="review-1") == {- "review": replacement- }+ assert workflow("get-review", key="review-1") == {"review": replacement}@pytest.mark.parametrize(@@ -292,13 +223,13 @@ def test_review_checkpoints_keep_the_first_valid_result_and_enforce_workflow_own],)def test_rejected_workflow_mutations_roll_back_without_leaking_state(- workbench_api, workbench_db, payload+ workflow, workbench_db, payload):- saved = workflow(workbench_api, workbench_db, "bind", binding={})+ saved = workflow("bind", binding={})with pytest.raises(SystemExit):- workflow(workbench_api, workbench_db, **payload)+ workflow(**payload)assert not workbench_db.in_transaction- assert workflow(workbench_api, workbench_db, "get") == saved+ assert workflow("get") == saveddef test_workflow_column_migration_is_atomic_and_preserves_resume_state(workbench_api, tmp_path):@@ -428,13 +359,10 @@ def test_workflow_column_migration_is_atomic_and_preserves_resume_state(workbencconnection.row_factory = sqlite3.Rowconnection.execute("PRAGMA foreign_keys = ON")with closing(connection):+ migrated_workflow = partial(run_workflow, workbench_api, connection)for state in (completed, unfinished, pending):- assert workflow(workbench_api, connection, "get", workflow_id=state["id"]) == {- "workflow": state- }- assert workflow(- workbench_api,- connection,+ assert migrated_workflow("get", workflow_id=state["id"]) == {"workflow": state}+ assert migrated_workflow("bind",workflow_id=completed["id"],binding={"scanId": completed["scanId"], "scope": completed["scope"]},@@ -442,22 +370,14 @@ def test_workflow_column_migration_is_atomic_and_preserves_resume_state(workbencreview = connection.execute("SELECT * FROM finding_workflow_reviews").fetchone()assert review["scope_repository_id"] == completed["scope"]["repositoryId"]assert review["prompt_digest"] == review_binding["promptDigest"]- assert workflow(- workbench_api,- connection,- "get-review",- workflow_id=completed["id"],- key="synthetic-review",+ assert migrated_workflow(+ "get-review", workflow_id=completed["id"], key="synthetic-review") == {"review": review_result}for stage in ("scan", "publish", "dedupe"):- state = workflow(- workbench_api, connection, "begin", workflow_id=completed["id"], stage=stage- )+ state = migrated_workflow("begin", workflow_id=completed["id"], stage=stage)assert state == {"workflow": completed}- workflow(workbench_api, connection, "begin", workflow_id=unfinished["id"], stage="scan")- resumed = workflow(- workbench_api,- connection,+ migrated_workflow("begin", workflow_id=unfinished["id"], stage="scan")+ resumed = migrated_workflow("complete",workflow_id=unfinished["id"],stage="scan",@@ -470,38 +390,27 @@ def test_workflow_column_migration_is_atomic_and_preserves_resume_state(workbenc@pytest.mark.skipif(not hasattr(os, "mkfifo"), reason="FIFOs require a Unix filesystem")-def test_optional_review_snapshot_disables_cache_for_unsupported_file_types(- workbench_api, workbench_db, tmp_path-):+def test_optional_review_snapshot_disables_cache_for_unsupported_file_types(workflow, tmp_path):target = tmp_path / "repository"target.mkdir()source = target / "source.ts"source.write_text("export const value = 1;\n", encoding="utf-8")- first = workflow(workbench_api, workbench_db, "source", repository=str(target))+ first = workflow("source", repository=str(target))assert first["source"]["content"].startswith("codex-security-snapshot/v1:")pipe = target / "pipe"os.mkfifo(pipe)with pytest.raises(SystemExit, match="Unsupported local file type: pipe"):- workflow(workbench_api, workbench_db, "source", repository=str(target))- assert workflow(- workbench_api, workbench_db, "source", repository=str(target), optional=True- ) == {"source": None}+ workflow("source", repository=str(target))+ assert workflow("source", repository=str(target), optional=True) == {"source": None}pipe.unlink()- assert (- workflow(workbench_api, workbench_db, "source", repository=str(target), optional=True)- == first- )+ assert workflow("source", repository=str(target), optional=True) == firstsource.write_text("export const value = 2;\n", encoding="utf-8")assert (- workflow(workbench_api, workbench_db, "source", repository=str(target), optional=True)[- "source"- ]["content"]+ workflow("source", repository=str(target), optional=True)["source"]["content"]!= first["source"]["content"])with pytest.raises(FileNotFoundError):- workflow(- workbench_api, workbench_db, "source", repository=str(target / "missing"), optional=True- )+ workflow("source", repository=str(target / "missing"), optional=True)@pytest.mark.skipif(@@ -509,9 +418,7 @@ def test_optional_review_snapshot_disables_cache_for_unsupported_file_types(reason="Mode permissions require a non-root Unix user",)@pytest.mark.parametrize("kind", ["file", "directory"])-def test_optional_review_snapshot_disables_cache_for_unreadable_entries(- workbench_api, workbench_db, tmp_path, kind-):+def test_optional_review_snapshot_disables_cache_for_unreadable_entries(workflow, tmp_path, kind):target = tmp_path / "repository"target.mkdir()(target / "source.ts").write_text("export const value = 1;\n", encoding="utf-8")@@ -521,78 +428,45 @@ def test_optional_review_snapshot_disables_cache_for_unreadable_entries((entry / "cache.txt").write_text("Synthetic cache data.", encoding="utf-8")else:entry.write_text("Synthetic cache data.", encoding="utf-8")- first = workflow(workbench_api, workbench_db, "source", repository=str(target), optional=True)+ first = workflow("source", repository=str(target), optional=True)mode = entry.stat().st_modeentry.chmod(0)try:with pytest.raises((PermissionError, SystemExit)):- workflow(workbench_api, workbench_db, "source", repository=str(target))- assert workflow(- workbench_api, workbench_db, "source", repository=str(target), optional=True- ) == {"source": None}+ workflow("source", repository=str(target))+ assert workflow("source", repository=str(target), optional=True) == {"source": None}finally:entry.chmod(mode)- assert (- workflow(workbench_api, workbench_db, "source", repository=str(target), optional=True)- == first- )+ assert workflow("source", repository=str(target), optional=True) == firstwith pytest.raises(FileNotFoundError):- workflow(- workbench_api, workbench_db, "source", repository=str(target / "missing"), optional=True- )+ workflow("source", repository=str(target / "missing"), optional=True)@pytest.mark.parametrize("metadata_name", [".git", ".svn"])-def test_optional_evidence_snapshot_includes_source_metadata(- workbench_api, workbench_db, tmp_path, metadata_name-):+def test_optional_evidence_snapshot_includes_source_metadata(workflow, tmp_path, metadata_name):evidence = tmp_path / "evidence"metadata = evidence / metadata_namemetadata.mkdir(parents=True)(evidence / "proof.txt").write_text("Original proof.\n")head = metadata / "HEAD"head.write_text("Synthetic metadata.\n")- source = workflow(workbench_api, workbench_db, "source", repository=str(evidence))- before = workflow(- workbench_api,- workbench_db,- "source",- repository=str(evidence),- evidence=True,- optional=True,- )+ source = workflow("source", repository=str(evidence))+ before = workflow("source", repository=str(evidence), evidence=True, optional=True)head.write_text("Changed metadata.\n")- assert workflow(workbench_api, workbench_db, "source", repository=str(evidence)) == source- assert (- workflow(- workbench_api,- workbench_db,- "source",- repository=str(evidence),- evidence=True,- optional=True,- )- != before- )+ assert workflow("source", repository=str(evidence)) == source+ assert workflow("source", repository=str(evidence), evidence=True, optional=True) != beforeif hasattr(os, "mkfifo"):pipe = metadata / "pipe"os.mkfifo(pipe)try:- assert workflow(- workbench_api,- workbench_db,- "source",- repository=str(evidence),- evidence=True,- optional=True,- ) == {"source": None}+ assert workflow("source", repository=str(evidence), evidence=True, optional=True) == {+ "source": None+ }finally:pipe.unlink()-def test_optional_evidence_snapshot_does_not_follow_directory_links(- workbench_api, workbench_db, tmp_path-):+def test_optional_evidence_snapshot_does_not_follow_directory_links(workflow, tmp_path):evidence = tmp_path / "evidence"evidence.mkdir()outside = tmp_path / "outside"@@ -606,40 +480,18 @@ def test_optional_evidence_snapshot_does_not_follow_directory_links(if os.name == "nt":pytest.skip("Directory symlinks are unavailable on this Windows filesystem")raise- before = workflow(- workbench_api,- workbench_db,- "source",- repository=str(evidence),- evidence=True,- optional=True,- )+ before = workflow("source", repository=str(evidence), evidence=True, optional=True)source.write_text("Changed outside contents.\n")- assert (- workflow(- workbench_api,- workbench_db,- "source",- repository=str(evidence),- evidence=True,- optional=True,- )- == before- )+ assert workflow("source", repository=str(evidence), evidence=True, optional=True) == beforeevidence.rename(tmp_path / "original")evidence.symlink_to(outside, target_is_directory=True)- assert workflow(- workbench_api,- workbench_db,- "source",- repository=str(evidence),- evidence=True,- optional=True,- ) == {"source": None}+ assert workflow("source", repository=str(evidence), evidence=True, optional=True) == {+ "source": None+ }@pytest.mark.parametrize("kind", ["missing", "file", "unreadable"])-def test_optional_evidence_snapshot_unavailable_roots(workbench_api, workbench_db, tmp_path, kind):+def test_optional_evidence_snapshot_unavailable_roots(workflow, tmp_path, kind):evidence = tmp_path / "evidence"if kind == "file":evidence.write_text("Not a directory.\n")@@ -649,23 +501,12 @@ def test_optional_evidence_snapshot_unavailable_roots(workbench_api, workbench_devidence.mkdir()evidence.chmod(0)try:- assert workflow(- workbench_api,- workbench_db,- "source",- repository=str(evidence),- evidence=True,- optional=True,- ) == {"source": None}+ assert workflow("source", repository=str(evidence), evidence=True, optional=True) == {+ "source": None+ }if kind == "missing":with pytest.raises(FileNotFoundError):- workflow(- workbench_api,- workbench_db,- "source",- repository=str(evidence),- optional=True,- )+ workflow("source", repository=str(evidence), optional=True)finally:if kind == "unreadable":evidence.chmod(0o700)diff --git a/plugins/codex-security/tests/test_workbench_native_indexes.py b/plugins/codex-security/tests/test_workbench_native_indexes.pyindex 3dda22264..6e8d34f74 100644--- a/plugins/codex-security/tests/test_workbench_native_indexes.py+++ b/plugins/codex-security/tests/test_workbench_native_indexes.py@@ -34,9 +34,9 @@ def complete_scan(workspace = save_workspace(state_dir, str(workspace["id"]), str(target), include_paths[0], "standard")- started = start_delivered_scan(state_dir, "--workspace-id", str(workspace["id"]))- scan_id = str(started["results"]["scanId"])- scan_dir = Path(str(started["results"]["scanDir"]))+ started = start_delivered_scan(state_dir, str(workspace["id"]))+ scan_id = str(started["scanId"])+ scan_dir = Path(str(started["scanDir"]))write_completed_contract(scan_dir,scan_id,@@ -259,7 +259,7 @@ def test_repository_index_reports_latest_scan_open_findings_and_missing_checkout"Fixture close decision.",)running_workspace = create_saved_workspace(state_dir, first_target)- older_running = start_delivered_scan(state_dir, "--workspace-id", str(running_workspace["id"]))+ older_running = start_delivered_scan(state_dir, str(running_workspace["id"]))complete_scan(state_dir, first_target, identity_anchor="first-finding")distinct_first = complete_scan(state_dir,@@ -269,7 +269,7 @@ def test_repository_index_reports_latest_scan_open_findings_and_missing_checkoutrelative_path="docs/extract.py",)latest_second = complete_scan(state_dir, second_target, identity_anchor="second-finding")- update_progress(state_dir, str(older_running["results"]["scanId"]), "--phase", "discovery")+ update_progress(state_dir, str(older_running["scanId"]), "--phase", "discovery")second_target.rename(tmp_path / "moved-second-repo")repositories = run_workbench(state_dir, "list-repositories")["repositories"]diff --git a/plugins/codex-security/tests/test_workbench_progress.py b/plugins/codex-security/tests/test_workbench_progress.pyindex cd24961ae..aa44a2898 100644--- a/plugins/codex-security/tests/test_workbench_progress.py+++ b/plugins/codex-security/tests/test_workbench_progress.py@@ -13,8 +13,8 @@ from workbench_test_support import (def test_progress_preserves_phase_receipts_until_validation(tmp_path: Path) -> None:state_dir, target = empty_target_paths(tmp_path)saved = create_saved_workspace(state_dir, target)- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- scan_id = str(started["results"]["scanId"])+ started = start_delivered_scan(state_dir, str(saved["id"]))+ scan_id = str(started["scanId"])discovery = update_progress(state_dir,@@ -69,8 +69,8 @@ def test_progress_preserves_phase_receipts_until_validation(tmp_path: Path) -> Ndef test_historical_preflight_issues_survive_progress_updates(tmp_path: Path) -> None:state_dir, target = empty_target_paths(tmp_path)saved = create_saved_workspace(state_dir, target)- started = start_delivered_scan(state_dir, "--workspace-id", str(saved["id"]))- scan_id = str(started["results"]["scanId"])+ started = start_delivered_scan(state_dir, str(saved["id"]))+ scan_id = str(started["scanId"])issues = [{"capability": "delegated_workers",diff --git a/plugins/codex-security/tests/test_workbench_recovery_outcome_validation.py b/plugins/codex-security/tests/test_workbench_recovery_outcome_validation.pyindex 518455549..de3d34da4 100644--- a/plugins/codex-security/tests/test_workbench_recovery_outcome_validation.py+++ b/plugins/codex-security/tests/test_workbench_recovery_outcome_validation.py@@ -2,11 +2,9 @@ from __future__ import annotationsimport copyimport json-import osfrom types import SimpleNamespaceimport pytest-from test_deep_scan_successful_publication import completefrom test_deep_scan_successful_publication import publication_scan as publication_scanfrom workbench_test_support import read_review_state, saved_draft, write_checkpoint@@ -30,9 +28,10 @@ def test_stopped_scan_requires_valid_rejection_evidence_before_sealing(finding = copy.deepcopy(scan.findings[0])finding["provenance"] = {"source": "local_plugin", "candidateId": "candidate-a"}checkpoint = write_checkpoint(- scan.scan_dir / "checkpoints", saved_draft(scan.scan_id, findings=[finding])+ scan.scan_dir / "checkpoints",+ saved_draft(scan.scan_id, findings=[finding]),+ mtime_ns=100,)- os.utime(checkpoint, ns=(100, 100))checkpoint_bytes = checkpoint.read_bytes()(scan.scan_dir / "findings.json").write_text(json.dumps({"findings": []}))scan.coverage["surfaces"] = (@@ -98,7 +97,7 @@ def test_stopped_scan_requires_valid_rejection_evidence_before_sealing(],)def test_standard_completion_requires_valid_closure_evidence_before_sealing(- workbench_api, workbench_db, publication_scan, reason, invalid+ workbench_db, publication_scan, reason, invalid):scan = publication_scan(mode="standard")report_path = scan.scan_dir / "report.md"@@ -109,8 +108,8 @@ def test_standard_completion_requires_valid_closure_evidence_before_sealing(scan.scan_id,deferred=[{"id": "review", "reason": "Review authorization remains pending."}],),+ mtime_ns=100,)- os.utime(checkpoint, ns=(100, 100))checkpoint_bytes = checkpoint.read_bytes()scan.coverage["deferred"] = []closure = {"id": "review", "reason": reason}@@ -121,9 +120,9 @@ def test_standard_completion_requires_valid_closure_evidence_before_sealing(if invalid:with pytest.raises(SystemExit, match="well-formed Unicode"):- complete(workbench_api, workbench_db, scan)+ scan.complete()else:- completed = complete(workbench_api, workbench_db, scan)+ completed = scan.complete()assert completed["warnings"] == []assert checkpoint.read_bytes() == checkpoint_bytesdiff --git a/plugins/codex-security/tests/test_workbench_remediation_cancellation.py b/plugins/codex-security/tests/test_workbench_remediation_cancellation.pyindex 178d2583f..4ef1c6d64 100644--- a/plugins/codex-security/tests/test_workbench_remediation_cancellation.py+++ b/plugins/codex-security/tests/test_workbench_remediation_cancellation.py@@ -1,19 +1,14 @@from __future__ import annotations-import hashlibimport subprocessimport uuidfrom pathlib import Pathimport pytestfrom workbench_test_support import (- cancel_remediation_request,+ RemediationRequest,empty_target_paths,- mark_remediation_delivered,- request_remediation,- request_remediation_action,run_workbench,- set_remediation,start_saved_scan,write_completed_contract,write_remediation_patch,@@ -32,78 +27,47 @@ def test_cancel_finding_remediation_request_restores_previous_state(completed = run_workbench(state_dir, "complete-scan", "--scan-id", scan_id)["scan"]occurrence_id = str(completed["findings"][0]["occurrenceId"])- canceled_request_id = str(uuid.uuid4())+ canceled_request = RemediationRequest(state_dir, occurrence_id)canceled_token = str(uuid.uuid4())- requested_initial = request_remediation(- state_dir, occurrence_id, canceled_request_id, canceled_token- )["scan"]- canceled_initial = cancel_remediation_request(- state_dir, occurrence_id, canceled_request_id, canceled_token- )["scan"]+ requested_initial = canceled_request.start(canceled_token)["scan"]+ canceled_initial = canceled_request.cancel(canceled_token)["scan"]assert canceled_initial["findings"][0]["remediationState"] == {"state": "idle"}assert canceled_initial["updatedAt"] > requested_initial["updatedAt"]- replayed_cancel = cancel_remediation_request(- state_dir, occurrence_id, canceled_request_id, canceled_token- )["scan"]+ replayed_cancel = canceled_request.cancel(canceled_token)["scan"]assert replayed_cancel["findings"][0]["remediationState"] == {"state": "idle"}- request_id = str(uuid.uuid4())+ request = RemediationRequest(state_dir, occurrence_id)generation_token = str(uuid.uuid4())- request_remediation(state_dir, occurrence_id, request_id, generation_token)+ request.start(generation_token)patch_path = scan_dir / "remediation.patch"write_remediation_patch(patch_path)- generated = set_remediation(- state_dir,- occurrence_id,- request_id,- generation_token,- "1",- "generated",- "--patch-path",- patch_path.name,- "--patch-digest",- f"sha256:{hashlib.sha256(patch_path.read_bytes()).hexdigest()}",- )["scan"]+ generated = request.generate(generation_token, patch_path)["scan"]assert generated["findings"][0]["remediationState"]["state"] == "generated"- replacement_request_id = str(uuid.uuid4())+ replacement_request = RemediationRequest(state_dir, occurrence_id)replacement_token = str(uuid.uuid4())- request_remediation(state_dir, occurrence_id, replacement_request_id, replacement_token)- canceled_replacement = cancel_remediation_request(- state_dir, occurrence_id, replacement_request_id, replacement_token- )["scan"]+ replacement_request.start(replacement_token)+ canceled_replacement = replacement_request.cancel(replacement_token)["scan"]restored = canceled_replacement["findings"][0]["remediationState"]- assert restored["requestId"] == request_id+ assert restored["requestId"] == request.idassert restored["state"] == "generated"assert restored["pendingAction"] is Noneassert restored["patchPath"] == patch_path.nameapply_token = str(uuid.uuid4())- request_remediation_action(- state_dir, occurrence_id, request_id, str(restored["version"]), "apply", apply_token- )- canceled_apply = cancel_remediation_request(state_dir, occurrence_id, request_id, apply_token)[- "scan"- ]+ request.request_action(str(restored["version"]), "apply", apply_token)+ canceled_apply = request.cancel(apply_token)["scan"]restored_after_apply = canceled_apply["findings"][0]["remediationState"]assert restored_after_apply["state"] == "generated"assert restored_after_apply["pendingAction"] is Noneassert restored_after_apply["actionClaimToken"] is Noneapply_token = str(uuid.uuid4())- apply_requested = request_remediation_action(- state_dir,- occurrence_id,- request_id,- str(restored_after_apply["version"]),- "apply",- apply_token,+ apply_requested = request.request_action(+ str(restored_after_apply["version"]), "apply", apply_token)["scan"]subprocess.run(["git", "apply", "--no-index", str(patch_path)], cwd=target, check=True)- applied = set_remediation(- state_dir,- occurrence_id,- request_id,+ applied = request.update(apply_token,str(apply_requested["findings"][0]["remediationState"]["version"]),"applied",@@ -112,20 +76,12 @@ def test_cancel_finding_remediation_request_restores_previous_state()["scan"]verify_token = str(uuid.uuid4())- verify_requested = request_remediation_action(- state_dir,- occurrence_id,- request_id,- str(applied["findings"][0]["remediationState"]["version"]),- "verify",- verify_token,+ verify_requested = request.request_action(+ str(applied["findings"][0]["remediationState"]["version"]), "verify", verify_token)["scan"]["findings"][0]["remediationState"]if verification_started:- mark_remediation_delivered(state_dir, occurrence_id, request_id, verify_token)- verify_requested = set_remediation(- state_dir,- occurrence_id,- request_id,+ request.mark_delivered(verify_token)+ verify_requested = request.update(verify_token,str(verify_requested["version"]),"verifying",@@ -138,16 +94,14 @@ def test_cancel_finding_remediation_request_restores_previous_state("--occurrence-id",occurrence_id,"--request-id",- request_id,+ request.id,"--action-token",str(uuid.uuid4()),check=False,)assert wrong_owner["returncode"] != 0assert "owned by a different action token" in wrong_owner["stderr"]- canceled_verify = cancel_remediation_request(- state_dir, occurrence_id, request_id, verify_token- )["scan"]+ canceled_verify = request.cancel(verify_token)["scan"]restored_after_verify = canceled_verify["findings"][0]["remediationState"]assert restored_after_verify["state"] == "applied"assert restored_after_verify["pendingAction"] is None@@ -158,28 +112,15 @@ def test_cancel_finding_remediation_request_restores_previous_state(assert restored_after_verify["patchPath"] == patch_path.nameassert restored_after_verify["patchDigest"] == verify_requested["patchDigest"]assert source.read_text() == "fixed\n"- replayed_verify = cancel_remediation_request(- state_dir, occurrence_id, request_id, verify_token- )["scan"]["findings"][0]["remediationState"]+ replayed_verify = request.cancel(verify_token)["scan"]["findings"][0]["remediationState"]assert replayed_verify == restored_after_verify- stale_update = set_remediation(- state_dir,- occurrence_id,- request_id,- verify_token,- str(verify_requested["version"]),- "failed",- check=False,+ stale_update = request.update(+ verify_token, str(verify_requested["version"]), "failed", check=False)assert stale_update["returncode"] != 0assert "This remediation request changed" in stale_update["stderr"]- retried_verify = request_remediation_action(- state_dir,- occurrence_id,- request_id,- str(restored_after_verify["version"]),- "verify",- str(uuid.uuid4()),+ retried_verify = request.request_action(+ str(restored_after_verify["version"]), "verify", str(uuid.uuid4()))["scan"]["findings"][0]["remediationState"]assert retried_verify["pendingAction"] == "verify"diff --git a/plugins/codex-security/tests/test_workbench_remediation_retry.py b/plugins/codex-security/tests/test_workbench_remediation_retry.pyindex 753a579f5..aa2e41ad1 100644--- a/plugins/codex-security/tests/test_workbench_remediation_retry.py+++ b/plugins/codex-security/tests/test_workbench_remediation_retry.py@@ -8,14 +8,9 @@ from pathlib import Pathfrom typing import Anyfrom workbench_test_support import (- cancel_remediation_request,- claim_remediation_resend,+ RemediationRequest,empty_target_paths,- mark_remediation_delivered,- request_remediation,- request_remediation_action,scan_command,- set_remediation,set_triage,start_saved_scan,write_completed_contract,@@ -24,17 +19,9 @@ from workbench_test_support import (def update_remediation(- state_dir: Path,- occurrence_id: str,- request_id: str,- action_token: str,- expected_version: int,- state: str,- *extra: str,+ request: RemediationRequest, action_token: str, expected_version: int, state: str, *extra: str) -> dict[str, Any]:- result = set_remediation(- state_dir, occurrence_id, request_id, action_token, str(expected_version), state, *extra- )+ result = request.update(action_token, str(expected_version), state, *extra)return result["scan"]["findings"][0]["remediationState"]@@ -47,14 +34,12 @@ def test_failed_remediation_steps_can_retry_or_regenerate(tmp_path: Path) -> Noncompleted = scan_command(state_dir, "complete-scan", scan_id)["scan"]occurrence_id = str(completed["findings"][0]["occurrenceId"])- request_id = str(uuid.uuid4())+ request = RemediationRequest(state_dir, occurrence_id)action_token = str(uuid.uuid4())- request_remediation(state_dir, occurrence_id, request_id, action_token)- mark_remediation_delivered(state_dir, occurrence_id, request_id, action_token)+ request.start(action_token)+ request.mark_delivered(action_token)failed = update_remediation(- state_dir,- occurrence_id,- request_id,+ request,action_token,1,"failed",@@ -78,30 +63,24 @@ def test_failed_remediation_steps_can_retry_or_regenerate(tmp_path: Path) -> Nonset_triage(state_dir, occurrence_id, "open")resend_token = str(uuid.uuid4())- resent = claim_remediation_resend(state_dir, occurrence_id, request_id, resend_token)["scan"]+ resent = request.claim_resend(resend_token)["scan"]assert resent["findings"][0]["remediationState"]["actionClaimToken"] == resend_token- delivered = mark_remediation_delivered(state_dir, occurrence_id, request_id, resend_token)[- "scan"- ]+ delivered = request.mark_delivered(resend_token)["scan"]assert delivered["findings"][0]["remediationState"]["actionDeliveredAt"]- canceled = cancel_remediation_request(state_dir, occurrence_id, request_id, resend_token)[- "scan"- ]["findings"][0]["remediationState"]+ canceled = request.cancel(resend_token)["scan"]["findings"][0]["remediationState"]assert canceled["state"] == "failed"assert canceled["pendingAction"] == "generate"assert canceled["actionClaimToken"] is None- replayed = cancel_remediation_request(state_dir, occurrence_id, request_id, resend_token)[- "scan"- ]["findings"][0]["remediationState"]+ replayed = request.cancel(resend_token)["scan"]["findings"][0]["remediationState"]assert replayed == canceledresend_token = str(uuid.uuid4())- claim_remediation_resend(state_dir, occurrence_id, request_id, resend_token)+ request.claim_resend(resend_token)- failed_request_id = request_id- request_id = str(uuid.uuid4())+ failed_request_id = request.id+ request = RemediationRequest(state_dir, occurrence_id)action_token = str(uuid.uuid4())- blocked = request_remediation(state_dir, occurrence_id, request_id, action_token, check=False)+ blocked = request.start(action_token, check=False)assert blocked["returncode"] != 0with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:connection.execute(@@ -112,18 +91,14 @@ def test_failed_remediation_steps_can_retry_or_regenerate(tmp_path: Path) -> Non""",("2000-01-01T00:00:00Z", failed_request_id),)- regenerated = request_remediation(state_dir, occurrence_id, request_id, action_token)["scan"][- "findings"- ][0]["remediationState"]+ regenerated = request.start(action_token)["scan"]["findings"][0]["remediationState"]assert regenerated["state"] == "requested"patch_path = scan_dir / "remediation.patch"write_remediation_patch(patch_path)patch_digest = f"sha256:{hashlib.sha256(patch_path.read_bytes()).hexdigest()}"update_remediation(- state_dir,- occurrence_id,- request_id,+ request,action_token,1,"generated",@@ -134,12 +109,10 @@ def test_failed_remediation_steps_can_retry_or_regenerate(tmp_path: Path) -> Non)apply_token = str(uuid.uuid4())- request_remediation_action(state_dir, occurrence_id, request_id, "2", "apply", apply_token)+ request.request_action("2", "apply", apply_token)subprocess.run(["git", "apply", "--no-index", str(patch_path)], cwd=target, check=True)failed = update_remediation(- state_dir,- occurrence_id,- request_id,+ request,apply_token,3,"failed",@@ -148,37 +121,19 @@ def test_failed_remediation_steps_can_retry_or_regenerate(tmp_path: Path) -> Non)assert failed["pendingAction"] == "apply"apply_retry_token = str(uuid.uuid4())- claim_remediation_resend(state_dir, occurrence_id, request_id, apply_retry_token)+ request.claim_resend(apply_retry_token)applied = update_remediation(- state_dir,- occurrence_id,- request_id,- apply_retry_token,- 4,- "applied",- "--base-revision",- "unversioned",+ request, apply_retry_token, 4, "applied", "--base-revision", "unversioned")assert applied["state"] == "applied"assert applied["summary"] is Noneverify_token = str(uuid.uuid4())- request_remediation_action(state_dir, occurrence_id, request_id, "5", "verify", verify_token)- mark_remediation_delivered(state_dir, occurrence_id, request_id, verify_token)- update_remediation(- state_dir,- occurrence_id,- request_id,- verify_token,- 6,- "verifying",- "--base-revision",- "unversioned",- )+ request.request_action("5", "verify", verify_token)+ request.mark_delivered(verify_token)+ update_remediation(request, verify_token, 6, "verifying", "--base-revision", "unversioned")failed = update_remediation(- state_dir,- occurrence_id,- request_id,+ request,verify_token,7,"failed",@@ -188,19 +143,15 @@ def test_failed_remediation_steps_can_retry_or_regenerate(tmp_path: Path) -> Nonassert failed["pendingAction"] == "verify"assert failed["actionClaimToken"] is Noneassert failed["actionDeliveredAt"] is None- canceled = cancel_remediation_request(state_dir, occurrence_id, request_id, verify_token)[- "scan"- ]["findings"][0]["remediationState"]+ canceled = request.cancel(verify_token)["scan"]["findings"][0]["remediationState"]assert canceled == failedverify_retry_token = str(uuid.uuid4())- claim_remediation_resend(state_dir, occurrence_id, request_id, verify_retry_token)- delivered = mark_remediation_delivered(- state_dir, occurrence_id, request_id, verify_retry_token- )["scan"]["findings"][0]["remediationState"]+ request.claim_resend(verify_retry_token)+ delivered = request.mark_delivered(verify_retry_token)["scan"]["findings"][0][+ "remediationState"+ ]assert delivered["actionDeliveredAt"]- canceled = cancel_remediation_request(state_dir, occurrence_id, request_id, verify_retry_token)[- "scan"- ]["findings"][0]["remediationState"]+ canceled = request.cancel(verify_retry_token)["scan"]["findings"][0]["remediationState"]assert canceled["state"] == "failed"assert canceled["pendingAction"] == "verify"assert canceled["summary"] == failed["summary"]@@ -209,11 +160,9 @@ def test_failed_remediation_steps_can_retry_or_regenerate(tmp_path: Path) -> Nonassert canceled["actionClaimedAt"] is Noneassert canceled["actionDeliveredAt"] is Noneverify_retry_token = str(uuid.uuid4())- claim_remediation_resend(state_dir, occurrence_id, request_id, verify_retry_token)+ request.claim_resend(verify_retry_token)verified = update_remediation(- state_dir,- occurrence_id,- request_id,+ request,verify_retry_token,8,"verified",diff --git a/plugins/codex-security/tests/test_workbench_saved_source_order.py b/plugins/codex-security/tests/test_workbench_saved_source_order.pyindex e18dfdaa5..3defae934 100644--- a/plugins/codex-security/tests/test_workbench_saved_source_order.py+++ b/plugins/codex-security/tests/test_workbench_saved_source_order.py@@ -7,7 +7,7 @@ import sysfrom pathlib import Pathimport pytest-from test_workbench_checkpoint_heads import drafts, select+from test_workbench_checkpoint_heads import draftsfrom test_workbench_standard_deep_results import (accepted_standard_worker,deep_scan_fixture,@@ -22,7 +22,9 @@ from workbench_test_support import (saved_discovery_worker,saved_draft,scan_command,+ select_checkpoint,write_checkpoint,+ write_result_draft,)sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))@@ -74,11 +76,9 @@ def test_failed_publication_keeps_order_after_identical_result_rewrite(worker_id, result = accepted_standard_worker(state, codex_home, scan_dir, scan_id)pending, closed = drafts(scan_id)old, latest = (closed, pending) if latest_pending else (pending, closed)- result.write_text(json.dumps(old))- os.utime(result, ns=(100, 100))- checkpoint = write_checkpoint(result.parent / "checkpoints", latest)- os.utime(checkpoint, ns=(200, 200))- select(result.parent, checkpoint, 200)+ write_result_draft(result, old, mtime_ns=100)+ checkpoint = write_checkpoint(result.parent / "checkpoints", latest, mtime_ns=200)+ select_checkpoint(result.parent, checkpoint, mtime_ns=200)def fail_publication(*args, **kwargs):raise OSError("injected publication failure")@@ -193,12 +193,10 @@ def test_head_capture_includes_checkpoint_published_after_enumeration(state, codex_home, _, scan_dir, scan_id = deep_scan_fixture(tmp_path)worker_id, result = accepted_standard_worker(state, codex_home, scan_dir, scan_id)pending, closed = drafts(scan_id)- result.write_text(json.dumps(closed))- os.utime(result, ns=(100, 100))+ write_result_draft(result, closed, mtime_ns=100)output = scan_dir if layout == "parent" else result.parent- original = write_checkpoint(output / "checkpoints", closed)- os.utime(original, ns=(100, 100))- select(output, original, 100)+ original = write_checkpoint(output / "checkpoints", closed, mtime_ns=100)+ select_checkpoint(output, original, mtime_ns=100)if explicit_recovery:fail_deep_scan(state, codex_home, scan_id, deep_status="failed")result.write_bytes(result.read_bytes())@@ -210,9 +208,8 @@ def test_head_capture_includes_checkpoint_published_after_enumeration(def publish_after_listing(root, relative):names = children(root, relative)if relative == directory and not published:- checkpoint = write_checkpoint(output / "checkpoints", pending)- os.utime(checkpoint, ns=(200, 200))- select(output, checkpoint, 200)+ checkpoint = write_checkpoint(output / "checkpoints", pending, mtime_ns=200)+ select_checkpoint(output, checkpoint, mtime_ns=200)published.append(checkpoint)return names@@ -262,10 +259,8 @@ def test_frozen_times_cover_sources_without_checkpoint_heads(output = tmp_path / "worker"output.mkdir()result = output / "result.json"- result.write_text(json.dumps(closed))- os.utime(result, ns=(100, 100))- checkpoint = write_checkpoint(output / "checkpoints", pending)- os.utime(checkpoint, ns=(200, 200))+ write_result_draft(result, closed, mtime_ns=100)+ checkpoint = write_checkpoint(output / "checkpoints", pending, mtime_ns=200)workers = [saved_discovery_worker(output, "worker", 1)]binding = saved_binding("deep_repository", repository="synthetic")@@ -351,12 +346,11 @@ def test_restored_head_uses_its_captured_observation(tmp_path: Path, head_time:]for checkpoint, observed in zip(checkpoints, (300, 400)):os.utime(checkpoint, ns=(50, 50))- select(output, checkpoint, observed)+ select_checkpoint(output, checkpoint, mtime_ns=observed)saved._capture_saved_source(tmp_path, "worker/checkpoint-head.json", scan_id)result = output / "result.json"- result.write_text(json.dumps(reported))- os.utime(result, ns=(200, 200))- select(output, checkpoints[0], head_time)+ write_result_draft(result, reported, mtime_ns=200)+ select_checkpoint(output, checkpoints[0], mtime_ns=head_time)original = {path: path.read_bytes() for path in (result, *checkpoints)}workers = [saved_discovery_worker(output)]binding = saved_binding("deep_repository", repository="synthetic")@@ -402,10 +396,9 @@ def test_first_stop_freezes_sources_before_report_preparation(}],)- result.write_text(json.dumps(reported))- os.utime(result, ns=(100, 100))+ write_result_draft(result, reported, mtime_ns=100)checkpoint = write_checkpoint(result.parent / "checkpoints", reported)- select(result.parent, checkpoint, 100)+ select_checkpoint(result.parent, checkpoint, mtime_ns=100)report = scan_dir / "report.md"report.mkdir()environment = {"CODEX_HOME": str(codex_home)}@@ -435,7 +428,7 @@ def test_first_stop_freezes_sources_before_report_preparation(complete=True,)latest = write_checkpoint(result.parent / "checkpoints", rejection)- select(result.parent, latest, 300)+ select_checkpoint(result.parent, latest, mtime_ns=300)preserved = preserve_scan_results(state, scan_id, "standard-worker-thread", environment=environment)["scan"]@@ -453,7 +446,7 @@ def test_invalid_explicit_recovery_does_not_replace_first_selection(tmp_path: Painitial = json.loads(result.read_text())os.utime(result, ns=(100, 100))checkpoint = write_checkpoint(result.parent / "checkpoints", initial)- select(result.parent, checkpoint, 100)+ select_checkpoint(result.parent, checkpoint, mtime_ns=100)fail_deep_scan(state, codex_home, scan_id, deep_status="failed")artifacts = {name: (scan_dir / name).read_bytes()@@ -469,7 +462,7 @@ def test_invalid_explicit_recovery_does_not_replace_first_selection(tmp_path: Pacheckpoint.write_text(json.dumps({**initial, "scanId": "other-scan"}))valid = {**initial, "complete": True, "threatModel": {"summary": "Completed review."}}later = write_checkpoint(result.parent / "checkpoints", valid)- select(result.parent, later, 200)+ select_checkpoint(result.parent, later, mtime_ns=200)environment = {"CODEX_HOME": str(codex_home)}failed = scan_command(state, "recover-scan-results", scan_id, environment=environment, check=Falsediff --git a/plugins/codex-security/tests/test_workbench_scan_history.py b/plugins/codex-security/tests/test_workbench_scan_history.pyindex 53a429f33..524d00eaa 100644--- a/plugins/codex-security/tests/test_workbench_scan_history.py+++ b/plugins/codex-security/tests/test_workbench_scan_history.py@@ -40,9 +40,7 @@ FINALIZER = SCRIPT.with_name("finalize_scan_contract.py")def test_rename_persists_without_changing_scan_results(tmp_path: Path, complete: bool, upgrade: bool) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)scan = create_cli_scan(state_dir, tmp_path / "scans", repository, complete=complete)before = run_workbench(state_dir, "get-scan", "--scan-id", scan["scanId"])["scan"]if upgrade:@@ -64,9 +62,7 @@ def test_rename_persists_without_changing_scan_results(def test_rename_rejects_blank_names(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)scan = create_cli_scan(state_dir, tmp_path / "scans", repository, complete=False)blank = run_workbench(@@ -97,9 +93,7 @@ def test_scan_history_search_casefolds_displayed_fields(tmp_path: Path) -> None:def test_rename_does_not_reorder_scan_history(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)older = create_cli_scan(state_dir, tmp_path / "scans", repository)newer = create_cli_scan(state_dir, tmp_path / "scans", repository)with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:@@ -124,6 +118,12 @@ def test_rename_does_not_reorder_scan_history(tmp_path: Path) -> None:assert after == [before[0], {**before[1], "name": "Release audit"}]+def empty_history_paths(tmp_path: Path) -> tuple[Path, Path]:+ repository = tmp_path / "repository"+ repository.mkdir()+ return tmp_path / "state", repository++def compare_scan_pair(state_dir: Path,before: dict[str, Any],@@ -270,9 +270,7 @@ def create_cli_scan(def test_get_scan_reads_pinned_coverage_independently_of_unrelated_artifacts(tmp_path: Path, artifact: str, mutation: str) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)scan = create_cli_scan(state_dir, tmp_path / "results", repository, finding=True)scan_dir = Path(scan["scanDir"])before = get_scan(state_dir, scan["scanId"])["scan"]@@ -297,9 +295,7 @@ def test_get_scan_reads_pinned_coverage_independently_of_unrelated_artifacts(def test_get_scan_reads_legacy_sealed_history_coverage_without_writes(tmp_path: Path, finding: bool, follow_up: bool) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)scan = create_cli_scan(state_dir, tmp_path / "results", repository, finding=finding, follow_up=follow_up)@@ -334,9 +330,7 @@ def test_get_scan_reads_legacy_sealed_history_coverage_without_writes(def test_get_scan_rejects_unverifiable_legacy_sealed_history_coverage(tmp_path: Path, corruption: str) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)scan = create_cli_scan(state_dir, tmp_path / "results", repository, finding=False)scan_dir = Path(scan["scanDir"])with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:@@ -428,9 +422,7 @@ def test_cli_scan_lifecycle_persists_recipes_lineage_and_filtered_history(tmp_padef test_cli_scan_persists_its_continuation_thread(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)scan = create_cli_scan(state_dir, tmp_path / "results", repository, complete=False)initial = get_scan(state_dir, scan["scanId"])["scan"]assert initial["threadIds"] == []@@ -586,9 +578,7 @@ def test_cli_scan_preserves_original_revision_when_head_moves(tmp_path: Path) ->@pytest.mark.parametrize("context_reporting", ["bounded", "unknown_upper", "legacy"])def test_cli_scan_history_persists_per_scan_cost(tmp_path: Path, context_reporting: str) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)cost = {"model": "gpt-5.6-sol","inputTokens": 1250,@@ -635,9 +625,7 @@ def test_cli_scan_history_persists_per_scan_cost(tmp_path: Path, context_reportidef test_cli_scan_completion_persists_authoritative_cost_after_plugin_completion(tmp_path: Path,) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)scan = create_cli_scan(state_dir, tmp_path / "results", repository)cost = {"model": "gpt-5.6-sol",@@ -674,9 +662,7 @@ def test_cli_scan_completion_persists_authoritative_cost_after_plugin_completiondef test_failed_cli_scan_history_persists_measured_cost(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)cost = {"model": "gpt-5.6-terra","inputTokens": 1250,@@ -702,9 +688,7 @@ def test_failed_cli_scan_history_persists_measured_cost(tmp_path: Path) -> None:def test_scan_failure_rejects_invalid_cost_without_stopping_the_scan(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)scan = create_cli_scan(state_dir, tmp_path / "results", repository, complete=False)rejected = fail_scan(@@ -717,9 +701,7 @@ def test_scan_failure_rejects_invalid_cost_without_stopping_the_scan(tmp_path: Pdef test_scan_completion_rejects_invalid_cost_without_overwriting_history(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)scan = create_cli_scan(state_dir, tmp_path / "results", repository)invalid = ["null",@@ -749,9 +731,7 @@ def test_scan_completion_rejects_invalid_cost_without_overwriting_history(tmp_padef test_scan_history_resolves_unique_prefixes_and_rejects_ambiguity(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)root = tmp_path / "results"scan = create_cli_scan(state_dir, root, repository)after = create_cli_scan(state_dir, root, repository)@@ -917,9 +897,7 @@ def test_scan_list_includes_related_git_worktrees_and_clones(tmp_path: Path) ->def test_cli_scan_comparison_tracks_stable_findings_without_copying_triage(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)root = tmp_path / "results"before = create_cli_scan(state_dir, root, repository)after = create_cli_scan(state_dir, root, repository, parent_scan_id=before["scanId"])@@ -1025,9 +1003,7 @@ def test_finding_occurrence_history_uses_start_instants(def test_scan_comparison_requires_saved_matches_and_remains_read_only(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)root = tmp_path / "results"before = create_cli_scan(state_dir, root, repository)after = create_cli_scan(state_dir, root, repository)@@ -1076,9 +1052,7 @@ def test_scan_comparison_requires_saved_matches_and_remains_read_only(tmp_path:def test_list_unmatched_scan_pairs_groups_pending_pairs_and_skips_saved_results(tmp_path: Path,) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)root = tmp_path / "results"first, second, third = (create_cli_scan(state_dir, root, repository) for _ in range(3))save_scan_matches(state_dir, first, second)@@ -1147,9 +1121,7 @@ def test_automatic_matching_orders_instants_and_preserves_directed_cache(def test_automatic_matching_keeps_unavailable_history_without_future_links(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)earlier, unavailable, current, future = (create_cli_scan(state_dir, tmp_path / "results", repository, identity_anchor=f"archive-check-{index}"@@ -1204,9 +1176,7 @@ def test_automatic_matching_keeps_unavailable_history_without_future_links(tmp_pdef test_list_unmatched_scan_pairs_skips_unavailable_scan_artifacts(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)root = tmp_path / "results"first, unavailable, last = (create_cli_scan(state_dir, root, repository) for _ in range(3))save_scan_matches(state_dir, first, last)@@ -1232,9 +1202,7 @@ def test_list_unmatched_scan_pairs_skips_unavailable_scan_artifacts(tmp_path: Padef test_semantic_scan_comparison_caches_matches_and_exposes_related_findings(tmp_path: Path,) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)root = tmp_path / "results"before = create_cli_scan(state_dir, root, repository)after = create_cli_scan(@@ -1408,9 +1376,7 @@ def test_explicit_comparison_uses_chronological_matching_cutoff(def test_semantic_scan_matching_backfills_legacy_finding_details(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)root = tmp_path / "results"before = create_cli_scan(state_dir, root, repository)after = create_cli_scan(state_dir, root, repository)@@ -1439,9 +1405,7 @@ def test_semantic_scan_matching_backfills_legacy_finding_details(tmp_path: Path)def test_semantic_scan_comparison_supports_one_to_many_without_copying_triage(tmp_path: Path,) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)root = tmp_path / "results"before = create_cli_scan(state_dir, root, repository)after = create_cli_scan(@@ -1498,9 +1462,7 @@ def test_semantic_scan_comparison_supports_one_to_many_without_copying_triage(def test_uncertain_semantic_scan_matches_stay_separate(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)root = tmp_path / "results"before = create_cli_scan(state_dir, root, repository)after = create_cli_scan(@@ -1536,9 +1498,7 @@ def test_uncertain_semantic_scan_matches_stay_separate(tmp_path: Path) -> None:def test_semantic_scan_comparison_replaces_cached_matches_atomically(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)root = tmp_path / "results"before = create_cli_scan(state_dir, root, repository)after = create_cli_scan(@@ -1676,9 +1636,7 @@ def test_semantic_scan_comparison_accepts_matching_git_origins(tmp_path: Path) -def test_semantic_scan_comparison_rejects_unknown_overlapping_and_uncertain_groups(tmp_path: Path,) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)root = tmp_path / "results"before = create_cli_scan(state_dir, root, repository)after = create_cli_scan(state_dir, root, repository)@@ -1748,9 +1706,7 @@ def test_cli_scan_comparison_distinguishes_absence_from_out_of_scope(tmp_path: Pdef test_cli_scan_reruns_reject_other_repository_parents(tmp_path: Path) -> None:- state_dir = tmp_path / "state"- repository = tmp_path / "repository"- repository.mkdir()+ state_dir, repository = empty_history_paths(tmp_path)parent = create_cli_scan(state_dir, tmp_path / "results", repository)other = tmp_path / "other"other.mkdir()diff --git a/plugins/codex-security/tests/test_workbench_scan_usage.py b/plugins/codex-security/tests/test_workbench_scan_usage.pyindex a707eef99..77fd112d4 100644--- a/plugins/codex-security/tests/test_workbench_scan_usage.py+++ b/plugins/codex-security/tests/test_workbench_scan_usage.py@@ -42,6 +42,23 @@ class ScanFixture:mode: str = "standard"diff_target: dict[str, Any] | None = None+ def write_parent_rollout(+ self, events: list[dict[str, Any]], *, originator: str | None = None+ ) -> Path:+ rollout = _rollout(self.target.parent, "scan-parent", events, originator=originator)+ _state_graph(self.environment, {"scan-parent": rollout}, [])+ return rollout++ def command(self, name: str, *arguments: str, check: bool = True) -> dict[str, Any]:+ return scan_command(+ self.state_dir,+ name,+ self.scan_id,+ *arguments,+ check=check,+ environment=self.environment,+ )+def _start_scan(tmp_path: Path, *, mode: str = "standard") -> ScanFixture:state_dir = tmp_path / "workbench-state"@@ -92,12 +109,11 @@ def _start_scan(tmp_path: Path, *, mode: str = "standard") -> ScanFixture:started = start_delivered_scan(state_dir,- "--workspace-id",workspace_id,"--scan-root",str(tmp_path / "scans"),environment=environment,- )["results"]+ )if mode == "deep":begin_deep_scan(state_dir,@@ -219,6 +235,24 @@ def _rollout(return rollout.resolve()+def _measure_rollout(+ scan_usage,+ path: Path,+ started_at: datetime,+ completed_at: datetime,+ *,+ thread_id: str = "scan-parent",+ parent_id: str | None = None,+ **options: str | None,+):+ return scan_usage._read_rollout_usage(+ scan_usage.RolloutSession(thread_id, parent_id, path),+ started_at=started_at,+ completed_at=completed_at,+ **options,+ )++def _allowance_event(timestamp: datetime, percent: int, limit: str = "codex") -> dict[str, Any]:return _event(timestamp,@@ -336,13 +370,7 @@ def _complete_scan(fixture: ScanFixture, *arguments: str) -> dict[str, Any]:fixture.target,**options,)- return scan_command(- fixture.state_dir,- "complete-scan",- fixture.scan_id,- *arguments,- environment=fixture.environment,- )+ return fixture.command("complete-scan", *arguments)@pytest.mark.parametrize(@@ -361,8 +389,7 @@ def test_completion_preserves_usage_record_warnings(event["payload"]["info"] = Noneelif info == "invalid":event["payload"]["info"] = {}- parent = _rollout(tmp_path, "scan-parent", [event, _token_event(counted, 20, 10)])- _state_graph(fixture.environment, {"scan-parent": parent}, [])+ fixture.write_parent_rollout([event, _token_event(counted, 20, 10)])usage = _complete_scan(fixture)["scan"]["usage"]@@ -377,9 +404,7 @@ def test_completion_counts_native_responses_and_compaction_without_legacy_duplicfixture = _start_scan(tmp_path)counted = fixture.started_at + timedelta(microseconds=1)compaction = _response_event(counted, "scan-parent", "compaction", 40, 3, turn_usage=(60, 5))- parent = _rollout(- tmp_path,- "scan-parent",+ fixture.write_parent_rollout([_token_event(fixture.started_at - timedelta(seconds=1), 100, 10),_token_event(counted, 150, 15),@@ -400,9 +425,8 @@ def test_completion_counts_native_responses_and_compaction_without_legacy_dupliccompaction,_response_event(counted, "scan-parent", "last", 30, 4, turn_usage=(90, 9)),_event(counted, "event_msg", {"type": "task_complete"}),- ],+ ])- _state_graph(fixture.environment, {"scan-parent": parent}, [])assert (_complete_scan(fixture)["scan"]["usage"].items()@@ -452,17 +476,16 @@ def test_legacy_compaction_usage_is_unverifiable_only_inside_scan_window(events.append(_event(completed, "event_msg", {"type": "task_complete"}))parent = _rollout(tmp_path, "scan-parent", events)- measurement = scan_usage._read_rollout_usage(- scan_usage.RolloutSession("scan-parent", None, parent),- started_at=started,- completed_at=completed,+ usage = _measure_rollout(+ scan_usage,+ parent,+ started,+ completed,completed_turn_id="turn-scan-parent" if scope == "completion_turn" else None,)- usage = measurement.tokens- warnings = measurement.warnings- assert usage == _counts(10, 0, 1)- assert warnings == ({"compaction_usage_unavailable"} if expected_warning else set())+ assert usage.tokens == _counts(10, 0, 1)+ assert usage.warnings == ({"compaction_usage_unavailable"} if expected_warning else set())def test_legacy_compaction_warning_survives_a_later_native_turn(scan_usage, tmp_path: Path) -> None:@@ -479,16 +502,10 @@ def test_legacy_compaction_warning_survives_a_later_native_turn(scan_usage, tmp_],)- measurement = scan_usage._read_rollout_usage(- scan_usage.RolloutSession("scan-parent", None, parent),- started_at=started,- completed_at=started,- )- usage = measurement.tokens- warnings = measurement.warnings+ usage = _measure_rollout(scan_usage, parent, started, started)- assert usage == _counts(30, 0, 3)- assert warnings == {"compaction_usage_unavailable"}+ assert usage.tokens == _counts(30, 0, 3)+ assert usage.warnings == {"compaction_usage_unavailable"}def test_native_usage_respects_scan_window_and_preserves_cache_details(@@ -524,16 +541,10 @@ def test_native_usage_respects_scan_window_and_preserves_cache_details(],)- measurement = scan_usage._read_rollout_usage(- scan_usage.RolloutSession("scan-parent", None, parent),- started_at=started,- completed_at=completed,- )- usage = measurement.tokens- warnings = measurement.warnings+ usage = _measure_rollout(scan_usage, parent, started, completed)- assert usage == _counts(20, 3, 2, cache_write_input_tokens=4)- assert warnings == set()+ assert usage.tokens == _counts(20, 3, 2, cache_write_input_tokens=4)+ assert usage.warnings == set()def test_native_worker_usage_excludes_inherited_totals_and_survives_resume(@@ -566,16 +577,12 @@ def test_native_worker_usage_excludes_inherited_totals_and_survives_resume(copied_events=[inherited, _token_event(started, 1_000, 100)],)- measurement = scan_usage._read_rollout_usage(- scan_usage.RolloutSession("scan-worker", "scan-parent", worker),- started_at=started,- completed_at=started,+ usage = _measure_rollout(+ scan_usage, worker, started, started, thread_id="scan-worker", parent_id="scan-parent")- usage = measurement.tokens- warnings = measurement.warnings- assert usage == _counts(60, 0, 6)- assert warnings == set()+ assert usage.tokens == _counts(60, 0, 6)+ assert usage.warnings == set()@pytest.mark.parametrize("gap", ["before", "after", "inside"])@@ -611,16 +618,10 @@ def test_native_usage_only_marks_receipt_gaps_inside_scan_window(],)- measurement = scan_usage._read_rollout_usage(- scan_usage.RolloutSession("scan-parent", None, parent),- started_at=started,- completed_at=started,- )- usage = measurement.tokens- warnings = measurement.warnings+ usage = _measure_rollout(scan_usage, parent, started, started)- assert usage == _counts(30, 0, 0)- assert warnings == ({"token_record_incomplete"} if gap == "inside" else set())+ assert usage.tokens == _counts(30, 0, 0)+ assert usage.warnings == ({"token_record_incomplete"} if gap == "inside" else set())def test_native_usage_continues_cumulative_baseline_when_recovering_same_turn(@@ -645,16 +646,10 @@ def test_native_usage_continues_cumulative_baseline_when_recovering_same_turn(],)- measurement = scan_usage._read_rollout_usage(- scan_usage.RolloutSession("scan-parent", None, parent),- started_at=started,- completed_at=started,- )- usage = measurement.tokens- warnings = measurement.warnings+ usage = _measure_rollout(scan_usage, parent, started, started)- assert usage == _counts(30, 0, 3)- assert warnings == set()+ assert usage.tokens == _counts(30, 0, 3)+ assert usage.warnings == set()@pytest.mark.parametrize(@@ -694,16 +689,12 @@ def test_native_usage_preserves_partial_measurement_warnings(if failure == "truncated":parent.write_bytes(parent.read_bytes().removesuffix(b"\n"))- measurement = scan_usage._read_rollout_usage(- scan_usage.RolloutSession("scan-parent", None, parent),- started_at=started,- completed_at=started,- )- usage = measurement.tokens- warnings = measurement.warnings+ usage = _measure_rollout(scan_usage, parent, started, started)- assert usage == (_counts(20, 0, 2) if failure == "missing_receipt" else _counts(10, 0, 1))- assert warnings == {+ assert usage.tokens == (+ _counts(20, 0, 2) if failure == "missing_receipt" else _counts(10, 0, 1)+ )+ assert usage.warnings == {"token_record_incomplete"if failure == "missing_receipt"else "rollout_record_incomplete"@@ -912,8 +903,7 @@ def test_completion_includes_cached_and_cache_write_tokens(reported = event["payload"]["info"]["total_token_usage"]reported.pop("cache_write_input_tokens")reported[cache_write_field] = 12- parent = _rollout(tmp_path, "scan-parent", [event])- _state_graph(fixture.environment, {"scan-parent": parent}, [])+ fixture.write_parent_rollout([event])usage = _complete_scan(fixture)["scan"]["usage"]@@ -1112,11 +1102,7 @@ def test_completion_distinguishes_missing_usage_from_observed_zero(_response_event(counted, "scan-parent", "zero-response", 0, 0),_event(counted, "event_msg", {"type": "task_complete", "turn_id": "turn-scan-parent"}),]- _state_graph(- fixture.environment,- {"scan-parent": _rollout(tmp_path, "scan-parent", events)},- [],- )+ fixture.write_parent_rollout(events)usage = _complete_scan(fixture)["scan"]["usage"]@@ -1330,22 +1316,12 @@ def test_completion_preserves_explicit_legacy_cost() -> None:fixture = _start_scan(tmp_path)counted = fixture.started_at + timedelta(microseconds=1)- _state_graph(- fixture.environment,- {- "scan-parent": _rollout(- tmp_path,- "scan-parent",- [- _token_event(fixture.started_at - timedelta(seconds=1), 100, 0),- _token_event(- counted, 115, 6, cached_input_tokens=100 if delayed_cache_counts else 0- ),- _allowance_event(counted, 6),- ],- )- },- [],+ fixture.write_parent_rollout(+ [+ _token_event(fixture.started_at - timedelta(seconds=1), 100, 0),+ _token_event(counted, 115, 6, cached_input_tokens=100 if delayed_cache_counts else 0),+ _allowance_event(counted, 6),+ ])cost = {"model": "gpt-5.6-sol",@@ -1358,14 +1334,7 @@ def test_completion_preserves_explicit_legacy_cost(write_completed_contract(fixture.scan_dir, fixture.scan_id, fixture.target, relative_path="app.py")- completed = scan_command(- fixture.state_dir,- "complete-scan",- fixture.scan_id,- "--cost-json",- json.dumps(cost),- environment=fixture.environment,- )["scan"]+ completed = fixture.command("complete-scan", "--cost-json", json.dumps(cost))["scan"]assert completed["cost"] == costassert completed["usage"]["accountRateLimits"][0]["primary"]["usedPercent"] == 6if delayed_cache_counts:@@ -1444,9 +1413,7 @@ def test_completion_persists_direct_control_zero_without_hiding_missing_workers("threadCount": 1 if measured_worker else 0,}assert "cost" not in completed- repeated = scan_command(- fixture.state_dir, "complete-scan", fixture.scan_id, environment=fixture.environment- )["scan"]+ repeated = fixture.command("complete-scan")["scan"]assert repeated["usage"] == usageassert "cost" not in repeatedwith sqlite3.connect(fixture.state_dir / "workbench.sqlite3") as connection:@@ -1516,11 +1483,7 @@ def test_metadata_only_deep_owner_requires_direct_control_originator(tmp_path: Path, originator: str | None) -> None:fixture = _start_scan(tmp_path, mode="deep")- _state_graph(- fixture.environment,- {"scan-parent": _rollout(tmp_path, "scan-parent", [], originator=originator)},- [],- )+ fixture.write_parent_rollout([], originator=originator)assert _complete_scan(fixture)["scan"]["usage"] == {"coverage": "unavailable",@@ -1599,32 +1562,18 @@ def test_host_finalizes_exact_completion_turn_without_changing_artifacts()with parent.open("a", encoding="utf-8") as output:output.writelines(f"{json.dumps(event)}\n" for event in final_events)- mismatched = scan_command(- fixture.state_dir,+ mismatched = fixture.command("finalize-scan-usage",- fixture.scan_id,"--thread-id","scan-parent","--turn-id","another-turn",- environment=fixture.environment,check=False,)assert mismatched["returncode"] != 0- assert (- scan_command(- fixture.state_dir, "get-scan", fixture.scan_id, environment=fixture.environment- )["scan"]["usage"]- == initial["usage"]- )+ assert fixture.command("get-scan")["scan"]["usage"] == initial["usage"]arguments = ("--thread-id", "scan-parent", "--turn-id", turn_id)- finalized = scan_command(- fixture.state_dir,- "finalize-scan-usage",- fixture.scan_id,- *arguments,- environment=fixture.environment,- )["scan"]+ finalized = fixture.command("finalize-scan-usage", *arguments)["scan"]assert (finalized["usage"].items()>= {@@ -1646,26 +1595,16 @@ def test_host_finalizes_exact_completion_turn_without_changing_artifacts(]with parent.open("a", encoding="utf-8") as output:output.writelines(f"{json.dumps(event)}\n" for event in later_events)- measurement = scan_usage._read_rollout_usage(- scan_usage.RolloutSession("scan-parent", None, parent),- started_at=fixture.started_at,- completed_at=datetime.fromisoformat(stopped_at.replace("Z", "+00:00")),+ usage = _measure_rollout(+ scan_usage,+ parent,+ fixture.started_at,+ datetime.fromisoformat(stopped_at.replace("Z", "+00:00")),completed_turn_id=turn_id,)- measured = measurement.tokens- warnings = measurement.warnings- assert measured == _counts(90, 0, 11)- assert warnings == set()- assert (- scan_command(- fixture.state_dir,- "finalize-scan-usage",- fixture.scan_id,- *arguments,- environment=fixture.environment,- )["scan"]["usage"]- == finalized["usage"]- )+ assert usage.tokens == _counts(90, 0, 11)+ assert usage.warnings == set()+ assert fixture.command("finalize-scan-usage", *arguments)["scan"]["usage"] == finalized["usage"]assert {name: (fixture.scan_dir / name).read_bytes() for name in sealed_files} == sealed_files@@ -1689,17 +1628,14 @@ def test_host_final_usage_preserves_missing_worker_warning([("scan-parent", "scan-worker")],)_complete_scan(fixture, "--thread-id", "scan-parent", "--turn-id", turn_id)- usage = scan_command(- fixture.state_dir,+ usage = fixture.command("finalize-scan-usage",- fixture.scan_id,"--thread-id","scan-parent","--turn-id",turn_id,"--turn-status",turn_status,- environment=fixture.environment,)["scan"]["usage"]assert (usage.items()@@ -1722,12 +1658,7 @@ def test_host_final_usage_retries_a_locked_native_database_without_losing_counts) -> None:fixture = _start_scan(tmp_path)counted = fixture.started_at + timedelta(microseconds=1)- parent = _rollout(- tmp_path,- "scan-parent",- [_response_event(counted, "scan-parent", "first", 10, 2)],- )- _state_graph(fixture.environment, {"scan-parent": parent}, [])+ parent = fixture.write_parent_rollout([_response_event(counted, "scan-parent", "first", 10, 2)])arguments = ("--thread-id", "scan-parent", "--turn-id", "turn-scan-parent")initial = _complete_scan(fixture, *arguments)["scan"]["usage"]with parent.open("a", encoding="utf-8") as output:@@ -1737,24 +1668,12 @@ def test_host_final_usage_retries_a_locked_native_database_without_losing_countsPath(fixture.environment["CODEX_SQLITE_HOME"]) / "state_5.sqlite") as native_database:native_database.execute("BEGIN EXCLUSIVE")- pending = scan_command(- fixture.state_dir,- "finalize-scan-usage",- fixture.scan_id,- *arguments,- environment=fixture.environment,- )["scan"]["usage"]+ pending = fixture.command("finalize-scan-usage", *arguments)["scan"]["usage"]assert pending == initialassert pending["totalTokens"] == 12assert pending["completion"]["finalized"] is False- finalized = scan_command(- fixture.state_dir,- "finalize-scan-usage",- fixture.scan_id,- *arguments,- environment=fixture.environment,- )["scan"]["usage"]+ finalized = fixture.command("finalize-scan-usage", *arguments)["scan"]["usage"]assert (finalized.items()>= {@@ -1773,34 +1692,11 @@ def test_host_final_usage_recovers_initially_unavailable_records(tmp_path: Path)initial = _complete_scan(fixture, *arguments)["scan"]["usage"]assert initial["coverage"] == "unavailable"assert initial["completion"]["finalized"] is False- assert (- scan_command(- fixture.state_dir,- "finalize-scan-usage",- fixture.scan_id,- *arguments,- environment=fixture.environment,- )["scan"]["usage"]- == initial+ assert fixture.command("finalize-scan-usage", *arguments)["scan"]["usage"] == initial+ fixture.write_parent_rollout(+ [_response_event(fixture.started_at, "scan-parent", "final-response", 10, 2)])- _state_graph(- fixture.environment,- {- "scan-parent": _rollout(- tmp_path,- "scan-parent",- [_response_event(fixture.started_at, "scan-parent", "final-response", 10, 2)],- )- },- [],- )- finalized = scan_command(- fixture.state_dir,- "finalize-scan-usage",- fixture.scan_id,- *arguments,- environment=fixture.environment,- )["scan"]["usage"]+ finalized = fixture.command("finalize-scan-usage", *arguments)["scan"]["usage"]assert finalized["coverage"] == "complete"assert finalized["totalTokens"] == 12assert finalized["completion"]["finalized"] is True@@ -1809,28 +1705,14 @@ def test_host_final_usage_recovers_initially_unavailable_records(tmp_path: Path)def test_host_final_usage_does_not_invent_an_unrecorded_completion_turn(tmp_path: Path) -> None:fixture = _start_scan(tmp_path)arguments = ("--thread-id", "scan-parent", "--turn-id", "unrecorded-turn")- _state_graph(- fixture.environment,- {- "scan-parent": _rollout(- tmp_path,- "scan-parent",- [- _response_event(fixture.started_at, "scan-parent", "earlier-response", 10, 2),- _event(fixture.started_at, "event_msg", {"type": "task_complete"}),- ],- )- },- [],+ fixture.write_parent_rollout(+ [+ _response_event(fixture.started_at, "scan-parent", "earlier-response", 10, 2),+ _event(fixture.started_at, "event_msg", {"type": "task_complete"}),+ ])_complete_scan(fixture, *arguments)- finalized = scan_command(- fixture.state_dir,- "finalize-scan-usage",- fixture.scan_id,- *arguments,- environment=fixture.environment,- )["scan"]["usage"]+ finalized = fixture.command("finalize-scan-usage", *arguments)["scan"]["usage"]assert finalized["coverage"] == "partial"assert finalized["totalTokens"] == 12assert finalized["warnings"] == ["completion_turn_unavailable"]@@ -1840,11 +1722,7 @@ def test_host_final_usage_does_not_invent_an_unrecorded_completion_turn(tmp_pathdef test_failed_scan_preserves_legacy_failure_behavior(tmp_path: Path) -> None:fixture = _start_scan(tmp_path)counted = fixture.started_at + timedelta(microseconds=1)- _state_graph(- fixture.environment,- {"scan-parent": _rollout(tmp_path, "scan-parent", [_token_event(counted, 21, 8)])},- [],- )+ fixture.write_parent_rollout([_token_event(counted, 21, 8)])failed = fail_scan(fixture.state_dir, fixture.scan_id, "Fixture failure.", environment=fixture.environment)["scan"]@@ -2115,12 +1993,7 @@ def test_completion_uses_latest_account_snapshot_across_owned_sessions("secondary": None,}assert limits["another-limit"]["primary"]["usedPercent"] == 2- assert (- scan_command(- fixture.state_dir, "get-scan", fixture.scan_id, environment=fixture.environment- )["scan"]["usage"]- == usage- )+ assert fixture.command("get-scan")["scan"]["usage"] == usage@pytest.mark.parametrize("terminal_status", ["complete", "failed"])@@ -2160,9 +2033,7 @@ def test_large_usage_persists_with_terminal_scan_state(assert terminal["progress"]["status"] == terminal_statusassert terminal["usage"] == usage- stored = scan_command(- fixture.state_dir, "get-scan", fixture.scan_id, environment=fixture.environment- )["scan"]+ stored = fixture.command("get-scan")["scan"]assert stored["progress"]["status"] == terminal_statusassert stored["usage"] == usage@@ -2171,15 +2042,12 @@ def test_completion_collects_large_usage_from_owned_rollouts(tmp_path: Path) ->fixture = _start_scan(tmp_path)counted = fixture.started_at + timedelta(microseconds=1)limit_ids = {f"fixture-limit-{index}" for index in range(60)}- parent = _rollout(- tmp_path,- "scan-parent",+ fixture.write_parent_rollout([_token_event(counted, 120, 15, cached_input_tokens=30),*[_allowance_event(counted, 6, limit_id) for limit_id in sorted(limit_ids)],- ],+ ])- _state_graph(fixture.environment, {"scan-parent": parent}, [])completed = _complete_scan(fixture)["scan"]@@ -2190,53 +2058,26 @@ def test_completion_collects_large_usage_from_owned_rollouts(tmp_path: Path) ->assert usage["threadCount"] == 1assert usage.items() >= _counts(120, 30, 15).items()assert {limit["limitId"] for limit in usage["accountRateLimits"]} == limit_ids- assert (- scan_command(- fixture.state_dir, "get-scan", fixture.scan_id, environment=fixture.environment- )["scan"]["usage"]- == usage- )+ assert fixture.command("get-scan")["scan"]["usage"] == usagedef test_host_final_usage_saves_allowances_before_token_receipts(tmp_path: Path) -> None:fixture = _start_scan(tmp_path)arguments = ("--thread-id", "scan-parent", "--turn-id", "turn-scan-parent")initial = _complete_scan(fixture, *arguments)["scan"]["usage"]- parent = _rollout(- tmp_path,- "scan-parent",- [_allowance_event(fixture.started_at, 6)],- )- _state_graph(fixture.environment, {"scan-parent": parent}, [])+ parent = fixture.write_parent_rollout([_allowance_event(fixture.started_at, 6)])- pending = scan_command(- fixture.state_dir,- "finalize-scan-usage",- fixture.scan_id,- *arguments,- environment=fixture.environment,- )["scan"]["usage"]+ pending = fixture.command("finalize-scan-usage", *arguments)["scan"]["usage"]assert pending["coverage"] == "unavailable"assert pending["completion"] == initial["completion"]assert not set(_counts(0, 0, 0)).intersection(pending)assert pending["accountRateLimits"][0]["primary"]["usedPercent"] == 6- assert (- scan_command(- fixture.state_dir, "get-scan", fixture.scan_id, environment=fixture.environment- )["scan"]["usage"]- == pending- )+ assert fixture.command("get-scan")["scan"]["usage"] == pendingwith parent.open("a", encoding="utf-8") as output:receipt = _response_event(fixture.started_at, "scan-parent", "final-response", 10, 2)output.write(f"{json.dumps(receipt)}\n")- finalized = scan_command(- fixture.state_dir,- "finalize-scan-usage",- fixture.scan_id,- *arguments,- environment=fixture.environment,- )["scan"]["usage"]+ finalized = fixture.command("finalize-scan-usage", *arguments)["scan"]["usage"]assert finalized["totalTokens"] == 12assert finalized["completion"]["finalized"] is Trueassert finalized["accountRateLimits"] == pending["accountRateLimits"]@@ -2253,14 +2094,8 @@ def test_completion_validates_account_snapshots_without_token_usage("threadCount": 0,"accountRateLimits": limits,}- rejected = scan_command(- fixture.state_dir,- "complete-scan",- fixture.scan_id,- "--cost-json",- json.dumps({"usage": usage}),- environment=fixture.environment,- check=False,+ rejected = fixture.command(+ "complete-scan", "--cost-json", json.dumps({"usage": usage}), check=False)assert rejected["returncode"] != 0assert "invalid measured token usage" in rejected["stderr"]@@ -2305,16 +2140,13 @@ def test_completion_marks_in_flight_usage_partial_even_with_earlier_receipts() -> None:fixture = _start_scan(tmp_path)counted = fixture.started_at + timedelta(microseconds=1)- parent = _rollout(- tmp_path,- "scan-parent",+ fixture.write_parent_rollout([_event(counted, "event_msg", {"type": "task_started", "turn_id": "scan-turn"}),_token_event(counted, 12, 3),_event(counted + timedelta(days=1), "event_msg", {"type": "task_complete"}),- ],+ ])- _state_graph(fixture.environment, {"scan-parent": parent}, [])usage = _complete_scan(fixture)["scan"]["usage"]assert usage["coverage"] == "partial"assert usage["totalTokens"] == 15@@ -2395,14 +2227,13 @@ def test_closed_turn_coverage_survives_later_success(rollout = _rollout(tmp_path, thread_id, events, parent_thread_id=parent_id, include_task_start=False)- measurement = scan_usage._read_rollout_usage(- scan_usage.RolloutSession(thread_id, parent_id, rollout),- started_at=started,- completed_at=started,+ usage = _measure_rollout(+ scan_usage, rollout, started, started, thread_id=thread_id, parent_id=parent_id)- usage, warnings = measurement.tokens, measurement.warnings- assert usage == (_counts(30, 0, 5) if receipt in {"native", "legacy"} else _counts(10, 0, 2))- assert warnings == ({"token_record_missing"} if receipt == "none" else set()) | (+ assert usage.tokens == (+ _counts(30, 0, 5) if receipt in {"native", "legacy"} else _counts(10, 0, 2)+ )+ assert usage.warnings == ({"token_record_missing"} if receipt == "none" else set()) | ({"turn_interrupted"} if terminal == "turn_aborted" else set())@@ -2429,14 +2260,9 @@ def test_closed_turn_coverage_retains_before_window_receipt_evidence(])rollout = _rollout(tmp_path, "scan-parent", events)- measurement = scan_usage._read_rollout_usage(- scan_usage.RolloutSession("scan-parent", None, rollout),- started_at=started,- completed_at=started,- )- usage, warnings = measurement.tokens, measurement.warnings- assert usage == _counts(10, 0, 2)- assert warnings == (set() if receipt else {"token_record_missing"})+ usage = _measure_rollout(scan_usage, rollout, started, started)+ assert usage.tokens == _counts(10, 0, 2)+ assert usage.warnings == (set() if receipt else {"token_record_missing"})def test_closed_turn_coverage_accepts_delayed_receipt_for_same_turn(@@ -2452,14 +2278,9 @@ def test_closed_turn_coverage_accepts_delayed_receipt_for_same_turn(_response_event(started, "scan-parent", "late", 10, 2, turn_id="first"),],)- measurement = scan_usage._read_rollout_usage(- scan_usage.RolloutSession("scan-parent", None, rollout),- started_at=started,- completed_at=started,- )- usage, warnings = measurement.tokens, measurement.warnings- assert usage == _counts(10, 0, 2)- assert warnings == set()+ usage = _measure_rollout(scan_usage, rollout, started, started)+ assert usage.tokens == _counts(10, 0, 2)+ assert usage.warnings == set()def test_closed_turn_coverage_preserves_interruption_after_same_turn_resumes(@@ -2478,14 +2299,9 @@ def test_closed_turn_coverage_preserves_interruption_after_same_turn_resumes(_event(started, "event_msg", {"type": "task_complete", "turn_id": "same-turn"}),],)- measurement = scan_usage._read_rollout_usage(- scan_usage.RolloutSession("scan-parent", None, rollout),- started_at=started,- completed_at=started,- )- usage, warnings = measurement.tokens, measurement.warnings- assert usage == _counts(10, 0, 2)- assert warnings == {"turn_interrupted"}+ usage = _measure_rollout(scan_usage, rollout, started, started)+ assert usage.tokens == _counts(10, 0, 2)+ assert usage.warnings == {"turn_interrupted"}def test_closed_turn_coverage_ignores_inherited_and_outside_window_interruptions(@@ -2509,31 +2325,25 @@ def test_closed_turn_coverage_ignores_inherited_and_outside_window_interruptions_event(before, "event_msg", {"type": "turn_aborted", "turn_id": "inherited"}),],)- measurement = scan_usage._read_rollout_usage(- scan_usage.RolloutSession("worker", "scan-parent", rollout),- started_at=started,- completed_at=started,+ usage = _measure_rollout(+ scan_usage, rollout, started, started, thread_id="worker", parent_id="scan-parent")- usage, warnings = measurement.tokens, measurement.warnings- assert usage == _counts(10, 0, 2)- assert warnings == set()+ assert usage.tokens == _counts(10, 0, 2)+ assert usage.warnings == set()def test_completion_marks_closed_turn_without_receipt_partial(tmp_path: Path) -> None:fixture = _start_scan(tmp_path)counted = fixture.started_at + timedelta(microseconds=1)- parent = _rollout(- tmp_path,- "scan-parent",+ fixture.write_parent_rollout([_event(counted, "event_msg", {"type": "task_started", "turn_id": "empty"}),_event(counted, "event_msg", {"type": "task_complete", "turn_id": "empty"}),_event(counted, "event_msg", {"type": "task_started", "turn_id": "measured"}),_response_event(counted, "scan-parent", "response", 10, 2, turn_id="measured"),_event(counted, "event_msg", {"type": "task_complete", "turn_id": "measured"}),- ],+ ])- _state_graph(fixture.environment, {"scan-parent": parent}, [])assert (_complete_scan(fixture)["scan"]["usage"].items()>= {diff --git a/plugins/codex-security/tests/test_workbench_scan_validation.py b/plugins/codex-security/tests/test_workbench_scan_validation.pyindex f26858fdf..8c819f907 100644--- a/plugins/codex-security/tests/test_workbench_scan_validation.py+++ b/plugins/codex-security/tests/test_workbench_scan_validation.py@@ -7,15 +7,23 @@ from contextlib import closingfrom pathlib import Pathimport pytest-from test_workbench_scan_history import create_cli_scan, run_workbench-from workbench_test_support import initialize_git_repository+from test_workbench_scan_history import create_cli_scan+from workbench_test_support import get_scan, initialize_git_repository+++def start_validation_scan(+ tmp_path: Path, repository: Path, *, target: dict[str, object] | None = None+) -> tuple[Path, str]:+ state_dir = tmp_path / "state"+ results = tmp_path / "results"+ results.mkdir(mode=0o700)+ scan = create_cli_scan(state_dir, results, repository, complete=False, target=target)+ return state_dir, scan["scanId"]@pytest.mark.parametrize("kind", ["directory", "git", "dirty", "working_tree", "refs"])def test_check_scan_target_matches_recorded_contents(tmp_path: Path, kind: str) -> None:repository = tmp_path / "repository"- state_dir = tmp_path / "state"- (tmp_path / "results").mkdir(mode=0o700)if kind == "directory":repository.mkdir()(repository / "README.md").write_text("fixture\n")@@ -28,35 +36,30 @@ def test_check_scan_target_matches_recorded_contents(tmp_path: Path, kind: str)source.write_text("committed changes\n")subprocess.run(["git", "commit", "-qam", "Scanned revision"], cwd=repository, check=True)original = source.read_bytes()- scan = create_cli_scan(- state_dir,- tmp_path / "results",+ state_dir, scan_id = start_validation_scan(+ tmp_path,repository,- complete=False,- target=(- {- "kind": kind,- "paths": [],- "base": "HEAD^" if kind == "refs" else "HEAD",- "head": "HEAD",- }- if kind in {"working_tree", "refs"}- else None- ),+ target={+ "kind": kind,+ "paths": [],+ "base": "HEAD^" if kind == "refs" else "HEAD",+ "head": "HEAD",+ }+ if kind in {"working_tree", "refs"}+ else None,)- command = ("get-scan", "--scan-id", scan["scanId"])- checked = run_workbench(state_dir, *command, "--check-target")+ checked = get_scan(state_dir, scan_id, "--check-target")assert checked["scan"]["targetPath"] == str(repository.resolve())source.write_text("changed after scan\n")# Ordinary history remains readable after the checkout changes.- assert run_workbench(state_dir, *command)["scan"]["scanId"] == scan["scanId"]- changed = run_workbench(state_dir, *command, "--check-target", check=False)+ assert get_scan(state_dir, scan_id)["scan"]["scanId"] == scan_id+ changed = get_scan(state_dir, scan_id, "--check-target", check=False)assert changed["returncode"] != 0assert "Scan target contents changed" in changed["stderr"]source.write_bytes(original)- assert run_workbench(state_dir, *command, "--check-target")["scan"]["scanId"] == scan["scanId"]+ assert get_scan(state_dir, scan_id, "--check-target")["scan"]["scanId"] == scan_id@pytest.mark.parametrize("kind", ["repository", "refs"])@@ -65,33 +68,23 @@ def test_check_scan_target_rejects_changed_revision() -> None:repository = tmp_path / "repository"initialize_git_repository(repository)- state_dir = tmp_path / "state"- (tmp_path / "results").mkdir(mode=0o700)- scan = create_cli_scan(- state_dir,- tmp_path / "results",+ state_dir, scan_id = start_validation_scan(+ tmp_path,repository,- complete=False,- target=(- {"kind": "refs", "paths": [], "base": "HEAD", "head": "HEAD"}- if kind == "refs"- else None- ),+ target={"kind": "refs", "paths": [], "base": "HEAD", "head": "HEAD"}+ if kind == "refs"+ else None,)subprocess.run(["git", "commit", "--allow-empty", "-qm", "New revision"], cwd=repository, check=True)- changed = run_workbench(- state_dir, "get-scan", "--scan-id", scan["scanId"], "--check-target", check=False- )+ changed = get_scan(state_dir, scan_id, "--check-target", check=False)assert changed["returncode"] != 0assert "Repository HEAD changed" in changed["stderr"]assert "new scan" in changed["stderr"].lower()with closing(sqlite3.connect(state_dir / "workbench.sqlite3")) as connection:connection.row_factory = sqlite3.Row- stored_scan = connection.execute(- "SELECT * FROM scans WHERE id = ?", (scan["scanId"],)- ).fetchone()+ stored_scan = connection.execute("SELECT * FROM scans WHERE id = ?", (scan_id,)).fetchone()with pytest.raises(SystemExit, match="Regenerate the remediation patch"):workbench_api["remediation_checkout_snapshot"](stored_scan)@@ -104,17 +97,10 @@ def test_check_committed_diff_target_rejects_sparse_checkout_after_scan(tmp_pathsource.write_text("pass\n")subprocess.run(["git", "add", "src/source.py"], cwd=repository, check=True)subprocess.run(["git", "commit", "-qm", "Add source"], cwd=repository, check=True)- state_dir = tmp_path / "state"- (tmp_path / "results").mkdir(mode=0o700)- scan = create_cli_scan(- state_dir,- tmp_path / "results",- repository,- complete=False,- target={"kind": "refs", "paths": [], "base": "HEAD^", "head": "HEAD"},+ state_dir, scan_id = start_validation_scan(+ tmp_path, repository, target={"kind": "refs", "paths": [], "base": "HEAD^", "head": "HEAD"})- command = ("get-scan", "--scan-id", scan["scanId"])- assert run_workbench(state_dir, *command, "--check-target")["scan"]["scanId"] == scan["scanId"]+ assert get_scan(state_dir, scan_id, "--check-target")["scan"]["scanId"] == scan_idsubprocess.run(["git", "sparse-checkout", "set", "--no-cone", "/README.md"],@@ -123,30 +109,26 @@ def test_check_committed_diff_target_rejects_sparse_checkout_after_scan(tmp_path)assert not source.exists()subprocess.run(["git", "diff", "--quiet", "HEAD"], cwd=repository, check=True)- assert run_workbench(state_dir, *command)["scan"]["scanId"] == scan["scanId"]- changed = run_workbench(state_dir, *command, "--check-target", check=False)+ assert get_scan(state_dir, scan_id)["scan"]["scanId"] == scan_id+ changed = get_scan(state_dir, scan_id, "--check-target", check=False)assert changed["returncode"] != 0assert "full repository checkout" in changed["stderr"]subprocess.run(["git", "sparse-checkout", "disable"], cwd=repository, check=True)assert source.read_text() == "pass\n"- assert run_workbench(state_dir, *command, "--check-target")["scan"]["scanId"] == scan["scanId"]+ assert get_scan(state_dir, scan_id, "--check-target")["scan"]["scanId"] == scan_iddef test_check_scan_target_rejects_replaced_checkout(tmp_path: Path) -> None:repository = tmp_path / "repository"repository.mkdir()(repository / "source.py").write_text("pass\n")- state_dir = tmp_path / "state"- (tmp_path / "results").mkdir(mode=0o700)- scan = create_cli_scan(state_dir, tmp_path / "results", repository, complete=False)+ state_dir, scan_id = start_validation_scan(tmp_path, repository)repository.rename(tmp_path / "original")repository.mkdir()(repository / "source.py").write_text("pass\n")- changed = run_workbench(- state_dir, "get-scan", "--scan-id", scan["scanId"], "--check-target", check=False- )+ changed = get_scan(state_dir, scan_id, "--check-target", check=False)assert changed["returncode"] != 0assert "checkout path was replaced" in changed["stderr"]@@ -162,28 +144,25 @@ def test_check_scan_target_accepts_legacy_revision_only_git_scan(tmp_path: Path)check=True,)subprocess.run(["git", "commit", "-qm", "Add submodule"], cwd=repository, check=True)- state_dir = tmp_path / "state"- (tmp_path / "results").mkdir(mode=0o700)- scan = create_cli_scan(state_dir, tmp_path / "results", repository, complete=False)+ state_dir, scan_id = start_validation_scan(tmp_path, repository)with closing(sqlite3.connect(state_dir / "workbench.sqlite3")) as connection:connection.execute(- "UPDATE scans SET target_snapshot_digest = NULL WHERE id = ?", (scan["scanId"],)+ "UPDATE scans SET target_snapshot_digest = NULL WHERE id = ?", (scan_id,))connection.commit()- command = ("get-scan", "--scan-id", scan["scanId"], "--check-target")- assert run_workbench(state_dir, *command)["scan"]["scanId"] == scan["scanId"]+ assert get_scan(state_dir, scan_id, "--check-target")["scan"]["scanId"] == scan_id# Revision-only records cannot compare content against an unrecorded digest.source = repository / "README.md"original = source.read_bytes()source.write_text("Changed working-tree content.\n")- assert run_workbench(state_dir, *command)["scan"]["scanId"] == scan["scanId"]+ assert get_scan(state_dir, scan_id, "--check-target")["scan"]["scanId"] == scan_idsource.write_bytes(original)submodule_source = submodule / "README.md"submodule_original = submodule_source.read_bytes()submodule_source.write_text("Changed submodule content.\n")- dirty_submodule = run_workbench(state_dir, *command, check=False)+ dirty_submodule = get_scan(state_dir, scan_id, "--check-target", check=False)assert dirty_submodule["returncode"] != 0assert "Dirty Git submodules" in dirty_submodule["stderr"]submodule_source.write_bytes(submodule_original)@@ -191,14 +170,14 @@ def test_check_scan_target_accepts_legacy_revision_only_git_scan(tmp_path: Path)subprocess.run(["git", "commit", "--allow-empty", "-qm", "New revision"], cwd=repository, check=True)- changed = run_workbench(state_dir, *command, check=False)+ changed = get_scan(state_dir, scan_id, "--check-target", check=False)assert changed["returncode"] != 0assert "Repository HEAD changed" in changed["stderr"]repository.rename(tmp_path / "original")repository.mkdir()source.write_bytes(original)- replaced = run_workbench(state_dir, *command, check=False)+ replaced = get_scan(state_dir, scan_id, "--check-target", check=False)assert replaced["returncode"] != 0assert "checkout path was replaced" in replaced["stderr"]@@ -212,32 +191,27 @@ def test_check_legacy_scan_target_does_not_read_unrecorded_contents() -> None:repository = tmp_path / "repository"initialize_git_repository(repository)- state_dir = tmp_path / "state"- (tmp_path / "results").mkdir(mode=0o700)- scan = create_cli_scan(state_dir, tmp_path / "results", repository, complete=False)- command = ("get-scan", "--scan-id", scan["scanId"])+ state_dir, scan_id = start_validation_scan(tmp_path, repository)entry = repository / "unreadable.txt"entry.write_text("Synthetic untracked contents.\n")mode = entry.stat().st_modeentry.chmod(0)try:- assert run_workbench(state_dir, *command)["scan"]["scanId"] == scan["scanId"]- recorded = run_workbench(state_dir, *command, "--check-target", check=False)+ assert get_scan(state_dir, scan_id)["scan"]["scanId"] == scan_id+ recorded = get_scan(state_dir, scan_id, "--check-target", check=False)assert recorded["returncode"] != 0assert "Could not read untracked file" in recorded["stderr"]with closing(sqlite3.connect(state_dir / "workbench.sqlite3")) as connection:connection.execute(- "UPDATE scans SET target_snapshot_digest = NULL WHERE id = ?", (scan["scanId"],)+ "UPDATE scans SET target_snapshot_digest = NULL WHERE id = ?", (scan_id,))connection.commit()connection.row_factory = sqlite3.Rowstored_scan = connection.execute(- "SELECT * FROM scans WHERE id = ?", (scan["scanId"],)+ "SELECT * FROM scans WHERE id = ?", (scan_id,)).fetchone()with pytest.raises(SystemExit, match="Could not read untracked file"):workbench_api["remediation_checkout_snapshot"](stored_scan)- assert (- run_workbench(state_dir, *command, "--check-target")["scan"]["scanId"] == scan["scanId"]- )+ assert get_scan(state_dir, scan_id, "--check-target")["scan"]["scanId"] == scan_idfinally:entry.chmod(mode)diff --git a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py b/plugins/codex-security/tests/test_workbench_setup_and_migrations.pyindex 8a3e7f5b5..c00af1b92 100644--- a/plugins/codex-security/tests/test_workbench_setup_and_migrations.py+++ b/plugins/codex-security/tests/test_workbench_setup_and_migrations.py@@ -183,6 +183,52 @@ def create_historical_database(return connection, apply_migrations+def insert_historical_row(+ connection: sqlite3.Connection, table: str, timestamp: str, **columns: object+) -> None:+ columns.update(created_at=timestamp, updated_at=timestamp)+ names = ", ".join(columns)+ parameters = ", ".join("?" for _ in columns)+ connection.execute(+ f"INSERT INTO {table} ({names}) VALUES ({parameters})", tuple(columns.values())+ )+++def insert_legacy_scan(connection: sqlite3.Connection, timestamp: str, **overrides: object) -> None:+ columns = {+ "id": "legacy-scan",+ "workspace_id": "legacy-workspace",+ "target_path": "/legacy/target",+ "target_revision": "legacy-revision",+ "scope": ".",+ "mode": "standard",+ "scan_dir": "/legacy/scan",+ "status": "running",+ "phase": "discovery",+ "started_at": timestamp,+ }+ insert_historical_row(connection, "scans", timestamp, **(columns | overrides))+++def rename_migration(connection: sqlite3.Connection, version: int, name: str) -> None:+ connection.execute("UPDATE schema_migrations SET name = ? WHERE version = ?", (name, version))+++def record_migration(+ connection: sqlite3.Connection, version: int, name: str, timestamp: str+) -> None:+ connection.execute(+ "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",+ (version, name, timestamp),+ )+++def migration_name_row(connection: sqlite3.Connection, version: int):+ return connection.execute(+ "SELECT name FROM schema_migrations WHERE version = ?", (version,)+ ).fetchone()++def create_migration_history(connection: sqlite3.Connection) -> None:connection.execute("""@@ -203,10 +249,7 @@ def apply_historical_migrations(for version, name, sql in migrations:for statement in SCHEMA.sql_statements(sql):connection.execute(statement)- connection.execute(- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",- (version, name, timestamp),- )+ record_migration(connection, version, name, timestamp)def add_legacy_profile_columns(connection: sqlite3.Connection, *, dynamic: bool) -> tuple[str, str]:@@ -262,34 +305,25 @@ def insert_legacy_profile_scan(timestamp: str,) -> tuple[str, str]:workspace_id, scan_id = str(uuid.uuid4()), str(uuid.uuid4())- connection.execute(- """- INSERT INTO workspaces (- id, target_path, default_mode, execution_model, reasoning_effort,- created_at, updated_at- ) VALUES (?, ?, 'standard', ?, ?, ?, ?)- """,- (workspace_id, str(tmp_path / "target"), model, effort, timestamp, timestamp),+ insert_historical_row(+ connection,+ "workspaces",+ timestamp,+ id=workspace_id,+ target_path=str(tmp_path / "target"),+ default_mode="standard",+ execution_model=model,+ reasoning_effort=effort,)- connection.execute(- """- INSERT INTO scans (- id, workspace_id, target_path, target_revision, scope, mode, scan_dir,- status, phase, started_at, created_at, updated_at, execution_model, reasoning_effort- ) VALUES (?, ?, ?, 'legacy-revision', '.', 'standard', ?, 'running',- 'discovery', ?, ?, ?, ?, ?)- """,- (- scan_id,- workspace_id,- str(tmp_path / "target"),- str(tmp_path / "legacy-scan"),- timestamp,- timestamp,- timestamp,- model,- effort,- ),+ insert_legacy_scan(+ connection,+ timestamp,+ id=scan_id,+ workspace_id=workspace_id,+ target_path=str(tmp_path / "target"),+ scan_dir=str(tmp_path / "legacy-scan"),+ execution_model=model,+ reasoning_effort=effort,)return workspace_id, scan_id@@ -298,15 +332,14 @@ def assert_new_scan_model_is_independent(state_dir: Path, target: Path, scan_roocurrent_workspace = create_saved_workspace(state_dir, target)current_scan = start_delivered_scan(state_dir,- "--workspace-id",str(current_workspace["id"]),"--scan-root",str(scan_root),"--model","gpt-5.6-sol",)- current_scan_id = str(current_scan["results"]["scanId"])- assert current_scan["results"]["reasoningEffort"] is None+ current_scan_id = str(current_scan["scanId"])+ assert current_scan["reasoningEffort"] is Noneupdate_progress(state_dir, current_scan_id, "--reasoning-effort", "high")with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:assert connection.execute(@@ -925,6 +958,27 @@ def test_comparison_indexes_upgrade_without_skipping_findings_migrations(assert connection.execute("PRAGMA foreign_key_check").fetchall() == []+def insert_classified_scan(+ connection: sqlite3.Connection, scan_id: str, finding_ids: list[str], timestamp: str+) -> None:+ insert_legacy_scan(+ connection,+ timestamp,+ id=scan_id,+ workspace_id="workspace",+ target_path="/target",+ target_revision="revision",+ scan_dir=f"/scans/{scan_id}",+ status="complete",+ phase="reporting",+ )+ connection.execute(+ "INSERT INTO scan_severity_classifications (scan_id, finding_ids_json, assessed_at) "+ "VALUES (?, ?, ?)",+ (scan_id, json.dumps(finding_ids), timestamp),+ )++@pytest.mark.parametrize("indexed", [False, True])def test_severity_migration_only_copies_assessments_with_matching_scan_occurrences(indexed: bool,@@ -933,24 +987,9 @@ def test_severity_migration_only_copies_assessments_with_matching_scan_occurrenctimestamp = "2026-09-01T00:00:00Z"with connection:connection.execute("PRAGMA foreign_keys = ON")- connection.execute(- "INSERT INTO workspaces (id, created_at, updated_at) VALUES (?, ?, ?)",- ("workspace", timestamp, timestamp),- )+ insert_historical_row(connection, "workspaces", timestamp, id="workspace")for scan_id in ("first-scan", "second-scan"):- 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 (?, 'workspace', '/target', 'revision', '.', 'standard', ?,- 'complete', 'reporting', ?, ?, ?)""",- (scan_id, f"/scans/{scan_id}", timestamp, timestamp, timestamp),- )- connection.execute(- """INSERT INTO scan_severity_classifications- (scan_id, finding_ids_json, assessed_at) VALUES (?, '["finding"]', ?)""",- (scan_id, timestamp),- )+ insert_classified_scan(connection, scan_id, ["finding"], timestamp)connection.execute("""INSERT INTO findings(id, fingerprint, rule_id, identity_anchor, created_at, updated_at)@@ -994,27 +1033,12 @@ def test_severity_migration_scales_with_classified_scans() -> None:connection, apply_migrations = create_historical_database(43)with closing(connection):connection.execute("PRAGMA foreign_keys = ON")- connection.execute(- "INSERT INTO workspaces (id, created_at, updated_at) VALUES ('workspace', ?, ?)",- (timestamp, timestamp),- )+ insert_historical_row(connection, "workspaces", timestamp, id="workspace")scan_count, findings_per_scan = scale, 10for scan in range(scan_count):scan_id = f"scan-{scan}"finding_ids = [f"finding-{scan}-{item}" for item in range(findings_per_scan)]- 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 (?, 'workspace', '/target', 'revision', '.', 'standard', ?,- 'complete', 'reporting', ?, ?, ?)""",- (scan_id, f"/scans/{scan_id}", timestamp, timestamp, timestamp),- )- connection.execute(- """INSERT INTO scan_severity_classifications- (scan_id, finding_ids_json, assessed_at) VALUES (?, ?, ?)""",- (scan_id, json.dumps(finding_ids), timestamp),- )+ insert_classified_scan(connection, scan_id, finding_ids, timestamp)for finding_id in finding_ids:occurrence_id = f"occurrence-{finding_id}"connection.execute(@@ -1080,32 +1104,8 @@ def test_workbench_backfills_repository_targets_only_during_migration() -> None:def test_scan_model_migration_preserves_existing_scans() -> None:connection, apply_migrations = create_historical_database(25)timestamp = "2026-07-01T00:00:00Z"- connection.execute(- "INSERT INTO workspaces (id, created_at, updated_at) VALUES (?, ?, ?)",- ("legacy-workspace", timestamp, timestamp),- )- 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)- """,- (- "legacy-scan",- "legacy-workspace",- "/legacy/target",- "legacy-revision",- ".",- "standard",- "/legacy/scan",- "running",- "discovery",- timestamp,- timestamp,- timestamp,- ),- )+ insert_historical_row(connection, "workspaces", timestamp, id="legacy-workspace")+ insert_legacy_scan(connection, timestamp)connection.commit()apply_migrations(connection)@@ -1135,27 +1135,15 @@ def test_deep_scan_failure_counter_migration_backfills_existing_thresholds(timestamp = "2026-07-01T00:00:00Z"for scan_id, stop_after_no_new in (("legacy-scan-a", 2), ("legacy-scan-b", 7)):workspace_id = f"workspace-{scan_id}"- connection.execute(- "INSERT INTO workspaces (id, created_at, updated_at) VALUES (?, ?, ?)",- (workspace_id, timestamp, timestamp),- )- 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 (?, ?, ?, 'legacy-revision', '.', 'deep', ?,- 'running', 'discovery', ?, ?, ?)- """,- (- scan_id,- workspace_id,- f"/legacy/{scan_id}",- f"/legacy/scans/{scan_id}",- timestamp,- timestamp,- timestamp,- ),+ insert_historical_row(connection, "workspaces", timestamp, id=workspace_id)+ insert_legacy_scan(+ connection,+ timestamp,+ id=scan_id,+ workspace_id=workspace_id,+ target_path=f"/legacy/{scan_id}",+ mode="deep",+ scan_dir=f"/legacy/scans/{scan_id}",)connection.execute("""@@ -1170,10 +1158,7 @@ def test_deep_scan_failure_counter_migration_backfills_existing_thresholds(connection.execute("ALTER TABLE deep_scan_runs ADD COLUMN coordinator_owner_id TEXT")connection.execute("ALTER TABLE deep_scan_runs ADD COLUMN coordinator_heartbeat_at TEXT")if legacy_name is not None:- connection.execute(- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (27, ?, ?)",- (legacy_name, timestamp),- )+ record_migration(connection, 27, legacy_name, timestamp)connection.commit()apply_migrations(connection)assert [@@ -1196,9 +1181,9 @@ def test_deep_scan_failure_counter_migration_backfills_existing_thresholds("FROM deep_scan_runs ORDER BY scan_id")] == [(9, 3), (9, 3)]- assert connection.execute("SELECT name FROM schema_migrations WHERE version = 27").fetchone()[- "name"- ] == (legacy_name or "persist deep scan consecutive discovery failures")+ assert migration_name_row(connection, 27)["name"] == (+ legacy_name or "persist deep scan consecutive discovery failures"+ )if has_owner_columns:assert {"coordinator_owner_id", "coordinator_heartbeat_at"} <= {row[1] for row in connection.execute("PRAGMA table_info(deep_scan_runs)")@@ -1213,20 +1198,8 @@ def test_deep_scan_time_limit_migration_backfills_and_repairs_existing_runs(connection, apply_migrations = create_historical_database(28)timestamp = "2026-07-01T00:00:00Z"- connection.execute(- "INSERT INTO workspaces (id, created_at, updated_at) VALUES (?, ?, ?)",- ("legacy-workspace", timestamp, timestamp),- )- 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 (?, ?, '/legacy/target', 'legacy-revision', '.', 'deep',- '/legacy/scan', 'running', 'discovery', ?, ?, ?)- """,- ("legacy-scan", "legacy-workspace", timestamp, timestamp, timestamp),- )+ insert_historical_row(connection, "workspaces", timestamp, id="legacy-workspace")+ insert_legacy_scan(connection, timestamp, mode="deep")connection.execute("""INSERT INTO deep_scan_runs (@@ -1237,10 +1210,7 @@ def test_deep_scan_time_limit_migration_backfills_and_repairs_existing_runs(("legacy-scan", timestamp, timestamp),)if migration_recorded:- connection.execute(- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (28, ?, ?)",- ("persist deep scan discovery time limit", timestamp),- )+ record_migration(connection, 28, "persist deep scan discovery time limit", timestamp)connection.commit()apply_migrations(connection)@@ -1252,10 +1222,7 @@ def test_deep_scan_time_limit_migration_backfills_and_repairs_existing_runs(apply_migrations(connection)assert connection.execute("SELECT max_time_hours FROM deep_scan_runs").fetchone()[0] == 2.5- assert (- connection.execute("SELECT name FROM schema_migrations WHERE version = 28").fetchone()[0]- == "persist deep scan discovery time limit"- )+ assert migration_name_row(connection, 28)[0] == "persist deep scan discovery time limit"def test_workbench_reconciles_monorepo_migration_lineage() -> None:@@ -1559,15 +1526,11 @@ def test_workbench_repairs_recorded_deep_scan_migration(assert connection.execute("SELECT deep_scan_owner_thread_id, status FROM scans WHERE id = ?", (scan_id,)).fetchone() == (expected_owner, "running")- assert connection.execute(- "SELECT name FROM schema_migrations WHERE version = 11"- ).fetchone() == ("deep scan orchestration state",)- assert connection.execute(- "SELECT name FROM schema_migrations WHERE version = 27"- ).fetchone() == ("persist deep scan consecutive discovery failures",)- assert connection.execute(- "SELECT name FROM schema_migrations WHERE version = 28"- ).fetchone() == ("persist deep scan discovery time limit",)+ assert migration_name_row(connection, 11) == ("deep scan orchestration state",)+ assert migration_name_row(connection, 27) == (+ "persist deep scan consecutive discovery failures",+ )+ assert migration_name_row(connection, 28) == ("persist deep scan discovery time limit",)assert {"max_time_hours", "terminal_reviewed_surface_count"} <= {row[1] for row in connection.execute("PRAGMA table_info(deep_scan_runs)")}@@ -1668,31 +1631,27 @@ def test_workbench_repairs_pre_release_scan_continuation_migration(tmp_path: Patconnection.execute("PRAGMA foreign_keys = ON")create_migration_history(connection)apply_historical_migrations(connection, (*migrations[:10], *legacy_migrations), timestamp)- connection.execute(- """- INSERT INTO workspaces (- id, target_path, target_title, default_mode, submitted, thread_id,- created_at, updated_at- ) VALUES (?, ?, ?, 'deep', 1, ?, ?, ?)- """,- (workspace_id, str(target), "Legacy target", thread_id, timestamp, timestamp),- )- 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 (?, ?, ?, 'unversioned', '.', 'deep', ?, 'running', 'preflight', ?, ?, ?)- """,- (- scan_id,- workspace_id,- str(target),- str(tmp_path / "scans"),- timestamp,- timestamp,- timestamp,- ),+ insert_historical_row(+ connection,+ "workspaces",+ timestamp,+ id=workspace_id,+ target_path=str(target),+ target_title="Legacy target",+ default_mode="deep",+ submitted=1,+ thread_id=thread_id,+ )+ insert_legacy_scan(+ connection,+ timestamp,+ id=scan_id,+ workspace_id=workspace_id,+ target_path=str(target),+ target_revision="unversioned",+ mode="deep",+ scan_dir=str(tmp_path / "scans"),+ phase="preflight",)connection.execute("INSERT INTO scan_progress (scan_id, updated_at) VALUES (?, ?)",@@ -1732,10 +1691,7 @@ def test_workbench_repairs_pre_release_scan_continuation_migration(tmp_path: Pat(scan_id, timestamp, timestamp),)- first = workspace_command(state_dir, "get-workspace", workspace_id)- second = workspace_command(state_dir, "get-workspace", workspace_id)-- assert first == second+ first = read_migrated_workspace_twice(state_dir, workspace_id)assert first["results"]["continuationThreadId"] is Nonewith sqlite3.connect(database) as connection:assert connection.execute(@@ -1776,29 +1732,36 @@ def test_workbench_repairs_pre_release_scan_continuation_migration(tmp_path: Patassert connection.execute("PRAGMA foreign_key_check").fetchall() == []-def test_workbench_repairs_recorded_scan_scope_file_count_migration(tmp_path: Path) -> None:+def start_migration_scan(tmp_path: Path) -> tuple[Path, str, str, Path]:state_dir, target = empty_target_paths(tmp_path)- workspace = create_saved_workspace(state_dir, target)- started = start_scan_command(state_dir, str(workspace["id"]))- scan_id = str(started["results"]["scanId"])- database = state_dir / "workbench.sqlite3"+ workspace_id = str(create_saved_workspace(state_dir, target)["id"])+ started = start_scan_command(state_dir, workspace_id)+ return (+ state_dir,+ workspace_id,+ str(started["results"]["scanId"]),+ state_dir / "workbench.sqlite3",+ )+++def read_migrated_workspace_twice(state_dir: Path, workspace_id: str) -> dict[str, object]:+ first = workspace_command(state_dir, "get-workspace", workspace_id)+ second = workspace_command(state_dir, "get-workspace", workspace_id)+ assert first == second+ return first+++def test_workbench_repairs_recorded_scan_scope_file_count_migration(tmp_path: Path) -> None:+ state_dir, workspace_id, scan_id, database = start_migration_scan(tmp_path)with sqlite3.connect(database) as connection:connection.execute("ALTER TABLE scan_progress DROP COLUMN scope_file_count")- connection.execute(- "UPDATE schema_migrations SET name = ? WHERE version = 13",- ("linear scan phase enforcement",),- )-- first = workspace_command(state_dir, "get-workspace", str(workspace["id"]))- second = workspace_command(state_dir, "get-workspace", str(workspace["id"]))+ rename_migration(connection, 13, "linear scan phase enforcement")- assert first == second+ first = read_migrated_workspace_twice(state_dir, workspace_id)assert first["results"]["progress"]["coverage"]["filesTotal"] is Nonewith sqlite3.connect(database) as connection:- assert connection.execute(- "SELECT name FROM schema_migrations WHERE version = 13"- ).fetchone() == ("linear scan phase enforcement",)+ assert migration_name_row(connection, 13) == ("linear scan phase enforcement",)assert connection.execute("SELECT scope_file_count FROM scan_progress WHERE scan_id = ?", (scan_id,)).fetchone() == (None,)@@ -1822,9 +1785,7 @@ def test_workbench_repairs_shadowed_capability_preflight_migration(with sqlite3.connect(database) as connection:connection.execute("ALTER TABLE workspaces DROP COLUMN capability_preflight_json")- connection.execute(- "UPDATE schema_migrations SET name = ? WHERE version = 2", (legacy_name,)- )+ rename_migration(connection, 2, legacy_name)if owns_thread_schema:connection.execute("DELETE FROM schema_migrations WHERE version = 6")@@ -1872,32 +1833,24 @@ def test_workbench_upgrades_stable_scan_target_identity_migration(tmp_path: Path(*migrations[:10], legacy_migration, *migrations[11:15]),timestamp,)- connection.execute(- """- INSERT INTO workspaces (- id, target_path, target_title, default_mode, submitted, created_at, updated_at- ) VALUES (?, ?, 'Legacy target', 'standard', 1, ?, ?)- """,- (workspace_id, str(target), timestamp, timestamp),+ insert_historical_row(+ connection,+ "workspaces",+ timestamp,+ id=workspace_id,+ target_path=str(target),+ target_title="Legacy target",+ default_mode="standard",+ submitted=1,)- connection.execute(- """- INSERT INTO scans (- id, workspace_id, target_id, target_path, target_revision, scope, mode,- scan_dir, status, phase, started_at, created_at, updated_at- ) VALUES (?, ?, ?, ?, 'legacy-revision', '.', 'standard', ?, 'running',- 'discovery', ?, ?, ?)- """,- (- scan_id,- workspace_id,- legacy_target_id,- str(target),- str(tmp_path / "scans"),- timestamp,- timestamp,- timestamp,- ),+ insert_legacy_scan(+ connection,+ timestamp,+ id=scan_id,+ workspace_id=workspace_id,+ target_id=legacy_target_id,+ target_path=str(target),+ scan_dir=str(tmp_path / "scans"),)connection.execute("INSERT INTO scan_progress (scan_id, updated_at) VALUES (?, ?)",@@ -1948,11 +1901,7 @@ def test_workbench_upgrades_stable_scan_target_identity_migration(tmp_path: Pathdef test_workbench_applies_shadowed_delivered_claim_cleanup_once(tmp_path: Path) -> None:- state_dir, target = empty_target_paths(tmp_path)- workspace = create_saved_workspace(state_dir, target)- started = start_scan_command(state_dir, str(workspace["id"]))- scan_id = str(started["results"]["scanId"])- database = state_dir / "workbench.sqlite3"+ state_dir, _, scan_id, database = start_migration_scan(tmp_path)with sqlite3.connect(database) as connection:connection.execute(@@ -1960,10 +1909,7 @@ def test_workbench_applies_shadowed_delivered_claim_cleanup_once(tmp_path: Path)"handoff_claim_token = ? WHERE id = ?",("2026-06-30T00:00:00Z", "stale-claim", scan_id),)- connection.execute(- "UPDATE schema_migrations SET name = ? WHERE version = 18",- ("scan target summaries",),- )+ rename_migration(connection, 18, "scan target summaries")run_workbench(state_dir, "database-info")@@ -1971,9 +1917,7 @@ def test_workbench_applies_shadowed_delivered_claim_cleanup_once(tmp_path: Path)assert connection.execute("SELECT handoff_claimed_at, handoff_claim_token FROM scans WHERE id = ?", (scan_id,)).fetchone() == (None, None)- assert connection.execute(- "SELECT name FROM schema_migrations WHERE version = 18"- ).fetchone() == ("clear legacy delivered handoff claims",)+ assert migration_name_row(connection, 18) == ("clear legacy delivered handoff claims",)connection.execute("UPDATE scans SET handoff_claimed_at = ?, handoff_claim_token = ? WHERE id = ?",("2026-07-01T00:00:00Z", "current-claim", scan_id),@@ -2000,10 +1944,7 @@ def test_workbench_repairs_shadowed_setup_preferences_migration(with sqlite3.connect(database) as connection:connection.execute("DROP TABLE setup_preferences")- connection.execute(- "UPDATE schema_migrations SET name = ? WHERE version = 19",- (legacy_name,),- )+ rename_migration(connection, 19, legacy_name)run_workbench(state_dir, "database-info")with sqlite3.connect(database) as connection:@@ -2012,9 +1953,7 @@ def test_workbench_repairs_shadowed_setup_preferences_migration("skip_setup_ui","updated_at",]- assert connection.execute(- "SELECT name FROM schema_migrations WHERE version = 19"- ).fetchone() == ("persist setup workspace preference",)+ assert migration_name_row(connection, 19) == ("persist setup workspace preference",)@pytest.mark.parametrize(@@ -2024,34 +1963,22 @@ def test_workbench_repairs_shadowed_setup_preferences_migration(def test_workbench_repairs_shadowed_phase_progress_migration(tmp_path: Path, legacy_name: str) -> None:- state_dir, target = empty_target_paths(tmp_path)- workspace = create_saved_workspace(state_dir, target)- started = start_scan_command(state_dir, str(workspace["id"]))- scan_id = str(started["results"]["scanId"])- database = state_dir / "workbench.sqlite3"+ state_dir, workspace_id, scan_id, database = start_migration_scan(tmp_path)with sqlite3.connect(database) as connection:connection.execute("ALTER TABLE scan_progress DROP COLUMN phase_items_completed")connection.execute("ALTER TABLE scan_progress DROP COLUMN phase_items_total")connection.execute("ALTER TABLE scan_progress DROP COLUMN phase_progress_unit")- connection.execute(- "UPDATE schema_migrations SET name = ? WHERE version = 20",- (legacy_name,),- )-- first = workspace_command(state_dir, "get-workspace", str(workspace["id"]))- second = workspace_command(state_dir, "get-workspace", str(workspace["id"]))+ rename_migration(connection, 20, legacy_name)- assert first == second+ read_migrated_workspace_twice(state_dir, workspace_id)with sqlite3.connect(database) as connection:assert connection.execute("SELECT phase_items_total, phase_items_completed, phase_progress_unit ""FROM scan_progress WHERE scan_id = ?",(scan_id,),).fetchone() == (0, 0, None)- assert connection.execute(- "SELECT name FROM schema_migrations WHERE version = 20"- ).fetchone() == ("phase-specific scan progress",)+ assert migration_name_row(connection, 20) == ("phase-specific scan progress",)with pytest.raises(sqlite3.IntegrityError) as invalid_unit:connection.execute("UPDATE scan_progress SET phase_progress_unit = 'invalid' WHERE scan_id = ?",@@ -2071,11 +1998,7 @@ def test_workbench_repairs_shadowed_phase_progress_migration(def test_workbench_repairs_shadowed_preflight_progress_migration(tmp_path: Path, legacy_name: str) -> None:- state_dir, target = empty_target_paths(tmp_path)- saved = create_saved_workspace(state_dir, target)- started = start_scan_command(state_dir, str(saved["id"]))- scan_id = str(started["results"]["scanId"])- database = state_dir / "workbench.sqlite3"+ state_dir, _, scan_id, database = start_migration_scan(tmp_path)with sqlite3.connect(database) as connection:connection.execute("""@@ -2086,9 +2009,7 @@ def test_workbench_repairs_shadowed_preflight_progress_migration(""",(scan_id,),)- connection.execute(- "UPDATE schema_migrations SET name = ? WHERE version = 21", (legacy_name,)- )+ rename_migration(connection, 21, legacy_name)connection.execute("ALTER TABLE scan_progress DROP COLUMN preflight_checks_completed")connection.execute("ALTER TABLE scan_progress DROP COLUMN preflight_checks_total")connection.execute("ALTER TABLE scan_progress DROP COLUMN preflight_issues_json")@@ -2104,38 +2025,24 @@ def test_workbench_repairs_shadowed_preflight_progress_migration(""",(scan_id,),).fetchone() == (0, 0, "[]")- assert connection.execute(- "SELECT name FROM schema_migrations WHERE version = 21"- ).fetchone() == ("current scan preflight state",)+ assert migration_name_row(connection, 21) == ("current scan preflight state",)def test_workbench_repairs_shadowed_scan_recipe_migration(tmp_path: Path) -> None:- state_dir, target = empty_target_paths(tmp_path)- workspace = create_saved_workspace(state_dir, target)- started = start_scan_command(state_dir, str(workspace["id"]))- scan_id = str(started["results"]["scanId"])- database = state_dir / "workbench.sqlite3"+ state_dir, workspace_id, scan_id, database = start_migration_scan(tmp_path)with sqlite3.connect(database) as connection:connection.execute("ALTER TABLE scans DROP COLUMN parent_scan_id")connection.execute("ALTER TABLE scans DROP COLUMN recipe_json")- connection.execute(- "UPDATE schema_migrations SET name = ? WHERE version = 22",- ("dynamic scan execution profiles",),- )+ rename_migration(connection, 22, "dynamic scan execution profiles")- first = workspace_command(state_dir, "get-workspace", str(workspace["id"]))- second = workspace_command(state_dir, "get-workspace", str(workspace["id"]))-- assert first == second+ read_migrated_workspace_twice(state_dir, workspace_id)with sqlite3.connect(database) as connection:assert connection.execute("SELECT recipe_json, parent_scan_id FROM scans WHERE id = ?",(scan_id,),).fetchone() == (None, None)- assert connection.execute(- "SELECT name FROM schema_migrations WHERE version = 22"- ).fetchone() == ("replayable scan launch recipes",)+ assert migration_name_row(connection, 22) == ("replayable scan launch recipes",)assert any(row[3] == "parent_scan_id" and row[2] == "scans" and row[6] == "SET NULL"for row in connection.execute("PRAGMA foreign_key_list(scans)")@@ -2144,11 +2051,7 @@ def test_workbench_repairs_shadowed_scan_recipe_migration(tmp_path: Path) -> Nondef test_workbench_reconciles_released_completion_warning_version(tmp_path: Path) -> None:- state_dir, target = empty_target_paths(tmp_path)- workspace = create_saved_workspace(state_dir, target)- started = start_scan_command(state_dir, str(workspace["id"]))- scan_id = str(started["results"]["scanId"])- database = state_dir / "workbench.sqlite3"+ state_dir, workspace_id, scan_id, database = start_migration_scan(tmp_path)with sqlite3.connect(database) as connection:connection.execute(@@ -2158,15 +2061,9 @@ def test_workbench_reconciles_released_completion_warning_version(tmp_path: Pathconnection.execute("DELETE FROM schema_migrations WHERE version = 26")connection.execute("ALTER TABLE scans DROP COLUMN reasoning_effort")connection.execute("ALTER TABLE scans DROP COLUMN model")- connection.execute(- "UPDATE schema_migrations SET name = ? WHERE version = 25",- ("persist scan completion warnings",),- )+ rename_migration(connection, 25, "persist scan completion warnings")- first = workspace_command(state_dir, "get-workspace", str(workspace["id"]))- second = workspace_command(state_dir, "get-workspace", str(workspace["id"]))-- assert first == second+ read_migrated_workspace_twice(state_dir, workspace_id)with sqlite3.connect(database) as connection:assert connection.execute("SELECT model, reasoning_effort, completion_warnings_json FROM scans WHERE id = ?",@@ -2181,32 +2078,20 @@ def test_workbench_reconciles_released_completion_warning_version(tmp_path: Pathdef test_workbench_repairs_recorded_completion_warnings_migration(tmp_path: Path) -> None:- state_dir, target = empty_target_paths(tmp_path)- workspace = create_saved_workspace(state_dir, target)- started = start_scan_command(state_dir, str(workspace["id"]))- scan_id = str(started["results"]["scanId"])- database = state_dir / "workbench.sqlite3"+ state_dir, workspace_id, scan_id, database = start_migration_scan(tmp_path)with sqlite3.connect(database) as connection:connection.execute("ALTER TABLE scans DROP COLUMN completion_warnings_json")connection.execute("ALTER TABLE scans ADD COLUMN attention_message TEXT")- connection.execute(- "UPDATE schema_migrations SET name = ? WHERE version = 26",- ("recoverable scan attention",),- )+ rename_migration(connection, 26, "recoverable scan attention")- first = workspace_command(state_dir, "get-workspace", str(workspace["id"]))- second = workspace_command(state_dir, "get-workspace", str(workspace["id"]))-- assert first == second+ read_migrated_workspace_twice(state_dir, workspace_id)with sqlite3.connect(database) as connection:assert connection.execute("SELECT completion_warnings_json, attention_message FROM scans WHERE id = ?",(scan_id,),).fetchone() == ("[]", None)- assert connection.execute(- "SELECT name FROM schema_migrations WHERE version = 26"- ).fetchone() == ("recoverable scan attention",)+ assert migration_name_row(connection, 26) == ("recoverable scan attention",)assert connection.execute("PRAGMA foreign_key_check").fetchall() == []@@ -2243,10 +2128,7 @@ def test_workbench_reconciles_legacy_execution_profile_migrations(current_migrations = (*migrations[: legacy_version - 1], *migrations[legacy_version:24])apply_historical_migrations(connection, current_migrations, timestamp)legacy_model, legacy_effort = add_legacy_profile_columns(connection, dynamic=dynamic)- connection.execute(- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",- (legacy_version, legacy_name, timestamp),- )+ record_migration(connection, legacy_version, legacy_name, timestamp)if preexisting_model:connection.execute("ALTER TABLE scans ADD COLUMN model TEXT")workspace_id, scan_id = insert_legacy_profile_scan(@@ -2266,9 +2148,7 @@ def test_workbench_reconciles_legacy_execution_profile_migrations(25: "persist scan model settings",}.get(legacy_version, legacy_name)with sqlite3.connect(database) as connection:- assert connection.execute(- "SELECT name FROM schema_migrations WHERE version = ?", (legacy_version,)- ).fetchone() == (expected_migration_name,)+ assert migration_name_row(connection, legacy_version) == (expected_migration_name,)assert connection.execute("""SELECT legacy_execution_model, legacy_reasoning_effort, model, reasoning_effort@@ -2375,9 +2255,7 @@ def test_workbench_upgrades_pre_release_source_scope_migration(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 migration_name_row(connection, 34) == ("associate findings with repositories",)assert connection.execute("SELECT version, applied_at FROM schema_migrations WHERE name = ?",(source_scope[1],),@@ -2385,9 +2263,7 @@ def test_workbench_upgrades_pre_release_source_scope_migration(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 migration_name_row(connection, 43) == ("preserve severity assessments per scan",)assert connection.execute("SELECT COUNT(*) FROM scan_severity_assessments").fetchone() == (0,)@@ -2489,10 +2365,7 @@ def test_workbench_normalizes_pre_release_migration_numbers(tmp_path: Path) -> Nconnection.execute("UPDATE schema_migrations SET version = 2 WHERE version = -3")connection.execute("UPDATE schema_migrations SET version = 3 WHERE version = -4")connection.execute("UPDATE schema_migrations SET version = 4 WHERE version = -5")- connection.execute(- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",- (5, "scan target snapshot digests", "2026-06-01T00:00:00Z"),- )+ record_migration(connection, 5, "scan target snapshot digests", "2026-06-01T00:00:00Z")connection.execute("ALTER TABLE workspaces DROP COLUMN capability_preflight_json")run_workbench(state_dir, "database-info")@@ -2554,32 +2427,13 @@ def test_workbench_upgrades_public_cli_completion_warning_migration() -> None:)timestamp = "2026-07-01T00:00:00Z"- connection.execute(- "INSERT INTO workspaces (id, created_at, updated_at) VALUES (?, ?, ?)",- ("legacy-workspace", timestamp, timestamp),- )- connection.execute(- """- INSERT INTO scans (- id, workspace_id, target_path, target_revision, scope, mode, scan_dir,- status, phase, started_at, created_at, updated_at, completion_warnings_json- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)- """,- (- "legacy-scan",- "legacy-workspace",- "/legacy/target",- "legacy-revision",- ".",- "standard",- "/legacy/scan",- "complete",- "reporting",- timestamp,- timestamp,- timestamp,- '["existing warning"]',- ),+ insert_historical_row(connection, "workspaces", timestamp, id="legacy-workspace")+ insert_legacy_scan(+ connection,+ timestamp,+ status="complete",+ phase="reporting",+ completion_warnings_json='["existing warning"]',)connection.commit()@@ -2644,10 +2498,7 @@ def test_workbench_upgrades_legacy_execution_profile_migrations(if migration_history == "canonical-metadata":apply_historical_migrations(connection, migrations[10:24], timestamp)connection.execute("ALTER TABLE scans ADD COLUMN model TEXT")- connection.execute(- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",- (25, "persist scan model settings", timestamp),- )+ record_migration(connection, 25, "persist scan model settings", timestamp)else:legacy_migrations = [(11, "scan execution profiles", timestamp)]if has_dynamic_profile_migration:@@ -2675,13 +2526,11 @@ def test_workbench_upgrades_legacy_execution_profile_migrations(connection.execute("ALTER TABLE scans ADD COLUMN completion_warnings_json TEXT NOT NULL DEFAULT '[]'")- connection.execute(- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",- (- 26 if migration_history == "canonical-metadata" else 25,- "persist scan completion warnings",- timestamp,- ),+ record_migration(+ connection,+ 26 if migration_history == "canonical-metadata" else 25,+ "persist scan completion warnings",+ timestamp,)workspace_id, scan_id = insert_legacy_profile_scan(connection, tmp_path, model, reasoning_effort, timestampdiff --git a/plugins/codex-security/tests/test_workbench_standard_deep_results.py b/plugins/codex-security/tests/test_workbench_standard_deep_results.pyindex ece78efbd..61fc0617d 100644--- a/plugins/codex-security/tests/test_workbench_standard_deep_results.py+++ b/plugins/codex-security/tests/test_workbench_standard_deep_results.py@@ -33,11 +33,13 @@ from workbench_test_support import (saved_discovery_worker,saved_draft,scan_command,+ select_checkpoint,upsert_deep_worker,worker_paths,workspace_command,write_checkpoint,write_completed_contract,+ write_result_draft,)scripts_dir = Path(__file__).resolve().parents[1] / "scripts"@@ -80,7 +82,7 @@ def test_stopped_deep_scan_ignores_late_worker_checkpoints_without_reducer(write_checkpoint(checkpoint_dir, revised)head = max(checkpoint_dir.glob("*.json"))latest_model = json.loads(head.read_text())["threatModel"]- (result_path.parent / "checkpoint-head.json").write_text(json.dumps({"checkpoint": head.name}))+ select_checkpoint(result_path.parent, head)# The latest incomplete attempt need not be parseable for a saved checkpoint to survive.result_path.write_text("{incomplete")with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:@@ -160,7 +162,7 @@ def test_stopped_model_selection_survives_publication_retry(selected = (max if head_available else min)(checkpoint_dir.glob("*.json"))head_path = result_path.parent / "checkpoint-head.json"if head_available:- head_path.write_text(json.dumps({"checkpoint": selected.name}))+ select_checkpoint(result_path.parent, selected)if result_state != "unreadable":stale = {**checkpoint, "threatModel": {"summary": "The earlier worker model."}}result_path.write_text(json.dumps(stale))@@ -234,7 +236,7 @@ def test_stopped_model_selection_survives_publication_retry(checkpoint["threatModel"] = {"summary": "The later worker model."}late = write_checkpoint(checkpoint_dir, checkpoint)- head_path.write_text(json.dumps({"checkpoint": late.name}))+ select_checkpoint(result_path.parent, late)environment = {"CODEX_HOME": str(codex_home)}retried = preserve_scan_results(state_dir, scan_id, "standard-worker-thread", environment=environment@@ -1375,9 +1377,7 @@ def test_stopped_rejection_recovers_malformed_parent_surfaces("coverage": {**malformed_coverage, "surfaces": []},},)- head = scan_dir / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": parent_checkpoint.name}))- os.utime(head, ns=(200, 200))+ select_checkpoint(scan_dir, parent_checkpoint, mtime_ns=200)os.utime(scan_dir / "coverage.json", ns=(200, 200))current = json.loads(result_path.read_text())checkpoint = copy.deepcopy(current)@@ -1508,8 +1508,7 @@ def test_complete_worker_model_uses_latest_terminal_source(checkpoint["complete"] = Truecheckpoint["threatModel"] = {"summary": "The revised completed worker model."}head = write_checkpoint(result_path.parent / "checkpoints", checkpoint)- head_path = result_path.parent / "checkpoint-head.json"- head_path.write_text(json.dumps({"checkpoint": head.name}))+ head_path = select_checkpoint(result_path.parent, head)# Timestamp ordering represents an interruption between head and result publication,# or a successfully replaced result whose older head remains available.os.utime(result_path, ns=(2_000_000_000, 2_000_000_000))@@ -1563,7 +1562,7 @@ def test_complete_worker_supersedes_obsolete_checkpoint_coverage(tmp_path: Path)}checkpoints = result_path.parent / "checkpoints"head = write_checkpoint(checkpoints, checkpoint)- (result_path.parent / "checkpoint-head.json").write_text(json.dumps({"checkpoint": head.name}))+ select_checkpoint(result_path.parent, head)# This accepted checkpoint predates the successful result; a newer head reopens work.for path in (head, result_path.parent / "checkpoint-head.json"):@@ -2333,10 +2332,9 @@ def test_recovery_retains_generic_closures_without_a_canonical_write(}(scan_dir / "coverage.json").write_text(json.dumps(canonical_coverage))os.utime(scan_dir / "coverage.json", ns=(100, 100))- original = write_checkpoint(scan_dir / "checkpoints", initial)- os.utime(original, ns=(100, 100))+ original = write_checkpoint(scan_dir / "checkpoints", initial, mtime_ns=100)original_bytes = original.read_bytes()- terminal = write_checkpoint(+ write_checkpoint(scan_dir / "checkpoints",{**initial,@@ -2347,8 +2345,8 @@ def test_recovery_retains_generic_closures_without_a_canonical_write("resolvedDeferred": [closure],},},+ mtime_ns=200,)- os.utime(terminal, ns=(200, 200))if canonical_state == "reopened":os.utime(scan_dir / "coverage.json", ns=(300, 300))if canonical_state == "checkpoint_reopened":@@ -2416,11 +2414,10 @@ def test_recovered_generic_closure_cannot_close_another_workers_same_id(draft = saved_draft(scan_id, deferred=[{"id": "shared-review", "reason": f"Pending in {worker_id}."}])- pending = write_checkpoint(output / "checkpoints", draft)- os.utime(pending, ns=(100, 100))+ write_checkpoint(output / "checkpoints", draft, mtime_ns=100)if worker_id == "worker-one":# Recover the accepted final checkpoint after an interrupted result write.- terminal = write_checkpoint(+ write_checkpoint(output / "checkpoints",{**draft,@@ -2433,8 +2430,8 @@ def test_recovered_generic_closure_cannot_close_another_workers_same_id(],},},+ mtime_ns=closure_time,)- os.utime(terminal, ns=(closure_time, closure_time))else:(output / "result.json").write_text(json.dumps(draft))binding = saved_binding("deep_repository")@@ -2485,7 +2482,7 @@ def test_recovery_keeps_latest_generic_closure_reason(tmp_path: Path, canonical_(scan_dir / "findings.json").write_text(json.dumps({"findings": []}))(scan_dir / "coverage.json").write_text(json.dumps(coverage))os.utime(scan_dir / "coverage.json", ns=(canonical_time, canonical_time))- previous = write_checkpoint(+ write_checkpoint(scan_dir / "checkpoints",{"scanId": scan_id,@@ -2496,8 +2493,8 @@ def test_recovery_keeps_latest_generic_closure_reason(tmp_path: Path, canonical_"resolvedDeferred": [{**closure, "reason": "Initial review completed."}],},},+ mtime_ns=200,)- os.utime(previous, ns=(200, 200))binding = saved_binding()result = workbench_saved_results.merge_saved_results(scan_dir, scan_id, binding, [], [], stopped=True, reason="interrupted"@@ -2525,8 +2522,7 @@ def test_recovery_rejects_closures_that_would_discard_saved_evidence(}coverage = saved_coverage(deferred=[pending])draft = {"scanId": scan_id, "complete": False, "findings": [], "coverage": coverage}- original = write_checkpoint(scan_dir / "checkpoints", draft)- os.utime(original, ns=(100, 100))+ original = write_checkpoint(scan_dir / "checkpoints", draft, mtime_ns=100)original_bytes = original.read_bytes()closure = {"id": pending["id"], "reason": "Review closed."}closures = [closure]@@ -2536,7 +2532,7 @@ def test_recovery_rejects_closures_that_would_discard_saved_evidence(closures.append({"id": "another-review", "reason": "Done.", "evidence": "Invalid field."})elif case == "duplicate_closures":closures.append(dict(closure))- terminal = write_checkpoint(+ write_checkpoint(scan_dir / "checkpoints",{**draft,@@ -2547,8 +2543,8 @@ def test_recovery_rejects_closures_that_would_discard_saved_evidence("resolvedDeferred": closures,},},+ mtime_ns=200,)- os.utime(terminal, ns=(200, 200))binding = {"scanId": scan_id,"startedAt": "2026-01-01T00:00:00Z",@@ -2605,7 +2601,7 @@ def test_recovery_restores_work_reopened_after_parent_closure((scan_dir / "coverage.json").write_text(json.dumps(coverage))os.utime(scan_dir / "coverage.json", ns=(100, 100))pending = {"id": closure["id"], "reason": "New source evidence needs review."}- reopened = write_checkpoint(+ write_checkpoint(scan_dir / "checkpoints",{"scanId": scan_id,@@ -2617,8 +2613,8 @@ def test_recovery_restores_work_reopened_after_parent_closure("resolvedDeferred": [],},},+ mtime_ns=200,)- os.utime(reopened, ns=(200, 200))binding = saved_binding()documents = workbench_saved_results.merge_saved_results(scan_dir, scan_id, binding, [], [], stopped=True, reason="interrupted"@@ -2660,23 +2656,18 @@ def test_recovery_uses_frozen_worker_head_for_identical_reclosure("resolvedDeferred": [{"id": pending["id"], "reason": "Review completed."}],},},+ mtime_ns=100,)- os.utime(closed, ns=(100, 100))- reopened = write_checkpoint(attempt / "checkpoints", draft)- os.utime(reopened, ns=(200, 200))- (attempt / "result.json").write_text(json.dumps(draft))- os.utime(attempt / "result.json", ns=(200, 200))- head = attempt / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": closed.name}))- os.utime(head, ns=(300, 300))+ reopened = write_checkpoint(attempt / "checkpoints", draft, mtime_ns=200)+ write_result_draft(attempt / "result.json", draft, mtime_ns=200)+ select_checkpoint(attempt, closed, mtime_ns=300)if layout != "current":current = (draftif layout.endswith("attempt_reopens")else {**draft, "coverage": {**coverage, "deferred": []}})- (output / "result.json").write_text(json.dumps(current))- os.utime(output / "result.json", ns=(300, 300))+ write_result_draft(output / "result.json", current, mtime_ns=300)workers = [saved_discovery_worker(output, "worker-one", 1 if layout == "current" else 2)]if layout == "legacy_attempt_reopens":workers[0]["attempt"] = 0@@ -2702,8 +2693,7 @@ def test_recovery_uses_frozen_worker_head_for_identical_reclosure(assert replay is not Noneassert replay[2] == documents[2]for checkpoint in (reopened, closed):- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(400, 400))+ select_checkpoint(attempt, checkpoint, mtime_ns=400)replay = workbench_saved_results.merge_saved_results(scan_dir,scan_id,@@ -2717,7 +2707,7 @@ def test_recovery_uses_frozen_worker_head_for_identical_reclosure(assert replay is not Noneassert replay[2] == documents[2]# Legacy frozen evidence did not bind a head; a later head cannot change its result.- head.write_text(json.dumps({"checkpoint": closed.name}))+ select_checkpoint(attempt, closed)legacy = workbench_saved_results.merge_saved_results(scan_dir,scan_id,@@ -2750,15 +2740,11 @@ def test_recovery_keeps_worker_pending_saved_before_head_update("resolvedDeferred": [{"id": "review-closeout", "reason": "Initial review completed."}],}draft = {"scanId": scan_id, "complete": True, "findings": [], "coverage": coverage}- closed = write_checkpoint(output / "checkpoints", draft)- os.utime(closed, ns=(100, 100))- (output / "result.json").write_text(json.dumps(draft))- os.utime(output / "result.json", ns=(200, 200))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": closed.name}))- os.utime(head, ns=(200, 200))+ closed = write_checkpoint(output / "checkpoints", draft, mtime_ns=100)+ write_result_draft(output / "result.json", draft, mtime_ns=200)+ select_checkpoint(output, closed, mtime_ns=200)pending = {"id": "review-closeout", "reason": "New evidence needs review."}- reopened = write_checkpoint(+ write_checkpoint(output / "checkpoints",{**draft,@@ -2768,8 +2754,8 @@ def test_recovery_keeps_worker_pending_saved_before_head_update("resolvedDeferred": [],},},+ mtime_ns=pending_modified,)- os.utime(reopened, ns=(pending_modified, pending_modified))workers = [saved_discovery_worker(output, "worker-one", 1)]binding = saved_binding("deep_repository")documents = workbench_saved_results.merge_saved_results(@@ -2832,8 +2818,7 @@ def test_frozen_parent_retains_identical_latest_observation(original.parent.mkdir()original.write_bytes(payload)os.utime(original, ns=(100, 100))- checkpoint = write_checkpoint(original.parent, middle)- os.utime(checkpoint, ns=(200, 200))+ write_checkpoint(original.parent, middle, mtime_ns=200)write_saved_parent(tmp_path, latest, 300)first = module.merge_saved_results(tmp_path, pending["scanId"], binding, [], [], stopped=True, reason="interrupted"@@ -2897,19 +2882,16 @@ def test_recovery_applies_surface_update_with_generic_closure(output.mkdir(exist_ok=True)workers = []if layout.startswith("worker"):- (output / "result.json").write_text(json.dumps(pending))- os.utime(output / "result.json", ns=(100, 100))+ write_result_draft(output / "result.json", pending, mtime_ns=100)workers = [saved_discovery_worker(output, "worker", 1)]else:pending["complete"] = layout in {"terminal_parent", "reopened_parent"}write_saved_parent(tmp_path, pending, 100)- checkpoint = write_checkpoint(output / "checkpoints", pending)- os.utime(checkpoint, ns=(100, 100))+ write_checkpoint(output / "checkpoints", pending, mtime_ns=100)terminal_draft = copy.deepcopy(closed)if layout == "mixed_manifest":terminal_draft["coverage"]["surfaces"][0].pop("receiptRefs")- terminal = write_checkpoint(output / "checkpoints", terminal_draft)- os.utime(terminal, ns=(200, 200))+ write_checkpoint(output / "checkpoints", terminal_draft, mtime_ns=200)if layout == "worker_idless_other_surface":other = copy.deepcopy(pending)other["coverage"]["surfaces"] = [@@ -2917,8 +2899,7 @@ def test_recovery_applies_surface_update_with_generic_closure(]remaining = {"id": "other-review", "reason": "Independent review remains."}other["coverage"]["deferred"] = [remaining]- checkpoint = write_checkpoint(output / "checkpoints", other)- os.utime(checkpoint, ns=(300, 300))+ write_checkpoint(output / "checkpoints", other, mtime_ns=300)if layout == "mixed_manifest":canonical = {**closed, "complete": False}write_saved_parent(tmp_path, canonical, 300)@@ -2964,8 +2945,7 @@ def test_reopened_candidate_respects_current_outcome(if candidate_identity == "alias":candidate["id"] = "pending-review"for draft, modified in ((closed, 100), (pending, 200)):- checkpoint = write_checkpoint(tmp_path / "checkpoints", draft)- os.utime(checkpoint, ns=(modified, modified))+ write_checkpoint(tmp_path / "checkpoints", draft, mtime_ns=modified)terminal = copy.deepcopy(closed)terminal["coverage"].pop("resolvedDeferred")terminal["coverage"]["surfaces"] = [@@ -3046,11 +3026,8 @@ def test_worker_closure_preserves_ambiguous_legacy_surface_ids(original = write_checkpoint(output / "checkpoints", pending)for path in (result_path, original):os.utime(path, ns=(100, 100))- terminal = write_checkpoint(output / "checkpoints", closed)- os.utime(terminal, ns=(200, 200))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": terminal.name}))- os.utime(head, ns=(200, 200))+ terminal = write_checkpoint(output / "checkpoints", closed, mtime_ns=200)+ head = select_checkpoint(output, terminal, mtime_ns=200)original_bytes = {path: path.read_bytes() for path in (result_path, original, terminal, head)}workers = [saved_discovery_worker(output, "worker", 1)]@@ -3082,11 +3059,9 @@ def test_parent_closure_cannot_discard_reopened_worker_same_id(write_saved_parent(tmp_path, closed, 300)output = tmp_path / "worker"output.mkdir()- (output / "result.json").write_text(json.dumps(closed))- os.utime(output / "result.json", ns=(100, 100))+ write_result_draft(output / "result.json", closed, mtime_ns=100)for draft, modified in ((closed, 100), (pending, 200)):- checkpoint = write_checkpoint(output / "checkpoints", draft)- os.utime(checkpoint, ns=(modified, modified))+ write_checkpoint(output / "checkpoints", draft, mtime_ns=modified)workers = [saved_discovery_worker(output, "worker", 1)]first = module.merge_saved_results(tmp_path, pending["scanId"], binding, workers, [], stopped=True, reason="interrupted"@@ -3100,13 +3075,11 @@ def test_parent_head_preserves_newer_and_frozen_observations(tmp_path: Path, generic_review_recovery, layout: str) -> None:module, pending, closed, binding = generic_review_recovery- closed_checkpoint = write_checkpoint(tmp_path / "checkpoints", closed)- os.utime(closed_checkpoint, ns=(100, 100))- pending_checkpoint = write_checkpoint(tmp_path / "checkpoints", pending)- os.utime(pending_checkpoint, ns=(300, 300))+ closed_checkpoint = write_checkpoint(tmp_path / "checkpoints", closed, mtime_ns=100)+ pending_checkpoint = write_checkpoint(tmp_path / "checkpoints", pending, mtime_ns=300)head = tmp_path / "checkpoint-head.json"selected = pending_checkpoint if layout == "newer_head" else closed_checkpoint- head.write_text(json.dumps({"checkpoint": selected.name}))+ select_checkpoint(tmp_path, selected)head_time = 400 if layout == "newer_head" else 200os.utime(head, ns=(head_time, head_time))original_head = head.read_bytes()@@ -3167,8 +3140,7 @@ def test_generic_surface_recovery_preserves_unresolved_evidence(pending["coverage"]["deferred"][0].pop("surfaceIds")closed["coverage"]["surfaces"] = [{**old_surface, "disposition": "no_issue_found"}]for draft, modified in ((pending, 100), (closed, 200)):- checkpoint = write_checkpoint(tmp_path / "checkpoints", draft)- os.utime(checkpoint, ns=(modified, modified))+ write_checkpoint(tmp_path / "checkpoints", draft, mtime_ns=modified)latest = copy.deepcopy(closed)latest["coverage"]["surfaces"] = [old_surface]workers = []@@ -3195,8 +3167,7 @@ def test_generic_surface_recovery_preserves_unresolved_evidence((output / "result.json").write_text(json.dumps(latest))workers = [saved_discovery_worker(output, "worker", 1)]if later != "other_worker":- checkpoint = write_checkpoint(tmp_path / "checkpoints", latest)- os.utime(checkpoint, ns=(300, 300))+ write_checkpoint(tmp_path / "checkpoints", latest, mtime_ns=300)result = module.merge_saved_results(tmp_path, pending["scanId"], binding, workers, [], stopped=True, reason="interrupted")@@ -3233,10 +3204,8 @@ def test_worker_head_candidate_outcome_respects_newer_pending(candidate["id"] = "pending-review"observations = [(closed, 100)] if prior_closure else []for draft, modified in [*observations, (pending, pending_time)]:- checkpoint = write_checkpoint(output / "checkpoints", draft)- os.utime(checkpoint, ns=(modified, modified))- (output / "result.json").write_text(json.dumps(pending))- os.utime(output / "result.json", ns=(200, 200))+ write_checkpoint(output / "checkpoints", draft, mtime_ns=modified)+ write_result_draft(output / "result.json", pending, mtime_ns=200)terminal = copy.deepcopy(closed)terminal["coverage"].pop("resolvedDeferred")terminal["coverage"]["surfaces"] = [@@ -3248,11 +3217,8 @@ def test_worker_head_candidate_outcome_respects_newer_pending("receiptRefs": [],}]- selected = write_checkpoint(output / "checkpoints", terminal)- os.utime(selected, ns=(150, 150))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(300, 300))+ selected = write_checkpoint(output / "checkpoints", terminal, mtime_ns=150)+ select_checkpoint(output, selected, mtime_ns=300)workers = [saved_discovery_worker(output, "worker", 1)]first = module.merge_saved_results(tmp_path, pending["scanId"], binding, workers, [], stopped=True, reason="interrupted"@@ -3276,8 +3242,7 @@ def test_frozen_parent_keeps_unrelated_candidate_outcome("reason": "Validation remains.",}pending["coverage"]["deferred"].append(candidate)- checkpoint = write_checkpoint(tmp_path / "checkpoints", pending)- os.utime(checkpoint, ns=(100, 100))+ write_checkpoint(tmp_path / "checkpoints", pending, mtime_ns=100)closed["coverage"]["surfaces"] = [{"id": "candidate-outcome",@@ -3321,11 +3286,8 @@ def test_equal_time_parent_and_head_preserve_pending_on_replay(old.parent.mkdir()old.write_bytes(payload)os.utime(old, ns=(100, 100))- checkpoint = write_checkpoint(old.parent, previous)- os.utime(checkpoint, ns=(100, 100))- head = tmp_path / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(200, 200))+ checkpoint = write_checkpoint(old.parent, previous, mtime_ns=100)+ select_checkpoint(tmp_path, checkpoint, mtime_ns=200)write_saved_parent(tmp_path, canonical, 200)first = module.merge_saved_results(tmp_path, pending["scanId"], binding, [], [], stopped=True, reason="interrupted"@@ -3375,10 +3337,8 @@ def test_selected_candidate_outcome_keeps_its_evidence(]observations = [(closed, 100)] if prior_closure else []for draft, modified in [*observations, (pending, 200)]:- checkpoint = write_checkpoint(output / "checkpoints", draft)- os.utime(checkpoint, ns=(modified, modified))- (output / "result.json").write_text(json.dumps(pending))- os.utime(output / "result.json", ns=(200, 200))+ write_checkpoint(output / "checkpoints", draft, mtime_ns=modified)+ write_result_draft(output / "result.json", pending, mtime_ns=200)terminal = copy.deepcopy(closed)terminal["coverage"].pop("resolvedDeferred")rejection = {@@ -3407,11 +3367,8 @@ def test_selected_candidate_outcome_keeps_its_evidence(other_finding["extensions"]["candidateId"] = "other-candidate"other_finding["identity"]["anchor"] = "another-entry-point"terminal["findings"] = [finding, other_finding]- selected = write_checkpoint(output / "checkpoints", terminal)- os.utime(selected, ns=(300, 300))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(300, 300))+ selected = write_checkpoint(output / "checkpoints", terminal, mtime_ns=300)+ select_checkpoint(output, selected, mtime_ns=300)workers = [saved_discovery_worker(output, "worker", 1)]if not stopped:parent = copy.deepcopy(closed)@@ -3452,14 +3409,10 @@ def test_interrupted_worker_reopening_preserves_candidate_identity(pending["coverage"]["deferred"].append({**candidate, "id": "other-pending-review", "reason": "Another caller remains."})- (output / "result.json").write_text(json.dumps(closed))- os.utime(output / "result.json", ns=(100, 100))+ write_result_draft(output / "result.json", closed, mtime_ns=100)for draft, modified in ((closed, 100), (pending, 200)):- checkpoint = write_checkpoint(output / "checkpoints", draft)- os.utime(checkpoint, ns=(modified, modified))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": checkpoint.name}))- os.utime(head, ns=(200, 200))+ checkpoint = write_checkpoint(output / "checkpoints", draft, mtime_ns=modified)+ select_checkpoint(output, checkpoint, mtime_ns=200)workers = [saved_discovery_worker(output, "worker", 1)]result = module.merge_saved_results(tmp_path, pending["scanId"], binding, workers, [], stopped=True, reason="interrupted"diff --git a/plugins/codex-security/tests/test_workbench_stopped_worker_coverage.py b/plugins/codex-security/tests/test_workbench_stopped_worker_coverage.pyindex ddd60a7de..35fe9205e 100644--- a/plugins/codex-security/tests/test_workbench_stopped_worker_coverage.py+++ b/plugins/codex-security/tests/test_workbench_stopped_worker_coverage.py@@ -1,7 +1,6 @@from __future__ import annotationsimport json-import osimport uuidfrom pathlib import Path@@ -18,9 +17,11 @@ from workbench_test_support import (preserve_scan_results,read_review_state,saved_draft,+ select_checkpoint,upsert_deep_worker,worker_paths,write_checkpoint,+ write_result_draft,)@@ -256,10 +257,11 @@ def test_pending_worker_surface_receipt_precedes_historical_receipt_repair(tmp_p"receiptRefs": ["artifacts/other.md"],}task = {"id": "review-api", "reason": "Review remains.", "surfaceIds": ["api"]}- result.write_text(- json.dumps(saved_draft(scan_id, complete=True, surfaces=[surface, writer], deferred=[task]))+ write_result_draft(+ result,+ saved_draft(scan_id, complete=True, surfaces=[surface, writer], deferred=[task]),+ mtime_ns=100,)- os.utime(result, ns=(100, 100))historical = write_checkpoint(output / "checkpoints",saved_draft(@@ -272,15 +274,14 @@ def test_pending_worker_surface_receipt_precedes_historical_receipt_repair(tmp_p}],),+ mtime_ns=50,)- os.utime(historical, ns=(50, 50))selected = write_checkpoint(- output / "checkpoints", saved_draft(scan_id, complete=True, surfaces=[writer])+ output / "checkpoints",+ saved_draft(scan_id, complete=True, surfaces=[writer]),+ mtime_ns=300,)- os.utime(selected, ns=(300, 300))- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))- os.utime(head, ns=(400, 400))+ head = select_checkpoint(output, selected, mtime_ns=400)originals = {path: path.read_bytes()for path in (result, historical, selected, head, current_receipt, historical_receipt)diff --git a/plugins/codex-security/tests/test_workbench_target.py b/plugins/codex-security/tests/test_workbench_target.pyindex cd79c1582..33b07d5e2 100644--- a/plugins/codex-security/tests/test_workbench_target.py+++ b/plugins/codex-security/tests/test_workbench_target.py@@ -88,6 +88,28 @@ def junction_factory(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPreturn create+def write_reviewed_patch(+ tmp_path: Path, source: Path, patch: bytes, revision: str = "unversioned"+) -> dict[str, object]:+ scan_dir = tmp_path / "scan"+ scan_dir.mkdir(mode=0o700)+ (scan_dir / "reviewed.patch").write_bytes(patch)+ return {+ "target_path": str(source),+ "target_inode": source.stat().st_ino,+ "target_revision": revision,+ "scan_dir": str(scan_dir),+ }+++def reviewed_remediation(patch: bytes, revision: str, digest: str) -> dict[str, str]:+ return {+ "base_revision": revision,+ "base_content_digest": digest,+ "patch_digest": "sha256:" + hashlib.sha256(patch).hexdigest(),+ }++@pytest.mark.parametrize(("git_repository", "git_exclusion"),[@@ -145,24 +167,14 @@ def test_reviewed_patch_preserves_junction_boundaries(if change != "target_populated":(outside / "source.txt").write_text("original contents\n")junction_factory(junction, outside)- scan_dir = tmp_path / "scan"- scan_dir.mkdir(mode=0o700)patch = b"diff --git a/app.txt b/app.txt\n--- a/app.txt\n+++ b/app.txt\n@@ -1 +1 @@\n-before\n+after\n"- (scan_dir / "reviewed.patch").write_bytes(patch)- scan = {- "target_path": str(source),- "target_inode": source.stat().st_ino,- "target_revision": "unversioned",- "scan_dir": str(scan_dir),- }+ scan = write_reviewed_patch(tmp_path, source, patch)(source / "app.txt").write_text("before\n")if git_repository == "indexed":subprocess.run(["git", "add", "."], cwd=source, check=True)- remediation = {- "base_revision": "unversioned",- "base_content_digest": workbench_db.directory_content_digest(source),- "patch_digest": "sha256:" + hashlib.sha256(patch).hexdigest(),- }+ remediation = reviewed_remediation(+ patch, "unversioned", workbench_db.directory_content_digest(source)+ )(source / "app.txt").write_text("after\n")if change in {"junction_target", "same_content_target"}:junction_target = tmp_path / "other"@@ -346,21 +358,11 @@ def test_reviewed_patch_does_not_copy_through_indexed_directory_links((sentinel / "file.txt").write_text("staged outside sentinel\n")# A relative link resolves to this different directory after materialization.monkeypatch.setattr(workbench_db.tempfile, "tempdir", str(staging))- scan_dir = tmp_path / "scan"- scan_dir.mkdir(mode=0o700)patch = b"diff --git a/app.txt b/app.txt\n--- a/app.txt\n+++ b/app.txt\n@@ -1 +1 @@\n-before\n+after\n"- (scan_dir / "reviewed.patch").write_bytes(patch)- scan = {- "target_path": str(source),- "target_inode": source.stat().st_ino,- "target_revision": revision,- "scan_dir": str(scan_dir),- }- remediation = {- "base_revision": revision,- "base_content_digest": workbench_db.worktree_content_digest(source),- "patch_digest": "sha256:" + hashlib.sha256(patch).hexdigest(),- }+ scan = write_reviewed_patch(tmp_path, source, patch, revision)+ remediation = reviewed_remediation(+ patch, revision, workbench_db.worktree_content_digest(source)+ )(source / "app.txt").write_text("after\n")if unrelated_change:(source / "unrelated.txt").write_text("outside the reviewed patch\n")@@ -485,22 +487,10 @@ def assert_reviewed_change())).encode()- scan_dir = tmp_path / "scan"- scan_dir.mkdir(mode=0o700)- (scan_dir / "reviewed.patch").write_bytes(patch)- scan = {- "target_path": str(source),- "target_inode": source.stat().st_ino,- "target_revision": revision,- "scan_dir": str(scan_dir),- }+ scan = write_reviewed_patch(tmp_path, source, patch, revision)(source / relative).write_text(before, newline=newline)revision, digest = workbench_db.remediation_checkout_snapshot(scan)- remediation = {- "base_revision": revision,- "base_content_digest": digest,- "patch_digest": "sha256:" + hashlib.sha256(patch).hexdigest(),- }+ remediation = reviewed_remediation(patch, revision, digest)(source / relative).write_text(after, newline=newline)assert workbench_db.require_reviewed_patch_applied(scan, remediation, "reviewed.patch")@@ -575,21 +565,9 @@ def test_reverse_patch_cannot_write_through_junction((outside / "keep.txt").write_text("outside contents\n")junction = source / "linked"junction_factory(junction, outside)- scan_dir = tmp_path / "scan"- scan_dir.mkdir(mode=0o700)patch = b"diff --git a/linked/deleted.txt b/linked/deleted.txt\ndeleted file mode 100644\n--- a/linked/deleted.txt\n+++ /dev/null\n@@ -1 +0,0 @@\n-removed\n"- (scan_dir / "reviewed.patch").write_bytes(patch)- scan = {- "target_path": str(source),- "target_inode": source.stat().st_ino,- "target_revision": "unversioned",- "scan_dir": str(scan_dir),- }- remediation = {- "base_revision": "unversioned",- "base_content_digest": "different base",- "patch_digest": "sha256:" + hashlib.sha256(patch).hexdigest(),- }+ scan = write_reviewed_patch(tmp_path, source, patch)+ remediation = reviewed_remediation(patch, "unversioned", "different base")with pytest.raises(OSError):workbench_db.require_reviewed_patch_applied(scan, remediation, "reviewed.patch")assert sorted(path.name for path in outside.iterdir()) == ["keep.txt"]@@ -618,8 +596,6 @@ def test_reverse_patch_cannot_redirect_junction_restoration(config = tmp_path / "gitconfig"config.write_text("[core]\n\tsymlinks = true\n")monkeypatch.setenv("GIT_CONFIG_GLOBAL", str(config))- scan_dir = tmp_path / "scan"- scan_dir.mkdir(mode=0o700)patch = f"""diff --git a/app.txt b/app.txt--- a/app.txt+++ b/app.txt@@ -641,19 +617,11 @@ index {"0" * 40}..{"1" * 40}@@ -0,0 +1 @@+Subproject commit {"1" * 40}""".encode()- (scan_dir / "reviewed.patch").write_bytes(patch)- scan = {- "target_path": str(source),- "target_inode": source.stat().st_ino,- "target_revision": "unversioned",- "scan_dir": str(scan_dir),- }+ scan = write_reviewed_patch(tmp_path, source, patch)(source / "app.txt").write_text("before\n")- remediation = {- "base_revision": "unversioned",- "base_content_digest": workbench_db.directory_content_digest(source),- "patch_digest": "sha256:" + hashlib.sha256(patch).hexdigest(),- }+ remediation = reviewed_remediation(+ patch, "unversioned", workbench_db.directory_content_digest(source)+ )(source / "app.txt").write_text("after\n")try:with pytest.raises(SystemExit, match="checkout path was replaced"):@@ -794,26 +762,14 @@ def test_reviewed_patch_preserves_unborn_git_inventory(tmp_path: Path, change: scached_file = cache / "output.txt"cached_file.write_text("ignored build output\n")subprocess.run(["git", "add", ".gitignore", "src/app.txt"], cwd=source, check=True)- scan_dir = tmp_path / "scan"- scan_dir.mkdir(mode=0o700)patch = (b"diff --git a/src/app.txt b/src/app.txt\n"b"--- a/src/app.txt\n+++ b/src/app.txt\n@@ -1 +1 @@\n-before\n+after\n")- (scan_dir / "reviewed.patch").write_bytes(patch)- scan = {- "target_path": str(source),- "target_inode": source.stat().st_ino,- "target_revision": "unversioned",- "scan_dir": str(scan_dir),- }+ scan = write_reviewed_patch(tmp_path, source, patch)revision, digest = workbench_db.remediation_checkout_snapshot(scan)assert revision == "unversioned"- remediation = {- "base_revision": revision,- "base_content_digest": digest,- "patch_digest": "sha256:" + hashlib.sha256(patch).hexdigest(),- }+ remediation = reviewed_remediation(patch, revision, digest)app.write_text("after\n")if change == "ignored_file":cached_file.write_text("updated ignored build output\n")diff --git a/plugins/codex-security/tests/test_worker_receipt_admission.py b/plugins/codex-security/tests/test_worker_receipt_admission.pyindex 76106112f..dbfa24645 100644--- a/plugins/codex-security/tests/test_worker_receipt_admission.py+++ b/plugins/codex-security/tests/test_worker_receipt_admission.py@@ -8,12 +8,16 @@ from argparse import Namespacefrom pathlib import Pathimport pytest-from test_deep_scan_successful_publication import add_workerfrom test_deep_scan_successful_publication import publication_scan as publication_scanfrom test_partial_coverage_projection import stop_and_recover_projectionfrom test_partial_coverage_projection import stop_scan as stop_scanfrom test_saved_deferred_identity import recover, save_worker-from workbench_test_support import read_review_state, saved_draft, write_checkpoint+from workbench_test_support import (+ read_review_state,+ saved_draft,+ select_checkpoint,+ write_checkpoint,+)@pytest.mark.parametrize("reordered", [False, True])@@ -37,8 +41,7 @@ def test_unselected_pending_checkpoint_links_to_accepted_worker_surface(output = tmp_path / "reviewer"result_path = output / "result.json"selected = write_checkpoint(output / "checkpoints", accepted)- head = output / "checkpoint-head.json"- head.write_text(json.dumps({"checkpoint": selected.name}))+ head = select_checkpoint(output, selected)for path in (selected, result_path, head):os.utime(path, ns=(1_000_000_000, 1_000_000_000))pending = copy.deepcopy(accepted)@@ -50,8 +53,7 @@ def test_unselected_pending_checkpoint_links_to_accepted_worker_surface(pending["coverage"]["deferred"] = [{"id": "task", "reason": "Check boundary", "surfaceIds": ["api"]}]- unselected = write_checkpoint(output / "checkpoints", pending)- os.utime(unselected, ns=(2_000_000_000, 2_000_000_000))+ write_checkpoint(output / "checkpoints", pending, mtime_ns=2_000_000_000)originals = {path: path.read_bytes() for path in output.rglob("*.json")}worker.update(status="succeeded", result_manifest_path=str(result_path))recovered = recover(tmp_path, saved, [worker])@@ -71,7 +73,7 @@ def test_stopped_receipt_ownership_requires_a_matching_checkpoint(workbench_api, workbench_db, publication_scan, stop_scan, recorded, retry, archived):scan = publication_scan()- initial = add_worker(workbench_db, scan)+ initial = scan.add_worker()worker_id = initial.parent.nameoutput = scan.scan_dir / "artifacts" / "deep_discovery" / "workers" / worker_id / "output"output.mkdir(parents=True)@@ -209,7 +211,7 @@ def test_legacy_file_scope_preserves_shared_receipts_when_stopped(draft["scanId"] = scan.scan_iddraft["findings"][0]["locations"][0]["path"] = scopelegacy["checkpoint"]["findings"][1]["locations"][0]["path"] = f"{scope}/generated"- initial = add_worker(workbench_db, scan)+ initial = scan.add_worker()worker_id = initial.parent.nameoutput = scan.scan_dir / "artifacts" / "deep_discovery" / "workers" / worker_id / "output"output.mkdir(parents=True)@@ -220,8 +222,9 @@ def test_legacy_file_scope_preserves_shared_receipts_when_stopped((str(output), str(result), worker_id),)result.write_text(json.dumps(legacy["result"]))- checkpoint = write_checkpoint(result.parent / "checkpoints", legacy["checkpoint"])- os.utime(checkpoint, ns=(1_000_000_000, 1_000_000_000))+ checkpoint = write_checkpoint(+ result.parent / "checkpoints", legacy["checkpoint"], mtime_ns=1_000_000_000+ )os.utime(result, ns=(2_000_000_000, 2_000_000_000))ref = "artifacts/evidence.txt"shared, local = scan.scan_dir / ref, result.parent / refdiff --git a/plugins/codex-security/tests/workbench_test_support.py b/plugins/codex-security/tests/workbench_test_support.pyindex 9867eec45..c721c952d 100644--- a/plugins/codex-security/tests/workbench_test_support.py+++ b/plugins/codex-security/tests/workbench_test_support.py@@ -13,6 +13,7 @@ import sysimport tempfileimport uuidfrom contextlib import nullcontext+from dataclasses import dataclass, fieldfrom pathlib import Pathfrom types import ModuleTypefrom typing import Any@@ -118,14 +119,29 @@ def source_plugin_version() -> str:return version-def write_checkpoint(checkpoint_dir: Path, payload: Any) -> Path:+def write_checkpoint(checkpoint_dir: Path, payload: Any, *, mtime_ns: int | None = None) -> Path:encoded = json.dumps(payload).encode()checkpoint_dir.mkdir(parents=True, exist_ok=True)checkpoint_path = checkpoint_dir / f"{hashlib.sha256(encoded).hexdigest()}.json"checkpoint_path.write_bytes(encoded)+ if mtime_ns is not None:+ os.utime(checkpoint_path, ns=(mtime_ns, mtime_ns))return checkpoint_path+def select_checkpoint(output: Path, checkpoint: Path, *, mtime_ns: int | None = None) -> Path:+ head = output / "checkpoint-head.json"+ head.write_text(json.dumps({"checkpoint": checkpoint.name}))+ if mtime_ns is not None:+ os.utime(head, ns=(mtime_ns, mtime_ns))+ return head+++def write_result_draft(path: Path, draft: dict, *, mtime_ns: int) -> None:+ path.write_text(json.dumps(draft))+ os.utime(path, ns=(mtime_ns, mtime_ns))++def sort_checkpoint_entries(monkeypatch, *, key=None, reverse=False):from workbench import saved_sources@@ -500,80 +516,83 @@ def write_remediation_patch(patch_path: Path, **text_options: Any) -> None:)-def request_remediation(- state_dir: Path,- occurrence_id: str,- request_id: str,- action_token: str,- *,- check: bool = True,-) -> dict[str, object]:- return run_workbench(- state_dir,- "request-finding-remediation",- "--occurrence-id",- occurrence_id,- "--request-id",- request_id,- "--action-token",- action_token,- check=check,- )+@dataclass(frozen=True)+class RemediationRequest:+ state_dir: Path+ occurrence_id: str+ id: str = field(default_factory=lambda: str(uuid.uuid4()))++ def _run(self, command: str, *args: str, check: bool = True) -> dict[str, object]:+ return run_workbench(+ self.state_dir,+ command,+ "--occurrence-id",+ self.occurrence_id,+ "--request-id",+ self.id,+ *args,+ check=check,+ )+ def start(self, token: str, *, check: bool = True) -> dict[str, object]:+ return self._run("request-finding-remediation", "--action-token", token, check=check)++ def update(+ self, token: str, version: str, state: str, *args: str, check: bool = True+ ) -> dict[str, object]:+ return self._run(+ "set-finding-remediation",+ "--action-token",+ token,+ "--expected-version",+ version,+ "--state",+ state,+ *args,+ check=check,+ )-def set_remediation(- state_dir: Path,- occurrence_id: str,- request_id: str,- action_token: str,- expected_version: str,- state: str,- *extra: str,- check: bool = True,-) -> dict[str, object]:- return run_workbench(- state_dir,- "set-finding-remediation",- "--occurrence-id",- occurrence_id,- "--request-id",- request_id,- "--action-token",- action_token,- "--expected-version",- expected_version,- "--state",- state,- *extra,- check=check,- )+ def generate(+ self, token: str, patch: Path, *args: str, check: bool = True+ ) -> dict[str, object]:+ digest = hashlib.sha256(patch.read_bytes()).hexdigest()+ return self.update(+ token,+ "1",+ "generated",+ "--patch-path",+ patch.name,+ "--patch-digest",+ f"sha256:{digest}",+ *args,+ check=check,+ )+ def request_action(+ self, version: str, action: str, token: str, *, check: bool = True+ ) -> dict[str, object]:+ return self._run(+ "request-finding-remediation-action",+ "--expected-version",+ version,+ "--action",+ action,+ "--action-token",+ token,+ check=check,+ )-def request_remediation_action(- state_dir: Path,- occurrence_id: str,- request_id: str,- expected_version: str,- action: str,- action_token: str,- *,- check: bool = True,-) -> dict[str, object]:- return run_workbench(- state_dir,- "request-finding-remediation-action",- "--occurrence-id",- occurrence_id,- "--request-id",- request_id,- "--expected-version",- expected_version,- "--action",- action,- "--action-token",- action_token,- check=check,- )+ def claim_resend(self, token: str, *, check: bool = True) -> dict[str, object]:+ return self._run("claim-finding-remediation-resend", "--action-token", token, check=check)++ def cancel(self, token: str) -> dict[str, object]:+ return self._run("cancel-finding-remediation-request", "--action-token", token)++ def mark_delivered(self, token: str) -> dict[str, object]:+ return self._run("mark-finding-remediation-delivered", "--action-token", token)++ def release_claim(self, token: str) -> dict[str, object]:+ return self._run("release-finding-remediation-claim", "--action-token", token)def save_workspace(@@ -625,63 +644,6 @@ def update_progress()-def claim_remediation_resend(- state_dir: Path,- occurrence_id: str,- request_id: str,- action_token: str,- *,- check: bool = True,-) -> dict[str, object]:- return run_workbench(- state_dir,- "claim-finding-remediation-resend",- "--occurrence-id",- occurrence_id,- "--request-id",- request_id,- "--action-token",- action_token,- check=check,- )---def cancel_remediation_request(- state_dir: Path,- occurrence_id: str,- request_id: str,- action_token: str,-) -> dict[str, object]:- return run_workbench(- state_dir,- "cancel-finding-remediation-request",- "--occurrence-id",- occurrence_id,- "--request-id",- request_id,- "--action-token",- action_token,- )---def mark_remediation_delivered(- state_dir: Path,- occurrence_id: str,- request_id: str,- action_token: str,-) -> dict[str, object]:- return run_workbench(- state_dir,- "mark-finding-remediation-delivered",- "--occurrence-id",- occurrence_id,- "--request-id",- request_id,- "--action-token",- action_token,- )--def fail_deep_scan(state_dir, codex_home, scan_id, *, message="Worker stopped.", deep_status=None):return scan_command(state_dir,@@ -696,28 +658,27 @@ def fail_deep_scan(state_dir, codex_home, scan_id, *, message="Worker stopped.",def start_delivered_scan(state_dir: Path,+ workspace_id: str,*args: str,environment: dict[str, str] | None = None,) -> dict[str, object]:"""Prepare an unclaimed, delivered scan for tests outside handoff ownership."""- started = run_workbench(state_dir, "start-scan", *args, environment=environment)- scan = started["results"]+ scan = start_scan_command(state_dir, workspace_id, *args, environment=environment)["results"]with sqlite3.connect(state_dir / "workbench.sqlite3") as connection:connection.execute("UPDATE scans SET handoff_status = 'delivered' WHERE id = ?", (scan["scanId"],))scan["handoffStatus"] = "delivered"- return started+ return scandef start_workspace_scan(state_dir: Path, workspace_id: str, scan_root: Path) -> tuple[str, Path]:started = start_delivered_scan(state_dir,- "--workspace-id",workspace_id,"--scan-root",str(scan_root),- )["results"]+ )return str(started["scanId"]), Path(str(started["scanDir"]))@@ -831,6 +792,14 @@ def mark_deep_coordinator_succeeded(state_dir: Path, scan_id: str, scan_dir: Patreturn manifest+def read_draft_documents(scan_dir: Path) -> dict:+ return {+ "manifest": json.loads((scan_dir / "scan-manifest.json").read_text()),+ "findings": json.loads((scan_dir / "findings.json").read_text()),+ "coverage": json.loads((scan_dir / "coverage.json").read_text()),+ }++def write_completed_contract(scan_dir: Path,scan_id: str,