@fabricorg/platform-temporal 4.2.2 → 4.2.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.
package/dist/index.cjs CHANGED
@@ -1,341 +1,359 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- SAGA_SIGNALS: () => SAGA_SIGNALS,
24
- SagaCompensationError: () => SagaCompensationError,
25
- SagaWorkflowRegistry: () => SagaWorkflowRegistry,
26
- assertWorkerVersionEvidence: () => assertWorkerVersionEvidence,
27
- compensationInvocationIdentity: () => compensationInvocationIdentity,
28
- createGovernedActionActivities: () => createGovernedActionActivities,
29
- createSagaParentActivities: () => createSagaParentActivities,
30
- createSagaSignalDispatcher: () => createSagaSignalDispatcher,
31
- createSagaSignalInbox: () => createSagaSignalInbox,
32
- createTemporalSagaDispatcher: () => createTemporalSagaDispatcher,
33
- runCompensatedSaga: () => runCompensatedSaga,
34
- sagaChildCausation: () => sagaChildCausation,
35
- sagaParentActivityIdentity: () => sagaParentActivityIdentity,
36
- startGovernedWorkflow: () => startGovernedWorkflow,
37
- temporalInvocationIdentity: () => temporalInvocationIdentity,
38
- temporalWorkflowStartClient: () => temporalWorkflowStartClient
39
- });
40
- module.exports = __toCommonJS(index_exports);
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region index.ts
41
3
  function temporalInvocationIdentity(input) {
42
- return { idempotencyKey: `temporal:${input.workflowId}:${input.stepId}`, correlationId: `temporal:${input.workflowId}` };
4
+ return {
5
+ idempotencyKey: `temporal:${input.workflowId}:${input.stepId}`,
6
+ correlationId: `temporal:${input.workflowId}`
7
+ };
43
8
  }
9
+ /** Activity implementation. It always enters through the governed Host; handlers are never called directly. */
44
10
  function createGovernedActionActivities(invoker) {
45
- return {
46
- invokeGovernedAction: async (input) => {
47
- const identity = temporalInvocationIdentity(input);
48
- return invoker.invoke({
49
- actionId: input.actionId,
50
- parameters: input.parameters,
51
- tenantId: input.tenantId,
52
- spaceId: input.spaceId,
53
- actorId: input.actorId,
54
- actorType: input.actorType,
55
- ...identity,
56
- ...input.parentInvocationId ? { causationId: input.parentInvocationId } : {},
57
- executionEvidence: { temporalWorkflowId: input.workflowId, temporalRunId: input.runId, temporalStepId: input.stepId }
58
- });
59
- }
60
- };
11
+ return { invokeGovernedAction: async (input) => {
12
+ const identity = temporalInvocationIdentity(input);
13
+ return invoker.invoke({
14
+ actionId: input.actionId,
15
+ parameters: input.parameters,
16
+ tenantId: input.tenantId,
17
+ spaceId: input.spaceId,
18
+ actorId: input.actorId,
19
+ actorType: input.actorType,
20
+ ...identity,
21
+ ...input.parentInvocationId ? { causationId: input.parentInvocationId } : {},
22
+ executionEvidence: {
23
+ temporalWorkflowId: input.workflowId,
24
+ temporalRunId: input.runId,
25
+ temporalStepId: input.stepId
26
+ }
27
+ });
28
+ } };
61
29
  }
62
30
  function createSagaSignalDispatcher(client) {
63
- return (workflowId, envelope) => {
64
- assertSignalIdentity(envelope);
65
- return client.signal(workflowId, envelope.signal, envelope);
66
- };
31
+ return (workflowId, envelope) => {
32
+ assertSignalIdentity(envelope);
33
+ return client.signal(workflowId, envelope.signal, envelope);
34
+ };
67
35
  }
68
36
  function assertSignalIdentity(envelope) {
69
- if (envelope.signalId === void 0) return;
70
- if (typeof envelope.signalId !== "string" || !envelope.signalId.trim()) {
71
- throw new Error(`Saga signal "${envelope.signal}" for saga "${envelope.sagaId}" has a blank signalId; omit it or supply a stable non-blank identity.`);
72
- }
37
+ if (envelope.signalId === void 0) return;
38
+ if (typeof envelope.signalId !== "string" || !envelope.signalId.trim()) throw new Error(`Saga signal "${envelope.signal}" for saga "${envelope.sagaId}" has a blank signalId; omit it or supply a stable non-blank identity.`);
73
39
  }
40
+ /** A stable identity a receiver can deduplicate on, or undefined when the envelope carries none. */
74
41
  function signalIdentity(envelope) {
75
- return typeof envelope.signalId === "string" && envelope.signalId.trim() ? envelope.signalId : void 0;
42
+ return typeof envelope.signalId === "string" && envelope.signalId.trim() ? envelope.signalId : void 0;
76
43
  }
44
+ /**
45
+ * Deterministic receiver for saga signals. It has no Temporal dependency and reads
46
+ * no clock, randomness or process-global state, so it can run inside the workflow
47
+ * sandbox. Wire it with
48
+ * `setHandler(defineSignal(SAGA_SIGNALS.approval), (envelope) => { inbox.receive(envelope); })`.
49
+ *
50
+ * Buffering is FIFO per signal name. Deduplication is keyed on `signalId` across
51
+ * every signal name received by this inbox. Dedup state lives in this instance, so
52
+ * it lasts for one workflow run unless the caller carries
53
+ * {@link SagaSignalInbox.snapshot} into the next run. The remembered set grows
54
+ * with each distinct `signalId` and is not pruned, including across
55
+ * continue-as-new, because forgetting an ID would let its redelivery through.
56
+ */
77
57
  function createSagaSignalInbox(options = {}) {
78
- const seen = new Set(options.seenSignalIds ?? []);
79
- const queues = /* @__PURE__ */ new Map();
80
- const enqueue = (envelope) => {
81
- const queue = queues.get(envelope.signal);
82
- if (queue) queue.push(envelope);
83
- else queues.set(envelope.signal, [envelope]);
84
- };
85
- for (const envelope of options.pending ?? []) {
86
- const id = signalIdentity(envelope);
87
- if (id !== void 0) seen.add(id);
88
- enqueue(envelope);
89
- }
90
- return {
91
- receive(envelope) {
92
- const id = signalIdentity(envelope);
93
- if (id !== void 0) {
94
- if (seen.has(id)) return false;
95
- seen.add(id);
96
- }
97
- enqueue(envelope);
98
- return true;
99
- },
100
- take(signalName) {
101
- return queues.get(signalName)?.shift();
102
- },
103
- pending(signalName) {
104
- return queues.get(signalName)?.length ?? 0;
105
- },
106
- seenSignalIds() {
107
- return [...seen];
108
- },
109
- snapshot() {
110
- return { seenSignalIds: [...seen], pending: [...queues.values()].flat() };
111
- }
112
- };
58
+ const seen = new Set(options.seenSignalIds ?? []);
59
+ const queues = /* @__PURE__ */ new Map();
60
+ const enqueue = (envelope) => {
61
+ const queue = queues.get(envelope.signal);
62
+ if (queue) queue.push(envelope);
63
+ else queues.set(envelope.signal, [envelope]);
64
+ };
65
+ for (const envelope of options.pending ?? []) {
66
+ const id = signalIdentity(envelope);
67
+ if (id !== void 0) seen.add(id);
68
+ enqueue(envelope);
69
+ }
70
+ return {
71
+ receive(envelope) {
72
+ const id = signalIdentity(envelope);
73
+ if (id !== void 0) {
74
+ if (seen.has(id)) return false;
75
+ seen.add(id);
76
+ }
77
+ enqueue(envelope);
78
+ return true;
79
+ },
80
+ take(signalName) {
81
+ return queues.get(signalName)?.shift();
82
+ },
83
+ pending(signalName) {
84
+ return queues.get(signalName)?.length ?? 0;
85
+ },
86
+ seenSignalIds() {
87
+ return [...seen];
88
+ },
89
+ snapshot() {
90
+ return {
91
+ seenSignalIds: [...seen],
92
+ pending: [...queues.values()].flat()
93
+ };
94
+ }
95
+ };
113
96
  }
114
97
  function startGovernedWorkflow(client, input) {
115
- return client.start({ ...input, idConflictPolicy: "use_existing" });
98
+ return client.start({
99
+ ...input,
100
+ idConflictPolicy: "use_existing"
101
+ });
116
102
  }
103
+ /** Instance-scoped registry; registration order never changes resolution. */
117
104
  var SagaWorkflowRegistry = class {
118
- bindings = /* @__PURE__ */ new Map();
119
- constructor(bindings = []) {
120
- for (const binding of bindings) this.register(binding);
121
- }
122
- register(binding) {
123
- if (this.bindings.has(binding.actionId)) {
124
- throw new Error(`Saga workflow binding already registered: ${binding.actionId}`);
125
- }
126
- if (!binding.workflowType.trim() || !binding.taskQueue.trim()) {
127
- throw new Error(`Saga workflow binding ${binding.actionId} requires workflowType and taskQueue`);
128
- }
129
- this.bindings.set(binding.actionId, { ...binding });
130
- }
131
- require(actionId) {
132
- const binding = this.bindings.get(actionId);
133
- if (!binding) throw new Error(`No saga workflow binding registered for ${actionId}`);
134
- return { ...binding };
135
- }
105
+ bindings = /* @__PURE__ */ new Map();
106
+ constructor(bindings = []) {
107
+ for (const binding of bindings) this.register(binding);
108
+ }
109
+ register(binding) {
110
+ if (this.bindings.has(binding.actionId)) throw new Error(`Saga workflow binding already registered: ${binding.actionId}`);
111
+ if (!binding.workflowType.trim() || !binding.taskQueue.trim()) throw new Error(`Saga workflow binding ${binding.actionId} requires workflowType and taskQueue`);
112
+ this.bindings.set(binding.actionId, { ...binding });
113
+ }
114
+ require(actionId) {
115
+ const binding = this.bindings.get(actionId);
116
+ if (!binding) throw new Error(`No saga workflow binding registered for ${actionId}`);
117
+ return { ...binding };
118
+ }
136
119
  };
120
+ /** Starts the registered parent workflow with an invocation-only durable identity. */
137
121
  function createTemporalSagaDispatcher(client, registry) {
138
- return {
139
- dispatch: async (input) => {
140
- const binding = registry.require(input.actionId);
141
- const handle = await startGovernedWorkflow(client, {
142
- workflowType: binding.workflowType,
143
- workflowId: input.workflowId,
144
- taskQueue: binding.taskQueue,
145
- args: [{
146
- parentInvocationId: input.actionInvocationId,
147
- actionId: input.actionId,
148
- tenantId: input.tenantId,
149
- spaceId: input.spaceId
150
- }]
151
- });
152
- const runId = runIdFromHandle(handle);
153
- return { workflowId: input.workflowId, ...runId ? { runId } : {} };
154
- }
155
- };
122
+ return { dispatch: async (input) => {
123
+ const binding = registry.require(input.actionId);
124
+ const runId = runIdFromHandle(await startGovernedWorkflow(client, {
125
+ workflowType: binding.workflowType,
126
+ workflowId: input.workflowId,
127
+ taskQueue: binding.taskQueue,
128
+ args: [{
129
+ parentInvocationId: input.actionInvocationId,
130
+ actionId: input.actionId,
131
+ tenantId: input.tenantId,
132
+ spaceId: input.spaceId
133
+ }]
134
+ }));
135
+ return {
136
+ workflowId: input.workflowId,
137
+ ...runId ? { runId } : {}
138
+ };
139
+ } };
156
140
  }
157
141
  function runIdFromHandle(handle) {
158
- if (!handle || typeof handle !== "object") return void 0;
159
- const candidate = handle.firstExecutionRunId;
160
- return typeof candidate === "string" && candidate ? candidate : void 0;
142
+ if (!handle || typeof handle !== "object") return void 0;
143
+ const candidate = handle.firstExecutionRunId;
144
+ return typeof candidate === "string" && candidate ? candidate : void 0;
161
145
  }
146
+ /** Parent lifecycle remains explicit durable activities, never workflow-local state. */
162
147
  function createSagaParentActivities(lifecycle) {
163
- return {
164
- recordSagaProgress: (input) => lifecycle.recordProgress({
165
- ...input,
166
- lifecycleId: input.lifecycleId ?? sagaParentActivityIdentity(
167
- input.parentInvocationId,
168
- "progress",
169
- `${input.progress.stepId}:${input.progress.index}`
170
- ).idempotencyKey
171
- }),
172
- parkSagaApproval: (input) => lifecycle.parkApproval({
173
- ...input,
174
- lifecycleId: input.lifecycleId ?? sagaParentActivityIdentity(
175
- input.parentInvocationId,
176
- "approval",
177
- input.approvalId
178
- ).idempotencyKey
179
- }),
180
- cancelSagaParent: (input) => lifecycle.cancel({
181
- ...input,
182
- lifecycleId: input.lifecycleId ?? sagaParentActivityIdentity(
183
- input.parentInvocationId,
184
- "cancel"
185
- ).idempotencyKey
186
- }),
187
- completeSagaParent: (input) => lifecycle.complete({
188
- ...input,
189
- lifecycleId: input.lifecycleId ?? sagaParentActivityIdentity(
190
- input.parentInvocationId,
191
- "complete"
192
- ).idempotencyKey
193
- }),
194
- failSagaParent: (input) => lifecycle.fail({
195
- ...input,
196
- lifecycleId: input.lifecycleId ?? sagaParentActivityIdentity(
197
- input.parentInvocationId,
198
- "fail"
199
- ).idempotencyKey
200
- })
201
- };
148
+ return {
149
+ recordSagaProgress: (input) => lifecycle.recordProgress({
150
+ ...input,
151
+ lifecycleId: input.lifecycleId ?? sagaParentActivityIdentity(input.parentInvocationId, "progress", `${input.progress.stepId}:${input.progress.index}`).idempotencyKey
152
+ }),
153
+ parkSagaApproval: (input) => lifecycle.parkApproval({
154
+ ...input,
155
+ lifecycleId: input.lifecycleId ?? sagaParentActivityIdentity(input.parentInvocationId, "approval", input.approvalId).idempotencyKey
156
+ }),
157
+ cancelSagaParent: (input) => lifecycle.cancel({
158
+ ...input,
159
+ lifecycleId: input.lifecycleId ?? sagaParentActivityIdentity(input.parentInvocationId, "cancel").idempotencyKey
160
+ }),
161
+ completeSagaParent: (input) => lifecycle.complete({
162
+ ...input,
163
+ lifecycleId: input.lifecycleId ?? sagaParentActivityIdentity(input.parentInvocationId, "complete").idempotencyKey
164
+ }),
165
+ failSagaParent: (input) => lifecycle.fail({
166
+ ...input,
167
+ lifecycleId: input.lifecycleId ?? sagaParentActivityIdentity(input.parentInvocationId, "fail").idempotencyKey
168
+ })
169
+ };
202
170
  }
171
+ /**
172
+ * Stable identity for a saga parent's own lifecycle activity (progress,
173
+ * cancellation, approval parking, completion, failure). The activity stepId
174
+ * is namespaced under `parent:` so it can never collide with a forward step or
175
+ * a `compensate:` reversal.
176
+ */
203
177
  function sagaParentActivityIdentity(workflowId, activity, discriminator) {
204
- return temporalInvocationIdentity({
205
- workflowId,
206
- stepId: `parent:${activity}${discriminator ? `:${discriminator}` : ""}`
207
- });
178
+ return temporalInvocationIdentity({
179
+ workflowId,
180
+ stepId: `parent:${activity}${discriminator ? `:${discriminator}` : ""}`
181
+ });
208
182
  }
183
+ /**
184
+ * Stable causation identity linking a child step to its parent invocation.
185
+ * The parent invocation ID becomes the causation ID; the step keeps its own
186
+ * deterministic idempotency key and shares the workflow correlation ID.
187
+ */
209
188
  function sagaChildCausation(workflowId, parentInvocationId, stepId) {
210
- return { ...temporalInvocationIdentity({ workflowId, stepId }), causationId: parentInvocationId };
189
+ return {
190
+ ...temporalInvocationIdentity({
191
+ workflowId,
192
+ stepId
193
+ }),
194
+ causationId: parentInvocationId
195
+ };
211
196
  }
212
- var SAGA_SIGNALS = {
213
- proceed: "saga.proceed",
214
- cancel: "saga.cancel",
215
- approval: "saga.approval"
197
+ /** Stable signal names a saga workflow may register handlers for. */
198
+ const SAGA_SIGNALS = {
199
+ proceed: "saga.proceed",
200
+ cancel: "saga.cancel",
201
+ approval: "saga.approval"
216
202
  };
203
+ /** Adapts the official Temporal TypeScript `WorkflowClient` to the narrow testable binding seam. */
217
204
  function temporalWorkflowStartClient(client) {
218
- return {
219
- start: ({ workflowType, workflowId, taskQueue, args }) => client.start(workflowType, { workflowId, taskQueue, args, workflowIdConflictPolicy: "USE_EXISTING" })
220
- };
205
+ return { start: ({ workflowType, workflowId, taskQueue, args }) => client.start(workflowType, {
206
+ workflowId,
207
+ taskQueue,
208
+ args,
209
+ workflowIdConflictPolicy: "USE_EXISTING"
210
+ }) };
221
211
  }
222
212
  function assertWorkerVersionEvidence(value) {
223
- if (!value.buildId.trim() || !value.deploymentName.trim()) throw new Error("Temporal worker buildId and deploymentName are required for rollover evidence.");
213
+ if (!value.buildId.trim() || !value.deploymentName.trim()) throw new Error("Temporal worker buildId and deploymentName are required for rollover evidence.");
224
214
  }
215
+ /** Reversals are governed invocations too, so they carry their own stable identity. */
225
216
  function compensationInvocationIdentity(input) {
226
- return temporalInvocationIdentity({ workflowId: input.workflowId, stepId: `compensate:${input.stepId}` });
217
+ return temporalInvocationIdentity({
218
+ workflowId: input.workflowId,
219
+ stepId: `compensate:${input.stepId}`
220
+ });
227
221
  }
228
222
  var SagaCompensationError = class extends Error {
229
- constructor(result) {
230
- super(`Saga step "${result.failedStep?.stepId}" failed: ${result.failedStep?.error}`);
231
- this.result = result;
232
- }
233
- result;
234
- name = "SagaCompensationError";
223
+ result;
224
+ name = "SagaCompensationError";
225
+ constructor(result) {
226
+ super(`Saga step "${result.failedStep?.stepId}" failed: ${result.failedStep?.error}`);
227
+ this.result = result;
228
+ }
235
229
  };
236
- var SUCCESSFUL_STATUS = "completed";
230
+ /**
231
+ * The governed Host resolves for outcomes that are not success — a blocked policy, a
232
+ * failed validation, a reconciliation, an approval park. Treating a resolved promise
233
+ * as a completed step would silently skip the unwind, so the status is inspected.
234
+ */
235
+ const SUCCESSFUL_STATUS = "completed";
237
236
  function stepFailure(result, label) {
238
- if (!result || typeof result !== "object") return void 0;
239
- const status = result.status;
240
- if (typeof status !== "string" || status === SUCCESSFUL_STATUS) return void 0;
241
- const error = result.error;
242
- return `${label} resolved with status "${status}"${typeof error === "string" && error ? `: ${error}` : ""}`;
237
+ if (!result || typeof result !== "object") return void 0;
238
+ const status = result.status;
239
+ if (typeof status !== "string" || status === SUCCESSFUL_STATUS) return void 0;
240
+ const error = result.error;
241
+ return `${label} resolved with status "${status}"${typeof error === "string" && error ? `: ${error}` : ""}`;
243
242
  }
244
243
  function assertDistinctSteps(steps) {
245
- const seen = /* @__PURE__ */ new Set();
246
- for (const step of steps) {
247
- if (!step.stepId.trim()) throw new Error("Every saga step requires a stepId.");
248
- if (step.stepId.startsWith("compensate:")) throw new Error(`Saga step "${step.stepId}" must not use the reserved "compensate:" prefix.`);
249
- if (step.stepId.startsWith("parent:")) throw new Error(`Saga step "${step.stepId}" must not use the reserved "parent:" prefix.`);
250
- if (seen.has(step.stepId)) throw new Error(`Saga declares duplicate step "${step.stepId}"; step identity must be unique within a workflow.`);
251
- seen.add(step.stepId);
252
- }
244
+ const seen = /* @__PURE__ */ new Set();
245
+ for (const step of steps) {
246
+ if (!step.stepId.trim()) throw new Error("Every saga step requires a stepId.");
247
+ if (step.stepId.startsWith("compensate:")) throw new Error(`Saga step "${step.stepId}" must not use the reserved "compensate:" prefix.`);
248
+ if (step.stepId.startsWith("parent:")) throw new Error(`Saga step "${step.stepId}" must not use the reserved "parent:" prefix.`);
249
+ if (seen.has(step.stepId)) throw new Error(`Saga declares duplicate step "${step.stepId}"; step identity must be unique within a workflow.`);
250
+ seen.add(step.stepId);
251
+ }
253
252
  }
254
253
  function assertMatchesDeclaration(steps, workflow) {
255
- const declared = new Map((workflow.compensations ?? []).map((entry) => [entry.step, entry.actionId]));
256
- for (const step of steps) {
257
- const expected = declared.get(step.stepId);
258
- if (expected === void 0) {
259
- if (step.compensateWith) throw new Error(`Workflow "${workflow.name}" step "${step.stepId}" supplies a reversal the manifest does not declare.`);
260
- continue;
261
- }
262
- if (!step.compensateWith) throw new Error(`Workflow "${workflow.name}" step "${step.stepId}" omits the reversal "${expected}" its manifest declares.`);
263
- if (step.compensateWith.actionId !== expected) {
264
- throw new Error(`Workflow "${workflow.name}" step "${step.stepId}" reverses with "${step.compensateWith.actionId}" but its manifest declares "${expected}".`);
265
- }
266
- }
267
- for (const [step, actionId] of declared) {
268
- if (!steps.some((candidate) => candidate.stepId === step)) {
269
- throw new Error(`Workflow "${workflow.name}" declares a reversal "${actionId}" for step "${step}", which this saga never runs.`);
270
- }
271
- }
254
+ const declared = new Map((workflow.compensations ?? []).map((entry) => [entry.step, entry.actionId]));
255
+ for (const step of steps) {
256
+ const expected = declared.get(step.stepId);
257
+ if (expected === void 0) {
258
+ if (step.compensateWith) throw new Error(`Workflow "${workflow.name}" step "${step.stepId}" supplies a reversal the manifest does not declare.`);
259
+ continue;
260
+ }
261
+ if (!step.compensateWith) throw new Error(`Workflow "${workflow.name}" step "${step.stepId}" omits the reversal "${expected}" its manifest declares.`);
262
+ if (step.compensateWith.actionId !== expected) throw new Error(`Workflow "${workflow.name}" step "${step.stepId}" reverses with "${step.compensateWith.actionId}" but its manifest declares "${expected}".`);
263
+ }
264
+ for (const [step, actionId] of declared) if (!steps.some((candidate) => candidate.stepId === step)) throw new Error(`Workflow "${workflow.name}" declares a reversal "${actionId}" for step "${step}", which this saga never runs.`);
272
265
  }
266
+ /**
267
+ * Runs steps in order, accumulating reversals as each succeeds. On failure the
268
+ * accumulated reversals run newest-first, so an unwind never leaves an orphan.
269
+ * Every forward step and every reversal enters through the governed Host with a
270
+ * deterministic idempotency key, so a retried or replayed unwind is not double-applied.
271
+ */
273
272
  async function runCompensatedSaga(activity, input) {
274
- const scope = {
275
- tenantId: input.tenantId,
276
- spaceId: input.spaceId,
277
- actorId: input.actorId,
278
- actorType: input.actorType,
279
- workflowId: input.workflowId,
280
- runId: input.runId,
281
- ...input.parentInvocationId ? { parentInvocationId: input.parentInvocationId } : {}
282
- };
283
- assertDistinctSteps(input.steps);
284
- if (input.workflow) assertMatchesDeclaration(input.steps, input.workflow);
285
- const completed = [];
286
- const reversals = [];
287
- const unwind = async (failedStep) => {
288
- const compensated = [];
289
- for (const applied of [...reversals].reverse()) {
290
- const reversal = applied.compensateWith;
291
- const record = { stepId: applied.stepId, actionId: reversal.actionId };
292
- try {
293
- const result = await activity({
294
- ...scope,
295
- actionId: reversal.actionId,
296
- parameters: reversal.parameters ?? applied.parameters,
297
- stepId: `compensate:${applied.stepId}`
298
- });
299
- const refused = stepFailure(result, `reversal "${reversal.actionId}"`);
300
- if (refused) record.error = refused;
301
- } catch (compensationError) {
302
- record.error = compensationError instanceof Error ? compensationError.message : String(compensationError);
303
- }
304
- compensated.push(record);
305
- }
306
- throw new SagaCompensationError({ completed, compensated, failedStep });
307
- };
308
- for (const step of input.steps) {
309
- let result;
310
- try {
311
- result = await activity({ ...scope, actionId: step.actionId, parameters: step.parameters, stepId: step.stepId });
312
- } catch (error) {
313
- return await unwind({ stepId: step.stepId, error: error instanceof Error ? error.message : String(error) });
314
- }
315
- const refused = stepFailure(result, `step "${step.stepId}"`);
316
- if (refused) return await unwind({ stepId: step.stepId, error: refused });
317
- completed.push(step.stepId);
318
- if (step.compensateWith) reversals.push(step);
319
- }
320
- return { completed, compensated: [] };
273
+ const scope = {
274
+ tenantId: input.tenantId,
275
+ spaceId: input.spaceId,
276
+ actorId: input.actorId,
277
+ actorType: input.actorType,
278
+ workflowId: input.workflowId,
279
+ runId: input.runId,
280
+ ...input.parentInvocationId ? { parentInvocationId: input.parentInvocationId } : {}
281
+ };
282
+ assertDistinctSteps(input.steps);
283
+ if (input.workflow) assertMatchesDeclaration(input.steps, input.workflow);
284
+ const completed = [];
285
+ const reversals = [];
286
+ const unwind = async (failedStep) => {
287
+ const compensated = [];
288
+ for (const applied of [...reversals].reverse()) {
289
+ const reversal = applied.compensateWith;
290
+ const record = {
291
+ stepId: applied.stepId,
292
+ actionId: reversal.actionId
293
+ };
294
+ try {
295
+ const refused = stepFailure(await activity({
296
+ ...scope,
297
+ actionId: reversal.actionId,
298
+ parameters: reversal.parameters ?? applied.parameters,
299
+ stepId: `compensate:${applied.stepId}`
300
+ }), `reversal "${reversal.actionId}"`);
301
+ if (refused) record.error = refused;
302
+ } catch (compensationError) {
303
+ record.error = compensationError instanceof Error ? compensationError.message : String(compensationError);
304
+ }
305
+ compensated.push(record);
306
+ }
307
+ throw new SagaCompensationError({
308
+ completed,
309
+ compensated,
310
+ failedStep
311
+ });
312
+ };
313
+ for (const step of input.steps) {
314
+ let result;
315
+ try {
316
+ result = await activity({
317
+ ...scope,
318
+ actionId: step.actionId,
319
+ parameters: step.parameters,
320
+ stepId: step.stepId
321
+ });
322
+ } catch (error) {
323
+ return await unwind({
324
+ stepId: step.stepId,
325
+ error: error instanceof Error ? error.message : String(error)
326
+ });
327
+ }
328
+ const refused = stepFailure(result, `step "${step.stepId}"`);
329
+ if (refused) return await unwind({
330
+ stepId: step.stepId,
331
+ error: refused
332
+ });
333
+ completed.push(step.stepId);
334
+ if (step.compensateWith) reversals.push(step);
335
+ }
336
+ return {
337
+ completed,
338
+ compensated: []
339
+ };
321
340
  }
322
- // Annotate the CommonJS export names for ESM import in node:
323
- 0 && (module.exports = {
324
- SAGA_SIGNALS,
325
- SagaCompensationError,
326
- SagaWorkflowRegistry,
327
- assertWorkerVersionEvidence,
328
- compensationInvocationIdentity,
329
- createGovernedActionActivities,
330
- createSagaParentActivities,
331
- createSagaSignalDispatcher,
332
- createSagaSignalInbox,
333
- createTemporalSagaDispatcher,
334
- runCompensatedSaga,
335
- sagaChildCausation,
336
- sagaParentActivityIdentity,
337
- startGovernedWorkflow,
338
- temporalInvocationIdentity,
339
- temporalWorkflowStartClient
340
- });
341
+ //#endregion
342
+ exports.SAGA_SIGNALS = SAGA_SIGNALS;
343
+ exports.SagaCompensationError = SagaCompensationError;
344
+ exports.SagaWorkflowRegistry = SagaWorkflowRegistry;
345
+ exports.assertWorkerVersionEvidence = assertWorkerVersionEvidence;
346
+ exports.compensationInvocationIdentity = compensationInvocationIdentity;
347
+ exports.createGovernedActionActivities = createGovernedActionActivities;
348
+ exports.createSagaParentActivities = createSagaParentActivities;
349
+ exports.createSagaSignalDispatcher = createSagaSignalDispatcher;
350
+ exports.createSagaSignalInbox = createSagaSignalInbox;
351
+ exports.createTemporalSagaDispatcher = createTemporalSagaDispatcher;
352
+ exports.runCompensatedSaga = runCompensatedSaga;
353
+ exports.sagaChildCausation = sagaChildCausation;
354
+ exports.sagaParentActivityIdentity = sagaParentActivityIdentity;
355
+ exports.startGovernedWorkflow = startGovernedWorkflow;
356
+ exports.temporalInvocationIdentity = temporalInvocationIdentity;
357
+ exports.temporalWorkflowStartClient = temporalWorkflowStartClient;
358
+
341
359
  //# sourceMappingURL=index.cjs.map