zenkey-model 0.23.0

zk2 keys and contracts without a Zenoh session: the key grammar, the contract authoring format, canonical form, fingerprints and bundles
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
//! `redundancy`: one instance serves, the others stand by. `redundancy.v1`
//! (`spec/profiles/redundancy/v1.md`, #841), and `redundancy.v2` (`v2.md`,
//! #987's N1), which keeps v1's election whole and replaces its stamps:
//! no session HLC, and a follower that fences by the incumbent's runs.
//!
//! The profile's session-free half: the declaration read from a contract
//! (§2.2), how instance ids compare (§2.3), the descriptor's standby mark
//! read and checked (§2.4, §2.14, core §3.3), the election's decision over
//! one read (§2.5, §2.11), the heal's over two (§2.9), one candidate's
//! election as a pure state machine, two-phase like
//! [`crate::config::Owner`] (§2.5 to §2.11), and §5's four questions as
//! judgements that never fold an unestablished answer into no (§2.15).
//! Both majors share all of it: v2 restates §2.3 to §2.9 and §2.11 to §2.15
//! by reference. What v2 replaces is beside it: its declaration
//! ([`v2::declaration`]), a follower's read of a descriptor
//! ([`read_descriptor`]), the fence over either major ([`Fence`]), the draw
//! ([`draw_again`]), and an address's governance from its candidates'
//! revisions of either major ([`governance`]).
//!
//! | Rule | Item |
//! |---|---|
//! | §2.2 the declaration: governed, the election, invalid | [`declaration`], [`declaration_of`], [`Declaration`], [`Invalid`] |
//! | v2 §2.2 the declaration of either major, `majors`, `explicit_stream` | [`Major`], [`is_governed`], [`v2::declaration`], [`v2::declaration_of`], [`either_declaration_of`] |
//! | v2 §2.2 a linter's warnings, either major | [`lint_contract`], [`Warning`] |
//! | v2 §2.2, §2.15 an address's governance from its revisions | [`governance`], [`Revision`] |
//! | v2 §2.10, §2.14 a follower's read of a descriptor: its run and the mark | [`read_descriptor`], [`DescriptorRead`] |
//! | v2 §2.10 the fence by run, v1 §2.10's by zid, the hold for one read | [`Fence`], [`Holding`], [`Sample`], [`Fenced`], [`Discard`], [`Outcome`] |
//! | observer §3.6 (0.38): a read never undoes a token heard since it was asked | [`Fence::ask`], [`Asked`], carried by [`Holding::Read`] |
//! | v2 §2.10 a run no other states: the draw | [`draw_again`] |
//! | §2.3 the claim set, the incumbent, ids compared | [`Read`], [`Seen`], [`Listing`], [`Read::holders`], [`lower`] |
//! | §2.4, §2.14 the standby mark and its checks | [`Mark`], [`mark`], [`read_mark`], [`MarkFinding`] |
//! | §2.5 the election under `claim` | [`elect`], [`Decision`], [`Why`] |
//! | §2.6, §2.8 taking over, a vacancy | [`Elector`], [`Step::TakeOver`], [`Event::Withdrawn`] |
//! | §2.7 following the address, what is heard while a read runs (0.2) | [`Event::Appeared`], [`Event::Withdrawn`] (the runtime's watch), kept by the [`Elector`] |
//! | §2.7 a poll's reads compared with the last's (0.3) | [`poll`], [`Poll`] |
//! | §2.9 healing a split, a heal read not complete (0.2) | [`heal`], [`Heal`], [`Step::StepDown`], [`Step::Retry`] |
//! | §2.9 a candidate's own re-mint (0.2) | [`Event::Reminted`], [`Elector::is_own`] |
//! | §2.11 `election = "external"` | [`Election::External`], [`Event::Lock`] |
//! | §2.15, §5 what a tool concludes | [`one_incumbent`], [`vacancy`], [`standbys`], [`twin`] |
//!
//! ```text
//! declaration(uses, annotations, tokenless) ─▶ governed {claim│external} │ invalid [reasons] │ not governed   §2.2
//! read_mark(entry json, governance)        ─▶ standby │ serving │ malformed, with findings                  §2.4, §2.14
//! elect(own, read)                         ─▶ take over │ stand by {incumbent, incomplete, own claim missing,
//!                                               descriptor unread, lower candidate}                      §2.5
//! heal(own, election, first, second)       ─▶ step down {lower holder} │ stay {external, not holding,
//!                                               no split, lowest}                                        §2.9
//! Elector::start / observe(event) / finish ─▶ claim │ read │ grace │ retry │ stand by │ take over │ step down │ nothing
//! one_incumbent / vacancy / standbys / twin ─▶ §5's questions, in five states                            §2.15
//! ```
//!
//! There is no I/O and no clock here. A candidate's runtime declares what
//! [`Step::Claim`] says, makes each read [`Step::Read`] asks for and hands
//! it back as [`Event::Read`], arms the retry timer when a
//! [`Step::StandBy`] says `retry` or on a [`Step::Retry`], and the grace
//! timer on [`Step::Grace`], hands each to [`Elector::observe`] as
//! [`Event::Timer`], tells it what its watch hears ([`Event::Appeared`],
//! [`Event::Withdrawn`]), its own re-mint ([`Event::Reminted`]) and, under
//! `external`, its lock ([`Event::Lock`]); after a [`Step::Claim`], a
//! [`Step::TakeOver`] or a [`Step::StepDown`] it acts, in core §8.2's order
//! or its reverse, then calls [`Elector::finish`].

use std::collections::BTreeSet;
use std::fmt;

use serde_json::Value;

use crate::contract::Contract;
use crate::descriptor::InterfaceEntry;
use crate::grammar::{IfaceId, InstanceId, KindToken};
use crate::wire::Run;

mod fence;
pub mod v2;

pub(crate) use fence::Heard;
pub use fence::{Asked, Described, Discard, Fence, Fenced, Holding, Member, Outcome, Sample};

/// `redundancy.v1`'s id, as a contract lists it in `uses`.
pub const PROFILE: &str = "redundancy.v1";

/// The profile's name, as an annotation key's prefix carries it.
pub const NAME: &str = "redundancy";

/// `redundancy.election` (§4).
pub const ELECTION: &str = "redundancy.election";

/// The published vocabulary (§4): the keys under `redundancy.`, which W105
/// reads in place of core Appendix D's interim row (core 0.39), the same
/// key.
pub const VOCABULARY: &[&str] = &["election"];

/// A major of the profile (v2 §2.17). The election is one text, which both
/// share; each candidate stamps, answers a GET and is followed as its own
/// revision's major says.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Major {
    /// `redundancy.v1`: the candidate's session stamps every write, and a
    /// follower reads the stamp's id against `meta.zid` (v1 §2.10).
    V1,
    /// `redundancy.v2`: no session stamp, and a follower reads the run its
    /// descriptors state (v2 §2.10).
    V2,
}

impl Major {
    /// Both, in order.
    pub const ALL: [Major; 2] = [Major::V1, Major::V2];

    /// The major's id, as a contract lists it in `uses`.
    #[must_use]
    pub const fn profile(self) -> &'static str {
        match self {
            Major::V1 => PROFILE,
            Major::V2 => v2::PROFILE,
        }
    }

    /// The major's number.
    #[must_use]
    pub const fn number(self) -> u32 {
        match self {
            Major::V1 => 1,
            Major::V2 => 2,
        }
    }

    /// The major numbered `n`, `None` for any this crate does not read.
    #[must_use]
    pub const fn from_number(n: u32) -> Option<Major> {
        match n {
            1 => Some(Major::V1),
            2 => Some(Major::V2),
            _ => None,
        }
    }

    /// The major a contract's `uses` is read by, as a reader of both reads
    /// it (v2 §2.2): `redundancy.v2` where it lists it, `redundancy.v1`
    /// where it lists that and not `redundancy.v2`, `None` where it lists
    /// neither. One that lists both is `redundancy.v2`'s, which reads it
    /// `majors`.
    #[must_use]
    pub fn of<'a>(uses: impl IntoIterator<Item = &'a IfaceId>) -> Option<Major> {
        let mut found = None;
        for u in uses {
            if u.name() != NAME {
                continue;
            }
            match Major::from_number(u.major()) {
                Some(Major::V2) => return Some(Major::V2),
                Some(Major::V1) => found = Some(Major::V1),
                None => {}
            }
        }
        found
    }
}

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

// --- §2.2: the declaration -----------------------------------------------------

/// How a governed interface's serving instance is chosen (§4).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Election {
    /// This profile's protocol (§2.5 to §2.9), the default.
    Claim,
    /// A lock outside the bus (§2.11).
    External,
}

impl Election {
    /// The vocabulary's value, and the fixture's token.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Claim => "claim",
            Self::External => "external",
        }
    }

    /// Reads an annotation's value: the string `claim` or `external`, and
    /// nothing else.
    #[must_use]
    pub fn parse(v: &Value) -> Option<Self> {
        match v.as_str() {
            Some("claim") => Some(Self::Claim),
            Some("external") => Some(Self::External),
            _ => None,
        }
    }
}

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

/// Why a declaration is invalid (§2.2), in the order checked. `Majors` and
/// `ExplicitStream` are `redundancy.v2`'s alone (v2 §2.2): the first before
/// v1's four, the second after them.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Invalid {
    /// `uses` lists `redundancy.v2` and another major of the profile (v2
    /// §2.2; core E002 to a linter). The interface is read as governed for
    /// the checks after it.
    Majors,
    /// `redundancy.election` carried, and `uses` names no major of the
    /// profile (core E020's).
    NotUsed,
    /// A value that is not the string `claim` or `external`.
    Value,
    /// Two resources resolve to two elections.
    Disagrees,
    /// The interface is governed, and the deployment's tokenless set names
    /// it (§2.14).
    Tokenless,
    /// The interface is governed by `redundancy.v2`, and declares a
    /// `stream` resource with `explicit = true`: its samples carry no
    /// envelope, so no run a follower can fence (v2 §2.2).
    ExplicitStream,
}

impl Invalid {
    /// The fixture's token.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Majors => "majors",
            Self::NotUsed => "not_used",
            Self::Value => "value",
            Self::Disagrees => "disagrees",
            Self::Tokenless => "tokenless",
            Self::ExplicitStream => "explicit_stream",
        }
    }

    /// What it says, for a linter's line.
    #[must_use]
    pub fn says(self) -> &'static str {
        match self {
            Self::Majors => "uses lists redundancy.v2 and another major of redundancy",
            Self::NotUsed => "redundancy.election is written, and uses does not list redundancy.v1",
            Self::Value => "redundancy.election is the string claim or external",
            Self::Disagrees => "every resource of a governed interface resolves to one election",
            Self::Tokenless => {
                "a governed interface is never tokenless: its incumbent is the holder of its token"
            }
            Self::ExplicitStream => {
                "an explicit stream's samples carry no envelope, so no run a follower can fence: \
                 a redundancy.v2 interface declares none"
            }
        }
    }
}

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

/// An interface's declaration (§2.2).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Declaration {
    /// `uses` lists no `redundancy.v1`: not this profile's.
    NotGoverned,
    /// The reasons, in the order checked.
    Invalid(Vec<Invalid>),
    /// Governed, with its election.
    Governed(Election),
}

impl Declaration {
    /// The fixture's token.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::NotGoverned => "not_governed",
            Self::Invalid(_) => "invalid",
            Self::Governed(_) => "governed",
        }
    }

    /// The election of a valid declaration.
    #[must_use]
    pub fn election(&self) -> Option<Election> {
        match self {
            Self::Governed(e) => Some(*e),
            _ => None,
        }
    }
}

/// Reads an interface's declaration as `redundancy.v1` alone reads it
/// (§2.2; v1's `declarations.json`) from its contract's `uses`, the
/// resolved annotations of each of its resources (core §9.3, in any
/// order), and whether the deployment's tokenless set names it. Each check
/// runs alone; a resource whose value is found wrong takes no part in
/// `disagrees`, and one carrying none reads as `claim`. A contract listing
/// `redundancy.v1` beside another major is v1's here; a reader of both
/// majors reads it `majors` ([`either_declaration_of`]).
#[must_use]
pub fn declaration<'a, U, A>(uses: U, annotations: A, tokenless: bool) -> Declaration
where
    U: IntoIterator<Item = &'a IfaceId>,
    A: IntoIterator<Item = &'a std::collections::BTreeMap<String, Value>>,
{
    let mut governed = false;
    let mut another_major = false;
    for u in uses {
        if u.name() == NAME {
            if u.major() == 1 {
                governed = true;
            } else {
                another_major = true;
            }
        }
    }
    if another_major && !governed {
        // Another major's text: its key is its own.
        return Declaration::NotGoverned;
    }
    let values: Vec<Option<&Value>> = annotations.into_iter().map(|a| a.get(ELECTION)).collect();
    let carried = values.iter().any(Option::is_some);
    if !governed && !carried {
        return Declaration::NotGoverned;
    }
    let mut why = Vec::new();
    if !governed {
        why.push(Invalid::NotUsed);
    }
    let election = elections(&values, &mut why);
    if governed && tokenless {
        why.push(Invalid::Tokenless);
    }
    if why.is_empty() {
        Declaration::Governed(election)
    } else {
        Declaration::Invalid(why)
    }
}

/// §2.2's `value` and `disagrees`, which both majors check alike, pushed
/// onto `why` in that order: a resource carrying none reads as `claim`, and
/// one found `value` takes no part in `disagrees`. Returns the one election
/// the valid resources resolve to, `claim` with none.
pub(crate) fn elections(values: &[Option<&Value>], why: &mut Vec<Invalid>) -> Election {
    let mut elections = BTreeSet::new();
    let mut wrong = false;
    for v in values {
        match v {
            None => {
                elections.insert(Election::Claim);
            }
            Some(v) => match Election::parse(v) {
                Some(e) => {
                    elections.insert(e);
                }
                None => wrong = true,
            },
        }
    }
    if wrong {
        why.push(Invalid::Value);
    }
    if elections.len() > 1 {
        why.push(Invalid::Disagrees);
    }
    elections.into_iter().next().unwrap_or(Election::Claim)
}

/// [`declaration`] of a contract, `tokenless` saying whether the
/// deployment's tokenless set names its interface: `redundancy.v1`'s
/// reading alone.
#[must_use]
pub fn declaration_of(c: &Contract, tokenless: bool) -> Declaration {
    declaration(
        &c.uses,
        c.resources.iter().map(|r| &r.annotations),
        tokenless,
    )
}

/// A contract's declaration as a reader of both majors reads it (v2 §2.2):
/// [`v2::declaration_of`] where its `uses` lists `redundancy.v2`, and
/// [`declaration_of`], `redundancy.v1`'s, otherwise. The major of a
/// governed one is [`Major::of`] its `uses`.
#[must_use]
pub fn either_declaration_of(c: &Contract, tokenless: bool) -> Declaration {
    if Major::of(&c.uses) == Some(Major::V2) {
        v2::declaration_of(c, tokenless)
    } else {
        declaration_of(c, tokenless)
    }
}

/// Whether a contract governs its interface: its `uses` lists
/// `redundancy.v1` or `redundancy.v2` (§2.2, v2 §2.2), governed by either
/// major. [`Major::of`] says which.
#[must_use]
pub fn is_governed(c: &Contract) -> bool {
    Major::of(&c.uses).is_some()
}

/// One finding a contract linter warns of (v1 §2.2, v2 §2.2): `value`,
/// `disagrees`, and under `redundancy.v2` `explicit_stream`. Each is an
/// invalid declaration, which a runtime refuses to start; the core has no
/// code for any, so a linter warns. `majors` and `not_used` are core E002
/// and E020, which refuse the contract's load, and `tokenless` is the
/// deployment's, never the contract's.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Warning {
    /// The major whose text the reason is under.
    pub major: Major,
    /// The resource it is of, as [`crate::words::Lint::place`] names one,
    /// or the interface id for `disagrees`.
    pub at: String,
    pub reason: Invalid,
}

impl fmt::Display for Warning {
    /// A linter's line: `warning[<profile> <token>] <at>: <says>`.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "warning[{} {}] {}: {}",
            self.major.profile(),
            self.reason.as_str(),
            self.at,
            self.reason.says()
        )
    }
}

/// A contract's declaration findings a linter warns of (v1 §2.2, v2 §2.2),
/// read by the major [`Major::of`] its `uses` names: each resource whose
/// `redundancy.election` is `value`, the interface once for `disagrees`,
/// and each explicit stream of a `redundancy.v2` one. None for a contract
/// governed by neither major.
#[must_use]
pub fn lint_contract(c: &Contract) -> Vec<Warning> {
    let Some(major) = Major::of(&c.uses) else {
        return Vec::new();
    };
    let Declaration::Invalid(why) = either_declaration_of(c, false) else {
        return Vec::new();
    };
    let warn = |at: String, reason| Warning { major, at, reason };
    let mut out = Vec::new();
    for reason in why {
        match reason {
            Invalid::Value => out.extend(
                c.resources
                    .iter()
                    .filter(|r| {
                        r.annotations
                            .get(ELECTION)
                            .is_some_and(|v| Election::parse(v).is_none())
                    })
                    .map(|r| warn(crate::words::Lint::place(r), reason)),
            ),
            Invalid::Disagrees => out.push(warn(c.iface.to_string(), reason)),
            Invalid::ExplicitStream => out.extend(
                c.resources
                    .iter()
                    .filter(|r| r.token == KindToken::ExplicitStream)
                    .map(|r| warn(crate::words::Lint::place(r), reason)),
            ),
            Invalid::Majors | Invalid::NotUsed | Invalid::Tokenless => {}
        }
    }
    out
}

// --- §2.3: ids, the claim set, the incumbent ----------------------------------------

/// Whether `a` is lower than `b` (§2.3): their 16 lowercase hexadecimal
/// digits compared character by character, which is their order as
/// unsigned 64-bit integers.
#[must_use]
pub fn lower(a: &InstanceId, b: &InstanceId) -> bool {
    a.as_str() < b.as_str()
}

/// What an instance's descriptor says of the governed interface, as a read
/// found it (§2.3).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Listing {
    /// Its descriptor was not read.
    Unread,
    /// It does not list the interface: no candidate.
    NotListed,
    /// It lists the interface, marked standby or not (core §3.3).
    Listed { standby: bool },
}

impl Listing {
    /// Whether the instance is a candidate (§2.3).
    #[must_use]
    pub fn lists(self) -> bool {
        matches!(self, Self::Listed { .. })
    }
}

/// One instance of the address, as a read found it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Seen {
    pub id: InstanceId,
    /// It holds the governed interface's token, at any revision.
    pub token: bool,
    pub descriptor: Listing,
}

/// One read of the address (§0): its instances, with their tokens and
/// descriptors, and whether it is complete (core §8.1).
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Read {
    pub complete: bool,
    pub instances: Vec<Seen>,
}

impl Read {
    /// The incumbents: the instances holding the interface's token,
    /// lowest first, each once (§2.3).
    #[must_use]
    pub fn holders(&self) -> Vec<&InstanceId> {
        let set: BTreeSet<&InstanceId> = self
            .instances
            .iter()
            .filter(|s| s.token)
            .map(|s| &s.id)
            .collect();
        set.into_iter().collect()
    }

    /// What the read found of `id`, if it found it.
    #[must_use]
    pub fn get(&self, id: &InstanceId) -> Option<&Seen> {
        self.instances.iter().find(|s| &s.id == id)
    }

    fn holders_owned(&self) -> Vec<InstanceId> {
        self.holders().into_iter().cloned().collect()
    }
}

// --- §2.4, §2.14: the standby mark ------------------------------------------------

/// What an interface entry says the instance does with the interface (core
/// §3.3, 0.39).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Mark {
    /// Unmarked: it serves the interface, or claims to.
    Serving,
    /// `"standby": true`: it exposes nothing of it and holds none of its
    /// tokens (core §6).
    Standby,
}

impl Mark {
    /// The fixture's token.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Serving => "serving",
            Self::Standby => "standby",
        }
    }
}

/// The mark of a parsed entry.
#[must_use]
pub fn mark(e: &InterfaceEntry) -> Mark {
    if e.standby {
        Mark::Standby
    } else {
        Mark::Serving
    }
}

/// What a reader knows of an interface's governance (§2.15 premise 3).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Governance {
    /// Governed, by contracts read and agreeing, with their election.
    Governed(Election),
    /// No candidate's contract uses the profile: not this profile's.
    NotGoverned,
    /// A candidate's contract was not read.
    NoContract,
    /// The candidates' revisions disagree on governance or the election
    /// (§2.2).
    Disagree,
}

impl Governance {
    /// The fixture's token.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Governed(e) => e.as_str(),
            Self::NotGoverned => "not_governed",
            Self::NoContract => "no_contract",
            Self::Disagree => "disagree",
        }
    }

    /// Reads a fixture's token.
    #[must_use]
    pub fn parse(s: &str) -> Option<Self> {
        match s {
            "claim" => Some(Self::Governed(Election::Claim)),
            "external" => Some(Self::Governed(Election::External)),
            "not_governed" => Some(Self::NotGoverned),
            "no_contract" => Some(Self::NoContract),
            "disagree" => Some(Self::Disagree),
            _ => None,
        }
    }
}

/// What a reader finds of an entry's mark (§2.14), in this order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MarkFinding {
    /// `standby` or `token` present and not a boolean, or the entry not an
    /// object: not the record's shape (core D000). Nothing else is read.
    Malformed,
    /// Marked `"standby": true` and `"token": false` (core D012).
    StandbyTokenless,
    /// Marked `"token": false`, the interface governed (§2.14).
    GovernedTokenless,
    /// Marked standby, the interface not governed: the mark says what core
    /// §6 says, and none of §5's questions is this profile's.
    NotGoverned,
}

impl MarkFinding {
    /// The fixture's token.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Malformed => "malformed",
            Self::StandbyTokenless => "standby_tokenless",
            Self::GovernedTokenless => "governed_tokenless",
            Self::NotGoverned => "not_governed",
        }
    }
}

/// An entry's mark, `None` when it is malformed, and the findings.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MarkRead {
    pub mark: Option<Mark>,
    pub findings: Vec<MarkFinding>,
}

/// Reads the mark of an interface entry as JSON (core §3.3, §2.14):
/// `standby` absent is serving, `token` absent is `true`.
#[must_use]
pub fn read_mark(entry: &Value, governance: Governance) -> MarkRead {
    let malformed = MarkRead {
        mark: None,
        findings: vec![MarkFinding::Malformed],
    };
    let Some(o) = entry.as_object() else {
        return malformed;
    };
    let flag = |name: &str, absent: bool| match o.get(name) {
        None => Some(absent),
        Some(Value::Bool(b)) => Some(*b),
        Some(_) => None,
    };
    let (Some(standby), Some(token)) = (flag("standby", false), flag("token", true)) else {
        return malformed;
    };
    let mut findings = Vec::new();
    if standby && !token {
        findings.push(MarkFinding::StandbyTokenless);
    }
    if matches!(governance, Governance::Governed(_)) && !token {
        findings.push(MarkFinding::GovernedTokenless);
    }
    if standby && governance == Governance::NotGoverned {
        findings.push(MarkFinding::NotGoverned);
    }
    MarkRead {
        mark: Some(if standby {
            Mark::Standby
        } else {
            Mark::Serving
        }),
        findings,
    }
}

/// What a follower reads of one descriptor (v2 §2.10, §2.14;
/// `v2/descriptors.json`): the instance's run, and the governed
/// interface's entry, its mark and the findings.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DescriptorRead {
    /// The run the descriptor states, `None` where it states none or
    /// spells it otherwise than 4 or 8 lowercase hexadecimal digits (core
    /// D000, D002): the follower then holds no run from it. Read whatever
    /// the governance and the listing.
    pub run: Option<Run>,
    /// Whether an entry of `interfaces` is the interface's. An entry that
    /// is not an object is read as the interface's, malformed.
    pub listed: bool,
    /// The entry's mark ([`read_mark`]), `None` unlisted or malformed.
    pub mark: Option<Mark>,
    /// [`read_mark`]'s findings, none unlisted.
    pub findings: Vec<MarkFinding>,
}

/// Reads a descriptor, as JSON, for the interface `iface` whose governance
/// the reader knows as `governance` (v2 §2.10, §2.14): its run, and the
/// first entry of `interfaces` that is `iface`'s or is not an object, read
/// by [`read_mark`]. `meta.zid` is not read: `redundancy.v2`'s fence reads
/// no zid.
#[must_use]
pub fn read_descriptor(descriptor: &Value, iface: &str, governance: Governance) -> DescriptorRead {
    let run = descriptor
        .get("run")
        .and_then(Value::as_str)
        .and_then(Run::from_hex);
    let entry = descriptor
        .get("interfaces")
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
        .find(|e| {
            e.as_object()
                .is_none_or(|o| o.get("iface").and_then(Value::as_str) == Some(iface))
        });
    match entry {
        Some(e) => {
            let m = read_mark(e, governance);
            DescriptorRead {
                run,
                listed: true,
                mark: m.mark,
                findings: m.findings,
            }
        }
        None => DescriptorRead {
            run,
            listed: false,
            mark: None,
            findings: Vec::new(),
        },
    }
}

/// Whether a candidate draws a new run before it serves (v2 §2.10, a
/// SHOULD): its own run, by width and value, is among the runs it read in
/// other instances' descriptors of its address. A new run is core §4.3's:
/// the candidate puts its descriptor again before it writes under it.
#[must_use]
pub fn draw_again(own: Run, read: impl IntoIterator<Item = Run>) -> bool {
    read.into_iter().any(|r| r == own)
}

/// What one candidate's revision of the interface declares, as a tool
/// reads it for an address's governance (v2 §2.2 "Across revisions",
/// §2.15; `v2/declarations.json`'s `addresses`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Revision {
    /// Governed by `major`, with its election.
    Governed(Major, Election),
    NotGoverned,
    /// Invalid: it gives no election.
    Invalid,
    /// Its contract not read.
    Unread,
}

impl Revision {
    /// The revision of `c`, as a reader of both majors reads it
    /// ([`either_declaration_of`], no tokenless set).
    #[must_use]
    pub fn of(c: &Contract) -> Revision {
        match (either_declaration_of(c, false), Major::of(&c.uses)) {
            (Declaration::Governed(e), Some(m)) => Revision::Governed(m, e),
            (Declaration::Governed(_) | Declaration::NotGoverned, _) => Revision::NotGoverned,
            (Declaration::Invalid(_), _) => Revision::Invalid,
        }
    }
}

/// The governance a tool reads of an address from the revisions its
/// candidates' descriptors name, one entry a revision (v2 §2.2, §2.15):
/// `no_contract` where none is given, or one is unread or invalid; else
/// `not_governed` where none is governed; else `disagree` where one is not
/// governed, or the governed ones give two elections; else their one
/// election. The major of a governed revision takes no part: a
/// `redundancy.v1` candidate and a `redundancy.v2` one with one election
/// are one election.
#[must_use]
pub fn governance(revisions: impl IntoIterator<Item = Revision>) -> Governance {
    let mut any = false;
    let mut elections = BTreeSet::new();
    let mut not_governed = false;
    for r in revisions {
        any = true;
        match r {
            Revision::Unread | Revision::Invalid => return Governance::NoContract,
            Revision::NotGoverned => not_governed = true,
            Revision::Governed(_, e) => {
                elections.insert(e);
            }
        }
    }
    if !any {
        return Governance::NoContract;
    }
    match (elections.len(), not_governed) {
        (0, _) => Governance::NotGoverned,
        (1, false) => Governance::Governed(*elections.iter().next().expect("one")),
        _ => Governance::Disagree,
    }
}

// --- §2.5, §2.11: the election over one read -----------------------------------------

/// Why an instance stands by (§2.5, §2.11).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Why {
    /// Another instance holds the interface's token: a live incumbent wins.
    Incumbent(InstanceId),
    /// The read was not complete.
    Incomplete,
    /// The read did not show the instance's own claim.
    OwnClaimMissing,
    /// A lower instance's descriptor was not read.
    DescriptorUnread(InstanceId),
    /// A lower candidate is in the claim set.
    LowerCandidate(InstanceId),
    /// `external`: the instance does not hold the lock.
    NoLock,
}

impl Why {
    /// The fixture's token.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Incumbent(_) => "incumbent",
            Self::Incomplete => "incomplete",
            Self::OwnClaimMissing => "own_claim_missing",
            Self::DescriptorUnread(_) => "descriptor_unread",
            Self::LowerCandidate(_) => "lower_candidate",
            Self::NoLock => "no_lock",
        }
    }

    /// The instance it names, if it names one.
    #[must_use]
    pub fn instance(&self) -> Option<&InstanceId> {
        match self {
            Self::Incumbent(i) | Self::DescriptorUnread(i) | Self::LowerCandidate(i) => Some(i),
            Self::Incomplete | Self::OwnClaimMissing | Self::NoLock => None,
        }
    }

    /// Whether the instance campaigns again after its retry interval
    /// (§2.5): when it saw no incumbent, whose withdrawal its watch would
    /// hear, and holds its lock where one elects.
    #[must_use]
    pub fn retries(&self) -> bool {
        !matches!(self, Self::Incumbent(_) | Self::NoLock)
    }
}

/// What one read decides (§2.5).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Decision {
    TakeOver,
    StandBy(Why),
}

impl Decision {
    /// The fixture's token.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::TakeOver => "take_over",
            Self::StandBy(_) => "stand_by",
        }
    }
}

/// §2.5 steps 2 and 3's first two conditions, shared by both elections:
/// an incumbent, then the read's completeness and the instance's own claim.
fn premises(own: &InstanceId, read: &Read) -> Option<Why> {
    if let Some(h) = read.holders().into_iter().find(|h| *h != own) {
        return Some(Why::Incumbent(h.clone()));
    }
    if !read.complete {
        return Some(Why::Incomplete);
    }
    if read.get(own).is_none() {
        return Some(Why::OwnClaimMissing);
    }
    None
}

/// What a candidate whose election is `claim` does with one read (§2.5,
/// steps 2 to 4): stand by for a live incumbent, whatever the read's
/// completeness; for a read not complete, or missing its own claim; for
/// the lowest instance below its own that lists the interface (a lower
/// candidate) or whose descriptor was not read; and otherwise take over.
#[must_use]
pub fn elect(own: &InstanceId, read: &Read) -> Decision {
    if let Some(why) = premises(own, read) {
        return Decision::StandBy(why);
    }
    let lowest = read
        .instances
        .iter()
        .filter(|s| lower(&s.id, own) && s.descriptor != Listing::NotListed)
        .min_by(|a, b| a.id.cmp(&b.id));
    match lowest {
        Some(s) if s.descriptor == Listing::Unread => {
            Decision::StandBy(Why::DescriptorUnread(s.id.clone()))
        }
        Some(s) => Decision::StandBy(Why::LowerCandidate(s.id.clone())),
        None => Decision::TakeOver,
    }
}

/// What a candidate whose election is `external`, holding its lock, does
/// with one read (§2.11): §2.5's step 2 and step 3's first two conditions,
/// never step 4, since the lock decided.
#[must_use]
pub fn elect_external(own: &InstanceId, read: &Read) -> Decision {
    match premises(own, read) {
        Some(why) => Decision::StandBy(why),
        None => Decision::TakeOver,
    }
}

// --- §2.9: the heal ------------------------------------------------------------------

/// Why an incumbent stays (§2.9).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Stay {
    /// `external`: never by this rule (§2.11).
    External,
    /// It holds no token in the second read.
    NotHolding,
    /// Fewer than two holders in one of the reads: no split.
    NoSplit,
    /// It is the lowest holder of the second read.
    Lowest,
}

impl Stay {
    /// The fixture's token.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::External => "external",
            Self::NotHolding => "not_holding",
            Self::NoSplit => "no_split",
            Self::Lowest => "lowest",
        }
    }
}

/// What two reads a grace apart decide for an incumbent (§2.9).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Heal {
    /// Step down: the second read shows this lower holder.
    StepDown(InstanceId),
    Stay(Stay),
}

/// Decides a heal for `own` from the holders of two reads a grace apart,
/// `first` then `second` (§2.9): under `claim`, an incumbent of a split,
/// two or more holders in both reads, steps down when the second read
/// shows a holder lower than itself. A read's completeness plays no part: a
/// read cannot invent a holder.
#[must_use]
pub fn heal(
    own: &InstanceId,
    election: Election,
    first: &[InstanceId],
    second: &[InstanceId],
) -> Heal {
    if election == Election::External {
        return Heal::Stay(Stay::External);
    }
    let first: BTreeSet<&InstanceId> = first.iter().collect();
    let second: BTreeSet<&InstanceId> = second.iter().collect();
    if !second.contains(own) {
        return Heal::Stay(Stay::NotHolding);
    }
    if first.len() < 2 || second.len() < 2 {
        return Heal::Stay(Stay::NoSplit);
    }
    match second.into_iter().next() {
        Some(low) if lower(low, own) => Heal::StepDown(low.clone()),
        _ => Heal::Stay(Stay::Lowest),
    }
}

// --- §2.7: a poll ----------------------------------------------------------------------

/// What one read of a poll found, compared with the last's (§2.7; text
/// 0.3, F-215).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Poll {
    /// The holders it shows that the last's did not: appearances.
    pub appeared: Vec<InstanceId>,
    /// The last's holders it shows gone, a complete read only:
    /// withdrawals.
    pub withdrawn: Vec<InstanceId>,
    /// What the next read is compared with: a complete read's holders, or,
    /// from a read not complete, the last's with the new ones added, since
    /// it removes none.
    pub next: BTreeSet<InstanceId>,
}

/// One read of a poll compared with `last`, the holders the read before it
/// left (§2.7; text 0.3, F-215): a holder it shows that is new is an
/// appearance, and only a complete read can show one gone, so a read not
/// complete removes no holder from the last's. The first read is compared
/// with no holder.
#[must_use]
pub fn poll(last: &BTreeSet<InstanceId>, complete: bool, holders: &BTreeSet<InstanceId>) -> Poll {
    let appeared = holders.difference(last).cloned().collect();
    if complete {
        Poll {
            appeared,
            withdrawn: last.difference(holders).cloned().collect(),
            next: holders.clone(),
        }
    } else {
        Poll {
            appeared,
            withdrawn: Vec::new(),
            next: last.union(holders).cloned().collect(),
        }
    }
}

// --- §2.5 to §2.11: one candidate's election -----------------------------------------

/// What the runtime tells an [`Elector`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event {
    /// The read a [`Step::Read`] asked for (§2.5, §2.8, §2.9).
    Read(Read),
    /// The watch heard an interface token of the interface appear at the
    /// address, held by this instance (§2.7, §2.9).
    Appeared(InstanceId),
    /// The watch heard the address's interface tokens withdrawn, leaving
    /// none held: one event for a burst (§2.8).
    Withdrawn,
    /// The timer the last step armed fired: the retry interval, or the
    /// grace.
    Timer,
    /// `external`: the deployment's lock taken (`true`) or lost (§2.11).
    Lock(bool),
    /// The candidate's own re-mint (core §1.5, §8.1; text 0.2, F-209): the
    /// new id is its own from then on, and every former id is its own,
    /// never another holder or another candidate. The runtime tells it
    /// before the new id's token is declared, in every state.
    Reminted(InstanceId),
}

/// Why an incumbent steps down (§2.9, §2.11).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum StepDownWhy {
    /// Under `claim`, a split whose second read shows this lower holder.
    LowerHolder(InstanceId),
    /// Under `external`, the lock lost.
    LockLost,
}

impl StepDownWhy {
    /// The fixture's token.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::LowerHolder(_) => "lower_holder",
            Self::LockLost => "lock_lost",
        }
    }
}

/// What an [`Elector`] asks its runtime to do.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Step {
    /// Put the descriptor, the interface marked standby, declare the
    /// instance token and the watch with its round trip (§2.5 step 1,
    /// §2.7), then call [`Elector::finish`].
    Claim,
    /// Make one read of the address (§0), then hand it back as
    /// [`Event::Read`].
    Read,
    /// Arm the grace timer, then hand it back as [`Event::Timer`] (§2.9).
    Grace,
    /// Stand by (§2.4), following the address; arm the retry timer when
    /// `retry` (§2.5).
    StandBy { why: Why, retry: bool },
    /// Serving, arm the retry timer, then hand it back as [`Event::Timer`]:
    /// a heal's read not complete showing fewer than two holders, which
    /// ends nothing (§2.9; text 0.2, F-199).
    Retry,
    /// Take over in core §8.2's order (§2.6), then call
    /// [`Elector::finish`].
    TakeOver,
    /// Step down in the reverse order (§2.9), then call
    /// [`Elector::finish`].
    StepDown(StepDownWhy),
    /// Nothing to do.
    Nothing,
}

impl Step {
    /// The fixture's token.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Claim => "claim",
            Self::Read => "read",
            Self::Grace => "grace",
            Self::StandBy { .. } => "stand_by",
            Self::Retry => "retry",
            Self::TakeOver => "take_over",
            Self::StepDown(_) => "step_down",
            Self::Nothing => "nothing",
        }
    }
}

/// What the instance's descriptor says of the interface now.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Role {
    /// Not claimed yet.
    Unclaimed,
    /// Marked standby: campaigning, standing by, or taking over.
    Standby,
    /// Serving: an incumbent, healing, or stepping down.
    Serving,
}

impl Role {
    /// The fixture's token.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Unclaimed => "unclaimed",
            Self::Standby => "standby",
            Self::Serving => "serving",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum Phase {
    New,
    Claiming,
    Campaigning,
    Standby {
        retry: bool,
    },
    TakingOver,
    Serving,
    HealFirst,
    HealGrace(Vec<InstanceId>),
    HealSecond(Vec<InstanceId>),
    /// A heal read not complete showing fewer than two holders: read again
    /// after the retry interval, the second read again when the first is
    /// held (F-199).
    HealRetry(Option<Vec<InstanceId>>),
    SteppingDown(StepDownWhy),
}

/// What the watch heard while a read was asked or an act ran, kept to be
/// decided once it ends (text 0.2, F-199).
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
struct Kept {
    appeared: bool,
    withdrawn: bool,
}

/// One candidate's election on one governed interface (§2.5 to §2.11): a
/// pure state machine, two-phase like [`crate::config::Owner`]. A restart
/// is a new `Elector`, and a new instance id; a re-mint in the same run is
/// [`Event::Reminted`].
///
/// **What is heard meanwhile is kept** (text 0.2, F-199). An appearance of
/// another holder's token, or a withdrawal, heard while a read is asked
/// (campaigning or healing) or while an act runs (taking over or stepping
/// down) is kept, and decided once the read's decision or the act's finish
/// is made, as if heard then, in the state it leaves: an appearance kept
/// through a takeover reads for a heal once it serves, a withdrawal kept
/// past a stand-by on an incumbent reads again, one kept through a
/// step-down campaigns once it stands by (under `external`, only with the
/// lock, F-207). A timer or a read nothing waits for is ignored.
#[derive(Debug, Clone)]
pub struct Elector {
    own: InstanceId,
    /// Its own former ids, after a re-mint (F-209).
    former: BTreeSet<InstanceId>,
    election: Election,
    phase: Phase,
    lock: bool,
    kept: Kept,
}

impl Elector {
    /// An instance `own` of an interface whose election is `election`,
    /// before its claim. Under `external`, it holds no lock until told.
    #[must_use]
    pub fn new(own: InstanceId, election: Election) -> Self {
        Self {
            own,
            former: BTreeSet::new(),
            election,
            phase: Phase::New,
            lock: false,
            kept: Kept::default(),
        }
    }

    /// This instance's id: the last one it was told of (F-209).
    #[must_use]
    pub fn own(&self) -> &InstanceId {
        &self.own
    }

    /// Whether `id` is this instance's: its id, or one it held before a
    /// re-mint (F-209).
    #[must_use]
    pub fn is_own(&self, id: &InstanceId) -> bool {
        *id == self.own || self.former.contains(id)
    }

    /// The interface's election.
    #[must_use]
    pub fn election(&self) -> Election {
        self.election
    }

    /// What its descriptor says now.
    #[must_use]
    pub fn role(&self) -> Role {
        match self.phase {
            Phase::New | Phase::Claiming => Role::Unclaimed,
            Phase::Campaigning | Phase::Standby { .. } | Phase::TakingOver => Role::Standby,
            Phase::Serving
            | Phase::HealFirst
            | Phase::HealGrace(_)
            | Phase::HealSecond(_)
            | Phase::HealRetry(_)
            | Phase::SteppingDown(_) => Role::Serving,
        }
    }

    /// Whether it serves the interface: an incumbent, which its role says.
    #[must_use]
    pub fn serving(&self) -> bool {
        self.role() == Role::Serving
    }

    /// Whether it holds the lock, as last told (`external`).
    #[must_use]
    pub fn lock(&self) -> bool {
        self.lock
    }

    /// Starts: claim before reading (§2.5 step 1). Called once.
    pub fn start(&mut self) -> Step {
        if self.phase != Phase::New {
            return Step::Nothing;
        }
        self.phase = Phase::Claiming;
        Step::Claim
    }

    /// The act a [`Step::Claim`], a [`Step::TakeOver`] or a
    /// [`Step::StepDown`] asked for is done: the next step.
    pub fn finish(&mut self) -> Step {
        let step = match std::mem::replace(&mut self.phase, Phase::New) {
            Phase::Claiming => {
                // Nothing is kept before the claim's end: the first read
                // asks the bus whole.
                self.kept = Kept::default();
                return self.campaign();
            }
            Phase::TakingOver => {
                self.phase = Phase::Serving;
                match self.election {
                    Election::External if !self.lock => {
                        self.kept = Kept::default();
                        return self.step_down(StepDownWhy::LockLost);
                    }
                    _ => Step::Nothing,
                }
            }
            Phase::SteppingDown(_) => {
                self.phase = Phase::Standby { retry: false };
                Step::Nothing
            }
            other => {
                self.phase = other;
                return Step::Nothing;
            }
        };
        self.decide_kept(step)
    }

    /// Hands it an event: the next step.
    pub fn observe(&mut self, event: Event) -> Step {
        match event {
            Event::Read(r) => {
                let r = self.normalized(&r);
                self.on_read(&r)
            }
            Event::Appeared(id) => {
                if self.is_own(&id) {
                    return Step::Nothing;
                }
                self.on_appeared()
            }
            Event::Withdrawn => self.on_withdrawn(),
            Event::Timer => self.on_timer(),
            Event::Lock(held) => self.on_lock(held),
            Event::Reminted(id) => {
                // F-209: no appearance, no step; the new id is its own from
                // here on, the old one too.
                if id != self.own {
                    let old = std::mem::replace(&mut self.own, id);
                    self.former.insert(old);
                    // A re-mint back to a former id: that id is its own
                    // again, never a former one beside itself.
                    self.former.remove(&self.own);
                }
                Step::Nothing
            }
        }
    }

    /// A read with every former id of its own read as its own (F-209): one
    /// entry, holding the token when any of them does.
    fn normalized(&self, r: &Read) -> Read {
        if self.former.is_empty() {
            return r.clone();
        }
        let mut instances = Vec::with_capacity(r.instances.len());
        let mut own: Option<Seen> = None;
        for s in &r.instances {
            if !self.is_own(&s.id) {
                instances.push(s.clone());
                continue;
            }
            match &mut own {
                None => {
                    own = Some(Seen {
                        id: self.own.clone(),
                        token: s.token,
                        descriptor: s.descriptor,
                    });
                }
                Some(o) => {
                    o.token |= s.token;
                    if s.id == self.own {
                        o.descriptor = s.descriptor;
                    }
                }
            }
        }
        instances.extend(own);
        Read {
            complete: r.complete,
            instances,
        }
    }

    /// Decides what was kept, in the state `step` leaves (F-199): the
    /// first kept event that asks something replaces a step that is no act
    /// (a stand-by, nothing); an act keeps what its own phase keeps.
    fn decide_kept(&mut self, step: Step) -> Step {
        fn replace(step: &mut Step, next: Step) {
            if next != Step::Nothing && matches!(step, Step::Nothing | Step::StandBy { .. }) {
                *step = next;
            }
        }
        let kept = std::mem::take(&mut self.kept);
        let mut step = step;
        if kept.appeared {
            let next = self.on_appeared();
            replace(&mut step, next);
        }
        if kept.withdrawn {
            let next = self.on_withdrawn();
            replace(&mut step, next);
        }
        step
    }

    /// A campaign: a read, but under `external` without the lock.
    fn campaign(&mut self) -> Step {
        if self.election == Election::External && !self.lock {
            return self.stand_by(Why::NoLock);
        }
        self.phase = Phase::Campaigning;
        Step::Read
    }

    fn stand_by(&mut self, why: Why) -> Step {
        let retry = why.retries();
        self.phase = Phase::Standby { retry };
        Step::StandBy { why, retry }
    }

    fn step_down(&mut self, why: StepDownWhy) -> Step {
        self.phase = Phase::SteppingDown(why.clone());
        Step::StepDown(why)
    }

    /// A heal read's holders, its own counted: it knows it holds.
    fn holding(&self, r: &Read) -> Vec<InstanceId> {
        let mut h = r.holders_owned();
        if !h.contains(&self.own) {
            h.push(self.own.clone());
            h.sort();
        }
        h
    }

    fn on_read(&mut self, r: &Read) -> Step {
        let step = match self.phase.clone() {
            Phase::Campaigning => {
                let decision = match self.election {
                    Election::Claim => elect(&self.own, r),
                    Election::External if !self.lock => Decision::StandBy(Why::NoLock),
                    Election::External => elect_external(&self.own, r),
                };
                match decision {
                    Decision::TakeOver => {
                        self.phase = Phase::TakingOver;
                        Step::TakeOver
                    }
                    Decision::StandBy(why) => self.stand_by(why),
                }
            }
            Phase::HealFirst => {
                let holders = self.holding(r);
                if holders.len() >= 2 {
                    self.phase = Phase::HealGrace(holders);
                    Step::Grace
                } else if r.complete {
                    self.phase = Phase::Serving;
                    Step::Nothing
                } else {
                    // F-199: a read not complete showing one holder ends
                    // nothing.
                    self.phase = Phase::HealRetry(None);
                    Step::Retry
                }
            }
            Phase::HealSecond(first) => {
                let second = self.holding(r);
                match heal(&self.own, self.election, &first, &second) {
                    Heal::StepDown(low) => self.step_down(StepDownWhy::LowerHolder(low)),
                    Heal::Stay(Stay::NoSplit) if !r.complete => {
                        self.phase = Phase::HealRetry(Some(first));
                        Step::Retry
                    }
                    Heal::Stay(_) => {
                        self.phase = Phase::Serving;
                        Step::Nothing
                    }
                }
            }
            _ => return Step::Nothing,
        };
        self.decide_kept(step)
    }

    /// Another instance's interface token appeared (after the own-id
    /// filter).
    fn on_appeared(&mut self) -> Step {
        if self.election != Election::Claim {
            return Step::Nothing;
        }
        match self.phase {
            Phase::Serving => {
                self.phase = Phase::HealFirst;
                Step::Read
            }
            Phase::Campaigning | Phase::TakingOver | Phase::HealFirst | Phase::HealSecond(_) => {
                self.kept.appeared = true;
                Step::Nothing
            }
            _ => Step::Nothing,
        }
    }

    fn on_withdrawn(&mut self) -> Step {
        match self.phase {
            Phase::Standby { .. } => {
                if self.election == Election::External && !self.lock {
                    Step::Nothing
                } else {
                    self.campaign()
                }
            }
            Phase::Campaigning
            | Phase::SteppingDown(_)
            | Phase::HealFirst
            | Phase::HealSecond(_) => {
                self.kept.withdrawn = true;
                Step::Nothing
            }
            _ => Step::Nothing,
        }
    }

    fn on_timer(&mut self) -> Step {
        match self.phase.clone() {
            Phase::Standby { retry: true } => self.campaign(),
            Phase::HealGrace(first) => {
                self.phase = Phase::HealSecond(first);
                Step::Read
            }
            Phase::HealRetry(first) => {
                self.phase = match first {
                    None => Phase::HealFirst,
                    Some(first) => Phase::HealSecond(first),
                };
                Step::Read
            }
            _ => Step::Nothing,
        }
    }

    fn on_lock(&mut self, held: bool) -> Step {
        if self.election != Election::External {
            return Step::Nothing;
        }
        self.lock = held;
        match self.phase {
            Phase::Standby { .. } if held => self.campaign(),
            Phase::Serving if !held => self.step_down(StepDownWhy::LockLost),
            _ => Step::Nothing,
        }
    }
}

// --- §2.15, §5: what a tool may conclude ---------------------------------------------

/// What a tool read of an address and a governed interface (§2.15): two
/// reads a grace apart.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Reading {
    /// Across a constrained face, where presence does not cross (§2.13).
    pub across_face: bool,
    pub governance: Governance,
    pub first: Read,
    pub second: Read,
}

/// A question of §5, answered in its states (core O5).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Answer {
    Yes,
    No,
    /// The question was put, and the reading could not answer it.
    Unobservable,
    /// Nobody put the question.
    NotAsked,
    /// The interface is not governed.
    NotThisProfiles,
}

impl Answer {
    /// The fixture's token.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Yes => "yes",
            Self::No => "no",
            Self::Unobservable => "unobservable",
            Self::NotAsked => "not_asked",
            Self::NotThisProfiles => "not_this_profiles",
        }
    }
}

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

/// Why an answer is what it is: the fixture's reason (§2.15).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Reason {
    AcrossFace,
    NotGoverned,
    NoContract,
    RevisionsDisagree,
    Absent,
    PresenceIncomplete,
    Split,
    InTransition,
    One,
    NoDescriptor,
    NoCandidate,
    Vacant,
    Held,
    Lasting,
    StandingBy,
    NoneStanding,
    SplitLowest,
    NotACandidate,
    Unlisted,
    Incumbent,
    MarkedHolding,
    Standby,
    Unmarked,
}

impl Reason {
    /// The fixture's token.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::AcrossFace => "across_face",
            Self::NotGoverned => "not_governed",
            Self::NoContract => "no_contract",
            Self::RevisionsDisagree => "revisions_disagree",
            Self::Absent => "absent",
            Self::PresenceIncomplete => "presence_incomplete",
            Self::Split => "split",
            Self::InTransition => "in_transition",
            Self::One => "one",
            Self::NoDescriptor => "no_descriptor",
            Self::NoCandidate => "no_candidate",
            Self::Vacant => "vacant",
            Self::Held => "held",
            Self::Lasting => "lasting",
            Self::StandingBy => "standing_by",
            Self::NoneStanding => "none",
            Self::SplitLowest => "split_lowest",
            Self::NotACandidate => "not_a_candidate",
            Self::Unlisted => "unlisted",
            Self::Incumbent => "incumbent",
            Self::MarkedHolding => "marked_holding",
            Self::Standby => "standby",
            Self::Unmarked => "unmarked",
        }
    }
}

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

/// An answer and its reason.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Judged {
    pub answer: Answer,
    pub reason: Reason,
}

impl Judged {
    const fn new(answer: Answer, reason: Reason) -> Self {
        Self { answer, reason }
    }
}

/// How many stand by (§2.15): exact, or a lower bound.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Count {
    pub count: usize,
    pub exact: bool,
}

/// §2.15's premises, in order: a face, the presence of the address (or of
/// one instance) in the second read, the governance.
fn premises_of(r: &Reading, present: bool) -> Result<(), Judged> {
    use Answer::{NotAsked, NotThisProfiles, Unobservable};
    if r.across_face {
        return Err(Judged::new(Unobservable, Reason::AcrossFace));
    }
    if !present {
        return Err(if r.second.complete {
            Judged::new(NotAsked, Reason::Absent)
        } else {
            Judged::new(Unobservable, Reason::PresenceIncomplete)
        });
    }
    match r.governance {
        Governance::Governed(_) => Ok(()),
        Governance::NotGoverned => Err(Judged::new(NotThisProfiles, Reason::NotGoverned)),
        Governance::NoContract => Err(Judged::new(Unobservable, Reason::NoContract)),
        Governance::Disagree => Err(Judged::new(Unobservable, Reason::RevisionsDisagree)),
    }
}

fn address_premises(r: &Reading) -> Result<(), Judged> {
    premises_of(r, !r.second.instances.is_empty())
}

/// A read with no candidate: `no_descriptor` when a descriptor in it was
/// not read, else `no_candidate`. `None` when it holds a candidate.
fn no_candidate(read: &Read) -> Option<Judged> {
    if read.instances.iter().any(|s| s.descriptor.lists()) {
        return None;
    }
    Some(
        if read
            .instances
            .iter()
            .any(|s| s.descriptor == Listing::Unread)
        {
            Judged::new(Answer::Unobservable, Reason::NoDescriptor)
        } else {
            Judged::new(Answer::NotAsked, Reason::NoCandidate)
        },
    )
}

/// §5's first question, "is the address served by exactly one
/// incumbent?" (§2.15). No is the finding.
#[must_use]
pub fn one_incumbent(r: &Reading) -> Judged {
    use Answer::{No, Unobservable, Yes};
    if let Err(j) = address_premises(r) {
        return j;
    }
    let h1 = r.first.holders().len();
    let h2 = r.second.holders().len();
    if h1 >= 2 && h2 >= 2 {
        return Judged::new(No, Reason::Split);
    }
    if h2 >= 2 {
        return Judged::new(Unobservable, Reason::InTransition);
    }
    if h2 == 1 {
        return if r.second.complete {
            Judged::new(Yes, Reason::One)
        } else {
            Judged::new(Unobservable, Reason::PresenceIncomplete)
        };
    }
    if !r.second.complete {
        return Judged::new(Unobservable, Reason::PresenceIncomplete);
    }
    if let Some(j) = no_candidate(&r.second) {
        return j;
    }
    if h1 >= 1 {
        return Judged::new(Unobservable, Reason::InTransition);
    }
    if !r.first.complete {
        return Judged::new(Unobservable, Reason::PresenceIncomplete);
    }
    Judged::new(No, Reason::Vacant)
}

/// §5's second question, "is there a vacancy?" (§2.15). Yes, `lasting`, is
/// the finding; `vacant` is a takeover due.
#[must_use]
pub fn vacancy(r: &Reading) -> Judged {
    use Answer::{No, Unobservable, Yes};
    if let Err(j) = address_premises(r) {
        return j;
    }
    if !r.second.holders().is_empty() {
        return Judged::new(No, Reason::Held);
    }
    if !r.second.complete {
        return Judged::new(Unobservable, Reason::PresenceIncomplete);
    }
    if let Some(j) = no_candidate(&r.second) {
        return j;
    }
    if r.first.complete && r.first.holders().is_empty() {
        Judged::new(Yes, Reason::Lasting)
    } else {
        Judged::new(Yes, Reason::Vacant)
    }
}

/// §5's third question, "does any instance stand by, and how many?"
/// (§2.15): the instances of the second read marked standby and holding no
/// token. Informative.
#[must_use]
pub fn standbys(r: &Reading) -> (Judged, Count) {
    use Answer::{No, Unobservable, Yes};
    let unknown = Count {
        count: 0,
        exact: false,
    };
    if let Err(j) = address_premises(r) {
        return (j, unknown);
    }
    let read = &r.second;
    let n = read
        .instances
        .iter()
        .filter(|s| !s.token && s.descriptor == Listing::Listed { standby: true })
        .count();
    let unread = read
        .instances
        .iter()
        .any(|s| s.descriptor == Listing::Unread);
    if n >= 1 {
        return (
            Judged::new(Yes, Reason::StandingBy),
            Count {
                count: n,
                exact: read.complete && !unread,
            },
        );
    }
    if !read.complete {
        return (
            Judged::new(Unobservable, Reason::PresenceIncomplete),
            unknown,
        );
    }
    if unread {
        return (Judged::new(Unobservable, Reason::NoDescriptor), unknown);
    }
    (
        Judged::new(No, Reason::NoneStanding),
        Count {
            count: 0,
            exact: true,
        },
    )
}

/// §5's fourth question, "is this instance a standby, or a twin?"
/// (§2.15). Yes, a twin, is the finding.
#[must_use]
pub fn twin(r: &Reading, x: &InstanceId) -> Judged {
    use Answer::{No, NotAsked, Unobservable, Yes};
    let x2 = r.second.get(x);
    if let Err(j) = premises_of(r, x2.is_some()) {
        return j;
    }
    let Some(x2) = x2 else {
        return Judged::new(NotAsked, Reason::Absent);
    };
    let x1 = r.first.get(x);
    let h1 = r.first.holders();
    let h2 = r.second.holders();
    let same = x1.is_some_and(|a| a.token == x2.token && a.descriptor == x2.descriptor);
    if x2.token && x1.is_some_and(|a| a.token) && h1.len() >= 2 && h2.len() >= 2 {
        return if h2.first() == Some(&x) {
            Judged::new(Yes, Reason::SplitLowest)
        } else {
            Judged::new(Yes, Reason::Split)
        };
    }
    match x2.descriptor {
        Listing::Unread => Judged::new(Unobservable, Reason::NoDescriptor),
        Listing::NotListed if !x2.token => Judged::new(NotAsked, Reason::NotACandidate),
        Listing::NotListed if same => Judged::new(Yes, Reason::Unlisted),
        Listing::NotListed => Judged::new(Unobservable, Reason::InTransition),
        Listing::Listed { standby } if x2.token => {
            if h2.len() >= 2 {
                Judged::new(Unobservable, Reason::InTransition)
            } else if standby {
                if same {
                    Judged::new(Yes, Reason::MarkedHolding)
                } else {
                    Judged::new(Unobservable, Reason::InTransition)
                }
            } else if r.second.complete {
                Judged::new(No, Reason::Incumbent)
            } else {
                Judged::new(Unobservable, Reason::PresenceIncomplete)
            }
        }
        Listing::Listed { standby: true } => {
            if r.second.complete {
                Judged::new(No, Reason::Standby)
            } else {
                Judged::new(Unobservable, Reason::PresenceIncomplete)
            }
        }
        Listing::Listed { standby: false } => {
            if !same {
                Judged::new(Unobservable, Reason::InTransition)
            } else if r.first.complete && r.second.complete {
                Judged::new(Yes, Reason::Unmarked)
            } else {
                Judged::new(Unobservable, Reason::PresenceIncomplete)
            }
        }
    }
}

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

    fn id(n: u64) -> InstanceId {
        InstanceId::from_u64(n)
    }

    fn seen(n: u64, token: bool, descriptor: Listing) -> Seen {
        Seen {
            id: id(n),
            token,
            descriptor,
        }
    }

    const STANDBY: Listing = Listing::Listed { standby: true };
    const SERVING: Listing = Listing::Listed { standby: false };

    fn read(complete: bool, instances: Vec<Seen>) -> Read {
        Read {
            complete,
            instances,
        }
    }

    #[test]
    fn ids_compare_as_unsigned_integers() {
        assert!(lower(&id(0x9), &id(0xa)));
        assert!(lower(&id(0xff), &id(0x100)));
        assert!(!lower(&id(5), &id(5)));
    }

    #[test]
    fn a_live_incumbent_wins_whatever_the_read() {
        let r = read(false, vec![seen(9, true, Listing::Unread)]);
        assert_eq!(elect(&id(1), &r), Decision::StandBy(Why::Incumbent(id(9))));
    }

    #[test]
    fn the_lowest_candidate_wins_a_complete_read() {
        let own = id(5);
        let r = read(true, vec![seen(5, false, STANDBY), seen(7, false, STANDBY)]);
        assert_eq!(elect(&own, &r), Decision::TakeOver);
        let r = read(
            true,
            vec![
                seen(2, false, Listing::NotListed),
                seen(3, false, STANDBY),
                seen(5, false, STANDBY),
            ],
        );
        assert_eq!(
            elect(&own, &r),
            Decision::StandBy(Why::LowerCandidate(id(3)))
        );
        let r = read(true, vec![seen(3, false, STANDBY)]);
        assert_eq!(elect(&own, &r), Decision::StandBy(Why::OwnClaimMissing));
    }

    #[test]
    fn a_split_leaves_the_lowest() {
        let two = [id(2), id(8)];
        assert_eq!(
            heal(&id(8), Election::Claim, &two, &two),
            Heal::StepDown(id(2))
        );
        assert_eq!(
            heal(&id(2), Election::Claim, &two, &two),
            Heal::Stay(Stay::Lowest)
        );
        assert_eq!(
            heal(&id(8), Election::External, &two, &two),
            Heal::Stay(Stay::External)
        );
        assert_eq!(
            heal(&id(8), Election::Claim, &[id(8)], &two),
            Heal::Stay(Stay::NoSplit)
        );
    }

    #[test]
    fn a_standby_takes_over_at_a_vacancy_and_steps_down_to_a_lower_one() {
        let mut e = Elector::new(id(8), Election::Claim);
        assert_eq!(e.start(), Step::Claim);
        assert_eq!(e.finish(), Step::Read);
        let incumbent = read(true, vec![seen(4, true, SERVING), seen(8, false, STANDBY)]);
        assert_eq!(
            e.observe(Event::Read(incumbent)),
            Step::StandBy {
                why: Why::Incumbent(id(4)),
                retry: false
            }
        );
        assert_eq!(e.role(), Role::Standby);
        assert_eq!(e.observe(Event::Timer), Step::Nothing);
        assert_eq!(e.observe(Event::Withdrawn), Step::Read);
        let vacant = read(true, vec![seen(8, false, STANDBY)]);
        assert_eq!(e.observe(Event::Read(vacant)), Step::TakeOver);
        assert_eq!(e.finish(), Step::Nothing);
        assert!(e.serving());
        assert_eq!(e.observe(Event::Appeared(id(2))), Step::Read);
        let split = read(false, vec![seen(2, true, SERVING), seen(8, true, SERVING)]);
        assert_eq!(e.observe(Event::Read(split.clone())), Step::Grace);
        assert_eq!(e.observe(Event::Timer), Step::Read);
        assert_eq!(
            e.observe(Event::Read(split)),
            Step::StepDown(StepDownWhy::LowerHolder(id(2)))
        );
        assert_eq!(e.finish(), Step::Nothing);
        assert_eq!(e.role(), Role::Standby);
    }

    #[test]
    fn external_serves_only_with_its_lock() {
        let mut e = Elector::new(id(1), Election::External);
        assert_eq!(e.start(), Step::Claim);
        assert_eq!(
            e.finish(),
            Step::StandBy {
                why: Why::NoLock,
                retry: false
            }
        );
        assert_eq!(e.observe(Event::Withdrawn), Step::Nothing);
        assert_eq!(e.observe(Event::Lock(true)), Step::Read);
        let alone = read(true, vec![seen(1, false, STANDBY), seen(9, false, STANDBY)]);
        assert_eq!(e.observe(Event::Read(alone)), Step::TakeOver);
        assert_eq!(e.finish(), Step::Nothing);
        assert_eq!(e.observe(Event::Appeared(id(0))), Step::Nothing);
        assert_eq!(
            e.observe(Event::Lock(false)),
            Step::StepDown(StepDownWhy::LockLost)
        );
    }

    /// A read asked is never asked again, nor dropped, before its answer
    /// (#841 RB's review): from every state the machine reaches, while a
    /// read it asked is unanswered, every event but that read's answer
    /// returns `nothing`, so a runtime never holds two reads at once and no
    /// phase waiting on a read (campaigning, a heal's first or second read)
    /// is stranded. Explored breadth-first over every reachable state, for
    /// both elections; after an act (a claim, a takeover, a step-down) the
    /// runtime's next call is `finish`, as the reference's loop makes it.
    #[test]
    fn a_read_asked_is_never_asked_again_before_its_answer() {
        use std::collections::{HashSet, VecDeque};
        let own = id(0xb);
        let reads = [
            read(true, vec![seen(0xb, false, STANDBY)]),
            read(
                true,
                vec![seen(0xa, true, SERVING), seen(0xb, false, STANDBY)],
            ),
            read(false, vec![]),
            read(
                true,
                vec![seen(0xa, true, SERVING), seen(0xb, true, SERVING)],
            ),
            read(false, vec![seen(0xb, true, SERVING)]),
            read(
                true,
                vec![seen(0x1, false, STANDBY), seen(0xb, false, STANDBY)],
            ),
            read(true, vec![seen(0xb, true, SERVING)]),
        ];
        let others = [
            Event::Appeared(id(0xa)),
            Event::Appeared(own.clone()),
            Event::Withdrawn,
            Event::Timer,
            Event::Lock(true),
            Event::Lock(false),
            Event::Reminted(id(0xc)),
        ];
        for election in [Election::Claim, Election::External] {
            let mut e = Elector::new(own.clone(), election);
            assert_eq!(e.start(), Step::Claim);
            // (machine, a read asked and unanswered, an act to finish)
            let mut queue = VecDeque::from([(e.clone(), false, true)]);
            let mut seen_states = HashSet::new();
            let mut asked_states = 0;
            while let Some((e, asked, acting)) = queue.pop_front() {
                if !seen_states.insert(format!("{e:?} {asked} {acting}")) {
                    continue;
                }
                let mut next = |e: Elector, step: Step, was_asked: bool| {
                    let act = matches!(step, Step::Claim | Step::TakeOver | Step::StepDown(_));
                    let asks = step == Step::Read;
                    assert!(
                        !(was_asked && asks),
                        "{election}: a second read asked while one runs: {e:?}"
                    );
                    queue.push_back((e, was_asked || asks, act));
                };
                if acting {
                    let mut e2 = e.clone();
                    let step = e2.finish();
                    next(e2, step, asked);
                    continue;
                }
                if asked {
                    asked_states += 1;
                    for ev in &others {
                        let mut e2 = e.clone();
                        let step = e2.observe(ev.clone());
                        assert_eq!(
                            step,
                            Step::Nothing,
                            "{election}: {ev:?} while a read runs, in {e:?}"
                        );
                        assert_eq!(e2.finish(), Step::Nothing, "no act runs while a read does");
                        next(e2, Step::Nothing, true);
                    }
                    for r in &reads {
                        let mut e2 = e.clone();
                        let step = e2.observe(Event::Read(r.clone()));
                        // The answer is taken: a phase waiting on it moves on.
                        next(e2, step, false);
                    }
                } else {
                    for ev in &others {
                        let mut e2 = e.clone();
                        let step = e2.observe(ev.clone());
                        next(e2, step, false);
                    }
                }
            }
            assert!(
                asked_states > 0,
                "{election}: the exploration reached reads asked"
            );
        }
    }

    #[test]
    fn a_tool_tells_a_standby_from_a_twin() {
        let one = read(true, vec![seen(4, true, SERVING), seen(8, false, STANDBY)]);
        let r = Reading {
            across_face: false,
            governance: Governance::Governed(Election::Claim),
            first: one.clone(),
            second: one,
        };
        assert_eq!(one_incumbent(&r).reason, Reason::One);
        assert_eq!(vacancy(&r).reason, Reason::Held);
        assert_eq!(
            standbys(&r).1,
            Count {
                count: 1,
                exact: true
            }
        );
        assert_eq!(twin(&r, &id(4)).reason, Reason::Incumbent);
        assert_eq!(twin(&r, &id(8)).reason, Reason::Standby);
        let split = read(false, vec![seen(4, true, SERVING), seen(8, true, SERVING)]);
        let r = Reading {
            first: split.clone(),
            second: split,
            ..r
        };
        assert_eq!(one_incumbent(&r).reason, Reason::Split);
        assert_eq!(twin(&r, &id(4)).reason, Reason::SplitLowest);
        assert_eq!(twin(&r, &id(8)).reason, Reason::Split);
    }
}