@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
@@ -1,11 +1,9 @@
1
- """Build deterministic structural or sampled source previews for rank inputs."""
1
+ """Build bounded source previews for rank inputs regardless of language."""
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
5
  import argparse
6
- import ast
7
- import json
8
- import re
6
+ from bisect import bisect_right
9
7
  from pathlib import Path
10
8
 
11
9
  DEFAULT_PREVIEW_BYTES = 1024
@@ -14,82 +12,9 @@ PREVIEW_HEAD_LINES = 12
14
12
  PREVIEW_SAMPLE_LINES = 10
15
13
  _UTF16_BOMS = (b"\xff\xfe", b"\xfe\xff")
16
14
 
17
- TEXT_CODE_EXTENSIONS = {
18
- ".c",
19
- ".cc",
20
- ".cfg",
21
- ".cjs",
22
- ".clj",
23
- ".cpp",
24
- ".cs",
25
- ".css",
26
- ".cts",
27
- ".cue",
28
- ".cxx",
29
- ".dart",
30
- ".ex",
31
- ".exs",
32
- ".go",
33
- ".graphql",
34
- ".h",
35
- ".hpp",
36
- ".hs",
37
- ".html",
38
- ".java",
39
- ".js",
40
- ".json",
41
- ".jsx",
42
- ".kt",
43
- ".kts",
44
- ".lua",
45
- ".m",
46
- ".mjs",
47
- ".mm",
48
- ".mts",
49
- ".php",
50
- ".proto",
51
- ".ps1",
52
- ".psd1",
53
- ".psm1",
54
- ".py",
55
- ".rb",
56
- ".rs",
57
- ".scala",
58
- ".sh",
59
- ".sql",
60
- ".swift",
61
- ".tf",
62
- ".toml",
63
- ".ts",
64
- ".tsx",
65
- ".vue",
66
- ".xml",
67
- ".yaml",
68
- ".yml",
69
- }
70
-
71
- JAVASCRIPT_EXTENSIONS = {".cjs", ".cts", ".js", ".jsx", ".mjs", ".mts", ".ts", ".tsx", ".vue"}
72
- JAVA_LIKE_EXTENSIONS = {".c", ".cc", ".cpp", ".cs", ".cxx", ".h", ".hpp", ".java", ".mm"}
73
- BRACE_LANGUAGE_EXTENSIONS = {
74
- *JAVASCRIPT_EXTENSIONS,
75
- *JAVA_LIKE_EXTENSIONS,
76
- ".dart",
77
- ".go",
78
- ".kt",
79
- ".kts",
80
- ".php",
81
- ".rs",
82
- ".scala",
83
- ".swift",
84
- }
85
- NESTED_BLOCK_COMMENT_EXTENSIONS = {".kt", ".kts", ".rs", ".scala", ".swift"}
86
- RUST_RAW_STRING_RE = re.compile(r'(?:br|r)(#{0,16})"')
87
- RUST_LIFETIME_RE = re.compile(r"'[A-Za-z_][A-Za-z0-9_]*")
88
- PHP_HEREDOC_RE = re.compile(r"<<<\s*['\"]?([A-Za-z_]\w*)['\"]?")
89
-
90
15
 
91
16
  def _decode_source(data: bytes) -> str:
92
- encoding = "utf-16" if data.startswith(_UTF16_BOMS) else "utf-8"
17
+ encoding = "utf-16" if data.startswith(_UTF16_BOMS) else "utf-8-sig"
93
18
  return data.decode(encoding, errors="ignore")
94
19
 
95
20
 
@@ -99,8 +24,21 @@ def is_binary_sample(data: bytes) -> bool:
99
24
  return b"\0" in data
100
25
 
101
26
 
102
- def compact_preview_line(line: str) -> str:
103
- return " ".join(line.split())
27
+ def is_binary_file(path: Path) -> bool:
28
+ """Classify the whole file in bounded chunks."""
29
+ try:
30
+ with path.open("rb") as source:
31
+ sample = source.read(DEFAULT_PREVIEW_READ_BYTES)
32
+ if is_binary_sample(sample):
33
+ return True
34
+ bom = sample[:2] if sample.startswith(_UTF16_BOMS) else b""
35
+ while chunk := source.read(DEFAULT_PREVIEW_READ_BYTES):
36
+ # Even-sized reads preserve UTF-16 code-unit alignment and byte order.
37
+ if is_binary_sample(bom + chunk) if bom else b"\0" in chunk:
38
+ return True
39
+ except OSError:
40
+ return True
41
+ return False
104
42
 
105
43
 
106
44
  def truncate_utf8(text: str, max_bytes: int) -> str:
@@ -113,9 +51,9 @@ def truncate_utf8(text: str, max_bytes: int) -> str:
113
51
 
114
52
 
115
53
  def select_preview_lines(lines: list[str]) -> list[str]:
116
- compact = [value for line in lines if (value := compact_preview_line(line))]
117
- head = compact[:PREVIEW_HEAD_LINES]
118
- remainder = compact[PREVIEW_HEAD_LINES:]
54
+ nonblank = [line for line in lines if line.strip()]
55
+ head = nonblank[:PREVIEW_HEAD_LINES]
56
+ remainder = nonblank[PREVIEW_HEAD_LINES:]
119
57
  if len(remainder) <= PREVIEW_SAMPLE_LINES:
120
58
  return [*head, *remainder]
121
59
 
@@ -137,819 +75,19 @@ def fit_preview_lines(lines: list[str], max_bytes: int) -> str:
137
75
 
138
76
  def render(line_bytes: int) -> str:
139
77
  return "\n".join(
140
- line if line == "..." else truncate_utf8(line, line_bytes).rstrip() for line in lines
78
+ line if line == "..." else truncate_utf8(line, line_bytes) for line in lines
141
79
  )
142
80
 
143
- content_lines = [line for line in lines if line != "..."]
144
- if not content_lines:
145
- return truncate_utf8(full_preview, max_bytes)
146
- low = 0
147
- high = max(len(line.encode("utf-8")) for line in content_lines)
148
- best = ""
149
- while low <= high:
150
- middle = (low + high) // 2
151
- candidate = render(middle)
152
- if len(candidate.encode("utf-8")) <= max_bytes:
153
- best = candidate
154
- low = middle + 1
155
- else:
156
- high = middle - 1
157
- if best:
81
+ high = max(len(line.encode("utf-8")) for line in lines)
82
+ limit = bisect_right(
83
+ range(high + 1), max_bytes, key=lambda size: len(render(size).encode("utf-8"))
84
+ )
85
+ best = render(limit - 1) if limit else ""
86
+ if best.strip() not in {"", "..."}:
158
87
  return best
159
88
  return truncate_utf8(full_preview, max_bytes)
160
89
 
161
90
 
162
- def python_decorators(node: ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef) -> str:
163
- values: list[str] = []
164
- for decorator in node.decorator_list:
165
- try:
166
- rendered = compact_preview_line(ast.unparse(decorator))
167
- except (AttributeError, RecursionError, ValueError):
168
- continue
169
- if rendered:
170
- values.append(f"@{rendered}")
171
- return " ".join(values)
172
-
173
-
174
- def python_arguments(node: ast.FunctionDef | ast.AsyncFunctionDef) -> str:
175
- arguments = [argument.arg for argument in (*node.args.posonlyargs, *node.args.args)]
176
- if node.args.vararg is not None:
177
- arguments.append(f"*{node.args.vararg.arg}")
178
- arguments.extend(argument.arg for argument in node.args.kwonlyargs)
179
- if node.args.kwarg is not None:
180
- arguments.append(f"**{node.args.kwarg.arg}")
181
- return ", ".join(arguments)
182
-
183
-
184
- def python_outline(text: str) -> list[str]:
185
- try:
186
- tree = ast.parse(text)
187
- except (SyntaxError, ValueError, TypeError, MemoryError):
188
- return []
189
-
190
- outline: list[str] = []
191
- for node in tree.body:
192
- if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
193
- prefix = python_decorators(node)
194
- kind = "async function" if isinstance(node, ast.AsyncFunctionDef) else "function"
195
- declaration = f"{kind} {node.name}({python_arguments(node)})"
196
- outline.append(f"{prefix} {declaration}".strip())
197
- elif isinstance(node, ast.ClassDef):
198
- prefix = python_decorators(node)
199
- bases: list[str] = []
200
- for base in node.bases:
201
- try:
202
- bases.append(compact_preview_line(ast.unparse(base)))
203
- except (AttributeError, RecursionError, ValueError):
204
- continue
205
- suffix = f"({', '.join(bases)})" if bases else ""
206
- outline.append(f"{prefix} class {node.name}{suffix}".strip())
207
- for member in node.body:
208
- if not isinstance(member, (ast.FunctionDef, ast.AsyncFunctionDef)):
209
- continue
210
- member_prefix = python_decorators(member)
211
- kind = "async method" if isinstance(member, ast.AsyncFunctionDef) else "method"
212
- declaration = f"{kind} {node.name}.{member.name}"
213
- outline.append(f"{member_prefix} {declaration}".strip())
214
- return outline
215
-
216
-
217
- def javascript_regex_end(text: str, start: int) -> int | None:
218
- if start + 1 >= len(text) or text[start + 1] in {"/", "*"}:
219
- return None
220
- previous = start - 1
221
- while previous >= 0 and text[previous] in " \t\r":
222
- previous -= 1
223
- if previous >= 0 and text[previous] not in "=(:,[!&|?{};\n":
224
- prefix = text[max(0, previous - 8) : previous + 1]
225
- if not re.search(r"\b(?:case|return|throw)$", prefix):
226
- return None
227
-
228
- index = start + 1
229
- in_character_class = False
230
- while index < len(text):
231
- char = text[index]
232
- if char == "\n":
233
- return None
234
- if char == "\\" and index + 1 < len(text):
235
- index += 2
236
- continue
237
- if char == "[":
238
- in_character_class = True
239
- elif char == "]":
240
- in_character_class = False
241
- elif char == "/" and not in_character_class:
242
- index += 1
243
- while index < len(text) and text[index].isalpha():
244
- index += 1
245
- return index
246
- index += 1
247
- return None
248
-
249
-
250
- def mask_c_style_source(text: str, suffix: str) -> str:
251
- masked: list[str] = []
252
- index = 0
253
- block_comment_depth = 0
254
- in_line_comment = False
255
- quote = ""
256
- raw_terminator = ""
257
- heredoc_terminator = ""
258
- while index < len(text):
259
- char = text[index]
260
- next_char = text[index + 1] if index + 1 < len(text) else ""
261
- if char == "\n":
262
- masked.append(char)
263
- in_line_comment = False
264
- if quote and quote not in {"`", '"""', "'''", '@"'}:
265
- quote = ""
266
- index += 1
267
- continue
268
- if in_line_comment:
269
- masked.append(" ")
270
- index += 1
271
- continue
272
- if block_comment_depth:
273
- if char == "/" and next_char == "*" and suffix in NESTED_BLOCK_COMMENT_EXTENSIONS:
274
- masked.extend((" ", " "))
275
- block_comment_depth += 1
276
- index += 2
277
- elif char == "*" and next_char == "/":
278
- masked.extend((" ", " "))
279
- block_comment_depth -= 1
280
- index += 2
281
- else:
282
- masked.append(" ")
283
- index += 1
284
- continue
285
- if heredoc_terminator:
286
- if index == 0 or text[index - 1] == "\n":
287
- line_end = text.find("\n", index)
288
- if line_end < 0:
289
- line_end = len(text)
290
- candidate = text[index:line_end].strip().removesuffix(";")
291
- if candidate == heredoc_terminator:
292
- masked.extend(" " * (line_end - index))
293
- index = line_end
294
- heredoc_terminator = ""
295
- continue
296
- masked.append(" ")
297
- index += 1
298
- continue
299
- if raw_terminator:
300
- if text.startswith(raw_terminator, index):
301
- masked.extend(" " * len(raw_terminator))
302
- index += len(raw_terminator)
303
- raw_terminator = ""
304
- else:
305
- masked.append(" ")
306
- index += 1
307
- continue
308
- if quote in {'"""', "'''"}:
309
- if text.startswith(quote, index):
310
- masked.extend(" " * len(quote))
311
- index += len(quote)
312
- quote = ""
313
- else:
314
- masked.append(" ")
315
- index += 1
316
- continue
317
- if quote == '@"':
318
- masked.append(" ")
319
- if char == '"' and next_char == '"':
320
- masked.append(" ")
321
- index += 2
322
- else:
323
- if char == '"':
324
- quote = ""
325
- index += 1
326
- continue
327
- if quote:
328
- if char == "\\" and next_char:
329
- masked.append(" ")
330
- if next_char == "\n":
331
- index += 1
332
- else:
333
- masked.append(" ")
334
- index += 2
335
- else:
336
- masked.append(" ")
337
- if char == quote:
338
- quote = ""
339
- index += 1
340
- continue
341
- if suffix == ".rs":
342
- raw_match = RUST_RAW_STRING_RE.match(text, index)
343
- if raw_match:
344
- token = raw_match.group(0)
345
- masked.extend(" " * len(token))
346
- raw_terminator = f'"{raw_match.group(1)}'
347
- index += len(token)
348
- continue
349
- if char == "'":
350
- lifetime_match = RUST_LIFETIME_RE.match(text, index)
351
- if lifetime_match:
352
- token = lifetime_match.group(0)
353
- following = text[index + len(token) : index + len(token) + 1]
354
- if following != "'":
355
- masked.extend(token)
356
- index += len(token)
357
- continue
358
- if suffix == ".cs" and char == "@" and next_char == '"':
359
- masked.extend((" ", " "))
360
- quote = '@"'
361
- index += 2
362
- continue
363
- triple_quote = text[index : index + 3]
364
- if triple_quote in {'"""', "'''"}:
365
- masked.extend((" ", " ", " "))
366
- quote = triple_quote
367
- index += 3
368
- continue
369
- if suffix == ".php" and text.startswith("<<<", index):
370
- heredoc_match = PHP_HEREDOC_RE.match(text, index)
371
- if heredoc_match:
372
- token = heredoc_match.group(0)
373
- masked.extend(" " * len(token))
374
- heredoc_terminator = heredoc_match.group(1)
375
- index += len(token)
376
- continue
377
- if suffix in JAVASCRIPT_EXTENSIONS and char == "/":
378
- regex_end = javascript_regex_end(text, index)
379
- if regex_end is not None:
380
- masked.extend(" " * (regex_end - index))
381
- index = regex_end
382
- continue
383
- if char == "/" and next_char == "/":
384
- masked.extend((" ", " "))
385
- in_line_comment = True
386
- index += 2
387
- continue
388
- if char == "/" and next_char == "*":
389
- masked.extend((" ", " "))
390
- block_comment_depth = 1
391
- index += 2
392
- continue
393
- if char == "#" and suffix == ".php" and next_char != "[":
394
- masked.append(" ")
395
- in_line_comment = True
396
- index += 1
397
- continue
398
- if char in {'"', "'", "`"}:
399
- quote = char
400
- masked.append(" ")
401
- index += 1
402
- continue
403
- masked.append(char)
404
- index += 1
405
- return "".join(masked)
406
-
407
-
408
- def match_type_declaration(line: str, suffix: str) -> tuple[str, str] | None:
409
- match: re.Match[str] | None = None
410
- if suffix in JAVASCRIPT_EXTENSIONS:
411
- match = re.match(
412
- r"^(?:(?:export|default|declare|abstract)\s+)*(class|interface|enum|namespace)\s+"
413
- r"([A-Za-z_$][\w$]*)",
414
- line,
415
- )
416
- elif suffix == ".java":
417
- match = re.search(r"\b(class|interface|enum|record|@interface)\s+([A-Za-z_$][\w$]*)", line)
418
- elif suffix == ".cs":
419
- match = re.search(
420
- r"\b(class|interface|struct|enum|record(?:\s+(?:class|struct))?)\s+"
421
- r"(@?[A-Za-z_]\w*)",
422
- line,
423
- )
424
- elif suffix == ".php":
425
- match = re.match(
426
- r"^(?:(?:abstract|final|readonly)\s+)*(class|interface|trait|enum)\s+"
427
- r"([A-Za-z_]\w*)",
428
- line,
429
- )
430
- elif suffix == ".go":
431
- match = re.match(r"^type\s+([A-Za-z_]\w*)\s+(struct|interface)\b", line)
432
- if match:
433
- return match.group(2), match.group(1)
434
- elif suffix in {".kt", ".kts"}:
435
- companion_match = re.search(r"\bcompanion\s+object(?:\s+([A-Za-z_]\w*))?", line)
436
- if companion_match:
437
- return "object", companion_match.group(1) or "Companion"
438
- match = re.search(
439
- r"\b((?:(?:data|enum|sealed|annotation|value)\s+)?(?:class|interface|object))\s+"
440
- r"([A-Za-z_]\w*)",
441
- line,
442
- )
443
- elif suffix == ".scala":
444
- match = re.match(
445
- r"^(?:(?:case|sealed|abstract)\s+)*(class|trait|object|enum)\s+([A-Za-z_]\w*)", line
446
- )
447
- elif suffix == ".rs":
448
- match = re.search(r"\b(struct|enum|trait|union)\s+([A-Za-z_]\w*)", line)
449
- if not match:
450
- impl_match = re.search(
451
- r"\bimpl(?:\s*<[^>{}]*>)?\s+(?:[\w:<>]+\s+for\s+)?([A-Za-z_]\w*)",
452
- line,
453
- )
454
- if impl_match:
455
- return "impl", impl_match.group(1)
456
- elif suffix == ".swift":
457
- match = re.search(r"\b(class|struct|enum|protocol|actor|extension)\s+([A-Za-z_]\w*)", line)
458
- elif suffix == ".dart":
459
- match = re.search(r"\b(class|mixin|enum|extension)\s+([A-Za-z_]\w*)", line)
460
- elif suffix in {".c", ".cc", ".cpp", ".cxx", ".h", ".hpp", ".mm"}:
461
- match = re.search(
462
- r"\b(class|struct|union|enum(?:\s+class)?)\s+([A-Za-z_]\w*)",
463
- line,
464
- )
465
- if not match:
466
- return None
467
- return match.group(1), match.group(2)
468
-
469
-
470
- CONTROL_NAMES = {
471
- "assert",
472
- "catch",
473
- "for",
474
- "foreach",
475
- "if",
476
- "lock",
477
- "new",
478
- "return",
479
- "sizeof",
480
- "static_assert",
481
- "switch",
482
- "synchronized",
483
- "throw",
484
- "typeof",
485
- "while",
486
- }
487
-
488
-
489
- def match_java_like_function(
490
- line: str, suffix: str, type_name: str | None
491
- ) -> tuple[str, str] | None:
492
- paren = line.find("(")
493
- if paren < 0:
494
- return None
495
- before = line[:paren].rstrip()
496
- if not before or "=" in before or before.startswith("#"):
497
- return None
498
- before = re.sub(r"<[^<>]*>$", "", before).rstrip()
499
- name_match = re.search(
500
- r"(~?[A-Za-z_$@][\w$@]*(?:(?:::|\.)~?[A-Za-z_$@][\w$@]*)?)$",
501
- before,
502
- )
503
- if not name_match:
504
- return None
505
- qualified_name = name_match.group(1)
506
- name = re.split(r"::|\.", qualified_name)[-1].lstrip("~")
507
- if name in CONTROL_NAMES:
508
- return None
509
- prefix = before[: name_match.start()].strip()
510
- if not prefix and name != (type_name or ""):
511
- return None
512
- if suffix in {".c", ".cc", ".cpp", ".cxx", ".h", ".hpp", ".mm"} and prefix.startswith(
513
- ("typedef", "using")
514
- ):
515
- return None
516
- if (
517
- suffix in {".c", ".cc", ".cpp", ".cxx", ".h", ".hpp", ".mm"}
518
- and type_name is None
519
- and line.endswith(";")
520
- and not re.search(
521
- r"(?:\b(?:auto|bool|char|consteval|constexpr|double|extern|float|inline|int|long|"
522
- r"short|signed|static|unsigned|void)\b|[*&:]|<)",
523
- prefix,
524
- )
525
- ):
526
- return None
527
- return ("method" if type_name else "function"), qualified_name
528
-
529
-
530
- def match_function_declaration(
531
- line: str, suffix: str, type_name: str | None
532
- ) -> tuple[str, str] | None:
533
- if suffix in JAVASCRIPT_EXTENSIONS:
534
- function_match = re.match(
535
- r"^(?:(?:export|default|declare)\s+)*(async\s+)?function\s*\*?\s*"
536
- r"([A-Za-z_$][\w$]*)\s*(?:<[^>{}]*>)?\s*\(",
537
- line,
538
- )
539
- if function_match:
540
- kind = "async function" if function_match.group(1) else "function"
541
- return kind, function_match.group(2)
542
- arrow_match = re.match(
543
- r"^(?:(?:export|declare)\s+)*(?:const|let|var)\s+([A-Za-z_$][\w$]*)"
544
- r"\s*(?::[^=]+)?=\s*(?:async\s+)?(?:function\b|\([^)]*\)\s*(?::[^=]+)?=>|"
545
- r"[A-Za-z_$][\w$]*\s*=>)",
546
- line,
547
- )
548
- if arrow_match:
549
- return "function", arrow_match.group(1)
550
- if type_name:
551
- field_arrow_match = re.match(
552
- r"^(?:(?:public|private|protected|static|abstract|override|readonly|declare|"
553
- r"accessor)\s+)*(#?[A-Za-z_$][\w$]*)(?:[?!])?\s*(?::[^=]+)?=\s*"
554
- r"(?:async\s+)?(?:\([^)]*\)\s*(?::[^=]+)?=>|[A-Za-z_$][\w$]*\s*=>)",
555
- line,
556
- )
557
- if field_arrow_match:
558
- return "method", field_arrow_match.group(1)
559
- method_match = re.match(
560
- r"^(?:(?:public|private|protected|static|abstract|async|override|readonly|"
561
- r"declare|get|set)\s+)*(#?[A-Za-z_$][\w$]*|constructor)\s*"
562
- r"(?:<[^>{}]*>)?\s*\(",
563
- line,
564
- )
565
- if method_match and method_match.group(1) not in CONTROL_NAMES:
566
- return "method", method_match.group(1)
567
- return None
568
- if suffix == ".php":
569
- match = re.match(
570
- r"^(?:(?:public|protected|private|static|final|abstract|readonly)\s+)*"
571
- r"function\s*&?\s*([A-Za-z_]\w*)\s*\(",
572
- line,
573
- )
574
- if match:
575
- return ("method" if type_name else "function"), match.group(1)
576
- return None
577
- if suffix == ".go":
578
- match = re.match(
579
- r"^func\s+(?:\(([^)]*)\)\s*)?([A-Za-z_]\w*)\s*(?:\[[^]]+\]\s*)?\(",
580
- line,
581
- )
582
- if match:
583
- receiver = match.group(1)
584
- name = match.group(2)
585
- if receiver:
586
- receiver_match = re.search(r"\*?([A-Za-z_]\w*)(?:\[[^]]*])?\s*$", receiver)
587
- if receiver_match:
588
- return "method", f"{receiver_match.group(1)}.{name}"
589
- return "method", name
590
- return ("method" if type_name else "function"), name
591
- return None
592
- if suffix in {".kt", ".kts"}:
593
- match = re.search(r"\bfun\s+(?:<[^>{}]*>\s*)?(?:[\w.<>?]+\.)?([A-Za-z_]\w*)\s*\(", line)
594
- if match:
595
- return ("method" if type_name else "function"), match.group(1)
596
- return None
597
- if suffix == ".scala":
598
- match = re.search(r"\bdef\s+([A-Za-z_]\w*)\b", line)
599
- if match:
600
- return ("method" if type_name else "function"), match.group(1)
601
- return None
602
- if suffix == ".rs":
603
- match = re.search(
604
- r"\b(?:async\s+)?(?:unsafe\s+)?(?:extern\s+\"[^\"]+\"\s+)?(?:const\s+)?"
605
- r"fn\s+([A-Za-z_]\w*)",
606
- line,
607
- )
608
- if match:
609
- return ("method" if type_name else "function"), match.group(1)
610
- return None
611
- if suffix == ".swift":
612
- match = re.search(r"\bfunc\s+([A-Za-z_]\w*)\s*(?:<[^>{}]*>)?\s*\(", line)
613
- if match:
614
- return ("method" if type_name else "function"), match.group(1)
615
- if type_name and re.search(r"\binit\s*\(", line):
616
- return "method", "init"
617
- return None
618
- if suffix == ".dart":
619
- return match_java_like_function(line, suffix, type_name)
620
- if suffix in JAVA_LIKE_EXTENSIONS:
621
- return match_java_like_function(line, suffix, type_name)
622
- return None
623
-
624
-
625
- def is_annotation_line(line: str) -> bool:
626
- return bool(
627
- re.match(r"^@[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*(?:\(.*\))?$", line)
628
- or re.match(r"^\[[A-Za-z_][^]]*]$", line)
629
- or re.match(r"^#\[[A-Za-z_][^]]*]$", line)
630
- )
631
-
632
-
633
- LEADING_ANNOTATION_RE = re.compile(
634
- r"^(?:@[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*(?:\([^)]*\))?|"
635
- r"\[[A-Za-z_][^]]*]|#\[[A-Za-z_][^]]*])\s*"
636
- )
637
-
638
-
639
- def strip_leading_annotations(original: str, masked: str) -> tuple[str, str, list[str]]:
640
- annotations: list[str] = []
641
- if masked.startswith("@interface "):
642
- return original, masked, annotations
643
- while masked_match := LEADING_ANNOTATION_RE.match(masked):
644
- original_match = LEADING_ANNOTATION_RE.match(original)
645
- if original_match:
646
- annotations.append(compact_preview_line(original_match.group(0)))
647
- original = original[original_match.end() :].lstrip()
648
- masked = masked[masked_match.end() :].lstrip()
649
- return original, masked, annotations
650
-
651
-
652
- def brace_language_outline(text: str, suffix: str) -> list[str]:
653
- original_lines = text.splitlines()
654
- masked_lines = mask_c_style_source(text, suffix).splitlines()
655
- outline: list[str] = []
656
- seen: set[str] = set()
657
- type_stack: list[tuple[str, int]] = []
658
- function_depths: list[int] = []
659
- blocked_depths: list[int] = []
660
- pending_type: str | None = None
661
- pending_function = False
662
- pending_annotations: list[str] = []
663
- depth = 0
664
-
665
- def add(value: str) -> None:
666
- decorated = f"{' '.join(pending_annotations[-2:])} {value}".strip()
667
- if decorated not in seen:
668
- seen.add(decorated)
669
- outline.append(decorated)
670
-
671
- def next_lines_open_body(line_index: int) -> bool:
672
- inspected = 0
673
- for future_index in range(line_index + 1, len(masked_lines)):
674
- future = masked_lines[future_index]
675
- future_line = compact_preview_line(future)
676
- if not future_line:
677
- continue
678
- inspected += 1
679
- if "{" in future_line:
680
- return True
681
- if "}" in future_line or future_line.endswith(";") or inspected >= 6:
682
- return False
683
- return False
684
-
685
- for line_index, (original, masked) in enumerate(
686
- zip(original_lines, masked_lines, strict=False)
687
- ):
688
- while function_depths and depth < function_depths[-1]:
689
- function_depths.pop()
690
- while type_stack and depth < type_stack[-1][1]:
691
- type_stack.pop()
692
- while blocked_depths and depth < blocked_depths[-1]:
693
- blocked_depths.pop()
694
-
695
- masked_line = compact_preview_line(masked)
696
- original_line = compact_preview_line(original)
697
- opens = masked.count("{")
698
- closes = masked.count("}")
699
-
700
- opened_pending_scope = False
701
- if pending_type and "{" in masked:
702
- type_stack.append((pending_type, depth + 1))
703
- pending_type = None
704
- opened_pending_scope = True
705
- if pending_function and "{" in masked:
706
- function_depths.append(depth + 1)
707
- pending_function = False
708
- opened_pending_scope = True
709
-
710
- if (
711
- masked_line
712
- and original_line
713
- and is_annotation_line(original_line)
714
- and not function_depths
715
- and not blocked_depths
716
- ):
717
- pending_annotations.append(original_line)
718
- elif masked_line and not function_depths and not blocked_depths:
719
- _, masked_line, inline_annotations = strip_leading_annotations(
720
- original_line, masked_line
721
- )
722
- pending_annotations.extend(inline_annotations)
723
- current_type = type_stack[-1][0] if type_stack else None
724
- direct_type_body = not type_stack or depth == type_stack[-1][1]
725
- type_match = match_type_declaration(masked_line, suffix) if direct_type_body else None
726
- if type_match:
727
- kind, name = type_match
728
- if (
729
- current_type
730
- and suffix in {".kt", ".kts"}
731
- and kind == "object"
732
- and name == "Companion"
733
- ):
734
- name = f"{current_type}.{name}"
735
- add(f"{kind} {name}")
736
- pending_annotations.clear()
737
- if "{" in masked:
738
- type_stack.append((name, depth + 1))
739
- elif not masked_line.endswith(";") and next_lines_open_body(line_index):
740
- pending_type = name
741
- elif direct_type_body:
742
- function_match = match_function_declaration(masked_line, suffix, current_type)
743
- if function_match:
744
- kind, name = function_match
745
- qualified_name = (
746
- f"{current_type}.{name}"
747
- if current_type and "." not in name and "::" not in name
748
- else name
749
- )
750
- add(f"{kind} {qualified_name}")
751
- pending_annotations.clear()
752
- if "{" in masked:
753
- function_depths.append(depth + 1)
754
- elif (
755
- not masked_line.endswith(";")
756
- and "=>" not in masked_line
757
- and "=" not in masked_line
758
- and next_lines_open_body(line_index)
759
- ):
760
- pending_function = True
761
- else:
762
- pending_annotations.clear()
763
- is_transparent_container = bool(
764
- re.match(
765
- r"^(?:(?:export|inline)\s+)?namespace\b|"
766
- r"^(?:pub\s+)?mod\b|^(?:declare\s+)?module\b|"
767
- r"^(?:unsafe\s+)?extern\b",
768
- masked_line,
769
- )
770
- )
771
- if opens and not opened_pending_scope and not is_transparent_container:
772
- blocked_depths.append(depth + 1)
773
-
774
- depth = max(0, depth + opens - closes)
775
- while function_depths and depth < function_depths[-1]:
776
- function_depths.pop()
777
- while type_stack and depth < type_stack[-1][1]:
778
- type_stack.pop()
779
- while blocked_depths and depth < blocked_depths[-1]:
780
- blocked_depths.pop()
781
- return outline
782
-
783
-
784
- def ruby_outline(text: str) -> list[str]:
785
- outline: list[str] = []
786
- seen: set[str] = set()
787
- type_stack: list[tuple[int, str]] = []
788
- function_indents: list[int] = []
789
-
790
- def add(value: str) -> None:
791
- if value not in seen:
792
- seen.add(value)
793
- outline.append(value)
794
-
795
- for raw_line in text.splitlines():
796
- stripped = raw_line.strip()
797
- if not stripped or stripped.startswith("#"):
798
- continue
799
- indent = len(raw_line.expandtabs(4)) - len(raw_line.lstrip().expandtabs(4))
800
- while function_indents and indent <= function_indents[-1]:
801
- function_indents.pop()
802
- while type_stack and indent <= type_stack[-1][0]:
803
- type_stack.pop()
804
-
805
- type_match = re.match(r"^(class|module)\s+([A-Z]\w*(?:::[A-Z]\w*)*)", stripped)
806
- if type_match and not function_indents:
807
- kind, name = type_match.groups()
808
- add(f"{kind} {name}")
809
- type_stack.append((indent, name))
810
- continue
811
-
812
- function_match = re.match(r"^def\s+(?:self\.)?([^\s(]+)", stripped)
813
- if function_match and not function_indents:
814
- name = function_match.group(1)
815
- if type_stack:
816
- add(f"method {type_stack[-1][1]}.{name}")
817
- else:
818
- add(f"function {name}")
819
- function_indents.append(indent)
820
- return outline
821
-
822
-
823
- def simple_language_outline(text: str, suffix: str) -> list[str]:
824
- outline: list[str] = []
825
- seen: set[str] = set()
826
-
827
- def add(value: str) -> None:
828
- if value not in seen:
829
- seen.add(value)
830
- outline.append(value)
831
-
832
- for raw_line in text.splitlines():
833
- line = compact_preview_line(raw_line)
834
- if not line:
835
- continue
836
- match: re.Match[str] | None = None
837
- if suffix == ".py":
838
- match = re.match(r"^(async\s+)?def\s+([A-Za-z_]\w*)\s*\(", line)
839
- if match:
840
- add(f"{'async function' if match.group(1) else 'function'} {match.group(2)}")
841
- continue
842
- match = re.match(r"^class\s+([A-Za-z_]\w*)", line)
843
- if match:
844
- add(f"class {match.group(1)}")
845
- elif suffix == ".rb":
846
- match = re.match(r"^(class|module)\s+([A-Z]\w*(?:::[A-Z]\w*)*)", line)
847
- if match:
848
- add(f"{match.group(1)} {match.group(2)}")
849
- continue
850
- match = re.match(r"^def\s+(?:self\.)?([^\s(]+)", line)
851
- if match:
852
- add(f"function {match.group(1)}")
853
- elif suffix in {".ex", ".exs"}:
854
- match = re.match(r"^defmodule\s+([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*)", line)
855
- if match:
856
- add(f"module {match.group(1)}")
857
- continue
858
- match = re.match(r"^(?:def|defp|defmacro)\s+([A-Za-z_]\w*[!?]?)", line)
859
- if match:
860
- add(f"function {match.group(1)}")
861
- elif suffix == ".clj":
862
- match = re.match(
863
- r"^\((defn|defmacro|defprotocol|defrecord|deftype|defmulti|defmethod)\s+([^\s)]+)",
864
- line,
865
- )
866
- if match:
867
- add(f"{match.group(1)} {match.group(2)}")
868
- elif suffix == ".lua":
869
- match = re.match(r"^(?:local\s+)?function\s+([A-Za-z_]\w*(?:[.:][A-Za-z_]\w*)*)", line)
870
- if match:
871
- add(f"function {match.group(1)}")
872
- elif suffix == ".sh":
873
- match = re.match(r"^(?:function\s+)?([A-Za-z_]\w*)\s*\(\s*\)\s*\{?", line)
874
- if match:
875
- add(f"function {match.group(1)}")
876
- elif suffix == ".hs":
877
- match = re.match(r"^(data|newtype|type|class)\s+([A-Z]\w*)", line)
878
- if match:
879
- add(f"{match.group(1)} {match.group(2)}")
880
- continue
881
- match = re.match(r"^([a-z_]\w*)\s*::", line)
882
- if match:
883
- add(f"function {match.group(1)}")
884
- elif suffix == ".proto":
885
- match = re.match(r"^(message|service|enum)\s+([A-Za-z_]\w*)", line)
886
- if match:
887
- add(f"{match.group(1)} {match.group(2)}")
888
- continue
889
- match = re.match(r"^rpc\s+([A-Za-z_]\w*)\s*\(", line)
890
- if match:
891
- add(f"rpc {match.group(1)}")
892
- elif suffix == ".graphql":
893
- match = re.match(
894
- r"^(type|interface|input|enum|union|scalar|directive)\s+([A-Za-z_]\w*)",
895
- line,
896
- )
897
- if match:
898
- add(f"{match.group(1)} {match.group(2)}")
899
- continue
900
- match = re.match(r"^(query|mutation|subscription)\s+([A-Za-z_]\w*)", line)
901
- if match:
902
- add(f"{match.group(1)} {match.group(2)}")
903
- elif suffix == ".sql":
904
- match = re.match(
905
- r"^CREATE\s+(?:OR\s+REPLACE\s+)?(FUNCTION|PROCEDURE|TABLE|VIEW|TRIGGER)\s+"
906
- r"([^\s(;]+)",
907
- line,
908
- flags=re.IGNORECASE,
909
- )
910
- if match:
911
- add(f"{match.group(1).lower()} {match.group(2)}")
912
- elif suffix in {".yaml", ".yml"}:
913
- match = re.match(r"^([A-Za-z0-9_.-]+):(?:\s|$)", raw_line)
914
- if match:
915
- add(f"key {match.group(1)}")
916
- elif suffix in {".toml", ".cfg"}:
917
- match = re.match(r"^\[([^]]+)]$", line)
918
- if match:
919
- add(f"section {match.group(1)}")
920
- return outline
921
-
922
-
923
- def json_outline(text: str) -> list[str]:
924
- try:
925
- parsed = json.loads(text)
926
- except (json.JSONDecodeError, RecursionError, MemoryError):
927
- return []
928
- if not isinstance(parsed, dict):
929
- return []
930
- outline: list[str] = []
931
- for key, value in parsed.items():
932
- if isinstance(value, dict) and value:
933
- children = ", ".join(str(child) for child in value)
934
- outline.append(f"key {key} [{children}]")
935
- else:
936
- outline.append(f"key {key}")
937
- return outline
938
-
939
-
940
- def structural_outline(path: Path, text: str) -> list[str]:
941
- suffix = path.suffix.lower()
942
- if suffix == ".py":
943
- return python_outline(text) or simple_language_outline(text, suffix)
944
- if suffix == ".rb":
945
- return ruby_outline(text)
946
- if suffix in BRACE_LANGUAGE_EXTENSIONS:
947
- return brace_language_outline(text, suffix)
948
- if suffix == ".json":
949
- return json_outline(text)
950
- return simple_language_outline(text, suffix)
951
-
952
-
953
91
  def preview_for(
954
92
  path: Path, preview_bytes: int, *, max_read_bytes: int = DEFAULT_PREVIEW_READ_BYTES
955
93
  ) -> tuple[str, bool]:
@@ -965,24 +103,27 @@ def preview_for(
965
103
  data = sample + remaining
966
104
  except OSError:
967
105
  return "", True
968
- return preview_for_bytes(path, data, preview_bytes)
106
+ return preview_for_bytes(data, preview_bytes)
969
107
 
970
108
 
971
- def preview_for_bytes(path: Path, data: bytes, preview_bytes: int) -> tuple[str, bool]:
109
+ def preview_for_bytes(data: bytes, preview_bytes: int) -> tuple[str, bool]:
972
110
  if is_binary_sample(data):
973
111
  return "", True
974
- text = _decode_source(data)
975
- outline = structural_outline(path, text)
976
- preview_lines = select_preview_lines(outline or text.splitlines())
977
- return fit_preview_lines(preview_lines, preview_bytes), False
112
+ lines = _decode_source(data).replace("\r\n", "\n").replace("\r", "\n").split("\n")
113
+ start, end = 0, len(lines)
114
+ while start < end and not lines[start].strip():
115
+ start += 1
116
+ while end > start and not lines[end - 1].strip():
117
+ end -= 1
118
+ lines = lines[start:end]
119
+ text = "\n".join(lines)
120
+ if len(text.encode("utf-8")) <= preview_bytes:
121
+ return text, False
122
+ return fit_preview_lines(select_preview_lines(lines), preview_bytes), False
978
123
 
979
124
 
980
- def main() -> None:
125
+ if __name__ == "__main__":
981
126
  parser = argparse.ArgumentParser(
982
- description="Build structural or sampled previews for Codex Security rank inputs."
127
+ description="Build bounded source previews for Codex Security rank inputs."
983
128
  )
984
129
  parser.parse_args()
985
-
986
-
987
- if __name__ == "__main__":
988
- main()