crtx-core 0.1.1

Core IDs, errors, and schema constants for Cortex.
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
//! Typed pai-axiom <-> Cortex trust exchange envelopes (ADR 0042 / 0043).
//!
//! These structs are the receiver-side shape for the three pai-axiom
//! schemas that drive Cortex's field-level admission gate:
//!
//! - `cortex_context_trust` (Cortex -> pai-axiom, mirrored back through the
//!   boundary for consumption checks).
//! - `axiom_execution_trust` (pai-axiom -> Cortex execution receipt).
//! - `authority_feedback_loop` (cross-system loop record).
//!
//! The module is shape-only: it deserializes the envelope, runs per-field
//! validators that emit *stable invariant names*, and produces a typed
//! report. It does not persist state, does not grant authority, and does
//! not run the admission gate itself.
//!
//! Every struct uses `#[serde(deny_unknown_fields)]` so that schema drift
//! at the producer side fails closed at the parse boundary. Stable
//! invariant names follow `<schema_name>.<field_path>.<failure_class>`.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Stable schema string for [`CortexContextTrust`] envelopes.
pub const CORTEX_CONTEXT_TRUST_SCHEMA: &str = "cortex_context_trust";

/// Stable schema string for [`AxiomExecutionTrust`] envelopes.
pub const AXIOM_EXECUTION_TRUST_SCHEMA: &str = "axiom_execution_trust";

/// Stable schema string for [`AuthorityFeedbackLoop`] envelopes.
pub const AUTHORITY_FEEDBACK_LOOP_SCHEMA: &str = "authority_feedback_loop";

/// Stable schema version supported on the Cortex receiver side.
pub const TRUST_EXCHANGE_SCHEMA_VERSION: u16 = 1;

// ---------------------------------------------------------------------------
// Cortex context trust envelope
// ---------------------------------------------------------------------------

/// Cortex context trust envelope (`CORTEX_CONTEXT_TRUST.schema.json` v1).
///
/// `compatibility_trust_label` is intentionally kept on the struct but
/// classified as display-only by [`Self::validate`]: per
/// `CORTEX_AXIOM_TRUST_EXCHANGE_COMPATIBILITY.json` it MUST NOT satisfy
/// any behavior-changing gate. Cortex consumes the decomposed fields.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CortexContextTrust {
    /// Schema discriminator. Must equal [`CORTEX_CONTEXT_TRUST_SCHEMA`].
    pub schema: String,
    /// Schema version. Must equal [`TRUST_EXCHANGE_SCHEMA_VERSION`].
    pub version: u16,
    /// Stable receiver-side reference id (optional in pai-axiom's wire,
    /// supplied by the boundary tool when present).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cortex_context_trust_ref: Option<String>,
    /// Stable context identifier.
    pub context_id: String,
    /// Coarse compatibility label kept for back-compat — display only.
    pub compatibility_trust_label: CompatibilityTrustLabel,
    /// Proof closure state and failing edges.
    pub proof_state: ContextProofState,
    /// Truth ceiling permitted to the receiving consumer.
    pub truth_ceiling: TruthCeiling,
    /// Semantic trust decomposition.
    pub semantic_trust: ContextSemanticTrust,
    /// Provenance references (optional in some fixtures).
    #[serde(default)]
    pub provenance_refs: Vec<String>,
    /// Contradiction posture for the context.
    #[serde(default = "ContradictionState::default_unknown")]
    pub contradiction_state: ContradictionState,
    /// Promotion posture for the context.
    #[serde(default = "PromotionState::default_candidate")]
    pub promotion_state: PromotionState,
    /// Quarantine posture for the context.
    pub quarantine_state: ContextQuarantineState,
    /// Redaction posture and references.
    pub redaction_state: ContextRedactionState,
    /// Confidence value and scale (optional in some fixtures).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub confidence: Option<ContextConfidence>,
    /// Policy decision record.
    pub policy_result: ContextPolicyResult,
    /// Allowed claim language vocabulary.
    #[serde(default)]
    pub allowed_claim_language: Vec<ContextAllowedClaimLanguage>,
    /// Forbidden authority-bearing uses.
    #[serde(default)]
    pub forbidden_uses: Vec<ContextForbiddenUse>,
    /// Allowed use vocabulary.
    #[serde(default)]
    pub allowed_use: Vec<ContextAllowedUse>,
    /// Evidence references backing the context.
    #[serde(default)]
    pub evidence_refs: Vec<String>,
    /// Source anchors backing the context.
    pub source_anchors: Vec<ContextSourceAnchor>,
    /// Residual risk strings copied from the producer.
    #[serde(default)]
    pub residual_risk: Vec<String>,
}

/// Coarse compatibility label vocabulary (display only).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CompatibilityTrustLabel {
    /// Compatibility alias only.
    Untrusted,
    /// Compatibility alias only.
    Advisory,
    /// Compatibility alias only.
    Observed,
    /// Compatibility alias only.
    Validated,
    /// Compatibility alias only.
    AuthorityClaimed,
}

/// Proof closure state inside a Cortex context envelope.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextProofState {
    /// Proof closure state value.
    pub state: ContextProofStateValue,
    /// Failing edges names. Must be present (even if empty).
    pub failing_edges: Vec<String>,
    /// Proof references for the closure.
    pub proof_refs: Vec<String>,
}

/// Proof closure state vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextProofStateValue {
    /// No proof was supplied.
    Missing,
    /// Proof is partial.
    Partial,
    /// Proof is closed.
    Closed,
    /// Proof failed.
    Failed,
}

/// Truth ceiling vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum TruthCeiling {
    /// No truth claim permitted.
    None,
    /// Advisory only.
    Advisory,
    /// Observed only.
    Observed,
    /// Validated.
    Validated,
    /// Already promoted by Cortex (display only at the receiver).
    PromotedByCortex,
}

impl Serialize for TruthCeiling {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(self.wire_string())
    }
}

impl TruthCeiling {
    /// Stable wire string mirroring the JSON Schema enum.
    #[must_use]
    pub const fn wire_string(self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Advisory => "advisory",
            Self::Observed => "observed",
            Self::Validated => "validated",
            Self::PromotedByCortex => "promoted-by-cortex",
        }
    }
}

/// Decomposed semantic trust block.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextSemanticTrust {
    /// Provenance class for the context.
    pub provenance_class: ContextProvenanceClass,
    /// Trust weight in [0, 1].
    pub trust_weight: f64,
    /// Free-form weighting basis explanation.
    pub weighting_basis: String,
}

/// Provenance class vocabulary inside context trust.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextProvenanceClass {
    /// Unknown provenance.
    Unknown,
    /// Claimed but unverified.
    Claimed,
    /// Observed.
    Observed,
    /// Derived from named premises.
    Derived,
    /// Curated.
    Curated,
    /// Already promoted (display only).
    Promoted,
}

/// Contradiction state vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContradictionState {
    /// No contradiction known.
    NoneKnown,
    /// Contradictions remain unresolved.
    Unresolved,
    /// Contradictions were resolved.
    Resolved,
    /// Contradiction posture unknown.
    Unknown,
}

impl ContradictionState {
    fn default_unknown() -> Self {
        Self::Unknown
    }
}

/// Promotion state vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PromotionState {
    /// Candidate only.
    Candidate,
    /// Observed.
    Observed,
    /// Validated.
    Validated,
    /// Already promoted (display only).
    Promoted,
    /// Stale.
    Stale,
    /// Quarantined.
    Quarantined,
}

impl PromotionState {
    fn default_candidate() -> Self {
        Self::Candidate
    }
}

/// Quarantine state vocabulary for context envelopes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextQuarantineState {
    /// Clear of quarantine.
    Clear,
    /// Quarantined.
    Quarantined,
    /// Derived from a quarantined ancestor.
    DerivedFromQuarantined,
    /// Posture unknown.
    Unknown,
}

impl ContextQuarantineState {
    /// Whether this state propagates quarantine into Cortex.
    #[must_use]
    pub const fn propagates_quarantine(self) -> bool {
        matches!(
            self,
            Self::Quarantined | Self::DerivedFromQuarantined | Self::Unknown
        )
    }
}

/// Redaction state block on a context envelope.
///
/// The `blocks_critical_premise` field is intentionally accepted because
/// some pai-axiom fixtures emit it as a redaction-policy extension. It is
/// not authoritative on the Cortex receiver — Cortex consumes
/// [`Self::status`] and [`Self::redaction_refs`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextRedactionState {
    /// Redaction status value.
    pub status: ContextRedactionStatus,
    /// Redaction references.
    pub redaction_refs: Vec<String>,
    /// Optional extension marker (some pai-axiom fixtures emit this).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub blocks_critical_premise: Option<bool>,
}

/// Redaction status vocabulary on context envelopes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextRedactionStatus {
    /// No redaction.
    None,
    /// Redacted.
    Redacted,
    /// Partially redacted.
    PartiallyRedacted,
    /// Redaction posture unknown.
    Unknown,
}

/// Confidence block on context envelopes.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextConfidence {
    /// Confidence value (numeric or discrete string).
    pub value: ContextConfidenceValue,
    /// Confidence scale.
    pub scale: ContextConfidenceScale,
}

/// Confidence value — either numeric (0..=1) or a discrete word.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ContextConfidenceValue {
    /// Numeric confidence in [0, 1].
    Numeric(f64),
    /// Discrete label such as "low", "medium", "high".
    Discrete(String),
}

/// Confidence scale vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextConfidenceScale {
    /// Discrete label set.
    Discrete,
    /// Numeric scale.
    Numeric,
}

/// Policy result block on a context envelope.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextPolicyResult {
    /// Stable decision identifier.
    pub decision_id: String,
    /// Policy result value.
    pub result: ContextPolicyResultValue,
}

/// Policy result vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextPolicyResultValue {
    /// Policy denied.
    Deny,
    /// Policy requires review.
    ReviewRequired,
    /// Policy partial.
    Partial,
    /// Policy allowed.
    Allow,
}

/// Allowed claim language vocabulary on a context envelope.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextAllowedClaimLanguage {
    /// Candidate claims permitted.
    Candidate,
    /// Observed claims permitted.
    Observed,
    /// Validated claims permitted.
    Validated,
}

/// Forbidden authority-bearing uses on a context envelope.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextForbiddenUse {
    /// Execution authority forbidden.
    ExecutionPermission,
    /// Durable truth promotion forbidden.
    DurableTruthPromotion,
    /// Release claim forbidden.
    ReleaseClaim,
}

/// Allowed use vocabulary on a context envelope.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextAllowedUse {
    /// Render only.
    RenderOnly,
    /// Advisory reasoning.
    AdvisoryReasoning,
    /// Planning input.
    PlanningInput,
}

/// Source anchor on a context envelope.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ContextSourceAnchor {
    /// Stable source identifier.
    pub source_id: String,
    /// Source type vocabulary.
    pub source_type: ContextSourceAnchorType,
    /// Stable source reference.
    pub r#ref: String,
    /// `sha256:<hex>` hash.
    pub hash: String,
}

/// Source anchor type vocabulary on context envelopes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContextSourceAnchorType {
    /// Cortex event.
    Event,
    /// Memory.
    Memory,
    /// Principle.
    Principle,
    /// Doctrine.
    Doctrine,
    /// Ledger entry.
    Ledger,
    /// Context pack.
    ContextPack,
    /// External anchor.
    External,
}

// ---------------------------------------------------------------------------
// pai-axiom execution trust envelope
// ---------------------------------------------------------------------------

/// pai-axiom execution trust envelope (`AXIOM_EXECUTION_TRUST.schema.json` v1).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AxiomExecutionTrust {
    /// Schema discriminator. Must equal [`AXIOM_EXECUTION_TRUST_SCHEMA`].
    pub schema: String,
    /// Schema version. Must equal [`TRUST_EXCHANGE_SCHEMA_VERSION`].
    pub version: u16,
    /// Optional stable reference (not in the raw schema, supplied by some
    /// boundary tools).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub axiom_execution_trust_ref: Option<String>,
    /// Stable action identifier.
    pub action_id: String,
    /// Execution trust level.
    pub execution_trust_level: ExecutionTrustLevel,
    /// Repo trust block.
    pub repo_trust: RepoTrust,
    /// Actor attestation block.
    pub actor_attestation: ActorAttestation,
    /// Policy decision block.
    pub policy_decision: ExecutionPolicyDecision,
    /// Token scope block.
    pub token_scope: TokenScope,
    /// Tool provenance block.
    pub tool_provenance: ExecutionToolProvenance,
    /// Source anchors backing the execution receipt.
    pub source_anchors: Vec<ExecutionSourceAnchor>,
    /// Runtime mode label.
    pub runtime_mode: String,
    /// Evidence references.
    #[serde(default)]
    pub evidence_refs: Vec<String>,
    /// Residual risk strings.
    #[serde(default)]
    pub residual_risk: Vec<String>,
}

/// Execution trust level vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ExecutionTrustLevel {
    /// Developer-only.
    Dev,
    /// Locally unsigned.
    LocalUnsigned,
    /// Signed locally.
    SignedLocal,
    /// Anchored externally.
    ExternallyAnchored,
    /// Authority-grade.
    AuthorityGrade,
}

/// Repo trust block.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RepoTrust {
    /// Repo trust result.
    pub result: RepoTrustResult,
    /// Stable evaluation reference.
    pub evaluation_ref: String,
}

/// Repo trust result vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RepoTrustResult {
    /// Repo trusted.
    Trusted,
    /// Repo trust partial.
    Partial,
    /// Repo untrusted.
    Untrusted,
}

/// Actor attestation block.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ActorAttestation {
    /// Stable actor identity reference.
    pub identity_ref: String,
    /// Stable attestation reference.
    pub attestation_ref: String,
    /// Operator approval reference.
    pub operator_approval_ref: String,
    /// `sha256:<hex>` operator approval hash.
    pub operator_approval_hash: String,
}

/// Policy decision block on execution receipts.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ExecutionPolicyDecision {
    /// Stable decision identifier.
    pub decision_id: String,
    /// Policy decision value.
    pub result: ExecutionPolicyResult,
}

/// Policy decision result vocabulary on execution receipts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ExecutionPolicyResult {
    /// Policy denied.
    Deny,
    /// Policy review required.
    ReviewRequired,
    /// Policy partial.
    Partial,
    /// Policy allowed.
    Allow,
}

/// Token scope block.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TokenScope {
    /// Stable token identifier.
    pub token_id: String,
    /// Token audience.
    pub audience: String,
    /// `sha256:<hex>` scope hash.
    pub scope_hash: String,
    /// `sha256:<hex>` operation hash.
    pub operation_hash: String,
    /// `sha256:<hex>` manifest hash.
    pub manifest_hash: String,
    /// `sha256:<hex>` request hash.
    pub request_hash: String,
    /// RFC 3339 token expiry.
    pub expires_at: DateTime<Utc>,
    /// Token revocation result.
    pub revocation_result: TokenRevocationResult,
}

/// Token revocation result vocabulary.
///
/// `Inactive` is accepted as an extension state observed in some pai-axiom
/// fixtures (`inactive-token-axiom-execution-trust.json`); it is treated as
/// equivalent to `Revoked` for admission gating.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TokenRevocationResult {
    /// Token active.
    Active,
    /// Token revoked.
    Revoked,
    /// Token inactive (fixture extension).
    Inactive,
    /// Token state unknown.
    Unknown,
}

impl TokenRevocationResult {
    /// Whether this revocation state must reject admission.
    #[must_use]
    pub const fn must_reject(self) -> bool {
        matches!(self, Self::Revoked | Self::Inactive)
    }
}

/// Tool provenance block on execution receipts.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ExecutionToolProvenance {
    /// Stable tool identifier.
    pub tool_id: String,
    /// Tool version string.
    pub tool_version: String,
    /// Command reference.
    pub command_ref: String,
    /// 40-hex source commit.
    pub source_commit: String,
    /// Dependency lock reference.
    pub dependency_lock_ref: String,
}

/// Source anchor on an execution receipt.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ExecutionSourceAnchor {
    /// Stable source identifier.
    pub source_id: String,
    /// Source type vocabulary on execution receipts.
    pub source_type: ExecutionSourceAnchorType,
    /// Stable source reference.
    pub r#ref: String,
    /// `sha256:<hex>` hash.
    pub hash: String,
}

/// Source anchor type vocabulary on execution receipts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ExecutionSourceAnchorType {
    /// Command anchor.
    Command,
    /// File anchor.
    File,
    /// Test anchor.
    Test,
    /// Ledger anchor.
    Ledger,
    /// Runtime anchor.
    Runtime,
    /// Operator approval anchor.
    Approval,
}

// ---------------------------------------------------------------------------
// Authority feedback loop envelope
// ---------------------------------------------------------------------------

/// Authority feedback loop record (`AUTHORITY_FEEDBACK_LOOP.schema.json` v1).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AuthorityFeedbackLoop {
    /// Schema discriminator.
    pub schema: String,
    /// Schema version.
    pub version: u16,
    /// Optional stable receiver-side reference.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub authority_feedback_loop_ref: Option<String>,
    /// Stable loop identifier.
    pub loop_id: String,
    /// Loop start timestamp.
    pub started_at: DateTime<Utc>,
    /// Initiating context block.
    pub initiating_context: FeedbackInitiatingContext,
    /// AXIOM action block.
    pub axiom_action: FeedbackAxiomAction,
    /// Returned artifact descriptors.
    pub returned_artifacts: Vec<FeedbackReturnedArtifact>,
    /// Amplification risk level.
    pub amplification_risk: AmplificationRisk,
    /// Independent evidence references.
    #[serde(default)]
    pub independent_evidence_refs: Vec<String>,
    /// External grounding references.
    #[serde(default)]
    pub external_grounding_refs: Vec<String>,
    /// Contradiction scan reference.
    pub contradiction_scan_ref: String,
    /// Loop quarantine state.
    pub quarantine_state: ContextQuarantineState,
    /// Confidence ceiling permitted on loop output.
    pub confidence_ceiling: ConfidenceCeiling,
    /// Same-loop promotion permission. Must be `false`.
    pub same_loop_promotion_allowed: bool,
    /// Authority claims block (optional only in the strictest pai-axiom
    /// negative fixtures; required in valid emission).
    pub authority_claims: FeedbackAuthorityClaims,
    /// Target-domain validation block.
    pub target_domain_validation: TargetDomainValidation,
    /// Residual risk strings.
    #[serde(default)]
    pub residual_risk: Vec<String>,
}

/// Initiating-context block on a feedback loop record.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FeedbackInitiatingContext {
    /// Stable initiating context identifier.
    pub context_id: String,
    /// Reference to the matching Cortex context trust envelope.
    pub cortex_context_trust_ref: String,
}

/// AXIOM action block on a feedback loop record.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FeedbackAxiomAction {
    /// Stable action identifier.
    pub action_id: String,
    /// Reference to the matching pai-axiom execution trust envelope.
    pub axiom_execution_trust_ref: String,
}

/// Returned-artifact descriptor on a feedback loop record.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FeedbackReturnedArtifact {
    /// Stable artifact identifier.
    pub artifact_id: String,
    /// Lineage reference.
    pub lineage_ref: String,
    /// Lifecycle state of the artifact.
    pub lifecycle_state: ArtifactLifecycleState,
    /// Reproducibility level of the artifact.
    pub reproducibility_level: ReproducibilityLevel,
}

/// Lifecycle state vocabulary for returned artifacts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ArtifactLifecycleState {
    /// Candidate only.
    Candidate,
    /// Observed.
    Observed,
    /// Validated.
    Validated,
    /// Promoted.
    Promoted,
    /// Stale.
    Stale,
    /// Quarantined.
    Quarantined,
}

/// Reproducibility level vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ReproducibilityLevel {
    /// Deterministic.
    Deterministic,
    /// Bounded nondeterministic.
    BoundedNondeterministic,
    /// Observational.
    Observational,
    /// Non-reproducible.
    NonReproducible,
}

impl Serialize for ReproducibilityLevel {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(self.wire_string())
    }
}

impl ReproducibilityLevel {
    /// Stable wire string.
    #[must_use]
    pub const fn wire_string(self) -> &'static str {
        match self {
            Self::Deterministic => "deterministic",
            Self::BoundedNondeterministic => "bounded-nondeterministic",
            Self::Observational => "observational",
            Self::NonReproducible => "non-reproducible",
        }
    }
}

/// Amplification risk vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AmplificationRisk {
    /// Low.
    Low,
    /// Medium.
    Medium,
    /// High.
    High,
    /// Critical.
    Critical,
}

/// Confidence ceiling vocabulary on the feedback loop record.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConfidenceCeiling {
    /// Untrusted.
    Untrusted,
    /// Advisory.
    Advisory,
    /// Observed.
    Observed,
    /// Validated.
    Validated,
}

/// Authority claims block.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FeedbackAuthorityClaims {
    /// Durable truth promotion claim status.
    pub durable_truth_promotion: AuthorityClaimStatus,
    /// Full execution authority claim status.
    pub full_execution_authority: AuthorityClaimStatus,
    /// Whether review is required.
    pub review_required: bool,
}

/// Authority claim status vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuthorityClaimStatus {
    /// Denied.
    Denied,
    /// Review required.
    ReviewRequired,
    /// Eligible after independent validation.
    EligibleAfterIndependentValidation,
}

impl AuthorityClaimStatus {
    /// Whether the producer claimed authority-grade promotion. Cortex
    /// rejects this regardless of any other contributor.
    #[must_use]
    pub const fn claims_authority(self) -> bool {
        matches!(self, Self::EligibleAfterIndependentValidation)
    }
}

/// Target-domain validation block.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TargetDomainValidation {
    /// Whether validation is required (must be `true`).
    pub required: bool,
    /// Reference to the independent validation, if produced.
    pub independent_validation_ref: Option<String>,
    /// Validation result.
    pub result: TargetDomainValidationResult,
}

/// Target-domain validation result vocabulary.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TargetDomainValidationResult {
    /// Pending validation.
    Pending,
    /// Validation passed.
    Pass,
    /// Validation failed.
    Fail,
    /// Validation partial.
    Partial,
}

// ---------------------------------------------------------------------------
// Named quarantine outputs (ADR 0042 §7)
// ---------------------------------------------------------------------------

/// Per-source quarantine output as required by ADR 0042 §7.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct QuarantineOutput {
    /// Stable invariant name explaining why this output is quarantined.
    pub invariant: String,
    /// Operator-facing reason.
    pub reason: String,
    /// Optional reference back to the source artifact.
    pub source_ref: Option<String>,
}

impl QuarantineOutput {
    /// Construct a quarantine output.
    #[must_use]
    pub fn new(invariant: impl Into<String>, reason: impl Into<String>) -> Self {
        Self {
            invariant: invariant.into(),
            reason: reason.into(),
            source_ref: None,
        }
    }

    /// Attach a stable source reference.
    #[must_use]
    pub fn with_source_ref(mut self, source_ref: impl Into<String>) -> Self {
        self.source_ref = Some(source_ref.into());
        self
    }
}

/// Named per-source quarantine outputs, mirroring ADR 0042 §7.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NamedQuarantineOutputs {
    /// Source-context quarantine (Cortex side).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source_context: Option<QuarantineOutput>,
    /// Token revocation quarantine.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub token_revocation: Option<QuarantineOutput>,
    /// Repo trust quarantine.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub repo_trust: Option<QuarantineOutput>,
    /// Policy denial quarantine.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub policy_denial: Option<QuarantineOutput>,
    /// Target-domain validation quarantine.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub target_validation: Option<QuarantineOutput>,
    /// Derived artifact quarantine.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub derived_artifact: Option<QuarantineOutput>,
    /// Contradiction-state quarantine.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub contradiction: Option<QuarantineOutput>,
}

impl NamedQuarantineOutputs {
    /// Whether any of the seven named outputs is populated.
    #[must_use]
    pub fn any_present(&self) -> bool {
        self.source_context.is_some()
            || self.token_revocation.is_some()
            || self.repo_trust.is_some()
            || self.policy_denial.is_some()
            || self.target_validation.is_some()
            || self.derived_artifact.is_some()
            || self.contradiction.is_some()
    }

    /// Iterate over every populated stable invariant name.
    pub fn invariants(&self) -> impl Iterator<Item = &str> {
        [
            self.source_context.as_ref(),
            self.token_revocation.as_ref(),
            self.repo_trust.as_ref(),
            self.policy_denial.as_ref(),
            self.target_validation.as_ref(),
            self.derived_artifact.as_ref(),
            self.contradiction.as_ref(),
        ]
        .into_iter()
        .flatten()
        .map(|q| q.invariant.as_str())
    }
}

// ---------------------------------------------------------------------------
// Field validation errors (stable invariant names)
// ---------------------------------------------------------------------------

/// Stable field-level invariant failure.
///
/// `invariant` is the dot-pathed stable name (e.g.
/// `axiom_execution_trust.token_scope.audience.missing`) consumed by the
/// admission gate and by external pai-axiom acceptance tests.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TrustExchangeFieldError {
    /// Stable dot-pathed invariant name.
    pub invariant: String,
    /// Operator-facing reason.
    pub reason: String,
}

impl TrustExchangeFieldError {
    /// Construct a field-level error.
    #[must_use]
    pub fn new(invariant: impl Into<String>, reason: impl Into<String>) -> Self {
        Self {
            invariant: invariant.into(),
            reason: reason.into(),
        }
    }
}

/// Result type for field-level validation across the trust exchange envelopes.
pub type TrustExchangeValidation = Result<(), Vec<TrustExchangeFieldError>>;

// ---------------------------------------------------------------------------
// Wrappers — pai-axiom fixtures wrap the inner envelope in a single key
// ---------------------------------------------------------------------------

/// Envelope shape matching pai-axiom's wrapped fixtures
/// (`{ "cortex_context_trust": { ... } }`).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CortexContextTrustEnvelope {
    /// Wrapped cortex context trust object.
    pub cortex_context_trust: CortexContextTrust,
}

/// Envelope shape matching pai-axiom's wrapped fixtures
/// (`{ "axiom_execution_trust": { ... } }`).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AxiomExecutionTrustEnvelope {
    /// Wrapped axiom execution trust object.
    pub axiom_execution_trust: AxiomExecutionTrust,
}

// ---------------------------------------------------------------------------
// Parse helpers — accept wrapped or unwrapped form
// ---------------------------------------------------------------------------

/// Parse a Cortex context trust envelope from either the wrapped pai-axiom
/// fixture shape `{ "cortex_context_trust": { ... } }` or the unwrapped
/// schema-bare object form.
pub fn parse_cortex_context_trust(
    input: &str,
) -> Result<CortexContextTrust, TrustExchangeFieldError> {
    let value: serde_json::Value = serde_json::from_str(input).map_err(|err| {
        TrustExchangeFieldError::new(
            "cortex_context_trust.envelope.invalid_json",
            format!("invalid JSON: {err}"),
        )
    })?;
    let inner = match value.get("cortex_context_trust") {
        Some(inner) => inner.clone(),
        None => value,
    };
    serde_json::from_value(inner).map_err(|err| {
        TrustExchangeFieldError::new(
            "cortex_context_trust.envelope.schema_drift",
            format!("envelope failed schema check: {err}"),
        )
    })
}

/// Parse a pai-axiom execution trust envelope from either the wrapped form
/// `{ "axiom_execution_trust": { ... } }` or the bare object form.
pub fn parse_axiom_execution_trust(
    input: &str,
) -> Result<AxiomExecutionTrust, TrustExchangeFieldError> {
    let value: serde_json::Value = serde_json::from_str(input).map_err(|err| {
        TrustExchangeFieldError::new(
            "axiom_execution_trust.envelope.invalid_json",
            format!("invalid JSON: {err}"),
        )
    })?;
    let inner = match value.get("axiom_execution_trust") {
        Some(inner) => inner.clone(),
        None => value,
    };
    serde_json::from_value(inner).map_err(|err| {
        TrustExchangeFieldError::new(
            "axiom_execution_trust.envelope.schema_drift",
            format!("envelope failed schema check: {err}"),
        )
    })
}

/// Parse an authority feedback loop record. The feedback loop fixture is
/// not wrapped in pai-axiom's emission, so we accept the bare object only.
pub fn parse_authority_feedback_loop(
    input: &str,
) -> Result<AuthorityFeedbackLoop, TrustExchangeFieldError> {
    serde_json::from_str(input).map_err(|err| {
        TrustExchangeFieldError::new(
            "authority_feedback_loop.envelope.schema_drift",
            format!("envelope failed schema check: {err}"),
        )
    })
}

// ---------------------------------------------------------------------------
// Receiver-side axiom source-commit freshness gate
// ---------------------------------------------------------------------------
//
// The first live Cortex ↔ axiom admission exchange against axiom packet SHA
// `9a15d281` (record at `docs/transcripts/AXIOM_LIVE_EXCHANGE_2026-05-13/
// SUMMARY.md`) surfaced one real receiver-side gap: the
// `stale-pai-axiom-sha` fixture was admitted by Cortex despite the packet's
// `axiom_execution_trust.tool_provenance.source_commit` pointing at a stale
// axiom SHA. The axiom team's fixture expected `freshness_refusal` keyed on
// `axiom_execution_trust.tool_provenance.source_commit.stale`; Cortex
// returned `decision=admit_candidate`, exit 0.
//
// This module section is the receiver-side closure: a small, additive
// freshness gate Cortex callers can run alongside the existing structural
// validation. The accept set is owned at the Cortex side (ADR 0042
// acceptance pin + subsequent known-good axiom syncs); the env-var override
// lets operators rotate the pin without a re-deploy.

/// Stable invariant emitted when the axiom-execution-trust envelope's
/// `tool_provenance.source_commit` is not on the receiver-side acceptance
/// list. Downstream tooling (operator scripts, axiom-side test harnesses)
/// greps on this string.
pub const AXIOM_EXECUTION_TRUST_SOURCE_COMMIT_STALE_INVARIANT: &str =
    "axiom_execution_trust.tool_provenance.source_commit.stale";

/// Environment variable that, when set, REPLACES the default
/// [`DEFAULT_ACCEPTED_AXIOM_SOURCE_COMMITS`] accept-list with a
/// comma-separated set of operator-supplied 40-hex commit SHAs.
///
/// Setting this to the empty string is equivalent to setting it to an empty
/// list — the freshness gate will then refuse every source_commit. Operators
/// using this surface MUST supply at least one SHA.
///
/// Whitespace around each SHA is trimmed; the value is normalised to
/// lowercase before comparison.
pub const CORTEX_AXIOM_ACCEPTED_SOURCE_COMMITS_ENV: &str = "CORTEX_AXIOM_ACCEPTED_SOURCE_COMMITS";

/// Default Cortex-side accept-list for the
/// `axiom_execution_trust.tool_provenance.source_commit` freshness gate.
///
/// Three production axiom SHAs are listed by virtue of being either the ADR
/// 0042 acceptance pin or a subsequent axiom-side sync the Cortex team has
/// already accepted as known-good:
///
/// - `44b9a25dfbe5a93e64120f53b65730828d1af91c` — ADR 0042 acceptance pin.
/// - `062d0d42ac5ef300fa3e04ef5b49b14864babcdd` — axiom team's alignment
///   acknowledgement to Cortex receiver-readiness at `0e67a32`
///   (2026-05-13).
/// - `9a15d281ddcc2bcf36956fbe6d6c5736d8ce706a` — axiom team's V2 live-
///   exchange payload corpus delivery (2026-05-13).
///
/// One test-fixture placeholder SHA is also listed so the Cortex-side
/// fixtures (which use `aaaa…aaaa` deliberately to flag the value as a
/// test placeholder) continue to admit. The fixture SHA is well-known and
/// carries no real authority — including it on the accept-list has no
/// security impact because any payload reaching the freshness gate has
/// already passed the upstream structural validation that an attacker
/// cannot trivially forge.
///
/// To rotate without re-deploying, set
/// [`CORTEX_AXIOM_ACCEPTED_SOURCE_COMMITS_ENV`].
pub const DEFAULT_ACCEPTED_AXIOM_SOURCE_COMMITS: &[&str] = &[
    // ADR 0042 acceptance pin.
    "44b9a25dfbe5a93e64120f53b65730828d1af91c",
    // Axiom alignment ack to Cortex 0e67a32 readiness (2026-05-13).
    "062d0d42ac5ef300fa3e04ef5b49b14864babcdd",
    // Axiom V2 live-exchange payload corpus delivery (2026-05-13).
    "9a15d281ddcc2bcf36956fbe6d6c5736d8ce706a",
    // Test-fixture placeholder; see doc-comment above for the rationale.
    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
];

/// Resolve the active accept-list for the
/// [`AXIOM_EXECUTION_TRUST_SOURCE_COMMIT_STALE_INVARIANT`] freshness gate.
///
/// Resolution order:
///
/// 1. If [`CORTEX_AXIOM_ACCEPTED_SOURCE_COMMITS_ENV`] is set, parse it as a
///    comma-separated list of 40-hex SHAs (whitespace trimmed, lowercase
///    normalised, empty entries discarded).
/// 2. Otherwise, return [`DEFAULT_ACCEPTED_AXIOM_SOURCE_COMMITS`] as a
///    `Vec<String>`.
///
/// Returned SHAs are lower-cased; callers using
/// [`is_axiom_source_commit_fresh`] do not need to lower-case the candidate
/// value separately.
#[must_use]
pub fn accepted_axiom_source_commits() -> Vec<String> {
    if let Ok(raw) = std::env::var(CORTEX_AXIOM_ACCEPTED_SOURCE_COMMITS_ENV) {
        return raw
            .split(',')
            .map(|piece| piece.trim().to_ascii_lowercase())
            .filter(|piece| !piece.is_empty())
            .collect();
    }
    DEFAULT_ACCEPTED_AXIOM_SOURCE_COMMITS
        .iter()
        .map(|sha| (*sha).to_string())
        .collect()
}

/// Check whether a candidate `source_commit` is on the supplied
/// receiver-side accept-list.
///
/// Comparison is case-insensitive on the candidate; the accept-list is
/// assumed to be lower-cased (the canonical form returned by
/// [`accepted_axiom_source_commits`]). The candidate must also pass the
/// 40-character lowercase-hex structural check
/// (`tool_provenance.source_commit.invalid_format`) before reaching this
/// gate; this function does NOT re-validate format.
#[must_use]
pub fn is_axiom_source_commit_fresh(candidate: &str, accepted: &[String]) -> bool {
    let lowered = candidate.to_ascii_lowercase();
    accepted.iter().any(|sha| sha == &lowered)
}

// ---------------------------------------------------------------------------
// Field validators — stable invariant names
// ---------------------------------------------------------------------------

const SHA256_PATTERN_PREFIX: &str = "sha256:";
const SHA256_HEX_LEN: usize = 64;

fn is_sha256_hash(value: &str) -> bool {
    if let Some(hex) = value.strip_prefix(SHA256_PATTERN_PREFIX) {
        hex.len() == SHA256_HEX_LEN
            && hex
                .chars()
                .all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase())
    } else {
        false
    }
}

fn is_commit_sha(value: &str) -> bool {
    value.len() == 40
        && value
            .chars()
            .all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase())
}

fn push(errors: &mut Vec<TrustExchangeFieldError>, invariant: &str, reason: impl Into<String>) {
    errors.push(TrustExchangeFieldError::new(invariant, reason));
}

fn require_nonblank(
    errors: &mut Vec<TrustExchangeFieldError>,
    value: &str,
    invariant: &str,
    reason: &str,
) {
    if value.trim().is_empty() {
        push(errors, invariant, reason);
    }
}

impl CortexContextTrust {
    /// Validate the decomposed fields required for Cortex admission.
    ///
    /// Returns a list of stable invariant failures. The `compatibility_trust_label`
    /// is always treated as display-only and never satisfies a required field.
    pub fn validate(&self) -> TrustExchangeValidation {
        let mut errors: Vec<TrustExchangeFieldError> = Vec::new();

        if self.schema != CORTEX_CONTEXT_TRUST_SCHEMA {
            push(
                &mut errors,
                "cortex_context_trust.schema.mismatch",
                format!(
                    "schema must be `{CORTEX_CONTEXT_TRUST_SCHEMA}`, got `{}`",
                    self.schema
                ),
            );
        }
        if self.version != TRUST_EXCHANGE_SCHEMA_VERSION {
            push(
                &mut errors,
                "cortex_context_trust.version.mismatch",
                format!(
                    "version must be {TRUST_EXCHANGE_SCHEMA_VERSION}, got {}",
                    self.version
                ),
            );
        }

        require_nonblank(
            &mut errors,
            &self.context_id,
            "cortex_context_trust.context_id.missing",
            "context_id must be a non-empty string",
        );

        // proof_state.* (each subfield contributes its own invariant)
        if self
            .proof_state
            .failing_edges
            .iter()
            .any(|e| e.trim().is_empty())
        {
            push(
                &mut errors,
                "cortex_context_trust.proof_state.failing_edges.blank_entry",
                "proof_state.failing_edges entries must not be blank",
            );
        }
        if self
            .proof_state
            .proof_refs
            .iter()
            .any(|e| e.trim().is_empty())
        {
            push(
                &mut errors,
                "cortex_context_trust.proof_state.proof_refs.blank_entry",
                "proof_state.proof_refs entries must not be blank",
            );
        }
        if matches!(
            self.proof_state.state,
            ContextProofStateValue::Missing | ContextProofStateValue::Failed
        ) {
            push(
                &mut errors,
                "cortex_context_trust.proof_state.state.unusable",
                "proof_state.state must be `closed` or `partial` for Cortex consumption",
            );
        }

        // semantic_trust.*
        require_nonblank(
            &mut errors,
            &self.semantic_trust.weighting_basis,
            "cortex_context_trust.semantic_trust.weighting_basis.missing",
            "semantic_trust.weighting_basis must be a non-empty string",
        );
        if !(0.0..=1.0).contains(&self.semantic_trust.trust_weight) {
            push(
                &mut errors,
                "cortex_context_trust.semantic_trust.trust_weight.out_of_range",
                "semantic_trust.trust_weight must be in [0, 1]",
            );
        }
        if matches!(
            self.semantic_trust.provenance_class,
            ContextProvenanceClass::Unknown
        ) {
            push(
                &mut errors,
                "cortex_context_trust.semantic_trust.provenance_class.unknown",
                "semantic_trust.provenance_class must be a known class",
            );
        }

        // redaction_state.*
        if matches!(self.redaction_state.status, ContextRedactionStatus::Unknown) {
            push(
                &mut errors,
                "cortex_context_trust.redaction_state.status.unknown",
                "redaction_state.status must be a known status",
            );
        }
        if self
            .redaction_state
            .redaction_refs
            .iter()
            .any(|e| e.trim().is_empty())
        {
            push(
                &mut errors,
                "cortex_context_trust.redaction_state.redaction_refs.blank_entry",
                "redaction_state.redaction_refs entries must not be blank",
            );
        }

        // policy_result.*
        require_nonblank(
            &mut errors,
            &self.policy_result.decision_id,
            "cortex_context_trust.policy_result.decision_id.missing",
            "policy_result.decision_id must be a non-empty string",
        );

        // allowed_claim_language / forbidden_uses minimum coverage
        if self.allowed_claim_language.len() < 3 {
            push(
                &mut errors,
                "cortex_context_trust.allowed_claim_language.insufficient_coverage",
                "allowed_claim_language must include candidate, observed, and validated",
            );
        }
        if self.forbidden_uses.len() < 3 {
            push(
                &mut errors,
                "cortex_context_trust.forbidden_uses.insufficient_coverage",
                "forbidden_uses must include execution_permission, durable_truth_promotion, release_claim",
            );
        }

        // source_anchors.*
        if self.source_anchors.is_empty() {
            push(
                &mut errors,
                "cortex_context_trust.source_anchors.missing",
                "source_anchors must contain at least one anchor",
            );
        }
        for anchor in &self.source_anchors {
            require_nonblank(
                &mut errors,
                &anchor.source_id,
                "cortex_context_trust.source_anchors.source_id.missing",
                "source_anchors[].source_id must be a non-empty string",
            );
            require_nonblank(
                &mut errors,
                &anchor.r#ref,
                "cortex_context_trust.source_anchors.ref.missing",
                "source_anchors[].ref must be a non-empty string",
            );
            if !is_sha256_hash(&anchor.hash) {
                push(
                    &mut errors,
                    "cortex_context_trust.source_anchors.hash.invalid_format",
                    "source_anchors[].hash must match sha256:<64 lowercase hex>",
                );
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }
}

impl AxiomExecutionTrust {
    /// Validate the decomposed fields required for Cortex admission of a
    /// pai-axiom execution receipt.
    pub fn validate(&self) -> TrustExchangeValidation {
        let mut errors: Vec<TrustExchangeFieldError> = Vec::new();

        if self.schema != AXIOM_EXECUTION_TRUST_SCHEMA {
            push(
                &mut errors,
                "axiom_execution_trust.schema.mismatch",
                format!(
                    "schema must be `{AXIOM_EXECUTION_TRUST_SCHEMA}`, got `{}`",
                    self.schema
                ),
            );
        }
        if self.version != TRUST_EXCHANGE_SCHEMA_VERSION {
            push(
                &mut errors,
                "axiom_execution_trust.version.mismatch",
                format!(
                    "version must be {TRUST_EXCHANGE_SCHEMA_VERSION}, got {}",
                    self.version
                ),
            );
        }

        require_nonblank(
            &mut errors,
            &self.action_id,
            "axiom_execution_trust.action_id.missing",
            "action_id must be a non-empty string",
        );

        // repo_trust.*
        require_nonblank(
            &mut errors,
            &self.repo_trust.evaluation_ref,
            "axiom_execution_trust.repo_trust.evaluation_ref.missing",
            "repo_trust.evaluation_ref must be a non-empty string",
        );

        // actor_attestation.*
        require_nonblank(
            &mut errors,
            &self.actor_attestation.identity_ref,
            "axiom_execution_trust.actor_attestation.identity_ref.missing",
            "actor_attestation.identity_ref must be a non-empty string",
        );
        require_nonblank(
            &mut errors,
            &self.actor_attestation.attestation_ref,
            "axiom_execution_trust.actor_attestation.attestation_ref.missing",
            "actor_attestation.attestation_ref must be a non-empty string",
        );
        require_nonblank(
            &mut errors,
            &self.actor_attestation.operator_approval_ref,
            "axiom_execution_trust.actor_attestation.operator_approval_ref.missing",
            "actor_attestation.operator_approval_ref must be a non-empty string",
        );
        if !is_sha256_hash(&self.actor_attestation.operator_approval_hash) {
            push(
                &mut errors,
                "axiom_execution_trust.actor_attestation.operator_approval_hash.invalid_format",
                "actor_attestation.operator_approval_hash must match sha256:<64 lowercase hex>",
            );
        }

        // policy_decision.*
        require_nonblank(
            &mut errors,
            &self.policy_decision.decision_id,
            "axiom_execution_trust.policy_decision.decision_id.missing",
            "policy_decision.decision_id must be a non-empty string",
        );

        // token_scope.*
        require_nonblank(
            &mut errors,
            &self.token_scope.token_id,
            "axiom_execution_trust.token_scope.token_id.missing",
            "token_scope.token_id must be a non-empty string",
        );
        require_nonblank(
            &mut errors,
            &self.token_scope.audience,
            "axiom_execution_trust.token_scope.audience.missing",
            "token_scope.audience must be a non-empty string",
        );
        for (field, value, invariant) in [
            (
                "scope_hash",
                &self.token_scope.scope_hash,
                "axiom_execution_trust.token_scope.scope_hash.invalid_format",
            ),
            (
                "operation_hash",
                &self.token_scope.operation_hash,
                "axiom_execution_trust.token_scope.operation_hash.invalid_format",
            ),
            (
                "manifest_hash",
                &self.token_scope.manifest_hash,
                "axiom_execution_trust.token_scope.manifest_hash.invalid_format",
            ),
            (
                "request_hash",
                &self.token_scope.request_hash,
                "axiom_execution_trust.token_scope.request_hash.invalid_format",
            ),
        ] {
            if !is_sha256_hash(value) {
                push(
                    &mut errors,
                    invariant,
                    format!("token_scope.{field} must match sha256:<64 lowercase hex>"),
                );
            }
        }

        // tool_provenance.*
        require_nonblank(
            &mut errors,
            &self.tool_provenance.tool_id,
            "axiom_execution_trust.tool_provenance.tool_id.missing",
            "tool_provenance.tool_id must be a non-empty string",
        );
        require_nonblank(
            &mut errors,
            &self.tool_provenance.tool_version,
            "axiom_execution_trust.tool_provenance.tool_version.missing",
            "tool_provenance.tool_version must be a non-empty string",
        );
        require_nonblank(
            &mut errors,
            &self.tool_provenance.command_ref,
            "axiom_execution_trust.tool_provenance.command_ref.missing",
            "tool_provenance.command_ref must be a non-empty string",
        );
        if !is_commit_sha(&self.tool_provenance.source_commit) {
            push(
                &mut errors,
                "axiom_execution_trust.tool_provenance.source_commit.invalid_format",
                "tool_provenance.source_commit must be a 40-character lowercase hex SHA",
            );
        }
        require_nonblank(
            &mut errors,
            &self.tool_provenance.dependency_lock_ref,
            "axiom_execution_trust.tool_provenance.dependency_lock_ref.missing",
            "tool_provenance.dependency_lock_ref must be a non-empty string",
        );

        // source_anchors.*
        if self.source_anchors.is_empty() {
            push(
                &mut errors,
                "axiom_execution_trust.source_anchors.missing",
                "source_anchors must contain at least one anchor",
            );
        }
        for anchor in &self.source_anchors {
            require_nonblank(
                &mut errors,
                &anchor.source_id,
                "axiom_execution_trust.source_anchors.source_id.missing",
                "source_anchors[].source_id must be a non-empty string",
            );
            require_nonblank(
                &mut errors,
                &anchor.r#ref,
                "axiom_execution_trust.source_anchors.ref.missing",
                "source_anchors[].ref must be a non-empty string",
            );
            if !is_sha256_hash(&anchor.hash) {
                push(
                    &mut errors,
                    "axiom_execution_trust.source_anchors.hash.invalid_format",
                    "source_anchors[].hash must match sha256:<64 lowercase hex>",
                );
            }
        }

        // runtime_mode
        require_nonblank(
            &mut errors,
            &self.runtime_mode,
            "axiom_execution_trust.runtime_mode.missing",
            "runtime_mode must be a non-empty string",
        );

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    /// Check whether the token is expired relative to `now`.
    #[must_use]
    pub fn token_expired_at(&self, now: DateTime<Utc>) -> bool {
        self.token_scope.expires_at < now
    }
}

impl AuthorityFeedbackLoop {
    /// Validate decomposed fields required at the Cortex consumer.
    pub fn validate(&self) -> TrustExchangeValidation {
        let mut errors: Vec<TrustExchangeFieldError> = Vec::new();

        if self.schema != AUTHORITY_FEEDBACK_LOOP_SCHEMA {
            push(
                &mut errors,
                "authority_feedback_loop.schema.mismatch",
                format!(
                    "schema must be `{AUTHORITY_FEEDBACK_LOOP_SCHEMA}`, got `{}`",
                    self.schema
                ),
            );
        }
        if self.version != TRUST_EXCHANGE_SCHEMA_VERSION {
            push(
                &mut errors,
                "authority_feedback_loop.version.mismatch",
                format!(
                    "version must be {TRUST_EXCHANGE_SCHEMA_VERSION}, got {}",
                    self.version
                ),
            );
        }

        require_nonblank(
            &mut errors,
            &self.loop_id,
            "authority_feedback_loop.loop_id.missing",
            "loop_id must be a non-empty string",
        );

        require_nonblank(
            &mut errors,
            &self.initiating_context.context_id,
            "authority_feedback_loop.initiating_context.context_id.missing",
            "initiating_context.context_id must be a non-empty string",
        );
        require_nonblank(
            &mut errors,
            &self.initiating_context.cortex_context_trust_ref,
            "authority_feedback_loop.initiating_context.cortex_context_trust_ref.missing",
            "initiating_context.cortex_context_trust_ref must be a non-empty string",
        );
        require_nonblank(
            &mut errors,
            &self.axiom_action.action_id,
            "authority_feedback_loop.axiom_action.action_id.missing",
            "axiom_action.action_id must be a non-empty string",
        );
        require_nonblank(
            &mut errors,
            &self.axiom_action.axiom_execution_trust_ref,
            "authority_feedback_loop.axiom_action.axiom_execution_trust_ref.missing",
            "axiom_action.axiom_execution_trust_ref must be a non-empty string",
        );

        if self.returned_artifacts.is_empty() {
            push(
                &mut errors,
                "authority_feedback_loop.returned_artifacts.missing",
                "returned_artifacts must contain at least one artifact",
            );
        }
        for artifact in &self.returned_artifacts {
            require_nonblank(
                &mut errors,
                &artifact.artifact_id,
                "authority_feedback_loop.returned_artifacts.artifact_id.missing",
                "returned_artifacts[].artifact_id must be a non-empty string",
            );
            require_nonblank(
                &mut errors,
                &artifact.lineage_ref,
                "authority_feedback_loop.returned_artifacts.lineage_ref.missing",
                "returned_artifacts[].lineage_ref must be a non-empty string",
            );
        }

        require_nonblank(
            &mut errors,
            &self.contradiction_scan_ref,
            "authority_feedback_loop.contradiction_scan_ref.missing",
            "contradiction_scan_ref must be a non-empty string",
        );

        if !self.target_domain_validation.required {
            push(
                &mut errors,
                "authority_feedback_loop.target_domain_validation.required.must_be_true",
                "target_domain_validation.required must be true",
            );
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    /// Hard structural Cortex refusal: same-loop promotion must be `false`.
    /// Returns `true` when the loop record is structurally inadmissible.
    #[must_use]
    pub const fn violates_same_loop_invariant(&self) -> bool {
        self.same_loop_promotion_allowed
    }

    /// Hard structural Cortex refusal: authority claims signaling durable
    /// truth or full execution authority must be rejected.
    #[must_use]
    pub const fn claims_durable_authority(&self) -> bool {
        self.authority_claims
            .durable_truth_promotion
            .claims_authority()
            || self
                .authority_claims
                .full_execution_authority
                .claims_authority()
    }
}

// ---------------------------------------------------------------------------
// Tests — round-trip with the pai-axiom valid fixtures, stable invariant names
// ---------------------------------------------------------------------------

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

    const VALID_CTX: &str =
        include_str!("../tests/fixtures/pai-axiom/valid-cortex-context-trust.json");
    const VALID_EXEC: &str =
        include_str!("../tests/fixtures/pai-axiom/valid-axiom-execution-trust.json");

    #[test]
    fn parse_valid_cortex_context_trust_round_trips() {
        let envelope = parse_cortex_context_trust(VALID_CTX).expect("valid fixture parses");
        assert_eq!(envelope.schema, CORTEX_CONTEXT_TRUST_SCHEMA);
        assert_eq!(envelope.version, TRUST_EXCHANGE_SCHEMA_VERSION);
        assert_eq!(envelope.context_id, "ctx_valid_behavior_change");
        assert_eq!(envelope.quarantine_state, ContextQuarantineState::Clear);
        assert!(matches!(
            envelope.proof_state.state,
            ContextProofStateValue::Closed
        ));
        envelope.validate().expect("valid fixture validates");

        let reserialized = serde_json::to_value(&envelope).unwrap();
        assert_eq!(reserialized["schema"], "cortex_context_trust");
        assert_eq!(reserialized["truth_ceiling"], "validated");
    }

    #[test]
    fn parse_valid_axiom_execution_trust_round_trips() {
        let envelope = parse_axiom_execution_trust(VALID_EXEC).expect("valid fixture parses");
        assert_eq!(envelope.schema, AXIOM_EXECUTION_TRUST_SCHEMA);
        assert_eq!(envelope.version, TRUST_EXCHANGE_SCHEMA_VERSION);
        assert_eq!(envelope.action_id, "action_valid_authority_grade");
        assert_eq!(envelope.token_scope.audience, "cortex-admission");
        assert_eq!(
            envelope.token_scope.revocation_result,
            TokenRevocationResult::Active
        );
        envelope.validate().expect("valid fixture validates");

        let reserialized = serde_json::to_value(&envelope).unwrap();
        assert_eq!(reserialized["schema"], "axiom_execution_trust");
        assert_eq!(reserialized["execution_trust_level"], "authority_grade");
    }

    #[test]
    fn missing_token_audience_emits_stable_invariant() {
        let value: serde_json::Value = serde_json::from_str(VALID_EXEC).unwrap();
        let mut inner = value["axiom_execution_trust"].clone();
        inner["token_scope"]["audience"] = serde_json::Value::String(String::new());

        let envelope: AxiomExecutionTrust = serde_json::from_value(inner).unwrap();
        let errors = envelope.validate().expect_err("empty audience must fail");
        assert!(errors
            .iter()
            .any(|e| e.invariant == "axiom_execution_trust.token_scope.audience.missing"));
    }

    #[test]
    fn missing_operator_approval_hash_emits_stable_invariant() {
        let value: serde_json::Value = serde_json::from_str(VALID_EXEC).unwrap();
        let mut inner = value["axiom_execution_trust"].clone();
        inner["actor_attestation"]["operator_approval_hash"] =
            serde_json::Value::String("not-a-hash".to_string());

        let envelope: AxiomExecutionTrust = serde_json::from_value(inner).unwrap();
        let errors = envelope.validate().expect_err("bad hash must fail");
        assert!(errors.iter().any(|e| e.invariant

            == "axiom_execution_trust.actor_attestation.operator_approval_hash.invalid_format"));
    }

    #[test]
    fn invalid_source_commit_emits_stable_invariant() {
        let value: serde_json::Value = serde_json::from_str(VALID_EXEC).unwrap();
        let mut inner = value["axiom_execution_trust"].clone();
        inner["tool_provenance"]["source_commit"] =
            serde_json::Value::String("not-a-commit".to_string());

        let envelope: AxiomExecutionTrust = serde_json::from_value(inner).unwrap();
        let errors = envelope.validate().expect_err("bad commit must fail");
        assert!(errors
            .iter()
            .any(|e| e.invariant

                == "axiom_execution_trust.tool_provenance.source_commit.invalid_format"));
    }

    #[test]
    fn proof_state_failed_emits_stable_invariant() {
        let value: serde_json::Value = serde_json::from_str(VALID_CTX).unwrap();
        let mut inner = value["cortex_context_trust"].clone();
        inner["proof_state"]["state"] = serde_json::Value::String("failed".to_string());
        inner["proof_state"]["failing_edges"] = serde_json::json!(["proof.audit.missing"]);

        let envelope: CortexContextTrust = serde_json::from_value(inner).unwrap();
        let errors = envelope
            .validate()
            .expect_err("failed proof state must fail");
        assert!(errors
            .iter()
            .any(|e| e.invariant == "cortex_context_trust.proof_state.state.unusable"));
    }

    #[test]
    fn extra_top_level_field_fails_closed() {
        let json = serde_json::json!({
            "schema": "cortex_context_trust",
            "version": 1,
            "context_id": "ctx",
            "compatibility_trust_label": "validated",
            "proof_state": {"state": "closed", "failing_edges": [], "proof_refs": ["p"]},
            "truth_ceiling": "validated",
            "semantic_trust": {"provenance_class": "observed", "trust_weight": 1, "weighting_basis": "x"},
            "quarantine_state": "clear",
            "redaction_state": {"status": "none", "redaction_refs": []},
            "policy_result": {"decision_id": "p", "result": "allow"},
            "source_anchors": [{
                "source_id": "s",
                "source_type": "event",
                "ref": "r",
                "hash": "sha256:0000000000000000000000000000000000000000000000000000000000000000"
            }],
            "rogue_field": true
        });
        let err = serde_json::from_value::<CortexContextTrust>(json)
            .expect_err("deny_unknown_fields must reject rogue field");
        assert!(err.to_string().contains("rogue_field"));
    }

    #[test]
    fn revoked_token_is_must_reject() {
        assert!(TokenRevocationResult::Revoked.must_reject());
        assert!(TokenRevocationResult::Inactive.must_reject());
        assert!(!TokenRevocationResult::Active.must_reject());
    }

    #[test]
    fn token_expiry_check_uses_injected_now() {
        let mut envelope = parse_axiom_execution_trust(VALID_EXEC).unwrap();
        envelope.token_scope.expires_at = Utc.with_ymd_and_hms(2025, 1, 1, 0, 0, 0).unwrap();
        let now = Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap();
        assert!(envelope.token_expired_at(now));
    }

    #[test]
    fn same_loop_promotion_invariant_is_structural() {
        let loop_record = AuthorityFeedbackLoop {
            schema: AUTHORITY_FEEDBACK_LOOP_SCHEMA.to_string(),
            version: 1,
            authority_feedback_loop_ref: None,
            loop_id: "loop_x".to_string(),
            started_at: Utc::now(),
            initiating_context: FeedbackInitiatingContext {
                context_id: "ctx".to_string(),
                cortex_context_trust_ref: "ref".to_string(),
            },
            axiom_action: FeedbackAxiomAction {
                action_id: "act".to_string(),
                axiom_execution_trust_ref: "ref".to_string(),
            },
            returned_artifacts: vec![FeedbackReturnedArtifact {
                artifact_id: "art".to_string(),
                lineage_ref: "lin".to_string(),
                lifecycle_state: ArtifactLifecycleState::Candidate,
                reproducibility_level: ReproducibilityLevel::Observational,
            }],
            amplification_risk: AmplificationRisk::Low,
            independent_evidence_refs: vec![],
            external_grounding_refs: vec![],
            contradiction_scan_ref: "scan".to_string(),
            quarantine_state: ContextQuarantineState::Clear,
            confidence_ceiling: ConfidenceCeiling::Advisory,
            same_loop_promotion_allowed: true,
            authority_claims: FeedbackAuthorityClaims {
                durable_truth_promotion: AuthorityClaimStatus::Denied,
                full_execution_authority: AuthorityClaimStatus::Denied,
                review_required: true,
            },
            target_domain_validation: TargetDomainValidation {
                required: true,
                independent_validation_ref: None,
                result: TargetDomainValidationResult::Pending,
            },
            residual_risk: vec![],
        };
        assert!(loop_record.violates_same_loop_invariant());
        assert!(!loop_record.claims_durable_authority());
    }

    #[test]
    fn named_quarantine_outputs_iterate_invariants() {
        let outputs = NamedQuarantineOutputs {
            source_context: Some(QuarantineOutput::new(
                "axiom.admission.quarantine.propagated",
                "source quarantine",
            )),
            token_revocation: Some(QuarantineOutput::new(
                "axiom_execution_trust.token_scope.revoked",
                "revoked",
            )),
            ..Default::default()
        };
        let names: Vec<&str> = outputs.invariants().collect();
        assert_eq!(names.len(), 2);
        assert!(names.contains(&"axiom.admission.quarantine.propagated"));
        assert!(names.contains(&"axiom_execution_trust.token_scope.revoked"));
    }

    #[test]
    fn quarantine_state_propagation_predicate() {
        assert!(ContextQuarantineState::Quarantined.propagates_quarantine());
        assert!(ContextQuarantineState::DerivedFromQuarantined.propagates_quarantine());
        assert!(ContextQuarantineState::Unknown.propagates_quarantine());
        assert!(!ContextQuarantineState::Clear.propagates_quarantine());
    }

    // -----------------------------------------------------------------
    // Receiver-side source-commit freshness gate
    // -----------------------------------------------------------------

    /// Guard the stable invariant string against accidental rename. Operator
    /// scripts and the axiom-side test harness grep on this exact value.
    #[test]
    fn source_commit_stale_invariant_is_stable() {
        assert_eq!(
            AXIOM_EXECUTION_TRUST_SOURCE_COMMIT_STALE_INVARIANT,
            "axiom_execution_trust.tool_provenance.source_commit.stale"
        );
    }

    /// Guard the env-var name against rename. Operators set this in their
    /// shell; renaming silently breaks deployments.
    #[test]
    fn accepted_source_commits_env_var_name_is_stable() {
        assert_eq!(
            CORTEX_AXIOM_ACCEPTED_SOURCE_COMMITS_ENV,
            "CORTEX_AXIOM_ACCEPTED_SOURCE_COMMITS"
        );
    }

    #[test]
    fn default_accept_list_includes_adr_0042_pin() {
        assert!(DEFAULT_ACCEPTED_AXIOM_SOURCE_COMMITS
            .contains(&"44b9a25dfbe5a93e64120f53b65730828d1af91c"));
    }

    #[test]
    fn default_accept_list_includes_known_good_v2_corpus() {
        assert!(DEFAULT_ACCEPTED_AXIOM_SOURCE_COMMITS
            .contains(&"9a15d281ddcc2bcf36956fbe6d6c5736d8ce706a"));
        assert!(DEFAULT_ACCEPTED_AXIOM_SOURCE_COMMITS
            .contains(&"062d0d42ac5ef300fa3e04ef5b49b14864babcdd"));
    }

    #[test]
    fn default_accept_list_entries_are_well_formed_commits() {
        for sha in DEFAULT_ACCEPTED_AXIOM_SOURCE_COMMITS {
            assert!(
                is_commit_sha(sha),
                "default accept-list entry `{sha}` must be a 40-char lowercase-hex commit SHA",
            );
        }
    }

    #[test]
    fn freshness_predicate_admits_accepted_sha() {
        let accepted: Vec<String> = DEFAULT_ACCEPTED_AXIOM_SOURCE_COMMITS
            .iter()
            .map(|sha| (*sha).to_string())
            .collect();
        assert!(is_axiom_source_commit_fresh(
            "44b9a25dfbe5a93e64120f53b65730828d1af91c",
            &accepted
        ));
    }

    #[test]
    fn freshness_predicate_refuses_unknown_sha() {
        let accepted: Vec<String> = DEFAULT_ACCEPTED_AXIOM_SOURCE_COMMITS
            .iter()
            .map(|sha| (*sha).to_string())
            .collect();
        assert!(!is_axiom_source_commit_fresh(
            "0000000000000000000000000000000000000000",
            &accepted
        ));
        assert!(!is_axiom_source_commit_fresh(
            "1234567890abcdef1234567890abcdef12345678",
            &accepted
        ));
    }

    #[test]
    fn freshness_predicate_is_case_insensitive_on_candidate() {
        let accepted = vec!["44b9a25dfbe5a93e64120f53b65730828d1af91c".to_string()];
        assert!(is_axiom_source_commit_fresh(
            "44B9A25DFBE5A93E64120F53B65730828D1AF91C",
            &accepted
        ));
        assert!(is_axiom_source_commit_fresh(
            "44b9a25dfbe5a93e64120f53b65730828d1af91c",
            &accepted
        ));
    }

    #[test]
    fn freshness_predicate_empty_accept_list_refuses_everything() {
        let accepted: Vec<String> = Vec::new();
        assert!(!is_axiom_source_commit_fresh(
            "44b9a25dfbe5a93e64120f53b65730828d1af91c",
            &accepted
        ));
    }

    /// Verify env-var parsing semantics. We run this against a temporary
    /// override; the test restores the prior env state at the end so other
    /// tests in this module see the default.
    #[test]
    fn accepted_axiom_source_commits_env_var_replaces_default() {
        // Race-free: this test mutates a process-global env var, so it
        // must not be parallelised with other tests reading the same var.
        // No other test in this module reads `accepted_axiom_source_commits`
        // through the env-var path (the freshness-predicate tests build
        // their accept-list inline), so a single-test guard is sufficient.
        let prior = std::env::var(CORTEX_AXIOM_ACCEPTED_SOURCE_COMMITS_ENV).ok();

        std::env::set_var(
            CORTEX_AXIOM_ACCEPTED_SOURCE_COMMITS_ENV,
            "  AAAAaaaaAAAAaaaaAAAAaaaaAAAAaaaaAAAAaaaa , 1111111111111111111111111111111111111111  ,,",
        );
        let resolved = accepted_axiom_source_commits();
        assert_eq!(resolved.len(), 2);
        assert!(resolved.contains(&"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".to_string()));
        assert!(resolved.contains(&"1111111111111111111111111111111111111111".to_string()));
        // The ADR 0042 default pin MUST be excluded — env var REPLACES the
        // default, it does not extend it.
        assert!(!resolved.contains(&"44b9a25dfbe5a93e64120f53b65730828d1af91c".to_string()));

        // Restore prior state so neighbouring tests are unaffected.
        match prior {
            Some(value) => std::env::set_var(CORTEX_AXIOM_ACCEPTED_SOURCE_COMMITS_ENV, value),
            None => std::env::remove_var(CORTEX_AXIOM_ACCEPTED_SOURCE_COMMITS_ENV),
        }
    }
}