@gobing-ai/ts-dual-workflow-engine 0.5.17 → 0.5.18
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 +18 -0
- package/dist/config.js +1 -1
- package/dist/dag.d.ts +30 -1
- package/dist/dag.d.ts.map +1 -1
- package/dist/dag.js +390 -116
- package/dist/persistence.d.ts +16 -6
- package/dist/persistence.d.ts.map +1 -1
- package/dist/persistence.js +84 -18
- package/dist/run-lifecycle.d.ts +2 -0
- package/dist/run-lifecycle.d.ts.map +1 -1
- package/dist/run-lifecycle.js +4 -0
- package/dist/service.d.ts.map +1 -1
- package/dist/service.js +33 -7
- package/dist/transition-flow.d.ts.map +1 -1
- package/dist/transition-flow.js +111 -40
- package/dist/types.d.ts +7 -3
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/config.ts +1 -1
- package/src/dag.ts +443 -138
- package/src/persistence.ts +119 -15
- package/src/run-lifecycle.ts +5 -0
- package/src/service.ts +31 -7
- package/src/transition-flow.ts +147 -36
- package/src/types.ts +11 -1
package/src/persistence.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DbAdapter, DbBatchOp } from '@gobing-ai/ts-db';
|
|
2
|
-
import { RunCollisionError } from './errors';
|
|
2
|
+
import { RunCollisionError, WorkflowResumeError } from './errors';
|
|
3
3
|
import { WORKFLOW_ENGINE_MIGRATIONS_SQL, WORKFLOW_ENGINE_SCHEMA_SQL } from './schema-sql';
|
|
4
4
|
import type {
|
|
5
5
|
ActionRedactor,
|
|
@@ -413,15 +413,24 @@ export class DbWorkflowPersistenceAdapter implements WorkflowPersistenceAdapter
|
|
|
413
413
|
}
|
|
414
414
|
|
|
415
415
|
/** Record the start of a branch execution in a parallel region. */
|
|
416
|
-
async saveBranchStart(
|
|
416
|
+
async saveBranchStart(
|
|
417
|
+
runId: string,
|
|
418
|
+
parallelNode: string,
|
|
419
|
+
branchId: string,
|
|
420
|
+
startNode: string,
|
|
421
|
+
ownerAttempt?: string,
|
|
422
|
+
): Promise<string> {
|
|
423
|
+
await this.assertBranchOwner(runId, ownerAttempt);
|
|
417
424
|
await this.ensureSchema();
|
|
418
425
|
const id = crypto.randomUUID();
|
|
419
426
|
const now = Date.now();
|
|
420
427
|
await this.db.run(
|
|
421
428
|
`INSERT INTO workflow_branches (id, run_id, parallel_node, branch_id, status, node, started_at, created_at, updated_at)
|
|
422
|
-
|
|
429
|
+
SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?
|
|
430
|
+
WHERE (? IS NULL OR EXISTS (SELECT 1 FROM runs WHERE id = ? AND owner_attempt = ? AND status = 'running'))
|
|
423
431
|
ON CONFLICT(run_id, parallel_node, branch_id) DO UPDATE SET
|
|
424
|
-
status = 'running', node = excluded.node, started_at = excluded.started_at, completed_at = NULL, error = NULL, updated_at = excluded.updated_at
|
|
432
|
+
status = 'running', node = excluded.node, started_at = excluded.started_at, completed_at = NULL, error = NULL, updated_at = excluded.updated_at
|
|
433
|
+
WHERE (? IS NULL OR EXISTS (SELECT 1 FROM runs WHERE id = ? AND owner_attempt = ? AND status = 'running'))`,
|
|
425
434
|
id,
|
|
426
435
|
runId,
|
|
427
436
|
parallelNode,
|
|
@@ -431,8 +440,16 @@ export class DbWorkflowPersistenceAdapter implements WorkflowPersistenceAdapter
|
|
|
431
440
|
new Date(now).toISOString(),
|
|
432
441
|
now,
|
|
433
442
|
now,
|
|
443
|
+
ownerAttempt ?? null,
|
|
444
|
+
runId,
|
|
445
|
+
ownerAttempt ?? null,
|
|
446
|
+
ownerAttempt ?? null,
|
|
447
|
+
runId,
|
|
448
|
+
ownerAttempt ?? null,
|
|
434
449
|
);
|
|
435
|
-
|
|
450
|
+
await this.assertBranchOwner(runId, ownerAttempt);
|
|
451
|
+
const branches = await this.listRunBranches(runId, parallelNode);
|
|
452
|
+
return branches.find((branch) => branch.branch_id === branchId)?.id ?? id;
|
|
436
453
|
}
|
|
437
454
|
|
|
438
455
|
/** Finalize a branch execution with terminal status, duration, output variables, and optional error. */
|
|
@@ -443,22 +460,32 @@ export class DbWorkflowPersistenceAdapter implements WorkflowPersistenceAdapter
|
|
|
443
460
|
durationMs: number,
|
|
444
461
|
outputVars?: Vars,
|
|
445
462
|
error?: string,
|
|
463
|
+
checkpoint?: { readonly parallelNode: string; readonly node: string; readonly ownerAttempt?: string },
|
|
446
464
|
): Promise<void> {
|
|
465
|
+
await this.assertBranchOwner(runId, checkpoint?.ownerAttempt);
|
|
447
466
|
await this.ensureSchema();
|
|
448
467
|
const now = Date.now();
|
|
449
468
|
await this.db.run(
|
|
450
469
|
`UPDATE workflow_branches
|
|
451
|
-
SET status = ?, duration_ms = ?, output_vars_json = ?, error = ?, completed_at = ?, updated_at =
|
|
452
|
-
WHERE run_id = ? AND branch_id =
|
|
470
|
+
SET status = ?, duration_ms = ?, output_vars_json = ?, error = ?, completed_at = ?, updated_at = ?, node = COALESCE(?, node)
|
|
471
|
+
WHERE run_id = ? AND branch_id = ? AND (? IS NULL OR parallel_node = ?)
|
|
472
|
+
AND (? IS NULL OR EXISTS (SELECT 1 FROM runs WHERE id = ? AND owner_attempt = ? AND status = 'running'))`,
|
|
453
473
|
status,
|
|
454
474
|
durationMs,
|
|
455
475
|
outputVars !== undefined ? JSON.stringify(outputVars) : null,
|
|
456
476
|
error ?? null,
|
|
457
|
-
new Date(now).toISOString(),
|
|
477
|
+
status === 'running' ? null : new Date(now).toISOString(),
|
|
458
478
|
now,
|
|
479
|
+
checkpoint?.node ?? null,
|
|
459
480
|
runId,
|
|
460
481
|
branchId,
|
|
482
|
+
checkpoint?.parallelNode ?? null,
|
|
483
|
+
checkpoint?.parallelNode ?? null,
|
|
484
|
+
checkpoint?.ownerAttempt ?? null,
|
|
485
|
+
runId,
|
|
486
|
+
checkpoint?.ownerAttempt ?? null,
|
|
461
487
|
);
|
|
488
|
+
await this.assertBranchOwner(runId, checkpoint?.ownerAttempt);
|
|
462
489
|
}
|
|
463
490
|
|
|
464
491
|
/** List all branch records for a run, optionally filtered by parallel node ID. */
|
|
@@ -484,11 +511,58 @@ export class DbWorkflowPersistenceAdapter implements WorkflowPersistenceAdapter
|
|
|
484
511
|
joinNode: string,
|
|
485
512
|
mergedVars?: Vars,
|
|
486
513
|
phase?: { phase: string; status: WorkflowStatus },
|
|
514
|
+
ownerAttempt?: string,
|
|
515
|
+
transitionsTaken?: number,
|
|
516
|
+
collectedFailure?: string,
|
|
487
517
|
): Promise<void> {
|
|
488
|
-
await this.
|
|
489
|
-
const data: Record<string, unknown> = {};
|
|
518
|
+
await this.assertBranchOwner(runId, ownerAttempt);
|
|
519
|
+
const data: Record<string, unknown> = { transitionsTaken };
|
|
520
|
+
if (collectedFailure !== undefined) data.collectedFailure = collectedFailure;
|
|
490
521
|
if (mergedVars !== undefined) data.effectiveVars = mergedVars;
|
|
491
|
-
|
|
522
|
+
const now = Date.now();
|
|
523
|
+
const branches = await this.listRunBranches(runId, parallelNode);
|
|
524
|
+
const ops: DbBatchOp[] = branches.map((branch) => ({
|
|
525
|
+
sql: `UPDATE workflow_branches SET status = ?, node = ?, output_vars_json = ?, error = ?, duration_ms = ?, completed_at = ?, updated_at = ? WHERE id = ?`,
|
|
526
|
+
params: [
|
|
527
|
+
branch.status,
|
|
528
|
+
branch.node,
|
|
529
|
+
branch.output_vars_json,
|
|
530
|
+
branch.error,
|
|
531
|
+
branch.duration_ms,
|
|
532
|
+
branch.completed_at,
|
|
533
|
+
now,
|
|
534
|
+
branch.id,
|
|
535
|
+
],
|
|
536
|
+
}));
|
|
537
|
+
ops.push(
|
|
538
|
+
this.transitionRow(runId, parallelNode, joinNode, '__join__', now),
|
|
539
|
+
this.stateRow(runId, joinNode, data, now),
|
|
540
|
+
);
|
|
541
|
+
if (phase) ops.push(this.phaseRow(runId, phase.phase, phase.status, now));
|
|
542
|
+
// Each mutation carries the fence inside the transaction; a stale owner cannot
|
|
543
|
+
// rewrite ledger outcomes or activate a join after another attempt claims the run.
|
|
544
|
+
const fenced =
|
|
545
|
+
ownerAttempt === undefined
|
|
546
|
+
? ops
|
|
547
|
+
: ops.map((op) => ({
|
|
548
|
+
sql: op.sql.startsWith('INSERT')
|
|
549
|
+
? op.sql.replace(
|
|
550
|
+
/VALUES \(([^)]*)\)/,
|
|
551
|
+
"SELECT $1 WHERE EXISTS (SELECT 1 FROM runs WHERE id = ? AND owner_attempt = ? AND status = 'running')",
|
|
552
|
+
)
|
|
553
|
+
: `${op.sql} AND EXISTS (SELECT 1 FROM runs WHERE id = ? AND owner_attempt = ? AND status = 'running')`,
|
|
554
|
+
params: [...op.params, runId, ownerAttempt],
|
|
555
|
+
}));
|
|
556
|
+
await this.db.batch(fenced);
|
|
557
|
+
await this.assertBranchOwner(runId, ownerAttempt);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
private async assertBranchOwner(runId: string, ownerAttempt?: string): Promise<void> {
|
|
561
|
+
if (ownerAttempt === undefined) return;
|
|
562
|
+
const run = await this.loadRun(runId);
|
|
563
|
+
if (run?.status !== 'running' || run.owner_attempt !== ownerAttempt) {
|
|
564
|
+
throw new WorkflowResumeError(`Stale branch owner "${ownerAttempt}" for run "${runId}"`);
|
|
565
|
+
}
|
|
492
566
|
}
|
|
493
567
|
}
|
|
494
568
|
|
|
@@ -720,7 +794,14 @@ export class MemoryWorkflowPersistenceAdapter implements WorkflowPersistenceAdap
|
|
|
720
794
|
readonly branches = new Map<string, WorkflowBranchRecord>();
|
|
721
795
|
|
|
722
796
|
/** Record the start of a branch execution in a parallel region. */
|
|
723
|
-
async saveBranchStart(
|
|
797
|
+
async saveBranchStart(
|
|
798
|
+
runId: string,
|
|
799
|
+
parallelNode: string,
|
|
800
|
+
branchId: string,
|
|
801
|
+
startNode: string,
|
|
802
|
+
ownerAttempt?: string,
|
|
803
|
+
): Promise<string> {
|
|
804
|
+
this.assertBranchOwner(runId, ownerAttempt);
|
|
724
805
|
const id = crypto.randomUUID();
|
|
725
806
|
const now = Date.now();
|
|
726
807
|
const key = `${runId}:${parallelNode}:${branchId}`;
|
|
@@ -749,15 +830,22 @@ export class MemoryWorkflowPersistenceAdapter implements WorkflowPersistenceAdap
|
|
|
749
830
|
durationMs: number,
|
|
750
831
|
outputVars?: Vars,
|
|
751
832
|
error?: string,
|
|
833
|
+
checkpoint?: { readonly parallelNode: string; readonly node: string; readonly ownerAttempt?: string },
|
|
752
834
|
): Promise<void> {
|
|
835
|
+
this.assertBranchOwner(runId, checkpoint?.ownerAttempt);
|
|
753
836
|
const now = Date.now();
|
|
754
837
|
for (const [key, b] of this.branches.entries()) {
|
|
755
|
-
if (
|
|
838
|
+
if (
|
|
839
|
+
b.run_id === runId &&
|
|
840
|
+
b.branch_id === branchId &&
|
|
841
|
+
(checkpoint === undefined || b.parallel_node === checkpoint.parallelNode)
|
|
842
|
+
) {
|
|
756
843
|
this.branches.set(key, {
|
|
757
844
|
...b,
|
|
845
|
+
node: checkpoint?.node ?? b.node,
|
|
758
846
|
status,
|
|
759
847
|
duration_ms: durationMs,
|
|
760
|
-
completed_at: new Date(now).toISOString(),
|
|
848
|
+
completed_at: status === 'running' ? null : new Date(now).toISOString(),
|
|
761
849
|
output_vars_json: outputVars !== undefined ? JSON.stringify(outputVars) : null,
|
|
762
850
|
error: error ?? null,
|
|
763
851
|
});
|
|
@@ -784,9 +872,25 @@ export class MemoryWorkflowPersistenceAdapter implements WorkflowPersistenceAdap
|
|
|
784
872
|
joinNode: string,
|
|
785
873
|
mergedVars?: Vars,
|
|
786
874
|
phase?: { phase: string; status: WorkflowStatus },
|
|
875
|
+
ownerAttempt?: string,
|
|
876
|
+
transitionsTaken?: number,
|
|
877
|
+
collectedFailure?: string,
|
|
787
878
|
): Promise<void> {
|
|
788
|
-
const
|
|
879
|
+
const run = this.runs.get(runId);
|
|
880
|
+
if (ownerAttempt !== undefined && (run?.status !== 'running' || run.owner_attempt !== ownerAttempt)) {
|
|
881
|
+
throw new WorkflowResumeError(`Stale branch owner "${ownerAttempt}" for run "${runId}"`);
|
|
882
|
+
}
|
|
883
|
+
const data: Record<string, unknown> = { transitionsTaken };
|
|
884
|
+
if (collectedFailure !== undefined) data.collectedFailure = collectedFailure;
|
|
789
885
|
if (mergedVars !== undefined) data.effectiveVars = mergedVars;
|
|
790
886
|
await this.commitTransition(runId, parallelNode, joinNode, '__join__', joinNode, data, phase);
|
|
791
887
|
}
|
|
888
|
+
|
|
889
|
+
private assertBranchOwner(runId: string, ownerAttempt?: string): void {
|
|
890
|
+
if (ownerAttempt === undefined) return;
|
|
891
|
+
const run = this.runs.get(runId);
|
|
892
|
+
if (run?.status !== 'running' || run.owner_attempt !== ownerAttempt) {
|
|
893
|
+
throw new WorkflowResumeError(`Stale branch owner "${ownerAttempt}" for run "${runId}"`);
|
|
894
|
+
}
|
|
895
|
+
}
|
|
792
896
|
}
|
package/src/run-lifecycle.ts
CHANGED
|
@@ -90,6 +90,11 @@ export class RunLifecycle {
|
|
|
90
90
|
/** Owner attempt recorded on the run row; undefined for lifecycles that never claimed a run (external transitions, direct construction). Gates the finalize fence (task 0086 AC11). */
|
|
91
91
|
private ownerAttempt: string | undefined;
|
|
92
92
|
|
|
93
|
+
/** Owner fence for branch ledger writes and join commits. */
|
|
94
|
+
get ownerAttemptId(): string | undefined {
|
|
95
|
+
return this.ownerAttempt;
|
|
96
|
+
}
|
|
97
|
+
|
|
93
98
|
private constructor(
|
|
94
99
|
runId: string,
|
|
95
100
|
private readonly workflowName: string,
|
package/src/service.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type BusLifecycleEvents, EventBus } from '@gobing-ai/ts-infra';
|
|
2
2
|
import { loadWorkflowDef } from './config';
|
|
3
|
-
import { DagDriver } from './dag';
|
|
3
|
+
import { assertDagResumeAllowed, DagDriver } from './dag';
|
|
4
4
|
import { FSMError, WorkflowResumeError } from './errors';
|
|
5
5
|
import type { WorkflowEngineEvents } from './events';
|
|
6
6
|
import type { WorkflowEngineHost } from './host';
|
|
@@ -221,10 +221,8 @@ export class WorkflowService {
|
|
|
221
221
|
if (run.status !== 'paused' && run.status !== 'interrupted') {
|
|
222
222
|
throw new WorkflowResumeError(`Run "${runId}" is not resumable (status: ${run.status})`);
|
|
223
223
|
}
|
|
224
|
-
const
|
|
225
|
-
|
|
226
|
-
throw new WorkflowResumeError(`Run "${runId}" has no persisted state to resume from`);
|
|
227
|
-
}
|
|
224
|
+
const snapshotState = await this.persistence.loadCurrentState(runId);
|
|
225
|
+
let currentState: string;
|
|
228
226
|
|
|
229
227
|
// Resolve recovery semantics (task 0902): interrupted runs default to re-running
|
|
230
228
|
// the current state's actions (they may have half-completed at the interruption);
|
|
@@ -232,14 +230,40 @@ export class WorkflowService {
|
|
|
232
230
|
// An explicit `resumeMode` option overrides either default.
|
|
233
231
|
const resumeMode: WorkflowResumeMode =
|
|
234
232
|
options?.resumeMode ?? (run.status === 'interrupted' ? 'rerun-enter' : 'skip-enter');
|
|
235
|
-
|
|
233
|
+
if (workflow.kind === 'dag') {
|
|
234
|
+
// Task 0104 Design 6: DAG runs admit via the per-node ledger — the
|
|
235
|
+
// recovery source for interrupted runs without a snapshot — so the
|
|
236
|
+
// generic no-snapshot refusal and anchor-only rerun check do not apply.
|
|
237
|
+
const admission = await assertDagResumeAllowed(workflow, runId, this.persistence, resumeMode);
|
|
238
|
+
currentState = snapshotState ?? admission.anchor;
|
|
239
|
+
} else {
|
|
240
|
+
if (snapshotState === undefined) {
|
|
241
|
+
throw new WorkflowResumeError(`Run "${runId}" has no persisted state to resume from`);
|
|
242
|
+
}
|
|
243
|
+
currentState = snapshotState;
|
|
244
|
+
this.assertResumeRerunAllowed(workflow, currentState, resumeMode);
|
|
245
|
+
// 9d56aa83: a rerun-enter resume of a fork-join parallel node must
|
|
246
|
+
// also clear the rerun guard for each not-yet-done branch's start node.
|
|
247
|
+
if (workflow.kind === 'transition-flow' && resumeMode === 'rerun-enter') {
|
|
248
|
+
const parallel = workflow.nodes.find((node) => node.id === currentState && node.type === 'parallel');
|
|
249
|
+
if (parallel) {
|
|
250
|
+
const branches = await this.persistence.listRunBranches(runId, parallel.id);
|
|
251
|
+
for (const branch of branches) {
|
|
252
|
+
if (branch.status === 'done') continue;
|
|
253
|
+
this.assertResumeRerunAllowed(workflow, branch.node, resumeMode);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
236
258
|
|
|
237
259
|
// Restore effectiveVars persisted in the last state snapshot so resume
|
|
238
260
|
// continues with the same runtime variables (e.g. `__hitlAnswer`). Caller
|
|
239
261
|
// overrides in `options.vars` win over the persisted snapshot (R3 of 0366).
|
|
262
|
+
// Task 0104 Design 8: DAG runs pass caller vars through un-merged — the
|
|
263
|
+
// DAG driver owns the snapshot → ledger-delta → caller precedence.
|
|
240
264
|
const snapshot = await this.persistence.loadLatestStateSnapshot(runId);
|
|
241
265
|
const persistedVars = extractEffectiveVars(snapshot?.data);
|
|
242
|
-
const restoredVars = mergeVars(persistedVars, options?.vars);
|
|
266
|
+
const restoredVars = workflow.kind === 'dag' ? options?.vars : mergeVars(persistedVars, options?.vars);
|
|
243
267
|
const owner: ResumeOwnership = options?.resumeOwner ?? { attemptId: crypto.randomUUID() };
|
|
244
268
|
const mergedOptions: WorkflowRunOptions = { ...options, vars: restoredVars, resumeMode, resumeOwner: owner };
|
|
245
269
|
|
package/src/transition-flow.ts
CHANGED
|
@@ -82,6 +82,8 @@ export class TransitionFlowDriver {
|
|
|
82
82
|
// phase atomically (every iteration after the first), `enter` must skip
|
|
83
83
|
// the persist half to avoid duplicate INSERT rows (ADR-020).
|
|
84
84
|
let persistedViaHop = false;
|
|
85
|
+
let collectedFailure =
|
|
86
|
+
typeof snapshot?.data.collectedFailure === 'string' ? snapshot.data.collectedFailure : undefined;
|
|
85
87
|
|
|
86
88
|
if (current === undefined) {
|
|
87
89
|
const label = entryNode ?? workflow.initialNode;
|
|
@@ -132,7 +134,7 @@ export class TransitionFlowDriver {
|
|
|
132
134
|
});
|
|
133
135
|
lastActionResult = step.result;
|
|
134
136
|
if (step.result?.setVars) vars = mergeSetVars(vars, step.result.setVars);
|
|
135
|
-
if (step.outcome === 'terminal') {
|
|
137
|
+
if (step.outcome === 'terminal' && collectedFailure === undefined) {
|
|
136
138
|
return await lifecycle.done(current.id, transitionsTaken);
|
|
137
139
|
}
|
|
138
140
|
if (step.outcome === 'fail') {
|
|
@@ -146,6 +148,10 @@ export class TransitionFlowDriver {
|
|
|
146
148
|
}
|
|
147
149
|
}
|
|
148
150
|
|
|
151
|
+
if (collectedFailure !== undefined) {
|
|
152
|
+
return await lifecycle.fail(current.id, transitionsTaken, collectedFailure);
|
|
153
|
+
}
|
|
154
|
+
|
|
149
155
|
// Structured parallel fork-join execution
|
|
150
156
|
if (current.type === 'parallel') {
|
|
151
157
|
if (!current.branches || !current.join) {
|
|
@@ -160,10 +166,6 @@ export class TransitionFlowDriver {
|
|
|
160
166
|
for (const b of parallelNode.branches) {
|
|
161
167
|
const ctrl = new AbortController();
|
|
162
168
|
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
169
|
}
|
|
168
170
|
|
|
169
171
|
const abortSiblings = (failingBranchId: string) => {
|
|
@@ -192,13 +194,21 @@ export class TransitionFlowDriver {
|
|
|
192
194
|
}
|
|
193
195
|
|
|
194
196
|
const branchCtrl = branchControllers.get(branch.id) ?? new AbortController();
|
|
197
|
+
const branchSignal = options.signal
|
|
198
|
+
? AbortSignal.any([options.signal, branchCtrl.signal])
|
|
199
|
+
: branchCtrl.signal;
|
|
195
200
|
const branchStartMs = Date.now();
|
|
196
201
|
let branchCurrent: FlowNodeDef | undefined;
|
|
197
202
|
let skipEnter = false;
|
|
198
203
|
|
|
199
|
-
if (existing?.status === 'paused') {
|
|
204
|
+
if (existing?.status === 'paused' || existing?.status === 'running') {
|
|
200
205
|
branchCurrent = nodes.get(existing.node) ?? nodes.get(branch.startNode);
|
|
201
|
-
skipEnter = (
|
|
206
|
+
skipEnter = (options.resumeMode ?? 'skip-enter') === 'skip-enter';
|
|
207
|
+
if (!skipEnter && branchCurrent?.resumeRerun !== true) {
|
|
208
|
+
throw new FSMError(
|
|
209
|
+
`Cannot rerun-resume branch node "${branchCurrent?.id}": not marked resumeRerun: true`,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
202
212
|
} else {
|
|
203
213
|
branchCurrent = nodes.get(branch.startNode);
|
|
204
214
|
await this.options.persistence.saveBranchStart(
|
|
@@ -206,16 +216,19 @@ export class TransitionFlowDriver {
|
|
|
206
216
|
parallelNode.id,
|
|
207
217
|
branch.id,
|
|
208
218
|
branch.startNode,
|
|
219
|
+
lifecycle.ownerAttemptId,
|
|
209
220
|
);
|
|
210
221
|
}
|
|
211
222
|
|
|
212
223
|
lifecycle.branchStarted(parallelNode.id, branch.id, branchCurrent?.id ?? branch.startNode);
|
|
213
224
|
|
|
214
|
-
let
|
|
215
|
-
|
|
225
|
+
let branchSetVars: Vars = existing?.output_vars_json
|
|
226
|
+
? JSON.parse(existing.output_vars_json)
|
|
227
|
+
: {};
|
|
228
|
+
let branchVars = mergeSetVars(vars, branchSetVars);
|
|
216
229
|
|
|
217
230
|
while (branchCurrent && branchCurrent.id !== parallelNode.join) {
|
|
218
|
-
if (
|
|
231
|
+
if (branchSignal.aborted) {
|
|
219
232
|
lifecycle.branchFailed(parallelNode.id, branch.id, 'cancelled');
|
|
220
233
|
await this.options.persistence.saveBranchFinalize(
|
|
221
234
|
runId,
|
|
@@ -224,6 +237,11 @@ export class TransitionFlowDriver {
|
|
|
224
237
|
Date.now() - branchStartMs,
|
|
225
238
|
undefined,
|
|
226
239
|
'cancelled',
|
|
240
|
+
{
|
|
241
|
+
parallelNode: parallelNode.id,
|
|
242
|
+
node: branchCurrent.id,
|
|
243
|
+
ownerAttempt: lifecycle.ownerAttemptId,
|
|
244
|
+
},
|
|
227
245
|
);
|
|
228
246
|
return { ok: false, branchId: branch.id, error: 'cancelled' };
|
|
229
247
|
}
|
|
@@ -231,6 +249,19 @@ export class TransitionFlowDriver {
|
|
|
231
249
|
if (skipEnter) {
|
|
232
250
|
skipEnter = false;
|
|
233
251
|
} else {
|
|
252
|
+
await this.options.persistence.saveBranchFinalize(
|
|
253
|
+
runId,
|
|
254
|
+
branch.id,
|
|
255
|
+
'running',
|
|
256
|
+
Date.now() - branchStartMs,
|
|
257
|
+
branchSetVars,
|
|
258
|
+
undefined,
|
|
259
|
+
{
|
|
260
|
+
parallelNode: parallelNode.id,
|
|
261
|
+
node: branchCurrent.id,
|
|
262
|
+
ownerAttempt: lifecycle.ownerAttemptId,
|
|
263
|
+
},
|
|
264
|
+
);
|
|
234
265
|
if (branchCurrent.action !== undefined) {
|
|
235
266
|
const step = await runActionStep(branchCurrent.action, branchVars, {
|
|
236
267
|
host: this.options.host,
|
|
@@ -244,13 +275,13 @@ export class TransitionFlowDriver {
|
|
|
244
275
|
env,
|
|
245
276
|
options,
|
|
246
277
|
defaultOnError,
|
|
247
|
-
signal:
|
|
278
|
+
signal: branchSignal,
|
|
248
279
|
});
|
|
249
280
|
if (step.result?.setVars) {
|
|
250
281
|
branchVars = mergeSetVars(branchVars, step.result.setVars);
|
|
251
282
|
branchSetVars = mergeSetVars(branchSetVars, step.result.setVars);
|
|
252
283
|
}
|
|
253
|
-
if (
|
|
284
|
+
if (branchSignal.aborted) {
|
|
254
285
|
lifecycle.branchFailed(parallelNode.id, branch.id, 'cancelled');
|
|
255
286
|
await this.options.persistence.saveBranchFinalize(
|
|
256
287
|
runId,
|
|
@@ -259,6 +290,11 @@ export class TransitionFlowDriver {
|
|
|
259
290
|
Date.now() - branchStartMs,
|
|
260
291
|
undefined,
|
|
261
292
|
'cancelled',
|
|
293
|
+
{
|
|
294
|
+
parallelNode: parallelNode.id,
|
|
295
|
+
node: branchCurrent.id,
|
|
296
|
+
ownerAttempt: lifecycle.ownerAttemptId,
|
|
297
|
+
},
|
|
262
298
|
);
|
|
263
299
|
return { ok: false, branchId: branch.id, error: 'cancelled' };
|
|
264
300
|
}
|
|
@@ -273,6 +309,11 @@ export class TransitionFlowDriver {
|
|
|
273
309
|
Date.now() - branchStartMs,
|
|
274
310
|
undefined,
|
|
275
311
|
err,
|
|
312
|
+
{
|
|
313
|
+
parallelNode: parallelNode.id,
|
|
314
|
+
node: branchCurrent.id,
|
|
315
|
+
ownerAttempt: lifecycle.ownerAttemptId,
|
|
316
|
+
},
|
|
276
317
|
);
|
|
277
318
|
return {
|
|
278
319
|
ok: false,
|
|
@@ -293,6 +334,12 @@ export class TransitionFlowDriver {
|
|
|
293
334
|
'paused',
|
|
294
335
|
Date.now() - branchStartMs,
|
|
295
336
|
branchSetVars,
|
|
337
|
+
undefined,
|
|
338
|
+
{
|
|
339
|
+
parallelNode: parallelNode.id,
|
|
340
|
+
node: branchCurrent.id,
|
|
341
|
+
ownerAttempt: lifecycle.ownerAttemptId,
|
|
342
|
+
},
|
|
296
343
|
);
|
|
297
344
|
return {
|
|
298
345
|
ok: true,
|
|
@@ -307,19 +354,40 @@ export class TransitionFlowDriver {
|
|
|
307
354
|
if (outbound.length === 0 || terminal.has(branchCurrent.id)) {
|
|
308
355
|
break;
|
|
309
356
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
357
|
+
let edge: TransitionFlowWorkflowDef['edges'][number] | undefined;
|
|
358
|
+
try {
|
|
359
|
+
edge = await firstPassingEdge(
|
|
360
|
+
outbound,
|
|
361
|
+
this.options.host,
|
|
362
|
+
{
|
|
363
|
+
runId,
|
|
364
|
+
current: branchCurrent.id,
|
|
365
|
+
vars: branchVars,
|
|
366
|
+
env,
|
|
367
|
+
workdir: options.workdir,
|
|
368
|
+
signal: branchSignal,
|
|
369
|
+
},
|
|
370
|
+
lifecycle,
|
|
371
|
+
);
|
|
372
|
+
} catch (error) {
|
|
373
|
+
abortSiblings(branch.id);
|
|
374
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
375
|
+
lifecycle.branchFailed(parallelNode.id, branch.id, reason);
|
|
376
|
+
await this.options.persistence.saveBranchFinalize(
|
|
314
377
|
runId,
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
378
|
+
branch.id,
|
|
379
|
+
'failed',
|
|
380
|
+
Date.now() - branchStartMs,
|
|
381
|
+
branchSetVars,
|
|
382
|
+
reason,
|
|
383
|
+
{
|
|
384
|
+
parallelNode: parallelNode.id,
|
|
385
|
+
node: branchCurrent.id,
|
|
386
|
+
ownerAttempt: lifecycle.ownerAttemptId,
|
|
387
|
+
},
|
|
388
|
+
);
|
|
389
|
+
return { ok: false, branchId: branch.id, error: reason };
|
|
390
|
+
}
|
|
323
391
|
if (edge === undefined) {
|
|
324
392
|
abortSiblings(branch.id);
|
|
325
393
|
lifecycle.branchFailed(parallelNode.id, branch.id, 'no-passing-edge');
|
|
@@ -330,13 +398,18 @@ export class TransitionFlowDriver {
|
|
|
330
398
|
Date.now() - branchStartMs,
|
|
331
399
|
undefined,
|
|
332
400
|
'no-passing-edge',
|
|
401
|
+
{
|
|
402
|
+
parallelNode: parallelNode.id,
|
|
403
|
+
node: branchCurrent.id,
|
|
404
|
+
ownerAttempt: lifecycle.ownerAttemptId,
|
|
405
|
+
},
|
|
333
406
|
);
|
|
334
407
|
return { ok: false, branchId: branch.id, error: 'no-passing-edge' };
|
|
335
408
|
}
|
|
336
409
|
branchCurrent = nodes.get(edge.to);
|
|
337
410
|
}
|
|
338
411
|
|
|
339
|
-
if (
|
|
412
|
+
if (branchSignal.aborted) {
|
|
340
413
|
lifecycle.branchFailed(parallelNode.id, branch.id, 'cancelled');
|
|
341
414
|
await this.options.persistence.saveBranchFinalize(
|
|
342
415
|
runId,
|
|
@@ -345,6 +418,11 @@ export class TransitionFlowDriver {
|
|
|
345
418
|
Date.now() - branchStartMs,
|
|
346
419
|
undefined,
|
|
347
420
|
'cancelled',
|
|
421
|
+
{
|
|
422
|
+
parallelNode: parallelNode.id,
|
|
423
|
+
node: branchCurrent?.id ?? branch.startNode,
|
|
424
|
+
ownerAttempt: lifecycle.ownerAttemptId,
|
|
425
|
+
},
|
|
348
426
|
);
|
|
349
427
|
return { ok: false, branchId: branch.id, error: 'cancelled' };
|
|
350
428
|
}
|
|
@@ -356,30 +434,55 @@ export class TransitionFlowDriver {
|
|
|
356
434
|
'done',
|
|
357
435
|
Date.now() - branchStartMs,
|
|
358
436
|
branchSetVars,
|
|
437
|
+
undefined,
|
|
438
|
+
{
|
|
439
|
+
parallelNode: parallelNode.id,
|
|
440
|
+
node: branchCurrent?.id ?? branch.startNode,
|
|
441
|
+
ownerAttempt: lifecycle.ownerAttemptId,
|
|
442
|
+
},
|
|
359
443
|
);
|
|
360
444
|
return { ok: true, branchId: branch.id, setVars: branchSetVars };
|
|
361
445
|
},
|
|
446
|
+
() => {
|
|
447
|
+
for (const controller of branchControllers.values()) controller.abort();
|
|
448
|
+
},
|
|
362
449
|
);
|
|
363
450
|
|
|
364
|
-
const failed =
|
|
451
|
+
const failed =
|
|
452
|
+
branchResults.find((r) => !r.ok && r.error !== 'cancelled') ?? branchResults.find((r) => !r.ok);
|
|
365
453
|
if (failed) {
|
|
366
|
-
|
|
454
|
+
if (parallelNode.failurePolicy === 'fail-fast' || options.signal?.aborted) {
|
|
455
|
+
return await lifecycle.fail(current.id, transitionsTaken, failed.error ?? 'branch-failed');
|
|
456
|
+
}
|
|
457
|
+
collectedFailure = failed.error ?? 'branch-failed';
|
|
367
458
|
}
|
|
368
459
|
|
|
369
460
|
const anyPaused = branchResults.some((r) => 'paused' in r && r.paused === true);
|
|
461
|
+
if (anyPaused && collectedFailure === undefined) {
|
|
462
|
+
return await lifecycle.pause(current.id, transitionsTaken, vars);
|
|
463
|
+
}
|
|
370
464
|
for (const res of branchResults) {
|
|
371
465
|
if (res.setVars) vars = mergeSetVars(vars, res.setVars);
|
|
372
466
|
}
|
|
373
|
-
if (anyPaused) {
|
|
374
|
-
return await lifecycle.pause(current.id, transitionsTaken, vars);
|
|
375
|
-
}
|
|
376
467
|
}
|
|
377
468
|
|
|
378
469
|
transitionsTaken += 1;
|
|
379
|
-
await this.options.persistence.commitJoin(
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
470
|
+
await this.options.persistence.commitJoin(
|
|
471
|
+
runId,
|
|
472
|
+
current.id,
|
|
473
|
+
parallelNode.join,
|
|
474
|
+
vars,
|
|
475
|
+
{
|
|
476
|
+
phase: parallelNode.join,
|
|
477
|
+
status: 'running',
|
|
478
|
+
},
|
|
479
|
+
lifecycle.ownerAttemptId,
|
|
480
|
+
transitionsTaken,
|
|
481
|
+
collectedFailure,
|
|
482
|
+
);
|
|
483
|
+
if (transitionsTaken > iterationBound) {
|
|
484
|
+
return await lifecycle.fail(current.id, transitionsTaken, 'iteration-bound-exceeded');
|
|
485
|
+
}
|
|
383
486
|
|
|
384
487
|
const joinNode = nodes.get(parallelNode.join);
|
|
385
488
|
if (joinNode === undefined) {
|
|
@@ -476,6 +579,7 @@ async function runWithConcurrencyLimit<T, R>(
|
|
|
476
579
|
items: readonly T[],
|
|
477
580
|
limit: number,
|
|
478
581
|
fn: (item: T) => Promise<R>,
|
|
582
|
+
onError: () => void,
|
|
479
583
|
): Promise<R[]> {
|
|
480
584
|
const results: R[] = new Array(items.length);
|
|
481
585
|
let nextIndex = 0;
|
|
@@ -485,10 +589,17 @@ async function runWithConcurrencyLimit<T, R>(
|
|
|
485
589
|
const index = nextIndex++;
|
|
486
590
|
const item = items[index];
|
|
487
591
|
if (item !== undefined) {
|
|
488
|
-
|
|
592
|
+
try {
|
|
593
|
+
results[index] = await fn(item);
|
|
594
|
+
} catch (error) {
|
|
595
|
+
onError();
|
|
596
|
+
throw error;
|
|
597
|
+
}
|
|
489
598
|
}
|
|
490
599
|
}
|
|
491
600
|
});
|
|
492
|
-
await Promise.
|
|
601
|
+
const settled = await Promise.allSettled(workers);
|
|
602
|
+
const failed = settled.find((result) => result.status === 'rejected');
|
|
603
|
+
if (failed?.status === 'rejected') throw failed.reason;
|
|
493
604
|
return results;
|
|
494
605
|
}
|