hirn-engine 0.1.0

Engine for the hirn cognitive memory database
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
//! LanceDB-backed persistent graph engine.
//!
//! Replaces the in-memory `PropertyGraph` (petgraph) with a persistent
//! graph stored in LanceDB datasets. The graph survives process restarts,
//! scales beyond RAM, and supports indexed adjacency queries.
//!
//! # Datasets
//!
//! - `graph_nodes` — one row per graph node (memory)
//! - `graph_edges` — one row per directed edge

use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use arrow_array::Array;
use futures::TryStreamExt;
use hirn_core::HirnResult;
use hirn_core::id::MemoryId;
use hirn_core::metadata::Metadata;
use hirn_core::timestamp::Timestamp;
use hirn_core::types::{EdgeRelation, Layer, Namespace};
use hirn_graph::graph::{
    EdgeId, GraphEdge, GraphNodeData, MAX_EDGES_PER_NODE, validate_edge_metadata,
};

use hirn_storage::PhysicalStore;
use hirn_storage::datasets::graph::{self, DATASET_EDGES_NAME, DATASET_NODES_NAME};
use hirn_storage::store::{ExactMatchFilter, IndexConfig, IndexType, ScanOptions};

/// Result of a batch BFS traversal.
///
/// Contains the edges discovered at each depth level and the full set of
/// visited node IDs (including the start nodes).
#[derive(Debug, Clone)]
pub struct BfsResult {
    /// Edges at each depth level. `depths[0]` = edges from start nodes,
    /// `depths[1]` = edges from depth-1 targets, etc.
    pub depths: Vec<Vec<GraphEdge>>,
    /// All node IDs visited during the BFS (including start nodes).
    pub visited: Vec<MemoryId>,
}

impl BfsResult {
    /// Collect all unique target node IDs across all depths.
    pub fn all_targets(&self) -> Vec<MemoryId> {
        use std::collections::HashSet;
        let mut seen = HashSet::new();
        let mut targets = Vec::new();
        for depth_edges in &self.depths {
            for edge in depth_edges {
                if seen.insert(edge.target) {
                    targets.push(edge.target);
                }
            }
        }
        targets
    }

    /// Total number of edges across all depths.
    pub fn total_edges(&self) -> usize {
        self.depths.iter().map(Vec::len).sum()
    }
}

/// A single row from [`PersistentGraph::deep_causal_bfs`].
///
/// Each row represents one edge in a causal chain, tagged with chain_id,
/// depth, and a per-chain composite score.
#[derive(Debug, Clone)]
pub struct CausalBfsRow {
    pub chain_id: String,
    pub source_id: MemoryId,
    pub target_id: MemoryId,
    pub strength: f32,
    pub confidence: f32,
    pub evidence_count: u32,
    pub mechanism: Option<String>,
    pub depth: u32,
    pub chain_score: f32,
}

/// Internal edge data accumulated during DFS over BFS results.
#[derive(Debug, Clone)]
struct CausalBfsEdge {
    source: MemoryId,
    target: MemoryId,
    strength: f32,
    confidence: f32,
    evidence_count: u32,
    mechanism: Option<String>,
}

/// Convert a chain of edges into [`CausalBfsRow`]s and append to `rows`.
fn emit_causal_rows(
    chain_edges: &[CausalBfsEdge],
    rows: &mut Vec<CausalBfsRow>,
    chain_counter: &mut u32,
) {
    let chain_id = format!("chain_{}", *chain_counter);
    *chain_counter += 1;

    // Chain score = Σ(strength × confidence × ln(1 + evidence)) / chain_length.
    let score_sum: f32 = chain_edges
        .iter()
        .map(|e| e.strength * e.confidence * (1.0_f32 + e.evidence_count as f32).ln())
        .sum();
    let chain_score = score_sum / chain_edges.len().max(1) as f32;

    for (depth, edge) in chain_edges.iter().enumerate() {
        rows.push(CausalBfsRow {
            chain_id: chain_id.clone(),
            source_id: edge.source,
            target_id: edge.target,
            strength: edge.strength,
            confidence: edge.confidence,
            evidence_count: edge.evidence_count,
            mechanism: edge.mechanism.clone(),
            depth: depth as u32,
            chain_score,
        });
    }
}

/// Persistent graph backed by LanceDB datasets.
///
/// All operations go through the `PhysicalStore` trait, so the graph
/// works identically with local Lance files or remote S3-backed brains.
pub struct PersistentGraph {
    storage: Arc<dyn PhysicalStore>,
}

impl PersistentGraph {
    fn layer_exact_filter(layer: Layer) -> ExactMatchFilter {
        ExactMatchFilter::utf8_value("layer", layer_to_str(layer))
    }

    fn namespace_exact_filter(namespace: &Namespace) -> ExactMatchFilter {
        ExactMatchFilter::utf8_value("namespace", namespace.as_str())
    }

    fn source_exact_filter(source: MemoryId) -> ExactMatchFilter {
        ExactMatchFilter::utf8_value("source", source.to_string())
    }

    fn target_exact_filter(target: MemoryId) -> ExactMatchFilter {
        ExactMatchFilter::utf8_value("target", target.to_string())
    }

    fn quoted_in_values<T>(ids: &[T]) -> Vec<String>
    where
        T: ToString,
    {
        ids.iter()
            .map(|id| {
                let value = id.to_string();
                let escaped = value.replace('\'', "''");
                format!("'{escaped}'")
            })
            .collect()
    }

    fn quoted_namespace_values(namespaces: &[Namespace]) -> Vec<String> {
        namespaces
            .iter()
            .map(|namespace| {
                let escaped = namespace.as_str().replace('\'', "''");
                format!("'{escaped}'")
            })
            .collect()
    }

    /// Create a persistent graph handle without async index setup.
    ///
    /// Use this when constructing `HirnDB` synchronously. Call
    /// `ensure_indices()` later (e.g. on first
    /// async operation) if you need index guarantees.
    #[must_use]
    pub fn new(storage: Arc<dyn PhysicalStore>) -> Self {
        Self { storage }
    }

    /// Open or create a persistent graph on the given storage backend.
    ///
    /// On first open, creates the `graph_nodes` and `graph_edges` datasets
    /// with appropriate indices.
    pub async fn open(storage: Arc<dyn PhysicalStore>) -> HirnResult<Self> {
        let pg = Self { storage };
        pg.ensure_indices().await?;
        Ok(pg)
    }

    /// Ensure indices exist for efficient queries.
    async fn ensure_indices(&self) -> HirnResult<()> {
        // Only create indices if the datasets exist and have rows.
        if self
            .storage
            .exists(DATASET_NODES_NAME)
            .await
            .unwrap_or(false)
        {
            let count = self
                .storage
                .count(DATASET_NODES_NAME, None)
                .await
                .unwrap_or(0);
            if count > 0 {
                let _ = self
                    .storage
                    .create_index(
                        DATASET_NODES_NAME,
                        IndexConfig {
                            columns: vec!["id".into()],
                            index_type: IndexType::BTree,
                            replace: false,
                            params: Default::default(),
                        },
                    )
                    .await;
                let _ = self
                    .storage
                    .create_index(
                        DATASET_NODES_NAME,
                        IndexConfig {
                            columns: vec!["layer".into()],
                            index_type: IndexType::Bitmap,
                            replace: false,
                            params: Default::default(),
                        },
                    )
                    .await;
            }
        }
        if self
            .storage
            .exists(DATASET_EDGES_NAME)
            .await
            .unwrap_or(false)
        {
            let count = self
                .storage
                .count(DATASET_EDGES_NAME, None)
                .await
                .unwrap_or(0);
            if count > 0 {
                let _ = self
                    .storage
                    .create_index(
                        DATASET_EDGES_NAME,
                        IndexConfig {
                            columns: vec!["source".into()],
                            index_type: IndexType::Bitmap,
                            replace: false,
                            params: Default::default(),
                        },
                    )
                    .await;
                let _ = self
                    .storage
                    .create_index(
                        DATASET_EDGES_NAME,
                        IndexConfig {
                            columns: vec!["target".into()],
                            index_type: IndexType::BTree,
                            replace: false,
                            params: Default::default(),
                        },
                    )
                    .await;
                let _ = self
                    .storage
                    .create_index(
                        DATASET_EDGES_NAME,
                        IndexConfig {
                            columns: vec!["relation".into()],
                            index_type: IndexType::Bitmap,
                            replace: false,
                            params: Default::default(),
                        },
                    )
                    .await;
            }
        }
        Ok(())
    }

    async fn scan_nodes(&self, options: ScanOptions) -> HirnResult<Vec<GraphNodeData>> {
        let mut stream = self
            .storage
            .scan_stream(DATASET_NODES_NAME, options)
            .await?;
        let mut nodes = Vec::new();

        while let Some(batch) = stream.try_next().await? {
            nodes.extend(graph::nodes_from_batch(&batch)?);
        }

        Ok(nodes)
    }

    async fn scan_edges(&self, options: ScanOptions) -> HirnResult<Vec<GraphEdge>> {
        let mut stream = self
            .storage
            .scan_stream(DATASET_EDGES_NAME, options)
            .await?;
        let mut edges = Vec::new();

        while let Some(batch) = stream.try_next().await? {
            // Filter out soft-expired edges so all live read paths respect
            // bi-temporal semantics without duplicating the predicate in every
            // call site.  Time-travel queries must use a dedicated API that
            // bypasses this filter.
            edges.extend(
                graph::edges_from_batch(&batch)?
                    .into_iter()
                    .filter(|e| e.is_currently_active()),
            );
        }

        Ok(edges)
    }

    // ── Node Operations ─────────────────────────────────

    /// Add or update a node in the graph.
    pub async fn add_node(
        &self,
        id: MemoryId,
        layer: Layer,
        importance: f32,
        created_at: Timestamp,
        namespace: Namespace,
    ) -> HirnResult<bool> {
        let node = GraphNodeData {
            id,
            layer,
            importance,
            created_at,
            namespace,
            access_count: 0,
        };
        let batch = graph::nodes_to_batch(&[node])?;
        self.storage
            .merge_insert(DATASET_NODES_NAME, &["id"], batch)
            .await?;
        Ok(true)
    }

    /// Add or update multiple nodes in the graph with one storage write.
    pub async fn add_nodes(&self, nodes: &[GraphNodeData]) -> HirnResult<()> {
        if nodes.is_empty() {
            return Ok(());
        }

        let batch = graph::nodes_to_batch(nodes)?;
        self.storage
            .merge_insert(DATASET_NODES_NAME, &["id"], batch)
            .await?;
        Ok(())
    }

    /// Retrieve a node by ID.
    pub async fn get_node(&self, id: MemoryId) -> HirnResult<Option<GraphNodeData>> {
        let id_str = id.to_string();
        let nodes = self
            .scan_nodes(ScanOptions {
                columns: None,
                filter: None,
                exact_filter: Some(ExactMatchFilter::utf8_value("id", id_str)),
                order_by: None,
                limit: Some(1),
                offset: None,
            })
            .await?;

        Ok(nodes.into_iter().next())
    }

    /// Update node metadata (importance, etc.) via merge-insert.
    pub async fn update_node(&self, node: GraphNodeData) -> HirnResult<()> {
        let batch = graph::nodes_to_batch(&[node])?;
        self.storage
            .merge_insert(DATASET_NODES_NAME, &["id"], batch)
            .await?;
        Ok(())
    }

    /// Bulk-update `access_count` for a batch of nodes in the cold-tier Lance dataset.
    ///
    /// Called periodically by `CachedGraphStore::flush_hot_access_counts()` to
    /// persist the hot-tier access counts to Lance without issuing one merge-insert
    /// per node.  Uses a CASE expression to update all rows in a single pass.
    ///
    /// Silently skips when `dirty` is empty or the dataset does not yet exist.
    pub async fn flush_access_counts(&self, dirty: &[(MemoryId, u64)]) -> HirnResult<()> {
        if dirty.is_empty() {
            return Ok(());
        }

        // Process in chunks of 500 to keep the SQL expression manageable.
        for chunk in dirty.chunks(500) {
            // Build the IN-list filter.
            let id_list: Vec<String> = chunk
                .iter()
                .map(|(id, _)| format!("'{}'", id.to_string().replace('\'', "''")))
                .collect();
            let filter = format!("id IN ({})", id_list.join(", "));

            // Build CASE expression: CASE id WHEN 'id1' THEN 10 … ELSE access_count END
            let mut case_expr = String::from("CASE id");
            for (id, count) in chunk {
                case_expr.push_str(&format!(
                    " WHEN '{}' THEN {}",
                    id.to_string().replace('\'', "''"),
                    count
                ));
            }
            // Keep existing value for any rows not in the IN-list (defensive).
            case_expr.push_str(" ELSE access_count END");

            let case_expr_ref: &str = &case_expr;
            let updates: &[(&str, &str)] = &[("access_count", case_expr_ref)];

            if let Err(e) = self
                .storage
                .update_where(DATASET_NODES_NAME, &filter, updates)
                .await
            {
                tracing::warn!(error = %e, "flush_access_counts: update_where failed; skipping chunk");
            }
        }

        Ok(())
    }

    /// Remove a node and all its edges.
    pub async fn remove_node(&self, id: MemoryId) -> HirnResult<bool> {
        let id_str = id.to_string();

        // Check if node exists.
        if self.get_node(id).await?.is_none() {
            return Ok(false);
        }

        // Expire all edges from/to this node instead of hard-deleting them so
        // that `AS OF` time-travel queries on the cold tier can still find them.
        self.expire_node_edges(id, Timestamp::now()).await?;

        // Remove the node.
        let exact_filter = ExactMatchFilter::utf8_value("id", id_str);
        self.storage
            .delete_exact(DATASET_NODES_NAME, &exact_filter)
            .await?;

        Ok(true)
    }

    /// Set `valid_until_ms` on all Lance edges whose `source` or `target` is
    /// `node_id`, recording the bi-temporal expiry timestamp.
    ///
    /// This soft-deletes the edges from live traversal (which filters on
    /// `valid_until_ms IS NULL OR valid_until_ms > now()`) while keeping them
    /// readable for `AS OF` time-travel queries.
    pub async fn expire_node_edges(
        &self,
        node_id: MemoryId,
        expiry: Timestamp,
    ) -> HirnResult<()> {
        let id_str = node_id.to_string();
        let expiry_ms = expiry.timestamp_ms() as i64;
        let expiry_expr = expiry_ms.to_string();

        // Only set valid_until_ms on edges that are not yet expired.
        let filter_source = format!(
            "source = '{}' AND (valid_until_ms IS NULL OR valid_until_ms = 0)",
            id_str.replace('\'', "''")
        );
        let filter_target = format!(
            "target = '{}' AND (valid_until_ms IS NULL OR valid_until_ms = 0)",
            id_str.replace('\'', "''")
        );

        let updates: &[(&str, &str)] = &[("valid_until_ms", &expiry_expr)];
        // Best-effort: log warnings but don't fail node removal on edge-expiry errors.
        if let Err(e) = self
            .storage
            .update_where(DATASET_EDGES_NAME, &filter_source, updates)
            .await
        {
            tracing::warn!(node_id = %node_id, error = %e, "expire_node_edges: failed to expire source edges");
        }
        if let Err(e) = self
            .storage
            .update_where(DATASET_EDGES_NAME, &filter_target, updates)
            .await
        {
            tracing::warn!(node_id = %node_id, error = %e, "expire_node_edges: failed to expire target edges");
        }
        Ok(())
    }


    /// Check if a node exists.
    pub async fn has_node(&self, id: MemoryId) -> HirnResult<bool> {
        Ok(self.get_node(id).await?.is_some())
    }

    /// Count all nodes.
    pub async fn node_count(&self) -> HirnResult<u64> {
        if !self.storage.exists(DATASET_NODES_NAME).await? {
            return Ok(0);
        }
        self.storage
            .count(DATASET_NODES_NAME, None)
            .await
            .map_err(Into::into)
    }

    /// Get all node IDs.
    pub async fn node_ids(&self) -> HirnResult<Vec<MemoryId>> {
        if !self.storage.exists(DATASET_NODES_NAME).await? {
            return Ok(vec![]);
        }
        let mut stream = self
            .storage
            .scan_stream(
                DATASET_NODES_NAME,
                ScanOptions {
                    columns: Some(vec!["id".into()]),
                    filter: None,
                    exact_filter: None,
                    order_by: None,
                    limit: None,
                    offset: None,
                },
            )
            .await?;

        let mut ids = Vec::new();
        while let Some(batch) = stream.try_next().await? {
            let col = batch
                .column_by_name("id")
                .and_then(|c| c.as_any().downcast_ref::<arrow_array::StringArray>());
            if let Some(arr) = col {
                for i in 0..arr.len() {
                    if let Ok(id) = MemoryId::parse(arr.value(i)) {
                        ids.push(id);
                    }
                }
            }
        }
        Ok(ids)
    }

    /// Filter nodes by layer.
    pub async fn nodes_by_layer(&self, layer: Layer) -> HirnResult<Vec<GraphNodeData>> {
        if !self.storage.exists(DATASET_NODES_NAME).await? {
            return Ok(vec![]);
        }
        self.scan_nodes(ScanOptions {
            columns: None,
            filter: None,
            exact_filter: Some(Self::layer_exact_filter(layer)),
            order_by: None,
            limit: None,
            offset: None,
        })
        .await
    }

    /// Filter nodes by namespace.
    pub async fn nodes_by_namespace(&self, ns: &Namespace) -> HirnResult<Vec<GraphNodeData>> {
        if !self.storage.exists(DATASET_NODES_NAME).await? {
            return Ok(vec![]);
        }
        self.scan_nodes(ScanOptions {
            columns: None,
            filter: None,
            exact_filter: Some(Self::namespace_exact_filter(ns)),
            order_by: None,
            limit: None,
            offset: None,
        })
        .await
    }

    /// Get node importance.
    pub async fn node_importance(&self, id: MemoryId) -> HirnResult<Option<f32>> {
        Ok(self.get_node(id).await?.map(|n| n.importance))
    }

    /// Set node importance.
    pub async fn set_node_importance(&self, id: MemoryId, importance: f32) -> HirnResult<()> {
        if let Some(mut node) = self.get_node(id).await? {
            node.importance = importance;
            self.update_node(node).await?;
        }
        Ok(())
    }

    // ── Edge Operations ─────────────────────────────────

    /// Add a directed edge. Returns the edge ID.
    ///
    /// Enforces `MAX_EDGES_PER_NODE` fan-out cap and prevents duplicate
    /// edges with the same (source, target, relation) triple.
    ///
    /// For bidirectional relations (`RelatedTo`, `Contradicts`, `SimilarTo`),
    /// the reverse edge is automatically created so graph traversal works
    /// symmetrically from either endpoint.
    pub async fn add_edge(
        &self,
        source: MemoryId,
        target: MemoryId,
        relation: EdgeRelation,
        weight: f32,
        metadata: Metadata,
    ) -> HirnResult<EdgeId> {
        let id = self
            .add_edge_one_dir(source, target, relation, weight, metadata.clone(), None)
            .await?;

        // Automatically create the reverse edge for bidirectional relations.
        if relation.is_bidirectional() && source != target {
            match self
                .add_edge_one_dir(target, source, relation, weight, metadata, None)
                .await
            {
                Ok(_) => {}
                Err(hirn_core::HirnError::AlreadyExists(_)) => {}
                Err(e) => return Err(e),
            }
        }

        Ok(id)
    }

    /// Create a causal edge with associated [`CausalEdgeData`] on the cold tier.
    ///
    /// Populates `strength`, `confidence`, `evidence_count`, and `mechanism`
    /// on the stored edge. Bidirectional relations get an automatic reverse
    /// edge that shares the same causal data.
    pub async fn add_causal_edge(
        &self,
        source: MemoryId,
        target: MemoryId,
        relation: EdgeRelation,
        weight: f32,
        metadata: Metadata,
        causal: hirn_graph::CausalEdgeData,
    ) -> HirnResult<EdgeId> {
        let id = self
            .add_edge_one_dir(
                source,
                target,
                relation,
                weight,
                metadata.clone(),
                Some(Box::new(causal.clone())),
            )
            .await?;

        if relation.is_bidirectional() && source != target {
            match self
                .add_edge_one_dir(
                    target,
                    source,
                    relation,
                    weight,
                    metadata,
                    Some(Box::new(causal)),
                )
                .await
            {
                Ok(_) => {}
                Err(hirn_core::HirnError::AlreadyExists(_)) => {}
                Err(e) => return Err(e),
            }
        }

        Ok(id)
    }

    /// Internal: create a single directed edge (no automatic reverse).
    async fn add_edge_one_dir(
        &self,
        source: MemoryId,
        target: MemoryId,
        relation: EdgeRelation,
        weight: f32,
        metadata: Metadata,
        causal: Option<Box<hirn_graph::CausalEdgeData>>,
    ) -> HirnResult<EdgeId> {
        validate_edge_metadata(&metadata)?;

        // Fan-out cap.
        let existing = self.get_edges_from(source).await?;
        if existing.len() >= MAX_EDGES_PER_NODE {
            return Err(hirn_core::HirnError::InvalidInput(format!(
                "node {} has reached the maximum of {} edges",
                source, MAX_EDGES_PER_NODE
            )));
        }

        // Duplicate check.
        for e in &existing {
            if e.target == target && e.relation == relation {
                return Err(hirn_core::HirnError::AlreadyExists(format!(
                    "edge {source} -[{relation:?}]-> {target} already exists"
                )));
            }
        }

        let now = Timestamp::now();
        let id = MemoryId::new();

        // Inherit namespace from source node (default if not found).
        let ns = match self.get_node(source).await? {
            Some(n) => n.namespace,
            None => Namespace::default(),
        };

        let edge = GraphEdge {
            id,
            source,
            target,
            relation,
            weight: weight.clamp(0.01, 1.0),
            co_retrieval_count: 0,
            created_at: now,
            updated_at: now,
            valid_from: None,
            valid_until: None,
            metadata,
            resolved: false,
            namespace: ns,
            causal,
        };

        let batch = graph::edges_to_batch(&[edge])?;
        self.storage
            .merge_insert(DATASET_EDGES_NAME, &["id"], batch)
            .await?;

        Ok(id)
    }

    /// Get all edges originating from a node.
    pub async fn get_edges_from(&self, source: MemoryId) -> HirnResult<Vec<GraphEdge>> {
        if !self.storage.exists(DATASET_EDGES_NAME).await? {
            return Ok(vec![]);
        }
        self.scan_edges(ScanOptions {
            columns: None,
            filter: None,
            exact_filter: Some(Self::source_exact_filter(source)),
            order_by: None,
            limit: None,
            offset: None,
        })
        .await
    }

    /// Get all edges pointing to a node.
    pub async fn get_edges_to(&self, target: MemoryId) -> HirnResult<Vec<GraphEdge>> {
        if !self.storage.exists(DATASET_EDGES_NAME).await? {
            return Ok(vec![]);
        }
        self.scan_edges(ScanOptions {
            columns: None,
            filter: None,
            exact_filter: Some(Self::target_exact_filter(target)),
            order_by: None,
            limit: None,
            offset: None,
        })
        .await
    }

    /// Get all edges from/to a node.
    pub async fn get_edges(&self, node_id: MemoryId) -> HirnResult<Vec<GraphEdge>> {
        if !self.storage.exists(DATASET_EDGES_NAME).await? {
            return Ok(vec![]);
        }
        let id_str = node_id.to_string();
        self.scan_edges(ScanOptions {
            columns: None,
            filter: None,
            exact_filter: Some(ExactMatchFilter::utf8_multi_column_or(
                vec!["source".to_string(), "target".to_string()],
                &id_str,
            )),
            order_by: None,
            limit: None,
            offset: None,
        })
        .await
    }

    /// Get edges between two nodes (both directions).
    pub async fn get_edges_between(&self, a: MemoryId, b: MemoryId) -> HirnResult<Vec<GraphEdge>> {
        if !self.storage.exists(DATASET_EDGES_NAME).await? {
            return Ok(vec![]);
        }
        let a_str = a.to_string();
        let b_str = b.to_string();
        self.scan_edges(ScanOptions {
            columns: None,
            filter: Some(format!(
                "(source = '{a_str}' AND target = '{b_str}') OR (source = '{b_str}' AND target = '{a_str}')"
            )),
            exact_filter: None,
            order_by: None,
            limit: None,
            offset: None,
        })
        .await
    }

    /// Get edges of a specific type from a node.
    pub async fn get_edges_of_type(
        &self,
        node_id: MemoryId,
        relation: EdgeRelation,
    ) -> HirnResult<Vec<GraphEdge>> {
        if !self.storage.exists(DATASET_EDGES_NAME).await? {
            return Ok(vec![]);
        }
        let id_str = node_id.to_string();
        let rel_str = edge_relation_to_str(relation);
        self.scan_edges(ScanOptions {
            columns: None,
            filter: Some(format!(
                "(source = '{id_str}' OR target = '{id_str}') AND relation = '{rel_str}'"
            )),
            exact_filter: None,
            order_by: None,
            limit: None,
            offset: None,
        })
        .await
    }

    /// Update edge weight via merge-insert.
    pub async fn update_edge_weight(
        &self,
        edge_id: EdgeId,
        new_weight: f32,
        co_retrieval_count: Option<u64>,
    ) -> HirnResult<()> {
        if let Some(mut edge) = self.get_edges_by_ids(&[edge_id]).await?.into_iter().next() {
            edge.weight = new_weight.clamp(0.01, 1.0);
            edge.updated_at = Timestamp::now();
            if let Some(count) = co_retrieval_count {
                edge.co_retrieval_count = count;
            }
            self.upsert_edges(&[edge]).await?;
        }
        Ok(())
    }

    /// Get a batch of edges by ID in a single scan.
    pub async fn get_edges_by_ids(&self, edge_ids: &[EdgeId]) -> HirnResult<Vec<GraphEdge>> {
        if edge_ids.is_empty() {
            return Ok(vec![]);
        }
        if !self.storage.exists(DATASET_EDGES_NAME).await? {
            return Ok(vec![]);
        }

        let unique_ids: Vec<EdgeId> = edge_ids
            .iter()
            .copied()
            .collect::<HashSet<_>>()
            .into_iter()
            .collect();
        let predicate = format!("id IN ({})", Self::quoted_in_values(&unique_ids).join(", "));
        self.scan_edges(ScanOptions {
            columns: None,
            filter: Some(predicate),
            exact_filter: None,
            order_by: None,
            limit: None,
            offset: None,
        })
        .await
    }

    /// Get all edges incident to any of the provided nodes in a single scan.
    pub async fn get_edges_for_nodes(&self, node_ids: &[MemoryId]) -> HirnResult<Vec<GraphEdge>> {
        if node_ids.is_empty() {
            return Ok(vec![]);
        }
        if !self.storage.exists(DATASET_EDGES_NAME).await? {
            return Ok(vec![]);
        }

        let unique_ids: Vec<MemoryId> = node_ids
            .iter()
            .copied()
            .collect::<HashSet<_>>()
            .into_iter()
            .collect();
        let in_values = Self::quoted_in_values(&unique_ids).join(", ");
        self.scan_edges(ScanOptions {
            columns: None,
            filter: Some(format!(
                "source IN ({in_values}) OR target IN ({in_values})"
            )),
            exact_filter: None,
            order_by: None,
            limit: None,
            offset: None,
        })
        .await
    }

    /// Get a single edge by ID.
    pub async fn get_edge(&self, edge_id: EdgeId) -> HirnResult<Option<GraphEdge>> {
        if !self.storage.exists(DATASET_EDGES_NAME).await? {
            return Ok(None);
        }
        let id_str = edge_id.to_string();
        let edges = self
            .scan_edges(ScanOptions {
                columns: None,
                filter: None,
                exact_filter: Some(ExactMatchFilter::utf8_value("id", id_str)),
                order_by: None,
                limit: Some(1),
                offset: None,
            })
            .await?;

        Ok(edges.into_iter().next())
    }

    /// Remove a single edge by ID.
    pub async fn remove_edge(&self, edge_id: EdgeId) -> HirnResult<()> {
        let id_str = edge_id.to_string();
        let exact_filter = ExactMatchFilter::utf8_value("id", id_str);
        self.storage
            .delete_exact(DATASET_EDGES_NAME, &exact_filter)
            .await?;
        Ok(())
    }

    /// Count currently-active (non-expired) edges.
    ///
    /// Excludes edges whose `valid_until_ms` has been set (soft-expired),
    /// consistent with the hot-tier `PropertyGraph::edge_count()` which filters
    /// by `is_currently_active()`.
    pub async fn edge_count(&self) -> HirnResult<u64> {
        if !self.storage.exists(DATASET_EDGES_NAME).await? {
            return Ok(0);
        }
        // Active = valid_until_ms not set (NULL or 0) or in the future.
        let now_ms = hirn_core::timestamp::Timestamp::now().timestamp_ms();
        let active_filter = format!(
            "valid_until_ms IS NULL OR valid_until_ms = 0 OR valid_until_ms > {now_ms}"
        );
        self.storage
            .count(DATASET_EDGES_NAME, Some(&active_filter))
            .await
            .map_err(Into::into)
    }

    /// Batch add edges.
    pub async fn add_edges(&self, edges: &[GraphEdge]) -> HirnResult<()> {
        self.upsert_edges(edges).await
    }

    /// Batch upsert edges by ID.
    pub async fn upsert_edges(&self, edges: &[GraphEdge]) -> HirnResult<()> {
        if edges.is_empty() {
            return Ok(());
        }
        let batch = graph::edges_to_batch(edges)?;
        self.storage
            .merge_insert(DATASET_EDGES_NAME, &["id"], batch)
            .await?;
        Ok(())
    }

    // ── Graph Traversal ──────────────────────────

    /// Get outgoing weighted edges for spreading activation.
    /// Returns (target_id, weight, relation) for each outgoing edge.
    pub async fn outgoing_weighted(
        &self,
        node_id: MemoryId,
    ) -> HirnResult<Vec<(MemoryId, f32, EdgeRelation)>> {
        let edges = self.get_edges_from(node_id).await?;
        Ok(edges
            .into_iter()
            .map(|e| (e.target, e.weight, e.relation))
            .collect())
    }

    // ── Batch Graph Operations ──────────────────

    pub async fn batch_adjacency_read(&self, frontier: &[MemoryId]) -> HirnResult<Vec<GraphEdge>> {
        self.batch_adjacency_read_scoped(frontier, None).await
    }

    /// Batch adjacency read: fetch all outgoing edges for a set of frontier
    /// nodes in a single scan using an `IN (...)` predicate.
    ///
    /// Replaces `O(frontier)` individual `get_edges_from()` calls with a
    /// single scan that uses the bitmap index on `graph_edges.source`.
    pub async fn batch_adjacency_read_scoped(
        &self,
        frontier: &[MemoryId],
        allowed_namespaces: Option<&[Namespace]>,
    ) -> HirnResult<Vec<GraphEdge>> {
        if frontier.is_empty() {
            return Ok(vec![]);
        }
        if allowed_namespaces.is_some_and(<[Namespace]>::is_empty) {
            return Ok(vec![]);
        }
        if !self.storage.exists(DATASET_EDGES_NAME).await? {
            return Ok(vec![]);
        }

        let mut predicate = format!(
            "source IN ({})",
            Self::quoted_in_values(frontier).join(", ")
        );
        if let Some(allowed_namespaces) = allowed_namespaces {
            predicate.push_str(" AND namespace IN (");
            predicate.push_str(&Self::quoted_namespace_values(allowed_namespaces).join(", "));
            predicate.push(')');
        }

        let edges = self
            .scan_edges(ScanOptions {
                columns: None,
                filter: Some(predicate),
                exact_filter: None,
                order_by: None,
                limit: None,
                offset: None,
            })
            .await?;

        self.filter_edges_by_target_namespace(edges, allowed_namespaces)
            .await
    }

    /// Batch adjacency read with a relation type filter.
    ///
    /// Same as [`Self::batch_adjacency_read`] but additionally filters edges by
    /// relation type, enabling efficient traversal of specific edge kinds
    /// (e.g., causal chains).
    pub async fn batch_adjacency_read_filtered(
        &self,
        frontier: &[MemoryId],
        relation: EdgeRelation,
    ) -> HirnResult<Vec<GraphEdge>> {
        self.batch_adjacency_read_filtered_scoped(frontier, relation, None)
            .await
    }

    /// Batch adjacency read with relation and namespace filters.
    pub async fn batch_adjacency_read_filtered_scoped(
        &self,
        frontier: &[MemoryId],
        relation: EdgeRelation,
        allowed_namespaces: Option<&[Namespace]>,
    ) -> HirnResult<Vec<GraphEdge>> {
        if frontier.is_empty() {
            return Ok(vec![]);
        }
        if allowed_namespaces.is_some_and(<[Namespace]>::is_empty) {
            return Ok(vec![]);
        }
        if !self.storage.exists(DATASET_EDGES_NAME).await? {
            return Ok(vec![]);
        }

        let rel_str = edge_relation_to_str(relation);
        let mut predicate = format!(
            "source IN ({}) AND relation = '{rel_str}'",
            Self::quoted_in_values(frontier).join(", ")
        );
        if let Some(allowed_namespaces) = allowed_namespaces {
            predicate.push_str(" AND namespace IN (");
            predicate.push_str(&Self::quoted_namespace_values(allowed_namespaces).join(", "));
            predicate.push(')');
        }

        let edges = self
            .scan_edges(ScanOptions {
                columns: None,
                filter: Some(predicate),
                exact_filter: None,
                order_by: None,
                limit: None,
                offset: None,
            })
            .await?;

        self.filter_edges_by_target_namespace(edges, allowed_namespaces)
            .await
    }

    async fn filter_edges_by_target_namespace(
        &self,
        edges: Vec<GraphEdge>,
        allowed_namespaces: Option<&[Namespace]>,
    ) -> HirnResult<Vec<GraphEdge>> {
        let Some(allowed_namespaces) = allowed_namespaces else {
            return Ok(edges);
        };
        if allowed_namespaces.is_empty() || edges.is_empty() {
            return Ok(vec![]);
        }

        let mut namespace_cache = HashMap::new();
        let mut visible_edges = Vec::with_capacity(edges.len());
        for edge in edges {
            if let std::collections::hash_map::Entry::Vacant(entry) =
                namespace_cache.entry(edge.target)
            {
                let is_visible = self
                    .node_namespace(edge.target)
                    .await?
                    .is_some_and(|namespace| allowed_namespaces.contains(&namespace));
                entry.insert(is_visible);
            }
            if namespace_cache.get(&edge.target).copied().unwrap_or(false) {
                visible_edges.push(edge);
            }
        }

        Ok(visible_edges)
    }

    async fn filter_node_ids_by_namespace(
        &self,
        ids: &[MemoryId],
        allowed_namespaces: Option<&[Namespace]>,
    ) -> HirnResult<Vec<MemoryId>> {
        let Some(allowed_namespaces) = allowed_namespaces else {
            return Ok(ids.to_vec());
        };
        if allowed_namespaces.is_empty() || ids.is_empty() {
            return Ok(vec![]);
        }

        let mut visible = Vec::with_capacity(ids.len());
        for &id in ids {
            if self
                .node_namespace(id)
                .await?
                .is_some_and(|namespace| allowed_namespaces.contains(&namespace))
            {
                visible.push(id);
            }
        }

        Ok(visible)
    }

    /// Batch BFS using batch adjacency reads.
    ///
    /// Performs breadth-first search starting from `start_ids`, expanding
    /// the frontier at each depth level with a single batch scan. Total
    /// number of scans = `max_depth` (not frontier_size × depth).
    ///
    /// Returns a [`BfsResult`] containing edges at each depth level and
    /// all visited node IDs.
    pub async fn batch_bfs(
        &self,
        start_ids: &[MemoryId],
        max_depth: usize,
    ) -> HirnResult<BfsResult> {
        self.batch_bfs_filtered(start_ids, max_depth, None).await
    }

    /// Batch BFS with optional relation type filter.
    pub async fn batch_bfs_filtered(
        &self,
        start_ids: &[MemoryId],
        max_depth: usize,
        relation: Option<EdgeRelation>,
    ) -> HirnResult<BfsResult> {
        self.batch_bfs_filtered_scoped(start_ids, max_depth, relation, None)
            .await
    }

    /// Batch BFS with optional relation and namespace filters.
    pub async fn batch_bfs_filtered_scoped(
        &self,
        start_ids: &[MemoryId],
        max_depth: usize,
        relation: Option<EdgeRelation>,
        allowed_namespaces: Option<&[Namespace]>,
    ) -> HirnResult<BfsResult> {
        use std::collections::HashSet;

        let start_ids = self
            .filter_node_ids_by_namespace(start_ids, allowed_namespaces)
            .await?;
        let mut visited: HashSet<MemoryId> = start_ids.iter().copied().collect();
        let mut depths: Vec<Vec<GraphEdge>> = Vec::with_capacity(max_depth);
        let mut frontier: Vec<MemoryId> = start_ids;

        for _ in 0..max_depth {
            if frontier.is_empty() {
                break;
            }

            let edges = match relation {
                Some(rel) => {
                    self.batch_adjacency_read_filtered_scoped(&frontier, rel, allowed_namespaces)
                        .await?
                }
                None => {
                    self.batch_adjacency_read_scoped(&frontier, allowed_namespaces)
                        .await?
                }
            };

            let mut next_frontier = Vec::new();
            let mut depth_edges = Vec::new();

            for edge in edges {
                depth_edges.push(edge.clone());
                if visited.insert(edge.target) {
                    next_frontier.push(edge.target);
                }
            }

            depths.push(depth_edges);
            frontier = next_frontier;
        }

        Ok(BfsResult {
            depths,
            visited: visited.into_iter().collect(),
        })
    }

    /// Deep causal BFS on the cold (Lance) tier.
    ///
    /// Performs batched breadth-first search following only edges of the given
    /// `relation` type, pruning edges below `confidence_threshold` and outside
    /// `allowed_namespaces`. Returns a flat list of [`CausalBfsRow`] records
    /// suitable for converting to Arrow `RecordBatch`.
    ///
    /// This is the cold-tier counterpart of `CausalChainExec`'s in-memory DFS.
    /// Each row represents one edge in a causal chain, tagged with a chain_id,
    /// depth, and the chain's composite score.
    ///
    /// Complexity: one Lance scan per depth level (not per node).
    pub async fn deep_causal_bfs(
        &self,
        start_ids: &[MemoryId],
        max_depth: usize,
        confidence_threshold: f32,
        relation: EdgeRelation,
        allowed_namespaces: Option<&[Namespace]>,
    ) -> HirnResult<Vec<CausalBfsRow>> {
        use std::collections::{HashMap, HashSet};

        let bfs = self
            .batch_bfs_filtered_scoped(start_ids, max_depth, Some(relation), allowed_namespaces)
            .await?;

        // Build a map: source → Vec<GraphEdge> for chain reconstruction.
        let mut adjacency: HashMap<MemoryId, Vec<&GraphEdge>> = HashMap::new();
        for depth_edges in &bfs.depths {
            for edge in depth_edges {
                adjacency.entry(edge.source).or_default().push(edge);
            }
        }

        // DFS over the BFS result to enumerate individual chains.
        let mut rows = Vec::new();
        let mut chain_counter = 0_u32;

        for &seed in start_ids {
            // Stack: (current_node, depth, chain_edges_so_far, visited)
            let mut stack: Vec<(MemoryId, usize, Vec<CausalBfsEdge>, HashSet<MemoryId>)> = vec![{
                let mut visited = HashSet::new();
                visited.insert(seed);
                (seed, 0, Vec::new(), visited)
            }];

            while let Some((node, depth, chain_edges, visited)) = stack.pop() {
                if depth >= max_depth {
                    if !chain_edges.is_empty() {
                        emit_causal_rows(&chain_edges, &mut rows, &mut chain_counter);
                    }
                    continue;
                }

                let neighbors = adjacency.get(&node);
                let causal: Vec<&GraphEdge> = neighbors
                    .map(|edges| {
                        edges
                            .iter()
                            .filter(|e| {
                                let conf = e.confidence().unwrap_or(0.5);
                                conf >= confidence_threshold && !visited.contains(&e.target)
                            })
                            .copied()
                            .collect()
                    })
                    .unwrap_or_default();

                if causal.is_empty() {
                    if !chain_edges.is_empty() {
                        emit_causal_rows(&chain_edges, &mut rows, &mut chain_counter);
                    }
                    continue;
                }

                for edge in causal {
                    let mut new_chain = chain_edges.clone();
                    new_chain.push(CausalBfsEdge {
                        source: edge.source,
                        target: edge.target,
                        strength: edge.strength().unwrap_or(edge.weight),
                        confidence: edge.confidence().unwrap_or(0.5),
                        evidence_count: edge.evidence_count().unwrap_or(1) as u32,
                        mechanism: edge.mechanism().map(str::to_owned),
                    });
                    let mut new_visited = visited.clone();
                    new_visited.insert(edge.target);
                    stack.push((edge.target, depth + 1, new_chain, new_visited));
                }
            }
        }

        Ok(rows)
    }

    /// BFS neighbor traversal.
    pub async fn get_neighbors(
        &self,
        start: MemoryId,
        max_depth: usize,
        min_weight: f32,
    ) -> HirnResult<Vec<MemoryId>> {
        self.get_neighbors_filtered(start, max_depth, min_weight, None)
            .await
    }

    /// BFS neighbor traversal with optional namespace filter.
    ///
    /// Uses batch adjacency reads: one scan per depth level instead of
    /// one scan per frontier node.
    pub async fn get_neighbors_filtered(
        &self,
        start: MemoryId,
        max_depth: usize,
        min_weight: f32,
        namespace: Option<&Namespace>,
    ) -> HirnResult<Vec<MemoryId>> {
        use std::collections::HashSet;

        let mut visited = HashSet::new();
        visited.insert(start);

        let mut frontier = vec![start];
        let mut result = Vec::new();

        for _ in 0..max_depth {
            if frontier.is_empty() {
                break;
            }

            let edges = self.batch_adjacency_read(&frontier).await?;
            let mut next_frontier = Vec::new();

            for edge in edges {
                if edge.weight < min_weight {
                    continue;
                }
                if visited.contains(&edge.target) {
                    continue;
                }

                // Namespace filter.
                if let Some(ns) = namespace {
                    if let Some(node) = self.get_node(edge.target).await? {
                        let shared = Namespace::shared();
                        if node.namespace != *ns && node.namespace != shared && *ns != shared {
                            continue;
                        }
                    }
                }

                visited.insert(edge.target);
                result.push(edge.target);
                next_frontier.push(edge.target);
            }

            frontier = next_frontier;
        }

        Ok(result)
    }

    /// Shortest path between two nodes (BFS, unweighted).
    ///
    /// Uses batch adjacency reads: one scan per depth level.
    pub async fn shortest_path(
        &self,
        source: MemoryId,
        target: MemoryId,
    ) -> HirnResult<Option<Vec<MemoryId>>> {
        use std::collections::{HashMap as StdMap, HashSet};

        if source == target {
            return Ok(Some(vec![source]));
        }

        let mut visited = HashSet::new();
        visited.insert(source);
        let mut parent: StdMap<MemoryId, MemoryId> = StdMap::new();
        let mut frontier = vec![source];

        while !frontier.is_empty() {
            let edges = self.batch_adjacency_read(&frontier).await?;
            let mut next_frontier = Vec::new();

            for edge in edges {
                if visited.contains(&edge.target) {
                    continue;
                }
                parent.insert(edge.target, edge.source);
                if edge.target == target {
                    // Reconstruct path.
                    let mut path = vec![target];
                    let mut node = target;
                    while let Some(&prev) = parent.get(&node) {
                        path.push(prev);
                        node = prev;
                    }
                    path.reverse();
                    return Ok(Some(path));
                }
                visited.insert(edge.target);
                next_frontier.push(edge.target);
            }

            frontier = next_frontier;
        }
        Ok(None)
    }

    /// Extract subgraph: return all edges between the given node set.
    ///
    /// Uses batch adjacency read to fetch all outgoing edges in one scan,
    /// then filters to edges whose target is also in the node set.
    pub async fn subgraph(&self, node_ids: &[MemoryId]) -> HirnResult<Vec<GraphEdge>> {
        if node_ids.is_empty() {
            return Ok(vec![]);
        }

        let id_set: std::collections::HashSet<MemoryId> = node_ids.iter().copied().collect();
        let all_edges = self.batch_adjacency_read(node_ids).await?;

        Ok(all_edges
            .into_iter()
            .filter(|e| id_set.contains(&e.target))
            .collect())
    }

    /// Degree centrality: count of edges per node.
    pub async fn degree_centrality(&self) -> HirnResult<HashMap<MemoryId, usize>> {
        if !self.storage.exists(DATASET_EDGES_NAME).await? {
            return Ok(HashMap::new());
        }
        let mut stream = self
            .storage
            .scan_stream(
                DATASET_EDGES_NAME,
                ScanOptions {
                    columns: Some(vec!["source".into(), "target".into()]),
                    filter: None,
                    exact_filter: None,
                    order_by: None,
                    limit: None,
                    offset: None,
                },
            )
            .await?;

        let mut degrees: HashMap<MemoryId, usize> = HashMap::new();
        while let Some(batch) = stream.try_next().await? {
            let src = batch
                .column_by_name("source")
                .and_then(|c| c.as_any().downcast_ref::<arrow_array::StringArray>());
            let tgt = batch
                .column_by_name("target")
                .and_then(|c| c.as_any().downcast_ref::<arrow_array::StringArray>());
            if let (Some(s), Some(t)) = (src, tgt) {
                for i in 0..batch.num_rows() {
                    if let Ok(id) = MemoryId::parse(s.value(i)) {
                        *degrees.entry(id).or_default() += 1;
                    }
                    if let Ok(id) = MemoryId::parse(t.value(i)) {
                        *degrees.entry(id).or_default() += 1;
                    }
                }
            }
        }
        Ok(degrees)
    }

    /// Check if a path exists between two nodes via specific edge types.
    ///
    /// Uses batch adjacency reads with relation filter.
    pub async fn path_exists_via(
        &self,
        source: MemoryId,
        target: MemoryId,
        allowed_relations: &[EdgeRelation],
    ) -> HirnResult<bool> {
        use std::collections::HashSet;

        if source == target {
            return Ok(true);
        }

        let mut visited = HashSet::new();
        visited.insert(source);
        let mut frontier = vec![source];

        while !frontier.is_empty() {
            let edges = self.batch_adjacency_read(&frontier).await?;
            let mut next_frontier = Vec::new();

            for edge in edges {
                if !allowed_relations.contains(&edge.relation) {
                    continue;
                }
                if visited.contains(&edge.target) {
                    continue;
                }
                if edge.target == target {
                    return Ok(true);
                }
                visited.insert(edge.target);
                next_frontier.push(edge.target);
            }

            frontier = next_frontier;
        }
        Ok(false)
    }

    /// Get the layer of a node.
    pub async fn node_layer(&self, id: MemoryId) -> HirnResult<Option<Layer>> {
        Ok(self.get_node(id).await?.map(|n| n.layer))
    }

    /// Get the namespace of a node.
    pub async fn node_namespace(&self, id: MemoryId) -> HirnResult<Option<Namespace>> {
        Ok(self.get_node(id).await?.map(|n| n.namespace))
    }

    /// Get all edges in the graph.
    pub async fn all_edges(&self) -> HirnResult<Vec<GraphEdge>> {
        if !self.storage.exists(DATASET_EDGES_NAME).await? {
            return Ok(vec![]);
        }
        let mut batches = self
            .storage
            .scan_stream(
                DATASET_EDGES_NAME,
                ScanOptions {
                    columns: None,
                    filter: None,
                    exact_filter: None,
                    order_by: None,
                    limit: None,
                    offset: None,
                },
            )
            .await?;

        let mut result = Vec::new();
        while let Some(batch) = batches.try_next().await? {
            result.extend(graph::edges_from_batch(&batch)?);
        }
        Ok(result)
    }

    /// Check if two nodes' namespaces are compatible for auto-edge creation.
    /// Compatible means: same namespace, or either is "shared".
    pub async fn namespaces_compatible(&self, a: MemoryId, b: MemoryId) -> HirnResult<bool> {
        let ns_a = self.node_namespace(a).await?;
        let ns_b = self.node_namespace(b).await?;
        match (ns_a, ns_b) {
            (Some(a), Some(b)) => {
                let shared = Namespace::shared();
                Ok(a == b || a == shared || b == shared)
            }
            _ => Ok(false),
        }
    }
}

fn layer_to_str(l: Layer) -> &'static str {
    match l {
        Layer::Working => "Working",
        Layer::Episodic => "Episodic",
        Layer::Semantic => "Semantic",
        Layer::Procedural => "Procedural",
    }
}

fn edge_relation_to_str(r: EdgeRelation) -> &'static str {
    match r {
        EdgeRelation::RelatedTo => "RelatedTo",
        EdgeRelation::Causes => "Causes",
        EdgeRelation::CausedBy => "CausedBy",
        EdgeRelation::DerivedFrom => "DerivedFrom",
        EdgeRelation::Contradicts => "Contradicts",
        EdgeRelation::Supports => "Supports",
        EdgeRelation::TemporalNext => "TemporalNext",
        EdgeRelation::PartOf => "PartOf",
        EdgeRelation::InstanceOf => "InstanceOf",
        EdgeRelation::SimilarTo => "SimilarTo",
        EdgeRelation::Inhibits => "Inhibits",
        EdgeRelation::ParticipatesIn => "ParticipatesIn",
    }
}

// ── GraphStore trait implementation ─────────────────────────────────

use crate::graph_store::GraphStore;
use async_trait::async_trait;

#[async_trait]
impl GraphStore for PersistentGraph {
    async fn add_node(
        &self,
        id: MemoryId,
        layer: Layer,
        importance: f32,
        created_at: Timestamp,
        namespace: Namespace,
    ) -> HirnResult<bool> {
        self.add_node(id, layer, importance, created_at, namespace)
            .await
    }

    async fn remove_node(&self, id: MemoryId) -> HirnResult<bool> {
        self.remove_node(id).await
    }

    async fn has_node(&self, id: MemoryId) -> HirnResult<bool> {
        self.has_node(id).await
    }

    async fn get_node(&self, id: MemoryId) -> HirnResult<Option<GraphNodeData>> {
        self.get_node(id).await
    }

    async fn node_ids(&self) -> HirnResult<Vec<MemoryId>> {
        self.node_ids().await
    }

    async fn node_importance(&self, id: MemoryId) -> HirnResult<Option<f32>> {
        self.node_importance(id).await
    }

    async fn set_node_importance(&self, id: MemoryId, importance: f32) -> HirnResult<()> {
        self.set_node_importance(id, importance).await
    }

    async fn node_layer(&self, id: MemoryId) -> HirnResult<Option<Layer>> {
        self.node_layer(id).await
    }

    async fn node_namespace(&self, id: MemoryId) -> HirnResult<Option<Namespace>> {
        self.node_namespace(id).await
    }

    async fn namespaces_compatible(&self, a: MemoryId, b: MemoryId) -> HirnResult<bool> {
        self.namespaces_compatible(a, b).await
    }

    async fn add_edge(
        &self,
        source: MemoryId,
        target: MemoryId,
        relation: EdgeRelation,
        weight: f32,
        metadata: Metadata,
    ) -> HirnResult<EdgeId> {
        self.add_edge(source, target, relation, weight, metadata)
            .await
    }

    async fn add_causal_edge(
        &self,
        source: MemoryId,
        target: MemoryId,
        relation: EdgeRelation,
        weight: f32,
        metadata: Metadata,
        causal: hirn_graph::CausalEdgeData,
    ) -> HirnResult<EdgeId> {
        self.add_causal_edge(source, target, relation, weight, metadata, causal)
            .await
    }

    async fn remove_edge(&self, edge_id: EdgeId) -> HirnResult<()> {
        self.remove_edge(edge_id).await
    }

    async fn get_edge(&self, edge_id: EdgeId) -> HirnResult<Option<GraphEdge>> {
        self.get_edge(edge_id).await
    }

    async fn get_edges(&self, node_id: MemoryId) -> HirnResult<Vec<GraphEdge>> {
        self.get_edges(node_id).await
    }

    async fn get_edges_between(&self, a: MemoryId, b: MemoryId) -> HirnResult<Vec<GraphEdge>> {
        self.get_edges_between(a, b).await
    }

    async fn get_edges_of_type(
        &self,
        node_id: MemoryId,
        relation: EdgeRelation,
    ) -> HirnResult<Vec<GraphEdge>> {
        self.get_edges_of_type(node_id, relation).await
    }

    async fn all_edges(&self) -> HirnResult<Vec<GraphEdge>> {
        self.all_edges().await
    }

    async fn update_edge_weight(
        &self,
        edge_id: EdgeId,
        new_weight: f32,
        co_retrieval_count: Option<u64>,
    ) -> HirnResult<()> {
        self.update_edge_weight(edge_id, new_weight, co_retrieval_count)
            .await
    }

    async fn get_neighbors(
        &self,
        start: MemoryId,
        depth: usize,
        min_weight: f32,
    ) -> HirnResult<Vec<MemoryId>> {
        self.get_neighbors(start, depth, min_weight).await
    }

    async fn get_neighbors_filtered(
        &self,
        start: MemoryId,
        depth: usize,
        min_weight: f32,
        namespace: Option<&Namespace>,
    ) -> HirnResult<Vec<MemoryId>> {
        self.get_neighbors_filtered(start, depth, min_weight, namespace)
            .await
    }

    async fn outgoing_weighted(
        &self,
        node_id: MemoryId,
    ) -> HirnResult<Vec<(MemoryId, f32, EdgeRelation)>> {
        self.outgoing_weighted(node_id).await
    }

    async fn shortest_path(
        &self,
        source: MemoryId,
        target: MemoryId,
    ) -> HirnResult<Option<Vec<MemoryId>>> {
        self.shortest_path(source, target).await
    }

    async fn node_count(&self) -> HirnResult<usize> {
        self.node_count().await.map(|c| c as usize)
    }

    async fn edge_count(&self) -> HirnResult<usize> {
        self.edge_count().await.map(|c| c as usize)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use hirn_core::metadata::MetadataValue;
    use hirn_graph::MAX_EDGE_METADATA_BYTES;

    fn dummy_storage() -> Arc<dyn PhysicalStore> {
        Arc::new(hirn_storage::memory_store::MemoryStore::new())
    }

    #[tokio::test]
    async fn open_on_empty_storage() {
        let pg = PersistentGraph::open(dummy_storage()).await.unwrap();
        assert_eq!(pg.node_count().await.unwrap(), 0);
        assert_eq!(pg.edge_count().await.unwrap(), 0);
    }

    #[tokio::test]
    async fn add_edge_rejects_oversized_metadata() {
        let pg = PersistentGraph::new(dummy_storage());
        let ns = Namespace::default_ns();
        let now = Timestamp::now();
        let a = MemoryId::new();
        let b = MemoryId::new();
        pg.add_node(a, Layer::Episodic, 0.5, now, ns.clone())
            .await
            .unwrap();
        pg.add_node(b, Layer::Episodic, 0.5, now, ns).await.unwrap();

        let mut metadata = Metadata::new();
        metadata.insert(
            "payload".into(),
            MetadataValue::String("x".repeat(MAX_EDGE_METADATA_BYTES + 64)),
        );

        let err = pg
            .add_edge(a, b, EdgeRelation::Causes, 0.8, metadata)
            .await
            .unwrap_err();
        assert!(err.to_string().contains("edge metadata exceeds"));
    }

    // ── Helper to build a populated graph ──

    /// Create a graph with `node_count` nodes and directed edges forming
    /// a chain: n0 → n1 → n2 → ... plus some cross-links.
    async fn populated_graph(node_count: usize) -> (PersistentGraph, Vec<MemoryId>) {
        let pg = PersistentGraph::new(dummy_storage());
        let ns = Namespace::default_ns();
        let now = Timestamp::now();
        let mut ids = Vec::with_capacity(node_count);

        for _ in 0..node_count {
            let id = MemoryId::new();
            ids.push(id);
            pg.add_node(id, Layer::Episodic, 0.5, now, ns.clone())
                .await
                .unwrap();
        }

        // Chain: n0 → n1 → n2 → ...
        for i in 0..node_count.saturating_sub(1) {
            pg.add_edge(
                ids[i],
                ids[i + 1],
                EdgeRelation::TemporalNext,
                0.8,
                Metadata::default(),
            )
            .await
            .unwrap();
        }

        (pg, ids)
    }

    // ── Batch adjacency read tests ──

    #[tokio::test]
    async fn batch_adjacency_read_empty_frontier() {
        let pg = PersistentGraph::new(dummy_storage());
        let result = pg.batch_adjacency_read(&[]).await.unwrap();
        assert!(result.is_empty());
    }

    #[tokio::test]
    async fn batch_adjacency_read_no_edges() {
        let pg = PersistentGraph::new(dummy_storage());
        let ns = Namespace::default_ns();
        let id = MemoryId::new();
        pg.add_node(id, Layer::Episodic, 0.5, Timestamp::now(), ns)
            .await
            .unwrap();
        let result = pg.batch_adjacency_read(&[id]).await.unwrap();
        assert!(result.is_empty());
    }

    #[tokio::test]
    async fn batch_adjacency_read_single_node() {
        let (pg, ids) = populated_graph(5).await;
        // Node 0 has one outgoing edge (0 → 1)
        let result = pg.batch_adjacency_read(&[ids[0]]).await.unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].source, ids[0]);
        assert_eq!(result[0].target, ids[1]);
    }

    #[tokio::test]
    async fn batch_adjacency_read_multiple_nodes() {
        let (pg, ids) = populated_graph(5).await;
        // Nodes 0, 1, 2 each have one outgoing edge
        let frontier = vec![ids[0], ids[1], ids[2]];
        let result = pg.batch_adjacency_read(&frontier).await.unwrap();
        assert_eq!(result.len(), 3);

        let targets: std::collections::HashSet<MemoryId> =
            result.iter().map(|e| e.target).collect();
        assert!(targets.contains(&ids[1]));
        assert!(targets.contains(&ids[2]));
        assert!(targets.contains(&ids[3]));
    }

    #[tokio::test]
    async fn batch_adjacency_read_filtered_by_relation() {
        let (pg, ids) = populated_graph(5).await;
        // Add a non-chain edge
        pg.add_edge(
            ids[0],
            ids[3],
            EdgeRelation::Causes,
            0.9,
            Metadata::default(),
        )
        .await
        .unwrap();

        // Filter to only Causes edges from node 0
        let result = pg
            .batch_adjacency_read_filtered(&[ids[0]], EdgeRelation::Causes)
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].target, ids[3]);

        // TemporalNext edges from node 0
        let result = pg
            .batch_adjacency_read_filtered(&[ids[0]], EdgeRelation::TemporalNext)
            .await
            .unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].target, ids[1]);
    }

    // ── Batch BFS tests ──

    #[tokio::test]
    async fn batch_bfs_depth_zero() {
        let (pg, ids) = populated_graph(5).await;
        let result = pg.batch_bfs(&[ids[0]], 0).await.unwrap();
        assert!(result.depths.is_empty());
        assert_eq!(result.visited.len(), 1);
        assert!(result.visited.contains(&ids[0]));
    }

    #[tokio::test]
    async fn batch_bfs_depth_one() {
        let (pg, ids) = populated_graph(5).await;
        let result = pg.batch_bfs(&[ids[0]], 1).await.unwrap();
        assert_eq!(result.depths.len(), 1);
        assert_eq!(result.depths[0].len(), 1); // 0 → 1
        assert_eq!(result.depths[0][0].target, ids[1]);
        assert_eq!(result.visited.len(), 2); // {0, 1}
    }

    #[tokio::test]
    async fn batch_bfs_depth_two() {
        let (pg, ids) = populated_graph(5).await;
        let result = pg.batch_bfs(&[ids[0]], 2).await.unwrap();
        assert_eq!(result.depths.len(), 2);
        // Depth 0: 0 → 1
        assert_eq!(result.depths[0].len(), 1);
        // Depth 1: 1 → 2
        assert_eq!(result.depths[1].len(), 1);
        assert_eq!(result.visited.len(), 3); // {0, 1, 2}
    }

    #[tokio::test]
    async fn batch_bfs_multiple_start_nodes() {
        let (pg, ids) = populated_graph(10).await;
        let result = pg.batch_bfs(&[ids[0], ids[5]], 1).await.unwrap();
        assert_eq!(result.depths.len(), 1);
        // Both start nodes expand: 0→1, 5→6
        assert_eq!(result.depths[0].len(), 2);
        assert_eq!(result.visited.len(), 4); // {0, 5, 1, 6}
    }

    #[tokio::test]
    async fn batch_bfs_cycle_terminates() {
        let pg = PersistentGraph::new(dummy_storage());
        let ns = Namespace::default_ns();
        let now = Timestamp::now();
        let a = MemoryId::new();
        let b = MemoryId::new();
        let c = MemoryId::new();
        pg.add_node(a, Layer::Episodic, 0.5, now, ns.clone())
            .await
            .unwrap();
        pg.add_node(b, Layer::Episodic, 0.5, now, ns.clone())
            .await
            .unwrap();
        pg.add_node(c, Layer::Episodic, 0.5, now, ns).await.unwrap();

        // a → b → c → a (cycle)
        pg.add_edge(a, b, EdgeRelation::Causes, 0.8, Metadata::default())
            .await
            .unwrap();
        pg.add_edge(b, c, EdgeRelation::Causes, 0.8, Metadata::default())
            .await
            .unwrap();
        pg.add_edge(c, a, EdgeRelation::Causes, 0.8, Metadata::default())
            .await
            .unwrap();

        let result = pg.batch_bfs(&[a], 10).await.unwrap();
        // BFS should visit a, b, c and then stop (cycle detection)
        assert_eq!(result.visited.len(), 3);
        assert!(result.depths.len() <= 3);
    }

    #[tokio::test]
    async fn batch_bfs_disconnected_graph() {
        let pg = PersistentGraph::new(dummy_storage());
        let ns = Namespace::default_ns();
        let now = Timestamp::now();
        let a = MemoryId::new();
        let b = MemoryId::new();
        let c = MemoryId::new(); // isolated
        pg.add_node(a, Layer::Episodic, 0.5, now, ns.clone())
            .await
            .unwrap();
        pg.add_node(b, Layer::Episodic, 0.5, now, ns.clone())
            .await
            .unwrap();
        pg.add_node(c, Layer::Episodic, 0.5, now, ns).await.unwrap();
        pg.add_edge(a, b, EdgeRelation::Causes, 0.8, Metadata::default())
            .await
            .unwrap();

        let result = pg.batch_bfs(&[a], 5).await.unwrap();
        assert!(result.visited.contains(&a));
        assert!(result.visited.contains(&b));
        assert!(!result.visited.contains(&c)); // c is unreachable
    }

    #[tokio::test]
    async fn batch_bfs_filtered_causal_only() {
        let pg = PersistentGraph::new(dummy_storage());
        let ns = Namespace::default_ns();
        let now = Timestamp::now();
        let a = MemoryId::new();
        let b = MemoryId::new();
        let c = MemoryId::new();
        pg.add_node(a, Layer::Episodic, 0.5, now, ns.clone())
            .await
            .unwrap();
        pg.add_node(b, Layer::Episodic, 0.5, now, ns.clone())
            .await
            .unwrap();
        pg.add_node(c, Layer::Episodic, 0.5, now, ns).await.unwrap();
        pg.add_edge(a, b, EdgeRelation::Causes, 0.8, Metadata::default())
            .await
            .unwrap();
        pg.add_edge(a, c, EdgeRelation::TemporalNext, 0.8, Metadata::default())
            .await
            .unwrap();

        // BFS filtered to Causes only
        let result = pg
            .batch_bfs_filtered(&[a], 2, Some(EdgeRelation::Causes))
            .await
            .unwrap();
        assert!(result.visited.contains(&b));
        assert!(!result.visited.contains(&c)); // c only reachable via TemporalNext
    }

    #[tokio::test]
    async fn batch_bfs_filtered_scoped_blocks_hidden_targets() {
        let pg = PersistentGraph::new(dummy_storage());
        let visible_ns = Namespace::new("visible").unwrap();
        let hidden_ns = Namespace::new("hidden").unwrap();
        let now = Timestamp::now();
        let a = MemoryId::new();
        let b = MemoryId::new();
        let c = MemoryId::new();
        pg.add_node(a, Layer::Episodic, 0.5, now, visible_ns)
            .await
            .unwrap();
        pg.add_node(b, Layer::Episodic, 0.5, now, hidden_ns)
            .await
            .unwrap();
        pg.add_node(c, Layer::Episodic, 0.5, now, visible_ns)
            .await
            .unwrap();
        pg.add_edge(a, b, EdgeRelation::Causes, 0.8, Metadata::default())
            .await
            .unwrap();
        pg.add_edge(b, c, EdgeRelation::Causes, 0.8, Metadata::default())
            .await
            .unwrap();

        let result = pg
            .batch_bfs_filtered_scoped(&[a], 3, Some(EdgeRelation::Causes), Some(&[visible_ns]))
            .await
            .unwrap();

        assert!(result.visited.contains(&a));
        assert!(!result.visited.contains(&b));
        assert!(!result.visited.contains(&c));
        assert_eq!(result.total_edges(), 0);
    }

    #[tokio::test]
    async fn deep_causal_bfs_scoped_does_not_traverse_hidden_bridges() {
        let pg = PersistentGraph::new(dummy_storage());
        let visible_ns = Namespace::new("visible_causal").unwrap();
        let hidden_ns = Namespace::new("hidden_causal").unwrap();
        let now = Timestamp::now();
        let a = MemoryId::new();
        let b = MemoryId::new();
        let c = MemoryId::new();
        pg.add_node(a, Layer::Episodic, 0.5, now, visible_ns)
            .await
            .unwrap();
        pg.add_node(b, Layer::Episodic, 0.5, now, hidden_ns)
            .await
            .unwrap();
        pg.add_node(c, Layer::Episodic, 0.5, now, visible_ns)
            .await
            .unwrap();
        pg.add_edge(a, b, EdgeRelation::Causes, 0.9, Metadata::default())
            .await
            .unwrap();
        pg.add_edge(b, c, EdgeRelation::Causes, 0.9, Metadata::default())
            .await
            .unwrap();

        let rows = pg
            .deep_causal_bfs(&[a], 3, 0.0, EdgeRelation::Causes, Some(&[visible_ns]))
            .await
            .unwrap();

        assert!(rows.is_empty());
    }

    #[tokio::test]
    async fn bfs_result_all_targets() {
        let (pg, ids) = populated_graph(5).await;
        let result = pg.batch_bfs(&[ids[0]], 3).await.unwrap();
        let targets = result.all_targets();
        assert!(targets.contains(&ids[1]));
        assert!(targets.contains(&ids[2]));
        assert!(targets.contains(&ids[3]));
    }

    #[tokio::test]
    async fn bfs_result_total_edges() {
        let (pg, ids) = populated_graph(5).await;
        let result = pg.batch_bfs(&[ids[0]], 4).await.unwrap();
        assert_eq!(result.total_edges(), 4); // 0→1, 1→2, 2→3, 3→4
    }
}