@apifuse/provider-sdk 2.2.0-beta.1 → 2.2.0-beta.10

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 (203) hide show
  1. package/AUTHORING.md +106 -0
  2. package/CHANGELOG.md +37 -0
  3. package/README.md +5 -1
  4. package/SUBMISSION.md +1 -1
  5. package/bin/apifuse-check.ts +44 -59
  6. package/bin/apifuse-create.ts +1 -1
  7. package/bin/apifuse-dev.ts +27 -52
  8. package/bin/apifuse-pack-check.ts +14 -0
  9. package/bin/apifuse-pack-smoke.ts +22 -81
  10. package/bin/apifuse-pack-types.ts +266 -0
  11. package/bin/apifuse-perf.ts +45 -127
  12. package/bin/apifuse-record.ts +53 -70
  13. package/bin/apifuse-submit-check.ts +510 -45
  14. package/bin/apifuse-sync-assets.ts +117 -0
  15. package/bin/apifuse.ts +1 -1
  16. package/bin/submit-check-delimited-text.ts +50 -0
  17. package/bin/submit-check-xml-semantics.ts +204 -0
  18. package/bin/submit-check-xml.ts +134 -0
  19. package/dist/auth-turn/index.d.ts +3 -3
  20. package/dist/auth.d.ts +2 -2
  21. package/dist/auth.js +9 -18
  22. package/dist/ceremonies/index.d.ts +1 -1
  23. package/dist/ceremonies/index.js +8 -18
  24. package/dist/cli/commands.d.ts +1 -1
  25. package/dist/cli/commands.js +8 -0
  26. package/dist/cli/create.d.ts +3 -0
  27. package/dist/cli/create.js +34 -35
  28. package/dist/cli/prompt-assets.d.ts +80 -0
  29. package/dist/cli/prompt-assets.js +743 -0
  30. package/dist/cli/templates/provider/AGENTS.md.tpl +17 -8
  31. package/dist/config/loader.d.ts +149 -8
  32. package/dist/config/loader.js +378 -89
  33. package/dist/contract-serialization.d.ts +2 -2
  34. package/dist/contract-serialization.js +3 -6
  35. package/dist/contract-types.d.ts +2 -2
  36. package/dist/contract.d.ts +3 -3
  37. package/dist/contract.js +4 -6
  38. package/dist/define.d.ts +8 -1
  39. package/dist/define.js +119 -122
  40. package/dist/dev.d.ts +1 -1
  41. package/dist/dev.js +1 -1
  42. package/dist/errors.d.ts +4 -1
  43. package/dist/errors.js +48 -0
  44. package/dist/i18n/catalog.d.ts +2 -2
  45. package/dist/i18n/catalog.js +4 -10
  46. package/dist/i18n/index.d.ts +2 -2
  47. package/dist/i18n/index.js +2 -2
  48. package/dist/i18n/keys.d.ts +2 -2
  49. package/dist/index.d.ts +44 -42
  50. package/dist/index.js +39 -37
  51. package/dist/lint.js +8 -15
  52. package/dist/provider.d.ts +11 -11
  53. package/dist/provider.js +9 -9
  54. package/dist/public-schema-field-lint.d.ts +1 -1
  55. package/dist/recipes/gov-api.js +1 -1
  56. package/dist/runtime/auth-flow.d.ts +1 -1
  57. package/dist/runtime/auth-flow.js +3 -3
  58. package/dist/runtime/browser.d.ts +1 -1
  59. package/dist/runtime/browser.js +15 -29
  60. package/dist/runtime/cache.d.ts +1 -1
  61. package/dist/runtime/cache.js +4 -8
  62. package/dist/runtime/choice.d.ts +1 -1
  63. package/dist/runtime/choice.js +31 -35
  64. package/dist/runtime/credential.d.ts +1 -1
  65. package/dist/runtime/credential.js +1 -1
  66. package/dist/runtime/env.d.ts +1 -1
  67. package/dist/runtime/executor.d.ts +1 -1
  68. package/dist/runtime/executor.js +15 -3
  69. package/dist/runtime/http.d.ts +2 -2
  70. package/dist/runtime/http.js +144 -348
  71. package/dist/runtime/insights.d.ts +1 -1
  72. package/dist/runtime/insights.js +6 -13
  73. package/dist/runtime/instrumentation.d.ts +2 -2
  74. package/dist/runtime/instrumentation.js +56 -19
  75. package/dist/runtime/keyring.js +1 -1
  76. package/dist/runtime/namespace.js +1 -1
  77. package/dist/runtime/otlp.d.ts +1 -1
  78. package/dist/runtime/perf.d.ts +1 -1
  79. package/dist/runtime/provider.d.ts +1 -1
  80. package/dist/runtime/provider.js +1 -2
  81. package/dist/runtime/proxy-errors.d.ts +1 -1
  82. package/dist/runtime/proxy-errors.js +9 -7
  83. package/dist/runtime/proxy-nodemaven.d.ts +35 -0
  84. package/dist/runtime/proxy-nodemaven.js +128 -0
  85. package/dist/runtime/proxy-retry-policy.d.ts +40 -0
  86. package/dist/runtime/proxy-retry-policy.js +326 -0
  87. package/dist/runtime/proxy-telemetry.d.ts +2 -1
  88. package/dist/runtime/proxy-telemetry.js +55 -52
  89. package/dist/runtime/redis.d.ts +1 -1
  90. package/dist/runtime/redis.js +2 -4
  91. package/dist/runtime/request-options.d.ts +1 -1
  92. package/dist/runtime/secrets.d.ts +27 -0
  93. package/dist/runtime/secrets.js +51 -0
  94. package/dist/runtime/state.d.ts +2 -2
  95. package/dist/runtime/state.js +15 -4
  96. package/dist/runtime/stealth.d.ts +2 -2
  97. package/dist/runtime/stealth.js +108 -212
  98. package/dist/runtime/stt.d.ts +1 -1
  99. package/dist/runtime/stt.js +11 -15
  100. package/dist/runtime/trace.d.ts +2 -2
  101. package/dist/runtime/trace.js +2 -4
  102. package/dist/runtime/waterfall.d.ts +1 -1
  103. package/dist/schema.d.ts +1 -1
  104. package/dist/schema.js +7 -15
  105. package/dist/serve.d.ts +1 -1
  106. package/dist/serve.js +1 -1
  107. package/dist/server/index.d.ts +7 -7
  108. package/dist/server/index.js +6 -6
  109. package/dist/server/self-test-redaction.d.ts +1 -1
  110. package/dist/server/self-test-redaction.js +1 -1
  111. package/dist/server/self-test.d.ts +104 -3
  112. package/dist/server/self-test.js +673 -115
  113. package/dist/server/serve.d.ts +8 -3
  114. package/dist/server/serve.js +105 -59
  115. package/dist/server/types.d.ts +10 -9
  116. package/dist/server/types.js +3 -7
  117. package/dist/stealth/profiles.d.ts +1 -1
  118. package/dist/stealth/profiles.js +5 -14
  119. package/dist/stream.d.ts +1 -1
  120. package/dist/testing/index.d.ts +2 -2
  121. package/dist/testing/index.js +2 -2
  122. package/dist/testing/run.d.ts +1 -1
  123. package/dist/testing/run.js +7 -14
  124. package/dist/types.d.ts +87 -1
  125. package/dist/user-input.d.ts +30 -0
  126. package/dist/user-input.js +66 -0
  127. package/package.json +9 -5
  128. package/src/auth-turn/index.ts +1 -1
  129. package/src/auth.ts +28 -86
  130. package/src/ceremonies/index.ts +24 -78
  131. package/src/cli/commands.ts +10 -0
  132. package/src/cli/create.ts +42 -35
  133. package/src/cli/prompt-assets.ts +865 -0
  134. package/src/cli/templates/provider/AGENTS.md.tpl +17 -8
  135. package/src/config/loader.ts +543 -208
  136. package/src/contract-serialization.ts +5 -11
  137. package/src/contract-types.ts +2 -2
  138. package/src/contract.ts +12 -28
  139. package/src/define.ts +247 -505
  140. package/src/dev.ts +4 -9
  141. package/src/errors.ts +58 -5
  142. package/src/i18n/catalog.ts +10 -32
  143. package/src/i18n/index.ts +2 -2
  144. package/src/i18n/keys.ts +5 -11
  145. package/src/index.ts +49 -42
  146. package/src/lint.ts +89 -159
  147. package/src/provider.ts +15 -11
  148. package/src/public-schema-field-lint.ts +7 -33
  149. package/src/recipes/gov-api.ts +2 -5
  150. package/src/runtime/auth-flow.ts +5 -7
  151. package/src/runtime/browser.ts +64 -187
  152. package/src/runtime/cache.ts +20 -67
  153. package/src/runtime/choice.ts +79 -132
  154. package/src/runtime/credential.ts +2 -2
  155. package/src/runtime/env.ts +1 -1
  156. package/src/runtime/executor.ts +23 -20
  157. package/src/runtime/http.ts +195 -559
  158. package/src/runtime/insights.ts +15 -53
  159. package/src/runtime/instrumentation.ts +78 -65
  160. package/src/runtime/keyring.ts +7 -19
  161. package/src/runtime/namespace.ts +2 -7
  162. package/src/runtime/otlp.ts +12 -23
  163. package/src/runtime/perf.ts +1 -1
  164. package/src/runtime/provider.ts +4 -9
  165. package/src/runtime/proxy-errors.ts +29 -42
  166. package/src/runtime/proxy-nodemaven.ts +178 -0
  167. package/src/runtime/proxy-retry-policy.ts +469 -0
  168. package/src/runtime/proxy-telemetry.ts +79 -77
  169. package/src/runtime/redis.ts +4 -12
  170. package/src/runtime/request-options.ts +4 -13
  171. package/src/runtime/secrets.ts +64 -0
  172. package/src/runtime/state.ts +41 -110
  173. package/src/runtime/stealth.ts +162 -377
  174. package/src/runtime/stt.ts +38 -94
  175. package/src/runtime/trace.ts +14 -44
  176. package/src/runtime/waterfall.ts +5 -18
  177. package/src/schema.ts +23 -84
  178. package/src/serve.ts +1 -1
  179. package/src/server/index.ts +12 -7
  180. package/src/server/self-test-redaction.ts +2 -2
  181. package/src/server/self-test.ts +857 -132
  182. package/src/server/serve.ts +172 -245
  183. package/src/server/types.ts +7 -19
  184. package/src/stealth/profiles.ts +10 -26
  185. package/src/stream.ts +8 -19
  186. package/src/testing/index.ts +2 -2
  187. package/src/testing/run.ts +19 -67
  188. package/src/types.ts +88 -1
  189. package/src/user-input.ts +118 -0
  190. package/dist/cli/templates/provider/CLAUDE.md.tpl +0 -1
  191. package/src/cli/templates/provider/CLAUDE.md.tpl +0 -1
  192. /package/dist/cli/templates/provider/{skills → .agents/skills}/fixtures-and-recording/SKILL.md.tpl +0 -0
  193. /package/dist/cli/templates/provider/{skills → .agents/skills}/health-checks-and-fail-closed/SKILL.md.tpl +0 -0
  194. /package/dist/cli/templates/provider/{skills → .agents/skills}/normalization-standards/SKILL.md.tpl +0 -0
  195. /package/dist/cli/templates/provider/{skills → .agents/skills}/pagination-and-counts/SKILL.md.tpl +0 -0
  196. /package/dist/cli/templates/provider/{skills → .agents/skills}/upstream-contract-verification/SKILL.md.tpl +0 -0
  197. /package/dist/cli/templates/provider/{skills → .agents/skills}/upstream-notes/README.md.tpl +0 -0
  198. /package/src/cli/templates/provider/{skills → .agents/skills}/fixtures-and-recording/SKILL.md.tpl +0 -0
  199. /package/src/cli/templates/provider/{skills → .agents/skills}/health-checks-and-fail-closed/SKILL.md.tpl +0 -0
  200. /package/src/cli/templates/provider/{skills → .agents/skills}/normalization-standards/SKILL.md.tpl +0 -0
  201. /package/src/cli/templates/provider/{skills → .agents/skills}/pagination-and-counts/SKILL.md.tpl +0 -0
  202. /package/src/cli/templates/provider/{skills → .agents/skills}/upstream-contract-verification/SKILL.md.tpl +0 -0
  203. /package/src/cli/templates/provider/{skills → .agents/skills}/upstream-notes/README.md.tpl +0 -0
@@ -11,15 +11,20 @@ import * as acorn from "acorn";
11
11
  import { z } from "zod";
12
12
 
13
13
  import packageJson from "../package.json";
14
- import type { ProviderDefinition } from "../src";
14
+ import {
15
+ formatPromptAssetIssues,
16
+ verifyPromptAssets,
17
+ } from "../src/cli/prompt-assets.js";
15
18
  import {
16
19
  loadProviderLocaleCatalogs,
17
20
  type ProviderLocale,
18
21
  validateProviderLocaleCatalogs,
19
- } from "../src/i18n";
20
- import { APIFUSE_DESCRIPTION_KEY_META_KEY } from "../src/schema";
21
- import { safeParseSchemaSync } from "../src/schema";
22
- import { type CheckResult, runChecks } from "./apifuse-check";
22
+ } from "../src/i18n/index.js";
23
+ import type { ProviderDefinition } from "../src/index.js";
24
+ import { APIFUSE_DESCRIPTION_KEY_META_KEY, safeParseSchemaSync } from "../src/schema.js";
25
+ import { type CheckResult, PROMPT_ASSETS_CHECK_MESSAGE, runChecks } from "./apifuse-check.js";
26
+ import { hasSubstantiveDelimitedTextStructure } from "./submit-check-delimited-text.js";
27
+ import { hasSubstantiveXmlStructure } from "./submit-check-xml.js";
23
28
 
24
29
  const TIERS = ["bronze", "silver", "gold", "diamond"] as const;
25
30
  const TIER_VALUES: ReadonlySet<string> = new Set(TIERS);
@@ -266,7 +271,15 @@ export async function buildSubmitCheckReport(
266
271
  const baseChecks = await safeRunChecks(providerRoot);
267
272
  const provider = await safeLoadProvider(providerRoot);
268
273
 
269
- checks.push(...scoreBaseChecks(baseChecks));
274
+ // Prompt-asset freshness is reported by its own dedicated zero-point
275
+ // blocker below; filter the base-check duplicate so it is not double
276
+ // penalized under the definition category.
277
+ checks.push(
278
+ ...scoreBaseChecks(
279
+ baseChecks.filter((result) => result.message !== PROMPT_ASSETS_CHECK_MESSAGE),
280
+ ),
281
+ );
282
+ checks.push(scorePromptAssetFreshness(providerRoot));
270
283
  checks.push(scoreProviderIdSlug(providerRoot, provider));
271
284
  checks.push(scoreNoVendorShim(providerRoot));
272
285
  checks.push(scoreNoVendorImport(providerRoot));
@@ -282,6 +295,7 @@ export async function buildSubmitCheckReport(
282
295
  if (provider) {
283
296
  const smokeResult = args.smoke ? await runSubmitCheckSmoke(providerRoot, provider) : undefined;
284
297
  checks.push(scoreCredentialUsage(providerRoot, provider));
298
+ checks.push(scoreSdkOwnedSecretPresence(providerRoot, provider));
285
299
  checks.push(scoreLocaleCatalog(providerRoot, provider));
286
300
  checks.push(scoreOperationMetadata(provider));
287
301
  checks.push(scoreFixtureCoverage(provider));
@@ -1423,6 +1437,159 @@ function scoreCredentialUsage(providerRoot: string, provider: ProviderDefinition
1423
1437
  );
1424
1438
  }
1425
1439
 
1440
+ // ---------------------------------------------------------------------------
1441
+ // sdk-owned-secret-presence (warn): provider-local double validation of
1442
+ // declared env secrets.
1443
+ //
1444
+ // The SDK runtime is the single source of truth for secret presence: declared
1445
+ // `required: true` secrets are validated before every handler/auth-flow
1446
+ // invocation and fail with the canonical structured MISSING_SECRET error
1447
+ // (HTTP 400, category credential_unavailable). Provider-local presence guards
1448
+ // (requireServiceKey/requireApiKey style) are dead weight that historically
1449
+ // diverged into inconsistent shapes (CONFIGURATION_ERROR vs MISSING_SECRET,
1450
+ // with/without category), which broke uniform incident attribution when nine
1451
+ // providers shipped with unprovisioned secrets (2026-07-22).
1452
+ //
1453
+ // Heuristic, warn-only: a line reading a declared `required: true` secret via
1454
+ // `.env.get(...)` (string literal or a const alias of a declared name)
1455
+ // followed within a small window by a falsy presence check plus a `throw`.
1456
+ // The rule flags duplication of the SDK gate ONLY: env names that are not
1457
+ // declared in defineProvider secrets[], and optional declarations
1458
+ // (`required: false`/omitted) that the runtime deliberately does not enforce,
1459
+ // are out of scope. Escape hatch:
1460
+ // `// @apifuse-allow sdk-owned-secret-presence: <reason>`.
1461
+ // ---------------------------------------------------------------------------
1462
+
1463
+ const SDK_OWNED_SECRET_PRESENCE_RULE_ID = "sdk-owned-secret-presence";
1464
+ const SECRET_PRESENCE_GUARD_LOOKAHEAD_LINES = 10;
1465
+
1466
+ const ENV_GET_CALL_PATTERN =
1467
+ /\.env\.get\(\s*(?:"([^"]+)"|'([^']+)'|`([^`$]+)`|([A-Za-z_$][\w$]*))\s*\)/;
1468
+
1469
+ const SECRET_ALIAS_CONST_PATTERN =
1470
+ /\bconst\s+([A-Za-z_$][\w$]*)\s*=\s*(?:"([^"]+)"|'([^']+)'|`([^`$]+)`)/g;
1471
+
1472
+ const ENV_GET_ASSIGNMENT_PATTERN = /\b(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=[^;]*\.env\.get\(/;
1473
+
1474
+ // Const aliases of declared secret names (e.g. `const SERVICE_KEY_ENV =
1475
+ // "APIFUSE__PROVIDER__X__SERVICE_KEY"`) so aliased `.env.get(SERVICE_KEY_ENV)`
1476
+ // guards are detected, not just direct string literals.
1477
+ function buildDeclaredSecretAliasMap(
1478
+ providerRoot: string,
1479
+ declaredNames: ReadonlySet<string>,
1480
+ ): Map<string, string> {
1481
+ const aliases = new Map<string, string>();
1482
+ for (const filePath of listNonTestTypeScriptFiles(providerRoot)) {
1483
+ const content = readFileSync(filePath, "utf8");
1484
+ for (const match of content.matchAll(SECRET_ALIAS_CONST_PATTERN)) {
1485
+ const alias = match[1];
1486
+ const name = match[2] ?? match[3] ?? match[4];
1487
+ if (alias && name && declaredNames.has(name)) {
1488
+ aliases.set(alias, name);
1489
+ }
1490
+ }
1491
+ }
1492
+ return aliases;
1493
+ }
1494
+
1495
+ function hasLocalSecretPresenceGuard(
1496
+ line: string,
1497
+ remainingLines: readonly string[],
1498
+ declaredNames: ReadonlySet<string>,
1499
+ aliases: ReadonlyMap<string, string>,
1500
+ ): boolean {
1501
+ const match = ENV_GET_CALL_PATTERN.exec(line);
1502
+ if (!match) {
1503
+ return false;
1504
+ }
1505
+ const literal = match[1] ?? match[2] ?? match[3];
1506
+ const identifier = match[4];
1507
+ const readsDeclaredSecret =
1508
+ literal !== undefined
1509
+ ? declaredNames.has(literal)
1510
+ : identifier !== undefined && aliases.has(identifier);
1511
+ if (!readsDeclaredSecret) {
1512
+ return false;
1513
+ }
1514
+
1515
+ const window = [line, ...remainingLines.slice(0, SECRET_PRESENCE_GUARD_LOOKAHEAD_LINES)];
1516
+ if (!window.some((candidate) => /\bthrow\b/.test(candidate))) {
1517
+ return false;
1518
+ }
1519
+
1520
+ // Assigned read (`const key = ctx.env.get(...)`): only a falsy/undefined
1521
+ // check on THAT variable counts as a presence guard. Anchoring on the
1522
+ // assigned identifier avoids false positives from unrelated guards/throws
1523
+ // that merely sit near the env read (mirrors the aliased runtime-guard rule).
1524
+ const assigned = ENV_GET_ASSIGNMENT_PATTERN.exec(line)?.[1];
1525
+ if (assigned) {
1526
+ const escaped = assigned.replace(/\$/g, "\\$");
1527
+ const guardPattern = new RegExp(
1528
+ `(?:!\\s*${escaped}\\b|\\b${escaped}\\s*===?\\s*(?:undefined|null)\\b|\\b${escaped}\\s*==\\s*null\\b|\\b${escaped}(?:\\?\\.|\\.)length\\s*===?\\s*0\\b)`,
1529
+ );
1530
+ return window.some((candidate) => guardPattern.test(candidate));
1531
+ }
1532
+
1533
+ // Un-assigned read: only an inline presence check on the same line counts,
1534
+ // e.g. `if (!ctx.env.get(KEY)) throw ...`.
1535
+ return /(?:if\s*\(\s*!|===?\s*undefined\b|==\s*null\b)/.test(line);
1536
+ }
1537
+
1538
+ function scoreSdkOwnedSecretPresence(
1539
+ providerRoot: string,
1540
+ provider: ProviderDefinition,
1541
+ ): SubmitCheck {
1542
+ const passMessage = "Provider relies on SDK-owned secret presence validation.";
1543
+ // Only `required: true` declarations: those are exactly what the runtime
1544
+ // gate enforces. A presence guard over an optional secret is conditional
1545
+ // business logic the SDK will not replace, not double validation.
1546
+ const declaredNames: ReadonlySet<string> = new Set(
1547
+ (provider.secrets ?? [])
1548
+ .filter((secret) => secret.required === true)
1549
+ .map((secret) => secret.name),
1550
+ );
1551
+ if (declaredNames.size === 0) {
1552
+ return pass(SDK_OWNED_SECRET_PRESENCE_RULE_ID, SDK_NATIVE_CATEGORY, passMessage, 0);
1553
+ }
1554
+
1555
+ const aliases = buildDeclaredSecretAliasMap(providerRoot, declaredNames);
1556
+ const findings = findSourceFindings(providerRoot, (line, remainingLines) =>
1557
+ hasLocalSecretPresenceGuard(line, remainingLines, declaredNames, aliases),
1558
+ );
1559
+ if (findings.length === 0) {
1560
+ return pass(SDK_OWNED_SECRET_PRESENCE_RULE_ID, SDK_NATIVE_CATEGORY, passMessage, 0);
1561
+ }
1562
+
1563
+ const { violations, overridden } = partitionAllowOverrides(
1564
+ providerRoot,
1565
+ findings,
1566
+ SDK_OWNED_SECRET_PRESENCE_RULE_ID,
1567
+ );
1568
+ if (violations.length === 0) {
1569
+ return pass(
1570
+ SDK_OWNED_SECRET_PRESENCE_RULE_ID,
1571
+ SDK_NATIVE_CATEGORY,
1572
+ `${passMessage} ${overridden.length} acknowledged @apifuse-allow override(s).`,
1573
+ 0,
1574
+ formatSourceFindings(overridden),
1575
+ );
1576
+ }
1577
+
1578
+ return {
1579
+ id: SDK_OWNED_SECRET_PRESENCE_RULE_ID,
1580
+ category: SDK_NATIVE_CATEGORY,
1581
+ level: "warn",
1582
+ status: "warn",
1583
+ points: 0,
1584
+ maxPoints: 0,
1585
+ message:
1586
+ "Provider source re-validates declared env secret presence locally; the SDK owns this check.",
1587
+ remediation:
1588
+ "The provider SDK validates declared required secrets before handlers and auth flows run and returns the canonical MISSING_SECRET error (HTTP 400, category credential_unavailable). Declare the secret with required: true in defineProvider({ secrets: [...] }), delete the provider-local presence guard (requireServiceKey/requireApiKey style), and read the value directly with ctx.env.get(); the guard is dead weight and its divergent CONFIGURATION_ERROR-style shape is deprecated. Acknowledge intentional exceptions with `// @apifuse-allow sdk-owned-secret-presence: <reason>`.",
1589
+ evidence: formatSourceFindings(violations),
1590
+ };
1591
+ }
1592
+
1426
1593
  function findSourceLineMatches(
1427
1594
  providerRoot: string,
1428
1595
  pattern: RegExp | ((line: string) => boolean),
@@ -1522,6 +1689,10 @@ function isScannableProviderSourceFile(relativePath: string): boolean {
1522
1689
  );
1523
1690
  }
1524
1691
 
1692
+ // `.agents`/`.apifuse` stay IN scope on purpose: managed content there is
1693
+ // markdown/JSON (never scannable), while a planted `.ts`/`.sh` under those
1694
+ // directories must not become a scan-exempt hiding place for secrets, raw
1695
+ // fetch, or vendor imports.
1525
1696
  function shouldScanSourceDirectory(relativePath: string): boolean {
1526
1697
  return ![".git", "node_modules", "dist", "build", "coverage"].includes(relativePath);
1527
1698
  }
@@ -1544,6 +1715,27 @@ function formatSourceFindings(findings: readonly SourceFinding[]): string[] {
1544
1715
  return findings.map((finding) => `${finding.file}:${finding.line}`);
1545
1716
  }
1546
1717
 
1718
+ function scorePromptAssetFreshness(providerRoot: string): SubmitCheck {
1719
+ const verification = verifyPromptAssets(providerRoot);
1720
+ if (verification.ok) {
1721
+ return pass(
1722
+ "prompt-assets-fresh",
1723
+ "docs",
1724
+ "SDK-managed agent prompt assets match the installed SDK version.",
1725
+ 0,
1726
+ );
1727
+ }
1728
+
1729
+ return blocker(
1730
+ "prompt-assets-fresh",
1731
+ "docs",
1732
+ "SDK-managed agent prompt assets are missing, stale, or modified.",
1733
+ "Run `bun run sync-assets` (or `bunx apifuse sync-assets .`) to regenerate AGENTS.md, .agents/skills/**, the CLAUDE.md/.claude/.codex symlinks, and .apifuse/prompt-assets.json for the installed SDK version.",
1734
+ 0,
1735
+ formatPromptAssetIssues(verification),
1736
+ );
1737
+ }
1738
+
1547
1739
  function scoreRepositoryDx(providerRoot: string): SubmitCheck {
1548
1740
  const missing: string[] = [];
1549
1741
  if (!existsSync(resolve(providerRoot, ".gitignore"))) {
@@ -1979,10 +2171,7 @@ function scoreFixtureCoverage(provider: ProviderDefinition): SubmitCheck {
1979
2171
 
1980
2172
  const GENERATED_LOCAL_ONLY_SCAFFOLD_REASON = /generated local-only scaffold/i;
1981
2173
 
1982
- function scoreFixtureProvenance(
1983
- providerRoot: string,
1984
- provider: ProviderDefinition,
1985
- ): SubmitCheck {
2174
+ function scoreFixtureProvenance(providerRoot: string, provider: ProviderDefinition): SubmitCheck {
1986
2175
  const rawPath = resolve(providerRoot, "__fixtures__", "raw.json");
1987
2176
  let hasRecordedEvidence = false;
1988
2177
  if (existsSync(rawPath)) {
@@ -2028,7 +2217,7 @@ function scoreFixtureProvenance(
2028
2217
  );
2029
2218
  }
2030
2219
 
2031
- function hasNonEmptyRecordedFixture(value: unknown): boolean {
2220
+ export function hasNonEmptyRecordedFixture(value: unknown): boolean {
2032
2221
  return recordedFixtureStats(value, 0).hasNestedSubstance;
2033
2222
  }
2034
2223
 
@@ -2066,8 +2255,18 @@ function recordedFixtureStats(
2066
2255
  leafValues,
2067
2256
  };
2068
2257
  }
2069
- if (typeof value === "string" && value.length === 0) {
2070
- return { hasNestedSubstance: false, leafValues: 0 };
2258
+ if (typeof value === "string") {
2259
+ if (value.length === 0) {
2260
+ return { hasNestedSubstance: false, leafValues: 0 };
2261
+ }
2262
+ // A recorded operation value may be a raw XML or delimited-text success
2263
+ // payload. Treat a substantive one as nested evidence while still counting
2264
+ // the string as a leaf so existing JSON provenance heuristics are unchanged.
2265
+ return {
2266
+ hasNestedSubstance:
2267
+ hasSubstantiveXmlStructure(value) || hasSubstantiveDelimitedTextStructure(value),
2268
+ leafValues: 1,
2269
+ };
2071
2270
  }
2072
2271
  return { hasNestedSubstance: false, leafValues: 1 };
2073
2272
  }
@@ -2083,12 +2282,17 @@ function allOperationsAreGeneratedLocalScaffold(provider: ProviderDefinition): b
2083
2282
  }
2084
2283
 
2085
2284
  function scoreVendorKeyLeak(providerRoot: string): SubmitCheck {
2086
- return escapeHatchResult(providerRoot, "vendor-key-leak", findVendorKeyLeakFindings(providerRoot), {
2087
- blockerMessage: "Public schema keys leak raw vendor field names.",
2088
- remediation:
2089
- "Normalize public request/response fields to APIFuse-standard lowerCamelCase names (e.g. isOpen24h, latitude); keep raw vendor keys only in upstream-parsing schemas (const upstream... = z.object(...)). Add `// @apifuse-allow vendor-key-leak` only with a comment explaining why the vendor name is genuinely canonical.",
2090
- passMessage: "No vendor field-name leaks detected in public schemas.",
2091
- });
2285
+ return escapeHatchResult(
2286
+ providerRoot,
2287
+ "vendor-key-leak",
2288
+ findVendorKeyLeakFindings(providerRoot),
2289
+ {
2290
+ blockerMessage: "Public schema keys leak raw vendor field names.",
2291
+ remediation:
2292
+ "Normalize public request/response fields to APIFuse-standard lowerCamelCase names (e.g. isOpen24h, latitude); keep raw vendor keys only in upstream-parsing schemas (const upstream... = z.object(...)). Add `// @apifuse-allow vendor-key-leak` only with a comment explaining why the vendor name is genuinely canonical.",
2293
+ passMessage: "No vendor field-name leaks detected in public schemas.",
2294
+ },
2295
+ );
2092
2296
  }
2093
2297
 
2094
2298
  function scoreVendorTimestampLeak(providerRoot: string): SubmitCheck {
@@ -2191,26 +2395,57 @@ function vendorKeyFindingsForObject(
2191
2395
  const keys = collectTopLevelObjectKeys(source, zObject.objectStart, zObject.objectEnd);
2192
2396
  const digitFamilies = new Map<string, Set<string>>();
2193
2397
  for (const key of keys) {
2194
- const digitMatch = /^([a-z][a-zA-Z]*)(\d+)[a-z]*$/i.exec(key.name);
2195
- if (!digitMatch?.[1] || !digitMatch[2]) {
2398
+ const member = numberedFamilyMember(key.name);
2399
+ if (!member) {
2196
2400
  continue;
2197
2401
  }
2198
- const digits = digitFamilies.get(digitMatch[1]) ?? new Set<string>();
2199
- digits.add(digitMatch[2]);
2200
- digitFamilies.set(digitMatch[1], digits);
2402
+ const positions = digitFamilies.get(member.base) ?? new Set<string>();
2403
+ positions.add(member.position);
2404
+ digitFamilies.set(member.base, positions);
2201
2405
  }
2202
2406
 
2203
2407
  return keys
2204
2408
  .filter((key) => {
2205
- if (!/^[a-z][a-zA-Z0-9]*$/.test(key.name)) {
2409
+ if (!isAllowedPublicOutputKeyName(key.name)) {
2206
2410
  return true;
2207
2411
  }
2208
- const digitMatch = /^([a-z][a-zA-Z]*)(\d+)[a-z]*$/i.exec(key.name);
2209
- return digitMatch?.[1] !== undefined && (digitFamilies.get(digitMatch[1])?.size ?? 0) >= 3;
2412
+ const member = numberedFamilyMember(key.name);
2413
+ return member !== null && (digitFamilies.get(member.base)?.size ?? 0) >= 3;
2210
2414
  })
2211
2415
  .map((key) => ({ key: key.name, line: offsetToLine(source, key.offset) }));
2212
2416
  }
2213
2417
 
2418
+ // A numbered vendor family is a base name plus a numeric position and an
2419
+ // optional trailing letter suffix, in either compact/camel form (sensor1,
2420
+ // duty1s) or semantic snake_case form (sensor_1, duty_time_1s). Both styles
2421
+ // normalize to the same { base, position } so a family of >=3 distinct
2422
+ // positions is caught regardless of which naming style the vendor leaked
2423
+ // through. Returns null for names that carry no numeric position.
2424
+ function numberedFamilyMember(name: string): { base: string; position: string } | null {
2425
+ const camelMatch = /^([a-z][a-zA-Z]*)(\d+)[a-z]*$/i.exec(name);
2426
+ if (camelMatch?.[1] && camelMatch[2]) {
2427
+ return { base: camelMatch[1], position: camelMatch[2] };
2428
+ }
2429
+ const snakeMatch = /^([a-z][a-z0-9]*(?:_[a-z0-9]+)*?)_(\d+)[a-z]*$/.exec(name);
2430
+ if (snakeMatch?.[1] && snakeMatch[2]) {
2431
+ return { base: snakeMatch[1], position: snakeMatch[2] };
2432
+ }
2433
+ return null;
2434
+ }
2435
+
2436
+ // Public output keys may use APIFuse lowerCamelCase (isOpen24h, latitude) or
2437
+ // semantic snake_case (pharmacy_id, weekly_hours, total_count, scan_exhausted).
2438
+ // Both are normalized, human-authored names. Raw vendor keys leak through mixed
2439
+ // case or uppercase acronyms (MKioskTy) and match neither, so they stay flagged.
2440
+ // Numbered vendor families still pass this name gate in either style
2441
+ // (sensor1/2/3 or sensor_1/sensor_2/sensor_3), so they are caught separately by
2442
+ // the >=3-member numberedFamilyMember check in vendorKeyFindingsForObject.
2443
+ function isAllowedPublicOutputKeyName(name: string): boolean {
2444
+ const isLowerCamelCase = /^[a-z][a-zA-Z0-9]*$/.test(name);
2445
+ const isSemanticSnakeCase = /^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$/.test(name);
2446
+ return isLowerCamelCase || isSemanticSnakeCase;
2447
+ }
2448
+
2214
2449
  function collectTopLevelObjectKeys(
2215
2450
  source: string,
2216
2451
  objectStart: number,
@@ -2244,9 +2479,7 @@ function collectTopLevelObjectKeys(
2244
2479
  if (computedQuote === '"' || computedQuote === "'") {
2245
2480
  const literalEnd = findStringEnd(source, literalStart);
2246
2481
  const afterLiteral =
2247
- literalEnd === -1
2248
- ? -1
2249
- : skipWhitespaceAndComments(masked, literalEnd + 1, computedEnd);
2482
+ literalEnd === -1 ? -1 : skipWhitespaceAndComments(masked, literalEnd + 1, computedEnd);
2250
2483
  if (literalEnd !== -1 && afterLiteral === computedEnd) {
2251
2484
  key = source.slice(literalStart + 1, literalEnd);
2252
2485
  }
@@ -2364,8 +2597,13 @@ function findNamedConstValueRanges(source: string): NamedObjectRange[] {
2364
2597
  return ranges;
2365
2598
  }
2366
2599
 
2367
- function findConstValueRangeContaining(source: string, offset: number): NamedObjectRange | undefined {
2368
- return findNamedConstValueRanges(source).find((range) => offset >= range.start && offset <= range.end);
2600
+ function findConstValueRangeContaining(
2601
+ source: string,
2602
+ offset: number,
2603
+ ): NamedObjectRange | undefined {
2604
+ return findNamedConstValueRanges(source).find(
2605
+ (range) => offset >= range.start && offset <= range.end,
2606
+ );
2369
2607
  }
2370
2608
 
2371
2609
  function findStringLiteralsInRange(
@@ -3503,9 +3741,47 @@ function scoreProviderDocs(providerRoot: string): SubmitCheck[] {
3503
3741
  ];
3504
3742
  }
3505
3743
 
3744
+ // Splits secret findings into still-active findings and acknowledged
3745
+ // `// @apifuse-allow secret-scan` overrides, mirroring partitionAllowOverrides
3746
+ // (same pragma placement: the finding line or the line directly above it).
3747
+ // Every finding source carries a line number (entropy candidates and located
3748
+ // SECRET_PATTERNS matches); a finding that somehow lacks one stays active
3749
+ // defensively.
3750
+ function partitionSecretScanAllowOverrides(
3751
+ providerRoot: string,
3752
+ findings: readonly SecretFinding[],
3753
+ ): { active: SecretFinding[]; overridden: SecretFinding[] } {
3754
+ const fileLineCache = new Map<string, string[]>();
3755
+ const active: SecretFinding[] = [];
3756
+ const overridden: SecretFinding[] = [];
3757
+
3758
+ for (const finding of findings) {
3759
+ if (finding.line === undefined) {
3760
+ active.push(finding);
3761
+ continue;
3762
+ }
3763
+ const absolute = resolve(providerRoot, finding.file);
3764
+ let lines = fileLineCache.get(absolute);
3765
+ if (lines === undefined) {
3766
+ lines = existsSync(absolute) ? readFileSync(absolute, "utf8").split(/\r?\n/) : [];
3767
+ fileLineCache.set(absolute, lines);
3768
+ }
3769
+ if (hasAllowOverride(lines, finding.line, "secret-scan")) {
3770
+ overridden.push(finding);
3771
+ } else {
3772
+ active.push(finding);
3773
+ }
3774
+ }
3775
+
3776
+ return { active, overridden };
3777
+ }
3778
+
3506
3779
  function scoreSecrets(providerRoot: string, provider?: ProviderDefinition): SubmitCheck {
3507
- const findings = findSecretFindings(providerRoot, provider?.id);
3508
- const blockerFindings = findings.filter((finding) => finding.level !== "warn");
3780
+ const { active, overridden } = partitionSecretScanAllowOverrides(
3781
+ providerRoot,
3782
+ findSecretFindings(providerRoot, provider?.id),
3783
+ );
3784
+ const blockerFindings = active.filter((finding) => finding.level !== "warn");
3509
3785
  if (blockerFindings.length > 0) {
3510
3786
  return {
3511
3787
  id: "secret-scan",
@@ -3525,7 +3801,15 @@ function scoreSecrets(providerRoot: string, provider?: ProviderDefinition): Subm
3525
3801
  ),
3526
3802
  };
3527
3803
  }
3528
- if (findings.length > 0) {
3804
+ if (active.length > 0 || overridden.length > 0) {
3805
+ const messageBase =
3806
+ active.length > 0
3807
+ ? "High-entropy source strings were found without secret-like identifier context; they may be false positives."
3808
+ : "Potential credential-like strings were found in shareable files.";
3809
+ const message =
3810
+ overridden.length > 0
3811
+ ? `${messageBase} ${overridden.length} acknowledged @apifuse-allow override(s).`
3812
+ : messageBase;
3529
3813
  return {
3530
3814
  id: "secret-scan",
3531
3815
  category: "security",
@@ -3533,11 +3817,10 @@ function scoreSecrets(providerRoot: string, provider?: ProviderDefinition): Subm
3533
3817
  status: "warn",
3534
3818
  points: 8,
3535
3819
  maxPoints: CATEGORY_MAX_POINTS.security,
3536
- message:
3537
- "High-entropy source strings were found without secret-like identifier context; they may be false positives.",
3820
+ message,
3538
3821
  remediation:
3539
- 'Review the listed strings. If any are credentials, move them to env vars read via `ctx.env.get("APIFUSE__PROVIDER__<ID>__<NAME>")` and rotate the leaked credential; otherwise keep generated blobs in fixtures/tests or document why they are public.',
3540
- evidence: findings.map(
3822
+ 'Review the listed strings. If any are credentials, move them to env vars read via `ctx.env.get("APIFUSE__PROVIDER__<ID>__<NAME>")` and rotate the leaked credential; otherwise keep generated blobs in fixtures/tests or document why they are public with `// @apifuse-allow secret-scan: <reason>`.',
3823
+ evidence: [...active, ...overridden].map(
3541
3824
  (finding) =>
3542
3825
  finding.evidence ??
3543
3826
  `${finding.file}${finding.line ? `:${finding.line}` : ""}: ${finding.label}`,
@@ -3568,8 +3851,19 @@ function findSecretFindings(providerRoot: string, providerId = "<ID>"): SecretFi
3568
3851
  if (!existsSync(filePath)) continue;
3569
3852
  const content = readFileSync(filePath, "utf8");
3570
3853
  for (const [label, pattern] of SECRET_PATTERNS) {
3571
- if (pattern.test(content)) {
3572
- findings.push({ label, file: relativePath });
3854
+ // Locate every match to its line so pattern findings carry the line
3855
+ // information hasAllowOverride needs: `// @apifuse-allow secret-scan`
3856
+ // must behave uniformly across entropy findings and pattern findings.
3857
+ const globalPattern = new RegExp(
3858
+ pattern.source,
3859
+ pattern.flags.includes("g") ? pattern.flags : `${pattern.flags}g`,
3860
+ );
3861
+ const seenLines = new Set<number>();
3862
+ for (const match of content.matchAll(globalPattern)) {
3863
+ const line = offsetToLine(content, match.index);
3864
+ if (seenLines.has(line)) continue;
3865
+ seenLines.add(line);
3866
+ findings.push({ label, file: relativePath, line });
3573
3867
  }
3574
3868
  }
3575
3869
  }
@@ -3627,7 +3921,7 @@ export function extractStringLiteralCandidates(line: string): string[] {
3627
3921
  continue;
3628
3922
  }
3629
3923
  if (char === quote) {
3630
- if (cursor - contentStart >= 20) {
3924
+ if (cursor - contentStart >= ENTROPY_CANDIDATE_MIN_LENGTH) {
3631
3925
  candidates.push(line.slice(contentStart, cursor));
3632
3926
  }
3633
3927
  index = cursor;
@@ -3652,6 +3946,25 @@ function classifyEntropyCandidate(input: {
3652
3946
  if (!charset) return undefined;
3653
3947
  const entropy = shannonEntropy(value);
3654
3948
  const secretishContext = SECRETISH_IDENTIFIER_PATTERN.test(input.line);
3949
+ // Word-like SCREAMING_SNAKE values (e.g. error-code constants such as
3950
+ // "AUTH_PASSWORD_LOGIN_CAPTCHA_REQUIRED") may contain secret-ish words
3951
+ // (AUTH/PASSWORD/...) in their own text and would otherwise be permanently
3952
+ // blocker-flagged. They are never skipped — entropy classification always
3953
+ // runs — but when the secret-ish context comes solely from identifier-
3954
+ // constant-shaped literal text (the line with those literals stripped
3955
+ // carries no secret-ish identifier), the finding is capped at a
3956
+ // non-blocking warning instead of a blocker. Stripping constant-shaped
3957
+ // siblings — not just the candidate — matters for lines holding several
3958
+ // constants (e.g. an ERROR_CODES array), while quoted property keys and
3959
+ // header names ("Authorization", "apiKey") stay visible as genuine
3960
+ // external context. Assignments to `apiKey`/`token`/`secret`-style names
3961
+ // still escalate to blockers via the identifier side, and
3962
+ // `// @apifuse-allow secret-scan` remains the reviewed way to silence the
3963
+ // warning.
3964
+ const selfContextOnlyConstant =
3965
+ secretishContext &&
3966
+ isScreamingSnakeConstantValue(value) &&
3967
+ !SECRETISH_IDENTIFIER_PATTERN.test(stripIdentifierConstantLiterals(input.line, value));
3655
3968
  const threshold = charset === "hex" ? 3.0 : secretishContext ? 4.0 : 4.5;
3656
3969
  if (entropy < threshold) return undefined;
3657
3970
 
@@ -3662,16 +3975,163 @@ function classifyEntropyCandidate(input: {
3662
3975
  charset === "hex"
3663
3976
  ? `high-entropy hex string (${entropy.toFixed(2)} bits/char)`
3664
3977
  : `high-entropy base64-like string (${entropy.toFixed(2)} bits/char)`;
3978
+ const contextNote = selfContextOnlyConstant
3979
+ ? "; identifier-like constant (downgraded to warning)"
3980
+ : secretishContext
3981
+ ? ""
3982
+ : "; may be a false positive";
3665
3983
  return {
3666
3984
  label,
3667
3985
  file: input.file,
3668
3986
  line: input.lineNumber,
3669
- level: secretishContext ? "blocker" : "warn",
3987
+ level: secretishContext && !selfContextOnlyConstant ? "blocker" : "warn",
3670
3988
  remediation: `Move ${location} to an env var read via \`ctx.env.get("${envName}")\` and rotate the leaked credential.`,
3671
- evidence: `${location}: ${label}; preview ${preview}${secretishContext ? "" : "; may be a false positive"}`,
3989
+ evidence: `${location}: ${label}; preview ${preview}${contextNote}`,
3672
3990
  };
3673
3991
  }
3674
3992
 
3993
+ // Word-like SCREAMING_SNAKE identifier shape: at least two underscore-
3994
+ // separated segments, each essentially pure alphabetic — letters optionally
3995
+ // followed by a SHORT digit suffix (at most 2, e.g. version markers like
3996
+ // "V2") — and at most 15% digits across the whole value. Dictionary-style
3997
+ // constants like "AUTH_PASSWORD_LOGIN_CAPTCHA_REQUIRED" or
3998
+ // "PROVIDER_CONTRACT_V2_REQUIRED" match; digit-heavy segmented material
3999
+ // (e.g. license/credential shapes like "ABCD1234_EFGH5678_IJKL9012"),
4000
+ // uppercase blobs ("XK9J_Q2ZP_M7VN"), and underscore-free hex-like values
4001
+ // ("A1B2C3D4...") do not. This shape gate never skips entropy classification;
4002
+ // it only decides whether a finding whose secret-ish context comes solely
4003
+ // from the literal's own text is downgraded from blocker to warning, so it
4004
+ // deliberately stays strict: values that merely contain a secret-ish word but
4005
+ // are not word-like constants keep full blocker severity.
4006
+ function isScreamingSnakeConstantValue(value: string): boolean {
4007
+ if (!/^[A-Z][A-Z0-9_]*$/.test(value) || !value.includes("_")) return false;
4008
+ const segments = value.split("_");
4009
+ if (segments.length < 2) return false;
4010
+ if (!segments.every((segment) => /^[A-Z]+[0-9]{0,2}$/.test(segment))) return false;
4011
+ const digitCount = value.match(/[0-9]/g)?.length ?? 0;
4012
+ return digitCount / value.length <= 0.15;
4013
+ }
4014
+
4015
+ type LineStringLiteral = {
4016
+ // Index of the opening quote.
4017
+ start: number;
4018
+ // Index just past the closing quote (line end when unterminated).
4019
+ end: number;
4020
+ content: string;
4021
+ closed: boolean;
4022
+ role: "key" | "value";
4023
+ // Nearest unclosed bracket enclosing the literal's start, if any.
4024
+ container?: { bracket: "[" | "(" | "{"; index: number };
4025
+ };
4026
+
4027
+ // Stable identity for the container a literal sits in ("top" when the
4028
+ // literal is not inside any bracket on the line).
4029
+ function literalContainerKey(literal: LineStringLiteral): string {
4030
+ return literal.container
4031
+ ? `${literal.container.bracket}${literal.container.index}`
4032
+ : "top";
4033
+ }
4034
+
4035
+ // Single-pass line tokenizer: extracts every string literal with its span and
4036
+ // classifies its syntactic role once. A literal is a KEY when it is preceded
4037
+ // (ignoring whitespace) by "{", ",", "(", or the line start AND followed
4038
+ // (ignoring whitespace) by ":" — i.e. it names the value next to it. Every
4039
+ // other literal is a VALUE: ternary arms (preceded by "?" or ":"), array
4040
+ // elements, call arguments, and assignment right-hand sides, even when a
4041
+ // ternary's ":" happens to follow them. Uses the same quote/escape walking as
4042
+ // extractStringLiteralCandidates.
4043
+ function tokenizeLineStringLiterals(line: string): LineStringLiteral[] {
4044
+ const literals: LineStringLiteral[] = [];
4045
+ const bracketStack: Array<{ bracket: "[" | "(" | "{"; index: number }> = [];
4046
+ let index = 0;
4047
+ while (index < line.length) {
4048
+ const char = line[index];
4049
+ if (char !== '"' && char !== "'" && char !== "`") {
4050
+ if (char === "[" || char === "(" || char === "{") {
4051
+ bracketStack.push({ bracket: char, index });
4052
+ } else if (char === "]" || char === ")" || char === "}") {
4053
+ bracketStack.pop();
4054
+ }
4055
+ index += 1;
4056
+ continue;
4057
+ }
4058
+ const quote = char;
4059
+ const start = index;
4060
+ const contentStart = index + 1;
4061
+ let cursor = contentStart;
4062
+ let closed = false;
4063
+ while (cursor < line.length) {
4064
+ const inner = line[cursor];
4065
+ if (inner === "\\") {
4066
+ cursor += 2;
4067
+ continue;
4068
+ }
4069
+ if (inner === quote) {
4070
+ closed = true;
4071
+ break;
4072
+ }
4073
+ cursor += 1;
4074
+ }
4075
+ const contentEnd = Math.min(cursor, line.length);
4076
+ const end = closed ? cursor + 1 : line.length;
4077
+ const before = line.slice(0, start).trimEnd();
4078
+ const keyPreceded = before === "" || /[{,(]$/.test(before);
4079
+ const keyFollowed = closed && /^\s*:/.test(line.slice(end));
4080
+ literals.push({
4081
+ start,
4082
+ end,
4083
+ content: line.slice(contentStart, contentEnd),
4084
+ closed,
4085
+ role: keyPreceded && keyFollowed ? "key" : "value",
4086
+ container: bracketStack[bracketStack.length - 1],
4087
+ });
4088
+ index = end;
4089
+ }
4090
+ return literals;
4091
+ }
4092
+
4093
+ // Builds the context text used to decide whether a candidate's secret-ish
4094
+ // context is genuine. The candidate's own literal is ALWAYS stripped
4095
+ // (self-context rule). SIBLING identifier-constant-shaped candidate literals
4096
+ // (VALUE role, SCREAMING_SNAKE shape, >= ENTROPY_CANDIDATE_MIN_LENGTH) are
4097
+ // stripped only when they share the candidate's non-call container — the same
4098
+ // `[...]` array, the same `{...}` object value list, or the bracket-free top
4099
+ // level (ternary arms) — so a value list of error codes cannot poison its own
4100
+ // members' context. Call-argument siblings (inside `(...)`) always keep their
4101
+ // context: in `headers.set("X_LONG_AUTH_TOKEN_NAME", "QWERTY_...")` the first
4102
+ // argument genuinely describes the second, so stripping it would erase real
4103
+ // auth/token context. KEY-role literals are never stripped.
4104
+ function stripIdentifierConstantLiterals(line: string, candidate: string): string {
4105
+ const literals = tokenizeLineStringLiterals(line);
4106
+ const candidateContainers = new Set<string>();
4107
+ for (const literal of literals) {
4108
+ if (literal.content === candidate) {
4109
+ candidateContainers.add(literalContainerKey(literal));
4110
+ }
4111
+ }
4112
+ let result = "";
4113
+ let previousEnd = 0;
4114
+ for (const literal of literals) {
4115
+ result += line.slice(previousEnd, literal.start);
4116
+ const isSelf = literal.content === candidate;
4117
+ const isSameNonCallContainerSibling =
4118
+ literal.role === "value" &&
4119
+ literal.content.length >= ENTROPY_CANDIDATE_MIN_LENGTH &&
4120
+ isScreamingSnakeConstantValue(literal.content) &&
4121
+ literal.container?.bracket !== "(" &&
4122
+ candidateContainers.has(literalContainerKey(literal));
4123
+ if (isSelf || isSameNonCallContainerSibling) {
4124
+ const quote = line[literal.start] ?? "";
4125
+ result += quote + (literal.closed ? quote : "");
4126
+ } else {
4127
+ result += line.slice(literal.start, literal.end);
4128
+ }
4129
+ previousEnd = literal.end;
4130
+ }
4131
+ result += line.slice(previousEnd);
4132
+ return result;
4133
+ }
4134
+
3675
4135
  function shouldConsiderEntropyValue(value: string): boolean {
3676
4136
  const lower = value.toLowerCase();
3677
4137
  if (/^(?:dev-only|local|example|sample|your-|replace|<)/i.test(value)) {
@@ -3687,7 +4147,7 @@ function shouldConsiderEntropyValue(value: string): boolean {
3687
4147
  if (lower.includes("/") && /\.[a-z0-9]{1,8}(?:$|[/?#])/i.test(value)) {
3688
4148
  return false;
3689
4149
  }
3690
- return value.length >= 20;
4150
+ return value.length >= ENTROPY_CANDIDATE_MIN_LENGTH;
3691
4151
  }
3692
4152
 
3693
4153
  function classifyEntropyCharset(value: string): "base64" | "hex" | undefined {
@@ -3721,6 +4181,11 @@ function guessSecretName(line: string): string {
3721
4181
 
3722
4182
  const SECRETISH_IDENTIFIER_PATTERN = /key|token|secret|password|credential|auth/i;
3723
4183
 
4184
+ // Minimum length for a string literal to be considered an entropy candidate.
4185
+ // Shared by candidate extraction, entropy screening, and the context strip so
4186
+ // the three stay coherent.
4187
+ const ENTROPY_CANDIDATE_MIN_LENGTH = 20;
4188
+
3724
4189
  const SECRET_PATTERNS: Array<[string, RegExp]> = [
3725
4190
  ["JWT-like token", /eyJ[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{10,}/],
3726
4191
  ["GitHub token", /gh[pousr]_[A-Za-z0-9_]{30,}/],