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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
//! # HAT Index Adapter
//!
//! Hierarchical Attention Tree - a novel index structure for AI memory.
//! Exploits known semantic hierarchy and temporal locality.
//!
//! Key insight: Unlike HNSW which learns topology from data,
//! HAT uses KNOWN hierarchy (session → document → chunk).
//!
//! Query complexity: O(log n) via tree descent
//! Insert complexity: O(log n) with incremental centroid updates

use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

use crate::core::{Id, Point};
use crate::core::proximity::Proximity;
use crate::core::merge::Merge;
use crate::ports::{Near, NearError, NearResult, SearchResult};

use super::consolidation::{
    Consolidate, ConsolidationConfig, ConsolidationPhase, ConsolidationState,
    ConsolidationMetrics, ConsolidationProgress, ConsolidationTickResult,
    compute_exact_centroid, centroid_drift,
};

/// Centroid computation method
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CentroidMethod {
    /// Euclidean mean + renormalize (fast but geometrically imprecise)
    Euclidean,
    /// Fréchet mean on hypersphere (manifold-aware, more accurate)
    Frechet,
}

impl Default for CentroidMethod {
    fn default() -> Self {
        CentroidMethod::Euclidean
    }
}

/// HAT configuration parameters
#[derive(Debug, Clone)]
pub struct HatConfig {
    /// Maximum children per container before splitting
    pub max_children: usize,

    /// Minimum children to maintain (for merging)
    pub min_children: usize,

    /// Number of branches to explore at each level (beam width)
    pub beam_width: usize,

    /// Weight for temporal proximity in scoring (0.0 = pure semantic)
    pub temporal_weight: f32,

    /// Time decay factor (higher = faster decay)
    pub time_decay: f32,

    /// Threshold for sparse centroid propagation (0.0 = always propagate)
    /// Only propagate to parent if centroid change magnitude exceeds this
    pub propagation_threshold: f32,

    /// Method for computing centroids
    pub centroid_method: CentroidMethod,

    /// Number of iterations for Fréchet mean computation
    pub frechet_iterations: usize,

    /// Enable subspace-aware routing (default: false for backward compatibility)
    pub subspace_enabled: bool,

    /// Configuration for subspace representation
    pub subspace_config: super::subspace::SubspaceConfig,

    /// Enable learnable routing (default: false for backward compatibility)
    pub learnable_routing_enabled: bool,

    /// Configuration for learnable routing
    pub learnable_routing_config: super::learnable_routing::LearnableRoutingConfig,
}

impl Default for HatConfig {
    fn default() -> Self {
        Self {
            max_children: 50,
            min_children: 5,
            beam_width: 3,
            temporal_weight: 0.0, // Start with pure semantic
            time_decay: 0.001,
            propagation_threshold: 0.0, // Default: always propagate (backward compatible)
            centroid_method: CentroidMethod::Euclidean, // Default: backward compatible
            frechet_iterations: 5, // Enough for convergence on hypersphere
            subspace_enabled: false, // Default: disabled for backward compatibility
            subspace_config: super::subspace::SubspaceConfig::default(),
            learnable_routing_enabled: false, // Default: disabled for backward compatibility
            learnable_routing_config: super::learnable_routing::LearnableRoutingConfig::default(),
        }
    }
}

impl HatConfig {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_beam_width(mut self, width: usize) -> Self {
        self.beam_width = width;
        self
    }

    pub fn with_temporal_weight(mut self, weight: f32) -> Self {
        self.temporal_weight = weight;
        self
    }

    pub fn with_propagation_threshold(mut self, threshold: f32) -> Self {
        self.propagation_threshold = threshold;
        self
    }

    pub fn with_centroid_method(mut self, method: CentroidMethod) -> Self {
        self.centroid_method = method;
        self
    }

    pub fn with_frechet_iterations(mut self, iterations: usize) -> Self {
        self.frechet_iterations = iterations;
        self
    }

    pub fn with_subspace_enabled(mut self, enabled: bool) -> Self {
        self.subspace_enabled = enabled;
        self
    }

    pub fn with_subspace_config(mut self, config: super::subspace::SubspaceConfig) -> Self {
        self.subspace_config = config;
        self.subspace_enabled = true;  // Automatically enable when config is provided
        self
    }

    pub fn with_learnable_routing_enabled(mut self, enabled: bool) -> Self {
        self.learnable_routing_enabled = enabled;
        self
    }

    pub fn with_learnable_routing_config(mut self, config: super::learnable_routing::LearnableRoutingConfig) -> Self {
        self.learnable_routing_config = config;
        self.learnable_routing_enabled = true;  // Automatically enable when config is provided
        self
    }
}

/// Level in the hierarchy
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContainerLevel {
    /// Root level - single global container
    Global,
    /// Session level - conversation/context boundaries
    Session,
    /// Document level - logical groupings within session
    Document,
    /// Chunk level - leaf nodes, actual attention states
    Chunk,
}

impl ContainerLevel {
    fn child_level(&self) -> Option<ContainerLevel> {
        match self {
            ContainerLevel::Global => Some(ContainerLevel::Session),
            ContainerLevel::Session => Some(ContainerLevel::Document),
            ContainerLevel::Document => Some(ContainerLevel::Chunk),
            ContainerLevel::Chunk => None,
        }
    }

    fn depth(&self) -> usize {
        match self {
            ContainerLevel::Global => 0,
            ContainerLevel::Session => 1,
            ContainerLevel::Document => 2,
            ContainerLevel::Chunk => 3,
        }
    }
}

/// Summary of a session for coarse queries (multi-resolution API)
#[derive(Debug, Clone)]
pub struct SessionSummary {
    /// Session ID
    pub id: Id,

    /// Similarity score to query
    pub score: f32,

    /// Number of chunks in this session
    pub chunk_count: usize,

    /// Session timestamp
    pub timestamp: u64,
}

/// Summary of a document for coarse queries
#[derive(Debug, Clone)]
pub struct DocumentSummary {
    /// Document ID
    pub id: Id,

    /// Similarity score to query
    pub score: f32,

    /// Number of chunks in this document
    pub chunk_count: usize,

    /// Document timestamp
    pub timestamp: u64,
}

/// A container in the HAT hierarchy
#[derive(Debug, Clone)]
struct Container {
    /// Unique identifier
    id: Id,

    /// Level in hierarchy
    level: ContainerLevel,

    /// Centroid (mean of children)
    centroid: Point,

    /// Creation timestamp (ms since epoch)
    timestamp: u64,

    /// Child container IDs (empty for chunks)
    children: Vec<Id>,

    /// Number of descendant chunks (for weighted centroid updates)
    descendant_count: usize,

    /// Accumulated sum of all descendant points (for Euclidean centroid)
    /// Stored as unnormalized to enable incremental updates
    accumulated_sum: Option<Point>,

    /// Subspace representation (optional, for non-chunk containers)
    /// Captures variance/spread of points within the container
    subspace: Option<super::subspace::Subspace>,
}

impl Container {
    fn new(id: Id, level: ContainerLevel, centroid: Point) -> Self {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64;

        // For chunks, the accumulated sum is the point itself
        let accumulated_sum = if level == ContainerLevel::Chunk {
            Some(centroid.clone())
        } else {
            None
        };

        // Initialize subspace for non-chunk containers
        let subspace = if level != ContainerLevel::Chunk {
            Some(super::subspace::Subspace::new(centroid.dimensionality()))
        } else {
            None
        };

        Self {
            id,
            level,
            centroid,
            timestamp,
            children: Vec::new(),
            descendant_count: if level == ContainerLevel::Chunk { 1 } else { 0 },
            accumulated_sum,
            subspace,
        }
    }

    fn is_leaf(&self) -> bool {
        self.level == ContainerLevel::Chunk
    }
}

/// Hierarchical Attention Tree Index
pub struct HatIndex {
    /// All containers (including root, sessions, documents, chunks)
    containers: HashMap<Id, Container>,

    /// Root container ID
    root_id: Option<Id>,

    /// Current active session (where new documents go)
    active_session: Option<Id>,

    /// Current active document (where new chunks go)
    active_document: Option<Id>,

    /// Expected dimensionality
    dimensionality: usize,

    /// Proximity function
    proximity: Arc<dyn Proximity>,

    /// Merge function (for centroids)
    merge: Arc<dyn Merge>,

    /// Whether higher proximity = more similar
    higher_is_better: bool,

    /// Configuration
    config: HatConfig,

    /// Consolidation state (None if not consolidating)
    consolidation_state: Option<ConsolidationState>,

    /// Cache of child points during consolidation
    consolidation_points_cache: HashMap<Id, Vec<Point>>,

    /// Learnable router for adaptive routing weights
    learnable_router: Option<super::learnable_routing::LearnableRouter>,
}

impl HatIndex {
    /// Create a new HAT index with cosine similarity
    pub fn cosine(dimensionality: usize) -> Self {
        use crate::core::proximity::Cosine;
        use crate::core::merge::Mean;
        Self::new(
            dimensionality,
            Arc::new(Cosine),
            Arc::new(Mean),
            true,
            HatConfig::default(),
        )
    }

    /// Create with custom config
    pub fn with_config(mut self, config: HatConfig) -> Self {
        // Initialize learnable router if enabled
        if config.learnable_routing_enabled {
            self.learnable_router = Some(super::learnable_routing::LearnableRouter::new(
                self.dimensionality,
                config.learnable_routing_config.clone(),
            ));
        }
        self.config = config;
        self
    }

    /// Create with custom proximity and merge functions
    pub fn new(
        dimensionality: usize,
        proximity: Arc<dyn Proximity>,
        merge: Arc<dyn Merge>,
        higher_is_better: bool,
        config: HatConfig,
    ) -> Self {
        // Initialize learnable router if enabled
        let learnable_router = if config.learnable_routing_enabled {
            Some(super::learnable_routing::LearnableRouter::new(
                dimensionality,
                config.learnable_routing_config.clone(),
            ))
        } else {
            None
        };

        Self {
            containers: HashMap::new(),
            root_id: None,
            active_session: None,
            active_document: None,
            dimensionality,
            proximity,
            merge,
            higher_is_better,
            config,
            consolidation_state: None,
            consolidation_points_cache: HashMap::new(),
            learnable_router,
        }
    }

    /// Compute distance (lower = more similar)
    fn distance(&self, a: &Point, b: &Point) -> f32 {
        let prox = self.proximity.proximity(a, b);
        if self.higher_is_better {
            1.0 - prox
        } else {
            prox
        }
    }

    /// Compute temporal distance (normalized to 0-1)
    fn temporal_distance(&self, t1: u64, t2: u64) -> f32 {
        let diff = (t1 as i64 - t2 as i64).unsigned_abs() as f64;
        // Exponential decay: e^(-λ * diff)
        // diff is in milliseconds, normalize to hours
        let hours = diff / (1000.0 * 60.0 * 60.0);
        (1.0 - (-self.config.time_decay as f64 * hours).exp()) as f32
    }

    /// Combined distance with temporal component, optional subspace, and learnable routing
    fn combined_distance(&self, query: &Point, query_time: u64, container: &Container) -> f32 {
        // Compute semantic distance
        let semantic = if self.config.learnable_routing_enabled {
            // Use learnable routing weights
            if let Some(ref router) = self.learnable_router {
                // weighted_similarity returns similarity (higher = better)
                // convert to distance (lower = better)
                let sim = router.weighted_similarity(query, &container.centroid);
                1.0 - sim
            } else {
                self.distance(query, &container.centroid)
            }
        } else if self.config.subspace_enabled && !container.is_leaf() {
            // Use subspace-aware similarity if available
            if let Some(ref subspace) = container.subspace {
                // combined_subspace_similarity returns similarity (higher = better)
                // convert to distance (lower = better)
                let sim = super::subspace::combined_subspace_similarity(
                    query, subspace, &self.config.subspace_config
                );
                1.0 - sim
            } else {
                self.distance(query, &container.centroid)
            }
        } else {
            self.distance(query, &container.centroid)
        };

        let temporal = self.temporal_distance(query_time, container.timestamp);

        // Weighted combination
        let w = self.config.temporal_weight;
        semantic * (1.0 - w) + temporal * w
    }

    /// Ensure root exists
    fn ensure_root(&mut self) {
        if self.root_id.is_none() {
            let root = Container::new(
                Id::now(),
                ContainerLevel::Global,
                Point::origin(self.dimensionality),
            );
            let root_id = root.id;
            self.containers.insert(root_id, root);
            self.root_id = Some(root_id);
        }
    }

    /// Ensure active session exists
    fn ensure_session(&mut self) {
        self.ensure_root();

        if self.active_session.is_none() {
            let session = Container::new(
                Id::now(),
                ContainerLevel::Session,
                Point::origin(self.dimensionality),
            );
            let session_id = session.id;
            self.containers.insert(session_id, session);

            // Add to root's children
            if let Some(root_id) = self.root_id {
                if let Some(root) = self.containers.get_mut(&root_id) {
                    root.children.push(session_id);
                }
            }

            self.active_session = Some(session_id);
        }
    }

    /// Ensure active document exists
    fn ensure_document(&mut self) {
        self.ensure_session();

        if self.active_document.is_none() {
            let document = Container::new(
                Id::now(),
                ContainerLevel::Document,
                Point::origin(self.dimensionality),
            );
            let doc_id = document.id;
            self.containers.insert(doc_id, document);

            // Add to session's children
            if let Some(session_id) = self.active_session {
                if let Some(session) = self.containers.get_mut(&session_id) {
                    session.children.push(doc_id);
                }
            }

            self.active_document = Some(doc_id);
        }
    }

    /// Start a new session (call this to create session boundaries)
    pub fn new_session(&mut self) {
        self.active_session = None;
        self.active_document = None;
    }

    /// Start a new document within current session
    pub fn new_document(&mut self) {
        self.active_document = None;
    }

    /// Compute Fréchet mean on the unit hypersphere using iterative algorithm
    /// This finds the point that minimizes sum of squared geodesic distances
    fn compute_frechet_mean(&self, points: &[Point], initial: &Point) -> Point {
        let mut mean = initial.clone();
        let iterations = self.config.frechet_iterations;

        for _ in 0..iterations {
            // Compute weighted tangent vectors (log map)
            let mut tangent_sum = vec![0.0f32; mean.dimensionality()];

            for point in points {
                // Log map: project point onto tangent space at mean
                // For unit sphere: log_p(q) = θ * (q - (q·p)p) / ||q - (q·p)p||
                // where θ = arccos(p·q)
                let dot: f32 = mean.dims().iter()
                    .zip(point.dims().iter())
                    .map(|(a, b)| a * b)
                    .sum();

                // Clamp dot product to valid range for arccos
                let dot_clamped = dot.clamp(-1.0, 1.0);
                let theta = dot_clamped.acos();

                if theta.abs() < 1e-8 {
                    // Points are identical, tangent vector is zero
                    continue;
                }

                // Direction in tangent space
                let mut direction: Vec<f32> = point.dims().iter()
                    .zip(mean.dims().iter())
                    .map(|(q, p)| q - dot * p)
                    .collect();

                // Normalize direction
                let dir_norm: f32 = direction.iter().map(|x| x * x).sum::<f32>().sqrt();
                if dir_norm < 1e-8 {
                    continue;
                }

                for (i, d) in direction.iter_mut().enumerate() {
                    tangent_sum[i] += theta * (*d / dir_norm);
                }
            }

            // Average tangent vector
            let n = points.len() as f32;
            for t in tangent_sum.iter_mut() {
                *t /= n;
            }

            // Compute tangent vector magnitude
            let tangent_norm: f32 = tangent_sum.iter().map(|x| x * x).sum::<f32>().sqrt();

            if tangent_norm < 1e-8 {
                // Converged
                break;
            }

            // Exp map: move along geodesic from mean in tangent direction
            // For unit sphere: exp_p(v) = cos(||v||)p + sin(||v||)(v/||v||)
            let cos_t = tangent_norm.cos();
            let sin_t = tangent_norm.sin();

            let new_dims: Vec<f32> = mean.dims().iter()
                .zip(tangent_sum.iter())
                .map(|(p, v)| cos_t * p + sin_t * (v / tangent_norm))
                .collect();

            mean = Point::new(new_dims);
        }

        // Ensure result is normalized (on the unit sphere)
        mean.normalize()
    }

    /// Update centroid incrementally when adding a child
    /// Returns the magnitude of the change (for sparse propagation)
    fn update_centroid(&mut self, container_id: Id, new_point: &Point) -> f32 {
        let method = self.config.centroid_method;

        // First, extract what we need from the container
        let (old_centroid, n, accumulated_sum) = {
            if let Some(container) = self.containers.get(&container_id) {
                (
                    container.centroid.clone(),
                    container.descendant_count as f32,
                    container.accumulated_sum.clone(),
                )
            } else {
                return 0.0;
            }
        };

        // Handle first child case
        if n == 0.0 {
            if let Some(container) = self.containers.get_mut(&container_id) {
                container.centroid = new_point.clone();
                container.accumulated_sum = Some(new_point.clone());
                container.descendant_count += 1;
            }
            return f32::MAX; // Always propagate first point
        }

        // Compute new centroid based on method
        let (new_centroid, new_sum) = match method {
            CentroidMethod::Euclidean => {
                // Incremental Euclidean mean using accumulated sum
                let new_sum = if let Some(ref sum) = accumulated_sum {
                    sum.dims().iter()
                        .zip(new_point.dims().iter())
                        .map(|(s, p)| s + p)
                        .collect::<Vec<f32>>()
                } else {
                    new_point.dims().to_vec()
                };

                // Compute centroid as normalized mean
                let count = n + 1.0;
                let mean_dims: Vec<f32> = new_sum.iter().map(|s| s / count).collect();
                let centroid = Point::new(mean_dims).normalize();
                (centroid, Point::new(new_sum))
            }
            CentroidMethod::Frechet => {
                // Update accumulated sum
                let new_sum = if let Some(ref sum) = accumulated_sum {
                    sum.dims().iter()
                        .zip(new_point.dims().iter())
                        .map(|(s, p)| s + p)
                        .collect::<Vec<f32>>()
                } else {
                    new_point.dims().to_vec()
                };

                // For incremental Fréchet, use geodesic interpolation
                let new_count = n + 1.0;
                let weight = 1.0 / new_count;
                let centroid = Self::geodesic_interpolate_static(&old_centroid, new_point, weight);
                (centroid, Point::new(new_sum))
            }
        };

        // Now update the container
        let subspace_enabled = self.config.subspace_enabled;
        if let Some(container) = self.containers.get_mut(&container_id) {
            container.centroid = new_centroid.clone();
            container.accumulated_sum = Some(new_sum);
            container.descendant_count += 1;

            // Update subspace if enabled, incremental covariance is on, and not a chunk
            // When incremental_covariance is false (default), we skip the expensive
            // O(d²) outer product accumulation per insert, deferring to consolidation.
            if subspace_enabled
                && self.config.subspace_config.incremental_covariance
                && container.level != ContainerLevel::Chunk
            {
                if let Some(ref mut subspace) = container.subspace {
                    subspace.add_point(new_point);
                    // Principal directions recomputed during consolidation
                }
            }
        }

        // Calculate change magnitude (L2 norm of delta)
        let delta: f32 = old_centroid.dims()
            .iter()
            .zip(new_centroid.dims().iter())
            .map(|(old, new)| (new - old).powi(2))
            .sum::<f32>()
            .sqrt();

        delta
    }

    /// Static version of geodesic interpolation (no self reference needed)
    fn geodesic_interpolate_static(a: &Point, b: &Point, t: f32) -> Point {
        // Compute dot product
        let dot: f32 = a.dims().iter()
            .zip(b.dims().iter())
            .map(|(x, y)| x * y)
            .sum();

        // Clamp to valid range
        let dot_clamped = dot.clamp(-0.9999, 0.9999);
        let theta = dot_clamped.acos();

        if theta.abs() < 1e-8 {
            // Points are nearly identical
            return a.clone();
        }

        // Slerp formula: (sin((1-t)θ)/sin(θ)) * a + (sin(tθ)/sin(θ)) * b
        let sin_theta = theta.sin();
        let weight_a = ((1.0 - t) * theta).sin() / sin_theta;
        let weight_b = (t * theta).sin() / sin_theta;

        let result_dims: Vec<f32> = a.dims().iter()
            .zip(b.dims().iter())
            .map(|(x, y)| weight_a * x + weight_b * y)
            .collect();

        Point::new(result_dims).normalize()
    }

    /// Geodesic interpolation on the unit hypersphere (slerp)
    /// Returns a point t fraction of the way from a to b along the great circle
    fn geodesic_interpolate(&self, a: &Point, b: &Point, t: f32) -> Point {
        // Compute dot product
        let dot: f32 = a.dims().iter()
            .zip(b.dims().iter())
            .map(|(x, y)| x * y)
            .sum();

        // Clamp to valid range
        let dot_clamped = dot.clamp(-0.9999, 0.9999);
        let theta = dot_clamped.acos();

        if theta.abs() < 1e-8 {
            // Points are nearly identical
            return a.clone();
        }

        // Slerp formula: (sin((1-t)θ)/sin(θ)) * a + (sin(tθ)/sin(θ)) * b
        let sin_theta = theta.sin();
        let weight_a = ((1.0 - t) * theta).sin() / sin_theta;
        let weight_b = (t * theta).sin() / sin_theta;

        let result_dims: Vec<f32> = a.dims().iter()
            .zip(b.dims().iter())
            .map(|(x, y)| weight_a * x + weight_b * y)
            .collect();

        Point::new(result_dims).normalize()
    }

    /// Sparse propagation: only update parent if change exceeds threshold
    fn propagate_centroid_update(
        &mut self,
        container_id: Id,
        new_point: &Point,
        ancestors: &[Id],
    ) {
        let threshold = self.config.propagation_threshold;
        let mut delta = self.update_centroid(container_id, new_point);

        // Propagate up the tree if delta exceeds threshold
        for ancestor_id in ancestors {
            if delta < threshold {
                break; // Stop propagation - change too small
            }
            delta = self.update_centroid(*ancestor_id, new_point);
        }
    }

    /// Search the tree from a starting container
    fn search_tree(
        &self,
        query: &Point,
        query_time: u64,
        start_id: Id,
        k: usize,
    ) -> Vec<(Id, f32)> {
        let mut results: Vec<(Id, f32)> = Vec::new();

        // Adaptive beam width based on k
        let beam_width = self.config.beam_width.max(k);

        // BFS with beam search
        let mut current_level = vec![start_id];

        while !current_level.is_empty() {
            let mut next_level: Vec<(Id, f32)> = Vec::new();

            for container_id in &current_level {
                if let Some(container) = self.containers.get(container_id) {
                    if container.is_leaf() {
                        // Leaf node - add to results
                        let dist = self.combined_distance(query, query_time, container);
                        results.push((*container_id, dist));
                    } else {
                        // Internal node - score children and add to next level
                        for child_id in &container.children {
                            if let Some(child) = self.containers.get(child_id) {
                                let dist = self.combined_distance(query, query_time, child);
                                next_level.push((*child_id, dist));
                            }
                        }
                    }
                }
            }

            if next_level.is_empty() {
                break;
            }

            // Sort by distance and take beam_width best
            next_level.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
            current_level = next_level
                .into_iter()
                .take(beam_width)
                .map(|(id, _)| id)
                .collect();
        }

        // Sort results and return top k
        results.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
        results.truncate(k);
        results
    }

    // =========================================================================
    // Multi-Resolution Query API (inspired by VAR next-scale prediction)
    // =========================================================================

    /// Coarse query: Get session summaries without descending to chunks
    /// Use this for fast "is there relevant memory?" checks
    pub fn near_sessions(&self, query: &Point, k: usize) -> NearResult<Vec<SessionSummary>> {
        if query.dimensionality() != self.dimensionality {
            return Err(NearError::DimensionalityMismatch {
                expected: self.dimensionality,
                got: query.dimensionality(),
            });
        }

        let root_id = match self.root_id {
            Some(id) => id,
            None => return Ok(vec![]),
        };

        let query_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64;

        // Get root's children (sessions)
        let root = match self.containers.get(&root_id) {
            Some(r) => r,
            None => return Ok(vec![]),
        };

        let mut sessions: Vec<SessionSummary> = root.children
            .iter()
            .filter_map(|session_id| {
                let session = self.containers.get(session_id)?;
                if session.level != ContainerLevel::Session {
                    return None;
                }
                let dist = self.combined_distance(query, query_time, session);
                let score = if self.higher_is_better { 1.0 - dist } else { dist };

                Some(SessionSummary {
                    id: *session_id,
                    score,
                    chunk_count: session.descendant_count,
                    timestamp: session.timestamp,
                })
            })
            .collect();

        // Sort by score (higher is better)
        sessions.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap());
        sessions.truncate(k);

        Ok(sessions)
    }

    /// Refine within a specific session: Get document summaries
    pub fn near_documents(&self, session_id: Id, query: &Point, k: usize) -> NearResult<Vec<DocumentSummary>> {
        if query.dimensionality() != self.dimensionality {
            return Err(NearError::DimensionalityMismatch {
                expected: self.dimensionality,
                got: query.dimensionality(),
            });
        }

        let query_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64;

        let session = match self.containers.get(&session_id) {
            Some(s) => s,
            None => return Ok(vec![]),
        };

        let mut documents: Vec<DocumentSummary> = session.children
            .iter()
            .filter_map(|doc_id| {
                let doc = self.containers.get(doc_id)?;
                if doc.level != ContainerLevel::Document {
                    return None;
                }
                let dist = self.combined_distance(query, query_time, doc);
                let score = if self.higher_is_better { 1.0 - dist } else { dist };

                Some(DocumentSummary {
                    id: *doc_id,
                    score,
                    chunk_count: doc.descendant_count,
                    timestamp: doc.timestamp,
                })
            })
            .collect();

        documents.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap());
        documents.truncate(k);

        Ok(documents)
    }

    /// Refine within a specific document: Get chunk results
    pub fn near_in_document(&self, doc_id: Id, query: &Point, k: usize) -> NearResult<Vec<SearchResult>> {
        if query.dimensionality() != self.dimensionality {
            return Err(NearError::DimensionalityMismatch {
                expected: self.dimensionality,
                got: query.dimensionality(),
            });
        }

        let query_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64;

        let doc = match self.containers.get(&doc_id) {
            Some(d) => d,
            None => return Ok(vec![]),
        };

        let mut chunks: Vec<SearchResult> = doc.children
            .iter()
            .filter_map(|chunk_id| {
                let chunk = self.containers.get(chunk_id)?;
                if chunk.level != ContainerLevel::Chunk {
                    return None;
                }
                let dist = self.combined_distance(query, query_time, chunk);
                let score = if self.higher_is_better { 1.0 - dist } else { dist };

                Some(SearchResult::new(*chunk_id, score))
            })
            .collect();

        chunks.sort_by(|a, b| b.score.partial_cmp(&a.score).unwrap());
        chunks.truncate(k);

        Ok(chunks)
    }

    /// Get statistics about the tree structure
    pub fn stats(&self) -> HatStats {
        let mut stats = HatStats::default();

        for container in self.containers.values() {
            match container.level {
                ContainerLevel::Global => stats.global_count += 1,
                ContainerLevel::Session => stats.session_count += 1,
                ContainerLevel::Document => stats.document_count += 1,
                ContainerLevel::Chunk => stats.chunk_count += 1,
            }
        }

        stats
    }

    // =========================================================================
    // Learnable Routing API
    // =========================================================================

    /// Record positive feedback for a query result (successful retrieval)
    ///
    /// Call this when a retrieved result was useful/relevant.
    /// The router learns to route similar queries to similar containers.
    pub fn record_retrieval_success(&mut self, query: &Point, result_id: Id) {
        if let Some(ref mut router) = self.learnable_router {
            // Find the container for this result and record feedback for each level
            if let Some(container) = self.containers.get(&result_id) {
                router.record_success(query, &container.centroid, container.level.depth());
            }
        }
    }

    /// Record negative feedback for a query result (unsuccessful retrieval)
    ///
    /// Call this when a retrieved result was not useful/relevant.
    pub fn record_retrieval_failure(&mut self, query: &Point, result_id: Id) {
        if let Some(ref mut router) = self.learnable_router {
            if let Some(container) = self.containers.get(&result_id) {
                router.record_failure(query, &container.centroid, container.level.depth());
            }
        }
    }

    /// Record implicit feedback with a relevance score (0.0 = irrelevant, 1.0 = highly relevant)
    ///
    /// Use this for continuous feedback signals like click-through rate, dwell time, etc.
    pub fn record_implicit_feedback(&mut self, query: &Point, result_id: Id, relevance: f32) {
        if let Some(ref mut router) = self.learnable_router {
            if let Some(container) = self.containers.get(&result_id) {
                router.record_implicit(query, &container.centroid, container.level.depth(), relevance);
            }
        }
    }

    /// Get learnable router statistics (if enabled)
    pub fn router_stats(&self) -> Option<super::learnable_routing::RouterStats> {
        self.learnable_router.as_ref().map(|r| r.stats())
    }

    /// Get current routing weights (if learnable routing is enabled)
    pub fn routing_weights(&self) -> Option<&[f32]> {
        self.learnable_router.as_ref().map(|r| r.weights())
    }

    /// Reset learnable routing weights to uniform
    pub fn reset_routing_weights(&mut self) {
        if let Some(ref mut router) = self.learnable_router {
            router.reset_weights();
        }
    }

    /// Check if learnable routing is enabled
    pub fn is_learnable_routing_enabled(&self) -> bool {
        self.learnable_router.is_some()
    }
}

/// Statistics about the HAT tree structure
#[derive(Debug, Clone, Default)]
pub struct HatStats {
    pub global_count: usize,
    pub session_count: usize,
    pub document_count: usize,
    pub chunk_count: usize,
}

impl Near for HatIndex {
    fn near(&self, query: &Point, k: usize) -> NearResult<Vec<SearchResult>> {
        // Check dimensionality
        if query.dimensionality() != self.dimensionality {
            return Err(NearError::DimensionalityMismatch {
                expected: self.dimensionality,
                got: query.dimensionality(),
            });
        }

        // Handle empty index
        let root_id = match self.root_id {
            Some(id) => id,
            None => return Ok(vec![]),
        };

        // Current time for temporal scoring
        let query_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64;

        // Search tree
        let results = self.search_tree(query, query_time, root_id, k);

        // Convert to SearchResult
        let search_results: Vec<SearchResult> = results
            .into_iter()
            .map(|(id, dist)| {
                let score = if self.higher_is_better {
                    1.0 - dist
                } else {
                    dist
                };
                SearchResult::new(id, score)
            })
            .collect();

        Ok(search_results)
    }

    fn within(&self, query: &Point, threshold: f32) -> NearResult<Vec<SearchResult>> {
        // Check dimensionality
        if query.dimensionality() != self.dimensionality {
            return Err(NearError::DimensionalityMismatch {
                expected: self.dimensionality,
                got: query.dimensionality(),
            });
        }

        // Use near with all points, then filter
        let all_results = self.near(query, self.containers.len())?;

        let filtered: Vec<SearchResult> = all_results
            .into_iter()
            .filter(|r| {
                if self.higher_is_better {
                    r.score >= threshold
                } else {
                    r.score <= threshold
                }
            })
            .collect();

        Ok(filtered)
    }

    fn add(&mut self, id: Id, point: &Point) -> NearResult<()> {
        // Check dimensionality
        if point.dimensionality() != self.dimensionality {
            return Err(NearError::DimensionalityMismatch {
                expected: self.dimensionality,
                got: point.dimensionality(),
            });
        }

        // Ensure hierarchy exists
        self.ensure_document();

        // Create chunk container
        let chunk = Container::new(id, ContainerLevel::Chunk, point.clone());
        self.containers.insert(id, chunk);

        // Add to document's children
        if let Some(doc_id) = self.active_document {
            if let Some(doc) = self.containers.get_mut(&doc_id) {
                doc.children.push(id);
            }

            // Build ancestor chain for sparse propagation
            let mut ancestors = Vec::new();
            if let Some(session_id) = self.active_session {
                ancestors.push(session_id);
                if let Some(root_id) = self.root_id {
                    ancestors.push(root_id);
                }
            }

            // Sparse propagation: only update ancestors if change is significant
            self.propagate_centroid_update(doc_id, point, &ancestors);
        }

        // Check if document needs splitting
        if let Some(doc_id) = self.active_document {
            if let Some(doc) = self.containers.get(&doc_id) {
                if doc.children.len() >= self.config.max_children {
                    // Start a new document
                    self.new_document();
                }
            }
        }

        // Check if session needs splitting
        if let Some(session_id) = self.active_session {
            if let Some(session) = self.containers.get(&session_id) {
                if session.children.len() >= self.config.max_children {
                    // Start a new session
                    self.new_session();
                }
            }
        }

        Ok(())
    }

    fn remove(&mut self, id: Id) -> NearResult<()> {
        // Remove the chunk
        self.containers.remove(&id);

        // Note: We don't update centroids on remove for simplicity
        // A production implementation would need to handle this

        Ok(())
    }

    fn rebuild(&mut self) -> NearResult<()> {
        // Recalculate all centroids from scratch
        // For now, this is a no-op since we maintain incrementally
        Ok(())
    }

    fn is_ready(&self) -> bool {
        true
    }

    fn len(&self) -> usize {
        // Count only chunk-level containers
        self.containers.values()
            .filter(|c| c.level == ContainerLevel::Chunk)
            .count()
    }
}

// =============================================================================
// Consolidation Implementation
// =============================================================================

impl HatIndex {
    /// Collect all leaf points for a container (recursively)
    fn collect_leaf_points(&self, container_id: Id) -> Vec<Point> {
        let container = match self.containers.get(&container_id) {
            Some(c) => c,
            None => return vec![],
        };

        if container.is_leaf() {
            return vec![container.centroid.clone()];
        }

        let mut points = Vec::new();
        for child_id in &container.children {
            points.extend(self.collect_leaf_points(*child_id));
        }
        points
    }

    /// Get all container IDs at a given level
    fn containers_at_level(&self, level: ContainerLevel) -> Vec<Id> {
        self.containers
            .iter()
            .filter(|(_, c)| c.level == level)
            .map(|(id, _)| *id)
            .collect()
    }

    /// Recompute a container's centroid from its descendants
    fn recompute_centroid(&mut self, container_id: Id) -> Option<f32> {
        // First collect the points (need to release borrow)
        let points = self.collect_leaf_points(container_id);

        if points.is_empty() {
            return None;
        }

        let new_centroid = match compute_exact_centroid(&points) {
            Some(c) => c,
            None => return None,
        };

        // Get subspace config for recomputation
        let subspace_enabled = self.config.subspace_enabled;
        let subspace_rank = self.config.subspace_config.rank;

        // Now update the container
        let drift = if let Some(container) = self.containers.get_mut(&container_id) {
            let old_centroid = container.centroid.clone();
            let drift = centroid_drift(&old_centroid, &new_centroid);
            container.centroid = new_centroid;
            container.descendant_count = points.len();

            // Update accumulated sum
            let sum: Vec<f32> = points.iter()
                .fold(vec![0.0f32; self.dimensionality], |mut acc, p| {
                    for (i, &v) in p.dims().iter().enumerate() {
                        acc[i] += v;
                    }
                    acc
                });
            container.accumulated_sum = Some(Point::new(sum));

            // Recompute subspace during consolidation if enabled
            if subspace_enabled && container.level != ContainerLevel::Chunk {
                let mut subspace = super::subspace::Subspace::new(self.dimensionality);
                for point in &points {
                    subspace.add_point(point);
                }
                subspace.recompute_subspace(subspace_rank);
                container.subspace = Some(subspace);
            }

            Some(drift)
        } else {
            None
        };

        drift
    }

    /// Check if a container should be merged (too few children)
    fn should_merge(&self, container_id: Id, threshold: usize) -> bool {
        if let Some(container) = self.containers.get(&container_id) {
            // Don't merge chunks, root, or sessions (for now)
            if container.level == ContainerLevel::Chunk ||
               container.level == ContainerLevel::Global ||
               container.level == ContainerLevel::Session {
                return false;
            }
            container.children.len() < threshold
        } else {
            false
        }
    }

    /// Check if a container should be split (too many children)
    fn should_split(&self, container_id: Id, threshold: usize) -> bool {
        if let Some(container) = self.containers.get(&container_id) {
            // Don't split chunks
            if container.level == ContainerLevel::Chunk {
                return false;
            }
            container.children.len() > threshold
        } else {
            false
        }
    }

    /// Find a sibling container to merge with
    fn find_merge_sibling(&self, container_id: Id) -> Option<Id> {
        // Find parent
        let parent_id = self.containers.iter()
            .find(|(_, c)| c.children.contains(&container_id))
            .map(|(id, _)| *id)?;

        let parent = self.containers.get(&parent_id)?;

        // Find smallest sibling
        let mut smallest: Option<(Id, usize)> = None;
        for child_id in &parent.children {
            if *child_id == container_id {
                continue;
            }
            if let Some(child) = self.containers.get(child_id) {
                let size = child.children.len();
                if smallest.is_none() || size < smallest.unwrap().1 {
                    smallest = Some((*child_id, size));
                }
            }
        }

        smallest.map(|(id, _)| id)
    }

    /// Merge container B into container A
    fn merge_containers(&mut self, a_id: Id, b_id: Id) {
        // Get children from B
        let b_children: Vec<Id> = if let Some(b) = self.containers.get(&b_id) {
            b.children.clone()
        } else {
            return;
        };

        // Add children to A
        if let Some(a) = self.containers.get_mut(&a_id) {
            a.children.extend(b_children);
        }

        // Remove B from its parent's children
        let parent_id = self.containers.iter()
            .find(|(_, c)| c.children.contains(&b_id))
            .map(|(id, _)| *id);

        if let Some(pid) = parent_id {
            if let Some(parent) = self.containers.get_mut(&pid) {
                parent.children.retain(|id| *id != b_id);
            }
        }

        // Remove B
        self.containers.remove(&b_id);

        // Recompute A's centroid
        self.recompute_centroid(a_id);
    }

    /// Split a container into two
    fn split_container(&mut self, container_id: Id) -> Option<Id> {
        // Get container info
        let (level, children, parent_id) = {
            let container = self.containers.get(&container_id)?;
            let parent_id = self.containers.iter()
                .find(|(_, c)| c.children.contains(&container_id))
                .map(|(id, _)| *id);
            (container.level, container.children.clone(), parent_id)
        };

        if children.len() < 2 {
            return None;
        }

        // Simple split: divide children in half
        let mid = children.len() / 2;
        let (keep, move_to_new) = children.split_at(mid);

        // Create new container
        let new_id = Id::now();
        let new_container = Container::new(
            new_id,
            level,
            Point::origin(self.dimensionality),
        );
        self.containers.insert(new_id, new_container);

        // Update original container
        if let Some(container) = self.containers.get_mut(&container_id) {
            container.children = keep.to_vec();
        }

        // Set new container's children
        if let Some(new_container) = self.containers.get_mut(&new_id) {
            new_container.children = move_to_new.to_vec();
        }

        // Add new container to parent
        if let Some(pid) = parent_id {
            if let Some(parent) = self.containers.get_mut(&pid) {
                parent.children.push(new_id);
            }
        }

        // Recompute centroids
        self.recompute_centroid(container_id);
        self.recompute_centroid(new_id);

        Some(new_id)
    }

    /// Remove containers with no children (except chunks)
    fn prune_empty(&mut self) -> usize {
        let mut pruned = 0;

        loop {
            let empty_ids: Vec<Id> = self.containers
                .iter()
                .filter(|(_, c)| {
                    c.level != ContainerLevel::Chunk &&
                    c.level != ContainerLevel::Global &&
                    c.children.is_empty()
                })
                .map(|(id, _)| *id)
                .collect();

            if empty_ids.is_empty() {
                break;
            }

            for id in empty_ids {
                // Remove from parent's children
                let parent_id = self.containers.iter()
                    .find(|(_, c)| c.children.contains(&id))
                    .map(|(pid, _)| *pid);

                if let Some(pid) = parent_id {
                    if let Some(parent) = self.containers.get_mut(&pid) {
                        parent.children.retain(|cid| *cid != id);
                    }
                }

                self.containers.remove(&id);
                pruned += 1;
            }
        }

        pruned
    }
}

impl Consolidate for HatIndex {
    fn begin_consolidation(&mut self, config: ConsolidationConfig) {
        let mut state = ConsolidationState::new(config);
        state.start();

        // Initialize work queue with all containers for leaf collection
        let all_ids: VecDeque<Id> = self.containers.keys().copied().collect();
        state.work_queue = all_ids;

        self.consolidation_state = Some(state);
        self.consolidation_points_cache.clear();
    }

    fn consolidation_tick(&mut self) -> ConsolidationTickResult {
        // Take ownership of state to avoid borrow issues
        let mut state = match self.consolidation_state.take() {
            Some(s) => s,
            None => {
                return ConsolidationTickResult::Complete(ConsolidationMetrics::default());
            }
        };

        let batch_size = state.config.batch_size;

        match state.phase {
            ConsolidationPhase::Idle => {
                state.start();
            }

            ConsolidationPhase::CollectingLeaves => {
                state.next_phase();

                // Populate work queue with non-chunk containers (bottom-up)
                let docs = self.containers_at_level(ContainerLevel::Document);
                let sessions = self.containers_at_level(ContainerLevel::Session);
                let globals = self.containers_at_level(ContainerLevel::Global);

                state.work_queue.clear();
                state.work_queue.extend(docs);
                state.work_queue.extend(sessions);
                state.work_queue.extend(globals);
            }

            ConsolidationPhase::RecomputingCentroids => {
                let mut processed = 0;
                let mut to_recompute = Vec::new();

                while processed < batch_size {
                    match state.work_queue.pop_front() {
                        Some(id) => {
                            to_recompute.push(id);
                            state.processed.insert(id);
                            processed += 1;
                        }
                        None => break,
                    };
                }

                // Now recompute without holding state borrow
                for container_id in to_recompute {
                    if let Some(drift) = self.recompute_centroid(container_id) {
                        state.record_drift(drift);
                        state.metrics.centroids_recomputed += 1;
                    }
                    state.metrics.containers_processed += 1;
                }

                if state.work_queue.is_empty() {
                    state.next_phase();

                    if state.phase == ConsolidationPhase::AnalyzingStructure {
                        let docs = self.containers_at_level(ContainerLevel::Document);
                        state.work_queue.extend(docs);
                    }
                }
            }

            ConsolidationPhase::AnalyzingStructure => {
                let merge_threshold = state.config.merge_threshold;
                let split_threshold = state.config.split_threshold;
                let mut processed = 0;
                let mut to_analyze = Vec::new();

                while processed < batch_size {
                    match state.work_queue.pop_front() {
                        Some(id) => {
                            to_analyze.push(id);
                            state.processed.insert(id);
                            processed += 1;
                        }
                        None => break,
                    };
                }

                // Analyze without holding state borrow
                for container_id in to_analyze {
                    if self.should_merge(container_id, merge_threshold) {
                        if let Some(sibling) = self.find_merge_sibling(container_id) {
                            state.add_merge_candidate(container_id, sibling);
                        }
                    } else if self.should_split(container_id, split_threshold) {
                        state.add_split_candidate(container_id);
                    }
                }

                if state.work_queue.is_empty() {
                    state.next_phase();
                }
            }

            ConsolidationPhase::Merging => {
                let mut processed = 0;
                let mut to_merge = Vec::new();

                while processed < batch_size {
                    match state.next_merge() {
                        Some(pair) => {
                            to_merge.push(pair);
                            processed += 1;
                        }
                        None => break,
                    };
                }

                for (a, b) in to_merge {
                    self.merge_containers(a, b);
                    state.metrics.containers_merged += 1;
                }

                if !state.has_merges() {
                    state.next_phase();
                }
            }

            ConsolidationPhase::Splitting => {
                let mut processed = 0;
                let mut to_split = Vec::new();

                while processed < batch_size {
                    match state.next_split() {
                        Some(id) => {
                            to_split.push(id);
                            processed += 1;
                        }
                        None => break,
                    };
                }

                for container_id in to_split {
                    if self.split_container(container_id).is_some() {
                        state.metrics.containers_split += 1;
                    }
                }

                if !state.has_splits() {
                    state.next_phase();
                }
            }

            ConsolidationPhase::Pruning => {
                let pruned = self.prune_empty();
                state.metrics.containers_pruned = pruned;
                state.next_phase();
            }

            ConsolidationPhase::OptimizingLayout => {
                for container in self.containers.values_mut() {
                    if container.children.len() > 1 {
                        // Placeholder for future optimization
                    }
                }
                state.next_phase();
            }

            ConsolidationPhase::Complete => {
                // Already complete
            }
        }

        state.metrics.ticks += 1;

        if state.is_complete() {
            let metrics = state.metrics.clone();
            self.consolidation_points_cache.clear();
            ConsolidationTickResult::Complete(metrics)
        } else {
            let progress = state.progress();
            self.consolidation_state = Some(state);
            ConsolidationTickResult::Continue(progress)
        }
    }

    fn is_consolidating(&self) -> bool {
        self.consolidation_state.is_some()
    }

    fn consolidation_progress(&self) -> Option<ConsolidationProgress> {
        self.consolidation_state.as_ref().map(|s| s.progress())
    }

    fn cancel_consolidation(&mut self) {
        self.consolidation_state = None;
        self.consolidation_points_cache.clear();
    }
}

// =============================================================================
// Persistence Implementation
// =============================================================================

impl HatIndex {
    /// Serialize the index to bytes
    ///
    /// # Example
    /// ```rust,ignore
    /// let bytes = hat.to_bytes()?;
    /// std::fs::write("index.hat", bytes)?;
    /// ```
    pub fn to_bytes(&self) -> Result<Vec<u8>, super::persistence::PersistError> {
        use super::persistence::{SerializedHat, SerializedContainer, LevelByte};

        let containers: Vec<SerializedContainer> = self.containers.iter()
            .map(|(_, c)| {
                let level = match c.level {
                    ContainerLevel::Global => LevelByte::Root,
                    ContainerLevel::Session => LevelByte::Session,
                    ContainerLevel::Document => LevelByte::Document,
                    ContainerLevel::Chunk => LevelByte::Chunk,
                };

                SerializedContainer {
                    id: c.id,
                    level,
                    timestamp: c.timestamp,
                    children: c.children.clone(),
                    descendant_count: c.descendant_count as u64,
                    centroid: c.centroid.dims().to_vec(),
                    accumulated_sum: c.accumulated_sum.as_ref().map(|p| p.dims().to_vec()),
                }
            })
            .collect();

        let router_weights = self.learnable_router.as_ref()
            .map(|r| r.weights().to_vec());

        let serialized = SerializedHat {
            version: 1,
            dimensionality: self.dimensionality as u32,
            root_id: self.root_id,
            containers,
            active_session: self.active_session,
            active_document: self.active_document,
            router_weights,
        };

        serialized.to_bytes()
    }

    /// Deserialize an index from bytes
    ///
    /// # Example
    /// ```rust,ignore
    /// let bytes = std::fs::read("index.hat")?;
    /// let hat = HatIndex::from_bytes(&bytes)?;
    /// ```
    pub fn from_bytes(data: &[u8]) -> Result<Self, super::persistence::PersistError> {
        use super::persistence::{SerializedHat, LevelByte, PersistError};
        use crate::core::proximity::Cosine;
        use crate::core::merge::Mean;

        let serialized = SerializedHat::from_bytes(data)?;
        let dimensionality = serialized.dimensionality as usize;

        // Create a new index with default settings
        let mut index = Self::new(
            dimensionality,
            Arc::new(Cosine),
            Arc::new(Mean),
            true,
            HatConfig::default(),
        );

        // Restore containers
        for sc in serialized.containers {
            let level = match sc.level {
                LevelByte::Root => ContainerLevel::Global,
                LevelByte::Session => ContainerLevel::Session,
                LevelByte::Document => ContainerLevel::Document,
                LevelByte::Chunk => ContainerLevel::Chunk,
            };

            // Verify dimension
            if sc.centroid.len() != dimensionality {
                return Err(PersistError::DimensionMismatch {
                    expected: dimensionality,
                    found: sc.centroid.len(),
                });
            }

            let centroid = Point::new(sc.centroid);
            let accumulated_sum = sc.accumulated_sum.map(Point::new);

            let container = Container {
                id: sc.id,
                level,
                centroid,
                timestamp: sc.timestamp,
                children: sc.children,
                descendant_count: sc.descendant_count as usize,
                accumulated_sum,
                subspace: if level != ContainerLevel::Chunk {
                    Some(super::subspace::Subspace::new(dimensionality))
                } else {
                    None
                },
            };

            index.containers.insert(sc.id, container);
        }

        // Restore state
        index.root_id = serialized.root_id;
        index.active_session = serialized.active_session;
        index.active_document = serialized.active_document;

        // Restore router weights if present
        if let Some(weights) = serialized.router_weights {
            let mut router = super::learnable_routing::LearnableRouter::default_for_dims(dimensionality);
            let weight_bytes: Vec<u8> = weights.iter()
                .flat_map(|w| w.to_le_bytes())
                .collect();
            router.deserialize_weights(&weight_bytes)
                .map_err(|e| PersistError::Corrupted(e.to_string()))?;
            index.learnable_router = Some(router);
        }

        Ok(index)
    }

    /// Save the index to a file
    pub fn save_to_file(&self, path: &std::path::Path) -> Result<(), super::persistence::PersistError> {
        let bytes = self.to_bytes()?;
        std::fs::write(path, bytes)?;
        Ok(())
    }

    /// Load an index from a file
    pub fn load_from_file(path: &std::path::Path) -> Result<Self, super::persistence::PersistError> {
        let bytes = std::fs::read(path)?;
        Self::from_bytes(&bytes)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_hat_add() {
        let mut index = HatIndex::cosine(3);

        let id = Id::now();
        let point = Point::new(vec![1.0, 0.0, 0.0]);

        index.add(id, &point).unwrap();

        assert_eq!(index.len(), 1);
    }

    #[test]
    fn test_hat_near() {
        let mut index = HatIndex::cosine(3);

        // Add some points
        let points = vec![
            Point::new(vec![1.0, 0.0, 0.0]),
            Point::new(vec![0.0, 1.0, 0.0]),
            Point::new(vec![0.0, 0.0, 1.0]),
            Point::new(vec![0.7, 0.7, 0.0]).normalize(),
        ];

        for point in &points {
            index.add(Id::now(), point).unwrap();
        }

        // Query near [1, 0, 0]
        let query = Point::new(vec![1.0, 0.0, 0.0]);
        let results = index.near(&query, 2).unwrap();

        assert_eq!(results.len(), 2);
        // First result should have high similarity (close to 1.0)
        assert!(results[0].score > 0.5);
    }

    #[test]
    fn test_hat_sessions() {
        let mut index = HatIndex::cosine(3);

        // Add points to first session
        for i in 0..5 {
            let point = Point::new(vec![1.0, i as f32 * 0.1, 0.0]).normalize();
            index.add(Id::now(), &point).unwrap();
        }

        // Start new session
        index.new_session();

        // Add points to second session
        for i in 0..5 {
            let point = Point::new(vec![0.0, 1.0, i as f32 * 0.1]).normalize();
            index.add(Id::now(), &point).unwrap();
        }

        assert_eq!(index.len(), 10);

        // Query should find both sessions
        let query = Point::new(vec![0.5, 0.5, 0.0]).normalize();
        let results = index.near(&query, 5).unwrap();

        assert_eq!(results.len(), 5);
    }

    #[test]
    fn test_hat_hierarchy_structure() {
        let mut index = HatIndex::cosine(3);

        // Add some points
        for _ in 0..10 {
            let point = Point::new(vec![1.0, 0.0, 0.0]);
            index.add(Id::now(), &point).unwrap();
        }

        // Should have: 1 root + 1 session + 1 document + 10 chunks = 13 containers
        assert!(index.containers.len() >= 13);

        // Check that root exists
        assert!(index.root_id.is_some());
    }

    #[test]
    fn test_hat_empty() {
        let index = HatIndex::cosine(3);

        let query = Point::new(vec![1.0, 0.0, 0.0]);
        let results = index.near(&query, 5).unwrap();

        assert!(results.is_empty());
    }

    #[test]
    fn test_hat_dimensionality_check() {
        let mut index = HatIndex::cosine(3);

        let wrong_dims = Point::new(vec![1.0, 0.0]); // 2 dims
        let result = index.add(Id::now(), &wrong_dims);

        match result {
            Err(NearError::DimensionalityMismatch { expected, got }) => {
                assert_eq!(expected, 3);
                assert_eq!(got, 2);
            }
            _ => panic!("Expected DimensionalityMismatch error"),
        }
    }

    #[test]
    fn test_hat_scale() {
        let mut index = HatIndex::cosine(128);

        // Add 1000 points
        for i in 0..1000 {
            let mut dims = vec![0.0f32; 128];
            dims[i % 128] = 1.0;
            let point = Point::new(dims).normalize();
            index.add(Id::now(), &point).unwrap();
        }

        assert_eq!(index.len(), 1000);

        // Query should work
        let query = Point::new(vec![1.0; 128]).normalize();
        let results = index.near(&query, 10).unwrap();

        assert_eq!(results.len(), 10);
    }
}