@openai/codex-security 0.1.25 → 0.1.26

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 (81) hide show
  1. package/README.md +222 -8
  2. package/_bundled_plugin/.app.json +0 -5
  3. package/_bundled_plugin/.codex-plugin/plugin.json +1 -1
  4. package/_bundled_plugin/.mcp.json +1 -0
  5. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  6. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  7. package/_bundled_plugin/references/config-preflight.md +2 -2
  8. package/_bundled_plugin/references/final-report.md +5 -3
  9. package/_bundled_plugin/references/scan-artifacts.md +5 -1
  10. package/_bundled_plugin/references/scan-contract.md +7 -3
  11. package/_bundled_plugin/schemas/tools/deep-reducer.schema.json +27 -1
  12. package/_bundled_plugin/scripts/deep_scan_workbench.py +6 -1
  13. package/_bundled_plugin/scripts/finalize_scan_contract.py +2 -5
  14. package/_bundled_plugin/scripts/report_projection.py +1 -1
  15. package/_bundled_plugin/scripts/workbench_cli.py +3 -0
  16. package/_bundled_plugin/scripts/workbench_db.py +11 -2
  17. package/_bundled_plugin/scripts/workbench_saved_results.py +41 -22
  18. package/_bundled_plugin/scripts/workbench_scan_usage.py +7 -6
  19. package/_bundled_plugin/scripts/workbench_schema.py +31 -0
  20. package/_bundled_plugin/scripts/workbench_severity.py +119 -0
  21. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +5 -3
  22. package/_bundled_plugin/skills/verify-fix/SKILL.md +5 -1
  23. package/dist/api.d.ts.map +1 -1
  24. package/dist/api.js +4 -5
  25. package/dist/api.js.map +1 -1
  26. package/dist/classify-scan-severity.d.ts +16 -0
  27. package/dist/classify-scan-severity.d.ts.map +1 -0
  28. package/dist/classify-scan-severity.js +96 -0
  29. package/dist/classify-scan-severity.js.map +1 -0
  30. package/dist/classify-severity.d.ts +38 -0
  31. package/dist/classify-severity.d.ts.map +1 -0
  32. package/dist/classify-severity.js +185 -0
  33. package/dist/classify-severity.js.map +1 -0
  34. package/dist/cli.d.ts +5 -2
  35. package/dist/cli.d.ts.map +1 -1
  36. package/dist/cli.js +323 -89
  37. package/dist/cli.js.map +1 -1
  38. package/dist/cost.js +20 -6
  39. package/dist/cost.js.map +1 -1
  40. package/dist/deduplication/deduplication-reviewer.js +1 -1
  41. package/dist/deduplication/deduplication-reviewer.js.map +1 -1
  42. package/dist/finding-catalogue.d.ts +11 -0
  43. package/dist/finding-catalogue.d.ts.map +1 -0
  44. package/dist/finding-catalogue.js +150 -0
  45. package/dist/finding-catalogue.js.map +1 -0
  46. package/dist/index.d.ts +6 -0
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +3 -0
  49. package/dist/index.js.map +1 -1
  50. package/dist/patch-tui.js +1 -1
  51. package/dist/patch-tui.js.map +1 -1
  52. package/dist/publication-store.d.ts.map +1 -1
  53. package/dist/publication-store.js +9 -4
  54. package/dist/publication-store.js.map +1 -1
  55. package/dist/publication.d.ts +9 -0
  56. package/dist/publication.d.ts.map +1 -1
  57. package/dist/publication.js +45 -5
  58. package/dist/publication.js.map +1 -1
  59. package/dist/publish.d.ts +4 -2
  60. package/dist/publish.d.ts.map +1 -1
  61. package/dist/publish.js +2 -2
  62. package/dist/publish.js.map +1 -1
  63. package/dist/scan-comparison.d.ts +42 -37
  64. package/dist/scan-comparison.d.ts.map +1 -1
  65. package/dist/scan-comparison.js +535 -161
  66. package/dist/scan-comparison.js.map +1 -1
  67. package/dist/scan-history-renderer.d.ts.map +1 -1
  68. package/dist/scan-history-renderer.js +5 -0
  69. package/dist/scan-history-renderer.js.map +1 -1
  70. package/dist/severity-store.d.ts +2 -0
  71. package/dist/severity-store.d.ts.map +1 -0
  72. package/dist/severity-store.js +111 -0
  73. package/dist/severity-store.js.map +1 -0
  74. package/dist/thread-source.d.ts +1 -0
  75. package/dist/thread-source.d.ts.map +1 -1
  76. package/dist/thread-source.js +1 -0
  77. package/dist/thread-source.js.map +1 -1
  78. package/dist/version.d.ts +1 -1
  79. package/dist/version.js +1 -1
  80. package/dist/version.js.map +1 -1
  81. package/package.json +4 -4
package/dist/cli.js CHANGED
@@ -17,6 +17,7 @@ import { classifyConnectionFailure, createSecurityInternal, environmentValue, fo
17
17
  import { accountStatus } from "./auth.js";
18
18
  import { publishScanToCustom } from "./custom-publish.js";
19
19
  import { deduplicateScanInternal } from "./deduplication/scan.js";
20
+ import { classifyScanSeverityInternal, classifyScanDirectorySeverityInternal, } from "./classify-scan-severity.js";
20
21
  import { resolveCompletedScan, resolveWorkflowScan, } from "./saved-scan.js";
21
22
  import { publishFindingsCsvToCloud, publishScanToCloud, } from "./cloud-publish.js";
22
23
  import { createBulkScanDiscoveryDependencies, runBulkScanWizard, } from "./bulk-scan-discovery.js";
@@ -27,10 +28,10 @@ import { GITHUB_ALERT_STATES, importGitHubCodeScanningAlerts, } from "./github.j
27
28
  import { importLinearIssues, isLinearIssueIdentifier, resolveLinearApiKey, } from "./linear.js";
28
29
  import { runMultiscan } from "./multiscan.js";
29
30
  import { componentPlanSchema } from "./component-plan.js";
30
- import { runComponentScans } from "./component-scan.js";
31
+ import { runComponentScans, } from "./component-scan.js";
31
32
  import { checkScanPublication, forceTerminatePublicationProcesses as terminatePublishers, publishScan, } from "./publish.js";
32
33
  import { bundledPluginRoot, canonicalizeModelSafePath, codexSecurityCredentialHome, codexSecurityStateDirectory, executablePathForSpawn, expandHome, prepareCodexSecurityCredentialHome, pythonUtf8Environment, resolveCodexCommand, resolvePluginPython, runWorkbench, setCodexSecurityCredentialLogout, } from "./runtime.js";
33
- import { comparisonFindingGroups, matchScanFindingsInternal, unionFindingGroups, } from "./scan-comparison.js";
34
+ import { comparisonFindingGroups, comparisonForScan, matchScanFindingsInternal, unionFindingGroups, } from "./scan-comparison.js";
34
35
  import { scanActivitiesFromEvent } from "./scan-activity.js";
35
36
  import { CODEX_SECURITY_THREAD_SOURCES, } from "./thread-source.js";
36
37
  import { readScanLogs } from "./scan-logs.js";
@@ -48,6 +49,7 @@ const OUTPUT_OPTION = /^--(?:format|filter-output|full-output|token-count|token-
48
49
  const HIDE_CURSOR = "\u001B[?25l";
49
50
  const SHOW_CURSOR = "\u001B[?25h";
50
51
  const CHILD_TERMINATION_GRACE_MS = 1_000;
52
+ const DUPLICATE_SIGNAL_WINDOW_MS = 500;
51
53
  const PUBLICATION_GRAPHEME_SEGMENTER = new Intl.Segmenter(undefined, {
52
54
  granularity: "grapheme",
53
55
  });
@@ -87,6 +89,8 @@ const VALUE_OPTIONS = new Set([
87
89
  "--component",
88
90
  "--components-file",
89
91
  "--knowledge-base",
92
+ "--rubric",
93
+ "--finding-id",
90
94
  "--scan-prompt-file",
91
95
  "--validation-prompt-file",
92
96
  "--post-scan-prompt-file",
@@ -144,7 +148,7 @@ const PROVIDER_OPTION = z
144
148
  const CREATE_PR_OPTION = z
145
149
  .boolean()
146
150
  .default(false)
147
- .describe("Create a draft GitHub pull request after verified patches.");
151
+ .describe("Create a draft GitHub pull request or GitLab merge request after verified patches.");
148
152
  const ASSESS_PATCH_RISK_OPTION = z
149
153
  .boolean()
150
154
  .default(false)
@@ -799,16 +803,17 @@ const DEFAULT_DEPENDENCIES = {
799
803
  }
800
804
  return undefined;
801
805
  },
802
- runWorkbench: async (args, input) => {
806
+ runWorkbench: async (args, input, signal) => {
803
807
  const environment = {
804
808
  ...exportEnvironment(),
805
809
  CODEX_SECURITY_STATE_DIR: codexSecurityStateDirectory(),
806
810
  };
807
- const python = await resolvePluginPython({ environment });
811
+ const python = await resolvePluginPython({ environment, signal });
808
812
  return await runWorkbench({
809
813
  python,
810
814
  pluginRoot: await bundledPluginRoot(),
811
815
  environment,
816
+ signal,
812
817
  failureMessage: "Could not read Codex Security scan history",
813
818
  }, args, input);
814
819
  },
@@ -1069,16 +1074,82 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1069
1074
  });
1070
1075
  return result?.["scans"];
1071
1076
  };
1072
- const matchScanPair = async (beforeId, afterId, force = false) => history([
1073
- "compare-scans",
1074
- "--before-scan-id",
1075
- beforeId,
1076
- "--after-scan-id",
1077
- afterId,
1078
- "--include-matching-inputs",
1079
- ], async ({ matchingCached, matchingInputs, ...comparison }) => {
1077
+ const runMatching = async (operation) => {
1078
+ const controller = new AbortController();
1079
+ let firstSignalAt = 0;
1080
+ const cancel = (signal) => {
1081
+ if (controller.signal.aborted) {
1082
+ if (signal === controller.signal.reason &&
1083
+ dependencies.now() - firstSignalAt < DUPLICATE_SIGNAL_WINDOW_MS) {
1084
+ return;
1085
+ }
1086
+ removeListeners();
1087
+ dependencies.forceExit(signal);
1088
+ }
1089
+ else {
1090
+ firstSignalAt = dependencies.now();
1091
+ controller.abort(signal);
1092
+ }
1093
+ };
1094
+ const onInterrupt = () => cancel("SIGINT");
1095
+ const onTerminate = () => cancel("SIGTERM");
1096
+ const removeListeners = () => {
1097
+ dependencies.removeSignalListener("SIGINT", onInterrupt);
1098
+ dependencies.removeSignalListener("SIGTERM", onTerminate);
1099
+ };
1100
+ dependencies.addSignalListener("SIGINT", onInterrupt);
1101
+ dependencies.addSignalListener("SIGTERM", onTerminate);
1102
+ let previousProgress = "";
1103
+ try {
1104
+ const result = await operation({
1105
+ environment: dependencies.environment,
1106
+ workingDirectory: dependencies.currentDirectory(),
1107
+ signal: controller.signal,
1108
+ onProgress(progress) {
1109
+ if (errorOutput.isTTY !== true || progress.phase === "complete")
1110
+ return;
1111
+ const message = progress.phase === "evidence"
1112
+ ? "Reading selected finding evidence."
1113
+ : `Matching ${progress.afterFindings} findings against ${progress.beforeIssues} known issues${(progress.pages ?? 1) > 1 ? ` (catalogue page ${progress.page}/${progress.pages})` : ""}.`;
1114
+ if (message === previousProgress)
1115
+ return;
1116
+ previousProgress = message;
1117
+ errorOutput.write(`codex-security: ${message}\n`);
1118
+ },
1119
+ });
1120
+ controller.signal.throwIfAborted();
1121
+ return result;
1122
+ }
1123
+ catch (error) {
1124
+ const interrupted = controller.signal.reason;
1125
+ exitCode =
1126
+ interrupted === "SIGINT" ? 130 : interrupted === "SIGTERM" ? 143 : 2;
1127
+ const message = interrupted === "SIGINT"
1128
+ ? "Finding matching canceled by Ctrl-C. Saved comparisons are preserved."
1129
+ : interrupted === "SIGTERM"
1130
+ ? "Finding matching terminated by SIGTERM. Saved comparisons are preserved."
1131
+ : errorMessage(error);
1132
+ errorOutput.write(`codex-security: ${message}\n`);
1133
+ throw error;
1134
+ }
1135
+ finally {
1136
+ removeListeners();
1137
+ }
1138
+ };
1139
+ const matchScanPair = async (beforeId, afterId, force = false) => runMatching(async (options) => {
1140
+ const { matchingCached, matchingInputs, ...comparison } = await dependencies.runWorkbench([
1141
+ "compare-scans",
1142
+ "--before-scan-id",
1143
+ beforeId,
1144
+ "--after-scan-id",
1145
+ afterId,
1146
+ "--include-matching-inputs",
1147
+ ], undefined, options.signal);
1080
1148
  if (matchingCached && !force)
1081
1149
  return comparison;
1150
+ const input = matchingInputs;
1151
+ const matching = await dependencies.matchFindings(input, options);
1152
+ options.signal?.throwIfAborted();
1082
1153
  return await dependencies.runWorkbench([
1083
1154
  "save-scan-comparison",
1084
1155
  "--before-scan-id",
@@ -1086,7 +1157,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1086
1157
  "--after-scan-id",
1087
1158
  afterId,
1088
1159
  "--matches-json-stdin",
1089
- ], JSON.stringify(await dependencies.matchFindings(matchingInputs)));
1160
+ ], JSON.stringify(matching), options.signal);
1090
1161
  });
1091
1162
  const presentHistory = (result, command, format, settings = {}) => {
1092
1163
  if (result === undefined ||
@@ -1356,17 +1427,10 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1356
1427
  }),
1357
1428
  output: z.record(z.string(), z.unknown()).optional(),
1358
1429
  async run({ args, format, options }) {
1359
- try {
1360
- if (options.all) {
1361
- return presentHistory(await matchAllScans(dependencies, options.force), "match-all", format);
1362
- }
1363
- return presentHistory(await matchScanPair(args.beforeId, args.afterId, options.force), "compare", format);
1364
- }
1365
- catch (error) {
1366
- errorOutput.write(`codex-security: ${errorMessage(error)}\n`);
1367
- exitCode = 2;
1368
- throw error;
1430
+ if (options.all) {
1431
+ return presentHistory(await runMatching((matchingOptions) => matchAllScans(dependencies, options.force, matchingOptions)), "match-all", format);
1369
1432
  }
1433
+ return presentHistory(await matchScanPair(args.beforeId, args.afterId, options.force), "compare", format);
1370
1434
  },
1371
1435
  })
1372
1436
  .command("compare", {
@@ -1430,6 +1494,10 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1430
1494
  .describe("Completed scan directory; omit to select a saved scan."),
1431
1495
  }),
1432
1496
  options: PUBLICATION_DESTINATION_OPTIONS.extend({
1497
+ findingId: z
1498
+ .array(optionValue("--finding-id"))
1499
+ .default([])
1500
+ .describe("Publish only this finding ID; repeat to select deduplicated findings (Linear only)."),
1433
1501
  workflowId: optionValue("--workflow-id")
1434
1502
  .optional()
1435
1503
  .describe("Resume the named local scan, custom publication, and dedupe workflow."),
@@ -1517,6 +1585,9 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1517
1585
  };
1518
1586
  try {
1519
1587
  const currentDirectory = dependencies.currentDirectory();
1588
+ if (options.findingId.length > 0 && options.to !== "linear") {
1589
+ throw new CodexSecurityError("--finding-id is only supported with --to linear.");
1590
+ }
1520
1591
  const csvPath = options.csv === undefined
1521
1592
  ? undefined
1522
1593
  : resolveCliPath(currentDirectory, options.csv);
@@ -1826,6 +1897,9 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
1826
1897
  try {
1827
1898
  result = await (dependencies.publishScan ?? publishScan)(resolveCliPath(currentDirectory, scanDir), {
1828
1899
  ...destination,
1900
+ ...(options.findingId.length === 0
1901
+ ? {}
1902
+ : { findingIds: options.findingId }),
1829
1903
  ...(selectedScans[0]?.scanId === undefined
1830
1904
  ? {}
1831
1905
  : { expectedScanId: selectedScans[0].scanId }),
@@ -2274,6 +2348,82 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
2274
2348
  .command(scanHistory)
2275
2349
  .command(findingFeedback)
2276
2350
  .command(publication)
2351
+ .command("classify-severity", {
2352
+ description: "Classify saved findings using an optional rubric and save a separate severity assessment.",
2353
+ destructive: true,
2354
+ mcp: false,
2355
+ options: z.object({
2356
+ reprocess: z
2357
+ .boolean()
2358
+ .default(false)
2359
+ .describe("Reclassify selected findings even when a matching assessment is saved."),
2360
+ scan: optionValue("--scan")
2361
+ .optional()
2362
+ .describe("Saved scan ID, unique prefix, or latest."),
2363
+ scanDir: optionValue("--scan-dir")
2364
+ .optional()
2365
+ .describe("External completed scan directory."),
2366
+ rubric: optionValue("--rubric")
2367
+ .optional()
2368
+ .describe("Classification policy document; omit to inherit existing severity without a model call."),
2369
+ knowledgeBase: z
2370
+ .array(optionValue("--knowledge-base"))
2371
+ .default([])
2372
+ .describe("Supporting security context; repeat for more files or directories."),
2373
+ findingId: z
2374
+ .array(optionValue("--finding-id"))
2375
+ .default([])
2376
+ .describe("Classify only this finding ID; repeat to select deduplicated findings."),
2377
+ model: optionValue("--model")
2378
+ .optional()
2379
+ .describe("Model for rubric classification."),
2380
+ effort: effortOption().describe("Classification reasoning effort (default: medium)."),
2381
+ }),
2382
+ output: z.record(z.string(), z.unknown()).optional(),
2383
+ async run({ options }) {
2384
+ const controller = new AbortController();
2385
+ const onInterrupt = () => controller.abort("SIGINT");
2386
+ const onTerminate = () => controller.abort("SIGTERM");
2387
+ dependencies.addSignalListener("SIGINT", onInterrupt);
2388
+ dependencies.addSignalListener("SIGTERM", onTerminate);
2389
+ try {
2390
+ if ((options.scan === undefined) ===
2391
+ (options.scanDir === undefined)) {
2392
+ throw new CodexSecurityError("Severity classification requires exactly one of --scan or --scan-dir.");
2393
+ }
2394
+ const currentDirectory = dependencies.currentDirectory();
2395
+ const settings = {
2396
+ environment: dependencies.environment,
2397
+ workingDirectory: currentDirectory,
2398
+ signal: controller.signal,
2399
+ rubricPath: options.rubric === undefined
2400
+ ? undefined
2401
+ : resolveCliPath(currentDirectory, options.rubric),
2402
+ knowledgeBasePaths: options.knowledgeBase.map((path) => resolveCliPath(currentDirectory, path)),
2403
+ findingIds: options.findingId.length === 0 ? undefined : options.findingId,
2404
+ reprocess: options.reprocess,
2405
+ model: options.model,
2406
+ reasoningEffort: options.effort,
2407
+ };
2408
+ const result = options.scan !== undefined
2409
+ ? await (dependencies.classifyScanSeverity ??
2410
+ classifyScanSeverityInternal)(options.scan, settings, dependencies, "cli")
2411
+ : await (dependencies.classifyScanDirectorySeverity ??
2412
+ classifyScanDirectorySeverityInternal)(resolveCliPath(currentDirectory, options.scanDir), settings, "cli");
2413
+ return { ...result };
2414
+ }
2415
+ catch (error) {
2416
+ const signal = controller.signal.reason;
2417
+ errorOutput.write(`codex-security: ${signal === "SIGINT" || signal === "SIGTERM" ? "Severity classification canceled." : safeErrorMessage(error)}\n`);
2418
+ exitCode = signal === "SIGINT" ? 130 : signal === "SIGTERM" ? 143 : 2;
2419
+ return undefined;
2420
+ }
2421
+ finally {
2422
+ dependencies.removeSignalListener("SIGINT", onInterrupt);
2423
+ dependencies.removeSignalListener("SIGTERM", onTerminate);
2424
+ }
2425
+ },
2426
+ })
2277
2427
  .command("dedupe", {
2278
2428
  description: "Review a saved scan with local Codex and save duplicate groups to the findings API.",
2279
2429
  destructive: true,
@@ -2428,6 +2578,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
2428
2578
  async run({ args, options }) {
2429
2579
  const controller = new AbortController();
2430
2580
  let dashboard = null;
2581
+ const componentNames = new Map();
2431
2582
  const stopDashboard = () => {
2432
2583
  try {
2433
2584
  dashboard?.stop();
@@ -2500,9 +2651,19 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
2500
2651
  createSecurity: dependencies.createSecurity,
2501
2652
  planComponents: dependencies.planComponents,
2502
2653
  matchFindings: dependencies.matchFindings,
2503
- onPlan: (components) => dashboard?.setComponents(components),
2654
+ onPlan: (components) => {
2655
+ for (const component of components)
2656
+ componentNames.set(component.id, component.name);
2657
+ dashboard?.setComponents(components);
2658
+ },
2504
2659
  onScanEvent: dashboard === null
2505
- ? undefined
2660
+ ? (event) => {
2661
+ const componentName = componentNames.get(event.componentId) ??
2662
+ event.componentId;
2663
+ const line = componentScanEventLine(componentName, event);
2664
+ if (line !== null)
2665
+ errorOutput.write(line);
2666
+ }
2506
2667
  : (event) => dashboard?.recordComponentEvent(event),
2507
2668
  onDeduplicationStarted: () => {
2508
2669
  if (dashboard !== null)
@@ -2772,7 +2933,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
2772
2933
  codex: z
2773
2934
  .array(optionValue("--codex"))
2774
2935
  .default([])
2775
- .describe('Repeat TOML model="gpt-5.6-terra" or model_reasoning_effort="high" only.'),
2936
+ .describe('Repeat TOML model="gpt-5.6-terra", model_reasoning_effort="high", or analytics.enabled=false.'),
2776
2937
  }),
2777
2938
  async run({ options }) {
2778
2939
  try {
@@ -2818,7 +2979,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
2818
2979
  codex: z
2819
2980
  .array(optionValue("--codex"))
2820
2981
  .default([])
2821
- .describe('Repeat TOML model="gpt-5.6-terra" or model_reasoning_effort="high" only.'),
2982
+ .describe('Repeat TOML model="gpt-5.6-terra", model_reasoning_effort="high", or analytics.enabled=false.'),
2822
2983
  }),
2823
2984
  output: z.record(z.string(), z.unknown()).optional(),
2824
2985
  async run({ format, options }) {
@@ -2981,7 +3142,7 @@ export async function main(argv = process.argv.slice(2), output = process.stdout
2981
3142
  codex: z
2982
3143
  .array(optionValue("--codex"))
2983
3144
  .default([])
2984
- .describe('Repeat TOML model="gpt-5.6-terra" or model_reasoning_effort="high" only.'),
3145
+ .describe('Repeat TOML model="gpt-5.6-terra", model_reasoning_effort="high", or analytics.enabled=false.'),
2985
3146
  }),
2986
3147
  output: z.record(z.string(), z.unknown()).optional(),
2987
3148
  async run({ format, options }) {
@@ -3553,18 +3714,21 @@ function validateCliArguments(argv, positionals) {
3553
3714
  return `Unexpected positional argument for ${command}${subcommand === undefined ? "" : ` ${subcommand}`}.`;
3554
3715
  }
3555
3716
  }
3556
- async function matchAllScans(dependencies, force) {
3717
+ async function matchAllScans(dependencies, force, options = {}) {
3557
3718
  const result = (await dependencies.runWorkbench([
3558
3719
  "list-unmatched-scan-pairs",
3559
3720
  "--repository",
3560
3721
  dependencies.currentDirectory(),
3561
3722
  ...(force ? ["--force"] : []),
3562
- ]));
3723
+ ], undefined, options.signal));
3563
3724
  const { repository, scanCount, unavailableScans, skippedPairs, batches } = result;
3564
3725
  let matchedPairs = 0;
3565
3726
  let findingMatches = 0;
3727
+ let relatedPairs = 0;
3728
+ let uncertainPairs = 0;
3566
3729
  const newlyMatchedGroups = [];
3567
3730
  for (const { afterScanId, afterFindings, beforeScans, knownFindingGroups = [], } of batches) {
3731
+ options.signal?.throwIfAborted();
3568
3732
  const before = beforeScans.flatMap(({ findings }) => findings);
3569
3733
  const knownGroups = unionFindingGroups([
3570
3734
  ...knownFindingGroups,
@@ -3578,25 +3742,15 @@ async function matchAllScans(dependencies, force) {
3578
3742
  const matching = before.length === 0 || afterFindings.length === 0
3579
3743
  ? { matches: [], uncertain: [] }
3580
3744
  : await dependencies.matchFindings(input, {
3745
+ ...options,
3581
3746
  allowHistoricalUncertainty: true,
3582
3747
  });
3583
- const comparisons = beforeScans.map(({ scanId, findings }) => {
3584
- const beforeIds = new Set(findings.map(({ occurrenceId }) => occurrenceId));
3585
- const matches = matching.matches.flatMap((match) => {
3586
- const beforeOccurrenceIds = match.beforeOccurrenceIds.filter((id) => beforeIds.has(id));
3587
- return beforeOccurrenceIds.length === 0
3588
- ? []
3589
- : [{ ...match, beforeOccurrenceIds }];
3590
- });
3591
- const uncertain = matching.uncertain.filter(({ beforeOccurrenceId }) => beforeIds.has(beforeOccurrenceId));
3592
- const related = matching.related?.filter(({ beforeOccurrenceId }) => beforeIds.has(beforeOccurrenceId));
3593
- const matchedAfter = new Set(matches.flatMap(({ afterOccurrenceIds }) => afterOccurrenceIds));
3594
- if (uncertain.some(({ afterOccurrenceId }) => matchedAfter.has(afterOccurrenceId))) {
3595
- throw new CodexSecurityError("Scan matching returned conflicting confirmed and uncertain findings.");
3596
- }
3597
- return { scanId, matches, uncertain, related };
3598
- });
3599
- for (const { scanId, matches, uncertain, related } of comparisons) {
3748
+ const comparisons = beforeScans.map(({ scanId, findings }) => ({
3749
+ scanId,
3750
+ comparison: comparisonForScan(matching, findings),
3751
+ }));
3752
+ for (const { scanId, comparison } of comparisons) {
3753
+ options.signal?.throwIfAborted();
3600
3754
  await dependencies.runWorkbench([
3601
3755
  "save-scan-comparison",
3602
3756
  "--before-scan-id",
@@ -3604,9 +3758,11 @@ async function matchAllScans(dependencies, force) {
3604
3758
  "--after-scan-id",
3605
3759
  afterScanId,
3606
3760
  "--matches-json-stdin",
3607
- ], JSON.stringify({ matches, uncertain, related }));
3761
+ ], JSON.stringify(comparison), options.signal);
3608
3762
  matchedPairs += 1;
3609
- findingMatches += matches.reduce((count, { beforeOccurrenceIds, afterOccurrenceIds }) => count + beforeOccurrenceIds.length * afterOccurrenceIds.length, 0);
3763
+ findingMatches += comparison.matches.reduce((count, { beforeOccurrenceIds, afterOccurrenceIds }) => count + beforeOccurrenceIds.length * afterOccurrenceIds.length, 0);
3764
+ relatedPairs += comparison.related?.length ?? 0;
3765
+ uncertainPairs += comparison.uncertain.length;
3610
3766
  }
3611
3767
  newlyMatchedGroups.push(...comparisonFindingGroups(input, matching));
3612
3768
  }
@@ -3617,6 +3773,8 @@ async function matchAllScans(dependencies, force) {
3617
3773
  matchedPairs,
3618
3774
  skippedPairs,
3619
3775
  findingMatches,
3776
+ relatedPairs,
3777
+ uncertainPairs,
3620
3778
  };
3621
3779
  }
3622
3780
  function staysWithinWindowsDeviceRoot(input, root) {
@@ -3776,33 +3934,75 @@ function directPatchDigest(positionals, imports) {
3776
3934
  async function publishPatchBranch(repository, branch, body, stderr, dependencies) {
3777
3935
  const run = (command, args) => dependencies.runRepositoryCommand(command, args, repository);
3778
3936
  try {
3779
- let url = await run("gh", [
3780
- "pr",
3781
- "list",
3782
- "--head",
3783
- branch,
3784
- "--state",
3785
- "all",
3786
- "--json",
3787
- "url",
3788
- "--jq",
3789
- ".[0].url // empty",
3790
- ]);
3791
- if (!url) {
3792
- await run("git", ["push", "--set-upstream", "origin", branch]);
3793
- url = await run("gh", [
3937
+ const remote = await run("git", ["remote", "get-url", "--push", "origin"]);
3938
+ const host = patchRemoteHost(remote);
3939
+ const gitlabHost = dependencies.environment["GITLAB_HOST"] ||
3940
+ dependencies.environment["GITLAB_URI"] ||
3941
+ dependencies.environment["GL_HOST"];
3942
+ const gitlab = host === "gitlab.com" ||
3943
+ (host !== undefined &&
3944
+ gitlabHost !== undefined &&
3945
+ host ===
3946
+ patchRemoteHost(gitlabHost.includes("://") ? gitlabHost : `https://${gitlabHost}`));
3947
+ const command = gitlab ? "glab" : "gh";
3948
+ let url = await run(command, gitlab
3949
+ ? [
3950
+ "mr",
3951
+ "list",
3952
+ "--all",
3953
+ "--source-branch",
3954
+ branch,
3955
+ "--output",
3956
+ "json",
3957
+ "--jq",
3958
+ ".[0].web_url // empty",
3959
+ "--repo",
3960
+ remote,
3961
+ ]
3962
+ : [
3794
3963
  "pr",
3795
- "create",
3796
- "--draft",
3964
+ "list",
3797
3965
  "--head",
3798
3966
  branch,
3799
- "--title",
3800
- PATCH_PR_TITLE,
3801
- "--body",
3802
- body,
3967
+ "--state",
3968
+ "all",
3969
+ "--json",
3970
+ "url",
3971
+ "--jq",
3972
+ ".[0].url // empty",
3803
3973
  ]);
3974
+ if (!url) {
3975
+ await run("git", ["push", "--set-upstream", "origin", branch]);
3976
+ url = await run(command, gitlab
3977
+ ? [
3978
+ "mr",
3979
+ "create",
3980
+ "--draft",
3981
+ "--head",
3982
+ remote,
3983
+ "--source-branch",
3984
+ branch,
3985
+ "--title",
3986
+ PATCH_PR_TITLE,
3987
+ "--description",
3988
+ body,
3989
+ "--yes",
3990
+ "--repo",
3991
+ remote,
3992
+ ]
3993
+ : [
3994
+ "pr",
3995
+ "create",
3996
+ "--draft",
3997
+ "--head",
3998
+ branch,
3999
+ "--title",
4000
+ PATCH_PR_TITLE,
4001
+ "--body",
4002
+ body,
4003
+ ]);
3804
4004
  }
3805
- stderr.write(`Pull request: ${safePatchText(url)}\n`);
4005
+ stderr.write(`${gitlab ? "Merge" : "Pull"} request: ${safePatchText(url)}\n`);
3806
4006
  return { branch, url };
3807
4007
  }
3808
4008
  catch (error) {
@@ -3810,6 +4010,11 @@ async function publishPatchBranch(repository, branch, body, stderr, dependencies
3810
4010
  throw error;
3811
4011
  }
3812
4012
  }
4013
+ function patchRemoteHost(remote) {
4014
+ if (remote.includes("://"))
4015
+ return new URL(remote).hostname.toLowerCase();
4016
+ return /^(?:[^@/]+@)?([^:/]+):[^/]/u.exec(remote)?.[1]?.toLowerCase();
4017
+ }
3813
4018
  async function resumePatchPullRequest(repository, branch, stderr, dependencies) {
3814
4019
  const run = (args) => dependencies.runRepositoryCommand("git", args, repository);
3815
4020
  const commit = await run([
@@ -3855,11 +4060,11 @@ async function createPatchPullRequest(repository, patchId, files, stderr, depend
3855
4060
  }
3856
4061
  const branch = `codex-security/patch-${patchId.replaceAll(/[^a-z\d._-]/giu, "-")}`;
3857
4062
  const body = patchPullRequestBody(patchRiskSummary, introduction);
3858
- const run = (command, args) => dependencies.runRepositoryCommand(command, args, repository);
3859
- stderr.write("Creating a draft GitHub pull request for verified patches...\n");
3860
- await run("git", ["switch", "-c", branch]);
3861
- await run("git", ["--literal-pathspecs", "add", "--", ...files]);
3862
- await run("git", [
4063
+ const run = (args) => dependencies.runRepositoryCommand("git", args, repository);
4064
+ stderr.write("Creating a draft pull request or merge request for verified patches...\n");
4065
+ await run(["switch", "-c", branch]);
4066
+ await run(["--literal-pathspecs", "add", "--", ...files]);
4067
+ await run([
3863
4068
  "--literal-pathspecs",
3864
4069
  "commit",
3865
4070
  "--only",
@@ -3868,14 +4073,9 @@ async function createPatchPullRequest(repository, patchId, files, stderr, depend
3868
4073
  "--",
3869
4074
  ...files,
3870
4075
  ]);
3871
- const commit = await run("git", ["rev-parse", "HEAD"]);
3872
- await run("git", ["config", "--local", patchCommitKey(branch), commit]);
3873
- await run("git", [
3874
- "config",
3875
- "--local",
3876
- patchPullRequestBodyKey(branch),
3877
- body,
3878
- ]);
4076
+ const commit = await run(["rev-parse", "HEAD"]);
4077
+ await run(["config", "--local", patchCommitKey(branch), commit]);
4078
+ await run(["config", "--local", patchPullRequestBodyKey(branch), body]);
3879
4079
  return publishPatchBranch(repository, branch, body, stderr, dependencies);
3880
4080
  }
3881
4081
  async function requireCleanPatchPullRequestBase(repository, base, dependencies) {
@@ -4080,8 +4280,12 @@ async function runFindingPatches(selected, codexOverrides, effort, stderr, depen
4080
4280
  }
4081
4281
  async function runSkill(skill, inputs, codexOverrides, effort, stdout, stderr, dependencies, options = {}) {
4082
4282
  const overrides = parseCodexOverrides(codexOverrides, undefined, effort);
4083
- if (Object.keys(overrides).some((key) => key !== "model" && key !== "model_reasoning_effort")) {
4084
- throw new CodexSecurityError("Validation and patching only support model and model_reasoning_effort overrides.");
4283
+ if (Object.entries(overrides).some(([key, value]) => key !== "model" &&
4284
+ key !== "model_reasoning_effort" &&
4285
+ !(key === "analytics" &&
4286
+ isJsonObject(value) &&
4287
+ Object.keys(value).every((key) => key === "enabled")))) {
4288
+ throw new CodexSecurityError("Skill commands only support model, model_reasoning_effort, and analytics.enabled overrides.");
4085
4289
  }
4086
4290
  const { model, reasoningEffort } = scanModelConfiguration(await mergedCodexConfig({ codexOverrides: overrides }));
4087
4291
  const directory = options.directory ?? dependencies.currentDirectory();
@@ -4201,6 +4405,9 @@ async function runSkill(skill, inputs, codexOverrides, effort, stdout, stderr, d
4201
4405
  `model=${JSON.stringify(model)}`,
4202
4406
  "--config",
4203
4407
  `model_reasoning_effort=${JSON.stringify(reasoningEffort)}`,
4408
+ ...codexOverrides
4409
+ .filter((value) => value.startsWith("analytics.") || value.startsWith("analytics="))
4410
+ .flatMap((value) => ["--config", value]),
4204
4411
  ...(options.provider === undefined
4205
4412
  ? []
4206
4413
  : ["--config", `model_provider=${JSON.stringify(options.provider)}`]),
@@ -4677,7 +4884,7 @@ async function executeScan(arguments_, errorOutput, dependencies, interactive =
4677
4884
  // Launchers and terminals can deliver the same initial signal twice.
4678
4885
  // A later repeated signal intentionally restores the conventional escape hatch.
4679
4886
  if (signal === requestedSignal &&
4680
- dependencies.now() - firstSignalAt < 500) {
4887
+ dependencies.now() - firstSignalAt < DUPLICATE_SIGNAL_WINDOW_MS) {
4681
4888
  return;
4682
4889
  }
4683
4890
  requestedSignal = signal;
@@ -4712,6 +4919,7 @@ async function executeScan(arguments_, errorOutput, dependencies, interactive =
4712
4919
  let effectiveModel = DEFAULT_SCAN_MODEL_CONFIGURATION.model;
4713
4920
  let effectiveReasoningEffort = DEFAULT_SCAN_MODEL_CONFIGURATION.reasoningEffort;
4714
4921
  let providerOptions = {};
4922
+ let patchAnalyticsOverride;
4715
4923
  let selectedAuthentication = null;
4716
4924
  let repository = "";
4717
4925
  let failed = false;
@@ -4735,6 +4943,12 @@ async function executeScan(arguments_, errorOutput, dependencies, interactive =
4735
4943
  ({ model: effectiveModel, reasoningEffort: effectiveReasoningEffort } =
4736
4944
  scanModelConfiguration(effectiveConfiguration));
4737
4945
  const provider = scanModelProvider(effectiveConfiguration);
4946
+ const analytics = effectiveConfiguration["analytics"];
4947
+ if (analytics !== undefined &&
4948
+ isJsonObject(analytics) &&
4949
+ analytics["enabled"] !== undefined) {
4950
+ patchAnalyticsOverride = `analytics.enabled=${JSON.stringify(analytics["enabled"])}`;
4951
+ }
4738
4952
  const auth = !arguments_.dryRun && !arguments_.mock && interactive
4739
4953
  ? await chooseInteractiveAuthentication({
4740
4954
  auth: arguments_.auth,
@@ -5259,7 +5473,12 @@ async function executeScan(arguments_, errorOutput, dependencies, interactive =
5259
5473
  environment["CODEX_HOME"] = codexSecurityCredentialHome(dependencies.environment);
5260
5474
  }
5261
5475
  try {
5262
- patches = await runFindingPatches(selected, [`model=${JSON.stringify(effectiveModel)}`], effectiveReasoningEffort, errorOutput, dependencies, {
5476
+ patches = await runFindingPatches(selected, [
5477
+ `model=${JSON.stringify(effectiveModel)}`,
5478
+ ...(patchAnalyticsOverride === undefined
5479
+ ? []
5480
+ : [patchAnalyticsOverride]),
5481
+ ], effectiveReasoningEffort, errorOutput, dependencies, {
5263
5482
  ...providerOptions,
5264
5483
  safetyIdentifier: arguments_.safetyIdentifier,
5265
5484
  environment,
@@ -5522,6 +5741,21 @@ function formatTokenUsage(usage) {
5522
5741
  .filter((value) => value !== null)
5523
5742
  .join(", ") || null);
5524
5743
  }
5744
+ function componentScanEventLine(componentName, event) {
5745
+ if (event.type === "progress") {
5746
+ const progress = event.value;
5747
+ return `codex-security: ${componentName} ${scanPhase(progress.phase)} | Files: ${progress.filesCompleted.toLocaleString("en-US")}/${progress.filesTotal.toLocaleString("en-US")}\n`;
5748
+ }
5749
+ if (event.type !== "cost")
5750
+ return null;
5751
+ const cost = event.value;
5752
+ const tokens = formatTokenUsage({
5753
+ input_tokens: cost.inputTokens,
5754
+ cached_input_tokens: cost.cachedInputTokens,
5755
+ output_tokens: cost.outputTokens,
5756
+ });
5757
+ return `codex-security: ${componentName} | ${tokens === null ? "" : `Tokens: ${tokens} | `}Cost: ${formatUsd(cost.estimatedUsd)}\n`;
5758
+ }
5525
5759
  function protectedRootErrorMessage(error) {
5526
5760
  const description = error.pathKind === "output"
5527
5761
  ? "Scan output directory"