@openai/codex-security 0.1.31 → 0.3.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 (514) hide show
  1. package/README.md +442 -2434
  2. package/_bundled_plugin/.app.json +5 -1
  3. package/_bundled_plugin/.codex-plugin/plugin.json +9 -5
  4. package/_bundled_plugin/.mcp.json +3 -0
  5. package/_bundled_plugin/examples/completed-scan/coverage.json +0 -1
  6. package/_bundled_plugin/examples/completed-scan/report.md +28 -26
  7. package/_bundled_plugin/examples/completed-scan/scan-manifest.json +1 -1
  8. package/_bundled_plugin/mcp/helpers.mjs +1 -0
  9. package/_bundled_plugin/mcp/helpers.mjs.br.part-000 +0 -0
  10. package/_bundled_plugin/mcp/native/THIRD_PARTY_NOTICES.txt +7 -214
  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/permission-profile-preflight.mjs +563 -0
  20. package/_bundled_plugin/mcp/server.mjs +1 -0
  21. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  22. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  23. package/_bundled_plugin/preflight/capability-profiles.toml +0 -13
  24. package/_bundled_plugin/references/artifact-storage.md +5 -5
  25. package/_bundled_plugin/references/config-preflight.md +1 -5
  26. package/_bundled_plugin/references/core-scan.md +34 -10
  27. package/_bundled_plugin/references/final-report.md +13 -13
  28. package/_bundled_plugin/references/finding-detail-fields.md +90 -80
  29. package/_bundled_plugin/references/sarif-adapter.md +1 -1
  30. package/_bundled_plugin/references/scan-artifacts.md +3 -4
  31. package/_bundled_plugin/references/scan-contract.md +11 -6
  32. package/_bundled_plugin/references/scan-prologue.md +12 -0
  33. package/_bundled_plugin/references/security-guidance.md +2 -2
  34. package/_bundled_plugin/references/standalone-validation.md +11 -0
  35. package/_bundled_plugin/references/threat-model.md +2 -2
  36. package/_bundled_plugin/schemas/coverage.schema.json +23 -47
  37. package/_bundled_plugin/schemas/definitions/artifact-common.schema.json +2 -24
  38. package/_bundled_plugin/schemas/definitions/discovery-candidate.schema.json +49 -25
  39. package/_bundled_plugin/schemas/findings.schema.json +71 -192
  40. package/_bundled_plugin/schemas/scan-manifest.schema.json +83 -95
  41. package/_bundled_plugin/schemas/tools/candidate-attack-paths.schema.json +91 -83
  42. package/_bundled_plugin/schemas/tools/candidate-validations.schema.json +35 -64
  43. package/_bundled_plugin/schemas/tools/deep-reducer.schema.json +5 -2
  44. package/_bundled_plugin/schemas/tools/discovery-candidates.schema.json +6 -55
  45. package/_bundled_plugin/schemas/tools/review-items.schema.json +2 -38
  46. package/_bundled_plugin/schemas/tools/scan-draft.schema.json +245 -356
  47. package/_bundled_plugin/scripts/codex_profile.mjs +239 -0
  48. package/_bundled_plugin/scripts/codex_session.mjs +53 -0
  49. package/_bundled_plugin/scripts/config_preflight.py +29 -69
  50. package/_bundled_plugin/scripts/deep_scan_config.py +9 -11
  51. package/_bundled_plugin/scripts/deep_scan_workbench.py +208 -363
  52. package/_bundled_plugin/scripts/filesystem_identity.py +1 -5
  53. package/_bundled_plugin/scripts/finalize_scan_contract.py +733 -375
  54. package/_bundled_plugin/scripts/finding_preview.py +81 -91
  55. package/_bundled_plugin/scripts/generate_in_scope_files.py +48 -102
  56. package/_bundled_plugin/scripts/generate_rank_input.py +117 -888
  57. package/_bundled_plugin/scripts/launch_codex_security_mcp +35 -6
  58. package/_bundled_plugin/scripts/launch_codex_security_mcp.cmd +5 -5
  59. package/_bundled_plugin/scripts/rank_preview.py +43 -902
  60. package/_bundled_plugin/scripts/report_projection.py +143 -219
  61. package/_bundled_plugin/scripts/snapshot_sqlite.py +6 -1
  62. package/_bundled_plugin/scripts/threat_model_projection.py +161 -0
  63. package/_bundled_plugin/scripts/validate_scan_contract.py +4 -34
  64. package/_bundled_plugin/scripts/validate_tracking_source.py +3 -13
  65. package/_bundled_plugin/scripts/windows_scan_local_files.py +59 -9
  66. package/_bundled_plugin/scripts/workbench/handoff.py +25 -50
  67. package/_bundled_plugin/scripts/workbench/runtime_toml.py +22 -0
  68. package/_bundled_plugin/scripts/workbench/storage.py +21 -6
  69. package/_bundled_plugin/scripts/workbench_cli.py +99 -190
  70. package/_bundled_plugin/scripts/workbench_constants.py +6 -14
  71. package/_bundled_plugin/scripts/workbench_db.py +524 -672
  72. package/_bundled_plugin/scripts/workbench_feedback.py +6 -3
  73. package/_bundled_plugin/scripts/workbench_finding_index.py +20 -7
  74. package/_bundled_plugin/scripts/workbench_finding_workflows.py +18 -4
  75. package/_bundled_plugin/scripts/workbench_local_dedupe.py +136 -0
  76. package/_bundled_plugin/scripts/workbench_native_indexes.py +28 -18
  77. package/_bundled_plugin/scripts/workbench_progress.py +26 -92
  78. package/_bundled_plugin/scripts/workbench_publication.py +77 -116
  79. package/_bundled_plugin/scripts/workbench_remediation.py +14 -20
  80. package/_bundled_plugin/scripts/workbench_saved_results.py +1402 -263
  81. package/_bundled_plugin/scripts/workbench_scan_history.py +57 -57
  82. package/_bundled_plugin/scripts/workbench_scan_start.py +109 -52
  83. package/_bundled_plugin/scripts/workbench_scan_usage.py +30 -49
  84. package/_bundled_plugin/scripts/workbench_schema.py +71 -1370
  85. package/_bundled_plugin/scripts/workbench_source_excerpt.py +16 -7
  86. package/_bundled_plugin/scripts/workbench_target.py +526 -150
  87. package/_bundled_plugin/scripts/workbench_target_state.py +1 -5
  88. package/_bundled_plugin/scripts/workbench_validation.py +19 -44
  89. package/_bundled_plugin/shared/workbench-history-repairs.json +218 -0
  90. package/_bundled_plugin/shared/workbench-migrations.json +397 -0
  91. package/_bundled_plugin/skills/assess-patch-risk/SKILL.md +15 -4
  92. package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +1 -0
  93. package/_bundled_plugin/skills/cloud-findings/SKILL.md +54 -0
  94. package/_bundled_plugin/skills/cloud-findings/agents/openai.yaml +4 -0
  95. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +6 -6
  96. package/_bundled_plugin/skills/deep-security-scan/agents/openai.yaml +1 -1
  97. package/_bundled_plugin/skills/finding-discovery/SKILL.md +3 -3
  98. package/_bundled_plugin/skills/propose-security-hardening/references/proposal-format.md +50 -20
  99. package/_bundled_plugin/skills/security-diff-scan/SKILL.md +8 -14
  100. package/_bundled_plugin/skills/security-diff-scan/agents/openai.yaml +1 -1
  101. package/_bundled_plugin/skills/security-scan/SKILL.md +22 -7
  102. package/_bundled_plugin/skills/security-scan/agents/openai.yaml +1 -1
  103. package/_bundled_plugin/skills/security-scan/references/desktop-scan.md +2 -2
  104. package/_bundled_plugin/skills/security-scan/references/scan-artifacts-and-ledger.md +13 -1
  105. package/_bundled_plugin/skills/threat-model/SKILL.md +2 -2
  106. package/_bundled_plugin/skills/track-findings/SKILL.md +46 -178
  107. package/_bundled_plugin/skills/track-findings/agents/openai.yaml +1 -1
  108. package/_bundled_plugin/skills/track-findings/references/github-security-advisories.md +16 -24
  109. package/_bundled_plugin/skills/track-findings/references/jira.md +15 -61
  110. package/_bundled_plugin/skills/triage-finding/SKILL.md +50 -289
  111. package/_bundled_plugin/skills/triage-finding/agents/openai.yaml +2 -2
  112. package/_bundled_plugin/skills/triage-finding/references/ticket-intake.md +23 -43
  113. package/_bundled_plugin/skills/triage-finding/references/triage-result-contract.md +7 -1
  114. package/bin/codex-security.mjs +9 -2
  115. package/dist/api.d.ts +63 -15
  116. package/dist/api.d.ts.map +1 -1
  117. package/dist/api.js +970 -553
  118. package/dist/api.js.map +1 -1
  119. package/dist/artifact-export.d.ts +70 -0
  120. package/dist/artifact-export.d.ts.map +1 -0
  121. package/dist/artifact-export.js +292 -0
  122. package/dist/artifact-export.js.map +1 -0
  123. package/dist/auth.d.ts +2 -6
  124. package/dist/auth.d.ts.map +1 -1
  125. package/dist/auth.js +27 -12
  126. package/dist/auth.js.map +1 -1
  127. package/dist/bulk-scan-discovery.d.ts +1 -0
  128. package/dist/bulk-scan-discovery.d.ts.map +1 -1
  129. package/dist/bulk-scan-discovery.js +9 -9
  130. package/dist/bulk-scan-discovery.js.map +1 -1
  131. package/dist/classify-scan-severity.d.ts.map +1 -1
  132. package/dist/classify-scan-severity.js +2 -5
  133. package/dist/classify-scan-severity.js.map +1 -1
  134. package/dist/classify-severity.d.ts +28 -17
  135. package/dist/classify-severity.d.ts.map +1 -1
  136. package/dist/classify-severity.js +16 -35
  137. package/dist/classify-severity.js.map +1 -1
  138. package/dist/cli-help.d.ts +3 -0
  139. package/dist/cli-help.d.ts.map +1 -0
  140. package/dist/cli-help.js +285 -0
  141. package/dist/cli-help.js.map +1 -0
  142. package/dist/cli-signals.d.ts +8 -0
  143. package/dist/cli-signals.d.ts.map +1 -0
  144. package/dist/cli-signals.js +28 -0
  145. package/dist/cli-signals.js.map +1 -0
  146. package/dist/cli.d.ts +22 -24
  147. package/dist/cli.d.ts.map +1 -1
  148. package/dist/cli.js +1660 -1556
  149. package/dist/cli.js.map +1 -1
  150. package/dist/cloud-publish.d.ts.map +1 -1
  151. package/dist/cloud-publish.js +11 -17
  152. package/dist/cloud-publish.js.map +1 -1
  153. package/dist/codeowners.d.ts +19 -0
  154. package/dist/codeowners.d.ts.map +1 -0
  155. package/dist/codeowners.js +138 -0
  156. package/dist/codeowners.js.map +1 -0
  157. package/dist/codex-prompt.d.ts.map +1 -1
  158. package/dist/codex-prompt.js +1 -2
  159. package/dist/codex-prompt.js.map +1 -1
  160. package/dist/codex-sdk-environment.d.ts +3 -0
  161. package/dist/codex-sdk-environment.d.ts.map +1 -0
  162. package/dist/codex-sdk-environment.js +68 -0
  163. package/dist/codex-sdk-environment.js.map +1 -0
  164. package/dist/component-plan.d.ts +2 -5
  165. package/dist/component-plan.d.ts.map +1 -1
  166. package/dist/component-plan.js +38 -34
  167. package/dist/component-plan.js.map +1 -1
  168. package/dist/component-scan.d.ts +7 -5
  169. package/dist/component-scan.d.ts.map +1 -1
  170. package/dist/component-scan.js +54 -44
  171. package/dist/component-scan.js.map +1 -1
  172. package/dist/config.d.ts.map +1 -1
  173. package/dist/config.js +93 -104
  174. package/dist/config.js.map +1 -1
  175. package/dist/contract.d.ts +15 -2
  176. package/dist/contract.d.ts.map +1 -1
  177. package/dist/contract.js +124 -348
  178. package/dist/contract.js.map +1 -1
  179. package/dist/cost-model.d.ts +13 -6
  180. package/dist/cost-model.d.ts.map +1 -1
  181. package/dist/cost-model.js +75 -34
  182. package/dist/cost-model.js.map +1 -1
  183. package/dist/cost.d.ts +10 -2
  184. package/dist/cost.d.ts.map +1 -1
  185. package/dist/cost.js +116 -134
  186. package/dist/cost.js.map +1 -1
  187. package/dist/custom-validation-prompt.d.ts.map +1 -1
  188. package/dist/custom-validation-prompt.js +7 -6
  189. package/dist/custom-validation-prompt.js.map +1 -1
  190. package/dist/custom-validation.d.ts +1 -2
  191. package/dist/custom-validation.d.ts.map +1 -1
  192. package/dist/custom-validation.js +23 -16
  193. package/dist/custom-validation.js.map +1 -1
  194. package/dist/deduplication/checkpointed-review.d.ts +4 -2
  195. package/dist/deduplication/checkpointed-review.d.ts.map +1 -1
  196. package/dist/deduplication/checkpointed-review.js +33 -20
  197. package/dist/deduplication/checkpointed-review.js.map +1 -1
  198. package/dist/deduplication/codex-review.d.ts +9 -1
  199. package/dist/deduplication/codex-review.d.ts.map +1 -1
  200. package/dist/deduplication/codex-review.js +129 -20
  201. package/dist/deduplication/codex-review.js.map +1 -1
  202. package/dist/deduplication/deduplication-prompts.d.ts +5 -2
  203. package/dist/deduplication/deduplication-prompts.d.ts.map +1 -1
  204. package/dist/deduplication/deduplication-prompts.js +18 -4
  205. package/dist/deduplication/deduplication-prompts.js.map +1 -1
  206. package/dist/deduplication/deduplication-reviewer.d.ts +26 -4
  207. package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -1
  208. package/dist/deduplication/deduplication-reviewer.js +59 -47
  209. package/dist/deduplication/deduplication-reviewer.js.map +1 -1
  210. package/dist/deduplication/deduplication.d.ts.map +1 -1
  211. package/dist/deduplication/deduplication.js +59 -113
  212. package/dist/deduplication/deduplication.js.map +1 -1
  213. package/dist/deduplication/diagnostics.d.ts +17 -0
  214. package/dist/deduplication/diagnostics.d.ts.map +1 -0
  215. package/dist/deduplication/diagnostics.js +5 -0
  216. package/dist/deduplication/diagnostics.js.map +1 -0
  217. package/dist/deduplication/finding-schema.d.ts.map +1 -1
  218. package/dist/deduplication/finding-schema.js +7 -1
  219. package/dist/deduplication/finding-schema.js.map +1 -1
  220. package/dist/deduplication/local.d.ts +31 -0
  221. package/dist/deduplication/local.d.ts.map +1 -0
  222. package/dist/deduplication/local.js +166 -0
  223. package/dist/deduplication/local.js.map +1 -0
  224. package/dist/deduplication/records-protocol.d.ts.map +1 -1
  225. package/dist/deduplication/records-protocol.js +12 -28
  226. package/dist/deduplication/records-protocol.js.map +1 -1
  227. package/dist/deduplication/records.d.ts +188 -12
  228. package/dist/deduplication/records.d.ts.map +1 -1
  229. package/dist/deduplication/records.js +34 -24
  230. package/dist/deduplication/records.js.map +1 -1
  231. package/dist/deduplication/review.d.ts +7 -0
  232. package/dist/deduplication/review.d.ts.map +1 -1
  233. package/dist/deduplication/review.js.map +1 -1
  234. package/dist/deduplication/scan.d.ts +9 -3
  235. package/dist/deduplication/scan.d.ts.map +1 -1
  236. package/dist/deduplication/scan.js +159 -44
  237. package/dist/deduplication/scan.js.map +1 -1
  238. package/dist/deep-config.d.ts +2 -1
  239. package/dist/deep-config.d.ts.map +1 -1
  240. package/dist/deep-config.js +7 -23
  241. package/dist/deep-config.js.map +1 -1
  242. package/dist/deep-progress.d.ts +2 -1
  243. package/dist/deep-progress.d.ts.map +1 -1
  244. package/dist/deep-progress.js +32 -47
  245. package/dist/deep-progress.js.map +1 -1
  246. package/dist/deep-scan.d.ts +20 -0
  247. package/dist/deep-scan.d.ts.map +1 -0
  248. package/dist/deep-scan.js +113 -0
  249. package/dist/deep-scan.js.map +1 -0
  250. package/dist/errors.d.ts +5 -2
  251. package/dist/errors.d.ts.map +1 -1
  252. package/dist/errors.js +7 -7
  253. package/dist/errors.js.map +1 -1
  254. package/dist/feedback.d.ts.map +1 -1
  255. package/dist/feedback.js +53 -21
  256. package/dist/feedback.js.map +1 -1
  257. package/dist/finding-catalogue.d.ts.map +1 -1
  258. package/dist/finding-catalogue.js +26 -30
  259. package/dist/finding-catalogue.js.map +1 -1
  260. package/dist/finding-normalization.d.ts +5 -0
  261. package/dist/finding-normalization.d.ts.map +1 -0
  262. package/dist/finding-normalization.js +120 -0
  263. package/dist/finding-normalization.js.map +1 -0
  264. package/dist/finding-retrieval.d.ts +9 -0
  265. package/dist/finding-retrieval.d.ts.map +1 -1
  266. package/dist/finding-retrieval.js.map +1 -1
  267. package/dist/finding-workflow.d.ts +13 -8
  268. package/dist/finding-workflow.d.ts.map +1 -1
  269. package/dist/finding-workflow.js +18 -13
  270. package/dist/finding-workflow.js.map +1 -1
  271. package/dist/findings-client.d.ts +2 -1
  272. package/dist/findings-client.d.ts.map +1 -1
  273. package/dist/findings-client.js +36 -9
  274. package/dist/findings-client.js.map +1 -1
  275. package/dist/findings-errors.d.ts +16 -0
  276. package/dist/findings-errors.d.ts.map +1 -0
  277. package/dist/findings-errors.js +24 -0
  278. package/dist/findings-errors.js.map +1 -0
  279. package/dist/findings-import.d.ts +1 -1
  280. package/dist/findings-import.d.ts.map +1 -1
  281. package/dist/findings-import.js +16 -44
  282. package/dist/findings-import.js.map +1 -1
  283. package/dist/github.d.ts.map +1 -1
  284. package/dist/github.js +9 -4
  285. package/dist/github.js.map +1 -1
  286. package/dist/import-scan.d.ts.map +1 -1
  287. package/dist/import-scan.js +49 -39
  288. package/dist/import-scan.js.map +1 -1
  289. package/dist/index.d.ts +11 -5
  290. package/dist/index.d.ts.map +1 -1
  291. package/dist/index.js +2 -1
  292. package/dist/index.js.map +1 -1
  293. package/dist/knowledge-base.d.ts +7 -1
  294. package/dist/knowledge-base.d.ts.map +1 -1
  295. package/dist/knowledge-base.js +115 -47
  296. package/dist/knowledge-base.js.map +1 -1
  297. package/dist/linear.d.ts.map +1 -1
  298. package/dist/linear.js +4 -10
  299. package/dist/linear.js.map +1 -1
  300. package/dist/mock-scan.d.ts.map +1 -1
  301. package/dist/mock-scan.js +0 -1
  302. package/dist/mock-scan.js.map +1 -1
  303. package/dist/models.d.ts +24 -2
  304. package/dist/models.d.ts.map +1 -1
  305. package/dist/models.js.map +1 -1
  306. package/dist/multiscan.d.ts +9 -3
  307. package/dist/multiscan.d.ts.map +1 -1
  308. package/dist/multiscan.js +225 -153
  309. package/dist/multiscan.js.map +1 -1
  310. package/dist/owner-evidence.d.ts +10 -2
  311. package/dist/owner-evidence.d.ts.map +1 -1
  312. package/dist/owner-evidence.js +69 -14
  313. package/dist/owner-evidence.js.map +1 -1
  314. package/dist/patch-tui.d.ts.map +1 -1
  315. package/dist/patch-tui.js +5 -6
  316. package/dist/patch-tui.js.map +1 -1
  317. package/dist/project-config-schema.d.ts +10 -0
  318. package/dist/project-config-schema.d.ts.map +1 -1
  319. package/dist/project-config-schema.js +1 -0
  320. package/dist/project-config-schema.js.map +1 -1
  321. package/dist/project-config.d.ts +1 -0
  322. package/dist/project-config.d.ts.map +1 -1
  323. package/dist/project-config.js +5 -3
  324. package/dist/project-config.js.map +1 -1
  325. package/dist/prompt-files.d.ts +0 -1
  326. package/dist/prompt-files.d.ts.map +1 -1
  327. package/dist/prompt-files.js +2 -4
  328. package/dist/prompt-files.js.map +1 -1
  329. package/dist/provider-profile.d.ts +21 -0
  330. package/dist/provider-profile.d.ts.map +1 -0
  331. package/dist/provider-profile.js +133 -0
  332. package/dist/provider-profile.js.map +1 -0
  333. package/dist/publication-events.d.ts +6 -8
  334. package/dist/publication-events.d.ts.map +1 -1
  335. package/dist/publication-events.js +40 -79
  336. package/dist/publication-events.js.map +1 -1
  337. package/dist/publication-store.d.ts.map +1 -1
  338. package/dist/publication-store.js +48 -76
  339. package/dist/publication-store.js.map +1 -1
  340. package/dist/publication.d.ts +1 -2
  341. package/dist/publication.d.ts.map +1 -1
  342. package/dist/publication.js +7 -16
  343. package/dist/publication.js.map +1 -1
  344. package/dist/publish.d.ts +1 -5
  345. package/dist/publish.d.ts.map +1 -1
  346. package/dist/publish.js +138 -199
  347. package/dist/publish.js.map +1 -1
  348. package/dist/record.d.ts +2 -0
  349. package/dist/record.d.ts.map +1 -0
  350. package/dist/record.js +4 -0
  351. package/dist/record.js.map +1 -0
  352. package/dist/request-metadata.d.ts +9 -0
  353. package/dist/request-metadata.d.ts.map +1 -0
  354. package/dist/request-metadata.js +13 -0
  355. package/dist/request-metadata.js.map +1 -0
  356. package/dist/result.d.ts +4 -1
  357. package/dist/result.d.ts.map +1 -1
  358. package/dist/result.js +7 -0
  359. package/dist/result.js.map +1 -1
  360. package/dist/runtime.d.ts +25 -17
  361. package/dist/runtime.d.ts.map +1 -1
  362. package/dist/runtime.js +619 -255
  363. package/dist/runtime.js.map +1 -1
  364. package/dist/saved-scan-bootstrap.d.ts +14 -0
  365. package/dist/saved-scan-bootstrap.d.ts.map +1 -0
  366. package/dist/saved-scan-bootstrap.js +261 -0
  367. package/dist/saved-scan-bootstrap.js.map +1 -0
  368. package/dist/saved-scan.d.ts +1 -1
  369. package/dist/saved-scan.d.ts.map +1 -1
  370. package/dist/saved-scan.js +16 -32
  371. package/dist/saved-scan.js.map +1 -1
  372. package/dist/scan-activity.d.ts.map +1 -1
  373. package/dist/scan-activity.js +19 -30
  374. package/dist/scan-activity.js.map +1 -1
  375. package/dist/scan-comparison.d.ts +31 -18
  376. package/dist/scan-comparison.d.ts.map +1 -1
  377. package/dist/scan-comparison.js +307 -347
  378. package/dist/scan-comparison.js.map +1 -1
  379. package/dist/scan-dashboard.d.ts +3 -1
  380. package/dist/scan-dashboard.d.ts.map +1 -1
  381. package/dist/scan-dashboard.js +351 -92
  382. package/dist/scan-dashboard.js.map +1 -1
  383. package/dist/scan-history-renderer.d.ts.map +1 -1
  384. package/dist/scan-history-renderer.js +55 -15
  385. package/dist/scan-history-renderer.js.map +1 -1
  386. package/dist/scan-inputs.d.ts +6 -0
  387. package/dist/scan-inputs.d.ts.map +1 -0
  388. package/dist/scan-inputs.js +71 -0
  389. package/dist/scan-inputs.js.map +1 -0
  390. package/dist/scan-logs.d.ts +2 -3
  391. package/dist/scan-logs.d.ts.map +1 -1
  392. package/dist/scan-logs.js +14 -12
  393. package/dist/scan-logs.js.map +1 -1
  394. package/dist/scan-sessions.d.ts +4 -0
  395. package/dist/scan-sessions.d.ts.map +1 -1
  396. package/dist/scan-sessions.js +18 -3
  397. package/dist/scan-sessions.js.map +1 -1
  398. package/dist/scan-settings.d.ts +5 -0
  399. package/dist/scan-settings.d.ts.map +1 -1
  400. package/dist/scan-settings.js +4 -0
  401. package/dist/scan-settings.js.map +1 -1
  402. package/dist/scan-skill-paths.d.ts +8 -0
  403. package/dist/scan-skill-paths.d.ts.map +1 -0
  404. package/dist/scan-skill-paths.js +47 -0
  405. package/dist/scan-skill-paths.js.map +1 -0
  406. package/dist/security-policy-cli.d.ts.map +1 -1
  407. package/dist/security-policy-cli.js +15 -33
  408. package/dist/security-policy-cli.js.map +1 -1
  409. package/dist/security-policy.d.ts +17 -9
  410. package/dist/security-policy.d.ts.map +1 -1
  411. package/dist/security-policy.js +131 -98
  412. package/dist/security-policy.js.map +1 -1
  413. package/dist/server/api.d.ts +0 -1
  414. package/dist/server/api.d.ts.map +1 -1
  415. package/dist/server/api.js +0 -1
  416. package/dist/server/api.js.map +1 -1
  417. package/dist/server/embeddings.d.ts +3 -1
  418. package/dist/server/embeddings.d.ts.map +1 -1
  419. package/dist/server/embeddings.js +11 -3
  420. package/dist/server/embeddings.js.map +1 -1
  421. package/dist/server/errors.d.ts +5 -2
  422. package/dist/server/errors.d.ts.map +1 -1
  423. package/dist/server/errors.js +7 -0
  424. package/dist/server/errors.js.map +1 -1
  425. package/dist/server/sqlite-store.d.ts +0 -2
  426. package/dist/server/sqlite-store.d.ts.map +1 -1
  427. package/dist/server/sqlite-store.js +55 -55
  428. package/dist/server/sqlite-store.js.map +1 -1
  429. package/dist/server/storage.d.ts +0 -2
  430. package/dist/server/storage.d.ts.map +1 -1
  431. package/dist/server/storage.js.map +1 -1
  432. package/dist/severity-store.js +20 -32
  433. package/dist/severity-store.js.map +1 -1
  434. package/dist/standalone-validation-prompt.d.ts +2 -0
  435. package/dist/standalone-validation-prompt.d.ts.map +1 -0
  436. package/dist/standalone-validation-prompt.js +10 -0
  437. package/dist/standalone-validation-prompt.js.map +1 -0
  438. package/dist/suggest-owners.d.ts +6 -5
  439. package/dist/suggest-owners.d.ts.map +1 -1
  440. package/dist/suggest-owners.js +15 -10
  441. package/dist/suggest-owners.js.map +1 -1
  442. package/dist/targets.d.ts +12 -0
  443. package/dist/targets.d.ts.map +1 -1
  444. package/dist/targets.js +124 -62
  445. package/dist/targets.js.map +1 -1
  446. package/dist/trusted-executable.d.ts +6 -1
  447. package/dist/trusted-executable.d.ts.map +1 -1
  448. package/dist/trusted-executable.js +28 -9
  449. package/dist/trusted-executable.js.map +1 -1
  450. package/dist/value.d.ts +2 -0
  451. package/dist/value.d.ts.map +1 -0
  452. package/dist/value.js +27 -0
  453. package/dist/value.js.map +1 -0
  454. package/dist/version.d.ts.map +1 -1
  455. package/dist/version.js +3 -6
  456. package/dist/version.js.map +1 -1
  457. package/dist/windows-path.d.ts.map +1 -1
  458. package/dist/windows-path.js +1 -2
  459. package/dist/windows-path.js.map +1 -1
  460. package/dist/workbench-arguments.d.ts +3 -0
  461. package/dist/workbench-arguments.d.ts.map +1 -0
  462. package/dist/workbench-arguments.js +15 -0
  463. package/dist/workbench-arguments.js.map +1 -0
  464. package/dist/worker-progress.d.ts +2 -0
  465. package/dist/worker-progress.d.ts.map +1 -1
  466. package/dist/worker-progress.js +40 -46
  467. package/dist/worker-progress.js.map +1 -1
  468. package/docs/cli.md +1778 -0
  469. package/docs/dedupe-records.md +24 -0
  470. package/docs/findings-service.md +402 -0
  471. package/package.json +23 -23
  472. package/schemas/project-config.schema.json +5 -0
  473. package/_bundled_plugin/schemas/tools/worker-threat-model.schema.json +0 -18
  474. package/_bundled_plugin/scripts/normalize_candidates.py +0 -338
  475. package/_bundled_plugin/scripts/workbench_dashboard.py +0 -159
  476. package/_bundled_plugin/scripts/workbench_findings.py +0 -221
  477. package/_bundled_plugin/scripts/workbench_severity.py +0 -119
  478. package/_bundled_plugin/skills/assess-patch-risk/scripts/validate_patch_risk_assessment.py +0 -163
  479. package/dist/server/dashboard/THIRD_PARTY_NOTICES.txt +0 -276
  480. package/dist/server/dashboard/app.css +0 -2
  481. package/dist/server/dashboard/app.js +0 -66
  482. package/dist/server/dashboard/index.html +0 -18
  483. package/dist/server/dashboard-types.d.ts +0 -48
  484. package/dist/server/dashboard-types.d.ts.map +0 -1
  485. package/dist/server/dashboard-types.js +0 -2
  486. package/dist/server/dashboard-types.js.map +0 -1
  487. package/dist/server/dashboard.d.ts +0 -6
  488. package/dist/server/dashboard.d.ts.map +0 -1
  489. package/dist/server/dashboard.js +0 -59
  490. package/dist/server/dashboard.js.map +0 -1
  491. package/dist/server/findings-service.d.ts +0 -20
  492. package/dist/server/findings-service.d.ts.map +0 -1
  493. package/dist/server/findings-service.js +0 -31
  494. package/dist/server/findings-service.js.map +0 -1
  495. package/dist/server/index.d.ts +0 -2
  496. package/dist/server/index.d.ts.map +0 -1
  497. package/dist/server/index.js +0 -6
  498. package/dist/server/index.js.map +0 -1
  499. package/dist/server/routes.d.ts +0 -6
  500. package/dist/server/routes.d.ts.map +0 -1
  501. package/dist/server/routes.js +0 -86
  502. package/dist/server/routes.js.map +0 -1
  503. package/dist/server/serve.d.ts +0 -2
  504. package/dist/server/serve.d.ts.map +0 -1
  505. package/dist/server/serve.js +0 -28
  506. package/dist/server/serve.js.map +0 -1
  507. package/dist/server/server.d.ts +0 -10
  508. package/dist/server/server.d.ts.map +0 -1
  509. package/dist/server/server.js +0 -17
  510. package/dist/server/server.js.map +0 -1
  511. package/dist/server/validation.d.ts +0 -17
  512. package/dist/server/validation.d.ts.map +0 -1
  513. package/dist/server/validation.js +0 -53
  514. package/dist/server/validation.js.map +0 -1
@@ -16,9 +16,12 @@ import sys
16
16
  import tempfile
17
17
  import time
18
18
  import uuid
19
- from contextlib import closing, contextmanager
19
+ from collections.abc import Callable
20
+ from contextlib import closing, contextmanager, nullcontext
20
21
  from datetime import datetime, timedelta, timezone
22
+ from functools import partial
21
23
  from pathlib import Path, PurePosixPath
24
+ from types import SimpleNamespace
22
25
  from typing import Any
23
26
 
24
27
  try:
@@ -36,17 +39,10 @@ import deep_scan_workbench as deep_scan
36
39
  import workbench_native_indexes as native_indexes
37
40
  import workbench_progress as progress
38
41
  import workbench_publication as publication
39
- import workbench_remediation as remediation
42
+ import workbench_remediation as remediation_state
40
43
  import workbench_saved_results as saved_results
41
44
  import workbench_scan_history as scan_history
42
45
  import workbench_scan_usage as scan_usage
43
- import workbench_severity as severity
44
- from filesystem_identity import (
45
- serialize_filesystem_identity as serialize_filesystem_identity,
46
- )
47
- from filesystem_identity import (
48
- stored_filesystem_identity_matches as stored_filesystem_identity_matches,
49
- )
50
46
  from finalize_scan_contract import (
51
47
  PRODUCER_NAME,
52
48
  ContractError,
@@ -60,7 +56,7 @@ from finalize_scan_contract import (
60
56
  )
61
57
  from finding_preview import bounded_finding_details
62
58
  from workbench import handoff
63
- from workbench.storage import resolve_scan_root, state_dir
59
+ from workbench.storage import create_private_directory, resolve_scan_root, state_dir
64
60
  from workbench_cli import parse_args
65
61
  from workbench_constants import (
66
62
  ARTIFACTS,
@@ -81,21 +77,12 @@ from workbench_constants import (
81
77
  PATCH_PREVIEW_BYTES,
82
78
  SQLITE_RETRY_ATTEMPTS,
83
79
  )
84
- from workbench_dashboard import dashboard
85
80
  from workbench_feedback import get_scan_feedback
86
81
  from workbench_finding_index import index_findings
87
82
  from workbench_finding_workflows import finding_workflow, register_workflow_scan
88
- from workbench_findings import (
89
- find_potential_duplicates,
90
- list_dedupe_groups,
91
- list_stored_findings,
92
- store_dedupe_groups,
93
- store_findings,
94
- )
95
- from workbench_remediation import remediation_claim_is_active
83
+ from workbench_local_dedupe import local_dedupe
96
84
  from workbench_scan_start import (
97
85
  archive_scan,
98
- compact_timestamp,
99
86
  insert_running_scan,
100
87
  safe_segment,
101
88
  scan_diff_identity,
@@ -108,27 +95,26 @@ from workbench_schema import (
108
95
  from workbench_schema import (
109
96
  apply_migrations as apply_schema_migrations,
110
97
  )
111
- from workbench_schema import (
112
- sql_statements as sql_statements,
113
- )
114
98
  from workbench_source_excerpt import finding_source_excerpt, safe_source_path
115
99
  from workbench_target import (
116
100
  clean_worktree_content_digest,
117
101
  copy_directory_excluding,
118
102
  copy_git_worktree_files,
103
+ diff_snapshot_digest,
119
104
  directory_content_digest,
120
105
  directory_snapshot_regular_file_count,
121
106
  git_bytes,
122
107
  git_command,
123
108
  git_output,
124
109
  git_revision,
125
- git_submodule_paths,
110
+ git_submodule_entries,
126
111
  git_target_metadata,
127
112
  git_worktree_context,
128
113
  remediation_checkout_snapshot,
129
114
  require_git_worktree_head,
130
115
  require_remediation_target,
131
116
  require_scan_target_identity,
117
+ restore_directory_junctions,
132
118
  scan_target_warning,
133
119
  worktree_content_digest,
134
120
  worktree_content_digest_for_context,
@@ -169,7 +155,7 @@ def database_path() -> Path:
169
155
  @contextmanager
170
156
  def scan_completion_lock(scan_id: str) -> Any:
171
157
  lock_dir = state_dir() / "completion-locks"
172
- lock_dir.mkdir(parents=True, exist_ok=True)
158
+ create_private_directory(lock_dir)
173
159
  lock_path = lock_dir / f"{require_uuid(scan_id, 'scan-id')}.lock"
174
160
  descriptor = os.open(
175
161
  lock_path,
@@ -230,29 +216,46 @@ def release_completion_file_lock(descriptor: int) -> None:
230
216
  windows_file_lock.locking(descriptor, windows_file_lock.LK_UNLCK, 1)
231
217
 
232
218
 
233
- def connect() -> sqlite3.Connection:
219
+ def connect(*, deferred: bool = False) -> sqlite3.Connection:
234
220
  path = database_path()
235
- path.parent.mkdir(parents=True, exist_ok=True)
221
+ guidance = (
222
+ f"Workbench database: {path}. Ensure the state directory and SQLite journal files "
223
+ "are writable, or set CODEX_SECURITY_STATE_DIR to a writable directory outside "
224
+ "the scanned repository."
225
+ )
226
+ try:
227
+ create_private_directory(path.parent)
228
+ except OSError:
229
+ print(guidance, file=sys.stderr)
230
+ raise
236
231
  for attempt in range(SQLITE_RETRY_ATTEMPTS):
237
- connection = sqlite3.connect(path, timeout=5)
232
+ try:
233
+ connection = sqlite3.connect(path, timeout=5)
234
+ except sqlite3.OperationalError:
235
+ print(guidance, file=sys.stderr)
236
+ raise
238
237
  try:
239
238
  connection.row_factory = sqlite3.Row
240
239
  connection.execute("PRAGMA foreign_keys = ON")
241
240
  connection.execute("PRAGMA busy_timeout = 5000")
242
- apply_migrations(connection)
241
+ # Keep writer admission retries; readers only wait when a repair needs to write.
242
+ apply_migrations(connection, immediate=not deferred or attempt > 0)
243
243
  connection.execute("PRAGMA journal_mode = WAL")
244
244
  path.chmod(0o600)
245
245
  return connection
246
246
  except sqlite3.OperationalError as exc:
247
247
  connection.close()
248
248
  if attempt == SQLITE_RETRY_ATTEMPTS - 1 or not sqlite_busy(exc):
249
+ print(guidance, file=sys.stderr)
249
250
  raise
250
251
  time.sleep(0.05 * (2**attempt))
251
252
  raise AssertionError("SQLite retry loop exhausted unexpectedly.")
252
253
 
253
254
 
254
- def apply_migrations(connection: sqlite3.Connection) -> None:
255
- apply_schema_migrations(connection, MIGRATIONS, now, backfill_security_targets)
255
+ def apply_migrations(connection: sqlite3.Connection, *, immediate: bool = False) -> None:
256
+ apply_schema_migrations(
257
+ connection, MIGRATIONS, now, backfill_security_targets, immediate=immediate
258
+ )
256
259
 
257
260
 
258
261
  def require_target(value: str) -> Path:
@@ -326,7 +329,11 @@ def require_diff_target(
326
329
  if commit is None:
327
330
  raise SystemExit(f"Commit is not available in the local checkout: {head}")
328
331
  parent_line = next(
329
- (line for line in commit.splitlines() if line.startswith(b"parent ")),
332
+ (
333
+ line
334
+ for line in commit.split(b"\n\n", 1)[0].splitlines()
335
+ if line.startswith(b"parent ")
336
+ ),
330
337
  None,
331
338
  )
332
339
  if parent_line is None:
@@ -395,22 +402,10 @@ def inspect_setup_values(
395
402
  }
396
403
 
397
404
 
398
- def inspect_setup(args: argparse.Namespace) -> dict[str, Any]:
399
- return inspect_setup_values(
400
- args.target_path,
401
- args.scope,
402
- args.mode,
403
- args.diff_target_kind,
404
- args.diff_base_revision,
405
- args.diff_head_revision,
406
- args.diff_content_digest,
407
- )
408
-
409
-
410
405
  def require_review_changes_target(target: Path) -> str:
411
406
  revision = require_git_worktree_head(target)
412
407
  repository_root = git_output(target, "rev-parse", "--show-toplevel")
413
- if repository_root is None or Path(repository_root).resolve() != target:
408
+ if repository_root is None or not Path(repository_root).samefile(target):
414
409
  raise SystemExit(
415
410
  "Review changes requires the checked-out Git repository root as the target."
416
411
  )
@@ -446,6 +441,20 @@ def requested_scan_paths(scan: sqlite3.Row) -> list[str]:
446
441
  return [scan["scope"]]
447
442
 
448
443
 
444
+ def requested_scan_file_paths(scan: sqlite3.Row) -> list[str]:
445
+ # Older scans did not record path kinds. Retain their lexical scope semantics
446
+ # rather than infer the original target from a possibly changed checkout.
447
+ if scan["recipe_json"] is None:
448
+ return []
449
+ recipe = json.loads(scan["recipe_json"], parse_constant=reject_non_finite_json)
450
+ return recipe.get("scopeFilePaths", [])
451
+
452
+
453
+ def requested_finding_scope(scan: sqlite3.Row) -> list[tuple[str, bool]]:
454
+ files = {PurePosixPath(path) for path in requested_scan_file_paths(scan)}
455
+ return [(path, PurePosixPath(path) in files) for path in requested_scan_paths(scan)]
456
+
457
+
449
458
  def scan_contract(scan: sqlite3.Row) -> dict[str, Any]:
450
459
  target = Path(scan["target_path"])
451
460
  target_contract = {
@@ -467,6 +476,7 @@ def scan_contract(scan: sqlite3.Row) -> dict[str, Any]:
467
476
  "scope": {
468
477
  "requiredExcludePaths": [],
469
478
  "requestedPath": scan["scope"],
479
+ "filePaths": requested_scan_file_paths(scan),
470
480
  **(
471
481
  {"requiredIncludePaths": requested_scan_paths(scan)}
472
482
  if scan["mode"] != "diff"
@@ -514,8 +524,8 @@ def workbench_completion_binding(
514
524
  if scan["mode"] == "diff":
515
525
  target["baseRevision"] = scan["diff_base_revision"]
516
526
  target["headRevision"] = scan["diff_head_revision"]
517
- if scan["diff_target_kind"] == "working_tree" and scan["diff_content_digest"]:
518
- target["snapshotDigest"] = scan["diff_content_digest"]
527
+ if snapshot_digest := diff_snapshot_digest(scan, manifest):
528
+ target["snapshotDigest"] = snapshot_digest
519
529
  else:
520
530
  if scan["target_revision"] != "unversioned":
521
531
  target["revision"] = scan["target_revision"]
@@ -612,7 +622,7 @@ def verify_manifest_binding(scan: sqlite3.Row, manifest: dict[str, Any]) -> None
612
622
 
613
623
 
614
624
  def require_scope(scope: str, mode: str, target: Path) -> str:
615
- value = scope.strip() or "."
625
+ value = scope if scope.strip() else "."
616
626
  requested_scope = Path(value)
617
627
  if "\\" in value and (os.name != "nt" or not requested_scope.is_absolute()):
618
628
  raise SystemExit("Scan scope must use repository-relative POSIX paths.")
@@ -626,6 +636,8 @@ def require_scope(scope: str, mode: str, target: Path) -> str:
626
636
  except (RuntimeError, ValueError) as exc:
627
637
  raise SystemExit("Scan scope must stay inside the scanned target.") from exc
628
638
  normalized = relative_scope.as_posix() or "."
639
+ if not normalized.strip():
640
+ normalized = "./" + normalized
629
641
  if mode == "deep" and normalized != ".":
630
642
  raise SystemExit("Deep Scan is repository-wide and cannot use a scoped path.")
631
643
  if not resolved_scope.is_dir():
@@ -671,8 +683,10 @@ def resolve_scan_id(connection: sqlite3.Connection, scan_id: str) -> str:
671
683
  def create_workspace(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
672
684
  workspace_id = require_uuid(args.workspace_id, "workspace-id")
673
685
  timestamp = now()
674
- target_path = optional_text(args.target_path, maximum=4096)
675
- default_scope = optional_text(args.scope, maximum=4096) or "."
686
+ target_path = args.target_path if optional_text(args.target_path, maximum=4096) else None
687
+ default_scope = args.scope if optional_text(args.scope, maximum=4096) else "."
688
+ if any(len(value) > 4096 for value in (target_path, default_scope) if value is not None):
689
+ raise SystemExit("Text value must be no longer than 4096 characters.")
676
690
  diff_target_kind = args.diff_target_kind if args.mode == "diff" else None
677
691
  diff_base_revision = (
678
692
  optional_text(args.diff_base_revision, maximum=512) if args.mode == "diff" else None
@@ -702,6 +716,7 @@ def create_workspace(connection: sqlite3.Connection, args: argparse.Namespace) -
702
716
  diff_head_revision = inspected["diffTarget"]["headRevision"]
703
717
  diff_content_digest = inspected["diffTarget"].get("contentDigest")
704
718
  except SystemExit:
719
+ # Preserve an incomplete setup so workspace_state can report its validation error.
705
720
  pass
706
721
  with connection:
707
722
  target_id = (
@@ -785,10 +800,7 @@ def save_workspace(connection: sqlite3.Connection, args: argparse.Namespace) ->
785
800
  scope,
786
801
  args.mode,
787
802
  user_context_argument(args),
788
- diff_target["kind"] if diff_target else None,
789
- diff_target["baseRevision"] if diff_target else None,
790
- diff_target["headRevision"] if diff_target else None,
791
- diff_target.get("contentDigest") if diff_target else None,
803
+ *scan_diff_identity(diff_target or None),
792
804
  timestamp,
793
805
  workspace["id"],
794
806
  ),
@@ -855,7 +867,7 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
855
867
  metadata=target_metadata,
856
868
  )
857
869
  target_root = scan_target_root(args.scan_root, target)
858
- target_root.mkdir(parents=True, exist_ok=True)
870
+ create_private_directory(target_root)
859
871
  if manages_transaction:
860
872
  connection.execute("BEGIN IMMEDIATE")
861
873
  workspace = require_workspace(connection, workspace_id)
@@ -918,18 +930,6 @@ def start_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict
918
930
  return workspace_state(connection, workspace["id"])
919
931
 
920
932
 
921
- def start_prompt_only_scan(
922
- connection: sqlite3.Connection, args: argparse.Namespace
923
- ) -> dict[str, Any]:
924
- return _start_prompt_driven_scan(connection, args, headless_standard=False)
925
-
926
-
927
- def start_headless_standard_scan(
928
- connection: sqlite3.Connection, args: argparse.Namespace
929
- ) -> dict[str, Any]:
930
- return _start_prompt_driven_scan(connection, args, headless_standard=True)
931
-
932
-
933
933
  def _start_prompt_driven_scan(
934
934
  connection: sqlite3.Connection, args: argparse.Namespace, *, headless_standard: bool
935
935
  ) -> dict[str, Any]:
@@ -960,130 +960,148 @@ def _start_prompt_driven_scan(
960
960
  target_identity = scan_target_identity(target, diff_target)
961
961
  target_root = scan_target_root(args.scan_root, target)
962
962
 
963
- connection.execute("BEGIN IMMEDIATE")
964
- try:
965
- current_target = require_remediation_target(target_path)
966
- current_diff_target = (
967
- require_diff_target(
968
- current_target,
969
- args.diff_target_kind,
970
- args.diff_base_revision,
971
- args.diff_head_revision,
972
- args.diff_content_digest,
973
- )
974
- if args.mode == "diff"
975
- else None
976
- )
977
- if (
978
- scan_target_identity(current_target, current_diff_target) != target_identity
979
- or scan_diff_identity(current_diff_target) != diff_identity
980
- ):
981
- raise SystemExit(
982
- "The selected scan target changed while the scan was starting. Try again."
963
+ busy_timeout = int(connection.execute("PRAGMA busy_timeout").fetchone()[0])
964
+ for attempt in range(SQLITE_RETRY_ATTEMPTS):
965
+ try:
966
+ # Wait before hashing, then release the writer slot while reading the target.
967
+ connection.execute("BEGIN IMMEDIATE")
968
+ connection.rollback()
969
+ current_target = require_remediation_target(target_path)
970
+ current_diff_target = (
971
+ require_diff_target(
972
+ current_target,
973
+ args.diff_target_kind,
974
+ args.diff_base_revision,
975
+ args.diff_head_revision,
976
+ args.diff_content_digest,
977
+ )
978
+ if args.mode == "diff"
979
+ else None
983
980
  )
984
- existing = connection.execute(
985
- """
986
- SELECT scans.* FROM scans
987
- JOIN workspaces ON workspaces.active_scan_id = scans.id
988
- WHERE workspaces.thread_id = ? AND workspaces.target_path = ?
989
- AND workspaces.default_scope = ? AND workspaces.default_mode = ?
990
- AND workspaces.user_context IS ? AND workspaces.target_summary IS ?
991
- AND workspaces.diff_target_kind IS ? AND workspaces.diff_base_revision IS ?
992
- AND workspaces.diff_head_revision IS ? AND workspaces.diff_content_digest IS ?
993
- AND workspaces.submitted = 1 AND scans.target_revision = ?
994
- AND scans.target_snapshot_digest IS ? AND scans.target_device = ?
995
- AND scans.target_inode = ? AND scans.status = 'running'
996
- AND scans.handoff_status = 'delivered'
997
- AND (
998
- (? = 0 AND scans.handoff_claim_token IS NULL)
999
- OR (
1000
- ? = 1 AND scans.handoff_claim_token IS NOT NULL
1001
- AND scans.continuation_thread_id = ?
1002
- )
981
+ if (
982
+ scan_target_identity(current_target, current_diff_target) != target_identity
983
+ or scan_diff_identity(current_diff_target) != diff_identity
984
+ ):
985
+ raise SystemExit(
986
+ "The selected scan target changed while the scan was starting. Try again."
1003
987
  )
1004
- ORDER BY scans.updated_at DESC, scans.started_at DESC, scans.id LIMIT 1
1005
- """,
1006
- (
1007
- thread_id,
1008
- target_path,
1009
- scope,
1010
- args.mode,
1011
- user_context,
1012
- target_summary,
1013
- *diff_identity,
1014
- *target_identity,
1015
- int(headless_standard),
1016
- int(headless_standard),
1017
- thread_id,
1018
- ),
1019
- ).fetchone()
1020
- if existing is not None:
1021
- connection.commit()
1022
- return {
1023
- **scan_context(connection, existing["id"]),
1024
- "startDisposition": "joined",
1025
- }
1026
- target_root.mkdir(parents=True, exist_ok=True)
1027
- workspace_id = str(uuid.uuid4())
1028
- scan_id = str(uuid.uuid4())
1029
- timestamp = now()
1030
- target_id = ensure_security_target(connection, target_path)
1031
- connection.execute(
1032
- """
1033
- INSERT INTO workspaces (
1034
- id, thread_id, target_id, target_path, target_title, target_summary, default_scope,
1035
- default_mode, user_context, diff_target_kind, diff_base_revision,
1036
- diff_head_revision, diff_content_digest, submitted, created_at, updated_at
1037
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?)
1038
- """,
1039
- (
1040
- workspace_id,
1041
- thread_id,
1042
- target_id,
1043
- target_path,
1044
- target.name,
1045
- target_summary,
1046
- scope,
1047
- args.mode,
1048
- user_context,
1049
- *diff_identity,
1050
- timestamp,
1051
- timestamp,
1052
- ),
1053
- )
1054
- workspace = require_workspace(connection, workspace_id)
1055
- insert_running_scan(
1056
- connection,
1057
- scan_id=scan_id,
1058
- workspace=workspace,
1059
- target=target,
1060
- scope=scope,
1061
- diff_target=diff_target,
1062
- target_identity=target_identity,
1063
- target_root=target_root,
1064
- target_summary=target_summary,
1065
- scope_file_count=scope_file_count,
1066
- timestamp=timestamp,
1067
- handoff_status="delivered",
1068
- model=args.model,
1069
- reasoning_effort=args.reasoning_effort,
1070
- )
1071
- if headless_standard:
1072
- claimed = connection.execute(
988
+ # If another writer arrived during hashing, retry without waiting on a stale hash.
989
+ connection.execute("PRAGMA busy_timeout = 0")
990
+ try:
991
+ connection.execute("BEGIN IMMEDIATE")
992
+ finally:
993
+ connection.execute(f"PRAGMA busy_timeout = {busy_timeout}")
994
+ existing = connection.execute(
1073
995
  """
1074
- UPDATE scans
1075
- SET handoff_claim_token = ?, continuation_thread_id = ?
1076
- WHERE id = ? AND status = 'running' AND handoff_status = 'delivered'
1077
- AND handoff_claim_token IS NULL AND continuation_thread_id IS NULL
996
+ SELECT scans.* FROM scans
997
+ JOIN workspaces ON workspaces.active_scan_id = scans.id
998
+ WHERE workspaces.thread_id = ? AND workspaces.target_path = ?
999
+ AND workspaces.default_scope = ? AND workspaces.default_mode = ?
1000
+ AND workspaces.user_context IS ? AND workspaces.target_summary IS ?
1001
+ AND workspaces.diff_target_kind IS ? AND workspaces.diff_base_revision IS ?
1002
+ AND workspaces.diff_head_revision IS ? AND workspaces.diff_content_digest IS ?
1003
+ AND workspaces.submitted = 1 AND scans.target_revision = ?
1004
+ AND scans.target_snapshot_digest IS ? AND scans.target_device = ?
1005
+ AND scans.target_inode = ? AND scans.status = 'running'
1006
+ AND scans.handoff_status = 'delivered'
1007
+ AND (
1008
+ (? = 0 AND scans.handoff_claim_token IS NULL)
1009
+ OR (
1010
+ ? = 1 AND scans.handoff_claim_token IS NOT NULL
1011
+ AND scans.continuation_thread_id = ?
1012
+ )
1013
+ )
1014
+ ORDER BY scans.updated_at DESC, scans.started_at DESC, scans.id LIMIT 1
1078
1015
  """,
1079
- (str(uuid.uuid4()), thread_id, scan_id),
1016
+ (
1017
+ thread_id,
1018
+ target_path,
1019
+ scope,
1020
+ args.mode,
1021
+ user_context,
1022
+ target_summary,
1023
+ *diff_identity,
1024
+ *target_identity,
1025
+ int(headless_standard),
1026
+ int(headless_standard),
1027
+ thread_id,
1028
+ ),
1029
+ ).fetchone()
1030
+ if existing is not None:
1031
+ connection.commit()
1032
+ return {
1033
+ **scan_context(connection, existing["id"]),
1034
+ "startDisposition": "joined",
1035
+ }
1036
+ create_private_directory(target_root)
1037
+ workspace_id = str(uuid.uuid4())
1038
+ scan_id = str(uuid.uuid4())
1039
+ timestamp = now()
1040
+ target_id = ensure_security_target(connection, target_path)
1041
+ connection.execute(
1042
+ """
1043
+ INSERT INTO workspaces (
1044
+ id, thread_id, target_id, target_path, target_title, target_summary, default_scope,
1045
+ default_mode, user_context, diff_target_kind, diff_base_revision,
1046
+ diff_head_revision, diff_content_digest, submitted, created_at, updated_at
1047
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?)
1048
+ """,
1049
+ (
1050
+ workspace_id,
1051
+ thread_id,
1052
+ target_id,
1053
+ target_path,
1054
+ target.name,
1055
+ target_summary,
1056
+ scope,
1057
+ args.mode,
1058
+ user_context,
1059
+ *diff_identity,
1060
+ timestamp,
1061
+ timestamp,
1062
+ ),
1080
1063
  )
1081
- if claimed.rowcount != 1:
1082
- raise SystemExit("Codex Security headless scan ownership could not be recorded.")
1083
- connection.commit()
1084
- except BaseException:
1085
- connection.rollback()
1086
- raise
1064
+ workspace = require_workspace(connection, workspace_id)
1065
+ insert_running_scan(
1066
+ connection,
1067
+ scan_id=scan_id,
1068
+ workspace=workspace,
1069
+ target=target,
1070
+ scope=scope,
1071
+ diff_target=diff_target,
1072
+ target_identity=target_identity,
1073
+ target_root=target_root,
1074
+ target_summary=target_summary,
1075
+ scope_file_count=scope_file_count,
1076
+ timestamp=timestamp,
1077
+ handoff_status="delivered",
1078
+ model=args.model,
1079
+ reasoning_effort=args.reasoning_effort,
1080
+ )
1081
+ if headless_standard:
1082
+ claimed = connection.execute(
1083
+ """
1084
+ UPDATE scans
1085
+ SET handoff_claim_token = ?, continuation_thread_id = ?
1086
+ WHERE id = ? AND status = 'running' AND handoff_status = 'delivered'
1087
+ AND handoff_claim_token IS NULL AND continuation_thread_id IS NULL
1088
+ """,
1089
+ (str(uuid.uuid4()), thread_id, scan_id),
1090
+ )
1091
+ if claimed.rowcount != 1:
1092
+ raise SystemExit(
1093
+ "Codex Security headless scan ownership could not be recorded."
1094
+ )
1095
+ connection.commit()
1096
+ break
1097
+ except sqlite3.OperationalError as exc:
1098
+ connection.rollback()
1099
+ if attempt == SQLITE_RETRY_ATTEMPTS - 1 or not sqlite_busy(exc):
1100
+ raise
1101
+ time.sleep(0.05 * (2**attempt))
1102
+ except BaseException:
1103
+ connection.rollback()
1104
+ raise
1087
1105
  return {**scan_context(connection, scan_id), "startDisposition": "created"}
1088
1106
 
1089
1107
 
@@ -1116,7 +1134,7 @@ def pin_legacy_manifest_digest(
1116
1134
  connection: sqlite3.Connection, scan_id: str, manifest_digest: str
1117
1135
  ) -> None:
1118
1136
  connection.execute("BEGIN IMMEDIATE")
1119
- try:
1137
+ with connection:
1120
1138
  scan = require_scan(connection, scan_id)
1121
1139
  current = scan["seal_manifest_digest"]
1122
1140
  if current is not None and current != manifest_digest:
@@ -1126,10 +1144,6 @@ def pin_legacy_manifest_digest(
1126
1144
  "UPDATE scans SET seal_manifest_digest = ? WHERE id = ?",
1127
1145
  (manifest_digest, scan["id"]),
1128
1146
  )
1129
- connection.commit()
1130
- except BaseException:
1131
- connection.rollback()
1132
- raise
1133
1147
 
1134
1148
 
1135
1149
  def complete_scan(
@@ -1326,12 +1340,11 @@ def budget_exhausted_draft(
1326
1340
  manifest = {
1327
1341
  "scan": {
1328
1342
  "target": target,
1329
- "scope": {"limitations": [warning], "validationMode": "incomplete"},
1343
+ "scope": {"limitations": [warning]},
1330
1344
  }
1331
1345
  }
1332
1346
  findings = {"findings": []}
1333
1347
  coverage = {
1334
- "completeness": "partial",
1335
1348
  "inventoryStrategy": (
1336
1349
  "scoped_path" if expected_coverage_mode(scan) == "scoped_path" else "repository"
1337
1350
  ),
@@ -1417,7 +1430,7 @@ def budget_exhausted_draft(
1417
1430
  "reason": "Validation was deferred because the scan reached its cost limit.",
1418
1431
  }
1419
1432
  )
1420
- coverage["completeness"] = "partial"
1433
+ coverage.pop("completeness", None)
1421
1434
  for name, payload in (
1422
1435
  ("findings.json", findings),
1423
1436
  ("coverage.json", coverage),
@@ -1444,22 +1457,9 @@ def complete_scan_locked(
1444
1457
  ) -> dict[str, Any]:
1445
1458
  scan = require_scan(connection, scan_id)
1446
1459
  if scan["status"] == "complete":
1447
- scan_dir = require_canonical_scan_directory(Path(scan["scan_dir"]))
1448
- require_recorded_manifest_digest(scan, scan_dir)
1449
- verify_manifest_binding(scan, read_json_object(scan_dir / ARTIFACTS["manifest"]))
1450
- try:
1451
- manifest, _, _ = finalize_scan(
1452
- scan_dir,
1453
- expected_coverage_mode=expected_coverage_mode(scan),
1454
- )
1455
- except ContractError as exc:
1456
- raise SystemExit(str(exc)) from exc
1457
- verify_manifest_binding(scan, manifest)
1458
- manifest_digest = published_manifest_digest(scan_dir, manifest)
1459
- pin_legacy_manifest_digest(connection, scan["id"], manifest_digest)
1460
- if cost_json is not None and scan["recipe_json"] is not None:
1461
- scan_usage.reconcile_completed_scan_cost(connection, scan, cost_json)
1462
- return scan_context(connection, scan["id"])
1460
+ return saved_results.refresh_completed_scan(
1461
+ _WORKBENCH_DB_CONTEXT, connection, scan, cost_json
1462
+ )
1463
1463
  if scan["status"] != "running":
1464
1464
  raise SystemExit("Only a running scan can be completed.")
1465
1465
  handoff.require_current_continuation(
@@ -1484,13 +1484,6 @@ def complete_scan_locked(
1484
1484
  current_manifest = None
1485
1485
  if current_manifest_path is not None:
1486
1486
  current_manifest = read_json_object(current_manifest_path)
1487
- if (
1488
- isinstance(current_manifest.get("scan"), dict)
1489
- and current_manifest["scan"].get("complete") is False
1490
- ):
1491
- raise SystemExit(
1492
- "The latest saved scan draft is incomplete; continue the scan before completing it."
1493
- )
1494
1487
  already_sealed = (
1495
1488
  current_manifest_path is not None
1496
1489
  and isinstance(current_manifest.get("scan"), dict)
@@ -1519,42 +1512,78 @@ def complete_scan_locked(
1519
1512
  f"{', '.join(missing_drafts)}. Check that the scan agent can run shell "
1520
1513
  "commands and write to the scan directory before retrying."
1521
1514
  )
1515
+ merge_parent_draft = (
1516
+ scan["mode"] != "deep" and current_manifest_path is not None and not already_sealed
1517
+ )
1522
1518
  wrote = False
1523
1519
  try:
1524
- prepared = _prepare_scan_finalization(
1525
- scan_dir,
1526
- expected_coverage_mode=expected_coverage_mode(scan),
1527
- completion_binding=completion_binding,
1528
- # Save the finished Deep result as submitted. Worker drafts and
1529
- # recovery repairs belong to the stopped-scan path.
1530
- completion_warnings=warnings if scan["mode"] != "deep" else None,
1531
- draft_documents=saved_results.merge_saved_results(
1520
+ with (
1521
+ saved_results.preserve_parent_head_on_error(scan_dir)
1522
+ if merge_parent_draft
1523
+ else nullcontext()
1524
+ ):
1525
+ draft_documents = (
1526
+ saved_results.merge_saved_results(
1527
+ scan_dir,
1528
+ scan["id"],
1529
+ completion_binding,
1530
+ connection.execute(
1531
+ "SELECT * FROM deep_scan_workers WHERE scan_id = ? ORDER BY created_at, id",
1532
+ (scan["id"],),
1533
+ ).fetchall(),
1534
+ warnings,
1535
+ stopped=False,
1536
+ reason="",
1537
+ )
1538
+ if merge_parent_draft
1539
+ else None
1540
+ )
1541
+ completion_manifest = draft_documents[0] if draft_documents else current_manifest
1542
+ if (
1543
+ completion_manifest is not None
1544
+ and isinstance(completion_manifest.get("scan"), dict)
1545
+ and completion_manifest["scan"].get("complete") is False
1546
+ ):
1547
+ raise RecoverableContractError(
1548
+ "The latest saved scan draft is incomplete; continue the scan before completing it."
1549
+ )
1550
+ prepared = _prepare_scan_finalization(
1532
1551
  scan_dir,
1533
- scan["id"],
1534
- completion_binding,
1535
- connection.execute(
1536
- "SELECT * FROM deep_scan_workers WHERE scan_id = ? ORDER BY created_at, id",
1537
- (scan["id"],),
1538
- ).fetchall(),
1539
- warnings,
1540
- stopped=False,
1541
- reason="",
1552
+ expected_coverage_mode=expected_coverage_mode(scan),
1553
+ completion_binding=completion_binding,
1554
+ recover_unsealed=scan["mode"] != "deep",
1555
+ allow_missing_writeups=True,
1556
+ draft_documents=draft_documents,
1557
+ finding_scope=requested_finding_scope(scan) if scan["mode"] == "deep" else None,
1542
1558
  )
1543
- if scan["mode"] != "deep" and current_manifest_path is not None and not already_sealed
1544
- else None,
1545
- )
1559
+ warnings.extend(warning for warning in prepared.warnings if warning not in warnings)
1546
1560
  add_warning()
1561
+ if prepared.warnings:
1562
+ # Save the explanation before publication removes the broken reference,
1563
+ # so a retry after failed publication retains it.
1564
+ with connection:
1565
+ connection.execute(
1566
+ "UPDATE scans SET completion_warnings_json = ? WHERE id = ? AND status = 'running'",
1567
+ (json.dumps(warnings), scan_id),
1568
+ )
1547
1569
  wrote = True
1548
- manifest, findings, _ = _write_prepared_scan_finalization(prepared)
1570
+ manifest, findings, _ = _write_prepared_scan_finalization(
1571
+ prepared, projection_warnings=warnings
1572
+ )
1549
1573
  except ContractError as exc:
1550
- if wrote or (
1574
+ # Only a coordinator-selected parent is fenced for output-write replay.
1575
+ selected_deep_parent = scan["mode"] == "deep" and deep_scan.require_deep_scan_run(
1576
+ connection, scan_id
1577
+ )["manifest_path"] == str(scan_dir / ARTIFACTS["manifest"])
1578
+ if (wrote and not selected_deep_parent) or (
1551
1579
  scan["mode"] == "deep"
1580
+ and not wrote
1552
1581
  and not already_sealed
1553
1582
  and not isinstance(exc, RecoverableContractError)
1554
1583
  ):
1555
1584
  args = argparse.Namespace(claim_token=claim_token, cost_json=cost_json)
1556
1585
  args.message, args.scan_id = str(exc), scan_id
1557
- fail_scan_locked(connection, args)
1586
+ saved_results.fail_scan_locked(_WORKBENCH_DB_CONTEXT, connection, args)
1558
1587
  raise SystemExit(str(exc)) from exc
1559
1588
  artifacts = {
1560
1589
  kind: artifact_path(scan_dir, filename, required=True)
@@ -1563,17 +1592,13 @@ def complete_scan_locked(
1563
1592
  manifest_digest = published_manifest_digest(scan_dir, manifest)
1564
1593
  if prepare_only:
1565
1594
  connection.execute("BEGIN IMMEDIATE")
1566
- try:
1595
+ with connection:
1567
1596
  updated = connection.execute(
1568
1597
  "UPDATE scans SET completion_warnings_json = ? WHERE id = ? AND status = 'running'",
1569
1598
  (json.dumps(warnings), scan["id"]),
1570
1599
  )
1571
1600
  if updated.rowcount != 1:
1572
1601
  raise SystemExit("Only a running scan can be prepared for completion.")
1573
- connection.commit()
1574
- except BaseException:
1575
- connection.rollback()
1576
- raise
1577
1602
  context = scan_context(connection, scan["id"])
1578
1603
  context["targetWarnings"] = target_warnings
1579
1604
  return context
@@ -1587,7 +1612,7 @@ def complete_scan_locked(
1587
1612
  )
1588
1613
  cost_json = parse_scan_cost(scan_usage.measured_scan_cost_json(measured_usage))
1589
1614
  connection.execute("BEGIN IMMEDIATE")
1590
- try:
1615
+ with connection:
1591
1616
  timestamp = manifest["scan"]["completedAt"]
1592
1617
  scan = require_scan(connection, scan["id"])
1593
1618
  if scan["status"] == "complete":
@@ -1639,24 +1664,36 @@ def complete_scan_locked(
1639
1664
  )
1640
1665
  if updated.rowcount != 1:
1641
1666
  raise SystemExit("Only a running scan can be completed.")
1642
- connection.commit()
1643
- except BaseException:
1644
- connection.rollback()
1645
- raise
1646
1667
  context = scan_context(connection, scan["id"])
1647
1668
  context["targetWarnings"] = target_warnings
1648
1669
  return context
1649
1670
 
1650
1671
 
1651
- def register_cli_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1652
- repository = require_target(args.repository)
1653
- require_scannable_target(repository)
1672
+ def require_cli_scan_directory(repository: Path, args: argparse.Namespace) -> Path:
1654
1673
  scan_dir = require_canonical_scan_directory(Path(args.scan_dir).expanduser())
1655
- if scan_dir == repository or repository in scan_dir.parents:
1656
- raise SystemExit("The scan artifact directory must be outside the selected target.")
1657
- if next(scan_dir.iterdir(), None) is not None:
1674
+ if scan_dir == repository or repository in scan_dir.parents or scan_dir in repository.parents:
1675
+ raise SystemExit(
1676
+ "The scan artifact directory must be outside and not contain the selected target."
1677
+ )
1678
+ if (not args.archive_existing or args.archived_scan_dir is not None) and next(
1679
+ scan_dir.iterdir(), None
1680
+ ) is not None:
1658
1681
  raise SystemExit("The scan artifact directory must be empty before the scan starts.")
1682
+ return scan_dir
1659
1683
 
1684
+
1685
+ def register_cli_scan(
1686
+ connection: sqlite3.Connection,
1687
+ args: argparse.Namespace,
1688
+ *,
1689
+ before_archive: Callable[[], None] | None = None,
1690
+ ) -> dict[str, Any]:
1691
+ repository = require_target(args.repository)
1692
+ require_scannable_target(repository)
1693
+ scan_dir = None
1694
+ if not args.archive_existing or args.archived_scan_dir is not None:
1695
+ # Preserve early output rejection when this call will not move the directory.
1696
+ scan_dir = require_cli_scan_directory(repository, args)
1660
1697
  user_context = None
1661
1698
  workflow_id = None
1662
1699
  if args.registration_json_stdin:
@@ -1669,6 +1706,10 @@ def register_cli_scan(connection: sqlite3.Connection, args: argparse.Namespace)
1669
1706
  recipe = parse_scan_recipe(recipe_json, repository)
1670
1707
  requested_target = recipe["target"]
1671
1708
  paths = requested_target["paths"]
1709
+ # Capture host-observed kinds once; resumes and publication use the saved recipe.
1710
+ recipe.pop("scopeFilePaths", None)
1711
+ if file_paths := [path for path in paths if (repository / path).is_file()]:
1712
+ recipe["scopeFilePaths"] = file_paths
1672
1713
  scope = paths[0] if len(paths) == 1 else "."
1673
1714
  diff_target = None
1674
1715
  if requested_target["kind"] in {"refs", "working_tree"}:
@@ -1707,66 +1748,80 @@ def register_cli_scan(connection: sqlite3.Connection, args: argparse.Namespace)
1707
1748
 
1708
1749
  connection.execute("BEGIN IMMEDIATE")
1709
1750
  try:
1710
- archive_scan(connection, args, scan_dir, timestamp, require_canonical_scan_directory)
1711
- target_id = ensure_security_target(connection, str(repository))
1712
- if parent_scan_id is not None:
1713
- parent = require_scan(connection, parent_scan_id)
1714
- if parent["target_id"] != target_id:
1715
- raise SystemExit("A rerun must belong to the same repository as its parent scan.")
1716
-
1717
- connection.execute(
1718
- """
1719
- INSERT INTO workspaces (
1720
- id, target_id, target_path, target_title, default_scope, default_mode,
1721
- diff_target_kind, diff_base_revision, diff_head_revision,
1722
- diff_content_digest, submitted, created_at, updated_at
1723
- ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?)
1724
- """,
1725
- (
1726
- workspace_id,
1727
- target_id,
1728
- str(repository),
1729
- repository.name,
1730
- scope,
1731
- mode,
1732
- *scan_diff_identity(diff_target),
1733
- timestamp,
1734
- timestamp,
1735
- ),
1736
- )
1737
- workspace = require_workspace(connection, workspace_id)
1738
- insert_running_scan(
1751
+ if scan_dir is None:
1752
+ # Host archiving temporarily removes the output directory. Wait for
1753
+ # the existing writer before inspecting its replacement.
1754
+ scan_dir = require_cli_scan_directory(repository, args)
1755
+ with archive_scan(
1739
1756
  connection,
1740
- scan_id=scan_id,
1741
- workspace=workspace,
1742
- target=repository,
1743
- scope=scope,
1744
- diff_target=diff_target,
1745
- target_identity=target_identity,
1746
- target_root=scan_dir.parent,
1747
- target_summary=None,
1748
- scope_file_count=scope_file_count,
1749
- timestamp=timestamp,
1750
- handoff_status="delivered",
1751
- scan_dir=scan_dir,
1752
- )
1753
- connection.execute(
1754
- "UPDATE scans SET recipe_json = ?, parent_scan_id = ?, user_context = ? WHERE id = ?",
1755
- (
1756
- json.dumps(recipe, allow_nan=False, separators=(",", ":"), sort_keys=True),
1757
- parent_scan_id,
1758
- user_context,
1759
- scan_id,
1760
- ),
1761
- )
1762
- if workflow_id is not None:
1763
- register_workflow_scan(connection, workflow_id, scan_id, str(scan_dir), timestamp)
1764
- connection.commit()
1757
+ args,
1758
+ scan_dir,
1759
+ timestamp,
1760
+ require_canonical_scan_directory,
1761
+ before_archive=before_archive,
1762
+ ) as archived_scan_dir:
1763
+ target_id = ensure_security_target(connection, str(repository))
1764
+ if parent_scan_id is not None:
1765
+ parent = require_scan(connection, parent_scan_id)
1766
+ if parent["target_id"] != target_id:
1767
+ raise SystemExit(
1768
+ "A rerun must belong to the same repository as its parent scan."
1769
+ )
1770
+
1771
+ connection.execute(
1772
+ """
1773
+ INSERT INTO workspaces (
1774
+ id, target_id, target_path, target_title, default_scope, default_mode,
1775
+ diff_target_kind, diff_base_revision, diff_head_revision,
1776
+ diff_content_digest, submitted, created_at, updated_at
1777
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?)
1778
+ """,
1779
+ (
1780
+ workspace_id,
1781
+ target_id,
1782
+ str(repository),
1783
+ repository.name,
1784
+ scope,
1785
+ mode,
1786
+ *scan_diff_identity(diff_target),
1787
+ timestamp,
1788
+ timestamp,
1789
+ ),
1790
+ )
1791
+ workspace = require_workspace(connection, workspace_id)
1792
+ insert_running_scan(
1793
+ connection,
1794
+ scan_id=scan_id,
1795
+ workspace=workspace,
1796
+ target=repository,
1797
+ scope=scope,
1798
+ diff_target=diff_target,
1799
+ target_identity=target_identity,
1800
+ target_root=scan_dir.parent,
1801
+ target_summary=None,
1802
+ scope_file_count=scope_file_count,
1803
+ timestamp=timestamp,
1804
+ handoff_status="delivered",
1805
+ scan_dir=scan_dir,
1806
+ )
1807
+ connection.execute(
1808
+ "UPDATE scans SET recipe_json = ?, parent_scan_id = ?, user_context = ? WHERE id = ?",
1809
+ (
1810
+ json.dumps(recipe, allow_nan=False, separators=(",", ":"), sort_keys=True),
1811
+ parent_scan_id,
1812
+ user_context,
1813
+ scan_id,
1814
+ ),
1815
+ )
1816
+ if workflow_id is not None:
1817
+ register_workflow_scan(connection, workflow_id, scan_id, str(scan_dir), timestamp)
1818
+ connection.commit()
1765
1819
  except BaseException:
1766
1820
  connection.rollback()
1767
1821
  raise
1768
1822
  scan = require_scan(connection, scan_id)
1769
1823
  return {
1824
+ **({"archivedScanDir": str(archived_scan_dir)} if archived_scan_dir is not None else {}),
1770
1825
  "contract": scan_contract(scan),
1771
1826
  "scanDir": str(scan_dir),
1772
1827
  "scanId": scan_id,
@@ -1860,49 +1915,10 @@ def parse_scan_recipe(value: str, repository: Path) -> dict[str, Any]:
1860
1915
  return recipe
1861
1916
 
1862
1917
 
1863
- _WORKBENCH_DB_CONTEXT: saved_results.WorkbenchDbContext
1864
-
1865
-
1866
1918
  def coverage_for_comparison(scan: sqlite3.Row) -> dict[str, Any]:
1867
1919
  return saved_results.coverage_for_comparison(_WORKBENCH_DB_CONTEXT, scan)
1868
1920
 
1869
1921
 
1870
- def preserve_scan_results_locked(connection: sqlite3.Connection, scan_id: str) -> bool:
1871
- return saved_results.preserve_scan_results_locked(_WORKBENCH_DB_CONTEXT, connection, scan_id)
1872
-
1873
-
1874
- def recover_scan_results(
1875
- connection: sqlite3.Connection, args: argparse.Namespace
1876
- ) -> dict[str, Any]:
1877
- return saved_results.recover_scan_results(_WORKBENCH_DB_CONTEXT, connection, args)
1878
-
1879
-
1880
- def preserve_scan_results(
1881
- connection: sqlite3.Connection, args: argparse.Namespace
1882
- ) -> dict[str, Any]:
1883
- return saved_results.preserve_scan_results(_WORKBENCH_DB_CONTEXT, connection, args)
1884
-
1885
-
1886
- def write_scan_draft(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1887
- return saved_results.write_scan_draft(_WORKBENCH_DB_CONTEXT, connection, args)
1888
-
1889
-
1890
- def fail_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1891
- return saved_results.fail_scan(_WORKBENCH_DB_CONTEXT, connection, args)
1892
-
1893
-
1894
- def fail_scan_locked(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1895
- return saved_results.fail_scan_locked(_WORKBENCH_DB_CONTEXT, connection, args)
1896
-
1897
-
1898
- def cancel_scan(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1899
- return saved_results.cancel_scan(_WORKBENCH_DB_CONTEXT, connection, args)
1900
-
1901
-
1902
- def cancel_scan_locked(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1903
- return saved_results.cancel_scan_locked(_WORKBENCH_DB_CONTEXT, connection, args)
1904
-
1905
-
1906
1922
  def preserve_stopped_results_after_transition(connection: sqlite3.Connection, scan_id: str) -> None:
1907
1923
  saved_results.preserve_stopped_results_after_transition(
1908
1924
  _WORKBENCH_DB_CONTEXT, connection, scan_id
@@ -1918,7 +1934,7 @@ def set_finding_triage(connection: sqlite3.Connection, args: argparse.Namespace)
1918
1934
  note = optional_text(args.note, maximum=2400)
1919
1935
  require_close_note(close_reason, note)
1920
1936
  connection.execute("BEGIN IMMEDIATE")
1921
- try:
1937
+ with connection:
1922
1938
  timestamp = now()
1923
1939
  occurrence = require_occurrence(connection, args.occurrence_id)
1924
1940
  if args.status == "closed":
@@ -1937,7 +1953,7 @@ def set_finding_triage(connection: sqlite3.Connection, args: argparse.Namespace)
1937
1953
  and remediation["pending_action"] is not None
1938
1954
  and not (
1939
1955
  remediation["state"] == "failed"
1940
- and not remediation_claim_is_active(remediation)
1956
+ and not remediation_state.remediation_claim_is_active(remediation)
1941
1957
  )
1942
1958
  ):
1943
1959
  raise SystemExit(
@@ -1990,10 +2006,6 @@ def set_finding_triage(connection: sqlite3.Connection, args: argparse.Namespace)
1990
2006
  """,
1991
2007
  (occurrence["id"], args.status, close_reason, note, timestamp),
1992
2008
  )
1993
- connection.commit()
1994
- except BaseException:
1995
- connection.rollback()
1996
- raise
1997
2009
  return scan_context(connection, occurrence["scan_id"])
1998
2010
 
1999
2011
 
@@ -2011,7 +2023,7 @@ def request_finding_remediation(
2011
2023
  ) -> dict[str, Any]:
2012
2024
  request_id = require_uuid(args.request_id, "request-id")
2013
2025
  action_token = require_uuid(args.action_token, "action-token")
2014
- try:
2026
+ with connection:
2015
2027
  occurrence = require_occurrence(connection, args.occurrence_id)
2016
2028
  require_finding_open(connection, occurrence["id"])
2017
2029
  scan = require_scan(connection, occurrence["scan_id"])
@@ -2053,7 +2065,7 @@ def request_finding_remediation(
2053
2065
  "verifying",
2054
2066
  }
2055
2067
  if active_operation and (
2056
- latest["state"] != "failed" or remediation_claim_is_active(latest)
2068
+ latest["state"] != "failed" or remediation_state.remediation_claim_is_active(latest)
2057
2069
  ):
2058
2070
  raise SystemExit(
2059
2071
  "Finish or retry the active remediation operation before regenerating."
@@ -2100,10 +2112,6 @@ def request_finding_remediation(
2100
2112
  timestamp,
2101
2113
  ),
2102
2114
  )
2103
- connection.commit()
2104
- except BaseException:
2105
- connection.rollback()
2106
- raise
2107
2115
  return scan_context(connection, occurrence["scan_id"])
2108
2116
 
2109
2117
 
@@ -2112,16 +2120,11 @@ def request_finding_remediation_action(
2112
2120
  ) -> dict[str, Any]:
2113
2121
  request_id = require_uuid(args.request_id, "request-id")
2114
2122
  action_token = require_uuid(args.action_token, "action-token")
2115
- try:
2123
+ with connection:
2116
2124
  occurrence = require_occurrence(connection, args.occurrence_id)
2117
2125
  require_finding_open(connection, occurrence["id"])
2118
2126
  scan = require_scan(connection, occurrence["scan_id"])
2119
- current = connection.execute(
2120
- "SELECT * FROM finding_remediation_attempts WHERE request_id = ?",
2121
- (request_id,),
2122
- ).fetchone()
2123
- if current is None or current["occurrence_id"] != occurrence["id"]:
2124
- raise SystemExit("Codex Security finding remediation request not found.")
2127
+ current = remediation_state.require_request(connection, request_id, occurrence)
2125
2128
  if current["pending_action"] is not None:
2126
2129
  if (
2127
2130
  current["pending_action"] == args.action
@@ -2176,10 +2179,6 @@ def request_finding_remediation_action(
2176
2179
  raise SystemExit(
2177
2180
  "This remediation request changed. Refresh it before recording an update."
2178
2181
  )
2179
- connection.commit()
2180
- except BaseException:
2181
- connection.rollback()
2182
- raise
2183
2182
  return scan_context(connection, occurrence["scan_id"])
2184
2183
 
2185
2184
 
@@ -2189,16 +2188,11 @@ def claim_finding_remediation_resend(
2189
2188
  request_id = require_uuid(args.request_id, "request-id")
2190
2189
  action_token = require_uuid(args.action_token, "action-token")
2191
2190
  connection.execute("BEGIN IMMEDIATE")
2192
- try:
2191
+ with connection:
2193
2192
  timestamp = now()
2194
2193
  occurrence = require_occurrence(connection, args.occurrence_id)
2195
2194
  require_finding_open(connection, occurrence["id"])
2196
- current = connection.execute(
2197
- "SELECT * FROM finding_remediation_attempts WHERE request_id = ?",
2198
- (request_id,),
2199
- ).fetchone()
2200
- if current is None or current["occurrence_id"] != occurrence["id"]:
2201
- raise SystemExit("Codex Security finding remediation request not found.")
2195
+ current = remediation_state.require_request(connection, request_id, occurrence)
2202
2196
  if current["pending_action"] is None:
2203
2197
  raise SystemExit("This remediation attempt does not have a pending host request.")
2204
2198
  if current["pending_action_claim_token"] == action_token:
@@ -2208,7 +2202,6 @@ def claim_finding_remediation_resend(
2208
2202
  return result
2209
2203
  delivered_at = current["pending_action_delivered_at"]
2210
2204
  if delivered_at is not None:
2211
- claimed_token = action_token
2212
2205
  updated = connection.execute(
2213
2206
  """
2214
2207
  UPDATE finding_remediation_attempts
@@ -2231,7 +2224,6 @@ def claim_finding_remediation_resend(
2231
2224
  "This remediation worker is still within its execution lease. Retry later."
2232
2225
  )
2233
2226
  else:
2234
- claimed_token = action_token
2235
2227
  updated = connection.execute(
2236
2228
  """
2237
2229
  UPDATE finding_remediation_attempts
@@ -2256,12 +2248,8 @@ def claim_finding_remediation_resend(
2256
2248
  unavailable = "This remediation host request is still owned by another panel. Retry after its lease expires."
2257
2249
  if updated.rowcount != 1:
2258
2250
  raise SystemExit(unavailable)
2259
- connection.commit()
2260
- except BaseException:
2261
- connection.rollback()
2262
- raise
2263
2251
  result = scan_context(connection, occurrence["scan_id"])
2264
- result["actionToken"] = claimed_token
2252
+ result["actionToken"] = action_token
2265
2253
  return result
2266
2254
 
2267
2255
 
@@ -2317,16 +2305,11 @@ def set_finding_remediation(
2317
2305
  action_token = require_uuid(args.action_token, "action-token")
2318
2306
  summary = optional_text(args.summary, maximum=2400)
2319
2307
  verification_summary = optional_text(args.verification_summary, maximum=2400)
2320
- try:
2308
+ with connection:
2321
2309
  occurrence = require_occurrence(connection, args.occurrence_id)
2322
2310
  require_finding_open(connection, occurrence["id"])
2323
2311
  scan = require_scan(connection, occurrence["scan_id"])
2324
- current = connection.execute(
2325
- "SELECT * FROM finding_remediation_attempts WHERE request_id = ?",
2326
- (request_id,),
2327
- ).fetchone()
2328
- if current is None or current["occurrence_id"] != occurrence["id"]:
2329
- raise SystemExit("Codex Security finding remediation request not found.")
2312
+ current = remediation_state.require_request(connection, request_id, occurrence)
2330
2313
  if current["version"] != args.expected_version:
2331
2314
  raise SystemExit(
2332
2315
  "This remediation request changed. Refresh it before recording an update."
@@ -2337,8 +2320,8 @@ def set_finding_remediation(
2337
2320
  )
2338
2321
  if current["pending_action_claim_token"] != action_token:
2339
2322
  raise SystemExit("This remediation host request is owned by a different action token.")
2340
- remediation.require_transition(current["state"], args.state)
2341
- remediation.require_pending_action(current, args.state)
2323
+ remediation_state.require_transition(current["state"], args.state)
2324
+ remediation_state.require_pending_action(current, args.state)
2342
2325
  patch_path = current["patch_path"]
2343
2326
  if args.patch_path is not None:
2344
2327
  requested_patch_path = require_scan_relative_file(scan, args.patch_path)
@@ -2432,24 +2415,9 @@ def set_finding_remediation(
2432
2415
  raise SystemExit(
2433
2416
  "This remediation request changed. Refresh it before recording an update."
2434
2417
  )
2435
- connection.commit()
2436
- except BaseException:
2437
- connection.rollback()
2438
- raise
2439
2418
  return scan_context(connection, occurrence["scan_id"])
2440
2419
 
2441
2420
 
2442
- _WORKBENCH_PUBLICATION_CONTEXT: publication.WorkbenchPublicationContext
2443
-
2444
-
2445
- def inspect_linear_publication(args: argparse.Namespace) -> dict[str, Any]:
2446
- return publication.inspect_linear_publication(_WORKBENCH_PUBLICATION_CONTEXT, args)
2447
-
2448
-
2449
- def export_findings(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
2450
- return publication.export_findings(_WORKBENCH_PUBLICATION_CONTEXT, connection, args)
2451
-
2452
-
2453
2421
  def require_reviewed_patch_applied(
2454
2422
  scan: sqlite3.Row, remediation: sqlite3.Row, patch_path: str
2455
2423
  ) -> str | None:
@@ -2463,14 +2431,13 @@ def require_reviewed_patch_applied(
2463
2431
  )
2464
2432
  unversioned = remediation["base_revision"] == "unversioned"
2465
2433
  excluded = (Path(scan["scan_dir"]),)
2466
- git_dir = None
2467
- pathspec = None
2434
+ # Unborn repositories still need their config while metadata junctions are deferred.
2435
+ git_dir = git_output(target, "rev-parse", "--absolute-git-dir")
2468
2436
  if not unversioned:
2469
- _, pathspec = git_worktree_context(target)
2470
- git_dir = git_output(target, "rev-parse", "--absolute-git-dir")
2437
+ repository, pathspec = git_worktree_context(target)
2471
2438
  if git_dir is None:
2472
2439
  raise SystemExit("Could not inspect the selected Git working tree.")
2473
- excluded += git_submodule_paths(target)
2440
+ excluded += tuple(path for path, _ in git_submodule_entries(target))
2474
2441
  with tempfile.TemporaryDirectory(prefix="codex-security-remediation-") as temporary:
2475
2442
  reviewed_patch = Path(temporary) / "reviewed.patch"
2476
2443
  digest = hashlib.sha256()
@@ -2484,9 +2451,9 @@ def require_reviewed_patch_applied(
2484
2451
  checkout_root = Path(temporary) / "checkout"
2485
2452
  if unversioned:
2486
2453
  checkout = checkout_root
2487
- copy_directory_excluding(target, checkout, excluded)
2454
+ junctions = copy_directory_excluding(target, checkout, excluded)
2488
2455
  else:
2489
- copy_git_worktree_files(target, checkout_root, excluded)
2456
+ _, junctions = copy_git_worktree_files(target, checkout_root, excluded)
2490
2457
  arguments = ["apply", "--reverse", "--whitespace=nowarn"]
2491
2458
  if unversioned:
2492
2459
  arguments.append("--no-index")
@@ -2504,6 +2471,11 @@ def require_reviewed_patch_applied(
2504
2471
  raise SystemExit(
2505
2472
  "The selected checkout does not contain the reviewed remediation patch. Apply exactly that patch before recording it as applied."
2506
2473
  )
2474
+ restore_directory_junctions(
2475
+ target if unversioned else repository,
2476
+ checkout if unversioned else checkout_root,
2477
+ junctions,
2478
+ )
2507
2479
  reverted_digest = (
2508
2480
  directory_content_digest(checkout)
2509
2481
  if unversioned
@@ -2516,11 +2488,16 @@ def require_reviewed_patch_applied(
2516
2488
  )
2517
2489
  if reverted_digest != remediation["base_content_digest"] and unversioned:
2518
2490
  checkout = Path(temporary) / "checkout-lf"
2519
- copy_directory_excluding(target, checkout, excluded)
2491
+ junctions = copy_directory_excluding(target, checkout, excluded)
2520
2492
  applied_without_conversion = git_command(
2521
- checkout, "-c", "core.autocrlf=input", *arguments, text=True
2493
+ checkout,
2494
+ "-c",
2495
+ "core.autocrlf=input",
2496
+ *arguments,
2497
+ text=True,
2522
2498
  )
2523
2499
  if applied_without_conversion.returncode == 0:
2500
+ restore_directory_junctions(target, checkout, junctions)
2524
2501
  reverted_digest = directory_content_digest(checkout)
2525
2502
  if reverted_digest != remediation["base_content_digest"]:
2526
2503
  raise SystemExit(
@@ -2567,15 +2544,14 @@ def require_sha256_digest(value: str, label: str) -> str:
2567
2544
 
2568
2545
 
2569
2546
  def require_scan_relative_file(scan: sqlite3.Row, value: str) -> str:
2570
- normalized = optional_text(value, maximum=4096)
2571
- if normalized is None or "\\" in normalized:
2547
+ if len(value) > 4096:
2548
+ raise SystemExit("Text value must be no longer than 4096 characters.")
2549
+ if not value.strip() or "\\" in value:
2572
2550
  raise SystemExit("Patch path must identify a scan-local regular file.")
2573
- parsed = PurePosixPath(normalized)
2551
+ parsed = PurePosixPath(value)
2574
2552
  if parsed.is_absolute() or ".." in parsed.parts:
2575
2553
  raise SystemExit("Patch path must identify a scan-local regular file.")
2576
- path = artifact_path(Path(scan["scan_dir"]), parsed.as_posix(), required=True)
2577
- if path is None:
2578
- raise SystemExit("Patch path must identify a scan-local regular file.")
2554
+ artifact_path(Path(scan["scan_dir"]), parsed.as_posix(), required=True)
2579
2555
  return parsed.as_posix()
2580
2556
 
2581
2557
 
@@ -2669,6 +2645,7 @@ def workspace_state(
2669
2645
  target = require_target(workspace["target_path"])
2670
2646
  target_metadata = git_target_metadata(target)
2671
2647
  except SystemExit:
2648
+ # Target metadata is optional; retain the original setup error for the caller.
2672
2649
  pass
2673
2650
  result["diffTarget"] = validated_diff_target or persisted_diff_target
2674
2651
  result["setupValidation"] = {
@@ -2772,6 +2749,9 @@ def scan_result(
2772
2749
  )
2773
2750
  if sarif_path is not None:
2774
2751
  artifacts["sarifReport"] = str(sarif_path)
2752
+ model_fields = saved_results.threat_model_fields(_WORKBENCH_DB_CONTEXT, scan)
2753
+ if path := model_fields.get("threatModelPath"):
2754
+ artifacts["threatModel"] = path
2775
2755
  occurrence_rows = scan_history.finding_occurrence_rows(
2776
2756
  connection, scan["id"], offset=0, limit=FINDINGS_RESULT_LIMIT
2777
2757
  )
@@ -2823,18 +2803,16 @@ def scan_result(
2823
2803
  "status": "canceled" if scan["canceled_at"] else scan["status"],
2824
2804
  "updatedAt": progress["updated_at"],
2825
2805
  }
2806
+ review_updated_at = ""
2826
2807
  if independent_reviews is not None:
2827
- progress_result["independentReviews"] = {
2828
- "active": independent_reviews["active"],
2829
- "completed": independent_reviews["completed"],
2830
- "maximum": independent_reviews["maximum"],
2831
- "consolidating": independent_reviews["consolidating"],
2832
- }
2808
+ review_updated_at = str(independent_reviews.pop("updatedAt"))
2809
+ progress_result["independentReviews"] = independent_reviews
2833
2810
  relations = scan_history.finding_relations(
2834
2811
  connection, scan["id"], (row["id"] for row in occurrence_rows)
2835
2812
  )
2836
2813
  return {
2837
2814
  "artifacts": artifacts,
2815
+ **model_fields,
2838
2816
  "canceledAt": scan["canceled_at"],
2839
2817
  **scan_usage.stored_scan_cost_fields(scan["cost_json"]),
2840
2818
  "contract": scan_contract(scan),
@@ -2855,6 +2833,7 @@ def scan_result(
2855
2833
  "mode": scan["mode"],
2856
2834
  "model": scan["model"],
2857
2835
  "diffTarget": stored_diff_target(scan),
2836
+ "name": scan["name"],
2858
2837
  "progress": progress_result,
2859
2838
  "reasoningEffort": scan["reasoning_effort"],
2860
2839
  "remediationAvailable": remediation_available,
@@ -2872,7 +2851,7 @@ def scan_result(
2872
2851
  "updatedAt": max(
2873
2852
  scan["updated_at"],
2874
2853
  progress["updated_at"],
2875
- str(independent_reviews["updatedAt"]) if independent_reviews is not None else "",
2854
+ review_updated_at,
2876
2855
  finding_management_updated_at(connection, scan["id"]) or "",
2877
2856
  ),
2878
2857
  "userContext": scan["user_context"],
@@ -2882,7 +2861,7 @@ def scan_result(
2882
2861
 
2883
2862
  def remediation_availability(scan: sqlite3.Row) -> tuple[bool, str | None]:
2884
2863
  if scan["status"] != "complete":
2885
- return False, remediation.SCAN_STATUS_ERROR
2864
+ return False, remediation_state.SCAN_STATUS_ERROR
2886
2865
  try:
2887
2866
  current_revision = git_revision(require_scan_target_identity(scan))
2888
2867
  except SystemExit as exc:
@@ -2926,14 +2905,7 @@ def backfill_legacy_finding_details(connection: sqlite3.Connection, scan: sqlite
2926
2905
  except (ContractError, OSError, SystemExit, ValueError):
2927
2906
  return
2928
2907
 
2929
- findings = findings_document.get("findings")
2930
- if not isinstance(findings, list):
2931
- return
2932
- by_occurrence = {
2933
- finding.get("occurrenceId"): finding
2934
- for finding in findings
2935
- if isinstance(finding, dict) and isinstance(finding.get("occurrenceId"), str)
2936
- }
2908
+ by_occurrence = {finding["occurrenceId"]: finding for finding in findings_document["findings"]}
2937
2909
  updates = []
2938
2910
  for row in legacy_rows:
2939
2911
  finding = by_occurrence.get(row["id"])
@@ -2949,8 +2921,16 @@ def backfill_legacy_finding_details(connection: sqlite3.Connection, scan: sqlite
2949
2921
  if not updates:
2950
2922
  return
2951
2923
 
2952
- connection.execute("BEGIN IMMEDIATE")
2953
- try:
2924
+ # Reads normally avoid writer admission, but legacy details still need a write.
2925
+ for attempt in range(SQLITE_RETRY_ATTEMPTS):
2926
+ try:
2927
+ connection.execute("BEGIN IMMEDIATE")
2928
+ break
2929
+ except sqlite3.OperationalError as exc:
2930
+ if attempt == SQLITE_RETRY_ATTEMPTS - 1 or not sqlite_busy(exc):
2931
+ raise
2932
+ time.sleep(0.05 * (2**attempt))
2933
+ with connection:
2954
2934
  current = require_scan(connection, scan["id"])
2955
2935
  recorded_digest = current["seal_manifest_digest"]
2956
2936
  if recorded_digest is not None and recorded_digest != manifest_digest:
@@ -2968,26 +2948,18 @@ def backfill_legacy_finding_details(connection: sqlite3.Connection, scan: sqlite
2968
2948
  "UPDATE scans SET seal_manifest_digest = ? WHERE id = ?",
2969
2949
  (manifest_digest, scan["id"]),
2970
2950
  )
2971
- connection.commit()
2972
- except BaseException:
2973
- connection.rollback()
2974
- raise
2975
2951
 
2976
2952
 
2977
2953
  def legacy_finding_matches(row: sqlite3.Row, finding: Any) -> bool:
2978
2954
  if not isinstance(finding, dict):
2979
2955
  return False
2980
- severity = finding.get("severity")
2981
- confidence = finding.get("confidence")
2982
2956
  return (
2983
2957
  finding.get("findingId") == row["finding_id"]
2984
2958
  and finding.get("title") == row["title"]
2985
2959
  and finding.get("summary") == row["summary"]
2986
2960
  and finding.get("remediation") == row["remediation"]
2987
- and isinstance(severity, dict)
2988
- and severity.get("level") == row["severity"]
2989
- and isinstance(confidence, dict)
2990
- and confidence.get("level") == row["confidence"]
2961
+ and finding["severity"]["level"] == row["severity"]
2962
+ and finding["confidence"]["level"] == row["confidence"]
2991
2963
  )
2992
2964
 
2993
2965
 
@@ -3063,7 +3035,6 @@ def finding_result(
3063
3035
  result["knownScanIds"] = known_scan_ids
3064
3036
  if related:
3065
3037
  result["related"] = related
3066
- result.pop("artifactPaths", None)
3067
3038
  source_excerpt = finding_source_excerpt(scan, target, locations)
3068
3039
  if source_excerpt:
3069
3040
  result["sourceExcerpt"] = source_excerpt
@@ -3095,13 +3066,10 @@ def finding_artifact_paths(scan_dir: Path, details: dict[str, Any]) -> list[str]
3095
3066
  except OSError:
3096
3067
  return artifacts
3097
3068
 
3098
- directories_seen = 0
3099
- for current_directory, directory_names, file_names in os.walk(
3100
- poc_root, topdown=True, followlinks=False
3069
+ for directories_seen, (current_directory, directory_names, file_names) in enumerate(
3070
+ os.walk(poc_root, topdown=True, followlinks=False), start=1
3101
3071
  ):
3102
- directories_seen += 1
3103
3072
  if directories_seen > FINDING_ARTIFACT_DIRECTORIES_LIMIT:
3104
- directory_names[:] = []
3105
3073
  break
3106
3074
  current_path = Path(current_directory)
3107
3075
  directory_names[:] = [
@@ -3122,15 +3090,15 @@ def finding_artifact_paths(scan_dir: Path, details: dict[str, Any]) -> list[str]
3122
3090
 
3123
3091
 
3124
3092
  def scan_local_regular_file(scan_dir: Path, relative_path: str) -> bool:
3125
- if len(relative_path.encode("utf-8")) > FINDING_LOCATION_PATH_BYTES:
3126
- return False
3127
3093
  try:
3094
+ if len(relative_path.encode("utf-8")) > FINDING_LOCATION_PATH_BYTES:
3095
+ return False
3128
3096
  descriptor = open_scan_local_file_descriptor(
3129
3097
  scan_dir,
3130
3098
  relative_path,
3131
3099
  f"finding artifact {relative_path}",
3132
3100
  )
3133
- except (ContractError, OSError):
3101
+ except (ContractError, OSError, UnicodeEncodeError):
3134
3102
  return False
3135
3103
  try:
3136
3104
  return stat.S_ISREG(os.fstat(descriptor).st_mode)
@@ -3187,8 +3155,7 @@ def finding_remediation_result(
3187
3155
  row = connection.execute(
3188
3156
  """
3189
3157
  SELECT remediation.request_id, remediation.state, remediation.version,
3190
- remediation.base_revision, remediation.base_content_digest,
3191
- remediation.applied_content_digest, remediation.pending_action,
3158
+ remediation.base_revision, remediation.pending_action,
3192
3159
  remediation.pending_action_claimed_at, remediation.pending_action_claim_token,
3193
3160
  remediation.pending_action_delivered_at,
3194
3161
  remediation.patch_path, remediation.patch_digest, remediation.summary,
@@ -3358,72 +3325,18 @@ def read_json_object(path: Path) -> dict[str, Any]:
3358
3325
  return payload
3359
3326
 
3360
3327
 
3361
- _WORKBENCH_PUBLICATION_CONTEXT = publication.WorkbenchPublicationContext(
3362
- ARTIFACTS=ARTIFACTS,
3363
- artifact_path=artifact_path,
3364
- available_artifact_path=available_artifact_path,
3365
- database_path=database_path,
3366
- expected_coverage_mode=expected_coverage_mode,
3367
- now=now,
3368
- pin_legacy_manifest_digest=pin_legacy_manifest_digest,
3369
- published_manifest_digest=published_manifest_digest,
3370
- read_json_object=read_json_object,
3371
- require_canonical_scan_directory=require_canonical_scan_directory,
3372
- require_recorded_manifest_digest=require_recorded_manifest_digest,
3373
- require_scan=require_scan,
3374
- scan_result=scan_result,
3375
- verify_manifest_binding=verify_manifest_binding,
3376
- workspace_state=workspace_state,
3377
- )
3378
-
3379
-
3380
- _WORKBENCH_DB_CONTEXT = saved_results.WorkbenchDbContext(
3381
- ARTIFACTS=ARTIFACTS,
3382
- artifact_path=artifact_path,
3383
- deep_scan=deep_scan,
3384
- expected_coverage_mode=expected_coverage_mode,
3385
- handoff=handoff,
3386
- index_findings=index_findings,
3387
- now=now,
3388
- optional_text=optional_text,
3389
- parse_scan_cost=parse_scan_cost,
3390
- published_manifest_digest=published_manifest_digest,
3391
- read_json_object=read_json_object,
3392
- require_canonical_scan_directory=require_canonical_scan_directory,
3393
- require_recorded_manifest_digest=require_recorded_manifest_digest,
3394
- require_scan=require_scan,
3395
- require_uuid=require_uuid,
3396
- require_workspace=require_workspace,
3397
- scan_completion_lock=scan_completion_lock,
3398
- scan_context=scan_context,
3399
- verify_manifest_binding=verify_manifest_binding,
3400
- workbench_completion_binding=workbench_completion_binding,
3401
- workspace_state=workspace_state,
3402
- )
3328
+ _WORKBENCH_PUBLICATION_CONTEXT = _WORKBENCH_DB_CONTEXT = SimpleNamespace(**globals())
3403
3329
 
3404
3330
 
3405
- def main() -> None:
3331
+ def main(*, before_archive: Callable[[], None] | None = None) -> None:
3406
3332
  # Workbench callers send UTF-8 even when Windows uses a legacy code page.
3407
3333
  sys.stdin.reconfigure(encoding="utf-8")
3408
3334
  args = parse_args(__doc__)
3409
- deep_scan.configure(
3410
- deep_scan.DeepScanDependencies(
3411
- now=now,
3412
- state_dir=state_dir,
3413
- require_scan=require_scan,
3414
- require_workspace=require_workspace,
3415
- require_target=require_target,
3416
- require_remediation_target=require_remediation_target,
3417
- require_scannable_target=require_scannable_target,
3418
- require_scope=require_scope,
3419
- ensure_security_target=ensure_security_target,
3420
- require_canonical_scan_directory=require_canonical_scan_directory,
3421
- safe_segment=safe_segment,
3422
- compact_timestamp=compact_timestamp,
3423
- scan_completion_lock=scan_completion_lock,
3424
- preserve_stopped_results=preserve_stopped_results_after_transition,
3425
- )
3335
+ command_context = SimpleNamespace(
3336
+ **globals(),
3337
+ preserve_stopped_results=preserve_stopped_results_after_transition,
3426
3338
  )
3339
+ deep_scan.configure(command_context)
3427
3340
  if args.command == "resolve-scan-root":
3428
3341
  print(json.dumps({"scanRoot": str(resolve_scan_root(args.scan_root))}))
3429
3342
  return
@@ -3432,62 +3345,109 @@ def main() -> None:
3432
3345
  print(json.dumps(result, allow_nan=False, sort_keys=True))
3433
3346
  return
3434
3347
  if args.command == "inspect-setup":
3435
- result = inspect_setup(args)
3348
+ result = inspect_setup_values(
3349
+ args.target_path,
3350
+ args.scope,
3351
+ args.mode,
3352
+ args.diff_target_kind,
3353
+ args.diff_base_revision,
3354
+ args.diff_head_revision,
3355
+ args.diff_content_digest,
3356
+ )
3436
3357
  print(json.dumps(result, allow_nan=False, sort_keys=True))
3437
3358
  return
3438
3359
  if args.command in {"save-artifact", "read-artifact"}:
3439
3360
  print(json.dumps(saved_results.read_or_save_artifact(args)))
3440
3361
  return
3441
- if args.command == "read-severity-classification":
3442
- result = severity.read_classification(database_path(), args.scan_id)
3443
- print(json.dumps(result, allow_nan=False, sort_keys=True))
3444
- return
3445
3362
  if args.command == "inspect-linear-publication":
3446
- result = inspect_linear_publication(args)
3363
+ result = publication.inspect_linear_publication(
3364
+ _WORKBENCH_PUBLICATION_CONTEXT, read_json_object(Path(args.input_file))
3365
+ )
3447
3366
  print(json.dumps(result, allow_nan=False, sort_keys=True))
3448
3367
  return
3449
- with closing(connect()) as connection:
3450
- remediation.require_available(connection, args, require_scan)
3451
- if args.command == "create-workspace":
3452
- result = create_workspace(connection, args)
3368
+ with closing(
3369
+ connect(deferred=args.command in {"get-scan", "list-scans", "database-info"})
3370
+ ) as connection:
3371
+ remediation_state.require_available(connection, args, require_scan)
3372
+ handlers = {
3373
+ "create-workspace": create_workspace,
3374
+ "save-workspace": save_workspace,
3375
+ "start-scan": start_scan,
3376
+ "begin-deep-scan": deep_scan.begin_deep_scan,
3377
+ "get-deep-scan": deep_scan.get_deep_scan,
3378
+ "claim-deep-scan-coordinator": deep_scan.claim_deep_scan_coordinator,
3379
+ "upsert-deep-scan-worker": deep_scan.upsert_deep_scan_worker,
3380
+ "claim-deep-scan-dedup": deep_scan.claim_deep_scan_dedup,
3381
+ "commit-deep-scan-dedup": deep_scan.commit_deep_scan_dedup,
3382
+ "finish-deep-scan": deep_scan.finish_deep_scan,
3383
+ "fail-deep-scan": deep_scan.fail_deep_scan,
3384
+ "record-deep-scan-publication-failure": deep_scan.record_deep_scan_publication_failure,
3385
+ "list-scans": scan_history.list_scans,
3386
+ "register-cli-scan": partial(register_cli_scan, before_archive=before_archive),
3387
+ "set-scan-thread": set_scan_thread,
3388
+ "set-scan-cost-limit": set_scan_cost_limit,
3389
+ "list-global-findings": native_indexes.list_global_findings,
3390
+ "list-repositories": native_indexes.list_repositories,
3391
+ "list-findings": list_findings,
3392
+ "complete-budget-exhausted-scan": complete_budget_exhausted_scan,
3393
+ "set-finding-triage": set_finding_triage,
3394
+ "request-finding-remediation": request_finding_remediation,
3395
+ "request-finding-remediation-action": request_finding_remediation_action,
3396
+ "claim-finding-remediation-resend": claim_finding_remediation_resend,
3397
+ "mark-finding-remediation-delivered": mark_finding_remediation_delivered,
3398
+ "release-finding-remediation-claim": release_finding_remediation_claim,
3399
+ "set-finding-remediation": set_finding_remediation,
3400
+ "update-progress": partial(progress.update_progress, command_context),
3401
+ "update-scan-context": partial(progress.update_context, command_context),
3402
+ "cancel-scan": partial(saved_results.cancel_scan, _WORKBENCH_DB_CONTEXT),
3403
+ "fail-scan": partial(saved_results.fail_scan, _WORKBENCH_DB_CONTEXT),
3404
+ "preserve-scan-results": partial(
3405
+ saved_results.preserve_scan_results, _WORKBENCH_DB_CONTEXT
3406
+ ),
3407
+ "recover-scan-results": partial(
3408
+ saved_results.recover_scan_results, _WORKBENCH_DB_CONTEXT
3409
+ ),
3410
+ "write-scan-draft": partial(saved_results.write_scan_draft, _WORKBENCH_DB_CONTEXT),
3411
+ "save-scan-artifact": partial(saved_results.save_scan_artifact, _WORKBENCH_DB_CONTEXT),
3412
+ "mark-handoff-delivered": partial(handoff.mark_handoff_delivered, command_context),
3413
+ "claim-handoff-delivery": partial(handoff.claim_handoff_delivery, command_context),
3414
+ "release-handoff-delivery": partial(handoff.release_handoff_delivery, command_context),
3415
+ "attach-scan-continuation-thread": partial(
3416
+ handoff.attach_scan_continuation_thread, command_context
3417
+ ),
3418
+ "prepare-linear-publication": partial(
3419
+ publication.prepare_linear_publication, _WORKBENCH_PUBLICATION_CONTEXT
3420
+ ),
3421
+ "record-linear-publications": partial(
3422
+ publication.record_linear_publications, _WORKBENCH_PUBLICATION_CONTEXT
3423
+ ),
3424
+ "export-findings": partial(publication.export_findings, _WORKBENCH_PUBLICATION_CONTEXT),
3425
+ }
3426
+ if handler := handlers.get(args.command):
3427
+ payload = (
3428
+ read_json_object(Path(args.input_file))
3429
+ if args.command in {"prepare-linear-publication", "record-linear-publications"}
3430
+ else args
3431
+ )
3432
+ result = handler(connection, payload)
3453
3433
  elif args.command == "get-workspace":
3454
3434
  result = workspace_state(
3455
3435
  connection,
3456
3436
  args.workspace_id,
3457
3437
  thread_id=args.thread_id,
3458
3438
  )
3459
- elif args.command == "save-workspace":
3460
- result = save_workspace(connection, args)
3461
- elif args.command == "start-scan":
3462
- result = start_scan(connection, args)
3463
3439
  elif args.command == "start-prompt-only-scan":
3464
- result = start_prompt_only_scan(connection, args)
3440
+ result = _start_prompt_driven_scan(connection, args, headless_standard=False)
3465
3441
  elif args.command == "start-headless-standard-scan":
3466
- result = start_headless_standard_scan(connection, args)
3467
- elif args.command == "begin-deep-scan":
3468
- result = deep_scan.begin_deep_scan(connection, args)
3469
- elif args.command == "get-deep-scan":
3470
- result = deep_scan.get_deep_scan(connection, args)
3471
- elif args.command == "claim-deep-scan-coordinator":
3472
- result = deep_scan.claim_deep_scan_coordinator(connection, args)
3473
- elif args.command == "upsert-deep-scan-worker":
3474
- result = deep_scan.upsert_deep_scan_worker(connection, args)
3475
- elif args.command == "claim-deep-scan-dedup":
3476
- result = deep_scan.claim_deep_scan_dedup(connection, args)
3477
- elif args.command == "commit-deep-scan-dedup":
3478
- result = deep_scan.commit_deep_scan_dedup(connection, args)
3479
- elif args.command == "finish-deep-scan":
3480
- result = deep_scan.finish_deep_scan(connection, args)
3481
- elif args.command == "fail-deep-scan":
3482
- result = deep_scan.fail_deep_scan(connection, args)
3483
- elif args.command == "record-deep-scan-publication-failure":
3484
- result = deep_scan.record_deep_scan_publication_failure(connection, args)
3442
+ result = _start_prompt_driven_scan(connection, args, headless_standard=True)
3485
3443
  elif args.command == "get-scan":
3486
3444
  result = scan_context(connection, args.scan_id, args.occurrence_id)
3445
+ elif args.command == "rename-scan":
3446
+ result = scan_history.rename_scan(
3447
+ connection, require_scan(connection, args.scan_id), args.name
3448
+ )
3487
3449
  elif args.command == "get-scan-feedback":
3488
3450
  result = get_scan_feedback(connection, require_scan(connection, args.scan_id))
3489
- elif args.command == "list-scans":
3490
- result = scan_history.list_scans(connection, args)
3491
3451
  elif args.command == "list-unmatched-scan-pairs":
3492
3452
  result = scan_history.list_unmatched_scan_pairs(
3493
3453
  connection,
@@ -3495,27 +3455,16 @@ def main() -> None:
3495
3455
  backfill_finding_details=backfill_legacy_finding_details,
3496
3456
  read_coverage=coverage_for_comparison,
3497
3457
  )
3498
- elif args.command == "register-cli-scan":
3499
- result = register_cli_scan(connection, args)
3500
- elif args.command == "set-scan-thread":
3501
- result = set_scan_thread(connection, args)
3502
- elif args.command == "set-scan-cost-limit":
3503
- result = set_scan_cost_limit(connection, args)
3504
3458
  elif args.command == "get-scan-recipe":
3505
3459
  result = scan_history.scan_recipe(require_scan(connection, args.scan_id))
3506
3460
  elif args.command == "get-cli-scan-resume":
3507
3461
  scan = require_scan(connection, args.scan_id)
3508
3462
  try:
3509
3463
  result = scan_history.cli_scan_resume(
3464
+ command_context,
3510
3465
  connection,
3511
3466
  scan,
3512
3467
  require_workspace(connection, scan["workspace_id"]),
3513
- parse_scan_recipe=parse_scan_recipe,
3514
- scan_contract=scan_contract,
3515
- require_scan_directory=require_canonical_scan_directory,
3516
- artifact_path=artifact_path,
3517
- read_json_object=read_json_object,
3518
- workbench_completion_binding=workbench_completion_binding,
3519
3468
  )
3520
3469
  except SystemExit as exc:
3521
3470
  if not args.allow_unavailable:
@@ -3539,118 +3488,21 @@ def main() -> None:
3539
3488
  require_scan=require_scan,
3540
3489
  read_coverage=coverage_for_comparison,
3541
3490
  )
3542
- elif args.command == "list-global-findings":
3543
- result = native_indexes.list_global_findings(connection, args)
3544
- elif args.command == "list-repositories":
3545
- result = native_indexes.list_repositories(connection, args)
3546
- elif args.command == "list-findings":
3547
- result = list_findings(connection, args)
3548
- elif args.command in {"update-progress", "update-scan-context"}:
3549
- result = progress.update(
3550
- connection, args, now, require_scan, require_workspace, scan_context
3551
- )
3552
3491
  elif args.command in {"prepare-scan-completion", "complete-scan"}:
3553
3492
  result = complete_scan(
3554
3493
  connection, args, prepare_only=args.command == "prepare-scan-completion"
3555
3494
  )
3556
- elif args.command == "complete-budget-exhausted-scan":
3557
- result = complete_budget_exhausted_scan(connection, args)
3558
- elif args.command == "cancel-scan":
3559
- result = cancel_scan(connection, args)
3560
- elif args.command == "fail-scan":
3561
- result = fail_scan(connection, args)
3562
- elif args.command == "preserve-scan-results":
3563
- result = preserve_scan_results(connection, args)
3564
- elif args.command == "recover-scan-results":
3565
- result = recover_scan_results(connection, args)
3566
- elif args.command == "write-scan-draft":
3567
- result = write_scan_draft(connection, args)
3568
- elif args.command == "save-scan-artifact":
3569
- result = saved_results.save_scan_artifact(_WORKBENCH_DB_CONTEXT, connection, args)
3570
- elif args.command == "mark-handoff-delivered":
3571
- result = handoff.mark_handoff_delivered(
3572
- connection,
3573
- args,
3574
- now=now,
3575
- require_scan=require_scan,
3576
- require_workspace=require_workspace,
3577
- workspace_state=workspace_state,
3578
- )
3579
- elif args.command == "claim-handoff-delivery":
3580
- result = handoff.claim_handoff_delivery(
3581
- connection,
3582
- args,
3583
- now=now,
3584
- require_scan=require_scan,
3585
- stale_claim_before=stale_claim_before,
3586
- workspace_state=workspace_state,
3587
- )
3588
- elif args.command == "release-handoff-delivery":
3589
- result = handoff.release_handoff_delivery(
3590
- connection,
3591
- args,
3592
- now=now,
3593
- require_scan=require_scan,
3594
- workspace_state=workspace_state,
3595
- )
3596
- elif args.command == "attach-scan-continuation-thread":
3597
- result = handoff.attach_scan_continuation_thread(
3598
- connection,
3599
- args,
3600
- now=now,
3601
- require_scan=require_scan,
3602
- workspace_state=workspace_state,
3603
- )
3604
- elif args.command == "set-finding-triage":
3605
- result = set_finding_triage(connection, args)
3606
- elif args.command == "request-finding-remediation":
3607
- result = request_finding_remediation(connection, args)
3608
- elif args.command == "request-finding-remediation-action":
3609
- result = request_finding_remediation_action(connection, args)
3610
- elif args.command == "claim-finding-remediation-resend":
3611
- result = claim_finding_remediation_resend(connection, args)
3612
- elif args.command == "mark-finding-remediation-delivered":
3613
- result = mark_finding_remediation_delivered(connection, args)
3614
- elif args.command == "release-finding-remediation-claim":
3615
- result = release_finding_remediation_claim(connection, args)
3616
3495
  elif args.command == "cancel-finding-remediation-request":
3617
3496
  result = scan_context(
3618
3497
  connection,
3619
- remediation.cancel_finding_remediation_request(connection, args),
3620
- )
3621
- elif args.command == "set-finding-remediation":
3622
- result = set_finding_remediation(connection, args)
3623
- elif args.command == "prepare-linear-publication":
3624
- result = publication.prepare_linear_publication(
3625
- _WORKBENCH_PUBLICATION_CONTEXT, connection, args
3498
+ remediation_state.cancel_finding_remediation_request(connection, args),
3626
3499
  )
3627
- elif args.command == "record-linear-publications":
3628
- result = publication.record_linear_publications(
3629
- _WORKBENCH_PUBLICATION_CONTEXT, connection, args
3630
- )
3631
- elif args.command == "export-findings":
3632
- result = export_findings(connection, args)
3633
3500
  elif args.command == "database-info":
3634
3501
  result = {"databasePath": str(database_path())}
3635
- elif args.command == "severity-classification":
3636
- result = severity.checkpoint(connection, json.load(sys.stdin), now())
3502
+ elif args.command == "local-dedupe":
3503
+ result = local_dedupe(connection, json.load(sys.stdin), now())
3637
3504
  elif args.command == "finding-workflow":
3638
3505
  result = finding_workflow(connection, json.load(sys.stdin), now())
3639
- elif args.command == "dashboard":
3640
- result = dashboard(connection, json.load(sys.stdin))
3641
- elif args.command == "store-findings":
3642
- payload = json.load(sys.stdin)
3643
- result = store_findings(
3644
- connection, payload["entries"], now(), payload.get("repositoryId")
3645
- )
3646
- elif args.command == "find-potential-duplicates":
3647
- result = find_potential_duplicates(connection, args.finding_id, args.repository_id)
3648
- elif args.command == "store-dedupe-groups":
3649
- result = store_dedupe_groups(connection, json.load(sys.stdin)["groups"], now())
3650
- elif args.command == "list-dedupe-groups":
3651
- result = list_dedupe_groups(connection, args.finding_id)
3652
- elif args.command == "list-stored-findings":
3653
- result = list_stored_findings(connection, limit=args.limit, offset=args.offset)
3654
3506
  else:
3655
3507
  raise SystemExit(f"Unknown command: {args.command}")
3656
3508
  print(json.dumps(result, allow_nan=False, sort_keys=True))