rivetkit 3.1.2-rc.4 → 3.1.2-rc.6

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 (89) hide show
  1. package/dist/browser/client.d.ts +16 -2
  2. package/dist/browser/client.js +1 -9
  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/{chunk-UWN46MY2.js → chunk-2OEJM5K3.js} +2 -2
  7. package/dist/tsup/{chunk-UWN46MY2.js.map → chunk-2OEJM5K3.js.map} +1 -1
  8. package/dist/tsup/{chunk-YCGI3WIV.js → chunk-4GCDILHQ.js} +2 -2
  9. package/dist/tsup/{chunk-Z367MDQC.cjs → chunk-7EDYE6RS.cjs} +147 -155
  10. package/dist/tsup/chunk-7EDYE6RS.cjs.map +1 -0
  11. package/dist/tsup/{chunk-J6FABVZY.cjs → chunk-AKADRXK5.cjs} +3 -3
  12. package/dist/tsup/{chunk-J6FABVZY.cjs.map → chunk-AKADRXK5.cjs.map} +1 -1
  13. package/dist/tsup/{chunk-YJWR3YQX.js → chunk-AUC22H3Q.js} +4 -4
  14. package/dist/tsup/{chunk-OJ37CYEU.js → chunk-GPNKYTKX.js} +2 -2
  15. package/dist/tsup/{chunk-6476KISG.js → chunk-HEWH252Y.js} +2 -2
  16. package/dist/tsup/{chunk-VWIV375X.cjs → chunk-JHBRUK2G.cjs} +2 -2
  17. package/dist/tsup/{chunk-VWIV375X.cjs.map → chunk-JHBRUK2G.cjs.map} +1 -1
  18. package/dist/tsup/{chunk-6AD2HIBH.cjs → chunk-KCL4AISR.cjs} +4 -4
  19. package/dist/tsup/{chunk-6AD2HIBH.cjs.map → chunk-KCL4AISR.cjs.map} +1 -1
  20. package/dist/tsup/{chunk-S26YAPYA.js → chunk-LFGYDRKK.js} +2 -2
  21. package/dist/tsup/{chunk-QSO2JLX2.cjs → chunk-NHTD4LYE.cjs} +3 -3
  22. package/dist/tsup/{chunk-QSO2JLX2.cjs.map → chunk-NHTD4LYE.cjs.map} +1 -1
  23. package/dist/tsup/{chunk-OS3NPWVL.js → chunk-O6BVKXY4.js} +6 -14
  24. package/dist/tsup/chunk-O6BVKXY4.js.map +1 -0
  25. package/dist/tsup/{chunk-2NDVJNGH.cjs → chunk-V32FXQ3Y.cjs} +3 -3
  26. package/dist/tsup/{chunk-2NDVJNGH.cjs.map → chunk-V32FXQ3Y.cjs.map} +1 -1
  27. package/dist/tsup/{chunk-P3P4I45V.cjs → chunk-X3EPJHTD.cjs} +7 -7
  28. package/dist/tsup/{chunk-P3P4I45V.cjs.map → chunk-X3EPJHTD.cjs.map} +1 -1
  29. package/dist/tsup/{chunk-WJA6TRRE.cjs → chunk-X7HD5E4N.cjs} +10 -10
  30. package/dist/tsup/{chunk-WJA6TRRE.cjs.map → chunk-X7HD5E4N.cjs.map} +1 -1
  31. package/dist/tsup/{chunk-NSAMWM3A.js → chunk-Y5RH2IT6.js} +3 -3
  32. package/dist/tsup/client/mod.cjs +5 -5
  33. package/dist/tsup/client/mod.d.cts +2 -2
  34. package/dist/tsup/client/mod.d.ts +2 -2
  35. package/dist/tsup/client/mod.js +4 -4
  36. package/dist/tsup/common/log.cjs +2 -2
  37. package/dist/tsup/common/log.js +1 -1
  38. package/dist/tsup/common/websocket.cjs +3 -3
  39. package/dist/tsup/common/websocket.js +2 -2
  40. package/dist/tsup/{config-BbRMYCV8.d.cts → config-BMGUEKMZ.d.cts} +16 -2
  41. package/dist/tsup/{config-CW3AWqHa.d.ts → config-iBpXyITj.d.ts} +16 -2
  42. package/dist/tsup/{context-Cp64yaMW.d.ts → context-hyVakgnU.d.ts} +1 -1
  43. package/dist/tsup/{context-NuwP8HjQ.d.cts → context-qfvLuF0P.d.cts} +1 -1
  44. package/dist/tsup/db/drizzle.cjs +2 -2
  45. package/dist/tsup/db/drizzle.js +1 -1
  46. package/dist/tsup/db/mod.cjs +3 -3
  47. package/dist/tsup/db/mod.js +2 -2
  48. package/dist/tsup/dynamic/mod.d.cts +1 -1
  49. package/dist/tsup/dynamic/mod.d.ts +1 -1
  50. package/dist/tsup/inspector/mod.cjs +5 -5
  51. package/dist/tsup/inspector/mod.js +4 -4
  52. package/dist/tsup/inspector/workflow.cjs +4 -4
  53. package/dist/tsup/inspector/workflow.js +3 -3
  54. package/dist/tsup/inspector-tab/mod.d.cts +2 -2
  55. package/dist/tsup/inspector-tab/mod.d.ts +2 -2
  56. package/dist/tsup/mod.cjs +391 -127
  57. package/dist/tsup/mod.cjs.map +1 -1
  58. package/dist/tsup/mod.d.cts +3 -3
  59. package/dist/tsup/mod.d.ts +3 -3
  60. package/dist/tsup/mod.js +310 -46
  61. package/dist/tsup/mod.js.map +1 -1
  62. package/dist/tsup/test/mod.cjs +6 -6
  63. package/dist/tsup/test/mod.d.cts +1 -1
  64. package/dist/tsup/test/mod.d.ts +1 -1
  65. package/dist/tsup/test/mod.js +4 -4
  66. package/dist/tsup/utils.cjs +2 -2
  67. package/dist/tsup/utils.js +1 -1
  68. package/dist/tsup/workflow/mod.cjs +7 -7
  69. package/dist/tsup/workflow/mod.d.cts +3 -3
  70. package/dist/tsup/workflow/mod.d.ts +3 -3
  71. package/dist/tsup/workflow/mod.js +4 -4
  72. package/package.json +9 -9
  73. package/src/client/lifecycle-errors.ts +0 -15
  74. package/src/common/database/config.ts +52 -0
  75. package/src/common/database/native-database.ts +229 -18
  76. package/src/registry/config/index.ts +1 -3
  77. package/src/registry/index.ts +14 -15
  78. package/src/registry/napi-runtime.ts +81 -2
  79. package/src/registry/native.ts +66 -17
  80. package/src/registry/runtime.ts +53 -2
  81. package/src/registry/wasm-runtime.ts +88 -0
  82. package/dist/tsup/chunk-OS3NPWVL.js.map +0 -1
  83. package/dist/tsup/chunk-Z367MDQC.cjs.map +0 -1
  84. /package/dist/tsup/{chunk-YCGI3WIV.js.map → chunk-4GCDILHQ.js.map} +0 -0
  85. /package/dist/tsup/{chunk-YJWR3YQX.js.map → chunk-AUC22H3Q.js.map} +0 -0
  86. /package/dist/tsup/{chunk-OJ37CYEU.js.map → chunk-GPNKYTKX.js.map} +0 -0
  87. /package/dist/tsup/{chunk-6476KISG.js.map → chunk-HEWH252Y.js.map} +0 -0
  88. /package/dist/tsup/{chunk-S26YAPYA.js.map → chunk-LFGYDRKK.js.map} +0 -0
  89. /package/dist/tsup/{chunk-NSAMWM3A.js.map → chunk-Y5RH2IT6.js.map} +0 -0
@@ -289,9 +289,7 @@ export const RegistryConfigSchema = z
289
289
  /**
290
290
  * Wait this many milliseconds for the serve promise to resolve
291
291
  * after calling `CoreRegistry::shutdown()`. Defaults to the
292
- * engine's shutdown budget once the node connects: the pool's
293
- * eviction delay and period, over which it stops the node's actors,
294
- * plus the time each actor is given to stop. A
292
+ * engine-provided actor stop threshold once the node connects. A
295
293
  * signal-driven shutdown of a managed local development engine waits
296
294
  * four seconds by default so actors can acknowledge stopping before
297
295
  * a file watcher restarts the process.
@@ -39,10 +39,9 @@ const LOCAL_ENGINE_READY_TIMEOUT_MS = 60_000;
39
39
  // reports. Leave time to close the node connection after that, while staying
40
40
  // below the five-second force-kill ceiling used by tsx watch.
41
41
  const LOCAL_DEV_SHUTDOWN_GRACE_PERIOD_MS = 4_000;
42
- // Matches the engine's default shutdown budget: a pool without an eviction delay or period,
43
- // plus the default `pegboard.actor_stop_threshold`. Used only when the node has not received
44
- // the engine's advertised budget before shutdown begins.
45
- const DEFAULT_SHUTDOWN_BUDGET_MS = 30_000;
42
+ // Matches the engine's default `pegboard.actor_stop_threshold`. Used only when the
43
+ // node has not received the engine's advertised threshold before shutdown begins.
44
+ const DEFAULT_ACTOR_STOP_THRESHOLD_MS = 30_000;
46
45
 
47
46
  // Serverless is disabled for v3; keep the implementation for a later release.
48
47
  function rejectServerless(): void {
@@ -824,14 +823,13 @@ export class Registry<A extends RegistryActors> {
824
823
  if (gracePeriodMs === undefined) {
825
824
  gracePeriodMs = localDevSignal
826
825
  ? LOCAL_DEV_SHUTDOWN_GRACE_PERIOD_MS
827
- : ((await this.#shutdownBudgetMs(
826
+ : ((await this.#actorStopThresholdMs(
828
827
  modeAPromise ?? modeBPromise,
829
- )) ?? DEFAULT_SHUTDOWN_BUDGET_MS);
828
+ )) ?? DEFAULT_ACTOR_STOP_THRESHOLD_MS);
830
829
  }
831
830
  // Race the entire drain sequence (both modes + serve promise) against
832
831
  // a single grace ceiling. Local dev signal shutdowns allow the watcher
833
- // to restart promptly; other shutdowns use the engine's shutdown budget, its drain
834
- // period plus its actor stop threshold.
832
+ // to restart promptly; other shutdowns use the engine's actor stop threshold.
835
833
  const drain = async () => {
836
834
  // Shut down every live `CoreRegistry` we know about. Mode A
837
835
  // (`start()`) and Mode B (`handler()`) each build a separate
@@ -894,7 +892,7 @@ export class Registry<A extends RegistryActors> {
894
892
  );
895
893
  }
896
894
 
897
- async #shutdownBudgetMs(
895
+ async #actorStopThresholdMs(
898
896
  configuredRegistryPromise:
899
897
  | ReturnType<typeof buildConfiguredRegistry>
900
898
  | undefined,
@@ -902,18 +900,19 @@ export class Registry<A extends RegistryActors> {
902
900
  if (configuredRegistryPromise === undefined) return undefined;
903
901
  try {
904
902
  const { runtime, registry } = await configuredRegistryPromise;
905
- const budgetMs = await runtime.registryShutdownBudgetMs?.(registry);
903
+ const thresholdMs =
904
+ await runtime.registryActorStopThresholdMs?.(registry);
906
905
  if (
907
- budgetMs !== undefined &&
908
- Number.isFinite(budgetMs) &&
909
- budgetMs > 0
906
+ thresholdMs !== undefined &&
907
+ Number.isFinite(thresholdMs) &&
908
+ thresholdMs > 0
910
909
  ) {
911
- return budgetMs;
910
+ return thresholdMs;
912
911
  }
913
912
  } catch (err) {
914
913
  logger().warn(
915
914
  { err },
916
- "failed to read the shutdown budget for shutdown grace",
915
+ "failed to read actor stop threshold for shutdown grace",
917
916
  );
918
917
  }
919
918
  return undefined;
@@ -345,11 +345,12 @@ export class NapiCoreRuntime implements CoreRuntime {
345
345
  await this.#bindings.shutdownTelemetry();
346
346
  }
347
347
 
348
- async registryShutdownBudgetMs(
348
+ async registryActorStopThresholdMs(
349
349
  registry: RegistryHandle,
350
350
  ): Promise<number | undefined> {
351
351
  return (
352
- (await asNativeRegistry(registry).shutdownBudgetMs()) ?? undefined
352
+ (await asNativeRegistry(registry).actorStopThresholdMs()) ??
353
+ undefined
353
354
  );
354
355
  }
355
356
 
@@ -882,6 +883,13 @@ export class NapiCoreRuntime implements CoreRuntime {
882
883
  return await this.#actorSql(ctx).exec(sql);
883
884
  }
884
885
 
886
+ actorSqlExecSync(
887
+ ctx: ActorContextHandle,
888
+ sql: string,
889
+ ): RuntimeSqlExecResult {
890
+ return this.#actorSql(ctx).execSync(sql);
891
+ }
892
+
885
893
  async actorSqlExecute(
886
894
  ctx: ActorContextHandle,
887
895
  sql: string,
@@ -894,6 +902,18 @@ export class NapiCoreRuntime implements CoreRuntime {
894
902
  return normalizeRuntimeSqlExecuteResult(result);
895
903
  }
896
904
 
905
+ actorSqlExecuteSync(
906
+ ctx: ActorContextHandle,
907
+ sql: string,
908
+ params?: RuntimeSqlBindParams,
909
+ ): RuntimeSqlExecuteResult {
910
+ const result = this.#actorSql(ctx).executeSync(
911
+ sql,
912
+ toNapiSqlBindParams(params),
913
+ );
914
+ return normalizeRuntimeSqlExecuteResult(result);
915
+ }
916
+
897
917
  async actorSqlExecuteBatch(
898
918
  ctx: ActorContextHandle,
899
919
  statements: RuntimeSqlBatchStatement[],
@@ -915,6 +935,17 @@ export class NapiCoreRuntime implements CoreRuntime {
915
935
  )) as unknown as SqliteTransactionHandle;
916
936
  }
917
937
 
938
+ actorSqlBeginTransactionSync(
939
+ ctx: ActorContextHandle,
940
+ timeoutMs?: number,
941
+ name?: string,
942
+ ): SqliteTransactionHandle {
943
+ return this.#actorSql(ctx).beginTransactionSync(
944
+ timeoutMs,
945
+ name,
946
+ ) as unknown as SqliteTransactionHandle;
947
+ }
948
+
918
949
  async actorSqlTransactionExec(
919
950
  transaction: SqliteTransactionHandle,
920
951
  sql: string,
@@ -922,6 +953,13 @@ export class NapiCoreRuntime implements CoreRuntime {
922
953
  return await asNativeSqlTransaction(transaction).exec(sql);
923
954
  }
924
955
 
956
+ actorSqlTransactionExecSync(
957
+ transaction: SqliteTransactionHandle,
958
+ sql: string,
959
+ ): RuntimeSqlExecResult {
960
+ return asNativeSqlTransaction(transaction).execSync(sql);
961
+ }
962
+
925
963
  async actorSqlTransactionExecute(
926
964
  transaction: SqliteTransactionHandle,
927
965
  sql: string,
@@ -934,17 +972,39 @@ export class NapiCoreRuntime implements CoreRuntime {
934
972
  return normalizeRuntimeSqlExecuteResult(result);
935
973
  }
936
974
 
975
+ actorSqlTransactionExecuteSync(
976
+ transaction: SqliteTransactionHandle,
977
+ sql: string,
978
+ params?: RuntimeSqlBindParams,
979
+ ): RuntimeSqlExecuteResult {
980
+ const result = asNativeSqlTransaction(transaction).executeSync(
981
+ sql,
982
+ toNapiSqlBindParams(params),
983
+ );
984
+ return normalizeRuntimeSqlExecuteResult(result);
985
+ }
986
+
937
987
  async actorSqlTransactionCommit(
938
988
  transaction: SqliteTransactionHandle,
939
989
  ): Promise<void> {
940
990
  await asNativeSqlTransaction(transaction).commit();
941
991
  }
942
992
 
993
+ actorSqlTransactionCommitSync(transaction: SqliteTransactionHandle): void {
994
+ asNativeSqlTransaction(transaction).commitSync();
995
+ }
996
+
943
997
  async actorSqlTransactionRollback(
944
998
  transaction: SqliteTransactionHandle,
945
999
  ): Promise<void> {
946
1000
  await asNativeSqlTransaction(transaction).rollback();
947
1001
  }
1002
+
1003
+ actorSqlTransactionRollbackSync(
1004
+ transaction: SqliteTransactionHandle,
1005
+ ): void {
1006
+ asNativeSqlTransaction(transaction).rollbackSync();
1007
+ }
948
1008
  async actorBeginStateTransaction(
949
1009
  ctx: ActorContextHandle,
950
1010
  timeoutMs?: number,
@@ -1010,6 +1070,25 @@ export class NapiCoreRuntime implements CoreRuntime {
1010
1070
  return this.#actorSql(ctx).takeLastKvError?.() ?? null;
1011
1071
  }
1012
1072
 
1073
+ async actorSqlOpen(ctx: ActorContextHandle): Promise<void> {
1074
+ await this.#actorSql(ctx).open();
1075
+ }
1076
+
1077
+ actorSqlCommitTicket(ctx: ActorContextHandle): number {
1078
+ return this.#actorSql(ctx).commitTicket();
1079
+ }
1080
+
1081
+ actorSqlAbortGeneration(ctx: ActorContextHandle, reason: string): void {
1082
+ this.#actorSql(ctx).abortGeneration(reason);
1083
+ }
1084
+
1085
+ async actorSqlWhenDurable(
1086
+ ctx: ActorContextHandle,
1087
+ ticket: number,
1088
+ ): Promise<void> {
1089
+ await this.#actorSql(ctx).whenDurable(ticket);
1090
+ }
1091
+
1013
1092
  async actorSqlClose(ctx: ActorContextHandle): Promise<void> {
1014
1093
  const nativeCtx = asNativeActorContext(ctx);
1015
1094
  if (!this.#sqlOpened.delete(nativeCtx)) {
@@ -62,7 +62,11 @@ import {
62
62
  db as defaultDb,
63
63
  registerNativeStateTransactionOpener,
64
64
  } from "@/common/database/mod";
65
- import { wrapJsNativeDatabase } from "@/common/database/native-database";
65
+ import {
66
+ type JsNativeDatabaseLike,
67
+ type JsNativeSynchronousTransactionLike,
68
+ wrapJsNativeDatabase,
69
+ } from "@/common/database/native-database";
66
70
  import { assertJsonCompatValue, type JsonCompatValue } from "@/common/encoding";
67
71
  import { isResponseLike } from "@/common/fetch-like";
68
72
  import {
@@ -127,6 +131,7 @@ import type {
127
131
  RuntimeScheduledFireInfo,
128
132
  RuntimeServeConfig,
129
133
  RuntimeStateDeltaPayload,
134
+ SqliteTransactionHandle,
130
135
  WebSocketHandle,
131
136
  } from "./runtime";
132
137
  import { servicesBinaryUnavailable } from "./runtime";
@@ -657,6 +662,26 @@ async function closeNativeDatabaseClient(
657
662
  }
658
663
  }
659
664
 
665
+ function createNativeSqlTransaction(
666
+ runtime: CoreRuntime,
667
+ transaction: SqliteTransactionHandle,
668
+ ): JsNativeSynchronousTransactionLike {
669
+ return {
670
+ exec: (sql) => runtime.actorSqlTransactionExec(transaction, sql),
671
+ execSync: (sql) =>
672
+ runtime.actorSqlTransactionExecSync(transaction, sql),
673
+ execute: (sql, params) =>
674
+ runtime.actorSqlTransactionExecute(transaction, sql, params),
675
+ executeSync: (sql, params) =>
676
+ runtime.actorSqlTransactionExecuteSync(transaction, sql, params),
677
+ commit: () => runtime.actorSqlTransactionCommit(transaction),
678
+ commitSync: () => runtime.actorSqlTransactionCommitSync(transaction),
679
+ rollback: () => runtime.actorSqlTransactionRollback(transaction),
680
+ rollbackSync: () =>
681
+ runtime.actorSqlTransactionRollbackSync(transaction),
682
+ };
683
+ }
684
+
660
685
  function getOrCreateNativeSqlDatabase(
661
686
  runtime: CoreRuntime,
662
687
  ctx: ActorContextHandle,
@@ -667,9 +692,12 @@ function getOrCreateNativeSqlDatabase(
667
692
  return cachedDatabase;
668
693
  }
669
694
 
670
- const database = wrapJsNativeDatabase({
695
+ const bindings: JsNativeDatabaseLike = {
671
696
  exec: (sql) => runtime.actorSqlExec(ctx, sql),
697
+ execSync: (sql) => runtime.actorSqlExecSync(ctx, sql),
672
698
  execute: (sql, params) => runtime.actorSqlExecute(ctx, sql, params),
699
+ executeSync: (sql, params) =>
700
+ runtime.actorSqlExecuteSync(ctx, sql, params),
673
701
  executeBatch: (statements) =>
674
702
  runtime.actorSqlExecuteBatch(ctx, statements),
675
703
  beginTransaction: async (timeoutMs, name) => {
@@ -678,19 +706,15 @@ function getOrCreateNativeSqlDatabase(
678
706
  timeoutMs,
679
707
  name,
680
708
  );
681
- return {
682
- exec: (sql) =>
683
- runtime.actorSqlTransactionExec(transaction, sql),
684
- execute: (sql, params) =>
685
- runtime.actorSqlTransactionExecute(
686
- transaction,
687
- sql,
688
- params,
689
- ),
690
- commit: () => runtime.actorSqlTransactionCommit(transaction),
691
- rollback: () =>
692
- runtime.actorSqlTransactionRollback(transaction),
693
- };
709
+ return createNativeSqlTransaction(runtime, transaction);
710
+ },
711
+ beginTransactionSync: (timeoutMs, name) => {
712
+ const transaction = runtime.actorSqlBeginTransactionSync(
713
+ ctx,
714
+ timeoutMs,
715
+ name,
716
+ );
717
+ return createNativeSqlTransaction(runtime, transaction);
694
718
  },
695
719
  beginStateTransaction: async (timeoutMs, context, name) => {
696
720
  const scope = context as NativeStateTransactionScope | undefined;
@@ -731,8 +755,14 @@ function getOrCreateNativeSqlDatabase(
731
755
  run: (sql, params) => runtime.actorSqlRun(ctx, sql, params),
732
756
  metrics: () => runtime.actorSqlMetrics(ctx),
733
757
  takeLastKvError: () => runtime.actorSqlTakeLastKvError(ctx),
758
+ open: () => runtime.actorSqlOpen(ctx),
759
+ commitTicket: () => runtime.actorSqlCommitTicket(ctx),
760
+ whenDurable: (ticket) => runtime.actorSqlWhenDurable(ctx, ticket),
761
+ abortGeneration: (reason) =>
762
+ runtime.actorSqlAbortGeneration(ctx, reason),
734
763
  close: () => runtime.actorSqlClose(ctx),
735
- });
764
+ };
765
+ const database = wrapJsNativeDatabase(bindings);
736
766
  runtimeState.sql = database;
737
767
  return database;
738
768
  }
@@ -3196,10 +3226,16 @@ export class ActorContextHandleAdapter {
3196
3226
  {
3197
3227
  open: async (requestedActorId) => {
3198
3228
  void requestedActorId;
3199
- return getOrCreateNativeSqlDatabase(
3229
+ const database = getOrCreateNativeSqlDatabase(
3200
3230
  this.#runtime,
3201
3231
  this.#ctx,
3202
3232
  );
3233
+ // Synchronous calls need an open database. Other actors keep opening
3234
+ // it on first use.
3235
+ if (usesSqliteDeferredCommits(this.#databaseProvider)) {
3236
+ await database.open();
3237
+ }
3238
+ return database;
3203
3239
  },
3204
3240
  },
3205
3241
  async (timeoutMs, context, name) =>
@@ -3996,6 +4032,17 @@ function buildActorTracingConfig(
3996
4032
  };
3997
4033
  }
3998
4034
 
4035
+ const SQLITE_DEFERRED_COMMITS = Symbol.for("rivetkit.sqlite.deferredCommits");
4036
+
4037
+ // Internal opt-in for embedders, such as Open Orange, that gate outputs on `whenDurable`.
4038
+ function usesSqliteDeferredCommits(databaseProvider: unknown): boolean {
4039
+ return (
4040
+ (databaseProvider as Record<symbol, unknown> | undefined)?.[
4041
+ SQLITE_DEFERRED_COMMITS
4042
+ ] === true
4043
+ );
4044
+ }
4045
+
3999
4046
  function buildActorConfig(
4000
4047
  definition: AnyActorDefinition,
4001
4048
  registryConfig: RegistryConfig,
@@ -4009,12 +4056,14 @@ function buildActorConfig(
4009
4056
  const sqliteProfiling = (
4010
4057
  config.db as { sqliteProfiling?: SqliteProfilingOptions } | undefined
4011
4058
  )?.sqliteProfiling;
4059
+ const sqliteDeferredCommits = usesSqliteDeferredCommits(config.db);
4012
4060
 
4013
4061
  return {
4014
4062
  name: options.name as string | undefined,
4015
4063
  icon: options.icon as string | undefined,
4016
4064
  hasDatabase: true,
4017
4065
  remoteSqlite: usesRemoteSqlite,
4066
+ sqliteDeferredCommits: sqliteDeferredCommits || undefined,
4018
4067
  sqliteProfiling,
4019
4068
  tracing: buildActorTracingConfig(
4020
4069
  definition.config.actions,
@@ -287,10 +287,15 @@ export interface RuntimeSqlRunResult {
287
287
 
288
288
  export interface RuntimeSqlDatabase {
289
289
  exec(sql: string): Promise<RuntimeSqlExecResult>;
290
+ execSync(sql: string): RuntimeSqlExecResult;
290
291
  execute(
291
292
  sql: string,
292
293
  params?: RuntimeSqlBindParams,
293
294
  ): Promise<RuntimeSqlExecuteResult>;
295
+ executeSync(
296
+ sql: string,
297
+ params?: RuntimeSqlBindParams,
298
+ ): RuntimeSqlExecuteResult;
294
299
  executeBatch(
295
300
  statements: RuntimeSqlBatchStatement[],
296
301
  ): Promise<RuntimeSqlExecuteResult[]>;
@@ -306,19 +311,33 @@ export interface RuntimeSqlDatabase {
306
311
  timeoutMs?: number,
307
312
  name?: string,
308
313
  ): Promise<RuntimeSqlTransactionDatabase>;
314
+ beginTransactionSync(
315
+ timeoutMs?: number,
316
+ name?: string,
317
+ ): RuntimeSqlTransactionDatabase;
309
318
  metrics?(): SqliteNativeMetrics | null;
310
319
  takeLastKvError?(): string | null;
320
+ open(): Promise<void>;
321
+ commitTicket(): number;
322
+ whenDurable(ticket: number): Promise<void>;
311
323
  close(): Promise<void>;
312
324
  }
313
325
 
314
326
  export interface RuntimeSqlTransactionDatabase {
315
327
  exec(sql: string): Promise<RuntimeSqlExecResult>;
328
+ execSync(sql: string): RuntimeSqlExecResult;
316
329
  execute(
317
330
  sql: string,
318
331
  params?: RuntimeSqlBindParams,
319
332
  ): Promise<RuntimeSqlExecuteResult>;
333
+ executeSync(
334
+ sql: string,
335
+ params?: RuntimeSqlBindParams,
336
+ ): RuntimeSqlExecuteResult;
320
337
  commit(): Promise<void>;
338
+ commitSync(): void;
321
339
  rollback(): Promise<void>;
340
+ rollbackSync(): void;
322
341
  }
323
342
 
324
343
  /** Sample rates for the traces an actor starts, in the shape core takes. */
@@ -333,6 +352,7 @@ export interface RuntimeActorConfig {
333
352
  icon?: string;
334
353
  hasDatabase?: boolean;
335
354
  remoteSqlite?: boolean;
355
+ sqliteDeferredCommits?: boolean;
336
356
  sqliteProfiling?: SqliteProfilingOptions;
337
357
  tracing?: RuntimeActorTracingConfig;
338
358
  hasState?: boolean;
@@ -499,8 +519,7 @@ export interface CoreRuntime {
499
519
  waitRegistryReady(registry: RegistryHandle): Promise<void>;
500
520
  shutdownRegistry(registry: RegistryHandle): Promise<void>;
501
521
  shutdownTelemetry?(): Promise<void>;
502
- /** The engine's drain period plus its actor stop threshold, once the node connected. */
503
- registryShutdownBudgetMs?(
522
+ registryActorStopThresholdMs?(
504
523
  registry: RegistryHandle,
505
524
  ): Promise<number | undefined>;
506
525
  registryActiveActorCount?(registry: RegistryHandle): number;
@@ -709,11 +728,20 @@ export interface CoreRuntime {
709
728
  ctx: ActorContextHandle,
710
729
  sql: string,
711
730
  ): Promise<RuntimeSqlExecResult>;
731
+ actorSqlExecSync(
732
+ ctx: ActorContextHandle,
733
+ sql: string,
734
+ ): RuntimeSqlExecResult;
712
735
  actorSqlExecute(
713
736
  ctx: ActorContextHandle,
714
737
  sql: string,
715
738
  params?: RuntimeSqlBindParams,
716
739
  ): Promise<RuntimeSqlExecuteResult>;
740
+ actorSqlExecuteSync(
741
+ ctx: ActorContextHandle,
742
+ sql: string,
743
+ params?: RuntimeSqlBindParams,
744
+ ): RuntimeSqlExecuteResult;
717
745
  actorSqlExecuteBatch(
718
746
  ctx: ActorContextHandle,
719
747
  statements: RuntimeSqlBatchStatement[],
@@ -723,21 +751,37 @@ export interface CoreRuntime {
723
751
  timeoutMs?: number,
724
752
  name?: string,
725
753
  ): Promise<SqliteTransactionHandle>;
754
+ actorSqlBeginTransactionSync(
755
+ ctx: ActorContextHandle,
756
+ timeoutMs?: number,
757
+ name?: string,
758
+ ): SqliteTransactionHandle;
726
759
  actorSqlTransactionExec(
727
760
  transaction: SqliteTransactionHandle,
728
761
  sql: string,
729
762
  ): Promise<RuntimeSqlExecResult>;
763
+ actorSqlTransactionExecSync(
764
+ transaction: SqliteTransactionHandle,
765
+ sql: string,
766
+ ): RuntimeSqlExecResult;
730
767
  actorSqlTransactionExecute(
731
768
  transaction: SqliteTransactionHandle,
732
769
  sql: string,
733
770
  params?: RuntimeSqlBindParams,
734
771
  ): Promise<RuntimeSqlExecuteResult>;
772
+ actorSqlTransactionExecuteSync(
773
+ transaction: SqliteTransactionHandle,
774
+ sql: string,
775
+ params?: RuntimeSqlBindParams,
776
+ ): RuntimeSqlExecuteResult;
735
777
  actorSqlTransactionCommit(
736
778
  transaction: SqliteTransactionHandle,
737
779
  ): Promise<void>;
780
+ actorSqlTransactionCommitSync(transaction: SqliteTransactionHandle): void;
738
781
  actorSqlTransactionRollback(
739
782
  transaction: SqliteTransactionHandle,
740
783
  ): Promise<void>;
784
+ actorSqlTransactionRollbackSync(transaction: SqliteTransactionHandle): void;
741
785
  actorBeginStateTransaction(
742
786
  ctx: ActorContextHandle,
743
787
  timeoutMs?: number,
@@ -767,6 +811,13 @@ export interface CoreRuntime {
767
811
  ): Promise<RuntimeSqlRunResult>;
768
812
  actorSqlMetrics(ctx: ActorContextHandle): SqliteNativeMetrics | null;
769
813
  actorSqlTakeLastKvError(ctx: ActorContextHandle): string | null;
814
+ /** Opens the actor database so later synchronous calls never fetch its first pages. */
815
+ actorSqlOpen(ctx: ActorContextHandle): Promise<void>;
816
+ /** Returns the ticket of the latest local commit. */
817
+ actorSqlCommitTicket(ctx: ActorContextHandle): number;
818
+ /** Resolves once the commit `ticket` names and every earlier commit are durable. */
819
+ actorSqlWhenDurable(ctx: ActorContextHandle, ticket: number): Promise<void>;
820
+ actorSqlAbortGeneration(ctx: ActorContextHandle, reason: string): void;
770
821
  actorSqlClose(ctx: ActorContextHandle): Promise<void>;
771
822
 
772
823
  actorQueueSend(
@@ -151,6 +151,18 @@ function wasmNumber(value: number | bigint): number {
151
151
  return typeof value === "bigint" ? Number(value) : value;
152
152
  }
153
153
 
154
+ function synchronousSqliteUnavailable(): never {
155
+ throw new RivetError(
156
+ "sqlite",
157
+ "sync_unsupported",
158
+ "Synchronous SQLite is unavailable because the actor runs on the wasm runtime.",
159
+ {
160
+ public: true,
161
+ metadata: { runtime: "wasm" },
162
+ },
163
+ );
164
+ }
165
+
154
166
  function normalizeKvEntry(entry: RuntimeKvEntry): RuntimeKvEntry {
155
167
  return {
156
168
  key: toBytes(entry.key),
@@ -368,6 +380,14 @@ export class WasmCoreRuntime implements CoreRuntime {
368
380
  callbacks: object,
369
381
  config?: RuntimeActorConfig | undefined | null,
370
382
  ): ActorFactoryHandle {
383
+ if (config?.sqliteDeferredCommits) {
384
+ throw new RivetError(
385
+ "sqlite",
386
+ "deferred_commits_unsupported",
387
+ "SQLite deferred commits require local native SQLite, but the wasm runtime uses remote SQLite.",
388
+ { metadata: { runtime: "wasm" } },
389
+ );
390
+ }
371
391
  return callWasmSync(() =>
372
392
  asActorFactoryHandle(
373
393
  new this.#constructors.ActorFactory(callbacks, config),
@@ -809,6 +829,13 @@ export class WasmCoreRuntime implements CoreRuntime {
809
829
  return await callWasm(() => this.#actorSql(ctx).exec(sql));
810
830
  }
811
831
 
832
+ actorSqlExecSync(
833
+ _ctx: ActorContextHandle,
834
+ _sql: string,
835
+ ): RuntimeSqlExecResult {
836
+ return synchronousSqliteUnavailable();
837
+ }
838
+
812
839
  async actorSqlExecute(
813
840
  ctx: ActorContextHandle,
814
841
  sql: string,
@@ -820,6 +847,14 @@ export class WasmCoreRuntime implements CoreRuntime {
820
847
  return normalizeRuntimeSqlExecuteResult(result);
821
848
  }
822
849
 
850
+ actorSqlExecuteSync(
851
+ _ctx: ActorContextHandle,
852
+ _sql: string,
853
+ _params?: RuntimeSqlBindParams,
854
+ ): RuntimeSqlExecuteResult {
855
+ return synchronousSqliteUnavailable();
856
+ }
857
+
823
858
  async actorSqlExecuteBatch(
824
859
  ctx: ActorContextHandle,
825
860
  statements: RuntimeSqlBatchStatement[],
@@ -840,6 +875,14 @@ export class WasmCoreRuntime implements CoreRuntime {
840
875
  )) as unknown as SqliteTransactionHandle;
841
876
  }
842
877
 
878
+ actorSqlBeginTransactionSync(
879
+ _ctx: ActorContextHandle,
880
+ _timeoutMs?: number,
881
+ _name?: string,
882
+ ): SqliteTransactionHandle {
883
+ return synchronousSqliteUnavailable();
884
+ }
885
+
843
886
  async actorSqlTransactionExec(
844
887
  transaction: SqliteTransactionHandle,
845
888
  sql: string,
@@ -853,6 +896,13 @@ export class WasmCoreRuntime implements CoreRuntime {
853
896
  );
854
897
  }
855
898
 
899
+ actorSqlTransactionExecSync(
900
+ _transaction: SqliteTransactionHandle,
901
+ _sql: string,
902
+ ): RuntimeSqlExecResult {
903
+ return synchronousSqliteUnavailable();
904
+ }
905
+
856
906
  async actorSqlTransactionExecute(
857
907
  transaction: SqliteTransactionHandle,
858
908
  sql: string,
@@ -871,6 +921,14 @@ export class WasmCoreRuntime implements CoreRuntime {
871
921
  return normalizeRuntimeSqlExecuteResult(result);
872
922
  }
873
923
 
924
+ actorSqlTransactionExecuteSync(
925
+ _transaction: SqliteTransactionHandle,
926
+ _sql: string,
927
+ _params?: RuntimeSqlBindParams,
928
+ ): RuntimeSqlExecuteResult {
929
+ return synchronousSqliteUnavailable();
930
+ }
931
+
874
932
  async actorSqlTransactionCommit(
875
933
  transaction: SqliteTransactionHandle,
876
934
  ): Promise<void> {
@@ -879,6 +937,10 @@ export class WasmCoreRuntime implements CoreRuntime {
879
937
  );
880
938
  }
881
939
 
940
+ actorSqlTransactionCommitSync(_transaction: SqliteTransactionHandle): void {
941
+ synchronousSqliteUnavailable();
942
+ }
943
+
882
944
  async actorSqlTransactionRollback(
883
945
  transaction: SqliteTransactionHandle,
884
946
  ): Promise<void> {
@@ -888,6 +950,13 @@ export class WasmCoreRuntime implements CoreRuntime {
888
950
  ).rollback(),
889
951
  );
890
952
  }
953
+
954
+ actorSqlTransactionRollbackSync(
955
+ _transaction: SqliteTransactionHandle,
956
+ ): void {
957
+ synchronousSqliteUnavailable();
958
+ }
959
+
891
960
  async actorBeginStateTransaction(
892
961
  ctx: ActorContextHandle,
893
962
  timeoutMs?: number,
@@ -970,6 +1039,25 @@ export class WasmCoreRuntime implements CoreRuntime {
970
1039
  return this.#actorSql(ctx).takeLastKvError?.() ?? null;
971
1040
  }
972
1041
 
1042
+ async actorSqlOpen(_ctx: ActorContextHandle): Promise<void> {
1043
+ // Remote SQLite opens on the engine side with each request.
1044
+ }
1045
+
1046
+ actorSqlCommitTicket(_ctx: ActorContextHandle): number {
1047
+ return synchronousSqliteUnavailable();
1048
+ }
1049
+
1050
+ actorSqlAbortGeneration(_ctx: ActorContextHandle, _reason: string): void {
1051
+ synchronousSqliteUnavailable();
1052
+ }
1053
+
1054
+ async actorSqlWhenDurable(
1055
+ _ctx: ActorContextHandle,
1056
+ _ticket: number,
1057
+ ): Promise<void> {
1058
+ synchronousSqliteUnavailable();
1059
+ }
1060
+
973
1061
  async actorSqlClose(ctx: ActorContextHandle): Promise<void> {
974
1062
  const wasmCtx = asWasmActorContext(ctx);
975
1063
  const database = this.#sql.get(wasmCtx);