@openai/codex-security 0.1.16 → 0.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/README.md +228 -18
  2. package/_bundled_plugin/.app.json +1 -1
  3. package/_bundled_plugin/.mcp.json +10 -3
  4. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  5. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  6. package/_bundled_plugin/references/config-preflight.md +1 -1
  7. package/_bundled_plugin/references/core-scan.md +1 -1
  8. package/_bundled_plugin/references/final-report.md +4 -2
  9. package/_bundled_plugin/references/sarif-adapter.md +2 -0
  10. package/_bundled_plugin/references/scan-artifacts.md +1 -1
  11. package/_bundled_plugin/references/scan-contract.md +18 -7
  12. package/_bundled_plugin/schemas/scan-manifest.schema.json +2 -2
  13. package/_bundled_plugin/schemas/tools/scan-draft.schema.json +23 -0
  14. package/_bundled_plugin/scripts/deep_scan_workbench.py +122 -7
  15. package/_bundled_plugin/scripts/finalize_scan_contract.py +106 -38
  16. package/_bundled_plugin/scripts/generate_in_scope_files.py +20 -0
  17. package/_bundled_plugin/scripts/generate_rank_input.py +23 -7
  18. package/_bundled_plugin/scripts/launch_codex_security_mcp +32 -0
  19. package/_bundled_plugin/scripts/launch_codex_security_mcp.cmd +27 -0
  20. package/_bundled_plugin/scripts/report_projection.py +19 -8
  21. package/_bundled_plugin/scripts/workbench_cli.py +35 -15
  22. package/_bundled_plugin/scripts/workbench_db.py +184 -202
  23. package/_bundled_plugin/scripts/workbench_finding_index.py +101 -0
  24. package/_bundled_plugin/scripts/workbench_progress.py +6 -3
  25. package/_bundled_plugin/scripts/workbench_remediation.py +19 -0
  26. package/_bundled_plugin/scripts/workbench_saved_results.py +1289 -0
  27. package/_bundled_plugin/scripts/workbench_scan_history.py +17 -2
  28. package/_bundled_plugin/scripts/workbench_schema.py +28 -0
  29. package/_bundled_plugin/scripts/workbench_target.py +36 -5
  30. package/_bundled_plugin/scripts/workbench_validation.py +9 -0
  31. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +4 -4
  32. package/_bundled_plugin/skills/fix-finding/SKILL.md +17 -1
  33. package/_bundled_plugin/skills/security-diff-scan/SKILL.md +1 -1
  34. package/_bundled_plugin/skills/security-scan/SKILL.md +1 -1
  35. package/_bundled_plugin/skills/security-scan/references/desktop-scan.md +1 -1
  36. package/dist/api.d.ts +18 -1
  37. package/dist/api.d.ts.map +1 -1
  38. package/dist/api.js +234 -18
  39. package/dist/api.js.map +1 -1
  40. package/dist/cli.d.ts +5 -3
  41. package/dist/cli.d.ts.map +1 -1
  42. package/dist/cli.js +348 -43
  43. package/dist/cli.js.map +1 -1
  44. package/dist/component-plan.d.ts +15 -0
  45. package/dist/component-plan.d.ts.map +1 -0
  46. package/dist/component-plan.js +145 -0
  47. package/dist/component-plan.js.map +1 -0
  48. package/dist/component-scan.d.ts +83 -0
  49. package/dist/component-scan.d.ts.map +1 -0
  50. package/dist/component-scan.js +279 -0
  51. package/dist/component-scan.js.map +1 -0
  52. package/dist/contract.d.ts +8 -2
  53. package/dist/contract.d.ts.map +1 -1
  54. package/dist/contract.js +27 -8
  55. package/dist/contract.js.map +1 -1
  56. package/dist/cost-model.d.ts +20 -0
  57. package/dist/cost-model.d.ts.map +1 -0
  58. package/dist/cost-model.js +86 -0
  59. package/dist/cost-model.js.map +1 -0
  60. package/dist/cost.d.ts +2 -11
  61. package/dist/cost.d.ts.map +1 -1
  62. package/dist/cost.js +15 -98
  63. package/dist/cost.js.map +1 -1
  64. package/dist/custom-validation-prompt.d.ts +4 -0
  65. package/dist/custom-validation-prompt.d.ts.map +1 -0
  66. package/dist/custom-validation-prompt.js +94 -0
  67. package/dist/custom-validation-prompt.js.map +1 -0
  68. package/dist/custom-validation.d.ts +45 -0
  69. package/dist/custom-validation.d.ts.map +1 -0
  70. package/dist/custom-validation.js +292 -0
  71. package/dist/custom-validation.js.map +1 -0
  72. package/dist/errors.d.ts +1 -1
  73. package/dist/errors.d.ts.map +1 -1
  74. package/dist/errors.js +1 -1
  75. package/dist/errors.js.map +1 -1
  76. package/dist/index.d.ts +6 -1
  77. package/dist/index.d.ts.map +1 -1
  78. package/dist/index.js +2 -0
  79. package/dist/index.js.map +1 -1
  80. package/dist/models.d.ts +1 -1
  81. package/dist/models.d.ts.map +1 -1
  82. package/dist/models.js.map +1 -1
  83. package/dist/multiscan.d.ts +1 -0
  84. package/dist/multiscan.d.ts.map +1 -1
  85. package/dist/multiscan.js +53 -5
  86. package/dist/multiscan.js.map +1 -1
  87. package/dist/patch-tui.js +1 -1
  88. package/dist/patch-tui.js.map +1 -1
  89. package/dist/publication.d.ts.map +1 -1
  90. package/dist/publication.js +3 -4
  91. package/dist/publication.js.map +1 -1
  92. package/dist/runtime.d.ts +4 -2
  93. package/dist/runtime.d.ts.map +1 -1
  94. package/dist/runtime.js +109 -38
  95. package/dist/runtime.js.map +1 -1
  96. package/dist/scan-comparison.d.ts +12 -5
  97. package/dist/scan-comparison.d.ts.map +1 -1
  98. package/dist/scan-comparison.js +72 -28
  99. package/dist/scan-comparison.js.map +1 -1
  100. package/dist/scan-dashboard.d.ts +9 -2
  101. package/dist/scan-dashboard.d.ts.map +1 -1
  102. package/dist/scan-dashboard.js +198 -3
  103. package/dist/scan-dashboard.js.map +1 -1
  104. package/dist/scan-logs.d.ts.map +1 -1
  105. package/dist/scan-logs.js +5 -21
  106. package/dist/scan-logs.js.map +1 -1
  107. package/dist/scan-sessions.d.ts +4 -0
  108. package/dist/scan-sessions.d.ts.map +1 -0
  109. package/dist/scan-sessions.js +35 -0
  110. package/dist/scan-sessions.js.map +1 -0
  111. package/dist/targets.d.ts.map +1 -1
  112. package/dist/targets.js +16 -1
  113. package/dist/targets.js.map +1 -1
  114. package/dist/version.d.ts.map +1 -1
  115. package/dist/version.js +2 -20
  116. package/dist/version.js.map +1 -1
  117. package/dist/windows-path.d.ts +3 -0
  118. package/dist/windows-path.d.ts.map +1 -0
  119. package/dist/windows-path.js +16 -0
  120. package/dist/windows-path.js.map +1 -0
  121. package/dist/worker-progress.d.ts +1 -0
  122. package/dist/worker-progress.d.ts.map +1 -1
  123. package/dist/worker-progress.js +12 -0
  124. package/dist/worker-progress.js.map +1 -1
  125. package/package.json +8 -2
@@ -37,6 +37,7 @@ import deep_scan_workbench as deep_scan
37
37
  import workbench_native_indexes as native_indexes
38
38
  import workbench_progress as progress
39
39
  import workbench_remediation as remediation
40
+ import workbench_saved_results as saved_results
40
41
  import workbench_scan_history as scan_history
41
42
  import workbench_scan_usage as scan_usage
42
43
  from filesystem_identity import serialize_filesystem_identity as serialize_filesystem_identity
@@ -46,6 +47,7 @@ from filesystem_identity import (
46
47
  from finalize_scan_contract import (
47
48
  PRODUCER_NAME,
48
49
  ContractError,
50
+ RecoverableContractError,
49
51
  _prepare_scan_finalization,
50
52
  _write_prepared_scan_finalization,
51
53
  csv_cell,
@@ -78,6 +80,7 @@ from workbench_constants import (
78
80
  SQLITE_RETRY_ATTEMPTS,
79
81
  )
80
82
  from workbench_feedback import get_scan_feedback
83
+ from workbench_finding_index import index_findings
81
84
  from workbench_remediation import remediation_claim_is_active
82
85
  from workbench_scan_start import (
83
86
  archive_scan,
@@ -104,12 +107,14 @@ from workbench_target import (
104
107
  copy_git_worktree_files,
105
108
  directory_content_digest,
106
109
  directory_snapshot_regular_file_count,
110
+ git_bytes,
107
111
  git_command,
108
112
  git_output,
109
113
  git_revision,
110
114
  git_submodule_paths,
111
115
  git_target_metadata,
112
116
  git_worktree_context,
117
+ remediation_checkout_snapshot,
113
118
  require_git_worktree_head,
114
119
  require_remediation_target,
115
120
  require_scan_target_identity,
@@ -127,7 +132,7 @@ from workbench_validation import (
127
132
  require_occurrence,
128
133
  require_uuid,
129
134
  sqlite_busy,
130
- user_text,
135
+ user_context_argument,
131
136
  )
132
137
 
133
138
  FINDING_ARTIFACT_DIRECTORIES_LIMIT = 80
@@ -314,11 +319,11 @@ def require_diff_target(
314
319
  }
315
320
  if kind == "commit":
316
321
  head = resolve_git_commit(target, head_revision or "", "Commit")
317
- commit = git_output(target, "cat-file", "-p", head)
322
+ commit = git_bytes(target, "cat-file", "-p", head)
318
323
  if commit is None:
319
324
  raise SystemExit(f"Commit is not available in the local checkout: {head}")
320
325
  parent_line = next(
321
- (line for line in commit.splitlines() if line.startswith("parent ")),
326
+ (line for line in commit.splitlines() if line.startswith(b"parent ")),
322
327
  None,
323
328
  )
324
329
  if parent_line is None:
@@ -326,7 +331,7 @@ def require_diff_target(
326
331
  else:
327
332
  parent = resolve_git_commit(
328
333
  target,
329
- parent_line.removeprefix("parent ").strip(),
334
+ parent_line.removeprefix(b"parent ").decode("ascii").strip(),
330
335
  "Commit parent",
331
336
  )
332
337
  if base_revision and base_revision != parent:
@@ -602,17 +607,15 @@ def verify_manifest_binding(scan: sqlite3.Row, manifest: dict[str, Any]) -> None
602
607
 
603
608
  def require_scope(scope: str, mode: str, target: Path) -> str:
604
609
  value = scope.strip() or "."
605
- if "\\" in value:
610
+ requested_scope = Path(value)
611
+ if "\\" in value and (os.name != "nt" or not requested_scope.is_absolute()):
606
612
  raise SystemExit("Scan scope must use repository-relative POSIX paths.")
607
- parsed = PurePosixPath(value)
608
- if ".." in parsed.parts:
613
+ if ".." in requested_scope.parts:
609
614
  raise SystemExit("Scan scope must stay inside the scanned target.")
610
615
  try:
611
616
  resolved_scope = (
612
- Path(parsed.as_posix()).resolve()
613
- if parsed.is_absolute()
614
- else (target / parsed.as_posix()).resolve()
615
- )
617
+ requested_scope if requested_scope.is_absolute() else target / requested_scope
618
+ ).resolve()
616
619
  relative_scope = resolved_scope.relative_to(target)
617
620
  except (RuntimeError, ValueError) as exc:
618
621
  raise SystemExit("Scan scope must stay inside the scanned target.") from exc
@@ -716,7 +719,7 @@ def create_workspace(connection: sqlite3.Connection, args: argparse.Namespace) -
716
719
  optional_text(args.target_summary, maximum=2400),
717
720
  default_scope,
718
721
  args.mode,
719
- user_text(args.user_context),
722
+ user_context_argument(args),
720
723
  diff_target_kind,
721
724
  diff_base_revision,
722
725
  diff_head_revision,
@@ -775,7 +778,7 @@ def save_workspace(connection: sqlite3.Connection, args: argparse.Namespace) ->
775
778
  target_summary,
776
779
  scope,
777
780
  args.mode,
778
- user_text(args.user_context),
781
+ user_context_argument(args),
779
782
  diff_target["kind"] if diff_target else None,
780
783
  diff_target["baseRevision"] if diff_target else None,
781
784
  diff_target["headRevision"] if diff_target else None,
@@ -940,7 +943,7 @@ def _start_prompt_driven_scan(
940
943
  target_path = str(target)
941
944
  scope = inspected["scope"]
942
945
  diff_target = inspected["diffTarget"]
943
- user_context = user_text(args.user_context)
946
+ user_context = user_context_argument(args)
944
947
  target_summary = optional_text(args.target_summary, maximum=2400)
945
948
  if diff_target is not None and not target_summary:
946
949
  target_summary = diff_target_summary(diff_target)
@@ -1467,6 +1470,24 @@ def complete_scan_locked(
1467
1470
  scan_dir = require_canonical_scan_directory(Path(scan["scan_dir"]))
1468
1471
  completion_timestamp = now()
1469
1472
  completion_binding = workbench_completion_binding(scan, completion_timestamp)
1473
+ current_manifest_path = artifact_path(scan_dir, ARTIFACTS["manifest"], required=False)
1474
+ if current_manifest_path is not None:
1475
+ current_manifest = read_json_object(current_manifest_path)
1476
+ if (
1477
+ isinstance(current_manifest.get("scan"), dict)
1478
+ and current_manifest["scan"].get("complete") is False
1479
+ ):
1480
+ raise SystemExit(
1481
+ "The latest saved scan draft is incomplete; continue the scan before completing it."
1482
+ )
1483
+ already_sealed = (
1484
+ current_manifest_path is not None
1485
+ and isinstance(current_manifest.get("scan"), dict)
1486
+ and (
1487
+ current_manifest["scan"].get("sealedAt") is not None
1488
+ or current_manifest["scan"].get("artifacts") is not None
1489
+ )
1490
+ )
1470
1491
  if scan["recipe_json"] is not None:
1471
1492
  draft_artifacts: dict[str, Path] = {}
1472
1493
  missing_drafts = []
@@ -1494,16 +1515,38 @@ def complete_scan_locked(
1494
1515
  if isinstance(manifest_scan, dict) and manifest_scan.get("sealedAt") is not None:
1495
1516
  completion_binding["startedAt"] = manifest_scan.get("startedAt")
1496
1517
  completion_binding["completedAt"] = manifest_scan.get("completedAt")
1518
+ wrote = False
1497
1519
  try:
1498
1520
  prepared = _prepare_scan_finalization(
1499
1521
  scan_dir,
1500
1522
  expected_coverage_mode=expected_coverage_mode(scan),
1501
1523
  completion_binding=completion_binding,
1502
1524
  completion_warnings=warnings,
1525
+ draft_documents=saved_results.merge_saved_results(
1526
+ scan_dir,
1527
+ scan["id"],
1528
+ completion_binding,
1529
+ connection.execute(
1530
+ "SELECT * FROM deep_scan_workers WHERE scan_id = ? ORDER BY created_at, id",
1531
+ (scan["id"],),
1532
+ ).fetchall(),
1533
+ warnings,
1534
+ stopped=False,
1535
+ reason="",
1536
+ )
1537
+ if current_manifest_path is not None and not already_sealed
1538
+ else None,
1503
1539
  )
1504
1540
  add_warning()
1541
+ wrote = True
1505
1542
  manifest, findings, _ = _write_prepared_scan_finalization(prepared)
1506
1543
  except ContractError as exc:
1544
+ if wrote or (
1545
+ scan["mode"] == "deep" and not isinstance(exc, RecoverableContractError)
1546
+ ):
1547
+ args = argparse.Namespace(claim_token=claim_token, cost_json=cost_json)
1548
+ args.message, args.scan_id = str(exc), scan_id
1549
+ fail_scan_locked(connection, args)
1507
1550
  raise SystemExit(str(exc)) from exc
1508
1551
  artifacts = {
1509
1552
  kind: artifact_path(scan_dir, filename, required=True)
@@ -1606,7 +1649,14 @@ def register_cli_scan(connection: sqlite3.Connection, args: argparse.Namespace)
1606
1649
  if next(scan_dir.iterdir(), None) is not None:
1607
1650
  raise SystemExit("The scan artifact directory must be empty before the scan starts.")
1608
1651
 
1609
- recipe = parse_scan_recipe(args.recipe_json, repository)
1652
+ user_context = None
1653
+ if args.registration_json_stdin:
1654
+ registration = json.load(sys.stdin)
1655
+ recipe_json = json.dumps(registration["recipe"], ensure_ascii=False, separators=(",", ":"))
1656
+ user_context = registration.get("userContext")
1657
+ else:
1658
+ recipe_json = sys.stdin.read() if args.recipe_json_stdin else args.recipe_json
1659
+ recipe = parse_scan_recipe(recipe_json, repository)
1610
1660
  requested_target = recipe["target"]
1611
1661
  paths = requested_target["paths"]
1612
1662
  scope = paths[0] if len(paths) == 1 else "."
@@ -1691,10 +1741,11 @@ def register_cli_scan(connection: sqlite3.Connection, args: argparse.Namespace)
1691
1741
  scan_dir=scan_dir,
1692
1742
  )
1693
1743
  connection.execute(
1694
- "UPDATE scans SET recipe_json = ?, parent_scan_id = ? WHERE id = ?",
1744
+ "UPDATE scans SET recipe_json = ?, parent_scan_id = ?, user_context = ? WHERE id = ?",
1695
1745
  (
1696
1746
  json.dumps(recipe, allow_nan=False, separators=(",", ":"), sort_keys=True),
1697
1747
  parent_scan_id,
1748
+ user_context,
1698
1749
  scan_id,
1699
1750
  ),
1700
1751
  )
@@ -1785,101 +1836,63 @@ def get_scan_recipe(connection: sqlite3.Connection, args: argparse.Namespace) ->
1785
1836
  }
1786
1837
 
1787
1838
 
1839
+ _WORKBENCH_DB_CONTEXT: saved_results.WorkbenchDbContext
1840
+
1841
+
1788
1842
  def coverage_for_comparison(scan: sqlite3.Row) -> dict[str, Any]:
1789
- if scan["seal_manifest_digest"] is None:
1790
- raise SystemExit("Only sealed scans can be compared.")
1791
- scan_dir = require_canonical_scan_directory(Path(scan["scan_dir"]))
1792
- require_recorded_manifest_digest(scan, scan_dir)
1793
- try:
1794
- _, _, manifest, _, coverage, was_sealed, _ = _prepare_scan_finalization(scan_dir)
1795
- except ContractError as exc:
1796
- raise SystemExit(str(exc)) from exc
1797
- if not was_sealed or manifest["scan"]["id"] != scan["id"]:
1798
- raise SystemExit("Only sealed scans can be compared.")
1799
- return coverage
1843
+ return saved_results.coverage_for_comparison(_WORKBENCH_DB_CONTEXT, scan)
1844
+
1845
+
1846
+ def preserve_scan_results_locked(connection: sqlite3.Connection, scan_id: str) -> bool:
1847
+ return saved_results.preserve_scan_results_locked(_WORKBENCH_DB_CONTEXT, connection, scan_id)
1848
+
1849
+
1850
+ def refresh_stopped_scan_results(
1851
+ connection: sqlite3.Connection, scan_id: str, *, strict: bool = False
1852
+ ) -> None:
1853
+ saved_results.refresh_stopped_scan_results(
1854
+ _WORKBENCH_DB_CONTEXT, connection, scan_id, strict=strict
1855
+ )
1856
+
1857
+
1858
+ def refresh_all_stopped_scan_results(connection: sqlite3.Connection) -> None:
1859
+ scan_ids = [
1860
+ row["id"] for row in connection.execute("SELECT id FROM scans WHERE status = 'failed'")
1861
+ ]
1862
+ for scan_id in scan_ids:
1863
+ refresh_stopped_scan_results(connection, scan_id)
1864
+
1865
+
1866
+ def preserve_scan_results(
1867
+ connection: sqlite3.Connection, args: argparse.Namespace
1868
+ ) -> dict[str, Any]:
1869
+ return saved_results.preserve_scan_results(_WORKBENCH_DB_CONTEXT, connection, args)
1870
+
1871
+
1872
+ def write_scan_draft(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1873
+ return saved_results.write_scan_draft(_WORKBENCH_DB_CONTEXT, connection, args)
1800
1874
 
1801
1875
 
1802
1876
  def fail_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1803
- scan_id = require_uuid(args.scan_id, "scan-id")
1804
- cost_json = parse_scan_cost(args.cost_json)
1805
- connection.execute("BEGIN IMMEDIATE")
1806
- try:
1807
- timestamp = now()
1808
- scan = require_scan(connection, scan_id)
1809
- if scan["status"] == "failed":
1810
- connection.commit()
1811
- return scan_context(connection, scan["id"])
1812
- if scan["status"] == "complete":
1813
- raise SystemExit("A completed scan cannot be marked failed.")
1814
- handoff.require_current_continuation(
1815
- scan,
1816
- args.claim_token,
1817
- error_message="Scan failure is owned by another continuation.",
1818
- )
1819
- message = optional_text(args.message, maximum=2400)
1820
- updated = connection.execute(
1821
- """
1822
- UPDATE scans
1823
- SET status = 'failed', failure_message = ?, completed_at = ?, updated_at = ?,
1824
- cost_json = ?
1825
- WHERE id = ? AND status = 'running'
1826
- """,
1827
- (message, timestamp, timestamp, cost_json, scan["id"]),
1828
- )
1829
- if updated.rowcount != 1:
1830
- raise SystemExit("Only a running scan can be marked failed.")
1831
- deep_scan.fail_from_parent_scan(connection, scan["id"], message, timestamp)
1832
- progress_updated = connection.execute(
1833
- "UPDATE scan_progress SET updated_at = ? WHERE scan_id = ?",
1834
- (timestamp, scan["id"]),
1835
- )
1836
- if progress_updated.rowcount != 1:
1837
- raise SystemExit("Codex Security scan progress not found.")
1838
- connection.commit()
1839
- except BaseException:
1840
- connection.rollback()
1841
- raise
1842
- return scan_context(connection, scan["id"])
1877
+ return saved_results.fail_scan(_WORKBENCH_DB_CONTEXT, connection, args)
1878
+
1879
+
1880
+ def fail_scan_locked(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1881
+ return saved_results.fail_scan_locked(_WORKBENCH_DB_CONTEXT, connection, args)
1843
1882
 
1844
1883
 
1845
1884
  def cancel_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1846
- scan_id = require_uuid(args.scan_id, "scan-id")
1847
- thread_id = optional_text(args.thread_id, maximum=512)
1848
- connection.execute("BEGIN IMMEDIATE")
1849
- try:
1850
- timestamp = now()
1851
- scan = require_scan(connection, scan_id)
1852
- workspace = require_workspace(connection, scan["workspace_id"])
1853
- owning_thread_id = scan["continuation_thread_id"] or workspace["thread_id"]
1854
- if thread_id is not None and owning_thread_id != thread_id:
1855
- raise SystemExit("A scan can only be canceled from its owning Codex thread.")
1856
- if scan["canceled_at"] is not None:
1857
- connection.commit()
1858
- return workspace_state(connection, scan["workspace_id"])
1859
- if scan["status"] != "running":
1860
- raise SystemExit("Only a running scan can be canceled.")
1861
- updated = connection.execute(
1862
- """
1863
- UPDATE scans
1864
- SET status = 'failed', canceled_at = ?, completed_at = ?, updated_at = ?
1865
- WHERE id = ? AND status = 'running'
1866
- """,
1867
- (timestamp, timestamp, timestamp, scan["id"]),
1868
- )
1869
- if updated.rowcount != 1:
1870
- raise SystemExit("Only a running scan can be canceled.")
1871
- deep_scan.cancel_from_parent_scan(connection, scan["id"], timestamp)
1872
- progress_updated = connection.execute(
1873
- "UPDATE scan_progress SET updated_at = ? WHERE scan_id = ?",
1874
- (timestamp, scan["id"]),
1875
- )
1876
- if progress_updated.rowcount != 1:
1877
- raise SystemExit("Codex Security scan progress not found.")
1878
- connection.commit()
1879
- except BaseException:
1880
- connection.rollback()
1881
- raise
1882
- return workspace_state(connection, scan["workspace_id"])
1885
+ return saved_results.cancel_scan(_WORKBENCH_DB_CONTEXT, connection, args)
1886
+
1887
+
1888
+ def cancel_scan_locked(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1889
+ return saved_results.cancel_scan_locked(_WORKBENCH_DB_CONTEXT, connection, args)
1890
+
1891
+
1892
+ def preserve_stopped_results_after_transition(connection: sqlite3.Connection, scan_id: str) -> None:
1893
+ saved_results.preserve_stopped_results_after_transition(
1894
+ _WORKBENCH_DB_CONTEXT, connection, scan_id
1895
+ )
1883
1896
 
1884
1897
 
1885
1898
  def set_finding_triage(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
@@ -2658,9 +2671,14 @@ def record_linear_publications(
2658
2671
 
2659
2672
 
2660
2673
  def export_findings(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
2674
+ refresh_stopped_scan_results(connection, args.scan_id, strict=True)
2661
2675
  scan = require_scan(connection, args.scan_id)
2662
- if scan["status"] != "complete":
2663
- raise SystemExit("Findings can be exported after the scan completes.")
2676
+ if scan["status"] != "complete" and not (
2677
+ scan["status"] == "failed" and scan["seal_manifest_digest"]
2678
+ ):
2679
+ raise SystemExit(
2680
+ "Findings can be exported after the scan completes or preserves stopped results."
2681
+ )
2664
2682
  scan_dir = require_canonical_scan_directory(Path(scan["scan_dir"]))
2665
2683
  require_recorded_manifest_digest(scan, scan_dir)
2666
2684
  verify_manifest_binding(scan, read_json_object(scan_dir / ARTIFACTS["manifest"]))
@@ -2842,24 +2860,6 @@ def require_pending_remediation_action(current: sqlite3.Row, requested: str) ->
2842
2860
  )
2843
2861
 
2844
2862
 
2845
- def remediation_checkout_snapshot(
2846
- scan: sqlite3.Row, *, expected_revision: str | None = None
2847
- ) -> tuple[str, str | None]:
2848
- target = require_scan_target_identity(scan)
2849
- revision = git_revision(target)
2850
- required_revision = expected_revision or scan["target_revision"]
2851
- if revision != required_revision:
2852
- raise SystemExit(
2853
- "Repository HEAD changed. Regenerate the remediation patch against the current checkout."
2854
- )
2855
- content_digest = (
2856
- worktree_content_digest(target)
2857
- if revision != "unversioned"
2858
- else directory_content_digest(target, excluded=(Path(scan["scan_dir"]),))
2859
- )
2860
- return revision, content_digest
2861
-
2862
-
2863
2863
  def require_reviewed_patch_applied(
2864
2864
  scan: sqlite3.Row, remediation: sqlite3.Row, patch_path: str
2865
2865
  ) -> str | None:
@@ -2906,7 +2906,7 @@ def require_reviewed_patch_applied(
2906
2906
  applied = git_command(
2907
2907
  checkout if unversioned else checkout_root,
2908
2908
  *arguments,
2909
- text=True,
2909
+ text=False,
2910
2910
  git_dir=Path(git_dir) if git_dir is not None else None,
2911
2911
  work_tree=checkout_root if git_dir is not None else None,
2912
2912
  )
@@ -2924,6 +2924,14 @@ def require_reviewed_patch_applied(
2924
2924
  work_tree=checkout_root,
2925
2925
  )
2926
2926
  )
2927
+ if reverted_digest != remediation["base_content_digest"] and unversioned:
2928
+ checkout = Path(temporary) / "checkout-lf"
2929
+ copy_directory_excluding(target, checkout, excluded)
2930
+ applied_without_conversion = git_command(
2931
+ checkout, "-c", "core.autocrlf=input", *arguments, text=True
2932
+ )
2933
+ if applied_without_conversion.returncode == 0:
2934
+ reverted_digest = directory_content_digest(checkout)
2927
2935
  if reverted_digest != remediation["base_content_digest"]:
2928
2936
  raise SystemExit(
2929
2937
  "The selected checkout contains changes outside the reviewed patch. Remove them before recording the patch as applied."
@@ -3006,82 +3014,6 @@ def open_scan_local_file(scan_dir: Path, relative_path: str) -> Any:
3006
3014
  raise SystemExit("Patch path must identify a scan-local regular file.") from exc
3007
3015
 
3008
3016
 
3009
- def index_findings(
3010
- connection: sqlite3.Connection,
3011
- scan_id: str,
3012
- document: dict[str, Any],
3013
- timestamp: str,
3014
- ) -> None:
3015
- findings = document.get("findings")
3016
- if not isinstance(findings, list):
3017
- raise SystemExit("findings.json must contain a findings array.")
3018
- for finding in findings:
3019
- if not isinstance(finding, dict):
3020
- raise SystemExit("findings.json entries must be objects.")
3021
- identity = finding["identity"]
3022
- fingerprints = finding["fingerprints"]
3023
- severity = finding["severity"]
3024
- confidence = finding["confidence"]
3025
- connection.execute(
3026
- """
3027
- INSERT INTO findings (
3028
- id, fingerprint, rule_id, identity_anchor, identity_instance, created_at, updated_at
3029
- ) VALUES (?, ?, ?, ?, ?, ?, ?)
3030
- ON CONFLICT(id) DO UPDATE SET
3031
- fingerprint = excluded.fingerprint,
3032
- rule_id = excluded.rule_id,
3033
- identity_anchor = excluded.identity_anchor,
3034
- identity_instance = excluded.identity_instance,
3035
- updated_at = excluded.updated_at
3036
- """,
3037
- (
3038
- finding["findingId"],
3039
- fingerprints["primary"],
3040
- finding["ruleId"],
3041
- identity["anchor"],
3042
- identity.get("instance"),
3043
- timestamp,
3044
- timestamp,
3045
- ),
3046
- )
3047
- connection.execute(
3048
- """
3049
- INSERT INTO finding_occurrences (
3050
- id, finding_id, scan_id, title, summary, severity, confidence, remediation,
3051
- details_json, created_at
3052
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
3053
- """,
3054
- (
3055
- finding["occurrenceId"],
3056
- finding["findingId"],
3057
- scan_id,
3058
- finding["title"],
3059
- finding["summary"],
3060
- severity["level"],
3061
- confidence["level"],
3062
- finding["remediation"],
3063
- json.dumps(finding, allow_nan=False, sort_keys=True),
3064
- timestamp,
3065
- ),
3066
- )
3067
- for index, location in enumerate(finding["locations"]):
3068
- connection.execute(
3069
- """
3070
- INSERT INTO finding_locations (
3071
- occurrence_id, relative_path, start_line, end_line, role, sort_order
3072
- ) VALUES (?, ?, ?, ?, ?, ?)
3073
- """,
3074
- (
3075
- finding["occurrenceId"],
3076
- location["path"],
3077
- location["startLine"],
3078
- location.get("endLine", location["startLine"]),
3079
- location.get("role"),
3080
- index,
3081
- ),
3082
- )
3083
-
3084
-
3085
3017
  def diff_target_summary(diff_target: dict[str, str]) -> str:
3086
3018
  kind = diff_target["kind"]
3087
3019
  if kind == "working_tree":
@@ -3097,6 +3029,7 @@ def workspace_state(
3097
3029
  *,
3098
3030
  result_scan_id: str | None = None,
3099
3031
  thread_id: str | None = None,
3032
+ refresh_stopped: bool = False,
3100
3033
  ) -> dict[str, Any]:
3101
3034
  workspace = require_workspace(connection, workspace_id)
3102
3035
  if thread_id is not None and workspace["thread_id"] != optional_text(thread_id, maximum=512):
@@ -3117,6 +3050,8 @@ def workspace_state(
3117
3050
  }
3118
3051
  selected_scan_id = result_scan_id or workspace["active_scan_id"]
3119
3052
  if selected_scan_id:
3053
+ if refresh_stopped:
3054
+ refresh_stopped_scan_results(connection, selected_scan_id)
3120
3055
  selected_scan = require_scan(connection, selected_scan_id)
3121
3056
  result["userContext"] = selected_scan["user_context"]
3122
3057
  result["results"] = scan_result(connection, selected_scan)
@@ -3178,6 +3113,7 @@ def scan_context(
3178
3113
 
3179
3114
 
3180
3115
  def list_findings(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
3116
+ refresh_stopped_scan_results(connection, args.scan_id)
3181
3117
  scan = require_scan(connection, args.scan_id)
3182
3118
  backfill_legacy_finding_details(connection, scan)
3183
3119
  limit = min(args.limit, FINDINGS_PAGE_MAX)
@@ -3337,6 +3273,8 @@ def scan_result(
3337
3273
 
3338
3274
 
3339
3275
  def remediation_availability(scan: sqlite3.Row) -> tuple[bool, str | None]:
3276
+ if scan["status"] != "complete":
3277
+ return False, remediation.SCAN_STATUS_ERROR
3340
3278
  try:
3341
3279
  current_revision = git_revision(require_scan_target_identity(scan))
3342
3280
  except SystemExit as exc:
@@ -3817,7 +3755,34 @@ def reject_non_finite_json(value: str) -> None:
3817
3755
  raise ValueError(f"non-finite JSON number {value!r} is not supported")
3818
3756
 
3819
3757
 
3758
+ _WORKBENCH_DB_CONTEXT = saved_results.WorkbenchDbContext(
3759
+ ARTIFACTS=ARTIFACTS,
3760
+ artifact_path=artifact_path,
3761
+ deep_scan=deep_scan,
3762
+ expected_coverage_mode=expected_coverage_mode,
3763
+ handoff=handoff,
3764
+ index_findings=index_findings,
3765
+ now=now,
3766
+ optional_text=optional_text,
3767
+ parse_scan_cost=parse_scan_cost,
3768
+ published_manifest_digest=published_manifest_digest,
3769
+ read_json_object=read_json_object,
3770
+ require_canonical_scan_directory=require_canonical_scan_directory,
3771
+ require_recorded_manifest_digest=require_recorded_manifest_digest,
3772
+ require_scan=require_scan,
3773
+ require_uuid=require_uuid,
3774
+ require_workspace=require_workspace,
3775
+ scan_completion_lock=scan_completion_lock,
3776
+ scan_context=scan_context,
3777
+ verify_manifest_binding=verify_manifest_binding,
3778
+ workbench_completion_binding=workbench_completion_binding,
3779
+ workspace_state=workspace_state,
3780
+ )
3781
+
3782
+
3820
3783
  def main() -> None:
3784
+ # Workbench callers send UTF-8 even when Windows uses a legacy code page.
3785
+ sys.stdin.reconfigure(encoding="utf-8")
3821
3786
  args = parse_args(__doc__)
3822
3787
  deep_scan.configure(
3823
3788
  deep_scan.DeepScanDependencies(
@@ -3834,6 +3799,7 @@ def main() -> None:
3834
3799
  safe_segment=safe_segment,
3835
3800
  compact_timestamp=compact_timestamp,
3836
3801
  scan_completion_lock=scan_completion_lock,
3802
+ preserve_stopped_results=preserve_stopped_results_after_transition,
3837
3803
  )
3838
3804
  )
3839
3805
  if args.command == "inspect-target":
@@ -3845,10 +3811,16 @@ def main() -> None:
3845
3811
  print(json.dumps(result, allow_nan=False, sort_keys=True))
3846
3812
  return
3847
3813
  with closing(connect()) as connection:
3814
+ remediation.require_available(connection, args, require_scan)
3848
3815
  if args.command == "create-workspace":
3849
3816
  result = create_workspace(connection, args)
3850
3817
  elif args.command == "get-workspace":
3851
- result = workspace_state(connection, args.workspace_id, thread_id=args.thread_id)
3818
+ result = workspace_state(
3819
+ connection,
3820
+ args.workspace_id,
3821
+ thread_id=args.thread_id,
3822
+ refresh_stopped=True,
3823
+ )
3852
3824
  elif args.command == "save-workspace":
3853
3825
  result = save_workspace(connection, args)
3854
3826
  elif args.command == "start-scan":
@@ -3873,11 +3845,15 @@ def main() -> None:
3873
3845
  result = deep_scan.finish_deep_scan(connection, args)
3874
3846
  elif args.command == "fail-deep-scan":
3875
3847
  result = deep_scan.fail_deep_scan(connection, args)
3848
+ elif args.command == "record-deep-scan-publication-failure":
3849
+ result = deep_scan.record_deep_scan_publication_failure(connection, args)
3876
3850
  elif args.command == "get-scan":
3851
+ refresh_stopped_scan_results(connection, args.scan_id)
3877
3852
  result = scan_context(connection, args.scan_id, args.occurrence_id)
3878
3853
  elif args.command == "get-scan-feedback":
3879
3854
  result = get_scan_feedback(connection, require_scan(connection, args.scan_id))
3880
3855
  elif args.command == "list-scans":
3856
+ refresh_all_stopped_scan_results(connection)
3881
3857
  result = scan_history.list_scans(connection, args)
3882
3858
  elif args.command == "list-unmatched-scan-pairs":
3883
3859
  result = scan_history.list_unmatched_scan_pairs(
@@ -3911,8 +3887,10 @@ def main() -> None:
3911
3887
  read_coverage=coverage_for_comparison,
3912
3888
  )
3913
3889
  elif args.command == "list-global-findings":
3890
+ refresh_all_stopped_scan_results(connection)
3914
3891
  result = native_indexes.list_global_findings(connection, args)
3915
3892
  elif args.command == "list-repositories":
3893
+ refresh_all_stopped_scan_results(connection)
3916
3894
  result = native_indexes.list_repositories(connection, args)
3917
3895
  elif args.command == "list-findings":
3918
3896
  result = list_findings(connection, args)
@@ -3930,6 +3908,10 @@ def main() -> None:
3930
3908
  result = cancel_scan(connection, args)
3931
3909
  elif args.command == "fail-scan":
3932
3910
  result = fail_scan(connection, args)
3911
+ elif args.command == "preserve-scan-results":
3912
+ result = preserve_scan_results(connection, args)
3913
+ elif args.command == "write-scan-draft":
3914
+ result = write_scan_draft(connection, args)
3933
3915
  elif args.command == "mark-handoff-delivered":
3934
3916
  result = handoff.mark_handoff_delivered(
3935
3917
  connection,