vela-protocol 0.915.1

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

use serde::{
    Deserialize, Serialize,
    de::{self, DeserializeOwned, DeserializeSeed, MapAccess, SeqAccess, Visitor},
};
use serde_json::{Map, Value};
use sha2::{Digest, Sha256};
use std::{collections::HashSet, fmt};

use crate::receipt_v1::{ExecutionBindingV1, is_full_sha256_root};

/// The three routing outcomes. `defer` is the safe default and carries the reason
/// the transition needs a named human; `deny` is a structural/authority/explicit
/// prohibition.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Outcome {
    Permit,
    Defer,
    Deny,
}

impl Outcome {
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Outcome::Permit => "permit",
            Outcome::Defer => "defer",
            Outcome::Deny => "deny",
        }
    }
}

/// The bounded, structured context a rule is evaluated against. Every field is
/// derived deterministically from the proposal + its assurance evidence + the
/// frontier state (see the host that builds it). The evaluator reads ONLY these
/// fields; it never makes a network call, reads wall-clock outside `now`, or
/// consults a mutable reputation score.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PolicyContext {
    /// The structural claim class (e.g. "sidon_lower_bound", "formal_theorem",
    /// "literature_finding", "metadata_repair", "governance").
    pub claim_class: String,
    /// Assurance level A0..A4 (0..4): A2 = one exact/formal check passed, A3 =
    /// independent corroboration, A4 = adversarial+semantic fidelity. Derived from
    /// the gate, NOT self-asserted.
    pub assurance_level: u8,
    /// Transition impact tier I0..I4 (0..4).
    pub impact_tier: u8,
    pub changed_findings: u32,
    pub downstream_dependents: u32,
    /// Does the proposal mutate claim LANGUAGE (vs attach exact evidence)?
    pub assertion_text_mutated: bool,
    pub target_contested: bool,
    pub governance_mutation: bool,
    /// Independence derived from failure-domain diversity (not self-declared).
    pub independence_satisfied: bool,
    /// MethodIntegrity::Sound on the matched attachments.
    pub method_integrity_sound: bool,
    /// The producer/delegate credential resolved and is unexpired/unrevoked.
    pub credential_valid: bool,
    /// The evaluator saw a field it does not recognize → never permit (monotonic).
    pub has_unknown_fields: bool,
    /// The originating receipt's honest replay classification (`exact` | `bounded`
    /// | `approximate` | `unavailable` | `unknown`; docs/RECEIPTS.md). Hosts set
    /// `unknown` explicitly for non-receipt transitions. A policy MAY require
    /// `exact` to auto-admit a serious claim class.
    pub replayability: String,
    /// Optional closed Receipt v1 execution identity. It is absent from
    /// historical contexts and ignored by policy v0.1; policy v0.2/v0.3 may
    /// require exact full-root matches before Permit.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub execution_binding: Option<ExecutionBindingV1>,
    /// Full root of the verified Receipt v1 producer identity binding. It is
    /// absent from historical contexts and populated only for policy v0.3 so
    /// v0.1/v0.2 decision certificates retain their exact context bytes.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub producer_credential_root: Option<String>,
}

impl Default for PolicyContext {
    fn default() -> Self {
        // The maximally-cautious context: nothing proven, everything that would
        // force a defer/deny set. A rule must positively clear each gate.
        PolicyContext {
            claim_class: String::new(),
            assurance_level: 0,
            impact_tier: 4,
            changed_findings: u32::MAX,
            downstream_dependents: u32::MAX,
            assertion_text_mutated: true,
            target_contested: true,
            governance_mutation: true,
            independence_satisfied: false,
            method_integrity_sound: false,
            credential_valid: false,
            has_unknown_fields: true,
            replayability: "unknown".to_string(),
            execution_binding: None,
            producer_credential_root: None,
        }
    }
}

impl PolicyContext {
    /// Canonical digest of the complete input surface consumed by the live
    /// policy language.
    ///
    /// Keep this helper beside [`PolicyContext`]: callers must not maintain a
    /// second hand-picked projection that can drift when the evaluator gains a
    /// field. The digest is evidence about which facts were evaluated; it is
    /// not an authority token and cannot turn a producer assertion into a
    /// verified fact.
    pub fn policy_language_digest(&self) -> Result<String, String> {
        crate::canonical::sha256_canonical(self).map(|digest| format!("sha256:{digest}"))
    }
}

/// The constraints a `permit` rule places on the transition. A `permit` fires only
/// if ALL hold; otherwise the rule does not match and routing falls through.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Constraints {
    pub max_changed_findings: u32,
    pub max_downstream_dependents: u32,
    /// Lowest assurance level (0..4) that satisfies the rule.
    pub required_assurance_min: u8,
    /// `false` (the default) forbids claim-language mutation in this lane.
    #[serde(default)]
    pub allow_semantic_text_change: bool,
    #[serde(default)]
    pub allow_contested: bool,
    #[serde(default)]
    pub allow_governance_mutation: bool,
    /// Require failure-domain-diverse independent verification.
    #[serde(default)]
    pub require_independence: bool,
    /// Require MethodIntegrity::Sound.
    #[serde(default)]
    pub require_method_integrity: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub allowed_packet_roots: Option<Vec<String>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub allowed_profile_roots: Option<Vec<String>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub allowed_verifier_capsule_roots: Option<Vec<String>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub allowed_result_contract_roots: Option<Vec<String>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub allowed_producer_credential_roots: Option<Vec<String>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub required_replayability: Option<String>,
}

impl Default for Constraints {
    fn default() -> Self {
        Constraints {
            max_changed_findings: 0,
            max_downstream_dependents: 0,
            required_assurance_min: 4,
            allow_semantic_text_change: false,
            allow_contested: false,
            allow_governance_mutation: false,
            require_independence: true,
            require_method_integrity: true,
            allowed_packet_roots: None,
            allowed_profile_roots: None,
            allowed_verifier_capsule_roots: None,
            allowed_result_contract_roots: None,
            allowed_producer_credential_roots: None,
            required_replayability: None,
        }
    }
}

/// One rule: an effect plus the claim classes and constraints it applies to.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PolicyRule {
    pub id: String,
    pub effect: Outcome,
    /// Claim classes this rule governs. Empty = applies to any class.
    #[serde(default)]
    pub claim_classes: Vec<String>,
    #[serde(default)]
    pub constraints: Constraints,
}

impl PolicyRule {
    fn applies_to_class(&self, class: &str) -> bool {
        self.claim_classes.is_empty() || self.claim_classes.iter().any(|c| c == class)
    }
}

/// The quorum that must have signed the policy for it to carry authority.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Quorum {
    pub threshold: u32,
    #[serde(default)]
    pub eligible_roles: Vec<String>,
}

/// A scoped, revocable, content-addressed acceptance policy. Humans sign THIS
/// (once); the evaluator applies it (many times). Shares signature/quorum/expiry/
/// revocation shape with registry governance; this governs ordinary scientific
/// state transitions, not owner rotation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AcceptancePolicy {
    pub schema: String,
    pub id: String,
    pub frontier_id: String,
    pub epoch: u32,
    pub issued_by: Vec<String>,
    pub quorum: Quorum,
    pub rules: Vec<PolicyRule>,
    /// MUST be `Defer` or `Deny` (a permit default would be a footgun).
    pub default: Outcome,
    /// RFC3339; the evaluator denies after this instant.
    pub expires_at: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub revocation_ref: Option<String>,
}

pub const ACCEPTANCE_POLICY_V0_1_SCHEMA: &str = "vela.acceptance_policy.v0.1";
pub const ACCEPTANCE_POLICY_V0_2_SCHEMA: &str = "vela.acceptance_policy.v0.2";
pub const ACCEPTANCE_POLICY_V0_3_SCHEMA: &str = "vela.acceptance_policy.v0.3";

/// The evaluator version, bound into every decision for replay.
pub const EVALUATOR_VERSION: &str = "vela-policy@0.1.0";
pub const EVALUATOR_VERSION_V0_2: &str = "vela-policy@0.2.0";
pub const EVALUATOR_VERSION_V0_3: &str = "vela-policy@0.3.0";

impl AcceptancePolicy {
    /// Content address of the policy's normative body (everything but `id`), so
    /// the id is reproducible and a tampered policy fails to verify. `vap_` prefix.
    #[must_use]
    pub fn content_address(&self) -> String {
        let mut body = self.clone();
        body.id.clear();
        // AcceptancePolicy contains only the canonical JSON data model. Keep
        // its content address on the same canonical bytes used by signatures;
        // never collapse a serialization failure to the hash of empty bytes.
        let bytes = crate::canonical::to_canonical_bytes(&body)
            .expect("AcceptancePolicy must serialize as canonical JSON");
        let mut h = Sha256::new();
        h.update(&bytes);
        format!("vap_{}", hex16(&h.finalize()))
    }

    /// True iff `id` matches the content address (tamper check).
    #[must_use]
    pub fn id_is_valid(&self) -> bool {
        self.id == self.content_address()
    }

    /// Is the policy expired at `now`? Both values are parsed as RFC3339
    /// instants before comparison, so equivalent offsets compare correctly.
    /// Malformed lifecycle data fails closed and is treated as expired.
    #[must_use]
    pub fn is_expired(&self, now_rfc3339: &str) -> bool {
        let Ok(expires_at) = chrono::DateTime::parse_from_rfc3339(&self.expires_at) else {
            return true;
        };
        let Ok(now) = chrono::DateTime::parse_from_rfc3339(now_rfc3339) else {
            return true;
        };
        now >= expires_at
    }
}

/// A pure, replayable routing decision.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Decision {
    pub outcome: Outcome,
    pub matched_rule_ids: Vec<String>,
    /// Machine-readable reason codes (always non-empty).
    pub reasons: Vec<String>,
    pub evaluator: String,
    pub policy_id: String,
}

/// The deterministic routing engine (memo Appendix C). Order of precedence:
/// 1. structural/authority DENY (expired/revoked policy, governance mutation
///    without an explicit governance-permit rule, invalid credential);
/// 2. any matching `deny` rule → DENY;
/// 3. escalation triggers (contested, semantic mutation, missing independence/
///    integrity where required, downstream over bound, unknown fields) → DEFER;
/// 4. a matching `permit` rule whose constraints all hold → PERMIT;
/// 5. otherwise the policy `default` (DEFER or DENY).
///
/// Monotonic on unknown: `has_unknown_fields` or an unrecognized class can only
/// push toward `defer`/`deny`.
#[must_use]
pub fn evaluate(policy: &AcceptancePolicy, ctx: &PolicyContext, now_rfc3339: &str) -> Decision {
    let evaluator = match policy.schema.as_str() {
        ACCEPTANCE_POLICY_V0_2_SCHEMA => EVALUATOR_VERSION_V0_2,
        ACCEPTANCE_POLICY_V0_3_SCHEMA => EVALUATOR_VERSION_V0_3,
        _ => EVALUATOR_VERSION,
    };
    let mk = |outcome: Outcome, rules: Vec<String>, reasons: Vec<String>| Decision {
        outcome,
        matched_rule_ids: rules,
        reasons: if reasons.is_empty() {
            vec!["default".to_string()]
        } else {
            reasons
        },
        evaluator: evaluator.to_string(),
        policy_id: policy.id.clone(),
    };

    // (0) Policy integrity + lifecycle: structural DENY.
    if !matches!(
        policy.schema.as_str(),
        ACCEPTANCE_POLICY_V0_1_SCHEMA
            | ACCEPTANCE_POLICY_V0_2_SCHEMA
            | ACCEPTANCE_POLICY_V0_3_SCHEMA
    ) {
        return mk(
            Outcome::Deny,
            vec![],
            vec!["policy_schema_unsupported".into()],
        );
    }
    if !policy.id_is_valid() {
        return mk(Outcome::Deny, vec![], vec!["policy_id_mismatch".into()]);
    }
    if policy.is_expired(now_rfc3339) {
        return mk(Outcome::Deny, vec![], vec!["policy_expired".into()]);
    }
    if policy.revocation_ref.is_some() {
        return mk(Outcome::Deny, vec![], vec!["policy_revoked".into()]);
    }
    if !matches!(policy.default, Outcome::Defer | Outcome::Deny) {
        // A permit default is rejected at evaluation time, defense in depth.
        return mk(Outcome::Deny, vec![], vec!["illegal_permit_default".into()]);
    }
    if let Some(reason) = binding_policy_error(policy) {
        return mk(Outcome::Deny, vec![], vec![reason]);
    }

    // (1) Explicit DENY rules win over everything below.
    for r in &policy.rules {
        if r.effect == Outcome::Deny && r.applies_to_class(&ctx.claim_class) {
            return mk(
                Outcome::Deny,
                vec![r.id.clone()],
                vec!["explicit_deny_rule".into()],
            );
        }
    }

    // (2) Find a permit rule for this class; if it matches but constraints fail,
    // that is an escalation (defer), not a silent denial.
    let mut escalations: Vec<String> = Vec::new();
    for r in &policy.rules {
        if r.effect != Outcome::Permit || !r.applies_to_class(&ctx.claim_class) {
            continue;
        }
        let c = &r.constraints;
        let mut blocked: Vec<String> = Vec::new();

        // Monotonic-on-unknown + the universal escalation triggers.
        if ctx.has_unknown_fields {
            blocked.push("unknown_fields".into());
        }
        if policy.schema == ACCEPTANCE_POLICY_V0_3_SCHEMA {
            match ctx.producer_credential_root.as_ref() {
                Some(root)
                    if c.allowed_producer_credential_roots
                        .as_ref()
                        .is_some_and(|roots| roots.iter().any(|allowed| allowed == root)) => {}
                Some(_) => blocked.push("producer_credential_root_not_allowed".into()),
                None => blocked.push("producer_credential_root_missing".into()),
            }
        } else if !ctx.credential_valid {
            blocked.push("credential_invalid".into());
        }
        if ctx.governance_mutation && !c.allow_governance_mutation {
            blocked.push("governance_mutation".into());
        }
        if ctx.target_contested && !c.allow_contested {
            blocked.push("target_contested".into());
        }
        if ctx.assertion_text_mutated && !c.allow_semantic_text_change {
            blocked.push("semantic_text_change".into());
        }
        if ctx.assurance_level < c.required_assurance_min {
            blocked.push(format!(
                "assurance_below_min({}<{})",
                ctx.assurance_level, c.required_assurance_min
            ));
        }
        if ctx.changed_findings > c.max_changed_findings {
            blocked.push("changed_findings_over_bound".into());
        }
        if ctx.downstream_dependents > c.max_downstream_dependents {
            blocked.push("downstream_over_bound".into());
        }
        if c.require_independence && !ctx.independence_satisfied {
            blocked.push("independence_unsatisfied".into());
        }
        if c.require_method_integrity && !ctx.method_integrity_sound {
            blocked.push("method_integrity_unattested".into());
        }
        if matches!(
            policy.schema.as_str(),
            ACCEPTANCE_POLICY_V0_2_SCHEMA | ACCEPTANCE_POLICY_V0_3_SCHEMA
        ) {
            let Some(binding) = ctx.execution_binding.as_ref() else {
                blocked.push("execution_binding_missing".into());
                escalations.extend(
                    blocked
                        .into_iter()
                        .map(|reason| format!("{}:{reason}", r.id)),
                );
                continue;
            };
            if binding.validate().is_err() {
                blocked.push("execution_binding_invalid".into());
                escalations.extend(
                    blocked
                        .into_iter()
                        .map(|reason| format!("{}:{reason}", r.id)),
                );
                continue;
            }
            for (field, allowlist, actual) in [
                ("packet_root", &c.allowed_packet_roots, &binding.packet_root),
                (
                    "profile_root",
                    &c.allowed_profile_roots,
                    &binding.profile_root,
                ),
                (
                    "verifier_capsule_root",
                    &c.allowed_verifier_capsule_roots,
                    &binding.verifier_capsule_root,
                ),
                (
                    "result_contract_root",
                    &c.allowed_result_contract_roots,
                    &binding.result_contract_root,
                ),
            ] {
                if !allowlist
                    .as_ref()
                    .is_some_and(|roots| roots.iter().any(|root| root == actual))
                {
                    blocked.push(format!("{field}_not_allowed"));
                }
            }
            if c.required_replayability.as_deref() != Some(ctx.replayability.as_str()) {
                blocked.push("replayability_not_allowed".into());
            }
        }

        if blocked.is_empty() {
            return mk(
                Outcome::Permit,
                vec![r.id.clone()],
                vec!["all_constraints_satisfied".into()],
            );
        }
        // This permit rule matched the class but is blocked → remember why; a
        // matched-but-blocked permit rule means the item is plausibly valid but
        // needs human judgment, so we DEFER (not deny).
        escalations.extend(blocked.into_iter().map(|b| format!("{}:{}", r.id, b)));
    }

    if !escalations.is_empty() {
        return mk(Outcome::Defer, vec![], escalations);
    }

    // (3) No deny, no matching permit → the policy default.
    mk(
        policy.default,
        vec![],
        vec![format!("default_{}", policy.default.as_str())],
    )
}

fn binding_policy_error(policy: &AcceptancePolicy) -> Option<String> {
    for rule in policy
        .rules
        .iter()
        .filter(|rule| rule.effect == Outcome::Permit)
    {
        let constraints = &rule.constraints;
        let v2_fields_present = constraints.allowed_packet_roots.is_some()
            || constraints.allowed_profile_roots.is_some()
            || constraints.allowed_verifier_capsule_roots.is_some()
            || constraints.allowed_result_contract_roots.is_some()
            || constraints.required_replayability.is_some();
        let v3_fields_present = constraints.allowed_producer_credential_roots.is_some();
        if policy.schema == ACCEPTANCE_POLICY_V0_1_SCHEMA {
            if v2_fields_present || v3_fields_present {
                return Some("policy_v0_2_constraints_under_v0_1".to_string());
            }
            continue;
        }
        if policy.schema == ACCEPTANCE_POLICY_V0_2_SCHEMA && v3_fields_present {
            return Some("policy_v0_3_constraints_under_v0_2".to_string());
        }
        if !matches!(
            policy.schema.as_str(),
            ACCEPTANCE_POLICY_V0_2_SCHEMA | ACCEPTANCE_POLICY_V0_3_SCHEMA
        ) {
            continue;
        }
        for (field, roots) in [
            ("packet", &constraints.allowed_packet_roots),
            ("profile", &constraints.allowed_profile_roots),
            (
                "verifier_capsule",
                &constraints.allowed_verifier_capsule_roots,
            ),
            (
                "result_contract",
                &constraints.allowed_result_contract_roots,
            ),
        ] {
            let Some(roots) = roots.as_ref() else {
                return Some(format!("policy_{field}_allowlist_missing"));
            };
            if roots.is_empty() || roots.iter().any(|root| !is_full_sha256_root(root)) {
                return Some(format!("policy_{field}_allowlist_invalid"));
            }
        }
        if constraints.required_replayability.as_deref() != Some("exact") {
            return Some("policy_exact_replayability_required".to_string());
        }
        if policy.schema == ACCEPTANCE_POLICY_V0_3_SCHEMA {
            let Some(roots) = constraints.allowed_producer_credential_roots.as_ref() else {
                return Some("policy_producer_credential_allowlist_missing".to_string());
            };
            if roots.is_empty() || roots.iter().any(|root| !is_full_sha256_root(root)) {
                return Some("policy_producer_credential_allowlist_invalid".to_string());
            }
            let unique = roots.iter().collect::<HashSet<_>>();
            if unique.len() != roots.len() {
                return Some("policy_producer_credential_allowlist_duplicate".to_string());
            }
            if roots.len() != 1 {
                return Some("policy_producer_credential_allowlist_invalid".to_string());
            }
            for (_, allowed) in [
                ("packet", &constraints.allowed_packet_roots),
                ("profile", &constraints.allowed_profile_roots),
                (
                    "verifier_capsule",
                    &constraints.allowed_verifier_capsule_roots,
                ),
                (
                    "result_contract",
                    &constraints.allowed_result_contract_roots,
                ),
            ] {
                let roots = allowed.as_ref().expect("v0.3 exact roots validated above");
                if roots.iter().collect::<HashSet<_>>().len() != roots.len() {
                    return Some("policy_exact_allowlist_duplicate".to_string());
                }
            }
        }
    }
    None
}

/// First 16 bytes of a digest as hex (mirrors the substrate's short-id style).
fn hex16(digest: &[u8]) -> String {
    digest.iter().take(16).map(|b| format!("{b:02x}")).collect()
}

/// How the transition was authorized. `PolicyDelegation` = a human-signed policy
/// permitted it (the engine executed); `DirectHuman` = a person signed this item;
/// `Quorum` = a governance quorum signed. Never collapse these into one "signed".
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AuthorityMode {
    PolicyDelegation,
    DirectHuman,
    Quorum,
}

/// The portable, content-addressed receipt of one acceptance decision — produced
/// by the engine, not performed by a human. It binds the proposal, the exact state
/// roots, the policy + matched rules, the authority chain, the assurance profile,
/// and (once recorded) the event + log inclusion proof, so any relying party can
/// REPLAY the decision. Reading it requires no signature; only a durable
/// endorsement/commitment does (the memo's separation of receipt from endorsement).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DecisionCertificate {
    #[serde(default = "default_cert_schema")]
    pub schema: String,
    pub id: String,
    pub frontier_id: String,
    pub proposal_id: String,
    pub state_root_before: String,
    pub state_root_after: String,
    pub outcome: Outcome,
    pub policy_id: String,
    pub rule_ids: Vec<String>,
    pub evaluator: String,
    pub authority_mode: AuthorityMode,
    /// The human(s)/quorum whose signed policy authorized this (for PolicyDelegation).
    pub human_authorizers: Vec<String>,
    /// The service/agent that executed under the policy.
    pub executor: String,
    /// The named assurance profile the evidence cleared (e.g.
    /// "exact_construction_dual_check_v1"); the policy NEVER manufactures this.
    pub assurance_profile: String,
    pub assurance_level: u8,
    pub claim_digest: String,
    pub impact_tier: u8,
    pub reasons: Vec<String>,
    /// Selected for post-accept audit (the calibrated-sample rollout).
    pub audit_required: bool,
}

fn default_cert_schema() -> String {
    "vela.decision_certificate.v0.1".to_string()
}

impl DecisionCertificate {
    /// Build a certificate from a decision + its bindings. `vdc_` prefix; the id is
    /// the content address of everything but `id`, so it is reproducible and a
    /// tampered certificate fails `id_is_valid`.
    #[must_use]
    #[allow(clippy::too_many_arguments)]
    pub fn build(
        decision: &Decision,
        frontier_id: &str,
        proposal_id: &str,
        state_root_before: &str,
        state_root_after: &str,
        authority_mode: AuthorityMode,
        human_authorizers: Vec<String>,
        executor: &str,
        assurance_profile: &str,
        assurance_level: u8,
        claim_digest: &str,
        impact_tier: u8,
        audit_required: bool,
    ) -> Self {
        let mut c = DecisionCertificate {
            schema: default_cert_schema(),
            id: String::new(),
            frontier_id: frontier_id.to_string(),
            proposal_id: proposal_id.to_string(),
            state_root_before: state_root_before.to_string(),
            state_root_after: state_root_after.to_string(),
            outcome: decision.outcome,
            policy_id: decision.policy_id.clone(),
            rule_ids: decision.matched_rule_ids.clone(),
            evaluator: decision.evaluator.clone(),
            authority_mode,
            human_authorizers,
            executor: executor.to_string(),
            assurance_profile: assurance_profile.to_string(),
            assurance_level,
            claim_digest: claim_digest.to_string(),
            impact_tier,
            reasons: decision.reasons.clone(),
            audit_required,
        };
        c.id = c.content_address();
        c
    }

    #[must_use]
    pub fn content_address(&self) -> String {
        let mut probe = self.clone();
        probe.id = String::new();
        let bytes = serde_json::to_vec(&probe).unwrap_or_default();
        let mut h = Sha256::new();
        h.update(&bytes);
        format!("vdc_{}", hex16(&h.finalize()))
    }

    #[must_use]
    pub fn id_is_valid(&self) -> bool {
        self.id == self.content_address()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ed25519_dalek::Signer;
    use serde_json::json;
    use tempfile::TempDir;

    fn exact_sidon_policy() -> AcceptancePolicy {
        let mut p = AcceptancePolicy {
            schema: ACCEPTANCE_POLICY_V0_1_SCHEMA.to_string(),
            id: String::new(),
            frontier_id: "vfr_test".into(),
            epoch: 1,
            issued_by: vec!["reviewer:will".into()],
            quorum: Quorum {
                threshold: 1,
                eligible_roles: vec!["steward".into()],
            },
            rules: vec![PolicyRule {
                id: "sidon-exact-auto-v1".into(),
                effect: Outcome::Permit,
                claim_classes: vec!["sidon_lower_bound".into()],
                constraints: Constraints {
                    max_changed_findings: 1,
                    max_downstream_dependents: 5,
                    required_assurance_min: 3,
                    allow_semantic_text_change: false,
                    allow_contested: false,
                    allow_governance_mutation: false,
                    require_independence: true,
                    require_method_integrity: true,
                    ..Constraints::default()
                },
            }],
            default: Outcome::Defer,
            expires_at: "2099-12-31T23:59:59Z".into(),
            revocation_ref: None,
        };
        p.id = p.content_address();
        p
    }

    fn clean_exact_ctx() -> PolicyContext {
        PolicyContext {
            claim_class: "sidon_lower_bound".into(),
            assurance_level: 3,
            impact_tier: 2,
            changed_findings: 1,
            downstream_dependents: 0,
            assertion_text_mutated: false,
            target_contested: false,
            governance_mutation: false,
            independence_satisfied: true,
            method_integrity_sound: true,
            credential_valid: true,
            has_unknown_fields: false,
            replayability: "unknown".to_string(),
            execution_binding: None,
            producer_credential_root: None,
        }
    }

    const NOW: &str = "2026-06-23T00:00:00Z";
    const SIGNED_AT: &str = "2026-06-22T00:00:00Z";

    fn signed_policy_bytes(policy: &AcceptancePolicy, signed_at: &str) -> (Vec<u8>, Vec<u8>) {
        let key = ed25519_dalek::SigningKey::from_bytes(&[11u8; 32]);
        let preimage = policy_signature_preimage(policy, signed_at).unwrap();
        let signature = key.sign(&preimage);
        let record = PolicySignatureRecord {
            policy_id: policy.id.clone(),
            signer_pubkey_hex: hex::encode(key.verifying_key().to_bytes()),
            signature: hex::encode(signature.to_bytes()),
            signed_at: signed_at.to_string(),
        };
        (
            serde_json::to_vec_pretty(policy).unwrap(),
            serde_json::to_vec_pretty(&record).unwrap(),
        )
    }

    fn write_active_pair(policy_bytes: &[u8], signature_bytes: &[u8]) -> TempDir {
        let temp = TempDir::new().unwrap();
        let directory = temp.path().join(".vela/policies");
        std::fs::create_dir_all(&directory).unwrap();
        std::fs::write(directory.join("active.json"), policy_bytes).unwrap();
        std::fs::write(directory.join("active.sig.json"), signature_bytes).unwrap();
        temp
    }

    #[test]
    fn exact_clean_witness_permits() {
        let d = evaluate(&exact_sidon_policy(), &clean_exact_ctx(), NOW);
        assert_eq!(d.outcome, Outcome::Permit);
        assert_eq!(d.matched_rule_ids, vec!["sidon-exact-auto-v1"]);
    }

    #[test]
    fn contested_target_escalates_to_defer() {
        let mut ctx = clean_exact_ctx();
        ctx.target_contested = true;
        let d = evaluate(&exact_sidon_policy(), &ctx, NOW);
        assert_eq!(d.outcome, Outcome::Defer);
        assert!(d.reasons.iter().any(|r| r.contains("target_contested")));
    }

    #[test]
    fn semantic_text_mutation_escalates_to_defer() {
        let mut ctx = clean_exact_ctx();
        ctx.assertion_text_mutated = true;
        assert_eq!(
            evaluate(&exact_sidon_policy(), &ctx, NOW).outcome,
            Outcome::Defer
        );
    }

    #[test]
    fn low_assurance_escalates_to_defer() {
        let mut ctx = clean_exact_ctx();
        ctx.assurance_level = 2; // below the rule's min of 3
        let d = evaluate(&exact_sidon_policy(), &ctx, NOW);
        assert_eq!(d.outcome, Outcome::Defer);
        assert!(d.reasons.iter().any(|r| r.contains("assurance_below_min")));
    }

    #[test]
    fn monotonic_on_unknown_fields() {
        let mut ctx = clean_exact_ctx();
        ctx.has_unknown_fields = true;
        assert_eq!(
            evaluate(&exact_sidon_policy(), &ctx, NOW).outcome,
            Outcome::Defer
        );
    }

    #[test]
    fn unknown_claim_class_falls_through_to_default_defer() {
        let mut ctx = clean_exact_ctx();
        ctx.claim_class = "literature_finding".into(); // no rule covers it
        let d = evaluate(&exact_sidon_policy(), &ctx, NOW);
        assert_eq!(d.outcome, Outcome::Defer);
        assert!(d.reasons.iter().any(|r| r.contains("default")));
    }

    #[test]
    fn expired_policy_denies() {
        let p = exact_sidon_policy();
        let d = evaluate(&p, &clean_exact_ctx(), "2100-01-01T00:00:00Z");
        assert_eq!(d.outcome, Outcome::Deny);
        assert!(d.reasons.iter().any(|r| r == "policy_expired"));
    }

    #[test]
    fn expiry_compares_rfc3339_instants_and_malformed_values_fail_closed() {
        let mut policy = exact_sidon_policy();
        policy.expires_at = "2026-06-23T00:00:00Z".to_string();

        assert!(!policy.is_expired("2026-06-23T01:59:59+02:00"));
        assert!(policy.is_expired("2026-06-22T20:00:00-04:00"));
        assert!(policy.is_expired("not-rfc3339"));

        policy.id = policy.content_address();
        let decision = evaluate(&policy, &clean_exact_ctx(), "not-rfc3339");
        assert_eq!(decision.outcome, Outcome::Deny);
        assert_eq!(decision.reasons, vec!["policy_expired"]);

        policy.expires_at = "also-not-rfc3339".to_string();
        assert!(policy.is_expired(NOW));
    }

    #[test]
    fn unsupported_policy_schema_denies_even_with_a_matching_id() {
        let mut policy = exact_sidon_policy();
        policy.schema = "vela.acceptance_policy.v999".to_string();
        policy.id = policy.content_address();
        let decision = evaluate(&policy, &clean_exact_ctx(), NOW);
        assert_eq!(decision.outcome, Outcome::Deny);
        assert_eq!(decision.reasons, vec!["policy_schema_unsupported"]);
    }

    #[test]
    fn policy_scoped_producer_credential_is_exact_and_narrower_than_registry_status() {
        let root = |digit: char| format!("sha256:{}", digit.to_string().repeat(64));
        let binding = ExecutionBindingV1 {
            schema: crate::receipt_v1::EXECUTION_BINDING_SCHEMA.to_string(),
            packet_root: root('1'),
            profile_root: root('2'),
            verifier_capsule_root: root('3'),
            result_contract_root: root('4'),
        };
        let credential = root('5');
        let mut policy = exact_sidon_policy();
        policy.schema = ACCEPTANCE_POLICY_V0_3_SCHEMA.to_string();
        let constraints = &mut policy.rules[0].constraints;
        constraints.allowed_packet_roots = Some(vec![binding.packet_root.clone()]);
        constraints.allowed_profile_roots = Some(vec![binding.profile_root.clone()]);
        constraints.allowed_verifier_capsule_roots =
            Some(vec![binding.verifier_capsule_root.clone()]);
        constraints.allowed_result_contract_roots =
            Some(vec![binding.result_contract_root.clone()]);
        constraints.allowed_producer_credential_roots = Some(vec![credential.clone()]);
        constraints.required_replayability = Some("exact".to_string());
        policy.id = policy.content_address();

        let mut context = clean_exact_ctx();
        context.credential_valid = false;
        context.replayability = "exact".to_string();
        context.execution_binding = Some(binding);
        context.producer_credential_root = Some(credential.clone());
        let decision = evaluate(&policy, &context, NOW);
        assert_eq!(decision.outcome, Outcome::Permit, "{decision:?}");
        assert_eq!(decision.evaluator, EVALUATOR_VERSION_V0_3);

        context.credential_valid = true;
        context.producer_credential_root = Some(root('6'));
        let wrong = evaluate(&policy, &context, NOW);
        assert_eq!(wrong.outcome, Outcome::Defer);
        assert!(
            wrong
                .reasons
                .iter()
                .any(|reason| reason.ends_with("producer_credential_root_not_allowed"))
        );

        context.producer_credential_root = None;
        let missing = evaluate(&policy, &context, NOW);
        assert_eq!(missing.outcome, Outcome::Defer);
        assert!(
            missing
                .reasons
                .iter()
                .any(|reason| reason.ends_with("producer_credential_root_missing"))
        );

        let mut duplicate = policy.clone();
        duplicate.rules[0]
            .constraints
            .allowed_producer_credential_roots = Some(vec![credential.clone(), credential]);
        duplicate.id = duplicate.content_address();
        let denied = evaluate(&duplicate, &context, NOW);
        assert_eq!(denied.outcome, Outcome::Deny);
        assert_eq!(
            denied.reasons,
            vec!["policy_producer_credential_allowlist_duplicate"]
        );
    }

    #[test]
    fn tampered_policy_id_denies() {
        let mut p = exact_sidon_policy();
        p.rules[0].constraints.required_assurance_min = 0; // change body, keep old id
        let d = evaluate(&p, &clean_exact_ctx(), NOW);
        assert_eq!(d.outcome, Outcome::Deny);
        assert!(d.reasons.iter().any(|r| r == "policy_id_mismatch"));
    }

    #[test]
    fn revoked_policy_denies() {
        let mut p = exact_sidon_policy();
        p.revocation_ref = Some("vrv_x".into());
        p.id = p.content_address();
        assert_eq!(evaluate(&p, &clean_exact_ctx(), NOW).outcome, Outcome::Deny);
    }

    #[test]
    fn governance_mutation_without_permission_escalates() {
        let mut ctx = clean_exact_ctx();
        ctx.governance_mutation = true;
        let d = evaluate(&exact_sidon_policy(), &ctx, NOW);
        assert_eq!(d.outcome, Outcome::Defer);
        assert!(d.reasons.iter().any(|r| r.contains("governance_mutation")));
    }

    #[test]
    fn evaluation_is_deterministic() {
        let p = exact_sidon_policy();
        let ctx = clean_exact_ctx();
        assert_eq!(evaluate(&p, &ctx, NOW), evaluate(&p, &ctx, NOW));
    }

    #[test]
    fn policy_context_digest_is_canonical_and_binds_every_live_field() {
        let baseline = clean_exact_ctx();
        let digest = baseline.policy_language_digest().unwrap();
        assert!(digest.starts_with("sha256:"));
        assert_eq!(digest, baseline.policy_language_digest().unwrap());

        let mut variants = Vec::new();
        let mut value = baseline.clone();
        value.claim_class.push_str("_changed");
        variants.push(value);
        let mut value = baseline.clone();
        value.assurance_level += 1;
        variants.push(value);
        let mut value = baseline.clone();
        value.impact_tier += 1;
        variants.push(value);
        let mut value = baseline.clone();
        value.changed_findings += 1;
        variants.push(value);
        let mut value = baseline.clone();
        value.downstream_dependents += 1;
        variants.push(value);
        let mut value = baseline.clone();
        value.assertion_text_mutated = !value.assertion_text_mutated;
        variants.push(value);
        let mut value = baseline.clone();
        value.target_contested = !value.target_contested;
        variants.push(value);
        let mut value = baseline.clone();
        value.governance_mutation = !value.governance_mutation;
        variants.push(value);
        let mut value = baseline.clone();
        value.independence_satisfied = !value.independence_satisfied;
        variants.push(value);
        let mut value = baseline.clone();
        value.method_integrity_sound = !value.method_integrity_sound;
        variants.push(value);
        let mut value = baseline.clone();
        value.credential_valid = !value.credential_valid;
        variants.push(value);
        let mut value = baseline.clone();
        value.has_unknown_fields = !value.has_unknown_fields;
        variants.push(value);
        let mut value = baseline.clone();
        value.replayability = "exact".to_string();
        variants.push(value);
        let mut value = baseline.clone();
        value.execution_binding = Some(ExecutionBindingV1 {
            schema: crate::receipt_v1::EXECUTION_BINDING_SCHEMA.to_string(),
            packet_root: format!("sha256:{}", "1".repeat(64)),
            profile_root: format!("sha256:{}", "2".repeat(64)),
            verifier_capsule_root: format!("sha256:{}", "3".repeat(64)),
            result_contract_root: format!("sha256:{}", "4".repeat(64)),
        });
        variants.push(value);
        let mut value = baseline.clone();
        value.producer_credential_root = Some(format!("sha256:{}", "5".repeat(64)));
        variants.push(value);

        for variant in variants {
            assert_ne!(digest, variant.policy_language_digest().unwrap());
        }
    }

    #[test]
    fn policy_context_wire_requires_replayability() {
        let mut value = serde_json::to_value(clean_exact_ctx()).unwrap();
        value.as_object_mut().unwrap().remove("replayability");
        assert!(serde_json::from_value::<PolicyContext>(value).is_err());
    }

    #[test]
    fn content_address_is_stable_and_prefixed() {
        let p = exact_sidon_policy();
        assert!(p.id.starts_with("vap_"));
        assert!(p.id_is_valid());

        let mut body = p.clone();
        body.id.clear();
        let canonical = crate::canonical::to_canonical_bytes(&body).unwrap();
        let mut digest = Sha256::new();
        digest.update(canonical);
        assert_eq!(p.id, format!("vap_{}", hex16(&digest.finalize())));
    }

    #[test]
    fn bounded_closed_parser_accepts_only_the_supported_policy_shape() {
        let policy = exact_sidon_policy();
        let (policy_bytes, signature_bytes) = signed_policy_bytes(&policy, SIGNED_AT);
        let verified = verify_policy_signature_bytes(
            &policy_bytes,
            &signature_bytes,
            Some(&policy.id),
            "test policy",
        )
        .unwrap();
        assert_eq!(verified.policy, policy);

        let mut cases = Vec::new();
        let mut value = serde_json::to_value(&policy).unwrap();
        value["unexpected"] = json!(true);
        cases.push(value);
        let mut value = serde_json::to_value(&policy).unwrap();
        value["quorum"]["unexpected"] = json!(true);
        cases.push(value);
        let mut value = serde_json::to_value(&policy).unwrap();
        value["rules"][0]["unexpected"] = json!(true);
        cases.push(value);
        let mut value = serde_json::to_value(&policy).unwrap();
        value["rules"][0]["constraints"]["unexpected"] = json!(true);
        cases.push(value);

        for value in cases {
            let error = verify_policy_signature_bytes(
                &serde_json::to_vec(&value).unwrap(),
                &signature_bytes,
                None,
                "test policy",
            )
            .unwrap_err();
            assert!(error.contains("unknown field `unexpected`"), "{error}");
        }

        let mut missing_schema = serde_json::to_value(&policy).unwrap();
        missing_schema.as_object_mut().unwrap().remove("schema");
        let error = verify_policy_signature_bytes(
            &serde_json::to_vec(&missing_schema).unwrap(),
            &signature_bytes,
            None,
            "test policy",
        )
        .unwrap_err();
        assert!(error.contains("missing field `schema`"), "{error}");

        let mut unsupported_schema = serde_json::to_value(&policy).unwrap();
        unsupported_schema["schema"] = json!("vela.acceptance_policy.v999");
        let error = verify_policy_signature_bytes(
            &serde_json::to_vec(&unsupported_schema).unwrap(),
            &signature_bytes,
            None,
            "test policy",
        )
        .unwrap_err();
        assert!(error.contains("schema must be"), "{error}");

        let mut bad_expiry = serde_json::to_value(&policy).unwrap();
        bad_expiry["expires_at"] = json!("not-rfc3339");
        let error = verify_policy_signature_bytes(
            &serde_json::to_vec(&bad_expiry).unwrap(),
            &signature_bytes,
            None,
            "test policy",
        )
        .unwrap_err();
        assert!(error.contains("expires_at must be RFC3339"), "{error}");

        let mut signature_value: Value = serde_json::from_slice(&signature_bytes).unwrap();
        signature_value["unexpected"] = json!(true);
        let error = verify_policy_signature_bytes(
            &policy_bytes,
            &serde_json::to_vec(&signature_value).unwrap(),
            None,
            "test policy",
        )
        .unwrap_err();
        assert!(error.contains("unknown field `unexpected`"), "{error}");
    }

    #[test]
    fn duplicate_names_are_rejected_in_policy_and_signature_envelopes() {
        let policy = exact_sidon_policy();
        let (policy_bytes, signature_bytes) = signed_policy_bytes(&policy, SIGNED_AT);
        let policy_json = String::from_utf8(policy_bytes).unwrap();
        for malicious in [
            policy_json.replacen("\"epoch\": 1", "\"epoch\": 1,\n  \"epoch\": 2", 1),
            policy_json.replacen("\"epoch\": 1", "\"epoch\": 1,\n  \"e\\u0070och\": 2", 1),
            policy_json.replacen(
                "\"threshold\": 1",
                "\"threshold\": 1,\n    \"threshold\": 2",
                1,
            ),
        ] {
            let error = verify_policy_signature_bytes(
                malicious.as_bytes(),
                &signature_bytes,
                None,
                "test policy",
            )
            .unwrap_err();
            assert!(error.contains("duplicate object name"), "{error}");
        }

        let signature_json = String::from_utf8(signature_bytes).unwrap();
        let malicious = signature_json.replacen(
            &format!("\"signed_at\": \"{SIGNED_AT}\""),
            &format!("\"signed_at\": \"{SIGNED_AT}\",\n  \"signed_at\": \"2099-01-01T00:00:00Z\""),
            1,
        );
        let policy_bytes = serde_json::to_vec(&policy).unwrap();
        let error =
            verify_policy_signature_bytes(&policy_bytes, malicious.as_bytes(), None, "test policy")
                .unwrap_err();
        assert!(error.contains("duplicate object name"), "{error}");
    }

    #[test]
    fn parser_budgets_reject_oversized_and_excessively_deep_governance_json() {
        let policy = exact_sidon_policy();
        let (_, signature_bytes) = signed_policy_bytes(&policy, SIGNED_AT);
        let oversized = vec![b' '; POLICY_JSON_MAX_BYTES + 1];
        let error =
            verify_policy_signature_bytes(&oversized, &signature_bytes, None, "test policy")
                .unwrap_err();
        assert!(error.contains("limit is 65536 bytes"), "{error}");

        let deep = format!(
            "{}0{}",
            "{\"nested\":".repeat(GOVERNANCE_JSON_MAX_DEPTH + 1),
            "}".repeat(GOVERNANCE_JSON_MAX_DEPTH + 1)
        );
        let error =
            verify_policy_signature_bytes(deep.as_bytes(), &signature_bytes, None, "test policy")
                .unwrap_err();
        assert!(error.contains("JSON depth"), "{error}");

        let policy_bytes = serde_json::to_vec(&policy).unwrap();
        let oversized_signature = vec![b' '; POLICY_SIGNATURE_JSON_MAX_BYTES + 1];
        let error =
            verify_policy_signature_bytes(&policy_bytes, &oversized_signature, None, "test policy")
                .unwrap_err();
        assert!(error.contains("limit is 8192 bytes"), "{error}");
    }

    #[test]
    fn signatures_require_supported_policy_and_bound_signed_at() {
        let policy = exact_sidon_policy();
        let (policy_bytes, signature_bytes) = signed_policy_bytes(&policy, SIGNED_AT);

        let mut rewritten: PolicySignatureRecord =
            serde_json::from_slice(&signature_bytes).unwrap();
        rewritten.signed_at = "2026-06-22T00:00:01Z".to_string();
        let error = verify_policy_signature_bytes(
            &policy_bytes,
            &serde_json::to_vec(&rewritten).unwrap(),
            None,
            "test policy",
        )
        .unwrap_err();
        assert!(error.contains("signature does not verify"), "{error}");

        rewritten.signed_at = "not-rfc3339".to_string();
        let error = verify_policy_signature_bytes(
            &policy_bytes,
            &serde_json::to_vec(&rewritten).unwrap(),
            None,
            "test policy",
        )
        .unwrap_err();
        assert!(error.contains("signed_at must be RFC3339"), "{error}");
        assert!(policy_signature_preimage(&policy, "not-rfc3339").is_err());

        let key = ed25519_dalek::SigningKey::from_bytes(&[11u8; 32]);
        let unbound_signature = key.sign(&crate::canonical::to_canonical_bytes(&policy).unwrap());
        let unbound_record = PolicySignatureRecord {
            policy_id: policy.id.clone(),
            signer_pubkey_hex: hex::encode(key.verifying_key().to_bytes()),
            signature: hex::encode(unbound_signature.to_bytes()),
            signed_at: SIGNED_AT.to_string(),
        };
        let error = verify_policy_signature_bytes(
            &policy_bytes,
            &serde_json::to_vec(&unbound_record).unwrap(),
            None,
            "test policy",
        )
        .unwrap_err();
        assert!(error.contains("signature does not verify"), "{error}");

        let mut changed = policy.clone();
        changed.expires_at = "2098-12-31T23:59:59Z".to_string();
        changed.id = changed.content_address();
        let mut stale_record: PolicySignatureRecord =
            serde_json::from_slice(&signature_bytes).unwrap();
        stale_record.policy_id = changed.id.clone();
        let error = verify_policy_signature_bytes(
            &serde_json::to_vec(&changed).unwrap(),
            &serde_json::to_vec(&stale_record).unwrap(),
            None,
            "test policy",
        )
        .unwrap_err();
        assert!(error.contains("signature does not verify"), "{error}");
    }

    #[test]
    fn active_pair_accepts_only_current_id_and_bound_signature() {
        let policy = exact_sidon_policy();
        let (policy_bytes, signature_bytes) = signed_policy_bytes(&policy, SIGNED_AT);

        let mut wrong_id = policy.clone();
        wrong_id.id = "vap_00000000000000000000000000000000".to_string();
        let temp = write_active_pair(&serde_json::to_vec(&wrong_id).unwrap(), &signature_bytes);
        let error = load_active_policy_snapshot(temp.path())
            .expect_err("a non-current content address must be a strict error");
        assert!(error.contains("id does not re-derive"), "{error}");

        let key = ed25519_dalek::SigningKey::from_bytes(&[11u8; 32]);
        let unbound = key.sign(&crate::canonical::to_canonical_bytes(&policy).unwrap());
        let record = PolicySignatureRecord {
            policy_id: policy.id,
            signer_pubkey_hex: hex::encode(key.verifying_key().to_bytes()),
            signature: hex::encode(unbound.to_bytes()),
            signed_at: SIGNED_AT.to_string(),
        };
        let temp = write_active_pair(&policy_bytes, &serde_json::to_vec(&record).unwrap());
        let error = load_active_policy_snapshot(temp.path())
            .expect_err("an unbound signature must be a strict error");
        assert!(error.contains("signature does not verify"), "{error}");
    }

    #[test]
    fn malformed_or_stale_unsigned_policy_is_broken_not_staged() {
        let temp = TempDir::new().unwrap();
        let directory = temp.path().join(".vela/policies");
        std::fs::create_dir_all(&directory).unwrap();
        std::fs::write(directory.join("active.json"), b"{not-json\n").unwrap();
        let error = load_active_policy_snapshot(temp.path())
            .expect_err("malformed unsigned bytes must not become a staged policy");
        assert!(error.contains("active policy parse"), "{error}");

        let mut stale = exact_sidon_policy();
        stale.id = "vap_00000000000000000000000000000000".to_string();
        std::fs::write(
            directory.join("active.json"),
            serde_json::to_vec_pretty(&stale).unwrap(),
        )
        .unwrap();
        let error = load_active_policy_snapshot(temp.path())
            .expect_err("a stale unsigned id must not become a staged policy");
        assert!(error.contains("id does not re-derive"), "{error}");
    }

    #[test]
    fn active_snapshot_retains_the_policy_parsed_from_its_exact_bytes() {
        let temp = TempDir::new().unwrap();
        let directory = temp.path().join(".vela/policies");
        std::fs::create_dir_all(&directory).unwrap();

        let original = exact_sidon_policy();
        let original_bytes = serde_json::to_vec_pretty(&original).unwrap();
        std::fs::write(directory.join("active.json"), &original_bytes).unwrap();
        let snapshot = load_active_policy_snapshot(temp.path()).unwrap();

        let mut replacement = original.clone();
        replacement.epoch += 1;
        replacement.id = replacement.content_address();
        std::fs::write(
            directory.join("active.json"),
            serde_json::to_vec_pretty(&replacement).unwrap(),
        )
        .unwrap();

        assert_eq!(
            snapshot.policy_bytes.as_deref(),
            Some(original_bytes.as_slice())
        );
        assert_eq!(
            snapshot.policy().map(|policy| policy.id.as_str()),
            Some(original.id.as_str())
        );
        assert_ne!(snapshot.policy().unwrap().id, replacement.id);
    }

    #[test]
    fn decision_certificate_binds_and_verifies() {
        let p = exact_sidon_policy();
        let d = evaluate(&p, &clean_exact_ctx(), NOW);
        let c = DecisionCertificate::build(
            &d,
            "vfr_test",
            "vpr_abc",
            "sha256:before",
            "sha256:after",
            AuthorityMode::PolicyDelegation,
            vec!["reviewer:will".into()],
            "service:vela-policy-engine",
            "exact_construction_dual_check_v1",
            3,
            "sha256:claim",
            2,
            true,
        );
        assert!(c.id.starts_with("vdc_"));
        assert!(c.id_is_valid());
        assert_eq!(c.outcome, Outcome::Permit);
        assert_eq!(c.authority_mode, AuthorityMode::PolicyDelegation);
        // The authority is the policy a human signed, NOT a per-item human click.
        assert_eq!(c.human_authorizers, vec!["reviewer:will"]);
        assert_eq!(c.executor, "service:vela-policy-engine");
        // Tamper detection.
        let mut bad = c.clone();
        bad.outcome = Outcome::Deny;
        assert!(!bad.id_is_valid());
    }
}

/// The signature envelope over a SEALED policy: the one governance act.
///
/// Signatures are Ed25519 over [`policy_signature_preimage`], which binds both
/// the sealed policy and `signed_at`. Every other signature preimage is rejected.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PolicySignatureRecord {
    pub policy_id: String,
    pub signer_pubkey_hex: String,
    pub signature: String,
    pub signed_at: String,
}

/// A policy that passed loading checks: sealed id re-derives and the
/// signature verifies over the sealed bytes. AUTHORITY still requires the
/// caller to check the signer is a registered human reviewer on the
/// frontier — the file system is not an actor table.
#[derive(Debug, Clone)]
pub struct VerifiedPolicy {
    pub policy: AcceptancePolicy,
    pub signer_pubkey_hex: String,
    pub signed_at: String,
}

/// Classification of the mutable active-policy pointer. Only `Active` carries
/// a [`VerifiedPolicy`]. Any other signed encoding is a strict load error.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ActivePolicyMode {
    Absent,
    StagedUnsigned,
    Active,
}

impl ActivePolicyMode {
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Absent => "absent",
            Self::StagedUnsigned => "staged_unsigned",
            Self::Active => "active",
        }
    }
}

/// One read of both mutable active-policy paths while the caller owns its
/// frontier recovery barrier. The exact bytes (or exact absence) are retained
/// so callers can bind the same snapshot into a marker-time read set and copy
/// the same bytes into a content-addressed policy snapshot.
#[derive(Debug, Clone)]
pub struct ActivePolicySnapshot {
    pub policy_bytes: Option<Vec<u8>>,
    pub signature_bytes: Option<Vec<u8>>,
    pub verified: Option<VerifiedPolicy>,
    pub mode: ActivePolicyMode,
    policy: Option<AcceptancePolicy>,
}

impl ActivePolicySnapshot {
    /// Return the policy parsed from this exact byte snapshot. This never
    /// re-reads the mutable active-policy pointer.
    #[must_use]
    pub fn policy(&self) -> Option<&AcceptancePolicy> {
        self.policy.as_ref()
    }
}

// Governance files are deliberately much smaller than general repository
// objects. The byte ceiling is the first bound; the structural budgets prevent
// a compact adversarial document from spending unbounded parser work.
pub const POLICY_JSON_MAX_BYTES: usize = 64 * 1024;
pub const POLICY_SIGNATURE_JSON_MAX_BYTES: usize = 8 * 1024;
const GOVERNANCE_JSON_MAX_DEPTH: usize = 16;
const GOVERNANCE_JSON_MAX_NODES: usize = 4_096;
const GOVERNANCE_JSON_MAX_OBJECT_FIELDS: usize = 2_048;
const GOVERNANCE_JSON_MAX_ARRAY_ELEMENTS: usize = 2_048;
const GOVERNANCE_JSON_MAX_STRING_BYTES: usize = 16 * 1024;

#[derive(Debug, Clone, Copy)]
struct GovernanceJsonLimits {
    bytes: usize,
}

#[derive(Debug)]
struct GovernanceJsonState {
    nodes: usize,
    object_fields: usize,
    array_elements: usize,
}

impl GovernanceJsonState {
    fn bump_node<E: de::Error>(&mut self, path: &str) -> Result<(), E> {
        self.nodes = self.nodes.saturating_add(1);
        if self.nodes > GOVERNANCE_JSON_MAX_NODES {
            return Err(E::custom(format!(
                "{path}: JSON node budget exceeds {GOVERNANCE_JSON_MAX_NODES}"
            )));
        }
        Ok(())
    }

    fn bump_object_field<E: de::Error>(&mut self, path: &str) -> Result<(), E> {
        self.object_fields = self.object_fields.saturating_add(1);
        if self.object_fields > GOVERNANCE_JSON_MAX_OBJECT_FIELDS {
            return Err(E::custom(format!(
                "{path}: object-field budget exceeds {GOVERNANCE_JSON_MAX_OBJECT_FIELDS}"
            )));
        }
        Ok(())
    }

    fn bump_array_element<E: de::Error>(&mut self, path: &str) -> Result<(), E> {
        self.array_elements = self.array_elements.saturating_add(1);
        if self.array_elements > GOVERNANCE_JSON_MAX_ARRAY_ELEMENTS {
            return Err(E::custom(format!(
                "{path}: array-element budget exceeds {GOVERNANCE_JSON_MAX_ARRAY_ELEMENTS}"
            )));
        }
        Ok(())
    }

    fn check_depth<E: de::Error>(&self, path: &str, depth: usize) -> Result<(), E> {
        if depth > GOVERNANCE_JSON_MAX_DEPTH {
            return Err(E::custom(format!(
                "{path}: JSON depth is {depth}; limit is {GOVERNANCE_JSON_MAX_DEPTH}"
            )));
        }
        Ok(())
    }

    fn check_string<E: de::Error>(&self, path: &str, value: &str) -> Result<(), E> {
        if value.len() > GOVERNANCE_JSON_MAX_STRING_BYTES {
            return Err(E::custom(format!(
                "{path}: string is {} bytes; limit is {GOVERNANCE_JSON_MAX_STRING_BYTES} bytes",
                value.len()
            )));
        }
        Ok(())
    }
}

struct GovernanceValueSeed<'a> {
    state: &'a mut GovernanceJsonState,
    parent_depth: usize,
    path: String,
}

impl<'de> DeserializeSeed<'de> for GovernanceValueSeed<'_> {
    type Value = Value;

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        self.state.bump_node::<D::Error>(&self.path)?;
        deserializer.deserialize_any(GovernanceValueVisitor {
            state: self.state,
            parent_depth: self.parent_depth,
            path: self.path,
        })
    }
}

struct GovernanceValueVisitor<'a> {
    state: &'a mut GovernanceJsonState,
    parent_depth: usize,
    path: String,
}

impl<'de> Visitor<'de> for GovernanceValueVisitor<'_> {
    type Value = Value;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("bounded JSON without duplicate object names")
    }

    fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E> {
        Ok(Value::Bool(value))
    }

    fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E> {
        Ok(Value::Number(value.into()))
    }

    fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
        Ok(Value::Number(value.into()))
    }

    fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        serde_json::Number::from_f64(value)
            .map(Value::Number)
            .ok_or_else(|| E::custom(format!("{}: non-finite number", self.path)))
    }

    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        self.state.check_string::<E>(&self.path, value)?;
        Ok(Value::String(value.to_string()))
    }

    fn visit_borrowed_str<E>(self, value: &'de str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        self.visit_str(value)
    }

    fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        self.state.check_string::<E>(&self.path, &value)?;
        Ok(Value::String(value))
    }

    fn visit_none<E>(self) -> Result<Self::Value, E> {
        Ok(Value::Null)
    }

    fn visit_unit<E>(self) -> Result<Self::Value, E> {
        Ok(Value::Null)
    }

    fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        let depth = self.parent_depth.saturating_add(1);
        self.state.check_depth::<A::Error>(&self.path, depth)?;
        let mut values = Vec::with_capacity(sequence.size_hint().unwrap_or(0).min(256));
        let mut index = 0usize;
        loop {
            let child_path = format!("{}[{index}]", self.path);
            let seed = GovernanceValueSeed {
                state: self.state,
                parent_depth: depth,
                path: child_path.clone(),
            };
            let Some(value) = sequence.next_element_seed(seed)? else {
                break;
            };
            self.state.bump_array_element::<A::Error>(&child_path)?;
            values.push(value);
            index = index.saturating_add(1);
        }
        Ok(Value::Array(values))
    }

    fn visit_map<A>(self, mut entries: A) -> Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        let depth = self.parent_depth.saturating_add(1);
        self.state.check_depth::<A::Error>(&self.path, depth)?;
        let mut value = Map::new();
        let mut names = HashSet::new();
        while let Some(key) = entries.next_key::<String>()? {
            self.state.check_string::<A::Error>(&self.path, &key)?;
            let child_path = format!("{}.{}", self.path, key);
            self.state.bump_object_field::<A::Error>(&child_path)?;
            if !names.insert(key.clone()) {
                return Err(<A::Error as de::Error>::custom(format!(
                    "{child_path}: duplicate object name `{key}`"
                )));
            }
            let item = entries.next_value_seed(GovernanceValueSeed {
                state: self.state,
                parent_depth: depth,
                path: child_path,
            })?;
            value.insert(key, item);
        }
        Ok(Value::Object(value))
    }
}

fn parse_bounded_governance_json<T: DeserializeOwned>(
    bytes: &[u8],
    limits: GovernanceJsonLimits,
    label: &str,
) -> Result<T, String> {
    if bytes.len() > limits.bytes {
        return Err(format!(
            "{label} parse: encoded JSON is {} bytes; limit is {} bytes",
            bytes.len(),
            limits.bytes
        ));
    }
    let mut state = GovernanceJsonState {
        nodes: 0,
        object_fields: 0,
        array_elements: 0,
    };
    let mut deserializer = serde_json::Deserializer::from_slice(bytes);
    let value = GovernanceValueSeed {
        state: &mut state,
        parent_depth: 0,
        path: "$".to_string(),
    }
    .deserialize(&mut deserializer)
    .map_err(|error| format!("{label} parse: {error}"))?;
    deserializer
        .end()
        .map_err(|error| format!("{label} parse: {error}"))?;
    serde_json::from_value(value).map_err(|error| format!("{label} parse: {error}"))
}

fn validate_rfc3339(label: &str, value: &str) -> Result<(), String> {
    chrono::DateTime::parse_from_rfc3339(value)
        .map(|_| ())
        .map_err(|error| format!("{label} must be RFC3339: {error}"))
}

fn validate_supported_policy(policy: &AcceptancePolicy, label: &str) -> Result<(), String> {
    if !matches!(
        policy.schema.as_str(),
        ACCEPTANCE_POLICY_V0_1_SCHEMA
            | ACCEPTANCE_POLICY_V0_2_SCHEMA
            | ACCEPTANCE_POLICY_V0_3_SCHEMA
    ) {
        return Err(format!(
            "{label} schema must be {ACCEPTANCE_POLICY_V0_1_SCHEMA}, {ACCEPTANCE_POLICY_V0_2_SCHEMA}, or {ACCEPTANCE_POLICY_V0_3_SCHEMA}, got {}",
            policy.schema
        ));
    }
    if let Some(reason) = binding_policy_error(policy) {
        return Err(format!("{label} binding constraints are invalid: {reason}"));
    }
    validate_rfc3339(&format!("{label} expires_at"), &policy.expires_at)
}

const POLICY_SIGNATURE_INPUT_SCHEMA: &str = "vela.policy-signature-input.v1";

#[derive(Serialize)]
struct PolicySignatureInput<'a> {
    schema: &'static str,
    policy: &'a AcceptancePolicy,
    signed_at: &'a str,
}

/// Domain-separated bytes signed by every newly issued policy signature.
/// Binding `signed_at` prevents an attacker from rewriting policy chronology
/// while retaining a valid signature over the policy body.
pub fn policy_signature_preimage(
    policy: &AcceptancePolicy,
    signed_at: &str,
) -> Result<Vec<u8>, String> {
    validate_supported_policy(policy, "policy signature input")?;
    validate_rfc3339("policy signed_at", signed_at)?;
    crate::canonical::to_canonical_bytes(&PolicySignatureInput {
        schema: POLICY_SIGNATURE_INPUT_SCHEMA,
        policy,
        signed_at,
    })
    .map_err(|error| format!("canonical policy signature input: {error}"))
}

/// Frontier-scoped authority resolved from a verified singular policy
/// signature. Actor ids, rather than public keys, are the durable human
/// authorizers carried into decision certificates.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PolicyAuthority {
    pub human_authorizers: Vec<String>,
}

/// Resolve the human authority behind a cryptographically valid policy.
///
/// Loading first proves a closed, supported policy shape and a signature that
/// binds its timestamp. This frontier-scoped predicate then proves lifecycle
/// and actor authority for both live Permit and strict replay. The current
/// signature envelope contains one signature, so it can satisfy only a
/// one-person quorum whose eligible roles admit a reviewer or steward.
pub fn resolve_policy_authority(
    project: &crate::project::Project,
    verified: &VerifiedPolicy,
    decision_time: &str,
) -> Result<PolicyAuthority, String> {
    resolve_policy_authority_inner(project, verified, decision_time)
}

fn resolve_policy_authority_inner(
    project: &crate::project::Project,
    verified: &VerifiedPolicy,
    decision_time: &str,
) -> Result<PolicyAuthority, String> {
    validate_supported_policy(&verified.policy, "verified policy")?;
    if !verified.policy.id_is_valid() {
        return Err(format!(
            "verified policy {} id does not re-derive",
            verified.policy.id
        ));
    }
    validate_rfc3339("policy decision time", decision_time)?;
    if verified.policy.is_expired(decision_time) {
        return Err(format!(
            "policy {} is expired at decision time",
            verified.policy.id
        ));
    }
    if verified.policy.revocation_ref.is_some() {
        return Err(format!("policy {} is revoked", verified.policy.id));
    }
    let frontier_id = project
        .frontier_id
        .as_deref()
        .ok_or_else(|| "policy authority requires a frontier id".to_string())?;
    if verified.policy.frontier_id != frontier_id {
        return Err(format!(
            "policy frontier mismatch: policy {} targets {}, current frontier is {frontier_id}",
            verified.policy.id, verified.policy.frontier_id
        ));
    }

    if verified.policy.quorum.threshold != 1 {
        return Err(format!(
            "singular policy signature cannot satisfy quorum threshold {} (must be exactly 1)",
            verified.policy.quorum.threshold
        ));
    }
    let eligible_roles = verified
        .policy
        .quorum
        .eligible_roles
        .iter()
        .map(|role| role.trim().to_ascii_lowercase())
        .collect::<std::collections::BTreeSet<_>>();
    if !eligible_roles
        .iter()
        .any(|role| matches!(role.as_str(), "reviewer" | "steward"))
    {
        return Err("policy quorum eligible_roles must admit a reviewer or steward".to_string());
    }

    let matching = project
        .actors
        .iter()
        .filter(|actor| {
            actor
                .public_key
                .eq_ignore_ascii_case(&verified.signer_pubkey_hex)
        })
        .collect::<Vec<_>>();
    let actor = match matching.as_slice() {
        [] => {
            return Err(format!(
                "policy signer pubkey {} is not a registered actor on this frontier",
                verified.signer_pubkey_hex
            ));
        }
        [actor] => *actor,
        _ => {
            return Err(format!(
                "policy signer pubkey {} resolves ambiguously to {} actors on this frontier",
                verified.signer_pubkey_hex,
                matching.len()
            ));
        }
    };
    let actor_role = actor
        .id
        .split_once(':')
        .map(|(role, _)| role.trim().to_ascii_lowercase())
        .unwrap_or_default();
    let registered_human_role = matches!(actor_role.as_str(), "reviewer" | "steward")
        && !crate::proposals::is_placeholder_reviewer(&actor.id);
    if !registered_human_role {
        return Err(format!(
            "policy signer actor '{}' is not a registered reviewer or steward human",
            actor.id
        ));
    }
    if !eligible_roles.contains(&actor_role) {
        return Err(format!(
            "policy signer actor '{}' has registered role '{}', which is not in eligible_roles",
            actor.id, actor_role
        ));
    }
    if !verified
        .policy
        .issued_by
        .iter()
        .any(|issuer| issuer == &actor.id)
    {
        return Err(format!(
            "policy signer actor '{}' is not named in issued_by",
            actor.id
        ));
    }

    let parse_time = |label: &str, value: &str| {
        chrono::DateTime::parse_from_rfc3339(value)
            .map_err(|error| format!("{label} must be RFC3339: {error}"))
    };
    let actor_created_at = parse_time("actor created_at", &actor.created_at)?;
    let signed_at = parse_time("policy signed_at", &verified.signed_at)?;
    let decision_at = parse_time("policy decision time", decision_time)?;
    if actor_created_at >= signed_at {
        return Err(format!(
            "policy signer actor '{}' was not registered before policy signing",
            actor.id
        ));
    }
    if signed_at > decision_at {
        return Err(format!(
            "policy {} was signed after the decision time",
            verified.policy.id
        ));
    }
    if let Some(revoked_at) = actor.revoked_at.as_deref() {
        let revoked_at = parse_time("actor revoked_at", revoked_at)?;
        if revoked_at <= signed_at {
            return Err(format!(
                "policy signer actor '{}' was revoked at policy signing",
                actor.id
            ));
        }
        if revoked_at <= decision_at {
            return Err(format!(
                "policy signer actor '{}' is revoked at decision time",
                actor.id
            ));
        }
    }

    Ok(PolicyAuthority {
        human_authorizers: vec![actor.id.clone()],
    })
}

/// Load `.vela/policies/active.json` + `active.sig.json` from a frontier
/// dir. Returns None when absent; Err on a present-but-broken pair (corrupt
/// governance must never fail open into the absent state).
pub fn load_active_policy(
    frontier_dir: &std::path::Path,
) -> Result<Option<VerifiedPolicy>, String> {
    Ok(load_active_policy_snapshot(frontier_dir)?.verified)
}

/// Content observation for a prelaunch policy pair that no longer parses as a
/// supported [`AcceptancePolicy`]. This is provenance for a human retirement
/// proposal, never policy authority: it deliberately does not rederive the
/// policy id, validate the current schema, or verify the historical signature.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LegacyPolicyPairObservation {
    pub stored_policy_id: String,
    pub policy_bytes_root: String,
    pub signature_bytes_root: String,
}

/// Inspect exact legacy policy/signature bytes under the same structural and
/// byte budgets as current governance inputs. Duplicate object names are
/// rejected before the two stored ids are compared. A successful observation
/// says only which immutable bytes are present; it must never be interpreted as
/// a valid signature or an authorization to admit scientific state.
pub fn observe_legacy_policy_pair_bytes(
    policy_bytes: &[u8],
    signature_bytes: &[u8],
) -> Result<LegacyPolicyPairObservation, String> {
    let policy: Value = parse_bounded_governance_json(
        policy_bytes,
        GovernanceJsonLimits {
            bytes: POLICY_JSON_MAX_BYTES,
        },
        "legacy active policy",
    )?;
    let signature: Value = parse_bounded_governance_json(
        signature_bytes,
        GovernanceJsonLimits {
            bytes: POLICY_SIGNATURE_JSON_MAX_BYTES,
        },
        "legacy active policy signature",
    )?;
    let stored_policy_id = policy
        .as_object()
        .and_then(|object| object.get("id"))
        .and_then(Value::as_str)
        .ok_or_else(|| "legacy active policy must contain a string id".to_string())?;
    let signature_policy_id = signature
        .as_object()
        .and_then(|object| object.get("policy_id"))
        .and_then(Value::as_str)
        .ok_or_else(|| {
            "legacy active policy signature must contain a string policy_id".to_string()
        })?;
    if !is_policy_id_shape(stored_policy_id) {
        return Err(
            "legacy active policy id must be vap_ followed by 32 lowercase hex characters"
                .to_string(),
        );
    }
    if signature_policy_id != stored_policy_id {
        return Err(
            "legacy active policy and signature name different stored policy ids".to_string(),
        );
    }
    Ok(LegacyPolicyPairObservation {
        stored_policy_id: stored_policy_id.to_string(),
        policy_bytes_root: format!("sha256:{}", hex::encode(Sha256::digest(policy_bytes))),
        signature_bytes_root: format!("sha256:{}", hex::encode(Sha256::digest(signature_bytes))),
    })
}

fn is_policy_id_shape(value: &str) -> bool {
    value.strip_prefix("vap_").is_some_and(|digest| {
        digest.len() == 32
            && digest
                .bytes()
                .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
    })
}

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

    #[test]
    fn observes_legacy_bytes_without_granting_current_policy_authority() {
        let id = "vap_e0abc750544408e637bd90e0661bac15";
        let policy =
            format!(r#"{{"schema":"vela.acceptance_policy.prelaunch","id":"{id}","legacy":true}}"#);
        let signature = format!(
            r#"{{"policy_id":"{id}","signer_pubkey_hex":"not-authority","signature":"historical","signed_at":"before-current-format"}}"#
        );
        let observed =
            observe_legacy_policy_pair_bytes(policy.as_bytes(), signature.as_bytes()).unwrap();
        assert_eq!(observed.stored_policy_id, id);
        assert_eq!(
            observed.policy_bytes_root,
            format!("sha256:{}", hex::encode(Sha256::digest(policy.as_bytes())))
        );
        assert!(
            verify_policy_signature_bytes(
                policy.as_bytes(),
                signature.as_bytes(),
                None,
                "legacy fixture"
            )
            .is_err(),
            "observation must not make unsupported legacy bytes authoritative"
        );
    }

    #[test]
    fn rejects_duplicate_names_and_mismatched_envelope_ids() {
        let id = "vap_e0abc750544408e637bd90e0661bac15";
        let duplicate = format!(r#"{{"id":"{id}","id":"{id}"}}"#);
        let signature = format!(r#"{{"policy_id":"{id}"}}"#);
        let error = observe_legacy_policy_pair_bytes(duplicate.as_bytes(), signature.as_bytes())
            .unwrap_err();
        assert!(error.contains("duplicate object name"), "{error}");

        let other = "vap_dbbc9caf67767317e42e217c65bab979";
        let policy = format!(r#"{{"id":"{id}"}}"#);
        let signature = format!(r#"{{"policy_id":"{other}"}}"#);
        let error =
            observe_legacy_policy_pair_bytes(policy.as_bytes(), signature.as_bytes()).unwrap_err();
        assert!(error.contains("different stored policy ids"), "{error}");
    }
}

/// Load the active policy paths exactly once, retaining the bytes that were
/// verified. A present policy without a signature is staged and human-only;
/// an orphan signature is corruption and never fails open.
pub fn load_active_policy_snapshot(
    frontier_dir: &std::path::Path,
) -> Result<ActivePolicySnapshot, String> {
    let dir = frontier_dir.join(".vela").join("policies");
    let policy_path = dir.join("active.json");
    let sig_path = dir.join("active.sig.json");
    let policy_bytes =
        read_optional_regular_file(&policy_path, "active policy", POLICY_JSON_MAX_BYTES)?;
    let signature_bytes = read_optional_regular_file(
        &sig_path,
        "active policy signature",
        POLICY_SIGNATURE_JSON_MAX_BYTES,
    )?;
    let (mode, policy, verified) = match (&policy_bytes, &signature_bytes) {
        (None, None) => (ActivePolicyMode::Absent, None, None),
        (Some(policy), None) => {
            let policy = parse_supported_policy_bytes(policy, "active policy")?;
            if !policy.id_is_valid() {
                return Err(format!(
                    "active policy id does not re-derive: stored {}, sealed {}",
                    policy.id,
                    policy.content_address()
                ));
            }
            (ActivePolicyMode::StagedUnsigned, Some(policy), None)
        }
        (None, Some(_)) => {
            return Err(
                "active policy signature exists without .vela/policies/active.json".to_string(),
            );
        }
        (Some(policy), Some(signature)) => {
            let verified = verify_policy_signature_bytes(policy, signature, None, "active policy")?;
            (
                ActivePolicyMode::Active,
                Some(verified.policy.clone()),
                Some(verified),
            )
        }
    };
    Ok(ActivePolicySnapshot {
        policy_bytes,
        signature_bytes,
        verified,
        mode,
        policy,
    })
}

fn parse_supported_policy_bytes(
    policy_bytes: &[u8],
    label: &str,
) -> Result<AcceptancePolicy, String> {
    let policy: AcceptancePolicy = parse_bounded_governance_json(
        policy_bytes,
        GovernanceJsonLimits {
            bytes: POLICY_JSON_MAX_BYTES,
        },
        label,
    )?;
    validate_supported_policy(&policy, label)?;
    Ok(policy)
}

fn read_optional_regular_file(
    path: &std::path::Path,
    label: &str,
    max_bytes: usize,
) -> Result<Option<Vec<u8>>, String> {
    match std::fs::symlink_metadata(path) {
        Ok(metadata) if metadata.file_type().is_file() && !metadata.file_type().is_symlink() => {
            if metadata.len() > max_bytes as u64 {
                return Err(format!(
                    "{label} exceeds the {max_bytes}-byte input limit: {}",
                    path.display()
                ));
            }
            std::fs::read(path)
                .map(Some)
                .map_err(|error| format!("read {label} {}: {error}", path.display()))
        }
        Ok(_) => Err(format!(
            "{label} must be a regular non-symlink file: {}",
            path.display()
        )),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(error) => Err(format!("inspect {label} {}: {error}", path.display())),
    }
}

/// Verify an exact policy/signature byte pair. `expected_policy_id` binds
/// content-addressed historical snapshots to their path name.
pub fn verify_policy_signature_bytes(
    policy_bytes: &[u8],
    signature_bytes: &[u8],
    expected_policy_id: Option<&str>,
    label: &str,
) -> Result<VerifiedPolicy, String> {
    use ed25519_dalek::Verifier;

    let policy: AcceptancePolicy = parse_bounded_governance_json(
        policy_bytes,
        GovernanceJsonLimits {
            bytes: POLICY_JSON_MAX_BYTES,
        },
        label,
    )?;
    validate_supported_policy(&policy, label)?;
    if expected_policy_id.is_some_and(|expected| expected != policy.id) || !policy.id_is_valid() {
        return Err(format!(
            "{label} id does not re-derive: stored {}, sealed {}",
            policy.id,
            policy.content_address()
        ));
    }
    let signature_label = format!("{label} signature");
    let sig: PolicySignatureRecord = parse_bounded_governance_json(
        signature_bytes,
        GovernanceJsonLimits {
            bytes: POLICY_SIGNATURE_JSON_MAX_BYTES,
        },
        &signature_label,
    )?;
    validate_rfc3339("policy signed_at", &sig.signed_at)?;
    if sig.policy_id != policy.id {
        return Err(format!("{label} signature is for a different policy id"));
    }
    let pk_bytes: [u8; 32] = hex::decode(&sig.signer_pubkey_hex)
        .map_err(|e| format!("pubkey hex: {e}"))?
        .try_into()
        .map_err(|_| "pubkey must be 32 bytes".to_string())?;
    let vk = ed25519_dalek::VerifyingKey::from_bytes(&pk_bytes).map_err(|e| e.to_string())?;
    let sig_bytes: [u8; 64] = hex::decode(&sig.signature)
        .map_err(|e| format!("signature hex: {e}"))?
        .try_into()
        .map_err(|_| "signature must be 64 bytes".to_string())?;
    let signature = ed25519_dalek::Signature::from_bytes(&sig_bytes);
    let bound_body = policy_signature_preimage(&policy, &sig.signed_at)?;
    vk.verify(&bound_body, &signature)
        .map_err(|_| format!("{label} signature does not verify"))?;
    Ok(VerifiedPolicy {
        policy,
        signer_pubkey_hex: sig.signer_pubkey_hex,
        signed_at: sig.signed_at,
    })
}