@apifuse/provider-sdk 2.2.0-beta.61 → 2.2.0-beta.63

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 (258) hide show
  1. package/AUTHORING.md +268 -9
  2. package/CHANGELOG.md +8 -0
  3. package/README.md +1 -0
  4. package/bin/apifuse-check.ts +84 -2
  5. package/bin/apifuse-dev.ts +11 -31
  6. package/bin/apifuse-migrate-deployment.ts +200 -0
  7. package/bin/apifuse-pack-check.ts +11 -4
  8. package/bin/apifuse-pack-types.ts +191 -4
  9. package/bin/apifuse-record.ts +44 -32
  10. package/bin/apifuse-test.ts +3 -1
  11. package/dist/cli/commands.d.ts +1 -1
  12. package/dist/cli/commands.js +11 -0
  13. package/dist/cli/deployment-intent.d.ts +178 -0
  14. package/dist/cli/deployment-intent.js +277 -0
  15. package/dist/cli/migrate-deployment-intent.d.ts +132 -0
  16. package/dist/cli/migrate-deployment-intent.js +1552 -0
  17. package/dist/cli/migrate-provider-shape.d.ts +5 -1
  18. package/dist/cli/migrate-provider-shape.js +4 -2
  19. package/dist/cli/resolver-runtime.d.ts +21 -0
  20. package/dist/cli/resolver-runtime.js +31 -0
  21. package/dist/define.d.ts +3 -0
  22. package/dist/define.js +5 -0
  23. package/dist/dev.d.ts +3 -1
  24. package/dist/dev.js +6 -1
  25. package/dist/engine.d.ts +35 -1
  26. package/dist/engine.js +21 -2
  27. package/dist/error-resolution.d.ts +1 -0
  28. package/dist/error-resolution.js +54 -0
  29. package/dist/errors.d.ts +39 -1
  30. package/dist/errors.js +50 -0
  31. package/dist/handle-meta.d.ts +18 -2
  32. package/dist/handle-meta.js +30 -2
  33. package/dist/handle.d.ts +33 -12
  34. package/dist/handle.js +69 -24
  35. package/dist/i18n/error-messages.d.ts +86 -0
  36. package/dist/i18n/error-messages.js +192 -0
  37. package/dist/i18n/index.d.ts +1 -0
  38. package/dist/i18n/index.js +1 -0
  39. package/dist/i18n/keys.d.ts +12 -0
  40. package/dist/i18n/keys.js +76 -5
  41. package/dist/index.d.ts +10 -2
  42. package/dist/index.js +9 -1
  43. package/dist/lint.d.ts +10 -0
  44. package/dist/lint.js +440 -57
  45. package/dist/provider.d.ts +7 -5
  46. package/dist/provider.js +4 -2
  47. package/dist/runtime/browser-telemetry-binding.d.ts +5 -0
  48. package/dist/runtime/browser-telemetry-binding.js +83 -0
  49. package/dist/runtime/browser-telemetry.d.ts +76 -0
  50. package/dist/runtime/browser-telemetry.js +165 -0
  51. package/dist/runtime/browser.d.ts +3 -0
  52. package/dist/runtime/browser.js +208 -47
  53. package/dist/runtime/cache-telemetry.d.ts +71 -0
  54. package/dist/runtime/cache-telemetry.js +177 -0
  55. package/dist/runtime/cache.js +67 -7
  56. package/dist/runtime/capability-response.d.ts +3 -0
  57. package/dist/runtime/capability-response.js +56 -0
  58. package/dist/runtime/capability-telemetry.d.ts +46 -0
  59. package/dist/runtime/capability-telemetry.js +68 -0
  60. package/dist/runtime/diagnostic-env.d.ts +3 -0
  61. package/dist/runtime/diagnostic-env.js +10 -1
  62. package/dist/runtime/engine-mode.d.ts +74 -0
  63. package/dist/runtime/engine-mode.js +152 -0
  64. package/dist/runtime/handle.js +15 -10
  65. package/dist/runtime/http-telemetry-binding.d.ts +10 -0
  66. package/dist/runtime/http-telemetry-binding.js +152 -0
  67. package/dist/runtime/http-telemetry-guard.d.ts +16 -0
  68. package/dist/runtime/http-telemetry-guard.js +202 -0
  69. package/dist/runtime/http-telemetry.d.ts +104 -0
  70. package/dist/runtime/http-telemetry.js +190 -0
  71. package/dist/runtime/http.d.ts +31 -0
  72. package/dist/runtime/http.js +356 -174
  73. package/dist/runtime/instrumentation.js +92 -13
  74. package/dist/runtime/ocr-telemetry.d.ts +82 -0
  75. package/dist/runtime/ocr-telemetry.js +192 -0
  76. package/dist/runtime/ocr.js +30 -8
  77. package/dist/runtime/request-telemetry.d.ts +2 -0
  78. package/dist/runtime/request-telemetry.js +2 -1
  79. package/dist/runtime/resolver-runtime-options.d.ts +14 -0
  80. package/dist/runtime/resolver-runtime-options.js +33 -0
  81. package/dist/runtime/resolver-shared.d.ts +3 -2
  82. package/dist/runtime/resolver-shared.js +4 -4
  83. package/dist/runtime/resolver-telemetry.d.ts +1 -0
  84. package/dist/runtime/resolver-telemetry.js +1 -1
  85. package/dist/runtime/resolver-vendors/browser.d.ts +2 -0
  86. package/dist/runtime/resolver-vendors/browser.js +46 -0
  87. package/dist/runtime/resolver-vendors/hosts.d.ts +21 -0
  88. package/dist/runtime/resolver-vendors/hosts.js +35 -0
  89. package/dist/runtime/resolver-vendors/hypersolutions.js +2 -2
  90. package/dist/runtime/resolver-vendors/types.d.ts +5 -4
  91. package/dist/runtime/resolver.d.ts +4 -1
  92. package/dist/runtime/resolver.js +46 -13
  93. package/dist/runtime/state-telemetry.d.ts +79 -0
  94. package/dist/runtime/state-telemetry.js +194 -0
  95. package/dist/runtime/state.d.ts +3 -0
  96. package/dist/runtime/state.js +26 -7
  97. package/dist/runtime/stealth-cookies.d.ts +5 -1
  98. package/dist/runtime/stealth-cookies.js +21 -1
  99. package/dist/runtime/stealth-owned-headers.d.ts +26 -0
  100. package/dist/runtime/stealth-owned-headers.js +37 -0
  101. package/dist/runtime/stealth-telemetry.d.ts +92 -0
  102. package/dist/runtime/stealth-telemetry.js +179 -0
  103. package/dist/runtime/stealth.d.ts +4 -1
  104. package/dist/runtime/stealth.js +228 -85
  105. package/dist/runtime/stt-telemetry.d.ts +77 -0
  106. package/dist/runtime/stt-telemetry.js +176 -0
  107. package/dist/runtime/stt.js +15 -6
  108. package/dist/server/error-stack-frames.d.ts +35 -0
  109. package/dist/server/error-stack-frames.js +219 -0
  110. package/dist/server/index.d.ts +9 -0
  111. package/dist/server/index.js +5 -0
  112. package/dist/server/sdk-version.d.ts +3 -0
  113. package/dist/server/sdk-version.js +19 -0
  114. package/dist/server/self-test.js +1 -18
  115. package/dist/server/sensitive-values.d.ts +1 -1
  116. package/dist/server/sensitive-values.js +9 -0
  117. package/dist/server/serve-implementation.d.ts +53 -2
  118. package/dist/server/serve-implementation.js +369 -123
  119. package/dist/server/types.d.ts +1 -0
  120. package/dist/server/types.js +7 -0
  121. package/dist/stateful/stateful-provider-owner-forwarder.js +22 -0
  122. package/dist/stateful/stateful-provider-runtime-context.d.ts +3 -0
  123. package/dist/stateful/stateful-provider-runtime-context.js +1 -0
  124. package/dist/types.d.ts +94 -1
  125. package/package.json +6 -1
  126. package/src/cli/commands.ts +14 -0
  127. package/src/cli/deployment-intent.ts +364 -0
  128. package/src/cli/migrate-deployment-intent.ts +1908 -0
  129. package/src/cli/migrate-provider-shape.ts +10 -4
  130. package/src/cli/resolver-runtime.ts +72 -0
  131. package/src/define.ts +13 -0
  132. package/src/dev.ts +9 -2
  133. package/src/engine.ts +38 -2
  134. package/src/error-resolution.ts +58 -0
  135. package/src/errors.ts +77 -1
  136. package/src/handle-meta.ts +39 -4
  137. package/src/handle.ts +121 -38
  138. package/src/i18n/error-messages.ts +255 -0
  139. package/src/i18n/index.ts +1 -0
  140. package/src/i18n/keys.ts +80 -6
  141. package/src/index.ts +91 -0
  142. package/src/lint.ts +552 -57
  143. package/src/provider.ts +20 -0
  144. package/src/runtime/browser-telemetry-binding.ts +92 -0
  145. package/src/runtime/browser-telemetry.ts +289 -0
  146. package/src/runtime/browser.ts +249 -65
  147. package/src/runtime/cache-telemetry.ts +247 -0
  148. package/src/runtime/cache.ts +68 -17
  149. package/src/runtime/capability-response.ts +53 -0
  150. package/src/runtime/capability-telemetry.ts +101 -0
  151. package/src/runtime/diagnostic-env.ts +11 -1
  152. package/src/runtime/engine-mode.ts +233 -0
  153. package/src/runtime/handle.ts +20 -10
  154. package/src/runtime/http-telemetry-binding.ts +162 -0
  155. package/src/runtime/http-telemetry-guard.ts +208 -0
  156. package/src/runtime/http-telemetry.ts +292 -0
  157. package/src/runtime/http.ts +457 -204
  158. package/src/runtime/instrumentation.ts +105 -16
  159. package/src/runtime/ocr-telemetry.ts +308 -0
  160. package/src/runtime/ocr.ts +109 -74
  161. package/src/runtime/request-telemetry.ts +2 -1
  162. package/src/runtime/resolver-runtime-options.ts +50 -0
  163. package/src/runtime/resolver-shared.ts +9 -4
  164. package/src/runtime/resolver-telemetry.ts +1 -1
  165. package/src/runtime/resolver-vendors/browser.ts +65 -7
  166. package/src/runtime/resolver-vendors/hosts.ts +43 -0
  167. package/src/runtime/resolver-vendors/hypersolutions.ts +2 -2
  168. package/src/runtime/resolver-vendors/types.ts +5 -4
  169. package/src/runtime/resolver.ts +64 -21
  170. package/src/runtime/state-telemetry.ts +298 -0
  171. package/src/runtime/state.ts +46 -45
  172. package/src/runtime/stealth-cookies.ts +24 -1
  173. package/src/runtime/stealth-owned-headers.ts +41 -0
  174. package/src/runtime/stealth-telemetry.ts +304 -0
  175. package/src/runtime/stealth.ts +355 -150
  176. package/src/runtime/stt-telemetry.ts +282 -0
  177. package/src/runtime/stt.ts +93 -71
  178. package/src/server/error-stack-frames.ts +214 -0
  179. package/src/server/index.ts +66 -0
  180. package/src/server/sdk-version.ts +22 -0
  181. package/src/server/self-test.ts +1 -21
  182. package/src/server/sensitive-values.ts +14 -0
  183. package/src/server/serve-implementation.ts +601 -157
  184. package/src/server/types.ts +7 -0
  185. package/src/stateful/stateful-provider-owner-forwarder.ts +23 -0
  186. package/src/stateful/stateful-provider-runtime-context.ts +4 -0
  187. package/src/types.ts +99 -1
  188. package/src/cli/__tests__/fixtures/migrate-operation-declaration/approval-override.ts.txt +0 -6
  189. package/src/cli/__tests__/fixtures/migrate-operation-declaration/codemod-syntax.ts.txt +0 -3
  190. package/src/cli/__tests__/fixtures/migrate-operation-declaration/connection-precedence.ts.txt +0 -10
  191. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-ambiguous-a.ts.txt +0 -3
  192. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-ambiguous-b.ts.txt +0 -3
  193. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-ambiguous-barrel.ts.txt +0 -2
  194. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-ambiguous-index.ts.txt +0 -3
  195. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-computed.ts.txt +0 -6
  196. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-cycle-a.ts.txt +0 -1
  197. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-cycle-b.ts.txt +0 -1
  198. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-cycle-index.ts.txt +0 -3
  199. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-daiso-barrel.ts.txt +0 -7
  200. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-daiso-catalog.ts.txt +0 -19
  201. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-daiso-docs.ts.txt +0 -6
  202. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-daiso-index.ts.txt +0 -3
  203. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-daiso-stores.ts.txt +0 -8
  204. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-indirect-config.ts.txt +0 -5
  205. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-nonstatic.ts.txt +0 -4
  206. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-package-index.ts.txt +0 -3
  207. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-reexport-index.ts.txt +0 -3
  208. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-reexport-leaf.ts.txt +0 -10
  209. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-reexport-one.ts.txt +0 -1
  210. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-reexport-two.ts.txt +0 -1
  211. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-runtime-composed-index.ts.txt +0 -3
  212. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-runtime-composed-one.ts.txt +0 -6
  213. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-runtime-composed-two.ts.txt +0 -6
  214. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-runtime-mixed-index.ts.txt +0 -14
  215. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-runtime-registry.ts.txt +0 -3
  216. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-star-package-barrel.ts.txt +0 -2
  217. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-star-package-index.ts.txt +0 -3
  218. package/src/cli/__tests__/fixtures/migrate-operation-declaration/discovery-star-package-leaf.ts.txt +0 -3
  219. package/src/cli/__tests__/fixtures/migrate-operation-declaration/docs-conflict.ts.txt +0 -8
  220. package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-hoisted-array-spread.ts.txt +0 -15
  221. package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-imported-array-spread.ts.txt +0 -9
  222. package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-kebab-operation-id.ts.txt +0 -17
  223. package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-map.ts.txt +0 -5
  224. package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-mixed-array-spread.ts.txt +0 -24
  225. package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-operation.ts.txt +0 -16
  226. package/src/cli/__tests__/fixtures/migrate-operation-declaration/examples-snake-operation-id.ts.txt +0 -21
  227. package/src/cli/__tests__/fixtures/migrate-operation-declaration/factory-duplicate-provider.ts.txt +0 -11
  228. package/src/cli/__tests__/fixtures/migrate-operation-declaration/factory-id-unresolved.ts.txt +0 -7
  229. package/src/cli/__tests__/fixtures/migrate-operation-declaration/factory-map.ts.txt +0 -3
  230. package/src/cli/__tests__/fixtures/migrate-operation-declaration/factory-one-to-many.ts.txt +0 -18
  231. package/src/cli/__tests__/fixtures/migrate-operation-declaration/factory-one-to-one.ts.txt +0 -13
  232. package/src/cli/__tests__/fixtures/migrate-operation-declaration/hoist-all.ts.txt +0 -31
  233. package/src/cli/__tests__/fixtures/migrate-operation-declaration/hoisted-const.ts.txt +0 -11
  234. package/src/cli/__tests__/fixtures/migrate-operation-declaration/imported-spread.ts.txt +0 -11
  235. package/src/cli/__tests__/fixtures/migrate-operation-declaration/inline-map.ts.txt +0 -11
  236. package/src/cli/__tests__/fixtures/migrate-operation-declaration/inline-spread-cast-tail.ts.txt +0 -21
  237. package/src/cli/__tests__/fixtures/migrate-operation-declaration/inline-spread-ekitan.ts.txt +0 -11
  238. package/src/cli/__tests__/fixtures/migrate-operation-declaration/inline-spread-override.ts.txt +0 -14
  239. package/src/cli/__tests__/fixtures/migrate-operation-declaration/locale-canonical-en.json +0 -1
  240. package/src/cli/__tests__/fixtures/migrate-operation-declaration/locale-canonical-ja.json +0 -1
  241. package/src/cli/__tests__/fixtures/migrate-operation-declaration/locale-canonical-ko.json +0 -1
  242. package/src/cli/__tests__/fixtures/migrate-operation-declaration/locale-existing-operation-namespace-en.json +0 -8
  243. package/src/cli/__tests__/fixtures/migrate-operation-declaration/missing-english-locale.ts.txt +0 -7
  244. package/src/cli/__tests__/fixtures/migrate-operation-declaration/no-safety.ts.txt +0 -6
  245. package/src/cli/__tests__/fixtures/migrate-operation-declaration/non-literal.ts.txt +0 -7
  246. package/src/cli/__tests__/fixtures/migrate-operation-declaration/redundant-approval.ts.txt +0 -6
  247. package/src/cli/__tests__/fixtures/migrate-operation-declaration/registry-binding-ambiguous.ts.txt +0 -12
  248. package/src/cli/__tests__/fixtures/migrate-operation-declaration/registry-imported-other.ts.txt +0 -7
  249. package/src/cli/__tests__/fixtures/migrate-operation-declaration/registry-imported.ts.txt +0 -5
  250. package/src/cli/__tests__/fixtures/migrate-operation-declaration/registry-key-ambiguous.ts.txt +0 -19
  251. package/src/cli/__tests__/fixtures/migrate-operation-declaration/registry-shorthand.ts.txt +0 -9
  252. package/src/cli/__tests__/fixtures/migrate-operation-declaration/registry-typed.ts.txt +0 -11
  253. package/src/cli/__tests__/fixtures/migrate-operation-declaration/registry-unrelated.ts.txt +0 -13
  254. package/src/cli/__tests__/fixtures/migrate-operation-declaration/safety-conflict.ts.txt +0 -7
  255. package/src/cli/__tests__/fixtures/migrate-operation-declaration/stream.ts.txt +0 -7
  256. package/src/cli/__tests__/fixtures/migrate-operation-declaration/tool-router-spread.ts.txt +0 -15
  257. package/src/cli/__tests__/fixtures/migrate-operation-declaration/unparseable.ts.txt +0 -4
  258. package/src/cli/__tests__/fixtures/migrate-operation-declaration/verbatim-template.ts.txt +0 -12
@@ -0,0 +1,233 @@
1
+ import {
2
+ createInProcessProviderEngine,
3
+ PROVIDER_ENGINE_MODE_ENV,
4
+ type ProviderEngine,
5
+ type ProviderEngineMode,
6
+ } from "../engine.js";
7
+ import { ProviderError, ValidationError } from "../errors.js";
8
+
9
+ /**
10
+ * Engine attachment selection. Internal on purpose: ADR-0011 removes the
11
+ * in-process lane, so every symbol exported here would become a breaking removal
12
+ * later. The server surfaces the outcome on the `provider_engine_mode` boot
13
+ * event instead.
14
+ */
15
+
16
+ const KNOWN_PROVIDER_ENGINE_MODES = ["in-process", "remote"] as const;
17
+
18
+ /** Mode used when neither `engineMode` nor `APIFUSE__ENGINE__MODE` names one. */
19
+ export const DEFAULT_PROVIDER_ENGINE_MODE = "in-process" satisfies ProviderEngineMode;
20
+
21
+ /** Remote engine client endpoint; engine-owned, so never declarable as a provider secret. */
22
+ export const REMOTE_ENGINE_CLIENT_URL_ENV = "APIFUSE__ENGINE__URL";
23
+ /** Remote engine client credential; registered with the diagnostic redactor by sensitive-values. */
24
+ export const REMOTE_ENGINE_CLIENT_API_KEY_ENV = "APIFUSE__ENGINE__API_KEY";
25
+
26
+ /** Engine env names that only mean something in remote mode. */
27
+ const REMOTE_ONLY_ENGINE_ENV_NAMES = [
28
+ REMOTE_ENGINE_CLIENT_URL_ENV,
29
+ REMOTE_ENGINE_CLIENT_API_KEY_ENV,
30
+ ] as const;
31
+
32
+ /** Where the resolved mode came from, in precedence order. */
33
+ export type ProviderEngineModeSource = "engine" | "env" | "option" | "default";
34
+
35
+ /**
36
+ * Non-fatal conditions worth reporting at boot:
37
+ * - `invalid_env_value`: `APIFUSE__ENGINE__MODE` held an unrecognized value.
38
+ * - `env_overrode_option`: the manifest and the host code disagreed; the manifest won.
39
+ * - `engine_object_overrode_requested_mode`: an explicit engine object cannot honour the requested mode.
40
+ * - `engine_env_ignored`: remote-only engine env is projected but the mode is not
41
+ * remote (not reported for a host-supplied engine, which may consume it itself).
42
+ */
43
+ export type ProviderEngineModeWarning =
44
+ | "invalid_env_value"
45
+ | "env_overrode_option"
46
+ | "engine_object_overrode_requested_mode"
47
+ | "engine_env_ignored";
48
+
49
+ export interface ProviderEngineModeResolution {
50
+ readonly mode: ProviderEngineMode | "custom";
51
+ readonly source: ProviderEngineModeSource;
52
+ /** True for every non-remote attachment, whose removal is scheduled by ADR-0011. */
53
+ readonly deprecated: boolean;
54
+ readonly warnings: readonly ProviderEngineModeWarning[];
55
+ }
56
+
57
+ export interface ProviderEngineModeResolutionInput {
58
+ /** Explicit `engineMode` option passed by the host process. */
59
+ readonly option?: ProviderEngineMode;
60
+ /** Explicit engine object passed by the host; used as given when present. */
61
+ readonly engine?: Pick<ProviderEngine, "kind">;
62
+ /** Environment to read the engine env from; defaults to `process.env`. */
63
+ readonly environment?: Readonly<Record<string, string | undefined>>;
64
+ }
65
+
66
+ function isKnownMode(value: string): value is (typeof KNOWN_PROVIDER_ENGINE_MODES)[number] {
67
+ return (KNOWN_PROVIDER_ENGINE_MODES as readonly string[]).includes(value);
68
+ }
69
+
70
+ function normalize(value: unknown): string | undefined {
71
+ if (typeof value !== "string") return undefined;
72
+ const trimmed = value.trim().toLowerCase();
73
+ return trimmed === "" ? undefined : trimmed;
74
+ }
75
+
76
+ /**
77
+ * Resolve the engine attachment mode once at boot.
78
+ *
79
+ * Precedence: an explicit `engine` object, then `APIFUSE__ENGINE__MODE`, then the
80
+ * `engineMode` option, then the default. The manifest outranks the option so
81
+ * provider code cannot silently downgrade what the deployment asked for; every
82
+ * disagreement is reported in `warnings` with a truthful `source` rather than
83
+ * being dropped.
84
+ *
85
+ * Deployment-authored input is never fatal (an unrecognized env value is warned
86
+ * about and ignored). A malformed `engineMode` option is a fault in the calling
87
+ * code, which no manifest can reach, so it throws.
88
+ */
89
+ export function resolveProviderEngineMode(
90
+ input: ProviderEngineModeResolutionInput = {},
91
+ ): ProviderEngineModeResolution {
92
+ const environment = input.environment ?? process.env;
93
+ const warnings: ProviderEngineModeWarning[] = [];
94
+
95
+ // Read directly: this is a mode name, not a credential, so it must not be
96
+ // registered with the diagnostic redactor the way readDiagnosticEnv is.
97
+ const rawEnv = normalize(environment[PROVIDER_ENGINE_MODE_ENV]);
98
+ let envMode: ProviderEngineMode | undefined;
99
+ if (rawEnv !== undefined) {
100
+ if (isKnownMode(rawEnv)) envMode = rawEnv;
101
+ else warnings.push("invalid_env_value");
102
+ }
103
+
104
+ const optionMode = normalize(input.option);
105
+ if (input.option !== undefined && (optionMode === undefined || !isKnownMode(optionMode))) {
106
+ throw new ValidationError(
107
+ `Invalid engineMode option "${String(input.option)}"; expected ${KNOWN_PROVIDER_ENGINE_MODES.join(" or ")}`,
108
+ { fix: 'Pass engineMode: "in-process" | "remote", or omit it to use the default.' },
109
+ );
110
+ }
111
+ if (optionMode !== undefined && envMode !== undefined && optionMode !== envMode) {
112
+ warnings.push("env_overrode_option");
113
+ }
114
+
115
+ const requested = envMode ?? optionMode;
116
+ const requestedSource: ProviderEngineModeSource | undefined =
117
+ envMode !== undefined ? "env" : optionMode !== undefined ? "option" : undefined;
118
+
119
+ // Normalized like every other mode input: an un-normalized host `kind` would
120
+ // reach the boot event and /readyz verbatim and mis-score the fleet audit.
121
+ const engineKind =
122
+ input.engine === undefined ? undefined : (normalize(input.engine.kind) ?? "custom");
123
+ if (engineKind !== undefined) {
124
+ if (requested !== undefined && engineKind !== requested) {
125
+ warnings.push("engine_object_overrode_requested_mode");
126
+ }
127
+ return finalize(engineKind, "engine", environment, warnings);
128
+ }
129
+
130
+ return finalize(
131
+ requested ?? DEFAULT_PROVIDER_ENGINE_MODE,
132
+ requestedSource ?? "default",
133
+ environment,
134
+ warnings,
135
+ );
136
+ }
137
+
138
+ function finalize(
139
+ mode: ProviderEngineMode | "custom",
140
+ source: ProviderEngineModeSource,
141
+ environment: Readonly<Record<string, string | undefined>>,
142
+ warnings: ProviderEngineModeWarning[],
143
+ ): ProviderEngineModeResolution {
144
+ // Presence of the remote engine env never selects a mode (the manifest
145
+ // generator projects it fleet-wide before the SDK can use it), but staying
146
+ // silent about it would hide a half-applied cutover. Skipped for a host-supplied
147
+ // engine: that object may be a remote client that consumes the env itself, so
148
+ // claiming the env is unused would be a false report.
149
+ if (
150
+ mode !== "remote" &&
151
+ source !== "engine" &&
152
+ REMOTE_ONLY_ENGINE_ENV_NAMES.some((name) => normalize(environment[name]) !== undefined)
153
+ ) {
154
+ warnings.push("engine_env_ignored");
155
+ }
156
+ // Fail-closed for the ADR-0011 audit: everything that is not the remote lane
157
+ // still attaches capabilities inside the pod process and is scheduled for
158
+ // removal, including an opaque host engine that declares no kind.
159
+ return { mode, source, deprecated: mode !== "remote", warnings };
160
+ }
161
+
162
+ const UNAVAILABLE_ENGINES = new WeakSet<ProviderEngine>();
163
+
164
+ /** True when the engine cannot attach, so readiness must report the process as unready. */
165
+ export function isUnavailableProviderEngine(engine: ProviderEngine): boolean {
166
+ return UNAVAILABLE_ENGINES.has(engine);
167
+ }
168
+
169
+ /**
170
+ * Engine that refuses every attachment with `reason`.
171
+ *
172
+ * Requesting a mode this release cannot serve must not crash the process: the SDK
173
+ * answers a structured error per request and reports the process unready, the same
174
+ * trade-off `provider_secrets_missing` already makes at boot. Non-retryable, so a
175
+ * caller-side retry loop cannot turn a configuration fault into load.
176
+ */
177
+ export function createUnavailableProviderEngine(
178
+ kind: ProviderEngineMode,
179
+ reason: { message: string; code: string; fix: string },
180
+ ): ProviderEngine {
181
+ const engine: ProviderEngine = {
182
+ kind,
183
+ attach: () => {
184
+ throw new ProviderError(reason.message, {
185
+ code: reason.code,
186
+ retryable: false,
187
+ fix: reason.fix,
188
+ });
189
+ },
190
+ };
191
+ UNAVAILABLE_ENGINES.add(engine);
192
+ return engine;
193
+ }
194
+
195
+ /**
196
+ * Engine for a resolved mode. `remote` has no client in this release, so it
197
+ * resolves to an engine that fails every request instead of falling back to
198
+ * in-process (ADR-0011 Pitfall 2: in-process is never a fallback).
199
+ */
200
+ export function createEngineForMode(mode: ProviderEngineMode | "custom"): ProviderEngine {
201
+ // Default-deny on the mode name, not just on "remote": the mode union is open
202
+ // so a later lane can be named, and falling through to in-process for an
203
+ // unserved lane is the silent downgrade ADR-0011 Pitfall 2 forbids.
204
+ if (mode === DEFAULT_PROVIDER_ENGINE_MODE) return createInProcessProviderEngine();
205
+ return createUnavailableProviderEngine(mode, {
206
+ message: `Provider engine mode "${mode}" was requested but this @apifuse/provider-sdk release only serves the ${DEFAULT_PROVIDER_ENGINE_MODE} engine`,
207
+ code: "PROVIDER_ENGINE_MODE_UNSUPPORTED",
208
+ fix: `Unset ${PROVIDER_ENGINE_MODE_ENV} (or the engineMode option), or upgrade to a release that serves this mode.`,
209
+ });
210
+ }
211
+
212
+ /**
213
+ * Fail fast for the CLIs (`apifuse dev`, `apifuse record`). They are developer
214
+ * tools, not pods: an unusable mode should stop the command rather than start a
215
+ * server that refuses every request.
216
+ */
217
+ export function assertProcessEngineModeSupported(
218
+ environment: Readonly<Record<string, string | undefined>> = process.env,
219
+ ): ProviderEngineModeResolution {
220
+ const resolution = resolveProviderEngineMode({ environment });
221
+ if (resolution.mode !== DEFAULT_PROVIDER_ENGINE_MODE) {
222
+ throw new ProviderError(
223
+ `Provider engine mode "${resolution.mode}" was requested but this @apifuse/provider-sdk release only serves the ${DEFAULT_PROVIDER_ENGINE_MODE} engine`,
224
+ {
225
+ code: "PROVIDER_ENGINE_MODE_UNSUPPORTED",
226
+ retryable: false,
227
+ details: { mode: resolution.mode, source: resolution.source },
228
+ fix: `Unset ${PROVIDER_ENGINE_MODE_ENV}, or upgrade to a release that serves this mode.`,
229
+ },
230
+ );
231
+ }
232
+ return resolution;
233
+ }
@@ -3,7 +3,8 @@
3
3
  *
4
4
  * Storage layout: one state namespace per kind, `handle.<kind>`, scoped by the
5
5
  * kind's access (bound → connection scope via `state.forConnection`, public →
6
- * provider scope). The state key is the canonical handle string. Records hold
6
+ * provider scope; both cursors and drafts may be either). The state key is the
7
+ * canonical handle string. Records hold
7
8
  * the validated data plus status/timestamps; the handle string itself carries
8
9
  * no payload and no signature.
9
10
  */
@@ -26,6 +27,7 @@ import {
26
27
  isDraftKind,
27
28
  type ResultOf,
28
29
  } from "../handle.js";
30
+ import { formatHandleIssuers } from "../handle-meta.js";
29
31
  import type {
30
32
  ProviderRequestContext,
31
33
  ProviderRuntimeState,
@@ -246,9 +248,8 @@ export function formatNormalizationClasses(classes: readonly string[]): string {
246
248
  // ---------------------------------------------------------------------------
247
249
 
248
250
  function publicCollapsedMessage(kind: HandleKind): string {
249
- const again = kind.issuedBy
250
- ? `Call \`${kind.issuedBy}\` again to get a new one.`
251
- : "Request a new one.";
251
+ const issuers = formatHandleIssuers(kind.issuedBy);
252
+ const again = issuers ? `Call ${issuers} again to get a new one.` : "Request a new one.";
252
253
  return `\`${kind.fieldName}\` is not valid or has expired. ${again}`;
253
254
  }
254
255
 
@@ -257,7 +258,7 @@ function normalizationError(kind: HandleKind, failure: NormalizationFailure): Ha
257
258
  return new HandleError(
258
259
  "HANDLE_KIND_MISMATCH",
259
260
  `\`${kind.fieldName}\` must be a \`${kind.name}_...\` handle as returned by ${
260
- kind.issuedBy ? `\`${kind.issuedBy}\`` : "the issuing operation"
261
+ formatHandleIssuers(kind.issuedBy) ?? "the issuing operation"
261
262
  }. ${handleRecoverySentence(kind)}`,
262
263
  { kind: kind.name, details: { field: kind.fieldName } },
263
264
  );
@@ -352,7 +353,7 @@ function committedError(kind: HandleKind): HandleError {
352
353
  function connectionRequiredError(kind: HandleKind, operation: HandleOperation): HandleError {
353
354
  return new HandleError(
354
355
  "HANDLE_CONNECTION_REQUIRED",
355
- `Handle kind "${kind.name}" is bound to a connection, but request.connectionId is missing for ${operation}. Bound handles require a connection-scoped request; use access: "public" for connection-less cursors.`,
356
+ `Handle kind "${kind.name}" is bound to a connection, but request.connectionId is missing for ${operation}. Bound handles require a connection-scoped request; use access: "public" for connection-less kinds.`,
356
357
  { kind: kind.name },
357
358
  );
358
359
  }
@@ -738,8 +739,11 @@ export function createHandleContext(options: CreateHandleContextOptions): Handle
738
739
  };
739
740
  }
740
741
 
741
- async function create<K extends HandleKind>(kind: K, data: InputOf<K>): Promise<string> {
742
- return await instrumented<string>(kind, "create", async (report) => {
742
+ async function createRecord<K extends HandleKind>(
743
+ kind: K,
744
+ data: InputOf<K>,
745
+ ): Promise<HandleRecord<K>> {
746
+ return await instrumented<HandleRecord<K>>(kind, "create", async (report) => {
743
747
  const validated = validateData(kind, "create", data);
744
748
  const namespace = resolveNamespace(kind, "create");
745
749
  const now = nowMs();
@@ -766,7 +770,9 @@ export function createHandleContext(options: CreateHandleContextOptions): Handle
766
770
  const result = await storage(kind, () =>
767
771
  namespace.compareAndSet(canonical, 0, record, { ttl: msDuration(writeTtlMs) }),
768
772
  );
769
- if (result.ok) return { value: canonical, outcome: "success" };
773
+ if (result.ok) {
774
+ return { value: toHandleRecord(kind, canonical, record), outcome: "success" };
775
+ }
770
776
  }
771
777
  throw storageUnavailableError(
772
778
  kind,
@@ -775,6 +781,10 @@ export function createHandleContext(options: CreateHandleContextOptions): Handle
775
781
  });
776
782
  }
777
783
 
784
+ async function create<K extends HandleKind>(kind: K, data: InputOf<K>): Promise<string> {
785
+ return (await createRecord(kind, data)).handle;
786
+ }
787
+
778
788
  async function read<K extends HandleKind>(kind: K, handle: string): Promise<HandleRecord<K>> {
779
789
  return await instrumented<HandleRecord<K>>(kind, "read", async (report) => {
780
790
  const loaded = await load(kind, "read", handle, report);
@@ -1139,7 +1149,7 @@ export function createHandleContext(options: CreateHandleContextOptions): Handle
1139
1149
  });
1140
1150
  }
1141
1151
 
1142
- return { create, read, update, commit, discard };
1152
+ return { create, createRecord, read, update, commit, discard };
1143
1153
  }
1144
1154
 
1145
1155
  /**
@@ -0,0 +1,162 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ import type { HttpClient } from "../types.js";
3
+ import { type HttpTelemetrySink, httpTelemetryErrorCode } from "./http-telemetry.js";
4
+ import { startHttpTelemetry } from "./http-telemetry-guard.js";
5
+ import {
6
+ proxyTransportRetryErrorCode,
7
+ proxyTransportRetryErrorStatus,
8
+ } from "./proxy-retry-policy.js";
9
+
10
+ type HttpTelemetryScope = { sink: HttpTelemetrySink; nativeObserved: boolean };
11
+ const scopes = new AsyncLocalStorage<HttpTelemetryScope>();
12
+ const methods = new Set(["request", "get", "post", "put", "delete", "stream", "sse"]);
13
+ type AdapterState = { sink: HttpTelemetrySink; adapter: HttpClient };
14
+ const adapters = new WeakMap<object, AdapterState>();
15
+ let unsupportedShapeWarned = false;
16
+
17
+ /** Return whether a host binding has at least one callable declared HTTP method. */
18
+ export function isSupportedHttpBinding(value: unknown): value is HttpClient {
19
+ if ((typeof value !== "object" && typeof value !== "function") || value === null) return false;
20
+ for (const method of methods) {
21
+ try {
22
+ if (typeof Reflect.get(value, method) === "function") return true;
23
+ } catch {
24
+ // A hostile property getter is not a usable binding.
25
+ }
26
+ }
27
+ return false;
28
+ }
29
+
30
+ /** Emit the unsupported-shape warning once for the process. */
31
+ export function warnUnsupportedHttpBinding(): void {
32
+ if (unsupportedShapeWarned) return;
33
+ unsupportedShapeWarned = true;
34
+ try {
35
+ console.warn("[apifuse] http telemetry not attached; reason=unsupported_binding_shape");
36
+ } catch {
37
+ // Warning output is diagnostic only and must not affect request handling.
38
+ }
39
+ }
40
+
41
+ /** Route SDK transports inside a host binding through the same attempt collector. */
42
+ export function scopedHttpTelemetry(
43
+ existing: HttpTelemetrySink | undefined,
44
+ ): HttpTelemetrySink | undefined {
45
+ const scope = scopes.getStore();
46
+ if (!scope) return existing;
47
+ scope.nativeObserved = true;
48
+ if (!existing || existing === scope.sink) return scope.sink;
49
+ return {
50
+ markTelemetryFailed: () => scope.sink.markTelemetryFailed?.(),
51
+ startRequest(options) {
52
+ const request = startHttpTelemetry(scope.sink, options.retryPreset);
53
+ const observer = startHttpTelemetry(
54
+ {
55
+ startRequest: (options) => existing.startRequest(options),
56
+ markTelemetryFailed() {
57
+ scope.sink.markTelemetryFailed?.();
58
+ existing.markTelemetryFailed?.();
59
+ },
60
+ },
61
+ options.retryPreset,
62
+ );
63
+ return {
64
+ recordAttempt(event) {
65
+ request.recordAttempt(event);
66
+ observer.recordAttempt(event);
67
+ },
68
+ finish(ms) {
69
+ request.finish(ms);
70
+ observer.finish(ms);
71
+ },
72
+ toTenantRetryPayload() {
73
+ observer.toTenantRetryPayload();
74
+ return request.toTenantRetryPayload();
75
+ },
76
+ };
77
+ },
78
+ };
79
+ }
80
+
81
+ /** Observe opaque host calls without changing their arguments, receiver or returned promise. */
82
+ export function bindHttpTelemetry(client: HttpClient, sink: HttpTelemetrySink): HttpClient {
83
+ if ((typeof client === "object" && client !== null) || typeof client === "function") {
84
+ const existing = adapters.get(client as object);
85
+ if (existing) {
86
+ existing.sink = sink;
87
+ return existing.adapter;
88
+ }
89
+ }
90
+ const wrappers = new Map<
91
+ PropertyKey,
92
+ { original: unknown; wrapped: (...args: unknown[]) => unknown }
93
+ >();
94
+ // An inheriting target also supports hosts whose own methods are frozen.
95
+ const adapter = new Proxy(Object.create(client) as HttpClient, {
96
+ get(_target, property) {
97
+ const original: unknown = Reflect.get(client, property, client);
98
+ if (typeof property !== "string" || !methods.has(property) || typeof original !== "function")
99
+ return original;
100
+ const cached = wrappers.get(property);
101
+ if (cached?.original === original) return cached.wrapped;
102
+ const wrapped = function (this: unknown, ...args: unknown[]) {
103
+ // Direct calls keep the host receiver; explicit and unbound receivers pass through.
104
+ const receiver = this === adapter ? client : this;
105
+ const scope: HttpTelemetryScope = { sink: state.sink, nativeObserved: false };
106
+ const started = performance.now();
107
+ const settled = (result: unknown, failed: boolean) => {
108
+ if (scope.nativeObserved) return;
109
+ const request = startHttpTelemetry(state.sink, undefined);
110
+ request.observe(() => {
111
+ const status = failed
112
+ ? proxyTransportRetryErrorStatus(result)
113
+ : result &&
114
+ typeof result === "object" &&
115
+ "status" in result &&
116
+ typeof result.status === "number"
117
+ ? result.status
118
+ : undefined;
119
+ request.recordAttempt({
120
+ ms: performance.now() - started,
121
+ proxyUsed: false,
122
+ ...(status === undefined ? {} : { status }),
123
+ ...(failed
124
+ ? { e: httpTelemetryErrorCode(proxyTransportRetryErrorCode(result)) }
125
+ : {}),
126
+ });
127
+ });
128
+ request.finish(performance.now() - started);
129
+ };
130
+ return scopes.run(scope, () => {
131
+ let result: unknown;
132
+ try {
133
+ result = Reflect.apply(original, receiver, args);
134
+ } catch (error) {
135
+ settled(error, true);
136
+ throw error;
137
+ }
138
+ try {
139
+ if (result instanceof Promise) {
140
+ void Promise.prototype.then.call(
141
+ result,
142
+ (value: unknown) => settled(value, false),
143
+ (error: unknown) => settled(error, true),
144
+ );
145
+ } else settled(result, false);
146
+ } catch (error) {
147
+ // Promise subclasses may throw while a telemetry continuation is attached.
148
+ startHttpTelemetry(state.sink, undefined).observe(() => {
149
+ throw error;
150
+ });
151
+ }
152
+ return result;
153
+ });
154
+ };
155
+ wrappers.set(property, { original, wrapped });
156
+ return wrapped;
157
+ },
158
+ });
159
+ const state: AdapterState = { sink, adapter };
160
+ adapters.set(adapter as object, state);
161
+ return adapter;
162
+ }
@@ -0,0 +1,208 @@
1
+ import { isPromise } from "node:util/types";
2
+ import type { HttpRetrySummary } from "../types.js";
3
+ import {
4
+ HttpTelemetryCollector,
5
+ type HttpTelemetryRequestSink,
6
+ type HttpTelemetrySink,
7
+ } from "./http-telemetry.js";
8
+
9
+ const intrinsicPromiseThen = Promise.prototype.then;
10
+ const ignoreObserverResult = () => {};
11
+
12
+ /** Attach handlers only through the Promise intrinsic; never invoke a host thenable. */
13
+ function absorbObserverPromise(value: object): void {
14
+ let constructorDescriptor: PropertyDescriptor | undefined;
15
+ let shadowed = false;
16
+ try {
17
+ constructorDescriptor = Object.getOwnPropertyDescriptor(value, "constructor");
18
+ // Even the intrinsic `then` consults Symbol.species. An undefined constructor
19
+ // selects its intrinsic Promise without reading a subclass's species getter.
20
+ Object.defineProperty(value, "constructor", {
21
+ value: undefined,
22
+ configurable: constructorDescriptor?.configurable ?? true,
23
+ });
24
+ shadowed = true;
25
+ } catch {
26
+ // Non-extensible ordinary promises can still accept intrinsic handlers.
27
+ }
28
+ try {
29
+ void intrinsicPromiseThen.call(value, ignoreObserverResult, ignoreObserverResult);
30
+ } catch {
31
+ // A non-Promise thenable has no intrinsic rejection to absorb. Do not run it.
32
+ } finally {
33
+ if (shadowed) {
34
+ try {
35
+ if (constructorDescriptor) {
36
+ Object.defineProperty(value, "constructor", constructorDescriptor);
37
+ } else {
38
+ Reflect.deleteProperty(value, "constructor");
39
+ }
40
+ } catch {
41
+ // Host object traps are also observer failures.
42
+ }
43
+ }
44
+ }
45
+ }
46
+
47
+ function isAsyncObserverReturn(value: unknown): boolean {
48
+ if (!value || (typeof value !== "object" && typeof value !== "function")) return false;
49
+ try {
50
+ if (!isPromise(value) && (!("then" in value) || typeof value.then !== "function")) return false;
51
+ } catch {
52
+ // A rejected Promise can itself have a throwing `then` getter. Still absorb it.
53
+ }
54
+ absorbObserverPromise(value);
55
+ return true;
56
+ }
57
+
58
+ /** Shared synchronous observer boundary for transport and capability contributors. */
59
+ export function observeTelemetry<T>(callback: () => T, fail: () => void): T | undefined {
60
+ try {
61
+ const result = callback();
62
+ if (isAsyncObserverReturn(result)) {
63
+ fail();
64
+ return undefined;
65
+ }
66
+ return result;
67
+ } catch {
68
+ fail();
69
+ return undefined;
70
+ }
71
+ }
72
+
73
+ /** Isolate synchronous void capability hooks using the same Promise defence. */
74
+ export function createTelemetryObserver(
75
+ sink: { markTelemetryFailed?(): void } | undefined,
76
+ ): (callback: () => unknown) => void {
77
+ let failed = false;
78
+ const fail = () => {
79
+ if (failed) return;
80
+ failed = true;
81
+ try {
82
+ isAsyncObserverReturn(sink?.markTelemetryFailed?.());
83
+ } catch {
84
+ // The failure marker is also an observer.
85
+ }
86
+ };
87
+ return (callback) => {
88
+ try {
89
+ const result = callback();
90
+ if (result !== undefined) {
91
+ isAsyncObserverReturn(result);
92
+ fail();
93
+ }
94
+ } catch {
95
+ fail();
96
+ }
97
+ };
98
+ }
99
+
100
+ /** Keep all public observers, including property access, outside transport failures. */
101
+ export function startHttpTelemetry(
102
+ sink: HttpTelemetrySink | undefined,
103
+ retryPreset: HttpRetrySummary["preset"],
104
+ ): HttpTelemetryRequestSink & { observe(callback: () => void): void } {
105
+ let failed = false;
106
+ const fail = () => {
107
+ if (failed) return;
108
+ failed = true;
109
+ try {
110
+ isAsyncObserverReturn(sink?.markTelemetryFailed?.());
111
+ } catch {
112
+ // The failure reporter is itself an observer. Never recurse.
113
+ }
114
+ };
115
+ const observe = <T>(callback: () => T): T | undefined => observeTelemetry(callback, fail);
116
+ const result = observe(() =>
117
+ (sink ?? new HttpTelemetryCollector()).startRequest({ retryPreset }),
118
+ );
119
+ const request = observe(() => {
120
+ if (
121
+ !result ||
122
+ typeof result.recordAttempt !== "function" ||
123
+ typeof result.finish !== "function" ||
124
+ typeof result.toTenantRetryPayload !== "function"
125
+ ) {
126
+ fail();
127
+ return undefined;
128
+ }
129
+ return result;
130
+ });
131
+ return {
132
+ observe,
133
+ recordAttempt: (event) => {
134
+ observe(() => request?.recordAttempt(event));
135
+ },
136
+ finish: (ms) => {
137
+ observe(() => request?.finish(ms));
138
+ },
139
+ toTenantRetryPayload: () => {
140
+ if (failed) return undefined;
141
+ return observe(() => {
142
+ const value = observe(() => request?.toTenantRetryPayload());
143
+ if (value === undefined) return undefined;
144
+ // Copy only validated primitive fields; never pass observer-owned objects on.
145
+ const { attempts, retries, preset, transport, lastErrorCode, lastStatus } = value;
146
+ if (
147
+ !Number.isSafeInteger(attempts) ||
148
+ attempts < 2 ||
149
+ !Number.isSafeInteger(retries) ||
150
+ (retries !== attempts - 1 &&
151
+ !(attempts === Number.MAX_SAFE_INTEGER && retries === Number.MAX_SAFE_INTEGER)) ||
152
+ transport !== "native" ||
153
+ (preset !== undefined &&
154
+ ![
155
+ "off",
156
+ "transport_transient",
157
+ "safe_read",
158
+ "aggressive_read",
159
+ "rate_limit_aware",
160
+ ].includes(preset)) ||
161
+ (lastErrorCode !== undefined &&
162
+ ![
163
+ "transport_network_error",
164
+ "transport_timeout",
165
+ "transport_cancelled",
166
+ "upstream_http_error",
167
+ "other",
168
+ ].includes(lastErrorCode)) ||
169
+ (lastStatus !== undefined && (!Number.isSafeInteger(lastStatus) || lastStatus < 0))
170
+ ) {
171
+ fail();
172
+ return undefined;
173
+ }
174
+ return {
175
+ attempts,
176
+ retries,
177
+ ...(preset === undefined ? {} : { preset }),
178
+ transport,
179
+ ...(lastErrorCode === undefined ? {} : { lastErrorCode }),
180
+ ...(lastStatus === undefined ? {} : { lastStatus }),
181
+ };
182
+ });
183
+ },
184
+ };
185
+ }
186
+
187
+ /** Generic observer guard shared by capability hooks. Returned thenables are absorbed. */
188
+ export function observeTelemetryCallback(
189
+ sink: { markTelemetryFailed?: () => unknown } | undefined,
190
+ callback: () => unknown,
191
+ ): void {
192
+ let failed = false;
193
+ const fail = () => {
194
+ if (failed) return;
195
+ failed = true;
196
+ try {
197
+ isAsyncObserverReturn(sink?.markTelemetryFailed?.());
198
+ } catch {
199
+ // Observer failure is never transport failure.
200
+ }
201
+ };
202
+ try {
203
+ const result = callback();
204
+ if (isAsyncObserverReturn(result) || result !== undefined) fail();
205
+ } catch {
206
+ fail();
207
+ }
208
+ }