vela-protocol 0.940.6

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
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
//! Canonical replayable frontier events.
//!
//! Events are the authoritative record for user-visible state transitions in
//! the finding-centered v0 kernel. Frontier snapshots remain the convenient
//! materialized state, but checks and proof packets can validate the event log.

use std::collections::{BTreeMap, BTreeSet};

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};

use crate::bundle::FindingBundle;
use crate::canonical;
use crate::project::Project;

pub const EVENT_SCHEMA: &str = "vela.event.v0.1";
pub const NULL_HASH: &str = "sha256:null";

/// v0.49: explicit event kind for actor-key revocation. Coalition
/// governance promises that key compromise is handled by a signed
/// `RevocationEvent` that names the key, the moment of compromise,
/// and the recommended replacement. This constant pairs with
/// `RevocationPayload` and `new_revocation_event` below.
///
/// Existing signed history stays valid as a record of what was
/// signed when; clients that re-verify against a governed actor
/// registry may flag signatures whose `signed_at` is after the
/// `revoked_at` moment. This historical event is audit-only in the
/// repository reducer: it does not itself mutate `.vela/actors.json`.
/// Profile v1 therefore freezes the actor registry until a separate,
/// repository-local rotation and recovery contract is accepted.
pub const EVENT_KIND_KEY_REVOKE: &str = "key.revoke";

/// Generic artifact lifecycle. Carries the full `Artifact` inline on
/// `payload.artifact` so protocol snapshots can be replayed without
/// resolving a sidecar file first.
pub const EVENT_KIND_ARTIFACT_ASSERTED: &str = "artifact.asserted";
pub const EVENT_KIND_ARTIFACT_REVIEWED: &str = "artifact.reviewed";
pub const EVENT_KIND_ARTIFACT_RETRACTED: &str = "artifact.retracted";

/// A verifier attachment was bound to a finding (target = vf_…). The attachment
/// travels in payload.attachment; the reducer appends it to the sidecar
/// verifier_attachments collection. Per-finding trust status is derived on read.
pub const EVENT_KIND_VERIFIER_ATTACHMENT_ADDED: &str = "verifier_attachment.added";

/// v0.51: Re-classify a finding or artifact's read-side
/// access tier. Audit-trail event for the dual-use channel: the
/// fact that an object's tier changed (and who changed it, when, with
/// what reason) is itself part of the substrate's accountability
/// surface and must replay deterministically.
pub const EVENT_KIND_TIER_SET: &str = "tier.set";

/// v0.56: Mechanical evidence-atom locator repair. Targets a single
/// evidence atom by id and sets its `locator` field to the value
/// resolved from the parent source's locator. Carries the resolved
/// locator string and the source id it was derived from on the event
/// payload so a fresh replay reconstructs the atom's locator without
/// needing to re-resolve the source.
///
/// This event mutates `state.evidence_atoms[i].locator` and clears the
/// "missing evidence locator" caveat on the same atom. It does not
/// touch `state.findings`, so cross-impl reducer fixtures whose
/// post-replay digest covers `findings[]` only treat this event as a
/// no-op on finding state. The Rust reducer still has an explicit arm
/// to avoid silently dropping the repair from a fresh replay.
pub const EVENT_KIND_EVIDENCE_ATOM_LOCATOR_REPAIRED: &str = "evidence_atom.locator_repaired";

/// v0.57: Mechanical evidence-span repair on a finding. Appends one
/// `{section, text}` span to `state.findings[i].evidence.evidence_spans`.
/// Required payload: `{proposal_id, section, text}`. The reducer arm
/// is idempotent under identical re-application (refuses to append an
/// equal span twice on the same finding).
pub const EVENT_KIND_FINDING_SPAN_REPAIRED: &str = "finding.span_repaired";

/// Per-event attestation. Frontier-level review and decision records are too
/// coarse to bind one exact event. Per-event attestation lets a reviewer or
/// external verifier
/// attest one specific canonical event (`vev_*`) by emitting a
/// new `attestation.recorded` event that points at it.
///
/// Required payload: `{target_event_id, attester_id, scope_note}`.
/// Optional: `scopes`, `reviewer_role`, `orcid`, `ror`,
/// `attestation_id`, `signature` (Ed25519 over the target event's
/// preimage), `proof_id` (`vpf_*` from the v0.75 Vela proof
/// primitive when the attestation is backed by a proof-assistant
/// verification), `signed_at` (RFC3339).
///
/// Reducer arm: no-op on findings. Attestations live as
/// append-only canonical events; consumers (Workbench, audit
/// scripts, hub mirrors) project them per-event by reading the
/// log.
pub const EVENT_KIND_ATTESTATION_RECORDED: &str = "attestation.recorded";

/// Review verdict over non-mutating frontier observation material,
/// such as research traces and correction returns. This records what
/// entered the review ledger without asserting a finding by itself.
pub const EVENT_KIND_FRONTIER_OBSERVATION_REVIEWED: &str = "frontier.observation_reviewed";

/// T7: a reviewer's decision on a Contradiction object (`vcx_`). The
/// event carries the full resolved `Contradiction` in
/// `payload.contradiction`; the reducer upserts it into
/// `Project.contradictions` (latest resolution per id wins). This is
/// the only canonical state a contradiction accrues — candidates are
/// derived from the graph and never written. Honest by construction:
/// the stored object's status records a *named reviewer's* judgment,
/// never platform-adjudicated truth.
pub const EVENT_KIND_CONTRADICTION_RESOLVED: &str = "contradiction.resolved";

/// A signed banked attempt (`vat_`) is deposited into the frontier. The full
/// [`crate::attempt::Attempt`] travels in `payload.attempt`; the reducer
/// verifies its id + signature and upserts it into `Project.attempts`.
pub const EVENT_KIND_ATTEMPT_DEPOSITED: &str = "attempt.deposited";

/// A signed cross-domain [`crate::transfer::Transfer`] is deposited. The full
/// object travels in `payload.transfer`; the reducer verifies its id +
/// signature and upserts it into `Project.transfers`. Admission (whether the
/// link is sound) is derived on read, never stored.
pub const EVENT_KIND_TRANSFER_DEPOSITED: &str = "transfer.deposited";

/// A signed significance [`crate::endorsement::Endorsement`] is deposited. The
/// object travels in `payload.endorsement`; the reducer verifies and upserts
/// into `Project.endorsements`. Endorsements are stored individually and NEVER
/// aggregated into a score.
pub const EVENT_KIND_ENDORSEMENT_DEPOSITED: &str = "endorsement.deposited";

/// An append-only lifecycle transition on an attempt. The
/// [`crate::attempt::ResolutionEvent`] travels in `payload.resolution`; the
/// reducer upserts it into `Project.attempt_resolutions` (idempotent by
/// `vre_` id; the head per attempt is the latest by `at`).
pub const EVENT_KIND_ATTEMPT_RESOLVED: &str = "attempt.resolved";

/// A signed statement-faithfulness attestation rides in
/// `payload.attestation`; the reducer upserts it (idempotent by vsa_ id).
pub const EVENT_KIND_STATEMENT_ATTESTED: &str = "statement.attested";

/// A TTL lease on an open obligation: fleet coordination so producers
/// route around in-flight work (the ETP ran 22M-implication crowd+AI
/// coordination on GitHub-issue leases; this is that primitive, signed).
/// Expiry is computed at READ time from event timestamp + ttl — the
/// reducer never reads a clock.
pub const EVENT_KIND_ATTEMPT_CLAIMED: &str = "attempt.claimed";
/// Maximum duration of one coordination lease: 365 days.
///
/// A lease is temporary operational coordination, not scientific authority.
/// Bounding its wire value prevents integer/date overflow and makes every
/// implementation agree on whether a lease can be live. A zero value remains
/// reserved for the compare-and-swap release form.
pub const MAX_ATTEMPT_LEASE_TTL_SECONDS: u64 = 31_536_000;

/// Parse and safely add one protocol-bounded coordination lease duration.
///
/// Callers must not reproduce date arithmetic with integer casts: malformed
/// historical input is an error, never a perpetually live lease or a panic.
pub fn attempt_lease_expiry(
    claimed_at: &str,
    ttl_seconds: u64,
) -> Result<chrono::DateTime<chrono::FixedOffset>, String> {
    if ttl_seconds > MAX_ATTEMPT_LEASE_TTL_SECONDS {
        return Err(format!(
            "lease_ttl_seconds must be at most {MAX_ATTEMPT_LEASE_TTL_SECONDS}"
        ));
    }
    let claimed = chrono::DateTime::parse_from_rfc3339(claimed_at)
        .map_err(|error| format!("invalid lease timestamp: {error}"))?;
    let seconds = i64::try_from(ttl_seconds)
        .map_err(|_| "lease_ttl_seconds cannot be represented safely".to_string())?;
    let duration = chrono::Duration::try_seconds(seconds)
        .ok_or_else(|| "lease_ttl_seconds exceeds the supported duration".to_string())?;
    claimed
        .checked_add_signed(duration)
        .ok_or_else(|| "lease expiry exceeds the supported timestamp range".to_string())
}

/// Priority registration: a content-addressed statement hash with a hub
/// receipt timestamp. External anchoring rides the release-archive
/// chain (every Zenodo/GH bundle embeds the event log), so the time
/// claim does not depend on trusting the hub.
pub const EVENT_KIND_STATEMENT_REGISTERED: &str = "statement.registered";

/// A reviewer's decision on a proposal, recorded as a first-class,
/// signed, append-only event. Before these existed, an accept graduated
/// into a signed domain event (`finding.asserted`, …) but a REJECT
/// mutated only the proposal file's `status` field — leaving no
/// tamper-evident, replayable trace of the decision. That asymmetry was
/// the silent-drop vector (THREAT_MODEL A11): a maintainer could suppress
/// an outside producer's proposal with no signed record. These three
/// kinds close it: every decision a key-registered reviewer makes is now
/// a side-table event (`before_hash == after_hash == NULL_HASH`, so it is
/// transparent to the per-finding hash chain) targeting the proposal,
/// signed by the reviewer key. Proposal `status` becomes a projection of
/// these events, verified against the stored field by the parity gate
/// (`verify_proposal_decision_parity`).
pub const EVENT_KIND_REVIEW_ACCEPTED: &str = "review.accepted";
pub const EVENT_KIND_REVIEW_REJECTED: &str = "review.rejected";
pub const EVENT_KIND_REVIEW_REVISION_REQUESTED: &str = "review.revision_requested";

/// A producer closes its own still-pending proposal. This is an audit-only
/// lifecycle event: it cannot mutate accepted scientific state and is
/// authorized by the self-signed producer identity embedded in the exact
/// Receipt v1 already bound by the proposal.
pub const EVENT_KIND_PROPOSAL_WITHDRAWN: &str = "proposal.withdrawn";
pub const PROPOSAL_WITHDRAWAL_PAYLOAD_SCHEMA_V1: &str = "vela.proposal-withdrawal.v1";
pub const PROPOSAL_WITHDRAWAL_PAYLOAD_SCHEMA: &str = "vela.proposal-withdrawal.v2";

/// Signed temporal activation of an actor registration. The event is an
/// audit-only boundary over exact Git/Vela history; it neither authenticates
/// unsigned legacy events nor mutates scientific state.
pub const EVENT_KIND_ACTOR_REGISTRATION_ACTIVATED: &str = "actor.registration_activated";

/// Signed, non-scientific binding between one exact Frontier repository
/// history, its stable identity, and its exact dependency list.
pub const EVENT_KIND_FRONTIER_REPOSITORY_BOUND: &str = "frontier.repository_bound";

pub const FRONTIER_CREATED_SCHEMA_V1: &str = "vela.frontier-created.v1";

/// Deterministic machine-verified admission (Phase 1A). Emitted, UNSIGNED, when a
/// proposal clears the exact-lane auto-admission predicate (kernel-clean, >=2
/// independent attachments derive `Verified`, a present-and-Survived
/// FormalismFidelity probe, Sound method integrity, no synthetic-source block, no
/// frontier contradiction). The trust is the frozen verifier + the audited
/// deterministic predicate, never a model and never a human rubber stamp; this
/// materializes the `machine_verified` tier, distinct from `review.accepted`
/// (human, signed, = significance/release). `before_hash == after_hash` (no
/// finding mutation): it is an audit record of the admission decision, binding
/// (proposal_id, attachment_ids, signal_status, policy_version).
pub const EVENT_KIND_POLICY_AUTO_ADMITTED: &str = "policy.auto_admitted";

/// The complete registry of event kinds the protocol can emit or store.
/// This is the writer-side universe; the reducer must handle every kind
/// here (a real arm or an explicit no-op) — `reducer::every_known_kind_reduces`
/// pins that invariant, so a new kind added to a writer without a reducer
/// arm fails CI instead of erroring on the next replay. If you add a kind
/// anywhere, add it here.
pub const KNOWN_EVENT_KINDS: &[&str] = &[
    "frontier.created",
    "finding.asserted",
    "finding.reviewed",
    "finding.noted",
    "finding.caveated",
    "finding.confidence_revised",
    "finding.rejected",
    "finding.retracted",
    "finding.contribution.recorded",
    "finding.superseded",
    "finding.dependency_invalidated",
    "finding.span_repaired",
    "assertion.reinterpreted_causal",
    "source_text.reviewed",
    "artifact.asserted",
    "artifact.reviewed",
    "artifact.retracted",
    "verifier_attachment.added",
    "tier.set",
    "evidence_atom.locator_repaired",
    "attestation.recorded",
    "frontier.observation_reviewed",
    "diff_pack.released",
    "diff_pack.reviewed",
    "verdict_conflict.resolved",
    "contradiction.resolved",
    "attempt.deposited",
    "attempt.resolved",
    "transfer.deposited",
    "endorsement.deposited",
    "statement.attested",
    "attempt.claimed",
    "statement.registered",
    "anchor.attached",
    "anchor.retracted",
    "proposal.recommended",
    "correction_return.review",
    "research_trace.review",
    "key.revoke",
    "review.accepted",
    "review.rejected",
    "review.revision_requested",
    "proposal.withdrawn",
    "actor.registration_activated",
    "frontier.repository_bound",
    "policy.auto_admitted",
];

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct StateTarget {
    pub r#type: String,
    pub id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct StateActor {
    pub id: String,
    pub r#type: String,
}

/// Canonical actor classification. Provenance must never claim more
/// than is true: an `agent:`-prefixed id, or one whose handle ends in
/// `-bot` / `-sim`, is a machine actor, never `human`. This is the one
/// classifier used at event construction and in review-count stats so
/// an agent or bot is never recorded or counted as human review.
pub fn actor_kind(id: &str) -> &'static str {
    let id = id.trim();
    let handle = id.split(':').next_back().unwrap_or(id);
    if id.starts_with("agent:")
        || id.starts_with("sim:")
        || id.starts_with("ci:")
        || id.starts_with("verifier:")
        || id.starts_with("policy:")
        || handle.ends_with("-bot")
        || handle.ends_with("-sim")
    {
        "agent"
    } else {
        "human"
    }
}

/// The kind of a canonical state event: a typed enum over the wire strings.
///
/// It serializes to, and deserializes from, the exact `"domain.verb"` string the
/// log has always used, so canonical bytes and event ids are unchanged. The
/// difference is upstream: the reducer dispatches on this enum, so a removed or
/// mistyped handler is a compile error rather than a silent fall-through (the old
/// `match event.kind.as_str()` over bare string literals could drop a typo'd kind
/// into the default arm). `Other` round-trips any kind a future build does not
/// know, keeping the log forward-compatible. Single source of truth: the
/// `event_kinds!` table below generates `as_str` and `From<&str>` together, so
/// they cannot drift.
macro_rules! event_kinds {
    ($($variant:ident => $wire:literal),+ $(,)?) => {
        #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
        pub enum EventKind {
            $($variant,)+
            /// A kind not known to this build; holds its original wire string.
            Other(String),
        }

        impl EventKind {
            /// The canonical wire string (what serializes into the log).
            pub fn as_str(&self) -> &str {
                match self {
                    $(EventKind::$variant => $wire,)+
                    EventKind::Other(s) => s.as_str(),
                }
            }
        }

        impl From<&str> for EventKind {
            fn from(s: &str) -> Self {
                match s {
                    $($wire => EventKind::$variant,)+
                    other => EventKind::Other(other.to_string()),
                }
            }
        }
    };
}

event_kinds! {
    FrontierCreated => "frontier.created",
    FindingAsserted => "finding.asserted",
    FindingReviewed => "finding.reviewed",
    FindingNoted => "finding.noted",
    FindingCaveated => "finding.caveated",
    SourceTextReviewed => "source_text.reviewed",
    FindingConfidenceRevised => "finding.confidence_revised",
    FindingRejected => "finding.rejected",
    FindingRetracted => "finding.retracted",
    FindingContributionRecorded => "finding.contribution.recorded",
    FindingDependencyInvalidated => "finding.dependency_invalidated",
    ArtifactAsserted => "artifact.asserted",
    VerifierAttachmentAdded => "verifier_attachment.added",
    ArtifactReviewed => "artifact.reviewed",
    ArtifactRetracted => "artifact.retracted",
    TierSet => "tier.set",
    EvidenceAtomLocatorRepaired => "evidence_atom.locator_repaired",
    FindingSpanRepaired => "finding.span_repaired",
    AttestationRecorded => "attestation.recorded",
    DiffPackReleased => "diff_pack.released",
    DiffPackReviewed => "diff_pack.reviewed",
    VerdictConflictResolved => "verdict_conflict.resolved",
    ContradictionResolved => "contradiction.resolved",
    AttemptDeposited => "attempt.deposited",
    TransferDeposited => "transfer.deposited",
    EndorsementDeposited => "endorsement.deposited",
    AttemptResolved => "attempt.resolved",
    StatementAttested => "statement.attested",
    AnchorAttached => "anchor.attached",
    AnchorRetracted => "anchor.retracted",
    AttemptClaimed => "attempt.claimed",
    StatementRegistered => "statement.registered",
    FindingSuperseded => "finding.superseded",
    AssertionReinterpretedCausal => "assertion.reinterpreted_causal",
    ProposalRecommended => "proposal.recommended",
    FrontierObservationReviewed => "frontier.observation_reviewed",
    CorrectionReturnReview => "correction_return.review",
    ResearchTraceReview => "research_trace.review",
    KeyRevoke => "key.revoke",
    ReviewAccepted => "review.accepted",
    ReviewRejected => "review.rejected",
    ReviewRevisionRequested => "review.revision_requested",
    ProposalWithdrawn => "proposal.withdrawn",
    ActorRegistrationActivated => "actor.registration_activated",
    FrontierRepositoryBound => "frontier.repository_bound",
    PolicyAutoAdmitted => "policy.auto_admitted",
}

impl From<String> for EventKind {
    fn from(s: String) -> Self {
        EventKind::from(s.as_str())
    }
}

impl std::fmt::Display for EventKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

// Ergonomic comparisons so the many `event.kind == "domain.verb"` and
// `event.kind == EVENT_KIND_X` (a `&str`) call sites keep compiling unchanged.
impl PartialEq<str> for EventKind {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}
impl PartialEq<&str> for EventKind {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}

// Wire form is the bare string: `"kind":"finding.asserted"`, byte-identical to
// the pre-migration `String` field, so event ids and canonical hashes are stable.
impl Serialize for EventKind {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(self.as_str())
    }
}
impl<'de> Deserialize<'de> for EventKind {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = String::deserialize(deserializer)?;
        Ok(EventKind::from(s.as_str()))
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StateEvent {
    #[serde(default = "default_schema")]
    pub schema: String,
    pub id: String,
    pub kind: EventKind,
    pub target: StateTarget,
    pub actor: StateActor,
    pub timestamp: String,
    pub reason: String,
    pub before_hash: String,
    pub after_hash: String,
    #[serde(default)]
    pub payload: Value,
    #[serde(default)]
    pub caveats: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub signature: Option<String>,
}

pub struct FindingEventInput<'a> {
    pub kind: &'a str,
    pub finding_id: &'a str,
    pub actor_id: &'a str,
    pub actor_type: &'a str,
    pub reason: &'a str,
    pub before_hash: &'a str,
    pub after_hash: &'a str,
    pub payload: Value,
    pub caveats: Vec<String>,
    /// When the writer has already stamped a clock into the FINDING
    /// (annotation timestamp, confidence updated_at), it MUST pass that
    /// same instant here — the reducer reconstructs those fields from
    /// `event.timestamp`, and two clock reads diverge the replay hash
    /// by microseconds (caught live by verify_replay on the first
    /// replayed finding.noted events). None = stamp now.
    pub timestamp: Option<&'a str>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventLogSummary {
    pub count: usize,
    pub kinds: BTreeMap<String, usize>,
    pub first_timestamp: Option<String>,
    pub last_timestamp: Option<String>,
    pub duplicate_ids: Vec<String>,
    pub orphan_targets: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplayReport {
    pub ok: bool,
    pub status: String,
    pub event_log: EventLogSummary,
    pub source_hash: String,
    pub event_log_hash: String,
    pub replayed_hash: String,
    pub current_hash: String,
    pub conflicts: Vec<String>,
}

fn default_schema() -> String {
    EVENT_SCHEMA.to_string()
}

pub fn new_finding_event(input: FindingEventInput<'_>) -> StateEvent {
    let timestamp = input
        .timestamp
        .map(|t| t.to_string())
        .unwrap_or_else(|| Utc::now().to_rfc3339());
    let mut event = StateEvent {
        schema: EVENT_SCHEMA.to_string(),
        id: String::new(),
        kind: input.kind.into(),
        target: StateTarget {
            r#type: "finding".to_string(),
            id: input.finding_id.to_string(),
        },
        actor: StateActor {
            id: input.actor_id.to_string(),
            r#type: input.actor_type.to_string(),
        },
        timestamp,
        reason: input.reason.to_string(),
        before_hash: input.before_hash.to_string(),
        after_hash: input.after_hash.to_string(),
        payload: input.payload,
        caveats: input.caveats,
        signature: None,
    };
    event.id = event_id(&event);
    event
}

/// Payload of an `EVENT_KIND_KEY_REVOKE` event. Carries the
/// revoked Ed25519 pubkey (hex-encoded), the moment compromise was
/// detected (ISO-8601), an optional replacement pubkey the actor is
/// migrating to, and a free-form reason string. Stored on the event's
/// `payload` field; the event's `actor` is the actor whose key is
/// being revoked, and the event itself must be signed by a key that
/// was authoritative *before* the revocation (typically a co-signer
/// or the actor's prior key — never the revoked key itself).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RevocationPayload {
    /// The Ed25519 pubkey being revoked, hex-encoded (64 chars).
    pub revoked_pubkey: String,
    /// ISO-8601 moment when compromise was detected. Signatures
    /// whose `signed_at` falls after this should be flagged on
    /// re-verification.
    pub revoked_at: String,
    /// Optional replacement pubkey the actor is now signing with,
    /// hex-encoded. Reviewers re-verifying signed history use this
    /// to walk forward to the new key.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub replacement_pubkey: String,
    /// Free-form reason — "key file leaked", "stolen device",
    /// "scheduled rotation", etc. Reviewer-facing only; the
    /// substrate doesn't enumerate.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub reason: String,
}

/// Construct a signed-shape `key.revoke` event for the given actor.
/// Mirrors `new_finding_event` in shape but targets an actor and
/// carries a `RevocationPayload` in `payload`. The returned event is
/// unsigned (caller signs it); `event.id` is the canonical content
/// address of the unsigned shape.
pub fn new_revocation_event(
    actor_id: &str,
    actor_type: &str,
    payload: RevocationPayload,
    reason: &str,
    before_hash: &str,
    after_hash: &str,
) -> StateEvent {
    let timestamp = Utc::now().to_rfc3339();
    let payload_value =
        serde_json::to_value(&payload).expect("RevocationPayload serializes to a JSON object");
    let mut event = StateEvent {
        schema: EVENT_SCHEMA.to_string(),
        id: String::new(),
        kind: EVENT_KIND_KEY_REVOKE.into(),
        target: StateTarget {
            r#type: "actor".to_string(),
            id: actor_id.to_string(),
        },
        actor: StateActor {
            id: actor_id.to_string(),
            r#type: actor_type.to_string(),
        },
        timestamp,
        reason: reason.to_string(),
        before_hash: before_hash.to_string(),
        after_hash: after_hash.to_string(),
        payload: payload_value,
        caveats: Vec::new(),
        signature: None,
    };
    event.id = event_id(&event);
    event
}

/// Payload of a `review.accepted` / `review.rejected` /
/// `review.revision_requested` event. Records WHICH proposal was decided
/// and HOW, binding the decision to the exact proposal by its
/// content-addressed id (`vpr_…`). The event's `actor` is the deciding
/// reviewer; the event's `signature` (set by the caller) is the
/// non-repudiable proof the key holder made the call. `applied_event_id`
/// is set only for accepts and points at the domain event the accept
/// graduated into.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ReviewDecisionPayload {
    /// The proposal this decision applies to (`vpr_…`, content-addressed).
    pub proposal_id: String,
    /// The proposal's kind (e.g. `finding.add`), copied for legibility so
    /// a reviewer reading the log need not cross-reference the proposal.
    pub proposal_kind: String,
    /// `accepted` | `rejected` | `revision_requested`. Redundant with the
    /// event kind but explicit in the payload so consumers that index by
    /// payload need not parse the kind string.
    pub verdict: String,
    /// For accepts: the domain event id (`vev_…`) the accept produced.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub applied_event_id: Option<String>,
}

/// Exact authorization material carried by `proposal.withdrawn`.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ProposalWithdrawalPayload {
    pub schema: String,
    pub proposal_id: String,
    pub proposal_root: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub submission_root: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub receipt_root: Option<String>,
    pub identity_binding_id: String,
}

/// Construct an unsigned producer-withdrawal event. The caller verifies the
/// Submission-bound producer identity and applies the ordinary event signature.
pub fn new_proposal_withdrawal_event(
    payload: ProposalWithdrawalPayload,
    actor_id: &str,
    reason: &str,
    timestamp: Option<&str>,
) -> Result<StateEvent, String> {
    if !matches!(
        payload.schema.as_str(),
        PROPOSAL_WITHDRAWAL_PAYLOAD_SCHEMA | PROPOSAL_WITHDRAWAL_PAYLOAD_SCHEMA_V1
    ) {
        return Err(format!(
            "proposal withdrawal payload schema must be {PROPOSAL_WITHDRAWAL_PAYLOAD_SCHEMA} or {PROPOSAL_WITHDRAWAL_PAYLOAD_SCHEMA_V1}"
        ));
    }
    if !payload.proposal_id.starts_with("vpr_") {
        return Err("proposal withdrawal requires a vpr_ proposal id".to_string());
    }
    let producer_root = match payload.schema.as_str() {
        PROPOSAL_WITHDRAWAL_PAYLOAD_SCHEMA => match (
            payload.submission_root.as_deref(),
            payload.receipt_root.as_deref(),
        ) {
            (Some(root), None) => ("submission_root", root),
            _ => {
                return Err(
                    "proposal withdrawal v2 requires submission_root and forbids receipt_root"
                        .to_string(),
                );
            }
        },
        PROPOSAL_WITHDRAWAL_PAYLOAD_SCHEMA_V1 => match (
            payload.receipt_root.as_deref(),
            payload.submission_root.as_deref(),
        ) {
            (Some(root), None) => ("receipt_root", root),
            _ => {
                return Err(
                    "proposal withdrawal v1 requires receipt_root and forbids submission_root"
                        .to_string(),
                );
            }
        },
        _ => unreachable!("schema was closed above"),
    };
    for (name, root) in [
        ("proposal_root", payload.proposal_root.as_str()),
        producer_root,
    ] {
        if !root.strip_prefix("sha256:").is_some_and(|hex| {
            hex.len() == 64
                && hex
                    .bytes()
                    .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
        }) {
            return Err(format!(
                "proposal withdrawal {name} must be a full sha256 root"
            ));
        }
    }
    if !payload
        .identity_binding_id
        .strip_prefix("vib_")
        .is_some_and(|hex| {
            hex.len() == 16
                && hex
                    .bytes()
                    .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
        })
    {
        return Err(
            "proposal withdrawal identity_binding_id must be vib_<16 lowercase hex>".to_string(),
        );
    }
    if actor_kind(actor_id) != "agent" {
        return Err("only an agent or ci producer may withdraw its proposal".to_string());
    }
    if reason.trim().is_empty() {
        return Err("proposal withdrawal reason must be non-empty".to_string());
    }
    let timestamp = timestamp
        .map(str::to_string)
        .unwrap_or_else(|| Utc::now().to_rfc3339());
    let mut event = StateEvent {
        schema: EVENT_SCHEMA.to_string(),
        id: String::new(),
        kind: EVENT_KIND_PROPOSAL_WITHDRAWN.into(),
        target: StateTarget {
            r#type: "proposal".to_string(),
            id: payload.proposal_id.clone(),
        },
        actor: StateActor {
            id: actor_id.to_string(),
            r#type: "agent".to_string(),
        },
        timestamp,
        reason: reason.to_string(),
        before_hash: NULL_HASH.to_string(),
        after_hash: NULL_HASH.to_string(),
        payload: serde_json::to_value(payload)
            .map_err(|error| format!("serialize proposal withdrawal payload: {error}"))?,
        caveats: Vec::new(),
        signature: None,
    };
    event.id = event_id(&event);
    Ok(event)
}

/// Construct an unsigned `review.*` event for a proposal decision. The
/// event is a side-table record (`before_hash == after_hash ==
/// NULL_HASH`), so it is transparent to the per-finding hash chain. The
/// caller signs it (under the reviewer key) before persisting; `event.id`
/// is the content address of the unsigned shape, so signing never changes
/// the id. `verdict` must be one of `accepted` / `rejected` /
/// `revision_requested` and selects the event kind.
pub fn new_review_decision_event(
    proposal_id: &str,
    proposal_kind: &str,
    verdict: &str,
    applied_event_id: Option<String>,
    reviewer_id: &str,
    reason: &str,
    timestamp: Option<&str>,
) -> Result<StateEvent, String> {
    let kind = match verdict {
        "accepted" => EVENT_KIND_REVIEW_ACCEPTED,
        "rejected" => EVENT_KIND_REVIEW_REJECTED,
        "revision_requested" => EVENT_KIND_REVIEW_REVISION_REQUESTED,
        other => return Err(format!("unknown review verdict '{other}'")),
    };
    let payload = ReviewDecisionPayload {
        proposal_id: proposal_id.to_string(),
        proposal_kind: proposal_kind.to_string(),
        verdict: verdict.to_string(),
        applied_event_id,
    };
    let payload_value =
        serde_json::to_value(&payload).expect("ReviewDecisionPayload serializes to a JSON object");
    let timestamp = timestamp
        .map(|t| t.to_string())
        .unwrap_or_else(|| Utc::now().to_rfc3339());
    let mut event = StateEvent {
        schema: EVENT_SCHEMA.to_string(),
        id: String::new(),
        kind: kind.into(),
        target: StateTarget {
            r#type: "proposal".to_string(),
            id: proposal_id.to_string(),
        },
        actor: StateActor {
            id: reviewer_id.to_string(),
            r#type: actor_kind(reviewer_id).to_string(),
        },
        timestamp,
        reason: reason.to_string(),
        before_hash: NULL_HASH.to_string(),
        after_hash: NULL_HASH.to_string(),
        payload: payload_value,
        caveats: Vec::new(),
        signature: None,
    };
    event.id = event_id(&event);
    Ok(event)
}

/// Construct an `evidence_atom.locator_repaired` event targeting an
/// evidence atom by id. The payload carries the resolved locator and
/// the parent source id so a fresh replay can both apply the repair
/// and reconstruct its derivation. Returned event is unsigned; the
/// caller signs it before persisting.
pub fn new_evidence_atom_locator_repair_event(
    atom_id: &str,
    actor_id: &str,
    actor_type: &str,
    reason: &str,
    before_hash: &str,
    after_hash: &str,
    payload: Value,
    caveats: Vec<String>,
) -> StateEvent {
    let timestamp = Utc::now().to_rfc3339();
    let mut event = StateEvent {
        schema: EVENT_SCHEMA.to_string(),
        id: String::new(),
        kind: EVENT_KIND_EVIDENCE_ATOM_LOCATOR_REPAIRED.into(),
        target: StateTarget {
            r#type: "evidence_atom".to_string(),
            id: atom_id.to_string(),
        },
        actor: StateActor {
            id: actor_id.to_string(),
            r#type: actor_type.to_string(),
        },
        timestamp,
        reason: reason.to_string(),
        before_hash: before_hash.to_string(),
        after_hash: after_hash.to_string(),
        payload,
        caveats,
        signature: None,
    };
    event.id = event_id(&event);
    event
}

/// T7: build a `contradiction.resolved` event. Target is the
/// contradiction's content-addressed id (`vcx_*`); the full resolved
/// object travels in `payload.contradiction`. The reducer arm upserts
/// it into `Project.contradictions`.
pub fn new_contradiction_resolved_event(
    contradiction_id: &str,
    actor_id: &str,
    actor_type: &str,
    reason: &str,
    payload: Value,
    caveats: Vec<String>,
) -> StateEvent {
    let timestamp = Utc::now().to_rfc3339();
    let mut event = StateEvent {
        schema: EVENT_SCHEMA.to_string(),
        id: String::new(),
        kind: EVENT_KIND_CONTRADICTION_RESOLVED.into(),
        target: StateTarget {
            r#type: "contradiction".to_string(),
            id: contradiction_id.to_string(),
        },
        actor: StateActor {
            id: actor_id.to_string(),
            r#type: actor_type.to_string(),
        },
        timestamp,
        reason: reason.to_string(),
        before_hash: NULL_HASH.to_string(),
        after_hash: NULL_HASH.to_string(),
        payload,
        caveats,
        signature: None,
    };
    event.id = event_id(&event);
    event
}

/// Shared builder for the attempt-lifecycle events (`attempt.deposited` /
/// `attempt.resolved`): both target an attempt's `vat_` id and differ only in
/// `kind` and the payload they carry.
fn new_attempt_event(
    kind: &str,
    attempt_id: &str,
    actor_id: &str,
    actor_type: &str,
    reason: &str,
    payload: Value,
    caveats: Vec<String>,
) -> StateEvent {
    let mut event = StateEvent {
        schema: EVENT_SCHEMA.to_string(),
        id: String::new(),
        kind: kind.into(),
        target: StateTarget {
            r#type: "attempt".to_string(),
            id: attempt_id.to_string(),
        },
        actor: StateActor {
            id: actor_id.to_string(),
            r#type: actor_type.to_string(),
        },
        timestamp: Utc::now().to_rfc3339(),
        reason: reason.to_string(),
        before_hash: NULL_HASH.to_string(),
        after_hash: NULL_HASH.to_string(),
        payload,
        caveats,
        signature: None,
    };
    event.id = event_id(&event);
    event
}

/// Build an `attempt.deposited` event. The full signed object travels in
/// `payload.attempt`; the reducer verifies it and upserts into
/// `Project.attempts`.
pub fn new_attempt_deposited_event(
    attempt_id: &str,
    actor_id: &str,
    actor_type: &str,
    reason: &str,
    payload: Value,
    caveats: Vec<String>,
) -> StateEvent {
    new_attempt_event(
        EVENT_KIND_ATTEMPT_DEPOSITED,
        attempt_id,
        actor_id,
        actor_type,
        reason,
        payload,
        caveats,
    )
}

/// Build a `transfer.deposited` event. The full signed [`crate::transfer::Transfer`]
/// travels in `payload.transfer`; the reducer verifies it and upserts into
/// `Project.transfers`.
pub fn new_transfer_deposited_event(
    transfer_id: &str,
    actor_id: &str,
    actor_type: &str,
    reason: &str,
    payload: Value,
    caveats: Vec<String>,
) -> StateEvent {
    let mut event = StateEvent {
        schema: EVENT_SCHEMA.to_string(),
        id: String::new(),
        kind: EVENT_KIND_TRANSFER_DEPOSITED.into(),
        target: StateTarget {
            r#type: "transfer".to_string(),
            id: transfer_id.to_string(),
        },
        actor: StateActor {
            id: actor_id.to_string(),
            r#type: actor_type.to_string(),
        },
        timestamp: Utc::now().to_rfc3339(),
        reason: reason.to_string(),
        before_hash: NULL_HASH.to_string(),
        after_hash: NULL_HASH.to_string(),
        payload,
        caveats,
        signature: None,
    };
    event.id = event_id(&event);
    event
}

/// Build an `endorsement.deposited` event. The signed
/// [`crate::endorsement::Endorsement`] travels in `payload.endorsement`; the
/// reducer verifies it and upserts into `Project.endorsements`.
pub fn new_endorsement_deposited_event(
    endorsement_id: &str,
    actor_id: &str,
    actor_type: &str,
    reason: &str,
    payload: Value,
    caveats: Vec<String>,
) -> StateEvent {
    let mut event = StateEvent {
        schema: EVENT_SCHEMA.to_string(),
        id: String::new(),
        kind: EVENT_KIND_ENDORSEMENT_DEPOSITED.into(),
        target: StateTarget {
            r#type: "endorsement".to_string(),
            id: endorsement_id.to_string(),
        },
        actor: StateActor {
            id: actor_id.to_string(),
            r#type: actor_type.to_string(),
        },
        timestamp: Utc::now().to_rfc3339(),
        reason: reason.to_string(),
        before_hash: NULL_HASH.to_string(),
        after_hash: NULL_HASH.to_string(),
        payload,
        caveats,
        signature: None,
    };
    event.id = event_id(&event);
    event
}

/// Build an `attempt.resolved` event. The [`crate::attempt::ResolutionEvent`]
/// travels in `payload.resolution`; the reducer upserts it into
/// `Project.attempt_resolutions` (idempotent by `vre_` id).
pub fn new_attempt_resolved_event(
    attempt_id: &str,
    actor_id: &str,
    actor_type: &str,
    reason: &str,
    payload: Value,
    caveats: Vec<String>,
) -> StateEvent {
    new_attempt_event(
        EVENT_KIND_ATTEMPT_RESOLVED,
        attempt_id,
        actor_id,
        actor_type,
        reason,
        payload,
        caveats,
    )
}

/// Canonical hash of one evidence atom. Mirrors `finding_hash` for the
/// before/after pair on locator-repair events so a chain validator can
/// confirm that exactly the named atom changed and exactly the named
/// repair was applied.
pub fn evidence_atom_hash(atom: &crate::sources::EvidenceAtom) -> String {
    let bytes = canonical::to_canonical_bytes(atom).unwrap_or_default();
    format!("sha256:{}", hex::encode(Sha256::digest(bytes)))
}
pub fn finding_hash(finding: &FindingBundle) -> String {
    // Per Protocol §5, links are "review surfaces" — typed relationships
    // between findings inferred at compile or review time, NOT part of the
    // finding's content commitment. They are mutable: `vela link add`
    // appends links without emitting a state-event (links don't change
    // what the finding asserts; they change which findings know about
    // each other). For event-replay validity the finding hash must therefore
    // exclude `links`, otherwise any CLI-added link breaks the asserted-event
    // chain. v0.12: hash a links-cleared copy. State-changing events
    // (caveat/note/review/revise/retract) still mutate annotations/flags/
    // confidence — those remain in the hash and chain through events properly.
    let mut hashable = finding.clone();
    hashable.links.clear();
    let bytes = canonical::to_canonical_bytes(&hashable).unwrap_or_default();
    format!("sha256:{}", hex::encode(Sha256::digest(bytes)))
}

pub fn finding_hash_by_id(frontier: &Project, finding_id: &str) -> String {
    frontier
        .findings
        .iter()
        .find(|finding| finding.id == finding_id)
        .map(finding_hash)
        .unwrap_or_else(|| NULL_HASH.to_string())
}

/// The integrity commitment over an event log.
///
/// Canonical order is the content-addressed event `id`, so the hash is
/// INDEPENDENT of how the events were loaded: the packet path (authored
/// array order), the `.vela/events/` directory path (filename
/// `vev_<id>.json`, i.e. id order), and a future one-file-per-event git
/// layout all canonicalize to the same sequence. This is the property the
/// git-backed migration needs (ADR 0001): committing events as individual
/// files must not redefine the hash. Sorting by `id` (rather than the
/// causal `(timestamp, id)` replay order) is also what keeps existing
/// signed locks byte-stable, because the directory loader already yields
/// id order. Causal REPLAY order is enforced separately in
/// `reducer::sorted_for_replay`; this function is only the commitment.
pub fn event_log_hash(events: &[StateEvent]) -> String {
    let mut sorted: Vec<&StateEvent> = events.iter().collect();
    sorted.sort_by(|a, b| a.id.cmp(&b.id));
    // Content-only: the log hash binds what the events SAY, not who signed them.
    // The `signature` field is stripped before hashing, so re-signing (the v0->v1
    // flip, a key rotation) never perturbs `event_log_hash`. Signing is therefore
    // orthogonal to content-addressing, the same discipline `snapshot_hash`
    // already follows (it removes the top-level `signatures` key). The signatures
    // still live on the events on disk; they are simply not part of this digest.
    let stripped: Vec<serde_json::Value> = sorted
        .iter()
        .map(|event| {
            let mut value = serde_json::to_value(event).unwrap_or(serde_json::Value::Null);
            if let serde_json::Value::Object(map) = &mut value {
                map.remove("signature");
            }
            value
        })
        .collect();
    let bytes = canonical::to_canonical_bytes(&stripped).unwrap_or_default();
    hex::encode(Sha256::digest(bytes))
}

/// Commit to every canonical event except the signed coordination leases used
/// by `vela start`.
///
/// Proof exports describe scientific and authority state. A later
/// `attempt.claimed` event changes who is coordinating work, but it does not
/// change that proof subject. Keep this exclusion closed to the one named
/// event kind: every scientific, provenance, authority, and unknown future
/// event remains in the commitment and therefore stales the proof.
pub fn nonlease_event_log_hash(events: &[StateEvent]) -> String {
    let nonlease = events
        .iter()
        .filter(|event| event.kind != EVENT_KIND_ATTEMPT_CLAIMED)
        .cloned()
        .collect::<Vec<_>>();
    event_log_hash(&nonlease)
}

pub fn snapshot_hash(frontier: &Project) -> String {
    let value = serde_json::to_value(frontier).unwrap_or(Value::Null);
    let mut value = value;
    if let Value::Object(map) = &mut value {
        map.remove("events");
        map.remove("signatures");
        map.remove("proof_state");
    }
    let bytes = canonical::to_canonical_bytes(&value).unwrap_or_default();
    hex::encode(Sha256::digest(bytes))
}

pub fn events_for_finding<'a>(frontier: &'a Project, finding_id: &str) -> Vec<&'a StateEvent> {
    frontier
        .events
        .iter()
        .filter(|event| event.target.r#type == "finding" && event.target.id == finding_id)
        .collect()
}

pub fn replay_report(frontier: &Project) -> ReplayReport {
    let event_log = summarize(frontier);
    let mut conflicts = Vec::new();

    if frontier.events.is_empty() {
        let current_hash = snapshot_hash(frontier);
        return ReplayReport {
            ok: true,
            status: "no_events".to_string(),
            event_log,
            source_hash: current_hash.clone(),
            event_log_hash: event_log_hash(&frontier.events),
            replayed_hash: current_hash.clone(),
            current_hash,
            conflicts,
        };
    }

    for duplicate in &event_log.duplicate_ids {
        conflicts.push(format!("duplicate event id: {duplicate}"));
    }
    for orphan in &event_log.orphan_targets {
        conflicts.push(format!("orphan event target: {orphan}"));
    }
    conflicts.extend(
        crate::frontier_repository::validate_repository_boundary_event_set(&frontier.events),
    );

    let mut chains = BTreeMap::<String, Vec<&StateEvent>>::new();
    for event in &frontier.events {
        if event.schema != EVENT_SCHEMA {
            conflicts.push(format!(
                "unsupported event schema for {}: {}",
                event.id, event.schema
            ));
        }
        if event.reason.trim().is_empty() {
            conflicts.push(format!("event {} has empty reason", event.id));
        }
        if event.before_hash.trim().is_empty() || event.after_hash.trim().is_empty() {
            conflicts.push(format!("event {} has empty hash boundary", event.id));
        }
        // Phase E: per-kind payload schema validation. Each event kind has
        // a normative payload shape documented in `docs/PROTOCOL.md` §6;
        // payloads that don't match are conformance failures, not just
        // "weird optional content."
        if let Err(err) = validate_event_payload(event.kind.as_str(), &event.payload) {
            conflicts.push(format!("event {} payload invalid: {err}", event.id));
        }
        // Side-table events (statement.registered, attempt.deposited, …)
        // are minted with before_hash == after_hash == NULL_HASH: they
        // record activity against a finding without transitioning its
        // state, so they are transparent to the per-finding hash chain.
        // Including them would break the chain between the real
        // transitions on either side.
        if event.before_hash == NULL_HASH && event.after_hash == NULL_HASH {
            continue;
        }
        chains
            .entry(format!("{}:{}", event.target.r#type, event.target.id))
            .or_default()
            .push(event);
    }

    for (target, events) in chains {
        let mut sorted = events;
        sorted.sort_by(|a, b| a.timestamp.cmp(&b.timestamp).then(a.id.cmp(&b.id)));
        for pair in sorted.windows(2) {
            let previous = pair[0];
            let next = pair[1];
            if previous.after_hash != next.before_hash {
                conflicts.push(format!(
                    "event chain break for {target}: {} after_hash does not match {} before_hash",
                    previous.id, next.id
                ));
            }
        }
        if let Some(last) = sorted.last()
            && last.target.r#type == "finding"
        {
            let current = finding_hash_by_id(frontier, &last.target.id);
            if current != last.after_hash {
                conflicts.push(format!(
                    "materialized finding {} hash does not match last event {}",
                    last.target.id, last.id
                ));
            }
        }
    }

    let current_hash = snapshot_hash(frontier);
    let ok = conflicts.is_empty();
    ReplayReport {
        ok,
        status: if ok { "ok" } else { "conflict" }.to_string(),
        event_log,
        source_hash: current_hash.clone(),
        event_log_hash: event_log_hash(&frontier.events),
        replayed_hash: if ok {
            current_hash.clone()
        } else {
            "unavailable".to_string()
        },
        current_hash,
        conflicts,
    }
}
pub fn summarize(frontier: &Project) -> EventLogSummary {
    let mut kinds = BTreeMap::<String, usize>::new();
    let mut seen = BTreeSet::<String>::new();
    let mut duplicate_ids = BTreeSet::<String>::new();
    let finding_ids = frontier
        .findings
        .iter()
        .map(|finding| finding.id.as_str())
        .collect::<BTreeSet<_>>();
    let mut orphan_targets = BTreeSet::<String>::new();
    let mut timestamps = Vec::<String>::new();

    for event in &frontier.events {
        *kinds.entry(event.kind.to_string()).or_default() += 1;
        if !seen.insert(event.id.clone()) {
            duplicate_ids.insert(event.id.clone());
        }
        // An `attempt.claimed` lease may target a namespaced EXTERNAL
        // obligation (e.g. `erdos:443` — work on a problem that has no
        // finding yet). Coordination, not truth: not an orphan.
        let external_obligation = event.kind == "attempt.claimed"
            && event.target.id.contains(':')
            && !event.target.id.starts_with("vf_");
        let pending_proposal_evidence = event.kind == EVENT_KIND_VERIFIER_ATTACHMENT_ADDED
            && event
                .payload
                .get("proposal_id")
                .and_then(Value::as_str)
                .is_some_and(|proposal_id| {
                    frontier.proposals.iter().any(|proposal| {
                        proposal.id == proposal_id
                            && proposal.status == "pending_review"
                            && proposal.target.id == event.target.id
                    })
                });
        if event.target.r#type == "finding"
            && !finding_ids.contains(event.target.id.as_str())
            && event.kind != "finding.retracted"
            && !external_obligation
            && !pending_proposal_evidence
        {
            orphan_targets.insert(event.target.id.clone());
        }
        timestamps.push(event.timestamp.clone());
    }
    timestamps.sort();

    EventLogSummary {
        count: frontier.events.len(),
        kinds,
        first_timestamp: timestamps.first().cloned(),
        last_timestamp: timestamps.last().cloned(),
        duplicate_ids: duplicate_ids.into_iter().collect(),
        orphan_targets: orphan_targets.into_iter().collect(),
    }
}

/// Validate a canonical event's payload against its per-kind schema.
///
/// Each event kind has a normative payload shape. Phase E pins those
/// shapes so a second implementation can reject malformed events
/// without per-kind ad-hoc parsing. The schemas are documented in
/// `docs/PROTOCOL.md` §6 and conformance-checked at the v0.3 level.
///
/// Unknown kinds are rejected so future-event-kind reads from older
/// implementations fail fast rather than silently accepting opaque
/// content.
fn validate_sha256_commitment(field: &str, value: &str) -> Result<(), String> {
    let hex = value.strip_prefix("sha256:").unwrap_or(value);
    if hex.len() != 64 || !hex.chars().all(|c| c.is_ascii_hexdigit()) {
        return Err(format!("{field} must be sha256:<64hex>"));
    }
    Ok(())
}

pub fn validate_event_payload(kind: &str, payload: &Value) -> Result<(), String> {
    let object = payload.as_object().ok_or_else(|| {
        if matches!(payload, Value::Null) {
            "payload must be a JSON object (got null)".to_string()
        } else {
            "payload must be a JSON object".to_string()
        }
    })?;
    let require_str = |key: &str| -> Result<&str, String> {
        object
            .get(key)
            .and_then(Value::as_str)
            .ok_or_else(|| format!("missing required string field '{key}'"))
    };
    let require_f64 = |key: &str| -> Result<f64, String> {
        object
            .get(key)
            .and_then(Value::as_f64)
            .ok_or_else(|| format!("missing required number field '{key}'"))
    };
    match kind {
        "finding.asserted" => {
            // proposal_id required; optional `finding` for v0.3 genesis
            // events that carry the bootstrap finding inline.
            require_str("proposal_id")?;
        }
        "finding.reviewed" => {
            require_str("proposal_id")?;
            let status = require_str("status")?;
            if !matches!(
                status,
                "accepted" | "approved" | "contested" | "needs_revision" | "rejected"
            ) {
                return Err(format!("invalid review status '{status}'"));
            }
        }
        "diff_pack.released" => {
            let pack_id = require_str("pack_id")?;
            if !pack_id.starts_with("vsd_") {
                return Err(format!(
                    "payload.pack_id must start with 'vsd_', got '{pack_id}'"
                ));
            }
            let frontier_id = require_str("frontier_id")?;
            if !frontier_id.starts_with("vfr_") {
                return Err(format!(
                    "payload.frontier_id must start with 'vfr_', got '{frontier_id}'"
                ));
            }
            let summary = require_str("summary")?;
            if summary.trim().is_empty() {
                return Err("payload.summary must be non-empty".to_string());
            }
            let aggregate_kind = require_str("aggregate_kind")?;
            if aggregate_kind.trim().is_empty() {
                return Err("payload.aggregate_kind must be non-empty".to_string());
            }
        }
        "diff_pack.reviewed" => {
            let pack_id = require_str("pack_id")?;
            if !pack_id.starts_with("vsd_") {
                return Err(format!(
                    "payload.pack_id must start with 'vsd_', got '{pack_id}'"
                ));
            }
            let verdict = require_str("verdict")?;
            if !matches!(verdict, "accept" | "reject" | "revise") {
                return Err(format!(
                    "payload.verdict must be accept|reject|revise, got '{verdict}'"
                ));
            }
            let reviewer = require_str("reviewer_actor")?;
            if reviewer.trim().is_empty() {
                return Err("payload.reviewer_actor must be non-empty".to_string());
            }
            let reason = require_str("reason")?;
            if reason.trim().is_empty() {
                return Err("payload.reason must be non-empty".to_string());
            }
            if let Some(applied) = object.get("applied_members") {
                applied
                    .as_array()
                    .ok_or("payload.applied_members must be an array when present")?;
            }
            if let Some(sdk_only) = object.get("sdk_only_members") {
                sdk_only
                    .as_array()
                    .ok_or("payload.sdk_only_members must be an array when present")?;
            }
        }
        "finding.contribution.recorded" => {
            let contribution = object
                .get("contribution")
                .and_then(Value::as_object)
                .ok_or("payload.contribution must be a JSON object")?;
            for field in ["unit", "unit_type", "agent_kind", "agent_id", "role"] {
                let v = contribution
                    .get(field)
                    .and_then(Value::as_str)
                    .ok_or_else(|| format!("payload.contribution.{field} must be a string"))?;
                if v.trim().is_empty() {
                    return Err(format!("payload.contribution.{field} must be non-empty"));
                }
            }
            // The trust invariant, enforced at the wire boundary: `vouched` is a
            // human role. A model or agent can never vouch.
            if contribution.get("role").and_then(Value::as_str) == Some("vouched")
                && contribution.get("agent_kind").and_then(Value::as_str) != Some("human")
            {
                return Err(
                    "payload.contribution: role `vouched` requires agent_kind `human`".to_string(),
                );
            }
        }
        "finding.noted" | "finding.caveated" => {
            require_str("proposal_id")?;
            require_str("annotation_id")?;
            let text = require_str("text")?;
            if text.trim().is_empty() {
                return Err("payload.text must be non-empty".to_string());
            }
            // Phase β (v0.6): optional structured `provenance` block.
            // When present, MUST be an object and MUST carry at least one
            // identifying field (doi/pmid/title). An all-empty
            // `provenance: {}` is a contract violation, not a tolerable
            // default — agents that pass the field are expected to mean it.
            if let Some(prov) = object.get("provenance") {
                let prov_obj = prov
                    .as_object()
                    .ok_or("payload.provenance must be a JSON object when present")?;
                let has_id = prov_obj
                    .get("doi")
                    .and_then(Value::as_str)
                    .is_some_and(|s| !s.trim().is_empty())
                    || prov_obj
                        .get("pmid")
                        .and_then(Value::as_str)
                        .is_some_and(|s| !s.trim().is_empty())
                    || prov_obj
                        .get("title")
                        .and_then(Value::as_str)
                        .is_some_and(|s| !s.trim().is_empty());
                if !has_id {
                    return Err(
                        "payload.provenance must include at least one of doi/pmid/title"
                            .to_string(),
                    );
                }
            }
        }
        "source_text.reviewed" => {
            require_str("proposal_id")?;
            let status = require_str("status")?;
            if !matches!(status, "accepted" | "approved") {
                return Err(format!("invalid source text review status '{status}'"));
            }
            let promotion_id = require_str("source_text_promotion_id")?;
            if !promotion_id.starts_with("vslaketxtprom_") {
                return Err(format!(
                    "payload.source_text_promotion_id must start with 'vslaketxtprom_', got '{promotion_id}'"
                ));
            }
            let source_lake_record_id = require_str("source_lake_record_id")?;
            if !source_lake_record_id.starts_with("vslake_") {
                return Err(format!(
                    "payload.source_lake_record_id must start with 'vslake_', got '{source_lake_record_id}'"
                ));
            }
            require_str("lane_id")?;
            require_str("locator")?;
            require_str("materialized_from")?;
        }
        "finding.confidence_revised" => {
            require_str("proposal_id")?;
            let new_score = require_f64("new_score")?;
            if !(0.0..=1.0).contains(&new_score) {
                return Err(format!("new_score {new_score} out of [0.0, 1.0]"));
            }
            let _ = require_f64("previous_score")?;
        }
        "finding.rejected" => {
            require_str("proposal_id")?;
        }
        "finding.superseded" => {
            require_str("proposal_id")?;
            require_str("new_finding_id")?;
        }
        "finding.retracted" => {
            require_str("proposal_id")?;
            // affected and cascade are summary fields; optional but if
            // present, affected must be a non-negative integer.
            if let Some(affected) = object.get("affected") {
                let _ = affected
                    .as_u64()
                    .ok_or("affected must be a non-negative integer")?;
            }
        }
        // Phase L: per-dependent cascade events. Each one names the
        // upstream retraction it descends from, the cascade depth, and
        // the canonical event ID of the source retraction so a replay
        // can reconstruct the cascade without trusting summary fields.
        "finding.dependency_invalidated" => {
            require_str("upstream_finding_id")?;
            require_str("upstream_event_id")?;
            let depth = object
                .get("depth")
                .and_then(Value::as_u64)
                .ok_or("missing required positive integer 'depth'")?;
            if depth == 0 {
                return Err("depth must be >= 1 (genesis is the source retraction)".to_string());
            }
            // proposal_id present for cascade-source traceability.
            require_str("proposal_id")?;
        }
        "frontier.created" => {
            if object.get("schema").and_then(Value::as_str) == Some(FRONTIER_CREATED_SCHEMA_V1) {
                const FIELDS: &[&str] = &[
                    "schema",
                    "name_at_creation",
                    "creator",
                    "profile_schema",
                    "dependency_root",
                    "created_at",
                ];
                if object.len() != FIELDS.len()
                    || object.keys().any(|key| !FIELDS.contains(&key.as_str()))
                {
                    return Err(format!(
                        "{FRONTIER_CREATED_SCHEMA_V1} payload is closed; expected exactly {}",
                        FIELDS.join(", ")
                    ));
                }
                require_str("name_at_creation")?;
                require_str("creator")?;
                if require_str("profile_schema")? != "vela.frontier-profile.v1" {
                    return Err(
                        "payload.profile_schema must be vela.frontier-profile.v1".to_string()
                    );
                }
                let dependency_root = require_str("dependency_root")?;
                if !crate::receipt_v1::is_full_sha256_root(dependency_root) {
                    return Err(
                        "payload.dependency_root must use sha256:<64 lowercase hex>".to_string()
                    );
                }
                chrono::DateTime::parse_from_rfc3339(require_str("created_at")?).map_err(
                    |error| format!("payload.created_at must be an RFC3339 timestamp: {error}"),
                )?;
            } else {
                // Historical structural genesis events predate the closed v1
                // payload. They remain replayable, but only the v1 shape may
                // establish a Profile v1 repository identity.
                require_str("name")?;
                require_str("creator")?;
            }
        }
        "frontier.observation_reviewed" => {
            require_str("proposal_id")?;
            let proposal_kind = require_str("proposal_kind")?;
            if !matches!(
                proposal_kind,
                "research_trace.review" | "correction_return.review"
            ) {
                return Err(format!(
                    "payload.proposal_kind must be research_trace.review or correction_return.review, got '{proposal_kind}'"
                ));
            }
            let status = require_str("status")?;
            if status != "accepted" {
                return Err(format!("payload.status must be 'accepted', got '{status}'"));
            }
            if let Some(value) = object.get("decision_reason")
                && value.as_str().is_none_or(|reason| reason.trim().is_empty())
            {
                return Err(
                    "payload.decision_reason must be a non-empty string when present".to_string(),
                );
            }
        }
        // v0.38: causal-typing reinterpretation. The substrate doesn't
        // erase the prior reading; it appends a new event recording who
        // re-graded the claim and why. `before` and `after` payloads
        // each carry `claim` (correlation|mediation|intervention) and
        // optionally `grade` (rct|quasi_experimental|observational|
        // theoretical). Pre-v0.38 findings carried neither, so a
        // reinterpretation may originate from a block with both fields
        // absent or null.
        "assertion.reinterpreted_causal" => {
            require_str("proposal_id")?;
            let check_block = |block_name: &str| -> Result<(), String> {
                let block = object
                    .get(block_name)
                    .and_then(Value::as_object)
                    .ok_or_else(|| format!("payload.{block_name} must be an object"))?;
                if let Some(claim) = block.get("claim").and_then(Value::as_str)
                    && !crate::bundle::VALID_CAUSAL_CLAIMS.contains(&claim)
                {
                    return Err(format!(
                        "{block_name}.claim '{claim}' not in {:?}",
                        crate::bundle::VALID_CAUSAL_CLAIMS
                    ));
                }
                if let Some(grade) = block.get("grade").and_then(Value::as_str)
                    && !crate::bundle::VALID_CAUSAL_EVIDENCE_GRADES.contains(&grade)
                {
                    return Err(format!(
                        "{block_name}.grade '{grade}' not in {:?}",
                        crate::bundle::VALID_CAUSAL_EVIDENCE_GRADES
                    ));
                }
                Ok(())
            };
            check_block("before")?;
            check_block("after")?;
        }
        // v0.49: key revocation event. Carries the revoked Ed25519
        // pubkey (hex-encoded 64 chars), the ISO-8601 moment compromise
        // was detected, and an optional replacement pubkey + reason.
        // Validating here keeps a hand-emitted or peer-fetched
        // revocation honest at the event-pipeline boundary so a
        // malformed revocation can't slip through replay and silently
        // re-trust the compromised key.
        EVENT_KIND_KEY_REVOKE => {
            let revoked = require_str("revoked_pubkey")?;
            if revoked.len() != 64 || !revoked.chars().all(|c| c.is_ascii_hexdigit()) {
                return Err(format!(
                    "revoked_pubkey must be 64 hex chars (Ed25519 pubkey), got {} chars",
                    revoked.len()
                ));
            }
            let revoked_at = require_str("revoked_at")?;
            if revoked_at.trim().is_empty() {
                return Err("revoked_at must be a non-empty ISO-8601 timestamp".to_string());
            }
            // v0.49.1: parse as RFC-3339 / ISO-8601 so a typo'd value
            // ("yesterday", "x", "2026-13-99T...") fails at the
            // validator boundary rather than poisoning re-verification
            // of post-revocation signatures further downstream.
            if DateTime::parse_from_rfc3339(revoked_at).is_err() {
                return Err(format!(
                    "revoked_at must parse as RFC-3339/ISO-8601, got {revoked_at:?}"
                ));
            }
            // replacement_pubkey is optional but if present must be a
            // valid hex pubkey of the same shape — a typo here would
            // strand the actor's identity at the wrong forward key.
            if let Some(replacement) = object.get("replacement_pubkey")
                && let Some(rep_str) = replacement.as_str()
                && !rep_str.is_empty()
                && (rep_str.len() != 64 || !rep_str.chars().all(|c| c.is_ascii_hexdigit()))
            {
                return Err(format!(
                    "replacement_pubkey must be 64 hex chars when present, got {} chars",
                    rep_str.len()
                ));
            }
            // The revoked key cannot also be the replacement; that
            // would be a self-rotation that revokes nothing.
            if let Some(replacement) = object.get("replacement_pubkey").and_then(Value::as_str)
                && !replacement.is_empty()
                && replacement.eq_ignore_ascii_case(revoked)
            {
                return Err("replacement_pubkey must differ from revoked_pubkey".to_string());
            }
        }
        // Reviewer decision events. The proposal id binds the decision to
        // content (it is content-addressed); verdict must agree with the
        // kind so a consumer indexing by either field reads the same call.
        EVENT_KIND_REVIEW_ACCEPTED
        | EVENT_KIND_REVIEW_REJECTED
        | EVENT_KIND_REVIEW_REVISION_REQUESTED => {
            let proposal_id = require_str("proposal_id")?;
            if !proposal_id.starts_with("vpr_") {
                return Err(format!(
                    "payload.proposal_id must start with 'vpr_', got '{proposal_id}'"
                ));
            }
            require_str("proposal_kind")?;
            let verdict = require_str("verdict")?;
            let expected = match kind {
                EVENT_KIND_REVIEW_ACCEPTED => "accepted",
                EVENT_KIND_REVIEW_REJECTED => "rejected",
                _ => "revision_requested",
            };
            if verdict != expected {
                return Err(format!(
                    "review event kind '{kind}' requires verdict '{expected}', got '{verdict}'"
                ));
            }
            // applied_event_id only makes sense on an accept, and must be a
            // vev_ when present.
            if let Some(ev) = object.get("applied_event_id").and_then(Value::as_str) {
                if kind != EVENT_KIND_REVIEW_ACCEPTED {
                    return Err(format!(
                        "applied_event_id is only valid on '{EVENT_KIND_REVIEW_ACCEPTED}'"
                    ));
                }
                if !ev.starts_with("vev_") {
                    return Err(format!(
                        "payload.applied_event_id must start with 'vev_', got '{ev}'"
                    ));
                }
            }
        }
        EVENT_KIND_PROPOSAL_WITHDRAWN => {
            let parsed: ProposalWithdrawalPayload = serde_json::from_value(payload.clone())
                .map_err(|error| format!("invalid proposal-withdrawal payload: {error}"))?;
            // Reuse the constructor's closed shape and exact identifier/root
            // validation without admitting the resulting unsigned event.
            new_proposal_withdrawal_event(
                parsed,
                "agent:payload-validator",
                "validate proposal withdrawal payload",
                Some("1970-01-01T00:00:00Z"),
            )?;
        }
        EVENT_KIND_ACTOR_REGISTRATION_ACTIVATED => {
            let parsed: crate::actor_registration::ActorRegistrationBoundaryPayload =
                serde_json::from_value(payload.clone())
                    .map_err(|error| format!("invalid actor-registration payload: {error}"))?;
            parsed.validate()?;
        }
        EVENT_KIND_FRONTIER_REPOSITORY_BOUND => {
            let parsed: crate::frontier_repository::FrontierRepositoryBoundaryPayloadV1 =
                serde_json::from_value(payload.clone())
                    .map_err(|error| format!("invalid repository-boundary payload: {error}"))?;
            parsed.validate()?;
        }
        EVENT_KIND_ARTIFACT_ASSERTED => {
            require_str("proposal_id")?;
            let artifact = object
                .get("artifact")
                .and_then(Value::as_object)
                .ok_or("payload.artifact must be a JSON object")?;
            let id = artifact
                .get("id")
                .and_then(Value::as_str)
                .ok_or("payload.artifact.id must be a va_<hex>")?;
            if !id.starts_with("va_") {
                return Err(format!(
                    "payload.artifact.id must start with 'va_', got '{id}'"
                ));
            }
            let id_hex = id.trim_start_matches("va_");
            if id_hex.len() != 16 || !id_hex.chars().all(|c| c.is_ascii_hexdigit()) {
                return Err("payload.artifact.id must be va_<16hex>".to_string());
            }
            let kind = artifact
                .get("kind")
                .and_then(Value::as_str)
                .ok_or("payload.artifact.kind must be a string")?;
            if !crate::bundle::valid_artifact_kind(kind) {
                return Err(format!("payload.artifact.kind '{kind}' is not supported"));
            }
            for key in ["name", "content_hash", "storage_mode"] {
                let value = artifact
                    .get(key)
                    .and_then(Value::as_str)
                    .ok_or_else(|| format!("payload.artifact.{key} must be a string"))?;
                if value.trim().is_empty() {
                    return Err(format!("payload.artifact.{key} must be non-empty"));
                }
            }
            let content_hash = artifact
                .get("content_hash")
                .and_then(Value::as_str)
                .expect("content_hash checked above");
            validate_sha256_commitment("payload.artifact.content_hash", content_hash)?;
        }
        EVENT_KIND_ARTIFACT_REVIEWED => {
            require_str("proposal_id")?;
            let status = require_str("status")?;
            if !matches!(
                status,
                "accepted" | "approved" | "contested" | "needs_revision" | "rejected"
            ) {
                return Err(format!("invalid review status '{status}'"));
            }
        }
        EVENT_KIND_ARTIFACT_RETRACTED => {
            require_str("proposal_id")?;
        }
        // v0.51: Re-classify an object's read-side access tier.
        // Validates target.r#type up front so a `tier.set` event for
        // a non-tiered kernel object (dataset, source, etc.)
        // fails at the validator boundary rather than silently
        // succeeding under the reducer's match-or-noop.
        EVENT_KIND_TIER_SET => {
            require_str("proposal_id")?;
            let object_type = require_str("object_type")?;
            if !matches!(object_type, "finding" | "artifact") {
                return Err(format!(
                    "tier.set object_type '{object_type}' must be one of finding, artifact"
                ));
            }
            require_str("object_id")?;
            let new_tier = require_str("new_tier")?;
            crate::access_tier::AccessTier::parse(new_tier)?;
            // previous_tier is optional but if present must parse to
            // a valid tier so a stale or hand-edited event log can't
            // smuggle in `"prev"` strings the reducer would later
            // reject.
            if let Some(prev) = object.get("previous_tier").and_then(Value::as_str) {
                crate::access_tier::AccessTier::parse(prev)?;
            }
        }
        // v0.56: Mechanical evidence-atom locator repair. Required
        // payload shape: {proposal_id, source_id, locator}. The locator
        // string lands on the atom's `locator` field and the source_id
        // is recorded for traceability so a reader can reconstruct the
        // derivation without re-resolving the source registry.
        EVENT_KIND_EVIDENCE_ATOM_LOCATOR_REPAIRED => {
            require_str("proposal_id")?;
            let source_id = require_str("source_id")?;
            if source_id.trim().is_empty() {
                return Err("payload.source_id must be non-empty".to_string());
            }
            let locator = require_str("locator")?;
            if locator.trim().is_empty() {
                return Err("payload.locator must be non-empty".to_string());
            }
        }
        // v0.57: Append a `{section, text}` span to a finding's
        // evidence_spans. Required payload: {proposal_id, section, text}.
        EVENT_KIND_FINDING_SPAN_REPAIRED => {
            require_str("proposal_id")?;
            let section = require_str("section")?;
            if section.trim().is_empty() {
                return Err("payload.section must be non-empty".to_string());
            }
            let text = require_str("text")?;
            if text.trim().is_empty() {
                return Err("payload.text must be non-empty".to_string());
            }
        }
        // v0.79.4: Per-event attestation. Required payload:
        // `{target_event_id, attester_id, scope_note}`. Optional:
        // `scopes`, `reviewer_role`, `orcid`, `ror`,
        // `attestation_id`, `signature`, `proof_id` (vpf_*),
        // `signed_at`.
        EVENT_KIND_ATTESTATION_RECORDED => {
            let target_id = require_str("target_event_id")?;
            if !target_id.starts_with("vev_") {
                return Err(format!(
                    "payload.target_event_id must start with 'vev_', got '{target_id}'"
                ));
            }
            let attester = require_str("attester_id")?;
            if attester.trim().is_empty() {
                return Err("payload.attester_id must be non-empty".to_string());
            }
            let scope = require_str("scope_note")?;
            if scope.trim().is_empty() {
                return Err("payload.scope_note must be non-empty".to_string());
            }
            // Optional fields: type-check when present.
            if let Some(sig) = object.get("signature")
                && !sig.is_null()
                && !sig.is_string()
            {
                return Err("payload.signature must be a string when present".to_string());
            }
            if let Some(scopes) = object.get("scopes") {
                let Some(items) = scopes.as_array() else {
                    return Err("payload.scopes must be an array when present".to_string());
                };
                for scope in items {
                    let Some(scope) = scope.as_str() else {
                        return Err("payload.scopes entries must be strings".to_string());
                    };
                    match scope {
                        "source_extraction"
                        | "method_review"
                        | "statistical_review"
                        | "domain_relevance"
                        | "translation_clarity"
                        | "policy_approval" => {}
                        other => {
                            return Err(format!("payload.scopes contains unknown scope `{other}`"));
                        }
                    }
                }
            }
            for field in [
                "reviewer_role",
                "orcid",
                "ror",
                "attestation_id",
                "signed_at",
            ] {
                if let Some(value) = object.get(field)
                    && !value.is_null()
                    && !value.is_string()
                {
                    return Err(format!("payload.{field} must be a string when present"));
                }
            }
            if let Some(proof) = object.get("proof_id")
                && !proof.is_null()
                && let Some(s) = proof.as_str()
                && !s.starts_with("vpf_")
            {
                return Err(format!(
                    "payload.proof_id must start with 'vpf_' when present, got '{s}'"
                ));
            }
        }
        "verifier_attachment.added" => {
            // `proposal_id` is present when the attachment came through the
            // propose/accept flow, and absent when a verifier (e.g. CI) directly
            // signs its own evidence event. Evidence carries no signer-authority
            // claim, so it needs no human accept. Either way the embedded
            // attachment object is the load-bearing payload, and the reducer
            // re-verifies its content-addressed id on apply.
            if let Some(v) = object.get("proposal_id") {
                let Some(proposal_id) = v.as_str() else {
                    return Err(
                        "verifier_attachment.added payload.proposal_id must be a string when present"
                            .to_string(),
                    );
                };
                if !proposal_id.starts_with("vpr_") {
                    return Err(
                        "verifier_attachment.added payload.proposal_id must start with vpr_"
                            .to_string(),
                    );
                }
            }
            if !object.get("attachment").is_some_and(|v| v.is_object()) {
                return Err(
                    "verifier_attachment.added payload.attachment must be an object".to_string(),
                );
            }
        }
        // Loader=reducer parity: every kind the reducer replays must
        // also pass payload validation, or `vela check`'s event-replay
        // verdict reports valid history as conflict. These arms are
        // signature-pure shape checks; the reducer arms re-verify the
        // embedded objects' own ids/signatures on apply.
        "verdict_conflict.resolved" => {
            if !object.get("conflict").is_some_and(|v| v.is_object()) {
                return Err(
                    "verdict_conflict.resolved payload.conflict must be an object".to_string(),
                );
            }
        }
        "contradiction.resolved" => {
            if !object.get("contradiction").is_some_and(|v| v.is_object()) {
                return Err(
                    "contradiction.resolved payload.contradiction must be an object".to_string(),
                );
            }
        }
        EVENT_KIND_ATTEMPT_DEPOSITED => {
            if !object.get("attempt").is_some_and(|v| v.is_object()) {
                return Err("attempt.deposited payload.attempt must be an object".to_string());
            }
        }
        EVENT_KIND_TRANSFER_DEPOSITED => {
            if !object.get("transfer").is_some_and(|v| v.is_object()) {
                return Err("transfer.deposited payload.transfer must be an object".to_string());
            }
        }
        EVENT_KIND_ENDORSEMENT_DEPOSITED => {
            if !object.get("endorsement").is_some_and(|v| v.is_object()) {
                return Err(
                    "endorsement.deposited payload.endorsement must be an object".to_string(),
                );
            }
        }
        EVENT_KIND_ATTEMPT_RESOLVED => {
            if !object.get("resolution").is_some_and(|v| v.is_object()) {
                return Err("attempt.resolved payload.resolution must be an object".to_string());
            }
        }
        EVENT_KIND_STATEMENT_ATTESTED => {
            if !object.get("attestation").is_some_and(|v| v.is_object()) {
                return Err("statement.attested payload.attestation must be an object".to_string());
            }
        }
        EVENT_KIND_ATTEMPT_CLAIMED => {
            let obligation_id = require_str("obligation_id")?;
            if obligation_id.trim().is_empty() {
                return Err("payload.obligation_id must be non-empty".to_string());
            }
            let ttl = object
                .get("lease_ttl_seconds")
                .and_then(Value::as_u64)
                .ok_or_else(|| {
                    "attempt.claimed payload.lease_ttl_seconds must be a non-negative integer"
                        .to_string()
                })?;
            if ttl > MAX_ATTEMPT_LEASE_TTL_SECONDS {
                return Err(format!(
                    "attempt.claimed payload.lease_ttl_seconds must be at most {MAX_ATTEMPT_LEASE_TTL_SECONDS}"
                ));
            }
            for field in ["claimant_actor", "claimant_pubkey", "prior_claim_event_id"] {
                if let Some(value) = object.get(field)
                    && !value.is_string()
                {
                    return Err(format!(
                        "attempt.claimed payload.{field} must be a string when present"
                    ));
                }
            }
            if let Some(value) = object.get("release_reason")
                && !value.is_string()
            {
                return Err(
                    "attempt.claimed payload.release_reason must be a string when present"
                        .to_string(),
                );
            }
            if ttl == 0 {
                let prior = require_str("prior_claim_event_id")?;
                if !prior.strip_prefix("vev_").is_some_and(|hex| {
                    hex.len() == 16
                        && hex
                            .bytes()
                            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
                }) {
                    return Err(
                        "zero-TTL attempt.claimed payload.prior_claim_event_id must be a vev_ event id"
                            .to_string(),
                    );
                }
                if require_str("release_reason")?.trim().is_empty() {
                    return Err(
                        "zero-TTL attempt.claimed payload.release_reason must be non-empty"
                            .to_string(),
                    );
                }
            }
        }
        EVENT_KIND_STATEMENT_REGISTERED => {
            let hash = require_str("statement_hash")?;
            if hash.len() != 64 || hex::decode(hash).is_err() {
                return Err(
                    "payload.statement_hash must be 32 bytes of hex (64 hex chars)".to_string(),
                );
            }
            if let Some(finding) = object.get("finding_id")
                && !finding.is_null()
                && !finding.is_string()
            {
                return Err("payload.finding_id must be a string when present".to_string());
            }
        }
        "proposal.recommended" => {
            require_str("proposal_id")?;
        }
        "anchor.attached" => {
            if !payload.get("anchor_link").is_some_and(Value::is_object) {
                return Err("anchor.attached payload.anchor_link must be an object".to_string());
            }
        }
        "anchor.retracted" => {
            if !payload
                .get("anchor_link_id")
                .and_then(Value::as_str)
                .is_some_and(|s| !s.trim().is_empty())
            {
                return Err("anchor.retracted payload.anchor_link_id must be non-empty".to_string());
            }
        }
        // Historical audit-record kinds: the reducer replays them as
        // no-ops; the payload shape is whatever the retired surface
        // minted. Object-ness is already enforced above.
        "correction_return.review" | "research_trace.review" => {}

        // policy.auto_admitted (Phase 1A): deterministic machine-verified admission
        // audit record. The audit record is the only accountability artifact for
        // a human-free admit (checklist #10), so the binding fields are mandatory
        // and well-formed: the proposal it admitted, the claim digest the gate
        // matched, the frozen policy version, and the frozen verifier-env hash —
        // these let an auditor re-derive a FLOOR admission (reproduce the witness
        // under `verifier_env_hash` + check `claim_digest` against the assertion)
        // independently of attachments. `attachment_ids` lists the corroborating
        // attachments and MAY be empty: a floor-sufficient exact-lane admit has
        // none — the frozen verifier + faithful binding is itself the proof.
        "policy.auto_admitted" => {
            require_str("proposal_id")?;
            require_str("claim_digest")?;
            require_str("policy_version")?;
            require_str("verifier_env_hash")?;
            match payload.get("attachment_ids") {
                Some(serde_json::Value::Array(ids)) => {
                    if !ids.iter().all(|v| v.is_string()) {
                        return Err(
                            "policy.auto_admitted: attachment_ids must be strings".to_string()
                        );
                    }
                }
                _ => {
                    return Err("policy.auto_admitted: attachment_ids must be an array".to_string());
                }
            }
        }

        other => return Err(format!("unknown event kind '{other}'")),
    }
    Ok(())
}

/// Public form of `event_id` so callers building non-finding events
/// (e.g. the `frontier.created` genesis event in `project::assemble`)
/// can compute the canonical event ID with the same canonical-JSON
/// preimage shape as `new_finding_event`.
pub fn compute_event_id(event: &StateEvent) -> String {
    event_id(event)
}

/// The canonical content-address preimage bytes of an event — the exact bytes
/// hashed to form its `vev_` id. Deliberately EXCLUDES the event's own `id`,
/// and `signature`, so the preimage is stable under legitimate
/// re-signing/co-signing. Shared by `event_id` and the Merkle transparency log
/// (`crate::merkle`) so a log leaf is exactly the event's content address —
/// immune to re-signing and reproducible by any independent implementation.
/// Changing this shape is a protocol break.
pub fn event_content_preimage_bytes(event: &StateEvent) -> Vec<u8> {
    let content = json!({
        "schema": event.schema,
        "kind": event.kind,
        "target": event.target,
        "actor": event.actor,
        "timestamp": event.timestamp,
        "reason": event.reason,
        "before_hash": event.before_hash,
        "after_hash": event.after_hash,
        "payload": event.payload,
        "caveats": event.caveats,
    });
    canonical::to_canonical_bytes(&content).unwrap_or_default()
}

pub fn event_id(event: &StateEvent) -> String {
    format!(
        "vev_{}",
        &hex::encode(Sha256::digest(event_content_preimage_bytes(event)))[..16]
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::bundle::{
        Assertion, Conditions, Confidence, Evidence, Extraction, FindingBundle, Flags, Provenance,
    };
    use crate::project;

    #[test]
    fn snapshot_hash_exclusion_keys_exist_on_project() {
        // snapshot_hash removes "events"/"signatures"/"proof_state" by string
        // key from the serialized Project so the derived/mutable plane is
        // excluded from the hash. If a future serde rename or field rename made
        // one of these keys stop existing, the remove() would silently no-op and
        // snapshot_hash would begin covering the event log — a global byte-parity
        // break with NO compiler error. This pins the contract cheaply.
        let project = project::assemble("guard", vec![finding()], 0, 0, "guard");
        let value = serde_json::to_value(&project).expect("serialize Project");
        let map = value.as_object().expect("Project serializes as an object");
        for key in ["events", "signatures", "proof_state"] {
            assert!(
                map.contains_key(key),
                "snapshot_hash excludes `{key}` by string key, but it is absent from \
                 serde_json::to_value(Project) — the exclusion would silently no-op"
            );
        }
    }

    fn finding() -> FindingBundle {
        FindingBundle::new(
            Assertion {
                text: "LRP1 clears amyloid beta at the BBB".to_string(),
                assertion_type: "mechanism".to_string(),
                entities: Vec::new(),
                relation: None,
                direction: None,
                causal_claim: None,
                causal_evidence_grade: None,
            },
            Evidence {
                evidence_type: "experimental".to_string(),
                model_system: "mouse".to_string(),
                method: "assay".to_string(),
                replicated: false,
                replication_count: None,
                evidence_spans: Vec::new(),
            },
            Conditions {
                text: "mouse model".to_string(),
                duration: None,
            },
            Confidence::raw(0.6, "test", 0.8),
            Provenance {
                source_type: "published_paper".to_string(),
                doi: None,
                url: None,
                title: "Test source".to_string(),
                authors: Vec::new(),
                year: Some(2026),
                license: None,
                publisher: None,
                funders: Vec::new(),
                extraction: Extraction::default(),
                review: None,
                contributions: Vec::new(),
            },
            Flags {
                gap: false,
                negative_space: false,
                contested: false,
                retracted: false,
                declining: false,
                gravity_well: false,
                review_state: None,
                superseded: false,
                signature_threshold: None,
                jointly_accepted: false,
            },
        )
    }

    #[test]
    fn event_id_is_deterministic_for_content() {
        let event = new_finding_event(FindingEventInput {
            kind: "finding.reviewed",
            finding_id: "vf_test",
            actor_id: "reviewer",
            actor_type: "human",
            reason: "checked",
            before_hash: NULL_HASH,
            after_hash: "sha256:abc",
            payload: json!({"status": "accepted", "proposal_id": "vpr_test"}),
            caveats: vec![],
            timestamp: None,
        });
        let mut same = event.clone();
        same.id = String::new();
        same.id = super::event_id(&same);
        assert_eq!(event.id, same.id);
    }

    #[test]
    fn genesis_only_event_log_replays_ok() {
        // Phase J: assemble() emits a `frontier.created` genesis event,
        // so a freshly compiled frontier never has an empty event log.
        // Replay over genesis-only must succeed with status "ok" and the
        // single event accounted for.
        let frontier = project::assemble("test", Vec::new(), 0, 0, "test");
        let report = replay_report(&frontier);
        assert!(report.ok, "{:?}", report.conflicts);
        assert_eq!(report.event_log.count, 1);
        assert_eq!(report.event_log.kinds.get("frontier.created"), Some(&1));
    }

    #[test]
    fn replay_detects_duplicate_event_ids() {
        let finding = finding();
        let after_hash = finding_hash(&finding);
        let event = new_finding_event(FindingEventInput {
            kind: "finding.reviewed",
            finding_id: &finding.id,
            actor_id: "reviewer",
            actor_type: "human",
            reason: "checked",
            before_hash: &after_hash,
            after_hash: &after_hash,
            payload: json!({"status": "accepted", "proposal_id": "vpr_test"}),
            caveats: vec![],
            timestamp: None,
        });
        let mut frontier = project::assemble("test", vec![finding], 0, 0, "test");
        frontier.events = vec![event.clone(), event];

        let report = replay_report(&frontier);
        assert!(!report.ok);
        assert_eq!(report.status, "conflict");
        assert!(!report.event_log.duplicate_ids.is_empty());
    }

    #[test]
    fn replay_detects_orphan_targets() {
        let mut frontier = project::assemble("test", Vec::new(), 0, 0, "test");
        frontier.events.push(new_finding_event(FindingEventInput {
            kind: "finding.reviewed",
            finding_id: "vf_missing",
            actor_id: "reviewer",
            actor_type: "human",
            reason: "checked",
            before_hash: NULL_HASH,
            after_hash: "sha256:abc",
            payload: json!({"status": "accepted", "proposal_id": "vpr_test"}),
            caveats: vec![],
            timestamp: None,
        }));

        let report = replay_report(&frontier);
        assert!(!report.ok);
        assert_eq!(report.event_log.orphan_targets, vec!["vf_missing"]);
    }

    #[test]
    fn external_obligation_lease_is_not_an_orphan() {
        // Swarm regression (2026-07-02): an `attempt.claimed` lease on a
        // namespaced external target (erdos:443 — a problem with no
        // finding yet) must not read as an orphan event; ten of them
        // turned a green frontier red.
        let mut frontier = project::assemble("test", Vec::new(), 0, 0, "test");
        let mut event = new_finding_event(FindingEventInput {
            kind: "attempt.claimed",
            finding_id: "erdos:443",
            actor_id: "agent:swarm-443",
            actor_type: "agent",
            reason: "obligation lease",
            before_hash: "sha256:null",
            after_hash: "sha256:null",
            payload: serde_json::json!({
                "obligation_id": "erdos:443",
                "lease_ttl_seconds": 3600,
                "claimant_actor": "agent:swarm-443",
                "claimant_pubkey": "00",
            }),
            caveats: Vec::new(),
            timestamp: None,
        });
        event.id = compute_event_id(&event);
        frontier.events.push(event);
        let report = replay_report(&frontier);
        assert!(
            report.event_log.orphan_targets.is_empty(),
            "external obligation lease misread as orphan: {:?}",
            report.event_log.orphan_targets
        );
    }

    #[test]
    fn attempt_claimed_rejects_unbounded_lease_ttl() {
        let payload = serde_json::json!({
            "obligation_id": "erdos:1056",
            "lease_ttl_seconds": u64::MAX,
            "claimant_actor": "agent:fixture",
            "claimant_pubkey": "00",
        });
        let error = validate_event_payload(EVENT_KIND_ATTEMPT_CLAIMED, &payload).unwrap_err();
        assert!(error.contains("must be at most"), "{error}");
    }

    #[test]
    fn replay_accepts_current_hash_boundary() {
        let finding = finding();
        let hash = finding_hash(&finding);
        let event = new_finding_event(FindingEventInput {
            kind: "finding.reviewed",
            finding_id: &finding.id,
            actor_id: "reviewer",
            actor_type: "human",
            reason: "checked",
            before_hash: &hash,
            after_hash: &hash,
            payload: json!({"status": "accepted", "proposal_id": "vpr_test"}),
            caveats: vec![],
            timestamp: None,
        });
        let mut frontier = project::assemble("test", vec![finding], 0, 0, "test");
        frontier.events.push(event);

        let report = replay_report(&frontier);
        assert!(report.ok, "{:?}", report.conflicts);
        assert_eq!(report.status, "ok");
    }

    #[test]
    fn validates_diff_pack_lifecycle_payloads() {
        assert!(
            validate_event_payload(
                "diff_pack.released",
                &json!({
                    "pack_id": "vsd_1234567890abcdef",
                    "frontier_id": "vfr_1234567890abcdef",
                    "summary": "Review bounded proposed changes.",
                    "aggregate_kind": "clinical_translation.review_set",
                }),
            )
            .is_ok()
        );
        assert!(
            validate_event_payload(
                "diff_pack.reviewed",
                &json!({
                    "pack_id": "vsd_1234567890abcdef",
                    "verdict": "revise",
                    "reviewer_actor": "reviewer:test",
                    "reason": "Needs source locator repair before acceptance.",
                    "applied_members": [],
                    "sdk_only_members": ["vpr_sdk_only"],
                }),
            )
            .is_ok()
        );
        assert!(
            validate_event_payload(
                "diff_pack.released",
                &json!({
                    "pack_id": "bad",
                    "frontier_id": "vfr_1234567890abcdef",
                    "summary": "Review bounded proposed changes.",
                    "aggregate_kind": "clinical_translation.review_set",
                }),
            )
            .is_err()
        );
        assert!(
            validate_event_payload(
                "diff_pack.reviewed",
                &json!({
                    "pack_id": "vsd_1234567890abcdef",
                    "verdict": "maybe",
                    "reviewer_actor": "reviewer:test",
                    "reason": "Needs source locator repair before acceptance.",
                }),
            )
            .is_err()
        );
    }

    #[test]
    fn validates_artifact_asserted_payload() {
        let good_hash = "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        assert!(
            validate_event_payload(
                EVENT_KIND_ARTIFACT_ASSERTED,
                &json!({
                    "proposal_id": "vpr_test",
                    "artifact": {
                        "id": "va_1234567890abcdef",
                        "kind": "clinical_trial_record",
                        "name": "NCT test record",
                        "content_hash": good_hash,
                        "storage_mode": "embedded",
                    },
                }),
            )
            .is_ok()
        );
        assert!(
            validate_event_payload(
                EVENT_KIND_ARTIFACT_ASSERTED,
                &json!({
                    "proposal_id": "vpr_test",
                    "artifact": {
                        "id": "va_123",
                        "kind": "clinical_trial_record",
                        "name": "NCT test record",
                        "content_hash": good_hash,
                        "storage_mode": "embedded",
                    },
                }),
            )
            .is_err()
        );
        assert!(
            validate_event_payload(
                EVENT_KIND_ARTIFACT_ASSERTED,
                &json!({
                    "proposal_id": "vpr_test",
                    "artifact": {
                        "id": "va_1234567890abcdef",
                        "kind": "clinical_trial_record",
                        "name": "NCT test record",
                        "content_hash": "sha256:not-a-real-hash",
                        "storage_mode": "embedded",
                    },
                }),
            )
            .is_err()
        );
    }

    // v0.38 — causal-typing event validation
    #[test]
    fn validates_reinterpreted_causal_payload() {
        // OK: missing claim/grade is fine (None means no prior reading).
        assert!(
            validate_event_payload(
                "assertion.reinterpreted_causal",
                &json!({
                    "proposal_id": "vpr_test",
                    "before": {},
                    "after": { "claim": "intervention", "grade": "rct" },
                }),
            )
            .is_ok()
        );
        // OK: pure claim revision, no grade.
        assert!(
            validate_event_payload(
                "assertion.reinterpreted_causal",
                &json!({
                    "proposal_id": "vpr_test",
                    "before": { "claim": "correlation" },
                    "after": { "claim": "mediation" },
                }),
            )
            .is_ok()
        );
        // FAIL: invalid claim.
        assert!(
            validate_event_payload(
                "assertion.reinterpreted_causal",
                &json!({
                    "proposal_id": "vpr_test",
                    "before": {},
                    "after": { "claim": "magic" },
                }),
            )
            .is_err()
        );
        // FAIL: invalid grade.
        assert!(
            validate_event_payload(
                "assertion.reinterpreted_causal",
                &json!({
                    "proposal_id": "vpr_test",
                    "before": {},
                    "after": { "claim": "intervention", "grade": "vibes" },
                }),
            )
            .is_err()
        );
        // FAIL: missing proposal_id.
        assert!(
            validate_event_payload(
                "assertion.reinterpreted_causal",
                &json!({
                    "before": {},
                    "after": { "claim": "intervention" },
                }),
            )
            .is_err()
        );
    }

    /// v0.49: a `key.revoke` event names the revoked pubkey, the
    /// moment of compromise, and (optionally) the replacement key.
    /// Empty optional fields skip canonical-JSON serialization so
    /// existing event logs round-trip byte-identically.
    #[test]
    fn revocation_event_canonical_shape() {
        use crate::canonical;
        let payload = RevocationPayload {
            revoked_pubkey: "4892f93877e637b5f59af31d9ec6704814842fb278cacb0eb94704baef99455e"
                .to_string(),
            revoked_at: "2026-05-01T17:00:00Z".to_string(),
            replacement_pubkey: "8891a2ab35ca2ed2182ed4e46b6567ce8dacc9985eb496d895578201272a1cd9"
                .to_string(),
            reason: "key file leaked from CI cache".to_string(),
        };
        let event = new_revocation_event(
            "reviewer:will-blair",
            "human",
            payload,
            "rotating compromised key",
            NULL_HASH,
            NULL_HASH,
        );
        assert_eq!(event.kind, EVENT_KIND_KEY_REVOKE);
        assert_eq!(event.target.r#type, "actor");
        assert!(event.id.starts_with("vev_"));
        let bytes = canonical::to_canonical_bytes(&event).unwrap();
        let s = std::str::from_utf8(&bytes).unwrap();
        assert!(
            s.contains("\"revoked_pubkey\""),
            "canonical bytes missing revoked_pubkey: {s}"
        );
        assert!(
            s.contains("\"revoked_at\""),
            "canonical bytes missing revoked_at: {s}"
        );
        assert!(
            s.contains("\"replacement_pubkey\""),
            "canonical bytes missing replacement_pubkey: {s}"
        );

        // Empty replacement_pubkey skips serialization.
        let payload_minimal = RevocationPayload {
            revoked_pubkey: "a".repeat(64),
            revoked_at: "2026-05-01T17:00:00Z".to_string(),
            replacement_pubkey: String::new(),
            reason: String::new(),
        };
        let minimal_event = new_revocation_event(
            "reviewer:will-blair",
            "human",
            payload_minimal,
            "scheduled rotation",
            NULL_HASH,
            NULL_HASH,
        );
        let minimal_bytes = canonical::to_canonical_bytes(&minimal_event).unwrap();
        let minimal_s = std::str::from_utf8(&minimal_bytes).unwrap();
        assert!(
            !minimal_s.contains("\"replacement_pubkey\""),
            "empty replacement_pubkey leaked into canonical JSON: {minimal_s}"
        );
        assert!(
            !minimal_s.contains("\"reason\":\"\""),
            "empty payload reason leaked into canonical JSON: {minimal_s}"
        );
    }

    /// v0.49: validate_event_payload now recognises `key.revoke`.
    /// Tests cover the four real failure modes plus the happy path.
    #[test]
    fn revocation_payload_validation() {
        let good_pubkey = "4892f93877e637b5f59af31d9ec6704814842fb278cacb0eb94704baef99455e";
        let other_pubkey = "8891a2ab35ca2ed2182ed4e46b6567ce8dacc9985eb496d895578201272a1cd9";

        // OK: minimal valid payload.
        assert!(
            validate_event_payload(
                EVENT_KIND_KEY_REVOKE,
                &json!({
                    "revoked_pubkey": good_pubkey,
                    "revoked_at": "2026-05-01T17:00:00Z",
                }),
            )
            .is_ok()
        );

        // OK: full payload with replacement and reason.
        assert!(
            validate_event_payload(
                EVENT_KIND_KEY_REVOKE,
                &json!({
                    "revoked_pubkey": good_pubkey,
                    "revoked_at": "2026-05-01T17:00:00Z",
                    "replacement_pubkey": other_pubkey,
                    "reason": "key file leaked",
                }),
            )
            .is_ok()
        );

        // FAIL: revoked_pubkey wrong length (32 bytes ASCII, not 64 hex).
        assert!(
            validate_event_payload(
                EVENT_KIND_KEY_REVOKE,
                &json!({
                    "revoked_pubkey": "abc123",
                    "revoked_at": "2026-05-01T17:00:00Z",
                }),
            )
            .is_err()
        );

        // FAIL: revoked_pubkey contains non-hex chars.
        assert!(
            validate_event_payload(
                EVENT_KIND_KEY_REVOKE,
                &json!({
                    "revoked_pubkey": "ZZ".repeat(32),
                    "revoked_at": "2026-05-01T17:00:00Z",
                }),
            )
            .is_err()
        );

        // FAIL: missing revoked_at.
        assert!(
            validate_event_payload(
                EVENT_KIND_KEY_REVOKE,
                &json!({
                    "revoked_pubkey": good_pubkey,
                }),
            )
            .is_err()
        );

        // FAIL: replacement_pubkey wrong length.
        assert!(
            validate_event_payload(
                EVENT_KIND_KEY_REVOKE,
                &json!({
                    "revoked_pubkey": good_pubkey,
                    "revoked_at": "2026-05-01T17:00:00Z",
                    "replacement_pubkey": "deadbeef",
                }),
            )
            .is_err()
        );

        // FAIL: replacement equals revoked (no-op rotation).
        assert!(
            validate_event_payload(
                EVENT_KIND_KEY_REVOKE,
                &json!({
                    "revoked_pubkey": good_pubkey,
                    "revoked_at": "2026-05-01T17:00:00Z",
                    "replacement_pubkey": good_pubkey,
                }),
            )
            .is_err()
        );

        // FAIL: revoked_at is non-empty but not a valid ISO-8601 stamp.
        // The v0.49.1 validator parses it as RFC-3339 so typos can't
        // reach replay verification.
        // chrono's parse_from_rfc3339 is intentionally lenient on the
        // `T` vs space separator (RFC-3339 §5.6), so we don't include
        // that case here — chronologically nonsensical strings still
        // fail, which is the bar we care about.
        for bad in [
            "yesterday",
            "2026-13-01T00:00:00Z", // month 13
            "2026-05-01",           // date only, no time
            "x",
        ] {
            assert!(
                validate_event_payload(
                    EVENT_KIND_KEY_REVOKE,
                    &json!({
                        "revoked_pubkey": good_pubkey,
                        "revoked_at": bad,
                    }),
                )
                .is_err(),
                "expected revoked_at {bad:?} to fail validation"
            );
        }
    }

    /// v0.79.4: per-event attestation validator. Required
    /// payload: target_event_id (must start with vev_),
    /// attester_id (non-empty), scope_note (non-empty). Optional
    /// signature (string), proof_id (vpf_*).
    #[test]
    fn attestation_recorded_validator() {
        // PASS: minimal good payload.
        let good = json!({
            "target_event_id": "vev_abc",
            "attester_id": "reviewer:will-blair",
            "scope_note": "Independent re-verification of the Stupp protocol finding."
        });
        assert!(validate_event_payload(EVENT_KIND_ATTESTATION_RECORDED, &good).is_ok());
    }

    #[test]
    fn validates_policy_auto_admitted_payload() {
        // PASS: all binding fields present + well-formed.
        let good = json!({
            "proposal_id": "vpr_abc",
            "claim_digest": "deadbeefdeadbeef",
            "attachment_ids": ["vva_a", "vva_b"],
            "policy_version": "exact-lane.v1",
            "verifier_env_hash": "sha256:cafe"
        });
        assert!(validate_event_payload(EVENT_KIND_POLICY_AUTO_ADMITTED, &good).is_ok());

        // FAIL: the pre-hardening minimal payload (proposal_id only) is now rejected.
        let minimal = json!({ "proposal_id": "vpr_abc" });
        assert!(validate_event_payload(EVENT_KIND_POLICY_AUTO_ADMITTED, &minimal).is_err());

        // PASS: empty attachment_ids — a floor-sufficient admit has no
        // attachments; the frozen verifier + faithful binding is the proof, and
        // claim_digest + verifier_env_hash are the accountability.
        let floor_admit = json!({
            "proposal_id": "vpr_abc",
            "claim_digest": "deadbeef",
            "attachment_ids": [],
            "policy_version": "exact-lane.v1",
            "verifier_env_hash": "sha256:cafe"
        });
        assert!(validate_event_payload(EVENT_KIND_POLICY_AUTO_ADMITTED, &floor_admit).is_ok());

        // FAIL: a missing claim_digest (the floor accountability) still rejects.
        let no_digest = json!({
            "proposal_id": "vpr_abc",
            "attachment_ids": [],
            "policy_version": "exact-lane.v1",
            "verifier_env_hash": "sha256:cafe"
        });
        assert!(validate_event_payload(EVENT_KIND_POLICY_AUTO_ADMITTED, &no_digest).is_err());

        // PASS: with optional signature + proof_id.
        let with_proof = json!({
            "target_event_id": "vev_abc",
            "attester_id": "reviewer:will-blair",
            "scope_note": "Lean-formalized.",
            "scopes": ["domain_relevance", "method_review"],
            "reviewer_role": "domain_reviewer",
            "orcid": "https://orcid.org/0000-0000-0000-000X",
            "ror": "https://ror.org/03yrm5c26",
            "attestation_id": "vatt_demo",
            "signature": "ed25519:cafebabe",
            "proof_id": "vpf_demo"
        });
        assert!(validate_event_payload(EVENT_KIND_ATTESTATION_RECORDED, &with_proof).is_ok());

        // FAIL: target_event_id without vev_ prefix.
        let bad_target = json!({
            "target_event_id": "something_else",
            "attester_id": "reviewer:x",
            "scope_note": "x"
        });
        assert!(validate_event_payload(EVENT_KIND_ATTESTATION_RECORDED, &bad_target).is_err());

        // FAIL: empty attester_id.
        let no_attester = json!({
            "target_event_id": "vev_abc",
            "attester_id": "",
            "scope_note": "x"
        });
        assert!(validate_event_payload(EVENT_KIND_ATTESTATION_RECORDED, &no_attester).is_err());

        // FAIL: proof_id without vpf_ prefix.
        let bad_proof = json!({
            "target_event_id": "vev_abc",
            "attester_id": "reviewer:x",
            "scope_note": "x",
            "proof_id": "not_a_vpf"
        });
        assert!(validate_event_payload(EVENT_KIND_ATTESTATION_RECORDED, &bad_proof).is_err());
    }

    #[test]
    fn event_log_hash_is_independent_of_input_order() {
        // ADR 0001 Phase 0a: the integrity commitment must not depend on how
        // the events were loaded (packet authored-order vs `.vela/events/`
        // filename-order vs a future git per-file layout). event_log_hash
        // canonicalizes on the content-addressed event id, so any permutation
        // of the same events hashes identically.
        let mk = |id: &str, ts: &str| StateEvent {
            schema: EVENT_SCHEMA.to_string(),
            id: id.to_string(),
            kind: "note.added".into(),
            target: StateTarget {
                r#type: "finding".to_string(),
                id: "vf_x".to_string(),
            },
            actor: StateActor {
                id: "reviewer:t".to_string(),
                r#type: "human".to_string(),
            },
            timestamp: ts.to_string(),
            reason: "t".to_string(),
            before_hash: String::new(),
            after_hash: String::new(),
            payload: Value::Null,
            caveats: vec![],
            signature: None,
        };
        // ids deliberately NOT in timestamp order: proves the canonical order
        // is id-sort (load-path stable), not (timestamp, id) replay order.
        let a = mk("vev_c", "2026-01-01T00:00:03Z");
        let b = mk("vev_a", "2026-01-01T00:00:01Z");
        let c = mk("vev_b", "2026-01-01T00:00:02Z");
        let forward = vec![a.clone(), b.clone(), c.clone()];
        let shuffled = vec![c, a.clone(), b];
        assert_eq!(
            event_log_hash(&forward),
            event_log_hash(&shuffled),
            "event_log_hash must be independent of input order"
        );
        let mut reversed = forward.clone();
        reversed.reverse();
        assert_eq!(event_log_hash(&forward), event_log_hash(&reversed));
        // sanity: it is NOT the trivial empty hash
        assert_ne!(event_log_hash(&forward), event_log_hash(&[]));
    }
}