ljos 0.33.0

Memory, a work tracker and a shell-command gate for coding agents (Claude Code, Codex, Grok Build, agy, opencode) over MCP and hooks.
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
//! Record schema v1: questions and ballots, signed as canonical JSON.

use anyhow::{anyhow, bail, Context, Result};
use chrono::{DateTime, FixedOffset};
use serde::de::{self, MapAccess, SeqAccess, Visitor};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};

/// The only accepted schema value for record entries.
pub const SCHEMA_V1: &str = "record/v1";
const SIGNING_LABEL: &str = "ljos-record-entry/v1";
const RESERVED_PLAIN_FIELDS: &[&str] = &["host", "session", "agent"];
const QUESTION_TYPES: &[&str] = &["correct", "locate", "change", "design", "lead"];
const BALLOT_STANCES: &[&str] = &["accept", "reject", "withdrawn"];

/// One signed entry in the plain record layer.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SignedEntry {
    pub schema: String,
    #[serde(flatten)]
    pub entry: Entry,
    pub signature: EntrySignature,
}

impl SignedEntry {
    fn unsigned(&self) -> UnsignedEntry {
        UnsignedEntry {
            schema: self.schema.clone(),
            entry: self.entry.clone(),
        }
    }
}

/// A record entry before signing.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UnsignedEntry {
    pub schema: String,
    #[serde(flatten)]
    pub entry: Entry,
}

/// Signature material for one entry.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct EntrySignature {
    pub key: String,
    pub sig: String,
}

/// Entry kinds in schema v1.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Entry {
    Question(QuestionEntry),
    Ballot(BallotEntry),
}

/// A question entry.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct QuestionEntry {
    pub id: String,
    #[serde(rename = "type")]
    pub question_type: String,
    pub title: String,
    pub options: Vec<String>,
    pub anchors: Vec<String>,
    pub opened_by: String,
    pub opened_at: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub dup_of: Option<String>,
}

/// A ballot entry.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BallotEntry {
    /// The canonical id: first 16 hex chars of sha256(canonical ballot JSON
    /// without this `id` field).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    pub question: String,
    pub stance: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub confidence: Option<f64>,
    pub voter: String,
    pub commit: String,
    #[serde(default)]
    pub used: Vec<String>,
    pub lineage: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub replaces: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub expect: Option<String>,
    pub cast_at: String,
}

/// Summary of `ljos record check`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CheckReport {
    pub files: usize,
    pub entries: usize,
    pub questions: usize,
    pub ballots: usize,
    pub questions_with_live_ballots: usize,
}

/// Sign one entry with `seat`'s ballot key.
///
/// The key is the file [`crate::ballot_sig::seat_key_path`] names, loaded
/// with [`crate::ballot_sig::load_or_create`], the same path a seat ballot
/// uses. Ballot entries get `id` filled when absent.
///
/// # Errors
///
/// The seat key cannot be loaded, or the entry does not pass schema
/// validation.
pub fn sign_entry(mut unsigned: UnsignedEntry, seat: &str) -> Result<SignedEntry> {
    if unsigned.schema.trim().is_empty() {
        unsigned.schema = SCHEMA_V1.to_string();
    }
    if let Entry::Ballot(ballot) = &mut unsigned.entry {
        if ballot.id.is_none() {
            ballot.id = Some(ballot_id(ballot)?);
        }
    }
    validate_unsigned(&unsigned)?;
    let (role, named) = match &unsigned.entry {
        Entry::Question(question) => ("opener", question.opened_by.as_str()),
        Entry::Ballot(ballot) => ("voter", ballot.voter.as_str()),
    };
    if seat.trim() != named {
        bail!("record signer `{seat}` must match entry {role} `{named}`");
    }
    let path = crate::ballot_sig::seat_key_path(seat)
        .context("no seat key path: HOME and XDG_DATA_HOME are unset")?;
    let key = crate::ballot_sig::load_or_create(&path)?;
    let payload = signing_payload(&unsigned)?;
    let (public, sig) = crate::ballot_sig::signed_hex(&key, &payload);
    Ok(SignedEntry {
        schema: unsigned.schema,
        entry: unsigned.entry,
        signature: EntrySignature { key: public, sig },
    })
}

/// Verify a signed entry and its schema rules.
///
/// # Errors
///
/// Schema validation failed, signature data was malformed, or signature
/// verification failed.
pub fn verify_entry(entry: &SignedEntry) -> Result<()> {
    let unsigned = entry.unsigned();
    validate_unsigned(&unsigned)?;
    let payload = signing_payload(&unsigned)?;
    if !crate::ballot_sig::is_hex_len(&entry.signature.key, 64) {
        bail!("signature key must be 64 hex chars");
    }
    if !crate::ballot_sig::is_hex_len(&entry.signature.sig, 128) {
        bail!("signature must be 128 hex chars");
    }
    if crate::ballot_sig::weak_key(&entry.signature.key) {
        bail!("signature key is a small-order point");
    }
    if !crate::ballot_sig::signature_ok(&entry.signature.key, &entry.signature.sig, &payload) {
        bail!("entry signature does not verify");
    }
    Ok(())
}

/// Run schema + signature checks over many entries, including voter-key pinning.
///
/// Every voter id is pinned, including a question's `opened_by`, and so
/// is the seat after its `/`. A pin file row for the voter id or its seat
/// is the pin; otherwise the first key seen in this check is. A later
/// entry under another key is a mismatch, and so is a seat seen under two
/// keys, whichever comes first. A run id is `<identity>/run-<id>`. Two
/// ballots of one voter in one hour are not an error here: the tally
/// drops that voter on that question.
///
/// # Errors
///
/// Any entry fails validation or signature verification, a pin file voter
/// is not spelled the way the check looks it up, the pin file pins one
/// seat to two keys, an entry's voter or seat is pinned to another key,
/// or a `replaces` names no ballot of the same voter and question.
pub fn check_entries(entries: &[SignedEntry]) -> Result<CheckReport> {
    let mut report = CheckReport {
        entries: entries.len(),
        ..CheckReport::default()
    };
    let (mut pins, pin_source) = crate::ballot_sig::pins_for_check()?;
    for voter in pins.keys() {
        validate_pin_voter(voter).with_context(|| format!("pin source: {pin_source}"))?;
    }
    let seat_pins = seat_pins(&pins).with_context(|| format!("pin source: {pin_source}"))?;
    let mut seat_keys = BTreeMap::new();
    for entry in entries {
        verify_entry(entry)?;
        match &entry.entry {
            Entry::Question(question) => {
                report.questions += 1;
                observe_seat_pin(&seat_pins, &question.opened_by, &entry.signature.key)
                    .with_context(|| format!("pin source: {pin_source}"))?;
                crate::ballot_sig::observe_pin(
                    &mut pins,
                    &question.opened_by,
                    &entry.signature.key,
                )
                .with_context(|| format!("pin source: {pin_source}"))?;
                observe_seat_key(&mut seat_keys, &question.opened_by, &entry.signature.key)?;
            }
            Entry::Ballot(ballot) => {
                report.ballots += 1;
                observe_seat_pin(&seat_pins, &ballot.voter, &entry.signature.key)
                    .with_context(|| format!("pin source: {pin_source}"))?;
                crate::ballot_sig::observe_pin(&mut pins, &ballot.voter, &entry.signature.key)
                    .with_context(|| format!("pin source: {pin_source}"))?;
                observe_seat_key(&mut seat_keys, &ballot.voter, &entry.signature.key)?;
            }
        }
    }
    report.questions_with_live_ballots = tally(entries)?.len();
    Ok(report)
}

/// Refuse a pin row the lookups would never match. Nothing is stripped:
/// a BOM or a zero-width space before `seat-a` would leave that seat's
/// pin unread. A row with `/` must be a voter id as written. A bare row
/// pins the seat [`voter_seat`] folds it to, so `inky@39u` pins `inky`
/// and `judge:model-1.5` pins `judge-model-1-5`.
fn validate_pin_voter(voter: &str) -> Result<()> {
    if !voter.bytes().all(|b| b.is_ascii_graphic()) {
        bail!("pin voter {voter:?} is not printable ASCII");
    }
    if voter.contains('/') {
        return validate_voter_id(voter).with_context(|| format!("pin voter `{voter}`"));
    }
    let seat = crate::seat_slug(voter);
    if valid_voter_part(voter) && seat != voter {
        bail!("pin voter `{voter}` is not a canonical seat; the seat is `{seat}`");
    }
    Ok(())
}

/// Pin rows by the seat each pins, as row to key. A bare row pins the
/// seat it folds to and a voter id row the seat after its `/`. A seat
/// holds one key: rows such as `inky`, `inky@39u` and `mallory/inky`
/// must pin the same key, or that seat would vote once under each.
fn seat_pins(
    pins: &BTreeMap<String, String>,
) -> Result<BTreeMap<String, BTreeMap<String, String>>> {
    let mut seats: BTreeMap<String, BTreeMap<String, String>> = BTreeMap::new();
    for (row, key) in pins {
        let seat_part = split_voter(row).map_or(row.as_str(), |(_, seat)| seat);
        let Some(seat) = voter_seat(seat_part) else {
            continue;
        };
        let rows = seats.entry(seat.clone()).or_default();
        let fingerprint = crate::ballot_sig::key_fingerprint(key);
        if let Some((other, pinned)) = rows
            .iter()
            .find(|(_, pinned)| crate::ballot_sig::key_fingerprint(pinned) != fingerprint)
        {
            bail!(
                "seat {seat} is pinned to two keys, {}… ({other}) and {}… ({row}); a seat holds one key",
                &pinned[..12.min(pinned.len())],
                &key[..12.min(key.len())]
            );
        }
        rows.insert(row.clone(), key.clone());
    }
    Ok(seats)
}

/// Hold `voter` to its seat's pinned key. The voter id is already
/// canonical, so the part after `/` is the seat as [`seat_pins`] keys it.
fn observe_seat_pin(
    seat_pins: &BTreeMap<String, BTreeMap<String, String>>,
    voter: &str,
    key: &str,
) -> Result<()> {
    let Some((_, seat)) = split_voter(voter) else {
        return Ok(());
    };
    let Some(rows) = seat_pins.get(seat) else {
        return Ok(());
    };
    let fingerprint = crate::ballot_sig::key_fingerprint(key);
    if rows
        .values()
        .any(|pinned| crate::ballot_sig::key_fingerprint(pinned) == fingerprint)
    {
        return Ok(());
    }
    let pinned: Vec<String> = rows
        .iter()
        .map(|(row, pinned)| format!("{}… ({row})", &pinned[..12.min(pinned.len())]))
        .collect();
    bail!(
        "{voter} is pinned through seat {seat} to key {}, got {}…",
        pinned.join(" or "),
        &key[..12.min(key.len())]
    );
}

/// Hold the seat `voter` sits as to one key across the check. A seat
/// seen under two keys fails whichever file comes first, and the error
/// names the keys in one order, so file order cannot pick the seat's key.
fn observe_seat_key(seats: &mut BTreeMap<String, String>, voter: &str, key: &str) -> Result<()> {
    let Some((_, seat)) = split_voter(voter) else {
        return Ok(());
    };
    let fingerprint = crate::ballot_sig::key_fingerprint(key);
    let held = seats
        .entry(seat.to_string())
        .or_insert_with(|| fingerprint.clone());
    if *held == fingerprint {
        return Ok(());
    }
    let (one, two) = if *held < fingerprint {
        (held.as_str(), fingerprint.as_str())
    } else {
        (fingerprint.as_str(), held.as_str())
    };
    bail!(
        "seat {seat} signs under two keys, {}… and {}…; a seat holds one key, so pin it or sign under one",
        &one[..12.min(one.len())],
        &two[..12.min(two.len())]
    );
}

/// Validate every `.json` entry under `dir`.
///
/// # Errors
///
/// The directory cannot be read, no JSON entries are found, or any entry is
/// invalid.
pub fn check_dir(dir: &Path) -> Result<CheckReport> {
    let paths = entry_files(dir)?;
    if paths.is_empty() {
        bail!("record check: no .json entries under {}", dir.display());
    }
    let mut entries = Vec::with_capacity(paths.len());
    for path in &paths {
        let raw = std::fs::read_to_string(path)
            .with_context(|| format!("record check: read {}", path.display()))?;
        let entry = parse_signed_json(&raw)
            .with_context(|| format!("record check: parse {}", path.display()))?;
        entries.push(entry);
    }
    let mut report = check_entries(&entries)?;
    report.files = paths.len();
    Ok(report)
}

/// Text form of a successful check.
#[must_use]
pub fn format_check_report(report: &CheckReport) -> String {
    format!(
        "record check: {} files, {} entries ({} questions, {} ballots), {} questions with live ballots\n",
        report.files,
        report.entries,
        report.questions,
        report.ballots,
        report.questions_with_live_ballots
    )
}

/// A tracker ballot that does not stand.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BadBallot {
    /// The file that holds the ballot, under the tracker root.
    pub file: PathBuf,
    /// The ballot's line in that file, from 1.
    pub line: usize,
    pub issue: String,
    pub reason: String,
}

/// Summary of `ljos record check --tracker`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TrackerReport {
    /// Issues that hold a ballot.
    pub issues: usize,
    pub ballots: usize,
    /// `ISSUE VOTER` for each unsigned ballot held before the first pin.
    pub legacy: Vec<String>,
    pub bad: Vec<BadBallot>,
}

/// Hold every ballot on the tracker at `root` to the push gate's
/// signature check ([`crate::ballot_sig::seat_check`]): the newest
/// `ballot-sig:` note of its voter names its choice, verifies, and is
/// under the key `.ljos-seat-keys` pins for that voter and for no other
/// seat or voter on the issue, so one key does not count as several
/// voters. A persona's or a judge's ballot is held to it too, since every
/// cast signs. An unsigned ballot the tracker held before its first pin,
/// by commit order, is legacy, as at the push gate
/// ([`crate::ballot_sig::recorded_before_signing_in`]). Only files are
/// read, and git for that order.
///
/// # Errors
///
/// The root holds no project, an issue file cannot be read or parsed, or
/// the pin file is malformed.
pub fn check_tracker(root: &Path) -> Result<TrackerReport> {
    crate::slot::require_tracker_history(root).map_err(anyhow::Error::msg)?;
    let layout = vissue_core::Layout::resolve(Some(root), None)?;
    let projects = vissue_core::store::list_projects(&layout)?;
    if projects.is_empty() {
        bail!(
            "record check: no issues.org under {}",
            layout.projects_dir().display()
        );
    }
    let (pins, _) = crate::ballot_sig::pins_for_check_in(root)?;
    let mut report = TrackerReport::default();
    let mut texts: BTreeMap<PathBuf, String> = BTreeMap::new();
    for project in projects {
        let board = layout.project_issues_path(&project);
        let doc = vissue_core::IssueDoc::parse_file(&project, &board)?;
        for heading in &doc.headings {
            if !heading.extra_drawers.iter().any(|d| is_votes_drawer(d)) {
                continue;
            }
            let issue = heading.id.as_str();
            let ballots = vissue_core::ops::ballots(&layout, issue)?;
            if ballots.is_empty() {
                continue;
            }
            report.issues += 1;
            let notes: Vec<String> = heading
                .logbook
                .iter()
                .filter_map(|e| e.note.clone())
                .collect();
            let file = doc.heading_path(issue).to_path_buf();
            if !texts.contains_key(&file) {
                let text = std::fs::read_to_string(&file)
                    .with_context(|| format!("record check: read {}", file.display()))?;
                texts.insert(file.clone(), text);
            }
            let span = (file == board).then_some((heading.line_start, heading.line_end));
            let legacy = |voter: &str, choice: &str| {
                crate::ballot_sig::recorded_before_signing_in(root, issue, voter, choice)
                    .unwrap_or(false)
            };
            let cast: Vec<&str> = ballots.iter().map(|b| b.agent.as_str()).collect();
            for ballot in &ballots {
                report.ballots += 1;
                let seat = [(ballot.agent.as_str(), ballot.choice.as_str())];
                let (problems, old) =
                    crate::ballot_sig::seat_check(issue, &seat, &notes, &pins, &legacy);
                if !old.is_empty() {
                    report.legacy.push(format!("{issue} {}", ballot.agent));
                    continue;
                }
                let reason = problems
                    .into_iter()
                    .next()
                    .or_else(|| shared_key(&ballot.agent, &pins, &cast));
                let Some(reason) = reason else {
                    continue;
                };
                let line =
                    ballot_line(&texts[&file], span, &ballot.agent).unwrap_or(heading.line_start);
                report.bad.push(BadBallot {
                    file: file.strip_prefix(root).unwrap_or(&file).to_path_buf(),
                    line,
                    issue: issue.to_string(),
                    reason,
                });
            }
        }
    }
    Ok(report)
}

/// The other voters `pins` holds `voter`'s key for, as a reason. Keys are
/// told apart by [`crate::ballot_sig::key_fingerprint`]. Two rows that
/// [`voter_seat`] folds to one seat, such as `inky` and `inky@39u`, may
/// share a key, unless both are among `cast`, the voters on this issue.
fn shared_key(voter: &str, pins: &BTreeMap<String, String>, cast: &[&str]) -> Option<String> {
    let key = crate::ballot_sig::key_fingerprint(pins.get(voter)?);
    let seat = voter_seat(voter);
    let others: Vec<&str> = pins
        .iter()
        .filter(|(other, k)| {
            other.as_str() != voter
                && (voter_seat(other) != seat || cast.contains(&other.as_str()))
                && crate::ballot_sig::key_fingerprint(k) == key
        })
        .map(|(other, _)| other.as_str())
        .collect();
    (!others.is_empty()).then(|| {
        format!(
            "{voter}'s key is also pinned for {}, so one key counts as several voters",
            others.join(", ")
        )
    })
}

fn is_votes_drawer(drawer: &str) -> bool {
    drawer
        .lines()
        .next()
        .is_some_and(|l| l.trim().eq_ignore_ascii_case(":VOTES:"))
}

/// The line of `voter`'s ballot in a `:VOTES:` drawer of `text`, within
/// `span` when given. The last one, as a read keeps the last of two.
fn ballot_line(text: &str, span: Option<(usize, usize)>, voter: &str) -> Option<usize> {
    let (from, to) = span.unwrap_or((1, usize::MAX));
    let mut inside = false;
    let mut found = None;
    for (at, line) in text.lines().enumerate() {
        let at = at + 1;
        if at < from || at > to {
            continue;
        }
        let line = line.trim();
        if line.eq_ignore_ascii_case(":VOTES:") {
            inside = true;
        } else if line.eq_ignore_ascii_case(":END:") {
            inside = false;
        } else if inside && ballot_voter(line) == Some(voter) {
            found = Some(at);
        }
    }
    found
}

/// The voter of a `[STAMP] VOTER: CHOICE` drawer line.
fn ballot_voter(line: &str) -> Option<&str> {
    let (_, rest) = line.strip_prefix('[')?.split_once("] ")?;
    Some(rest.split_once(": ")?.0.trim())
}

/// Text form of a tracker check: one `FILE:LINE: ISSUE: REASON` line per
/// ballot that does not stand, then the count.
#[must_use]
pub fn format_tracker_report(report: &TrackerReport) -> String {
    let mut out = String::new();
    for bad in &report.bad {
        out.push_str(&format!(
            "{}:{}: {}: {}\n",
            bad.file.display(),
            bad.line,
            bad.issue,
            bad.reason
        ));
    }
    if report.bad.is_empty() {
        out.push_str(&format!(
            "record check: {} ballots on {} issues stand\n",
            report.ballots, report.issues
        ));
    } else {
        out.push_str(&format!(
            "record check: {} of {} ballots on {} issues do not stand\n",
            report.bad.len(),
            report.ballots,
            report.issues
        ));
    }
    if !report.legacy.is_empty() {
        out.push_str(&format!(
            "legacy unsigned, committed before signing began: {}\n",
            report.legacy.join(", ")
        ));
    }
    out
}

/// Canonical JSON bytes: recursive key sort, no insignificant whitespace.
///
/// # Errors
///
/// Serialization failed.
pub fn canonical_json(value: &Value) -> Result<Vec<u8>> {
    Ok(serde_json::to_vec(&canonical_value(value))?)
}

/// Ballot id from canonical ballot JSON without `id`.
///
/// # Errors
///
/// Serialization failed.
pub fn ballot_id(ballot: &BallotEntry) -> Result<String> {
    let mut value = serde_json::to_value(ballot)?;
    let Some(obj) = value.as_object_mut() else {
        bail!("ballot entry did not serialize as an object");
    };
    obj.remove("id");
    let hash = Sha256::digest(canonical_json(&value)?);
    let hex = hex::encode(hash);
    Ok(hex[..16].to_string())
}

/// Latest unreplaced ballot per voter and question.
///
/// Voters are counted per question, by signing key fingerprint. Copies
/// of one ballot under one key count as that ballot once. A ballot named
/// by another entry's `replaces` never counts. Of what remains, the
/// latest `cast_at` per key wins, and a latest `withdrawn` contributes no
/// vote. A seat that appears under two keys on one question counts under
/// neither there, whatever order the ballots come in. Two unreplaced ballots in
/// a voter's latest hour are ambiguous and that voter is dropped on that
/// question only; a tie in an earlier hour is superseded like any older
/// ballot.
///
/// # Errors
///
/// A `replaces` names no ballot, or one from another voter or question, or
/// a `cast_at` is not an hour timestamp.
pub fn tally(entries: &[SignedEntry]) -> Result<BTreeMap<String, BTreeMap<String, usize>>> {
    let mut copies = BTreeSet::new();
    let mut ballots: Vec<(&BallotEntry, String, Option<String>)> = Vec::new();
    for entry in entries {
        let Entry::Ballot(ballot) = &entry.entry else {
            continue;
        };
        let key = crate::ballot_sig::key_fingerprint(&entry.signature.key);
        // A second file with one ballot under one key is a copy, whatever
        // the hex case of its key and signature, not a second ballot.
        if !copies.insert((ballot_id(ballot)?, key.clone())) {
            continue;
        }
        let seat = split_voter(&ballot.voter).and_then(|(_, seat)| voter_seat(seat));
        ballots.push((ballot, key, seat));
    }
    let by_id: BTreeMap<&str, usize> = ballots
        .iter()
        .enumerate()
        .filter_map(|(at, (ballot, _, _))| ballot.id.as_deref().map(|id| (id, at)))
        .collect();
    let mut replaced = BTreeSet::new();
    for (ballot, key, _) in &ballots {
        let Some(target_id) = ballot.replaces.as_deref() else {
            continue;
        };
        let source_id = ballot.id.as_deref().unwrap_or("<missing-id>");
        let target_at = by_id.get(target_id).copied().ok_or_else(|| {
            anyhow!(
                "ballot {source_id} from {} on {} replaces {target_id}, but no ballot has that id",
                ballot.voter,
                ballot.question
            )
        })?;
        let (target, target_key, _) = &ballots[target_at];
        if target_key != key {
            bail!(
                "ballot {source_id} from {} on {} replaces {target_id} from {}; replaces must stay on the same voter",
                ballot.voter,
                ballot.question,
                target.voter
            );
        }
        if target.question != ballot.question {
            bail!(
                "ballot {source_id} from {} on {} replaces {target_id} on {}; replaces must stay on the same question",
                ballot.voter,
                ballot.question,
                target.question
            );
        }
        replaced.insert(target_id);
    }
    let mut grouped: BTreeMap<String, Vec<(usize, i64)>> = BTreeMap::new();
    for (at, (ballot, _, _)) in ballots.iter().enumerate() {
        if ballot.id.as_deref().is_some_and(|id| replaced.contains(id)) {
            continue;
        }
        let stamp = parse_hour_timestamp(&ballot.cast_at)
            .with_context(|| format!("ballot cast_at `{}`", ballot.cast_at))?
            .timestamp();
        grouped
            .entry(ballot.question.clone())
            .or_default()
            .push((at, stamp));
    }
    let mut counts: BTreeMap<String, BTreeMap<String, usize>> = BTreeMap::new();
    for (question, group) in grouped {
        let mut seat_keys: BTreeMap<&str, BTreeSet<&str>> = BTreeMap::new();
        for (at, _) in &group {
            if let Some(seat) = ballots[*at].2.as_deref() {
                seat_keys
                    .entry(seat)
                    .or_default()
                    .insert(ballots[*at].1.as_str());
            }
        }
        let mut by_key: BTreeMap<String, Vec<(i64, usize)>> = BTreeMap::new();
        for (at, stamp) in group {
            let shared = ballots[at]
                .2
                .as_deref()
                .is_some_and(|seat| seat_keys[seat].len() > 1);
            if shared {
                continue;
            }
            by_key
                .entry(ballots[at].1.clone())
                .or_default()
                .push((stamp, at));
        }
        for (_, votes) in by_key {
            let Some(latest) = votes.iter().map(|(stamp, _)| *stamp).max() else {
                continue;
            };
            let mut current = votes.iter().filter(|(stamp, _)| *stamp == latest);
            let (Some((_, at)), None) = (current.next(), current.next()) else {
                continue;
            };
            let ballot = ballots[*at].0;
            if ballot.stance == "withdrawn" {
                continue;
            }
            *counts
                .entry(question.clone())
                .or_default()
                .entry(ballot.stance.clone())
                .or_default() += 1;
        }
    }
    Ok(counts)
}

fn validate_unsigned(unsigned: &UnsignedEntry) -> Result<()> {
    if unsigned.schema != SCHEMA_V1 {
        bail!(
            "record schema must be {SCHEMA_V1}, got {}",
            unsigned.schema.trim()
        );
    }
    match &unsigned.entry {
        Entry::Question(question) => validate_question(question),
        Entry::Ballot(ballot) => validate_ballot(ballot),
    }
}

fn validate_question(question: &QuestionEntry) -> Result<()> {
    validate_question_type(&question.question_type)?;
    validate_question_id(&question.id, Some(&question.question_type))?;
    if question.title.trim().is_empty() {
        bail!("question title is required");
    }
    if question.options.is_empty() {
        bail!("question options must not be empty");
    }
    if question.options.iter().any(|o| o.trim().is_empty()) {
        bail!("question options must not contain empty values");
    }
    if question.anchors.is_empty() {
        bail!("question anchors must not be empty");
    }
    for anchor in &question.anchors {
        validate_anchor(anchor)?;
    }
    validate_voter_id(&question.opened_by)?;
    parse_hour_timestamp(&question.opened_at)?;
    if let Some(dup_of) = &question.dup_of {
        validate_question_id(dup_of, None)?;
    }
    Ok(())
}

fn validate_ballot(ballot: &BallotEntry) -> Result<()> {
    let id = ballot.id.as_deref().context("ballot id is required")?;
    if !is_hex_len(id, 16) {
        bail!("ballot id must be 16 hex chars");
    }
    let computed = ballot_id(ballot)?;
    if id != computed {
        bail!("ballot id mismatch: expected {computed}, got {id}");
    }
    validate_question_id(&ballot.question, None)?;
    if !BALLOT_STANCES.contains(&ballot.stance.as_str()) {
        bail!("ballot stance must be one of `accept`, `reject` or `withdrawn`");
    }
    if let Some(confidence) = ballot.confidence {
        if !(confidence > 0.0 && confidence <= 1.0) {
            bail!("ballot confidence must be in (0, 1]");
        }
    }
    validate_voter_id(&ballot.voter)?;
    if !is_hex_len(&ballot.commit, 40) {
        bail!("ballot commit must be a full 40-hex SHA");
    }
    if ballot.used.is_empty() {
        bail!("ballot used must name one or more evidence refs, or `none`");
    }
    if ballot.used.iter().any(|u| u.trim().is_empty()) {
        bail!("ballot used must not contain empty entries");
    }
    if ballot.lineage.trim().is_empty() {
        bail!("ballot lineage is required");
    }
    if let Some(replaces) = &ballot.replaces {
        if !is_hex_len(replaces, 16) {
            bail!("ballot replaces must be a 16-hex ballot id");
        }
    }
    if let Some(expect) = &ballot.expect {
        if expect.trim().is_empty() {
            bail!("ballot expect must not be empty");
        }
    }
    parse_hour_timestamp(&ballot.cast_at)?;
    Ok(())
}

fn validate_question_type(kind: &str) -> Result<()> {
    if QUESTION_TYPES.contains(&kind) {
        Ok(())
    } else {
        bail!("unsupported question type `{kind}`")
    }
}

fn validate_question_id(id: &str, expected_type: Option<&str>) -> Result<()> {
    let (kind, suffix) = id
        .split_once('-')
        .context("question id must be `<type>-<8 hex>`")?;
    validate_question_type(kind)?;
    if suffix.len() != 8
        || !suffix
            .bytes()
            .all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
    {
        bail!("question id suffix must be 8 lowercase hex chars");
    }
    if let Some(expected_type) = expected_type {
        if expected_type != kind {
            bail!("question id type `{kind}` does not match field `{expected_type}`");
        }
    }
    Ok(())
}

fn validate_anchor(anchor: &str) -> Result<()> {
    let (kind, rest) = anchor
        .split_once(':')
        .context("anchor must be `<kind>:<value>`")?;
    if rest.trim().is_empty() {
        bail!("anchor `{anchor}` has an empty value");
    }
    match kind {
        "sha" => {
            let (repo, sha) = rest
                .rsplit_once('@')
                .context("sha anchor must be `sha:<repo>@<40-hex>`")?;
            if repo.trim().is_empty() || !is_hex_len(sha, 40) {
                bail!("sha anchor must be `sha:<repo>@<40-hex>`");
            }
        }
        "sym" => {
            if !rest.contains("::") {
                bail!("sym anchor must contain `::`");
            }
        }
        "ticket" | "name" => {}
        "pr" => {
            let (repo, num) = rest
                .rsplit_once('#')
                .context("pr anchor must be `pr:<repo>#<number>`")?;
            if repo.trim().is_empty() || num.parse::<u64>().is_err() {
                bail!("pr anchor must be `pr:<repo>#<number>`");
            }
        }
        _ => bail!("unsupported anchor kind `{kind}`"),
    }
    Ok(())
}

fn validate_voter_id(voter: &str) -> Result<()> {
    let (identity, seat_or_run) =
        split_voter(voter).ok_or_else(|| anyhow!("voter id must be `<identity>/<seat-or-run>`"))?;
    if !valid_voter_part(identity) || !valid_voter_part(seat_or_run) {
        bail!(
            "voter id must be `<identity>/<seat-or-run>` using ascii letters, digits, '-', '_' or '.'"
        );
    }
    let canonical = canonical_voter(identity, seat_or_run)
        .ok_or_else(|| anyhow!("voter id `{voter}` needs a letter or digit on each side of `/`"))?;
    // `run/<id>` names no identity. A run carries one: `<identity>/run-<id>`.
    if canonical.starts_with("run/") {
        bail!("voter id `{voter}` has no identity; a run is `<identity>/run-<id>`");
    }
    if canonical != voter {
        bail!("voter id `{voter}` must be written `{canonical}`; sign the entry under that id");
    }
    Ok(())
}

/// A voter id written one way: the identity as its slug and the rest as
/// [`voter_seat`]. `None` when a part has no letter or digit.
fn canonical_voter(identity: &str, seat_or_run: &str) -> Option<String> {
    Some(format!(
        "{}/{}",
        slug_of(identity)?,
        voter_seat(seat_or_run)?
    ))
}

/// The seat `name` sits as, read the way the tracker reads a holder: a
/// `@TAG`, an old pid suffix and an old seat name fold to the seat, so
/// `Inky`, `inky@39u` and `inky-n9o` are all `inky` and `ljos-bot` is
/// `ljosbot`. A run keeps its id and a judge its model, version and all:
/// `run-101` stays, and `judge:model-1.5` is `judge-model-1-5`. `None`
/// when the name has no letter or digit.
fn voter_seat(name: &str) -> Option<String> {
    let seat = slug_of(name.split_once('@').map_or(name, |(seat, _)| seat))?;
    if seat.starts_with("run-") || seat.starts_with("judge-") {
        return Some(seat);
    }
    Some(crate::seat_slug(&crate::holder_parts(&seat).0))
}

fn slug_of(part: &str) -> Option<String> {
    part.bytes()
        .any(|b| b.is_ascii_alphanumeric())
        .then(|| crate::seat_slug(part))
}

fn parse_hour_timestamp(text: &str) -> Result<DateTime<FixedOffset>> {
    // Local offsets and subseconds are distinct byte strings for one
    // instant, and each would mint its own ballot id. The plain layer
    // accepts a single UTC hour form.
    if !utc_hour_shape(text) {
        bail!("timestamp `{text}` must be YYYY-MM-DDTHH:00:00Z");
    }
    DateTime::parse_from_rfc3339(text)
        .with_context(|| format!("timestamp `{text}` must be YYYY-MM-DDTHH:00:00Z"))
}

fn utc_hour_shape(text: &str) -> bool {
    let b = text.as_bytes();
    b.len() == 20
        && b[4] == b'-'
        && b[7] == b'-'
        && b[10] == b'T'
        && b[13] == b':'
        && b[16] == b':'
        && b[19] == b'Z'
        && &b[14..16] == b"00"
        && &b[17..19] == b"00"
        && b[..4].iter().all(u8::is_ascii_digit)
        && b[5..7].iter().all(u8::is_ascii_digit)
        && b[8..10].iter().all(u8::is_ascii_digit)
        && b[11..13].iter().all(u8::is_ascii_digit)
}

fn split_voter(voter: &str) -> Option<(&str, &str)> {
    let mut parts = voter.split('/');
    let identity = parts.next()?;
    let seat_or_run = parts.next()?;
    (parts.next().is_none() && !identity.is_empty() && !seat_or_run.is_empty())
        .then_some((identity, seat_or_run))
}

fn valid_voter_part(part: &str) -> bool {
    part.bytes()
        .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.'))
}

fn signing_payload(unsigned: &UnsignedEntry) -> Result<Vec<u8>> {
    let value = serde_json::to_value(unsigned)?;
    let canonical = canonical_json(&value)?;
    let mut payload = Vec::with_capacity(SIGNING_LABEL.len() + 1 + canonical.len());
    payload.extend_from_slice(SIGNING_LABEL.as_bytes());
    payload.push(b'\n');
    payload.extend_from_slice(&canonical);
    Ok(payload)
}

struct NoDuplicateKeys(Value);

impl<'de> Deserialize<'de> for NoDuplicateKeys {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct ValueVisitor;

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

            fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                formatter.write_str("a JSON value")
            }

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

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

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

            fn visit_f64<E>(self, value: f64) -> std::result::Result<Value, E>
            where
                E: de::Error,
            {
                let number = serde_json::Number::from_f64(value)
                    .ok_or_else(|| E::custom("JSON number is not finite"))?;
                Ok(Value::Number(number))
            }

            fn visit_str<E>(self, value: &str) -> std::result::Result<Value, E> {
                Ok(Value::String(value.to_string()))
            }

            fn visit_string<E>(self, value: String) -> std::result::Result<Value, E> {
                Ok(Value::String(value))
            }

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

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

            fn visit_seq<A>(self, mut seq: A) -> std::result::Result<Value, A::Error>
            where
                A: SeqAccess<'de>,
            {
                let mut items = Vec::new();
                while let Some(value) = seq.next_element::<NoDuplicateKeys>()? {
                    items.push(value.0);
                }
                Ok(Value::Array(items))
            }

            fn visit_map<A>(self, mut map: A) -> std::result::Result<Value, A::Error>
            where
                A: MapAccess<'de>,
            {
                let mut out = serde_json::Map::new();
                while let Some(key) = map.next_key::<String>()? {
                    if out.contains_key(&key) {
                        return Err(de::Error::custom(format!("duplicate key `{key}`")));
                    }
                    let value = map.next_value::<NoDuplicateKeys>()?;
                    out.insert(key, value.0);
                }
                Ok(Value::Object(out))
            }
        }

        deserializer.deserialize_any(ValueVisitor).map(Self)
    }
}

fn parse_json_without_duplicate_keys(raw: &str) -> Result<Value> {
    let mut de = serde_json::Deserializer::from_str(raw);
    let value = NoDuplicateKeys::deserialize(&mut de)
        .map(|value| value.0)
        .context("record entry is not JSON")?;
    de.end().context("record entry is not JSON")?;
    Ok(value)
}

/// Parse one record file. Unknown keys are not dropped before the reserved
/// name walk, and the canonical bytes without `signature` must be the typed
/// entry. A field added after signing then fails instead of riding along.
fn parse_signed_json(raw: &str) -> Result<SignedEntry> {
    let value = parse_json_without_duplicate_keys(raw)?;
    reject_reserved_plain_fields(&value)?;
    let entry: SignedEntry =
        serde_json::from_value(value.clone()).context("record entry does not match schema v1")?;
    require_signed_coverage(&value, &entry)?;
    Ok(entry)
}

fn require_signed_coverage(raw: &Value, entry: &SignedEntry) -> Result<()> {
    let Value::Object(map) = raw else {
        bail!("record entry must be a JSON object");
    };
    let mut unsigned = map.clone();
    unsigned.remove("signature");
    let raw_canon = canonical_json(&Value::Object(unsigned))?;
    let typed = serde_json::to_value(entry.unsigned())?;
    let typed_canon = canonical_json(&typed)?;
    if raw_canon != typed_canon {
        bail!("entry has a field the signature does not cover");
    }
    Ok(())
}

fn reject_reserved_plain_fields(value: &Value) -> Result<()> {
    reject_reserved_plain_fields_at(value, "$")
}

fn reject_reserved_plain_fields_at(value: &Value, path: &str) -> Result<()> {
    match value {
        Value::Object(map) => {
            for (k, v) in map {
                if RESERVED_PLAIN_FIELDS.contains(&k.as_str()) {
                    bail!("plain layer field `{k}` is not allowed at {path}");
                }
                let next = format!("{path}.{k}");
                reject_reserved_plain_fields_at(v, &next)?;
            }
        }
        Value::Array(items) => {
            for (i, item) in items.iter().enumerate() {
                let next = format!("{path}[{i}]");
                reject_reserved_plain_fields_at(item, &next)?;
            }
        }
        _ => {}
    }
    Ok(())
}

fn canonical_value(value: &Value) -> Value {
    match value {
        Value::Object(map) => {
            let mut keys: Vec<_> = map.keys().cloned().collect();
            keys.sort();
            let mut out = serde_json::Map::new();
            for key in keys {
                out.insert(key.clone(), canonical_value(&map[&key]));
            }
            Value::Object(out)
        }
        Value::Array(items) => Value::Array(items.iter().map(canonical_value).collect()),
        _ => value.clone(),
    }
}

fn is_hex_len(text: &str, len: usize) -> bool {
    crate::ballot_sig::is_hex_len(text, len)
}

fn entry_files(dir: &Path) -> Result<Vec<PathBuf>> {
    let mut pending = vec![dir.to_path_buf()];
    let mut files = Vec::new();
    while let Some(root) = pending.pop() {
        for row in std::fs::read_dir(&root)
            .with_context(|| format!("record check: read {}", root.display()))?
        {
            let row = row?;
            let path = row.path();
            let ty = row.file_type()?;
            if ty.is_dir() {
                pending.push(path);
            } else if ty.is_file()
                && path
                    .extension()
                    .and_then(|s| s.to_str())
                    .is_some_and(|e| e.eq_ignore_ascii_case("json"))
            {
                files.push(path);
            }
        }
    }
    files.sort();
    Ok(files)
}

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

    /// Hold a private seat-key directory for the rest of this test.
    fn test_home() -> (crate::EnvLock, tempfile::TempDir) {
        let lock = crate::test_env_lock();
        let home = tempfile::tempdir().expect("home");
        crate::with_env_mutation(|| {
            // SAFETY: this test holds the process environment lock.
            unsafe {
                std::env::set_var("XDG_DATA_HOME", home.path());
                std::env::remove_var("LJOS_SEAT_KEY");
                std::env::remove_var("LJOS_SEAT");
                std::env::remove_var("VISSUE_AGENT");
                std::env::remove_var("VISSUE_ROOT");
                std::env::remove_var("CURSOR_AGENT_ID");
            }
        });
        (lock, home)
    }

    fn sign_as(unsigned: UnsignedEntry, seed: u8) -> SignedEntry {
        let seat = signer_on_entry(&unsigned).to_string();
        let path = crate::ballot_sig::seat_key_path(&seat).expect("seat key path");
        if let Some(dir) = path.parent() {
            std::fs::create_dir_all(dir).expect("seat dir");
        }
        std::fs::write(&path, [seed; 32]).expect("seed");
        sign_entry(unsigned, &seat).unwrap_or_else(|err| panic!("seat-{seed}: {err:#}"))
    }

    /// Sign under the `[seed; 32]` key without the schema checks
    /// [`sign_entry`] runs first.
    fn sign_unchecked(mut unsigned: UnsignedEntry, seed: u8) -> SignedEntry {
        if let Entry::Ballot(ballot) = &mut unsigned.entry {
            ballot.id = Some(ballot_id(ballot).expect("id"));
        }
        let key = ed25519_dalek::SigningKey::from_bytes(&[seed; 32]);
        let payload = signing_payload(&unsigned).expect("payload");
        let (key, sig) = crate::ballot_sig::signed_hex(&key, &payload);
        SignedEntry {
            schema: unsigned.schema,
            entry: unsigned.entry,
            signature: EntrySignature { key, sig },
        }
    }

    fn signer_on_entry(unsigned: &UnsignedEntry) -> &str {
        match &unsigned.entry {
            Entry::Question(question) => &question.opened_by,
            Entry::Ballot(ballot) => &ballot.voter,
        }
    }

    fn question_entry() -> UnsignedEntry {
        UnsignedEntry {
            schema: SCHEMA_V1.to_string(),
            entry: Entry::Question(QuestionEntry {
                id: "correct-3f9a1c2e".to_string(),
                question_type: "correct".to_string(),
                title: "is this head mergeable".to_string(),
                options: vec!["accept".to_string(), "reject".to_string()],
                anchors: vec!["sha:owner/repo@0123456789abcdef0123456789abcdef01234567".to_string()],
                opened_by: "alpha/seat-a".to_string(),
                opened_at: "2026-10-21T09:00:00Z".to_string(),
                dup_of: None,
            }),
        }
    }

    fn ballot_entry(voter: &str, stance: &str, cast_at: &str) -> UnsignedEntry {
        UnsignedEntry {
            schema: SCHEMA_V1.to_string(),
            entry: Entry::Ballot(BallotEntry {
                id: None,
                question: "correct-3f9a1c2e".to_string(),
                stance: stance.to_string(),
                confidence: Some(0.9),
                voter: voter.to_string(),
                commit: "0123456789abcdef0123456789abcdef01234567".to_string(),
                used: vec!["none".to_string()],
                lineage: "model:synthetic".to_string(),
                replaces: None,
                expect: None,
                cast_at: cast_at.to_string(),
            }),
        }
    }

    fn ballot_on_question(
        voter: &str,
        stance: &str,
        cast_at: &str,
        question: &str,
    ) -> UnsignedEntry {
        let mut entry = ballot_entry(voter, stance, cast_at);
        let Entry::Ballot(ballot) = &mut entry.entry else {
            panic!("ballot");
        };
        ballot.question = question.to_string();
        entry
    }

    fn as_ballot(entry: SignedEntry) -> BallotEntry {
        match entry.entry {
            Entry::Ballot(ballot) => ballot,
            Entry::Question(_) => panic!("expected a ballot entry"),
        }
    }

    #[test]
    fn a_signed_entry_verifies() {
        let _home = test_home();
        let entry = sign_as(question_entry(), 7);
        verify_entry(&entry).expect("valid signature");
    }

    #[test]
    fn a_record_entry_is_signed_with_the_seat_ballot_key() {
        let _home = test_home();
        let entry = sign_as(question_entry(), 7);
        let path = crate::ballot_sig::seat_key_path("alpha/seat-a").expect("path");
        let key = crate::ballot_sig::load_or_create(&path).expect("seat key");
        assert_eq!(
            entry.signature.key,
            hex::encode(key.verifying_key().to_bytes())
        );
        verify_entry(&entry).expect("verifies under the seat key");
    }

    #[test]
    fn each_voter_signs_with_its_own_key_file() {
        let _home = test_home();
        let at = "2026-10-21T10:00:00Z";
        let one = sign_entry(ballot_entry("a/b-c", "accept", at), "a/b-c").expect("a/b-c");
        let two = sign_entry(ballot_entry("a-b/c", "accept", at), "a-b/c").expect("a-b/c");
        assert_ne!(
            one.signature.key, two.signature.key,
            "a/b-c and a-b/c signed with one key file"
        );
        let names = ["a/b_c", "a_b/c", "a_b_c", "a:b_c", "a%2fb_c", "", ".", ".."];
        let files: BTreeSet<PathBuf> = names
            .iter()
            .map(|name| crate::ballot_sig::seat_key_file(name).expect("path"))
            .collect();
        assert_eq!(files.len(), names.len(), "{files:?}");
    }

    /// Puts the captured variables back when it drops. The caller holds
    /// the environment mutation lock across that drop, so a parallel test
    /// cannot observe the temporary values, including after a panic.
    struct RestoreEnv {
        saved: Vec<(&'static str, Option<std::ffi::OsString>)>,
    }

    impl RestoreEnv {
        fn capture(keys: &[&'static str]) -> Self {
            Self {
                saved: keys
                    .iter()
                    .map(|key| (*key, std::env::var_os(key)))
                    .collect(),
            }
        }
    }

    impl Drop for RestoreEnv {
        fn drop(&mut self) {
            for (key, prev) in &self.saved {
                // SAFETY: the caller holds the process environment lock.
                unsafe {
                    match prev {
                        Some(value) => std::env::set_var(key, value),
                        None => std::env::remove_var(key),
                    }
                }
            }
        }
    }

    #[test]
    fn an_explicit_seat_key_is_used_as_given() {
        let _home = test_home();
        let root = tempfile::tempdir().expect("root");
        let given = root.path().join("seat.key");
        let path = crate::with_env_mutation(|| {
            let _restore = RestoreEnv::capture(&["LJOS_SEAT_KEY", "CURSOR_AGENT_ID"]);
            // SAFETY: this test holds the process environment lock.
            unsafe {
                std::env::set_var("LJOS_SEAT_KEY", &given);
                std::env::set_var("CURSOR_AGENT_ID", "agent-run-1111");
            }
            crate::ballot_sig::seat_key_path("alpha/seat-a")
        });
        assert_eq!(
            path.as_deref(),
            Some(given.as_path()),
            "LJOS_SEAT_KEY was not used as given"
        );
    }

    #[test]
    fn sign_entry_refuses_a_signer_not_named_on_the_entry() {
        let _home = test_home();
        let err = sign_entry(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            "seat-8",
        )
        .expect_err("mismatched signer");
        assert!(
            err_chain(&err).contains("voter"),
            "mismatched signer passed: {}",
            err_chain(&err)
        );
    }

    #[test]
    fn sign_entry_refuses_a_voter_id_in_another_spelling() {
        let _home = test_home();
        let at = "2026-10-21T10:00:00Z";
        for (written, canonical) in [
            ("Alpha/seat-a", "alpha/seat-a"),
            ("alpha./seat-a", "alpha/seat-a"),
            ("alpha_/seat-a", "alpha/seat-a"),
            ("alpha/Seat-a", "alpha/seat-a"),
            ("Alpha/run-101", "alpha/run-101"),
            ("alpha./run-101", "alpha/run-101"),
            ("alpha_/run-101", "alpha/run-101"),
            ("alpha/Run-101", "alpha/run-101"),
            ("alpha/run-101.", "alpha/run-101"),
            ("alpha/run--101", "alpha/run-101"),
            ("alpha/Inky", "alpha/inky"),
            ("alpha/inky-39u", "alpha/inky"),
            ("alpha/inky-n9o", "alpha/inky"),
            ("alpha/disk-saver-t4t", "alpha/disk-saver"),
            ("alpha/ljos-bot", "alpha/ljosbot"),
            ("alpha/GPT-5", "alpha/gpt-5"),
            ("alpha/Ridley-CI-A-555001", "alpha/ridley-ci-a-555001"),
            ("alpha/judge-Model-1.5", "alpha/judge-model-1-5"),
        ] {
            let err = sign_entry(ballot_entry(written, "accept", at), written).expect_err(written);
            let text = err_chain(&err);
            assert!(
                text.contains(&format!("must be written `{canonical}`")),
                "{written}: {text}"
            );
            sign_entry(ballot_entry(canonical, "accept", at), canonical)
                .unwrap_or_else(|err| panic!("{canonical}: {err:#}"));
        }
        for (voter, said) in [
            ("Run/run-101", "no identity"),
            ("run./run-101", "no identity"),
            ("alpha/...", "letter or digit"),
            ("_/seat-a", "letter or digit"),
        ] {
            let err = sign_entry(ballot_entry(voter, "accept", at), voter).expect_err(voter);
            assert!(err_chain(&err).contains(said), "{voter}: {err:#}");
        }
    }

    #[test]
    fn a_version_suffix_stays_part_of_the_seat() {
        let _home = test_home();
        let at = "2026-10-21T10:00:00Z";
        let entries = ["alpha/gpt-5", "alpha/gpt"].map(|voter| {
            sign_entry(ballot_entry(voter, "accept", at), voter)
                .unwrap_or_else(|err| panic!("{voter}: {err:#}"))
        });
        check_entries(&entries).expect("two seats");
        let read = tally(&entries).expect("tally");
        assert_eq!(counted(&read, "accept"), Some(2), "{read:?}");
        let err = sign_entry(ballot_entry("alpha/gpt-39u", "accept", at), "alpha/gpt-39u")
            .expect_err("a pid tag folds");
        let text = err_chain(&err);
        assert!(text.contains("must be written `alpha/gpt`"), "{text}");
    }

    #[test]
    fn a_stored_ballot_in_another_spelling_names_its_voter_id() {
        let _home = test_home();
        // One seed stands in for the key file `Alpha/seat-a` and
        // `alpha/seat-a` share on a case-insensitive volume.
        for (written, canonical) in [
            ("Alpha/seat-a", "alpha/seat-a"),
            ("alpha./seat-a", "alpha/seat-a"),
            ("alpha_/run-101", "alpha/run-101"),
            ("alpha/Run-101", "alpha/run-101"),
            ("alpha/inky-39u", "alpha/inky"),
            ("alpha/ljos-bot", "alpha/ljosbot"),
        ] {
            let kept = sign_as(ballot_entry(canonical, "accept", "2026-10-21T10:00:00Z"), 1);
            let stored = sign_unchecked(ballot_entry(written, "accept", "2026-10-21T11:00:00Z"), 1);
            let entries = [kept, stored];
            let Err(err) = check_entries(&entries) else {
                panic!("{written} passed beside {canonical}: {:?}", tally(&entries));
            };
            let text = err_chain(&err);
            assert!(
                text.contains(&format!("`{written}` must be written `{canonical}`")),
                "{text}"
            );
        }
    }

    #[test]
    fn a_one_byte_edit_fails_verification() {
        let _home = test_home();
        let mut entry = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            8,
        );
        let Entry::Ballot(ballot) = &mut entry.entry else {
            panic!("ballot expected");
        };
        // `lineage` feeds the ballot id. Recompute it so the failure is the
        // signature, not the id check that used to hide this edit.
        ballot.lineage.push('x');
        ballot.id = Some(ballot_id(ballot).expect("id"));
        let err = verify_entry(&entry).expect_err("edited ballot");
        assert!(
            err.to_string().contains("does not verify"),
            "edit never reached signature verification: {err}"
        );
    }

    #[test]
    fn a_small_order_key_is_refused() {
        let _home = test_home();
        let key = format!("01{}", "00".repeat(31));
        let sig = format!("01{}", "00".repeat(63));
        for stance in ["accept", "reject"] {
            let mut unsigned = ballot_entry("alpha/seat-a", stance, "2026-10-21T10:00:00Z");
            let Entry::Ballot(ballot) = &mut unsigned.entry else {
                panic!("ballot");
            };
            ballot.id = Some(ballot_id(ballot).expect("id"));
            let forged = SignedEntry {
                schema: unsigned.schema,
                entry: unsigned.entry,
                signature: EntrySignature {
                    key: key.clone(),
                    sig: sig.clone(),
                },
            };
            let err = check_value(&serde_json::to_value(&forged).expect("value"))
                .expect_err("a small-order key passed record check");
            assert!(
                err_chain(&err).contains("small-order"),
                "{stance}: {}",
                err_chain(&err)
            );
        }
        assert!(
            !crate::ballot_sig::signature_ok(&key, &sig, b"any message"),
            "the identity key verified a signature"
        );
    }

    #[test]
    fn number_and_unicode_spellings_follow_the_typed_form() {
        let _home = test_home();
        let mut unsigned = ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z");
        let Entry::Ballot(ballot) = &mut unsigned.entry else {
            panic!("ballot");
        };
        ballot.confidence = Some(1.0);
        ballot.lineage = "café".to_string();
        let signed = sign_as(unsigned, 8);
        let id = as_ballot(signed.clone()).id.clone().expect("id");
        let text = serde_json::to_string(&signed).expect("json");
        let checked = |body: &str| {
            let dir = tempfile::tempdir().expect("tempdir");
            std::fs::write(dir.path().join("entry.json"), body).expect("write");
            check_dir(dir.path())
        };
        let integer = checked(&text.replace("1.0", "1")).expect_err("integer 1");
        assert!(
            err_chain(&integer).contains("does not cover"),
            "{}",
            err_chain(&integer)
        );
        checked(&text.replace("1.0", "1e0")).expect("1e0");
        checked(&text.replace("1.0", "10E-1")).expect("10E-1");
        checked(&text.replace("café", "caf\\u00e9")).expect("unicode escape");
        let mut decomposed = ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z");
        let Entry::Ballot(ballot) = &mut decomposed.entry else {
            panic!("ballot");
        };
        ballot.confidence = Some(1.0);
        ballot.lineage = "cafe\u{0301}".to_string();
        let nfd = as_ballot(sign_as(decomposed, 8)).id.expect("nfd id");
        assert_ne!(id, nfd, "unicode normalisation changed the ballot id");
    }

    #[test]
    fn the_documented_ballot_id_matches_the_example() {
        let _home = test_home();
        let entry = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            8,
        );
        let id = as_ballot(entry).id.expect("id");
        let doc = include_str!("../doc/record-v1.md");
        assert!(
            doc.contains(&format!("\"id\": \"{id}\"")),
            "documented ballot id is stale; computed {id}"
        );
    }

    #[test]
    fn docs_name_first_seen_pin_scope() {
        let doc = include_str!("../doc/record-v1.md");
        let normalized = doc.split_whitespace().collect::<Vec<_>>().join(" ");
        assert!(
            normalized.contains(
                "none for its seat, the first key seen in this check is the pin for this check only"
            ),
            "record-v1.md must state that first-seen pins are check-local and yield to a seat pin"
        );
    }

    #[test]
    fn docs_name_the_deleted_withdrawal_limit() {
        let doc = include_str!("../doc/record-v1.md");
        let normalized = doc.split_whitespace().collect::<Vec<_>>().join(" ");
        assert!(
            normalized.contains("A deleted withdrawal revives the earlier ballot"),
            "record-v1.md must say the check cannot see a deleted withdrawal"
        );
    }

    #[test]
    fn docs_name_the_tracker_and_key_minting_limits() {
        let doc = include_str!("../doc/record-v1.md");
        let normalized = doc.split_whitespace().collect::<Vec<_>>().join(" ");
        assert!(
            normalized.contains("A ballot is not bound to a tracker"),
            "record-v1.md must say a ballot names no tracker"
        );
        assert!(
            normalized.contains("a new identity and seat signed with a new key is a new voter"),
            "record-v1.md must say a newly minted key is a new voter"
        );
    }

    #[test]
    fn docs_name_voter_pin_precedence_and_ambiguity_drop() {
        let doc = include_str!("../doc/record-v1.md");
        let normalized = doc.split_whitespace().collect::<Vec<_>>().join(" ");
        assert!(
            normalized.contains("a pin file row for a voter id also pins the seat after its `/`"),
            "record-v1.md must say a voter-id row pins its seat"
        );
        assert!(
            normalized.contains("are ambiguous and are dropped on that question only"),
            "record-v1.md must say same-hour ambiguity is local to one question"
        );
    }

    #[test]
    fn a_second_key_under_one_run_id_is_a_mismatch() {
        let _home = test_home();
        let first = sign_as(
            ballot_entry("alpha/run-101", "accept", "2026-10-21T10:00:00Z"),
            1,
        );
        let second = sign_as(
            ballot_entry("alpha/run-101", "reject", "2026-10-21T11:00:00Z"),
            2,
        );
        let err = check_entries(&[first, second]).expect_err("must reject run key mismatch");
        let text = err_chain(&err);
        assert!(text.contains("pinned"), "{text}");
        assert!(text.contains("none: first-seen in this check"), "{text}");
    }

    #[test]
    fn a_seat_voter_is_pinned_to_one_key() {
        let _home = test_home();
        let first = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            1,
        );
        let second = sign_as(
            ballot_entry("alpha/seat-a", "reject", "2026-10-21T11:00:00Z"),
            2,
        );
        let err = check_entries(&[first, second]).expect_err("seat pin");
        assert!(err_chain(&err).contains("pinned"), "{}", err_chain(&err));
    }

    #[test]
    fn a_question_opener_is_pinned_with_the_voter() {
        let _home = test_home();
        let question = sign_as(question_entry(), 1);
        let ballot = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            2,
        );
        let err = check_entries(&[question, ballot]).expect_err("opener pin");
        assert!(err_chain(&err).contains("pinned"), "{}", err_chain(&err));
    }

    #[test]
    fn a_run_keeps_its_identity_and_one_key() {
        let _home = test_home();
        let err = sign_entry(
            ballot_entry("run/run-101", "accept", "2026-10-21T10:00:00Z"),
            "run/run-101",
        )
        .expect_err("bare run identity");
        assert!(err_chain(&err).contains("identity"), "{}", err_chain(&err));
        let first = sign_as(
            ballot_entry("alpha/run-101", "accept", "2026-10-21T10:00:00Z"),
            1,
        );
        let second = sign_as(
            ballot_entry("alpha/run-101", "reject", "2026-10-21T11:00:00Z"),
            2,
        );
        let err = check_entries(&[first, second]).expect_err("run pin");
        assert!(err_chain(&err).contains("pinned"), "{}", err_chain(&err));
    }

    #[test]
    fn latest_ballot_per_voter_wins_and_withdrawn_does_not_count() {
        let _home = test_home();
        let a1 = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            3,
        );
        let a2 = sign_as(
            ballot_entry("alpha/seat-a", "withdrawn", "2026-10-21T11:00:00Z"),
            3,
        );
        let b1 = sign_as(
            ballot_entry("beta/seat-b", "accept", "2026-10-21T10:00:00Z"),
            4,
        );
        let read = tally(&[a1, a2, b1]).expect("tally");
        let by_stance = read.get("correct-3f9a1c2e").expect("question tally");
        assert_eq!(by_stance.get("accept"), Some(&1));
        assert_eq!(by_stance.get("withdrawn"), None);
    }

    #[test]
    fn a_question_id_in_capitals_is_refused() {
        let _home = test_home();
        let at = "2026-10-21T10:00:00Z";
        let lower = sign_as(ballot_entry("alpha/seat-a", "accept", at), 3);
        let mut upper = ballot_entry("alpha/seat-a", "accept", at);
        let Entry::Ballot(ballot) = &mut upper.entry else {
            panic!("ballot");
        };
        ballot.question = "correct-3F9A1C2E".to_string();
        let upper = sign_unchecked(upper, 3);
        let entries = [lower, upper];
        let Err(err) = check_entries(&entries) else {
            panic!("one voter counted twice: {:?}", tally(&entries));
        };
        assert!(err_chain(&err).contains("lowercase"), "{}", err_chain(&err));
    }

    #[test]
    fn two_unreplaced_ballots_in_one_hour_are_flagged() {
        let _home = test_home();
        let accept = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            3,
        );
        let reject = sign_as(
            ballot_entry("alpha/seat-a", "reject", "2026-10-21T10:00:00Z"),
            3,
        );
        let read = tally(&[accept, reject]).expect("same-hour ballots are dropped");
        assert!(read.is_empty(), "{read:?}");
    }

    #[test]
    fn a_later_ballot_stands_past_a_same_hour_pair() {
        let _home = test_home();
        let cast = |stance: &str, hour: u8| {
            sign_as(
                ballot_entry("alpha/seat-a", stance, &format!("2026-10-21T{hour}:00:00Z")),
                3,
            )
        };
        let entries = [cast("accept", 10), cast("reject", 10), cast("accept", 12)];
        let read = tally(&entries).expect("tally");
        assert_eq!(
            counted(&read, "accept"),
            Some(1),
            "a same-hour pair hid a later ballot: {read:?}"
        );
        assert_eq!(counted(&read, "reject"), None, "{read:?}");
        let entries = [cast("accept", 10), cast("accept", 12), cast("reject", 12)];
        let read = tally(&entries).expect("tally");
        assert!(read.is_empty(), "a latest same-hour pair counted: {read:?}");
    }

    fn counted(read: &BTreeMap<String, BTreeMap<String, usize>>, stance: &str) -> Option<usize> {
        read.get("correct-3f9a1c2e")
            .and_then(|stances| stances.get(stance))
            .copied()
    }

    #[test]
    fn a_replayed_ballot_does_not_erase_its_voter() {
        let _home = test_home();
        let cast = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            3,
        );
        let mut shouted = cast.clone();
        shouted.signature.key = shouted.signature.key.to_ascii_uppercase();
        shouted.signature.sig = shouted.signature.sig.to_ascii_uppercase();
        let later = sign_as(
            ballot_entry("alpha/seat-a", "reject", "2026-10-21T12:00:00Z"),
            3,
        );
        for (name, entries, stance) in [
            ("copy", vec![cast.clone(), cast.clone()], "accept"),
            ("hex case", vec![cast.clone(), shouted.clone()], "accept"),
            (
                "superseded copy",
                vec![cast.clone(), later.clone(), cast.clone()],
                "reject",
            ),
        ] {
            let report = check_entries(&entries).unwrap_or_else(|err| panic!("{name}: {err:#}"));
            let read = tally(&entries).expect("tally");
            assert_eq!(
                (report.questions_with_live_ballots, counted(&read, stance)),
                (1, Some(1)),
                "a replayed ballot erased its voter ({name}): {read:?}"
            );
        }
    }

    #[test]
    fn one_key_counts_once_on_a_question() {
        let _home = test_home();
        let entries = [
            sign_as(
                ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
                3,
            ),
            sign_as(
                ballot_entry("beta/seat-b", "accept", "2026-10-21T11:00:00Z"),
                3,
            ),
            sign_as(
                ballot_entry("alpha/run-101", "reject", "2026-10-21T12:00:00Z"),
                3,
            ),
            sign_as(
                ballot_entry("gamma/seat-c", "accept", "2026-10-21T10:00:00Z"),
                4,
            ),
        ];
        let report = check_entries(&entries).expect("check");
        assert_eq!(report.questions_with_live_ballots, 1);
        let read = tally(&entries).expect("tally");
        assert_eq!(counted(&read, "reject"), Some(1), "{read:?}");
        assert_eq!(counted(&read, "accept"), Some(1), "{read:?}");
    }

    #[test]
    fn distinct_persona_seats_with_distinct_keys_count_apart() {
        let _home = test_home();
        let entries = [
            sign_as(
                ballot_entry("ada/persona-a", "accept", "2026-10-21T10:00:00Z"),
                1,
            ),
            sign_as(
                ballot_entry("grace/persona-b", "reject", "2026-10-21T11:00:00Z"),
                2,
            ),
        ];
        let read = tally(&entries).expect("tally");
        assert_eq!(counted(&read, "accept"), Some(1), "{read:?}");
        assert_eq!(counted(&read, "reject"), Some(1), "{read:?}");
    }

    #[test]
    fn key_links_on_another_question_do_not_merge_voters_here() {
        let _home = test_home();
        let entries = [
            sign_as(
                ballot_on_question(
                    "alpha/seat-a",
                    "accept",
                    "2026-10-21T10:00:00Z",
                    "correct-3f9a1c2e",
                ),
                1,
            ),
            sign_as(
                ballot_on_question(
                    "beta/seat-b",
                    "reject",
                    "2026-10-21T11:00:00Z",
                    "correct-3f9a1c2e",
                ),
                2,
            ),
            sign_as(
                ballot_on_question(
                    "gamma/seat-a",
                    "accept",
                    "2026-10-21T12:00:00Z",
                    "change-deadbeef",
                ),
                2,
            ),
        ];
        let read = tally(&entries).expect("tally");
        let first = read.get("correct-3f9a1c2e").expect("first question");
        assert_eq!(first.get("accept"), Some(&1), "{read:?}");
        assert_eq!(first.get("reject"), Some(&1), "{read:?}");
    }

    #[test]
    fn spellings_of_one_voter_under_one_key_count_once() {
        let _home = test_home();
        let spellings = [
            "alpha/seat-a",
            "Alpha/seat-a",
            "alpha./seat-a",
            "alpha_/seat-a",
            "alpha/Seat-a",
            "alpha/run-101",
            "Alpha/run-101",
            "alpha./run-101",
            "alpha_/run-101",
            "alpha/inky",
            "alpha/inky-39u",
            "alpha/ljos-bot",
            "alpha/ljosbot",
            "alpha/judge-model-1-5",
        ];
        let entries: Vec<SignedEntry> = spellings
            .iter()
            .zip(1..)
            .map(|(voter, hour)| {
                let at = format!("2026-10-21T{hour:02}:00:00Z");
                sign_unchecked(ballot_entry(voter, "accept", &at), 3)
            })
            .collect();
        let read = tally(&entries).expect("tally");
        assert_eq!(counted(&read, "accept"), Some(1), "{read:?}");
    }

    #[test]
    fn one_seat_under_two_keys_counts_under_neither() {
        let _home = test_home();
        let entries = [
            sign_as(
                ballot_entry("alpha/inky", "accept", "2026-10-21T10:00:00Z"),
                1,
            ),
            sign_as(
                ballot_entry("beta/inky", "reject", "2026-10-21T11:00:00Z"),
                2,
            ),
        ];
        let err = check_entries(&entries).expect_err("one seat under two keys");
        assert!(
            err_chain(&err).contains("seat inky signs under two keys"),
            "{}",
            err_chain(&err)
        );
        let read = tally(&entries).expect("tally");
        assert!(read.is_empty(), "{read:?}");
    }

    fn orders<T: Clone>(items: &[T]) -> Vec<Vec<T>> {
        if items.len() <= 1 {
            return vec![items.to_vec()];
        }
        let mut out = Vec::new();
        for at in 0..items.len() {
            let mut rest = items.to_vec();
            let first = rest.remove(at);
            for mut tail in orders(&rest) {
                tail.insert(0, first.clone());
                out.push(tail);
            }
        }
        out
    }

    #[test]
    fn a_seat_under_two_keys_reads_the_same_in_every_order() {
        let _home = test_home();
        let entries = [
            sign_as(
                ballot_entry("alpha/inky", "accept", "2026-10-21T10:00:00Z"),
                1,
            ),
            sign_as(
                ballot_entry("beta/inky", "reject", "2026-10-21T11:00:00Z"),
                2,
            ),
            sign_as(
                ballot_entry("gamma/seat-c", "accept", "2026-10-21T10:00:00Z"),
                3,
            ),
            sign_as(
                ballot_entry("delta/seat-d", "reject", "2026-10-21T11:00:00Z"),
                4,
            ),
        ];
        let read = |order: &[SignedEntry]| {
            let check = check_entries(order).map_err(|err| err_chain(&err));
            (
                check.map(|report| report.questions_with_live_ballots),
                tally(order).expect("tally"),
            )
        };
        let first = read(&entries);
        for order in orders(&entries) {
            assert_eq!(read(&order), first, "file order changed the result");
        }
        let (check, tallied) = first;
        let err = check.expect_err("a seat signed under two keys passed the check");
        assert!(err.contains("seat inky"), "{err}");
        assert_eq!(counted(&tallied, "accept"), Some(1), "{tallied:?}");
        assert_eq!(counted(&tallied, "reject"), Some(1), "{tallied:?}");
    }

    #[test]
    fn a_digit_suffix_is_a_seat_of_its_own() {
        let _home = test_home();
        let entries = [
            sign_as(
                ballot_entry("alpha/inky", "accept", "2026-10-21T10:00:00Z"),
                1,
            ),
            sign_as(
                ballot_entry("beta/inky-10", "accept", "2026-10-21T11:00:00Z"),
                2,
            ),
        ];
        check_entries(&entries).expect("two seats");
        let read = tally(&entries).expect("tally");
        assert_eq!(counted(&read, "accept"), Some(2), "{read:?}");
    }

    #[test]
    fn runs_and_judges_keep_their_ids_whole() {
        let _home = test_home();
        for (one, two) in [
            ("alpha/judge-gpt-oss-120b", "beta/judge-gpt-oss-20b"),
            ("alpha/judge-llama-3-70b", "beta/judge-llama-3-8b"),
            ("alpha/judge-qwen", "beta/judge-qwen-72"),
            ("alpha/run-pr-1234", "beta/run-pr-5678"),
            ("alpha/judge-grok-4", "beta/grok-4"),
        ] {
            let entries = [
                sign_as(ballot_entry(one, "accept", "2026-10-21T10:00:00Z"), 1),
                sign_as(ballot_entry(two, "accept", "2026-10-21T11:00:00Z"), 2),
            ];
            check_entries(&entries).unwrap_or_else(|err| panic!("{one} {two}: {err:#}"));
            let read = tally(&entries).expect("tally");
            assert_eq!(
                counted(&read, "accept"),
                Some(2),
                "{one} and {two} were merged into one voter"
            );
        }
    }

    #[test]
    fn one_key_under_two_voters_in_one_hour_is_flagged() {
        let _home = test_home();
        let at = "2026-10-21T10:00:00Z";
        let entries = [
            sign_as(ballot_entry("alpha/seat-a", "accept", at), 3),
            sign_as(ballot_entry("beta/seat-b", "reject", at), 3),
        ];
        let report = check_entries(&entries).expect("one key, one-hour ambiguity");
        assert_eq!(report.questions_with_live_ballots, 0, "{report:?}");
        let read = tally(&entries).expect("tally");
        assert!(read.is_empty(), "{read:?}");
    }

    #[test]
    fn a_key_in_capital_hex_is_the_same_key() {
        let _home = test_home();
        let lower = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            3,
        );
        let mut upper = sign_as(
            ballot_entry("beta/seat-b", "accept", "2026-10-21T11:00:00Z"),
            3,
        );
        upper.signature.key = upper.signature.key.to_ascii_uppercase();
        assert_ne!(lower.signature.key, upper.signature.key);
        let entries = [lower, upper];
        check_entries(&entries).expect("capital hex verifies");
        let read = tally(&entries).expect("tally");
        assert_eq!(counted(&read, "accept"), Some(1), "{read:?}");
    }

    #[test]
    fn a_key_with_a_small_order_part_is_the_same_key() {
        let _home = test_home();
        let seed = [3u8; 32];
        let first = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            3,
        );
        let mut order_two = [0xff; 32];
        order_two[0] = 0xec;
        order_two[31] = 0x7f;
        let order_two = ed25519_dalek::VerifyingKey::from_bytes(&order_two).expect("point");
        assert!(order_two.is_weak());
        let public = ed25519_dalek::SigningKey::from_bytes(&seed).verifying_key();
        let twin = (public.to_edwards() + order_two.to_edwards())
            .compress()
            .to_bytes();
        let twin = ed25519_dalek::VerifyingKey::from_bytes(&twin).expect("twin");
        assert!(!twin.is_weak());
        let secret = ed25519_dalek::hazmat::ExpandedSecretKey::from(&seed);
        // Under the twin a signature verifies only when its challenge is even.
        let second = (11..24)
            .find_map(|hour| {
                let at = format!("2026-10-21T{hour}:00:00Z");
                let mut unsigned = ballot_entry("beta/seat-b", "accept", &at);
                let Entry::Ballot(ballot) = &mut unsigned.entry else {
                    panic!("ballot");
                };
                ballot.id = Some(ballot_id(ballot).expect("id"));
                let payload = signing_payload(&unsigned).expect("payload");
                let sig = ed25519_dalek::hazmat::raw_sign::<sha2::Sha512>(&secret, &payload, &twin);
                twin.verify_strict(&payload, &sig)
                    .is_ok()
                    .then(|| SignedEntry {
                        schema: unsigned.schema,
                        entry: unsigned.entry,
                        signature: EntrySignature {
                            key: hex::encode(twin.to_bytes()),
                            sig: hex::encode(sig.to_bytes()),
                        },
                    })
            })
            .expect("a ballot the twin key verifies");
        let entries = [first, second];
        check_entries(&entries).expect("both keys verify");
        let read = tally(&entries).expect("tally");
        assert_eq!(counted(&read, "accept"), Some(1), "{read:?}");
    }

    fn with_replaces(mut entry: UnsignedEntry, id: &str) -> UnsignedEntry {
        let Entry::Ballot(ballot) = &mut entry.entry else {
            panic!("ballot");
        };
        ballot.replaces = Some(id.to_string());
        entry
    }

    #[test]
    fn a_same_hour_withdrawal_does_not_count_in_either_order() {
        let _home = test_home();
        let cast = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            3,
        );
        let cast_id = as_ballot(cast.clone()).id.clone().expect("id");
        let withdrawal = sign_as(
            with_replaces(
                ballot_entry("alpha/seat-a", "withdrawn", "2026-10-21T10:00:00Z"),
                &cast_id,
            ),
            3,
        );
        for (name, order) in [
            (
                "cast then withdrawal",
                vec![cast.clone(), withdrawal.clone()],
            ),
            (
                "withdrawal then cast",
                vec![withdrawal.clone(), cast.clone()],
            ),
        ] {
            let read = tally(&order).unwrap_or_else(|err| panic!("{name}: {err:#}"));
            let accepts = read
                .get("correct-3f9a1c2e")
                .and_then(|stances| stances.get("accept"))
                .copied();
            assert_eq!(accepts, None, "{name}: {read:?}");
        }
        for (cast_name, withdraw_name) in [("a.json", "b.json"), ("b.json", "a.json")] {
            let dir = tempfile::tempdir().expect("tempdir");
            std::fs::write(
                dir.path().join(cast_name),
                serde_json::to_vec(&cast).expect("json"),
            )
            .expect("write cast");
            std::fs::write(
                dir.path().join(withdraw_name),
                serde_json::to_vec(&withdrawal).expect("json"),
            )
            .expect("write withdrawal");
            let report = check_dir(dir.path()).unwrap_or_else(|err| panic!("{cast_name}: {err:#}"));
            assert_eq!(
                report.questions_with_live_ballots, 0,
                "{cast_name} before {withdraw_name}"
            );
        }
    }

    #[test]
    fn replaces_must_target_the_same_voter_and_question() {
        let _home = test_home();
        let missing = sign_as(
            with_replaces(
                ballot_entry("alpha/seat-a", "withdrawn", "2026-10-21T11:00:00Z"),
                "deadbeefdeadbeef",
            ),
            3,
        );
        let err = tally(std::slice::from_ref(&missing)).expect_err("missing replace target");
        assert!(
            err_chain(&err).contains("replaces"),
            "missing replace target passed: {}",
            err_chain(&err)
        );

        let cast = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            3,
        );
        let cast_id = as_ballot(cast.clone()).id.expect("id");
        let same_key_other_voter = sign_as(
            with_replaces(
                ballot_entry("beta/seat-b", "withdrawn", "2026-10-21T11:00:00Z"),
                &cast_id,
            ),
            3,
        );
        let read = tally(&[cast.clone(), same_key_other_voter])
            .expect("one key is one voter across voter ids");
        assert_eq!(counted(&read, "accept"), None, "{read:?}");

        let other_voter = sign_as(
            with_replaces(
                ballot_entry("beta/seat-b", "withdrawn", "2026-10-21T11:00:00Z"),
                &cast_id,
            ),
            4,
        );
        let err = tally(&[cast.clone(), other_voter]).expect_err("cross-voter replace");
        assert!(
            err_chain(&err).contains("same voter"),
            "cross-voter replace passed: {}",
            err_chain(&err)
        );

        let mut other_question_unsigned = with_replaces(
            ballot_entry("alpha/seat-a", "withdrawn", "2026-10-21T11:00:00Z"),
            &cast_id,
        );
        let Entry::Ballot(ballot) = &mut other_question_unsigned.entry else {
            panic!("ballot");
        };
        ballot.question = "change-deadbeef".to_string();
        let other_question = sign_as(other_question_unsigned, 3);
        let err = tally(&[cast, other_question]).expect_err("cross-question replace");
        assert!(
            err_chain(&err).contains("same question"),
            "cross-question replace passed: {}",
            err_chain(&err)
        );
    }

    #[test]
    fn check_dir_reads_and_verifies_json_entries() {
        let _home = test_home();
        let dir = tempfile::tempdir().expect("tempdir");
        let q = sign_as(question_entry(), 5);
        let b = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            5,
        );
        std::fs::write(
            dir.path().join("question.json"),
            serde_json::to_string_pretty(&q).expect("json"),
        )
        .expect("write question");
        std::fs::write(
            dir.path().join("ballot.json"),
            serde_json::to_string_pretty(&b).expect("json"),
        )
        .expect("write ballot");
        let report = check_dir(dir.path()).expect("check");
        assert_eq!(report.files, 2);
        assert_eq!(report.entries, 2);
        assert_eq!(report.questions, 1);
        assert_eq!(report.ballots, 1);
        assert_eq!(report.questions_with_live_ballots, 1);
    }

    fn err_chain(err: &anyhow::Error) -> String {
        format!("{err:#}")
    }

    #[test]
    fn timestamps_must_be_utc_hours() {
        let _home = test_home();
        for bad in [
            "2026-10-21T10:00:00+05:30",
            "2026-10-21T04:00:00-05:00",
            "2026-10-21T10:00:00.000Z",
            "2026-10-21t10:00:00z",
            "2026-10-21 10:00:00Z",
            "2026-10-21T10:30:00Z",
        ] {
            let err = sign_entry(ballot_entry("alpha/seat-a", "accept", bad), "alpha/seat-a")
                .expect_err(bad);
            let text = err_chain(&err);
            assert!(
                text.contains("YYYY-MM-DDTHH:00:00Z"),
                "{bad} was accepted or the error hid the form: {text}"
            );
        }
        let mut opened = question_entry();
        let Entry::Question(question) = &mut opened.entry else {
            panic!("question");
        };
        question.opened_at = "2026-10-21T09:00:00+00:00".to_string();
        let err = sign_entry(opened, "alpha/seat-a").expect_err("offset opened_at");
        assert!(
            err_chain(&err).contains("YYYY-MM-DDTHH:00:00Z"),
            "{}",
            err_chain(&err)
        );
        sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            8,
        );
    }

    #[test]
    fn stance_spelling_must_be_exact_lowercase() {
        let _home = test_home();
        for bad in ["accept ", "accept\u{200b}", "Accept", "WITHDRAWN"] {
            let err = sign_entry(
                ballot_entry("alpha/seat-a", bad, "2026-10-21T10:00:00Z"),
                "alpha/seat-a",
            )
            .expect_err(bad);
            assert!(
                err_chain(&err).contains("stance"),
                "{bad} passed stance validation: {}",
                err_chain(&err)
            );
        }
        for ok in ["accept", "reject", "withdrawn"] {
            sign_entry(
                ballot_entry("alpha/seat-a", ok, "2026-10-21T10:00:00Z"),
                "alpha/seat-a",
            )
            .unwrap_or_else(|err| panic!("{ok}: {err:#}"));
        }
    }

    /// Reserved names and any key the signature does not cover are refused
    /// on the file, including inside `signature`.
    #[test]
    fn reserved_and_unsigned_fields_are_refused() {
        let _home = test_home();
        let signed = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            8,
        );
        let base = serde_json::to_value(&signed).expect("value");

        let mut host_in_sig = base.clone();
        host_in_sig["signature"]["host"] = serde_json::json!("box");
        let err = check_value(&host_in_sig).expect_err("host inside signature");
        assert!(
            err_chain(&err).contains("host"),
            "reserved host was accepted: {}",
            err_chain(&err)
        );

        let mut nested_agent = base.clone();
        nested_agent["signature"]["note"] = serde_json::json!({"agent": "runner"});
        let err = check_value(&nested_agent).expect_err("nested agent");
        assert!(
            err_chain(&err).contains("agent"),
            "nested agent was accepted: {}",
            err_chain(&err)
        );

        let mut session_in_array = base.clone();
        session_in_array["signature"]["meta"] = serde_json::json!([{"session": "s"}]);
        let err = check_value(&session_in_array).expect_err("session in array");
        assert!(
            err_chain(&err).contains("session"),
            "session in an array was accepted: {}",
            err_chain(&err)
        );

        let mut rationale = base.clone();
        rationale["rationale"] = serde_json::json!("because");
        let err = check_value(&rationale).expect_err("unsigned rationale");
        assert!(
            err_chain(&err).contains("does not cover"),
            "unsigned rationale was accepted: {}",
            err_chain(&err)
        );
    }

    #[test]
    fn unknown_signature_fields_are_refused() {
        let _home = test_home();
        let signed = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            8,
        );
        let base = serde_json::to_value(&signed).expect("value");
        for (field, value) in [
            ("rationale", serde_json::json!("because")),
            ("findings", serde_json::json!(["one"])),
            ("Host", serde_json::json!("box")),
        ] {
            let mut changed = base.clone();
            changed["signature"][field] = value;
            let err = check_value(&changed).expect_err(field);
            assert!(
                err_chain(&err).contains(field),
                "{field} passed under signature: {}",
                err_chain(&err)
            );
        }
        let mut nested = base.clone();
        nested["signature"]["note"] = serde_json::json!({"Session": "s"});
        let err = check_value(&nested).expect_err("nested note.Session");
        assert!(
            err_chain(&err).contains("note"),
            "nested note.Session passed under signature: {}",
            err_chain(&err)
        );
    }

    #[test]
    fn duplicate_json_keys_are_refused() {
        let _home = test_home();
        let signed = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            8,
        );
        let text = serde_json::to_string(&signed).expect("json");

        let dup_stance = text.replacen(
            "\"stance\":\"accept\",",
            "\"stance\":\"accept\",\"stance\":\"accept\",",
            1,
        );
        assert_ne!(dup_stance, text, "duplicate stance fixture");
        let err = check_raw(&dup_stance).expect_err("duplicate stance");
        assert!(
            err_chain(&err).contains("duplicate key `stance`"),
            "duplicate stance was not refused as duplicate: {}",
            err_chain(&err)
        );

        let sig = format!("\"sig\":\"{}\"", signed.signature.sig);
        let dup_sig = text.replacen(&sig, &format!("{sig},{sig}"), 1);
        assert_ne!(dup_sig, text, "duplicate signature fixture");
        let err = check_raw(&dup_sig).expect_err("duplicate signature");
        assert!(
            err_chain(&err).contains("duplicate key `sig`"),
            "duplicate signature key was not refused as duplicate: {}",
            err_chain(&err)
        );
    }

    #[test]
    fn a_pin_file_rejects_a_different_run_key() {
        let _home = test_home();
        let root = tempfile::tempdir().expect("root");
        crate::with_env_mutation(|| {
            // SAFETY: this test holds the process environment lock.
            unsafe { std::env::set_var("VISSUE_ROOT", root.path()) };
        });
        let entry = sign_as(
            ballot_entry("alpha/run-101", "accept", "2026-10-21T10:00:00Z"),
            1,
        );
        let other = sign_as(
            ballot_entry("beta/seat-b", "reject", "2026-10-21T11:00:00Z"),
            2,
        );
        let line = format!("alpha/run-101 {}\n", other.signature.key);
        let pin_path = root.path().join(crate::ballot_sig::PIN_FILE);
        std::fs::write(&pin_path, line).expect("pin");
        let err = check_entries(std::slice::from_ref(&entry)).expect_err("pinned key");
        let text = err_chain(&err);
        assert!(text.contains("pinned"), "{text}");
        assert!(text.contains(pin_path.to_string_lossy().as_ref()), "{text}");
    }

    #[test]
    fn unreadable_pin_file_is_refused() {
        let _home = test_home();
        let root = tempfile::tempdir().expect("root");
        crate::with_env_mutation(|| {
            // SAFETY: this test holds the process environment lock.
            unsafe { std::env::set_var("VISSUE_ROOT", root.path()) };
        });
        let pin_path = root.path().join(crate::ballot_sig::PIN_FILE);
        std::fs::write(&pin_path, "alpha/run-101 deadbeef\n").expect("pin");
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = std::fs::metadata(&pin_path).expect("meta").permissions();
            perms.set_mode(0o000);
            std::fs::set_permissions(&pin_path, perms).expect("chmod");
        }
        let entry = sign_as(
            ballot_entry("alpha/run-101", "accept", "2026-10-21T10:00:00Z"),
            1,
        );
        let err = check_entries(std::slice::from_ref(&entry)).expect_err("unreadable pin file");
        assert!(
            err_chain(&err).contains(pin_path.to_string_lossy().as_ref()),
            "{}",
            err_chain(&err)
        );
    }

    #[test]
    fn a_dangling_pin_file_link_is_refused() {
        let _home = test_home();
        let root = tempfile::tempdir().expect("root");
        crate::with_env_mutation(|| {
            // SAFETY: this test holds the process environment lock.
            unsafe { std::env::set_var("VISSUE_ROOT", root.path()) };
        });
        let pin_path = root.path().join(crate::ballot_sig::PIN_FILE);
        std::os::unix::fs::symlink(root.path().join("gone"), &pin_path).expect("symlink");
        let entry = sign_as(
            ballot_entry("alpha/run-101", "accept", "2026-10-21T10:00:00Z"),
            1,
        );
        let err = check_entries(std::slice::from_ref(&entry))
            .expect_err("a dangling pin file link read as no pin file");
        assert!(
            err_chain(&err).contains(pin_path.to_string_lossy().as_ref()),
            "{}",
            err_chain(&err)
        );
    }

    #[test]
    fn duplicate_pin_voter_lines_are_refused() {
        let _home = test_home();
        let root = tempfile::tempdir().expect("root");
        crate::with_env_mutation(|| {
            // SAFETY: this test holds the process environment lock.
            unsafe { std::env::set_var("VISSUE_ROOT", root.path()) };
        });
        let entry = sign_as(
            ballot_entry("alpha/run-101", "accept", "2026-10-21T10:00:00Z"),
            1,
        );
        let line = format!(
            "alpha/run-101 {}\nalpha/run-101 {}\n",
            entry.signature.key, entry.signature.key
        );
        std::fs::write(root.path().join(crate::ballot_sig::PIN_FILE), line).expect("pin");
        let err = check_entries(std::slice::from_ref(&entry)).expect_err("duplicate pin voter");
        assert!(err_chain(&err).contains("duplicate"), "{}", err_chain(&err));
    }

    #[test]
    fn a_seat_voter_must_match_the_tracker_seat_pin() {
        let _home = test_home();
        let root = tempfile::tempdir().expect("root");
        crate::with_env_mutation(|| {
            // SAFETY: this test holds the process environment lock.
            unsafe { std::env::set_var("VISSUE_ROOT", root.path()) };
        });
        let pinned = sign_as(
            ballot_entry("beta/seat-b", "accept", "2026-10-21T10:00:00Z"),
            1,
        );
        let entry = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            2,
        );
        let line = format!("seat-a {}\n", pinned.signature.key);
        std::fs::write(root.path().join(crate::ballot_sig::PIN_FILE), line).expect("pin");
        let err = check_entries(std::slice::from_ref(&entry)).expect_err("seat pin mismatch");
        assert!(err_chain(&err).contains("seat-a"), "{}", err_chain(&err));
    }

    #[test]
    fn a_voter_pin_row_cannot_give_a_seat_a_second_key() {
        let _home = test_home();
        let root = tempfile::tempdir().expect("root");
        crate::with_env_mutation(|| {
            // SAFETY: this test holds the process environment lock.
            unsafe { std::env::set_var("VISSUE_ROOT", root.path()) };
        });
        let at = "2026-10-21T10:00:00Z";
        let owner = sign_as(ballot_entry("alpha/inky", "accept", at), 1);
        let mallory = sign_as(ballot_entry("mallory/inky", "reject", at), 9);
        let (k1, k9) = (&owner.signature.key, &mallory.signature.key);
        let pin_path = root.path().join(crate::ballot_sig::PIN_FILE);
        for rows in [
            format!("inky {k1}\nmallory/inky {k9}\n"),
            format!("alpha/inky {k1}\nmallory/inky {k9}\n"),
        ] {
            std::fs::write(&pin_path, &rows).expect("pin");
            let Err(err) = check_entries(std::slice::from_ref(&mallory)) else {
                panic!("a second key voted under a pinned seat: {rows}");
            };
            assert!(
                err_chain(&err).contains("seat inky is pinned to two keys"),
                "{}",
                err_chain(&err)
            );
        }
    }

    #[test]
    fn a_seat_pin_accepts_same_key_in_capital_hex() {
        let _home = test_home();
        let root = tempfile::tempdir().expect("root");
        crate::with_env_mutation(|| {
            // SAFETY: this test holds the process environment lock.
            unsafe { std::env::set_var("VISSUE_ROOT", root.path()) };
        });
        let pinned = sign_as(
            ballot_entry("beta/seat-b", "accept", "2026-10-21T10:00:00Z"),
            1,
        );
        let mut entry = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T11:00:00Z"),
            1,
        );
        let pin_path = root.path().join(crate::ballot_sig::PIN_FILE);
        std::fs::write(&pin_path, format!("seat-a {}\n", pinned.signature.key)).expect("pin");
        entry.signature.key = entry.signature.key.to_ascii_uppercase();
        check_entries(std::slice::from_ref(&entry))
            .unwrap_or_else(|err| panic!("seat pin should match key hex case: {err:#}"));
    }

    #[test]
    fn a_pin_voter_that_is_not_canonical_is_refused() {
        let _home = test_home();
        let root = tempfile::tempdir().expect("root");
        crate::with_env_mutation(|| {
            // SAFETY: this test holds the process environment lock.
            unsafe { std::env::set_var("VISSUE_ROOT", root.path()) };
        });
        let pinned = sign_as(
            ballot_entry("beta/seat-b", "accept", "2026-10-21T10:00:00Z"),
            1,
        );
        let entry = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            2,
        );
        let key = &pinned.signature.key;
        let pin_path = root.path().join(crate::ballot_sig::PIN_FILE);
        for voter in [
            "\u{feff}seat-a",
            "\u{200b}seat-a",
            "Seat-a",
            "seat_a",
            "Alpha/run-101",
        ] {
            std::fs::write(&pin_path, format!("{voter} {key}\n")).expect("pin");
            let Err(err) = check_entries(std::slice::from_ref(&entry)) else {
                panic!("the {voter:?} pin row was skipped");
            };
            assert!(
                err_chain(&err).contains("pin voter"),
                "{voter:?}: {}",
                err_chain(&err)
            );
        }
    }

    #[test]
    fn holder_judge_and_legacy_pin_rows_hold_their_seat() {
        let _home = test_home();
        let root = tempfile::tempdir().expect("root");
        crate::with_env_mutation(|| {
            // SAFETY: this test holds the process environment lock.
            unsafe { std::env::set_var("VISSUE_ROOT", root.path()) };
        });
        let at = "2026-10-21T10:00:00Z";
        let key = sign_as(ballot_entry("beta/seat-b", "accept", at), 1)
            .signature
            .key;
        let rows: String = [
            "inky@39u",
            "disk-saver-t4t",
            "ljos-bot",
            "judge:model-1.5",
            "seat-b",
            "alpha/run-101",
        ]
        .iter()
        .map(|row| format!("{row} {key}\n"))
        .collect();
        std::fs::write(root.path().join(crate::ballot_sig::PIN_FILE), rows).expect("pin");
        for (voter, seat) in [
            ("alpha/inky", "inky"),
            ("alpha/disk-saver", "disk-saver"),
            ("alpha/ljosbot", "ljosbot"),
            ("alpha/judge-model-1-5", "judge-model-1-5"),
        ] {
            let other = sign_as(ballot_entry(voter, "accept", at), 2);
            let err = check_entries(std::slice::from_ref(&other))
                .expect_err("a second key voted under a pinned seat's alias");
            assert!(
                err_chain(&err).contains(&format!("{voter} is pinned through seat {seat}")),
                "{voter}: {}",
                err_chain(&err)
            );
            let held = sign_as(ballot_entry(voter, "accept", at), 1);
            check_entries(std::slice::from_ref(&held))
                .unwrap_or_else(|err| panic!("{voter} under its pinned key: {err:#}"));
        }
        let free = sign_as(ballot_entry("alpha/seat-a", "accept", at), 2);
        check_entries(std::slice::from_ref(&free)).expect("no row folds to seat-a");
    }

    #[test]
    fn a_seat_pin_does_not_hold_a_run_or_judge_of_that_name() {
        let _home = test_home();
        let root = tempfile::tempdir().expect("root");
        crate::with_env_mutation(|| {
            // SAFETY: this test holds the process environment lock.
            unsafe { std::env::set_var("VISSUE_ROOT", root.path()) };
        });
        let pinned = sign_as(
            ballot_entry("beta/seat-b", "accept", "2026-10-21T10:00:00Z"),
            1,
        );
        std::fs::write(
            root.path().join(crate::ballot_sig::PIN_FILE),
            format!("inky {}\n", pinned.signature.key),
        )
        .expect("pin");
        for voter in ["alpha/run-inky", "alpha/judge-inky"] {
            let other = sign_as(ballot_entry(voter, "accept", "2026-10-21T11:00:00Z"), 2);
            check_entries(std::slice::from_ref(&other))
                .unwrap_or_else(|err| panic!("{voter} was held to seat inky: {err:#}"));
        }
    }

    #[test]
    fn a_seat_pinned_to_two_keys_under_two_names_is_refused() {
        let _home = test_home();
        let root = tempfile::tempdir().expect("root");
        crate::with_env_mutation(|| {
            // SAFETY: this test holds the process environment lock.
            unsafe { std::env::set_var("VISSUE_ROOT", root.path()) };
        });
        let pin_path = root.path().join(crate::ballot_sig::PIN_FILE);
        let at = "2026-10-21T10:00:00Z";
        let bare = sign_as(ballot_entry("alpha/inky", "accept", at), 1);
        let tagged = sign_as(ballot_entry("beta/inky", "accept", at), 2);
        let (key1, key2) = (&bare.signature.key, &tagged.signature.key);
        std::fs::write(&pin_path, format!("inky {key1}\ninky@39u {key2}\n")).expect("pin");
        let entries = [bare.clone(), tagged.clone()];
        let Err(err) = check_entries(&entries) else {
            panic!("one seat voted under two keys: {:?}", tally(&entries));
        };
        let text = err_chain(&err);
        assert!(text.contains("seat inky is pinned to two keys"), "{text}");
        let read = tally(&entries).expect("one seat under two keys");
        assert!(read.is_empty(), "{read:?}");
        let later = sign_as(
            ballot_entry("beta/inky", "accept", "2026-10-21T11:00:00Z"),
            2,
        );
        let read = tally(&[later, bare.clone()]).expect("tally");
        assert!(read.is_empty(), "{read:?}");
        std::fs::write(&pin_path, format!("inky {key1}\ninky@39u {key1}\n")).expect("pin");
        check_entries(std::slice::from_ref(&bare)).expect("one key under both names");
        let err = check_entries(std::slice::from_ref(&tagged)).expect_err("an unpinned key");
        let text = err_chain(&err);
        assert!(
            text.contains("(inky)") && text.contains("(inky@39u)"),
            "{text}"
        );
    }

    #[test]
    fn a_run_seat_with_a_pin_row_is_held_to_it() {
        let _home = test_home();
        let root = tempfile::tempdir().expect("root");
        crate::with_env_mutation(|| {
            // SAFETY: this test holds the process environment lock.
            unsafe { std::env::set_var("VISSUE_ROOT", root.path()) };
        });
        let pinned = sign_as(
            ballot_entry("beta/seat-b", "accept", "2026-10-21T10:00:00Z"),
            1,
        );
        let entry = sign_as(
            ballot_entry("alpha/run-101", "accept", "2026-10-21T10:00:00Z"),
            2,
        );
        let line = format!("run-101 {}\n", pinned.signature.key);
        std::fs::write(root.path().join(crate::ballot_sig::PIN_FILE), line).expect("pin");
        let err = check_entries(std::slice::from_ref(&entry))
            .expect_err("alpha/run-101 passed beside a run-101 pin row");
        assert!(
            err_chain(&err).contains("through seat run-101"),
            "{}",
            err_chain(&err)
        );
    }

    #[test]
    fn a_voter_seat_part_must_match_its_slug() {
        let _home = test_home();
        let root = tempfile::tempdir().expect("root");
        crate::with_env_mutation(|| {
            // SAFETY: this test holds the process environment lock.
            unsafe { std::env::set_var("VISSUE_ROOT", root.path()) };
        });
        let pinned = sign_as(
            ballot_entry("alpha/seat-a", "accept", "2026-10-21T10:00:00Z"),
            1,
        );
        let line = format!("seat-a {}\n", pinned.signature.key);
        std::fs::write(root.path().join(crate::ballot_sig::PIN_FILE), line).expect("pin");
        let other = sign_unchecked(
            ballot_entry("alpha/Seat-a", "accept", "2026-10-21T11:00:00Z"),
            1,
        );
        let entries = [pinned, other];
        match check_entries(&entries) {
            Ok(_) => panic!(
                "alpha/Seat-a passed beside alpha/seat-a: {:?}",
                tally(&entries)
            ),
            Err(err) => {
                let text = err_chain(&err);
                assert!(text.contains("must be written `alpha/seat-a`"), "{text}");
            }
        }
    }

    fn check_value(value: &serde_json::Value) -> anyhow::Result<CheckReport> {
        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::write(
            dir.path().join("entry.json"),
            serde_json::to_vec(value).expect("json"),
        )
        .expect("write");
        check_dir(dir.path())
    }

    fn check_raw(raw: &str) -> anyhow::Result<CheckReport> {
        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::write(dir.path().join("entry.json"), raw).expect("write");
        check_dir(dir.path())
    }
}