@openai/codex-security 0.8.2 → 0.9.1

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 (649) hide show
  1. package/README.md +201 -178
  2. package/_bundled_plugin/.app.json +4 -0
  3. package/_bundled_plugin/.mcp.json +6 -1
  4. package/_bundled_plugin/examples/completed-scan/findings.json +16 -4
  5. package/_bundled_plugin/examples/completed-scan/report.md +11 -8
  6. package/_bundled_plugin/examples/completed-scan/scan-manifest.json +4 -2
  7. package/_bundled_plugin/mcp/fzstd-LICENSE.txt +21 -0
  8. package/_bundled_plugin/mcp/helpers.mjs +2 -11
  9. package/_bundled_plugin/mcp/helpers.mjs.br +0 -0
  10. package/_bundled_plugin/mcp/native/THIRD_PARTY_NOTICES.txt +3 -210
  11. package/_bundled_plugin/mcp/native/darwin-arm64/unix.node +0 -0
  12. package/_bundled_plugin/mcp/native/darwin-x64/unix.node +0 -0
  13. package/_bundled_plugin/mcp/native/linux-arm64-gnu/unix.node +0 -0
  14. package/_bundled_plugin/mcp/native/linux-arm64-musl/unix.node +0 -0
  15. package/_bundled_plugin/mcp/native/linux-x64-gnu/unix.node +0 -0
  16. package/_bundled_plugin/mcp/native/linux-x64-musl/unix.node +0 -0
  17. package/_bundled_plugin/mcp/native/win32-arm64/windows.node +0 -0
  18. package/_bundled_plugin/mcp/native/win32-x64/windows.node +0 -0
  19. package/_bundled_plugin/mcp/package.json +1 -0
  20. package/_bundled_plugin/mcp/permission-profile-preflight.mjs +51 -14
  21. package/_bundled_plugin/mcp/server.mjs +2 -11
  22. package/_bundled_plugin/mcp/server.mjs.br +0 -0
  23. package/_bundled_plugin/mcp/session-decompression.mjs +712 -0
  24. package/_bundled_plugin/references/artifact-storage.md +1 -1
  25. package/_bundled_plugin/references/core-scan.md +14 -5
  26. package/_bundled_plugin/references/default-severity-rubric.md +33 -0
  27. package/_bundled_plugin/references/final-report.md +5 -3
  28. package/_bundled_plugin/references/finding-detail-fields.md +19 -18
  29. package/_bundled_plugin/references/sarif-adapter.md +8 -2
  30. package/_bundled_plugin/references/scan-artifacts.md +21 -12
  31. package/_bundled_plugin/references/scan-contract.md +27 -33
  32. package/_bundled_plugin/references/threat-model.md +1 -0
  33. package/_bundled_plugin/schemas/tools/scan-draft.schema.json +2 -2
  34. package/_bundled_plugin/scripts/check_deep_scan_target.py +97 -0
  35. package/_bundled_plugin/scripts/codex_profile.mjs +7 -1
  36. package/_bundled_plugin/scripts/deep_scan_config.py +20 -1
  37. package/_bundled_plugin/scripts/deep_scan_workbench.py +188 -454
  38. package/_bundled_plugin/scripts/filesystem_identity.py +1 -0
  39. package/_bundled_plugin/scripts/finalize_scan_contract.py +317 -377
  40. package/_bundled_plugin/scripts/finding_preview.py +25 -29
  41. package/_bundled_plugin/scripts/launch_codex_security_mcp +1 -0
  42. package/_bundled_plugin/scripts/legacy_findings.py +148 -0
  43. package/_bundled_plugin/scripts/posix_scan_local_files.py +198 -0
  44. package/_bundled_plugin/scripts/read_codex_session.mjs +21 -0
  45. package/_bundled_plugin/scripts/report_projection.py +167 -178
  46. package/_bundled_plugin/scripts/review_state.py +8 -2
  47. package/_bundled_plugin/scripts/servicenow_avit.py +20 -0
  48. package/_bundled_plugin/scripts/snapshot_sqlite.py +1 -0
  49. package/_bundled_plugin/scripts/threat_model_projection.py +22 -26
  50. package/_bundled_plugin/scripts/validate_scan_contract.py +9 -7
  51. package/_bundled_plugin/scripts/windows_scan_local_files.py +600 -72
  52. package/_bundled_plugin/scripts/workbench/__init__.py +1 -1
  53. package/_bundled_plugin/scripts/workbench/handoff.py +47 -0
  54. package/_bundled_plugin/scripts/workbench/retained_coverage.py +38 -75
  55. package/_bundled_plugin/scripts/workbench/retained_records.py +18 -30
  56. package/_bundled_plugin/scripts/workbench/runtime_toml.py +4 -1
  57. package/_bundled_plugin/scripts/workbench/saved_coverage.py +152 -248
  58. package/_bundled_plugin/scripts/workbench/saved_outputs.py +9 -0
  59. package/_bundled_plugin/scripts/workbench/saved_sources.py +47 -40
  60. package/_bundled_plugin/scripts/workbench/storage.py +71 -7
  61. package/_bundled_plugin/scripts/workbench_cli.py +121 -14
  62. package/_bundled_plugin/scripts/workbench_constants.py +19 -0
  63. package/_bundled_plugin/scripts/workbench_coverage_receipts.py +3 -3
  64. package/_bundled_plugin/scripts/workbench_db.py +550 -492
  65. package/_bundled_plugin/scripts/workbench_feedback.py +8 -7
  66. package/_bundled_plugin/scripts/workbench_finding_index.py +1 -0
  67. package/_bundled_plugin/scripts/workbench_finding_workflows.py +90 -4
  68. package/_bundled_plugin/scripts/workbench_native_indexes.py +29 -33
  69. package/_bundled_plugin/scripts/workbench_progress.py +10 -1
  70. package/_bundled_plugin/scripts/workbench_publication.py +14 -10
  71. package/_bundled_plugin/scripts/workbench_remediation.py +508 -11
  72. package/_bundled_plugin/scripts/workbench_saved_results.py +132 -77
  73. package/_bundled_plugin/scripts/workbench_scan_history.py +139 -80
  74. package/_bundled_plugin/scripts/workbench_scan_start.py +84 -25
  75. package/_bundled_plugin/scripts/workbench_scan_usage.py +666 -117
  76. package/_bundled_plugin/scripts/workbench_schema.py +44 -55
  77. package/_bundled_plugin/scripts/workbench_setup.py +251 -0
  78. package/_bundled_plugin/scripts/workbench_source_excerpt.py +13 -0
  79. package/_bundled_plugin/scripts/workbench_source_scopes.py +33 -23
  80. package/_bundled_plugin/scripts/workbench_target.py +330 -66
  81. package/_bundled_plugin/scripts/workbench_target_state.py +3 -0
  82. package/_bundled_plugin/scripts/workbench_validation.py +44 -12
  83. package/_bundled_plugin/shared/workbench-history-repairs.json +19 -16
  84. package/_bundled_plugin/shared/workbench-migrations.json +53 -0
  85. package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +1 -1
  86. package/_bundled_plugin/skills/attack-path-analysis/references/severity-policy.md +4 -68
  87. package/_bundled_plugin/skills/check-skills/SKILL.md +43 -0
  88. package/_bundled_plugin/skills/check-skills/agents/openai.yaml +4 -0
  89. package/_bundled_plugin/skills/cloud-findings/SKILL.md +6 -4
  90. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +7 -50
  91. package/_bundled_plugin/skills/fix-finding/SKILL.md +19 -2
  92. package/_bundled_plugin/skills/publish-findings/SKILL.md +81 -45
  93. package/_bundled_plugin/skills/publish-findings/references/checkmarx-import.md +37 -0
  94. package/_bundled_plugin/skills/publish-findings/references/wiz-import.md +5 -24
  95. package/_bundled_plugin/skills/publish-findings/scripts/collect_checkmarx.py +464 -0
  96. package/_bundled_plugin/skills/threat-model-visualize/scripts/build_explorer.py +36 -9
  97. package/_bundled_plugin/skills/track-findings/SKILL.md +5 -3
  98. package/_bundled_plugin/skills/triage-finding/references/github-rest-intake.md +22 -68
  99. package/_bundled_plugin/skills/triage-finding/references/ticket-intake.md +12 -6
  100. package/dist/api.d.ts +45 -5
  101. package/dist/api.d.ts.map +1 -1
  102. package/dist/api.js +1144 -411
  103. package/dist/api.js.map +1 -1
  104. package/dist/auth.d.ts.map +1 -1
  105. package/dist/auth.js +10 -7
  106. package/dist/auth.js.map +1 -1
  107. package/dist/bedrock-oss-pricing.d.ts +6 -0
  108. package/dist/bedrock-oss-pricing.d.ts.map +1 -0
  109. package/dist/bedrock-oss-pricing.js +58 -0
  110. package/dist/bedrock-oss-pricing.js.map +1 -0
  111. package/dist/bedrock-pricing.d.ts +15 -0
  112. package/dist/bedrock-pricing.d.ts.map +1 -0
  113. package/dist/bedrock-pricing.js +117 -0
  114. package/dist/bedrock-pricing.js.map +1 -0
  115. package/dist/blackduck-findings.d.ts +8 -0
  116. package/dist/blackduck-findings.d.ts.map +1 -0
  117. package/dist/blackduck-findings.js +320 -0
  118. package/dist/blackduck-findings.js.map +1 -0
  119. package/dist/bulk-scan-discovery.d.ts.map +1 -1
  120. package/dist/bulk-scan-discovery.js +51 -36
  121. package/dist/bulk-scan-discovery.js.map +1 -1
  122. package/dist/checkmarx-evidence.d.ts +19 -0
  123. package/dist/checkmarx-evidence.d.ts.map +1 -0
  124. package/dist/checkmarx-evidence.js +36 -0
  125. package/dist/checkmarx-evidence.js.map +1 -0
  126. package/dist/checkmarx-findings.d.ts +4 -0
  127. package/dist/checkmarx-findings.d.ts.map +1 -0
  128. package/dist/checkmarx-findings.js +27 -0
  129. package/dist/checkmarx-findings.js.map +1 -0
  130. package/dist/checkmarx-one.d.ts +4 -0
  131. package/dist/checkmarx-one.d.ts.map +1 -0
  132. package/dist/checkmarx-one.js +53 -0
  133. package/dist/checkmarx-one.js.map +1 -0
  134. package/dist/checkmarx-repository.d.ts +12 -0
  135. package/dist/checkmarx-repository.d.ts.map +1 -0
  136. package/dist/checkmarx-repository.js +181 -0
  137. package/dist/checkmarx-repository.js.map +1 -0
  138. package/dist/checkmarx-xml.d.ts +12 -0
  139. package/dist/checkmarx-xml.d.ts.map +1 -0
  140. package/dist/checkmarx-xml.js +196 -0
  141. package/dist/checkmarx-xml.js.map +1 -0
  142. package/dist/classify-severity-records.d.ts +6 -9
  143. package/dist/classify-severity-records.d.ts.map +1 -1
  144. package/dist/classify-severity-records.js +9 -21
  145. package/dist/classify-severity-records.js.map +1 -1
  146. package/dist/classify-severity.d.ts +4 -1
  147. package/dist/classify-severity.d.ts.map +1 -1
  148. package/dist/classify-severity.js +30 -13
  149. package/dist/classify-severity.js.map +1 -1
  150. package/dist/cli.d.ts +14 -5
  151. package/dist/cli.d.ts.map +1 -1
  152. package/dist/cli.js +2112 -1266
  153. package/dist/cli.js.map +1 -1
  154. package/dist/cloud-publish.js +2 -2
  155. package/dist/cloud-publish.js.map +1 -1
  156. package/dist/codex-prompt.d.ts +2 -0
  157. package/dist/codex-prompt.d.ts.map +1 -1
  158. package/dist/codex-prompt.js +6 -0
  159. package/dist/codex-prompt.js.map +1 -1
  160. package/dist/component-scan.d.ts +16 -3
  161. package/dist/component-scan.d.ts.map +1 -1
  162. package/dist/component-scan.js +32 -6
  163. package/dist/component-scan.js.map +1 -1
  164. package/dist/config.d.ts +2 -1
  165. package/dist/config.d.ts.map +1 -1
  166. package/dist/config.js +221 -65
  167. package/dist/config.js.map +1 -1
  168. package/dist/continuum-findings.d.ts +7 -0
  169. package/dist/continuum-findings.d.ts.map +1 -0
  170. package/dist/continuum-findings.js +225 -0
  171. package/dist/continuum-findings.js.map +1 -0
  172. package/dist/contract-values.d.ts +30 -0
  173. package/dist/contract-values.d.ts.map +1 -0
  174. package/dist/contract-values.js +78 -0
  175. package/dist/contract-values.js.map +1 -0
  176. package/dist/contract.d.ts +2 -20
  177. package/dist/contract.d.ts.map +1 -1
  178. package/dist/contract.js +58 -669
  179. package/dist/contract.js.map +1 -1
  180. package/dist/cost-model.d.ts +13 -1
  181. package/dist/cost-model.d.ts.map +1 -1
  182. package/dist/cost-model.js +66 -115
  183. package/dist/cost-model.js.map +1 -1
  184. package/dist/cost.d.ts +61 -4
  185. package/dist/cost.d.ts.map +1 -1
  186. package/dist/cost.js +2560 -100
  187. package/dist/cost.js.map +1 -1
  188. package/dist/custom-publish.d.ts +4 -0
  189. package/dist/custom-publish.d.ts.map +1 -1
  190. package/dist/custom-publish.js +40 -3
  191. package/dist/custom-publish.js.map +1 -1
  192. package/dist/custom-validation-prompt.d.ts +1 -1
  193. package/dist/custom-validation-prompt.d.ts.map +1 -1
  194. package/dist/custom-validation-prompt.js +5 -0
  195. package/dist/custom-validation-prompt.js.map +1 -1
  196. package/dist/custom-validation.d.ts +2 -0
  197. package/dist/custom-validation.d.ts.map +1 -1
  198. package/dist/custom-validation.js +4 -0
  199. package/dist/custom-validation.js.map +1 -1
  200. package/dist/deduplication/deduplication-prompts.d.ts +4 -4
  201. package/dist/deduplication/deduplication-prompts.d.ts.map +1 -1
  202. package/dist/deduplication/deduplication-prompts.js +2 -2
  203. package/dist/deduplication/deduplication-prompts.js.map +1 -1
  204. package/dist/deduplication/deduplication-reviewer.d.ts +14 -11
  205. package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -1
  206. package/dist/deduplication/deduplication-reviewer.js +11 -4
  207. package/dist/deduplication/deduplication-reviewer.js.map +1 -1
  208. package/dist/deduplication/deduplication.js.map +1 -1
  209. package/dist/deduplication/finding-schema.d.ts +19 -1
  210. package/dist/deduplication/finding-schema.d.ts.map +1 -1
  211. package/dist/deduplication/finding-schema.js +25 -0
  212. package/dist/deduplication/finding-schema.js.map +1 -1
  213. package/dist/deduplication/local.d.ts +2 -2
  214. package/dist/deduplication/local.d.ts.map +1 -1
  215. package/dist/deduplication/local.js +5 -10
  216. package/dist/deduplication/local.js.map +1 -1
  217. package/dist/deduplication/records.d.ts +8 -2
  218. package/dist/deduplication/records.d.ts.map +1 -1
  219. package/dist/deduplication/records.js +36 -8
  220. package/dist/deduplication/records.js.map +1 -1
  221. package/dist/deduplication/refusal.d.ts.map +1 -1
  222. package/dist/deduplication/refusal.js +25 -5
  223. package/dist/deduplication/refusal.js.map +1 -1
  224. package/dist/deduplication/scan.d.ts +3 -0
  225. package/dist/deduplication/scan.d.ts.map +1 -1
  226. package/dist/deduplication/scan.js +1 -1
  227. package/dist/deduplication/scan.js.map +1 -1
  228. package/dist/deep-config.d.ts +0 -1
  229. package/dist/deep-config.d.ts.map +1 -1
  230. package/dist/deep-config.js +22 -33
  231. package/dist/deep-config.js.map +1 -1
  232. package/dist/deep-scan-defaults.d.ts +2 -0
  233. package/dist/deep-scan-defaults.d.ts.map +1 -1
  234. package/dist/deep-scan-defaults.js +28 -1
  235. package/dist/deep-scan-defaults.js.map +1 -1
  236. package/dist/deep-scan.d.ts +13 -0
  237. package/dist/deep-scan.d.ts.map +1 -1
  238. package/dist/deep-scan.js +262 -7
  239. package/dist/deep-scan.js.map +1 -1
  240. package/dist/default-severity-rubric.d.ts +1 -2
  241. package/dist/default-severity-rubric.d.ts.map +1 -1
  242. package/dist/default-severity-rubric.js +2 -66
  243. package/dist/default-severity-rubric.js.map +1 -1
  244. package/dist/endor-findings.d.ts +3 -1
  245. package/dist/endor-findings.d.ts.map +1 -1
  246. package/dist/endor-findings.js +108 -80
  247. package/dist/endor-findings.js.map +1 -1
  248. package/dist/errors.d.ts +1 -1
  249. package/dist/errors.d.ts.map +1 -1
  250. package/dist/errors.js +9 -2
  251. package/dist/errors.js.map +1 -1
  252. package/dist/external-findings-import.d.ts +5 -3
  253. package/dist/external-findings-import.d.ts.map +1 -1
  254. package/dist/external-findings-import.js +11 -16
  255. package/dist/external-findings-import.js.map +1 -1
  256. package/dist/external-findings-local.d.ts +15 -4
  257. package/dist/external-findings-local.d.ts.map +1 -1
  258. package/dist/external-findings-local.js +619 -225
  259. package/dist/external-findings-local.js.map +1 -1
  260. package/dist/external-findings-publish.d.ts +20 -6
  261. package/dist/external-findings-publish.d.ts.map +1 -1
  262. package/dist/external-findings-publish.js +2356 -308
  263. package/dist/external-findings-publish.js.map +1 -1
  264. package/dist/external-findings-read.d.ts +28 -2
  265. package/dist/external-findings-read.d.ts.map +1 -1
  266. package/dist/external-findings-read.js +174 -81
  267. package/dist/external-findings-read.js.map +1 -1
  268. package/dist/external-findings.d.ts +17 -3
  269. package/dist/external-findings.d.ts.map +1 -1
  270. package/dist/external-findings.js +153 -1
  271. package/dist/external-findings.js.map +1 -1
  272. package/dist/external-import-contract.d.ts +33 -1
  273. package/dist/external-import-contract.d.ts.map +1 -1
  274. package/dist/external-import-contract.js +335 -51
  275. package/dist/external-import-contract.js.map +1 -1
  276. package/dist/external-import-models.d.ts +1 -1
  277. package/dist/external-import-models.d.ts.map +1 -1
  278. package/dist/external-import-models.js.map +1 -1
  279. package/dist/feedback.d.ts.map +1 -1
  280. package/dist/feedback.js +5 -4
  281. package/dist/feedback.js.map +1 -1
  282. package/dist/finding-prompt.d.ts +5 -0
  283. package/dist/finding-prompt.d.ts.map +1 -0
  284. package/dist/finding-prompt.js +32 -0
  285. package/dist/finding-prompt.js.map +1 -0
  286. package/dist/finding-retrieval.d.ts +3 -3
  287. package/dist/finding-retrieval.d.ts.map +1 -1
  288. package/dist/finding-retrieval.js.map +1 -1
  289. package/dist/finding-workflow.d.ts +6 -1
  290. package/dist/finding-workflow.d.ts.map +1 -1
  291. package/dist/finding-workflow.js +2 -2
  292. package/dist/finding-workflow.js.map +1 -1
  293. package/dist/findings-client.d.ts +1 -0
  294. package/dist/findings-client.d.ts.map +1 -1
  295. package/dist/findings-client.js +27 -3
  296. package/dist/findings-client.js.map +1 -1
  297. package/dist/git-remote.d.ts +4 -0
  298. package/dist/git-remote.d.ts.map +1 -1
  299. package/dist/git-remote.js +20 -0
  300. package/dist/git-remote.js.map +1 -1
  301. package/dist/github-advisories.d.ts +3 -12
  302. package/dist/github-advisories.d.ts.map +1 -1
  303. package/dist/github-advisories.js +9 -31
  304. package/dist/github-advisories.js.map +1 -1
  305. package/dist/github-dependabot-findings.d.ts.map +1 -1
  306. package/dist/github-dependabot-findings.js +22 -20
  307. package/dist/github-dependabot-findings.js.map +1 -1
  308. package/dist/github-findings.d.ts +4 -1
  309. package/dist/github-findings.d.ts.map +1 -1
  310. package/dist/github-findings.js +392 -79
  311. package/dist/github-findings.js.map +1 -1
  312. package/dist/github.d.ts +17 -7
  313. package/dist/github.d.ts.map +1 -1
  314. package/dist/github.js +43 -30
  315. package/dist/github.js.map +1 -1
  316. package/dist/host-protocol.d.ts +2 -2
  317. package/dist/host-protocol.d.ts.map +1 -1
  318. package/dist/host-protocol.js.map +1 -1
  319. package/dist/host-records-protocol.d.ts +10 -11
  320. package/dist/host-records-protocol.d.ts.map +1 -1
  321. package/dist/host-records-protocol.js +69 -9
  322. package/dist/host-records-protocol.js.map +1 -1
  323. package/dist/hosted-git-remotes.d.ts +5 -0
  324. package/dist/hosted-git-remotes.d.ts.map +1 -0
  325. package/dist/hosted-git-remotes.js +285 -0
  326. package/dist/hosted-git-remotes.js.map +1 -0
  327. package/dist/hosted-git-repository.d.ts +28 -0
  328. package/dist/hosted-git-repository.d.ts.map +1 -0
  329. package/dist/hosted-git-repository.js +222 -0
  330. package/dist/hosted-git-repository.js.map +1 -0
  331. package/dist/hosted-scan-protocol.js +2 -2
  332. package/dist/hosted-scan-protocol.js.map +1 -1
  333. package/dist/hosted-scan.d.ts +15 -0
  334. package/dist/hosted-scan.d.ts.map +1 -1
  335. package/dist/hosted-scan.js +71 -44
  336. package/dist/hosted-scan.js.map +1 -1
  337. package/dist/hosted-scanner-http.d.ts +21 -0
  338. package/dist/hosted-scanner-http.d.ts.map +1 -0
  339. package/dist/hosted-scanner-http.js +204 -0
  340. package/dist/hosted-scanner-http.js.map +1 -0
  341. package/dist/hosted-scanner-v1.d.ts +6 -0
  342. package/dist/hosted-scanner-v1.d.ts.map +1 -0
  343. package/dist/hosted-scanner-v1.js +60 -0
  344. package/dist/hosted-scanner-v1.js.map +1 -0
  345. package/dist/import-scan.d.ts.map +1 -1
  346. package/dist/import-scan.js +3 -0
  347. package/dist/import-scan.js.map +1 -1
  348. package/dist/index.d.ts +24 -6
  349. package/dist/index.d.ts.map +1 -1
  350. package/dist/index.js +15 -1
  351. package/dist/index.js.map +1 -1
  352. package/dist/inspector-findings.d.ts.map +1 -1
  353. package/dist/inspector-findings.js +58 -81
  354. package/dist/inspector-findings.js.map +1 -1
  355. package/dist/knowledge-base.d.ts.map +1 -1
  356. package/dist/knowledge-base.js +86 -10
  357. package/dist/knowledge-base.js.map +1 -1
  358. package/dist/local-git-metadata.d.ts +5 -0
  359. package/dist/local-git-metadata.d.ts.map +1 -0
  360. package/dist/local-git-metadata.js +130 -0
  361. package/dist/local-git-metadata.js.map +1 -0
  362. package/dist/multiscan.d.ts +6 -4
  363. package/dist/multiscan.d.ts.map +1 -1
  364. package/dist/multiscan.js +160 -77
  365. package/dist/multiscan.js.map +1 -1
  366. package/dist/native-json-findings.d.ts +2 -1
  367. package/dist/native-json-findings.d.ts.map +1 -1
  368. package/dist/native-json-findings.js +12 -2
  369. package/dist/native-json-findings.js.map +1 -1
  370. package/dist/opengrep-findings.d.ts +1 -0
  371. package/dist/opengrep-findings.d.ts.map +1 -1
  372. package/dist/opengrep-findings.js +49 -31
  373. package/dist/opengrep-findings.js.map +1 -1
  374. package/dist/operation-telemetry.d.ts +104 -0
  375. package/dist/operation-telemetry.d.ts.map +1 -0
  376. package/dist/operation-telemetry.js +293 -0
  377. package/dist/operation-telemetry.js.map +1 -0
  378. package/dist/patch-tui.d.ts.map +1 -1
  379. package/dist/patch-tui.js +90 -35
  380. package/dist/patch-tui.js.map +1 -1
  381. package/dist/plugin-python-probe.d.ts +4 -0
  382. package/dist/plugin-python-probe.d.ts.map +1 -0
  383. package/dist/plugin-python-probe.js +10 -0
  384. package/dist/plugin-python-probe.js.map +1 -0
  385. package/dist/private-finding-import.d.ts +341 -0
  386. package/dist/private-finding-import.d.ts.map +1 -0
  387. package/dist/private-finding-import.js +411 -0
  388. package/dist/private-finding-import.js.map +1 -0
  389. package/dist/project-config.d.ts.map +1 -1
  390. package/dist/project-config.js +2 -2
  391. package/dist/project-config.js.map +1 -1
  392. package/dist/provider-profile.d.ts +11 -3
  393. package/dist/provider-profile.d.ts.map +1 -1
  394. package/dist/provider-profile.js +80 -11
  395. package/dist/provider-profile.js.map +1 -1
  396. package/dist/publication-telemetry.d.ts +45 -0
  397. package/dist/publication-telemetry.d.ts.map +1 -0
  398. package/dist/publication-telemetry.js +50 -0
  399. package/dist/publication-telemetry.js.map +1 -0
  400. package/dist/publication.js +7 -1
  401. package/dist/publication.js.map +1 -1
  402. package/dist/publish.d.ts +4 -0
  403. package/dist/publish.d.ts.map +1 -1
  404. package/dist/publish.js +42 -2
  405. package/dist/publish.js.map +1 -1
  406. package/dist/reader-selection.d.ts +15 -0
  407. package/dist/reader-selection.d.ts.map +1 -0
  408. package/dist/reader-selection.js +89 -0
  409. package/dist/reader-selection.js.map +1 -0
  410. package/dist/record.d.ts +6 -1
  411. package/dist/record.d.ts.map +1 -1
  412. package/dist/record.js +53 -2
  413. package/dist/record.js.map +1 -1
  414. package/dist/remediation.d.ts +3 -1
  415. package/dist/remediation.d.ts.map +1 -1
  416. package/dist/remediation.js +28 -2
  417. package/dist/remediation.js.map +1 -1
  418. package/dist/request-metadata.d.ts +3 -0
  419. package/dist/request-metadata.d.ts.map +1 -1
  420. package/dist/request-metadata.js +9 -0
  421. package/dist/request-metadata.js.map +1 -1
  422. package/dist/result.d.ts +6 -0
  423. package/dist/result.d.ts.map +1 -1
  424. package/dist/result.js +3 -1
  425. package/dist/result.js.map +1 -1
  426. package/dist/retry-windows-rename.d.ts +8 -0
  427. package/dist/retry-windows-rename.d.ts.map +1 -0
  428. package/dist/retry-windows-rename.js +24 -0
  429. package/dist/retry-windows-rename.js.map +1 -0
  430. package/dist/runtime.d.ts +22 -1
  431. package/dist/runtime.d.ts.map +1 -1
  432. package/dist/runtime.js +408 -162
  433. package/dist/runtime.js.map +1 -1
  434. package/dist/saved-scan-bootstrap.d.ts +1 -0
  435. package/dist/saved-scan-bootstrap.d.ts.map +1 -1
  436. package/dist/saved-scan-bootstrap.js +18 -15
  437. package/dist/saved-scan-bootstrap.js.map +1 -1
  438. package/dist/scan-activity.d.ts.map +1 -1
  439. package/dist/scan-activity.js +172 -64
  440. package/dist/scan-activity.js.map +1 -1
  441. package/dist/scan-comparison.d.ts +10 -5
  442. package/dist/scan-comparison.d.ts.map +1 -1
  443. package/dist/scan-comparison.js +68 -25
  444. package/dist/scan-comparison.js.map +1 -1
  445. package/dist/scan-dashboard.d.ts +7 -6
  446. package/dist/scan-dashboard.d.ts.map +1 -1
  447. package/dist/scan-dashboard.js +91 -79
  448. package/dist/scan-dashboard.js.map +1 -1
  449. package/dist/scan-executor.d.ts +12 -1
  450. package/dist/scan-executor.d.ts.map +1 -1
  451. package/dist/scan-executor.js +22 -2
  452. package/dist/scan-executor.js.map +1 -1
  453. package/dist/scan-history-renderer.d.ts.map +1 -1
  454. package/dist/scan-history-renderer.js +31 -28
  455. package/dist/scan-history-renderer.js.map +1 -1
  456. package/dist/scan-logs.d.ts +12 -0
  457. package/dist/scan-logs.d.ts.map +1 -1
  458. package/dist/scan-logs.js +106 -77
  459. package/dist/scan-logs.js.map +1 -1
  460. package/dist/scan-measurement.d.ts +26 -0
  461. package/dist/scan-measurement.d.ts.map +1 -0
  462. package/dist/scan-measurement.js +77 -0
  463. package/dist/scan-measurement.js.map +1 -0
  464. package/dist/scan-progress.d.ts +1 -0
  465. package/dist/scan-progress.d.ts.map +1 -1
  466. package/dist/scan-progress.js +3 -0
  467. package/dist/scan-progress.js.map +1 -1
  468. package/dist/scan-sessions.d.ts.map +1 -1
  469. package/dist/scan-sessions.js +4 -1
  470. package/dist/scan-sessions.js.map +1 -1
  471. package/dist/scan-settings.d.ts +1 -1
  472. package/dist/scan-settings.d.ts.map +1 -1
  473. package/dist/scan-settings.js +1 -24
  474. package/dist/scan-settings.js.map +1 -1
  475. package/dist/scanner-import.d.ts +64 -6
  476. package/dist/scanner-import.d.ts.map +1 -1
  477. package/dist/scanner-import.js +406 -48
  478. package/dist/scanner-import.js.map +1 -1
  479. package/dist/scanner-repository-url.d.ts +17 -0
  480. package/dist/scanner-repository-url.d.ts.map +1 -0
  481. package/dist/scanner-repository-url.js +71 -0
  482. package/dist/scanner-repository-url.js.map +1 -0
  483. package/dist/security-policy.d.ts +1 -1
  484. package/dist/security-policy.d.ts.map +1 -1
  485. package/dist/security-policy.js.map +1 -1
  486. package/dist/semgrep-findings.d.ts +6 -3
  487. package/dist/semgrep-findings.d.ts.map +1 -1
  488. package/dist/semgrep-findings.js +124 -88
  489. package/dist/semgrep-findings.js.map +1 -1
  490. package/dist/semgrep-guardian-findings.d.ts +38 -0
  491. package/dist/semgrep-guardian-findings.d.ts.map +1 -0
  492. package/dist/semgrep-guardian-findings.js +165 -0
  493. package/dist/semgrep-guardian-findings.js.map +1 -0
  494. package/dist/semgrep-platform-collector.d.ts +26 -0
  495. package/dist/semgrep-platform-collector.d.ts.map +1 -0
  496. package/dist/semgrep-platform-collector.js +108 -0
  497. package/dist/semgrep-platform-collector.js.map +1 -0
  498. package/dist/semgrep-platform-findings.d.ts +213 -0
  499. package/dist/semgrep-platform-findings.d.ts.map +1 -0
  500. package/dist/semgrep-platform-findings.js +1078 -0
  501. package/dist/semgrep-platform-findings.js.map +1 -0
  502. package/dist/semgrep-platform-v1.d.ts +6 -0
  503. package/dist/semgrep-platform-v1.d.ts.map +1 -0
  504. package/dist/semgrep-platform-v1.js +83 -0
  505. package/dist/semgrep-platform-v1.js.map +1 -0
  506. package/dist/server/api.d.ts +1 -1
  507. package/dist/server/api.d.ts.map +1 -1
  508. package/dist/server/api.js.map +1 -1
  509. package/dist/server/embedding-tokenizer.cdx.json +33 -0
  510. package/dist/server/embedding-tokenizer.d.ts +2 -0
  511. package/dist/server/embedding-tokenizer.d.ts.map +1 -0
  512. package/dist/server/embedding-tokenizer.js +322 -0
  513. package/dist/server/embedding-tokenizer.js.map +6 -0
  514. package/dist/server/embeddings.d.ts +16 -2
  515. package/dist/server/embeddings.d.ts.map +1 -1
  516. package/dist/server/embeddings.js +35 -5
  517. package/dist/server/embeddings.js.map +1 -1
  518. package/dist/session-lines.d.ts +5 -0
  519. package/dist/session-lines.d.ts.map +1 -0
  520. package/dist/session-lines.js +60 -0
  521. package/dist/session-lines.js.map +1 -0
  522. package/dist/severity-review.d.ts +3 -1
  523. package/dist/severity-review.d.ts.map +1 -1
  524. package/dist/severity-review.js +3 -2
  525. package/dist/severity-review.js.map +1 -1
  526. package/dist/severity-rubric.d.ts +20 -0
  527. package/dist/severity-rubric.d.ts.map +1 -0
  528. package/dist/severity-rubric.js +73 -0
  529. package/dist/severity-rubric.js.map +1 -0
  530. package/dist/severity-store.js +15 -5
  531. package/dist/severity-store.js.map +1 -1
  532. package/dist/skill-authentication.d.ts +1 -0
  533. package/dist/skill-authentication.d.ts.map +1 -1
  534. package/dist/skill-authentication.js +1 -0
  535. package/dist/skill-authentication.js.map +1 -1
  536. package/dist/snyk-findings.d.ts +7 -2
  537. package/dist/snyk-findings.d.ts.map +1 -1
  538. package/dist/snyk-findings.js +108 -84
  539. package/dist/snyk-findings.js.map +1 -1
  540. package/dist/snyk-rest-findings.d.ts +76 -0
  541. package/dist/snyk-rest-findings.d.ts.map +1 -0
  542. package/dist/snyk-rest-findings.js +1051 -0
  543. package/dist/snyk-rest-findings.js.map +1 -0
  544. package/dist/snyk-rest-v1.d.ts +28 -0
  545. package/dist/snyk-rest-v1.d.ts.map +1 -0
  546. package/dist/snyk-rest-v1.js +132 -0
  547. package/dist/snyk-rest-v1.js.map +1 -0
  548. package/dist/snyk-token-origin.d.ts +4 -0
  549. package/dist/snyk-token-origin.d.ts.map +1 -0
  550. package/dist/snyk-token-origin.js +82 -0
  551. package/dist/snyk-token-origin.js.map +1 -0
  552. package/dist/sonar-collector.d.ts +16 -0
  553. package/dist/sonar-collector.d.ts.map +1 -0
  554. package/dist/sonar-collector.js +380 -0
  555. package/dist/sonar-collector.js.map +1 -0
  556. package/dist/sonar-findings.d.ts +49 -0
  557. package/dist/sonar-findings.d.ts.map +1 -0
  558. package/dist/sonar-findings.js +288 -0
  559. package/dist/sonar-findings.js.map +1 -0
  560. package/dist/sonar-import.d.ts +111 -0
  561. package/dist/sonar-import.d.ts.map +1 -0
  562. package/dist/sonar-import.js +277 -0
  563. package/dist/sonar-import.js.map +1 -0
  564. package/dist/suggest-owners.d.ts.map +1 -1
  565. package/dist/suggest-owners.js +2 -1
  566. package/dist/suggest-owners.js.map +1 -1
  567. package/dist/target-drift.d.ts +9 -0
  568. package/dist/target-drift.d.ts.map +1 -0
  569. package/dist/target-drift.js +27 -0
  570. package/dist/target-drift.js.map +1 -0
  571. package/dist/targets.d.ts +5 -0
  572. package/dist/targets.d.ts.map +1 -1
  573. package/dist/targets.js +73 -31
  574. package/dist/targets.js.map +1 -1
  575. package/dist/tenable-findings.d.ts +9 -0
  576. package/dist/tenable-findings.d.ts.map +1 -0
  577. package/dist/tenable-findings.js +122 -0
  578. package/dist/tenable-findings.js.map +1 -0
  579. package/dist/terminal-columns.d.ts +4 -0
  580. package/dist/terminal-columns.d.ts.map +1 -0
  581. package/dist/terminal-columns.js +19 -0
  582. package/dist/terminal-columns.js.map +1 -0
  583. package/dist/terminal-stream.d.ts +13 -0
  584. package/dist/terminal-stream.d.ts.map +1 -0
  585. package/dist/terminal-stream.js +35 -0
  586. package/dist/terminal-stream.js.map +1 -0
  587. package/dist/trivy-findings.d.ts +19 -0
  588. package/dist/trivy-findings.d.ts.map +1 -0
  589. package/dist/trivy-findings.js +435 -0
  590. package/dist/trivy-findings.js.map +1 -0
  591. package/dist/trivy-import.d.ts +19 -0
  592. package/dist/trivy-import.d.ts.map +1 -0
  593. package/dist/trivy-import.js +151 -0
  594. package/dist/trivy-import.js.map +1 -0
  595. package/dist/validation-records.d.ts +0 -3
  596. package/dist/validation-records.d.ts.map +1 -1
  597. package/dist/validation-records.js +1 -17
  598. package/dist/validation-records.js.map +1 -1
  599. package/dist/veracode-findings.d.ts +9 -0
  600. package/dist/veracode-findings.d.ts.map +1 -0
  601. package/dist/veracode-findings.js +409 -0
  602. package/dist/veracode-findings.js.map +1 -0
  603. package/dist/vertex-pricing.d.ts +8 -0
  604. package/dist/vertex-pricing.d.ts.map +1 -0
  605. package/dist/vertex-pricing.js +20 -0
  606. package/dist/vertex-pricing.js.map +1 -0
  607. package/dist/windows-path.d.ts +1 -0
  608. package/dist/windows-path.d.ts.map +1 -1
  609. package/dist/windows-path.js +27 -1
  610. package/dist/windows-path.js.map +1 -1
  611. package/dist/wiz-csv.d.ts +2 -2
  612. package/dist/wiz-csv.d.ts.map +1 -1
  613. package/dist/wiz-csv.js +1 -3
  614. package/dist/wiz-csv.js.map +1 -1
  615. package/dist/wiz-findings.d.ts +14 -2
  616. package/dist/wiz-findings.d.ts.map +1 -1
  617. package/dist/wiz-findings.js +143 -50
  618. package/dist/wiz-findings.js.map +1 -1
  619. package/docs/classify-severity-records.md +28 -13
  620. package/docs/cli.md +623 -843
  621. package/docs/dedupe-records.md +54 -24
  622. package/docs/findings-service.md +87 -47
  623. package/docs/hosted-remediation.md +14 -7
  624. package/docs/hosted-scans.md +64 -41
  625. package/docs/telemetry.md +236 -0
  626. package/docs/validate-records.md +78 -44
  627. package/package.json +19 -17
  628. package/schemas/external-findings.schema.json +14 -1
  629. package/schemas/hosted-scan-v2.fixture.json +16 -1
  630. package/_bundled_plugin/mcp/helpers.mjs.br.part-000 +0 -0
  631. package/_bundled_plugin/mcp/helpers.mjs.br.part-001 +0 -0
  632. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  633. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  634. package/_bundled_plugin/mcp/server.mjs.br.part-002 +0 -0
  635. package/_bundled_plugin/schemas/tools/deep-reducer.schema.json +0 -96
  636. package/_bundled_plugin/scripts/validate_tracking_source.py +0 -82
  637. package/_bundled_plugin/scripts/workbench_local_dedupe.py +0 -136
  638. package/dist/deduplication/records-protocol.d.ts +0 -4
  639. package/dist/deduplication/records-protocol.d.ts.map +0 -1
  640. package/dist/deduplication/records-protocol.js +0 -49
  641. package/dist/deduplication/records-protocol.js.map +0 -1
  642. package/dist/external-imports-feature.d.ts +0 -6
  643. package/dist/external-imports-feature.d.ts.map +0 -1
  644. package/dist/external-imports-feature.js +0 -51
  645. package/dist/external-imports-feature.js.map +0 -1
  646. package/dist/finding-normalization.d.ts +0 -5
  647. package/dist/finding-normalization.d.ts.map +0 -1
  648. package/dist/finding-normalization.js +0 -120
  649. package/dist/finding-normalization.js.map +0 -1
@@ -17,7 +17,7 @@ import tempfile
17
17
  import time
18
18
  import uuid
19
19
  from collections.abc import Callable
20
- from contextlib import closing, contextmanager, nullcontext
20
+ from contextlib import ExitStack, closing, contextmanager, nullcontext
21
21
  from datetime import datetime, timedelta, timezone
22
22
  from functools import partial
23
23
  from pathlib import Path, PurePosixPath
@@ -43,7 +43,6 @@ import workbench_remediation as remediation_state
43
43
  import workbench_saved_results as saved_results
44
44
  import workbench_scan_history as scan_history
45
45
  import workbench_scan_usage as scan_usage
46
- from deep_scan_config import resolve_deep_scan_setup
47
46
  from finalize_scan_contract import (
48
47
  PRODUCER_NAME,
49
48
  ContractError,
@@ -56,24 +55,26 @@ from finalize_scan_contract import (
56
55
  )
57
56
  from finding_preview import bounded_finding_details
58
57
  from workbench import handoff
59
- from workbench.storage import create_private_directory, resolve_scan_root, state_dir
58
+ from workbench.storage import (
59
+ create_private_directory,
60
+ resolve_scan_root,
61
+ saved_deep_workers,
62
+ state_dir,
63
+ )
60
64
  from workbench_cli import parse_args
61
65
  from workbench_constants import (
62
66
  ARTIFACTS,
63
67
  CLAIM_LEASE_SECONDS,
64
68
  DELIVERED_ACTION_LEASE_SECONDS,
65
- DIFF_TARGET_KINDS,
66
- EMPTY_GIT_TREE,
67
- EMPTY_GIT_TREES,
68
69
  FINDING_ABSOLUTE_PATH_BYTES,
69
70
  FINDING_LEVEL_BYTES,
71
+ FINDING_LOCATION_ORDER_SQL,
70
72
  FINDING_LOCATION_PATH_BYTES,
71
73
  FINDING_LOCATION_ROLE_BYTES,
72
74
  FINDING_LOCATIONS_LIMIT,
73
75
  FINDING_REMEDIATION_BYTES,
74
76
  FINDING_SUMMARY_BYTES,
75
77
  FINDING_TITLE_BYTES,
76
- FINDINGS_PAGE_MAX,
77
78
  FINDINGS_RESULT_LIMIT,
78
79
  PATCH_PREVIEW_BYTES,
79
80
  SQLITE_RETRY_ATTEMPTS,
@@ -81,10 +82,10 @@ from workbench_constants import (
81
82
  from workbench_feedback import get_scan_feedback
82
83
  from workbench_finding_index import index_findings
83
84
  from workbench_finding_workflows import finding_workflow, register_workflow_scan
84
- from workbench_local_dedupe import local_dedupe
85
85
  from workbench_scan_start import (
86
86
  archive_scan,
87
87
  insert_running_scan,
88
+ loaded_plugin_version,
88
89
  safe_segment,
89
90
  scan_diff_identity,
90
91
  scan_target_identity,
@@ -96,6 +97,17 @@ from workbench_schema import (
96
97
  from workbench_schema import (
97
98
  apply_migrations as apply_schema_migrations,
98
99
  )
100
+ from workbench_setup import (
101
+ inspect_command,
102
+ inspect_selected_setup,
103
+ inspect_setup_values,
104
+ require_diff_target,
105
+ require_review_changes_target,
106
+ require_scannable_target,
107
+ require_scope,
108
+ require_target,
109
+ resolve_git_commit,
110
+ )
99
111
  from workbench_source_excerpt import finding_source_excerpt
100
112
  from workbench_source_scopes import (
101
113
  capture_source_scopes,
@@ -108,10 +120,10 @@ from workbench_target import (
108
120
  clean_worktree_content_digest,
109
121
  copy_directory_excluding,
110
122
  copy_git_worktree_files,
123
+ diff_scope_file_count,
111
124
  diff_snapshot_digest,
112
125
  directory_content_digest,
113
126
  directory_snapshot_regular_file_count,
114
- git_bytes,
115
127
  git_command,
116
128
  git_output,
117
129
  git_revision,
@@ -119,7 +131,6 @@ from workbench_target import (
119
131
  git_target_metadata,
120
132
  git_worktree_context,
121
133
  remediation_checkout_snapshot,
122
- require_git_worktree_head,
123
134
  require_remediation_target,
124
135
  require_scan_target_identity,
125
136
  require_scan_target_unchanged,
@@ -134,6 +145,7 @@ from workbench_validation import (
134
145
  optional_text,
135
146
  parse_scan_cost,
136
147
  path_within_scope,
148
+ read_json_object,
137
149
  reject_non_finite_json,
138
150
  require_close_note,
139
151
  require_occurrence,
@@ -163,6 +175,7 @@ def database_path() -> Path:
163
175
 
164
176
  @contextmanager
165
177
  def scan_completion_lock(scan_id: str) -> Any:
178
+ """Hold the cross-process lock for a scan ID until the context exits."""
166
179
  lock_dir = state_dir() / "completion-locks"
167
180
  create_private_directory(lock_dir)
168
181
  lock_path = lock_dir / f"{require_uuid(scan_id, 'scan-id')}.lock"
@@ -226,6 +239,20 @@ def release_completion_file_lock(descriptor: int) -> None:
226
239
 
227
240
 
228
241
  def connect(*, deferred: bool = False) -> sqlite3.Connection:
242
+ """Open the workbench SQLite database and bring its schema up to date.
243
+
244
+ Args:
245
+ deferred (bool): Let the first migration attempt defer its write lock.
246
+ Retries after lock contention use an immediate transaction.
247
+
248
+ Returns:
249
+ sqlite3.Connection: A caller-owned connection with named rows, foreign
250
+ keys enabled, and write-ahead logging.
251
+
252
+ Raises:
253
+ sqlite3.OperationalError: Opening or configuring the database fails, or
254
+ lock contention persists after retries.
255
+ """
229
256
  path = database_path()
230
257
  guidance = (
231
258
  f"Workbench database: {path}. Ensure the state directory and SQLite journal files "
@@ -267,168 +294,6 @@ def apply_migrations(connection: sqlite3.Connection, *, immediate: bool = False)
267
294
  )
268
295
 
269
296
 
270
- def require_target(value: str) -> Path:
271
- expanded = Path(value).expanduser()
272
- if not expanded.is_absolute():
273
- raise SystemExit("Scan target must be an absolute local directory path.")
274
- target = expanded.resolve()
275
- if not target.is_dir():
276
- raise SystemExit(f"Scan target is not a readable local directory: {target}")
277
- return target
278
-
279
-
280
- def inspect_target(target_path: str) -> dict[str, Any]:
281
- target = require_target(target_path)
282
- return {
283
- "displayName": target.name,
284
- "targetMetadata": git_target_metadata(target),
285
- "targetPath": str(target),
286
- }
287
-
288
-
289
- def resolve_git_commit(target: Path, revision: str, label: str) -> str:
290
- value = optional_text(revision, maximum=512)
291
- if not value:
292
- raise SystemExit(f"{label} is required.")
293
- resolved = git_output(
294
- target,
295
- "rev-parse",
296
- "--verify",
297
- "--end-of-options",
298
- f"{value}^{{commit}}",
299
- )
300
- if resolved is None:
301
- raise SystemExit(f"{label} does not resolve to a local Git commit: {value}")
302
- return resolved
303
-
304
-
305
- def require_diff_target(
306
- target: Path,
307
- kind: str | None,
308
- base_revision: str | None,
309
- head_revision: str | None,
310
- content_digest: str | None,
311
- ) -> dict[str, str]:
312
- current_head = require_review_changes_target(target)
313
- if kind not in DIFF_TARGET_KINDS:
314
- raise SystemExit("Choose which Git changes to review before starting a diff scan.")
315
- if kind == "working_tree":
316
- base = resolve_git_commit(target, base_revision or "HEAD", "Working-tree base")
317
- head = resolve_git_commit(target, head_revision or current_head, "Working-tree HEAD")
318
- current_digest = worktree_content_digest(target)
319
- if base != current_head or head != current_head:
320
- raise SystemExit(
321
- "Repository HEAD changed after these working-tree changes were selected. "
322
- "Select Uncommitted changes again."
323
- )
324
- if content_digest and content_digest != current_digest:
325
- raise SystemExit(
326
- "Working-tree contents changed after they were selected. "
327
- "Select Uncommitted changes again."
328
- )
329
- return {
330
- "kind": kind,
331
- "baseRevision": current_head,
332
- "headRevision": current_head,
333
- "contentDigest": current_digest,
334
- }
335
- if kind == "commit":
336
- head = resolve_git_commit(target, head_revision or "", "Commit")
337
- commit = git_bytes(target, "cat-file", "-p", head)
338
- if commit is None:
339
- raise SystemExit(f"Commit is not available in the local checkout: {head}")
340
- parent_line = next(
341
- (
342
- line
343
- for line in commit.split(b"\n\n", 1)[0].splitlines()
344
- if line.startswith(b"parent ")
345
- ),
346
- None,
347
- )
348
- if parent_line is None:
349
- parent = EMPTY_GIT_TREES[git_output(target, "rev-parse", "--show-object-format")]
350
- else:
351
- parent = resolve_git_commit(
352
- target,
353
- parent_line.removeprefix(b"parent ").decode("ascii").strip(),
354
- "Commit parent",
355
- )
356
- if base_revision and base_revision != parent:
357
- supplied_base = (
358
- base_revision
359
- if base_revision == EMPTY_GIT_TREE
360
- else resolve_git_commit(target, base_revision, "Commit base")
361
- )
362
- if supplied_base != parent:
363
- raise SystemExit("Commit base revision must match the selected commit's parent.")
364
- return {"kind": kind, "baseRevision": parent, "headRevision": head}
365
- base = resolve_git_commit(target, base_revision or "", "Base revision")
366
- head = resolve_git_commit(target, head_revision or "", "Head revision")
367
- if base == head:
368
- raise SystemExit("Base and head revisions must identify different commits.")
369
- return {"kind": kind, "baseRevision": base, "headRevision": head}
370
-
371
-
372
- def inspect_setup_values(
373
- target_path: str,
374
- scope: str,
375
- mode: str,
376
- diff_target_kind: str | None,
377
- diff_base_revision: str | None,
378
- diff_head_revision: str | None,
379
- diff_content_digest: str | None,
380
- ) -> dict[str, Any]:
381
- target = require_target(target_path)
382
- require_scannable_target(target)
383
- normalized_scope = require_scope(scope, mode, target)
384
- if mode == "diff" and normalized_scope != ".":
385
- raise SystemExit("Review changes requires the whole target; use scope '.'.")
386
- if mode != "diff" and any(
387
- value is not None
388
- for value in (
389
- diff_target_kind,
390
- diff_base_revision,
391
- diff_head_revision,
392
- diff_content_digest,
393
- )
394
- ):
395
- raise SystemExit("A Git diff target requires Review changes mode.")
396
- diff_target = (
397
- require_diff_target(
398
- target,
399
- diff_target_kind,
400
- diff_base_revision,
401
- diff_head_revision,
402
- diff_content_digest,
403
- )
404
- if mode == "diff"
405
- else None
406
- )
407
- return {
408
- "diffTarget": diff_target,
409
- "scope": normalized_scope,
410
- "target": inspect_target(str(target)),
411
- }
412
-
413
-
414
- def require_review_changes_target(target: Path) -> str:
415
- revision = require_git_worktree_head(target)
416
- repository_root = git_output(target, "rev-parse", "--show-toplevel")
417
- if repository_root is None or not Path(repository_root).samefile(target):
418
- raise SystemExit(
419
- "Review changes requires the checked-out Git repository root as the target."
420
- )
421
- return revision
422
-
423
-
424
- def require_scannable_target(target: Path) -> None:
425
- metadata = git_target_metadata(target)
426
- if metadata["isGit"] and not metadata["isWorktree"]:
427
- raise SystemExit(
428
- "Codex Security requires a checked-out worktree, not a bare Git repository."
429
- )
430
-
431
-
432
297
  def requested_scan_file_paths(scan: sqlite3.Row) -> list[str]:
433
298
  # Older scans did not record path kinds. Retain their lexical scope semantics
434
299
  # rather than infer the original target from a possibly changed checkout.
@@ -439,11 +304,13 @@ def requested_scan_file_paths(scan: sqlite3.Row) -> list[str]:
439
304
 
440
305
 
441
306
  def requested_finding_scope(scan: sqlite3.Row) -> list[tuple[str, bool]]:
307
+ """Return requested paths paired with whether the stored recipe identified each one as a file."""
442
308
  files = {PurePosixPath(path) for path in requested_scan_file_paths(scan)}
443
309
  return [(path, PurePosixPath(path) in files) for path in requested_scan_paths(scan)]
444
310
 
445
311
 
446
312
  def scan_contract(scan: sqlite3.Row) -> dict[str, Any]:
313
+ """Return the target identity, scope requirements, and diff target recorded for this scan."""
447
314
  target = Path(scan["target_path"])
448
315
  target_contract = {
449
316
  "allowedKinds": expected_target_kinds(scan),
@@ -476,6 +343,7 @@ def scan_contract(scan: sqlite3.Row) -> dict[str, Any]:
476
343
 
477
344
 
478
345
  def expected_coverage_mode(scan: sqlite3.Row) -> str:
346
+ """Return the coverage mode implied by the recorded scope, scan mode, and diff target kind."""
479
347
  if scan["mode"] == "diff":
480
348
  mode = {
481
349
  "commit": "commit",
@@ -497,14 +365,10 @@ def expected_coverage_mode(scan: sqlite3.Row) -> str:
497
365
  def workbench_completion_binding(
498
366
  scan: sqlite3.Row, completed_at: str, manifest: dict[str, Any] | None = None
499
367
  ) -> dict[str, Any]:
368
+ """Build the scan ID, timestamps, producer, target, and scope used to bind output artifacts at completion."""
500
369
  contract = scan_contract(scan)
501
370
  target_contract = contract["target"]
502
- plugin_manifest = read_json_object(
503
- Path(__file__).resolve().parent.parent / ".codex-plugin" / "plugin.json"
504
- )
505
- plugin_version = plugin_manifest.get("version")
506
- if not isinstance(plugin_version, str) or not plugin_version:
507
- raise SystemExit("plugin.json: expected a nonempty Codex Security plugin version.")
371
+ plugin_version = loaded_plugin_version()
508
372
  target: dict[str, Any] = {
509
373
  "targetId": target_contract["targetId"],
510
374
  "displayName": target_contract["displayName"],
@@ -539,6 +403,7 @@ def workbench_completion_binding(
539
403
 
540
404
 
541
405
  def verify_manifest_binding(scan: sqlite3.Row, manifest: dict[str, Any]) -> None:
406
+ """Raise SystemExit if the supplied manifest does not match the registered scan, target, or scope."""
542
407
  manifest_scan = manifest.get("scan")
543
408
  if not isinstance(manifest_scan, dict):
544
409
  raise SystemExit("scan-manifest.json scan must be an object.")
@@ -609,30 +474,6 @@ def verify_manifest_binding(scan: sqlite3.Row, manifest: dict[str, Any]) -> None
609
474
  raise SystemExit("scan-manifest.json scope must stay inside the workbench scan scope.")
610
475
 
611
476
 
612
- def require_scope(scope: str, mode: str, target: Path) -> str:
613
- value = scope if scope.strip() else "."
614
- requested_scope = Path(value)
615
- if "\\" in value and (os.name != "nt" or not requested_scope.is_absolute()):
616
- raise SystemExit("Scan scope must use repository-relative POSIX paths.")
617
- if ".." in requested_scope.parts:
618
- raise SystemExit("Scan scope must stay inside the scanned target.")
619
- try:
620
- resolved_scope = (
621
- requested_scope if requested_scope.is_absolute() else target / requested_scope
622
- ).resolve()
623
- relative_scope = resolved_scope.relative_to(target)
624
- except (RuntimeError, ValueError) as exc:
625
- raise SystemExit("Scan scope must stay inside the scanned target.") from exc
626
- normalized = relative_scope.as_posix() or "."
627
- if not normalized.strip():
628
- normalized = "./" + normalized
629
- if mode == "deep" and normalized != ".":
630
- raise SystemExit("Deep Scan is repository-wide and cannot use a scoped path.")
631
- if not resolved_scope.is_dir():
632
- raise SystemExit("Scan scope must reference an existing directory inside the target.")
633
- return normalized
634
-
635
-
636
477
  def require_workspace(connection: sqlite3.Connection, workspace_id: str) -> sqlite3.Row:
637
478
  workspace_id = require_uuid(workspace_id, "workspace-id")
638
479
  row = connection.execute("SELECT * FROM workspaces WHERE id = ?", (workspace_id,)).fetchone()
@@ -650,6 +491,10 @@ def require_scan(connection: sqlite3.Connection, scan_id: str) -> sqlite3.Row:
650
491
 
651
492
 
652
493
  def resolve_scan_id(connection: sqlite3.Connection, scan_id: str) -> str:
494
+ """Return a canonical UUID or resolve a unique scan ID prefix of at least eight characters.
495
+
496
+ Raise SystemExit for a prefix that is too short, ambiguous, or absent from the database.
497
+ """
653
498
  try:
654
499
  return str(uuid.UUID(scan_id))
655
500
  except ValueError:
@@ -740,25 +585,6 @@ def create_workspace(connection: sqlite3.Connection, args: argparse.Namespace) -
740
585
  return workspace_state(connection, workspace_id)
741
586
 
742
587
 
743
- def inspect_selected_setup(args: argparse.Namespace) -> dict[str, Any]:
744
- inspected = inspect_setup_values(
745
- args.target_path,
746
- args.scope,
747
- args.mode,
748
- args.diff_target_kind,
749
- args.diff_base_revision,
750
- args.diff_head_revision,
751
- args.diff_content_digest,
752
- )
753
- if args.mode == "deep":
754
- inspected["deepScanConfig"] = resolve_deep_scan_setup(
755
- os.cpu_count() or 1, args.deep_scan_project_json
756
- )
757
- elif args.deep_scan_project_json is not None:
758
- raise SystemExit("Project Deep Scan settings require Deep Scan mode.")
759
- return inspected
760
-
761
-
762
588
  def save_workspace(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
763
589
  workspace = require_workspace(connection, args.workspace_id)
764
590
  if workspace["active_scan_id"]:
@@ -814,12 +640,37 @@ def save_workspace(connection: sqlite3.Connection, args: argparse.Namespace) ->
814
640
 
815
641
  def scan_target_root(scan_root: str | None, target: Path) -> Path:
816
642
  root = resolve_scan_root(scan_root)
817
- target_root = (root / safe_segment(target.name)).resolve()
643
+ target_root = root / safe_segment(target.name)
644
+ if os.name != "nt":
645
+ target_root = target_root.resolve()
818
646
  if target_root == target or target in target_root.parents:
819
647
  raise SystemExit("The scan artifact directory must be outside the selected target.")
820
648
  return target_root
821
649
 
822
650
 
651
+ def scan_scope_file_count(
652
+ target: Path, paths: list[str], diff_target: dict[str, str] | None
653
+ ) -> int | None:
654
+ if diff_target is not None:
655
+ return diff_scope_file_count(target, diff_target)
656
+ return sum(
657
+ 1 if (target / path).is_file() else directory_snapshot_regular_file_count(target / path)
658
+ for path in paths
659
+ )
660
+
661
+
662
+ def require_working_tree_unchanged(target: Path, diff_target: dict[str, str] | None) -> None:
663
+ if (
664
+ diff_target is not None
665
+ and diff_target["kind"] == "working_tree"
666
+ and (
667
+ git_revision(target) != diff_target["headRevision"]
668
+ or worktree_content_digest(target) != diff_target["contentDigest"]
669
+ )
670
+ ):
671
+ raise SystemExit("The selected scan target changed while the scan was starting. Try again.")
672
+
673
+
823
674
  def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
824
675
  workspace_id = require_uuid(args.workspace_id, "workspace-id")
825
676
  manages_transaction = not connection.in_transaction
@@ -836,7 +687,7 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
836
687
  (workspace["id"],),
837
688
  ).fetchone()
838
689
  if active is not None:
839
- return workspace_state(connection, workspace["id"])
690
+ return workspace_state(connection, workspace["id"], result_scan_id=active["id"])
840
691
  workspace_version = workspace["updated_at"]
841
692
  scan_id = str(uuid.uuid4())
842
693
  timestamp = now()
@@ -858,9 +709,7 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
858
709
  )
859
710
  if diff_target is not None and not target_summary:
860
711
  target_summary = diff_target_summary(diff_target)
861
- scope_file_count = directory_snapshot_regular_file_count(
862
- target if scope == "." else target / scope
863
- )
712
+ scope_file_count = scan_scope_file_count(target, [scope], diff_target)
864
713
  target_identity = scan_target_identity(
865
714
  target,
866
715
  diff_target,
@@ -873,7 +722,8 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
873
722
  diff_target_kind=diff_target["kind"] if diff_target is not None else None,
874
723
  )
875
724
  target_root = scan_target_root(args.scan_root, target)
876
- create_private_directory(target_root)
725
+ create_private_directory(target_root, scan_output=True)
726
+ require_working_tree_unchanged(target, diff_target)
877
727
  if manages_transaction:
878
728
  connection.execute("BEGIN IMMEDIATE")
879
729
  workspace = require_workspace(connection, workspace_id)
@@ -888,7 +738,7 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
888
738
  if active is not None:
889
739
  if manages_transaction:
890
740
  connection.commit()
891
- return workspace_state(connection, workspace["id"])
741
+ return workspace_state(connection, workspace["id"], result_scan_id=active["id"])
892
742
  if workspace["updated_at"] != workspace_version:
893
743
  raise SystemExit("Codex Security setup changed while the scan was starting. Try again.")
894
744
  current_target = require_remediation_target(str(target))
@@ -934,7 +784,7 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
934
784
  if manages_transaction:
935
785
  connection.rollback()
936
786
  raise
937
- return workspace_state(connection, workspace["id"])
787
+ return workspace_state(connection, workspace["id"], result_scan_id=scan_id)
938
788
 
939
789
 
940
790
  def _start_prompt_driven_scan(
@@ -960,9 +810,7 @@ def _start_prompt_driven_scan(
960
810
  target_summary = optional_text(args.target_summary, maximum=2400)
961
811
  if diff_target is not None and not target_summary:
962
812
  target_summary = diff_target_summary(diff_target)
963
- scope_file_count = directory_snapshot_regular_file_count(
964
- target if scope == "." else target / scope
965
- )
813
+ scope_file_count = scan_scope_file_count(target, [scope], diff_target)
966
814
  diff_identity = scan_diff_identity(diff_target)
967
815
  target_identity = scan_target_identity(target, diff_target)
968
816
  source_scopes = capture_source_scopes(
@@ -1008,7 +856,8 @@ def _start_prompt_driven_scan(
1008
856
  """
1009
857
  SELECT scans.* FROM scans
1010
858
  JOIN workspaces ON workspaces.active_scan_id = scans.id
1011
- WHERE workspaces.thread_id = ? AND workspaces.target_path = ?
859
+ WHERE COALESCE(scans.continuation_thread_id, workspaces.thread_id) = ?
860
+ AND scans.recipe_json IS NULL AND workspaces.target_path = ?
1012
861
  AND workspaces.default_scope = ? AND workspaces.default_mode = ?
1013
862
  AND workspaces.user_context IS ? AND workspaces.target_summary IS ?
1014
863
  AND workspaces.diff_target_kind IS ? AND workspaces.diff_base_revision IS ?
@@ -1021,7 +870,7 @@ def _start_prompt_driven_scan(
1021
870
  (? = 0 AND scans.handoff_claim_token IS NULL)
1022
871
  OR (
1023
872
  ? = 1 AND scans.handoff_claim_token IS NOT NULL
1024
- AND scans.continuation_thread_id = ?
873
+ AND scans.continuation_thread_id = workspaces.thread_id
1025
874
  )
1026
875
  )
1027
876
  ORDER BY scans.updated_at DESC, scans.started_at DESC, scans.id LIMIT 1
@@ -1037,16 +886,15 @@ def _start_prompt_driven_scan(
1037
886
  *target_identity,
1038
887
  int(headless_standard),
1039
888
  int(headless_standard),
1040
- thread_id,
1041
889
  ),
1042
890
  ).fetchone()
1043
891
  if existing is not None:
1044
892
  connection.commit()
1045
893
  return {
1046
- **scan_context(connection, existing["id"]),
894
+ **scan_context(connection, existing["id"], model_thread_id=thread_id),
1047
895
  "startDisposition": "joined",
1048
896
  }
1049
- create_private_directory(target_root)
897
+ create_private_directory(target_root, scan_output=True)
1050
898
  workspace_id = str(uuid.uuid4())
1051
899
  scan_id = str(uuid.uuid4())
1052
900
  timestamp = now()
@@ -1120,6 +968,7 @@ def _start_prompt_driven_scan(
1120
968
 
1121
969
 
1122
970
  def scan_local_file_digest(scan_dir: Path, relative_path: str) -> str:
971
+ """Return the SHA-256 of the scan-local file contents, prefixed with sha256:."""
1123
972
  digest = hashlib.sha256()
1124
973
  with open_scan_local_file(scan_dir, relative_path) as source:
1125
974
  for chunk in iter(lambda: source.read(1024 * 1024), b""):
@@ -1128,6 +977,7 @@ def scan_local_file_digest(scan_dir: Path, relative_path: str) -> str:
1128
977
 
1129
978
 
1130
979
  def published_manifest_digest(scan_dir: Path, manifest: dict[str, Any]) -> str:
980
+ """Return the canonical manifest digest or raise SystemExit if the published bytes differ."""
1131
981
  canonical = (json.dumps(manifest, allow_nan=False, indent=2, sort_keys=True) + "\n").encode()
1132
982
  expected = f"sha256:{hashlib.sha256(canonical).hexdigest()}"
1133
983
  actual = scan_local_file_digest(scan_dir, ARTIFACTS["manifest"])
@@ -1137,6 +987,7 @@ def published_manifest_digest(scan_dir: Path, manifest: dict[str, Any]) -> str:
1137
987
 
1138
988
 
1139
989
  def require_recorded_manifest_digest(scan: sqlite3.Row, scan_dir: Path) -> None:
990
+ """Raise SystemExit if the saved manifest differs from the digest recorded when the scan was sealed."""
1140
991
  expected = scan["seal_manifest_digest"]
1141
992
  if expected is None:
1142
993
  return
@@ -1147,6 +998,7 @@ def require_recorded_manifest_digest(scan: sqlite3.Row, scan_dir: Path) -> None:
1147
998
  def pin_legacy_manifest_digest(
1148
999
  connection: sqlite3.Connection, scan_id: str, manifest_digest: str
1149
1000
  ) -> None:
1001
+ """Record a previously missing digest in a write transaction, rejecting a conflict with an existing digest."""
1150
1002
  connection.execute("BEGIN IMMEDIATE")
1151
1003
  with connection:
1152
1004
  scan = require_scan(connection, scan_id)
@@ -1177,9 +1029,24 @@ def complete_scan(
1177
1029
  prepare_only=prepare_only,
1178
1030
  thread_id=getattr(args, "thread_id", None),
1179
1031
  turn_id=getattr(args, "turn_id", None),
1032
+ model_thread_id=getattr(args, "model_thread_id", None),
1180
1033
  )
1181
1034
 
1182
1035
 
1036
+ def read_scan_usage(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1037
+ # Validation can finish after the scan's recorded completion timestamp.
1038
+ usage = scan_usage.collect_scan_usage(
1039
+ connection,
1040
+ require_scan(connection, args.scan_id),
1041
+ thread_id=args.thread_id,
1042
+ validation_thread_id=args.validation_thread_id,
1043
+ completed_at=now(),
1044
+ )
1045
+ if "cacheWriteInputTokens" in usage:
1046
+ usage.setdefault("cacheWriteInputTokensReported", True)
1047
+ return usage
1048
+
1049
+
1183
1050
  def finalize_scan_usage(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1184
1051
  scan_id = require_uuid(args.scan_id, "scan-id")
1185
1052
  with scan_completion_lock(scan_id):
@@ -1192,7 +1059,7 @@ def finalize_scan_usage(connection: sqlite3.Connection, args: argparse.Namespace
1192
1059
  if completion["threadId"] != args.thread_id or completion["turnId"] != args.turn_id:
1193
1060
  raise SystemExit("The scan usage belongs to a different completion turn.")
1194
1061
  if not completion["finalized"]:
1195
- usage = scan_usage.collect_scan_usage(
1062
+ usage = scan_usage.collect_scan_usage_safely(
1196
1063
  connection,
1197
1064
  scan,
1198
1065
  thread_id=args.thread_id,
@@ -1211,7 +1078,9 @@ def finalize_scan_usage(connection: sqlite3.Connection, args: argparse.Namespace
1211
1078
  f"scan_turn_{args.turn_status}",
1212
1079
  ]
1213
1080
  usage["completion"] = {**completion, "finalized": True}
1214
- cost_json = parse_scan_cost(json.dumps({**stored, "usage": usage}))
1081
+ fields = scan_usage.merge_scan_usage_fields(stored, {"usage": usage})
1082
+ fields["usage"]["completion"] = usage["completion"]
1083
+ cost_json = parse_scan_cost(json.dumps(fields))
1215
1084
  with connection:
1216
1085
  connection.execute(
1217
1086
  "UPDATE scans SET cost_json = ? WHERE id = ?", (cost_json, scan_id)
@@ -1222,6 +1091,7 @@ def finalize_scan_usage(connection: sqlite3.Connection, args: argparse.Namespace
1222
1091
  def complete_budget_exhausted_scan(
1223
1092
  connection: sqlite3.Connection, args: argparse.Namespace
1224
1093
  ) -> dict[str, Any]:
1094
+ """Complete a running CLI Deep Scan using the measured cost and saved results after its budget is exhausted."""
1225
1095
  scan_id = require_uuid(args.scan_id, "scan-id")
1226
1096
  cost_json = parse_scan_cost(args.cost_json)
1227
1097
  if cost_json is None:
@@ -1367,19 +1237,21 @@ def complete_scan_locked(
1367
1237
  prepare_only: bool = False,
1368
1238
  thread_id: str | None = None,
1369
1239
  turn_id: str | None = None,
1240
+ model_thread_id: str | None = None,
1370
1241
  ) -> dict[str, Any]:
1371
1242
  scan = require_scan(connection, scan_id)
1372
- if scan["status"] == "complete":
1373
- return saved_results.refresh_completed_scan(
1374
- _WORKBENCH_DB_CONTEXT, connection, scan, cost_json
1375
- )
1376
- if scan["status"] != "running":
1243
+ handoff.require_tokenless_scan_owner(connection, scan, model_thread_id)
1244
+ if scan["status"] not in {"running", "complete"}:
1377
1245
  raise SystemExit("Only a running scan can be completed.")
1378
1246
  handoff.require_current_continuation(
1379
1247
  scan,
1380
1248
  claim_token,
1381
1249
  error_message="Scan completion is owned by another continuation.",
1382
1250
  )
1251
+ if scan["status"] == "complete":
1252
+ return saved_results.refresh_completed_scan(
1253
+ _WORKBENCH_DB_CONTEXT, connection, scan, cost_json
1254
+ )
1383
1255
  deep_scan.require_deep_scan_ready_for_parent_completion(connection, scan)
1384
1256
  warnings = json.loads(scan["completion_warnings_json"])
1385
1257
  target_warnings: list[str] = []
@@ -1436,10 +1308,7 @@ def complete_scan_locked(
1436
1308
  scan_dir,
1437
1309
  scan["id"],
1438
1310
  completion_binding,
1439
- connection.execute(
1440
- "SELECT * FROM deep_scan_workers WHERE scan_id = ? ORDER BY created_at, id",
1441
- (scan["id"],),
1442
- ).fetchall(),
1311
+ saved_deep_workers(connection, scan),
1443
1312
  warnings,
1444
1313
  stopped=False,
1445
1314
  scan_mode=scan["mode"],
@@ -1468,14 +1337,24 @@ def complete_scan_locked(
1468
1337
  )
1469
1338
  warnings.extend(warning for warning in prepared.warnings if warning not in warnings)
1470
1339
  add_warning()
1471
- if warnings != json.loads(scan["completion_warnings_json"]):
1472
- # Persist all observed warnings before output writes can fail, so
1473
- # stopped-result publication and retries retain those diagnostics.
1474
- with connection:
1340
+ with connection:
1341
+ if warnings != json.loads(scan["completion_warnings_json"]):
1342
+ # Retain pre-publication diagnostics even if output writes fail.
1475
1343
  connection.execute(
1476
1344
  "UPDATE scans SET completion_warnings_json = ? WHERE id = ? AND status = 'running'",
1477
1345
  (json.dumps(warnings), scan_id),
1478
1346
  )
1347
+ prepared_scan = prepared.manifest["scan"]
1348
+ if scan["mode"] == "deep" and (
1349
+ not prepared.was_sealed
1350
+ or "coverageRef" in prepared_scan
1351
+ or "surfaces" in prepared_scan.get("reviewState", {})
1352
+ ):
1353
+ # A summary-only seal omits surfaces. Keep the count from its first preparation.
1354
+ connection.execute(
1355
+ "UPDATE deep_scan_runs SET terminal_reviewed_surface_count = ? WHERE scan_id = ?",
1356
+ (len(prepared.coverage["surfaces"]), scan_id),
1357
+ )
1479
1358
  wrote = True
1480
1359
  manifest, findings, _ = _write_prepared_scan_finalization(
1481
1360
  prepared, projection_warnings=warnings
@@ -1513,42 +1392,44 @@ def complete_scan_locked(
1513
1392
  context["targetWarnings"] = target_warnings
1514
1393
  return context
1515
1394
 
1516
- cost_fields = scan_usage.stored_scan_cost_fields(cost_json)
1517
- if "usage" not in cost_fields:
1518
- cost_fields["usage"] = scan_usage.collect_scan_usage(
1519
- connection,
1520
- scan,
1521
- thread_id=thread_id,
1522
- completed_at=completion_timestamp,
1395
+ measured_usage = scan_usage.collect_scan_usage_safely(
1396
+ connection,
1397
+ scan,
1398
+ thread_id=thread_id,
1399
+ completed_at=completion_timestamp,
1400
+ )
1401
+ cost_fields = scan_usage.merge_scan_usage_fields(
1402
+ scan_usage.stored_scan_cost_fields(cost_json), {"usage": measured_usage}
1403
+ )
1404
+ measured_usage = cost_fields["usage"]
1405
+ if thread_id is not None and turn_id is not None:
1406
+ measured_usage["completion"] = {
1407
+ "threadId": thread_id,
1408
+ "turnId": turn_id,
1409
+ "finalized": False,
1410
+ }
1411
+ if measured_usage["coverage"] == "complete":
1412
+ measured_usage["coverage"] = "partial"
1413
+ measured_usage["warnings"] = sorted(
1414
+ {*measured_usage.get("warnings", []), "turn_in_progress"}
1523
1415
  )
1524
- measured_usage = cost_fields["usage"]
1525
- if thread_id is not None and turn_id is not None:
1526
- measured_usage["completion"] = {
1527
- "threadId": thread_id,
1528
- "turnId": turn_id,
1529
- "finalized": False,
1530
- }
1531
- if measured_usage["coverage"] == "complete":
1532
- measured_usage["coverage"] = "partial"
1533
- measured_usage["warnings"] = sorted(
1534
- {*measured_usage.get("warnings", []), "turn_in_progress"}
1535
- )
1536
1416
  cost_json = parse_scan_cost(json.dumps(cost_fields))
1537
1417
  connection.execute("BEGIN IMMEDIATE")
1538
1418
  with connection:
1539
1419
  timestamp = manifest["scan"]["completedAt"]
1540
1420
  scan = require_scan(connection, scan["id"])
1541
- if scan["status"] == "complete":
1542
- connection.commit()
1543
- return scan_context(connection, scan["id"])
1544
- if scan["status"] != "running":
1421
+ handoff.require_tokenless_scan_owner(connection, scan, model_thread_id)
1422
+ if scan["status"] not in {"running", "complete"}:
1545
1423
  raise SystemExit("Only a running scan can be completed.")
1546
- deep_scan.require_deep_scan_ready_for_parent_completion(connection, scan)
1547
1424
  handoff.require_current_continuation(
1548
1425
  scan,
1549
1426
  claim_token,
1550
1427
  error_message="Scan completion is owned by another continuation.",
1551
1428
  )
1429
+ if scan["status"] == "complete":
1430
+ connection.commit()
1431
+ return scan_context(connection, scan["id"])
1432
+ deep_scan.require_deep_scan_ready_for_parent_completion(connection, scan)
1552
1433
  connection.execute("DELETE FROM scan_artifacts WHERE scan_id = ?", (scan["id"],))
1553
1434
  for kind, path in artifacts.items():
1554
1435
  if path is not None:
@@ -1573,12 +1454,14 @@ def complete_scan_locked(
1573
1454
  """
1574
1455
  UPDATE scans
1575
1456
  SET status = 'complete', phase = 'reporting', completed_at = ?, updated_at = ?,
1576
- seal_manifest_digest = ?, cost_json = ?, completion_warnings_json = ?
1457
+ log_completed_at = ?, seal_manifest_digest = ?, cost_json = ?,
1458
+ completion_warnings_json = ?
1577
1459
  WHERE id = ? AND status = 'running'
1578
1460
  """,
1579
1461
  (
1580
1462
  timestamp,
1581
1463
  timestamp,
1464
+ now(),
1582
1465
  manifest_digest,
1583
1466
  cost_json,
1584
1467
  json.dumps(warnings),
@@ -1593,7 +1476,11 @@ def complete_scan_locked(
1593
1476
 
1594
1477
 
1595
1478
  def require_cli_scan_directory(repository: Path, args: argparse.Namespace) -> Path:
1596
- scan_dir = require_canonical_scan_directory(Path(args.scan_dir).expanduser())
1479
+ # Registration uses an empty output, including the replacement created with
1480
+ # the original DACL after archival. Check that ACL before moving any results.
1481
+ scan_dir = require_canonical_scan_directory(
1482
+ Path(args.scan_dir).expanduser(), writable_output=True
1483
+ )
1597
1484
  if scan_dir == repository or repository in scan_dir.parents or scan_dir in repository.parents:
1598
1485
  raise SystemExit(
1599
1486
  "The scan artifact directory must be outside and not contain the selected target."
@@ -1611,6 +1498,12 @@ def register_cli_scan(
1611
1498
  *,
1612
1499
  before_archive: Callable[[], None] | None = None,
1613
1500
  ) -> dict[str, Any]:
1501
+ """Register a CLI scan and its selected target before execution begins.
1502
+
1503
+ Record the launch recipe and target identity with the new scan. When reusing
1504
+ an output directory, coordinate archiving with registration so previous results
1505
+ can still be located. Return the scan ID, contract, and target metadata.
1506
+ """
1614
1507
  repository = require_target(args.repository)
1615
1508
  require_scannable_target(repository)
1616
1509
  scan_dir = None
@@ -1619,14 +1512,25 @@ def register_cli_scan(
1619
1512
  scan_dir = require_cli_scan_directory(repository, args)
1620
1513
  user_context = None
1621
1514
  workflow_id = None
1515
+ surface = None
1516
+ package_version = None
1622
1517
  if args.registration_json_stdin:
1623
1518
  registration = json.load(sys.stdin)
1624
1519
  recipe_json = json.dumps(registration["recipe"], ensure_ascii=False, separators=(",", ":"))
1625
1520
  user_context = registration.get("userContext")
1626
1521
  workflow_id = registration.get("workflowId")
1522
+ if registration.get("surface") in ("cli", "sdk"):
1523
+ surface = registration["surface"]
1524
+ if isinstance(registration.get("packageVersion"), str):
1525
+ package_version = registration["packageVersion"] or None
1627
1526
  else:
1628
1527
  recipe_json = sys.stdin.read() if args.recipe_json_stdin else args.recipe_json
1629
1528
  recipe = parse_scan_recipe(recipe_json, repository)
1529
+ launch_kind = "scan"
1530
+ if isinstance(recipe.get("import"), dict):
1531
+ launch_kind = "import"
1532
+ elif recipe.get("mock") is True:
1533
+ launch_kind = "mock"
1630
1534
  requested_target = recipe["target"]
1631
1535
  paths = requested_target["paths"]
1632
1536
  recipe.pop("_codexSecurityFileScopes", None)
@@ -1657,16 +1561,8 @@ def register_cli_scan(
1657
1561
  paths or ["."],
1658
1562
  diff_target_kind=diff_target["kind"] if diff_target is not None else None,
1659
1563
  )
1660
- scope_file_count = (
1661
- directory_snapshot_regular_file_count(repository)
1662
- if not paths
1663
- else sum(
1664
- 1
1665
- if (repository / path).is_file()
1666
- else directory_snapshot_regular_file_count(repository / path)
1667
- for path in paths
1668
- )
1669
- )
1564
+ scope_file_count = scan_scope_file_count(repository, paths or ["."], diff_target)
1565
+ require_working_tree_unchanged(repository, diff_target)
1670
1566
  parent_scan_id = (
1671
1567
  require_uuid(args.parent_scan_id, "parent-scan-id")
1672
1568
  if args.parent_scan_id is not None
@@ -1678,18 +1574,43 @@ def register_cli_scan(
1678
1574
 
1679
1575
  connection.execute("BEGIN IMMEDIATE")
1680
1576
  try:
1681
- if scan_dir is None:
1682
- # Host archiving temporarily removes the output directory. Wait for
1683
- # the existing writer before inspecting its replacement.
1684
- scan_dir = require_cli_scan_directory(repository, args)
1685
- with archive_scan(
1686
- connection,
1687
- args,
1688
- scan_dir,
1689
- timestamp,
1690
- require_canonical_scan_directory,
1691
- before_archive=before_archive,
1692
- ) as archived_scan_dir:
1577
+ with ExitStack() as archive:
1578
+ deadline = None
1579
+ while True:
1580
+ if scan_dir is None:
1581
+ # Wait for any previous archive to finish before inspecting its replacement.
1582
+ scan_dir = require_cli_scan_directory(repository, args)
1583
+ try:
1584
+ archived_scan_dir = archive.enter_context(
1585
+ archive_scan(
1586
+ connection,
1587
+ args,
1588
+ scan_dir,
1589
+ timestamp,
1590
+ require_canonical_scan_directory,
1591
+ before_archive=before_archive,
1592
+ )
1593
+ )
1594
+ break
1595
+ except PermissionError as exc:
1596
+ # A Windows patch writer can temporarily pin the source of this rename.
1597
+ if (
1598
+ getattr(exc, "winerror", None) not in {5, 32}
1599
+ or exc.filename != str(scan_dir)
1600
+ or not exc.filename2
1601
+ ):
1602
+ raise
1603
+ if deadline is None:
1604
+ timeout = connection.execute("PRAGMA busy_timeout").fetchone()[0]
1605
+ deadline = time.monotonic() + timeout / 1000
1606
+ remaining = deadline - time.monotonic()
1607
+ if remaining <= 0:
1608
+ raise
1609
+ connection.rollback()
1610
+ before_archive = None # The host already acknowledged this registration.
1611
+ scan_dir = None # Revalidate the directory and its owner under the next lock.
1612
+ time.sleep(min(0.05, remaining))
1613
+ connection.execute("BEGIN IMMEDIATE")
1693
1614
  target_id = ensure_security_target(connection, str(repository))
1694
1615
  if parent_scan_id is not None:
1695
1616
  parent = require_scan(connection, parent_scan_id)
@@ -1734,6 +1655,9 @@ def register_cli_scan(
1734
1655
  timestamp=timestamp,
1735
1656
  handoff_status="delivered",
1736
1657
  scan_dir=scan_dir,
1658
+ surface=surface,
1659
+ package_version=package_version,
1660
+ launch_kind=launch_kind,
1737
1661
  )
1738
1662
  connection.execute(
1739
1663
  "UPDATE scans SET recipe_json = ?, parent_scan_id = ?, user_context = ? WHERE id = ?",
@@ -1746,14 +1670,19 @@ def register_cli_scan(
1746
1670
  )
1747
1671
  if workflow_id is not None:
1748
1672
  register_workflow_scan(connection, workflow_id, scan_id, str(scan_dir), timestamp)
1673
+ scan = connection.execute(
1674
+ "SELECT scans.*, snapshot AS lifecycle_json FROM scans "
1675
+ "JOIN scan_lifecycles ON scan_id = scans.id WHERE scans.id = ?",
1676
+ (scan_id,),
1677
+ ).fetchone()
1749
1678
  connection.commit()
1750
1679
  except BaseException:
1751
1680
  connection.rollback()
1752
1681
  raise
1753
- scan = require_scan(connection, scan_id)
1754
1682
  return {
1755
1683
  **({"archivedScanDir": str(archived_scan_dir)} if archived_scan_dir is not None else {}),
1756
1684
  "contract": scan_contract(scan),
1685
+ "lifecycle": json.loads(scan["lifecycle_json"]),
1757
1686
  "scanDir": str(scan_dir),
1758
1687
  "scanId": scan_id,
1759
1688
  "scopeFileCount": scope_file_count,
@@ -1875,16 +1804,7 @@ def set_finding_triage(connection: sqlite3.Connection, args: argparse.Namespace)
1875
1804
  timestamp = now()
1876
1805
  occurrence = require_occurrence(connection, args.occurrence_id)
1877
1806
  if args.status == "closed":
1878
- remediation = connection.execute(
1879
- """
1880
- SELECT *
1881
- FROM finding_remediation_attempts
1882
- WHERE occurrence_id = ?
1883
- ORDER BY created_at DESC, rowid DESC
1884
- LIMIT 1
1885
- """,
1886
- (occurrence["id"],),
1887
- ).fetchone()
1807
+ remediation = remediation_state.latest_attempt(connection, occurrence["id"])
1888
1808
  if (
1889
1809
  remediation is not None
1890
1810
  and remediation["pending_action"] is not None
@@ -1986,16 +1906,7 @@ def request_finding_remediation(
1986
1906
  raise SystemExit("This remediation request belongs to a different finding.")
1987
1907
  connection.commit()
1988
1908
  return scan_context(connection, occurrence["scan_id"])
1989
- latest = connection.execute(
1990
- """
1991
- SELECT *
1992
- FROM finding_remediation_attempts
1993
- WHERE occurrence_id = ?
1994
- ORDER BY created_at DESC, rowid DESC
1995
- LIMIT 1
1996
- """,
1997
- (occurrence["id"],),
1998
- ).fetchone()
1909
+ latest = remediation_state.latest_attempt(connection, occurrence["id"])
1999
1910
  if latest is not None:
2000
1911
  active_operation = latest["pending_action"] is not None or latest["state"] in {
2001
1912
  "requested",
@@ -2259,27 +2170,22 @@ def set_finding_remediation(
2259
2170
  raise SystemExit("This remediation host request is owned by a different action token.")
2260
2171
  remediation_state.require_transition(current["state"], args.state)
2261
2172
  remediation_state.require_pending_action(current, args.state)
2262
- patch_path = current["patch_path"]
2263
- if args.patch_path is not None:
2264
- requested_patch_path = require_scan_relative_file(scan, args.patch_path)
2265
- if patch_path is not None and requested_patch_path != patch_path:
2266
- raise SystemExit("A remediation attempt cannot replace its reviewed patch path.")
2267
- patch_path = requested_patch_path
2268
- patch_digest = current["patch_digest"]
2269
- if args.patch_digest is not None:
2270
- requested_patch_digest = require_sha256_digest(args.patch_digest, "patch-digest")
2271
- if patch_digest is not None and requested_patch_digest != patch_digest:
2272
- raise SystemExit("A remediation attempt cannot replace its reviewed patch digest.")
2273
- patch_digest = requested_patch_digest
2173
+ patch = sys.stdin.buffer.read() if args.patch_stdin else None
2174
+ patch_path, patch_digest = remediation_state.patch_metadata(
2175
+ current, args, patch, partial(require_scan_relative_file, scan)
2176
+ )
2274
2177
  base_revision = optional_text(args.base_revision, maximum=512)
2275
2178
  if args.state in {"generated", "applied", "verifying", "verified"}:
2276
2179
  if patch_path is None or patch_digest is None:
2277
2180
  raise SystemExit(
2278
2181
  "Generated remediation states require a scan-local patch path and digest."
2279
2182
  )
2280
- require_matching_patch_digest(scan, patch_path, patch_digest)
2183
+ if patch is None:
2184
+ require_matching_patch_digest(scan, patch_path, patch_digest)
2281
2185
  if args.state == "generated":
2282
2186
  require_remediation_checkout_unchanged(scan, current, require_base_content=True)
2187
+ if patch is not None:
2188
+ remediation_state.require_valid_patch(Path(scan["target_path"]), patch)
2283
2189
  if args.state in {"applied", "verifying", "verified"}:
2284
2190
  if base_revision != current["base_revision"]:
2285
2191
  raise SystemExit(
@@ -2300,59 +2206,133 @@ def set_finding_remediation(
2300
2206
  current,
2301
2207
  patch_path,
2302
2208
  )
2303
- connection.execute("BEGIN IMMEDIATE")
2304
- timestamp = now()
2305
- occurrence = require_occurrence(connection, args.occurrence_id)
2306
- require_finding_open(connection, occurrence["id"])
2307
- replace_failure_summary = current["state"] == "failed" and args.state != "failed"
2308
- updated = connection.execute(
2309
- """
2310
- UPDATE finding_remediation_attempts
2311
- SET state = ?, version = version + 1, patch_path = ?, patch_digest = ?,
2312
- applied_content_digest = ?,
2313
- pending_action = CASE
2314
- WHEN ? IN ('verifying', 'failed') THEN pending_action ELSE NULL
2315
- END,
2316
- pending_action_claimed_at = CASE
2317
- WHEN ? = 'verifying' THEN pending_action_claimed_at ELSE NULL
2318
- END,
2319
- pending_action_claim_token = CASE
2320
- WHEN ? = 'verifying' THEN pending_action_claim_token ELSE NULL
2321
- END,
2322
- pending_action_delivered_at = CASE
2323
- WHEN ? = 'verifying' THEN pending_action_delivered_at ELSE NULL
2324
- END,
2325
- summary = CASE WHEN ? THEN ? ELSE COALESCE(?, summary) END,
2326
- verification_summary = COALESCE(?, verification_summary),
2327
- updated_at = ?
2328
- WHERE request_id = ? AND occurrence_id = ? AND version = ?
2329
- AND pending_action_claim_token = ?
2330
- """,
2331
- (
2332
- args.state,
2333
- patch_path,
2334
- patch_digest,
2335
- applied_content_digest,
2336
- args.state,
2337
- args.state,
2338
- args.state,
2339
- args.state,
2340
- replace_failure_summary,
2341
- summary,
2342
- summary,
2343
- verification_summary,
2344
- timestamp,
2345
- request_id,
2346
- occurrence["id"],
2347
- args.expected_version,
2348
- action_token,
2349
- ),
2209
+
2210
+ def record_update() -> None:
2211
+ connection.execute("BEGIN IMMEDIATE")
2212
+ timestamp = now()
2213
+ occurrence = require_occurrence(connection, args.occurrence_id)
2214
+ require_finding_open(connection, occurrence["id"])
2215
+ replace_failure_summary = current["state"] == "failed" and args.state != "failed"
2216
+ updated = connection.execute(
2217
+ """
2218
+ UPDATE finding_remediation_attempts
2219
+ SET state = ?, version = version + 1, patch_path = ?, patch_digest = ?,
2220
+ applied_content_digest = ?,
2221
+ pending_action = CASE
2222
+ WHEN ? IN ('verifying', 'failed') THEN pending_action ELSE NULL
2223
+ END,
2224
+ pending_action_claimed_at = CASE
2225
+ WHEN ? = 'verifying' THEN pending_action_claimed_at ELSE NULL
2226
+ END,
2227
+ pending_action_claim_token = CASE
2228
+ WHEN ? = 'verifying' THEN pending_action_claim_token ELSE NULL
2229
+ END,
2230
+ pending_action_delivered_at = CASE
2231
+ WHEN ? = 'verifying' THEN pending_action_delivered_at ELSE NULL
2232
+ END,
2233
+ summary = CASE WHEN ? THEN ? ELSE COALESCE(?, summary) END,
2234
+ verification_summary = COALESCE(?, verification_summary),
2235
+ updated_at = ?
2236
+ WHERE request_id = ? AND occurrence_id = ? AND version = ?
2237
+ AND pending_action_claim_token = ?
2238
+ """,
2239
+ (
2240
+ args.state,
2241
+ patch_path,
2242
+ patch_digest,
2243
+ applied_content_digest,
2244
+ args.state,
2245
+ args.state,
2246
+ args.state,
2247
+ args.state,
2248
+ replace_failure_summary,
2249
+ summary,
2250
+ summary,
2251
+ verification_summary,
2252
+ timestamp,
2253
+ request_id,
2254
+ occurrence["id"],
2255
+ args.expected_version,
2256
+ action_token,
2257
+ ),
2258
+ )
2259
+ if updated.rowcount != 1:
2260
+ raise SystemExit(
2261
+ "This remediation request changed. Refresh it before recording an update."
2262
+ )
2263
+
2264
+ if patch is None:
2265
+ record_update()
2266
+ else:
2267
+ try:
2268
+ remediation_state.save_patch(
2269
+ _WORKBENCH_DB_CONTEXT,
2270
+ connection,
2271
+ scan["id"],
2272
+ current,
2273
+ patch_path,
2274
+ patch_digest,
2275
+ patch,
2276
+ record_update,
2277
+ )
2278
+ except (ContractError, OSError, SystemExit) as exc:
2279
+ raise SystemExit(f"Could not save the remediation patch: {exc}") from exc
2280
+ if getattr(args, "assigned_finding", False):
2281
+ return assigned_remediation_context(
2282
+ connection,
2283
+ occurrence["scan_id"],
2284
+ occurrence["id"],
2285
+ request_id,
2286
+ action_token,
2287
+ written_version=args.expected_version + 1,
2350
2288
  )
2351
- if updated.rowcount != 1:
2289
+ return scan_context(connection, occurrence["scan_id"], occurrence["id"])
2290
+
2291
+
2292
+ def assigned_remediation_context(
2293
+ connection: sqlite3.Connection,
2294
+ scan_id: str,
2295
+ occurrence_id: str,
2296
+ request_id: str,
2297
+ action_token: str,
2298
+ *,
2299
+ written_version: int | None = None,
2300
+ ) -> dict[str, Any]:
2301
+ """After authorization, project only the assigned finding and check the returned action state."""
2302
+ occurrence = require_occurrence(connection, occurrence_id)
2303
+ if occurrence["details_json"] == "{}":
2304
+ backfill_legacy_finding_details(connection, require_scan(connection, scan_id))
2305
+ occurrence = require_occurrence(connection, occurrence_id)
2306
+ if occurrence["scan_id"] != scan_id:
2307
+ raise SystemExit("This finding does not belong to the selected scan.")
2308
+ scan = require_scan(connection, scan_id)
2309
+ finding = finding_result(connection, scan, occurrence, related=None)
2310
+ state = finding["remediationState"]
2311
+ if written_version is None:
2312
+ if not (
2313
+ state.get("requestId") == request_id
2314
+ and state.get("actionClaimToken") == action_token
2315
+ and state.get("pendingAction")
2316
+ ):
2352
2317
  raise SystemExit(
2353
- "This remediation request changed. Refresh it before recording an update."
2318
+ "This remediation action is no longer owned by the supplied request and action token."
2354
2319
  )
2355
- return scan_context(connection, occurrence["scan_id"])
2320
+ elif (
2321
+ state.get("requestId") != request_id
2322
+ or state.get("version") != written_version
2323
+ or state.get("actionClaimToken") not in (None, action_token)
2324
+ ):
2325
+ # The next action may already have started since this write committed.
2326
+ return {"occurrenceId": occurrence_id, "requestId": request_id, "version": written_version}
2327
+ return {
2328
+ "scan": {
2329
+ "scanId": scan["id"],
2330
+ "targetPath": scan["target_path"],
2331
+ "targetRevision": scan["target_revision"],
2332
+ "scanDir": scan["scan_dir"],
2333
+ "findings": [finding],
2334
+ }
2335
+ }
2356
2336
 
2357
2337
 
2358
2338
  def require_reviewed_patch_applied(
@@ -2466,20 +2446,6 @@ def require_remediation_checkout_unchanged(
2466
2446
  )
2467
2447
 
2468
2448
 
2469
- def require_sha256_digest(value: str, label: str) -> str:
2470
- normalized = optional_text(value, maximum=71)
2471
- if (
2472
- normalized is None
2473
- or not normalized.startswith("sha256:")
2474
- or len(normalized) != 71
2475
- or any(
2476
- character not in "0123456789abcdef" for character in normalized.removeprefix("sha256:")
2477
- )
2478
- ):
2479
- raise SystemExit(f"{label} must use sha256:<64 lowercase hex characters>.")
2480
- return normalized
2481
-
2482
-
2483
2449
  def require_scan_relative_file(scan: sqlite3.Row, value: str) -> str:
2484
2450
  if len(value) > 4096:
2485
2451
  raise SystemExit("Text value must be no longer than 4096 characters.")
@@ -2493,11 +2459,7 @@ def require_scan_relative_file(scan: sqlite3.Row, value: str) -> str:
2493
2459
 
2494
2460
 
2495
2461
  def require_matching_patch_digest(scan: sqlite3.Row, patch_path: str, patch_digest: str) -> None:
2496
- digest = hashlib.sha256()
2497
- with open_scan_local_file(Path(scan["scan_dir"]), patch_path) as patch:
2498
- while chunk := patch.read(1024 * 1024):
2499
- digest.update(chunk)
2500
- if f"sha256:{digest.hexdigest()}" != patch_digest:
2462
+ if scan_local_file_digest(Path(scan["scan_dir"]), patch_path) != patch_digest:
2501
2463
  raise SystemExit("Patch digest does not match the scan-local patch file.")
2502
2464
 
2503
2465
 
@@ -2558,11 +2520,10 @@ def workspace_state(
2558
2520
  }
2559
2521
  selected_scan_id = result_scan_id or workspace["active_scan_id"]
2560
2522
  if selected_scan_id:
2561
- selected_scan = require_scan(connection, selected_scan_id)
2562
- result["userContext"] = selected_scan["user_context"]
2563
- result["results"] = (
2564
- result_scan if result_scan is not None else scan_result(connection, selected_scan)
2565
- )
2523
+ if result_scan is None:
2524
+ result_scan = scan_result(connection, require_scan(connection, selected_scan_id))
2525
+ result["userContext"] = result_scan["userContext"]
2526
+ result["results"] = result_scan
2566
2527
  return result
2567
2528
 
2568
2529
  target_metadata = None
@@ -2603,10 +2564,20 @@ def scan_context(
2603
2564
  connection: sqlite3.Connection,
2604
2565
  scan_id: str,
2605
2566
  occurrence_id: str | None = None,
2567
+ *,
2568
+ model_thread_id: str | None = None,
2606
2569
  ) -> dict[str, Any]:
2607
2570
  scan = require_scan(connection, scan_id)
2608
- result = scan_result(connection, scan, occurrence_id=occurrence_id)
2609
- workspace_result = result if occurrence_id is None else scan_result(connection, scan)
2571
+ # Legacy backfill may write before scan_result reloads the row for its response.
2572
+ handoff.require_tokenless_scan_owner(connection, scan, model_thread_id)
2573
+ result = scan_result(
2574
+ connection, scan, occurrence_id=occurrence_id, model_thread_id=model_thread_id
2575
+ )
2576
+ workspace_result = result
2577
+ if len(result["findings"]) > FINDINGS_RESULT_LIMIT:
2578
+ workspace_result = dict(
2579
+ result, findings=result["findings"][:FINDINGS_RESULT_LIMIT], findingsTruncated=True
2580
+ )
2610
2581
  workspace = workspace_state(
2611
2582
  connection,
2612
2583
  scan["workspace_id"],
@@ -2625,7 +2596,77 @@ def scan_context(
2625
2596
 
2626
2597
 
2627
2598
  def get_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
2628
- result = scan_context(connection, args.scan_id, args.occurrence_id)
2599
+ if getattr(args, "progress_only", False):
2600
+ scan = connection.execute(
2601
+ """
2602
+ SELECT scans.id, scans.mode, scans.phase, scans.status, scans.canceled_at,
2603
+ progress.*, progress.updated_at AS progress_updated_at
2604
+ FROM scans JOIN scan_progress AS progress ON progress.scan_id = scans.id
2605
+ WHERE scans.id = ?
2606
+ """,
2607
+ (resolve_scan_id(connection, args.scan_id),),
2608
+ ).fetchone()
2609
+ if scan is None:
2610
+ raise SystemExit("Codex Security scan not found.")
2611
+ progress, _ = scan_progress_result(connection, scan)
2612
+ return {"scan": {"progress": progress}}
2613
+ request_id = getattr(args, "request_id", None)
2614
+ action_token = getattr(args, "action_token", None)
2615
+ if request_id is not None or action_token is not None:
2616
+ scan_id = require_uuid(args.scan_id, "scan-id")
2617
+ request_id = require_uuid(request_id or "", "request-id")
2618
+ action_token = require_uuid(action_token or "", "action-token")
2619
+ authorized = connection.execute(
2620
+ """
2621
+ SELECT 1 FROM finding_remediation_attempts AS action
2622
+ JOIN finding_occurrences AS finding ON finding.id = action.occurrence_id
2623
+ WHERE finding.scan_id = ? AND finding.id = ? AND action.request_id = ?
2624
+ AND action.pending_action_claim_token = ? AND action.pending_action IS NOT NULL
2625
+ """,
2626
+ (scan_id, args.occurrence_id, request_id, action_token),
2627
+ ).fetchone()
2628
+ if authorized is None:
2629
+ raise SystemExit(
2630
+ "This remediation action is no longer owned by the supplied request and action token."
2631
+ )
2632
+ return assigned_remediation_context(
2633
+ connection, scan_id, args.occurrence_id, request_id, action_token
2634
+ )
2635
+ context_claim = getattr(args, "context_claim_token", None)
2636
+ model_thread = getattr(args, "model_thread_id", None)
2637
+ if context_claim is not None:
2638
+ scan = require_scan(connection, args.scan_id)
2639
+ handoff.require_tokenless_scan_owner(connection, scan, model_thread)
2640
+ if scan["handoff_status"] == "pending":
2641
+ detail = (
2642
+ "Pass the handoffClaimToken returned by the Codex Security scan launcher."
2643
+ if scan["handoff_claim_token"]
2644
+ else "Claim the pending Codex Security scan handoff before loading its context."
2645
+ )
2646
+ raise SystemExit(f"This Codex Security scan handoff has not been delivered. {detail}")
2647
+ if context_claim:
2648
+ context_claim = handoff.require_handoff_claim_token(context_claim)
2649
+ if scan["handoff_claim_token"] and scan["handoff_claim_token"] != context_claim:
2650
+ if not model_thread:
2651
+ raise SystemExit("Scan context requires its current continuation claim.")
2652
+ owner = (
2653
+ scan["continuation_thread_id"]
2654
+ or require_workspace(connection, scan["workspace_id"])["thread_id"]
2655
+ )
2656
+ if model_thread != owner:
2657
+ raise SystemExit("Scan context is owned by another continuation.")
2658
+ if (
2659
+ args.occurrence_id is not None
2660
+ and connection.execute(
2661
+ "SELECT 1 FROM finding_occurrences WHERE scan_id = ? AND id = ?",
2662
+ (scan["id"], args.occurrence_id),
2663
+ ).fetchone()
2664
+ is None
2665
+ ):
2666
+ raise SystemExit("This finding was not found in the selected scan.")
2667
+ result = scan_context(
2668
+ connection, args.scan_id, args.occurrence_id, model_thread_id=model_thread
2669
+ )
2629
2670
  if result["scan"]["progress"]["status"] != "running":
2630
2671
  scan = require_scan(connection, args.scan_id)
2631
2672
  try:
@@ -2638,7 +2679,7 @@ def get_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[s
2638
2679
  def list_findings(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
2639
2680
  scan = require_scan(connection, args.scan_id)
2640
2681
  backfill_legacy_finding_details(connection, scan)
2641
- limit = min(args.limit, FINDINGS_PAGE_MAX)
2682
+ limit = args.limit
2642
2683
  rows = scan_history.finding_occurrence_rows(
2643
2684
  connection,
2644
2685
  scan["id"],
@@ -2677,16 +2718,60 @@ def list_findings(connection: sqlite3.Connection, args: argparse.Namespace) -> d
2677
2718
  }
2678
2719
 
2679
2720
 
2721
+ def scan_progress_result(
2722
+ connection: sqlite3.Connection, scan: sqlite3.Row
2723
+ ) -> tuple[dict[str, Any], str]:
2724
+ independent_reviews = (
2725
+ deep_scan.independent_review_progress(
2726
+ connection, scan["id"], parent_running=scan["status"] == "running"
2727
+ )
2728
+ if scan["mode"] == "deep"
2729
+ else None
2730
+ )
2731
+ progress_result = {
2732
+ "candidates": {"reportable": scan["reportable_findings_count"]},
2733
+ "coverage": {
2734
+ "closedRows": scan["review_items_completed"],
2735
+ "filesTotal": scan["scope_file_count"],
2736
+ "worklistRows": scan["review_items_total"],
2737
+ },
2738
+ "phase": scan["phase"],
2739
+ "phaseProgress": {
2740
+ "completed": scan["phase_items_completed"],
2741
+ "total": scan["phase_items_total"],
2742
+ "unit": scan["phase_progress_unit"],
2743
+ },
2744
+ "preflightProgress": {
2745
+ "completed": scan["preflight_checks_completed"],
2746
+ "total": scan["preflight_checks_total"],
2747
+ },
2748
+ "preflightIssues": json.loads(scan["preflight_issues_json"]),
2749
+ "reviewPass": scan["deep_review_pass"],
2750
+ "status": "canceled" if scan["canceled_at"] else scan["status"],
2751
+ "updatedAt": scan["progress_updated_at"],
2752
+ }
2753
+ review_updated_at = ""
2754
+ if independent_reviews is not None:
2755
+ review_updated_at = str(independent_reviews.pop("updatedAt"))
2756
+ progress_result["independentReviews"] = independent_reviews
2757
+ return progress_result, review_updated_at
2758
+
2759
+
2680
2760
  def scan_result(
2681
2761
  connection: sqlite3.Connection,
2682
2762
  scan: sqlite3.Row,
2683
2763
  *,
2684
2764
  occurrence_id: str | None = None,
2765
+ model_thread_id: str | None = None,
2685
2766
  ) -> dict[str, Any]:
2767
+ """Assemble the workbench view of a saved scan, backfilling older finding details as needed.
2768
+
2769
+ An occurrence ID selects one finding from that scan. An occurrence belonging
2770
+ to a different scan raises SystemExit.
2771
+ """
2686
2772
  backfill_legacy_finding_details(connection, scan)
2687
- progress = connection.execute(
2688
- "SELECT * FROM scan_progress WHERE scan_id = ?", (scan["id"],)
2689
- ).fetchone()
2773
+ scan = scan_history.scan_with_progress(connection, scan["id"])
2774
+ handoff.require_tokenless_scan_owner(connection, scan, model_thread_id)
2690
2775
  artifact_rows = connection.execute(
2691
2776
  "SELECT kind, path FROM scan_artifacts WHERE scan_id = ?", (scan["id"],)
2692
2777
  )
@@ -2708,14 +2793,12 @@ def scan_result(
2708
2793
  occurrence_rows = scan_history.finding_occurrence_rows(
2709
2794
  connection, scan["id"], offset=0, limit=FINDINGS_RESULT_LIMIT
2710
2795
  )
2711
- if occurrence_id is not None and all(row["id"] != occurrence_id for row in occurrence_rows):
2796
+ if occurrence_id is not None:
2712
2797
  occurrence = require_occurrence(connection, occurrence_id)
2713
2798
  if occurrence["scan_id"] != scan["id"]:
2714
2799
  raise SystemExit("This finding does not belong to the selected scan.")
2715
- occurrence_rows.append(occurrence)
2716
- finding_count = connection.execute(
2717
- "SELECT COUNT(*) FROM finding_occurrences WHERE scan_id = ?", (scan["id"],)
2718
- ).fetchone()[0]
2800
+ if all(row["id"] != occurrence["id"] for row in occurrence_rows):
2801
+ occurrence_rows.append(occurrence)
2719
2802
  severity_counts = {
2720
2803
  row["severity"]: row["count"]
2721
2804
  for row in connection.execute(
@@ -2728,38 +2811,9 @@ def scan_result(
2728
2811
  (scan["id"],),
2729
2812
  )
2730
2813
  }
2814
+ finding_count = sum(severity_counts.values())
2731
2815
  remediation_available, remediation_unavailable_reason = remediation_availability(scan)
2732
- independent_reviews = (
2733
- deep_scan.independent_review_progress(connection, scan["id"])
2734
- if scan["mode"] == "deep"
2735
- else None
2736
- )
2737
- progress_result = {
2738
- "candidates": {"reportable": progress["reportable_findings_count"]},
2739
- "coverage": {
2740
- "closedRows": progress["review_items_completed"],
2741
- "filesTotal": progress["scope_file_count"],
2742
- "worklistRows": progress["review_items_total"],
2743
- },
2744
- "phase": scan["phase"],
2745
- "phaseProgress": {
2746
- "completed": progress["phase_items_completed"],
2747
- "total": progress["phase_items_total"],
2748
- "unit": progress["phase_progress_unit"],
2749
- },
2750
- "preflightProgress": {
2751
- "completed": progress["preflight_checks_completed"],
2752
- "total": progress["preflight_checks_total"],
2753
- },
2754
- "preflightIssues": json.loads(progress["preflight_issues_json"]),
2755
- "reviewPass": progress["deep_review_pass"],
2756
- "status": "canceled" if scan["canceled_at"] else scan["status"],
2757
- "updatedAt": progress["updated_at"],
2758
- }
2759
- review_updated_at = ""
2760
- if independent_reviews is not None:
2761
- review_updated_at = str(independent_reviews.pop("updatedAt"))
2762
- progress_result["independentReviews"] = independent_reviews
2816
+ progress_result, review_updated_at = scan_progress_result(connection, scan)
2763
2817
  relations = scan_history.finding_relations(
2764
2818
  connection, scan["id"], (row["id"] for row in occurrence_rows)
2765
2819
  )
@@ -2767,12 +2821,14 @@ def scan_result(
2767
2821
  "artifacts": artifacts,
2768
2822
  **model_fields,
2769
2823
  "canceledAt": scan["canceled_at"],
2824
+ "logCompletedAt": scan["log_completed_at"],
2770
2825
  **scan_usage.stored_scan_cost_fields(scan["cost_json"]),
2771
2826
  "contract": scan_contract(scan),
2772
2827
  "continuationThreadId": scan["continuation_thread_id"],
2773
2828
  "threadIds": scan_usage._scan_root_thread_ids(connection, scan, None),
2774
2829
  "executionThreadIds": scan_usage._scan_execution_thread_ids(connection, scan),
2775
2830
  "failureMessage": scan["failure_message"],
2831
+ "lifecycle": json.loads(scan["lifecycle_json"]),
2776
2832
  "findings": [
2777
2833
  finding_result(connection, scan, row, related=relations.get(row["id"], []))
2778
2834
  for row in occurrence_rows
@@ -2803,7 +2859,7 @@ def scan_result(
2803
2859
  "targetSummary": scan["target_summary"],
2804
2860
  "updatedAt": max(
2805
2861
  scan["updated_at"],
2806
- progress["updated_at"],
2862
+ scan["progress_updated_at"],
2807
2863
  review_updated_at,
2808
2864
  finding_management_updated_at(connection, scan["id"]) or "",
2809
2865
  ),
@@ -2921,7 +2977,7 @@ def finding_result(
2921
2977
  scan: sqlite3.Row,
2922
2978
  occurrence: sqlite3.Row,
2923
2979
  *,
2924
- related: list[dict[str, Any]],
2980
+ related: list[dict[str, Any]] | None,
2925
2981
  ) -> dict[str, Any]:
2926
2982
  details = bounded_finding_details(read_finding_details(occurrence["details_json"]))
2927
2983
  confidence = details.get("confidence")
@@ -2935,11 +2991,11 @@ def finding_result(
2935
2991
  except SystemExit:
2936
2992
  target = None
2937
2993
  for row in connection.execute(
2938
- """
2994
+ f"""
2939
2995
  SELECT relative_path, start_line, end_line, role
2940
2996
  FROM finding_locations
2941
2997
  WHERE occurrence_id = ?
2942
- ORDER BY CASE WHEN role = 'root_control' THEN 0 ELSE 1 END, sort_order
2998
+ ORDER BY {FINDING_LOCATION_ORDER_SQL}
2943
2999
  LIMIT ?
2944
3000
  """,
2945
3001
  (occurrence["id"], FINDING_LOCATIONS_LIMIT),
@@ -2990,15 +3046,16 @@ def finding_result(
2990
3046
  "title": bounded_output_text(occurrence["title"], FINDING_TITLE_BYTES),
2991
3047
  "triage": finding_triage_result(connection, occurrence["id"]),
2992
3048
  }
2993
- matches, known_since, known_scan_ids = scan_history.finding_matches(
2994
- connection, occurrence["id"], scan["id"], scan["started_at"]
2995
- )
2996
- if matches:
2997
- result["matches"] = matches
2998
- result["knownSince"] = known_since
2999
- result["knownScanIds"] = known_scan_ids
3000
- if related:
3001
- result["related"] = related
3049
+ if related is not None:
3050
+ matches, known_since, known_scan_ids = scan_history.finding_matches(
3051
+ connection, occurrence["id"], scan["id"], scan["started_at"]
3052
+ )
3053
+ if matches:
3054
+ result["matches"] = matches
3055
+ result["knownSince"] = known_since
3056
+ result["knownScanIds"] = known_scan_ids
3057
+ if related:
3058
+ result["related"] = related
3002
3059
  source_excerpt = finding_source_excerpt(
3003
3060
  scan, target, excerpt_locations, requested_scan_paths(scan)
3004
3061
  )
@@ -3235,7 +3292,7 @@ def artifact_path(scan_dir: Path, file_name: str, *, required: bool) -> Path | N
3235
3292
  return resolved
3236
3293
 
3237
3294
 
3238
- def require_canonical_scan_directory(scan_dir: Path) -> Path:
3295
+ def require_canonical_scan_directory(scan_dir: Path, *, writable_output: bool = False) -> Path:
3239
3296
  scan_dir = scan_dir.absolute()
3240
3297
  try:
3241
3298
  metadata = scan_dir.lstat()
@@ -3249,7 +3306,14 @@ def require_canonical_scan_directory(scan_dir: Path) -> Path:
3249
3306
  ):
3250
3307
  raise SystemExit("Scan directory must be an existing canonical non-symlink directory.")
3251
3308
  # Re-check ownership and parents so the scan directory cannot be substituted.
3252
- if os.name != "nt":
3309
+ if os.name == "nt":
3310
+ from windows_scan_local_files import require_windows_output_path
3311
+
3312
+ try:
3313
+ require_windows_output_path(scan_dir, writable_output=writable_output)
3314
+ except OSError as exc:
3315
+ raise SystemExit(str(exc)) from exc
3316
+ else:
3253
3317
  geteuid = getattr(os, "geteuid", None)
3254
3318
  effective_uid = geteuid() if geteuid is not None else None
3255
3319
  if effective_uid is not None and metadata.st_uid != effective_uid:
@@ -3276,19 +3340,6 @@ def require_canonical_scan_directory(scan_dir: Path) -> Path:
3276
3340
  return scan_dir
3277
3341
 
3278
3342
 
3279
- def read_json_object(path: Path) -> dict[str, Any]:
3280
- try:
3281
- payload = json.loads(
3282
- path.read_text(encoding="utf-8"),
3283
- parse_constant=reject_non_finite_json,
3284
- )
3285
- except (OSError, ValueError) as exc:
3286
- raise SystemExit(f"{path.name}: invalid JSON: {exc}") from exc
3287
- if not isinstance(payload, dict):
3288
- raise SystemExit(f"{path.name}: expected a JSON object.")
3289
- return payload
3290
-
3291
-
3292
3343
  _WORKBENCH_PUBLICATION_CONTEXT = _WORKBENCH_DB_CONTEXT = SimpleNamespace(**globals())
3293
3344
 
3294
3345
 
@@ -3304,22 +3355,28 @@ def main(*, before_archive: Callable[[], None] | None = None) -> None:
3304
3355
  if args.command == "resolve-scan-root":
3305
3356
  print(json.dumps({"scanRoot": str(resolve_scan_root(args.scan_root))}))
3306
3357
  return
3307
- if args.command == "inspect-target":
3308
- result = inspect_target(args.target_path)
3309
- print(json.dumps(result, allow_nan=False, sort_keys=True))
3310
- return
3311
- if args.command == "inspect-setup":
3312
- print(json.dumps(inspect_selected_setup(args), allow_nan=False, sort_keys=True))
3358
+ if args.command in {"inspect-target", "inspect-setup"}:
3359
+ print(json.dumps(inspect_command(args), allow_nan=False, sort_keys=True))
3313
3360
  return
3314
3361
  if args.command in {"save-artifact", "read-artifact"}:
3315
3362
  print(json.dumps(saved_results.read_or_save_artifact(args)))
3316
3363
  return
3317
3364
  if args.command == "finding-issues":
3318
- result = publication.finding_issues(_WORKBENCH_PUBLICATION_CONTEXT, json.load(sys.stdin))
3365
+ try:
3366
+ result = publication.finding_issues(
3367
+ _WORKBENCH_PUBLICATION_CONTEXT, json.load(sys.stdin)
3368
+ )
3369
+ except sqlite3.OperationalError as exc:
3370
+ if not getattr(exc, "_codex_security_state_unavailable", False):
3371
+ raise
3372
+ print(f"sqlite3.OperationalError: {exc}", file=sys.stderr)
3373
+ raise SystemExit(73) from None
3319
3374
  print(json.dumps(result, allow_nan=False, sort_keys=True))
3320
3375
  return
3321
3376
  with closing(
3322
- connect(deferred=args.command in {"get-scan", "list-scans", "database-info"})
3377
+ connect(
3378
+ deferred=args.command in {"get-scan", "read-scan-usage", "list-scans", "database-info"}
3379
+ )
3323
3380
  ) as connection:
3324
3381
  remediation_state.require_available(connection, args, require_scan)
3325
3382
  handlers = {
@@ -3331,8 +3388,7 @@ def main(*, before_archive: Callable[[], None] | None = None) -> None:
3331
3388
  "get-deep-scan": deep_scan.get_deep_scan,
3332
3389
  "claim-deep-scan-coordinator": deep_scan.claim_deep_scan_coordinator,
3333
3390
  "upsert-deep-scan-worker": deep_scan.upsert_deep_scan_worker,
3334
- "claim-deep-scan-dedup": deep_scan.claim_deep_scan_dedup,
3335
- "commit-deep-scan-dedup": deep_scan.commit_deep_scan_dedup,
3391
+ "record-deep-scan-iteration": deep_scan.record_deep_scan_iteration,
3336
3392
  "finish-deep-scan": deep_scan.finish_deep_scan,
3337
3393
  "fail-deep-scan": deep_scan.fail_deep_scan,
3338
3394
  "record-deep-scan-publication-failure": deep_scan.record_deep_scan_publication_failure,
@@ -3340,6 +3396,7 @@ def main(*, before_archive: Callable[[], None] | None = None) -> None:
3340
3396
  "register-cli-scan": partial(register_cli_scan, before_archive=before_archive),
3341
3397
  "set-scan-thread": set_scan_thread,
3342
3398
  "set-scan-cost-limit": set_scan_cost_limit,
3399
+ "read-scan-usage": read_scan_usage,
3343
3400
  "finalize-scan-usage": finalize_scan_usage,
3344
3401
  "list-global-findings": native_indexes.list_global_findings,
3345
3402
  "list-repositories": native_indexes.list_repositories,
@@ -3445,8 +3502,6 @@ def main(*, before_archive: Callable[[], None] | None = None) -> None:
3445
3502
  )
3446
3503
  elif args.command == "database-info":
3447
3504
  result = {"databasePath": str(database_path())}
3448
- elif args.command == "local-dedupe":
3449
- result = local_dedupe(connection, json.load(sys.stdin), now())
3450
3505
  elif args.command == "finding-workflow":
3451
3506
  result = finding_workflow(connection, json.load(sys.stdin), now())
3452
3507
  else:
@@ -3455,4 +3510,7 @@ def main(*, before_archive: Callable[[], None] | None = None) -> None:
3455
3510
 
3456
3511
 
3457
3512
  if __name__ == "__main__":
3458
- main()
3513
+ try:
3514
+ main()
3515
+ except ContractError as exc:
3516
+ raise SystemExit(str(exc)) from None