@voltro/workflow 0.86.0 → 0.88.0

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 (99) hide show
  1. package/CHANGELOG.md +5484 -0
  2. package/THIRD-PARTY-NOTICES.md +3 -227
  3. package/dist/admission.js +1 -2
  4. package/dist/admissionDrainer.js +5 -3
  5. package/dist/admissionGate.js +1 -2
  6. package/dist/admissionStore.js +1 -2
  7. package/dist/admissionSubmission.js +1 -2
  8. package/dist/admissionTables.js +4 -4
  9. package/dist/admissionTransaction.js +1 -2
  10. package/dist/awaitEvent.js +1 -2
  11. package/dist/budgetHold.js +1 -2
  12. package/dist/cancelOnPlan.js +1 -2
  13. package/dist/cancelOnSweep.js +1 -2
  14. package/dist/cancelPendingStart.js +1 -2
  15. package/dist/cluster.d.ts +111 -27
  16. package/dist/cluster.js +12 -8
  17. package/dist/clusterCron.js +18 -11
  18. package/dist/clusterDiagnostics.js +1 -2
  19. package/dist/clusterDiagnosticsSql.js +1 -2
  20. package/dist/clusterLayer.js +105 -71
  21. package/dist/clusterMigrationGate.js +1 -2
  22. package/dist/clusterRunnerLayer.js +27 -18
  23. package/dist/clusterShardGroups.js +32 -0
  24. package/dist/clusterTestSuite.d.ts +45 -2
  25. package/dist/clusterTestSuite.js +225 -197
  26. package/dist/controlMonitor.js +1 -2
  27. package/dist/controlRequest.js +1 -2
  28. package/dist/executionRefusal.js +1 -2
  29. package/dist/failureHandoff.js +1 -2
  30. package/dist/failureMessage.js +1 -2
  31. package/dist/failureRecovery.js +1 -2
  32. package/dist/failureReportStore.js +1 -2
  33. package/dist/flowControl.js +1 -2
  34. package/dist/forwardStartReceipts.js +1 -2
  35. package/dist/inMemoryLayer.js +1 -2
  36. package/dist/inMemoryRecorder.js +1 -2
  37. package/dist/index.d.ts +50 -23
  38. package/dist/index.js +24 -23
  39. package/dist/inspectWorkflow.js +2 -3
  40. package/dist/journalEncryption.js +1 -2
  41. package/dist/journalRoutingGuard.js +49 -0
  42. package/dist/journalRoutingLayout.js +46 -0
  43. package/dist/journalRoutingLock.js +38 -0
  44. package/dist/journalRoutingMaintenance.js +23 -0
  45. package/dist/journalRoutingMessageStorage.js +31 -0
  46. package/dist/journalRoutingNamespace.js +22 -0
  47. package/dist/journalRoutingRuntime.js +32 -0
  48. package/dist/journalRoutingSharding.js +27 -0
  49. package/dist/journalRoutingState.js +92 -0
  50. package/dist/journalRoutingTable.js +10 -0
  51. package/dist/journalRoutingTransaction.js +15 -0
  52. package/dist/messages.js +1 -2
  53. package/dist/nondeterminism.js +1 -2
  54. package/dist/parentClosure.js +1 -2
  55. package/dist/parentClosureRecovery.js +1 -2
  56. package/dist/pendingSubmission.js +1 -2
  57. package/dist/prepareSubmissionDispatch.js +1 -2
  58. package/dist/preparedExecution.js +1 -2
  59. package/dist/primitives.js +1 -2
  60. package/dist/queuedCancellationRequest.js +1 -2
  61. package/dist/queuedControl.js +1 -2
  62. package/dist/recorder.js +1 -2
  63. package/dist/residentExecutionEvidence.js +1 -2
  64. package/dist/retryPolicy.js +1 -2
  65. package/dist/runEventsTable.js +1 -2
  66. package/dist/runGeneration.js +1 -2
  67. package/dist/runRecording.js +1 -2
  68. package/dist/runStepsTable.js +1 -2
  69. package/dist/runnerStorageGuard.js +1 -2
  70. package/dist/runsTable.js +1 -2
  71. package/dist/scheduleDeclaration.js +1 -2
  72. package/dist/signalHint.js +1 -2
  73. package/dist/signals.js +1 -2
  74. package/dist/signalsSuspending.js +1 -2
  75. package/dist/sqliteWorkflowOwnership.js +1 -2
  76. package/dist/stalenessSweep.js +1 -2
  77. package/dist/startContextsTable.js +1 -2
  78. package/dist/startReceipt.js +1 -2
  79. package/dist/submissionRecovery.js +1 -2
  80. package/dist/submissionRejection.js +1 -2
  81. package/dist/submissionRetry.js +1 -2
  82. package/dist/suspensionUnavailable.js +1 -2
  83. package/dist/updates.js +1 -2
  84. package/dist/watermarkTable.js +1 -2
  85. package/dist/webCrypto.js +4 -17
  86. package/dist/workflowDelivery.js +1 -2
  87. package/dist/workflowExecutorRouting.js +32 -0
  88. package/dist/workflowJournal.js +1 -2
  89. package/dist/workflowJournalReset.js +1 -2
  90. package/dist/workflowReadSelections.js +1 -2
  91. package/dist/workflowRestartDispatch.js +30 -25
  92. package/dist/workflowRestartObservation.js +1 -2
  93. package/dist/workflowRestartPreparation.js +1 -2
  94. package/dist/workflowRestartReservation.js +158 -157
  95. package/dist/workflowResumeDispatch.js +18 -18
  96. package/dist/workflowRuntimeReadiness.js +11 -0
  97. package/dist/workflowShardRelocation.js +57 -0
  98. package/dist/workflowShardRelocationSql.js +71 -0
  99. package/package.json +5 -11
@@ -1,50 +1,52 @@
1
1
  import { resolveAdmissionKeys as e } from "./admission.js";
2
2
  import { decodePendingPayload as t, evaluateAdmission as n, recordAdmission as r } from "./admissionStore.js";
3
- import { resetExpectedWorkflowReplies as i } from "./workflowJournalReset.js";
4
- import { Effect as a, Schema as o } from "effect";
5
- import { WorkflowRestartObservationSchema as s, WorkflowRestartSelectionSchema as c, WorkflowRestartTargetSchema as l } from "@voltro/protocol";
6
- import { and as u, derivedRowId as d, eq as f, id as p, integer as m, json as h, or as g, table as _, text as v, timestamp as y } from "@voltro/database";
7
- //#region src/workflowRestartReservation.ts
8
- var b = _("_voltro_workflow_restart_requests", {
9
- id: p({ prefix: "wfrs" }),
10
- runId: v(),
11
- workflowName: v(),
12
- executionId: v(),
13
- generation: v(),
14
- token: v(),
15
- ledgerId: v(),
16
- tenantId: v().nullable(),
17
- delivery: h(),
18
- replies: h(),
19
- selection: h().nullable(),
20
- eviction: h().nullable(),
21
- requestedAt: y(),
22
- dispatchAttempts: m().default(0),
23
- enteredGeneration: v().nullable()
24
- }).unique(["runId", "generation"]).unique(["runId", "token"]).nonReactive(), x = o.NonEmptyString, S = o.Struct({
25
- requestId: x,
26
- restartToken: o.NullOr(x)
27
- }), C = o.Struct({
28
- requestId: x,
29
- replyId: x
30
- }), w = c, T = _("_voltro_workflow_restart_withdrawals", {
31
- id: p({ prefix: "wfrw" }),
32
- runId: v(),
33
- workflowName: v(),
34
- tenantId: v().nullable(),
35
- selection: h(),
36
- withdrawnAt: y()
37
- }).nonReactive(), E = o.Struct({
38
- id: o.NonEmptyString,
39
- runId: o.NonEmptyString,
40
- workflowName: o.NonEmptyString,
41
- tenantId: o.NullOr(o.NonEmptyString),
42
- selection: w,
43
- withdrawnAt: o.Date
44
- }), D = async (e, t) => {
45
- let n = o.decodeUnknownSync(l)(t), [r] = await e.query({
46
- table: T.tableName,
47
- predicate: f("id", n.requestId),
3
+ import { ownedJournalRoutingPromise as i } from "./journalRoutingGuard.js";
4
+ import { resetExpectedWorkflowReplies as a } from "./workflowJournalReset.js";
5
+ import { Effect as o, Schema as s } from "effect";
6
+ import { WorkflowRestartObservationSchema as c, WorkflowRestartSelectionSchema as l, WorkflowRestartTargetSchema as u } from "@voltro/protocol";
7
+ import { and as d, derivedRowId as f, eq as p, id as m, integer as h, json as g, or as _, table as v, text as y, timestamp as b } from "@voltro/database";
8
+ // src/workflowRestartReservation.ts
9
+ var x = v("_voltro_workflow_restart_requests", {
10
+ id: m({ prefix: "wfrs" }),
11
+ runId: y(),
12
+ workflowName: y(),
13
+ executionId: y(),
14
+ generation: y(),
15
+ token: y(),
16
+ ledgerId: y(),
17
+ tenantId: y().nullable(),
18
+ delivery: g(),
19
+ replies: g(),
20
+ selection: g().nullable(),
21
+ eviction: g().nullable(),
22
+ requestedAt: b(),
23
+ dispatchAttempts: h().default(0),
24
+ enteredGeneration: y().nullable(),
25
+ poolReleaseSettledAt: b().nullable()
26
+ }).unique(["runId", "generation"]).unique(["runId", "token"]).index("_voltro_workflow_restart_requests_pool_settlement_idx", ["poolReleaseSettledAt", "id"]).nonReactive(), S = s.NonEmptyString, C = s.Struct({
27
+ requestId: S,
28
+ restartToken: s.NullOr(S)
29
+ }), w = s.Struct({
30
+ requestId: S,
31
+ replyId: S
32
+ }), T = l, E = v("_voltro_workflow_restart_withdrawals", {
33
+ id: m({ prefix: "wfrw" }),
34
+ runId: y(),
35
+ workflowName: y(),
36
+ tenantId: y().nullable(),
37
+ selection: g(),
38
+ withdrawnAt: b()
39
+ }).nonReactive(), D = s.Struct({
40
+ id: s.NonEmptyString,
41
+ runId: s.NonEmptyString,
42
+ workflowName: s.NonEmptyString,
43
+ tenantId: s.NullOr(s.NonEmptyString),
44
+ selection: T,
45
+ withdrawnAt: s.Date
46
+ }), O = async (e, t) => {
47
+ let n = s.decodeUnknownSync(u)(t), [r] = await e.query({
48
+ table: E.tableName,
49
+ predicate: p("id", n.requestId),
48
50
  lock: "update",
49
51
  order: [],
50
52
  take: 1,
@@ -52,35 +54,35 @@ var b = _("_voltro_workflow_restart_requests", {
52
54
  projection: void 0
53
55
  });
54
56
  if (r === void 0) return;
55
- let i = o.decodeUnknownSync(E)(r);
57
+ let i = s.decodeUnknownSync(D)(r);
56
58
  if (i.runId !== n.runId) throw Error("Restart identity belongs to a different command");
57
59
  if (!Number.isFinite(i.withdrawnAt.getTime())) throw Error("Invalid restart withdrawal timestamp");
58
60
  return i;
59
- }, O = async (e, t) => {
60
- if (await D(e, t) !== void 0) throw Error("Restart request was permanently withdrawn");
61
- }, k = o.Struct({
62
- id: x,
63
- runId: x,
64
- workflowName: x,
65
- executionId: x,
66
- generation: x,
67
- token: x,
68
- ledgerId: x,
69
- tenantId: o.NullOr(x),
70
- delivery: S,
71
- replies: o.Array(C),
72
- selection: w,
73
- eviction: o.NullOr(o.Struct({
74
- executionId: x,
75
- runId: o.String
61
+ }, k = async (e, t) => {
62
+ if (await O(e, t) !== void 0) throw Error("Restart request was permanently withdrawn");
63
+ }, A = s.Struct({
64
+ id: S,
65
+ runId: S,
66
+ workflowName: S,
67
+ executionId: S,
68
+ generation: S,
69
+ token: S,
70
+ ledgerId: S,
71
+ tenantId: s.NullOr(S),
72
+ delivery: C,
73
+ replies: s.Array(w),
74
+ selection: T,
75
+ eviction: s.NullOr(s.Struct({
76
+ executionId: S,
77
+ runId: s.String
76
78
  })),
77
- requestedAt: o.Date,
78
- dispatchAttempts: o.Number.check(o.isInt(), o.isGreaterThanOrEqualTo(0)),
79
- enteredGeneration: o.NullOr(x)
80
- }), A = (e, t) => e.kind === t.kind && (e.kind === "failed-steps" || t.kind === "from-step" && e.stepName === t.stepName), j = async (e, t) => {
81
- let n = o.decodeUnknownSync(l)(t), [r] = await e.query({
82
- table: b.tableName,
83
- predicate: f("id", n.requestId),
79
+ requestedAt: s.Date,
80
+ dispatchAttempts: s.Number.check(s.isInt(), s.isGreaterThanOrEqualTo(0)),
81
+ enteredGeneration: s.NullOr(S)
82
+ }), j = (e, t) => e.kind === t.kind && (e.kind === "failed-steps" || t.kind === "from-step" && e.stepName === t.stepName), M = async (e, t) => {
83
+ let n = s.decodeUnknownSync(u)(t), [r] = await e.query({
84
+ table: x.tableName,
85
+ predicate: p("id", n.requestId),
84
86
  lock: "update",
85
87
  order: [],
86
88
  take: 1,
@@ -88,20 +90,20 @@ var b = _("_voltro_workflow_restart_requests", {
88
90
  projection: void 0
89
91
  });
90
92
  if (r === void 0) return;
91
- let i = o.decodeUnknownSync(k)(r);
93
+ let i = s.decodeUnknownSync(A)(r);
92
94
  if (i.runId !== n.runId) throw Error("Restart identity belongs to a different command");
93
95
  return i;
94
- }, M = async (e, t) => {
95
- let n = await D(e, t);
96
- if (n !== void 0) return o.decodeUnknownSync(s)({
96
+ }, N = async (e, t) => {
97
+ let n = await O(e, t);
98
+ if (n !== void 0) return s.decodeUnknownSync(c)({
97
99
  ...t,
98
100
  kind: "withdrawn",
99
101
  workflowName: n.workflowName,
100
102
  selection: n.selection,
101
103
  withdrawnAt: n.withdrawnAt.getTime()
102
104
  });
103
- let r = await j(e, t);
104
- if (r === void 0) return o.decodeUnknownSync(s)({
105
+ let r = await M(e, t);
106
+ if (r === void 0) return s.decodeUnknownSync(c)({
105
107
  ...t,
106
108
  kind: "not-found"
107
109
  });
@@ -115,7 +117,7 @@ var b = _("_voltro_workflow_restart_requests", {
115
117
  requestedAt: r.requestedAt.getTime(),
116
118
  dispatchAttempts: r.dispatchAttempts
117
119
  };
118
- return o.decodeUnknownSync(s)(r.enteredGeneration === null ? {
120
+ return s.decodeUnknownSync(c)(r.enteredGeneration === null ? {
119
121
  ...i,
120
122
  kind: "pending"
121
123
  } : {
@@ -123,56 +125,56 @@ var b = _("_voltro_workflow_restart_requests", {
123
125
  kind: "entered",
124
126
  enteredGeneration: r.enteredGeneration
125
127
  });
126
- }, N = async (e, t) => {
128
+ }, P = async (e, t) => {
127
129
  for (let e of [
128
130
  t.id,
129
131
  t.runId,
130
132
  t.workflowName
131
- ]) o.decodeUnknownSync(x)(e);
133
+ ]) s.decodeUnknownSync(S)(e);
132
134
  if (t.id.length > 64) throw Error("Invalid restart request identity");
133
- o.decodeUnknownSync(o.NullOr(x))(t.tenantId);
134
- let n = o.decodeUnknownSync(w)(t.selection);
135
- await O(e, {
135
+ s.decodeUnknownSync(s.NullOr(S))(t.tenantId);
136
+ let n = s.decodeUnknownSync(T)(t.selection);
137
+ await k(e, {
136
138
  requestId: t.id,
137
139
  runId: t.runId
138
140
  });
139
- let r = await j(e, {
141
+ let r = await M(e, {
140
142
  requestId: t.id,
141
143
  runId: t.runId
142
144
  });
143
145
  if (r !== void 0) {
144
- if (r.runId !== t.runId || r.workflowName !== t.workflowName || r.tenantId !== t.tenantId || !A(r.selection, n)) throw Error("Restart identity belongs to a different command");
146
+ if (r.runId !== t.runId || r.workflowName !== t.workflowName || r.tenantId !== t.tenantId || !j(r.selection, n)) throw Error("Restart identity belongs to a different command");
145
147
  return r;
146
148
  }
147
- }, P = async (e, t) => {
148
- let n = o.decodeUnknownSync(E)({
149
+ }, F = async (e, t) => {
150
+ let n = s.decodeUnknownSync(D)({
149
151
  ...t,
150
152
  withdrawnAt: new Date(t.now)
151
153
  });
152
- if (o.decodeUnknownSync(l)({
154
+ if (s.decodeUnknownSync(u)({
153
155
  requestId: n.id,
154
156
  runId: n.runId
155
157
  }), !Number.isFinite(n.withdrawnAt.getTime())) throw Error("Invalid restart withdrawal timestamp");
156
- let r = await D(e, {
158
+ let r = await O(e, {
157
159
  requestId: n.id,
158
160
  runId: n.runId
159
161
  });
160
162
  if (r !== void 0) {
161
- if (r.workflowName !== n.workflowName || r.tenantId !== n.tenantId || !A(r.selection, n.selection)) throw Error("Restart identity belongs to a different command");
163
+ if (r.workflowName !== n.workflowName || r.tenantId !== n.tenantId || !j(r.selection, n.selection)) throw Error("Restart identity belongs to a different command");
162
164
  return {
163
165
  kind: "withdrawn",
164
166
  withdrawal: r,
165
167
  reused: !0
166
168
  };
167
169
  }
168
- let i = await N(e, n);
170
+ let i = await P(e, n);
169
171
  if (i !== void 0) return {
170
172
  kind: "reserved",
171
173
  request: i
172
174
  };
173
175
  let [a] = await e.query({
174
176
  table: "_voltro_workflow_runs",
175
- predicate: f("id", n.runId),
177
+ predicate: p("id", n.runId),
176
178
  lock: "update",
177
179
  order: [],
178
180
  take: 1,
@@ -180,49 +182,49 @@ var b = _("_voltro_workflow_restart_requests", {
180
182
  projection: void 0
181
183
  });
182
184
  if (a === void 0 || a.tag !== n.workflowName) throw Error("Restart withdrawal requires its resident workflow");
183
- if ((o.decodeUnknownSync(o.NullOr(o.Struct({ tenantId: o.NullOr(x) })))(a.subject ?? null)?.tenantId ?? null) !== n.tenantId) throw Error("Restart withdrawal belongs to another resident tenant");
184
- return await e.insert(T.tableName, n), {
185
+ if ((s.decodeUnknownSync(s.NullOr(s.Struct({ tenantId: s.NullOr(S) })))(a.subject ?? null)?.tenantId ?? null) !== n.tenantId) throw Error("Restart withdrawal belongs to another resident tenant");
186
+ return await e.insert(E.tableName, n), {
185
187
  kind: "withdrawn",
186
188
  withdrawal: n,
187
189
  reused: !1
188
190
  };
189
- }, F = async (e, t) => {
190
- o.decodeUnknownSync(x)(t.generation);
191
- let n = await N(e, t);
191
+ }, I = async (e, t) => {
192
+ s.decodeUnknownSync(S)(t.generation);
193
+ let n = await P(e, t);
192
194
  if (n !== void 0 && n.generation !== t.generation) throw Error("Restart identity belongs to a different command");
193
195
  return n;
194
- }, I = async (i, a) => {
196
+ }, L = async (i, a) => {
195
197
  for (let e of [
196
198
  a.id,
197
199
  a.runId,
198
200
  a.generation,
199
201
  a.token
200
- ]) o.decodeUnknownSync(x)(e);
202
+ ]) s.decodeUnknownSync(S)(e);
201
203
  if (a.id.length > 64 || !Number.isFinite(new Date(a.now).getTime())) throw Error("Invalid restart request identity or timestamp");
202
- await O(i, {
204
+ await k(i, {
203
205
  requestId: a.id,
204
206
  runId: a.runId
205
207
  });
206
- let s = o.decodeUnknownSync(S)(a.delivery), c = o.decodeUnknownSync(w)(a.selection);
207
- if (s.restartToken === a.token) throw Error("Restart requires a new delivery token");
208
- let l = o.decodeUnknownSync(o.Array(C))(a.replies), d = /* @__PURE__ */ new Map();
208
+ let o = s.decodeUnknownSync(C)(a.delivery), c = s.decodeUnknownSync(T)(a.selection);
209
+ if (o.restartToken === a.token) throw Error("Restart requires a new delivery token");
210
+ let l = s.decodeUnknownSync(s.Array(w))(a.replies), u = /* @__PURE__ */ new Map();
209
211
  for (let e of l) {
210
- if (!/^[1-9]\d*$/.test(e.requestId) || !/^[1-9]\d*$/.test(e.replyId) || d.has(e.requestId)) throw Error("Restart requires distinct, exact journal reply identities");
211
- d.set(e.requestId, e.replyId);
212
+ if (!/^[1-9]\d*$/.test(e.requestId) || !/^[1-9]\d*$/.test(e.replyId) || u.has(e.requestId)) throw Error("Restart requires distinct, exact journal reply identities");
213
+ u.set(e.requestId, e.replyId);
212
214
  }
213
- if (!d.has(s.requestId)) throw Error("Restart selection must include its run reply");
214
- let [p] = await i.query({
215
- table: b.tableName,
216
- predicate: f("id", a.id),
215
+ if (!u.has(o.requestId)) throw Error("Restart selection must include its run reply");
216
+ let [f] = await i.query({
217
+ table: x.tableName,
218
+ predicate: p("id", a.id),
217
219
  lock: "update",
218
220
  order: [],
219
221
  take: 1,
220
222
  skip: void 0,
221
223
  projection: void 0
222
224
  });
223
- if (p !== void 0) {
224
- let e = o.decodeUnknownSync(k)(p);
225
- if (e.runId !== a.runId || e.generation !== a.generation || e.token !== a.token || e.workflowName !== a.control.workflowName || e.tenantId !== a.tenantId || e.delivery.requestId !== s.requestId || e.delivery.restartToken !== s.restartToken || !A(e.selection, c) || e.replies.length !== d.size || e.replies.some((e) => d.get(e.requestId) !== e.replyId)) throw Error("Restart identity belongs to a different request");
225
+ if (f !== void 0) {
226
+ let e = s.decodeUnknownSync(A)(f);
227
+ if (e.runId !== a.runId || e.generation !== a.generation || e.token !== a.token || e.workflowName !== a.control.workflowName || e.tenantId !== a.tenantId || e.delivery.requestId !== o.requestId || e.delivery.restartToken !== o.restartToken || !j(e.selection, c) || e.replies.length !== u.size || e.replies.some((e) => u.get(e.requestId) !== e.replyId)) throw Error("Restart identity belongs to a different request");
226
228
  return {
227
229
  kind: "reserved",
228
230
  request: e,
@@ -231,7 +233,7 @@ var b = _("_voltro_workflow_restart_requests", {
231
233
  }
232
234
  let [m] = await i.query({
233
235
  table: "_voltro_workflow_runs",
234
- predicate: f("id", a.runId),
236
+ predicate: p("id", a.runId),
235
237
  lock: "update",
236
238
  order: [],
237
239
  take: 1,
@@ -239,23 +241,23 @@ var b = _("_voltro_workflow_restart_requests", {
239
241
  projection: void 0
240
242
  });
241
243
  if (m === void 0 || m.tag !== a.control.workflowName || m.runnerGeneration !== a.generation || m.status !== "failed" || m.discardedAt != null || typeof m.executionId != "string" || m.executionId.length === 0) throw Error("Restart requires its undiscarded failed resident generation");
242
- if ((o.decodeUnknownSync(o.NullOr(o.Struct({ tenantId: o.NullOr(x) })))(m.subject ?? null)?.tenantId ?? null) !== a.tenantId) throw Error("Restart belongs to another resident tenant");
243
- let h = o.decodeUnknownSync(S)(m.engineDelivery);
244
- if (h.requestId !== s.requestId || h.restartToken !== s.restartToken) throw Error("Restart journal delivery does not belong to its resident generation");
245
- let [_] = await i.query({
246
- table: b.tableName,
247
- predicate: u(f("runId", a.runId), g(f("generation", a.generation), f("token", a.token))),
244
+ if ((s.decodeUnknownSync(s.NullOr(s.Struct({ tenantId: s.NullOr(S) })))(m.subject ?? null)?.tenantId ?? null) !== a.tenantId) throw Error("Restart belongs to another resident tenant");
245
+ let h = s.decodeUnknownSync(C)(m.engineDelivery);
246
+ if (h.requestId !== o.requestId || h.restartToken !== o.restartToken) throw Error("Restart journal delivery does not belong to its resident generation");
247
+ let [g] = await i.query({
248
+ table: x.tableName,
249
+ predicate: d(p("runId", a.runId), _(p("generation", a.generation), p("token", a.token))),
248
250
  lock: "update",
249
251
  order: [],
250
252
  take: 1,
251
253
  skip: void 0,
252
254
  projection: ["id", "generation"]
253
255
  });
254
- if (_ !== void 0) {
255
- if (_.generation !== a.generation) throw Error("Restart delivery token was already used by this run");
256
+ if (g !== void 0) {
257
+ if (g.generation !== a.generation) throw Error("Restart delivery token was already used by this run");
256
258
  return { kind: "already-requested" };
257
259
  }
258
- let v = t(i, a.control.workflowName, m.payload), y = e(a.control, v), T = await n({
260
+ let v = t(i, a.control.workflowName, m.payload), y = e(a.control, v), b = await n({
259
261
  store: i,
260
262
  control: a.control,
261
263
  keys: y,
@@ -263,9 +265,9 @@ var b = _("_voltro_workflow_restart_requests", {
263
265
  now: a.now,
264
266
  reconsidering: !0
265
267
  });
266
- if (T.kind !== "admit") return {
268
+ if (b.kind !== "admit") return {
267
269
  kind: "not-admitted",
268
- decision: T
270
+ decision: b
269
271
  };
270
272
  let E = await r({
271
273
  store: i,
@@ -273,7 +275,7 @@ var b = _("_voltro_workflow_restart_requests", {
273
275
  keys: y,
274
276
  outcome: "admitted",
275
277
  mode: "restart",
276
- reason: T.reason,
278
+ reason: b.reason,
277
279
  collapsed: 1,
278
280
  waitedMs: 0,
279
281
  executionId: null,
@@ -282,7 +284,7 @@ var b = _("_voltro_workflow_restart_requests", {
282
284
  now: a.now
283
285
  });
284
286
  if (E === void 0) throw Error("Restart has no admission reservation");
285
- let D = o.decodeUnknownSync(k)({
287
+ let D = s.decodeUnknownSync(A)({
286
288
  id: a.id,
287
289
  runId: a.runId,
288
290
  workflowName: a.control.workflowName,
@@ -291,23 +293,23 @@ var b = _("_voltro_workflow_restart_requests", {
291
293
  token: a.token,
292
294
  ledgerId: E,
293
295
  tenantId: a.tenantId,
294
- delivery: s,
296
+ delivery: o,
295
297
  replies: l,
296
298
  selection: c,
297
- eviction: T.evict ?? null,
299
+ eviction: b.evict ?? null,
298
300
  requestedAt: new Date(a.now),
299
301
  dispatchAttempts: 0,
300
302
  enteredGeneration: null
301
303
  });
302
- return await i.insert(b.tableName, D), {
304
+ return await i.insert(x.tableName, D), {
303
305
  kind: "reserved",
304
306
  request: D,
305
307
  reused: !1
306
308
  };
307
- }, L = async (e, t) => {
309
+ }, R = async (e, t) => {
308
310
  let [n] = await e.query({
309
- table: b.tableName,
310
- predicate: f("id", t),
311
+ table: x.tableName,
312
+ predicate: p("id", t),
311
313
  lock: "update",
312
314
  order: [],
313
315
  take: 1,
@@ -315,11 +317,11 @@ var b = _("_voltro_workflow_restart_requests", {
315
317
  projection: void 0
316
318
  });
317
319
  if (n === void 0) throw Error("Restart dispatch has no resident request");
318
- let r = o.decodeUnknownSync(k)(n);
320
+ let r = s.decodeUnknownSync(A)(n);
319
321
  if (r.enteredGeneration !== null) throw Error("Restart request has already entered its resident generation");
320
322
  let [i] = await e.query({
321
323
  table: "_voltro_workflow_admissions",
322
- predicate: f("id", r.ledgerId),
324
+ predicate: p("id", r.ledgerId),
323
325
  lock: "update",
324
326
  order: [],
325
327
  take: 1,
@@ -329,7 +331,7 @@ var b = _("_voltro_workflow_restart_requests", {
329
331
  if (i === void 0 || i.outcome !== "admitted" || i.releasedAt !== null || i.workflowName !== r.workflowName || i.tenantId !== r.tenantId || i.executionId !== null || i.runId !== null) throw Error("Restart dispatch has no matching held resident reservation");
330
332
  let [a] = await e.query({
331
333
  table: "_voltro_workflow_runs",
332
- predicate: f("id", r.runId),
334
+ predicate: p("id", r.runId),
333
335
  lock: "update",
334
336
  order: [],
335
337
  take: 1,
@@ -337,44 +339,44 @@ var b = _("_voltro_workflow_restart_requests", {
337
339
  projection: void 0
338
340
  });
339
341
  if (a === void 0 || a.tag !== r.workflowName || a.executionId !== r.executionId || a.runnerGeneration !== r.generation || a.status !== "failed" || a.discardedAt != null) throw Error("Restart dispatch no longer owns its failed resident generation");
340
- let s = o.decodeUnknownSync(S)(a.engineDelivery);
341
- if (s.requestId !== r.delivery.requestId || s.restartToken !== r.delivery.restartToken) throw Error("Restart dispatch no longer owns its journal delivery");
342
+ let o = s.decodeUnknownSync(C)(a.engineDelivery);
343
+ if (o.requestId !== r.delivery.requestId || o.restartToken !== r.delivery.restartToken) throw Error("Restart dispatch no longer owns its journal delivery");
342
344
  return {
343
345
  request: r,
344
346
  run: a
345
347
  };
346
- }, R = async (e, t) => {
347
- let { request: n } = await L(e, t), r = n.dispatchAttempts + 1;
348
+ }, z = async (e, t) => {
349
+ let { request: n } = await R(e, t), r = n.dispatchAttempts + 1;
348
350
  if (!Number.isSafeInteger(r)) throw Error("Restart dispatch attempt counter exhausted");
349
- if (await e.update(b.tableName, t, { dispatchAttempts: r }) === null) throw Error("Restart request disappeared before dispatch");
351
+ if (await e.update(x.tableName, t, { dispatchAttempts: r }) === null) throw Error("Restart request disappeared before dispatch");
350
352
  return {
351
353
  ...n,
352
354
  dispatchAttempts: r
353
355
  };
354
- }, z = (e) => a.gen(function* () {
355
- let { request: t } = yield* a.promise(() => e.transaction((t) => L(t, e.requestId)));
356
- if (!(yield* a.promise(() => e.acquireCapacity(t)))) return { kind: "waiting-capacity" };
357
- let n = yield* a.promise(() => e.transaction((e) => R(e, t.id)));
358
- return yield* i(n.replies, {
356
+ }, B = (e) => o.gen(function* () {
357
+ let { request: t } = yield* i(() => e.transaction((t) => R(t, e.requestId)));
358
+ if (!(yield* i(() => e.acquireCapacity(t)))) return { kind: "waiting-capacity" };
359
+ let n = yield* i(() => e.transaction((e) => z(e, t.id)));
360
+ return yield* a(n.replies, {
359
361
  requestId: n.delivery.requestId,
360
362
  previousToken: n.delivery.restartToken,
361
363
  token: n.token,
362
364
  workflowName: n.workflowName,
363
365
  executionId: n.executionId
364
366
  });
365
- }), B = async (e, t) => {
367
+ }), V = async (e, t) => {
366
368
  for (let e of [
367
369
  t.runId,
368
370
  t.workflowName,
369
371
  t.executionId,
370
372
  t.generation
371
- ]) o.decodeUnknownSync(x)(e);
373
+ ]) s.decodeUnknownSync(S)(e);
372
374
  if (!Number.isFinite(new Date(t.now).getTime())) throw Error("Restart entry requires a valid timestamp");
373
- let n = o.decodeUnknownSync(S)(t.delivery);
375
+ let n = s.decodeUnknownSync(C)(t.delivery);
374
376
  if (n.restartToken === null) throw Error("Restart entry has no delivery token");
375
377
  let [r] = await e.query({
376
- table: b.tableName,
377
- predicate: u(f("runId", t.runId), f("token", n.restartToken)),
378
+ table: x.tableName,
379
+ predicate: d(p("runId", t.runId), p("token", n.restartToken)),
378
380
  lock: "update",
379
381
  order: [],
380
382
  take: 1,
@@ -382,15 +384,15 @@ var b = _("_voltro_workflow_restart_requests", {
382
384
  projection: ["id"]
383
385
  });
384
386
  if (r === void 0) throw Error("Restart delivery has no matching resident request");
385
- let { request: i, run: a } = await L(e, o.decodeUnknownSync(x)(r.id));
387
+ let { request: i, run: a } = await R(e, s.decodeUnknownSync(S)(r.id));
386
388
  if (i.runId !== t.runId || i.workflowName !== t.workflowName || i.executionId !== t.executionId || i.generation === t.generation || i.delivery.requestId !== n.requestId || i.token !== n.restartToken) throw Error("Restart entry does not match its reserved delivery");
387
389
  if (i.dispatchAttempts === 0) throw Error("Restart entry has no committed dispatch attempt");
388
390
  if (await e.update("_voltro_workflow_admissions", i.ledgerId, {
389
391
  executionId: i.executionId,
390
392
  runId: i.runId
391
393
  }) === null) throw Error("Restart reservation disappeared during entry");
392
- if (await e.update(b.tableName, i.id, { enteredGeneration: t.generation }) === null) throw Error("Restart request disappeared during entry");
393
- let s = await e.update("_voltro_workflow_runs", i.runId, {
394
+ if (await e.update(x.tableName, i.id, { enteredGeneration: t.generation }) === null) throw Error("Restart request disappeared during entry");
395
+ let o = await e.update("_voltro_workflow_runs", i.runId, {
394
396
  runnerGeneration: t.generation,
395
397
  engineDelivery: n,
396
398
  runnerEnteredAt: new Date(t.now),
@@ -402,12 +404,12 @@ var b = _("_voltro_workflow_restart_requests", {
402
404
  completedAt: null,
403
405
  durationMs: null
404
406
  });
405
- if (s === null) throw Error("Restart run disappeared during entry");
407
+ if (o === null) throw Error("Restart run disappeared during entry");
406
408
  return {
407
- row: s,
409
+ row: o,
408
410
  previous: a,
409
411
  event: await e.insert("_voltro_workflow_run_events", {
410
- id: d("wfev", JSON.stringify(["restart", i.id])),
412
+ id: f("wfev", JSON.stringify(["restart", i.id])),
411
413
  runId: i.runId,
412
414
  eventType: "run-redriven",
413
415
  occurredAt: new Date(t.now),
@@ -422,5 +424,4 @@ var b = _("_voltro_workflow_restart_requests", {
422
424
  })
423
425
  };
424
426
  };
425
- //#endregion
426
- export { R as beginWorkflowRestartDispatchInTransaction, B as consumeWorkflowRestartInTransaction, N as readWorkflowRestartCommandInTransaction, M as readWorkflowRestartObservationInTransaction, F as readWorkflowRestartReservationInTransaction, I as reserveWorkflowRestartInTransaction, z as resetReservedWorkflowJournal, P as withdrawWorkflowRestartInTransaction, b as workflowRestartRequestsTable, T as workflowRestartWithdrawalsTable };
427
+ export { z as beginWorkflowRestartDispatchInTransaction, V as consumeWorkflowRestartInTransaction, P as readWorkflowRestartCommandInTransaction, N as readWorkflowRestartObservationInTransaction, I as readWorkflowRestartReservationInTransaction, L as reserveWorkflowRestartInTransaction, B as resetReservedWorkflowJournal, F as withdrawWorkflowRestartInTransaction, x as workflowRestartRequestsTable, E as workflowRestartWithdrawalsTable };
@@ -1,29 +1,29 @@
1
1
  import { beginWorkflowControlDispatchInTransaction as e, bindWorkflowResumeJournalInTransaction as t } from "./controlRequest.js";
2
- import { observeWorkflowSuspension as n, resetExpectedWorkflowSuspension as r } from "./workflowJournalReset.js";
3
- import { Context as i, Effect as a } from "effect";
4
- import { SqlClient as o } from "effect/sql";
5
- import { Sharding as s } from "effect/cluster";
6
- //#region src/workflowResumeDispatch.ts
7
- var c = (i) => a.gen(function* () {
8
- let o = yield* a.context(), c = yield* a.promise(() => i.transaction((e) => t(e, i.requestId, (e) => a.runPromiseWith(o)(n(e)))));
9
- if (c.kind !== "bound") return c;
10
- let l = yield* a.promise(() => i.transaction((t) => e(t, i.requestId)));
2
+ import { JournalRoutingGuard as n, ownedJournalRoutingPromise as r, protectJournalRoutingEffect as i } from "./journalRoutingGuard.js";
3
+ import { observeWorkflowSuspension as a, resetExpectedWorkflowSuspension as o } from "./workflowJournalReset.js";
4
+ import { Context as s, Effect as c, Option as l } from "effect";
5
+ import { SqlClient as u } from "effect/sql";
6
+ import { Sharding as d } from "effect/cluster";
7
+ // src/workflowResumeDispatch.ts
8
+ var f = (n) => c.gen(function* () {
9
+ let i = yield* c.context(), s = yield* r(() => n.transaction((e) => t(e, n.requestId, (e) => c.runPromiseWith(i)(a(e)))));
10
+ if (s.kind !== "bound") return s;
11
+ let l = yield* r(() => n.transaction((t) => e(t, n.requestId)));
11
12
  if (l.kind !== "dispatch") return l;
12
13
  let { request: u } = l;
13
- if (u.operation !== "resume" || u.resumeJournal === null) return yield* a.die(/* @__PURE__ */ Error("Resume dispatch requires its committed suspension binding"));
14
- let d = yield* r({
14
+ if (u.operation !== "resume" || u.resumeJournal === null) return yield* c.die(/* @__PURE__ */ Error("Resume dispatch requires its committed suspension binding"));
15
+ let f = yield* o({
15
16
  workflowName: u.workflowName,
16
17
  executionId: u.executionId,
17
18
  ...u.resumeJournal
18
19
  });
19
- return yield* (yield* s.Sharding).pollStorage, {
20
+ return yield* (yield* d.Sharding).pollStorage, {
20
21
  kind: "attempted",
21
22
  request: u,
22
- journal: d
23
+ journal: f
23
24
  };
24
- }), l = class extends i.Service()("@voltro/workflow/WorkflowResumeDispatcher") {}, u = a.gen(function* () {
25
- let e = yield* o.SqlClient, t = yield* s.Sharding;
26
- return l.of({ dispatch: (n) => c(n).pipe(a.provideService(o.SqlClient, e), a.provideService(s.Sharding, t)) });
25
+ }), p = class extends s.Service()("@voltro/workflow/WorkflowResumeDispatcher") {}, m = c.gen(function* () {
26
+ let e = l.getOrUndefined(yield* c.serviceOption(n)), t = yield* u.SqlClient, r = yield* d.Sharding;
27
+ return p.of({ dispatch: (n) => i(e, f(n)).pipe(c.provideService(u.SqlClient, t), c.provideService(d.Sharding, r)) });
27
28
  });
28
- //#endregion
29
- export { l as WorkflowResumeDispatcher, c as dispatchWorkflowResume, u as makeWorkflowResumeDispatcher };
29
+ export { p as WorkflowResumeDispatcher, f as dispatchWorkflowResume, m as makeWorkflowResumeDispatcher };
@@ -0,0 +1,11 @@
1
+ import { JournalRoutingGuard as e } from "./journalRoutingGuard.js";
2
+ import { Context as t, Effect as n, Option as r } from "effect";
3
+ import { Sharding as i } from "effect/cluster";
4
+ // src/workflowRuntimeReadiness.ts
5
+ var a = class extends t.Service()("@voltro/workflow/WorkflowRuntimeReadiness") {}, o = n.gen(function* () {
6
+ let t = yield* i.Sharding, a = r.getOrUndefined(yield* n.serviceOption(e)), o = !0;
7
+ return yield* n.addFinalizer(() => n.sync(() => {
8
+ o = !1;
9
+ })), { isReady: () => o && !a?.isClosed() && !n.runSync(t.isShutdown) };
10
+ }), s = n.map(n.serviceOption(a), (e) => r.isSome(e) ? e.value.isReady : () => !0);
11
+ export { a as WorkflowRuntimeReadiness, s as captureWorkflowRuntimeReadiness, o as makeWorkflowRuntimeReadiness };