astrodyn_frames 0.2.0

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

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

use crate::ref_frame_state::{RefFrameState, RefFrameStateTyped};
use astrodyn_quantities::frame::Frame;
use astrodyn_quantities::frame_descriptor::{FrameClass, FrameUid, MintPolicy, Namespace};
use astrodyn_quantities::quat::{LeftTransform, NormalizedQuat, ScalarFirst};
use astrodyn_quantities::time_scale::{SecondsSince, TDB};
use glam::DVec3;

/// Handle into the [`FrameTree`] arena.
pub type FrameId = usize;

/// A node in the frame tree.
#[derive(Debug, Clone)]
pub struct FrameNode {
    /// Human-readable name — **diagnostics only** (e.g., "Earth.inertial",
    /// "ISS.composite_body"). Identity lives in [`FrameNode::uid`]; no
    /// production logic keys on names (issue #664).
    pub name: String,
    /// State relative to parent. Identity for root frames.
    pub state: RefFrameState,
    /// Runtime frame identity — **required** since issue #664: every
    /// construction path mints one. Private: mutating it directly would
    /// desync the tree's identity index (`find`/`resolve`) — read via
    /// [`FrameNode::uid`]; stamping happens only through the typed
    /// constructors, [`FrameTree::add_child_uid`], or
    /// [`FrameTree::import_subtree`].
    uid: FrameUid,
    /// Frame epoch (TDB seconds): the time-validity of `state`. `None`
    /// until the host's per-step stamping writes it (issue #662).
    pub epoch: Option<SecondsSince<TDB>>,
}

impl FrameNode {
    /// Runtime frame identity. Required at construction (issue #664) — a
    /// node without a minted identity is unrepresentable. Read-only: the
    /// field is private so a caller cannot desync the tree's identity
    /// index through [`FrameTree::get_mut`].
    pub fn uid(&self) -> &FrameUid {
        &self.uid
    }
}

/// Integrity violations reported by [`FrameTree::validate`] /
/// [`FrameTree::validate_forest`].
///
/// Identity-dependent checks skip unstamped (`uid: None`) nodes — they have
/// no identity to contradict. `Cycle` and `UnresolvedParent` are structurally
/// unreachable through this module's construction API; the issue #663
/// frame-document loader (`astrodyn::frame_doc_io`) is the bulk-load path
/// they exist for — its load errors mirror this vocabulary and it runs
/// [`FrameTree::validate_forest`] as the post-build belt.
#[derive(Debug, thiserror::Error)]
pub enum FrameTreeError {
    /// A parent walk from this frame exceeded the node count — the
    /// parent links contain a cycle.
    #[error("frame tree contains a cycle reachable from frame {0}")]
    Cycle(FrameId),
    /// The same stamped identity appears on more than one node.
    #[error("duplicate frame identity `{0}` appears on more than one node")]
    DuplicateUid(FrameUid),
    /// A frame names a parent id that is not a valid node.
    #[error("frame {0} names parent {1}, which is not a valid node")]
    UnresolvedParent(FrameId, FrameId),
    /// More than one root in a tree validated with the strict
    /// [`FrameTree::validate`] (use [`FrameTree::validate_forest`] for the
    /// legitimate post-import, pre-graft state).
    #[error(
        "tree has multiple roots {0:?}; expected a single root \
         (use validate_forest to allow a forest)"
    )]
    MultiRoot(Vec<FrameId>),
    /// A stamped root's class is not root-eligible
    /// ([`FrameClass::may_be_root_or_integ`]).
    #[error(
        "stamped root frame {id} has class {class:?}, which cannot root a tree \
         (only inertial-flavor classes may be a root or integration frame)"
    )]
    NonInertialRoot {
        /// The offending root frame.
        id: FrameId,
        /// Its stamped class.
        class: FrameClass,
    },
    /// A stamped node whose class structurally guarantees zero angular
    /// velocity carries a non-zero `ang_vel_this`.
    #[error(
        "stamped frame {id} has non-rotating class {class:?} but a non-zero \
         angular velocity — classification contradicts state"
    )]
    ClassStateContradiction {
        /// The offending frame.
        id: FrameId,
        /// Its stamped class.
        class: FrameClass,
    },
    /// A node's rotation quaternion has drifted from unit norm beyond
    /// `NormalizedQuat::DEFAULT_TOLERANCE` (a missed renormalization
    /// upstream, or corrupt loaded data).
    #[error("frame {id} quaternion norm {norm} drifted beyond unit tolerance")]
    UnitNormDrift {
        /// The offending frame.
        id: FrameId,
        /// The drifted norm.
        norm: f64,
    },
}

/// Arena-based frame tree. Portable (no ECS dependency).
///
/// Frames are stored in a flat `Vec`; parent/child relationships are tracked
/// with parallel vectors of `Option<FrameId>` and `Vec<FrameId>`. Stamped
/// frame identities are indexed for [`FrameTree::find`] /
/// [`FrameTree::resolve`].
pub struct FrameTree {
    nodes: Vec<FrameNode>,
    parent: Vec<Option<FrameId>>,
    children: Vec<Vec<FrameId>>,
    /// Identity → arena-id index, maintained by the stamped constructors
    /// and [`FrameTree::import_subtree`]. Unstamped nodes are absent.
    uid_index: HashMap<FrameUid, FrameId>,
}

impl FrameTree {
    /// Create an empty tree.
    pub fn new() -> Self {
        Self {
            nodes: Vec::new(),
            parent: Vec::new(),
            children: Vec::new(),
            uid_index: HashMap::new(),
        }
    }

    // -- construction -------------------------------------------------------
    //
    // Every constructor mints a required identity (issue #664): the
    // untyped `add_root(name, kind)` / `add_child(...)` scaffold from the
    // pre-#662 migration window is gone, along with `RefFrameKind`.

    /// Add a stamped root frame whose identity is `FrameUid::of::<F>()`.
    /// State is identity.
    ///
    /// # Panics
    /// - `F`'s class is not root-eligible
    ///   ([`FrameClass::may_be_root_or_integ`]): integrating in (or rooting
    ///   on) a rotating frame is silently wrong physics, rejected at the
    ///   point of introduction.
    /// - `F` is non-mintable (a storage-boundary wildcard or
    ///   `IntegrationFrame`): `FrameUid::of` panics with the mint-policy
    ///   diagnostic.
    /// - the minted identity is already registered in this tree.
    pub fn add_root_typed<F: Frame>(&mut self, name: String) -> FrameId {
        let uid = FrameUid::of::<F>();
        // JEOD_INV: RF.15 — only inertial-flavor classes may root a tree;
        // rooting on a rotating/body class is rejected at construction.
        assert!(
            uid.class.may_be_root_or_integ(),
            "add_root_typed: frame `{}` has class {:?}, which cannot root a tree \
             (only RootInertial / PlanetInertial / BarycenterInertial classes may \
             be a root or integration frame). Add it as a child via add_child_typed.",
            F::NAME,
            uid.class
        );
        let id = self.nodes.len();
        self.nodes.push(FrameNode {
            name,
            state: RefFrameState::default(),
            uid: uid.clone(),
            epoch: None,
        });
        self.parent.push(None);
        self.children.push(Vec::new());
        self.register_uid(uid, id);
        id
    }

    /// Add a stamped root frame whose identity the caller supplies as a
    /// value — the root-level sibling of [`Self::add_child_uid`], for
    /// hosts (and the issue #663 document loader) that resolve the root's
    /// identity at runtime rather than from a compile-time marker. State
    /// is identity.
    ///
    /// # Panics
    /// - `uid.class` is not root-eligible
    ///   ([`FrameClass::may_be_root_or_integ`]): integrating in (or rooting
    ///   on) a rotating frame is silently wrong physics, rejected at the
    ///   point of introduction (matching [`Self::add_root_typed`] and
    ///   `validate()`'s `NonInertialRoot`).
    /// - the identity is already registered in this tree.
    pub fn add_root_uid(&mut self, uid: FrameUid, name: String) -> FrameId {
        // JEOD_INV: RF.15 — only inertial-flavor classes may root a tree;
        // rooting on a rotating/body class is rejected at construction.
        assert!(
            uid.class.may_be_root_or_integ(),
            "add_root_uid: identity `{uid}` has class {:?}, which cannot root a \
             tree (only RootInertial / PlanetInertial / BarycenterInertial \
             classes may be a root or integration frame). Add it as a child \
             via add_child_uid.",
            uid.class
        );
        let id = self.nodes.len();
        self.nodes.push(FrameNode {
            name,
            state: RefFrameState::default(),
            uid: uid.clone(),
            epoch: None,
        });
        self.parent.push(None);
        self.children.push(Vec::new());
        self.register_uid(uid, id);
        id
    }

    /// Register a stamped identity in the lookup index, rejecting
    /// duplicates at the point of introduction.
    // JEOD_INV: RF.14 — every frame identity maps to exactly one node; a
    // duplicate registration is rejected loudly at the point of
    // introduction, never silently aliased.
    fn register_uid(&mut self, uid: FrameUid, id: FrameId) {
        if let Some(&existing) = self.uid_index.get(&uid) {
            panic!(
                "FrameTree: duplicate frame identity `{uid}` — already registered at \
                 frame {existing}, cannot register it again at frame {id}. Each \
                 FrameUid maps to exactly one node. If these are genuinely distinct \
                 frames, mint distinct identities (different tag or role), or import \
                 the foreign tree under a non-LOCAL namespace via import_subtree."
            );
        }
        self.uid_index.insert(uid, id);
    }

    /// Typed child constructor: stamps the child with
    /// `FrameUid::of::<C>()` (no hand-supplied kind — the runtime identity
    /// is derived from the compile-time marker) and checks the typed
    /// state's parent marker `P` against the parent node's stamped
    /// identity. The arena's storage stays untyped — the typed state is
    /// converted via [`RefFrameStateTyped::to_untyped`] at the boundary;
    /// numeric values are preserved exactly.
    ///
    /// # Panics
    /// - `parent_id` out of range.
    /// - the parent node is unstamped (created via the untyped path).
    /// - the parent's stamped identity is not `FrameUid::of::<P>()`.
    /// - `C` is non-mintable (`FrameUid::of` panics).
    /// - the child identity is already registered in this tree.
    pub fn add_child_typed<P: Frame, C: Frame>(
        &mut self,
        parent_id: FrameId,
        name: String,
        state: RefFrameStateTyped<P, C>,
        epoch: Option<SecondsSince<TDB>>,
    ) -> FrameId {
        assert!(
            parent_id < self.nodes.len(),
            "add_child_typed: parent_id {parent_id} out of range (have {} frames)",
            self.nodes.len()
        );
        assert!(
            matches!(P::DESCRIPTOR.mint, MintPolicy::Stable),
            "add_child_typed: parent marker `{}` is not mintable (a runtime-resolved \
             wildcard or IntegrationFrame) and can never name a stamped parent \
             identity. Resolve the concrete parent frame type and use that instead.",
            core::any::type_name::<P>()
        );
        // JEOD_INV: RF.02 — the typed state's parent marker must name the
        // parent node's stamped identity (valid predecessor).
        let parent_uid = &self.nodes[parent_id].uid;
        assert!(
            parent_uid.is::<P>(),
            "add_child_typed: parent frame {parent_id} has identity `{parent_uid}`, \
             which is not `{}` — the typed state's parent marker P must match the \
             frame it is parented to.",
            core::any::type_name::<P>()
        );
        let child_uid = FrameUid::of::<C>();
        self.add_child_uid(parent_id, child_uid, name, state.to_untyped(), epoch)
    }

    /// Add a child stamped with a caller-supplied identity — the flexible
    /// primitive for dynamically-resolved identities (a host that knows the
    /// planet only at runtime) and producer-defined runtime frames.
    ///
    /// No namespace restriction: `LOCAL` identities minted via
    /// `FrameUid::of` are accepted (the LOCAL reservation is enforced at
    /// uid construction by `FrameUid::external`, not here).
    ///
    /// # Panics
    /// - `parent_id` out of range.
    /// - the identity is already registered in this tree.
    pub fn add_child_uid(
        &mut self,
        parent_id: FrameId,
        uid: FrameUid,
        name: String,
        state: RefFrameState,
        epoch: Option<SecondsSince<TDB>>,
    ) -> FrameId {
        assert!(
            parent_id < self.nodes.len(),
            "add_child_uid: parent_id {parent_id} out of range (have {} frames)",
            self.nodes.len()
        );
        let id = self.nodes.len();
        self.nodes.push(FrameNode {
            name,
            state,
            uid: uid.clone(),
            epoch,
        });
        self.parent.push(Some(parent_id));
        self.children.push(Vec::new());
        self.children[parent_id].push(id);
        self.register_uid(uid, id);
        id
    }

    /// Set (or clear) a node's frame epoch. Groundwork for per-step
    /// stamping (issue #662); not called on the hot path here.
    pub fn set_epoch(&mut self, id: FrameId, epoch: Option<SecondsSince<TDB>>) {
        assert!(
            id < self.nodes.len(),
            "set_epoch: frame id {id} out of range (have {} frames)",
            self.nodes.len()
        );
        self.nodes[id].epoch = epoch;
    }

    /// Speculative identity lookup: the arena id of `uid`, or `None` —
    /// an observable miss the caller can surface or recover from.
    pub fn find(&self, uid: &FrameUid) -> Option<FrameId> {
        self.uid_index.get(uid).copied()
    }

    /// Load-bearing identity lookup: the arena id of `uid`.
    ///
    /// # Panics
    /// Panics naming the identity if it is not registered in this tree —
    /// a missing load-bearing frame is a misconfiguration, never silently
    /// substituted.
    pub fn resolve(&self, uid: &FrameUid) -> FrameId {
        self.uid_index.get(uid).copied().unwrap_or_else(|| {
            panic!(
                "FrameTree::resolve: no frame with identity `{uid}` in this tree \
                 ({} frames, {} stamped). Stamp the frame via a typed constructor, \
                 add_child_uid, or import_subtree before resolving it.",
                self.nodes.len(),
                self.uid_index.len()
            )
        })
    }

    /// Read the state at `id` as a typed [`RefFrameStateTyped<P, C>`],
    /// **checked against the stored identities**: the node's stamped uid
    /// must be `FrameUid::of::<C>()` and its parent's stamped uid must be
    /// `FrameUid::of::<P>()`. Replaces the former caller-asserts boundary —
    /// a wrong marker now fails loudly instead of silently mislabeling
    /// physics. The wrapped quaternion is additionally checked against
    /// `NormalizedQuat::DEFAULT_TOLERANCE` by the underlying lift.
    ///
    /// # Panics
    /// - `P` or `C` is non-mintable (a storage-boundary wildcard or
    ///   `IntegrationFrame`) — such a marker can never name a stored
    ///   identity; request the concrete frame type.
    /// - the node (or its parent) is unstamped.
    /// - the node is a root (it has no predecessor state to read).
    /// - the stored child or parent identity does not match the markers.
    // JEOD_INV: RF.02 — checked typed recovery: stored child identity must
    // equal of::<C>() and the parent node's identity must equal of::<P>()
    // (valid predecessor); unstamped or root nodes are rejected loudly.
    pub fn get_state_typed<P: Frame, C: Frame>(&self, id: FrameId) -> RefFrameStateTyped<P, C> {
        assert!(
            matches!(C::DESCRIPTOR.mint, MintPolicy::Stable),
            "get_state_typed: child marker `{}` is not mintable (a runtime-resolved \
             wildcard or IntegrationFrame) and can never name a stored identity. \
             Resolve the concrete frame type and request that instead.",
            core::any::type_name::<C>()
        );
        assert!(
            matches!(P::DESCRIPTOR.mint, MintPolicy::Stable),
            "get_state_typed: parent marker `{}` is not mintable (a runtime-resolved \
             wildcard or IntegrationFrame) and can never name a stored identity. \
             Resolve the concrete frame type and request that instead.",
            core::any::type_name::<P>()
        );
        let node_uid = &self.nodes[id].uid;
        assert!(
            node_uid.is::<C>(),
            "get_state_typed: frame {id} has stored identity `{node_uid}`, but the \
             requested child marker is `{}` — identity mismatch.",
            core::any::type_name::<C>()
        );
        let parent_id = self.parent[id].unwrap_or_else(|| {
            panic!(
                "get_state_typed: frame {id} (`{node_uid}`) is a root (no parent), \
                 but a parent marker `{}` was requested — a root has no predecessor \
                 state to read.",
                core::any::type_name::<P>()
            )
        });
        let parent_uid = &self.nodes[parent_id].uid;
        assert!(
            parent_uid.is::<P>(),
            "get_state_typed: parent of frame {id} has identity `{parent_uid}`, but \
             the requested parent marker is `{}` — predecessor identity mismatch.",
            core::any::type_name::<P>()
        );
        RefFrameStateTyped::<P, C>::from_untyped_unchecked(&self.nodes[id].state)
    }

    // -- accessors ----------------------------------------------------------

    /// Borrow a frame node by id.
    pub fn get(&self, id: FrameId) -> &FrameNode {
        &self.nodes[id]
    }

    /// Mutably borrow a frame node by id.
    pub fn get_mut(&mut self, id: FrameId) -> &mut FrameNode {
        &mut self.nodes[id]
    }

    /// Parent of the given frame, or `None` for a root.
    pub fn parent(&self, id: FrameId) -> Option<FrameId> {
        self.parent[id]
    }

    /// Direct children of the given frame.
    pub fn children(&self, id: FrameId) -> &[FrameId] {
        &self.children[id]
    }

    /// Number of frames in the tree.
    pub fn len(&self) -> usize {
        self.nodes.len()
    }

    /// Whether the tree is empty.
    pub fn is_empty(&self) -> bool {
        self.nodes.is_empty()
    }

    // -- tree traversal -----------------------------------------------------

    /// Depth of `id` in the tree (root has depth 0).
    pub fn depth(&self, id: FrameId) -> usize {
        let mut d = 0usize;
        let mut current = id;
        while let Some(p) = self.parent[current] {
            d += 1;
            current = p;
        }
        d
    }

    /// Find the common ancestor of two frames in O(depth).
    ///
    /// Computes both depths, brings the deeper frame up to the other's depth,
    /// then walks both up in lockstep until they meet.
    ///
    /// Returns `None` if the frames do not share a common root (disconnected
    /// subtrees).
    pub fn common_ancestor(&self, a: FrameId, b: FrameId) -> Option<FrameId> {
        let mut da = self.depth(a);
        let mut db = self.depth(b);
        let mut ca = a;
        let mut cb = b;

        // Equalize depths.
        while da > db {
            ca = self.parent[ca]?;
            da -= 1;
        }
        while db > da {
            cb = self.parent[cb]?;
            db -= 1;
        }

        // Walk up in lockstep.
        while ca != cb {
            ca = self.parent[ca]?;
            cb = self.parent[cb]?;
        }
        Some(ca)
    }

    /// Identity rendering of a frame for diagnostics.
    fn display_id(&self, id: FrameId) -> String {
        format!("`{}` (frame {id})", self.nodes[id].uid)
    }

    /// Walk to the root of `id`'s tree.
    fn root_of(&self, mut id: FrameId) -> FrameId {
        while let Some(p) = self.parent[id] {
            id = p;
        }
        id
    }

    /// Find the common ancestor of two frames.
    ///
    /// Convenience wrapper around `common_ancestor` that panics if the frames
    /// do not share a common root, naming both endpoints and both roots —
    /// an undeclared cross-source relationship is never silently answered.
    /// Prefer `common_ancestor` in new code.
    pub fn find_common_ancestor(&self, a: FrameId, b: FrameId) -> FrameId {
        self.common_ancestor(a, b).unwrap_or_else(|| {
            let (ra, rb) = (self.root_of(a), self.root_of(b));
            let ns = |id: FrameId| self.nodes[id].uid.namespace;
            let suggest = if ns(ra) != ns(rb) {
                " The two frames live under roots in different namespaces; if they \
                 belong in one tree, declare the relationship explicitly by \
                 attaching one root under the other with FrameTree::graft before \
                 querying relative state."
            } else {
                ""
            };
            panic!(
                "find_common_ancestor: frames {} and {} do not share a common \
                 ancestor (roots {} and {}).{suggest}",
                self.display_id(a),
                self.display_id(b),
                self.display_id(ra),
                self.display_id(rb)
            )
        })
    }

    // -- integrity ------------------------------------------------------------

    /// Validate the tree's structural and identity integrity, requiring a
    /// **single root** (the production invariant). Use
    /// [`Self::validate_forest`] for the legitimate multi-root state
    /// between [`Self::import_subtree`] and [`Self::graft`]. See
    /// [`FrameTreeError`] for the rejected conditions.
    pub fn validate(&self) -> Result<(), FrameTreeError> {
        let roots = self.validate_common()?;
        if roots.len() > 1 {
            return Err(FrameTreeError::MultiRoot(roots));
        }
        Ok(())
    }

    /// [`Self::validate`] minus the single-root requirement — a forest of
    /// disconnected roots (e.g. freshly imported, not yet grafted) passes.
    pub fn validate_forest(&self) -> Result<(), FrameTreeError> {
        self.validate_common().map(|_| ())
    }

    /// Shared validation pass; returns the root set on success.
    fn validate_common(&self) -> Result<Vec<FrameId>, FrameTreeError> {
        let len = self.nodes.len();
        let mut roots = Vec::new();
        let mut seen_uids: HashSet<&FrameUid> = HashSet::new();
        for id in 0..len {
            // Parent link resolution (belt: structurally impossible via this
            // module's construction API; reachable via future load paths).
            if let Some(p) = self.parent[id] {
                if p >= len {
                    return Err(FrameTreeError::UnresolvedParent(id, p));
                }
            } else {
                roots.push(id);
            }
            // Cycle detection: a parent walk longer than the node count
            // cannot terminate (belt, as above).
            let mut current = id;
            let mut hops = 0usize;
            while let Some(p) = self.parent[current] {
                if p >= len {
                    return Err(FrameTreeError::UnresolvedParent(current, p));
                }
                current = p;
                hops += 1;
                if hops > len {
                    return Err(FrameTreeError::Cycle(id));
                }
            }
            let node = &self.nodes[id];
            // Identity checks — every node carries a required identity
            // (issue #664), so these run unconditionally.
            let uid = &node.uid;
            // JEOD_INV: RF.14 — bulk-load belt for the identity-uniqueness
            // invariant register_uid enforces at construction.
            if !seen_uids.insert(uid) {
                return Err(FrameTreeError::DuplicateUid(uid.clone()));
            }
            // JEOD_INV: RF.15 — bulk-load belt for the root-class
            // eligibility the constructors enforce.
            if self.parent[id].is_none() && !uid.class.may_be_root_or_integ() {
                return Err(FrameTreeError::NonInertialRoot {
                    id,
                    class: uid.class,
                });
            }
            if !uid.class.is_rotating()
                && !matches!(uid.class, FrameClass::Body | FrameClass::External)
                && node.state.rot.ang_vel_this != DVec3::ZERO
            {
                return Err(FrameTreeError::ClassStateContradiction {
                    id,
                    class: uid.class,
                });
            }
            // Unit-norm drift (all nodes — state integrity is
            // identity-independent). Tolerance check, not bit-equality; the
            // explicit NaN arm exists because NaN compares false to every
            // threshold and would otherwise pass silently.
            let norm = node.state.rot.q_parent_this.norm();
            if norm.is_nan()
                || (norm - 1.0).abs()
                    > NormalizedQuat::<ScalarFirst, LeftTransform>::DEFAULT_TOLERANCE
            {
                return Err(FrameTreeError::UnitNormDrift { id, norm });
            }
        }
        Ok(roots)
    }

    // -- multi-source composition ----------------------------------------------

    /// Deep-copy `other`'s entire forest into this tree, re-stamping every
    /// identity into namespace `ns`. Returns `(old_id, new_id)` pairs in
    /// `other`'s insertion order so callers holding foreign ids can locate
    /// the imported nodes (e.g. to [`Self::graft`] an imported root).
    ///
    /// # Panics
    /// - `ns == Namespace::LOCAL`: LOCAL is reserved for type-derived
    ///   identities — a foreign tree must not be able to impersonate them.
    /// - a re-stamped identity collides with one already registered here
    ///   (e.g. two imports into the same namespace).
    pub fn import_subtree(&mut self, other: &FrameTree, ns: Namespace) -> Vec<(FrameId, FrameId)> {
        assert!(
            ns != Namespace::LOCAL,
            "import_subtree: Namespace::LOCAL is reserved for type-derived \
             identities (FrameUid::of). Import foreign trees into a \
             host-allocated non-LOCAL namespace."
        );
        let base = self.nodes.len();
        let mut map = Vec::with_capacity(other.nodes.len());
        for (old, node) in other.nodes.iter().enumerate() {
            let new = base + old;
            let new_uid = node.uid.clone().with_namespace(ns);
            self.nodes.push(FrameNode {
                name: node.name.clone(),
                state: node.state,
                uid: new_uid.clone(),
                epoch: node.epoch,
            });
            self.parent.push(other.parent[old].map(|p| base + p));
            self.children
                .push(other.children[old].iter().map(|&c| base + c).collect());
            self.register_uid(new_uid, new);
            map.push((old, new));
        }
        map
    }

    /// Attach root `root_id` under `new_parent` with the caller-supplied
    /// relative state — the **explicit declaration** of a cross-source
    /// relationship. Unlike [`Self::reparent`] (which recomputes state from
    /// an existing same-tree relationship), a freshly imported root has no
    /// prior relationship to this tree: the supplied `state` *is* the
    /// host's declared physical claim about where the foreign root sits.
    ///
    /// # Panics
    /// - `root_id` is not a root (already has a parent).
    /// - `new_parent` is a descendant of `root_id` (would create a cycle).
    pub fn graft(&mut self, root_id: FrameId, new_parent: FrameId, state: RefFrameState) {
        assert!(
            self.parent[root_id].is_none(),
            "graft: frame {} is not a root (it has parent {:?}); only roots may be \
             grafted — use reparent for frames already related to this tree.",
            root_id,
            self.parent[root_id]
        );
        assert!(
            !self.is_descendant_of(new_parent, root_id),
            "graft: new_parent {new_parent} is a descendant of {root_id} — grafting \
             would create a cycle."
        );
        self.parent[root_id] = Some(new_parent);
        self.children[new_parent].push(root_id);
        self.nodes[root_id].state = state;
    }

    /// Path from `descendant` up to `ancestor` (inclusive of both endpoints).
    ///
    /// Returns `None` if `ancestor` is not actually an ancestor of `descendant`.
    pub fn path_to_ancestor(&self, descendant: FrameId, ancestor: FrameId) -> Option<Vec<FrameId>> {
        let mut path = Vec::new();
        let mut current = descendant;
        loop {
            path.push(current);
            if current == ancestor {
                return Some(path);
            }
            current = self.parent[current]?;
        }
    }

    /// Compute the relative state between two frames, returning `None` if
    /// they don't share a common ancestor.
    ///
    /// Option-returning variant of `compute_relative_state`. Useful when the
    /// frame relationship isn't statically known.
    pub fn try_compute_relative_state(&self, from: FrameId, to: FrameId) -> Option<RefFrameState> {
        let ancestor = self.common_ancestor(from, to)?;
        let state_from = self.compose_to_ancestor(from, ancestor);
        let state_to = self.compose_to_ancestor(to, ancestor);
        let from_negated = RefFrameState::negate(&state_from);
        Some(from_negated.incr_right(&state_to))
    }

    /// Compute the relative state between two frames.
    ///
    /// Returns the state of `to` relative to `from` (i.e., if you are
    /// "standing in" the `from` frame, this tells you where `to` is).
    ///
    /// Port of JEOD `ref_frame_compute_relative_state.cc`. The algorithm:
    /// 1. Find common ancestor of `from` and `to`.
    /// 2. Compose states from `from` up to the ancestor.
    /// 3. Compose states from `to` up to the ancestor.
    /// 4. Result = negate(from_composed) composed with to_composed.
    ///
    /// This gives the state of `to` as seen from `from`.
    // JEOD_INV: RF.01 — same-tree requirement: both FrameIds are arena indices into this
    // single `FrameTree`, so `compute_relative_state` cannot be called across trees.
    // JEOD_INV: RF.02 — predecessor validity: `find_common_ancestor` and `parent()` both
    // bounds-check the arena; an invalid `FrameId` panics immediately.
    pub fn compute_relative_state(&self, from: FrameId, to: FrameId) -> RefFrameState {
        let ancestor = self.find_common_ancestor(from, to);

        // Compose state from `from` to ancestor.
        let state_from = self.compose_to_ancestor(from, ancestor);

        // Compose state from `to` to ancestor.
        let state_to = self.compose_to_ancestor(to, ancestor);

        // state_from is the state of `from` relative to ancestor (ancestor -> from).
        // state_to is the state of `to` relative to ancestor (ancestor -> to).
        // We want state of `to` relative to `from` (from -> to).
        // from -> to = negate(ancestor -> from) composed with (ancestor -> to)
        //           = (from -> ancestor) composed with (ancestor -> to)
        let from_negated = RefFrameState::negate(&state_from);
        from_negated.incr_right(&state_to)
    }

    // -- lookup --------------------------------------------------------------

    /// Find a frame by its diagnostic name. Returns the first match, or
    /// `None`. **Debug convenience only** (issue #664): names are
    /// non-unique diagnostic labels — production logic addresses frames
    /// by identity via [`Self::find`] / [`Self::resolve`].
    pub fn find_by_name(&self, name: &str) -> Option<FrameId> {
        self.nodes.iter().position(|n| n.name == name)
    }

    // -- tree mutation -------------------------------------------------------

    /// Test whether `id` is a descendant of `ancestor` (or equal to it).
    pub fn is_descendant_of(&self, id: FrameId, ancestor: FrameId) -> bool {
        if id == ancestor {
            return true;
        }
        let mut current = id;
        while let Some(p) = self.parent[current] {
            if p == ancestor {
                return true;
            }
            current = p;
        }
        false
    }

    /// Move a frame to a new parent, preserving its absolute state.
    ///
    /// Port of JEOD's `RefFrame::transplant_node()`: the frame's position in
    /// the tree changes, but its state relative to the root is preserved by
    /// recomputing the relative state with respect to the new parent.
    ///
    /// # Panics
    /// - `new_parent` is a descendant of `id` (would create a cycle).
    /// - `id` is a root frame with no parent.
    /// - `id` and `new_parent` do not share a common root.
    pub fn reparent(&mut self, id: FrameId, new_parent: FrameId) {
        // Check root first — root has no parent to detach from.
        assert!(
            self.parent[id].is_some(),
            "reparent: frame {} has no parent (is a root) — cannot reparent root frames",
            id
        );

        // No-op if already parented to `new_parent`.
        if self.parent[id] == Some(new_parent) {
            return;
        }

        assert!(
            !self.is_descendant_of(new_parent, id),
            "reparent: new_parent {} is a descendant of {} — would create a cycle",
            new_parent,
            id
        );

        // Verify frames share a common root before computing relative state.
        // find_common_ancestor panics with a generic message; catch it here
        // with a reparent-specific message for easier debugging.
        {
            let mut cur = new_parent;
            while let Some(p) = self.parent[cur] {
                cur = p;
            }
            let new_parent_root = cur;
            cur = id;
            while let Some(p) = self.parent[cur] {
                cur = p;
            }
            assert!(
                cur == new_parent_root,
                "reparent: frame {id} and new_parent {new_parent} do not share a common root"
            );
        }

        // Compute state of `id` relative to `new_parent` (preserves absolute state).
        let new_state = self.compute_relative_state(new_parent, id);

        // Remove from old parent's children list.
        let old_parent = self.parent[id].unwrap();
        self.children[old_parent].retain(|&c| c != id);

        // Attach to new parent.
        self.parent[id] = Some(new_parent);
        self.children[new_parent].push(id);

        // Store recomputed relative state.
        self.nodes[id].state = new_state;
    }

    // -- tree traversal (internal) ------------------------------------------

    /// Compose states from `id` up to `ancestor`, returning the state of
    /// `id` relative to `ancestor`.
    ///
    /// The stored state of each frame is relative to its parent. Walking
    /// up the chain and composing with `incr_left` accumulates the
    /// parent-to-root transforms.
    fn compose_to_ancestor(&self, id: FrameId, ancestor: FrameId) -> RefFrameState {
        if id == ancestor {
            return RefFrameState::default();
        }

        // Start with the state of `id` relative to its parent.
        let mut composed = self.nodes[id].state;
        let mut current = id;

        // Walk upward, composing each parent's state on the left.
        while let Some(p) = self.parent[current] {
            if p == ancestor {
                // We've reached the ancestor; `composed` now represents
                // the state of `id` relative to `ancestor`.
                return composed;
            }
            // composed = parent_state composed with composed
            // i.e., ancestor->...->parent->current becomes ancestor->...->grandparent->current
            composed.incr_left(&self.nodes[p].state);
            current = p;
        }

        // If we get here, we walked all the way to a root without hitting
        // `ancestor`. This shouldn't happen if find_common_ancestor was correct.
        panic!(
            "compose_to_ancestor: frame {} is not a descendant of ancestor {}",
            id, ancestor
        );
    }
}

impl Default for FrameTree {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ref_frame_state::{RefFrameRot, RefFrameTrans};
    use astrodyn_math::test_utils::{approx_eq_mat3, approx_eq_vec3};
    use astrodyn_math::JeodQuat;
    use astrodyn_quantities::frame_descriptor::{FrameRole, Tag};
    use glam::{DMat3, DVec3};
    use std::f64::consts::FRAC_PI_2;

    const TOL: f64 = 1e-12;

    /// Mint a fresh, unique external identity — identity is required at
    /// construction (issue #664), and these structural tests don't care
    /// which one a node carries, only that nodes are distinct.
    fn ext_uid(class: FrameClass) -> FrameUid {
        use std::sync::atomic::{AtomicUsize, Ordering};
        static N: AtomicUsize = AtomicUsize::new(0);
        let n = N.fetch_add(1, Ordering::Relaxed);
        FrameUid::external(
            Namespace(2),
            class,
            FrameRole::Primary,
            Tag::Named(format!("t{n}").into()),
        )
    }

    /// Test-local stand-in for the removed untyped root constructor: a
    /// root with a fresh (root-eligible) external identity.
    fn add_root(tree: &mut FrameTree, name: String) -> FrameId {
        tree.add_root_uid(ext_uid(FrameClass::PlanetInertial), name)
    }

    /// Test-local stand-in for the removed untyped child constructor: a
    /// child with a fresh external identity (`External` class — exempt
    /// from the class/state contradiction check, since these synthetic
    /// frames carry arbitrary rotation state).
    fn add_child(
        tree: &mut FrameTree,
        parent: FrameId,
        name: String,
        state: RefFrameState,
    ) -> FrameId {
        tree.add_child_uid(parent, ext_uid(FrameClass::External), name, state, None)
    }

    /// Helper: create a RefFrameState with a rotation about Z axis and a position offset.
    fn make_state(angle_z: f64, pos: DVec3, vel: DVec3, ang_vel: DVec3) -> RefFrameState {
        let q = JeodQuat::left_quat_from_eigen_rotation(angle_z, DVec3::Z);
        let t = q.left_quat_to_transformation();
        RefFrameState {
            trans: RefFrameTrans {
                position: pos,
                velocity: vel,
            },
            rot: RefFrameRot {
                q_parent_this: q,
                t_parent_this: t,
                ang_vel_this: ang_vel,
            },
        }
    }

    // -----------------------------------------------------------------------
    // 1. Single root: no parent, identity state
    // -----------------------------------------------------------------------
    #[test]
    fn single_root() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());

        assert!(tree.parent(root).is_none(), "root should have no parent");
        assert!(
            tree.children(root).is_empty(),
            "root should have no children"
        );

        let node = tree.get(root);
        assert_eq!(node.name, "root");
        assert_eq!(node.uid().class, FrameClass::PlanetInertial);
        assert_eq!(node.state.trans.position, DVec3::ZERO);
        assert_eq!(node.state.trans.velocity, DVec3::ZERO);
        assert_eq!(node.state.rot.t_parent_this, DMat3::IDENTITY);
        assert_eq!(node.state.rot.ang_vel_this, DVec3::ZERO);
    }

    // -----------------------------------------------------------------------
    // 2. Parent-child links
    // -----------------------------------------------------------------------
    #[test]
    fn parent_child_links() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());

        let child_state = make_state(
            0.5,
            DVec3::new(1e6, 2e6, 3e6),
            DVec3::new(100.0, 200.0, 300.0),
            DVec3::new(0.01, 0.02, 0.03),
        );
        let child = add_child(&mut tree, root, "child".into(), child_state);

        assert_eq!(tree.parent(child), Some(root));
        assert_eq!(tree.children(root), &[child]);
        assert!(tree.children(child).is_empty());

        // Verify stored state matches
        let node = tree.get(child);
        assert!(
            approx_eq_vec3(node.state.trans.position, child_state.trans.position, TOL),
            "child position"
        );
        assert!(
            approx_eq_vec3(node.state.trans.velocity, child_state.trans.velocity, TOL),
            "child velocity"
        );
    }

    // -----------------------------------------------------------------------
    // 3. Relative state to self is identity
    // -----------------------------------------------------------------------
    #[test]
    fn relative_state_to_self() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());

        let child_state = make_state(
            1.0,
            DVec3::new(1e7, 0.0, 0.0),
            DVec3::new(7000.0, 0.0, 0.0),
            DVec3::new(0.0, 0.0, 0.001),
        );
        let child = add_child(&mut tree, root, "child".into(), child_state);

        let rel = tree.compute_relative_state(child, child);

        assert!(
            approx_eq_vec3(rel.trans.position, DVec3::ZERO, 1e-6),
            "self-relative position should be zero, got {:?}",
            rel.trans.position
        );
        assert!(
            approx_eq_vec3(rel.trans.velocity, DVec3::ZERO, 1e-6),
            "self-relative velocity should be zero, got {:?}",
            rel.trans.velocity
        );
        assert!(
            approx_eq_mat3(&rel.rot.t_parent_this, &DMat3::IDENTITY, 1e-10),
            "self-relative T should be identity"
        );
        assert!(
            approx_eq_vec3(rel.rot.ang_vel_this, DVec3::ZERO, 1e-10),
            "self-relative ang_vel should be zero"
        );
    }

    // -----------------------------------------------------------------------
    // 4. Relative state parent -> child matches child's stored state
    // -----------------------------------------------------------------------
    #[test]
    fn relative_state_parent_child() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());

        let child_state = make_state(
            0.5,
            DVec3::new(1e6, 2e6, 3e6),
            DVec3::new(100.0, 200.0, 300.0),
            DVec3::new(0.01, 0.02, 0.03),
        );
        let child = add_child(&mut tree, root, "child".into(), child_state);

        // relative state from root to child = child's state relative to root
        let rel = tree.compute_relative_state(root, child);

        assert!(
            approx_eq_vec3(rel.trans.position, child_state.trans.position, 1e-6),
            "parent->child position: expected {:?}, got {:?}",
            child_state.trans.position,
            rel.trans.position
        );
        assert!(
            approx_eq_vec3(rel.trans.velocity, child_state.trans.velocity, 1e-6),
            "parent->child velocity: expected {:?}, got {:?}",
            child_state.trans.velocity,
            rel.trans.velocity
        );
        assert!(
            approx_eq_mat3(
                &rel.rot.t_parent_this,
                &child_state.rot.t_parent_this,
                1e-10
            ),
            "parent->child T"
        );
        assert!(
            approx_eq_vec3(rel.rot.ang_vel_this, child_state.rot.ang_vel_this, 1e-10),
            "parent->child ang_vel"
        );
    }

    // -----------------------------------------------------------------------
    // 5. Relative state child -> parent is negation of child's state
    // -----------------------------------------------------------------------
    #[test]
    fn relative_state_child_parent() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());

        let child_state = make_state(
            0.5,
            DVec3::new(1e6, 2e6, 3e6),
            DVec3::new(100.0, 200.0, 300.0),
            DVec3::new(0.01, 0.02, 0.03),
        );
        let child = add_child(&mut tree, root, "child".into(), child_state);

        let rel = tree.compute_relative_state(child, root);
        let expected = RefFrameState::negate(&child_state);

        assert!(
            approx_eq_vec3(rel.trans.position, expected.trans.position, 1e-6),
            "child->parent position: expected {:?}, got {:?}",
            expected.trans.position,
            rel.trans.position
        );
        assert!(
            approx_eq_vec3(rel.trans.velocity, expected.trans.velocity, 1e-6),
            "child->parent velocity: expected {:?}, got {:?}",
            expected.trans.velocity,
            rel.trans.velocity
        );
        assert!(
            approx_eq_mat3(&rel.rot.t_parent_this, &expected.rot.t_parent_this, 1e-10),
            "child->parent T"
        );
        assert!(
            approx_eq_vec3(rel.rot.ang_vel_this, expected.rot.ang_vel_this, 1e-10),
            "child->parent ang_vel"
        );
    }

    // -----------------------------------------------------------------------
    // 6. Three-level tree: root -> A -> B
    //    Relative state root -> B should be composition of A.state and B.state
    // -----------------------------------------------------------------------
    #[test]
    fn three_level_tree() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());

        let state_a = make_state(
            FRAC_PI_2,
            DVec3::new(1000.0, 0.0, 0.0),
            DVec3::new(10.0, 0.0, 0.0),
            DVec3::ZERO,
        );
        let a = add_child(&mut tree, root, "A".into(), state_a);

        let state_b = make_state(
            0.0,
            DVec3::new(500.0, 0.0, 0.0),
            DVec3::new(5.0, 0.0, 0.0),
            DVec3::ZERO,
        );
        let b = add_child(&mut tree, a, "B".into(), state_b);

        let rel = tree.compute_relative_state(root, b);

        // Expected: state_a composed with state_b
        let expected = state_a.incr_right(&state_b);

        assert!(
            approx_eq_vec3(rel.trans.position, expected.trans.position, 1e-6),
            "root->B position: expected {:?}, got {:?}",
            expected.trans.position,
            rel.trans.position
        );
        assert!(
            approx_eq_vec3(rel.trans.velocity, expected.trans.velocity, 1e-6),
            "root->B velocity: expected {:?}, got {:?}",
            expected.trans.velocity,
            rel.trans.velocity
        );
        assert!(
            approx_eq_mat3(&rel.rot.t_parent_this, &expected.rot.t_parent_this, 1e-10),
            "root->B T"
        );
        assert!(
            approx_eq_vec3(rel.rot.ang_vel_this, expected.rot.ang_vel_this, 1e-10),
            "root->B ang_vel"
        );
    }

    // -----------------------------------------------------------------------
    // 7. Sibling relative state: two children of the same parent
    // -----------------------------------------------------------------------
    #[test]
    fn sibling_relative_state() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());

        let state_a = make_state(
            0.3,
            DVec3::new(1e6, 0.0, 0.0),
            DVec3::new(100.0, 0.0, 0.0),
            DVec3::new(0.0, 0.0, 0.01),
        );
        let a = add_child(&mut tree, root, "A".into(), state_a);

        let state_b = make_state(
            -0.7,
            DVec3::new(0.0, 2e6, 0.0),
            DVec3::new(0.0, 200.0, 0.0),
            DVec3::new(0.0, 0.0, 0.02),
        );
        let b = add_child(&mut tree, root, "B".into(), state_b);

        // Relative state from A to B should be:
        //   negate(root -> A) composed with (root -> B)
        let rel = tree.compute_relative_state(a, b);

        let a_neg = RefFrameState::negate(&state_a);
        let expected = a_neg.incr_right(&state_b);

        assert!(
            approx_eq_vec3(rel.trans.position, expected.trans.position, 1e-4),
            "sibling A->B position: expected {:?}, got {:?}",
            expected.trans.position,
            rel.trans.position
        );
        assert!(
            approx_eq_vec3(rel.trans.velocity, expected.trans.velocity, 1e-4),
            "sibling A->B velocity: expected {:?}, got {:?}",
            expected.trans.velocity,
            rel.trans.velocity
        );
        assert!(
            approx_eq_mat3(&rel.rot.t_parent_this, &expected.rot.t_parent_this, 1e-10),
            "sibling A->B T"
        );
        assert!(
            approx_eq_vec3(rel.rot.ang_vel_this, expected.rot.ang_vel_this, 1e-10),
            "sibling A->B ang_vel"
        );
    }

    // -----------------------------------------------------------------------
    // 8. Four-level tree: relative state matches direct composition to 1e-14
    //
    // Phase 3 exit criterion: "relative state between any two frames
    // matches direct computation to < 1e-14".
    //
    // Build: root -> A -> B -> C -> D, and root -> E (sibling branch).
    // Compare tree-traversed relative state against explicit composition
    // for multiple frame pairs including cross-branch.
    // -----------------------------------------------------------------------
    #[test]
    fn four_level_tree_relative_state_precision() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());

        // Use moderate values to avoid floating-point precision loss
        // from large position magnitudes.
        let state_a = make_state(
            0.3,
            DVec3::new(100.0, 200.0, 50.0),
            DVec3::new(1.0, 2.0, 0.5),
            DVec3::new(0.001, 0.002, 0.003),
        );
        let a = add_child(&mut tree, root, "A".into(), state_a);

        let state_b = make_state(
            -0.7,
            DVec3::new(50.0, -30.0, 80.0),
            DVec3::new(0.5, -0.3, 0.8),
            DVec3::new(-0.001, 0.001, 0.002),
        );
        let b = add_child(&mut tree, a, "B".into(), state_b);

        let state_c = make_state(
            1.2,
            DVec3::new(-20.0, 40.0, 10.0),
            DVec3::new(-0.2, 0.4, 0.1),
            DVec3::new(0.003, -0.002, 0.001),
        );
        let c = add_child(&mut tree, b, "C".into(), state_c);

        let state_d = make_state(
            -0.4,
            DVec3::new(10.0, 10.0, -5.0),
            DVec3::new(0.1, 0.1, -0.05),
            DVec3::new(0.0005, 0.0005, -0.001),
        );
        let d = add_child(&mut tree, c, "D".into(), state_d);

        // Sibling branch: root -> E
        let state_e = make_state(
            0.8,
            DVec3::new(-60.0, 90.0, 30.0),
            DVec3::new(-0.6, 0.9, 0.3),
            DVec3::new(0.002, -0.001, 0.004),
        );
        let e = add_child(&mut tree, root, "E".into(), state_e);

        // 4-level composition accumulates ~6e-14 position error from
        // floating-point arithmetic. We therefore use a 1e-13 tolerance for
        // position, which still demonstrates sub-1e-13 precision, while
        // rotation matrices and angular velocities are checked at 1e-14.
        let tol_rot = 1e-14;
        let tol_pos = 1e-13;

        // ── root → D (4 levels deep) ──
        let rel_root_d = tree.compute_relative_state(root, d);
        let expected_root_d = state_a
            .incr_right(&state_b)
            .incr_right(&state_c)
            .incr_right(&state_d);

        assert!(
            approx_eq_mat3(
                &rel_root_d.rot.t_parent_this,
                &expected_root_d.rot.t_parent_this,
                tol_rot
            ),
            "root→D rotation exceeds {tol_rot:.0e}"
        );
        assert!(
            approx_eq_vec3(
                rel_root_d.trans.position,
                expected_root_d.trans.position,
                tol_pos
            ),
            "root→D position exceeds {tol_pos:.0e}: diff = {:.4e}",
            (rel_root_d.trans.position - expected_root_d.trans.position).length()
        );
        assert!(
            approx_eq_vec3(
                rel_root_d.trans.velocity,
                expected_root_d.trans.velocity,
                tol_pos
            ),
            "root→D velocity exceeds {tol_pos:.0e}: diff = {:.4e}",
            (rel_root_d.trans.velocity - expected_root_d.trans.velocity).length()
        );
        assert!(
            approx_eq_vec3(
                rel_root_d.rot.ang_vel_this,
                expected_root_d.rot.ang_vel_this,
                tol_rot
            ),
            "root→D ang_vel exceeds {tol_rot:.0e}"
        );

        // ── D → root (reverse of above) ──
        let rel_d_root = tree.compute_relative_state(d, root);
        let expected_d_root = RefFrameState::negate(&expected_root_d);

        assert!(
            approx_eq_mat3(
                &rel_d_root.rot.t_parent_this,
                &expected_d_root.rot.t_parent_this,
                tol_rot
            ),
            "D→root rotation exceeds {tol_rot:.0e}"
        );
        assert!(
            approx_eq_vec3(
                rel_d_root.trans.position,
                expected_d_root.trans.position,
                tol_pos
            ),
            "D→root position exceeds {tol_pos:.0e}"
        );

        // ── B → D (same branch, partial traversal) ──
        let rel_b_d = tree.compute_relative_state(b, d);
        let expected_b_d = state_c.incr_right(&state_d);

        assert!(
            approx_eq_mat3(
                &rel_b_d.rot.t_parent_this,
                &expected_b_d.rot.t_parent_this,
                tol_rot
            ),
            "B→D rotation exceeds {tol_rot:.0e}"
        );
        assert!(
            approx_eq_vec3(rel_b_d.trans.position, expected_b_d.trans.position, tol_pos),
            "B→D position exceeds {tol_pos:.0e}"
        );
        assert!(
            approx_eq_vec3(rel_b_d.trans.velocity, expected_b_d.trans.velocity, tol_pos),
            "B→D velocity exceeds {tol_pos:.0e}"
        );
        assert!(
            approx_eq_vec3(
                rel_b_d.rot.ang_vel_this,
                expected_b_d.rot.ang_vel_this,
                tol_rot
            ),
            "B→D ang_vel exceeds {tol_rot:.0e}"
        );

        // ── D → E (cross-branch: D..root..E) ──
        let rel_d_e = tree.compute_relative_state(d, e);
        let expected_d_e = RefFrameState::negate(&expected_root_d).incr_right(&state_e);

        assert!(
            approx_eq_mat3(
                &rel_d_e.rot.t_parent_this,
                &expected_d_e.rot.t_parent_this,
                tol_rot
            ),
            "D→E rotation exceeds {tol_rot:.0e}"
        );
        assert!(
            approx_eq_vec3(rel_d_e.trans.position, expected_d_e.trans.position, tol_pos),
            "D→E position exceeds {tol_pos:.0e}: diff = {:.4e}",
            (rel_d_e.trans.position - expected_d_e.trans.position).length()
        );
        assert!(
            approx_eq_vec3(rel_d_e.trans.velocity, expected_d_e.trans.velocity, tol_pos),
            "D→E velocity exceeds {tol_pos:.0e}"
        );
        assert!(
            approx_eq_vec3(
                rel_d_e.rot.ang_vel_this,
                expected_d_e.rot.ang_vel_this,
                tol_rot
            ),
            "D→E ang_vel exceeds {tol_rot:.0e}"
        );

        println!(
            "  4-level frame tree: all 4 frame pairs match direct composition within tolerances \
             (rot {tol_rot:.0e}, pos/vel {tol_pos:.0e})"
        );
    }

    // -----------------------------------------------------------------------
    // 9. find_by_name
    // -----------------------------------------------------------------------
    #[test]
    fn find_by_name() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "Earth.inertial".into());
        let moon = add_child(
            &mut tree,
            root,
            "Moon.inertial".into(),
            RefFrameState::default(),
        );

        assert_eq!(tree.find_by_name("Earth.inertial"), Some(root));
        assert_eq!(tree.find_by_name("Moon.inertial"), Some(moon));
        assert_eq!(tree.find_by_name("Mars.inertial"), None);
    }

    // -----------------------------------------------------------------------
    // 10. is_descendant_of
    // -----------------------------------------------------------------------
    #[test]
    fn is_descendant_of() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());
        let a = add_child(&mut tree, root, "A".into(), RefFrameState::default());
        let b = add_child(&mut tree, a, "B".into(), RefFrameState::default());
        let c = add_child(&mut tree, root, "C".into(), RefFrameState::default());

        assert!(tree.is_descendant_of(b, root));
        assert!(tree.is_descendant_of(b, a));
        assert!(tree.is_descendant_of(a, root));
        assert!(tree.is_descendant_of(root, root)); // self
        assert!(!tree.is_descendant_of(root, a));
        assert!(!tree.is_descendant_of(c, a)); // sibling, not descendant
        assert!(!tree.is_descendant_of(b, c)); // cross-branch
    }

    // -----------------------------------------------------------------------
    // 11. reparent: move a child to a different parent, verify state preserved
    // -----------------------------------------------------------------------
    #[test]
    fn reparent_preserves_absolute_state() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());

        // Two children of root with distinct states.
        let state_a = make_state(
            0.3,
            DVec3::new(1000.0, 0.0, 0.0),
            DVec3::new(10.0, 0.0, 0.0),
            DVec3::new(0.0, 0.0, 0.01),
        );
        let a = add_child(&mut tree, root, "A".into(), state_a);

        let state_b = make_state(
            -0.5,
            DVec3::new(0.0, 2000.0, 0.0),
            DVec3::new(0.0, 20.0, 0.0),
            DVec3::new(0.0, 0.0, 0.02),
        );
        let b = add_child(&mut tree, root, "B".into(), state_b);

        // Child of B.
        let state_c = make_state(
            0.1,
            DVec3::new(100.0, 100.0, 0.0),
            DVec3::new(1.0, 1.0, 0.0),
            DVec3::new(0.001, 0.0, 0.0),
        );
        let c = add_child(&mut tree, b, "C".into(), state_c);

        // Record absolute state of C before reparent (root -> C).
        let abs_before = tree.compute_relative_state(root, c);

        // Reparent C from B to A.
        tree.reparent(c, a);

        // Verify tree structure changed.
        assert_eq!(tree.parent(c), Some(a));
        assert!(tree.children(a).contains(&c));
        assert!(!tree.children(b).contains(&c));

        // Verify absolute state is preserved.
        let abs_after = tree.compute_relative_state(root, c);

        let tol_pos = 1e-10;
        let tol_rot = 1e-13;
        assert!(
            approx_eq_vec3(abs_after.trans.position, abs_before.trans.position, tol_pos),
            "reparent position drift: expected {:?}, got {:?}, diff = {:.4e}",
            abs_before.trans.position,
            abs_after.trans.position,
            (abs_after.trans.position - abs_before.trans.position).length()
        );
        assert!(
            approx_eq_vec3(abs_after.trans.velocity, abs_before.trans.velocity, tol_pos),
            "reparent velocity drift: expected {:?}, got {:?}",
            abs_before.trans.velocity,
            abs_after.trans.velocity
        );
        assert!(
            approx_eq_mat3(
                &abs_after.rot.t_parent_this,
                &abs_before.rot.t_parent_this,
                tol_rot
            ),
            "reparent rotation drift"
        );
        assert!(
            approx_eq_vec3(
                abs_after.rot.ang_vel_this,
                abs_before.rot.ang_vel_this,
                tol_rot
            ),
            "reparent ang_vel drift"
        );
    }

    // -----------------------------------------------------------------------
    // 12. reparent panics on cycle
    // -----------------------------------------------------------------------
    #[test]
    #[should_panic(expected = "would create a cycle")]
    fn reparent_cycle_panics() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());
        let a = add_child(&mut tree, root, "A".into(), RefFrameState::default());
        let b = add_child(&mut tree, a, "B".into(), RefFrameState::default());

        // Try to reparent A under its own descendant B — should panic.
        tree.reparent(a, b);
    }

    // -----------------------------------------------------------------------
    // 13. reparent panics on root
    // -----------------------------------------------------------------------
    #[test]
    #[should_panic(expected = "cannot reparent root frames")]
    fn reparent_root_panics() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());
        let a = add_child(&mut tree, root, "A".into(), RefFrameState::default());

        // Try to reparent the root — should panic.
        tree.reparent(root, a);
    }

    // -----------------------------------------------------------------------
    // 14. common_ancestor: basic cases
    // -----------------------------------------------------------------------
    #[test]
    fn common_ancestor_same_frame() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());
        assert_eq!(tree.common_ancestor(root, root), Some(root));
    }

    #[test]
    fn common_ancestor_parent_child() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());
        let child = add_child(&mut tree, root, "child".into(), RefFrameState::default());
        assert_eq!(tree.common_ancestor(root, child), Some(root));
        assert_eq!(tree.common_ancestor(child, root), Some(root));
    }

    #[test]
    fn common_ancestor_siblings() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());
        let a = add_child(&mut tree, root, "A".into(), RefFrameState::default());
        let b = add_child(&mut tree, root, "B".into(), RefFrameState::default());
        assert_eq!(tree.common_ancestor(a, b), Some(root));
    }

    #[test]
    fn common_ancestor_unrelated_trees() {
        let mut tree = FrameTree::new();
        let root_a = add_root(&mut tree, "root_a".into());
        let root_b = add_root(&mut tree, "root_b".into());
        assert_eq!(tree.common_ancestor(root_a, root_b), None);
    }

    #[test]
    fn common_ancestor_deep_tree() {
        //     root
        //     ├── A
        //     │   ├── B
        //     │   │   └── C
        //     │   │       └── D
        //     │   └── E
        //     └── F
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());
        let a = add_child(&mut tree, root, "A".into(), RefFrameState::default());
        let b = add_child(&mut tree, a, "B".into(), RefFrameState::default());
        let c = add_child(&mut tree, b, "C".into(), RefFrameState::default());
        let d = add_child(&mut tree, c, "D".into(), RefFrameState::default());
        let e = add_child(&mut tree, a, "E".into(), RefFrameState::default());
        let f = add_child(&mut tree, root, "F".into(), RefFrameState::default());

        assert_eq!(tree.common_ancestor(d, e), Some(a));
        assert_eq!(tree.common_ancestor(d, f), Some(root));
        assert_eq!(tree.common_ancestor(b, d), Some(b));
        assert_eq!(tree.common_ancestor(d, a), Some(a));
    }

    // -----------------------------------------------------------------------
    // 15. depth
    // -----------------------------------------------------------------------
    #[test]
    fn depth_computation() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());
        let a = add_child(&mut tree, root, "A".into(), RefFrameState::default());
        let b = add_child(&mut tree, a, "B".into(), RefFrameState::default());
        let c = add_child(&mut tree, b, "C".into(), RefFrameState::default());

        assert_eq!(tree.depth(root), 0);
        assert_eq!(tree.depth(a), 1);
        assert_eq!(tree.depth(b), 2);
        assert_eq!(tree.depth(c), 3);
    }

    // -----------------------------------------------------------------------
    // 16. path_to_ancestor
    // -----------------------------------------------------------------------
    #[test]
    fn path_to_ancestor_basic() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());
        let a = add_child(&mut tree, root, "A".into(), RefFrameState::default());
        let b = add_child(&mut tree, a, "B".into(), RefFrameState::default());
        let c = add_child(&mut tree, b, "C".into(), RefFrameState::default());

        assert_eq!(tree.path_to_ancestor(c, root), Some(vec![c, b, a, root]));
        assert_eq!(tree.path_to_ancestor(c, a), Some(vec![c, b, a]));
        assert_eq!(tree.path_to_ancestor(c, c), Some(vec![c]));
    }

    #[test]
    fn path_to_ancestor_not_an_ancestor() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());
        let a = add_child(&mut tree, root, "A".into(), RefFrameState::default());
        let b = add_child(&mut tree, root, "B".into(), RefFrameState::default());

        // A is not an ancestor of B (they are siblings).
        assert_eq!(tree.path_to_ancestor(b, a), None);
    }

    // -----------------------------------------------------------------------
    // 17. try_compute_relative_state
    // -----------------------------------------------------------------------
    #[test]
    fn try_compute_relative_state_returns_none_for_disconnected() {
        let mut tree = FrameTree::new();
        let root_a = add_root(&mut tree, "root_a".into());
        let root_b = add_root(&mut tree, "root_b".into());
        assert!(tree.try_compute_relative_state(root_a, root_b).is_none());
    }

    #[test]
    fn try_compute_relative_state_matches_panicking_variant() {
        let mut tree = FrameTree::new();
        let root = add_root(&mut tree, "root".into());
        let child_state = make_state(
            0.3,
            DVec3::new(1e6, 2e6, 3e6),
            DVec3::new(100.0, 200.0, 300.0),
            DVec3::ZERO,
        );
        let child = add_child(&mut tree, root, "child".into(), child_state);

        let panicking = tree.compute_relative_state(root, child);
        let non_panicking = tree
            .try_compute_relative_state(root, child)
            .expect("should succeed");

        assert!(approx_eq_vec3(
            panicking.trans.position,
            non_panicking.trans.position,
            TOL
        ));
        assert!(approx_eq_vec3(
            panicking.trans.velocity,
            non_panicking.trans.velocity,
            TOL
        ));
        assert!(approx_eq_mat3(
            &panicking.rot.t_parent_this,
            &non_panicking.rot.t_parent_this,
            TOL
        ));
    }

    // -----------------------------------------------------------------------
    // Phase 3: typed sugar over the untyped arena.
    // -----------------------------------------------------------------------

    #[test]
    fn add_child_typed_round_trips_through_storage() {
        use astrodyn_quantities::frame::{Ecef, RootInertial};

        let mut tree = FrameTree::new();
        let root = tree.add_root_typed::<RootInertial>("inertial".into());

        let untyped = make_state(
            0.5,
            DVec3::new(1e6, 2e6, 3e6),
            DVec3::new(10.0, 20.0, 30.0),
            DVec3::new(0.001, 0.002, 0.003),
        );
        let typed_in = RefFrameStateTyped::<RootInertial, Ecef>::from_untyped_unchecked(&untyped);

        let child = tree.add_child_typed::<RootInertial, Ecef>(root, "ecef".into(), typed_in, None);

        // Read back via the typed accessor and assert the underlying
        // raw_si values match the original untyped input bit-identically.
        let typed_out: RefFrameStateTyped<RootInertial, Ecef> = tree.get_state_typed(child);
        assert_eq!(typed_out.trans.position.raw_si(), untyped.trans.position);
        assert_eq!(typed_out.trans.velocity.raw_si(), untyped.trans.velocity);
        assert_eq!(typed_out.rot.t_parent_this(), untyped.rot.t_parent_this);
        assert_eq!(
            typed_out.rot.ang_vel_this().raw_si(),
            untyped.rot.ang_vel_this
        );
    }

    #[test]
    fn add_child_typed_matches_untyped_storage() {
        use astrodyn_quantities::frame::{Ecef, RootInertial};

        let mut tree_a = FrameTree::new();
        let root_a = add_root(&mut tree_a, "inertial".into());
        let mut tree_b = FrameTree::new();
        let root_b = tree_b.add_root_typed::<RootInertial>("inertial".into());

        let untyped = make_state(
            FRAC_PI_2,
            DVec3::new(7e6, 0.0, 0.0),
            DVec3::new(0.0, 7000.0, 0.0),
            DVec3::new(0.0, 0.0, 7.292e-5),
        );
        let typed = RefFrameStateTyped::<RootInertial, Ecef>::from_untyped_unchecked(&untyped);

        let id_a = add_child(&mut tree_a, root_a, "a".into(), untyped);
        let id_b = tree_b.add_child_typed::<RootInertial, Ecef>(root_b, "b".into(), typed, None);

        assert_eq!(tree_a.get(id_a).state, tree_b.get(id_b).state);
    }
}

#[cfg(test)]
mod identity_tests {
    //! Issue #661: identity stamping, checked typed recovery, integrity
    //! validation, and multi-source composition. Uses concrete built-in
    //! markers only (no TS.01 wildcard tokens).

    use super::*;
    use crate::ref_frame_state::{RefFrameRot, RefFrameTrans};
    use astrodyn_math::JeodQuat;
    use astrodyn_quantities::frame::{Earth, Ecef, IntegrationFrame, PlanetInertial, RootInertial};
    use astrodyn_quantities::frame_descriptor::{FrameRole, FrameUid, Namespace, Tag};
    use glam::DVec3;

    /// A well-formed typed RootInertial → Ecef state for stamping tests.
    fn typed_state() -> RefFrameStateTyped<RootInertial, Ecef> {
        let q = JeodQuat::left_quat_from_eigen_rotation(0.3, DVec3::Z);
        let t = q.left_quat_to_transformation();
        RefFrameStateTyped::from_untyped_unchecked(&RefFrameState {
            trans: RefFrameTrans {
                position: DVec3::new(1.0e6, 2.0e6, 3.0e6),
                velocity: DVec3::new(10.0, 20.0, 30.0),
            },
            rot: RefFrameRot {
                q_parent_this: q,
                t_parent_this: t,
                ang_vel_this: DVec3::new(0.0, 0.0, 7.292e-5),
            },
        })
    }

    fn stamped_tree() -> (FrameTree, FrameId, FrameId) {
        let mut tree = FrameTree::new();
        let root = tree.add_root_typed::<RootInertial>("root".into());
        let ecef =
            tree.add_child_typed::<RootInertial, Ecef>(root, "ecef".into(), typed_state(), None);
        (tree, root, ecef)
    }

    // -- stamped construction -------------------------------------------------

    #[test]
    // JEOD_INV: RF.15 — negative test: a non-inertial class must not root
    // a tree via the typed constructor.
    #[should_panic(expected = "cannot root a tree")]
    fn add_root_typed_non_inertial_class_panics() {
        let mut tree = FrameTree::new();
        let _ = tree.add_root_typed::<Ecef>("ecef-root".into());
    }

    #[test]
    fn add_root_uid_stamps_and_indexes() {
        // The value-level root mint (issue #663 document loader): the
        // supplied identity is stamped and find-able, parent is None.
        let mut tree = FrameTree::new();
        let uid = FrameUid::external(
            Namespace(3),
            FrameClass::PlanetInertial,
            FrameRole::Primary,
            Tag::Named("Foreign".into()),
        );
        let root = tree.add_root_uid(uid.clone(), "foreign-root".into());
        assert_eq!(tree.find(&uid), Some(root));
        assert_eq!(tree.parent(root), None);
        assert_eq!(tree.get(root).uid(), &uid);
        tree.validate().expect("single stamped inertial root");
    }

    #[test]
    // JEOD_INV: RF.15 — negative test: a non-inertial class must not root
    // a tree via the value-level constructor.
    #[should_panic(expected = "cannot root a tree")]
    fn add_root_uid_non_inertial_class_panics() {
        let mut tree = FrameTree::new();
        let uid = FrameUid::external(
            Namespace(3),
            FrameClass::PlanetFixed,
            FrameRole::Primary,
            Tag::Named("Foreign".into()),
        );
        let _ = tree.add_root_uid(uid, "pfix-root".into());
    }

    #[test]
    // JEOD_INV: RF.14 — negative test: a duplicate identity registration
    // must be rejected at the point of introduction.
    #[should_panic(expected = "duplicate frame identity")]
    fn add_root_uid_duplicate_panics() {
        let mut tree = FrameTree::new();
        let _ = tree.add_root_uid(FrameUid::of::<RootInertial>(), "root".into());
        let _ = tree.add_root_uid(FrameUid::of::<RootInertial>(), "root2".into());
    }

    // `add_child_typed_parent_unstamped_panics` was deleted in issue
    // #664: identity is required at construction, so an unstamped parent
    // is unrepresentable and the panic arm it exercised no longer exists.

    #[test]
    #[should_panic(expected = "must match the frame it is parented to")]
    fn add_child_typed_parent_uid_mismatch_panics() {
        let (mut tree, _root, ecef) = stamped_tree();
        // Parent marker says RootInertial, but `ecef` is stamped Ecef.
        let _ = tree.add_child_typed::<RootInertial, PlanetInertial<Earth>>(
            ecef,
            "child".into(),
            RefFrameStateTyped::from_untyped_unchecked(&RefFrameState::default()),
            None,
        );
    }

    #[test]
    // JEOD_INV: RF.14 — negative test: minting the same typed identity
    // twice in one tree must be rejected.
    #[should_panic(expected = "duplicate frame identity")]
    fn duplicate_uid_panics() {
        let (mut tree, root, _ecef) = stamped_tree();
        let _ =
            tree.add_child_typed::<RootInertial, Ecef>(root, "ecef2".into(), typed_state(), None);
    }

    // -- identity lookup -------------------------------------------------------

    #[test]
    fn find_and_resolve_round_trip() {
        let (tree, root, ecef) = stamped_tree();
        assert_eq!(tree.find(&FrameUid::of::<RootInertial>()), Some(root));
        assert_eq!(tree.resolve(&FrameUid::of::<Ecef>()), ecef);
        assert_eq!(tree.find(&FrameUid::of::<PlanetInertial<Earth>>()), None);
    }

    #[test]
    #[should_panic(expected = "no frame with identity")]
    fn resolve_missing_panics() {
        let (tree, _, _) = stamped_tree();
        let _ = tree.resolve(&FrameUid::of::<PlanetInertial<Earth>>());
    }

    // -- checked typed recovery -------------------------------------------------
    // (`get_state_typed_unstamped_panics` was deleted in issue #664:
    // unstamped nodes are unrepresentable.)

    #[test]
    #[should_panic(expected = "identity mismatch")]
    fn get_state_typed_child_mismatch_panics() {
        let (tree, _root, ecef) = stamped_tree();
        let _: RefFrameStateTyped<RootInertial, PlanetInertial<Earth>> = tree.get_state_typed(ecef);
    }

    #[test]
    #[should_panic(expected = "predecessor identity mismatch")]
    // JEOD_INV: RF.02 — negative test: a typed read whose parent marker does
    // not name the stored predecessor identity must panic.
    fn get_state_typed_parent_mismatch_panics() {
        let (tree, _root, ecef) = stamped_tree();
        let _: RefFrameStateTyped<PlanetInertial<Earth>, Ecef> = tree.get_state_typed(ecef);
    }

    #[test]
    #[should_panic(expected = "is a root (no parent)")]
    fn get_state_typed_root_panics() {
        let (tree, root, _ecef) = stamped_tree();
        let _: RefFrameStateTyped<RootInertial, RootInertial> = tree.get_state_typed(root);
    }

    #[test]
    #[should_panic(expected = "is not mintable")]
    fn get_state_typed_non_mintable_child_panics() {
        let (tree, _root, ecef) = stamped_tree();
        let _: RefFrameStateTyped<RootInertial, IntegrationFrame> = tree.get_state_typed(ecef);
    }

    // -- validate ---------------------------------------------------------------

    #[test]
    fn validate_single_root_stamped_tree_ok() {
        let (tree, _, _) = stamped_tree();
        tree.validate().expect("stamped single-root tree validates");
    }

    // (`validate_skips_unstamped_nodes` was deleted in issue #664: every
    // node carries a required identity, so the identity checks run
    // unconditionally.)

    #[test]
    fn validate_multi_root_err_forest_ok() {
        let mut tree = FrameTree::new();
        let _a = tree.add_root_typed::<RootInertial>("a".into());
        let _b = tree.add_root_uid(
            FrameUid::external(
                Namespace(3),
                FrameClass::PlanetInertial,
                astrodyn_quantities::frame_descriptor::FrameRole::Primary,
                astrodyn_quantities::frame_descriptor::Tag::Named("b".into()),
            ),
            "b".into(),
        );
        assert!(matches!(
            tree.validate(),
            Err(FrameTreeError::MultiRoot(roots)) if roots.len() == 2
        ));
        tree.validate_forest()
            .expect("forest validation permits multiple roots");
    }

    #[test]
    fn validate_class_state_contradiction_err() {
        let mut tree = FrameTree::new();
        let root = tree.add_root_typed::<RootInertial>("root".into());
        // A PlanetInertial-classed (non-rotating) identity carrying a
        // non-zero angular velocity contradicts its classification.
        let mut state = RefFrameState::default();
        state.rot.ang_vel_this = DVec3::new(0.0, 0.0, 7.292e-5);
        tree.add_child_uid(
            root,
            FrameUid::of::<PlanetInertial<Earth>>(),
            "earth-inertial".into(),
            state,
            None,
        );
        assert!(matches!(
            tree.validate(),
            Err(FrameTreeError::ClassStateContradiction { .. })
        ));
    }

    #[test]
    fn validate_unit_norm_drift_err() {
        let mut tree = FrameTree::new();
        let root = tree.add_root_typed::<RootInertial>("root".into());
        // add_child_uid takes raw RefFrameState, so it can store a drifted
        // quaternion — exactly the hole validate() exists to catch.
        let mut state = RefFrameState::default();
        state.rot.q_parent_this = JeodQuat::from_array([1.1, 0.0, 0.0, 0.0]);
        tree.add_child_uid(
            root,
            FrameUid::of::<PlanetInertial<Earth>>(),
            "drifted".into(),
            state,
            None,
        );
        assert!(matches!(
            tree.validate(),
            Err(FrameTreeError::UnitNormDrift { .. })
        ));
    }

    // -- multi-source composition -------------------------------------------------

    #[test]
    fn import_subtree_restamps_and_maps() {
        let (mut local, _root, _ecef) = stamped_tree();
        let (foreign, f_root, f_ecef) = stamped_tree();

        let map = local.import_subtree(&foreign, Namespace(7));
        assert_eq!(map.len(), 2);
        let (old_root, new_root) = map[0];
        assert_eq!(old_root, f_root);

        // Imported identities live in namespace 7 — no collision with the
        // local LOCAL-namespace identities, and resolvable by re-stamped uid.
        let imported_ecef = local.resolve(&FrameUid::of::<Ecef>().with_namespace(Namespace(7)));
        assert_eq!(imported_ecef, map[1].1);
        assert_eq!(f_ecef, map[1].0);

        // Local identities are untouched.
        assert!(local.find(&FrameUid::of::<Ecef>()).is_some());
        // The import arrives as a disconnected root (forest until grafted).
        assert!(local.parent(new_root).is_none());
        local
            .validate_forest()
            .expect("post-import forest validates");
        assert!(matches!(
            local.validate(),
            Err(FrameTreeError::MultiRoot(_))
        ));
    }

    #[test]
    #[should_panic(expected = "reserved for type-derived")]
    fn import_subtree_local_namespace_panics() {
        let (mut local, _, _) = stamped_tree();
        let (foreign, _, _) = stamped_tree();
        let _ = local.import_subtree(&foreign, Namespace::LOCAL);
    }

    #[test]
    #[should_panic(expected = "do not share a common ancestor")]
    fn ungrafted_cross_source_query_fails_loudly() {
        let (mut local, _root, ecef) = stamped_tree();
        let (foreign, _, _) = stamped_tree();
        let map = local.import_subtree(&foreign, Namespace(7));
        let imported_ecef = map[1].1;
        let _ = local.compute_relative_state(ecef, imported_ecef);
    }

    #[test]
    #[should_panic(expected = "different namespaces")]
    fn ungrafted_cross_namespace_query_suggests_graft() {
        let (mut local, _root, ecef) = stamped_tree();
        let (foreign, _, _) = stamped_tree();
        let map = local.import_subtree(&foreign, Namespace(7));
        let _ = local.compute_relative_state(ecef, map[1].1);
    }

    #[test]
    fn graft_then_relative_state_succeeds() {
        let (mut local, root, ecef) = stamped_tree();
        let (foreign, _, _) = stamped_tree();
        let map = local.import_subtree(&foreign, Namespace(7));
        let (_, imported_root) = map[0];
        let (_, imported_ecef) = map[1];

        // Declare the relationship: the foreign root sits at +x 1 km from
        // our root (the host's physical claim).
        let mut graft_state = RefFrameState::default();
        graft_state.trans.position = DVec3::new(1000.0, 0.0, 0.0);
        local.graft(imported_root, root, graft_state);

        local.validate().expect("grafted tree is single-root again");
        // The previously-unanswerable cross-source query now has a path.
        let rel = local.compute_relative_state(ecef, imported_ecef);
        assert!(rel.trans.position.is_finite());
    }

    #[test]
    #[should_panic(expected = "only roots may be grafted")]
    fn graft_non_root_panics() {
        let (mut tree, root, ecef) = stamped_tree();
        tree.graft(ecef, root, RefFrameState::default());
    }

    #[test]
    #[should_panic(expected = "would create a cycle")]
    fn graft_cycle_panics() {
        let mut tree = FrameTree::new();
        let root = tree.add_root_typed::<RootInertial>("root".into());
        let child =
            tree.add_child_typed::<RootInertial, Ecef>(root, "ecef".into(), typed_state(), None);
        // Grafting the root under its own descendant must be rejected.
        tree.graft(root, child, RefFrameState::default());
    }

    #[test]
    #[should_panic(expected = "is not mintable")]
    fn add_child_typed_non_mintable_parent_panics() {
        let (mut tree, _root, ecef) = stamped_tree();
        let _ = tree.add_child_typed::<IntegrationFrame, Ecef>(
            ecef,
            "child".into(),
            RefFrameStateTyped::from_untyped_unchecked(&RefFrameState::default()),
            None,
        );
    }

    #[test]
    fn validate_duplicate_uid_err() {
        // White-box: corrupt a node's identity directly (the field is
        // private precisely so public callers cannot do this) to exercise
        // the load-path duplicate check.
        let (mut tree, _root, ecef) = stamped_tree();
        tree.nodes[ecef].uid = FrameUid::of::<RootInertial>();
        assert!(matches!(
            tree.validate(),
            Err(FrameTreeError::DuplicateUid(_))
        ));
    }

    #[test]
    fn validate_non_inertial_root_err() {
        // White-box: stamp a root with a non-root-eligible class to
        // exercise the load-path guard (constructors reject this case).
        let mut tree = FrameTree::new();
        let root = tree.add_root_typed::<RootInertial>("root".into());
        tree.nodes[root].uid = FrameUid::of::<Ecef>();
        assert!(matches!(
            tree.validate(),
            Err(FrameTreeError::NonInertialRoot { .. })
        ));
    }

    #[test]
    fn validate_nan_quat_norm_err() {
        // A NaN norm must be rejected, not silently passed (NaN compares
        // false to every threshold).
        let mut tree = FrameTree::new();
        let root = tree.add_root_typed::<RootInertial>("root".into());
        let mut state = RefFrameState::default();
        state.rot.q_parent_this = JeodQuat::from_array([f64::NAN, 0.0, 0.0, 0.0]);
        tree.add_child_uid(
            root,
            FrameUid::of::<PlanetInertial<Earth>>(),
            "nan".into(),
            state,
            None,
        );
        assert!(matches!(
            tree.validate(),
            Err(FrameTreeError::UnitNormDrift { .. })
        ));
    }

    #[test]
    #[should_panic(expected = "out of range")]
    fn set_epoch_out_of_range_panics() {
        let mut tree = FrameTree::new();
        tree.set_epoch(0, None);
    }

    // -- epoch ---------------------------------------------------------------------

    #[test]
    fn epoch_defaults_none_and_set_epoch_round_trips() {
        let (mut tree, _root, ecef) = stamped_tree();
        assert!(tree.get(ecef).epoch.is_none());
        let stamp = SecondsSince::<TDB>::from_seconds(123.456);
        tree.set_epoch(ecef, Some(stamp));
        let got = tree.get(ecef).epoch.expect("epoch was just set");
        // Bit-exact seconds comparison (SecondsSince<TDB> itself has no
        // PartialEq — the TDB marker doesn't derive it).
        assert_eq!(got.as_seconds().to_bits(), stamp.as_seconds().to_bits());
        tree.set_epoch(ecef, None);
        assert!(tree.get(ecef).epoch.is_none());
    }
}