@rikalabs/akter 0.1.0-alpha.0 → 0.1.0-alpha.3

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 (308) hide show
  1. package/CHANGELOG.md +46 -3
  2. package/README.md +2 -2
  3. package/dist/actor/definition.d.ts +13 -9
  4. package/dist/actor/descriptor.d.ts +2 -2
  5. package/dist/actor/descriptor.js +4 -4
  6. package/dist/client/calls.js +1 -1
  7. package/dist/client/index.d.ts +1 -1
  8. package/dist/client/index.js +1 -1
  9. package/dist/errors/actor.d.ts +12 -42
  10. package/dist/errors/actor.js +13 -42
  11. package/dist/index.d.ts +1 -1
  12. package/dist/index.js +1 -1
  13. package/dist/members/payload.d.ts +1 -1
  14. package/dist/protocol/inspection.d.ts +221 -1
  15. package/dist/protocol/inspection.js +151 -2
  16. package/dist/protocol/wire.d.ts +6 -49
  17. package/dist/protocol/wire.js +1 -10
  18. package/dist/runtime/actors.d.ts +3 -0
  19. package/dist/runtime/adoption/backfill.js +1 -1
  20. package/dist/runtime/adoption/enforce.js +2 -2
  21. package/dist/runtime/adoption/observe.js +1 -1
  22. package/dist/runtime/adoption/plan.d.ts +1 -1
  23. package/dist/runtime/adoption/startup.js +1 -1
  24. package/dist/runtime/adoption/status.d.ts +1 -1
  25. package/dist/runtime/app.d.ts +65 -0
  26. package/dist/runtime/app.js +38 -0
  27. package/dist/runtime/connections/holder.d.ts +11 -1
  28. package/dist/runtime/connections/holder.js +8 -4
  29. package/dist/runtime/connections/streams.d.ts +4 -4
  30. package/dist/runtime/content/store.js +15 -11
  31. package/dist/runtime/database/bounded.d.ts +4 -18
  32. package/dist/runtime/database/bounded.js +36 -22
  33. package/dist/runtime/database/coordination.d.ts +12 -3
  34. package/dist/runtime/database/coordination.js +29 -36
  35. package/dist/runtime/database/migrations.d.ts +61 -29
  36. package/dist/runtime/database/migrations.js +375 -139
  37. package/dist/runtime/database/replica.d.ts +4 -1
  38. package/dist/runtime/database/replica.js +4 -1
  39. package/dist/runtime/database/schema.d.ts +13 -0
  40. package/dist/runtime/database/schema.js +13 -0
  41. package/dist/runtime/database/shards.d.ts +31 -13
  42. package/dist/runtime/database/shards.js +38 -26
  43. package/dist/runtime/entity/register.d.ts +8 -1
  44. package/dist/runtime/entity/register.js +58 -7
  45. package/dist/runtime/fleet/checks.js +4 -4
  46. package/dist/runtime/fleet/maintainer.d.ts +1 -1
  47. package/dist/runtime/fleet/maintainer.js +1 -1
  48. package/dist/runtime/index.d.ts +2 -0
  49. package/dist/runtime/index.js +1 -0
  50. package/dist/runtime/inspector/layer.d.ts +20 -0
  51. package/dist/runtime/inspector/layer.js +224 -5
  52. package/dist/runtime/inspector/queries.d.ts +54 -8
  53. package/dist/runtime/inspector/queries.js +84 -15
  54. package/dist/runtime/layer.d.ts +48 -39
  55. package/dist/runtime/layer.js +124 -56
  56. package/dist/runtime/operators/audit.d.ts +3 -0
  57. package/dist/runtime/operators/audit.js +6 -2
  58. package/dist/runtime/operators/repair.js +30 -10
  59. package/dist/runtime/operators/seed.js +4 -2
  60. package/dist/runtime/payloads/versions.d.ts +4 -4
  61. package/dist/runtime/payloads/versions.js +24 -15
  62. package/dist/runtime/queries.js +4 -2
  63. package/dist/runtime/registration.d.ts +5 -2
  64. package/dist/runtime/registration.js +3 -3
  65. package/dist/runtime/runner.d.ts +12 -0
  66. package/dist/runtime/runner.js +12 -0
  67. package/dist/runtime/storage/codec.d.ts +19 -7
  68. package/dist/runtime/storage/codec.js +23 -7
  69. package/dist/runtime/storage/placements.d.ts +6 -1
  70. package/dist/runtime/storage/placements.js +98 -6
  71. package/dist/runtime/storage/retention.d.ts +6 -0
  72. package/dist/runtime/storage/retention.js +25 -13
  73. package/dist/runtime/subscriptions/relay.js +47 -44
  74. package/dist/runtime/subscriptions/storage.d.ts +3 -2
  75. package/dist/runtime/subscriptions/storage.js +9 -6
  76. package/dist/runtime/telemetry/live.d.ts +255 -0
  77. package/dist/runtime/telemetry/live.js +540 -0
  78. package/dist/runtime/telemetry/metrics.d.ts +2 -0
  79. package/dist/runtime/telemetry/metrics.js +4 -1
  80. package/dist/runtime/telemetry/sampler.js +33 -13
  81. package/dist/runtime/telemetry/spans.d.ts +1 -1
  82. package/dist/runtime/telemetry/spans.js +1 -1
  83. package/dist/runtime/topology/locks.d.ts +6 -3
  84. package/dist/runtime/topology/locks.js +27 -4
  85. package/dist/runtime/turn/execute.d.ts +2 -0
  86. package/dist/runtime/turn/execute.js +5 -2
  87. package/dist/runtime/turn/pipeline.d.ts +5 -12
  88. package/dist/runtime/turn/pipeline.js +11 -11
  89. package/dist/runtime/turn/relay.d.ts +13 -3
  90. package/dist/runtime/turn/relay.js +27 -14
  91. package/dist/runtime/turn/rows.js +4 -4
  92. package/dist/runtime/workflows/compatibility.d.ts +1 -1
  93. package/dist/runtime/workflows/compatibility.js +85 -31
  94. package/dist/runtime/workflows/engine.js +20 -17
  95. package/dist/serve/layer.js +3 -2
  96. package/dist/tables/owned.d.ts +1 -1
  97. package/dist/tables/owned.js +1 -1
  98. package/dist/testing/actor-test.d.ts +2 -24
  99. package/dist/testing/actor-test.js +1 -27
  100. package/dist/testing/index.d.ts +0 -12
  101. package/dist/testing/index.js +0 -6
  102. package/package.json +1 -1
  103. package/dist/runtime/database/neki/migrations.d.ts +0 -31
  104. package/dist/runtime/database/neki/migrations.js +0 -276
  105. package/dist/runtime/database/neki/session.d.ts +0 -23
  106. package/dist/runtime/database/neki/session.js +0 -31
  107. package/dist/testing/cluster.d.ts +0 -93
  108. package/dist/testing/cluster.js +0 -296
  109. package/dist/testing/conformance/access.d.ts +0 -5
  110. package/dist/testing/conformance/access.js +0 -531
  111. package/dist/testing/conformance/admission.d.ts +0 -11
  112. package/dist/testing/conformance/admission.js +0 -182
  113. package/dist/testing/conformance/adoption.d.ts +0 -2
  114. package/dist/testing/conformance/adoption.js +0 -1019
  115. package/dist/testing/conformance/assertions.d.ts +0 -202
  116. package/dist/testing/conformance/assertions.js +0 -849
  117. package/dist/testing/conformance/batches.d.ts +0 -41
  118. package/dist/testing/conformance/batches.js +0 -388
  119. package/dist/testing/conformance/blobs.d.ts +0 -14
  120. package/dist/testing/conformance/blobs.js +0 -405
  121. package/dist/testing/conformance/capacity.d.ts +0 -8
  122. package/dist/testing/conformance/capacity.js +0 -433
  123. package/dist/testing/conformance/client.d.ts +0 -5
  124. package/dist/testing/conformance/client.js +0 -719
  125. package/dist/testing/conformance/cold-serve.d.ts +0 -16
  126. package/dist/testing/conformance/cold-serve.js +0 -254
  127. package/dist/testing/conformance/connections/actors.d.ts +0 -1143
  128. package/dist/testing/conformance/connections/actors.js +0 -180
  129. package/dist/testing/conformance/connections/harness.d.ts +0 -1754
  130. package/dist/testing/conformance/connections/harness.js +0 -142
  131. package/dist/testing/conformance/connections/holders.d.ts +0 -4
  132. package/dist/testing/conformance/connections/holders.js +0 -165
  133. package/dist/testing/conformance/connections/lifecycle.d.ts +0 -4
  134. package/dist/testing/conformance/connections/lifecycle.js +0 -113
  135. package/dist/testing/conformance/connections/limits.d.ts +0 -4
  136. package/dist/testing/conformance/connections/limits.js +0 -127
  137. package/dist/testing/conformance/connections/reauthorization.d.ts +0 -4
  138. package/dist/testing/conformance/connections/reauthorization.js +0 -170
  139. package/dist/testing/conformance/connections/resync.d.ts +0 -4
  140. package/dist/testing/conformance/connections/resync.js +0 -360
  141. package/dist/testing/conformance/connections/takeover.d.ts +0 -4
  142. package/dist/testing/conformance/connections/takeover.js +0 -287
  143. package/dist/testing/conformance/connections.d.ts +0 -4
  144. package/dist/testing/conformance/connections.js +0 -15
  145. package/dist/testing/conformance/content-blobs.d.ts +0 -16
  146. package/dist/testing/conformance/content-blobs.js +0 -534
  147. package/dist/testing/conformance/counter.d.ts +0 -16
  148. package/dist/testing/conformance/counter.js +0 -813
  149. package/dist/testing/conformance/cron.d.ts +0 -5
  150. package/dist/testing/conformance/cron.js +0 -665
  151. package/dist/testing/conformance/drain.d.ts +0 -26
  152. package/dist/testing/conformance/drain.js +0 -540
  153. package/dist/testing/conformance/effect-control.d.ts +0 -38
  154. package/dist/testing/conformance/effect-control.js +0 -1059
  155. package/dist/testing/conformance/effects.d.ts +0 -23
  156. package/dist/testing/conformance/effects.js +0 -432
  157. package/dist/testing/conformance/events.d.ts +0 -28
  158. package/dist/testing/conformance/events.js +0 -335
  159. package/dist/testing/conformance/export.d.ts +0 -2
  160. package/dist/testing/conformance/export.js +0 -368
  161. package/dist/testing/conformance/fleet.d.ts +0 -444
  162. package/dist/testing/conformance/fleet.js +0 -1009
  163. package/dist/testing/conformance/groups.d.ts +0 -7
  164. package/dist/testing/conformance/groups.js +0 -353
  165. package/dist/testing/conformance/heap.d.ts +0 -3
  166. package/dist/testing/conformance/heap.js +0 -149
  167. package/dist/testing/conformance/http.d.ts +0 -2809
  168. package/dist/testing/conformance/http.js +0 -1234
  169. package/dist/testing/conformance/inspection-views.d.ts +0 -8
  170. package/dist/testing/conformance/inspection-views.js +0 -272
  171. package/dist/testing/conformance/inspector.d.ts +0 -7
  172. package/dist/testing/conformance/inspector.js +0 -650
  173. package/dist/testing/conformance/mint.d.ts +0 -58
  174. package/dist/testing/conformance/mint.js +0 -692
  175. package/dist/testing/conformance/multi-runner.d.ts +0 -8
  176. package/dist/testing/conformance/multi-runner.js +0 -403
  177. package/dist/testing/conformance/neki/plan.d.ts +0 -19
  178. package/dist/testing/conformance/neki/plan.js +0 -30
  179. package/dist/testing/conformance/observability.d.ts +0 -3
  180. package/dist/testing/conformance/observability.js +0 -343
  181. package/dist/testing/conformance/offline.d.ts +0 -4
  182. package/dist/testing/conformance/offline.js +0 -474
  183. package/dist/testing/conformance/operator-harness.d.ts +0 -57
  184. package/dist/testing/conformance/operator-harness.js +0 -78
  185. package/dist/testing/conformance/operator.d.ts +0 -3
  186. package/dist/testing/conformance/operator.js +0 -379
  187. package/dist/testing/conformance/outbox-cross-shard.d.ts +0 -14
  188. package/dist/testing/conformance/outbox-cross-shard.js +0 -160
  189. package/dist/testing/conformance/outbox.d.ts +0 -36
  190. package/dist/testing/conformance/outbox.js +0 -437
  191. package/dist/testing/conformance/payload-migrations.d.ts +0 -3
  192. package/dist/testing/conformance/payload-migrations.js +0 -612
  193. package/dist/testing/conformance/pipeline.d.ts +0 -3
  194. package/dist/testing/conformance/pipeline.js +0 -1040
  195. package/dist/testing/conformance/placement.d.ts +0 -22
  196. package/dist/testing/conformance/placement.js +0 -539
  197. package/dist/testing/conformance/platform.d.ts +0 -40
  198. package/dist/testing/conformance/platform.js +0 -101
  199. package/dist/testing/conformance/progress.d.ts +0 -39
  200. package/dist/testing/conformance/progress.js +0 -666
  201. package/dist/testing/conformance/properties.d.ts +0 -6
  202. package/dist/testing/conformance/properties.js +0 -295
  203. package/dist/testing/conformance/protocols.d.ts +0 -4
  204. package/dist/testing/conformance/protocols.js +0 -618
  205. package/dist/testing/conformance/provider/advertise.d.ts +0 -21
  206. package/dist/testing/conformance/provider/advertise.js +0 -44
  207. package/dist/testing/conformance/read-your-writes.d.ts +0 -20
  208. package/dist/testing/conformance/read-your-writes.js +0 -192
  209. package/dist/testing/conformance/reducers.d.ts +0 -8
  210. package/dist/testing/conformance/reducers.js +0 -258
  211. package/dist/testing/conformance/relay.d.ts +0 -42
  212. package/dist/testing/conformance/relay.js +0 -1115
  213. package/dist/testing/conformance/restore.d.ts +0 -43
  214. package/dist/testing/conformance/restore.js +0 -341
  215. package/dist/testing/conformance/retention.d.ts +0 -14
  216. package/dist/testing/conformance/retention.js +0 -463
  217. package/dist/testing/conformance/rls.d.ts +0 -24
  218. package/dist/testing/conformance/rls.js +0 -576
  219. package/dist/testing/conformance/simulation.d.ts +0 -31
  220. package/dist/testing/conformance/simulation.js +0 -167
  221. package/dist/testing/conformance/single-shard.d.ts +0 -2
  222. package/dist/testing/conformance/single-shard.js +0 -125
  223. package/dist/testing/conformance/singleton.d.ts +0 -3
  224. package/dist/testing/conformance/singleton.js +0 -293
  225. package/dist/testing/conformance/statements.d.ts +0 -60
  226. package/dist/testing/conformance/statements.js +0 -108
  227. package/dist/testing/conformance/streams.d.ts +0 -7
  228. package/dist/testing/conformance/streams.js +0 -280
  229. package/dist/testing/conformance/subscriptions/access.d.ts +0 -4
  230. package/dist/testing/conformance/subscriptions/access.js +0 -98
  231. package/dist/testing/conformance/subscriptions/actors.d.ts +0 -4910
  232. package/dist/testing/conformance/subscriptions/actors.js +0 -372
  233. package/dist/testing/conformance/subscriptions/cluster.d.ts +0 -4
  234. package/dist/testing/conformance/subscriptions/cluster.js +0 -89
  235. package/dist/testing/conformance/subscriptions/delivery.d.ts +0 -4
  236. package/dist/testing/conformance/subscriptions/delivery.js +0 -253
  237. package/dist/testing/conformance/subscriptions/deploys.d.ts +0 -4
  238. package/dist/testing/conformance/subscriptions/deploys.js +0 -162
  239. package/dist/testing/conformance/subscriptions/epochs.d.ts +0 -4
  240. package/dist/testing/conformance/subscriptions/epochs.js +0 -169
  241. package/dist/testing/conformance/subscriptions/failures.d.ts +0 -4
  242. package/dist/testing/conformance/subscriptions/failures.js +0 -108
  243. package/dist/testing/conformance/subscriptions/feeds.d.ts +0 -4
  244. package/dist/testing/conformance/subscriptions/feeds.js +0 -218
  245. package/dist/testing/conformance/subscriptions/harness.d.ts +0 -124
  246. package/dist/testing/conformance/subscriptions/harness.js +0 -143
  247. package/dist/testing/conformance/subscriptions/operator.d.ts +0 -4
  248. package/dist/testing/conformance/subscriptions/operator.js +0 -128
  249. package/dist/testing/conformance/subscriptions/retention.d.ts +0 -4
  250. package/dist/testing/conformance/subscriptions/retention.js +0 -288
  251. package/dist/testing/conformance/subscriptions/routing.d.ts +0 -4
  252. package/dist/testing/conformance/subscriptions/routing.js +0 -179
  253. package/dist/testing/conformance/subscriptions.d.ts +0 -4
  254. package/dist/testing/conformance/subscriptions.js +0 -19
  255. package/dist/testing/conformance/tables.d.ts +0 -125
  256. package/dist/testing/conformance/tables.js +0 -664
  257. package/dist/testing/conformance/transports/actors.d.ts +0 -1907
  258. package/dist/testing/conformance/transports/actors.js +0 -163
  259. package/dist/testing/conformance/transports/client.d.ts +0 -3
  260. package/dist/testing/conformance/transports/client.js +0 -237
  261. package/dist/testing/conformance/transports/feeds.d.ts +0 -3
  262. package/dist/testing/conformance/transports/feeds.js +0 -232
  263. package/dist/testing/conformance/transports/harness.d.ts +0 -93
  264. package/dist/testing/conformance/transports/harness.js +0 -84
  265. package/dist/testing/conformance/transports/streams.d.ts +0 -3
  266. package/dist/testing/conformance/transports/streams.js +0 -96
  267. package/dist/testing/conformance/transports/websocket.d.ts +0 -3
  268. package/dist/testing/conformance/transports/websocket.js +0 -384
  269. package/dist/testing/conformance/transports/wire.d.ts +0 -141
  270. package/dist/testing/conformance/transports/wire.js +0 -204
  271. package/dist/testing/conformance/transports.d.ts +0 -3
  272. package/dist/testing/conformance/transports.js +0 -11
  273. package/dist/testing/conformance/watch.d.ts +0 -59
  274. package/dist/testing/conformance/watch.js +0 -879
  275. package/dist/testing/conformance/workflow-engine.d.ts +0 -97
  276. package/dist/testing/conformance/workflow-engine.js +0 -253
  277. package/dist/testing/conformance/workflow-versions.d.ts +0 -3
  278. package/dist/testing/conformance/workflow-versions.js +0 -545
  279. package/dist/testing/conformance/workflows/access.d.ts +0 -4
  280. package/dist/testing/conformance/workflows/access.js +0 -147
  281. package/dist/testing/conformance/workflows/actors.d.ts +0 -2317
  282. package/dist/testing/conformance/workflows/actors.js +0 -305
  283. package/dist/testing/conformance/workflows/basics.d.ts +0 -4
  284. package/dist/testing/conformance/workflows/basics.js +0 -144
  285. package/dist/testing/conformance/workflows/engine.d.ts +0 -4
  286. package/dist/testing/conformance/workflows/engine.js +0 -80
  287. package/dist/testing/conformance/workflows/guards.d.ts +0 -4
  288. package/dist/testing/conformance/workflows/guards.js +0 -172
  289. package/dist/testing/conformance/workflows/harness.d.ts +0 -14
  290. package/dist/testing/conformance/workflows/harness.js +0 -44
  291. package/dist/testing/conformance/workflows/recovery.d.ts +0 -4
  292. package/dist/testing/conformance/workflows/recovery.js +0 -181
  293. package/dist/testing/conformance/workflows/starts.d.ts +0 -4
  294. package/dist/testing/conformance/workflows/starts.js +0 -57
  295. package/dist/testing/conformance/workflows/suspension.d.ts +0 -4
  296. package/dist/testing/conformance/workflows/suspension.js +0 -106
  297. package/dist/testing/conformance/workflows/waits.d.ts +0 -4
  298. package/dist/testing/conformance/workflows/waits.js +0 -284
  299. package/dist/testing/conformance/workflows.d.ts +0 -4
  300. package/dist/testing/conformance/workflows.js +0 -19
  301. package/dist/testing/conformance.d.ts +0 -330
  302. package/dist/testing/conformance.js +0 -364
  303. package/dist/testing/foundation.d.ts +0 -41
  304. package/dist/testing/foundation.js +0 -517
  305. package/dist/testing/simulate-cluster.d.ts +0 -112
  306. package/dist/testing/simulate-cluster.js +0 -257
  307. package/dist/testing/simulate.d.ts +0 -90
  308. package/dist/testing/simulate.js +0 -127
@@ -11,6 +11,7 @@ import { Caller, principal, System } from "../../identity/caller.js";
11
11
  import { decodeExecutionId, encodeExecutionId } from "../../identity/execution.js";
12
12
  import { bucketOf, OutboxRuntime, writeOutbox } from "../turn/outbox.js";
13
13
  import { databaseTime } from "../turn/admission.js";
14
+ import { registry } from "../database/coordination.js";
14
15
  import { TurnHooks } from "../turn/hooks.js";
15
16
  import { compress, decompress } from "../storage/codec.js";
16
17
  import { actorRow, fence } from "../storage/generation.js";
@@ -77,7 +78,9 @@ const deleteTimer = (sql, routingKey, ref, executionId) => sql `DELETE FROM acto
77
78
  * start is delivered. The run starts at once on this activation and the recovery
78
79
  * timer resumes it if this runner dies first. The start manifest is restored if
79
80
  * retention pruned it while a runner of an older deployment still starts
80
- * executions under it.
81
+ * executions under it. The manifest is a deployment registry row, so it is
82
+ * written on the registry client after the execution row: inside the turn on
83
+ * one database, and committed before the turn on a sharded one.
81
84
  */
82
85
  const insertExecution = Effect.fnUntraced(function* (options) {
83
86
  const sql = yield* SqlClient.SqlClient;
@@ -85,7 +88,7 @@ const insertExecution = Effect.fnUntraced(function* (options) {
85
88
  const now = yield* databaseTime;
86
89
  const manifest = yield* manifestOf(ref.actor, workflow.member);
87
90
  const inserted = yield* sql `
88
- WITH x AS (INSERT INTO actor_workflow_executions (routing_key, execution_id, bucket, tenant_id, actor_type, actor_id,
91
+ INSERT INTO actor_workflow_executions (routing_key, execution_id, bucket, tenant_id, actor_type, actor_id,
89
92
  workflow, workflow_key, manifest_hash, payload, caller, event_cursor, status, started_at_ms)
90
93
  SELECT ${routingKey}, ${executionId}, ${bucketOf(routingKey)}, ${ref.tenant}, ${ref.actor}, ${ref.id},
91
94
  ${workflow.member.tag}, ${options.key}, ${manifest.hash}, ${compress(options.payload)},
@@ -93,14 +96,13 @@ const insertExecution = Effect.fnUntraced(function* (options) {
93
96
  COALESCE(${options.after}::bigint, g.event_sequence),
94
97
  'running', ${now}
95
98
  FROM actor_generations g WHERE ${actorRow({ sql, actor: { key: routingKey, ref } })}
96
- ON CONFLICT DO NOTHING RETURNING 1),
97
- m AS (INSERT INTO actor_workflow_manifests (actor_type, workflow, manifest_hash, manifest, accepted_at_ms)
98
- SELECT ${ref.actor}, ${workflow.member.tag}, ${manifest.hash}, ${toJson(manifest.manifest)}::jsonb, 0
99
- WHERE EXISTS (SELECT 1 FROM x)
100
- ON CONFLICT DO NOTHING)
101
- SELECT 1 FROM x`;
99
+ ON CONFLICT DO NOTHING RETURNING 1`;
102
100
  if (inserted.length === 0)
103
101
  return false;
102
+ yield* (yield* registry) `INSERT INTO actor_workflow_manifests
103
+ (actor_type, workflow, manifest_hash, manifest, accepted_at_ms)
104
+ VALUES (${ref.actor}, ${workflow.member.tag}, ${manifest.hash}, ${toJson(manifest.manifest)}::jsonb, 0)
105
+ ON CONFLICT DO NOTHING`;
104
106
  const markers = Object.entries(workflow.member.versions);
105
107
  if (markers.length > 0)
106
108
  yield* sql `INSERT INTO actor_workflow_step ${sql.insert(markers.map(([name, range]) => ({
@@ -304,7 +306,7 @@ export const activationEngine = (options) => Effect.gen(function* () {
304
306
  const known = hash === declared.hash ? undefined : startVerdicts.get(hash);
305
307
  if (hash === declared.hash || known !== undefined)
306
308
  return known ?? startVerdict({ declared, hash, start: undefined, newer: false });
307
- const [row] = yield* sql `
309
+ const [row] = yield* (yield* registry) `
308
310
  SELECT m.manifest::text AS manifest, m.accepted_at_ms > COALESCE((SELECT o.accepted_at_ms
309
311
  FROM actor_workflow_manifests o WHERE o.actor_type = m.actor_type AND o.workflow = m.workflow
310
312
  AND o.manifest_hash = ${declared.hash}), -1) AS newer
@@ -367,14 +369,15 @@ export const activationEngine = (options) => Effect.gen(function* () {
367
369
  */
368
370
  const adoptManifest = (declared, workflow, execution, executionId, changed) => execution.manifest_hash === declared.hash || changed.length === 0
369
371
  ? Effect.void
370
- : fenced(sql `
371
- WITH m AS (INSERT INTO actor_workflow_manifests
372
- (actor_type, workflow, manifest_hash, manifest, accepted_at_ms)
373
- VALUES (${ref.actor}, ${workflow.member.tag}, ${declared.hash}, ${toJson(declared.manifest)}::jsonb, 0)
374
- ON CONFLICT DO NOTHING)
375
- UPDATE actor_workflow_executions SET manifest_hash = ${declared.hash}
376
- WHERE routing_key = ${routingKey} AND execution_id = ${executionId}
377
- AND manifest_hash = ${execution.manifest_hash}`).pipe(Effect.asVoid);
372
+ : fenced(Effect.gen(function* () {
373
+ yield* (yield* registry) `INSERT INTO actor_workflow_manifests
374
+ (actor_type, workflow, manifest_hash, manifest, accepted_at_ms)
375
+ VALUES (${ref.actor}, ${workflow.member.tag}, ${declared.hash}, ${toJson(declared.manifest)}::jsonb, 0)
376
+ ON CONFLICT DO NOTHING`;
377
+ yield* sql `UPDATE actor_workflow_executions SET manifest_hash = ${declared.hash}
378
+ WHERE routing_key = ${routingKey} AND execution_id = ${executionId}
379
+ AND manifest_hash = ${execution.manifest_hash}`;
380
+ }));
378
381
  /** Records an execution's result, then drops its steps and its timer. */
379
382
  const finish = (executionId, result) => fenced(Effect.gen(function* () {
380
383
  const at = yield* now;
@@ -520,14 +520,15 @@ export const serve = (options) => HttpRouter.use(Effect.fnUntraced(function* (ro
520
520
  const payload = yield* member
521
521
  .payload(body)
522
522
  .pipe(Effect.mapError((error) => undecodable(error)));
523
+ const ref = refOf(definition, id, authenticated);
523
524
  const elements = actors.subscribe(Request.make({
524
- ref: refOf(definition, id, authenticated),
525
+ ref,
525
526
  caller: authenticated.caller,
526
527
  command: member.tag,
527
528
  commandId: "",
528
529
  payload,
529
530
  }));
530
- return elements.pipe(streamResponse, eventStream);
531
+ return Stream.unwrap(Effect.acquireRelease(Effect.sync(() => actors.live.streamOpened(ref)), (release) => Effect.sync(release)).pipe(Effect.as(elements))).pipe(streamResponse, eventStream);
531
532
  });
532
533
  const watchHandler = (definition, member) => Effect.fnUntraced(function* (request) {
533
534
  const id = yield* pathId(definition);
@@ -235,7 +235,7 @@ export type Declared<T extends AnyPgTable, O extends OwnerColumns<T> | undefined
235
235
  * `tenant` and `actor` columns, which must be text, varchar, or uuid, hold the
236
236
  * tenant and the actor's key, and nothing else about the table changes, so
237
237
  * foreign keys stay. A writable adopted table gains a nullable `routing_key`
238
- * and starts only after `durable adopt observe`; its primary key stays global,
238
+ * and starts only after `akter adopt observe`; its primary key stays global,
239
239
  * so a key another actor already holds fails the turn. `access: "read"` maps
240
240
  * the two columns for reading and adds nothing.
241
241
  *
@@ -119,7 +119,7 @@ const adopt = (source, options) => {
119
119
  * `tenant` and `actor` columns, which must be text, varchar, or uuid, hold the
120
120
  * tenant and the actor's key, and nothing else about the table changes, so
121
121
  * foreign keys stay. A writable adopted table gains a nullable `routing_key`
122
- * and starts only after `durable adopt observe`; its primary key stays global,
122
+ * and starts only after `akter adopt observe`; its primary key stays global,
123
123
  * so a key another actor already holds fails the turn. `access: "read"` maps
124
124
  * the two columns for reading and adds nothing.
125
125
  *
@@ -14,15 +14,12 @@ import { type Options } from "../runtime/layer.ts";
14
14
  import { type TurnPoint } from "../runtime/turn/hooks.ts";
15
15
  import { type ProgressClosed, type ProgressMessage } from "../runtime/jobs/progress.ts";
16
16
  import type { Swept } from "../runtime/storage/retention.ts";
17
- import { ActorCluster, type ClusterOptions } from "./cluster.ts";
18
- import { type Simulation, type SimulationOptions } from "./simulate.ts";
19
- import { type ClusterSimulation, type ClusterSimulationOptions } from "./simulate-cluster.ts";
20
17
  declare const ClusterMember_base: Context.ServiceClass<ClusterMember, "@rikalabs/akter/testing/actor-test/ClusterMember", {
21
18
  readonly tenant: string;
22
19
  readonly connect: NonNullable<PgClient.PgPoolConfig["stream"]>;
23
20
  }>;
24
21
  /**
25
- * Present while `ActorTest.cluster` builds one of its runners: the runner
22
+ * Present while the framework's cluster harness builds one of its runners: the runner
26
23
  * shares the cluster's tenant and opens its database connections through
27
24
  * `connect`, so killing it can cut them.
28
25
  */
@@ -259,7 +256,7 @@ export declare const ProgressRecord: {
259
256
  /** Options of `ActorTest.actor`. */
260
257
  export interface TestActorOptions {
261
258
  /**
262
- * Path of a seed file `durable export` wrote. The actor starts from its
259
+ * Path of a seed file `akter export` wrote. The actor starts from its
263
260
  * state and pending work, staged as the caller the test runs as; the seed
264
261
  * carries no tenant, caller, or credential. Reading it needs a `FileSystem`.
265
262
  * An actor that already exists, a seed of another actor type, and a seed
@@ -336,25 +333,6 @@ declare const ActorTest_base: Context.ServiceClass<ActorTest, "@rikalabs/akter/t
336
333
  readonly dropProgress: (predicate: (message: ProgressMessage | ProgressClosed) => boolean) => Effect.Effect<void>;
337
334
  }>;
338
335
  export declare class ActorTest extends ActorTest_base {
339
- /**
340
- * Runs `runners` runtimes in this process against one Postgres database,
341
- * each a distinct Cluster runner with its own address, connection pool, and
342
- * expiring shard locks. Provides `ActorCluster`; see its controls.
343
- */
344
- static readonly cluster: <ROut, E, RIn>(options: ClusterOptions<ROut, E, RIn>) => Layer.Layer<ActorCluster, never, Crypto.Crypto | Exclude<Exclude<RIn, ActorTest | Actors | import("../index.ts").ContentStore | import("../runtime/index.ts").DefectLog | InternalActors | import("../runtime/operators/repair.ts").OperatorRuntime | PgliteClient.PgliteClient | import("../runtime/drain.ts").RuntimeControl | SqlClient.SqlClient | import("../runtime/index.ts").TelemetrySampler>, import("effect/Scope").Scope>>;
345
- /**
346
- * Runs `program` on the current test runtime under a fault schedule drawn
347
- * from `seed`, then checks exactly-once receipts and outbox delivery; a
348
- * failure dies with the seed that reproduces it.
349
- */
350
- static readonly simulate: <E, R>(options: SimulationOptions, program: (simulation: Simulation) => Effect.Effect<void, E, R>) => Effect.Effect<import("./simulate.ts").SimulationReport, never, R | ActorTest | Actors | SqlClient.SqlClient>;
351
- /**
352
- * Runs `program` on the current `ActorTest.cluster` under a fault schedule
353
- * drawn from `seed`, with runner kills, lost heartbeats, and primary
354
- * failovers as well as crashes, then checks exactly-once receipts and
355
- * outbox delivery; a failure dies with the seed that reproduces it.
356
- */
357
- static readonly simulateCluster: <E, R>(options: ClusterSimulationOptions, program: (simulation: ClusterSimulation) => Effect.Effect<void, E, R>) => Effect.Effect<import("./simulate-cluster.ts").ClusterSimulationReport, never, R | ActorCluster>;
358
336
  static readonly layer: (options?: TestOptions) => Layer.Layer<ActorTest | Actors | import("../index.ts").ContentStore | import("../runtime/index.ts").DefectLog | InternalActors | import("../runtime/operators/repair.ts").OperatorRuntime | PgliteClient.PgliteClient | import("../runtime/drain.ts").RuntimeControl | SqlClient.SqlClient | import("../runtime/index.ts").TelemetrySampler, import("../errors/database.ts").DataDirLocked | import("../errors/database.ts").DataDirVersion | import("effect/sql/Migrator").MigrationError | import("effect/sql/SqlError").SqlError, Crypto.Crypto>;
359
337
  }
360
338
  export {};
@@ -17,11 +17,8 @@ import { VERSION_KEY } from "../state/migration.js";
17
17
  import { CleanupHooks, RetryTurn, TurnHooks } from "../runtime/turn/hooks.js";
18
18
  import { ProgressTap } from "../runtime/jobs/progress.js";
19
19
  import { databaseTime, FrameworkClock } from "../runtime/turn/admission.js";
20
- import { ActorCluster, clusterLayer } from "./cluster.js";
21
- import { simulate } from "./simulate.js";
22
- import { simulateCluster, } from "./simulate-cluster.js";
23
20
  /**
24
- * Present while `ActorTest.cluster` builds one of its runners: the runner
21
+ * Present while the framework's cluster harness builds one of its runners: the runner
25
22
  * shares the cluster's tenant and opens its database connections through
26
23
  * `connect`, so killing it can cut them.
27
24
  */
@@ -59,29 +56,6 @@ export const sweepContent = Effect.gen(function* () {
59
56
  });
60
57
  export const ProgressRecord = Data.taggedEnum();
61
58
  export class ActorTest extends Context.Service()("@rikalabs/akter/testing/actor-test/ActorTest") {
62
- /**
63
- * Runs `runners` runtimes in this process against one Postgres database,
64
- * each a distinct Cluster runner with its own address, connection pool, and
65
- * expiring shard locks. Provides `ActorCluster`; see its controls.
66
- */
67
- static cluster = (options) => clusterLayer(options);
68
- /**
69
- * Runs `program` on the current test runtime under a fault schedule drawn
70
- * from `seed`, then checks exactly-once receipts and outbox delivery; a
71
- * failure dies with the seed that reproduces it.
72
- */
73
- static simulate = (options, program) => Effect.gen(function* () {
74
- return yield* simulate(yield* ActorTest)(options, program);
75
- });
76
- /**
77
- * Runs `program` on the current `ActorTest.cluster` under a fault schedule
78
- * drawn from `seed`, with runner kills, lost heartbeats, and primary
79
- * failovers as well as crashes, then checks exactly-once receipts and
80
- * outbox delivery; a failure dies with the seed that reproduces it.
81
- */
82
- static simulateCluster = (options, program) => Effect.gen(function* () {
83
- return yield* simulateCluster(yield* ActorCluster)(options, program);
84
- });
85
59
  static layer = (options = {}) => Layer.unwrap(Effect.gen(function* () {
86
60
  const crypto = yield* Crypto.Crypto;
87
61
  const member = Option.getOrUndefined(yield* Effect.serviceOption(ClusterMember));
@@ -1,19 +1,7 @@
1
1
  export { ActorTest, cleanup, sweepContent } from "./actor-test.ts";
2
2
  export type { FaultOptions, TestActorOptions, TestConnection, TestMessage } from "./actor-test.ts";
3
- export { ActorCluster } from "./cluster.ts";
4
3
  export { checkBatchLaw } from "./property.ts";
5
4
  export { disposableDatabase, testDatabase } from "./database.ts";
6
- export { SIMULATION_SEEDS, simulationSeeds } from "./simulate.ts";
7
- export type { Simulation, SimulationFault, SimulationOptions, SimulationReport, SimulationStep, } from "./simulate.ts";
8
- export { CLUSTER_SIMULATION_SEEDS, clusterSimulationSeeds, FAILOVER_SIMULATION_SEEDS, failoverSimulationSeeds, NIGHTLY_CLUSTER_SIMULATION_SEEDS, NIGHTLY_FAILOVER_SIMULATION_SEEDS, } from "./simulate-cluster.ts";
9
- export type { ClusterSimulation, ClusterSimulationFault, ClusterSimulationOptions, ClusterSimulationReport, ClusterSimulationStep, } from "./simulate-cluster.ts";
10
- export type { ClusterOptions, RunnerServices } from "./cluster.ts";
11
- export { conformance, describeConformance } from "./conformance.ts";
12
- export type { ConformanceBackend, ConformanceCase, ConformanceConnection, ConformanceContext, ConformanceDatabase, ConformanceEnvironment, ConformanceExpect, ConformanceAccess, ConformanceGroup, ConformanceSuite, ConformanceMatchers, ConformanceRegistrar, ConformanceRuntime, ConformanceServices, } from "./conformance.ts";
13
- export { edgeKey } from "./conformance/assertions.ts";
14
- export type { ConformanceEdge, EdgeKey, EdgeRunner, HostedEdge } from "./conformance/assertions.ts";
15
- export { describeWorkflowEngine, engineCases, engineFixture, Flake, probeBody, ProbeInput, } from "./conformance/workflow-engine.ts";
16
- export type { EngineCase, EngineCaseContext, EngineFixture, EnginePrimitives, EngineRun, Scenario, WorkflowEngineDriver, } from "./conformance/workflow-engine.ts";
17
5
  export { CleanupHooks, TurnHooks } from "../runtime/turn/hooks.ts";
18
6
  export { TurnPoolSettings } from "../runtime/turn/pipeline.ts";
19
7
  export type { TurnPoint } from "../runtime/turn/hooks.ts";
@@ -1,12 +1,6 @@
1
1
  export { ActorTest, cleanup, sweepContent } from "./actor-test.js";
2
- export { ActorCluster } from "./cluster.js";
3
2
  export { checkBatchLaw } from "./property.js";
4
3
  export { disposableDatabase, testDatabase } from "./database.js";
5
- export { SIMULATION_SEEDS, simulationSeeds } from "./simulate.js";
6
- export { CLUSTER_SIMULATION_SEEDS, clusterSimulationSeeds, FAILOVER_SIMULATION_SEEDS, failoverSimulationSeeds, NIGHTLY_CLUSTER_SIMULATION_SEEDS, NIGHTLY_FAILOVER_SIMULATION_SEEDS, } from "./simulate-cluster.js";
7
- export { conformance, describeConformance } from "./conformance.js";
8
- export { edgeKey } from "./conformance/assertions.js";
9
- export { describeWorkflowEngine, engineCases, engineFixture, Flake, probeBody, ProbeInput, } from "./conformance/workflow-engine.js";
10
4
  export { CleanupHooks, TurnHooks } from "../runtime/turn/hooks.js";
11
5
  export { TurnPoolSettings } from "../runtime/turn/pipeline.js";
12
6
  export { InternalActors } from "../runtime/actors.js";
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@rikalabs/akter","version":"0.1.0-alpha.0","files":["dist","CHANGELOG.md","README.md","LICENSE","NOTICE"],"dependencies":{"@effect/platform-node-shared":"4.0.0","@electric-sql/pglite":"0.5.8","@noble/hashes":"2.4.0","@node-rs/xxhash":"1.7.8","koffi":"3.3.2"},"peerDependencies":{"@effect/platform-bun":"4.0.0","@effect/platform-node":"4.0.0","@effect/sql-pg":"4.0.0","@effect/sql-pglite":"4.0.0","drizzle-orm":"1.0.0-rc.5-5935859","effect":"4.0.0"},"publishConfig":{"access":"public"},"description":"The framework for durable, stateful backends that power realtime apps, background work, and agents.","keywords":["actor-model","actors","agents","background-jobs","bun","durable","effect","effect-ts","pglite","postgres","realtime","stateful","transactions","workflows"],"homepage":"https://github.com/Rika-Labs/akter#readme","bugs":"https://github.com/Rika-Labs/akter/issues","license":"Apache-2.0","author":"Rika Labs","repository":{"type":"git","url":"git+https://github.com/Rika-Labs/akter.git","directory":"packages/akter"},"type":"module","types":"./dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"./runtime":{"types":"./dist/runtime/index.d.ts","default":"./dist/runtime/index.js"},"./client":{"types":"./dist/client/index.d.ts","default":"./dist/client/index.js"},"./testing":{"types":"./dist/testing/index.d.ts","default":"./dist/testing/index.js"},"./package.json":"./package.json"},"peerDependenciesMeta":{"@effect/platform-bun":{"optional":true},"@effect/platform-node":{"optional":true}},"engines":{"bun":">=1.4.2","node":">=24.0.0"}}
1
+ {"name":"@rikalabs/akter","version":"0.1.0-alpha.3","files":["dist","CHANGELOG.md","README.md","LICENSE","NOTICE"],"dependencies":{"@effect/platform-node-shared":"4.0.2","@electric-sql/pglite":"0.5.8","@noble/hashes":"2.4.0","@node-rs/xxhash":"1.7.8","koffi":"3.3.2"},"peerDependencies":{"@effect/platform-bun":"^4.0.2","@effect/platform-node":"^4.0.2","@effect/sql-pg":"^4.0.2","@effect/sql-pglite":"^4.0.2","drizzle-orm":"^1.0.0-rc.5-5935859","effect":"^4.0.2"},"publishConfig":{"access":"public"},"description":"The framework for durable, stateful backends that power realtime apps, background work, and agents.","keywords":["actor-model","actors","agents","background-jobs","bun","durable","effect","effect-ts","pglite","postgres","realtime","stateful","transactions","workflows"],"homepage":"https://github.com/Rika-Labs/akter#readme","bugs":"https://github.com/Rika-Labs/akter/issues","license":"Apache-2.0","author":"Rika Labs","repository":{"type":"git","url":"git+https://github.com/Rika-Labs/akter.git","directory":"packages/akter"},"type":"module","types":"./dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","default":"./dist/index.js"},"./runtime":{"types":"./dist/runtime/index.d.ts","default":"./dist/runtime/index.js"},"./client":{"types":"./dist/client/index.d.ts","default":"./dist/client/index.js"},"./testing":{"types":"./dist/testing/index.d.ts","default":"./dist/testing/index.js"},"./package.json":"./package.json"},"peerDependenciesMeta":{"@effect/platform-bun":{"optional":true},"@effect/platform-node":{"optional":true}},"engines":{"bun":">=1.4.2","node":">=24.0.0"}}
@@ -1,31 +0,0 @@
1
- import { Context, Effect } from "effect";
2
- import { Migrator, SqlClient } from "effect/sql";
3
- import type { SqlConnection, SqlError } from "effect/sql";
4
- import type { Scope } from "effect";
5
- import { ShardingConfig } from "effect/cluster";
6
- /** Holds startup coordination before even the history table is created, on one leased session. */
7
- export declare const withMigrationCoordination: <A, E>(effect: Effect.Effect<A, E, SqlClient.SqlClient | Scope.Scope>) => Effect.Effect<A, E | SqlError.SqlError, SqlClient.SqlClient>;
8
- /** Propagation must finish before a later statement uses the changed schema. */
9
- export declare const MigrationBarrier: Context.Reference<(connection: SqlConnection.Connection) => Effect.Effect<unknown, SqlError.SqlError>>;
10
- /** A reserved session owns coordination across autocommit DDL and progress writes. */
11
- export declare const MigrationCoordination: Context.Reference<(connection: SqlConnection.Connection) => Effect.Effect<unknown, SqlError.SqlError, Scope.Scope>>;
12
- /** Failure injection runs after a durable boundary, never in place of the database operation. */
13
- export declare const MigrationBoundary: Context.Reference<(point: string) => Effect.Effect<void>>;
14
- /** A preflight is already satisfied when this migration has started changing its schema. */
15
- export declare const MigrationResuming: Context.Reference<boolean>;
16
- /**
17
- * Cluster creates its tables outside migration history. Creating its default schema
18
- * under startup coordination first prevents concurrent CREATE races without lending
19
- * Cluster a migration-scoped SQL client or consuming a third off-turn connection.
20
- */
21
- export declare const prepareRunnerStorage: Effect.Effect<void, SqlError.SqlError, ShardingConfig.ShardingConfig | SqlClient.SqlClient>;
22
- /**
23
- * DDL and its propagation run without BEGIN. A durable statement journal closes the
24
- * crash gap between applying DDL and recording it; replay touches only an unfinished
25
- * step, so a later drop/recreate cannot make an earlier create run again. Existing
26
- * data rewrites set fixed values and are replayable, never increments or external work.
27
- */
28
- export declare const nekiMigrator: ({ record, refuseSkipped, }: {
29
- readonly record: Record<string, Effect.Effect<void, never, SqlClient.SqlClient>>;
30
- readonly refuseSkipped: Effect.Effect<void, Migrator.MigrationError | SqlError.SqlError, SqlClient.SqlClient>;
31
- }) => Effect.Effect<(readonly [number, string])[], Migrator.MigrationError | SqlError.SqlError, SqlClient.SqlClient>;
@@ -1,276 +0,0 @@
1
- import { PgClient } from "@effect/sql-pg";
2
- import { Context, Effect } from "effect";
3
- import { Migrator, SqlClient } from "effect/sql";
4
- import { Reactivity } from "effect/reactivity";
5
- import { ShardingConfig } from "effect/cluster";
6
- import { NekiTurnSessions } from "./session.js";
7
- /** Holds startup coordination before even the history table is created, on one leased session. */
8
- export const withMigrationCoordination = (effect) => Effect.scoped(Effect.gen(function* () {
9
- const source = yield* SqlClient.SqlClient;
10
- const connection = yield* source.reserve;
11
- yield* (yield* MigrationCoordination)(connection);
12
- const reactivity = yield* Reactivity.make;
13
- const client = yield* SqlClient.make({
14
- acquirer: Effect.succeed(connection),
15
- compiler: PgClient.makeCompiler(),
16
- spanAttributes: [],
17
- }).pipe(Effect.provideService(Reactivity.Reactivity, reactivity));
18
- return yield* Effect.provideService(effect, SqlClient.SqlClient, client);
19
- }));
20
- /** Propagation must finish before a later statement uses the changed schema. */
21
- export const MigrationBarrier = Context.Reference("akter/MigrationBarrier", {
22
- defaultValue: () => (connection) => connection.execute("SELECT __neki.wait_for_ddl()", [], undefined),
23
- });
24
- /** A reserved session owns coordination across autocommit DDL and progress writes. */
25
- export const MigrationCoordination = Context.Reference("akter/MigrationCoordination", {
26
- defaultValue: () => (connection) => Effect.acquireRelease(connection.execute("SELECT pg_advisory_lock(1935764837, 487)", [], undefined), () => connection
27
- .execute("SELECT pg_advisory_unlock(1935764837, 487)", [], undefined)
28
- .pipe(Effect.orDie)),
29
- });
30
- /** Failure injection runs after a durable boundary, never in place of the database operation. */
31
- export const MigrationBoundary = Context.Reference("akter/MigrationBoundary", { defaultValue: () => () => Effect.void });
32
- /** A preflight is already satisfied when this migration has started changing its schema. */
33
- export const MigrationResuming = Context.Reference("akter/MigrationResuming", {
34
- defaultValue: () => false,
35
- });
36
- /**
37
- * Cluster creates its tables outside migration history. Creating its default schema
38
- * under startup coordination first prevents concurrent CREATE races without lending
39
- * Cluster a migration-scoped SQL client or consuming a third off-turn connection.
40
- */
41
- export const prepareRunnerStorage = Effect.gen(function* () {
42
- const config = yield* ShardingConfig.ShardingConfig;
43
- const neki = yield* NekiTurnSessions;
44
- return yield* withMigrationCoordination(Effect.gen(function* () {
45
- const sql = yield* SqlClient.SqlClient;
46
- const connection = yield* sql.reserve;
47
- const boundary = yield* MigrationBoundary;
48
- const barrier = yield* MigrationBarrier;
49
- if (neki)
50
- yield* barrier(connection);
51
- yield* sql `CREATE TABLE IF NOT EXISTS cluster_runners (
52
- machine_id SERIAL PRIMARY KEY,
53
- address VARCHAR(255) NOT NULL,
54
- runner TEXT NOT NULL,
55
- healthy BOOLEAN NOT NULL DEFAULT TRUE,
56
- last_heartbeat TIMESTAMP NOT NULL DEFAULT NOW(),
57
- UNIQUE(address)
58
- )`;
59
- yield* boundary("cluster:runners:ddl");
60
- if (neki)
61
- yield* barrier(connection);
62
- yield* boundary("cluster:runners:propagated");
63
- if (config.shardLockDisableAdvisory) {
64
- yield* sql `CREATE TABLE IF NOT EXISTS cluster_locks (
65
- shard_id VARCHAR(50) PRIMARY KEY,
66
- address VARCHAR(255) NOT NULL,
67
- acquired_at TIMESTAMP NOT NULL
68
- )`;
69
- yield* boundary("cluster:locks:ddl");
70
- if (neki)
71
- yield* barrier(connection);
72
- yield* boundary("cluster:locks:propagated");
73
- }
74
- }));
75
- });
76
- /** ALTER actions split only at commas outside quoted identifiers, literals and expressions. */
77
- const alterActions = (text) => {
78
- const actions = [];
79
- let start = 0;
80
- let depth = 0;
81
- let quote = "";
82
- for (let i = 0; i < text.length; i++) {
83
- const character = text[i];
84
- if (quote !== "") {
85
- if (character === quote) {
86
- if (text[i + 1] === quote)
87
- i++;
88
- else
89
- quote = "";
90
- }
91
- }
92
- else if (character === "'" || character === '"')
93
- quote = character;
94
- else if (character === "(")
95
- depth++;
96
- else if (character === ")")
97
- depth--;
98
- else if (character === "," && depth === 0) {
99
- actions.push(text.slice(start, i).trim());
100
- start = i + 1;
101
- }
102
- }
103
- actions.push(text.slice(start).trim());
104
- return actions;
105
- };
106
- const identifier = '(?:"(?:[^"]|"")+"|[a-z_][a-z_0-9]*)(?:\\.(?:"(?:[^"]|"")+"|[a-z_][a-z_0-9]*))?';
107
- const alter = new RegExp(`^ALTER TABLE (${identifier})\\s+([\\s\\S]+)$`, "i");
108
- const unquote = (name) => name.replace(/^"|"$/g, "").replaceAll('""', '"');
109
- /**
110
- * Resumption rewrites only the framework's audited DDL forms; an unknown form fails closed.
111
- * A statement that already carries its own existence guard is replayed unchanged.
112
- */
113
- const replayDdl = (sql, text) => Effect.gen(function* () {
114
- if (/^CREATE (?:UNIQUE )?(?:TABLE|INDEX|SCHEMA) /i.test(text))
115
- return text.replace(/^(CREATE (?:UNIQUE )?(?:TABLE|INDEX|SCHEMA)) (?!IF NOT EXISTS )/i, "$1 IF NOT EXISTS ");
116
- if (/^DROP (?:TABLE|INDEX|VIEW) /i.test(text))
117
- return text.replace(/^(DROP (?:TABLE|INDEX|VIEW)) (?!IF EXISTS )/i, "$1 IF EXISTS ");
118
- if (/^CREATE (?:OR REPLACE )?(?:VIEW|FUNCTION) /i.test(text))
119
- return text.replace(/^CREATE (?:OR REPLACE )?/i, "CREATE OR REPLACE ");
120
- const policy = new RegExp(`^CREATE POLICY (${identifier}) ON (${identifier})`, "i").exec(text);
121
- if (policy !== null) {
122
- const [row] = yield* sql `SELECT EXISTS (
123
- SELECT 1 FROM pg_policy WHERE polrelid = to_regclass(${policy[2]})
124
- AND polname = ${unquote(policy[1])}) AS exists`;
125
- return row?.exists === true ? undefined : text;
126
- }
127
- const match = alter.exec(text);
128
- if (match !== null) {
129
- const table = match[1];
130
- const action = match[2];
131
- if (/^ADD COLUMN /i.test(action))
132
- return `ALTER TABLE ${table} ${action.replace(/^ADD COLUMN (?!IF NOT EXISTS )/i, "ADD COLUMN IF NOT EXISTS ")}`;
133
- if (/^DROP CONSTRAINT /i.test(action))
134
- return `ALTER TABLE ${table} ${action.replace(/^DROP CONSTRAINT (?!IF EXISTS )/i, "DROP CONSTRAINT IF EXISTS ")}`;
135
- if (/^ENABLE ROW LEVEL SECURITY$/i.test(action))
136
- return text;
137
- const constraint = new RegExp(`^ADD CONSTRAINT (${identifier})\\s+`, "i").exec(action);
138
- if (constraint !== null) {
139
- const [row] = yield* sql `SELECT EXISTS (
140
- SELECT 1 FROM pg_constraint WHERE conrelid = to_regclass(${table})
141
- AND conname = ${unquote(constraint[1])}) AS exists`;
142
- return row?.exists === true ? undefined : text;
143
- }
144
- const rename = new RegExp(`^RENAME COLUMN (${identifier}) TO (${identifier})$`, "i").exec(action);
145
- if (rename !== null) {
146
- const rows = yield* sql `SELECT attname AS name FROM pg_attribute
147
- WHERE attrelid = to_regclass(${table}) AND NOT attisdropped
148
- AND attname IN (${unquote(rename[1])}, ${unquote(rename[2])})`;
149
- if (rows.some(({ name }) => name === unquote(rename[1])))
150
- return text;
151
- if (rows.some(({ name }) => name === unquote(rename[2])))
152
- return undefined;
153
- }
154
- }
155
- return yield* new Migrator.MigrationError({
156
- kind: "BadState",
157
- message: `No replay rule for migration DDL: ${text}`,
158
- });
159
- });
160
- /**
161
- * DDL and its propagation run without BEGIN. A durable statement journal closes the
162
- * crash gap between applying DDL and recording it; replay touches only an unfinished
163
- * step, so a later drop/recreate cannot make an earlier create run again. Existing
164
- * data rewrites set fixed values and are replayable, never increments or external work.
165
- */
166
- export const nekiMigrator = ({ record, refuseSkipped, }) => Effect.scoped(Effect.gen(function* () {
167
- const source = yield* SqlClient.SqlClient;
168
- const connection = yield* source.reserve;
169
- const barrier = yield* MigrationBarrier;
170
- const boundary = yield* MigrationBoundary;
171
- const reactivity = yield* Reactivity.make;
172
- const direct = yield* SqlClient.make({
173
- acquirer: Effect.succeed(connection),
174
- compiler: PgClient.makeCompiler(),
175
- spanAttributes: [],
176
- }).pipe(Effect.provideService(Reactivity.Reactivity, reactivity));
177
- for (const [name, ddl] of [
178
- [
179
- "history",
180
- "CREATE TABLE IF NOT EXISTS actor_migrations (migration_id integer PRIMARY KEY, created_at timestamptz NOT NULL DEFAULT now(), name text NOT NULL)",
181
- ],
182
- [
183
- "journal",
184
- "CREATE TABLE IF NOT EXISTS actor_migration_steps (migration_id integer NOT NULL, step integer NOT NULL, statement text NOT NULL, completed boolean NOT NULL DEFAULT false, PRIMARY KEY (migration_id, step))",
185
- ],
186
- ]) {
187
- yield* barrier(connection);
188
- yield* direct.unsafe(ddl);
189
- yield* boundary(`bootstrap:${name}:ddl`);
190
- yield* barrier(connection);
191
- yield* boundary(`bootstrap:${name}:propagated`);
192
- }
193
- yield* Effect.provideService(refuseSkipped, SqlClient.SqlClient, direct);
194
- const rows = yield* direct `SELECT migration_id::int AS id FROM actor_migrations`;
195
- const latest = Math.max(0, ...rows.map(({ id }) => id));
196
- const loaded = yield* Migrator.fromRecord(record);
197
- if (new Set(loaded.map(([id]) => id)).size !== loaded.length)
198
- return yield* new Migrator.MigrationError({
199
- kind: "Duplicates",
200
- message: "Found duplicate migration ids",
201
- });
202
- const applied = [];
203
- for (const [id, name] of loaded) {
204
- if (id <= latest)
205
- continue;
206
- const [progress] = yield* direct `SELECT EXISTS (
207
- SELECT 1 FROM actor_migration_steps WHERE migration_id = ${id}) AS started`;
208
- let step = 0;
209
- const execute = (text, parameters, transform) => Effect.gen(function* () {
210
- if (/^SELECT\b/i.test(text.trim()))
211
- return yield* connection.execute(text, parameters, transform);
212
- const mutation = text.trim();
213
- const ddl = /^(CREATE|ALTER|DROP)\b/i.test(mutation);
214
- if (!ddl && !(id === 26 && /^UPDATE\b/i.test(mutation)))
215
- return yield* Effect.die(new Migrator.MigrationError({
216
- kind: "BadState",
217
- message: `Migration ${id} has no replayable data step: ${mutation}`,
218
- }));
219
- const alteration = alter.exec(mutation);
220
- const statements = alteration !== null
221
- ? alterActions(alteration[2]).map((action) => `ALTER TABLE ${alteration[1]} ${action}`)
222
- : [mutation];
223
- for (const statement of statements) {
224
- const position = ++step;
225
- const point = `${id}:${position}`;
226
- const [saved] = yield* direct `SELECT statement, completed
227
- FROM actor_migration_steps WHERE migration_id = ${id} AND step = ${position}`;
228
- if (saved !== undefined && saved.statement !== statement)
229
- return yield* Effect.die(new Migrator.MigrationError({
230
- kind: "BadState",
231
- message: `Migration ${point} changed after it started`,
232
- }));
233
- if (saved?.completed === true)
234
- continue;
235
- if (saved === undefined) {
236
- yield* direct `INSERT INTO actor_migration_steps (migration_id, step, statement) VALUES (${id}, ${position}, ${statement})`;
237
- yield* boundary(`${point}:pending`);
238
- }
239
- if (ddl)
240
- yield* barrier(connection);
241
- const replay = ddl
242
- ? yield* replayDdl(direct, statement).pipe(Effect.orDie)
243
- : statement;
244
- if (replay !== undefined)
245
- yield* connection.execute(replay, parameters, transform);
246
- yield* boundary(`${point}:applied`);
247
- if (ddl) {
248
- yield* barrier(connection);
249
- yield* boundary(`${point}:propagated`);
250
- }
251
- yield* direct `UPDATE actor_migration_steps SET completed = true WHERE migration_id = ${id} AND step = ${position}`;
252
- yield* boundary(`${point}:completed`);
253
- }
254
- return [];
255
- });
256
- const client = yield* SqlClient.make({
257
- acquirer: Effect.succeed({ ...connection, execute }),
258
- compiler: PgClient.makeCompiler(),
259
- spanAttributes: [],
260
- }).pipe(Effect.provideService(Reactivity.Reactivity, reactivity));
261
- yield* record[Object.keys(record).find((key) => Number(key.split("_")[0]) === id)].pipe(Effect.provideService(SqlClient.SqlClient, client), Effect.provideService(MigrationResuming, progress?.started === true));
262
- const [removed] = yield* direct `SELECT EXISTS (
263
- SELECT 1 FROM actor_migration_steps WHERE migration_id = ${id} AND step > ${step}
264
- ) AS exists`;
265
- if (removed?.exists === true)
266
- return yield* new Migrator.MigrationError({
267
- kind: "BadState",
268
- message: `Migration ${id} removed steps after it started`,
269
- });
270
- yield* direct `INSERT INTO actor_migrations (migration_id, name) VALUES (${id}, ${name})`;
271
- yield* boundary(`${id}:recorded`);
272
- applied.push([id, name]);
273
- }
274
- yield* Effect.provideService(refuseSkipped, SqlClient.SqlClient, direct);
275
- return applied;
276
- }));
@@ -1,23 +0,0 @@
1
- import type { PgConnection, PgPool } from "@effect/sql-pg";
2
- import { Context, Effect } from "effect";
3
- import type { Scope } from "effect";
4
- import type { SqlError } from "effect/sql";
5
- /**
6
- * Whether the database is a Neki router. Turn sessions then run
7
- * `NEKI_SESSION_SETTINGS`; nothing sets this outside the Neki conformance
8
- * backend until Neki is a supported database.
9
- */
10
- export declare const NekiTurnSessions: Context.Reference<boolean>;
11
- /**
12
- * Session settings a turn connection needs on Neki, in the order they are set.
13
- * The router reads the transaction mode when `BEGIN` arrives, so both are set
14
- * on the session before any turn opens a transaction, never inside one.
15
- */
16
- export declare const NEKI_SESSION_SETTINGS: ReadonlyArray<string>;
17
- /**
18
- * `pool.get` with `NEKI_SESSION_SETTINGS` run once on each session, on its
19
- * first lease, so a turn that reaches a second shard fails instead of
20
- * committing on one shard and not the other. A session whose settings fail is
21
- * dropped from the pool: a turn must never run on one that lacks them.
22
- */
23
- export declare const nekiLease: (pool: PgPool.PgPool) => Effect.Effect<PgConnection.PgConnection, SqlError.SqlError, Scope.Scope>;
@@ -1,31 +0,0 @@
1
- import { Context, Effect } from "effect";
2
- /**
3
- * Whether the database is a Neki router. Turn sessions then run
4
- * `NEKI_SESSION_SETTINGS`; nothing sets this outside the Neki conformance
5
- * backend until Neki is a supported database.
6
- */
7
- export const NekiTurnSessions = Context.Reference("akter/NekiTurnSessions", {
8
- defaultValue: () => false,
9
- });
10
- /**
11
- * Session settings a turn connection needs on Neki, in the order they are set.
12
- * The router reads the transaction mode when `BEGIN` arrives, so both are set
13
- * on the session before any turn opens a transaction, never inside one.
14
- */
15
- export const NEKI_SESSION_SETTINGS = [
16
- "SET __neki.tx_mode = 'single'",
17
- "SET __neki.fanout = 'single'",
18
- ];
19
- /**
20
- * `pool.get` with `NEKI_SESSION_SETTINGS` run once on each session, on its
21
- * first lease, so a turn that reaches a second shard fails instead of
22
- * committing on one shard and not the other. A session whose settings fail is
23
- * dropped from the pool: a turn must never run on one that lacks them.
24
- */
25
- export const nekiLease = (pool) => {
26
- const configured = new WeakSet();
27
- const configure = (connection) => Effect.forEach(NEKI_SESSION_SETTINGS, (setting) => connection.query(setting, [], false), {
28
- discard: true,
29
- }).pipe(Effect.tap(() => Effect.sync(() => configured.add(connection))), Effect.tapCause(() => pool.invalidate(connection)));
30
- return Effect.tap(pool.get, (connection) => configured.has(connection) ? Effect.void : configure(connection));
31
- };