@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
@@ -4,934 +4,39 @@ import argparse
4
4
  import json
5
5
  import sqlite3
6
6
  from collections.abc import Callable
7
-
8
- MIGRATIONS = (
9
- (
10
- 1,
11
- "initial workbench schema",
12
- """
13
- CREATE TABLE workspaces (
14
- id TEXT PRIMARY KEY,
15
- target_path TEXT,
16
- target_title TEXT,
17
- target_summary TEXT,
18
- default_scope TEXT NOT NULL DEFAULT '.',
19
- default_mode TEXT NOT NULL DEFAULT 'standard'
20
- CHECK (default_mode IN ('diff', 'standard', 'deep')),
21
- user_context TEXT,
22
- diff_target_kind TEXT
23
- CHECK (diff_target_kind IN ('working_tree', 'commit', 'range')),
24
- diff_base_revision TEXT,
25
- diff_head_revision TEXT,
26
- diff_content_digest TEXT,
27
- diff_resolution_id TEXT,
28
- submitted INTEGER NOT NULL DEFAULT 0 CHECK (submitted IN (0, 1)),
29
- active_scan_id TEXT REFERENCES scans(id) ON DELETE SET NULL,
30
- created_at TEXT NOT NULL,
31
- updated_at TEXT NOT NULL
32
- );
33
-
34
- CREATE TABLE scans (
35
- id TEXT PRIMARY KEY,
36
- workspace_id TEXT NOT NULL REFERENCES workspaces(id) ON DELETE CASCADE,
37
- target_path TEXT NOT NULL,
38
- target_revision TEXT NOT NULL,
39
- target_snapshot_digest TEXT,
40
- scope TEXT NOT NULL,
41
- mode TEXT NOT NULL CHECK (mode IN ('diff', 'standard', 'deep')),
42
- user_context TEXT,
43
- diff_target_kind TEXT
44
- CHECK (diff_target_kind IN ('working_tree', 'commit', 'range')),
45
- diff_base_revision TEXT,
46
- diff_head_revision TEXT,
47
- diff_content_digest TEXT,
48
- scan_dir TEXT NOT NULL UNIQUE,
49
- status TEXT NOT NULL CHECK (status IN ('running', 'complete', 'failed')),
50
- phase TEXT NOT NULL CHECK (
51
- phase IN ('preflight', 'threat_model', 'discovery', 'validation', 'attack_path', 'reporting')
52
- ),
53
- handoff_status TEXT NOT NULL DEFAULT 'pending'
54
- CHECK (handoff_status IN ('pending', 'delivered')),
55
- failure_message TEXT,
56
- started_at TEXT NOT NULL,
57
- completed_at TEXT,
58
- created_at TEXT NOT NULL,
59
- updated_at TEXT NOT NULL
60
- );
61
-
62
- CREATE TABLE scan_progress (
63
- scan_id TEXT PRIMARY KEY REFERENCES scans(id) ON DELETE CASCADE,
64
- review_items_total INTEGER NOT NULL DEFAULT 0 CHECK (review_items_total >= 0),
65
- review_items_completed INTEGER NOT NULL DEFAULT 0
66
- CHECK (review_items_completed >= 0 AND review_items_completed <= review_items_total),
67
- reportable_findings_count INTEGER NOT NULL DEFAULT 0
68
- CHECK (reportable_findings_count >= 0),
69
- deep_review_pass INTEGER CHECK (deep_review_pass IS NULL OR deep_review_pass >= 1),
70
- updated_at TEXT NOT NULL
71
- );
72
-
73
- CREATE TABLE scan_artifacts (
74
- scan_id TEXT NOT NULL REFERENCES scans(id) ON DELETE CASCADE,
75
- kind TEXT NOT NULL CHECK (
76
- kind IN ('coverage', 'findings', 'manifest', 'markdownReport')
77
- ),
78
- path TEXT NOT NULL,
79
- created_at TEXT NOT NULL,
80
- PRIMARY KEY (scan_id, kind)
81
- );
82
-
83
- CREATE TABLE findings (
84
- id TEXT PRIMARY KEY,
85
- fingerprint TEXT NOT NULL UNIQUE,
86
- rule_id TEXT NOT NULL,
87
- identity_anchor TEXT NOT NULL,
88
- identity_instance TEXT,
89
- created_at TEXT NOT NULL,
90
- updated_at TEXT NOT NULL
91
- );
92
-
93
- CREATE TABLE finding_occurrences (
94
- id TEXT PRIMARY KEY,
95
- finding_id TEXT NOT NULL REFERENCES findings(id),
96
- scan_id TEXT NOT NULL REFERENCES scans(id) ON DELETE CASCADE,
97
- title TEXT NOT NULL,
98
- summary TEXT NOT NULL,
99
- severity TEXT NOT NULL,
100
- confidence TEXT NOT NULL,
101
- remediation TEXT NOT NULL,
102
- created_at TEXT NOT NULL,
103
- UNIQUE (scan_id, finding_id)
104
- );
105
-
106
- CREATE TABLE finding_locations (
107
- id INTEGER PRIMARY KEY AUTOINCREMENT,
108
- occurrence_id TEXT NOT NULL REFERENCES finding_occurrences(id) ON DELETE CASCADE,
109
- relative_path TEXT NOT NULL,
110
- start_line INTEGER NOT NULL CHECK (start_line >= 1),
111
- end_line INTEGER NOT NULL CHECK (end_line >= start_line),
112
- role TEXT,
113
- sort_order INTEGER NOT NULL CHECK (sort_order >= 0),
114
- UNIQUE (occurrence_id, sort_order)
115
- );
116
-
117
- CREATE UNIQUE INDEX scans_one_running_per_workspace
118
- ON scans(workspace_id)
119
- WHERE status = 'running';
120
- """,
121
- ),
122
- (
123
- 2,
124
- "persist capability preflight summaries",
125
- """
126
- ALTER TABLE workspaces ADD COLUMN capability_preflight_json TEXT;
127
- """,
128
- ),
129
- (
130
- 3,
131
- "finding management schema",
132
- """
133
- CREATE TABLE finding_triage (
134
- occurrence_id TEXT PRIMARY KEY REFERENCES finding_occurrences(id) ON DELETE CASCADE,
135
- status TEXT NOT NULL CHECK (status IN ('open', 'closed')),
136
- close_reason TEXT CHECK (
137
- close_reason IS NULL OR close_reason IN ('already_fixed', 'wont_fix', 'false_positive')
138
- ),
139
- note TEXT,
140
- updated_at TEXT NOT NULL,
141
- CHECK (
142
- (status = 'open' AND close_reason IS NULL)
143
- OR (status = 'closed' AND close_reason IS NOT NULL)
144
- )
145
- );
146
-
147
- CREATE TABLE finding_remediation_attempts (
148
- request_id TEXT PRIMARY KEY,
149
- occurrence_id TEXT NOT NULL REFERENCES finding_occurrences(id) ON DELETE CASCADE,
150
- state TEXT NOT NULL CHECK (
151
- state IN ('idle', 'requested', 'generated', 'applied', 'verifying', 'verified', 'failed', 'superseded')
152
- ),
153
- version INTEGER NOT NULL CHECK (version >= 1),
154
- base_revision TEXT NOT NULL,
155
- base_content_digest TEXT,
156
- applied_content_digest TEXT,
157
- pending_action TEXT CHECK (
158
- pending_action IS NULL OR pending_action IN ('generate', 'apply', 'verify')
159
- ),
160
- patch_path TEXT,
161
- patch_digest TEXT,
162
- summary TEXT,
163
- verification_summary TEXT,
164
- created_at TEXT NOT NULL,
165
- updated_at TEXT NOT NULL
166
- );
167
-
168
- CREATE INDEX finding_remediation_attempts_by_occurrence
169
- ON finding_remediation_attempts(occurrence_id, created_at DESC);
170
-
171
- ALTER TABLE finding_occurrences
172
- ADD COLUMN details_json TEXT NOT NULL DEFAULT '{}';
173
-
174
- """,
175
- ),
176
- (
177
- 4,
178
- "scan handoff delivery claims",
179
- """
180
- ALTER TABLE scans
181
- ADD COLUMN handoff_claimed_at TEXT;
182
-
183
- ALTER TABLE scans
184
- ADD COLUMN handoff_claim_token TEXT;
185
- """,
186
- ),
187
- (
188
- 5,
189
- "finding remediation action claims",
190
- """
191
- ALTER TABLE finding_remediation_attempts
192
- ADD COLUMN pending_action_claimed_at TEXT;
193
-
194
- ALTER TABLE finding_remediation_attempts
195
- ADD COLUMN pending_action_claim_token TEXT;
196
- """,
197
- ),
198
- (
199
- 6,
200
- "thread-scoped workspaces",
201
- """
202
- ALTER TABLE workspaces
203
- ADD COLUMN thread_id TEXT;
204
-
205
- CREATE INDEX workspaces_by_thread_and_updated_at
206
- ON workspaces(thread_id, updated_at DESC);
207
- """,
208
- ),
209
- (
210
- 7,
211
- "remediation host delivery state",
212
- """
213
- ALTER TABLE finding_remediation_attempts
214
- ADD COLUMN pending_action_delivered_at TEXT;
215
- """,
216
- ),
217
- (
218
- 8,
219
- "sealed manifest digests",
220
- """
221
- ALTER TABLE scans
222
- ADD COLUMN seal_manifest_digest TEXT;
223
- """,
224
- ),
225
- (
226
- 9,
227
- "scan target filesystem identity",
228
- """
229
- ALTER TABLE scans
230
- ADD COLUMN target_device INTEGER;
231
-
232
- ALTER TABLE scans
233
- ADD COLUMN target_inode INTEGER;
234
- """,
235
- ),
236
- (
237
- 10,
238
- "scan cancellation state",
239
- """
240
- ALTER TABLE scans
241
- ADD COLUMN canceled_at TEXT;
242
- """,
243
- ),
244
- (
245
- 11,
246
- "deep scan orchestration state",
247
- """
248
- ALTER TABLE scans
249
- ADD COLUMN deep_scan_owner_thread_id TEXT;
250
-
251
- UPDATE scans
252
- SET deep_scan_owner_thread_id = (
253
- SELECT workspaces.thread_id
254
- FROM workspaces
255
- WHERE workspaces.id = scans.workspace_id
256
- )
257
- WHERE mode = 'deep'
258
- AND status = 'running'
259
- AND id IN (
260
- SELECT MIN(active_scans.id)
261
- FROM scans AS active_scans
262
- JOIN workspaces AS active_workspaces
263
- ON active_workspaces.id = active_scans.workspace_id
264
- WHERE active_scans.mode = 'deep'
265
- AND active_scans.status = 'running'
266
- AND active_workspaces.thread_id IS NOT NULL
267
- GROUP BY active_workspaces.thread_id,
268
- active_scans.target_path,
269
- active_scans.scope
270
- );
271
-
272
- CREATE UNIQUE INDEX scans_one_running_deep_per_owner_target
273
- ON scans(deep_scan_owner_thread_id, target_path, scope)
274
- WHERE mode = 'deep'
275
- AND status = 'running'
276
- AND deep_scan_owner_thread_id IS NOT NULL;
277
-
278
- CREATE TABLE deep_scan_runs (
279
- scan_id TEXT PRIMARY KEY REFERENCES scans(id) ON DELETE CASCADE,
280
- schema_version INTEGER NOT NULL CHECK (schema_version = 1),
281
- workflow_version TEXT NOT NULL,
282
- coordinator_generation INTEGER NOT NULL DEFAULT 1
283
- CHECK (coordinator_generation >= 1),
284
- status TEXT NOT NULL CHECK (
285
- status IN ('running', 'succeeded', 'failed', 'canceled', 'interrupted')
286
- ),
287
- phase TEXT NOT NULL CHECK (
288
- phase IN ('setup', 'discovery', 'reducing', 'terminal')
289
- ),
290
- workers INTEGER NOT NULL CHECK (workers >= 1),
291
- subagents INTEGER NOT NULL CHECK (subagents >= 0),
292
- stop_after_no_new INTEGER NOT NULL CHECK (stop_after_no_new >= 1),
293
- max_discovery_runs INTEGER NOT NULL CHECK (max_discovery_runs >= 1),
294
- discovery_runs_dispatched INTEGER NOT NULL DEFAULT 0
295
- CHECK (discovery_runs_dispatched >= 0),
296
- completion_sequence INTEGER NOT NULL DEFAULT 0
297
- CHECK (completion_sequence >= 0),
298
- consecutive_no_new INTEGER NOT NULL DEFAULT 0
299
- CHECK (consecutive_no_new >= 0),
300
- cancel_requested INTEGER NOT NULL DEFAULT 0
301
- CHECK (cancel_requested IN (0, 1)),
302
- canonical_inventory_path TEXT,
303
- canonical_finding_report_path TEXT,
304
- canonical_candidates_path TEXT,
305
- dedupe_report_path TEXT,
306
- seed_research_path TEXT,
307
- work_ledger_path TEXT,
308
- raw_candidates_path TEXT,
309
- coverage_ledger_path TEXT,
310
- findings_dir TEXT,
311
- manifest_path TEXT,
312
- terminal_reason TEXT CHECK (
313
- terminal_reason IS NULL OR terminal_reason IN ('saturated', 'capped')
314
- ),
315
- error_message TEXT,
316
- created_at TEXT NOT NULL,
317
- updated_at TEXT NOT NULL,
318
- completed_at TEXT,
319
- CHECK (
320
- (
321
- canonical_inventory_path IS NULL
322
- AND canonical_finding_report_path IS NULL
323
- AND canonical_candidates_path IS NULL
324
- AND dedupe_report_path IS NULL
325
- AND seed_research_path IS NULL
326
- AND work_ledger_path IS NULL
327
- AND raw_candidates_path IS NULL
328
- AND coverage_ledger_path IS NULL
329
- AND findings_dir IS NULL
330
- ) OR (
331
- canonical_inventory_path IS NOT NULL
332
- AND canonical_finding_report_path IS NOT NULL
333
- AND canonical_candidates_path IS NOT NULL
334
- AND dedupe_report_path IS NOT NULL
335
- AND seed_research_path IS NOT NULL
336
- AND work_ledger_path IS NOT NULL
337
- AND raw_candidates_path IS NOT NULL
338
- AND coverage_ledger_path IS NOT NULL
339
- AND findings_dir IS NOT NULL
340
- )
341
- )
342
- );
343
-
344
- CREATE TABLE deep_scan_workers (
345
- id TEXT PRIMARY KEY,
346
- scan_id TEXT NOT NULL REFERENCES deep_scan_runs(scan_id) ON DELETE CASCADE,
347
- kind TEXT NOT NULL CHECK (kind IN ('setup', 'discovery', 'dedup')),
348
- status TEXT NOT NULL CHECK (
349
- status IN ('queued', 'running', 'succeeded', 'failed', 'canceled')
350
- ),
351
- merge_state TEXT NOT NULL DEFAULT 'none' CHECK (
352
- merge_state IN ('none', 'buffered', 'merging', 'merged')
353
- ),
354
- prompt_path TEXT NOT NULL,
355
- artifact_dir TEXT NOT NULL,
356
- result_manifest_path TEXT,
357
- attempt INTEGER NOT NULL DEFAULT 0 CHECK (attempt >= 0),
358
- sdk_thread_id TEXT,
359
- completion_sequence INTEGER CHECK (completion_sequence >= 1),
360
- error_message TEXT,
361
- created_at TEXT NOT NULL,
362
- started_at TEXT,
363
- completed_at TEXT,
364
- updated_at TEXT NOT NULL,
365
- UNIQUE (scan_id, id),
366
- CHECK (kind = 'discovery' OR merge_state = 'none'),
367
- CHECK (kind = 'discovery' OR completion_sequence IS NULL)
368
- );
369
-
370
- CREATE UNIQUE INDEX deep_scan_workers_completion_sequence
371
- ON deep_scan_workers(scan_id, completion_sequence)
372
- WHERE completion_sequence IS NOT NULL;
373
-
374
- CREATE INDEX deep_scan_workers_by_scan_status
375
- ON deep_scan_workers(scan_id, status, kind, created_at);
376
-
377
- CREATE TABLE deep_scan_dedup_inputs (
378
- scan_id TEXT NOT NULL,
379
- dedup_worker_id TEXT NOT NULL,
380
- discovery_worker_id TEXT NOT NULL,
381
- input_order INTEGER NOT NULL CHECK (input_order >= 0),
382
- PRIMARY KEY (dedup_worker_id, discovery_worker_id),
383
- UNIQUE (dedup_worker_id, input_order),
384
- FOREIGN KEY (scan_id, dedup_worker_id)
385
- REFERENCES deep_scan_workers(scan_id, id) ON DELETE CASCADE,
386
- FOREIGN KEY (scan_id, discovery_worker_id)
387
- REFERENCES deep_scan_workers(scan_id, id) ON DELETE CASCADE
388
- );
389
- """,
390
- ),
391
- (
392
- 12,
393
- "scan continuation threads",
394
- """
395
- ALTER TABLE scans
396
- ADD COLUMN continuation_thread_id TEXT;
397
- """,
398
- ),
399
- (
400
- 13,
401
- "scan scope file counts",
402
- """
403
- ALTER TABLE scan_progress
404
- ADD COLUMN scope_file_count INTEGER
405
- CHECK (scope_file_count >= 0);
406
- """,
407
- ),
408
- (
409
- 14,
410
- "imported triage results",
411
- """
412
- CREATE TABLE triage_results (
413
- id TEXT PRIMARY KEY,
414
- repository_path TEXT NOT NULL,
415
- repository_revision TEXT,
416
- finding_count INTEGER NOT NULL CHECK (finding_count >= 0),
417
- result_json TEXT NOT NULL,
418
- created_at TEXT NOT NULL,
419
- updated_at TEXT NOT NULL
420
- );
421
-
422
- CREATE INDEX triage_results_by_updated_at
423
- ON triage_results(updated_at DESC, id);
424
- """,
425
- ),
426
- (
427
- 15,
428
- "append-only finding decisions",
429
- """
430
- CREATE TABLE finding_decisions (
431
- id TEXT PRIMARY KEY,
432
- occurrence_id TEXT NOT NULL REFERENCES finding_occurrences(id) ON DELETE CASCADE,
433
- status TEXT NOT NULL CHECK (status IN ('open', 'closed')),
434
- close_reason TEXT CHECK (
435
- close_reason IS NULL OR close_reason IN ('already_fixed', 'wont_fix', 'false_positive')
436
- ),
437
- note TEXT,
438
- created_at TEXT NOT NULL,
439
- CHECK (
440
- (status = 'open' AND close_reason IS NULL)
441
- OR (status = 'closed' AND close_reason IS NOT NULL)
442
- )
443
- );
444
-
445
- CREATE INDEX finding_decisions_by_occurrence
446
- ON finding_decisions(occurrence_id, created_at DESC, id DESC);
447
-
448
- INSERT INTO finding_decisions (
449
- id, occurrence_id, status, close_reason, note, created_at
450
- )
451
- SELECT
452
- 'legacy_' || occurrence_id,
453
- occurrence_id,
454
- status,
455
- close_reason,
456
- note,
457
- updated_at
458
- FROM finding_triage;
459
- """,
460
- ),
461
- (
462
- 16,
463
- "stable repository targets",
464
- """
465
- CREATE TABLE security_targets (
466
- id TEXT PRIMARY KEY,
467
- current_path TEXT NOT NULL UNIQUE,
468
- display_name TEXT NOT NULL,
469
- created_at TEXT NOT NULL,
470
- updated_at TEXT NOT NULL
471
- );
472
-
473
- ALTER TABLE workspaces
474
- ADD COLUMN target_id TEXT REFERENCES security_targets(id);
475
-
476
- ALTER TABLE scans
477
- ADD COLUMN target_id TEXT REFERENCES security_targets(id);
478
-
479
- CREATE INDEX scans_by_target
480
- ON scans(target_id, started_at DESC, id);
481
- """,
482
- ),
483
- (
484
- 17,
485
- "scan target summaries",
486
- """
487
- ALTER TABLE scans
488
- ADD COLUMN target_summary TEXT;
489
- """,
490
- ),
491
- (
492
- 18,
493
- "clear legacy delivered handoff claims",
494
- """
495
- UPDATE scans
496
- SET handoff_claimed_at = NULL, handoff_claim_token = NULL
497
- WHERE handoff_status = 'delivered';
498
- """,
499
- ),
500
- (
501
- 19,
502
- "persist setup workspace preference",
503
- """
504
- CREATE TABLE setup_preferences (
505
- singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
506
- skip_setup_ui INTEGER NOT NULL CHECK (skip_setup_ui IN (0, 1)),
507
- updated_at TEXT NOT NULL
508
- );
509
- """,
510
- ),
511
- (
512
- 20,
513
- "phase-specific scan progress",
514
- """
515
- ALTER TABLE scan_progress
516
- ADD COLUMN phase_items_total INTEGER NOT NULL DEFAULT 0
517
- CHECK (phase_items_total >= 0);
518
-
519
- ALTER TABLE scan_progress
520
- ADD COLUMN phase_items_completed INTEGER NOT NULL DEFAULT 0
521
- CHECK (phase_items_completed >= 0 AND phase_items_completed <= phase_items_total);
522
-
523
- ALTER TABLE scan_progress
524
- ADD COLUMN phase_progress_unit TEXT CHECK (
525
- phase_progress_unit IS NULL OR phase_progress_unit IN (
526
- 'checks',
527
- 'threat_surfaces',
528
- 'review_receipts',
529
- 'candidate_findings',
530
- 'validated_findings',
531
- 'report_artifacts'
532
- )
533
- );
534
- """,
535
- ),
536
- (
537
- 21,
538
- "current scan preflight state",
539
- """
540
- ALTER TABLE scan_progress
541
- ADD COLUMN preflight_issues_json TEXT NOT NULL DEFAULT '[]';
542
-
543
- ALTER TABLE scan_progress
544
- ADD COLUMN preflight_checks_total INTEGER NOT NULL DEFAULT 0
545
- CHECK (preflight_checks_total >= 0);
546
-
547
- ALTER TABLE scan_progress
548
- ADD COLUMN preflight_checks_completed INTEGER NOT NULL DEFAULT 0
549
- CHECK (
550
- preflight_checks_completed >= 0
551
- AND preflight_checks_completed <= preflight_checks_total
552
- );
553
-
554
- """,
555
- ),
556
- (
557
- 22,
558
- "replayable scan launch recipes",
559
- """
560
- ALTER TABLE scans ADD COLUMN recipe_json TEXT;
561
- ALTER TABLE scans ADD COLUMN parent_scan_id TEXT REFERENCES scans(id) ON DELETE SET NULL;
562
- """,
563
- ),
564
- (
565
- 23,
566
- "semantic scan comparison matches",
567
- """
568
- CREATE TABLE scan_comparisons (
569
- before_scan_id TEXT NOT NULL REFERENCES scans(id) ON DELETE CASCADE,
570
- after_scan_id TEXT NOT NULL REFERENCES scans(id) ON DELETE CASCADE,
571
- result_json TEXT NOT NULL,
572
- created_at TEXT NOT NULL,
573
- updated_at TEXT NOT NULL,
574
- PRIMARY KEY (before_scan_id, after_scan_id),
575
- CHECK (before_scan_id != after_scan_id)
576
- );
577
-
578
- CREATE TABLE scan_comparison_matches (
579
- before_scan_id TEXT NOT NULL,
580
- after_scan_id TEXT NOT NULL,
581
- before_occurrence_id TEXT NOT NULL
582
- REFERENCES finding_occurrences(id) ON DELETE CASCADE,
583
- after_occurrence_id TEXT NOT NULL
584
- REFERENCES finding_occurrences(id) ON DELETE CASCADE,
585
- reason TEXT NOT NULL,
586
- PRIMARY KEY (
587
- before_scan_id, after_scan_id, before_occurrence_id, after_occurrence_id
588
- ),
589
- FOREIGN KEY (before_scan_id, after_scan_id)
590
- REFERENCES scan_comparisons(before_scan_id, after_scan_id) ON DELETE CASCADE
591
- );
592
-
593
- CREATE INDEX scan_comparison_matches_by_before_occurrence
594
- ON scan_comparison_matches(before_occurrence_id);
595
-
596
- CREATE INDEX scan_comparison_matches_by_after_occurrence
597
- ON scan_comparison_matches(after_occurrence_id);
598
- """,
599
- ),
600
- (
601
- 24,
602
- "persist scan cost estimates",
603
- """
604
- ALTER TABLE scans ADD COLUMN cost_json TEXT;
605
- """,
606
- ),
607
- (
608
- 25,
609
- "persist scan model settings",
610
- """
611
- ALTER TABLE scans ADD COLUMN model TEXT;
612
- ALTER TABLE scans ADD COLUMN reasoning_effort TEXT;
613
- """,
614
- ),
615
- (
616
- 26,
617
- "persist scan completion warnings",
618
- """
619
- ALTER TABLE scans
620
- ADD COLUMN completion_warnings_json TEXT NOT NULL DEFAULT '[]';
621
- """,
622
- ),
623
- (
624
- 27,
625
- "persist deep scan consecutive discovery failures",
626
- """
627
- ALTER TABLE deep_scan_runs
628
- ADD COLUMN stop_after_consecutive_errors INTEGER NOT NULL DEFAULT 1
629
- CHECK (stop_after_consecutive_errors >= 1);
630
-
631
- ALTER TABLE deep_scan_runs
632
- ADD COLUMN consecutive_errors INTEGER NOT NULL DEFAULT 0
633
- CHECK (consecutive_errors >= 0);
634
-
635
- UPDATE deep_scan_runs
636
- SET stop_after_consecutive_errors = stop_after_no_new;
637
- """,
638
- ),
639
- (
640
- 28,
641
- "persist deep scan discovery time limit",
642
- """
643
- ALTER TABLE deep_scan_runs
644
- ADD COLUMN max_time_hours REAL NOT NULL DEFAULT 96;
645
- """,
646
- ),
647
- (
648
- 29,
649
- "persist finding publication associations",
650
- """
651
- CREATE TABLE finding_publications (
652
- id INTEGER PRIMARY KEY AUTOINCREMENT,
653
- scan_id TEXT NOT NULL REFERENCES scans(id) ON DELETE CASCADE,
654
- finding_id TEXT NOT NULL REFERENCES findings(id),
655
- occurrence_id TEXT NOT NULL
656
- REFERENCES finding_occurrences(id) ON DELETE CASCADE,
657
- destination_type TEXT NOT NULL,
658
- team_id TEXT,
659
- project_id TEXT,
660
- external_id TEXT NOT NULL,
661
- external_url TEXT,
662
- created_at TEXT NOT NULL,
663
- UNIQUE (occurrence_id, destination_type, team_id, project_id, external_id),
664
- UNIQUE (destination_type, team_id, project_id, external_id)
665
- );
666
-
667
- CREATE INDEX finding_publications_by_scan
668
- ON finding_publications(scan_id, occurrence_id, id);
669
-
670
- CREATE INDEX finding_publications_by_finding
671
- ON finding_publications(finding_id, id);
672
- """,
673
- ),
674
- (
675
- 30,
676
- "preserve team-only finding publication associations",
677
- """
678
- CREATE UNIQUE INDEX finding_publications_team_only_occurrence
679
- ON finding_publications(occurrence_id, destination_type, team_id, external_id)
680
- WHERE project_id IS NULL;
681
-
682
- CREATE UNIQUE INDEX finding_publications_team_only_external_issue
683
- ON finding_publications(destination_type, team_id, external_id)
684
- WHERE project_id IS NULL;
685
- """,
686
- ),
687
- (
688
- 31,
689
- "freeze stopped scan source digests",
690
- """
691
- ALTER TABLE scans ADD COLUMN retained_source_digests_json TEXT;
692
- """,
693
- ),
694
- (
695
- 32,
696
- "separate deep scan publication failures",
697
- """
698
- ALTER TABLE deep_scan_runs ADD COLUMN publication_error_message TEXT;
699
- """,
700
- ),
701
- (
702
- 33,
703
- "store complete findings and embeddings without a scan",
704
- """
705
- ALTER TABLE findings ADD COLUMN details_json TEXT;
706
-
707
- UPDATE findings SET details_json = (
708
- SELECT details_json FROM finding_occurrences
709
- WHERE finding_id = findings.id AND details_json != '{}'
710
- ORDER BY created_at DESC, id DESC LIMIT 1
711
- );
712
-
713
- CREATE TABLE finding_embeddings (
714
- finding_id TEXT PRIMARY KEY REFERENCES findings(id) ON DELETE CASCADE,
715
- model TEXT NOT NULL,
716
- vector_json TEXT NOT NULL
717
- );
718
-
719
- CREATE TRIGGER invalidate_finding_embedding
720
- AFTER UPDATE OF details_json ON findings
721
- WHEN OLD.details_json IS NOT NEW.details_json
722
- BEGIN
723
- DELETE FROM finding_embeddings WHERE finding_id = NEW.id;
724
- END;
725
- """,
726
- ),
727
- (
728
- 34,
729
- "associate findings with repositories",
730
- """
731
- CREATE TABLE finding_repositories (
732
- repository_id TEXT NOT NULL,
733
- finding_id TEXT NOT NULL REFERENCES findings(id) ON DELETE CASCADE,
734
- PRIMARY KEY (repository_id, finding_id)
735
- );
736
-
737
- INSERT OR IGNORE INTO finding_repositories (repository_id, finding_id)
738
- SELECT scans.target_id, finding_occurrences.finding_id
739
- FROM finding_occurrences JOIN scans ON scans.id = finding_occurrences.scan_id
740
- WHERE scans.target_id IS NOT NULL;
741
- """,
742
- ),
743
- (
744
- 35,
745
- "persist finding dedupe groups",
746
- """
747
- CREATE TABLE finding_dedupe_groups (
748
- id TEXT PRIMARY KEY,
749
- created_at TEXT NOT NULL
750
- );
751
-
752
- CREATE TABLE finding_dedupe_group_members (
753
- group_id TEXT NOT NULL REFERENCES finding_dedupe_groups(id) ON DELETE CASCADE,
754
- finding_id TEXT NOT NULL REFERENCES findings(id),
755
- PRIMARY KEY (group_id, finding_id)
756
- );
757
-
758
- CREATE INDEX finding_dedupe_groups_by_finding
759
- ON finding_dedupe_group_members(finding_id, group_id);
760
- """,
761
- ),
762
- (
763
- 36,
764
- "persist local findings workflows",
765
- """
766
- CREATE TABLE finding_workflows (
767
- id TEXT PRIMARY KEY,
768
- state_json TEXT NOT NULL,
769
- created_at TEXT NOT NULL,
770
- updated_at TEXT NOT NULL
771
- );
772
- """,
773
- ),
774
- (
775
- 37,
776
- "checkpoint validated dedupe reviews",
777
- """
778
- CREATE TABLE finding_workflow_reviews (
779
- workflow_id TEXT NOT NULL REFERENCES finding_workflows(id) ON DELETE CASCADE,
780
- review_key TEXT NOT NULL,
781
- binding_json TEXT NOT NULL,
782
- result_json TEXT NOT NULL,
783
- created_at TEXT NOT NULL,
784
- PRIMARY KEY (workflow_id, review_key)
785
- );
786
- """,
787
- ),
788
- (
789
- 38,
790
- "store findings workflow metadata in columns",
791
- """
792
- ALTER TABLE finding_workflows RENAME COLUMN state_json TO results_json;
793
- ALTER TABLE finding_workflows ADD COLUMN repository_path TEXT;
794
- ALTER TABLE finding_workflows ADD COLUMN scan_request_digest TEXT;
795
- ALTER TABLE finding_workflows ADD COLUMN scan_id TEXT;
796
- ALTER TABLE finding_workflows ADD COLUMN scan_dir TEXT;
797
- ALTER TABLE finding_workflows ADD COLUMN artifact_digest TEXT;
798
- ALTER TABLE finding_workflows ADD COLUMN destination TEXT;
799
- ALTER TABLE finding_workflows ADD COLUMN scope_repository_id TEXT;
800
- ALTER TABLE finding_workflows ADD COLUMN scope_all_repositories INTEGER;
801
- ALTER TABLE finding_workflows ADD COLUMN scan_status TEXT NOT NULL DEFAULT 'pending';
802
- ALTER TABLE finding_workflows ADD COLUMN scan_error TEXT;
803
- ALTER TABLE finding_workflows ADD COLUMN publish_status TEXT NOT NULL DEFAULT 'pending';
804
- ALTER TABLE finding_workflows ADD COLUMN publish_error TEXT;
805
- ALTER TABLE finding_workflows ADD COLUMN dedupe_status TEXT NOT NULL DEFAULT 'pending';
806
- ALTER TABLE finding_workflows ADD COLUMN dedupe_error TEXT;
807
- """,
808
- ),
809
- (
810
- 39,
811
- "store dedupe checkpoint bindings in columns",
812
- """
813
- ALTER TABLE finding_workflow_reviews RENAME COLUMN binding_json TO prompt_digest;
814
- ALTER TABLE finding_workflow_reviews ADD COLUMN review_contract_version INTEGER;
815
- ALTER TABLE finding_workflow_reviews ADD COLUMN codex_version TEXT;
816
- ALTER TABLE finding_workflow_reviews ADD COLUMN source_repository_path TEXT;
817
- ALTER TABLE finding_workflow_reviews ADD COLUMN source_revision TEXT;
818
- ALTER TABLE finding_workflow_reviews ADD COLUMN source_refs_digest TEXT;
819
- ALTER TABLE finding_workflow_reviews ADD COLUMN source_content_digest TEXT;
820
- ALTER TABLE finding_workflow_reviews ADD COLUMN scope_repository_id TEXT;
821
- ALTER TABLE finding_workflow_reviews ADD COLUMN scope_all_repositories INTEGER;
822
- ALTER TABLE finding_workflow_reviews ADD COLUMN model TEXT;
823
- ALTER TABLE finding_workflow_reviews ADD COLUMN effort TEXT;
824
- ALTER TABLE finding_workflow_reviews ADD COLUMN settings_digest TEXT;
825
- ALTER TABLE finding_workflow_reviews ADD COLUMN contract_digest TEXT;
826
- """,
827
- ),
828
- (
829
- 40,
830
- "index finding identity and comparison history",
831
- """
832
- CREATE INDEX finding_occurrences_by_finding
833
- ON finding_occurrences(finding_id, id);
834
-
835
- CREATE INDEX scan_comparisons_by_after_scan
836
- ON scan_comparisons(after_scan_id, before_scan_id);
837
- """,
838
- ),
839
- (
840
- 41,
841
- "checkpoint finding severity assessments",
842
- """
843
- CREATE TABLE finding_severity_assessments (
844
- finding_id TEXT PRIMARY KEY REFERENCES findings(id) ON DELETE CASCADE,
845
- occurrence_id TEXT,
846
- input_sha256 TEXT NOT NULL,
847
- rubric_sha256 TEXT,
848
- knowledge_base_sha256 TEXT,
849
- assessed_at TEXT NOT NULL,
850
- source TEXT NOT NULL CHECK (source IN ('existing-severity', 'rubric')),
851
- decision TEXT NOT NULL CHECK (decision IN ('assessed', 'excluded')),
852
- level TEXT CHECK (level IN ('critical', 'high', 'medium', 'low', 'informational')),
853
- rubric_label TEXT,
854
- rationale TEXT NOT NULL,
855
- confidence TEXT CHECK (confidence IN ('high', 'medium', 'low')),
856
- review_trigger TEXT,
857
- CHECK ((decision = 'assessed' AND level IS NOT NULL)
858
- OR (decision = 'excluded' AND level IS NULL AND rubric_label IS NULL))
859
- );
860
-
861
- CREATE TABLE scan_severity_classifications (
862
- scan_id TEXT PRIMARY KEY,
863
- finding_ids_json TEXT NOT NULL,
864
- assessed_at TEXT NOT NULL,
865
- rubric_sha256 TEXT,
866
- knowledge_base_sha256 TEXT
867
- );
868
- """,
869
- ),
7
+ from pathlib import Path
8
+
9
+ _SHARED = Path(__file__).resolve().parents[1] / "shared"
10
+ _HISTORY = json.loads((_SHARED / "workbench-migrations.json").read_text(encoding="utf-8"))
11
+ _REPAIR_HISTORY = json.dumps(_HISTORY)
12
+ _REPAIR_PLAN = json.loads((_SHARED / "workbench-history-repairs.json").read_text(encoding="utf-8"))
13
+ MIGRATIONS = tuple(
14
+ (migration["version"], migration["name"], "\n".join(migration["statements"]))
15
+ for migration in _HISTORY
870
16
  )
871
17
 
872
18
 
873
- def migrate_finding_workflow_review_columns(connection: sqlite3.Connection) -> None:
874
- for row in connection.execute(
875
- "SELECT workflow_id, review_key, prompt_digest FROM finding_workflow_reviews"
876
- ).fetchall():
877
- binding = json.loads(row["prompt_digest"])
878
- source = binding["source"]
879
- scope = binding["scope"]
880
- connection.execute(
881
- """UPDATE finding_workflow_reviews SET review_contract_version = ?, codex_version = ?,
882
- source_repository_path = ?, source_revision = ?, source_refs_digest = ?,
883
- source_content_digest = ?, scope_repository_id = ?, scope_all_repositories = ?,
884
- model = ?, effort = ?, settings_digest = ?, prompt_digest = ?, contract_digest = ?
885
- WHERE workflow_id = ? AND review_key = ?""",
886
- (
887
- binding["version"],
888
- binding["codexVersion"],
889
- source["repository"],
890
- source["revision"],
891
- source["refsDigest"],
892
- source["content"],
893
- scope.get("repositoryId"),
894
- scope.get("allRepositories"),
895
- binding["model"],
896
- binding["effort"],
897
- binding.get("settingsDigest"),
898
- binding["promptDigest"],
899
- binding["contractDigest"],
900
- row["workflow_id"],
901
- row["review_key"],
902
- ),
903
- )
19
+ def migration_json_extract(document: str, path: str) -> str | int | float | None:
20
+ # The fixed scalar paths in migrations 38/39 must retain embedded NULs on older SQLite.
21
+ value = json.loads(document)
22
+ for key in path[2:].split("."):
23
+ value = value.get(key) if isinstance(value, dict) else None
24
+ return value
904
25
 
905
26
 
906
- def migrate_finding_workflow_columns(connection: sqlite3.Connection) -> None:
907
- # Rename/backfill in place so existing checkpoint foreign keys and rows survive.
27
+ def migrate_finding_workflow_results(connection: sqlite3.Connection) -> None:
908
28
  for row in connection.execute("SELECT id, results_json FROM finding_workflows").fetchall():
909
- state = json.loads(row["results_json"])
910
- scope = state.get("scope", {})
911
- stages = state["stages"]
29
+ stages = json.loads(row["results_json"])["stages"]
912
30
  results = {stage: value["result"] for stage, value in stages.items() if "result" in value}
913
31
  if "pendingWrite" in stages["dedupe"]:
914
32
  results["dedupePendingWrite"] = stages["dedupe"]["pendingWrite"]
33
+ # SQLite JSON extraction on older runtimes truncates embedded NULs in diagnostics.
915
34
  connection.execute(
916
- """UPDATE finding_workflows SET
917
- repository_path = ?, scan_request_digest = ?, scan_id = ?, scan_dir = ?,
918
- artifact_digest = ?, destination = ?, scope_repository_id = ?, scope_all_repositories = ?,
919
- scan_status = ?, scan_error = ?, publish_status = ?, publish_error = ?,
920
- dedupe_status = ?, dedupe_error = ?, results_json = ? WHERE id = ?""",
35
+ "UPDATE finding_workflows SET scan_error = ?, publish_error = ?, dedupe_error = ?, "
36
+ "results_json = ? WHERE id = ?",
921
37
  (
922
- state.get("repositoryPath"),
923
- state.get("scanRequestDigest"),
924
- state.get("scanId"),
925
- state.get("scanDir"),
926
- state.get("artifactDigest"),
927
- state.get("destination"),
928
- scope.get("repositoryId"),
929
- scope.get("allRepositories"),
930
- stages["scan"]["status"],
931
38
  stages["scan"].get("error"),
932
- stages["publish"]["status"],
933
39
  stages["publish"].get("error"),
934
- stages["dedupe"]["status"],
935
40
  stages["dedupe"].get("error"),
936
41
  json.dumps(results, allow_nan=False),
937
42
  row["id"],
@@ -944,10 +49,12 @@ def apply_migrations(
944
49
  migrations: tuple[tuple[int, str, str], ...],
945
50
  now: Callable[[], str],
946
51
  backfill_security_targets: Callable[[sqlite3.Connection], None],
52
+ *,
53
+ immediate: bool = False,
947
54
  ) -> None:
948
55
  connection.commit()
949
- connection.execute("BEGIN IMMEDIATE")
950
- try:
56
+ connection.execute("BEGIN IMMEDIATE" if immediate else "BEGIN")
57
+ with connection:
951
58
  connection.execute(
952
59
  """
953
60
  CREATE TABLE IF NOT EXISTS schema_migrations (
@@ -963,442 +70,47 @@ def apply_migrations(
963
70
  }
964
71
  should_backfill_targets = False
965
72
  for version, name, sql in migrations:
966
- if version in applied:
967
- if version == 2:
968
- add_column_if_missing(
969
- connection, "workspaces", "capability_preflight_json", "TEXT"
970
- )
971
- elif version == 6:
972
- repair_thread_scoped_workspaces_migration(connection)
973
- elif version == 11:
974
- repair_deep_scan_migration(connection)
975
- elif version == 12:
976
- add_column_if_missing(connection, "scans", "continuation_thread_id", "TEXT")
977
- elif version == 13:
978
- add_column_if_missing(
979
- connection,
980
- "scan_progress",
981
- "scope_file_count",
982
- "INTEGER CHECK (scope_file_count >= 0)",
983
- )
984
- elif version == 16:
985
- should_backfill_targets = repair_stable_targets_migration(connection)
986
- elif version == 26:
987
- add_column_if_missing(
988
- connection,
989
- "scans",
990
- "completion_warnings_json",
991
- "TEXT NOT NULL DEFAULT '[]'",
992
- )
993
- elif version == 28:
994
- add_column_if_missing(
995
- connection,
996
- "deep_scan_runs",
997
- "max_time_hours",
998
- "REAL NOT NULL DEFAULT 96",
999
- )
1000
- elif version == 31:
1001
- add_column_if_missing(
1002
- connection,
1003
- "scans",
1004
- "retained_source_digests_json",
1005
- "TEXT",
1006
- )
1007
- elif version == 32:
1008
- add_column_if_missing(
1009
- connection,
1010
- "deep_scan_runs",
1011
- "publication_error_message",
1012
- "TEXT",
1013
- )
1014
- continue
1015
73
  if version == 6:
1016
74
  repair_thread_scoped_workspaces_migration(connection)
1017
75
  elif version == 16:
1018
76
  should_backfill_targets = repair_stable_targets_migration(connection)
77
+ elif version in applied:
78
+ if version in (2, 12, 13, 26, 28, 31, 32, 47):
79
+ repair_additive_migration(connection, version)
80
+ elif version == 11:
81
+ repair_deep_scan_migration(connection)
1019
82
  else:
83
+ if version in (38, 39):
84
+ connection.create_function("migration_json_extract", 2, migration_json_extract)
85
+ sql = sql.replace("json_extract(", "migration_json_extract(")
1020
86
  for statement in sql_statements(sql):
1021
87
  connection.execute(statement)
1022
88
  if version == 38:
1023
- migrate_finding_workflow_columns(connection)
1024
- elif version == 39:
1025
- migrate_finding_workflow_review_columns(connection)
1026
- connection.execute(
1027
- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",
1028
- (version, name, now()),
1029
- )
89
+ migrate_finding_workflow_results(connection)
90
+ if version not in applied:
91
+ connection.execute(
92
+ "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",
93
+ (version, name, now()),
94
+ )
1030
95
  if 27 in applied:
1031
96
  repair_deep_scan_failure_counter_migration(connection)
1032
97
  if should_backfill_targets:
1033
98
  backfill_security_targets(connection)
1034
- connection.commit()
1035
- except BaseException:
1036
- connection.rollback()
1037
- raise
1038
-
1039
-
1040
- def normalize_pre_release_execution_profile_migrations(
1041
- connection: sqlite3.Connection, timestamp: str
1042
- ) -> None:
1043
- scan_columns = {row["name"] for row in connection.execute("PRAGMA table_info(scans)")}
1044
- workspace_columns = {row["name"] for row in connection.execute("PRAGMA table_info(workspaces)")}
1045
- legacy_columns = {"execution_model", "reasoning_effort"}
1046
- renamed_columns = {
1047
- "legacy_execution_model",
1048
- "legacy_reasoning_effort",
1049
- }
1050
- execution_migrations = {
1051
- row["version"]: row["name"]
1052
- for row in connection.execute(
1053
- "SELECT version, name FROM schema_migrations WHERE version IN (11, 12, 25)"
1054
- )
1055
- }
1056
- supported_execution_migrations = {
1057
- 11: {"deep scan orchestration state", "scan execution profiles"},
1058
- 12: {
1059
- "scan continuation threads",
1060
- "scan execution profiles",
1061
- "dynamic scan execution profiles",
1062
- },
1063
- }
1064
- model_migration_name = "persist scan model settings"
1065
- if execution_migrations.get(25) == "dynamic scan execution profiles":
1066
- connection.execute(
1067
- "UPDATE schema_migrations SET name = ? WHERE version = 25 AND name = ?",
1068
- (model_migration_name, "dynamic scan execution profiles"),
1069
- )
1070
- execution_migrations[25] = model_migration_name
1071
- has_legacy_profile_history = any(
1072
- execution_migrations.get(version) in legacy_names
1073
- for version, legacy_names in (
1074
- (11, {"scan execution profiles"}),
1075
- (12, {"scan execution profiles", "dynamic scan execution profiles"}),
1076
- )
1077
- )
1078
- has_legacy_profile_columns = any(
1079
- column in columns
1080
- for column, columns in (
1081
- ("execution_model", scan_columns),
1082
- ("execution_model", workspace_columns),
1083
- ("reasoning_effort", workspace_columns),
1084
- )
1085
- )
1086
- if not (has_legacy_profile_history or has_legacy_profile_columns):
1087
- return
1088
-
1089
- if any(
1090
- execution_migrations.get(version) not in ({None} | supported_names)
1091
- for version, supported_names in supported_execution_migrations.items()
1092
- ):
1093
- raise SystemExit(
1094
- "The Codex Security database has an unsupported execution-profile migration history."
1095
- )
1096
-
1097
- if has_legacy_profile_columns and not (
1098
- legacy_columns <= scan_columns
1099
- and legacy_columns <= workspace_columns
1100
- and not renamed_columns.intersection(scan_columns | workspace_columns)
1101
- ):
1102
- raise SystemExit(
1103
- "The Codex Security database has an unsupported execution-profile migration history."
1104
- )
1105
- if has_legacy_profile_history and not has_legacy_profile_columns:
1106
- raise SystemExit(
1107
- "The Codex Security database has an unsupported execution-profile migration history."
1108
- )
1109
-
1110
- if execution_migrations.get(25) not in (None, model_migration_name):
1111
- raise SystemExit(
1112
- "The Codex Security database has an unsupported execution-profile migration history."
1113
- )
1114
-
1115
- # Keep the historical values and constraints for recovery while moving
1116
- # them out of the namespace used by the current independent scan settings.
1117
- for table in ("workspaces", "scans"):
1118
- connection.execute(
1119
- f"ALTER TABLE {table} RENAME COLUMN execution_model TO legacy_execution_model"
1120
- )
1121
- connection.execute(
1122
- f"ALTER TABLE {table} RENAME COLUMN reasoning_effort TO legacy_reasoning_effort"
1123
- )
1124
- add_column_if_missing(connection, "scans", "model", "TEXT")
1125
- add_column_if_missing(connection, "scans", "reasoning_effort", "TEXT")
1126
- connection.execute(
1127
- """
1128
- UPDATE scans
1129
- SET model = COALESCE(model, legacy_execution_model),
1130
- reasoning_effort = COALESCE(reasoning_effort, legacy_reasoning_effort)
1131
- """
1132
- )
1133
- for version, name in (
1134
- (11, "scan execution profiles"),
1135
- (12, "scan execution profiles"),
1136
- (12, "dynamic scan execution profiles"),
1137
- ):
1138
- connection.execute(
1139
- "DELETE FROM schema_migrations WHERE version = ? AND name = ?",
1140
- (version, name),
1141
- )
1142
- if execution_migrations.get(25) is None:
1143
- connection.execute(
1144
- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",
1145
- (25, model_migration_name, timestamp),
1146
- )
1147
99
 
1148
100
 
1149
101
  def normalize_pre_release_migrations(connection: sqlite3.Connection, timestamp: str) -> None:
1150
- normalize_mirror_lineage_migrations(connection)
1151
- connection.execute(
1152
- "UPDATE schema_migrations SET version = 40 WHERE version = 33 AND name = ?",
1153
- ("index finding identity and comparison history",),
1154
- )
1155
-
1156
- completion_warning_migration = connection.execute(
1157
- "SELECT name FROM schema_migrations WHERE version = 25"
1158
- ).fetchone()
1159
- if (
1160
- completion_warning_migration is not None
1161
- and completion_warning_migration["name"] == "persist scan completion warnings"
1162
- ):
1163
- if (
1164
- connection.execute("SELECT 1 FROM schema_migrations WHERE version = 26").fetchone()
1165
- is not None
1166
- ):
1167
- raise SystemExit(
1168
- "The Codex Security database has an unsupported pre-release migration history."
1169
- )
1170
- connection.execute(
1171
- "UPDATE schema_migrations SET version = 26 WHERE version = 25 AND name = ?",
1172
- ("persist scan completion warnings",),
1173
- )
1174
-
1175
- phase_progress_migration = connection.execute(
1176
- "SELECT name FROM schema_migrations WHERE version = 12"
1177
- ).fetchone()
1178
- if (
1179
- phase_progress_migration is not None
1180
- and phase_progress_migration["name"] == "phase-specific scan progress"
1181
- ):
1182
- target_migration = connection.execute(
1183
- "SELECT name FROM schema_migrations WHERE version = 20"
1184
- ).fetchone()
1185
- if target_migration is not None:
1186
- raise SystemExit(
1187
- "The Codex Security database has an unsupported pre-release migration history."
1188
- )
1189
- connection.execute(
1190
- "UPDATE schema_migrations SET version = 20 WHERE version = 12 AND name = ?",
1191
- ("phase-specific scan progress",),
1192
- )
1193
-
1194
- normalize_pre_release_execution_profile_migrations(connection, timestamp)
1195
-
1196
- preflight_progress_migration = connection.execute(
1197
- "SELECT name FROM schema_migrations WHERE version = 13"
1198
- ).fetchone()
1199
- if (
1200
- preflight_progress_migration is not None
1201
- and preflight_progress_migration["name"] == "current scan preflight state"
1202
- ):
1203
- target_migration = connection.execute(
1204
- "SELECT name FROM schema_migrations WHERE version = 21"
1205
- ).fetchone()
1206
- if target_migration is not None:
1207
- raise SystemExit(
1208
- "The Codex Security database has an unsupported pre-release migration history."
1209
- )
1210
- connection.execute(
1211
- "UPDATE schema_migrations SET version = 21 WHERE version = 13 AND name = ?",
1212
- ("current scan preflight state",),
1213
- )
1214
-
1215
- delivered_claim_migration = connection.execute(
1216
- "SELECT name FROM schema_migrations WHERE version = 18"
1217
- ).fetchone()
1218
- if (
1219
- delivered_claim_migration is not None
1220
- and delivered_claim_migration["name"] == "scan target summaries"
1221
- ):
1222
- connection.execute(
1223
- "UPDATE scans SET handoff_claimed_at = NULL, handoff_claim_token = NULL "
1224
- "WHERE handoff_status = 'delivered'"
1225
- )
1226
- connection.execute(
1227
- "UPDATE schema_migrations SET name = ? WHERE version = 18 AND name = ?",
1228
- ("clear legacy delivered handoff claims", "scan target summaries"),
1229
- )
1230
-
1231
- setup_preferences_migration = connection.execute(
1232
- "SELECT name FROM schema_migrations WHERE version = 19"
1233
- ).fetchone()
1234
- legacy_setup_preferences_migrations = {
1235
- "structured scan guidance context",
1236
- "idempotent scan lifecycle requests",
1237
- }
1238
- if (
1239
- setup_preferences_migration is not None
1240
- and setup_preferences_migration["name"] in legacy_setup_preferences_migrations
1241
- ):
1242
- migration_sql = next(sql for version, _, sql in MIGRATIONS if version == 19)
1243
- for statement in sql_statements(migration_sql):
1244
- connection.execute(statement.replace("CREATE TABLE ", "CREATE TABLE IF NOT EXISTS ", 1))
1245
- connection.execute(
1246
- "UPDATE schema_migrations SET name = ? WHERE version = 19 AND name = ?",
1247
- ("persist setup workspace preference", setup_preferences_migration["name"]),
1248
- )
1249
-
1250
- phase_progress_migration = connection.execute(
1251
- "SELECT name FROM schema_migrations WHERE version = 20"
1252
- ).fetchone()
1253
- legacy_phase_progress_migrations = {
1254
- "retain superseded scan lifecycle requests",
1255
- "threat model publication receipts",
1256
- }
1257
- if (
1258
- phase_progress_migration is not None
1259
- and phase_progress_migration["name"] in legacy_phase_progress_migrations
1260
- ):
1261
- add_column_if_missing(
1262
- connection,
1263
- "scan_progress",
1264
- "phase_items_total",
1265
- "INTEGER NOT NULL DEFAULT 0 CHECK (phase_items_total >= 0)",
1266
- )
1267
- add_column_if_missing(
1268
- connection,
1269
- "scan_progress",
1270
- "phase_items_completed",
1271
- "INTEGER NOT NULL DEFAULT 0 "
1272
- "CHECK (phase_items_completed >= 0 AND phase_items_completed <= phase_items_total)",
1273
- )
1274
- add_column_if_missing(
1275
- connection,
1276
- "scan_progress",
1277
- "phase_progress_unit",
1278
- "TEXT CHECK (phase_progress_unit IS NULL OR phase_progress_unit IN ("
1279
- "'checks', 'threat_surfaces', 'review_receipts', 'candidate_findings', "
1280
- "'validated_findings', 'report_artifacts'))",
1281
- )
1282
- connection.execute(
1283
- "UPDATE schema_migrations SET name = ? WHERE version = 20 AND name = ?",
1284
- ("phase-specific scan progress", phase_progress_migration["name"]),
1285
- )
1286
-
1287
- preflight_progress_migration = connection.execute(
1288
- "SELECT name FROM schema_migrations WHERE version = 21"
1289
- ).fetchone()
1290
- legacy_preflight_progress_migrations = {
1291
- "scan progress projection and activity",
1292
- "deep coordinator manifest receipts",
1293
- }
1294
- if (
1295
- preflight_progress_migration is not None
1296
- and preflight_progress_migration["name"] in legacy_preflight_progress_migrations
1297
- ):
1298
- add_column_if_missing(
1299
- connection,
1300
- "scan_progress",
1301
- "preflight_issues_json",
1302
- "TEXT NOT NULL DEFAULT '[]'",
1303
- )
1304
- add_column_if_missing(
1305
- connection,
1306
- "scan_progress",
1307
- "preflight_checks_total",
1308
- "INTEGER NOT NULL DEFAULT 0 CHECK (preflight_checks_total >= 0)",
1309
- )
1310
- add_column_if_missing(
1311
- connection,
1312
- "scan_progress",
1313
- "preflight_checks_completed",
1314
- "INTEGER NOT NULL DEFAULT 0 CHECK (preflight_checks_completed >= 0 "
1315
- "AND preflight_checks_completed <= preflight_checks_total)",
1316
- )
1317
- connection.execute(
1318
- "UPDATE schema_migrations SET name = ? WHERE version = 21 AND name = ?",
1319
- ("current scan preflight state", preflight_progress_migration["name"]),
1320
- )
1321
-
1322
- scan_recipe_migration = connection.execute(
1323
- "SELECT name FROM schema_migrations WHERE version = 22"
1324
- ).fetchone()
1325
- if (
1326
- scan_recipe_migration is not None
1327
- and scan_recipe_migration["name"] == "dynamic scan execution profiles"
1328
- ):
1329
- add_column_if_missing(connection, "scans", "recipe_json", "TEXT")
1330
- add_column_if_missing(
1331
- connection,
1332
- "scans",
1333
- "parent_scan_id",
1334
- "TEXT REFERENCES scans(id) ON DELETE SET NULL",
1335
- )
1336
- connection.execute(
1337
- "UPDATE schema_migrations SET name = ? WHERE version = 22 AND name = ?",
1338
- ("replayable scan launch recipes", "dynamic scan execution profiles"),
1339
- )
1340
-
1341
- migration = connection.execute(
1342
- "SELECT name FROM schema_migrations WHERE version = 2"
1343
- ).fetchone()
1344
- if migration is None or migration["name"] != "finding management schema":
1345
- return
1346
-
1347
- legacy_versions = {
1348
- row["version"]: row["name"]
1349
- for row in connection.execute(
1350
- "SELECT version, name FROM schema_migrations WHERE version BETWEEN 2 AND 5"
1351
- )
1352
- }
1353
- expected = {
1354
- 2: "finding management schema",
1355
- 3: "scan handoff delivery claims",
1356
- 4: "finding remediation action claims",
1357
- 5: "scan target snapshot digests",
1358
- }
1359
- for version, name in legacy_versions.items():
1360
- if expected.get(version) != name:
1361
- raise SystemExit(
1362
- "The Codex Security database has an unsupported pre-release migration history."
1363
- )
1364
-
1365
- connection.execute(
1366
- "DELETE FROM schema_migrations WHERE version = 5 AND name = ?",
1367
- (expected[5],),
1368
- )
1369
- for old_version, new_version in ((4, 5), (3, 4), (2, 3)):
1370
- connection.execute(
1371
- "UPDATE schema_migrations SET version = ? WHERE version = ? AND name = ?",
1372
- (new_version, old_version, expected[old_version]),
1373
- )
1374
- add_column_if_missing(connection, "workspaces", "capability_preflight_json", "TEXT")
1375
- add_column_if_missing(connection, "scans", "target_snapshot_digest", "TEXT")
1376
- connection.execute(
1377
- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",
1378
- (2, "persist capability preflight summaries", timestamp),
1379
- )
1380
-
1381
-
1382
- def normalize_mirror_lineage_migrations(connection: sqlite3.Connection) -> None:
1383
- mirror_names = {
1384
- 29: "freeze stopped scan source digests",
1385
- 30: "separate deep scan publication failures",
1386
- }
1387
- migrations = {
1388
- row["version"]: row["name"]
1389
- for row in connection.execute(
1390
- "SELECT version, name FROM schema_migrations WHERE version BETWEEN 29 AND 32"
1391
- )
1392
- }
1393
- if not any(migrations.get(version) == name for version, name in mirror_names.items()):
1394
- return
1395
- if migrations != mirror_names:
1396
- raise SystemExit("The Codex Security database has an unsupported mirror migration history.")
1397
- for old_version, new_version in ((30, 32), (29, 31)):
1398
- connection.execute(
1399
- "UPDATE schema_migrations SET version = ? WHERE version = ? AND name = ?",
1400
- (new_version, old_version, mirror_names[old_version]),
1401
- )
102
+ for query in _REPAIR_PLAN:
103
+ actions = connection.execute("\n".join(query), {"history": _REPAIR_HISTORY}).fetchall()
104
+ for action in actions:
105
+ operation, value = action["operation"], action["value"]
106
+ if operation == "error":
107
+ raise SystemExit(value)
108
+ if operation == "additive":
109
+ repair_additive_migration(connection, value)
110
+ elif operation == "column":
111
+ add_migration_column(connection, value)
112
+ else:
113
+ connection.execute(value, (timestamp,) if operation == "timestamp" else ())
1402
114
 
1403
115
 
1404
116
  def repair_deep_scan_migration(connection: sqlite3.Connection) -> None:
@@ -1444,24 +156,17 @@ def repair_deep_scan_migration(connection: sqlite3.Connection) -> None:
1444
156
 
1445
157
 
1446
158
  def repair_deep_scan_failure_counter_migration(connection: sqlite3.Connection) -> None:
159
+ threshold_column, count_column, _backfill = sql_statements(
160
+ next(sql for version, _, sql in MIGRATIONS if version == 27)
161
+ )
1447
162
  columns = {row["name"] for row in connection.execute("PRAGMA table_info(deep_scan_runs)")}
1448
163
  threshold_missing = "stop_after_consecutive_errors" not in columns
1449
- add_column_if_missing(
1450
- connection,
1451
- "deep_scan_runs",
1452
- "stop_after_consecutive_errors",
1453
- "INTEGER NOT NULL DEFAULT 1 CHECK (stop_after_consecutive_errors >= 1)",
1454
- )
164
+ add_migration_column(connection, threshold_column)
1455
165
  if threshold_missing:
1456
166
  connection.execute(
1457
167
  "UPDATE deep_scan_runs SET stop_after_consecutive_errors = stop_after_no_new"
1458
168
  )
1459
- add_column_if_missing(
1460
- connection,
1461
- "deep_scan_runs",
1462
- "consecutive_errors",
1463
- "INTEGER NOT NULL DEFAULT 0 CHECK (consecutive_errors >= 0)",
1464
- )
169
+ add_migration_column(connection, count_column)
1465
170
 
1466
171
 
1467
172
  def repair_thread_scoped_workspaces_migration(connection: sqlite3.Connection) -> None:
@@ -1490,21 +195,8 @@ def repair_stable_targets_migration(connection: sqlite3.Connection) -> bool:
1490
195
 
1491
196
  migration_sql = next(sql for version, _, sql in MIGRATIONS if version == 16)
1492
197
  for statement in sql_statements(migration_sql):
1493
- if statement.startswith("ALTER TABLE workspaces"):
1494
- add_column_if_missing(
1495
- connection,
1496
- "workspaces",
1497
- "target_id",
1498
- "TEXT REFERENCES security_targets(id)",
1499
- )
1500
- continue
1501
- if statement.startswith("ALTER TABLE scans"):
1502
- add_column_if_missing(
1503
- connection,
1504
- "scans",
1505
- "target_id",
1506
- "TEXT REFERENCES security_targets(id)",
1507
- )
198
+ if statement.startswith(("ALTER TABLE workspaces", "ALTER TABLE scans")):
199
+ add_migration_column(connection, statement)
1508
200
  continue
1509
201
  statement = statement.replace("CREATE TABLE ", "CREATE TABLE IF NOT EXISTS ", 1)
1510
202
  statement = statement.replace("CREATE INDEX ", "CREATE INDEX IF NOT EXISTS ", 1)
@@ -1522,6 +214,19 @@ def repair_stable_targets_migration(connection: sqlite3.Connection) -> bool:
1522
214
  return True
1523
215
 
1524
216
 
217
+ def repair_additive_migration(connection: sqlite3.Connection, version: int) -> None:
218
+ sql = next(sql for migration_version, _, sql in MIGRATIONS if migration_version == version)
219
+ for statement in sql_statements(sql):
220
+ add_migration_column(connection, statement)
221
+
222
+
223
+ def add_migration_column(connection: sqlite3.Connection, statement: str) -> None:
224
+ _, _, table, _, _, column, definition = statement.removesuffix(";").split(None, 6)
225
+ # Preserve the compact declarations used by historical repairs, including CHECK errors.
226
+ definition = " ".join(definition.split()).replace("( ", "(").replace(" )", ")")
227
+ add_column_if_missing(connection, table, column, definition)
228
+
229
+
1525
230
  def add_column_if_missing(
1526
231
  connection: sqlite3.Connection, table: str, column: str, definition: str
1527
232
  ) -> None:
@@ -1543,9 +248,5 @@ def sql_statements(script: str) -> list[str]:
1543
248
  return statements
1544
249
 
1545
250
 
1546
- def main() -> None:
1547
- argparse.ArgumentParser(description=__doc__).parse_args()
1548
-
1549
-
1550
251
  if __name__ == "__main__":
1551
- main()
252
+ argparse.ArgumentParser(description=__doc__).parse_args()