rivetkit 0.0.0-sqlite-query-metrics-v3.1dff84f → 0.0.0-sqlite-sync-3-0-6-final.cf8679c

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 (118) hide show
  1. package/dist/browser/client.d.ts +95 -20
  2. package/dist/browser/client.js +2 -2
  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-HXJ5U2ZI.cjs → chunk-275ZGRSG.cjs} +3 -3
  7. package/dist/tsup/{chunk-HXJ5U2ZI.cjs.map → chunk-275ZGRSG.cjs.map} +1 -1
  8. package/dist/tsup/{chunk-HFGXV2CC.cjs → chunk-E54YPM4V.cjs} +10 -10
  9. package/dist/tsup/{chunk-HFGXV2CC.cjs.map → chunk-E54YPM4V.cjs.map} +1 -1
  10. package/dist/tsup/{chunk-Y4QLVCCS.cjs → chunk-I26AW6BG.cjs} +2 -2
  11. package/dist/tsup/{chunk-Y4QLVCCS.cjs.map → chunk-I26AW6BG.cjs.map} +1 -1
  12. package/dist/tsup/{chunk-CWN7N5AF.cjs → chunk-JHHUCWBC.cjs} +7 -7
  13. package/dist/tsup/{chunk-CWN7N5AF.cjs.map → chunk-JHHUCWBC.cjs.map} +1 -1
  14. package/dist/tsup/{chunk-22S5RCCJ.cjs → chunk-LYXPQB6H.cjs} +3 -3
  15. package/dist/tsup/{chunk-22S5RCCJ.cjs.map → chunk-LYXPQB6H.cjs.map} +1 -1
  16. package/dist/tsup/{chunk-NNCL4IBC.js → chunk-M72ZKQ7R.js} +2 -2
  17. package/dist/tsup/{chunk-NNCL4IBC.js.map → chunk-M72ZKQ7R.js.map} +1 -1
  18. package/dist/tsup/{chunk-QQIFRR3B.js → chunk-ME74LHJM.js} +7 -7
  19. package/dist/tsup/chunk-ME74LHJM.js.map +1 -0
  20. package/dist/tsup/{chunk-UXDASKP6.js → chunk-PBIQPYE5.js} +12 -13
  21. package/dist/tsup/chunk-PBIQPYE5.js.map +1 -0
  22. package/dist/tsup/{chunk-2CTZU7GL.js → chunk-R4QCQBB4.js} +2 -2
  23. package/dist/tsup/{chunk-SW33HMM5.js → chunk-RJ7WDGOJ.js} +3 -3
  24. package/dist/tsup/{chunk-NG5CUXMM.cjs → chunk-SJJWJU26.cjs} +145 -145
  25. package/dist/tsup/chunk-SJJWJU26.cjs.map +1 -0
  26. package/dist/tsup/{chunk-HRKFVQPS.js → chunk-SSMFNH4N.js} +4 -4
  27. package/dist/tsup/{chunk-Y5CFDGPA.cjs → chunk-TKCX6KNZ.cjs} +4 -4
  28. package/dist/tsup/{chunk-Y5CFDGPA.cjs.map → chunk-TKCX6KNZ.cjs.map} +1 -1
  29. package/dist/tsup/{chunk-JJUSO7GJ.cjs → chunk-TVBQQXQE.cjs} +3 -3
  30. package/dist/tsup/{chunk-JJUSO7GJ.cjs.map → chunk-TVBQQXQE.cjs.map} +1 -1
  31. package/dist/tsup/{chunk-F5PWAGS7.js → chunk-VXVJHTW6.js} +2 -2
  32. package/dist/tsup/{chunk-FT6VRK4C.js → chunk-XATHAIZX.js} +2 -2
  33. package/dist/tsup/{chunk-D7DWNYEO.js → chunk-Y5TV6V5Z.js} +2 -2
  34. package/dist/tsup/{chunk-SSF2XC33.cjs → chunk-YWWDUC6D.cjs} +12 -13
  35. package/dist/tsup/chunk-YWWDUC6D.cjs.map +1 -0
  36. package/dist/tsup/client/mod.cjs +5 -5
  37. package/dist/tsup/client/mod.d.cts +3 -3
  38. package/dist/tsup/client/mod.d.ts +3 -3
  39. package/dist/tsup/client/mod.js +4 -4
  40. package/dist/tsup/common/log.cjs +2 -2
  41. package/dist/tsup/common/log.js +1 -1
  42. package/dist/tsup/common/websocket.cjs +3 -3
  43. package/dist/tsup/common/websocket.js +2 -2
  44. package/dist/tsup/{config-BnMhcPHl.d.cts → config-5K0rzjv1.d.cts} +72 -25
  45. package/dist/tsup/{config-CBTr4IhE.d.cts → config-Df5jOcde.d.cts} +26 -0
  46. package/dist/tsup/{config-CBTr4IhE.d.ts → config-Df5jOcde.d.ts} +26 -0
  47. package/dist/tsup/{config-BiiS5T5M.d.ts → config-DsAjIyfZ.d.ts} +72 -25
  48. package/dist/tsup/{context-iMxXq_Lt.d.ts → context-B19Rx5Zg.d.cts} +5 -2
  49. package/dist/tsup/{context-DVE8XMm1.d.cts → context-BEh8x4jn.d.ts} +5 -2
  50. package/dist/tsup/db/drizzle.cjs +2 -2
  51. package/dist/tsup/db/drizzle.d.cts +1 -1
  52. package/dist/tsup/db/drizzle.d.ts +1 -1
  53. package/dist/tsup/db/drizzle.js +1 -1
  54. package/dist/tsup/db/mod.cjs +3 -3
  55. package/dist/tsup/db/mod.d.cts +2 -2
  56. package/dist/tsup/db/mod.d.ts +2 -2
  57. package/dist/tsup/db/mod.js +2 -2
  58. package/dist/tsup/dynamic/mod.cjs +2 -2
  59. package/dist/tsup/dynamic/mod.d.cts +2 -2
  60. package/dist/tsup/dynamic/mod.d.ts +2 -2
  61. package/dist/tsup/dynamic/mod.js +1 -1
  62. package/dist/tsup/inspector/mod.cjs +5 -5
  63. package/dist/tsup/inspector/mod.js +4 -4
  64. package/dist/tsup/inspector/workflow.cjs +4 -4
  65. package/dist/tsup/inspector/workflow.js +3 -3
  66. package/dist/tsup/inspector-tab/mod.cjs +2 -2
  67. package/dist/tsup/inspector-tab/mod.d.cts +3 -3
  68. package/dist/tsup/inspector-tab/mod.d.ts +3 -3
  69. package/dist/tsup/inspector-tab/mod.js +1 -1
  70. package/dist/tsup/mod.cjs +548 -169
  71. package/dist/tsup/mod.cjs.map +1 -1
  72. package/dist/tsup/mod.d.cts +6 -4
  73. package/dist/tsup/mod.d.ts +6 -4
  74. package/dist/tsup/mod.js +447 -68
  75. package/dist/tsup/mod.js.map +1 -1
  76. package/dist/tsup/test/mod.cjs +6 -6
  77. package/dist/tsup/test/mod.d.cts +2 -2
  78. package/dist/tsup/test/mod.d.ts +2 -2
  79. package/dist/tsup/test/mod.js +4 -4
  80. package/dist/tsup/unstable/migrations.d.cts +1 -1
  81. package/dist/tsup/unstable/migrations.d.ts +1 -1
  82. package/dist/tsup/utils.cjs +2 -2
  83. package/dist/tsup/utils.js +1 -1
  84. package/dist/tsup/workflow/mod.cjs +13 -10
  85. package/dist/tsup/workflow/mod.cjs.map +1 -1
  86. package/dist/tsup/workflow/mod.d.cts +4 -4
  87. package/dist/tsup/workflow/mod.d.ts +4 -4
  88. package/dist/tsup/workflow/mod.js +8 -5
  89. package/dist/tsup/workflow/mod.js.map +1 -1
  90. package/package.json +9 -9
  91. package/src/actor/config.ts +41 -29
  92. package/src/actor/contexts/index.ts +2 -0
  93. package/src/actor/definition.ts +33 -1
  94. package/src/actor/schema.ts +4 -0
  95. package/src/client/actor-common.ts +4 -0
  96. package/src/client/actor-conn.ts +2 -0
  97. package/src/client/actor-handle.ts +2 -0
  98. package/src/client/config.ts +1 -1
  99. package/src/client/queue.ts +2 -0
  100. package/src/common/database/config.ts +39 -0
  101. package/src/common/database/native-database.ts +230 -21
  102. package/src/registry/index.ts +142 -1
  103. package/src/registry/napi-runtime.ts +93 -4
  104. package/src/registry/native.ts +61 -28
  105. package/src/registry/runtime.ts +58 -8
  106. package/src/registry/wasm-runtime.ts +88 -14
  107. package/src/workflow/context.ts +3 -0
  108. package/dist/tsup/chunk-NG5CUXMM.cjs.map +0 -1
  109. package/dist/tsup/chunk-QQIFRR3B.js.map +0 -1
  110. package/dist/tsup/chunk-SSF2XC33.cjs.map +0 -1
  111. package/dist/tsup/chunk-UXDASKP6.js.map +0 -1
  112. package/src/common/bare/generated/actor-runtime-socket-protocol/v1.ts +0 -848
  113. /package/dist/tsup/{chunk-2CTZU7GL.js.map → chunk-R4QCQBB4.js.map} +0 -0
  114. /package/dist/tsup/{chunk-SW33HMM5.js.map → chunk-RJ7WDGOJ.js.map} +0 -0
  115. /package/dist/tsup/{chunk-HRKFVQPS.js.map → chunk-SSMFNH4N.js.map} +0 -0
  116. /package/dist/tsup/{chunk-F5PWAGS7.js.map → chunk-VXVJHTW6.js.map} +0 -0
  117. /package/dist/tsup/{chunk-FT6VRK4C.js.map → chunk-XATHAIZX.js.map} +0 -0
  118. /package/dist/tsup/{chunk-D7DWNYEO.js.map → chunk-Y5TV6V5Z.js.map} +0 -0
@@ -20,6 +20,7 @@ import { logger } from "./log";
20
20
  import { buildConfiguredRegistry } from "./native";
21
21
  import { convertNativeHttpResponse } from "./native-http";
22
22
  import type {
23
+ CancellationTokenHandle,
23
24
  CoreRuntime,
24
25
  RuntimeApplicationFetch,
25
26
  RuntimeHttpResponseBodyStream,
@@ -28,6 +29,8 @@ import type {
28
29
 
29
30
  type ShutdownSignal = "SIGINT" | "SIGTERM";
30
31
 
32
+ type ConfiguredRegistry = Awaited<ReturnType<typeof buildConfiguredRegistry>>;
33
+
31
34
  const REGISTRY_READY_TIMEOUT_MS = 30_000;
32
35
  // A managed Engine restart can wait for the old process to release storage and
33
36
  // for its worker lease to expire before the replacement can register.
@@ -36,6 +39,9 @@ const LOCAL_ENGINE_READY_TIMEOUT_MS = 60_000;
36
39
  // reports. Leave time to close the node connection after that, while staying
37
40
  // below the five-second force-kill ceiling used by tsx watch.
38
41
  const LOCAL_DEV_SHUTDOWN_GRACE_PERIOD_MS = 4_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;
39
45
 
40
46
  // Serverless is disabled for v3; keep the implementation for a later release.
41
47
  function rejectServerless(): void {
@@ -176,6 +182,11 @@ export class Registry<A extends RegistryActors> {
176
182
  #shutdownInstalled = false;
177
183
  #shutdownInFlight: Promise<void> | null = null;
178
184
  #signalHandlers: Partial<Record<ShutdownSignal, () => void>> = {};
185
+ #envoyConfiguredRegistry?: ConfiguredRegistry;
186
+ #activeActorCountListeners = new Set<(count: number) => void>();
187
+ #activeActorCountToken?: CancellationTokenHandle;
188
+ #lastActiveActorCount?: number;
189
+ #activeActorCountSupported?: boolean;
179
190
 
180
191
  constructor(config: RegistryConfigInput<A>, deps?: Partial<RegistryDeps>) {
181
192
  this.#config = config;
@@ -674,6 +685,21 @@ export class Registry<A extends RegistryActors> {
674
685
  const configuredRegistryPromise =
675
686
  this.#buildConfiguredRegistry(config);
676
687
  this.#runtimeServeConfiguredPromise = configuredRegistryPromise;
688
+ configuredRegistryPromise.then(
689
+ (configured) => {
690
+ this.#envoyConfiguredRegistry = configured;
691
+ try {
692
+ this.#syncActiveActorCountSubscription();
693
+ } catch (error) {
694
+ logger().error(
695
+ { error },
696
+ "failed to subscribe to the active actor count",
697
+ );
698
+ }
699
+ },
700
+ // The serve lifecycle below reports build failures.
701
+ () => undefined,
702
+ );
677
703
  this.#runtimeServeLifecyclePromise = configuredRegistryPromise.then(
678
704
  async ({ runtime, registry, serveConfig }) => {
679
705
  await runtime.serveRegistry(registry, serveConfig);
@@ -799,7 +825,7 @@ export class Registry<A extends RegistryActors> {
799
825
  ? LOCAL_DEV_SHUTDOWN_GRACE_PERIOD_MS
800
826
  : ((await this.#actorStopThresholdMs(
801
827
  modeAPromise ?? modeBPromise,
802
- )) ?? 30 * 60 * 1000);
828
+ )) ?? DEFAULT_ACTOR_STOP_THRESHOLD_MS);
803
829
  }
804
830
  // Race the entire drain sequence (both modes + serve promise) against
805
831
  // a single grace ceiling. Local dev signal shutdowns allow the watcher
@@ -1026,6 +1052,121 @@ export class Registry<A extends RegistryActors> {
1026
1052
  this.#startEnvoy(config, true);
1027
1053
  }
1028
1054
 
1055
+ /**
1056
+ * Returns the number of actors hosted by this process's envoy, which is zero
1057
+ * while it is not serving. Requires the native runtime.
1058
+ */
1059
+ public activeActorCount(): number {
1060
+ this.#assertActiveActorCountSupported("activeActorCount");
1061
+ const configured = this.#envoyConfiguredRegistry;
1062
+ if (!configured) return 0;
1063
+ const { runtime, registry } = configured;
1064
+ if (!runtime.registryActiveActorCount) {
1065
+ throw new Error(
1066
+ "registry.activeActorCount() is not supported by this runtime",
1067
+ );
1068
+ }
1069
+ return runtime.registryActiveActorCount(registry);
1070
+ }
1071
+
1072
+ /**
1073
+ * Calls `callback` with the number of actors hosted by this process's envoy
1074
+ * and again whenever it changes. Rapid changes may coalesce.
1075
+ * Returns a function that unsubscribes. Requires the native runtime.
1076
+ *
1077
+ * @example
1078
+ * ```ts
1079
+ * const unsubscribe = registry.onActiveActorCountChange((count) => {
1080
+ * setTaskProtection(count > 0);
1081
+ * });
1082
+ * ```
1083
+ */
1084
+ public onActiveActorCountChange(
1085
+ callback: (count: number) => void,
1086
+ ): () => void {
1087
+ this.#assertActiveActorCountSupported("onActiveActorCountChange");
1088
+ // Wrap so the same callback can subscribe twice and unsubscribe independently.
1089
+ const listener = (count: number) => callback(count);
1090
+ this.#activeActorCountListeners.add(listener);
1091
+ this.#syncActiveActorCountSubscription();
1092
+
1093
+ // Start late listeners from the stream, not a direct read, which could see a
1094
+ // change the stream coalesced away and never corrects.
1095
+ const lastCount = this.#lastActiveActorCount;
1096
+ if (lastCount !== undefined) {
1097
+ queueMicrotask(() => {
1098
+ if (this.#activeActorCountListeners.has(listener)) {
1099
+ this.#deliverActiveActorCount(listener, lastCount);
1100
+ }
1101
+ });
1102
+ }
1103
+
1104
+ return () => {
1105
+ this.#activeActorCountListeners.delete(listener);
1106
+ this.#syncActiveActorCountSubscription();
1107
+ };
1108
+ }
1109
+
1110
+ #assertActiveActorCountSupported(method: string): void {
1111
+ this.#activeActorCountSupported ??=
1112
+ this.parseConfig().runtime !== "wasm";
1113
+ if (!this.#activeActorCountSupported) {
1114
+ throw new Error(
1115
+ `registry.${method}() requires the native runtime; WebAssembly registries do not host an envoy`,
1116
+ );
1117
+ }
1118
+ }
1119
+
1120
+ /** Holds one native subscription while any listener is registered. */
1121
+ #syncActiveActorCountSubscription(): void {
1122
+ const configured = this.#envoyConfiguredRegistry;
1123
+ if (!configured) return;
1124
+ const { runtime, registry } = configured;
1125
+ const token = this.#activeActorCountToken;
1126
+
1127
+ if (this.#activeActorCountListeners.size === 0) {
1128
+ if (token !== undefined) {
1129
+ this.#activeActorCountToken = undefined;
1130
+ this.#lastActiveActorCount = undefined;
1131
+ runtime.cancelCancellationToken(token);
1132
+ }
1133
+ return;
1134
+ }
1135
+ if (token !== undefined) return;
1136
+
1137
+ if (!runtime.onRegistryActiveActorCountChange) {
1138
+ throw new Error(
1139
+ "active actor count subscriptions are not supported by this runtime",
1140
+ );
1141
+ }
1142
+ const nextToken = runtime.createCancellationToken();
1143
+ this.#activeActorCountToken = nextToken;
1144
+ runtime.onRegistryActiveActorCountChange(
1145
+ registry,
1146
+ (count) => {
1147
+ // Drop calls still queued from a released subscription.
1148
+ if (this.#activeActorCountToken !== nextToken) return;
1149
+ this.#lastActiveActorCount = count;
1150
+ // Iterate a copy so listeners added by a callback are not called twice.
1151
+ for (const listener of [...this.#activeActorCountListeners]) {
1152
+ this.#deliverActiveActorCount(listener, count);
1153
+ }
1154
+ },
1155
+ nextToken,
1156
+ );
1157
+ }
1158
+
1159
+ #deliverActiveActorCount(
1160
+ listener: (count: number) => void,
1161
+ count: number,
1162
+ ): void {
1163
+ try {
1164
+ listener(count);
1165
+ } catch (error) {
1166
+ logger().error({ error }, "active actor count listener threw");
1167
+ }
1168
+ }
1169
+
1029
1170
  #printWelcome(
1030
1171
  config: RegistryConfig,
1031
1172
  kind: "serverless" | "serverful",
@@ -354,6 +354,21 @@ export class NapiCoreRuntime implements CoreRuntime {
354
354
  );
355
355
  }
356
356
 
357
+ registryActiveActorCount(registry: RegistryHandle): number {
358
+ return asNativeRegistry(registry).activeActorCount();
359
+ }
360
+
361
+ onRegistryActiveActorCountChange(
362
+ registry: RegistryHandle,
363
+ callback: (count: number) => void,
364
+ cancelToken: CancellationTokenHandle,
365
+ ): void {
366
+ asNativeRegistry(registry).onActiveActorCountChange(
367
+ callback,
368
+ asNativeCancellationToken(cancelToken),
369
+ );
370
+ }
371
+
357
372
  async registryHealth(
358
373
  registry: RegistryHandle,
359
374
  ): Promise<RuntimeRegistryRouteResponse> {
@@ -860,6 +875,13 @@ export class NapiCoreRuntime implements CoreRuntime {
860
875
  return await this.#actorSql(ctx).exec(sql);
861
876
  }
862
877
 
878
+ actorSqlExecSync(
879
+ ctx: ActorContextHandle,
880
+ sql: string,
881
+ ): RuntimeSqlExecResult {
882
+ return this.#actorSql(ctx).execSync(sql);
883
+ }
884
+
863
885
  async actorSqlExecute(
864
886
  ctx: ActorContextHandle,
865
887
  sql: string,
@@ -872,6 +894,18 @@ export class NapiCoreRuntime implements CoreRuntime {
872
894
  return normalizeRuntimeSqlExecuteResult(result);
873
895
  }
874
896
 
897
+ actorSqlExecuteSync(
898
+ ctx: ActorContextHandle,
899
+ sql: string,
900
+ params?: RuntimeSqlBindParams,
901
+ ): RuntimeSqlExecuteResult {
902
+ const result = this.#actorSql(ctx).executeSync(
903
+ sql,
904
+ toNapiSqlBindParams(params),
905
+ );
906
+ return normalizeRuntimeSqlExecuteResult(result);
907
+ }
908
+
875
909
  async actorSqlExecuteBatch(
876
910
  ctx: ActorContextHandle,
877
911
  statements: RuntimeSqlBatchStatement[],
@@ -893,6 +927,17 @@ export class NapiCoreRuntime implements CoreRuntime {
893
927
  )) as unknown as SqliteTransactionHandle;
894
928
  }
895
929
 
930
+ actorSqlBeginTransactionSync(
931
+ ctx: ActorContextHandle,
932
+ timeoutMs?: number,
933
+ name?: string,
934
+ ): SqliteTransactionHandle {
935
+ return this.#actorSql(ctx).beginTransactionSync(
936
+ timeoutMs,
937
+ name,
938
+ ) as unknown as SqliteTransactionHandle;
939
+ }
940
+
896
941
  async actorSqlTransactionExec(
897
942
  transaction: SqliteTransactionHandle,
898
943
  sql: string,
@@ -900,6 +945,13 @@ export class NapiCoreRuntime implements CoreRuntime {
900
945
  return await asNativeSqlTransaction(transaction).exec(sql);
901
946
  }
902
947
 
948
+ actorSqlTransactionExecSync(
949
+ transaction: SqliteTransactionHandle,
950
+ sql: string,
951
+ ): RuntimeSqlExecResult {
952
+ return asNativeSqlTransaction(transaction).execSync(sql);
953
+ }
954
+
903
955
  async actorSqlTransactionExecute(
904
956
  transaction: SqliteTransactionHandle,
905
957
  sql: string,
@@ -912,17 +964,39 @@ export class NapiCoreRuntime implements CoreRuntime {
912
964
  return normalizeRuntimeSqlExecuteResult(result);
913
965
  }
914
966
 
967
+ actorSqlTransactionExecuteSync(
968
+ transaction: SqliteTransactionHandle,
969
+ sql: string,
970
+ params?: RuntimeSqlBindParams,
971
+ ): RuntimeSqlExecuteResult {
972
+ const result = asNativeSqlTransaction(transaction).executeSync(
973
+ sql,
974
+ toNapiSqlBindParams(params),
975
+ );
976
+ return normalizeRuntimeSqlExecuteResult(result);
977
+ }
978
+
915
979
  async actorSqlTransactionCommit(
916
980
  transaction: SqliteTransactionHandle,
917
981
  ): Promise<void> {
918
982
  await asNativeSqlTransaction(transaction).commit();
919
983
  }
920
984
 
985
+ actorSqlTransactionCommitSync(transaction: SqliteTransactionHandle): void {
986
+ asNativeSqlTransaction(transaction).commitSync();
987
+ }
988
+
921
989
  async actorSqlTransactionRollback(
922
990
  transaction: SqliteTransactionHandle,
923
991
  ): Promise<void> {
924
992
  await asNativeSqlTransaction(transaction).rollback();
925
993
  }
994
+
995
+ actorSqlTransactionRollbackSync(
996
+ transaction: SqliteTransactionHandle,
997
+ ): void {
998
+ asNativeSqlTransaction(transaction).rollbackSync();
999
+ }
926
1000
  async actorBeginStateTransaction(
927
1001
  ctx: ActorContextHandle,
928
1002
  timeoutMs?: number,
@@ -986,6 +1060,25 @@ export class NapiCoreRuntime implements CoreRuntime {
986
1060
  return this.#actorSql(ctx).takeLastKvError?.() ?? null;
987
1061
  }
988
1062
 
1063
+ async actorSqlOpen(ctx: ActorContextHandle): Promise<void> {
1064
+ await this.#actorSql(ctx).open();
1065
+ }
1066
+
1067
+ actorSqlCommitTicket(ctx: ActorContextHandle): number {
1068
+ return this.#actorSql(ctx).commitTicket();
1069
+ }
1070
+
1071
+ actorSqlAbortGeneration(ctx: ActorContextHandle, reason: string): void {
1072
+ this.#actorSql(ctx).abortGeneration(reason);
1073
+ }
1074
+
1075
+ async actorSqlWhenDurable(
1076
+ ctx: ActorContextHandle,
1077
+ ticket: number,
1078
+ ): Promise<void> {
1079
+ await this.#actorSql(ctx).whenDurable(ticket);
1080
+ }
1081
+
989
1082
  async actorSqlClose(ctx: ActorContextHandle): Promise<void> {
990
1083
  const nativeCtx = asNativeActorContext(ctx);
991
1084
  if (!this.#sqlOpened.delete(nativeCtx)) {
@@ -994,10 +1087,6 @@ export class NapiCoreRuntime implements CoreRuntime {
994
1087
  await nativeCtx.sql().close();
995
1088
  }
996
1089
 
997
- async actorRuntimeSocketProvision(ctx: ActorContextHandle) {
998
- return await asNativeActorContext(ctx).provisionActorRuntimeSocket();
999
- }
1000
-
1001
1090
  async actorQueueSend(
1002
1091
  ctx: ActorContextHandle,
1003
1092
  name: string,
@@ -61,7 +61,11 @@ import {
61
61
  db as defaultDb,
62
62
  registerNativeStateTransactionOpener,
63
63
  } from "@/common/database/mod";
64
- import { wrapJsNativeDatabase } from "@/common/database/native-database";
64
+ import {
65
+ type JsNativeDatabaseLike,
66
+ type JsNativeSynchronousTransactionLike,
67
+ wrapJsNativeDatabase,
68
+ } from "@/common/database/native-database";
65
69
  import { assertJsonCompatValue, type JsonCompatValue } from "@/common/encoding";
66
70
  import { isResponseLike } from "@/common/fetch-like";
67
71
  import {
@@ -125,6 +129,7 @@ import type {
125
129
  RuntimeScheduledFireInfo,
126
130
  RuntimeServeConfig,
127
131
  RuntimeStateDeltaPayload,
132
+ SqliteTransactionHandle,
128
133
  WebSocketHandle,
129
134
  } from "./runtime";
130
135
  import { servicesBinaryUnavailable } from "./runtime";
@@ -655,6 +660,26 @@ async function closeNativeDatabaseClient(
655
660
  }
656
661
  }
657
662
 
663
+ function createNativeSqlTransaction(
664
+ runtime: CoreRuntime,
665
+ transaction: SqliteTransactionHandle,
666
+ ): JsNativeSynchronousTransactionLike {
667
+ return {
668
+ exec: (sql) => runtime.actorSqlTransactionExec(transaction, sql),
669
+ execSync: (sql) =>
670
+ runtime.actorSqlTransactionExecSync(transaction, sql),
671
+ execute: (sql, params) =>
672
+ runtime.actorSqlTransactionExecute(transaction, sql, params),
673
+ executeSync: (sql, params) =>
674
+ runtime.actorSqlTransactionExecuteSync(transaction, sql, params),
675
+ commit: () => runtime.actorSqlTransactionCommit(transaction),
676
+ commitSync: () => runtime.actorSqlTransactionCommitSync(transaction),
677
+ rollback: () => runtime.actorSqlTransactionRollback(transaction),
678
+ rollbackSync: () =>
679
+ runtime.actorSqlTransactionRollbackSync(transaction),
680
+ };
681
+ }
682
+
658
683
  function getOrCreateNativeSqlDatabase(
659
684
  runtime: CoreRuntime,
660
685
  ctx: ActorContextHandle,
@@ -665,9 +690,12 @@ function getOrCreateNativeSqlDatabase(
665
690
  return cachedDatabase;
666
691
  }
667
692
 
668
- const database = wrapJsNativeDatabase({
693
+ const bindings: JsNativeDatabaseLike = {
669
694
  exec: (sql) => runtime.actorSqlExec(ctx, sql),
695
+ execSync: (sql) => runtime.actorSqlExecSync(ctx, sql),
670
696
  execute: (sql, params) => runtime.actorSqlExecute(ctx, sql, params),
697
+ executeSync: (sql, params) =>
698
+ runtime.actorSqlExecuteSync(ctx, sql, params),
671
699
  executeBatch: (statements) =>
672
700
  runtime.actorSqlExecuteBatch(ctx, statements),
673
701
  beginTransaction: async (timeoutMs, name) => {
@@ -676,19 +704,15 @@ function getOrCreateNativeSqlDatabase(
676
704
  timeoutMs,
677
705
  name,
678
706
  );
679
- return {
680
- exec: (sql) =>
681
- runtime.actorSqlTransactionExec(transaction, sql),
682
- execute: (sql, params) =>
683
- runtime.actorSqlTransactionExecute(
684
- transaction,
685
- sql,
686
- params,
687
- ),
688
- commit: () => runtime.actorSqlTransactionCommit(transaction),
689
- rollback: () =>
690
- runtime.actorSqlTransactionRollback(transaction),
691
- };
707
+ return createNativeSqlTransaction(runtime, transaction);
708
+ },
709
+ beginTransactionSync: (timeoutMs, name) => {
710
+ const transaction = runtime.actorSqlBeginTransactionSync(
711
+ ctx,
712
+ timeoutMs,
713
+ name,
714
+ );
715
+ return createNativeSqlTransaction(runtime, transaction);
692
716
  },
693
717
  beginStateTransaction: async (timeoutMs, context) => {
694
718
  const scope = context as NativeStateTransactionScope | undefined;
@@ -728,8 +752,14 @@ function getOrCreateNativeSqlDatabase(
728
752
  run: (sql, params) => runtime.actorSqlRun(ctx, sql, params),
729
753
  metrics: () => runtime.actorSqlMetrics(ctx),
730
754
  takeLastKvError: () => runtime.actorSqlTakeLastKvError(ctx),
755
+ open: () => runtime.actorSqlOpen(ctx),
756
+ commitTicket: () => runtime.actorSqlCommitTicket(ctx),
757
+ whenDurable: (ticket) => runtime.actorSqlWhenDurable(ctx, ticket),
758
+ abortGeneration: (reason) =>
759
+ runtime.actorSqlAbortGeneration(ctx, reason),
731
760
  close: () => runtime.actorSqlClose(ctx),
732
- });
761
+ };
762
+ const database = wrapJsNativeDatabase(bindings);
733
763
  runtimeState.sql = database;
734
764
  return database;
735
765
  }
@@ -2858,12 +2888,6 @@ export class ActorContextHandleAdapter {
2858
2888
  return this.#kv;
2859
2889
  }
2860
2890
 
2861
- async actorRuntimeSocket(): Promise<{ path: string }> {
2862
- return await callNative(() =>
2863
- this.#runtime.actorRuntimeSocketProvision(this.#ctx),
2864
- );
2865
- }
2866
-
2867
2891
  get db() {
2868
2892
  if (!this.#databaseProvider) {
2869
2893
  throw databaseNotConfiguredError();
@@ -3185,10 +3209,12 @@ export class ActorContextHandleAdapter {
3185
3209
  {
3186
3210
  open: async (requestedActorId) => {
3187
3211
  void requestedActorId;
3188
- return getOrCreateNativeSqlDatabase(
3212
+ const database = getOrCreateNativeSqlDatabase(
3189
3213
  this.#runtime,
3190
3214
  this.#ctx,
3191
3215
  );
3216
+ await database.open();
3217
+ return database;
3192
3218
  },
3193
3219
  },
3194
3220
  async (timeoutMs, context) =>
@@ -3529,17 +3555,17 @@ export class ActorContextHandleAdapter {
3529
3555
  // need to keep an actor awake should use `keepAwake(promise)` or
3530
3556
  // `waitUntil(promise)` so the native counter machinery in rivetkit-core
3531
3557
  // owns the lifecycle.
3532
- /** @deprecated Use `keepAwake(promise)` or `waitUntil(promise)` instead. */
3558
+ /** @deprecated `setPreventSleep` was deprecated in 3.0 and will be removed in a future version. It is a no-op. Use `c.keepAwake(promise)` or `c.waitUntil(promise)`. See https://rivet.dev/actors/docs/deprecated/prevent-sleep */
3533
3559
  setPreventSleep(_preventSleep: boolean): void {
3534
3560
  logger().warn({
3535
- msg: "setPreventSleep is deprecated and is a no-op; use keepAwake(promise) or waitUntil(promise) instead",
3561
+ msg: "setPreventSleep was deprecated in 3.0 and will be removed in a future version. It is a no-op; use keepAwake(promise) or waitUntil(promise) instead. See https://rivet.dev/actors/docs/deprecated/prevent-sleep",
3536
3562
  });
3537
3563
  }
3538
3564
 
3539
- /** @deprecated Use `keepAwake(promise)` or `waitUntil(promise)` instead. */
3565
+ /** @deprecated `preventSleep` was deprecated in 3.0 and will be removed in a future version. It is a no-op that always returns `false`. Use `c.keepAwake(promise)` or `c.waitUntil(promise)`. See https://rivet.dev/actors/docs/deprecated/prevent-sleep */
3540
3566
  get preventSleep(): boolean {
3541
3567
  logger().warn({
3542
- msg: "preventSleep is deprecated and always returns false; use keepAwake(promise) or waitUntil(promise) instead",
3568
+ msg: "preventSleep was deprecated in 3.0 and will be removed in a future version. It always returns false; use keepAwake(promise) or waitUntil(promise) instead. See https://rivet.dev/actors/docs/deprecated/prevent-sleep",
3543
3569
  });
3544
3570
  return false;
3545
3571
  }
@@ -3985,6 +4011,8 @@ function buildActorTracingConfig(
3985
4011
  };
3986
4012
  }
3987
4013
 
4014
+ const SQLITE_DEFERRED_COMMITS = Symbol.for("rivetkit.sqlite.deferredCommits");
4015
+
3988
4016
  function buildActorConfig(
3989
4017
  definition: AnyActorDefinition,
3990
4018
  registryConfig: RegistryConfig,
@@ -3998,18 +4026,23 @@ function buildActorConfig(
3998
4026
  const sqliteProfiling = (
3999
4027
  config.db as { sqliteProfiling?: SqliteProfilingOptions } | undefined
4000
4028
  )?.sqliteProfiling;
4029
+ // Internal opt-in for embedders, such as Open Orange, that gate outputs on `whenDurable`.
4030
+ const sqliteDeferredCommits =
4031
+ (config.db as Record<symbol, unknown> | undefined)?.[
4032
+ SQLITE_DEFERRED_COMMITS
4033
+ ] === true;
4001
4034
 
4002
4035
  return {
4003
4036
  name: options.name as string | undefined,
4004
4037
  icon: options.icon as string | undefined,
4005
4038
  hasDatabase: true,
4006
4039
  remoteSqlite: usesRemoteSqlite,
4040
+ sqliteDeferredCommits: sqliteDeferredCommits || undefined,
4007
4041
  sqliteProfiling,
4008
4042
  tracing: buildActorTracingConfig(
4009
4043
  definition.config.actions,
4010
4044
  definition.config.tracing,
4011
4045
  ),
4012
- enableActorRuntimeSocket: options.enableActorRuntimeSocket === true,
4013
4046
  hasState:
4014
4047
  config.state !== undefined ||
4015
4048
  typeof config.createState === "function",
@@ -248,10 +248,6 @@ export type RuntimeSqlBindParam =
248
248
 
249
249
  export type RuntimeSqlBindParams = RuntimeSqlBindParam[] | null;
250
250
 
251
- export interface RuntimeActorRuntimeSocketEndpointInfo {
252
- path: string;
253
- }
254
-
255
251
  export interface RuntimeSqlQueryResult {
256
252
  columns: string[];
257
253
  rows: unknown[][];
@@ -284,10 +280,15 @@ export interface RuntimeSqlRunResult {
284
280
 
285
281
  export interface RuntimeSqlDatabase {
286
282
  exec(sql: string): Promise<RuntimeSqlExecResult>;
283
+ execSync(sql: string): RuntimeSqlExecResult;
287
284
  execute(
288
285
  sql: string,
289
286
  params?: RuntimeSqlBindParams,
290
287
  ): Promise<RuntimeSqlExecuteResult>;
288
+ executeSync(
289
+ sql: string,
290
+ params?: RuntimeSqlBindParams,
291
+ ): RuntimeSqlExecuteResult;
291
292
  executeBatch(
292
293
  statements: RuntimeSqlBatchStatement[],
293
294
  ): Promise<RuntimeSqlExecuteResult[]>;
@@ -303,19 +304,33 @@ export interface RuntimeSqlDatabase {
303
304
  timeoutMs?: number,
304
305
  name?: string,
305
306
  ): Promise<RuntimeSqlTransactionDatabase>;
307
+ beginTransactionSync(
308
+ timeoutMs?: number,
309
+ name?: string,
310
+ ): RuntimeSqlTransactionDatabase;
306
311
  metrics?(): SqliteNativeMetrics | null;
307
312
  takeLastKvError?(): string | null;
313
+ open(): Promise<void>;
314
+ commitTicket(): number;
315
+ whenDurable(ticket: number): Promise<void>;
308
316
  close(): Promise<void>;
309
317
  }
310
318
 
311
319
  export interface RuntimeSqlTransactionDatabase {
312
320
  exec(sql: string): Promise<RuntimeSqlExecResult>;
321
+ execSync(sql: string): RuntimeSqlExecResult;
313
322
  execute(
314
323
  sql: string,
315
324
  params?: RuntimeSqlBindParams,
316
325
  ): Promise<RuntimeSqlExecuteResult>;
326
+ executeSync(
327
+ sql: string,
328
+ params?: RuntimeSqlBindParams,
329
+ ): RuntimeSqlExecuteResult;
317
330
  commit(): Promise<void>;
331
+ commitSync(): void;
318
332
  rollback(): Promise<void>;
333
+ rollbackSync(): void;
319
334
  }
320
335
 
321
336
  /** Sample rates for the traces an actor starts, in the shape core takes. */
@@ -330,9 +345,9 @@ export interface RuntimeActorConfig {
330
345
  icon?: string;
331
346
  hasDatabase?: boolean;
332
347
  remoteSqlite?: boolean;
348
+ sqliteDeferredCommits?: boolean;
333
349
  sqliteProfiling?: SqliteProfilingOptions;
334
350
  tracing?: RuntimeActorTracingConfig;
335
- enableActorRuntimeSocket?: boolean;
336
351
  hasState?: boolean;
337
352
  canHibernateWebsocket?: boolean;
338
353
  stateSaveIntervalMs?: number;
@@ -500,6 +515,12 @@ export interface CoreRuntime {
500
515
  registryActorStopThresholdMs?(
501
516
  registry: RegistryHandle,
502
517
  ): Promise<number | undefined>;
518
+ registryActiveActorCount?(registry: RegistryHandle): number;
519
+ onRegistryActiveActorCountChange?(
520
+ registry: RegistryHandle,
521
+ callback: (count: number) => void,
522
+ cancelToken: CancellationTokenHandle,
523
+ ): void;
503
524
  handleServerlessRequest(
504
525
  registry: RegistryHandle,
505
526
  req: RuntimeServerlessRequest,
@@ -696,11 +717,20 @@ export interface CoreRuntime {
696
717
  ctx: ActorContextHandle,
697
718
  sql: string,
698
719
  ): Promise<RuntimeSqlExecResult>;
720
+ actorSqlExecSync(
721
+ ctx: ActorContextHandle,
722
+ sql: string,
723
+ ): RuntimeSqlExecResult;
699
724
  actorSqlExecute(
700
725
  ctx: ActorContextHandle,
701
726
  sql: string,
702
727
  params?: RuntimeSqlBindParams,
703
728
  ): Promise<RuntimeSqlExecuteResult>;
729
+ actorSqlExecuteSync(
730
+ ctx: ActorContextHandle,
731
+ sql: string,
732
+ params?: RuntimeSqlBindParams,
733
+ ): RuntimeSqlExecuteResult;
704
734
  actorSqlExecuteBatch(
705
735
  ctx: ActorContextHandle,
706
736
  statements: RuntimeSqlBatchStatement[],
@@ -710,21 +740,37 @@ export interface CoreRuntime {
710
740
  timeoutMs?: number,
711
741
  name?: string,
712
742
  ): Promise<SqliteTransactionHandle>;
743
+ actorSqlBeginTransactionSync(
744
+ ctx: ActorContextHandle,
745
+ timeoutMs?: number,
746
+ name?: string,
747
+ ): SqliteTransactionHandle;
713
748
  actorSqlTransactionExec(
714
749
  transaction: SqliteTransactionHandle,
715
750
  sql: string,
716
751
  ): Promise<RuntimeSqlExecResult>;
752
+ actorSqlTransactionExecSync(
753
+ transaction: SqliteTransactionHandle,
754
+ sql: string,
755
+ ): RuntimeSqlExecResult;
717
756
  actorSqlTransactionExecute(
718
757
  transaction: SqliteTransactionHandle,
719
758
  sql: string,
720
759
  params?: RuntimeSqlBindParams,
721
760
  ): Promise<RuntimeSqlExecuteResult>;
761
+ actorSqlTransactionExecuteSync(
762
+ transaction: SqliteTransactionHandle,
763
+ sql: string,
764
+ params?: RuntimeSqlBindParams,
765
+ ): RuntimeSqlExecuteResult;
722
766
  actorSqlTransactionCommit(
723
767
  transaction: SqliteTransactionHandle,
724
768
  ): Promise<void>;
769
+ actorSqlTransactionCommitSync(transaction: SqliteTransactionHandle): void;
725
770
  actorSqlTransactionRollback(
726
771
  transaction: SqliteTransactionHandle,
727
772
  ): Promise<void>;
773
+ actorSqlTransactionRollbackSync(transaction: SqliteTransactionHandle): void;
728
774
  actorBeginStateTransaction(
729
775
  ctx: ActorContextHandle,
730
776
  timeoutMs?: number,
@@ -753,10 +799,14 @@ export interface CoreRuntime {
753
799
  ): Promise<RuntimeSqlRunResult>;
754
800
  actorSqlMetrics(ctx: ActorContextHandle): SqliteNativeMetrics | null;
755
801
  actorSqlTakeLastKvError(ctx: ActorContextHandle): string | null;
802
+ /** Opens the actor database so later synchronous calls never fetch its first pages. */
803
+ actorSqlOpen(ctx: ActorContextHandle): Promise<void>;
804
+ /** Returns the ticket of the latest local commit. */
805
+ actorSqlCommitTicket(ctx: ActorContextHandle): number;
806
+ /** Resolves once the commit `ticket` names and every earlier commit are durable. */
807
+ actorSqlWhenDurable(ctx: ActorContextHandle, ticket: number): Promise<void>;
808
+ actorSqlAbortGeneration(ctx: ActorContextHandle, reason: string): void;
756
809
  actorSqlClose(ctx: ActorContextHandle): Promise<void>;
757
- actorRuntimeSocketProvision(
758
- ctx: ActorContextHandle,
759
- ): Promise<RuntimeActorRuntimeSocketEndpointInfo>;
760
810
 
761
811
  actorQueueSend(
762
812
  ctx: ActorContextHandle,