@apifuse/provider-sdk 2.1.0-beta.0 → 2.1.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 (213) hide show
  1. package/AUTHORING.md +218 -21
  2. package/CHANGELOG.md +54 -0
  3. package/README.md +147 -10
  4. package/SUBMISSION.md +87 -0
  5. package/bin/apifuse-check.ts +86 -4
  6. package/bin/apifuse-dev.ts +87 -13
  7. package/bin/apifuse-pack-check.ts +120 -0
  8. package/bin/apifuse-pack-smoke.ts +423 -0
  9. package/bin/apifuse-perf.ts +142 -49
  10. package/bin/apifuse-record.ts +182 -104
  11. package/bin/apifuse-submit-check.ts +2538 -0
  12. package/bin/apifuse.ts +1 -1
  13. package/dist/ceremonies/index.d.ts +41 -0
  14. package/dist/ceremonies/index.js +490 -0
  15. package/dist/choice-token.d.ts +24 -0
  16. package/dist/choice-token.js +74 -0
  17. package/dist/cli/commands.d.ts +10 -0
  18. package/dist/cli/commands.js +80 -0
  19. package/dist/cli/create.d.ts +47 -0
  20. package/dist/cli/create.js +762 -0
  21. package/dist/cli/templates/provider/.dockerignore.tpl +22 -0
  22. package/dist/cli/templates/provider/.gitignore.tpl +22 -0
  23. package/dist/cli/templates/provider/Dockerfile.tpl +7 -0
  24. package/dist/cli/templates/provider/README.md.tpl +160 -0
  25. package/dist/cli/templates/provider/dev.ts.tpl +5 -0
  26. package/dist/cli/templates/provider/domain/README.md.tpl +3 -0
  27. package/dist/cli/templates/provider/index.test.ts.tpl +13 -0
  28. package/dist/cli/templates/provider/index.ts.tpl +15 -0
  29. package/dist/cli/templates/provider/mappers/README.md.tpl +3 -0
  30. package/dist/cli/templates/provider/meta.ts.tpl +7 -0
  31. package/dist/cli/templates/provider/operations/index.ts.tpl +5 -0
  32. package/dist/cli/templates/provider/operations/ping.ts.tpl +24 -0
  33. package/dist/cli/templates/provider/schemas/ping.ts.tpl +24 -0
  34. package/dist/cli/templates/provider/start.ts.tpl +5 -0
  35. package/dist/cli/templates/provider/upstream/README.md.tpl +3 -0
  36. package/dist/config/loader.d.ts +107 -0
  37. package/dist/config/loader.js +935 -0
  38. package/dist/contract-json.d.ts +9 -0
  39. package/dist/contract-json.js +51 -0
  40. package/dist/contract-serialization.d.ts +4 -0
  41. package/dist/contract-serialization.js +78 -0
  42. package/dist/contract-types.d.ts +49 -0
  43. package/dist/contract-types.js +1 -0
  44. package/dist/contract.d.ts +6 -0
  45. package/dist/contract.js +155 -0
  46. package/dist/define.d.ts +97 -0
  47. package/dist/define.js +1320 -0
  48. package/dist/dev.d.ts +9 -0
  49. package/dist/dev.js +15 -0
  50. package/dist/errors.d.ts +59 -0
  51. package/dist/errors.js +97 -0
  52. package/dist/i18n/catalog.d.ts +29 -0
  53. package/dist/i18n/catalog.js +159 -0
  54. package/dist/i18n/index.d.ts +2 -0
  55. package/dist/i18n/index.js +2 -0
  56. package/dist/i18n/keys.d.ts +10 -0
  57. package/dist/i18n/keys.js +34 -0
  58. package/dist/index.d.ts +41 -0
  59. package/dist/index.js +37 -0
  60. package/dist/lint.d.ts +73 -0
  61. package/dist/lint.js +702 -0
  62. package/dist/observability.d.ts +5 -0
  63. package/dist/observability.js +39 -0
  64. package/dist/provider.d.ts +9 -0
  65. package/dist/provider.js +8 -0
  66. package/dist/public-schema-field-lint.d.ts +2 -0
  67. package/dist/public-schema-field-lint.js +158 -0
  68. package/dist/recipes/gov-api.d.ts +19 -0
  69. package/dist/recipes/gov-api.js +72 -0
  70. package/dist/recipes/rest-api.d.ts +21 -0
  71. package/dist/recipes/rest-api.js +115 -0
  72. package/dist/runtime/auth-flow.d.ts +14 -0
  73. package/dist/runtime/auth-flow.js +44 -0
  74. package/dist/runtime/browser.d.ts +25 -0
  75. package/dist/runtime/browser.js +1034 -0
  76. package/dist/runtime/cache.d.ts +10 -0
  77. package/dist/runtime/cache.js +372 -0
  78. package/dist/runtime/choice.d.ts +15 -0
  79. package/dist/runtime/choice.js +435 -0
  80. package/dist/runtime/credential.d.ts +8 -0
  81. package/dist/runtime/credential.js +61 -0
  82. package/dist/runtime/env.d.ts +2 -0
  83. package/dist/runtime/env.js +10 -0
  84. package/dist/runtime/executor.d.ts +16 -0
  85. package/dist/runtime/executor.js +51 -0
  86. package/dist/runtime/http.d.ts +8 -0
  87. package/dist/runtime/http.js +706 -0
  88. package/dist/runtime/insights.d.ts +9 -0
  89. package/dist/runtime/insights.js +324 -0
  90. package/dist/runtime/instrumentation.d.ts +8 -0
  91. package/dist/runtime/instrumentation.js +269 -0
  92. package/dist/runtime/key-derivation.d.ts +24 -0
  93. package/dist/runtime/key-derivation.js +73 -0
  94. package/dist/runtime/keyring.d.ts +25 -0
  95. package/dist/runtime/keyring.js +93 -0
  96. package/dist/runtime/namespace.d.ts +9 -0
  97. package/dist/runtime/namespace.js +19 -0
  98. package/dist/runtime/otlp.d.ts +39 -0
  99. package/dist/runtime/otlp.js +103 -0
  100. package/dist/runtime/perf.d.ts +12 -0
  101. package/dist/runtime/perf.js +52 -0
  102. package/dist/runtime/prevalidate.d.ts +12 -0
  103. package/dist/runtime/prevalidate.js +173 -0
  104. package/dist/runtime/provider.d.ts +2 -0
  105. package/dist/runtime/provider.js +11 -0
  106. package/dist/runtime/proxy-errors.d.ts +21 -0
  107. package/dist/runtime/proxy-errors.js +83 -0
  108. package/dist/runtime/proxy-telemetry.d.ts +8 -0
  109. package/dist/runtime/proxy-telemetry.js +174 -0
  110. package/dist/runtime/redis.d.ts +17 -0
  111. package/dist/runtime/redis.js +82 -0
  112. package/dist/runtime/request-options.d.ts +3 -0
  113. package/dist/runtime/request-options.js +42 -0
  114. package/dist/runtime/state.d.ts +17 -0
  115. package/dist/runtime/state.js +344 -0
  116. package/dist/runtime/stealth.d.ts +18 -0
  117. package/dist/runtime/stealth.js +834 -0
  118. package/dist/runtime/stt.d.ts +22 -0
  119. package/dist/runtime/stt.js +480 -0
  120. package/dist/runtime/trace.d.ts +26 -0
  121. package/dist/runtime/trace.js +142 -0
  122. package/dist/runtime/waterfall.d.ts +12 -0
  123. package/dist/runtime/waterfall.js +147 -0
  124. package/dist/schema.d.ts +74 -0
  125. package/dist/schema.js +243 -0
  126. package/dist/serve.d.ts +1 -0
  127. package/dist/serve.js +1 -0
  128. package/dist/server/index.d.ts +3 -0
  129. package/dist/server/index.js +2 -0
  130. package/dist/server/serve.d.ts +64 -0
  131. package/dist/server/serve.js +1110 -0
  132. package/dist/server/types.d.ts +136 -0
  133. package/dist/server/types.js +86 -0
  134. package/dist/stealth/profiles.d.ts +4 -0
  135. package/dist/stealth/profiles.js +259 -0
  136. package/dist/stream.d.ts +44 -0
  137. package/dist/stream.js +151 -0
  138. package/dist/testing/helpers.d.ts +23 -0
  139. package/dist/testing/helpers.js +95 -0
  140. package/dist/testing/index.d.ts +2 -0
  141. package/dist/testing/index.js +2 -0
  142. package/dist/testing/run.d.ts +34 -0
  143. package/dist/testing/run.js +303 -0
  144. package/dist/types.d.ts +1326 -0
  145. package/dist/types.js +61 -0
  146. package/dist/utils/date.d.ts +6 -0
  147. package/dist/utils/date.js +101 -0
  148. package/dist/utils/parse.d.ts +16 -0
  149. package/dist/utils/parse.js +51 -0
  150. package/dist/utils/text.d.ts +4 -0
  151. package/dist/utils/text.js +14 -0
  152. package/dist/utils/transform.d.ts +8 -0
  153. package/dist/utils/transform.js +48 -0
  154. package/package.json +57 -29
  155. package/src/ceremonies/index.ts +30 -3
  156. package/src/choice-token.ts +165 -0
  157. package/src/cli/commands.ts +34 -11
  158. package/src/cli/create.ts +214 -52
  159. package/src/cli/templates/provider/.dockerignore.tpl +22 -0
  160. package/src/cli/templates/provider/.gitignore.tpl +22 -0
  161. package/src/cli/templates/provider/README.md.tpl +134 -2
  162. package/src/cli/templates/provider/dev.ts.tpl +1 -1
  163. package/src/cli/templates/provider/domain/README.md.tpl +3 -0
  164. package/src/cli/templates/provider/index.ts.tpl +5 -44
  165. package/src/cli/templates/provider/mappers/README.md.tpl +3 -0
  166. package/src/cli/templates/provider/meta.ts.tpl +7 -0
  167. package/src/cli/templates/provider/operations/index.ts.tpl +5 -0
  168. package/src/cli/templates/provider/operations/ping.ts.tpl +24 -0
  169. package/src/cli/templates/provider/schemas/ping.ts.tpl +24 -0
  170. package/src/cli/templates/provider/start.ts.tpl +1 -1
  171. package/src/cli/templates/provider/upstream/README.md.tpl +3 -0
  172. package/src/config/loader.ts +1282 -7
  173. package/src/contract-json.ts +75 -0
  174. package/src/contract-serialization.ts +89 -0
  175. package/src/contract-types.ts +52 -0
  176. package/src/contract.ts +215 -0
  177. package/src/define.ts +1726 -48
  178. package/src/errors.ts +27 -0
  179. package/src/i18n/catalog.ts +277 -0
  180. package/src/i18n/index.ts +2 -0
  181. package/src/i18n/keys.ts +64 -0
  182. package/src/index.ts +174 -15
  183. package/src/lint.ts +547 -73
  184. package/src/observability.ts +41 -0
  185. package/src/provider.ts +104 -5
  186. package/src/public-schema-field-lint.ts +237 -0
  187. package/src/runtime/auth-flow.ts +7 -0
  188. package/src/runtime/browser.ts +762 -51
  189. package/src/runtime/cache.ts +528 -0
  190. package/src/runtime/choice.ts +760 -0
  191. package/src/runtime/executor.ts +32 -3
  192. package/src/runtime/http.ts +945 -185
  193. package/src/runtime/insights.ts +11 -11
  194. package/src/runtime/instrumentation.ts +12 -4
  195. package/src/runtime/key-derivation.ts +1 -1
  196. package/src/runtime/keyring.ts +4 -3
  197. package/src/runtime/proxy-errors.ts +132 -0
  198. package/src/runtime/proxy-telemetry.ts +253 -0
  199. package/src/runtime/redis.ts +116 -0
  200. package/src/runtime/request-options.ts +66 -0
  201. package/src/runtime/state.ts +563 -0
  202. package/src/runtime/stealth.ts +1159 -0
  203. package/src/runtime/stt.ts +629 -0
  204. package/src/runtime/trace.ts +1 -1
  205. package/src/schema.ts +363 -1
  206. package/src/server/serve.ts +1172 -76
  207. package/src/server/types.ts +37 -0
  208. package/src/stream.ts +210 -0
  209. package/src/testing/run.ts +31 -5
  210. package/src/types.ts +1118 -44
  211. package/src/composite.ts +0 -43
  212. package/src/runtime/tls.ts +0 -425
  213. package/src/types/playwright-stealth.d.ts +0 -9
@@ -0,0 +1,2538 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { existsSync, readdirSync, readFileSync } from "node:fs";
4
+ import { writeFile } from "node:fs/promises";
5
+ import { basename, dirname, join, relative, resolve } from "node:path";
6
+ import { pathToFileURL } from "node:url";
7
+
8
+ import { z } from "zod";
9
+
10
+ import packageJson from "../package.json";
11
+ import type { ProviderDefinition } from "../src";
12
+ import {
13
+ loadProviderLocaleCatalogs,
14
+ type ProviderLocale,
15
+ validateProviderLocaleCatalogs,
16
+ } from "../src/i18n";
17
+ import { APIFUSE_DESCRIPTION_KEY_META_KEY } from "../src/schema";
18
+ import { type CheckResult, runChecks } from "./apifuse-check";
19
+
20
+ const TIERS = ["bronze", "silver", "gold", "diamond"] as const;
21
+ const TIER_VALUES: ReadonlySet<string> = new Set(TIERS);
22
+ type BountyTier = (typeof TIERS)[number];
23
+
24
+ type CheckLevel = "blocker" | "warn" | "info";
25
+ type CheckStatus = "pass" | "fail" | "warn" | "not_applicable";
26
+ type Verdict = "ready" | "reviewable_with_warnings" | "blocked";
27
+
28
+ export type SubmitCheck = {
29
+ id: string;
30
+ category: string;
31
+ level: CheckLevel;
32
+ status: CheckStatus;
33
+ points: number;
34
+ maxPoints: number;
35
+ message: string;
36
+ remediation?: string;
37
+ evidence?: string[];
38
+ };
39
+
40
+ export type SubmitCheckReport = {
41
+ schemaVersion: 1;
42
+ generatedAt: string;
43
+ provider: {
44
+ id: string;
45
+ version: string;
46
+ runtime: string;
47
+ authMode: string;
48
+ sdkVersion: string;
49
+ tier?: BountyTier;
50
+ };
51
+ score: {
52
+ total: number;
53
+ max: 100;
54
+ verdict: Verdict;
55
+ };
56
+ summary: {
57
+ blockers: number;
58
+ warnings: number;
59
+ passed: number;
60
+ };
61
+ checks: SubmitCheck[];
62
+ };
63
+
64
+ export function isAutoPromotionEligible(report: SubmitCheckReport): boolean {
65
+ return report.score.total >= 95 && report.summary.blockers === 0;
66
+ }
67
+
68
+ type CliArgs = {
69
+ isJson: boolean;
70
+ markdownPath?: string;
71
+ providerPath?: string;
72
+ smokeNote?: string;
73
+ tier?: BountyTier;
74
+ };
75
+
76
+ type SecretFinding = {
77
+ label: string;
78
+ file: string;
79
+ };
80
+
81
+ type SourceFinding = {
82
+ file: string;
83
+ line: number;
84
+ };
85
+
86
+ const SDK_NATIVE_CATEGORY = "sdk-native";
87
+ const VENDOR_SHIM_PROVIDER_ID_PREFIX = "apifuse-provider-";
88
+ const MAX_SOURCE_FINDING_EVIDENCE = 5;
89
+
90
+ const CATEGORY_MAX_POINTS = {
91
+ definition: 15,
92
+ operations: 15,
93
+ fixtures: 15,
94
+ health: 15,
95
+ smoke: 10,
96
+ auth: 10,
97
+ security: 10,
98
+ docs: 10,
99
+ } as const;
100
+
101
+ const REQUIRED_PUBLIC_PROVIDER_LOCALES = [
102
+ "en",
103
+ "ko",
104
+ ] as const satisfies readonly ProviderLocale[];
105
+
106
+ const HELP_TEXT = `Usage: apifuse submit-check [path] [--tier bronze|silver|gold|diamond] [--json] [--markdown <path>] [--smoke-note <text>]
107
+ Alias: apifuse bounty-check [path]
108
+ Default: apifuse submit-check .`;
109
+
110
+ export async function main() {
111
+ try {
112
+ const args = parseArgs(normalizeArgs(process.argv.slice(2)));
113
+
114
+ if (args.isJson && process.argv.includes("--help")) {
115
+ console.log(JSON.stringify({ help: HELP_TEXT }));
116
+ return;
117
+ }
118
+
119
+ const providerRoot = resolveProviderRoot(args.providerPath ?? ".");
120
+ const report = await buildSubmitCheckReport(providerRoot, args);
121
+
122
+ if (args.markdownPath) {
123
+ await writeFile(
124
+ resolve(process.cwd(), args.markdownPath),
125
+ renderMarkdown(report),
126
+ );
127
+ }
128
+
129
+ if (args.isJson) {
130
+ console.log(JSON.stringify(report, null, 2));
131
+ } else {
132
+ console.log(renderText(report));
133
+ if (args.markdownPath) {
134
+ console.log(`\nMarkdown report: ${args.markdownPath}`);
135
+ }
136
+ }
137
+
138
+ if (report.score.verdict === "blocked") {
139
+ process.exit(1);
140
+ }
141
+ } catch (error) {
142
+ console.error(error instanceof Error ? error.message : String(error));
143
+ process.exit(1);
144
+ }
145
+ }
146
+
147
+ function normalizeArgs(argv: string[]): string[] {
148
+ const [command, ...rest] = argv;
149
+ return command === "submit-check" || command === "bounty-check" ? rest : argv;
150
+ }
151
+
152
+ function parseArgs(argv: string[]): CliArgs {
153
+ const args: CliArgs = { isJson: false };
154
+
155
+ for (let index = 0; index < argv.length; index += 1) {
156
+ const arg = argv[index];
157
+ if (!arg) continue;
158
+
159
+ if (arg === "--help" || arg === "-h") {
160
+ console.log(HELP_TEXT);
161
+ process.exit(0);
162
+ }
163
+
164
+ if (arg === "--json") {
165
+ args.isJson = true;
166
+ continue;
167
+ }
168
+
169
+ if (arg === "--markdown") {
170
+ args.markdownPath = requireValue(argv, index, arg);
171
+ index += 1;
172
+ continue;
173
+ }
174
+
175
+ if (arg.startsWith("--markdown=")) {
176
+ args.markdownPath = arg.slice("--markdown=".length);
177
+ continue;
178
+ }
179
+
180
+ if (arg === "--smoke-note") {
181
+ args.smokeNote = requireValue(argv, index, arg);
182
+ index += 1;
183
+ continue;
184
+ }
185
+
186
+ if (arg.startsWith("--smoke-note=")) {
187
+ args.smokeNote = arg.slice("--smoke-note=".length);
188
+ continue;
189
+ }
190
+
191
+ if (arg === "--tier") {
192
+ args.tier = parseTier(requireValue(argv, index, arg));
193
+ index += 1;
194
+ continue;
195
+ }
196
+
197
+ if (arg.startsWith("--tier=")) {
198
+ args.tier = parseTier(arg.slice("--tier=".length));
199
+ continue;
200
+ }
201
+
202
+ if (arg.startsWith("-")) {
203
+ throw new Error(`Unknown option: ${arg}`);
204
+ }
205
+
206
+ if (!args.providerPath) {
207
+ args.providerPath = arg;
208
+ continue;
209
+ }
210
+
211
+ throw new Error(`Unexpected argument: ${arg}`);
212
+ }
213
+
214
+ return args;
215
+ }
216
+
217
+ function requireValue(argv: string[], index: number, label: string): string {
218
+ const value = argv[index + 1];
219
+ if (!value) {
220
+ throw new Error(`Missing value for ${label}.`);
221
+ }
222
+ return value;
223
+ }
224
+
225
+ function parseTier(value: string): BountyTier {
226
+ if (isBountyTier(value)) {
227
+ return value;
228
+ }
229
+ throw new Error(
230
+ `Invalid --tier "${value}". Expected one of: ${TIERS.join(", ")}`,
231
+ );
232
+ }
233
+
234
+ function isBountyTier(value: string): value is BountyTier {
235
+ return TIER_VALUES.has(value);
236
+ }
237
+
238
+ export async function buildSubmitCheckReport(
239
+ providerRoot: string,
240
+ args: { smokeNote?: string; tier?: BountyTier } = {},
241
+ ): Promise<SubmitCheckReport> {
242
+ const checks: SubmitCheck[] = [];
243
+ const baseChecks = await safeRunChecks(providerRoot);
244
+ const provider = await safeLoadProvider(providerRoot);
245
+
246
+ checks.push(...scoreBaseChecks(baseChecks));
247
+ checks.push(scoreProviderIdSlug(providerRoot, provider));
248
+ checks.push(scoreNoVendorShim(providerRoot));
249
+ checks.push(scoreNoVendorImport(providerRoot));
250
+ checks.push(scoreDescribeKey(providerRoot));
251
+ checks.push(scoreNoRawFetch(providerRoot));
252
+ checks.push(scoreNoRedundantRuntimeGuards(providerRoot));
253
+ checks.push(scoreManagedBrowserRuntime(providerRoot));
254
+ checks.push(scoreAsAssertionCount(providerRoot));
255
+ checks.push(scoreUnsafeInputPassthrough(providerRoot));
256
+ checks.push(scoreUnjustifiedLooseSchema(providerRoot));
257
+ checks.push(scoreFlatOperationComposition(providerRoot));
258
+
259
+ if (provider) {
260
+ checks.push(scoreCredentialUsage(providerRoot, provider));
261
+ checks.push(scoreLocaleCatalog(providerRoot, provider));
262
+ checks.push(scoreOperationMetadata(provider));
263
+ checks.push(scoreFixtureCoverage(provider));
264
+ checks.push(scoreHealthCoverage(provider));
265
+ checks.push(scoreAuthSafety(provider));
266
+ checks.push(scoreSmokeEvidence(args.smokeNote));
267
+ checks.push(...scoreProviderDocs(providerRoot));
268
+ checks.push(scoreRepositoryDx(providerRoot));
269
+ checks.push(scoreSecrets(providerRoot));
270
+ } else {
271
+ checks.push(
272
+ blocker(
273
+ "provider-load",
274
+ "definition",
275
+ "Provider could not be loaded.",
276
+ "Fix index.ts so it default-exports defineProvider(...).",
277
+ CATEGORY_MAX_POINTS.definition,
278
+ ),
279
+ );
280
+ }
281
+
282
+ const total = clamp(
283
+ Math.round(checks.reduce((sum, check) => sum + check.points, 0)),
284
+ 0,
285
+ 100,
286
+ );
287
+ const blockers = checks.filter(
288
+ (check) => check.level === "blocker" && check.status === "fail",
289
+ ).length;
290
+ const warnings = checks.filter((check) => check.status === "warn").length;
291
+ const passed = checks.filter((check) => check.status === "pass").length;
292
+ const verdict: Verdict =
293
+ blockers > 0
294
+ ? "blocked"
295
+ : total >= 90 && warnings === 0
296
+ ? "ready"
297
+ : "reviewable_with_warnings";
298
+
299
+ return {
300
+ schemaVersion: 1,
301
+ generatedAt: new Date().toISOString(),
302
+ provider: {
303
+ id: provider?.id ?? basename(providerRoot),
304
+ version: provider?.version ?? "unknown",
305
+ runtime: provider?.runtime ?? "unknown",
306
+ authMode: provider?.auth?.mode ?? "none",
307
+ sdkVersion: packageJson.version,
308
+ ...(args.tier ? { tier: args.tier } : {}),
309
+ },
310
+ score: { total, max: 100, verdict },
311
+ summary: { blockers, warnings, passed },
312
+ checks,
313
+ };
314
+ }
315
+
316
+ function scoreProviderIdSlug(
317
+ providerRoot: string,
318
+ provider: ProviderDefinition | undefined,
319
+ ): SubmitCheck {
320
+ const remediation =
321
+ 'Rename defineProvider({ id }) to the short slug (e.g. "tabelog", not "apifuse-provider-tabelog"). Also update manifest/PROVIDER_ID consts and tests. Grep: git grep "apifuse-provider-<name>".';
322
+
323
+ // Prefer the loaded provider id; fall back to scanning source so the rule
324
+ // still fires when the provider fails to load (e.g. a vendor shim or other
325
+ // structural problem prevents defineProvider from resolving).
326
+ if (provider) {
327
+ if (provider.id.startsWith(VENDOR_SHIM_PROVIDER_ID_PREFIX)) {
328
+ return blocker(
329
+ "id-slug",
330
+ SDK_NATIVE_CATEGORY,
331
+ "Provider id uses the apifuse-provider- prefix.",
332
+ remediation,
333
+ 0,
334
+ [provider.id],
335
+ );
336
+ }
337
+
338
+ return pass(
339
+ "id-slug",
340
+ SDK_NATIVE_CATEGORY,
341
+ "Provider id uses the short slug.",
342
+ 0,
343
+ );
344
+ }
345
+
346
+ const findings = findSourceLineMatches(
347
+ providerRoot,
348
+ /["'`]apifuse-provider-[a-z0-9-]/i,
349
+ );
350
+ if (findings.length > 0) {
351
+ return blocker(
352
+ "id-slug",
353
+ SDK_NATIVE_CATEGORY,
354
+ "Provider id uses the apifuse-provider- prefix.",
355
+ remediation,
356
+ 0,
357
+ formatSourceFindings(findings),
358
+ );
359
+ }
360
+
361
+ return pass(
362
+ "id-slug",
363
+ SDK_NATIVE_CATEGORY,
364
+ "Provider id uses the short slug.",
365
+ 0,
366
+ );
367
+ }
368
+
369
+ function scoreNoVendorShim(providerRoot: string): SubmitCheck {
370
+ const vendorPath = resolve(providerRoot, "vendor");
371
+ if (existsSync(vendorPath)) {
372
+ return blocker(
373
+ "no-vendor-shim",
374
+ SDK_NATIVE_CATEGORY,
375
+ "Provider contains a vendor/ SDK shim directory.",
376
+ "Delete vendor/ and import directly from @apifuse/provider-sdk (/provider, root, /testing). SDK-absent symbols (e.g. createStateContext) must use real SDK equivalents (createUnsupportedProviderRuntimeState for unused ctx.state).",
377
+ 0,
378
+ [vendorPath],
379
+ );
380
+ }
381
+
382
+ return pass(
383
+ "no-vendor-shim",
384
+ SDK_NATIVE_CATEGORY,
385
+ "Provider does not contain a vendor/ SDK shim directory.",
386
+ 0,
387
+ );
388
+ }
389
+
390
+ function scoreNoVendorImport(providerRoot: string): SubmitCheck {
391
+ const findings = findSourceLineMatches(
392
+ providerRoot,
393
+ /from\s+["'][^"']*vendor\//,
394
+ );
395
+ if (findings.length > 0) {
396
+ return blocker(
397
+ "no-vendor-import",
398
+ SDK_NATIVE_CATEGORY,
399
+ "Provider source imports from vendor/ shim.",
400
+ "Re-point every import from ../vendor/provider-sdk to @apifuse/provider-sdk/provider, @apifuse/provider-sdk, or @apifuse/provider-sdk/testing.",
401
+ 0,
402
+ formatSourceFindings(findings),
403
+ );
404
+ }
405
+
406
+ return pass(
407
+ "no-vendor-import",
408
+ SDK_NATIVE_CATEGORY,
409
+ "Provider source imports directly from the SDK.",
410
+ 0,
411
+ );
412
+ }
413
+
414
+ function scoreDescribeKey(providerRoot: string): SubmitCheck {
415
+ const findings = findSourceLineMatches(providerRoot, /\.describe\(["']/);
416
+ if (findings.length > 0) {
417
+ return blocker(
418
+ "describe-key",
419
+ SDK_NATIVE_CATEGORY,
420
+ "Schema descriptions use raw .describe() prose instead of describeKey.",
421
+ 'Replace .describe("prose") with describeKey(schema, key, { description }) backed by locale keys in locales/en.json + ko.json.',
422
+ 0,
423
+ formatSourceFindings(findings),
424
+ );
425
+ }
426
+
427
+ return pass(
428
+ "describe-key",
429
+ SDK_NATIVE_CATEGORY,
430
+ "Schema descriptions use describeKey.",
431
+ 0,
432
+ );
433
+ }
434
+
435
+ function scoreNoRawFetch(providerRoot: string): SubmitCheck {
436
+ const findings = findSourceLineMatches(providerRoot, /(?<![.\w])fetch\s*\(/);
437
+ if (findings.length > 0) {
438
+ return blocker(
439
+ "no-raw-fetch",
440
+ SDK_NATIVE_CATEGORY,
441
+ "Provider source calls raw fetch().",
442
+ "Replace raw fetch() with ctx.stealth.fetch() (cloud-IP-blocked otherwise) or ctx.http for non-stealth calls.",
443
+ 0,
444
+ formatSourceFindings(findings),
445
+ );
446
+ }
447
+
448
+ return pass(
449
+ "no-raw-fetch",
450
+ SDK_NATIVE_CATEGORY,
451
+ "Provider source avoids raw fetch().",
452
+ 0,
453
+ );
454
+ }
455
+
456
+ const REDUNDANT_RUNTIME_GUARD_PATTERNS: readonly RegExp[] = [
457
+ /\bctx\.(?:stealth|http|cache|state|browser|trace|auth|stt|choice)\?\./,
458
+ ];
459
+
460
+ const SDK_CONTEXT_METHOD_ALIAS_PATTERN =
461
+ /\bconst\s+(\w+)\s*=\s*ctx\.(?:stealth|http|cache|state|browser|trace|auth|stt|choice)\.(?:\w+)/;
462
+
463
+ function hasRedundantRuntimeGuard(
464
+ line: string,
465
+ remainingLines: readonly string[],
466
+ ): boolean {
467
+ if (REDUNDANT_RUNTIME_GUARD_PATTERNS.some((pattern) => pattern.test(line))) {
468
+ return true;
469
+ }
470
+
471
+ const aliasMatch = SDK_CONTEXT_METHOD_ALIAS_PATTERN.exec(line);
472
+ const alias = aliasMatch?.[1];
473
+ if (!alias) {
474
+ return false;
475
+ }
476
+
477
+ const guardPattern = new RegExp(
478
+ `(?:typeof\\s+${alias}\\s*!==\\s*["']function["']|!${alias}\\b)`,
479
+ );
480
+ return remainingLines
481
+ .slice(0, 8)
482
+ .some((candidate) => guardPattern.test(candidate));
483
+ }
484
+
485
+ function scoreNoRedundantRuntimeGuards(providerRoot: string): SubmitCheck {
486
+ const findings = findSourceFindings(providerRoot, hasRedundantRuntimeGuard);
487
+ if (findings.length > 0) {
488
+ return blocker(
489
+ "no-redundant-runtime-guards",
490
+ SDK_NATIVE_CATEGORY,
491
+ "Provider source has redundant runtime guard code for SDK-owned context APIs.",
492
+ "Trust the provider SDK context contract: call ctx.stealth.fetch(), ctx.http, and other SDK-owned context APIs directly. Remove optional chaining and typeof function guards around non-null runtime clients.",
493
+ 0,
494
+ formatSourceFindings(findings),
495
+ );
496
+ }
497
+
498
+ return pass(
499
+ "no-redundant-runtime-guards",
500
+ SDK_NATIVE_CATEGORY,
501
+ "Provider source avoids redundant runtime guard code around SDK-owned context APIs.",
502
+ 0,
503
+ );
504
+ }
505
+
506
+ const AS_ASSERTION_PATTERN =
507
+ /\bas\s+(any|unknown|never|string|number|boolean)\b|\bas\s+[A-Z]|\bas\s+\{|\bas\s+Record\b|\bas\s+typeof\b/;
508
+
509
+ function countAsAssertions(providerRoot: string): {
510
+ count: number;
511
+ findings: SourceFinding[];
512
+ } {
513
+ let count = 0;
514
+ const findings: SourceFinding[] = [];
515
+
516
+ for (const filePath of listNonTestTypeScriptFiles(providerRoot)) {
517
+ const content = readFileSync(filePath, "utf8");
518
+ const lines = content.split(/\r?\n/);
519
+ for (let index = 0; index < lines.length; index += 1) {
520
+ const line = lines[index];
521
+ if (
522
+ line === undefined ||
523
+ line.includes("import") ||
524
+ /\bas\s*const\b/.test(line) ||
525
+ !AS_ASSERTION_PATTERN.test(line)
526
+ ) {
527
+ continue;
528
+ }
529
+
530
+ count += 1;
531
+ if (findings.length < MAX_SOURCE_FINDING_EVIDENCE) {
532
+ findings.push({
533
+ file: toRelativeProviderPath(providerRoot, filePath),
534
+ line: index + 1,
535
+ });
536
+ }
537
+ }
538
+ }
539
+
540
+ return { count, findings };
541
+ }
542
+
543
+ function scoreAsAssertionCount(providerRoot: string): SubmitCheck {
544
+ const { count, findings } = countAsAssertions(providerRoot);
545
+ const assertionLabel = "as " + "Type";
546
+ const remediation = `Replace \`${assertionLabel}\` with zod \`schema.safeParse()\` or \`if ('key' in obj)\` type guards. \`as const\` is allowed.`;
547
+
548
+ if (count > 20) {
549
+ return blocker(
550
+ "as-assertion-count",
551
+ SDK_NATIVE_CATEGORY,
552
+ `Provider uses ${count} type assertions (${assertionLabel}). Replace with zod safeParse or type guards.`,
553
+ remediation,
554
+ 0,
555
+ formatSourceFindings(findings),
556
+ );
557
+ }
558
+
559
+ if (count >= 6) {
560
+ return {
561
+ id: "as-assertion-count",
562
+ category: SDK_NATIVE_CATEGORY,
563
+ level: "warn",
564
+ status: "warn",
565
+ points: 0,
566
+ maxPoints: 0,
567
+ message: `Provider uses ${count} type assertions (${assertionLabel}). Replace with zod safeParse or type guards.`,
568
+ remediation,
569
+ evidence: formatSourceFindings(findings),
570
+ };
571
+ }
572
+
573
+ return pass(
574
+ "as-assertion-count",
575
+ SDK_NATIVE_CATEGORY,
576
+ "Type assertions are within the recommended limit.",
577
+ 0,
578
+ );
579
+ }
580
+
581
+ // Returns true when `findingLine` (1-based) or the line directly above it
582
+ // carries an `// @apifuse-allow <ruleId>:` acknowledgement comment.
583
+ function hasAllowOverride(
584
+ lines: readonly string[],
585
+ findingLine: number,
586
+ ruleId: string,
587
+ ): boolean {
588
+ const pattern = new RegExp(`@apifuse-allow\\s+${ruleId}\\b`);
589
+ const current = lines[findingLine - 1];
590
+ const previous = lines[findingLine - 2];
591
+ return (
592
+ (current !== undefined && pattern.test(current)) ||
593
+ (previous !== undefined && pattern.test(previous))
594
+ );
595
+ }
596
+
597
+ // Splits source findings into non-overridden (still violations) and
598
+ // acknowledged (escape-hatched) sets by re-reading each file's lines.
599
+ function partitionAllowOverrides(
600
+ providerRoot: string,
601
+ findings: readonly SourceFinding[],
602
+ ruleId: string,
603
+ ): { violations: SourceFinding[]; overridden: SourceFinding[] } {
604
+ const fileLineCache = new Map<string, string[]>();
605
+ const violations: SourceFinding[] = [];
606
+ const overridden: SourceFinding[] = [];
607
+
608
+ for (const finding of findings) {
609
+ const absolute = resolve(providerRoot, finding.file);
610
+ let lines = fileLineCache.get(absolute);
611
+ if (lines === undefined) {
612
+ lines = readFileSync(absolute, "utf8").split(/\r?\n/);
613
+ fileLineCache.set(absolute, lines);
614
+ }
615
+ if (hasAllowOverride(lines, finding.line, ruleId)) {
616
+ overridden.push(finding);
617
+ } else {
618
+ violations.push(finding);
619
+ }
620
+ }
621
+
622
+ return { violations, overridden };
623
+ }
624
+
625
+ // Builds a blocker/warn/pass result for an escape-hatch-aware rule:
626
+ // any non-overridden finding => blocker; only acknowledged overrides => warn;
627
+ // nothing => pass.
628
+ function escapeHatchResult(
629
+ providerRoot: string,
630
+ ruleId: string,
631
+ findings: readonly SourceFinding[],
632
+ copy: { blockerMessage: string; remediation: string; passMessage: string },
633
+ ): SubmitCheck {
634
+ if (findings.length === 0) {
635
+ return pass(ruleId, SDK_NATIVE_CATEGORY, copy.passMessage, 0);
636
+ }
637
+
638
+ const { violations, overridden } = partitionAllowOverrides(
639
+ providerRoot,
640
+ findings,
641
+ ruleId,
642
+ );
643
+
644
+ if (violations.length > 0) {
645
+ return blocker(
646
+ ruleId,
647
+ SDK_NATIVE_CATEGORY,
648
+ copy.blockerMessage,
649
+ copy.remediation,
650
+ 0,
651
+ formatSourceFindings(violations),
652
+ );
653
+ }
654
+
655
+ return {
656
+ id: ruleId,
657
+ category: SDK_NATIVE_CATEGORY,
658
+ level: "warn",
659
+ status: "warn",
660
+ points: 0,
661
+ maxPoints: 0,
662
+ message: `${copy.blockerMessage} ${overridden.length} acknowledged @apifuse-allow override(s).`,
663
+ remediation: copy.remediation,
664
+ evidence: formatSourceFindings(overridden),
665
+ };
666
+ }
667
+
668
+ // 1-based line number of a character offset in `source`.
669
+ function offsetToLine(source: string, offset: number): number {
670
+ let line = 1;
671
+ for (let index = 0; index < offset && index < source.length; index += 1) {
672
+ if (source[index] === "\n") {
673
+ line += 1;
674
+ }
675
+ }
676
+ return line;
677
+ }
678
+
679
+ // ---------------------------------------------------------------------------
680
+ // SDK-native structural rules (input-passthrough, loose-schema, flat-operation)
681
+ //
682
+ // SCOPE & LIMITATION: these checks are source-grep heuristics, not a full AST
683
+ // analysis. They are deliberately tuned against the new-structure golden corpus
684
+ // (demaecan / kakaomap / triple) to catch the common non-standard SDK
685
+ // integration shapes seen in bounty submissions: inline/aliased/multi-line
686
+ // input .passthrough(), unjustified loose schemas, and factory-composed
687
+ // operations (inline, aliased, destructured, sibling-module, or unresolved
688
+ // import). They balance brackets and resolve one alias hop across the whole
689
+ // provider submission so trivial formatting/aliasing/module-split bypasses do
690
+ // not slip through.
691
+ //
692
+ // The flat-operation rule guards the "unsafe form" (an op map built by an
693
+ // OPAQUE builder whose operation set is hidden at the call site), not the mere
694
+ // presence of a function call. The stdlib enumerate-and-reshape idiom
695
+ // `Object.fromEntries(Object.entries(<source-visible obj>) ...)` is exempted:
696
+ // its op set still originates from a source-enumerable object and is only
697
+ // filtered/reshaped by pure built-ins. This is the verified golden pattern
698
+ // (triple narrows a statically-defined op object by a whitelist Set). Any other
699
+ // call — `makeOperations()`, a destructured factory, or
700
+ // `Object.fromEntries(buildEntries())` with no source-visible `Object.entries`
701
+ // — stays classified as factory composition and is blocked.
702
+ //
703
+ // They do NOT achieve AST-completeness. Known residual bypasses (schemas or
704
+ // operation maps imported from an external npm package, computed/dynamic
705
+ // property construction, or deliberate obfuscation) are out of reach for a
706
+ // text scan. submit-check is a bounty-workspace gate that runs ALONGSIDE human
707
+ // review; manual review remains the final backstop for adversarial submissions.
708
+ // Promoting these rules to a real TypeScript AST pass (ts.createSourceFile)
709
+ // is tracked as deferred follow-up work (Phase 8.7) and would require adding
710
+ // TypeScript as a provider-sdk dependency.
711
+ // ---------------------------------------------------------------------------
712
+
713
+ // Matches a `.passthrough()` call tolerant of whitespace before the parens or
714
+ // between them, so `.passthrough ()` / `.passthrough\n()` are still detected.
715
+ const PASSTHROUGH_CALL = /\.passthrough\s*\(\s*\)/;
716
+
717
+ // Strips redundant wrapping parentheses from an expression so that a value like
718
+ // `(makeOperations())` or `((x))` classifies the same as `makeOperations()`.
719
+ // Only unwraps when the leading `(` matches the trailing `)` at depth 0 (i.e.
720
+ // the whole expression is parenthesized), preserving call expressions such as
721
+ // `makeOperations()` whose first `(` is not a wrapper.
722
+ function unwrapParens(expr: string): string {
723
+ let value = expr.trim();
724
+ while (value.startsWith("(")) {
725
+ let depth = 0;
726
+ let matchIndex = -1;
727
+ for (let i = 0; i < value.length; i += 1) {
728
+ const ch = value[i];
729
+ if (ch === "(") {
730
+ depth += 1;
731
+ } else if (ch === ")") {
732
+ depth -= 1;
733
+ if (depth === 0) {
734
+ matchIndex = i;
735
+ break;
736
+ }
737
+ }
738
+ }
739
+ // Only a true wrapper spans the entire expression (closing paren is the
740
+ // last char). Otherwise the leading `(` belongs to a sub-expression.
741
+ if (matchIndex === value.length - 1) {
742
+ value = value.slice(1, -1).trim();
743
+ } else {
744
+ break;
745
+ }
746
+ }
747
+ return value;
748
+ }
749
+
750
+ // Returns the value-expression substring starting at `valueStart`, balanced
751
+ // across (){}[] and stopping at the first top-level `,`/`;` or unmatched
752
+ // closing bracket. This lets a property value be read across newlines, so a
753
+ // multi-line `input: z.object({...})\n.passthrough()` is captured whole.
754
+ function balancedValueExpression(source: string, valueStart: number): string {
755
+ let depth = 0;
756
+ let index = valueStart;
757
+ for (; index < source.length; index += 1) {
758
+ const ch = source[index];
759
+ if (ch === "(" || ch === "{" || ch === "[") {
760
+ depth += 1;
761
+ } else if (ch === ")" || ch === "}" || ch === "]") {
762
+ if (depth === 0) {
763
+ break;
764
+ }
765
+ depth -= 1;
766
+ } else if ((ch === "," || ch === ";") && depth === 0) {
767
+ break;
768
+ }
769
+ }
770
+ return source.slice(valueStart, index);
771
+ }
772
+
773
+ // True when an object-literal expression spreads a CALL expression at its top
774
+ // level, e.g. `{ ...makeOperations() }` or `{ ...a, ...build(x) }`. Spreads
775
+ // nested deeper than the outer object (inside handler bodies, nested objects,
776
+ // or arrays) are ignored, so only a factory composition of the object itself
777
+ // is detected. Input is expected to start at the outer `{`.
778
+ function hasTopLevelFactorySpread(expr: string): boolean {
779
+ const open = expr.indexOf("{");
780
+ if (open === -1) {
781
+ return false;
782
+ }
783
+ let depth = 0;
784
+ for (let i = open; i < expr.length; i += 1) {
785
+ const ch = expr[i];
786
+ if (ch === "{" || ch === "(" || ch === "[") {
787
+ depth += 1;
788
+ } else if (ch === "}" || ch === ")" || ch === "]") {
789
+ depth -= 1;
790
+ if (depth === 0) {
791
+ break;
792
+ }
793
+ } else if (ch === "." && depth === 1 && expr.startsWith("...", i)) {
794
+ // A spread at the object's own level. Check whether the spread
795
+ // argument is a call expression (factory) rather than a plain
796
+ // identifier/member spread of an already-built object.
797
+ const rest = expr.slice(i + 3);
798
+ if (/^\s*[A-Za-z_$][\w$.]*\s*\(/.test(rest)) {
799
+ return true;
800
+ }
801
+ }
802
+ }
803
+ return false;
804
+ }
805
+
806
+ // Collects the depth-1 spread IDENTIFIERS of an object-literal expression that
807
+ // are bare identifiers (not call expressions), e.g. `{ ...hidden, ...base }` ->
808
+ // ["hidden", "base"]. A `...makeOps()` call spread is already caught by
809
+ // hasTopLevelFactorySpread, so it is excluded here. These identifiers must be
810
+ // resolved to their declarations: `const hidden = makeOperations()` spread as
811
+ // `{ ...hidden }` is still a factory-composed map and must block.
812
+ function topLevelSpreadIdentifiers(expr: string): string[] {
813
+ const open = expr.indexOf("{");
814
+ if (open === -1) {
815
+ return [];
816
+ }
817
+ const names: string[] = [];
818
+ let depth = 0;
819
+ for (let i = open; i < expr.length; i += 1) {
820
+ const ch = expr[i];
821
+ if (ch === "{" || ch === "(" || ch === "[") {
822
+ depth += 1;
823
+ } else if (ch === "}" || ch === ")" || ch === "]") {
824
+ depth -= 1;
825
+ if (depth === 0) {
826
+ break;
827
+ }
828
+ } else if (ch === "." && depth === 1 && expr.startsWith("...", i)) {
829
+ const rest = expr.slice(i + 3);
830
+ // Bare identifier spread (no call parens) -> needs declaration
831
+ // resolution. `...obj.prop` member spreads are treated as already
832
+ // built and ignored (the leading identifier is captured).
833
+ const m = rest.match(/^\s*([A-Za-z_$][\w$]*)\s*(?![\w$(])/);
834
+ if (m?.[1]) {
835
+ names.push(m[1]);
836
+ }
837
+ }
838
+ }
839
+ return names;
840
+ }
841
+
842
+ // A call expression is an OPAQUE builder (block) when it invokes a
843
+ // provider-authored function whose body — and therefore the operation set — is
844
+ // not visible at the call site, e.g. `makeOperations()` or a destructured
845
+ // `const { operations } = createProviderComposition(...)`. It is NOT opaque
846
+ // when it is the stdlib `Object.fromEntries(Object.entries(<obj>) ...)`
847
+ // enumerate-and-reshape idiom: the operation set still originates from a
848
+ // source-visible object (the `Object.entries(...)` argument) and is merely
849
+ // filtered/reshaped by pure built-ins, so the registry/reviewer can still
850
+ // enumerate the op map from source. This is the verified golden pattern (a
851
+ // statically-defined op object narrowed by a whitelist Set).
852
+ //
853
+ // The exemption requires `Object.entries(` to be the ROOT of fromEntries'
854
+ // FIRST argument — not merely present somewhere inside the expression. This
855
+ // rejects opaque maps that only mention `Object.entries` deeper in a predicate,
856
+ // e.g. `Object.fromEntries(buildEntries().filter(([id]) => Object.entries(ALLOWED).some(...)))`,
857
+ // whose entries still originate from the opaque `buildEntries()` call. Any other
858
+ // expression — `Object.fromEntries(buildEntries())`, a destructured factory,
859
+ // `makeOperations()` — stays classified as factory composition.
860
+ const TRANSPARENT_RESHAPE_HEAD = /^Object\s*\.\s*fromEntries\s*\(/;
861
+ const OBJECT_ENTRIES_HEAD = /^Object\s*\.\s*entries\s*\(/;
862
+ function isTransparentObjectReshape(expr: string): boolean {
863
+ const head = TRANSPARENT_RESHAPE_HEAD.exec(expr);
864
+ if (!head) {
865
+ return false;
866
+ }
867
+ // First argument starts immediately after `fromEntries(`. The reshape is
868
+ // transparent only when that argument's root callee is `Object.entries(`
869
+ // (optionally chained: `Object.entries(obj).filter(...)`), so the source
870
+ // object is enumerable from source rather than produced by an opaque call.
871
+ const firstArg = expr.slice(head[0].length).trimStart();
872
+ return OBJECT_ENTRIES_HEAD.test(firstArg);
873
+ }
874
+
875
+ // Decide whether an `input:` property at `propIndex` is an operation's public
876
+ // input schema (the thing the rule guards) or merely a field literally named
877
+ // "input" inside a zod schema body (e.g. modelling an upstream payload that
878
+ // happens to have an `input` field: `z.object({ input: z.object(...) })`).
879
+ // We walk backwards to the directly-enclosing `{` and inspect the token that
880
+ // opened it: if that brace is the argument of a zod builder call such as
881
+ // `z.object(`, `z.strictObject(`, `z.looseObject(`, `z.record(`, or a bare
882
+ // `.object(` / `.shape(`, the `input` key is a schema field, not an operation
883
+ // input. Operation inputs live in a plain object literal (the operation
884
+ // definition), so their enclosing `{` is NOT immediately preceded by `(` of a
885
+ // schema builder.
886
+ function inputKeyIsSchemaField(source: string, propIndex: number): boolean {
887
+ let depth = 0;
888
+ let i = propIndex - 1;
889
+ for (; i >= 0; i -= 1) {
890
+ const ch = source[i];
891
+ if (ch === "}" || ch === ")" || ch === "]") {
892
+ depth += 1;
893
+ } else if (ch === "(" || ch === "[") {
894
+ if (depth === 0) {
895
+ // Reached an opening paren/bracket that directly contains the
896
+ // property — an array/call arg position, not an object literal.
897
+ return false;
898
+ }
899
+ depth -= 1;
900
+ } else if (ch === "{") {
901
+ if (depth === 0) {
902
+ break;
903
+ }
904
+ depth -= 1;
905
+ }
906
+ }
907
+ if (i < 0) {
908
+ return false;
909
+ }
910
+ // `i` indexes the directly-enclosing `{`. Look at the non-whitespace text
911
+ // immediately before it. A zod object/record builder opens with `(` then
912
+ // optionally whitespace then `{`, so the char before `{` is `(` and the
913
+ // callee just before that `(` is a zod builder identifier.
914
+ let j = i - 1;
915
+ while (j >= 0 && /\s/.test(source[j] ?? "")) {
916
+ j -= 1;
917
+ }
918
+ if (source[j] !== "(") {
919
+ return false;
920
+ }
921
+ // Capture the callee identifier chain that ends at this `(` and test its
922
+ // final member against the set of zod builders that take an object body.
923
+ const before = source.slice(Math.max(0, j - 60), j);
924
+ const calleeMatch = before.match(/([A-Za-z_$][\w$]*)\s*$/);
925
+ const callee = calleeMatch?.[1];
926
+ if (callee === undefined) {
927
+ return false;
928
+ }
929
+ const SCHEMA_BODY_BUILDERS = new Set([
930
+ "object",
931
+ "strictObject",
932
+ "looseObject",
933
+ "record",
934
+ "shape",
935
+ "extend",
936
+ "merge",
937
+ "catchall",
938
+ "partial",
939
+ "required",
940
+ "pick",
941
+ "omit",
942
+ "augment",
943
+ ]);
944
+ return SCHEMA_BODY_BUILDERS.has(callee);
945
+ }
946
+
947
+ // True when `source` imports the binding `name` from another module, i.e. a
948
+ // top-level `import { ..., name, ... } from "..."` (named or aliased) or a
949
+ // default/namespace import of `name`. Used to confirm an `input: <alias>`
950
+ // reference actually binds to an imported declaration before resolving it
951
+ // against the provider-wide passthrough map (prevents same-name collisions
952
+ // across unrelated modules from producing false positives).
953
+ function fileImportsBinding(source: string, name: string): boolean {
954
+ const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
955
+ return new RegExp(`\\bimport\\b[^;]*\\b${escaped}\\b[^;]*\\bfrom\\b`).test(
956
+ source,
957
+ );
958
+ }
959
+
960
+ // Resolves the ORIGINAL exported name for a local binding `localName`. When the
961
+ // file imports it under an alias — `import { requestSchema as inputSchema }` —
962
+ // the provider-wide passthrough map is keyed by the exported declaration name
963
+ // (`requestSchema`), not the local alias (`inputSchema`), so the alias must be
964
+ // mapped back before lookup. Returns `localName` unchanged when there is no
965
+ // aliased import (plain `import { requestSchema }` or a local declaration).
966
+ function importedOriginalName(source: string, localName: string): string {
967
+ const escaped = localName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
968
+ // Match `<original> as <localName>` inside any import specifier list.
969
+ const aliasMatch = new RegExp(
970
+ `\\bimport\\b[^;]*\\{[^}]*\\b([A-Za-z_$][\\w$]*)\\s+as\\s+${escaped}\\b[^}]*\\}[^;]*\\bfrom\\b`,
971
+ ).exec(source);
972
+ return aliasMatch?.[1] ?? localName;
973
+ }
974
+
975
+ function scoreUnsafeInputPassthrough(providerRoot: string): SubmitCheck {
976
+ const findings: SourceFinding[] = [];
977
+ const files = listNonTestTypeScriptFiles(providerRoot);
978
+
979
+ // Pass 1: collect every passthrough schema const across the WHOLE provider
980
+ // submission (not per-file), keyed by name -> declaration site. This lets an
981
+ // `input:` in index.ts resolve a non-`input`-named passthrough schema that
982
+ // was declared in another module (e.g. schemas.ts) and imported.
983
+ type ConstSite = { file: string; line: number };
984
+ const passthroughConsts = new Map<string, ConstSite>();
985
+ // Per-file map of passthrough const declarations, so an `input: <alias>` can
986
+ // resolve its ACTUAL binding (a same-file local declaration) before falling
987
+ // back to an imported cross-module schema. This prevents a generic name like
988
+ // `requestSchema` declared in one module from being matched against an
989
+ // unrelated `input: requestSchema` in another module (a false positive on a
990
+ // strict schema that merely shares the identifier).
991
+ const passthroughByFile = new Map<string, Map<string, ConstSite>>();
992
+ const fileSources = new Map<string, string>();
993
+ for (const filePath of files) {
994
+ const source = readFileSync(filePath, "utf8");
995
+ const relPath = toRelativeProviderPath(providerRoot, filePath);
996
+ fileSources.set(filePath, source);
997
+ const localMap = new Map<string, ConstSite>();
998
+ passthroughByFile.set(filePath, localMap);
999
+ const constDecl =
1000
+ /(?:^|\n)[ \t]*(?:export\s+)?(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=\n]+)?\s*=/g;
1001
+ for (
1002
+ let match = constDecl.exec(source);
1003
+ match !== null;
1004
+ match = constDecl.exec(source)
1005
+ ) {
1006
+ const name = match[1];
1007
+ if (name === undefined) {
1008
+ continue;
1009
+ }
1010
+ const valueStart = match.index + match[0].length;
1011
+ const value = balancedValueExpression(source, valueStart);
1012
+ if (PASSTHROUGH_CALL.test(value)) {
1013
+ const site: ConstSite = {
1014
+ file: relPath,
1015
+ line: offsetToLine(source, valueStart),
1016
+ };
1017
+ localMap.set(name, site);
1018
+ // First declaration wins for line attribution; duplicate names
1019
+ // across modules are rare and either site is a valid pointer.
1020
+ if (!passthroughConsts.has(name)) {
1021
+ passthroughConsts.set(name, site);
1022
+ }
1023
+ }
1024
+ }
1025
+ }
1026
+
1027
+ const seen = new Set<string>();
1028
+ const push = (site: ConstSite) => {
1029
+ const key = `${site.file}:${site.line}`;
1030
+ if (!seen.has(key)) {
1031
+ seen.add(key);
1032
+ findings.push({ file: site.file, line: site.line });
1033
+ }
1034
+ };
1035
+
1036
+ // Pass 2: inspect every `input:` property value across all files. A value
1037
+ // that is itself a passthrough expression, or that references a passthrough
1038
+ // const by name (resolved against the provider-wide map), is a violation.
1039
+ for (const filePath of files) {
1040
+ const source = fileSources.get(filePath) ?? readFileSync(filePath, "utf8");
1041
+ const relPath = toRelativeProviderPath(providerRoot, filePath);
1042
+
1043
+ const inputProp = /\binput\s*:\s*/g;
1044
+ for (
1045
+ let match = inputProp.exec(source);
1046
+ match !== null;
1047
+ match = inputProp.exec(source)
1048
+ ) {
1049
+ // Skip `input` keys that are fields inside a zod schema body (e.g. an
1050
+ // upstream payload modelled as `z.object({ input: ... })`). Only an
1051
+ // operation's public `input:` property is in scope for this rule.
1052
+ if (inputKeyIsSchemaField(source, match.index)) {
1053
+ continue;
1054
+ }
1055
+ const valueStart = match.index + match[0].length;
1056
+ const value = balancedValueExpression(source, valueStart);
1057
+ if (PASSTHROUGH_CALL.test(value)) {
1058
+ push({ file: relPath, line: offsetToLine(source, valueStart) });
1059
+ continue;
1060
+ }
1061
+ const ref = value.trim().match(/^([A-Za-z_$][\w$]*)/);
1062
+ const refName = ref?.[1];
1063
+ if (refName) {
1064
+ // Resolve the alias by BINDING, not by global name. Prefer a
1065
+ // passthrough const declared in THIS file; otherwise only fall
1066
+ // back to the provider-wide map when this file actually imports
1067
+ // `refName` (so a generic name shared across modules cannot link
1068
+ // an unrelated strict input to a foreign passthrough schema).
1069
+ const localSite = passthroughByFile.get(filePath)?.get(refName);
1070
+ if (localSite) {
1071
+ push(localSite);
1072
+ } else if (fileImportsBinding(source, refName)) {
1073
+ // Imported binding: map a possible `orig as refName` alias
1074
+ // back to the exported name the provider-wide map is keyed by.
1075
+ const originalName = importedOriginalName(source, refName);
1076
+ const site =
1077
+ passthroughConsts.get(refName) ??
1078
+ passthroughConsts.get(originalName);
1079
+ if (site) {
1080
+ push(site);
1081
+ }
1082
+ }
1083
+ }
1084
+ }
1085
+
1086
+ // `input,` shorthand binds a local `input` const; flag it if that const
1087
+ // is a passthrough schema declared in THIS file (the binding the
1088
+ // shorthand actually closes over).
1089
+ if (/(?:^|\n)[ \t]*input\s*,/.test(source)) {
1090
+ const localInput = passthroughByFile.get(filePath)?.get("input");
1091
+ if (localInput) {
1092
+ push(localInput);
1093
+ } else if (fileImportsBinding(source, "input")) {
1094
+ const site = passthroughConsts.get("input");
1095
+ if (site) {
1096
+ push(site);
1097
+ }
1098
+ }
1099
+ }
1100
+ }
1101
+
1102
+ return escapeHatchResult(providerRoot, "unsafe-input-passthrough", findings, {
1103
+ blockerMessage:
1104
+ "Public input schema uses .passthrough(); unknown caller fields are silently accepted or dropped.",
1105
+ remediation:
1106
+ "Use strict input schemas (z.object({...}) without .passthrough()). If upstream form replay genuinely needs it, allowlist the forwarded fields and add `// @apifuse-allow unsafe-input-passthrough: <reason>`.",
1107
+ passMessage: "Input schemas do not use unscoped .passthrough().",
1108
+ });
1109
+ }
1110
+
1111
+ function scoreUnjustifiedLooseSchema(providerRoot: string): SubmitCheck {
1112
+ const findings: SourceFinding[] = [];
1113
+
1114
+ for (const filePath of listNonTestTypeScriptFiles(providerRoot)) {
1115
+ const lines = readFileSync(filePath, "utf8").split(/\r?\n/);
1116
+ for (let index = 0; index < lines.length; index += 1) {
1117
+ const line = lines[index];
1118
+ if (line === undefined || !/\bz\.(record|unknown|any)\s*\(/.test(line)) {
1119
+ continue;
1120
+ }
1121
+ // A `//` justification comment on the same line or the line above
1122
+ // (including the `@apifuse-allow loose-schema:` form) acknowledges it.
1123
+ const previous = lines[index - 1];
1124
+ const justified =
1125
+ line.includes("//") || previous?.trim().startsWith("//") === true;
1126
+ if (!justified) {
1127
+ findings.push({
1128
+ file: toRelativeProviderPath(providerRoot, filePath),
1129
+ line: index + 1,
1130
+ });
1131
+ }
1132
+ }
1133
+ }
1134
+
1135
+ return escapeHatchResult(providerRoot, "unjustified-loose-schema", findings, {
1136
+ blockerMessage:
1137
+ "Loose schema (z.record/z.unknown/z.any) used without justification.",
1138
+ remediation:
1139
+ "Model the real shape with a typed zod schema. If the upstream payload is genuinely arbitrary, add a `// <reason>` comment or `// @apifuse-allow loose-schema: <reason>` on the line above.",
1140
+ passMessage: "Loose schemas are justified or absent.",
1141
+ });
1142
+ }
1143
+
1144
+ // True when `name` resolves, anywhere in the provider submission, to a
1145
+ // declaration whose initializer is an OPAQUE factory — a call expression
1146
+ // (`const hidden = makeOperations()`) or itself a factory spread — or to an
1147
+ // imported binding with no local declaration (out-of-view construction). Used
1148
+ // to classify a top-level spread identifier (`{ ...hidden }`) so an opaque
1149
+ // factory map cannot be laundered through a variable before being spread. The
1150
+ // stdlib transparent reshape is NOT treated as a factory (parity with the
1151
+ // direct-alias classification).
1152
+ function spreadIdentifierResolvesToFactory(
1153
+ providerRoot: string,
1154
+ indexPath: string,
1155
+ indexSource: string,
1156
+ name: string,
1157
+ ): boolean {
1158
+ const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1159
+ const declRe = new RegExp(
1160
+ `(?:^|\\n)[ \t]*(?:export\\s+)?(?:const|let|var)\\s+${escaped}\\s*(?::[^=\\n]+)?\\s*=`,
1161
+ "g",
1162
+ );
1163
+ let sawDeclaration = false;
1164
+ for (const filePath of [
1165
+ indexPath,
1166
+ ...listNonTestTypeScriptFiles(providerRoot).filter(
1167
+ (p) => resolve(p) !== resolve(indexPath),
1168
+ ),
1169
+ ]) {
1170
+ if (!existsSync(filePath)) {
1171
+ continue;
1172
+ }
1173
+ const fileSource =
1174
+ filePath === indexPath ? indexSource : readFileSync(filePath, "utf8");
1175
+ const re = new RegExp(declRe.source, "g");
1176
+ for (let m = re.exec(fileSource); m !== null; m = re.exec(fileSource)) {
1177
+ sawDeclaration = true;
1178
+ const expr = unwrapParens(
1179
+ balancedValueExpression(fileSource, m.index + m[0].length).trim(),
1180
+ );
1181
+ const isFactory =
1182
+ (/^[A-Za-z_$][\w$.]*\s*\(/.test(expr) ||
1183
+ hasTopLevelFactorySpread(expr)) &&
1184
+ !isTransparentObjectReshape(expr);
1185
+ if (isFactory) {
1186
+ return true;
1187
+ }
1188
+ }
1189
+ }
1190
+ // No local declaration anywhere but imported into index.ts => constructed
1191
+ // out of view; treat as factory (conservative, false-negative-safe).
1192
+ if (!sawDeclaration && fileImportsBinding(indexSource, name)) {
1193
+ return true;
1194
+ }
1195
+ return false;
1196
+ }
1197
+
1198
+ function scoreFlatOperationComposition(providerRoot: string): SubmitCheck {
1199
+ const indexPath = resolve(providerRoot, "index.ts");
1200
+ const ruleId = "flat-operation-composition";
1201
+ if (!existsSync(indexPath)) {
1202
+ return pass(
1203
+ ruleId,
1204
+ SDK_NATIVE_CATEGORY,
1205
+ "Provider index.ts not found; flat-operation check skipped.",
1206
+ 0,
1207
+ );
1208
+ }
1209
+
1210
+ const source = readFileSync(indexPath, "utf8");
1211
+ // Use the same whitespace-tolerant detection as the resolver below, so a
1212
+ // `defineProvider (` / `defineProvider\n(` formatting cannot pass the early
1213
+ // exit before the real classification runs.
1214
+ if (!/\bdefineProvider\s*\(/.test(source)) {
1215
+ return pass(
1216
+ ruleId,
1217
+ SDK_NATIVE_CATEGORY,
1218
+ "No defineProvider call to evaluate for operation composition.",
1219
+ 0,
1220
+ );
1221
+ }
1222
+
1223
+ // Scope the scan to the argument of the EXPORTED `defineProvider(...)` call.
1224
+ // A provider can contain helper/non-exported defineProvider calls before the
1225
+ // real default export (e.g. test scaffolds), so resolve the default export
1226
+ // rather than blindly taking the first regex match. Resolution order:
1227
+ // 1. `export default defineProvider(` — inline default export
1228
+ // 2. `export default <ident>` then `const <ident> = defineProvider(`
1229
+ // 3. fallback: first `defineProvider(` in the file
1230
+ let defineParenIndex = -1;
1231
+ const inlineDefault = /\bexport\s+default\s+defineProvider\s*\(/.exec(source);
1232
+ if (inlineDefault) {
1233
+ defineParenIndex = inlineDefault.index + inlineDefault[0].length - 1; // points at `(`
1234
+ } else {
1235
+ const namedDefault = /\bexport\s+default\s+([A-Za-z_$][\w$]*)\s*;?/.exec(
1236
+ source,
1237
+ );
1238
+ const exportedName = namedDefault?.[1];
1239
+ if (exportedName !== undefined) {
1240
+ const namedDecl = new RegExp(
1241
+ `(?:^|\\n)[ \t]*(?:export\\s+)?(?:const|let|var)\\s+${exportedName}\\s*(?::[^=\\n]+)?\\s*=\\s*defineProvider\\s*\\(`,
1242
+ ).exec(source);
1243
+ if (namedDecl) {
1244
+ defineParenIndex = namedDecl.index + namedDecl[0].length - 1;
1245
+ }
1246
+ }
1247
+ if (defineParenIndex === -1) {
1248
+ const firstCall = /\bdefineProvider\s*\(/.exec(source);
1249
+ if (firstCall) {
1250
+ defineParenIndex = firstCall.index + firstCall[0].length - 1;
1251
+ }
1252
+ }
1253
+ }
1254
+ if (defineParenIndex === -1) {
1255
+ return pass(
1256
+ ruleId,
1257
+ SDK_NATIVE_CATEGORY,
1258
+ "No defineProvider call to evaluate for operation composition.",
1259
+ 0,
1260
+ );
1261
+ }
1262
+ const argStart = defineParenIndex + 1;
1263
+ const argText = balancedValueExpression(source, argStart);
1264
+
1265
+ // Resolve the value passed as `operations:` inside the defineProvider call,
1266
+ // following one alias hop. The value is classified as a static object
1267
+ // literal (pass) or a factory/call expression (block). The regex index is
1268
+ // offset back into the full source so line numbers stay accurate.
1269
+ const opsProp = /\boperations\s*:\s*/.exec(argText);
1270
+ let opsValue: string | undefined;
1271
+ let opsLine = 1;
1272
+ if (opsProp) {
1273
+ const valueStart = argStart + opsProp.index + opsProp[0].length;
1274
+ opsValue = unwrapParens(balancedValueExpression(source, valueStart).trim());
1275
+ opsLine = offsetToLine(source, valueStart);
1276
+ }
1277
+
1278
+ // Property shorthand: `defineProvider({ ..., operations })` — resolve the
1279
+ // local `operations` const initializer.
1280
+ let aliasName: string | undefined;
1281
+ if (opsValue === undefined) {
1282
+ if (/\boperations\s*[,}]/.test(argText)) {
1283
+ aliasName = "operations";
1284
+ }
1285
+ } else if (/^[A-Za-z_$][\w$]*$/.test(opsValue)) {
1286
+ // `operations: ops` — a bare identifier alias to resolve.
1287
+ aliasName = opsValue;
1288
+ }
1289
+
1290
+ // Determine the effective initializer expression to classify. The alias may
1291
+ // be declared in index.ts OR re-exported from a sibling module (the common
1292
+ // generated scaffold: `import { operations } from "./operations"` where
1293
+ // ./operations.ts builds the map with makeOperations()/Object.fromEntries).
1294
+ // Resolve across every provider source file so cross-module factory
1295
+ // composition cannot evade the blocker.
1296
+ let effective = opsValue;
1297
+ let effectiveLine = opsLine;
1298
+ let effectiveFile = "index.ts";
1299
+ if (aliasName !== undefined) {
1300
+ const aliasDecl = new RegExp(
1301
+ `(?:^|\\n)[ \t]*(?:export\\s+)?(?:const|let|var)\\s+${aliasName}\\s*(?::[^=\\n]+)?\\s*=`,
1302
+ );
1303
+ // Destructured factory form: `const { operations } = makeOps()`.
1304
+ const destructured = new RegExp(
1305
+ `(?:^|\\n)[ \t]*(?:export\\s+)?(?:const|let|var)\\s*\\{[^}]*\\b${aliasName}\\b[^}]*\\}\\s*=\\s*([A-Za-z_$][\\w$.]*)\\s*\\(`,
1306
+ );
1307
+
1308
+ // Search index.ts first (its line attribution wins), then siblings.
1309
+ const searchOrder = [
1310
+ indexPath,
1311
+ ...listNonTestTypeScriptFiles(providerRoot).filter(
1312
+ (p) => resolve(p) !== resolve(indexPath),
1313
+ ),
1314
+ ];
1315
+
1316
+ // Collect EVERY same-named declaration across the submission and classify
1317
+ // each as factory vs static. A factory declaration anywhere wins, so a
1318
+ // decoy static `const operations = {}` in an earlier-scanned file cannot
1319
+ // mask a factory-composed declaration in another module. (We deliberately
1320
+ // do not resolve the exact import target path; "any same-named factory
1321
+ // blocks" is the conservative, false-negative-avoiding choice for a gate.)
1322
+ type Candidate = {
1323
+ expr: string;
1324
+ line: number;
1325
+ file: string;
1326
+ isFactory: boolean;
1327
+ };
1328
+ const candidates: Candidate[] = [];
1329
+ for (const filePath of searchOrder) {
1330
+ if (!existsSync(filePath)) {
1331
+ continue;
1332
+ }
1333
+ const fileSource =
1334
+ filePath === indexPath ? source : readFileSync(filePath, "utf8");
1335
+ const relPath = toRelativeProviderPath(providerRoot, filePath);
1336
+
1337
+ const declRe = new RegExp(aliasDecl.source, "g");
1338
+ for (
1339
+ let m = declRe.exec(fileSource);
1340
+ m !== null;
1341
+ m = declRe.exec(fileSource)
1342
+ ) {
1343
+ const valueStart = m.index + m[0].length;
1344
+ const expr = unwrapParens(
1345
+ balancedValueExpression(fileSource, valueStart).trim(),
1346
+ );
1347
+ const isFactory =
1348
+ (/^[A-Za-z_$][\w$.]*\s*\(/.test(expr) ||
1349
+ hasTopLevelFactorySpread(expr)) &&
1350
+ !isTransparentObjectReshape(expr);
1351
+ candidates.push({
1352
+ expr,
1353
+ line: offsetToLine(fileSource, valueStart),
1354
+ file: relPath,
1355
+ isFactory,
1356
+ });
1357
+ }
1358
+ const destructRe = new RegExp(destructured.source, "g");
1359
+ for (
1360
+ let m = destructRe.exec(fileSource);
1361
+ m !== null;
1362
+ m = destructRe.exec(fileSource)
1363
+ ) {
1364
+ candidates.push({
1365
+ expr: `${m[1]}(`,
1366
+ line: offsetToLine(fileSource, m.index),
1367
+ file: relPath,
1368
+ isFactory: true,
1369
+ });
1370
+ }
1371
+ }
1372
+
1373
+ const resolved = candidates.length > 0;
1374
+ if (resolved) {
1375
+ // Prefer a factory declaration (it blocks); otherwise keep the first
1376
+ // static declaration for line attribution.
1377
+ const factory = candidates.find((c) => c.isFactory);
1378
+ const chosen = factory ?? candidates[0];
1379
+ if (chosen !== undefined) {
1380
+ effective = chosen.expr;
1381
+ effectiveLine = chosen.line;
1382
+ effectiveFile = chosen.file;
1383
+ }
1384
+ }
1385
+
1386
+ // An imported alias that resolves to no local declaration anywhere in the
1387
+ // submission means the operations map is constructed out of view. Treat
1388
+ // the unresolved import as a factory-composed (non-static) shape rather
1389
+ // than silently passing.
1390
+ if (!resolved) {
1391
+ const importMatch = new RegExp(
1392
+ `\\bimport\\b[^;]*\\b${aliasName}\\b[^;]*\\bfrom\\b`,
1393
+ ).exec(source);
1394
+ if (importMatch) {
1395
+ effective = `${aliasName}(`;
1396
+ effectiveLine = offsetToLine(source, importMatch.index);
1397
+ effectiveFile = "index.ts";
1398
+ }
1399
+ }
1400
+ }
1401
+
1402
+ // A value starting with `{` is an object literal, but it is only STATIC if
1403
+ // its TOP-LEVEL entries are all explicit properties. A factory spread such
1404
+ // as `{ ...makeOperations() }` still composes the map dynamically. We only
1405
+ // inspect depth-1 entries so that ordinary spreads deep inside operation
1406
+ // handler bodies (e.g. `{ ...headers }`, `...arr.map(...)`) are NOT mistaken
1407
+ // for a top-level factory composition of the operations map itself.
1408
+ const hasFactorySpread =
1409
+ effective !== undefined && hasTopLevelFactorySpread(effective);
1410
+ // A spread of a bare identifier (`{ ...hidden }`) is static ONLY when that
1411
+ // identifier resolves to a non-factory declaration. Resolve each top-level
1412
+ // spread identifier so an opaque factory map laundered through a variable
1413
+ // (`const hidden = makeOperations(); operations: { ...hidden }`) still blocks.
1414
+ const hasFactorySpreadIdentifier =
1415
+ effective !== undefined &&
1416
+ topLevelSpreadIdentifiers(effective).some((name) =>
1417
+ spreadIdentifierResolvesToFactory(providerRoot, indexPath, source, name),
1418
+ );
1419
+ const isStaticLiteral =
1420
+ effective?.startsWith("{") === true &&
1421
+ !hasFactorySpread &&
1422
+ !hasFactorySpreadIdentifier;
1423
+ // A call expression `ident(...)` (factory) or a factory-spread literal is
1424
+ // the rejected, non-static shape — UNLESS it is the stdlib
1425
+ // `Object.fromEntries(Object.entries(<source-visible obj>)...)` reshape,
1426
+ // whose op set is still enumerable from source (verified golden pattern).
1427
+ const isFactoryCall =
1428
+ effective !== undefined &&
1429
+ (/^[A-Za-z_$][\w$.]*\s*\(/.test(effective) ||
1430
+ hasFactorySpread ||
1431
+ hasFactorySpreadIdentifier) &&
1432
+ !isTransparentObjectReshape(effective);
1433
+
1434
+ if (isFactoryCall && !isStaticLiteral) {
1435
+ // Route through the shared escape-hatch partitioner so an
1436
+ // `// @apifuse-allow flat-operation-composition: <reason>` comment on
1437
+ // the reported line (or the line above) downgrades this blocker to a
1438
+ // counted warning, consistent with the other structural rules.
1439
+ return escapeHatchResult(
1440
+ providerRoot,
1441
+ ruleId,
1442
+ [{ file: effectiveFile, line: effectiveLine }],
1443
+ {
1444
+ blockerMessage:
1445
+ "defineProvider operations are composed by a factory call instead of a static object literal.",
1446
+ remediation:
1447
+ "Declare operations as a static literal: defineProvider({ operations: { 'op-id': defineOperation({...}) } }). The provider-registry AST gate requires static runtime/operations; factory composition fails the registry build. If composition is unavoidable, add `// @apifuse-allow flat-operation-composition: <reason>`.",
1448
+ passMessage:
1449
+ "defineProvider declares operations as a static object literal.",
1450
+ },
1451
+ );
1452
+ }
1453
+
1454
+ return pass(
1455
+ ruleId,
1456
+ SDK_NATIVE_CATEGORY,
1457
+ "defineProvider declares operations as a static object literal.",
1458
+ 0,
1459
+ );
1460
+ }
1461
+
1462
+ function scoreCredentialUsage(
1463
+ providerRoot: string,
1464
+ provider: ProviderDefinition,
1465
+ ): SubmitCheck {
1466
+ const credentialReferences = findSourceLineMatches(
1467
+ providerRoot,
1468
+ /ctx\.credential/,
1469
+ );
1470
+ const authMode = provider.auth?.mode ?? "none";
1471
+ const credentialKeys = provider.credential?.keys ?? [];
1472
+ const storesProviderCredential =
1473
+ authMode !== "none" || credentialKeys.length > 0;
1474
+
1475
+ if (storesProviderCredential && credentialReferences.length === 0) {
1476
+ return {
1477
+ id: "credential-usage",
1478
+ category: SDK_NATIVE_CATEGORY,
1479
+ level: "warn",
1480
+ status: "warn",
1481
+ points: 0,
1482
+ maxPoints: 0,
1483
+ message:
1484
+ "Credential-backed provider does not reference credential persistence in source.",
1485
+ remediation:
1486
+ "Persist provider session state through the SDK credential context instead of process-local state. See providers/catchtable for the reference pattern.",
1487
+ };
1488
+ }
1489
+
1490
+ return pass(
1491
+ "credential-usage",
1492
+ SDK_NATIVE_CATEGORY,
1493
+ authMode === "none" && credentialKeys.length === 0
1494
+ ? "Provider does not declare reusable credentials."
1495
+ : "Credential-backed provider references ctx.credential.",
1496
+ 0,
1497
+ credentialReferences.length > 0
1498
+ ? formatSourceFindings(credentialReferences)
1499
+ : undefined,
1500
+ );
1501
+ }
1502
+
1503
+ function findSourceLineMatches(
1504
+ providerRoot: string,
1505
+ pattern: RegExp | ((line: string) => boolean),
1506
+ ): SourceFinding[] {
1507
+ return findSourceFindings(providerRoot, (line) =>
1508
+ matchesLinePattern(line, pattern),
1509
+ );
1510
+ }
1511
+
1512
+ function findSourceFindings(
1513
+ providerRoot: string,
1514
+ matchesLine: (line: string, remainingLines: readonly string[]) => boolean,
1515
+ ): SourceFinding[] {
1516
+ const findings: SourceFinding[] = [];
1517
+ for (const filePath of listNonTestTypeScriptFiles(providerRoot)) {
1518
+ const content = readFileSync(filePath, "utf8");
1519
+ const lines = content.split(/\r?\n/);
1520
+ for (let index = 0; index < lines.length; index += 1) {
1521
+ const line = lines[index];
1522
+ if (line !== undefined && matchesLine(line, lines.slice(index + 1))) {
1523
+ findings.push({
1524
+ file: toRelativeProviderPath(providerRoot, filePath),
1525
+ line: index + 1,
1526
+ });
1527
+ if (findings.length >= MAX_SOURCE_FINDING_EVIDENCE) {
1528
+ return findings;
1529
+ }
1530
+ }
1531
+ }
1532
+ }
1533
+ return findings;
1534
+ }
1535
+
1536
+ function matchesLinePattern(
1537
+ line: string,
1538
+ pattern: RegExp | ((line: string) => boolean),
1539
+ ): boolean {
1540
+ return typeof pattern === "function" ? pattern(line) : pattern.test(line);
1541
+ }
1542
+
1543
+ function listNonTestTypeScriptFiles(providerRoot: string): string[] {
1544
+ const files: string[] = [];
1545
+ collectNonTestTypeScriptFiles(providerRoot, providerRoot, files);
1546
+ return files;
1547
+ }
1548
+
1549
+ function listNonTestProviderSourceFiles(providerRoot: string): string[] {
1550
+ const files: string[] = [];
1551
+ collectNonTestProviderSourceFiles(providerRoot, providerRoot, files);
1552
+ return files;
1553
+ }
1554
+
1555
+ function collectNonTestProviderSourceFiles(
1556
+ providerRoot: string,
1557
+ currentPath: string,
1558
+ files: string[],
1559
+ ): void {
1560
+ for (const entry of readdirSync(currentPath, { withFileTypes: true })) {
1561
+ const entryPath = join(currentPath, entry.name);
1562
+ const relativePath = toRelativeProviderPath(providerRoot, entryPath);
1563
+ if (entry.isDirectory()) {
1564
+ if (shouldScanSourceDirectory(relativePath)) {
1565
+ collectNonTestProviderSourceFiles(providerRoot, entryPath, files);
1566
+ }
1567
+ continue;
1568
+ }
1569
+ if (
1570
+ entry.isFile() &&
1571
+ isScannableProviderSourceFile(relativePath) &&
1572
+ !isExcludedTestSource(relativePath)
1573
+ ) {
1574
+ files.push(entryPath);
1575
+ }
1576
+ }
1577
+ }
1578
+
1579
+ function collectNonTestTypeScriptFiles(
1580
+ providerRoot: string,
1581
+ currentPath: string,
1582
+ files: string[],
1583
+ ): void {
1584
+ for (const entry of readdirSync(currentPath, { withFileTypes: true })) {
1585
+ const entryPath = join(currentPath, entry.name);
1586
+ const relativePath = toRelativeProviderPath(providerRoot, entryPath);
1587
+ if (entry.isDirectory()) {
1588
+ if (shouldScanSourceDirectory(relativePath)) {
1589
+ collectNonTestTypeScriptFiles(providerRoot, entryPath, files);
1590
+ }
1591
+ continue;
1592
+ }
1593
+ if (
1594
+ entry.isFile() &&
1595
+ relativePath.endsWith(".ts") &&
1596
+ !isExcludedTestSource(relativePath)
1597
+ ) {
1598
+ files.push(entryPath);
1599
+ }
1600
+ }
1601
+ }
1602
+
1603
+ function isScannableProviderSourceFile(relativePath: string): boolean {
1604
+ return (
1605
+ /\.(?:ts|tsx|js|jsx|mjs|cjs|sh|bash)$/.test(relativePath) ||
1606
+ /(?:^|\/)Dockerfile(?:\.|$)/.test(relativePath) ||
1607
+ /(?:^|\/)entrypoint(?:\.|$)/.test(relativePath)
1608
+ );
1609
+ }
1610
+
1611
+ function shouldScanSourceDirectory(relativePath: string): boolean {
1612
+ return ![".git", "node_modules", "dist", "build", "coverage"].includes(
1613
+ relativePath,
1614
+ );
1615
+ }
1616
+
1617
+ function isExcludedTestSource(relativePath: string): boolean {
1618
+ return (
1619
+ relativePath.endsWith(".test.ts") ||
1620
+ relativePath.startsWith("__tests__/") ||
1621
+ relativePath.includes("/__tests__/") ||
1622
+ relativePath.startsWith("tests/") ||
1623
+ relativePath.includes("/tests/")
1624
+ );
1625
+ }
1626
+
1627
+ function toRelativeProviderPath(
1628
+ providerRoot: string,
1629
+ filePath: string,
1630
+ ): string {
1631
+ return relative(providerRoot, filePath).replaceAll("\\", "/");
1632
+ }
1633
+
1634
+ function formatSourceFindings(findings: readonly SourceFinding[]): string[] {
1635
+ return findings.map((finding) => `${finding.file}:${finding.line}`);
1636
+ }
1637
+
1638
+ function scoreRepositoryDx(providerRoot: string): SubmitCheck {
1639
+ const missing: string[] = [];
1640
+ if (!existsSync(resolve(providerRoot, ".gitignore"))) {
1641
+ missing.push(".gitignore");
1642
+ }
1643
+
1644
+ const packageJsonPath = resolve(providerRoot, "package.json");
1645
+ const packageScripts = readPackageScripts(packageJsonPath);
1646
+ if (typeof packageScripts?.["type-check"] !== "string") {
1647
+ missing.push("package.json scripts.type-check");
1648
+ }
1649
+ if (!checkScriptRunsTypeCheck(packageScripts?.check)) {
1650
+ missing.push("package.json scripts.check includes type-check");
1651
+ }
1652
+
1653
+ if (missing.length === 0) {
1654
+ return pass(
1655
+ "repository-dx",
1656
+ "docs",
1657
+ "Repository includes generated-provider DX guardrails.",
1658
+ 0,
1659
+ );
1660
+ }
1661
+
1662
+ return {
1663
+ id: "repository-dx",
1664
+ category: "docs",
1665
+ level: "warn",
1666
+ status: "warn",
1667
+ points: 0,
1668
+ maxPoints: 0,
1669
+ message: `Generated repository DX guardrails are missing: ${missing.join(", ")}.`,
1670
+ remediation:
1671
+ "Regenerate with the current `apifuse create` template or add .gitignore plus `type-check: tsc --noEmit` and include it from `check`.",
1672
+ evidence: missing,
1673
+ };
1674
+ }
1675
+
1676
+ function readPackageScripts(
1677
+ packageJsonPath: string,
1678
+ ): Record<string, unknown> | undefined {
1679
+ if (!existsSync(packageJsonPath)) {
1680
+ return undefined;
1681
+ }
1682
+
1683
+ try {
1684
+ const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf8"));
1685
+ if (!isRecord(packageJson) || !isRecord(packageJson.scripts)) {
1686
+ return undefined;
1687
+ }
1688
+ return packageJson.scripts;
1689
+ } catch {
1690
+ return undefined;
1691
+ }
1692
+ }
1693
+
1694
+ function checkScriptRunsTypeCheck(checkScript: unknown): boolean {
1695
+ return (
1696
+ typeof checkScript === "string" &&
1697
+ /(?:^|&&|;)\s*bun\s+run\s+type-check(?:\s|$)/.test(checkScript)
1698
+ );
1699
+ }
1700
+
1701
+ async function safeRunChecks(providerRoot: string): Promise<CheckResult[]> {
1702
+ try {
1703
+ return await runChecks(providerRoot, { lintMode: "standalone" });
1704
+ } catch (error) {
1705
+ return [
1706
+ {
1707
+ message: "Base provider checks can run",
1708
+ passed: false,
1709
+ details: [error instanceof Error ? error.message : String(error)],
1710
+ },
1711
+ ];
1712
+ }
1713
+ }
1714
+
1715
+ const SUBMIT_CHECK_BROWSER_PATTERNS: ReadonlyArray<{
1716
+ rule: string;
1717
+ pattern: RegExp;
1718
+ }> = [
1719
+ {
1720
+ rule: "browser-self-hosted-launch",
1721
+ pattern: /\b(?:playwright|chromium|firefox|webkit|puppeteer)\.launch\s*\(/,
1722
+ },
1723
+ {
1724
+ rule: "browser-self-hosted-child-process",
1725
+ pattern:
1726
+ /\b(?:spawn|spawnSync|exec|execSync|execFile|execFileSync|Bun\.spawn|Bun\.spawnSync)\s*\([^;]*\b(?:google-chrome|chrome|chromium|chromium-browser)\b|\$`[^`]*\b(?:google-chrome|chrome|chromium|chromium-browser)\b/,
1727
+ },
1728
+ {
1729
+ rule: "browser-self-hosted-remote-debugging-port",
1730
+ pattern:
1731
+ /(?:\b(?:google-chrome|chrome|chromium|chromium-browser)\b[\s\S]{0,240}--remote-debugging-port\b|--remote-debugging-port(?:=|\s+))/,
1732
+ },
1733
+ {
1734
+ rule: "browser-direct-cdp-version-poll",
1735
+ pattern: /\/json\/version\b/,
1736
+ },
1737
+ {
1738
+ rule: "browser-provider-local-cdp-env",
1739
+ pattern:
1740
+ /\b(?!APIFUSE__CDP_POOL__URL\b)[A-Z][A-Z0-9_]*_CDP_URL\b|process\.env(?:\.(?!APIFUSE__CDP_POOL__URL\b)[A-Z0-9_]*_CDP_URL\b|\[\s*["'`](?!APIFUSE__CDP_POOL__URL\b)[A-Z0-9_]*_CDP_URL["'`]\s*\])/,
1741
+ },
1742
+ ];
1743
+
1744
+ function scoreManagedBrowserRuntime(providerRoot: string): SubmitCheck {
1745
+ const maxManagedBrowserEvidence = MAX_SOURCE_FINDING_EVIDENCE * 2;
1746
+ const browserFindings: string[] = [];
1747
+ for (const filePath of listNonTestProviderSourceFiles(providerRoot)) {
1748
+ const content = readFileSync(filePath, "utf8");
1749
+ const lines = content.split(/\r?\n/);
1750
+ for (let index = 0; index < lines.length; index += 1) {
1751
+ const line = lines[index];
1752
+ if (line === undefined) continue;
1753
+ for (const { rule, pattern } of SUBMIT_CHECK_BROWSER_PATTERNS) {
1754
+ pattern.lastIndex = 0;
1755
+ if (!pattern.test(line)) continue;
1756
+ browserFindings.push(
1757
+ `${rule} ${toRelativeProviderPath(providerRoot, filePath)}:${index + 1}`,
1758
+ );
1759
+ if (browserFindings.length >= maxManagedBrowserEvidence) break;
1760
+ }
1761
+ if (browserFindings.length >= maxManagedBrowserEvidence) break;
1762
+ }
1763
+ if (browserFindings.length >= maxManagedBrowserEvidence) break;
1764
+ }
1765
+
1766
+ if (browserFindings.length > 0) {
1767
+ return {
1768
+ id: "managed-browser-runtime",
1769
+ category: SDK_NATIVE_CATEGORY,
1770
+ level: "warn",
1771
+ status: "warn",
1772
+ points: 0,
1773
+ maxPoints: 0,
1774
+ message:
1775
+ "Provider source contains self-hosted browser/CDP patterns that APIFuse maintainers must review before promotion.",
1776
+ remediation:
1777
+ "Use ctx.browser backed by the managed CDP Pool. Do not launch Playwright/Puppeteer/Chrome, poll /json/version, or read provider-local *_CDP_URL env vars in provider runtime code.",
1778
+ evidence: browserFindings.map(redact),
1779
+ };
1780
+ }
1781
+
1782
+ return pass(
1783
+ "managed-browser-runtime",
1784
+ SDK_NATIVE_CATEGORY,
1785
+ "Provider source avoids self-hosted browser/CDP runtime patterns.",
1786
+ 0,
1787
+ );
1788
+ }
1789
+
1790
+ function scoreBaseChecks(results: CheckResult[]): SubmitCheck[] {
1791
+ const failed = results.filter((result) => !result.passed);
1792
+ if (failed.length > 0) {
1793
+ return [
1794
+ {
1795
+ id: "base-checks",
1796
+ category: "definition",
1797
+ level: "blocker",
1798
+ status: "fail",
1799
+ points: 0,
1800
+ maxPoints: CATEGORY_MAX_POINTS.definition,
1801
+ message: "Base provider checks failed.",
1802
+ remediation:
1803
+ "Run `bun run check` and fix every failing item before bounty submission.",
1804
+ evidence: failed.map((result) =>
1805
+ redact(`${result.message}: ${(result.details ?? []).join("; ")}`),
1806
+ ),
1807
+ },
1808
+ ];
1809
+ }
1810
+
1811
+ return [
1812
+ {
1813
+ id: "base-checks",
1814
+ category: "definition",
1815
+ level: "info",
1816
+ status: "pass",
1817
+ points: CATEGORY_MAX_POINTS.definition,
1818
+ maxPoints: CATEGORY_MAX_POINTS.definition,
1819
+ message: "Base provider checks passed.",
1820
+ evidence: results.map((result) => result.message),
1821
+ },
1822
+ ];
1823
+ }
1824
+
1825
+ function scoreLocaleCatalog(
1826
+ providerRoot: string,
1827
+ provider: ProviderDefinition,
1828
+ ): SubmitCheck {
1829
+ const requiredKeys = collectProviderRequiredLocaleKeys(provider);
1830
+ if (requiredKeys.length === 0) {
1831
+ return pass(
1832
+ "locale-catalog",
1833
+ "operations",
1834
+ "No key-owned provider metadata or operation metadata requires locale catalog validation.",
1835
+ 0,
1836
+ );
1837
+ }
1838
+
1839
+ try {
1840
+ const availableLocales = REQUIRED_PUBLIC_PROVIDER_LOCALES.filter((locale) =>
1841
+ existsSync(join(providerRoot, "locales", `${locale}.json`)),
1842
+ );
1843
+ const catalogs = loadProviderLocaleCatalogs({
1844
+ providerDir: providerRoot,
1845
+ locales: availableLocales,
1846
+ });
1847
+ const validation = validateProviderLocaleCatalogs({
1848
+ catalogs,
1849
+ requiredLocales: REQUIRED_PUBLIC_PROVIDER_LOCALES,
1850
+ requiredKeys,
1851
+ });
1852
+ if (!validation.ok) {
1853
+ return blocker(
1854
+ "locale-catalog",
1855
+ "operations",
1856
+ "Provider locale catalog is missing required public-provider copy.",
1857
+ "Add provider-local locales/en.json and locales/ko.json values for every provider metadata key, operation descriptionKey, and .describeKey() or describeKey() schema field.",
1858
+ 0,
1859
+ validation.issues.map(
1860
+ (issue) => `${issue.locale}:${issue.key}: ${issue.message}`,
1861
+ ),
1862
+ );
1863
+ }
1864
+ } catch (error) {
1865
+ const message = error instanceof Error ? error.message : String(error);
1866
+ return blocker(
1867
+ "locale-catalog",
1868
+ "operations",
1869
+ "Provider locale catalog is missing required public-provider copy.",
1870
+ "Add provider-local locales/en.json and locales/ko.json values for every provider metadata key, operation descriptionKey, and .describeKey() or describeKey() schema field.",
1871
+ 0,
1872
+ [`*:*: ${message}`],
1873
+ );
1874
+ }
1875
+
1876
+ return pass(
1877
+ "locale-catalog",
1878
+ "operations",
1879
+ "Required provider and operation locale keys resolve in locales/en.json and locales/ko.json.",
1880
+ 0,
1881
+ );
1882
+ }
1883
+
1884
+ function collectProviderRequiredLocaleKeys(
1885
+ provider: ProviderDefinition,
1886
+ ): string[] {
1887
+ const keys = new Set<string>();
1888
+
1889
+ addLocaleKeys(keys, [
1890
+ provider.meta.descriptionKey,
1891
+ provider.meta.docTitleKey,
1892
+ provider.meta.docDescriptionKey,
1893
+ provider.meta.docSummaryKey,
1894
+ provider.meta.docMarkdownKey,
1895
+ ]);
1896
+
1897
+ const publicProfile = provider.meta.publicProfile;
1898
+ if (publicProfile) {
1899
+ addLocaleKeys(keys, [
1900
+ publicProfile.displayNameKey,
1901
+ publicProfile.shortDescriptionKey,
1902
+ publicProfile.longDescriptionKey,
1903
+ publicProfile.setupSummaryKey,
1904
+ ...(publicProfile.capabilityKeys ?? []),
1905
+ ...(publicProfile.examplePromptKeys ?? []),
1906
+ ...(publicProfile.requirementKeys ?? []),
1907
+ ...(publicProfile.limitationKeys ?? []),
1908
+ ]);
1909
+ }
1910
+
1911
+ for (const operation of Object.values(provider.operations)) {
1912
+ addLocaleKeys(keys, [
1913
+ operation.descriptionKey,
1914
+ operation.docs?.titleKey,
1915
+ operation.docs?.descriptionKey,
1916
+ operation.docs?.summaryKey,
1917
+ operation.docs?.markdownKey,
1918
+ ...(operation.whenToUseKeys ?? []),
1919
+ ...(operation.whenNotToUseKeys ?? []),
1920
+ ...collectSchemaDescriptionKeys(operation.input),
1921
+ ...collectSchemaDescriptionKeys(operation.output),
1922
+ ]);
1923
+ }
1924
+
1925
+ return Array.from(keys);
1926
+ }
1927
+
1928
+ function addLocaleKeys(keys: Set<string>, values: readonly unknown[]): void {
1929
+ for (const key of values) {
1930
+ if (typeof key === "string" && key.length > 0) {
1931
+ keys.add(key);
1932
+ }
1933
+ }
1934
+ }
1935
+
1936
+ function collectSchemaDescriptionKeys(schema: unknown): string[] {
1937
+ if (!(schema instanceof z.ZodType)) {
1938
+ return [];
1939
+ }
1940
+ const jsonSchema = z.toJSONSchema(schema);
1941
+ if (!isRecord(jsonSchema)) {
1942
+ return [];
1943
+ }
1944
+ const keys: string[] = [];
1945
+ collectJsonSchemaDescriptionKeys(jsonSchema, keys);
1946
+ return keys;
1947
+ }
1948
+
1949
+ function collectJsonSchemaDescriptionKeys(
1950
+ schema: Record<string, unknown>,
1951
+ keys: string[],
1952
+ ): void {
1953
+ const descriptionKey = schema[APIFUSE_DESCRIPTION_KEY_META_KEY];
1954
+ if (typeof descriptionKey === "string" && descriptionKey.length > 0) {
1955
+ keys.push(descriptionKey);
1956
+ }
1957
+
1958
+ for (const value of Object.values(schema)) {
1959
+ if (isRecord(value)) {
1960
+ collectJsonSchemaDescriptionKeys(value, keys);
1961
+ } else if (Array.isArray(value)) {
1962
+ for (const item of value) {
1963
+ if (isRecord(item)) {
1964
+ collectJsonSchemaDescriptionKeys(item, keys);
1965
+ }
1966
+ }
1967
+ }
1968
+ }
1969
+ }
1970
+
1971
+ function isRecord(value: unknown): value is Record<string, unknown> {
1972
+ return typeof value === "object" && value !== null && !Array.isArray(value);
1973
+ }
1974
+
1975
+ function scoreOperationMetadata(provider: ProviderDefinition): SubmitCheck {
1976
+ const operations = Object.entries(provider.operations);
1977
+ const weakDescriptions = operations
1978
+ .filter(([, operation]) => {
1979
+ // Hard-cut providers move operation copy into locale catalogs via
1980
+ // descriptionKey instead of raw inline prose; the resolved text length
1981
+ // is enforced at registry catalog-build time, matching how lintOperation
1982
+ // skips the raw-description min-length rule when a descriptionKey is set.
1983
+ const hasDescriptionKey =
1984
+ typeof operation.descriptionKey === "string" &&
1985
+ operation.descriptionKey.length > 0;
1986
+ if (hasDescriptionKey) return false;
1987
+ return true;
1988
+ })
1989
+ .map(([operationId]) => operationId);
1990
+ const missingAnnotations = operations
1991
+ .filter(([, operation]) => !operation.annotations)
1992
+ .map(([operationId]) => operationId);
1993
+
1994
+ if (weakDescriptions.length > 0) {
1995
+ return {
1996
+ id: "operation-metadata",
1997
+ category: "operations",
1998
+ level: "blocker",
1999
+ status: "fail",
2000
+ points: 0,
2001
+ maxPoints: CATEGORY_MAX_POINTS.operations,
2002
+ message: "One or more operations have weak descriptions.",
2003
+ remediation:
2004
+ "Add 150+ character English descriptions explaining when to use, when not to use, outputs, and caveats.",
2005
+ evidence: weakDescriptions,
2006
+ };
2007
+ }
2008
+
2009
+ const points =
2010
+ missingAnnotations.length > 0 ? 11 : CATEGORY_MAX_POINTS.operations;
2011
+ return {
2012
+ id: "operation-metadata",
2013
+ category: "operations",
2014
+ level: missingAnnotations.length > 0 ? "warn" : "info",
2015
+ status: missingAnnotations.length > 0 ? "warn" : "pass",
2016
+ points,
2017
+ maxPoints: CATEGORY_MAX_POINTS.operations,
2018
+ message:
2019
+ missingAnnotations.length > 0
2020
+ ? "Operations are described, but some are missing safety annotations."
2021
+ : "Operation descriptions and metadata are review-ready.",
2022
+ remediation:
2023
+ missingAnnotations.length > 0
2024
+ ? "Add annotations such as readOnly, destructive, idempotent, openWorld, rateLimit, or timeoutMs where applicable."
2025
+ : undefined,
2026
+ evidence:
2027
+ missingAnnotations.length > 0
2028
+ ? missingAnnotations.map(
2029
+ (operationId) => `${operationId}: missing annotations`,
2030
+ )
2031
+ : operations.map(([operationId]) => operationId),
2032
+ };
2033
+ }
2034
+
2035
+ function scoreFixtureCoverage(provider: ProviderDefinition): SubmitCheck {
2036
+ const missing = Object.entries(provider.operations)
2037
+ .filter(
2038
+ ([, operation]) =>
2039
+ !operation.fixtures?.request || !operation.fixtures?.response,
2040
+ )
2041
+ .map(([operationId]) => operationId);
2042
+ if (missing.length > 0) {
2043
+ return blocker(
2044
+ "fixtures",
2045
+ "fixtures",
2046
+ "One or more operations are missing bidirectional fixtures.",
2047
+ "Add fixtures.request and fixtures.response that parse against operation schemas.",
2048
+ CATEGORY_MAX_POINTS.fixtures,
2049
+ missing,
2050
+ );
2051
+ }
2052
+ return pass(
2053
+ "fixtures",
2054
+ "fixtures",
2055
+ "All operations include bidirectional fixtures.",
2056
+ CATEGORY_MAX_POINTS.fixtures,
2057
+ );
2058
+ }
2059
+
2060
+ function scoreHealthCoverage(provider: ProviderDefinition): SubmitCheck {
2061
+ const operations = Object.entries(provider.operations);
2062
+ const missing: string[] = [];
2063
+ const placeholder: string[] = [];
2064
+ const unsupported: string[] = [];
2065
+ const generatedStarter: string[] = [];
2066
+
2067
+ for (const [operationId, operation] of operations) {
2068
+ const hasCheck = operation.healthCheck !== undefined;
2069
+ const hasUnsupported = operation.healthCheckUnsupported !== undefined;
2070
+ if (!hasCheck && !hasUnsupported) {
2071
+ missing.push(operationId);
2072
+ continue;
2073
+ }
2074
+ if (hasUnsupported) {
2075
+ const reason = operation.healthCheckUnsupported?.reason ?? "";
2076
+ unsupported.push(operationId);
2077
+ if (/generated local-only scaffold/i.test(reason)) {
2078
+ generatedStarter.push(operationId);
2079
+ }
2080
+ if (
2081
+ /(todo|later|tbd|test fixture|unit test|placeholder|not sure|skip for test)/i.test(
2082
+ reason,
2083
+ )
2084
+ ) {
2085
+ placeholder.push(operationId);
2086
+ }
2087
+ }
2088
+ }
2089
+
2090
+ if (missing.length > 0) {
2091
+ return blocker(
2092
+ "health-coverage",
2093
+ "health",
2094
+ "One or more operations lack healthCheck or healthCheckUnsupported.",
2095
+ "Declare a safe healthCheck for read-only upstream probes or a specific healthCheckUnsupported.reason.",
2096
+ CATEGORY_MAX_POINTS.health,
2097
+ missing,
2098
+ );
2099
+ }
2100
+
2101
+ if (placeholder.length > 0) {
2102
+ return {
2103
+ id: "health-coverage",
2104
+ category: "health",
2105
+ level: "warn",
2106
+ status: "warn",
2107
+ points: 8,
2108
+ maxPoints: CATEGORY_MAX_POINTS.health,
2109
+ message: "Some healthCheckUnsupported reasons look placeholder-like.",
2110
+ remediation:
2111
+ "Replace placeholder rationale with a specific reason such as destructive mutation, paid call, credential sensitivity, or upstream flakiness.",
2112
+ evidence: placeholder,
2113
+ };
2114
+ }
2115
+
2116
+ if (generatedStarter.length > 0) {
2117
+ return {
2118
+ id: "health-coverage",
2119
+ category: "health",
2120
+ level: "warn",
2121
+ status: "warn",
2122
+ points: 10,
2123
+ maxPoints: CATEGORY_MAX_POINTS.health,
2124
+ message:
2125
+ "Generated starter operation health rationale is present; replace starter logic before bounty submission.",
2126
+ remediation:
2127
+ "Replace `ping` with real upstream-backed operations and prefer real healthCheck for safe read-only probes.",
2128
+ evidence: generatedStarter,
2129
+ };
2130
+ }
2131
+
2132
+ if (unsupported.length > 0) {
2133
+ return {
2134
+ id: "health-coverage",
2135
+ category: "health",
2136
+ level: "warn",
2137
+ status: "warn",
2138
+ points: 12,
2139
+ maxPoints: CATEGORY_MAX_POINTS.health,
2140
+ message:
2141
+ "Health coverage is declared, with one or more unsupported probes.",
2142
+ remediation:
2143
+ "Reviewers prefer real healthCheck for safe read-only upstream operations.",
2144
+ evidence: unsupported.map(
2145
+ (operationId) => `${operationId}: healthCheckUnsupported`,
2146
+ ),
2147
+ };
2148
+ }
2149
+
2150
+ return pass(
2151
+ "health-coverage",
2152
+ "health",
2153
+ "All operations declare real health checks.",
2154
+ CATEGORY_MAX_POINTS.health,
2155
+ );
2156
+ }
2157
+
2158
+ function scoreSmokeEvidence(smokeNote: string | undefined): SubmitCheck {
2159
+ if (smokeNote?.trim()) {
2160
+ return {
2161
+ id: "local-smoke",
2162
+ category: "smoke",
2163
+ level: "info",
2164
+ status: "pass",
2165
+ points: CATEGORY_MAX_POINTS.smoke,
2166
+ maxPoints: CATEGORY_MAX_POINTS.smoke,
2167
+ message: "Local smoke evidence was provided.",
2168
+ evidence: [redact(smokeNote.trim())],
2169
+ };
2170
+ }
2171
+
2172
+ return {
2173
+ id: "local-smoke",
2174
+ category: "smoke",
2175
+ level: "warn",
2176
+ status: "warn",
2177
+ points: 5,
2178
+ maxPoints: CATEGORY_MAX_POINTS.smoke,
2179
+ message: "No local smoke evidence was provided.",
2180
+ remediation:
2181
+ "Start `bun run dev`, call `/health` and at least one `POST /v1/{operation}`, then rerun with `--smoke-note` or paste notes in the assigned workspace PR.",
2182
+ };
2183
+ }
2184
+
2185
+ function scoreAuthSafety(provider: ProviderDefinition): SubmitCheck {
2186
+ const authMode = provider.auth?.mode ?? "none";
2187
+ const credentialKeys = provider.credential?.keys ?? [];
2188
+ if (authMode === "credentials" && credentialKeys.length === 0) {
2189
+ return blocker(
2190
+ "auth-safety",
2191
+ "auth",
2192
+ "Credential-backed auth mode is missing credential.keys.",
2193
+ "Declare credential.keys and document local-only connection.secrets debugging.",
2194
+ CATEGORY_MAX_POINTS.auth,
2195
+ );
2196
+ }
2197
+
2198
+ if (authMode === "oauth2" && credentialKeys.length === 0) {
2199
+ return {
2200
+ id: "auth-safety",
2201
+ category: "auth",
2202
+ level: "warn",
2203
+ status: "warn",
2204
+ points: 7,
2205
+ maxPoints: CATEGORY_MAX_POINTS.auth,
2206
+ message: "OAuth auth mode does not declare persisted credential.keys.",
2207
+ remediation:
2208
+ "Generated OAuth starters may begin without keys, but bounty-ready OAuth providers should declare persisted token keys once the real token exchange is implemented.",
2209
+ };
2210
+ }
2211
+
2212
+ if (authMode === "none") {
2213
+ const securedOperations = Object.entries(provider.operations).filter(
2214
+ ([, operation]) => operation.annotations?.openWorld === false,
2215
+ );
2216
+ if (securedOperations.length > 0) {
2217
+ return {
2218
+ id: "auth-safety",
2219
+ category: "auth",
2220
+ level: "warn",
2221
+ status: "warn",
2222
+ points: 7,
2223
+ maxPoints: CATEGORY_MAX_POINTS.auth,
2224
+ message:
2225
+ "Provider is no-auth but at least one operation is not marked openWorld.",
2226
+ remediation:
2227
+ "Confirm auth.mode and operation annotations match the actual upstream auth model.",
2228
+ evidence: securedOperations.map(([operationId]) => operationId),
2229
+ };
2230
+ }
2231
+ }
2232
+
2233
+ return pass(
2234
+ "auth-safety",
2235
+ "auth",
2236
+ "Auth and credential declarations are internally consistent.",
2237
+ CATEGORY_MAX_POINTS.auth,
2238
+ );
2239
+ }
2240
+
2241
+ function scoreProviderDocs(providerRoot: string): SubmitCheck[] {
2242
+ const readmePath = resolve(providerRoot, "README.md");
2243
+ if (!existsSync(readmePath)) {
2244
+ return [
2245
+ {
2246
+ id: "submission-docs",
2247
+ category: "docs",
2248
+ level: "warn",
2249
+ status: "warn",
2250
+ points: 4,
2251
+ maxPoints: CATEGORY_MAX_POINTS.docs,
2252
+ message: "Provider README.md is missing.",
2253
+ remediation:
2254
+ "Add README sections for parameters, response shape, examples, auth/env setup, health coverage, and known upstream constraints.",
2255
+ },
2256
+ ];
2257
+ }
2258
+
2259
+ const readme = readFileSync(readmePath, "utf8").toLowerCase();
2260
+ const missing = [
2261
+ ["parameters", "Parameters"],
2262
+ ["response", "Response"],
2263
+ ["example", "Example"],
2264
+ ].filter(([needle]) => !readme.includes(needle));
2265
+ const mentionsSubmitCheck = readme.includes("submit-check");
2266
+
2267
+ const points = Math.max(
2268
+ 0,
2269
+ CATEGORY_MAX_POINTS.docs -
2270
+ missing.length * 2 -
2271
+ (mentionsSubmitCheck ? 0 : 1),
2272
+ );
2273
+
2274
+ return [
2275
+ {
2276
+ id: "submission-docs",
2277
+ category: "docs",
2278
+ level: missing.length > 0 || !mentionsSubmitCheck ? "warn" : "info",
2279
+ status: missing.length > 0 || !mentionsSubmitCheck ? "warn" : "pass",
2280
+ points,
2281
+ maxPoints: CATEGORY_MAX_POINTS.docs,
2282
+ message:
2283
+ missing.length > 0 || !mentionsSubmitCheck
2284
+ ? "Provider README is present but missing some submission evidence guidance."
2285
+ : "Provider README includes expected submission guidance.",
2286
+ remediation:
2287
+ missing.length > 0 || !mentionsSubmitCheck
2288
+ ? "Include Parameters, Response, Example, and submit-check evidence guidance."
2289
+ : undefined,
2290
+ evidence: [
2291
+ ...missing.map(([, label]) => `missing ${label}`),
2292
+ ...(mentionsSubmitCheck ? [] : ["missing submit-check mention"]),
2293
+ ],
2294
+ },
2295
+ ];
2296
+ }
2297
+
2298
+ function scoreSecrets(providerRoot: string): SubmitCheck {
2299
+ const findings = findSecretFindings(providerRoot);
2300
+ if (findings.length > 0) {
2301
+ return {
2302
+ id: "secret-scan",
2303
+ category: "security",
2304
+ level: "blocker",
2305
+ status: "fail",
2306
+ points: 0,
2307
+ maxPoints: CATEGORY_MAX_POINTS.security,
2308
+ message:
2309
+ "Potential real credential material was found in shareable files.",
2310
+ remediation:
2311
+ "Remove real secrets from source, README, and fixtures. Use environment variables and local-only connection.secrets instead.",
2312
+ evidence: findings.map((finding) => `${finding.file}: ${finding.label}`),
2313
+ };
2314
+ }
2315
+
2316
+ return pass(
2317
+ "secret-scan",
2318
+ "security",
2319
+ "No high-confidence secrets were found in README, source, package, or fixtures.",
2320
+ CATEGORY_MAX_POINTS.security,
2321
+ );
2322
+ }
2323
+
2324
+ function findSecretFindings(providerRoot: string): SecretFinding[] {
2325
+ const candidateFiles = [
2326
+ "README.md",
2327
+ "index.ts",
2328
+ "package.json",
2329
+ "__fixtures__/raw.json",
2330
+ "__fixtures__/transform.snap.json",
2331
+ ];
2332
+ const findings: SecretFinding[] = [];
2333
+
2334
+ for (const relativePath of candidateFiles) {
2335
+ const filePath = resolve(providerRoot, relativePath);
2336
+ if (!existsSync(filePath)) continue;
2337
+ const content = readFileSync(filePath, "utf8");
2338
+ for (const [label, pattern] of SECRET_PATTERNS) {
2339
+ if (pattern.test(content)) {
2340
+ findings.push({ label, file: relativePath });
2341
+ }
2342
+ }
2343
+ }
2344
+
2345
+ return findings;
2346
+ }
2347
+
2348
+ const SECRET_PATTERNS: Array<[string, RegExp]> = [
2349
+ [
2350
+ "JWT-like token",
2351
+ /eyJ[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{20,}\.[A-Za-z0-9_-]{10,}/,
2352
+ ],
2353
+ ["GitHub token", /gh[pousr]_[A-Za-z0-9_]{30,}/],
2354
+ ["Stripe live key", /(?:sk|rk)_live_[A-Za-z0-9]{20,}/],
2355
+ ["Bearer token", /Bearer\s+[A-Za-z0-9._~+/=-]{32,}/i],
2356
+ [
2357
+ "credential field",
2358
+ /"(?:apiKey|api_key|accessToken|access_token|refreshToken|refresh_token|password|secret|sessionCookie|cookie)"\s*:\s*"(?!dev-only|local|example|sample|your-|replace|<)[^"]{16,}"/i,
2359
+ ],
2360
+ ];
2361
+
2362
+ async function safeLoadProvider(
2363
+ providerRoot: string,
2364
+ ): Promise<ProviderDefinition | undefined> {
2365
+ try {
2366
+ return await loadProvider(providerRoot);
2367
+ } catch {
2368
+ return undefined;
2369
+ }
2370
+ }
2371
+
2372
+ async function loadProvider(
2373
+ providerRoot: string,
2374
+ ): Promise<ProviderDefinition | undefined> {
2375
+ const entryPath = resolve(providerRoot, "index.ts");
2376
+ if (!existsSync(entryPath)) {
2377
+ return undefined;
2378
+ }
2379
+ const module = (await import(pathToFileURL(entryPath).href)) as {
2380
+ default?: ProviderDefinition;
2381
+ };
2382
+ return module.default;
2383
+ }
2384
+
2385
+ function resolveProviderRoot(inputPath: string): string {
2386
+ let current = resolve(process.cwd(), inputPath);
2387
+ if (!existsSync(current)) {
2388
+ throw new Error(`Provider path not found: ${inputPath}`);
2389
+ }
2390
+ if (!existsSync(resolve(current, "index.ts"))) {
2391
+ current = dirname(current);
2392
+ }
2393
+ while (!existsSync(resolve(current, "index.ts"))) {
2394
+ const parent = dirname(current);
2395
+ if (parent === current) {
2396
+ throw new Error(`Could not find provider root for: ${inputPath}`);
2397
+ }
2398
+ current = parent;
2399
+ }
2400
+ return current;
2401
+ }
2402
+
2403
+ function pass(
2404
+ id: string,
2405
+ category: string,
2406
+ message: string,
2407
+ points: number,
2408
+ evidence?: string[],
2409
+ ): SubmitCheck {
2410
+ return {
2411
+ id,
2412
+ category,
2413
+ level: "info",
2414
+ status: "pass",
2415
+ points,
2416
+ maxPoints: points,
2417
+ message,
2418
+ ...(evidence ? { evidence } : {}),
2419
+ };
2420
+ }
2421
+
2422
+ function blocker(
2423
+ id: string,
2424
+ category: string,
2425
+ message: string,
2426
+ remediation: string,
2427
+ maxPoints: number,
2428
+ evidence?: string[],
2429
+ ): SubmitCheck {
2430
+ return {
2431
+ id,
2432
+ category,
2433
+ level: "blocker",
2434
+ status: "fail",
2435
+ points: 0,
2436
+ maxPoints,
2437
+ message,
2438
+ remediation,
2439
+ ...(evidence ? { evidence: evidence.map(redact) } : {}),
2440
+ };
2441
+ }
2442
+
2443
+ export function renderText(report: SubmitCheckReport): string {
2444
+ const lines = [
2445
+ `APIFuse Provider Submission Score: ${report.score.total} / ${report.score.max}`,
2446
+ `Verdict: ${report.score.verdict.toUpperCase()}`,
2447
+ `Provider: ${report.provider.id}@${report.provider.version} (${report.provider.runtime}, auth: ${report.provider.authMode})`,
2448
+ `Blockers: ${report.summary.blockers} Warnings: ${report.summary.warnings} Passed: ${report.summary.passed}`,
2449
+ "",
2450
+ "Checklist:",
2451
+ ];
2452
+
2453
+ for (const check of report.checks) {
2454
+ const marker =
2455
+ check.status === "pass" ? "✓" : check.status === "warn" ? "⚠" : "✗";
2456
+ lines.push(
2457
+ `${marker} [${check.category}] ${check.message} (${check.points}/${check.maxPoints})`,
2458
+ );
2459
+ if (check.remediation) {
2460
+ lines.push(` Fix: ${check.remediation}`);
2461
+ }
2462
+ for (const evidence of check.evidence ?? []) {
2463
+ lines.push(` - ${redact(evidence)}`);
2464
+ }
2465
+ }
2466
+
2467
+ return lines.join("\n");
2468
+ }
2469
+
2470
+ export function renderMarkdown(report: SubmitCheckReport): string {
2471
+ const lines = [
2472
+ "# APIFuse Provider Submission Report",
2473
+ "",
2474
+ `- **Provider**: ${report.provider.id}@${report.provider.version}`,
2475
+ `- **SDK**: ${report.provider.sdkVersion}`,
2476
+ `- **Runtime/Auth**: ${report.provider.runtime} / ${report.provider.authMode}`,
2477
+ ...(report.provider.tier
2478
+ ? [`- **Bounty tier**: ${report.provider.tier}`]
2479
+ : []),
2480
+ `- **Score**: ${report.score.total}/${report.score.max}`,
2481
+ `- **Verdict**: ${report.score.verdict}`,
2482
+ `- **Blockers**: ${report.summary.blockers}`,
2483
+ `- **Warnings**: ${report.summary.warnings}`,
2484
+ "",
2485
+ "## Checklist",
2486
+ "",
2487
+ "| Status | Category | Check | Points | Remediation |",
2488
+ "|---|---|---|---:|---|",
2489
+ ];
2490
+
2491
+ for (const check of report.checks) {
2492
+ const status =
2493
+ check.status === "pass"
2494
+ ? "PASS"
2495
+ : check.status === "warn"
2496
+ ? "WARN"
2497
+ : "FAIL";
2498
+ lines.push(
2499
+ `| ${status} | ${escapeMarkdown(check.category)} | ${escapeMarkdown(check.message)} | ${check.points}/${check.maxPoints} | ${escapeMarkdown(check.remediation ?? "")} |`,
2500
+ );
2501
+ }
2502
+
2503
+ const evidence = report.checks.flatMap((check) =>
2504
+ (check.evidence ?? []).map((item) => `- **${check.id}**: ${redact(item)}`),
2505
+ );
2506
+ if (evidence.length > 0) {
2507
+ lines.push("", "## Evidence", "", ...evidence);
2508
+ }
2509
+
2510
+ lines.push("");
2511
+ return `${lines.join("\n")}\n`;
2512
+ }
2513
+
2514
+ function escapeMarkdown(value: string): string {
2515
+ return redact(value).replaceAll("|", "\\|").replaceAll("\n", " ");
2516
+ }
2517
+
2518
+ function redact(value: string): string {
2519
+ let output = value;
2520
+ for (const [, pattern] of SECRET_PATTERNS) {
2521
+ output = output.replace(toGlobalRegex(pattern), "[REDACTED]");
2522
+ }
2523
+ return output;
2524
+ }
2525
+
2526
+ function toGlobalRegex(pattern: RegExp): RegExp {
2527
+ return pattern.global
2528
+ ? pattern
2529
+ : new RegExp(pattern.source, `${pattern.flags}g`);
2530
+ }
2531
+
2532
+ function clamp(value: number, min: number, max: number): number {
2533
+ return Math.min(max, Math.max(min, value));
2534
+ }
2535
+
2536
+ if (import.meta.main) {
2537
+ await main();
2538
+ }