@openai/codex-security 0.1.9 → 0.1.11

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 (87) hide show
  1. package/README.md +43 -18
  2. package/_bundled_plugin/.codex-plugin/plugin.json +1 -1
  3. package/_bundled_plugin/.mcp.json +3 -1
  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/preflight/capability-profiles.toml +1 -1
  7. package/_bundled_plugin/references/scan-contract.md +1 -1
  8. package/_bundled_plugin/scripts/deep_scan_config.py +21 -1
  9. package/_bundled_plugin/scripts/deep_scan_workbench.py +66 -14
  10. package/_bundled_plugin/scripts/finalize_scan_contract.py +15 -207
  11. package/_bundled_plugin/scripts/generate_in_scope_files.py +127 -2
  12. package/_bundled_plugin/scripts/generate_rank_input.py +38 -5
  13. package/_bundled_plugin/scripts/normalize_candidates.py +20 -2
  14. package/_bundled_plugin/scripts/rank_preview.py +4 -0
  15. package/_bundled_plugin/scripts/report_projection.py +39 -1
  16. package/_bundled_plugin/scripts/workbench_cli.py +9 -0
  17. package/_bundled_plugin/scripts/workbench_db.py +295 -4
  18. package/_bundled_plugin/scripts/workbench_native_indexes.py +100 -43
  19. package/_bundled_plugin/scripts/workbench_schema.py +15 -0
  20. package/_bundled_plugin/scripts/workbench_source_excerpt.py +1 -5
  21. package/_bundled_plugin/scripts/workbench_target.py +82 -0
  22. package/_bundled_plugin/skills/deep-security-scan/SKILL.md +5 -3
  23. package/_bundled_plugin/skills/finding-discovery/SKILL.md +8 -4
  24. package/_bundled_plugin/skills/security-diff-scan/SKILL.md +21 -140
  25. package/dist/api.d.ts +43 -3
  26. package/dist/api.d.ts.map +1 -1
  27. package/dist/api.js +319 -135
  28. package/dist/api.js.map +1 -1
  29. package/dist/auth.d.ts +2 -3
  30. package/dist/auth.d.ts.map +1 -1
  31. package/dist/auth.js +59 -270
  32. package/dist/auth.js.map +1 -1
  33. package/dist/cli.d.ts +1 -1
  34. package/dist/cli.d.ts.map +1 -1
  35. package/dist/cli.js +107 -43
  36. package/dist/cli.js.map +1 -1
  37. package/dist/contract.js +7 -5
  38. package/dist/contract.js.map +1 -1
  39. package/dist/cost.d.ts +2 -0
  40. package/dist/cost.d.ts.map +1 -1
  41. package/dist/cost.js +24 -2
  42. package/dist/cost.js.map +1 -1
  43. package/dist/errors.d.ts +4 -2
  44. package/dist/errors.d.ts.map +1 -1
  45. package/dist/errors.js +10 -46
  46. package/dist/errors.js.map +1 -1
  47. package/dist/index.d.ts +2 -2
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +1 -1
  50. package/dist/index.js.map +1 -1
  51. package/dist/knowledge-base.d.ts.map +1 -1
  52. package/dist/knowledge-base.js +2 -1
  53. package/dist/knowledge-base.js.map +1 -1
  54. package/dist/multiscan.d.ts.map +1 -1
  55. package/dist/multiscan.js +3 -2
  56. package/dist/multiscan.js.map +1 -1
  57. package/dist/result.d.ts +13 -1
  58. package/dist/result.d.ts.map +1 -1
  59. package/dist/result.js +3 -0
  60. package/dist/result.js.map +1 -1
  61. package/dist/runtime.d.ts +9 -6
  62. package/dist/runtime.d.ts.map +1 -1
  63. package/dist/runtime.js +130 -287
  64. package/dist/runtime.js.map +1 -1
  65. package/dist/scan-comparison.d.ts +14 -0
  66. package/dist/scan-comparison.d.ts.map +1 -1
  67. package/dist/scan-comparison.js +90 -1
  68. package/dist/scan-comparison.js.map +1 -1
  69. package/dist/scan-dashboard.d.ts.map +1 -1
  70. package/dist/scan-dashboard.js +1 -0
  71. package/dist/scan-dashboard.js.map +1 -1
  72. package/dist/scan-history-renderer.d.ts +1 -1
  73. package/dist/scan-history-renderer.d.ts.map +1 -1
  74. package/dist/scan-history-renderer.js +10 -0
  75. package/dist/scan-history-renderer.js.map +1 -1
  76. package/dist/scan-logs.d.ts +17 -0
  77. package/dist/scan-logs.d.ts.map +1 -0
  78. package/dist/scan-logs.js +90 -0
  79. package/dist/scan-logs.js.map +1 -0
  80. package/dist/targets.d.ts +1 -0
  81. package/dist/targets.d.ts.map +1 -1
  82. package/dist/targets.js +22 -0
  83. package/dist/targets.js.map +1 -1
  84. package/dist/version.d.ts +1 -1
  85. package/dist/version.js +1 -1
  86. package/dist/version.js.map +1 -1
  87. package/package.json +2 -2
@@ -147,6 +147,10 @@ def parse_args(description: str) -> argparse.Namespace:
147
147
  register_cli_scan.add_argument("--archive-existing", action="store_true")
148
148
  register_cli_scan.add_argument("--archived-scan-dir")
149
149
 
150
+ set_scan_thread = subparsers.add_parser("set-scan-thread")
151
+ set_scan_thread.add_argument("--scan-id", required=True)
152
+ set_scan_thread.add_argument("--thread-id", required=True)
153
+
150
154
  get_scan_recipe = subparsers.add_parser("get-scan-recipe")
151
155
  get_scan_recipe.add_argument("--scan-id", required=True)
152
156
 
@@ -209,6 +213,11 @@ def parse_args(description: str) -> argparse.Namespace:
209
213
  complete_scan.add_argument("--cost-json")
210
214
  complete_scan.add_argument("--thread-id")
211
215
 
216
+ complete_budget_exhausted_scan = subparsers.add_parser("complete-budget-exhausted-scan")
217
+ complete_budget_exhausted_scan.add_argument("--scan-id", required=True)
218
+ complete_budget_exhausted_scan.add_argument("--cost-json", required=True)
219
+ complete_budget_exhausted_scan.add_argument("--message")
220
+
212
221
  cancel_scan = subparsers.add_parser("cancel-scan")
213
222
  cancel_scan.add_argument("--scan-id", required=True)
214
223
  cancel_scan.add_argument("--thread-id")
@@ -1136,6 +1136,285 @@ def complete_scan(
1136
1136
  )
1137
1137
 
1138
1138
 
1139
+ def complete_budget_exhausted_scan(
1140
+ connection: sqlite3.Connection, args: argparse.Namespace
1141
+ ) -> dict[str, Any]:
1142
+ scan_id = require_uuid(args.scan_id, "scan-id")
1143
+ cost_json = parse_scan_cost(args.cost_json)
1144
+ if cost_json is None:
1145
+ raise SystemExit("Budget-exhausted scan completion requires the measured scan cost.")
1146
+ with scan_completion_lock(scan_id):
1147
+ scan = require_scan(connection, scan_id)
1148
+ if scan["status"] != "running" or scan["mode"] != "deep" or scan["recipe_json"] is None:
1149
+ raise SystemExit("Only a running CLI Deep Scan can complete after its cost limit.")
1150
+ recipe = json.loads(scan["recipe_json"], parse_constant=reject_non_finite_json)
1151
+ if not isinstance(recipe, dict) or recipe.get("mode") != "deep":
1152
+ raise SystemExit("Budget-exhausted scan completion requires a Deep Scan launch recipe.")
1153
+ cost = json.loads(cost_json)
1154
+ measured = cost.get("cost", cost)
1155
+ limit = recipe.get("maxCostUsd")
1156
+ if (
1157
+ not isinstance(limit, (int, float))
1158
+ or isinstance(limit, bool)
1159
+ or not isinstance(measured, dict)
1160
+ or measured.get("estimatedUsd", 0) <= limit
1161
+ ):
1162
+ raise SystemExit("Deep Scan has not exceeded its configured cost limit.")
1163
+ run = connection.execute(
1164
+ "SELECT status, terminal_reason, manifest_path FROM deep_scan_runs WHERE scan_id = ?",
1165
+ (scan_id,),
1166
+ ).fetchone()
1167
+ if (
1168
+ run is None
1169
+ or run["status"] != "succeeded"
1170
+ or run["terminal_reason"] not in {"saturated", "capped"}
1171
+ or not run["manifest_path"]
1172
+ ):
1173
+ raise SystemExit(
1174
+ "Budget-exhausted scan completion requires successfully completed Deep Scan "
1175
+ "discovery."
1176
+ )
1177
+ scan_dir = require_canonical_scan_directory(Path(scan["scan_dir"]))
1178
+ candidates = budget_exhausted_candidates(scan, scan_dir)
1179
+ warning = optional_text(args.message, maximum=2400)
1180
+ if warning is None:
1181
+ warning = (
1182
+ f"Deep Scan reached its cost limit after an estimated "
1183
+ f"${measured['estimatedUsd']:.6g}; completed discovery was preserved."
1184
+ )
1185
+ budget_exhausted_draft(scan, scan_dir, candidates, warning)
1186
+ warnings = json.loads(scan["completion_warnings_json"])
1187
+ if warning not in warnings:
1188
+ connection.execute(
1189
+ "UPDATE scans SET completion_warnings_json = ? WHERE id = ? AND status = 'running'",
1190
+ (json.dumps([*warnings, warning]), scan_id),
1191
+ )
1192
+ connection.commit()
1193
+ return complete_scan_locked(connection, scan_id, None, cost_json)
1194
+
1195
+
1196
+ def budget_exhausted_candidates(scan: sqlite3.Row, scan_dir: Path) -> list[dict[str, Any]]:
1197
+ artifacts = deep_scan.canonical_discovery_artifacts(scan)
1198
+ ledger = Path(artifacts["candidateLedgerPath"])
1199
+ try:
1200
+ inventory = Path(artifacts["inScopeFilesPath"])
1201
+ inventory_descriptor = open_scan_local_file_descriptor(
1202
+ scan_dir,
1203
+ inventory.relative_to(scan_dir).as_posix(),
1204
+ "Canonical Deep Scan in-scope inventory",
1205
+ )
1206
+ with os.fdopen(inventory_descriptor, "rb") as source:
1207
+ lines = re.split(r"\r?\n", source.read().decode("utf-8"))
1208
+ in_scope = {re.sub(r"^(?:\./)+", "", line) for line in lines if line}
1209
+ descriptor = open_scan_local_file_descriptor(
1210
+ scan_dir,
1211
+ ledger.relative_to(scan_dir).as_posix(),
1212
+ "Canonical Deep Scan candidate ledger",
1213
+ )
1214
+ with os.fdopen(descriptor, "r", encoding="utf-8") as source:
1215
+ candidates = [
1216
+ json.loads(line, parse_constant=reject_non_finite_json)
1217
+ for line in source
1218
+ if line.strip()
1219
+ ]
1220
+ except (ContractError, OSError, UnicodeError, ValueError) as exc:
1221
+ raise SystemExit(f"Canonical Deep Scan candidate ledger is invalid: {exc}") from exc
1222
+
1223
+ candidate_ids: set[str] = set()
1224
+ for candidate in candidates:
1225
+ if not isinstance(candidate, dict):
1226
+ raise SystemExit("Canonical Deep Scan candidate ledger rows must be objects.")
1227
+ candidate_id = candidate.get("candidate_id")
1228
+ locations = candidate.get("locations")
1229
+ if (
1230
+ not isinstance(candidate_id, str)
1231
+ or not candidate_id.strip()
1232
+ or candidate_id in {".", ".."}
1233
+ or "/" in candidate_id
1234
+ or "\\" in candidate_id
1235
+ or candidate_id in candidate_ids
1236
+ or not isinstance(candidate.get("summary"), str)
1237
+ or not candidate["summary"].strip()
1238
+ or not isinstance(candidate.get("evidence"), str)
1239
+ or not candidate["evidence"].strip()
1240
+ or not isinstance(locations, list)
1241
+ or not locations
1242
+ ):
1243
+ raise SystemExit("Canonical Deep Scan candidate ledger contains an invalid candidate.")
1244
+ candidate_ids.add(candidate_id)
1245
+ for location in locations:
1246
+ if not isinstance(location, dict):
1247
+ raise SystemExit("Canonical Deep Scan candidate location must be an object.")
1248
+ path = location.get("path")
1249
+ if (
1250
+ not isinstance(path, str)
1251
+ or not path
1252
+ or "\\" in path
1253
+ or "\x00" in path
1254
+ or re.match(r"^[A-Za-z]:", path)
1255
+ or PurePosixPath(path).is_absolute()
1256
+ or any(part in {".", "..", ""} for part in path.split("/"))
1257
+ ):
1258
+ raise SystemExit(
1259
+ "Canonical Deep Scan candidate location must be repository-relative."
1260
+ )
1261
+ if not any(location["path"] in in_scope for location in locations):
1262
+ raise SystemExit(
1263
+ "Canonical Deep Scan candidate must include a location in its in-scope inventory."
1264
+ )
1265
+ return candidates
1266
+
1267
+
1268
+ def budget_exhausted_draft(
1269
+ scan: sqlite3.Row,
1270
+ scan_dir: Path,
1271
+ candidates: list[dict[str, Any]],
1272
+ warning: str,
1273
+ ) -> None:
1274
+ documents: dict[str, dict[str, Any]] = {}
1275
+ for name in ("scan-manifest.json", "findings.json", "coverage.json"):
1276
+ path = artifact_path(scan_dir, name, required=False)
1277
+ if path is not None:
1278
+ documents[name] = read_json_object(path)
1279
+ if documents and len(documents) != 3:
1280
+ raise SystemExit("Budget-exhausted scan contains an incomplete canonical scan draft.")
1281
+
1282
+ if documents:
1283
+ manifest = documents["scan-manifest.json"]
1284
+ findings = documents["findings.json"]
1285
+ coverage = documents["coverage.json"]
1286
+ if not isinstance(manifest.get("scan"), dict) or not isinstance(
1287
+ findings.get("findings"), list
1288
+ ):
1289
+ raise SystemExit("Budget-exhausted scan contains an invalid canonical scan draft.")
1290
+ for key in ("surfaces", "explicitExclusions", "deferred"):
1291
+ if not isinstance(coverage.get(key), list):
1292
+ raise SystemExit("Budget-exhausted scan contains invalid canonical coverage.")
1293
+ if manifest["scan"].get("sealedAt") is not None or manifest["scan"].get("artifacts"):
1294
+ raise SystemExit("Budget-exhausted scan cannot replace an already sealed scan draft.")
1295
+ else:
1296
+ contract = scan_contract(scan)
1297
+ target_contract = contract["target"]
1298
+ target: dict[str, Any] = {
1299
+ "kind": target_contract["allowedKinds"][0],
1300
+ "targetId": target_contract["targetId"],
1301
+ "displayName": target_contract["displayName"],
1302
+ }
1303
+ if scan["target_revision"] != "unversioned":
1304
+ target["revision"] = scan["target_revision"]
1305
+ if "requiredSnapshotDigest" in target_contract:
1306
+ target["snapshotDigest"] = target_contract["requiredSnapshotDigest"]
1307
+ manifest = {
1308
+ "scan": {
1309
+ "target": target,
1310
+ "scope": {"limitations": [warning], "validationMode": "incomplete"},
1311
+ }
1312
+ }
1313
+ findings = {"findings": []}
1314
+ coverage = {
1315
+ "completeness": "partial",
1316
+ "inventoryStrategy": (
1317
+ "scoped_path" if expected_coverage_mode(scan) == "scoped_path" else "repository"
1318
+ ),
1319
+ "surfaces": [],
1320
+ "explicitExclusions": [],
1321
+ "deferred": [],
1322
+ }
1323
+
1324
+ findings_by_candidate = {
1325
+ candidate_id
1326
+ for finding in findings["findings"]
1327
+ if isinstance(finding, dict)
1328
+ and isinstance(candidate_id := finding_candidate_id(finding), str)
1329
+ }
1330
+ existing_deferred = {
1331
+ item.get("candidateId", item.get("id"))
1332
+ for item in coverage["deferred"]
1333
+ if isinstance(item, dict)
1334
+ and isinstance(item.get("candidateId", item.get("id")), str)
1335
+ }
1336
+ existing_surfaces = {
1337
+ item.get("id")
1338
+ for item in coverage["surfaces"]
1339
+ if isinstance(item, dict) and isinstance(item.get("id"), str)
1340
+ }
1341
+ for candidate in candidates:
1342
+ candidate_id = candidate["candidate_id"]
1343
+ if candidate_id in findings_by_candidate or candidate_id in existing_deferred:
1344
+ continue
1345
+ paths = list(dict.fromkeys(location["path"] for location in candidate["locations"]))
1346
+ surface_id = f"candidate-{candidate_id}"
1347
+ validation = candidate.get("validation")
1348
+ validation = validation.get("disposition") if isinstance(validation, dict) else None
1349
+ attack = candidate.get("attack_path")
1350
+ attack = attack.get("decision") if isinstance(attack, dict) else None
1351
+ disposition = (
1352
+ "needs_follow_up"
1353
+ if validation == "deferred" or attack == "deferred"
1354
+ else "not_applicable"
1355
+ if validation == "not_applicable"
1356
+ else "rejected"
1357
+ if validation == "suppressed" or attack == "ignore"
1358
+ else "needs_follow_up"
1359
+ )
1360
+ if surface_id not in existing_surfaces:
1361
+ coverage["surfaces"].append(
1362
+ {
1363
+ "id": surface_id,
1364
+ "label": candidate["summary"],
1365
+ "disposition": disposition,
1366
+ "notes": candidate["evidence"],
1367
+ "receiptRefs": [],
1368
+ }
1369
+ )
1370
+ existing_surfaces.add(surface_id)
1371
+ if disposition != "needs_follow_up":
1372
+ continue
1373
+ coverage["deferred"].append(
1374
+ {
1375
+ "id": candidate_id,
1376
+ "candidateId": candidate_id,
1377
+ "reason": (
1378
+ "Validation was deferred because the scan reached its cost limit: "
1379
+ f"{candidate['summary']}. Evidence: {candidate['evidence']}"
1380
+ ),
1381
+ "paths": paths,
1382
+ "surfaceIds": [surface_id],
1383
+ }
1384
+ )
1385
+ if not any(
1386
+ isinstance(item, dict)
1387
+ and isinstance(reason := item.get("reason"), str)
1388
+ and (
1389
+ reason == "Validation was deferred because the scan reached its cost limit."
1390
+ or reason.startswith(
1391
+ "Validation was deferred because the scan reached its cost limit: "
1392
+ )
1393
+ )
1394
+ for item in coverage["deferred"]
1395
+ ):
1396
+ coverage["deferred"].append(
1397
+ {
1398
+ "id": "scan-cost-limit",
1399
+ "reason": "Validation was deferred because the scan reached its cost limit.",
1400
+ }
1401
+ )
1402
+ coverage["completeness"] = "partial"
1403
+ for name, payload in (
1404
+ ("findings.json", findings),
1405
+ ("coverage.json", coverage),
1406
+ ("scan-manifest.json", manifest),
1407
+ ):
1408
+ try:
1409
+ write_scan_local_bytes(
1410
+ scan_dir,
1411
+ name,
1412
+ (json.dumps(payload, allow_nan=False, indent=2, sort_keys=True) + "\n").encode(),
1413
+ )
1414
+ except (ContractError, OSError, TypeError, ValueError) as exc:
1415
+ raise SystemExit(f"Budget-exhausted scan draft could not be saved: {exc}") from exc
1416
+
1417
+
1139
1418
  def complete_scan_locked(
1140
1419
  connection: sqlite3.Connection,
1141
1420
  scan_id: str,
@@ -1168,8 +1447,7 @@ def complete_scan_locked(
1168
1447
  claim_token,
1169
1448
  error_message="Scan completion is owned by another continuation.",
1170
1449
  )
1171
- if scan["recipe_json"] is None:
1172
- deep_scan.require_deep_scan_ready_for_parent_completion(connection, scan)
1450
+ deep_scan.require_deep_scan_ready_for_parent_completion(connection, scan)
1173
1451
  warnings = json.loads(scan["completion_warnings_json"])
1174
1452
  target_warnings: list[str] = []
1175
1453
  warning = scan_target_warning(scan)
@@ -1262,8 +1540,7 @@ def complete_scan_locked(
1262
1540
  return scan_context(connection, scan["id"])
1263
1541
  if scan["status"] != "running":
1264
1542
  raise SystemExit("Only a running scan can be completed.")
1265
- if scan["recipe_json"] is None:
1266
- deep_scan.require_deep_scan_ready_for_parent_completion(connection, scan)
1543
+ deep_scan.require_deep_scan_ready_for_parent_completion(connection, scan)
1267
1544
  handoff.require_current_continuation(
1268
1545
  scan,
1269
1546
  claim_token,
@@ -1432,6 +1709,16 @@ def register_cli_scan(connection: sqlite3.Connection, args: argparse.Namespace)
1432
1709
  }
1433
1710
 
1434
1711
 
1712
+ def set_scan_thread(connection: sqlite3.Connection, args: argparse.Namespace) -> dict[str, Any]:
1713
+ scan = require_scan(connection, args.scan_id)
1714
+ with connection:
1715
+ connection.execute(
1716
+ "UPDATE scans SET continuation_thread_id = ?, updated_at = ? WHERE id = ?",
1717
+ (args.thread_id, now(), scan["id"]),
1718
+ )
1719
+ return {"scanId": scan["id"], "threadId": args.thread_id}
1720
+
1721
+
1435
1722
  def parse_scan_recipe(value: str, repository: Path) -> dict[str, Any]:
1436
1723
  if len(value.encode("utf-8")) > SCAN_RECIPE_MAX_BYTES:
1437
1724
  raise SystemExit("Scan launch recipe must be no larger than 256 KiB.")
@@ -3354,6 +3641,8 @@ def main() -> None:
3354
3641
  )
3355
3642
  elif args.command == "register-cli-scan":
3356
3643
  result = register_cli_scan(connection, args)
3644
+ elif args.command == "set-scan-thread":
3645
+ result = set_scan_thread(connection, args)
3357
3646
  elif args.command == "get-scan-recipe":
3358
3647
  result = get_scan_recipe(connection, args)
3359
3648
  elif args.command == "compare-scans":
@@ -3388,6 +3677,8 @@ def main() -> None:
3388
3677
  result = complete_scan(
3389
3678
  connection, args, prepare_only=args.command == "prepare-scan-completion"
3390
3679
  )
3680
+ elif args.command == "complete-budget-exhausted-scan":
3681
+ result = complete_budget_exhausted_scan(connection, args)
3391
3682
  elif args.command == "cancel-scan":
3392
3683
  result = cancel_scan(connection, args)
3393
3684
  elif args.command == "fail-scan":
@@ -47,9 +47,13 @@ def list_global_findings(
47
47
  return {
48
48
  "findings": [
49
49
  {
50
+ "confirmedInLatestScan": row["confirmed_in_latest_scan"],
50
51
  "createdAt": row["created_at"],
51
52
  "findingId": row["finding_id"],
53
+ "knownSince": row["known_since"],
54
+ "knownScanIds": row["known_scan_ids"],
52
55
  "locationPath": row["location_path"],
56
+ "matchedFindingIds": row["matched_finding_ids"],
53
57
  "occurrenceCount": row["occurrence_count"],
54
58
  "occurrenceId": row["occurrence_id"],
55
59
  "scanId": row["scan_id"],
@@ -70,64 +74,117 @@ def list_global_findings(
70
74
  }
71
75
 
72
76
 
73
- def _indexed_findings(connection: sqlite3.Connection) -> Iterator[sqlite3.Row]:
74
- yield from connection.execute(
77
+ def _indexed_findings(connection: sqlite3.Connection) -> Iterator[dict[str, Any]]:
78
+ parents: dict[tuple[str, str], tuple[str, str]] = {}
79
+
80
+ def group(identity: tuple[str, str]) -> tuple[str, str]:
81
+ while identity in parents:
82
+ identity = parents[identity]
83
+ return identity
84
+
85
+ for match in connection.execute(
75
86
  """
76
- WITH ranked_findings AS (
77
- SELECT
78
- occurrences.id AS occurrence_id,
79
- occurrences.finding_id,
80
- occurrences.severity,
81
- occurrences.created_at,
82
- scans.id AS scan_id,
83
- scans.target_id,
84
- targets.current_path AS target_path,
85
- scans.scope,
86
- MAX(scans.updated_at, COALESCE(triage.updated_at, '')) AS updated_at,
87
- COALESCE(triage.status, 'open') AS status,
88
- COUNT(*) OVER (
89
- PARTITION BY scans.target_id, occurrences.finding_id
90
- ) AS occurrence_count,
91
- ROW_NUMBER() OVER (
92
- PARTITION BY scans.target_id, occurrences.finding_id
93
- ORDER BY occurrences.created_at DESC, occurrences.id DESC
94
- ) AS occurrence_rank
95
- FROM finding_occurrences AS occurrences
96
- JOIN scans ON scans.id = occurrences.scan_id
97
- JOIN security_targets AS targets ON targets.id = scans.target_id
98
- LEFT JOIN finding_triage AS triage ON triage.occurrence_id = occurrences.id
87
+ SELECT before_scans.target_id, before.finding_id AS before_finding_id,
88
+ after.finding_id AS after_finding_id
89
+ FROM scan_comparison_matches AS matches
90
+ JOIN finding_occurrences AS before ON before.id = matches.before_occurrence_id
91
+ JOIN scans AS before_scans ON before_scans.id = before.scan_id
92
+ JOIN finding_occurrences AS after ON after.id = matches.after_occurrence_id
93
+ JOIN scans AS after_scans ON after_scans.id = after.scan_id
94
+ WHERE before_scans.target_id = after_scans.target_id
95
+ """
96
+ ):
97
+ before = group((match["target_id"], match["before_finding_id"]))
98
+ after = group((match["target_id"], match["after_finding_id"]))
99
+ if before != after:
100
+ parents[after] = before
101
+
102
+ latest_scan_by_target = dict(
103
+ connection.execute(
104
+ "SELECT target_id, id FROM scans "
105
+ "WHERE status = 'complete' ORDER BY started_at, id"
99
106
  )
107
+ )
108
+
109
+ grouped: dict[tuple[str, str], list[sqlite3.Row]] = {}
110
+ for row in connection.execute(
111
+ """
100
112
  SELECT
101
- selected_findings.*,
113
+ occurrences.id AS occurrence_id,
114
+ occurrences.finding_id,
115
+ occurrences.severity,
116
+ occurrences.created_at,
117
+ scans.id AS scan_id,
118
+ scans.started_at AS scan_started_at,
119
+ scans.target_id,
120
+ targets.current_path AS target_path,
121
+ scans.scope,
122
+ MAX(scans.updated_at, COALESCE(triage.updated_at, '')) AS updated_at,
123
+ triage.status AS decision_status,
124
+ triage.close_reason,
125
+ triage.updated_at AS decision_updated_at,
102
126
  occurrences.title,
103
127
  occurrences.summary,
104
128
  (
105
129
  SELECT locations.relative_path
106
130
  FROM finding_locations AS locations
107
- WHERE locations.occurrence_id = selected_findings.occurrence_id
131
+ WHERE locations.occurrence_id = occurrences.id
108
132
  ORDER BY
109
133
  CASE WHEN locations.role = 'root_control' THEN 0 ELSE 1 END,
110
134
  locations.sort_order
111
135
  LIMIT 1
112
136
  ) AS location_path
113
- FROM ranked_findings AS selected_findings
114
- JOIN finding_occurrences AS occurrences
115
- ON occurrences.id = selected_findings.occurrence_id
116
- WHERE selected_findings.occurrence_rank = 1
117
- ORDER BY
118
- CASE selected_findings.status WHEN 'open' THEN 0 ELSE 1 END,
119
- CASE selected_findings.severity
120
- WHEN 'critical' THEN 0
121
- WHEN 'high' THEN 1
122
- WHEN 'medium' THEN 2
123
- WHEN 'low' THEN 3
124
- WHEN 'informational' THEN 4
125
- ELSE 5
126
- END,
127
- selected_findings.created_at DESC,
128
- selected_findings.occurrence_id
137
+ FROM finding_occurrences AS occurrences
138
+ JOIN scans ON scans.id = occurrences.scan_id
139
+ JOIN security_targets AS targets ON targets.id = scans.target_id
140
+ LEFT JOIN finding_triage AS triage ON triage.occurrence_id = occurrences.id
129
141
  """,
142
+ ):
143
+ grouped.setdefault(group((row["target_id"], row["finding_id"])), []).append(row)
144
+
145
+ findings = []
146
+ for occurrences in grouped.values():
147
+ latest = max(occurrences, key=lambda row: (row["created_at"], row["occurrence_id"]))
148
+ decision = max(
149
+ (row for row in occurrences if row["decision_status"] is not None),
150
+ key=lambda row: (row["decision_updated_at"], row["occurrence_id"]),
151
+ default=None,
152
+ )
153
+ status = decision["decision_status"] if decision is not None else "open"
154
+ if (
155
+ status == "closed"
156
+ and decision["close_reason"] == "already_fixed"
157
+ and latest["created_at"] > decision["decision_updated_at"]
158
+ ):
159
+ status = "open"
160
+ scans = sorted({(row["scan_started_at"], row["scan_id"]) for row in occurrences})
161
+ findings.append(
162
+ {
163
+ **dict(latest),
164
+ "confirmed_in_latest_scan": latest_scan_by_target.get(latest["target_id"])
165
+ == latest["scan_id"],
166
+ "known_since": scans[0][0],
167
+ "known_scan_ids": [scan_id for _, scan_id in scans],
168
+ "matched_finding_ids": sorted({row["finding_id"] for row in occurrences}),
169
+ "occurrence_count": len(occurrences),
170
+ "status": status,
171
+ "updated_at": max(
172
+ latest["updated_at"],
173
+ decision["decision_updated_at"] if decision is not None else "",
174
+ ),
175
+ }
176
+ )
177
+
178
+ findings.sort(key=lambda finding: finding["occurrence_id"])
179
+ findings.sort(
180
+ key=lambda finding: (
181
+ finding["status"] == "open",
182
+ -scan_history.SEVERITY_ORDER.get(finding["severity"], 5),
183
+ finding["created_at"],
184
+ ),
185
+ reverse=True,
130
186
  )
187
+ yield from findings
131
188
 
132
189
 
133
190
  def list_repositories(
@@ -635,6 +635,14 @@ MIGRATIONS = (
635
635
  SET stop_after_consecutive_errors = stop_after_no_new;
636
636
  """,
637
637
  ),
638
+ (
639
+ 28,
640
+ "persist deep scan discovery time limit",
641
+ """
642
+ ALTER TABLE deep_scan_runs
643
+ ADD COLUMN max_time_hours REAL NOT NULL DEFAULT 96;
644
+ """,
645
+ ),
638
646
  )
639
647
 
640
648
 
@@ -689,6 +697,13 @@ def apply_migrations(
689
697
  "completion_warnings_json",
690
698
  "TEXT NOT NULL DEFAULT '[]'",
691
699
  )
700
+ elif version == 28:
701
+ add_column_if_missing(
702
+ connection,
703
+ "deep_scan_runs",
704
+ "max_time_hours",
705
+ "REAL NOT NULL DEFAULT 96",
706
+ )
692
707
  continue
693
708
  if version == 6:
694
709
  repair_thread_scoped_workspaces_migration(connection)
@@ -10,11 +10,10 @@ from typing import Any
10
10
 
11
11
  # Some plugin hosts launch Python with safe-path isolation enabled.
12
12
  sys.path.insert(0, str(Path(__file__).resolve().parent))
13
- from workbench_target import clean_worktree_content_digest, git_bytes, git_output
13
+ from workbench_target import clean_worktree_content_digest, git_bytes
14
14
 
15
15
  CONTEXT_LINES = 3
16
16
  MAX_BYTES = 16_000
17
- MAX_FILE_BYTES = 1_048_576
18
17
  MAX_LINES = 60
19
18
 
20
19
 
@@ -70,9 +69,6 @@ def scanned_source_text(scan: sqlite3.Row, target: Path, path: str) -> str | Non
70
69
  if snapshot_digest is not None and snapshot_digest != clean_worktree_content_digest():
71
70
  return None
72
71
  object_name = f"{revision}:{path}"
73
- size = git_output(target, "cat-file", "-s", object_name)
74
- if size is None or not size.isdigit() or int(size) > MAX_FILE_BYTES:
75
- return None
76
72
  content = git_bytes(target, "cat-file", "blob", object_name)
77
73
  return content.decode("utf-8", errors="replace") if content is not None else None
78
74