gantz_core 0.4.0

The core types and traits for gantz, an environment for creative systems.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
//! Tests related to the nesting of graphs.

use gantz_core::{
    Edge, ROOT_STATE,
    compile::{entry_fn_name, entrypoint, push_pull_entrypoints, push_source},
    node::{self, Node, WithPushEval},
};
use std::fmt::Debug;
use steel::{SteelVal, steel_vm::engine::Engine};

fn node_push() -> node::Push<node::Expr> {
    node::expr("'()").unwrap().with_push_eval()
}

fn node_int(i: i32) -> node::Expr {
    node::expr(format!("(begin $push {})", i)).unwrap()
}

fn node_mul() -> node::Expr {
    node::expr("(* $l $r)").unwrap()
}

fn node_assert_eq() -> node::Expr {
    node::expr("(begin (assert! (equal? $l $r)))").unwrap()
}

fn node_number() -> node::Expr {
    node::expr(
        "
        (let ((x $x))
          (set! state (if (number? x) x state))
          state)
    ",
    )
    .unwrap()
}

// Helper trait for debugging the graph.
trait DebugNode: Debug + Node {}
impl<T> DebugNode for T where T: Debug + Node {}

// A nested graph: now an ordinary `Graph` (which implements `Node`) boxed
// straight into its parent, in place of the removed `GraphNode` wrapper.
type Nested = node::graph::Graph<Box<dyn DebugNode>>;

// A no-op node lookup function for tests that don't need it.
fn no_lookup(_: &gantz_ca::ContentAddr) -> Option<&'static dyn Node> {
    None
}

// A simple test for nested graph support.
//
// This is the core method of abstraction provided by gantz, so it better work!
//
// GRAPH A
//
//    --------- ---------
//    | Inlet | | Inlet |
//    -+------- -+-------
//     |         |
//     |   -------
//     |   |
//    -+---+-
//    | Mul |
//    -+-----
//     |
//    -+--------
//    | Outlet |
//    ----------
//
// GRAPH B
//
//    --------
//    | push | // push_eval
//    -+------
//     |
//     |------------
//     |           |
//     |------     |
//     |     |     |
//    -+--- -+---  |
//    | 6 | | 7 |  |
//    -+--- -+---  |
//     |     |     |
//     |     ---   |
//     |       |   |
//    -+-------+- -+----
//    | GRAPH A | | 42 |
//    -+--------- -+----
//     |           |
//     |         ---
//     |         |
//    -+---------+-
//    | assert_eq |
//    -------------
#[test]
fn test_graph_nested_stateless() {
    env_logger::init();

    // Graph A, used as a nested node.
    let mut ga = Nested::default();
    let inlet_a = ga.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
    let inlet_b = ga.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
    let mul = ga.add_node(Box::new(node_mul()) as Box<_>);
    let outlet = ga.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    ga.add_edge(inlet_a, mul, Edge::from((0, 0)));
    ga.add_edge(inlet_b, mul, Edge::from((0, 1)));
    ga.add_edge(mul, outlet, Edge::from((0, 0)));

    // Graph B.
    let mut gb = petgraph::graph::DiGraph::new();
    let push = gb.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let six = gb.add_node(Box::new(node_int(6)) as Box<_>);
    let seven = gb.add_node(Box::new(node_int(7)) as Box<_>);
    let graph_a = gb.add_node(Box::new(ga) as Box<_>);
    let forty_two = gb.add_node(Box::new(node_int(42)) as Box<_>);
    let assert_eq = gb.add_node(Box::new(node_assert_eq()) as Box<_>);
    gb.add_edge(push, six, Edge::from((0, 0)));
    gb.add_edge(push, seven, Edge::from((0, 0)));
    gb.add_edge(push, forty_two, Edge::from((0, 0)));
    gb.add_edge(six, graph_a, Edge::from((0, 0)));
    gb.add_edge(seven, graph_a, Edge::from((0, 1)));
    gb.add_edge(graph_a, assert_eq, Edge::from((0, 0)));
    gb.add_edge(forty_two, assert_eq, Edge::from((0, 1)));

    // Generate the module, which should have just one top-level expr for `push`.
    let ctx = node::MetaCtx::new(&no_lookup);
    let eps = push_pull_entrypoints(&no_lookup, &gb);
    let module = gantz_core::compile::module(&no_lookup, &gb, &eps, &Default::default()).unwrap();

    // Create the VM.
    let mut vm = Engine::new_base();

    // Initialise the node state vars.
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, &gb, &[], &mut vm);

    // Register the fns.
    for f in module {
        vm.run(f.to_pretty(100)).unwrap();
    }

    // Call the `push` eval function.
    let ep = entrypoint::push(vec![push.index()], gb[push].n_outputs(ctx) as u8);
    vm.call_function_by_name_with_args(&entry_fn_name(&ep.id()), vec![])
        .unwrap();
}

// A simple test for nested graph support where the nested graph is stateful.
//
// GRAPH A
//
//    ---------
//    | Inlet |
//    -+-------
//     |
//    -+---------
//    | Counter |
//    -+---------
//     |
//    -+--------
//    | Outlet |
//    ----------
//
// GRAPH B
//
//    --------
//    | push | // push_eval
//    -+------
//     |
//    -+---------
//    | GRAPH A |
//    -+---------
//     |
//    -+--------
//    | number |
//    ----------
//
// We push evaluation from the root graph B's `push` node, and then check that
// the value is incremented by checking the state of the `number` node.
#[test]
fn test_graph_nested_counter() {
    // The counter node for the nested graph.
    let counter = node::expr(
        "
        (begin
          $bang
          (set! state
            (if (number? state) (+ state 1) 0))
          state)
    ",
    )
    .unwrap();

    // Graph A.
    let mut ga = Nested::default();
    let inlet = ga.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
    let counter = ga.add_node(Box::new(counter) as Box<_>);
    let outlet = ga.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    ga.add_edge(inlet, counter, Edge::from((0, 0)));
    ga.add_edge(counter, outlet, Edge::from((0, 0)));

    // Graph B.
    let mut gb = petgraph::graph::DiGraph::new();
    let push = gb.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let graph_a = gb.add_node(Box::new(ga) as Box<_>);
    let number = gb.add_node(Box::new(node_number()) as Box<_>);
    gb.add_edge(push, graph_a, Edge::from((0, 0)));
    gb.add_edge(graph_a, number, Edge::from((0, 0)));

    // Generate the module.
    let ctx = node::MetaCtx::new(&no_lookup);
    let eps = push_pull_entrypoints(&no_lookup, &gb);
    let module = gantz_core::compile::module(&no_lookup, &gb, &eps, &Default::default()).unwrap();

    // Create the VM.
    let mut vm = Engine::new_base();

    // Initialise the node state vars.
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, &gb, &[], &mut vm);

    // Register the fns.
    for f in module {
        println!("{}\n", f.to_pretty(100));
        vm.run(f.to_pretty(100)).unwrap();
    }

    // Increment the nested counter by pushing evaluation.
    // The first is `0`, the second is `1`.
    let ep = entrypoint::push(vec![push.index()], gb[push].n_outputs(ctx) as u8);
    let fn_name = entry_fn_name(&ep.id());
    vm.call_function_by_name_with_args(&fn_name, vec![])
        .unwrap();
    vm.call_function_by_name_with_args(&fn_name, vec![])
        .unwrap();

    // First, check that the nested expr's state is `1`.
    let counter_state = node::state::extract::<u32>(&vm, &[graph_a.index(), counter.index()])
        .expect("failed to extract counter state")
        .expect("counter state was `None`");
    assert_eq!(counter_state, 1);

    // Outlets are stateless - they just pass through their input value.
    // The value flows through to the downstream `number` node.

    // Check that the number in the root graph was updated from the outlet.
    let number_state = node::state::extract::<u32>(&vm, &[number.index()])
        .expect("failed to extract number state")
        .expect("number state was `None`");
    assert_eq!(number_state, 1);
}

// A simple test for pushing evaluation from a node within a nested graph.
//
// GRAPH A
//
//    --------
//    | Push |
//    -+------
//     |
//    -+----
//    | 42 |
//    -+----
//     |
//    -+--------
//    | Outlet |
//    ----------
//
// GRAPH B
//
//    -+---------
//    | GRAPH A |
//    -+---------
//     |
//    -+--------
//    | number |
//    ----------
//
// A simple-as-possible demonstration of pushing evaluation from within a nested
// node, and propagating that evaluation through the outlets of the graph node.
// Test pushing evaluation from a node inside a nested graph.
//
// GRAPH A (inner):
//
//    --------
//    | Push |
//    -+------
//     |
//    -+--------
//    | number | (stores received value in state)
//    ----------
//
// GRAPH B (outer):
//
//    -----------
//    | GRAPH A |
//    -----------
//
// The push fires inside graph A, driving evaluation to the number node
// which stores the received value. This demonstrates that entrypoints
// can target nodes inside nested graphs.
#[test]
fn test_graph_nested_push_eval() {
    // GRAPH A: push -> number (stateful, stores value)
    let mut ga = Nested::default();
    let push = ga.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let num = ga.add_node(Box::new(node_number()) as Box<_>);
    ga.add_edge(push, num, Edge::from((0, 0)));

    // Compute push connection count before moving `ga` into `gb`.
    let ctx = node::MetaCtx::new(&no_lookup);
    let push_n_outputs = ga[push].n_outputs(ctx) as u8;

    // Graph B: just contains graph A (no outlet propagation needed).
    let mut gb = petgraph::graph::DiGraph::new();
    let graph_a = gb.add_node(Box::new(ga) as Box<dyn DebugNode>);

    // Nested entrypoint: push inside graph A.
    let ep = entrypoint::from_source(push_source(
        vec![graph_a.index(), push.index()],
        push_n_outputs,
    ));

    // Generate the module.
    let module =
        gantz_core::compile::module(&no_lookup, &gb, &[ep.clone()], &Default::default()).unwrap();

    // Create the VM.
    let mut vm = Engine::new_base();
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, &gb, &[], &mut vm);

    // Register the fns.
    for f in &module {
        println!("{}\n", f.to_pretty(100));
        vm.run(f.to_pretty(100)).unwrap();
    }

    // Call the nested push eval fn.
    vm.call_function_by_name_with_args(&entry_fn_name(&ep.id()), vec![])
        .unwrap();

    // The number node inside graph A should have received the push value.
    // node_push outputs '() which is not a number, so number's state stays
    // at its initial void value. But we can verify the eval ran without
    // error - the state::extract call itself confirms the state path exists.
    let _num_state = node::state::extract_value(&vm, &[graph_a.index(), num.index()])
        .expect("failed to extract number state from nested graph");
}

// Test that inlet bindings work correctly when node indices don't match inlet positions.
//
// This verifies that inlets are correctly bound even when they're not the first nodes
// in the graph (i.e., their node indices don't match their inlet positions).
//
// GRAPH A (inner)
//
//    --------- ---------
//    | dummy | | dummy |  // Non-inlet nodes with indices 0, 1
//    --------- ---------
//
//    --------- ---------
//    | Inlet | | Inlet |  // Inlet nodes with indices 2, 3 (positions 0, 1)
//    -+------- -+-------
//     |         |
//     ----   ----
//        |   |
//       -+---+-
//       | sub |  // Subtracts second from first
//       -+-----
//        |
//       -+--------
//       | Outlet |
//       ----------
//
// GRAPH B (outer)
//
//    --------
//    | push | // push_eval
//    -+------
//     |
//     |------------
//     |           |
//    -+----     -+----
//    | 10 |     | 3 |
//    -+----     -+----
//     |           |
//     ----     ----
//        |     |
//    ----+-----+----
//    | GRAPH A |
//    -+-------------
//     |
//    -+----
//    | 7 |  // Expected result: 10 - 3 = 7
//    -+----
//     |
//    -+-----------
//    | assert_eq |
//    -------------
#[test]
fn test_graph_nested_non_sequential_inlets() {
    // Graph A with non-sequential inlet indices.
    let mut ga = Nested::default();

    // Add dummy nodes first to offset inlet indices
    let _dummy1 = ga.add_node(Box::new(node_int(999)) as Box<dyn DebugNode>);
    let _dummy2 = ga.add_node(Box::new(node_int(998)) as Box<_>);

    // Now add inlets - they'll have indices 2 and 3
    let inlet_a = ga.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
    let inlet_b = ga.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);

    // Add processing nodes
    let sub = ga.add_node(Box::new(node::expr("(- $l $r)").unwrap()) as Box<_>);
    let outlet = ga.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);

    // Connect the graph
    ga.add_edge(inlet_a, sub, Edge::from((0, 0)));
    ga.add_edge(inlet_b, sub, Edge::from((0, 1)));
    ga.add_edge(sub, outlet, Edge::from((0, 0)));

    // Graph B that uses graph A.
    let mut gb = petgraph::graph::DiGraph::new();
    let push = gb.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let ten = gb.add_node(Box::new(node_int(10)) as Box<_>);
    let three = gb.add_node(Box::new(node_int(3)) as Box<_>);
    let graph_a = gb.add_node(Box::new(ga) as Box<_>);
    let seven = gb.add_node(Box::new(node_int(7)) as Box<_>);
    let assert_eq = gb.add_node(Box::new(node_assert_eq()) as Box<_>);

    gb.add_edge(push, ten, Edge::from((0, 0)));
    gb.add_edge(push, three, Edge::from((0, 0)));
    gb.add_edge(push, seven, Edge::from((0, 0)));
    gb.add_edge(ten, graph_a, Edge::from((0, 0)));
    gb.add_edge(three, graph_a, Edge::from((0, 1)));
    gb.add_edge(graph_a, assert_eq, Edge::from((0, 0)));
    gb.add_edge(seven, assert_eq, Edge::from((0, 1)));

    // Generate the module.
    let ctx = node::MetaCtx::new(&no_lookup);
    let eps = push_pull_entrypoints(&no_lookup, &gb);
    let module = gantz_core::compile::module(&no_lookup, &gb, &eps, &Default::default()).unwrap();

    // Create the VM.
    let mut vm = Engine::new_base();

    // Initialise the node state vars.
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, &gb, &[], &mut vm);

    // Register the fns.
    for f in module {
        vm.run(f.to_pretty(100)).unwrap();
    }

    // Call the `push` eval function - should compute 10 - 3 = 7
    let ep = entrypoint::push(vec![push.index()], gb[push].n_outputs(ctx) as u8);
    vm.call_function_by_name_with_args(&entry_fn_name(&ep.id()), vec![])
        .unwrap();
}

// Test that push evaluation inside a nested graph propagates through its outlet
// to downstream nodes in the outer graph.
//
// GRAPH A (inner):
//
//    --------
//    | Push |
//    -+------
//     |
//    -+----
//    | 42 |
//    -+----
//     |
//    -+--------
//    | Outlet |
//    ----------
//
// GRAPH B (outer):
//
//    -----------
//    | GRAPH A |
//    -+---------
//     |
//    -+--------
//    | number |
//    ----------
//
// The push fires inside graph A, value 42 flows through the outlet to the
// number node in the outer graph. Verifies that nested push evaluation
// propagates through outlets.
#[test]
fn test_graph_nested_push_through_outlet() {
    // GRAPH A: push -> int(42) -> outlet
    let mut ga = Nested::default();
    let push = ga.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let forty_two = ga.add_node(Box::new(node_int(42)) as Box<_>);
    let outlet = ga.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    ga.add_edge(push, forty_two, Edge::from((0, 0)));
    ga.add_edge(forty_two, outlet, Edge::from((0, 0)));

    // Compute push connection count before moving `ga` into `gb`.
    let ctx = node::MetaCtx::new(&no_lookup);
    let push_n_outputs = ga[push].n_outputs(ctx) as u8;

    // GRAPH B: graph_a -> number
    let mut gb = petgraph::graph::DiGraph::new();
    let graph_a = gb.add_node(Box::new(ga) as Box<dyn DebugNode>);
    let number = gb.add_node(Box::new(node_number()) as Box<_>);
    gb.add_edge(graph_a, number, Edge::from((0, 0)));

    // Nested entrypoint: push inside graph A.
    let ep = entrypoint::from_source(push_source(
        vec![graph_a.index(), push.index()],
        push_n_outputs,
    ));

    // Generate the module.
    let module =
        gantz_core::compile::module(&no_lookup, &gb, &[ep.clone()], &Default::default()).unwrap();

    // Create the VM.
    let mut vm = Engine::new_base();
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, &gb, &[], &mut vm);

    // Register the fns.
    for f in &module {
        vm.run(f.to_pretty(100)).unwrap();
    }

    // Call the nested push eval fn.
    vm.call_function_by_name_with_args(&entry_fn_name(&ep.id()), vec![])
        .unwrap();

    // The number node should have received 42 via the outlet.
    let number_state = node::state::extract::<u32>(&vm, &[number.index()])
        .expect("failed to extract number state")
        .expect("number state was None");
    assert_eq!(number_state, 42);
}

// Test that a nested graph with multiple outlets correctly returns a list
// that is destructured via `define-values` in the outer graph.
//
// INNER GRAPH:
//
//    --------- ---------
//    | Inlet | | Inlet |
//    -+------- -+-------
//     |         |
//    -+-------  |
//    | Outlet | |
//    ---------- |
//              -+-------
//              | Outlet |
//              ----------
//
// OUTER GRAPH:
//
//    --------
//    | push |
//    -+------
//     |
//     |------
//     |     |
//    -+--- -+---
//    | 6 | | 7 |
//    -+--- -+---
//     |     |
//    -+-----+----
//    | INNER    |
//    -+------+---
//     |      |
//     o0     o1
//     |      |
//   num_a  num_b
#[test]
fn test_graph_nested_multi_outlet() {
    // Inner graph: 2 inlets pass through to 2 outlets.
    let mut inner = Nested::default();
    let inlet_a = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
    let inlet_b = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
    let outlet_a = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    let outlet_b = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    inner.add_edge(inlet_a, outlet_a, Edge::from((0, 0)));
    inner.add_edge(inlet_b, outlet_b, Edge::from((0, 0)));

    // Outer graph.
    let mut outer = petgraph::graph::DiGraph::new();
    let push = outer.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let six = outer.add_node(Box::new(node_int(6)) as Box<_>);
    let seven = outer.add_node(Box::new(node_int(7)) as Box<_>);
    let graph = outer.add_node(Box::new(inner) as Box<_>);
    let num_a = outer.add_node(Box::new(node_number()) as Box<_>);
    let num_b = outer.add_node(Box::new(node_number()) as Box<_>);

    outer.add_edge(push, six, Edge::from((0, 0)));
    outer.add_edge(push, seven, Edge::from((0, 0)));
    outer.add_edge(six, graph, Edge::from((0, 0)));
    outer.add_edge(seven, graph, Edge::from((0, 1)));
    outer.add_edge(graph, num_a, Edge::from((0, 0))); // outlet 0
    outer.add_edge(graph, num_b, Edge::from((1, 0))); // outlet 1

    let ctx = node::MetaCtx::new(&no_lookup);
    let eps = push_pull_entrypoints(&no_lookup, &outer);
    let module =
        gantz_core::compile::module(&no_lookup, &outer, &eps, &Default::default()).unwrap();

    let mut vm = Engine::new_base();
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, &outer, &[], &mut vm);

    for f in &module {
        vm.run(f.to_pretty(100)).unwrap();
    }

    let ep = entrypoint::push(vec![push.index()], outer[push].n_outputs(ctx) as u8);
    vm.call_function_by_name_with_args(&entry_fn_name(&ep.id()), vec![])
        .unwrap();

    let a = node::state::extract::<u32>(&vm, &[num_a.index()])
        .expect("failed to extract num_a state")
        .expect("num_a state was None");
    let b = node::state::extract::<u32>(&vm, &[num_b.index()])
        .expect("failed to extract num_b state")
        .expect("num_b state was None");
    assert_eq!(a, 6);
    assert_eq!(b, 7);
}

// Test nested push evaluation propagating through multiple outlets.
//
// INNER GRAPH:
//    push -> int(10) -> outlet_a
//                    -> outlet_b (via int(20))
//
// OUTER GRAPH:
//    inner_graph -> num_a (from outlet 0)
//               -> num_b (from outlet 1)
//
// Push fires inside inner graph. Both outlet values should propagate to
// the outer graph's number nodes.
#[test]
fn test_graph_nested_push_through_outlet_multi() {
    let mut inner = Nested::default();
    let push = inner.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let ten = inner.add_node(Box::new(node_int(10)) as Box<_>);
    let twenty = inner.add_node(Box::new(node_int(20)) as Box<_>);
    let outlet_a = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    let outlet_b = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    inner.add_edge(push, ten, Edge::from((0, 0)));
    inner.add_edge(push, twenty, Edge::from((0, 0)));
    inner.add_edge(ten, outlet_a, Edge::from((0, 0)));
    inner.add_edge(twenty, outlet_b, Edge::from((0, 0)));

    let ctx = node::MetaCtx::new(&no_lookup);
    let push_n_outputs = inner[push].n_outputs(ctx) as u8;

    let mut outer = petgraph::graph::DiGraph::new();
    let graph = outer.add_node(Box::new(inner) as Box<dyn DebugNode>);
    let num_a = outer.add_node(Box::new(node_number()) as Box<_>);
    let num_b = outer.add_node(Box::new(node_number()) as Box<_>);
    outer.add_edge(graph, num_a, Edge::from((0, 0)));
    outer.add_edge(graph, num_b, Edge::from((1, 0)));

    let ep = entrypoint::from_source(push_source(
        vec![graph.index(), push.index()],
        push_n_outputs,
    ));

    let module =
        gantz_core::compile::module(&no_lookup, &outer, &[ep.clone()], &Default::default())
            .unwrap();

    let mut vm = Engine::new_base();
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, &outer, &[], &mut vm);

    for f in &module {
        vm.run(f.to_pretty(100)).unwrap();
    }

    vm.call_function_by_name_with_args(&entry_fn_name(&ep.id()), vec![])
        .unwrap();

    let a = node::state::extract::<u32>(&vm, &[num_a.index()])
        .expect("failed to extract num_a state")
        .expect("num_a state was None");
    let b = node::state::extract::<u32>(&vm, &[num_b.index()])
        .expect("failed to extract num_b state")
        .expect("num_b state was None");
    assert_eq!(a, 10);
    assert_eq!(b, 20);
}

// Test nested push evaluation propagating through two levels of nesting.
//
// INNERMOST GRAPH:
//    push -> int(99) -> outlet
//
// MIDDLE GRAPH:
//    innermost -> outlet
//
// OUTER GRAPH:
//    middle -> number
//
// Push fires in innermost, value 99 propagates through two outlet levels
// to the outer number node.
#[test]
fn test_graph_nested_push_through_outlet_deep() {
    // Innermost: push -> int(99) -> outlet
    let mut innermost = Nested::default();
    let push = innermost.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let ninety_nine = innermost.add_node(Box::new(node_int(99)) as Box<_>);
    let outlet_inner = innermost.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    innermost.add_edge(push, ninety_nine, Edge::from((0, 0)));
    innermost.add_edge(ninety_nine, outlet_inner, Edge::from((0, 0)));

    let ctx = node::MetaCtx::new(&no_lookup);
    let push_n_outputs = innermost[push].n_outputs(ctx) as u8;

    // Middle: innermost_graph -> outlet
    let mut middle = Nested::default();
    let innermost_node = middle.add_node(Box::new(innermost) as Box<dyn DebugNode>);
    let outlet_mid = middle.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    middle.add_edge(innermost_node, outlet_mid, Edge::from((0, 0)));

    // Outer: middle_graph -> number
    let mut outer = petgraph::graph::DiGraph::new();
    let middle_node = outer.add_node(Box::new(middle) as Box<dyn DebugNode>);
    let number = outer.add_node(Box::new(node_number()) as Box<_>);
    outer.add_edge(middle_node, number, Edge::from((0, 0)));

    let ep = entrypoint::from_source(push_source(
        vec![middle_node.index(), innermost_node.index(), push.index()],
        push_n_outputs,
    ));

    let module =
        gantz_core::compile::module(&no_lookup, &outer, &[ep.clone()], &Default::default())
            .unwrap();

    let mut vm = Engine::new_base();
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, &outer, &[], &mut vm);

    for f in &module {
        vm.run(f.to_pretty(100)).unwrap();
    }

    vm.call_function_by_name_with_args(&entry_fn_name(&ep.id()), vec![])
        .unwrap();

    let val = node::state::extract::<u32>(&vm, &[number.index()])
        .expect("failed to extract number state")
        .expect("number state was None");
    assert_eq!(val, 99);
}

// Test that `push_pull_entrypoints` discovers push eval nodes inside nested
// graphs. This mirrors the real-world scenario of a FrameBang node inside a
// nested graph placed in a top-level graph via NamedRef.
//
// INNER GRAPH:
//    push -> int(42) -> outlet
//
// OUTER GRAPH:
//    inner_graph -> number
//
// `push_pull_entrypoints` on the outer graph should discover the push node
// inside the inner graph and create an entrypoint with path [graph_a, push].
#[test]
fn test_push_pull_entrypoints_discovers_nested_push() {
    // Inner graph: push -> int(42) -> outlet
    let mut inner = Nested::default();
    let push = inner.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let forty_two = inner.add_node(Box::new(node_int(42)) as Box<_>);
    let outlet = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    inner.add_edge(push, forty_two, Edge::from((0, 0)));
    inner.add_edge(forty_two, outlet, Edge::from((0, 0)));

    // Outer graph: inner -> number
    let mut outer = petgraph::graph::DiGraph::new();
    let graph_a = outer.add_node(Box::new(inner) as Box<dyn DebugNode>);
    let number = outer.add_node(Box::new(node_number()) as Box<_>);
    outer.add_edge(graph_a, number, Edge::from((0, 0)));

    // push_pull_entrypoints should find the nested push node.
    let eps = push_pull_entrypoints(&no_lookup, &outer);
    assert!(
        !eps.is_empty(),
        "push_pull_entrypoints should discover the nested push eval node"
    );

    // There should be an entrypoint with path [graph_a, push].
    let has_nested_push = eps.iter().any(|ep| {
        ep.0.iter()
            .any(|src| src.path == vec![graph_a.index(), push.index()])
    });
    assert!(
        has_nested_push,
        "expected entrypoint at path [{}, {}], found: {:?}",
        graph_a.index(),
        push.index(),
        eps.iter()
            .flat_map(|ep| ep.0.iter().map(|s| &s.path))
            .collect::<Vec<_>>()
    );

    // The generated module should include the entry fn for this entrypoint,
    // and it should work end-to-end (value 42 flows through outlet to number).
    let module =
        gantz_core::compile::module(&no_lookup, &outer, &eps, &Default::default()).unwrap();

    let mut vm = Engine::new_base();
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, &outer, &[], &mut vm);

    for f in &module {
        vm.run(f.to_pretty(100)).unwrap();
    }

    // Find and call the nested push entrypoint.
    let nested_ep = eps
        .iter()
        .find(|ep| {
            ep.0.iter()
                .any(|src| src.path == vec![graph_a.index(), push.index()])
        })
        .unwrap();
    vm.call_function_by_name_with_args(&entry_fn_name(&nested_ep.id()), vec![])
        .unwrap();

    let val = node::state::extract::<u32>(&vm, &[number.index()])
        .expect("failed to extract number state")
        .expect("number state was None");
    assert_eq!(val, 42);
}

// Test that two nested graph nodes sharing a multi-source entrypoint both
// propagate through their outlets to the parent graph.
//
// This mirrors the scenario of two NamedRef "deltams" nodes in a top-level
// graph, where both contain a FrameBang and are combined into a single
// multi-source entrypoint.
//
// INNER GRAPH (shared by both):
//    push -> int(10) -> outlet
//
// OUTER GRAPH:
//    graph_a -> num_a
//    graph_b -> num_b
//
// A single multi-source entrypoint fires push inside both graph_a and graph_b.
// Both outlets should propagate, writing 10 to both num_a and num_b.
#[test]
fn test_graph_nested_multi_source_outlet_propagation() {
    // Inner graph: push -> int(10) -> outlet
    let make_inner = || {
        let mut inner = Nested::default();
        let push = inner.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let ten = inner.add_node(Box::new(node_int(10)) as Box<_>);
        let outlet = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(push, ten, Edge::from((0, 0)));
        inner.add_edge(ten, outlet, Edge::from((0, 0)));
        (inner, push)
    };

    let (inner_a, push_a) = make_inner();
    let (inner_b, push_b) = make_inner();

    let ctx = node::MetaCtx::new(&no_lookup);
    let push_n_outputs = inner_a[push_a].n_outputs(ctx) as u8;

    // Outer graph: two graph nodes -> two number nodes
    let mut outer = petgraph::graph::DiGraph::new();
    let graph_a = outer.add_node(Box::new(inner_a) as Box<dyn DebugNode>);
    let graph_b = outer.add_node(Box::new(inner_b) as Box<dyn DebugNode>);
    let num_a = outer.add_node(Box::new(node_number()) as Box<_>);
    let num_b = outer.add_node(Box::new(node_number()) as Box<_>);
    outer.add_edge(graph_a, num_a, Edge::from((0, 0)));
    outer.add_edge(graph_b, num_b, Edge::from((0, 0)));

    // Multi-source entrypoint: both pushes in one entrypoint.
    let ep = entrypoint::from_sources([
        push_source(vec![graph_a.index(), push_a.index()], push_n_outputs),
        push_source(vec![graph_b.index(), push_b.index()], push_n_outputs),
    ]);

    let module =
        gantz_core::compile::module(&no_lookup, &outer, &[ep.clone()], &Default::default())
            .unwrap();

    let mut vm = Engine::new_base();
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, &outer, &[], &mut vm);

    for f in &module {
        vm.run(f.to_pretty(100)).unwrap();
    }

    vm.call_function_by_name_with_args(&entry_fn_name(&ep.id()), vec![])
        .unwrap();

    let a = node::state::extract::<u32>(&vm, &[num_a.index()])
        .expect("failed to extract num_a state")
        .expect("num_a state was None");
    let b = node::state::extract::<u32>(&vm, &[num_b.index()])
        .expect("failed to extract num_b state")
        .expect("num_b state was None");
    assert_eq!(a, 10, "graph_a outlet should propagate to num_a");
    assert_eq!(b, 10, "graph_b outlet should propagate to num_b");
}

// Test a multi-source entrypoint with sources at different nesting levels:
// a direct push source at the root and a nested push source inside a graph
// node that propagates through an outlet.
//
// This mirrors the scenario of a top-level FrameBang + a NamedRef "deltams"
// (which contains its own FrameBang inside) combined into one entrypoint.
//
// INNER GRAPH:
//    push_inner -> int(10) -> outlet
//
// OUTER GRAPH:
//    graph_node --(outlet)--> add (input 0)
//    push_outer ------------> add (input 1)
//    add -> number
//
// Both pushes fire in a single entrypoint. The graph_node outlet value (10)
// and push_outer value ('()) reach add, whose result is stored in number.
#[test]
fn test_graph_nested_mixed_level_multi_source() {
    // Inner graph: push -> int(10) -> outlet
    let mut inner = Nested::default();
    let push_inner = inner.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let ten = inner.add_node(Box::new(node_int(10)) as Box<_>);
    let outlet = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    inner.add_edge(push_inner, ten, Edge::from((0, 0)));
    inner.add_edge(ten, outlet, Edge::from((0, 0)));

    let ctx = node::MetaCtx::new(&no_lookup);
    let push_inner_n = inner[push_inner].n_outputs(ctx) as u8;

    // Outer graph
    let mut outer = petgraph::graph::DiGraph::new();
    let graph_node = outer.add_node(Box::new(inner) as Box<dyn DebugNode>);
    let push_outer = outer.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let twenty = outer.add_node(Box::new(node_int(20)) as Box<_>);
    // add: (+ $l $r) - takes two inputs
    let add = outer.add_node(Box::new(node::expr("(+ $l $r)").unwrap()) as Box<_>);
    let number = outer.add_node(Box::new(node_number()) as Box<_>);
    outer.add_edge(graph_node, add, Edge::from((0, 0))); // outlet(10) -> add input 0
    outer.add_edge(push_outer, twenty, Edge::from((0, 0))); // push -> int(20)
    outer.add_edge(twenty, add, Edge::from((0, 1))); // int(20) -> add input 1
    outer.add_edge(add, number, Edge::from((0, 0)));

    let push_outer_n = outer[push_outer].n_outputs(ctx) as u8;

    // Multi-source entrypoint: nested push + direct push
    let ep = entrypoint::from_sources([
        push_source(vec![graph_node.index(), push_inner.index()], push_inner_n),
        push_source(vec![push_outer.index()], push_outer_n),
    ]);

    let module =
        gantz_core::compile::module(&no_lookup, &outer, &[ep.clone()], &Default::default())
            .unwrap();

    let mut vm = Engine::new_base();
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, &outer, &[], &mut vm);

    for f in &module {
        vm.run(f.to_pretty(100)).unwrap();
    }

    vm.call_function_by_name_with_args(&entry_fn_name(&ep.id()), vec![])
        .unwrap();

    // outlet produces 10, push_outer -> int produces 20, add = 10 + 20 = 30
    let val = node::state::extract::<i32>(&vm, &[number.index()])
        .expect("failed to extract number state")
        .expect("number state was None");
    assert_eq!(val, 30);
}

// ===========================================================================
// Nested-graph branching tests.
//
// A nested graph whose interior branches should report that branching to the
// outer graph via `Node::branches`, so the outer graph only evaluates the
// downstream of outlets actually produced by the taken inner branch.
//
// Each test's INNER graph is sketched above it. `branches()` lists the sets of
// outputs active per external branch (`{}` = a branch producing nothing).
// Diagram legend:
//   [In]     inlet            [Out X]  outlet
//   [Sel]    node_select: input ==0 -> o0(42), else -> o1(99)
//   oN       branch output N    /  \   the two arms of a branch
// The OUTER graph is uniform: push -> int value(s) -> [Nested] -> a `number`
// store per output (which records the value it receives).
// ===========================================================================

// A 1-input, 2-output branch primitive. Routes to output 0 (value 42) when the
// input is 0, else to output 1 (value 99).
fn node_select() -> node::Branch {
    node::branch(
        "(if (= 0 $x) (list 0 42) (list 1 99))",
        vec![
            node::Conns::try_from([true, false]).unwrap(),
            node::Conns::try_from([false, true]).unwrap(),
        ],
    )
    .unwrap()
}

// Assert that `inner.branches()` reports exactly `expected` (order-insensitive),
// where each entry lists the output indices active in that branch.
fn assert_inner_branches<N: Node + ?Sized>(inner: &N, n_outputs: usize, expected: &[&[u16]]) {
    let ctx = node::MetaCtx::new(&no_lookup);
    let got: std::collections::BTreeSet<Vec<u16>> = inner
        .branches(ctx)
        .iter()
        .map(|b| {
            let node::EvalConf::Set(c) = b else {
                panic!("expected EvalConf::Set, got {b:?}");
            };
            (0..n_outputs as u16)
                .filter(|&i| c.get(i as usize).unwrap_or(false))
                .collect()
        })
        .collect();
    let want: std::collections::BTreeSet<Vec<u16>> = expected
        .iter()
        .map(|e| {
            let mut v = e.to_vec();
            v.sort();
            v
        })
        .collect();
    assert_eq!(got, want, "branch patterns mismatch");
}

// Build, compile and run a graph from `push`; returns the VM for state queries.
fn compile_and_push<N: DebugNode + ?Sized>(
    g: &petgraph::graph::DiGraph<Box<N>, Edge>,
    push: petgraph::graph::NodeIndex,
) -> Engine {
    let ctx = node::MetaCtx::new(&no_lookup);
    let eps = push_pull_entrypoints(&no_lookup, g);
    let module = gantz_core::compile::module(&no_lookup, g, &eps, &Default::default()).unwrap();
    let mut vm = Engine::new_base();
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, g, &[], &mut vm);
    for f in &module {
        vm.run(f.to_pretty(100)).unwrap();
    }
    let ep = entrypoint::push(vec![push.index()], g[push].n_outputs(ctx) as u8);
    vm.call_function_by_name_with_args(&entry_fn_name(&ep.id()), vec![])
        .unwrap();
    vm
}

// `Some(v)` if the store node holds a number, else `None` (never evaluated).
fn store_val(vm: &Engine, store: petgraph::graph::NodeIndex) -> Option<i32> {
    node::state::extract::<i32>(vm, &[store.index()])
        .ok()
        .flatten()
}

// Build, compile and run `g` from a `push_eval` nested at `path` (e.g.
// `[graph_node, push]`); returns the VM for state queries. Unlike
// `compile_and_push`, the push lives *inside* a nested graph and propagates out
// through that graph's outlets.
fn compile_and_push_nested<N: DebugNode + ?Sized>(
    g: &petgraph::graph::DiGraph<Box<N>, Edge>,
    path: Vec<usize>,
    push_n_outputs: u8,
) -> Engine {
    let ep = entrypoint::from_source(push_source(path, push_n_outputs));
    let module =
        gantz_core::compile::module(&no_lookup, g, &[ep.clone()], &Default::default()).unwrap();
    let mut vm = Engine::new_base();
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, g, &[], &mut vm);
    for f in &module {
        vm.run(f.to_pretty(100)).unwrap();
    }
    vm.call_function_by_name_with_args(&entry_fn_name(&ep.id()), vec![])
        .unwrap();
    vm
}

// Divergent branch: each arm routes to its own outlet.  branches: [{A}, {B}]
//
//        [In]
//         |
//       [Sel]
//      o0/  \o1
//   [Out A]  [Out B]
#[test]
fn test_graph_nested_divergent_branch() {
    let make_inner = || {
        let mut inner = Nested::default();
        let inlet = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let select = inner.add_node(Box::new(node_select()) as Box<_>);
        let outlet_a = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let outlet_b = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(inlet, select, Edge::from((0, 0)));
        inner.add_edge(select, outlet_a, Edge::from((0, 0)));
        inner.add_edge(select, outlet_b, Edge::from((1, 0)));
        inner
    };

    // Two arms: arm 0 -> outlet A only, arm 1 -> outlet B only.
    assert_inner_branches(&make_inner(), 2, &[&[0], &[1]]);

    let build = |sel: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let store_a = g.add_node(Box::new(node_number()) as Box<_>);
        let store_b = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, int, Edge::from((0, 0)));
        g.add_edge(int, inner_node, Edge::from((0, 0)));
        g.add_edge(inner_node, store_a, Edge::from((0, 0)));
        g.add_edge(inner_node, store_b, Edge::from((1, 0)));
        let vm = compile_and_push(&g, push);
        (store_val(&vm, store_a), store_val(&vm, store_b))
    };

    // sel == 0 -> arm 0 -> only store_a written (42).
    assert_eq!(build(0), (Some(42), None));
    // sel != 0 -> arm 1 -> only store_b written (99).
    assert_eq!(build(1), (None, Some(99)));
}

// Reconvergent: both arms feed the SAME outlet, so it is always produced and
// there is no external branching (the value still differs per arm).  branches: []
//
//        [In]
//         |
//       [Sel]
//      o0\  /o1
//       [Out A]
#[test]
fn test_graph_nested_reconvergent_branch() {
    let make_inner = || {
        let mut inner = Nested::default();
        let inlet = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let select = inner.add_node(Box::new(node_select()) as Box<_>);
        let outlet = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(inlet, select, Edge::from((0, 0)));
        inner.add_edge(select, outlet, Edge::from((0, 0)));
        inner.add_edge(select, outlet, Edge::from((1, 0)));
        inner
    };

    // No external branching: the outlet is always produced.
    assert_inner_branches(&make_inner(), 1, &[]);

    let build = |sel: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let store = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, int, Edge::from((0, 0)));
        g.add_edge(int, inner_node, Edge::from((0, 0)));
        g.add_edge(inner_node, store, Edge::from((0, 0)));
        store_val(&compile_and_push(&g, push), store)
    };

    // Outlet always written; the value differs per arm (phi reconvergence).
    assert_eq!(build(0), Some(42));
    assert_eq!(build(1), Some(99));
}

// Dead arm: arm 1's output is unconnected, so it produces nothing.
// branches: [{}, {A}]
//
//        [In]
//         |
//       [Sel]
//      o0|   \o1   (unconnected = dead arm)
//   [Out A]    x
#[test]
fn test_graph_nested_dead_arm() {
    let make_inner = || {
        let mut inner = Nested::default();
        let inlet = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let select = inner.add_node(Box::new(node_select()) as Box<_>);
        let outlet = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(inlet, select, Edge::from((0, 0)));
        inner.add_edge(select, outlet, Edge::from((0, 0)));
        // Output 1 of Select is left unconnected (a dead arm).
        inner
    };

    // Two patterns: empty (dead arm) and {outlet}.
    assert_inner_branches(&make_inner(), 1, &[&[], &[0]]);

    let build = |sel: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let store = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, int, Edge::from((0, 0)));
        g.add_edge(int, inner_node, Edge::from((0, 0)));
        g.add_edge(inner_node, store, Edge::from((0, 0)));
        store_val(&compile_and_push(&g, push), store)
    };

    assert_eq!(build(0), Some(42)); // arm 0 -> outlet
    assert_eq!(build(1), None); // arm 1 -> dead, nothing downstream evaluated
}

// Per-arm intermediates: each arm transforms its value before its outlet.
// branches: [{A}, {B}]
//
//         [In]
//          |
//        [Sel]
//      o0/    \o1
//    [+10]    [+20]
//      |        |
//   [Out A]   [Out B]
#[test]
fn test_graph_nested_branch_intermediates() {
    let make_inner = || {
        let mut inner = Nested::default();
        let inlet = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let select = inner.add_node(Box::new(node_select()) as Box<_>);
        let add10 = inner.add_node(Box::new(node::expr("(+ $x 10)").unwrap()) as Box<_>);
        let add20 = inner.add_node(Box::new(node::expr("(+ $x 20)").unwrap()) as Box<_>);
        let outlet_a = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let outlet_b = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(inlet, select, Edge::from((0, 0)));
        inner.add_edge(select, add10, Edge::from((0, 0)));
        inner.add_edge(add10, outlet_a, Edge::from((0, 0)));
        inner.add_edge(select, add20, Edge::from((1, 0)));
        inner.add_edge(add20, outlet_b, Edge::from((0, 0)));
        inner
    };

    assert_inner_branches(&make_inner(), 2, &[&[0], &[1]]);

    let build = |sel: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let store_a = g.add_node(Box::new(node_number()) as Box<_>);
        let store_b = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, int, Edge::from((0, 0)));
        g.add_edge(int, inner_node, Edge::from((0, 0)));
        g.add_edge(inner_node, store_a, Edge::from((0, 0)));
        g.add_edge(inner_node, store_b, Edge::from((1, 0)));
        let vm = compile_and_push(&g, push);
        (store_val(&vm, store_a), store_val(&vm, store_b))
    };

    assert_eq!(build(0), (Some(52), None)); // 42 + 10
    assert_eq!(build(1), (None, Some(119))); // 99 + 20
}

// Multi-output arm: arm 0 fires TWO outputs (a list value); arm 1 fires one.
// branches: [{A, B}, {C}]
//
//          [In]
//           |
//        [Branch]              arm 0 -> o0,o1  (value (10 20))
//      o0/ o1|  \o2            arm 1 -> o2     (value 30)
//  [Out A][Out B][Out C]
#[test]
fn test_graph_nested_branch_multi_outlet_arm() {
    let branch3 = || {
        node::branch(
            "(if (= 0 $x) (list 0 (list 10 20)) (list 1 30))",
            vec![
                node::Conns::try_from([true, true, false]).unwrap(),
                node::Conns::try_from([false, false, true]).unwrap(),
            ],
        )
        .unwrap()
    };
    let make_inner = || {
        let mut inner = Nested::default();
        let inlet = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let br = inner.add_node(Box::new(branch3()) as Box<_>);
        let outlet_a = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let outlet_b = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let outlet_c = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(inlet, br, Edge::from((0, 0)));
        inner.add_edge(br, outlet_a, Edge::from((0, 0)));
        inner.add_edge(br, outlet_b, Edge::from((1, 0)));
        inner.add_edge(br, outlet_c, Edge::from((2, 0)));
        inner
    };

    assert_inner_branches(&make_inner(), 3, &[&[0, 1], &[2]]);

    let build = |sel: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let store_a = g.add_node(Box::new(node_number()) as Box<_>);
        let store_b = g.add_node(Box::new(node_number()) as Box<_>);
        let store_c = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, int, Edge::from((0, 0)));
        g.add_edge(int, inner_node, Edge::from((0, 0)));
        g.add_edge(inner_node, store_a, Edge::from((0, 0)));
        g.add_edge(inner_node, store_b, Edge::from((1, 0)));
        g.add_edge(inner_node, store_c, Edge::from((2, 0)));
        let vm = compile_and_push(&g, push);
        (
            store_val(&vm, store_a),
            store_val(&vm, store_b),
            store_val(&vm, store_c),
        )
    };

    assert_eq!(build(0), (Some(10), Some(20), None)); // arm 0 -> a,b
    assert_eq!(build(1), (None, None, Some(30))); // arm 1 -> c
}

// Parallel branches: two independent Selects -> Cartesian product of 4 branches.
// Exercises a multi-component (multi-root) inner flow graph.
// branches: [{A,C}, {A,D}, {B,C}, {B,D}]
//
//   [In a]        [In b]
//     |             |
//   [Sel1]        [Sel2]
//  o0/  \o1      o0/  \o1
// [A]    [B]    [C]    [D]
#[test]
fn test_graph_nested_parallel_branches() {
    let make_inner = || {
        let mut inner = Nested::default();
        let inlet_a = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let inlet_b = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
        let sel1 = inner.add_node(Box::new(node_select()) as Box<_>);
        let sel2 = inner.add_node(Box::new(node_select()) as Box<_>);
        let oa = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let ob = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let oc = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let od = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(inlet_a, sel1, Edge::from((0, 0)));
        inner.add_edge(inlet_b, sel2, Edge::from((0, 0)));
        inner.add_edge(sel1, oa, Edge::from((0, 0)));
        inner.add_edge(sel1, ob, Edge::from((1, 0)));
        inner.add_edge(sel2, oc, Edge::from((0, 0)));
        inner.add_edge(sel2, od, Edge::from((1, 0)));
        inner
    };

    // Outputs A=0, B=1, C=2, D=3; 4 Cartesian arms.
    assert_inner_branches(&make_inner(), 4, &[&[0, 2], &[0, 3], &[1, 2], &[1, 3]]);

    let build = |s1: i32, s2: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let i1 = g.add_node(Box::new(node_int(s1)) as Box<_>);
        let i2 = g.add_node(Box::new(node_int(s2)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let sa = g.add_node(Box::new(node_number()) as Box<_>);
        let sb = g.add_node(Box::new(node_number()) as Box<_>);
        let sc = g.add_node(Box::new(node_number()) as Box<_>);
        let sd = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, i1, Edge::from((0, 0)));
        g.add_edge(push, i2, Edge::from((0, 0)));
        g.add_edge(i1, inner_node, Edge::from((0, 0)));
        g.add_edge(i2, inner_node, Edge::from((0, 1)));
        g.add_edge(inner_node, sa, Edge::from((0, 0)));
        g.add_edge(inner_node, sb, Edge::from((1, 0)));
        g.add_edge(inner_node, sc, Edge::from((2, 0)));
        g.add_edge(inner_node, sd, Edge::from((3, 0)));
        let vm = compile_and_push(&g, push);
        [
            store_val(&vm, sa),
            store_val(&vm, sb),
            store_val(&vm, sc),
            store_val(&vm, sd),
        ]
    };

    assert_eq!(build(0, 0), [Some(42), None, Some(42), None]); // A + C
    assert_eq!(build(0, 1), [Some(42), None, None, Some(99)]); // A + D
    assert_eq!(build(1, 0), [None, Some(99), Some(42), None]); // B + C
    assert_eq!(build(1, 1), [None, Some(99), None, Some(99)]); // B + D
}

// Nested/sequential: Gate exists only under Sel1's arm 0, so the result is
// PRUNED to 3 branches (not the Cartesian 4).  branches: [{A}, {B}, {C}]
//
//  [In sel]  [In val]
//       \      /
//      ($sel,$val)
//        [Sel1]            Sel1: ==0 -> o0=$val, else -> o1=88
//      o0/    \o1
//   [Gate]   [Out C]       Gate reached only via Sel1 arm 0,
//  o0/  \o1                then routes by ($val == 0)
// [Out A][Out B]
#[test]
fn test_graph_nested_sequential_branches() {
    // 2-input outer select: $sel picks the arm, $val is passed on arm 0.
    let outer_sel = || {
        node::branch(
            "(if (= 0 $sel) (list 0 $val) (list 1 88))",
            vec![
                node::Conns::try_from([true, false]).unwrap(),
                node::Conns::try_from([false, true]).unwrap(),
            ],
        )
        .unwrap()
    };
    let make_inner = || {
        let mut inner = Nested::default();
        let inlet_sel =
            inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let inlet_val = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
        let sel1 = inner.add_node(Box::new(outer_sel()) as Box<_>);
        let gate = inner.add_node(Box::new(node_select()) as Box<_>);
        let oa = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let ob = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let oc = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(inlet_sel, sel1, Edge::from((0, 0))); // $sel
        inner.add_edge(inlet_val, sel1, Edge::from((0, 1))); // $val
        inner.add_edge(sel1, gate, Edge::from((0, 0))); // arm 0 -> gate input
        inner.add_edge(sel1, oc, Edge::from((1, 0))); // arm 1 -> C
        inner.add_edge(gate, oa, Edge::from((0, 0)));
        inner.add_edge(gate, ob, Edge::from((1, 0)));
        inner
    };

    // A=0, B=1, C=2. Pruned (not Cartesian): {A}, {B}, {C}.
    assert_inner_branches(&make_inner(), 3, &[&[0], &[1], &[2]]);

    let build = |sel: i32, val: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let i_sel = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let i_val = g.add_node(Box::new(node_int(val)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let sa = g.add_node(Box::new(node_number()) as Box<_>);
        let sb = g.add_node(Box::new(node_number()) as Box<_>);
        let sc = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, i_sel, Edge::from((0, 0)));
        g.add_edge(push, i_val, Edge::from((0, 0)));
        g.add_edge(i_sel, inner_node, Edge::from((0, 0)));
        g.add_edge(i_val, inner_node, Edge::from((0, 1)));
        g.add_edge(inner_node, sa, Edge::from((0, 0)));
        g.add_edge(inner_node, sb, Edge::from((1, 0)));
        g.add_edge(inner_node, sc, Edge::from((2, 0)));
        let vm = compile_and_push(&g, push);
        [store_val(&vm, sa), store_val(&vm, sb), store_val(&vm, sc)]
    };

    // sel==0 reaches gate with $val; gate routes by ($val == 0).
    assert_eq!(build(0, 0), [Some(42), None, None]); // gate arm 0 -> A
    assert_eq!(build(0, 5), [None, Some(99), None]); // gate arm 1 -> B
    assert_eq!(build(9, 0), [None, None, Some(88)]); // sel arm 1 -> C
}

// Branch after a join: the branch is reachable from two inlet chains, but must
// be assigned ONCE per world (two branches, not four).  branches: [{A}, {B}]
//
//  [In l]  [In r]
//      \    /
//      [Sum]               (+ $a $b)
//        |
//      [Sel]
//     o0/  \o1
//  [Out A] [Out B]
#[test]
fn test_graph_nested_branch_after_join() {
    let make_inner = || {
        let mut inner = Nested::default();
        let inlet_l = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let inlet_r = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
        let sum = inner.add_node(Box::new(node::expr("(+ $a $b)").unwrap()) as Box<_>);
        let select = inner.add_node(Box::new(node_select()) as Box<_>);
        let oa = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let ob = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(inlet_l, sum, Edge::from((0, 0)));
        inner.add_edge(inlet_r, sum, Edge::from((0, 1)));
        inner.add_edge(sum, select, Edge::from((0, 0)));
        inner.add_edge(select, oa, Edge::from((0, 0)));
        inner.add_edge(select, ob, Edge::from((1, 0)));
        inner
    };

    // Two branches, NOT four (the join-fed branch is assigned once).
    assert_inner_branches(&make_inner(), 2, &[&[0], &[1]]);

    let build = |l: i32, r: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let il = g.add_node(Box::new(node_int(l)) as Box<_>);
        let ir = g.add_node(Box::new(node_int(r)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let sa = g.add_node(Box::new(node_number()) as Box<_>);
        let sb = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, il, Edge::from((0, 0)));
        g.add_edge(push, ir, Edge::from((0, 0)));
        g.add_edge(il, inner_node, Edge::from((0, 0)));
        g.add_edge(ir, inner_node, Edge::from((0, 1)));
        g.add_edge(inner_node, sa, Edge::from((0, 0)));
        g.add_edge(inner_node, sb, Edge::from((1, 0)));
        let vm = compile_and_push(&g, push);
        (store_val(&vm, sa), store_val(&vm, sb))
    };

    assert_eq!(build(0, 0), (Some(42), None)); // sum 0 -> arm 0
    assert_eq!(build(1, 0), (None, Some(99))); // sum 1 -> arm 1
}

// Static outlet: C is fed by a constant (reached via pull, no inlet), so it is
// active in EVERY branch.  branches: [{A, C}, {B, C}]
//
//    [In]          [const 123]
//     |                 |
//   [Sel]               |        (independent chain ->
//  o0/  \o1             |         always produced)
// [A]    [B]        [Out C]
#[test]
fn test_graph_nested_branch_with_constant_outlet() {
    let make_inner = || {
        let mut inner = Nested::default();
        let inlet = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let select = inner.add_node(Box::new(node_select()) as Box<_>);
        let oa = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let ob = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let konst = inner.add_node(Box::new(node::expr("123").unwrap()) as Box<_>);
        let oc = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(inlet, select, Edge::from((0, 0)));
        inner.add_edge(select, oa, Edge::from((0, 0)));
        inner.add_edge(select, ob, Edge::from((1, 0)));
        inner.add_edge(konst, oc, Edge::from((0, 0)));
        inner
    };

    // C (output 2) is active in both branches.
    assert_inner_branches(&make_inner(), 3, &[&[0, 2], &[1, 2]]);

    let build = |sel: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let sa = g.add_node(Box::new(node_number()) as Box<_>);
        let sb = g.add_node(Box::new(node_number()) as Box<_>);
        let sc = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, int, Edge::from((0, 0)));
        g.add_edge(int, inner_node, Edge::from((0, 0)));
        g.add_edge(inner_node, sa, Edge::from((0, 0)));
        g.add_edge(inner_node, sb, Edge::from((1, 0)));
        g.add_edge(inner_node, sc, Edge::from((2, 0)));
        let vm = compile_and_push(&g, push);
        [store_val(&vm, sa), store_val(&vm, sb), store_val(&vm, sc)]
    };

    assert_eq!(build(0), [Some(42), None, Some(123)]); // A + C
    assert_eq!(build(1), [None, Some(99), Some(123)]); // B + C
}

// Three-arm branch: one branch node with three arms, each to its own outlet.
// branches: [{A}, {B}, {C}]
//
//         [In]
//          |
//       [Branch]   (3 arms)
//     o0/ o1| \o2
//    [A]  [B]  [C]
#[test]
fn test_graph_nested_three_arm_branch() {
    let branch3 = || {
        node::branch(
            "(if (= 0 $x) (list 0 1) (if (= 1 $x) (list 1 2) (list 2 3)))",
            vec![
                node::Conns::try_from([true, false, false]).unwrap(),
                node::Conns::try_from([false, true, false]).unwrap(),
                node::Conns::try_from([false, false, true]).unwrap(),
            ],
        )
        .unwrap()
    };
    let make_inner = || {
        let mut inner = Nested::default();
        let inlet = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let br = inner.add_node(Box::new(branch3()) as Box<_>);
        let oa = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let ob = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let oc = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(inlet, br, Edge::from((0, 0)));
        inner.add_edge(br, oa, Edge::from((0, 0)));
        inner.add_edge(br, ob, Edge::from((1, 0)));
        inner.add_edge(br, oc, Edge::from((2, 0)));
        inner
    };

    assert_inner_branches(&make_inner(), 3, &[&[0], &[1], &[2]]);

    let build = |sel: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let sa = g.add_node(Box::new(node_number()) as Box<_>);
        let sb = g.add_node(Box::new(node_number()) as Box<_>);
        let sc = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, int, Edge::from((0, 0)));
        g.add_edge(int, inner_node, Edge::from((0, 0)));
        g.add_edge(inner_node, sa, Edge::from((0, 0)));
        g.add_edge(inner_node, sb, Edge::from((1, 0)));
        g.add_edge(inner_node, sc, Edge::from((2, 0)));
        let vm = compile_and_push(&g, push);
        [store_val(&vm, sa), store_val(&vm, sb), store_val(&vm, sc)]
    };

    assert_eq!(build(0), [Some(1), None, None]);
    assert_eq!(build(1), [None, Some(2), None]);
    assert_eq!(build(2), [None, None, Some(3)]);
}

// Two levels of nesting: branching propagates outward through both.
// inner1.branches: [{X}, {Y}]
//
//  inner2:            inner1 (wraps inner2):
//    [In]               [In]
//     |                  |
//   [Sel]            [inner2]   <- itself a branching nested graph
//  o0/ \o1           o0/  \o1
// [A]   [B]      [Out X]  [Out Y]
#[test]
fn test_graph_nested_branch_two_levels() {
    let make_inner2 = || {
        let mut g = Nested::default();
        let inlet = g.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let select = g.add_node(Box::new(node_select()) as Box<_>);
        let oa = g.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let ob = g.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        g.add_edge(inlet, select, Edge::from((0, 0)));
        g.add_edge(select, oa, Edge::from((0, 0)));
        g.add_edge(select, ob, Edge::from((1, 0)));
        g
    };
    let make_inner1 = || {
        let mut g = Nested::default();
        let inlet = g.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let inner2 = g.add_node(Box::new(make_inner2()) as Box<_>);
        let ox = g.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let oy = g.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        g.add_edge(inlet, inner2, Edge::from((0, 0)));
        g.add_edge(inner2, ox, Edge::from((0, 0)));
        g.add_edge(inner2, oy, Edge::from((1, 0)));
        g
    };

    // inner1 branches because inner2 branches.
    assert_inner_branches(&make_inner1(), 2, &[&[0], &[1]]);

    let build = |sel: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner1()) as Box<_>);
        let sx = g.add_node(Box::new(node_number()) as Box<_>);
        let sy = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, int, Edge::from((0, 0)));
        g.add_edge(int, inner_node, Edge::from((0, 0)));
        g.add_edge(inner_node, sx, Edge::from((0, 0)));
        g.add_edge(inner_node, sy, Edge::from((1, 0)));
        let vm = compile_and_push(&g, push);
        (store_val(&vm, sx), store_val(&vm, sy))
    };

    assert_eq!(build(0), (Some(42), None));
    assert_eq!(build(1), (None, Some(99)));
}

// Stateful node on a branch arm: its state persists across pushes taking arm 0.
// branches: [{A}, {B}]
//
//       [In]
//        |
//      [Sel]
//     o0/  \o1
//  [Counter] |          (state persists across pushes on arm 0)
//     |      |
//  [Out A] [Out B]
#[test]
fn test_graph_nested_branch_stateful() {
    let counter = || {
        node::expr("(begin $bang (set! state (if (number? state) (+ state 1) 0)) state)").unwrap()
    };
    let mut inner = Nested::default();
    let inlet = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
    let select = inner.add_node(Box::new(node_select()) as Box<_>);
    let count = inner.add_node(Box::new(counter()) as Box<_>);
    let oa = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    let ob = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    inner.add_edge(inlet, select, Edge::from((0, 0)));
    inner.add_edge(select, count, Edge::from((0, 0)));
    inner.add_edge(count, oa, Edge::from((0, 0)));
    inner.add_edge(select, ob, Edge::from((1, 0)));

    assert_inner_branches(&inner, 2, &[&[0], &[1]]);

    let mut g = petgraph::graph::DiGraph::new();
    let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let int = g.add_node(Box::new(node_int(0)) as Box<_>); // always arm 0
    let inner_node = g.add_node(Box::new(inner) as Box<_>);
    let sa = g.add_node(Box::new(node_number()) as Box<_>);
    g.add_edge(push, int, Edge::from((0, 0)));
    g.add_edge(int, inner_node, Edge::from((0, 0)));
    g.add_edge(inner_node, sa, Edge::from((0, 0)));

    let ctx = node::MetaCtx::new(&no_lookup);
    let eps = push_pull_entrypoints(&no_lookup, &g);
    let module = gantz_core::compile::module(&no_lookup, &g, &eps, &Default::default()).unwrap();
    let mut vm = Engine::new_base();
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, &g, &[], &mut vm);
    for f in &module {
        vm.run(f.to_pretty(100)).unwrap();
    }
    let ep = entrypoint::push(vec![push.index()], g[push].n_outputs(ctx) as u8);
    let fname = entry_fn_name(&ep.id());
    vm.call_function_by_name_with_args(&fname, vec![]).unwrap();
    vm.call_function_by_name_with_args(&fname, vec![]).unwrap();

    // Counter incremented twice on arm 0: 0 then 1.
    let count_state = node::state::extract::<i32>(&vm, &[inner_node.index(), count.index()])
        .unwrap()
        .unwrap();
    assert_eq!(count_state, 1);
    assert_eq!(store_val(&vm, sa), Some(1));
}

// Alignment: same inner shape as `parallel_branches` (two parallel Sels -> 4
// branches); asserts Node::branches() == outer meta.branches[inner_node],
// pointwise.
//
//   [In a]        [In b]
//     |             |
//   [Sel1]        [Sel2]
//  o0/  \o1      o0/  \o1
// [A]    [B]    [C]    [D]
#[test]
fn test_graph_nested_branches_align_with_meta() {
    let mut inner = Nested::default();
    let inlet_a = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
    let inlet_b = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
    let sel1 = inner.add_node(Box::new(node_select()) as Box<_>);
    let sel2 = inner.add_node(Box::new(node_select()) as Box<_>);
    let oa = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    let ob = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    let oc = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    let od = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    inner.add_edge(inlet_a, sel1, Edge::from((0, 0)));
    inner.add_edge(inlet_b, sel2, Edge::from((0, 0)));
    inner.add_edge(sel1, oa, Edge::from((0, 0)));
    inner.add_edge(sel1, ob, Edge::from((1, 0)));
    inner.add_edge(sel2, oc, Edge::from((0, 0)));
    inner.add_edge(sel2, od, Edge::from((1, 0)));

    let ctx = node::MetaCtx::new(&no_lookup);
    let declared = inner.branches(ctx);
    assert_eq!(declared.len(), 4);

    let mut g = petgraph::graph::DiGraph::new();
    let va = g.add_node(Box::new(node_int(0)) as Box<dyn DebugNode>);
    let vb = g.add_node(Box::new(node_int(0)) as Box<_>);
    let inner_node = g.add_node(Box::new(inner) as Box<_>);
    g.add_edge(va, inner_node, Edge::from((0, 0)));
    g.add_edge(vb, inner_node, Edge::from((0, 1)));
    let meta = gantz_core::compile::Meta::from_graph(&no_lookup, &g).unwrap();
    let observed = meta.branches.get(&inner_node.index()).unwrap();

    assert_eq!(observed.len(), declared.len());
    for (got, want) in observed.iter().zip(&declared) {
        let node::EvalConf::Set(want) = want else {
            panic!("expected Set")
        };
        assert_eq!(got, want);
    }
}

// Regression: two independent (non-branching) chains form a multi-component
// flow graph. It must compile (previously the single-entry assertion panicked)
// and report no external branching.  branches: []
//
//  [In a]   [In b]
//    |        |
//  [+1]     [+2]
//    |        |
// [Out A]  [Out B]
#[test]
fn test_graph_nested_multi_component_no_branch() {
    let mut inner = Nested::default();
    let inlet_a = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
    let inlet_b = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
    let id_a = inner.add_node(Box::new(node::expr("(+ $x 1)").unwrap()) as Box<_>);
    let id_b = inner.add_node(Box::new(node::expr("(+ $x 2)").unwrap()) as Box<_>);
    let oa = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    let ob = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    inner.add_edge(inlet_a, id_a, Edge::from((0, 0)));
    inner.add_edge(id_a, oa, Edge::from((0, 0)));
    inner.add_edge(inlet_b, id_b, Edge::from((0, 0)));
    inner.add_edge(id_b, ob, Edge::from((0, 0)));

    // No external branching.
    assert_inner_branches(&inner, 2, &[]);

    let mut g = petgraph::graph::DiGraph::new();
    let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let i10 = g.add_node(Box::new(node_int(10)) as Box<_>);
    let i20 = g.add_node(Box::new(node_int(20)) as Box<_>);
    let inner_node = g.add_node(Box::new(inner) as Box<_>);
    let sa = g.add_node(Box::new(node_number()) as Box<_>);
    let sb = g.add_node(Box::new(node_number()) as Box<_>);
    g.add_edge(push, i10, Edge::from((0, 0)));
    g.add_edge(push, i20, Edge::from((0, 0)));
    g.add_edge(i10, inner_node, Edge::from((0, 0)));
    g.add_edge(i20, inner_node, Edge::from((0, 1)));
    g.add_edge(inner_node, sa, Edge::from((0, 0)));
    g.add_edge(inner_node, sb, Edge::from((1, 0)));
    let vm = compile_and_push(&g, push);
    assert_eq!(store_val(&vm, sa), Some(11)); // 10 + 1
    assert_eq!(store_val(&vm, sb), Some(22)); // 20 + 2
}

// Reconvergent intermediates: both arms pass through a distinct intermediate
// then feed the SAME outlet -> no external branching (exercises is_join via an
// intermediate). branches: []
//
//        [In]
//         |
//       [Sel]
//      o0/  \o1
//    [+10]  [+1]
//       \   /
//      [Out A]
#[test]
fn test_graph_nested_branch_reconvergent_intermediates() {
    let make_inner = || {
        let mut inner = Nested::default();
        let inlet = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let select = inner.add_node(Box::new(node_select()) as Box<_>);
        let add10 = inner.add_node(Box::new(node::expr("(+ $x 10)").unwrap()) as Box<_>);
        let add1 = inner.add_node(Box::new(node::expr("(+ $x 1)").unwrap()) as Box<_>);
        let outlet = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(inlet, select, Edge::from((0, 0)));
        inner.add_edge(select, add10, Edge::from((0, 0)));
        inner.add_edge(add10, outlet, Edge::from((0, 0)));
        inner.add_edge(select, add1, Edge::from((1, 0)));
        inner.add_edge(add1, outlet, Edge::from((0, 0)));
        inner
    };
    assert_inner_branches(&make_inner(), 1, &[]);
    let build = |sel: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let store = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, int, Edge::from((0, 0)));
        g.add_edge(int, inner_node, Edge::from((0, 0)));
        g.add_edge(inner_node, store, Edge::from((0, 0)));
        store_val(&compile_and_push(&g, push), store)
    };
    assert_eq!(build(0), Some(52)); // 42 + 10
    assert_eq!(build(1), Some(100)); // 99 + 1
}

// Mixed direct/intermediate arms: arm 0 goes straight to its outlet, arm 1 via
// an intermediate. branches: [{A}, {B}]
#[test]
fn test_graph_nested_branch_mixed_direct_intermediate() {
    let make_inner = || {
        let mut inner = Nested::default();
        let inlet = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let select = inner.add_node(Box::new(node_select()) as Box<_>);
        let add1 = inner.add_node(Box::new(node::expr("(+ $x 1)").unwrap()) as Box<_>);
        let outlet_a = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let outlet_b = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(inlet, select, Edge::from((0, 0)));
        inner.add_edge(select, outlet_a, Edge::from((0, 0)));
        inner.add_edge(select, add1, Edge::from((1, 0)));
        inner.add_edge(add1, outlet_b, Edge::from((0, 0)));
        inner
    };
    assert_inner_branches(&make_inner(), 2, &[&[0], &[1]]);
    let build = |sel: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let sa = g.add_node(Box::new(node_number()) as Box<_>);
        let sb = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, int, Edge::from((0, 0)));
        g.add_edge(int, inner_node, Edge::from((0, 0)));
        g.add_edge(inner_node, sa, Edge::from((0, 0)));
        g.add_edge(inner_node, sb, Edge::from((1, 0)));
        let vm = compile_and_push(&g, push);
        (store_val(&vm, sa), store_val(&vm, sb))
    };
    assert_eq!(build(0), (Some(42), None)); // direct
    assert_eq!(build(1), (None, Some(100))); // 99 + 1
}

// Chained intermediates: each arm passes through a two-node chain.
// branches: [{A}, {B}]
#[test]
fn test_graph_nested_branch_chained_intermediates() {
    let make_inner = || {
        let mut inner = Nested::default();
        let inlet = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let select = inner.add_node(Box::new(node_select()) as Box<_>);
        let a10 = inner.add_node(Box::new(node::expr("(+ $x 10)").unwrap()) as Box<_>);
        let a1 = inner.add_node(Box::new(node::expr("(+ $x 1)").unwrap()) as Box<_>);
        let b10 = inner.add_node(Box::new(node::expr("(+ $x 10)").unwrap()) as Box<_>);
        let b1 = inner.add_node(Box::new(node::expr("(+ $x 1)").unwrap()) as Box<_>);
        let outlet_a = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let outlet_b = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(inlet, select, Edge::from((0, 0)));
        inner.add_edge(select, a10, Edge::from((0, 0)));
        inner.add_edge(a10, a1, Edge::from((0, 0)));
        inner.add_edge(a1, outlet_a, Edge::from((0, 0)));
        inner.add_edge(select, b10, Edge::from((1, 0)));
        inner.add_edge(b10, b1, Edge::from((0, 0)));
        inner.add_edge(b1, outlet_b, Edge::from((0, 0)));
        inner
    };
    assert_inner_branches(&make_inner(), 2, &[&[0], &[1]]);
    let build = |sel: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let sa = g.add_node(Box::new(node_number()) as Box<_>);
        let sb = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, int, Edge::from((0, 0)));
        g.add_edge(int, inner_node, Edge::from((0, 0)));
        g.add_edge(inner_node, sa, Edge::from((0, 0)));
        g.add_edge(inner_node, sb, Edge::from((1, 0)));
        let vm = compile_and_push(&g, push);
        (store_val(&vm, sa), store_val(&vm, sb))
    };
    assert_eq!(build(0), (Some(53), None)); // 42 + 10 + 1
    assert_eq!(build(1), (None, Some(110))); // 99 + 10 + 1
}

// Cascading reconvergence: three sequential branches, but Select A and Select B
// each reconverge at a join, so only Select C affects the outlet. The 2^3 = 8
// inner worlds collapse to just TWO external branches (dedup by outlet set;
// the prior implementation reported 8). branches: [{A}, {B}]
#[test]
fn test_graph_nested_cascading_reconvergence() {
    let make_inner = || {
        let mut inner = Nested::default();
        let in1 = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let in2 = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
        let in3 = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
        let sa = inner.add_node(Box::new(node_select()) as Box<_>);
        let a10 = inner.add_node(Box::new(node::expr("(+ $x 10)").unwrap()) as Box<_>);
        let a20 = inner.add_node(Box::new(node::expr("(+ $x 20)").unwrap()) as Box<_>);
        let joina = inner.add_node(Box::new(node::expr("(begin $x)").unwrap()) as Box<_>);
        let passa = inner.add_node(Box::new(node::expr("(begin $l $r)").unwrap()) as Box<_>);
        let sb = inner.add_node(Box::new(node_select()) as Box<_>);
        let b30 = inner.add_node(Box::new(node::expr("(+ $x 30)").unwrap()) as Box<_>);
        let b40 = inner.add_node(Box::new(node::expr("(+ $x 40)").unwrap()) as Box<_>);
        let joinb = inner.add_node(Box::new(node::expr("(begin $x)").unwrap()) as Box<_>);
        let passb = inner.add_node(Box::new(node::expr("(begin $l $r)").unwrap()) as Box<_>);
        let sc = inner.add_node(Box::new(node_select()) as Box<_>);
        let oa = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let ob = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(in1, sa, Edge::from((0, 0)));
        inner.add_edge(sa, a10, Edge::from((0, 0)));
        inner.add_edge(sa, a20, Edge::from((1, 0)));
        inner.add_edge(a10, joina, Edge::from((0, 0)));
        inner.add_edge(a20, joina, Edge::from((0, 0)));
        inner.add_edge(joina, passa, Edge::from((0, 0)));
        inner.add_edge(in2, passa, Edge::from((0, 1)));
        inner.add_edge(passa, sb, Edge::from((0, 0)));
        inner.add_edge(sb, b30, Edge::from((0, 0)));
        inner.add_edge(sb, b40, Edge::from((1, 0)));
        inner.add_edge(b30, joinb, Edge::from((0, 0)));
        inner.add_edge(b40, joinb, Edge::from((0, 0)));
        inner.add_edge(joinb, passb, Edge::from((0, 0)));
        inner.add_edge(in3, passb, Edge::from((0, 1)));
        inner.add_edge(passb, sc, Edge::from((0, 0)));
        inner.add_edge(sc, oa, Edge::from((0, 0)));
        inner.add_edge(sc, ob, Edge::from((1, 0)));
        inner
    };
    assert_inner_branches(&make_inner(), 2, &[&[0], &[1]]);
    let build = |c: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let v1 = g.add_node(Box::new(node_int(1)) as Box<_>);
        let v2 = g.add_node(Box::new(node_int(1)) as Box<_>);
        let v3 = g.add_node(Box::new(node_int(c)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let sa = g.add_node(Box::new(node_number()) as Box<_>);
        let sb = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, v1, Edge::from((0, 0)));
        g.add_edge(push, v2, Edge::from((0, 0)));
        g.add_edge(push, v3, Edge::from((0, 0)));
        g.add_edge(v1, inner_node, Edge::from((0, 0)));
        g.add_edge(v2, inner_node, Edge::from((0, 1)));
        g.add_edge(v3, inner_node, Edge::from((0, 2)));
        g.add_edge(inner_node, sa, Edge::from((0, 0)));
        g.add_edge(inner_node, sb, Edge::from((1, 0)));
        let vm = compile_and_push(&g, push);
        (store_val(&vm, sa), store_val(&vm, sb))
    };
    assert_eq!(build(0), (Some(42), None)); // SelectC arm 0 -> A
    assert_eq!(build(1), (None, Some(99))); // SelectC arm 1 -> B
}

// Inner reconvergence + an independent outer branch in the same graph:
// Select1 reconverges to A (always active); Select2 picks B or C.
// branches: [{A, B}, {A, C}]
#[test]
fn test_graph_nested_inner_reconvergence_outer_branching() {
    let make_inner = || {
        let mut inner = Nested::default();
        let in1 = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let in2 = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
        let s1 = inner.add_node(Box::new(node_select()) as Box<_>);
        let a10 = inner.add_node(Box::new(node::expr("(+ $x 10)").unwrap()) as Box<_>);
        let a20 = inner.add_node(Box::new(node::expr("(+ $x 20)").unwrap()) as Box<_>);
        let join = inner.add_node(Box::new(node::expr("(begin $x)").unwrap()) as Box<_>);
        let s2 = inner.add_node(Box::new(node_select()) as Box<_>);
        let oa = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let ob = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let oc = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(in1, s1, Edge::from((0, 0)));
        inner.add_edge(s1, a10, Edge::from((0, 0)));
        inner.add_edge(s1, a20, Edge::from((1, 0)));
        inner.add_edge(a10, join, Edge::from((0, 0)));
        inner.add_edge(a20, join, Edge::from((0, 0)));
        inner.add_edge(join, oa, Edge::from((0, 0)));
        inner.add_edge(in2, s2, Edge::from((0, 0)));
        inner.add_edge(s2, ob, Edge::from((0, 0)));
        inner.add_edge(s2, oc, Edge::from((1, 0)));
        inner
    };
    assert_inner_branches(&make_inner(), 3, &[&[0, 1], &[0, 2]]);
    let build = |x: i32, y: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let v1 = g.add_node(Box::new(node_int(x)) as Box<_>);
        let v2 = g.add_node(Box::new(node_int(y)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let sa = g.add_node(Box::new(node_number()) as Box<_>);
        let sb = g.add_node(Box::new(node_number()) as Box<_>);
        let sc = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, v1, Edge::from((0, 0)));
        g.add_edge(push, v2, Edge::from((0, 0)));
        g.add_edge(v1, inner_node, Edge::from((0, 0)));
        g.add_edge(v2, inner_node, Edge::from((0, 1)));
        g.add_edge(inner_node, sa, Edge::from((0, 0)));
        g.add_edge(inner_node, sb, Edge::from((1, 0)));
        g.add_edge(inner_node, sc, Edge::from((2, 0)));
        let vm = compile_and_push(&g, push);
        [store_val(&vm, sa), store_val(&vm, sb), store_val(&vm, sc)]
    };
    assert_eq!(build(0, 0), [Some(52), Some(42), None]); // A=42+10, B
    assert_eq!(build(0, 1), [Some(52), None, Some(99)]); // A, C
    assert_eq!(build(1, 0), [Some(119), Some(42), None]); // A=99+20, B
}

// A Static inlet used at branch depth 3: `value` feeds `depth3`, which sits on
// Select3's arm 0. node_inputs_in_scope must keep `value` in scope inside that
// arm even though it enters from outside the arm. Sequential -> 4 branches.
// branches: [{A}, {B}, {C}, {D}]
#[test]
fn test_graph_nested_static_inlet_at_depth_three() {
    let make_inner = || {
        let mut inner = Nested::default();
        let f1 = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let f2 = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
        let f3 = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
        let value = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
        let s1 = inner.add_node(Box::new(node_select()) as Box<_>);
        let pa = inner.add_node(Box::new(node::expr("(begin $l $r)").unwrap()) as Box<_>);
        let s2 = inner.add_node(Box::new(node_select()) as Box<_>);
        let pb = inner.add_node(Box::new(node::expr("(begin $l $r)").unwrap()) as Box<_>);
        let s3 = inner.add_node(Box::new(node_select()) as Box<_>);
        let d3 = inner.add_node(Box::new(node::expr("(begin $l $r)").unwrap()) as Box<_>);
        let oa = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let ob = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let oc = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let od = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(f1, s1, Edge::from((0, 0)));
        inner.add_edge(s1, oa, Edge::from((0, 0)));
        inner.add_edge(s1, pa, Edge::from((1, 0)));
        inner.add_edge(f2, pa, Edge::from((0, 1)));
        inner.add_edge(pa, s2, Edge::from((0, 0)));
        inner.add_edge(s2, ob, Edge::from((0, 0)));
        inner.add_edge(s2, pb, Edge::from((1, 0)));
        inner.add_edge(f3, pb, Edge::from((0, 1)));
        inner.add_edge(pb, s3, Edge::from((0, 0)));
        inner.add_edge(s3, d3, Edge::from((0, 0)));
        inner.add_edge(value, d3, Edge::from((0, 1)));
        inner.add_edge(d3, oc, Edge::from((0, 0)));
        inner.add_edge(s3, od, Edge::from((1, 0)));
        inner
    };
    assert_inner_branches(&make_inner(), 4, &[&[0], &[1], &[2], &[3]]);
    let build = |a: i32, b: i32, c: i32, v: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let n1 = g.add_node(Box::new(node_int(a)) as Box<_>);
        let n2 = g.add_node(Box::new(node_int(b)) as Box<_>);
        let n3 = g.add_node(Box::new(node_int(c)) as Box<_>);
        let nv = g.add_node(Box::new(node_int(v)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let st: Vec<_> = (0..4)
            .map(|_| g.add_node(Box::new(node_number()) as Box<_>))
            .collect();
        for n in [n1, n2, n3, nv] {
            g.add_edge(push, n, Edge::from((0, 0)));
        }
        g.add_edge(n1, inner_node, Edge::from((0, 0)));
        g.add_edge(n2, inner_node, Edge::from((0, 1)));
        g.add_edge(n3, inner_node, Edge::from((0, 2)));
        g.add_edge(nv, inner_node, Edge::from((0, 3)));
        for (k, &s) in st.iter().enumerate() {
            g.add_edge(inner_node, s, Edge::from((k as u16, 0)));
        }
        let vm = compile_and_push(&g, push);
        st.iter().map(|&s| store_val(&vm, s)).collect::<Vec<_>>()
    };
    assert_eq!(build(0, 9, 9, 7), [Some(42), None, None, None]); // f1==0 -> A
    assert_eq!(build(9, 0, 9, 7), [None, Some(42), None, None]); // f2==0 -> B
    assert_eq!(build(9, 9, 0, 7), [None, None, Some(7), None]); // f3==0 -> depth3 = value 7
    assert_eq!(build(9, 9, 9, 7), [None, None, None, Some(99)]); // all !=0 -> D
}

// Three independent parallel branches -> 2^3 = 8 external branches (a 3-component
// flow graph). branches: all 8 of {A|B} x {C|D} x {E|F}.
#[test]
fn test_graph_nested_multi_branch_three() {
    let make_inner = || {
        let mut inner = Nested::default();
        let i1 = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let i2 = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
        let i3 = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
        let s1 = inner.add_node(Box::new(node_select()) as Box<_>);
        let s2 = inner.add_node(Box::new(node_select()) as Box<_>);
        let s3 = inner.add_node(Box::new(node_select()) as Box<_>);
        let mut add =
            |n: i32| inner.add_node(Box::new(node::expr(format!("(+ $x {n})")).unwrap()) as Box<_>);
        let (a10, a20, a30, a40, a50, a60) = (add(10), add(20), add(30), add(40), add(50), add(60));
        let o: Vec<_> = (0..6)
            .map(|_| inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>))
            .collect();
        inner.add_edge(i1, s1, Edge::from((0, 0)));
        inner.add_edge(i2, s2, Edge::from((0, 0)));
        inner.add_edge(i3, s3, Edge::from((0, 0)));
        for (sel, lo, hi, ol, oh) in [
            (s1, a10, a20, o[0], o[1]),
            (s2, a30, a40, o[2], o[3]),
            (s3, a50, a60, o[4], o[5]),
        ] {
            inner.add_edge(sel, lo, Edge::from((0, 0)));
            inner.add_edge(lo, ol, Edge::from((0, 0)));
            inner.add_edge(sel, hi, Edge::from((1, 0)));
            inner.add_edge(hi, oh, Edge::from((0, 0)));
        }
        inner
    };
    assert_inner_branches(
        &make_inner(),
        6,
        &[
            &[0, 2, 4],
            &[0, 2, 5],
            &[0, 3, 4],
            &[0, 3, 5],
            &[1, 2, 4],
            &[1, 2, 5],
            &[1, 3, 4],
            &[1, 3, 5],
        ],
    );
    let build = |a: i32, b: i32, c: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let n1 = g.add_node(Box::new(node_int(a)) as Box<_>);
        let n2 = g.add_node(Box::new(node_int(b)) as Box<_>);
        let n3 = g.add_node(Box::new(node_int(c)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let st: Vec<_> = (0..6)
            .map(|_| g.add_node(Box::new(node_number()) as Box<_>))
            .collect();
        for n in [n1, n2, n3] {
            g.add_edge(push, n, Edge::from((0, 0)));
        }
        g.add_edge(n1, inner_node, Edge::from((0, 0)));
        g.add_edge(n2, inner_node, Edge::from((0, 1)));
        g.add_edge(n3, inner_node, Edge::from((0, 2)));
        for (k, &s) in st.iter().enumerate() {
            g.add_edge(inner_node, s, Edge::from((k as u16, 0)));
        }
        let vm = compile_and_push(&g, push);
        st.iter().map(|&s| store_val(&vm, s)).collect::<Vec<_>>()
    };
    // (0,0,0): A=42+10, C=42+30, E=42+50 fire; B,D,F dead.
    assert_eq!(
        build(0, 0, 0),
        [Some(52), None, Some(72), None, Some(92), None]
    );
    // (1,1,1): B=99+20, D=99+40, F=99+60.
    assert_eq!(
        build(1, 1, 1),
        [None, Some(119), None, Some(139), None, Some(159)]
    );
    // (0,1,0): A, D, E.
    assert_eq!(
        build(0, 1, 0),
        [Some(52), None, None, Some(139), Some(92), None]
    );
}

// ===========================================================================
// Push-through-outlet branching tests.
//
// Here the `push_eval` lives *inside* the nested graph and propagates *out*
// through the graph's outlets via an interior branch. The bridged graph node
// therefore acts as a branch node in the parent for that entrypoint, so the
// parent only evaluates downstream of the outlets the taken arm produced.
// Each test's INNER graph (with the push inside) is sketched above it; `[Sel]`
// is `node_select` (input ==0 -> o0(42), else -> o1(99)).
// ===========================================================================

// Divergent push-through: each arm drives its own outlet -> its own outer store.
//
//   INNER: [push]->[int(sel)]->[Sel]        OUTER: [inner]
//                            o0/  \o1               o0/  \o1
//                        [OutA]   [OutB]      [store_a] [store_b]
#[test]
fn test_graph_nested_push_through_divergent_branch() {
    let build = |sel: i32| {
        let mut inner = Nested::default();
        let push = inner.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = inner.add_node(Box::new(node_int(sel)) as Box<_>);
        let select = inner.add_node(Box::new(node_select()) as Box<_>);
        let outlet_a = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let outlet_b = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(push, int, Edge::from((0, 0)));
        inner.add_edge(int, select, Edge::from((0, 0)));
        inner.add_edge(select, outlet_a, Edge::from((0, 0)));
        inner.add_edge(select, outlet_b, Edge::from((1, 0)));

        let ctx = node::MetaCtx::new(&no_lookup);
        let push_n = inner[push].n_outputs(ctx) as u8;

        let mut g = petgraph::graph::DiGraph::new();
        let inner_node = g.add_node(Box::new(inner) as Box<dyn DebugNode>);
        let store_a = g.add_node(Box::new(node_number()) as Box<_>);
        let store_b = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(inner_node, store_a, Edge::from((0, 0)));
        g.add_edge(inner_node, store_b, Edge::from((1, 0)));

        let vm = compile_and_push_nested(&g, vec![inner_node.index(), push.index()], push_n);
        (store_val(&vm, store_a), store_val(&vm, store_b))
    };

    assert_eq!(build(0), (Some(42), None)); // arm 0 -> outlet A -> store_a
    assert_eq!(build(1), (None, Some(99))); // arm 1 -> outlet B -> store_b
}

// Dead-arm push-through: arm 1 leaves the select output unconnected, so it
// produces nothing and no outer store is written.
//
//   INNER: [push]->[int(sel)]->[Sel]        OUTER: [inner]
//                            o0|  \o1 (dead)         o0|
//                        [OutA]    x              [store]
#[test]
fn test_graph_nested_push_through_dead_arm() {
    let build = |sel: i32| {
        let mut inner = Nested::default();
        let push = inner.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = inner.add_node(Box::new(node_int(sel)) as Box<_>);
        let select = inner.add_node(Box::new(node_select()) as Box<_>);
        let outlet = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(push, int, Edge::from((0, 0)));
        inner.add_edge(int, select, Edge::from((0, 0)));
        inner.add_edge(select, outlet, Edge::from((0, 0)));
        // Select output 1 left unconnected (dead arm).

        let ctx = node::MetaCtx::new(&no_lookup);
        let push_n = inner[push].n_outputs(ctx) as u8;

        let mut g = petgraph::graph::DiGraph::new();
        let inner_node = g.add_node(Box::new(inner) as Box<dyn DebugNode>);
        let store = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(inner_node, store, Edge::from((0, 0)));

        let vm = compile_and_push_nested(&g, vec![inner_node.index(), push.index()], push_n);
        store_val(&vm, store)
    };

    assert_eq!(build(0), Some(42)); // arm 0 -> outlet -> store
    assert_eq!(build(1), None); // arm 1 -> dead, store never evaluated
}

// Multi-output-arm push-through: arm 0 fires two outlets, arm 1 fires one.
//
//   INNER: [push]->[int(sel)]->[Branch]     arm 0 -> o0,o1 (values 10,20)
//                          o0/o1|\o2         arm 1 -> o2    (value 30)
//                     [A][B][C]              OUTER stores: a,b,c
#[test]
fn test_graph_nested_push_through_multi_outlet_arm() {
    let branch3 = || {
        node::branch(
            "(if (= 0 $x) (list 0 (list 10 20)) (list 1 30))",
            vec![
                node::Conns::try_from([true, true, false]).unwrap(),
                node::Conns::try_from([false, false, true]).unwrap(),
            ],
        )
        .unwrap()
    };
    let build = |sel: i32| {
        let mut inner = Nested::default();
        let push = inner.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = inner.add_node(Box::new(node_int(sel)) as Box<_>);
        let br = inner.add_node(Box::new(branch3()) as Box<_>);
        let outlet_a = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let outlet_b = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let outlet_c = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(push, int, Edge::from((0, 0)));
        inner.add_edge(int, br, Edge::from((0, 0)));
        inner.add_edge(br, outlet_a, Edge::from((0, 0)));
        inner.add_edge(br, outlet_b, Edge::from((1, 0)));
        inner.add_edge(br, outlet_c, Edge::from((2, 0)));

        let ctx = node::MetaCtx::new(&no_lookup);
        let push_n = inner[push].n_outputs(ctx) as u8;

        let mut g = petgraph::graph::DiGraph::new();
        let inner_node = g.add_node(Box::new(inner) as Box<dyn DebugNode>);
        let store_a = g.add_node(Box::new(node_number()) as Box<_>);
        let store_b = g.add_node(Box::new(node_number()) as Box<_>);
        let store_c = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(inner_node, store_a, Edge::from((0, 0)));
        g.add_edge(inner_node, store_b, Edge::from((1, 0)));
        g.add_edge(inner_node, store_c, Edge::from((2, 0)));

        let vm = compile_and_push_nested(&g, vec![inner_node.index(), push.index()], push_n);
        (
            store_val(&vm, store_a),
            store_val(&vm, store_b),
            store_val(&vm, store_c),
        )
    };

    assert_eq!(build(0), (Some(10), Some(20), None)); // arm 0 -> a,b
    assert_eq!(build(1), (None, None, Some(30))); // arm 1 -> c
}

// Two-level push-through: the push is inside the *innermost* graph; its branch
// propagates out through two levels of outlets. The middle graph branches
// because the inner one does (multi-level pattern threading).
//
//   INNER2: [push]->[int(sel)]->[Sel]->{oa,ob}
//   INNER1: [inner2]->{ox,oy}
//   OUTER:  [inner1]->{store_x, store_y}
#[test]
fn test_graph_nested_push_through_two_levels() {
    let build = |sel: i32| {
        let mut inner2 = Nested::default();
        let push = inner2.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = inner2.add_node(Box::new(node_int(sel)) as Box<_>);
        let select = inner2.add_node(Box::new(node_select()) as Box<_>);
        let oa = inner2.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let ob = inner2.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner2.add_edge(push, int, Edge::from((0, 0)));
        inner2.add_edge(int, select, Edge::from((0, 0)));
        inner2.add_edge(select, oa, Edge::from((0, 0)));
        inner2.add_edge(select, ob, Edge::from((1, 0)));

        let ctx = node::MetaCtx::new(&no_lookup);
        let push_n = inner2[push].n_outputs(ctx) as u8;

        let mut inner1 = Nested::default();
        let inner2_node = inner1.add_node(Box::new(inner2) as Box<dyn DebugNode>);
        let ox = inner1.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let oy = inner1.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner1.add_edge(inner2_node, ox, Edge::from((0, 0)));
        inner1.add_edge(inner2_node, oy, Edge::from((1, 0)));

        let mut g = petgraph::graph::DiGraph::new();
        let inner1_node = g.add_node(Box::new(inner1) as Box<dyn DebugNode>);
        let store_x = g.add_node(Box::new(node_number()) as Box<_>);
        let store_y = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(inner1_node, store_x, Edge::from((0, 0)));
        g.add_edge(inner1_node, store_y, Edge::from((1, 0)));

        let vm = compile_and_push_nested(
            &g,
            vec![inner1_node.index(), inner2_node.index(), push.index()],
            push_n,
        );
        (store_val(&vm, store_x), store_val(&vm, store_y))
    };

    assert_eq!(build(0), (Some(42), None));
    assert_eq!(build(1), (None, Some(99)));
}

// Reconvergent push-through: a divergent interior branch whose two arms feed
// distinct outlets that re-join at a single outer store - a phi across the
// bridge boundary. The store always fires, with the taken arm's value.
//
//   INNER: [push]->[int(sel)]->[Sel]->{oa(o0), ob(o1)}
//   OUTER: inner.o0 -\
//          inner.o1 --> [store]
#[test]
fn test_graph_nested_push_through_reconvergent_branch() {
    let build = |sel: i32| {
        let mut inner = Nested::default();
        let push = inner.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = inner.add_node(Box::new(node_int(sel)) as Box<_>);
        let select = inner.add_node(Box::new(node_select()) as Box<_>);
        let oa = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let ob = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(push, int, Edge::from((0, 0)));
        inner.add_edge(int, select, Edge::from((0, 0)));
        inner.add_edge(select, oa, Edge::from((0, 0)));
        inner.add_edge(select, ob, Edge::from((1, 0)));

        let ctx = node::MetaCtx::new(&no_lookup);
        let push_n = inner[push].n_outputs(ctx) as u8;

        let mut g = petgraph::graph::DiGraph::new();
        let inner_node = g.add_node(Box::new(inner) as Box<dyn DebugNode>);
        let store = g.add_node(Box::new(node_number()) as Box<_>);
        // Both arms route to the same store (phi reconvergence across the bridge).
        g.add_edge(inner_node, store, Edge::from((0, 0)));
        g.add_edge(inner_node, store, Edge::from((1, 0)));

        let vm = compile_and_push_nested(&g, vec![inner_node.index(), push.index()], push_n);
        store_val(&vm, store)
    };

    assert_eq!(build(0), Some(42)); // arm 0 -> outlet A -> store
    assert_eq!(build(1), Some(99)); // arm 1 -> outlet B -> store
}

// Push-through branch alongside an always-active outlet: the push also drives a
// constant-fed outlet that every arm produces, so its store always fires while
// the branch arms route to their own stores.
//
//   INNER: [push]-+->[int(sel)]->[Sel]->{oa(o0), ob(o1)}
//                 +->[int(7)]---------->{oc(o2)}   (always produced)
//   OUTER: inner.{o0,o1,o2} -> {store_a, store_b, store_c}
#[test]
fn test_graph_nested_push_through_branch_with_constant_outlet() {
    let build = |sel: i32| {
        let mut inner = Nested::default();
        let push = inner.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = inner.add_node(Box::new(node_int(sel)) as Box<_>);
        let select = inner.add_node(Box::new(node_select()) as Box<_>);
        let seven = inner.add_node(Box::new(node_int(7)) as Box<_>);
        let outlet_a = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let outlet_b = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        let outlet_c = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(push, int, Edge::from((0, 0)));
        inner.add_edge(int, select, Edge::from((0, 0)));
        inner.add_edge(select, outlet_a, Edge::from((0, 0)));
        inner.add_edge(select, outlet_b, Edge::from((1, 0)));
        inner.add_edge(push, seven, Edge::from((0, 0)));
        inner.add_edge(seven, outlet_c, Edge::from((0, 0)));

        let ctx = node::MetaCtx::new(&no_lookup);
        let push_n = inner[push].n_outputs(ctx) as u8;

        let mut g = petgraph::graph::DiGraph::new();
        let inner_node = g.add_node(Box::new(inner) as Box<dyn DebugNode>);
        let store_a = g.add_node(Box::new(node_number()) as Box<_>);
        let store_b = g.add_node(Box::new(node_number()) as Box<_>);
        let store_c = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(inner_node, store_a, Edge::from((0, 0)));
        g.add_edge(inner_node, store_b, Edge::from((1, 0)));
        g.add_edge(inner_node, store_c, Edge::from((2, 0)));

        let vm = compile_and_push_nested(&g, vec![inner_node.index(), push.index()], push_n);
        (
            store_val(&vm, store_a),
            store_val(&vm, store_b),
            store_val(&vm, store_c),
        )
    };

    assert_eq!(build(0), (Some(42), None, Some(7))); // arm 0 -> a, plus constant c
    assert_eq!(build(1), (None, Some(99), Some(7))); // arm 1 -> b, plus constant c
}

// ===========================================================================
// Multi-root branch-reconvergence ordering tests.
//
// A single entrypoint with two flow roots, where one root branches and its arms
// reconverge at a join that ALSO consumes the other root's value. The join is
// the branch's post-dominator yet depends on a second root: previously the
// branch root was emitted first and the join referenced the second root's output
// before it was defined (a `FreeIdentifier` VM error). Fixed by `order_roots`
// (emit a producing component before a consuming one) plus destructuring a
// terminal block's last node so its outputs are available cross-component.
// ===========================================================================

// Build, compile and run `g` from two push sources in one entrypoint; returns
// the VM for state queries.
fn run_two_push<N: DebugNode + ?Sized>(
    g: &petgraph::graph::DiGraph<Box<N>, Edge>,
    a: (Vec<usize>, u8),
    b: (Vec<usize>, u8),
) -> Engine {
    let ep = entrypoint::from_sources([push_source(a.0, a.1), push_source(b.0, b.1)]);
    let module =
        gantz_core::compile::module(&no_lookup, g, &[ep.clone()], &Default::default()).unwrap();
    let mut vm = Engine::new_base();
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, g, &[], &mut vm);
    for f in &module {
        vm.run(f.to_pretty(100)).unwrap();
    }
    vm.call_function_by_name_with_args(&entry_fn_name(&ep.id()), vec![])
        .unwrap();
    vm
}

// Two push roots; Root A branches and its arms reconverge at `add`, which also
// takes Root B's `int(20)`.
//
//   ROOT A: [push_a]->[int sel]->[select]   o0,o1 -> add.$l (phi)
//   ROOT B: [push_b]->[int 20] -------------------> add.$r ;  add -> store
#[test]
fn test_multiroot_branch_join_external_pred() {
    let build = |sel: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push_a = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let select = g.add_node(Box::new(node_select()) as Box<_>);
        let push_b = g.add_node(Box::new(node_push()) as Box<_>);
        let twenty = g.add_node(Box::new(node_int(20)) as Box<_>);
        let add = g.add_node(Box::new(node::expr("(+ $l $r)").unwrap()) as Box<_>);
        let store = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push_a, int, Edge::from((0, 0)));
        g.add_edge(int, select, Edge::from((0, 0)));
        g.add_edge(select, add, Edge::from((0, 0))); // arm 0 -> add.l
        g.add_edge(select, add, Edge::from((1, 0))); // arm 1 -> add.l
        g.add_edge(push_b, twenty, Edge::from((0, 0)));
        g.add_edge(twenty, add, Edge::from((0, 1))); // -> add.r
        g.add_edge(add, store, Edge::from((0, 0)));
        let n = g[push_a].n_outputs(node::MetaCtx::new(&no_lookup)) as u8;
        let vm = run_two_push(&g, (vec![push_a.index()], n), (vec![push_b.index()], n));
        store_val(&vm, store)
    };
    assert_eq!(build(0), Some(62)); // arm 0: 42 + 20
    assert_eq!(build(1), Some(119)); // arm 1: 99 + 20
}

// Sibling-shape guard: the same logical graph, but the predecessor's nodes are
// added first so the topological `last`-chaining linearizes `int(20)` ahead of
// the branch within a single component. This shape already worked; it verifies
// the terminal-destructure / `order_roots` changes don't regress the linearized
// form.
#[test]
fn test_multiroot_branch_join_external_pred_reversed() {
    let build = |sel: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        // Root B first (lower ids).
        let push_b = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let twenty = g.add_node(Box::new(node_int(20)) as Box<_>);
        // Root A second.
        let push_a = g.add_node(Box::new(node_push()) as Box<_>);
        let int = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let select = g.add_node(Box::new(node_select()) as Box<_>);
        let add = g.add_node(Box::new(node::expr("(+ $l $r)").unwrap()) as Box<_>);
        let store = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push_b, twenty, Edge::from((0, 0)));
        g.add_edge(twenty, add, Edge::from((0, 1))); // -> add.r
        g.add_edge(push_a, int, Edge::from((0, 0)));
        g.add_edge(int, select, Edge::from((0, 0)));
        g.add_edge(select, add, Edge::from((0, 0))); // arm 0 -> add.l
        g.add_edge(select, add, Edge::from((1, 0))); // arm 1 -> add.l
        g.add_edge(add, store, Edge::from((0, 0)));
        let n = g[push_a].n_outputs(node::MetaCtx::new(&no_lookup)) as u8;
        let vm = run_two_push(&g, (vec![push_a.index()], n), (vec![push_b.index()], n));
        store_val(&vm, store)
    };
    assert_eq!(build(0), Some(62));
    assert_eq!(build(1), Some(119));
}

// The same branch-join shape one level down: inside a nested graph, inlet_a
// branches and its arms reconverge at `add`, which also takes inlet_b. Here the
// inlets linearize into one component, so it already worked - this guards the
// node-style `nested_expr` codegen path against the terminal-destructure change.
//
//   INNER: [inlet_a]->[select] o0,o1 -> add.$l ;  [inlet_b] -> add.$r ;  add -> outlet
//   OUTER: [push]->[int sel]->inlet_a ;  [push]->[int 20]->inlet_b ;  inner -> store
#[test]
fn test_nested_branch_join_external_inlet() {
    let make_inner = || {
        let mut inner = Nested::default();
        let inlet_a = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
        let inlet_b = inner.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
        let select = inner.add_node(Box::new(node_select()) as Box<_>);
        let add = inner.add_node(Box::new(node::expr("(+ $l $r)").unwrap()) as Box<_>);
        let outlet = inner.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
        inner.add_edge(inlet_a, select, Edge::from((0, 0)));
        inner.add_edge(select, add, Edge::from((0, 0)));
        inner.add_edge(select, add, Edge::from((1, 0)));
        inner.add_edge(inlet_b, add, Edge::from((0, 1)));
        inner.add_edge(add, outlet, Edge::from((0, 0)));
        inner
    };
    let build = |sel: i32| {
        let mut g = petgraph::graph::DiGraph::new();
        let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
        let int = g.add_node(Box::new(node_int(sel)) as Box<_>);
        let twenty = g.add_node(Box::new(node_int(20)) as Box<_>);
        let inner_node = g.add_node(Box::new(make_inner()) as Box<_>);
        let store = g.add_node(Box::new(node_number()) as Box<_>);
        g.add_edge(push, int, Edge::from((0, 0)));
        g.add_edge(push, twenty, Edge::from((0, 0)));
        g.add_edge(int, inner_node, Edge::from((0, 0))); // -> inlet_a (input 0)
        g.add_edge(twenty, inner_node, Edge::from((0, 1))); // -> inlet_b (input 1)
        g.add_edge(inner_node, store, Edge::from((0, 0)));
        store_val(&compile_and_push(&g, push), store)
    };
    assert_eq!(build(0), Some(62));
    assert_eq!(build(1), Some(119));
}

// An `Outlet` at the *root* level (no enclosing graph node) must compile and be
// ignored: there is no parent to read its value, so it is a no-op while the rest
// of the graph still evaluates.
//
//    --------
//    | push | // push_eval
//    -+------
//     |
//    -+----
//    | 42 |
//    -+----
//     |
//    -+--------
//    | number | (stores received value in state)
//    -+--------
//     |
//    -+--------
//    | Outlet | (root-level: ignored)
//    ----------
#[test]
fn test_graph_root_outlet_connected() {
    let mut g = petgraph::graph::DiGraph::new();
    let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let int = g.add_node(Box::new(node_int(42)) as Box<_>);
    let store = g.add_node(Box::new(node_number()) as Box<_>);
    let outlet = g.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    g.add_edge(push, int, Edge::from((0, 0)));
    g.add_edge(int, store, Edge::from((0, 0)));
    g.add_edge(store, outlet, Edge::from((0, 0)));

    // Compiles, runs, and the upstream `number` still receives the value even
    // though the root outlet leads nowhere.
    assert_eq!(store_val(&compile_and_push(&g, push), store), Some(42));
}

// A *disconnected* `Outlet` at the root level (no incoming edge) is never
// reached by the flow, so it emits nothing and the rest of the graph evaluates
// normally.
#[test]
fn test_graph_root_outlet_disconnected() {
    let mut g = petgraph::graph::DiGraph::new();
    let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let int = g.add_node(Box::new(node_int(42)) as Box<_>);
    let store = g.add_node(Box::new(node_number()) as Box<_>);
    let _outlet = g.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    g.add_edge(push, int, Edge::from((0, 0)));
    g.add_edge(int, store, Edge::from((0, 0)));

    assert_eq!(store_val(&compile_and_push(&g, push), store), Some(42));
}

// === pd+ optional-input ($?) cold/hot inlet tests ===
//
// Emulates Pure Data's stateful `+`: a left "hot" inlet (always outputs the
// sum) and a right "cold" inlet (updates internal state only, no output). The
// node is a nested `Graph` whose interior is a single `Branch` reading two
// optional inputs (`$?l`, `$?r`). The cold/hot behaviour relies on the inner
// branch seeing `(None)` for the inlet that did not fire - which only works once
// the active-input-set is propagated into the nested graph's interior.

// The pd+ Branch: cold (`$?r`) sets state; hot (`$?l`) outputs `left + state`.
// Branch 0 activates the single output (hot fired), branch 1 activates nothing
// (cold-only, no output).
fn pd_plus_branch() -> node::Branch {
    node::Branch::new(
        r#"
        (begin
          (if (Some? $?r) (set! state (Some->value $?r)) '())
          (if (Some? $?l)
            (list 0 (+ (Some->value $?l) (if (number? state) state 0)))
            (list 1 '())))
        "#,
        vec![
            node::Conns::try_from([true]).unwrap(),
            node::Conns::try_from([false]).unwrap(),
        ],
    )
    .unwrap()
}

// A pd+ nested graph. Input 0 = left/hot inlet, input 1 = right/cold inlet,
// output 0 = the sum. Returns the graph and the inner branch node id (for state
// queries via the path `[pd_node, branch]`).
//
//    [In L]   [In R]
//       \       /        (In R -> $?r branch input 0, In L -> $?l branch input 1)
//      -+-------+-
//      | Branch |
//      -+--------
//       |
//    -+--------
//    | Outlet |
//    ----------
fn pd_plus() -> (Nested, usize) {
    let mut g = Nested::default();
    let inlet_l = g.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
    let inlet_r = g.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
    let branch = g.add_node(Box::new(pd_plus_branch()) as Box<_>);
    let outlet = g.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    g.add_edge(inlet_r, branch, Edge::from((0, 0))); // In R -> $?r (branch input 0)
    g.add_edge(inlet_l, branch, Edge::from((0, 1))); // In L -> $?l (branch input 1)
    g.add_edge(branch, outlet, Edge::from((0, 0)));
    (g, branch.index())
}

// Compile `g` and register fns, returning a VM ready to be pushed.
fn compile_only<N: DebugNode + ?Sized>(g: &petgraph::graph::DiGraph<Box<N>, Edge>) -> Engine {
    let eps = push_pull_entrypoints(&no_lookup, g);
    let module = gantz_core::compile::module(&no_lookup, g, &eps, &Default::default()).unwrap();
    let mut vm = Engine::new_base();
    vm.register_value(ROOT_STATE, SteelVal::empty_hashmap());
    gantz_core::graph::register(&no_lookup, g, &[], &mut vm);
    for f in &module {
        vm.run(f.to_pretty(100)).unwrap();
    }
    vm
}

// Fire the entrypoint that pushes from `push`.
fn push_from<N: DebugNode + ?Sized>(
    vm: &mut Engine,
    g: &petgraph::graph::DiGraph<Box<N>, Edge>,
    push: petgraph::graph::NodeIndex,
) {
    let ctx = node::MetaCtx::new(&no_lookup);
    let ep = entrypoint::push(vec![push.index()], g[push].n_outputs(ctx) as u8);
    vm.call_function_by_name_with_args(&entry_fn_name(&ep.id()), vec![])
        .unwrap();
}

// Root graph wiring a left push -> left value, a right push -> right value, into
// a pd+ node whose output feeds a `store`. Returns (graph, left_push,
// right_push, pd, branch_id, store).
type PdPlusRoot = (
    petgraph::graph::DiGraph<Box<dyn DebugNode>, Edge>,
    petgraph::graph::NodeIndex,
    petgraph::graph::NodeIndex,
    petgraph::graph::NodeIndex,
    usize,
    petgraph::graph::NodeIndex,
);
fn pd_plus_root(left: i32, right: i32) -> PdPlusRoot {
    let (inner, branch_ix) = pd_plus();
    let mut g = petgraph::graph::DiGraph::new();
    let left_push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let right_push = g.add_node(Box::new(node_push()) as Box<_>);
    let left_val = g.add_node(Box::new(node_int(left)) as Box<_>);
    let right_val = g.add_node(Box::new(node_int(right)) as Box<_>);
    let pd = g.add_node(Box::new(inner) as Box<_>);
    let store = g.add_node(Box::new(node_number()) as Box<_>);
    g.add_edge(left_push, left_val, Edge::from((0, 0)));
    g.add_edge(right_push, right_val, Edge::from((0, 0)));
    g.add_edge(left_val, pd, Edge::from((0, 0))); // left -> pd input 0 (hot)
    g.add_edge(right_val, pd, Edge::from((0, 1))); // right -> pd input 1 (cold)
    g.add_edge(pd, store, Edge::from((0, 0)));
    (g, left_push, right_push, pd, branch_ix, store)
}

fn branch_state(vm: &Engine, pd: petgraph::graph::NodeIndex, branch_ix: usize) -> Option<i32> {
    node::state::extract::<i32>(vm, &[pd.index(), branch_ix])
        .ok()
        .flatten()
}

// Pushing ONLY the cold (right) inlet must set state and produce no output -
// and crucially must NOT raise `+ expects a number, found '()`.
#[test]
fn test_nested_pd_plus_cold_only() {
    let (g, _left_push, right_push, pd, branch_ix, store) = pd_plus_root(10, 5);
    let mut vm = compile_only(&g);
    push_from(&mut vm, &g, right_push);
    assert_eq!(branch_state(&vm, pd, branch_ix), Some(5), "cold sets state");
    assert_eq!(store_val(&vm, store), None, "cold produces no output");
}

// Cold (right) then hot (left): state is seeded by the cold push, the hot push
// outputs `left + state`.
#[test]
fn test_nested_pd_plus_hot_after_cold() {
    let (g, left_push, right_push, pd, branch_ix, store) = pd_plus_root(10, 5);
    let mut vm = compile_only(&g);
    push_from(&mut vm, &g, right_push); // cold: state = 5
    push_from(&mut vm, &g, left_push); // hot: 10 + 5 = 15
    assert_eq!(branch_state(&vm, pd, branch_ix), Some(5));
    assert_eq!(store_val(&vm, store), Some(15));
}

// Firing both inlets in one push: the cold value updates state first, then the
// hot arm outputs `left + state`.
#[test]
fn test_nested_pd_plus_both() {
    let (inner, branch_ix) = pd_plus();
    let mut g = petgraph::graph::DiGraph::new();
    let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let left_val = g.add_node(Box::new(node_int(10)) as Box<_>);
    let right_val = g.add_node(Box::new(node_int(5)) as Box<_>);
    let pd = g.add_node(Box::new(inner) as Box<_>);
    let store = g.add_node(Box::new(node_number()) as Box<_>);
    g.add_edge(push, left_val, Edge::from((0, 0)));
    g.add_edge(push, right_val, Edge::from((0, 0)));
    g.add_edge(left_val, pd, Edge::from((0, 0)));
    g.add_edge(right_val, pd, Edge::from((0, 1)));
    g.add_edge(pd, store, Edge::from((0, 0)));

    let mut vm = compile_only(&g);
    push_from(&mut vm, &g, push);
    assert_eq!(branch_state(&vm, pd, branch_ix), Some(5));
    assert_eq!(store_val(&vm, store), Some(15)); // 10 + 5
}

// A sequence of pushes across multiple entrypoint calls: two cold updates then
// a hot output, exercising state persistence.
#[test]
fn test_nested_pd_plus_sequence() {
    let (inner, branch_ix) = pd_plus();
    let mut g = petgraph::graph::DiGraph::new();
    let cold_a = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let cold_b = g.add_node(Box::new(node_push()) as Box<_>);
    let hot = g.add_node(Box::new(node_push()) as Box<_>);
    let cold_a_val = g.add_node(Box::new(node_int(3)) as Box<_>);
    let cold_b_val = g.add_node(Box::new(node_int(7)) as Box<_>);
    let hot_val = g.add_node(Box::new(node_int(10)) as Box<_>);
    let pd = g.add_node(Box::new(inner) as Box<_>);
    let store = g.add_node(Box::new(node_number()) as Box<_>);
    g.add_edge(cold_a, cold_a_val, Edge::from((0, 0)));
    g.add_edge(cold_b, cold_b_val, Edge::from((0, 0)));
    g.add_edge(hot, hot_val, Edge::from((0, 0)));
    g.add_edge(cold_a_val, pd, Edge::from((0, 1))); // cold -> right
    g.add_edge(cold_b_val, pd, Edge::from((0, 1))); // cold -> right
    g.add_edge(hot_val, pd, Edge::from((0, 0))); // hot -> left
    g.add_edge(pd, store, Edge::from((0, 0)));

    let mut vm = compile_only(&g);
    push_from(&mut vm, &g, cold_a); // state = 3
    assert_eq!(branch_state(&vm, pd, branch_ix), Some(3));
    assert_eq!(store_val(&vm, store), None);
    push_from(&mut vm, &g, cold_b); // state = 7
    assert_eq!(branch_state(&vm, pd, branch_ix), Some(7));
    assert_eq!(store_val(&vm, store), None);
    push_from(&mut vm, &g, hot); // 10 + 7 = 17
    assert_eq!(store_val(&vm, store), Some(17));
}

// The same Branch at top level (where `$?` already works) and nested must
// behave identically.
fn pd_plus_top_level(
    left: i32,
    right: i32,
) -> (
    petgraph::graph::DiGraph<Box<dyn DebugNode>, Edge>,
    petgraph::graph::NodeIndex,
    petgraph::graph::NodeIndex,
    petgraph::graph::NodeIndex,
) {
    let mut g = petgraph::graph::DiGraph::new();
    let left_push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let right_push = g.add_node(Box::new(node_push()) as Box<_>);
    let left_val = g.add_node(Box::new(node_int(left)) as Box<_>);
    let right_val = g.add_node(Box::new(node_int(right)) as Box<_>);
    let branch = g.add_node(Box::new(pd_plus_branch()) as Box<_>);
    let store = g.add_node(Box::new(node_number()) as Box<_>);
    g.add_edge(left_push, left_val, Edge::from((0, 0)));
    g.add_edge(right_push, right_val, Edge::from((0, 0)));
    g.add_edge(right_val, branch, Edge::from((0, 0))); // right -> $?r (input 0)
    g.add_edge(left_val, branch, Edge::from((0, 1))); // left -> $?l (input 1)
    g.add_edge(branch, store, Edge::from((0, 0)));
    (g, left_push, right_push, store)
}

#[test]
fn test_pd_plus_top_level_vs_nested_equivalence() {
    // Nested: cold(5) then hot(10).
    let (gn, ln, rn, _pd, _bix, sn) = pd_plus_root(10, 5);
    let mut vmn = compile_only(&gn);
    push_from(&mut vmn, &gn, rn);
    let cold_n = store_val(&vmn, sn);
    push_from(&mut vmn, &gn, ln);
    let hot_n = store_val(&vmn, sn);

    // Top level: same sequence.
    let (gt, lt, rt, st) = pd_plus_top_level(10, 5);
    let mut vmt = compile_only(&gt);
    push_from(&mut vmt, &gt, rt);
    let cold_t = store_val(&vmt, st);
    push_from(&mut vmt, &gt, lt);
    let hot_t = store_val(&vmt, st);

    assert_eq!(cold_n, cold_t);
    assert_eq!(hot_n, hot_t);
    assert_eq!(cold_n, None);
    assert_eq!(hot_n, Some(15));
}

// The reduced inner-branch variants (cold push -> i10, hot push -> i01) must be
// DEFINED in the module, not just the all-connected i11. Guards the conf
// post-pass and call/def agreement.
#[test]
fn test_nested_pd_plus_emits_reduced_variant() {
    let (g, _l, _r, pd, branch_ix, _store) = pd_plus_root(10, 5);
    let eps = push_pull_entrypoints(&no_lookup, &g);
    let module = gantz_core::compile::module(&no_lookup, &g, &eps, &Default::default()).unwrap();
    let text: String = module
        .iter()
        .map(|f| f.to_pretty(100))
        .collect::<Vec<_>>()
        .join("\n");
    let prefix = format!("node-fn-{}:{}-", pd.index(), branch_ix);
    assert!(
        text.contains(&format!("{prefix}i10-o1")),
        "missing reduced inner branch variant {prefix}i10-o1"
    );
    assert!(
        text.contains(&format!("{prefix}i01-o1")),
        "missing reduced inner branch variant {prefix}i01-o1"
    );
}

// pd+ wrapped in a second nested `Graph`. Returns (outer, pd_id_in_outer,
// branch_id_in_pd). Outer input 0 -> pd left (hot), input 1 -> pd right (cold).
fn pd_plus_wrapped() -> (Nested, usize, usize) {
    let (pd_inner, branch_ix) = pd_plus();
    let mut outer = Nested::default();
    let inlet_l = outer.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
    let inlet_r = outer.add_node(Box::new(node::graph::Inlet::default()) as Box<_>);
    let pd = outer.add_node(Box::new(pd_inner) as Box<_>);
    let outlet = outer.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    outer.add_edge(inlet_l, pd, Edge::from((0, 0))); // outer in 0 -> pd in 0 (hot)
    outer.add_edge(inlet_r, pd, Edge::from((0, 1))); // outer in 1 -> pd in 1 (cold)
    outer.add_edge(pd, outlet, Edge::from((0, 0)));
    (outer, pd.index(), branch_ix)
}

// Two-level nesting: cold-only push from the outside must propagate the reduced
// active-set through BOTH graph layers (outer + pd) so the grandchild branch
// sees `(None)` for the hot inlet - no error, state set, no output.
#[test]
fn test_nested_pd_plus_two_levels() {
    let (outer, pd_in_outer, branch_in_pd) = pd_plus_wrapped();
    let mut g = petgraph::graph::DiGraph::new();
    let left_push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let right_push = g.add_node(Box::new(node_push()) as Box<_>);
    let left_val = g.add_node(Box::new(node_int(10)) as Box<_>);
    let right_val = g.add_node(Box::new(node_int(5)) as Box<_>);
    let outer_node = g.add_node(Box::new(outer) as Box<_>);
    let store = g.add_node(Box::new(node_number()) as Box<_>);
    g.add_edge(left_push, left_val, Edge::from((0, 0)));
    g.add_edge(right_push, right_val, Edge::from((0, 0)));
    g.add_edge(left_val, outer_node, Edge::from((0, 0))); // -> outer in 0 (hot)
    g.add_edge(right_val, outer_node, Edge::from((0, 1))); // -> outer in 1 (cold)
    g.add_edge(outer_node, store, Edge::from((0, 0)));

    let branch_path = [outer_node.index(), pd_in_outer, branch_in_pd];
    let mut vm = compile_only(&g);

    // Cold-only: state set deep inside, no output.
    push_from(&mut vm, &g, right_push);
    assert_eq!(
        node::state::extract::<i32>(&vm, &branch_path)
            .ok()
            .flatten(),
        Some(5),
    );
    assert_eq!(store_val(&vm, store), None);

    // Hot: outputs 10 + 5 = 15.
    push_from(&mut vm, &g, left_push);
    assert_eq!(store_val(&vm, store), Some(15));
}

// A wrapper `Graph` exposing ONLY pd+'s hot inlet, leaving the cold inlet
// permanently unconnected. Even when the wrapper is invoked all-active, its
// interior invokes pd+ with a statically reduced active-set (only the hot inlet
// wired), whose inner branch variant must still be defined. Guards the conf
// post-pass recursing through an all-active parent into a reduced child.
#[test]
fn test_nested_reduced_child_under_active_parent() {
    let (pd_inner, _branch_ix) = pd_plus();
    let mut outer = Nested::default();
    let inlet = outer.add_node(Box::new(node::graph::Inlet::default()) as Box<dyn DebugNode>);
    let pd = outer.add_node(Box::new(pd_inner) as Box<_>);
    let outlet = outer.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    outer.add_edge(inlet, pd, Edge::from((0, 0))); // outer inlet -> pd left (hot)
    outer.add_edge(pd, outlet, Edge::from((0, 0))); // pd right inlet left unconnected

    let mut g = petgraph::graph::DiGraph::new();
    let push = g.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let val = g.add_node(Box::new(node_int(10)) as Box<_>);
    let outer_node = g.add_node(Box::new(outer) as Box<_>);
    let store = g.add_node(Box::new(node_number()) as Box<_>);
    g.add_edge(push, val, Edge::from((0, 0)));
    g.add_edge(val, outer_node, Edge::from((0, 0)));
    g.add_edge(outer_node, store, Edge::from((0, 0)));

    let mut vm = compile_only(&g);
    push_from(&mut vm, &g, push);
    // The cold inlet is never wired => `$?r` is `(None)` => state stays 0 =>
    // output = 10 + 0. Reaching this without a free-identifier error proves the
    // reduced inner branch variant was defined and called.
    assert_eq!(store_val(&vm, store), Some(10));
}

// Push-through reaching a nested-optional child: an interior push fires only the
// hot inlet of a nested pd+, whose output propagates out through the wrapper's
// outlet. Exercises a reduced inner-branch variant reached via push-through (not
// via the wrapper's own inlets).
#[test]
fn test_push_through_into_nested_optional_hot() {
    let (c_inner, _branch_ix) = pd_plus();
    let mut l = Nested::default();
    let p = l.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let val = l.add_node(Box::new(node_int(10)) as Box<_>);
    let c = l.add_node(Box::new(c_inner) as Box<_>);
    let outlet = l.add_node(Box::new(node::graph::Outlet::default()) as Box<_>);
    l.add_edge(p, val, Edge::from((0, 0)));
    l.add_edge(val, c, Edge::from((0, 0))); // -> C hot (input 0); C cold (input 1) unwired
    l.add_edge(c, outlet, Edge::from((0, 0)));

    let ctx = node::MetaCtx::new(&no_lookup);
    let push_n = l[p].n_outputs(ctx) as u8;

    let mut g = petgraph::graph::DiGraph::new();
    let l_node = g.add_node(Box::new(l) as Box<dyn DebugNode>);
    let store = g.add_node(Box::new(node_number()) as Box<_>);
    g.add_edge(l_node, store, Edge::from((0, 0)));

    let vm = compile_and_push_nested(&g, vec![l_node.index(), p.index()], push_n);
    assert_eq!(store_val(&vm, store), Some(10)); // 10 + state(0)
}

// Push-through into a side-effect-only nested-optional child: an interior push
// fires only the cold inlet of a nested pd+ that produces no output and feeds no
// outlet. The all-connected interior flow never reaches C, so C's reduced branch
// variant is discoverable only from the interior push's flow - which the
// all-connected nested_fg + node-style reduction miss.
#[test]
fn test_push_through_into_nested_optional_sideeffect() {
    let (c_inner, branch_ix) = pd_plus();
    let mut l = Nested::default();
    let p = l.add_node(Box::new(node_push()) as Box<dyn DebugNode>);
    let val = l.add_node(Box::new(node_int(5)) as Box<_>);
    let c = l.add_node(Box::new(c_inner) as Box<_>);
    l.add_edge(p, val, Edge::from((0, 0)));
    l.add_edge(val, c, Edge::from((0, 1))); // -> C cold inlet (input 1); no outlet

    let ctx = node::MetaCtx::new(&no_lookup);
    let push_n = l[p].n_outputs(ctx) as u8;
    let c_in_l = c.index();

    let mut g = petgraph::graph::DiGraph::new();
    let l_node = g.add_node(Box::new(l) as Box<dyn DebugNode>);

    let vm = compile_and_push_nested(&g, vec![l_node.index(), p.index()], push_n);
    assert_eq!(
        node::state::extract::<i32>(&vm, &[l_node.index(), c_in_l, branch_ix])
            .ok()
            .flatten(),
        Some(5),
    );
}