@gobing-ai/ts-dual-workflow-engine 0.5.14 → 0.5.16
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/README.md +53 -22
- package/dist/action-step.d.ts +1 -0
- package/dist/action-step.d.ts.map +1 -1
- package/dist/action-step.js +1 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +137 -3
- package/dist/dag.d.ts +25 -0
- package/dist/dag.d.ts.map +1 -0
- package/dist/dag.js +215 -0
- package/dist/events.d.ts +33 -0
- package/dist/events.d.ts.map +1 -1
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +4 -3
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/persistence.d.ts +24 -1
- package/dist/persistence.d.ts.map +1 -1
- package/dist/persistence.js +91 -0
- package/dist/run-lifecycle.d.ts +8 -0
- package/dist/run-lifecycle.d.ts.map +1 -1
- package/dist/run-lifecycle.js +71 -2
- package/dist/schema-sql.d.ts.map +1 -1
- package/dist/schema-sql.js +20 -0
- package/dist/schema.d.ts +185 -1
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +105 -14
- package/dist/service.d.ts +8 -0
- package/dist/service.d.ts.map +1 -1
- package/dist/service.js +55 -3
- package/dist/state-machine.d.ts.map +1 -1
- package/dist/state-machine.js +8 -4
- package/dist/transition-flow.d.ts.map +1 -1
- package/dist/transition-flow.js +199 -4
- package/dist/types.d.ts +109 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/schemas/dag-workflow.schema.json +93 -0
- package/schemas/transition-flow-workflow.schema.json +34 -0
- package/src/action-step.ts +2 -0
- package/src/config.ts +152 -5
- package/src/dag.ts +297 -0
- package/src/events.ts +33 -0
- package/src/host.ts +4 -2
- package/src/index.ts +6 -0
- package/src/persistence.ts +155 -0
- package/src/run-lifecycle.ts +75 -2
- package/src/schema-sql.ts +20 -0
- package/src/schema.ts +108 -16
- package/src/service.ts +72 -7
- package/src/state-machine.ts +8 -3
- package/src/transition-flow.ts +279 -3
- package/src/types.ts +128 -2
package/src/transition-flow.ts
CHANGED
|
@@ -4,7 +4,10 @@ import type { WorkflowEngineHost } from './host';
|
|
|
4
4
|
import { allowedEnv, RunLifecycle, snapshotActionResult, snapshotTransitions } from './run-lifecycle';
|
|
5
5
|
import type {
|
|
6
6
|
ActionResult,
|
|
7
|
+
FlowNodeDef,
|
|
8
|
+
FlowParallelNodeDef,
|
|
7
9
|
TransitionFlowWorkflowDef,
|
|
10
|
+
Vars,
|
|
8
11
|
WorkflowPersistenceAdapter,
|
|
9
12
|
WorkflowRunOptions,
|
|
10
13
|
WorkflowRunResult,
|
|
@@ -28,7 +31,7 @@ export class TransitionFlowDriver {
|
|
|
28
31
|
'transition-flow',
|
|
29
32
|
{ persistence: this.options.persistence, events: options.events },
|
|
30
33
|
options,
|
|
31
|
-
(lifecycle) => this.loop(workflow, options, lifecycle),
|
|
34
|
+
(lifecycle) => this.loop(workflow, options, lifecycle, undefined, options.startState),
|
|
32
35
|
);
|
|
33
36
|
}
|
|
34
37
|
|
|
@@ -56,13 +59,18 @@ export class TransitionFlowDriver {
|
|
|
56
59
|
options: WorkflowRunOptions,
|
|
57
60
|
lifecycle: RunLifecycle,
|
|
58
61
|
resumeFromNode?: string,
|
|
62
|
+
startAtNode?: string,
|
|
59
63
|
): Promise<WorkflowRunResult> {
|
|
60
64
|
const runId = lifecycle.runId;
|
|
61
65
|
const nodes = new Map(workflow.nodes.map((node) => [node.id, node]));
|
|
62
66
|
const terminal = new Set(workflow.terminalNodes ?? []);
|
|
63
67
|
let vars = mergeVars(workflow.vars, options.vars);
|
|
64
68
|
const env = allowedEnv(workflow.env?.allow ?? [], options.env);
|
|
65
|
-
|
|
69
|
+
// `startAtNode` is a FRESH-run start point, so it deliberately does not enter the
|
|
70
|
+
// resume branch below: no snapshot load, no `resumeMode`, the node action executes
|
|
71
|
+
// (task 0102 R1).
|
|
72
|
+
const entryNode = resumeFromNode ?? startAtNode;
|
|
73
|
+
let current = entryNode !== undefined ? nodes.get(entryNode) : nodes.get(workflow.initialNode);
|
|
66
74
|
const snapshot =
|
|
67
75
|
resumeFromNode === undefined ? undefined : await this.options.persistence.loadLatestStateSnapshot(runId);
|
|
68
76
|
let transitionsTaken = snapshotTransitions(snapshot?.data);
|
|
@@ -76,7 +84,7 @@ export class TransitionFlowDriver {
|
|
|
76
84
|
let persistedViaHop = false;
|
|
77
85
|
|
|
78
86
|
if (current === undefined) {
|
|
79
|
-
const label =
|
|
87
|
+
const label = entryNode ?? workflow.initialNode;
|
|
80
88
|
throw new FSMError(`Node "${label}" is not declared`);
|
|
81
89
|
}
|
|
82
90
|
|
|
@@ -120,6 +128,7 @@ export class TransitionFlowDriver {
|
|
|
120
128
|
env,
|
|
121
129
|
options,
|
|
122
130
|
defaultOnError,
|
|
131
|
+
signal: options.signal,
|
|
123
132
|
});
|
|
124
133
|
lastActionResult = step.result;
|
|
125
134
|
if (step.result?.setVars) vars = mergeSetVars(vars, step.result.setVars);
|
|
@@ -137,6 +146,250 @@ export class TransitionFlowDriver {
|
|
|
137
146
|
}
|
|
138
147
|
}
|
|
139
148
|
|
|
149
|
+
// Structured parallel fork-join execution
|
|
150
|
+
if (current.type === 'parallel') {
|
|
151
|
+
if (!current.branches || !current.join) {
|
|
152
|
+
throw new FSMError(`Parallel node "${current.id}" must declare branches and join target`);
|
|
153
|
+
}
|
|
154
|
+
const parallelNode = current as FlowParallelNodeDef;
|
|
155
|
+
const concurrency = parallelNode.concurrencyLimit ?? 4;
|
|
156
|
+
|
|
157
|
+
if (!options.dryRun) {
|
|
158
|
+
const existingBranches = await this.options.persistence.listRunBranches(runId, parallelNode.id);
|
|
159
|
+
const branchControllers = new Map<string, AbortController>();
|
|
160
|
+
for (const b of parallelNode.branches) {
|
|
161
|
+
const ctrl = new AbortController();
|
|
162
|
+
branchControllers.set(b.id, ctrl);
|
|
163
|
+
if (options.signal) {
|
|
164
|
+
if (options.signal.aborted) ctrl.abort();
|
|
165
|
+
else options.signal.addEventListener('abort', () => ctrl.abort(), { once: true });
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const abortSiblings = (failingBranchId: string) => {
|
|
170
|
+
if (parallelNode.failurePolicy === 'fail-fast') {
|
|
171
|
+
for (const [id, ctrl] of branchControllers.entries()) {
|
|
172
|
+
if (id !== failingBranchId) ctrl.abort();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const branchResults = await runWithConcurrencyLimit(
|
|
178
|
+
parallelNode.branches,
|
|
179
|
+
concurrency,
|
|
180
|
+
async (branch) => {
|
|
181
|
+
const existing = existingBranches.find((b) => b.branch_id === branch.id);
|
|
182
|
+
if (existing?.status === 'done') {
|
|
183
|
+
let restoredVars: Vars = {};
|
|
184
|
+
if (existing.output_vars_json) {
|
|
185
|
+
try {
|
|
186
|
+
restoredVars = JSON.parse(existing.output_vars_json);
|
|
187
|
+
} catch {
|
|
188
|
+
restoredVars = {};
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return { ok: true, branchId: branch.id, setVars: restoredVars };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const branchCtrl = branchControllers.get(branch.id) ?? new AbortController();
|
|
195
|
+
const branchStartMs = Date.now();
|
|
196
|
+
let branchCurrent: FlowNodeDef | undefined;
|
|
197
|
+
let skipEnter = false;
|
|
198
|
+
|
|
199
|
+
if (existing?.status === 'paused') {
|
|
200
|
+
branchCurrent = nodes.get(existing.node) ?? nodes.get(branch.startNode);
|
|
201
|
+
skipEnter = (resumeMode ?? options.resumeMode ?? 'skip-enter') === 'skip-enter';
|
|
202
|
+
} else {
|
|
203
|
+
branchCurrent = nodes.get(branch.startNode);
|
|
204
|
+
await this.options.persistence.saveBranchStart(
|
|
205
|
+
runId,
|
|
206
|
+
parallelNode.id,
|
|
207
|
+
branch.id,
|
|
208
|
+
branch.startNode,
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
lifecycle.branchStarted(parallelNode.id, branch.id, branchCurrent?.id ?? branch.startNode);
|
|
213
|
+
|
|
214
|
+
let branchVars = { ...vars };
|
|
215
|
+
let branchSetVars: Vars = {};
|
|
216
|
+
|
|
217
|
+
while (branchCurrent && branchCurrent.id !== parallelNode.join) {
|
|
218
|
+
if (branchCtrl.signal.aborted) {
|
|
219
|
+
lifecycle.branchFailed(parallelNode.id, branch.id, 'cancelled');
|
|
220
|
+
await this.options.persistence.saveBranchFinalize(
|
|
221
|
+
runId,
|
|
222
|
+
branch.id,
|
|
223
|
+
'cancelled',
|
|
224
|
+
Date.now() - branchStartMs,
|
|
225
|
+
undefined,
|
|
226
|
+
'cancelled',
|
|
227
|
+
);
|
|
228
|
+
return { ok: false, branchId: branch.id, error: 'cancelled' };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (skipEnter) {
|
|
232
|
+
skipEnter = false;
|
|
233
|
+
} else {
|
|
234
|
+
if (branchCurrent.action !== undefined) {
|
|
235
|
+
const step = await runActionStep(branchCurrent.action, branchVars, {
|
|
236
|
+
host: this.options.host,
|
|
237
|
+
persistence: this.options.persistence,
|
|
238
|
+
lifecycle,
|
|
239
|
+
workflowName: workflow.name,
|
|
240
|
+
stateOrNodeId: branchCurrent.id,
|
|
241
|
+
runId,
|
|
242
|
+
mode: 'transition-flow',
|
|
243
|
+
transitionsTaken,
|
|
244
|
+
env,
|
|
245
|
+
options,
|
|
246
|
+
defaultOnError,
|
|
247
|
+
signal: branchCtrl.signal,
|
|
248
|
+
});
|
|
249
|
+
if (step.result?.setVars) {
|
|
250
|
+
branchVars = mergeSetVars(branchVars, step.result.setVars);
|
|
251
|
+
branchSetVars = mergeSetVars(branchSetVars, step.result.setVars);
|
|
252
|
+
}
|
|
253
|
+
if (branchCtrl.signal.aborted) {
|
|
254
|
+
lifecycle.branchFailed(parallelNode.id, branch.id, 'cancelled');
|
|
255
|
+
await this.options.persistence.saveBranchFinalize(
|
|
256
|
+
runId,
|
|
257
|
+
branch.id,
|
|
258
|
+
'cancelled',
|
|
259
|
+
Date.now() - branchStartMs,
|
|
260
|
+
undefined,
|
|
261
|
+
'cancelled',
|
|
262
|
+
);
|
|
263
|
+
return { ok: false, branchId: branch.id, error: 'cancelled' };
|
|
264
|
+
}
|
|
265
|
+
if (step.outcome === 'fail') {
|
|
266
|
+
abortSiblings(branch.id);
|
|
267
|
+
const err = step.result?.error ?? 'action-failed';
|
|
268
|
+
lifecycle.branchFailed(parallelNode.id, branch.id, err);
|
|
269
|
+
await this.options.persistence.saveBranchFinalize(
|
|
270
|
+
runId,
|
|
271
|
+
branch.id,
|
|
272
|
+
'failed',
|
|
273
|
+
Date.now() - branchStartMs,
|
|
274
|
+
undefined,
|
|
275
|
+
err,
|
|
276
|
+
);
|
|
277
|
+
return {
|
|
278
|
+
ok: false,
|
|
279
|
+
branchId: branch.id,
|
|
280
|
+
error: err,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
if (step.outcome === 'terminal') {
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (branchCurrent.pause === true) {
|
|
289
|
+
lifecycle.branchPaused(parallelNode.id, branch.id, branchCurrent.id);
|
|
290
|
+
await this.options.persistence.saveBranchFinalize(
|
|
291
|
+
runId,
|
|
292
|
+
branch.id,
|
|
293
|
+
'paused',
|
|
294
|
+
Date.now() - branchStartMs,
|
|
295
|
+
branchSetVars,
|
|
296
|
+
);
|
|
297
|
+
return {
|
|
298
|
+
ok: true,
|
|
299
|
+
branchId: branch.id,
|
|
300
|
+
paused: true,
|
|
301
|
+
setVars: branchSetVars,
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const outbound = workflow.edges.filter((e) => e.from === branchCurrent?.id);
|
|
307
|
+
if (outbound.length === 0 || terminal.has(branchCurrent.id)) {
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
const edge = await firstPassingEdge(
|
|
311
|
+
outbound,
|
|
312
|
+
this.options.host,
|
|
313
|
+
{
|
|
314
|
+
runId,
|
|
315
|
+
current: branchCurrent.id,
|
|
316
|
+
vars: branchVars,
|
|
317
|
+
env,
|
|
318
|
+
workdir: options.workdir,
|
|
319
|
+
signal: branchCtrl.signal,
|
|
320
|
+
},
|
|
321
|
+
lifecycle,
|
|
322
|
+
);
|
|
323
|
+
if (edge === undefined) {
|
|
324
|
+
abortSiblings(branch.id);
|
|
325
|
+
lifecycle.branchFailed(parallelNode.id, branch.id, 'no-passing-edge');
|
|
326
|
+
await this.options.persistence.saveBranchFinalize(
|
|
327
|
+
runId,
|
|
328
|
+
branch.id,
|
|
329
|
+
'failed',
|
|
330
|
+
Date.now() - branchStartMs,
|
|
331
|
+
undefined,
|
|
332
|
+
'no-passing-edge',
|
|
333
|
+
);
|
|
334
|
+
return { ok: false, branchId: branch.id, error: 'no-passing-edge' };
|
|
335
|
+
}
|
|
336
|
+
branchCurrent = nodes.get(edge.to);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (branchCtrl.signal.aborted) {
|
|
340
|
+
lifecycle.branchFailed(parallelNode.id, branch.id, 'cancelled');
|
|
341
|
+
await this.options.persistence.saveBranchFinalize(
|
|
342
|
+
runId,
|
|
343
|
+
branch.id,
|
|
344
|
+
'cancelled',
|
|
345
|
+
Date.now() - branchStartMs,
|
|
346
|
+
undefined,
|
|
347
|
+
'cancelled',
|
|
348
|
+
);
|
|
349
|
+
return { ok: false, branchId: branch.id, error: 'cancelled' };
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
lifecycle.branchDone(parallelNode.id, branch.id, Date.now() - branchStartMs, true);
|
|
353
|
+
await this.options.persistence.saveBranchFinalize(
|
|
354
|
+
runId,
|
|
355
|
+
branch.id,
|
|
356
|
+
'done',
|
|
357
|
+
Date.now() - branchStartMs,
|
|
358
|
+
branchSetVars,
|
|
359
|
+
);
|
|
360
|
+
return { ok: true, branchId: branch.id, setVars: branchSetVars };
|
|
361
|
+
},
|
|
362
|
+
);
|
|
363
|
+
|
|
364
|
+
const failed = branchResults.find((r) => !r.ok);
|
|
365
|
+
if (failed) {
|
|
366
|
+
return await lifecycle.fail(current.id, transitionsTaken, failed.error ?? 'branch-failed');
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const anyPaused = branchResults.some((r) => 'paused' in r && r.paused === true);
|
|
370
|
+
for (const res of branchResults) {
|
|
371
|
+
if (res.setVars) vars = mergeSetVars(vars, res.setVars);
|
|
372
|
+
}
|
|
373
|
+
if (anyPaused) {
|
|
374
|
+
return await lifecycle.pause(current.id, transitionsTaken, vars);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
transitionsTaken += 1;
|
|
379
|
+
await this.options.persistence.commitJoin(runId, current.id, parallelNode.join, vars, {
|
|
380
|
+
phase: parallelNode.join,
|
|
381
|
+
status: 'running',
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
const joinNode = nodes.get(parallelNode.join);
|
|
385
|
+
if (joinNode === undefined) {
|
|
386
|
+
throw new FSMError(`Join target "${parallelNode.join}" is not declared`);
|
|
387
|
+
}
|
|
388
|
+
current = joinNode;
|
|
389
|
+
persistedViaHop = true;
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
|
|
140
393
|
// 3. Stop when the node is terminal or no outgoing edge exists.
|
|
141
394
|
const outbound = workflow.edges.filter((edge) => edge.from === current?.id);
|
|
142
395
|
if (terminal.has(current.id) || outbound.length === 0) {
|
|
@@ -155,6 +408,7 @@ export class TransitionFlowDriver {
|
|
|
155
408
|
lastActionResult,
|
|
156
409
|
// Shell conditions must share the run workdir with actions (relative paths).
|
|
157
410
|
workdir: options.workdir,
|
|
411
|
+
signal: options.signal,
|
|
158
412
|
},
|
|
159
413
|
lifecycle,
|
|
160
414
|
);
|
|
@@ -216,3 +470,25 @@ async function firstPassingEdge(
|
|
|
216
470
|
}
|
|
217
471
|
return undefined;
|
|
218
472
|
}
|
|
473
|
+
|
|
474
|
+
/** Execute tasks concurrently with a maximum concurrency limit. */
|
|
475
|
+
async function runWithConcurrencyLimit<T, R>(
|
|
476
|
+
items: readonly T[],
|
|
477
|
+
limit: number,
|
|
478
|
+
fn: (item: T) => Promise<R>,
|
|
479
|
+
): Promise<R[]> {
|
|
480
|
+
const results: R[] = new Array(items.length);
|
|
481
|
+
let nextIndex = 0;
|
|
482
|
+
const workerCount = Math.max(1, Math.min(limit, items.length));
|
|
483
|
+
const workers = new Array(workerCount).fill(null).map(async () => {
|
|
484
|
+
while (nextIndex < items.length) {
|
|
485
|
+
const index = nextIndex++;
|
|
486
|
+
const item = items[index];
|
|
487
|
+
if (item !== undefined) {
|
|
488
|
+
results[index] = await fn(item);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
await Promise.all(workers);
|
|
493
|
+
return results;
|
|
494
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -78,6 +78,13 @@ export interface StateDef {
|
|
|
78
78
|
* before any action executes (task 0902 R1/R2).
|
|
79
79
|
*/
|
|
80
80
|
readonly resumeRerun?: boolean;
|
|
81
|
+
/**
|
|
82
|
+
* Author opt-in for a FRESH run to begin at this state via
|
|
83
|
+
* `WorkflowRunOptions.startState`. There is no workflow-level allow and no
|
|
84
|
+
* caller override: a run started mid-graph on a state that assumed earlier
|
|
85
|
+
* artifacts must fail loud rather than silently skip them (task 0102 R2).
|
|
86
|
+
*/
|
|
87
|
+
readonly startable?: boolean;
|
|
81
88
|
}
|
|
82
89
|
|
|
83
90
|
/** One transition in a state-machine workflow. */
|
|
@@ -117,6 +124,19 @@ export interface StateMachineWorkflowDef {
|
|
|
117
124
|
readonly extensions?: WorkflowExtensions;
|
|
118
125
|
}
|
|
119
126
|
|
|
127
|
+
/** Join synchronization policy for parallel nodes. */
|
|
128
|
+
export type JoinPolicy = 'all';
|
|
129
|
+
|
|
130
|
+
/** Failure handling policy across parallel branches. */
|
|
131
|
+
export type FailurePolicy = 'collect' | 'fail-fast';
|
|
132
|
+
|
|
133
|
+
/** One branch in a structured parallel fork node. */
|
|
134
|
+
export interface FlowParallelBranchDef {
|
|
135
|
+
readonly id: string;
|
|
136
|
+
readonly startNode: string;
|
|
137
|
+
readonly description?: string;
|
|
138
|
+
}
|
|
139
|
+
|
|
120
140
|
/** Transition-flow node definition. */
|
|
121
141
|
export interface FlowNodeDef {
|
|
122
142
|
readonly id: string;
|
|
@@ -128,6 +148,28 @@ export interface FlowNodeDef {
|
|
|
128
148
|
readonly pause?: boolean;
|
|
129
149
|
/** Author declaration that this node's action is safe to re-run after an interruption. See StateDef.resumeRerun. */
|
|
130
150
|
readonly resumeRerun?: boolean;
|
|
151
|
+
/** Author opt-in for a FRESH run to begin at this node via `WorkflowRunOptions.startState`. See StateDef.startable. */
|
|
152
|
+
readonly startable?: boolean;
|
|
153
|
+
/** Declared branches for parallel fork nodes (type: 'parallel'). */
|
|
154
|
+
readonly branches?: readonly FlowParallelBranchDef[];
|
|
155
|
+
/** Target join node ID where branches converge for parallel fork nodes. */
|
|
156
|
+
readonly join?: string;
|
|
157
|
+
/** Join synchronization policy for parallel nodes. Defaults to 'all'. */
|
|
158
|
+
readonly joinPolicy?: JoinPolicy;
|
|
159
|
+
/** Failure handling policy across parallel branches. Defaults to 'collect'. */
|
|
160
|
+
readonly failurePolicy?: FailurePolicy;
|
|
161
|
+
/** Maximum number of branches executed concurrently. Defaults to 4. */
|
|
162
|
+
readonly concurrencyLimit?: number;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Transition-flow parallel fork node definition. */
|
|
166
|
+
export interface FlowParallelNodeDef extends FlowNodeDef {
|
|
167
|
+
readonly type: 'parallel';
|
|
168
|
+
readonly branches: readonly FlowParallelBranchDef[];
|
|
169
|
+
readonly join: string;
|
|
170
|
+
readonly joinPolicy?: JoinPolicy;
|
|
171
|
+
readonly failurePolicy?: FailurePolicy;
|
|
172
|
+
readonly concurrencyLimit?: number;
|
|
131
173
|
}
|
|
132
174
|
|
|
133
175
|
/** Transition-flow edge definition. */
|
|
@@ -159,8 +201,37 @@ export interface TransitionFlowWorkflowDef {
|
|
|
159
201
|
readonly extensions?: WorkflowExtensions;
|
|
160
202
|
}
|
|
161
203
|
|
|
204
|
+
/** Dependency synchronization policy for DAG nodes. */
|
|
205
|
+
export type DependencyPolicy = 'all' | 'any';
|
|
206
|
+
|
|
207
|
+
/** Single node in a static dependency DAG workflow. */
|
|
208
|
+
export interface DagNodeDef {
|
|
209
|
+
readonly id: string;
|
|
210
|
+
readonly description?: string;
|
|
211
|
+
readonly action?: ActionDef;
|
|
212
|
+
readonly dependsOn?: readonly string[];
|
|
213
|
+
readonly dependencyPolicy?: DependencyPolicy;
|
|
214
|
+
readonly condition?: GuardDef;
|
|
215
|
+
readonly pause?: boolean;
|
|
216
|
+
readonly resumeRerun?: boolean;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** Static dependency DAG workflow definition. */
|
|
220
|
+
export interface DagWorkflowDef {
|
|
221
|
+
readonly kind: 'dag';
|
|
222
|
+
readonly name: string;
|
|
223
|
+
readonly version?: string;
|
|
224
|
+
readonly description?: string;
|
|
225
|
+
readonly iterationBound?: number;
|
|
226
|
+
readonly defaultOnError?: OnErrorPolicy;
|
|
227
|
+
readonly vars?: Vars;
|
|
228
|
+
readonly env?: Env;
|
|
229
|
+
readonly nodes: readonly DagNodeDef[];
|
|
230
|
+
readonly extensions?: WorkflowExtensions;
|
|
231
|
+
}
|
|
232
|
+
|
|
162
233
|
/** Discriminated workflow definition union. */
|
|
163
|
-
export type WorkflowDef = StateMachineWorkflowDef | TransitionFlowWorkflowDef;
|
|
234
|
+
export type WorkflowDef = StateMachineWorkflowDef | TransitionFlowWorkflowDef | DagWorkflowDef;
|
|
164
235
|
|
|
165
236
|
/** Action execution context passed to action runners. */
|
|
166
237
|
export interface ActionRunContext {
|
|
@@ -174,6 +245,8 @@ export interface ActionRunContext {
|
|
|
174
245
|
readonly metadata?: Record<string, unknown>;
|
|
175
246
|
/** Optional event bus for in-process run observability. Action runners can emit workflow events through this. */
|
|
176
247
|
readonly events?: EventBus<WorkflowEngineEvents>;
|
|
248
|
+
/** Optional cancellation signal for cooperative and process-group termination (task 0096). */
|
|
249
|
+
readonly signal?: AbortSignal;
|
|
177
250
|
}
|
|
178
251
|
|
|
179
252
|
/** Result of a single action execution. */
|
|
@@ -201,6 +274,8 @@ export interface GuardContext {
|
|
|
201
274
|
/** Run env (allowed-env filtered) — guards resolve `${env.X}` identically to actions (task 0087 R5). */
|
|
202
275
|
readonly env?: Record<string, string>;
|
|
203
276
|
readonly lastActionResult?: ActionResult;
|
|
277
|
+
/** Optional cancellation signal for cooperative guard evaluation (task 0096). */
|
|
278
|
+
readonly signal?: AbortSignal;
|
|
204
279
|
}
|
|
205
280
|
|
|
206
281
|
/** Rich guard evaluation result. Boolean guard runners remain supported for compatibility. */
|
|
@@ -224,6 +299,8 @@ export interface WorkflowRunOptions {
|
|
|
224
299
|
readonly metadata?: Record<string, unknown>;
|
|
225
300
|
/** Optional event bus for structured run observability. */
|
|
226
301
|
readonly events?: EventBus<WorkflowEngineEvents>;
|
|
302
|
+
/** Optional cancellation signal for aborting in-flight child processes and actions (task 0096). */
|
|
303
|
+
readonly signal?: AbortSignal;
|
|
227
304
|
/** Run-level error policy override. Lowest precedence; action-level wins. */
|
|
228
305
|
readonly onError?: OnErrorPolicy;
|
|
229
306
|
/**
|
|
@@ -237,6 +314,16 @@ export interface WorkflowRunOptions {
|
|
|
237
314
|
readonly dryRun?: boolean;
|
|
238
315
|
/** Optional caller-supplied external key, unique per workflow definition. */
|
|
239
316
|
readonly externalKey?: string;
|
|
317
|
+
/**
|
|
318
|
+
* Fresh-run start point: begin this run at the named state (state-machine) or
|
|
319
|
+
* node (transition-flow) instead of the definition's `initialState` /
|
|
320
|
+
* `initialNode`. Fresh-run semantics, NOT resume semantics — no snapshot is
|
|
321
|
+
* loaded, `resumeMode` is never set, and the start state's on-enter/node action
|
|
322
|
+
* EXECUTES. The target must be declared, non-terminal, non-failure, and marked
|
|
323
|
+
* `startable: true`; anything else is refused with `FSMError` before the run row
|
|
324
|
+
* is created. Unsupported for `kind: 'dag'` (task 0102 R1/R3).
|
|
325
|
+
*/
|
|
326
|
+
readonly startState?: string;
|
|
240
327
|
/**
|
|
241
328
|
* Resume semantics override (task 0902). Only meaningful on driver/service
|
|
242
329
|
* resume paths; fresh runs ignore it. Defaults derive from run status:
|
|
@@ -256,7 +343,7 @@ export interface WorkflowRunOptions {
|
|
|
256
343
|
export interface WorkflowRunResult {
|
|
257
344
|
readonly runId: string;
|
|
258
345
|
readonly workflowName: string;
|
|
259
|
-
readonly mode: 'state-machine' | 'transition-flow';
|
|
346
|
+
readonly mode: 'state-machine' | 'transition-flow' | 'dag';
|
|
260
347
|
readonly status: WorkflowStatus;
|
|
261
348
|
readonly finalState: string;
|
|
262
349
|
readonly transitionsTaken: number;
|
|
@@ -319,6 +406,24 @@ export interface TransitionDenied {
|
|
|
319
406
|
/** Discriminated union result for external transition requests. */
|
|
320
407
|
export type TransitionRequestResult = TransitionAllowed | TransitionDenied;
|
|
321
408
|
|
|
409
|
+
/** Execution status of an individual branch in a parallel region. */
|
|
410
|
+
export type BranchStatus = 'pending' | 'running' | 'done' | 'failed' | 'cancelled' | 'paused';
|
|
411
|
+
|
|
412
|
+
/** Persisted branch execution record. */
|
|
413
|
+
export interface WorkflowBranchRecord {
|
|
414
|
+
readonly id: string;
|
|
415
|
+
readonly run_id: string;
|
|
416
|
+
readonly parallel_node: string;
|
|
417
|
+
readonly branch_id: string;
|
|
418
|
+
readonly status: BranchStatus;
|
|
419
|
+
readonly node: string;
|
|
420
|
+
readonly started_at: string;
|
|
421
|
+
readonly completed_at: string | null;
|
|
422
|
+
readonly duration_ms: number | null;
|
|
423
|
+
readonly output_vars_json: string | null;
|
|
424
|
+
readonly error: string | null;
|
|
425
|
+
}
|
|
426
|
+
|
|
322
427
|
/** Persisted action run record — one row per action executed in a workflow run. */
|
|
323
428
|
export interface ActionRunRecord {
|
|
324
429
|
readonly id: string;
|
|
@@ -436,4 +541,25 @@ export interface WorkflowPersistenceAdapter {
|
|
|
436
541
|
* updated record, or undefined when the run is missing or not running.
|
|
437
542
|
*/
|
|
438
543
|
interruptRun(runId: string, reason: string): Promise<WorkflowRunRecord | undefined>;
|
|
544
|
+
/** Record the start of a branch execution in a parallel region. */
|
|
545
|
+
saveBranchStart(runId: string, parallelNode: string, branchId: string, startNode: string): Promise<string>;
|
|
546
|
+
/** Finalize a branch execution with terminal status, duration, output variables, and optional error. */
|
|
547
|
+
saveBranchFinalize(
|
|
548
|
+
runId: string,
|
|
549
|
+
branchId: string,
|
|
550
|
+
status: BranchStatus,
|
|
551
|
+
durationMs: number,
|
|
552
|
+
outputVars?: Vars,
|
|
553
|
+
error?: string,
|
|
554
|
+
): Promise<void>;
|
|
555
|
+
/** List all branch records for a run, optionally filtered by parallel node ID. */
|
|
556
|
+
listRunBranches(runId: string, parallelNode?: string): Promise<readonly WorkflowBranchRecord[]>;
|
|
557
|
+
/** Atomically transition a parallel region to its join node and commit merged variables. */
|
|
558
|
+
commitJoin(
|
|
559
|
+
runId: string,
|
|
560
|
+
parallelNode: string,
|
|
561
|
+
joinNode: string,
|
|
562
|
+
mergedVars?: Vars,
|
|
563
|
+
phase?: { phase: string; status: WorkflowStatus },
|
|
564
|
+
): Promise<void>;
|
|
439
565
|
}
|