@openai/codex-security 0.1.21 → 0.1.24

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 (183) hide show
  1. package/README.md +549 -1
  2. package/_bundled_plugin/.codex-plugin/plugin.json +1 -1
  3. package/_bundled_plugin/.mcp.json +1 -0
  4. package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
  5. package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
  6. package/_bundled_plugin/references/config-preflight.md +2 -30
  7. package/_bundled_plugin/references/core-scan.md +1 -1
  8. package/_bundled_plugin/references/final-report.md +1 -1
  9. package/_bundled_plugin/references/finding-detail-fields.md +5 -10
  10. package/_bundled_plugin/references/sarif-adapter.md +1 -1
  11. package/_bundled_plugin/references/scan-contract.md +10 -0
  12. package/_bundled_plugin/scripts/config_preflight.py +2 -8
  13. package/_bundled_plugin/scripts/deep_scan_workbench.py +6 -1
  14. package/_bundled_plugin/scripts/finalize_scan_contract.py +115 -76
  15. package/_bundled_plugin/scripts/finding_preview.py +22 -4
  16. package/_bundled_plugin/scripts/generate_in_scope_files.py +29 -9
  17. package/_bundled_plugin/scripts/generate_rank_input.py +1 -5
  18. package/_bundled_plugin/scripts/rank_preview.py +1 -0
  19. package/_bundled_plugin/scripts/report_projection.py +8 -14
  20. package/_bundled_plugin/scripts/validate_scan_contract.py +1 -3
  21. package/_bundled_plugin/scripts/windows_scan_local_files.py +90 -6
  22. package/_bundled_plugin/scripts/workbench_cli.py +27 -1
  23. package/_bundled_plugin/scripts/workbench_dashboard.py +138 -0
  24. package/_bundled_plugin/scripts/workbench_db.py +110 -539
  25. package/_bundled_plugin/scripts/workbench_finding_index.py +42 -24
  26. package/_bundled_plugin/scripts/workbench_finding_workflows.py +208 -0
  27. package/_bundled_plugin/scripts/workbench_findings.py +221 -0
  28. package/_bundled_plugin/scripts/workbench_native_indexes.py +1 -2
  29. package/_bundled_plugin/scripts/workbench_publication.py +520 -0
  30. package/_bundled_plugin/scripts/workbench_remediation.py +37 -0
  31. package/_bundled_plugin/scripts/workbench_saved_results.py +322 -89
  32. package/_bundled_plugin/scripts/workbench_scan_history.py +1 -2
  33. package/_bundled_plugin/scripts/workbench_scan_usage.py +9 -33
  34. package/_bundled_plugin/scripts/workbench_schema.py +276 -58
  35. package/_bundled_plugin/scripts/workbench_target.py +30 -6
  36. package/_bundled_plugin/scripts/workbench_validation.py +1 -5
  37. package/_bundled_plugin/skills/assess-patch-risk/SKILL.md +1 -5
  38. package/_bundled_plugin/skills/attack-path-analysis/references/severity-policy.md +6 -1
  39. package/_bundled_plugin/skills/track-findings/SKILL.md +2 -0
  40. package/dist/api.d.ts +5 -1
  41. package/dist/api.d.ts.map +1 -1
  42. package/dist/api.js +135 -27
  43. package/dist/api.js.map +1 -1
  44. package/dist/cli.d.ts +4 -0
  45. package/dist/cli.d.ts.map +1 -1
  46. package/dist/cli.js +158 -62
  47. package/dist/cli.js.map +1 -1
  48. package/dist/cloud-publish.js +5 -1
  49. package/dist/cloud-publish.js.map +1 -1
  50. package/dist/contract.d.ts +1 -0
  51. package/dist/contract.d.ts.map +1 -1
  52. package/dist/contract.js +9 -0
  53. package/dist/contract.js.map +1 -1
  54. package/dist/custom-publish.d.ts +22 -0
  55. package/dist/custom-publish.d.ts.map +1 -0
  56. package/dist/custom-publish.js +47 -0
  57. package/dist/custom-publish.js.map +1 -0
  58. package/dist/custom-validation-prompt.js +1 -1
  59. package/dist/custom-validation-prompt.js.map +1 -1
  60. package/dist/deduplication/checkpointed-review.d.ts +16 -0
  61. package/dist/deduplication/checkpointed-review.d.ts.map +1 -0
  62. package/dist/deduplication/checkpointed-review.js +76 -0
  63. package/dist/deduplication/checkpointed-review.js.map +1 -0
  64. package/dist/deduplication/codex-review.d.ts +21 -0
  65. package/dist/deduplication/codex-review.d.ts.map +1 -0
  66. package/dist/deduplication/codex-review.js +252 -0
  67. package/dist/deduplication/codex-review.js.map +1 -0
  68. package/dist/deduplication/deduplication-prompts.d.ts +8 -0
  69. package/dist/deduplication/deduplication-prompts.d.ts.map +1 -0
  70. package/dist/deduplication/deduplication-prompts.js +35 -0
  71. package/dist/deduplication/deduplication-prompts.js.map +1 -0
  72. package/dist/deduplication/deduplication-reviewer.d.ts +46 -0
  73. package/dist/deduplication/deduplication-reviewer.d.ts.map +1 -0
  74. package/dist/deduplication/deduplication-reviewer.js +105 -0
  75. package/dist/deduplication/deduplication-reviewer.js.map +1 -0
  76. package/dist/deduplication/deduplication.d.ts +6 -0
  77. package/dist/deduplication/deduplication.d.ts.map +1 -0
  78. package/dist/deduplication/deduplication.js +85 -0
  79. package/dist/deduplication/deduplication.js.map +1 -0
  80. package/dist/deduplication/scan.d.ts +16 -0
  81. package/dist/deduplication/scan.d.ts.map +1 -0
  82. package/dist/deduplication/scan.js +101 -0
  83. package/dist/deduplication/scan.js.map +1 -0
  84. package/dist/deep-progress.d.ts +21 -0
  85. package/dist/deep-progress.d.ts.map +1 -0
  86. package/dist/deep-progress.js +108 -0
  87. package/dist/deep-progress.js.map +1 -0
  88. package/dist/finding-dedupe-groups.d.ts +7 -0
  89. package/dist/finding-dedupe-groups.d.ts.map +1 -0
  90. package/dist/finding-dedupe-groups.js +2 -0
  91. package/dist/finding-dedupe-groups.js.map +1 -0
  92. package/dist/finding-retrieval.d.ts +13 -0
  93. package/dist/finding-retrieval.d.ts.map +1 -0
  94. package/dist/finding-retrieval.js +2 -0
  95. package/dist/finding-retrieval.js.map +1 -0
  96. package/dist/finding-workflow.d.ts +53 -0
  97. package/dist/finding-workflow.d.ts.map +1 -0
  98. package/dist/finding-workflow.js +117 -0
  99. package/dist/finding-workflow.js.map +1 -0
  100. package/dist/findings-client.d.ts +15 -0
  101. package/dist/findings-client.d.ts.map +1 -0
  102. package/dist/findings-client.js +60 -0
  103. package/dist/findings-client.js.map +1 -0
  104. package/dist/index.d.ts +5 -0
  105. package/dist/index.d.ts.map +1 -1
  106. package/dist/index.js +2 -0
  107. package/dist/index.js.map +1 -1
  108. package/dist/publication.js +28 -40
  109. package/dist/publication.js.map +1 -1
  110. package/dist/runtime.d.ts +5 -1
  111. package/dist/runtime.d.ts.map +1 -1
  112. package/dist/runtime.js +119 -13
  113. package/dist/runtime.js.map +1 -1
  114. package/dist/saved-scan.d.ts +15 -0
  115. package/dist/saved-scan.d.ts.map +1 -0
  116. package/dist/saved-scan.js +68 -0
  117. package/dist/saved-scan.js.map +1 -0
  118. package/dist/scan-activity.d.ts.map +1 -1
  119. package/dist/scan-activity.js +12 -2
  120. package/dist/scan-activity.js.map +1 -1
  121. package/dist/scan-comparison.d.ts +3 -2
  122. package/dist/scan-comparison.d.ts.map +1 -1
  123. package/dist/scan-comparison.js +1 -1
  124. package/dist/scan-comparison.js.map +1 -1
  125. package/dist/server/dashboard/THIRD_PARTY_NOTICES.txt +276 -0
  126. package/dist/server/dashboard/app.css +2 -0
  127. package/dist/server/dashboard/app.js +66 -0
  128. package/dist/server/dashboard/index.html +18 -0
  129. package/dist/server/dashboard-types.d.ts +45 -0
  130. package/dist/server/dashboard-types.d.ts.map +1 -0
  131. package/dist/server/dashboard-types.js +2 -0
  132. package/dist/server/dashboard-types.js.map +1 -0
  133. package/dist/server/dashboard.d.ts +6 -0
  134. package/dist/server/dashboard.d.ts.map +1 -0
  135. package/dist/server/dashboard.js +45 -0
  136. package/dist/server/dashboard.js.map +1 -0
  137. package/dist/server/embeddings.d.ts +16 -0
  138. package/dist/server/embeddings.d.ts.map +1 -0
  139. package/dist/server/embeddings.js +114 -0
  140. package/dist/server/embeddings.js.map +1 -0
  141. package/dist/server/errors.d.ts +5 -0
  142. package/dist/server/errors.d.ts.map +1 -0
  143. package/dist/server/errors.js +8 -0
  144. package/dist/server/errors.js.map +1 -0
  145. package/dist/server/findings-service.d.ts +20 -0
  146. package/dist/server/findings-service.d.ts.map +1 -0
  147. package/dist/server/findings-service.js +31 -0
  148. package/dist/server/findings-service.js.map +1 -0
  149. package/dist/server/index.d.ts +2 -0
  150. package/dist/server/index.d.ts.map +1 -0
  151. package/dist/server/index.js +6 -0
  152. package/dist/server/index.js.map +1 -0
  153. package/dist/server/routes.d.ts +6 -0
  154. package/dist/server/routes.d.ts.map +1 -0
  155. package/dist/server/routes.js +86 -0
  156. package/dist/server/routes.js.map +1 -0
  157. package/dist/server/serve.d.ts +2 -0
  158. package/dist/server/serve.d.ts.map +1 -0
  159. package/dist/server/serve.js +28 -0
  160. package/dist/server/serve.js.map +1 -0
  161. package/dist/server/server.d.ts +10 -0
  162. package/dist/server/server.d.ts.map +1 -0
  163. package/dist/server/server.js +17 -0
  164. package/dist/server/server.js.map +1 -0
  165. package/dist/server/sqlite-store.d.ts +22 -0
  166. package/dist/server/sqlite-store.d.ts.map +1 -0
  167. package/dist/server/sqlite-store.js +82 -0
  168. package/dist/server/sqlite-store.js.map +1 -0
  169. package/dist/server/storage.d.ts +32 -0
  170. package/dist/server/storage.d.ts.map +1 -0
  171. package/dist/server/storage.js +2 -0
  172. package/dist/server/storage.js.map +1 -0
  173. package/dist/server/validation.d.ts +17 -0
  174. package/dist/server/validation.d.ts.map +1 -0
  175. package/dist/server/validation.js +53 -0
  176. package/dist/server/validation.js.map +1 -0
  177. package/dist/trusted-executable.d.ts.map +1 -1
  178. package/dist/trusted-executable.js +2 -1
  179. package/dist/trusted-executable.js.map +1 -1
  180. package/dist/version.d.ts +1 -1
  181. package/dist/version.js +1 -1
  182. package/dist/version.js.map +1 -1
  183. package/package.json +17 -5
@@ -333,13 +333,13 @@ def merged_root_cause(value: dict[str, Any]) -> tuple[str | None, Any]:
333
333
  "language",
334
334
  ):
335
335
  continue
336
- if field in text_fields and (
337
- not isinstance(item, str) or not item.strip()
338
- ):
336
+ if field in text_fields and (not isinstance(item, str) or not item.strip()):
339
337
  continue
340
338
  current = merged.get(field)
341
- if field not in merged or current in (None, "", [], {}) or (
342
- isinstance(current, str) and not current.strip()
339
+ if (
340
+ field not in merged
341
+ or current in (None, "", [], {})
342
+ or (isinstance(current, str) and not current.strip())
343
343
  ):
344
344
  merged[field] = item
345
345
 
@@ -363,9 +363,7 @@ def merged_root_cause(value: dict[str, Any]) -> tuple[str | None, Any]:
363
363
  item
364
364
  for detail in details
365
365
  for field in ("codeEvidence", "code_evidence")
366
- for item in (
367
- detail.get(field, []) if isinstance(detail.get(field), list) else []
368
- )
366
+ for item in (detail.get(field, []) if isinstance(detail.get(field), list) else [])
369
367
  ]
370
368
  if embedded_evidence:
371
369
  merged["codeEvidence"] = embedded_evidence
@@ -541,9 +539,7 @@ def _finding_section(number: int, finding: dict[str, Any]) -> list[str]:
541
539
  (
542
540
  section[key]
543
541
  for section in dataflow_sections
544
- if key in section
545
- and isinstance(section[key], str)
546
- and section[key].strip()
542
+ if key in section and isinstance(section[key], str) and section[key].strip()
547
543
  ),
548
544
  None,
549
545
  )
@@ -652,9 +648,7 @@ def _finding_section(number: int, finding: dict[str, Any]) -> list[str]:
652
648
  if validation.get("method"):
653
649
  lines.extend(["", f"Validation method: {_text(validation['method'], 'not recorded')}"])
654
650
  if validation_outcomes:
655
- lines.extend(
656
- ["", *(f"- **{label}:** {value}" for label, value in validation_outcomes)]
657
- )
651
+ lines.extend(["", *(f"- **{label}:** {value}" for label, value in validation_outcomes)])
658
652
  lines.extend(_code_evidence_lines(validation_code_evidence))
659
653
  if validation_assertions:
660
654
  lines.extend(["", "Assertions:", *_bullets(validation_assertions, "None recorded.")])
@@ -64,9 +64,7 @@ def validate_contract(scan_dir: Path) -> dict[str, Any]:
64
64
  FINALIZER._validate_derived_finding_identities(manifest, findings)
65
65
  FINALIZER._validate_coverage(manifest, coverage, scan_dir)
66
66
  FINALIZER._validate_sealed_coverage_receipts(scan, coverage)
67
- FINALIZER.validate_against_schema(
68
- findings_for_validation, schema_dir / "findings.schema.json"
69
- )
67
+ FINALIZER.validate_against_schema(findings_for_validation, schema_dir / "findings.schema.json")
70
68
  FINALIZER.validate_against_schema(coverage, schema_dir / "coverage.schema.json")
71
69
  FINALIZER._require_scan_local_file(scan_dir, "report.md", "report.md")
72
70
  return {
@@ -11,8 +11,8 @@ already-validated ancestor from being renamed or replaced during a read,
11
11
  write, or delete. Writes rename the exact temporary-file handle into place so
12
12
  an attacker cannot substitute another file at the temporary name.
13
13
 
14
- Importing this module is safe on non-Windows hosts. Its public operations
15
- raise ``WindowsScanLocalFileError`` when called anywhere other than Windows.
14
+ Importing this module and comparing streams work on every platform. Filesystem
15
+ operations raise ``WindowsScanLocalFileError`` outside Windows.
16
16
  """
17
17
 
18
18
  from __future__ import annotations
@@ -28,6 +28,7 @@ import secrets
28
28
  from collections.abc import Iterator
29
29
  from ctypes import wintypes
30
30
  from pathlib import Path, PurePosixPath
31
+ from typing import BinaryIO
31
32
 
32
33
  _msvcrt = importlib.import_module("msvcrt") if os.name == "nt" else None
33
34
 
@@ -63,11 +64,15 @@ _FILE_TYPE_DISK = 0x0001
63
64
  _FILE_NAME_OPENED = 0x00000008
64
65
  _ERROR_FILE_NOT_FOUND = 2
65
66
  _ERROR_PATH_NOT_FOUND = 3
67
+ _ERROR_ACCESS_DENIED = 5
68
+ _ERROR_SHARING_VIOLATION = 32
69
+ _ERROR_LOCK_VIOLATION = 33
66
70
  _ERROR_FILE_EXISTS = 80
67
71
  _ERROR_ALREADY_EXISTS = 183
68
72
  _MISSING_ERRORS = {_ERROR_FILE_NOT_FOUND, _ERROR_PATH_NOT_FOUND}
69
73
  _COLLISION_ERRORS = {_ERROR_FILE_EXISTS, _ERROR_ALREADY_EXISTS}
70
74
  _INVALID_HANDLE_VALUE = ctypes.c_void_p(-1).value
75
+ _MAX_COMPARE_CHUNK = 64 * 1024
71
76
  _MAX_WRITE_CHUNK = 1024 * 1024
72
77
 
73
78
  _INVALID_COMPONENT_CHARACTERS = frozenset('<>:"|?*')
@@ -398,12 +403,15 @@ def _locked_parent(
398
403
  relative_path: str,
399
404
  *,
400
405
  create: bool,
406
+ expected_root_identity: tuple[int, int] | None = None,
401
407
  ) -> Iterator[tuple[Path, str]]:
402
408
  """Hold non-deletable handles for every directory in the absolute target path."""
403
409
 
404
410
  _require_windows()
405
411
  parts = _validated_parts(relative_path)
406
- root_path, expected_root_identity = _canonical_scan_directory(scan_dir)
412
+ root_path, observed_root_identity = _canonical_scan_directory(scan_dir)
413
+ if expected_root_identity is not None and observed_root_identity != expected_root_identity:
414
+ raise _invalid_path(scan_dir, "scan directory changed after artifact restoration setup")
407
415
  handles: list[_OwnedHandle] = []
408
416
  try:
409
417
  # Absolute-path Win32 calls remain safe only while every ancestor is
@@ -414,7 +422,10 @@ def _locked_parent(
414
422
  assert directory_handle is not None
415
423
  handles.append(directory_handle)
416
424
  current_root = root_path.lstat()
417
- if (current_root.st_dev, current_root.st_ino) != expected_root_identity:
425
+ current_root_identity = (current_root.st_dev, current_root.st_ino)
426
+ if current_root_identity != observed_root_identity or (
427
+ expected_root_identity is not None and current_root_identity != expected_root_identity
428
+ ):
418
429
  raise _invalid_path(scan_dir, "scan directory changed while it was being opened")
419
430
  current_path = root_path
420
431
  for component in parts[:-1]:
@@ -431,6 +442,14 @@ def _locked_parent(
431
442
  handle.close()
432
443
 
433
444
 
445
+ def scan_root_identity(scan_dir: Path) -> tuple[Path, tuple[int, int]]:
446
+ """Return a canonical scan root and its identity while holding it fixed."""
447
+
448
+ with _locked_parent(scan_dir, ".identity", create=False) as (root_path, _):
449
+ metadata = root_path.lstat()
450
+ return root_path, (metadata.st_dev, metadata.st_ino)
451
+
452
+
434
453
  def open_read_fd(scan_dir: Path, relative_path: str, context: str) -> int:
435
454
  """Open a verified regular file and return an owned binary read descriptor."""
436
455
 
@@ -465,6 +484,16 @@ def open_read_fd(scan_dir: Path, relative_path: str, context: str) -> int:
465
484
  ) from exc
466
485
 
467
486
 
487
+ def stream_matches_payload(stream: BinaryIO, payload: bytes) -> bool:
488
+ offset = 0
489
+ while offset < len(payload):
490
+ chunk = stream.read(min(_MAX_COMPARE_CHUNK, len(payload) - offset))
491
+ if not chunk or chunk != payload[offset : offset + len(chunk)]:
492
+ return False
493
+ offset += len(chunk)
494
+ return stream.read(1) == b""
495
+
496
+
468
497
  def _write_all(handle: int, payload: bytes) -> None:
469
498
  view = memoryview(payload)
470
499
  offset = 0
@@ -532,12 +561,67 @@ def _validate_existing_output(path: Path) -> None:
532
561
  _verify_regular_file(handle.value, path)
533
562
 
534
563
 
535
- def atomic_write(scan_dir: Path, relative_path: str, payload: bytes) -> None:
564
+ def _existing_output_matches(path: Path, payload: bytes) -> bool:
565
+ try:
566
+ handle = _create_file(
567
+ path,
568
+ access=_GENERIC_READ | _FILE_READ_ATTRIBUTES,
569
+ # Deny write and delete sharing while comparing the opened contents.
570
+ share=_FILE_SHARE_READ,
571
+ disposition=_OPEN_EXISTING,
572
+ flags=_FILE_FLAG_OPEN_REPARSE_POINT | _FILE_FLAG_BACKUP_SEMANTICS,
573
+ missing_ok=True,
574
+ )
575
+ except WindowsScanLocalFileError as exc:
576
+ if exc.errno in {
577
+ _ERROR_ACCESS_DENIED,
578
+ _ERROR_SHARING_VIOLATION,
579
+ _ERROR_LOCK_VIOLATION,
580
+ }:
581
+ return False
582
+ raise
583
+ if handle is None:
584
+ return False
585
+ with handle:
586
+ assert handle.value is not None
587
+ _verify_regular_file(handle.value, path)
588
+ raw_handle = handle.detach()
589
+ try:
590
+ assert _msvcrt is not None
591
+ descriptor = _msvcrt.open_osfhandle(raw_handle, os.O_RDONLY | os.O_BINARY)
592
+ except BaseException:
593
+ _close_handle(raw_handle)
594
+ raise
595
+ try:
596
+ with os.fdopen(descriptor, "rb") as stream:
597
+ if os.fstat(stream.fileno()).st_size != len(payload):
598
+ return False
599
+ return stream_matches_payload(stream, payload)
600
+ except OSError:
601
+ return False
602
+
603
+
604
+ def atomic_write(
605
+ scan_dir: Path,
606
+ relative_path: str,
607
+ payload: bytes,
608
+ *,
609
+ expected_root_identity: tuple[int, int] | None = None,
610
+ ) -> None:
536
611
  """Atomically replace a scan-local regular file with ``payload``."""
537
612
 
538
- with _locked_parent(scan_dir, relative_path, create=True) as (parent_path, leaf_name):
613
+ with _locked_parent(
614
+ scan_dir,
615
+ relative_path,
616
+ create=True,
617
+ expected_root_identity=expected_root_identity,
618
+ ) as (parent_path, leaf_name):
539
619
  destination_path = parent_path / leaf_name
540
620
  _validate_existing_output(destination_path)
621
+ if expected_root_identity is not None and _existing_output_matches(
622
+ destination_path, payload
623
+ ):
624
+ return
541
625
 
542
626
  temp_handle: _OwnedHandle | None = None
543
627
  temp_path: Path | None = None
@@ -247,6 +247,12 @@ def parse_args(description: str) -> argparse.Namespace:
247
247
  preserve_scan.add_argument("--claim-token")
248
248
  preserve_scan.add_argument("--coordinator-generation", type=positive_int)
249
249
 
250
+ recovery_help = "Validate and republish retained checkpoints for a failed, non-canceled scan."
251
+ recover_scan = subparsers.add_parser(
252
+ "recover-scan-results", help=recovery_help, description=recovery_help
253
+ )
254
+ recover_scan.add_argument("--scan-id", required=True, help="ID of the stopped scan to recover.")
255
+
250
256
  write_scan_draft = subparsers.add_parser("write-scan-draft")
251
257
  write_scan_draft.add_argument("--scan-id", required=True)
252
258
  write_scan_draft.add_argument("--draft-path", required=True)
@@ -339,7 +345,27 @@ def parse_args(description: str) -> argparse.Namespace:
339
345
  publication.add_argument("--input-file", required=True)
340
346
 
341
347
  subparsers.add_parser("database-info")
342
- return parser.parse_args()
348
+ subparsers.add_parser("dashboard")
349
+ subparsers.add_parser("finding-workflow")
350
+ subparsers.add_parser("store-findings")
351
+ subparsers.add_parser("store-dedupe-groups")
352
+ dedupe_groups = subparsers.add_parser("list-dedupe-groups")
353
+ dedupe_groups.add_argument("--finding-id", required=True)
354
+ potential_duplicates = subparsers.add_parser("find-potential-duplicates")
355
+ potential_duplicates.add_argument("--finding-id", required=True)
356
+ scope = potential_duplicates.add_mutually_exclusive_group(required=True)
357
+ scope.add_argument("--repository-id")
358
+ scope.add_argument("--all-repositories", action="store_true")
359
+ stored_findings = subparsers.add_parser("list-stored-findings")
360
+ stored_findings.add_argument("--limit", type=positive_int, required=True)
361
+ stored_findings.add_argument("--offset", type=non_negative_int, required=True)
362
+ arguments = sys.argv[1:]
363
+ if "--user-context-stdin" in arguments:
364
+ if arguments.count("--user-context-stdin") != 1 or "--user-context" in arguments:
365
+ parser.error("pass exactly one user-context transport")
366
+ index = arguments.index("--user-context-stdin")
367
+ arguments[index] = "--user-context=" + sys.stdin.buffer.read().decode("utf-8")
368
+ return parser.parse_args(arguments)
343
369
 
344
370
 
345
371
  def non_negative_int(value: str) -> int:
@@ -0,0 +1,138 @@
1
+ """Read-only dashboard projections for stored findings and duplicate groups."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import json
7
+ import sqlite3
8
+ import sys
9
+ from pathlib import Path
10
+ from typing import Any
11
+
12
+ sys.path.insert(0, str(Path(__file__).resolve().parent))
13
+ from workbench_findings import list_dedupe_groups
14
+
15
+ FINDING_RECORDS = """
16
+ SELECT findings.id, json_extract(details_json, '$.title') AS title,
17
+ COALESCE(repositories.ids, '[]') AS repositoryIds,
18
+ json_extract(details_json, '$.severity.level') AS severity,
19
+ findings.created_at AS createdAt, findings.updated_at AS updatedAt
20
+ FROM findings LEFT JOIN (
21
+ SELECT finding_id, json_group_array(repository_id) AS ids
22
+ FROM finding_repositories GROUP BY finding_id
23
+ ) AS repositories ON repositories.finding_id = findings.id
24
+ WHERE details_json IS NOT NULL
25
+ """
26
+
27
+ GROUP_RECORDS = """
28
+ SELECT groups.id, groups.id AS title,
29
+ (SELECT json_group_array(DISTINCT repository_id)
30
+ FROM finding_dedupe_group_members AS members
31
+ JOIN finding_repositories ON finding_repositories.finding_id = members.finding_id
32
+ WHERE members.group_id = groups.id) AS repositoryIds,
33
+ groups.created_at AS createdAt, groups.created_at AS updatedAt,
34
+ (SELECT COUNT(*) FROM finding_dedupe_group_members WHERE group_id = groups.id) AS memberCount
35
+ FROM finding_dedupe_groups AS groups
36
+ """
37
+
38
+ RECORDS = {
39
+ "findings": FINDING_RECORDS,
40
+ "groups": GROUP_RECORDS,
41
+ }
42
+
43
+
44
+ def item(row: sqlite3.Row) -> dict[str, Any]:
45
+ result = dict(row)
46
+ result["repositoryIds"] = json.loads(result["repositoryIds"])
47
+ return result
48
+
49
+
50
+ def detail(connection: sqlite3.Connection, view: str, selected: dict[str, Any]) -> dict[str, Any]:
51
+ result: dict[str, Any] = {"item": selected}
52
+ selected_id = selected["id"]
53
+ if view == "findings":
54
+ result["finding"] = json.loads(
55
+ connection.execute(
56
+ "SELECT details_json FROM findings WHERE id = ?",
57
+ (selected_id,),
58
+ ).fetchone()[0]
59
+ )
60
+ result["groups"] = list_dedupe_groups(connection, selected_id)["groups"]
61
+ else:
62
+ result["group"] = {
63
+ "groupId": selected_id,
64
+ "createdAt": selected["createdAt"],
65
+ "findingIds": [
66
+ r[0]
67
+ for r in connection.execute(
68
+ "SELECT finding_id FROM finding_dedupe_group_members WHERE group_id = ? ORDER BY finding_id",
69
+ (selected_id,),
70
+ )
71
+ ],
72
+ }
73
+ return result
74
+
75
+
76
+ def dashboard(connection: sqlite3.Connection, query: dict[str, Any]) -> dict[str, Any]:
77
+ """One snapshot, no artifact reads, model calls, or writes."""
78
+ view = query["view"]
79
+ records = RECORDS[view]
80
+ clauses: list[str] = []
81
+ values: list[Any] = []
82
+ if query.get("query"):
83
+ connection.create_function("casefold", 1, str.casefold, deterministic=True)
84
+ columns = ["id", "title", "repositoryIds"]
85
+ clauses.append(
86
+ "("
87
+ + " OR ".join(f"instr(casefold(COALESCE({c}, '')), casefold(?)) > 0" for c in columns)
88
+ + ")"
89
+ )
90
+ values.extend([query["query"]] * len(columns))
91
+ if query.get("repository"):
92
+ clauses.append("EXISTS (SELECT 1 FROM json_each(repositoryIds) WHERE value = ?)")
93
+ values.append(query["repository"])
94
+ where = " WHERE " + " AND ".join(clauses) if clauses else ""
95
+ order = "createdAt DESC, id" if query["sort"] == "newest" else "updatedAt DESC, id"
96
+ connection.execute("BEGIN")
97
+ with connection:
98
+ repositories = connection.execute("""
99
+ SELECT DISTINCT repository_id AS id, repository_id AS label
100
+ FROM finding_repositories ORDER BY repository_id
101
+ """).fetchall()
102
+ total = connection.execute(f"SELECT COUNT(*) FROM ({records}) {where}", values).fetchone()[
103
+ 0
104
+ ]
105
+ rows = connection.execute(
106
+ f"SELECT * FROM ({records}) {where} ORDER BY {order} LIMIT ? OFFSET ?",
107
+ (*values, query["limit"], query["offset"]),
108
+ ).fetchall()
109
+ selected = (
110
+ connection.execute(
111
+ f"SELECT * FROM ({records}) WHERE id = ?",
112
+ (query["id"],),
113
+ ).fetchone()
114
+ if query.get("id")
115
+ else None
116
+ )
117
+ next_offset = query["offset"] + len(rows)
118
+ return {
119
+ "overview": {
120
+ "findings": connection.execute(
121
+ "SELECT COUNT(*) FROM findings WHERE details_json IS NOT NULL"
122
+ ).fetchone()[0],
123
+ "groups": connection.execute(
124
+ "SELECT COUNT(*) FROM finding_dedupe_groups"
125
+ ).fetchone()[0],
126
+ },
127
+ "repositories": [dict(row) for row in repositories],
128
+ "items": [item(row) for row in rows],
129
+ "total": total,
130
+ "limit": query["limit"],
131
+ "offset": query["offset"],
132
+ "nextOffset": next_offset if next_offset < total else None,
133
+ "detail": detail(connection, view, item(selected)) if selected is not None else None,
134
+ }
135
+
136
+
137
+ if __name__ == "__main__":
138
+ argparse.ArgumentParser(description=__doc__).parse_args()