asupersync 0.3.0

Spec-first, cancel-correct, capability-secure async runtime for Rust.
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
//! Typed namespace morphisms and facet-checked certificates for FABRIC.
//!
//! This module keeps the morphism surface finite and inspectable. A morphism
//! declares how one subject language lowers into another, what authority it
//! carries, whether the rewrite is reversible, what privacy and sharing rules
//! apply, and what quota envelope bounds the handoff.

use super::ir::{EvidencePolicy, MetadataDisclosure, PrivacyPolicy, ReplySpaceRule};
use super::subject::SubjectPattern;
use crate::util::DetHasher;
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;
use std::hash::Hasher;
use std::time::Duration;
use thiserror::Error;

/// Classification for a subject-language morphism.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
#[serde(rename_all = "snake_case")]
pub enum MorphismClass {
    /// Reversible, reply-authoritative, capability-bearing rewrites.
    Authoritative,
    /// Redacting or summarizing rewrites that must not originate authority.
    #[default]
    DerivedView,
    /// One-way export into a weaker trust or replay domain.
    Egress,
    /// Temporary sub-language delegation with bounded duration and revocation.
    Delegation,
}

impl MorphismClass {
    /// Exhaustive morphism-class taxonomy.
    pub const ALL: [Self; 4] = [
        Self::Authoritative,
        Self::DerivedView,
        Self::Egress,
        Self::Delegation,
    ];
}

/// Capability required to install or execute a morphism.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
#[serde(rename_all = "snake_case")]
pub enum FabricCapability {
    /// Rewrite or normalize a subject namespace.
    #[default]
    RewriteNamespace,
    /// Move authority across a morphism boundary.
    CarryAuthority,
    /// Rebind replies as authoritative responses.
    ReplyAuthority,
    /// Attach or inspect evidence produced by the morphism.
    ObserveEvidence,
    /// Delegate a bounded sub-language to another actor or steward.
    DelegateNamespace,
    /// Export traffic into a weaker or cross-boundary domain.
    CrossBoundaryEgress,
}

impl FabricCapability {
    /// Exhaustive capability taxonomy for the morphism surface.
    pub const ALL: [Self; 6] = [
        Self::RewriteNamespace,
        Self::CarryAuthority,
        Self::ReplyAuthority,
        Self::ObserveEvidence,
        Self::DelegateNamespace,
        Self::CrossBoundaryEgress,
    ];

    /// Return true when the capability moves or rebinds authority.
    #[must_use]
    pub const fn is_authority_bearing(self) -> bool {
        matches!(
            self,
            Self::CarryAuthority | Self::ReplyAuthority | Self::DelegateNamespace
        )
    }
}

/// Reversibility promise attached to a morphism.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
#[serde(rename_all = "snake_case")]
pub enum ReversibilityRequirement {
    /// The rewrite is explainable through retained evidence but not bijective.
    #[default]
    EvidenceBacked,
    /// The rewrite is structurally reversible without lossy steps.
    Bijective,
    /// The rewrite is intentionally one-way and may discard information.
    Irreversible,
}

/// Sharing boundary for traffic after the morphism applies.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
#[serde(rename_all = "snake_case")]
pub enum SharingPolicy {
    /// Keep the rewritten traffic inside the local authority boundary.
    #[default]
    Private,
    /// Share only within a tenant-scoped boundary.
    TenantScoped,
    /// Share across a federated but still policy-bound domain.
    Federated,
    /// Allow public read access to the rewritten output.
    PublicRead,
}

/// Reply-handling rule for the rewritten namespace.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
#[serde(rename_all = "snake_case")]
pub enum ResponsePolicy {
    /// Preserve caller-managed reply semantics.
    #[default]
    PreserveCallerReplies,
    /// Rebind replies as authoritative responses from the morphism destination.
    ReplyAuthoritative,
    /// Forward replies opaquely without rebinding authority.
    ForwardOpaque,
    /// Strip reply semantics entirely.
    StripReplies,
}

/// Finite transform vocabulary for typed namespace rewrites.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum SubjectTransform {
    /// Leave the subject language unchanged.
    #[default]
    Identity,
    /// Rename one namespace prefix to another.
    RenamePrefix {
        /// Source prefix to rewrite.
        from: SubjectPattern,
        /// Target prefix after rewriting.
        to: SubjectPattern,
    },
    /// Redact literal subject segments while preserving shape.
    RedactLiterals,
    /// Collapse the tail of a subject after keeping the leading prefix.
    SummarizeTail {
        /// Number of leading segments that remain visible.
        preserve_segments: usize,
    },
    /// Hash-partition the rewritten stream into a bounded number of buckets.
    HashPartition {
        /// Number of output buckets.
        buckets: u16,
    },
    /// Capture a single wildcard expansion by its 1-based index.
    WildcardCapture {
        /// 1-based wildcard/capture index.
        index: usize,
    },
    /// Deterministically hash one or more captured tokens into a bucket.
    DeterministicHash {
        /// Number of output buckets.
        buckets: u16,
        /// 1-based token indices used as the hash key. Empty means all tokens.
        source_indices: Vec<usize>,
    },
    /// Split a captured token and keep a bounded slice of the resulting pieces.
    SplitSlice {
        /// 1-based token index to split.
        index: usize,
        /// Delimiter used to split the selected token.
        delimiter: String,
        /// Zero-based starting piece after splitting.
        start: usize,
        /// Number of split pieces to keep.
        len: usize,
    },
    /// Keep the left-most characters from a captured token.
    LeftExtract {
        /// 1-based token index to project.
        index: usize,
        /// Number of characters to keep from the left.
        len: usize,
    },
    /// Keep the right-most characters from a captured token.
    RightExtract {
        /// 1-based token index to project.
        index: usize,
        /// Number of characters to keep from the right.
        len: usize,
    },
    /// Compose a finite sequence of transforms into a deterministic pipeline.
    Compose {
        /// Ordered transform pipeline.
        steps: Vec<Self>,
    },
}

impl SubjectTransform {
    /// Return true when the transform intentionally discards information.
    #[must_use]
    pub fn is_lossy(&self) -> bool {
        match self {
            Self::Identity | Self::RenamePrefix { .. } => false,
            Self::Compose { steps } => steps.iter().any(Self::is_lossy),
            Self::RedactLiterals
            | Self::SummarizeTail { .. }
            | Self::HashPartition { .. }
            | Self::WildcardCapture { .. }
            | Self::DeterministicHash { .. }
            | Self::SplitSlice { .. }
            | Self::LeftExtract { .. }
            | Self::RightExtract { .. } => true,
        }
    }

    /// Return true when the transform admits a structural inverse.
    #[must_use]
    pub fn is_invertible(&self) -> bool {
        self.inverse().is_some()
    }

    /// Return the structural inverse when the transform is bijective.
    #[must_use]
    pub fn inverse(&self) -> Option<Self> {
        match self {
            Self::Identity => Some(Self::Identity),
            Self::RenamePrefix { from, to } => Some(Self::RenamePrefix {
                from: to.clone(),
                to: from.clone(),
            }),
            Self::Compose { steps } => {
                let mut inverse_steps = Vec::with_capacity(steps.len());
                for step in steps.iter().rev() {
                    inverse_steps.push(step.inverse()?);
                }
                Some(Self::Compose {
                    steps: inverse_steps,
                })
            }
            Self::RedactLiterals
            | Self::SummarizeTail { .. }
            | Self::HashPartition { .. }
            | Self::WildcardCapture { .. }
            | Self::DeterministicHash { .. }
            | Self::SplitSlice { .. }
            | Self::LeftExtract { .. }
            | Self::RightExtract { .. } => None,
        }
    }

    /// Apply the transform deterministically to a token vector.
    ///
    /// Higher layers can feed this with wildcard captures or a tokenized
    /// concrete subject depending on which facet they are evaluating.
    pub fn apply_tokens(&self, tokens: &[String]) -> Result<Vec<String>, MorphismEvaluationError> {
        match self {
            Self::Identity => Ok(tokens.to_vec()),
            Self::RenamePrefix { from, to } => {
                let from_literals = literal_only_segments(from)?;
                let to_literals = literal_only_segments(to)?;
                if tokens.starts_with(&from_literals) {
                    let mut rewritten = to_literals;
                    rewritten.extend_from_slice(&tokens[from_literals.len()..]);
                    Ok(rewritten)
                } else {
                    Ok(tokens.to_vec())
                }
            }
            Self::RedactLiterals => Ok(tokens.iter().map(|_| String::from("_")).collect()),
            Self::SummarizeTail { preserve_segments } => {
                if tokens.len() <= *preserve_segments {
                    return Ok(tokens.to_vec());
                }
                let mut summarized = tokens[..*preserve_segments].to_vec();
                summarized.push(String::from("..."));
                Ok(summarized)
            }
            Self::HashPartition { buckets } => Ok(vec![
                deterministic_bucket(tokens, &[], *buckets)?.to_string(),
            ]),
            Self::WildcardCapture { index } => Ok(vec![select_token(tokens, *index)?.to_owned()]),
            Self::DeterministicHash {
                buckets,
                source_indices,
            } => Ok(vec![
                deterministic_bucket(tokens, source_indices, *buckets)?.to_string(),
            ]),
            Self::SplitSlice {
                index,
                delimiter,
                start,
                len,
            } => {
                let token = select_token(tokens, *index)?;
                let pieces = token.split(delimiter).collect::<Vec<_>>();
                if *start >= pieces.len() {
                    return Ok(Vec::new());
                }
                let end = start.saturating_add(*len).min(pieces.len());
                Ok(pieces[*start..end]
                    .iter()
                    .map(|piece| (*piece).to_owned())
                    .collect())
            }
            Self::LeftExtract { index, len } => {
                let token = select_token(tokens, *index)?;
                Ok(vec![take_left(token, *len)])
            }
            Self::RightExtract { index, len } => {
                let token = select_token(tokens, *index)?;
                Ok(vec![take_right(token, *len)])
            }
            Self::Compose { steps } => {
                let mut current = tokens.to_vec();
                for step in steps {
                    current = step.apply_tokens(&current)?;
                }
                Ok(current)
            }
        }
    }

    fn validate(&self) -> Result<(), MorphismValidationError> {
        match self {
            Self::RenamePrefix { from, to } if from == to => {
                Err(MorphismValidationError::RenamePrefixIdentity)
            }
            Self::SummarizeTail { preserve_segments } if *preserve_segments == 0 => {
                Err(MorphismValidationError::SummarizeTailMustPreserveSegments)
            }
            Self::HashPartition { buckets } if *buckets == 0 => {
                Err(MorphismValidationError::HashPartitionRequiresBuckets)
            }
            Self::WildcardCapture { index } if *index == 0 => {
                Err(MorphismValidationError::WildcardCaptureRequiresIndex)
            }
            Self::DeterministicHash { buckets, .. } if *buckets == 0 => {
                Err(MorphismValidationError::DeterministicHashRequiresBuckets)
            }
            Self::DeterministicHash { source_indices, .. } if source_indices.contains(&0) => {
                Err(MorphismValidationError::DeterministicHashIndexMustBePositive)
            }
            Self::SplitSlice { index, .. } if *index == 0 => {
                Err(MorphismValidationError::SplitSliceRequiresIndex)
            }
            Self::SplitSlice { delimiter, .. } if delimiter.is_empty() => {
                Err(MorphismValidationError::SplitSliceRequiresDelimiter)
            }
            Self::SplitSlice { len, .. } if *len == 0 => {
                Err(MorphismValidationError::SplitSliceRequiresLength)
            }
            Self::LeftExtract { index, .. } if *index == 0 => {
                Err(MorphismValidationError::LeftExtractRequiresIndex)
            }
            Self::LeftExtract { len, .. } if *len == 0 => {
                Err(MorphismValidationError::LeftExtractRequiresLength)
            }
            Self::RightExtract { index, .. } if *index == 0 => {
                Err(MorphismValidationError::RightExtractRequiresIndex)
            }
            Self::RightExtract { len, .. } if *len == 0 => {
                Err(MorphismValidationError::RightExtractRequiresLength)
            }
            Self::Compose { steps } if steps.is_empty() => {
                Err(MorphismValidationError::ComposeRequiresSteps)
            }
            Self::Compose { steps } => {
                for step in steps {
                    step.validate()?;
                }
                Ok(())
            }
            Self::Identity
            | Self::RedactLiterals
            | Self::RenamePrefix { .. }
            | Self::SummarizeTail { .. }
            | Self::HashPartition { .. }
            | Self::WildcardCapture { .. }
            | Self::DeterministicHash { .. }
            | Self::SplitSlice { .. }
            | Self::LeftExtract { .. }
            | Self::RightExtract { .. } => Ok(()),
        }
    }
}

/// Cost and handoff envelope for a morphism.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct QuotaPolicy {
    /// Maximum multiplicative expansion factor after rewriting.
    pub max_expansion_factor: u16,
    /// Maximum delivery fanout created by the morphism.
    pub max_fanout: u16,
    /// Maximum evidence or observability bytes emitted per decision.
    pub max_observability_bytes: u32,
    /// Maximum duration a delegated morphism may remain active.
    pub max_handoff_duration: Option<Duration>,
    /// Whether the handoff must support explicit revocation.
    pub revocation_required: bool,
}

impl Default for QuotaPolicy {
    fn default() -> Self {
        Self {
            max_expansion_factor: 1,
            max_fanout: 1,
            max_observability_bytes: 4_096,
            max_handoff_duration: None,
            revocation_required: false,
        }
    }
}

impl QuotaPolicy {
    fn validate(&self) -> Result<(), MorphismValidationError> {
        if self.max_expansion_factor == 0 {
            return Err(MorphismValidationError::ZeroMaxExpansionFactor);
        }
        if self.max_fanout == 0 {
            return Err(MorphismValidationError::ZeroMaxFanout);
        }
        if self.max_observability_bytes == 0 {
            return Err(MorphismValidationError::ZeroMaxObservabilityBytes);
        }
        if self
            .max_handoff_duration
            .is_some_and(|duration| duration.is_zero())
        {
            return Err(MorphismValidationError::ZeroMaxHandoffDuration);
        }
        Ok(())
    }
}

/// Typed namespace morphism declaration.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Morphism {
    /// Source subject language accepted by the morphism.
    pub source_language: SubjectPattern,
    /// Destination subject language emitted by the morphism.
    pub dest_language: SubjectPattern,
    /// High-level morphism class.
    pub class: MorphismClass,
    /// Concrete transform algebra element.
    pub transform: SubjectTransform,
    /// Reversibility promise for the rewrite.
    pub reversibility: ReversibilityRequirement,
    /// Capabilities required to authorize the morphism.
    pub capability_requirements: Vec<FabricCapability>,
    /// Sharing boundary for the rewritten output.
    pub sharing_policy: SharingPolicy,
    /// Privacy and metadata disclosure policy.
    pub privacy_policy: PrivacyPolicy,
    /// Reply-handling semantics after rewriting.
    pub response_policy: ResponsePolicy,
    /// Bounded quota envelope for the morphism.
    pub quota_policy: QuotaPolicy,
    /// Evidence policy attached to the morphism.
    pub evidence_policy: EvidencePolicy,
}

impl Default for Morphism {
    fn default() -> Self {
        Self {
            source_language: SubjectPattern::new("fabric.subject.>"),
            dest_language: SubjectPattern::new("fabric.subject.>"),
            class: MorphismClass::DerivedView,
            transform: SubjectTransform::Identity,
            reversibility: ReversibilityRequirement::EvidenceBacked,
            capability_requirements: vec![FabricCapability::RewriteNamespace],
            sharing_policy: SharingPolicy::Private,
            privacy_policy: PrivacyPolicy::default(),
            response_policy: ResponsePolicy::PreserveCallerReplies,
            quota_policy: QuotaPolicy::default(),
            evidence_policy: EvidencePolicy::default(),
        }
    }
}

impl Morphism {
    /// Validate the morphism against class-specific guardrails.
    pub fn validate(&self) -> Result<(), MorphismValidationError> {
        self.transform.validate()?;
        self.quota_policy.validate()?;

        if let Some(duplicate) = duplicate_capability(&self.capability_requirements) {
            return Err(MorphismValidationError::DuplicateCapability(duplicate));
        }
        if self.reversibility == ReversibilityRequirement::Bijective
            && !self.transform.is_invertible()
        {
            return Err(MorphismValidationError::TransformCannotSatisfyBijectiveRequirement);
        }

        match self.class {
            MorphismClass::Authoritative => {
                if self.capability_requirements.is_empty() {
                    return Err(MorphismValidationError::AuthoritativeRequiresCapability);
                }
                if !self
                    .capability_requirements
                    .iter()
                    .copied()
                    .any(FabricCapability::is_authority_bearing)
                {
                    return Err(MorphismValidationError::AuthoritativeRequiresAuthorityCapability);
                }
                if self.response_policy != ResponsePolicy::ReplyAuthoritative {
                    return Err(MorphismValidationError::AuthoritativeRequiresReplyAuthority);
                }
                if self.reversibility == ReversibilityRequirement::Irreversible {
                    return Err(MorphismValidationError::AuthoritativeMustBeReversible);
                }
                if self.transform.is_lossy() {
                    return Err(MorphismValidationError::AuthoritativeTransformMustBeLossless);
                }
                if let SubjectTransform::RenamePrefix { from, to } = &self.transform
                    && (from.has_wildcards() || to.has_wildcards())
                {
                    return Err(MorphismValidationError::AuthoritativeRenameMustBeLiteralOnly);
                }
            }
            MorphismClass::DerivedView => {
                if self
                    .capability_requirements
                    .iter()
                    .copied()
                    .any(FabricCapability::is_authority_bearing)
                {
                    return Err(
                        MorphismValidationError::DerivedViewCannotRequireAuthorityCapability,
                    );
                }
                if self.response_policy == ResponsePolicy::ReplyAuthoritative {
                    return Err(MorphismValidationError::DerivedViewCannotOriginateReplyAuthority);
                }
            }
            MorphismClass::Egress => {
                if self.response_policy != ResponsePolicy::StripReplies {
                    return Err(MorphismValidationError::EgressMustStripReplies);
                }
                if self.reversibility != ReversibilityRequirement::Irreversible {
                    return Err(MorphismValidationError::EgressMustBeIrreversible);
                }
                if self.sharing_policy == SharingPolicy::Private {
                    return Err(MorphismValidationError::EgressMustCrossBoundary);
                }
            }
            MorphismClass::Delegation => {
                if !self
                    .capability_requirements
                    .contains(&FabricCapability::DelegateNamespace)
                {
                    return Err(MorphismValidationError::DelegationRequiresDelegateCapability);
                }
                if self.reversibility == ReversibilityRequirement::Irreversible {
                    return Err(MorphismValidationError::DelegationMustBeReversible);
                }
                if self.transform.is_lossy() {
                    return Err(MorphismValidationError::DelegationTransformMustBeLossless);
                }
                if self.quota_policy.max_handoff_duration.is_none() {
                    return Err(MorphismValidationError::DelegationMustBeTimeBounded);
                }
                if !self.quota_policy.revocation_required {
                    return Err(MorphismValidationError::DelegationMustBeRevocable);
                }
            }
        }

        Ok(())
    }

    /// Return the authority facet of the morphism.
    #[must_use]
    pub fn authority_facet(&self) -> AuthorityFacet {
        AuthorityFacet {
            class: self.class,
            capability_requirements: canonical_capabilities(&self.capability_requirements),
            response_policy: self.response_policy,
        }
    }

    /// Return the reversibility facet of the morphism.
    #[must_use]
    pub fn reversibility_facet(&self) -> ReversibilityFacet {
        ReversibilityFacet {
            requirement: self.reversibility,
            lossy_transform: self.transform.is_lossy(),
        }
    }

    /// Return the secrecy and metadata-exposure facet of the morphism.
    #[must_use]
    pub fn secrecy_facet(&self) -> SecrecyFacet {
        SecrecyFacet {
            sharing_policy: self.sharing_policy,
            privacy_policy: self.privacy_policy.clone(),
        }
    }

    /// Return the bounded cost and quota facet of the morphism.
    #[must_use]
    pub fn cost_facet(&self) -> CostFacet {
        CostFacet {
            quota_policy: self.quota_policy.clone(),
        }
    }

    /// Return the observability and evidence facet of the morphism.
    #[must_use]
    pub fn observability_facet(&self) -> ObservabilityFacet {
        ObservabilityFacet {
            evidence_policy: self.evidence_policy.clone(),
        }
    }

    /// Return all five independently checkable morphism facets.
    #[must_use]
    pub fn facet_set(&self) -> MorphismFacetSet {
        MorphismFacetSet {
            authority: self.authority_facet(),
            reversibility: self.reversibility_facet(),
            secrecy: self.secrecy_facet(),
            cost: self.cost_facet(),
            observability: self.observability_facet(),
        }
    }

    /// Compile the morphism into a deterministic certificate.
    pub fn compile(&self) -> Result<MorphismCertificate, MorphismValidationError> {
        self.validate()?;

        let bytes = serde_json::to_vec(self)
            .map_err(|error| MorphismValidationError::SerializeCertificate(error.to_string()))?;
        let mut hasher = DetHasher::default();
        hasher.write(&bytes);

        Ok(MorphismCertificate {
            fingerprint: format!("{:016x}", hasher.finish()),
            class: self.class,
            source_language: self.source_language.clone(),
            dest_language: self.dest_language.clone(),
            transform: self.transform.clone(),
            facets: self.facet_set(),
        })
    }

    /// Compile the morphism into a verified export-side boundary plan.
    pub fn compile_export_plan(
        &self,
        requested_reply_space: Option<ReplySpaceRule>,
    ) -> Result<ExportPlan, MorphismCompileError> {
        let parts =
            compile_boundary_plan(self, MorphismPlanDirection::Export, requested_reply_space)?;
        Ok(ExportPlan {
            direction: parts.direction,
            certificate: parts.certificate,
            attached_capabilities: parts.attached_capabilities,
            selected_reply_space: parts.selected_reply_space,
            permitted_reply_spaces: parts.permitted_reply_spaces,
            metadata_boundary: parts.metadata_boundary,
            steps: parts.steps,
            reasoning: parts.reasoning,
        })
    }

    /// Compile the morphism into a verified import-side boundary plan.
    pub fn compile_import_plan(
        &self,
        requested_reply_space: Option<ReplySpaceRule>,
    ) -> Result<ImportPlan, MorphismCompileError> {
        let parts =
            compile_boundary_plan(self, MorphismPlanDirection::Import, requested_reply_space)?;
        Ok(ImportPlan {
            direction: parts.direction,
            certificate: parts.certificate,
            attached_capabilities: parts.attached_capabilities,
            selected_reply_space: parts.selected_reply_space,
            permitted_reply_spaces: parts.permitted_reply_spaces,
            metadata_boundary: parts.metadata_boundary,
            steps: parts.steps,
            reasoning: parts.reasoning,
        })
    }

    fn crosses_boundary(&self) -> bool {
        self.sharing_policy != SharingPolicy::Private
            || matches!(
                self.class,
                MorphismClass::Egress | MorphismClass::Delegation
            )
    }
}

/// Independently checkable authority facet.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AuthorityFacet {
    /// Class that determines the authority envelope.
    pub class: MorphismClass,
    /// Canonicalized capability requirements.
    pub capability_requirements: Vec<FabricCapability>,
    /// Reply-handling policy for the rewritten namespace.
    pub response_policy: ResponsePolicy,
}

/// Independently checkable reversibility facet.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReversibilityFacet {
    /// Declared reversibility requirement.
    pub requirement: ReversibilityRequirement,
    /// Whether the chosen transform is intrinsically lossy.
    pub lossy_transform: bool,
}

/// Independently checkable secrecy and metadata-exposure facet.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SecrecyFacet {
    /// Sharing boundary after the morphism applies.
    pub sharing_policy: SharingPolicy,
    /// Privacy rules for metadata and subject disclosure.
    pub privacy_policy: PrivacyPolicy,
}

/// Independently checkable cost and quota facet.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CostFacet {
    /// Quota envelope that bounds expansion, fanout, and delegation.
    pub quota_policy: QuotaPolicy,
}

/// Independently checkable observability and evidence facet.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ObservabilityFacet {
    /// Evidence policy emitted by the morphism.
    pub evidence_policy: EvidencePolicy,
}

/// Aggregated view of the five morphism facets.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MorphismFacetSet {
    /// Authority and capability requirements.
    pub authority: AuthorityFacet,
    /// Reversibility contract.
    pub reversibility: ReversibilityFacet,
    /// Secrecy and metadata-disclosure policy.
    pub secrecy: SecrecyFacet,
    /// Cost and quota envelope.
    pub cost: CostFacet,
    /// Evidence and observability obligations.
    pub observability: ObservabilityFacet,
}

/// Deterministic compiled artifact for a validated morphism.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MorphismCertificate {
    /// Stable fingerprint over the serialized morphism declaration.
    pub fingerprint: String,
    /// Class of the validated morphism.
    pub class: MorphismClass,
    /// Source language encoded into the certificate.
    pub source_language: SubjectPattern,
    /// Destination language encoded into the certificate.
    pub dest_language: SubjectPattern,
    /// Transform algebra element encoded into the certificate.
    pub transform: SubjectTransform,
    /// Faceted summary used by downstream validators.
    pub facets: MorphismFacetSet,
}

/// Direction for a compiled morphism boundary plan.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default,
)]
#[serde(rename_all = "snake_case")]
pub enum MorphismPlanDirection {
    /// Compile an inbound/import-side boundary plan.
    Import,
    /// Compile an outbound/export-side boundary plan.
    #[default]
    Export,
}

impl MorphismPlanDirection {
    #[must_use]
    const fn as_str(self) -> &'static str {
        match self {
            Self::Import => "import",
            Self::Export => "export",
        }
    }
}

/// Deterministic execution steps in a compiled boundary plan.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MorphismPlanStep {
    /// Match the incoming namespace against the source language.
    MatchSourceLanguage,
    /// Enforce the attached capability envelope.
    EnforceCapabilityEnvelope,
    /// Apply the transform certificate deterministically.
    ApplyTransformCertificate,
    /// Enforce reply-space policy for cross-boundary requests.
    EnforceReplySpace,
    /// Enforce metadata disclosure policy at the boundary.
    EnforceMetadataBoundary,
    /// Emit auditable reasoning for the plan installation.
    EmitAuditReasoning,
}

/// Semantic class for a risky morphism cycle.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SemanticCycleClass {
    /// The loop could preserve or re-amplify authority.
    Authority,
    /// The loop hides a lossy or irreversible rewrite.
    Reversibility,
    /// The loop composes boundary-crossing capabilities into a cycle.
    Capability,
}

/// Auditable summary of what metadata crosses a morphism boundary.
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MetadataBoundarySummary {
    /// Whether the plan crosses an authority or stewardship boundary.
    pub crosses_boundary: bool,
    /// Metadata disclosure level across the boundary.
    pub metadata_disclosure: MetadataDisclosure,
    /// Whether literal subject segments are redacted before crossing.
    pub subject_literals_redacted: bool,
    /// Whether cross-tenant metadata movement is explicitly allowed.
    pub cross_tenant_flow_allowed: bool,
    /// Whether payload hashes remain observable after the boundary.
    pub payload_hashes_recorded: bool,
}

/// Auditable reasoning note attached to a compiled plan.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MorphismAuditNote {
    /// Stable category for the note.
    pub code: String,
    /// Human-readable explanation for auditors.
    pub detail: String,
}

/// Compiled export-side routing plan for a morphism boundary.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExportPlan {
    /// Direction for the compiled plan.
    pub direction: MorphismPlanDirection,
    /// Transform certificate emitted by the compiler.
    pub certificate: MorphismCertificate,
    /// Canonical attached capability envelope.
    pub attached_capabilities: Vec<FabricCapability>,
    /// Reply space selected for the boundary.
    pub selected_reply_space: Option<ReplySpaceRule>,
    /// Reply spaces permitted by the compiled policy.
    pub permitted_reply_spaces: Vec<ReplySpaceRule>,
    /// Metadata disclosure summary for the boundary.
    pub metadata_boundary: MetadataBoundarySummary,
    /// Deterministic execution steps for the plan.
    pub steps: Vec<MorphismPlanStep>,
    /// Auditable reasoning notes emitted by compilation.
    pub reasoning: Vec<MorphismAuditNote>,
}

/// Compiled import-side routing plan for a morphism boundary.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ImportPlan {
    /// Direction for the compiled plan.
    pub direction: MorphismPlanDirection,
    /// Transform certificate emitted by the compiler.
    pub certificate: MorphismCertificate,
    /// Canonical attached capability envelope.
    pub attached_capabilities: Vec<FabricCapability>,
    /// Reply space selected for the boundary.
    pub selected_reply_space: Option<ReplySpaceRule>,
    /// Reply spaces permitted by the compiled policy.
    pub permitted_reply_spaces: Vec<ReplySpaceRule>,
    /// Metadata disclosure summary for the boundary.
    pub metadata_boundary: MetadataBoundarySummary,
    /// Deterministic execution steps for the plan.
    pub steps: Vec<MorphismPlanStep>,
    /// Auditable reasoning notes emitted by compilation.
    pub reasoning: Vec<MorphismAuditNote>,
}

/// Compilation failures while building import/export plans.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum MorphismCompileError {
    /// The morphism declaration itself failed validation.
    #[error(transparent)]
    InvalidMorphism(#[from] MorphismValidationError),
    /// Cross-boundary traffic selected a forbidden reply space.
    #[error(
        "cross-boundary reply space `{requested:?}` is not permitted for response policy `{policy:?}`; permitted reply spaces: {permitted:?}"
    )]
    ReplySpaceNotPermitted {
        /// Response policy being enforced.
        policy: ResponsePolicy,
        /// Requested reply space for the boundary.
        requested: ReplySpaceRule,
        /// Reply spaces admitted by the compiler.
        permitted: Vec<ReplySpaceRule>,
    },
    /// Cross-boundary traffic attempted to keep replies when the policy strips them.
    #[error("cross-boundary replies are forbidden for response policy `{policy:?}`")]
    ReplySpaceForbidden {
        /// Response policy being enforced.
        policy: ResponsePolicy,
    },
    /// A morphism chain creates a risky semantic loop.
    #[error("semantic {class:?} cycle detected across morphism chain {path:?}")]
    SemanticCycleDetected {
        /// Semantic class of the detected loop.
        class: SemanticCycleClass,
        /// Human-readable path for the cycle.
        path: Vec<String>,
    },
    /// The privacy boundary permits more metadata than the policy authorizes.
    #[error(
        "sharing policy `{sharing_policy:?}` with metadata disclosure `{metadata_disclosure:?}` requires explicit cross-tenant permission"
    )]
    MetadataBoundaryViolation {
        /// Boundary sharing level that triggered the violation.
        sharing_policy: SharingPolicy,
        /// Metadata disclosure mode that exceeded policy.
        metadata_disclosure: MetadataDisclosure,
    },
}

/// Detect risky semantic cycles across a chain of morphisms.
pub fn detect_semantic_cycles(morphisms: &[Morphism]) -> Result<(), MorphismCompileError> {
    for morphism in morphisms {
        morphism.validate()?;
    }

    for start in 0..morphisms.len() {
        let mut path = vec![start];
        let mut visited = BTreeSet::from([start]);
        detect_semantic_cycle_from(morphisms, start, start, &mut path, &mut visited)?;
    }

    Ok(())
}

/// Validation failures for typed namespace morphisms.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum MorphismValidationError {
    /// Authoritative morphisms must declare at least one capability.
    #[error("authoritative morphisms require at least one capability")]
    AuthoritativeRequiresCapability,
    /// Authoritative morphisms must carry an authority-bearing capability.
    #[error("authoritative morphisms require an authority-bearing capability")]
    AuthoritativeRequiresAuthorityCapability,
    /// Authoritative morphisms must control replies explicitly.
    #[error("authoritative morphisms must use reply-authoritative response policy")]
    AuthoritativeRequiresReplyAuthority,
    /// Authoritative morphisms must not be declared one-way.
    #[error("authoritative morphisms must be reversible")]
    AuthoritativeMustBeReversible,
    /// Authoritative morphisms must not use lossy transforms.
    #[error("authoritative morphisms must use lossless transforms")]
    AuthoritativeTransformMustBeLossless,
    /// Authoritative prefix rewrites must stay literal-only.
    #[error("authoritative rename-prefix morphisms must use literal-only patterns")]
    AuthoritativeRenameMustBeLiteralOnly,
    /// Derived views must not require authority-bearing capabilities.
    #[error("derived-view morphisms must not require authority-bearing capabilities")]
    DerivedViewCannotRequireAuthorityCapability,
    /// Derived views must not rebind replies as authority.
    #[error("derived-view morphisms must not originate reply authority")]
    DerivedViewCannotOriginateReplyAuthority,
    /// Egress morphisms must strip reply semantics.
    #[error("egress morphisms must strip replies")]
    EgressMustStripReplies,
    /// Egress morphisms are intentionally one-way.
    #[error("egress morphisms must be irreversible")]
    EgressMustBeIrreversible,
    /// Egress morphisms must leave the private boundary.
    #[error("egress morphisms must cross a non-private sharing boundary")]
    EgressMustCrossBoundary,
    /// Delegation requires the delegation capability.
    #[error("delegation morphisms require delegate-namespace capability")]
    DelegationRequiresDelegateCapability,
    /// Delegation preserves authority and therefore must not be one-way.
    #[error("delegation morphisms must be reversible")]
    DelegationMustBeReversible,
    /// Delegation preserves authority and therefore must not use lossy rewrites.
    #[error("delegation morphisms must use lossless transforms")]
    DelegationTransformMustBeLossless,
    /// Delegation must declare a finite handoff duration.
    #[error("delegation morphisms must declare a bounded handoff duration")]
    DelegationMustBeTimeBounded,
    /// Delegation must be explicitly revocable.
    #[error("delegation morphisms must be revocable")]
    DelegationMustBeRevocable,
    /// Capability requirements must be unique.
    #[error("duplicate capability requirement `{0:?}`")]
    DuplicateCapability(FabricCapability),
    /// Rename-prefix transforms must actually change the namespace.
    #[error("rename-prefix transform must change the namespace")]
    RenamePrefixIdentity,
    /// Tail summarization must preserve at least one segment.
    #[error("summarize-tail transform must preserve at least one segment")]
    SummarizeTailMustPreserveSegments,
    /// Hash partitioning requires at least one bucket.
    #[error("hash-partition transform requires at least one bucket")]
    HashPartitionRequiresBuckets,
    /// Wildcard capture transforms must name a 1-based index.
    #[error("wildcard-capture transform requires a positive index")]
    WildcardCaptureRequiresIndex,
    /// Deterministic hash transforms require at least one bucket.
    #[error("deterministic-hash transform requires at least one bucket")]
    DeterministicHashRequiresBuckets,
    /// Deterministic hash transforms use 1-based token indices.
    #[error("deterministic-hash source indices must be positive")]
    DeterministicHashIndexMustBePositive,
    /// Split-and-slice transforms must name a 1-based token index.
    #[error("split-slice transform requires a positive token index")]
    SplitSliceRequiresIndex,
    /// Split-and-slice transforms need a non-empty delimiter.
    #[error("split-slice transform requires a non-empty delimiter")]
    SplitSliceRequiresDelimiter,
    /// Split-and-slice transforms must keep at least one piece.
    #[error("split-slice transform requires a positive slice length")]
    SplitSliceRequiresLength,
    /// Left-extract transforms must name a 1-based token index.
    #[error("left-extract transform requires a positive token index")]
    LeftExtractRequiresIndex,
    /// Left-extract transforms must keep at least one character.
    #[error("left-extract transform requires a positive length")]
    LeftExtractRequiresLength,
    /// Right-extract transforms must name a 1-based token index.
    #[error("right-extract transform requires a positive token index")]
    RightExtractRequiresIndex,
    /// Right-extract transforms must keep at least one character.
    #[error("right-extract transform requires a positive length")]
    RightExtractRequiresLength,
    /// Compose transforms must contain at least one step.
    #[error("compose transform requires at least one step")]
    ComposeRequiresSteps,
    /// Bijective requirements need an invertible transform.
    #[error("bijective reversibility requires an invertible transform")]
    TransformCannotSatisfyBijectiveRequirement,
    /// Expansion factor must be positive.
    #[error("quota max expansion factor must be greater than zero")]
    ZeroMaxExpansionFactor,
    /// Fanout must be positive.
    #[error("quota max fanout must be greater than zero")]
    ZeroMaxFanout,
    /// Observability budget must be positive.
    #[error("quota max observability bytes must be greater than zero")]
    ZeroMaxObservabilityBytes,
    /// Handoff duration must be positive when present.
    #[error("quota max handoff duration must be greater than zero")]
    ZeroMaxHandoffDuration,
    /// Certificate compilation failed while serializing the morphism.
    #[error("failed to serialize morphism certificate: {0}")]
    SerializeCertificate(String),
}

/// Evaluation failures while executing a deterministic transform pipeline.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum MorphismEvaluationError {
    /// A transform referenced a token/capture index that is not present.
    #[error("token index {index} is out of range for {available} available tokens")]
    TokenIndexOutOfRange {
        /// 1-based index requested by the transform.
        index: usize,
        /// Number of tokens available to the transform.
        available: usize,
    },
    /// Prefix rewrites can only execute against literal-only patterns.
    #[error("pattern `{0}` must contain only literal segments for evaluation")]
    NonLiteralPattern(String),
    /// Deterministic hashing requires at least one bucket.
    #[error("deterministic bucket count must be greater than zero")]
    ZeroBuckets,
}

fn canonical_capabilities(capabilities: &[FabricCapability]) -> Vec<FabricCapability> {
    capabilities
        .iter()
        .copied()
        .collect::<BTreeSet<_>>()
        .into_iter()
        .collect()
}

fn duplicate_capability(capabilities: &[FabricCapability]) -> Option<FabricCapability> {
    let mut seen = BTreeSet::new();
    for capability in capabilities {
        if !seen.insert(*capability) {
            return Some(*capability);
        }
    }
    None
}

fn literal_only_segments(pattern: &SubjectPattern) -> Result<Vec<String>, MorphismEvaluationError> {
    pattern
        .segments()
        .iter()
        .map(|segment| match segment {
            super::subject::SubjectToken::Literal(value) => Ok(value.clone()),
            super::subject::SubjectToken::One | super::subject::SubjectToken::Tail => Err(
                MorphismEvaluationError::NonLiteralPattern(pattern.canonical_key()),
            ),
        })
        .collect()
}

fn select_token(tokens: &[String], index: usize) -> Result<&str, MorphismEvaluationError> {
    let offset = index
        .checked_sub(1)
        .ok_or(MorphismEvaluationError::TokenIndexOutOfRange {
            index,
            available: tokens.len(),
        })?;
    tokens
        .get(offset)
        .map(String::as_str)
        .ok_or(MorphismEvaluationError::TokenIndexOutOfRange {
            index,
            available: tokens.len(),
        })
}

fn deterministic_bucket(
    tokens: &[String],
    source_indices: &[usize],
    buckets: u16,
) -> Result<u16, MorphismEvaluationError> {
    if buckets == 0 {
        return Err(MorphismEvaluationError::ZeroBuckets);
    }

    let mut hasher = DetHasher::default();
    if source_indices.is_empty() {
        for token in tokens {
            hasher.write(token.as_bytes());
            hasher.write_u8(0xff);
        }
    } else {
        for index in source_indices {
            hasher.write(select_token(tokens, *index)?.as_bytes());
            hasher.write_u8(0xff);
        }
    }

    Ok((hasher.finish() % u64::from(buckets)) as u16)
}

fn take_left(token: &str, len: usize) -> String {
    let limit = token.chars().count().min(len);
    token.chars().take(limit).collect()
}

fn take_right(token: &str, len: usize) -> String {
    let char_count = token.chars().count();
    let start = char_count.saturating_sub(len);
    token.chars().skip(start).collect()
}

#[derive(Debug)]
struct CompiledPlanParts {
    direction: MorphismPlanDirection,
    certificate: MorphismCertificate,
    attached_capabilities: Vec<FabricCapability>,
    selected_reply_space: Option<ReplySpaceRule>,
    permitted_reply_spaces: Vec<ReplySpaceRule>,
    metadata_boundary: MetadataBoundarySummary,
    steps: Vec<MorphismPlanStep>,
    reasoning: Vec<MorphismAuditNote>,
}

fn compile_boundary_plan(
    morphism: &Morphism,
    direction: MorphismPlanDirection,
    requested_reply_space: Option<ReplySpaceRule>,
) -> Result<CompiledPlanParts, MorphismCompileError> {
    let certificate = morphism.compile()?;
    let attached_capabilities = canonical_capabilities(&morphism.capability_requirements);
    let permitted_reply_spaces = permitted_reply_spaces(morphism);
    let selected_reply_space =
        select_reply_space(morphism, requested_reply_space, &permitted_reply_spaces)?;
    let metadata_boundary = compile_metadata_boundary(morphism)?;
    let reasoning = vec![
        audit_note(
            "direction",
            format!(
                "{} plan rewrites `{}` into `{}`",
                direction.as_str(),
                morphism.source_language,
                morphism.dest_language
            ),
        ),
        audit_note(
            "capabilities",
            format!("attached capabilities: {attached_capabilities:?}"),
        ),
        audit_note(
            "reply_space",
            format!(
                "response policy {:?} selected {:?} from permitted spaces {:?}",
                morphism.response_policy, selected_reply_space, permitted_reply_spaces
            ),
        ),
        audit_note(
            "metadata_boundary",
            format!(
                "boundary crosses={}, disclosure={:?}, subject_literals_redacted={}, cross_tenant_flow_allowed={}, payload_hashes_recorded={}",
                metadata_boundary.crosses_boundary,
                metadata_boundary.metadata_disclosure,
                metadata_boundary.subject_literals_redacted,
                metadata_boundary.cross_tenant_flow_allowed,
                metadata_boundary.payload_hashes_recorded,
            ),
        ),
    ];

    Ok(CompiledPlanParts {
        direction,
        certificate,
        attached_capabilities,
        selected_reply_space,
        permitted_reply_spaces,
        metadata_boundary,
        steps: vec![
            MorphismPlanStep::MatchSourceLanguage,
            MorphismPlanStep::EnforceCapabilityEnvelope,
            MorphismPlanStep::ApplyTransformCertificate,
            MorphismPlanStep::EnforceReplySpace,
            MorphismPlanStep::EnforceMetadataBoundary,
            MorphismPlanStep::EmitAuditReasoning,
        ],
        reasoning,
    })
}

fn permitted_reply_spaces(morphism: &Morphism) -> Vec<ReplySpaceRule> {
    match morphism.response_policy {
        ResponsePolicy::StripReplies => Vec::new(),
        ResponsePolicy::PreserveCallerReplies | ResponsePolicy::ForwardOpaque => {
            vec![ReplySpaceRule::CallerInbox]
        }
        ResponsePolicy::ReplyAuthoritative => {
            let prefix = morphism.dest_language.as_str().to_owned();
            vec![
                ReplySpaceRule::DedicatedPrefix {
                    prefix: prefix.clone(),
                },
                ReplySpaceRule::SharedPrefix { prefix },
            ]
        }
    }
}

fn select_reply_space(
    morphism: &Morphism,
    requested_reply_space: Option<ReplySpaceRule>,
    permitted_reply_spaces: &[ReplySpaceRule],
) -> Result<Option<ReplySpaceRule>, MorphismCompileError> {
    let selected_reply_space = requested_reply_space.map_or_else(
        || default_reply_space(morphism, permitted_reply_spaces),
        Some,
    );

    if !morphism.crosses_boundary() {
        return Ok(selected_reply_space);
    }

    selected_reply_space.map_or(Ok(None), |reply_space| {
        if permitted_reply_spaces.contains(&reply_space) {
            Ok(Some(reply_space))
        } else if permitted_reply_spaces.is_empty() {
            Err(MorphismCompileError::ReplySpaceForbidden {
                policy: morphism.response_policy,
            })
        } else {
            Err(MorphismCompileError::ReplySpaceNotPermitted {
                policy: morphism.response_policy,
                requested: reply_space,
                permitted: permitted_reply_spaces.to_vec(),
            })
        }
    })
}

fn default_reply_space(
    morphism: &Morphism,
    permitted_reply_spaces: &[ReplySpaceRule],
) -> Option<ReplySpaceRule> {
    match morphism.response_policy {
        ResponsePolicy::StripReplies => None,
        ResponsePolicy::PreserveCallerReplies | ResponsePolicy::ForwardOpaque => {
            Some(ReplySpaceRule::CallerInbox)
        }
        ResponsePolicy::ReplyAuthoritative => permitted_reply_spaces.first().cloned(),
    }
}

fn compile_metadata_boundary(
    morphism: &Morphism,
) -> Result<MetadataBoundarySummary, MorphismCompileError> {
    let summary = MetadataBoundarySummary {
        crosses_boundary: morphism.crosses_boundary(),
        metadata_disclosure: morphism.privacy_policy.metadata_disclosure,
        subject_literals_redacted: morphism.privacy_policy.redact_subject_literals
            || matches!(morphism.transform, SubjectTransform::RedactLiterals),
        cross_tenant_flow_allowed: morphism.privacy_policy.allow_cross_tenant_flow,
        payload_hashes_recorded: morphism.evidence_policy.record_payload_hashes,
    };

    if summary.crosses_boundary
        && matches!(
            morphism.sharing_policy,
            SharingPolicy::Federated | SharingPolicy::PublicRead
        )
        && summary.metadata_disclosure == MetadataDisclosure::Full
        && !summary.cross_tenant_flow_allowed
    {
        return Err(MorphismCompileError::MetadataBoundaryViolation {
            sharing_policy: morphism.sharing_policy,
            metadata_disclosure: summary.metadata_disclosure,
        });
    }

    Ok(summary)
}

fn detect_semantic_cycle_from(
    morphisms: &[Morphism],
    start: usize,
    current: usize,
    path: &mut Vec<usize>,
    visited: &mut BTreeSet<usize>,
) -> Result<(), MorphismCompileError> {
    if morphisms[current]
        .dest_language
        .overlaps(&morphisms[start].source_language)
    {
        let cycle = path
            .iter()
            .map(|index| &morphisms[*index])
            .collect::<Vec<_>>();
        if let Some(class) = classify_semantic_cycle(&cycle) {
            return Err(MorphismCompileError::SemanticCycleDetected {
                class,
                path: path
                    .iter()
                    .map(|index| describe_morphism(&morphisms[*index]))
                    .collect(),
            });
        }
    }

    for next in 0..morphisms.len() {
        if !morphisms[current]
            .dest_language
            .overlaps(&morphisms[next].source_language)
        {
            continue;
        }
        if visited.contains(&next) {
            continue;
        }

        visited.insert(next);
        path.push(next);
        let result = detect_semantic_cycle_from(morphisms, start, next, path, visited);
        path.pop();
        visited.remove(&next);
        result?;
    }

    Ok(())
}

fn classify_semantic_cycle(morphisms: &[&Morphism]) -> Option<SemanticCycleClass> {
    if morphisms.iter().any(|morphism| {
        morphism.class == MorphismClass::Authoritative
            || morphism.response_policy == ResponsePolicy::ReplyAuthoritative
            || morphism
                .capability_requirements
                .iter()
                .copied()
                .any(FabricCapability::is_authority_bearing)
    }) {
        return Some(SemanticCycleClass::Authority);
    }

    if morphisms.iter().any(|morphism| {
        morphism.reversibility == ReversibilityRequirement::Irreversible
            || morphism.transform.is_lossy()
    }) {
        return Some(SemanticCycleClass::Reversibility);
    }

    if morphisms.iter().any(|morphism| {
        morphism.sharing_policy != SharingPolicy::Private
            || morphism.capability_requirements.iter().any(|capability| {
                matches!(
                    capability,
                    FabricCapability::ObserveEvidence | FabricCapability::CrossBoundaryEgress
                )
            })
    }) {
        return Some(SemanticCycleClass::Capability);
    }

    None
}

fn describe_morphism(morphism: &Morphism) -> String {
    format!(
        "{:?}:{}->{}",
        morphism.class, morphism.source_language, morphism.dest_language
    )
}

fn audit_note(code: &str, detail: String) -> MorphismAuditNote {
    MorphismAuditNote {
        code: code.to_owned(),
        detail,
    }
}

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

    fn authoritative_morphism() -> Morphism {
        Morphism {
            source_language: SubjectPattern::new("tenant.orders"),
            dest_language: SubjectPattern::new("authority.orders"),
            class: MorphismClass::Authoritative,
            transform: SubjectTransform::RenamePrefix {
                from: SubjectPattern::new("tenant.orders"),
                to: SubjectPattern::new("authority.orders"),
            },
            reversibility: ReversibilityRequirement::Bijective,
            capability_requirements: vec![
                FabricCapability::CarryAuthority,
                FabricCapability::ReplyAuthority,
            ],
            response_policy: ResponsePolicy::ReplyAuthoritative,
            ..Morphism::default()
        }
    }

    #[test]
    fn authoritative_compile_produces_deterministic_certificate() {
        let morphism = authoritative_morphism();
        let first = morphism.compile().expect("compile certificate");
        let second = morphism.compile().expect("compile certificate twice");

        assert_eq!(first, second);
        assert_eq!(first.class, MorphismClass::Authoritative);
        assert_eq!(
            first.facets.authority.capability_requirements,
            vec![
                FabricCapability::CarryAuthority,
                FabricCapability::ReplyAuthority,
            ]
        );
    }

    #[test]
    fn authoritative_morphisms_reject_lossy_or_wildcard_rewrites() {
        let mut lossy = authoritative_morphism();
        lossy.reversibility = ReversibilityRequirement::EvidenceBacked;
        lossy.transform = SubjectTransform::RedactLiterals;
        assert_eq!(
            lossy.validate(),
            Err(MorphismValidationError::AuthoritativeTransformMustBeLossless)
        );

        let mut wildcard = authoritative_morphism();
        wildcard.transform = SubjectTransform::RenamePrefix {
            from: SubjectPattern::new("tenant.*"),
            to: SubjectPattern::new("authority.orders"),
        };
        assert_eq!(
            wildcard.validate(),
            Err(MorphismValidationError::AuthoritativeRenameMustBeLiteralOnly)
        );
    }

    #[test]
    fn delegation_requires_delegate_capability_bounded_duration_and_revocation() {
        let mut delegation = Morphism {
            class: MorphismClass::Delegation,
            response_policy: ResponsePolicy::ForwardOpaque,
            ..Morphism::default()
        };

        assert_eq!(
            delegation.validate(),
            Err(MorphismValidationError::DelegationRequiresDelegateCapability)
        );

        delegation.capability_requirements = vec![FabricCapability::DelegateNamespace];
        assert_eq!(
            delegation.validate(),
            Err(MorphismValidationError::DelegationMustBeTimeBounded)
        );

        delegation.quota_policy.max_handoff_duration = Some(Duration::from_secs(30));
        assert_eq!(
            delegation.validate(),
            Err(MorphismValidationError::DelegationMustBeRevocable)
        );

        delegation.quota_policy.revocation_required = true;
        assert!(delegation.validate().is_ok());
    }

    #[test]
    fn delegation_rejects_irreversible_and_lossy_transforms() {
        let mut delegation = Morphism {
            source_language: SubjectPattern::new("tenant.rpc"),
            dest_language: SubjectPattern::new("delegate.rpc"),
            class: MorphismClass::Delegation,
            capability_requirements: vec![FabricCapability::DelegateNamespace],
            response_policy: ResponsePolicy::ForwardOpaque,
            sharing_policy: SharingPolicy::TenantScoped,
            ..Morphism::default()
        };
        delegation.quota_policy.max_handoff_duration = Some(Duration::from_secs(30));
        delegation.quota_policy.revocation_required = true;

        delegation.reversibility = ReversibilityRequirement::Irreversible;
        assert_eq!(
            delegation.validate(),
            Err(MorphismValidationError::DelegationMustBeReversible)
        );

        delegation.reversibility = ReversibilityRequirement::EvidenceBacked;
        delegation.transform = SubjectTransform::RedactLiterals;
        assert_eq!(
            delegation.validate(),
            Err(MorphismValidationError::DelegationTransformMustBeLossless)
        );
    }

    #[test]
    fn egress_requires_stripped_replies_and_one_way_reversibility() {
        let mut egress = Morphism {
            class: MorphismClass::Egress,
            sharing_policy: SharingPolicy::Federated,
            reversibility: ReversibilityRequirement::Irreversible,
            ..Morphism::default()
        };

        assert_eq!(
            egress.validate(),
            Err(MorphismValidationError::EgressMustStripReplies)
        );

        egress.response_policy = ResponsePolicy::StripReplies;
        egress.reversibility = ReversibilityRequirement::EvidenceBacked;
        assert_eq!(
            egress.validate(),
            Err(MorphismValidationError::EgressMustBeIrreversible)
        );

        egress.reversibility = ReversibilityRequirement::Irreversible;
        egress.sharing_policy = SharingPolicy::Private;
        assert_eq!(
            egress.validate(),
            Err(MorphismValidationError::EgressMustCrossBoundary)
        );
    }

    #[test]
    fn facet_views_change_independently() {
        let base = Morphism::default();

        let mut cost_variant = base.clone();
        cost_variant.quota_policy.max_fanout = 8;
        assert_eq!(base.authority_facet(), cost_variant.authority_facet());
        assert_eq!(
            base.reversibility_facet(),
            cost_variant.reversibility_facet()
        );
        assert_eq!(base.secrecy_facet(), cost_variant.secrecy_facet());
        assert_ne!(base.cost_facet(), cost_variant.cost_facet());
        assert_eq!(
            base.observability_facet(),
            cost_variant.observability_facet()
        );

        let mut observability_variant = base.clone();
        observability_variant
            .evidence_policy
            .record_counterfactual_branches = true;
        assert_eq!(
            base.authority_facet(),
            observability_variant.authority_facet()
        );
        assert_eq!(base.cost_facet(), observability_variant.cost_facet());
        assert_ne!(
            base.observability_facet(),
            observability_variant.observability_facet()
        );
    }

    #[test]
    fn duplicate_capabilities_fail_closed() {
        let mut morphism = authoritative_morphism();
        morphism.capability_requirements = vec![
            FabricCapability::ReplyAuthority,
            FabricCapability::CarryAuthority,
            FabricCapability::ReplyAuthority,
        ];

        assert_eq!(
            morphism.validate(),
            Err(MorphismValidationError::DuplicateCapability(
                FabricCapability::ReplyAuthority
            ))
        );
    }

    #[test]
    fn derived_views_cannot_smuggle_authority_or_reply_rebinding() {
        let mut derived_view = Morphism::default();
        derived_view.capability_requirements = vec![FabricCapability::CarryAuthority];
        assert_eq!(
            derived_view.validate(),
            Err(MorphismValidationError::DerivedViewCannotRequireAuthorityCapability)
        );

        derived_view.capability_requirements = vec![FabricCapability::RewriteNamespace];
        derived_view.response_policy = ResponsePolicy::ReplyAuthoritative;
        assert_eq!(
            derived_view.validate(),
            Err(MorphismValidationError::DerivedViewCannotOriginateReplyAuthority)
        );
    }

    #[test]
    fn wildcard_capture_and_compose_apply_deterministically() {
        let tokens = vec![
            String::from("tenant"),
            String::from("orders-eu"),
            String::from("priority"),
        ];
        let transform = SubjectTransform::Compose {
            steps: vec![
                SubjectTransform::WildcardCapture { index: 2 },
                SubjectTransform::SplitSlice {
                    index: 1,
                    delimiter: String::from("-"),
                    start: 0,
                    len: 1,
                },
            ],
        };

        assert_eq!(
            transform
                .apply_tokens(&tokens)
                .expect("compose should evaluate"),
            vec![String::from("orders")]
        );
        assert!(!transform.is_invertible());
    }

    #[test]
    fn rename_prefix_and_remaining_lossy_variants_cover_expected_behavior() {
        let tokens = vec![
            String::from("tenant"),
            String::from("orders"),
            String::from("priority"),
        ];
        let rename = SubjectTransform::RenamePrefix {
            from: SubjectPattern::new("tenant.orders"),
            to: SubjectPattern::new("authority.orders"),
        };
        let rewritten = rename
            .apply_tokens(&tokens)
            .expect("rename-prefix should rewrite matching literal prefixes");
        assert_eq!(
            rewritten,
            vec![
                String::from("authority"),
                String::from("orders"),
                String::from("priority"),
            ]
        );
        assert_eq!(
            rename
                .inverse()
                .expect("literal rename should be invertible")
                .apply_tokens(&rewritten)
                .expect("inverse rename should roundtrip"),
            tokens
        );

        let redacted = SubjectTransform::RedactLiterals;
        assert_eq!(
            redacted
                .apply_tokens(&rewritten)
                .expect("redaction should preserve token count"),
            vec![String::from("_"), String::from("_"), String::from("_"),]
        );
        assert!(redacted.is_lossy());

        let summarized = SubjectTransform::SummarizeTail {
            preserve_segments: 2,
        };
        assert_eq!(
            summarized
                .apply_tokens(&rewritten)
                .expect("tail summary should preserve requested prefix"),
            vec![
                String::from("authority"),
                String::from("orders"),
                String::from("..."),
            ]
        );
        assert!(summarized.is_lossy());

        let partition = SubjectTransform::HashPartition { buckets: 8 };
        let first = partition
            .apply_tokens(&rewritten)
            .expect("hash partition should evaluate deterministically");
        let second = partition
            .apply_tokens(&rewritten)
            .expect("hash partition should remain stable");
        assert_eq!(first, second);
        let bucket = first[0]
            .parse::<u16>()
            .expect("hash partition must emit a bucket number");
        assert!(bucket < 8);
    }

    #[test]
    fn deterministic_hash_is_stable_for_selected_tokens() {
        let tokens = vec![
            String::from("tenant"),
            String::from("region"),
            String::from("user"),
        ];
        let transform = SubjectTransform::DeterministicHash {
            buckets: 32,
            source_indices: vec![1, 3],
        };

        let first = transform
            .apply_tokens(&tokens)
            .expect("hash should evaluate deterministically");
        let second = transform
            .apply_tokens(&tokens)
            .expect("hash should evaluate deterministically twice");
        assert_eq!(first, second);
    }

    #[test]
    fn transform_validation_rejects_invalid_core_parameters() {
        assert_eq!(
            SubjectTransform::SummarizeTail {
                preserve_segments: 0,
            }
            .validate(),
            Err(MorphismValidationError::SummarizeTailMustPreserveSegments)
        );
        assert_eq!(
            SubjectTransform::HashPartition { buckets: 0 }.validate(),
            Err(MorphismValidationError::HashPartitionRequiresBuckets)
        );
        assert_eq!(
            SubjectTransform::Compose { steps: Vec::new() }.validate(),
            Err(MorphismValidationError::ComposeRequiresSteps)
        );
    }

    #[test]
    fn left_and_right_extract_project_expected_substrings() {
        let tokens = vec![String::from("priority")];
        assert_eq!(
            SubjectTransform::LeftExtract { index: 1, len: 4 }
                .apply_tokens(&tokens)
                .expect("left extract should evaluate"),
            vec![String::from("prio")]
        );
        assert_eq!(
            SubjectTransform::RightExtract { index: 1, len: 4 }
                .apply_tokens(&tokens)
                .expect("right extract should evaluate"),
            vec![String::from("rity")]
        );
    }

    #[test]
    fn bijective_reversibility_rejects_irreversible_transforms() {
        let mut morphism = authoritative_morphism();
        morphism.transform = SubjectTransform::DeterministicHash {
            buckets: 16,
            source_indices: vec![1],
        };
        assert_eq!(
            morphism.validate(),
            Err(MorphismValidationError::TransformCannotSatisfyBijectiveRequirement)
        );
    }

    #[test]
    fn reversible_compose_builds_inverse_in_reverse_order() {
        let transform = SubjectTransform::Compose {
            steps: vec![
                SubjectTransform::RenamePrefix {
                    from: SubjectPattern::new("tenant.orders"),
                    to: SubjectPattern::new("authority.orders"),
                },
                SubjectTransform::Identity,
            ],
        };

        let inverse = transform.inverse().expect("compose should be invertible");
        assert_eq!(
            inverse,
            SubjectTransform::Compose {
                steps: vec![
                    SubjectTransform::Identity,
                    SubjectTransform::RenamePrefix {
                        from: SubjectPattern::new("authority.orders"),
                        to: SubjectPattern::new("tenant.orders"),
                    },
                ],
            }
        );
    }

    #[test]
    fn export_plan_compilation_attaches_certificate_capabilities_and_reply_space() {
        let mut morphism = authoritative_morphism();
        morphism.sharing_policy = SharingPolicy::Federated;
        morphism.privacy_policy.allow_cross_tenant_flow = true;

        let plan = morphism
            .compile_export_plan(None)
            .expect("export plan should compile");

        assert_eq!(plan.direction, MorphismPlanDirection::Export);
        assert_eq!(
            plan.attached_capabilities,
            vec![
                FabricCapability::CarryAuthority,
                FabricCapability::ReplyAuthority,
            ]
        );
        assert_eq!(
            plan.selected_reply_space,
            Some(ReplySpaceRule::DedicatedPrefix {
                prefix: String::from("authority.orders"),
            })
        );
        assert!(
            plan.permitted_reply_spaces
                .contains(&ReplySpaceRule::SharedPrefix {
                    prefix: String::from("authority.orders"),
                })
        );
        assert!(plan.reasoning.iter().any(|note| note.code == "reply_space"));
    }

    #[test]
    fn import_plan_compilation_defaults_forward_opaque_to_caller_inbox() {
        let mut delegation = Morphism {
            source_language: SubjectPattern::new("tenant.rpc"),
            dest_language: SubjectPattern::new("delegate.rpc"),
            class: MorphismClass::Delegation,
            capability_requirements: vec![FabricCapability::DelegateNamespace],
            response_policy: ResponsePolicy::ForwardOpaque,
            sharing_policy: SharingPolicy::TenantScoped,
            ..Morphism::default()
        };
        delegation.quota_policy.max_handoff_duration = Some(Duration::from_secs(30));
        delegation.quota_policy.revocation_required = true;
        delegation.privacy_policy.allow_cross_tenant_flow = true;

        let plan = delegation
            .compile_import_plan(None)
            .expect("import plan should compile");

        assert_eq!(plan.direction, MorphismPlanDirection::Import);
        assert_eq!(plan.selected_reply_space, Some(ReplySpaceRule::CallerInbox));
        assert_eq!(
            plan.permitted_reply_spaces,
            vec![ReplySpaceRule::CallerInbox]
        );
    }

    #[test]
    fn cross_boundary_reply_space_enforcement_rejects_unpermitted_reply_prefixes() {
        let mut morphism = Morphism {
            source_language: SubjectPattern::new("tenant.requests"),
            dest_language: SubjectPattern::new("federated.requests"),
            sharing_policy: SharingPolicy::Federated,
            ..Morphism::default()
        };
        morphism.privacy_policy.allow_cross_tenant_flow = true;

        let err = morphism
            .compile_export_plan(Some(ReplySpaceRule::DedicatedPrefix {
                prefix: String::from("reply.bad"),
            }))
            .expect_err("non-caller reply prefix should be rejected");

        assert_eq!(
            err,
            MorphismCompileError::ReplySpaceNotPermitted {
                policy: ResponsePolicy::PreserveCallerReplies,
                requested: ReplySpaceRule::DedicatedPrefix {
                    prefix: String::from("reply.bad"),
                },
                permitted: vec![ReplySpaceRule::CallerInbox],
            }
        );
    }

    #[test]
    fn cross_boundary_strip_replies_forbids_any_selected_reply_space() {
        let mut egress = Morphism {
            source_language: SubjectPattern::new("tenant.audit"),
            dest_language: SubjectPattern::new("federated.audit"),
            class: MorphismClass::Egress,
            transform: SubjectTransform::RedactLiterals,
            reversibility: ReversibilityRequirement::Irreversible,
            sharing_policy: SharingPolicy::Federated,
            response_policy: ResponsePolicy::StripReplies,
            ..Morphism::default()
        };
        egress.privacy_policy.allow_cross_tenant_flow = true;

        let err = egress
            .compile_export_plan(Some(ReplySpaceRule::CallerInbox))
            .expect_err("strip-replies egress should reject every reply space");

        assert_eq!(
            err,
            MorphismCompileError::ReplySpaceForbidden {
                policy: ResponsePolicy::StripReplies,
            }
        );
    }

    #[test]
    fn metadata_boundary_checks_fail_closed_for_public_full_disclosure() {
        let mut morphism = Morphism {
            source_language: SubjectPattern::new("tenant.audit"),
            dest_language: SubjectPattern::new("public.audit"),
            sharing_policy: SharingPolicy::PublicRead,
            ..Morphism::default()
        };
        morphism.privacy_policy.metadata_disclosure = MetadataDisclosure::Full;

        let err = morphism
            .compile_export_plan(None)
            .expect_err("public full disclosure should require explicit cross-tenant permission");

        assert_eq!(
            err,
            MorphismCompileError::MetadataBoundaryViolation {
                sharing_policy: SharingPolicy::PublicRead,
                metadata_disclosure: MetadataDisclosure::Full,
            }
        );
    }

    #[test]
    fn metadata_boundary_summary_records_redaction_and_payload_hash_evidence() {
        let mut morphism = Morphism {
            source_language: SubjectPattern::new("tenant.audit"),
            dest_language: SubjectPattern::new("federated.audit"),
            transform: SubjectTransform::RedactLiterals,
            sharing_policy: SharingPolicy::Federated,
            ..Morphism::default()
        };
        morphism.privacy_policy.allow_cross_tenant_flow = true;
        morphism.evidence_policy.record_payload_hashes = true;

        let plan = morphism
            .compile_export_plan(None)
            .expect("cross-boundary plan should compile");

        assert_eq!(
            plan.metadata_boundary,
            MetadataBoundarySummary {
                crosses_boundary: true,
                metadata_disclosure: MetadataDisclosure::Hashed,
                subject_literals_redacted: true,
                cross_tenant_flow_allowed: true,
                payload_hashes_recorded: true,
            }
        );
        assert!(plan.reasoning.iter().any(|note| {
            note.code == "metadata_boundary"
                && note.detail.contains("subject_literals_redacted=true")
                && note.detail.contains("payload_hashes_recorded=true")
        }));
    }

    #[test]
    fn semantic_cycle_detection_flags_authority_cycles() {
        let forward = authoritative_morphism();
        let reverse = Morphism {
            source_language: SubjectPattern::new("authority.orders"),
            dest_language: SubjectPattern::new("tenant.orders"),
            class: MorphismClass::Authoritative,
            transform: SubjectTransform::RenamePrefix {
                from: SubjectPattern::new("authority.orders"),
                to: SubjectPattern::new("tenant.orders"),
            },
            reversibility: ReversibilityRequirement::Bijective,
            capability_requirements: vec![
                FabricCapability::CarryAuthority,
                FabricCapability::ReplyAuthority,
            ],
            response_policy: ResponsePolicy::ReplyAuthoritative,
            ..Morphism::default()
        };

        let err = detect_semantic_cycles(&[forward, reverse])
            .expect_err("authority-bearing cycle should be rejected");

        assert!(matches!(
            err,
            MorphismCompileError::SemanticCycleDetected {
                class: SemanticCycleClass::Authority,
                ..
            }
        ));
    }

    #[test]
    fn semantic_cycle_detection_flags_irreversible_cycles() {
        let irreversible = Morphism {
            source_language: SubjectPattern::new("tenant.audit"),
            dest_language: SubjectPattern::new("egress.audit"),
            class: MorphismClass::Egress,
            transform: SubjectTransform::RedactLiterals,
            reversibility: ReversibilityRequirement::Irreversible,
            sharing_policy: SharingPolicy::Federated,
            response_policy: ResponsePolicy::StripReplies,
            ..Morphism::default()
        };
        let reverse = Morphism {
            source_language: SubjectPattern::new("egress.audit"),
            dest_language: SubjectPattern::new("tenant.audit"),
            ..Morphism::default()
        };

        let err = detect_semantic_cycles(&[irreversible, reverse])
            .expect_err("irreversible cycle should be rejected");

        assert!(matches!(
            err,
            MorphismCompileError::SemanticCycleDetected {
                class: SemanticCycleClass::Reversibility,
                ..
            }
        ));
    }

    #[test]
    fn semantic_cycle_detection_flags_capability_cycles() {
        let mut boundary = Morphism {
            source_language: SubjectPattern::new("tenant.stream"),
            dest_language: SubjectPattern::new("federated.stream"),
            capability_requirements: vec![
                FabricCapability::RewriteNamespace,
                FabricCapability::CrossBoundaryEgress,
            ],
            sharing_policy: SharingPolicy::Federated,
            ..Morphism::default()
        };
        boundary.privacy_policy.allow_cross_tenant_flow = true;
        let reverse = Morphism {
            source_language: SubjectPattern::new("federated.stream"),
            dest_language: SubjectPattern::new("tenant.stream"),
            ..Morphism::default()
        };

        let err = detect_semantic_cycles(&[boundary, reverse])
            .expect_err("boundary capability cycle should be rejected");

        assert!(matches!(
            err,
            MorphismCompileError::SemanticCycleDetected {
                class: SemanticCycleClass::Capability,
                ..
            }
        ));
    }

    #[test]
    fn semantic_cycle_detection_flags_multi_hop_boundary_cycles() {
        let mut first = Morphism {
            source_language: SubjectPattern::new("tenant.stream"),
            dest_language: SubjectPattern::new("federated.stream"),
            capability_requirements: vec![
                FabricCapability::RewriteNamespace,
                FabricCapability::CrossBoundaryEgress,
            ],
            sharing_policy: SharingPolicy::Federated,
            ..Morphism::default()
        };
        first.privacy_policy.allow_cross_tenant_flow = true;

        let mut second = Morphism {
            source_language: SubjectPattern::new("federated.stream"),
            dest_language: SubjectPattern::new("shared.stream"),
            capability_requirements: vec![FabricCapability::ObserveEvidence],
            sharing_policy: SharingPolicy::Federated,
            ..Morphism::default()
        };
        second.privacy_policy.allow_cross_tenant_flow = true;

        let third = Morphism {
            source_language: SubjectPattern::new("shared.stream"),
            dest_language: SubjectPattern::new("tenant.stream"),
            ..Morphism::default()
        };

        let err = detect_semantic_cycles(&[first, second, third])
            .expect_err("multi-hop boundary cycle should be rejected");

        assert!(matches!(
            err,
            MorphismCompileError::SemanticCycleDetected {
                class: SemanticCycleClass::Capability,
                ref path,
            } if path.len() == 3
        ));
    }

    #[test]
    fn semantic_cycle_detection_ignores_safe_private_identity_overlap() {
        let morphism = Morphism {
            source_language: SubjectPattern::new("tenant.local"),
            dest_language: SubjectPattern::new("tenant.local"),
            ..Morphism::default()
        };

        detect_semantic_cycles(&[morphism]).expect("safe private overlap should be accepted");
    }
}