@openai/codex-security 0.8.3 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (658) hide show
  1. package/README.md +36 -746
  2. package/_bundled_plugin/.codex-plugin/plugin.json +2 -0
  3. package/_bundled_plugin/.mcp.json +7 -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 +5 -2
  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/baseline-auditor.md +18 -0
  26. package/_bundled_plugin/references/core-scan.md +5 -42
  27. package/_bundled_plugin/references/default-severity-rubric.md +9 -4
  28. package/_bundled_plugin/references/final-report.md +5 -3
  29. package/_bundled_plugin/references/finding-detail-fields.md +18 -18
  30. package/_bundled_plugin/references/focused-investigator.md +13 -0
  31. package/_bundled_plugin/references/sarif-adapter.md +8 -2
  32. package/_bundled_plugin/references/scan-artifacts.md +21 -12
  33. package/_bundled_plugin/references/scan-contract.md +27 -33
  34. package/_bundled_plugin/references/scan-prologue.md +2 -2
  35. package/_bundled_plugin/references/scan-runtime.md +4 -2
  36. package/_bundled_plugin/references/security-guidance.md +14 -1
  37. package/_bundled_plugin/references/validate-scan-findings.md +3 -0
  38. package/_bundled_plugin/schemas/tools/scan-draft.schema.json +2 -2
  39. package/_bundled_plugin/scripts/check_deep_scan_target.py +97 -0
  40. package/_bundled_plugin/scripts/codex_profile.mjs +7 -1
  41. package/_bundled_plugin/scripts/deep_scan_config.py +20 -1
  42. package/_bundled_plugin/scripts/deep_scan_workbench.py +192 -352
  43. package/_bundled_plugin/scripts/dispatch_inventory_tool +7 -0
  44. package/_bundled_plugin/scripts/filesystem_identity.py +1 -0
  45. package/_bundled_plugin/scripts/finalize_scan_contract.py +182 -230
  46. package/_bundled_plugin/scripts/finding_preview.py +20 -0
  47. package/_bundled_plugin/scripts/launch_codex_security_mcp +55 -16
  48. package/_bundled_plugin/scripts/legacy_findings.py +148 -0
  49. package/_bundled_plugin/scripts/posix_scan_local_files.py +37 -1
  50. package/_bundled_plugin/scripts/read_codex_session.mjs +21 -0
  51. package/_bundled_plugin/scripts/report_projection.py +81 -43
  52. package/_bundled_plugin/scripts/review_state.py +8 -2
  53. package/_bundled_plugin/scripts/servicenow_avit.py +20 -0
  54. package/_bundled_plugin/scripts/snapshot_sqlite.py +9 -2
  55. package/_bundled_plugin/scripts/threat_model_projection.py +22 -26
  56. package/_bundled_plugin/scripts/validate_scan_contract.py +7 -1
  57. package/_bundled_plugin/scripts/windows_scan_local_files.py +582 -20
  58. package/_bundled_plugin/scripts/workbench/__init__.py +1 -1
  59. package/_bundled_plugin/scripts/workbench/handoff.py +100 -23
  60. package/_bundled_plugin/scripts/workbench/retained_coverage.py +30 -51
  61. package/_bundled_plugin/scripts/workbench/retained_records.py +18 -30
  62. package/_bundled_plugin/scripts/workbench/runtime_toml.py +4 -1
  63. package/_bundled_plugin/scripts/workbench/saved_coverage.py +9 -17
  64. package/_bundled_plugin/scripts/workbench/saved_outputs.py +9 -0
  65. package/_bundled_plugin/scripts/workbench/saved_sources.py +47 -40
  66. package/_bundled_plugin/scripts/workbench/storage.py +91 -7
  67. package/_bundled_plugin/scripts/workbench_cli.py +54 -14
  68. package/_bundled_plugin/scripts/workbench_constants.py +12 -0
  69. package/_bundled_plugin/scripts/workbench_coverage_receipts.py +3 -3
  70. package/_bundled_plugin/scripts/workbench_db.py +562 -303
  71. package/_bundled_plugin/scripts/workbench_feedback.py +57 -11
  72. package/_bundled_plugin/scripts/workbench_finding_index.py +1 -0
  73. package/_bundled_plugin/scripts/workbench_finding_workflows.py +4 -0
  74. package/_bundled_plugin/scripts/workbench_native_indexes.py +238 -41
  75. package/_bundled_plugin/scripts/workbench_progress.py +169 -4
  76. package/_bundled_plugin/scripts/workbench_publication.py +16 -13
  77. package/_bundled_plugin/scripts/workbench_remediation.py +155 -2
  78. package/_bundled_plugin/scripts/workbench_saved_results.py +176 -81
  79. package/_bundled_plugin/scripts/workbench_scan_history.py +398 -174
  80. package/_bundled_plugin/scripts/workbench_scan_start.py +224 -31
  81. package/_bundled_plugin/scripts/workbench_scan_usage.py +675 -73
  82. package/_bundled_plugin/scripts/workbench_schema.py +258 -63
  83. package/_bundled_plugin/scripts/workbench_setup.py +14 -2
  84. package/_bundled_plugin/scripts/workbench_source_excerpt.py +13 -0
  85. package/_bundled_plugin/scripts/workbench_source_scopes.py +18 -0
  86. package/_bundled_plugin/scripts/workbench_target.py +594 -136
  87. package/_bundled_plugin/scripts/workbench_target_state.py +1110 -21
  88. package/_bundled_plugin/scripts/workbench_validation.py +43 -3
  89. package/_bundled_plugin/shared/workbench-history-repairs.json +77 -35
  90. package/_bundled_plugin/shared/workbench-migrations.json +100 -0
  91. package/_bundled_plugin/skills/cloud-findings/SKILL.md +5 -5
  92. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +7 -50
  93. package/_bundled_plugin/skills/fix-finding/SKILL.md +2 -1
  94. package/_bundled_plugin/skills/publish-findings/SKILL.md +24 -153
  95. package/_bundled_plugin/skills/publish-findings/agents/openai.yaml +2 -2
  96. package/_bundled_plugin/skills/publish-findings/references/checkmarx-import.md +37 -0
  97. package/_bundled_plugin/skills/publish-findings/references/wiz-import.md +3 -3
  98. package/_bundled_plugin/skills/publish-findings/scripts/collect_checkmarx.py +409 -0
  99. package/_bundled_plugin/skills/publish-findings/scripts/collect_continuum.py +224 -0
  100. package/_bundled_plugin/skills/publish-findings/scripts/collector_paths.py +63 -0
  101. package/_bundled_plugin/skills/security-scan/SKILL.md +9 -46
  102. package/_bundled_plugin/skills/security-scan/agents/openai.yaml +2 -2
  103. package/_bundled_plugin/skills/security-scan/references/desktop-scan.md +3 -21
  104. package/_bundled_plugin/skills/security-scan/references/managed-scan.md +11 -0
  105. package/_bundled_plugin/skills/security-scan/references/scan-results.md +15 -0
  106. package/_bundled_plugin/skills/security-scan/references/scoped-paths.md +23 -0
  107. package/_bundled_plugin/skills/security-scan/references/sdk-files.md +24 -0
  108. package/_bundled_plugin/skills/threat-model-visualize/scripts/build_explorer.py +22 -1
  109. package/_bundled_plugin/skills/triage-finding/references/ticket-intake.md +6 -2
  110. package/bin/codex-security.mjs +2 -2
  111. package/dist/api.d.ts +45 -5
  112. package/dist/api.d.ts.map +1 -1
  113. package/dist/api.js +1192 -427
  114. package/dist/api.js.map +1 -1
  115. package/dist/armorcode-findings.d.ts +9 -0
  116. package/dist/armorcode-findings.d.ts.map +1 -0
  117. package/dist/armorcode-findings.js +145 -0
  118. package/dist/armorcode-findings.js.map +1 -0
  119. package/dist/artifact-export.d.ts +2 -0
  120. package/dist/artifact-export.d.ts.map +1 -1
  121. package/dist/artifact-export.js +29 -13
  122. package/dist/artifact-export.js.map +1 -1
  123. package/dist/auth.d.ts +6 -2
  124. package/dist/auth.d.ts.map +1 -1
  125. package/dist/auth.js +37 -18
  126. package/dist/auth.js.map +1 -1
  127. package/dist/batch-cost.d.ts +18 -0
  128. package/dist/batch-cost.d.ts.map +1 -0
  129. package/dist/batch-cost.js +41 -0
  130. package/dist/batch-cost.js.map +1 -0
  131. package/dist/bedrock-oss-pricing.d.ts +6 -0
  132. package/dist/bedrock-oss-pricing.d.ts.map +1 -0
  133. package/dist/bedrock-oss-pricing.js +58 -0
  134. package/dist/bedrock-oss-pricing.js.map +1 -0
  135. package/dist/bedrock-pricing.d.ts +15 -0
  136. package/dist/bedrock-pricing.d.ts.map +1 -0
  137. package/dist/bedrock-pricing.js +117 -0
  138. package/dist/bedrock-pricing.js.map +1 -0
  139. package/dist/blackduck-findings.d.ts +1 -1
  140. package/dist/blackduck-findings.d.ts.map +1 -1
  141. package/dist/blackduck-findings.js +24 -22
  142. package/dist/blackduck-findings.js.map +1 -1
  143. package/dist/bulk-scan-discovery.d.ts.map +1 -1
  144. package/dist/bulk-scan-discovery.js +4 -4
  145. package/dist/bulk-scan-discovery.js.map +1 -1
  146. package/dist/bundled-plugin.d.ts +4 -0
  147. package/dist/bundled-plugin.d.ts.map +1 -0
  148. package/dist/bundled-plugin.js +23 -0
  149. package/dist/bundled-plugin.js.map +1 -0
  150. package/dist/checkmarx-evidence.d.ts +19 -0
  151. package/dist/checkmarx-evidence.d.ts.map +1 -0
  152. package/dist/checkmarx-evidence.js +36 -0
  153. package/dist/checkmarx-evidence.js.map +1 -0
  154. package/dist/checkmarx-findings.d.ts +4 -0
  155. package/dist/checkmarx-findings.d.ts.map +1 -0
  156. package/dist/checkmarx-findings.js +27 -0
  157. package/dist/checkmarx-findings.js.map +1 -0
  158. package/dist/checkmarx-one.d.ts +4 -0
  159. package/dist/checkmarx-one.d.ts.map +1 -0
  160. package/dist/checkmarx-one.js +53 -0
  161. package/dist/checkmarx-one.js.map +1 -0
  162. package/dist/checkmarx-repository.d.ts +12 -0
  163. package/dist/checkmarx-repository.d.ts.map +1 -0
  164. package/dist/checkmarx-repository.js +181 -0
  165. package/dist/checkmarx-repository.js.map +1 -0
  166. package/dist/checkmarx-xml.d.ts +12 -0
  167. package/dist/checkmarx-xml.d.ts.map +1 -0
  168. package/dist/checkmarx-xml.js +196 -0
  169. package/dist/checkmarx-xml.js.map +1 -0
  170. package/dist/classify-scan-severity.d.ts +1 -1
  171. package/dist/classify-scan-severity.d.ts.map +1 -1
  172. package/dist/classify-scan-severity.js +11 -3
  173. package/dist/classify-scan-severity.js.map +1 -1
  174. package/dist/classify-severity-records.d.ts +1 -9
  175. package/dist/classify-severity-records.d.ts.map +1 -1
  176. package/dist/classify-severity-records.js +3 -13
  177. package/dist/classify-severity-records.js.map +1 -1
  178. package/dist/classify-severity.d.ts.map +1 -1
  179. package/dist/classify-severity.js +11 -10
  180. package/dist/classify-severity.js.map +1 -1
  181. package/dist/cli-argument.d.ts +4 -0
  182. package/dist/cli-argument.d.ts.map +1 -0
  183. package/dist/cli-argument.js +17 -0
  184. package/dist/cli-argument.js.map +1 -0
  185. package/dist/cli-help.d.ts +1 -1
  186. package/dist/cli-help.d.ts.map +1 -1
  187. package/dist/cli-help.js +71 -29
  188. package/dist/cli-help.js.map +1 -1
  189. package/dist/cli-hook.d.ts +3 -0
  190. package/dist/cli-hook.d.ts.map +1 -0
  191. package/dist/cli-hook.js +87 -0
  192. package/dist/cli-hook.js.map +1 -0
  193. package/dist/cli-integrations.d.ts +13 -0
  194. package/dist/cli-integrations.d.ts.map +1 -0
  195. package/dist/cli-integrations.js +333 -0
  196. package/dist/cli-integrations.js.map +1 -0
  197. package/dist/cli-mcp.d.ts +15 -0
  198. package/dist/cli-mcp.d.ts.map +1 -0
  199. package/dist/cli-mcp.js +178 -0
  200. package/dist/cli-mcp.js.map +1 -0
  201. package/dist/cli-token-view.d.ts +10 -0
  202. package/dist/cli-token-view.d.ts.map +1 -0
  203. package/dist/cli-token-view.js +41 -0
  204. package/dist/cli-token-view.js.map +1 -0
  205. package/dist/cli.d.ts +16 -8
  206. package/dist/cli.d.ts.map +1 -1
  207. package/dist/cli.js +3382 -2017
  208. package/dist/cli.js.map +1 -1
  209. package/dist/cloud-publish.d.ts.map +1 -1
  210. package/dist/cloud-publish.js +35 -33
  211. package/dist/cloud-publish.js.map +1 -1
  212. package/dist/codex-sdk-environment.d.ts +6 -0
  213. package/dist/codex-sdk-environment.d.ts.map +1 -1
  214. package/dist/codex-sdk-environment.js +19 -0
  215. package/dist/codex-sdk-environment.js.map +1 -1
  216. package/dist/command-words.d.ts +3 -0
  217. package/dist/command-words.d.ts.map +1 -0
  218. package/dist/command-words.js +46 -0
  219. package/dist/command-words.js.map +1 -0
  220. package/dist/component-scan.d.ts +19 -4
  221. package/dist/component-scan.d.ts.map +1 -1
  222. package/dist/component-scan.js +67 -9
  223. package/dist/component-scan.js.map +1 -1
  224. package/dist/config.d.ts +1 -1
  225. package/dist/config.d.ts.map +1 -1
  226. package/dist/config.js +130 -67
  227. package/dist/config.js.map +1 -1
  228. package/dist/continuum-findings.d.ts +1 -1
  229. package/dist/continuum-findings.d.ts.map +1 -1
  230. package/dist/continuum-findings.js +149 -19
  231. package/dist/continuum-findings.js.map +1 -1
  232. package/dist/contract-values.d.ts +2 -0
  233. package/dist/contract-values.d.ts.map +1 -1
  234. package/dist/contract-values.js +12 -9
  235. package/dist/contract-values.js.map +1 -1
  236. package/dist/cost-model.d.ts +13 -1
  237. package/dist/cost-model.d.ts.map +1 -1
  238. package/dist/cost-model.js +66 -112
  239. package/dist/cost-model.js.map +1 -1
  240. package/dist/cost.d.ts +42 -3
  241. package/dist/cost.d.ts.map +1 -1
  242. package/dist/cost.js +484 -149
  243. package/dist/cost.js.map +1 -1
  244. package/dist/custom-publish.d.ts +4 -0
  245. package/dist/custom-publish.d.ts.map +1 -1
  246. package/dist/custom-publish.js +40 -3
  247. package/dist/custom-publish.js.map +1 -1
  248. package/dist/custom-validation-prompt.d.ts +1 -1
  249. package/dist/custom-validation-prompt.d.ts.map +1 -1
  250. package/dist/custom-validation-prompt.js +29 -19
  251. package/dist/custom-validation-prompt.js.map +1 -1
  252. package/dist/deduplication/local.d.ts +4 -2
  253. package/dist/deduplication/local.d.ts.map +1 -1
  254. package/dist/deduplication/local.js +9 -2
  255. package/dist/deduplication/local.js.map +1 -1
  256. package/dist/deduplication/refusal.d.ts.map +1 -1
  257. package/dist/deduplication/refusal.js +26 -5
  258. package/dist/deduplication/refusal.js.map +1 -1
  259. package/dist/deduplication/scan.d.ts +5 -0
  260. package/dist/deduplication/scan.d.ts.map +1 -1
  261. package/dist/deduplication/scan.js +24 -7
  262. package/dist/deduplication/scan.js.map +1 -1
  263. package/dist/deep-config.d.ts +0 -1
  264. package/dist/deep-config.d.ts.map +1 -1
  265. package/dist/deep-config.js +22 -33
  266. package/dist/deep-config.js.map +1 -1
  267. package/dist/deep-scan-defaults.d.ts +2 -0
  268. package/dist/deep-scan-defaults.d.ts.map +1 -1
  269. package/dist/deep-scan-defaults.js +28 -1
  270. package/dist/deep-scan-defaults.js.map +1 -1
  271. package/dist/deep-scan.d.ts +2 -0
  272. package/dist/deep-scan.d.ts.map +1 -1
  273. package/dist/deep-scan.js +44 -19
  274. package/dist/deep-scan.js.map +1 -1
  275. package/dist/default-severity-rubric.d.ts +1 -1
  276. package/dist/default-severity-rubric.d.ts.map +1 -1
  277. package/dist/default-severity-rubric.js +1 -1
  278. package/dist/default-severity-rubric.js.map +1 -1
  279. package/dist/endor-findings.d.ts +3 -1
  280. package/dist/endor-findings.d.ts.map +1 -1
  281. package/dist/endor-findings.js +86 -45
  282. package/dist/endor-findings.js.map +1 -1
  283. package/dist/errors.d.ts +4 -2
  284. package/dist/errors.d.ts.map +1 -1
  285. package/dist/errors.js +15 -4
  286. package/dist/errors.js.map +1 -1
  287. package/dist/executable-path.d.ts +3 -0
  288. package/dist/executable-path.d.ts.map +1 -1
  289. package/dist/executable-path.js +26 -0
  290. package/dist/executable-path.js.map +1 -1
  291. package/dist/external-findings-import.d.ts +5 -3
  292. package/dist/external-findings-import.d.ts.map +1 -1
  293. package/dist/external-findings-import.js +19 -7
  294. package/dist/external-findings-import.js.map +1 -1
  295. package/dist/external-findings-local.d.ts +69 -5
  296. package/dist/external-findings-local.d.ts.map +1 -1
  297. package/dist/external-findings-local.js +537 -108
  298. package/dist/external-findings-local.js.map +1 -1
  299. package/dist/external-findings-publish.d.ts +13 -6
  300. package/dist/external-findings-publish.d.ts.map +1 -1
  301. package/dist/external-findings-publish.js +895 -1142
  302. package/dist/external-findings-publish.js.map +1 -1
  303. package/dist/external-findings-read.d.ts +21 -2
  304. package/dist/external-findings-read.d.ts.map +1 -1
  305. package/dist/external-findings-read.js +130 -68
  306. package/dist/external-findings-read.js.map +1 -1
  307. package/dist/external-findings.d.ts +15 -3
  308. package/dist/external-findings.d.ts.map +1 -1
  309. package/dist/external-findings.js +54 -2
  310. package/dist/external-findings.js.map +1 -1
  311. package/dist/external-import-contract.d.ts +8 -12
  312. package/dist/external-import-contract.d.ts.map +1 -1
  313. package/dist/external-import-contract.js +51 -184
  314. package/dist/external-import-contract.js.map +1 -1
  315. package/dist/external-import-models.d.ts +1 -1
  316. package/dist/external-import-models.d.ts.map +1 -1
  317. package/dist/external-import-models.js.map +1 -1
  318. package/dist/feedback.d.ts.map +1 -1
  319. package/dist/feedback.js +5 -4
  320. package/dist/feedback.js.map +1 -1
  321. package/dist/finding-prompt.d.ts +5 -0
  322. package/dist/finding-prompt.d.ts.map +1 -0
  323. package/dist/finding-prompt.js +32 -0
  324. package/dist/finding-prompt.js.map +1 -0
  325. package/dist/findings-client.d.ts +1 -0
  326. package/dist/findings-client.d.ts.map +1 -1
  327. package/dist/findings-client.js +27 -3
  328. package/dist/findings-client.js.map +1 -1
  329. package/dist/github-advisories.d.ts.map +1 -1
  330. package/dist/github-advisories.js +0 -2
  331. package/dist/github-advisories.js.map +1 -1
  332. package/dist/github-dependabot-findings.d.ts.map +1 -1
  333. package/dist/github-dependabot-findings.js +18 -8
  334. package/dist/github-dependabot-findings.js.map +1 -1
  335. package/dist/github-findings.d.ts +4 -1
  336. package/dist/github-findings.d.ts.map +1 -1
  337. package/dist/github-findings.js +73 -316
  338. package/dist/github-findings.js.map +1 -1
  339. package/dist/hosted-git-remotes.d.ts +5 -0
  340. package/dist/hosted-git-remotes.d.ts.map +1 -0
  341. package/dist/hosted-git-remotes.js +285 -0
  342. package/dist/hosted-git-remotes.js.map +1 -0
  343. package/dist/hosted-git-repository.d.ts +28 -0
  344. package/dist/hosted-git-repository.d.ts.map +1 -0
  345. package/dist/hosted-git-repository.js +222 -0
  346. package/dist/hosted-git-repository.js.map +1 -0
  347. package/dist/hosted-remediation.d.ts +1 -0
  348. package/dist/hosted-remediation.d.ts.map +1 -1
  349. package/dist/hosted-remediation.js +23 -3
  350. package/dist/hosted-remediation.js.map +1 -1
  351. package/dist/hosted-scan-protocol.js +2 -2
  352. package/dist/hosted-scan-protocol.js.map +1 -1
  353. package/dist/hosted-scan.d.ts.map +1 -1
  354. package/dist/hosted-scan.js +16 -11
  355. package/dist/hosted-scan.js.map +1 -1
  356. package/dist/hosted-scanner-http.d.ts +21 -0
  357. package/dist/hosted-scanner-http.d.ts.map +1 -0
  358. package/dist/hosted-scanner-http.js +204 -0
  359. package/dist/hosted-scanner-http.js.map +1 -0
  360. package/dist/import-scan.d.ts.map +1 -1
  361. package/dist/import-scan.js +3 -0
  362. package/dist/import-scan.js.map +1 -1
  363. package/dist/index.d.ts +21 -9
  364. package/dist/index.d.ts.map +1 -1
  365. package/dist/index.js +12 -3
  366. package/dist/index.js.map +1 -1
  367. package/dist/inspector-findings.d.ts.map +1 -1
  368. package/dist/inspector-findings.js +52 -86
  369. package/dist/inspector-findings.js.map +1 -1
  370. package/dist/knowledge-base.d.ts +1 -1
  371. package/dist/knowledge-base.d.ts.map +1 -1
  372. package/dist/knowledge-base.js +102 -32
  373. package/dist/knowledge-base.js.map +1 -1
  374. package/dist/linear.d.ts +11 -0
  375. package/dist/linear.d.ts.map +1 -1
  376. package/dist/linear.js.map +1 -1
  377. package/dist/local-git-metadata.d.ts +5 -2
  378. package/dist/local-git-metadata.d.ts.map +1 -1
  379. package/dist/local-git-metadata.js +8 -5
  380. package/dist/local-git-metadata.js.map +1 -1
  381. package/dist/multiscan.d.ts +10 -4
  382. package/dist/multiscan.d.ts.map +1 -1
  383. package/dist/multiscan.js +217 -102
  384. package/dist/multiscan.js.map +1 -1
  385. package/dist/native-json-findings.d.ts +2 -1
  386. package/dist/native-json-findings.d.ts.map +1 -1
  387. package/dist/native-json-findings.js +12 -2
  388. package/dist/native-json-findings.js.map +1 -1
  389. package/dist/opengrep-findings.d.ts +5 -2
  390. package/dist/opengrep-findings.d.ts.map +1 -1
  391. package/dist/opengrep-findings.js +54 -33
  392. package/dist/opengrep-findings.js.map +1 -1
  393. package/dist/operation-telemetry.d.ts +104 -0
  394. package/dist/operation-telemetry.d.ts.map +1 -0
  395. package/dist/operation-telemetry.js +293 -0
  396. package/dist/operation-telemetry.js.map +1 -0
  397. package/dist/plugin-python-probe.d.ts +4 -0
  398. package/dist/plugin-python-probe.d.ts.map +1 -0
  399. package/dist/plugin-python-probe.js +10 -0
  400. package/dist/plugin-python-probe.js.map +1 -0
  401. package/dist/private-finding-import.d.ts +99 -9
  402. package/dist/private-finding-import.d.ts.map +1 -1
  403. package/dist/private-finding-import.js +116 -152
  404. package/dist/private-finding-import.js.map +1 -1
  405. package/dist/project-config.d.ts.map +1 -1
  406. package/dist/project-config.js +53 -13
  407. package/dist/project-config.js.map +1 -1
  408. package/dist/prompt-files.d.ts +7 -2
  409. package/dist/prompt-files.d.ts.map +1 -1
  410. package/dist/prompt-files.js +42 -20
  411. package/dist/prompt-files.js.map +1 -1
  412. package/dist/provider-profile.d.ts +1 -1
  413. package/dist/provider-profile.d.ts.map +1 -1
  414. package/dist/provider-profile.js +12 -10
  415. package/dist/provider-profile.js.map +1 -1
  416. package/dist/publication-telemetry.d.ts +45 -0
  417. package/dist/publication-telemetry.d.ts.map +1 -0
  418. package/dist/publication-telemetry.js +50 -0
  419. package/dist/publication-telemetry.js.map +1 -0
  420. package/dist/publication.d.ts +1 -1
  421. package/dist/publication.d.ts.map +1 -1
  422. package/dist/publication.js.map +1 -1
  423. package/dist/publish.d.ts +4 -0
  424. package/dist/publish.d.ts.map +1 -1
  425. package/dist/publish.js +42 -2
  426. package/dist/publish.js.map +1 -1
  427. package/dist/record.d.ts +1 -5
  428. package/dist/record.d.ts.map +1 -1
  429. package/dist/record.js +3 -51
  430. package/dist/record.js.map +1 -1
  431. package/dist/remediation.d.ts +1 -4
  432. package/dist/remediation.d.ts.map +1 -1
  433. package/dist/remediation.js +6 -8
  434. package/dist/remediation.js.map +1 -1
  435. package/dist/request-metadata.d.ts +3 -0
  436. package/dist/request-metadata.d.ts.map +1 -1
  437. package/dist/request-metadata.js +9 -0
  438. package/dist/request-metadata.js.map +1 -1
  439. package/dist/result.d.ts +4 -0
  440. package/dist/result.d.ts.map +1 -1
  441. package/dist/result.js +1 -1
  442. package/dist/result.js.map +1 -1
  443. package/dist/retry-windows-rename.d.ts +8 -0
  444. package/dist/retry-windows-rename.d.ts.map +1 -0
  445. package/dist/retry-windows-rename.js +24 -0
  446. package/dist/retry-windows-rename.js.map +1 -0
  447. package/dist/runtime.d.ts +54 -6
  448. package/dist/runtime.d.ts.map +1 -1
  449. package/dist/runtime.js +1084 -256
  450. package/dist/runtime.js.map +1 -1
  451. package/dist/saved-scan-bootstrap.d.ts +7 -1
  452. package/dist/saved-scan-bootstrap.d.ts.map +1 -1
  453. package/dist/saved-scan-bootstrap.js +269 -114
  454. package/dist/saved-scan-bootstrap.js.map +1 -1
  455. package/dist/scan-activity.d.ts.map +1 -1
  456. package/dist/scan-activity.js +141 -56
  457. package/dist/scan-activity.js.map +1 -1
  458. package/dist/scan-archive-path.d.ts +2 -0
  459. package/dist/scan-archive-path.d.ts.map +1 -0
  460. package/dist/scan-archive-path.js +5 -0
  461. package/dist/scan-archive-path.js.map +1 -0
  462. package/dist/scan-comparison.d.ts +10 -5
  463. package/dist/scan-comparison.d.ts.map +1 -1
  464. package/dist/scan-comparison.js +66 -17
  465. package/dist/scan-comparison.js.map +1 -1
  466. package/dist/scan-dashboard.d.ts +7 -6
  467. package/dist/scan-dashboard.d.ts.map +1 -1
  468. package/dist/scan-dashboard.js +92 -43
  469. package/dist/scan-dashboard.js.map +1 -1
  470. package/dist/scan-executor.d.ts +10 -1
  471. package/dist/scan-executor.d.ts.map +1 -1
  472. package/dist/scan-executor.js +24 -2
  473. package/dist/scan-executor.js.map +1 -1
  474. package/dist/scan-history-renderer.d.ts +1 -0
  475. package/dist/scan-history-renderer.d.ts.map +1 -1
  476. package/dist/scan-history-renderer.js +34 -28
  477. package/dist/scan-history-renderer.js.map +1 -1
  478. package/dist/scan-logs.d.ts +12 -0
  479. package/dist/scan-logs.d.ts.map +1 -1
  480. package/dist/scan-logs.js +131 -78
  481. package/dist/scan-logs.js.map +1 -1
  482. package/dist/scan-measurement.d.ts +26 -0
  483. package/dist/scan-measurement.d.ts.map +1 -0
  484. package/dist/scan-measurement.js +77 -0
  485. package/dist/scan-measurement.js.map +1 -0
  486. package/dist/scan-progress.d.ts +1 -0
  487. package/dist/scan-progress.d.ts.map +1 -1
  488. package/dist/scan-progress.js +3 -0
  489. package/dist/scan-progress.js.map +1 -1
  490. package/dist/scan-sessions.d.ts.map +1 -1
  491. package/dist/scan-sessions.js +4 -1
  492. package/dist/scan-sessions.js.map +1 -1
  493. package/dist/scan-settings.d.ts +1 -1
  494. package/dist/scan-settings.d.ts.map +1 -1
  495. package/dist/scan-settings.js +1 -24
  496. package/dist/scan-settings.js.map +1 -1
  497. package/dist/scanner-import.d.ts +25 -13
  498. package/dist/scanner-import.d.ts.map +1 -1
  499. package/dist/scanner-import.js +216 -97
  500. package/dist/scanner-import.js.map +1 -1
  501. package/dist/scanner-repository-url.d.ts +4 -1
  502. package/dist/scanner-repository-url.d.ts.map +1 -1
  503. package/dist/scanner-repository-url.js +10 -6
  504. package/dist/scanner-repository-url.js.map +1 -1
  505. package/dist/security-policy.d.ts +1 -1
  506. package/dist/security-policy.d.ts.map +1 -1
  507. package/dist/security-policy.js +26 -6
  508. package/dist/security-policy.js.map +1 -1
  509. package/dist/semgrep-findings.d.ts +6 -3
  510. package/dist/semgrep-findings.d.ts.map +1 -1
  511. package/dist/semgrep-findings.js +72 -42
  512. package/dist/semgrep-findings.js.map +1 -1
  513. package/dist/semgrep-guardian-findings.d.ts +38 -0
  514. package/dist/semgrep-guardian-findings.d.ts.map +1 -0
  515. package/dist/semgrep-guardian-findings.js +165 -0
  516. package/dist/semgrep-guardian-findings.js.map +1 -0
  517. package/dist/semgrep-platform-collector.d.ts +26 -0
  518. package/dist/semgrep-platform-collector.d.ts.map +1 -0
  519. package/dist/semgrep-platform-collector.js +108 -0
  520. package/dist/semgrep-platform-collector.js.map +1 -0
  521. package/dist/semgrep-platform-findings.d.ts +157 -6
  522. package/dist/semgrep-platform-findings.d.ts.map +1 -1
  523. package/dist/semgrep-platform-findings.js +766 -103
  524. package/dist/semgrep-platform-findings.js.map +1 -1
  525. package/dist/server/api.d.ts +1 -1
  526. package/dist/server/api.d.ts.map +1 -1
  527. package/dist/server/api.js.map +1 -1
  528. package/dist/server/embedding-tokenizer.cdx.json +33 -0
  529. package/dist/server/embedding-tokenizer.d.ts +2 -0
  530. package/dist/server/embedding-tokenizer.d.ts.map +1 -0
  531. package/dist/server/embedding-tokenizer.js +322 -0
  532. package/dist/server/embedding-tokenizer.js.map +6 -0
  533. package/dist/server/embeddings.d.ts +16 -2
  534. package/dist/server/embeddings.d.ts.map +1 -1
  535. package/dist/server/embeddings.js +35 -5
  536. package/dist/server/embeddings.js.map +1 -1
  537. package/dist/session-lines.d.ts +5 -0
  538. package/dist/session-lines.d.ts.map +1 -0
  539. package/dist/session-lines.js +60 -0
  540. package/dist/session-lines.js.map +1 -0
  541. package/dist/severity-review.d.ts +3 -1
  542. package/dist/severity-review.d.ts.map +1 -1
  543. package/dist/severity-review.js +3 -2
  544. package/dist/severity-review.js.map +1 -1
  545. package/dist/skill-authentication.d.ts +1 -0
  546. package/dist/skill-authentication.d.ts.map +1 -1
  547. package/dist/skill-authentication.js +1 -0
  548. package/dist/skill-authentication.js.map +1 -1
  549. package/dist/snyk-findings.d.ts +9 -2
  550. package/dist/snyk-findings.d.ts.map +1 -1
  551. package/dist/snyk-findings.js +90 -70
  552. package/dist/snyk-findings.js.map +1 -1
  553. package/dist/snyk-rest-findings.d.ts +59 -0
  554. package/dist/snyk-rest-findings.d.ts.map +1 -0
  555. package/dist/snyk-rest-findings.js +879 -0
  556. package/dist/snyk-rest-findings.js.map +1 -0
  557. package/dist/snyk-token-origin.d.ts +4 -0
  558. package/dist/snyk-token-origin.d.ts.map +1 -0
  559. package/dist/snyk-token-origin.js +82 -0
  560. package/dist/snyk-token-origin.js.map +1 -0
  561. package/dist/sonar-collector.d.ts.map +1 -1
  562. package/dist/sonar-collector.js +0 -3
  563. package/dist/sonar-collector.js.map +1 -1
  564. package/dist/sonar-findings.d.ts +1 -1
  565. package/dist/sonar-findings.d.ts.map +1 -1
  566. package/dist/sonar-findings.js +2 -4
  567. package/dist/sonar-findings.js.map +1 -1
  568. package/dist/sonar-import.d.ts +3 -0
  569. package/dist/sonar-import.d.ts.map +1 -1
  570. package/dist/sonar-import.js +30 -34
  571. package/dist/sonar-import.js.map +1 -1
  572. package/dist/stored-finding-projection.d.ts +6 -0
  573. package/dist/stored-finding-projection.d.ts.map +1 -0
  574. package/dist/stored-finding-projection.js +104 -0
  575. package/dist/stored-finding-projection.js.map +1 -0
  576. package/dist/stored-findings-publication.d.ts +23 -0
  577. package/dist/stored-findings-publication.d.ts.map +1 -0
  578. package/dist/stored-findings-publication.js +169 -0
  579. package/dist/stored-findings-publication.js.map +1 -0
  580. package/dist/target-drift.d.ts +9 -0
  581. package/dist/target-drift.d.ts.map +1 -0
  582. package/dist/target-drift.js +27 -0
  583. package/dist/target-drift.js.map +1 -0
  584. package/dist/targets.d.ts +8 -1
  585. package/dist/targets.d.ts.map +1 -1
  586. package/dist/targets.js +131 -42
  587. package/dist/targets.js.map +1 -1
  588. package/dist/tenable-findings.d.ts +6 -0
  589. package/dist/tenable-findings.d.ts.map +1 -0
  590. package/dist/tenable-findings.js +122 -0
  591. package/dist/tenable-findings.js.map +1 -0
  592. package/dist/terminal-stream.d.ts +13 -0
  593. package/dist/terminal-stream.d.ts.map +1 -0
  594. package/dist/terminal-stream.js +35 -0
  595. package/dist/terminal-stream.js.map +1 -0
  596. package/dist/trivy-findings.d.ts +19 -0
  597. package/dist/trivy-findings.d.ts.map +1 -0
  598. package/dist/trivy-findings.js +424 -0
  599. package/dist/trivy-findings.js.map +1 -0
  600. package/dist/trivy-import.d.ts +19 -0
  601. package/dist/trivy-import.d.ts.map +1 -0
  602. package/dist/trivy-import.js +151 -0
  603. package/dist/trivy-import.js.map +1 -0
  604. package/dist/trusted-executable.d.ts +6 -0
  605. package/dist/trusted-executable.d.ts.map +1 -1
  606. package/dist/trusted-executable.js +140 -37
  607. package/dist/trusted-executable.js.map +1 -1
  608. package/dist/validation-records.js +1 -1
  609. package/dist/validation-records.js.map +1 -1
  610. package/dist/veracode-findings.d.ts +1 -1
  611. package/dist/veracode-findings.d.ts.map +1 -1
  612. package/dist/veracode-findings.js +19 -6
  613. package/dist/veracode-findings.js.map +1 -1
  614. package/dist/version.d.ts +3 -1
  615. package/dist/version.d.ts.map +1 -1
  616. package/dist/version.js +184 -32
  617. package/dist/version.js.map +1 -1
  618. package/dist/vertex-pricing.d.ts +8 -0
  619. package/dist/vertex-pricing.d.ts.map +1 -0
  620. package/dist/vertex-pricing.js +20 -0
  621. package/dist/vertex-pricing.js.map +1 -0
  622. package/dist/windows-path.d.ts +1 -0
  623. package/dist/windows-path.d.ts.map +1 -1
  624. package/dist/windows-path.js +26 -1
  625. package/dist/windows-path.js.map +1 -1
  626. package/dist/wiz-findings.d.ts +6 -2
  627. package/dist/wiz-findings.d.ts.map +1 -1
  628. package/dist/wiz-findings.js +65 -89
  629. package/dist/wiz-findings.js.map +1 -1
  630. package/docs/classify-severity-records.md +27 -12
  631. package/docs/cli.md +853 -639
  632. package/docs/dedupe-records.md +44 -24
  633. package/docs/findings-service.md +84 -44
  634. package/docs/hosted-remediation.md +27 -11
  635. package/docs/hosted-scans.md +50 -42
  636. package/docs/sdk.md +903 -0
  637. package/docs/telemetry.md +236 -0
  638. package/docs/validate-records.md +78 -44
  639. package/package.json +23 -16
  640. package/schemas/external-findings.schema.json +4 -1
  641. package/_bundled_plugin/mcp/helpers.mjs.br.part-000 +0 -0
  642. package/_bundled_plugin/mcp/helpers.mjs.br.part-001 +0 -0
  643. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  644. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  645. package/_bundled_plugin/mcp/server.mjs.br.part-002 +0 -0
  646. package/_bundled_plugin/schemas/tools/deep-reducer.schema.json +0 -96
  647. package/dist/external-imports-feature.d.ts +0 -6
  648. package/dist/external-imports-feature.d.ts.map +0 -1
  649. package/dist/external-imports-feature.js +0 -51
  650. package/dist/external-imports-feature.js.map +0 -1
  651. package/dist/finding-normalization.d.ts +0 -5
  652. package/dist/finding-normalization.d.ts.map +0 -1
  653. package/dist/finding-normalization.js +0 -155
  654. package/dist/finding-normalization.js.map +0 -1
  655. package/dist/reader-selection.d.ts +0 -13
  656. package/dist/reader-selection.d.ts.map +0 -1
  657. package/dist/reader-selection.js +0 -81
  658. package/dist/reader-selection.js.map +0 -1
@@ -54,8 +54,14 @@ from finalize_scan_contract import (
54
54
  open_scan_local_file_descriptor,
55
55
  )
56
56
  from finding_preview import bounded_finding_details
57
+ from windows_scan_local_files import pin_replay_metadata
57
58
  from workbench import handoff
58
- from workbench.storage import create_private_directory, resolve_scan_root, state_dir
59
+ from workbench.storage import (
60
+ create_private_directory,
61
+ resolve_scan_root,
62
+ saved_deep_workers,
63
+ state_dir,
64
+ )
59
65
  from workbench_cli import parse_args
60
66
  from workbench_constants import (
61
67
  ARTIFACTS,
@@ -63,15 +69,14 @@ from workbench_constants import (
63
69
  DELIVERED_ACTION_LEASE_SECONDS,
64
70
  FINDING_ABSOLUTE_PATH_BYTES,
65
71
  FINDING_LEVEL_BYTES,
72
+ FINDING_LOCATION_ORDER_SQL,
66
73
  FINDING_LOCATION_PATH_BYTES,
67
74
  FINDING_LOCATION_ROLE_BYTES,
68
75
  FINDING_LOCATIONS_LIMIT,
69
76
  FINDING_REMEDIATION_BYTES,
70
77
  FINDING_SUMMARY_BYTES,
71
78
  FINDING_TITLE_BYTES,
72
- FINDINGS_PAGE_MAX,
73
79
  FINDINGS_RESULT_LIMIT,
74
- PATCH_PREVIEW_BYTES,
75
80
  SQLITE_RETRY_ATTEMPTS,
76
81
  )
77
82
  from workbench_feedback import get_scan_feedback
@@ -80,6 +85,10 @@ from workbench_finding_workflows import finding_workflow, register_workflow_scan
80
85
  from workbench_scan_start import (
81
86
  archive_scan,
82
87
  insert_running_scan,
88
+ loaded_plugin_version,
89
+ prepare_cli_scan_target,
90
+ register_windows_resume_output,
91
+ restore_cli_scan_archive,
83
92
  safe_segment,
84
93
  scan_diff_identity,
85
94
  scan_target_identity,
@@ -87,6 +96,8 @@ from workbench_scan_start import (
87
96
  )
88
97
  from workbench_schema import (
89
98
  MIGRATIONS,
99
+ migration_target_paths,
100
+ recorded_git_checkout_roots,
90
101
  )
91
102
  from workbench_schema import (
92
103
  apply_migrations as apply_schema_migrations,
@@ -111,6 +122,7 @@ from workbench_source_scopes import (
111
122
  safe_source_path,
112
123
  )
113
124
  from workbench_target import (
125
+ checked_scan_target,
114
126
  clean_worktree_content_digest,
115
127
  copy_directory_excluding,
116
128
  copy_git_worktree_files,
@@ -124,21 +136,31 @@ from workbench_target import (
124
136
  git_submodule_entries,
125
137
  git_target_metadata,
126
138
  git_worktree_context,
139
+ rejected_checkout_roots,
127
140
  remediation_checkout_snapshot,
128
141
  require_remediation_target,
129
142
  require_scan_target_identity,
130
143
  require_scan_target_unchanged,
131
144
  restore_directory_junctions,
132
145
  scan_target_warning,
146
+ scoped_git_discovery,
147
+ scoped_regular_file_count,
133
148
  worktree_content_digest,
134
149
  worktree_content_digest_for_context,
135
150
  )
136
- from workbench_target_state import backfill_security_targets, ensure_security_target
151
+ from workbench_target_state import (
152
+ RepositoryScanScope,
153
+ backfill_security_targets,
154
+ ensure_security_target,
155
+ register_security_target,
156
+ require_scan_checkout_owner,
157
+ )
137
158
  from workbench_validation import (
138
159
  bounded_output_text,
139
160
  optional_text,
140
161
  parse_scan_cost,
141
162
  path_within_scope,
163
+ read_json_object,
142
164
  reject_non_finite_json,
143
165
  require_close_note,
144
166
  require_occurrence,
@@ -168,6 +190,7 @@ def database_path() -> Path:
168
190
 
169
191
  @contextmanager
170
192
  def scan_completion_lock(scan_id: str) -> Any:
193
+ """Hold the cross-process lock for a scan ID until the context exits."""
171
194
  lock_dir = state_dir() / "completion-locks"
172
195
  create_private_directory(lock_dir)
173
196
  lock_path = lock_dir / f"{require_uuid(scan_id, 'scan-id')}.lock"
@@ -230,7 +253,29 @@ def release_completion_file_lock(descriptor: int) -> None:
230
253
  windows_file_lock.locking(descriptor, windows_file_lock.LK_UNLCK, 1)
231
254
 
232
255
 
233
- def connect(*, deferred: bool = False) -> sqlite3.Connection:
256
+ def connect(
257
+ *,
258
+ deferred: bool = False,
259
+ discovery_roots: tuple[Path, ...] = (),
260
+ in_memory: bool = False,
261
+ ) -> sqlite3.Connection:
262
+ """Open the workbench SQLite database and bring its schema up to date.
263
+
264
+ Args:
265
+ deferred (bool): Let the first migration attempt defer its write lock.
266
+ Retries after lock contention use an immediate transaction.
267
+
268
+ discovery_roots: Additional local paths for bounded target discovery.
269
+ in_memory: Keep a fresh profile read entirely in memory.
270
+
271
+ Returns:
272
+ sqlite3.Connection: A caller-owned connection with named rows, foreign
273
+ keys enabled, and write-ahead logging.
274
+
275
+ Raises:
276
+ sqlite3.OperationalError: Opening or configuring the database fails, or
277
+ lock contention persists after retries.
278
+ """
234
279
  path = database_path()
235
280
  guidance = (
236
281
  f"Workbench database: {path}. Ensure the state directory and SQLite journal files "
@@ -238,13 +283,14 @@ def connect(*, deferred: bool = False) -> sqlite3.Connection:
238
283
  "the scanned repository."
239
284
  )
240
285
  try:
241
- create_private_directory(path.parent)
286
+ if not in_memory:
287
+ create_private_directory(path.parent)
242
288
  except OSError:
243
289
  print(guidance, file=sys.stderr)
244
290
  raise
245
291
  for attempt in range(SQLITE_RETRY_ATTEMPTS):
246
292
  try:
247
- connection = sqlite3.connect(path, timeout=5)
293
+ connection = sqlite3.connect(":memory:" if in_memory else path, timeout=5)
248
294
  except sqlite3.OperationalError:
249
295
  print(guidance, file=sys.stderr)
250
296
  raise
@@ -253,9 +299,12 @@ def connect(*, deferred: bool = False) -> sqlite3.Connection:
253
299
  connection.execute("PRAGMA foreign_keys = ON")
254
300
  connection.execute("PRAGMA busy_timeout = 5000")
255
301
  # Keep writer admission retries; readers only wait when a repair needs to write.
256
- apply_migrations(connection, immediate=not deferred or attempt > 0)
302
+ apply_migrations(
303
+ connection, immediate=not deferred or attempt > 0, discovery_roots=discovery_roots
304
+ )
257
305
  connection.execute("PRAGMA journal_mode = WAL")
258
- path.chmod(0o600)
306
+ if not in_memory:
307
+ path.chmod(0o600)
259
308
  return connection
260
309
  except sqlite3.OperationalError as exc:
261
310
  connection.close()
@@ -266,9 +315,30 @@ def connect(*, deferred: bool = False) -> sqlite3.Connection:
266
315
  raise AssertionError("SQLite retry loop exhausted unexpectedly.")
267
316
 
268
317
 
269
- def apply_migrations(connection: sqlite3.Connection, *, immediate: bool = False) -> None:
318
+ def begin_immediate_with_retry(connection: sqlite3.Connection) -> None:
319
+ for attempt in range(SQLITE_RETRY_ATTEMPTS):
320
+ try:
321
+ connection.execute("BEGIN IMMEDIATE")
322
+ return
323
+ except sqlite3.OperationalError as exc:
324
+ if attempt == SQLITE_RETRY_ATTEMPTS - 1 or not sqlite_busy(exc):
325
+ raise
326
+ time.sleep(0.05 * (2**attempt))
327
+
328
+
329
+ def apply_migrations(
330
+ connection: sqlite3.Connection,
331
+ *,
332
+ immediate: bool = False,
333
+ discovery_roots: tuple[Path, ...] = (),
334
+ ) -> None:
270
335
  apply_schema_migrations(
271
- connection, MIGRATIONS, now, backfill_security_targets, immediate=immediate
336
+ connection,
337
+ MIGRATIONS,
338
+ now,
339
+ backfill_security_targets,
340
+ immediate=immediate,
341
+ discovery_roots=discovery_roots,
272
342
  )
273
343
 
274
344
 
@@ -282,11 +352,13 @@ def requested_scan_file_paths(scan: sqlite3.Row) -> list[str]:
282
352
 
283
353
 
284
354
  def requested_finding_scope(scan: sqlite3.Row) -> list[tuple[str, bool]]:
355
+ """Return requested paths paired with whether the stored recipe identified each one as a file."""
285
356
  files = {PurePosixPath(path) for path in requested_scan_file_paths(scan)}
286
357
  return [(path, PurePosixPath(path) in files) for path in requested_scan_paths(scan)]
287
358
 
288
359
 
289
360
  def scan_contract(scan: sqlite3.Row) -> dict[str, Any]:
361
+ """Return the target identity, scope requirements, and diff target recorded for this scan."""
290
362
  target = Path(scan["target_path"])
291
363
  target_contract = {
292
364
  "allowedKinds": expected_target_kinds(scan),
@@ -318,36 +390,16 @@ def scan_contract(scan: sqlite3.Row) -> dict[str, Any]:
318
390
  }
319
391
 
320
392
 
321
- def expected_coverage_mode(scan: sqlite3.Row) -> str:
322
- if scan["mode"] == "diff":
323
- mode = {
324
- "commit": "commit",
325
- "range": "branch_diff",
326
- "working_tree": "working_tree",
327
- }.get(scan["diff_target_kind"])
328
- if mode is None:
329
- raise SystemExit("This migrated diff scan does not have a validated change set.")
330
- return mode
331
- if scan["scope"] != "." or (
332
- "recipe_json" in scan.keys()
333
- and scan["recipe_json"] is not None
334
- and json.loads(scan["recipe_json"])["target"]["kind"] == "paths"
335
- ):
336
- return "scoped_path"
337
- return "deep_repository" if scan["mode"] == "deep" else "repository"
393
+ expected_coverage_mode = scan_history.expected_coverage_mode
338
394
 
339
395
 
340
396
  def workbench_completion_binding(
341
397
  scan: sqlite3.Row, completed_at: str, manifest: dict[str, Any] | None = None
342
398
  ) -> dict[str, Any]:
399
+ """Build the scan ID, timestamps, producer, target, and scope used to bind output artifacts at completion."""
343
400
  contract = scan_contract(scan)
344
401
  target_contract = contract["target"]
345
- plugin_manifest = read_json_object(
346
- Path(__file__).resolve().parent.parent / ".codex-plugin" / "plugin.json"
347
- )
348
- plugin_version = plugin_manifest.get("version")
349
- if not isinstance(plugin_version, str) or not plugin_version:
350
- raise SystemExit("plugin.json: expected a nonempty Codex Security plugin version.")
402
+ plugin_version = loaded_plugin_version()
351
403
  target: dict[str, Any] = {
352
404
  "targetId": target_contract["targetId"],
353
405
  "displayName": target_contract["displayName"],
@@ -382,6 +434,7 @@ def workbench_completion_binding(
382
434
 
383
435
 
384
436
  def verify_manifest_binding(scan: sqlite3.Row, manifest: dict[str, Any]) -> None:
437
+ """Raise SystemExit if the supplied manifest does not match the registered scan, target, or scope."""
385
438
  manifest_scan = manifest.get("scan")
386
439
  if not isinstance(manifest_scan, dict):
387
440
  raise SystemExit("scan-manifest.json scan must be an object.")
@@ -469,6 +522,10 @@ def require_scan(connection: sqlite3.Connection, scan_id: str) -> sqlite3.Row:
469
522
 
470
523
 
471
524
  def resolve_scan_id(connection: sqlite3.Connection, scan_id: str) -> str:
525
+ """Return a canonical UUID or resolve a unique scan ID prefix of at least eight characters.
526
+
527
+ Raise SystemExit for a prefix that is too short, ambiguous, or absent from the database.
528
+ """
472
529
  try:
473
530
  return str(uuid.UUID(scan_id))
474
531
  except ValueError:
@@ -504,6 +561,8 @@ def create_workspace(connection: sqlite3.Connection, args: argparse.Namespace) -
504
561
  diff_content_digest = (
505
562
  optional_text(args.diff_content_digest, maximum=128) if args.mode == "diff" else None
506
563
  )
564
+ target_path_unvalidated = False
565
+ rejected_roots = None
507
566
  if target_path:
508
567
  try:
509
568
  inspected = inspect_setup_values(
@@ -523,11 +582,17 @@ def create_workspace(connection: sqlite3.Connection, args: argparse.Namespace) -
523
582
  diff_head_revision = inspected["diffTarget"]["headRevision"]
524
583
  diff_content_digest = inspected["diffTarget"].get("contentDigest")
525
584
  except SystemExit:
526
- # Preserve an incomplete setup so workspace_state can report its validation error.
527
- pass
585
+ # Setup may fail on scope even when the target itself is valid.
586
+ try:
587
+ require_target(target_path)
588
+ except SystemExit:
589
+ target_path_unvalidated = True
590
+ rejected_roots = rejected_checkout_roots(target_path)
528
591
  with connection:
529
592
  target_id = (
530
- ensure_security_target(connection, target_path) if target_path is not None else None
593
+ ensure_security_target(connection, target_path)
594
+ if target_path is not None and not target_path_unvalidated
595
+ else None
531
596
  )
532
597
  connection.execute(
533
598
  """
@@ -535,8 +600,8 @@ def create_workspace(connection: sqlite3.Connection, args: argparse.Namespace) -
535
600
  id, thread_id, target_id, target_path, target_title, target_summary,
536
601
  default_scope, default_mode,
537
602
  user_context, diff_target_kind, diff_base_revision, diff_head_revision,
538
- diff_content_digest, created_at, updated_at
539
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
603
+ diff_content_digest, created_at, updated_at, target_path_unvalidated, rejected_checkout_roots_json
604
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
540
605
  """,
541
606
  (
542
607
  workspace_id,
@@ -554,6 +619,8 @@ def create_workspace(connection: sqlite3.Connection, args: argparse.Namespace) -
554
619
  diff_content_digest,
555
620
  timestamp,
556
621
  timestamp,
622
+ target_path_unvalidated,
623
+ json.dumps(rejected_roots) if rejected_roots is not None else None,
557
624
  ),
558
625
  )
559
626
  return workspace_state(connection, workspace_id)
@@ -588,7 +655,10 @@ def save_workspace(connection: sqlite3.Connection, args: argparse.Namespace) ->
588
655
  SET target_id = ?, target_path = ?, target_title = ?, target_summary = ?, default_scope = ?,
589
656
  default_mode = ?, user_context = ?, diff_target_kind = ?,
590
657
  diff_base_revision = ?, diff_head_revision = ?, diff_content_digest = ?,
591
- deep_scan_config_json = ?, submitted = 1, updated_at = ?
658
+ deep_scan_config_json = ?, submitted = 1, updated_at = ?, target_path_unvalidated = 0,
659
+ -- JSON null keeps PATH discovery disabled after a legacy rejection.
660
+ rejected_checkout_roots_json = COALESCE(rejected_checkout_roots_json,
661
+ CASE WHEN target_path_unvalidated = 1 AND target_id IS NULL THEN 'null' END)
592
662
  WHERE id = ? AND active_scan_id IS NULL
593
663
  """,
594
664
  (
@@ -614,7 +684,9 @@ def save_workspace(connection: sqlite3.Connection, args: argparse.Namespace) ->
614
684
 
615
685
  def scan_target_root(scan_root: str | None, target: Path) -> Path:
616
686
  root = resolve_scan_root(scan_root)
617
- target_root = (root / safe_segment(target.name)).resolve()
687
+ target_root = root / safe_segment(target.name)
688
+ if os.name != "nt":
689
+ target_root = target_root.resolve()
618
690
  if target_root == target or target in target_root.parents:
619
691
  raise SystemExit("The scan artifact directory must be outside the selected target.")
620
692
  return target_root
@@ -637,7 +709,8 @@ def require_working_tree_unchanged(target: Path, diff_target: dict[str, str] | N
637
709
  and diff_target["kind"] == "working_tree"
638
710
  and (
639
711
  git_revision(target) != diff_target["headRevision"]
640
- or worktree_content_digest(target) != diff_target["contentDigest"]
712
+ or worktree_content_digest(target, expected_digest=diff_target["contentDigest"])
713
+ != diff_target["contentDigest"]
641
714
  )
642
715
  ):
643
716
  raise SystemExit("The selected scan target changed while the scan was starting. Try again.")
@@ -659,7 +732,8 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
659
732
  (workspace["id"],),
660
733
  ).fetchone()
661
734
  if active is not None:
662
- return workspace_state(connection, workspace["id"])
735
+ require_scan_checkout_owner(connection, active)
736
+ return workspace_state(connection, workspace["id"], result_scan_id=active["id"])
663
737
  workspace_version = workspace["updated_at"]
664
738
  scan_id = str(uuid.uuid4())
665
739
  timestamp = now()
@@ -694,7 +768,7 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
694
768
  diff_target_kind=diff_target["kind"] if diff_target is not None else None,
695
769
  )
696
770
  target_root = scan_target_root(args.scan_root, target)
697
- create_private_directory(target_root)
771
+ create_private_directory(target_root, scan_output=True)
698
772
  require_working_tree_unchanged(target, diff_target)
699
773
  if manages_transaction:
700
774
  connection.execute("BEGIN IMMEDIATE")
@@ -708,9 +782,10 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
708
782
  (workspace["id"],),
709
783
  ).fetchone()
710
784
  if active is not None:
785
+ require_scan_checkout_owner(connection, active)
711
786
  if manages_transaction:
712
787
  connection.commit()
713
- return workspace_state(connection, workspace["id"])
788
+ return workspace_state(connection, workspace["id"], result_scan_id=active["id"])
714
789
  if workspace["updated_at"] != workspace_version:
715
790
  raise SystemExit("Codex Security setup changed while the scan was starting. Try again.")
716
791
  current_target = require_remediation_target(str(target))
@@ -734,6 +809,11 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
734
809
  "This Codex thread already has an active Deep Scan for the selected "
735
810
  "target and scope. Rejoin that scan instead of starting another one."
736
811
  )
812
+ registration = register_security_target(connection, str(current_target))
813
+ if registration.target_id != workspace["target_id"]:
814
+ raise SystemExit(
815
+ "The saved workspace no longer matches the selected repository target."
816
+ )
737
817
  insert_running_scan(
738
818
  connection,
739
819
  scan_id=scan_id,
@@ -742,6 +822,7 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
742
822
  scope=scope,
743
823
  diff_target=diff_target,
744
824
  target_identity=target_identity,
825
+ repository_generation=registration.repository_generation,
745
826
  source_scopes=source_scopes,
746
827
  target_root=target_root,
747
828
  target_summary=target_summary,
@@ -756,7 +837,7 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
756
837
  if manages_transaction:
757
838
  connection.rollback()
758
839
  raise
759
- return workspace_state(connection, workspace["id"])
840
+ return workspace_state(connection, workspace["id"], result_scan_id=scan_id)
760
841
 
761
842
 
762
843
  def _start_prompt_driven_scan(
@@ -828,7 +909,8 @@ def _start_prompt_driven_scan(
828
909
  """
829
910
  SELECT scans.* FROM scans
830
911
  JOIN workspaces ON workspaces.active_scan_id = scans.id
831
- WHERE workspaces.thread_id = ? AND workspaces.target_path = ?
912
+ WHERE COALESCE(scans.continuation_thread_id, workspaces.thread_id) = ?
913
+ AND scans.recipe_json IS NULL AND workspaces.target_path = ?
832
914
  AND workspaces.default_scope = ? AND workspaces.default_mode = ?
833
915
  AND workspaces.user_context IS ? AND workspaces.target_summary IS ?
834
916
  AND workspaces.diff_target_kind IS ? AND workspaces.diff_base_revision IS ?
@@ -841,7 +923,7 @@ def _start_prompt_driven_scan(
841
923
  (? = 0 AND scans.handoff_claim_token IS NULL)
842
924
  OR (
843
925
  ? = 1 AND scans.handoff_claim_token IS NOT NULL
844
- AND scans.continuation_thread_id = ?
926
+ AND scans.continuation_thread_id = workspaces.thread_id
845
927
  )
846
928
  )
847
929
  ORDER BY scans.updated_at DESC, scans.started_at DESC, scans.id LIMIT 1
@@ -857,20 +939,21 @@ def _start_prompt_driven_scan(
857
939
  *target_identity,
858
940
  int(headless_standard),
859
941
  int(headless_standard),
860
- thread_id,
861
942
  ),
862
943
  ).fetchone()
863
944
  if existing is not None:
945
+ require_scan_checkout_owner(connection, existing)
864
946
  connection.commit()
865
947
  return {
866
- **scan_context(connection, existing["id"]),
948
+ **scan_context(connection, existing["id"], model_thread_id=thread_id),
867
949
  "startDisposition": "joined",
868
950
  }
869
- create_private_directory(target_root)
951
+ create_private_directory(target_root, scan_output=True)
870
952
  workspace_id = str(uuid.uuid4())
871
953
  scan_id = str(uuid.uuid4())
872
954
  timestamp = now()
873
- target_id = ensure_security_target(connection, target_path)
955
+ registration = register_security_target(connection, target_path)
956
+ target_id = registration.target_id
874
957
  connection.execute(
875
958
  """
876
959
  INSERT INTO workspaces (
@@ -903,6 +986,7 @@ def _start_prompt_driven_scan(
903
986
  scope=scope,
904
987
  diff_target=diff_target,
905
988
  target_identity=target_identity,
989
+ repository_generation=registration.repository_generation,
906
990
  source_scopes=source_scopes,
907
991
  target_root=target_root,
908
992
  target_summary=target_summary,
@@ -940,6 +1024,7 @@ def _start_prompt_driven_scan(
940
1024
 
941
1025
 
942
1026
  def scan_local_file_digest(scan_dir: Path, relative_path: str) -> str:
1027
+ """Return the SHA-256 of the scan-local file contents, prefixed with sha256:."""
943
1028
  digest = hashlib.sha256()
944
1029
  with open_scan_local_file(scan_dir, relative_path) as source:
945
1030
  for chunk in iter(lambda: source.read(1024 * 1024), b""):
@@ -948,6 +1033,7 @@ def scan_local_file_digest(scan_dir: Path, relative_path: str) -> str:
948
1033
 
949
1034
 
950
1035
  def published_manifest_digest(scan_dir: Path, manifest: dict[str, Any]) -> str:
1036
+ """Return the canonical manifest digest or raise SystemExit if the published bytes differ."""
951
1037
  canonical = (json.dumps(manifest, allow_nan=False, indent=2, sort_keys=True) + "\n").encode()
952
1038
  expected = f"sha256:{hashlib.sha256(canonical).hexdigest()}"
953
1039
  actual = scan_local_file_digest(scan_dir, ARTIFACTS["manifest"])
@@ -957,6 +1043,7 @@ def published_manifest_digest(scan_dir: Path, manifest: dict[str, Any]) -> str:
957
1043
 
958
1044
 
959
1045
  def require_recorded_manifest_digest(scan: sqlite3.Row, scan_dir: Path) -> None:
1046
+ """Raise SystemExit if the saved manifest differs from the digest recorded when the scan was sealed."""
960
1047
  expected = scan["seal_manifest_digest"]
961
1048
  if expected is None:
962
1049
  return
@@ -967,6 +1054,7 @@ def require_recorded_manifest_digest(scan: sqlite3.Row, scan_dir: Path) -> None:
967
1054
  def pin_legacy_manifest_digest(
968
1055
  connection: sqlite3.Connection, scan_id: str, manifest_digest: str
969
1056
  ) -> None:
1057
+ """Record a previously missing digest in a write transaction, rejecting a conflict with an existing digest."""
970
1058
  connection.execute("BEGIN IMMEDIATE")
971
1059
  with connection:
972
1060
  scan = require_scan(connection, scan_id)
@@ -997,9 +1085,24 @@ def complete_scan(
997
1085
  prepare_only=prepare_only,
998
1086
  thread_id=getattr(args, "thread_id", None),
999
1087
  turn_id=getattr(args, "turn_id", None),
1088
+ model_thread_id=getattr(args, "model_thread_id", None),
1000
1089
  )
1001
1090
 
1002
1091
 
1092
+ def read_scan_usage(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1093
+ # Validation can finish after the scan's recorded completion timestamp.
1094
+ usage = scan_usage.collect_scan_usage(
1095
+ connection,
1096
+ require_scan(connection, args.scan_id),
1097
+ thread_id=args.thread_id,
1098
+ validation_thread_id=args.validation_thread_id,
1099
+ completed_at=now(),
1100
+ )
1101
+ if "cacheWriteInputTokens" in usage:
1102
+ usage.setdefault("cacheWriteInputTokensReported", True)
1103
+ return usage
1104
+
1105
+
1003
1106
  def finalize_scan_usage(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1004
1107
  scan_id = require_uuid(args.scan_id, "scan-id")
1005
1108
  with scan_completion_lock(scan_id):
@@ -1012,7 +1115,7 @@ def finalize_scan_usage(connection: sqlite3.Connection, args: argparse.Namespace
1012
1115
  if completion["threadId"] != args.thread_id or completion["turnId"] != args.turn_id:
1013
1116
  raise SystemExit("The scan usage belongs to a different completion turn.")
1014
1117
  if not completion["finalized"]:
1015
- usage = scan_usage.collect_scan_usage(
1118
+ usage = scan_usage.collect_scan_usage_safely(
1016
1119
  connection,
1017
1120
  scan,
1018
1121
  thread_id=args.thread_id,
@@ -1031,7 +1134,9 @@ def finalize_scan_usage(connection: sqlite3.Connection, args: argparse.Namespace
1031
1134
  f"scan_turn_{args.turn_status}",
1032
1135
  ]
1033
1136
  usage["completion"] = {**completion, "finalized": True}
1034
- cost_json = parse_scan_cost(json.dumps({**stored, "usage": usage}))
1137
+ fields = scan_usage.merge_scan_usage_fields(stored, {"usage": usage})
1138
+ fields["usage"]["completion"] = usage["completion"]
1139
+ cost_json = parse_scan_cost(json.dumps(fields))
1035
1140
  with connection:
1036
1141
  connection.execute(
1037
1142
  "UPDATE scans SET cost_json = ? WHERE id = ?", (cost_json, scan_id)
@@ -1042,6 +1147,7 @@ def finalize_scan_usage(connection: sqlite3.Connection, args: argparse.Namespace
1042
1147
  def complete_budget_exhausted_scan(
1043
1148
  connection: sqlite3.Connection, args: argparse.Namespace
1044
1149
  ) -> dict[str, Any]:
1150
+ """Complete a running CLI Deep Scan using the measured cost and saved results after its budget is exhausted."""
1045
1151
  scan_id = require_uuid(args.scan_id, "scan-id")
1046
1152
  cost_json = parse_scan_cost(args.cost_json)
1047
1153
  if cost_json is None:
@@ -1187,19 +1293,21 @@ def complete_scan_locked(
1187
1293
  prepare_only: bool = False,
1188
1294
  thread_id: str | None = None,
1189
1295
  turn_id: str | None = None,
1296
+ model_thread_id: str | None = None,
1190
1297
  ) -> dict[str, Any]:
1191
1298
  scan = require_scan(connection, scan_id)
1192
- if scan["status"] == "complete":
1193
- return saved_results.refresh_completed_scan(
1194
- _WORKBENCH_DB_CONTEXT, connection, scan, cost_json
1195
- )
1196
- if scan["status"] != "running":
1299
+ handoff.require_tokenless_scan_owner(connection, scan, model_thread_id)
1300
+ if scan["status"] not in {"running", "complete"}:
1197
1301
  raise SystemExit("Only a running scan can be completed.")
1198
1302
  handoff.require_current_continuation(
1199
1303
  scan,
1200
1304
  claim_token,
1201
1305
  error_message="Scan completion is owned by another continuation.",
1202
1306
  )
1307
+ if scan["status"] == "complete":
1308
+ return saved_results.refresh_completed_scan(
1309
+ _WORKBENCH_DB_CONTEXT, connection, scan, cost_json
1310
+ )
1203
1311
  deep_scan.require_deep_scan_ready_for_parent_completion(connection, scan)
1204
1312
  warnings = json.loads(scan["completion_warnings_json"])
1205
1313
  target_warnings: list[str] = []
@@ -1256,10 +1364,7 @@ def complete_scan_locked(
1256
1364
  scan_dir,
1257
1365
  scan["id"],
1258
1366
  completion_binding,
1259
- connection.execute(
1260
- "SELECT * FROM deep_scan_workers WHERE scan_id = ? ORDER BY created_at, id",
1261
- (scan["id"],),
1262
- ).fetchall(),
1367
+ saved_deep_workers(connection, scan),
1263
1368
  warnings,
1264
1369
  stopped=False,
1265
1370
  scan_mode=scan["mode"],
@@ -1330,6 +1435,7 @@ def complete_scan_locked(
1330
1435
  for kind, filename in ARTIFACTS.items()
1331
1436
  }
1332
1437
  manifest_digest = published_manifest_digest(scan_dir, manifest)
1438
+ reported_count = progress.reported_scope_file_count(_WORKBENCH_DB_CONTEXT, scan)
1333
1439
  if prepare_only:
1334
1440
  connection.execute("BEGIN IMMEDIATE")
1335
1441
  with connection:
@@ -1339,46 +1445,66 @@ def complete_scan_locked(
1339
1445
  )
1340
1446
  if updated.rowcount != 1:
1341
1447
  raise SystemExit("Only a running scan can be prepared for completion.")
1448
+ if reported_count is not None:
1449
+ connection.execute(
1450
+ """
1451
+ UPDATE scan_progress SET scope_file_count = ?, updated_at = ?
1452
+ WHERE scan_id = (SELECT id FROM scans WHERE id = ? AND scan_dir = ?
1453
+ AND handoff_status = ? AND handoff_claim_token IS ?)
1454
+ """,
1455
+ (
1456
+ reported_count,
1457
+ completion_timestamp,
1458
+ scan_id,
1459
+ scan["scan_dir"],
1460
+ scan["handoff_status"],
1461
+ scan["handoff_claim_token"],
1462
+ ),
1463
+ )
1342
1464
  context = scan_context(connection, scan["id"])
1343
1465
  context["targetWarnings"] = target_warnings
1344
1466
  return context
1345
1467
 
1346
- cost_fields = scan_usage.stored_scan_cost_fields(cost_json)
1347
- if "usage" not in cost_fields:
1348
- cost_fields["usage"] = scan_usage.collect_scan_usage(
1349
- connection,
1350
- scan,
1351
- thread_id=thread_id,
1352
- completed_at=completion_timestamp,
1468
+ measured_usage = scan_usage.collect_scan_usage_safely(
1469
+ connection,
1470
+ scan,
1471
+ thread_id=thread_id,
1472
+ completed_at=completion_timestamp,
1473
+ )
1474
+ cost_fields = scan_usage.merge_scan_usage_fields(
1475
+ scan_usage.stored_scan_cost_fields(cost_json), {"usage": measured_usage}
1476
+ )
1477
+ measured_usage = cost_fields["usage"]
1478
+ if thread_id is not None and turn_id is not None:
1479
+ measured_usage["completion"] = {
1480
+ "threadId": thread_id,
1481
+ "turnId": turn_id,
1482
+ "finalized": False,
1483
+ }
1484
+ if measured_usage["coverage"] == "complete":
1485
+ measured_usage["coverage"] = "partial"
1486
+ measured_usage["warnings"] = sorted(
1487
+ {*measured_usage.get("warnings", []), "turn_in_progress"}
1353
1488
  )
1354
- measured_usage = cost_fields["usage"]
1355
- if thread_id is not None and turn_id is not None:
1356
- measured_usage["completion"] = {
1357
- "threadId": thread_id,
1358
- "turnId": turn_id,
1359
- "finalized": False,
1360
- }
1361
- if measured_usage["coverage"] == "complete":
1362
- measured_usage["coverage"] = "partial"
1363
- measured_usage["warnings"] = sorted(
1364
- {*measured_usage.get("warnings", []), "turn_in_progress"}
1365
- )
1366
1489
  cost_json = parse_scan_cost(json.dumps(cost_fields))
1367
1490
  connection.execute("BEGIN IMMEDIATE")
1368
1491
  with connection:
1369
1492
  timestamp = manifest["scan"]["completedAt"]
1370
1493
  scan = require_scan(connection, scan["id"])
1371
- if scan["status"] == "complete":
1372
- connection.commit()
1373
- return scan_context(connection, scan["id"])
1374
- if scan["status"] != "running":
1494
+ handoff.require_tokenless_scan_owner(connection, scan, model_thread_id)
1495
+ if scan["status"] not in {"running", "complete"}:
1375
1496
  raise SystemExit("Only a running scan can be completed.")
1376
- deep_scan.require_deep_scan_ready_for_parent_completion(connection, scan)
1377
1497
  handoff.require_current_continuation(
1378
1498
  scan,
1379
1499
  claim_token,
1380
1500
  error_message="Scan completion is owned by another continuation.",
1381
1501
  )
1502
+ if scan["status"] == "complete":
1503
+ connection.commit()
1504
+ return scan_context(connection, scan["id"])
1505
+ deep_scan.require_deep_scan_ready_for_parent_completion(connection, scan)
1506
+ if scan["scan_dir"] != str(scan_dir):
1507
+ reported_count = None
1382
1508
  connection.execute("DELETE FROM scan_artifacts WHERE scan_id = ?", (scan["id"],))
1383
1509
  for kind, path in artifacts.items():
1384
1510
  if path is not None:
@@ -1394,21 +1520,30 @@ def complete_scan_locked(
1394
1520
  UPDATE scan_progress
1395
1521
  SET reportable_findings_count = ?, phase_items_total = ?,
1396
1522
  phase_items_completed = ?, phase_progress_unit = 'report_artifacts',
1397
- updated_at = ?
1523
+ scope_file_count = COALESCE(?, scope_file_count), updated_at = ?
1398
1524
  WHERE scan_id = ?
1399
1525
  """,
1400
- (finding_count, len(artifacts), len(artifacts), timestamp, scan["id"]),
1526
+ (
1527
+ finding_count,
1528
+ len(artifacts),
1529
+ len(artifacts),
1530
+ reported_count,
1531
+ timestamp,
1532
+ scan["id"],
1533
+ ),
1401
1534
  )
1402
1535
  updated = connection.execute(
1403
1536
  """
1404
1537
  UPDATE scans
1405
1538
  SET status = 'complete', phase = 'reporting', completed_at = ?, updated_at = ?,
1406
- seal_manifest_digest = ?, cost_json = ?, completion_warnings_json = ?
1539
+ log_completed_at = ?, seal_manifest_digest = ?, cost_json = ?,
1540
+ completion_warnings_json = ?
1407
1541
  WHERE id = ? AND status = 'running'
1408
1542
  """,
1409
1543
  (
1410
1544
  timestamp,
1411
1545
  timestamp,
1546
+ now(),
1412
1547
  manifest_digest,
1413
1548
  cost_json,
1414
1549
  json.dumps(warnings),
@@ -1423,7 +1558,11 @@ def complete_scan_locked(
1423
1558
 
1424
1559
 
1425
1560
  def require_cli_scan_directory(repository: Path, args: argparse.Namespace) -> Path:
1426
- scan_dir = require_canonical_scan_directory(Path(args.scan_dir).expanduser())
1561
+ # Registration uses an empty output, including the replacement created with
1562
+ # the original DACL after archival. Check that ACL before moving any results.
1563
+ scan_dir = require_canonical_scan_directory(
1564
+ Path(args.scan_dir).expanduser(), writable_output=True
1565
+ )
1427
1566
  if scan_dir == repository or repository in scan_dir.parents or scan_dir in repository.parents:
1428
1567
  raise SystemExit(
1429
1568
  "The scan artifact directory must be outside and not contain the selected target."
@@ -1441,22 +1580,46 @@ def register_cli_scan(
1441
1580
  *,
1442
1581
  before_archive: Callable[[], None] | None = None,
1443
1582
  ) -> dict[str, Any]:
1583
+ """Register the scan recipe and target identity, archiving any previous output atomically."""
1584
+ # A selected plugin may replace __main__ while delegating to this workbench.
1585
+ before_output_change = before_archive or getattr(
1586
+ sys, "_codex_security_before_output_change", None
1587
+ )
1588
+
1589
+ def acknowledge_output_change() -> None:
1590
+ nonlocal before_output_change
1591
+ if before_output_change is not None:
1592
+ before_output_change()
1593
+ before_output_change = None
1594
+
1444
1595
  repository = require_target(args.repository)
1445
1596
  require_scannable_target(repository)
1446
- scan_dir = None
1447
1597
  if not args.archive_existing or args.archived_scan_dir is not None:
1448
1598
  # Preserve early output rejection when this call will not move the directory.
1449
- scan_dir = require_cli_scan_directory(repository, args)
1599
+ require_cli_scan_directory(repository, args)
1450
1600
  user_context = None
1451
1601
  workflow_id = None
1602
+ hosted = False
1603
+ surface = None
1604
+ package_version = None
1452
1605
  if args.registration_json_stdin:
1453
1606
  registration = json.load(sys.stdin)
1454
1607
  recipe_json = json.dumps(registration["recipe"], ensure_ascii=False, separators=(",", ":"))
1608
+ hosted = registration.get("hosted") is True
1455
1609
  user_context = registration.get("userContext")
1456
1610
  workflow_id = registration.get("workflowId")
1611
+ if registration.get("surface") in ("cli", "sdk"):
1612
+ surface = registration["surface"]
1613
+ if isinstance(registration.get("packageVersion"), str):
1614
+ package_version = registration["packageVersion"] or None
1457
1615
  else:
1458
1616
  recipe_json = sys.stdin.read() if args.recipe_json_stdin else args.recipe_json
1459
1617
  recipe = parse_scan_recipe(recipe_json, repository)
1618
+ launch_kind = "scan"
1619
+ if isinstance(recipe.get("import"), dict):
1620
+ launch_kind = "import"
1621
+ elif recipe.get("mock") is True:
1622
+ launch_kind = "mock"
1460
1623
  requested_target = recipe["target"]
1461
1624
  paths = requested_target["paths"]
1462
1625
  recipe.pop("_codexSecurityFileScopes", None)
@@ -1480,14 +1643,20 @@ def register_cli_scan(
1480
1643
  raise SystemExit("Working-tree HEAD changed before the scan started.")
1481
1644
  diff_target["contentDigest"] = worktree_content_digest(repository)
1482
1645
  mode = "diff" if diff_target is not None else recipe["mode"]
1483
- target_identity = scan_target_identity(repository, diff_target)
1646
+ target_identity, resume_target = prepare_cli_scan_target(
1647
+ repository, diff_target, mode, launch_kind
1648
+ )
1484
1649
  source_scopes = capture_source_scopes(
1485
1650
  repository,
1486
1651
  target_identity,
1487
1652
  paths or ["."],
1488
1653
  diff_target_kind=diff_target["kind"] if diff_target is not None else None,
1489
1654
  )
1490
- scope_file_count = scan_scope_file_count(repository, paths or ["."], diff_target)
1655
+ scope_file_count = (
1656
+ scoped_regular_file_count(repository, paths)
1657
+ if mode == "standard" and len(paths) > 1
1658
+ else scan_scope_file_count(repository, paths or ["."], diff_target)
1659
+ )
1491
1660
  require_working_tree_unchanged(repository, diff_target)
1492
1661
  parent_scan_id = (
1493
1662
  require_uuid(args.parent_scan_id, "parent-scan-id")
@@ -1503,9 +1672,19 @@ def register_cli_scan(
1503
1672
  with ExitStack() as archive:
1504
1673
  deadline = None
1505
1674
  while True:
1506
- if scan_dir is None:
1507
- # Wait for any previous archive to finish before inspecting its replacement.
1508
- scan_dir = require_cli_scan_directory(repository, args)
1675
+ registration = register_security_target(connection, str(repository))
1676
+ target_id = registration.target_id
1677
+ if parent_scan_id is not None:
1678
+ parent = require_scan(connection, parent_scan_id)
1679
+ parent_scope = RepositoryScanScope(
1680
+ registration.repository_generation, target_id
1681
+ )
1682
+ if not parent_scope.contains(parent):
1683
+ raise SystemExit(
1684
+ "A rerun must belong to the same repository as its parent scan."
1685
+ )
1686
+ # Revalidate the output and its owner under the current write lock.
1687
+ scan_dir = require_cli_scan_directory(repository, args)
1509
1688
  try:
1510
1689
  archived_scan_dir = archive.enter_context(
1511
1690
  archive_scan(
@@ -1514,7 +1693,7 @@ def register_cli_scan(
1514
1693
  scan_dir,
1515
1694
  timestamp,
1516
1695
  require_canonical_scan_directory,
1517
- before_archive=before_archive,
1696
+ before_archive=acknowledge_output_change,
1518
1697
  )
1519
1698
  )
1520
1699
  break
@@ -1533,18 +1712,9 @@ def register_cli_scan(
1533
1712
  if remaining <= 0:
1534
1713
  raise
1535
1714
  connection.rollback()
1536
- before_archive = None # The host already acknowledged this registration.
1537
1715
  scan_dir = None # Revalidate the directory and its owner under the next lock.
1538
1716
  time.sleep(min(0.05, remaining))
1539
1717
  connection.execute("BEGIN IMMEDIATE")
1540
- target_id = ensure_security_target(connection, str(repository))
1541
- if parent_scan_id is not None:
1542
- parent = require_scan(connection, parent_scan_id)
1543
- if parent["target_id"] != target_id:
1544
- raise SystemExit(
1545
- "A rerun must belong to the same repository as its parent scan."
1546
- )
1547
-
1548
1718
  connection.execute(
1549
1719
  """
1550
1720
  INSERT INTO workspaces (
@@ -1574,6 +1744,7 @@ def register_cli_scan(
1574
1744
  scope=scope,
1575
1745
  diff_target=diff_target,
1576
1746
  target_identity=target_identity,
1747
+ repository_generation=registration.repository_generation,
1577
1748
  source_scopes=source_scopes,
1578
1749
  target_root=scan_dir.parent,
1579
1750
  target_summary=None,
@@ -1581,6 +1752,10 @@ def register_cli_scan(
1581
1752
  timestamp=timestamp,
1582
1753
  handoff_status="delivered",
1583
1754
  scan_dir=scan_dir,
1755
+ surface=surface,
1756
+ package_version=package_version,
1757
+ launch_kind=launch_kind,
1758
+ hosted=hosted,
1584
1759
  )
1585
1760
  connection.execute(
1586
1761
  "UPDATE scans SET recipe_json = ?, parent_scan_id = ?, user_context = ? WHERE id = ?",
@@ -1593,14 +1768,31 @@ def register_cli_scan(
1593
1768
  )
1594
1769
  if workflow_id is not None:
1595
1770
  register_workflow_scan(connection, workflow_id, scan_id, str(scan_dir), timestamp)
1771
+ if resume_target is not None:
1772
+ archive.enter_context(
1773
+ register_windows_resume_output(
1774
+ connection, scan_id, scan_dir, resume_target, acknowledge_output_change
1775
+ )
1776
+ )
1777
+ scan = connection.execute(
1778
+ "SELECT scans.*, snapshot AS lifecycle_json FROM scans "
1779
+ "JOIN scan_lifecycles ON scan_id = scans.id WHERE scans.id = ?",
1780
+ (scan_id,),
1781
+ ).fetchone()
1596
1782
  connection.commit()
1597
1783
  except BaseException:
1598
1784
  connection.rollback()
1599
1785
  raise
1600
- scan = require_scan(connection, scan_id)
1601
1786
  return {
1602
1787
  **({"archivedScanDir": str(archived_scan_dir)} if archived_scan_dir is not None else {}),
1603
1788
  "contract": scan_contract(scan),
1789
+ "lifecycle": json.loads(scan["lifecycle_json"]),
1790
+ **({"phase": scan["phase"]} if mode == "standard" else {}),
1791
+ **(
1792
+ {"canSaveTimeBudgetStart": True}
1793
+ if "maxTimeMinutes" in recipe and mode in {"standard", "diff"}
1794
+ else {}
1795
+ ),
1604
1796
  "scanDir": str(scan_dir),
1605
1797
  "scanId": scan_id,
1606
1798
  "scopeFileCount": scope_file_count,
@@ -1613,8 +1805,11 @@ def set_scan_thread(connection: sqlite3.Connection, args: argparse.Namespace) ->
1613
1805
  scan = require_scan(connection, args.scan_id)
1614
1806
  with connection:
1615
1807
  connection.execute(
1616
- "UPDATE scans SET continuation_thread_id = ?, updated_at = ? WHERE id = ?",
1617
- (args.thread_id, now(), scan["id"]),
1808
+ """UPDATE scans SET continuation_thread_id = ?1, updated_at = ?2,
1809
+ recipe_json = CASE WHEN ?3 IS NULL THEN recipe_json
1810
+ ELSE json_insert(recipe_json, '$.timeBudgetStartedAt', ?3) END
1811
+ WHERE id = ?4""",
1812
+ (args.thread_id, now(), getattr(args, "time_budget_started_at", None), scan["id"]),
1618
1813
  )
1619
1814
  return {"scanId": scan["id"], "threadId": args.thread_id}
1620
1815
 
@@ -2195,9 +2390,62 @@ def set_finding_remediation(
2195
2390
  )
2196
2391
  except (ContractError, OSError, SystemExit) as exc:
2197
2392
  raise SystemExit(f"Could not save the remediation patch: {exc}") from exc
2198
- return scan_context(
2199
- connection, occurrence["scan_id"], occurrence["id"] if patch is not None else None
2200
- )
2393
+ if getattr(args, "assigned_finding", False):
2394
+ return assigned_remediation_context(
2395
+ connection,
2396
+ occurrence["scan_id"],
2397
+ occurrence["id"],
2398
+ request_id,
2399
+ action_token,
2400
+ written_version=args.expected_version + 1,
2401
+ )
2402
+ return scan_context(connection, occurrence["scan_id"], occurrence["id"])
2403
+
2404
+
2405
+ def assigned_remediation_context(
2406
+ connection: sqlite3.Connection,
2407
+ scan_id: str,
2408
+ occurrence_id: str,
2409
+ request_id: str,
2410
+ action_token: str,
2411
+ *,
2412
+ written_version: int | None = None,
2413
+ ) -> dict[str, Any]:
2414
+ """After authorization, project only the assigned finding and check the returned action state."""
2415
+ occurrence = require_occurrence(connection, occurrence_id)
2416
+ if occurrence["details_json"] == "{}":
2417
+ backfill_legacy_finding_details(connection, require_scan(connection, scan_id))
2418
+ occurrence = require_occurrence(connection, occurrence_id)
2419
+ if occurrence["scan_id"] != scan_id:
2420
+ raise SystemExit("This finding does not belong to the selected scan.")
2421
+ scan = require_scan(connection, scan_id)
2422
+ finding = finding_result(connection, scan, occurrence, related=None)
2423
+ state = finding["remediationState"]
2424
+ if written_version is None:
2425
+ if not (
2426
+ state.get("requestId") == request_id
2427
+ and state.get("actionClaimToken") == action_token
2428
+ and state.get("pendingAction")
2429
+ ):
2430
+ raise SystemExit(
2431
+ "This remediation action is no longer owned by the supplied request and action token."
2432
+ )
2433
+ elif (
2434
+ state.get("requestId") != request_id
2435
+ or state.get("version") != written_version
2436
+ or state.get("actionClaimToken") not in (None, action_token)
2437
+ ):
2438
+ # The next action may already have started since this write committed.
2439
+ return {"occurrenceId": occurrence_id, "requestId": request_id, "version": written_version}
2440
+ return {
2441
+ "scan": {
2442
+ "scanId": scan["id"],
2443
+ "targetPath": scan["target_path"],
2444
+ "targetRevision": scan["target_revision"],
2445
+ "scanDir": scan["scan_dir"],
2446
+ "findings": [finding],
2447
+ }
2448
+ }
2201
2449
 
2202
2450
 
2203
2451
  def require_reviewed_patch_applied(
@@ -2205,7 +2453,9 @@ def require_reviewed_patch_applied(
2205
2453
  ) -> str | None:
2206
2454
  target = require_scan_target_identity(scan)
2207
2455
  _, content_digest = remediation_checkout_snapshot(
2208
- scan, expected_revision=remediation["base_revision"]
2456
+ scan,
2457
+ expected_revision=remediation["base_revision"],
2458
+ expected_digest=remediation["base_content_digest"],
2209
2459
  )
2210
2460
  if content_digest == remediation["base_content_digest"]:
2211
2461
  raise SystemExit(
@@ -2215,12 +2465,17 @@ def require_reviewed_patch_applied(
2215
2465
  excluded = (Path(scan["scan_dir"]),)
2216
2466
  # Unborn repositories still need their config while metadata junctions are deferred.
2217
2467
  git_dir = git_output(target, "rev-parse", "--absolute-git-dir")
2468
+ metadata = Path(git_dir) if git_dir is not None else None
2218
2469
  if not unversioned:
2219
2470
  repository, pathspec = git_worktree_context(target)
2220
2471
  if git_dir is None:
2221
2472
  raise SystemExit("Could not inspect the selected Git working tree.")
2222
2473
  excluded += tuple(path for path, _ in git_submodule_entries(target))
2223
- with tempfile.TemporaryDirectory(prefix="codex-security-remediation-") as temporary:
2474
+ with (
2475
+ tempfile.TemporaryDirectory(prefix="codex-security-remediation-") as temporary,
2476
+ ExitStack() as pins,
2477
+ ):
2478
+ pinned_metadata = pin_replay_metadata(pins, metadata, Path(temporary))
2224
2479
  reviewed_patch = Path(temporary) / "reviewed.patch"
2225
2480
  digest = hashlib.sha256()
2226
2481
  with open_scan_local_file(Path(scan["scan_dir"]), patch_path) as source:
@@ -2236,36 +2491,30 @@ def require_reviewed_patch_applied(
2236
2491
  junctions = copy_directory_excluding(target, checkout, excluded)
2237
2492
  else:
2238
2493
  _, junctions = copy_git_worktree_files(target, checkout_root, excluded)
2239
- arguments = ["apply", "--reverse", "--whitespace=nowarn"]
2240
- if unversioned:
2241
- arguments.append("--no-index")
2242
- elif pathspec != ".":
2243
- arguments.append(f"--directory={pathspec}")
2244
- arguments.append(str(reviewed_patch))
2245
- applied = git_command(
2246
- checkout if unversioned else checkout_root,
2247
- *arguments,
2248
- text=False,
2249
- git_dir=Path(git_dir) if git_dir is not None else None,
2250
- work_tree=checkout_root if git_dir is not None else None,
2494
+ short, replay_root = remediation_state.reverse_reviewed_patch(
2495
+ target,
2496
+ checkout_root,
2497
+ metadata,
2498
+ pinned_metadata,
2499
+ remediation["patch_digest"],
2500
+ pins,
2501
+ reviewed_patch,
2502
+ None if unversioned else pathspec,
2251
2503
  )
2252
- if applied.returncode != 0:
2253
- raise SystemExit(
2254
- "The selected checkout does not contain the reviewed remediation patch. Apply exactly that patch before recording it as applied."
2255
- )
2256
2504
  restore_directory_junctions(
2257
2505
  target if unversioned else repository,
2258
2506
  checkout if unversioned else checkout_root,
2259
2507
  junctions,
2260
2508
  )
2261
2509
  reverted_digest = (
2262
- directory_content_digest(checkout)
2510
+ directory_content_digest(checkout, replay_git_dir=replay_root)
2263
2511
  if unversioned
2264
2512
  else worktree_content_digest_for_context(
2265
2513
  checkout_root,
2266
2514
  pathspec or ".",
2267
- git_dir=Path(git_dir),
2515
+ git_dir=short or metadata,
2268
2516
  work_tree=checkout_root,
2517
+ expected_digest=remediation["base_content_digest"],
2269
2518
  )
2270
2519
  )
2271
2520
  if reverted_digest != remediation["base_content_digest"] and unversioned:
@@ -2275,12 +2524,16 @@ def require_reviewed_patch_applied(
2275
2524
  checkout,
2276
2525
  "-c",
2277
2526
  "core.autocrlf=input",
2278
- *arguments,
2527
+ "apply",
2528
+ "--reverse",
2529
+ "--whitespace=nowarn",
2530
+ "--no-index",
2531
+ str(reviewed_patch),
2279
2532
  text=True,
2280
2533
  )
2281
2534
  if applied_without_conversion.returncode == 0:
2282
2535
  restore_directory_junctions(target, checkout, junctions)
2283
- reverted_digest = directory_content_digest(checkout)
2536
+ reverted_digest = directory_content_digest(checkout, replay_git_dir=replay_root)
2284
2537
  if reverted_digest != remediation["base_content_digest"]:
2285
2538
  raise SystemExit(
2286
2539
  "The selected checkout contains changes outside the reviewed patch. Remove them before recording the patch as applied."
@@ -2295,9 +2548,6 @@ def require_remediation_checkout_unchanged(
2295
2548
  require_applied_content: bool = False,
2296
2549
  require_base_content: bool = False,
2297
2550
  ) -> None:
2298
- _, content_digest = remediation_checkout_snapshot(
2299
- scan, expected_revision=remediation["base_revision"]
2300
- )
2301
2551
  expected_digest = (
2302
2552
  remediation["applied_content_digest"]
2303
2553
  if require_applied_content
@@ -2305,6 +2555,9 @@ def require_remediation_checkout_unchanged(
2305
2555
  if require_base_content
2306
2556
  else None
2307
2557
  )
2558
+ _, content_digest = remediation_checkout_snapshot(
2559
+ scan, expected_revision=remediation["base_revision"], expected_digest=expected_digest
2560
+ )
2308
2561
  if expected_digest is not None and content_digest != expected_digest:
2309
2562
  raise SystemExit(
2310
2563
  "Working-tree contents changed. Regenerate the remediation patch against the current checkout."
@@ -2385,11 +2638,10 @@ def workspace_state(
2385
2638
  }
2386
2639
  selected_scan_id = result_scan_id or workspace["active_scan_id"]
2387
2640
  if selected_scan_id:
2388
- selected_scan = require_scan(connection, selected_scan_id)
2389
- result["userContext"] = selected_scan["user_context"]
2390
- result["results"] = (
2391
- result_scan if result_scan is not None else scan_result(connection, selected_scan)
2392
- )
2641
+ if result_scan is None:
2642
+ result_scan = scan_result(connection, require_scan(connection, selected_scan_id))
2643
+ result["userContext"] = result_scan["userContext"]
2644
+ result["results"] = result_scan
2393
2645
  return result
2394
2646
 
2395
2647
  target_metadata = None
@@ -2430,9 +2682,15 @@ def scan_context(
2430
2682
  connection: sqlite3.Connection,
2431
2683
  scan_id: str,
2432
2684
  occurrence_id: str | None = None,
2685
+ *,
2686
+ model_thread_id: str | None = None,
2433
2687
  ) -> dict[str, Any]:
2434
2688
  scan = require_scan(connection, scan_id)
2435
- result = scan_result(connection, scan, occurrence_id=occurrence_id)
2689
+ # Legacy backfill may write before scan_result reloads the row for its response.
2690
+ handoff.require_tokenless_scan_owner(connection, scan, model_thread_id)
2691
+ result = scan_result(
2692
+ connection, scan, occurrence_id=occurrence_id, model_thread_id=model_thread_id
2693
+ )
2436
2694
  workspace_result = result
2437
2695
  if len(result["findings"]) > FINDINGS_RESULT_LIMIT:
2438
2696
  workspace_result = dict(
@@ -2456,7 +2714,77 @@ def scan_context(
2456
2714
 
2457
2715
 
2458
2716
  def get_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
2459
- result = scan_context(connection, args.scan_id, args.occurrence_id)
2717
+ if getattr(args, "progress_only", False):
2718
+ scan = connection.execute(
2719
+ """
2720
+ SELECT scans.id, scans.mode, scans.phase, scans.status, scans.canceled_at,
2721
+ progress.*, progress.updated_at AS progress_updated_at
2722
+ FROM scans JOIN scan_progress AS progress ON progress.scan_id = scans.id
2723
+ WHERE scans.id = ?
2724
+ """,
2725
+ (resolve_scan_id(connection, args.scan_id),),
2726
+ ).fetchone()
2727
+ if scan is None:
2728
+ raise SystemExit("Codex Security scan not found.")
2729
+ progress_result, _ = progress.scan_progress_result(connection, scan)
2730
+ return {"scan": {"progress": progress_result}}
2731
+ request_id = getattr(args, "request_id", None)
2732
+ action_token = getattr(args, "action_token", None)
2733
+ if request_id is not None or action_token is not None:
2734
+ scan_id = require_uuid(args.scan_id, "scan-id")
2735
+ request_id = require_uuid(request_id or "", "request-id")
2736
+ action_token = require_uuid(action_token or "", "action-token")
2737
+ authorized = connection.execute(
2738
+ """
2739
+ SELECT 1 FROM finding_remediation_attempts AS action
2740
+ JOIN finding_occurrences AS finding ON finding.id = action.occurrence_id
2741
+ WHERE finding.scan_id = ? AND finding.id = ? AND action.request_id = ?
2742
+ AND action.pending_action_claim_token = ? AND action.pending_action IS NOT NULL
2743
+ """,
2744
+ (scan_id, args.occurrence_id, request_id, action_token),
2745
+ ).fetchone()
2746
+ if authorized is None:
2747
+ raise SystemExit(
2748
+ "This remediation action is no longer owned by the supplied request and action token."
2749
+ )
2750
+ return assigned_remediation_context(
2751
+ connection, scan_id, args.occurrence_id, request_id, action_token
2752
+ )
2753
+ context_claim = getattr(args, "context_claim_token", None)
2754
+ model_thread = getattr(args, "model_thread_id", None)
2755
+ if context_claim is not None:
2756
+ scan = require_scan(connection, args.scan_id)
2757
+ handoff.require_tokenless_scan_owner(connection, scan, model_thread)
2758
+ if scan["handoff_status"] == "pending":
2759
+ detail = (
2760
+ "Pass the handoffClaimToken returned by the Codex Security scan launcher."
2761
+ if scan["handoff_claim_token"]
2762
+ else "Claim the pending Codex Security scan handoff before loading its context."
2763
+ )
2764
+ raise SystemExit(f"This Codex Security scan handoff has not been delivered. {detail}")
2765
+ if context_claim:
2766
+ context_claim = handoff.require_handoff_claim_token(context_claim)
2767
+ if scan["handoff_claim_token"] and scan["handoff_claim_token"] != context_claim:
2768
+ if not model_thread:
2769
+ raise SystemExit("Scan context requires its current continuation claim.")
2770
+ owner = (
2771
+ scan["continuation_thread_id"]
2772
+ or require_workspace(connection, scan["workspace_id"])["thread_id"]
2773
+ )
2774
+ if model_thread != owner:
2775
+ raise SystemExit("Scan context is owned by another continuation.")
2776
+ if (
2777
+ args.occurrence_id is not None
2778
+ and connection.execute(
2779
+ "SELECT 1 FROM finding_occurrences WHERE scan_id = ? AND id = ?",
2780
+ (scan["id"], args.occurrence_id),
2781
+ ).fetchone()
2782
+ is None
2783
+ ):
2784
+ raise SystemExit("This finding was not found in the selected scan.")
2785
+ result = scan_context(
2786
+ connection, args.scan_id, args.occurrence_id, model_thread_id=model_thread
2787
+ )
2460
2788
  if result["scan"]["progress"]["status"] != "running":
2461
2789
  scan = require_scan(connection, args.scan_id)
2462
2790
  try:
@@ -2469,7 +2797,7 @@ def get_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[s
2469
2797
  def list_findings(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
2470
2798
  scan = require_scan(connection, args.scan_id)
2471
2799
  backfill_legacy_finding_details(connection, scan)
2472
- limit = min(args.limit, FINDINGS_PAGE_MAX)
2800
+ limit = args.limit
2473
2801
  rows = scan_history.finding_occurrence_rows(
2474
2802
  connection,
2475
2803
  scan["id"],
@@ -2513,11 +2841,16 @@ def scan_result(
2513
2841
  scan: sqlite3.Row,
2514
2842
  *,
2515
2843
  occurrence_id: str | None = None,
2844
+ model_thread_id: str | None = None,
2516
2845
  ) -> dict[str, Any]:
2846
+ """Assemble the workbench view of a saved scan, backfilling older finding details as needed.
2847
+
2848
+ An occurrence ID selects one finding from that scan. An occurrence belonging
2849
+ to a different scan raises SystemExit.
2850
+ """
2517
2851
  backfill_legacy_finding_details(connection, scan)
2518
- progress = connection.execute(
2519
- "SELECT * FROM scan_progress WHERE scan_id = ?", (scan["id"],)
2520
- ).fetchone()
2852
+ scan = scan_history.scan_with_progress(connection, scan["id"])
2853
+ handoff.require_tokenless_scan_owner(connection, scan, model_thread_id)
2521
2854
  artifact_rows = connection.execute(
2522
2855
  "SELECT kind, path FROM scan_artifacts WHERE scan_id = ?", (scan["id"],)
2523
2856
  )
@@ -2545,9 +2878,6 @@ def scan_result(
2545
2878
  raise SystemExit("This finding does not belong to the selected scan.")
2546
2879
  if all(row["id"] != occurrence["id"] for row in occurrence_rows):
2547
2880
  occurrence_rows.append(occurrence)
2548
- finding_count = connection.execute(
2549
- "SELECT COUNT(*) FROM finding_occurrences WHERE scan_id = ?", (scan["id"],)
2550
- ).fetchone()[0]
2551
2881
  severity_counts = {
2552
2882
  row["severity"]: row["count"]
2553
2883
  for row in connection.execute(
@@ -2560,38 +2890,9 @@ def scan_result(
2560
2890
  (scan["id"],),
2561
2891
  )
2562
2892
  }
2893
+ finding_count = sum(severity_counts.values())
2563
2894
  remediation_available, remediation_unavailable_reason = remediation_availability(scan)
2564
- independent_reviews = (
2565
- deep_scan.independent_review_progress(connection, scan["id"])
2566
- if scan["mode"] == "deep"
2567
- else None
2568
- )
2569
- progress_result = {
2570
- "candidates": {"reportable": progress["reportable_findings_count"]},
2571
- "coverage": {
2572
- "closedRows": progress["review_items_completed"],
2573
- "filesTotal": progress["scope_file_count"],
2574
- "worklistRows": progress["review_items_total"],
2575
- },
2576
- "phase": scan["phase"],
2577
- "phaseProgress": {
2578
- "completed": progress["phase_items_completed"],
2579
- "total": progress["phase_items_total"],
2580
- "unit": progress["phase_progress_unit"],
2581
- },
2582
- "preflightProgress": {
2583
- "completed": progress["preflight_checks_completed"],
2584
- "total": progress["preflight_checks_total"],
2585
- },
2586
- "preflightIssues": json.loads(progress["preflight_issues_json"]),
2587
- "reviewPass": progress["deep_review_pass"],
2588
- "status": "canceled" if scan["canceled_at"] else scan["status"],
2589
- "updatedAt": progress["updated_at"],
2590
- }
2591
- review_updated_at = ""
2592
- if independent_reviews is not None:
2593
- review_updated_at = str(independent_reviews.pop("updatedAt"))
2594
- progress_result["independentReviews"] = independent_reviews
2895
+ progress_result, review_updated_at = progress.scan_progress_result(connection, scan)
2595
2896
  relations = scan_history.finding_relations(
2596
2897
  connection, scan["id"], (row["id"] for row in occurrence_rows)
2597
2898
  )
@@ -2599,12 +2900,14 @@ def scan_result(
2599
2900
  "artifacts": artifacts,
2600
2901
  **model_fields,
2601
2902
  "canceledAt": scan["canceled_at"],
2903
+ "logCompletedAt": scan["log_completed_at"],
2602
2904
  **scan_usage.stored_scan_cost_fields(scan["cost_json"]),
2603
2905
  "contract": scan_contract(scan),
2604
2906
  "continuationThreadId": scan["continuation_thread_id"],
2605
2907
  "threadIds": scan_usage._scan_root_thread_ids(connection, scan, None),
2606
2908
  "executionThreadIds": scan_usage._scan_execution_thread_ids(connection, scan),
2607
2909
  "failureMessage": scan["failure_message"],
2910
+ "lifecycle": json.loads(scan["lifecycle_json"]),
2608
2911
  "findings": [
2609
2912
  finding_result(connection, scan, row, related=relations.get(row["id"], []))
2610
2913
  for row in occurrence_rows
@@ -2635,7 +2938,7 @@ def scan_result(
2635
2938
  "targetSummary": scan["target_summary"],
2636
2939
  "updatedAt": max(
2637
2940
  scan["updated_at"],
2638
- progress["updated_at"],
2941
+ scan["progress_updated_at"],
2639
2942
  review_updated_at,
2640
2943
  finding_management_updated_at(connection, scan["id"]) or "",
2641
2944
  ),
@@ -2648,7 +2951,8 @@ def remediation_availability(scan: sqlite3.Row) -> tuple[bool, str | None]:
2648
2951
  if scan["status"] != "complete":
2649
2952
  return False, remediation_state.SCAN_STATUS_ERROR
2650
2953
  try:
2651
- current_revision = git_revision(require_scan_target_identity(scan))
2954
+ with checked_scan_target(scan) as target:
2955
+ current_revision = git_revision(target)
2652
2956
  except SystemExit as exc:
2653
2957
  return False, str(exc)
2654
2958
  expected_revision = scan["target_revision"]
@@ -2706,15 +3010,7 @@ def backfill_legacy_finding_details(connection: sqlite3.Connection, scan: sqlite
2706
3010
  if not updates:
2707
3011
  return
2708
3012
 
2709
- # Reads normally avoid writer admission, but legacy details still need a write.
2710
- for attempt in range(SQLITE_RETRY_ATTEMPTS):
2711
- try:
2712
- connection.execute("BEGIN IMMEDIATE")
2713
- break
2714
- except sqlite3.OperationalError as exc:
2715
- if attempt == SQLITE_RETRY_ATTEMPTS - 1 or not sqlite_busy(exc):
2716
- raise
2717
- time.sleep(0.05 * (2**attempt))
3013
+ begin_immediate_with_retry(connection)
2718
3014
  with connection:
2719
3015
  current = require_scan(connection, scan["id"])
2720
3016
  recorded_digest = current["seal_manifest_digest"]
@@ -2753,7 +3049,7 @@ def finding_result(
2753
3049
  scan: sqlite3.Row,
2754
3050
  occurrence: sqlite3.Row,
2755
3051
  *,
2756
- related: list[dict[str, Any]],
3052
+ related: list[dict[str, Any]] | None,
2757
3053
  ) -> dict[str, Any]:
2758
3054
  details = bounded_finding_details(read_finding_details(occurrence["details_json"]))
2759
3055
  confidence = details.get("confidence")
@@ -2767,11 +3063,11 @@ def finding_result(
2767
3063
  except SystemExit:
2768
3064
  target = None
2769
3065
  for row in connection.execute(
2770
- """
3066
+ f"""
2771
3067
  SELECT relative_path, start_line, end_line, role
2772
3068
  FROM finding_locations
2773
3069
  WHERE occurrence_id = ?
2774
- ORDER BY CASE WHEN role = 'root_control' THEN 0 ELSE 1 END, sort_order
3070
+ ORDER BY {FINDING_LOCATION_ORDER_SQL}
2775
3071
  LIMIT ?
2776
3072
  """,
2777
3073
  (occurrence["id"], FINDING_LOCATIONS_LIMIT),
@@ -2822,15 +3118,16 @@ def finding_result(
2822
3118
  "title": bounded_output_text(occurrence["title"], FINDING_TITLE_BYTES),
2823
3119
  "triage": finding_triage_result(connection, occurrence["id"]),
2824
3120
  }
2825
- matches, known_since, known_scan_ids = scan_history.finding_matches(
2826
- connection, occurrence["id"], scan["id"], scan["started_at"]
2827
- )
2828
- if matches:
2829
- result["matches"] = matches
2830
- result["knownSince"] = known_since
2831
- result["knownScanIds"] = known_scan_ids
2832
- if related:
2833
- result["related"] = related
3121
+ if related is not None:
3122
+ matches, known_since, known_scan_ids = scan_history.finding_matches(
3123
+ connection, occurrence["id"], scan["id"], scan["started_at"]
3124
+ )
3125
+ if matches:
3126
+ result["matches"] = matches
3127
+ result["knownSince"] = known_since
3128
+ result["knownScanIds"] = known_scan_ids
3129
+ if related:
3130
+ result["related"] = related
2834
3131
  source_excerpt = finding_source_excerpt(
2835
3132
  scan, target, excerpt_locations, requested_scan_paths(scan)
2836
3133
  )
@@ -2912,39 +3209,10 @@ def read_finding_details(value: str) -> dict[str, Any]:
2912
3209
  return details if isinstance(details, dict) else {}
2913
3210
 
2914
3211
 
2915
- def finding_management_updated_at(connection: sqlite3.Connection, scan_id: str) -> str | None:
2916
- return connection.execute(
2917
- """
2918
- SELECT MAX(updated_at)
2919
- FROM (
2920
- SELECT triage.updated_at
2921
- FROM finding_triage AS triage
2922
- JOIN finding_occurrences AS occurrences ON occurrences.id = triage.occurrence_id
2923
- WHERE occurrences.scan_id = ?
2924
- UNION ALL
2925
- SELECT remediation.updated_at
2926
- FROM finding_remediation_attempts AS remediation
2927
- JOIN finding_occurrences AS occurrences ON occurrences.id = remediation.occurrence_id
2928
- WHERE occurrences.scan_id = ?
2929
- )
2930
- """,
2931
- (scan_id, scan_id),
2932
- ).fetchone()[0]
3212
+ finding_management_updated_at = scan_history.finding_management_updated_at
2933
3213
 
2934
3214
 
2935
- def finding_triage_result(connection: sqlite3.Connection, occurrence_id: str) -> dict[str, Any]:
2936
- row = connection.execute(
2937
- "SELECT status, close_reason, note, updated_at FROM finding_triage WHERE occurrence_id = ?",
2938
- (occurrence_id,),
2939
- ).fetchone()
2940
- if row is None:
2941
- return {"status": "open"}
2942
- return {
2943
- "closeReason": row["close_reason"],
2944
- "note": row["note"],
2945
- "status": row["status"],
2946
- "updatedAt": row["updated_at"],
2947
- }
3215
+ finding_triage_result = scan_history.finding_triage_result
2948
3216
 
2949
3217
 
2950
3218
  def finding_remediation_result(
@@ -2994,48 +3262,9 @@ def finding_remediation_result(
2994
3262
  def patch_artifact_preview(
2995
3263
  scan_dir: Path, relative_path: str | None, expected_digest: str | None
2996
3264
  ) -> tuple[str | None, dict[str, int | bool] | None]:
2997
- if relative_path is None or expected_digest is None:
2998
- return None, None
2999
- digest = hashlib.sha256()
3000
- preview = bytearray()
3001
- additions = 0
3002
- deletions = 0
3003
- file_count = 0
3004
- old_headers = 0
3005
- new_headers = 0
3006
- at_line_start = True
3007
- try:
3008
- with open_scan_local_file(scan_dir, relative_path) as patch:
3009
- while chunk := patch.readline(1024 * 1024):
3010
- digest.update(chunk)
3011
- if len(preview) <= PATCH_PREVIEW_BYTES:
3012
- preview.extend(chunk[: PATCH_PREVIEW_BYTES + 1 - len(preview)])
3013
- if at_line_start:
3014
- if chunk.startswith(b"diff --git "):
3015
- file_count += 1
3016
- elif chunk.startswith(b"+++ "):
3017
- new_headers += 1
3018
- elif chunk.startswith(b"--- "):
3019
- old_headers += 1
3020
- elif chunk.startswith(b"+"):
3021
- additions += 1
3022
- elif chunk.startswith(b"-"):
3023
- deletions += 1
3024
- at_line_start = chunk.endswith(b"\n")
3025
- except SystemExit:
3026
- return None, None
3027
- if f"sha256:{digest.hexdigest()}" != expected_digest:
3028
- return None, None
3029
- preview_truncated = len(preview) > PATCH_PREVIEW_BYTES
3030
- preview_text = preview[:PATCH_PREVIEW_BYTES].decode("utf-8", errors="replace")
3031
- if preview_truncated:
3032
- preview_text = f"{preview_text}\n... patch preview truncated ..."
3033
- return preview_text, {
3034
- "additions": additions,
3035
- "deletions": deletions,
3036
- "fileCount": file_count or min(old_headers, new_headers),
3037
- "previewTruncated": preview_truncated,
3038
- }
3265
+ return remediation_state.patch_artifact_preview(
3266
+ scan_dir, relative_path, expected_digest, open_scan_local_file
3267
+ )
3039
3268
 
3040
3269
 
3041
3270
  def available_artifact_path(scan_dir: Path, candidate: Path) -> Path | None:
@@ -3067,12 +3296,12 @@ def artifact_path(scan_dir: Path, file_name: str, *, required: bool) -> Path | N
3067
3296
  return resolved
3068
3297
 
3069
3298
 
3070
- def require_canonical_scan_directory(scan_dir: Path) -> Path:
3299
+ def require_canonical_scan_directory(scan_dir: Path, *, writable_output: bool = False) -> Path:
3071
3300
  scan_dir = scan_dir.absolute()
3072
3301
  try:
3073
3302
  metadata = scan_dir.lstat()
3074
3303
  resolved = scan_dir.resolve(strict=True)
3075
- except OSError as exc:
3304
+ except (OSError, RuntimeError) as exc:
3076
3305
  raise SystemExit(
3077
3306
  "Scan directory must be an existing canonical non-symlink directory."
3078
3307
  ) from exc
@@ -3081,7 +3310,14 @@ def require_canonical_scan_directory(scan_dir: Path) -> Path:
3081
3310
  ):
3082
3311
  raise SystemExit("Scan directory must be an existing canonical non-symlink directory.")
3083
3312
  # Re-check ownership and parents so the scan directory cannot be substituted.
3084
- if os.name != "nt":
3313
+ if os.name == "nt":
3314
+ from windows_scan_local_files import require_windows_output_path
3315
+
3316
+ try:
3317
+ require_windows_output_path(scan_dir, writable_output=writable_output)
3318
+ except OSError as exc:
3319
+ raise SystemExit(str(exc)) from exc
3320
+ else:
3085
3321
  geteuid = getattr(os, "geteuid", None)
3086
3322
  effective_uid = geteuid() if geteuid is not None else None
3087
3323
  if effective_uid is not None and metadata.st_uid != effective_uid:
@@ -3108,19 +3344,6 @@ def require_canonical_scan_directory(scan_dir: Path) -> Path:
3108
3344
  return scan_dir
3109
3345
 
3110
3346
 
3111
- def read_json_object(path: Path) -> dict[str, Any]:
3112
- try:
3113
- payload = json.loads(
3114
- path.read_text(encoding="utf-8"),
3115
- parse_constant=reject_non_finite_json,
3116
- )
3117
- except (OSError, ValueError) as exc:
3118
- raise SystemExit(f"{path.name}: invalid JSON: {exc}") from exc
3119
- if not isinstance(payload, dict):
3120
- raise SystemExit(f"{path.name}: expected a JSON object.")
3121
- return payload
3122
-
3123
-
3124
3347
  _WORKBENCH_PUBLICATION_CONTEXT = _WORKBENCH_DB_CONTEXT = SimpleNamespace(**globals())
3125
3348
 
3126
3349
 
@@ -3143,12 +3366,42 @@ def main(*, before_archive: Callable[[], None] | None = None) -> None:
3143
3366
  print(json.dumps(saved_results.read_or_save_artifact(args)))
3144
3367
  return
3145
3368
  if args.command == "finding-issues":
3146
- result = publication.finding_issues(_WORKBENCH_PUBLICATION_CONTEXT, json.load(sys.stdin))
3369
+ try:
3370
+ result = publication.finding_issues(
3371
+ _WORKBENCH_PUBLICATION_CONTEXT, json.load(sys.stdin)
3372
+ )
3373
+ except sqlite3.OperationalError as exc:
3374
+ if not getattr(exc, "_codex_security_state_unavailable", False):
3375
+ raise
3376
+ print(f"sqlite3.OperationalError: {exc}", file=sys.stderr)
3377
+ raise SystemExit(73) from None
3147
3378
  print(json.dumps(result, allow_nan=False, sort_keys=True))
3148
3379
  return
3149
- with closing(
3150
- connect(deferred=args.command in {"get-scan", "list-scans", "database-info"})
3151
- ) as connection:
3380
+ command_targets = tuple(
3381
+ Path(value).expanduser()
3382
+ for field in ("target_path", "repository")
3383
+ if (value := getattr(args, field, None))
3384
+ )
3385
+ with (
3386
+ closing(
3387
+ connect(
3388
+ deferred=args.command
3389
+ in {
3390
+ "get-scan",
3391
+ "read-scan-usage",
3392
+ "list-scans",
3393
+ "database-info",
3394
+ "update-progress",
3395
+ },
3396
+ discovery_roots=command_targets,
3397
+ in_memory=scan_history.reads_missing_history(args, database_path()),
3398
+ )
3399
+ ) as connection,
3400
+ scoped_git_discovery(
3401
+ lambda: [*migration_target_paths(connection), *command_targets],
3402
+ recorded_git_roots=lambda: recorded_git_checkout_roots(connection),
3403
+ ),
3404
+ ):
3152
3405
  remediation_state.require_available(connection, args, require_scan)
3153
3406
  handlers = {
3154
3407
  "create-workspace": create_workspace,
@@ -3159,15 +3412,18 @@ def main(*, before_archive: Callable[[], None] | None = None) -> None:
3159
3412
  "get-deep-scan": deep_scan.get_deep_scan,
3160
3413
  "claim-deep-scan-coordinator": deep_scan.claim_deep_scan_coordinator,
3161
3414
  "upsert-deep-scan-worker": deep_scan.upsert_deep_scan_worker,
3162
- "claim-deep-scan-dedup": deep_scan.claim_deep_scan_dedup,
3163
- "commit-deep-scan-dedup": deep_scan.commit_deep_scan_dedup,
3415
+ "record-deep-scan-iteration": deep_scan.record_deep_scan_iteration,
3164
3416
  "finish-deep-scan": deep_scan.finish_deep_scan,
3165
3417
  "fail-deep-scan": deep_scan.fail_deep_scan,
3166
3418
  "record-deep-scan-publication-failure": deep_scan.record_deep_scan_publication_failure,
3167
3419
  "list-scans": scan_history.list_scans,
3168
3420
  "register-cli-scan": partial(register_cli_scan, before_archive=before_archive),
3421
+ "restore-cli-scan-archive": partial(
3422
+ restore_cli_scan_archive, canonical_directory=require_canonical_scan_directory
3423
+ ),
3169
3424
  "set-scan-thread": set_scan_thread,
3170
3425
  "set-scan-cost-limit": set_scan_cost_limit,
3426
+ "read-scan-usage": read_scan_usage,
3171
3427
  "finalize-scan-usage": finalize_scan_usage,
3172
3428
  "list-global-findings": native_indexes.list_global_findings,
3173
3429
  "list-repositories": native_indexes.list_repositories,
@@ -3281,4 +3537,7 @@ def main(*, before_archive: Callable[[], None] | None = None) -> None:
3281
3537
 
3282
3538
 
3283
3539
  if __name__ == "__main__":
3284
- main()
3540
+ try:
3541
+ main()
3542
+ except ContractError as exc:
3543
+ raise SystemExit(str(exc)) from None