automation-structures 0.2.1

Reusable, formally specified building blocks for composing automation systems.
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
//! Checked public entry points for ordinary Rust consumers.
//!
//! Proof-oriented carriers mirror formal actions and use Verus preconditions.
//! These public types keep their carrier invariants internal and convert every
//! caller-controlled action guard into an executable result.

use crate::connectives::cursor::Cursor as CursorCarrier;
use crate::primitives::actuation_pass::ActuationPass as ActuationPassCarrier;
use crate::primitives::audit_sink::AuditSink as AuditSinkCarrier;
use crate::primitives::backtracking_traversal::BacktrackingTraversal as BacktrackingTraversalCarrier;
use crate::primitives::budget::Budget as BudgetCarrier;
use crate::primitives::competitive_selection::{
    CompetitiveSelectionHard as CompetitiveSelectionHardCarrier,
    CompetitiveSelectionHardExclusive as CompetitiveSelectionHardExclusiveCarrier,
    CompetitiveSelectionRanked as CompetitiveSelectionRankedCarrier,
    CompetitiveSelectionSoft as CompetitiveSelectionSoftCarrier,
};
use crate::primitives::convergence_governor_phase_aware::ConvergenceGovernorPhaseAware as ConvergenceGovernorCarrier;
use crate::primitives::propagation_pass::PropagationPass as PropagationPassCarrier;
use crate::primitives::quality_hierarchy::QualityHierarchy as QualityHierarchyCarrier;
use crate::primitives::resource_registry::ResourceRegistry as RegistryCarrier;
use vstd::prelude::*;

pub use crate::primitives::convergence_governor_phase_aware::{
    GovState as ConvergenceState, Phase as ConvergencePhase,
};
pub use crate::primitives::propagation_pass::Round as PropagationRound;

verus! {

/// A disabled budget transition.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum BudgetError {
    /// The requested amount exceeds the held reservation.
    AmountExceedsReservation,
    /// The requested amount exceeds the committed allocation.
    AmountExceedsAllocation,
    /// The requested amount exceeds the pending eviction amount.
    AmountExceedsPendingEviction,
}

/// A checked budget whose three claims cannot exceed its fixed capacity.
///
/// # Examples
///
/// ```rust
/// use automation_structures::Budget;
///
/// let mut budget = Budget::new(8);
/// assert!(budget.try_allocate(3));
/// assert_eq!(budget.available(), 5);
/// ```
pub struct Budget {
    inner: BudgetCarrier,
}

impl Budget {
    #[verifier::type_invariant]
    closed spec fn well_formed(&self) -> bool {
        self.inner.safety_invariant()
    }

    /// Construct an empty budget with `capacity` units.
    pub fn new(capacity: u64) -> (budget: Self) {
        let inner = BudgetCarrier::new(capacity);
        Self { inner }
    }

    /// Return the fixed budget ceiling.
    pub fn capacity(&self) -> u64 {
        self.inner.capacity
    }

    /// Return units committed for use.
    pub fn allocated(&self) -> u64 {
        self.inner.allocated
    }

    /// Return units held but not committed.
    pub fn reserved(&self) -> u64 {
        self.inner.reserved
    }

    /// Return units currently being reclaimed.
    pub fn pending_eviction(&self) -> u64 {
        self.inner.pending_eviction
    }

    /// Return units not claimed by any budget state.
    pub fn available(&self) -> (available: u64) {
        proof { use_type_invariant(&*self); }
        self.inner.available()
    }

    /// Try to commit unused capacity directly.
    #[must_use]
    pub fn try_allocate(&mut self, amount: u64) -> (accepted: bool) {
        proof { use_type_invariant(&*self); }
        let mut carrier = budget_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        let accepted = carrier.try_allocate(amount);
        core::mem::swap(&mut self.inner, &mut carrier);
        accepted
    }

    /// Try to reserve unused capacity.
    #[must_use]
    pub fn try_reserve(&mut self, amount: u64) -> (accepted: bool) {
        proof { use_type_invariant(&*self); }
        let mut carrier = budget_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        let accepted = carrier.reserve(amount);
        core::mem::swap(&mut self.inner, &mut carrier);
        accepted
    }

    /// Move held capacity into committed allocation.
    ///
    /// # Errors
    ///
    /// Returns [`BudgetError::AmountExceedsReservation`] when `amount` exceeds the held reservation.
    pub fn commit_reservation(&mut self, amount: u64) -> (result: Result<(), BudgetError>) {
        proof { use_type_invariant(&*self); }
        if amount <= self.inner.reserved {
            let mut carrier = budget_sentinel();
            core::mem::swap(&mut self.inner, &mut carrier);
            carrier.commit_reservation(amount);
            core::mem::swap(&mut self.inner, &mut carrier);
            Ok(())
        } else {
            Err(BudgetError::AmountExceedsReservation)
        }
    }

    /// Release committed allocation.
    ///
    /// # Errors
    ///
    /// Returns [`BudgetError::AmountExceedsAllocation`] when `amount` exceeds the allocation.
    pub fn release(&mut self, amount: u64) -> (result: Result<(), BudgetError>) {
        proof { use_type_invariant(&*self); }
        if amount <= self.inner.allocated {
            let mut carrier = budget_sentinel();
            core::mem::swap(&mut self.inner, &mut carrier);
            carrier.release(amount);
            core::mem::swap(&mut self.inner, &mut carrier);
            Ok(())
        } else {
            Err(BudgetError::AmountExceedsAllocation)
        }
    }

    /// Move committed allocation into pending eviction.
    ///
    /// # Errors
    ///
    /// Returns [`BudgetError::AmountExceedsAllocation`] when `amount` exceeds the allocation.
    pub fn mark_eviction(&mut self, amount: u64) -> (result: Result<(), BudgetError>) {
        proof { use_type_invariant(&*self); }
        if amount <= self.inner.allocated {
            let mut carrier = budget_sentinel();
            core::mem::swap(&mut self.inner, &mut carrier);
            carrier.mark_eviction(amount);
            core::mem::swap(&mut self.inner, &mut carrier);
            Ok(())
        } else {
            Err(BudgetError::AmountExceedsAllocation)
        }
    }

    /// Finish reclaiming pending eviction.
    ///
    /// # Errors
    ///
    /// Returns [`BudgetError::AmountExceedsPendingEviction`] when `amount` exceeds pending eviction.
    pub fn complete_eviction(&mut self, amount: u64) -> (result: Result<(), BudgetError>) {
        proof { use_type_invariant(&*self); }
        if amount <= self.inner.pending_eviction {
            let mut carrier = budget_sentinel();
            core::mem::swap(&mut self.inner, &mut carrier);
            carrier.complete_eviction(amount);
            core::mem::swap(&mut self.inner, &mut carrier);
            Ok(())
        } else {
            Err(BudgetError::AmountExceedsPendingEviction)
        }
    }
}

/// A unique-key resource registry.
///
/// # Examples
///
/// ```rust
/// use automation_structures::ResourceRegistry;
///
/// let mut registry = ResourceRegistry::new();
/// assert_eq!(registry.insert(7, 42), None);
/// assert_eq!(registry.get(7), Some(42));
/// ```
pub struct ResourceRegistry {
    inner: RegistryCarrier<u64, u64>,
}

impl ResourceRegistry {
    #[verifier::type_invariant]
    closed spec fn well_formed(&self) -> bool {
        self.inner.unique_mapping()
    }

    /// Construct an empty registry.
    pub fn new() -> (registry: Self) {
        let inner = RegistryCarrier::new();
        Self { inner }
    }

    /// Number of registered keys.
    pub fn len(&self) -> usize {
        self.inner.entries.len()
    }

    /// Whether no keys are registered.
    pub fn is_empty(&self) -> bool {
        self.inner.entries.is_empty()
    }

    /// Look up a registered value.
    pub fn get(&self, key: u64) -> (value: Option<u64>) {
        proof { use_type_invariant(&*self); }
        self.inner.lookup(key)
    }

    /// Insert or replace a key and return its previous value.
    pub fn insert(&mut self, key: u64, value: u64) -> (previous: Option<u64>) {
        proof { use_type_invariant(&*self); }
        let previous = self.inner.lookup(key);
        let mut carrier = registry_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.register(key, value);
        core::mem::swap(&mut self.inner, &mut carrier);
        previous
    }

    /// Remove a key and return its previous value.
    pub fn remove(&mut self, key: u64) -> (previous: Option<u64>) {
        proof { use_type_invariant(&*self); }
        let previous = self.inner.lookup(key);
        match previous {
            Some(value) => {
                let mut carrier = registry_sentinel();
                core::mem::swap(&mut self.inner, &mut carrier);
                carrier.deregister(key);
                core::mem::swap(&mut self.inner, &mut carrier);
                Some(value)
            },
            None => None,
        }
    }

    /// Read an entry by storage index for deterministic inspection.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the registry index is in bounds")]
    pub fn entry(&self, index: usize) -> Option<(u64, u64)> {
        if index < self.inner.entries.len() {
            Some(self.inner.entries[index])
        } else {
            None
        }
    }
}

/// A public immutable audit record.
///
/// # Examples
///
/// ```rust
/// use automation_structures::{AuditRecord, AuditSink};
///
/// let mut sink = AuditSink::new(1);
/// assert!(sink.try_record(9));
/// assert_eq!(sink.record(0), Some(AuditRecord {
///     operation: 9,
///     previous_hash: 0,
///     hash: 10,
/// }));
/// ```
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct AuditRecord {
    /// The operation recorded by the sink.
    pub operation: u64,
    /// The predecessor hash stored in this record.
    pub previous_hash: u64,
    /// The record's concrete model hash.
    pub hash: u64,
}

/// A bounded append-only audit chain.
///
/// # Examples
///
/// ```rust
/// use automation_structures::AuditSink;
///
/// let mut sink = AuditSink::new(2);
/// assert!(sink.try_record(4));
/// assert!(sink.validate());
/// assert_eq!(sink.records().count(), 1);
/// ```
pub struct AuditSink {
    inner: AuditSinkCarrier,
}

impl AuditSink {
    #[verifier::type_invariant]
    closed spec fn well_formed(&self) -> bool {
        self.inner.inv()
    }

    /// Construct an empty sink with a fixed record capacity.
    pub fn new(capacity: usize) -> (sink: Self) {
        let inner = AuditSinkCarrier::new(capacity);
        Self { inner }
    }

    /// Maximum number of retained records.
    pub fn capacity(&self) -> usize {
        self.inner.max_log_len
    }

    /// Number of retained records.
    pub fn len(&self) -> usize {
        self.inner.log.len()
    }

    /// Whether the sink contains no records.
    pub fn is_empty(&self) -> bool {
        self.inner.log.is_empty()
    }

    /// Current chain head.
    pub fn last_hash(&self) -> u64 {
        self.inner.last_hash
    }

    /// Append an operation if capacity remains.
    #[must_use]
    pub fn try_record(&mut self, operation: u64) -> (accepted: bool) {
        proof { use_type_invariant(&*self); }
        let mut carrier = audit_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        let accepted = carrier.record(operation);
        core::mem::swap(&mut self.inner, &mut carrier);
        accepted
    }

    /// Recompute and validate the concrete structural chain.
    pub fn validate(&self) -> (valid: bool) {
        proof { use_type_invariant(&*self); }
        self.inner.validate()
    }

    /// Read an immutable record by index.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the audit index is in bounds")]
    pub fn record(&self, index: usize) -> Option<AuditRecord> {
        if index < self.inner.log.len() {
            let entry = &self.inner.log[index];
            Some(AuditRecord {
                operation: entry.operation,
                previous_hash: entry.prev_hash,
                hash: entry.hash,
            })
        } else {
            None
        }
    }
}

/// A rejected monotone Cursor movement.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum CursorError {
    /// The requested position precedes the retained position.
    Regression,
}

/// A checked retained position for consumer progress.
///
/// # Examples
///
/// ```rust
/// use automation_structures::Cursor;
///
/// let mut cursor = Cursor::new(2);
/// cursor.advance_to(5)?;
/// assert_eq!(cursor.position(), 5);
/// # Ok::<(), automation_structures::CursorError>(())
/// ```
pub struct Cursor {
    inner: CursorCarrier,
}

impl Cursor {
    /// Construct a cursor at `position`.
    pub fn new(position: usize) -> (cursor: Self) {
        Self { inner: CursorCarrier::new(position) }
    }

    /// Read the retained position.
    pub fn position(&self) -> usize {
        self.inner.position
    }

    /// Move monotonically to `position`.
    ///
    /// # Errors
    ///
    /// Returns [`CursorError::Regression`] when `position` precedes the retained position.
    pub fn advance_to(&mut self, position: usize) -> (result: Result<(), CursorError>) {
        if position < self.inner.position {
            return Err(CursorError::Regression);
        }
        self.inner.advance_to(position);
        Ok(())
    }
}

/// Invalid construction input for a propagation pass.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum PropagationBuildError {
    /// An initial value exceeds the declared value ceiling.
    InitialValueOutOfRange,
    /// An edge endpoint is not a node in the initial value vector.
    EdgeEndpointOutOfRange,
}

/// A disabled propagation transition.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum PropagationError {
    /// A node index is outside the admitted graph.
    NodeOutOfRange,
    /// A round is already running.
    RoundAlreadyRunning,
    /// The operation requires a running round.
    RoundNotRunning,
    /// The node has already committed its update in this round.
    NodeAlreadyUpdated,
    /// Not every node has committed an update.
    RoundIncomplete,
    /// The pass is settled or has exhausted its iteration ceiling.
    PassTerminated,
    /// The pass is not yet settled and has not reached its ceiling.
    PassStillRunning,
}

/// A snapshot-local bounded propagation pass.
///
/// # Examples
///
/// ```rust
/// use automation_structures::PropagationPass;
///
/// let mut pass = PropagationPass::new(1, 9, vec![(0, 1)], vec![0, 1])?;
/// pass.start_round()?;
/// pass.update_node(0)?;
/// pass.update_node(1)?;
/// pass.end_round()?;
/// assert_eq!(pass.values(), &[0, 0]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct PropagationPass {
    inner: PropagationPassCarrier,
}

impl PropagationPass {
    #[verifier::type_invariant]
    closed spec fn well_formed(&self) -> bool {
        self.inner.inv()
    }

    /// Validate and construct a pass. The node universe is the initial value length.
    ///
    /// # Errors
    ///
    /// Returns [`PropagationBuildError`] when a value exceeds `max_value` or an edge endpoint is absent.
    pub fn new(
        max_iterations: u64,
        max_value: u64,
        edges: Vec<(usize, usize)>,
        initial_values: Vec<u64>,
    ) -> (result: Result<Self, PropagationBuildError>) {
        if !values_within_max(&initial_values, max_value) {
            return Err(PropagationBuildError::InitialValueOutOfRange);
        }
        let num_nodes = initial_values.len();
        if !edges_within_nodes(&edges, num_nodes) {
            return Err(PropagationBuildError::EdgeEndpointOutOfRange);
        }
        let inner = PropagationPassCarrier::new(
            num_nodes,
            max_iterations,
            max_value,
            edges,
            initial_values,
        );
        Ok(Self { inner })
    }

    /// Number of admitted nodes.
    pub fn num_nodes(&self) -> usize {
        self.inner.num_nodes
    }

    /// Maximum charged rounds.
    pub fn max_iterations(&self) -> u64 {
        self.inner.max_iterations
    }

    /// Largest admitted node value.
    pub fn max_value(&self) -> u64 {
        self.inner.max_value
    }

    /// Number of completed rounds.
    pub fn iteration(&self) -> u64 {
        self.inner.iteration
    }

    /// Current round phase.
    pub fn round(&self) -> PropagationRound {
        self.inner.round
    }

    /// Whether the previous completed round changed a value.
    pub fn changed(&self) -> bool {
        self.inner.changed
    }

    /// Read a current node value.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the node index is in bounds")]
    pub fn value(&self, node: usize) -> Option<u64> {
        if node < self.inner.values.len() {
            Some(self.inner.values[node])
        } else {
            None
        }
    }

    /// Read the round-start value for a node.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the snapshot index is in bounds")]
    pub fn snapshot_value(&self, node: usize) -> Option<u64> {
        if node < self.inner.snapshot.len() {
            Some(self.inner.snapshot[node])
        } else {
            None
        }
    }

    /// Whether a node has committed its update in the current round.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the update index is in bounds")]
    pub fn node_updated(&self, node: usize) -> Option<bool> {
        if node < self.inner.updated.len() {
            Some(self.inner.updated[node])
        } else {
            None
        }
    }

    /// Begin a new snapshot round.
    ///
    /// # Errors
    ///
    /// Returns [`PropagationError`] when a round is active or the pass has terminated.
    pub fn start_round(&mut self) -> (result: Result<(), PropagationError>) {
        proof { use_type_invariant(&*self); }
        match self.inner.round {
            PropagationRound::Running => {
                return Err(PropagationError::RoundAlreadyRunning);
            },
            PropagationRound::Idle => {},
        }
        if !self.inner.changed || self.inner.iteration >= self.inner.max_iterations {
            return Err(PropagationError::PassTerminated);
        }
        let mut carrier = propagation_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.start_round();
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }

    /// Commit one node's snapshot-local update.
    ///
    /// # Errors
    ///
    /// Returns [`PropagationError`] for an invalid node, inactive pass, or duplicate node update.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the update index is in bounds")]
    pub fn update_node(&mut self, node: usize) -> (result: Result<(), PropagationError>) {
        proof { use_type_invariant(&*self); }
        match self.inner.round {
            PropagationRound::Idle => {
                return Err(PropagationError::RoundNotRunning);
            },
            PropagationRound::Running => {},
        }
        if node >= self.inner.num_nodes {
            return Err(PropagationError::NodeOutOfRange);
        }
        if self.inner.updated[node] {
            return Err(PropagationError::NodeAlreadyUpdated);
        }
        let mut carrier = propagation_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.update_node(node);
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }

    /// Finish a fully updated round and charge one iteration.
    ///
    /// # Errors
    ///
    /// Returns [`PropagationError`] unless every node was updated in the active round.
    pub fn end_round(&mut self) -> (result: Result<(), PropagationError>) {
        proof { use_type_invariant(&*self); }
        match self.inner.round {
            PropagationRound::Idle => {
                return Err(PropagationError::RoundNotRunning);
            },
            PropagationRound::Running => {},
        }
        if !self.inner.all_nodes_updated() {
            return Err(PropagationError::RoundIncomplete);
        }
        let mut carrier = propagation_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.end_round();
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }

    /// Confirm the terminal self-loop at settlement or the iteration ceiling.
    ///
    /// # Errors
    ///
    /// Returns [`PropagationError`] while a round is active or the pass is not terminal.
    pub fn terminate(&mut self) -> (result: Result<(), PropagationError>) {
        proof { use_type_invariant(&*self); }
        match self.inner.round {
            PropagationRound::Running => {
                return Err(PropagationError::RoundAlreadyRunning);
            },
            PropagationRound::Idle => {},
        }
        if self.inner.changed && self.inner.iteration != self.inner.max_iterations {
            return Err(PropagationError::PassStillRunning);
        }
        let mut carrier = propagation_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.terminate();
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }
}

/// A disabled actuation transition.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ActuationError {
    /// A seat index is outside the admitted seat universe.
    SeatOutOfRange,
    /// The pass has already committed closure.
    PassComplete,
    /// The seat already holds a resource.
    SeatAlreadyAllocated,
    /// The seat holds no resource.
    SeatUnallocated,
    /// The seat has already committed its effect.
    SeatAlreadyActuated,
    /// At least one allocated seat has not committed its effect.
    PassIncomplete,
}

/// A governed resource actuation pass.
///
/// # Examples
///
/// ```rust
/// use automation_structures::ActuationPass;
///
/// let mut pass = ActuationPass::new(vec![Some(11)]);
/// pass.actuate(0)?;
/// pass.finish()?;
/// assert_eq!(pass.effects(), &[Some(11)]);
/// # Ok::<(), automation_structures::ActuationError>(())
/// ```
pub struct ActuationPass {
    inner: ActuationPassCarrier,
}

impl ActuationPass {
    #[verifier::type_invariant]
    closed spec fn well_formed(&self) -> bool {
        self.inner.invariant()
    }

    /// Construct a pass over the supplied allocation record.
    pub fn new(allocation: Vec<Option<u64>>) -> (pass: Self) {
        let num_seats = allocation.len();
        let inner = ActuationPassCarrier::new(allocation, num_seats);
        Self { inner }
    }

    /// Number of governed seats.
    pub fn len(&self) -> usize {
        self.inner.num_seats
    }

    /// Whether the pass has no seats.
    pub fn is_empty(&self) -> bool {
        self.inner.num_seats == 0
    }

    /// Whether closure has committed.
    pub fn is_complete(&self) -> bool {
        self.inner.complete
    }

    /// Read the current resource held by a seat.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the allocation index is in bounds")]
    pub fn allocation(&self, seat: usize) -> Option<Option<u64>> {
        if seat < self.inner.allocation.len() {
            Some(self.inner.allocation[seat])
        } else {
            None
        }
    }

    /// Read the resource whose effect has committed for a seat.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the effect index is in bounds")]
    pub fn effect(&self, seat: usize) -> Option<Option<u64>> {
        if seat < self.inner.effects.len() {
            Some(self.inner.effects[seat])
        } else {
            None
        }
    }

    /// Assign an unallocated seat.
    ///
    /// # Errors
    ///
    /// Returns [`ActuationError`] when the seat is invalid, allocated, or the pass is complete.
    pub fn allocate(&mut self, seat: usize, resource: u64) -> (result: Result<(), ActuationError>) {
        proof { use_type_invariant(&*self); }
        if seat >= self.inner.num_seats {
            return Err(ActuationError::SeatOutOfRange);
        }
        if self.inner.complete {
            return Err(ActuationError::PassComplete);
        }
        if !self.inner.can_allocate(seat) {
            return Err(ActuationError::SeatAlreadyAllocated);
        }
        let mut carrier = actuation_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.allocate(seat, resource);
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }

    /// Withdraw a seat that has not committed an effect.
    ///
    /// # Errors
    ///
    /// Returns [`ActuationError`] when the seat cannot be deallocated in the current state.
    pub fn deallocate(&mut self, seat: usize) -> (result: Result<(), ActuationError>) {
        proof { use_type_invariant(&*self); }
        if seat >= self.inner.num_seats {
            return Err(ActuationError::SeatOutOfRange);
        }
        if self.inner.complete {
            return Err(ActuationError::PassComplete);
        }
        if !self.inner.is_allocated(seat) {
            return Err(ActuationError::SeatUnallocated);
        }
        if !self.inner.can_deallocate(seat) {
            return Err(ActuationError::SeatAlreadyActuated);
        }
        let mut carrier = actuation_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.deallocate(seat);
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }

    /// Commit the effect for an allocated seat.
    ///
    /// # Errors
    ///
    /// Returns [`ActuationError`] when the seat cannot be actuated in the current state.
    pub fn actuate(&mut self, seat: usize) -> (result: Result<(), ActuationError>) {
        proof { use_type_invariant(&*self); }
        if seat >= self.inner.num_seats {
            return Err(ActuationError::SeatOutOfRange);
        }
        if self.inner.complete {
            return Err(ActuationError::PassComplete);
        }
        if !self.inner.is_allocated(seat) {
            return Err(ActuationError::SeatUnallocated);
        }
        if !self.inner.can_actuate(seat) {
            return Err(ActuationError::SeatAlreadyActuated);
        }
        let mut carrier = actuation_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.actuate(seat);
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }

    /// Whether every allocated seat has committed an effect.
    pub fn ready_to_finish(&self) -> (ready: bool) {
        proof { use_type_invariant(&*self); }
        self.inner.ready_to_finish_exec()
    }

    /// Commit closure after every allocated seat has committed an effect.
    ///
    /// # Errors
    ///
    /// Returns [`ActuationError`] when the pass is complete or an allocation is not actuated.
    pub fn finish(&mut self) -> (result: Result<(), ActuationError>) {
        proof { use_type_invariant(&*self); }
        if self.inner.complete {
            return Err(ActuationError::PassComplete);
        }
        if !self.inner.ready_to_finish_exec() {
            return Err(ActuationError::PassIncomplete);
        }
        let mut carrier = actuation_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.finish();
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }
}

/// A disabled quality-hierarchy transition.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum QualityHierarchyError {
    /// A node index is outside the admitted node set.
    NodeOutOfRange,
    /// A proposed parent index is outside the admitted node set.
    ParentOutOfRange,
    /// A proposed child index is outside the admitted node set.
    ChildOutOfRange,
    /// A proposed level exceeds the hierarchy ceiling.
    LevelOutOfRange,
    /// A proposed cost exceeds the hierarchy ceiling.
    CostOutOfRange,
    /// Node-property updates require an isolated node.
    NodeNotIsolated,
    /// A node cannot be its own child.
    SelfEdge,
    /// The exact parent-child edge already exists.
    EdgeAlreadyExists,
    /// The proposed child already has a parent.
    ChildAlreadyParented,
    /// Parent level must strictly exceed child level.
    LevelOrderViolation,
    /// Parent cost must not exceed child cost.
    CostOrderViolation,
}

/// A checked refinement forest over levels, costs, parents, and child edges.
///
/// # Examples
///
/// ```rust
/// use automation_structures::QualityHierarchy;
///
/// let mut hierarchy = QualityHierarchy::new(2, 3);
/// hierarchy.set_node_properties(0, 2, 1)?;
/// hierarchy.set_node_properties(1, 1, 2)?;
/// hierarchy.add_child(0, 1)?;
/// assert_eq!(hierarchy.parent(1), Some(0));
/// # Ok::<(), automation_structures::QualityHierarchyError>(())
/// ```
pub struct QualityHierarchy {
    inner: QualityHierarchyCarrier,
}

impl QualityHierarchy {
    #[verifier::type_invariant]
    closed spec fn well_formed(&self) -> bool {
        self.inner.type_invariant()
            && self.inner.strict_level_descent()
            && self.inner.parent_edge_agreement()
            && self.inner.cost_monotonicity()
    }

    /// Construct a discrete hierarchy with no parent-child edges.
    pub fn new(num_nodes: usize, max_level: u64) -> (hierarchy: Self) {
        let inner = QualityHierarchyCarrier::new(num_nodes, max_level);
        Self { inner }
    }

    /// Number of admitted nodes.
    pub fn len(&self) -> usize {
        self.inner.num_nodes
    }

    /// Whether the hierarchy has no nodes.
    pub fn is_empty(&self) -> bool {
        self.inner.num_nodes == 0
    }

    /// Maximum admitted level and cost value.
    pub fn max_level(&self) -> u64 {
        self.inner.max_level
    }

    /// Read one node level.
    pub fn level(&self, node: usize) -> Option<u64> {
        proof { use_type_invariant(&*self); }
        if node < self.inner.num_nodes {
            Some(self.inner.level_of(node))
        } else {
            None
        }
    }

    /// Read one node cost.
    pub fn cost(&self, node: usize) -> Option<u64> {
        proof { use_type_invariant(&*self); }
        if node < self.inner.num_nodes {
            Some(self.inner.cost_of(node))
        } else {
            None
        }
    }

    /// Read one parent, returning `None` for a root or an invalid node.
    pub fn parent(&self, node: usize) -> Option<usize> {
        proof { use_type_invariant(&*self); }
        if node >= self.inner.num_nodes {
            return None;
        }
        let parent = self.inner.parent_of(node);
        if parent == self.inner.num_nodes {
            None
        } else {
            Some(parent)
        }
    }

    /// Number of retained parent-child edges.
    pub fn edge_count(&self) -> usize {
        self.inner.edges.len()
    }

    /// Read one parent-child edge by deterministic carrier order.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the hierarchy edge index is in bounds")]
    pub fn edge(&self, index: usize) -> Option<(usize, usize)> {
        if index < self.inner.edges.len() {
            Some(self.inner.edges[index])
        } else {
            None
        }
    }

    /// Set the level and cost of an isolated node.
    ///
    /// # Errors
    ///
    /// Returns [`QualityHierarchyError`] for an invalid node, invalid value, or non-isolated node.
    pub fn set_node_properties(
        &mut self,
        node: usize,
        level: u64,
        cost: u64,
    ) -> (result: Result<(), QualityHierarchyError>) {
        proof { use_type_invariant(&*self); }
        if node >= self.inner.num_nodes {
            return Err(QualityHierarchyError::NodeOutOfRange);
        }
        if level > self.inner.max_level {
            return Err(QualityHierarchyError::LevelOutOfRange);
        }
        if cost > self.inner.max_level {
            return Err(QualityHierarchyError::CostOutOfRange);
        }
        if !self.inner.can_set_node_properties(node, level, cost) {
            return Err(QualityHierarchyError::NodeNotIsolated);
        }
        let mut carrier = quality_hierarchy_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.set_node_properties(node, level, cost);
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }

    /// Add one admitted parent-child relation.
    ///
    /// # Errors
    ///
    /// Returns [`QualityHierarchyError`] when the edge would violate the refinement forest.
    pub fn add_child(
        &mut self,
        parent: usize,
        child: usize,
    ) -> (result: Result<(), QualityHierarchyError>) {
        proof { use_type_invariant(&*self); }
        if parent >= self.inner.num_nodes {
            return Err(QualityHierarchyError::ParentOutOfRange);
        }
        if child >= self.inner.num_nodes {
            return Err(QualityHierarchyError::ChildOutOfRange);
        }
        if self.inner.can_add_child(parent, child) {
            let mut carrier = quality_hierarchy_sentinel();
            core::mem::swap(&mut self.inner, &mut carrier);
            carrier.add_child(parent, child);
            core::mem::swap(&mut self.inner, &mut carrier);
            return Ok(());
        }
        if parent == child {
            Err(QualityHierarchyError::SelfEdge)
        } else if self.inner.has_edge(parent, child) {
            Err(QualityHierarchyError::EdgeAlreadyExists)
        } else if self.inner.parent_of(child) != self.inner.num_nodes {
            Err(QualityHierarchyError::ChildAlreadyParented)
        } else if self.inner.level_of(parent) <= self.inner.level_of(child) {
            Err(QualityHierarchyError::LevelOrderViolation)
        } else {
            Err(QualityHierarchyError::CostOrderViolation)
        }
    }
}

/// Invalid BacktrackingTraversal construction input.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum BacktrackingBuildError {
    /// The initial auxiliary value must be in the modulo-three domain.
    InitialAuxOutOfRange,
}

/// A disabled BacktrackingTraversal transition.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum BacktrackingError {
    /// Descent is disabled at a full-depth leaf.
    AtLeaf,
    /// The branch choice is outside the admitted branch set.
    ChoiceOutOfRange,
    /// The mutation delta is outside the required inverse-pair domain.
    DeltaOutOfRange,
    /// Visit requires a full-depth leaf.
    NotLeaf,
    /// The current leaf was already recorded.
    AlreadyVisited,
    /// Ascent is disabled at the root.
    AtRoot,
}

/// A checked paired do-undo backtracking traversal.
///
/// # Examples
///
/// ```rust
/// use automation_structures::BacktrackingTraversal;
///
/// let mut traversal = BacktrackingTraversal::new(2, 1, 0)?;
/// traversal.descend(1, 2)?;
/// traversal.visit()?;
/// traversal.ascend()?;
/// assert_eq!(traversal.choices(), &[]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct BacktrackingTraversal {
    inner: BacktrackingTraversalCarrier,
}

impl BacktrackingTraversal {
    #[verifier::type_invariant]
    closed spec fn well_formed(&self) -> bool {
        self.inner.inv()
    }

    /// Validate and construct an empty traversal.
    ///
    /// # Errors
    ///
    /// Returns [`BacktrackingBuildError::InitialAuxOutOfRange`] when `initial_aux` is not in `0..3`.
    pub fn new(
        branch_factor: u64,
        max_depth: usize,
        initial_aux: u64,
    ) -> (result: Result<Self, BacktrackingBuildError>) {
        if initial_aux >= 3 {
            return Err(BacktrackingBuildError::InitialAuxOutOfRange);
        }
        let inner = BacktrackingTraversalCarrier::new(branch_factor, max_depth, initial_aux);
        Ok(Self { inner })
    }

    /// Maximum admitted traversal depth.
    pub fn max_depth(&self) -> usize {
        self.inner.max_depth
    }

    /// Current path depth.
    pub fn depth(&self) -> usize {
        self.inner.path.len()
    }

    /// Current auxiliary state.
    pub fn auxiliary(&self) -> u64 {
        self.inner.aux
    }

    /// Number of recorded leaves.
    pub fn visited_count(&self) -> usize {
        self.inner.visited.len()
    }

    /// Whether the current path is a full-depth leaf.
    pub fn is_leaf(&self) -> bool {
        self.inner.is_leaf_exec()
    }

    /// Read one current branch choice.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the path index is in bounds")]
    pub fn choice(&self, depth: usize) -> Option<u64> {
        if depth < self.inner.path.len() {
            Some(self.inner.path[depth])
        } else {
            None
        }
    }

    /// Descend one level and record the paired undo token.
    ///
    /// # Errors
    ///
    /// Returns [`BacktrackingError`] at a leaf or for an invalid choice or delta.
    pub fn descend(&mut self, choice: u64, delta: u64) -> (result: Result<(), BacktrackingError>) {
        proof { use_type_invariant(&*self); }
        if self.inner.is_leaf_exec() {
            return Err(BacktrackingError::AtLeaf);
        }
        if choice < 1 || choice > self.inner.branch_factor {
            return Err(BacktrackingError::ChoiceOutOfRange);
        }
        if delta < 1 || delta > 2 {
            return Err(BacktrackingError::DeltaOutOfRange);
        }
        let mut carrier = backtracking_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.descend(choice, delta);
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }

    /// Record the current leaf when it has not been visited.
    ///
    /// # Errors
    ///
    /// Returns [`BacktrackingError`] when the traversal is not at a fresh leaf.
    pub fn visit(&mut self) -> (result: Result<(), BacktrackingError>) {
        proof { use_type_invariant(&*self); }
        if !self.inner.is_leaf_exec() {
            return Err(BacktrackingError::NotLeaf);
        }
        if !self.inner.can_visit() {
            return Err(BacktrackingError::AlreadyVisited);
        }
        let mut carrier = backtracking_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.visit();
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }

    /// Ascend one level and restore the paired auxiliary state.
    ///
    /// # Errors
    ///
    /// Returns [`BacktrackingError::AtRoot`] when no parent frame exists.
    pub fn ascend(&mut self) -> (result: Result<(), BacktrackingError>) {
        proof { use_type_invariant(&*self); }
        if !self.inner.can_ascend() {
            return Err(BacktrackingError::AtRoot);
        }
        let mut carrier = backtracking_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.ascend();
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }
}

/// Invalid competitive-selection configuration or input.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum CompetitiveSelectionError {
    /// At least one candidate is required by this selection mode.
    NoCandidates,
    /// A candidate index is outside the admitted candidate set.
    CandidateOutOfRange,
    /// A seat index is outside the admitted seat set.
    SeatOutOfRange,
    /// The selected seat already holds an allocation.
    SeatAlreadyAllocated,
    /// Every candidate is currently allocated to another seat.
    NoCandidateAvailable,
    /// A score is outside the admitted score domain.
    ScoreOutOfRange,
    /// A score replacement has a different candidate count.
    ScoreCountMismatch,
    /// The weight total is smaller than the reserved unit per candidate.
    WeightTotalBelowReservedFloor,
    /// The weight total exceeds the verified arithmetic ceiling.
    WeightTotalOutOfRange,
    /// The declared maximum score exceeds the verified arithmetic ceiling.
    MaxScoreOutOfRange,
    /// Every available soft-selection unit has already been assigned.
    AllocationComplete,
}

/// Lowest-index argmax selection for one set of candidate scores.
///
/// # Examples
///
/// ```rust
/// use automation_structures::CompetitiveSelectionHard;
///
/// let mut selection = CompetitiveSelectionHard::new(2)?;
/// selection.update_score(0, 4)?;
/// selection.update_score(1, 7)?;
/// assert_eq!(selection.evaluate(), 1);
/// # Ok::<(), automation_structures::CompetitiveSelectionError>(())
/// ```
pub struct CompetitiveSelectionHard {
    inner: CompetitiveSelectionHardCarrier,
}

impl CompetitiveSelectionHard {
    #[verifier::type_invariant]
    closed spec fn well_formed(&self) -> bool {
        self.inner.inv() && self.inner.scores.len() >= 1
    }

    /// Construct a selection over `num_candidates` initially zero-valued scores.
    ///
    /// # Errors
    ///
    /// Returns [`CompetitiveSelectionError::NoCandidates`] for an empty candidate universe.
    pub fn new(num_candidates: usize) -> (result: Result<Self, CompetitiveSelectionError>) {
        if num_candidates == 0 {
            return Err(CompetitiveSelectionError::NoCandidates);
        }
        let inner = CompetitiveSelectionHardCarrier::new(num_candidates);
        Ok(Self { inner })
    }

    /// Number of admitted candidates.
    pub fn len(&self) -> usize {
        self.inner.scores.len()
    }

    /// Whether no candidates are admitted. Checked construction makes this always false.
    pub fn is_empty(&self) -> bool {
        false
    }

    /// Read one candidate score.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the candidate index is in bounds")]
    pub fn score(&self, candidate: usize) -> Option<u64> {
        if candidate < self.inner.scores.len() {
            Some(self.inner.scores[candidate])
        } else {
            None
        }
    }

    /// Read the current winner, if the scores have been evaluated.
    pub fn winner(&self) -> Option<usize> {
        self.inner.allocation
    }

    /// Replace one candidate score and invalidate the previous winner.
    ///
    /// # Errors
    ///
    /// Returns [`CompetitiveSelectionError::CandidateOutOfRange`] for an invalid candidate.
    pub fn update_score(
        &mut self,
        candidate: usize,
        score: u64,
    ) -> (result: Result<(), CompetitiveSelectionError>) {
        proof { use_type_invariant(&*self); }
        if candidate >= self.inner.scores.len() {
            return Err(CompetitiveSelectionError::CandidateOutOfRange);
        }
        let mut carrier = hard_selection_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.update_score(candidate, score);
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }

    /// Select the lowest-index candidate among those with the maximum score.
    #[expect(clippy::manual_unwrap_or_default, reason = "the explicit match is supported by the Verus boundary")]
    pub fn evaluate(&mut self) -> (winner: usize) {
        proof { use_type_invariant(&*self); }
        let mut carrier = hard_selection_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.evaluate();
        let winner = match carrier.allocation {
            Some(value) => value,
            None => 0,
        };
        core::mem::swap(&mut self.inner, &mut carrier);
        winner
    }
}

/// Lowest-index argmax selection across seats with exclusive candidates.
///
/// # Examples
///
/// ```rust
/// use automation_structures::CompetitiveSelectionHardExclusive;
///
/// let mut selection = CompetitiveSelectionHardExclusive::new(2, 2, 10)?;
/// selection.update_score(0, 0, 10)?;
/// selection.update_score(1, 0, 9)?;
/// assert_eq!(selection.evaluate(0)?, 0);
/// assert_eq!(selection.candidate_available(1, 0), Some(false));
/// # Ok::<(), automation_structures::CompetitiveSelectionError>(())
/// ```
pub struct CompetitiveSelectionHardExclusive {
    inner: CompetitiveSelectionHardExclusiveCarrier,
}

impl CompetitiveSelectionHardExclusive {
    #[verifier::type_invariant]
    closed spec fn well_formed(&self) -> bool {
        self.inner.inv()
    }

    /// Construct `num_seats` empty allocations over a nonempty candidate set.
    ///
    /// # Errors
    ///
    /// Returns [`CompetitiveSelectionError::NoCandidates`] for an empty candidate universe.
    pub fn new(
        num_seats: usize,
        num_candidates: usize,
        max_score: u64,
    ) -> (result: Result<Self, CompetitiveSelectionError>) {
        if num_candidates == 0 {
            return Err(CompetitiveSelectionError::NoCandidates);
        }
        let inner = CompetitiveSelectionHardExclusiveCarrier::new(
            num_seats,
            num_candidates,
            max_score,
        );
        Ok(Self { inner })
    }

    /// Number of allocation seats.
    pub fn seat_count(&self) -> usize {
        self.inner.num_seats
    }

    /// Number of candidates.
    pub fn candidate_count(&self) -> usize {
        self.inner.num_candidates
    }

    /// Maximum admitted score.
    pub fn max_score(&self) -> u64 {
        self.inner.max_score
    }

    /// Read one seat allocation, or `None` when the seat is invalid or unallocated.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the seat index is in bounds")]
    #[expect(clippy::manual_map, reason = "the explicit match is supported by the Verus boundary")]
    pub fn allocation(&self, seat: usize) -> Option<usize> {
        proof { use_type_invariant(&*self); }
        if seat >= self.inner.num_seats {
            return None;
        }
        match self.inner.allocation[seat] {
            Some(candidate) => Some(candidate as usize),
            None => None,
        }
    }

    /// Read one seat-candidate score.
    #[expect(clippy::indexing_slicing, reason = "the branches prove both score indices are in bounds")]
    pub fn score(&self, seat: usize, candidate: usize) -> Option<u64> {
        proof { use_type_invariant(&*self); }
        if seat >= self.inner.num_seats || candidate >= self.inner.num_candidates {
            None
        } else {
            Some(self.inner.scores[seat][candidate])
        }
    }

    /// Whether a candidate is free for one seat.
    pub fn candidate_available(&self, seat: usize, candidate: usize) -> Option<bool> {
        proof { use_type_invariant(&*self); }
        if seat >= self.inner.num_seats || candidate >= self.inner.num_candidates {
            return None;
        }
        Some(self.inner.candidate_available(seat, candidate))
    }

    /// Replace one score and invalidate every coupled seat allocation.
    ///
    /// # Errors
    ///
    /// Returns [`CompetitiveSelectionError`] for an invalid seat, candidate, or score.
    pub fn update_score(
        &mut self,
        seat: usize,
        candidate: usize,
        score: u64,
    ) -> (result: Result<(), CompetitiveSelectionError>) {
        proof { use_type_invariant(&*self); }
        if seat >= self.inner.num_seats {
            return Err(CompetitiveSelectionError::SeatOutOfRange);
        }
        if candidate >= self.inner.num_candidates {
            return Err(CompetitiveSelectionError::CandidateOutOfRange);
        }
        if score > self.inner.max_score {
            return Err(CompetitiveSelectionError::ScoreOutOfRange);
        }
        let mut carrier = hard_exclusive_selection_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.update_score(seat, candidate, score);
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }

    /// Select the lowest-index available argmax for one empty seat.
    ///
    /// # Errors
    ///
    /// Returns [`CompetitiveSelectionError`] when the seat is invalid, allocated, or has no candidate.
    #[expect(clippy::indexing_slicing, reason = "the guards prove the seat index is in bounds")]
    pub fn evaluate(
        &mut self,
        seat: usize,
    ) -> (result: Result<usize, CompetitiveSelectionError>) {
        proof { use_type_invariant(&*self); }
        if seat >= self.inner.num_seats {
            return Err(CompetitiveSelectionError::SeatOutOfRange);
        }
        if self.inner.allocation[seat].is_some() {
            return Err(CompetitiveSelectionError::SeatAlreadyAllocated);
        }
        if !self.inner.has_available(seat) {
            return Err(CompetitiveSelectionError::NoCandidateAvailable);
        }
        let mut carrier = hard_exclusive_selection_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.evaluate(seat);
        let winner = match carrier.allocation[seat] {
            Some(candidate) => candidate as usize,
            None => 0,
        };
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(winner)
    }
}

/// Reserved-floor sequential Webster apportionment over mutable scores.
///
/// # Examples
///
/// ```rust
/// use automation_structures::CompetitiveSelectionSoft;
///
/// let selection = CompetitiveSelectionSoft::new(vec![3, 1], 4, 3)?;
/// assert_eq!(selection.weights().collect::<Vec<_>>(), vec![3, 1]);
/// # Ok::<(), automation_structures::CompetitiveSelectionError>(())
/// ```
pub struct CompetitiveSelectionSoft {
    inner: CompetitiveSelectionSoftCarrier,
}

impl CompetitiveSelectionSoft {
    #[verifier::type_invariant]
    closed spec fn well_formed(&self) -> bool {
        self.inner.mutable_score_inv()
    }

    /// Construct a complete apportionment for `weight_total` units.
    ///
    /// # Errors
    ///
    /// Returns [`CompetitiveSelectionError`] when scores or weight bounds are invalid.
    pub fn new(
        scores: Vec<u64>,
        weight_total: u64,
        max_score: u64,
    ) -> (result: Result<Self, CompetitiveSelectionError>) {
        if scores.is_empty() {
            return Err(CompetitiveSelectionError::NoCandidates);
        }
        if weight_total > 1_000_000_000 {
            return Err(CompetitiveSelectionError::WeightTotalOutOfRange);
        }
        if max_score > 1_000_000_000 {
            return Err(CompetitiveSelectionError::MaxScoreOutOfRange);
        }
        if weight_total < scores.len() as u64 {
            return Err(CompetitiveSelectionError::WeightTotalBelowReservedFloor);
        }
        if !positive_values_within_max(&scores, max_score) {
            return Err(CompetitiveSelectionError::ScoreOutOfRange);
        }
        let inner = CompetitiveSelectionSoftCarrier::new(scores, weight_total, max_score);
        Ok(Self { inner })
    }

    /// Construct only the reserved floor so awards can be assigned incrementally.
    ///
    /// # Errors
    ///
    /// Returns [`CompetitiveSelectionError`] when scores or weight bounds are invalid.
    pub fn begin(
        scores: Vec<u64>,
        weight_total: u64,
        max_score: u64,
    ) -> (result: Result<Self, CompetitiveSelectionError>) {
        if scores.is_empty() {
            return Err(CompetitiveSelectionError::NoCandidates);
        }
        if weight_total > 1_000_000_000 {
            return Err(CompetitiveSelectionError::WeightTotalOutOfRange);
        }
        if max_score > 1_000_000_000 {
            return Err(CompetitiveSelectionError::MaxScoreOutOfRange);
        }
        if weight_total < scores.len() as u64 {
            return Err(CompetitiveSelectionError::WeightTotalBelowReservedFloor);
        }
        if !positive_values_within_max(&scores, max_score) {
            return Err(CompetitiveSelectionError::ScoreOutOfRange);
        }
        let inner = CompetitiveSelectionSoftCarrier::init(scores, weight_total, max_score);
        Ok(Self { inner })
    }

    /// Number of candidates.
    pub fn len(&self) -> usize {
        self.inner.scores.len()
    }

    /// Whether no candidates are admitted. Checked construction makes this always false.
    pub fn is_empty(&self) -> bool {
        false
    }

    /// Total weight to apportion.
    pub fn weight_total(&self) -> u64 {
        self.inner.weight_total
    }

    /// Maximum admitted score.
    pub fn max_score(&self) -> u64 {
        self.inner.max_score
    }

    /// Read one candidate score.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the candidate index is in bounds")]
    pub fn score(&self, candidate: usize) -> Option<u64> {
        if candidate < self.inner.scores.len() {
            Some(self.inner.scores[candidate])
        } else {
            None
        }
    }

    /// Read one candidate's current derived weight.
    pub fn weight(&self, candidate: usize) -> Option<u64> {
        proof { use_type_invariant(&*self); }
        if candidate < self.inner.extra.len() {
            Some(self.inner.weight_at(candidate))
        } else {
            None
        }
    }

    /// Number of units assigned so far.
    pub fn assigned_weight(&self) -> u64 {
        proof { use_type_invariant(&*self); }
        self.inner.assigned_weight()
    }

    /// Whether every unit has been assigned.
    pub fn is_complete(&self) -> bool {
        self.assigned_weight() == self.inner.weight_total
    }

    /// Award the next available unit to the current lowest-index priority winner.
    ///
    /// # Errors
    ///
    /// Returns [`CompetitiveSelectionError::AllocationComplete`] after every unit is assigned.
    pub fn assign_next(&mut self) -> (result: Result<usize, CompetitiveSelectionError>) {
        proof { use_type_invariant(&*self); }
        if self.inner.assigned_weight() >= self.inner.weight_total {
            return Err(CompetitiveSelectionError::AllocationComplete);
        }
        let mut carrier = soft_selection_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        let winner = carrier.assign_next();
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(winner)
    }

    /// Replace one score and reset every candidate to its reserved unit.
    ///
    /// # Errors
    ///
    /// Returns [`CompetitiveSelectionError`] for an invalid candidate or score.
    pub fn update_score(
        &mut self,
        candidate: usize,
        score: u64,
    ) -> (result: Result<(), CompetitiveSelectionError>) {
        proof { use_type_invariant(&*self); }
        if candidate >= self.inner.scores.len() {
            return Err(CompetitiveSelectionError::CandidateOutOfRange);
        }
        if score < 1 || score > self.inner.max_score {
            return Err(CompetitiveSelectionError::ScoreOutOfRange);
        }
        let mut carrier = soft_selection_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.update_score(candidate, score);
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }
}

/// Stable top-k selection by descending score and ascending candidate index.
///
/// # Examples
///
/// ```rust
/// use automation_structures::CompetitiveSelectionRanked;
///
/// let mut selection = CompetitiveSelectionRanked::new(vec![7, 7, 3], 2, 7)?;
/// selection.select();
/// assert_eq!(selection.selections(), &[true, true, false]);
/// # Ok::<(), automation_structures::CompetitiveSelectionError>(())
/// ```
pub struct CompetitiveSelectionRanked {
    inner: CompetitiveSelectionRankedCarrier,
}

impl CompetitiveSelectionRanked {
    #[verifier::type_invariant]
    closed spec fn well_formed(&self) -> bool {
        self.inner.inv()
    }

    /// Construct an empty selection over the supplied scores.
    ///
    /// # Errors
    ///
    /// Returns [`CompetitiveSelectionError::ScoreOutOfRange`] when a score exceeds `max_score`.
    pub fn new(
        scores: Vec<u64>,
        k: usize,
        max_score: u64,
    ) -> (result: Result<Self, CompetitiveSelectionError>) {
        if !values_within_max(&scores, max_score) {
            return Err(CompetitiveSelectionError::ScoreOutOfRange);
        }
        let inner = CompetitiveSelectionRankedCarrier::new(scores, k, max_score);
        Ok(Self { inner })
    }

    /// Number of candidates.
    pub fn len(&self) -> usize {
        self.inner.scores.len()
    }

    /// Whether the ranked candidate set is empty.
    pub fn is_empty(&self) -> bool {
        self.inner.scores.is_empty()
    }

    /// Requested maximum number of selected candidates.
    pub fn limit(&self) -> usize {
        self.inner.k
    }

    /// Maximum admitted score.
    pub fn max_score(&self) -> u64 {
        self.inner.max_score
    }

    /// Read one candidate score.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the candidate index is in bounds")]
    pub fn score(&self, candidate: usize) -> Option<u64> {
        if candidate < self.inner.scores.len() {
            Some(self.inner.scores[candidate])
        } else {
            None
        }
    }

    /// Whether one candidate is currently selected.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the candidate index is in bounds")]
    pub fn is_selected(&self, candidate: usize) -> Option<bool> {
        if candidate < self.inner.selected.len() {
            Some(self.inner.selected[candidate])
        } else {
            None
        }
    }

    /// Recompute the stable top-k selection.
    pub fn select(&mut self) {
        proof { use_type_invariant(&*self); }
        let mut carrier = ranked_selection_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.select();
        core::mem::swap(&mut self.inner, &mut carrier);
    }

    /// Replace all scores and clear the current selection.
    ///
    /// # Errors
    ///
    /// Returns [`CompetitiveSelectionError`] when the count changes or a score exceeds `max_score`.
    pub fn update_scores(
        &mut self,
        scores: Vec<u64>,
    ) -> (result: Result<(), CompetitiveSelectionError>) {
        proof { use_type_invariant(&*self); }
        if scores.len() != self.inner.scores.len() {
            return Err(CompetitiveSelectionError::ScoreCountMismatch);
        }
        if !values_within_max(&scores, self.inner.max_score) {
            return Err(CompetitiveSelectionError::ScoreOutOfRange);
        }
        let mut carrier = ranked_selection_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        carrier.update_scores(scores);
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(())
    }
}

/// Invalid convergence-governor construction input.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ConvergenceBuildError {
    /// Doubling the convergence threshold would overflow the carrier arithmetic.
    ThresholdOutOfRange,
    /// A moving-average window must retain at least one delta.
    EmptyWindow,
    /// The largest admitted window sum would overflow `u64`.
    WindowSumOutOfRange,
}

/// A disabled convergence-governor transition.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ConvergenceError {
    /// The submitted delta exceeds the configured maximum.
    DeltaOutOfRange,
}

/// A moving-window convergence state machine with peak-aware phases.
///
/// # Examples
///
/// ```rust
/// use automation_structures::ConvergenceGovernor;
///
/// let mut governor = ConvergenceGovernor::new(10, 30, 3, 50)?;
/// assert_eq!(governor.update(12)?, 12);
/// assert_eq!(governor.history_values(), &[12]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct ConvergenceGovernor {
    inner: ConvergenceGovernorCarrier,
}

impl ConvergenceGovernor {
    #[verifier::type_invariant]
    closed spec fn well_formed(&self) -> bool {
        self.inner.inv()
    }

    /// Validate arithmetic bounds and construct an active governor.
    ///
    /// # Errors
    ///
    /// Returns [`ConvergenceBuildError`] when the threshold, window, or maximum delta is invalid.
    pub fn new(
        threshold: u64,
        awaken_threshold: u64,
        window: usize,
        max_delta: u64,
    ) -> (result: Result<Self, ConvergenceBuildError>) {
        if threshold > u64::MAX / 2 {
            return Err(ConvergenceBuildError::ThresholdOutOfRange);
        }
        if window == 0 {
            return Err(ConvergenceBuildError::EmptyWindow);
        }
        if window > 1_000_000_000 || max_delta > 1_000_000_000 {
            return Err(ConvergenceBuildError::WindowSumOutOfRange);
        }
        proof {
            assert(window as int * max_delta as int <= u64::MAX as int) by (nonlinear_arith)
                requires
                    window <= 1_000_000_000,
                    max_delta <= 1_000_000_000,
                    u64::MAX >= 1_000_000_000 * 1_000_000_000;
        }
        let inner = ConvergenceGovernorCarrier::new(
            threshold,
            awaken_threshold,
            window,
            max_delta,
        );
        Ok(Self { inner })
    }

    /// Convergence threshold used by the state transition.
    pub fn threshold(&self) -> u64 {
        self.inner.threshold
    }

    /// Activity threshold that awakens a converged governor.
    pub fn awaken_threshold(&self) -> u64 {
        self.inner.awaken_threshold
    }

    /// Maximum retained history length.
    pub fn window(&self) -> usize {
        self.inner.window
    }

    /// Maximum admitted delta.
    pub fn max_delta(&self) -> u64 {
        self.inner.max_delta
    }

    /// Current convergence state.
    pub fn state(&self) -> ConvergenceState {
        self.inner.state
    }

    /// Current peak-aware gradient phase.
    pub fn phase(&self) -> ConvergencePhase {
        self.inner.gradient_phase
    }

    /// Whether a threshold event has ever been observed.
    pub fn peak_observed(&self) -> bool {
        self.inner.peak_observed
    }

    /// Number of retained deltas.
    pub fn history_len(&self) -> usize {
        self.inner.delta_history.len()
    }

    /// Read one retained delta from oldest to newest.
    #[expect(clippy::indexing_slicing, reason = "the branch proves the history index is in bounds")]
    pub fn history(&self, index: usize) -> Option<u64> {
        if index < self.inner.delta_history.len() {
            Some(self.inner.delta_history[index])
        } else {
            None
        }
    }

    /// Submit one delta, returning the resulting moving-window average.
    ///
    /// # Errors
    ///
    /// Returns [`ConvergenceError::DeltaOutOfRange`] when `delta` exceeds the configured maximum.
    pub fn update(&mut self, delta: u64) -> (result: Result<u64, ConvergenceError>) {
        proof { use_type_invariant(&*self); }
        if delta > self.inner.max_delta {
            return Err(ConvergenceError::DeltaOutOfRange);
        }
        let mut carrier = convergence_sentinel();
        core::mem::swap(&mut self.inner, &mut carrier);
        let average = carrier.update(delta);
        core::mem::swap(&mut self.inner, &mut carrier);
        Ok(average)
    }
}

fn budget_sentinel() -> (carrier: BudgetCarrier)
    ensures carrier.safety_invariant(),
{
    BudgetCarrier::new(0)
}

fn registry_sentinel() -> (carrier: RegistryCarrier<u64, u64>)
    ensures carrier.unique_mapping(),
{
    RegistryCarrier::new()
}

fn audit_sentinel() -> (carrier: AuditSinkCarrier)
    ensures carrier.inv(),
{
    AuditSinkCarrier::new(0)
}

fn propagation_sentinel() -> (carrier: PropagationPassCarrier)
    ensures carrier.inv(),
{
    let edges: Vec<(usize, usize)> = Vec::new();
    let values: Vec<u64> = Vec::new();
    PropagationPassCarrier::new(0, 0, 0, edges, values)
}

fn actuation_sentinel() -> (carrier: ActuationPassCarrier)
    ensures carrier.invariant(),
{
    let allocation: Vec<Option<u64>> = Vec::new();
    ActuationPassCarrier::new(allocation, 0)
}

fn quality_hierarchy_sentinel() -> (carrier: QualityHierarchyCarrier)
    ensures
        carrier.type_invariant(),
        carrier.strict_level_descent(),
        carrier.parent_edge_agreement(),
        carrier.cost_monotonicity(),
{
    QualityHierarchyCarrier::new(0, 0)
}

fn backtracking_sentinel() -> (carrier: BacktrackingTraversalCarrier)
    ensures carrier.inv(),
{
    BacktrackingTraversalCarrier::new(0, 0, 0)
}

fn hard_selection_sentinel() -> (carrier: CompetitiveSelectionHardCarrier)
    ensures
        carrier.inv(),
        carrier.scores.len() >= 1,
{
    CompetitiveSelectionHardCarrier::new(1)
}

fn hard_exclusive_selection_sentinel() -> (carrier: CompetitiveSelectionHardExclusiveCarrier)
    ensures carrier.inv(),
{
    CompetitiveSelectionHardExclusiveCarrier::new(0, 1, 0)
}

fn soft_selection_sentinel() -> (carrier: CompetitiveSelectionSoftCarrier)
    ensures carrier.mutable_score_inv(),
{
    let mut scores: Vec<u64> = Vec::new();
    scores.push(1);
    CompetitiveSelectionSoftCarrier::init(scores, 1, 1)
}

fn ranked_selection_sentinel() -> (carrier: CompetitiveSelectionRankedCarrier)
    ensures carrier.inv(),
{
    let scores: Vec<u64> = Vec::new();
    CompetitiveSelectionRankedCarrier::new(scores, 0, 0)
}

fn convergence_sentinel() -> (carrier: ConvergenceGovernorCarrier)
    ensures carrier.inv(),
{
    ConvergenceGovernorCarrier::new(0, 0, 1, 0)
}

#[expect(clippy::indexing_slicing, reason = "the loop proves the value index is in bounds")]
#[expect(clippy::arithmetic_side_effects, reason = "the loop proves the cursor remains within the vector")]
#[expect(clippy::ptr_arg, reason = "Verus sequence-view contracts are stated over Vec in this checked boundary")]
pub(crate) fn values_within_max(values: &Vec<u64>, max_value: u64) -> (valid: bool)
    ensures
        valid == (forall|i: int| 0 <= i < values.len() ==> values@[i] <= max_value),
{
    let mut index: usize = 0;
    while index < values.len()
        invariant
            index <= values.len(),
            forall|i: int| 0 <= i < index ==> values@[i] <= max_value,
        decreases values.len() - index,
    {
        if values[index] > max_value {
            assert(!(forall|i: int| 0 <= i < values.len() ==> values@[i] <= max_value));
            return false;
        }
        index += 1;
    }
    true
}

#[expect(clippy::indexing_slicing, reason = "the loop proves the value index is in bounds")]
#[expect(clippy::arithmetic_side_effects, reason = "the loop proves the cursor remains within the vector")]
#[expect(clippy::ptr_arg, reason = "Verus sequence-view contracts are stated over Vec in this checked boundary")]
fn positive_values_within_max(values: &Vec<u64>, max_value: u64) -> (valid: bool)
    ensures
        valid == (forall|i: int| 0 <= i < values.len()
            ==> 1 <= #[trigger] values@[i] <= max_value),
{
    let mut index: usize = 0;
    while index < values.len()
        invariant
            index <= values.len(),
            forall|i: int| 0 <= i < index ==> 1 <= #[trigger] values@[i] <= max_value,
        decreases values.len() - index,
    {
        if values[index] < 1 || values[index] > max_value {
            assert(!(forall|i: int| 0 <= i < values.len()
                ==> 1 <= #[trigger] values@[i] <= max_value));
            return false;
        }
        index += 1;
    }
    true
}

#[expect(clippy::indexing_slicing, reason = "the loop proves the edge index is in bounds")]
#[expect(clippy::arithmetic_side_effects, reason = "the loop proves the cursor remains within the vector")]
#[expect(clippy::ptr_arg, reason = "Verus sequence-view contracts are stated over Vec in this checked boundary")]
fn edges_within_nodes(edges: &Vec<(usize, usize)>, num_nodes: usize) -> (valid: bool)
    ensures
        valid == (forall|i: int| 0 <= i < edges.len()
            ==> edges@[i].0 < num_nodes && edges@[i].1 < num_nodes),
{
    let mut index: usize = 0;
    while index < edges.len()
        invariant
            index <= edges.len(),
            forall|i: int| 0 <= i < index
                ==> edges@[i].0 < num_nodes && edges@[i].1 < num_nodes,
        decreases edges.len() - index,
    {
        if edges[index].0 >= num_nodes || edges[index].1 >= num_nodes {
            assert(!(forall|i: int| 0 <= i < edges.len()
                ==> edges@[i].0 < num_nodes && edges@[i].1 < num_nodes));
            return false;
        }
        index += 1;
    }
    true
}

} // verus!

impl Budget {
    /// Whether the budget currently holds no claims.
    pub fn is_empty(&self) -> bool {
        self.allocated() == 0 && self.reserved() == 0 && self.pending_eviction() == 0
    }

    /// Whether every capacity unit is currently claimed.
    pub fn is_full(&self) -> bool {
        self.available() == 0
    }
}

impl ResourceRegistry {
    /// Whether `key` has a registered value.
    pub fn contains_key(&self, key: u64) -> bool {
        self.get(key).is_some()
    }

    /// Borrow registered entries in deterministic storage order.
    pub fn iter(&self) -> impl ExactSizeIterator<Item = &(u64, u64)> {
        self.inner.entries.iter()
    }
}

impl AuditSink {
    /// Whether the sink has reached its record capacity.
    pub fn is_full(&self) -> bool {
        self.len() == self.capacity()
    }

    /// Iterate over immutable records in chain order.
    pub fn records(&self) -> impl ExactSizeIterator<Item = AuditRecord> + '_ {
        self.inner.log.iter().map(|entry| AuditRecord {
            operation: entry.operation,
            previous_hash: entry.prev_hash,
            hash: entry.hash,
        })
    }
}

impl PropagationPass {
    /// Borrow directed propagation edges in configured order.
    pub fn edges(&self) -> &[(usize, usize)] {
        self.inner.edges.as_slice()
    }

    /// Borrow the current node values.
    pub fn values(&self) -> &[u64] {
        self.inner.values.as_slice()
    }

    /// Borrow the snapshot captured for the current or latest round.
    pub fn snapshot_values(&self) -> &[u64] {
        self.inner.snapshot.as_slice()
    }

    /// Borrow the per-node update markers for the current or latest round.
    pub fn updated_nodes(&self) -> &[bool] {
        self.inner.updated.as_slice()
    }
}

impl ActuationPass {
    /// Borrow current seat allocations.
    pub fn allocations(&self) -> &[Option<u64>] {
        self.inner.allocation.as_slice()
    }

    /// Borrow committed seat effects.
    pub fn effects(&self) -> &[Option<u64>] {
        self.inner.effects.as_slice()
    }
}

impl QualityHierarchy {
    /// Borrow all node levels by node index.
    pub fn levels(&self) -> &[u64] {
        self.inner.level.as_slice()
    }

    /// Borrow all node costs by node index.
    pub fn costs(&self) -> &[u64] {
        self.inner.cost.as_slice()
    }

    /// Borrow encoded parent identifiers by node index.
    ///
    /// The sentinel `self.len()` represents a node without a parent.
    pub fn encoded_parents(&self) -> &[usize] {
        self.inner.parent.as_slice()
    }

    /// Borrow parent-child edges in insertion order.
    pub fn edges(&self) -> &[(usize, usize)] {
        self.inner.edges.as_slice()
    }

    /// Whether an in-range node has at least one child.
    pub fn has_children(&self, node: usize) -> Option<bool> {
        (node < self.len()).then(|| self.inner.has_children(node))
    }

    /// Whether an in-range parent-child edge is present.
    pub fn has_edge(&self, parent: usize, child: usize) -> Option<bool> {
        (parent < self.len() && child < self.len()).then(|| self.inner.has_edge(parent, child))
    }
}

impl BacktrackingTraversal {
    /// Number of choices admitted at each non-leaf depth.
    pub fn branch_factor(&self) -> u64 {
        self.inner.branch_factor
    }

    /// Auxiliary value used at the root.
    pub fn initial_auxiliary(&self) -> u64 {
        self.inner.init_aux
    }

    /// Borrow the current choice path.
    pub fn choices(&self) -> &[u64] {
        self.inner.path.as_slice()
    }

    /// Borrow visited leaf paths in visit order.
    pub fn visited_paths(&self) -> impl ExactSizeIterator<Item = &[u64]> {
        self.inner.visited.iter().map(Vec::as_slice)
    }
}

impl CompetitiveSelectionHard {
    /// Borrow candidate scores by candidate index.
    pub fn scores(&self) -> &[u64] {
        self.inner.scores.as_slice()
    }
}

impl CompetitiveSelectionHardExclusive {
    /// Whether no seats are configured.
    pub fn is_empty(&self) -> bool {
        self.seat_count() == 0
    }

    /// Borrow current seat allocations.
    pub fn allocations(&self) -> &[Option<u64>] {
        self.inner.allocation.as_slice()
    }

    /// Borrow one seat's candidate scores.
    pub fn scores(&self, seat: usize) -> Option<&[u64]> {
        self.inner.scores.get(seat).map(Vec::as_slice)
    }
}

impl CompetitiveSelectionSoft {
    /// Borrow candidate scores by candidate index.
    pub fn scores(&self) -> &[u64] {
        self.inner.scores.as_slice()
    }

    /// Iterate over current candidate weights.
    pub fn weights(&self) -> impl ExactSizeIterator<Item = u64> + '_ {
        self.inner.extra.iter().map(|extra| extra + 1)
    }
}

impl CompetitiveSelectionRanked {
    /// Borrow candidate scores by candidate index.
    pub fn scores(&self) -> &[u64] {
        self.inner.scores.as_slice()
    }

    /// Borrow current selection markers by candidate index.
    pub fn selections(&self) -> &[bool] {
        self.inner.selected.as_slice()
    }

    /// Number of currently selected candidates.
    pub fn selected_len(&self) -> usize {
        self.inner
            .selected
            .iter()
            .filter(|selected| **selected)
            .count()
    }
}

impl ConvergenceGovernor {
    /// Borrow retained deltas from oldest to newest.
    pub fn history_values(&self) -> &[u64] {
        self.inner.delta_history.as_slice()
    }
}

impl Default for ResourceRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl_observational_debug!(Budget, "Budget",
    "capacity" => capacity,
    "allocated" => allocated,
    "reserved" => reserved,
    "pending_eviction" => pending_eviction,
    "available" => available,
);
impl_observational_debug!(ResourceRegistry, "ResourceRegistry", "len" => len);
impl_observational_debug!(AuditSink, "AuditSink",
    "capacity" => capacity,
    "len" => len,
    "last_hash" => last_hash,
    "valid" => validate,
);
impl_observational_debug!(Cursor, "Cursor", "position" => position);
impl_observational_debug!(PropagationPass, "PropagationPass",
    "num_nodes" => num_nodes,
    "max_iterations" => max_iterations,
    "iteration" => iteration,
    "round" => round,
    "changed" => changed,
);
impl_observational_debug!(ActuationPass, "ActuationPass",
    "len" => len,
    "complete" => is_complete,
    "ready_to_finish" => ready_to_finish,
);
impl_observational_debug!(QualityHierarchy, "QualityHierarchy",
    "len" => len,
    "max_level" => max_level,
    "edge_count" => edge_count,
);
impl_observational_debug!(BacktrackingTraversal, "BacktrackingTraversal",
    "max_depth" => max_depth,
    "depth" => depth,
    "auxiliary" => auxiliary,
    "visited_count" => visited_count,
    "leaf" => is_leaf,
);
impl_observational_debug!(CompetitiveSelectionHard, "CompetitiveSelectionHard",
    "len" => len,
    "winner" => winner,
);
impl_observational_debug!(CompetitiveSelectionHardExclusive, "CompetitiveSelectionHardExclusive",
    "seat_count" => seat_count,
    "candidate_count" => candidate_count,
    "max_score" => max_score,
);
impl_observational_debug!(CompetitiveSelectionSoft, "CompetitiveSelectionSoft",
    "len" => len,
    "weight_total" => weight_total,
    "assigned_weight" => assigned_weight,
    "max_score" => max_score,
    "complete" => is_complete,
);
impl_observational_debug!(CompetitiveSelectionRanked, "CompetitiveSelectionRanked",
    "len" => len,
    "limit" => limit,
    "max_score" => max_score,
);
impl_observational_debug!(ConvergenceGovernor, "ConvergenceGovernor",
    "threshold" => threshold,
    "awaken_threshold" => awaken_threshold,
    "window" => window,
    "max_delta" => max_delta,
    "state" => state,
    "phase" => phase,
    "peak_observed" => peak_observed,
    "history_len" => history_len,
);

impl_public_error!(BudgetError, {
    Self::AmountExceedsReservation => "amount exceeds the held reservation",
    Self::AmountExceedsAllocation => "amount exceeds the committed allocation",
    Self::AmountExceedsPendingEviction => "amount exceeds pending eviction",
});
impl_public_error!(CursorError, {
    Self::Regression => "cursor movement would regress the retained position",
});
impl_public_error!(PropagationBuildError, {
    Self::InitialValueOutOfRange => "an initial value exceeds the declared value ceiling",
    Self::EdgeEndpointOutOfRange => "an edge endpoint is outside the admitted node set",
});
impl_public_error!(PropagationError, {
    Self::NodeOutOfRange => "node is outside the admitted graph",
    Self::RoundAlreadyRunning => "a propagation round is already running",
    Self::RoundNotRunning => "no propagation round is running",
    Self::NodeAlreadyUpdated => "node already committed an update in this round",
    Self::RoundIncomplete => "not every node committed an update",
    Self::PassTerminated => "propagation pass is settled or exhausted",
    Self::PassStillRunning => "propagation pass has not reached a terminal state",
});
impl_public_error!(ActuationError, {
    Self::SeatOutOfRange => "seat is outside the admitted seat set",
    Self::PassComplete => "actuation pass is already complete",
    Self::SeatAlreadyAllocated => "seat already holds a resource",
    Self::SeatUnallocated => "seat holds no resource",
    Self::SeatAlreadyActuated => "seat already committed its effect",
    Self::PassIncomplete => "an allocated seat has not committed its effect",
});
impl_public_error!(QualityHierarchyError, {
    Self::NodeOutOfRange => "node is outside the admitted hierarchy",
    Self::ParentOutOfRange => "parent is outside the admitted hierarchy",
    Self::ChildOutOfRange => "child is outside the admitted hierarchy",
    Self::LevelOutOfRange => "level exceeds the hierarchy ceiling",
    Self::CostOutOfRange => "cost exceeds the hierarchy ceiling",
    Self::NodeNotIsolated => "node properties may change only while the node is isolated",
    Self::SelfEdge => "a hierarchy node cannot be its own child",
    Self::EdgeAlreadyExists => "the parent-child edge already exists",
    Self::ChildAlreadyParented => "the child already has a parent",
    Self::LevelOrderViolation => "parent level must strictly exceed child level",
    Self::CostOrderViolation => "parent cost must not exceed child cost",
});
impl_public_error!(BacktrackingBuildError, {
    Self::InitialAuxOutOfRange => "initial auxiliary value is outside the modulo-three domain",
});
impl_public_error!(BacktrackingError, {
    Self::AtLeaf => "descent is disabled at a leaf",
    Self::ChoiceOutOfRange => "branch choice is outside the admitted branch set",
    Self::DeltaOutOfRange => "mutation delta must be one or two",
    Self::NotLeaf => "visit requires a full-depth leaf",
    Self::AlreadyVisited => "the current leaf was already visited",
    Self::AtRoot => "ascent is disabled at the root",
});
impl_public_error!(CompetitiveSelectionError, {
    Self::NoCandidates => "at least one candidate is required",
    Self::CandidateOutOfRange => "candidate is outside the admitted candidate set",
    Self::SeatOutOfRange => "seat is outside the admitted seat set",
    Self::SeatAlreadyAllocated => "seat already holds an allocation",
    Self::NoCandidateAvailable => "no candidate is available for the seat",
    Self::ScoreOutOfRange => "score is outside the admitted score domain",
    Self::ScoreCountMismatch => "replacement scores have a different candidate count",
    Self::WeightTotalBelowReservedFloor => "weight total is smaller than the reserved candidate floor",
    Self::WeightTotalOutOfRange => "weight total exceeds the verified arithmetic ceiling",
    Self::MaxScoreOutOfRange => "maximum score exceeds the verified arithmetic ceiling",
    Self::AllocationComplete => "all soft-selection weight has been assigned",
});
impl_public_error!(ConvergenceBuildError, {
    Self::ThresholdOutOfRange => "convergence threshold cannot be doubled safely",
    Self::EmptyWindow => "convergence history window must be nonempty",
    Self::WindowSumOutOfRange => "maximum convergence window sum exceeds u64",
});
impl_public_error!(ConvergenceError, {
    Self::DeltaOutOfRange => "delta exceeds the configured maximum",
});