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.cjs CHANGED
@@ -30,6 +30,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
+ AI_REASONS: () => AI_REASONS,
34
+ AI_STATUSES: () => AI_STATUSES,
35
+ AI_SURFACES: () => AI_SURFACES,
36
+ BILLING_WEBHOOK_UNMATCHED_KEYS: () => BILLING_WEBHOOK_UNMATCHED_KEYS,
33
37
  CHECKABLE_CLAIMS: () => CHECKABLE_CLAIMS,
34
38
  DOTENV_ASSIGN_RE: () => DOTENV_ASSIGN_RE,
35
39
  FREE_RULE_COUNT: () => FREE_RULE_COUNT,
@@ -41,17 +45,26 @@ __export(index_exports, {
41
45
  MODEL_TEMPERATURE: () => MODEL_TEMPERATURE,
42
46
  MODEL_TRIAGE: () => MODEL_TRIAGE,
43
47
  NON_APP_PATH_PATTERNS: () => NON_APP_PATH_PATTERNS,
48
+ PLAN_CAUSES: () => PLAN_CAUSES,
49
+ PLAN_UNVERIFIED_NOTICE: () => PLAN_UNVERIFIED_NOTICE,
44
50
  PRO_RULE_COUNT: () => PRO_RULE_COUNT,
45
51
  PUBLIC_ENV_PREFIX_RE: () => PUBLIC_ENV_PREFIX_RE,
46
52
  QUALITY_EXAMPLES: () => QUALITY_EXAMPLES,
47
53
  RULE_IMPACTS: () => RULE_IMPACTS,
48
54
  SCANNER_VERSION: () => SCANNER_VERSION,
55
+ SCAN_AI_DEGRADED_KEYS: () => SCAN_AI_DEGRADED_KEYS,
56
+ SCAN_TOKEN_NULL_KEYS: () => SCAN_TOKEN_NULL_KEYS,
49
57
  SERVER_ACTION_WRITE_RE: () => SERVER_ACTION_WRITE_RE,
50
58
  TEST_FILE_PATTERN: () => TEST_FILE_PATTERN,
51
59
  TOTAL_RULE_COUNT: () => TOTAL_RULE_COUNT,
60
+ WEBHOOK_EVENT_TYPES: () => WEBHOOK_EVENT_TYPES,
61
+ WEBHOOK_REASONS: () => WEBHOOK_REASONS,
62
+ aiStatusLine: () => aiStatusLine,
52
63
  allRules: () => allRules,
53
64
  assessHeaders: () => assessHeaders,
54
65
  astMatch: () => astMatch,
66
+ billingWebhookUnmatchedProps: () => billingWebhookUnmatchedProps,
67
+ bucketFiles: () => bucketFiles,
55
68
  buildProjectContext: () => buildProjectContext,
56
69
  buildQualityContext: () => buildQualityContext,
57
70
  buildReviewPrompt: () => buildReviewPrompt,
@@ -136,21 +149,27 @@ __export(index_exports, {
136
149
  parseFile: () => parseFile,
137
150
  parseScanTarget: () => parseScanTarget,
138
151
  parseSupabaseConfig: () => parseSupabaseConfig,
152
+ perCallTimeoutMs: () => perCallTimeoutMs,
153
+ planCause: () => planCause,
139
154
  qualityRules: () => qualityRules,
140
155
  replaceBlockComments: () => replaceBlockComments,
141
156
  routeForFile: () => routeForFile,
142
157
  runCustomRules: () => runCustomRules,
158
+ scanAiDegradedProps: () => scanAiDegradedProps,
143
159
  scanConfigs: () => scanConfigs,
144
160
  scanDependencies: () => scanDependencies,
145
161
  scanEntropy: () => scanEntropy,
146
162
  scanMultiFile: () => scanMultiFile,
147
163
  scanQuality: () => scanQuality,
164
+ scanTokenNullProps: () => scanTokenNullProps,
148
165
  sqlInjection: () => sqlInjection,
149
166
  stripeWebhookUnprotected: () => stripeWebhookUnprotected,
150
167
  supabaseFunctionName: () => supabaseFunctionName,
151
168
  supabaseNoRLS: () => supabaseNoRLS,
169
+ toAiDegraded: () => toAiDegraded,
152
170
  todoLeftInCode: () => todoLeftInCode,
153
171
  tryParse: () => tryParse,
172
+ unknownAiStatus: () => unknownAiStatus,
154
173
  unmeasuredQualityRules: () => unmeasuredQualityRules,
155
174
  unvalidatedRedirect: () => unvalidatedRedirect,
156
175
  vc001TestFixtureSeverity: () => vc001TestFixtureSeverity,
@@ -163,7 +182,7 @@ __export(index_exports, {
163
182
  module.exports = __toCommonJS(index_exports);
164
183
 
165
184
  // src/version.ts
166
- var SCANNER_VERSION = "2.4.20";
185
+ var SCANNER_VERSION = "2.4.21";
167
186
 
168
187
  // src/snippet.ts
169
188
  var cacheContent = null;
@@ -6032,6 +6051,255 @@ function scanEntropyFiles(files, collect) {
6032
6051
  return findings;
6033
6052
  }
6034
6053
 
6054
+ // src/ai-status-constants.ts
6055
+ var AI_STATUSES = ["reviewed", "partial", "skipped", "failed", "unknown"];
6056
+ var AI_REASONS = [
6057
+ "no_api_key",
6058
+ "client_init",
6059
+ "capped",
6060
+ "budget_timeout",
6061
+ "model_bad_output",
6062
+ "model_error",
6063
+ "missing_content",
6064
+ "exception",
6065
+ "opted_out",
6066
+ "auth_failed",
6067
+ "jti_missing",
6068
+ "token_unverified",
6069
+ "pro_rules_unavailable",
6070
+ "engine_unavailable",
6071
+ "upload_failed",
6072
+ "upload_blocked",
6073
+ "upload_duplicate",
6074
+ "persist_failed",
6075
+ "quality_unavailable",
6076
+ "intent_unavailable",
6077
+ "not_supported",
6078
+ "token_secret_missing",
6079
+ "rate_limited",
6080
+ "jti_unavailable",
6081
+ "credit_error",
6082
+ "daily_limit",
6083
+ "signed_out"
6084
+ ];
6085
+ var AI_SURFACES = ["upload", "cli", "action", "app", "api", "mcp", "web", "save"];
6086
+ var PLAN_CAUSES = [
6087
+ "missing",
6088
+ "malformed",
6089
+ "expired",
6090
+ "revoked",
6091
+ "network",
6092
+ "upstream_5xx",
6093
+ "unknown"
6094
+ ];
6095
+ var WEBHOOK_REASONS = [
6096
+ "no_metadata_user",
6097
+ "malformed_user",
6098
+ "unknown_user",
6099
+ "user_bound_other_customer",
6100
+ "race_no_row",
6101
+ "deleted_unmatched",
6102
+ "clerk_lookup_failed"
6103
+ ];
6104
+ var WEBHOOK_EVENT_TYPES = [
6105
+ "checkout.session.completed",
6106
+ "customer.subscription.created",
6107
+ "customer.subscription.updated",
6108
+ "customer.subscription.deleted",
6109
+ "invoice.paid",
6110
+ "invoice.payment_failed",
6111
+ "created",
6112
+ "updated"
6113
+ ];
6114
+
6115
+ // src/ai-status.ts
6116
+ var PLAN_UNVERIFIED_NOTICE = "Couldn't verify your plan, scanned with free rules only.";
6117
+ var FAIL_REASONS = /* @__PURE__ */ new Set([
6118
+ "budget_timeout",
6119
+ "model_error",
6120
+ "model_bad_output",
6121
+ "auth_failed",
6122
+ "exception",
6123
+ "client_init"
6124
+ ]);
6125
+ function clampCount(raw) {
6126
+ if (typeof raw !== "number" || !Number.isFinite(raw) || raw < 0) return 0;
6127
+ return Math.min(Math.trunc(raw), 1e6);
6128
+ }
6129
+ function isStatus(raw) {
6130
+ return typeof raw === "string" && AI_STATUSES.includes(raw);
6131
+ }
6132
+ function isReason(raw) {
6133
+ return typeof raw === "string" && AI_REASONS.includes(raw);
6134
+ }
6135
+ function isSurface(raw) {
6136
+ return typeof raw === "string" && AI_SURFACES.includes(raw);
6137
+ }
6138
+ function bucketFiles(n) {
6139
+ if (typeof n !== "number" || !Number.isFinite(n) || n <= 0) return "0";
6140
+ const v = Math.trunc(n);
6141
+ if (v < 5) return "1-4";
6142
+ if (v < 20) return "5-19";
6143
+ return "20+";
6144
+ }
6145
+ function planCause(specific) {
6146
+ switch (specific) {
6147
+ case "secret_missing":
6148
+ return "missing";
6149
+ case "bad_shape":
6150
+ case "sig_mismatch":
6151
+ case "sig_length_mismatch":
6152
+ case "hmac_error":
6153
+ case "payload_parse_error":
6154
+ case "uid_or_tier_wrong_type":
6155
+ return "malformed";
6156
+ case "expired":
6157
+ case "exp_missing_or_nan":
6158
+ return "expired";
6159
+ case "tier_not_paid_unknown_value":
6160
+ case "http_401":
6161
+ case "http_403":
6162
+ return "revoked";
6163
+ case "no_cached_bundle_and_network_error":
6164
+ case "timeout":
6165
+ case "network":
6166
+ return "network";
6167
+ case "http_5xx":
6168
+ case "upstream_5xx":
6169
+ return "upstream_5xx";
6170
+ default:
6171
+ return "unknown";
6172
+ }
6173
+ }
6174
+ function toAiDegraded(input = {}) {
6175
+ const total = clampCount(input.total);
6176
+ let reviewed = clampCount(input.reviewed);
6177
+ let capped = clampCount(input.capped);
6178
+ if (reviewed > total) reviewed = total;
6179
+ if (reviewed + capped > total) capped = total - reviewed;
6180
+ const unreviewed = total - reviewed - capped;
6181
+ let status = isStatus(input.status) ? input.status : "unknown";
6182
+ let reason = isReason(input.reason) ? input.reason : null;
6183
+ const eligible = total - capped;
6184
+ if (status === "reviewed" && reviewed !== eligible) {
6185
+ status = reviewed === 0 && reason != null && FAIL_REASONS.has(reason) ? "failed" : "partial";
6186
+ }
6187
+ if (status === "reviewed" && unreviewed > 0) status = "partial";
6188
+ if (status === "skipped" && reason != null && FAIL_REASONS.has(reason)) status = "failed";
6189
+ if (status === "reviewed" || status === "unknown") {
6190
+ if (status === "unknown") reason = null;
6191
+ }
6192
+ const modelStatus = reason === "model_error" && typeof input.modelStatus === "number" && Number.isFinite(input.modelStatus) ? Math.trunc(input.modelStatus) : null;
6193
+ return {
6194
+ status,
6195
+ reason,
6196
+ surface: isSurface(input.surface) ? input.surface : "cli",
6197
+ total,
6198
+ reviewed,
6199
+ unreviewed,
6200
+ capped,
6201
+ modelStatus
6202
+ };
6203
+ }
6204
+ function unknownAiStatus(surface = "save") {
6205
+ return toAiDegraded({ status: "unknown", reason: null, surface, total: 0, reviewed: 0, unreviewed: 0, capped: 0 });
6206
+ }
6207
+ function aiStatusLine(d) {
6208
+ const shown = Math.max(0, d.total - d.capped);
6209
+ switch (d.reason) {
6210
+ case "no_api_key":
6211
+ return "AI review skipped: no Anthropic API key is set.";
6212
+ case "opted_out":
6213
+ return "AI review skipped: user opted out.";
6214
+ case "not_supported":
6215
+ return "AI review not run on MCP scans.";
6216
+ case "auth_failed":
6217
+ return "AI review failed: the Anthropic API key was rejected.";
6218
+ case "capped":
6219
+ return `AI review covered the first ${shown} of ${d.total} findings.`;
6220
+ case "budget_timeout":
6221
+ 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}).`;
6222
+ case "client_init":
6223
+ return "AI review failed: the AI client could not start.";
6224
+ case "model_bad_output":
6225
+ return "AI review failed: the model returned unusable output.";
6226
+ case "model_error":
6227
+ return `AI review failed: the model request was rejected (HTTP ${d.modelStatus ?? "error"}).`;
6228
+ case "missing_content":
6229
+ return "AI review was partial: source content was missing for some findings.";
6230
+ case "exception":
6231
+ return "AI review failed: an unexpected error occurred.";
6232
+ case "token_secret_missing":
6233
+ return "AI review skipped: scan token verification is not configured.";
6234
+ case "rate_limited":
6235
+ return "AI review skipped: rate limited.";
6236
+ case "jti_unavailable":
6237
+ return "AI review skipped: the review token could not be checked in time.";
6238
+ case "jti_missing":
6239
+ return "AI review skipped: the review token had no single-use id.";
6240
+ case "credit_error":
6241
+ return "AI review skipped: the review credit could not be spent.";
6242
+ case "daily_limit":
6243
+ return "AI review skipped: daily limit reached.";
6244
+ case "signed_out":
6245
+ return "AI review skipped: signed out.";
6246
+ case "quality_unavailable":
6247
+ return "Quality checks were unavailable for this scan.";
6248
+ case "intent_unavailable":
6249
+ return "Intent check was unavailable for this scan.";
6250
+ case "engine_unavailable":
6251
+ return "A scan engine was unavailable.";
6252
+ case "upload_failed":
6253
+ return "The scan could not be uploaded.";
6254
+ case "upload_blocked":
6255
+ return "The scan was not uploaded: the plan limit blocked it.";
6256
+ case "upload_duplicate":
6257
+ return "The scan was already uploaded for this job.";
6258
+ case "persist_failed":
6259
+ return "The scan status could not be stored.";
6260
+ case "token_unverified":
6261
+ case "pro_rules_unavailable":
6262
+ return PLAN_UNVERIFIED_NOTICE;
6263
+ default:
6264
+ if (d.status === "reviewed") return "AI review completed.";
6265
+ if (d.status === "unknown") return "AI review status is unknown.";
6266
+ if (d.status === "partial") return `AI review was partial: ${d.reviewed} of ${d.total} findings reviewed.`;
6267
+ if (d.status === "failed") return "AI review failed.";
6268
+ return "AI review skipped.";
6269
+ }
6270
+ }
6271
+ var SCAN_AI_DEGRADED_KEYS = [
6272
+ "status",
6273
+ "reason",
6274
+ "surface",
6275
+ "total",
6276
+ "reviewed",
6277
+ "capped",
6278
+ "files_received_bucket"
6279
+ ];
6280
+ function scanAiDegradedProps(d, filesReceived) {
6281
+ return {
6282
+ status: d.status,
6283
+ reason: d.reason,
6284
+ surface: d.surface,
6285
+ total: d.total,
6286
+ reviewed: d.reviewed,
6287
+ capped: d.capped,
6288
+ files_received_bucket: bucketFiles(filesReceived)
6289
+ };
6290
+ }
6291
+ var SCAN_TOKEN_NULL_KEYS = ["cause", "surface", "tier_expected"];
6292
+ function scanTokenNullProps(cause, surface) {
6293
+ const safeSurface = isSurface(surface) ? surface : "api";
6294
+ return { cause, surface: safeSurface, tier_expected: "paid" };
6295
+ }
6296
+ var BILLING_WEBHOOK_UNMATCHED_KEYS = ["type", "reason", "final"];
6297
+ function billingWebhookUnmatchedProps(input) {
6298
+ const reason = typeof input.reason === "string" && WEBHOOK_REASONS.includes(input.reason) ? input.reason : "race_no_row";
6299
+ const type = typeof input.type === "string" && input.type.length > 0 && input.type.length < 80 ? input.type : "unknown";
6300
+ return { type, reason, final: input.final === true };
6301
+ }
6302
+
6035
6303
  // src/ai-fp-filter.ts
6036
6304
  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.
6037
6305
 
@@ -6091,6 +6359,15 @@ var AI_FILTER_BUDGET_MS = (() => {
6091
6359
  return Math.min(n, 6e4);
6092
6360
  })();
6093
6361
  var PER_CALL_TIMEOUT_MS = 8e3;
6362
+ function perCallTimeoutMs(budgetMs) {
6363
+ const budget = Number.isFinite(budgetMs) && budgetMs >= 1 ? Math.min(Math.trunc(budgetMs), 6e4) : DEFAULT_AI_FILTER_BUDGET_MS;
6364
+ const scaled = Math.floor(budget * 0.6);
6365
+ const perCall = Math.min(PER_CALL_TIMEOUT_MS, scaled);
6366
+ return perCall;
6367
+ }
6368
+ if (!(perCallTimeoutMs(AI_FILTER_BUDGET_MS) < AI_FILTER_BUDGET_MS)) {
6369
+ throw new Error("per-call AI timeout must be strictly below the filter budget");
6370
+ }
6094
6371
  function isAbortError(err) {
6095
6372
  if (!err || typeof err !== "object") return false;
6096
6373
  const name = "name" in err ? String(err.name) : "";
@@ -6103,6 +6380,16 @@ function isTimeoutError(err) {
6103
6380
  const message = err instanceof Error ? err.message : String(err);
6104
6381
  return /\b(?:request|connection|operation|model call)\s+(?:has\s+)?timed out\b/i.test(message) || /\bETIMEDOUT\b/i.test(message);
6105
6382
  }
6383
+ function errorHttpStatus(err) {
6384
+ if (!err || typeof err !== "object") return null;
6385
+ const status = err.status;
6386
+ return typeof status === "number" && Number.isFinite(status) ? Math.trunc(status) : null;
6387
+ }
6388
+ function callWasAborted(err) {
6389
+ if (!err || typeof err !== "object") return false;
6390
+ const name = "name" in err ? String(err.name) : "";
6391
+ return name === "AbortError" || name === "APIUserAbortError";
6392
+ }
6106
6393
  var SEVERITY_PRIORITY = {
6107
6394
  critical: 0,
6108
6395
  high: 1,
@@ -6252,8 +6539,10 @@ function parseReviewResponse(text) {
6252
6539
  return null;
6253
6540
  }
6254
6541
  }
6255
- async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER_BUDGET_MS) {
6256
- const empty = {
6542
+ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER_BUDGET_MS, options) {
6543
+ const surface = options?.surface ?? "cli";
6544
+ const emptyMask = { prompt_batches: 0, prompt_chars: 0, masked_count: 0, had_secret_pattern: false, raw_after_mask: 0 };
6545
+ const early = (status2, reason2, extra) => ({
6257
6546
  findings,
6258
6547
  filteredFindings: [],
6259
6548
  policyRetainedFindings: [],
@@ -6261,15 +6550,27 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6261
6550
  removedCount: 0,
6262
6551
  policyRetainedCount: 0,
6263
6552
  totalBefore: findings.length,
6264
- reviewedCount: 0,
6265
- unreviewedCount: findings.length,
6266
- cappedCount: 0,
6553
+ reviewedCount: extra?.reviewed ?? 0,
6554
+ unreviewedCount: extra?.unreviewed ?? findings.length,
6555
+ cappedCount: extra?.capped ?? 0,
6267
6556
  missingContentCount: 0,
6268
6557
  timedOut: false,
6269
- maskStats: { prompt_batches: 0, prompt_chars: 0, masked_count: 0, had_secret_pattern: false, raw_after_mask: 0 }
6270
- };
6271
- if (findings.length === 0) return empty;
6272
- if (!process.env.ANTHROPIC_API_KEY) return empty;
6558
+ maskStats: emptyMask,
6559
+ aiDegraded: toAiDegraded({
6560
+ status: status2,
6561
+ reason: reason2,
6562
+ surface,
6563
+ total: findings.length,
6564
+ reviewed: extra?.reviewed ?? 0,
6565
+ unreviewed: extra?.unreviewed ?? findings.length,
6566
+ capped: extra?.capped ?? 0,
6567
+ modelStatus: extra?.modelStatus ?? null
6568
+ })
6569
+ });
6570
+ if (findings.length === 0) {
6571
+ return early("reviewed", null, { reviewed: 0, unreviewed: 0, capped: 0 });
6572
+ }
6573
+ if (!process.env.ANTHROPIC_API_KEY) return early("skipped", "no_api_key");
6273
6574
  const deadlineMs = Date.now() + Math.max(0, budgetMs);
6274
6575
  const abortController = new AbortController();
6275
6576
  let budgetExpired = budgetMs <= 0;
@@ -6299,12 +6600,20 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6299
6600
  try {
6300
6601
  client = new import_sdk.default();
6301
6602
  } catch {
6302
- return empty;
6603
+ if (budgetTimer) clearTimeout(budgetTimer);
6604
+ return early("failed", "client_init", {
6605
+ reviewed: 0,
6606
+ capped: overflow.length,
6607
+ unreviewed: findings.length - overflow.length
6608
+ });
6303
6609
  }
6304
6610
  const fpMap = /* @__PURE__ */ new Map();
6305
6611
  const policyRetainedMap = /* @__PURE__ */ new Map();
6306
6612
  const reviewedIndices = /* @__PURE__ */ new Set();
6307
6613
  let firstError = null;
6614
+ let firstErrorStatus = null;
6615
+ let authFailed = false;
6616
+ let badOutput = false;
6308
6617
  const maskStats = { prompt_batches: 0, prompt_chars: 0, masked_count: 0, had_secret_pattern: false, raw_after_mask: 0 };
6309
6618
  let failedBatches = 0;
6310
6619
  let timedOutCount = 0;
@@ -6330,9 +6639,11 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6330
6639
  const reviewBatch = async (task) => {
6331
6640
  const { targets, content, detections } = task;
6332
6641
  const batch = targets.map((target) => target.finding);
6333
- if (abortController.signal.aborted || Date.now() >= deadlineMs) {
6334
- budgetExpired = true;
6335
- timedOutCount += batch.length;
6642
+ if (authFailed || abortController.signal.aborted || Date.now() >= deadlineMs) {
6643
+ if (!authFailed) {
6644
+ budgetExpired = true;
6645
+ timedOutCount += batch.length;
6646
+ }
6336
6647
  return;
6337
6648
  }
6338
6649
  try {
@@ -6344,7 +6655,7 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6344
6655
  maskStats.masked_count += proof.detected - proof.raw;
6345
6656
  maskStats.raw_after_mask += proof.raw;
6346
6657
  if (proof.detected > 0) maskStats.had_secret_pattern = true;
6347
- console.info(
6658
+ console.error(
6348
6659
  `[ai-fp-filter] batch prompt ${prompt.length} chars (~${Math.ceil(prompt.length / 4)} tokens), ${batch.length} finding(s)`
6349
6660
  );
6350
6661
  if (prompt.length > MAX_BATCH_PROMPT_CHARS) {
@@ -6352,38 +6663,52 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6352
6663
  `[ai-fp-filter] batch prompt ${prompt.length} chars exceeds ${MAX_BATCH_PROMPT_CHARS} cap (single oversized finding after truncation)`
6353
6664
  );
6354
6665
  }
6355
- const response = await client.messages.create(
6356
- {
6357
- model: MODEL_TRIAGE,
6358
- max_tokens: 2048,
6359
- // Haiku 5.5 rejects non-default sampling params with HTTP 400
6360
- // ("temperature is deprecated for this model"). Omit temperature /
6361
- // top_p / top_k entirely — do NOT send MODEL_TEMPERATURE (0).
6362
- // Adaptive thinking is on by default for 5.5; disable it here: this
6363
- // is binary FP classification, not open-ended analysis, and thinking
6364
- // burns the shared AI budget (Isobar rescan: every call 400'd on
6365
- // temperature, then timed_out with aiReviewed:false → grade D).
6366
- thinking: { type: "disabled" },
6367
- system: REVIEW_SYSTEM_PROMPT,
6368
- messages: [{ role: "user", content: prompt }]
6369
- },
6370
- // Shared AbortController plus a per-call timeout capped at remaining
6371
- // budget. Previously 20s with no overall deadline.
6372
- { timeout: Math.min(PER_CALL_TIMEOUT_MS, remaining), signal: abortController.signal }
6373
- );
6666
+ const perCall = perCallTimeoutMs(budgetMs);
6667
+ const callTimeout = Math.min(perCall, remaining);
6668
+ const callAbort = new AbortController();
6669
+ const onParentAbort = () => callAbort.abort();
6670
+ if (abortController.signal.aborted) callAbort.abort();
6671
+ else abortController.signal.addEventListener("abort", onParentAbort, { once: true });
6672
+ const callTimer = setTimeout(() => callAbort.abort(), callTimeout);
6673
+ let response;
6674
+ try {
6675
+ response = await client.messages.create(
6676
+ {
6677
+ model: MODEL_TRIAGE,
6678
+ max_tokens: 2048,
6679
+ // Haiku 5.5 rejects non-default sampling params with HTTP 400
6680
+ // ("temperature is deprecated for this model"). Omit temperature /
6681
+ // top_p / top_k entirely — do NOT send MODEL_TEMPERATURE (0).
6682
+ // Adaptive thinking is on by default for 5.5; disable it here: this
6683
+ // is binary FP classification, not open-ended analysis, and thinking
6684
+ // burns the shared AI budget (Isobar rescan: every call 400'd on
6685
+ // temperature, then timed_out with aiReviewed:false → grade D).
6686
+ thinking: { type: "disabled" },
6687
+ system: REVIEW_SYSTEM_PROMPT,
6688
+ messages: [{ role: "user", content: prompt }]
6689
+ },
6690
+ { timeout: callTimeout, signal: callAbort.signal }
6691
+ );
6692
+ } finally {
6693
+ clearTimeout(callTimer);
6694
+ abortController.signal.removeEventListener("abort", onParentAbort);
6695
+ }
6374
6696
  const text = response.content.filter((b) => b.type === "text").map((b) => b.text).join("");
6375
6697
  const results = parseReviewResponse(text);
6376
- if (!results) throw new Error("AI review returned malformed JSON");
6698
+ if (!results) {
6699
+ badOutput = true;
6700
+ throw new Error("AI review returned malformed JSON");
6701
+ }
6377
6702
  for (const r of results) {
6378
6703
  if (r.index >= batch.length) continue;
6379
6704
  const target = targets[r.index];
6380
6705
  reviewedIndices.add(target.originalIndex);
6381
6706
  if (r.verdict !== "fp") continue;
6382
- const reason = truncateChars(applySecretMasks(r.reason, secrets), MAX_FIELD_CHARS);
6707
+ const reason2 = truncateChars(applySecretMasks(r.reason, secrets), MAX_FIELD_CHARS);
6383
6708
  if (isProtectedFromAiRemoval(target.finding)) {
6384
- policyRetainedMap.set(target.originalIndex, reason);
6709
+ policyRetainedMap.set(target.originalIndex, reason2);
6385
6710
  } else {
6386
- fpMap.set(target.originalIndex, reason);
6711
+ fpMap.set(target.originalIndex, reason2);
6387
6712
  }
6388
6713
  }
6389
6714
  if (results.filter((result) => result.index < batch.length).length < batch.length) {
@@ -6391,21 +6716,35 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6391
6716
  if (!firstError) firstError = new Error("AI review returned incomplete verdicts");
6392
6717
  }
6393
6718
  } catch (err) {
6394
- const callTimedOut = isAbortError(err) || isTimeoutError(err) || abortController.signal.aborted;
6719
+ const status2 = errorHttpStatus(err);
6720
+ if (status2 === 401 || status2 === 403) {
6721
+ authFailed = true;
6722
+ failedBatches++;
6723
+ if (!firstError) {
6724
+ firstError = err;
6725
+ firstErrorStatus = status2;
6726
+ }
6727
+ console.error(`[ai-fp-filter] AI review failed: auth status ${status2}`);
6728
+ return;
6729
+ }
6730
+ const callTimedOut = isAbortError(err) || isTimeoutError(err) || abortController.signal.aborted || callWasAborted(err);
6395
6731
  if (callTimedOut) {
6396
6732
  timedOutCount += batch.length;
6397
6733
  budgetExpired = true;
6398
6734
  return;
6399
6735
  }
6400
6736
  failedBatches++;
6401
- if (!firstError) firstError = err;
6737
+ if (!firstError) {
6738
+ firstError = err;
6739
+ firstErrorStatus = status2;
6740
+ }
6402
6741
  }
6403
6742
  };
6404
6743
  try {
6405
6744
  let next = 0;
6406
6745
  const workerCount = Math.min(MAX_CONCURRENT, tasks.length);
6407
6746
  const workers = Array.from({ length: workerCount }, async () => {
6408
- while (next < tasks.length) {
6747
+ while (next < tasks.length && !authFailed) {
6409
6748
  const cur = next++;
6410
6749
  await reviewBatch(tasks[cur]);
6411
6750
  }
@@ -6423,28 +6762,70 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6423
6762
  `[ai-fp-filter] budget expired or model call timed out \u2014 ${timedOutCount} finding(s) not reviewed; returning them unchanged`
6424
6763
  );
6425
6764
  }
6426
- if (firstError) {
6427
- const msg = firstError instanceof Error ? firstError.message : String(firstError);
6765
+ if (firstError && !authFailed) {
6766
+ const statusLabel = firstErrorStatus == null ? "none" : String(firstErrorStatus);
6428
6767
  if (!reviewedAny) {
6429
6768
  console.error(
6430
- "[ai-fp-filter] AI review unavailable \u2014 every model call failed; returning unreviewed findings:",
6431
- msg
6769
+ `[ai-fp-filter] AI review unavailable \u2014 every model call failed; status ${statusLabel}`
6432
6770
  );
6433
6771
  } else {
6434
6772
  console.warn(
6435
- `[ai-fp-filter] partial AI review \u2014 ${failedBatches} of ${tasks.length} batch(es) failed; those findings were not reviewed:`,
6436
- msg
6773
+ `[ai-fp-filter] partial AI review \u2014 ${failedBatches} of ${tasks.length} batch(es) failed; status ${statusLabel}`
6437
6774
  );
6438
6775
  }
6439
6776
  }
6440
6777
  const filtered = findings.filter((_, index) => !fpMap.has(index));
6441
6778
  const filteredFindings = [];
6442
- for (const [index, reason] of [...fpMap].sort((a, b) => a[0] - b[0])) {
6443
- filteredFindings.push({ finding: findings[index], reason });
6779
+ for (const [index, reason2] of [...fpMap].sort((a, b) => a[0] - b[0])) {
6780
+ filteredFindings.push({ finding: findings[index], reason: reason2 });
6444
6781
  }
6445
6782
  const policyRetainedFindings = [];
6446
- for (const [index, reason] of [...policyRetainedMap].sort((a, b) => a[0] - b[0])) {
6447
- policyRetainedFindings.push({ finding: findings[index], reason });
6783
+ for (const [index, reason2] of [...policyRetainedMap].sort((a, b) => a[0] - b[0])) {
6784
+ policyRetainedFindings.push({ finding: findings[index], reason: reason2 });
6785
+ }
6786
+ const cappedCount = overflow.length;
6787
+ const eligible = totalBefore - cappedCount;
6788
+ let reason = null;
6789
+ let status = "reviewed";
6790
+ let modelStatus = null;
6791
+ if (authFailed) {
6792
+ status = "failed";
6793
+ reason = "auth_failed";
6794
+ } else if (reviewedCount === 0 && (timedOut || budgetExpired && tasks.length > 0)) {
6795
+ status = "failed";
6796
+ reason = "budget_timeout";
6797
+ } else if (reviewedCount === 0 && badOutput) {
6798
+ status = "failed";
6799
+ reason = "model_bad_output";
6800
+ } else if (reviewedCount === 0 && firstErrorStatus != null) {
6801
+ status = "failed";
6802
+ reason = "model_error";
6803
+ modelStatus = firstErrorStatus;
6804
+ } else if (reviewedCount === 0 && firstError) {
6805
+ status = "failed";
6806
+ reason = "exception";
6807
+ } else if (reviewedCount === eligible && cappedCount > 0 && missingContentCount === 0) {
6808
+ status = "partial";
6809
+ reason = "capped";
6810
+ } else if (reviewedCount === eligible && missingContentCount === 0 && !firstError && !timedOut) {
6811
+ status = "reviewed";
6812
+ reason = null;
6813
+ } else if (timedOut) {
6814
+ status = reviewedCount === 0 ? "failed" : "partial";
6815
+ reason = "budget_timeout";
6816
+ } else if (missingContentCount > 0 && !firstError && !badOutput) {
6817
+ status = reviewedCount === 0 ? "partial" : "partial";
6818
+ reason = "missing_content";
6819
+ } else if (badOutput) {
6820
+ status = reviewedCount === 0 ? "failed" : "partial";
6821
+ reason = "model_bad_output";
6822
+ } else if (firstErrorStatus != null) {
6823
+ status = "partial";
6824
+ reason = "model_error";
6825
+ modelStatus = firstErrorStatus;
6826
+ } else if (firstError || reviewedCount < eligible) {
6827
+ status = reviewedCount === 0 ? "failed" : "partial";
6828
+ reason = "exception";
6448
6829
  }
6449
6830
  return {
6450
6831
  findings: filtered,
@@ -6456,10 +6837,20 @@ async function filterFalsePositives(findings, fileContents, budgetMs = AI_FILTER
6456
6837
  totalBefore,
6457
6838
  reviewedCount,
6458
6839
  unreviewedCount,
6459
- cappedCount: overflow.length,
6840
+ cappedCount,
6460
6841
  missingContentCount,
6461
6842
  timedOut,
6462
- maskStats
6843
+ maskStats,
6844
+ aiDegraded: toAiDegraded({
6845
+ status,
6846
+ reason,
6847
+ surface,
6848
+ total: totalBefore,
6849
+ reviewed: reviewedCount,
6850
+ unreviewed: totalBefore - reviewedCount - cappedCount,
6851
+ capped: cappedCount,
6852
+ modelStatus
6853
+ })
6463
6854
  };
6464
6855
  }
6465
6856
 
@@ -11102,6 +11493,10 @@ function scanQuality(input, rules = qualityRules) {
11102
11493
  }
11103
11494
  // Annotate the CommonJS export names for ESM import in node:
11104
11495
  0 && (module.exports = {
11496
+ AI_REASONS,
11497
+ AI_STATUSES,
11498
+ AI_SURFACES,
11499
+ BILLING_WEBHOOK_UNMATCHED_KEYS,
11105
11500
  CHECKABLE_CLAIMS,
11106
11501
  DOTENV_ASSIGN_RE,
11107
11502
  FREE_RULE_COUNT,
@@ -11113,17 +11508,26 @@ function scanQuality(input, rules = qualityRules) {
11113
11508
  MODEL_TEMPERATURE,
11114
11509
  MODEL_TRIAGE,
11115
11510
  NON_APP_PATH_PATTERNS,
11511
+ PLAN_CAUSES,
11512
+ PLAN_UNVERIFIED_NOTICE,
11116
11513
  PRO_RULE_COUNT,
11117
11514
  PUBLIC_ENV_PREFIX_RE,
11118
11515
  QUALITY_EXAMPLES,
11119
11516
  RULE_IMPACTS,
11120
11517
  SCANNER_VERSION,
11518
+ SCAN_AI_DEGRADED_KEYS,
11519
+ SCAN_TOKEN_NULL_KEYS,
11121
11520
  SERVER_ACTION_WRITE_RE,
11122
11521
  TEST_FILE_PATTERN,
11123
11522
  TOTAL_RULE_COUNT,
11523
+ WEBHOOK_EVENT_TYPES,
11524
+ WEBHOOK_REASONS,
11525
+ aiStatusLine,
11124
11526
  allRules,
11125
11527
  assessHeaders,
11126
11528
  astMatch,
11529
+ billingWebhookUnmatchedProps,
11530
+ bucketFiles,
11127
11531
  buildProjectContext,
11128
11532
  buildQualityContext,
11129
11533
  buildReviewPrompt,
@@ -11208,21 +11612,27 @@ function scanQuality(input, rules = qualityRules) {
11208
11612
  parseFile,
11209
11613
  parseScanTarget,
11210
11614
  parseSupabaseConfig,
11615
+ perCallTimeoutMs,
11616
+ planCause,
11211
11617
  qualityRules,
11212
11618
  replaceBlockComments,
11213
11619
  routeForFile,
11214
11620
  runCustomRules,
11621
+ scanAiDegradedProps,
11215
11622
  scanConfigs,
11216
11623
  scanDependencies,
11217
11624
  scanEntropy,
11218
11625
  scanMultiFile,
11219
11626
  scanQuality,
11627
+ scanTokenNullProps,
11220
11628
  sqlInjection,
11221
11629
  stripeWebhookUnprotected,
11222
11630
  supabaseFunctionName,
11223
11631
  supabaseNoRLS,
11632
+ toAiDegraded,
11224
11633
  todoLeftInCode,
11225
11634
  tryParse,
11635
+ unknownAiStatus,
11226
11636
  unmeasuredQualityRules,
11227
11637
  unvalidatedRedirect,
11228
11638
  vc001TestFixtureSeverity,