rivetkit 2.3.18-rc.4 → 2.3.19

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 (136) hide show
  1. package/dist/browser/client.d.ts +723 -616
  2. package/dist/browser/client.js +184 -28
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.js +3 -1
  5. package/dist/browser/inspector/client.js.map +1 -1
  6. package/dist/tsup/actor/errors.cjs +6 -2
  7. package/dist/tsup/actor/errors.cjs.map +1 -1
  8. package/dist/tsup/actor/errors.d.cts +1 -1
  9. package/dist/tsup/actor/errors.d.ts +1 -1
  10. package/dist/tsup/actor/errors.js +5 -1
  11. package/dist/tsup/agent-os/index.cjs +3 -1
  12. package/dist/tsup/agent-os/index.cjs.map +1 -1
  13. package/dist/tsup/agent-os/index.d.cts +110 -3
  14. package/dist/tsup/agent-os/index.d.ts +110 -3
  15. package/dist/tsup/agent-os/index.js +3 -1
  16. package/dist/tsup/agent-os/index.js.map +1 -1
  17. package/dist/tsup/{chunk-ENDOWQTM.js → chunk-464WE4RF.js} +2 -2
  18. package/dist/tsup/{chunk-KKG2CTSI.js → chunk-4SC2NSAV.js} +2 -2
  19. package/dist/tsup/{chunk-R2WRP6VV.cjs → chunk-7A5EIXJJ.cjs} +7 -7
  20. package/dist/tsup/{chunk-R2WRP6VV.cjs.map → chunk-7A5EIXJJ.cjs.map} +1 -1
  21. package/dist/tsup/{chunk-GUVDWB2O.cjs → chunk-7BRRGBRY.cjs} +10 -10
  22. package/dist/tsup/{chunk-GUVDWB2O.cjs.map → chunk-7BRRGBRY.cjs.map} +1 -1
  23. package/dist/tsup/{chunk-KVPU65TI.js → chunk-7HTST3NX.js} +3 -3
  24. package/dist/tsup/{chunk-MS6C3B2I.js → chunk-BKX7UDTA.js} +185 -28
  25. package/dist/tsup/chunk-BKX7UDTA.js.map +1 -0
  26. package/dist/tsup/{chunk-FL2FDUSZ.js → chunk-DK3L22R6.js} +4 -4
  27. package/dist/tsup/{chunk-CAL23WOC.js → chunk-DN6MD3KK.js} +7 -7
  28. package/dist/tsup/chunk-DN6MD3KK.js.map +1 -0
  29. package/dist/tsup/{chunk-6W5VGLFT.js → chunk-EB77NZL4.js} +1 -1
  30. package/dist/tsup/chunk-EB77NZL4.js.map +1 -0
  31. package/dist/tsup/{chunk-4KGXFC6M.cjs → chunk-EO5UOXXY.cjs} +1 -1
  32. package/dist/tsup/chunk-EO5UOXXY.cjs.map +1 -0
  33. package/dist/tsup/{chunk-QFLG6UB4.cjs → chunk-G4U5L6AX.cjs} +3 -3
  34. package/dist/tsup/{chunk-QFLG6UB4.cjs.map → chunk-G4U5L6AX.cjs.map} +1 -1
  35. package/dist/tsup/{chunk-TGGF6SZN.cjs → chunk-IJUXBWH4.cjs} +351 -194
  36. package/dist/tsup/chunk-IJUXBWH4.cjs.map +1 -0
  37. package/dist/tsup/{chunk-2JSYYH6P.cjs → chunk-JX2ITWAV.cjs} +13 -2
  38. package/dist/tsup/chunk-JX2ITWAV.cjs.map +1 -0
  39. package/dist/tsup/{chunk-N3DJTMSZ.cjs → chunk-OGPFN6BT.cjs} +3 -3
  40. package/dist/tsup/{chunk-N3DJTMSZ.cjs.map → chunk-OGPFN6BT.cjs.map} +1 -1
  41. package/dist/tsup/{chunk-OUQUIBVW.js → chunk-P2OFXH4G.js} +12 -1
  42. package/dist/tsup/chunk-P2OFXH4G.js.map +1 -0
  43. package/dist/tsup/{chunk-F3IP3SGO.cjs → chunk-PPEIPCW4.cjs} +4 -4
  44. package/dist/tsup/{chunk-F3IP3SGO.cjs.map → chunk-PPEIPCW4.cjs.map} +1 -1
  45. package/dist/tsup/{chunk-7E2W2YSM.cjs → chunk-RJC5O2SD.cjs} +13 -13
  46. package/dist/tsup/chunk-RJC5O2SD.cjs.map +1 -0
  47. package/dist/tsup/{chunk-YQD6RRVP.js → chunk-WME6KOXW.js} +2 -2
  48. package/dist/tsup/{chunk-PLAJJOTE.js → chunk-X7RMJQGY.js} +2 -2
  49. package/dist/tsup/{chunk-4OMHYVOQ.cjs → chunk-Z4RAXFSD.cjs} +3 -3
  50. package/dist/tsup/{chunk-4OMHYVOQ.cjs.map → chunk-Z4RAXFSD.cjs.map} +1 -1
  51. package/dist/tsup/client/mod.cjs +6 -6
  52. package/dist/tsup/client/mod.d.cts +5 -5
  53. package/dist/tsup/client/mod.d.ts +5 -5
  54. package/dist/tsup/client/mod.js +5 -5
  55. package/dist/tsup/common/log.cjs +3 -3
  56. package/dist/tsup/common/log.js +2 -2
  57. package/dist/tsup/common/websocket.cjs +4 -4
  58. package/dist/tsup/common/websocket.js +3 -3
  59. package/dist/tsup/{config-gvHwMas-.d.cts → config-8n2AOhXK.d.cts} +114 -10
  60. package/dist/tsup/{config-Ug5M2MOr.d.ts → config-DEm0yIVV.d.ts} +114 -10
  61. package/dist/tsup/{context-DererUCI.d.ts → context-DGg2-IOm.d.ts} +1 -1
  62. package/dist/tsup/{context-BSCBvbfM.d.cts → context-DwjOVvlE.d.cts} +1 -1
  63. package/dist/tsup/db/drizzle.cjs +3 -3
  64. package/dist/tsup/db/drizzle.js +2 -2
  65. package/dist/tsup/db/mod.cjs +4 -4
  66. package/dist/tsup/db/mod.js +3 -3
  67. package/dist/tsup/dynamic/mod.cjs +2 -2
  68. package/dist/tsup/dynamic/mod.d.cts +3 -3
  69. package/dist/tsup/dynamic/mod.d.ts +3 -3
  70. package/dist/tsup/dynamic/mod.js +1 -1
  71. package/dist/tsup/inspector/mod.cjs +6 -6
  72. package/dist/tsup/inspector/mod.js +5 -5
  73. package/dist/tsup/inspector/workflow.cjs +5 -5
  74. package/dist/tsup/inspector/workflow.js +4 -4
  75. package/dist/tsup/inspector-tab/mod.cjs +2 -2
  76. package/dist/tsup/inspector-tab/mod.d.cts +4 -4
  77. package/dist/tsup/inspector-tab/mod.d.ts +4 -4
  78. package/dist/tsup/inspector-tab/mod.js +1 -1
  79. package/dist/tsup/mod.cjs +492 -319
  80. package/dist/tsup/mod.cjs.map +1 -1
  81. package/dist/tsup/mod.d.cts +5 -5
  82. package/dist/tsup/mod.d.ts +5 -5
  83. package/dist/tsup/mod.js +367 -194
  84. package/dist/tsup/mod.js.map +1 -1
  85. package/dist/tsup/test/mod.cjs +7 -7
  86. package/dist/tsup/test/mod.d.cts +3 -3
  87. package/dist/tsup/test/mod.d.ts +3 -3
  88. package/dist/tsup/test/mod.js +5 -5
  89. package/dist/tsup/{utils-Bbz-3411.d.cts → utils-CNFeS-Dk.d.cts} +1 -1
  90. package/dist/tsup/{utils-vvJQyygr.d.ts → utils-DsoDinPz.d.cts} +9 -1
  91. package/dist/tsup/{utils-vvJQyygr.d.cts → utils-DsoDinPz.d.ts} +9 -1
  92. package/dist/tsup/{utils-BqdWVlzm.d.ts → utils-OQHBjfcM.d.ts} +1 -1
  93. package/dist/tsup/utils.cjs +3 -3
  94. package/dist/tsup/utils.d.cts +2 -2
  95. package/dist/tsup/utils.d.ts +2 -2
  96. package/dist/tsup/utils.js +2 -2
  97. package/dist/tsup/workflow/mod.cjs +33 -27
  98. package/dist/tsup/workflow/mod.cjs.map +1 -1
  99. package/dist/tsup/workflow/mod.d.cts +5 -5
  100. package/dist/tsup/workflow/mod.d.ts +5 -5
  101. package/dist/tsup/workflow/mod.js +25 -19
  102. package/dist/tsup/workflow/mod.js.map +1 -1
  103. package/package.json +10 -8
  104. package/src/actor/config.ts +38 -0
  105. package/src/actor/errors.ts +31 -0
  106. package/src/client/actor-conn.ts +7 -0
  107. package/src/client/actor-handle.ts +57 -17
  108. package/src/client/client.ts +26 -6
  109. package/src/client/outbound-telemetry.ts +66 -0
  110. package/src/client/queue.ts +3 -0
  111. package/src/client/raw-utils.ts +3 -0
  112. package/src/common/actor-router-consts.ts +3 -0
  113. package/src/common/actor-telemetry-context.ts +19 -0
  114. package/src/common/otel-context.ts +99 -0
  115. package/src/common/utils.ts +1 -19
  116. package/src/engine-client/actor-http-client.ts +15 -0
  117. package/src/registry/index.ts +6 -0
  118. package/src/registry/napi-runtime.ts +155 -35
  119. package/src/registry/native.ts +277 -191
  120. package/src/registry/runtime.ts +66 -0
  121. package/src/registry/wasm-runtime.ts +40 -0
  122. package/src/workflow/driver.ts +20 -12
  123. package/dist/tsup/chunk-2JSYYH6P.cjs.map +0 -1
  124. package/dist/tsup/chunk-4KGXFC6M.cjs.map +0 -1
  125. package/dist/tsup/chunk-6W5VGLFT.js.map +0 -1
  126. package/dist/tsup/chunk-7E2W2YSM.cjs.map +0 -1
  127. package/dist/tsup/chunk-CAL23WOC.js.map +0 -1
  128. package/dist/tsup/chunk-MS6C3B2I.js.map +0 -1
  129. package/dist/tsup/chunk-OUQUIBVW.js.map +0 -1
  130. package/dist/tsup/chunk-TGGF6SZN.cjs.map +0 -1
  131. /package/dist/tsup/{chunk-ENDOWQTM.js.map → chunk-464WE4RF.js.map} +0 -0
  132. /package/dist/tsup/{chunk-KKG2CTSI.js.map → chunk-4SC2NSAV.js.map} +0 -0
  133. /package/dist/tsup/{chunk-KVPU65TI.js.map → chunk-7HTST3NX.js.map} +0 -0
  134. /package/dist/tsup/{chunk-FL2FDUSZ.js.map → chunk-DK3L22R6.js.map} +0 -0
  135. /package/dist/tsup/{chunk-YQD6RRVP.js.map → chunk-WME6KOXW.js.map} +0 -0
  136. /package/dist/tsup/{chunk-PLAJJOTE.js.map → chunk-X7RMJQGY.js.map} +0 -0
@@ -1,3 +1,4 @@
1
+ import type { AsyncLocalStorage } from "node:async_hooks";
1
2
  import type {
2
3
  ActorContext as NativeActorContext,
3
4
  NapiActorFactory as NativeActorFactory,
@@ -7,6 +8,14 @@ import type {
7
8
  HttpResponseBodyStream as NativeHttpResponseBodyStream,
8
9
  WebSocket as NativeWebSocket,
9
10
  } from "@rivetkit/rivetkit-napi";
11
+ import type { WorkflowSpan } from "@/actor/config";
12
+ import { encodeErrorForBridge } from "@/actor/errors";
13
+ import type { ActorInvocationTraceContext } from "@/common/actor-telemetry-context";
14
+ import {
15
+ readActiveTraceHeaders,
16
+ runWithActorInvocationSpan,
17
+ } from "@/common/otel-context";
18
+ import { logger } from "./log";
10
19
  import type {
11
20
  ActorContextHandle,
12
21
  ActorFactoryHandle,
@@ -26,6 +35,7 @@ import type {
26
35
  RuntimeKvEntry,
27
36
  RuntimeKvListOptions,
28
37
  RuntimeListenerConfig,
38
+ RuntimeOutboundCall,
29
39
  RuntimeQueueEnqueueAndWaitOptions,
30
40
  RuntimeQueueMessage,
31
41
  RuntimeQueueNextBatchOptions,
@@ -65,6 +75,9 @@ type NapiSqlTransaction = Awaited<
65
75
  type NapiActorStateTransaction = Awaited<
66
76
  ReturnType<NativeActorContext["beginStateTransaction"]>
67
77
  >;
78
+ type NapiQueueMessage = Awaited<
79
+ ReturnType<ReturnType<NativeActorContext["queue"]>["send"]>
80
+ >;
68
81
 
69
82
  function asNativeRegistry(handle: RegistryHandle): NativeCoreRegistry {
70
83
  return handle as unknown as NativeCoreRegistry;
@@ -233,7 +246,7 @@ function toNapiKvEntry(entry: RuntimeKvEntry): {
233
246
  };
234
247
  }
235
248
 
236
- function toNapiQueueMessage(message: RuntimeQueueMessage): RuntimeQueueMessage {
249
+ function toNapiQueueMessage(message: NapiQueueMessage): RuntimeQueueMessage {
237
250
  return {
238
251
  id: () => message.id(),
239
252
  name: () => message.name(),
@@ -254,23 +267,54 @@ export class NapiCoreRuntime implements CoreRuntime {
254
267
  readonly kind = "napi";
255
268
 
256
269
  #bindings: NativeBindings;
257
- #sql = new WeakMap<NativeActorContext, NapiSqlDatabase>();
270
+ #sqlOpened = new WeakSet<NativeActorContext>();
271
+ #invocationContext: AsyncLocalStorage<NativeActorContext>;
258
272
 
259
- constructor(bindings: NativeBindings) {
273
+ constructor(
274
+ bindings: NativeBindings,
275
+ invocationContext: AsyncLocalStorage<NativeActorContext>,
276
+ ) {
260
277
  this.#bindings = bindings;
278
+ this.#invocationContext = invocationContext;
261
279
  }
262
280
 
263
- #actorSql(ctx: ActorContextHandle): NapiSqlDatabase {
264
- const nativeCtx = asNativeActorContext(ctx);
265
- let database = this.#sql.get(nativeCtx);
266
- if (!database) {
267
- database = nativeCtx.sql();
268
- this.#sql.set(nativeCtx, database);
281
+ #actorContextForOperation(owner: ActorContextHandle): NativeActorContext {
282
+ const ownerCtx = asNativeActorContext(owner);
283
+ const active = this.#invocationContext.getStore();
284
+ if (!active?.sameActorInstance(ownerCtx)) {
285
+ return ownerCtx;
269
286
  }
270
- return database;
287
+ const applicationSpan = readActiveTraceHeaders();
288
+ if (!applicationSpan) {
289
+ return active;
290
+ }
291
+ return active.withApplicationSpan(
292
+ applicationSpan.traceparent,
293
+ applicationSpan.tracestate ?? null,
294
+ );
295
+ }
296
+
297
+ #actorSql(ctx: ActorContextHandle): NapiSqlDatabase {
298
+ this.#sqlOpened.add(asNativeActorContext(ctx));
299
+ return this.#actorContextForOperation(ctx).sql();
271
300
  }
272
301
 
273
302
  createRegistry(): RegistryHandle {
303
+ this.#bindings.setTelemetryLogSink(
304
+ (event: {
305
+ name: string;
306
+ message: string;
307
+ level: "warn" | "error";
308
+ }) => {
309
+ const fields = { otelEvent: event.name };
310
+ const message = event.message || event.name;
311
+ if (event.level === "error") {
312
+ logger().error(fields, message);
313
+ } else {
314
+ logger().warn(fields, message);
315
+ }
316
+ },
317
+ );
274
318
  return asRegistryHandle(new this.#bindings.CoreRegistry());
275
319
  }
276
320
 
@@ -297,6 +341,10 @@ export class NapiCoreRuntime implements CoreRuntime {
297
341
  await asNativeRegistry(registry).shutdown();
298
342
  }
299
343
 
344
+ async shutdownTelemetry(): Promise<void> {
345
+ await this.#bindings.shutdownTelemetry();
346
+ }
347
+
300
348
  async registryActorStopThresholdMs(
301
349
  registry: RegistryHandle,
302
350
  ): Promise<number | undefined> {
@@ -552,6 +600,74 @@ export class NapiCoreRuntime implements CoreRuntime {
552
600
  return asNativeActorContext(ctx).actorId();
553
601
  }
554
602
 
603
+ runWithActorInvocationContext<T>(ctx: ActorContextHandle, run: () => T): T {
604
+ return this.#runAs(asNativeActorContext(ctx), run);
605
+ }
606
+
607
+ #runAs<T>(nativeCtx: NativeActorContext, run: () => T): T {
608
+ const span = nativeCtx.invocationTraceContext()?.span;
609
+ return this.#invocationContext.run(nativeCtx, () =>
610
+ runWithActorInvocationSpan(span, run),
611
+ );
612
+ }
613
+
614
+ actorInvocationTraceContext(
615
+ ctx: ActorContextHandle,
616
+ ): ActorInvocationTraceContext | undefined {
617
+ return (
618
+ this.#actorContextForOperation(ctx).invocationTraceContext() ??
619
+ undefined
620
+ );
621
+ }
622
+
623
+ startCallSpan(
624
+ ctx: ActorContextHandle,
625
+ actorName: string,
626
+ actionName: string,
627
+ ): RuntimeOutboundCall | undefined {
628
+ const call = this.#actorContextForOperation(ctx).startCallSpan(
629
+ actorName,
630
+ actionName,
631
+ );
632
+ if (!call) return undefined;
633
+ return {
634
+ span: call.spanContext() ?? undefined,
635
+ finish: (error?: string) => call.finish(error),
636
+ };
637
+ }
638
+
639
+ async startWorkflowSpan(ctx: ActorContextHandle): Promise<WorkflowSpan> {
640
+ const span = await asNativeActorContext(ctx).startWorkflowSpan();
641
+ const spanCtx = span.ctx();
642
+ return {
643
+ run: (body) => this.#runAs(spanCtx, body),
644
+ startStep: (name, attempt) => {
645
+ const step = spanCtx.startWorkflowStepSpan(name, attempt);
646
+ if (!step) return undefined;
647
+ const stepCtx = step.ctx();
648
+ return {
649
+ run: (body) => this.#runAs(stepCtx, body),
650
+ finish: (outcome, error) =>
651
+ step.finish(
652
+ outcome,
653
+ error === undefined
654
+ ? undefined
655
+ : encodeErrorForBridge(error),
656
+ ),
657
+ };
658
+ },
659
+ finish: (outcome) => span.finish(outcome),
660
+ };
661
+ }
662
+
663
+ runOutsideActorInvocationContext<T>(run: () => T): T {
664
+ return this.#invocationContext.exit(run);
665
+ }
666
+
667
+ currentInvocationScope(): object | undefined {
668
+ return this.#invocationContext.getStore();
669
+ }
670
+
555
671
  actorName(ctx: ActorContextHandle): string {
556
672
  return asNativeActorContext(ctx).name();
557
673
  }
@@ -600,7 +716,7 @@ export class NapiCoreRuntime implements CoreRuntime {
600
716
  }
601
717
 
602
718
  actorWaitUntil(ctx: ActorContextHandle, promise: Promise<unknown>): void {
603
- asNativeActorContext(ctx).waitUntil(promise);
719
+ this.#actorContextForOperation(ctx).waitUntil(promise);
604
720
  }
605
721
 
606
722
  async actorWaitForTrackedShutdownWork(
@@ -810,7 +926,7 @@ export class NapiCoreRuntime implements CoreRuntime {
810
926
  ctx: ActorContextHandle,
811
927
  timeoutMs?: number,
812
928
  ): Promise<ActorStateTransactionHandle> {
813
- return (await asNativeActorContext(ctx).beginStateTransaction(
929
+ return (await this.#actorContextForOperation(ctx).beginStateTransaction(
814
930
  timeoutMs,
815
931
  )) as unknown as ActorStateTransactionHandle;
816
932
  }
@@ -871,13 +987,10 @@ export class NapiCoreRuntime implements CoreRuntime {
871
987
 
872
988
  async actorSqlClose(ctx: ActorContextHandle): Promise<void> {
873
989
  const nativeCtx = asNativeActorContext(ctx);
874
- const database = this.#sql.get(nativeCtx);
875
- if (!database) {
990
+ if (!this.#sqlOpened.delete(nativeCtx)) {
876
991
  return;
877
992
  }
878
-
879
- this.#sql.delete(nativeCtx);
880
- await database.close();
993
+ await nativeCtx.sql().close();
881
994
  }
882
995
 
883
996
  async actorRuntimeSocketProvision(ctx: ActorContextHandle) {
@@ -890,7 +1003,7 @@ export class NapiCoreRuntime implements CoreRuntime {
890
1003
  body: RuntimeBytes,
891
1004
  ): Promise<RuntimeQueueMessage> {
892
1005
  return toNapiQueueMessage(
893
- await asNativeActorContext(ctx)
1006
+ await this.#actorContextForOperation(ctx)
894
1007
  .queue()
895
1008
  .send(name, toNapiBuffer(body)),
896
1009
  );
@@ -901,7 +1014,7 @@ export class NapiCoreRuntime implements CoreRuntime {
901
1014
  options?: RuntimeQueueNextBatchOptions | undefined | null,
902
1015
  signal?: CancellationTokenHandle | undefined | null,
903
1016
  ): Promise<RuntimeQueueMessage[]> {
904
- const messages = await asNativeActorContext(ctx)
1017
+ const messages = await this.#actorContextForOperation(ctx)
905
1018
  .queue()
906
1019
  .nextBatch(
907
1020
  options,
@@ -917,7 +1030,7 @@ export class NapiCoreRuntime implements CoreRuntime {
917
1030
  signal?: CancellationTokenHandle | undefined | null,
918
1031
  ): Promise<RuntimeQueueMessage> {
919
1032
  return toNapiQueueMessage(
920
- await asNativeActorContext(ctx)
1033
+ await this.#actorContextForOperation(ctx)
921
1034
  .queue()
922
1035
  .waitForNames(
923
1036
  names,
@@ -964,7 +1077,7 @@ export class NapiCoreRuntime implements CoreRuntime {
964
1077
  options?: RuntimeQueueEnqueueAndWaitOptions | undefined | null,
965
1078
  signal?: CancellationTokenHandle | undefined | null,
966
1079
  ): Promise<RuntimeBytes | null> {
967
- return await asNativeActorContext(ctx)
1080
+ return await this.#actorContextForOperation(ctx)
968
1081
  .queue()
969
1082
  .enqueueAndWait(
970
1083
  name,
@@ -1002,7 +1115,7 @@ export class NapiCoreRuntime implements CoreRuntime {
1002
1115
  actionName: string,
1003
1116
  args: RuntimeBytes,
1004
1117
  ): Promise<string> {
1005
- return await asNativeActorContext(ctx)
1118
+ return await this.#actorContextForOperation(ctx)
1006
1119
  .schedule()
1007
1120
  .after(durationMs, actionName, toNapiBuffer(args));
1008
1121
  }
@@ -1013,13 +1126,13 @@ export class NapiCoreRuntime implements CoreRuntime {
1013
1126
  actionName: string,
1014
1127
  args: RuntimeBytes,
1015
1128
  ): Promise<string> {
1016
- return await asNativeActorContext(ctx)
1129
+ return await this.#actorContextForOperation(ctx)
1017
1130
  .schedule()
1018
1131
  .at(timestampMs, actionName, toNapiBuffer(args));
1019
1132
  }
1020
1133
 
1021
1134
  async actorScheduleCancel(ctx: ActorContextHandle, id: string) {
1022
- return await asNativeActorContext(ctx).schedule().cancel(id);
1135
+ return await this.#actorContextForOperation(ctx).schedule().cancel(id);
1023
1136
  }
1024
1137
 
1025
1138
  async actorScheduleGet(
@@ -1027,12 +1140,13 @@ export class NapiCoreRuntime implements CoreRuntime {
1027
1140
  id: string,
1028
1141
  ): Promise<RuntimeScheduledEventInfo | undefined> {
1029
1142
  return (
1030
- (await asNativeActorContext(ctx).schedule().get(id)) ?? undefined
1143
+ (await this.#actorContextForOperation(ctx).schedule().get(id)) ??
1144
+ undefined
1031
1145
  );
1032
1146
  }
1033
1147
 
1034
1148
  async actorScheduleList(ctx: ActorContextHandle) {
1035
- return await asNativeActorContext(ctx).schedule().list();
1149
+ return await this.#actorContextForOperation(ctx).schedule().list();
1036
1150
  }
1037
1151
 
1038
1152
  async actorCronSet(
@@ -1044,7 +1158,7 @@ export class NapiCoreRuntime implements CoreRuntime {
1044
1158
  args: RuntimeBytes,
1045
1159
  maxHistory: number | undefined,
1046
1160
  ) {
1047
- await asNativeActorContext(ctx)
1161
+ await this.#actorContextForOperation(ctx)
1048
1162
  .schedule()
1049
1163
  .cronSet(
1050
1164
  name,
@@ -1064,7 +1178,7 @@ export class NapiCoreRuntime implements CoreRuntime {
1064
1178
  args: RuntimeBytes,
1065
1179
  maxHistory: number | undefined,
1066
1180
  ) {
1067
- await asNativeActorContext(ctx)
1181
+ await this.#actorContextForOperation(ctx)
1068
1182
  .schedule()
1069
1183
  .cronEvery(
1070
1184
  name,
@@ -1079,20 +1193,23 @@ export class NapiCoreRuntime implements CoreRuntime {
1079
1193
  ctx: ActorContextHandle,
1080
1194
  name: string,
1081
1195
  ): Promise<RuntimeCronJobInfo | undefined> {
1082
- return ((await asNativeActorContext(ctx).schedule().cronGet(name)) ??
1083
- undefined) as RuntimeCronJobInfo | undefined;
1196
+ return ((await this.#actorContextForOperation(ctx)
1197
+ .schedule()
1198
+ .cronGet(name)) ?? undefined) as RuntimeCronJobInfo | undefined;
1084
1199
  }
1085
1200
 
1086
1201
  async actorCronList(
1087
1202
  ctx: ActorContextHandle,
1088
1203
  ): Promise<RuntimeCronJobInfo[]> {
1089
- return (await asNativeActorContext(ctx)
1204
+ return (await this.#actorContextForOperation(ctx)
1090
1205
  .schedule()
1091
1206
  .cronList()) as RuntimeCronJobInfo[];
1092
1207
  }
1093
1208
 
1094
1209
  async actorCronDelete(ctx: ActorContextHandle, name: string) {
1095
- return await asNativeActorContext(ctx).schedule().cronDelete(name);
1210
+ return await this.#actorContextForOperation(ctx)
1211
+ .schedule()
1212
+ .cronDelete(name);
1096
1213
  }
1097
1214
 
1098
1215
  async actorCronHistory(
@@ -1100,7 +1217,7 @@ export class NapiCoreRuntime implements CoreRuntime {
1100
1217
  name: string,
1101
1218
  limit: number | undefined,
1102
1219
  ): Promise<RuntimeCronFire[]> {
1103
- return (await asNativeActorContext(ctx)
1220
+ return (await this.#actorContextForOperation(ctx)
1104
1221
  .schedule()
1105
1222
  .cronHistory(name, limit)) as RuntimeCronFire[];
1106
1223
  }
@@ -1173,9 +1290,12 @@ export async function loadNapiRuntime(): Promise<{
1173
1290
  // would snapshot the native `.node` addon into the deploy and 413. The
1174
1291
  // computed specifier keeps it opaque to static analysis so it is never
1175
1292
  // bundled. Enforced by scripts/ci/check-edge-native-closure.mjs.
1176
- const bindings = await import(["@rivetkit", "rivetkit-napi"].join("/"));
1293
+ const [{ AsyncLocalStorage }, bindings] = await Promise.all([
1294
+ import("node:async_hooks"),
1295
+ import(["@rivetkit", "rivetkit-napi"].join("/")),
1296
+ ]);
1177
1297
  return {
1178
1298
  bindings,
1179
- runtime: new NapiCoreRuntime(bindings),
1299
+ runtime: new NapiCoreRuntime(bindings, new AsyncLocalStorage()),
1180
1300
  };
1181
1301
  }