@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/CHANGELOG.md +22 -0
- package/dist/index.cjs +315 -297
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +250 -250
- package/dist/index.d.ts +250 -250
- package/dist/index.js +299 -257
- package/dist/index.js.map +1 -1
- package/package.json +7 -6
package/dist/index.cjs
CHANGED
|
@@ -1,341 +1,359 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
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
|
-
|
|
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
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
70
|
-
|
|
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
|
-
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
-
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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
|
-
|
|
205
|
-
|
|
206
|
-
|
|
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
|
-
|
|
189
|
+
return {
|
|
190
|
+
...temporalInvocationIdentity({
|
|
191
|
+
workflowId,
|
|
192
|
+
stepId
|
|
193
|
+
}),
|
|
194
|
+
causationId: parentInvocationId
|
|
195
|
+
};
|
|
211
196
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
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
|
-
|
|
219
|
-
|
|
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
|
-
|
|
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
|
-
|
|
217
|
+
return temporalInvocationIdentity({
|
|
218
|
+
workflowId: input.workflowId,
|
|
219
|
+
stepId: `compensate:${input.stepId}`
|
|
220
|
+
});
|
|
227
221
|
}
|
|
228
222
|
var SagaCompensationError = class extends Error {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
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
|
-
|
|
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
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
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
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
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
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
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
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
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
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
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
|