rivetkit 2.3.0-rc.5 → 2.3.0-rc.7

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 (142) hide show
  1. package/dist/browser/client.d.ts +35 -3
  2. package/dist/browser/client.js +795 -200
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.js +4 -3
  5. package/dist/browser/inspector/client.js.map +1 -1
  6. package/dist/tsup/actor/errors.cjs +4 -2
  7. package/dist/tsup/actor/errors.cjs.map +1 -1
  8. package/dist/tsup/actor/errors.d.cts +1 -74
  9. package/dist/tsup/actor/errors.d.ts +1 -74
  10. package/dist/tsup/actor/errors.js +3 -1
  11. package/dist/tsup/agent-os/index.cjs +7 -5
  12. package/dist/tsup/agent-os/index.cjs.map +1 -1
  13. package/dist/tsup/agent-os/index.d.cts +35 -3
  14. package/dist/tsup/agent-os/index.d.ts +35 -3
  15. package/dist/tsup/agent-os/index.js +7 -5
  16. package/dist/tsup/agent-os/index.js.map +1 -1
  17. package/dist/tsup/{chunk-2GANBXVP.cjs → chunk-2H4ISA4Y.cjs} +10 -10
  18. package/dist/tsup/chunk-2H4ISA4Y.cjs.map +1 -0
  19. package/dist/tsup/{chunk-UXTP4EBU.js → chunk-4DJMFOSU.js} +2 -2
  20. package/dist/tsup/{chunk-LDTT6WKJ.js → chunk-4LTY5TOO.js} +132 -4
  21. package/dist/tsup/chunk-4LTY5TOO.js.map +1 -0
  22. package/dist/tsup/{chunk-PGYEMIOE.js → chunk-52TPEKEC.js} +2 -2
  23. package/dist/tsup/{chunk-OVJX4IFY.cjs → chunk-55E7IR6D.cjs} +4 -4
  24. package/dist/tsup/{chunk-OVJX4IFY.cjs.map → chunk-55E7IR6D.cjs.map} +1 -1
  25. package/dist/tsup/{chunk-V3QNBJ7N.cjs → chunk-63WNTDRC.cjs} +31 -10
  26. package/dist/tsup/chunk-63WNTDRC.cjs.map +1 -0
  27. package/dist/tsup/{chunk-SULB574D.js → chunk-CMV6N5OX.js} +3 -3
  28. package/dist/tsup/{chunk-T6YVRM4K.js → chunk-D5G75T7J.js} +3 -1
  29. package/dist/tsup/chunk-D5G75T7J.js.map +1 -0
  30. package/dist/tsup/{chunk-NW2J4SOL.cjs → chunk-FEOG44WH.cjs} +137 -9
  31. package/dist/tsup/chunk-FEOG44WH.cjs.map +1 -0
  32. package/dist/tsup/{chunk-HR547GVH.cjs → chunk-G5HUSWP4.cjs} +8 -8
  33. package/dist/tsup/{chunk-HR547GVH.cjs.map → chunk-G5HUSWP4.cjs.map} +1 -1
  34. package/dist/tsup/{chunk-N2DQSJIW.js → chunk-HERL2VQ2.js} +17 -48
  35. package/dist/tsup/chunk-HERL2VQ2.js.map +1 -0
  36. package/dist/tsup/{chunk-LELRJK66.cjs → chunk-SJLPZEA3.cjs} +3 -3
  37. package/dist/tsup/{chunk-LELRJK66.cjs.map → chunk-SJLPZEA3.cjs.map} +1 -1
  38. package/dist/tsup/{chunk-WQ4HNA4W.cjs → chunk-SRNOPUC6.cjs} +4 -2
  39. package/dist/tsup/chunk-SRNOPUC6.cjs.map +1 -0
  40. package/dist/tsup/{chunk-K34B3OVG.js → chunk-TMLOKTRB.js} +30 -9
  41. package/dist/tsup/chunk-TMLOKTRB.js.map +1 -0
  42. package/dist/tsup/{chunk-NATOT3ET.js → chunk-VFIY6GWO.js} +4 -4
  43. package/dist/tsup/chunk-VFIY6GWO.js.map +1 -0
  44. package/dist/tsup/{chunk-JY73X7VU.js → chunk-VJ4Y4WBT.js} +692 -114
  45. package/dist/tsup/chunk-VJ4Y4WBT.js.map +1 -0
  46. package/dist/tsup/{chunk-JRCZDHXT.cjs → chunk-X6HIFXNK.cjs} +23 -54
  47. package/dist/tsup/chunk-X6HIFXNK.cjs.map +1 -0
  48. package/dist/tsup/{chunk-FTZIZ3JG.cjs → chunk-ZGPX6KAH.cjs} +838 -260
  49. package/dist/tsup/chunk-ZGPX6KAH.cjs.map +1 -0
  50. package/dist/tsup/client/mod.cjs +7 -7
  51. package/dist/tsup/client/mod.d.cts +3 -4
  52. package/dist/tsup/client/mod.d.ts +3 -4
  53. package/dist/tsup/client/mod.js +6 -6
  54. package/dist/tsup/common/log.cjs +3 -3
  55. package/dist/tsup/common/log.js +2 -2
  56. package/dist/tsup/common/websocket.cjs +4 -4
  57. package/dist/tsup/common/websocket.js +3 -3
  58. package/dist/tsup/{config-CvQUtDp9.d.ts → config-Ak1lv4gF.d.ts} +27 -5
  59. package/dist/tsup/{config-C-a9vrke.d.cts → config-DU_xj4qZ.d.cts} +27 -5
  60. package/dist/tsup/{context-A7R0bsZL.d.ts → context-DAAp4Lpg.d.ts} +1 -1
  61. package/dist/tsup/{context-CA3r-pf2.d.cts → context-Dt_L55q8.d.cts} +1 -1
  62. package/dist/tsup/inspector/mod.cjs +6 -6
  63. package/dist/tsup/inspector/mod.js +5 -5
  64. package/dist/tsup/mod.cjs +507 -308
  65. package/dist/tsup/mod.cjs.map +1 -1
  66. package/dist/tsup/mod.d.cts +4 -5
  67. package/dist/tsup/mod.d.ts +4 -5
  68. package/dist/tsup/mod.js +432 -233
  69. package/dist/tsup/mod.js.map +1 -1
  70. package/dist/tsup/process-metrics-NW754INA.js +118 -0
  71. package/dist/tsup/process-metrics-NW754INA.js.map +1 -0
  72. package/dist/tsup/process-metrics-TYAGKCEJ.cjs +118 -0
  73. package/dist/tsup/process-metrics-TYAGKCEJ.cjs.map +1 -0
  74. package/dist/tsup/test/mod.cjs +10 -10
  75. package/dist/tsup/test/mod.d.cts +2 -3
  76. package/dist/tsup/test/mod.d.ts +2 -3
  77. package/dist/tsup/test/mod.js +6 -6
  78. package/dist/tsup/utils-DVekpm4I.d.cts +103 -0
  79. package/dist/tsup/utils-DVekpm4I.d.ts +103 -0
  80. package/dist/tsup/utils.cjs +3 -3
  81. package/dist/tsup/utils.d.cts +1 -1
  82. package/dist/tsup/utils.d.ts +1 -1
  83. package/dist/tsup/utils.js +2 -2
  84. package/dist/tsup/workflow/mod.cjs +41 -16
  85. package/dist/tsup/workflow/mod.cjs.map +1 -1
  86. package/dist/tsup/workflow/mod.d.cts +4 -5
  87. package/dist/tsup/workflow/mod.d.ts +4 -5
  88. package/dist/tsup/workflow/mod.js +35 -10
  89. package/dist/tsup/workflow/mod.js.map +1 -1
  90. package/package.json +11 -10
  91. package/src/actor/config.ts +3 -0
  92. package/src/actor/errors.ts +53 -7
  93. package/src/agent-os/actor/session.ts +2 -2
  94. package/src/client/actor-conn.ts +55 -60
  95. package/src/client/actor-handle.ts +59 -24
  96. package/src/client/errors.ts +2 -1
  97. package/src/client/queue.ts +2 -1
  98. package/src/client/raw-utils.ts +2 -4
  99. package/src/client/utils.ts +32 -4
  100. package/src/common/actor-router-consts.ts +4 -0
  101. package/src/common/bare/generated/client-protocol/v4.ts +599 -0
  102. package/src/common/client-protocol-versioned.ts +125 -18
  103. package/src/common/client-protocol-zod.ts +7 -0
  104. package/src/common/client-protocol.ts +1 -1
  105. package/src/common/database/native-database.test.ts +35 -0
  106. package/src/common/database/native-database.ts +8 -4
  107. package/src/common/encoding.ts +243 -5
  108. package/src/common/inline-websocket-adapter.ts +12 -12
  109. package/src/common/log.ts +1 -0
  110. package/src/common/router.ts +40 -10
  111. package/src/common/utils.ts +9 -200
  112. package/src/drivers/engine/actor-driver.ts +29 -28
  113. package/src/engine-client/actor-websocket-client.ts +2 -1
  114. package/src/engine-client/mod.ts +3 -2
  115. package/src/registry/config/index.ts +3 -5
  116. package/src/registry/index.ts +90 -16
  117. package/src/registry/napi-runtime.ts +15 -0
  118. package/src/registry/native.ts +197 -255
  119. package/src/registry/process-metrics.ts +183 -0
  120. package/src/registry/runtime.ts +4 -0
  121. package/src/registry/wasm-runtime.ts +9 -0
  122. package/src/registry/write-through-proxy.ts +40 -0
  123. package/src/serde.ts +2 -2
  124. package/src/workflow/context.ts +32 -5
  125. package/src/workflow/inspector.ts +2 -1
  126. package/dist/tsup/chunk-2GANBXVP.cjs.map +0 -1
  127. package/dist/tsup/chunk-FTZIZ3JG.cjs.map +0 -1
  128. package/dist/tsup/chunk-JRCZDHXT.cjs.map +0 -1
  129. package/dist/tsup/chunk-JY73X7VU.js.map +0 -1
  130. package/dist/tsup/chunk-K34B3OVG.js.map +0 -1
  131. package/dist/tsup/chunk-LDTT6WKJ.js.map +0 -1
  132. package/dist/tsup/chunk-N2DQSJIW.js.map +0 -1
  133. package/dist/tsup/chunk-NATOT3ET.js.map +0 -1
  134. package/dist/tsup/chunk-NW2J4SOL.cjs.map +0 -1
  135. package/dist/tsup/chunk-T6YVRM4K.js.map +0 -1
  136. package/dist/tsup/chunk-V3QNBJ7N.cjs.map +0 -1
  137. package/dist/tsup/chunk-WQ4HNA4W.cjs.map +0 -1
  138. package/dist/tsup/utils-fwx3o3K9.d.cts +0 -18
  139. package/dist/tsup/utils-fwx3o3K9.d.ts +0 -18
  140. /package/dist/tsup/{chunk-UXTP4EBU.js.map → chunk-4DJMFOSU.js.map} +0 -0
  141. /package/dist/tsup/{chunk-PGYEMIOE.js.map → chunk-52TPEKEC.js.map} +0 -0
  142. /package/dist/tsup/{chunk-SULB574D.js.map → chunk-CMV6N5OX.js.map} +0 -0
@@ -1,6 +1,8 @@
1
+ import { Hono } from "hono";
1
2
  import { ENGINE_ENDPOINT } from "@/common/engine";
2
3
  import { configureServerlessPool } from "@/serverless/configure";
3
- import { VERSION } from "@/utils";
4
+ import { detectRuntime, VERSION } from "@/utils";
5
+ import { crossPlatformServe, loadRuntimeServeStatic } from "@/utils/serve";
4
6
  import {
5
7
  type RegistryActors,
6
8
  type RegistryConfig,
@@ -13,6 +15,22 @@ import type { RuntimeServerlessResponseHead } from "./runtime";
13
15
 
14
16
  type ShutdownSignal = "SIGINT" | "SIGTERM";
15
17
 
18
+ function signalExitCode(signal: ShutdownSignal): number {
19
+ switch (signal) {
20
+ case "SIGINT":
21
+ return 130;
22
+ case "SIGTERM":
23
+ return 143;
24
+ }
25
+ }
26
+
27
+ function finishShutdownSignal(signal: ShutdownSignal): void {
28
+ if (process.pid === 1) {
29
+ process.exit(signalExitCode(signal));
30
+ }
31
+ process.kill(process.pid, signal);
32
+ }
33
+
16
34
  export type FetchHandler = (
17
35
  request: Request,
18
36
  ...args: any
@@ -279,6 +297,36 @@ export class Registry<A extends RegistryActors> {
279
297
  };
280
298
  }
281
299
 
300
+ /**
301
+ * Starts an HTTP server that dispatches every request through the
302
+ * serverless handler. Uses `crossPlatformServe` to pick the right
303
+ * runtime (Node, Bun, Deno).
304
+ *
305
+ * @param opts.port Port to listen on. Defaults to 3000.
306
+ * @param opts.publicDir If set, serves static files from this directory
307
+ * before falling through to the registry handler.
308
+ *
309
+ * @example
310
+ * ```ts
311
+ * await registry.listen();
312
+ * await registry.listen({ port: 8080, publicDir: "./public" });
313
+ * ```
314
+ */
315
+ public async listen(
316
+ opts: { port?: number; publicDir?: string } = {},
317
+ ): Promise<void> {
318
+ const port = opts.port ?? 3000;
319
+ const config = this.parseConfig();
320
+ const runtime = detectRuntime();
321
+ const app = new Hono();
322
+ if (opts.publicDir) {
323
+ const serveStatic = await loadRuntimeServeStatic(runtime);
324
+ app.use("*", serveStatic({ root: opts.publicDir }));
325
+ }
326
+ app.all("*", (c) => this.handler(c.req.raw));
327
+ await crossPlatformServe(config, port, app, runtime);
328
+ }
329
+
282
330
  public async diagnostics(): Promise<RegistryDiagnostics> {
283
331
  const candidates = [
284
332
  this.#runtimeServerlessPromise,
@@ -307,12 +355,12 @@ export class Registry<A extends RegistryActors> {
307
355
  .then(async ({ runtime, registry, serveConfig }) => {
308
356
  await runtime.serveRegistry(registry, serveConfig);
309
357
  })
310
- .catch((err) => {
358
+ .catch((error) => {
311
359
  // Always-attached catch so the stored promise never leaves a
312
360
  // rejection unhandled. Downstream awaits (e.g. #runShutdown's
313
361
  // Promise.race) attach their own catches and still observe
314
362
  // resolution via the race.
315
- logger().warn({ err }, "runtime registry serve errored");
363
+ logger().warn({ error }, "runtime registry serve errored");
316
364
  });
317
365
  // Install signal handlers once an envoy lifecycle has begun. Only
318
366
  // Mode A ever reaches here. Mode B (handler(request)) intentionally
@@ -364,18 +412,19 @@ export class Registry<A extends RegistryActors> {
364
412
  ): void {
365
413
  if (this.#shutdownInFlight !== null) {
366
414
  // Second delivery of the same (or another) shutdown signal.
367
- // Remove our handler only (preserving any user-installed listeners)
368
- // and re-raise so Node proceeds with its default exit path.
415
+ // Remove our handler only, preserving any user-installed listeners.
416
+ // PID 1 must exit directly because re-raised default signals can be
417
+ // swallowed by the container signal path.
369
418
  this.#removeSignalHandlers();
370
- process.kill(process.pid, signal);
419
+ finishShutdownSignal(signal);
371
420
  return;
372
421
  }
373
422
  this.#shutdownInFlight = this.#runShutdown(
374
423
  signal,
375
424
  config,
376
425
  configuredRegistryPromise,
377
- ).catch((err) => {
378
- logger().warn({ err }, "shutdown error");
426
+ ).catch((error) => {
427
+ logger().warn({ error }, "shutdown error");
379
428
  });
380
429
  }
381
430
 
@@ -384,11 +433,13 @@ export class Registry<A extends RegistryActors> {
384
433
  config: RegistryConfig,
385
434
  configuredRegistryPromise: ReturnType<typeof buildConfiguredRegistry>,
386
435
  ): Promise<void> {
387
- const gracePeriodMs = config.shutdown?.gracePeriodMs ?? 30_000;
436
+ const gracePeriodMs =
437
+ config.shutdown?.gracePeriodMs ??
438
+ (await this.#actorStopThresholdMs(configuredRegistryPromise)) ??
439
+ 30 * 60 * 1000;
388
440
  // Race the entire drain sequence (both modes + serve promise) against
389
- // a single grace ceiling. Without this, each mode's Rust-side drain
390
- // (20s) could stack sequentially and blow past gracePeriodMs before
391
- // we re-raise the signal.
441
+ // a single grace ceiling. By default, this uses the engine-provided
442
+ // actor stop threshold, matching Pegboard's hard cutoff for actors.
392
443
  const drain = async () => {
393
444
  // Shut down every live `CoreRegistry` we know about. Mode A
394
445
  // (`start()`) and Mode B (`handler()`) each build a separate
@@ -400,9 +451,9 @@ export class Registry<A extends RegistryActors> {
400
451
  const { runtime, registry } =
401
452
  await configuredRegistryPromise;
402
453
  await runtime.shutdownRegistry(registry);
403
- } catch (err) {
454
+ } catch (error) {
404
455
  logger().warn(
405
- { err },
456
+ { error },
406
457
  "runtime registry shutdown errored (mode A)",
407
458
  );
408
459
  }
@@ -418,7 +469,7 @@ export class Registry<A extends RegistryActors> {
418
469
  await runtime.shutdownRegistry(registry);
419
470
  } catch (err) {
420
471
  logger().warn(
421
- { err },
472
+ { error: err },
422
473
  "runtime registry shutdown errored (mode B)",
423
474
  );
424
475
  }
@@ -442,7 +493,30 @@ export class Registry<A extends RegistryActors> {
442
493
  ),
443
494
  ]);
444
495
  this.#removeSignalHandlers();
445
- process.kill(process.pid, signal);
496
+ finishShutdownSignal(signal);
497
+ }
498
+
499
+ async #actorStopThresholdMs(
500
+ configuredRegistryPromise: ReturnType<typeof buildConfiguredRegistry>,
501
+ ): Promise<number | undefined> {
502
+ try {
503
+ const { runtime, registry } = await configuredRegistryPromise;
504
+ const thresholdMs =
505
+ await runtime.registryActorStopThresholdMs?.(registry);
506
+ if (
507
+ thresholdMs !== undefined &&
508
+ Number.isFinite(thresholdMs) &&
509
+ thresholdMs > 0
510
+ ) {
511
+ return thresholdMs;
512
+ }
513
+ } catch (err) {
514
+ logger().warn(
515
+ { err },
516
+ "failed to read actor stop threshold for shutdown grace",
517
+ );
518
+ }
519
+ return undefined;
446
520
  }
447
521
 
448
522
  #removeSignalHandlers(): void {
@@ -212,6 +212,15 @@ export class NapiCoreRuntime implements CoreRuntime {
212
212
  };
213
213
  }
214
214
 
215
+ async registryActorStopThresholdMs(
216
+ registry: RegistryHandle,
217
+ ): Promise<number | undefined> {
218
+ return (
219
+ (await asNativeRegistry(registry).actorStopThresholdMs()) ??
220
+ undefined
221
+ );
222
+ }
223
+
215
224
  async handleServerlessRequest(
216
225
  registry: RegistryHandle,
217
226
  req: RuntimeServerlessRequest,
@@ -414,6 +423,12 @@ export class NapiCoreRuntime implements CoreRuntime {
414
423
  asNativeActorContext(ctx).waitUntil(promise);
415
424
  }
416
425
 
426
+ async actorWaitForTrackedShutdownWork(
427
+ ctx: ActorContextHandle,
428
+ ): Promise<boolean> {
429
+ return await asNativeActorContext(ctx).waitForTrackedShutdownWork();
430
+ }
431
+
417
432
  actorKeepAwake(ctx: ActorContextHandle, promise: Promise<unknown>): void {
418
433
  asNativeActorContext(ctx).keepAwake(promise);
419
434
  }