xploitscan-shared-rules 2.4.20 → 2.4.21

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.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var SCANNER_VERSION = "2.4.20";
2
+ var SCANNER_VERSION = "2.4.21";
3
3
 
4
4
  // src/snippet.ts
5
5
  var cacheContent = null;
@@ -5868,6 +5868,255 @@ function scanEntropyFiles(files, collect) {
5868
5868
  return findings;
5869
5869
  }
5870
5870
 
5871
+ // src/ai-status-constants.ts
5872
+ var AI_STATUSES = ["reviewed", "partial", "skipped", "failed", "unknown"];
5873
+ var AI_REASONS = [
5874
+ "no_api_key",
5875
+ "client_init",
5876
+ "capped",
5877
+ "budget_timeout",
5878
+ "model_bad_output",
5879
+ "model_error",
5880
+ "missing_content",
5881
+ "exception",
5882
+ "opted_out",
5883
+ "auth_failed",
5884
+ "jti_missing",
5885
+ "token_unverified",
5886
+ "pro_rules_unavailable",
5887
+ "engine_unavailable",
5888
+ "upload_failed",
5889
+ "upload_blocked",
5890
+ "upload_duplicate",
5891
+ "persist_failed",
5892
+ "quality_unavailable",
5893
+ "intent_unavailable",
5894
+ "not_supported",
5895
+ "token_secret_missing",
5896
+ "rate_limited",
5897
+ "jti_unavailable",
5898
+ "credit_error",
5899
+ "daily_limit",
5900
+ "signed_out"
5901
+ ];
5902
+ var AI_SURFACES = ["upload", "cli", "action", "app", "api", "mcp", "web", "save"];
5903
+ var PLAN_CAUSES = [
5904
+ "missing",
5905
+ "malformed",
5906
+ "expired",
5907
+ "revoked",
5908
+ "network",
5909
+ "upstream_5xx",
5910
+ "unknown"
5911
+ ];
5912
+ var WEBHOOK_REASONS = [
5913
+ "no_metadata_user",
5914
+ "malformed_user",
5915
+ "unknown_user",
5916
+ "user_bound_other_customer",
5917
+ "race_no_row",
5918
+ "deleted_unmatched",
5919
+ "clerk_lookup_failed"
5920
+ ];
5921
+ var WEBHOOK_EVENT_TYPES = [
5922
+ "checkout.session.completed",
5923
+ "customer.subscription.created",
5924
+ "customer.subscription.updated",
5925
+ "customer.subscription.deleted",
5926
+ "invoice.paid",
5927
+ "invoice.payment_failed",
5928
+ "created",
5929
+ "updated"
5930
+ ];
5931
+
5932
+ // src/ai-status.ts
5933
+ var PLAN_UNVERIFIED_NOTICE = "Couldn't verify your plan, scanned with free rules only.";
5934
+ var FAIL_REASONS = /* @__PURE__ */ new Set([
5935
+ "budget_timeout",
5936
+ "model_error",
5937
+ "model_bad_output",
5938
+ "auth_failed",
5939
+ "exception",
5940
+ "client_init"
5941
+ ]);
5942
+ function clampCount(raw) {
5943
+ if (typeof raw !== "number" || !Number.isFinite(raw) || raw < 0) return 0;
5944
+ return Math.min(Math.trunc(raw), 1e6);
5945
+ }
5946
+ function isStatus(raw) {
5947
+ return typeof raw === "string" && AI_STATUSES.includes(raw);
5948
+ }
5949
+ function isReason(raw) {
5950
+ return typeof raw === "string" && AI_REASONS.includes(raw);
5951
+ }
5952
+ function isSurface(raw) {
5953
+ return typeof raw === "string" && AI_SURFACES.includes(raw);
5954
+ }
5955
+ function bucketFiles(n) {
5956
+ if (typeof n !== "number" || !Number.isFinite(n) || n <= 0) return "0";
5957
+ const v = Math.trunc(n);
5958
+ if (v < 5) return "1-4";
5959
+ if (v < 20) return "5-19";
5960
+ return "20+";
5961
+ }
5962
+ function planCause(specific) {
5963
+ switch (specific) {
5964
+ case "secret_missing":
5965
+ return "missing";
5966
+ case "bad_shape":
5967
+ case "sig_mismatch":
5968
+ case "sig_length_mismatch":
5969
+ case "hmac_error":
5970
+ case "payload_parse_error":
5971
+ case "uid_or_tier_wrong_type":
5972
+ return "malformed";
5973
+ case "expired":
5974
+ case "exp_missing_or_nan":
5975
+ return "expired";
5976
+ case "tier_not_paid_unknown_value":
5977
+ case "http_401":
5978
+ case "http_403":
5979
+ return "revoked";
5980
+ case "no_cached_bundle_and_network_error":
5981
+ case "timeout":
5982
+ case "network":
5983
+ return "network";
5984
+ case "http_5xx":
5985
+ case "upstream_5xx":
5986
+ return "upstream_5xx";
5987
+ default:
5988
+ return "unknown";
5989
+ }
5990
+ }
5991
+ function toAiDegraded(input = {}) {
5992
+ const total = clampCount(input.total);
5993
+ let reviewed = clampCount(input.reviewed);
5994
+ let capped = clampCount(input.capped);
5995
+ if (reviewed > total) reviewed = total;
5996
+ if (reviewed + capped > total) capped = total - reviewed;
5997
+ const unreviewed = total - reviewed - capped;
5998
+ let status = isStatus(input.status) ? input.status : "unknown";
5999
+ let reason = isReason(input.reason) ? input.reason : null;
6000
+ const eligible = total - capped;
6001
+ if (status === "reviewed" && reviewed !== eligible) {
6002
+ status = reviewed === 0 && reason != null && FAIL_REASONS.has(reason) ? "failed" : "partial";
6003
+ }
6004
+ if (status === "reviewed" && unreviewed > 0) status = "partial";
6005
+ if (status === "skipped" && reason != null && FAIL_REASONS.has(reason)) status = "failed";
6006
+ if (status === "reviewed" || status === "unknown") {
6007
+ if (status === "unknown") reason = null;
6008
+ }
6009
+ const modelStatus = reason === "model_error" && typeof input.modelStatus === "number" && Number.isFinite(input.modelStatus) ? Math.trunc(input.modelStatus) : null;
6010
+ return {
6011
+ status,
6012
+ reason,
6013
+ surface: isSurface(input.surface) ? input.surface : "cli",
6014
+ total,
6015
+ reviewed,
6016
+ unreviewed,
6017
+ capped,
6018
+ modelStatus
6019
+ };
6020
+ }
6021
+ function unknownAiStatus(surface = "save") {
6022
+ return toAiDegraded({ status: "unknown", reason: null, surface, total: 0, reviewed: 0, unreviewed: 0, capped: 0 });
6023
+ }
6024
+ function aiStatusLine(d) {
6025
+ const shown = Math.max(0, d.total - d.capped);
6026
+ switch (d.reason) {
6027
+ case "no_api_key":
6028
+ return "AI review skipped: no Anthropic API key is set.";
6029
+ case "opted_out":
6030
+ return "AI review skipped: user opted out.";
6031
+ case "not_supported":
6032
+ return "AI review not run on MCP scans.";
6033
+ case "auth_failed":
6034
+ return "AI review failed: the Anthropic API key was rejected.";
6035
+ case "capped":
6036
+ return `AI review covered the first ${shown} of ${d.total} findings.`;
6037
+ case "budget_timeout":
6038
+ return d.status === "failed" ? `AI review failed: every batch timed out (${d.reviewed} reviewed, ${d.unreviewed} unreviewed, ${d.capped} capped, ${d.total} total).` : `AI review was partial: the time limit ran out (${d.reviewed} reviewed of ${d.total}).`;
6039
+ case "client_init":
6040
+ return "AI review failed: the AI client could not start.";
6041
+ case "model_bad_output":
6042
+ return "AI review failed: the model returned unusable output.";
6043
+ case "model_error":
6044
+ return `AI review failed: the model request was rejected (HTTP ${d.modelStatus ?? "error"}).`;
6045
+ case "missing_content":
6046
+ return "AI review was partial: source content was missing for some findings.";
6047
+ case "exception":
6048
+ return "AI review failed: an unexpected error occurred.";
6049
+ case "token_secret_missing":
6050
+ return "AI review skipped: scan token verification is not configured.";
6051
+ case "rate_limited":
6052
+ return "AI review skipped: rate limited.";
6053
+ case "jti_unavailable":
6054
+ return "AI review skipped: the review token could not be checked in time.";
6055
+ case "jti_missing":
6056
+ return "AI review skipped: the review token had no single-use id.";
6057
+ case "credit_error":
6058
+ return "AI review skipped: the review credit could not be spent.";
6059
+ case "daily_limit":
6060
+ return "AI review skipped: daily limit reached.";
6061
+ case "signed_out":
6062
+ return "AI review skipped: signed out.";
6063
+ case "quality_unavailable":
6064
+ return "Quality checks were unavailable for this scan.";
6065
+ case "intent_unavailable":
6066
+ return "Intent check was unavailable for this scan.";
6067
+ case "engine_unavailable":
6068
+ return "A scan engine was unavailable.";
6069
+ case "upload_failed":
6070
+ return "The scan could not be uploaded.";
6071
+ case "upload_blocked":
6072
+ return "The scan was not uploaded: the plan limit blocked it.";
6073
+ case "upload_duplicate":
6074
+ return "The scan was already uploaded for this job.";
6075
+ case "persist_failed":
6076
+ return "The scan status could not be stored.";
6077
+ case "token_unverified":
6078
+ case "pro_rules_unavailable":
6079
+ return PLAN_UNVERIFIED_NOTICE;
6080
+ default:
6081
+ if (d.status === "reviewed") return "AI review completed.";
6082
+ if (d.status === "unknown") return "AI review status is unknown.";
6083
+ if (d.status === "partial") return `AI review was partial: ${d.reviewed} of ${d.total} findings reviewed.`;
6084
+ if (d.status === "failed") return "AI review failed.";
6085
+ return "AI review skipped.";
6086
+ }
6087
+ }
6088
+ var SCAN_AI_DEGRADED_KEYS = [
6089
+ "status",
6090
+ "reason",
6091
+ "surface",
6092
+ "total",
6093
+ "reviewed",
6094
+ "capped",
6095
+ "files_received_bucket"
6096
+ ];
6097
+ function scanAiDegradedProps(d, filesReceived) {
6098
+ return {
6099
+ status: d.status,
6100
+ reason: d.reason,
6101
+ surface: d.surface,
6102
+ total: d.total,
6103
+ reviewed: d.reviewed,
6104
+ capped: d.capped,
6105
+ files_received_bucket: bucketFiles(filesReceived)
6106
+ };
6107
+ }
6108
+ var SCAN_TOKEN_NULL_KEYS = ["cause", "surface", "tier_expected"];
6109
+ function scanTokenNullProps(cause, surface) {
6110
+ const safeSurface = isSurface(surface) ? surface : "api";
6111
+ return { cause, surface: safeSurface, tier_expected: "paid" };
6112
+ }
6113
+ var BILLING_WEBHOOK_UNMATCHED_KEYS = ["type", "reason", "final"];
6114
+ function billingWebhookUnmatchedProps(input) {
6115
+ const reason = typeof input.reason === "string" && WEBHOOK_REASONS.includes(input.reason) ? input.reason : "race_no_row";
6116
+ const type = typeof input.type === "string" && input.type.length > 0 && input.type.length < 80 ? input.type : "unknown";
6117
+ return { type, reason, final: input.final === true };
6118
+ }
6119
+
5871
6120
  // src/ai-fp-filter.ts
5872
6121
  var REVIEW_SYSTEM_PROMPT = `You are reviewing security scan findings for false positives. Your job is to look at each finding and the surrounding code to determine if it's a REAL security vulnerability or a FALSE POSITIVE.
5873
6122
 
@@ -5927,6 +6176,15 @@ var AI_FILTER_BUDGET_MS = (() => {
5927
6176
  return Math.min(n, 6e4);
5928
6177
  })();
5929
6178
  var PER_CALL_TIMEOUT_MS = 8e3;
6179
+ function perCallTimeoutMs(budgetMs) {
6180
+ const budget = Number.isFinite(budgetMs) && budgetMs >= 1 ? Math.min(Math.trunc(budgetMs), 6e4) : DEFAULT_AI_FILTER_BUDGET_MS;
6181
+ const scaled = Math.floor(budget * 0.6);
6182
+ const perCall = Math.min(PER_CALL_TIMEOUT_MS, scaled);
6183
+ return perCall;
6184
+ }
6185
+ if (!(perCallTimeoutMs(AI_FILTER_BUDGET_MS) < AI_FILTER_BUDGET_MS)) {
6186
+ throw new Error("per-call AI timeout must be strictly below the filter budget");
6187
+ }
5930
6188
  function isAbortError(err) {
5931
6189
  if (!err || typeof err !== "object") return false;
5932
6190
  const name = "name" in err ? String(err.name) : "";
@@ -5939,6 +6197,16 @@ function isTimeoutError(err) {
5939
6197
  const message = err instanceof Error ? err.message : String(err);
5940
6198
  return /\b(?:request|connection|operation|model call)\s+(?:has\s+)?timed out\b/i.test(message) || /\bETIMEDOUT\b/i.test(message);
5941
6199
  }
6200
+ function errorHttpStatus(err) {
6201
+ if (!err || typeof err !== "object") return null;
6202
+ const status = err.status;
6203
+ return typeof status === "number" && Number.isFinite(status) ? Math.trunc(status) : null;
6204
+ }
6205
+ function callWasAborted(err) {
6206
+ if (!err || typeof err !== "object") return false;
6207
+ const name = "name" in err ? String(err.name) : "";
6208
+ return name === "AbortError" || name === "APIUserAbortError";
6209
+ }
5942
6210
  var SEVERITY_PRIORITY = {
5943
6211
  critical: 0,
5944
6212
  high: 1,
@@ -6088,8 +6356,10 @@ function parseReviewResponse(text) {
6088
6356
  return null;
6089
6357
  }
6090
6358
  }
6091
- async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER_BUDGET_MS) {
6092
- const empty = {
6359
+ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER_BUDGET_MS, options) {
6360
+ const surface = options?.surface ?? "cli";
6361
+ const emptyMask = { prompt_batches: 0, prompt_chars: 0, masked_count: 0, had_secret_pattern: false, raw_after_mask: 0 };
6362
+ const early = (status2, reason2, extra) => ({
6093
6363
  findings,
6094
6364
  filteredFindings: [],
6095
6365
  policyRetainedFindings: [],
@@ -6097,15 +6367,27 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6097
6367
  removedCount: 0,
6098
6368
  policyRetainedCount: 0,
6099
6369
  totalBefore: findings.length,
6100
- reviewedCount: 0,
6101
- unreviewedCount: findings.length,
6102
- cappedCount: 0,
6370
+ reviewedCount: extra?.reviewed ?? 0,
6371
+ unreviewedCount: extra?.unreviewed ?? findings.length,
6372
+ cappedCount: extra?.capped ?? 0,
6103
6373
  missingContentCount: 0,
6104
6374
  timedOut: false,
6105
- maskStats: { prompt_batches: 0, prompt_chars: 0, masked_count: 0, had_secret_pattern: false, raw_after_mask: 0 }
6106
- };
6107
- if (findings.length === 0) return empty;
6108
- if (!process.env.ANTHROPIC_API_KEY) return empty;
6375
+ maskStats: emptyMask,
6376
+ aiDegraded: toAiDegraded({
6377
+ status: status2,
6378
+ reason: reason2,
6379
+ surface,
6380
+ total: findings.length,
6381
+ reviewed: extra?.reviewed ?? 0,
6382
+ unreviewed: extra?.unreviewed ?? findings.length,
6383
+ capped: extra?.capped ?? 0,
6384
+ modelStatus: extra?.modelStatus ?? null
6385
+ })
6386
+ });
6387
+ if (findings.length === 0) {
6388
+ return early("reviewed", null, { reviewed: 0, unreviewed: 0, capped: 0 });
6389
+ }
6390
+ if (!process.env.ANTHROPIC_API_KEY) return early("skipped", "no_api_key");
6109
6391
  const deadlineMs = Date.now() + Math.max(0, budgetMs);
6110
6392
  const abortController = new AbortController();
6111
6393
  let budgetExpired = budgetMs <= 0;
@@ -6135,12 +6417,20 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6135
6417
  try {
6136
6418
  client = new Anthropic();
6137
6419
  } catch {
6138
- return empty;
6420
+ if (budgetTimer) clearTimeout(budgetTimer);
6421
+ return early("failed", "client_init", {
6422
+ reviewed: 0,
6423
+ capped: overflow.length,
6424
+ unreviewed: findings.length - overflow.length
6425
+ });
6139
6426
  }
6140
6427
  const fpMap = /* @__PURE__ */ new Map();
6141
6428
  const policyRetainedMap = /* @__PURE__ */ new Map();
6142
6429
  const reviewedIndices = /* @__PURE__ */ new Set();
6143
6430
  let firstError = null;
6431
+ let firstErrorStatus = null;
6432
+ let authFailed = false;
6433
+ let badOutput = false;
6144
6434
  const maskStats = { prompt_batches: 0, prompt_chars: 0, masked_count: 0, had_secret_pattern: false, raw_after_mask: 0 };
6145
6435
  let failedBatches = 0;
6146
6436
  let timedOutCount = 0;
@@ -6166,9 +6456,11 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6166
6456
  const reviewBatch = async (task) => {
6167
6457
  const { targets, content, detections } = task;
6168
6458
  const batch = targets.map((target) => target.finding);
6169
- if (abortController.signal.aborted || Date.now() >= deadlineMs) {
6170
- budgetExpired = true;
6171
- timedOutCount += batch.length;
6459
+ if (authFailed || abortController.signal.aborted || Date.now() >= deadlineMs) {
6460
+ if (!authFailed) {
6461
+ budgetExpired = true;
6462
+ timedOutCount += batch.length;
6463
+ }
6172
6464
  return;
6173
6465
  }
6174
6466
  try {
@@ -6180,7 +6472,7 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6180
6472
  maskStats.masked_count += proof.detected - proof.raw;
6181
6473
  maskStats.raw_after_mask += proof.raw;
6182
6474
  if (proof.detected > 0) maskStats.had_secret_pattern = true;
6183
- console.info(
6475
+ console.error(
6184
6476
  `[ai-fp-filter] batch prompt ${prompt.length} chars (~${Math.ceil(prompt.length / 4)} tokens), ${batch.length} finding(s)`
6185
6477
  );
6186
6478
  if (prompt.length > MAX_BATCH_PROMPT_CHARS) {
@@ -6188,38 +6480,52 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6188
6480
  `[ai-fp-filter] batch prompt ${prompt.length} chars exceeds ${MAX_BATCH_PROMPT_CHARS} cap (single oversized finding after truncation)`
6189
6481
  );
6190
6482
  }
6191
- const response = await client.messages.create(
6192
- {
6193
- model: MODEL_TRIAGE,
6194
- max_tokens: 2048,
6195
- // Haiku 5.5 rejects non-default sampling params with HTTP 400
6196
- // ("temperature is deprecated for this model"). Omit temperature /
6197
- // top_p / top_k entirely — do NOT send MODEL_TEMPERATURE (0).
6198
- // Adaptive thinking is on by default for 5.5; disable it here: this
6199
- // is binary FP classification, not open-ended analysis, and thinking
6200
- // burns the shared AI budget (Isobar rescan: every call 400'd on
6201
- // temperature, then timed_out with aiReviewed:false → grade D).
6202
- thinking: { type: "disabled" },
6203
- system: REVIEW_SYSTEM_PROMPT,
6204
- messages: [{ role: "user", content: prompt }]
6205
- },
6206
- // Shared AbortController plus a per-call timeout capped at remaining
6207
- // budget. Previously 20s with no overall deadline.
6208
- { timeout: Math.min(PER_CALL_TIMEOUT_MS, remaining), signal: abortController.signal }
6209
- );
6483
+ const perCall = perCallTimeoutMs(budgetMs);
6484
+ const callTimeout = Math.min(perCall, remaining);
6485
+ const callAbort = new AbortController();
6486
+ const onParentAbort = () => callAbort.abort();
6487
+ if (abortController.signal.aborted) callAbort.abort();
6488
+ else abortController.signal.addEventListener("abort", onParentAbort, { once: true });
6489
+ const callTimer = setTimeout(() => callAbort.abort(), callTimeout);
6490
+ let response;
6491
+ try {
6492
+ response = await client.messages.create(
6493
+ {
6494
+ model: MODEL_TRIAGE,
6495
+ max_tokens: 2048,
6496
+ // Haiku 5.5 rejects non-default sampling params with HTTP 400
6497
+ // ("temperature is deprecated for this model"). Omit temperature /
6498
+ // top_p / top_k entirely — do NOT send MODEL_TEMPERATURE (0).
6499
+ // Adaptive thinking is on by default for 5.5; disable it here: this
6500
+ // is binary FP classification, not open-ended analysis, and thinking
6501
+ // burns the shared AI budget (Isobar rescan: every call 400'd on
6502
+ // temperature, then timed_out with aiReviewed:false → grade D).
6503
+ thinking: { type: "disabled" },
6504
+ system: REVIEW_SYSTEM_PROMPT,
6505
+ messages: [{ role: "user", content: prompt }]
6506
+ },
6507
+ { timeout: callTimeout, signal: callAbort.signal }
6508
+ );
6509
+ } finally {
6510
+ clearTimeout(callTimer);
6511
+ abortController.signal.removeEventListener("abort", onParentAbort);
6512
+ }
6210
6513
  const text = response.content.filter((b) => b.type === "text").map((b) => b.text).join("");
6211
6514
  const results = parseReviewResponse(text);
6212
- if (!results) throw new Error("AI review returned malformed JSON");
6515
+ if (!results) {
6516
+ badOutput = true;
6517
+ throw new Error("AI review returned malformed JSON");
6518
+ }
6213
6519
  for (const r of results) {
6214
6520
  if (r.index >= batch.length) continue;
6215
6521
  const target = targets[r.index];
6216
6522
  reviewedIndices.add(target.originalIndex);
6217
6523
  if (r.verdict !== "fp") continue;
6218
- const reason = truncateChars(applySecretMasks(r.reason, secrets), MAX_FIELD_CHARS);
6524
+ const reason2 = truncateChars(applySecretMasks(r.reason, secrets), MAX_FIELD_CHARS);
6219
6525
  if (isProtectedFromAiRemoval(target.finding)) {
6220
- policyRetainedMap.set(target.originalIndex, reason);
6526
+ policyRetainedMap.set(target.originalIndex, reason2);
6221
6527
  } else {
6222
- fpMap.set(target.originalIndex, reason);
6528
+ fpMap.set(target.originalIndex, reason2);
6223
6529
  }
6224
6530
  }
6225
6531
  if (results.filter((result) => result.index < batch.length).length < batch.length) {
@@ -6227,21 +6533,35 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6227
6533
  if (!firstError) firstError = new Error("AI review returned incomplete verdicts");
6228
6534
  }
6229
6535
  } catch (err) {
6230
- const callTimedOut = isAbortError(err) || isTimeoutError(err) || abortController.signal.aborted;
6536
+ const status2 = errorHttpStatus(err);
6537
+ if (status2 === 401 || status2 === 403) {
6538
+ authFailed = true;
6539
+ failedBatches++;
6540
+ if (!firstError) {
6541
+ firstError = err;
6542
+ firstErrorStatus = status2;
6543
+ }
6544
+ console.error(`[ai-fp-filter] AI review failed: auth status ${status2}`);
6545
+ return;
6546
+ }
6547
+ const callTimedOut = isAbortError(err) || isTimeoutError(err) || abortController.signal.aborted || callWasAborted(err);
6231
6548
  if (callTimedOut) {
6232
6549
  timedOutCount += batch.length;
6233
6550
  budgetExpired = true;
6234
6551
  return;
6235
6552
  }
6236
6553
  failedBatches++;
6237
- if (!firstError) firstError = err;
6554
+ if (!firstError) {
6555
+ firstError = err;
6556
+ firstErrorStatus = status2;
6557
+ }
6238
6558
  }
6239
6559
  };
6240
6560
  try {
6241
6561
  let next = 0;
6242
6562
  const workerCount = Math.min(MAX_CONCURRENT, tasks.length);
6243
6563
  const workers = Array.from({ length: workerCount }, async () => {
6244
- while (next < tasks.length) {
6564
+ while (next < tasks.length && !authFailed) {
6245
6565
  const cur = next++;
6246
6566
  await reviewBatch(tasks[cur]);
6247
6567
  }
@@ -6259,28 +6579,70 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6259
6579
  `[ai-fp-filter] budget expired or model call timed out \u2014 ${timedOutCount} finding(s) not reviewed; returning them unchanged`
6260
6580
  );
6261
6581
  }
6262
- if (firstError) {
6263
- const msg = firstError instanceof Error ? firstError.message : String(firstError);
6582
+ if (firstError && !authFailed) {
6583
+ const statusLabel = firstErrorStatus == null ? "none" : String(firstErrorStatus);
6264
6584
  if (!reviewedAny) {
6265
6585
  console.error(
6266
- "[ai-fp-filter] AI review unavailable \u2014 every model call failed; returning unreviewed findings:",
6267
- msg
6586
+ `[ai-fp-filter] AI review unavailable \u2014 every model call failed; status ${statusLabel}`
6268
6587
  );
6269
6588
  } else {
6270
6589
  console.warn(
6271
- `[ai-fp-filter] partial AI review \u2014 ${failedBatches} of ${tasks.length} batch(es) failed; those findings were not reviewed:`,
6272
- msg
6590
+ `[ai-fp-filter] partial AI review \u2014 ${failedBatches} of ${tasks.length} batch(es) failed; status ${statusLabel}`
6273
6591
  );
6274
6592
  }
6275
6593
  }
6276
6594
  const filtered = findings.filter((_, index) => !fpMap.has(index));
6277
6595
  const filteredFindings = [];
6278
- for (const [index, reason] of [...fpMap].sort((a, b) => a[0] - b[0])) {
6279
- filteredFindings.push({ finding: findings[index], reason });
6596
+ for (const [index, reason2] of [...fpMap].sort((a, b) => a[0] - b[0])) {
6597
+ filteredFindings.push({ finding: findings[index], reason: reason2 });
6280
6598
  }
6281
6599
  const policyRetainedFindings = [];
6282
- for (const [index, reason] of [...policyRetainedMap].sort((a, b) => a[0] - b[0])) {
6283
- policyRetainedFindings.push({ finding: findings[index], reason });
6600
+ for (const [index, reason2] of [...policyRetainedMap].sort((a, b) => a[0] - b[0])) {
6601
+ policyRetainedFindings.push({ finding: findings[index], reason: reason2 });
6602
+ }
6603
+ const cappedCount = overflow.length;
6604
+ const eligible = totalBefore - cappedCount;
6605
+ let reason = null;
6606
+ let status = "reviewed";
6607
+ let modelStatus = null;
6608
+ if (authFailed) {
6609
+ status = "failed";
6610
+ reason = "auth_failed";
6611
+ } else if (reviewedCount === 0 && (timedOut || budgetExpired && tasks.length > 0)) {
6612
+ status = "failed";
6613
+ reason = "budget_timeout";
6614
+ } else if (reviewedCount === 0 && badOutput) {
6615
+ status = "failed";
6616
+ reason = "model_bad_output";
6617
+ } else if (reviewedCount === 0 && firstErrorStatus != null) {
6618
+ status = "failed";
6619
+ reason = "model_error";
6620
+ modelStatus = firstErrorStatus;
6621
+ } else if (reviewedCount === 0 && firstError) {
6622
+ status = "failed";
6623
+ reason = "exception";
6624
+ } else if (reviewedCount === eligible && cappedCount > 0 && missingContentCount === 0) {
6625
+ status = "partial";
6626
+ reason = "capped";
6627
+ } else if (reviewedCount === eligible && missingContentCount === 0 && !firstError && !timedOut) {
6628
+ status = "reviewed";
6629
+ reason = null;
6630
+ } else if (timedOut) {
6631
+ status = reviewedCount === 0 ? "failed" : "partial";
6632
+ reason = "budget_timeout";
6633
+ } else if (missingContentCount > 0 && !firstError && !badOutput) {
6634
+ status = reviewedCount === 0 ? "partial" : "partial";
6635
+ reason = "missing_content";
6636
+ } else if (badOutput) {
6637
+ status = reviewedCount === 0 ? "failed" : "partial";
6638
+ reason = "model_bad_output";
6639
+ } else if (firstErrorStatus != null) {
6640
+ status = "partial";
6641
+ reason = "model_error";
6642
+ modelStatus = firstErrorStatus;
6643
+ } else if (firstError || reviewedCount < eligible) {
6644
+ status = reviewedCount === 0 ? "failed" : "partial";
6645
+ reason = "exception";
6284
6646
  }
6285
6647
  return {
6286
6648
  findings: filtered,
@@ -6292,10 +6654,20 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6292
6654
  totalBefore,
6293
6655
  reviewedCount,
6294
6656
  unreviewedCount,
6295
- cappedCount: overflow.length,
6657
+ cappedCount,
6296
6658
  missingContentCount,
6297
6659
  timedOut,
6298
- maskStats
6660
+ maskStats,
6661
+ aiDegraded: toAiDegraded({
6662
+ status,
6663
+ reason,
6664
+ surface,
6665
+ total: totalBefore,
6666
+ reviewed: reviewedCount,
6667
+ unreviewed: totalBefore - reviewedCount - cappedCount,
6668
+ capped: cappedCount,
6669
+ modelStatus
6670
+ })
6299
6671
  };
6300
6672
  }
6301
6673
 
@@ -10937,6 +11309,10 @@ function scanQuality(input, rules = qualityRules) {
10937
11309
  return findings;
10938
11310
  }
10939
11311
  export {
11312
+ AI_REASONS,
11313
+ AI_STATUSES,
11314
+ AI_SURFACES,
11315
+ BILLING_WEBHOOK_UNMATCHED_KEYS,
10940
11316
  CHECKABLE_CLAIMS,
10941
11317
  DOTENV_ASSIGN_RE,
10942
11318
  FREE_RULE_COUNT,
@@ -10948,17 +11324,26 @@ export {
10948
11324
  MODEL_TEMPERATURE,
10949
11325
  MODEL_TRIAGE,
10950
11326
  NON_APP_PATH_PATTERNS,
11327
+ PLAN_CAUSES,
11328
+ PLAN_UNVERIFIED_NOTICE,
10951
11329
  PRO_RULE_COUNT,
10952
11330
  PUBLIC_ENV_PREFIX_RE,
10953
11331
  QUALITY_EXAMPLES,
10954
11332
  RULE_IMPACTS,
10955
11333
  SCANNER_VERSION,
11334
+ SCAN_AI_DEGRADED_KEYS,
11335
+ SCAN_TOKEN_NULL_KEYS,
10956
11336
  SERVER_ACTION_WRITE_RE,
10957
11337
  TEST_FILE_PATTERN,
10958
11338
  TOTAL_RULE_COUNT,
11339
+ WEBHOOK_EVENT_TYPES,
11340
+ WEBHOOK_REASONS,
11341
+ aiStatusLine,
10959
11342
  allRules,
10960
11343
  assessHeaders,
10961
11344
  astMatch,
11345
+ billingWebhookUnmatchedProps,
11346
+ bucketFiles,
10962
11347
  buildProjectContext,
10963
11348
  buildQualityContext,
10964
11349
  buildReviewPrompt,
@@ -11043,21 +11428,27 @@ export {
11043
11428
  parseFile,
11044
11429
  parseScanTarget,
11045
11430
  parseSupabaseConfig,
11431
+ perCallTimeoutMs,
11432
+ planCause,
11046
11433
  qualityRules,
11047
11434
  replaceBlockComments,
11048
11435
  routeForFile,
11049
11436
  runCustomRules,
11437
+ scanAiDegradedProps,
11050
11438
  scanConfigs,
11051
11439
  scanDependencies,
11052
11440
  scanEntropy,
11053
11441
  scanMultiFile,
11054
11442
  scanQuality,
11443
+ scanTokenNullProps,
11055
11444
  sqlInjection,
11056
11445
  stripeWebhookUnprotected,
11057
11446
  supabaseFunctionName,
11058
11447
  supabaseNoRLS,
11448
+ toAiDegraded,
11059
11449
  todoLeftInCode,
11060
11450
  tryParse,
11451
+ unknownAiStatus,
11061
11452
  unmeasuredQualityRules,
11062
11453
  unvalidatedRedirect,
11063
11454
  vc001TestFixtureSeverity,