rill-graph 0.5.0

Real-time signal graph with block processing
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
use crate::factory::NodeFactory;
use std::sync::Arc;

use rill_core::buffer::{FixedBuffer, ResourceRegistry, TapeLoop};
use rill_core::io::{IoCapture, IoDriver, IoPlayback};
use rill_core::math::Transcendental;
use rill_core::queues::CommandEnum;
use rill_core::queues::SetParameter;
use rill_core::time::{ClockTick, RenderContext, SystemClock};
use rill_core::traits::port::Port;
use rill_core::traits::processable::Processable;
use rill_core::traits::{Node, NodeId, NodeVariant, Params, ProcessResult};
use rill_core_actor::{Actor, ActorRef, ActorSystem};
use std::cell::{RefCell, UnsafeCell};
use std::collections::{HashSet, VecDeque};
use std::rc::Rc;
use std::sync::atomic::AtomicBool;

// ============================================================================
// Internal routing metadata
// ============================================================================

// ============================================================================
// Build Errors
// ============================================================================

/// Errors that can occur during graph construction.
#[derive(Debug, Clone)]
pub enum BuildError {
    /// A cycle was detected in the signal edge graph.
    CycleDetected,
    /// Backend creation failed.
    Backend(String),
    /// Factory registration error (unknown node type).
    Registry(String),
}

impl std::fmt::Display for BuildError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::CycleDetected => write!(f, "graph cycle detected"),
            Self::Backend(msg) => write!(f, "backend error: {msg}"),
            Self::Registry(msg) => write!(f, "registry error: {msg}"),
        }
    }
}

// ============================================================================
// Graph Builder
// ============================================================================

// ============================================================================
// Node Storage
// ============================================================================

/// A deferred node recipe — constructed at [`build`](GraphBuilder::build) time.
struct NodeRecipe<T: Transcendental, const BUF_SIZE: usize> {
    type_name: String,
    id: NodeId,
    params: Params,
    routing_entries: Vec<(usize, usize, f32)>,
    _phantom: std::marker::PhantomData<(T, [(); BUF_SIZE])>,
}

/// Temporary holder during build — wraps a constructed node for wiring.
struct NodeEntry<T: Transcendental, const BUF_SIZE: usize> {
    node: NodeVariant<T, BUF_SIZE>,
}

// ============================================================================
// GraphBuilder (Mutable Construction)
// ============================================================================

/// A named resource (tape loop) shared between nodes in the graph.
#[derive(Clone)]
pub struct GraphResource {
    /// Unique name referenced by node parameters.
    pub name: String,
    /// Resource kind string (`"tape"`).
    pub kind: String,
    /// Capacity in samples (for `"tape"` kind).
    pub capacity: usize,
}

/// Mutable builder for an immutable signal graph.
///
/// Stores deferred node recipes until [`build`](Self::build), which
/// constructs all nodes, wires connections, and performs topological
/// sort.  This keeps `GraphBuilder` `Send` — all non‑`Send` data
/// is constructed inside the target thread.
///
/// # Node factory
///
/// The builder holds an [`Arc<NodeFactory>`] for constructing nodes by
/// type name. Nodes registered via [`add_node_with_id`](Self::add_node_with_id)
/// are only validated and constructed at [`build`](Self::build) time.
pub struct GraphBuilder<T: Transcendental, const BUF_SIZE: usize> {
    recipes: Vec<NodeRecipe<T, BUF_SIZE>>,
    signal_edges: Vec<(usize, usize, usize, usize)>,
    control_edges: Vec<(usize, usize, usize, usize)>,
    clock_edges: Vec<(usize, usize, usize, usize)>,
    feedback_edges: Vec<(usize, usize, usize, usize)>,
    resources: Vec<GraphResource>,
    /// Shared node factory (required, from Runtime).
    factory: Arc<NodeFactory<T, BUF_SIZE>>,
    /// Sample rate override. When set, used in [`build`](Self::build).
    /// Populated from [`GraphDef::sample_rate`] during deserialization.
    sample_rate: Option<f32>,
    /// Parent RackCase ActorRef — Graph sends ClockTick here.
    parent_ref: Option<ActorRef<CommandEnum>>,
}

impl<T: Transcendental, const BUF_SIZE: usize> GraphBuilder<T, BUF_SIZE> {
    /// Create a new empty graph builder.
    pub fn new(factory: Arc<NodeFactory<T, BUF_SIZE>>) -> Self {
        Self {
            recipes: Vec::new(),
            signal_edges: Vec::new(),
            control_edges: Vec::new(),
            clock_edges: Vec::new(),
            feedback_edges: Vec::new(),
            resources: Vec::new(),
            factory,
            sample_rate: None,
            parent_ref: None,
        }
    }

    /// Add a node by type name using the internal factory.
    ///
    /// The type must be registered in the factory before [`build`](Self::build)
    /// is called.
    ///
    /// Returns the index of the newly added node.
    pub fn add_node(&mut self, type_name: &str, params: &Params) -> usize {
        let id = NodeId(self.recipes.len() as u32);
        self.add_node_with_id(type_name, params, id)
    }

    /// Add a node with an explicit [`NodeId`].
    ///
    /// Like [`add_node`](Self::add_node) but uses the provided `id`.
    /// Important for serialization where external references depend on
    /// exact IDs.
    pub fn add_node_with_id(&mut self, type_name: &str, params: &Params, id: NodeId) -> usize {
        let idx = self.recipes.len();
        self.recipes.push(NodeRecipe {
            type_name: type_name.to_string(),
            id,
            params: params.clone(),
            routing_entries: Vec::new(),
            _phantom: std::marker::PhantomData,
        });
        idx
    }

    /// Store a routing matrix entry to be applied at build time.
    pub fn add_routing_entry(&mut self, idx: usize, from: usize, to: usize, gain: f32) {
        if let Some(recipe) = self.recipes.get_mut(idx) {
            recipe.routing_entries.push((from, to, gain));
        }
    }

    /// Register a named resource (tape loop, buffer, etc.).
    pub fn add_resource(&mut self, resource: GraphResource) {
        self.resources.push(resource);
    }

    /// Number of nodes added to the builder so far.
    pub fn node_count(&self) -> usize {
        self.recipes.len()
    }

    /// Set the sample rate for this builder.
    pub fn set_sample_rate(&mut self, sr: f32) {
        self.sample_rate = Some(sr);
    }

    /// Set the parent RackCase actor reference (Graph → parent ClockTick).
    pub fn set_parent_ref(&mut self, parent: ActorRef<CommandEnum>) {
        self.parent_ref = Some(parent);
    }

    /// Connect signal ports.
    pub fn connect_signal(
        &mut self,
        from_node: usize,
        from_port: usize,
        to_node: usize,
        to_port: usize,
    ) {
        self.signal_edges
            .push((from_node, from_port, to_node, to_port));
    }

    /// Connect control ports (modulation values).
    pub fn connect_control(
        &mut self,
        from_node: usize,
        from_port: usize,
        to_node: usize,
        to_port: usize,
    ) {
        self.control_edges
            .push((from_node, from_port, to_node, to_port));
    }

    /// Connect clock ports (timing events).
    pub fn connect_clock(
        &mut self,
        from_node: usize,
        from_port: usize,
        to_node: usize,
        to_port: usize,
    ) {
        self.clock_edges
            .push((from_node, from_port, to_node, to_port));
    }

    /// Connect feedback ports (delay lines, state carryover).
    pub fn connect_feedback(
        &mut self,
        from_node: usize,
        from_port: usize,
        to_node: usize,
        to_port: usize,
    ) {
        self.feedback_edges
            .push((from_node, from_port, to_node, to_port));
    }

    /// Build the graph.
    ///
    /// Creates backends for nodes that have a backend name set (via
    /// `SourceDef::backend` / `SinkDef::backend` or the builder's default).  Finds the active
    /// (driver) node and stores its index for [`Graph::run`].
    pub fn build(self, system: &ActorSystem) -> Result<Graph<T, BUF_SIZE>, BuildError> {
        // Phase 1: Construct all nodes from recipes
        let mut node_entries: Vec<NodeEntry<T, BUF_SIZE>> = Vec::with_capacity(self.recipes.len());
        for recipe in &self.recipes {
            let node = self
                .factory
                .construct(&recipe.type_name, recipe.id, &recipe.params)
                .map_err(|e| BuildError::Registry(format!("{e}")))?;
            node_entries.push(NodeEntry { node });
        }

        // Apply pre-configured routing entries
        for (idx, node) in node_entries.iter_mut().enumerate() {
            for &(from, to, gain) in &self.recipes[idx].routing_entries {
                if let NodeVariant::Router(ref mut router) = node.node {
                    router.set_connection(from, to, T::from_f32(gain)).ok();
                }
            }
        }

        let num_nodes = node_entries.len();

        // --- Phase 2: adjacency for Kahn (signal edges only) ---
        let mut in_degree = vec![0usize; num_nodes];
        let mut out_edges: Vec<Vec<(usize, usize, usize)>> = vec![Vec::new(); num_nodes];

        for &(from_n, from_p, to_n, to_p) in &self.signal_edges {
            in_degree[to_n] += 1;
            out_edges[from_n].push((from_p, to_n, to_p));
        }

        // Categorize root nodes (in_degree == 0) by type_name
        let recording_roots: Vec<usize> = in_degree
            .iter()
            .enumerate()
            .filter(|(i, &d)| d == 0 && self.recipes[*i].type_name == "rill/input")
            .map(|(i, _)| i)
            .collect();
        let playback_roots: Vec<usize> = in_degree
            .iter()
            .enumerate()
            .filter(|(i, &d)| d == 0 && self.recipes[*i].type_name != "rill/input")
            .map(|(i, _)| i)
            .collect();

        // --- Kahn's algorithm ---
        let mut queue: VecDeque<usize> = in_degree
            .iter()
            .enumerate()
            .filter(|(_, &d)| d == 0)
            .map(|(i, _)| i)
            .collect();

        let mut topo = Vec::with_capacity(num_nodes);
        let mut indeg = in_degree;
        while let Some(idx) = queue.pop_front() {
            topo.push(idx);
            for &(_, to_n, _) in &out_edges[idx] {
                indeg[to_n] -= 1;
                if indeg[to_n] == 0 {
                    queue.push_back(to_n);
                }
            }
        }

        if topo.len() != num_nodes {
            return Err(BuildError::CycleDetected);
        }

        // --- collect nodes into final Vec ---
        let mut nodes: Vec<NodeVariant<T, BUF_SIZE>> =
            node_entries.into_iter().map(|e| e.node).collect();

        // --- Phase 5: port pointer wiring on the final nodes Vec ---
        for &(from_n, from_p, to_n, to_p) in &self.signal_edges {
            if let Some(port) = nodes[from_n].output_port_mut(from_p) {
                port.add_downstream(to_n, to_p);
            }
            let in_ptr: *mut Port<T, BUF_SIZE> = nodes[to_n]
                .input_port_mut(to_p)
                .map(|p| p as *mut Port<T, BUF_SIZE>)
                .unwrap_or(std::ptr::null_mut());
            let out_ptr: *mut Port<T, BUF_SIZE> = nodes[from_n]
                .output_port_mut(from_p)
                .map(|p| p as *mut Port<T, BUF_SIZE>)
                .unwrap_or(std::ptr::null_mut());
            if !in_ptr.is_null() && !out_ptr.is_null() {
                #[allow(unsafe_code)]
                unsafe {
                    (*out_ptr).add_downstream_input_ptr(in_ptr);
                }
            }
        }

        // --- chain membership (recording vs playback) ---
        //
        // Recording roots are I/O input nodes (type "rill/input").
        let has_split_chain = !recording_roots.is_empty() && !playback_roots.is_empty();

        let mut recording_set: HashSet<usize> = HashSet::new();
        let mut playback_set: HashSet<usize> = HashSet::new();

        if has_split_chain {
            recording_set = recording_roots.iter().copied().collect();
            let mut queue: VecDeque<usize> = recording_roots.iter().copied().collect();
            while let Some(node) = queue.pop_front() {
                for &(_, to_n, _) in &out_edges[node] {
                    if recording_set.insert(to_n) {
                        queue.push_back(to_n);
                    }
                }
            }

            playback_set = playback_roots.iter().copied().collect();
            let mut queue: VecDeque<usize> = playback_roots.iter().copied().collect();
            while let Some(node) = queue.pop_front() {
                for &(_, to_n, _) in &out_edges[node] {
                    if playback_set.insert(to_n) {
                        queue.push_back(to_n);
                    }
                }
            }

            // Intersection → playback wins
            for node in recording_set.clone() {
                if playback_set.contains(&node) && !recording_roots.contains(&node) {
                    recording_set.remove(&node);
                }
            }
        }

        // --- downstream_nodes (chain-filtered) ---
        for &(from_n, from_p, to_n, _) in &self.signal_edges {
            let parent: *mut NodeVariant<T, BUF_SIZE> = &mut nodes[to_n];
            if let Some(port) = nodes[from_n].output_port_mut(from_p) {
                if has_split_chain {
                    let same_chain = (recording_set.contains(&from_n)
                        && recording_set.contains(&to_n))
                        || (playback_set.contains(&from_n) && playback_set.contains(&to_n));
                    if !same_chain {
                        continue;
                    }
                }
                port.add_downstream_node(parent);
            }
        }

        // --- upstream_node (pull-model, same-chain only) ---
        for &(from_n, _from_p, to_n, to_p) in &self.signal_edges {
            let same_chain = if has_split_chain {
                (recording_set.contains(&from_n) && recording_set.contains(&to_n))
                    || (playback_set.contains(&from_n) && playback_set.contains(&to_n))
            } else {
                true
            };
            if same_chain {
                let src: *mut NodeVariant<T, BUF_SIZE> = &mut nodes[from_n];
                if let Some(port) = nodes[to_n].input_port_mut(to_p) {
                    port.set_upstream_node(src);
                }
            }
        }

        // --- upstream_buffer (zero-copy alias, exclusive 1:1 edges only) ---
        //
        // An input port may read its upstream output buffer directly (no copy)
        // ONLY when the edge is exclusive: the source output has exactly one
        // consumer AND the input has exactly one producer. Fan-out branches
        // must each receive an independent copy so downstream processing is
        // isolated (per the zero-copy rules); fan-in ports need their own
        // buffer too. Both leave `upstream_buffer` as `None` → materialized.
        let mut out_degree: std::collections::HashMap<(usize, usize), usize> =
            std::collections::HashMap::new();
        let mut in_degree_port: std::collections::HashMap<(usize, usize), usize> =
            std::collections::HashMap::new();
        for &(from_n, from_p, to_n, to_p) in &self.signal_edges {
            *out_degree.entry((from_n, from_p)).or_insert(0) += 1;
            *in_degree_port.entry((to_n, to_p)).or_insert(0) += 1;
        }
        for &(from_n, from_p, to_n, to_p) in &self.signal_edges {
            let exclusive = out_degree.get(&(from_n, from_p)) == Some(&1)
                && in_degree_port.get(&(to_n, to_p)) == Some(&1);
            let upstream = if exclusive {
                nodes[from_n]
                    .output_port(from_p)
                    .map(|p| p.buffer() as *const FixedBuffer<T, BUF_SIZE>)
            } else {
                None
            };
            if let Some(port) = nodes[to_n].input_port_mut(to_p) {
                port.set_upstream_buffer(upstream);
            }
        }

        // --- feedback buffers ---
        for &(from_n, from_p, to_n, to_p) in &self.feedback_edges {
            if let Some(port) = nodes[from_n].output_port_mut(from_p) {
                port.init_feedback_buffer();
                port.add_feedback_downstream(to_n, to_p);
            }
            if let Some(port) = nodes[to_n].input_port_mut(to_p) {
                port.init_feedback_buffer();
            }
        }
        for &(from_n, from_p, to_n, to_p) in &self.feedback_edges {
            let ptr = nodes[to_n]
                .input_port(to_p)
                .map(|p| p.feedback_buffer_ptr());
            if let Some(port) = nodes[from_n].output_port_mut(from_p) {
                if let Some(p) = ptr {
                    port.add_feedback_ptr(p);
                }
            }
        }

        // Allocate named shared resources (tape loops) and hand out capability
        // handles. The registry is build-time only: nodes keep their handles,
        // which reference-count the resource, so it is dropped after resolution.
        let mut registry = ResourceRegistry::new();
        for r in &self.resources {
            if r.kind == "tape" {
                if let Some(tape) = TapeLoop::<T>::new(r.capacity) {
                    registry.register_tape(&r.name, tape);
                }
            }
        }
        for entry in nodes.iter_mut() {
            entry.resolve_resources(&mut registry);
        }

        let allocated = self.resources.clone();

        // Compute node pointers for branch-chain processing
        let rec_ptrs: Vec<_> = recording_roots
            .iter()
            .map(|&i| &mut nodes[i] as *mut _)
            .collect();

        // Real sink = last Sink node in topological order. Using the actual
        // Sink (rather than `topo.last()`) is robust against extra signal-DAG
        // leaves introduced by feedback-source branches (e.g. an effect chain
        // inside a tape feedback loop terminates on a feedback edge, becoming a
        // second leaf that could otherwise be mistaken for the sink).
        let sink_idx = if has_split_chain {
            topo.iter()
                .rev()
                .copied()
                .find(|&i| matches!(nodes[i], NodeVariant::Sink(_)))
                .or_else(|| topo.last().copied())
        } else {
            None
        };
        let sink_ptr = match sink_idx {
            Some(i) => &mut nodes[i] as *mut _,
            None => std::ptr::null_mut(),
        };

        // Feedback-branch nodes: signal-ancestors of feedback-edge sources that
        // are processed by NEITHER the recording push NOR the playback pull.
        // In split mode the pull only processes nodes upstream of the sink, so
        // side-branch nodes feeding a feedback edge (e.g. effects inside a tape
        // feedback loop) would never run and their `snapshot_feedback` would
        // never fire. They are processed explicitly, in topological order,
        // after the pull.
        let feedback_ptrs: Vec<*mut NodeVariant<T, BUF_SIZE>> =
            if has_split_chain && !self.feedback_edges.is_empty() {
                let mut rev: Vec<Vec<usize>> = vec![Vec::new(); num_nodes];
                for &(f, _, t, _) in &self.signal_edges {
                    rev[t].push(f);
                }
                // Nodes that can reach the sink (already processed by the pull).
                let mut sink_reachable = vec![false; num_nodes];
                if let Some(si) = sink_idx {
                    let mut q = VecDeque::new();
                    q.push_back(si);
                    sink_reachable[si] = true;
                    while let Some(n) = q.pop_front() {
                        for &u in &rev[n] {
                            if !sink_reachable[u] {
                                sink_reachable[u] = true;
                                q.push_back(u);
                            }
                        }
                    }
                }
                // Signal-ancestors (inclusive) of feedback-edge sources.
                let mut in_branch = vec![false; num_nodes];
                let mut q = VecDeque::new();
                for &(from_n, _, _, _) in &self.feedback_edges {
                    if !in_branch[from_n] {
                        in_branch[from_n] = true;
                        q.push_back(from_n);
                    }
                }
                while let Some(n) = q.pop_front() {
                    for &u in &rev[n] {
                        if !in_branch[u] {
                            in_branch[u] = true;
                            q.push_back(u);
                        }
                    }
                }
                topo.iter()
                    .copied()
                    .filter(|&i| in_branch[i] && !sink_reachable[i] && !recording_set.contains(&i))
                    .map(|i| &mut nodes[i] as *mut _)
                    .collect()
            } else {
                Vec::new()
            };

        // Wrap nodes in Rc<UnsafeCell<Vec<>>> — port pointers already valid in this Vec.
        let nodes: Rc<UnsafeCell<Vec<NodeVariant<T, BUF_SIZE>>>> = Rc::new(UnsafeCell::new(nodes));

        let pending_params: PendingParams = Rc::new(RefCell::new(Vec::new()));

        let actor = system.spawn("graph", {
            let n = nodes.clone();
            let pending = pending_params.clone();
            #[allow(unsafe_code)]
            move |msg: CommandEnum| {
                if let CommandEnum::SetParameter(param) = msg {
                    if param.sample_pos.is_some() {
                        // Sample-accurate: defer to the block containing sample_pos.
                        pending.borrow_mut().push(param);
                        return;
                    }
                    let idx = param.port.node_id().inner() as usize;
                    unsafe {
                        let nv = &mut *n.get();
                        if idx < nv.len() {
                            let _ = nv[idx].set_parameter(&param.parameter, param.value);
                        }
                    }
                }
            }
        });

        let actor_ref = actor.actor_ref();

        Ok(Graph {
            nodes,
            topo_order: topo,
            resources: allocated,
            current_tick: ClockTick::new(
                0,
                BUF_SIZE as u32,
                self.sample_rate.unwrap_or(44100.0),
                String::new(),
            ),
            recording_roots: recording_roots.clone(),
            playback_roots: playback_roots.clone(),
            recording_ptrs: rec_ptrs,
            sink_ptr,
            feedback_ptrs,
            actor: Some(actor),
            actor_ref,
            parent_ref: self.parent_ref.clone(),
            system_clock: None,
            pending_params,
        })
    }
}

// ============================================================================
// Graph (Static DAG)
// ============================================================================

/// Shared queue of sample-accurate parameter changes awaiting application.
///
/// Populated by the graph actor handler when a [`SetParameter`] carries a
/// `sample_pos`; drained per processing block by [`apply_due_params`].
type PendingParams = Rc<RefCell<Vec<SetParameter>>>;

/// Apply all pending sample-accurate parameter changes that are due by
/// `chunk_end` (the absolute sample position just past the current block).
///
/// Changes are applied in ascending `sample_pos` order; anything scheduled for
/// a later block stays queued. This is what makes parameter automation land at
/// the right sample position regardless of how the backend batches blocks into
/// I/O callbacks.
#[allow(unsafe_code)]
fn apply_due_params<T: Transcendental, const BUF_SIZE: usize>(
    nodes: &UnsafeCell<Vec<NodeVariant<T, BUF_SIZE>>>,
    pending: &RefCell<Vec<SetParameter>>,
    chunk_end: u64,
) {
    let mut pend = pending.borrow_mut();
    if pend.is_empty() {
        return;
    }
    pend.sort_by_key(|p| p.sample_pos.unwrap_or(0));
    let split = pend.partition_point(|p| p.sample_pos.is_none_or(|sp| sp < chunk_end));
    if split == 0 {
        return;
    }
    unsafe {
        let nv = &mut *nodes.get();
        for p in pend.drain(0..split) {
            let idx = p.port.node_id().inner() as usize;
            if idx < nv.len() {
                let _ = nv[idx].set_parameter(&p.parameter, p.value);
            }
        }
    }
}

/// Owned parts of a [`Graph`] returned by `into_parts` (test only).
#[cfg(test)]
type GraphParts<T, const BUF_SIZE: usize> = (Vec<NodeVariant<T, BUF_SIZE>>, Vec<usize>, ClockTick);

/// Immutable signal graph with static DAG topology.
///
/// Once built the graph cannot be modified. The graph owns no processing
/// logic — it is a pure topology description. Processing is driven by
/// port-level methods (`pre_process`, `snapshot_feedback`, `propagate`)
/// called from external code (e.g. a real-time signal callback or an
/// offline renderer).
pub struct Graph<T: Transcendental, const BUF_SIZE: usize> {
    nodes: Rc<UnsafeCell<Vec<NodeVariant<T, BUF_SIZE>>>>,
    topo_order: Vec<usize>,
    recording_roots: Vec<usize>,
    playback_roots: Vec<usize>,
    recording_ptrs: Vec<*mut NodeVariant<T, BUF_SIZE>>,
    sink_ptr: *mut NodeVariant<T, BUF_SIZE>,
    feedback_ptrs: Vec<*mut NodeVariant<T, BUF_SIZE>>,
    current_tick: ClockTick,
    pub(crate) resources: Vec<GraphResource>,
    actor: Option<Actor<CommandEnum>>,
    actor_ref: ActorRef<CommandEnum>,
    parent_ref: Option<ActorRef<CommandEnum>>,
    /// Optional shared system clock, updated by external sync sources (MIDI, JACK transport).
    /// When set, the I/O callback reads BPM from it and creates `ClockTick::with_tempo`.
    pub system_clock: Option<Arc<SystemClock>>,
    /// Sample-accurate parameter changes awaiting application (shared with the actor handler).
    pending_params: PendingParams,
}

/// Owned processing state extracted from a [`Graph`].
///
/// Holds the parts needed for the I/O callback loop: the actor mailbox
/// for draining `SetParameter` commands, the node array, and routing
/// metadata.  The state is `!Send + !Sync` — it stays on the I/O thread.
///
/// Created via [`Graph::into_processing_state`].
pub struct ProcessingState<T: Transcendental, const BUF_SIZE: usize> {
    actor: Actor<CommandEnum>,
    nodes: Rc<UnsafeCell<Vec<NodeVariant<T, BUF_SIZE>>>>,
    recording_roots: Vec<usize>,
    playback_roots: Vec<usize>,
    recording_ptrs: Vec<*mut NodeVariant<T, BUF_SIZE>>,
    sink_ptr: *mut NodeVariant<T, BUF_SIZE>,
    feedback_ptrs: Vec<*mut NodeVariant<T, BUF_SIZE>>,
    parent_ref: Option<ActorRef<CommandEnum>>,
    system_clock: Option<Arc<SystemClock>>,
    /// Sample rate the graph nodes are currently initialised for.
    ///
    /// The graph has no clock of its own — it runs entirely inside the backend
    /// process callback and adopts the rate carried by each [`ClockTick`]. When
    /// a backend reports a hardware rate that differs from the rate the nodes
    /// were built with (e.g. JACK locked to 48 kHz while the graph was
    /// configured for 44.1 kHz), the nodes are re-initialised on the first tick
    /// so DSP (chip clocks, filter coefficients, …) matches the real rate.
    sample_rate: f32,
    /// Sample-accurate parameter changes awaiting application (shared with the actor handler).
    pending_params: PendingParams,
}

impl<T: Transcendental, const BUF_SIZE: usize> ProcessingState<T, BUF_SIZE> {
    /// Re-initialise every node for a new sample rate.
    ///
    /// Called from [`process_block`](Self::process_block) when the driving
    /// [`ClockTick`] reports a rate different from the one the graph is
    /// currently initialised for.
    #[allow(unsafe_code)]
    fn reinit_sample_rate(&mut self, sample_rate: f32) {
        unsafe {
            let nv = &mut *self.nodes.get();
            for node in nv.iter_mut() {
                node.init(sample_rate);
            }
        }
        self.sample_rate = sample_rate;
    }

    /// Process one block of signal data driven by an external [`ClockTick`].
    ///
    /// Processes all root nodes (recording + playback) — used when there's no
    /// split between input and output streams (single-stream backends).
    #[allow(unsafe_code)]
    pub fn process_block(&mut self, tick: &ClockTick) -> ProcessResult<()> {
        if tick.sample_rate > 0.0 && (tick.sample_rate - self.sample_rate).abs() > 0.5 {
            self.reinit_sample_rate(tick.sample_rate);
        }
        self.actor.drain();
        apply_due_params(
            &self.nodes,
            &self.pending_params,
            tick.sample_pos + tick.samples_since_last as u64,
        );
        let mut ctx = if let Some(ref clock) = self.system_clock {
            RenderContext::with_tempo(
                tick.sample_pos,
                tick.samples_since_last,
                tick.sample_rate,
                clock.bpm() as f32,
            )
        } else {
            RenderContext::new(tick.sample_pos, tick.samples_since_last, tick.sample_rate)
        };
        ctx.speed_ratio = tick.speed_ratio;
        unsafe {
            let nv = &mut *self.nodes.get();
            for &root in self
                .recording_roots
                .iter()
                .chain(self.playback_roots.iter())
            {
                let _ = nv[root].process_block(&ctx, tick);
                for po in 0..nv[root].num_signal_outputs() {
                    if let Some(port) = nv[root].output_port(po) {
                        let _ = port.propagate(&ctx, tick);
                    }
                }
            }
        }
        Ok(())
    }

    /// Send a ClockTick to the parent actor (rack fan-out).
    ///
    /// Called by the backend's process callback at the appropriate time
    /// (once per I/O callback for standard backends, once per DMA buffer
    /// for chunking backends).
    pub fn send_clock_tick(&self, tick: &ClockTick) {
        if tick.is_final {
            if let Some(ref parent) = self.parent_ref {
                parent.send(CommandEnum::ClockTick(tick.clone()));
            }
        }
    }

    /// Wire capture/playback backends into Source/Sink nodes after graph construction.
    ///
    /// Must be called after `into_processing_state()` and before the driver starts.
    /// Only Source nodes respond to `set_capture`; only Sink nodes respond to
    /// `set_playback`.  Processor and Router nodes ignore both.
    #[allow(unsafe_code)]
    pub fn wire_backends(
        &mut self,
        capture: Option<Arc<dyn IoCapture>>,
        playback: Option<Arc<dyn IoPlayback>>,
    ) {
        unsafe {
            let nv = &mut *self.nodes.get();
            for node in nv.iter_mut() {
                if let Some(ref c) = capture {
                    if let NodeVariant::Source(src) = node {
                        src.set_capture(c.clone())
                    }
                }
                if let Some(ref p) = playback {
                    if let NodeVariant::Sink(sink) = node {
                        sink.set_playback(p.clone())
                    }
                }
            }
        }
    }

    /// Run this processing state with a pre-created driver backend.
    ///
    /// Consumes `self`, wires the process callback, enters the I/O loop.
    /// The `running` flag controls shutdown.
    pub fn run_with_driver(
        mut self,
        driver: Arc<dyn IoDriver>,
        running: Arc<AtomicBool>,
    ) -> Result<(), String> {
        self.actor.drain();
        let use_split = !self.recording_roots.is_empty() && !self.playback_roots.is_empty();
        if use_split {
            let mut actor = self.actor;
            let rec_ptrs = self.recording_ptrs;
            let sink = self.sink_ptr;
            let fb_ptrs = self.feedback_ptrs;
            let clock = self.system_clock;
            let parent = self.parent_ref;

            let clock_rec = clock.clone();
            driver.set_input_process_callback(Box::new(move |tick: &ClockTick| {
                actor.drain();
                if let Some(ref c) = clock_rec {
                    let ctx = RenderContext::with_tempo(
                        tick.sample_pos,
                        tick.samples_since_last,
                        tick.sample_rate,
                        c.bpm() as f32,
                    );
                    p_forward(&rec_ptrs, &ctx, tick);
                } else {
                    let ctx = RenderContext::new(
                        tick.sample_pos,
                        tick.samples_since_last,
                        tick.sample_rate,
                    );
                    p_forward(&rec_ptrs, &ctx, tick);
                }
            }));
            driver.set_process_callback(Box::new(move |tick: &ClockTick| {
                if let Some(ref c) = clock {
                    let mut ctx = RenderContext::with_tempo(
                        tick.sample_pos,
                        tick.samples_since_last,
                        tick.sample_rate,
                        c.bpm() as f32,
                    );
                    ctx.speed_ratio = tick.speed_ratio;
                    p_pull(sink, &ctx, tick);
                    p_process_branch(&fb_ptrs, &ctx, tick);
                } else {
                    let mut ctx = RenderContext::new(
                        tick.sample_pos,
                        tick.samples_since_last,
                        tick.sample_rate,
                    );
                    ctx.speed_ratio = tick.speed_ratio;
                    p_pull(sink, &ctx, tick);
                    p_process_branch(&fb_ptrs, &ctx, tick);
                }
                if tick.is_final {
                    if let Some(ref p) = parent {
                        p.send(CommandEnum::ClockTick(tick.clone()));
                    }
                }
            }));
            driver.run(running.clone())?;
        } else {
            driver.set_process_callback(Box::new(move |tick: &ClockTick| {
                let _ = self.process_block(tick);
                self.send_clock_tick(tick);
            }));
            driver.run(running.clone())?;
        }
        while running.load(std::sync::atomic::Ordering::Acquire) {
            std::thread::park();
        }
        let _ = driver.stop();
        Ok(())
    }
}

// ============================================================================
// Pointer-based chain processing (used by split-chain run_with_driver closures)
// ============================================================================

/// Forward propagate from recording roots.
#[allow(unsafe_code)]
fn p_forward<T: Transcendental, const BUF_SIZE: usize>(
    roots: &[*mut NodeVariant<T, BUF_SIZE>],
    ctx: &RenderContext,
    tick: &ClockTick,
) {
    for &root in roots {
        unsafe {
            let nv = &mut *root;
            let _ = nv.process_block(ctx, tick);
            for po in 0..nv.num_signal_outputs() {
                if let Some(port) = nv.output_port(po) {
                    let _ = port.propagate(ctx, tick);
                }
            }
        }
    }
}

/// Pull-model playback chain: start from sink, recursively process upstream.
#[allow(unsafe_code)]
fn p_pull<T: Transcendental, const BUF_SIZE: usize>(
    sink: *mut NodeVariant<T, BUF_SIZE>,
    ctx: &RenderContext,
    tick: &ClockTick,
) {
    if sink.is_null() {
        return;
    }
    unsafe {
        p_pull_recurse(&mut *sink, ctx, tick);
    }
}

#[allow(unsafe_code)]
fn p_pull_recurse<T: Transcendental, const BUF_SIZE: usize>(
    node: &mut NodeVariant<T, BUF_SIZE>,
    ctx: &RenderContext,
    tick: &ClockTick,
) {
    for pi in 0..node.num_signal_inputs() {
        if let Some(p) = node.input_port_mut(pi) {
            p.pre_process();
        }
    }
    for pi in 0..node.num_signal_inputs() {
        if let Some(p) = node.input_port(pi) {
            let src = p.upstream_node();
            if !src.is_null() {
                unsafe {
                    p_pull_recurse(&mut *src, ctx, tick);
                }
            }
        }
    }
    let _ = node.process_block(ctx, tick);
    for po in 0..node.num_signal_outputs() {
        if let Some(p) = node.output_port_mut(po) {
            p.snapshot_feedback();
        }
    }
    for po in 0..node.num_signal_outputs() {
        if let Some(port) = node.output_port(po) {
            let buf = port.buffer();
            for &in_ptr in port.downstream_input_ptrs() {
                unsafe {
                    let ip = &mut *in_ptr;
                    if !ip.is_zero_copy() {
                        let _ = ip.run_action(Some(buf.as_array()));
                    }
                    ip.set_data_received(true);
                }
            }
        }
    }
}

/// Process a topologically-ordered list of feedback-branch nodes.
///
/// These are side-branch nodes (e.g. effects inside a tape feedback loop) that
/// feed a feedback edge but do not reach the sink, so the playback pull never
/// processes them. Their inputs were already filled by the pull (their upstream
/// producers are on the sink path); here each is processed in order so its
/// output is produced and `snapshot_feedback` captures it into the downstream
/// feedback buffer for the next block.
#[allow(unsafe_code)]
fn p_process_branch<T: Transcendental, const BUF_SIZE: usize>(
    branch: &[*mut NodeVariant<T, BUF_SIZE>],
    ctx: &RenderContext,
    tick: &ClockTick,
) {
    for &np in branch {
        unsafe {
            let node = &mut *np;
            for pi in 0..node.num_signal_inputs() {
                if let Some(p) = node.input_port_mut(pi) {
                    p.pre_process();
                }
            }
            let _ = node.process_block(ctx, tick);
            for po in 0..node.num_signal_outputs() {
                if let Some(p) = node.output_port_mut(po) {
                    p.snapshot_feedback();
                }
            }
            for po in 0..node.num_signal_outputs() {
                if let Some(port) = node.output_port(po) {
                    let buf = port.buffer();
                    for &in_ptr in port.downstream_input_ptrs() {
                        let ip = &mut *in_ptr;
                        if !ip.is_zero_copy() {
                            let _ = ip.run_action(Some(buf.as_array()));
                        }
                        ip.set_data_received(true);
                    }
                }
            }
        }
    }
}

impl<T: Transcendental, const BUF_SIZE: usize> Graph<T, BUF_SIZE> {
    // ========================================================================
    // Accessors
    // ========================================================================

    /// Borrow the node array (read-only).
    #[allow(unsafe_code)]
    pub fn nodes(&self) -> &[NodeVariant<T, BUF_SIZE>] {
        unsafe { &*self.nodes.get() }
    }

    /// Return the current clock tick.
    pub fn current_tick(&self) -> ClockTick {
        self.current_tick.clone()
    }

    /// Return the number of nodes in the graph.
    #[allow(unsafe_code)]
    pub fn node_count(&self) -> usize {
        unsafe { (*self.nodes.get()).len() }
    }

    /// Return the topological ordering of node indices.
    pub fn topo_order(&self) -> &[usize] {
        &self.topo_order
    }

    #[allow(dead_code)]
    pub(crate) fn sample_rate(&self) -> f32 {
        self.current_tick.sample_rate
    }

    /// Access the named resources (tape loops, etc.) allocated for this graph.
    #[allow(dead_code)]
    pub fn resources(&self) -> &[GraphResource] {
        &self.resources
    }

    /// Process one block of signal data driven by an external [`ClockTick`].
    ///
    /// Called from the backend's process callback. Performs:
    ///
    /// 1. Drains the graph's actor mailbox (applies queued `SetParameter`s).
    /// 2. Creates a [`RenderContext`] from the tick.
    /// 3. Calls `process_block` on each root node and recursively
    ///    propagates through the DAG via [`Port::propagate`].
    ///
    /// The graph is `!Send + !Sync` — it stays on the I/O callback thread.
    #[allow(unsafe_code)]
    pub fn process_block(&mut self, tick: &ClockTick) -> ProcessResult<()> {
        if let Some(ref mut actor) = self.actor {
            actor.drain();
        }
        apply_due_params(
            &self.nodes,
            &self.pending_params,
            tick.sample_pos + tick.samples_since_last as u64,
        );
        let ctx = if let Some(ref clock) = self.system_clock {
            RenderContext::with_tempo(
                tick.sample_pos,
                tick.samples_since_last,
                tick.sample_rate,
                clock.bpm() as f32,
            )
        } else {
            RenderContext::new(tick.sample_pos, tick.samples_since_last, tick.sample_rate)
        };
        self.current_tick = tick.clone();
        unsafe {
            let nv = &mut *self.nodes.get();
            for &root in self
                .recording_roots
                .iter()
                .chain(self.playback_roots.iter())
            {
                let _ = nv[root].process_block(&ctx, tick);
                for po in 0..nv[root].num_signal_outputs() {
                    if let Some(port) = nv[root].output_port(po) {
                        let _ = port.propagate(&ctx, tick);
                    }
                }
            }
        }
        Ok(())
    }

    /// Consume the graph and return a [`ProcessingState`] that owns all
    /// parts needed for the I/O callback loop.
    ///
    /// `ProcessingState` is `!Send + !Sync` — it stays on the I/O thread
    /// and is moved into the backend's process callback closure.
    pub fn into_processing_state(mut self) -> ProcessingState<T, BUF_SIZE> {
        let actor = self.actor.take().expect("graph actor missing");
        ProcessingState {
            actor,
            nodes: self.nodes,
            recording_roots: self.recording_roots,
            playback_roots: self.playback_roots,
            recording_ptrs: self.recording_ptrs,
            sink_ptr: self.sink_ptr,
            feedback_ptrs: self.feedback_ptrs,
            parent_ref: self.parent_ref,
            system_clock: self.system_clock,
            sample_rate: self.current_tick.sample_rate,
            pending_params: self.pending_params,
        }
    }

    /// Obtain an [`ActorRef`] for sending commands to this graph.
    pub fn handle(&self) -> ActorRef<CommandEnum> {
        self.actor_ref.clone()
    }

    /// Consume the graph and return its owned parts (test only).
    #[cfg(test)]
    pub fn into_parts(self) -> GraphParts<T, BUF_SIZE> {
        let Self {
            nodes,
            topo_order,
            current_tick,
            resources: _,
            recording_roots: _,
            playback_roots: _,
            recording_ptrs: _,
            sink_ptr: _,
            feedback_ptrs: _,
            actor,
            actor_ref: _,
            parent_ref: _,
            system_clock: _,
            pending_params: _,
        } = self;
        drop(actor);
        let nodes = Rc::try_unwrap(nodes).unwrap().into_inner();
        (nodes, topo_order, current_tick)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rill_core::math::Transcendental;
    use rill_core::time::RenderContext;

    use rill_core::traits::{
        Node, NodeCategory, NodeId, NodeMetadata, NodeState, ParamValue, ParameterId, Port,
        ProcessResult, Processor, Sink, Source,
    };
    use rill_core_actor::ActorSystem;
    use std::sync::Arc;

    fn test_system() -> ActorSystem {
        ActorSystem::new()
    }

    fn test_factory<const B: usize>() -> Arc<NodeFactory<f32, B>> {
        let mut f = NodeFactory::<f32, B>::new();

        f.register_fn("test/const", |id, params| {
            let value = params.get_f32("value", 1.0);
            let mut node = ConstantSource::<f32, B>::new(id, value, params.sample_rate);
            node.init(params.sample_rate);
            NodeVariant::Source(Box::new(node))
        });

        f.register_fn("test/gain", |id, params| {
            let gain = params.get_f32("gain", 1.0);
            let mut node = GainProcessor::<f32, B>::new(id, params.sample_rate, gain);
            node.init(params.sample_rate);
            NodeVariant::Processor(Box::new(node))
        });

        f.register_fn("test/capture", |id, params| {
            let mut node = CaptureSink::<f32, B>::new(id, params.sample_rate);
            node.init(params.sample_rate);
            NodeVariant::Sink(Box::new(node))
        });

        Arc::new(f)
    }

    fn test_builder<const B: usize>(factory: &Arc<NodeFactory<f32, B>>) -> GraphBuilder<f32, B> {
        GraphBuilder::new(factory.clone())
    }

    fn test_params(sample_rate: f32) -> Params {
        let mut p = Params::new(sample_rate);
        p.insert("value".to_string(), ParamValue::Float(sample_rate));
        p
    }

    // ------------------------------------------------------------------------
    // Test node implementations
    // ------------------------------------------------------------------------

    pub(crate) struct ConstantSource<T: Transcendental, const B: usize> {
        id: NodeId,
        value: T,
        state: NodeState<T, B>,
        output: Port<T, B>,
    }

    impl<T: Transcendental, const B: usize> ConstantSource<T, B> {
        pub fn new(id: NodeId, value: T, sample_rate: f32) -> Self {
            let state = NodeState::new(sample_rate);
            let output = Port::output(id, 0, "out");
            Self {
                id,
                value,
                state,
                output,
            }
        }
    }

    impl<T: Transcendental, const B: usize> Node<T, B> for ConstantSource<T, B> {
        fn id(&self) -> NodeId {
            self.id
        }
        fn set_id(&mut self, id: NodeId) {
            self.id = id;
        }
        fn metadata(&self) -> NodeMetadata {
            NodeMetadata {
                name: "ConstantSource".into(),
                type_name: Some("test/const".into()),
                category: NodeCategory::Source,
                description: String::new(),
                author: String::new(),
                version: String::new(),
                parameters: vec![],
                signal_inputs: 0,
                signal_outputs: 1,
                control_inputs: 0,
                control_outputs: 0,
                clock_inputs: 0,
                clock_outputs: 0,
                feedback_ports: 0,
            }
        }
        fn init(&mut self, _: f32) {}
        fn reset(&mut self) {}
        fn get_parameter(&self, _: &ParameterId) -> Option<ParamValue> {
            None
        }
        fn set_parameter(&mut self, _: &ParameterId, _: ParamValue) -> ProcessResult<()> {
            Ok(())
        }
        fn control_port(&self, _: usize) -> Option<&Port<T, B>> {
            None
        }
        fn control_port_mut(&mut self, _: usize) -> Option<&mut Port<T, B>> {
            None
        }
        fn output_port(&self, i: usize) -> Option<&Port<T, B>> {
            if i == 0 {
                Some(&self.output)
            } else {
                None
            }
        }
        fn output_port_mut(&mut self, i: usize) -> Option<&mut Port<T, B>> {
            if i == 0 {
                Some(&mut self.output)
            } else {
                None
            }
        }
        fn num_signal_outputs(&self) -> usize {
            1
        }
        fn input_port(&self, _: usize) -> Option<&Port<T, B>> {
            None
        }
        fn input_port_mut(&mut self, _: usize) -> Option<&mut Port<T, B>> {
            None
        }
        fn state(&self) -> &NodeState<T, B> {
            &self.state
        }
        fn state_mut(&mut self) -> &mut NodeState<T, B> {
            &mut self.state
        }
    }

    impl<T: Transcendental, const B: usize> Source<T, B> for ConstantSource<T, B> {
        fn generate(
            &mut self,
            _: &RenderContext,
            _: &[T],
            _: &[RenderContext],
            _: &ClockTick,
        ) -> ProcessResult<()> {
            self.output.write().fill(self.value);
            Ok(())
        }
    }

    // ------------------------------------------------------------------------
    // GainProcessor
    // ------------------------------------------------------------------------

    pub(crate) struct GainProcessor<T: Transcendental, const B: usize> {
        id: NodeId,
        gain: T,
        state: NodeState<T, B>,
        input: Port<T, B>,
        output: Port<T, B>,
    }

    impl<T: Transcendental, const B: usize> GainProcessor<T, B> {
        pub fn new(id: NodeId, sample_rate: f32, gain: T) -> Self {
            let state = NodeState::new(sample_rate);
            let input = Port::input(id, 0, "in");
            let output = Port::output(id, 0, "out");
            Self {
                id,
                gain,
                state,
                input,
                output,
            }
        }
    }

    impl<T: Transcendental, const B: usize> Node<T, B> for GainProcessor<T, B> {
        fn id(&self) -> NodeId {
            self.id
        }
        fn set_id(&mut self, id: NodeId) {
            self.id = id;
        }
        fn metadata(&self) -> NodeMetadata {
            NodeMetadata {
                name: "GainProcessor".into(),
                type_name: Some("test/gain".into()),
                category: NodeCategory::Processor,
                description: String::new(),
                author: String::new(),
                version: String::new(),
                parameters: vec![],
                signal_inputs: 1,
                signal_outputs: 1,
                control_inputs: 0,
                control_outputs: 0,
                clock_inputs: 0,
                clock_outputs: 0,
                feedback_ports: 0,
            }
        }
        fn init(&mut self, _: f32) {}
        fn reset(&mut self) {}
        fn get_parameter(&self, _: &ParameterId) -> Option<ParamValue> {
            None
        }
        fn set_parameter(&mut self, _: &ParameterId, _: ParamValue) -> ProcessResult<()> {
            Ok(())
        }
        fn control_port(&self, _: usize) -> Option<&Port<T, B>> {
            None
        }
        fn control_port_mut(&mut self, _: usize) -> Option<&mut Port<T, B>> {
            None
        }
        fn input_port(&self, i: usize) -> Option<&Port<T, B>> {
            if i == 0 {
                Some(&self.input)
            } else {
                None
            }
        }
        fn input_port_mut(&mut self, i: usize) -> Option<&mut Port<T, B>> {
            if i == 0 {
                Some(&mut self.input)
            } else {
                None
            }
        }
        fn num_signal_outputs(&self) -> usize {
            1
        }
        fn num_signal_inputs(&self) -> usize {
            1
        }
        fn output_port(&self, i: usize) -> Option<&Port<T, B>> {
            if i == 0 {
                Some(&self.output)
            } else {
                None
            }
        }
        fn output_port_mut(&mut self, i: usize) -> Option<&mut Port<T, B>> {
            if i == 0 {
                Some(&mut self.output)
            } else {
                None
            }
        }
        fn state(&self) -> &NodeState<T, B> {
            &self.state
        }
        fn state_mut(&mut self) -> &mut NodeState<T, B> {
            &mut self.state
        }
    }

    impl<T: Transcendental, const B: usize> Processor<T, B> for GainProcessor<T, B> {
        fn process(
            &mut self,
            _: &RenderContext,
            _: &[&[T; B]],
            _: &[T],
            _: &[RenderContext],
            _: &[&[T; B]],
        ) -> ProcessResult<()> {
            let src = self.input.read();
            let buf = self.output.write();
            for i in 0..B {
                buf[i] = src[i] * self.gain;
            }
            Ok(())
        }
    }

    // ------------------------------------------------------------------------
    // CaptureSink — captures first sample of each block
    // ------------------------------------------------------------------------

    pub(crate) struct CaptureSink<T: Transcendental, const B: usize> {
        id: NodeId,
        state: NodeState<T, B>,
        input: Port<T, B>,
    }

    impl<T: Transcendental, const B: usize> CaptureSink<T, B> {
        pub fn new(id: NodeId, sample_rate: f32) -> Self {
            let state = NodeState::new(sample_rate);
            let input = Port::input(id, 0, "in");
            Self { id, state, input }
        }
    }

    impl<T: Transcendental, const B: usize> Node<T, B> for CaptureSink<T, B> {
        fn id(&self) -> NodeId {
            self.id
        }
        fn set_id(&mut self, id: NodeId) {
            self.id = id;
        }
        fn metadata(&self) -> NodeMetadata {
            NodeMetadata {
                name: "CaptureSink".into(),
                type_name: Some("test/capture".into()),
                category: NodeCategory::Sink,
                description: String::new(),
                author: String::new(),
                version: String::new(),
                parameters: vec![],
                signal_inputs: 1,
                signal_outputs: 0,
                control_inputs: 0,
                control_outputs: 0,
                clock_inputs: 0,
                clock_outputs: 0,
                feedback_ports: 0,
            }
        }
        fn init(&mut self, _: f32) {}
        fn reset(&mut self) {}
        fn get_parameter(&self, _: &ParameterId) -> Option<ParamValue> {
            None
        }
        fn set_parameter(&mut self, _: &ParameterId, _: ParamValue) -> ProcessResult<()> {
            Ok(())
        }
        fn control_port(&self, _: usize) -> Option<&Port<T, B>> {
            None
        }
        fn control_port_mut(&mut self, _: usize) -> Option<&mut Port<T, B>> {
            None
        }
        fn output_port(&self, _: usize) -> Option<&Port<T, B>> {
            None
        }
        fn output_port_mut(&mut self, _: usize) -> Option<&mut Port<T, B>> {
            None
        }
        fn input_port(&self, i: usize) -> Option<&Port<T, B>> {
            if i == 0 {
                Some(&self.input)
            } else {
                None
            }
        }
        fn input_port_mut(&mut self, i: usize) -> Option<&mut Port<T, B>> {
            if i == 0 {
                Some(&mut self.input)
            } else {
                None
            }
        }
        fn num_signal_inputs(&self) -> usize {
            1
        }
        fn state(&self) -> &NodeState<T, B> {
            &self.state
        }
        fn state_mut(&mut self) -> &mut NodeState<T, B> {
            &mut self.state
        }
    }

    impl<T: Transcendental, const B: usize> Sink<T, B> for CaptureSink<T, B> {
        fn consume(
            &mut self,
            _: &RenderContext,
            _: &[&[T; B]],
            _: &[T],
            _: &[RenderContext],
            _: &[&[T; B]],
            _: &ClockTick,
        ) -> ProcessResult<()> {
            Ok(())
        }
    }

    // ------------------------------------------------------------------------
    // Graph signal flow tests
    // ------------------------------------------------------------------------

    const BUF: usize = 64;

    #[test]
    fn test_fanout_branches_are_independent_not_zero_copy() {
        // One source output feeding two consumers is a fan-out: each branch
        // must own an independent buffer so downstream processing is isolated.
        let factory = test_factory::<BUF>();
        let mut builder = test_builder::<BUF>(&factory);
        let system = test_system();

        let src = builder.add_node("test/const", &test_params(44100.0));
        let a = builder.add_node("test/gain", &test_params(44100.0));
        let b = builder.add_node("test/gain", &test_params(44100.0));
        builder.connect_signal(src, 0, a, 0);
        builder.connect_signal(src, 0, b, 0);

        let graph = builder.build(&system).unwrap();
        let nodes = graph.nodes();
        assert!(
            !nodes[a].input_port(0).unwrap().is_zero_copy(),
            "fan-out branch A must not alias the shared source buffer"
        );
        assert!(
            !nodes[b].input_port(0).unwrap().is_zero_copy(),
            "fan-out branch B must not alias the shared source buffer"
        );
        assert!(!nodes[a].input_port(0).unwrap().has_upstream_buffer());
        assert!(!nodes[b].input_port(0).unwrap().has_upstream_buffer());
    }

    #[test]
    fn test_linear_chain_edge_is_zero_copy() {
        // An exclusive 1:1 edge is safe to alias (single consumer).
        let factory = test_factory::<BUF>();
        let mut builder = test_builder::<BUF>(&factory);
        let system = test_system();

        let src = builder.add_node("test/const", &test_params(44100.0));
        let g = builder.add_node("test/gain", &test_params(44100.0));
        builder.connect_signal(src, 0, g, 0);

        let graph = builder.build(&system).unwrap();
        let nodes = graph.nodes();
        assert!(
            nodes[g].input_port(0).unwrap().is_zero_copy(),
            "exclusive 1:1 edge should be zero-copy"
        );
    }

    #[test]
    #[allow(unsafe_code)]
    fn test_graph_source_to_sink() {
        let factory = test_factory::<BUF>();
        let mut builder = test_builder::<BUF>(&factory);
        let system = test_system();

        let src_idx = builder.add_node("test/const", &test_params(44100.0));
        let snk_idx = builder.add_node("test/capture", &test_params(44100.0));
        builder.connect_signal(src_idx, 0, snk_idx, 0);

        let graph = builder.build(&system).unwrap();
        let source_idx = graph
            .recording_roots
            .first()
            .or(graph.playback_roots.first())
            .copied()
            .unwrap_or(0);

        let ctx = RenderContext::new(0, BUF as u32, 44100.0);
        let tick = ClockTick::new(0, BUF as u32, 44100.0, String::new());
        let nodes = graph.nodes.clone();
        unsafe {
            let nv = &mut *nodes.get();
            nv[source_idx].process_block(&ctx, &tick).unwrap();
            if let Some(port) = nv[source_idx].output_port(0) {
                port.propagate(&ctx, &tick).unwrap();
            }
        }
        unsafe {
            let nv = &*nodes.get();
            let val = nv[snk_idx]
                .input_port(0)
                .unwrap()
                .signal_buffer()
                .as_array()[0];
            assert!(val != 0.0, "signal should have propagated, got {}", val);
        }
    }

    #[test]
    #[allow(unsafe_code)]
    fn test_graph_source_proc_sink() {
        let factory = test_factory::<BUF>();
        let mut builder = test_builder::<BUF>(&factory);
        let system = test_system();

        let mut params = test_params(44100.0);
        params.insert("value".to_string(), ParamValue::Float(5.0));
        let src_idx = builder.add_node("test/const", &params);

        let mut gain_params = test_params(44100.0);
        gain_params.insert("gain".to_string(), ParamValue::Float(3.0));
        let proc_idx = builder.add_node("test/gain", &gain_params);

        let snk_idx = builder.add_node("test/capture", &test_params(44100.0));

        builder.connect_signal(src_idx, 0, proc_idx, 0);
        builder.connect_signal(proc_idx, 0, snk_idx, 0);

        let graph = builder.build(&system).unwrap();
        let source_idx = graph
            .recording_roots
            .first()
            .or(graph.playback_roots.first())
            .copied()
            .unwrap_or(0);

        eprintln!("topo: {:?}", graph.topo_order);
        eprintln!("source_idx: {source_idx}, src_idx: {src_idx}, proc_idx: {proc_idx}, snk_idx: {snk_idx}");

        let ctx = RenderContext::new(0, BUF as u32, 44100.0);
        let tick = ClockTick::new(0, BUF as u32, 44100.0, String::new());
        let nodes = graph.nodes.clone();
        unsafe {
            let nv = &mut *nodes.get();
            eprintln!(
                "node types: src={:?}, proc={:?}, snk={:?}",
                std::mem::discriminant(&nv[0]),
                std::mem::discriminant(&nv[1]),
                std::mem::discriminant(&nv[2]),
            );

            let _ = nv[source_idx].process_block(&ctx, &tick);
            let src_val = nv[source_idx].output_port(0).unwrap().read()[0];
            eprintln!("source output: {src_val}");

            let out_port = nv[source_idx].output_port(0).unwrap();
            eprintln!(
                "source output port downstream_nodes: {}",
                out_port.downstream_nodes().len()
            );
            eprintln!(
                "source output port downstream_input_ptrs: {}",
                out_port.downstream_input_ptrs().len()
            );

            // Check processor output port connections BEFORE propagate
            {
                let proc_port = nv[proc_idx].output_port(0).unwrap();
                eprintln!(
                    "PROC OUT port downstream_nodes: {}",
                    proc_port.downstream_nodes().len()
                );
                eprintln!(
                    "PROC OUT port downstream_input_ptrs: {}",
                    proc_port.downstream_input_ptrs().len()
                );
                for (i, &dn) in proc_port.downstream().iter().enumerate() {
                    eprintln!("  downstream[{}]: (node={}, port={})", i, dn.0, dn.1);
                }
            }

            // --- BUFFER ADDRESS DEBUG ---
            let src_out = nv[source_idx].output_port(0).unwrap();
            let proc_in = nv[proc_idx].input_port(0).unwrap();
            let proc_out = nv[proc_idx].output_port(0).unwrap();
            let snk_in = nv[snk_idx].input_port(0).unwrap();
            eprintln!("BUFFER ADDRESSES:");
            eprintln!("  src output buf:  {:p}", src_out.read().as_ptr());
            eprintln!("  proc input buf:  {:p}", proc_in.read().as_ptr());
            eprintln!("  proc output buf: {:p}", proc_out.read().as_ptr());
            eprintln!("  snk input buf:   {:p}", snk_in.read().as_ptr());
            eprintln!(
                "  proc_in.has_upstream_buffer(): {}",
                proc_in.has_upstream_buffer()
            );
            eprintln!(
                "  snk_in.has_upstream_buffer(): {}",
                snk_in.has_upstream_buffer()
            );
            // --- END DEBUG ---

            out_port.propagate(&ctx, &tick).unwrap();

            // --- AFTER PROPAGATE: debug buffer values ---
            {
                let nv = &*nodes.get();
                let snk_in = nv[snk_idx].input_port(0).unwrap();
                eprintln!("AFTER propagate - snk input buf[0]: {}", snk_in.read()[0]);
            }

            let sink_buf = nv[snk_idx]
                .input_port(0)
                .unwrap()
                .signal_buffer()
                .as_array();
            eprintln!("SINK input port buffer first sample: {}", sink_buf[0]);

            // Check processor output port propagation
            let proc_out_port = nv[proc_idx].output_port(0).unwrap();
            eprintln!(
                "proc output port downstream_nodes: {}",
                proc_out_port.downstream_nodes().len()
            );
            eprintln!(
                "proc output port downstream_input_ptrs: {}",
                proc_out_port.downstream_input_ptrs().len()
            );

            // Sink
            let sink_val = nv[snk_idx]
                .input_port(0)
                .unwrap()
                .signal_buffer()
                .as_array()[0];
            eprintln!("sink input AFTER propagate: {sink_val}");

            assert!(
                (sink_val - 15.0).abs() < 1e-4,
                "expected 15.0, got {}",
                sink_val
            );
        }
    }

    /// A feedback-branch node (feeds a feedback edge but does not reach the
    /// sink) must be processed every block in split mode via `p_process_branch`.
    /// Before the fix these side-branch nodes were never run, so their
    /// `snapshot_feedback` never fired and feedback loops stayed silent.
    #[test]
    #[allow(unsafe_code)]
    fn test_split_processes_feedback_branch() {
        let mut f = NodeFactory::<f32, BUF>::new();
        f.register_fn("rill/input", |id, params| {
            let mut n = ConstantSource::<f32, BUF>::new(id, 0.0, params.sample_rate);
            n.init(params.sample_rate);
            NodeVariant::Source(Box::new(n))
        });
        f.register_fn("test/const", |id, params| {
            let v = params.get_f32("value", 1.0);
            let mut n = ConstantSource::<f32, BUF>::new(id, v, params.sample_rate);
            n.init(params.sample_rate);
            NodeVariant::Source(Box::new(n))
        });
        f.register_fn("test/gain", |id, params| {
            let g = params.get_f32("gain", 1.0);
            let mut n = GainProcessor::<f32, BUF>::new(id, params.sample_rate, g);
            n.init(params.sample_rate);
            NodeVariant::Processor(Box::new(n))
        });
        f.register_fn("test/capture", |id, params| {
            let mut n = CaptureSink::<f32, BUF>::new(id, params.sample_rate);
            n.init(params.sample_rate);
            NodeVariant::Sink(Box::new(n))
        });
        let factory = Arc::new(f);
        let mut builder = test_builder::<BUF>(&factory);
        let system = test_system();

        let rec_in = builder.add_node("rill/input", &test_params(44100.0)); // 0 (recording root)
        let mut cparams = test_params(44100.0);
        cparams.insert("value", ParamValue::Float(2.0));
        let play = builder.add_node("test/const", &cparams); // 1 (playback root)
        let sink = builder.add_node("test/capture", &test_params(44100.0)); // 2
        let branch = builder.add_node("test/gain", &test_params(44100.0)); // 3 (feedback branch)
        let rec_proc = builder.add_node("test/gain", &test_params(44100.0)); // 4 (recording)

        builder.connect_signal(play, 0, sink, 0); // playback: const -> sink
        builder.connect_signal(play, 0, branch, 0); // branch: const -> gain(branch)
        builder.connect_signal(rec_in, 0, rec_proc, 0); // recording: input -> gain
        builder.connect_feedback(branch, 0, rec_proc, 0); // branch -> recording (feedback)

        let graph = builder.build(&system).unwrap();
        assert_eq!(
            graph.feedback_ptrs.len(),
            1,
            "the feedback-branch node must be detected at build time"
        );

        let ctx = RenderContext::new(0, BUF as u32, 44100.0);
        for i in 0..3u64 {
            let tick = ClockTick::new(i * BUF as u64, BUF as u32, 44100.0, String::new());
            p_forward(&graph.recording_ptrs, &ctx, &tick);
            p_pull(graph.sink_ptr, &ctx, &tick);
            p_process_branch(&graph.feedback_ptrs, &ctx, &tick);
        }

        unsafe {
            let nv = &*graph.nodes.get();
            let branch_out = nv[branch].output_port(0).unwrap().read()[0];
            assert!(
                (branch_out - 2.0).abs() < 1e-4,
                "feedback-branch node was not processed (out={branch_out}, expected 2.0)"
            );
        }
    }
}