@apifuse/provider-sdk 2.2.0-beta.4 → 2.2.0-beta.40

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 (303) hide show
  1. package/AUTHORING.md +501 -13
  2. package/CHANGELOG.md +165 -1
  3. package/README.md +72 -14
  4. package/SUBMISSION.md +1 -1
  5. package/bin/apifuse-check.ts +88 -4
  6. package/bin/apifuse-dev.ts +38 -5
  7. package/bin/apifuse-pack-check.ts +22 -2
  8. package/bin/apifuse-pack-smoke.ts +57 -2
  9. package/bin/apifuse-pack-types.ts +356 -38
  10. package/bin/apifuse-perf.ts +14 -13
  11. package/bin/apifuse-record.ts +691 -68
  12. package/bin/apifuse-submit-check.ts +518 -37
  13. package/bin/apifuse-sync-assets.ts +117 -0
  14. package/bin/submit-check-delimited-text.ts +50 -0
  15. package/dist/auth-turn/index.d.ts +3 -3
  16. package/dist/auth-turn/index.js +1 -1
  17. package/dist/auth.d.ts +14 -0
  18. package/dist/auth.js +67 -0
  19. package/dist/ceremonies/index.d.ts +16 -0
  20. package/dist/ceremonies/index.js +141 -36
  21. package/dist/cli/commands.d.ts +1 -1
  22. package/dist/cli/commands.js +8 -0
  23. package/dist/cli/create.d.ts +3 -0
  24. package/dist/cli/create.js +34 -35
  25. package/dist/cli/prompt-assets.d.ts +80 -0
  26. package/dist/cli/prompt-assets.js +743 -0
  27. package/dist/cli/templates/provider/AGENTS.md.tpl +17 -8
  28. package/dist/cli/templates/provider/Dockerfile.tpl +1 -1
  29. package/dist/cli/templates/provider/README.md.tpl +4 -4
  30. package/dist/cli/templates/provider/index.ts.tpl +6 -3
  31. package/dist/cli/templates/provider/operations/ping.ts.tpl +2 -1
  32. package/dist/config/loader.d.ts +177 -16
  33. package/dist/config/loader.js +424 -127
  34. package/dist/contract-serialization.js +4 -8
  35. package/dist/contract-types.d.ts +1 -0
  36. package/dist/contract.js +2 -0
  37. package/dist/declaration-validation.d.ts +32 -0
  38. package/dist/declaration-validation.js +207 -0
  39. package/dist/define.d.ts +51 -25
  40. package/dist/define.js +752 -38
  41. package/dist/error-resolution.d.ts +4 -0
  42. package/dist/error-resolution.js +122 -0
  43. package/dist/errors.d.ts +18 -0
  44. package/dist/errors.js +40 -0
  45. package/dist/fixture-sanitization.d.ts +28 -0
  46. package/dist/fixture-sanitization.js +217 -0
  47. package/dist/health-scenario.d.ts +1842 -0
  48. package/dist/health-scenario.js +624 -0
  49. package/dist/index.d.ts +19 -9
  50. package/dist/index.js +10 -6
  51. package/dist/lint.d.ts +6 -1
  52. package/dist/lint.js +362 -3
  53. package/dist/native-address.d.ts +43 -0
  54. package/dist/native-address.js +281 -0
  55. package/dist/native-egress-policy.d.ts +31 -0
  56. package/dist/native-egress-policy.js +288 -0
  57. package/dist/observability.d.ts +5 -2
  58. package/dist/observability.js +48 -1
  59. package/dist/provider.d.ts +8 -2
  60. package/dist/provider.js +3 -1
  61. package/dist/runtime/auth-flow.d.ts +5 -1
  62. package/dist/runtime/auth-flow.js +6 -0
  63. package/dist/runtime/browser.d.ts +1 -0
  64. package/dist/runtime/browser.js +492 -49
  65. package/dist/runtime/cache.d.ts +1 -0
  66. package/dist/runtime/cache.js +169 -15
  67. package/dist/runtime/choice-wordlist.d.ts +9 -0
  68. package/dist/runtime/choice-wordlist.js +138 -0
  69. package/dist/runtime/choice.d.ts +13 -1
  70. package/dist/runtime/choice.js +490 -102
  71. package/dist/runtime/executor.d.ts +2 -2
  72. package/dist/runtime/executor.js +26 -2
  73. package/dist/runtime/http.d.ts +1 -0
  74. package/dist/runtime/http.js +515 -53
  75. package/dist/runtime/instrumentation.d.ts +2 -2
  76. package/dist/runtime/instrumentation.js +366 -8
  77. package/dist/runtime/native-network-errors.d.ts +33 -0
  78. package/dist/runtime/native-network-errors.js +69 -0
  79. package/dist/runtime/native-network.d.ts +96 -0
  80. package/dist/runtime/native-network.js +1232 -0
  81. package/dist/runtime/ocr.d.ts +29 -0
  82. package/dist/runtime/ocr.js +440 -0
  83. package/dist/runtime/proxy-errors.js +6 -2
  84. package/dist/runtime/proxy-nodemaven.d.ts +56 -0
  85. package/dist/runtime/proxy-nodemaven.js +146 -0
  86. package/dist/runtime/proxy-telemetry.d.ts +80 -1
  87. package/dist/runtime/proxy-telemetry.js +154 -47
  88. package/dist/runtime/redirects.d.ts +29 -0
  89. package/dist/runtime/redirects.js +36 -0
  90. package/dist/runtime/redis.d.ts +1 -1
  91. package/dist/runtime/redis.js +4 -2
  92. package/dist/runtime/request-options.d.ts +68 -1
  93. package/dist/runtime/request-options.js +548 -0
  94. package/dist/runtime/resolver-config.d.ts +6 -0
  95. package/dist/runtime/resolver-config.js +6 -0
  96. package/dist/runtime/resolver-public.d.ts +1 -0
  97. package/dist/runtime/resolver-public.js +1 -0
  98. package/dist/runtime/resolver-shared.d.ts +3 -0
  99. package/dist/runtime/resolver-shared.js +12 -0
  100. package/dist/runtime/resolver-vendors/bindings.d.ts +48 -0
  101. package/dist/runtime/resolver-vendors/bindings.js +40 -0
  102. package/dist/runtime/resolver-vendors/browser.d.ts +22 -0
  103. package/dist/runtime/resolver-vendors/browser.js +377 -0
  104. package/dist/runtime/resolver-vendors/capsolver.d.ts +22 -0
  105. package/dist/runtime/resolver-vendors/capsolver.js +526 -0
  106. package/dist/runtime/resolver-vendors/hosts.d.ts +2 -0
  107. package/dist/runtime/resolver-vendors/hosts.js +33 -0
  108. package/dist/runtime/resolver-vendors/twocaptcha.d.ts +24 -0
  109. package/dist/runtime/resolver-vendors/twocaptcha.js +407 -0
  110. package/dist/runtime/resolver-vendors/types.d.ts +94 -0
  111. package/dist/runtime/resolver-vendors/types.js +96 -0
  112. package/dist/runtime/resolver.d.ts +60 -0
  113. package/dist/runtime/resolver.js +737 -0
  114. package/dist/runtime/secrets.d.ts +27 -0
  115. package/dist/runtime/secrets.js +51 -0
  116. package/dist/runtime/state.d.ts +3 -0
  117. package/dist/runtime/state.js +277 -71
  118. package/dist/runtime/stealth-cookies.d.ts +20 -0
  119. package/dist/runtime/stealth-cookies.js +111 -0
  120. package/dist/runtime/stealth.d.ts +28 -3
  121. package/dist/runtime/stealth.js +519 -255
  122. package/dist/runtime/stt.js +1 -12
  123. package/dist/runtime/timeout.d.ts +5 -0
  124. package/dist/runtime/timeout.js +12 -0
  125. package/dist/runtime/trace-config.d.ts +12 -0
  126. package/dist/runtime/trace-config.js +61 -0
  127. package/dist/serve.d.ts +1 -1
  128. package/dist/serve.js +1 -1
  129. package/dist/server/index.d.ts +5 -3
  130. package/dist/server/index.js +3 -3
  131. package/dist/server/self-test-input-tokens.d.ts +2 -1
  132. package/dist/server/self-test-input-tokens.js +18 -14
  133. package/dist/server/self-test.d.ts +114 -0
  134. package/dist/server/self-test.js +784 -148
  135. package/dist/server/serve-implementation.d.ts +213 -0
  136. package/dist/server/serve-implementation.js +2173 -0
  137. package/dist/server/serve.d.ts +1 -70
  138. package/dist/server/serve.js +1 -1130
  139. package/dist/server/trace-output.d.ts +4 -0
  140. package/dist/server/trace-output.js +20 -0
  141. package/dist/server/types.d.ts +30 -5
  142. package/dist/server/types.js +13 -1
  143. package/dist/stateful/errors.d.ts +19 -0
  144. package/dist/stateful/errors.js +24 -0
  145. package/dist/stateful/http-provider-event-emitter.d.ts +40 -0
  146. package/dist/stateful/http-provider-event-emitter.js +237 -0
  147. package/dist/stateful/http-session-owner-registry.d.ts +44 -0
  148. package/dist/stateful/http-session-owner-registry.js +210 -0
  149. package/dist/stateful/index.d.ts +18 -0
  150. package/dist/stateful/index.js +18 -0
  151. package/dist/stateful/provider-event-delivery-failures.d.ts +32 -0
  152. package/dist/stateful/provider-event-delivery-failures.js +43 -0
  153. package/dist/stateful/provider-event-pipeline-metrics.d.ts +46 -0
  154. package/dist/stateful/provider-event-pipeline-metrics.js +48 -0
  155. package/dist/stateful/provider-event-pipeline.d.ts +50 -0
  156. package/dist/stateful/provider-event-pipeline.js +1 -0
  157. package/dist/stateful/provider-events.d.ts +101 -0
  158. package/dist/stateful/provider-events.js +289 -0
  159. package/dist/stateful/session-key.d.ts +15 -0
  160. package/dist/stateful/session-key.js +86 -0
  161. package/dist/stateful/stateful-provider-adapter-context.d.ts +5 -0
  162. package/dist/stateful/stateful-provider-adapter-context.js +42 -0
  163. package/dist/stateful/stateful-provider-adapter-metrics.d.ts +15 -0
  164. package/dist/stateful/stateful-provider-adapter-metrics.js +21 -0
  165. package/dist/stateful/stateful-provider-adapter.d.ts +98 -0
  166. package/dist/stateful/stateful-provider-adapter.js +287 -0
  167. package/dist/stateful/stateful-provider-observability.d.ts +62 -0
  168. package/dist/stateful/stateful-provider-observability.js +161 -0
  169. package/dist/stateful/stateful-provider-owner-forwarder.d.ts +41 -0
  170. package/dist/stateful/stateful-provider-owner-forwarder.js +215 -0
  171. package/dist/stateful/stateful-provider-runtime-context.d.ts +32 -0
  172. package/dist/stateful/stateful-provider-runtime-context.js +60 -0
  173. package/dist/stateful/stateful-provider-runtime-executor.d.ts +34 -0
  174. package/dist/stateful/stateful-provider-runtime-executor.js +52 -0
  175. package/dist/stateful/stateful-provider-session-routing.d.ts +67 -0
  176. package/dist/stateful/stateful-provider-session-routing.js +345 -0
  177. package/dist/stateful/stateful-provider-session-runtime.d.ts +98 -0
  178. package/dist/stateful/stateful-provider-session-runtime.js +245 -0
  179. package/dist/stateful-signing.d.ts +18 -0
  180. package/dist/stateful-signing.js +27 -0
  181. package/dist/stealth/profiles.js +16 -7
  182. package/dist/stream-evidence.d.ts +74 -0
  183. package/dist/stream-evidence.js +785 -0
  184. package/dist/stream.js +7 -1
  185. package/dist/testing/index.d.ts +2 -1
  186. package/dist/testing/index.js +2 -1
  187. package/dist/testing/run.d.ts +32 -2
  188. package/dist/testing/run.js +489 -21
  189. package/dist/trace-sanitization.d.ts +5 -0
  190. package/dist/trace-sanitization.js +45 -0
  191. package/dist/types.d.ts +545 -23
  192. package/dist/types.js +1 -0
  193. package/package.json +44 -5
  194. package/src/auth-turn/index.ts +1 -1
  195. package/src/auth.ts +118 -0
  196. package/src/ceremonies/index.ts +189 -46
  197. package/src/cli/commands.ts +10 -0
  198. package/src/cli/create.ts +42 -35
  199. package/src/cli/prompt-assets.ts +865 -0
  200. package/src/cli/templates/provider/AGENTS.md.tpl +17 -8
  201. package/src/cli/templates/provider/Dockerfile.tpl +1 -1
  202. package/src/cli/templates/provider/README.md.tpl +4 -4
  203. package/src/cli/templates/provider/index.ts.tpl +6 -3
  204. package/src/cli/templates/provider/operations/ping.ts.tpl +2 -1
  205. package/src/config/loader.ts +665 -163
  206. package/src/contract-serialization.ts +5 -7
  207. package/src/contract-types.ts +1 -0
  208. package/src/contract.ts +2 -0
  209. package/src/declaration-validation.ts +266 -0
  210. package/src/define.ts +970 -87
  211. package/src/error-resolution.ts +127 -0
  212. package/src/errors.ts +52 -0
  213. package/src/fixture-sanitization.ts +248 -0
  214. package/src/health-scenario.ts +875 -0
  215. package/src/index.ts +204 -8
  216. package/src/lint.ts +408 -4
  217. package/src/native-address.ts +340 -0
  218. package/src/native-egress-policy.ts +358 -0
  219. package/src/observability.ts +51 -1
  220. package/src/provider.ts +133 -0
  221. package/src/runtime/auth-flow.ts +12 -0
  222. package/src/runtime/browser.ts +661 -63
  223. package/src/runtime/cache.ts +189 -14
  224. package/src/runtime/choice-wordlist.ts +145 -0
  225. package/src/runtime/choice.ts +631 -120
  226. package/src/runtime/executor.ts +40 -7
  227. package/src/runtime/http.ts +641 -61
  228. package/src/runtime/instrumentation.ts +520 -15
  229. package/src/runtime/native-network-errors.ts +99 -0
  230. package/src/runtime/native-network.ts +1605 -0
  231. package/src/runtime/ocr.ts +523 -0
  232. package/src/runtime/proxy-errors.ts +12 -4
  233. package/src/runtime/proxy-nodemaven.ts +221 -0
  234. package/src/runtime/proxy-telemetry.ts +244 -75
  235. package/src/runtime/redirects.ts +66 -0
  236. package/src/runtime/redis.ts +7 -2
  237. package/src/runtime/request-options.ts +680 -1
  238. package/src/runtime/resolver-config.ts +6 -0
  239. package/src/runtime/resolver-public.ts +20 -0
  240. package/src/runtime/resolver-shared.ts +17 -0
  241. package/src/runtime/resolver-vendors/bindings.ts +56 -0
  242. package/src/runtime/resolver-vendors/browser.ts +533 -0
  243. package/src/runtime/resolver-vendors/capsolver.ts +700 -0
  244. package/src/runtime/resolver-vendors/hosts.ts +38 -0
  245. package/src/runtime/resolver-vendors/twocaptcha.ts +539 -0
  246. package/src/runtime/resolver-vendors/types.ts +212 -0
  247. package/src/runtime/resolver.ts +1103 -0
  248. package/src/runtime/secrets.ts +64 -0
  249. package/src/runtime/state.ts +394 -77
  250. package/src/runtime/stealth-cookies.ts +132 -0
  251. package/src/runtime/stealth.ts +675 -289
  252. package/src/runtime/stt.ts +1 -19
  253. package/src/runtime/timeout.ts +18 -0
  254. package/src/runtime/trace-config.ts +77 -0
  255. package/src/serve.ts +6 -1
  256. package/src/server/index.ts +37 -2
  257. package/src/server/self-test-input-tokens.ts +29 -14
  258. package/src/server/self-test.ts +1025 -175
  259. package/src/server/serve-implementation.ts +3250 -0
  260. package/src/server/serve.ts +1 -1626
  261. package/src/server/trace-output.ts +32 -0
  262. package/src/server/types.ts +13 -1
  263. package/src/stateful/README.md +146 -0
  264. package/src/stateful/errors.ts +35 -0
  265. package/src/stateful/http-provider-event-emitter.ts +314 -0
  266. package/src/stateful/http-session-owner-registry.ts +306 -0
  267. package/src/stateful/index.ts +18 -0
  268. package/src/stateful/provider-event-delivery-failures.ts +80 -0
  269. package/src/stateful/provider-event-pipeline-metrics.ts +95 -0
  270. package/src/stateful/provider-event-pipeline.ts +61 -0
  271. package/src/stateful/provider-events.ts +462 -0
  272. package/src/stateful/session-key.ts +111 -0
  273. package/src/stateful/stateful-provider-adapter-context.ts +59 -0
  274. package/src/stateful/stateful-provider-adapter-metrics.ts +48 -0
  275. package/src/stateful/stateful-provider-adapter.ts +562 -0
  276. package/src/stateful/stateful-provider-observability.ts +261 -0
  277. package/src/stateful/stateful-provider-owner-forwarder.ts +287 -0
  278. package/src/stateful/stateful-provider-runtime-context.ts +92 -0
  279. package/src/stateful/stateful-provider-runtime-executor.ts +96 -0
  280. package/src/stateful/stateful-provider-session-routing.ts +546 -0
  281. package/src/stateful/stateful-provider-session-runtime.ts +403 -0
  282. package/src/stateful-signing.ts +46 -0
  283. package/src/stealth/profiles.ts +17 -7
  284. package/src/stream-evidence.ts +988 -0
  285. package/src/stream.ts +8 -1
  286. package/src/testing/index.ts +10 -1
  287. package/src/testing/run.ts +658 -15
  288. package/src/trace-sanitization.ts +63 -0
  289. package/src/types.ts +660 -38
  290. package/dist/cli/templates/provider/CLAUDE.md.tpl +0 -1
  291. package/src/cli/templates/provider/CLAUDE.md.tpl +0 -1
  292. /package/dist/cli/templates/provider/{skills → .agents/skills}/fixtures-and-recording/SKILL.md.tpl +0 -0
  293. /package/dist/cli/templates/provider/{skills → .agents/skills}/health-checks-and-fail-closed/SKILL.md.tpl +0 -0
  294. /package/dist/cli/templates/provider/{skills → .agents/skills}/normalization-standards/SKILL.md.tpl +0 -0
  295. /package/dist/cli/templates/provider/{skills → .agents/skills}/pagination-and-counts/SKILL.md.tpl +0 -0
  296. /package/dist/cli/templates/provider/{skills → .agents/skills}/upstream-contract-verification/SKILL.md.tpl +0 -0
  297. /package/dist/cli/templates/provider/{skills → .agents/skills}/upstream-notes/README.md.tpl +0 -0
  298. /package/src/cli/templates/provider/{skills → .agents/skills}/fixtures-and-recording/SKILL.md.tpl +0 -0
  299. /package/src/cli/templates/provider/{skills → .agents/skills}/health-checks-and-fail-closed/SKILL.md.tpl +0 -0
  300. /package/src/cli/templates/provider/{skills → .agents/skills}/normalization-standards/SKILL.md.tpl +0 -0
  301. /package/src/cli/templates/provider/{skills → .agents/skills}/pagination-and-counts/SKILL.md.tpl +0 -0
  302. /package/src/cli/templates/provider/{skills → .agents/skills}/upstream-contract-verification/SKILL.md.tpl +0 -0
  303. /package/src/cli/templates/provider/{skills → .agents/skills}/upstream-notes/README.md.tpl +0 -0
package/src/lint.ts CHANGED
@@ -1,9 +1,19 @@
1
1
  import type { ZodType } from "zod";
2
2
 
3
+ import {
4
+ SDK_RUNTIME_OWNED_ERROR_CODES,
5
+ SDK_STATUS_MAPPED_PROVIDER_ERROR_CODES,
6
+ } from "./error-resolution.js";
3
7
  import { lintPublicSchemaFieldNames } from "./public-schema-field-lint.js";
4
8
  import { APIFUSE_DESCRIPTION_KEY_META_KEY, APIFUSE_SENSITIVE_META_KEY } from "./schema.js";
5
9
 
6
- type AuthModeLike = "none" | "platform-managed" | "credentials" | "oauth2" | "api-key";
10
+ type AuthModeLike =
11
+ | "none"
12
+ | "platform-managed"
13
+ | "credentials"
14
+ | "oauth2"
15
+ | "oauth2_proxied"
16
+ | "api-key";
7
17
 
8
18
  type ProviderAuthLike = {
9
19
  mode?: AuthModeLike;
@@ -17,9 +27,97 @@ type ProviderAuthLike = {
17
27
  exchange?: unknown;
18
28
  };
19
29
 
30
+ // Operations that perform an auth-lifecycle action belong on the single
31
+ // `auth.flow` interface, never on a provider operation:
32
+ // - entry (login / signin / authenticate) => auth.flow.start/continue
33
+ // - exit (logout / signout / disconnect) => auth.flow.abort
34
+ //
35
+ // Matching works on `-`/`_` separated segments rather than a raw substring or a
36
+ // leading anchor, so `shop-logout`, `shop_logout` and `user-sign-out-everywhere`
37
+ // are all recognised: a domain prefix does not make the operation any less of an
38
+ // auth-lifecycle action, and operation ids may use either separator.
39
+ //
40
+ // Vocabulary is split into two tiers because auth words collide with ordinary
41
+ // domain verbs. Measured against the live fleet plus synthetic domain ids:
42
+ // - `authorize-payment`, `revoke-invitation`, `unlink-record`,
43
+ // `disconnect-device` are domain actions that never touch the connection
44
+ // credential, so these verbs are NOT matched as segments;
45
+ // - the same verbs as a complete operation id (`authorize`, `revoke`) do
46
+ // refer to the credential itself, so they are matched only in that form.
47
+ // `exchange`, `callback`, `connect`, `session`, `token`, `credential`,
48
+ // `password` and `otp` stay out entirely for the same reason.
49
+ const AUTH_LIFECYCLE_SEGMENT_WORDS = new Set([
50
+ "login",
51
+ "logout",
52
+ "signin",
53
+ "signout",
54
+ "signup",
55
+ "authenticate",
56
+ "reauth",
57
+ "auth",
58
+ ]);
59
+
60
+ // Ambiguous as a prefix, unambiguous when they are the whole operation id.
61
+ const AUTH_LIFECYCLE_WHOLE_ID_WORDS = new Set([
62
+ "authorize",
63
+ "revoke",
64
+ "unlink",
65
+ "disconnect",
66
+ ]);
67
+
68
+ // A verb stem followed by a direction word across two segments: `sign-out`,
69
+ // `user_sign_up_flow`, and the spelled-out `log-in` / `shop-log-out` forms
70
+ // (their fused equivalents `login`/`logout` live in the segment set above).
71
+ // `sign` pairs match anywhere; `log` pairs match only at the END of the id,
72
+ // because mid-id `log` is the noun in domain phrases measured against real
73
+ // fleets (`audit-log-in-range`, `change-log-out-of-band` are reads of a log,
74
+ // while `shop-log-out` is a logout).
75
+ const AUTH_DIRECTION_PAIRS: ReadonlyMap<
76
+ string,
77
+ { directions: ReadonlySet<string>; endOnly: boolean }
78
+ > = new Map([
79
+ ["sign", { directions: new Set(["in", "out", "up"]), endOnly: false }],
80
+ ["log", { directions: new Set(["in", "out"]), endOnly: true }],
81
+ ]);
82
+
83
+ // Legacy anchored form kept for token-plumbing words whose bare use is only
84
+ // auth-related when it leads the operation id (`exchange-code`, `refresh`).
20
85
  const AUTH_OPERATION_ID_PATTERN =
21
86
  /^(?:auth[-_])?(?:login|exchange|continue|refresh|callback)(?:[-_]|$)/i;
22
87
 
88
+ function isAuthLifecycleOperationId(operationId: string, authMode: string): boolean {
89
+ const segments = operationId.toLowerCase().split(/[-_]+/).filter(Boolean);
90
+
91
+ if (segments.some((segment) => AUTH_LIFECYCLE_SEGMENT_WORDS.has(segment))) return true;
92
+
93
+ // A verb stem + direction spread across two segments (`sign-out`,
94
+ // `sign_up`, `shop-log-out`); see AUTH_DIRECTION_PAIRS for positioning.
95
+ if (
96
+ segments.some((segment, index) => {
97
+ const pair = AUTH_DIRECTION_PAIRS.get(segment);
98
+ if (pair === undefined || index + 1 >= segments.length) return false;
99
+ if (!pair.directions.has(segments[index + 1] as string)) return false;
100
+ return pair.endOnly ? index + 2 === segments.length : true;
101
+ })
102
+ ) {
103
+ return true;
104
+ }
105
+
106
+ if (segments.length === 1 && AUTH_LIFECYCLE_WHOLE_ID_WORDS.has(segments[0] as string)) {
107
+ return true;
108
+ }
109
+
110
+ // The legacy anchored pattern keeps its original scope. It matches ordinary
111
+ // domain ids such as `exchange-rates` and `refresh-catalog`, so extending it
112
+ // to `oauth2_proxied` would spread that behavior to providers it never
113
+ // applied to; proxied providers are covered by the segment tiers above.
114
+ if (authMode === "credentials" || authMode === "oauth2") {
115
+ return AUTH_OPERATION_ID_PATTERN.test(operationId);
116
+ }
117
+
118
+ return false;
119
+ }
120
+
23
121
  type ProviderContractMetaLike = {
24
122
  publicSchemaFieldNames?: "normalized";
25
123
  };
@@ -793,6 +891,303 @@ function lintSelfHostedBrowserPatterns(
793
891
  return diagnostics;
794
892
  }
795
893
 
894
+ const THROWN_ERROR_CONSTRUCTION_PATTERN = /new\s+(?:ProviderError|ValidationError)\s*\(/g;
895
+
896
+ const TEST_SOURCE_FILE_PATTERN = /(?:^|\/)(?:__tests__|__mocks__)\/|\.(?:test|spec)\.[cm]?[jt]sx?$/;
897
+
898
+ /**
899
+ * Skips a string literal starting at `startIndex` (which must point at the
900
+ * opening quote). Returns the index of the closing quote, or -1 when the
901
+ * literal is unterminated. Template literals handle nested `${...}`
902
+ * expressions, including strings inside them.
903
+ */
904
+ function skipStringLiteral(source: string, startIndex: number): number {
905
+ const quote = source[startIndex];
906
+ for (let index = startIndex + 1; index < source.length; index++) {
907
+ const char = source[index];
908
+ if (char === "\\") {
909
+ index++;
910
+ continue;
911
+ }
912
+ if (quote === "`" && char === "$" && source[index + 1] === "{") {
913
+ index = skipTemplateExpression(source, index + 2);
914
+ if (index < 0) {
915
+ return -1;
916
+ }
917
+ continue;
918
+ }
919
+ if (char === quote) {
920
+ return index;
921
+ }
922
+ if (quote !== "`" && char === "\n") {
923
+ return -1;
924
+ }
925
+ }
926
+ return -1;
927
+ }
928
+
929
+ function skipTemplateExpression(source: string, startIndex: number): number {
930
+ let depth = 1;
931
+ for (let index = startIndex; index < source.length; index++) {
932
+ const char = source[index];
933
+ if (char === '"' || char === "'" || char === "`") {
934
+ index = skipStringLiteral(source, index);
935
+ if (index < 0) {
936
+ return -1;
937
+ }
938
+ continue;
939
+ }
940
+ if (char === "{") {
941
+ depth++;
942
+ } else if (char === "}") {
943
+ depth--;
944
+ if (depth === 0) {
945
+ return index;
946
+ }
947
+ }
948
+ }
949
+ return -1;
950
+ }
951
+
952
+ /**
953
+ * Extracts the argument text of a call whose opening paren has already been
954
+ * consumed (`startIndex` points just past it). Returns undefined when the
955
+ * call never closes in this source, which the caller treats as "skip
956
+ * silently" — this scanner is conservative by design.
957
+ */
958
+ function extractBalancedCallArguments(source: string, startIndex: number): string | undefined {
959
+ let depth = 1;
960
+ for (let index = startIndex; index < source.length; index++) {
961
+ const char = source[index];
962
+ if (char === '"' || char === "'" || char === "`") {
963
+ index = skipStringLiteral(source, index);
964
+ if (index < 0) {
965
+ return undefined;
966
+ }
967
+ continue;
968
+ }
969
+ if (char === "/" && source[index + 1] === "/") {
970
+ const newline = source.indexOf("\n", index);
971
+ if (newline === -1) {
972
+ return undefined;
973
+ }
974
+ index = newline;
975
+ continue;
976
+ }
977
+ if (char === "/" && source[index + 1] === "*") {
978
+ const end = source.indexOf("*/", index + 2);
979
+ if (end === -1) {
980
+ return undefined;
981
+ }
982
+ index = end + 1;
983
+ continue;
984
+ }
985
+ if (char === "(") {
986
+ depth++;
987
+ } else if (char === ")") {
988
+ depth--;
989
+ if (depth === 0) {
990
+ return source.slice(startIndex, index);
991
+ }
992
+ }
993
+ }
994
+ return undefined;
995
+ }
996
+
997
+ /**
998
+ * Collects literal string values of top-level `code:` properties inside a
999
+ * ProviderError/ValidationError options object. Only plain `"..."` / `'...'`
1000
+ * literals at options-object depth count; computed codes (identifiers,
1001
+ * ternaries, template substitutions, concatenations, escapes) are skipped
1002
+ * silently so the rule never guesses.
1003
+ */
1004
+ function collectLiteralErrorCodeValues(args: string): string[] {
1005
+ const codes: string[] = [];
1006
+ let braceDepth = 0;
1007
+ let parenDepth = 0;
1008
+ let bracketDepth = 0;
1009
+ let previousSignificantChar = "";
1010
+ for (let index = 0; index < args.length; index++) {
1011
+ const char = args[index] ?? "";
1012
+ if (char === '"' || char === "'" || char === "`") {
1013
+ const end = skipStringLiteral(args, index);
1014
+ if (end < 0) {
1015
+ return codes;
1016
+ }
1017
+ index = end;
1018
+ previousSignificantChar = char;
1019
+ continue;
1020
+ }
1021
+ if (char === "/" && args[index + 1] === "/") {
1022
+ const newline = args.indexOf("\n", index);
1023
+ if (newline === -1) {
1024
+ return codes;
1025
+ }
1026
+ index = newline;
1027
+ continue;
1028
+ }
1029
+ if (char === "/" && args[index + 1] === "*") {
1030
+ const end = args.indexOf("*/", index + 2);
1031
+ if (end === -1) {
1032
+ return codes;
1033
+ }
1034
+ index = end + 1;
1035
+ continue;
1036
+ }
1037
+ if (/\s/.test(char)) {
1038
+ continue;
1039
+ }
1040
+ if (char === "{") {
1041
+ braceDepth++;
1042
+ } else if (char === "}") {
1043
+ braceDepth--;
1044
+ } else if (char === "(") {
1045
+ parenDepth++;
1046
+ } else if (char === ")") {
1047
+ parenDepth--;
1048
+ } else if (char === "[") {
1049
+ bracketDepth++;
1050
+ } else if (char === "]") {
1051
+ bracketDepth--;
1052
+ } else if (
1053
+ braceDepth === 1 &&
1054
+ parenDepth === 0 &&
1055
+ bracketDepth === 0 &&
1056
+ (previousSignificantChar === "{" || previousSignificantChar === ",") &&
1057
+ args.startsWith("code", index)
1058
+ ) {
1059
+ let cursor = index + "code".length;
1060
+ while (cursor < args.length && /\s/.test(args[cursor] ?? "")) {
1061
+ cursor++;
1062
+ }
1063
+ if (args[cursor] === ":") {
1064
+ cursor++;
1065
+ while (cursor < args.length && /\s/.test(args[cursor] ?? "")) {
1066
+ cursor++;
1067
+ }
1068
+ const quote = args[cursor];
1069
+ if (quote === '"' || quote === "'") {
1070
+ const end = skipStringLiteral(args, cursor);
1071
+ if (end > cursor) {
1072
+ const value = args.slice(cursor + 1, end);
1073
+ let after = end + 1;
1074
+ while (after < args.length && /\s/.test(args[after] ?? "")) {
1075
+ after++;
1076
+ }
1077
+ const nextChar = after < args.length ? (args[after] ?? "") : "";
1078
+ if (!value.includes("\\") && (nextChar === "," || nextChar === "}" || nextChar === "")) {
1079
+ codes.push(value);
1080
+ }
1081
+ index = end;
1082
+ previousSignificantChar = quote;
1083
+ continue;
1084
+ }
1085
+ return codes;
1086
+ }
1087
+ }
1088
+ }
1089
+ previousSignificantChar = char;
1090
+ }
1091
+ return codes;
1092
+ }
1093
+
1094
+ function collectLiteralThrownErrorCodes(source: string): string[] {
1095
+ const codes: string[] = [];
1096
+ THROWN_ERROR_CONSTRUCTION_PATTERN.lastIndex = 0;
1097
+ for (
1098
+ let match = THROWN_ERROR_CONSTRUCTION_PATTERN.exec(source);
1099
+ match;
1100
+ match = THROWN_ERROR_CONSTRUCTION_PATTERN.exec(source)
1101
+ ) {
1102
+ const argsStart = match.index + match[0].length;
1103
+ const args = extractBalancedCallArguments(source, argsStart);
1104
+ if (args !== undefined) {
1105
+ codes.push(...collectLiteralErrorCodeValues(args));
1106
+ }
1107
+ THROWN_ERROR_CONSTRUCTION_PATTERN.lastIndex = argsStart;
1108
+ }
1109
+ return codes;
1110
+ }
1111
+
1112
+ /**
1113
+ * Static counterpart of the runtime `unregistered_provider_error_code`
1114
+ * signal (honest-provider-error-contract Phase 3.5.5): flags
1115
+ * `new ProviderError(...)` / `new ValidationError(...)` constructions whose
1116
+ * literal `code` is neither SDK-registered (SDK_RUNTIME_OWNED_ERROR_CODES
1117
+ * plus the canonical status-mapped codes shared with serve.ts toStatusCode)
1118
+ * nor declared in any operation's docs.errorCodes. At runtime such a code
1119
+ * serves HTTP 500 and emits the signal; this rule surfaces it at check time.
1120
+ *
1121
+ * A throw site cannot be attributed to a specific operation statically —
1122
+ * providers routinely throw from helpers shared across operations — so this
1123
+ * rule matches against the provider-level union of declared codes. That is
1124
+ * the honest scope: it will not catch a code declared only on the "wrong"
1125
+ * operation, and it never claims per-operation attribution it cannot prove.
1126
+ * Only literal string codes are checked; computed/dynamic codes and test
1127
+ * sources are skipped silently. Warning level: the long tail of existing
1128
+ * providers converges gradually, so this must not fail `apifuse check`.
1129
+ */
1130
+ function lintUndeclaredThrownErrorCodes(provider: {
1131
+ authFlowSource?: string;
1132
+ providerSourceFiles?: Record<string, string>;
1133
+ operations?: Record<
1134
+ string,
1135
+ {
1136
+ handler?: unknown;
1137
+ source?: string;
1138
+ docs?: { errorCodes?: ReadonlyArray<{ code: string }> };
1139
+ }
1140
+ >;
1141
+ }): LintDiagnostic[] {
1142
+ const knownCodes = new Set<string>([
1143
+ ...SDK_RUNTIME_OWNED_ERROR_CODES,
1144
+ ...SDK_STATUS_MAPPED_PROVIDER_ERROR_CODES.keys(),
1145
+ ]);
1146
+ for (const operation of Object.values(provider.operations ?? {})) {
1147
+ for (const entry of operation.docs?.errorCodes ?? []) {
1148
+ if (typeof entry?.code === "string") {
1149
+ knownCodes.add(entry.code);
1150
+ }
1151
+ }
1152
+ }
1153
+
1154
+ const sources: Array<{ field: string; source: string }> = [];
1155
+ const sourceFiles = Object.entries(provider.providerSourceFiles ?? {}).filter(
1156
+ ([filePath]) => !TEST_SOURCE_FILE_PATTERN.test(filePath),
1157
+ );
1158
+ if (sourceFiles.length > 0) {
1159
+ for (const [filePath, source] of sourceFiles) {
1160
+ sources.push({ field: `sourceFiles.${filePath}`, source });
1161
+ }
1162
+ } else {
1163
+ if (provider.authFlowSource) {
1164
+ sources.push({ field: "auth.flow", source: provider.authFlowSource });
1165
+ }
1166
+ for (const [operationKey, operation] of Object.entries(provider.operations ?? {})) {
1167
+ const source = getOperationSource(operation);
1168
+ if (source) {
1169
+ sources.push({ field: `operations.${operationKey}.handler`, source });
1170
+ }
1171
+ }
1172
+ }
1173
+
1174
+ const diagnostics: LintDiagnostic[] = [];
1175
+ for (const { field, source } of sources) {
1176
+ const undeclaredCodes = new Set(
1177
+ collectLiteralThrownErrorCodes(source).filter((code) => !knownCodes.has(code)),
1178
+ );
1179
+ for (const code of undeclaredCodes) {
1180
+ diagnostics.push({
1181
+ rule: "thrown-error-code-undeclared",
1182
+ level: "warn",
1183
+ field,
1184
+ message: `Thrown error code "${code}" (${field}) is neither SDK-registered nor declared in any operation's docs.errorCodes; at runtime it serves HTTP 500 and emits the unregistered_provider_error_code signal. Declare it in the owning operation's docs.errorCodes with status and retryable.`,
1185
+ });
1186
+ }
1187
+ }
1188
+ return diagnostics;
1189
+ }
1190
+
796
1191
  export function lintOperation(op: {
797
1192
  description?: string;
798
1193
  descriptionKey?: string;
@@ -935,6 +1330,7 @@ export function lintProvider(
935
1330
  derivations?: Record<string, string>;
936
1331
  handler?: unknown;
937
1332
  source?: string;
1333
+ docs?: { errorCodes?: ReadonlyArray<{ code: string }> };
938
1334
  }
939
1335
  >;
940
1336
  meta?: {
@@ -952,18 +1348,26 @@ export function lintProvider(
952
1348
  ...lintCredentialWriteUsage(provider),
953
1349
  ...lintPlaywrightDirectImports(provider),
954
1350
  ...lintSelfHostedBrowserPatterns(provider, options),
1351
+ ...lintUndeclaredThrownErrorCodes(provider),
955
1352
  ];
956
1353
 
957
1354
  if (provider.operations) {
958
1355
  const authMode = provider.auth?.mode;
959
- if (authMode === "credentials" || authMode === "oauth2") {
1356
+ // Every authenticated mode owns an auth.flow; `oauth2_proxied` was
1357
+ // previously exempt, which let auth-lifecycle operations ship on
1358
+ // proxied providers unchecked.
1359
+ if (
1360
+ authMode === "credentials" ||
1361
+ authMode === "oauth2" ||
1362
+ authMode === "oauth2_proxied"
1363
+ ) {
960
1364
  for (const operationKey of Object.keys(provider.operations)) {
961
- if (AUTH_OPERATION_ID_PATTERN.test(operationKey)) {
1365
+ if (isAuthLifecycleOperationId(operationKey, authMode)) {
962
1366
  diagnostics.push({
963
1367
  rule: "auth-operation-unsupported",
964
1368
  level: "error",
965
1369
  field: `operations.${operationKey}`,
966
- message: `Provider "${provider.id ?? "unknown"}" operation "${operationKey}" looks like a login/token/session exchange endpoint. Authenticated providers must expose login through the single auth.flow interface because Gateway persists only auth.flow complete turn data.credential as the connection credential. Move this logic into auth.flow.continue instead of a provider operation.`,
1370
+ message: `Provider "${provider.id ?? "unknown"}" operation "${operationKey}" performs an auth-lifecycle action (login, logout, token exchange or similar). Authenticated providers must expose the whole credential lifecycle through the single auth.flow interface because Gateway persists only auth.flow complete turn data.credential as the connection credential, and an operation that mutates the session outside that interface leaves the stored connection stale. Move sign-in logic into auth.flow.start/continue and sign-out/disconnect logic into auth.flow.abort (served by POST /auth/disconnect) instead of a provider operation.`,
967
1371
  });
968
1372
  }
969
1373
  }