@qvac/sdk 0.22.0 → 0.24.0

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 (284) hide show
  1. package/NOTICE +194 -97
  2. package/README.md +29 -470
  3. package/dist/examples/asr/parakeet-sortformer.js +24 -14
  4. package/dist/examples/asr/parakeet-transcribepsy-filesystem.d.ts +2 -0
  5. package/dist/examples/asr/parakeet-transcribepsy-filesystem.js +105 -0
  6. package/dist/examples/asr/parakeet-unified-coreml.js +14 -9
  7. package/dist/examples/asr/whispercpp-prompt.js +9 -5
  8. package/dist/examples/assess-model-fit.d.ts +7 -9
  9. package/dist/examples/assess-model-fit.js +22 -43
  10. package/dist/examples/calc-tokens.d.ts +2 -0
  11. package/dist/examples/calc-tokens.js +21 -0
  12. package/dist/examples/classification/classify-image.js +11 -5
  13. package/dist/examples/completion-stop-reason.d.ts +3 -3
  14. package/dist/examples/completion-stop-reason.js +4 -4
  15. package/dist/examples/diffusion-flux3-klein.d.ts +2 -0
  16. package/dist/examples/diffusion-flux3-klein.js +102 -0
  17. package/dist/examples/diffusion-img2vid-ltx.js +7 -14
  18. package/dist/examples/diffusion-moss-image.d.ts +2 -0
  19. package/dist/examples/diffusion-moss-image.js +126 -0
  20. package/dist/examples/diffusion-qwen-image-21.d.ts +2 -0
  21. package/dist/examples/diffusion-qwen-image-21.js +115 -0
  22. package/dist/examples/diffusion-txt2vid-ltx.js +7 -14
  23. package/dist/examples/embed-multimodal.d.ts +2 -0
  24. package/dist/examples/embed-multimodal.js +80 -0
  25. package/dist/examples/embed-p2p.js +2 -2
  26. package/dist/examples/finetune/llamacpp-finetune.js +9 -4
  27. package/dist/examples/image/megaloc.d.ts +2 -0
  28. package/dist/examples/image/megaloc.js +37 -0
  29. package/dist/examples/llamacpp-multimodal.js +21 -1
  30. package/dist/examples/mcp-websearch.js +1 -1
  31. package/dist/examples/plugins.js +1 -1
  32. package/dist/examples/rag/rag-hyperdb/pipeline.js +2 -2
  33. package/dist/examples/rag/rag-turbovec.js +2 -2
  34. package/dist/examples/rerank.d.ts +2 -0
  35. package/dist/examples/rerank.js +33 -0
  36. package/dist/examples/sessions.d.ts +17 -0
  37. package/dist/examples/sessions.js +63 -0
  38. package/dist/examples/tools/llamacpp-deferred-tools.js +12 -8
  39. package/dist/examples/tools/llamacpp-tools-auto-dialect.js +4 -3
  40. package/dist/examples/tools/llamacpp-tools-gemma4.js +3 -3
  41. package/dist/examples/tools/llamacpp-tools-qwen35.js +4 -3
  42. package/dist/scripts/check-resource-collectors-packaging.js +7 -6
  43. package/dist/scripts/contract/build-contract.d.ts +2 -1
  44. package/dist/scripts/contract/build-contract.js +13 -1
  45. package/dist/scripts/contract/constants-registry.d.ts +19 -0
  46. package/dist/scripts/contract/constants-registry.js +9 -2
  47. package/dist/scripts/contract/method-shapes.d.ts +5 -0
  48. package/dist/scripts/contract/method-shapes.js +5 -0
  49. package/dist/scripts/enforce-inference-versions.d.ts +1 -1
  50. package/dist/scripts/enforce-inference-versions.js +28 -18
  51. package/dist/scripts/generate-bare-kit-runtimes.js +101 -69
  52. package/dist/src/client/api/assess-model-fit.d.ts +17 -17
  53. package/dist/src/client/api/assess-model-fit.js +19 -32
  54. package/dist/src/client/api/audio-edit.d.ts +5 -3
  55. package/dist/src/client/api/audio-edit.js +7 -5
  56. package/dist/src/client/api/audio-gen-result.js +136 -47
  57. package/dist/src/client/api/audio-gen.d.ts +40 -3
  58. package/dist/src/client/api/audio-gen.js +42 -5
  59. package/dist/src/client/api/audio-understand.d.ts +2 -2
  60. package/dist/src/client/api/audio-understand.js +4 -4
  61. package/dist/src/client/api/batch-completion.js +4 -3
  62. package/dist/src/client/api/bci-transcribe.js +3 -3
  63. package/dist/src/client/api/calc-tokens.d.ts +33 -0
  64. package/dist/src/client/api/calc-tokens.js +44 -0
  65. package/dist/src/client/api/cancel.d.ts +2 -2
  66. package/dist/src/client/api/cancel.js +3 -3
  67. package/dist/src/client/api/classify.d.ts +12 -5
  68. package/dist/src/client/api/classify.js +18 -6
  69. package/dist/src/client/api/client-request-id.d.ts +0 -15
  70. package/dist/src/client/api/client-request-id.js +15 -12
  71. package/dist/src/client/api/completion-stream.d.ts +9 -1
  72. package/dist/src/client/api/completion-stream.js +14 -3
  73. package/dist/src/client/api/decide.d.ts +1 -1
  74. package/dist/src/client/api/decide.js +3 -3
  75. package/dist/src/client/api/delete-cache.d.ts +9 -2
  76. package/dist/src/client/api/delete-cache.js +14 -4
  77. package/dist/src/client/api/diffusion.d.ts +38 -5
  78. package/dist/src/client/api/diffusion.js +42 -7
  79. package/dist/src/client/api/download-asset.js +3 -3
  80. package/dist/src/client/api/embed.d.ts +82 -29
  81. package/dist/src/client/api/embed.js +66 -21
  82. package/dist/src/client/api/finetune.js +3 -3
  83. package/dist/src/client/api/get-loaded-model-info.js +1 -1
  84. package/dist/src/client/api/get-model-info.d.ts +2 -2
  85. package/dist/src/client/api/get-model-info.js +3 -3
  86. package/dist/src/client/api/get-system-resources.d.ts +2 -2
  87. package/dist/src/client/api/get-system-resources.js +3 -3
  88. package/dist/src/client/api/heartbeat.d.ts +3 -2
  89. package/dist/src/client/api/heartbeat.js +4 -3
  90. package/dist/src/client/api/index.d.ts +8 -6
  91. package/dist/src/client/api/index.js +6 -4
  92. package/dist/src/client/api/invoke-plugin.d.ts +8 -2
  93. package/dist/src/client/api/invoke-plugin.js +18 -3
  94. package/dist/src/client/api/load-model.js +9 -6
  95. package/dist/src/client/api/logging-stream.d.ts +11 -2
  96. package/dist/src/client/api/logging-stream.js +12 -3
  97. package/dist/src/client/api/ocr.d.ts +5 -3
  98. package/dist/src/client/api/ocr.js +45 -22
  99. package/dist/src/client/api/rag.d.ts +1 -1
  100. package/dist/src/client/api/rag.js +3 -3
  101. package/dist/src/client/api/registry.d.ts +19 -8
  102. package/dist/src/client/api/registry.js +43 -17
  103. package/dist/src/client/api/rerank.d.ts +22 -0
  104. package/dist/src/client/api/rerank.js +39 -0
  105. package/dist/src/client/api/resume.d.ts +2 -1
  106. package/dist/src/client/api/resume.js +3 -3
  107. package/dist/src/client/api/rpc-server.d.ts +4 -4
  108. package/dist/src/client/api/rpc-server.js +12 -12
  109. package/dist/src/client/api/state.d.ts +2 -2
  110. package/dist/src/client/api/state.js +3 -3
  111. package/dist/src/client/api/subscribe-logs.d.ts +2 -2
  112. package/dist/src/client/api/subscribe-logs.js +4 -3
  113. package/dist/src/client/api/suspend.d.ts +4 -2
  114. package/dist/src/client/api/suspend.js +5 -4
  115. package/dist/src/client/api/text-to-speech.d.ts +14 -5
  116. package/dist/src/client/api/text-to-speech.js +86 -32
  117. package/dist/src/client/api/transcribe.d.ts +7 -3
  118. package/dist/src/client/api/transcribe.js +9 -3
  119. package/dist/src/client/api/translate.js +2 -2
  120. package/dist/src/client/api/unload-model.d.ts +19 -7
  121. package/dist/src/client/api/unload-model.js +31 -13
  122. package/dist/src/client/api/upscale.d.ts +6 -3
  123. package/dist/src/client/api/upscale.js +10 -5
  124. package/dist/src/client/api/vector-index.d.ts +51 -17
  125. package/dist/src/client/api/vector-index.js +101 -49
  126. package/dist/src/client/api/video-request.d.ts +5 -5
  127. package/dist/src/client/api/video.d.ts +3 -2
  128. package/dist/src/client/api/video.js +6 -5
  129. package/dist/src/client/api/vla.d.ts +9 -5
  130. package/dist/src/client/api/vla.js +17 -8
  131. package/dist/src/client/api/world-result.js +2 -2
  132. package/dist/src/client/api/world.d.ts +6 -6
  133. package/dist/src/client/api/world.js +6 -6
  134. package/dist/src/client/init-hooks.d.ts +6 -4
  135. package/dist/src/client/init-hooks.js +12 -15
  136. package/dist/src/client/logging-stream-registry.d.ts +6 -3
  137. package/dist/src/client/logging-stream-registry.js +36 -14
  138. package/dist/src/client/plugins-factory.d.ts +1 -1
  139. package/dist/src/client/plugins-factory.js +1 -1
  140. package/dist/src/client/rpc/bundle-stamp.d.ts +29 -0
  141. package/dist/src/client/rpc/bundle-stamp.js +75 -0
  142. package/dist/src/client/rpc/expo-home-dir.d.ts +2 -0
  143. package/dist/src/client/rpc/expo-home-dir.js +4 -0
  144. package/dist/src/client/rpc/expo-rpc-client.js +192 -138
  145. package/dist/src/client/rpc/node-rpc-client.js +324 -426
  146. package/dist/src/client/rpc/pear-runtime.d.ts +12 -0
  147. package/dist/src/client/rpc/pear-runtime.js +12 -0
  148. package/dist/src/client/rpc/rpc-client.d.ts +6 -4
  149. package/dist/src/client/rpc/rpc-client.js +168 -31
  150. package/dist/src/client/rpc/rpc-error.js +24 -2
  151. package/dist/src/client/rpc/unbundled-harness.d.ts +16 -0
  152. package/dist/src/client/rpc/unbundled-harness.js +27 -0
  153. package/dist/src/client/rpc/worker-failure.d.ts +27 -0
  154. package/dist/src/client/rpc/worker-failure.js +61 -0
  155. package/dist/src/client/session-registry.d.ts +75 -0
  156. package/dist/src/client/session-registry.js +149 -0
  157. package/dist/src/client/session.d.ts +125 -0
  158. package/dist/src/client/session.js +148 -0
  159. package/dist/src/commands/bundle/addons.d.ts +14 -0
  160. package/dist/src/commands/bundle/addons.js +65 -0
  161. package/dist/src/commands/bundle/constants.js +2 -0
  162. package/dist/src/commands/bundle/entry-gen.d.ts +1 -5
  163. package/dist/src/commands/bundle/entry-gen.js +18 -51
  164. package/dist/src/commands/bundle/index.d.ts +60 -14
  165. package/dist/src/commands/bundle/index.js +350 -126
  166. package/dist/src/commands/bundle/link.d.ts +58 -0
  167. package/dist/src/commands/bundle/link.js +135 -0
  168. package/dist/src/commands/bundle/node-support.d.ts +13 -0
  169. package/dist/src/commands/bundle/node-support.js +23 -0
  170. package/dist/src/commands/bundle/plugins.js +1 -1
  171. package/dist/src/commands/bundle/read-bundle.d.ts +9 -0
  172. package/dist/src/commands/bundle/read-bundle.js +27 -0
  173. package/dist/src/commands/bundle/resolve-sdk-import.d.ts +6 -0
  174. package/dist/src/commands/bundle/resolve-sdk-import.js +24 -28
  175. package/dist/src/commands/bundle/scratch.d.ts +35 -0
  176. package/dist/src/commands/bundle/scratch.js +178 -0
  177. package/dist/src/commands/bundle/targets.d.ts +14 -0
  178. package/dist/src/commands/bundle/targets.js +37 -0
  179. package/dist/src/commands/host-prebuilds/index.d.ts +8 -7
  180. package/dist/src/commands/host-prebuilds/index.js +13 -11
  181. package/dist/src/commands/index.d.ts +13 -12
  182. package/dist/src/commands/index.js +7 -7
  183. package/dist/src/commands/verify/abi.d.ts +21 -15
  184. package/dist/src/commands/verify/abi.js +12 -80
  185. package/dist/src/commands/verify/bare-kit-runtime.d.ts +20 -22
  186. package/dist/src/commands/verify/bare-kit-runtime.js +102 -150
  187. package/dist/src/commands/verify/bare-kit-runtimes.d.ts +10 -9
  188. package/dist/src/commands/verify/bare-kit-runtimes.js +21 -21
  189. package/dist/src/commands/verify/bundle-source.d.ts +1 -1
  190. package/dist/src/commands/verify/bundle-source.js +27 -23
  191. package/dist/src/commands/verify/engines-advice.d.ts +6 -12
  192. package/dist/src/commands/verify/engines-advice.js +9 -56
  193. package/dist/src/commands/verify/index.d.ts +34 -33
  194. package/dist/src/commands/verify/index.js +96 -204
  195. package/dist/src/commands/verify/node-modules-source.d.ts +1 -1
  196. package/dist/src/commands/verify/node-modules-source.js +1 -1
  197. package/dist/src/commands/verify/prebuilds.d.ts +4 -6
  198. package/dist/src/commands/verify/prebuilds.js +3 -4
  199. package/dist/src/commands/verify/resource-collectors.d.ts +0 -6
  200. package/dist/src/commands/verify/resource-collectors.js +3 -52
  201. package/dist/src/commands/verify/sidecar-runtime.d.ts +6 -0
  202. package/dist/src/commands/verify/sidecar-runtime.js +51 -0
  203. package/dist/src/expo/plugins/get-project-root.d.ts +1 -1
  204. package/dist/src/expo/plugins/withAndroidArchitecture.d.ts +1 -1
  205. package/dist/src/expo/plugins/withAndroidNdkVersion.d.ts +1 -1
  206. package/dist/src/expo/plugins/withAndroidNdkVersion.js +16 -10
  207. package/dist/src/expo/plugins/withBareKit.d.ts +59 -0
  208. package/dist/src/expo/plugins/withBareKit.js +204 -0
  209. package/dist/src/expo/plugins/withDeviceInfo.d.ts +1 -1
  210. package/dist/src/expo/plugins/withDeviceInfo.js +19 -5
  211. package/dist/src/expo/plugins/withMobileBundle.d.ts +5 -12
  212. package/dist/src/expo/plugins/withMobileBundle.js +38 -59
  213. package/dist/src/expo/plugins/withOpenCL.d.ts +1 -1
  214. package/dist/src/expo/plugins/withQvacSDK.d.ts +2 -1
  215. package/dist/src/expo/plugins/withQvacSDK.js +3 -0
  216. package/dist/src/index.d.ts +6 -5
  217. package/dist/src/index.js +10 -14
  218. package/dist/src/logging/addon.d.ts +1 -1
  219. package/dist/src/logging/base-logger.js +1 -1
  220. package/dist/src/logging/utils.js +11 -3
  221. package/dist/src/plugin-ids.d.ts +3 -1
  222. package/dist/src/plugin-ids.js +4 -0
  223. package/dist/src/schemas/public.d.ts +1 -1
  224. package/dist/src/schemas/public.js +1 -1
  225. package/dist/src/schemas/sdk-errors-client.d.ts +5 -6
  226. package/dist/src/schemas/sdk-errors-client.js +30 -36
  227. package/dist/src/schemas/sdk-errors-server.d.ts +12 -1
  228. package/dist/src/schemas/sdk-errors-server.js +65 -2
  229. package/dist/src/server/rpc/rpc-utils.d.ts +26 -0
  230. package/dist/src/server/rpc/rpc-utils.js +41 -0
  231. package/dist/src/utils/aggregate-events.d.ts +1 -1
  232. package/dist/src/utils/aggregate-events.js +2 -2
  233. package/dist/src/utils/errors-client.d.ts +37 -29
  234. package/dist/src/utils/errors-client.js +50 -41
  235. package/dist/src/utils/errors-server.d.ts +65 -1
  236. package/dist/src/utils/errors-server.js +122 -1
  237. package/dist/src/utils/mcp-adapter.d.ts +1 -1
  238. package/dist/src/utils/mcp-adapter.js +1 -1
  239. package/dist/src/utils/tool-helpers.js +1 -1
  240. package/dist/src/utils/withTimeout.js +34 -17
  241. package/dist/src/worker/handle-request.js +26 -21
  242. package/dist/src/worker/handler-utils.d.ts +2 -6
  243. package/dist/src/worker/handler-utils.js +2 -20
  244. package/dist/src/worker/index.d.ts +2 -4
  245. package/dist/src/worker/index.js +24 -34
  246. package/dist/src/worker/plugins/ggml-image-embedding.d.ts +2 -0
  247. package/dist/src/worker/plugins/ggml-image-embedding.js +1 -0
  248. package/dist/src/worker/plugins/llamacpp-rerank.d.ts +2 -0
  249. package/dist/src/worker/plugins/llamacpp-rerank.js +1 -0
  250. package/dist/src/worker/shim.d.ts +4 -0
  251. package/dist/src/worker/shim.js +7 -0
  252. package/dist/src/worker/start.d.ts +9 -0
  253. package/dist/src/worker/start.js +16 -0
  254. package/dist/src/worker/unbundled-shim.d.ts +2 -0
  255. package/dist/src/worker/unbundled-shim.js +4 -0
  256. package/docs/system-resources-support-matrix.md +27 -19
  257. package/package.json +91 -74
  258. package/src/electron-forge/index.cjs +65 -91
  259. package/dist/examples/model-fit-ui/server.d.ts +0 -24
  260. package/dist/examples/model-fit-ui/server.js +0 -221
  261. package/dist/src/client/rpc/ensure-worker-ready.d.ts +0 -9
  262. package/dist/src/client/rpc/ensure-worker-ready.js +0 -17
  263. package/dist/src/client/rpc/init-timeout.d.ts +0 -26
  264. package/dist/src/client/rpc/init-timeout.js +0 -25
  265. package/dist/src/client/rpc/worker-startup-error.d.ts +0 -12
  266. package/dist/src/client/rpc/worker-startup-error.js +0 -13
  267. package/dist/src/commands/bundle/bare-pack.d.ts +0 -21
  268. package/dist/src/commands/bundle/bare-pack.js +0 -77
  269. package/dist/src/commands/bundle/manifest.d.ts +0 -25
  270. package/dist/src/commands/bundle/manifest.js +0 -192
  271. package/dist/src/expo/plugins/link-addons.d.ts +0 -18
  272. package/dist/src/expo/plugins/link-addons.js +0 -65
  273. package/dist/src/pear/pre.d.ts +0 -21
  274. package/dist/src/pear/pre.js +0 -448
  275. package/dist/src/server/env.d.ts +0 -22
  276. package/dist/src/server/env.js +0 -59
  277. package/dist/src/server/utils/qvac-paths.d.ts +0 -2
  278. package/dist/src/server/utils/qvac-paths.js +0 -5
  279. package/dist/src/server/utils/worker-lock.d.ts +0 -3
  280. package/dist/src/server/utils/worker-lock.js +0 -67
  281. package/dist/src/worker/create-server.d.ts +0 -8
  282. package/dist/src/worker/create-server.js +0 -51
  283. package/dist/src/worker/lifecycle.d.ts +0 -23
  284. package/dist/src/worker/lifecycle.js +0 -149
@@ -0,0 +1,12 @@
1
+ /** The part of a `pear-runtime` instance the worker harness uses. */
2
+ export interface PearRuntime {
3
+ run(path: string, args?: string[], opts?: object): unknown;
4
+ }
5
+ /**
6
+ * Starts the SDK worker through `pear` from then on, from the `pear-runtime`
7
+ * bundle that `qvac bundle sdk --target pear-runtime` writes. Call it before
8
+ * the first SDK call.
9
+ */
10
+ export declare function usePearRuntime(pear: PearRuntime): void;
11
+ export declare function getPearRuntime(): PearRuntime | null;
12
+ //# sourceMappingURL=pear-runtime.d.ts.map
@@ -0,0 +1,12 @@
1
+ let pearRuntime = null;
2
+ /**
3
+ * Starts the SDK worker through `pear` from then on, from the `pear-runtime`
4
+ * bundle that `qvac bundle sdk --target pear-runtime` writes. Call it before
5
+ * the first SDK call.
6
+ */
7
+ export function usePearRuntime(pear) {
8
+ pearRuntime = pear;
9
+ }
10
+ export function getPearRuntime() {
11
+ return pearRuntime;
12
+ }
@@ -1,7 +1,8 @@
1
1
  import type RPC from 'bare-rpc';
2
- import { type Request, type Response, type RPCOptions } from '@qvac/inference/surface';
3
- export declare function send<T extends Request>(request: T, options?: RPCOptions, rpc?: RPC): Promise<Response>;
4
- export declare function stream<T extends Request>(request: T, options?: RPCOptions, rpc?: RPC): AsyncGenerator<Response>;
2
+ import { type AbortableRPCOptions, type Request, type Response, type RPCOptions } from '@qvac/inference/surface';
3
+ export declare function send<T extends Request>(input: T, options?: RPCOptions, rpc?: RPC): Promise<Response>;
4
+ /** `options.signal` ends the stream and cancels the request on the worker. */
5
+ export declare function stream<T extends Request>(input: T, options?: AbortableRPCOptions, rpc?: RPC): AsyncGenerator<Response>;
5
6
  export interface DuplexWritable {
6
7
  write(chunk: Uint8Array): void;
7
8
  end(): void;
@@ -14,6 +15,7 @@ export interface DuplexSession {
14
15
  requestStream: DuplexWritable;
15
16
  responseStream: DuplexReadable;
16
17
  }
17
- export declare function duplex<T extends Request>(request: T, options?: RPCOptions): Promise<DuplexSession>;
18
+ export declare function duplex<T extends Request>(input: T, options?: RPCOptions): Promise<DuplexSession>;
19
+ /** Owner shutdown. Every session handle opened on this runtime ends with it. */
18
20
  export declare function close(): Promise<void>;
19
21
  //# sourceMappingURL=rpc-client.d.ts.map
@@ -1,9 +1,10 @@
1
- import { requestSchema, responseSchema, PROFILING_TRAILER_KEY, nowMs, shouldProfile, shouldIncludeServerBreakdown, shouldIncludeResourceGauges, generateId as createProfileId, createProfilingMeta, createProfilingDisabledMeta, injectProfilingMetaIntoObject, extractProfilingMeta, stripProfilingMeta, recordFailure } from '@qvac/inference/surface';
1
+ import { requestSchema, responseSchema, PROFILING_TRAILER_KEY, nowMs, shouldProfile, shouldIncludeServerBreakdown, shouldIncludeResourceGauges, generateId as createProfileId, createProfilingMeta, createProfilingDisabledMeta, injectProfilingMetaIntoObject, extractProfilingMeta, stripProfilingMeta, recordFailure, untilAborted } from '@qvac/inference/surface';
2
2
  import { reconstructError } from './rpc-error.js';
3
3
  import { withTimeout, withTimeoutStream, OperationTimeoutError } from '../../utils/withTimeout.js';
4
4
  import { getClientLogger, summarizeRequest } from '../../logging/index.js';
5
5
  import { getRPC, close as closeRPC, createDuplexSession, getWorkerLifeSignal, notifyChannelClosed } from '#rpc';
6
6
  import { WorkerCrashedError, RequestValidationFailedError } from '../../utils/errors-client.js';
7
+ import { assertOwnerModelUsable, assertSessionUsable, awaitSessionUsable, endAllSessions, sessionOf } from '../../client/session-registry.js';
7
8
  import { formatZodError } from '../../utils/zod-error.js';
8
9
  import { z } from 'zod';
9
10
  import { createClientTimings, createClientStreamTimings, recordClientEvents, recordClientStreamEvents, cacheConnectionTime, flushConnectionTime, resetConnectionTracking } from './profiling/index.js';
@@ -30,7 +31,19 @@ function pinpointRequestError(request, fallback) {
30
31
  if (memberDiscriminator(option) !== type)
31
32
  continue;
32
33
  const result = option.safeParse(request);
33
- return result.success ? fallback : result.error;
34
+ if (result.success)
35
+ return fallback;
36
+ // A method can itself accept several input shapes. Preserve field errors
37
+ // shared by every shape, such as an invalid modelId on either embed arm.
38
+ const union = result.error.issues.find((issue) => issue.code === 'invalid_union' && issue.path.length === 0);
39
+ if (union?.code === 'invalid_union' && union.errors.length > 0) {
40
+ const shared = union.errors[0].filter((issue) => union.errors.every((branch) => branch.some((other) => other.code === issue.code &&
41
+ other.message === issue.message &&
42
+ JSON.stringify(other.path) === JSON.stringify(issue.path))));
43
+ if (shared.length > 0)
44
+ return new z.ZodError(shared);
45
+ }
46
+ return result.error;
34
47
  }
35
48
  return fallback;
36
49
  }
@@ -49,7 +62,7 @@ function parseRequest(request) {
49
62
  // bare-rpc's `_onerror`/`_onclose` reject a pending reply or destroy a
50
63
  // stream with its own generic `RPCError` (e.g. `CHANNEL_CLOSED`) as soon
51
64
  // as the channel dies, which can beat the SDK's own crash detection
52
- // (driven by the child process's `'exit'` event). Since `req.reply()` and
65
+ // (driven by the worker IPC's `'close'` event). Since `req.reply()` and
53
66
  // response streams never reject with an application-level error — those
54
67
  // always arrive as a resolved payload handled by `checkAndThrowError` —
55
68
  // any rejection reaching these helpers other than our own `withTimeout`
@@ -60,7 +73,7 @@ function parseRequest(request) {
60
73
  // When we win that race, the life signal hasn't aborted yet, so nothing
61
74
  // has invalidated the cached RPC connection — the next call would reuse
62
75
  // the same dead channel and fail again. `notifyChannelClosed()` tears it
63
- // down eagerly instead of waiting on the slow exit event; it's a no-op
76
+ // down eagerly instead of waiting on the `'close'` event; it's a no-op
64
77
  // once the signal is already aborted, so this is safe to call every time.
65
78
  function lifeSignalAbortError(signal) {
66
79
  if (!signal.aborted)
@@ -70,15 +83,15 @@ function lifeSignalAbortError(signal) {
70
83
  return new WorkerCrashedError(null, null);
71
84
  }
72
85
  async function awaitWithLifeSignal(p) {
73
- // Null on Bare-direct / Expo and pre-spawn.
86
+ // Null on Bare-direct and before the worker starts.
74
87
  const lifeSignal = getWorkerLifeSignal();
75
88
  if (!lifeSignal)
76
89
  return p;
77
90
  if (lifeSignal.aborted) {
78
- // `p` (e.g. `req.reply()`) may already be a rejected promise — bare-rpc
79
- // 1.3.8 rejects synchronously once its channel is closed. We're
80
- // discarding it in favour of the life signal's classification, so give
81
- // it a handler or its rejection surfaces as an unhandled rejection.
91
+ // `p` (e.g. `req.reply()`) may already be rejected, since bare-rpc rejects
92
+ // once its channel is closed. It is discarded in favour of the life
93
+ // signal's classification, so it gets a handler or its rejection surfaces
94
+ // as an unhandled rejection.
82
95
  p.catch(() => { });
83
96
  throw lifeSignalAbortError(lifeSignal);
84
97
  }
@@ -108,7 +121,7 @@ async function awaitWithLifeSignal(p) {
108
121
  });
109
122
  }
110
123
  async function* iterateWithLifeSignal(source) {
111
- // Null on Bare-direct / Expo and pre-spawn.
124
+ // Null on Bare-direct and before the worker starts.
112
125
  const lifeSignal = getWorkerLifeSignal();
113
126
  if (!lifeSignal) {
114
127
  yield* source;
@@ -157,6 +170,32 @@ async function* iterateWithLifeSignal(source) {
157
170
  void source.return?.(undefined).catch(() => { });
158
171
  }
159
172
  }
173
+ /** A reply that times out cancels the request, so the worker stops its work too. */
174
+ async function replyWithin(req, timeout) {
175
+ try {
176
+ return await withTimeout(req.reply('utf-8'), timeout);
177
+ }
178
+ catch (error) {
179
+ if (error instanceof OperationTimeoutError)
180
+ req.cancel(error);
181
+ throw error;
182
+ }
183
+ }
184
+ // Destroying the response stream before the reply ends cancels the request,
185
+ // and the worker cancels its work. Registered before `untilAborted`, so the
186
+ // destroyed stream's late rejection is dropped there instead of reaching
187
+ // `iterateWithLifeSignal` as a worker crash.
188
+ function destroyOnAbort(responseStream, signal) {
189
+ if (!signal)
190
+ return () => { };
191
+ const onAbort = () => responseStream.destroy();
192
+ if (signal.aborted) {
193
+ onAbort();
194
+ return () => { };
195
+ }
196
+ signal.addEventListener('abort', onAbort, { once: true });
197
+ return () => signal.removeEventListener('abort', onAbort);
198
+ }
160
199
  function checkAndThrowError(response) {
161
200
  if (response.type === 'error') {
162
201
  // Use the typed-error reconstructor map in `rpc-error.ts` so the
@@ -172,20 +211,84 @@ function invalidateRPCInstance() {
172
211
  firstConnectionPending = true;
173
212
  resetConnectionTracking();
174
213
  }
214
+ // The worker died under the sessions: every handle this process opened is
215
+ // now invalid, and later calls on them fail locally.
216
+ function onWorkerLost() {
217
+ invalidateRPCInstance();
218
+ endAllSessions('crashed');
219
+ }
220
+ /**
221
+ * A request made through a session handle carries the session id. The call
222
+ * waits for the session's `openSession` round trip, so a handle is usable the
223
+ * moment `openSession()` returns, and is refused locally once the session is
224
+ * closed or lost. The handle's id replaces any id the request carries, so a
225
+ * caller cannot send a request as another session; only the session methods
226
+ * themselves (`openSession`, `closeSession`) name the session they act on.
227
+ *
228
+ * An owner request is refused the same way when it names a model the owner
229
+ * loaded on a runtime that has since stopped.
230
+ *
231
+ * An owner request is stamped without a promise: it must claim the worker
232
+ * start in the tick it is made, so a `close()` called right after it sees
233
+ * that start and shuts it down.
234
+ */
235
+ function stampSession(request, options) {
236
+ const sessionId = options?.sessionId;
237
+ const target = request;
238
+ if (sessionId === undefined) {
239
+ assertOwnerModelUsable(request);
240
+ return request;
241
+ }
242
+ return stampSessionRequest(request, sessionId, options, target);
243
+ }
244
+ async function stampSessionRequest(request, sessionId, options, target) {
245
+ // A bare `sessionId` with no handle behind it has no local state; the
246
+ // worker decides whether that session is open.
247
+ const record = sessionOf(options);
248
+ if (record)
249
+ await awaitSessionUsable(record, target);
250
+ if (request.type === 'openSession' || request.type === 'closeSession')
251
+ return request;
252
+ return { ...request, sessionId };
253
+ }
254
+ function isPromise(value) {
255
+ return value instanceof Promise;
256
+ }
257
+ /**
258
+ * The owner's `close()` can end a session while its request waits for the
259
+ * stamp. Each call checks again right before acquiring the connection, with
260
+ * no await in between, so such a call fails locally instead of starting a
261
+ * worker that never opened the session.
262
+ */
263
+ function assertStampedSessionUsable(request, options) {
264
+ const record = sessionOf(options);
265
+ if (record)
266
+ assertSessionUsable(record, request);
267
+ }
175
268
  async function getRPCInstance() {
176
269
  if (rpcInstance)
177
270
  return { rpc: await rpcInstance };
178
271
  const connectionStart = firstConnectionPending ? nowMs() : null;
179
- rpcInstance = getRPC();
180
- const rpc = await rpcInstance;
272
+ const connecting = getRPC();
273
+ rpcInstance = connecting;
274
+ let rpc;
275
+ try {
276
+ rpc = await connecting;
277
+ }
278
+ catch (error) {
279
+ // A failed start is not cached, so the next call starts a new worker.
280
+ if (rpcInstance === connecting)
281
+ rpcInstance = null;
282
+ throw error;
283
+ }
181
284
  // Drop the cache on worker death so the next call respawns and
182
285
  // records connectionMs again as a "first" connection.
183
286
  const lifeSignal = getWorkerLifeSignal();
184
287
  if (lifeSignal?.aborted) {
185
- invalidateRPCInstance();
288
+ onWorkerLost();
186
289
  }
187
290
  else if (lifeSignal) {
188
- lifeSignal.addEventListener('abort', invalidateRPCInstance, {
291
+ lifeSignal.addEventListener('abort', onWorkerLost, {
189
292
  once: true
190
293
  });
191
294
  }
@@ -207,7 +310,10 @@ async function prepareRPCContext(requestType, perCallProfiling, rpc) {
207
310
  }
208
311
  return { rpc: rpcResult.rpc, profilingEnabled, signalDisable };
209
312
  }
210
- export async function send(request, options, rpc) {
313
+ export async function send(input, options, rpc) {
314
+ const stamped = stampSession(input, options);
315
+ const request = isPromise(stamped) ? await stamped : stamped;
316
+ assertStampedSessionUsable(request, options);
211
317
  const ctx = await prepareRPCContext(request.type, options?.profiling, rpc);
212
318
  if (!ctx.profilingEnabled) {
213
319
  return sendBase(request, ctx.rpc, options, ctx.signalDisable);
@@ -223,7 +329,7 @@ async function sendBase(request, rpc, options, signalDisable = false) {
223
329
  : parsedRequest;
224
330
  const payload = JSON.stringify(payloadObj);
225
331
  req.send(payload, 'utf-8');
226
- const response = await awaitWithLifeSignal(withTimeout(req.reply('utf-8'), options?.timeout));
332
+ const response = await awaitWithLifeSignal(replyWithin(req, options?.timeout));
227
333
  const resPayload = responseSchema.parse(JSON.parse(response?.toString() || '{}'));
228
334
  logger.debug('ResPayload', { type: resPayload.type });
229
335
  checkAndThrowError(resPayload);
@@ -248,7 +354,7 @@ async function sendProfiled(request, rpc, options) {
248
354
  timings.requestStringifyMs = nowMs() - stringifyStart;
249
355
  timings.sendStart = nowMs();
250
356
  req.send(payload, 'utf-8');
251
- const response = await awaitWithLifeSignal(withTimeout(req.reply('utf-8'), options?.timeout));
357
+ const response = await awaitWithLifeSignal(replyWithin(req, options?.timeout));
252
358
  timings.firstResponseAt = nowMs();
253
359
  const parseStart = nowMs();
254
360
  const rawParsed = JSON.parse(response?.toString() || '{}');
@@ -277,8 +383,16 @@ async function sendProfiled(request, rpc, options) {
277
383
  throw error;
278
384
  }
279
385
  }
280
- export async function* stream(request, options = {}, rpc) {
386
+ /** `options.signal` ends the stream and cancels the request on the worker. */
387
+ export async function* stream(input, options = {}, rpc) {
388
+ if (options.signal?.aborted)
389
+ return;
390
+ const stamped = stampSession(input, options);
391
+ const request = isPromise(stamped) ? await stamped : stamped;
392
+ assertStampedSessionUsable(request, options);
281
393
  const ctx = await prepareRPCContext(request.type, options?.profiling, rpc);
394
+ if (options.signal?.aborted)
395
+ return;
282
396
  if (!ctx.profilingEnabled) {
283
397
  yield* streamBase(request, ctx.rpc, options, ctx.signalDisable);
284
398
  return;
@@ -294,6 +408,7 @@ async function* streamBase(request, rpc, options = {}, signalDisable = false) {
294
408
  : parsedRequest;
295
409
  req.send(JSON.stringify(payloadObj), 'utf-8');
296
410
  const responseStream = req.createResponseStream({ encoding: 'utf-8' });
411
+ const stopDestroyOnAbort = destroyOnAbort(responseStream, options.signal);
297
412
  let buffer = '';
298
413
  async function* processStream() {
299
414
  for await (const chunk of responseStream) {
@@ -301,19 +416,28 @@ async function* streamBase(request, rpc, options = {}, signalDisable = false) {
301
416
  }
302
417
  }
303
418
  const streamWithTimeout = withTimeoutStream(processStream(), options?.timeout);
304
- for await (const chunk of iterateWithLifeSignal(streamWithTimeout)) {
305
- buffer += chunk.toString();
306
- // Process complete lines (newline-delimited JSON)
307
- const lines = buffer.split('\n');
308
- buffer = lines.pop() || ''; // Keep incomplete line in buffer
309
- for (const line of lines) {
310
- if (line.trim()) {
311
- const response = responseSchema.parse(JSON.parse(line));
312
- checkAndThrowError(response);
313
- yield response;
419
+ try {
420
+ for await (const chunk of iterateWithLifeSignal(untilAborted(streamWithTimeout, options.signal))) {
421
+ buffer += chunk.toString();
422
+ // Process complete lines (newline-delimited JSON)
423
+ const lines = buffer.split('\n');
424
+ buffer = lines.pop() || ''; // Keep incomplete line in buffer
425
+ for (const line of lines) {
426
+ // Aborted mid-chunk.
427
+ if (options.signal?.aborted)
428
+ return;
429
+ if (line.trim()) {
430
+ const response = responseSchema.parse(JSON.parse(line));
431
+ checkAndThrowError(response);
432
+ yield response;
433
+ }
314
434
  }
315
435
  }
316
436
  }
437
+ finally {
438
+ stopDestroyOnAbort();
439
+ responseStream.destroy();
440
+ }
317
441
  }
318
442
  async function* streamProfiled(request, rpc, options = {}) {
319
443
  const requestType = request.type;
@@ -322,6 +446,8 @@ async function* streamProfiled(request, rpc, options = {}) {
322
446
  const includeResources = shouldIncludeResourceGauges(options?.profiling);
323
447
  const timings = createClientStreamTimings(profileId, requestType);
324
448
  let profilingMeta = undefined;
449
+ let stopDestroyOnAbort = () => { };
450
+ let responseStream;
325
451
  try {
326
452
  const zodStart = nowMs();
327
453
  const parsedRequest = parseRequest(request);
@@ -335,7 +461,8 @@ async function* streamProfiled(request, rpc, options = {}) {
335
461
  timings.requestStringifyMs = nowMs() - stringifyStart;
336
462
  timings.sendStart = nowMs();
337
463
  req.send(payload, 'utf-8');
338
- const responseStream = req.createResponseStream({ encoding: 'utf-8' });
464
+ responseStream = req.createResponseStream({ encoding: 'utf-8' });
465
+ stopDestroyOnAbort = destroyOnAbort(responseStream, options.signal);
339
466
  let buffer = '';
340
467
  async function* processStream() {
341
468
  for await (const chunk of responseStream) {
@@ -343,7 +470,7 @@ async function* streamProfiled(request, rpc, options = {}) {
343
470
  }
344
471
  }
345
472
  const streamWithTimeout = withTimeoutStream(processStream(), options?.timeout);
346
- for await (const chunk of iterateWithLifeSignal(streamWithTimeout)) {
473
+ for await (const chunk of iterateWithLifeSignal(untilAborted(streamWithTimeout, options.signal))) {
347
474
  const chunkTime = nowMs();
348
475
  if (timings.firstChunkAt === undefined) {
349
476
  timings.firstChunkAt = chunkTime;
@@ -353,6 +480,9 @@ async function* streamProfiled(request, rpc, options = {}) {
353
480
  const lines = buffer.split('\n');
354
481
  buffer = lines.pop() || '';
355
482
  for (const line of lines) {
483
+ // Aborted mid-chunk.
484
+ if (options.signal?.aborted)
485
+ return;
356
486
  if (line.trim()) {
357
487
  const rawParsed = JSON.parse(line);
358
488
  const chunkMeta = extractProfilingMeta(rawParsed);
@@ -383,6 +513,8 @@ async function* streamProfiled(request, rpc, options = {}) {
383
513
  throw error;
384
514
  }
385
515
  finally {
516
+ stopDestroyOnAbort();
517
+ responseStream?.destroy();
386
518
  if (timings.chunkCount > 0) {
387
519
  timings.requestEnd = nowMs();
388
520
  recordClientStreamEvents(timings, profilingMeta);
@@ -410,7 +542,10 @@ function withLifeSignalDuplex(raw) {
410
542
  }
411
543
  };
412
544
  }
413
- export async function duplex(request, options) {
545
+ export async function duplex(input, options) {
546
+ const stamped = stampSession(input, options);
547
+ const request = isPromise(stamped) ? await stamped : stamped;
548
+ assertStampedSessionUsable(request, options);
414
549
  const ctx = await prepareRPCContext(request.type, options?.profiling);
415
550
  if (!ctx.profilingEnabled) {
416
551
  return duplexBase(request, ctx.signalDisable, options?.timeout);
@@ -542,7 +677,9 @@ async function duplexProfiled(request, options = {}) {
542
677
  responseStream: wrappedResponseStream
543
678
  };
544
679
  }
680
+ /** Owner shutdown. Every session handle opened on this runtime ends with it. */
545
681
  export async function close() {
682
+ endAllSessions('shutdown');
546
683
  if (!rpcInstance)
547
684
  return;
548
685
  invalidateRPCInstance();
@@ -1,4 +1,4 @@
1
- import { RpcServerOperationError, InferenceCancelledError, ContextOverflowError, RequestIdConflictError, RequestNotFoundError, RequestRejectedByPolicyError, TranslationFailedError } from '../../utils/errors-server.js';
1
+ import { RpcServerOperationError, InferenceCancelledError, ContextOverflowError, ModelRegistryModelNotFoundError, ModelRegistryBusyError, RequestIdConflictError, RequestNotFoundError, RequestRejectedByPolicyError, TranslationFailedError, AudioEncodeFailedError, InvalidSeedFromParamsError, KvCacheSeedSourceNotFoundError, KvCacheSeedSharingDisabledError, SessionIdConflictError, SessionNotOpenError, ModelLeasedError, ModelReconfiguredError, OwnerOnlyError, RAGWorkspaceBusyError } from '../../utils/errors-server.js';
2
2
  export class RPCError extends Error {
3
3
  timestamp;
4
4
  remoteStack;
@@ -98,6 +98,9 @@ function readOptionalStringField(fields, key) {
98
98
  * errors support both paths.
99
99
  */
100
100
  const RECONSTRUCTORS = {
101
+ INVALID_SEED_FROM_PARAMS: (response) => new InvalidSeedFromParamsError(response.cause),
102
+ KV_CACHE_SEED_SOURCE_NOT_FOUND: (response) => new KvCacheSeedSourceNotFoundError(readStringField(response.typedFields, 'seedFrom', ''), readStringField(response.typedFields, 'modelId', ''), response.cause),
103
+ KV_CACHE_SEED_SHARING_DISABLED: (response) => new KvCacheSeedSharingDisabledError(readStringField(response.typedFields, 'modelId', ''), response.cause),
101
104
  INFERENCE_CANCELLED: (response) => {
102
105
  const partial = response.typedFields?.['partial'];
103
106
  return new InferenceCancelledError(readStringField(response.typedFields, 'requestId', ''), partial && typeof partial === 'object' && !Array.isArray(partial)
@@ -108,6 +111,14 @@ const RECONSTRUCTORS = {
108
111
  REQUEST_ID_CONFLICT: (response) => {
109
112
  return new RequestIdConflictError(readStringField(response.typedFields, 'requestId', ''), response.cause);
110
113
  },
114
+ QVAC_MODEL_REGISTRY_MODEL_NOT_FOUND: (response) => {
115
+ return new ModelRegistryModelNotFoundError(readStringField(response.typedFields, 'registrySource', ''), readStringField(response.typedFields, 'registryPath', ''), response.cause);
116
+ },
117
+ QVAC_MODEL_REGISTRY_BUSY: (response) => {
118
+ return new ModelRegistryBusyError(readStringField(response.typedFields, 'holder', 'current') === 'legacy'
119
+ ? 'legacy'
120
+ : 'current', readOptionalNumberField(response.typedFields, 'waitedMs') ?? 0, response.cause);
121
+ },
111
122
  REQUEST_NOT_FOUND: (response) => {
112
123
  return new RequestNotFoundError(readStringField(response.typedFields, 'requestId', ''), response.cause);
113
124
  },
@@ -127,7 +138,18 @@ const RECONSTRUCTORS = {
127
138
  // class -- matching the Python client's reconstructor.
128
139
  TRANSLATION_FAILED: (response) => {
129
140
  return new TranslationFailedError(response.message, response.cause);
130
- }
141
+ },
142
+ // An encoder the inference host lacks fails an `outputFormat` request in the
143
+ // worker; the format and the encoder's own message come back as fields.
144
+ AUDIO_ENCODE_FAILED: (response) => {
145
+ return new AudioEncodeFailedError(readStringField(response.typedFields, 'format', ''), readOptionalStringField(response.typedFields, 'details'), response.cause);
146
+ },
147
+ SESSION_ID_CONFLICT: (response) => new SessionIdConflictError(readStringField(response.typedFields, 'sessionId', ''), response.cause),
148
+ SESSION_NOT_OPEN: (response) => new SessionNotOpenError(readStringField(response.typedFields, 'sessionId', ''), response.cause),
149
+ MODEL_LEASED: (response) => new ModelLeasedError(readStringField(response.typedFields, 'modelId', ''), readOptionalNumberField(response.typedFields, 'leaseCount') ?? 0, response.cause),
150
+ OWNER_ONLY: (response) => new OwnerOnlyError(readStringField(response.typedFields, 'operation', ''), readStringField(response.typedFields, 'sessionId', ''), response.cause),
151
+ MODEL_RECONFIGURED: (response) => new ModelReconfiguredError(readStringField(response.typedFields, 'modelId', ''), response.cause),
152
+ RAG_WORKSPACE_BUSY: (response) => new RAGWorkspaceBusyError(readStringField(response.typedFields, 'workspace', ''), response.cause)
131
153
  };
132
154
  /**
133
155
  * Rebuild the original server-thrown typed error from its serialised
@@ -0,0 +1,16 @@
1
+ import { type IPC } from 'bare-stow-host';
2
+ import type { WorkerProcess } from './worker-failure.js';
3
+ /**
4
+ * Starts a worker entry without a bundle on bare-sidecar, wrapped by
5
+ * bare-stow-host. The worker's stderr goes to `onStderr`, and the spawned
6
+ * sidecar to `onSpawn`.
7
+ */
8
+ export declare function start(entry: string, options?: {
9
+ timeout?: number;
10
+ grace?: number;
11
+ onStderr?: (chunk: unknown) => void;
12
+ onSpawn?: (sidecar: WorkerProcess) => void;
13
+ }): Promise<{
14
+ ipc: IPC;
15
+ }>;
16
+ //# sourceMappingURL=unbundled-harness.d.ts.map
@@ -0,0 +1,27 @@
1
+ import { wrap } from 'bare-stow-host';
2
+ import { fileURLToPath } from 'node:url';
3
+ function unbundledShim() {
4
+ const asset = import.meta.asset;
5
+ return fileURLToPath(asset
6
+ ? asset('../../worker/unbundled-shim.js')
7
+ : new URL('../../worker/unbundled-shim.js', import.meta.url));
8
+ }
9
+ /**
10
+ * Starts a worker entry without a bundle on bare-sidecar, wrapped by
11
+ * bare-stow-host. The worker's stderr goes to `onStderr`, and the spawned
12
+ * sidecar to `onSpawn`.
13
+ */
14
+ export async function start(entry, options = {}) {
15
+ const { onStderr, onSpawn, ...ipcOptions } = options;
16
+ const { default: Sidecar } = await import('bare-sidecar');
17
+ const sidecar = new Sidecar(unbundledShim(), [entry], {
18
+ stdio: ['inherit', 'inherit', onStderr ? 'pipe' : 'inherit']
19
+ });
20
+ if (onStderr)
21
+ sidecar.stderr?.on('data', onStderr);
22
+ onSpawn?.(sidecar);
23
+ const ipc = wrap(sidecar, { ...ipcOptions, kill: () => sidecar.kill('SIGKILL') });
24
+ sidecar.on('exit', (code, signal) => ipc.exited(code, signal));
25
+ await ipc.ready;
26
+ return { ipc };
27
+ }
@@ -0,0 +1,27 @@
1
+ import { WorkerCrashedError, WorkerStartupError } from '../../utils/errors-client.js';
2
+ /**
3
+ * How long a worker gets to start, from launch until it has applied its
4
+ * configuration, when `rpcInitTimeoutMs` is not set.
5
+ */
6
+ export declare const DEFAULT_WORKER_START_TIMEOUT_MS = 60000;
7
+ /** How long a stopping worker gets to unload its engines before it is killed. */
8
+ export declare const WORKER_SHUTDOWN_GRACE_MS = 10000;
9
+ /** A worker process the client spawned. */
10
+ export interface WorkerProcess {
11
+ kill(signal: 'SIGKILL'): unknown;
12
+ }
13
+ export declare function workerCrashed(cause: unknown): WorkerCrashedError;
14
+ /** Rejects with the signal's reason once it aborts. */
15
+ export declare function rejectOnAbort(signal: AbortSignal): Promise<never>;
16
+ /** Rejects once `ms` have passed, for the part of the start after the worker is ready. */
17
+ export declare function startDeadline(ms: number): {
18
+ expired: Promise<never>;
19
+ clear: () => void;
20
+ };
21
+ export declare function workerStartupFailed(cause: unknown, stderrTail?: string): WorkerStartupError;
22
+ /** Keeps the end of a worker's stderr and passes each chunk to `log`. */
23
+ export declare function stderrTail(log: (text: string) => void): {
24
+ write: (chunk: unknown) => void;
25
+ read: () => string | undefined;
26
+ };
27
+ //# sourceMappingURL=worker-failure.d.ts.map
@@ -0,0 +1,61 @@
1
+ import { WorkerCrashedError, WorkerStartupError } from '../../utils/errors-client.js';
2
+ /**
3
+ * How long a worker gets to start, from launch until it has applied its
4
+ * configuration, when `rpcInitTimeoutMs` is not set.
5
+ */
6
+ export const DEFAULT_WORKER_START_TIMEOUT_MS = 60_000;
7
+ /** How long a stopping worker gets to unload its engines before it is killed. */
8
+ export const WORKER_SHUTDOWN_GRACE_MS = 10_000;
9
+ function exitOf(cause) {
10
+ const exit = cause;
11
+ return {
12
+ exitCode: exit?.exitCode ?? null,
13
+ exitSignal: (exit?.signalCode ?? null)
14
+ };
15
+ }
16
+ export function workerCrashed(cause) {
17
+ const { exitCode, exitSignal } = exitOf(cause);
18
+ return new WorkerCrashedError(exitCode, exitSignal, cause);
19
+ }
20
+ /** Rejects with the signal's reason once it aborts. */
21
+ export function rejectOnAbort(signal) {
22
+ return new Promise((_, reject) => {
23
+ const fail = () => reject(signal.reason instanceof Error ? signal.reason : new WorkerCrashedError(null, null));
24
+ if (signal.aborted) {
25
+ fail();
26
+ return;
27
+ }
28
+ signal.addEventListener('abort', fail, { once: true });
29
+ });
30
+ }
31
+ /** Rejects once `ms` have passed, for the part of the start after the worker is ready. */
32
+ export function startDeadline(ms) {
33
+ let timer;
34
+ const expired = new Promise((_, reject) => {
35
+ timer = setTimeout(() => {
36
+ reject(new WorkerStartupError('Worker did not apply its configuration in time', {
37
+ exitCode: null,
38
+ exitSignal: null,
39
+ timedOut: true
40
+ }));
41
+ }, Math.max(ms, 0));
42
+ });
43
+ return { expired, clear: () => clearTimeout(timer) };
44
+ }
45
+ export function workerStartupFailed(cause, stderrTail) {
46
+ const timedOut = cause?.code === 'WORKER_TIMEOUT';
47
+ return new WorkerStartupError(timedOut ? 'Worker did not signal ready in time' : 'Worker failed before signalling ready', { ...exitOf(cause), timedOut, ...(stderrTail ? { stderrTail } : {}) }, cause);
48
+ }
49
+ const STDERR_TAIL_CHARS = 4096;
50
+ /** Keeps the end of a worker's stderr and passes each chunk to `log`. */
51
+ export function stderrTail(log) {
52
+ let tail = '';
53
+ return {
54
+ write(chunk) {
55
+ const text = String(chunk);
56
+ log(text.trimEnd());
57
+ tail = (tail + text).slice(-STDERR_TAIL_CHARS);
58
+ },
59
+ read: () => tail || undefined
60
+ };
61
+ }