rivetkit 0.0.0-main.41efcce → 0.0.0-main.49693dc

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 (299) hide show
  1. package/dist/browser/client.d.ts +939 -104
  2. package/dist/browser/client.js +1867 -900
  3. package/dist/browser/client.js.map +1 -1
  4. package/dist/browser/inspector/client.d.ts +252 -129
  5. package/dist/browser/inspector/client.js +2079 -233
  6. package/dist/browser/inspector/client.js.map +1 -1
  7. package/dist/tsup/actor/errors.cjs +6 -2
  8. package/dist/tsup/actor/errors.cjs.map +1 -1
  9. package/dist/tsup/actor/errors.d.cts +1 -74
  10. package/dist/tsup/actor/errors.d.ts +1 -74
  11. package/dist/tsup/actor/errors.js +5 -1
  12. package/dist/tsup/agent-os/index.cjs +2408 -2029
  13. package/dist/tsup/agent-os/index.cjs.map +1 -1
  14. package/dist/tsup/agent-os/index.d.cts +932 -114
  15. package/dist/tsup/agent-os/index.d.ts +932 -114
  16. package/dist/tsup/agent-os/index.js +2412 -2033
  17. package/dist/tsup/agent-os/index.js.map +1 -1
  18. package/dist/tsup/{chunk-J27CEW6F.js → chunk-2OBVQZX6.js} +6 -6
  19. package/dist/tsup/chunk-2OBVQZX6.js.map +1 -0
  20. package/dist/tsup/chunk-3ZWHWMFJ.js +14 -0
  21. package/dist/tsup/chunk-3ZWHWMFJ.js.map +1 -0
  22. package/dist/tsup/{chunk-AK57AL7S.cjs → chunk-4KGXFC6M.cjs} +238 -22
  23. package/dist/tsup/chunk-4KGXFC6M.cjs.map +1 -0
  24. package/dist/tsup/{chunk-CVHUXINE.js → chunk-5IU3MI7X.js} +594 -72
  25. package/dist/tsup/chunk-5IU3MI7X.js.map +1 -0
  26. package/dist/tsup/{chunk-VD7AZIBR.js → chunk-6CXQMHZR.js} +1161 -361
  27. package/dist/tsup/chunk-6CXQMHZR.js.map +1 -0
  28. package/dist/tsup/{chunk-SBEFJ4IQ.js → chunk-6W2UIH3X.js} +70 -8
  29. package/dist/tsup/chunk-6W2UIH3X.js.map +1 -0
  30. package/dist/tsup/{chunk-7MRGNVSR.js → chunk-6W5VGLFT.js} +237 -21
  31. package/dist/tsup/chunk-6W5VGLFT.js.map +1 -0
  32. package/dist/tsup/{chunk-RF3MEJV6.cjs → chunk-B73TBMMG.cjs} +18 -11
  33. package/dist/tsup/chunk-B73TBMMG.cjs.map +1 -0
  34. package/dist/tsup/chunk-D6TORRA4.js +243 -0
  35. package/dist/tsup/chunk-D6TORRA4.js.map +1 -0
  36. package/dist/tsup/{chunk-PXLJOMJD.js → chunk-FXT3HN32.js} +2 -2
  37. package/dist/tsup/{chunk-ZGWSH7YV.js → chunk-IXBD7BXC.js} +48 -72
  38. package/dist/tsup/chunk-IXBD7BXC.js.map +1 -0
  39. package/dist/tsup/{chunk-3BFYSYRU.js → chunk-JI6GZ2C2.js} +4 -12
  40. package/dist/tsup/chunk-JI6GZ2C2.js.map +1 -0
  41. package/dist/tsup/{chunk-PCBNKI2J.js → chunk-JTHHCZCZ.js} +24 -1
  42. package/dist/tsup/chunk-JTHHCZCZ.js.map +1 -0
  43. package/dist/tsup/{chunk-2NDZ7JCR.cjs → chunk-KVFIONKQ.cjs} +25 -2
  44. package/dist/tsup/chunk-KVFIONKQ.cjs.map +1 -0
  45. package/dist/tsup/{chunk-SPMMF6TR.cjs → chunk-LGOWPZJ5.cjs} +3 -3
  46. package/dist/tsup/chunk-LGOWPZJ5.cjs.map +1 -0
  47. package/dist/tsup/chunk-LYNWTX6R.cjs +14 -0
  48. package/dist/tsup/chunk-LYNWTX6R.cjs.map +1 -0
  49. package/dist/tsup/{chunk-LLD2D5LO.cjs → chunk-MYWZCDML.cjs} +4 -12
  50. package/dist/tsup/chunk-MYWZCDML.cjs.map +1 -0
  51. package/dist/tsup/{chunk-XSC2WWXD.js → chunk-NYDZLWHZ.js} +16 -9
  52. package/dist/tsup/chunk-NYDZLWHZ.js.map +1 -0
  53. package/dist/tsup/{chunk-AV54CVAG.cjs → chunk-RB7WUDX6.cjs} +1290 -490
  54. package/dist/tsup/chunk-RB7WUDX6.cjs.map +1 -0
  55. package/dist/tsup/{chunk-JTL6Y2UN.cjs → chunk-SPJORZMM.cjs} +49 -73
  56. package/dist/tsup/chunk-SPJORZMM.cjs.map +1 -0
  57. package/dist/tsup/chunk-SSMKIYEX.cjs +243 -0
  58. package/dist/tsup/chunk-SSMKIYEX.cjs.map +1 -0
  59. package/dist/tsup/{chunk-TQSL4NJ3.cjs → chunk-TQ4HSX2U.cjs} +72 -10
  60. package/dist/tsup/chunk-TQ4HSX2U.cjs.map +1 -0
  61. package/dist/tsup/{chunk-EVADNK6Z.cjs → chunk-U6ZQELGJ.cjs} +10 -10
  62. package/dist/tsup/chunk-U6ZQELGJ.cjs.map +1 -0
  63. package/dist/tsup/chunk-XLBMKZD3.cjs +1257 -0
  64. package/dist/tsup/chunk-XLBMKZD3.cjs.map +1 -0
  65. package/dist/tsup/client/mod.cjs +8 -9
  66. package/dist/tsup/client/mod.cjs.map +1 -1
  67. package/dist/tsup/client/mod.d.cts +18 -7
  68. package/dist/tsup/client/mod.d.ts +18 -7
  69. package/dist/tsup/client/mod.js +7 -8
  70. package/dist/tsup/common/log.cjs +5 -3
  71. package/dist/tsup/common/log.cjs.map +1 -1
  72. package/dist/tsup/common/log.d.cts +5 -1
  73. package/dist/tsup/common/log.d.ts +5 -1
  74. package/dist/tsup/common/log.js +6 -4
  75. package/dist/tsup/common/websocket.cjs +4 -4
  76. package/dist/tsup/common/websocket.cjs.map +1 -1
  77. package/dist/tsup/common/websocket.js +3 -3
  78. package/dist/tsup/{config-B3RIidaJ.d.ts → config-BXSV17G7.d.cts} +987 -72
  79. package/dist/tsup/{config-D6WxBYjV.d.cts → config-Bi1r6gI7.d.ts} +987 -72
  80. package/dist/tsup/{config-B9qUIHX4.d.ts → config-ey2I2zz5.d.cts} +82 -15
  81. package/dist/tsup/{config-B9qUIHX4.d.cts → config-ey2I2zz5.d.ts} +82 -15
  82. package/dist/tsup/context-4fJemY0D.d.cts +129 -0
  83. package/dist/tsup/context-D4sNYboo.d.ts +129 -0
  84. package/dist/tsup/db/drizzle.cjs +168 -103
  85. package/dist/tsup/db/drizzle.cjs.map +1 -1
  86. package/dist/tsup/db/drizzle.d.cts +8 -4
  87. package/dist/tsup/db/drizzle.d.ts +8 -4
  88. package/dist/tsup/db/drizzle.js +170 -105
  89. package/dist/tsup/db/drizzle.js.map +1 -1
  90. package/dist/tsup/db/mod.cjs +6 -163
  91. package/dist/tsup/db/mod.cjs.map +1 -1
  92. package/dist/tsup/db/mod.d.cts +12 -4
  93. package/dist/tsup/db/mod.d.ts +12 -4
  94. package/dist/tsup/db/mod.js +5 -162
  95. package/dist/tsup/db/mod.js.map +1 -1
  96. package/dist/tsup/dynamic/mod.cjs +24 -0
  97. package/dist/tsup/dynamic/mod.cjs.map +1 -0
  98. package/dist/tsup/dynamic/mod.d.cts +38 -0
  99. package/dist/tsup/dynamic/mod.d.ts +38 -0
  100. package/dist/tsup/dynamic/mod.js +24 -0
  101. package/dist/tsup/dynamic/mod.js.map +1 -0
  102. package/dist/tsup/inspector/mod.cjs +47 -12
  103. package/dist/tsup/inspector/mod.cjs.map +1 -1
  104. package/dist/tsup/inspector/mod.d.cts +26 -136
  105. package/dist/tsup/inspector/mod.d.ts +26 -136
  106. package/dist/tsup/inspector/mod.js +46 -11
  107. package/dist/tsup/inspector/mod.js.map +1 -1
  108. package/dist/tsup/inspector/workflow.cjs +21 -0
  109. package/dist/tsup/inspector/workflow.cjs.map +1 -0
  110. package/dist/tsup/inspector/workflow.d.cts +144 -0
  111. package/dist/tsup/inspector/workflow.d.ts +144 -0
  112. package/dist/tsup/inspector/workflow.js +21 -0
  113. package/dist/tsup/inspector/workflow.js.map +1 -0
  114. package/dist/tsup/inspector-tab/mod.cjs +162 -0
  115. package/dist/tsup/inspector-tab/mod.cjs.map +1 -0
  116. package/dist/tsup/inspector-tab/mod.d.cts +241 -0
  117. package/dist/tsup/inspector-tab/mod.d.ts +241 -0
  118. package/dist/tsup/inspector-tab/mod.js +162 -0
  119. package/dist/tsup/inspector-tab/mod.js.map +1 -0
  120. package/dist/tsup/mod.cjs +4268 -944
  121. package/dist/tsup/mod.cjs.map +1 -1
  122. package/dist/tsup/mod.d.cts +9 -8
  123. package/dist/tsup/mod.d.ts +9 -8
  124. package/dist/tsup/mod.js +4229 -905
  125. package/dist/tsup/mod.js.map +1 -1
  126. package/dist/tsup/test/mod.cjs +26 -21
  127. package/dist/tsup/test/mod.cjs.map +1 -1
  128. package/dist/tsup/test/mod.d.cts +7 -6
  129. package/dist/tsup/test/mod.d.ts +7 -6
  130. package/dist/tsup/test/mod.js +25 -20
  131. package/dist/tsup/test/mod.js.map +1 -1
  132. package/dist/tsup/unstable/migrations.cjs +101 -0
  133. package/dist/tsup/unstable/migrations.cjs.map +1 -0
  134. package/dist/tsup/unstable/migrations.d.cts +29 -0
  135. package/dist/tsup/unstable/migrations.d.ts +29 -0
  136. package/dist/tsup/unstable/migrations.js +101 -0
  137. package/dist/tsup/unstable/migrations.js.map +1 -0
  138. package/dist/tsup/utils-Bbz-3411.d.cts +162 -0
  139. package/dist/tsup/utils-BqdWVlzm.d.ts +162 -0
  140. package/dist/tsup/utils-vvJQyygr.d.cts +109 -0
  141. package/dist/tsup/utils-vvJQyygr.d.ts +109 -0
  142. package/dist/tsup/utils.cjs +7 -3
  143. package/dist/tsup/utils.cjs.map +1 -1
  144. package/dist/tsup/utils.d.cts +4 -147
  145. package/dist/tsup/utils.d.ts +4 -147
  146. package/dist/tsup/utils.js +6 -2
  147. package/dist/tsup/workflow/mod.cjs +517 -412
  148. package/dist/tsup/workflow/mod.cjs.map +1 -1
  149. package/dist/tsup/workflow/mod.d.cts +11 -10
  150. package/dist/tsup/workflow/mod.d.ts +11 -10
  151. package/dist/tsup/workflow/mod.js +511 -406
  152. package/dist/tsup/workflow/mod.js.map +1 -1
  153. package/package.json +62 -14
  154. package/src/actor/actions.ts +119 -0
  155. package/src/actor/config.test.ts +78 -0
  156. package/src/actor/config.ts +518 -96
  157. package/src/actor/contexts/index.ts +7 -2
  158. package/src/actor/definition.ts +38 -26
  159. package/src/actor/errors.ts +101 -89
  160. package/src/actor/keys.ts +2 -1
  161. package/src/actor/mod.ts +32 -21
  162. package/src/actor/schema.ts +2 -2
  163. package/src/agent-os/actor/cron.ts +37 -2
  164. package/src/agent-os/actor/index.ts +39 -19
  165. package/src/agent-os/actor/preview.ts +1 -2
  166. package/src/agent-os/actor/session.ts +2 -2
  167. package/src/agent-os/config.ts +1 -1
  168. package/src/agent-os/fs/database-vfs.ts +1 -1
  169. package/src/agent-os/index.ts +16 -15
  170. package/src/agent-os/types.ts +11 -0
  171. package/src/client/actor-common.ts +106 -53
  172. package/src/client/actor-conn.ts +94 -45
  173. package/src/client/actor-handle.ts +198 -79
  174. package/src/client/actor-query.ts +13 -6
  175. package/src/client/client.ts +43 -15
  176. package/src/client/config.ts +8 -0
  177. package/src/client/errors.ts +4 -2
  178. package/src/client/lifecycle-errors.ts +28 -4
  179. package/src/client/mod.ts +9 -4
  180. package/src/client/query.ts +3 -1
  181. package/src/client/queue.ts +8 -3
  182. package/src/client/raw-utils.ts +24 -16
  183. package/src/client/resolve-gateway-target.ts +3 -1
  184. package/src/client/utils.ts +36 -10
  185. package/src/common/actor-router-consts.ts +7 -0
  186. package/src/common/actor-websocket.ts +3 -1
  187. package/src/common/bare/actor-persist/v1.ts +205 -163
  188. package/src/common/bare/actor-persist/v2.ts +265 -213
  189. package/src/common/bare/actor-persist/v3.ts +176 -172
  190. package/src/common/bare/actor-persist/v4.ts +254 -253
  191. package/src/common/bare/generated/actor-runtime-socket-protocol/v1.ts +848 -0
  192. package/src/common/bare/generated/client-protocol/v4.ts +599 -0
  193. package/src/common/bare/generated/inspector/v5.ts +1016 -0
  194. package/src/common/bare/generated/inspector/v6.ts +1367 -0
  195. package/src/common/bare/transport/v1.ts +694 -542
  196. package/src/common/client-protocol-versioned.ts +176 -67
  197. package/src/common/client-protocol-zod.ts +7 -0
  198. package/src/common/client-protocol.ts +1 -1
  199. package/src/common/database/config.ts +103 -10
  200. package/src/common/database/mod.test.ts +364 -0
  201. package/src/common/database/mod.ts +278 -149
  202. package/src/common/database/native-database.test.ts +220 -0
  203. package/src/common/database/native-database.ts +394 -58
  204. package/src/common/database/shared.ts +25 -0
  205. package/src/common/encoding.ts +302 -38
  206. package/src/common/engine.ts +28 -1
  207. package/src/common/eventsource.ts +1 -1
  208. package/src/common/fetch-like.ts +49 -0
  209. package/src/common/inline-websocket-adapter.ts +14 -13
  210. package/src/common/inspector-transport.ts +12 -2
  211. package/src/common/log.ts +117 -14
  212. package/src/common/router.ts +49 -23
  213. package/src/common/utils.ts +23 -203
  214. package/src/common/websocket-interface.ts +1 -1
  215. package/src/db/drizzle.test.ts +164 -0
  216. package/src/db/drizzle.ts +207 -121
  217. package/src/db/mod.ts +6 -1
  218. package/src/devtools-loader/index.ts +4 -7
  219. package/src/devtools-loader/serve-devtools.ts +26 -0
  220. package/src/dynamic/instance.ts +32 -0
  221. package/src/dynamic/internal.ts +50 -0
  222. package/src/dynamic/isolate-runtime.ts +64 -0
  223. package/src/dynamic/mod.ts +32 -0
  224. package/src/engine-client/actor-http-client.ts +23 -4
  225. package/src/engine-client/actor-websocket-client.ts +32 -5
  226. package/src/engine-client/api-endpoints.ts +52 -2
  227. package/src/engine-client/api-utils.ts +2 -2
  228. package/src/engine-client/driver.ts +21 -17
  229. package/src/engine-client/mod.ts +116 -64
  230. package/src/engine-client/ws-proxy.ts +9 -4
  231. package/src/globals.d.ts +4 -0
  232. package/src/inspector/client.browser.ts +127 -15
  233. package/src/inspector/mod.ts +2 -4
  234. package/src/inspector/workflow.ts +61 -0
  235. package/src/inspector-tab/mod.ts +301 -0
  236. package/src/registry/config/envoy.ts +1 -2
  237. package/src/registry/config/index.ts +160 -53
  238. package/src/registry/config/serverless.ts +7 -1
  239. package/src/registry/index.ts +659 -96
  240. package/src/registry/napi-runtime.ts +1154 -0
  241. package/src/registry/native-validation.ts +10 -12
  242. package/src/registry/native.ts +2005 -955
  243. package/src/registry/process-metrics.ts +250 -0
  244. package/src/registry/run-handler-coordinator.test.ts +252 -0
  245. package/src/registry/run-handler-coordinator.ts +219 -0
  246. package/src/registry/runtime.test.ts +50 -0
  247. package/src/registry/runtime.ts +839 -0
  248. package/src/registry/wasm-runtime.ts +1283 -0
  249. package/src/registry/write-through-proxy.ts +141 -0
  250. package/src/serde.ts +8 -2
  251. package/src/serverless/configure.ts +102 -49
  252. package/src/test/mod.ts +2 -17
  253. package/src/test/ready.ts +22 -0
  254. package/src/unstable/migrations.test.ts +170 -0
  255. package/src/unstable/migrations.ts +141 -0
  256. package/src/utils/crypto.ts +14 -0
  257. package/src/utils/endpoint-parser.ts +1 -1
  258. package/src/utils/env-vars.ts +39 -0
  259. package/src/utils/node.ts +44 -28
  260. package/src/utils/router.ts +1 -1
  261. package/src/utils.ts +4 -2
  262. package/src/workflow/context.ts +683 -242
  263. package/src/workflow/driver.test.ts +62 -0
  264. package/src/workflow/driver.ts +246 -119
  265. package/src/workflow/inspector.ts +24 -24
  266. package/src/workflow/mod.ts +76 -84
  267. package/dist/tsup/chunk-2NDZ7JCR.cjs.map +0 -1
  268. package/dist/tsup/chunk-3BFYSYRU.js.map +0 -1
  269. package/dist/tsup/chunk-6AVUJ52Q.cjs +0 -253
  270. package/dist/tsup/chunk-6AVUJ52Q.cjs.map +0 -1
  271. package/dist/tsup/chunk-7MRGNVSR.js.map +0 -1
  272. package/dist/tsup/chunk-7X7RVTFF.cjs +0 -735
  273. package/dist/tsup/chunk-7X7RVTFF.cjs.map +0 -1
  274. package/dist/tsup/chunk-AK57AL7S.cjs.map +0 -1
  275. package/dist/tsup/chunk-AV54CVAG.cjs.map +0 -1
  276. package/dist/tsup/chunk-CVHUXINE.js.map +0 -1
  277. package/dist/tsup/chunk-EVADNK6Z.cjs.map +0 -1
  278. package/dist/tsup/chunk-J27CEW6F.js.map +0 -1
  279. package/dist/tsup/chunk-JTL6Y2UN.cjs.map +0 -1
  280. package/dist/tsup/chunk-LLD2D5LO.cjs.map +0 -1
  281. package/dist/tsup/chunk-PCBNKI2J.js.map +0 -1
  282. package/dist/tsup/chunk-RF3MEJV6.cjs.map +0 -1
  283. package/dist/tsup/chunk-SB4MXRN2.js +0 -253
  284. package/dist/tsup/chunk-SB4MXRN2.js.map +0 -1
  285. package/dist/tsup/chunk-SBEFJ4IQ.js.map +0 -1
  286. package/dist/tsup/chunk-SPMMF6TR.cjs.map +0 -1
  287. package/dist/tsup/chunk-TQSL4NJ3.cjs.map +0 -1
  288. package/dist/tsup/chunk-VD7AZIBR.js.map +0 -1
  289. package/dist/tsup/chunk-XSC2WWXD.js.map +0 -1
  290. package/dist/tsup/chunk-ZGWSH7YV.js.map +0 -1
  291. package/dist/tsup/context-BxvHh9wi.d.cts +0 -92
  292. package/dist/tsup/context-CNIZBIKl.d.ts +0 -92
  293. package/dist/tsup/utils-fwx3o3K9.d.cts +0 -18
  294. package/dist/tsup/utils-fwx3o3K9.d.ts +0 -18
  295. package/src/actor/driver.ts +0 -141
  296. package/src/actor/instance/mod.ts +0 -2501
  297. package/src/drivers/engine/actor-driver.ts +0 -2470
  298. package/src/utils/serve.ts +0 -217
  299. /package/dist/tsup/{chunk-PXLJOMJD.js.map → chunk-FXT3HN32.js.map} +0 -0
@@ -0,0 +1,62 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import { ActorWorkflowDriver, chunkWorkflowWrites } from "./driver";
3
+
4
+ function write(keyBytes: number, valueBytes: number) {
5
+ return {
6
+ key: new Uint8Array(keyBytes),
7
+ value: new Uint8Array(valueBytes),
8
+ };
9
+ }
10
+
11
+ describe("workflow sqlite batching", () => {
12
+ test.each([
13
+ [127, 1],
14
+ [128, 1],
15
+ [129, 2],
16
+ ])("chunks %i rows into %i transaction(s)", (rowCount, chunkCount) => {
17
+ const chunks = chunkWorkflowWrites(
18
+ Array.from({ length: rowCount }, () => write(1, 1)),
19
+ );
20
+ expect(chunks).toHaveLength(chunkCount);
21
+ expect(chunks.flat()).toHaveLength(rowCount);
22
+ expect(chunks.every((chunk) => chunk.length <= 128)).toBe(true);
23
+ });
24
+
25
+ test("splits immediately above the 512 KiB payload budget", () => {
26
+ // Each persisted key also includes the two-byte workflow namespace.
27
+ const exact = [write(1, 256 * 1024 - 3), write(1, 256 * 1024 - 3)];
28
+ expect(chunkWorkflowWrites(exact)).toHaveLength(1);
29
+
30
+ const over = [write(1, 256 * 1024 - 3), write(1, 256 * 1024 - 2)];
31
+ expect(chunkWorkflowWrites(over)).toHaveLength(2);
32
+ });
33
+
34
+ test("delegates actor state and the whole workflow batch to one atomic runtime call", async () => {
35
+ let receivedWrites: Array<{ key: Uint8Array; value: Uint8Array }> = [];
36
+ const sql = {
37
+ query: async () => ({ columns: [], rows: [] }),
38
+ execute: async () => [],
39
+ run: async () => ({ changes: 0 }),
40
+ executeBatch: async () => [],
41
+ };
42
+ const actor = {
43
+ stateManager: {
44
+ saveStateAndWorkflowBatch: async (
45
+ writes: Array<{ key: Uint8Array; value: Uint8Array }>,
46
+ ) => {
47
+ receivedWrites = writes;
48
+ },
49
+ },
50
+ queueManager: {},
51
+ };
52
+ const runCtx = {
53
+ sql,
54
+ internalKeepAwake: async <T>(promise: Promise<T>) => await promise,
55
+ };
56
+ const driver = new ActorWorkflowDriver(actor as never, runCtx as never);
57
+ const writes = [write(1, 1), write(2, 2)];
58
+ await driver.batch(writes);
59
+ expect(driver.atomicBatch).toBe(true);
60
+ expect(receivedWrites).toBe(writes);
61
+ });
62
+ });
@@ -1,10 +1,3 @@
1
- // @ts-nocheck
2
- import type { RunContext } from "@/actor/config";
3
- import type {
4
- AnyActorInstance,
5
- AnyStaticActorInstance,
6
- } from "@/actor/definition";
7
- import { makeWorkflowKey, workflowStoragePrefix } from "@/actor/keys";
8
1
  import type {
9
2
  EngineDriver,
10
3
  KVEntry,
@@ -12,8 +5,30 @@ import type {
12
5
  Message,
13
6
  WorkflowMessageDriver,
14
7
  } from "@rivetkit/workflow-engine";
8
+ import type { RunContext } from "@/actor/config";
9
+ import type { AnyStaticActorInstance } from "@/actor/definition";
10
+ import { makeWorkflowKey, workflowStoragePrefix } from "@/actor/keys";
11
+ import type { RawAccess } from "@/common/database/config";
15
12
 
16
13
  const WORKFLOW_STORAGE_PREFIX = workflowStoragePrefix();
14
+ // Keep workflow flushes below depot's 320-dirty-page commit ceiling. The
15
+ // former actor-KV transport allowed 976 KiB batches; SQLite also dirties table
16
+ // and index pages, so it needs the same conservative budget as core imports.
17
+ const WORKFLOW_SQLITE_MAX_BATCH_ROWS = 128;
18
+ const WORKFLOW_SQLITE_MAX_BATCH_BYTES = 512 * 1024;
19
+
20
+ // Mirrors the element shape returned by `queueManager.receive`. The actor
21
+ // instance is reached through a loose type here, so the call's result is
22
+ // untyped and the message must be annotated explicitly.
23
+ interface ReceivedQueueMessage {
24
+ id: bigint;
25
+ name: string;
26
+ body: unknown;
27
+ createdAt: number;
28
+ complete?: (response?: unknown) => Promise<void>;
29
+ }
30
+
31
+ type KVEntryTuple = [Uint8Array, Uint8Array];
17
32
 
18
33
  function stripWorkflowKey(prefixed: Uint8Array): Uint8Array {
19
34
  return prefixed.slice(WORKFLOW_STORAGE_PREFIX.length);
@@ -30,6 +45,204 @@ function computeUpperBound(prefix: Uint8Array): Uint8Array | null {
30
45
  return null;
31
46
  }
32
47
 
48
+ function normalizeSqlBlob(value: unknown): Uint8Array {
49
+ if (value instanceof Uint8Array) {
50
+ return value;
51
+ }
52
+ if (value instanceof ArrayBuffer) {
53
+ return new Uint8Array(value);
54
+ }
55
+ if (ArrayBuffer.isView(value)) {
56
+ return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
57
+ }
58
+ if (Array.isArray(value)) {
59
+ const bytes = new Uint8Array(value.length);
60
+ for (const [i, item] of value.entries()) {
61
+ if (!Number.isInteger(item) || item < 0 || item > 255) {
62
+ throw new Error("workflow sqlite value was not a byte array");
63
+ }
64
+ bytes[i] = item;
65
+ }
66
+ return bytes;
67
+ }
68
+ throw new Error("workflow sqlite value was not a blob");
69
+ }
70
+
71
+ function runtimeDbFromContext(
72
+ runCtx?: RunContext<any, any, any, any, any, any, any, any>,
73
+ ): RawAccess | undefined {
74
+ const db = (runCtx as unknown as { db?: unknown } | undefined)?.db;
75
+ if (
76
+ db &&
77
+ typeof db === "object" &&
78
+ "execute" in db &&
79
+ "transaction" in db
80
+ ) {
81
+ return db as RawAccess;
82
+ }
83
+ return undefined;
84
+ }
85
+
86
+ class WorkflowStorage {
87
+ #db: RawAccess;
88
+
89
+ constructor(db?: RawAccess) {
90
+ if (!db) {
91
+ throw new Error(
92
+ "workflow storage requires embedded SQLite; actor KV workflow storage is no longer supported",
93
+ );
94
+ }
95
+ this.#db = db;
96
+ }
97
+
98
+ async get(key: Uint8Array): Promise<Uint8Array | null> {
99
+ const prefixed = makeWorkflowKey(key);
100
+ const rows = await this.#db.execute<{ value: unknown }>(
101
+ "SELECT value FROM _rivet_wf_kv WHERE key = ?",
102
+ prefixed,
103
+ );
104
+ return rows[0]?.value == null ? null : normalizeSqlBlob(rows[0].value);
105
+ }
106
+
107
+ async set(key: Uint8Array, value: Uint8Array): Promise<void> {
108
+ await this.batch([{ key, value }]);
109
+ }
110
+
111
+ async delete(key: Uint8Array): Promise<void> {
112
+ const prefixed = makeWorkflowKey(key);
113
+ await this.#db.execute(
114
+ "DELETE FROM _rivet_wf_kv WHERE key = ?",
115
+ prefixed,
116
+ );
117
+ }
118
+
119
+ async deletePrefix(prefix: Uint8Array): Promise<void> {
120
+ const start = makeWorkflowKey(prefix);
121
+ const end = computeUpperBound(start);
122
+ if (end) {
123
+ await this.#db.execute(
124
+ "DELETE FROM _rivet_wf_kv WHERE key >= ? AND key < ?",
125
+ start,
126
+ end,
127
+ );
128
+ return;
129
+ }
130
+ const entries = await this.listRaw(start);
131
+ await this.deleteRawKeys(
132
+ entries
133
+ .filter(([key]) => keyStartsWith(key, start))
134
+ .map(([key]) => key),
135
+ );
136
+ }
137
+
138
+ async deleteRange(start: Uint8Array, end: Uint8Array): Promise<void> {
139
+ const prefixedStart = makeWorkflowKey(start);
140
+ const prefixedEnd = makeWorkflowKey(end);
141
+ await this.#db.execute(
142
+ "DELETE FROM _rivet_wf_kv WHERE key >= ? AND key < ?",
143
+ prefixedStart,
144
+ prefixedEnd,
145
+ );
146
+ }
147
+
148
+ async list(prefix: Uint8Array): Promise<KVEntry[]> {
149
+ const prefixed = makeWorkflowKey(prefix);
150
+ const entries = await this.listRaw(prefixed);
151
+ return entries
152
+ .filter(([key]) => keyStartsWith(key, prefixed))
153
+ .map(([key, value]: KVEntryTuple) => ({
154
+ key: stripWorkflowKey(key),
155
+ value,
156
+ }));
157
+ }
158
+
159
+ async batch(writes: KVWrite[]): Promise<void> {
160
+ if (writes.length === 0) return;
161
+ for (const chunk of chunkWorkflowWrites(writes)) {
162
+ await this.#db.transaction(async (tx) => {
163
+ for (const { key, value } of chunk) {
164
+ await tx.execute(
165
+ "INSERT INTO _rivet_wf_kv (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value",
166
+ makeWorkflowKey(key),
167
+ value,
168
+ );
169
+ }
170
+ });
171
+ }
172
+ }
173
+
174
+ async listRaw(prefix: Uint8Array): Promise<KVEntryTuple[]> {
175
+ const end = computeUpperBound(prefix);
176
+ const rows = end
177
+ ? await this.#db.execute<{ key: unknown; value: unknown }>(
178
+ "SELECT key, value FROM _rivet_wf_kv WHERE key >= ? AND key < ? ORDER BY key ASC",
179
+ prefix,
180
+ end,
181
+ )
182
+ : await this.#db.execute<{ key: unknown; value: unknown }>(
183
+ "SELECT key, value FROM _rivet_wf_kv WHERE key >= ? ORDER BY key ASC",
184
+ prefix,
185
+ );
186
+ return rows.map((row) => [
187
+ normalizeSqlBlob(row.key),
188
+ normalizeSqlBlob(row.value),
189
+ ]);
190
+ }
191
+
192
+ async deleteRawKeys(keys: Uint8Array[]): Promise<void> {
193
+ for (
194
+ let start = 0;
195
+ start < keys.length;
196
+ start += WORKFLOW_SQLITE_MAX_BATCH_ROWS
197
+ ) {
198
+ await this.#db.transaction(async (tx) => {
199
+ for (const key of keys.slice(
200
+ start,
201
+ start + WORKFLOW_SQLITE_MAX_BATCH_ROWS,
202
+ )) {
203
+ await tx.execute(
204
+ "DELETE FROM _rivet_wf_kv WHERE key = ?",
205
+ key,
206
+ );
207
+ }
208
+ });
209
+ }
210
+ }
211
+ }
212
+
213
+ export function chunkWorkflowWrites(writes: KVWrite[]): KVWrite[][] {
214
+ const chunks: KVWrite[][] = [];
215
+ let chunk: KVWrite[] = [];
216
+ let chunkBytes = 0;
217
+ for (const write of writes) {
218
+ const writeBytes =
219
+ WORKFLOW_STORAGE_PREFIX.length +
220
+ write.key.length +
221
+ write.value.length;
222
+ if (
223
+ chunk.length > 0 &&
224
+ (chunk.length >= WORKFLOW_SQLITE_MAX_BATCH_ROWS ||
225
+ chunkBytes + writeBytes > WORKFLOW_SQLITE_MAX_BATCH_BYTES)
226
+ ) {
227
+ chunks.push(chunk);
228
+ chunk = [];
229
+ chunkBytes = 0;
230
+ }
231
+ chunk.push(write);
232
+ chunkBytes += writeBytes;
233
+ }
234
+ if (chunk.length > 0) chunks.push(chunk);
235
+ return chunks;
236
+ }
237
+
238
+ function keyStartsWith(key: Uint8Array, prefix: Uint8Array): boolean {
239
+ if (key.length < prefix.length) return false;
240
+ for (let i = 0; i < prefix.length; i++) {
241
+ if (key[i] !== prefix[i]) return false;
242
+ }
243
+ return true;
244
+ }
245
+
33
246
  class ActorWorkflowMessageDriver implements WorkflowMessageDriver {
34
247
  #actor: AnyStaticActorInstance;
35
248
  #runCtx: RunContext<any, any, any, any, any, any, any, any>;
@@ -64,7 +277,7 @@ class ActorWorkflowMessageDriver implements WorkflowMessageDriver {
64
277
  opts.completable,
65
278
  ),
66
279
  );
67
- return messages.map((message) => ({
280
+ return messages.map((message: ReceivedQueueMessage) => ({
68
281
  id: message.id.toString(),
69
282
  name: message.name,
70
283
  data: message.body,
@@ -102,10 +315,12 @@ class ActorWorkflowMessageDriver implements WorkflowMessageDriver {
102
315
  }
103
316
 
104
317
  export class ActorWorkflowDriver implements EngineDriver {
318
+ readonly atomicBatch = true;
105
319
  readonly workerPollInterval = 100;
106
320
  readonly messageDriver: WorkflowMessageDriver;
107
321
  #actor: AnyStaticActorInstance;
108
322
  #runCtx: RunContext<any, any, any, any, any, any, any, any>;
323
+ #storage: WorkflowStorage;
109
324
 
110
325
  constructor(
111
326
  actor: AnyStaticActorInstance,
@@ -114,97 +329,42 @@ export class ActorWorkflowDriver implements EngineDriver {
114
329
  this.#actor = actor;
115
330
  this.#runCtx = runCtx;
116
331
  this.messageDriver = new ActorWorkflowMessageDriver(actor, runCtx);
332
+ this.#storage = new WorkflowStorage(runtimeDbFromContext(runCtx));
117
333
  }
118
334
 
119
335
  async get(key: Uint8Array): Promise<Uint8Array | null> {
120
- const [value] = await this.#runCtx.internalKeepAwake(
121
- this.#actor.driver.kvBatchGet(this.#actor.id, [
122
- makeWorkflowKey(key),
123
- ]),
124
- );
125
- return value ?? null;
336
+ return await this.#runCtx.internalKeepAwake(this.#storage.get(key));
126
337
  }
127
338
 
128
339
  async set(key: Uint8Array, value: Uint8Array): Promise<void> {
129
- await this.#runCtx.internalKeepAwake(
130
- this.#actor.driver.kvBatchPut(this.#actor.id, [
131
- [makeWorkflowKey(key), value],
132
- ]),
133
- );
340
+ await this.#runCtx.internalKeepAwake(this.#storage.set(key, value));
134
341
  }
135
342
 
136
343
  async delete(key: Uint8Array): Promise<void> {
137
- await this.#runCtx.internalKeepAwake(
138
- this.#actor.driver.kvBatchDelete(this.#actor.id, [
139
- makeWorkflowKey(key),
140
- ]),
141
- );
344
+ await this.#runCtx.internalKeepAwake(this.#storage.delete(key));
142
345
  }
143
346
 
144
347
  async deletePrefix(prefix: Uint8Array): Promise<void> {
145
- const start = makeWorkflowKey(prefix);
146
- const end = computeUpperBound(start);
147
- if (end) {
148
- await this.#runCtx.internalKeepAwake(
149
- this.#actor.driver.kvDeleteRange(this.#actor.id, start, end),
150
- );
151
- } else {
152
- const entries = await this.#runCtx.internalKeepAwake(
153
- this.#actor.driver.kvListPrefix(this.#actor.id, start),
154
- );
155
- if (entries.length === 0) {
156
- return;
157
- }
158
- await this.#runCtx.internalKeepAwake(
159
- this.#actor.driver.kvBatchDelete(
160
- this.#actor.id,
161
- entries.map(([key]) => key),
162
- ),
163
- );
164
- }
348
+ await this.#runCtx.internalKeepAwake(
349
+ this.#storage.deletePrefix(prefix),
350
+ );
165
351
  }
166
352
 
167
353
  async deleteRange(start: Uint8Array, end: Uint8Array): Promise<void> {
168
354
  await this.#runCtx.internalKeepAwake(
169
- this.#actor.driver.kvDeleteRange(
170
- this.#actor.id,
171
- makeWorkflowKey(start),
172
- makeWorkflowKey(end),
173
- ),
355
+ this.#storage.deleteRange(start, end),
174
356
  );
175
357
  }
176
358
 
177
359
  async list(prefix: Uint8Array): Promise<KVEntry[]> {
178
- const entries = await this.#runCtx.internalKeepAwake(
179
- this.#actor.driver.kvListPrefix(
180
- this.#actor.id,
181
- makeWorkflowKey(prefix),
182
- ),
183
- );
184
- return entries.map(([key, value]) => ({
185
- key: stripWorkflowKey(key),
186
- value,
187
- }));
360
+ return await this.#runCtx.internalKeepAwake(this.#storage.list(prefix));
188
361
  }
189
362
 
190
363
  async batch(writes: KVWrite[]): Promise<void> {
191
364
  if (writes.length === 0) return;
192
365
 
193
- // Flush actor state together with workflow state to ensure atomicity.
194
- // If the server crashes after workflow flush, actor state must also be persisted.
195
366
  await this.#runCtx.internalKeepAwake(
196
- Promise.all([
197
- this.#actor.driver.kvBatchPut(
198
- this.#actor.id,
199
- writes.map(({ key, value }) => [
200
- makeWorkflowKey(key),
201
- value,
202
- ]),
203
- ),
204
- this.#actor.stateManager.saveState({
205
- immediate: true,
206
- }),
207
- ]),
367
+ this.#actor.stateManager.saveStateAndWorkflowBatch(writes),
208
368
  );
209
369
  }
210
370
 
@@ -256,68 +416,38 @@ export class ActorWorkflowControlDriver implements EngineDriver {
256
416
  readonly messageDriver: WorkflowMessageDriver =
257
417
  new NoopWorkflowMessageDriver();
258
418
  #actor: AnyStaticActorInstance;
419
+ #storage: WorkflowStorage;
259
420
 
260
- constructor(actor: AnyStaticActorInstance) {
421
+ constructor(
422
+ actor: AnyStaticActorInstance,
423
+ runCtx?: RunContext<any, any, any, any, any, any, any, any>,
424
+ ) {
261
425
  this.#actor = actor;
426
+ this.#storage = new WorkflowStorage(runtimeDbFromContext(runCtx));
262
427
  }
263
428
 
264
429
  async get(key: Uint8Array): Promise<Uint8Array | null> {
265
- const [value] = await this.#actor.driver.kvBatchGet(this.#actor.id, [
266
- makeWorkflowKey(key),
267
- ]);
268
- return value ?? null;
430
+ return await this.#storage.get(key);
269
431
  }
270
432
 
271
433
  async set(key: Uint8Array, value: Uint8Array): Promise<void> {
272
- await this.#actor.driver.kvBatchPut(this.#actor.id, [
273
- [makeWorkflowKey(key), value],
274
- ]);
434
+ await this.#storage.set(key, value);
275
435
  }
276
436
 
277
437
  async delete(key: Uint8Array): Promise<void> {
278
- await this.#actor.driver.kvBatchDelete(this.#actor.id, [
279
- makeWorkflowKey(key),
280
- ]);
438
+ await this.#storage.delete(key);
281
439
  }
282
440
 
283
441
  async deletePrefix(prefix: Uint8Array): Promise<void> {
284
- const start = makeWorkflowKey(prefix);
285
- const end = computeUpperBound(start);
286
- if (end) {
287
- await this.#actor.driver.kvDeleteRange(this.#actor.id, start, end);
288
- return;
289
- }
290
-
291
- const entries = await this.#actor.driver.kvListPrefix(
292
- this.#actor.id,
293
- start,
294
- );
295
- if (entries.length === 0) {
296
- return;
297
- }
298
- await this.#actor.driver.kvBatchDelete(
299
- this.#actor.id,
300
- entries.map(([key]) => key),
301
- );
442
+ await this.#storage.deletePrefix(prefix);
302
443
  }
303
444
 
304
445
  async deleteRange(start: Uint8Array, end: Uint8Array): Promise<void> {
305
- await this.#actor.driver.kvDeleteRange(
306
- this.#actor.id,
307
- makeWorkflowKey(start),
308
- makeWorkflowKey(end),
309
- );
446
+ await this.#storage.deleteRange(start, end);
310
447
  }
311
448
 
312
449
  async list(prefix: Uint8Array): Promise<KVEntry[]> {
313
- const entries = await this.#actor.driver.kvListPrefix(
314
- this.#actor.id,
315
- makeWorkflowKey(prefix),
316
- );
317
- return entries.map(([key, value]) => ({
318
- key: stripWorkflowKey(key),
319
- value,
320
- }));
450
+ return await this.#storage.list(prefix);
321
451
  }
322
452
 
323
453
  async batch(writes: KVWrite[]): Promise<void> {
@@ -325,10 +455,7 @@ export class ActorWorkflowControlDriver implements EngineDriver {
325
455
  return;
326
456
  }
327
457
 
328
- await this.#actor.driver.kvBatchPut(
329
- this.#actor.id,
330
- writes.map(({ key, value }) => [makeWorkflowKey(key), value]),
331
- );
458
+ await this.#storage.batch(writes);
332
459
  }
333
460
 
334
461
  async setAlarm(_workflowId: string, wakeAt: number): Promise<void> {
@@ -5,18 +5,21 @@ import type {
5
5
  EntryStatus,
6
6
  Location,
7
7
  SleepState,
8
+ WorkflowEntryMetadataSnapshot,
8
9
  WorkflowHistoryEntry,
9
10
  WorkflowHistorySnapshot,
10
- WorkflowEntryMetadataSnapshot,
11
11
  WorkflowState,
12
12
  } from "@rivetkit/workflow-engine";
13
- import { encodeWorkflowHistoryTransport } from "@/common/inspector-transport";
14
- import type * as inspectorSchema from "@/common/bare/generated/inspector/v4";
15
- import * as transport from "@/common/bare/transport/v1";
16
- import { encodeCborCompat } from "@/serde";
17
- import { assertUnreachable, bufferToArrayBuffer } from "@/utils";
13
+ import * as transport from "@/inspector/workflow";
14
+ import {
15
+ encodeWorkflowHistoryTransport,
16
+ encodeWorkflowInspectorValue,
17
+ type WorkflowInspectorAdapter as PublicWorkflowInspectorAdapter,
18
+ type WorkflowHistoryBytes,
19
+ } from "@/inspector/workflow";
20
+ import { assertUnreachable } from "@/utils";
18
21
 
19
- type HistoryListener = (history: inspectorSchema.WorkflowHistory) => void;
22
+ type HistoryListener = (history: WorkflowHistoryBytes) => void;
20
23
 
21
24
  function createHistoryEmitter() {
22
25
  const listeners = new Set<HistoryListener>();
@@ -26,7 +29,7 @@ function createHistoryEmitter() {
26
29
  listeners.add(listener);
27
30
  return () => listeners.delete(listener);
28
31
  },
29
- emit: (history: inspectorSchema.WorkflowHistory) => {
32
+ emit: (history: WorkflowHistoryBytes) => {
30
33
  for (const listener of listeners) {
31
34
  listener(history);
32
35
  }
@@ -34,33 +37,22 @@ function createHistoryEmitter() {
34
37
  };
35
38
  }
36
39
 
37
- export interface WorkflowInspectorAdapter {
38
- getHistory: () => inspectorSchema.WorkflowHistory | null;
39
- getState: () => Promise<WorkflowState | null>;
40
- onHistoryUpdated: (
41
- listener: (history: inspectorSchema.WorkflowHistory) => void,
42
- ) => () => void;
43
- replayFromStep: (
44
- entryId?: string,
45
- ) => Promise<inspectorSchema.WorkflowHistory | null>;
46
- }
40
+ export type WorkflowInspectorAdapter = PublicWorkflowInspectorAdapter;
47
41
 
48
42
  export function createWorkflowInspectorAdapter(): {
49
43
  adapter: WorkflowInspectorAdapter;
50
44
  update: (snapshot: WorkflowHistorySnapshot) => void;
51
45
  setGetState: (fn: () => Promise<WorkflowState | null>) => void;
52
46
  setReplayFromStep: (
53
- fn: (
54
- entryId?: string,
55
- ) => Promise<inspectorSchema.WorkflowHistory | null>,
47
+ fn: (entryId?: string) => Promise<WorkflowHistoryBytes | null>,
56
48
  ) => void;
57
49
  } {
58
50
  const emitter = createHistoryEmitter();
59
- let history: inspectorSchema.WorkflowHistory | null = null;
51
+ let history: WorkflowHistoryBytes | null = null;
60
52
  let getState: () => Promise<WorkflowState | null> = async () => null;
61
53
  let replayFromStep: (
62
54
  entryId?: string,
63
- ) => Promise<inspectorSchema.WorkflowHistory | null> = async () => {
55
+ ) => Promise<WorkflowHistoryBytes | null> = async () => {
64
56
  throw new Error("Workflow replay controls are not initialized");
65
57
  };
66
58
 
@@ -91,7 +83,7 @@ export function createWorkflowInspectorAdapter(): {
91
83
  }
92
84
 
93
85
  function encodeCbor(value: unknown): ArrayBuffer {
94
- return bufferToArrayBuffer(encodeCborCompat(value));
86
+ return encodeWorkflowInspectorValue(value);
95
87
  }
96
88
 
97
89
  function encodeOptionalCbor(value: unknown): ArrayBuffer | null {
@@ -183,6 +175,14 @@ function toWorkflowEntryKind(kind: EntryKind): transport.WorkflowEntryKind {
183
175
  originalName: kind.data.originalName ?? null,
184
176
  },
185
177
  };
178
+ case "version_check":
179
+ return {
180
+ tag: "WorkflowVersionCheckEntry",
181
+ val: {
182
+ resolved: kind.data.resolved,
183
+ latest: kind.data.latest,
184
+ },
185
+ };
186
186
  default:
187
187
  assertUnreachable(kind as never);
188
188
  }