@openai/codex-security 0.5.1 → 0.7.0

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 (313) hide show
  1. package/README.md +151 -36
  2. package/_bundled_plugin/.mcp.json +1 -0
  3. package/_bundled_plugin/mcp/helpers.mjs.br.part-000 +0 -0
  4. package/_bundled_plugin/mcp/helpers.mjs.br.part-001 +0 -0
  5. package/_bundled_plugin/mcp/native/THIRD_PARTY_NOTICES.txt +1 -1
  6. package/_bundled_plugin/mcp/native/darwin-arm64/unix.node +0 -0
  7. package/_bundled_plugin/mcp/native/darwin-x64/unix.node +0 -0
  8. package/_bundled_plugin/mcp/native/linux-arm64-gnu/unix.node +0 -0
  9. package/_bundled_plugin/mcp/native/linux-arm64-musl/unix.node +0 -0
  10. package/_bundled_plugin/mcp/native/linux-x64-gnu/unix.node +0 -0
  11. package/_bundled_plugin/mcp/native/win32-arm64/windows.node +0 -0
  12. package/_bundled_plugin/mcp/native/win32-x64/windows.node +0 -0
  13. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  14. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  15. package/_bundled_plugin/mcp/server.mjs.br.part-002 +0 -0
  16. package/_bundled_plugin/references/access-advisory.md +11 -0
  17. package/_bundled_plugin/references/core-scan.md +30 -77
  18. package/_bundled_plugin/references/final-report.md +45 -189
  19. package/_bundled_plugin/references/scan-contract.md +2 -0
  20. package/_bundled_plugin/references/scan-prologue.md +1 -1
  21. package/_bundled_plugin/references/scan-runtime.md +1 -5
  22. package/_bundled_plugin/references/threat-model.md +33 -41
  23. package/_bundled_plugin/schemas/coverage.schema.json +12 -1
  24. package/_bundled_plugin/schemas/scan-manifest.schema.json +1 -0
  25. package/_bundled_plugin/schemas/tools/deep-reducer.schema.json +38 -1
  26. package/_bundled_plugin/scripts/deep_scan_config.py +39 -39
  27. package/_bundled_plugin/scripts/deep_scan_workbench.py +145 -85
  28. package/_bundled_plugin/scripts/finalize_scan_contract.py +189 -57
  29. package/_bundled_plugin/scripts/report_projection.py +44 -15
  30. package/_bundled_plugin/scripts/threat_model_projection.py +4 -2
  31. package/_bundled_plugin/scripts/windows_scan_local_files.py +43 -14
  32. package/_bundled_plugin/scripts/workbench/retained_coverage.py +3218 -0
  33. package/_bundled_plugin/scripts/workbench/retained_records.py +627 -0
  34. package/_bundled_plugin/scripts/workbench/saved_coverage.py +644 -1
  35. package/_bundled_plugin/scripts/workbench/saved_outputs.py +72 -0
  36. package/_bundled_plugin/scripts/workbench/saved_sources.py +712 -0
  37. package/_bundled_plugin/scripts/workbench_cli.py +3 -0
  38. package/_bundled_plugin/scripts/workbench_coverage_receipts.py +479 -0
  39. package/_bundled_plugin/scripts/workbench_db.py +51 -22
  40. package/_bundled_plugin/scripts/workbench_feedback.py +0 -1
  41. package/_bundled_plugin/scripts/workbench_finding_workflows.py +53 -9
  42. package/_bundled_plugin/scripts/workbench_native_indexes.py +56 -44
  43. package/_bundled_plugin/scripts/workbench_saved_results.py +641 -2387
  44. package/_bundled_plugin/scripts/workbench_scan_history.py +72 -7
  45. package/_bundled_plugin/scripts/workbench_schema.py +1 -1
  46. package/_bundled_plugin/scripts/workbench_source_scopes.py +4 -1
  47. package/_bundled_plugin/scripts/workbench_target.py +142 -17
  48. package/_bundled_plugin/scripts/workbench_validation.py +9 -2
  49. package/_bundled_plugin/shared/workbench-history-repairs.json +21 -0
  50. package/_bundled_plugin/shared/workbench-migrations.json +44 -0
  51. package/_bundled_plugin/skills/assess-patch-risk/SKILL.md +3 -14
  52. package/_bundled_plugin/skills/assess-patch-risk/references/windows-validation.md +14 -0
  53. package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +38 -103
  54. package/_bundled_plugin/skills/attack-path-analysis/references/attack-path-facts.md +11 -73
  55. package/_bundled_plugin/skills/attack-path-analysis/references/severity-policy.md +57 -116
  56. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +28 -51
  57. package/_bundled_plugin/skills/define-security-policy/SKILL.md +9 -9
  58. package/_bundled_plugin/skills/finding-discovery/SKILL.md +26 -140
  59. package/_bundled_plugin/skills/fix-finding/SKILL.md +19 -30
  60. package/_bundled_plugin/skills/propose-security-hardening/SKILL.md +28 -218
  61. package/_bundled_plugin/skills/propose-security-hardening/references/proposal-format.md +29 -424
  62. package/_bundled_plugin/skills/publish-findings/SKILL.md +134 -0
  63. package/_bundled_plugin/skills/publish-findings/agents/openai.yaml +6 -0
  64. package/_bundled_plugin/skills/publish-findings/references/wiz-import.md +64 -0
  65. package/_bundled_plugin/skills/security-diff-scan/SKILL.md +12 -12
  66. package/_bundled_plugin/skills/security-scan/SKILL.md +28 -16
  67. package/_bundled_plugin/skills/security-scan/references/desktop-scan.md +11 -17
  68. package/_bundled_plugin/skills/security-scan/references/scan-artifacts-and-ledger.md +52 -69
  69. package/_bundled_plugin/skills/threat-model/SKILL.md +16 -9
  70. package/_bundled_plugin/skills/triage-finding/SKILL.md +5 -15
  71. package/_bundled_plugin/skills/triage-finding/references/ticket-intake.md +6 -6
  72. package/_bundled_plugin/skills/validation/SKILL.md +42 -104
  73. package/_bundled_plugin/skills/validation/references/validation-guidance.md +21 -106
  74. package/_bundled_plugin/skills/verify-fix/SKILL.md +8 -4
  75. package/_bundled_plugin/skills/vulnerability-writeup/SKILL.md +21 -159
  76. package/_bundled_plugin/skills/vulnerability-writeup/references/report-format.md +18 -60
  77. package/dist/api.d.ts +8 -3
  78. package/dist/api.d.ts.map +1 -1
  79. package/dist/api.js +414 -72
  80. package/dist/api.js.map +1 -1
  81. package/dist/auth.d.ts.map +1 -1
  82. package/dist/auth.js +4 -1
  83. package/dist/auth.js.map +1 -1
  84. package/dist/classify-severity-records.d.ts +5 -2
  85. package/dist/classify-severity-records.d.ts.map +1 -1
  86. package/dist/classify-severity-records.js +40 -34
  87. package/dist/classify-severity-records.js.map +1 -1
  88. package/dist/classify-severity.d.ts +5 -1
  89. package/dist/classify-severity.d.ts.map +1 -1
  90. package/dist/classify-severity.js +20 -30
  91. package/dist/classify-severity.js.map +1 -1
  92. package/dist/cli-help.d.ts.map +1 -1
  93. package/dist/cli-help.js +1 -0
  94. package/dist/cli-help.js.map +1 -1
  95. package/dist/cli.d.ts +9 -2
  96. package/dist/cli.d.ts.map +1 -1
  97. package/dist/cli.js +802 -149
  98. package/dist/cli.js.map +1 -1
  99. package/dist/cloud-endpoint.d.ts +6 -0
  100. package/dist/cloud-endpoint.d.ts.map +1 -0
  101. package/dist/cloud-endpoint.js +51 -0
  102. package/dist/cloud-endpoint.js.map +1 -0
  103. package/dist/cloud-publish.d.ts.map +1 -1
  104. package/dist/cloud-publish.js +7 -5
  105. package/dist/cloud-publish.js.map +1 -1
  106. package/dist/component-scan.d.ts +8 -2
  107. package/dist/component-scan.d.ts.map +1 -1
  108. package/dist/component-scan.js +2 -0
  109. package/dist/component-scan.js.map +1 -1
  110. package/dist/contract.d.ts +2 -0
  111. package/dist/contract.d.ts.map +1 -1
  112. package/dist/contract.js +67 -33
  113. package/dist/contract.js.map +1 -1
  114. package/dist/cost.d.ts +18 -0
  115. package/dist/cost.d.ts.map +1 -1
  116. package/dist/cost.js +107 -12
  117. package/dist/cost.js.map +1 -1
  118. package/dist/coverage-presentation.d.ts +6 -0
  119. package/dist/coverage-presentation.d.ts.map +1 -0
  120. package/dist/coverage-presentation.js +34 -0
  121. package/dist/coverage-presentation.js.map +1 -0
  122. package/dist/custom-validation-prompt.js +3 -3
  123. package/dist/custom-validation-prompt.js.map +1 -1
  124. package/dist/custom-validation.d.ts.map +1 -1
  125. package/dist/custom-validation.js +30 -4
  126. package/dist/custom-validation.js.map +1 -1
  127. package/dist/deduplication/checkpointed-review.d.ts.map +1 -1
  128. package/dist/deduplication/checkpointed-review.js +3 -1
  129. package/dist/deduplication/checkpointed-review.js.map +1 -1
  130. package/dist/deduplication/scan.d.ts.map +1 -1
  131. package/dist/deduplication/scan.js +7 -5
  132. package/dist/deduplication/scan.js.map +1 -1
  133. package/dist/default-severity-rubric.d.ts +3 -0
  134. package/dist/default-severity-rubric.d.ts.map +1 -0
  135. package/dist/default-severity-rubric.js +67 -0
  136. package/dist/default-severity-rubric.js.map +1 -0
  137. package/dist/endor-findings.d.ts +8 -0
  138. package/dist/endor-findings.d.ts.map +1 -0
  139. package/dist/endor-findings.js +238 -0
  140. package/dist/endor-findings.js.map +1 -0
  141. package/dist/external-findings-import.d.ts +5 -0
  142. package/dist/external-findings-import.d.ts.map +1 -0
  143. package/dist/external-findings-import.js +46 -0
  144. package/dist/external-findings-import.js.map +1 -0
  145. package/dist/external-findings-local.d.ts +63 -0
  146. package/dist/external-findings-local.d.ts.map +1 -0
  147. package/dist/external-findings-local.js +344 -0
  148. package/dist/external-findings-local.js.map +1 -0
  149. package/dist/external-findings-publish.d.ts +73 -0
  150. package/dist/external-findings-publish.d.ts.map +1 -0
  151. package/dist/external-findings-publish.js +721 -0
  152. package/dist/external-findings-publish.js.map +1 -0
  153. package/dist/external-findings-read.d.ts +16 -0
  154. package/dist/external-findings-read.d.ts.map +1 -0
  155. package/dist/external-findings-read.js +97 -0
  156. package/dist/external-findings-read.js.map +1 -0
  157. package/dist/external-findings.d.ts +21 -0
  158. package/dist/external-findings.d.ts.map +1 -0
  159. package/dist/external-findings.js +2 -0
  160. package/dist/external-findings.js.map +1 -0
  161. package/dist/external-import-contract.d.ts +15 -0
  162. package/dist/external-import-contract.d.ts.map +1 -0
  163. package/dist/external-import-contract.js +262 -0
  164. package/dist/external-import-contract.js.map +1 -0
  165. package/dist/external-import-models.d.ts +362 -0
  166. package/dist/external-import-models.d.ts.map +1 -0
  167. package/dist/external-import-models.js +3 -0
  168. package/dist/external-import-models.js.map +1 -0
  169. package/dist/external-imports-feature.d.ts +6 -0
  170. package/dist/external-imports-feature.d.ts.map +1 -0
  171. package/dist/external-imports-feature.js +51 -0
  172. package/dist/external-imports-feature.js.map +1 -0
  173. package/dist/finding-workflow.d.ts +23 -6
  174. package/dist/finding-workflow.d.ts.map +1 -1
  175. package/dist/finding-workflow.js +23 -10
  176. package/dist/finding-workflow.js.map +1 -1
  177. package/dist/github-advisories.d.ts +386 -0
  178. package/dist/github-advisories.d.ts.map +1 -0
  179. package/dist/github-advisories.js +133 -0
  180. package/dist/github-advisories.js.map +1 -0
  181. package/dist/github-findings.d.ts +11 -0
  182. package/dist/github-findings.d.ts.map +1 -0
  183. package/dist/github-findings.js +258 -0
  184. package/dist/github-findings.js.map +1 -0
  185. package/dist/hosted-remediation-protocol.d.ts +2 -1
  186. package/dist/hosted-remediation-protocol.d.ts.map +1 -1
  187. package/dist/hosted-remediation-protocol.js +17 -11
  188. package/dist/hosted-remediation-protocol.js.map +1 -1
  189. package/dist/hosted-remediation.d.ts +4 -0
  190. package/dist/hosted-remediation.d.ts.map +1 -1
  191. package/dist/hosted-remediation.js +22 -3
  192. package/dist/hosted-remediation.js.map +1 -1
  193. package/dist/hosted-scan.d.ts +16 -0
  194. package/dist/hosted-scan.d.ts.map +1 -1
  195. package/dist/hosted-scan.js +9 -2
  196. package/dist/hosted-scan.js.map +1 -1
  197. package/dist/import-scan.js +1 -1
  198. package/dist/import-scan.js.map +1 -1
  199. package/dist/index.d.ts +16 -3
  200. package/dist/index.d.ts.map +1 -1
  201. package/dist/index.js +9 -1
  202. package/dist/index.js.map +1 -1
  203. package/dist/knowledge-base.d.ts +1 -0
  204. package/dist/knowledge-base.d.ts.map +1 -1
  205. package/dist/knowledge-base.js +17 -8
  206. package/dist/knowledge-base.js.map +1 -1
  207. package/dist/models.d.ts +1 -0
  208. package/dist/models.d.ts.map +1 -1
  209. package/dist/models.js.map +1 -1
  210. package/dist/owner-evidence.d.ts +2 -0
  211. package/dist/owner-evidence.d.ts.map +1 -1
  212. package/dist/owner-evidence.js +2 -1
  213. package/dist/owner-evidence.js.map +1 -1
  214. package/dist/project-config-schema.d.ts +2 -0
  215. package/dist/project-config-schema.d.ts.map +1 -1
  216. package/dist/project-config-schema.js +1 -0
  217. package/dist/project-config-schema.js.map +1 -1
  218. package/dist/project-config.d.ts +1 -0
  219. package/dist/project-config.d.ts.map +1 -1
  220. package/dist/project-config.js +2 -0
  221. package/dist/project-config.js.map +1 -1
  222. package/dist/publish.d.ts +2 -0
  223. package/dist/publish.d.ts.map +1 -1
  224. package/dist/publish.js +1 -1
  225. package/dist/publish.js.map +1 -1
  226. package/dist/remediation.d.ts +7 -25
  227. package/dist/remediation.d.ts.map +1 -1
  228. package/dist/remediation.js +18 -28
  229. package/dist/remediation.js.map +1 -1
  230. package/dist/result.d.ts +4 -0
  231. package/dist/result.d.ts.map +1 -1
  232. package/dist/result.js +10 -0
  233. package/dist/result.js.map +1 -1
  234. package/dist/runtime.d.ts +1 -0
  235. package/dist/runtime.d.ts.map +1 -1
  236. package/dist/runtime.js +10 -8
  237. package/dist/runtime.js.map +1 -1
  238. package/dist/scan-dashboard.d.ts +5 -3
  239. package/dist/scan-dashboard.d.ts.map +1 -1
  240. package/dist/scan-dashboard.js +79 -26
  241. package/dist/scan-dashboard.js.map +1 -1
  242. package/dist/scan-history-renderer.d.ts.map +1 -1
  243. package/dist/scan-history-renderer.js +8 -2
  244. package/dist/scan-history-renderer.js.map +1 -1
  245. package/dist/scan-owner-suggestions.d.ts +8 -0
  246. package/dist/scan-owner-suggestions.d.ts.map +1 -0
  247. package/dist/scan-owner-suggestions.js +33 -0
  248. package/dist/scan-owner-suggestions.js.map +1 -0
  249. package/dist/scan-settings.d.ts +1 -0
  250. package/dist/scan-settings.d.ts.map +1 -1
  251. package/dist/scan-settings.js +4 -0
  252. package/dist/scan-settings.js.map +1 -1
  253. package/dist/scan-usage-display.d.ts +8 -0
  254. package/dist/scan-usage-display.d.ts.map +1 -0
  255. package/dist/scan-usage-display.js +46 -0
  256. package/dist/scan-usage-display.js.map +1 -0
  257. package/dist/scanner-import.d.ts +20 -0
  258. package/dist/scanner-import.d.ts.map +1 -0
  259. package/dist/scanner-import.js +208 -0
  260. package/dist/scanner-import.js.map +1 -0
  261. package/dist/security-policy-diff.d.ts +2 -0
  262. package/dist/security-policy-diff.d.ts.map +1 -0
  263. package/dist/security-policy-diff.js +197 -0
  264. package/dist/security-policy-diff.js.map +1 -0
  265. package/dist/security-policy.d.ts +7 -3
  266. package/dist/security-policy.d.ts.map +1 -1
  267. package/dist/security-policy.js +11 -76
  268. package/dist/security-policy.js.map +1 -1
  269. package/dist/semgrep-findings.d.ts +10 -0
  270. package/dist/semgrep-findings.d.ts.map +1 -0
  271. package/dist/semgrep-findings.js +293 -0
  272. package/dist/semgrep-findings.js.map +1 -0
  273. package/dist/severity-review.d.ts +3 -0
  274. package/dist/severity-review.d.ts.map +1 -1
  275. package/dist/severity-review.js +19 -0
  276. package/dist/severity-review.js.map +1 -1
  277. package/dist/severity-store.js +7 -2
  278. package/dist/severity-store.js.map +1 -1
  279. package/dist/skill-authentication.d.ts +18 -0
  280. package/dist/skill-authentication.d.ts.map +1 -0
  281. package/dist/skill-authentication.js +82 -0
  282. package/dist/skill-authentication.js.map +1 -0
  283. package/dist/snyk-findings.d.ts +10 -0
  284. package/dist/snyk-findings.d.ts.map +1 -0
  285. package/dist/snyk-findings.js +350 -0
  286. package/dist/snyk-findings.js.map +1 -0
  287. package/dist/suggest-owners.d.ts +4 -1
  288. package/dist/suggest-owners.d.ts.map +1 -1
  289. package/dist/suggest-owners.js +53 -21
  290. package/dist/suggest-owners.js.map +1 -1
  291. package/dist/targets.d.ts +2 -2
  292. package/dist/targets.d.ts.map +1 -1
  293. package/dist/targets.js +16 -3
  294. package/dist/targets.js.map +1 -1
  295. package/dist/wiz-csv.d.ts +9 -0
  296. package/dist/wiz-csv.d.ts.map +1 -0
  297. package/dist/wiz-csv.js +116 -0
  298. package/dist/wiz-csv.js.map +1 -0
  299. package/dist/wiz-findings.d.ts +5 -0
  300. package/dist/wiz-findings.d.ts.map +1 -0
  301. package/dist/wiz-findings.js +464 -0
  302. package/dist/wiz-findings.js.map +1 -0
  303. package/docs/classify-severity-records.md +30 -13
  304. package/docs/cli.md +835 -47
  305. package/docs/findings-service.md +119 -118
  306. package/docs/hosted-remediation.md +43 -24
  307. package/docs/hosted-scans.md +38 -0
  308. package/package.json +11 -9
  309. package/schemas/external-findings.schema.json +1421 -0
  310. package/schemas/project-config.schema.json +4 -0
  311. package/_bundled_plugin/scripts/generate_in_scope_files.py +0 -277
  312. package/_bundled_plugin/scripts/generate_rank_input.py +0 -350
  313. package/_bundled_plugin/scripts/rank_preview.py +0 -129
@@ -0,0 +1,712 @@
1
+ """Read and freeze saved scan checkpoint observations."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import hashlib
7
+ import json
8
+ import os
9
+ import re
10
+ import stat
11
+ from collections.abc import Iterator
12
+ from functools import partial
13
+ from pathlib import Path
14
+ from typing import Any
15
+
16
+ from finalize_scan_contract import (
17
+ ContractError,
18
+ _read_scan_local_json,
19
+ _read_scan_local_json_with_metadata,
20
+ manifest_is_sealed,
21
+ write_scan_local_bytes,
22
+ )
23
+ from workbench_coverage_receipts import capture_receipt_checkpoints
24
+
25
+
26
+ class _CheckpointSnapshotError(ContractError):
27
+ """A captured source observation could not be persisted for publication replay."""
28
+
29
+ def __init__(self, message: str, captured: dict[str, tuple[str, int]]) -> None:
30
+ super().__init__(message)
31
+ self.captured = captured
32
+ self.records: dict[str, tuple[dict[str, Any], str, int]] = {}
33
+ self.heads: dict[str, str | None] = {}
34
+ self.model_source: list[str] = []
35
+ self.parent_context: dict[str, str] = {}
36
+ self.include_parent = False
37
+ self.documents: tuple[dict[str, Any], dict[str, Any], dict[str, Any]] | None = None
38
+
39
+
40
+ def _encoded(value: Any) -> bytes:
41
+ return json.dumps(
42
+ value, ensure_ascii=True, allow_nan=False, sort_keys=True, separators=(",", ":")
43
+ ).encode()
44
+
45
+
46
+ def _digest(value: Any) -> str:
47
+ return hashlib.sha256(_encoded(value)).hexdigest()
48
+
49
+
50
+ def _source_digests(value: Any, error: str) -> dict[str, str]:
51
+ if not isinstance(value, dict) or not all(
52
+ isinstance(relative, str) and isinstance(digest, str) for relative, digest in value.items()
53
+ ):
54
+ raise ContractError(error)
55
+ return value
56
+
57
+
58
+ def _retained_source_state(value: Any) -> tuple[dict[str, str], str | None]:
59
+ if isinstance(value, dict) and isinstance(value.get("sources"), dict):
60
+ sources = _source_digests(
61
+ value["sources"], "Saved stopped-scan source digests are malformed."
62
+ )
63
+ model_source = value.get("threatModelSource")
64
+ if "threatModelSource" in value and (
65
+ not isinstance(model_source, str) or model_source not in sources
66
+ ):
67
+ raise ContractError("Saved stopped-scan model source is outside its checkpoint set.")
68
+ return sources, model_source
69
+ return _source_digests(value, "Saved stopped-scan source digests are malformed."), None
70
+
71
+
72
+ def _retained_checkpoint_state(
73
+ scan: Any,
74
+ ) -> tuple[dict[str, str | None] | None, dict[str, Any] | None]:
75
+ raw = scan["retained_checkpoint_heads_json"]
76
+ if raw is None:
77
+ return None, None
78
+ saved = json.loads(raw)
79
+ if not isinstance(saved.get("heads"), dict):
80
+ return saved, None
81
+ if saved["recoveryBaseDigest"] != scan["seal_manifest_digest"]:
82
+ raise ContractError("Stopped scan recovery publication changed after selection.")
83
+ return saved["heads"], saved
84
+
85
+
86
+ def _encode_retained_sources(
87
+ sources: dict[str, str],
88
+ model_source: list[str],
89
+ source_times: dict[str, int] | None = None,
90
+ parent_context: dict[str, str] | None = None,
91
+ ) -> str:
92
+ state: dict[str, Any] = sources
93
+ if model_source or source_times is not None:
94
+ state = {"sources": sources}
95
+ if model_source:
96
+ state["threatModelSource"] = model_source[0]
97
+ if source_times is not None:
98
+ state["sourceTimes"] = source_times
99
+ if parent_context:
100
+ state["parentContext"] = parent_context
101
+ return json.dumps(state, sort_keys=True)
102
+
103
+
104
+ def _children(scan_dir: Path, relative: str) -> list[str]:
105
+ cursor = scan_dir
106
+ for part in Path(relative).parts:
107
+ if part in {"..", "."}:
108
+ return []
109
+ cursor = cursor / part
110
+ try:
111
+ if not stat.S_ISDIR(cursor.lstat().st_mode):
112
+ return []
113
+ except FileNotFoundError:
114
+ return []
115
+ return sorted(child.name for child in cursor.iterdir())
116
+
117
+
118
+ def _checkpoint_paths(scan_dir: Path, directory: str) -> list[str]:
119
+ return [
120
+ f"{directory}/{name}"
121
+ for name in _children(scan_dir, directory)
122
+ if re.fullmatch(r"[0-9a-f]{64}\.json", name)
123
+ ]
124
+
125
+
126
+ def _checkpoint_head_directory(relative: str) -> Path | None:
127
+ path = Path(relative)
128
+ if path.name == "checkpoint-head.json":
129
+ return path.parent
130
+ if path.parent.name == "checkpoint-heads":
131
+ return path.parent.parent
132
+ return None
133
+
134
+
135
+ def _unselected_root_order(
136
+ saved_heads: dict[str, str],
137
+ checkpoint_heads: dict[str, str | None],
138
+ source_order: dict[str, tuple[int, int]],
139
+ ) -> tuple[int, int]:
140
+ # One payload can have several accepted observations; retain their order.
141
+ return max(
142
+ (
143
+ source_order[head]
144
+ for head, checkpoint in saved_heads.items()
145
+ if _checkpoint_head_directory(head) == Path(".")
146
+ and checkpoint_heads.get(".") not in (None, head, f"checkpoints/{checkpoint}")
147
+ ),
148
+ default=(-1, 0),
149
+ )
150
+
151
+
152
+ def _capture_saved_source(
153
+ scan_dir: Path,
154
+ relative: str,
155
+ scan_id: str,
156
+ *,
157
+ kind: str | None = None,
158
+ snapshot_head: bool = True,
159
+ write: bool = True,
160
+ created_paths: set[str] | None = None,
161
+ checkpoint_heads: dict[str, str | None] | None = None,
162
+ expected_digest: str | None = None,
163
+ ) -> dict[str, tuple[str, int]]:
164
+ if not snapshot_head or Path(relative).name != "checkpoint-head.json":
165
+ _, digest, observed = _read_saved_result(scan_dir, relative, scan_id, kind=kind)
166
+ return capture_receipt_checkpoints(
167
+ relative,
168
+ kind,
169
+ {relative: (digest, observed)},
170
+ partial(_checkpoint_paths, scan_dir),
171
+ partial(_read_saved_result, scan_dir, scan_id=scan_id),
172
+ )
173
+ head, head_digest, observed = _read_saved_result(scan_dir, relative, scan_id)
174
+ if expected_digest is not None and head_digest != expected_digest:
175
+ raise ContractError("checkpoint changed after the scan stopped")
176
+ observation = {
177
+ **{key: head[key] for key in ("checkpoint", "inputCheckpoint") if key in head},
178
+ "observedAtNs": str(observed),
179
+ }
180
+ directory = Path(relative).parent
181
+ selected_records = {}
182
+ for key in ("checkpoint", "inputCheckpoint"):
183
+ if key in head:
184
+ selected = (directory / "checkpoints" / head[key]).as_posix()
185
+ selected_records[selected] = _read_saved_result(scan_dir, selected, scan_id)
186
+ selected_sources = {
187
+ path: (digest, seen) for path, (_, digest, seen) in selected_records.items()
188
+ }
189
+ digest = _digest(observation)
190
+ snapshot = (directory / "checkpoint-heads" / f"{digest}.json").as_posix()
191
+ # Capture the selected file even if the worker created it after directory enumeration.
192
+ if checkpoint_heads is not None:
193
+ checkpoint_heads[directory.as_posix()] = snapshot
194
+ if write and not (scan_dir / snapshot).exists():
195
+ if created_paths is not None and not os.path.lexists(scan_dir / snapshot):
196
+ created_paths.add(snapshot)
197
+ try:
198
+ write_scan_local_bytes(scan_dir, snapshot, _encoded(observation))
199
+ except (ContractError, OSError) as exc:
200
+ if checkpoint_heads is not None:
201
+ checkpoint_heads[directory.as_posix()] = relative
202
+ failure = _CheckpointSnapshotError(
203
+ str(exc), {relative: (head_digest, observed), **selected_sources}
204
+ )
205
+ failure.records = {relative: (head, head_digest, observed), **selected_records}
206
+ raise failure from exc
207
+ return {
208
+ snapshot: (digest, observed),
209
+ **selected_sources,
210
+ }
211
+
212
+
213
+ def _read_saved_result(
214
+ scan_dir: Path, relative: str, scan_id: str, *, kind: str | None = None
215
+ ) -> tuple[dict[str, Any], str, int]:
216
+ draft, _, metadata = _read_scan_local_json_with_metadata(
217
+ scan_dir, relative, "Saved scan checkpoint"
218
+ )
219
+ directory = _checkpoint_head_directory(relative)
220
+ if directory is not None:
221
+ checkpoint = draft.get("checkpoint")
222
+ if not isinstance(checkpoint, str) or not re.fullmatch(r"[0-9a-f]{64}\.json", checkpoint):
223
+ raise ContractError("checkpoint head does not name a saved checkpoint")
224
+ _read_saved_result(scan_dir, (directory / "checkpoints" / checkpoint).as_posix(), scan_id)
225
+ if "inputCheckpoint" in draft:
226
+ raw = draft["inputCheckpoint"]
227
+ if not isinstance(raw, str) or not re.fullmatch(r"[0-9a-f]{64}\.json", raw):
228
+ raise ContractError("checkpoint head does not name a saved input checkpoint")
229
+ _read_saved_result(scan_dir, (directory / "checkpoints" / raw).as_posix(), scan_id)
230
+ if Path(relative).name == "checkpoint-head.json":
231
+ return draft, _digest([draft, metadata.st_mtime_ns]), metadata.st_mtime_ns
232
+ observed = draft.get("observedAtNs")
233
+ if not isinstance(observed, str) or not re.fullmatch(r"-?[0-9]+", observed):
234
+ raise ContractError("checkpoint head has no observation time")
235
+ return draft, _digest(draft), int(observed)
236
+ if draft.get("scanId") != scan_id:
237
+ raise ContractError("checkpoint belongs to a different scan")
238
+ if not _is_source_order_snapshot(relative) and (
239
+ not isinstance(draft.get("findings"), list)
240
+ or not isinstance(draft.get("coverage", {} if kind == "dedup" else None), dict)
241
+ ):
242
+ raise ContractError("checkpoint has no semantic findings or coverage")
243
+ return draft, _digest(draft), metadata.st_mtime_ns
244
+
245
+
246
+ def _is_source_order_snapshot(relative: str) -> bool:
247
+ path = Path(relative)
248
+ return path.parent == Path("source-order") and bool(
249
+ re.fullmatch(r"[0-9a-f]{64}\.json", path.name)
250
+ )
251
+
252
+
253
+ def _frozen_source_times(
254
+ scan_dir: Path,
255
+ scan_id: str,
256
+ sources: dict[str, str],
257
+ parent_context: dict[str, str] | None = None,
258
+ ) -> dict[str, int]:
259
+ times: dict[str, int] = {}
260
+ snapshots = [path for path in sources if _is_source_order_snapshot(path)]
261
+ for path in snapshots:
262
+ record, digest, _ = _read_saved_result(scan_dir, path, scan_id)
263
+ if digest != sources[path] or not isinstance(record.get("sources"), dict):
264
+ raise ContractError("saved source ordering changed after the scan stopped")
265
+ context = record.get("parentContext")
266
+ if parent_context is not None and context is not None:
267
+ if not isinstance(context, dict) or any(
268
+ (
269
+ "canonical" not in context
270
+ or not isinstance(relative, str)
271
+ or not re.fullmatch(r"-?[0-9]+", relative)
272
+ )
273
+ if role == "canonicalObservedAtNs"
274
+ else (
275
+ role not in ("canonical", "head")
276
+ or not isinstance(relative, str)
277
+ or relative not in sources
278
+ or (role == "canonical" and Path(relative).parent != Path("checkpoints"))
279
+ or (role == "head" and _checkpoint_head_directory(relative) != Path("."))
280
+ )
281
+ for role, relative in context.items()
282
+ ):
283
+ raise ContractError("saved parent context is outside the frozen sources")
284
+ if any(
285
+ role in parent_context and parent_context[role] != relative
286
+ for role, relative in context.items()
287
+ ):
288
+ raise ContractError("saved parent context has conflicting observations")
289
+ parent_context.update(context)
290
+ for relative, observation in record["sources"].items():
291
+ if (
292
+ not isinstance(observation, dict)
293
+ or relative not in sources
294
+ or observation.get("digest") != sources[relative]
295
+ or not isinstance(observation.get("observedAtNs"), str)
296
+ or not re.fullmatch(r"-?[0-9]+", observation["observedAtNs"])
297
+ ):
298
+ raise ContractError("saved source ordering does not match its frozen sources")
299
+ observed = int(observation["observedAtNs"])
300
+ if relative in times and times[relative] != observed:
301
+ raise ContractError("saved source ordering has conflicting observations")
302
+ times[relative] = observed
303
+ if snapshots and sources.keys() - set(snapshots) - times.keys():
304
+ raise ContractError("saved source ordering is incomplete")
305
+ return times
306
+
307
+
308
+ def _frozen_checkpoint_heads(
309
+ scan_dir: Path, scan_id: str, sources: dict[str, str], *, skip_unreadable: bool = False
310
+ ) -> dict[str, str]:
311
+ parent_context: dict[str, str] = {}
312
+ try:
313
+ times = _frozen_source_times(scan_dir, scan_id, sources, parent_context)
314
+ except (ContractError, OSError, ValueError):
315
+ if not skip_unreadable:
316
+ raise
317
+ times = {}
318
+ parent_context.clear()
319
+ # A frozen parent can identify its accepted head even when observation times tie.
320
+ accepted_root = None
321
+ heads: dict[str, tuple[int, str]] = {}
322
+ ambiguous: set[str] = set()
323
+ for relative, expected_digest in sources.items():
324
+ directory = _checkpoint_head_directory(relative)
325
+ if directory is None:
326
+ continue
327
+ try:
328
+ head, digest, observed = _read_saved_result(scan_dir, relative, scan_id)
329
+ if digest != expected_digest:
330
+ raise ContractError("checkpoint changed after the scan stopped")
331
+ except (ContractError, OSError, ValueError):
332
+ if skip_unreadable:
333
+ continue
334
+ raise
335
+ selected = (directory / "checkpoints" / head["checkpoint"]).as_posix()
336
+ observation = (times.get(relative, observed), selected)
337
+ key = directory.as_posix()
338
+ if relative == parent_context.get("head"):
339
+ accepted_root = relative
340
+ if key not in heads or observation[0] > heads[key][0]:
341
+ heads[key] = observation
342
+ ambiguous.discard(key)
343
+ elif observation[0] == heads[key][0] and selected != heads[key][1]:
344
+ ambiguous.add(key)
345
+ # A legacy tie retains both observations until recovery selects a head.
346
+ frozen = {
347
+ directory: selected
348
+ for directory, (_, selected) in heads.items()
349
+ if directory not in ambiguous
350
+ }
351
+ if accepted_root is not None:
352
+ frozen["."] = accepted_root
353
+ return frozen
354
+
355
+
356
+ def _legacy_checkpoint_observed_at(
357
+ scan_dir: Path, scan_id: str, sources: dict[str, str], selected: str
358
+ ) -> int | None:
359
+ times = _frozen_source_times(scan_dir, scan_id, sources)
360
+ checkpoint = Path(selected)
361
+ observations = []
362
+ for relative, expected_digest in sources.items():
363
+ if _checkpoint_head_directory(relative) != checkpoint.parent.parent:
364
+ continue
365
+ head, digest, observed = _read_saved_result(scan_dir, relative, scan_id)
366
+ if digest != expected_digest:
367
+ raise ContractError("checkpoint changed after the scan stopped")
368
+ if head["checkpoint"] == checkpoint.name:
369
+ observations.append(times.get(relative, observed))
370
+ # Payload-only selections used the newest retained observation for that payload.
371
+ return max(observations, default=None)
372
+
373
+
374
+ def _restore_checkpoint_head(
375
+ heads: dict[str, str | None],
376
+ previous_heads: dict[str, str | None],
377
+ sources: dict[str, str],
378
+ relative: str,
379
+ ) -> None:
380
+ path = Path(relative)
381
+ if path.name != "checkpoint-head.json":
382
+ return
383
+ directory = path.parent.as_posix()
384
+ if directory in previous_heads:
385
+ heads[directory] = previous_heads[directory]
386
+ return
387
+ directories = {path.parent, path.parent / "checkpoints", path.parent / "checkpoint-heads"}
388
+ if any(Path(source).parent in directories for source in sources):
389
+ # Legacy evidence can remain accepted without one selected head.
390
+ heads.pop(directory, None)
391
+
392
+
393
+ def _freeze_source_times(
394
+ scan_dir: Path,
395
+ scan_id: str,
396
+ sources: dict[str, str],
397
+ times: dict[str, int],
398
+ parent_context: dict[str, str] | None = None,
399
+ *,
400
+ created_paths: set[str] | None = None,
401
+ ) -> None:
402
+ # Identical result rewrites must not change the order of frozen review evidence.
403
+ observations = {
404
+ path: {"digest": digest, "observedAtNs": str(times[path])}
405
+ for path, digest in sources.items()
406
+ if not _is_source_order_snapshot(path)
407
+ }
408
+ if not observations:
409
+ return
410
+ record = {"scanId": scan_id, "sources": observations}
411
+ if parent_context:
412
+ record["parentContext"] = parent_context
413
+ digest = _digest(record)
414
+ path = f"source-order/{digest}.json"
415
+ if not (scan_dir / path).exists():
416
+ if created_paths is not None and not os.path.lexists(scan_dir / path):
417
+ created_paths.add(path)
418
+ write_scan_local_bytes(scan_dir, path, _encoded(record))
419
+ if _read_saved_result(scan_dir, path, scan_id)[1] != digest:
420
+ raise ContractError("saved source ordering does not match its digest")
421
+ if parent_context:
422
+ for previous in [relative for relative in sources if _is_source_order_snapshot(relative)]:
423
+ previous_record, _, _ = _read_saved_result(scan_dir, previous, scan_id)
424
+ if previous_record.get("parentContext"):
425
+ del sources[previous]
426
+ sources[path] = digest
427
+
428
+
429
+ def _parent_publication_ancestors(
430
+ input_checkpoint: str | None, drafts: dict[str, dict[str, Any]]
431
+ ) -> set[str]:
432
+ """Find stale canonical projections without relying on publication clocks."""
433
+ ancestors = set()
434
+ inputs = set()
435
+ # Input hashes distinguish repeated publication of the same projected result.
436
+ while isinstance(input_checkpoint, str) and input_checkpoint not in inputs:
437
+ inputs.add(input_checkpoint)
438
+ publication = drafts.get(f"checkpoints/{input_checkpoint}", {}).get("publication")
439
+ if not isinstance(publication, dict):
440
+ break
441
+ previous = publication.get("previousCheckpoint")
442
+ if isinstance(previous, str):
443
+ ancestors.add(f"checkpoints/{previous}")
444
+ input_checkpoint = publication.get("previousInputCheckpoint")
445
+ return ancestors
446
+
447
+
448
+ def _admit_parent_checkpoint(
449
+ scan_dir: Path, scan_id: str, checkpoint: dict[str, Any], projected: dict[str, Any]
450
+ ) -> bytes:
451
+ """Bind a Deep input to the accepted head under the host's publication lock."""
452
+ previous = {}
453
+ try:
454
+ previous, _, _ = _read_saved_result(scan_dir, "checkpoint-head.json", scan_id)
455
+ if (
456
+ previous.get("checkpoint") == f"{_digest(projected)}.json"
457
+ and "inputCheckpoint" in previous
458
+ ):
459
+ accepted, _, _ = _read_saved_result(
460
+ scan_dir, f"checkpoints/{previous['inputCheckpoint']}", scan_id
461
+ )
462
+ if {key: value for key, value in accepted.items() if key != "publication"} == {
463
+ key: value for key, value in checkpoint.items() if key != "publication"
464
+ }:
465
+ for relative in _checkpoint_paths(scan_dir, "checkpoints"):
466
+ pending, _, _ = _read_saved_result(scan_dir, relative, scan_id)
467
+ publication = pending.get("publication")
468
+ if (
469
+ isinstance(publication, dict)
470
+ and publication.get("previousInputCheckpoint")
471
+ == previous["inputCheckpoint"]
472
+ ):
473
+ break
474
+ else:
475
+ return _encoded(accepted)
476
+ except (ContractError, OSError, ValueError):
477
+ pass # An absent or unreadable previous head contributes no predecessor.
478
+ return _encoded(
479
+ {
480
+ **checkpoint,
481
+ "publication": {
482
+ "previousCheckpoint": previous.get("checkpoint"),
483
+ "previousInputCheckpoint": previous.get("inputCheckpoint"),
484
+ },
485
+ }
486
+ )
487
+
488
+
489
+ def _capture_parent_observation(
490
+ scan_dir: Path,
491
+ scan_id: str,
492
+ checkpoint: str,
493
+ observed: int,
494
+ legacy_head: bool = False,
495
+ *,
496
+ created_paths: set[str] | None = None,
497
+ ) -> dict[str, tuple[str, int]]:
498
+ """Freeze canonical input, preserving the legacy sealed-parent migration."""
499
+ if legacy_head:
500
+ write_scan_local_bytes(
501
+ scan_dir, "checkpoint-head.json", _encoded({"checkpoint": checkpoint})
502
+ )
503
+ os.utime(scan_dir / "checkpoint-head.json", ns=(observed, observed))
504
+ return _capture_saved_source(
505
+ scan_dir, "checkpoint-head.json", scan_id, created_paths=created_paths
506
+ )
507
+ observation = {"checkpoint": checkpoint, "observedAtNs": str(observed)}
508
+ digest = _digest(observation)
509
+ snapshot = f"checkpoint-heads/{digest}.json"
510
+ if not (scan_dir / snapshot).exists():
511
+ if created_paths is not None and not os.path.lexists(scan_dir / snapshot):
512
+ created_paths.add(snapshot)
513
+ try:
514
+ write_scan_local_bytes(scan_dir, snapshot, _encoded(observation))
515
+ except (ContractError, OSError) as exc:
516
+ # The canonical checkpoint carries this observation without a live head.
517
+ raise _CheckpointSnapshotError(str(exc), {}) from exc
518
+ return {snapshot: (digest, observed)}
519
+
520
+
521
+ def _parent_scan_draft(
522
+ scan_id: str,
523
+ parent_scan: dict[str, Any],
524
+ findings: dict[str, Any],
525
+ coverage: dict[str, Any],
526
+ ) -> dict[str, Any]:
527
+ parent = {
528
+ "scanId": scan_id,
529
+ "findings": findings.get("findings"),
530
+ "coverage": coverage,
531
+ **{
532
+ key: parent_scan[key]
533
+ for key in ("scope", "threatModel", "complete")
534
+ if key in parent_scan
535
+ },
536
+ }
537
+ if not isinstance(parent["findings"], list):
538
+ raise ContractError("Saved parent draft has no findings array")
539
+ return parent
540
+
541
+
542
+ def _model_capture(scan_id: str, model: dict[str, Any]) -> dict[str, Any]:
543
+ return _parent_scan_draft(scan_id, {"threatModel": model}, {"findings": []}, {})
544
+
545
+
546
+ def _restore_frozen_model_capture(
547
+ scan_dir: Path,
548
+ scan_id: str,
549
+ sources: dict[str, str],
550
+ times: dict[str, int] | None,
551
+ model_source: str | None,
552
+ ) -> None:
553
+ if model_source not in (times or {}) or (scan_dir / model_source).exists():
554
+ return
555
+ manifest = _read_scan_local_json(scan_dir, "scan-manifest.json", "Saved scan manifest")
556
+ scan = manifest.get("scan")
557
+ if not isinstance(scan, dict) or not isinstance(scan.get("threatModel"), dict):
558
+ return
559
+ draft = _model_capture(scan_id, scan["threatModel"])
560
+ digest = _digest(draft)
561
+ if model_source != f"checkpoints/{digest}.json" or sources.get(model_source) != digest:
562
+ return
563
+ # A failed first capture can retry only the model and time bound by that stop.
564
+ write_scan_local_bytes(scan_dir, model_source, _encoded(draft))
565
+ observed = times[model_source]
566
+ os.utime(scan_dir / model_source, ns=(observed, observed))
567
+
568
+
569
+ def _read_saved_parent_result(
570
+ scan_dir: Path, scan_id: str
571
+ ) -> tuple[dict[str, Any], dict[str, Any]]:
572
+ manifest = _read_scan_local_json(scan_dir, "scan-manifest.json", "Saved parent manifest")
573
+ findings = _read_scan_local_json(scan_dir, "findings.json", "Saved parent findings")
574
+ coverage = _read_scan_local_json(scan_dir, "coverage.json", "Saved parent coverage")
575
+ parent_scan = manifest.get("scan")
576
+ if not isinstance(parent_scan, dict):
577
+ raise ContractError("Saved parent manifest has no scan object")
578
+ if manifest_is_sealed(parent_scan) and (
579
+ parent_scan.get("id", scan_id) != scan_id
580
+ or findings.get("scanId", scan_id) != scan_id
581
+ or coverage.get("scanId", scan_id) != scan_id
582
+ ):
583
+ raise ContractError("Saved parent documents belong to a different scan")
584
+ return manifest, _parent_scan_draft(scan_id, parent_scan, findings, coverage)
585
+
586
+
587
+ def _require_current_deep_publication(
588
+ db: Any, connection: Any, scan: Any, draft: dict[str, Any]
589
+ ) -> None:
590
+ scan_id = scan["id"]
591
+ run = connection.execute(
592
+ "SELECT * FROM deep_scan_runs WHERE scan_id = ?", (scan_id,)
593
+ ).fetchone()
594
+ publication = draft.get("deepScanPublication")
595
+ if (
596
+ publication is None
597
+ and draft["manifest"]["scan"].get("complete") is False
598
+ and (run is None or run["status"] == "running")
599
+ ):
600
+ return
601
+ if run is None:
602
+ run = db.deep_scan.require_deep_scan_run(connection, scan_id)
603
+ # Finished discovery-only runs hand remaining publication back to the parent.
604
+ if (
605
+ publication is None
606
+ and run["status"] != "running"
607
+ and run["manifest_path"] != str(Path(scan["scan_dir"]) / "scan-manifest.json")
608
+ ):
609
+ return
610
+ db.deep_scan.require_current_coordinator(
611
+ run,
612
+ argparse.Namespace(
613
+ coordinator_generation=publication.get("coordinatorGeneration") if publication else None
614
+ ),
615
+ )
616
+ # Once finish selects the canonical parent, only completion may replay it.
617
+ if publication is None:
618
+ if run["status"] != "running" and run["manifest_path"] == str(
619
+ Path(scan["scan_dir"]) / "scan-manifest.json"
620
+ ):
621
+ raise SystemExit("Deep Scan is terminal; drafts cannot replace its publication.")
622
+ return
623
+
624
+ # Match the durable reducer sequence used by coordinator recovery.
625
+ def reducer_order(worker: Any) -> tuple[int, str]:
626
+ match = re.fullmatch(r"dedup-([0-9]+)", Path(worker["prompt_path"]).parent.name)
627
+ return (int(match[1]) if match else 0, worker["id"])
628
+
629
+ reducer = max(
630
+ (
631
+ worker
632
+ for worker in connection.execute(
633
+ "SELECT * FROM deep_scan_workers WHERE scan_id = ?", (scan_id,)
634
+ )
635
+ if worker["kind"] == "dedup"
636
+ and worker["status"] == "succeeded"
637
+ and worker["result_manifest_path"]
638
+ ),
639
+ key=reducer_order,
640
+ default=None,
641
+ )
642
+ selected_result = reducer["result_manifest_path"] if reducer is not None else None
643
+ if publication["resultPath"] != selected_result:
644
+ raise SystemExit("Deep Scan aggregate belongs to a superseded publication selection.")
645
+
646
+
647
+ if __name__ == "__main__":
648
+ argparse.ArgumentParser(description=__doc__).parse_args()
649
+
650
+
651
+ def _latest_successful_reducer(workers: list[Any]) -> Any | None:
652
+ return max(
653
+ (
654
+ worker
655
+ for worker in workers
656
+ if worker["kind"] == "dedup"
657
+ and worker["status"] == "succeeded"
658
+ and worker["result_manifest_path"]
659
+ ),
660
+ key=lambda worker: (worker["completed_at"] or "", worker["id"]),
661
+ default=None,
662
+ )
663
+
664
+
665
+ def _worker_outputs(scan_dir: Path, worker: Any) -> list[tuple[str, int]]:
666
+ output = Path(worker["artifact_dir"]).relative_to(scan_dir)
667
+ attempts = (output.parent if output.name == "output" else output) / "attempts"
668
+ archived = [
669
+ ((attempts / name).as_posix(), int(name.split("-")[1]))
670
+ for name in _children(scan_dir, attempts.as_posix())
671
+ if re.fullmatch(r"attempt-\d+", name)
672
+ ]
673
+ attempt = int(worker["attempt"] or 0) if "attempt" in worker.keys() else 0
674
+ if worker["kind"] == "discovery" and not attempt:
675
+ attempt = max((attempt for _, attempt in archived), default=0) + 1
676
+ return [(output.as_posix(), attempt), *archived]
677
+
678
+
679
+ def _saved_result_paths(scan_dir: Path, workers: list[Any]) -> Iterator[tuple[str, str | None]]:
680
+ latest_reducer = _latest_successful_reducer(workers)
681
+ yield "checkpoint-head.json", None
682
+ for directory in ("checkpoint-heads", "checkpoints"):
683
+ yield from ((path, None) for path in _checkpoint_paths(scan_dir, directory))
684
+ for worker in workers:
685
+ if worker["kind"] not in {"dedup", "discovery"}:
686
+ continue
687
+ try:
688
+ outputs = _worker_outputs(scan_dir, worker)
689
+ except (TypeError, ValueError):
690
+ continue
691
+ for directory, _ in outputs:
692
+ if worker["kind"] == "discovery":
693
+ yield f"{directory}/checkpoint-head.json", worker["kind"]
694
+ yield from (
695
+ (path, worker["kind"])
696
+ for path in _checkpoint_paths(scan_dir, f"{directory}/checkpoint-heads")
697
+ )
698
+ checkpoint_paths = _checkpoint_paths(scan_dir, f"{directory}/checkpoints")
699
+ if worker["kind"] == "discovery" or checkpoint_paths:
700
+ yield f"{directory}/result.json", worker["kind"]
701
+ yield from ((path, worker["kind"]) for path in checkpoint_paths)
702
+ if worker["result_manifest_path"] and (
703
+ worker["kind"] == "discovery"
704
+ or (latest_reducer is not None and worker["id"] == latest_reducer["id"])
705
+ ):
706
+ try:
707
+ yield (
708
+ Path(worker["result_manifest_path"]).relative_to(scan_dir).as_posix(),
709
+ worker["kind"],
710
+ )
711
+ except ValueError:
712
+ continue