@rivetkit/supabase 2.3.23 → 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 (3) hide show
  1. package/dist/mod.js +321 -93
  2. package/dist/mod.mjs +321 -93
  3. package/package.json +3 -3
package/dist/mod.mjs CHANGED
@@ -500,7 +500,7 @@ function unsupportedFeature(feature) {
500
500
  );
501
501
  }
502
502
 
503
- // ../rivetkit/dist/tsup/chunk-674WU4L6.js
503
+ // ../rivetkit/dist/tsup/chunk-JJCUSUWO.js
504
504
  import {
505
505
  pino,
506
506
  stdTimeFunctions
@@ -13176,7 +13176,7 @@ var classic_default = external_exports;
13176
13176
  // ../../../node_modules/.pnpm/zod@4.1.13/node_modules/zod/v4/index.js
13177
13177
  var v4_default = classic_default;
13178
13178
 
13179
- // ../rivetkit/dist/tsup/chunk-674WU4L6.js
13179
+ // ../rivetkit/dist/tsup/chunk-JJCUSUWO.js
13180
13180
  var import_invariant = __toESM(require_invariant(), 1);
13181
13181
  import * as cbor from "cbor-x";
13182
13182
  var getRivetEngine = () => getEnvUniversal("RIVET_ENGINE");
@@ -13347,7 +13347,7 @@ function noopNext() {
13347
13347
  }
13348
13348
  var package_default = {
13349
13349
  name: "rivetkit",
13350
- version: "2.3.23",
13350
+ version: "2.3.24-rc.1",
13351
13351
  description: "Lightweight libraries for building stateful actors on edge platforms",
13352
13352
  license: "Apache-2.0",
13353
13353
  keywords: [
@@ -14654,7 +14654,7 @@ function Config({ initialBufferLength = 1024, maxBufferLength = 1024 * 1024 * 32
14654
14654
  };
14655
14655
  }
14656
14656
 
14657
- // ../rivetkit/dist/tsup/chunk-Z3Z5EOSP.js
14657
+ // ../rivetkit/dist/tsup/chunk-M5J3PXNU.js
14658
14658
  var config2 = /* @__PURE__ */ Config({});
14659
14659
  function readWorkflowCbor(bc) {
14660
14660
  return readData(bc);
@@ -14952,7 +14952,7 @@ function decodeWorkflowHistoryTransport(data) {
14952
14952
  return decodeWorkflowHistory(toUint8Array(data));
14953
14953
  }
14954
14954
 
14955
- // ../rivetkit/dist/tsup/chunk-CSOHAAZK.js
14955
+ // ../rivetkit/dist/tsup/chunk-LTGBHN5Z.js
14956
14956
  function flattenActionHandlers(actions) {
14957
14957
  const flattened = /* @__PURE__ */ Object.create(null);
14958
14958
  for (const { name, handler } of collectActionEntries(actions)) {
@@ -15647,7 +15647,7 @@ var AsyncMutex = class {
15647
15647
  }
15648
15648
  };
15649
15649
 
15650
- // ../rivetkit/dist/tsup/chunk-KC3T3MMB.js
15650
+ // ../rivetkit/dist/tsup/chunk-OB5JLDBM.js
15651
15651
  function logger() {
15652
15652
  return getLogger("actor-client");
15653
15653
  }
@@ -15685,7 +15685,7 @@ async function importWebSocket() {
15685
15685
  return webSocketPromise;
15686
15686
  }
15687
15687
 
15688
- // ../rivetkit/dist/tsup/chunk-LKNGMRIT.js
15688
+ // ../rivetkit/dist/tsup/chunk-B6WTPGOR.js
15689
15689
  var import_invariant2 = __toESM(require_invariant(), 1);
15690
15690
 
15691
15691
  // ../../../node_modules/.pnpm/p-retry@6.2.1/node_modules/p-retry/index.js
@@ -16900,7 +16900,7 @@ var TraceAPI = (
16900
16900
  // ../../../node_modules/.pnpm/@opentelemetry+api@1.9.0/node_modules/@opentelemetry/api/build/esm/trace-api.js
16901
16901
  var trace = TraceAPI.getInstance();
16902
16902
 
16903
- // ../rivetkit/dist/tsup/chunk-LKNGMRIT.js
16903
+ // ../rivetkit/dist/tsup/chunk-B6WTPGOR.js
16904
16904
  var import_invariant3 = __toESM(require_invariant(), 1);
16905
16905
  var import_invariant4 = __toESM(require_invariant(), 1);
16906
16906
  var PATH_CONNECT = "/connect";
@@ -23077,7 +23077,7 @@ function apiActorToOutput(actor2) {
23077
23077
  };
23078
23078
  }
23079
23079
 
23080
- // ../rivetkit/dist/tsup/chunk-HO6UA2RJ.js
23080
+ // ../rivetkit/dist/tsup/chunk-QQ454DVR.js
23081
23081
  var nativeStateTransactionOpeners = /* @__PURE__ */ new WeakMap();
23082
23082
  var nativeStateTransactionClientBinders = /* @__PURE__ */ new WeakMap();
23083
23083
  function registerNativeStateTransactionOpener(provider, opener) {
@@ -26136,6 +26136,12 @@ var NapiCoreRuntime = class {
26136
26136
  actorId(ctx) {
26137
26137
  return asNativeActorContext(ctx).actorId();
26138
26138
  }
26139
+ actorGeneration(ctx) {
26140
+ return asNativeActorContext(ctx).generation() ?? void 0;
26141
+ }
26142
+ actorIsLost(ctx) {
26143
+ return asNativeActorContext(ctx).isLost();
26144
+ }
26139
26145
  runWithActorInvocationContext(ctx, run) {
26140
26146
  return this.#runAs(asNativeActorContext(ctx), run);
26141
26147
  }
@@ -26749,6 +26755,27 @@ function notifyStateChanged(state) {
26749
26755
  waiter();
26750
26756
  }
26751
26757
  }
26758
+ function createRunState(actorGeneration) {
26759
+ return {
26760
+ actorGeneration,
26761
+ active: false,
26762
+ closed: false,
26763
+ exclusive: false,
26764
+ exclusiveGeneration: 0,
26765
+ inspectorInitialized: false,
26766
+ queued: 0,
26767
+ waiters: /* @__PURE__ */ new Set()
26768
+ };
26769
+ }
26770
+ function closeRunState(state, disposeInspector = true) {
26771
+ var _a2, _b;
26772
+ state.closed = true;
26773
+ if (disposeInspector) {
26774
+ (_b = (_a2 = state.inspector) == null ? void 0 : _a2.dispose) == null ? void 0 : _b.call(_a2);
26775
+ }
26776
+ state.inspector = void 0;
26777
+ notifyStateChanged(state);
26778
+ }
26752
26779
  function waitForStateChange(state) {
26753
26780
  return new Promise((resolve) => {
26754
26781
  state.waiters.add(resolve);
@@ -26763,8 +26790,11 @@ var RunHandlerCoordinator = class {
26763
26790
  get inspectorKind() {
26764
26791
  return getRunInspectorKind(this.#run);
26765
26792
  }
26766
- async run(actorId, restart, callback) {
26767
- const state = this.#getOrCreate(actorId);
26793
+ async run(actorId, restart, callback, actorGeneration) {
26794
+ const state = this.#getOrCreate(actorId, actorGeneration);
26795
+ if (state.closed) {
26796
+ return "closed";
26797
+ }
26768
26798
  state.restart = restart;
26769
26799
  this.#initializeInspector(actorId, state);
26770
26800
  state.queued += 1;
@@ -26791,21 +26821,27 @@ var RunHandlerCoordinator = class {
26791
26821
  notifyStateChanged(state);
26792
26822
  }
26793
26823
  }
26794
- getInspector(actorId, restart) {
26824
+ getInspector(actorId, restart, actorGeneration) {
26795
26825
  var _a2;
26796
- const state = this.#getOrCreate(actorId);
26826
+ const state = this.#getOrCreate(actorId, actorGeneration);
26827
+ if (state.closed) {
26828
+ return void 0;
26829
+ }
26797
26830
  state.restart = restart;
26798
26831
  this.#initializeInspector(actorId, state);
26799
26832
  return (_a2 = state.inspector) == null ? void 0 : _a2.inspector;
26800
26833
  }
26801
- destroy(actorId) {
26802
- var _a2, _b;
26834
+ /**
26835
+ * Closes the run state for `actorId`. With `actorGeneration`, only state owned by that
26836
+ * generation is closed, so an older generation's late cleanup cannot close a newer one.
26837
+ */
26838
+ destroy(actorId, actorGeneration, options = {}) {
26803
26839
  const state = this.#states.get(actorId);
26804
26840
  if (!state) return;
26805
- state.closed = true;
26806
- (_b = (_a2 = state.inspector) == null ? void 0 : _a2.dispose) == null ? void 0 : _b.call(_a2);
26807
- state.inspector = void 0;
26808
- notifyStateChanged(state);
26841
+ if (actorGeneration !== void 0 && state.actorGeneration !== void 0 && state.actorGeneration !== actorGeneration) {
26842
+ return;
26843
+ }
26844
+ closeRunState(state, options.disposeInspector ?? true);
26809
26845
  this.#states.delete(actorId);
26810
26846
  }
26811
26847
  #control(actorId, state) {
@@ -26851,18 +26887,20 @@ var RunHandlerCoordinator = class {
26851
26887
  }
26852
26888
  };
26853
26889
  }
26854
- #getOrCreate(actorId) {
26890
+ #getOrCreate(actorId, actorGeneration) {
26855
26891
  let state = this.#states.get(actorId);
26892
+ if (state && actorGeneration !== void 0 && state.actorGeneration !== void 0 && state.actorGeneration !== actorGeneration) {
26893
+ if (actorGeneration < state.actorGeneration) {
26894
+ const detached = createRunState(actorGeneration);
26895
+ detached.closed = true;
26896
+ return detached;
26897
+ }
26898
+ closeRunState(state, false);
26899
+ this.#states.delete(actorId);
26900
+ state = void 0;
26901
+ }
26856
26902
  if (!state) {
26857
- state = {
26858
- active: false,
26859
- closed: false,
26860
- exclusive: false,
26861
- exclusiveGeneration: 0,
26862
- inspectorInitialized: false,
26863
- queued: 0,
26864
- waiters: /* @__PURE__ */ new Set()
26865
- };
26903
+ state = createRunState(actorGeneration);
26866
26904
  this.#states.set(actorId, state);
26867
26905
  }
26868
26906
  return state;
@@ -26871,6 +26909,7 @@ var RunHandlerCoordinator = class {
26871
26909
  if (state.inspectorInitialized) return;
26872
26910
  const inspector = createRunInspector(this.#run, {
26873
26911
  actorId,
26912
+ actorGeneration: state.actorGeneration,
26874
26913
  control: this.#control(actorId, state)
26875
26914
  });
26876
26915
  if (this.inspectorKind === "workflow") {
@@ -27196,6 +27235,12 @@ var WasmCoreRuntime = class {
27196
27235
  actorId(ctx) {
27197
27236
  return callHandle(asWasmActorContext(ctx), "actorId");
27198
27237
  }
27238
+ actorGeneration(ctx) {
27239
+ return callHandle(asWasmActorContext(ctx), "generation") ?? void 0;
27240
+ }
27241
+ actorIsLost(ctx) {
27242
+ return callHandle(asWasmActorContext(ctx), "isLost");
27243
+ }
27199
27244
  runWithActorInvocationContext(_ctx, run) {
27200
27245
  return run();
27201
27246
  }
@@ -28022,8 +28067,11 @@ async function cleanupNativeSleepRuntimeState(runtime, ctx, afterTrackedWorkDrai
28022
28067
  await closeNativeDatabaseClient(runtime, ctx);
28023
28068
  await closeNativeSqlDatabase(runtime, ctx);
28024
28069
  void runtime.actorWaitForTrackedShutdownWorkUnbounded(ctx).then(async () => {
28025
- await (afterTrackedWorkDrained == null ? void 0 : afterTrackedWorkDrained());
28026
- clearNativeRuntimeState(runtime, ctx);
28070
+ try {
28071
+ await (afterTrackedWorkDrained == null ? void 0 : afterTrackedWorkDrained());
28072
+ } finally {
28073
+ clearNativeRuntimeState(runtime, ctx);
28074
+ }
28027
28075
  }).catch((error46) => {
28028
28076
  logger22().warn({
28029
28077
  msg: "deferred native sleep cleanup failed",
@@ -28032,10 +28080,24 @@ async function cleanupNativeSleepRuntimeState(runtime, ctx, afterTrackedWorkDrai
28032
28080
  });
28033
28081
  return;
28034
28082
  }
28035
- await (afterTrackedWorkDrained == null ? void 0 : afterTrackedWorkDrained());
28083
+ try {
28084
+ await (afterTrackedWorkDrained == null ? void 0 : afterTrackedWorkDrained());
28085
+ } finally {
28086
+ try {
28087
+ await closeNativeDatabaseClient(runtime, ctx);
28088
+ await closeNativeSqlDatabase(runtime, ctx);
28089
+ } finally {
28090
+ clearNativeRuntimeState(runtime, ctx);
28091
+ }
28092
+ }
28093
+ }
28094
+ async function cleanupNativeLostRuntimeState(runtime, ctx) {
28036
28095
  await closeNativeDatabaseClient(runtime, ctx);
28037
- await closeNativeSqlDatabase(runtime, ctx);
28038
- clearNativeRuntimeState(runtime, ctx);
28096
+ try {
28097
+ await closeNativeSqlDatabase(runtime, ctx);
28098
+ } finally {
28099
+ clearNativeRuntimeState(runtime, ctx);
28100
+ }
28039
28101
  }
28040
28102
  function closeNativeSqlDatabase(runtime, ctx) {
28041
28103
  const runtimeState = getNativeRuntimeState(runtime, ctx);
@@ -28053,6 +28115,9 @@ async function closeNativeDatabaseClient(runtime, ctx) {
28053
28115
  return;
28054
28116
  }
28055
28117
  runtimeState.databaseClient = void 0;
28118
+ if (callNativeSync(() => runtime.actorIsLost(ctx))) {
28119
+ return;
28120
+ }
28056
28121
  if (entry.client && typeof entry.client === "object" && "close" in entry.client && typeof entry.client.close === "function") {
28057
28122
  await entry.client.close();
28058
28123
  }
@@ -28478,6 +28543,49 @@ function wrapNativeCallback(callback) {
28478
28543
  }
28479
28544
  };
28480
28545
  }
28546
+ var LOST_GENERATION_CLEANUP_CALLBACKS = /* @__PURE__ */ new Set(["onSleep", "onDestroy"]);
28547
+ function throwIfActorLost(runtime, ctx) {
28548
+ if (callNativeSync(() => runtime.actorIsLost(ctx))) {
28549
+ throw actorGenerationLostError();
28550
+ }
28551
+ }
28552
+ function actorGenerationLostError() {
28553
+ return new RivetError(
28554
+ "actor",
28555
+ "stopping",
28556
+ "Actor generation was declared lost; it starts no new user callbacks."
28557
+ );
28558
+ }
28559
+ function refuseCallbacksAfterLost(runtime, callbacks) {
28560
+ const guard = (callback) => {
28561
+ return async (...args) => {
28562
+ var _a2;
28563
+ const ctx = (_a2 = args[1]) == null ? void 0 : _a2.ctx;
28564
+ if (ctx !== void 0 && callNativeSync(() => runtime.actorIsLost(ctx))) {
28565
+ throw encodeNativeCallbackError(actorGenerationLostError());
28566
+ }
28567
+ return await callback(...args);
28568
+ };
28569
+ };
28570
+ const guarded = {};
28571
+ for (const [name, value] of Object.entries(callbacks)) {
28572
+ if (LOST_GENERATION_CLEANUP_CALLBACKS.has(name)) {
28573
+ guarded[name] = value;
28574
+ } else if (typeof value === "function") {
28575
+ guarded[name] = guard(value);
28576
+ } else if (name === "actions" && value !== null && typeof value === "object") {
28577
+ guarded[name] = Object.fromEntries(
28578
+ Object.entries(value).map(([actionName, handler]) => [
28579
+ actionName,
28580
+ guard(handler)
28581
+ ])
28582
+ );
28583
+ } else {
28584
+ guarded[name] = value;
28585
+ }
28586
+ }
28587
+ return guarded;
28588
+ }
28481
28589
  function decodeArgs(value) {
28482
28590
  const decoded = decodeValue(value);
28483
28591
  return normalizeArgs(decoded);
@@ -28915,17 +29023,30 @@ var NativeQueueAdapter = class {
28915
29023
  this.#ctx = ctx;
28916
29024
  this.#schemas = schemas;
28917
29025
  }
29026
+ // Queue results arrive after awaits, and their schema validation is user code, so each
29027
+ // validation checks that the generation was not lost first.
29028
+ #validateBody(name, body) {
29029
+ throwIfActorLost(this.#runtime, this.#ctx);
29030
+ return validateQueueBody(this.#schemas, name, body);
29031
+ }
29032
+ #validateComplete(name, response) {
29033
+ throwIfActorLost(this.#runtime, this.#ctx);
29034
+ return validateQueueComplete(this.#schemas, name, response);
29035
+ }
29036
+ #wrap(message) {
29037
+ throwIfActorLost(this.#runtime, this.#ctx);
29038
+ return wrapQueueMessage(message, this.#schemas);
29039
+ }
28918
29040
  async send(name, body) {
28919
- const validatedBody = validateQueueBody(this.#schemas, name, body);
28920
- return wrapQueueMessage(
29041
+ const validatedBody = this.#validateBody(name, body);
29042
+ return this.#wrap(
28921
29043
  await callNative(
28922
29044
  () => this.#runtime.actorQueueSend(
28923
29045
  this.#ctx,
28924
29046
  name,
28925
29047
  encodeValue(validatedBody)
28926
29048
  )
28927
- ),
28928
- this.#schemas
29049
+ )
28929
29050
  );
28930
29051
  }
28931
29052
  async next(options) {
@@ -28968,9 +29089,7 @@ var NativeQueueAdapter = class {
28968
29089
  token
28969
29090
  )
28970
29091
  );
28971
- const wrapped = messages.map(
28972
- (message) => wrapQueueMessage(message, this.#schemas)
28973
- );
29092
+ const wrapped = messages.map((message) => this.#wrap(message));
28974
29093
  return completable ? wrapped.map(
28975
29094
  (message) => this.#makeCompletableMessage(message)
28976
29095
  ) : wrapped;
@@ -28984,7 +29103,7 @@ var NativeQueueAdapter = class {
28984
29103
  options == null ? void 0 : options.signal
28985
29104
  );
28986
29105
  try {
28987
- return wrapQueueMessage(
29106
+ return this.#wrap(
28988
29107
  await callNative(
28989
29108
  () => this.#runtime.actorQueueWaitForNames(
28990
29109
  this.#ctx,
@@ -28995,8 +29114,7 @@ var NativeQueueAdapter = class {
28995
29114
  },
28996
29115
  token
28997
29116
  )
28998
- ),
28999
- this.#schemas
29117
+ )
29000
29118
  );
29001
29119
  } finally {
29002
29120
  cleanup == null ? void 0 : cleanup();
@@ -29026,8 +29144,7 @@ var NativeQueueAdapter = class {
29026
29144
  await this.waitForNamesAvailable(names ?? [], options);
29027
29145
  }
29028
29146
  async complete(message, response) {
29029
- const validatedResponse = validateQueueComplete(
29030
- this.#schemas,
29147
+ const validatedResponse = this.#validateComplete(
29031
29148
  message.name,
29032
29149
  response
29033
29150
  );
@@ -29042,7 +29159,7 @@ var NativeQueueAdapter = class {
29042
29159
  this.#pendingCompletableMessageIds.delete(message.id.toString());
29043
29160
  }
29044
29161
  async enqueueAndWait(name, body, options) {
29045
- const validatedBody = validateQueueBody(this.#schemas, name, body);
29162
+ const validatedBody = this.#validateBody(name, body);
29046
29163
  const { token, cleanup } = await createCancellationTokenHandle(
29047
29164
  this.#runtime,
29048
29165
  options == null ? void 0 : options.signal
@@ -29059,11 +29176,7 @@ var NativeQueueAdapter = class {
29059
29176
  token
29060
29177
  )
29061
29178
  );
29062
- return response === void 0 || response === null ? void 0 : validateQueueComplete(
29063
- this.#schemas,
29064
- name,
29065
- decodeValue(response)
29066
- );
29179
+ return response === void 0 || response === null ? void 0 : this.#validateComplete(name, decodeValue(response));
29067
29180
  } finally {
29068
29181
  cleanup == null ? void 0 : cleanup();
29069
29182
  }
@@ -29444,6 +29557,9 @@ var TrackedWebSocketHandleAdapter = class {
29444
29557
  }
29445
29558
  }
29446
29559
  #callHandler(type, handler, event2) {
29560
+ if ((type === "message" || type === "open") && this.#ctx.isLost) {
29561
+ return;
29562
+ }
29447
29563
  try {
29448
29564
  const result = handler(event2);
29449
29565
  if (!this.#isPromiseLike(result)) {
@@ -29726,6 +29842,13 @@ var ActorContextHandleAdapter = class {
29726
29842
  get actorId() {
29727
29843
  return callNativeSync(() => this.#runtime.actorId(this.#ctx));
29728
29844
  }
29845
+ get actorGeneration() {
29846
+ return callNativeSync(() => this.#runtime.actorGeneration(this.#ctx));
29847
+ }
29848
+ /** Whether this generation was declared lost. A lost generation starts no new user code. */
29849
+ get isLost() {
29850
+ return callNativeSync(() => this.#runtime.actorIsLost(this.#ctx));
29851
+ }
29729
29852
  get name() {
29730
29853
  return callNativeSync(() => this.#runtime.actorName(this.#ctx));
29731
29854
  }
@@ -29840,6 +29963,9 @@ var ActorContextHandleAdapter = class {
29840
29963
  return this.#db;
29841
29964
  }
29842
29965
  const actorId = this.actorId;
29966
+ if (this.isLost) {
29967
+ throw actorGenerationLostError();
29968
+ }
29843
29969
  const createdClient = await this.#databaseProvider.createClient({
29844
29970
  actorId,
29845
29971
  kv: {
@@ -29992,9 +30118,11 @@ var ActorContextHandleAdapter = class {
29992
30118
  if (!this.#databaseProvider) {
29993
30119
  return;
29994
30120
  }
29995
- await this.#databaseProvider.onMigrate(
29996
- await this.ensureDatabaseClient()
29997
- );
30121
+ const client = await this.ensureDatabaseClient();
30122
+ if (this.isLost) {
30123
+ throw actorGenerationLostError();
30124
+ }
30125
+ await this.#databaseProvider.onMigrate(client);
29998
30126
  }
29999
30127
  async closeDatabase() {
30000
30128
  this.#db = void 0;
@@ -30266,7 +30394,7 @@ var ActorContextHandleAdapter = class {
30266
30394
  callNativeSync(
30267
30395
  () => this.#runtime.actorRequestSave(this.#ctx, { immediate: false })
30268
30396
  );
30269
- if (!this.#onStateChange) {
30397
+ if (!this.#onStateChange || this.isLost) {
30270
30398
  return;
30271
30399
  }
30272
30400
  actorState.isInOnStateChange = true;
@@ -30306,6 +30434,12 @@ var NativeWorkflowRuntimeAdapter = class {
30306
30434
  #onMessagesReceived;
30307
30435
  #completions = /* @__PURE__ */ new Map();
30308
30436
  id;
30437
+ /** Actor generation, used to keep workflow registrations of different generations apart. */
30438
+ generation;
30439
+ /** Whether this generation was declared lost. Workflow code checks it before each user callback. */
30440
+ get isLost() {
30441
+ return this.#ctx.isLost;
30442
+ }
30309
30443
  driver;
30310
30444
  queueManager;
30311
30445
  stateManager;
@@ -30313,6 +30447,7 @@ var NativeWorkflowRuntimeAdapter = class {
30313
30447
  this.#ctx = ctx;
30314
30448
  this.#onMessagesReceived = onMessagesReceived;
30315
30449
  this.id = ctx.actorId;
30450
+ this.generation = ctx.actorGeneration;
30316
30451
  this.driver = {
30317
30452
  kvBatchGet: async (actorId, keys) => {
30318
30453
  this.#assertActorId(actorId);
@@ -30595,9 +30730,23 @@ function buildNativeFactory(runtime, registryConfig, definition) {
30595
30730
  const runHandlerCoordinator = getRunInspectorKind(config3.run) === "workflow" ? new RunHandlerCoordinator(config3.run) : void 0;
30596
30731
  const getNativeWorkflowInspector = (ctx) => {
30597
30732
  var _a22, _b;
30733
+ if (callNativeSync(() => runtime.actorIsLost(ctx))) {
30734
+ return void 0;
30735
+ }
30598
30736
  const actorId = callNativeSync(() => runtime.actorId(ctx));
30599
30737
  const restart = () => callNativeSync(() => runtime.actorRestartRunHandler(ctx));
30600
- return ((_a22 = runHandlerCoordinator == null ? void 0 : runHandlerCoordinator.getInspector(actorId, restart)) == null ? void 0 : _a22.workflow) ?? ((_b = getRunInspectorConfig(config3.run, actorId)) == null ? void 0 : _b.workflow);
30738
+ const actorGeneration = callNativeSync(
30739
+ () => runtime.actorGeneration(ctx)
30740
+ );
30741
+ const inspector = ((_a22 = runHandlerCoordinator == null ? void 0 : runHandlerCoordinator.getInspector(
30742
+ actorId,
30743
+ restart,
30744
+ actorGeneration
30745
+ )) == null ? void 0 : _a22.workflow) ?? ((_b = getRunInspectorConfig(config3.run, actorId)) == null ? void 0 : _b.workflow);
30746
+ if (callNativeSync(() => runtime.actorIsLost(ctx))) {
30747
+ return void 0;
30748
+ }
30749
+ return inspector;
30601
30750
  };
30602
30751
  const onStateChange = typeof config3.onStateChange === "function" ? (actorCtx, nextState) => {
30603
30752
  config3.onStateChange(actorCtx, nextState);
@@ -30674,6 +30823,10 @@ function buildNativeFactory(runtime, registryConfig, definition) {
30674
30823
  return errorResponse(error46, 401);
30675
30824
  }
30676
30825
  }
30826
+ const inspectorConnParams = (conn) => {
30827
+ throwIfActorLost(runtime, ctx);
30828
+ return conn.params;
30829
+ };
30677
30830
  const workflowHistory = () => {
30678
30831
  var _a3;
30679
30832
  return serializeWorkflowHistoryForJson(
@@ -30707,7 +30860,7 @@ function buildNativeFactory(runtime, registryConfig, definition) {
30707
30860
  id: conn.id,
30708
30861
  details: {
30709
30862
  type: null,
30710
- params: conn.params,
30863
+ params: inspectorConnParams(conn),
30711
30864
  stateEnabled: true,
30712
30865
  state: conn.state,
30713
30866
  subscriptions: 0,
@@ -30911,7 +31064,7 @@ function buildNativeFactory(runtime, registryConfig, definition) {
30911
31064
  id: conn.id,
30912
31065
  details: {
30913
31066
  type: null,
30914
- params: conn.params,
31067
+ params: inspectorConnParams(conn),
30915
31068
  stateEnabled: true,
30916
31069
  state: conn.state,
30917
31070
  subscriptions: 0,
@@ -30958,15 +31111,20 @@ function buildNativeFactory(runtime, registryConfig, definition) {
30958
31111
  );
30959
31112
  }
30960
31113
  const args = body.properties !== void 0 ? [body.properties] : normalizeArgs(body.args);
31114
+ if (callNativeSync(() => runtime.actorIsLost(ctx))) {
31115
+ return jsonResponse(
31116
+ { error: "actor generation was declared lost" },
31117
+ { status: 503 }
31118
+ );
31119
+ }
30961
31120
  try {
30962
- const output = await action(
30963
- actorCtx,
30964
- ...validateActionArgs(
30965
- schemaConfig.actionInputSchemas,
30966
- actionName,
30967
- args
30968
- )
31121
+ const validatedArgs = validateActionArgs(
31122
+ schemaConfig.actionInputSchemas,
31123
+ actionName,
31124
+ args
30969
31125
  );
31126
+ throwIfActorLost(runtime, ctx);
31127
+ const output = await action(actorCtx, ...validatedArgs);
30970
31128
  return jsonResponse({ output });
30971
31129
  } catch (error46) {
30972
31130
  logger22().error({
@@ -31070,7 +31228,9 @@ function buildNativeFactory(runtime, registryConfig, definition) {
31070
31228
  await actorCtx.closeDatabase();
31071
31229
  }
31072
31230
  await actorCtx.runDatabaseMigrations();
31073
- if (typeof config3.onMigrate === "function") {
31231
+ if (typeof config3.onMigrate === "function" && !callNativeSync(
31232
+ () => runtime.actorIsLost(ctx)
31233
+ )) {
31074
31234
  await config3.onMigrate(actorCtx, isNew);
31075
31235
  }
31076
31236
  } catch (error210) {
@@ -31113,7 +31273,29 @@ function buildNativeFactory(runtime, registryConfig, definition) {
31113
31273
  const { ctx } = unwrapTsfnPayload(error46, payload);
31114
31274
  const actorCtx = makeActorCtx(ctx);
31115
31275
  const actorId = callNativeSync(() => runtime.actorId(ctx));
31276
+ const actorGeneration = callNativeSync(
31277
+ () => runtime.actorGeneration(ctx)
31278
+ );
31279
+ const isLost = () => callNativeSync(() => runtime.actorIsLost(ctx));
31280
+ if (isLost()) {
31281
+ try {
31282
+ await cleanupNativeLostRuntimeState(runtime, ctx);
31283
+ } finally {
31284
+ runHandlerCoordinator == null ? void 0 : runHandlerCoordinator.destroy(
31285
+ actorId,
31286
+ actorGeneration,
31287
+ {
31288
+ disposeInspector: false
31289
+ }
31290
+ );
31291
+ await actorCtx.dispose();
31292
+ }
31293
+ return;
31294
+ }
31116
31295
  const saveActorState = async () => {
31296
+ if (isLost()) {
31297
+ return;
31298
+ }
31117
31299
  if (runtime.kind === "wasm") {
31118
31300
  await runtime.actorSaveState(
31119
31301
  ctx,
@@ -31132,14 +31314,26 @@ function buildNativeFactory(runtime, registryConfig, definition) {
31132
31314
  await saveActorState();
31133
31315
  } finally {
31134
31316
  try {
31135
- await cleanupNativeSleepRuntimeState(
31136
- runtime,
31137
- ctx,
31138
- saveActorState
31139
- );
31317
+ if (isLost()) {
31318
+ await cleanupNativeLostRuntimeState(runtime, ctx);
31319
+ } else {
31320
+ await cleanupNativeSleepRuntimeState(
31321
+ runtime,
31322
+ ctx,
31323
+ saveActorState
31324
+ );
31325
+ }
31140
31326
  } finally {
31141
- runHandlerCoordinator == null ? void 0 : runHandlerCoordinator.destroy(actorId);
31142
- disposeRunInspector(config3.run, actorId);
31327
+ runHandlerCoordinator == null ? void 0 : runHandlerCoordinator.destroy(
31328
+ actorId,
31329
+ actorGeneration,
31330
+ {
31331
+ disposeInspector: !isLost()
31332
+ }
31333
+ );
31334
+ if (!isLost()) {
31335
+ disposeRunInspector(config3.run, actorId);
31336
+ }
31143
31337
  await actorCtx.dispose();
31144
31338
  }
31145
31339
  }
@@ -31150,10 +31344,17 @@ function buildNativeFactory(runtime, registryConfig, definition) {
31150
31344
  const { ctx } = unwrapTsfnPayload(error46, payload);
31151
31345
  const actorCtx = makeActorCtx(ctx);
31152
31346
  const actorId = callNativeSync(() => runtime.actorId(ctx));
31153
- runHandlerCoordinator == null ? void 0 : runHandlerCoordinator.destroy(actorId);
31154
- disposeRunInspector(config3.run, actorId);
31347
+ const isLost = () => callNativeSync(() => runtime.actorIsLost(ctx));
31348
+ runHandlerCoordinator == null ? void 0 : runHandlerCoordinator.destroy(
31349
+ actorId,
31350
+ callNativeSync(() => runtime.actorGeneration(ctx)),
31351
+ { disposeInspector: !isLost() }
31352
+ );
31353
+ if (!isLost()) {
31354
+ disposeRunInspector(config3.run, actorId);
31355
+ }
31155
31356
  try {
31156
- if (typeof config3.onDestroy === "function") {
31357
+ if (typeof config3.onDestroy === "function" && !isLost()) {
31157
31358
  await config3.onDestroy(actorCtx);
31158
31359
  }
31159
31360
  } finally {
@@ -31175,12 +31376,14 @@ function buildNativeFactory(runtime, registryConfig, definition) {
31175
31376
  request ? buildNativeHttpRequest(request) : void 0
31176
31377
  );
31177
31378
  try {
31379
+ const validatedParams = validateConnParams(
31380
+ schemaConfig.connParamsSchema,
31381
+ decodeValue(params)
31382
+ );
31383
+ throwIfActorLost(runtime, ctx);
31178
31384
  await config3.onBeforeConnect(
31179
31385
  actorCtx,
31180
- validateConnParams(
31181
- schemaConfig.connParamsSchema,
31182
- decodeValue(params)
31183
- )
31386
+ validatedParams
31184
31387
  );
31185
31388
  } finally {
31186
31389
  await actorCtx.dispose();
@@ -31208,13 +31411,22 @@ function buildNativeFactory(runtime, registryConfig, definition) {
31208
31411
  () => actorCtx.assertCanMutateState()
31209
31412
  );
31210
31413
  try {
31211
- const nextConnState = hasStaticConnState ? structuredClone(config3.connState) : await config3.createConnState(
31212
- actorCtx,
31213
- validateConnParams(
31414
+ let nextConnState;
31415
+ if (hasStaticConnState) {
31416
+ nextConnState = structuredClone(
31417
+ config3.connState
31418
+ );
31419
+ } else {
31420
+ const validatedParams = validateConnParams(
31214
31421
  schemaConfig.connParamsSchema,
31215
31422
  decodeValue(params)
31216
- )
31217
- );
31423
+ );
31424
+ throwIfActorLost(runtime, ctx);
31425
+ nextConnState = await config3.createConnState(
31426
+ actorCtx,
31427
+ validatedParams
31428
+ );
31429
+ }
31218
31430
  connAdapter.initializeState(nextConnState);
31219
31431
  return encodeValue(nextConnState);
31220
31432
  } finally {
@@ -31319,6 +31531,9 @@ function buildNativeFactory(runtime, registryConfig, definition) {
31319
31531
  onBeforeActionResponse: typeof config3.onBeforeActionResponse === "function" ? wrapNativeCallback(
31320
31532
  async (error46, payload) => {
31321
31533
  const { ctx, name, args, output } = unwrapTsfnPayload(error46, payload);
31534
+ if (callNativeSync(() => runtime.actorIsLost(ctx))) {
31535
+ return output;
31536
+ }
31322
31537
  const actorCtx = makeActorCtx(ctx);
31323
31538
  try {
31324
31539
  return encodeValue(
@@ -31387,6 +31602,7 @@ function buildNativeFactory(runtime, registryConfig, definition) {
31387
31602
  }
31388
31603
  };
31389
31604
  try {
31605
+ throwIfActorLost(runtime, ctx);
31390
31606
  const connParams = validateConnParams(
31391
31607
  schemaConfig.connParamsSchema,
31392
31608
  rawConnParams ? JSON.parse(rawConnParams) : void 0
@@ -31421,6 +31637,11 @@ function buildNativeFactory(runtime, registryConfig, definition) {
31421
31637
  abortRequest
31422
31638
  );
31423
31639
  }
31640
+ if (callNativeSync(
31641
+ () => runtime.actorIsLost(ctx)
31642
+ )) {
31643
+ throw actorGenerationLostError();
31644
+ }
31424
31645
  const response = await config3.onRequest(
31425
31646
  requestCtx,
31426
31647
  handlerRequest
@@ -31493,6 +31714,9 @@ function buildNativeFactory(runtime, registryConfig, definition) {
31493
31714
  const { ctx } = unwrapTsfnPayload(error46, payload);
31494
31715
  const actorId = callNativeSync(() => runtime.actorId(ctx));
31495
31716
  const executeRun = async () => {
31717
+ if (callNativeSync(() => runtime.actorIsLost(ctx))) {
31718
+ return;
31719
+ }
31496
31720
  const actorCtx = makeActorCtx(ctx);
31497
31721
  try {
31498
31722
  await run(actorCtx);
@@ -31506,7 +31730,8 @@ function buildNativeFactory(runtime, registryConfig, definition) {
31506
31730
  () => callNativeSync(
31507
31731
  () => runtime.actorRestartRunHandler(ctx)
31508
31732
  ),
31509
- executeRun
31733
+ executeRun,
31734
+ callNativeSync(() => runtime.actorGeneration(ctx))
31510
31735
  );
31511
31736
  return outcome !== "suppressed";
31512
31737
  }
@@ -31546,14 +31771,16 @@ function buildNativeFactory(runtime, registryConfig, definition) {
31546
31771
  const actorCtx = conn != null ? makeConnCtx(ctx, conn, void 0, cancelToken) : makeActorCtx(ctx, void 0, cancelToken);
31547
31772
  const runAction = async () => {
31548
31773
  try {
31774
+ const validatedArgs = validateActionArgs(
31775
+ schemaConfig.actionInputSchemas,
31776
+ name,
31777
+ decodeArgs(args)
31778
+ );
31779
+ throwIfActorLost(runtime, ctx);
31549
31780
  return encodeValue(
31550
31781
  await handler(
31551
31782
  actorCtx,
31552
- ...validateActionArgs(
31553
- schemaConfig.actionInputSchemas,
31554
- name,
31555
- decodeArgs(args)
31556
- ),
31783
+ ...validatedArgs,
31557
31784
  ...scheduledFire ? [scheduledFire] : []
31558
31785
  )
31559
31786
  );
@@ -31609,6 +31836,7 @@ function buildNativeFactory(runtime, registryConfig, definition) {
31609
31836
  if (canPublish && !await canPublish(actorCtx)) {
31610
31837
  throw forbiddenError();
31611
31838
  }
31839
+ throwIfActorLost(runtime, ctx);
31612
31840
  const decodedBody = decodeValue(body);
31613
31841
  if (wait) {
31614
31842
  try {
@@ -31652,7 +31880,7 @@ function buildNativeFactory(runtime, registryConfig, definition) {
31652
31880
  )
31653
31881
  };
31654
31882
  return runtime.createActorFactory(
31655
- callbacks,
31883
+ refuseCallbacksAfterLost(runtime, callbacks),
31656
31884
  buildActorConfig(definition, registryConfig, runtime.kind)
31657
31885
  );
31658
31886
  }