@openai/codex-security 0.1.30 → 0.2.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 (320) hide show
  1. package/README.md +386 -37
  2. package/_bundled_plugin/.mcp.json +1 -0
  3. package/_bundled_plugin/mcp/helpers.mjs.br.part-000 +0 -0
  4. package/_bundled_plugin/mcp/native/THIRD_PARTY_NOTICES.txt +4 -4
  5. package/_bundled_plugin/mcp/native/darwin-arm64/unix.node +0 -0
  6. package/_bundled_plugin/mcp/native/darwin-x64/unix.node +0 -0
  7. package/_bundled_plugin/mcp/native/linux-arm64-gnu/unix.node +0 -0
  8. package/_bundled_plugin/mcp/native/linux-arm64-musl/unix.node +0 -0
  9. package/_bundled_plugin/mcp/native/linux-x64-gnu/unix.node +0 -0
  10. package/_bundled_plugin/mcp/native/linux-x64-musl/unix.node +0 -0
  11. package/_bundled_plugin/mcp/native/win32-arm64/windows.node +0 -0
  12. package/_bundled_plugin/mcp/native/win32-x64/windows.node +0 -0
  13. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  14. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  15. package/_bundled_plugin/references/artifact-storage.md +4 -4
  16. package/_bundled_plugin/references/config-preflight.md +1 -1
  17. package/_bundled_plugin/references/core-scan.md +14 -2
  18. package/_bundled_plugin/references/final-report.md +4 -2
  19. package/_bundled_plugin/references/scan-artifacts.md +2 -2
  20. package/_bundled_plugin/references/scan-contract.md +2 -2
  21. package/_bundled_plugin/references/threat-model.md +2 -2
  22. package/_bundled_plugin/schemas/coverage.schema.json +22 -0
  23. package/_bundled_plugin/schemas/scan-manifest.schema.json +100 -85
  24. package/_bundled_plugin/schemas/tools/deep-reducer.schema.json +19 -4
  25. package/_bundled_plugin/schemas/tools/scan-draft.schema.json +116 -102
  26. package/_bundled_plugin/scripts/config_preflight.py +4 -8
  27. package/_bundled_plugin/scripts/deep_scan_workbench.py +83 -217
  28. package/_bundled_plugin/scripts/filesystem_identity.py +1 -5
  29. package/_bundled_plugin/scripts/finalize_scan_contract.py +441 -166
  30. package/_bundled_plugin/scripts/finding_preview.py +42 -82
  31. package/_bundled_plugin/scripts/generate_in_scope_files.py +30 -34
  32. package/_bundled_plugin/scripts/generate_rank_input.py +58 -99
  33. package/_bundled_plugin/scripts/launch_codex_security_mcp +5 -1
  34. package/_bundled_plugin/scripts/rank_preview.py +86 -68
  35. package/_bundled_plugin/scripts/report_projection.py +37 -58
  36. package/_bundled_plugin/scripts/threat_model_projection.py +164 -0
  37. package/_bundled_plugin/scripts/windows_scan_local_files.py +11 -6
  38. package/_bundled_plugin/scripts/workbench/storage.py +11 -0
  39. package/_bundled_plugin/scripts/workbench_cli.py +11 -17
  40. package/_bundled_plugin/scripts/workbench_constants.py +6 -14
  41. package/_bundled_plugin/scripts/workbench_db.py +116 -228
  42. package/_bundled_plugin/scripts/workbench_finding_index.py +1 -6
  43. package/_bundled_plugin/scripts/workbench_native_indexes.py +1 -4
  44. package/_bundled_plugin/scripts/workbench_progress.py +12 -34
  45. package/_bundled_plugin/scripts/workbench_publication.py +47 -62
  46. package/_bundled_plugin/scripts/workbench_remediation.py +2 -20
  47. package/_bundled_plugin/scripts/workbench_saved_results.py +1246 -230
  48. package/_bundled_plugin/scripts/workbench_scan_start.py +4 -11
  49. package/_bundled_plugin/scripts/workbench_scan_usage.py +7 -15
  50. package/_bundled_plugin/scripts/workbench_schema.py +86 -271
  51. package/_bundled_plugin/scripts/workbench_severity.py +3 -0
  52. package/_bundled_plugin/scripts/workbench_source_excerpt.py +5 -6
  53. package/_bundled_plugin/scripts/workbench_target.py +98 -10
  54. package/_bundled_plugin/scripts/workbench_target_state.py +1 -5
  55. package/_bundled_plugin/scripts/workbench_validation.py +6 -20
  56. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +3 -3
  57. package/_bundled_plugin/skills/security-diff-scan/SKILL.md +3 -3
  58. package/_bundled_plugin/skills/security-scan/SKILL.md +2 -2
  59. package/_bundled_plugin/skills/security-scan/references/desktop-scan.md +2 -2
  60. package/_bundled_plugin/skills/threat-model/SKILL.md +2 -2
  61. package/dist/api.d.ts +3 -5
  62. package/dist/api.d.ts.map +1 -1
  63. package/dist/api.js +201 -207
  64. package/dist/api.js.map +1 -1
  65. package/dist/artifact-export.d.ts +70 -0
  66. package/dist/artifact-export.d.ts.map +1 -0
  67. package/dist/artifact-export.js +277 -0
  68. package/dist/artifact-export.js.map +1 -0
  69. package/dist/auth.d.ts.map +1 -1
  70. package/dist/auth.js +9 -4
  71. package/dist/auth.js.map +1 -1
  72. package/dist/bulk-scan-discovery.d.ts +1 -0
  73. package/dist/bulk-scan-discovery.d.ts.map +1 -1
  74. package/dist/bulk-scan-discovery.js +8 -9
  75. package/dist/bulk-scan-discovery.js.map +1 -1
  76. package/dist/classify-scan-severity.d.ts.map +1 -1
  77. package/dist/classify-scan-severity.js +2 -5
  78. package/dist/classify-scan-severity.js.map +1 -1
  79. package/dist/classify-severity.d.ts +2 -8
  80. package/dist/classify-severity.d.ts.map +1 -1
  81. package/dist/classify-severity.js +3 -5
  82. package/dist/classify-severity.js.map +1 -1
  83. package/dist/cli-help.d.ts +3 -0
  84. package/dist/cli-help.d.ts.map +1 -0
  85. package/dist/cli-help.js +281 -0
  86. package/dist/cli-help.js.map +1 -0
  87. package/dist/cli.d.ts +21 -22
  88. package/dist/cli.d.ts.map +1 -1
  89. package/dist/cli.js +673 -701
  90. package/dist/cli.js.map +1 -1
  91. package/dist/cloud-publish.d.ts.map +1 -1
  92. package/dist/cloud-publish.js +1 -8
  93. package/dist/cloud-publish.js.map +1 -1
  94. package/dist/codex-prompt.d.ts.map +1 -1
  95. package/dist/codex-prompt.js +1 -2
  96. package/dist/codex-prompt.js.map +1 -1
  97. package/dist/codex-sdk-environment.d.ts +2 -0
  98. package/dist/codex-sdk-environment.d.ts.map +1 -0
  99. package/dist/codex-sdk-environment.js +30 -0
  100. package/dist/codex-sdk-environment.js.map +1 -0
  101. package/dist/component-plan.d.ts.map +1 -1
  102. package/dist/component-plan.js +2 -6
  103. package/dist/component-plan.js.map +1 -1
  104. package/dist/component-scan.d.ts +1 -0
  105. package/dist/component-scan.d.ts.map +1 -1
  106. package/dist/component-scan.js +31 -9
  107. package/dist/component-scan.js.map +1 -1
  108. package/dist/config.d.ts.map +1 -1
  109. package/dist/config.js +32 -33
  110. package/dist/config.js.map +1 -1
  111. package/dist/contract.d.ts +2 -1
  112. package/dist/contract.d.ts.map +1 -1
  113. package/dist/contract.js +63 -122
  114. package/dist/contract.js.map +1 -1
  115. package/dist/cost-model.d.ts.map +1 -1
  116. package/dist/cost-model.js +65 -34
  117. package/dist/cost-model.js.map +1 -1
  118. package/dist/cost.d.ts.map +1 -1
  119. package/dist/cost.js +15 -31
  120. package/dist/cost.js.map +1 -1
  121. package/dist/custom-validation-prompt.js +4 -4
  122. package/dist/custom-validation-prompt.js.map +1 -1
  123. package/dist/custom-validation.d.ts.map +1 -1
  124. package/dist/custom-validation.js +17 -16
  125. package/dist/custom-validation.js.map +1 -1
  126. package/dist/deduplication/checkpointed-review.d.ts.map +1 -1
  127. package/dist/deduplication/checkpointed-review.js +10 -11
  128. package/dist/deduplication/checkpointed-review.js.map +1 -1
  129. package/dist/deduplication/codex-review.d.ts +2 -0
  130. package/dist/deduplication/codex-review.d.ts.map +1 -1
  131. package/dist/deduplication/codex-review.js +3 -3
  132. package/dist/deduplication/codex-review.js.map +1 -1
  133. package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -1
  134. package/dist/deduplication/deduplication-reviewer.js +2 -0
  135. package/dist/deduplication/deduplication-reviewer.js.map +1 -1
  136. package/dist/deduplication/deduplication.js +40 -62
  137. package/dist/deduplication/deduplication.js.map +1 -1
  138. package/dist/deduplication/records-protocol.d.ts.map +1 -1
  139. package/dist/deduplication/records-protocol.js +1 -4
  140. package/dist/deduplication/records-protocol.js.map +1 -1
  141. package/dist/deduplication/records.d.ts +2 -1
  142. package/dist/deduplication/records.d.ts.map +1 -1
  143. package/dist/deduplication/records.js +24 -6
  144. package/dist/deduplication/records.js.map +1 -1
  145. package/dist/deduplication/review.d.ts +7 -0
  146. package/dist/deduplication/review.d.ts.map +1 -1
  147. package/dist/deduplication/review.js.map +1 -1
  148. package/dist/deduplication/scan.js +3 -7
  149. package/dist/deduplication/scan.js.map +1 -1
  150. package/dist/deep-config.d.ts.map +1 -1
  151. package/dist/deep-config.js +3 -19
  152. package/dist/deep-config.js.map +1 -1
  153. package/dist/deep-progress.d.ts +0 -1
  154. package/dist/deep-progress.d.ts.map +1 -1
  155. package/dist/deep-progress.js +22 -40
  156. package/dist/deep-progress.js.map +1 -1
  157. package/dist/errors.d.ts +3 -2
  158. package/dist/errors.d.ts.map +1 -1
  159. package/dist/errors.js +5 -7
  160. package/dist/errors.js.map +1 -1
  161. package/dist/finding-catalogue.d.ts.map +1 -1
  162. package/dist/finding-catalogue.js +22 -22
  163. package/dist/finding-catalogue.js.map +1 -1
  164. package/dist/finding-workflow.d.ts.map +1 -1
  165. package/dist/finding-workflow.js +4 -7
  166. package/dist/finding-workflow.js.map +1 -1
  167. package/dist/findings-import.d.ts.map +1 -1
  168. package/dist/findings-import.js +7 -17
  169. package/dist/findings-import.js.map +1 -1
  170. package/dist/github.d.ts.map +1 -1
  171. package/dist/github.js +2 -3
  172. package/dist/github.js.map +1 -1
  173. package/dist/import-scan.js +2 -2
  174. package/dist/import-scan.js.map +1 -1
  175. package/dist/index.d.ts +6 -2
  176. package/dist/index.d.ts.map +1 -1
  177. package/dist/index.js +3 -1
  178. package/dist/index.js.map +1 -1
  179. package/dist/knowledge-base.js +7 -3
  180. package/dist/knowledge-base.js.map +1 -1
  181. package/dist/linear.d.ts.map +1 -1
  182. package/dist/linear.js +2 -3
  183. package/dist/linear.js.map +1 -1
  184. package/dist/models.d.ts +24 -0
  185. package/dist/models.d.ts.map +1 -1
  186. package/dist/models.js.map +1 -1
  187. package/dist/multiscan.d.ts.map +1 -1
  188. package/dist/multiscan.js +36 -31
  189. package/dist/multiscan.js.map +1 -1
  190. package/dist/owner-evidence.d.ts +29 -0
  191. package/dist/owner-evidence.d.ts.map +1 -0
  192. package/dist/owner-evidence.js +192 -0
  193. package/dist/owner-evidence.js.map +1 -0
  194. package/dist/patch-tui.d.ts.map +1 -1
  195. package/dist/patch-tui.js +2 -4
  196. package/dist/patch-tui.js.map +1 -1
  197. package/dist/project-config-schema.d.ts +10 -0
  198. package/dist/project-config-schema.d.ts.map +1 -1
  199. package/dist/project-config-schema.js +1 -0
  200. package/dist/project-config-schema.js.map +1 -1
  201. package/dist/project-config.d.ts +1 -0
  202. package/dist/project-config.d.ts.map +1 -1
  203. package/dist/project-config.js +2 -0
  204. package/dist/project-config.js.map +1 -1
  205. package/dist/prompt-files.d.ts +0 -1
  206. package/dist/prompt-files.d.ts.map +1 -1
  207. package/dist/prompt-files.js +2 -4
  208. package/dist/prompt-files.js.map +1 -1
  209. package/dist/publication-events.d.ts.map +1 -1
  210. package/dist/publication-events.js +15 -29
  211. package/dist/publication-events.js.map +1 -1
  212. package/dist/publication-store.d.ts.map +1 -1
  213. package/dist/publication-store.js +9 -13
  214. package/dist/publication-store.js.map +1 -1
  215. package/dist/publication.d.ts.map +1 -1
  216. package/dist/publication.js +11 -14
  217. package/dist/publication.js.map +1 -1
  218. package/dist/publish.d.ts.map +1 -1
  219. package/dist/publish.js +31 -41
  220. package/dist/publish.js.map +1 -1
  221. package/dist/record.d.ts +2 -0
  222. package/dist/record.d.ts.map +1 -0
  223. package/dist/record.js +4 -0
  224. package/dist/record.js.map +1 -0
  225. package/dist/result.d.ts +4 -1
  226. package/dist/result.d.ts.map +1 -1
  227. package/dist/result.js +7 -0
  228. package/dist/result.js.map +1 -1
  229. package/dist/runtime.d.ts +10 -13
  230. package/dist/runtime.d.ts.map +1 -1
  231. package/dist/runtime.js +230 -145
  232. package/dist/runtime.js.map +1 -1
  233. package/dist/saved-scan.d.ts.map +1 -1
  234. package/dist/saved-scan.js +10 -20
  235. package/dist/saved-scan.js.map +1 -1
  236. package/dist/scan-activity.d.ts.map +1 -1
  237. package/dist/scan-activity.js +8 -22
  238. package/dist/scan-activity.js.map +1 -1
  239. package/dist/scan-comparison.d.ts +4 -4
  240. package/dist/scan-comparison.d.ts.map +1 -1
  241. package/dist/scan-comparison.js +38 -73
  242. package/dist/scan-comparison.js.map +1 -1
  243. package/dist/scan-dashboard.d.ts +0 -1
  244. package/dist/scan-dashboard.d.ts.map +1 -1
  245. package/dist/scan-dashboard.js +10 -19
  246. package/dist/scan-dashboard.js.map +1 -1
  247. package/dist/scan-history-renderer.d.ts.map +1 -1
  248. package/dist/scan-history-renderer.js +1 -2
  249. package/dist/scan-history-renderer.js.map +1 -1
  250. package/dist/scan-logs.d.ts.map +1 -1
  251. package/dist/scan-logs.js +2 -6
  252. package/dist/scan-logs.js.map +1 -1
  253. package/dist/scan-sessions.d.ts.map +1 -1
  254. package/dist/scan-sessions.js +1 -3
  255. package/dist/scan-sessions.js.map +1 -1
  256. package/dist/scan-settings.d.ts +5 -0
  257. package/dist/scan-settings.d.ts.map +1 -1
  258. package/dist/scan-settings.js +4 -0
  259. package/dist/scan-settings.js.map +1 -1
  260. package/dist/security-policy-cli.d.ts.map +1 -1
  261. package/dist/security-policy-cli.js +8 -6
  262. package/dist/security-policy-cli.js.map +1 -1
  263. package/dist/security-policy.d.ts +8 -4
  264. package/dist/security-policy.d.ts.map +1 -1
  265. package/dist/security-policy.js +98 -84
  266. package/dist/security-policy.js.map +1 -1
  267. package/dist/server/dashboard/app.js +5 -5
  268. package/dist/server/embeddings.d.ts.map +1 -1
  269. package/dist/server/embeddings.js +1 -0
  270. package/dist/server/embeddings.js.map +1 -1
  271. package/dist/server/routes.d.ts +3 -2
  272. package/dist/server/routes.d.ts.map +1 -1
  273. package/dist/server/routes.js +19 -7
  274. package/dist/server/routes.js.map +1 -1
  275. package/dist/server/server.d.ts.map +1 -1
  276. package/dist/server/server.js +2 -3
  277. package/dist/server/server.js.map +1 -1
  278. package/dist/server/sqlite-store.d.ts.map +1 -1
  279. package/dist/server/sqlite-store.js +4 -14
  280. package/dist/server/sqlite-store.js.map +1 -1
  281. package/dist/severity-store.js +8 -15
  282. package/dist/severity-store.js.map +1 -1
  283. package/dist/suggest-owners.d.ts +37 -0
  284. package/dist/suggest-owners.d.ts.map +1 -0
  285. package/dist/suggest-owners.js +111 -0
  286. package/dist/suggest-owners.js.map +1 -0
  287. package/dist/targets.d.ts +7 -2
  288. package/dist/targets.d.ts.map +1 -1
  289. package/dist/targets.js +29 -25
  290. package/dist/targets.js.map +1 -1
  291. package/dist/thread-source.d.ts +1 -0
  292. package/dist/thread-source.d.ts.map +1 -1
  293. package/dist/thread-source.js +1 -0
  294. package/dist/thread-source.js.map +1 -1
  295. package/dist/trusted-executable.d.ts +5 -0
  296. package/dist/trusted-executable.d.ts.map +1 -1
  297. package/dist/trusted-executable.js +22 -5
  298. package/dist/trusted-executable.js.map +1 -1
  299. package/dist/value.d.ts +2 -0
  300. package/dist/value.d.ts.map +1 -0
  301. package/dist/value.js +20 -0
  302. package/dist/value.js.map +1 -0
  303. package/dist/version.d.ts.map +1 -1
  304. package/dist/version.js +3 -6
  305. package/dist/version.js.map +1 -1
  306. package/dist/windows-path.d.ts.map +1 -1
  307. package/dist/windows-path.js +1 -2
  308. package/dist/windows-path.js.map +1 -1
  309. package/dist/worker-progress.d.ts +1 -0
  310. package/dist/worker-progress.d.ts.map +1 -1
  311. package/dist/worker-progress.js +13 -34
  312. package/dist/worker-progress.js.map +1 -1
  313. package/docs/dedupe-records.md +24 -0
  314. package/package.json +15 -13
  315. package/schemas/project-config.schema.json +5 -0
  316. package/_bundled_plugin/scripts/normalize_candidates.py +0 -338
  317. package/dist/server/findings-service.d.ts +0 -20
  318. package/dist/server/findings-service.d.ts.map +0 -1
  319. package/dist/server/findings-service.js +0 -31
  320. package/dist/server/findings-service.js.map +0 -1
@@ -22,7 +22,7 @@ from workbench_target import (
22
22
  git_revision,
23
23
  worktree_content_digest,
24
24
  )
25
- from workbench_validation import optional_text, user_text
25
+ from workbench_validation import optional_text
26
26
 
27
27
 
28
28
  def safe_segment(value: str) -> str:
@@ -168,7 +168,7 @@ def insert_running_scan(
168
168
  ) -> str:
169
169
  revision = target_identity[0]
170
170
  native_scan = scan_dir is None
171
- user_context = user_text(workspace["user_context"])
171
+ user_context = optional_text(workspace["user_context"])
172
172
  if scan_dir is None:
173
173
  scan_dir = Path(
174
174
  tempfile.mkdtemp(
@@ -197,10 +197,7 @@ def insert_running_scan(
197
197
  workspace["default_mode"],
198
198
  user_context,
199
199
  workspace["thread_id"] if workspace["default_mode"] == "deep" else None,
200
- diff_target["kind"] if diff_target else None,
201
- diff_target["baseRevision"] if diff_target else None,
202
- diff_target["headRevision"] if diff_target else None,
203
- diff_target.get("contentDigest") if diff_target else None,
200
+ *scan_diff_identity(diff_target or None),
204
201
  target_summary,
205
202
  str(scan_dir),
206
203
  optional_text(model, maximum=200),
@@ -236,9 +233,5 @@ def insert_running_scan(
236
233
  return scan_id
237
234
 
238
235
 
239
- def main() -> None:
240
- argparse.ArgumentParser(description=__doc__).parse_args()
241
-
242
-
243
236
  if __name__ == "__main__":
244
- main()
237
+ argparse.ArgumentParser(description=__doc__).parse_args()
@@ -70,15 +70,11 @@ def reconcile_completed_scan_cost(
70
70
  allow_nan=False,
71
71
  )
72
72
  connection.execute("BEGIN IMMEDIATE")
73
- try:
73
+ with connection:
74
74
  connection.execute(
75
75
  "UPDATE scans SET cost_json = ? WHERE id = ? AND status = 'complete'",
76
76
  (cost_json, scan["id"]),
77
77
  )
78
- connection.commit()
79
- except BaseException:
80
- connection.rollback()
81
- raise
82
78
 
83
79
 
84
80
  def collect_scan_usage(
@@ -117,7 +113,6 @@ def collect_scan_usage(
117
113
  return _unavailable_usage("scan_thread_unavailable", warnings=warnings)
118
114
 
119
115
  total = _empty_token_usage()
120
- observed_thread_count = 0
121
116
  accepted_thread_ids: set[str] = set()
122
117
  excluded_thread_ids: set[str] = set()
123
118
  for session in sessions:
@@ -151,17 +146,16 @@ def collect_scan_usage(
151
146
  missing_thread_ids.add(session.thread_id)
152
147
  continue
153
148
  accepted_thread_ids.add(session.thread_id)
154
- observed_thread_count += 1
155
149
  _add_token_usage(total, session_usage)
156
150
 
157
- if not observed_thread_count:
151
+ if not accepted_thread_ids:
158
152
  return _unavailable_usage("scan_thread_unavailable", warnings=warnings)
159
153
 
160
154
  result: dict[str, Any] = {
161
155
  "coverage": "partial" if missing_thread_ids or warnings else "complete",
162
156
  "source": "codex_rollout",
163
157
  **total,
164
- "threadCount": observed_thread_count,
158
+ "threadCount": len(accepted_thread_ids),
165
159
  }
166
160
  if missing_thread_ids:
167
161
  result["missingThreadCount"] = len(missing_thread_ids)
@@ -202,13 +196,11 @@ def _scan_root_thread_ids(
202
196
  (scan["id"],),
203
197
  )
204
198
  )
205
- roots: list[str] = []
206
- seen: set[str] = set()
199
+ roots: dict[str, None] = {}
207
200
  for candidate in candidates:
208
- if isinstance(candidate, str) and candidate.strip() and candidate not in seen:
209
- roots.append(candidate)
210
- seen.add(candidate)
211
- return roots
201
+ if isinstance(candidate, str) and candidate.strip():
202
+ roots[candidate] = None
203
+ return list(roots)
212
204
 
213
205
 
214
206
  def _scan_execution_thread_ids(connection: sqlite3.Connection, scan: sqlite3.Row) -> list[str]:
@@ -944,10 +944,12 @@ def apply_migrations(
944
944
  migrations: tuple[tuple[int, str, str], ...],
945
945
  now: Callable[[], str],
946
946
  backfill_security_targets: Callable[[sqlite3.Connection], None],
947
+ *,
948
+ immediate: bool = False,
947
949
  ) -> None:
948
950
  connection.commit()
949
- connection.execute("BEGIN IMMEDIATE")
950
- try:
951
+ connection.execute("BEGIN IMMEDIATE" if immediate else "BEGIN")
952
+ with connection:
951
953
  connection.execute(
952
954
  """
953
955
  CREATE TABLE IF NOT EXISTS schema_migrations (
@@ -963,59 +965,15 @@ def apply_migrations(
963
965
  }
964
966
  should_backfill_targets = False
965
967
  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
968
  if version == 6:
1016
969
  repair_thread_scoped_workspaces_migration(connection)
1017
970
  elif version == 16:
1018
971
  should_backfill_targets = repair_stable_targets_migration(connection)
972
+ elif version in applied:
973
+ if version in (2, 12, 13, 26, 28, 31, 32):
974
+ repair_additive_migration(connection, version)
975
+ elif version == 11:
976
+ repair_deep_scan_migration(connection)
1019
977
  else:
1020
978
  for statement in sql_statements(sql):
1021
979
  connection.execute(statement)
@@ -1023,18 +981,15 @@ def apply_migrations(
1023
981
  migrate_finding_workflow_columns(connection)
1024
982
  elif version == 39:
1025
983
  migrate_finding_workflow_review_columns(connection)
1026
- connection.execute(
1027
- "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",
1028
- (version, name, now()),
1029
- )
984
+ if version not in applied:
985
+ connection.execute(
986
+ "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",
987
+ (version, name, now()),
988
+ )
1030
989
  if 27 in applied:
1031
990
  repair_deep_scan_failure_counter_migration(connection)
1032
991
  if should_backfill_targets:
1033
992
  backfill_security_targets(connection)
1034
- connection.commit()
1035
- except BaseException:
1036
- connection.rollback()
1037
- raise
1038
993
 
1039
994
 
1040
995
  def normalize_pre_release_execution_profile_migrations(
@@ -1121,8 +1076,7 @@ def normalize_pre_release_execution_profile_migrations(
1121
1076
  connection.execute(
1122
1077
  f"ALTER TABLE {table} RENAME COLUMN reasoning_effort TO legacy_reasoning_effort"
1123
1078
  )
1124
- add_column_if_missing(connection, "scans", "model", "TEXT")
1125
- add_column_if_missing(connection, "scans", "reasoning_effort", "TEXT")
1079
+ repair_additive_migration(connection, 25)
1126
1080
  connection.execute(
1127
1081
  """
1128
1082
  UPDATE scans
@@ -1146,196 +1100,68 @@ def normalize_pre_release_execution_profile_migrations(
1146
1100
  )
1147
1101
 
1148
1102
 
1149
- 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"
1103
+ def move_pre_release_migration(
1104
+ connection: sqlite3.Connection, old_version: int, new_version: int, name: str
1105
+ ) -> None:
1106
+ migration = connection.execute(
1107
+ "SELECT name FROM schema_migrations WHERE version = ?", (old_version,)
1158
1108
  ).fetchone()
1109
+ if migration is None or migration["name"] != name:
1110
+ return
1159
1111
  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
1112
  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"
1113
+ "SELECT 1 FROM schema_migrations WHERE version = ?", (new_version,)
1184
1114
  ).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"
1115
+ is not None
1202
1116
  ):
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",),
1117
+ raise SystemExit(
1118
+ "The Codex Security database has an unsupported pre-release migration history."
1213
1119
  )
1120
+ connection.execute(
1121
+ "UPDATE schema_migrations SET version = ? WHERE version = ? AND name = ?",
1122
+ (new_version, old_version, name),
1123
+ )
1214
1124
 
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
1125
 
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
- )
1126
+ def normalize_pre_release_migrations(connection: sqlite3.Connection, timestamp: str) -> None:
1127
+ normalize_mirror_lineage_migrations(connection)
1128
+ move_pre_release_migration(connection, 33, 40, "index finding identity and comparison history")
1249
1129
 
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
- )
1130
+ move_pre_release_migration(connection, 25, 26, "persist scan completion warnings")
1131
+ move_pre_release_migration(connection, 12, 20, "phase-specific scan progress")
1286
1132
 
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
- )
1133
+ normalize_pre_release_execution_profile_migrations(connection, timestamp)
1321
1134
 
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"
1135
+ move_pre_release_migration(connection, 13, 21, "current scan preflight state")
1136
+
1137
+ for version, legacy_names in (
1138
+ (18, {"scan target summaries"}),
1139
+ (19, {"structured scan guidance context", "idempotent scan lifecycle requests"}),
1140
+ (20, {"retain superseded scan lifecycle requests", "threat model publication receipts"}),
1141
+ (21, {"scan progress projection and activity", "deep coordinator manifest receipts"}),
1142
+ (22, {"dynamic scan execution profiles"}),
1328
1143
  ):
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
- )
1144
+ migration = connection.execute(
1145
+ "SELECT name FROM schema_migrations WHERE version = ?", (version,)
1146
+ ).fetchone()
1147
+ if migration is None or migration["name"] not in legacy_names:
1148
+ continue
1149
+ _, name, sql = next(migration for migration in MIGRATIONS if migration[0] == version)
1150
+ if version == 18:
1151
+ connection.execute(
1152
+ "UPDATE scans SET handoff_claimed_at = NULL, handoff_claim_token = NULL "
1153
+ "WHERE handoff_status = 'delivered'"
1154
+ )
1155
+ elif version == 19:
1156
+ for statement in sql_statements(sql):
1157
+ connection.execute(
1158
+ statement.replace("CREATE TABLE ", "CREATE TABLE IF NOT EXISTS ", 1)
1159
+ )
1160
+ else:
1161
+ repair_additive_migration(connection, version)
1336
1162
  connection.execute(
1337
- "UPDATE schema_migrations SET name = ? WHERE version = 22 AND name = ?",
1338
- ("replayable scan launch recipes", "dynamic scan execution profiles"),
1163
+ "UPDATE schema_migrations SET name = ? WHERE version = ? AND name = ?",
1164
+ (name, version, migration["name"]),
1339
1165
  )
1340
1166
 
1341
1167
  migration = connection.execute(
@@ -1371,7 +1197,7 @@ def normalize_pre_release_migrations(connection: sqlite3.Connection, timestamp:
1371
1197
  "UPDATE schema_migrations SET version = ? WHERE version = ? AND name = ?",
1372
1198
  (new_version, old_version, expected[old_version]),
1373
1199
  )
1374
- add_column_if_missing(connection, "workspaces", "capability_preflight_json", "TEXT")
1200
+ repair_additive_migration(connection, 2)
1375
1201
  add_column_if_missing(connection, "scans", "target_snapshot_digest", "TEXT")
1376
1202
  connection.execute(
1377
1203
  "INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)",
@@ -1444,24 +1270,17 @@ def repair_deep_scan_migration(connection: sqlite3.Connection) -> None:
1444
1270
 
1445
1271
 
1446
1272
  def repair_deep_scan_failure_counter_migration(connection: sqlite3.Connection) -> None:
1273
+ threshold_column, count_column, _backfill = sql_statements(
1274
+ next(sql for version, _, sql in MIGRATIONS if version == 27)
1275
+ )
1447
1276
  columns = {row["name"] for row in connection.execute("PRAGMA table_info(deep_scan_runs)")}
1448
1277
  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
- )
1278
+ add_migration_column(connection, threshold_column)
1455
1279
  if threshold_missing:
1456
1280
  connection.execute(
1457
1281
  "UPDATE deep_scan_runs SET stop_after_consecutive_errors = stop_after_no_new"
1458
1282
  )
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
- )
1283
+ add_migration_column(connection, count_column)
1465
1284
 
1466
1285
 
1467
1286
  def repair_thread_scoped_workspaces_migration(connection: sqlite3.Connection) -> None:
@@ -1490,21 +1309,8 @@ def repair_stable_targets_migration(connection: sqlite3.Connection) -> bool:
1490
1309
 
1491
1310
  migration_sql = next(sql for version, _, sql in MIGRATIONS if version == 16)
1492
1311
  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
- )
1312
+ if statement.startswith(("ALTER TABLE workspaces", "ALTER TABLE scans")):
1313
+ add_migration_column(connection, statement)
1508
1314
  continue
1509
1315
  statement = statement.replace("CREATE TABLE ", "CREATE TABLE IF NOT EXISTS ", 1)
1510
1316
  statement = statement.replace("CREATE INDEX ", "CREATE INDEX IF NOT EXISTS ", 1)
@@ -1522,6 +1328,19 @@ def repair_stable_targets_migration(connection: sqlite3.Connection) -> bool:
1522
1328
  return True
1523
1329
 
1524
1330
 
1331
+ def repair_additive_migration(connection: sqlite3.Connection, version: int) -> None:
1332
+ sql = next(sql for migration_version, _, sql in MIGRATIONS if migration_version == version)
1333
+ for statement in sql_statements(sql):
1334
+ add_migration_column(connection, statement)
1335
+
1336
+
1337
+ def add_migration_column(connection: sqlite3.Connection, statement: str) -> None:
1338
+ _, _, table, _, _, column, definition = statement.removesuffix(";").split(None, 6)
1339
+ # Preserve the compact declarations used by historical repairs, including CHECK errors.
1340
+ definition = " ".join(definition.split()).replace("( ", "(").replace(" )", ")")
1341
+ add_column_if_missing(connection, table, column, definition)
1342
+
1343
+
1525
1344
  def add_column_if_missing(
1526
1345
  connection: sqlite3.Connection, table: str, column: str, definition: str
1527
1346
  ) -> None:
@@ -1543,9 +1362,5 @@ def sql_statements(script: str) -> list[str]:
1543
1362
  return statements
1544
1363
 
1545
1364
 
1546
- def main() -> None:
1547
- argparse.ArgumentParser(description=__doc__).parse_args()
1548
-
1549
-
1550
1365
  if __name__ == "__main__":
1551
- main()
1366
+ argparse.ArgumentParser(description=__doc__).parse_args()
@@ -3,11 +3,14 @@
3
3
  import argparse
4
4
  import json
5
5
  import sqlite3
6
+ import sys
6
7
  from contextlib import closing
7
8
  from pathlib import Path
8
9
  from typing import Any
9
10
  from urllib.parse import quote
10
11
 
12
+ # Some plugin hosts launch Python with safe-path isolation enabled.
13
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
11
14
  from workbench_finding_index import upsert_finding
12
15
 
13
16
  FIELDS = {
@@ -40,7 +40,10 @@ def finding_source_excerpt(
40
40
  source = scanned_source_text(scan, target, path)
41
41
  if not source or "\0" in source:
42
42
  return None
43
- lines = source.splitlines()
43
+ # Match location validation and SARIF hashing: CR, LF, and CRLF are line breaks.
44
+ # Keep form feeds and Unicode separators within their original source lines.
45
+ normalized = source.replace("\r\n", "\n").replace("\r", "\n")
46
+ lines = normalized.removesuffix("\n").split("\n")
44
47
  if start_line < 1 or start_line > len(lines):
45
48
  return None
46
49
  last_affected_line = end_line if isinstance(end_line, int) else start_line
@@ -87,9 +90,5 @@ def safe_source_path(target: Path, relative_path: str) -> Path | None:
87
90
  return path
88
91
 
89
92
 
90
- def main() -> None:
91
- argparse.ArgumentParser(description=__doc__).parse_args()
92
-
93
-
94
93
  if __name__ == "__main__":
95
- main()
94
+ argparse.ArgumentParser(description=__doc__).parse_args()