-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathedge-manager.test.ts
More file actions
3507 lines (2909 loc) · 124 KB
/
Copy pathedge-manager.test.ts
File metadata and controls
3507 lines (2909 loc) · 124 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, expect, it } from 'vitest'
import { EDGE } from '@/executor/constants'
import type { DAG, DAGNode } from '@/executor/dag/builder'
import type { DAGEdge } from '@/executor/dag/types'
import type { SerializedBlock } from '@/serializer/types'
import { EdgeManager } from './edge-manager'
function createMockBlock(id: string): SerializedBlock {
return {
id,
metadata: { id: 'test', name: 'Test Block' },
position: { x: 0, y: 0 },
config: { tool: '', params: {} },
inputs: {},
outputs: {},
enabled: true,
}
}
function createMockNode(
id: string,
outgoingEdges: DAGEdge[] = [],
incomingEdges: string[] = []
): DAGNode {
const outEdgesMap = new Map<string, DAGEdge>()
outgoingEdges.forEach((edge, i) => {
outEdgesMap.set(`edge-${i}`, edge)
})
return {
id,
block: createMockBlock(id),
outgoingEdges: outEdgesMap,
incomingEdges: new Set(incomingEdges),
metadata: {},
}
}
function createMockDAG(nodes: Map<string, DAGNode>): DAG {
return {
nodes,
loopConfigs: new Map(),
parallelConfigs: new Map(),
}
}
describe('EdgeManager', () => {
describe('Happy path - basic workflows', () => {
it('should handle simple linear flow (A → B → C)', () => {
const blockAId = 'block-a'
const blockBId = 'block-b'
const blockCId = 'block-c'
const blockANode = createMockNode(blockAId, [{ target: blockBId }])
const blockBNode = createMockNode(blockBId, [{ target: blockCId }], [blockAId])
const blockCNode = createMockNode(blockCId, [], [blockBId])
const nodes = new Map<string, DAGNode>([
[blockAId, blockANode],
[blockBId, blockBNode],
[blockCId, blockCNode],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
const readyAfterA = edgeManager.processOutgoingEdges(blockANode, {
result: 'done',
})
expect(readyAfterA).toContain(blockBId)
expect(readyAfterA).not.toContain(blockCId)
const readyAfterB = edgeManager.processOutgoingEdges(blockBNode, {
result: 'done',
})
expect(readyAfterB).toContain(blockCId)
})
it('should handle branching and each branch executing independently', () => {
const startId = 'start'
const branch1Id = 'branch-1'
const branch2Id = 'branch-2'
const startNode = createMockNode(startId, [
{ target: branch1Id, sourceHandle: 'condition-opt1' },
{ target: branch2Id, sourceHandle: 'condition-opt2' },
])
const branch1Node = createMockNode(branch1Id, [], [startId])
const branch2Node = createMockNode(branch2Id, [], [startId])
const nodes = new Map<string, DAGNode>([
[startId, startNode],
[branch1Id, branch1Node],
[branch2Id, branch2Node],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
const readyNodes = edgeManager.processOutgoingEdges(startNode, { selectedOption: 'opt1' })
expect(readyNodes).toContain(branch1Id)
expect(readyNodes).not.toContain(branch2Id)
})
it('should process standard block output with result', () => {
const sourceId = 'source'
const targetId = 'target'
const sourceNode = createMockNode(sourceId, [{ target: targetId }])
const targetNode = createMockNode(targetId, [], [sourceId])
const nodes = new Map<string, DAGNode>([
[sourceId, sourceNode],
[targetId, targetNode],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
const output = {
result: { data: 'test' },
content: 'Hello world',
tokens: { input: 10, output: 20, total: 30 },
}
const readyNodes = edgeManager.processOutgoingEdges(sourceNode, output)
expect(readyNodes).toContain(targetId)
})
it('should handle multiple sequential blocks completing in order', () => {
const block1Id = 'block-1'
const block2Id = 'block-2'
const block3Id = 'block-3'
const block4Id = 'block-4'
const block1Node = createMockNode(block1Id, [{ target: block2Id }])
const block2Node = createMockNode(block2Id, [{ target: block3Id }], [block1Id])
const block3Node = createMockNode(block3Id, [{ target: block4Id }], [block2Id])
const block4Node = createMockNode(block4Id, [], [block3Id])
const nodes = new Map<string, DAGNode>([
[block1Id, block1Node],
[block2Id, block2Node],
[block3Id, block3Node],
[block4Id, block4Node],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
let ready = edgeManager.processOutgoingEdges(block1Node, {})
expect(ready).toEqual([block2Id])
ready = edgeManager.processOutgoingEdges(block2Node, {})
expect(ready).toEqual([block3Id])
ready = edgeManager.processOutgoingEdges(block3Node, {})
expect(ready).toEqual([block4Id])
ready = edgeManager.processOutgoingEdges(block4Node, {})
expect(ready).toEqual([])
})
})
describe('Subflow control edges', () => {
it('does not activate loop exit edge without matching selected route', () => {
const loopStartId = 'loop-loop-1-sentinel-start'
const bodyId = 'body'
const afterLoopId = 'after-loop'
const loopStartNode = createMockNode(loopStartId, [
{ target: bodyId },
{ target: afterLoopId, sourceHandle: EDGE.LOOP_EXIT },
])
const bodyNode = createMockNode(bodyId, [], [loopStartId])
const afterLoopNode = createMockNode(afterLoopId, [], [loopStartId])
const dag = createMockDAG(
new Map<string, DAGNode>([
[loopStartId, loopStartNode],
[bodyId, bodyNode],
[afterLoopId, afterLoopNode],
])
)
const edgeManager = new EdgeManager(dag)
const readyNodes = edgeManager.processOutgoingEdges(loopStartNode, { sentinelStart: true })
expect(readyNodes).toContain(bodyId)
expect(readyNodes).not.toContain(afterLoopId)
})
})
describe('Multiple condition edges to same target', () => {
it('should not cascade-deactivate when multiple edges from same source go to same target', () => {
const conditionId = 'condition-1'
const function1Id = 'function-1'
const function2Id = 'function-2'
const conditionNode = createMockNode(conditionId, [
{ target: function1Id, sourceHandle: 'condition-if' },
{ target: function1Id, sourceHandle: 'condition-else' },
])
const function1Node = createMockNode(function1Id, [{ target: function2Id }], [conditionId])
const function2Node = createMockNode(function2Id, [], [function1Id])
const nodes = new Map<string, DAGNode>([
[conditionId, conditionNode],
[function1Id, function1Node],
[function2Id, function2Node],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
const output = { selectedOption: 'if' }
const readyNodes = edgeManager.processOutgoingEdges(conditionNode, output)
expect(readyNodes).toContain(function1Id)
expect(function1Node.incomingEdges.size).toBe(0)
})
it('should handle "else if" selected when "if" points to same target', () => {
const conditionId = 'condition-1'
const function1Id = 'function-1'
const conditionNode = createMockNode(conditionId, [
{ target: function1Id, sourceHandle: 'condition-if-id' },
{ target: function1Id, sourceHandle: 'condition-elseif-id' },
])
const function1Node = createMockNode(function1Id, [], [conditionId])
const nodes = new Map<string, DAGNode>([
[conditionId, conditionNode],
[function1Id, function1Node],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
const output = { selectedOption: 'elseif-id' }
const readyNodes = edgeManager.processOutgoingEdges(conditionNode, output)
expect(readyNodes).toContain(function1Id)
})
it('should handle condition with if→A, elseif→B, else→A pattern', () => {
const conditionId = 'condition-1'
const function1Id = 'function-1'
const function2Id = 'function-2'
const conditionNode = createMockNode(conditionId, [
{ target: function1Id, sourceHandle: 'condition-if' },
{ target: function2Id, sourceHandle: 'condition-elseif' },
{ target: function1Id, sourceHandle: 'condition-else' },
])
const function1Node = createMockNode(function1Id, [], [conditionId])
const function2Node = createMockNode(function2Id, [], [conditionId])
const nodes = new Map<string, DAGNode>([
[conditionId, conditionNode],
[function1Id, function1Node],
[function2Id, function2Node],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
const output = { selectedOption: 'if' }
const readyNodes = edgeManager.processOutgoingEdges(conditionNode, output)
expect(readyNodes).toContain(function1Id)
expect(readyNodes).not.toContain(function2Id)
})
it('should activate correct target when elseif is selected (iteration 2)', () => {
const conditionId = 'condition-1'
const function1Id = 'function-1'
const function2Id = 'function-2'
const conditionNode = createMockNode(conditionId, [
{ target: function1Id, sourceHandle: 'condition-if' },
{ target: function2Id, sourceHandle: 'condition-elseif' },
{ target: function1Id, sourceHandle: 'condition-else' },
])
const function1Node = createMockNode(function1Id, [], [conditionId])
const function2Node = createMockNode(function2Id, [], [conditionId])
const nodes = new Map<string, DAGNode>([
[conditionId, conditionNode],
[function1Id, function1Node],
[function2Id, function2Node],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
const output = { selectedOption: 'elseif' }
const readyNodes = edgeManager.processOutgoingEdges(conditionNode, output)
expect(readyNodes).toContain(function2Id)
expect(readyNodes).not.toContain(function1Id)
})
it('should activate Function1 when else is selected (iteration 3+)', () => {
const conditionId = 'condition-1'
const function1Id = 'function-1'
const function2Id = 'function-2'
const conditionNode = createMockNode(conditionId, [
{ target: function1Id, sourceHandle: 'condition-if' },
{ target: function2Id, sourceHandle: 'condition-elseif' },
{ target: function1Id, sourceHandle: 'condition-else' },
])
const function1Node = createMockNode(function1Id, [], [conditionId])
const function2Node = createMockNode(function2Id, [], [conditionId])
const nodes = new Map<string, DAGNode>([
[conditionId, conditionNode],
[function1Id, function1Node],
[function2Id, function2Node],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
const output = { selectedOption: 'else' }
const readyNodes = edgeManager.processOutgoingEdges(conditionNode, output)
expect(readyNodes).toContain(function1Id)
expect(readyNodes).not.toContain(function2Id)
})
})
describe('Cascade deactivation', () => {
it('should cascade-deactivate descendants when ALL edges to target are deactivated', () => {
const conditionId = 'condition-1'
const function1Id = 'function-1'
const function2Id = 'function-2'
const conditionNode = createMockNode(conditionId, [
{ target: function1Id, sourceHandle: 'condition-if' },
])
const function1Node = createMockNode(function1Id, [{ target: function2Id }], [conditionId])
const function2Node = createMockNode(function2Id, [], [function1Id])
const nodes = new Map<string, DAGNode>([
[conditionId, conditionNode],
[function1Id, function1Node],
[function2Id, function2Node],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
const output = { selectedOption: 'else' }
const readyNodes = edgeManager.processOutgoingEdges(conditionNode, output)
expect(readyNodes).not.toContain(function1Id)
})
})
describe('Exact workflow reproduction: modern-atoll', () => {
const conditionId = '63353190-ed15-427b-af6b-c0967ba06010'
const function1Id = '576cc8a3-c3f3-40f5-a515-8320462b8162'
const function2Id = 'b96067c5-0c5c-4a91-92bd-299e8c4ab42d'
const ifConditionId = '63353190-ed15-427b-af6b-c0967ba06010-if'
const elseIfConditionId = '63353190-ed15-427b-af6b-c0967ba06010-else-if-1766204485970'
const elseConditionId = '63353190-ed15-427b-af6b-c0967ba06010-else'
function setupWorkflow() {
const conditionNode = createMockNode(conditionId, [
{ target: function1Id, sourceHandle: `condition-${ifConditionId}` },
{ target: function2Id, sourceHandle: `condition-${elseIfConditionId}` },
{ target: function1Id, sourceHandle: `condition-${elseConditionId}` },
])
const function1Node = createMockNode(function1Id, [], [conditionId])
const function2Node = createMockNode(function2Id, [], [conditionId])
const nodes = new Map<string, DAGNode>([
[conditionId, conditionNode],
[function1Id, function1Node],
[function2Id, function2Node],
])
return createMockDAG(nodes)
}
it('iteration 1: if selected (loop.index == 1) should activate Function 1', () => {
const dag = setupWorkflow()
const edgeManager = new EdgeManager(dag)
const conditionNode = dag.nodes.get(conditionId)!
const output = { selectedOption: ifConditionId }
const readyNodes = edgeManager.processOutgoingEdges(conditionNode, output)
expect(readyNodes).toContain(function1Id)
expect(readyNodes).not.toContain(function2Id)
})
it('iteration 2: else if selected (loop.index == 2) should activate Function 2', () => {
const dag = setupWorkflow()
const edgeManager = new EdgeManager(dag)
const conditionNode = dag.nodes.get(conditionId)!
const output = { selectedOption: elseIfConditionId }
const readyNodes = edgeManager.processOutgoingEdges(conditionNode, output)
expect(readyNodes).toContain(function2Id)
expect(readyNodes).not.toContain(function1Id)
})
it('iteration 3+: else selected (loop.index > 2) should activate Function 1', () => {
const dag = setupWorkflow()
const edgeManager = new EdgeManager(dag)
const conditionNode = dag.nodes.get(conditionId)!
const output = { selectedOption: elseConditionId }
const readyNodes = edgeManager.processOutgoingEdges(conditionNode, output)
expect(readyNodes).toContain(function1Id)
expect(readyNodes).not.toContain(function2Id)
})
it('should handle multiple iterations correctly (simulating loop)', () => {
const dag = setupWorkflow()
const edgeManager = new EdgeManager(dag)
const conditionNode = dag.nodes.get(conditionId)!
// Iteration 1: if selected
{
dag.nodes.get(function1Id)!.incomingEdges = new Set([conditionId])
dag.nodes.get(function2Id)!.incomingEdges = new Set([conditionId])
edgeManager.clearDeactivatedEdges()
const output = { selectedOption: ifConditionId }
const readyNodes = edgeManager.processOutgoingEdges(conditionNode, output)
expect(readyNodes).toContain(function1Id)
expect(readyNodes).not.toContain(function2Id)
}
// Iteration 2: else if selected
{
dag.nodes.get(function1Id)!.incomingEdges = new Set([conditionId])
dag.nodes.get(function2Id)!.incomingEdges = new Set([conditionId])
edgeManager.clearDeactivatedEdges()
const output = { selectedOption: elseIfConditionId }
const readyNodes = edgeManager.processOutgoingEdges(conditionNode, output)
expect(readyNodes).toContain(function2Id)
expect(readyNodes).not.toContain(function1Id)
}
// Iteration 3: else selected
{
dag.nodes.get(function1Id)!.incomingEdges = new Set([conditionId])
dag.nodes.get(function2Id)!.incomingEdges = new Set([conditionId])
edgeManager.clearDeactivatedEdges()
const output = { selectedOption: elseConditionId }
const readyNodes = edgeManager.processOutgoingEdges(conditionNode, output)
expect(readyNodes).toContain(function1Id)
expect(readyNodes).not.toContain(function2Id)
}
})
})
describe('Error/Success edge handling', () => {
it('should activate error edge when output has error', () => {
const sourceId = 'source-1'
const successTargetId = 'success-target'
const errorTargetId = 'error-target'
const sourceNode = createMockNode(sourceId, [
{ target: successTargetId, sourceHandle: 'source' },
{ target: errorTargetId, sourceHandle: 'error' },
])
const successNode = createMockNode(successTargetId, [], [sourceId])
const errorNode = createMockNode(errorTargetId, [], [sourceId])
const nodes = new Map<string, DAGNode>([
[sourceId, sourceNode],
[successTargetId, successNode],
[errorTargetId, errorNode],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
const output = { error: 'Something went wrong' }
const readyNodes = edgeManager.processOutgoingEdges(sourceNode, output)
expect(readyNodes).toContain(errorTargetId)
expect(readyNodes).not.toContain(successTargetId)
})
it('should activate source edge when no error', () => {
const sourceId = 'source-1'
const successTargetId = 'success-target'
const errorTargetId = 'error-target'
const sourceNode = createMockNode(sourceId, [
{ target: successTargetId, sourceHandle: 'source' },
{ target: errorTargetId, sourceHandle: 'error' },
])
const successNode = createMockNode(successTargetId, [], [sourceId])
const errorNode = createMockNode(errorTargetId, [], [sourceId])
const nodes = new Map<string, DAGNode>([
[sourceId, sourceNode],
[successTargetId, successNode],
[errorTargetId, errorNode],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
const output = { result: 'success' }
const readyNodes = edgeManager.processOutgoingEdges(sourceNode, output)
expect(readyNodes).toContain(successTargetId)
expect(readyNodes).not.toContain(errorTargetId)
})
})
describe('Router edge handling', () => {
it('should activate only the selected route', () => {
const routerId = 'router-1'
const route1Id = 'route-1'
const route2Id = 'route-2'
const route3Id = 'route-3'
const routerNode = createMockNode(routerId, [
{ target: route1Id, sourceHandle: 'router-route1' },
{ target: route2Id, sourceHandle: 'router-route2' },
{ target: route3Id, sourceHandle: 'router-route3' },
])
const route1Node = createMockNode(route1Id, [], [routerId])
const route2Node = createMockNode(route2Id, [], [routerId])
const route3Node = createMockNode(route3Id, [], [routerId])
const nodes = new Map<string, DAGNode>([
[routerId, routerNode],
[route1Id, route1Node],
[route2Id, route2Node],
[route3Id, route3Node],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
const output = { selectedRoute: 'route2' }
const readyNodes = edgeManager.processOutgoingEdges(routerNode, output)
expect(readyNodes).toContain(route2Id)
expect(readyNodes).not.toContain(route1Id)
expect(readyNodes).not.toContain(route3Id)
})
})
describe('Node with multiple incoming sources', () => {
it('should wait for all incoming edges before becoming ready', () => {
const source1Id = 'source-1'
const source2Id = 'source-2'
const targetId = 'target'
const source1Node = createMockNode(source1Id, [{ target: targetId }])
const source2Node = createMockNode(source2Id, [{ target: targetId }])
const targetNode = createMockNode(targetId, [], [source1Id, source2Id])
const nodes = new Map<string, DAGNode>([
[source1Id, source1Node],
[source2Id, source2Node],
[targetId, targetNode],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
const readyAfterFirst = edgeManager.processOutgoingEdges(source1Node, {})
expect(readyAfterFirst).not.toContain(targetId)
const readyAfterSecond = edgeManager.processOutgoingEdges(source2Node, {})
expect(readyAfterSecond).toContain(targetId)
})
})
describe('clearDeactivatedEdgesForNodes', () => {
it('should clear deactivated edges for specified nodes', () => {
const conditionId = 'condition-1'
const function1Id = 'function-1'
const conditionNode = createMockNode(conditionId, [
{ target: function1Id, sourceHandle: 'condition-if' },
])
const function1Node = createMockNode(function1Id, [], [conditionId])
const nodes = new Map<string, DAGNode>([
[conditionId, conditionNode],
[function1Id, function1Node],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
edgeManager.processOutgoingEdges(conditionNode, { selectedOption: 'nonexistent' })
edgeManager.clearDeactivatedEdgesForNodes(new Set([conditionId]))
function1Node.incomingEdges.add(conditionId)
const readyNodes = edgeManager.processOutgoingEdges(conditionNode, {
selectedOption: 'if',
})
expect(readyNodes).toContain(function1Id)
})
it('does not clear deactivated edges from prefix-sharing source node IDs', () => {
const shortSourceId = 'a'
const longSourceId = 'a-b'
const targetId = 'target'
const shortSourceNode = createMockNode(shortSourceId)
const longSourceNode = createMockNode(longSourceId, [
{ target: targetId, sourceHandle: 'condition-if' },
])
const targetNode = createMockNode(targetId, [], [longSourceId])
const dag = createMockDAG(
new Map<string, DAGNode>([
[shortSourceId, shortSourceNode],
[longSourceId, longSourceNode],
[targetId, targetNode],
])
)
const edgeManager = new EdgeManager(dag)
edgeManager.processOutgoingEdges(longSourceNode, { selectedOption: 'else' })
expect(edgeManager.isNodeReady(targetNode)).toBe(true)
edgeManager.clearDeactivatedEdgesForNodes(new Set([shortSourceId]))
expect(edgeManager.isNodeReady(targetNode)).toBe(true)
})
/**
* Regression for the substring-match bug in clearDeactivatedEdgesForNodes.
*
* Reproduces the real workflow pattern where an empty upstream loop (e.g. KG) cascade
* deactivates its `loop_exit` edge into the next loop's sentinel-start (e.g. SBJ). When
* SBJ iterates and resets its state between iterations, the old buggy `includes(\`-${nodeId}-\`)`
* check matched edge keys where the sentinel was the TARGET (not the source), wrongly
* reactivating that external edge. That made countActiveIncomingEdges see a phantom pending
* upstream and SBJ's sentinel-start stopped being ready, stalling the loop after iteration 1.
*/
it('should not re-activate external cascade-deactivated edges pointing INTO a loop node', () => {
const externalNodeId = 'external-node'
const sbjSentinelStartId = 'loop-sbj-sentinel-start'
const sbjSentinelEndId = 'loop-sbj-sentinel-end'
const bodyNodeId = 'body-node'
const externalNode = createMockNode(externalNodeId, [
{ target: sbjSentinelStartId, sourceHandle: 'condition-if' },
])
const sbjSentinelStartNode = createMockNode(
sbjSentinelStartId,
[{ target: bodyNodeId }],
[externalNodeId]
)
const bodyNode = createMockNode(
bodyNodeId,
[{ target: sbjSentinelEndId }],
[sbjSentinelStartId]
)
const sbjSentinelEndNode = createMockNode(sbjSentinelEndId, [], [bodyNodeId])
const nodes = new Map<string, DAGNode>([
[externalNodeId, externalNode],
[sbjSentinelStartId, sbjSentinelStartNode],
[bodyNodeId, bodyNode],
[sbjSentinelEndId, sbjSentinelEndNode],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
edgeManager.processOutgoingEdges(externalNode, { selectedOption: 'else' })
expect(edgeManager.isNodeReady(sbjSentinelStartNode)).toBe(true)
edgeManager.clearDeactivatedEdgesForNodes(
new Set([sbjSentinelStartId, sbjSentinelEndId, bodyNodeId])
)
expect(edgeManager.isNodeReady(sbjSentinelStartNode)).toBe(true)
})
/**
* End-to-end regression: after a loop reset while an external edge is cascade-deactivated,
* the backwards `loop_continue` edge from sentinel-end must still mark sentinel-start as
* ready. The old code removed the external edge's deactivation entry, leaving a phantom
* active incoming and producing the exact "loop stops after 1 iteration" symptom the user
* hit on the Group A workflow.
*/
it('should leave sbjSentinelStart ready after loop reset when external edge is cascade-deactivated', () => {
const externalNodeId = 'external-node'
const sbjSentinelStartId = 'loop-sbj-sentinel-start'
const sbjSentinelEndId = 'loop-sbj-sentinel-end'
const bodyNodeId = 'body-node'
const externalNode = createMockNode(externalNodeId, [
{ target: sbjSentinelStartId, sourceHandle: 'condition-if' },
])
const sbjSentinelStartNode = createMockNode(
sbjSentinelStartId,
[{ target: bodyNodeId }],
[externalNodeId]
)
const bodyNode = createMockNode(
bodyNodeId,
[{ target: sbjSentinelEndId }],
[sbjSentinelStartId]
)
const sbjSentinelEndNode = createMockNode(
sbjSentinelEndId,
[{ target: sbjSentinelStartId, sourceHandle: 'loop_continue' }],
[bodyNodeId]
)
const nodes = new Map<string, DAGNode>([
[externalNodeId, externalNode],
[sbjSentinelStartId, sbjSentinelStartNode],
[bodyNodeId, bodyNode],
[sbjSentinelEndId, sbjSentinelEndNode],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
edgeManager.processOutgoingEdges(externalNode, { selectedOption: 'else' })
edgeManager.clearDeactivatedEdgesForNodes(
new Set([sbjSentinelStartId, sbjSentinelEndId, bodyNodeId])
)
const readyNodes = edgeManager.processOutgoingEdges(sbjSentinelEndNode, {
selectedRoute: 'loop_continue',
})
expect(readyNodes).toContain(sbjSentinelStartId)
})
/**
* Guard against an overly narrow fix: edges whose SOURCE is inside the loop (e.g. a body
* node that deactivated its outgoing edge during the previous iteration) must still be
* cleared on reset so the next iteration can traverse them.
*/
it('should re-activate internal loop edges (source inside loop) when resetting loop state', () => {
const sbjSentinelStartId = 'loop-sbj-sentinel-start'
const sbjSentinelEndId = 'loop-sbj-sentinel-end'
const conditionInLoopId = 'condition-in-loop'
const thenBranchId = 'then-branch'
const sbjSentinelStartNode = createMockNode(sbjSentinelStartId, [
{ target: conditionInLoopId },
])
const conditionInLoopNode = createMockNode(
conditionInLoopId,
[
{ target: thenBranchId, sourceHandle: 'condition-if' },
{ target: sbjSentinelEndId, sourceHandle: 'condition-else' },
],
[sbjSentinelStartId]
)
const thenBranchNode = createMockNode(
thenBranchId,
[{ target: sbjSentinelEndId }],
[conditionInLoopId]
)
const sbjSentinelEndNode = createMockNode(
sbjSentinelEndId,
[],
[conditionInLoopId, thenBranchId]
)
const nodes = new Map<string, DAGNode>([
[sbjSentinelStartId, sbjSentinelStartNode],
[conditionInLoopId, conditionInLoopNode],
[thenBranchId, thenBranchNode],
[sbjSentinelEndId, sbjSentinelEndNode],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
edgeManager.processOutgoingEdges(conditionInLoopNode, { selectedOption: 'else' })
edgeManager.clearDeactivatedEdgesForNodes(
new Set([sbjSentinelStartId, sbjSentinelEndId, conditionInLoopId, thenBranchId])
)
thenBranchNode.incomingEdges.add(conditionInLoopId)
const readyNodes = edgeManager.processOutgoingEdges(conditionInLoopNode, {
selectedOption: 'if',
})
expect(readyNodes).toContain(thenBranchId)
})
})
describe('restoreIncomingEdge', () => {
it('should restore an incoming edge to a target node', () => {
const sourceId = 'source-1'
const targetId = 'target-1'
const sourceNode = createMockNode(sourceId, [{ target: targetId }])
const targetNode = createMockNode(targetId, [], [])
const nodes = new Map<string, DAGNode>([
[sourceId, sourceNode],
[targetId, targetNode],
])
const dag = createMockDAG(nodes)
const edgeManager = new EdgeManager(dag)
expect(targetNode.incomingEdges.has(sourceId)).toBe(false)
edgeManager.restoreIncomingEdge(targetId, sourceId)
expect(targetNode.incomingEdges.has(sourceId)).toBe(true)
})
})
describe('restoreDeactivatedEdges', () => {
it('restores activated target state used by convergent routing after resume', () => {
const edgeManager = new EdgeManager(createMockDAG(new Map()))
edgeManager.restoreDeactivatedEdges([], ['join'])
expect(edgeManager.getNodesWithActivatedEdge()).toEqual(['join'])
})
it('normalizes legacy deactivated edge keys on restore', () => {
const sourceNode = createMockNode('source-node', [
{ target: 'target-node', sourceHandle: 'condition-if' },
])
const targetNode = createMockNode('target-node', [], ['source-node'])
const edgeManager = new EdgeManager(
createMockDAG(
new Map<string, DAGNode>([
[sourceNode.id, sourceNode],
[targetNode.id, targetNode],
])
)
)
edgeManager.restoreDeactivatedEdges(['source-node-target-node-condition-if'])
expect(edgeManager.getDeactivatedEdges()).toEqual([
JSON.stringify(['source-node', 'target-node', 'condition-if']),
])
})
})
describe('deactivateResumedEdge', () => {
it('prunes a resumed pause block error edge without firing the error target', () => {
// Models the HITL-resume bug: a pause block and a regular block both feed
// an error-notifier via `error` handles. On a fully successful run the
// notifier must never run.
const pauseId = 'pause-block'
const regularId = 'regular-block'
const notifyId = 'error-notify'
const pauseNode = createMockNode(
pauseId,
[
{ target: 'next', sourceHandle: EDGE.SOURCE },
{ target: notifyId, sourceHandle: EDGE.ERROR },
],
[]
)
const regularNode = createMockNode(regularId, [
{ target: notifyId, sourceHandle: EDGE.ERROR },
])
const notifyNode = createMockNode(notifyId, [], [pauseId, regularId])
const dag = createMockDAG(
new Map<string, DAGNode>([
[pauseId, pauseNode],
[regularId, regularNode],
[notifyId, notifyNode],
])
)
const edgeManager = new EdgeManager(dag)
// Resume releases the pause block's error edge as deactivated.
edgeManager.deactivateResumedEdge(pauseId, notifyId, EDGE.ERROR)
expect(edgeManager.getDeactivatedEdges()).toContain(
JSON.stringify([pauseId, notifyId, EDGE.ERROR])
)
expect(edgeManager.getNodesWithActivatedEdge()).not.toContain(notifyId)
// The regular block then completes successfully → its error edge deactivates too.
const readyNodes = edgeManager.processOutgoingEdges(regularNode, { result: 'ok' })
// With no error edge ever activated, the notifier is never scheduled.
expect(readyNodes).not.toContain(notifyId)
expect(edgeManager.getNodesWithActivatedEdge()).not.toContain(notifyId)
})
it('still fires the error target when a real upstream block errors', () => {
// Same topology, but here the regular block genuinely errors — the notifier
// must fire even though the pause block's error edge was pruned on resume.
const pauseId = 'pause-block'
const regularId = 'regular-block'
const notifyId = 'error-notify'
const regularNode = createMockNode(regularId, [
{ target: notifyId, sourceHandle: EDGE.ERROR },
])
const notifyNode = createMockNode(notifyId, [], [pauseId, regularId])
const dag = createMockDAG(
new Map<string, DAGNode>([
[regularId, regularNode],
[notifyId, notifyNode],
])
)
const edgeManager = new EdgeManager(dag)
edgeManager.deactivateResumedEdge(pauseId, notifyId, EDGE.ERROR)
const readyNodes = edgeManager.processOutgoingEdges(regularNode, { error: 'boom' })
expect(readyNodes).toContain(notifyId)
expect(edgeManager.getNodesWithActivatedEdge()).toContain(notifyId)
})
it('leaves a convergence join ready+activated after pruning a pause error edge', () => {
// Join `C` is fed by `B.source` (succeeds) and `P.error` (pause block).
// After B activates and P's error edge is pruned on resume, C must be both
// activated and ready so the engine re-queues it.
const sourceId = 'block-b'
const pauseId = 'pause-block'
const joinId = 'join-c'
const sourceNode = createMockNode(sourceId, [{ target: joinId, sourceHandle: EDGE.SOURCE }])
const pauseNode = createMockNode(pauseId, [{ target: joinId, sourceHandle: EDGE.ERROR }])
const joinNode = createMockNode(joinId, [], [sourceId, pauseId])
const edgeManager = new EdgeManager(
createMockDAG(
new Map<string, DAGNode>([
[sourceId, sourceNode],
[pauseId, pauseNode],
[joinId, joinNode],
])
)
)
// Phase 1: B succeeds → activates B→C, but C still waits on the pause edge.
const readyAfterB = edgeManager.processOutgoingEdges(sourceNode, { result: 'ok' })
expect(readyAfterB).not.toContain(joinId)
expect(edgeManager.hasActivatedEdge(joinId)).toBe(true)
expect(edgeManager.isNodeReady(joinNode)).toBe(false)
// Resume: pause block's error edge is pruned → C is now ready (and stays
// activated), which is exactly the state the engine uses to re-queue it.
edgeManager.deactivateResumedEdge(pauseId, joinId, EDGE.ERROR)
expect(edgeManager.hasActivatedEdge(joinId)).toBe(true)
expect(edgeManager.isNodeReady(joinNode)).toBe(true)
})
})
describe('Diamond pattern (convergent paths)', () => {
it('should handle diamond: condition splits then converges at merge point', () => {
const conditionId = 'condition-1'
const branchAId = 'branch-a'
const branchBId = 'branch-b'
const mergeId = 'merge-point'
const conditionNode = createMockNode(conditionId, [
{ target: branchAId, sourceHandle: 'condition-if' },
{ target: branchBId, sourceHandle: 'condition-else' },
])