rivetkit 2.3.22 → 2.3.24-rc.1

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 (109) hide show
  1. package/dist/browser/client.d.ts +2 -0
  2. package/dist/browser/client.js +1 -1
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.js +1 -1
  5. package/dist/browser/inspector/client.js.map +1 -1
  6. package/dist/tsup/agent-os/index.cjs +1 -1
  7. package/dist/tsup/agent-os/index.cjs.map +1 -1
  8. package/dist/tsup/agent-os/index.d.cts +2 -0
  9. package/dist/tsup/agent-os/index.d.ts +2 -0
  10. package/dist/tsup/agent-os/index.js +1 -1
  11. package/dist/tsup/agent-os/index.js.map +1 -1
  12. package/dist/tsup/{chunk-4DTWJFIZ.js → chunk-64CRWLOX.js} +3 -3
  13. package/dist/tsup/{chunk-GINS4O73.js → chunk-6HW3TU7Q.js} +2 -2
  14. package/dist/tsup/{chunk-VD47X7KQ.js → chunk-B6WTPGOR.js} +3 -3
  15. package/dist/tsup/{chunk-LUMROGNM.cjs → chunk-CE6NBGOL.cjs} +3 -3
  16. package/dist/tsup/{chunk-LUMROGNM.cjs.map → chunk-CE6NBGOL.cjs.map} +1 -1
  17. package/dist/tsup/{chunk-QOQA4JPN.cjs → chunk-DAC73W53.cjs} +7 -7
  18. package/dist/tsup/{chunk-QOQA4JPN.cjs.map → chunk-DAC73W53.cjs.map} +1 -1
  19. package/dist/tsup/{chunk-WLRGUAMW.cjs → chunk-ERBNWARB.cjs} +4 -4
  20. package/dist/tsup/{chunk-WLRGUAMW.cjs.map → chunk-ERBNWARB.cjs.map} +1 -1
  21. package/dist/tsup/{chunk-DE2IFYJK.js → chunk-JJCUSUWO.js} +2 -2
  22. package/dist/tsup/chunk-JJCUSUWO.js.map +1 -0
  23. package/dist/tsup/{chunk-QXBNHJH5.js → chunk-JKDAOE4T.js} +4 -4
  24. package/dist/tsup/{chunk-GZZNJGXY.cjs → chunk-JMO6AVUC.cjs} +3 -3
  25. package/dist/tsup/{chunk-GZZNJGXY.cjs.map → chunk-JMO6AVUC.cjs.map} +1 -1
  26. package/dist/tsup/{chunk-CSOHAAZK.js → chunk-LTGBHN5Z.js} +1 -1
  27. package/dist/tsup/chunk-LTGBHN5Z.js.map +1 -0
  28. package/dist/tsup/{chunk-XZBDVEQN.js → chunk-M5J3PXNU.js} +2 -2
  29. package/dist/tsup/{chunk-NWCAIWHL.cjs → chunk-NGJWQMUU.cjs} +1 -1
  30. package/dist/tsup/chunk-NGJWQMUU.cjs.map +1 -0
  31. package/dist/tsup/{chunk-47FSGXYN.js → chunk-OB5JLDBM.js} +2 -2
  32. package/dist/tsup/{chunk-KEAUN5F5.cjs → chunk-PFHGTLF2.cjs} +10 -10
  33. package/dist/tsup/{chunk-KEAUN5F5.cjs.map → chunk-PFHGTLF2.cjs.map} +1 -1
  34. package/dist/tsup/{chunk-RIPVNPR6.js → chunk-QQ454DVR.js} +2 -2
  35. package/dist/tsup/{chunk-HEY27WZX.cjs → chunk-R3UI7MO3.cjs} +140 -140
  36. package/dist/tsup/{chunk-HEY27WZX.cjs.map → chunk-R3UI7MO3.cjs.map} +1 -1
  37. package/dist/tsup/{chunk-WMNXGH25.cjs → chunk-SWQCSORE.cjs} +3 -3
  38. package/dist/tsup/{chunk-WMNXGH25.cjs.map → chunk-SWQCSORE.cjs.map} +1 -1
  39. package/dist/tsup/{chunk-BDKVTNJF.cjs → chunk-YVVFOKEP.cjs} +2 -2
  40. package/dist/tsup/chunk-YVVFOKEP.cjs.map +1 -0
  41. package/dist/tsup/client/mod.cjs +5 -5
  42. package/dist/tsup/client/mod.d.cts +2 -2
  43. package/dist/tsup/client/mod.d.ts +2 -2
  44. package/dist/tsup/client/mod.js +4 -4
  45. package/dist/tsup/common/log.cjs +2 -2
  46. package/dist/tsup/common/log.js +1 -1
  47. package/dist/tsup/common/websocket.cjs +3 -3
  48. package/dist/tsup/common/websocket.js +2 -2
  49. package/dist/tsup/{config-osdJqQwX.d.cts → config-CuKzfH58.d.cts} +4 -0
  50. package/dist/tsup/{config-Cv6pjCt-.d.ts → config-RV6gIa2L.d.ts} +4 -0
  51. package/dist/tsup/{context-DpjrM7n7.d.cts → context-DPCxyjax.d.cts} +1 -1
  52. package/dist/tsup/{context-CoVbKBuk.d.ts → context-DoMKZBQA.d.ts} +1 -1
  53. package/dist/tsup/db/drizzle.cjs +2 -2
  54. package/dist/tsup/db/drizzle.js +1 -1
  55. package/dist/tsup/db/mod.cjs +3 -3
  56. package/dist/tsup/db/mod.js +2 -2
  57. package/dist/tsup/dynamic/mod.cjs +2 -2
  58. package/dist/tsup/dynamic/mod.d.cts +1 -1
  59. package/dist/tsup/dynamic/mod.d.ts +1 -1
  60. package/dist/tsup/dynamic/mod.js +1 -1
  61. package/dist/tsup/inspector/mod.cjs +5 -5
  62. package/dist/tsup/inspector/mod.js +4 -4
  63. package/dist/tsup/inspector/workflow.cjs +4 -4
  64. package/dist/tsup/inspector/workflow.js +3 -3
  65. package/dist/tsup/inspector-tab/mod.cjs +2 -2
  66. package/dist/tsup/inspector-tab/mod.d.cts +2 -2
  67. package/dist/tsup/inspector-tab/mod.d.ts +2 -2
  68. package/dist/tsup/inspector-tab/mod.js +1 -1
  69. package/dist/tsup/mod.cjs +416 -188
  70. package/dist/tsup/mod.cjs.map +1 -1
  71. package/dist/tsup/mod.d.cts +3 -3
  72. package/dist/tsup/mod.d.ts +3 -3
  73. package/dist/tsup/mod.js +318 -90
  74. package/dist/tsup/mod.js.map +1 -1
  75. package/dist/tsup/test/mod.cjs +6 -6
  76. package/dist/tsup/test/mod.d.cts +1 -1
  77. package/dist/tsup/test/mod.d.ts +1 -1
  78. package/dist/tsup/test/mod.js +4 -4
  79. package/dist/tsup/utils.cjs +2 -2
  80. package/dist/tsup/utils.js +1 -1
  81. package/dist/tsup/workflow/mod.cjs +69 -21
  82. package/dist/tsup/workflow/mod.cjs.map +1 -1
  83. package/dist/tsup/workflow/mod.d.cts +3 -3
  84. package/dist/tsup/workflow/mod.d.ts +3 -3
  85. package/dist/tsup/workflow/mod.js +64 -16
  86. package/dist/tsup/workflow/mod.js.map +1 -1
  87. package/package.json +9 -9
  88. package/src/actor/config.ts +2 -0
  89. package/src/registry/lost-callbacks.test.ts +64 -0
  90. package/src/registry/napi-runtime.ts +8 -0
  91. package/src/registry/native.ts +332 -65
  92. package/src/registry/run-handler-coordinator.test.ts +102 -0
  93. package/src/registry/run-handler-coordinator.ts +84 -17
  94. package/src/registry/runtime.ts +2 -0
  95. package/src/registry/wasm-runtime.ts +8 -0
  96. package/src/workflow/context.ts +20 -0
  97. package/src/workflow/driver.ts +25 -1
  98. package/src/workflow/mod.ts +46 -12
  99. package/dist/tsup/chunk-BDKVTNJF.cjs.map +0 -1
  100. package/dist/tsup/chunk-CSOHAAZK.js.map +0 -1
  101. package/dist/tsup/chunk-DE2IFYJK.js.map +0 -1
  102. package/dist/tsup/chunk-NWCAIWHL.cjs.map +0 -1
  103. /package/dist/tsup/{chunk-4DTWJFIZ.js.map → chunk-64CRWLOX.js.map} +0 -0
  104. /package/dist/tsup/{chunk-GINS4O73.js.map → chunk-6HW3TU7Q.js.map} +0 -0
  105. /package/dist/tsup/{chunk-VD47X7KQ.js.map → chunk-B6WTPGOR.js.map} +0 -0
  106. /package/dist/tsup/{chunk-QXBNHJH5.js.map → chunk-JKDAOE4T.js.map} +0 -0
  107. /package/dist/tsup/{chunk-XZBDVEQN.js.map → chunk-M5J3PXNU.js.map} +0 -0
  108. /package/dist/tsup/{chunk-47FSGXYN.js.map → chunk-OB5JLDBM.js.map} +0 -0
  109. /package/dist/tsup/{chunk-RIPVNPR6.js.map → chunk-QQ454DVR.js.map} +0 -0
package/dist/tsup/mod.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  decodeWorkflowHistoryTransport
3
- } from "./chunk-XZBDVEQN.js";
3
+ } from "./chunk-M5J3PXNU.js";
4
4
  import {
5
5
  ACTOR_CONTEXT_INTERNAL_SYMBOL,
6
6
  ActorConfigSchema,
@@ -17,7 +17,7 @@ import {
17
17
  getRunInspectorKind,
18
18
  getRunMetadata,
19
19
  hasRunInspectorConfig
20
- } from "./chunk-CSOHAAZK.js";
20
+ } from "./chunk-LTGBHN5Z.js";
21
21
  import {
22
22
  ALLOWED_PUBLIC_HEADERS,
23
23
  ENGINE_HOST,
@@ -35,7 +35,7 @@ import {
35
35
  runWithActorInvocationSpan,
36
36
  tryParseEndpoint,
37
37
  updateRunnerConfig
38
- } from "./chunk-VD47X7KQ.js";
38
+ } from "./chunk-B6WTPGOR.js";
39
39
  import {
40
40
  makePrefixedKey,
41
41
  removePrefixFromKey
@@ -44,9 +44,9 @@ import {
44
44
  bindNativeStateTransactionContext,
45
45
  db,
46
46
  registerNativeStateTransactionOpener
47
- } from "./chunk-RIPVNPR6.js";
47
+ } from "./chunk-QQ454DVR.js";
48
48
  import "./chunk-JTHHCZCZ.js";
49
- import "./chunk-47FSGXYN.js";
49
+ import "./chunk-OB5JLDBM.js";
50
50
  import {
51
51
  LogLevelSchema,
52
52
  VERSION,
@@ -80,7 +80,7 @@ import {
80
80
  parsePortEnv,
81
81
  stringifyError,
82
82
  toUint8Array
83
- } from "./chunk-DE2IFYJK.js";
83
+ } from "./chunk-JJCUSUWO.js";
84
84
  import {
85
85
  INTERNAL_ERROR_CODE,
86
86
  RivetError,
@@ -1732,6 +1732,12 @@ var NapiCoreRuntime = class {
1732
1732
  actorId(ctx) {
1733
1733
  return asNativeActorContext(ctx).actorId();
1734
1734
  }
1735
+ actorGeneration(ctx) {
1736
+ return asNativeActorContext(ctx).generation() ?? void 0;
1737
+ }
1738
+ actorIsLost(ctx) {
1739
+ return asNativeActorContext(ctx).isLost();
1740
+ }
1735
1741
  runWithActorInvocationContext(ctx, run) {
1736
1742
  return this.#runAs(asNativeActorContext(ctx), run);
1737
1743
  }
@@ -2351,6 +2357,27 @@ function notifyStateChanged(state) {
2351
2357
  waiter();
2352
2358
  }
2353
2359
  }
2360
+ function createRunState(actorGeneration) {
2361
+ return {
2362
+ actorGeneration,
2363
+ active: false,
2364
+ closed: false,
2365
+ exclusive: false,
2366
+ exclusiveGeneration: 0,
2367
+ inspectorInitialized: false,
2368
+ queued: 0,
2369
+ waiters: /* @__PURE__ */ new Set()
2370
+ };
2371
+ }
2372
+ function closeRunState(state, disposeInspector = true) {
2373
+ var _a, _b;
2374
+ state.closed = true;
2375
+ if (disposeInspector) {
2376
+ (_b = (_a = state.inspector) == null ? void 0 : _a.dispose) == null ? void 0 : _b.call(_a);
2377
+ }
2378
+ state.inspector = void 0;
2379
+ notifyStateChanged(state);
2380
+ }
2354
2381
  function waitForStateChange(state) {
2355
2382
  return new Promise((resolve) => {
2356
2383
  state.waiters.add(resolve);
@@ -2365,8 +2392,11 @@ var RunHandlerCoordinator = class {
2365
2392
  get inspectorKind() {
2366
2393
  return getRunInspectorKind(this.#run);
2367
2394
  }
2368
- async run(actorId, restart, callback) {
2369
- const state = this.#getOrCreate(actorId);
2395
+ async run(actorId, restart, callback, actorGeneration) {
2396
+ const state = this.#getOrCreate(actorId, actorGeneration);
2397
+ if (state.closed) {
2398
+ return "closed";
2399
+ }
2370
2400
  state.restart = restart;
2371
2401
  this.#initializeInspector(actorId, state);
2372
2402
  state.queued += 1;
@@ -2393,21 +2423,27 @@ var RunHandlerCoordinator = class {
2393
2423
  notifyStateChanged(state);
2394
2424
  }
2395
2425
  }
2396
- getInspector(actorId, restart) {
2426
+ getInspector(actorId, restart, actorGeneration) {
2397
2427
  var _a;
2398
- const state = this.#getOrCreate(actorId);
2428
+ const state = this.#getOrCreate(actorId, actorGeneration);
2429
+ if (state.closed) {
2430
+ return void 0;
2431
+ }
2399
2432
  state.restart = restart;
2400
2433
  this.#initializeInspector(actorId, state);
2401
2434
  return (_a = state.inspector) == null ? void 0 : _a.inspector;
2402
2435
  }
2403
- destroy(actorId) {
2404
- var _a, _b;
2436
+ /**
2437
+ * Closes the run state for `actorId`. With `actorGeneration`, only state owned by that
2438
+ * generation is closed, so an older generation's late cleanup cannot close a newer one.
2439
+ */
2440
+ destroy(actorId, actorGeneration, options = {}) {
2405
2441
  const state = this.#states.get(actorId);
2406
2442
  if (!state) return;
2407
- state.closed = true;
2408
- (_b = (_a = state.inspector) == null ? void 0 : _a.dispose) == null ? void 0 : _b.call(_a);
2409
- state.inspector = void 0;
2410
- notifyStateChanged(state);
2443
+ if (actorGeneration !== void 0 && state.actorGeneration !== void 0 && state.actorGeneration !== actorGeneration) {
2444
+ return;
2445
+ }
2446
+ closeRunState(state, options.disposeInspector ?? true);
2411
2447
  this.#states.delete(actorId);
2412
2448
  }
2413
2449
  #control(actorId, state) {
@@ -2453,18 +2489,20 @@ var RunHandlerCoordinator = class {
2453
2489
  }
2454
2490
  };
2455
2491
  }
2456
- #getOrCreate(actorId) {
2492
+ #getOrCreate(actorId, actorGeneration) {
2457
2493
  let state = this.#states.get(actorId);
2494
+ if (state && actorGeneration !== void 0 && state.actorGeneration !== void 0 && state.actorGeneration !== actorGeneration) {
2495
+ if (actorGeneration < state.actorGeneration) {
2496
+ const detached = createRunState(actorGeneration);
2497
+ detached.closed = true;
2498
+ return detached;
2499
+ }
2500
+ closeRunState(state, false);
2501
+ this.#states.delete(actorId);
2502
+ state = void 0;
2503
+ }
2458
2504
  if (!state) {
2459
- state = {
2460
- active: false,
2461
- closed: false,
2462
- exclusive: false,
2463
- exclusiveGeneration: 0,
2464
- inspectorInitialized: false,
2465
- queued: 0,
2466
- waiters: /* @__PURE__ */ new Set()
2467
- };
2505
+ state = createRunState(actorGeneration);
2468
2506
  this.#states.set(actorId, state);
2469
2507
  }
2470
2508
  return state;
@@ -2473,6 +2511,7 @@ var RunHandlerCoordinator = class {
2473
2511
  if (state.inspectorInitialized) return;
2474
2512
  const inspector = createRunInspector(this.#run, {
2475
2513
  actorId,
2514
+ actorGeneration: state.actorGeneration,
2476
2515
  control: this.#control(actorId, state)
2477
2516
  });
2478
2517
  if (this.inspectorKind === "workflow") {
@@ -2800,6 +2839,12 @@ var WasmCoreRuntime = class {
2800
2839
  actorId(ctx) {
2801
2840
  return callHandle(asWasmActorContext(ctx), "actorId");
2802
2841
  }
2842
+ actorGeneration(ctx) {
2843
+ return callHandle(asWasmActorContext(ctx), "generation") ?? void 0;
2844
+ }
2845
+ actorIsLost(ctx) {
2846
+ return callHandle(asWasmActorContext(ctx), "isLost");
2847
+ }
2803
2848
  runWithActorInvocationContext(_ctx, run) {
2804
2849
  return run();
2805
2850
  }
@@ -3631,8 +3676,11 @@ async function cleanupNativeSleepRuntimeState(runtime, ctx, afterTrackedWorkDrai
3631
3676
  await closeNativeDatabaseClient(runtime, ctx);
3632
3677
  await closeNativeSqlDatabase(runtime, ctx);
3633
3678
  void runtime.actorWaitForTrackedShutdownWorkUnbounded(ctx).then(async () => {
3634
- await (afterTrackedWorkDrained == null ? void 0 : afterTrackedWorkDrained());
3635
- clearNativeRuntimeState(runtime, ctx);
3679
+ try {
3680
+ await (afterTrackedWorkDrained == null ? void 0 : afterTrackedWorkDrained());
3681
+ } finally {
3682
+ clearNativeRuntimeState(runtime, ctx);
3683
+ }
3636
3684
  }).catch((error) => {
3637
3685
  logger2().warn({
3638
3686
  msg: "deferred native sleep cleanup failed",
@@ -3641,10 +3689,24 @@ async function cleanupNativeSleepRuntimeState(runtime, ctx, afterTrackedWorkDrai
3641
3689
  });
3642
3690
  return;
3643
3691
  }
3644
- await (afterTrackedWorkDrained == null ? void 0 : afterTrackedWorkDrained());
3692
+ try {
3693
+ await (afterTrackedWorkDrained == null ? void 0 : afterTrackedWorkDrained());
3694
+ } finally {
3695
+ try {
3696
+ await closeNativeDatabaseClient(runtime, ctx);
3697
+ await closeNativeSqlDatabase(runtime, ctx);
3698
+ } finally {
3699
+ clearNativeRuntimeState(runtime, ctx);
3700
+ }
3701
+ }
3702
+ }
3703
+ async function cleanupNativeLostRuntimeState(runtime, ctx) {
3645
3704
  await closeNativeDatabaseClient(runtime, ctx);
3646
- await closeNativeSqlDatabase(runtime, ctx);
3647
- clearNativeRuntimeState(runtime, ctx);
3705
+ try {
3706
+ await closeNativeSqlDatabase(runtime, ctx);
3707
+ } finally {
3708
+ clearNativeRuntimeState(runtime, ctx);
3709
+ }
3648
3710
  }
3649
3711
  function closeNativeSqlDatabase(runtime, ctx) {
3650
3712
  const runtimeState = getNativeRuntimeState(runtime, ctx);
@@ -3662,6 +3724,9 @@ async function closeNativeDatabaseClient(runtime, ctx) {
3662
3724
  return;
3663
3725
  }
3664
3726
  runtimeState.databaseClient = void 0;
3727
+ if (callNativeSync(() => runtime.actorIsLost(ctx))) {
3728
+ return;
3729
+ }
3665
3730
  if (entry.client && typeof entry.client === "object" && "close" in entry.client && typeof entry.client.close === "function") {
3666
3731
  await entry.client.close();
3667
3732
  }
@@ -4087,6 +4152,49 @@ function wrapNativeCallback(callback) {
4087
4152
  }
4088
4153
  };
4089
4154
  }
4155
+ var LOST_GENERATION_CLEANUP_CALLBACKS = /* @__PURE__ */ new Set(["onSleep", "onDestroy"]);
4156
+ function throwIfActorLost(runtime, ctx) {
4157
+ if (callNativeSync(() => runtime.actorIsLost(ctx))) {
4158
+ throw actorGenerationLostError();
4159
+ }
4160
+ }
4161
+ function actorGenerationLostError() {
4162
+ return new RivetError(
4163
+ "actor",
4164
+ "stopping",
4165
+ "Actor generation was declared lost; it starts no new user callbacks."
4166
+ );
4167
+ }
4168
+ function refuseCallbacksAfterLost(runtime, callbacks) {
4169
+ const guard = (callback) => {
4170
+ return async (...args) => {
4171
+ var _a;
4172
+ const ctx = (_a = args[1]) == null ? void 0 : _a.ctx;
4173
+ if (ctx !== void 0 && callNativeSync(() => runtime.actorIsLost(ctx))) {
4174
+ throw encodeNativeCallbackError(actorGenerationLostError());
4175
+ }
4176
+ return await callback(...args);
4177
+ };
4178
+ };
4179
+ const guarded = {};
4180
+ for (const [name, value] of Object.entries(callbacks)) {
4181
+ if (LOST_GENERATION_CLEANUP_CALLBACKS.has(name)) {
4182
+ guarded[name] = value;
4183
+ } else if (typeof value === "function") {
4184
+ guarded[name] = guard(value);
4185
+ } else if (name === "actions" && value !== null && typeof value === "object") {
4186
+ guarded[name] = Object.fromEntries(
4187
+ Object.entries(value).map(([actionName, handler]) => [
4188
+ actionName,
4189
+ guard(handler)
4190
+ ])
4191
+ );
4192
+ } else {
4193
+ guarded[name] = value;
4194
+ }
4195
+ }
4196
+ return guarded;
4197
+ }
4090
4198
  function decodeArgs(value) {
4091
4199
  const decoded = decodeValue(value);
4092
4200
  return normalizeArgs(decoded);
@@ -4524,17 +4632,30 @@ var NativeQueueAdapter = class {
4524
4632
  this.#ctx = ctx;
4525
4633
  this.#schemas = schemas;
4526
4634
  }
4635
+ // Queue results arrive after awaits, and their schema validation is user code, so each
4636
+ // validation checks that the generation was not lost first.
4637
+ #validateBody(name, body) {
4638
+ throwIfActorLost(this.#runtime, this.#ctx);
4639
+ return validateQueueBody(this.#schemas, name, body);
4640
+ }
4641
+ #validateComplete(name, response) {
4642
+ throwIfActorLost(this.#runtime, this.#ctx);
4643
+ return validateQueueComplete(this.#schemas, name, response);
4644
+ }
4645
+ #wrap(message) {
4646
+ throwIfActorLost(this.#runtime, this.#ctx);
4647
+ return wrapQueueMessage(message, this.#schemas);
4648
+ }
4527
4649
  async send(name, body) {
4528
- const validatedBody = validateQueueBody(this.#schemas, name, body);
4529
- return wrapQueueMessage(
4650
+ const validatedBody = this.#validateBody(name, body);
4651
+ return this.#wrap(
4530
4652
  await callNative(
4531
4653
  () => this.#runtime.actorQueueSend(
4532
4654
  this.#ctx,
4533
4655
  name,
4534
4656
  encodeValue(validatedBody)
4535
4657
  )
4536
- ),
4537
- this.#schemas
4658
+ )
4538
4659
  );
4539
4660
  }
4540
4661
  async next(options) {
@@ -4577,9 +4698,7 @@ var NativeQueueAdapter = class {
4577
4698
  token
4578
4699
  )
4579
4700
  );
4580
- const wrapped = messages.map(
4581
- (message) => wrapQueueMessage(message, this.#schemas)
4582
- );
4701
+ const wrapped = messages.map((message) => this.#wrap(message));
4583
4702
  return completable ? wrapped.map(
4584
4703
  (message) => this.#makeCompletableMessage(message)
4585
4704
  ) : wrapped;
@@ -4593,7 +4712,7 @@ var NativeQueueAdapter = class {
4593
4712
  options == null ? void 0 : options.signal
4594
4713
  );
4595
4714
  try {
4596
- return wrapQueueMessage(
4715
+ return this.#wrap(
4597
4716
  await callNative(
4598
4717
  () => this.#runtime.actorQueueWaitForNames(
4599
4718
  this.#ctx,
@@ -4604,8 +4723,7 @@ var NativeQueueAdapter = class {
4604
4723
  },
4605
4724
  token
4606
4725
  )
4607
- ),
4608
- this.#schemas
4726
+ )
4609
4727
  );
4610
4728
  } finally {
4611
4729
  cleanup == null ? void 0 : cleanup();
@@ -4635,8 +4753,7 @@ var NativeQueueAdapter = class {
4635
4753
  await this.waitForNamesAvailable(names ?? [], options);
4636
4754
  }
4637
4755
  async complete(message, response) {
4638
- const validatedResponse = validateQueueComplete(
4639
- this.#schemas,
4756
+ const validatedResponse = this.#validateComplete(
4640
4757
  message.name,
4641
4758
  response
4642
4759
  );
@@ -4651,7 +4768,7 @@ var NativeQueueAdapter = class {
4651
4768
  this.#pendingCompletableMessageIds.delete(message.id.toString());
4652
4769
  }
4653
4770
  async enqueueAndWait(name, body, options) {
4654
- const validatedBody = validateQueueBody(this.#schemas, name, body);
4771
+ const validatedBody = this.#validateBody(name, body);
4655
4772
  const { token, cleanup } = await createCancellationTokenHandle(
4656
4773
  this.#runtime,
4657
4774
  options == null ? void 0 : options.signal
@@ -4668,11 +4785,7 @@ var NativeQueueAdapter = class {
4668
4785
  token
4669
4786
  )
4670
4787
  );
4671
- return response === void 0 || response === null ? void 0 : validateQueueComplete(
4672
- this.#schemas,
4673
- name,
4674
- decodeValue(response)
4675
- );
4788
+ return response === void 0 || response === null ? void 0 : this.#validateComplete(name, decodeValue(response));
4676
4789
  } finally {
4677
4790
  cleanup == null ? void 0 : cleanup();
4678
4791
  }
@@ -5053,6 +5166,9 @@ var TrackedWebSocketHandleAdapter = class {
5053
5166
  }
5054
5167
  }
5055
5168
  #callHandler(type, handler, event2) {
5169
+ if ((type === "message" || type === "open") && this.#ctx.isLost) {
5170
+ return;
5171
+ }
5056
5172
  try {
5057
5173
  const result = handler(event2);
5058
5174
  if (!this.#isPromiseLike(result)) {
@@ -5335,6 +5451,13 @@ var ActorContextHandleAdapter = class {
5335
5451
  get actorId() {
5336
5452
  return callNativeSync(() => this.#runtime.actorId(this.#ctx));
5337
5453
  }
5454
+ get actorGeneration() {
5455
+ return callNativeSync(() => this.#runtime.actorGeneration(this.#ctx));
5456
+ }
5457
+ /** Whether this generation was declared lost. A lost generation starts no new user code. */
5458
+ get isLost() {
5459
+ return callNativeSync(() => this.#runtime.actorIsLost(this.#ctx));
5460
+ }
5338
5461
  get name() {
5339
5462
  return callNativeSync(() => this.#runtime.actorName(this.#ctx));
5340
5463
  }
@@ -5449,6 +5572,9 @@ var ActorContextHandleAdapter = class {
5449
5572
  return this.#db;
5450
5573
  }
5451
5574
  const actorId = this.actorId;
5575
+ if (this.isLost) {
5576
+ throw actorGenerationLostError();
5577
+ }
5452
5578
  const createdClient = await this.#databaseProvider.createClient({
5453
5579
  actorId,
5454
5580
  kv: {
@@ -5601,9 +5727,11 @@ var ActorContextHandleAdapter = class {
5601
5727
  if (!this.#databaseProvider) {
5602
5728
  return;
5603
5729
  }
5604
- await this.#databaseProvider.onMigrate(
5605
- await this.ensureDatabaseClient()
5606
- );
5730
+ const client = await this.ensureDatabaseClient();
5731
+ if (this.isLost) {
5732
+ throw actorGenerationLostError();
5733
+ }
5734
+ await this.#databaseProvider.onMigrate(client);
5607
5735
  }
5608
5736
  async closeDatabase() {
5609
5737
  this.#db = void 0;
@@ -5875,7 +6003,7 @@ var ActorContextHandleAdapter = class {
5875
6003
  callNativeSync(
5876
6004
  () => this.#runtime.actorRequestSave(this.#ctx, { immediate: false })
5877
6005
  );
5878
- if (!this.#onStateChange) {
6006
+ if (!this.#onStateChange || this.isLost) {
5879
6007
  return;
5880
6008
  }
5881
6009
  actorState.isInOnStateChange = true;
@@ -5915,6 +6043,12 @@ var NativeWorkflowRuntimeAdapter = class {
5915
6043
  #onMessagesReceived;
5916
6044
  #completions = /* @__PURE__ */ new Map();
5917
6045
  id;
6046
+ /** Actor generation, used to keep workflow registrations of different generations apart. */
6047
+ generation;
6048
+ /** Whether this generation was declared lost. Workflow code checks it before each user callback. */
6049
+ get isLost() {
6050
+ return this.#ctx.isLost;
6051
+ }
5918
6052
  driver;
5919
6053
  queueManager;
5920
6054
  stateManager;
@@ -5922,6 +6056,7 @@ var NativeWorkflowRuntimeAdapter = class {
5922
6056
  this.#ctx = ctx;
5923
6057
  this.#onMessagesReceived = onMessagesReceived;
5924
6058
  this.id = ctx.actorId;
6059
+ this.generation = ctx.actorGeneration;
5925
6060
  this.driver = {
5926
6061
  kvBatchGet: async (actorId, keys) => {
5927
6062
  this.#assertActorId(actorId);
@@ -6204,9 +6339,23 @@ function buildNativeFactory(runtime, registryConfig, definition) {
6204
6339
  const runHandlerCoordinator = getRunInspectorKind(config.run) === "workflow" ? new RunHandlerCoordinator(config.run) : void 0;
6205
6340
  const getNativeWorkflowInspector = (ctx) => {
6206
6341
  var _a2, _b;
6342
+ if (callNativeSync(() => runtime.actorIsLost(ctx))) {
6343
+ return void 0;
6344
+ }
6207
6345
  const actorId = callNativeSync(() => runtime.actorId(ctx));
6208
6346
  const restart = () => callNativeSync(() => runtime.actorRestartRunHandler(ctx));
6209
- return ((_a2 = runHandlerCoordinator == null ? void 0 : runHandlerCoordinator.getInspector(actorId, restart)) == null ? void 0 : _a2.workflow) ?? ((_b = getRunInspectorConfig(config.run, actorId)) == null ? void 0 : _b.workflow);
6347
+ const actorGeneration = callNativeSync(
6348
+ () => runtime.actorGeneration(ctx)
6349
+ );
6350
+ const inspector = ((_a2 = runHandlerCoordinator == null ? void 0 : runHandlerCoordinator.getInspector(
6351
+ actorId,
6352
+ restart,
6353
+ actorGeneration
6354
+ )) == null ? void 0 : _a2.workflow) ?? ((_b = getRunInspectorConfig(config.run, actorId)) == null ? void 0 : _b.workflow);
6355
+ if (callNativeSync(() => runtime.actorIsLost(ctx))) {
6356
+ return void 0;
6357
+ }
6358
+ return inspector;
6210
6359
  };
6211
6360
  const onStateChange = typeof config.onStateChange === "function" ? (actorCtx, nextState) => {
6212
6361
  config.onStateChange(actorCtx, nextState);
@@ -6283,6 +6432,10 @@ function buildNativeFactory(runtime, registryConfig, definition) {
6283
6432
  return errorResponse(error, 401);
6284
6433
  }
6285
6434
  }
6435
+ const inspectorConnParams = (conn) => {
6436
+ throwIfActorLost(runtime, ctx);
6437
+ return conn.params;
6438
+ };
6286
6439
  const workflowHistory = () => {
6287
6440
  var _a3;
6288
6441
  return serializeWorkflowHistoryForJson(
@@ -6316,7 +6469,7 @@ function buildNativeFactory(runtime, registryConfig, definition) {
6316
6469
  id: conn.id,
6317
6470
  details: {
6318
6471
  type: null,
6319
- params: conn.params,
6472
+ params: inspectorConnParams(conn),
6320
6473
  stateEnabled: true,
6321
6474
  state: conn.state,
6322
6475
  subscriptions: 0,
@@ -6520,7 +6673,7 @@ function buildNativeFactory(runtime, registryConfig, definition) {
6520
6673
  id: conn.id,
6521
6674
  details: {
6522
6675
  type: null,
6523
- params: conn.params,
6676
+ params: inspectorConnParams(conn),
6524
6677
  stateEnabled: true,
6525
6678
  state: conn.state,
6526
6679
  subscriptions: 0,
@@ -6567,15 +6720,20 @@ function buildNativeFactory(runtime, registryConfig, definition) {
6567
6720
  );
6568
6721
  }
6569
6722
  const args = body.properties !== void 0 ? [body.properties] : normalizeArgs(body.args);
6723
+ if (callNativeSync(() => runtime.actorIsLost(ctx))) {
6724
+ return jsonResponse(
6725
+ { error: "actor generation was declared lost" },
6726
+ { status: 503 }
6727
+ );
6728
+ }
6570
6729
  try {
6571
- const output = await action(
6572
- actorCtx,
6573
- ...validateActionArgs(
6574
- schemaConfig.actionInputSchemas,
6575
- actionName,
6576
- args
6577
- )
6730
+ const validatedArgs = validateActionArgs(
6731
+ schemaConfig.actionInputSchemas,
6732
+ actionName,
6733
+ args
6578
6734
  );
6735
+ throwIfActorLost(runtime, ctx);
6736
+ const output = await action(actorCtx, ...validatedArgs);
6579
6737
  return jsonResponse({ output });
6580
6738
  } catch (error) {
6581
6739
  logger2().error({
@@ -6679,7 +6837,9 @@ function buildNativeFactory(runtime, registryConfig, definition) {
6679
6837
  await actorCtx.closeDatabase();
6680
6838
  }
6681
6839
  await actorCtx.runDatabaseMigrations();
6682
- if (typeof config.onMigrate === "function") {
6840
+ if (typeof config.onMigrate === "function" && !callNativeSync(
6841
+ () => runtime.actorIsLost(ctx)
6842
+ )) {
6683
6843
  await config.onMigrate(actorCtx, isNew);
6684
6844
  }
6685
6845
  } catch (error2) {
@@ -6722,7 +6882,29 @@ function buildNativeFactory(runtime, registryConfig, definition) {
6722
6882
  const { ctx } = unwrapTsfnPayload(error, payload);
6723
6883
  const actorCtx = makeActorCtx(ctx);
6724
6884
  const actorId = callNativeSync(() => runtime.actorId(ctx));
6885
+ const actorGeneration = callNativeSync(
6886
+ () => runtime.actorGeneration(ctx)
6887
+ );
6888
+ const isLost = () => callNativeSync(() => runtime.actorIsLost(ctx));
6889
+ if (isLost()) {
6890
+ try {
6891
+ await cleanupNativeLostRuntimeState(runtime, ctx);
6892
+ } finally {
6893
+ runHandlerCoordinator == null ? void 0 : runHandlerCoordinator.destroy(
6894
+ actorId,
6895
+ actorGeneration,
6896
+ {
6897
+ disposeInspector: false
6898
+ }
6899
+ );
6900
+ await actorCtx.dispose();
6901
+ }
6902
+ return;
6903
+ }
6725
6904
  const saveActorState = async () => {
6905
+ if (isLost()) {
6906
+ return;
6907
+ }
6726
6908
  if (runtime.kind === "wasm") {
6727
6909
  await runtime.actorSaveState(
6728
6910
  ctx,
@@ -6741,14 +6923,26 @@ function buildNativeFactory(runtime, registryConfig, definition) {
6741
6923
  await saveActorState();
6742
6924
  } finally {
6743
6925
  try {
6744
- await cleanupNativeSleepRuntimeState(
6745
- runtime,
6746
- ctx,
6747
- saveActorState
6748
- );
6926
+ if (isLost()) {
6927
+ await cleanupNativeLostRuntimeState(runtime, ctx);
6928
+ } else {
6929
+ await cleanupNativeSleepRuntimeState(
6930
+ runtime,
6931
+ ctx,
6932
+ saveActorState
6933
+ );
6934
+ }
6749
6935
  } finally {
6750
- runHandlerCoordinator == null ? void 0 : runHandlerCoordinator.destroy(actorId);
6751
- disposeRunInspector(config.run, actorId);
6936
+ runHandlerCoordinator == null ? void 0 : runHandlerCoordinator.destroy(
6937
+ actorId,
6938
+ actorGeneration,
6939
+ {
6940
+ disposeInspector: !isLost()
6941
+ }
6942
+ );
6943
+ if (!isLost()) {
6944
+ disposeRunInspector(config.run, actorId);
6945
+ }
6752
6946
  await actorCtx.dispose();
6753
6947
  }
6754
6948
  }
@@ -6759,10 +6953,17 @@ function buildNativeFactory(runtime, registryConfig, definition) {
6759
6953
  const { ctx } = unwrapTsfnPayload(error, payload);
6760
6954
  const actorCtx = makeActorCtx(ctx);
6761
6955
  const actorId = callNativeSync(() => runtime.actorId(ctx));
6762
- runHandlerCoordinator == null ? void 0 : runHandlerCoordinator.destroy(actorId);
6763
- disposeRunInspector(config.run, actorId);
6956
+ const isLost = () => callNativeSync(() => runtime.actorIsLost(ctx));
6957
+ runHandlerCoordinator == null ? void 0 : runHandlerCoordinator.destroy(
6958
+ actorId,
6959
+ callNativeSync(() => runtime.actorGeneration(ctx)),
6960
+ { disposeInspector: !isLost() }
6961
+ );
6962
+ if (!isLost()) {
6963
+ disposeRunInspector(config.run, actorId);
6964
+ }
6764
6965
  try {
6765
- if (typeof config.onDestroy === "function") {
6966
+ if (typeof config.onDestroy === "function" && !isLost()) {
6766
6967
  await config.onDestroy(actorCtx);
6767
6968
  }
6768
6969
  } finally {
@@ -6784,12 +6985,14 @@ function buildNativeFactory(runtime, registryConfig, definition) {
6784
6985
  request ? buildNativeHttpRequest(request) : void 0
6785
6986
  );
6786
6987
  try {
6988
+ const validatedParams = validateConnParams(
6989
+ schemaConfig.connParamsSchema,
6990
+ decodeValue(params)
6991
+ );
6992
+ throwIfActorLost(runtime, ctx);
6787
6993
  await config.onBeforeConnect(
6788
6994
  actorCtx,
6789
- validateConnParams(
6790
- schemaConfig.connParamsSchema,
6791
- decodeValue(params)
6792
- )
6995
+ validatedParams
6793
6996
  );
6794
6997
  } finally {
6795
6998
  await actorCtx.dispose();
@@ -6817,13 +7020,22 @@ function buildNativeFactory(runtime, registryConfig, definition) {
6817
7020
  () => actorCtx.assertCanMutateState()
6818
7021
  );
6819
7022
  try {
6820
- const nextConnState = hasStaticConnState ? structuredClone(config.connState) : await config.createConnState(
6821
- actorCtx,
6822
- validateConnParams(
7023
+ let nextConnState;
7024
+ if (hasStaticConnState) {
7025
+ nextConnState = structuredClone(
7026
+ config.connState
7027
+ );
7028
+ } else {
7029
+ const validatedParams = validateConnParams(
6823
7030
  schemaConfig.connParamsSchema,
6824
7031
  decodeValue(params)
6825
- )
6826
- );
7032
+ );
7033
+ throwIfActorLost(runtime, ctx);
7034
+ nextConnState = await config.createConnState(
7035
+ actorCtx,
7036
+ validatedParams
7037
+ );
7038
+ }
6827
7039
  connAdapter.initializeState(nextConnState);
6828
7040
  return encodeValue(nextConnState);
6829
7041
  } finally {
@@ -6928,6 +7140,9 @@ function buildNativeFactory(runtime, registryConfig, definition) {
6928
7140
  onBeforeActionResponse: typeof config.onBeforeActionResponse === "function" ? wrapNativeCallback(
6929
7141
  async (error, payload) => {
6930
7142
  const { ctx, name, args, output } = unwrapTsfnPayload(error, payload);
7143
+ if (callNativeSync(() => runtime.actorIsLost(ctx))) {
7144
+ return output;
7145
+ }
6931
7146
  const actorCtx = makeActorCtx(ctx);
6932
7147
  try {
6933
7148
  return encodeValue(
@@ -6996,6 +7211,7 @@ function buildNativeFactory(runtime, registryConfig, definition) {
6996
7211
  }
6997
7212
  };
6998
7213
  try {
7214
+ throwIfActorLost(runtime, ctx);
6999
7215
  const connParams = validateConnParams(
7000
7216
  schemaConfig.connParamsSchema,
7001
7217
  rawConnParams ? JSON.parse(rawConnParams) : void 0
@@ -7030,6 +7246,11 @@ function buildNativeFactory(runtime, registryConfig, definition) {
7030
7246
  abortRequest
7031
7247
  );
7032
7248
  }
7249
+ if (callNativeSync(
7250
+ () => runtime.actorIsLost(ctx)
7251
+ )) {
7252
+ throw actorGenerationLostError();
7253
+ }
7033
7254
  const response = await config.onRequest(
7034
7255
  requestCtx,
7035
7256
  handlerRequest
@@ -7102,6 +7323,9 @@ function buildNativeFactory(runtime, registryConfig, definition) {
7102
7323
  const { ctx } = unwrapTsfnPayload(error, payload);
7103
7324
  const actorId = callNativeSync(() => runtime.actorId(ctx));
7104
7325
  const executeRun = async () => {
7326
+ if (callNativeSync(() => runtime.actorIsLost(ctx))) {
7327
+ return;
7328
+ }
7105
7329
  const actorCtx = makeActorCtx(ctx);
7106
7330
  try {
7107
7331
  await run(actorCtx);
@@ -7115,7 +7339,8 @@ function buildNativeFactory(runtime, registryConfig, definition) {
7115
7339
  () => callNativeSync(
7116
7340
  () => runtime.actorRestartRunHandler(ctx)
7117
7341
  ),
7118
- executeRun
7342
+ executeRun,
7343
+ callNativeSync(() => runtime.actorGeneration(ctx))
7119
7344
  );
7120
7345
  return outcome !== "suppressed";
7121
7346
  }
@@ -7155,14 +7380,16 @@ function buildNativeFactory(runtime, registryConfig, definition) {
7155
7380
  const actorCtx = conn != null ? makeConnCtx(ctx, conn, void 0, cancelToken) : makeActorCtx(ctx, void 0, cancelToken);
7156
7381
  const runAction = async () => {
7157
7382
  try {
7383
+ const validatedArgs = validateActionArgs(
7384
+ schemaConfig.actionInputSchemas,
7385
+ name,
7386
+ decodeArgs(args)
7387
+ );
7388
+ throwIfActorLost(runtime, ctx);
7158
7389
  return encodeValue(
7159
7390
  await handler(
7160
7391
  actorCtx,
7161
- ...validateActionArgs(
7162
- schemaConfig.actionInputSchemas,
7163
- name,
7164
- decodeArgs(args)
7165
- ),
7392
+ ...validatedArgs,
7166
7393
  ...scheduledFire ? [scheduledFire] : []
7167
7394
  )
7168
7395
  );
@@ -7218,6 +7445,7 @@ function buildNativeFactory(runtime, registryConfig, definition) {
7218
7445
  if (canPublish && !await canPublish(actorCtx)) {
7219
7446
  throw forbiddenError();
7220
7447
  }
7448
+ throwIfActorLost(runtime, ctx);
7221
7449
  const decodedBody = decodeValue(body);
7222
7450
  if (wait) {
7223
7451
  try {
@@ -7261,7 +7489,7 @@ function buildNativeFactory(runtime, registryConfig, definition) {
7261
7489
  )
7262
7490
  };
7263
7491
  return runtime.createActorFactory(
7264
- callbacks,
7492
+ refuseCallbacksAfterLost(runtime, callbacks),
7265
7493
  buildActorConfig(definition, registryConfig, runtime.kind)
7266
7494
  );
7267
7495
  }