wingfoil 4.0.0

graph based stream processing framework
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
use crate::queue::HashByRef;
use crate::queue::TimeQueue;
use crate::types::{NanoTime, Node};

use crossbeam::channel::{Receiver, SendError, Sender, select};
use lazy_static::lazy_static;
use std::cmp::{max, min};
use std::collections::HashMap;
#[cfg(feature = "dynamic-graph-beta")]
use std::collections::HashSet;
use std::convert::TryInto;
use std::fs::File;
use std::io::{Error, Write};
use std::path::Path;
use std::rc::Rc;
#[cfg(feature = "async")]
use std::sync::Arc;
use std::sync::Mutex;
#[cfg(feature = "async")]
use std::sync::OnceLock;
use std::time::{Duration, Instant};
use std::vec;

lazy_static! {
    static ref GRAPH_ID: Mutex<usize> = Mutex::new(0);
}

struct NodeData {
    node: Rc<dyn Node>,
    upstreams: Vec<(usize, bool)>,
    downstreams: Vec<(usize, bool)>,
    layer: usize,
    active: bool,
}

/// Whether the [Graph] should run RealTime or Historical mode.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum RunMode {
    RealTime,
    HistoricalFrom(NanoTime),
}

impl RunMode {
    pub fn start_time(&self) -> NanoTime {
        match self {
            RunMode::RealTime => NanoTime::now(),
            RunMode::HistoricalFrom(start_time) => *start_time,
        }
    }
}

/// Defines how long the graph should run for.  Can be a
/// Duration, number of cycles or forever.
#[derive(Clone, Copy, Debug)]
pub enum RunFor {
    Duration(Duration),
    Cycles(u32),
    Forever,
}

impl RunFor {
    pub fn done(&self, cycle: u32, elapsed: NanoTime) -> bool {
        match self {
            RunFor::Cycles(cycles) => cycle > *cycles,
            RunFor::Duration(duration) => elapsed > NanoTime::from(*duration),
            RunFor::Forever => false,
        }
    }
}

fn average_duration(duration: Duration, n: u32) -> Duration {
    let avg_nanos = if n == 0 {
        0
    } else {
        duration.as_nanos() / n as u128
    };
    Duration::from_nanos(avg_nanos as u64)
}

/// A struct produced by [Graph] that can be used by a [Node]
/// to notify the [Graph] that it is required to be cycled
/// on the next engine cycle.   It is bound to the [Node]
/// that created it.
#[derive(Clone, Debug)]
pub(crate) struct ReadyNotifier {
    pub node_index: usize,
    pub sender: Sender<usize>,
}

impl ReadyNotifier {
    pub fn notify(&self) -> anyhow::Result<(), SendError<usize>> {
        self.sender.send(self.node_index)
    }
}

/// Maintains the parts of the graph state that is accessible to Nodes.
pub struct GraphState {
    time: NanoTime,
    /// True until the first engine cycle completes; suppresses the
    /// strict-advance check so the very first cycle can fire at NanoTime::ZERO.
    first_cycle: bool,
    is_last_cycle: bool,
    stop_requested: bool,
    current_node_index: Option<usize>,
    scheduled_callbacks: TimeQueue<usize>,
    always_callbacks: Vec<usize>,
    node_to_index: HashMap<HashByRef<dyn Node>, usize>,
    node_ticked: Vec<bool>,
    #[cfg(feature = "async")]
    run_time: OnceLock<Arc<tokio::runtime::Runtime>>,
    run_mode: RunMode,
    run_for: RunFor,
    ready_notifier: Sender<usize>,
    ready_callbacks: Receiver<usize>,
    start_time: NanoTime,
    id: usize,
    nodes: Vec<NodeData>,
    dirty_nodes_by_layer: Vec<Vec<usize>>,
    node_dirty: Vec<bool>,
    #[cfg(feature = "dynamic-graph-beta")]
    pending_additions: Vec<(Rc<dyn Node>, usize, bool, bool)>,
    #[cfg(feature = "dynamic-graph-beta")]
    pending_removals: Vec<Rc<dyn Node>>,
}

impl GraphState {
    pub fn new(run_mode: RunMode, run_for: RunFor, start_time: NanoTime) -> Self {
        let (ready_notifier, ready_callbacks) = crossbeam::channel::unbounded();
        let mut id = GRAPH_ID.lock().unwrap();
        let slf = Self {
            time: NanoTime::ZERO,
            first_cycle: true,
            is_last_cycle: false,
            stop_requested: false,
            current_node_index: None,
            scheduled_callbacks: TimeQueue::new(),
            always_callbacks: Vec::new(),
            node_to_index: HashMap::new(),
            node_ticked: Vec::new(),
            #[cfg(feature = "async")]
            run_time: OnceLock::new(),
            ready_notifier,
            run_mode,
            run_for,
            ready_callbacks,
            start_time,
            id: *id,
            nodes: Vec::new(),
            dirty_nodes_by_layer: Vec::new(),
            node_dirty: Vec::new(),
            #[cfg(feature = "dynamic-graph-beta")]
            pending_additions: Vec::new(),
            #[cfg(feature = "dynamic-graph-beta")]
            pending_removals: Vec::new(),
        };
        *id += 1;
        slf
    }

    /// The current engine time
    pub fn time(&self) -> NanoTime {
        self.time
    }

    /// The current engine time
    pub fn elapsed(&self) -> NanoTime {
        self.time - self.start_time
    }

    pub fn start_time(&self) -> NanoTime {
        self.start_time
    }

    pub(crate) fn ready_notifier(&self) -> ReadyNotifier {
        ReadyNotifier {
            node_index: self.current_node_index.unwrap(),
            sender: self.ready_notifier.clone(),
        }
    }

    #[cfg(feature = "async")]
    pub fn tokio_runtime(&self) -> Arc<tokio::runtime::Runtime> {
        self.run_time
            .get_or_init(|| {
                if tokio::runtime::Handle::try_current().is_ok() {
                    panic!(
                        "wingfoil cannot be run from an async context (e.g. `#[tokio::main]`). \
                     Call graph.run() from a synchronous thread instead. \
                     Tip: std::thread::spawn(|| graph.run(...)).join().unwrap()"
                    );
                }
                Arc::new(
                    tokio::runtime::Builder::new_multi_thread()
                        .enable_all()
                        .build()
                        .unwrap(),
                )
            })
            .clone()
    }

    pub fn add_callback(&mut self, time: NanoTime) {
        self.add_callback_for_node(self.current_node_index.unwrap(), time);
    }

    pub(crate) fn current_node_id(&self) -> usize {
        self.current_node_index.unwrap()
    }

    pub fn always_callback(&mut self) {
        let ix = self.current_node_index.unwrap();
        self.always_callbacks.push(ix);
    }

    pub fn is_last_cycle(&self) -> bool {
        self.is_last_cycle
    }

    /// Request early termination of the graph execution.
    /// This is useful for nodes like `limit` that need to stop the graph
    /// when they've finished producing values, even with RunFor::Forever.
    pub fn request_stop(&mut self) {
        self.stop_requested = true;
    }

    /// Returns true if node has ticked on the current engine cycle.
    /// Returns false (rather than panicking) for nodes not yet registered in the graph,
    /// which can happen when a node was just queued via `add_upstream` but not yet wired.
    pub fn ticked(&self, node: Rc<dyn Node>) -> bool {
        self.node_index(node)
            .map(|i| self.node_ticked[i])
            .unwrap_or(false)
    }

    /// Wire `upstream` (and its upstream subgraph) into the graph and register
    /// it as an upstream of the calling node. `is_active` controls whether it
    /// triggers the calling node on each tick (true) or is read-only (false).
    /// Processed at the end of the current cycle.
    ///
    /// If `recycle` is true, `add_callback(state.time() + 1ns)` is called on
    /// the new upstream after it is wired, scheduling it to fire at `t+1ns`.
    /// This lets the calling node catch the value that triggered the
    /// `add_upstream` call without waiting for the next source tick.
    #[cfg(feature = "dynamic-graph-beta")]
    pub fn add_upstream(&mut self, upstream: Rc<dyn Node>, is_active: bool, recycle: bool) {
        let caller_index = self.current_node_index.unwrap();
        self.pending_additions
            .push((upstream, caller_index, is_active, recycle));
    }

    /// Deregister `node` at the end of the current cycle:
    /// unlinks it from all upstream downstream-lists and all downstream upstream-lists,
    /// then calls stop() + teardown().
    ///
    /// **Note on memory**: removed nodes are marked inactive but their entries in the
    /// internal index (`node_to_index`, `node_ticked`, `node_dirty`, `nodes`) are never
    /// freed. In long-running graphs that add and remove many nodes over time, these
    /// dead entries accumulate. This is a known limitation of the current implementation.
    #[cfg(feature = "dynamic-graph-beta")]
    pub fn remove_node(&mut self, node: Rc<dyn Node>) {
        self.pending_removals.push(node);
    }

    #[allow(dead_code)]
    /// Returns true if node has ticked on the current engine cycle
    pub(crate) fn node_index_ticked(&self, node_index: usize) -> bool {
        self.node_ticked[node_index]
    }

    fn has_scheduled_callbacks(&self) -> bool {
        !self.scheduled_callbacks.is_empty()
    }

    fn next_scheduled_time(&self) -> NanoTime {
        if self.scheduled_callbacks.is_empty() {
            NanoTime::MAX
        } else {
            self.scheduled_callbacks.next_time()
        }
    }

    pub(crate) fn add_callback_for_node(&mut self, node_index: usize, time: NanoTime) {
        self.scheduled_callbacks.push(node_index, time);
    }

    fn has_pending_scheduled_callbacks(&self) -> bool {
        self.scheduled_callbacks.pending(self.time)
    }

    fn wait_ready_callback(&mut self, end_time: NanoTime) -> Option<usize> {
        let now = NanoTime::now();
        if now > end_time {
            // might step in here if timeout on recv isnt 100%
            // accurate
            None
        } else {
            let timeout = u64::from(end_time - now);
            select! {
                recv(self.ready_callbacks) -> msg => {
                    Some(msg.unwrap())
                },
                default(Duration::from_nanos(timeout)) => {
                    None
                }
            }
        }
    }

    pub fn node_index(&self, node: Rc<dyn Node>) -> Option<usize> {
        let key = HashByRef::new(node.clone());
        self.node_to_index.get(&key).copied()
    }

    fn reset(&mut self) {
        for i in self.node_ticked.iter_mut() {
            *i = false;
        }
    }

    fn push_node(&mut self, node: Rc<dyn Node>) {
        let index = self.node_ticked.len();
        self.node_ticked.push(false);
        //self.nodes.push(node.clone());
        self.node_to_index
            .insert(HashByRef::new(node.clone()), index);
    }

    fn seen(&self, node: Rc<dyn Node>) -> bool {
        self.node_to_index.contains_key(&HashByRef::new(node))
    }

    fn set_ticked(&mut self, index: usize) {
        self.node_ticked[index] = true;
    }

    pub fn run_mode(&self) -> RunMode {
        self.run_mode
    }

    pub fn run_for(&self) -> RunFor {
        self.run_for
    }

    pub fn log(&self, level: log::Level, msg: &str) {
        let Some(ix) = self.current_node_index else {
            return;
        };
        let id = self.id;
        #[cfg(not(feature = "tracing"))]
        if log_enabled!(level) {
            let type_name = self.nodes[ix].node.type_name();
            log!(target: &type_name, level, "[{id},{ix}]{msg}");
        }
        #[cfg(feature = "tracing")]
        if tracing_log_enabled!(level) {
            let type_name = self.nodes[ix].node.type_name();
            tracing_log!(level, node = %type_name, "[{id},{ix}]{msg}");
        }
    }

    pub(crate) fn mark_dirty(&mut self, index: usize) {
        if !self.node_dirty[index] {
            let layer = self.nodes[index].layer;
            self.dirty_nodes_by_layer[layer].push(index);
            self.node_dirty[index] = true;
        }
    }
}

/// Engine for co-ordinating execution of [Node]s
pub struct Graph {
    pub(crate) state: GraphState,
}

impl Graph {
    pub fn new(root_nodes: Vec<Rc<dyn Node>>, run_mode: RunMode, run_for: RunFor) -> Graph {
        let start_time = run_mode.start_time();
        let state = GraphState::new(run_mode, run_for, start_time);
        let mut graph = Graph { state };
        graph.initialise(root_nodes);
        graph
    }

    #[cfg(feature = "async")]
    pub fn new_with(
        root_nodes: Vec<Rc<dyn Node>>,
        tokio_runtime: Arc<tokio::runtime::Runtime>,
        run_mode: RunMode,
        run_for: RunFor,
        start_time: NanoTime,
    ) -> Graph {
        let state = GraphState::new(run_mode, run_for, start_time);
        state.run_time.set(tokio_runtime).ok();
        let mut graph = Graph { state };
        graph.initialise(root_nodes);
        graph
    }

    pub(crate) fn setup_nodes(&mut self) -> anyhow::Result<()> {
        self.apply_nodes("setup", |node, state| node.setup(state))
    }

    pub(crate) fn start_nodes(&mut self) -> anyhow::Result<()> {
        self.apply_nodes("start", |node, state| node.start(state))
    }

    pub(crate) fn stop_nodes(&mut self) -> anyhow::Result<()> {
        self.apply_nodes("stop", |node, state| node.stop(state))
    }

    pub(crate) fn teardown_nodes(&mut self) -> anyhow::Result<()> {
        self.apply_nodes("teardown", |node, state| node.teardown(state))
    }

    #[cfg_attr(
        feature = "instrument-apply-nodes",
        tracing::instrument(skip(self, func))
    )]
    fn apply_nodes(
        &mut self,
        desc: &str,
        func: impl Fn(Rc<dyn Node>, &mut GraphState) -> anyhow::Result<()>,
    ) -> anyhow::Result<()> {
        let timer = Instant::now();
        for ix in 0..self.state.nodes.len() {
            if !self.state.nodes[ix].active {
                continue;
            }
            let node = self.state.nodes[ix].node.clone();
            self.state.current_node_index = Some(ix);
            func(node, &mut self.state).map_err(|e| {
                let context = self.format_context(ix, 3);
                e.context(format!("Error during {desc} in node [{ix}]:\n{context}"))
            })?;
            self.state.current_node_index = None;
        }
        debug!(
            "graph {:?}, {:?} took {:?} for {:?} nodes",
            self.state.id,
            desc,
            timer.elapsed(),
            self.state.nodes.len()
        );
        //println!("*** {:}graph {:} {:} done", "   ".repeat(self.state.id), self.state.id, desc);
        Ok(())
    }

    fn resolve_start_end(
        &self,
        start_time: &mut NanoTime,
        end_time: &mut NanoTime,
        end_cycle: &mut u32,
        is_realtime: &mut bool,
    ) {
        *end_time = NanoTime::MAX; // can update after first tick
        *end_cycle = u32::MAX; // can update after first tick
        match self.state.run_mode() {
            RunMode::RealTime => {
                *is_realtime = true;
                *start_time = NanoTime::now();
            }
            RunMode::HistoricalFrom(t) => {
                *is_realtime = false;
                *start_time = t;
            }
        };
        match self.state.run_for {
            RunFor::Duration(duration) => {
                *end_time = *start_time + duration.as_nanos() as u64;
                debug!("end_time = {end_time}",);
            }
            RunFor::Cycles(cycle) => {
                *end_cycle = cycle;
                debug!("end_cycle = {end_cycle}",);
            }
            RunFor::Forever => {}
        }
    }

    pub(crate) fn run_nodes(&mut self) -> anyhow::Result<()> {
        //println!("*** {:}graph {:} run_nodes", "   ".repeat(self.state.id), self.state.id);
        let run_timer = Instant::now();
        let mut cycles: u32 = 0;
        let mut empty_cycles: u32 = 0;
        let mut end_time = NanoTime::MAX;
        let mut end_cycle = u32::MAX;
        let mut is_realtime = false;
        let mut start_time = NanoTime::ZERO;
        self.resolve_start_end(
            &mut start_time,
            &mut end_time,
            &mut end_cycle,
            &mut is_realtime,
        );
        self.state.start_time = start_time;
        loop {
            if self.state.is_last_cycle && (self.state.time >= end_time || cycles >= end_cycle) {
                debug!(
                    "Finished. {:}, {:}, {:}, {:}",
                    self.state.time >= end_time,
                    cycles >= end_cycle,
                    self.state.time,
                    end_time
                );
                break;
            }
            if !self.state.is_last_cycle && (cycles >= end_cycle - 1 || self.state.time >= end_time)
            {
                debug!("last cycle");
                self.state.is_last_cycle = true;
            }
            if is_realtime {
                let progressed = self.process_callbacks_realtime(end_time);
                if !progressed {
                    empty_cycles += 1;
                    continue;
                }
            } else {
                let progressed = self.process_callbacks_historical();
                if !progressed {
                    debug!("Terminating early.");
                    break;
                }
            }
            self.cycle()?;
            /*
            if cycles == 0 && !is_realtime {
                // first cycle
                if let RunFor::Duration(duration) = run_for {
                    end_time = self.state.time + duration.as_nanos();
                    debug!("end_time = {:}", end_time);
                }
            }
             */
            cycles += 1;
            debug!("cycles={cycles}");
            if self.state.stop_requested {
                debug!("Stop requested by node, terminating early.");
                break;
            }
        }
        let elapsed = run_timer.elapsed();
        debug!("{empty_cycles} empty cycles");
        debug!(
            "Completed {:} cycles  in {:?}. {:?} average.",
            cycles,
            run_timer.elapsed(),
            average_duration(elapsed, cycles)
        );
        //println!("*** {:}graph {:} run_nodes done", "   ".repeat(self.state.id), self.state.id);
        Ok(())
    }

    #[cfg_attr(feature = "instrument-run", tracing::instrument(skip_all))]
    pub fn run(&mut self) -> anyhow::Result<()> {
        self.setup_nodes()?;
        self.start_nodes()?;
        self.run_nodes()?;
        self.stop_nodes()?;
        self.teardown_nodes()?;
        Ok(())
    }

    #[cfg_attr(feature = "instrument-initialise", tracing::instrument(skip_all))]
    fn initialise(&mut self, root_nodes: Vec<Rc<dyn Node>>) -> &mut Graph {
        let timer = Instant::now();
        for node in root_nodes {
            if !self.state.seen(node.clone()) {
                self.initialise_node(&node);
            }
        }
        let mut max_layer: i32 = -1;
        for i in 0..self.state.nodes.len() {
            max_layer = max(max_layer, self.state.nodes[i].layer.try_into().unwrap());
            self.state.node_dirty.push(false);
            for j in 0..self.state.nodes[i].upstreams.len() {
                let (up_index, active) = self.state.nodes[i].upstreams[j];
                self.state.nodes[up_index].downstreams.push((i, active));
            }
        }
        for _ in 0..max_layer + 1 {
            self.state.dirty_nodes_by_layer.push(vec![]);
        }
        debug!(
            "{:} nodes wired in {:?}",
            self.state.nodes.len(),
            timer.elapsed()
        );
        self
    }

    fn initialise_upstreams(
        &mut self,
        upstreams: &[Rc<dyn Node>],
        is_active: bool,
        layer: &mut usize,
        upstream_indexes: &mut Vec<(usize, bool)>,
    ) {
        for upstream_node in upstreams {
            let upstream_index = self.initialise_node(upstream_node);
            upstream_indexes.push((upstream_index, is_active));
            *layer = max(*layer, self.state.nodes[upstream_index].layer + 1);
        }
    }

    fn initialise_node(&mut self, node: &Rc<dyn Node>) -> usize {
        // recursively crawl through graph defined by node
        // constructing NodeData wrapper for each node and pushing
        // onto self.nodes returns index of new NodeData in self.nodes
        if self.state.seen(node.clone()) {
            self.state.node_index(node.clone()).unwrap()
        } else {
            let mut layer = 0;
            let mut upstream_indexes = vec![];
            let upstreams = node.upstreams();
            self.initialise_upstreams(&upstreams.active, true, &mut layer, &mut upstream_indexes);
            self.initialise_upstreams(&upstreams.passive, false, &mut layer, &mut upstream_indexes);
            let node_data = NodeData {
                node: node.clone(),
                upstreams: upstream_indexes,
                downstreams: vec![],
                layer,
                active: true,
            };
            let index = self.state.nodes.len();
            self.state.push_node(node.clone());
            self.state.nodes.push(node_data);
            index
        }
    }

    fn mark_dirty(&mut self, index: usize) {
        if !self.state.node_dirty[index] {
            let layer = self.state.nodes[index].layer;
            self.state.dirty_nodes_by_layer[layer].push(index);
            self.state.node_dirty[index] = true;
        }
    }

    fn process_scheduled_callbacks(&mut self) -> bool {
        let mut progressed = false;
        for i in 0..self.state.always_callbacks.len() {
            let ix = self.state.always_callbacks[i];
            self.mark_dirty(ix);
            progressed = true;
        }
        while self.state.has_pending_scheduled_callbacks() {
            let ix = self.state.scheduled_callbacks.pop();
            self.mark_dirty(ix);
            progressed = true;
        }
        progressed
    }

    fn process_callbacks_historical(&mut self) -> bool {
        if !self.state.ready_callbacks.is_empty() {
            panic!("ready_callbacks are not supported in realtime mode.");
        }
        if self.state.has_scheduled_callbacks() {
            let next = self.state.next_scheduled_time();
            self.state.time = if self.state.first_cycle {
                // First cycle: use the scheduled time as-is (may be NanoTime::ZERO).
                self.state.first_cycle = false;
                next
            } else {
                // Enforce strict monotonic progression: bump to prev+1 if needed.
                next.max(self.state.time + 1)
            };
        }
        self.process_scheduled_callbacks()
    }

    fn process_ready_callbacks(&mut self) -> bool {
        let mut progressed = false;
        while !self.state.ready_callbacks.is_empty() {
            let ix = self.state.ready_callbacks.recv().unwrap();
            self.mark_dirty(ix);
            progressed = true;
        }
        progressed
    }

    fn process_callbacks_realtime(&mut self, end_time: NanoTime) -> bool {
        let mut progressed = self.process_ready_callbacks();
        if self.process_scheduled_callbacks() {
            progressed = true;
        }
        if !progressed {
            let wait_until = min(end_time, self.state.next_scheduled_time());
            if let Some(ix) = self.state.wait_ready_callback(wait_until) {
                self.mark_dirty(ix);
                progressed = true;
            }
        }
        self.state.time = NanoTime::now().max(self.state.time + 1);
        progressed
    }

    #[cfg_attr(feature = "instrument-cycle", tracing::instrument(skip_all))]
    fn cycle(&mut self) -> anyhow::Result<()> {
        for lyr in 0..self.state.dirty_nodes_by_layer.len() {
            for i in 0..self.state.dirty_nodes_by_layer[lyr].len() {
                let ix = self.state.dirty_nodes_by_layer[lyr][i];
                self.cycle_node(ix)?;
            }
        }
        self.reset();
        #[cfg(feature = "dynamic-graph-beta")]
        self.process_pending_removals()?;
        #[cfg(feature = "dynamic-graph-beta")]
        self.process_pending_additions()?;
        Ok(())
    }

    #[cfg_attr(
        feature = "instrument-cycle-node",
        tracing::instrument(skip(self), fields(node = tracing::field::Empty))
    )]
    fn cycle_node(&mut self, index: usize) -> anyhow::Result<()> {
        if !self.state.nodes[index].active {
            return Ok(());
        }
        #[cfg(feature = "instrument-cycle-node")]
        tracing::Span::current().record("node", self.state.nodes[index].node.type_name());
        let node = &self.state.nodes[index].node;
        self.state.current_node_index = Some(index);
        let result = node.clone().cycle(&mut self.state);
        self.state.current_node_index = None;

        let ticked = result.map_err(|e| {
            let context = self.format_context(index, 3);
            e.context(format!("Error in node [{index}]:\n{context}"))
        })?;

        if ticked {
            self.state.set_ticked(index);
            for i in 0..self.state.nodes[index].downstreams.len() {
                let (downstream_index, active) = self.state.nodes[index].downstreams[i];
                if active {
                    self.mark_dirty(downstream_index)
                }
            }
        }
        Ok(())
    }

    fn reset(&mut self) {
        self.state.reset();
        for layer in self.state.dirty_nodes_by_layer.iter_mut() {
            layer.clear();
        }
        // compiler actually optimises this into memset :)
        // https://users.rust-lang.org/t/fastest-way-to-zero-an-array/39222
        for i in self.state.node_dirty.iter_mut() {
            *i = false;
        }
    }

    #[cfg(feature = "dynamic-graph-beta")]
    fn process_pending_removals(&mut self) -> anyhow::Result<()> {
        let removals = std::mem::take(&mut self.state.pending_removals);
        for node in removals {
            let Some(index) = self.state.node_index(node.clone()) else {
                continue;
            };
            if !self.state.nodes[index].active {
                continue;
            }
            // Unlink from upstreams' downstreams
            let upstreams: Vec<(usize, bool)> = self.state.nodes[index].upstreams.clone();
            for (up_idx, _) in &upstreams {
                self.state.nodes[*up_idx]
                    .downstreams
                    .retain(|(di, _)| *di != index);
            }
            // Unlink from downstreams' upstreams
            let downstreams: Vec<(usize, bool)> = self.state.nodes[index].downstreams.clone();
            for (dn_idx, _) in &downstreams {
                self.state.nodes[*dn_idx]
                    .upstreams
                    .retain(|(ui, _)| *ui != index);
            }
            // stop + teardown
            self.state.current_node_index = Some(index);
            node.clone().stop(&mut self.state).map_err(|e| {
                e.context(format!(
                    "Error during stop in node [{index}] (pending removal)"
                ))
            })?;
            node.clone().teardown(&mut self.state).map_err(|e| {
                e.context(format!(
                    "Error during teardown in node [{index}] (pending removal)"
                ))
            })?;
            self.state.current_node_index = None;
            self.state.nodes[index].active = false;
        }
        Ok(())
    }

    #[cfg(feature = "dynamic-graph-beta")]
    fn process_pending_additions(&mut self) -> anyhow::Result<()> {
        let additions = std::mem::take(&mut self.state.pending_additions);
        if additions.is_empty() {
            return Ok(());
        }
        let start_index = self.state.nodes.len();

        // Recurse through each new subgraph; seen() prevents re-registering existing nodes.
        for (node, _caller_index, _is_active, _recycle) in &additions {
            if !self.state.seen(node.clone()) {
                self.initialise_node(node);
            }
        }

        // Indices of truly new nodes
        let new_indices: Vec<usize> = (start_index..self.state.nodes.len()).collect();

        // Push node_dirty entries for new nodes (node_ticked already pushed by push_node)
        for _ in &new_indices {
            self.state.node_dirty.push(false);
        }

        // Wire declared upstreams' downstreams for new nodes
        for &ix in &new_indices {
            let upstreams = self.state.nodes[ix].upstreams.clone();
            for (up_idx, active) in upstreams {
                self.state.nodes[up_idx].downstreams.push((ix, active));
            }
        }

        // Wire the dynamic caller→node edges and fix layers.
        // Track wired (caller, node) pairs to skip duplicates if add_upstream
        // is called more than once with the same node in a single cycle.
        let mut wired_edges: HashSet<(usize, usize)> = HashSet::new();
        for (node, caller_index, is_active, recycle) in &additions {
            let node_index = self.state.node_index(node.clone()).unwrap();
            if wired_edges.insert((*caller_index, node_index)) {
                self.state.nodes[*caller_index]
                    .upstreams
                    .push((node_index, *is_active));
                self.state.nodes[node_index]
                    .downstreams
                    .push((*caller_index, *is_active));
            }
            self.fix_layers(*caller_index);
            if *recycle {
                let time = self.state.time + NanoTime::new(1);
                let new_set: HashSet<usize> = new_indices.iter().cloned().collect();
                // Walk upward from the leaf through the newly-added subgraph to find
                // attachment points: new nodes that connect directly to the pre-existing
                // graph (have at least one pre-existing upstream) or are new sources
                // (have no upstreams at all).  Scheduling recycle callbacks there runs
                // the pipeline in correct dependency order instead of firing the leaf
                // with stale default values from intermediate nodes.
                let mut stack = vec![node_index];
                let mut visited: HashSet<usize> = HashSet::new();
                while let Some(ix) = stack.pop() {
                    if !visited.insert(ix) {
                        continue;
                    }
                    let upstreams: Vec<(usize, bool)> = self.state.nodes[ix].upstreams.clone();
                    let has_preexisting = upstreams.iter().any(|(up, _)| !new_set.contains(up));
                    let is_source = upstreams.is_empty();
                    if has_preexisting || is_source {
                        self.state.add_callback_for_node(ix, time);
                    }
                    for &(up_idx, _) in &upstreams {
                        if new_set.contains(&up_idx) {
                            stack.push(up_idx);
                        }
                    }
                }
            }
        }

        // Batch setup then batch start for new nodes only
        for &ix in &new_indices {
            let node = self.state.nodes[ix].node.clone();
            self.state.current_node_index = Some(ix);
            node.setup(&mut self.state).map_err(|e| {
                e.context(format!(
                    "Error during setup in node [{ix}] (dynamic addition)"
                ))
            })?;
            self.state.current_node_index = None;
        }
        for &ix in &new_indices {
            let node = self.state.nodes[ix].node.clone();
            self.state.current_node_index = Some(ix);
            node.start(&mut self.state).map_err(|e| {
                e.context(format!(
                    "Error during start in node [{ix}] (dynamic addition)"
                ))
            })?;
            self.state.current_node_index = None;
        }
        Ok(())
    }

    /// Recalculate `node_index`'s layer based on its current upstreams,
    /// propagate any increase to its downstreams, and extend
    /// `dirty_nodes_by_layer` to accommodate the new layer.
    ///
    /// Iterative BFS to avoid stack overflows on deep graphs.
    #[cfg(feature = "dynamic-graph-beta")]
    fn fix_layers(&mut self, start: usize) {
        let mut queue = std::collections::VecDeque::new();
        queue.push_back(start);
        while let Some(node_index) = queue.pop_front() {
            let required = self.state.nodes[node_index]
                .upstreams
                .iter()
                .map(|(up_idx, _)| self.state.nodes[*up_idx].layer)
                .max()
                .map_or(0, |m| m + 1);
            if required > self.state.nodes[node_index].layer {
                self.state.nodes[node_index].layer = required;
                let downstreams: Vec<usize> = self.state.nodes[node_index]
                    .downstreams
                    .iter()
                    .map(|(di, _)| *di)
                    .collect();
                for dn_idx in downstreams {
                    queue.push_back(dn_idx);
                }
            }
            let layer = self.state.nodes[node_index].layer;
            while self.state.dirty_nodes_by_layer.len() <= layer {
                self.state.dirty_nodes_by_layer.push(vec![]);
            }
        }
    }

    /// Format nodes around the given index for error context.
    /// Shows `range` nodes before and after, marking the target node.
    fn format_context(&self, target_index: usize, range: usize) -> String {
        let mut output = String::new();
        let start = target_index.saturating_sub(range);
        let end = (target_index + range + 1).min(self.state.nodes.len());

        for i in start..end {
            let node_data = &self.state.nodes[i];
            let marker = if i == target_index { ">>> " } else { "    " };
            output.push_str(&format!("{marker}[{i:02}] "));
            for _ in 0..node_data.layer {
                output.push_str("   ");
            }
            output.push_str(&format!("{}\n", node_data.node));
        }
        output
    }

    pub fn print(&mut self) -> &mut Graph {
        for (i, node_data) in self.state.nodes.iter().enumerate() {
            print!("[{i:02}] ");
            for _ in 0..node_data.layer {
                print!("   ");
            }
            println!("{:}", node_data.node);
        }
        self
    }

    pub fn export(&self, path: &str) -> Result<(), Error> {
        let path = Path::new(&path);
        let mut output = File::create(path)?;
        writeln!(output, "graph [")?;
        for i in 0..self.state.nodes.len() {
            writeln!(output, "    node [")?;
            writeln!(output, "        id {i}")?;
            writeln!(
                output,
                "        label \"[{i}] {}\"",
                self.state.nodes[i].node
            )?;
            writeln!(output, "        graphics")?;
            writeln!(output, "        [")?;
            writeln!(output, "            w 200.0")?;
            writeln!(output, "            h 30.0")?;
            writeln!(output, "        ]")?;
            writeln!(output, "    ]")?;
        }
        for (i, node) in self.state.nodes.iter().enumerate() {
            for downstream in node.downstreams.iter() {
                let downstream_index = downstream.0;
                writeln!(output, "    edge [")?;
                writeln!(output, "        source {i}")?;
                writeln!(output, "        target {downstream_index}")?;
                writeln!(output, "    ]")?;
            }
        }
        writeln!(output, "]")
    }
}

#[cfg(test)]
mod tests {

    use crate::graph::*;
    use crate::nodes::*;
    use crate::queue::ValueAt;
    use crate::types::*;
    use std::cell::RefCell;

    use itertools::Itertools;

    // ── RunFor::done ─────────────────────────────────────────────────────────

    #[test]
    fn run_for_cycles_done_when_exceeded() {
        let rf = RunFor::Cycles(3);
        assert!(!rf.done(3, NanoTime::ZERO)); // cycle == limit → not done
        assert!(rf.done(4, NanoTime::ZERO)); // cycle > limit → done
    }

    #[test]
    fn run_for_duration_done_when_elapsed_exceeds() {
        use std::time::Duration;
        let rf = RunFor::Duration(Duration::from_nanos(100));
        assert!(!rf.done(0, NanoTime::new(100))); // equal → not done
        assert!(rf.done(0, NanoTime::new(101))); // exceeded → done
    }

    #[test]
    fn run_for_forever_never_done() {
        let rf = RunFor::Forever;
        assert!(!rf.done(u32::MAX, NanoTime::MAX));
    }

    // ── average_duration ─────────────────────────────────────────────────────

    #[test]
    fn average_duration_normal() {
        // function is private — exercise it indirectly via the graph cycle timing
        // by running a trivial graph and checking it doesn't panic.
        let src = Rc::new(RefCell::new(CallBackStream::<u64>::new()));
        src.borrow_mut().push(ValueAt::new(1u64, NanoTime::new(10)));
        src.clone()
            .as_stream()
            .run(RunMode::HistoricalFrom(NanoTime::ZERO), RunFor::Cycles(1))
            .unwrap();
        // If average_duration panicked we wouldn't reach here.
        // Also cover the n=0 branch by running a graph that completes normally.
        let result = Graph::new(
            vec![Rc::new(RefCell::new(CallBackStream::<u64>::new())).as_node()],
            RunMode::HistoricalFrom(NanoTime::ZERO),
            RunFor::Cycles(1),
        )
        .run();
        assert!(result.is_ok());
    }

    // ── average_duration (private fn) ────────────────────────────────────────

    #[test]
    fn average_duration_zero_n_returns_zero() {
        use std::time::Duration;
        // n=0 branch
        assert_eq!(average_duration(Duration::from_secs(10), 0), Duration::ZERO);
    }

    #[test]
    fn average_duration_normal_case() {
        use std::time::Duration;
        // 100ns / 4 = 25ns
        assert_eq!(
            average_duration(Duration::from_nanos(100), 4),
            Duration::from_nanos(25)
        );
    }

    // ── GraphState::node_index_ticked (pub(crate)) ────────────────────────────

    #[test]
    fn node_index_ticked_reflects_cycle() {
        let src = Rc::new(RefCell::new(CallBackStream::<u64>::new()));
        src.borrow_mut().push(ValueAt::new(1u64, NanoTime::new(1)));
        let cnt = src.clone().as_stream().count();
        cnt.run(RunMode::HistoricalFrom(NanoTime::ZERO), RunFor::Forever)
            .unwrap();
        // Just verify the fn exists and is callable by using it indirectly.
        // GraphState is not directly accessible after run(), but the fn is
        // exercised internally. We test the function directly:
        let mut state = GraphState::new(
            RunMode::HistoricalFrom(NanoTime::ZERO),
            RunFor::Cycles(1),
            NanoTime::ZERO,
        );
        state.node_ticked.push(false);
        assert!(!state.node_index_ticked(0));
        state.node_ticked[0] = true;
        assert!(state.node_index_ticked(0));
    }

    // ── GraphState::log (when current_node_index is None) ────────────────────

    #[test]
    fn graph_state_log_with_no_current_node_is_noop() {
        let state = GraphState::new(
            RunMode::HistoricalFrom(NanoTime::ZERO),
            RunFor::Cycles(1),
            NanoTime::ZERO,
        );
        // current_node_index is None → should return immediately without panic
        state.log(log::Level::Info, "test message");
    }

    // ── Graph::export ─────────────────────────────────────────────────────────

    #[test]
    fn graph_export_writes_gml_file() {
        use std::fs;
        let src: Rc<dyn Stream<u64>> = Rc::new(RefCell::new(CallBackStream::<u64>::new()));
        let mapped = src.map(|v| v + 1);
        let graph = mapped.into_graph(RunMode::HistoricalFrom(NanoTime::ZERO), RunFor::Cycles(0));
        let path = "/tmp/wingfoil_test_export.gml";
        graph.export(path).unwrap();
        let content = fs::read_to_string(path).unwrap();
        assert!(content.contains("graph ["));
        assert!(content.contains("node ["));
        fs::remove_file(path).unwrap();
    }

    #[test]
    fn historical_mode_works() {
        // wire up graph..
        //env_logger::init();
        let num_inputs = 7;
        let inputs: Vec<Rc<RefCell<CallBackStream<i32>>>> = (0..num_inputs)
            .map(|_| Rc::new(RefCell::new(CallBackStream::new())))
            .collect();
        let captured = inputs
            .iter()
            .map(|stream| stream.clone().as_stream().distinct())
            .tree_reduce(
                // https://docs.rs/itertools/0.8.0/itertools/trait.Itertools.html#method.tree_fold1
                // 1 2 3 4 5 6 7
                // │ │ │ │ │ │ │
                // └─f └─f └─f │
                //   │   │   │ │
                //   └───f   └─f
                //       │     │
                //       └─────f
                |a, b| add(&a, &b),
            )
            .unwrap()
            .collect();
        let mut expected: Vec<ValueAt<i32>> = vec![];
        // mock up input data
        // at future time 100 push all inputs with value 1
        push_all(
            &inputs,
            ValueAt {
                value: 1,
                time: NanoTime::new(100),
            },
        );
        expected.push(ValueAt {
            value: 7,
            time: NanoTime::new(100),
        });
        // at future time 200 push all inputs with value 1 again.
        push_all(
            &inputs,
            ValueAt {
                value: 1,
                time: NanoTime::new(200),
            },
        );
        // nothing expected - only input layer ticked because it is wrapped in distinct.
        // at future time 300 push first input with value 2
        push_first(
            &inputs,
            ValueAt {
                value: 2,
                time: NanoTime::new(300),
            },
        );
        expected.push(ValueAt {
            value: 8,
            time: NanoTime::new(300),
        });
        // at future time 400 push first input with value 2 again.  Only first input node ticks.
        push_first(
            &inputs,
            ValueAt {
                value: 2,
                time: NanoTime::new(400),
            },
        );
        // nothing expected
        // wire up graph and set historical mode
        let run_mode = RunMode::HistoricalFrom(NanoTime::ZERO);
        Graph::new(vec![captured.clone().as_node()], run_mode, RunFor::Forever)
            .print()
            .run()
            .unwrap();
        //.export("historical_mode_works")
        //.unwrap();
        let captured_data = captured.peek_value();
        println!();
        println!("captured_data  {:?}", captured_data);
        println!("expected       {:?}", expected);
        println!();
        assert_eq!(captured_data, expected);
    }

    #[test]
    fn error_context_shows_graph_structure() {
        use std::time::Duration;

        // Build deep graph: ticker -> count -> 10 maps -> try_map (fails) -> 10 maps
        let mut stream: Rc<dyn Stream<u64>> = ticker(Duration::from_nanos(100)).count();

        // 10 maps before try_map
        for _ in 0..10 {
            stream = stream.map(|x: u64| x);
        }

        // try_map that fails on count == 3
        stream = stream.try_map(|x: u64| {
            if x == 3 {
                anyhow::bail!("intentional failure at count 3")
            } else {
                Ok(x)
            }
        });

        // 10 maps after try_map
        for _ in 0..10 {
            stream = stream.map(|x: u64| x);
        }

        let mut graph = Graph::new(
            vec![stream.as_node()],
            RunMode::HistoricalFrom(NanoTime::ZERO),
            RunFor::Cycles(10),
        );
        graph.print();
        let result = graph.run();

        assert!(result.is_err(), "Expected error but got: {:?}", result);
        let err_msg = format!("{:?}", result.unwrap_err());

        let expected = r#"Error in node [14]:
    [11]                               MapStream<u64, u64>
    [12]                                  MapStream<u64, u64>
    [13]                                     MapStream<u64, u64>
>>> [14]                                        TryMapStream<u64, u64>
    [15]                                           MapStream<u64, u64>
    [16]                                              MapStream<u64, u64>
    [17]                                                 MapStream<u64, u64>


Caused by:
    intentional failure at count 3"#;

        assert!(
            err_msg.contains(expected),
            "Error message mismatch.\n\nExpected to contain:\n{expected}\n\nActual:\n{err_msg}"
        );
    }

    fn push_all(inputs: &[Rc<RefCell<CallBackStream<i32>>>], value_at: ValueAt<i32>) {
        inputs
            .iter()
            .for_each(|input| input.borrow_mut().push(value_at.clone()));
    }

    fn push_first(inputs: &[Rc<RefCell<CallBackStream<i32>>>], value_at: ValueAt<i32>) {
        inputs[0].borrow_mut().push(value_at);
    }

    /// Minimal test node: records state.time() on each cycle, and on its first
    /// cycle re-schedules itself at a caller-supplied time (which may equal or
    /// precede the current engine time).
    struct TimeCapturingNode {
        times: Vec<NanoTime>,
        resched_time: NanoTime,
    }

    impl MutableNode for TimeCapturingNode {
        fn cycle(&mut self, state: &mut GraphState) -> anyhow::Result<bool> {
            let t = state.time();
            self.times.push(t);
            if self.times.len() == 1 {
                state.add_callback(self.resched_time);
            }
            Ok(true)
        }

        fn start(&mut self, state: &mut GraphState) -> anyhow::Result<()> {
            state.add_callback(NanoTime::new(100));
            Ok(())
        }
    }

    /// Time must advance even when a node re-schedules itself at the current time.
    #[test]
    fn time_advances_with_duplicate_scheduled_time() {
        let node = Rc::new(RefCell::new(TimeCapturingNode {
            times: vec![],
            resched_time: NanoTime::new(100), // same as initial fire time
        }));
        Graph::new(
            vec![node.clone().as_node()],
            RunMode::HistoricalFrom(NanoTime::ZERO),
            RunFor::Cycles(2),
        )
        .run()
        .unwrap();

        let times = node.borrow().times.clone();
        assert_eq!(times.len(), 2, "expected exactly 2 cycles");
        assert!(
            times[1] > times[0],
            "time did not advance between cycles: {:?} -> {:?}",
            times[0],
            times[1]
        );
    }

    /// Time must not go backwards when a node re-schedules itself in the past.
    #[test]
    fn time_advances_with_past_scheduled_time() {
        let node = Rc::new(RefCell::new(TimeCapturingNode {
            times: vec![],
            resched_time: NanoTime::new(50), // in the past relative to initial fire at 100
        }));
        Graph::new(
            vec![node.clone().as_node()],
            RunMode::HistoricalFrom(NanoTime::ZERO),
            RunFor::Cycles(2),
        )
        .run()
        .unwrap();

        let times = node.borrow().times.clone();
        assert_eq!(times.len(), 2, "expected exactly 2 cycles");
        assert!(
            times[1] > times[0],
            "time went backwards or stalled: {:?} -> {:?}",
            times[0],
            times[1]
        );
    }

    // ── Dynamism tests ────────────────────────────────────────────────────────

    #[test]
    fn ticked_on_unregistered_node_returns_false() {
        use std::time::Duration;
        let orphan = ticker(Duration::from_nanos(1)).count();
        let state = GraphState::new(
            RunMode::HistoricalFrom(NanoTime::ZERO),
            RunFor::Cycles(1),
            NanoTime::ZERO,
        );
        // Must return false (not panic) for a node not yet registered in the graph
        assert!(!state.ticked(orphan.as_node()));
    }

    #[cfg(feature = "dynamic-graph-beta")]
    mod dynamism {
        use super::*;

        /// A node that, after `add_after` cycles, calls `state.add_upstream(extra, true, false)`.
        /// Counts how many times `extra` has ticked via `extra_ticks`.
        struct DynAdderNode {
            trigger: Rc<dyn Node>,
            extra: Rc<dyn Node>,
            add_after: u64,
            cycle_count: u64,
            extra_ticks: Rc<RefCell<u64>>,
            stop_count: Rc<RefCell<u64>>,
            teardown_count: Rc<RefCell<u64>>,
        }

        impl MutableNode for DynAdderNode {
            fn upstreams(&self) -> UpStreams {
                UpStreams::new(vec![self.trigger.clone()], vec![])
            }

            fn cycle(&mut self, state: &mut GraphState) -> anyhow::Result<bool> {
                self.cycle_count += 1;
                if self.cycle_count == self.add_after {
                    state.add_upstream(self.extra.clone(), true, false);
                }
                if state.ticked(self.extra.clone()) {
                    *self.extra_ticks.borrow_mut() += 1;
                }
                Ok(true)
            }

            fn stop(&mut self, _state: &mut GraphState) -> anyhow::Result<()> {
                *self.stop_count.borrow_mut() += 1;
                Ok(())
            }

            fn teardown(&mut self, _state: &mut GraphState) -> anyhow::Result<()> {
                *self.teardown_count.borrow_mut() += 1;
                Ok(())
            }
        }

        /// A stream that counts how many times its trigger has ticked.
        /// Unlike `count()`, it does not use `constant` internally, so it never
        /// schedules a past-time callback and is safe to add dynamically.
        struct SimpleCounter {
            trigger: Rc<dyn Node>,
            n: u64,
        }

        impl MutableNode for SimpleCounter {
            fn upstreams(&self) -> UpStreams {
                UpStreams::new(vec![self.trigger.clone()], vec![])
            }

            fn cycle(&mut self, _: &mut GraphState) -> anyhow::Result<bool> {
                self.n += 1;
                Ok(true)
            }
        }

        impl StreamPeekRef<u64> for SimpleCounter {
            fn peek_ref(&self) -> &u64 {
                &self.n
            }
        }

        /// A node that records setup/start/cycle/stop/teardown call counts.
        struct LifecycleCounterNode {
            trigger: Rc<dyn Node>,
            cycle_count: Rc<RefCell<u64>>,
            stop_count: Rc<RefCell<u64>>,
            teardown_count: Rc<RefCell<u64>>,
        }

        impl MutableNode for LifecycleCounterNode {
            fn upstreams(&self) -> UpStreams {
                UpStreams::new(vec![self.trigger.clone()], vec![])
            }

            fn cycle(&mut self, _: &mut GraphState) -> anyhow::Result<bool> {
                *self.cycle_count.borrow_mut() += 1;
                Ok(true)
            }

            fn stop(&mut self, _: &mut GraphState) -> anyhow::Result<()> {
                *self.stop_count.borrow_mut() += 1;
                Ok(())
            }

            fn teardown(&mut self, _: &mut GraphState) -> anyhow::Result<()> {
                *self.teardown_count.borrow_mut() += 1;
                Ok(())
            }
        }

        #[test]
        fn add_upstream_dynamically_fires_only_after_wired() {
            use std::time::Duration;

            let ticker = ticker(Duration::from_nanos(1));
            // Use SimpleCounter instead of ticker.count() to avoid the constant
            // node scheduling a past-time callback when added dynamically.
            let extra: Rc<dyn Stream<u64>> = SimpleCounter {
                trigger: ticker.clone(),
                n: 0,
            }
            .into_stream();

            let extra_ticks = Rc::new(RefCell::new(0u64));
            let stop_count = Rc::new(RefCell::new(0u64));
            let teardown_count = Rc::new(RefCell::new(0u64));

            // adder is triggered by ticker; after cycle 3 it adds `extra` as an upstream
            let adder = Rc::new(RefCell::new(DynAdderNode {
                trigger: ticker.clone(),
                extra: extra.clone().as_node(),
                add_after: 3,
                cycle_count: 0,
                extra_ticks: extra_ticks.clone(),
                stop_count: stop_count.clone(),
                teardown_count: teardown_count.clone(),
            }));

            Graph::new(
                vec![adder.clone().as_node()],
                RunMode::HistoricalFrom(NanoTime::ZERO),
                RunFor::Cycles(6),
            )
            .run()
            .unwrap();

            // extra was wired at the end of cycle 3; it should tick on cycles 4, 5, 6 → 3 times
            assert_eq!(*extra_ticks.borrow(), 3, "extra_ticks");
        }

        /// A node that removes `target` from the graph after `remove_after` ticks.
        struct DynRemoverNode {
            trigger: Rc<dyn Node>,
            target: Rc<dyn Node>,
            remove_after: u64,
            cycle_count: u64,
        }

        impl MutableNode for DynRemoverNode {
            fn upstreams(&self) -> UpStreams {
                UpStreams::new(vec![self.trigger.clone()], vec![])
            }

            fn cycle(&mut self, state: &mut GraphState) -> anyhow::Result<bool> {
                self.cycle_count += 1;
                if self.cycle_count == self.remove_after {
                    state.remove_node(self.target.clone());
                }
                Ok(true)
            }
        }

        #[test]
        fn remove_node_stops_firing_and_calls_lifecycle() {
            use std::time::Duration;

            let ticker = ticker(Duration::from_nanos(1));
            let extra_ticks = Rc::new(RefCell::new(0u64));
            let stop_count = Rc::new(RefCell::new(0u64));
            let teardown_count = Rc::new(RefCell::new(0u64));

            let extra_ticks_c = extra_ticks.clone();
            let stop_c = stop_count.clone();
            let teardown_c = teardown_count.clone();

            // adder: after cycle 1, wires `extra` (count stream) as an upstream
            let extra = ticker.count();
            let adder = Rc::new(RefCell::new(DynAdderNode {
                trigger: ticker.clone(),
                extra: extra.clone().as_node(),
                add_after: 1,
                cycle_count: 0,
                extra_ticks: extra_ticks_c,
                stop_count: stop_c,
                teardown_count: teardown_c,
            }));

            // remover: after cycle 5, removes adder from the graph
            let remover = Rc::new(RefCell::new(DynRemoverNode {
                trigger: ticker.clone(),
                target: adder.clone().as_node(),
                remove_after: 5,
                cycle_count: 0,
            }));

            Graph::new(
                vec![adder.clone().as_node(), remover.clone().as_node()],
                RunMode::HistoricalFrom(NanoTime::ZERO),
                RunFor::Cycles(8),
            )
            .run()
            .unwrap();

            // adder fires on cycles 1..=8 = 8 times; extra wired at end of cycle 1,
            // so extra fires on cycles 2..=8 → but adder is removed at end of cycle 5,
            // so extra_ticks should be counted on cycles 2,3,4,5 = 4
            assert_eq!(*extra_ticks.borrow(), 4, "extra_ticks after removal");
            // stop + teardown each called exactly once (at removal, not at graph shutdown)
            assert_eq!(*stop_count.borrow(), 1, "stop_count");
            assert_eq!(*teardown_count.borrow(), 1, "teardown_count");
        }

        #[test]
        fn remove_cleans_up_caller_upstreams() {
            use std::time::Duration;

            let ticker = ticker(Duration::from_nanos(1));
            let extra = ticker.count();
            let extra_ticks = Rc::new(RefCell::new(0u64));
            let stop_count = Rc::new(RefCell::new(0u64));
            let teardown_count = Rc::new(RefCell::new(0u64));

            // Wire extra at cycle 1, then remove it at cycle 3
            let adder = Rc::new(RefCell::new(DynAdderNode {
                trigger: ticker.clone(),
                extra: extra.clone().as_node(),
                add_after: 1,
                cycle_count: 0,
                extra_ticks: extra_ticks.clone(),
                stop_count: stop_count.clone(),
                teardown_count: teardown_count.clone(),
            }));

            let remover = Rc::new(RefCell::new(DynRemoverNode {
                trigger: ticker.clone(),
                target: extra.clone().as_node(),
                remove_after: 3,
                cycle_count: 0,
            }));

            let mut graph = Graph::new(
                vec![adder.clone().as_node(), remover.clone().as_node()],
                RunMode::HistoricalFrom(NanoTime::ZERO),
                RunFor::Cycles(6),
            );
            graph.run().unwrap();

            // After removal, adder's upstreams should not contain the removed node's index
            let adder_idx = graph
                .state
                .node_index(adder.clone().as_node())
                .expect("adder in graph");
            let extra_idx = graph.state.node_index(extra.clone().as_node());
            if let Some(extra_idx) = extra_idx {
                let adder_upstreams = &graph.state.nodes[adder_idx].upstreams;
                assert!(
                    !adder_upstreams.iter().any(|(ui, _)| *ui == extra_idx),
                    "removed node should not remain in caller's upstreams"
                );
            }
        }

        #[test]
        fn add_upstream_with_recycle_delivers_first_value() {
            use std::time::Duration;

            // A node that adds `extra` as an upstream on cycle 1 with recycle=true,
            // and then records extra's value every time it ticks.
            struct RecycleNode {
                trigger: Rc<dyn Node>,
                extra: Rc<dyn Stream<u64>>,
                added: bool,
                values: Rc<RefCell<Vec<u64>>>,
            }

            impl MutableNode for RecycleNode {
                fn upstreams(&self) -> UpStreams {
                    UpStreams::new(vec![self.trigger.clone()], vec![])
                }

                fn cycle(&mut self, state: &mut GraphState) -> anyhow::Result<bool> {
                    if !self.added {
                        state.add_upstream(self.extra.clone().as_node(), true, true);
                        self.added = true;
                    }
                    if state.ticked(self.extra.clone().as_node()) {
                        self.values.borrow_mut().push(self.extra.peek_value());
                    }
                    Ok(true)
                }
            }

            let ticker = ticker(Duration::from_nanos(1));
            // Use SimpleCounter to avoid constant's past-time start callback.
            let extra: Rc<dyn Stream<u64>> = SimpleCounter {
                trigger: ticker.clone(),
                n: 0,
            }
            .into_stream();
            let values = Rc::new(RefCell::new(Vec::<u64>::new()));

            let node = Rc::new(RefCell::new(RecycleNode {
                trigger: ticker.clone(),
                extra: extra.clone(),
                added: false,
                values: values.clone(),
            }));

            Graph::new(
                vec![node.clone().as_node()],
                RunMode::HistoricalFrom(NanoTime::ZERO),
                RunFor::Cycles(3),
            )
            .run()
            .unwrap();

            // Cycle 1 (t=0ns): ticker fires. RecycleNode wires extra with recycle=true.
            // process_pending_additions schedules the recycle callback at state.time+1 = 1ns.
            // Cycle 2 (t=1ns): ticker fires (2nd tick) AND recycle fires extra — both at 1ns.
            //   extra cycles → n=1. RecycleNode triggered → values=[1].
            // Cycle 3 (t=2ns): ticker fires (3rd tick). extra cycles → n=2. values=[1,2].
            // RunFor::Cycles(3) → 3 cycles total → values = [1, 2].
            assert_eq!(*values.borrow(), vec![1u64, 2], "recycle values");
        }

        #[test]
        fn add_upstream_passive_does_not_trigger() {
            use std::time::Duration;

            // A node that adds `extra` as a PASSIVE upstream; should not be triggered by it.
            struct PassiveNode {
                trigger: Rc<dyn Node>,
                extra: Rc<dyn Stream<u64>>,
                added: bool,
                trigger_ticks: u64,
                extra_observed: Rc<RefCell<Vec<u64>>>,
            }

            impl MutableNode for PassiveNode {
                fn upstreams(&self) -> UpStreams {
                    UpStreams::new(vec![self.trigger.clone()], vec![])
                }

                fn cycle(&mut self, state: &mut GraphState) -> anyhow::Result<bool> {
                    if !self.added {
                        // passive upstream: is_active = false
                        state.add_upstream(self.extra.clone().as_node(), false, false);
                        self.added = true;
                    }
                    // We are only triggered by `trigger`, not by `extra`.
                    // But we can still peek `extra`'s value.
                    if state.ticked(self.trigger.clone()) {
                        self.trigger_ticks += 1;
                        self.extra_observed
                            .borrow_mut()
                            .push(self.extra.peek_value());
                    }
                    Ok(true)
                }
            }

            let ticker = ticker(Duration::from_nanos(1));
            // Use SimpleCounter to avoid constant's past-time start callback.
            let extra: Rc<dyn Stream<u64>> = SimpleCounter {
                trigger: ticker.clone(),
                n: 0,
            }
            .into_stream();
            let extra_observed = Rc::new(RefCell::new(Vec::<u64>::new()));

            let node = Rc::new(RefCell::new(PassiveNode {
                trigger: ticker.clone(),
                extra: extra.clone(),
                added: false,
                trigger_ticks: 0,
                extra_observed: extra_observed.clone(),
            }));

            Graph::new(
                vec![node.clone().as_node()],
                RunMode::HistoricalFrom(NanoTime::ZERO),
                RunFor::Cycles(4),
            )
            .run()
            .unwrap();

            // The node ticked 4 times (triggered by ticker each cycle).
            // extra is a passive upstream — node is NOT triggered by extra.
            assert_eq!(extra_observed.borrow().len(), 4);
        }

        #[test]
        fn seen_prevents_double_setup_start() {
            use std::time::Duration;

            struct CountedNode {
                ticker: Rc<dyn Node>,
                setup_count: Rc<RefCell<u32>>,
                start_count: Rc<RefCell<u32>>,
            }

            impl MutableNode for CountedNode {
                fn upstreams(&self) -> UpStreams {
                    UpStreams::new(vec![self.ticker.clone()], vec![])
                }

                fn cycle(&mut self, _state: &mut GraphState) -> anyhow::Result<bool> {
                    Ok(true)
                }

                fn setup(&mut self, _state: &mut GraphState) -> anyhow::Result<()> {
                    *self.setup_count.borrow_mut() += 1;
                    Ok(())
                }

                fn start(&mut self, _state: &mut GraphState) -> anyhow::Result<()> {
                    *self.start_count.borrow_mut() += 1;
                    Ok(())
                }
            }

            // A node that adds `counted` as upstream twice on consecutive cycles
            struct DoublerNode {
                trigger: Rc<dyn Node>,
                counted: Rc<dyn Node>,
                add_count: u64,
            }

            impl MutableNode for DoublerNode {
                fn upstreams(&self) -> UpStreams {
                    UpStreams::new(vec![self.trigger.clone()], vec![])
                }

                fn cycle(&mut self, state: &mut GraphState) -> anyhow::Result<bool> {
                    if self.add_count < 2 {
                        state.add_upstream(self.counted.clone(), true, false);
                        self.add_count += 1;
                    }
                    Ok(true)
                }
            }

            let setup_count = Rc::new(RefCell::new(0u32));
            let start_count = Rc::new(RefCell::new(0u32));
            let ticker = ticker(Duration::from_nanos(1));
            let counted = Rc::new(RefCell::new(CountedNode {
                ticker: ticker.clone(),
                setup_count: setup_count.clone(),
                start_count: start_count.clone(),
            }));
            let doubler = Rc::new(RefCell::new(DoublerNode {
                trigger: ticker.clone(),
                counted: counted.clone().as_node(),
                add_count: 0,
            }));

            Graph::new(
                vec![doubler.clone().as_node()],
                RunMode::HistoricalFrom(NanoTime::ZERO),
                RunFor::Cycles(3),
            )
            .run()
            .unwrap();

            // setup and start should only be called once even though add_upstream was called twice
            assert_eq!(*setup_count.borrow(), 1, "setup called once");
            assert_eq!(*start_count.borrow(), 1, "start called once");
        }

        #[test]
        fn layer_resort_after_deep_upstream_addition() {
            use std::time::Duration;

            // Build: ticker → depth1 → depth2 → aggregation
            // aggregation's initial layer is > depth2's layer.
            // Then add depth2 as an upstream of aggregation — fix_layers should
            // ensure aggregation's layer is >= depth2.layer + 1.
            let ticker = ticker(Duration::from_nanos(1));
            let depth1 = ticker.count(); // layer 1
            let depth2 = depth1.map(|x: u64| x * 2); // layer 2

            // aggregation starts triggered by ticker (layer 0), so its layer = 1.
            // We then add depth2 (layer 2) as an upstream → aggregation should move to layer 3.
            struct LayerCheckNode {
                trigger: Rc<dyn Node>,
                deep: Rc<dyn Stream<u64>>,
                added: bool,
                sum: u64,
            }

            impl MutableNode for LayerCheckNode {
                fn upstreams(&self) -> UpStreams {
                    UpStreams::new(vec![self.trigger.clone()], vec![])
                }

                fn cycle(&mut self, state: &mut GraphState) -> anyhow::Result<bool> {
                    if !self.added {
                        state.add_upstream(self.deep.clone().as_node(), true, false);
                        self.added = true;
                    }
                    if state.ticked(self.deep.clone().as_node()) {
                        self.sum += self.deep.peek_value();
                    }
                    Ok(true)
                }
            }

            let node = Rc::new(RefCell::new(LayerCheckNode {
                trigger: ticker.clone(),
                deep: depth2.clone(),
                added: false,
                sum: 0,
            }));

            let mut graph = Graph::new(
                vec![node.clone().as_node()],
                RunMode::HistoricalFrom(NanoTime::ZERO),
                RunFor::Cycles(4),
            );
            graph.run().unwrap();

            // After adding depth2, node's layer should be >= depth2's layer + 1
            let node_idx = graph
                .state
                .node_index(node.clone().as_node())
                .expect("node in graph");
            let depth2_idx = graph
                .state
                .node_index(depth2.clone().as_node())
                .expect("depth2 in graph");
            assert!(
                graph.state.nodes[node_idx].layer > graph.state.nodes[depth2_idx].layer,
                "aggregation layer should be > depth2 layer after fix_layers"
            );
        }
        #[test]
        fn add_and_remove_in_same_cycle_node_is_wired() {
            use std::time::Duration;

            // Calling add_upstream and remove_node for the same node in the same cycle:
            // process_pending_removals runs before process_pending_additions, so the
            // removal is a no-op (node is not yet in the graph) and the node ends up wired.
            struct AdderRemoverNode {
                trigger: Rc<dyn Node>,
                extra: Rc<dyn Node>,
                done: bool,
                extra_ticks: Rc<RefCell<u64>>,
            }

            impl MutableNode for AdderRemoverNode {
                fn upstreams(&self) -> UpStreams {
                    UpStreams::new(vec![self.trigger.clone()], vec![])
                }

                fn cycle(&mut self, state: &mut GraphState) -> anyhow::Result<bool> {
                    if !self.done {
                        state.add_upstream(self.extra.clone(), true, false);
                        state.remove_node(self.extra.clone());
                        self.done = true;
                    }
                    if state.ticked(self.extra.clone()) {
                        *self.extra_ticks.borrow_mut() += 1;
                    }
                    Ok(true)
                }
            }

            let ticker = ticker(Duration::from_nanos(1));
            let extra: Rc<dyn Stream<u64>> = SimpleCounter {
                trigger: ticker.clone(),
                n: 0,
            }
            .into_stream();
            let extra_ticks = Rc::new(RefCell::new(0u64));

            let node = Rc::new(RefCell::new(AdderRemoverNode {
                trigger: ticker.clone(),
                extra: extra.clone().as_node(),
                done: false,
                extra_ticks: extra_ticks.clone(),
            }));

            Graph::new(
                vec![node.clone().as_node()],
                RunMode::HistoricalFrom(NanoTime::ZERO),
                RunFor::Cycles(4),
            )
            .run()
            .unwrap();

            // Removal was a no-op (ran before addition), so extra is wired and fires
            // on cycles 2, 3, 4 → 3 ticks.
            assert_eq!(*extra_ticks.borrow(), 3, "extra_ticks");
        }

        #[test]
        fn remove_node_that_never_cycled_calls_lifecycle() {
            use std::time::Duration;

            // Wire a node whose trigger never fires (a CallBackStream with no callbacks
            // scheduled), so cycle() is never called on it. Removing it should still
            // invoke stop() and teardown() exactly once.
            //
            // Note: a ticker is unsuitable here because even a 1-hour ticker fires once
            // at t=0 in HistoricalFrom mode, which would cause extra to cycle.
            let clk = ticker(Duration::from_nanos(1));
            let never = Rc::new(RefCell::new(CallBackStream::<i32>::new()));

            let cycle_count = Rc::new(RefCell::new(0u64));
            let stop_count = Rc::new(RefCell::new(0u64));
            let teardown_count = Rc::new(RefCell::new(0u64));

            let extra = Rc::new(RefCell::new(LifecycleCounterNode {
                trigger: never.clone().as_node(),
                cycle_count: cycle_count.clone(),
                stop_count: stop_count.clone(),
                teardown_count: teardown_count.clone(),
            }));

            // Wire extra at cycle 1, remove it at cycle 3.
            let adder = Rc::new(RefCell::new(DynAdderNode {
                trigger: clk.clone(),
                extra: extra.clone().as_node(),
                add_after: 1,
                cycle_count: 0,
                extra_ticks: Rc::new(RefCell::new(0u64)),
                stop_count: Rc::new(RefCell::new(0u64)),
                teardown_count: Rc::new(RefCell::new(0u64)),
            }));

            let remover = Rc::new(RefCell::new(DynRemoverNode {
                trigger: clk.clone(),
                target: extra.clone().as_node(),
                remove_after: 3,
                cycle_count: 0,
            }));

            Graph::new(
                vec![adder.clone().as_node(), remover.clone().as_node()],
                RunMode::HistoricalFrom(NanoTime::ZERO),
                RunFor::Cycles(5),
            )
            .run()
            .unwrap();

            assert_eq!(*cycle_count.borrow(), 0, "extra cycle() never called");
            assert_eq!(*stop_count.borrow(), 1, "stop called once on removal");
            assert_eq!(
                *teardown_count.borrow(),
                1,
                "teardown called once on removal"
            );
        }
    } // mod dynamism
}